anv/query: Write both dwords in emit_zero_queries
[mesa.git] / src / intel / vulkan / genX_query.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 #include "genxml/gen_macros.h"
33 #include "genxml/genX_pack.h"
34
35 VkResult genX(CreateQueryPool)(
36 VkDevice _device,
37 const VkQueryPoolCreateInfo* pCreateInfo,
38 const VkAllocationCallbacks* pAllocator,
39 VkQueryPool* pQueryPool)
40 {
41 ANV_FROM_HANDLE(anv_device, device, _device);
42 const struct anv_physical_device *pdevice = &device->instance->physicalDevice;
43 struct anv_query_pool *pool;
44 VkResult result;
45
46 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO);
47
48 /* Query pool slots are made up of some number of 64-bit values packed
49 * tightly together. The first 64-bit value is always the "available" bit
50 * which is 0 when the query is unavailable and 1 when it is available.
51 * The 64-bit values that follow are determined by the type of query.
52 */
53 uint32_t uint64s_per_slot = 1;
54
55 VkQueryPipelineStatisticFlags pipeline_statistics = 0;
56 switch (pCreateInfo->queryType) {
57 case VK_QUERY_TYPE_OCCLUSION:
58 /* Occlusion queries have two values: begin and end. */
59 uint64s_per_slot += 2;
60 break;
61 case VK_QUERY_TYPE_TIMESTAMP:
62 /* Timestamps just have the one timestamp value */
63 uint64s_per_slot += 1;
64 break;
65 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
66 pipeline_statistics = pCreateInfo->pipelineStatistics;
67 /* We're going to trust this field implicitly so we need to ensure that
68 * no unhandled extension bits leak in.
69 */
70 pipeline_statistics &= ANV_PIPELINE_STATISTICS_MASK;
71
72 /* Statistics queries have a min and max for every statistic */
73 uint64s_per_slot += 2 * util_bitcount(pipeline_statistics);
74 break;
75 default:
76 assert(!"Invalid query type");
77 }
78
79 pool = vk_alloc2(&device->alloc, pAllocator, sizeof(*pool), 8,
80 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
81 if (pool == NULL)
82 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
83
84 pool->type = pCreateInfo->queryType;
85 pool->pipeline_statistics = pipeline_statistics;
86 pool->stride = uint64s_per_slot * sizeof(uint64_t);
87 pool->slots = pCreateInfo->queryCount;
88
89 uint64_t size = pool->slots * pool->stride;
90 result = anv_bo_init_new(&pool->bo, device, size);
91 if (result != VK_SUCCESS)
92 goto fail;
93
94 if (pdevice->supports_48bit_addresses)
95 pool->bo.flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
96
97 if (pdevice->use_softpin)
98 pool->bo.flags |= EXEC_OBJECT_PINNED;
99
100 if (pdevice->has_exec_async)
101 pool->bo.flags |= EXEC_OBJECT_ASYNC;
102
103 anv_vma_alloc(device, &pool->bo);
104
105 /* For query pools, we set the caching mode to I915_CACHING_CACHED. On LLC
106 * platforms, this does nothing. On non-LLC platforms, this means snooping
107 * which comes at a slight cost. However, the buffers aren't big, won't be
108 * written frequently, and trying to handle the flushing manually without
109 * doing too much flushing is extremely painful.
110 */
111 anv_gem_set_caching(device, pool->bo.gem_handle, I915_CACHING_CACHED);
112
113 pool->bo.map = anv_gem_mmap(device, pool->bo.gem_handle, 0, size, 0);
114
115 *pQueryPool = anv_query_pool_to_handle(pool);
116
117 return VK_SUCCESS;
118
119 fail:
120 vk_free2(&device->alloc, pAllocator, pool);
121
122 return result;
123 }
124
125 void genX(DestroyQueryPool)(
126 VkDevice _device,
127 VkQueryPool _pool,
128 const VkAllocationCallbacks* pAllocator)
129 {
130 ANV_FROM_HANDLE(anv_device, device, _device);
131 ANV_FROM_HANDLE(anv_query_pool, pool, _pool);
132
133 if (!pool)
134 return;
135
136 anv_gem_munmap(pool->bo.map, pool->bo.size);
137 anv_vma_free(device, &pool->bo);
138 anv_gem_close(device, pool->bo.gem_handle);
139 vk_free2(&device->alloc, pAllocator, pool);
140 }
141
142 static void
143 cpu_write_query_result(void *dst_slot, VkQueryResultFlags flags,
144 uint32_t value_index, uint64_t result)
145 {
146 if (flags & VK_QUERY_RESULT_64_BIT) {
147 uint64_t *dst64 = dst_slot;
148 dst64[value_index] = result;
149 } else {
150 uint32_t *dst32 = dst_slot;
151 dst32[value_index] = result;
152 }
153 }
154
155 static bool
156 query_is_available(uint64_t *slot)
157 {
158 return *(volatile uint64_t *)slot;
159 }
160
161 static VkResult
162 wait_for_available(struct anv_device *device,
163 struct anv_query_pool *pool, uint64_t *slot)
164 {
165 while (true) {
166 if (query_is_available(slot))
167 return VK_SUCCESS;
168
169 int ret = anv_gem_busy(device, pool->bo.gem_handle);
170 if (ret == 1) {
171 /* The BO is still busy, keep waiting. */
172 continue;
173 } else if (ret == -1) {
174 /* We don't know the real error. */
175 device->lost = true;
176 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
177 "gem wait failed: %m");
178 } else {
179 assert(ret == 0);
180 /* The BO is no longer busy. */
181 if (query_is_available(slot)) {
182 return VK_SUCCESS;
183 } else {
184 VkResult status = anv_device_query_status(device);
185 if (status != VK_SUCCESS)
186 return status;
187
188 /* If we haven't seen availability yet, then we never will. This
189 * can only happen if we have a client error where they call
190 * GetQueryPoolResults on a query that they haven't submitted to
191 * the GPU yet. The spec allows us to do anything in this case,
192 * but returning VK_SUCCESS doesn't seem right and we shouldn't
193 * just keep spinning.
194 */
195 return VK_NOT_READY;
196 }
197 }
198 }
199 }
200
201 VkResult genX(GetQueryPoolResults)(
202 VkDevice _device,
203 VkQueryPool queryPool,
204 uint32_t firstQuery,
205 uint32_t queryCount,
206 size_t dataSize,
207 void* pData,
208 VkDeviceSize stride,
209 VkQueryResultFlags flags)
210 {
211 ANV_FROM_HANDLE(anv_device, device, _device);
212 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
213
214 assert(pool->type == VK_QUERY_TYPE_OCCLUSION ||
215 pool->type == VK_QUERY_TYPE_PIPELINE_STATISTICS ||
216 pool->type == VK_QUERY_TYPE_TIMESTAMP);
217
218 if (unlikely(device->lost))
219 return VK_ERROR_DEVICE_LOST;
220
221 if (pData == NULL)
222 return VK_SUCCESS;
223
224 void *data_end = pData + dataSize;
225
226 VkResult status = VK_SUCCESS;
227 for (uint32_t i = 0; i < queryCount; i++) {
228 uint64_t *slot = pool->bo.map + (firstQuery + i) * pool->stride;
229
230 /* Availability is always at the start of the slot */
231 bool available = slot[0];
232
233 if (!available && (flags & VK_QUERY_RESULT_WAIT_BIT)) {
234 status = wait_for_available(device, pool, slot);
235 if (status != VK_SUCCESS)
236 return status;
237
238 available = true;
239 }
240
241 /* From the Vulkan 1.0.42 spec:
242 *
243 * "If VK_QUERY_RESULT_WAIT_BIT and VK_QUERY_RESULT_PARTIAL_BIT are
244 * both not set then no result values are written to pData for
245 * queries that are in the unavailable state at the time of the call,
246 * and vkGetQueryPoolResults returns VK_NOT_READY. However,
247 * availability state is still written to pData for those queries if
248 * VK_QUERY_RESULT_WITH_AVAILABILITY_BIT is set."
249 */
250 bool write_results = available || (flags & VK_QUERY_RESULT_PARTIAL_BIT);
251
252 uint32_t idx = 0;
253 switch (pool->type) {
254 case VK_QUERY_TYPE_OCCLUSION:
255 if (write_results)
256 cpu_write_query_result(pData, flags, idx, slot[2] - slot[1]);
257 idx++;
258 break;
259
260 case VK_QUERY_TYPE_PIPELINE_STATISTICS: {
261 uint32_t statistics = pool->pipeline_statistics;
262 while (statistics) {
263 uint32_t stat = u_bit_scan(&statistics);
264 if (write_results) {
265 uint64_t result = slot[idx * 2 + 2] - slot[idx * 2 + 1];
266
267 /* WaDividePSInvocationCountBy4:HSW,BDW */
268 if ((device->info.gen == 8 || device->info.is_haswell) &&
269 (1 << stat) == VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT)
270 result >>= 2;
271
272 cpu_write_query_result(pData, flags, idx, result);
273 }
274 idx++;
275 }
276 assert(idx == util_bitcount(pool->pipeline_statistics));
277 break;
278 }
279
280 case VK_QUERY_TYPE_TIMESTAMP:
281 if (write_results)
282 cpu_write_query_result(pData, flags, idx, slot[1]);
283 idx++;
284 break;
285
286 default:
287 unreachable("invalid pool type");
288 }
289
290 if (!write_results)
291 status = VK_NOT_READY;
292
293 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT)
294 cpu_write_query_result(pData, flags, idx, available);
295
296 pData += stride;
297 if (pData >= data_end)
298 break;
299 }
300
301 return status;
302 }
303
304 static void
305 emit_ps_depth_count(struct anv_cmd_buffer *cmd_buffer,
306 struct anv_bo *bo, uint32_t offset)
307 {
308 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
309 pc.DestinationAddressType = DAT_PPGTT;
310 pc.PostSyncOperation = WritePSDepthCount;
311 pc.DepthStallEnable = true;
312 pc.Address = (struct anv_address) { bo, offset };
313
314 if (GEN_GEN == 9 && cmd_buffer->device->info.gt == 4)
315 pc.CommandStreamerStallEnable = true;
316 }
317 }
318
319 static void
320 emit_query_availability(struct anv_cmd_buffer *cmd_buffer,
321 struct anv_bo *bo, uint32_t offset)
322 {
323 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
324 pc.DestinationAddressType = DAT_PPGTT;
325 pc.PostSyncOperation = WriteImmediateData;
326 pc.Address = (struct anv_address) { bo, offset };
327 pc.ImmediateData = 1;
328 }
329 }
330
331 /**
332 * Goes through a series of consecutive query indices in the given pool
333 * setting all element values to 0 and emitting them as available.
334 */
335 static void
336 emit_zero_queries(struct anv_cmd_buffer *cmd_buffer,
337 struct anv_query_pool *pool,
338 uint32_t first_index, uint32_t num_queries)
339 {
340 const uint32_t num_elements = pool->stride / sizeof(uint64_t);
341
342 for (uint32_t i = 0; i < num_queries; i++) {
343 uint32_t slot_offset = (first_index + i) * pool->stride;
344 for (uint32_t j = 1; j < num_elements; j++) {
345 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
346 sdi.Address.bo = &pool->bo;
347 sdi.Address.offset = slot_offset + j * sizeof(uint64_t);
348 sdi.ImmediateData = 0ull;
349 }
350 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdi) {
351 sdi.Address.bo = &pool->bo;
352 sdi.Address.offset = slot_offset + j * sizeof(uint64_t) + 4;
353 sdi.ImmediateData = 0ull;
354 }
355 }
356 emit_query_availability(cmd_buffer, &pool->bo, slot_offset);
357 }
358 }
359
360 void genX(CmdResetQueryPool)(
361 VkCommandBuffer commandBuffer,
362 VkQueryPool queryPool,
363 uint32_t firstQuery,
364 uint32_t queryCount)
365 {
366 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
367 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
368
369 for (uint32_t i = 0; i < queryCount; i++) {
370 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_DATA_IMM), sdm) {
371 sdm.Address = (struct anv_address) {
372 .bo = &pool->bo,
373 .offset = (firstQuery + i) * pool->stride,
374 };
375 sdm.ImmediateData = 0;
376 }
377 }
378 }
379
380 static const uint32_t vk_pipeline_stat_to_reg[] = {
381 GENX(IA_VERTICES_COUNT_num),
382 GENX(IA_PRIMITIVES_COUNT_num),
383 GENX(VS_INVOCATION_COUNT_num),
384 GENX(GS_INVOCATION_COUNT_num),
385 GENX(GS_PRIMITIVES_COUNT_num),
386 GENX(CL_INVOCATION_COUNT_num),
387 GENX(CL_PRIMITIVES_COUNT_num),
388 GENX(PS_INVOCATION_COUNT_num),
389 GENX(HS_INVOCATION_COUNT_num),
390 GENX(DS_INVOCATION_COUNT_num),
391 GENX(CS_INVOCATION_COUNT_num),
392 };
393
394 static void
395 emit_pipeline_stat(struct anv_cmd_buffer *cmd_buffer, uint32_t stat,
396 struct anv_bo *bo, uint32_t offset)
397 {
398 STATIC_ASSERT(ANV_PIPELINE_STATISTICS_MASK ==
399 (1 << ARRAY_SIZE(vk_pipeline_stat_to_reg)) - 1);
400
401 assert(stat < ARRAY_SIZE(vk_pipeline_stat_to_reg));
402 uint32_t reg = vk_pipeline_stat_to_reg[stat];
403
404 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), lrm) {
405 lrm.RegisterAddress = reg,
406 lrm.MemoryAddress = (struct anv_address) { bo, offset };
407 }
408 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), lrm) {
409 lrm.RegisterAddress = reg + 4,
410 lrm.MemoryAddress = (struct anv_address) { bo, offset + 4 };
411 }
412 }
413
414 void genX(CmdBeginQuery)(
415 VkCommandBuffer commandBuffer,
416 VkQueryPool queryPool,
417 uint32_t query,
418 VkQueryControlFlags flags)
419 {
420 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
421 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
422
423 switch (pool->type) {
424 case VK_QUERY_TYPE_OCCLUSION:
425 emit_ps_depth_count(cmd_buffer, &pool->bo, query * pool->stride + 8);
426 break;
427
428 case VK_QUERY_TYPE_PIPELINE_STATISTICS: {
429 /* TODO: This might only be necessary for certain stats */
430 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
431 pc.CommandStreamerStallEnable = true;
432 pc.StallAtPixelScoreboard = true;
433 }
434
435 uint32_t statistics = pool->pipeline_statistics;
436 uint32_t offset = query * pool->stride + 8;
437 while (statistics) {
438 uint32_t stat = u_bit_scan(&statistics);
439 emit_pipeline_stat(cmd_buffer, stat, &pool->bo, offset);
440 offset += 16;
441 }
442 break;
443 }
444
445 default:
446 unreachable("");
447 }
448 }
449
450 void genX(CmdEndQuery)(
451 VkCommandBuffer commandBuffer,
452 VkQueryPool queryPool,
453 uint32_t query)
454 {
455 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
456 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
457
458 switch (pool->type) {
459 case VK_QUERY_TYPE_OCCLUSION:
460 emit_ps_depth_count(cmd_buffer, &pool->bo, query * pool->stride + 16);
461 emit_query_availability(cmd_buffer, &pool->bo, query * pool->stride);
462 break;
463
464 case VK_QUERY_TYPE_PIPELINE_STATISTICS: {
465 /* TODO: This might only be necessary for certain stats */
466 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
467 pc.CommandStreamerStallEnable = true;
468 pc.StallAtPixelScoreboard = true;
469 }
470
471 uint32_t statistics = pool->pipeline_statistics;
472 uint32_t offset = query * pool->stride + 16;
473 while (statistics) {
474 uint32_t stat = u_bit_scan(&statistics);
475 emit_pipeline_stat(cmd_buffer, stat, &pool->bo, offset);
476 offset += 16;
477 }
478
479 emit_query_availability(cmd_buffer, &pool->bo, query * pool->stride);
480 break;
481 }
482
483 default:
484 unreachable("");
485 }
486
487 /* When multiview is active the spec requires that N consecutive query
488 * indices are used, where N is the number of active views in the subpass.
489 * The spec allows that we only write the results to one of the queries
490 * but we still need to manage result availability for all the query indices.
491 * Since we only emit a single query for all active views in the
492 * first index, mark the other query indices as being already available
493 * with result 0.
494 */
495 if (cmd_buffer->state.subpass && cmd_buffer->state.subpass->view_mask) {
496 const uint32_t num_queries =
497 util_bitcount(cmd_buffer->state.subpass->view_mask);
498 if (num_queries > 1)
499 emit_zero_queries(cmd_buffer, pool, query + 1, num_queries - 1);
500 }
501 }
502
503 #define TIMESTAMP 0x2358
504
505 void genX(CmdWriteTimestamp)(
506 VkCommandBuffer commandBuffer,
507 VkPipelineStageFlagBits pipelineStage,
508 VkQueryPool queryPool,
509 uint32_t query)
510 {
511 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
512 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
513 uint32_t offset = query * pool->stride;
514
515 assert(pool->type == VK_QUERY_TYPE_TIMESTAMP);
516
517 switch (pipelineStage) {
518 case VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT:
519 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
520 srm.RegisterAddress = TIMESTAMP;
521 srm.MemoryAddress = (struct anv_address) { &pool->bo, offset + 8 };
522 }
523 anv_batch_emit(&cmd_buffer->batch, GENX(MI_STORE_REGISTER_MEM), srm) {
524 srm.RegisterAddress = TIMESTAMP + 4;
525 srm.MemoryAddress = (struct anv_address) { &pool->bo, offset + 12 };
526 }
527 break;
528
529 default:
530 /* Everything else is bottom-of-pipe */
531 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
532 pc.DestinationAddressType = DAT_PPGTT;
533 pc.PostSyncOperation = WriteTimestamp;
534 pc.Address = (struct anv_address) { &pool->bo, offset + 8 };
535
536 if (GEN_GEN == 9 && cmd_buffer->device->info.gt == 4)
537 pc.CommandStreamerStallEnable = true;
538 }
539 break;
540 }
541
542 emit_query_availability(cmd_buffer, &pool->bo, offset);
543
544 /* When multiview is active the spec requires that N consecutive query
545 * indices are used, where N is the number of active views in the subpass.
546 * The spec allows that we only write the results to one of the queries
547 * but we still need to manage result availability for all the query indices.
548 * Since we only emit a single query for all active views in the
549 * first index, mark the other query indices as being already available
550 * with result 0.
551 */
552 if (cmd_buffer->state.subpass && cmd_buffer->state.subpass->view_mask) {
553 const uint32_t num_queries =
554 util_bitcount(cmd_buffer->state.subpass->view_mask);
555 if (num_queries > 1)
556 emit_zero_queries(cmd_buffer, pool, query + 1, num_queries - 1);
557 }
558 }
559
560 #if GEN_GEN > 7 || GEN_IS_HASWELL
561
562 static uint32_t
563 mi_alu(uint32_t opcode, uint32_t operand1, uint32_t operand2)
564 {
565 struct GENX(MI_MATH_ALU_INSTRUCTION) instr = {
566 .ALUOpcode = opcode,
567 .Operand1 = operand1,
568 .Operand2 = operand2,
569 };
570
571 uint32_t dw;
572 GENX(MI_MATH_ALU_INSTRUCTION_pack)(NULL, &dw, &instr);
573
574 return dw;
575 }
576
577 #define CS_GPR(n) (0x2600 + (n) * 8)
578
579 static void
580 emit_load_alu_reg_u64(struct anv_batch *batch, uint32_t reg,
581 struct anv_bo *bo, uint32_t offset)
582 {
583 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
584 lrm.RegisterAddress = reg,
585 lrm.MemoryAddress = (struct anv_address) { bo, offset };
586 }
587 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
588 lrm.RegisterAddress = reg + 4;
589 lrm.MemoryAddress = (struct anv_address) { bo, offset + 4 };
590 }
591 }
592
593 static void
594 emit_load_alu_reg_imm32(struct anv_batch *batch, uint32_t reg, uint32_t imm)
595 {
596 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
597 lri.RegisterOffset = reg;
598 lri.DataDWord = imm;
599 }
600 }
601
602 static void
603 emit_load_alu_reg_imm64(struct anv_batch *batch, uint32_t reg, uint64_t imm)
604 {
605 emit_load_alu_reg_imm32(batch, reg, (uint32_t)imm);
606 emit_load_alu_reg_imm32(batch, reg + 4, (uint32_t)(imm >> 32));
607 }
608
609 static void
610 emit_load_alu_reg_reg32(struct anv_batch *batch, uint32_t src, uint32_t dst)
611 {
612 anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_REG), lrr) {
613 lrr.SourceRegisterAddress = src;
614 lrr.DestinationRegisterAddress = dst;
615 }
616 }
617
618 /*
619 * GPR0 = GPR0 & ((1ull << n) - 1);
620 */
621 static void
622 keep_gpr0_lower_n_bits(struct anv_batch *batch, uint32_t n)
623 {
624 assert(n < 64);
625 emit_load_alu_reg_imm64(batch, CS_GPR(1), (1ull << n) - 1);
626
627 uint32_t *dw = anv_batch_emitn(batch, 5, GENX(MI_MATH));
628 if (!dw) {
629 anv_batch_set_error(batch, VK_ERROR_OUT_OF_HOST_MEMORY);
630 return;
631 }
632
633 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, MI_ALU_REG0);
634 dw[2] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, MI_ALU_REG1);
635 dw[3] = mi_alu(MI_ALU_AND, 0, 0);
636 dw[4] = mi_alu(MI_ALU_STORE, MI_ALU_REG0, MI_ALU_ACCU);
637 }
638
639 /*
640 * GPR0 = GPR0 << 30;
641 */
642 static void
643 shl_gpr0_by_30_bits(struct anv_batch *batch)
644 {
645 /* First we mask 34 bits of GPR0 to prevent overflow */
646 keep_gpr0_lower_n_bits(batch, 34);
647
648 const uint32_t outer_count = 5;
649 const uint32_t inner_count = 6;
650 STATIC_ASSERT(outer_count * inner_count == 30);
651 const uint32_t cmd_len = 1 + inner_count * 4;
652
653 /* We'll emit 5 commands, each shifting GPR0 left by 6 bits, for a total of
654 * 30 left shifts.
655 */
656 for (int o = 0; o < outer_count; o++) {
657 /* Submit one MI_MATH to shift left by 6 bits */
658 uint32_t *dw = anv_batch_emitn(batch, cmd_len, GENX(MI_MATH));
659 if (!dw) {
660 anv_batch_set_error(batch, VK_ERROR_OUT_OF_HOST_MEMORY);
661 return;
662 }
663
664 dw++;
665 for (int i = 0; i < inner_count; i++, dw += 4) {
666 dw[0] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, MI_ALU_REG0);
667 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, MI_ALU_REG0);
668 dw[2] = mi_alu(MI_ALU_ADD, 0, 0);
669 dw[3] = mi_alu(MI_ALU_STORE, MI_ALU_REG0, MI_ALU_ACCU);
670 }
671 }
672 }
673
674 /*
675 * GPR0 = GPR0 >> 2;
676 *
677 * Note that the upper 30 bits of GPR are lost!
678 */
679 static void
680 shr_gpr0_by_2_bits(struct anv_batch *batch)
681 {
682 shl_gpr0_by_30_bits(batch);
683 emit_load_alu_reg_reg32(batch, CS_GPR(0) + 4, CS_GPR(0));
684 emit_load_alu_reg_imm32(batch, CS_GPR(0) + 4, 0);
685 }
686
687 static void
688 gpu_write_query_result(struct anv_batch *batch,
689 struct anv_buffer *dst_buffer, uint32_t dst_offset,
690 VkQueryResultFlags flags,
691 uint32_t value_index, uint32_t reg)
692 {
693 if (flags & VK_QUERY_RESULT_64_BIT)
694 dst_offset += value_index * 8;
695 else
696 dst_offset += value_index * 4;
697
698 anv_batch_emit(batch, GENX(MI_STORE_REGISTER_MEM), srm) {
699 srm.RegisterAddress = reg;
700 srm.MemoryAddress = anv_address_add(dst_buffer->address, dst_offset);
701 }
702
703 if (flags & VK_QUERY_RESULT_64_BIT) {
704 anv_batch_emit(batch, GENX(MI_STORE_REGISTER_MEM), srm) {
705 srm.RegisterAddress = reg + 4;
706 srm.MemoryAddress = anv_address_add(dst_buffer->address,
707 dst_offset + 4);
708 }
709 }
710 }
711
712 static void
713 compute_query_result(struct anv_batch *batch, uint32_t dst_reg,
714 struct anv_bo *bo, uint32_t offset)
715 {
716 emit_load_alu_reg_u64(batch, CS_GPR(0), bo, offset);
717 emit_load_alu_reg_u64(batch, CS_GPR(1), bo, offset + 8);
718
719 /* FIXME: We need to clamp the result for 32 bit. */
720
721 uint32_t *dw = anv_batch_emitn(batch, 5, GENX(MI_MATH));
722 if (!dw) {
723 anv_batch_set_error(batch, VK_ERROR_OUT_OF_HOST_MEMORY);
724 return;
725 }
726
727 dw[1] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCA, MI_ALU_REG1);
728 dw[2] = mi_alu(MI_ALU_LOAD, MI_ALU_SRCB, MI_ALU_REG0);
729 dw[3] = mi_alu(MI_ALU_SUB, 0, 0);
730 dw[4] = mi_alu(MI_ALU_STORE, dst_reg, MI_ALU_ACCU);
731 }
732
733 void genX(CmdCopyQueryPoolResults)(
734 VkCommandBuffer commandBuffer,
735 VkQueryPool queryPool,
736 uint32_t firstQuery,
737 uint32_t queryCount,
738 VkBuffer destBuffer,
739 VkDeviceSize destOffset,
740 VkDeviceSize destStride,
741 VkQueryResultFlags flags)
742 {
743 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
744 ANV_FROM_HANDLE(anv_query_pool, pool, queryPool);
745 ANV_FROM_HANDLE(anv_buffer, buffer, destBuffer);
746 uint32_t slot_offset;
747
748 if (flags & VK_QUERY_RESULT_WAIT_BIT) {
749 anv_batch_emit(&cmd_buffer->batch, GENX(PIPE_CONTROL), pc) {
750 pc.CommandStreamerStallEnable = true;
751 pc.StallAtPixelScoreboard = true;
752 }
753 }
754
755 for (uint32_t i = 0; i < queryCount; i++) {
756 slot_offset = (firstQuery + i) * pool->stride;
757 uint32_t idx = 0;
758 switch (pool->type) {
759 case VK_QUERY_TYPE_OCCLUSION:
760 compute_query_result(&cmd_buffer->batch, MI_ALU_REG2,
761 &pool->bo, slot_offset + 8);
762 gpu_write_query_result(&cmd_buffer->batch, buffer, destOffset,
763 flags, idx++, CS_GPR(2));
764 break;
765
766 case VK_QUERY_TYPE_PIPELINE_STATISTICS: {
767 uint32_t statistics = pool->pipeline_statistics;
768 while (statistics) {
769 uint32_t stat = u_bit_scan(&statistics);
770
771 compute_query_result(&cmd_buffer->batch, MI_ALU_REG0,
772 &pool->bo, slot_offset + idx * 16 + 8);
773
774 /* WaDividePSInvocationCountBy4:HSW,BDW */
775 if ((cmd_buffer->device->info.gen == 8 ||
776 cmd_buffer->device->info.is_haswell) &&
777 (1 << stat) == VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT) {
778 shr_gpr0_by_2_bits(&cmd_buffer->batch);
779 }
780
781 gpu_write_query_result(&cmd_buffer->batch, buffer, destOffset,
782 flags, idx++, CS_GPR(0));
783 }
784 assert(idx == util_bitcount(pool->pipeline_statistics));
785 break;
786 }
787
788 case VK_QUERY_TYPE_TIMESTAMP:
789 emit_load_alu_reg_u64(&cmd_buffer->batch,
790 CS_GPR(2), &pool->bo, slot_offset + 8);
791 gpu_write_query_result(&cmd_buffer->batch, buffer, destOffset,
792 flags, 0, CS_GPR(2));
793 break;
794
795 default:
796 unreachable("unhandled query type");
797 }
798
799 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
800 emit_load_alu_reg_u64(&cmd_buffer->batch, CS_GPR(0),
801 &pool->bo, slot_offset);
802 gpu_write_query_result(&cmd_buffer->batch, buffer, destOffset,
803 flags, idx, CS_GPR(0));
804 }
805
806 destOffset += destStride;
807 }
808 }
809
810 #else
811 void genX(CmdCopyQueryPoolResults)(
812 VkCommandBuffer commandBuffer,
813 VkQueryPool queryPool,
814 uint32_t firstQuery,
815 uint32_t queryCount,
816 VkBuffer destBuffer,
817 VkDeviceSize destOffset,
818 VkDeviceSize destStride,
819 VkQueryResultFlags flags)
820 {
821 anv_finishme("Queries not yet supported on Ivy Bridge");
822 }
823 #endif