2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2014 Marek Olšák <marek.olsak@amd.com>
4 * Copyright 2018 Advanced Micro Devices, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * on the rights to use, copy, modify, merge, publish, distribute, sub
11 * license, and/or sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include "util/u_memory.h"
30 #include "util/u_upload_mgr.h"
31 #include "util/os_time.h"
32 #include "util/u_suballoc.h"
33 #include "tgsi/tgsi_text.h"
34 #include "amd/common/sid.h"
36 #define SI_MAX_STREAMS 4
38 struct si_hw_query_params
{
39 unsigned start_offset
;
41 unsigned fence_offset
;
46 /* Queries without buffer handling or suspend/resume. */
50 uint64_t begin_result
;
56 /* Fence for GPU_FINISHED. */
57 struct pipe_fence_handle
*fence
;
60 static void si_query_sw_destroy(struct si_screen
*sscreen
,
61 struct si_query
*rquery
)
63 struct si_query_sw
*query
= (struct si_query_sw
*)rquery
;
65 sscreen
->b
.fence_reference(&sscreen
->b
, &query
->fence
, NULL
);
69 static enum radeon_value_id
winsys_id_from_type(unsigned type
)
72 case SI_QUERY_REQUESTED_VRAM
: return RADEON_REQUESTED_VRAM_MEMORY
;
73 case SI_QUERY_REQUESTED_GTT
: return RADEON_REQUESTED_GTT_MEMORY
;
74 case SI_QUERY_MAPPED_VRAM
: return RADEON_MAPPED_VRAM
;
75 case SI_QUERY_MAPPED_GTT
: return RADEON_MAPPED_GTT
;
76 case SI_QUERY_BUFFER_WAIT_TIME
: return RADEON_BUFFER_WAIT_TIME_NS
;
77 case SI_QUERY_NUM_MAPPED_BUFFERS
: return RADEON_NUM_MAPPED_BUFFERS
;
78 case SI_QUERY_NUM_GFX_IBS
: return RADEON_NUM_GFX_IBS
;
79 case SI_QUERY_NUM_SDMA_IBS
: return RADEON_NUM_SDMA_IBS
;
80 case SI_QUERY_GFX_BO_LIST_SIZE
: return RADEON_GFX_BO_LIST_COUNTER
;
81 case SI_QUERY_GFX_IB_SIZE
: return RADEON_GFX_IB_SIZE_COUNTER
;
82 case SI_QUERY_NUM_BYTES_MOVED
: return RADEON_NUM_BYTES_MOVED
;
83 case SI_QUERY_NUM_EVICTIONS
: return RADEON_NUM_EVICTIONS
;
84 case SI_QUERY_NUM_VRAM_CPU_PAGE_FAULTS
: return RADEON_NUM_VRAM_CPU_PAGE_FAULTS
;
85 case SI_QUERY_VRAM_USAGE
: return RADEON_VRAM_USAGE
;
86 case SI_QUERY_VRAM_VIS_USAGE
: return RADEON_VRAM_VIS_USAGE
;
87 case SI_QUERY_GTT_USAGE
: return RADEON_GTT_USAGE
;
88 case SI_QUERY_GPU_TEMPERATURE
: return RADEON_GPU_TEMPERATURE
;
89 case SI_QUERY_CURRENT_GPU_SCLK
: return RADEON_CURRENT_SCLK
;
90 case SI_QUERY_CURRENT_GPU_MCLK
: return RADEON_CURRENT_MCLK
;
91 case SI_QUERY_CS_THREAD_BUSY
: return RADEON_CS_THREAD_TIME
;
92 default: unreachable("query type does not correspond to winsys id");
96 static bool si_query_sw_begin(struct si_context
*sctx
,
97 struct si_query
*rquery
)
99 struct si_query_sw
*query
= (struct si_query_sw
*)rquery
;
100 enum radeon_value_id ws_id
;
102 switch(query
->b
.type
) {
103 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
104 case PIPE_QUERY_GPU_FINISHED
:
106 case SI_QUERY_DRAW_CALLS
:
107 query
->begin_result
= sctx
->num_draw_calls
;
109 case SI_QUERY_DECOMPRESS_CALLS
:
110 query
->begin_result
= sctx
->num_decompress_calls
;
112 case SI_QUERY_MRT_DRAW_CALLS
:
113 query
->begin_result
= sctx
->num_mrt_draw_calls
;
115 case SI_QUERY_PRIM_RESTART_CALLS
:
116 query
->begin_result
= sctx
->num_prim_restart_calls
;
118 case SI_QUERY_SPILL_DRAW_CALLS
:
119 query
->begin_result
= sctx
->num_spill_draw_calls
;
121 case SI_QUERY_COMPUTE_CALLS
:
122 query
->begin_result
= sctx
->num_compute_calls
;
124 case SI_QUERY_SPILL_COMPUTE_CALLS
:
125 query
->begin_result
= sctx
->num_spill_compute_calls
;
127 case SI_QUERY_DMA_CALLS
:
128 query
->begin_result
= sctx
->num_dma_calls
;
130 case SI_QUERY_CP_DMA_CALLS
:
131 query
->begin_result
= sctx
->num_cp_dma_calls
;
133 case SI_QUERY_NUM_VS_FLUSHES
:
134 query
->begin_result
= sctx
->num_vs_flushes
;
136 case SI_QUERY_NUM_PS_FLUSHES
:
137 query
->begin_result
= sctx
->num_ps_flushes
;
139 case SI_QUERY_NUM_CS_FLUSHES
:
140 query
->begin_result
= sctx
->num_cs_flushes
;
142 case SI_QUERY_NUM_CB_CACHE_FLUSHES
:
143 query
->begin_result
= sctx
->num_cb_cache_flushes
;
145 case SI_QUERY_NUM_DB_CACHE_FLUSHES
:
146 query
->begin_result
= sctx
->num_db_cache_flushes
;
148 case SI_QUERY_NUM_L2_INVALIDATES
:
149 query
->begin_result
= sctx
->num_L2_invalidates
;
151 case SI_QUERY_NUM_L2_WRITEBACKS
:
152 query
->begin_result
= sctx
->num_L2_writebacks
;
154 case SI_QUERY_NUM_RESIDENT_HANDLES
:
155 query
->begin_result
= sctx
->num_resident_handles
;
157 case SI_QUERY_TC_OFFLOADED_SLOTS
:
158 query
->begin_result
= sctx
->tc
? sctx
->tc
->num_offloaded_slots
: 0;
160 case SI_QUERY_TC_DIRECT_SLOTS
:
161 query
->begin_result
= sctx
->tc
? sctx
->tc
->num_direct_slots
: 0;
163 case SI_QUERY_TC_NUM_SYNCS
:
164 query
->begin_result
= sctx
->tc
? sctx
->tc
->num_syncs
: 0;
166 case SI_QUERY_REQUESTED_VRAM
:
167 case SI_QUERY_REQUESTED_GTT
:
168 case SI_QUERY_MAPPED_VRAM
:
169 case SI_QUERY_MAPPED_GTT
:
170 case SI_QUERY_VRAM_USAGE
:
171 case SI_QUERY_VRAM_VIS_USAGE
:
172 case SI_QUERY_GTT_USAGE
:
173 case SI_QUERY_GPU_TEMPERATURE
:
174 case SI_QUERY_CURRENT_GPU_SCLK
:
175 case SI_QUERY_CURRENT_GPU_MCLK
:
176 case SI_QUERY_BACK_BUFFER_PS_DRAW_RATIO
:
177 case SI_QUERY_NUM_MAPPED_BUFFERS
:
178 query
->begin_result
= 0;
180 case SI_QUERY_BUFFER_WAIT_TIME
:
181 case SI_QUERY_GFX_IB_SIZE
:
182 case SI_QUERY_NUM_GFX_IBS
:
183 case SI_QUERY_NUM_SDMA_IBS
:
184 case SI_QUERY_NUM_BYTES_MOVED
:
185 case SI_QUERY_NUM_EVICTIONS
:
186 case SI_QUERY_NUM_VRAM_CPU_PAGE_FAULTS
: {
187 enum radeon_value_id ws_id
= winsys_id_from_type(query
->b
.type
);
188 query
->begin_result
= sctx
->ws
->query_value(sctx
->ws
, ws_id
);
191 case SI_QUERY_GFX_BO_LIST_SIZE
:
192 ws_id
= winsys_id_from_type(query
->b
.type
);
193 query
->begin_result
= sctx
->ws
->query_value(sctx
->ws
, ws_id
);
194 query
->begin_time
= sctx
->ws
->query_value(sctx
->ws
,
197 case SI_QUERY_CS_THREAD_BUSY
:
198 ws_id
= winsys_id_from_type(query
->b
.type
);
199 query
->begin_result
= sctx
->ws
->query_value(sctx
->ws
, ws_id
);
200 query
->begin_time
= os_time_get_nano();
202 case SI_QUERY_GALLIUM_THREAD_BUSY
:
203 query
->begin_result
=
204 sctx
->tc
? util_queue_get_thread_time_nano(&sctx
->tc
->queue
, 0) : 0;
205 query
->begin_time
= os_time_get_nano();
207 case SI_QUERY_GPU_LOAD
:
208 case SI_QUERY_GPU_SHADERS_BUSY
:
209 case SI_QUERY_GPU_TA_BUSY
:
210 case SI_QUERY_GPU_GDS_BUSY
:
211 case SI_QUERY_GPU_VGT_BUSY
:
212 case SI_QUERY_GPU_IA_BUSY
:
213 case SI_QUERY_GPU_SX_BUSY
:
214 case SI_QUERY_GPU_WD_BUSY
:
215 case SI_QUERY_GPU_BCI_BUSY
:
216 case SI_QUERY_GPU_SC_BUSY
:
217 case SI_QUERY_GPU_PA_BUSY
:
218 case SI_QUERY_GPU_DB_BUSY
:
219 case SI_QUERY_GPU_CP_BUSY
:
220 case SI_QUERY_GPU_CB_BUSY
:
221 case SI_QUERY_GPU_SDMA_BUSY
:
222 case SI_QUERY_GPU_PFP_BUSY
:
223 case SI_QUERY_GPU_MEQ_BUSY
:
224 case SI_QUERY_GPU_ME_BUSY
:
225 case SI_QUERY_GPU_SURF_SYNC_BUSY
:
226 case SI_QUERY_GPU_CP_DMA_BUSY
:
227 case SI_QUERY_GPU_SCRATCH_RAM_BUSY
:
228 query
->begin_result
= si_begin_counter(sctx
->screen
,
231 case SI_QUERY_NUM_COMPILATIONS
:
232 query
->begin_result
= p_atomic_read(&sctx
->screen
->num_compilations
);
234 case SI_QUERY_NUM_SHADERS_CREATED
:
235 query
->begin_result
= p_atomic_read(&sctx
->screen
->num_shaders_created
);
237 case SI_QUERY_NUM_SHADER_CACHE_HITS
:
238 query
->begin_result
=
239 p_atomic_read(&sctx
->screen
->num_shader_cache_hits
);
241 case SI_QUERY_GPIN_ASIC_ID
:
242 case SI_QUERY_GPIN_NUM_SIMD
:
243 case SI_QUERY_GPIN_NUM_RB
:
244 case SI_QUERY_GPIN_NUM_SPI
:
245 case SI_QUERY_GPIN_NUM_SE
:
248 unreachable("si_query_sw_begin: bad query type");
254 static bool si_query_sw_end(struct si_context
*sctx
,
255 struct si_query
*rquery
)
257 struct si_query_sw
*query
= (struct si_query_sw
*)rquery
;
258 enum radeon_value_id ws_id
;
260 switch(query
->b
.type
) {
261 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
263 case PIPE_QUERY_GPU_FINISHED
:
264 sctx
->b
.flush(&sctx
->b
, &query
->fence
, PIPE_FLUSH_DEFERRED
);
266 case SI_QUERY_DRAW_CALLS
:
267 query
->end_result
= sctx
->num_draw_calls
;
269 case SI_QUERY_DECOMPRESS_CALLS
:
270 query
->end_result
= sctx
->num_decompress_calls
;
272 case SI_QUERY_MRT_DRAW_CALLS
:
273 query
->end_result
= sctx
->num_mrt_draw_calls
;
275 case SI_QUERY_PRIM_RESTART_CALLS
:
276 query
->end_result
= sctx
->num_prim_restart_calls
;
278 case SI_QUERY_SPILL_DRAW_CALLS
:
279 query
->end_result
= sctx
->num_spill_draw_calls
;
281 case SI_QUERY_COMPUTE_CALLS
:
282 query
->end_result
= sctx
->num_compute_calls
;
284 case SI_QUERY_SPILL_COMPUTE_CALLS
:
285 query
->end_result
= sctx
->num_spill_compute_calls
;
287 case SI_QUERY_DMA_CALLS
:
288 query
->end_result
= sctx
->num_dma_calls
;
290 case SI_QUERY_CP_DMA_CALLS
:
291 query
->end_result
= sctx
->num_cp_dma_calls
;
293 case SI_QUERY_NUM_VS_FLUSHES
:
294 query
->end_result
= sctx
->num_vs_flushes
;
296 case SI_QUERY_NUM_PS_FLUSHES
:
297 query
->end_result
= sctx
->num_ps_flushes
;
299 case SI_QUERY_NUM_CS_FLUSHES
:
300 query
->end_result
= sctx
->num_cs_flushes
;
302 case SI_QUERY_NUM_CB_CACHE_FLUSHES
:
303 query
->end_result
= sctx
->num_cb_cache_flushes
;
305 case SI_QUERY_NUM_DB_CACHE_FLUSHES
:
306 query
->end_result
= sctx
->num_db_cache_flushes
;
308 case SI_QUERY_NUM_L2_INVALIDATES
:
309 query
->end_result
= sctx
->num_L2_invalidates
;
311 case SI_QUERY_NUM_L2_WRITEBACKS
:
312 query
->end_result
= sctx
->num_L2_writebacks
;
314 case SI_QUERY_NUM_RESIDENT_HANDLES
:
315 query
->end_result
= sctx
->num_resident_handles
;
317 case SI_QUERY_TC_OFFLOADED_SLOTS
:
318 query
->end_result
= sctx
->tc
? sctx
->tc
->num_offloaded_slots
: 0;
320 case SI_QUERY_TC_DIRECT_SLOTS
:
321 query
->end_result
= sctx
->tc
? sctx
->tc
->num_direct_slots
: 0;
323 case SI_QUERY_TC_NUM_SYNCS
:
324 query
->end_result
= sctx
->tc
? sctx
->tc
->num_syncs
: 0;
326 case SI_QUERY_REQUESTED_VRAM
:
327 case SI_QUERY_REQUESTED_GTT
:
328 case SI_QUERY_MAPPED_VRAM
:
329 case SI_QUERY_MAPPED_GTT
:
330 case SI_QUERY_VRAM_USAGE
:
331 case SI_QUERY_VRAM_VIS_USAGE
:
332 case SI_QUERY_GTT_USAGE
:
333 case SI_QUERY_GPU_TEMPERATURE
:
334 case SI_QUERY_CURRENT_GPU_SCLK
:
335 case SI_QUERY_CURRENT_GPU_MCLK
:
336 case SI_QUERY_BUFFER_WAIT_TIME
:
337 case SI_QUERY_GFX_IB_SIZE
:
338 case SI_QUERY_NUM_MAPPED_BUFFERS
:
339 case SI_QUERY_NUM_GFX_IBS
:
340 case SI_QUERY_NUM_SDMA_IBS
:
341 case SI_QUERY_NUM_BYTES_MOVED
:
342 case SI_QUERY_NUM_EVICTIONS
:
343 case SI_QUERY_NUM_VRAM_CPU_PAGE_FAULTS
: {
344 enum radeon_value_id ws_id
= winsys_id_from_type(query
->b
.type
);
345 query
->end_result
= sctx
->ws
->query_value(sctx
->ws
, ws_id
);
348 case SI_QUERY_GFX_BO_LIST_SIZE
:
349 ws_id
= winsys_id_from_type(query
->b
.type
);
350 query
->end_result
= sctx
->ws
->query_value(sctx
->ws
, ws_id
);
351 query
->end_time
= sctx
->ws
->query_value(sctx
->ws
,
354 case SI_QUERY_CS_THREAD_BUSY
:
355 ws_id
= winsys_id_from_type(query
->b
.type
);
356 query
->end_result
= sctx
->ws
->query_value(sctx
->ws
, ws_id
);
357 query
->end_time
= os_time_get_nano();
359 case SI_QUERY_GALLIUM_THREAD_BUSY
:
361 sctx
->tc
? util_queue_get_thread_time_nano(&sctx
->tc
->queue
, 0) : 0;
362 query
->end_time
= os_time_get_nano();
364 case SI_QUERY_GPU_LOAD
:
365 case SI_QUERY_GPU_SHADERS_BUSY
:
366 case SI_QUERY_GPU_TA_BUSY
:
367 case SI_QUERY_GPU_GDS_BUSY
:
368 case SI_QUERY_GPU_VGT_BUSY
:
369 case SI_QUERY_GPU_IA_BUSY
:
370 case SI_QUERY_GPU_SX_BUSY
:
371 case SI_QUERY_GPU_WD_BUSY
:
372 case SI_QUERY_GPU_BCI_BUSY
:
373 case SI_QUERY_GPU_SC_BUSY
:
374 case SI_QUERY_GPU_PA_BUSY
:
375 case SI_QUERY_GPU_DB_BUSY
:
376 case SI_QUERY_GPU_CP_BUSY
:
377 case SI_QUERY_GPU_CB_BUSY
:
378 case SI_QUERY_GPU_SDMA_BUSY
:
379 case SI_QUERY_GPU_PFP_BUSY
:
380 case SI_QUERY_GPU_MEQ_BUSY
:
381 case SI_QUERY_GPU_ME_BUSY
:
382 case SI_QUERY_GPU_SURF_SYNC_BUSY
:
383 case SI_QUERY_GPU_CP_DMA_BUSY
:
384 case SI_QUERY_GPU_SCRATCH_RAM_BUSY
:
385 query
->end_result
= si_end_counter(sctx
->screen
,
387 query
->begin_result
);
388 query
->begin_result
= 0;
390 case SI_QUERY_NUM_COMPILATIONS
:
391 query
->end_result
= p_atomic_read(&sctx
->screen
->num_compilations
);
393 case SI_QUERY_NUM_SHADERS_CREATED
:
394 query
->end_result
= p_atomic_read(&sctx
->screen
->num_shaders_created
);
396 case SI_QUERY_BACK_BUFFER_PS_DRAW_RATIO
:
397 query
->end_result
= sctx
->last_tex_ps_draw_ratio
;
399 case SI_QUERY_NUM_SHADER_CACHE_HITS
:
401 p_atomic_read(&sctx
->screen
->num_shader_cache_hits
);
403 case SI_QUERY_GPIN_ASIC_ID
:
404 case SI_QUERY_GPIN_NUM_SIMD
:
405 case SI_QUERY_GPIN_NUM_RB
:
406 case SI_QUERY_GPIN_NUM_SPI
:
407 case SI_QUERY_GPIN_NUM_SE
:
410 unreachable("si_query_sw_end: bad query type");
416 static bool si_query_sw_get_result(struct si_context
*sctx
,
417 struct si_query
*rquery
,
419 union pipe_query_result
*result
)
421 struct si_query_sw
*query
= (struct si_query_sw
*)rquery
;
423 switch (query
->b
.type
) {
424 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
425 /* Convert from cycles per millisecond to cycles per second (Hz). */
426 result
->timestamp_disjoint
.frequency
=
427 (uint64_t)sctx
->screen
->info
.clock_crystal_freq
* 1000;
428 result
->timestamp_disjoint
.disjoint
= false;
430 case PIPE_QUERY_GPU_FINISHED
: {
431 struct pipe_screen
*screen
= sctx
->b
.screen
;
432 struct pipe_context
*ctx
= rquery
->b
.flushed
? NULL
: &sctx
->b
;
434 result
->b
= screen
->fence_finish(screen
, ctx
, query
->fence
,
435 wait
? PIPE_TIMEOUT_INFINITE
: 0);
439 case SI_QUERY_GFX_BO_LIST_SIZE
:
440 result
->u64
= (query
->end_result
- query
->begin_result
) /
441 (query
->end_time
- query
->begin_time
);
443 case SI_QUERY_CS_THREAD_BUSY
:
444 case SI_QUERY_GALLIUM_THREAD_BUSY
:
445 result
->u64
= (query
->end_result
- query
->begin_result
) * 100 /
446 (query
->end_time
- query
->begin_time
);
448 case SI_QUERY_GPIN_ASIC_ID
:
451 case SI_QUERY_GPIN_NUM_SIMD
:
452 result
->u32
= sctx
->screen
->info
.num_good_compute_units
;
454 case SI_QUERY_GPIN_NUM_RB
:
455 result
->u32
= sctx
->screen
->info
.num_render_backends
;
457 case SI_QUERY_GPIN_NUM_SPI
:
458 result
->u32
= 1; /* all supported chips have one SPI per SE */
460 case SI_QUERY_GPIN_NUM_SE
:
461 result
->u32
= sctx
->screen
->info
.max_se
;
465 result
->u64
= query
->end_result
- query
->begin_result
;
467 switch (query
->b
.type
) {
468 case SI_QUERY_BUFFER_WAIT_TIME
:
469 case SI_QUERY_GPU_TEMPERATURE
:
472 case SI_QUERY_CURRENT_GPU_SCLK
:
473 case SI_QUERY_CURRENT_GPU_MCLK
:
474 result
->u64
*= 1000000;
482 static struct si_query_ops sw_query_ops
= {
483 .destroy
= si_query_sw_destroy
,
484 .begin
= si_query_sw_begin
,
485 .end
= si_query_sw_end
,
486 .get_result
= si_query_sw_get_result
,
487 .get_result_resource
= NULL
490 static struct pipe_query
*si_query_sw_create(unsigned query_type
)
492 struct si_query_sw
*query
;
494 query
= CALLOC_STRUCT(si_query_sw
);
498 query
->b
.type
= query_type
;
499 query
->b
.ops
= &sw_query_ops
;
501 return (struct pipe_query
*)query
;
504 void si_query_hw_destroy(struct si_screen
*sscreen
,
505 struct si_query
*rquery
)
507 struct si_query_hw
*query
= (struct si_query_hw
*)rquery
;
508 struct si_query_buffer
*prev
= query
->buffer
.previous
;
510 /* Release all query buffers. */
512 struct si_query_buffer
*qbuf
= prev
;
513 prev
= prev
->previous
;
514 r600_resource_reference(&qbuf
->buf
, NULL
);
518 r600_resource_reference(&query
->buffer
.buf
, NULL
);
519 r600_resource_reference(&query
->workaround_buf
, NULL
);
523 static struct r600_resource
*si_new_query_buffer(struct si_screen
*sscreen
,
524 struct si_query_hw
*query
)
526 unsigned buf_size
= MAX2(query
->result_size
,
527 sscreen
->info
.min_alloc_size
);
529 /* Queries are normally read by the CPU after
530 * being written by the gpu, hence staging is probably a good
533 struct r600_resource
*buf
= r600_resource(
534 pipe_buffer_create(&sscreen
->b
, 0,
535 PIPE_USAGE_STAGING
, buf_size
));
539 if (!query
->ops
->prepare_buffer(sscreen
, query
, buf
)) {
540 r600_resource_reference(&buf
, NULL
);
547 static bool si_query_hw_prepare_buffer(struct si_screen
*sscreen
,
548 struct si_query_hw
*query
,
549 struct r600_resource
*buffer
)
551 /* Callers ensure that the buffer is currently unused by the GPU. */
552 uint32_t *results
= sscreen
->ws
->buffer_map(buffer
->buf
, NULL
,
553 PIPE_TRANSFER_WRITE
|
554 PIPE_TRANSFER_UNSYNCHRONIZED
);
558 memset(results
, 0, buffer
->b
.b
.width0
);
560 if (query
->b
.type
== PIPE_QUERY_OCCLUSION_COUNTER
||
561 query
->b
.type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
562 query
->b
.type
== PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
) {
563 unsigned max_rbs
= sscreen
->info
.num_render_backends
;
564 unsigned enabled_rb_mask
= sscreen
->info
.enabled_rb_mask
;
565 unsigned num_results
;
568 /* Set top bits for unused backends. */
569 num_results
= buffer
->b
.b
.width0
/ query
->result_size
;
570 for (j
= 0; j
< num_results
; j
++) {
571 for (i
= 0; i
< max_rbs
; i
++) {
572 if (!(enabled_rb_mask
& (1<<i
))) {
573 results
[(i
* 4)+1] = 0x80000000;
574 results
[(i
* 4)+3] = 0x80000000;
577 results
+= 4 * max_rbs
;
584 static void si_query_hw_get_result_resource(struct si_context
*sctx
,
585 struct si_query
*rquery
,
587 enum pipe_query_value_type result_type
,
589 struct pipe_resource
*resource
,
592 static struct si_query_ops query_hw_ops
= {
593 .destroy
= si_query_hw_destroy
,
594 .begin
= si_query_hw_begin
,
595 .end
= si_query_hw_end
,
596 .get_result
= si_query_hw_get_result
,
597 .get_result_resource
= si_query_hw_get_result_resource
,
600 static void si_query_hw_do_emit_start(struct si_context
*sctx
,
601 struct si_query_hw
*query
,
602 struct r600_resource
*buffer
,
604 static void si_query_hw_do_emit_stop(struct si_context
*sctx
,
605 struct si_query_hw
*query
,
606 struct r600_resource
*buffer
,
608 static void si_query_hw_add_result(struct si_screen
*sscreen
,
609 struct si_query_hw
*, void *buffer
,
610 union pipe_query_result
*result
);
611 static void si_query_hw_clear_result(struct si_query_hw
*,
612 union pipe_query_result
*);
614 static struct si_query_hw_ops query_hw_default_hw_ops
= {
615 .prepare_buffer
= si_query_hw_prepare_buffer
,
616 .emit_start
= si_query_hw_do_emit_start
,
617 .emit_stop
= si_query_hw_do_emit_stop
,
618 .clear_result
= si_query_hw_clear_result
,
619 .add_result
= si_query_hw_add_result
,
622 bool si_query_hw_init(struct si_screen
*sscreen
,
623 struct si_query_hw
*query
)
625 query
->buffer
.buf
= si_new_query_buffer(sscreen
, query
);
626 if (!query
->buffer
.buf
)
632 static struct pipe_query
*si_query_hw_create(struct si_screen
*sscreen
,
636 struct si_query_hw
*query
= CALLOC_STRUCT(si_query_hw
);
640 query
->b
.type
= query_type
;
641 query
->b
.ops
= &query_hw_ops
;
642 query
->ops
= &query_hw_default_hw_ops
;
644 switch (query_type
) {
645 case PIPE_QUERY_OCCLUSION_COUNTER
:
646 case PIPE_QUERY_OCCLUSION_PREDICATE
:
647 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
648 query
->result_size
= 16 * sscreen
->info
.num_render_backends
;
649 query
->result_size
+= 16; /* for the fence + alignment */
650 query
->num_cs_dw_end
= 6 + si_gfx_write_fence_dwords(sscreen
);
652 case PIPE_QUERY_TIME_ELAPSED
:
653 query
->result_size
= 24;
654 query
->num_cs_dw_end
= 8 + si_gfx_write_fence_dwords(sscreen
);
656 case PIPE_QUERY_TIMESTAMP
:
657 query
->result_size
= 16;
658 query
->num_cs_dw_end
= 8 + si_gfx_write_fence_dwords(sscreen
);
659 query
->flags
= SI_QUERY_HW_FLAG_NO_START
;
661 case PIPE_QUERY_PRIMITIVES_EMITTED
:
662 case PIPE_QUERY_PRIMITIVES_GENERATED
:
663 case PIPE_QUERY_SO_STATISTICS
:
664 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
665 /* NumPrimitivesWritten, PrimitiveStorageNeeded. */
666 query
->result_size
= 32;
667 query
->num_cs_dw_end
= 6;
668 query
->stream
= index
;
670 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
:
671 /* NumPrimitivesWritten, PrimitiveStorageNeeded. */
672 query
->result_size
= 32 * SI_MAX_STREAMS
;
673 query
->num_cs_dw_end
= 6 * SI_MAX_STREAMS
;
675 case PIPE_QUERY_PIPELINE_STATISTICS
:
676 /* 11 values on GCN. */
677 query
->result_size
= 11 * 16;
678 query
->result_size
+= 8; /* for the fence + alignment */
679 query
->num_cs_dw_end
= 6 + si_gfx_write_fence_dwords(sscreen
);
687 if (!si_query_hw_init(sscreen
, query
)) {
692 return (struct pipe_query
*)query
;
695 static void si_update_occlusion_query_state(struct si_context
*sctx
,
696 unsigned type
, int diff
)
698 if (type
== PIPE_QUERY_OCCLUSION_COUNTER
||
699 type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
700 type
== PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
) {
701 bool old_enable
= sctx
->num_occlusion_queries
!= 0;
702 bool old_perfect_enable
=
703 sctx
->num_perfect_occlusion_queries
!= 0;
704 bool enable
, perfect_enable
;
706 sctx
->num_occlusion_queries
+= diff
;
707 assert(sctx
->num_occlusion_queries
>= 0);
709 if (type
!= PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
) {
710 sctx
->num_perfect_occlusion_queries
+= diff
;
711 assert(sctx
->num_perfect_occlusion_queries
>= 0);
714 enable
= sctx
->num_occlusion_queries
!= 0;
715 perfect_enable
= sctx
->num_perfect_occlusion_queries
!= 0;
717 if (enable
!= old_enable
|| perfect_enable
!= old_perfect_enable
) {
718 si_set_occlusion_query_state(sctx
, old_perfect_enable
);
723 static unsigned event_type_for_stream(unsigned stream
)
727 case 0: return V_028A90_SAMPLE_STREAMOUTSTATS
;
728 case 1: return V_028A90_SAMPLE_STREAMOUTSTATS1
;
729 case 2: return V_028A90_SAMPLE_STREAMOUTSTATS2
;
730 case 3: return V_028A90_SAMPLE_STREAMOUTSTATS3
;
734 static void emit_sample_streamout(struct radeon_winsys_cs
*cs
, uint64_t va
,
737 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
738 radeon_emit(cs
, EVENT_TYPE(event_type_for_stream(stream
)) | EVENT_INDEX(3));
740 radeon_emit(cs
, va
>> 32);
743 static void si_query_hw_do_emit_start(struct si_context
*sctx
,
744 struct si_query_hw
*query
,
745 struct r600_resource
*buffer
,
748 struct radeon_winsys_cs
*cs
= sctx
->gfx_cs
;
750 switch (query
->b
.type
) {
751 case PIPE_QUERY_OCCLUSION_COUNTER
:
752 case PIPE_QUERY_OCCLUSION_PREDICATE
:
753 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
754 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
755 radeon_emit(cs
, EVENT_TYPE(V_028A90_ZPASS_DONE
) | EVENT_INDEX(1));
757 radeon_emit(cs
, va
>> 32);
759 case PIPE_QUERY_PRIMITIVES_EMITTED
:
760 case PIPE_QUERY_PRIMITIVES_GENERATED
:
761 case PIPE_QUERY_SO_STATISTICS
:
762 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
763 emit_sample_streamout(cs
, va
, query
->stream
);
765 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
:
766 for (unsigned stream
= 0; stream
< SI_MAX_STREAMS
; ++stream
)
767 emit_sample_streamout(cs
, va
+ 32 * stream
, stream
);
769 case PIPE_QUERY_TIME_ELAPSED
:
770 /* Write the timestamp from the CP not waiting for
771 * outstanding draws (top-of-pipe).
773 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, 0));
774 radeon_emit(cs
, COPY_DATA_COUNT_SEL
|
775 COPY_DATA_SRC_SEL(COPY_DATA_TIMESTAMP
) |
776 COPY_DATA_DST_SEL(COPY_DATA_MEM_ASYNC
));
780 radeon_emit(cs
, va
>> 32);
782 case PIPE_QUERY_PIPELINE_STATISTICS
:
783 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
784 radeon_emit(cs
, EVENT_TYPE(V_028A90_SAMPLE_PIPELINESTAT
) | EVENT_INDEX(2));
786 radeon_emit(cs
, va
>> 32);
791 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, query
->buffer
.buf
, RADEON_USAGE_WRITE
,
795 static void si_query_hw_emit_start(struct si_context
*sctx
,
796 struct si_query_hw
*query
)
800 if (!query
->buffer
.buf
)
801 return; // previous buffer allocation failure
803 si_update_occlusion_query_state(sctx
, query
->b
.type
, 1);
804 si_update_prims_generated_query_state(sctx
, query
->b
.type
, 1);
806 si_need_gfx_cs_space(sctx
);
808 /* Get a new query buffer if needed. */
809 if (query
->buffer
.results_end
+ query
->result_size
> query
->buffer
.buf
->b
.b
.width0
) {
810 struct si_query_buffer
*qbuf
= MALLOC_STRUCT(si_query_buffer
);
811 *qbuf
= query
->buffer
;
812 query
->buffer
.results_end
= 0;
813 query
->buffer
.previous
= qbuf
;
814 query
->buffer
.buf
= si_new_query_buffer(sctx
->screen
, query
);
815 if (!query
->buffer
.buf
)
819 /* emit begin query */
820 va
= query
->buffer
.buf
->gpu_address
+ query
->buffer
.results_end
;
822 query
->ops
->emit_start(sctx
, query
, query
->buffer
.buf
, va
);
824 sctx
->num_cs_dw_queries_suspend
+= query
->num_cs_dw_end
;
827 static void si_query_hw_do_emit_stop(struct si_context
*sctx
,
828 struct si_query_hw
*query
,
829 struct r600_resource
*buffer
,
832 struct radeon_winsys_cs
*cs
= sctx
->gfx_cs
;
833 uint64_t fence_va
= 0;
835 switch (query
->b
.type
) {
836 case PIPE_QUERY_OCCLUSION_COUNTER
:
837 case PIPE_QUERY_OCCLUSION_PREDICATE
:
838 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
840 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
841 radeon_emit(cs
, EVENT_TYPE(V_028A90_ZPASS_DONE
) | EVENT_INDEX(1));
843 radeon_emit(cs
, va
>> 32);
845 fence_va
= va
+ sctx
->screen
->info
.num_render_backends
* 16 - 8;
847 case PIPE_QUERY_PRIMITIVES_EMITTED
:
848 case PIPE_QUERY_PRIMITIVES_GENERATED
:
849 case PIPE_QUERY_SO_STATISTICS
:
850 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
852 emit_sample_streamout(cs
, va
, query
->stream
);
854 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
:
856 for (unsigned stream
= 0; stream
< SI_MAX_STREAMS
; ++stream
)
857 emit_sample_streamout(cs
, va
+ 32 * stream
, stream
);
859 case PIPE_QUERY_TIME_ELAPSED
:
862 case PIPE_QUERY_TIMESTAMP
:
863 si_gfx_write_event_eop(sctx
, V_028A90_BOTTOM_OF_PIPE_TS
,
864 0, EOP_DATA_SEL_TIMESTAMP
, NULL
, va
,
868 case PIPE_QUERY_PIPELINE_STATISTICS
: {
869 unsigned sample_size
= (query
->result_size
- 8) / 2;
872 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
873 radeon_emit(cs
, EVENT_TYPE(V_028A90_SAMPLE_PIPELINESTAT
) | EVENT_INDEX(2));
875 radeon_emit(cs
, va
>> 32);
877 fence_va
= va
+ sample_size
;
883 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, query
->buffer
.buf
, RADEON_USAGE_WRITE
,
887 si_gfx_write_event_eop(sctx
, V_028A90_BOTTOM_OF_PIPE_TS
, 0,
888 EOP_DATA_SEL_VALUE_32BIT
,
889 query
->buffer
.buf
, fence_va
, 0x80000000,
893 static void si_query_hw_emit_stop(struct si_context
*sctx
,
894 struct si_query_hw
*query
)
898 if (!query
->buffer
.buf
)
899 return; // previous buffer allocation failure
901 /* The queries which need begin already called this in begin_query. */
902 if (query
->flags
& SI_QUERY_HW_FLAG_NO_START
)
903 si_need_gfx_cs_space(sctx
);
906 va
= query
->buffer
.buf
->gpu_address
+ query
->buffer
.results_end
;
908 query
->ops
->emit_stop(sctx
, query
, query
->buffer
.buf
, va
);
910 query
->buffer
.results_end
+= query
->result_size
;
912 if (!(query
->flags
& SI_QUERY_HW_FLAG_NO_START
))
913 sctx
->num_cs_dw_queries_suspend
-= query
->num_cs_dw_end
;
915 si_update_occlusion_query_state(sctx
, query
->b
.type
, -1);
916 si_update_prims_generated_query_state(sctx
, query
->b
.type
, -1);
919 static void emit_set_predicate(struct si_context
*ctx
,
920 struct r600_resource
*buf
, uint64_t va
,
923 struct radeon_winsys_cs
*cs
= ctx
->gfx_cs
;
925 if (ctx
->chip_class
>= GFX9
) {
926 radeon_emit(cs
, PKT3(PKT3_SET_PREDICATION
, 2, 0));
929 radeon_emit(cs
, va
>> 32);
931 radeon_emit(cs
, PKT3(PKT3_SET_PREDICATION
, 1, 0));
933 radeon_emit(cs
, op
| ((va
>> 32) & 0xFF));
935 radeon_add_to_buffer_list(ctx
, ctx
->gfx_cs
, buf
, RADEON_USAGE_READ
,
939 static void si_emit_query_predication(struct si_context
*ctx
)
941 struct si_query_hw
*query
= (struct si_query_hw
*)ctx
->render_cond
;
942 struct si_query_buffer
*qbuf
;
944 bool flag_wait
, invert
;
949 invert
= ctx
->render_cond_invert
;
950 flag_wait
= ctx
->render_cond_mode
== PIPE_RENDER_COND_WAIT
||
951 ctx
->render_cond_mode
== PIPE_RENDER_COND_BY_REGION_WAIT
;
953 if (query
->workaround_buf
) {
954 op
= PRED_OP(PREDICATION_OP_BOOL64
);
956 switch (query
->b
.type
) {
957 case PIPE_QUERY_OCCLUSION_COUNTER
:
958 case PIPE_QUERY_OCCLUSION_PREDICATE
:
959 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
960 op
= PRED_OP(PREDICATION_OP_ZPASS
);
962 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
963 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
:
964 op
= PRED_OP(PREDICATION_OP_PRIMCOUNT
);
973 /* if true then invert, see GL_ARB_conditional_render_inverted */
975 op
|= PREDICATION_DRAW_NOT_VISIBLE
; /* Draw if not visible or overflow */
977 op
|= PREDICATION_DRAW_VISIBLE
; /* Draw if visible or no overflow */
979 /* Use the value written by compute shader as a workaround. Note that
980 * the wait flag does not apply in this predication mode.
982 * The shader outputs the result value to L2. Workarounds only affect VI
983 * and later, where the CP reads data from L2, so we don't need an
986 if (query
->workaround_buf
) {
987 uint64_t va
= query
->workaround_buf
->gpu_address
+ query
->workaround_offset
;
988 emit_set_predicate(ctx
, query
->workaround_buf
, va
, op
);
992 op
|= flag_wait
? PREDICATION_HINT_WAIT
: PREDICATION_HINT_NOWAIT_DRAW
;
994 /* emit predicate packets for all data blocks */
995 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
996 unsigned results_base
= 0;
997 uint64_t va_base
= qbuf
->buf
->gpu_address
;
999 while (results_base
< qbuf
->results_end
) {
1000 uint64_t va
= va_base
+ results_base
;
1002 if (query
->b
.type
== PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
) {
1003 for (unsigned stream
= 0; stream
< SI_MAX_STREAMS
; ++stream
) {
1004 emit_set_predicate(ctx
, qbuf
->buf
, va
+ 32 * stream
, op
);
1006 /* set CONTINUE bit for all packets except the first */
1007 op
|= PREDICATION_CONTINUE
;
1010 emit_set_predicate(ctx
, qbuf
->buf
, va
, op
);
1011 op
|= PREDICATION_CONTINUE
;
1014 results_base
+= query
->result_size
;
1019 static struct pipe_query
*si_create_query(struct pipe_context
*ctx
, unsigned query_type
, unsigned index
)
1021 struct si_screen
*sscreen
=
1022 (struct si_screen
*)ctx
->screen
;
1024 if (query_type
== PIPE_QUERY_TIMESTAMP_DISJOINT
||
1025 query_type
== PIPE_QUERY_GPU_FINISHED
||
1026 query_type
>= PIPE_QUERY_DRIVER_SPECIFIC
)
1027 return si_query_sw_create(query_type
);
1029 return si_query_hw_create(sscreen
, query_type
, index
);
1032 static void si_destroy_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
1034 struct si_context
*sctx
= (struct si_context
*)ctx
;
1035 struct si_query
*rquery
= (struct si_query
*)query
;
1037 rquery
->ops
->destroy(sctx
->screen
, rquery
);
1040 static boolean
si_begin_query(struct pipe_context
*ctx
,
1041 struct pipe_query
*query
)
1043 struct si_context
*sctx
= (struct si_context
*)ctx
;
1044 struct si_query
*rquery
= (struct si_query
*)query
;
1046 return rquery
->ops
->begin(sctx
, rquery
);
1049 void si_query_hw_reset_buffers(struct si_context
*sctx
,
1050 struct si_query_hw
*query
)
1052 struct si_query_buffer
*prev
= query
->buffer
.previous
;
1054 /* Discard the old query buffers. */
1056 struct si_query_buffer
*qbuf
= prev
;
1057 prev
= prev
->previous
;
1058 r600_resource_reference(&qbuf
->buf
, NULL
);
1062 query
->buffer
.results_end
= 0;
1063 query
->buffer
.previous
= NULL
;
1065 /* Obtain a new buffer if the current one can't be mapped without a stall. */
1066 if (si_rings_is_buffer_referenced(sctx
, query
->buffer
.buf
->buf
, RADEON_USAGE_READWRITE
) ||
1067 !sctx
->ws
->buffer_wait(query
->buffer
.buf
->buf
, 0, RADEON_USAGE_READWRITE
)) {
1068 r600_resource_reference(&query
->buffer
.buf
, NULL
);
1069 query
->buffer
.buf
= si_new_query_buffer(sctx
->screen
, query
);
1071 if (!query
->ops
->prepare_buffer(sctx
->screen
, query
, query
->buffer
.buf
))
1072 r600_resource_reference(&query
->buffer
.buf
, NULL
);
1076 bool si_query_hw_begin(struct si_context
*sctx
,
1077 struct si_query
*rquery
)
1079 struct si_query_hw
*query
= (struct si_query_hw
*)rquery
;
1081 if (query
->flags
& SI_QUERY_HW_FLAG_NO_START
) {
1086 if (!(query
->flags
& SI_QUERY_HW_FLAG_BEGIN_RESUMES
))
1087 si_query_hw_reset_buffers(sctx
, query
);
1089 r600_resource_reference(&query
->workaround_buf
, NULL
);
1091 si_query_hw_emit_start(sctx
, query
);
1092 if (!query
->buffer
.buf
)
1095 LIST_ADDTAIL(&query
->list
, &sctx
->active_queries
);
1099 static bool si_end_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
1101 struct si_context
*sctx
= (struct si_context
*)ctx
;
1102 struct si_query
*rquery
= (struct si_query
*)query
;
1104 return rquery
->ops
->end(sctx
, rquery
);
1107 bool si_query_hw_end(struct si_context
*sctx
,
1108 struct si_query
*rquery
)
1110 struct si_query_hw
*query
= (struct si_query_hw
*)rquery
;
1112 if (query
->flags
& SI_QUERY_HW_FLAG_NO_START
)
1113 si_query_hw_reset_buffers(sctx
, query
);
1115 si_query_hw_emit_stop(sctx
, query
);
1117 if (!(query
->flags
& SI_QUERY_HW_FLAG_NO_START
))
1118 LIST_DELINIT(&query
->list
);
1120 if (!query
->buffer
.buf
)
1126 static void si_get_hw_query_params(struct si_context
*sctx
,
1127 struct si_query_hw
*rquery
, int index
,
1128 struct si_hw_query_params
*params
)
1130 unsigned max_rbs
= sctx
->screen
->info
.num_render_backends
;
1132 params
->pair_stride
= 0;
1133 params
->pair_count
= 1;
1135 switch (rquery
->b
.type
) {
1136 case PIPE_QUERY_OCCLUSION_COUNTER
:
1137 case PIPE_QUERY_OCCLUSION_PREDICATE
:
1138 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
1139 params
->start_offset
= 0;
1140 params
->end_offset
= 8;
1141 params
->fence_offset
= max_rbs
* 16;
1142 params
->pair_stride
= 16;
1143 params
->pair_count
= max_rbs
;
1145 case PIPE_QUERY_TIME_ELAPSED
:
1146 params
->start_offset
= 0;
1147 params
->end_offset
= 8;
1148 params
->fence_offset
= 16;
1150 case PIPE_QUERY_TIMESTAMP
:
1151 params
->start_offset
= 0;
1152 params
->end_offset
= 0;
1153 params
->fence_offset
= 8;
1155 case PIPE_QUERY_PRIMITIVES_EMITTED
:
1156 params
->start_offset
= 8;
1157 params
->end_offset
= 24;
1158 params
->fence_offset
= params
->end_offset
+ 4;
1160 case PIPE_QUERY_PRIMITIVES_GENERATED
:
1161 params
->start_offset
= 0;
1162 params
->end_offset
= 16;
1163 params
->fence_offset
= params
->end_offset
+ 4;
1165 case PIPE_QUERY_SO_STATISTICS
:
1166 params
->start_offset
= 8 - index
* 8;
1167 params
->end_offset
= 24 - index
* 8;
1168 params
->fence_offset
= params
->end_offset
+ 4;
1170 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
:
1171 params
->pair_count
= SI_MAX_STREAMS
;
1172 params
->pair_stride
= 32;
1173 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
1174 params
->start_offset
= 0;
1175 params
->end_offset
= 16;
1177 /* We can re-use the high dword of the last 64-bit value as a
1178 * fence: it is initialized as 0, and the high bit is set by
1179 * the write of the streamout stats event.
1181 params
->fence_offset
= rquery
->result_size
- 4;
1183 case PIPE_QUERY_PIPELINE_STATISTICS
:
1185 /* Offsets apply to EG+ */
1186 static const unsigned offsets
[] = {56, 48, 24, 32, 40, 16, 8, 0, 64, 72, 80};
1187 params
->start_offset
= offsets
[index
];
1188 params
->end_offset
= 88 + offsets
[index
];
1189 params
->fence_offset
= 2 * 88;
1193 unreachable("si_get_hw_query_params unsupported");
1197 static unsigned si_query_read_result(void *map
, unsigned start_index
, unsigned end_index
,
1198 bool test_status_bit
)
1200 uint32_t *current_result
= (uint32_t*)map
;
1201 uint64_t start
, end
;
1203 start
= (uint64_t)current_result
[start_index
] |
1204 (uint64_t)current_result
[start_index
+1] << 32;
1205 end
= (uint64_t)current_result
[end_index
] |
1206 (uint64_t)current_result
[end_index
+1] << 32;
1208 if (!test_status_bit
||
1209 ((start
& 0x8000000000000000UL
) && (end
& 0x8000000000000000UL
))) {
1215 static void si_query_hw_add_result(struct si_screen
*sscreen
,
1216 struct si_query_hw
*query
,
1218 union pipe_query_result
*result
)
1220 unsigned max_rbs
= sscreen
->info
.num_render_backends
;
1222 switch (query
->b
.type
) {
1223 case PIPE_QUERY_OCCLUSION_COUNTER
: {
1224 for (unsigned i
= 0; i
< max_rbs
; ++i
) {
1225 unsigned results_base
= i
* 16;
1227 si_query_read_result(buffer
+ results_base
, 0, 2, true);
1231 case PIPE_QUERY_OCCLUSION_PREDICATE
:
1232 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
: {
1233 for (unsigned i
= 0; i
< max_rbs
; ++i
) {
1234 unsigned results_base
= i
* 16;
1235 result
->b
= result
->b
||
1236 si_query_read_result(buffer
+ results_base
, 0, 2, true) != 0;
1240 case PIPE_QUERY_TIME_ELAPSED
:
1241 result
->u64
+= si_query_read_result(buffer
, 0, 2, false);
1243 case PIPE_QUERY_TIMESTAMP
:
1244 result
->u64
= *(uint64_t*)buffer
;
1246 case PIPE_QUERY_PRIMITIVES_EMITTED
:
1247 /* SAMPLE_STREAMOUTSTATS stores this structure:
1249 * u64 NumPrimitivesWritten;
1250 * u64 PrimitiveStorageNeeded;
1252 * We only need NumPrimitivesWritten here. */
1253 result
->u64
+= si_query_read_result(buffer
, 2, 6, true);
1255 case PIPE_QUERY_PRIMITIVES_GENERATED
:
1256 /* Here we read PrimitiveStorageNeeded. */
1257 result
->u64
+= si_query_read_result(buffer
, 0, 4, true);
1259 case PIPE_QUERY_SO_STATISTICS
:
1260 result
->so_statistics
.num_primitives_written
+=
1261 si_query_read_result(buffer
, 2, 6, true);
1262 result
->so_statistics
.primitives_storage_needed
+=
1263 si_query_read_result(buffer
, 0, 4, true);
1265 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
1266 result
->b
= result
->b
||
1267 si_query_read_result(buffer
, 2, 6, true) !=
1268 si_query_read_result(buffer
, 0, 4, true);
1270 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
:
1271 for (unsigned stream
= 0; stream
< SI_MAX_STREAMS
; ++stream
) {
1272 result
->b
= result
->b
||
1273 si_query_read_result(buffer
, 2, 6, true) !=
1274 si_query_read_result(buffer
, 0, 4, true);
1275 buffer
= (char *)buffer
+ 32;
1278 case PIPE_QUERY_PIPELINE_STATISTICS
:
1279 result
->pipeline_statistics
.ps_invocations
+=
1280 si_query_read_result(buffer
, 0, 22, false);
1281 result
->pipeline_statistics
.c_primitives
+=
1282 si_query_read_result(buffer
, 2, 24, false);
1283 result
->pipeline_statistics
.c_invocations
+=
1284 si_query_read_result(buffer
, 4, 26, false);
1285 result
->pipeline_statistics
.vs_invocations
+=
1286 si_query_read_result(buffer
, 6, 28, false);
1287 result
->pipeline_statistics
.gs_invocations
+=
1288 si_query_read_result(buffer
, 8, 30, false);
1289 result
->pipeline_statistics
.gs_primitives
+=
1290 si_query_read_result(buffer
, 10, 32, false);
1291 result
->pipeline_statistics
.ia_primitives
+=
1292 si_query_read_result(buffer
, 12, 34, false);
1293 result
->pipeline_statistics
.ia_vertices
+=
1294 si_query_read_result(buffer
, 14, 36, false);
1295 result
->pipeline_statistics
.hs_invocations
+=
1296 si_query_read_result(buffer
, 16, 38, false);
1297 result
->pipeline_statistics
.ds_invocations
+=
1298 si_query_read_result(buffer
, 18, 40, false);
1299 result
->pipeline_statistics
.cs_invocations
+=
1300 si_query_read_result(buffer
, 20, 42, false);
1301 #if 0 /* for testing */
1302 printf("Pipeline stats: IA verts=%llu, IA prims=%llu, VS=%llu, HS=%llu, "
1303 "DS=%llu, GS=%llu, GS prims=%llu, Clipper=%llu, "
1304 "Clipper prims=%llu, PS=%llu, CS=%llu\n",
1305 result
->pipeline_statistics
.ia_vertices
,
1306 result
->pipeline_statistics
.ia_primitives
,
1307 result
->pipeline_statistics
.vs_invocations
,
1308 result
->pipeline_statistics
.hs_invocations
,
1309 result
->pipeline_statistics
.ds_invocations
,
1310 result
->pipeline_statistics
.gs_invocations
,
1311 result
->pipeline_statistics
.gs_primitives
,
1312 result
->pipeline_statistics
.c_invocations
,
1313 result
->pipeline_statistics
.c_primitives
,
1314 result
->pipeline_statistics
.ps_invocations
,
1315 result
->pipeline_statistics
.cs_invocations
);
1323 static boolean
si_get_query_result(struct pipe_context
*ctx
,
1324 struct pipe_query
*query
, boolean wait
,
1325 union pipe_query_result
*result
)
1327 struct si_context
*sctx
= (struct si_context
*)ctx
;
1328 struct si_query
*rquery
= (struct si_query
*)query
;
1330 return rquery
->ops
->get_result(sctx
, rquery
, wait
, result
);
1333 static void si_get_query_result_resource(struct pipe_context
*ctx
,
1334 struct pipe_query
*query
,
1336 enum pipe_query_value_type result_type
,
1338 struct pipe_resource
*resource
,
1341 struct si_context
*sctx
= (struct si_context
*)ctx
;
1342 struct si_query
*rquery
= (struct si_query
*)query
;
1344 rquery
->ops
->get_result_resource(sctx
, rquery
, wait
, result_type
, index
,
1348 static void si_query_hw_clear_result(struct si_query_hw
*query
,
1349 union pipe_query_result
*result
)
1351 util_query_clear_result(result
, query
->b
.type
);
1354 bool si_query_hw_get_result(struct si_context
*sctx
,
1355 struct si_query
*rquery
,
1356 bool wait
, union pipe_query_result
*result
)
1358 struct si_screen
*sscreen
= sctx
->screen
;
1359 struct si_query_hw
*query
= (struct si_query_hw
*)rquery
;
1360 struct si_query_buffer
*qbuf
;
1362 query
->ops
->clear_result(query
, result
);
1364 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
1365 unsigned usage
= PIPE_TRANSFER_READ
|
1366 (wait
? 0 : PIPE_TRANSFER_DONTBLOCK
);
1367 unsigned results_base
= 0;
1370 if (rquery
->b
.flushed
)
1371 map
= sctx
->ws
->buffer_map(qbuf
->buf
->buf
, NULL
, usage
);
1373 map
= si_buffer_map_sync_with_rings(sctx
, qbuf
->buf
, usage
);
1378 while (results_base
!= qbuf
->results_end
) {
1379 query
->ops
->add_result(sscreen
, query
, map
+ results_base
,
1381 results_base
+= query
->result_size
;
1385 /* Convert the time to expected units. */
1386 if (rquery
->type
== PIPE_QUERY_TIME_ELAPSED
||
1387 rquery
->type
== PIPE_QUERY_TIMESTAMP
) {
1388 result
->u64
= (1000000 * result
->u64
) / sscreen
->info
.clock_crystal_freq
;
1393 /* Create the compute shader that is used to collect the results.
1395 * One compute grid with a single thread is launched for every query result
1396 * buffer. The thread (optionally) reads a previous summary buffer, then
1397 * accumulates data from the query result buffer, and writes the result either
1398 * to a summary buffer to be consumed by the next grid invocation or to the
1399 * user-supplied buffer.
1405 * 0.y = result_stride
1406 * 0.z = result_count
1408 * 1: read previously accumulated values
1409 * 2: write accumulated values for chaining
1410 * 4: write result available
1411 * 8: convert result to boolean (0/1)
1412 * 16: only read one dword and use that as result
1413 * 32: apply timestamp conversion
1414 * 64: store full 64 bits result
1415 * 128: store signed 32 bits result
1416 * 256: SO_OVERFLOW mode: take the difference of two successive half-pairs
1417 * 1.x = fence_offset
1421 * BUFFER[0] = query result buffer
1422 * BUFFER[1] = previous summary buffer
1423 * BUFFER[2] = next summary buffer or user-supplied buffer
1425 static void si_create_query_result_shader(struct si_context
*sctx
)
1427 /* TEMP[0].xy = accumulated result so far
1428 * TEMP[0].z = result not available
1430 * TEMP[1].x = current result index
1431 * TEMP[1].y = current pair index
1433 static const char text_tmpl
[] =
1435 "PROPERTY CS_FIXED_BLOCK_WIDTH 1\n"
1436 "PROPERTY CS_FIXED_BLOCK_HEIGHT 1\n"
1437 "PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
1441 "DCL CONST[0][0..1]\n"
1443 "IMM[0] UINT32 {0, 31, 2147483647, 4294967295}\n"
1444 "IMM[1] UINT32 {1, 2, 4, 8}\n"
1445 "IMM[2] UINT32 {16, 32, 64, 128}\n"
1446 "IMM[3] UINT32 {1000000, 0, %u, 0}\n" /* for timestamp conversion */
1447 "IMM[4] UINT32 {256, 0, 0, 0}\n"
1449 "AND TEMP[5], CONST[0][0].wwww, IMM[2].xxxx\n"
1451 /* Check result availability. */
1452 "LOAD TEMP[1].x, BUFFER[0], CONST[0][1].xxxx\n"
1453 "ISHR TEMP[0].z, TEMP[1].xxxx, IMM[0].yyyy\n"
1454 "MOV TEMP[1], TEMP[0].zzzz\n"
1455 "NOT TEMP[0].z, TEMP[0].zzzz\n"
1457 /* Load result if available. */
1459 "LOAD TEMP[0].xy, BUFFER[0], IMM[0].xxxx\n"
1462 /* Load previously accumulated result if requested. */
1463 "MOV TEMP[0], IMM[0].xxxx\n"
1464 "AND TEMP[4], CONST[0][0].wwww, IMM[1].xxxx\n"
1466 "LOAD TEMP[0].xyz, BUFFER[1], IMM[0].xxxx\n"
1469 "MOV TEMP[1].x, IMM[0].xxxx\n"
1471 /* Break if accumulated result so far is not available. */
1472 "UIF TEMP[0].zzzz\n"
1476 /* Break if result_index >= result_count. */
1477 "USGE TEMP[5], TEMP[1].xxxx, CONST[0][0].zzzz\n"
1482 /* Load fence and check result availability */
1483 "UMAD TEMP[5].x, TEMP[1].xxxx, CONST[0][0].yyyy, CONST[0][1].xxxx\n"
1484 "LOAD TEMP[5].x, BUFFER[0], TEMP[5].xxxx\n"
1485 "ISHR TEMP[0].z, TEMP[5].xxxx, IMM[0].yyyy\n"
1486 "NOT TEMP[0].z, TEMP[0].zzzz\n"
1487 "UIF TEMP[0].zzzz\n"
1491 "MOV TEMP[1].y, IMM[0].xxxx\n"
1493 /* Load start and end. */
1494 "UMUL TEMP[5].x, TEMP[1].xxxx, CONST[0][0].yyyy\n"
1495 "UMAD TEMP[5].x, TEMP[1].yyyy, CONST[0][1].yyyy, TEMP[5].xxxx\n"
1496 "LOAD TEMP[2].xy, BUFFER[0], TEMP[5].xxxx\n"
1498 "UADD TEMP[5].y, TEMP[5].xxxx, CONST[0][0].xxxx\n"
1499 "LOAD TEMP[3].xy, BUFFER[0], TEMP[5].yyyy\n"
1501 "U64ADD TEMP[4].xy, TEMP[3], -TEMP[2]\n"
1503 "AND TEMP[5].z, CONST[0][0].wwww, IMM[4].xxxx\n"
1504 "UIF TEMP[5].zzzz\n"
1505 /* Load second start/end half-pair and
1506 * take the difference
1508 "UADD TEMP[5].xy, TEMP[5], IMM[1].wwww\n"
1509 "LOAD TEMP[2].xy, BUFFER[0], TEMP[5].xxxx\n"
1510 "LOAD TEMP[3].xy, BUFFER[0], TEMP[5].yyyy\n"
1512 "U64ADD TEMP[3].xy, TEMP[3], -TEMP[2]\n"
1513 "U64ADD TEMP[4].xy, TEMP[4], -TEMP[3]\n"
1516 "U64ADD TEMP[0].xy, TEMP[0], TEMP[4]\n"
1518 /* Increment pair index */
1519 "UADD TEMP[1].y, TEMP[1].yyyy, IMM[1].xxxx\n"
1520 "USGE TEMP[5], TEMP[1].yyyy, CONST[0][1].zzzz\n"
1526 /* Increment result index */
1527 "UADD TEMP[1].x, TEMP[1].xxxx, IMM[1].xxxx\n"
1531 "AND TEMP[4], CONST[0][0].wwww, IMM[1].yyyy\n"
1533 /* Store accumulated data for chaining. */
1534 "STORE BUFFER[2].xyz, IMM[0].xxxx, TEMP[0]\n"
1536 "AND TEMP[4], CONST[0][0].wwww, IMM[1].zzzz\n"
1538 /* Store result availability. */
1539 "NOT TEMP[0].z, TEMP[0]\n"
1540 "AND TEMP[0].z, TEMP[0].zzzz, IMM[1].xxxx\n"
1541 "STORE BUFFER[2].x, IMM[0].xxxx, TEMP[0].zzzz\n"
1543 "AND TEMP[4], CONST[0][0].wwww, IMM[2].zzzz\n"
1545 "STORE BUFFER[2].y, IMM[0].xxxx, IMM[0].xxxx\n"
1548 /* Store result if it is available. */
1549 "NOT TEMP[4], TEMP[0].zzzz\n"
1551 /* Apply timestamp conversion */
1552 "AND TEMP[4], CONST[0][0].wwww, IMM[2].yyyy\n"
1554 "U64MUL TEMP[0].xy, TEMP[0], IMM[3].xyxy\n"
1555 "U64DIV TEMP[0].xy, TEMP[0], IMM[3].zwzw\n"
1558 /* Convert to boolean */
1559 "AND TEMP[4], CONST[0][0].wwww, IMM[1].wwww\n"
1561 "U64SNE TEMP[0].x, TEMP[0].xyxy, IMM[4].zwzw\n"
1562 "AND TEMP[0].x, TEMP[0].xxxx, IMM[1].xxxx\n"
1563 "MOV TEMP[0].y, IMM[0].xxxx\n"
1566 "AND TEMP[4], CONST[0][0].wwww, IMM[2].zzzz\n"
1568 "STORE BUFFER[2].xy, IMM[0].xxxx, TEMP[0].xyxy\n"
1571 "UIF TEMP[0].yyyy\n"
1572 "MOV TEMP[0].x, IMM[0].wwww\n"
1575 "AND TEMP[4], CONST[0][0].wwww, IMM[2].wwww\n"
1577 "UMIN TEMP[0].x, TEMP[0].xxxx, IMM[0].zzzz\n"
1580 "STORE BUFFER[2].x, IMM[0].xxxx, TEMP[0].xxxx\n"
1588 char text
[sizeof(text_tmpl
) + 32];
1589 struct tgsi_token tokens
[1024];
1590 struct pipe_compute_state state
= {};
1592 /* Hard code the frequency into the shader so that the backend can
1593 * use the full range of optimizations for divide-by-constant.
1595 snprintf(text
, sizeof(text
), text_tmpl
,
1596 sctx
->screen
->info
.clock_crystal_freq
);
1598 if (!tgsi_text_translate(text
, tokens
, ARRAY_SIZE(tokens
))) {
1603 state
.ir_type
= PIPE_SHADER_IR_TGSI
;
1604 state
.prog
= tokens
;
1606 sctx
->query_result_shader
= sctx
->b
.create_compute_state(&sctx
->b
, &state
);
1609 static void si_restore_qbo_state(struct si_context
*sctx
,
1610 struct si_qbo_state
*st
)
1612 sctx
->b
.bind_compute_state(&sctx
->b
, st
->saved_compute
);
1614 sctx
->b
.set_constant_buffer(&sctx
->b
, PIPE_SHADER_COMPUTE
, 0, &st
->saved_const0
);
1615 pipe_resource_reference(&st
->saved_const0
.buffer
, NULL
);
1617 sctx
->b
.set_shader_buffers(&sctx
->b
, PIPE_SHADER_COMPUTE
, 0, 3, st
->saved_ssbo
);
1618 for (unsigned i
= 0; i
< 3; ++i
)
1619 pipe_resource_reference(&st
->saved_ssbo
[i
].buffer
, NULL
);
1622 static void si_query_hw_get_result_resource(struct si_context
*sctx
,
1623 struct si_query
*rquery
,
1625 enum pipe_query_value_type result_type
,
1627 struct pipe_resource
*resource
,
1630 struct si_query_hw
*query
= (struct si_query_hw
*)rquery
;
1631 struct si_query_buffer
*qbuf
;
1632 struct si_query_buffer
*qbuf_prev
;
1633 struct pipe_resource
*tmp_buffer
= NULL
;
1634 unsigned tmp_buffer_offset
= 0;
1635 struct si_qbo_state saved_state
= {};
1636 struct pipe_grid_info grid
= {};
1637 struct pipe_constant_buffer constant_buffer
= {};
1638 struct pipe_shader_buffer ssbo
[3];
1639 struct si_hw_query_params params
;
1641 uint32_t end_offset
;
1642 uint32_t result_stride
;
1643 uint32_t result_count
;
1645 uint32_t fence_offset
;
1646 uint32_t pair_stride
;
1647 uint32_t pair_count
;
1650 if (!sctx
->query_result_shader
) {
1651 si_create_query_result_shader(sctx
);
1652 if (!sctx
->query_result_shader
)
1656 if (query
->buffer
.previous
) {
1657 u_suballocator_alloc(sctx
->allocator_zeroed_memory
, 16, 16,
1658 &tmp_buffer_offset
, &tmp_buffer
);
1663 si_save_qbo_state(sctx
, &saved_state
);
1665 si_get_hw_query_params(sctx
, query
, index
>= 0 ? index
: 0, ¶ms
);
1666 consts
.end_offset
= params
.end_offset
- params
.start_offset
;
1667 consts
.fence_offset
= params
.fence_offset
- params
.start_offset
;
1668 consts
.result_stride
= query
->result_size
;
1669 consts
.pair_stride
= params
.pair_stride
;
1670 consts
.pair_count
= params
.pair_count
;
1672 constant_buffer
.buffer_size
= sizeof(consts
);
1673 constant_buffer
.user_buffer
= &consts
;
1675 ssbo
[1].buffer
= tmp_buffer
;
1676 ssbo
[1].buffer_offset
= tmp_buffer_offset
;
1677 ssbo
[1].buffer_size
= 16;
1681 sctx
->b
.bind_compute_state(&sctx
->b
, sctx
->query_result_shader
);
1693 if (query
->b
.type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1694 query
->b
.type
== PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
)
1696 else if (query
->b
.type
== PIPE_QUERY_SO_OVERFLOW_PREDICATE
||
1697 query
->b
.type
== PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
)
1698 consts
.config
|= 8 | 256;
1699 else if (query
->b
.type
== PIPE_QUERY_TIMESTAMP
||
1700 query
->b
.type
== PIPE_QUERY_TIME_ELAPSED
)
1701 consts
.config
|= 32;
1703 switch (result_type
) {
1704 case PIPE_QUERY_TYPE_U64
:
1705 case PIPE_QUERY_TYPE_I64
:
1706 consts
.config
|= 64;
1708 case PIPE_QUERY_TYPE_I32
:
1709 consts
.config
|= 128;
1711 case PIPE_QUERY_TYPE_U32
:
1715 sctx
->flags
|= sctx
->screen
->barrier_flags
.cp_to_L2
;
1717 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf_prev
) {
1718 if (query
->b
.type
!= PIPE_QUERY_TIMESTAMP
) {
1719 qbuf_prev
= qbuf
->previous
;
1720 consts
.result_count
= qbuf
->results_end
/ query
->result_size
;
1721 consts
.config
&= ~3;
1722 if (qbuf
!= &query
->buffer
)
1727 /* Only read the last timestamp. */
1729 consts
.result_count
= 0;
1730 consts
.config
|= 16;
1731 params
.start_offset
+= qbuf
->results_end
- query
->result_size
;
1734 sctx
->b
.set_constant_buffer(&sctx
->b
, PIPE_SHADER_COMPUTE
, 0, &constant_buffer
);
1736 ssbo
[0].buffer
= &qbuf
->buf
->b
.b
;
1737 ssbo
[0].buffer_offset
= params
.start_offset
;
1738 ssbo
[0].buffer_size
= qbuf
->results_end
- params
.start_offset
;
1740 if (!qbuf
->previous
) {
1741 ssbo
[2].buffer
= resource
;
1742 ssbo
[2].buffer_offset
= offset
;
1743 ssbo
[2].buffer_size
= 8;
1745 r600_resource(resource
)->TC_L2_dirty
= true;
1748 sctx
->b
.set_shader_buffers(&sctx
->b
, PIPE_SHADER_COMPUTE
, 0, 3, ssbo
);
1750 if (wait
&& qbuf
== &query
->buffer
) {
1753 /* Wait for result availability. Wait only for readiness
1754 * of the last entry, since the fence writes should be
1755 * serialized in the CP.
1757 va
= qbuf
->buf
->gpu_address
+ qbuf
->results_end
- query
->result_size
;
1758 va
+= params
.fence_offset
;
1760 si_gfx_wait_fence(sctx
, va
, 0x80000000, 0x80000000);
1763 sctx
->b
.launch_grid(&sctx
->b
, &grid
);
1764 sctx
->flags
|= SI_CONTEXT_CS_PARTIAL_FLUSH
;
1767 si_restore_qbo_state(sctx
, &saved_state
);
1768 pipe_resource_reference(&tmp_buffer
, NULL
);
1771 static void si_render_condition(struct pipe_context
*ctx
,
1772 struct pipe_query
*query
,
1774 enum pipe_render_cond_flag mode
)
1776 struct si_context
*sctx
= (struct si_context
*)ctx
;
1777 struct si_query_hw
*rquery
= (struct si_query_hw
*)query
;
1778 struct si_atom
*atom
= &sctx
->atoms
.s
.render_cond
;
1781 bool needs_workaround
= false;
1783 /* There was a firmware regression in VI which causes successive
1784 * SET_PREDICATION packets to give the wrong answer for
1785 * non-inverted stream overflow predication.
1787 if (((sctx
->chip_class
== VI
&& sctx
->screen
->info
.pfp_fw_feature
< 49) ||
1788 (sctx
->chip_class
== GFX9
&& sctx
->screen
->info
.pfp_fw_feature
< 38)) &&
1790 (rquery
->b
.type
== PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
||
1791 (rquery
->b
.type
== PIPE_QUERY_SO_OVERFLOW_PREDICATE
&&
1792 (rquery
->buffer
.previous
||
1793 rquery
->buffer
.results_end
> rquery
->result_size
)))) {
1794 needs_workaround
= true;
1797 if (needs_workaround
&& !rquery
->workaround_buf
) {
1798 bool old_force_off
= sctx
->render_cond_force_off
;
1799 sctx
->render_cond_force_off
= true;
1801 u_suballocator_alloc(
1802 sctx
->allocator_zeroed_memory
, 8, 8,
1803 &rquery
->workaround_offset
,
1804 (struct pipe_resource
**)&rquery
->workaround_buf
);
1806 /* Reset to NULL to avoid a redundant SET_PREDICATION
1807 * from launching the compute grid.
1809 sctx
->render_cond
= NULL
;
1811 ctx
->get_query_result_resource(
1812 ctx
, query
, true, PIPE_QUERY_TYPE_U64
, 0,
1813 &rquery
->workaround_buf
->b
.b
, rquery
->workaround_offset
);
1815 /* Settings this in the render cond atom is too late,
1816 * so set it here. */
1817 sctx
->flags
|= sctx
->screen
->barrier_flags
.L2_to_cp
|
1818 SI_CONTEXT_FLUSH_FOR_RENDER_COND
;
1820 sctx
->render_cond_force_off
= old_force_off
;
1824 sctx
->render_cond
= query
;
1825 sctx
->render_cond_invert
= condition
;
1826 sctx
->render_cond_mode
= mode
;
1828 si_set_atom_dirty(sctx
, atom
, query
!= NULL
);
1831 void si_suspend_queries(struct si_context
*sctx
)
1833 struct si_query_hw
*query
;
1835 LIST_FOR_EACH_ENTRY(query
, &sctx
->active_queries
, list
) {
1836 si_query_hw_emit_stop(sctx
, query
);
1838 assert(sctx
->num_cs_dw_queries_suspend
== 0);
1841 void si_resume_queries(struct si_context
*sctx
)
1843 struct si_query_hw
*query
;
1845 assert(sctx
->num_cs_dw_queries_suspend
== 0);
1847 /* Check CS space here. Resuming must not be interrupted by flushes. */
1848 si_need_gfx_cs_space(sctx
);
1850 LIST_FOR_EACH_ENTRY(query
, &sctx
->active_queries
, list
) {
1851 si_query_hw_emit_start(sctx
, query
);
1855 #define XFULL(name_, query_type_, type_, result_type_, group_id_) \
1858 .query_type = SI_QUERY_##query_type_, \
1859 .type = PIPE_DRIVER_QUERY_TYPE_##type_, \
1860 .result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_##result_type_, \
1861 .group_id = group_id_ \
1864 #define X(name_, query_type_, type_, result_type_) \
1865 XFULL(name_, query_type_, type_, result_type_, ~(unsigned)0)
1867 #define XG(group_, name_, query_type_, type_, result_type_) \
1868 XFULL(name_, query_type_, type_, result_type_, SI_QUERY_GROUP_##group_)
1870 static struct pipe_driver_query_info si_driver_query_list
[] = {
1871 X("num-compilations", NUM_COMPILATIONS
, UINT64
, CUMULATIVE
),
1872 X("num-shaders-created", NUM_SHADERS_CREATED
, UINT64
, CUMULATIVE
),
1873 X("num-shader-cache-hits", NUM_SHADER_CACHE_HITS
, UINT64
, CUMULATIVE
),
1874 X("draw-calls", DRAW_CALLS
, UINT64
, AVERAGE
),
1875 X("decompress-calls", DECOMPRESS_CALLS
, UINT64
, AVERAGE
),
1876 X("MRT-draw-calls", MRT_DRAW_CALLS
, UINT64
, AVERAGE
),
1877 X("prim-restart-calls", PRIM_RESTART_CALLS
, UINT64
, AVERAGE
),
1878 X("spill-draw-calls", SPILL_DRAW_CALLS
, UINT64
, AVERAGE
),
1879 X("compute-calls", COMPUTE_CALLS
, UINT64
, AVERAGE
),
1880 X("spill-compute-calls", SPILL_COMPUTE_CALLS
, UINT64
, AVERAGE
),
1881 X("dma-calls", DMA_CALLS
, UINT64
, AVERAGE
),
1882 X("cp-dma-calls", CP_DMA_CALLS
, UINT64
, AVERAGE
),
1883 X("num-vs-flushes", NUM_VS_FLUSHES
, UINT64
, AVERAGE
),
1884 X("num-ps-flushes", NUM_PS_FLUSHES
, UINT64
, AVERAGE
),
1885 X("num-cs-flushes", NUM_CS_FLUSHES
, UINT64
, AVERAGE
),
1886 X("num-CB-cache-flushes", NUM_CB_CACHE_FLUSHES
, UINT64
, AVERAGE
),
1887 X("num-DB-cache-flushes", NUM_DB_CACHE_FLUSHES
, UINT64
, AVERAGE
),
1888 X("num-L2-invalidates", NUM_L2_INVALIDATES
, UINT64
, AVERAGE
),
1889 X("num-L2-writebacks", NUM_L2_WRITEBACKS
, UINT64
, AVERAGE
),
1890 X("num-resident-handles", NUM_RESIDENT_HANDLES
, UINT64
, AVERAGE
),
1891 X("tc-offloaded-slots", TC_OFFLOADED_SLOTS
, UINT64
, AVERAGE
),
1892 X("tc-direct-slots", TC_DIRECT_SLOTS
, UINT64
, AVERAGE
),
1893 X("tc-num-syncs", TC_NUM_SYNCS
, UINT64
, AVERAGE
),
1894 X("CS-thread-busy", CS_THREAD_BUSY
, UINT64
, AVERAGE
),
1895 X("gallium-thread-busy", GALLIUM_THREAD_BUSY
, UINT64
, AVERAGE
),
1896 X("requested-VRAM", REQUESTED_VRAM
, BYTES
, AVERAGE
),
1897 X("requested-GTT", REQUESTED_GTT
, BYTES
, AVERAGE
),
1898 X("mapped-VRAM", MAPPED_VRAM
, BYTES
, AVERAGE
),
1899 X("mapped-GTT", MAPPED_GTT
, BYTES
, AVERAGE
),
1900 X("buffer-wait-time", BUFFER_WAIT_TIME
, MICROSECONDS
, CUMULATIVE
),
1901 X("num-mapped-buffers", NUM_MAPPED_BUFFERS
, UINT64
, AVERAGE
),
1902 X("num-GFX-IBs", NUM_GFX_IBS
, UINT64
, AVERAGE
),
1903 X("num-SDMA-IBs", NUM_SDMA_IBS
, UINT64
, AVERAGE
),
1904 X("GFX-BO-list-size", GFX_BO_LIST_SIZE
, UINT64
, AVERAGE
),
1905 X("GFX-IB-size", GFX_IB_SIZE
, UINT64
, AVERAGE
),
1906 X("num-bytes-moved", NUM_BYTES_MOVED
, BYTES
, CUMULATIVE
),
1907 X("num-evictions", NUM_EVICTIONS
, UINT64
, CUMULATIVE
),
1908 X("VRAM-CPU-page-faults", NUM_VRAM_CPU_PAGE_FAULTS
, UINT64
, CUMULATIVE
),
1909 X("VRAM-usage", VRAM_USAGE
, BYTES
, AVERAGE
),
1910 X("VRAM-vis-usage", VRAM_VIS_USAGE
, BYTES
, AVERAGE
),
1911 X("GTT-usage", GTT_USAGE
, BYTES
, AVERAGE
),
1912 X("back-buffer-ps-draw-ratio", BACK_BUFFER_PS_DRAW_RATIO
, UINT64
, AVERAGE
),
1914 /* GPIN queries are for the benefit of old versions of GPUPerfStudio,
1915 * which use it as a fallback path to detect the GPU type.
1917 * Note: The names of these queries are significant for GPUPerfStudio
1918 * (and possibly their order as well). */
1919 XG(GPIN
, "GPIN_000", GPIN_ASIC_ID
, UINT
, AVERAGE
),
1920 XG(GPIN
, "GPIN_001", GPIN_NUM_SIMD
, UINT
, AVERAGE
),
1921 XG(GPIN
, "GPIN_002", GPIN_NUM_RB
, UINT
, AVERAGE
),
1922 XG(GPIN
, "GPIN_003", GPIN_NUM_SPI
, UINT
, AVERAGE
),
1923 XG(GPIN
, "GPIN_004", GPIN_NUM_SE
, UINT
, AVERAGE
),
1925 X("temperature", GPU_TEMPERATURE
, UINT64
, AVERAGE
),
1926 X("shader-clock", CURRENT_GPU_SCLK
, HZ
, AVERAGE
),
1927 X("memory-clock", CURRENT_GPU_MCLK
, HZ
, AVERAGE
),
1929 /* The following queries must be at the end of the list because their
1930 * availability is adjusted dynamically based on the DRM version. */
1931 X("GPU-load", GPU_LOAD
, UINT64
, AVERAGE
),
1932 X("GPU-shaders-busy", GPU_SHADERS_BUSY
, UINT64
, AVERAGE
),
1933 X("GPU-ta-busy", GPU_TA_BUSY
, UINT64
, AVERAGE
),
1934 X("GPU-gds-busy", GPU_GDS_BUSY
, UINT64
, AVERAGE
),
1935 X("GPU-vgt-busy", GPU_VGT_BUSY
, UINT64
, AVERAGE
),
1936 X("GPU-ia-busy", GPU_IA_BUSY
, UINT64
, AVERAGE
),
1937 X("GPU-sx-busy", GPU_SX_BUSY
, UINT64
, AVERAGE
),
1938 X("GPU-wd-busy", GPU_WD_BUSY
, UINT64
, AVERAGE
),
1939 X("GPU-bci-busy", GPU_BCI_BUSY
, UINT64
, AVERAGE
),
1940 X("GPU-sc-busy", GPU_SC_BUSY
, UINT64
, AVERAGE
),
1941 X("GPU-pa-busy", GPU_PA_BUSY
, UINT64
, AVERAGE
),
1942 X("GPU-db-busy", GPU_DB_BUSY
, UINT64
, AVERAGE
),
1943 X("GPU-cp-busy", GPU_CP_BUSY
, UINT64
, AVERAGE
),
1944 X("GPU-cb-busy", GPU_CB_BUSY
, UINT64
, AVERAGE
),
1945 X("GPU-sdma-busy", GPU_SDMA_BUSY
, UINT64
, AVERAGE
),
1946 X("GPU-pfp-busy", GPU_PFP_BUSY
, UINT64
, AVERAGE
),
1947 X("GPU-meq-busy", GPU_MEQ_BUSY
, UINT64
, AVERAGE
),
1948 X("GPU-me-busy", GPU_ME_BUSY
, UINT64
, AVERAGE
),
1949 X("GPU-surf-sync-busy", GPU_SURF_SYNC_BUSY
, UINT64
, AVERAGE
),
1950 X("GPU-cp-dma-busy", GPU_CP_DMA_BUSY
, UINT64
, AVERAGE
),
1951 X("GPU-scratch-ram-busy", GPU_SCRATCH_RAM_BUSY
, UINT64
, AVERAGE
),
1958 static unsigned si_get_num_queries(struct si_screen
*sscreen
)
1960 if (sscreen
->info
.drm_major
== 2 && sscreen
->info
.drm_minor
>= 42)
1961 return ARRAY_SIZE(si_driver_query_list
);
1962 else if (sscreen
->info
.drm_major
== 3) {
1963 if (sscreen
->info
.chip_class
>= VI
)
1964 return ARRAY_SIZE(si_driver_query_list
);
1966 return ARRAY_SIZE(si_driver_query_list
) - 7;
1969 return ARRAY_SIZE(si_driver_query_list
) - 25;
1972 static int si_get_driver_query_info(struct pipe_screen
*screen
,
1974 struct pipe_driver_query_info
*info
)
1976 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1977 unsigned num_queries
= si_get_num_queries(sscreen
);
1980 unsigned num_perfcounters
=
1981 si_get_perfcounter_info(sscreen
, 0, NULL
);
1983 return num_queries
+ num_perfcounters
;
1986 if (index
>= num_queries
)
1987 return si_get_perfcounter_info(sscreen
, index
- num_queries
, info
);
1989 *info
= si_driver_query_list
[index
];
1991 switch (info
->query_type
) {
1992 case SI_QUERY_REQUESTED_VRAM
:
1993 case SI_QUERY_VRAM_USAGE
:
1994 case SI_QUERY_MAPPED_VRAM
:
1995 info
->max_value
.u64
= sscreen
->info
.vram_size
;
1997 case SI_QUERY_REQUESTED_GTT
:
1998 case SI_QUERY_GTT_USAGE
:
1999 case SI_QUERY_MAPPED_GTT
:
2000 info
->max_value
.u64
= sscreen
->info
.gart_size
;
2002 case SI_QUERY_GPU_TEMPERATURE
:
2003 info
->max_value
.u64
= 125;
2005 case SI_QUERY_VRAM_VIS_USAGE
:
2006 info
->max_value
.u64
= sscreen
->info
.vram_vis_size
;
2010 if (info
->group_id
!= ~(unsigned)0 && sscreen
->perfcounters
)
2011 info
->group_id
+= sscreen
->perfcounters
->num_groups
;
2016 /* Note: Unfortunately, GPUPerfStudio hardcodes the order of hardware
2017 * performance counter groups, so be careful when changing this and related
2020 static int si_get_driver_query_group_info(struct pipe_screen
*screen
,
2022 struct pipe_driver_query_group_info
*info
)
2024 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
2025 unsigned num_pc_groups
= 0;
2027 if (sscreen
->perfcounters
)
2028 num_pc_groups
= sscreen
->perfcounters
->num_groups
;
2031 return num_pc_groups
+ SI_NUM_SW_QUERY_GROUPS
;
2033 if (index
< num_pc_groups
)
2034 return si_get_perfcounter_group_info(sscreen
, index
, info
);
2036 index
-= num_pc_groups
;
2037 if (index
>= SI_NUM_SW_QUERY_GROUPS
)
2040 info
->name
= "GPIN";
2041 info
->max_active_queries
= 5;
2042 info
->num_queries
= 5;
2046 void si_init_query_functions(struct si_context
*sctx
)
2048 sctx
->b
.create_query
= si_create_query
;
2049 sctx
->b
.create_batch_query
= si_create_batch_query
;
2050 sctx
->b
.destroy_query
= si_destroy_query
;
2051 sctx
->b
.begin_query
= si_begin_query
;
2052 sctx
->b
.end_query
= si_end_query
;
2053 sctx
->b
.get_query_result
= si_get_query_result
;
2054 sctx
->b
.get_query_result_resource
= si_get_query_result_resource
;
2055 sctx
->atoms
.s
.render_cond
.emit
= si_emit_query_predication
;
2057 if (((struct si_screen
*)sctx
->b
.screen
)->info
.num_render_backends
> 0)
2058 sctx
->b
.render_condition
= si_render_condition
;
2060 LIST_INITHEAD(&sctx
->active_queries
);
2063 void si_init_screen_query_functions(struct si_screen
*sscreen
)
2065 sscreen
->b
.get_driver_query_info
= si_get_driver_query_info
;
2066 sscreen
->b
.get_driver_query_group_info
= si_get_driver_query_group_info
;