r600g: move queries to drivers/radeon
[mesa.git] / src / gallium / drivers / radeon / r600_query.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2014 Marek Olšák <marek.olsak@amd.com>
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 #include "r600_cs.h"
26 #include "util/u_memory.h"
27
28
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 */
33 unsigned results_end;
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;
38 };
39
40 struct r600_query {
41 /* The query buffer and how many results are in it. */
42 struct r600_query_buffer buffer;
43 /* The type of query */
44 unsigned type;
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. */
47 unsigned result_size;
48 /* The number of dwords for begin_query or end_query. */
49 unsigned num_cs_dw;
50 /* linked list of queries */
51 struct list_head list;
52 /* for custom non-GPU queries */
53 uint64_t begin_result;
54 uint64_t end_result;
55 };
56
57
58 static bool r600_is_timer_query(unsigned type)
59 {
60 return type == PIPE_QUERY_TIME_ELAPSED ||
61 type == PIPE_QUERY_TIMESTAMP ||
62 type == PIPE_QUERY_TIMESTAMP_DISJOINT;
63 }
64
65 static bool r600_query_needs_begin(unsigned type)
66 {
67 return type != PIPE_QUERY_GPU_FINISHED &&
68 type != PIPE_QUERY_TIMESTAMP;
69 }
70
71 static struct r600_resource *r600_new_query_buffer(struct r600_common_context *ctx, unsigned type)
72 {
73 unsigned j, i, num_results, buf_size = 4096;
74 uint32_t *results;
75
76 /* Non-GPU queries. */
77 switch (type) {
78 case R600_QUERY_DRAW_CALLS:
79 case R600_QUERY_REQUESTED_VRAM:
80 case R600_QUERY_REQUESTED_GTT:
81 case R600_QUERY_BUFFER_WAIT_TIME:
82 return NULL;
83 }
84
85 /* Queries are normally read by the CPU after
86 * being written by the gpu, hence staging is probably a good
87 * usage pattern.
88 */
89 struct r600_resource *buf = (struct r600_resource*)
90 pipe_buffer_create(ctx->b.screen, PIPE_BIND_CUSTOM,
91 PIPE_USAGE_STAGING, buf_size);
92
93 switch (type) {
94 case PIPE_QUERY_OCCLUSION_COUNTER:
95 case PIPE_QUERY_OCCLUSION_PREDICATE:
96 results = r600_buffer_map_sync_with_rings(ctx, buf, PIPE_TRANSFER_WRITE);
97 memset(results, 0, buf_size);
98
99 /* Set top bits for unused backends. */
100 num_results = buf_size / (16 * ctx->max_db);
101 for (j = 0; j < num_results; j++) {
102 for (i = 0; i < ctx->max_db; i++) {
103 if (!(ctx->backend_mask & (1<<i))) {
104 results[(i * 4)+1] = 0x80000000;
105 results[(i * 4)+3] = 0x80000000;
106 }
107 }
108 results += 4 * ctx->max_db;
109 }
110 ctx->ws->buffer_unmap(buf->cs_buf);
111 break;
112 case PIPE_QUERY_TIME_ELAPSED:
113 case PIPE_QUERY_TIMESTAMP:
114 break;
115 case PIPE_QUERY_PRIMITIVES_EMITTED:
116 case PIPE_QUERY_PRIMITIVES_GENERATED:
117 case PIPE_QUERY_SO_STATISTICS:
118 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
119 case PIPE_QUERY_PIPELINE_STATISTICS:
120 results = r600_buffer_map_sync_with_rings(ctx, buf, PIPE_TRANSFER_WRITE);
121 memset(results, 0, buf_size);
122 ctx->ws->buffer_unmap(buf->cs_buf);
123 break;
124 default:
125 assert(0);
126 }
127 return buf;
128 }
129
130 static void r600_update_occlusion_query_state(struct r600_common_context *rctx,
131 unsigned type, int diff)
132 {
133 if (type == PIPE_QUERY_OCCLUSION_COUNTER ||
134 type == PIPE_QUERY_OCCLUSION_PREDICATE) {
135 bool old_enable = rctx->num_occlusion_queries != 0;
136 bool enable;
137
138 rctx->num_occlusion_queries += diff;
139 assert(rctx->num_occlusion_queries >= 0);
140
141 enable = rctx->num_occlusion_queries != 0;
142
143 if (enable != old_enable) {
144 rctx->set_occlusion_query_state(&rctx->b, enable);
145 }
146 }
147 }
148
149 static void r600_emit_query_begin(struct r600_common_context *ctx, struct r600_query *query)
150 {
151 struct radeon_winsys_cs *cs = ctx->rings.gfx.cs;
152 uint64_t va;
153
154 r600_update_occlusion_query_state(ctx, query->type, 1);
155 ctx->need_gfx_cs_space(&ctx->b, query->num_cs_dw * 2, TRUE);
156
157 /* Get a new query buffer if needed. */
158 if (query->buffer.results_end + query->result_size > query->buffer.buf->b.b.width0) {
159 struct r600_query_buffer *qbuf = MALLOC_STRUCT(r600_query_buffer);
160 *qbuf = query->buffer;
161 query->buffer.buf = r600_new_query_buffer(ctx, query->type);
162 query->buffer.results_end = 0;
163 query->buffer.previous = qbuf;
164 }
165
166 /* emit begin query */
167 va = r600_resource_va(ctx->b.screen, (void*)query->buffer.buf);
168 va += query->buffer.results_end;
169
170 switch (query->type) {
171 case PIPE_QUERY_OCCLUSION_COUNTER:
172 case PIPE_QUERY_OCCLUSION_PREDICATE:
173 cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE, 2, 0);
174 cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_ZPASS_DONE) | EVENT_INDEX(1);
175 cs->buf[cs->cdw++] = va;
176 cs->buf[cs->cdw++] = (va >> 32UL) & 0xFF;
177 break;
178 case PIPE_QUERY_PRIMITIVES_EMITTED:
179 case PIPE_QUERY_PRIMITIVES_GENERATED:
180 case PIPE_QUERY_SO_STATISTICS:
181 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
182 cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE, 2, 0);
183 cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_SAMPLE_STREAMOUTSTATS) | EVENT_INDEX(3);
184 cs->buf[cs->cdw++] = va;
185 cs->buf[cs->cdw++] = (va >> 32UL) & 0xFF;
186 break;
187 case PIPE_QUERY_TIME_ELAPSED:
188 cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE_EOP, 4, 0);
189 cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_CACHE_FLUSH_AND_INV_TS_EVENT) | EVENT_INDEX(5);
190 cs->buf[cs->cdw++] = va;
191 cs->buf[cs->cdw++] = (3 << 29) | ((va >> 32UL) & 0xFF);
192 cs->buf[cs->cdw++] = 0;
193 cs->buf[cs->cdw++] = 0;
194 break;
195 case PIPE_QUERY_PIPELINE_STATISTICS:
196 if (!ctx->num_pipelinestat_queries) {
197 cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE, 0, 0);
198 cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_PIPELINESTAT_START) | EVENT_INDEX(0);
199 }
200 ctx->num_pipelinestat_queries++;
201 cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE, 2, 0);
202 cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT) | EVENT_INDEX(2);
203 cs->buf[cs->cdw++] = va;
204 cs->buf[cs->cdw++] = (va >> 32UL) & 0xFF;
205 break;
206 default:
207 assert(0);
208 }
209 cs->buf[cs->cdw++] = PKT3(PKT3_NOP, 0, 0);
210 cs->buf[cs->cdw++] = r600_context_bo_reloc(ctx, &ctx->rings.gfx, query->buffer.buf, RADEON_USAGE_WRITE);
211
212 if (!r600_is_timer_query(query->type)) {
213 ctx->num_cs_dw_nontimer_queries_suspend += query->num_cs_dw;
214 }
215 }
216
217 static void r600_emit_query_end(struct r600_common_context *ctx, struct r600_query *query)
218 {
219 struct radeon_winsys_cs *cs = ctx->rings.gfx.cs;
220 uint64_t va;
221
222 /* The queries which need begin already called this in begin_query. */
223 if (!r600_query_needs_begin(query->type)) {
224 ctx->need_gfx_cs_space(&ctx->b, query->num_cs_dw, FALSE);
225 }
226
227 va = r600_resource_va(ctx->b.screen, (void*)query->buffer.buf);
228 /* emit end query */
229 switch (query->type) {
230 case PIPE_QUERY_OCCLUSION_COUNTER:
231 case PIPE_QUERY_OCCLUSION_PREDICATE:
232 va += query->buffer.results_end + 8;
233 cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE, 2, 0);
234 cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_ZPASS_DONE) | EVENT_INDEX(1);
235 cs->buf[cs->cdw++] = va;
236 cs->buf[cs->cdw++] = (va >> 32UL) & 0xFF;
237 break;
238 case PIPE_QUERY_PRIMITIVES_EMITTED:
239 case PIPE_QUERY_PRIMITIVES_GENERATED:
240 case PIPE_QUERY_SO_STATISTICS:
241 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
242 va += query->buffer.results_end + query->result_size/2;
243 cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE, 2, 0);
244 cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_SAMPLE_STREAMOUTSTATS) | EVENT_INDEX(3);
245 cs->buf[cs->cdw++] = va;
246 cs->buf[cs->cdw++] = (va >> 32UL) & 0xFF;
247 break;
248 case PIPE_QUERY_TIME_ELAPSED:
249 va += query->buffer.results_end + query->result_size/2;
250 /* fall through */
251 case PIPE_QUERY_TIMESTAMP:
252 cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE_EOP, 4, 0);
253 cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_CACHE_FLUSH_AND_INV_TS_EVENT) | EVENT_INDEX(5);
254 cs->buf[cs->cdw++] = va;
255 cs->buf[cs->cdw++] = (3 << 29) | ((va >> 32UL) & 0xFF);
256 cs->buf[cs->cdw++] = 0;
257 cs->buf[cs->cdw++] = 0;
258 break;
259 case PIPE_QUERY_PIPELINE_STATISTICS:
260 assert(ctx->num_pipelinestat_queries > 0);
261 ctx->num_pipelinestat_queries--;
262 if (!ctx->num_pipelinestat_queries) {
263 cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE, 0, 0);
264 cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_PIPELINESTAT_STOP) | EVENT_INDEX(0);
265 }
266 va += query->buffer.results_end + query->result_size/2;
267 cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE, 2, 0);
268 cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT) | EVENT_INDEX(2);
269 cs->buf[cs->cdw++] = va;
270 cs->buf[cs->cdw++] = (va >> 32UL) & 0xFF;
271 break;
272 default:
273 assert(0);
274 }
275 cs->buf[cs->cdw++] = PKT3(PKT3_NOP, 0, 0);
276 cs->buf[cs->cdw++] = r600_context_bo_reloc(ctx, &ctx->rings.gfx, query->buffer.buf, RADEON_USAGE_WRITE);
277
278 query->buffer.results_end += query->result_size;
279
280 if (r600_query_needs_begin(query->type)) {
281 if (!r600_is_timer_query(query->type)) {
282 ctx->num_cs_dw_nontimer_queries_suspend -= query->num_cs_dw;
283 }
284 }
285
286 r600_update_occlusion_query_state(ctx, query->type, -1);
287 }
288
289 static void r600_emit_query_predication(struct r600_common_context *ctx, struct r600_query *query,
290 int operation, bool flag_wait)
291 {
292 struct radeon_winsys_cs *cs = ctx->rings.gfx.cs;
293
294 if (operation == PREDICATION_OP_CLEAR) {
295 ctx->need_gfx_cs_space(&ctx->b, 3, FALSE);
296
297 cs->buf[cs->cdw++] = PKT3(PKT3_SET_PREDICATION, 1, 0);
298 cs->buf[cs->cdw++] = 0;
299 cs->buf[cs->cdw++] = PRED_OP(PREDICATION_OP_CLEAR);
300 } else {
301 struct r600_query_buffer *qbuf;
302 unsigned count;
303 uint32_t op;
304
305 /* Find how many results there are. */
306 count = 0;
307 for (qbuf = &query->buffer; qbuf; qbuf = qbuf->previous) {
308 count += qbuf->results_end / query->result_size;
309 }
310
311 ctx->need_gfx_cs_space(&ctx->b, 5 * count, TRUE);
312
313 op = PRED_OP(operation) | PREDICATION_DRAW_VISIBLE |
314 (flag_wait ? PREDICATION_HINT_WAIT : PREDICATION_HINT_NOWAIT_DRAW);
315
316 /* emit predicate packets for all data blocks */
317 for (qbuf = &query->buffer; qbuf; qbuf = qbuf->previous) {
318 unsigned results_base = 0;
319 uint64_t va = r600_resource_va(ctx->b.screen, &qbuf->buf->b.b);
320
321 while (results_base < qbuf->results_end) {
322 cs->buf[cs->cdw++] = PKT3(PKT3_SET_PREDICATION, 1, 0);
323 cs->buf[cs->cdw++] = (va + results_base) & 0xFFFFFFFFUL;
324 cs->buf[cs->cdw++] = op | (((va + results_base) >> 32UL) & 0xFF);
325 cs->buf[cs->cdw++] = PKT3(PKT3_NOP, 0, 0);
326 cs->buf[cs->cdw++] = r600_context_bo_reloc(ctx, &ctx->rings.gfx, qbuf->buf, RADEON_USAGE_READ);
327 results_base += query->result_size;
328
329 /* set CONTINUE bit for all packets except the first */
330 op |= PREDICATION_CONTINUE;
331 }
332 } while (qbuf);
333 }
334 }
335
336 static struct pipe_query *r600_create_query(struct pipe_context *ctx, unsigned query_type)
337 {
338 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
339 struct r600_query *query;
340 bool skip_allocation = false;
341
342 query = CALLOC_STRUCT(r600_query);
343 if (query == NULL)
344 return NULL;
345
346 query->type = query_type;
347
348 switch (query_type) {
349 case PIPE_QUERY_OCCLUSION_COUNTER:
350 case PIPE_QUERY_OCCLUSION_PREDICATE:
351 query->result_size = 16 * rctx->max_db;
352 query->num_cs_dw = 6;
353 break;
354 case PIPE_QUERY_TIME_ELAPSED:
355 query->result_size = 16;
356 query->num_cs_dw = 8;
357 break;
358 case PIPE_QUERY_TIMESTAMP:
359 query->result_size = 8;
360 query->num_cs_dw = 8;
361 break;
362 case PIPE_QUERY_PRIMITIVES_EMITTED:
363 case PIPE_QUERY_PRIMITIVES_GENERATED:
364 case PIPE_QUERY_SO_STATISTICS:
365 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
366 /* NumPrimitivesWritten, PrimitiveStorageNeeded. */
367 query->result_size = 32;
368 query->num_cs_dw = 6;
369 break;
370 case PIPE_QUERY_PIPELINE_STATISTICS:
371 /* 11 values on EG, 8 on R600. */
372 query->result_size = (rctx->chip_class >= EVERGREEN ? 11 : 8) * 16;
373 query->num_cs_dw = 8;
374 break;
375 /* Non-GPU queries. */
376 case R600_QUERY_DRAW_CALLS:
377 case R600_QUERY_REQUESTED_VRAM:
378 case R600_QUERY_REQUESTED_GTT:
379 case R600_QUERY_BUFFER_WAIT_TIME:
380 skip_allocation = true;
381 break;
382 default:
383 assert(0);
384 FREE(query);
385 return NULL;
386 }
387
388 if (!skip_allocation) {
389 query->buffer.buf = r600_new_query_buffer(rctx, query_type);
390 if (!query->buffer.buf) {
391 FREE(query);
392 return NULL;
393 }
394 }
395 return (struct pipe_query*)query;
396 }
397
398 static void r600_destroy_query(struct pipe_context *ctx, struct pipe_query *query)
399 {
400 struct r600_query *rquery = (struct r600_query*)query;
401 struct r600_query_buffer *prev = rquery->buffer.previous;
402
403 /* Release all query buffers. */
404 while (prev) {
405 struct r600_query_buffer *qbuf = prev;
406 prev = prev->previous;
407 pipe_resource_reference((struct pipe_resource**)&qbuf->buf, NULL);
408 FREE(qbuf);
409 }
410
411 pipe_resource_reference((struct pipe_resource**)&rquery->buffer.buf, NULL);
412 FREE(query);
413 }
414
415 static void r600_begin_query(struct pipe_context *ctx, struct pipe_query *query)
416 {
417 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
418 struct r600_query *rquery = (struct r600_query *)query;
419 struct r600_query_buffer *prev = rquery->buffer.previous;
420
421 if (!r600_query_needs_begin(rquery->type)) {
422 assert(0);
423 return;
424 }
425
426 /* Non-GPU queries. */
427 switch (rquery->type) {
428 case R600_QUERY_DRAW_CALLS:
429 rquery->begin_result = rctx->num_draw_calls;
430 return;
431 case R600_QUERY_REQUESTED_VRAM:
432 case R600_QUERY_REQUESTED_GTT:
433 rquery->begin_result = 0;
434 return;
435 case R600_QUERY_BUFFER_WAIT_TIME:
436 rquery->begin_result = rctx->ws->query_value(rctx->ws, RADEON_BUFFER_WAIT_TIME_NS);
437 return;
438 }
439
440 /* Discard the old query buffers. */
441 while (prev) {
442 struct r600_query_buffer *qbuf = prev;
443 prev = prev->previous;
444 pipe_resource_reference((struct pipe_resource**)&qbuf->buf, NULL);
445 FREE(qbuf);
446 }
447
448 /* Obtain a new buffer if the current one can't be mapped without a stall. */
449 if (r600_rings_is_buffer_referenced(rctx, rquery->buffer.buf->cs_buf, RADEON_USAGE_READWRITE) ||
450 rctx->ws->buffer_is_busy(rquery->buffer.buf->buf, RADEON_USAGE_READWRITE)) {
451 pipe_resource_reference((struct pipe_resource**)&rquery->buffer.buf, NULL);
452 rquery->buffer.buf = r600_new_query_buffer(rctx, rquery->type);
453 }
454
455 rquery->buffer.results_end = 0;
456 rquery->buffer.previous = NULL;
457
458 r600_emit_query_begin(rctx, rquery);
459
460 if (!r600_is_timer_query(rquery->type)) {
461 LIST_ADDTAIL(&rquery->list, &rctx->active_nontimer_queries);
462 }
463 }
464
465 static void r600_end_query(struct pipe_context *ctx, struct pipe_query *query)
466 {
467 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
468 struct r600_query *rquery = (struct r600_query *)query;
469
470 /* Non-GPU queries. */
471 switch (rquery->type) {
472 case R600_QUERY_DRAW_CALLS:
473 rquery->end_result = rctx->num_draw_calls;
474 return;
475 case R600_QUERY_REQUESTED_VRAM:
476 rquery->end_result = rctx->ws->query_value(rctx->ws, RADEON_REQUESTED_VRAM_MEMORY);
477 return;
478 case R600_QUERY_REQUESTED_GTT:
479 rquery->end_result = rctx->ws->query_value(rctx->ws, RADEON_REQUESTED_GTT_MEMORY);
480 return;
481 case R600_QUERY_BUFFER_WAIT_TIME:
482 rquery->end_result = rctx->ws->query_value(rctx->ws, RADEON_BUFFER_WAIT_TIME_NS);
483 return;
484 }
485
486 r600_emit_query_end(rctx, rquery);
487
488 if (r600_query_needs_begin(rquery->type) && !r600_is_timer_query(rquery->type)) {
489 LIST_DELINIT(&rquery->list);
490 }
491 }
492
493 static unsigned r600_query_read_result(char *map, unsigned start_index, unsigned end_index,
494 bool test_status_bit)
495 {
496 uint32_t *current_result = (uint32_t*)map;
497 uint64_t start, end;
498
499 start = (uint64_t)current_result[start_index] |
500 (uint64_t)current_result[start_index+1] << 32;
501 end = (uint64_t)current_result[end_index] |
502 (uint64_t)current_result[end_index+1] << 32;
503
504 if (!test_status_bit ||
505 ((start & 0x8000000000000000UL) && (end & 0x8000000000000000UL))) {
506 return end - start;
507 }
508 return 0;
509 }
510
511 static boolean r600_get_query_buffer_result(struct r600_common_context *ctx,
512 struct r600_query *query,
513 struct r600_query_buffer *qbuf,
514 boolean wait,
515 union pipe_query_result *result)
516 {
517 unsigned results_base = 0;
518 char *map;
519
520 /* Non-GPU queries. */
521 switch (query->type) {
522 case R600_QUERY_DRAW_CALLS:
523 case R600_QUERY_REQUESTED_VRAM:
524 case R600_QUERY_REQUESTED_GTT:
525 case R600_QUERY_BUFFER_WAIT_TIME:
526 result->u64 = query->end_result - query->begin_result;
527 return TRUE;
528 }
529
530 map = r600_buffer_map_sync_with_rings(ctx, qbuf->buf,
531 PIPE_TRANSFER_READ |
532 (wait ? 0 : PIPE_TRANSFER_DONTBLOCK));
533 if (!map)
534 return FALSE;
535
536 /* count all results across all data blocks */
537 switch (query->type) {
538 case PIPE_QUERY_OCCLUSION_COUNTER:
539 while (results_base != qbuf->results_end) {
540 result->u64 +=
541 r600_query_read_result(map + results_base, 0, 2, true);
542 results_base += 16;
543 }
544 break;
545 case PIPE_QUERY_OCCLUSION_PREDICATE:
546 while (results_base != qbuf->results_end) {
547 result->b = result->b ||
548 r600_query_read_result(map + results_base, 0, 2, true) != 0;
549 results_base += 16;
550 }
551 break;
552 case PIPE_QUERY_TIME_ELAPSED:
553 while (results_base != qbuf->results_end) {
554 result->u64 +=
555 r600_query_read_result(map + results_base, 0, 2, false);
556 results_base += query->result_size;
557 }
558 break;
559 case PIPE_QUERY_TIMESTAMP:
560 {
561 uint32_t *current_result = (uint32_t*)map;
562 result->u64 = (uint64_t)current_result[0] |
563 (uint64_t)current_result[1] << 32;
564 break;
565 }
566 case PIPE_QUERY_PRIMITIVES_EMITTED:
567 /* SAMPLE_STREAMOUTSTATS stores this structure:
568 * {
569 * u64 NumPrimitivesWritten;
570 * u64 PrimitiveStorageNeeded;
571 * }
572 * We only need NumPrimitivesWritten here. */
573 while (results_base != qbuf->results_end) {
574 result->u64 +=
575 r600_query_read_result(map + results_base, 2, 6, true);
576 results_base += query->result_size;
577 }
578 break;
579 case PIPE_QUERY_PRIMITIVES_GENERATED:
580 /* Here we read PrimitiveStorageNeeded. */
581 while (results_base != qbuf->results_end) {
582 result->u64 +=
583 r600_query_read_result(map + results_base, 0, 4, true);
584 results_base += query->result_size;
585 }
586 break;
587 case PIPE_QUERY_SO_STATISTICS:
588 while (results_base != qbuf->results_end) {
589 result->so_statistics.num_primitives_written +=
590 r600_query_read_result(map + results_base, 2, 6, true);
591 result->so_statistics.primitives_storage_needed +=
592 r600_query_read_result(map + results_base, 0, 4, true);
593 results_base += query->result_size;
594 }
595 break;
596 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
597 while (results_base != qbuf->results_end) {
598 result->b = result->b ||
599 r600_query_read_result(map + results_base, 2, 6, true) !=
600 r600_query_read_result(map + results_base, 0, 4, true);
601 results_base += query->result_size;
602 }
603 break;
604 case PIPE_QUERY_PIPELINE_STATISTICS:
605 if (ctx->chip_class >= EVERGREEN) {
606 while (results_base != qbuf->results_end) {
607 result->pipeline_statistics.ps_invocations +=
608 r600_query_read_result(map + results_base, 0, 22, false);
609 result->pipeline_statistics.c_primitives +=
610 r600_query_read_result(map + results_base, 2, 24, false);
611 result->pipeline_statistics.c_invocations +=
612 r600_query_read_result(map + results_base, 4, 26, false);
613 result->pipeline_statistics.vs_invocations +=
614 r600_query_read_result(map + results_base, 6, 28, false);
615 result->pipeline_statistics.gs_invocations +=
616 r600_query_read_result(map + results_base, 8, 30, false);
617 result->pipeline_statistics.gs_primitives +=
618 r600_query_read_result(map + results_base, 10, 32, false);
619 result->pipeline_statistics.ia_primitives +=
620 r600_query_read_result(map + results_base, 12, 34, false);
621 result->pipeline_statistics.ia_vertices +=
622 r600_query_read_result(map + results_base, 14, 36, false);
623 result->pipeline_statistics.hs_invocations +=
624 r600_query_read_result(map + results_base, 16, 38, false);
625 result->pipeline_statistics.ds_invocations +=
626 r600_query_read_result(map + results_base, 18, 40, false);
627 result->pipeline_statistics.cs_invocations +=
628 r600_query_read_result(map + results_base, 20, 42, false);
629 results_base += query->result_size;
630 }
631 } else {
632 while (results_base != qbuf->results_end) {
633 result->pipeline_statistics.ps_invocations +=
634 r600_query_read_result(map + results_base, 0, 16, false);
635 result->pipeline_statistics.c_primitives +=
636 r600_query_read_result(map + results_base, 2, 18, false);
637 result->pipeline_statistics.c_invocations +=
638 r600_query_read_result(map + results_base, 4, 20, false);
639 result->pipeline_statistics.vs_invocations +=
640 r600_query_read_result(map + results_base, 6, 22, false);
641 result->pipeline_statistics.gs_invocations +=
642 r600_query_read_result(map + results_base, 8, 24, false);
643 result->pipeline_statistics.gs_primitives +=
644 r600_query_read_result(map + results_base, 10, 26, false);
645 result->pipeline_statistics.ia_primitives +=
646 r600_query_read_result(map + results_base, 12, 28, false);
647 result->pipeline_statistics.ia_vertices +=
648 r600_query_read_result(map + results_base, 14, 30, false);
649 results_base += query->result_size;
650 }
651 }
652 #if 0 /* for testing */
653 printf("Pipeline stats: IA verts=%llu, IA prims=%llu, VS=%llu, HS=%llu, "
654 "DS=%llu, GS=%llu, GS prims=%llu, Clipper=%llu, "
655 "Clipper prims=%llu, PS=%llu, CS=%llu\n",
656 result->pipeline_statistics.ia_vertices,
657 result->pipeline_statistics.ia_primitives,
658 result->pipeline_statistics.vs_invocations,
659 result->pipeline_statistics.hs_invocations,
660 result->pipeline_statistics.ds_invocations,
661 result->pipeline_statistics.gs_invocations,
662 result->pipeline_statistics.gs_primitives,
663 result->pipeline_statistics.c_invocations,
664 result->pipeline_statistics.c_primitives,
665 result->pipeline_statistics.ps_invocations,
666 result->pipeline_statistics.cs_invocations);
667 #endif
668 break;
669 default:
670 assert(0);
671 }
672
673 ctx->ws->buffer_unmap(qbuf->buf->cs_buf);
674 return TRUE;
675 }
676
677 static boolean r600_get_query_result(struct pipe_context *ctx,
678 struct pipe_query *query,
679 boolean wait, union pipe_query_result *result)
680 {
681 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
682 struct r600_query *rquery = (struct r600_query *)query;
683 struct r600_query_buffer *qbuf;
684
685 util_query_clear_result(result, rquery->type);
686
687 for (qbuf = &rquery->buffer; qbuf; qbuf = qbuf->previous) {
688 if (!r600_get_query_buffer_result(rctx, rquery, qbuf, wait, result)) {
689 return FALSE;
690 }
691 }
692
693 /* Convert the time to expected units. */
694 if (rquery->type == PIPE_QUERY_TIME_ELAPSED ||
695 rquery->type == PIPE_QUERY_TIMESTAMP) {
696 result->u64 = (1000000 * result->u64) / rctx->screen->info.r600_clock_crystal_freq;
697 }
698 return TRUE;
699 }
700
701 static void r600_render_condition(struct pipe_context *ctx,
702 struct pipe_query *query,
703 boolean condition,
704 uint mode)
705 {
706 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
707 struct r600_query *rquery = (struct r600_query *)query;
708 bool wait_flag = false;
709
710 rctx->current_render_cond = query;
711 rctx->current_render_cond_cond = condition;
712 rctx->current_render_cond_mode = mode;
713
714 if (query == NULL) {
715 if (rctx->predicate_drawing) {
716 rctx->predicate_drawing = false;
717 r600_emit_query_predication(rctx, NULL, PREDICATION_OP_CLEAR, false);
718 }
719 return;
720 }
721
722 if (mode == PIPE_RENDER_COND_WAIT ||
723 mode == PIPE_RENDER_COND_BY_REGION_WAIT) {
724 wait_flag = true;
725 }
726
727 rctx->predicate_drawing = true;
728
729 switch (rquery->type) {
730 case PIPE_QUERY_OCCLUSION_COUNTER:
731 case PIPE_QUERY_OCCLUSION_PREDICATE:
732 r600_emit_query_predication(rctx, rquery, PREDICATION_OP_ZPASS, wait_flag);
733 break;
734 case PIPE_QUERY_PRIMITIVES_EMITTED:
735 case PIPE_QUERY_PRIMITIVES_GENERATED:
736 case PIPE_QUERY_SO_STATISTICS:
737 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
738 r600_emit_query_predication(rctx, rquery, PREDICATION_OP_PRIMCOUNT, wait_flag);
739 break;
740 default:
741 assert(0);
742 }
743 }
744
745 void r600_suspend_nontimer_queries(struct r600_common_context *ctx)
746 {
747 struct r600_query *query;
748
749 LIST_FOR_EACH_ENTRY(query, &ctx->active_nontimer_queries, list) {
750 r600_emit_query_end(ctx, query);
751 }
752 assert(ctx->num_cs_dw_nontimer_queries_suspend == 0);
753 }
754
755 void r600_resume_nontimer_queries(struct r600_common_context *ctx)
756 {
757 struct r600_query *query;
758
759 assert(ctx->num_cs_dw_nontimer_queries_suspend == 0);
760
761 LIST_FOR_EACH_ENTRY(query, &ctx->active_nontimer_queries, list) {
762 r600_emit_query_begin(ctx, query);
763 }
764 }
765
766 /* Get backends mask */
767 void r600_query_init_backend_mask(struct r600_common_context *ctx)
768 {
769 struct radeon_winsys_cs *cs = ctx->rings.gfx.cs;
770 struct r600_resource *buffer;
771 uint32_t *results;
772 unsigned num_backends = ctx->screen->info.r600_num_backends;
773 unsigned i, mask = 0;
774 uint64_t va;
775
776 /* if backend_map query is supported by the kernel */
777 if (ctx->screen->info.r600_backend_map_valid) {
778 unsigned num_tile_pipes = ctx->screen->info.r600_num_tile_pipes;
779 unsigned backend_map = ctx->screen->info.r600_backend_map;
780 unsigned item_width, item_mask;
781
782 if (ctx->chip_class >= EVERGREEN) {
783 item_width = 4;
784 item_mask = 0x7;
785 } else {
786 item_width = 2;
787 item_mask = 0x3;
788 }
789
790 while(num_tile_pipes--) {
791 i = backend_map & item_mask;
792 mask |= (1<<i);
793 backend_map >>= item_width;
794 }
795 if (mask != 0) {
796 ctx->backend_mask = mask;
797 return;
798 }
799 }
800
801 /* otherwise backup path for older kernels */
802
803 /* create buffer for event data */
804 buffer = (struct r600_resource*)
805 pipe_buffer_create(ctx->b.screen, PIPE_BIND_CUSTOM,
806 PIPE_USAGE_STAGING, ctx->max_db*16);
807 if (!buffer)
808 goto err;
809 va = r600_resource_va(ctx->b.screen, (void*)buffer);
810
811 /* initialize buffer with zeroes */
812 results = r600_buffer_map_sync_with_rings(ctx, buffer, PIPE_TRANSFER_WRITE);
813 if (results) {
814 memset(results, 0, ctx->max_db * 4 * 4);
815 ctx->ws->buffer_unmap(buffer->cs_buf);
816
817 /* emit EVENT_WRITE for ZPASS_DONE */
818 cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE, 2, 0);
819 cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_ZPASS_DONE) | EVENT_INDEX(1);
820 cs->buf[cs->cdw++] = va;
821 cs->buf[cs->cdw++] = (va >> 32UL) & 0xFF;
822
823 cs->buf[cs->cdw++] = PKT3(PKT3_NOP, 0, 0);
824 cs->buf[cs->cdw++] = r600_context_bo_reloc(ctx, &ctx->rings.gfx, buffer, RADEON_USAGE_WRITE);
825
826 /* analyze results */
827 results = r600_buffer_map_sync_with_rings(ctx, buffer, PIPE_TRANSFER_READ);
828 if (results) {
829 for(i = 0; i < ctx->max_db; i++) {
830 /* at least highest bit will be set if backend is used */
831 if (results[i*4 + 1])
832 mask |= (1<<i);
833 }
834 ctx->ws->buffer_unmap(buffer->cs_buf);
835 }
836 }
837
838 pipe_resource_reference((struct pipe_resource**)&buffer, NULL);
839
840 if (mask != 0) {
841 ctx->backend_mask = mask;
842 return;
843 }
844
845 err:
846 /* fallback to old method - set num_backends lower bits to 1 */
847 ctx->backend_mask = (~((uint32_t)0))>>(32-num_backends);
848 return;
849 }
850
851 void r600_query_init(struct r600_common_context *rctx)
852 {
853 rctx->b.create_query = r600_create_query;
854 rctx->b.destroy_query = r600_destroy_query;
855 rctx->b.begin_query = r600_begin_query;
856 rctx->b.end_query = r600_end_query;
857 rctx->b.get_query_result = r600_get_query_result;
858
859 if (((struct r600_common_screen*)rctx->b.screen)->info.r600_num_backends > 0)
860 rctx->b.render_condition = r600_render_condition;
861
862 LIST_INITHEAD(&rctx->active_nontimer_queries);
863 }