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_timer_queries_suspend
+= query
->num_cs_dw
;
232 ctx
->num_cs_dw_nontimer_queries_suspend
+= query
->num_cs_dw
;
235 static void r600_emit_query_end(struct r600_common_context
*ctx
, struct r600_query
*query
)
237 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
240 /* The queries which need begin already called this in begin_query. */
241 if (!r600_query_needs_begin(query
->type
)) {
242 ctx
->need_gfx_cs_space(&ctx
->b
, query
->num_cs_dw
, FALSE
);
245 va
= query
->buffer
.buf
->gpu_address
;
248 switch (query
->type
) {
249 case PIPE_QUERY_OCCLUSION_COUNTER
:
250 case PIPE_QUERY_OCCLUSION_PREDICATE
:
251 va
+= query
->buffer
.results_end
+ 8;
252 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
253 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1));
255 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
257 case PIPE_QUERY_PRIMITIVES_EMITTED
:
258 case PIPE_QUERY_PRIMITIVES_GENERATED
:
259 case PIPE_QUERY_SO_STATISTICS
:
260 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
261 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
262 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
263 radeon_emit(cs
, EVENT_TYPE(event_type_for_stream(query
)) | EVENT_INDEX(3));
265 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
267 case PIPE_QUERY_TIME_ELAPSED
:
268 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
270 case PIPE_QUERY_TIMESTAMP
:
271 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE_EOP
, 4, 0));
272 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_CACHE_FLUSH_AND_INV_TS_EVENT
) | EVENT_INDEX(5));
274 radeon_emit(cs
, (3 << 29) | ((va
>> 32UL) & 0xFF));
278 case PIPE_QUERY_PIPELINE_STATISTICS
:
279 va
+= query
->buffer
.results_end
+ query
->result_size
/2;
280 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
281 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT
) | EVENT_INDEX(2));
283 radeon_emit(cs
, (va
>> 32UL) & 0xFF);
288 r600_emit_reloc(ctx
, &ctx
->rings
.gfx
, query
->buffer
.buf
, RADEON_USAGE_WRITE
,
291 query
->buffer
.results_end
+= query
->result_size
;
293 if (r600_query_needs_begin(query
->type
)) {
294 if (r600_is_timer_query(query
->type
))
295 ctx
->num_cs_dw_timer_queries_suspend
-= query
->num_cs_dw
;
297 ctx
->num_cs_dw_nontimer_queries_suspend
-= query
->num_cs_dw
;
300 r600_update_occlusion_query_state(ctx
, query
->type
, -1);
301 r600_update_prims_generated_query_state(ctx
, query
->type
, -1);
304 static void r600_emit_query_predication(struct r600_common_context
*ctx
, struct r600_query
*query
,
305 int operation
, bool flag_wait
)
307 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
308 uint32_t op
= PRED_OP(operation
);
310 /* if true then invert, see GL_ARB_conditional_render_inverted */
311 if (ctx
->current_render_cond_cond
)
312 op
|= PREDICATION_DRAW_NOT_VISIBLE
; /* Draw if not visable/overflow */
314 op
|= PREDICATION_DRAW_VISIBLE
; /* Draw if visable/overflow */
316 if (operation
== PREDICATION_OP_CLEAR
) {
317 ctx
->need_gfx_cs_space(&ctx
->b
, 3, FALSE
);
319 radeon_emit(cs
, PKT3(PKT3_SET_PREDICATION
, 1, 0));
321 radeon_emit(cs
, PRED_OP(PREDICATION_OP_CLEAR
));
323 struct r600_query_buffer
*qbuf
;
325 /* Find how many results there are. */
327 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
328 count
+= qbuf
->results_end
/ query
->result_size
;
331 ctx
->need_gfx_cs_space(&ctx
->b
, 5 * count
, TRUE
);
333 op
|= flag_wait
? PREDICATION_HINT_WAIT
: PREDICATION_HINT_NOWAIT_DRAW
;
335 /* emit predicate packets for all data blocks */
336 for (qbuf
= &query
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
337 unsigned results_base
= 0;
338 uint64_t va
= qbuf
->buf
->gpu_address
;
340 while (results_base
< qbuf
->results_end
) {
341 radeon_emit(cs
, PKT3(PKT3_SET_PREDICATION
, 1, 0));
342 radeon_emit(cs
, (va
+ results_base
) & 0xFFFFFFFFUL
);
343 radeon_emit(cs
, op
| (((va
+ results_base
) >> 32UL) & 0xFF));
344 r600_emit_reloc(ctx
, &ctx
->rings
.gfx
, qbuf
->buf
, RADEON_USAGE_READ
,
346 results_base
+= query
->result_size
;
348 /* set CONTINUE bit for all packets except the first */
349 op
|= PREDICATION_CONTINUE
;
355 static struct pipe_query
*r600_create_query(struct pipe_context
*ctx
, unsigned query_type
, unsigned index
)
357 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
358 struct r600_query
*query
;
359 bool skip_allocation
= false;
361 query
= CALLOC_STRUCT(r600_query
);
365 query
->type
= query_type
;
367 switch (query_type
) {
368 case PIPE_QUERY_OCCLUSION_COUNTER
:
369 case PIPE_QUERY_OCCLUSION_PREDICATE
:
370 query
->result_size
= 16 * rctx
->max_db
;
371 query
->num_cs_dw
= 6;
374 case PIPE_QUERY_TIME_ELAPSED
:
375 query
->result_size
= 16;
376 query
->num_cs_dw
= 8;
378 case PIPE_QUERY_TIMESTAMP
:
379 query
->result_size
= 8;
380 query
->num_cs_dw
= 8;
382 case PIPE_QUERY_PRIMITIVES_EMITTED
:
383 case PIPE_QUERY_PRIMITIVES_GENERATED
:
384 case PIPE_QUERY_SO_STATISTICS
:
385 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
386 /* NumPrimitivesWritten, PrimitiveStorageNeeded. */
387 query
->result_size
= 32;
388 query
->num_cs_dw
= 6;
389 query
->stream
= index
;
391 case PIPE_QUERY_PIPELINE_STATISTICS
:
392 /* 11 values on EG, 8 on R600. */
393 query
->result_size
= (rctx
->chip_class
>= EVERGREEN
? 11 : 8) * 16;
394 query
->num_cs_dw
= 6;
396 /* Non-GPU queries and queries not requiring a buffer. */
397 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
398 case PIPE_QUERY_GPU_FINISHED
:
399 case R600_QUERY_DRAW_CALLS
:
400 case R600_QUERY_REQUESTED_VRAM
:
401 case R600_QUERY_REQUESTED_GTT
:
402 case R600_QUERY_BUFFER_WAIT_TIME
:
403 case R600_QUERY_NUM_CS_FLUSHES
:
404 case R600_QUERY_NUM_BYTES_MOVED
:
405 case R600_QUERY_VRAM_USAGE
:
406 case R600_QUERY_GTT_USAGE
:
407 case R600_QUERY_GPU_TEMPERATURE
:
408 case R600_QUERY_CURRENT_GPU_SCLK
:
409 case R600_QUERY_CURRENT_GPU_MCLK
:
410 case R600_QUERY_GPU_LOAD
:
411 skip_allocation
= true;
419 if (!skip_allocation
) {
420 query
->buffer
.buf
= r600_new_query_buffer(rctx
, query_type
);
421 if (!query
->buffer
.buf
) {
426 return (struct pipe_query
*)query
;
429 static void r600_destroy_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
431 struct r600_query
*rquery
= (struct r600_query
*)query
;
432 struct r600_query_buffer
*prev
= rquery
->buffer
.previous
;
434 /* Release all query buffers. */
436 struct r600_query_buffer
*qbuf
= prev
;
437 prev
= prev
->previous
;
438 pipe_resource_reference((struct pipe_resource
**)&qbuf
->buf
, NULL
);
442 pipe_resource_reference((struct pipe_resource
**)&rquery
->buffer
.buf
, NULL
);
446 static boolean
r600_begin_query(struct pipe_context
*ctx
,
447 struct pipe_query
*query
)
449 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
450 struct r600_query
*rquery
= (struct r600_query
*)query
;
451 struct r600_query_buffer
*prev
= rquery
->buffer
.previous
;
453 if (!r600_query_needs_begin(rquery
->type
)) {
458 /* Non-GPU queries. */
459 switch (rquery
->type
) {
460 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
462 case R600_QUERY_DRAW_CALLS
:
463 rquery
->begin_result
= rctx
->num_draw_calls
;
465 case R600_QUERY_REQUESTED_VRAM
:
466 case R600_QUERY_REQUESTED_GTT
:
467 case R600_QUERY_VRAM_USAGE
:
468 case R600_QUERY_GTT_USAGE
:
469 case R600_QUERY_GPU_TEMPERATURE
:
470 case R600_QUERY_CURRENT_GPU_SCLK
:
471 case R600_QUERY_CURRENT_GPU_MCLK
:
472 rquery
->begin_result
= 0;
474 case R600_QUERY_BUFFER_WAIT_TIME
:
475 rquery
->begin_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_BUFFER_WAIT_TIME_NS
) / 1000;
477 case R600_QUERY_NUM_CS_FLUSHES
:
478 rquery
->begin_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_NUM_CS_FLUSHES
);
480 case R600_QUERY_NUM_BYTES_MOVED
:
481 rquery
->begin_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_NUM_BYTES_MOVED
);
483 case R600_QUERY_GPU_LOAD
:
484 rquery
->begin_result
= r600_gpu_load_begin(rctx
->screen
);
488 /* Discard the old query buffers. */
490 struct r600_query_buffer
*qbuf
= prev
;
491 prev
= prev
->previous
;
492 pipe_resource_reference((struct pipe_resource
**)&qbuf
->buf
, NULL
);
496 /* Obtain a new buffer if the current one can't be mapped without a stall. */
497 if (r600_rings_is_buffer_referenced(rctx
, rquery
->buffer
.buf
->cs_buf
, RADEON_USAGE_READWRITE
) ||
498 rctx
->ws
->buffer_is_busy(rquery
->buffer
.buf
->buf
, RADEON_USAGE_READWRITE
)) {
499 pipe_resource_reference((struct pipe_resource
**)&rquery
->buffer
.buf
, NULL
);
500 rquery
->buffer
.buf
= r600_new_query_buffer(rctx
, rquery
->type
);
503 rquery
->buffer
.results_end
= 0;
504 rquery
->buffer
.previous
= NULL
;
506 r600_emit_query_begin(rctx
, rquery
);
508 if (r600_is_timer_query(rquery
->type
))
509 LIST_ADDTAIL(&rquery
->list
, &rctx
->active_timer_queries
);
511 LIST_ADDTAIL(&rquery
->list
, &rctx
->active_nontimer_queries
);
515 static void r600_end_query(struct pipe_context
*ctx
, struct pipe_query
*query
)
517 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
518 struct r600_query
*rquery
= (struct r600_query
*)query
;
520 /* Non-GPU queries. */
521 switch (rquery
->type
) {
522 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
524 case PIPE_QUERY_GPU_FINISHED
:
525 rctx
->rings
.gfx
.flush(rctx
, RADEON_FLUSH_ASYNC
, &rquery
->fence
);
527 case R600_QUERY_DRAW_CALLS
:
528 rquery
->end_result
= rctx
->num_draw_calls
;
530 case R600_QUERY_REQUESTED_VRAM
:
531 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_REQUESTED_VRAM_MEMORY
);
533 case R600_QUERY_REQUESTED_GTT
:
534 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_REQUESTED_GTT_MEMORY
);
536 case R600_QUERY_BUFFER_WAIT_TIME
:
537 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_BUFFER_WAIT_TIME_NS
) / 1000;
539 case R600_QUERY_NUM_CS_FLUSHES
:
540 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_NUM_CS_FLUSHES
);
542 case R600_QUERY_NUM_BYTES_MOVED
:
543 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_NUM_BYTES_MOVED
);
545 case R600_QUERY_VRAM_USAGE
:
546 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_VRAM_USAGE
);
548 case R600_QUERY_GTT_USAGE
:
549 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_GTT_USAGE
);
551 case R600_QUERY_GPU_TEMPERATURE
:
552 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_GPU_TEMPERATURE
) / 1000;
554 case R600_QUERY_CURRENT_GPU_SCLK
:
555 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_CURRENT_SCLK
) * 1000000;
557 case R600_QUERY_CURRENT_GPU_MCLK
:
558 rquery
->end_result
= rctx
->ws
->query_value(rctx
->ws
, RADEON_CURRENT_MCLK
) * 1000000;
560 case R600_QUERY_GPU_LOAD
:
561 rquery
->end_result
= r600_gpu_load_end(rctx
->screen
, rquery
->begin_result
);
565 r600_emit_query_end(rctx
, rquery
);
567 if (r600_query_needs_begin(rquery
->type
))
568 LIST_DELINIT(&rquery
->list
);
571 static unsigned r600_query_read_result(char *map
, unsigned start_index
, unsigned end_index
,
572 bool test_status_bit
)
574 uint32_t *current_result
= (uint32_t*)map
;
577 start
= (uint64_t)current_result
[start_index
] |
578 (uint64_t)current_result
[start_index
+1] << 32;
579 end
= (uint64_t)current_result
[end_index
] |
580 (uint64_t)current_result
[end_index
+1] << 32;
582 if (!test_status_bit
||
583 ((start
& 0x8000000000000000UL
) && (end
& 0x8000000000000000UL
))) {
589 static boolean
r600_get_query_buffer_result(struct r600_common_context
*ctx
,
590 struct r600_query
*query
,
591 struct r600_query_buffer
*qbuf
,
593 union pipe_query_result
*result
)
595 struct pipe_screen
*screen
= ctx
->b
.screen
;
596 unsigned results_base
= 0;
599 /* Non-GPU queries. */
600 switch (query
->type
) {
601 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
602 /* Convert from cycles per millisecond to cycles per second (Hz). */
603 result
->timestamp_disjoint
.frequency
=
604 (uint64_t)ctx
->screen
->info
.r600_clock_crystal_freq
* 1000;
605 result
->timestamp_disjoint
.disjoint
= FALSE
;
607 case PIPE_QUERY_GPU_FINISHED
:
608 result
->b
= screen
->fence_finish(screen
, query
->fence
,
609 wait
? PIPE_TIMEOUT_INFINITE
: 0);
611 case R600_QUERY_DRAW_CALLS
:
612 case R600_QUERY_REQUESTED_VRAM
:
613 case R600_QUERY_REQUESTED_GTT
:
614 case R600_QUERY_BUFFER_WAIT_TIME
:
615 case R600_QUERY_NUM_CS_FLUSHES
:
616 case R600_QUERY_NUM_BYTES_MOVED
:
617 case R600_QUERY_VRAM_USAGE
:
618 case R600_QUERY_GTT_USAGE
:
619 case R600_QUERY_GPU_TEMPERATURE
:
620 case R600_QUERY_CURRENT_GPU_SCLK
:
621 case R600_QUERY_CURRENT_GPU_MCLK
:
622 result
->u64
= query
->end_result
- query
->begin_result
;
624 case R600_QUERY_GPU_LOAD
:
625 result
->u64
= query
->end_result
;
629 map
= r600_buffer_map_sync_with_rings(ctx
, qbuf
->buf
,
631 (wait
? 0 : PIPE_TRANSFER_DONTBLOCK
));
635 /* count all results across all data blocks */
636 switch (query
->type
) {
637 case PIPE_QUERY_OCCLUSION_COUNTER
:
638 while (results_base
!= qbuf
->results_end
) {
640 r600_query_read_result(map
+ results_base
, 0, 2, true);
644 case PIPE_QUERY_OCCLUSION_PREDICATE
:
645 while (results_base
!= qbuf
->results_end
) {
646 result
->b
= result
->b
||
647 r600_query_read_result(map
+ results_base
, 0, 2, true) != 0;
651 case PIPE_QUERY_TIME_ELAPSED
:
652 while (results_base
!= qbuf
->results_end
) {
654 r600_query_read_result(map
+ results_base
, 0, 2, false);
655 results_base
+= query
->result_size
;
658 case PIPE_QUERY_TIMESTAMP
:
660 uint32_t *current_result
= (uint32_t*)map
;
661 result
->u64
= (uint64_t)current_result
[0] |
662 (uint64_t)current_result
[1] << 32;
665 case PIPE_QUERY_PRIMITIVES_EMITTED
:
666 /* SAMPLE_STREAMOUTSTATS stores this structure:
668 * u64 NumPrimitivesWritten;
669 * u64 PrimitiveStorageNeeded;
671 * We only need NumPrimitivesWritten here. */
672 while (results_base
!= qbuf
->results_end
) {
674 r600_query_read_result(map
+ results_base
, 2, 6, true);
675 results_base
+= query
->result_size
;
678 case PIPE_QUERY_PRIMITIVES_GENERATED
:
679 /* Here we read PrimitiveStorageNeeded. */
680 while (results_base
!= qbuf
->results_end
) {
682 r600_query_read_result(map
+ results_base
, 0, 4, true);
683 results_base
+= query
->result_size
;
686 case PIPE_QUERY_SO_STATISTICS
:
687 while (results_base
!= qbuf
->results_end
) {
688 result
->so_statistics
.num_primitives_written
+=
689 r600_query_read_result(map
+ results_base
, 2, 6, true);
690 result
->so_statistics
.primitives_storage_needed
+=
691 r600_query_read_result(map
+ results_base
, 0, 4, true);
692 results_base
+= query
->result_size
;
695 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
696 while (results_base
!= qbuf
->results_end
) {
697 result
->b
= result
->b
||
698 r600_query_read_result(map
+ results_base
, 2, 6, true) !=
699 r600_query_read_result(map
+ results_base
, 0, 4, true);
700 results_base
+= query
->result_size
;
703 case PIPE_QUERY_PIPELINE_STATISTICS
:
704 if (ctx
->chip_class
>= EVERGREEN
) {
705 while (results_base
!= qbuf
->results_end
) {
706 result
->pipeline_statistics
.ps_invocations
+=
707 r600_query_read_result(map
+ results_base
, 0, 22, false);
708 result
->pipeline_statistics
.c_primitives
+=
709 r600_query_read_result(map
+ results_base
, 2, 24, false);
710 result
->pipeline_statistics
.c_invocations
+=
711 r600_query_read_result(map
+ results_base
, 4, 26, false);
712 result
->pipeline_statistics
.vs_invocations
+=
713 r600_query_read_result(map
+ results_base
, 6, 28, false);
714 result
->pipeline_statistics
.gs_invocations
+=
715 r600_query_read_result(map
+ results_base
, 8, 30, false);
716 result
->pipeline_statistics
.gs_primitives
+=
717 r600_query_read_result(map
+ results_base
, 10, 32, false);
718 result
->pipeline_statistics
.ia_primitives
+=
719 r600_query_read_result(map
+ results_base
, 12, 34, false);
720 result
->pipeline_statistics
.ia_vertices
+=
721 r600_query_read_result(map
+ results_base
, 14, 36, false);
722 result
->pipeline_statistics
.hs_invocations
+=
723 r600_query_read_result(map
+ results_base
, 16, 38, false);
724 result
->pipeline_statistics
.ds_invocations
+=
725 r600_query_read_result(map
+ results_base
, 18, 40, false);
726 result
->pipeline_statistics
.cs_invocations
+=
727 r600_query_read_result(map
+ results_base
, 20, 42, false);
728 results_base
+= query
->result_size
;
731 while (results_base
!= qbuf
->results_end
) {
732 result
->pipeline_statistics
.ps_invocations
+=
733 r600_query_read_result(map
+ results_base
, 0, 16, false);
734 result
->pipeline_statistics
.c_primitives
+=
735 r600_query_read_result(map
+ results_base
, 2, 18, false);
736 result
->pipeline_statistics
.c_invocations
+=
737 r600_query_read_result(map
+ results_base
, 4, 20, false);
738 result
->pipeline_statistics
.vs_invocations
+=
739 r600_query_read_result(map
+ results_base
, 6, 22, false);
740 result
->pipeline_statistics
.gs_invocations
+=
741 r600_query_read_result(map
+ results_base
, 8, 24, false);
742 result
->pipeline_statistics
.gs_primitives
+=
743 r600_query_read_result(map
+ results_base
, 10, 26, false);
744 result
->pipeline_statistics
.ia_primitives
+=
745 r600_query_read_result(map
+ results_base
, 12, 28, false);
746 result
->pipeline_statistics
.ia_vertices
+=
747 r600_query_read_result(map
+ results_base
, 14, 30, false);
748 results_base
+= query
->result_size
;
751 #if 0 /* for testing */
752 printf("Pipeline stats: IA verts=%llu, IA prims=%llu, VS=%llu, HS=%llu, "
753 "DS=%llu, GS=%llu, GS prims=%llu, Clipper=%llu, "
754 "Clipper prims=%llu, PS=%llu, CS=%llu\n",
755 result
->pipeline_statistics
.ia_vertices
,
756 result
->pipeline_statistics
.ia_primitives
,
757 result
->pipeline_statistics
.vs_invocations
,
758 result
->pipeline_statistics
.hs_invocations
,
759 result
->pipeline_statistics
.ds_invocations
,
760 result
->pipeline_statistics
.gs_invocations
,
761 result
->pipeline_statistics
.gs_primitives
,
762 result
->pipeline_statistics
.c_invocations
,
763 result
->pipeline_statistics
.c_primitives
,
764 result
->pipeline_statistics
.ps_invocations
,
765 result
->pipeline_statistics
.cs_invocations
);
775 static boolean
r600_get_query_result(struct pipe_context
*ctx
,
776 struct pipe_query
*query
,
777 boolean wait
, union pipe_query_result
*result
)
779 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
780 struct r600_query
*rquery
= (struct r600_query
*)query
;
781 struct r600_query_buffer
*qbuf
;
783 util_query_clear_result(result
, rquery
->type
);
785 for (qbuf
= &rquery
->buffer
; qbuf
; qbuf
= qbuf
->previous
) {
786 if (!r600_get_query_buffer_result(rctx
, rquery
, qbuf
, wait
, result
)) {
791 /* Convert the time to expected units. */
792 if (rquery
->type
== PIPE_QUERY_TIME_ELAPSED
||
793 rquery
->type
== PIPE_QUERY_TIMESTAMP
) {
794 result
->u64
= (1000000 * result
->u64
) / rctx
->screen
->info
.r600_clock_crystal_freq
;
799 static void r600_render_condition(struct pipe_context
*ctx
,
800 struct pipe_query
*query
,
804 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
805 struct r600_query
*rquery
= (struct r600_query
*)query
;
806 bool wait_flag
= false;
808 rctx
->current_render_cond
= query
;
809 rctx
->current_render_cond_cond
= condition
;
810 rctx
->current_render_cond_mode
= mode
;
813 if (rctx
->predicate_drawing
) {
814 rctx
->predicate_drawing
= false;
815 r600_emit_query_predication(rctx
, NULL
, PREDICATION_OP_CLEAR
, false);
820 if (mode
== PIPE_RENDER_COND_WAIT
||
821 mode
== PIPE_RENDER_COND_BY_REGION_WAIT
) {
825 rctx
->predicate_drawing
= true;
827 switch (rquery
->type
) {
828 case PIPE_QUERY_OCCLUSION_COUNTER
:
829 case PIPE_QUERY_OCCLUSION_PREDICATE
:
830 r600_emit_query_predication(rctx
, rquery
, PREDICATION_OP_ZPASS
, wait_flag
);
832 case PIPE_QUERY_PRIMITIVES_EMITTED
:
833 case PIPE_QUERY_PRIMITIVES_GENERATED
:
834 case PIPE_QUERY_SO_STATISTICS
:
835 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
836 r600_emit_query_predication(rctx
, rquery
, PREDICATION_OP_PRIMCOUNT
, wait_flag
);
843 static void r600_suspend_queries(struct r600_common_context
*ctx
,
844 struct list_head
*query_list
,
845 unsigned *num_cs_dw_queries_suspend
)
847 struct r600_query
*query
;
849 LIST_FOR_EACH_ENTRY(query
, query_list
, list
) {
850 r600_emit_query_end(ctx
, query
);
852 assert(*num_cs_dw_queries_suspend
== 0);
855 void r600_suspend_nontimer_queries(struct r600_common_context
*ctx
)
857 r600_suspend_queries(ctx
, &ctx
->active_nontimer_queries
,
858 &ctx
->num_cs_dw_nontimer_queries_suspend
);
861 void r600_suspend_timer_queries(struct r600_common_context
*ctx
)
863 r600_suspend_queries(ctx
, &ctx
->active_timer_queries
,
864 &ctx
->num_cs_dw_timer_queries_suspend
);
867 static unsigned r600_queries_num_cs_dw_for_resuming(struct r600_common_context
*ctx
,
868 struct list_head
*query_list
)
870 struct r600_query
*query
;
873 LIST_FOR_EACH_ENTRY(query
, query_list
, list
) {
875 num_dw
+= query
->num_cs_dw
* 2;
877 /* Workaround for the fact that
878 * num_cs_dw_nontimer_queries_suspend is incremented for every
879 * resumed query, which raises the bar in need_cs_space for
880 * queries about to be resumed.
882 num_dw
+= query
->num_cs_dw
;
884 /* primitives generated query */
885 num_dw
+= ctx
->streamout
.enable_atom
.num_dw
;
886 /* guess for ZPASS enable or PERFECT_ZPASS_COUNT enable updates */
892 static void r600_resume_queries(struct r600_common_context
*ctx
,
893 struct list_head
*query_list
,
894 unsigned *num_cs_dw_queries_suspend
)
896 struct r600_query
*query
;
897 unsigned num_cs_dw
= r600_queries_num_cs_dw_for_resuming(ctx
, query_list
);
899 assert(*num_cs_dw_queries_suspend
== 0);
901 /* Check CS space here. Resuming must not be interrupted by flushes. */
902 ctx
->need_gfx_cs_space(&ctx
->b
, num_cs_dw
, TRUE
);
904 LIST_FOR_EACH_ENTRY(query
, query_list
, list
) {
905 r600_emit_query_begin(ctx
, query
);
909 void r600_resume_nontimer_queries(struct r600_common_context
*ctx
)
911 r600_resume_queries(ctx
, &ctx
->active_nontimer_queries
,
912 &ctx
->num_cs_dw_nontimer_queries_suspend
);
915 void r600_resume_timer_queries(struct r600_common_context
*ctx
)
917 r600_resume_queries(ctx
, &ctx
->active_timer_queries
,
918 &ctx
->num_cs_dw_timer_queries_suspend
);
921 /* Get backends mask */
922 void r600_query_init_backend_mask(struct r600_common_context
*ctx
)
924 struct radeon_winsys_cs
*cs
= ctx
->rings
.gfx
.cs
;
925 struct r600_resource
*buffer
;
927 unsigned num_backends
= ctx
->screen
->info
.r600_num_backends
;
928 unsigned i
, mask
= 0;
930 /* if backend_map query is supported by the kernel */
931 if (ctx
->screen
->info
.r600_backend_map_valid
) {
932 unsigned num_tile_pipes
= ctx
->screen
->info
.r600_num_tile_pipes
;
933 unsigned backend_map
= ctx
->screen
->info
.r600_backend_map
;
934 unsigned item_width
, item_mask
;
936 if (ctx
->chip_class
>= EVERGREEN
) {
944 while(num_tile_pipes
--) {
945 i
= backend_map
& item_mask
;
947 backend_map
>>= item_width
;
950 ctx
->backend_mask
= mask
;
955 /* otherwise backup path for older kernels */
957 /* create buffer for event data */
958 buffer
= (struct r600_resource
*)
959 pipe_buffer_create(ctx
->b
.screen
, PIPE_BIND_CUSTOM
,
960 PIPE_USAGE_STAGING
, ctx
->max_db
*16);
964 /* initialize buffer with zeroes */
965 results
= r600_buffer_map_sync_with_rings(ctx
, buffer
, PIPE_TRANSFER_WRITE
);
967 memset(results
, 0, ctx
->max_db
* 4 * 4);
969 /* emit EVENT_WRITE for ZPASS_DONE */
970 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 2, 0));
971 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE
) | EVENT_INDEX(1));
972 radeon_emit(cs
, buffer
->gpu_address
);
973 radeon_emit(cs
, buffer
->gpu_address
>> 32);
975 r600_emit_reloc(ctx
, &ctx
->rings
.gfx
, buffer
, RADEON_USAGE_WRITE
, RADEON_PRIO_MIN
);
977 /* analyze results */
978 results
= r600_buffer_map_sync_with_rings(ctx
, buffer
, PIPE_TRANSFER_READ
);
980 for(i
= 0; i
< ctx
->max_db
; i
++) {
981 /* at least highest bit will be set if backend is used */
982 if (results
[i
*4 + 1])
988 pipe_resource_reference((struct pipe_resource
**)&buffer
, NULL
);
991 ctx
->backend_mask
= mask
;
996 /* fallback to old method - set num_backends lower bits to 1 */
997 ctx
->backend_mask
= (~((uint32_t)0))>>(32-num_backends
);
1001 void r600_query_init(struct r600_common_context
*rctx
)
1003 rctx
->b
.create_query
= r600_create_query
;
1004 rctx
->b
.destroy_query
= r600_destroy_query
;
1005 rctx
->b
.begin_query
= r600_begin_query
;
1006 rctx
->b
.end_query
= r600_end_query
;
1007 rctx
->b
.get_query_result
= r600_get_query_result
;
1009 if (((struct r600_common_screen
*)rctx
->b
.screen
)->info
.r600_num_backends
> 0)
1010 rctx
->b
.render_condition
= r600_render_condition
;
1012 LIST_INITHEAD(&rctx
->active_nontimer_queries
);
1013 LIST_INITHEAD(&rctx
->active_timer_queries
);