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.
26 #include "util/u_memory.h"
29 struct r600_query_buffer
{
30 /* The buffer where query results are stored. */
31 struct r600_resource
*buf
;
32 /* Offset of the next free result after current query data */
34 /* If a query buffer is full, a new buffer is created and the old one
35 * is put in here. When we calculate the result, we sum up the samples
36 * from all buffers. */
37 struct r600_query_buffer
*previous
;
41 /* The query buffer and how many results are in it. */
42 struct r600_query_buffer buffer
;
43 /* The type of query */
45 /* Size of the result in memory for both begin_query and end_query,
46 * this can be one or two numbers, or it could even be a size of a structure. */
48 /* The number of dwords for begin_query or end_query. */
50 /* linked list of queries */
51 struct list_head list
;
52 /* for custom non-GPU queries */
53 uint64_t begin_result
;
55 /* Fence for GPU_FINISHED. */
56 struct pipe_fence_handle
*fence
;
57 /* For transform feedback: which stream the query is for */
62 static bool r600_is_timer_query(unsigned type
)
64 return type
== PIPE_QUERY_TIME_ELAPSED
||
65 type
== PIPE_QUERY_TIMESTAMP
;
68 static bool r600_query_needs_begin(unsigned type
)
70 return type
!= PIPE_QUERY_GPU_FINISHED
&&
71 type
!= PIPE_QUERY_TIMESTAMP
;
74 static struct r600_resource
*r600_new_query_buffer(struct r600_common_context
*ctx
, unsigned type
)
76 unsigned j
, i
, num_results
, buf_size
= 4096;
79 /* Non-GPU queries. */
81 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
82 case PIPE_QUERY_GPU_FINISHED
:
83 case R600_QUERY_DRAW_CALLS
:
84 case R600_QUERY_REQUESTED_VRAM
:
85 case R600_QUERY_REQUESTED_GTT
:
86 case R600_QUERY_BUFFER_WAIT_TIME
:
87 case R600_QUERY_NUM_CS_FLUSHES
:
88 case R600_QUERY_NUM_BYTES_MOVED
:
89 case R600_QUERY_VRAM_USAGE
:
90 case R600_QUERY_GTT_USAGE
:
91 case R600_QUERY_GPU_TEMPERATURE
:
92 case R600_QUERY_CURRENT_GPU_SCLK
:
93 case R600_QUERY_CURRENT_GPU_MCLK
:
94 case R600_QUERY_GPU_LOAD
:
98 /* Queries are normally read by the CPU after
99 * being written by the gpu, hence staging is probably a good
102 struct r600_resource
*buf
= (struct r600_resource
*)
103 pipe_buffer_create(ctx
->b
.screen
, PIPE_BIND_CUSTOM
,
104 PIPE_USAGE_STAGING
, buf_size
);
107 case PIPE_QUERY_OCCLUSION_COUNTER
:
108 case PIPE_QUERY_OCCLUSION_PREDICATE
:
109 results
= r600_buffer_map_sync_with_rings(ctx
, buf
, PIPE_TRANSFER_WRITE
);
110 memset(results
, 0, buf_size
);
112 /* Set top bits for unused backends. */
113 num_results
= buf_size
/ (16 * ctx
->max_db
);
114 for (j
= 0; j
< num_results
; j
++) {
115 for (i
= 0; i
< ctx
->max_db
; i
++) {
116 if (!(ctx
->backend_mask
& (1<<i
))) {
117 results
[(i
* 4)+1] = 0x80000000;
118 results
[(i
* 4)+3] = 0x80000000;
121 results
+= 4 * ctx
->max_db
;
124 case PIPE_QUERY_TIME_ELAPSED
:
125 case PIPE_QUERY_TIMESTAMP
:
127 case PIPE_QUERY_PRIMITIVES_EMITTED
:
128 case PIPE_QUERY_PRIMITIVES_GENERATED
:
129 case PIPE_QUERY_SO_STATISTICS
:
130 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
131 case PIPE_QUERY_PIPELINE_STATISTICS
:
132 results
= r600_buffer_map_sync_with_rings(ctx
, buf
, PIPE_TRANSFER_WRITE
);
133 memset(results
, 0, buf_size
);
141 static void r600_update_occlusion_query_state(struct r600_common_context
*rctx
,
142 unsigned type
, int diff
)
144 if (type
== PIPE_QUERY_OCCLUSION_COUNTER
||
145 type
== PIPE_QUERY_OCCLUSION_PREDICATE
) {
146 bool old_enable
= rctx
->num_occlusion_queries
!= 0;
149 rctx
->num_occlusion_queries
+= diff
;
150 assert(rctx
->num_occlusion_queries
>= 0);
152 enable
= rctx
->num_occlusion_queries
!= 0;
154 if (enable
!= old_enable
) {
155 rctx
->set_occlusion_query_state(&rctx
->b
, enable
);
160 static unsigned event_type_for_stream(struct r600_query
*query
)
162 switch (query
->stream
) {
164 case 0: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS
;
165 case 1: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS1
;
166 case 2: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS2
;
167 case 3: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS3
;
171 static void r600_emit_query_begin(struct r600_common_context
*ctx
, struct r600_query
*query
)
173 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
176 r600_update_occlusion_query_state(ctx
, query
->type
, 1);
177 r600_update_prims_generated_query_state(ctx
, query
->type
, 1);
178 ctx
->need_gfx_cs_space(&ctx
->b
, query
->num_cs_dw
* 2, TRUE
);
180 /* Get a new query buffer if needed. */
181 if (query
->buffer
.results_end
+ query
->result_size
> query
->buffer
.buf
->b
.b
.width0
) {
182 struct r600_query_buffer
*qbuf
= MALLOC_STRUCT(r600_query_buffer
);
183 *qbuf
= query
->buffer
;
184 query
->buffer
.buf
= r600_new_query_buffer(ctx
, query
->type
);
185 query
->buffer
.results_end
= 0;
186 query
->buffer
.previous
= qbuf
;
189 /* emit begin query */
190 va
= query
->buffer
.buf
->gpu_address
+ query
->buffer
.results_end
;
192 switch (query
->type
) {
193 case PIPE_QUERY_OCCLUSION_COUNTER
:
194 case PIPE_QUERY_OCCLUSION_PREDICATE
:
195 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
196 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1));
198 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
200 case PIPE_QUERY_PRIMITIVES_EMITTED
:
201 case PIPE_QUERY_PRIMITIVES_GENERATED
:
202 case PIPE_QUERY_SO_STATISTICS
:
203 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
204 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
205 radeon_emit(cs
, EVENT_TYPE(event_type_for_stream(query
)) | EVENT_INDEX(3));
207 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
209 case PIPE_QUERY_TIME_ELAPSED
:
210 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE_EOP
, 4, 0));
211 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_CACHE_FLUSH_AND_INV_TS_EVENT
) | EVENT_INDEX(5));
213 radeon_emit(cs
, (3 << 29) | ((va
>> 32UL) & 0xFF));
217 case PIPE_QUERY_PIPELINE_STATISTICS
:
218 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
219 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT
) | EVENT_INDEX(2));
221 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
226 r600_emit_reloc(ctx
, &ctx
->rings
.gfx
, query
->buffer
.buf
, RADEON_USAGE_WRITE
,
229 if (!r600_is_timer_query(query
->type
)) {
230 ctx
->num_cs_dw_nontimer_queries_suspend
+= query
->num_cs_dw
;
234 static void r600_emit_query_end(struct r600_common_context
*ctx
, struct r600_query
*query
)
236 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
239 /* The queries which need begin already called this in begin_query. */
240 if (!r600_query_needs_begin(query
->type
)) {
241 ctx
->need_gfx_cs_space(&ctx
->b
, query
->num_cs_dw
, FALSE
);
244 va
= query
->buffer
.buf
->gpu_address
;
247 switch (query
->type
) {
248 case PIPE_QUERY_OCCLUSION_COUNTER
:
249 case PIPE_QUERY_OCCLUSION_PREDICATE
:
250 va
+= query
->buffer
.results_end
+ 8;
251 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
252 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1));
254 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
256 case PIPE_QUERY_PRIMITIVES_EMITTED
:
257 case PIPE_QUERY_PRIMITIVES_GENERATED
:
258 case PIPE_QUERY_SO_STATISTICS
:
259 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
260 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
261 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
262 radeon_emit(cs
, EVENT_TYPE(event_type_for_stream(query
)) | EVENT_INDEX(3));
264 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
266 case PIPE_QUERY_TIME_ELAPSED
:
267 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
269 case PIPE_QUERY_TIMESTAMP
:
270 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE_EOP
, 4, 0));
271 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_CACHE_FLUSH_AND_INV_TS_EVENT
) | EVENT_INDEX(5));
273 radeon_emit(cs
, (3 << 29) | ((va
>> 32UL) & 0xFF));
277 case PIPE_QUERY_PIPELINE_STATISTICS
:
278 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
279 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
280 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT
) | EVENT_INDEX(2));
282 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
287 r600_emit_reloc(ctx
, &ctx
->rings
.gfx
, query
->buffer
.buf
, RADEON_USAGE_WRITE
,
290 query
->buffer
.results_end
+= query
->result_size
;
292 if (r600_query_needs_begin(query
->type
)) {
293 if (!r600_is_timer_query(query
->type
)) {
294 ctx
->num_cs_dw_nontimer_queries_suspend
-= query
->num_cs_dw
;
298 r600_update_occlusion_query_state(ctx
, query
->type
, -1);
299 r600_update_prims_generated_query_state(ctx
, query
->type
, -1);
302 static void r600_emit_query_predication(struct r600_common_context
*ctx
, struct r600_query
*query
,
303 int operation
, bool flag_wait
)
305 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
306 uint32_t op
= PRED_OP(operation
);
308 /* if true then invert, see GL_ARB_conditional_render_inverted */
309 if (ctx
->current_render_cond_cond
)
310 op
|= PREDICATION_DRAW_NOT_VISIBLE
; /* Draw if not visable/overflow */
312 op
|= PREDICATION_DRAW_VISIBLE
; /* Draw if visable/overflow */
314 if (operation
== PREDICATION_OP_CLEAR
) {
315 ctx
->need_gfx_cs_space(&ctx
->b
, 3, FALSE
);
317 radeon_emit(cs
, PKT3(PKT3_SET_PREDICATION
, 1, 0));
319 radeon_emit(cs
, PRED_OP(PREDICATION_OP_CLEAR
));
321 struct r600_query_buffer
*qbuf
;
323 /* Find how many results there are. */
325 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
326 count
+= qbuf
->results_end
/ query
->result_size
;
329 ctx
->need_gfx_cs_space(&ctx
->b
, 5 * count
, TRUE
);
331 op
|= flag_wait
? PREDICATION_HINT_WAIT
: PREDICATION_HINT_NOWAIT_DRAW
;
333 /* emit predicate packets for all data blocks */
334 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
335 unsigned results_base
= 0;
336 uint64_t va
= qbuf
->buf
->gpu_address
;
338 while (results_base
< qbuf
->results_end
) {
339 radeon_emit(cs
, PKT3(PKT3_SET_PREDICATION
, 1, 0));
340 radeon_emit(cs
, (va
+ results_base
) & 0xFFFFFFFFUL
);
341 radeon_emit(cs
, op
| (((va
+ results_base
) >> 32UL) & 0xFF));
342 r600_emit_reloc(ctx
, &ctx
->rings
.gfx
, qbuf
->buf
, RADEON_USAGE_READ
,
344 results_base
+= query
->result_size
;
346 /* set CONTINUE bit for all packets except the first */
347 op
|= PREDICATION_CONTINUE
;
353 static struct pipe_query
*r600_create_query(struct pipe_context
*ctx
, unsigned query_type
, unsigned index
)
355 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
356 struct r600_query
*query
;
357 bool skip_allocation
= false;
359 query
= CALLOC_STRUCT(r600_query
);
363 query
->type
= query_type
;
365 switch (query_type
) {
366 case PIPE_QUERY_OCCLUSION_COUNTER
:
367 case PIPE_QUERY_OCCLUSION_PREDICATE
:
368 query
->result_size
= 16 * rctx
->max_db
;
369 query
->num_cs_dw
= 6;
372 case PIPE_QUERY_TIME_ELAPSED
:
373 query
->result_size
= 16;
374 query
->num_cs_dw
= 8;
376 case PIPE_QUERY_TIMESTAMP
:
377 query
->result_size
= 8;
378 query
->num_cs_dw
= 8;
380 case PIPE_QUERY_PRIMITIVES_EMITTED
:
381 case PIPE_QUERY_PRIMITIVES_GENERATED
:
382 case PIPE_QUERY_SO_STATISTICS
:
383 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
384 /* NumPrimitivesWritten, PrimitiveStorageNeeded. */
385 query
->result_size
= 32;
386 query
->num_cs_dw
= 6;
387 query
->stream
= index
;
389 case PIPE_QUERY_PIPELINE_STATISTICS
:
390 /* 11 values on EG, 8 on R600. */
391 query
->result_size
= (rctx
->chip_class
>= EVERGREEN
? 11 : 8) * 16;
392 query
->num_cs_dw
= 6;
394 /* Non-GPU queries and queries not requiring a buffer. */
395 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
396 case PIPE_QUERY_GPU_FINISHED
:
397 case R600_QUERY_DRAW_CALLS
:
398 case R600_QUERY_REQUESTED_VRAM
:
399 case R600_QUERY_REQUESTED_GTT
:
400 case R600_QUERY_BUFFER_WAIT_TIME
:
401 case R600_QUERY_NUM_CS_FLUSHES
:
402 case R600_QUERY_NUM_BYTES_MOVED
:
403 case R600_QUERY_VRAM_USAGE
:
404 case R600_QUERY_GTT_USAGE
:
405 case R600_QUERY_GPU_TEMPERATURE
:
406 case R600_QUERY_CURRENT_GPU_SCLK
:
407 case R600_QUERY_CURRENT_GPU_MCLK
:
408 case R600_QUERY_GPU_LOAD
:
409 skip_allocation
= true;
417 if (!skip_allocation
) {
418 query
->buffer
.buf
= r600_new_query_buffer(rctx
, query_type
);
419 if (!query
->buffer
.buf
) {
424 return (struct pipe_query
*)query
;
427 static void r600_destroy_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
429 struct r600_query
*rquery
= (struct r600_query
*)query
;
430 struct r600_query_buffer
*prev
= rquery
->buffer
.previous
;
432 /* Release all query buffers. */
434 struct r600_query_buffer
*qbuf
= prev
;
435 prev
= prev
->previous
;
436 pipe_resource_reference((struct pipe_resource
**)&qbuf
->buf
, NULL
);
440 pipe_resource_reference((struct pipe_resource
**)&rquery
->buffer
.buf
, NULL
);
444 static boolean
r600_begin_query(struct pipe_context
*ctx
,
445 struct pipe_query
*query
)
447 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
448 struct r600_query
*rquery
= (struct r600_query
*)query
;
449 struct r600_query_buffer
*prev
= rquery
->buffer
.previous
;
451 if (!r600_query_needs_begin(rquery
->type
)) {
456 /* Non-GPU queries. */
457 switch (rquery
->type
) {
458 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
460 case R600_QUERY_DRAW_CALLS
:
461 rquery
->begin_result
= rctx
->num_draw_calls
;
463 case R600_QUERY_REQUESTED_VRAM
:
464 case R600_QUERY_REQUESTED_GTT
:
465 case R600_QUERY_VRAM_USAGE
:
466 case R600_QUERY_GTT_USAGE
:
467 case R600_QUERY_GPU_TEMPERATURE
:
468 case R600_QUERY_CURRENT_GPU_SCLK
:
469 case R600_QUERY_CURRENT_GPU_MCLK
:
470 rquery
->begin_result
= 0;
472 case R600_QUERY_BUFFER_WAIT_TIME
:
473 rquery
->begin_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_BUFFER_WAIT_TIME_NS
);
475 case R600_QUERY_NUM_CS_FLUSHES
:
476 rquery
->begin_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_NUM_CS_FLUSHES
);
478 case R600_QUERY_NUM_BYTES_MOVED
:
479 rquery
->begin_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_NUM_BYTES_MOVED
);
481 case R600_QUERY_GPU_LOAD
:
482 rquery
->begin_result
= r600_gpu_load_begin(rctx
->screen
);
486 /* Discard the old query buffers. */
488 struct r600_query_buffer
*qbuf
= prev
;
489 prev
= prev
->previous
;
490 pipe_resource_reference((struct pipe_resource
**)&qbuf
->buf
, NULL
);
494 /* Obtain a new buffer if the current one can't be mapped without a stall. */
495 if (r600_rings_is_buffer_referenced(rctx
, rquery
->buffer
.buf
->cs_buf
, RADEON_USAGE_READWRITE
) ||
496 rctx
->ws
->buffer_is_busy(rquery
->buffer
.buf
->buf
, RADEON_USAGE_READWRITE
)) {
497 pipe_resource_reference((struct pipe_resource
**)&rquery
->buffer
.buf
, NULL
);
498 rquery
->buffer
.buf
= r600_new_query_buffer(rctx
, rquery
->type
);
501 rquery
->buffer
.results_end
= 0;
502 rquery
->buffer
.previous
= NULL
;
504 r600_emit_query_begin(rctx
, rquery
);
506 if (!r600_is_timer_query(rquery
->type
)) {
507 LIST_ADDTAIL(&rquery
->list
, &rctx
->active_nontimer_queries
);
512 static void r600_end_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
514 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
515 struct r600_query
*rquery
= (struct r600_query
*)query
;
517 /* Non-GPU queries. */
518 switch (rquery
->type
) {
519 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
521 case PIPE_QUERY_GPU_FINISHED
:
522 rctx
->rings
.gfx
.flush(rctx
, RADEON_FLUSH_ASYNC
, &rquery
->fence
);
524 case R600_QUERY_DRAW_CALLS
:
525 rquery
->end_result
= rctx
->num_draw_calls
;
527 case R600_QUERY_REQUESTED_VRAM
:
528 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_REQUESTED_VRAM_MEMORY
);
530 case R600_QUERY_REQUESTED_GTT
:
531 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_REQUESTED_GTT_MEMORY
);
533 case R600_QUERY_BUFFER_WAIT_TIME
:
534 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_BUFFER_WAIT_TIME_NS
);
536 case R600_QUERY_NUM_CS_FLUSHES
:
537 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_NUM_CS_FLUSHES
);
539 case R600_QUERY_NUM_BYTES_MOVED
:
540 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_NUM_BYTES_MOVED
);
542 case R600_QUERY_VRAM_USAGE
:
543 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_VRAM_USAGE
);
545 case R600_QUERY_GTT_USAGE
:
546 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_GTT_USAGE
);
548 case R600_QUERY_GPU_TEMPERATURE
:
549 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_GPU_TEMPERATURE
) / 1000;
551 case R600_QUERY_CURRENT_GPU_SCLK
:
552 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_CURRENT_SCLK
) * 1000000;
554 case R600_QUERY_CURRENT_GPU_MCLK
:
555 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_CURRENT_MCLK
) * 1000000;
557 case R600_QUERY_GPU_LOAD
:
558 rquery
->end_result
= r600_gpu_load_end(rctx
->screen
, rquery
->begin_result
);
562 r600_emit_query_end(rctx
, rquery
);
564 if (r600_query_needs_begin(rquery
->type
) && !r600_is_timer_query(rquery
->type
)) {
565 LIST_DELINIT(&rquery
->list
);
569 static unsigned r600_query_read_result(char *map
, unsigned start_index
, unsigned end_index
,
570 bool test_status_bit
)
572 uint32_t *current_result
= (uint32_t*)map
;
575 start
= (uint64_t)current_result
[start_index
] |
576 (uint64_t)current_result
[start_index
+1] << 32;
577 end
= (uint64_t)current_result
[end_index
] |
578 (uint64_t)current_result
[end_index
+1] << 32;
580 if (!test_status_bit
||
581 ((start
& 0x8000000000000000UL
) && (end
& 0x8000000000000000UL
))) {
587 static boolean
r600_get_query_buffer_result(struct r600_common_context
*ctx
,
588 struct r600_query
*query
,
589 struct r600_query_buffer
*qbuf
,
591 union pipe_query_result
*result
)
593 struct pipe_screen
*screen
= ctx
->b
.screen
;
594 unsigned results_base
= 0;
597 /* Non-GPU queries. */
598 switch (query
->type
) {
599 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
600 /* Convert from cycles per millisecond to cycles per second (Hz). */
601 result
->timestamp_disjoint
.frequency
=
602 (uint64_t)ctx
->screen
->info
.r600_clock_crystal_freq
* 1000;
603 result
->timestamp_disjoint
.disjoint
= FALSE
;
605 case PIPE_QUERY_GPU_FINISHED
:
606 result
->b
= screen
->fence_finish(screen
, query
->fence
,
607 wait
? PIPE_TIMEOUT_INFINITE
: 0);
609 case R600_QUERY_DRAW_CALLS
:
610 case R600_QUERY_REQUESTED_VRAM
:
611 case R600_QUERY_REQUESTED_GTT
:
612 case R600_QUERY_BUFFER_WAIT_TIME
:
613 case R600_QUERY_NUM_CS_FLUSHES
:
614 case R600_QUERY_NUM_BYTES_MOVED
:
615 case R600_QUERY_VRAM_USAGE
:
616 case R600_QUERY_GTT_USAGE
:
617 case R600_QUERY_GPU_TEMPERATURE
:
618 case R600_QUERY_CURRENT_GPU_SCLK
:
619 case R600_QUERY_CURRENT_GPU_MCLK
:
620 result
->u64
= query
->end_result
- query
->begin_result
;
622 case R600_QUERY_GPU_LOAD
:
623 result
->u64
= query
->end_result
;
627 map
= r600_buffer_map_sync_with_rings(ctx
, qbuf
->buf
,
629 (wait
? 0 : PIPE_TRANSFER_DONTBLOCK
));
633 /* count all results across all data blocks */
634 switch (query
->type
) {
635 case PIPE_QUERY_OCCLUSION_COUNTER
:
636 while (results_base
!= qbuf
->results_end
) {
638 r600_query_read_result(map
+ results_base
, 0, 2, true);
642 case PIPE_QUERY_OCCLUSION_PREDICATE
:
643 while (results_base
!= qbuf
->results_end
) {
644 result
->b
= result
->b
||
645 r600_query_read_result(map
+ results_base
, 0, 2, true) != 0;
649 case PIPE_QUERY_TIME_ELAPSED
:
650 while (results_base
!= qbuf
->results_end
) {
652 r600_query_read_result(map
+ results_base
, 0, 2, false);
653 results_base
+= query
->result_size
;
656 case PIPE_QUERY_TIMESTAMP
:
658 uint32_t *current_result
= (uint32_t*)map
;
659 result
->u64
= (uint64_t)current_result
[0] |
660 (uint64_t)current_result
[1] << 32;
663 case PIPE_QUERY_PRIMITIVES_EMITTED
:
664 /* SAMPLE_STREAMOUTSTATS stores this structure:
666 * u64 NumPrimitivesWritten;
667 * u64 PrimitiveStorageNeeded;
669 * We only need NumPrimitivesWritten here. */
670 while (results_base
!= qbuf
->results_end
) {
672 r600_query_read_result(map
+ results_base
, 2, 6, true);
673 results_base
+= query
->result_size
;
676 case PIPE_QUERY_PRIMITIVES_GENERATED
:
677 /* Here we read PrimitiveStorageNeeded. */
678 while (results_base
!= qbuf
->results_end
) {
680 r600_query_read_result(map
+ results_base
, 0, 4, true);
681 results_base
+= query
->result_size
;
684 case PIPE_QUERY_SO_STATISTICS
:
685 while (results_base
!= qbuf
->results_end
) {
686 result
->so_statistics
.num_primitives_written
+=
687 r600_query_read_result(map
+ results_base
, 2, 6, true);
688 result
->so_statistics
.primitives_storage_needed
+=
689 r600_query_read_result(map
+ results_base
, 0, 4, true);
690 results_base
+= query
->result_size
;
693 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
694 while (results_base
!= qbuf
->results_end
) {
695 result
->b
= result
->b
||
696 r600_query_read_result(map
+ results_base
, 2, 6, true) !=
697 r600_query_read_result(map
+ results_base
, 0, 4, true);
698 results_base
+= query
->result_size
;
701 case PIPE_QUERY_PIPELINE_STATISTICS
:
702 if (ctx
->chip_class
>= EVERGREEN
) {
703 while (results_base
!= qbuf
->results_end
) {
704 result
->pipeline_statistics
.ps_invocations
+=
705 r600_query_read_result(map
+ results_base
, 0, 22, false);
706 result
->pipeline_statistics
.c_primitives
+=
707 r600_query_read_result(map
+ results_base
, 2, 24, false);
708 result
->pipeline_statistics
.c_invocations
+=
709 r600_query_read_result(map
+ results_base
, 4, 26, false);
710 result
->pipeline_statistics
.vs_invocations
+=
711 r600_query_read_result(map
+ results_base
, 6, 28, false);
712 result
->pipeline_statistics
.gs_invocations
+=
713 r600_query_read_result(map
+ results_base
, 8, 30, false);
714 result
->pipeline_statistics
.gs_primitives
+=
715 r600_query_read_result(map
+ results_base
, 10, 32, false);
716 result
->pipeline_statistics
.ia_primitives
+=
717 r600_query_read_result(map
+ results_base
, 12, 34, false);
718 result
->pipeline_statistics
.ia_vertices
+=
719 r600_query_read_result(map
+ results_base
, 14, 36, false);
720 result
->pipeline_statistics
.hs_invocations
+=
721 r600_query_read_result(map
+ results_base
, 16, 38, false);
722 result
->pipeline_statistics
.ds_invocations
+=
723 r600_query_read_result(map
+ results_base
, 18, 40, false);
724 result
->pipeline_statistics
.cs_invocations
+=
725 r600_query_read_result(map
+ results_base
, 20, 42, false);
726 results_base
+= query
->result_size
;
729 while (results_base
!= qbuf
->results_end
) {
730 result
->pipeline_statistics
.ps_invocations
+=
731 r600_query_read_result(map
+ results_base
, 0, 16, false);
732 result
->pipeline_statistics
.c_primitives
+=
733 r600_query_read_result(map
+ results_base
, 2, 18, false);
734 result
->pipeline_statistics
.c_invocations
+=
735 r600_query_read_result(map
+ results_base
, 4, 20, false);
736 result
->pipeline_statistics
.vs_invocations
+=
737 r600_query_read_result(map
+ results_base
, 6, 22, false);
738 result
->pipeline_statistics
.gs_invocations
+=
739 r600_query_read_result(map
+ results_base
, 8, 24, false);
740 result
->pipeline_statistics
.gs_primitives
+=
741 r600_query_read_result(map
+ results_base
, 10, 26, false);
742 result
->pipeline_statistics
.ia_primitives
+=
743 r600_query_read_result(map
+ results_base
, 12, 28, false);
744 result
->pipeline_statistics
.ia_vertices
+=
745 r600_query_read_result(map
+ results_base
, 14, 30, false);
746 results_base
+= query
->result_size
;
749 #if 0 /* for testing */
750 printf("Pipeline stats: IA verts=%llu, IA prims=%llu, VS=%llu, HS=%llu, "
751 "DS=%llu, GS=%llu, GS prims=%llu, Clipper=%llu, "
752 "Clipper prims=%llu, PS=%llu, CS=%llu\n",
753 result
->pipeline_statistics
.ia_vertices
,
754 result
->pipeline_statistics
.ia_primitives
,
755 result
->pipeline_statistics
.vs_invocations
,
756 result
->pipeline_statistics
.hs_invocations
,
757 result
->pipeline_statistics
.ds_invocations
,
758 result
->pipeline_statistics
.gs_invocations
,
759 result
->pipeline_statistics
.gs_primitives
,
760 result
->pipeline_statistics
.c_invocations
,
761 result
->pipeline_statistics
.c_primitives
,
762 result
->pipeline_statistics
.ps_invocations
,
763 result
->pipeline_statistics
.cs_invocations
);
773 static boolean
r600_get_query_result(struct pipe_context
*ctx
,
774 struct pipe_query
*query
,
775 boolean wait
, union pipe_query_result
*result
)
777 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
778 struct r600_query
*rquery
= (struct r600_query
*)query
;
779 struct r600_query_buffer
*qbuf
;
781 util_query_clear_result(result
, rquery
->type
);
783 for (qbuf
= &rquery
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
784 if (!r600_get_query_buffer_result(rctx
, rquery
, qbuf
, wait
, result
)) {
789 /* Convert the time to expected units. */
790 if (rquery
->type
== PIPE_QUERY_TIME_ELAPSED
||
791 rquery
->type
== PIPE_QUERY_TIMESTAMP
) {
792 result
->u64
= (1000000 * result
->u64
) / rctx
->screen
->info
.r600_clock_crystal_freq
;
797 static void r600_render_condition(struct pipe_context
*ctx
,
798 struct pipe_query
*query
,
802 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
803 struct r600_query
*rquery
= (struct r600_query
*)query
;
804 bool wait_flag
= false;
806 rctx
->current_render_cond
= query
;
807 rctx
->current_render_cond_cond
= condition
;
808 rctx
->current_render_cond_mode
= mode
;
811 if (rctx
->predicate_drawing
) {
812 rctx
->predicate_drawing
= false;
813 r600_emit_query_predication(rctx
, NULL
, PREDICATION_OP_CLEAR
, false);
818 if (mode
== PIPE_RENDER_COND_WAIT
||
819 mode
== PIPE_RENDER_COND_BY_REGION_WAIT
) {
823 rctx
->predicate_drawing
= true;
825 switch (rquery
->type
) {
826 case PIPE_QUERY_OCCLUSION_COUNTER
:
827 case PIPE_QUERY_OCCLUSION_PREDICATE
:
828 r600_emit_query_predication(rctx
, rquery
, PREDICATION_OP_ZPASS
, wait_flag
);
830 case PIPE_QUERY_PRIMITIVES_EMITTED
:
831 case PIPE_QUERY_PRIMITIVES_GENERATED
:
832 case PIPE_QUERY_SO_STATISTICS
:
833 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
834 r600_emit_query_predication(rctx
, rquery
, PREDICATION_OP_PRIMCOUNT
, wait_flag
);
841 void r600_suspend_nontimer_queries(struct r600_common_context
*ctx
)
843 struct r600_query
*query
;
845 LIST_FOR_EACH_ENTRY(query
, &ctx
->active_nontimer_queries
, list
) {
846 r600_emit_query_end(ctx
, query
);
848 assert(ctx
->num_cs_dw_nontimer_queries_suspend
== 0);
851 static unsigned r600_queries_num_cs_dw_for_resuming(struct r600_common_context
*ctx
)
853 struct r600_query
*query
;
856 LIST_FOR_EACH_ENTRY(query
, &ctx
->active_nontimer_queries
, list
) {
858 num_dw
+= query
->num_cs_dw
* 2;
860 /* Workaround for the fact that
861 * num_cs_dw_nontimer_queries_suspend is incremented for every
862 * resumed query, which raises the bar in need_cs_space for
863 * queries about to be resumed.
865 num_dw
+= query
->num_cs_dw
;
867 /* primitives generated query */
868 num_dw
+= ctx
->streamout
.enable_atom
.num_dw
;
869 /* guess for ZPASS enable or PERFECT_ZPASS_COUNT enable updates */
875 void r600_resume_nontimer_queries(struct r600_common_context
*ctx
)
877 struct r600_query
*query
;
879 assert(ctx
->num_cs_dw_nontimer_queries_suspend
== 0);
881 /* Check CS space here. Resuming must not be interrupted by flushes. */
882 ctx
->need_gfx_cs_space(&ctx
->b
,
883 r600_queries_num_cs_dw_for_resuming(ctx
), TRUE
);
885 LIST_FOR_EACH_ENTRY(query
, &ctx
->active_nontimer_queries
, list
) {
886 r600_emit_query_begin(ctx
, query
);
890 /* Get backends mask */
891 void r600_query_init_backend_mask(struct r600_common_context
*ctx
)
893 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
894 struct r600_resource
*buffer
;
896 unsigned num_backends
= ctx
->screen
->info
.r600_num_backends
;
897 unsigned i
, mask
= 0;
899 /* if backend_map query is supported by the kernel */
900 if (ctx
->screen
->info
.r600_backend_map_valid
) {
901 unsigned num_tile_pipes
= ctx
->screen
->info
.r600_num_tile_pipes
;
902 unsigned backend_map
= ctx
->screen
->info
.r600_backend_map
;
903 unsigned item_width
, item_mask
;
905 if (ctx
->chip_class
>= EVERGREEN
) {
913 while(num_tile_pipes
--) {
914 i
= backend_map
& item_mask
;
916 backend_map
>>= item_width
;
919 ctx
->backend_mask
= mask
;
924 /* otherwise backup path for older kernels */
926 /* create buffer for event data */
927 buffer
= (struct r600_resource
*)
928 pipe_buffer_create(ctx
->b
.screen
, PIPE_BIND_CUSTOM
,
929 PIPE_USAGE_STAGING
, ctx
->max_db
*16);
933 /* initialize buffer with zeroes */
934 results
= r600_buffer_map_sync_with_rings(ctx
, buffer
, PIPE_TRANSFER_WRITE
);
936 memset(results
, 0, ctx
->max_db
* 4 * 4);
938 /* emit EVENT_WRITE for ZPASS_DONE */
939 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
940 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1));
941 radeon_emit(cs
, buffer
->gpu_address
);
942 radeon_emit(cs
, buffer
->gpu_address
>> 32);
944 r600_emit_reloc(ctx
, &ctx
->rings
.gfx
, buffer
, RADEON_USAGE_WRITE
, RADEON_PRIO_MIN
);
946 /* analyze results */
947 results
= r600_buffer_map_sync_with_rings(ctx
, buffer
, PIPE_TRANSFER_READ
);
949 for(i
= 0; i
< ctx
->max_db
; i
++) {
950 /* at least highest bit will be set if backend is used */
951 if (results
[i
*4 + 1])
957 pipe_resource_reference((struct pipe_resource
**)&buffer
, NULL
);
960 ctx
->backend_mask
= mask
;
965 /* fallback to old method - set num_backends lower bits to 1 */
966 ctx
->backend_mask
= (~((uint32_t)0))>>(32-num_backends
);
970 void r600_query_init(struct r600_common_context
*rctx
)
972 rctx
->b
.create_query
= r600_create_query
;
973 rctx
->b
.destroy_query
= r600_destroy_query
;
974 rctx
->b
.begin_query
= r600_begin_query
;
975 rctx
->b
.end_query
= r600_end_query
;
976 rctx
->b
.get_query_result
= r600_get_query_result
;
978 if (((struct r600_common_screen
*)rctx
->b
.screen
)->info
.r600_num_backends
> 0)
979 rctx
->b
.render_condition
= r600_render_condition
;
981 LIST_INITHEAD(&rctx
->active_nontimer_queries
);