2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2014 Marek Olšák <marek.olsak@amd.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "r600_query.h"
27 #include "util/u_memory.h"
28 #include "util/u_upload_mgr.h"
29 #include "os/os_time.h"
30 #include "tgsi/tgsi_text.h"
32 struct r600_hw_query_params
{
33 unsigned start_offset
;
35 unsigned fence_offset
;
40 /* Queries without buffer handling or suspend/resume. */
41 struct r600_query_sw
{
44 uint64_t begin_result
;
50 /* Fence for GPU_FINISHED. */
51 struct pipe_fence_handle
*fence
;
54 static void r600_query_sw_destroy(struct r600_common_screen
*rscreen
,
55 struct r600_query
*rquery
)
57 struct r600_query_sw
*query
= (struct r600_query_sw
*)rquery
;
59 rscreen
->b
.fence_reference(&rscreen
->b
, &query
->fence
, NULL
);
63 static enum radeon_value_id
winsys_id_from_type(unsigned type
)
66 case R600_QUERY_REQUESTED_VRAM
: return RADEON_REQUESTED_VRAM_MEMORY
;
67 case R600_QUERY_REQUESTED_GTT
: return RADEON_REQUESTED_GTT_MEMORY
;
68 case R600_QUERY_MAPPED_VRAM
: return RADEON_MAPPED_VRAM
;
69 case R600_QUERY_MAPPED_GTT
: return RADEON_MAPPED_GTT
;
70 case R600_QUERY_BUFFER_WAIT_TIME
: return RADEON_BUFFER_WAIT_TIME_NS
;
71 case R600_QUERY_NUM_MAPPED_BUFFERS
: return RADEON_NUM_MAPPED_BUFFERS
;
72 case R600_QUERY_NUM_GFX_IBS
: return RADEON_NUM_GFX_IBS
;
73 case R600_QUERY_NUM_SDMA_IBS
: return RADEON_NUM_SDMA_IBS
;
74 case R600_QUERY_NUM_BYTES_MOVED
: return RADEON_NUM_BYTES_MOVED
;
75 case R600_QUERY_NUM_EVICTIONS
: return RADEON_NUM_EVICTIONS
;
76 case R600_QUERY_NUM_VRAM_CPU_PAGE_FAULTS
: return RADEON_NUM_VRAM_CPU_PAGE_FAULTS
;
77 case R600_QUERY_VRAM_USAGE
: return RADEON_VRAM_USAGE
;
78 case R600_QUERY_VRAM_VIS_USAGE
: return RADEON_VRAM_VIS_USAGE
;
79 case R600_QUERY_GTT_USAGE
: return RADEON_GTT_USAGE
;
80 case R600_QUERY_GPU_TEMPERATURE
: return RADEON_GPU_TEMPERATURE
;
81 case R600_QUERY_CURRENT_GPU_SCLK
: return RADEON_CURRENT_SCLK
;
82 case R600_QUERY_CURRENT_GPU_MCLK
: return RADEON_CURRENT_MCLK
;
83 case R600_QUERY_CS_THREAD_BUSY
: return RADEON_CS_THREAD_TIME
;
84 default: unreachable("query type does not correspond to winsys id");
88 static bool r600_query_sw_begin(struct r600_common_context
*rctx
,
89 struct r600_query
*rquery
)
91 struct r600_query_sw
*query
= (struct r600_query_sw
*)rquery
;
92 enum radeon_value_id ws_id
;
94 switch(query
->b
.type
) {
95 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
96 case PIPE_QUERY_GPU_FINISHED
:
98 case R600_QUERY_DRAW_CALLS
:
99 query
->begin_result
= rctx
->num_draw_calls
;
101 case R600_QUERY_PRIM_RESTART_CALLS
:
102 query
->begin_result
= rctx
->num_prim_restart_calls
;
104 case R600_QUERY_SPILL_DRAW_CALLS
:
105 query
->begin_result
= rctx
->num_spill_draw_calls
;
107 case R600_QUERY_COMPUTE_CALLS
:
108 query
->begin_result
= rctx
->num_compute_calls
;
110 case R600_QUERY_SPILL_COMPUTE_CALLS
:
111 query
->begin_result
= rctx
->num_spill_compute_calls
;
113 case R600_QUERY_DMA_CALLS
:
114 query
->begin_result
= rctx
->num_dma_calls
;
116 case R600_QUERY_CP_DMA_CALLS
:
117 query
->begin_result
= rctx
->num_cp_dma_calls
;
119 case R600_QUERY_NUM_VS_FLUSHES
:
120 query
->begin_result
= rctx
->num_vs_flushes
;
122 case R600_QUERY_NUM_PS_FLUSHES
:
123 query
->begin_result
= rctx
->num_ps_flushes
;
125 case R600_QUERY_NUM_CS_FLUSHES
:
126 query
->begin_result
= rctx
->num_cs_flushes
;
128 case R600_QUERY_NUM_CB_CACHE_FLUSHES
:
129 query
->begin_result
= rctx
->num_cb_cache_flushes
;
131 case R600_QUERY_NUM_DB_CACHE_FLUSHES
:
132 query
->begin_result
= rctx
->num_db_cache_flushes
;
134 case R600_QUERY_NUM_L2_INVALIDATES
:
135 query
->begin_result
= rctx
->num_L2_invalidates
;
137 case R600_QUERY_NUM_L2_WRITEBACKS
:
138 query
->begin_result
= rctx
->num_L2_writebacks
;
140 case R600_QUERY_NUM_RESIDENT_HANDLES
:
141 query
->begin_result
= rctx
->num_resident_handles
;
143 case R600_QUERY_TC_OFFLOADED_SLOTS
:
144 query
->begin_result
= rctx
->tc
? rctx
->tc
->num_offloaded_slots
: 0;
146 case R600_QUERY_TC_DIRECT_SLOTS
:
147 query
->begin_result
= rctx
->tc
? rctx
->tc
->num_direct_slots
: 0;
149 case R600_QUERY_TC_NUM_SYNCS
:
150 query
->begin_result
= rctx
->tc
? rctx
->tc
->num_syncs
: 0;
152 case R600_QUERY_REQUESTED_VRAM
:
153 case R600_QUERY_REQUESTED_GTT
:
154 case R600_QUERY_MAPPED_VRAM
:
155 case R600_QUERY_MAPPED_GTT
:
156 case R600_QUERY_VRAM_USAGE
:
157 case R600_QUERY_VRAM_VIS_USAGE
:
158 case R600_QUERY_GTT_USAGE
:
159 case R600_QUERY_GPU_TEMPERATURE
:
160 case R600_QUERY_CURRENT_GPU_SCLK
:
161 case R600_QUERY_CURRENT_GPU_MCLK
:
162 case R600_QUERY_BACK_BUFFER_PS_DRAW_RATIO
:
163 case R600_QUERY_NUM_MAPPED_BUFFERS
:
164 query
->begin_result
= 0;
166 case R600_QUERY_BUFFER_WAIT_TIME
:
167 case R600_QUERY_NUM_GFX_IBS
:
168 case R600_QUERY_NUM_SDMA_IBS
:
169 case R600_QUERY_NUM_BYTES_MOVED
:
170 case R600_QUERY_NUM_EVICTIONS
:
171 case R600_QUERY_NUM_VRAM_CPU_PAGE_FAULTS
: {
172 enum radeon_value_id ws_id
= winsys_id_from_type(query
->b
.type
);
173 query
->begin_result
= rctx
->ws
->query_value(rctx
->ws
, ws_id
);
176 case R600_QUERY_CS_THREAD_BUSY
:
177 ws_id
= winsys_id_from_type(query
->b
.type
);
178 query
->begin_result
= rctx
->ws
->query_value(rctx
->ws
, ws_id
);
179 query
->begin_time
= os_time_get_nano();
181 case R600_QUERY_GALLIUM_THREAD_BUSY
:
182 query
->begin_result
=
183 rctx
->tc
? util_queue_get_thread_time_nano(&rctx
->tc
->queue
, 0) : 0;
184 query
->begin_time
= os_time_get_nano();
186 case R600_QUERY_GPU_LOAD
:
187 case R600_QUERY_GPU_SHADERS_BUSY
:
188 case R600_QUERY_GPU_TA_BUSY
:
189 case R600_QUERY_GPU_GDS_BUSY
:
190 case R600_QUERY_GPU_VGT_BUSY
:
191 case R600_QUERY_GPU_IA_BUSY
:
192 case R600_QUERY_GPU_SX_BUSY
:
193 case R600_QUERY_GPU_WD_BUSY
:
194 case R600_QUERY_GPU_BCI_BUSY
:
195 case R600_QUERY_GPU_SC_BUSY
:
196 case R600_QUERY_GPU_PA_BUSY
:
197 case R600_QUERY_GPU_DB_BUSY
:
198 case R600_QUERY_GPU_CP_BUSY
:
199 case R600_QUERY_GPU_CB_BUSY
:
200 case R600_QUERY_GPU_SDMA_BUSY
:
201 case R600_QUERY_GPU_PFP_BUSY
:
202 case R600_QUERY_GPU_MEQ_BUSY
:
203 case R600_QUERY_GPU_ME_BUSY
:
204 case R600_QUERY_GPU_SURF_SYNC_BUSY
:
205 case R600_QUERY_GPU_DMA_BUSY
:
206 case R600_QUERY_GPU_SCRATCH_RAM_BUSY
:
207 case R600_QUERY_GPU_CE_BUSY
:
208 query
->begin_result
= r600_begin_counter(rctx
->screen
,
211 case R600_QUERY_NUM_COMPILATIONS
:
212 query
->begin_result
= p_atomic_read(&rctx
->screen
->num_compilations
);
214 case R600_QUERY_NUM_SHADERS_CREATED
:
215 query
->begin_result
= p_atomic_read(&rctx
->screen
->num_shaders_created
);
217 case R600_QUERY_NUM_SHADER_CACHE_HITS
:
218 query
->begin_result
=
219 p_atomic_read(&rctx
->screen
->num_shader_cache_hits
);
221 case R600_QUERY_GPIN_ASIC_ID
:
222 case R600_QUERY_GPIN_NUM_SIMD
:
223 case R600_QUERY_GPIN_NUM_RB
:
224 case R600_QUERY_GPIN_NUM_SPI
:
225 case R600_QUERY_GPIN_NUM_SE
:
228 unreachable("r600_query_sw_begin: bad query type");
234 static bool r600_query_sw_end(struct r600_common_context
*rctx
,
235 struct r600_query
*rquery
)
237 struct r600_query_sw
*query
= (struct r600_query_sw
*)rquery
;
238 enum radeon_value_id ws_id
;
240 switch(query
->b
.type
) {
241 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
243 case PIPE_QUERY_GPU_FINISHED
:
244 rctx
->b
.flush(&rctx
->b
, &query
->fence
, PIPE_FLUSH_DEFERRED
);
246 case R600_QUERY_DRAW_CALLS
:
247 query
->end_result
= rctx
->num_draw_calls
;
249 case R600_QUERY_PRIM_RESTART_CALLS
:
250 query
->end_result
= rctx
->num_prim_restart_calls
;
252 case R600_QUERY_SPILL_DRAW_CALLS
:
253 query
->end_result
= rctx
->num_spill_draw_calls
;
255 case R600_QUERY_COMPUTE_CALLS
:
256 query
->end_result
= rctx
->num_compute_calls
;
258 case R600_QUERY_SPILL_COMPUTE_CALLS
:
259 query
->end_result
= rctx
->num_spill_compute_calls
;
261 case R600_QUERY_DMA_CALLS
:
262 query
->end_result
= rctx
->num_dma_calls
;
264 case R600_QUERY_CP_DMA_CALLS
:
265 query
->end_result
= rctx
->num_cp_dma_calls
;
267 case R600_QUERY_NUM_VS_FLUSHES
:
268 query
->end_result
= rctx
->num_vs_flushes
;
270 case R600_QUERY_NUM_PS_FLUSHES
:
271 query
->end_result
= rctx
->num_ps_flushes
;
273 case R600_QUERY_NUM_CS_FLUSHES
:
274 query
->end_result
= rctx
->num_cs_flushes
;
276 case R600_QUERY_NUM_CB_CACHE_FLUSHES
:
277 query
->end_result
= rctx
->num_cb_cache_flushes
;
279 case R600_QUERY_NUM_DB_CACHE_FLUSHES
:
280 query
->end_result
= rctx
->num_db_cache_flushes
;
282 case R600_QUERY_NUM_L2_INVALIDATES
:
283 query
->end_result
= rctx
->num_L2_invalidates
;
285 case R600_QUERY_NUM_L2_WRITEBACKS
:
286 query
->end_result
= rctx
->num_L2_writebacks
;
288 case R600_QUERY_NUM_RESIDENT_HANDLES
:
289 query
->end_result
= rctx
->num_resident_handles
;
291 case R600_QUERY_TC_OFFLOADED_SLOTS
:
292 query
->end_result
= rctx
->tc
? rctx
->tc
->num_offloaded_slots
: 0;
294 case R600_QUERY_TC_DIRECT_SLOTS
:
295 query
->end_result
= rctx
->tc
? rctx
->tc
->num_direct_slots
: 0;
297 case R600_QUERY_TC_NUM_SYNCS
:
298 query
->end_result
= rctx
->tc
? rctx
->tc
->num_syncs
: 0;
300 case R600_QUERY_REQUESTED_VRAM
:
301 case R600_QUERY_REQUESTED_GTT
:
302 case R600_QUERY_MAPPED_VRAM
:
303 case R600_QUERY_MAPPED_GTT
:
304 case R600_QUERY_VRAM_USAGE
:
305 case R600_QUERY_VRAM_VIS_USAGE
:
306 case R600_QUERY_GTT_USAGE
:
307 case R600_QUERY_GPU_TEMPERATURE
:
308 case R600_QUERY_CURRENT_GPU_SCLK
:
309 case R600_QUERY_CURRENT_GPU_MCLK
:
310 case R600_QUERY_BUFFER_WAIT_TIME
:
311 case R600_QUERY_NUM_MAPPED_BUFFERS
:
312 case R600_QUERY_NUM_GFX_IBS
:
313 case R600_QUERY_NUM_SDMA_IBS
:
314 case R600_QUERY_NUM_BYTES_MOVED
:
315 case R600_QUERY_NUM_EVICTIONS
:
316 case R600_QUERY_NUM_VRAM_CPU_PAGE_FAULTS
: {
317 enum radeon_value_id ws_id
= winsys_id_from_type(query
->b
.type
);
318 query
->end_result
= rctx
->ws
->query_value(rctx
->ws
, ws_id
);
321 case R600_QUERY_CS_THREAD_BUSY
:
322 ws_id
= winsys_id_from_type(query
->b
.type
);
323 query
->end_result
= rctx
->ws
->query_value(rctx
->ws
, ws_id
);
324 query
->end_time
= os_time_get_nano();
326 case R600_QUERY_GALLIUM_THREAD_BUSY
:
328 rctx
->tc
? util_queue_get_thread_time_nano(&rctx
->tc
->queue
, 0) : 0;
329 query
->end_time
= os_time_get_nano();
331 case R600_QUERY_GPU_LOAD
:
332 case R600_QUERY_GPU_SHADERS_BUSY
:
333 case R600_QUERY_GPU_TA_BUSY
:
334 case R600_QUERY_GPU_GDS_BUSY
:
335 case R600_QUERY_GPU_VGT_BUSY
:
336 case R600_QUERY_GPU_IA_BUSY
:
337 case R600_QUERY_GPU_SX_BUSY
:
338 case R600_QUERY_GPU_WD_BUSY
:
339 case R600_QUERY_GPU_BCI_BUSY
:
340 case R600_QUERY_GPU_SC_BUSY
:
341 case R600_QUERY_GPU_PA_BUSY
:
342 case R600_QUERY_GPU_DB_BUSY
:
343 case R600_QUERY_GPU_CP_BUSY
:
344 case R600_QUERY_GPU_CB_BUSY
:
345 case R600_QUERY_GPU_SDMA_BUSY
:
346 case R600_QUERY_GPU_PFP_BUSY
:
347 case R600_QUERY_GPU_MEQ_BUSY
:
348 case R600_QUERY_GPU_ME_BUSY
:
349 case R600_QUERY_GPU_SURF_SYNC_BUSY
:
350 case R600_QUERY_GPU_DMA_BUSY
:
351 case R600_QUERY_GPU_SCRATCH_RAM_BUSY
:
352 case R600_QUERY_GPU_CE_BUSY
:
353 query
->end_result
= r600_end_counter(rctx
->screen
,
355 query
->begin_result
);
356 query
->begin_result
= 0;
358 case R600_QUERY_NUM_COMPILATIONS
:
359 query
->end_result
= p_atomic_read(&rctx
->screen
->num_compilations
);
361 case R600_QUERY_NUM_SHADERS_CREATED
:
362 query
->end_result
= p_atomic_read(&rctx
->screen
->num_shaders_created
);
364 case R600_QUERY_BACK_BUFFER_PS_DRAW_RATIO
:
365 query
->end_result
= rctx
->last_tex_ps_draw_ratio
;
367 case R600_QUERY_NUM_SHADER_CACHE_HITS
:
369 p_atomic_read(&rctx
->screen
->num_shader_cache_hits
);
371 case R600_QUERY_GPIN_ASIC_ID
:
372 case R600_QUERY_GPIN_NUM_SIMD
:
373 case R600_QUERY_GPIN_NUM_RB
:
374 case R600_QUERY_GPIN_NUM_SPI
:
375 case R600_QUERY_GPIN_NUM_SE
:
378 unreachable("r600_query_sw_end: bad query type");
384 static bool r600_query_sw_get_result(struct r600_common_context
*rctx
,
385 struct r600_query
*rquery
,
387 union pipe_query_result
*result
)
389 struct r600_query_sw
*query
= (struct r600_query_sw
*)rquery
;
391 switch (query
->b
.type
) {
392 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
393 /* Convert from cycles per millisecond to cycles per second (Hz). */
394 result
->timestamp_disjoint
.frequency
=
395 (uint64_t)rctx
->screen
->info
.clock_crystal_freq
* 1000;
396 result
->timestamp_disjoint
.disjoint
= false;
398 case PIPE_QUERY_GPU_FINISHED
: {
399 struct pipe_screen
*screen
= rctx
->b
.screen
;
400 struct pipe_context
*ctx
= rquery
->b
.flushed
? NULL
: &rctx
->b
;
402 result
->b
= screen
->fence_finish(screen
, ctx
, query
->fence
,
403 wait
? PIPE_TIMEOUT_INFINITE
: 0);
407 case R600_QUERY_CS_THREAD_BUSY
:
408 case R600_QUERY_GALLIUM_THREAD_BUSY
:
409 result
->u64
= (query
->end_result
- query
->begin_result
) * 100 /
410 (query
->end_time
- query
->begin_time
);
412 case R600_QUERY_GPIN_ASIC_ID
:
415 case R600_QUERY_GPIN_NUM_SIMD
:
416 result
->u32
= rctx
->screen
->info
.num_good_compute_units
;
418 case R600_QUERY_GPIN_NUM_RB
:
419 result
->u32
= rctx
->screen
->info
.num_render_backends
;
421 case R600_QUERY_GPIN_NUM_SPI
:
422 result
->u32
= 1; /* all supported chips have one SPI per SE */
424 case R600_QUERY_GPIN_NUM_SE
:
425 result
->u32
= rctx
->screen
->info
.max_se
;
429 result
->u64
= query
->end_result
- query
->begin_result
;
431 switch (query
->b
.type
) {
432 case R600_QUERY_BUFFER_WAIT_TIME
:
433 case R600_QUERY_GPU_TEMPERATURE
:
436 case R600_QUERY_CURRENT_GPU_SCLK
:
437 case R600_QUERY_CURRENT_GPU_MCLK
:
438 result
->u64
*= 1000000;
446 static struct r600_query_ops sw_query_ops
= {
447 .destroy
= r600_query_sw_destroy
,
448 .begin
= r600_query_sw_begin
,
449 .end
= r600_query_sw_end
,
450 .get_result
= r600_query_sw_get_result
,
451 .get_result_resource
= NULL
454 static struct pipe_query
*r600_query_sw_create(unsigned query_type
)
456 struct r600_query_sw
*query
;
458 query
= CALLOC_STRUCT(r600_query_sw
);
462 query
->b
.type
= query_type
;
463 query
->b
.ops
= &sw_query_ops
;
465 return (struct pipe_query
*)query
;
468 void r600_query_hw_destroy(struct r600_common_screen
*rscreen
,
469 struct r600_query
*rquery
)
471 struct r600_query_hw
*query
= (struct r600_query_hw
*)rquery
;
472 struct r600_query_buffer
*prev
= query
->buffer
.previous
;
474 /* Release all query buffers. */
476 struct r600_query_buffer
*qbuf
= prev
;
477 prev
= prev
->previous
;
478 r600_resource_reference(&qbuf
->buf
, NULL
);
482 r600_resource_reference(&query
->buffer
.buf
, NULL
);
486 static struct r600_resource
*r600_new_query_buffer(struct r600_common_screen
*rscreen
,
487 struct r600_query_hw
*query
)
489 unsigned buf_size
= MAX2(query
->result_size
,
490 rscreen
->info
.min_alloc_size
);
492 /* Queries are normally read by the CPU after
493 * being written by the gpu, hence staging is probably a good
496 struct r600_resource
*buf
= (struct r600_resource
*)
497 pipe_buffer_create(&rscreen
->b
, 0,
498 PIPE_USAGE_STAGING
, buf_size
);
502 if (!query
->ops
->prepare_buffer(rscreen
, query
, buf
)) {
503 r600_resource_reference(&buf
, NULL
);
510 static bool r600_query_hw_prepare_buffer(struct r600_common_screen
*rscreen
,
511 struct r600_query_hw
*query
,
512 struct r600_resource
*buffer
)
514 /* Callers ensure that the buffer is currently unused by the GPU. */
515 uint32_t *results
= rscreen
->ws
->buffer_map(buffer
->buf
, NULL
,
516 PIPE_TRANSFER_WRITE
|
517 PIPE_TRANSFER_UNSYNCHRONIZED
);
521 memset(results
, 0, buffer
->b
.b
.width0
);
523 if (query
->b
.type
== PIPE_QUERY_OCCLUSION_COUNTER
||
524 query
->b
.type
== PIPE_QUERY_OCCLUSION_PREDICATE
) {
525 unsigned max_rbs
= rscreen
->info
.num_render_backends
;
526 unsigned enabled_rb_mask
= rscreen
->info
.enabled_rb_mask
;
527 unsigned num_results
;
530 /* Set top bits for unused backends. */
531 num_results
= buffer
->b
.b
.width0
/ query
->result_size
;
532 for (j
= 0; j
< num_results
; j
++) {
533 for (i
= 0; i
< max_rbs
; i
++) {
534 if (!(enabled_rb_mask
& (1<<i
))) {
535 results
[(i
* 4)+1] = 0x80000000;
536 results
[(i
* 4)+3] = 0x80000000;
539 results
+= 4 * max_rbs
;
546 static void r600_query_hw_get_result_resource(struct r600_common_context
*rctx
,
547 struct r600_query
*rquery
,
549 enum pipe_query_value_type result_type
,
551 struct pipe_resource
*resource
,
554 static struct r600_query_ops query_hw_ops
= {
555 .destroy
= r600_query_hw_destroy
,
556 .begin
= r600_query_hw_begin
,
557 .end
= r600_query_hw_end
,
558 .get_result
= r600_query_hw_get_result
,
559 .get_result_resource
= r600_query_hw_get_result_resource
,
562 static void r600_query_hw_do_emit_start(struct r600_common_context
*ctx
,
563 struct r600_query_hw
*query
,
564 struct r600_resource
*buffer
,
566 static void r600_query_hw_do_emit_stop(struct r600_common_context
*ctx
,
567 struct r600_query_hw
*query
,
568 struct r600_resource
*buffer
,
570 static void r600_query_hw_add_result(struct r600_common_screen
*rscreen
,
571 struct r600_query_hw
*, void *buffer
,
572 union pipe_query_result
*result
);
573 static void r600_query_hw_clear_result(struct r600_query_hw
*,
574 union pipe_query_result
*);
576 static struct r600_query_hw_ops query_hw_default_hw_ops
= {
577 .prepare_buffer
= r600_query_hw_prepare_buffer
,
578 .emit_start
= r600_query_hw_do_emit_start
,
579 .emit_stop
= r600_query_hw_do_emit_stop
,
580 .clear_result
= r600_query_hw_clear_result
,
581 .add_result
= r600_query_hw_add_result
,
584 bool r600_query_hw_init(struct r600_common_screen
*rscreen
,
585 struct r600_query_hw
*query
)
587 query
->buffer
.buf
= r600_new_query_buffer(rscreen
, query
);
588 if (!query
->buffer
.buf
)
594 static struct pipe_query
*r600_query_hw_create(struct r600_common_screen
*rscreen
,
598 struct r600_query_hw
*query
= CALLOC_STRUCT(r600_query_hw
);
602 query
->b
.type
= query_type
;
603 query
->b
.ops
= &query_hw_ops
;
604 query
->ops
= &query_hw_default_hw_ops
;
606 switch (query_type
) {
607 case PIPE_QUERY_OCCLUSION_COUNTER
:
608 case PIPE_QUERY_OCCLUSION_PREDICATE
:
609 query
->result_size
= 16 * rscreen
->info
.num_render_backends
;
610 query
->result_size
+= 16; /* for the fence + alignment */
611 query
->num_cs_dw_begin
= 6;
612 query
->num_cs_dw_end
= 6 + r600_gfx_write_fence_dwords(rscreen
);
614 case PIPE_QUERY_TIME_ELAPSED
:
615 query
->result_size
= 24;
616 query
->num_cs_dw_begin
= 8;
617 query
->num_cs_dw_end
= 8 + r600_gfx_write_fence_dwords(rscreen
);
619 case PIPE_QUERY_TIMESTAMP
:
620 query
->result_size
= 16;
621 query
->num_cs_dw_end
= 8 + r600_gfx_write_fence_dwords(rscreen
);
622 query
->flags
= R600_QUERY_HW_FLAG_NO_START
;
624 case PIPE_QUERY_PRIMITIVES_EMITTED
:
625 case PIPE_QUERY_PRIMITIVES_GENERATED
:
626 case PIPE_QUERY_SO_STATISTICS
:
627 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
628 /* NumPrimitivesWritten, PrimitiveStorageNeeded. */
629 query
->result_size
= 32;
630 query
->num_cs_dw_begin
= 6;
631 query
->num_cs_dw_end
= 6;
632 query
->stream
= index
;
634 case PIPE_QUERY_PIPELINE_STATISTICS
:
635 /* 11 values on EG, 8 on R600. */
636 query
->result_size
= (rscreen
->chip_class
>= EVERGREEN
? 11 : 8) * 16;
637 query
->result_size
+= 8; /* for the fence + alignment */
638 query
->num_cs_dw_begin
= 6;
639 query
->num_cs_dw_end
= 6 + r600_gfx_write_fence_dwords(rscreen
);
647 if (!r600_query_hw_init(rscreen
, query
)) {
652 return (struct pipe_query
*)query
;
655 static void r600_update_occlusion_query_state(struct r600_common_context
*rctx
,
656 unsigned type
, int diff
)
658 if (type
== PIPE_QUERY_OCCLUSION_COUNTER
||
659 type
== PIPE_QUERY_OCCLUSION_PREDICATE
) {
660 bool old_enable
= rctx
->num_occlusion_queries
!= 0;
661 bool old_perfect_enable
=
662 rctx
->num_perfect_occlusion_queries
!= 0;
663 bool enable
, perfect_enable
;
665 rctx
->num_occlusion_queries
+= diff
;
666 assert(rctx
->num_occlusion_queries
>= 0);
668 if (type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
669 rctx
->num_perfect_occlusion_queries
+= diff
;
670 assert(rctx
->num_perfect_occlusion_queries
>= 0);
673 enable
= rctx
->num_occlusion_queries
!= 0;
674 perfect_enable
= rctx
->num_perfect_occlusion_queries
!= 0;
676 if (enable
!= old_enable
|| perfect_enable
!= old_perfect_enable
) {
677 rctx
->set_occlusion_query_state(&rctx
->b
, enable
);
682 static unsigned event_type_for_stream(struct r600_query_hw
*query
)
684 switch (query
->stream
) {
686 case 0: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS
;
687 case 1: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS1
;
688 case 2: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS2
;
689 case 3: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS3
;
693 static void r600_query_hw_do_emit_start(struct r600_common_context
*ctx
,
694 struct r600_query_hw
*query
,
695 struct r600_resource
*buffer
,
698 struct radeon_winsys_cs
*cs
= ctx
->gfx
.cs
;
700 switch (query
->b
.type
) {
701 case PIPE_QUERY_OCCLUSION_COUNTER
:
702 case PIPE_QUERY_OCCLUSION_PREDICATE
:
703 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
704 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1));
706 radeon_emit(cs
, va
>> 32);
708 case PIPE_QUERY_PRIMITIVES_EMITTED
:
709 case PIPE_QUERY_PRIMITIVES_GENERATED
:
710 case PIPE_QUERY_SO_STATISTICS
:
711 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
712 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
713 radeon_emit(cs
, EVENT_TYPE(event_type_for_stream(query
)) | EVENT_INDEX(3));
715 radeon_emit(cs
, va
>> 32);
717 case PIPE_QUERY_TIME_ELAPSED
:
718 if (ctx
->chip_class
>= SI
) {
719 /* Write the timestamp from the CP not waiting for
720 * outstanding draws (top-of-pipe).
722 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, 0));
723 radeon_emit(cs
, COPY_DATA_COUNT_SEL
|
724 COPY_DATA_SRC_SEL(COPY_DATA_TIMESTAMP
) |
725 COPY_DATA_DST_SEL(COPY_DATA_MEM_ASYNC
));
729 radeon_emit(cs
, va
>> 32);
731 /* Write the timestamp after the last draw is done.
734 r600_gfx_write_event_eop(ctx
, EVENT_TYPE_BOTTOM_OF_PIPE_TS
,
735 0, 3, NULL
, va
, 0, 0);
738 case PIPE_QUERY_PIPELINE_STATISTICS
:
739 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
740 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT
) | EVENT_INDEX(2));
742 radeon_emit(cs
, va
>> 32);
747 r600_emit_reloc(ctx
, &ctx
->gfx
, query
->buffer
.buf
, RADEON_USAGE_WRITE
,
751 static void r600_query_hw_emit_start(struct r600_common_context
*ctx
,
752 struct r600_query_hw
*query
)
756 if (!query
->buffer
.buf
)
757 return; // previous buffer allocation failure
759 r600_update_occlusion_query_state(ctx
, query
->b
.type
, 1);
760 r600_update_prims_generated_query_state(ctx
, query
->b
.type
, 1);
762 ctx
->need_gfx_cs_space(&ctx
->b
, query
->num_cs_dw_begin
+ query
->num_cs_dw_end
,
765 /* Get a new query buffer if needed. */
766 if (query
->buffer
.results_end
+ query
->result_size
> query
->buffer
.buf
->b
.b
.width0
) {
767 struct r600_query_buffer
*qbuf
= MALLOC_STRUCT(r600_query_buffer
);
768 *qbuf
= query
->buffer
;
769 query
->buffer
.results_end
= 0;
770 query
->buffer
.previous
= qbuf
;
771 query
->buffer
.buf
= r600_new_query_buffer(ctx
->screen
, query
);
772 if (!query
->buffer
.buf
)
776 /* emit begin query */
777 va
= query
->buffer
.buf
->gpu_address
+ query
->buffer
.results_end
;
779 query
->ops
->emit_start(ctx
, query
, query
->buffer
.buf
, va
);
781 ctx
->num_cs_dw_queries_suspend
+= query
->num_cs_dw_end
;
784 static void r600_query_hw_do_emit_stop(struct r600_common_context
*ctx
,
785 struct r600_query_hw
*query
,
786 struct r600_resource
*buffer
,
789 struct radeon_winsys_cs
*cs
= ctx
->gfx
.cs
;
790 uint64_t fence_va
= 0;
792 switch (query
->b
.type
) {
793 case PIPE_QUERY_OCCLUSION_COUNTER
:
794 case PIPE_QUERY_OCCLUSION_PREDICATE
:
796 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
797 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1));
799 radeon_emit(cs
, va
>> 32);
801 fence_va
= va
+ ctx
->screen
->info
.num_render_backends
* 16 - 8;
803 case PIPE_QUERY_PRIMITIVES_EMITTED
:
804 case PIPE_QUERY_PRIMITIVES_GENERATED
:
805 case PIPE_QUERY_SO_STATISTICS
:
806 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
807 va
+= query
->result_size
/2;
808 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
809 radeon_emit(cs
, EVENT_TYPE(event_type_for_stream(query
)) | EVENT_INDEX(3));
811 radeon_emit(cs
, va
>> 32);
813 case PIPE_QUERY_TIME_ELAPSED
:
816 case PIPE_QUERY_TIMESTAMP
:
817 r600_gfx_write_event_eop(ctx
, EVENT_TYPE_BOTTOM_OF_PIPE_TS
,
818 0, 3, NULL
, va
, 0, 0);
821 case PIPE_QUERY_PIPELINE_STATISTICS
: {
822 unsigned sample_size
= (query
->result_size
- 8) / 2;
825 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
826 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT
) | EVENT_INDEX(2));
828 radeon_emit(cs
, va
>> 32);
830 fence_va
= va
+ sample_size
;
836 r600_emit_reloc(ctx
, &ctx
->gfx
, query
->buffer
.buf
, RADEON_USAGE_WRITE
,
840 r600_gfx_write_event_eop(ctx
, EVENT_TYPE_BOTTOM_OF_PIPE_TS
, 0, 1,
841 query
->buffer
.buf
, fence_va
, 0, 0x80000000);
844 static void r600_query_hw_emit_stop(struct r600_common_context
*ctx
,
845 struct r600_query_hw
*query
)
849 if (!query
->buffer
.buf
)
850 return; // previous buffer allocation failure
852 /* The queries which need begin already called this in begin_query. */
853 if (query
->flags
& R600_QUERY_HW_FLAG_NO_START
) {
854 ctx
->need_gfx_cs_space(&ctx
->b
, query
->num_cs_dw_end
, false);
858 va
= query
->buffer
.buf
->gpu_address
+ query
->buffer
.results_end
;
860 query
->ops
->emit_stop(ctx
, query
, query
->buffer
.buf
, va
);
862 query
->buffer
.results_end
+= query
->result_size
;
864 if (!(query
->flags
& R600_QUERY_HW_FLAG_NO_START
))
865 ctx
->num_cs_dw_queries_suspend
-= query
->num_cs_dw_end
;
867 r600_update_occlusion_query_state(ctx
, query
->b
.type
, -1);
868 r600_update_prims_generated_query_state(ctx
, query
->b
.type
, -1);
871 static void r600_emit_query_predication(struct r600_common_context
*ctx
,
872 struct r600_atom
*atom
)
874 struct radeon_winsys_cs
*cs
= ctx
->gfx
.cs
;
875 struct r600_query_hw
*query
= (struct r600_query_hw
*)ctx
->render_cond
;
876 struct r600_query_buffer
*qbuf
;
883 flag_wait
= ctx
->render_cond_mode
== PIPE_RENDER_COND_WAIT
||
884 ctx
->render_cond_mode
== PIPE_RENDER_COND_BY_REGION_WAIT
;
886 switch (query
->b
.type
) {
887 case PIPE_QUERY_OCCLUSION_COUNTER
:
888 case PIPE_QUERY_OCCLUSION_PREDICATE
:
889 op
= PRED_OP(PREDICATION_OP_ZPASS
);
891 case PIPE_QUERY_PRIMITIVES_EMITTED
:
892 case PIPE_QUERY_PRIMITIVES_GENERATED
:
893 case PIPE_QUERY_SO_STATISTICS
:
894 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
895 op
= PRED_OP(PREDICATION_OP_PRIMCOUNT
);
902 /* if true then invert, see GL_ARB_conditional_render_inverted */
903 if (ctx
->render_cond_invert
)
904 op
|= PREDICATION_DRAW_NOT_VISIBLE
; /* Draw if not visable/overflow */
906 op
|= PREDICATION_DRAW_VISIBLE
; /* Draw if visable/overflow */
908 op
|= flag_wait
? PREDICATION_HINT_WAIT
: PREDICATION_HINT_NOWAIT_DRAW
;
910 /* emit predicate packets for all data blocks */
911 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
912 unsigned results_base
= 0;
913 uint64_t va_base
= qbuf
->buf
->gpu_address
;
915 while (results_base
< qbuf
->results_end
) {
916 uint64_t va
= va_base
+ results_base
;
918 if (ctx
->chip_class
>= GFX9
) {
919 radeon_emit(cs
, PKT3(PKT3_SET_PREDICATION
, 2, 0));
922 radeon_emit(cs
, va
>> 32);
924 radeon_emit(cs
, PKT3(PKT3_SET_PREDICATION
, 1, 0));
926 radeon_emit(cs
, op
| ((va
>> 32) & 0xFF));
928 r600_emit_reloc(ctx
, &ctx
->gfx
, qbuf
->buf
, RADEON_USAGE_READ
,
930 results_base
+= query
->result_size
;
932 /* set CONTINUE bit for all packets except the first */
933 op
|= PREDICATION_CONTINUE
;
938 static struct pipe_query
*r600_create_query(struct pipe_context
*ctx
, unsigned query_type
, unsigned index
)
940 struct r600_common_screen
*rscreen
=
941 (struct r600_common_screen
*)ctx
->screen
;
943 if (query_type
== PIPE_QUERY_TIMESTAMP_DISJOINT
||
944 query_type
== PIPE_QUERY_GPU_FINISHED
||
945 query_type
>= PIPE_QUERY_DRIVER_SPECIFIC
)
946 return r600_query_sw_create(query_type
);
948 return r600_query_hw_create(rscreen
, query_type
, index
);
951 static void r600_destroy_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
953 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
954 struct r600_query
*rquery
= (struct r600_query
*)query
;
956 rquery
->ops
->destroy(rctx
->screen
, rquery
);
959 static boolean
r600_begin_query(struct pipe_context
*ctx
,
960 struct pipe_query
*query
)
962 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
963 struct r600_query
*rquery
= (struct r600_query
*)query
;
965 return rquery
->ops
->begin(rctx
, rquery
);
968 void r600_query_hw_reset_buffers(struct r600_common_context
*rctx
,
969 struct r600_query_hw
*query
)
971 struct r600_query_buffer
*prev
= query
->buffer
.previous
;
973 /* Discard the old query buffers. */
975 struct r600_query_buffer
*qbuf
= prev
;
976 prev
= prev
->previous
;
977 r600_resource_reference(&qbuf
->buf
, NULL
);
981 query
->buffer
.results_end
= 0;
982 query
->buffer
.previous
= NULL
;
984 /* Obtain a new buffer if the current one can't be mapped without a stall. */
985 if (r600_rings_is_buffer_referenced(rctx
, query
->buffer
.buf
->buf
, RADEON_USAGE_READWRITE
) ||
986 !rctx
->ws
->buffer_wait(query
->buffer
.buf
->buf
, 0, RADEON_USAGE_READWRITE
)) {
987 r600_resource_reference(&query
->buffer
.buf
, NULL
);
988 query
->buffer
.buf
= r600_new_query_buffer(rctx
->screen
, query
);
990 if (!query
->ops
->prepare_buffer(rctx
->screen
, query
, query
->buffer
.buf
))
991 r600_resource_reference(&query
->buffer
.buf
, NULL
);
995 bool r600_query_hw_begin(struct r600_common_context
*rctx
,
996 struct r600_query
*rquery
)
998 struct r600_query_hw
*query
= (struct r600_query_hw
*)rquery
;
1000 if (query
->flags
& R600_QUERY_HW_FLAG_NO_START
) {
1005 if (!(query
->flags
& R600_QUERY_HW_FLAG_BEGIN_RESUMES
))
1006 r600_query_hw_reset_buffers(rctx
, query
);
1008 r600_query_hw_emit_start(rctx
, query
);
1009 if (!query
->buffer
.buf
)
1012 LIST_ADDTAIL(&query
->list
, &rctx
->active_queries
);
1016 static bool r600_end_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
1018 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
1019 struct r600_query
*rquery
= (struct r600_query
*)query
;
1021 return rquery
->ops
->end(rctx
, rquery
);
1024 bool r600_query_hw_end(struct r600_common_context
*rctx
,
1025 struct r600_query
*rquery
)
1027 struct r600_query_hw
*query
= (struct r600_query_hw
*)rquery
;
1029 if (query
->flags
& R600_QUERY_HW_FLAG_NO_START
)
1030 r600_query_hw_reset_buffers(rctx
, query
);
1032 r600_query_hw_emit_stop(rctx
, query
);
1034 if (!(query
->flags
& R600_QUERY_HW_FLAG_NO_START
))
1035 LIST_DELINIT(&query
->list
);
1037 if (!query
->buffer
.buf
)
1043 static void r600_get_hw_query_params(struct r600_common_context
*rctx
,
1044 struct r600_query_hw
*rquery
, int index
,
1045 struct r600_hw_query_params
*params
)
1047 unsigned max_rbs
= rctx
->screen
->info
.num_render_backends
;
1049 params
->pair_stride
= 0;
1050 params
->pair_count
= 1;
1052 switch (rquery
->b
.type
) {
1053 case PIPE_QUERY_OCCLUSION_COUNTER
:
1054 case PIPE_QUERY_OCCLUSION_PREDICATE
:
1055 params
->start_offset
= 0;
1056 params
->end_offset
= 8;
1057 params
->fence_offset
= max_rbs
* 16;
1058 params
->pair_stride
= 16;
1059 params
->pair_count
= max_rbs
;
1061 case PIPE_QUERY_TIME_ELAPSED
:
1062 params
->start_offset
= 0;
1063 params
->end_offset
= 8;
1064 params
->fence_offset
= 16;
1066 case PIPE_QUERY_TIMESTAMP
:
1067 params
->start_offset
= 0;
1068 params
->end_offset
= 0;
1069 params
->fence_offset
= 8;
1071 case PIPE_QUERY_PRIMITIVES_EMITTED
:
1072 params
->start_offset
= 8;
1073 params
->end_offset
= 24;
1074 params
->fence_offset
= params
->end_offset
+ 4;
1076 case PIPE_QUERY_PRIMITIVES_GENERATED
:
1077 params
->start_offset
= 0;
1078 params
->end_offset
= 16;
1079 params
->fence_offset
= params
->end_offset
+ 4;
1081 case PIPE_QUERY_SO_STATISTICS
:
1082 params
->start_offset
= 8 - index
* 8;
1083 params
->end_offset
= 24 - index
* 8;
1084 params
->fence_offset
= params
->end_offset
+ 4;
1086 case PIPE_QUERY_PIPELINE_STATISTICS
:
1088 /* Offsets apply to EG+ */
1089 static const unsigned offsets
[] = {56, 48, 24, 32, 40, 16, 8, 0, 64, 72, 80};
1090 params
->start_offset
= offsets
[index
];
1091 params
->end_offset
= 88 + offsets
[index
];
1092 params
->fence_offset
= 2 * 88;
1096 unreachable("r600_get_hw_query_params unsupported");
1100 static unsigned r600_query_read_result(void *map
, unsigned start_index
, unsigned end_index
,
1101 bool test_status_bit
)
1103 uint32_t *current_result
= (uint32_t*)map
;
1104 uint64_t start
, end
;
1106 start
= (uint64_t)current_result
[start_index
] |
1107 (uint64_t)current_result
[start_index
+1] << 32;
1108 end
= (uint64_t)current_result
[end_index
] |
1109 (uint64_t)current_result
[end_index
+1] << 32;
1111 if (!test_status_bit
||
1112 ((start
& 0x8000000000000000UL
) && (end
& 0x8000000000000000UL
))) {
1118 static void r600_query_hw_add_result(struct r600_common_screen
*rscreen
,
1119 struct r600_query_hw
*query
,
1121 union pipe_query_result
*result
)
1123 unsigned max_rbs
= rscreen
->info
.num_render_backends
;
1125 switch (query
->b
.type
) {
1126 case PIPE_QUERY_OCCLUSION_COUNTER
: {
1127 for (unsigned i
= 0; i
< max_rbs
; ++i
) {
1128 unsigned results_base
= i
* 16;
1130 r600_query_read_result(buffer
+ results_base
, 0, 2, true);
1134 case PIPE_QUERY_OCCLUSION_PREDICATE
: {
1135 for (unsigned i
= 0; i
< max_rbs
; ++i
) {
1136 unsigned results_base
= i
* 16;
1137 result
->b
= result
->b
||
1138 r600_query_read_result(buffer
+ results_base
, 0, 2, true) != 0;
1142 case PIPE_QUERY_TIME_ELAPSED
:
1143 result
->u64
+= r600_query_read_result(buffer
, 0, 2, false);
1145 case PIPE_QUERY_TIMESTAMP
:
1146 result
->u64
= *(uint64_t*)buffer
;
1148 case PIPE_QUERY_PRIMITIVES_EMITTED
:
1149 /* SAMPLE_STREAMOUTSTATS stores this structure:
1151 * u64 NumPrimitivesWritten;
1152 * u64 PrimitiveStorageNeeded;
1154 * We only need NumPrimitivesWritten here. */
1155 result
->u64
+= r600_query_read_result(buffer
, 2, 6, true);
1157 case PIPE_QUERY_PRIMITIVES_GENERATED
:
1158 /* Here we read PrimitiveStorageNeeded. */
1159 result
->u64
+= r600_query_read_result(buffer
, 0, 4, true);
1161 case PIPE_QUERY_SO_STATISTICS
:
1162 result
->so_statistics
.num_primitives_written
+=
1163 r600_query_read_result(buffer
, 2, 6, true);
1164 result
->so_statistics
.primitives_storage_needed
+=
1165 r600_query_read_result(buffer
, 0, 4, true);
1167 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
1168 result
->b
= result
->b
||
1169 r600_query_read_result(buffer
, 2, 6, true) !=
1170 r600_query_read_result(buffer
, 0, 4, true);
1172 case PIPE_QUERY_PIPELINE_STATISTICS
:
1173 if (rscreen
->chip_class
>= EVERGREEN
) {
1174 result
->pipeline_statistics
.ps_invocations
+=
1175 r600_query_read_result(buffer
, 0, 22, false);
1176 result
->pipeline_statistics
.c_primitives
+=
1177 r600_query_read_result(buffer
, 2, 24, false);
1178 result
->pipeline_statistics
.c_invocations
+=
1179 r600_query_read_result(buffer
, 4, 26, false);
1180 result
->pipeline_statistics
.vs_invocations
+=
1181 r600_query_read_result(buffer
, 6, 28, false);
1182 result
->pipeline_statistics
.gs_invocations
+=
1183 r600_query_read_result(buffer
, 8, 30, false);
1184 result
->pipeline_statistics
.gs_primitives
+=
1185 r600_query_read_result(buffer
, 10, 32, false);
1186 result
->pipeline_statistics
.ia_primitives
+=
1187 r600_query_read_result(buffer
, 12, 34, false);
1188 result
->pipeline_statistics
.ia_vertices
+=
1189 r600_query_read_result(buffer
, 14, 36, false);
1190 result
->pipeline_statistics
.hs_invocations
+=
1191 r600_query_read_result(buffer
, 16, 38, false);
1192 result
->pipeline_statistics
.ds_invocations
+=
1193 r600_query_read_result(buffer
, 18, 40, false);
1194 result
->pipeline_statistics
.cs_invocations
+=
1195 r600_query_read_result(buffer
, 20, 42, false);
1197 result
->pipeline_statistics
.ps_invocations
+=
1198 r600_query_read_result(buffer
, 0, 16, false);
1199 result
->pipeline_statistics
.c_primitives
+=
1200 r600_query_read_result(buffer
, 2, 18, false);
1201 result
->pipeline_statistics
.c_invocations
+=
1202 r600_query_read_result(buffer
, 4, 20, false);
1203 result
->pipeline_statistics
.vs_invocations
+=
1204 r600_query_read_result(buffer
, 6, 22, false);
1205 result
->pipeline_statistics
.gs_invocations
+=
1206 r600_query_read_result(buffer
, 8, 24, false);
1207 result
->pipeline_statistics
.gs_primitives
+=
1208 r600_query_read_result(buffer
, 10, 26, false);
1209 result
->pipeline_statistics
.ia_primitives
+=
1210 r600_query_read_result(buffer
, 12, 28, false);
1211 result
->pipeline_statistics
.ia_vertices
+=
1212 r600_query_read_result(buffer
, 14, 30, false);
1214 #if 0 /* for testing */
1215 printf("Pipeline stats: IA verts=%llu, IA prims=%llu, VS=%llu, HS=%llu, "
1216 "DS=%llu, GS=%llu, GS prims=%llu, Clipper=%llu, "
1217 "Clipper prims=%llu, PS=%llu, CS=%llu\n",
1218 result
->pipeline_statistics
.ia_vertices
,
1219 result
->pipeline_statistics
.ia_primitives
,
1220 result
->pipeline_statistics
.vs_invocations
,
1221 result
->pipeline_statistics
.hs_invocations
,
1222 result
->pipeline_statistics
.ds_invocations
,
1223 result
->pipeline_statistics
.gs_invocations
,
1224 result
->pipeline_statistics
.gs_primitives
,
1225 result
->pipeline_statistics
.c_invocations
,
1226 result
->pipeline_statistics
.c_primitives
,
1227 result
->pipeline_statistics
.ps_invocations
,
1228 result
->pipeline_statistics
.cs_invocations
);
1236 static boolean
r600_get_query_result(struct pipe_context
*ctx
,
1237 struct pipe_query
*query
, boolean wait
,
1238 union pipe_query_result
*result
)
1240 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
1241 struct r600_query
*rquery
= (struct r600_query
*)query
;
1243 return rquery
->ops
->get_result(rctx
, rquery
, wait
, result
);
1246 static void r600_get_query_result_resource(struct pipe_context
*ctx
,
1247 struct pipe_query
*query
,
1249 enum pipe_query_value_type result_type
,
1251 struct pipe_resource
*resource
,
1254 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
1255 struct r600_query
*rquery
= (struct r600_query
*)query
;
1257 rquery
->ops
->get_result_resource(rctx
, rquery
, wait
, result_type
, index
,
1261 static void r600_query_hw_clear_result(struct r600_query_hw
*query
,
1262 union pipe_query_result
*result
)
1264 util_query_clear_result(result
, query
->b
.type
);
1267 bool r600_query_hw_get_result(struct r600_common_context
*rctx
,
1268 struct r600_query
*rquery
,
1269 bool wait
, union pipe_query_result
*result
)
1271 struct r600_common_screen
*rscreen
= rctx
->screen
;
1272 struct r600_query_hw
*query
= (struct r600_query_hw
*)rquery
;
1273 struct r600_query_buffer
*qbuf
;
1275 query
->ops
->clear_result(query
, result
);
1277 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
1278 unsigned usage
= PIPE_TRANSFER_READ
|
1279 (wait
? 0 : PIPE_TRANSFER_DONTBLOCK
);
1280 unsigned results_base
= 0;
1283 if (rquery
->b
.flushed
)
1284 map
= rctx
->ws
->buffer_map(qbuf
->buf
->buf
, NULL
, usage
);
1286 map
= r600_buffer_map_sync_with_rings(rctx
, qbuf
->buf
, usage
);
1291 while (results_base
!= qbuf
->results_end
) {
1292 query
->ops
->add_result(rscreen
, query
, map
+ results_base
,
1294 results_base
+= query
->result_size
;
1298 /* Convert the time to expected units. */
1299 if (rquery
->type
== PIPE_QUERY_TIME_ELAPSED
||
1300 rquery
->type
== PIPE_QUERY_TIMESTAMP
) {
1301 result
->u64
= (1000000 * result
->u64
) / rscreen
->info
.clock_crystal_freq
;
1306 /* Create the compute shader that is used to collect the results.
1308 * One compute grid with a single thread is launched for every query result
1309 * buffer. The thread (optionally) reads a previous summary buffer, then
1310 * accumulates data from the query result buffer, and writes the result either
1311 * to a summary buffer to be consumed by the next grid invocation or to the
1312 * user-supplied buffer.
1318 * 0.y = result_stride
1319 * 0.z = result_count
1321 * 1: read previously accumulated values
1322 * 2: write accumulated values for chaining
1323 * 4: write result available
1324 * 8: convert result to boolean (0/1)
1325 * 16: only read one dword and use that as result
1326 * 32: apply timestamp conversion
1327 * 64: store full 64 bits result
1328 * 128: store signed 32 bits result
1329 * 1.x = fence_offset
1333 * BUFFER[0] = query result buffer
1334 * BUFFER[1] = previous summary buffer
1335 * BUFFER[2] = next summary buffer or user-supplied buffer
1337 static void r600_create_query_result_shader(struct r600_common_context
*rctx
)
1339 /* TEMP[0].xy = accumulated result so far
1340 * TEMP[0].z = result not available
1342 * TEMP[1].x = current result index
1343 * TEMP[1].y = current pair index
1345 static const char text_tmpl
[] =
1347 "PROPERTY CS_FIXED_BLOCK_WIDTH 1\n"
1348 "PROPERTY CS_FIXED_BLOCK_HEIGHT 1\n"
1349 "PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
1355 "IMM[0] UINT32 {0, 31, 2147483647, 4294967295}\n"
1356 "IMM[1] UINT32 {1, 2, 4, 8}\n"
1357 "IMM[2] UINT32 {16, 32, 64, 128}\n"
1358 "IMM[3] UINT32 {1000000, 0, %u, 0}\n" /* for timestamp conversion */
1360 "AND TEMP[5], CONST[0].wwww, IMM[2].xxxx\n"
1362 /* Check result availability. */
1363 "LOAD TEMP[1].x, BUFFER[0], CONST[1].xxxx\n"
1364 "ISHR TEMP[0].z, TEMP[1].xxxx, IMM[0].yyyy\n"
1365 "MOV TEMP[1], TEMP[0].zzzz\n"
1366 "NOT TEMP[0].z, TEMP[0].zzzz\n"
1368 /* Load result if available. */
1370 "LOAD TEMP[0].xy, BUFFER[0], IMM[0].xxxx\n"
1373 /* Load previously accumulated result if requested. */
1374 "MOV TEMP[0], IMM[0].xxxx\n"
1375 "AND TEMP[4], CONST[0].wwww, IMM[1].xxxx\n"
1377 "LOAD TEMP[0].xyz, BUFFER[1], IMM[0].xxxx\n"
1380 "MOV TEMP[1].x, IMM[0].xxxx\n"
1382 /* Break if accumulated result so far is not available. */
1383 "UIF TEMP[0].zzzz\n"
1387 /* Break if result_index >= result_count. */
1388 "USGE TEMP[5], TEMP[1].xxxx, CONST[0].zzzz\n"
1393 /* Load fence and check result availability */
1394 "UMAD TEMP[5].x, TEMP[1].xxxx, CONST[0].yyyy, CONST[1].xxxx\n"
1395 "LOAD TEMP[5].x, BUFFER[0], TEMP[5].xxxx\n"
1396 "ISHR TEMP[0].z, TEMP[5].xxxx, IMM[0].yyyy\n"
1397 "NOT TEMP[0].z, TEMP[0].zzzz\n"
1398 "UIF TEMP[0].zzzz\n"
1402 "MOV TEMP[1].y, IMM[0].xxxx\n"
1404 /* Load start and end. */
1405 "UMUL TEMP[5].x, TEMP[1].xxxx, CONST[0].yyyy\n"
1406 "UMAD TEMP[5].x, TEMP[1].yyyy, CONST[1].yyyy, TEMP[5].xxxx\n"
1407 "LOAD TEMP[2].xy, BUFFER[0], TEMP[5].xxxx\n"
1409 "UADD TEMP[5].x, TEMP[5].xxxx, CONST[0].xxxx\n"
1410 "LOAD TEMP[3].xy, BUFFER[0], TEMP[5].xxxx\n"
1412 "U64ADD TEMP[3].xy, TEMP[3], -TEMP[2]\n"
1413 "U64ADD TEMP[0].xy, TEMP[0], TEMP[3]\n"
1415 /* Increment pair index */
1416 "UADD TEMP[1].y, TEMP[1].yyyy, IMM[1].xxxx\n"
1417 "USGE TEMP[5], TEMP[1].yyyy, CONST[1].zzzz\n"
1423 /* Increment result index */
1424 "UADD TEMP[1].x, TEMP[1].xxxx, IMM[1].xxxx\n"
1428 "AND TEMP[4], CONST[0].wwww, IMM[1].yyyy\n"
1430 /* Store accumulated data for chaining. */
1431 "STORE BUFFER[2].xyz, IMM[0].xxxx, TEMP[0]\n"
1433 "AND TEMP[4], CONST[0].wwww, IMM[1].zzzz\n"
1435 /* Store result availability. */
1436 "NOT TEMP[0].z, TEMP[0]\n"
1437 "AND TEMP[0].z, TEMP[0].zzzz, IMM[1].xxxx\n"
1438 "STORE BUFFER[2].x, IMM[0].xxxx, TEMP[0].zzzz\n"
1440 "AND TEMP[4], CONST[0].wwww, IMM[2].zzzz\n"
1442 "STORE BUFFER[2].y, IMM[0].xxxx, IMM[0].xxxx\n"
1445 /* Store result if it is available. */
1446 "NOT TEMP[4], TEMP[0].zzzz\n"
1448 /* Apply timestamp conversion */
1449 "AND TEMP[4], CONST[0].wwww, IMM[2].yyyy\n"
1451 "U64MUL TEMP[0].xy, TEMP[0], IMM[3].xyxy\n"
1452 "U64DIV TEMP[0].xy, TEMP[0], IMM[3].zwzw\n"
1455 /* Convert to boolean */
1456 "AND TEMP[4], CONST[0].wwww, IMM[1].wwww\n"
1458 "U64SNE TEMP[0].x, TEMP[0].xyxy, IMM[0].xxxx\n"
1459 "AND TEMP[0].x, TEMP[0].xxxx, IMM[1].xxxx\n"
1460 "MOV TEMP[0].y, IMM[0].xxxx\n"
1463 "AND TEMP[4], CONST[0].wwww, IMM[2].zzzz\n"
1465 "STORE BUFFER[2].xy, IMM[0].xxxx, TEMP[0].xyxy\n"
1468 "UIF TEMP[0].yyyy\n"
1469 "MOV TEMP[0].x, IMM[0].wwww\n"
1472 "AND TEMP[4], CONST[0].wwww, IMM[2].wwww\n"
1474 "UMIN TEMP[0].x, TEMP[0].xxxx, IMM[0].zzzz\n"
1477 "STORE BUFFER[2].x, IMM[0].xxxx, TEMP[0].xxxx\n"
1485 char text
[sizeof(text_tmpl
) + 32];
1486 struct tgsi_token tokens
[1024];
1487 struct pipe_compute_state state
= {};
1489 /* Hard code the frequency into the shader so that the backend can
1490 * use the full range of optimizations for divide-by-constant.
1492 snprintf(text
, sizeof(text
), text_tmpl
,
1493 rctx
->screen
->info
.clock_crystal_freq
);
1495 if (!tgsi_text_translate(text
, tokens
, ARRAY_SIZE(tokens
))) {
1500 state
.ir_type
= PIPE_SHADER_IR_TGSI
;
1501 state
.prog
= tokens
;
1503 rctx
->query_result_shader
= rctx
->b
.create_compute_state(&rctx
->b
, &state
);
1506 static void r600_restore_qbo_state(struct r600_common_context
*rctx
,
1507 struct r600_qbo_state
*st
)
1509 rctx
->b
.bind_compute_state(&rctx
->b
, st
->saved_compute
);
1511 rctx
->b
.set_constant_buffer(&rctx
->b
, PIPE_SHADER_COMPUTE
, 0, &st
->saved_const0
);
1512 pipe_resource_reference(&st
->saved_const0
.buffer
, NULL
);
1514 rctx
->b
.set_shader_buffers(&rctx
->b
, PIPE_SHADER_COMPUTE
, 0, 3, st
->saved_ssbo
);
1515 for (unsigned i
= 0; i
< 3; ++i
)
1516 pipe_resource_reference(&st
->saved_ssbo
[i
].buffer
, NULL
);
1519 static void r600_query_hw_get_result_resource(struct r600_common_context
*rctx
,
1520 struct r600_query
*rquery
,
1522 enum pipe_query_value_type result_type
,
1524 struct pipe_resource
*resource
,
1527 struct r600_query_hw
*query
= (struct r600_query_hw
*)rquery
;
1528 struct r600_query_buffer
*qbuf
;
1529 struct r600_query_buffer
*qbuf_prev
;
1530 struct pipe_resource
*tmp_buffer
= NULL
;
1531 unsigned tmp_buffer_offset
= 0;
1532 struct r600_qbo_state saved_state
= {};
1533 struct pipe_grid_info grid
= {};
1534 struct pipe_constant_buffer constant_buffer
= {};
1535 struct pipe_shader_buffer ssbo
[3];
1536 struct r600_hw_query_params params
;
1538 uint32_t end_offset
;
1539 uint32_t result_stride
;
1540 uint32_t result_count
;
1542 uint32_t fence_offset
;
1543 uint32_t pair_stride
;
1544 uint32_t pair_count
;
1547 if (!rctx
->query_result_shader
) {
1548 r600_create_query_result_shader(rctx
);
1549 if (!rctx
->query_result_shader
)
1553 if (query
->buffer
.previous
) {
1554 u_suballocator_alloc(rctx
->allocator_zeroed_memory
, 16, 16,
1555 &tmp_buffer_offset
, &tmp_buffer
);
1560 rctx
->save_qbo_state(&rctx
->b
, &saved_state
);
1562 r600_get_hw_query_params(rctx
, query
, index
>= 0 ? index
: 0, ¶ms
);
1563 consts
.end_offset
= params
.end_offset
- params
.start_offset
;
1564 consts
.fence_offset
= params
.fence_offset
- params
.start_offset
;
1565 consts
.result_stride
= query
->result_size
;
1566 consts
.pair_stride
= params
.pair_stride
;
1567 consts
.pair_count
= params
.pair_count
;
1569 constant_buffer
.buffer_size
= sizeof(consts
);
1570 constant_buffer
.user_buffer
= &consts
;
1572 ssbo
[1].buffer
= tmp_buffer
;
1573 ssbo
[1].buffer_offset
= tmp_buffer_offset
;
1574 ssbo
[1].buffer_size
= 16;
1578 rctx
->b
.bind_compute_state(&rctx
->b
, rctx
->query_result_shader
);
1590 if (query
->b
.type
== PIPE_QUERY_OCCLUSION_PREDICATE
||
1591 query
->b
.type
== PIPE_QUERY_SO_OVERFLOW_PREDICATE
)
1593 else if (query
->b
.type
== PIPE_QUERY_TIMESTAMP
||
1594 query
->b
.type
== PIPE_QUERY_TIME_ELAPSED
)
1595 consts
.config
|= 32;
1597 switch (result_type
) {
1598 case PIPE_QUERY_TYPE_U64
:
1599 case PIPE_QUERY_TYPE_I64
:
1600 consts
.config
|= 64;
1602 case PIPE_QUERY_TYPE_I32
:
1603 consts
.config
|= 128;
1605 case PIPE_QUERY_TYPE_U32
:
1609 rctx
->flags
|= rctx
->screen
->barrier_flags
.cp_to_L2
;
1611 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf_prev
) {
1612 if (query
->b
.type
!= PIPE_QUERY_TIMESTAMP
) {
1613 qbuf_prev
= qbuf
->previous
;
1614 consts
.result_count
= qbuf
->results_end
/ query
->result_size
;
1615 consts
.config
&= ~3;
1616 if (qbuf
!= &query
->buffer
)
1621 /* Only read the last timestamp. */
1623 consts
.result_count
= 0;
1624 consts
.config
|= 16;
1625 params
.start_offset
+= qbuf
->results_end
- query
->result_size
;
1628 rctx
->b
.set_constant_buffer(&rctx
->b
, PIPE_SHADER_COMPUTE
, 0, &constant_buffer
);
1630 ssbo
[0].buffer
= &qbuf
->buf
->b
.b
;
1631 ssbo
[0].buffer_offset
= params
.start_offset
;
1632 ssbo
[0].buffer_size
= qbuf
->results_end
- params
.start_offset
;
1634 if (!qbuf
->previous
) {
1635 ssbo
[2].buffer
= resource
;
1636 ssbo
[2].buffer_offset
= offset
;
1637 ssbo
[2].buffer_size
= 8;
1639 ((struct r600_resource
*)resource
)->TC_L2_dirty
= true;
1642 rctx
->b
.set_shader_buffers(&rctx
->b
, PIPE_SHADER_COMPUTE
, 0, 3, ssbo
);
1644 if (wait
&& qbuf
== &query
->buffer
) {
1647 /* Wait for result availability. Wait only for readiness
1648 * of the last entry, since the fence writes should be
1649 * serialized in the CP.
1651 va
= qbuf
->buf
->gpu_address
+ qbuf
->results_end
- query
->result_size
;
1652 va
+= params
.fence_offset
;
1654 r600_gfx_wait_fence(rctx
, va
, 0x80000000, 0x80000000);
1657 rctx
->b
.launch_grid(&rctx
->b
, &grid
);
1658 rctx
->flags
|= rctx
->screen
->barrier_flags
.compute_to_L2
;
1661 r600_restore_qbo_state(rctx
, &saved_state
);
1662 pipe_resource_reference(&tmp_buffer
, NULL
);
1665 static void r600_render_condition(struct pipe_context
*ctx
,
1666 struct pipe_query
*query
,
1668 enum pipe_render_cond_flag mode
)
1670 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
1671 struct r600_query_hw
*rquery
= (struct r600_query_hw
*)query
;
1672 struct r600_query_buffer
*qbuf
;
1673 struct r600_atom
*atom
= &rctx
->render_cond_atom
;
1675 rctx
->render_cond
= query
;
1676 rctx
->render_cond_invert
= condition
;
1677 rctx
->render_cond_mode
= mode
;
1679 /* Compute the size of SET_PREDICATION packets. */
1682 for (qbuf
= &rquery
->buffer
; qbuf
; qbuf
= qbuf
->previous
)
1683 atom
->num_dw
+= (qbuf
->results_end
/ rquery
->result_size
) * 5;
1686 rctx
->set_atom_dirty(rctx
, atom
, query
!= NULL
);
1689 void r600_suspend_queries(struct r600_common_context
*ctx
)
1691 struct r600_query_hw
*query
;
1693 LIST_FOR_EACH_ENTRY(query
, &ctx
->active_queries
, list
) {
1694 r600_query_hw_emit_stop(ctx
, query
);
1696 assert(ctx
->num_cs_dw_queries_suspend
== 0);
1699 static unsigned r600_queries_num_cs_dw_for_resuming(struct r600_common_context
*ctx
,
1700 struct list_head
*query_list
)
1702 struct r600_query_hw
*query
;
1703 unsigned num_dw
= 0;
1705 LIST_FOR_EACH_ENTRY(query
, query_list
, list
) {
1707 num_dw
+= query
->num_cs_dw_begin
+ query
->num_cs_dw_end
;
1709 /* Workaround for the fact that
1710 * num_cs_dw_nontimer_queries_suspend is incremented for every
1711 * resumed query, which raises the bar in need_cs_space for
1712 * queries about to be resumed.
1714 num_dw
+= query
->num_cs_dw_end
;
1716 /* primitives generated query */
1717 num_dw
+= ctx
->streamout
.enable_atom
.num_dw
;
1718 /* guess for ZPASS enable or PERFECT_ZPASS_COUNT enable updates */
1724 void r600_resume_queries(struct r600_common_context
*ctx
)
1726 struct r600_query_hw
*query
;
1727 unsigned num_cs_dw
= r600_queries_num_cs_dw_for_resuming(ctx
, &ctx
->active_queries
);
1729 assert(ctx
->num_cs_dw_queries_suspend
== 0);
1731 /* Check CS space here. Resuming must not be interrupted by flushes. */
1732 ctx
->need_gfx_cs_space(&ctx
->b
, num_cs_dw
, true);
1734 LIST_FOR_EACH_ENTRY(query
, &ctx
->active_queries
, list
) {
1735 r600_query_hw_emit_start(ctx
, query
);
1739 /* Fix radeon_info::enabled_rb_mask for R600, R700, EVERGREEN, NI. */
1740 void r600_query_fix_enabled_rb_mask(struct r600_common_screen
*rscreen
)
1742 struct r600_common_context
*ctx
=
1743 (struct r600_common_context
*)rscreen
->aux_context
;
1744 struct radeon_winsys_cs
*cs
= ctx
->gfx
.cs
;
1745 struct r600_resource
*buffer
;
1747 unsigned i
, mask
= 0;
1748 unsigned max_rbs
= ctx
->screen
->info
.num_render_backends
;
1750 assert(rscreen
->chip_class
<= CAYMAN
);
1752 /* if backend_map query is supported by the kernel */
1753 if (rscreen
->info
.r600_gb_backend_map_valid
) {
1754 unsigned num_tile_pipes
= rscreen
->info
.num_tile_pipes
;
1755 unsigned backend_map
= rscreen
->info
.r600_gb_backend_map
;
1756 unsigned item_width
, item_mask
;
1758 if (ctx
->chip_class
>= EVERGREEN
) {
1766 while (num_tile_pipes
--) {
1767 i
= backend_map
& item_mask
;
1769 backend_map
>>= item_width
;
1772 rscreen
->info
.enabled_rb_mask
= mask
;
1777 /* otherwise backup path for older kernels */
1779 /* create buffer for event data */
1780 buffer
= (struct r600_resource
*)
1781 pipe_buffer_create(ctx
->b
.screen
, 0,
1782 PIPE_USAGE_STAGING
, max_rbs
* 16);
1786 /* initialize buffer with zeroes */
1787 results
= r600_buffer_map_sync_with_rings(ctx
, buffer
, PIPE_TRANSFER_WRITE
);
1789 memset(results
, 0, max_rbs
* 4 * 4);
1791 /* emit EVENT_WRITE for ZPASS_DONE */
1792 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
1793 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1));
1794 radeon_emit(cs
, buffer
->gpu_address
);
1795 radeon_emit(cs
, buffer
->gpu_address
>> 32);
1797 r600_emit_reloc(ctx
, &ctx
->gfx
, buffer
,
1798 RADEON_USAGE_WRITE
, RADEON_PRIO_QUERY
);
1800 /* analyze results */
1801 results
= r600_buffer_map_sync_with_rings(ctx
, buffer
, PIPE_TRANSFER_READ
);
1803 for(i
= 0; i
< max_rbs
; i
++) {
1804 /* at least highest bit will be set if backend is used */
1805 if (results
[i
*4 + 1])
1811 r600_resource_reference(&buffer
, NULL
);
1814 rscreen
->info
.enabled_rb_mask
= mask
;
1817 #define XFULL(name_, query_type_, type_, result_type_, group_id_) \
1820 .query_type = R600_QUERY_##query_type_, \
1821 .type = PIPE_DRIVER_QUERY_TYPE_##type_, \
1822 .result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_##result_type_, \
1823 .group_id = group_id_ \
1826 #define X(name_, query_type_, type_, result_type_) \
1827 XFULL(name_, query_type_, type_, result_type_, ~(unsigned)0)
1829 #define XG(group_, name_, query_type_, type_, result_type_) \
1830 XFULL(name_, query_type_, type_, result_type_, R600_QUERY_GROUP_##group_)
1832 static struct pipe_driver_query_info r600_driver_query_list
[] = {
1833 X("num-compilations", NUM_COMPILATIONS
, UINT64
, CUMULATIVE
),
1834 X("num-shaders-created", NUM_SHADERS_CREATED
, UINT64
, CUMULATIVE
),
1835 X("num-shader-cache-hits", NUM_SHADER_CACHE_HITS
, UINT64
, CUMULATIVE
),
1836 X("draw-calls", DRAW_CALLS
, UINT64
, AVERAGE
),
1837 X("prim-restart-calls", PRIM_RESTART_CALLS
, UINT64
, AVERAGE
),
1838 X("spill-draw-calls", SPILL_DRAW_CALLS
, UINT64
, AVERAGE
),
1839 X("compute-calls", COMPUTE_CALLS
, UINT64
, AVERAGE
),
1840 X("spill-compute-calls", SPILL_COMPUTE_CALLS
, UINT64
, AVERAGE
),
1841 X("dma-calls", DMA_CALLS
, UINT64
, AVERAGE
),
1842 X("cp-dma-calls", CP_DMA_CALLS
, UINT64
, AVERAGE
),
1843 X("num-vs-flushes", NUM_VS_FLUSHES
, UINT64
, AVERAGE
),
1844 X("num-ps-flushes", NUM_PS_FLUSHES
, UINT64
, AVERAGE
),
1845 X("num-cs-flushes", NUM_CS_FLUSHES
, UINT64
, AVERAGE
),
1846 X("num-CB-cache-flushes", NUM_CB_CACHE_FLUSHES
, UINT64
, AVERAGE
),
1847 X("num-DB-cache-flushes", NUM_DB_CACHE_FLUSHES
, UINT64
, AVERAGE
),
1848 X("num-L2-invalidates", NUM_L2_INVALIDATES
, UINT64
, AVERAGE
),
1849 X("num-L2-writebacks", NUM_L2_WRITEBACKS
, UINT64
, AVERAGE
),
1850 X("num-resident-handles", NUM_RESIDENT_HANDLES
, UINT64
, AVERAGE
),
1851 X("tc-offloaded-slots", TC_OFFLOADED_SLOTS
, UINT64
, AVERAGE
),
1852 X("tc-direct-slots", TC_DIRECT_SLOTS
, UINT64
, AVERAGE
),
1853 X("tc-num-syncs", TC_NUM_SYNCS
, UINT64
, AVERAGE
),
1854 X("CS-thread-busy", CS_THREAD_BUSY
, UINT64
, AVERAGE
),
1855 X("gallium-thread-busy", GALLIUM_THREAD_BUSY
, UINT64
, AVERAGE
),
1856 X("requested-VRAM", REQUESTED_VRAM
, BYTES
, AVERAGE
),
1857 X("requested-GTT", REQUESTED_GTT
, BYTES
, AVERAGE
),
1858 X("mapped-VRAM", MAPPED_VRAM
, BYTES
, AVERAGE
),
1859 X("mapped-GTT", MAPPED_GTT
, BYTES
, AVERAGE
),
1860 X("buffer-wait-time", BUFFER_WAIT_TIME
, MICROSECONDS
, CUMULATIVE
),
1861 X("num-mapped-buffers", NUM_MAPPED_BUFFERS
, UINT64
, AVERAGE
),
1862 X("num-GFX-IBs", NUM_GFX_IBS
, UINT64
, AVERAGE
),
1863 X("num-SDMA-IBs", NUM_SDMA_IBS
, UINT64
, AVERAGE
),
1864 X("num-bytes-moved", NUM_BYTES_MOVED
, BYTES
, CUMULATIVE
),
1865 X("num-evictions", NUM_EVICTIONS
, UINT64
, CUMULATIVE
),
1866 X("VRAM-CPU-page-faults", NUM_VRAM_CPU_PAGE_FAULTS
, UINT64
, CUMULATIVE
),
1867 X("VRAM-usage", VRAM_USAGE
, BYTES
, AVERAGE
),
1868 X("VRAM-vis-usage", VRAM_VIS_USAGE
, BYTES
, AVERAGE
),
1869 X("GTT-usage", GTT_USAGE
, BYTES
, AVERAGE
),
1870 X("back-buffer-ps-draw-ratio", BACK_BUFFER_PS_DRAW_RATIO
, UINT64
, AVERAGE
),
1872 /* GPIN queries are for the benefit of old versions of GPUPerfStudio,
1873 * which use it as a fallback path to detect the GPU type.
1875 * Note: The names of these queries are significant for GPUPerfStudio
1876 * (and possibly their order as well). */
1877 XG(GPIN
, "GPIN_000", GPIN_ASIC_ID
, UINT
, AVERAGE
),
1878 XG(GPIN
, "GPIN_001", GPIN_NUM_SIMD
, UINT
, AVERAGE
),
1879 XG(GPIN
, "GPIN_002", GPIN_NUM_RB
, UINT
, AVERAGE
),
1880 XG(GPIN
, "GPIN_003", GPIN_NUM_SPI
, UINT
, AVERAGE
),
1881 XG(GPIN
, "GPIN_004", GPIN_NUM_SE
, UINT
, AVERAGE
),
1883 X("temperature", GPU_TEMPERATURE
, UINT64
, AVERAGE
),
1884 X("shader-clock", CURRENT_GPU_SCLK
, HZ
, AVERAGE
),
1885 X("memory-clock", CURRENT_GPU_MCLK
, HZ
, AVERAGE
),
1887 /* The following queries must be at the end of the list because their
1888 * availability is adjusted dynamically based on the DRM version. */
1889 X("GPU-load", GPU_LOAD
, UINT64
, AVERAGE
),
1890 X("GPU-shaders-busy", GPU_SHADERS_BUSY
, UINT64
, AVERAGE
),
1891 X("GPU-ta-busy", GPU_TA_BUSY
, UINT64
, AVERAGE
),
1892 X("GPU-gds-busy", GPU_GDS_BUSY
, UINT64
, AVERAGE
),
1893 X("GPU-vgt-busy", GPU_VGT_BUSY
, UINT64
, AVERAGE
),
1894 X("GPU-ia-busy", GPU_IA_BUSY
, UINT64
, AVERAGE
),
1895 X("GPU-sx-busy", GPU_SX_BUSY
, UINT64
, AVERAGE
),
1896 X("GPU-wd-busy", GPU_WD_BUSY
, UINT64
, AVERAGE
),
1897 X("GPU-bci-busy", GPU_BCI_BUSY
, UINT64
, AVERAGE
),
1898 X("GPU-sc-busy", GPU_SC_BUSY
, UINT64
, AVERAGE
),
1899 X("GPU-pa-busy", GPU_PA_BUSY
, UINT64
, AVERAGE
),
1900 X("GPU-db-busy", GPU_DB_BUSY
, UINT64
, AVERAGE
),
1901 X("GPU-cp-busy", GPU_CP_BUSY
, UINT64
, AVERAGE
),
1902 X("GPU-cb-busy", GPU_CB_BUSY
, UINT64
, AVERAGE
),
1903 X("GPU-sdma-busy", GPU_SDMA_BUSY
, UINT64
, AVERAGE
),
1904 X("GPU-pfp-busy", GPU_PFP_BUSY
, UINT64
, AVERAGE
),
1905 X("GPU-meq-busy", GPU_MEQ_BUSY
, UINT64
, AVERAGE
),
1906 X("GPU-me-busy", GPU_ME_BUSY
, UINT64
, AVERAGE
),
1907 X("GPU-surf-sync-busy", GPU_SURF_SYNC_BUSY
, UINT64
, AVERAGE
),
1908 X("GPU-dma-busy", GPU_DMA_BUSY
, UINT64
, AVERAGE
),
1909 X("GPU-scratch-ram-busy", GPU_SCRATCH_RAM_BUSY
, UINT64
, AVERAGE
),
1910 X("GPU-ce-busy", GPU_CE_BUSY
, UINT64
, AVERAGE
),
1917 static unsigned r600_get_num_queries(struct r600_common_screen
*rscreen
)
1919 if (rscreen
->info
.drm_major
== 2 && rscreen
->info
.drm_minor
>= 42)
1920 return ARRAY_SIZE(r600_driver_query_list
);
1921 else if (rscreen
->info
.drm_major
== 3) {
1922 if (rscreen
->chip_class
>= VI
)
1923 return ARRAY_SIZE(r600_driver_query_list
);
1925 return ARRAY_SIZE(r600_driver_query_list
) - 7;
1928 return ARRAY_SIZE(r600_driver_query_list
) - 25;
1931 static int r600_get_driver_query_info(struct pipe_screen
*screen
,
1933 struct pipe_driver_query_info
*info
)
1935 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1936 unsigned num_queries
= r600_get_num_queries(rscreen
);
1939 unsigned num_perfcounters
=
1940 r600_get_perfcounter_info(rscreen
, 0, NULL
);
1942 return num_queries
+ num_perfcounters
;
1945 if (index
>= num_queries
)
1946 return r600_get_perfcounter_info(rscreen
, index
- num_queries
, info
);
1948 *info
= r600_driver_query_list
[index
];
1950 switch (info
->query_type
) {
1951 case R600_QUERY_REQUESTED_VRAM
:
1952 case R600_QUERY_VRAM_USAGE
:
1953 case R600_QUERY_MAPPED_VRAM
:
1954 info
->max_value
.u64
= rscreen
->info
.vram_size
;
1956 case R600_QUERY_REQUESTED_GTT
:
1957 case R600_QUERY_GTT_USAGE
:
1958 case R600_QUERY_MAPPED_GTT
:
1959 info
->max_value
.u64
= rscreen
->info
.gart_size
;
1961 case R600_QUERY_GPU_TEMPERATURE
:
1962 info
->max_value
.u64
= 125;
1964 case R600_QUERY_VRAM_VIS_USAGE
:
1965 info
->max_value
.u64
= rscreen
->info
.vram_vis_size
;
1969 if (info
->group_id
!= ~(unsigned)0 && rscreen
->perfcounters
)
1970 info
->group_id
+= rscreen
->perfcounters
->num_groups
;
1975 /* Note: Unfortunately, GPUPerfStudio hardcodes the order of hardware
1976 * performance counter groups, so be careful when changing this and related
1979 static int r600_get_driver_query_group_info(struct pipe_screen
*screen
,
1981 struct pipe_driver_query_group_info
*info
)
1983 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1984 unsigned num_pc_groups
= 0;
1986 if (rscreen
->perfcounters
)
1987 num_pc_groups
= rscreen
->perfcounters
->num_groups
;
1990 return num_pc_groups
+ R600_NUM_SW_QUERY_GROUPS
;
1992 if (index
< num_pc_groups
)
1993 return r600_get_perfcounter_group_info(rscreen
, index
, info
);
1995 index
-= num_pc_groups
;
1996 if (index
>= R600_NUM_SW_QUERY_GROUPS
)
1999 info
->name
= "GPIN";
2000 info
->max_active_queries
= 5;
2001 info
->num_queries
= 5;
2005 void r600_query_init(struct r600_common_context
*rctx
)
2007 rctx
->b
.create_query
= r600_create_query
;
2008 rctx
->b
.create_batch_query
= r600_create_batch_query
;
2009 rctx
->b
.destroy_query
= r600_destroy_query
;
2010 rctx
->b
.begin_query
= r600_begin_query
;
2011 rctx
->b
.end_query
= r600_end_query
;
2012 rctx
->b
.get_query_result
= r600_get_query_result
;
2013 rctx
->b
.get_query_result_resource
= r600_get_query_result_resource
;
2014 rctx
->render_cond_atom
.emit
= r600_emit_query_predication
;
2016 if (((struct r600_common_screen
*)rctx
->b
.screen
)->info
.num_render_backends
> 0)
2017 rctx
->b
.render_condition
= r600_render_condition
;
2019 LIST_INITHEAD(&rctx
->active_queries
);
2022 void r600_init_screen_query_functions(struct r600_common_screen
*rscreen
)
2024 rscreen
->b
.get_driver_query_info
= r600_get_driver_query_info
;
2025 rscreen
->b
.get_driver_query_group_info
= r600_get_driver_query_group_info
;