2 * Copyright 2011 Christoph Bumiller
3 * Copyright 2015 Samuel Pitoiset
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 #define NVC0_PUSH_EXPLICIT_SPACE_CHECKING
26 #include "nvc0/nvc0_context.h"
27 #include "nvc0/nvc0_query_hw.h"
28 #include "nvc0/nvc0_query_hw_metric.h"
29 #include "nvc0/nvc0_query_hw_sm.h"
31 #define NVC0_HW_QUERY_STATE_READY 0
32 #define NVC0_HW_QUERY_STATE_ACTIVE 1
33 #define NVC0_HW_QUERY_STATE_ENDED 2
34 #define NVC0_HW_QUERY_STATE_FLUSHED 3
36 #define NVC0_HW_QUERY_ALLOC_SPACE 256
39 nvc0_hw_query_allocate(struct nvc0_context
*nvc0
, struct nvc0_query
*q
,
42 struct nvc0_hw_query
*hq
= nvc0_hw_query(q
);
43 struct nvc0_screen
*screen
= nvc0
->screen
;
47 nouveau_bo_ref(NULL
, &hq
->bo
);
49 if (hq
->state
== NVC0_HW_QUERY_STATE_READY
)
50 nouveau_mm_free(hq
->mm
);
52 nouveau_fence_work(screen
->base
.fence
.current
,
53 nouveau_mm_free_work
, hq
->mm
);
57 hq
->mm
= nouveau_mm_allocate(screen
->base
.mm_GART
, size
, &hq
->bo
,
61 hq
->offset
= hq
->base_offset
;
63 ret
= nouveau_bo_map(hq
->bo
, 0, screen
->base
.client
);
65 nvc0_hw_query_allocate(nvc0
, q
, 0);
68 hq
->data
= (uint32_t *)((uint8_t *)hq
->bo
->map
+ hq
->base_offset
);
74 nvc0_hw_query_get(struct nouveau_pushbuf
*push
, struct nvc0_query
*q
,
75 unsigned offset
, uint32_t get
)
77 struct nvc0_hw_query
*hq
= nvc0_hw_query(q
);
82 PUSH_REFN (push
, hq
->bo
, NOUVEAU_BO_GART
| NOUVEAU_BO_WR
);
83 BEGIN_NVC0(push
, NVC0_3D(QUERY_ADDRESS_HIGH
), 4);
84 PUSH_DATAh(push
, hq
->bo
->offset
+ offset
);
85 PUSH_DATA (push
, hq
->bo
->offset
+ offset
);
86 PUSH_DATA (push
, hq
->sequence
);
87 PUSH_DATA (push
, get
);
91 nvc0_hw_query_rotate(struct nvc0_context
*nvc0
, struct nvc0_query
*q
)
93 struct nvc0_hw_query
*hq
= nvc0_hw_query(q
);
95 hq
->offset
+= hq
->rotate
;
96 hq
->data
+= hq
->rotate
/ sizeof(*hq
->data
);
97 if (hq
->offset
- hq
->base_offset
== NVC0_HW_QUERY_ALLOC_SPACE
)
98 nvc0_hw_query_allocate(nvc0
, q
, NVC0_HW_QUERY_ALLOC_SPACE
);
102 nvc0_hw_query_update(struct nouveau_client
*cli
, struct nvc0_query
*q
)
104 struct nvc0_hw_query
*hq
= nvc0_hw_query(q
);
107 if (nouveau_fence_signalled(hq
->fence
))
108 hq
->state
= NVC0_HW_QUERY_STATE_READY
;
110 if (hq
->data
[0] == hq
->sequence
)
111 hq
->state
= NVC0_HW_QUERY_STATE_READY
;
116 nvc0_hw_destroy_query(struct nvc0_context
*nvc0
, struct nvc0_query
*q
)
118 struct nvc0_hw_query
*hq
= nvc0_hw_query(q
);
120 if (hq
->funcs
&& hq
->funcs
->destroy_query
) {
121 hq
->funcs
->destroy_query(nvc0
, hq
);
125 nvc0_hw_query_allocate(nvc0
, q
, 0);
126 nouveau_fence_ref(NULL
, &hq
->fence
);
131 nvc0_hw_begin_query(struct nvc0_context
*nvc0
, struct nvc0_query
*q
)
133 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
134 struct nvc0_hw_query
*hq
= nvc0_hw_query(q
);
137 if (hq
->funcs
&& hq
->funcs
->begin_query
)
138 return hq
->funcs
->begin_query(nvc0
, hq
);
140 /* For occlusion queries we have to change the storage, because a previous
141 * query might set the initial render conition to false even *after* we re-
142 * initialized it to true.
145 nvc0_hw_query_rotate(nvc0
, q
);
147 /* XXX: can we do this with the GPU, and sync with respect to a previous
150 hq
->data
[0] = hq
->sequence
; /* initialize sequence */
151 hq
->data
[1] = 1; /* initial render condition = true */
152 hq
->data
[4] = hq
->sequence
+ 1; /* for comparison COND_MODE */
158 case PIPE_QUERY_OCCLUSION_COUNTER
:
159 case PIPE_QUERY_OCCLUSION_PREDICATE
:
160 hq
->nesting
= nvc0
->screen
->num_occlusion_queries_active
++;
162 nvc0_hw_query_get(push
, q
, 0x10, 0x0100f002);
165 BEGIN_NVC0(push
, NVC0_3D(COUNTER_RESET
), 1);
166 PUSH_DATA (push
, NVC0_3D_COUNTER_RESET_SAMPLECNT
);
167 IMMED_NVC0(push
, NVC0_3D(SAMPLECNT_ENABLE
), 1);
170 case PIPE_QUERY_PRIMITIVES_GENERATED
:
171 nvc0_hw_query_get(push
, q
, 0x10, 0x09005002 | (q
->index
<< 5));
173 case PIPE_QUERY_PRIMITIVES_EMITTED
:
174 nvc0_hw_query_get(push
, q
, 0x10, 0x05805002 | (q
->index
<< 5));
176 case PIPE_QUERY_SO_STATISTICS
:
177 nvc0_hw_query_get(push
, q
, 0x20, 0x05805002 | (q
->index
<< 5));
178 nvc0_hw_query_get(push
, q
, 0x30, 0x06805002 | (q
->index
<< 5));
180 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
181 nvc0_hw_query_get(push
, q
, 0x10, 0x03005002 | (q
->index
<< 5));
183 case PIPE_QUERY_TIME_ELAPSED
:
184 nvc0_hw_query_get(push
, q
, 0x10, 0x00005002);
186 case PIPE_QUERY_PIPELINE_STATISTICS
:
187 nvc0_hw_query_get(push
, q
, 0xc0 + 0x00, 0x00801002); /* VFETCH, VERTICES */
188 nvc0_hw_query_get(push
, q
, 0xc0 + 0x10, 0x01801002); /* VFETCH, PRIMS */
189 nvc0_hw_query_get(push
, q
, 0xc0 + 0x20, 0x02802002); /* VP, LAUNCHES */
190 nvc0_hw_query_get(push
, q
, 0xc0 + 0x30, 0x03806002); /* GP, LAUNCHES */
191 nvc0_hw_query_get(push
, q
, 0xc0 + 0x40, 0x04806002); /* GP, PRIMS_OUT */
192 nvc0_hw_query_get(push
, q
, 0xc0 + 0x50, 0x07804002); /* RAST, PRIMS_IN */
193 nvc0_hw_query_get(push
, q
, 0xc0 + 0x60, 0x08804002); /* RAST, PRIMS_OUT */
194 nvc0_hw_query_get(push
, q
, 0xc0 + 0x70, 0x0980a002); /* ROP, PIXELS */
195 nvc0_hw_query_get(push
, q
, 0xc0 + 0x80, 0x0d808002); /* TCP, LAUNCHES */
196 nvc0_hw_query_get(push
, q
, 0xc0 + 0x90, 0x0e809002); /* TEP, LAUNCHES */
201 hq
->state
= NVC0_HW_QUERY_STATE_ACTIVE
;
206 nvc0_hw_end_query(struct nvc0_context
*nvc0
, struct nvc0_query
*q
)
208 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
209 struct nvc0_hw_query
*hq
= nvc0_hw_query(q
);
211 if (hq
->funcs
&& hq
->funcs
->end_query
) {
212 hq
->funcs
->end_query(nvc0
, hq
);
216 if (hq
->state
!= NVC0_HW_QUERY_STATE_ACTIVE
) {
217 /* some queries don't require 'begin' to be called (e.g. GPU_FINISHED) */
219 nvc0_hw_query_rotate(nvc0
, q
);
222 hq
->state
= NVC0_HW_QUERY_STATE_ENDED
;
225 case PIPE_QUERY_OCCLUSION_COUNTER
:
226 case PIPE_QUERY_OCCLUSION_PREDICATE
:
227 nvc0_hw_query_get(push
, q
, 0, 0x0100f002);
228 if (--nvc0
->screen
->num_occlusion_queries_active
== 0) {
230 IMMED_NVC0(push
, NVC0_3D(SAMPLECNT_ENABLE
), 0);
233 case PIPE_QUERY_PRIMITIVES_GENERATED
:
234 nvc0_hw_query_get(push
, q
, 0, 0x09005002 | (q
->index
<< 5));
236 case PIPE_QUERY_PRIMITIVES_EMITTED
:
237 nvc0_hw_query_get(push
, q
, 0, 0x05805002 | (q
->index
<< 5));
239 case PIPE_QUERY_SO_STATISTICS
:
240 nvc0_hw_query_get(push
, q
, 0x00, 0x05805002 | (q
->index
<< 5));
241 nvc0_hw_query_get(push
, q
, 0x10, 0x06805002 | (q
->index
<< 5));
243 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
244 /* TODO: How do we sum over all streams for render condition ? */
245 /* PRIMS_DROPPED doesn't write sequence, use a ZERO query to sync on */
246 nvc0_hw_query_get(push
, q
, 0x00, 0x03005002 | (q
->index
<< 5));
247 nvc0_hw_query_get(push
, q
, 0x20, 0x00005002);
249 case PIPE_QUERY_TIMESTAMP
:
250 case PIPE_QUERY_TIME_ELAPSED
:
251 nvc0_hw_query_get(push
, q
, 0, 0x00005002);
253 case PIPE_QUERY_GPU_FINISHED
:
254 nvc0_hw_query_get(push
, q
, 0, 0x1000f010);
256 case PIPE_QUERY_PIPELINE_STATISTICS
:
257 nvc0_hw_query_get(push
, q
, 0x00, 0x00801002); /* VFETCH, VERTICES */
258 nvc0_hw_query_get(push
, q
, 0x10, 0x01801002); /* VFETCH, PRIMS */
259 nvc0_hw_query_get(push
, q
, 0x20, 0x02802002); /* VP, LAUNCHES */
260 nvc0_hw_query_get(push
, q
, 0x30, 0x03806002); /* GP, LAUNCHES */
261 nvc0_hw_query_get(push
, q
, 0x40, 0x04806002); /* GP, PRIMS_OUT */
262 nvc0_hw_query_get(push
, q
, 0x50, 0x07804002); /* RAST, PRIMS_IN */
263 nvc0_hw_query_get(push
, q
, 0x60, 0x08804002); /* RAST, PRIMS_OUT */
264 nvc0_hw_query_get(push
, q
, 0x70, 0x0980a002); /* ROP, PIXELS */
265 nvc0_hw_query_get(push
, q
, 0x80, 0x0d808002); /* TCP, LAUNCHES */
266 nvc0_hw_query_get(push
, q
, 0x90, 0x0e809002); /* TEP, LAUNCHES */
268 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
269 /* This query is not issued on GPU because disjoint is forced to false */
270 hq
->state
= NVC0_HW_QUERY_STATE_READY
;
272 case NVC0_HW_QUERY_TFB_BUFFER_OFFSET
:
273 /* indexed by TFB buffer instead of by vertex stream */
274 nvc0_hw_query_get(push
, q
, 0x00, 0x0d005002 | (q
->index
<< 5));
280 nouveau_fence_ref(nvc0
->screen
->base
.fence
.current
, &hq
->fence
);
284 nvc0_hw_get_query_result(struct nvc0_context
*nvc0
, struct nvc0_query
*q
,
285 boolean wait
, union pipe_query_result
*result
)
287 struct nvc0_hw_query
*hq
= nvc0_hw_query(q
);
288 uint64_t *res64
= (uint64_t*)result
;
289 uint32_t *res32
= (uint32_t*)result
;
290 uint8_t *res8
= (uint8_t*)result
;
291 uint64_t *data64
= (uint64_t *)hq
->data
;
294 if (hq
->funcs
&& hq
->funcs
->get_query_result
)
295 return hq
->funcs
->get_query_result(nvc0
, hq
, wait
, result
);
297 if (hq
->state
!= NVC0_HW_QUERY_STATE_READY
)
298 nvc0_hw_query_update(nvc0
->screen
->base
.client
, q
);
300 if (hq
->state
!= NVC0_HW_QUERY_STATE_READY
) {
302 if (hq
->state
!= NVC0_HW_QUERY_STATE_FLUSHED
) {
303 hq
->state
= NVC0_HW_QUERY_STATE_FLUSHED
;
304 /* flush for silly apps that spin on GL_QUERY_RESULT_AVAILABLE */
305 PUSH_KICK(nvc0
->base
.pushbuf
);
309 if (nouveau_bo_wait(hq
->bo
, NOUVEAU_BO_RD
, nvc0
->screen
->base
.client
))
311 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, query_sync_count
, 1);
313 hq
->state
= NVC0_HW_QUERY_STATE_READY
;
316 case PIPE_QUERY_GPU_FINISHED
:
319 case PIPE_QUERY_OCCLUSION_COUNTER
: /* u32 sequence, u32 count, u64 time */
320 res64
[0] = hq
->data
[1] - hq
->data
[5];
322 case PIPE_QUERY_OCCLUSION_PREDICATE
:
323 res8
[0] = hq
->data
[1] != hq
->data
[5];
325 case PIPE_QUERY_PRIMITIVES_GENERATED
: /* u64 count, u64 time */
326 case PIPE_QUERY_PRIMITIVES_EMITTED
: /* u64 count, u64 time */
327 res64
[0] = data64
[0] - data64
[2];
329 case PIPE_QUERY_SO_STATISTICS
:
330 res64
[0] = data64
[0] - data64
[4];
331 res64
[1] = data64
[2] - data64
[6];
333 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
334 res8
[0] = data64
[0] != data64
[2];
336 case PIPE_QUERY_TIMESTAMP
:
337 res64
[0] = data64
[1];
339 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
340 res64
[0] = 1000000000;
343 case PIPE_QUERY_TIME_ELAPSED
:
344 res64
[0] = data64
[1] - data64
[3];
346 case PIPE_QUERY_PIPELINE_STATISTICS
:
347 for (i
= 0; i
< 10; ++i
)
348 res64
[i
] = data64
[i
* 2] - data64
[24 + i
* 2];
350 case NVC0_HW_QUERY_TFB_BUFFER_OFFSET
:
351 res32
[0] = hq
->data
[1];
354 assert(0); /* can't happen, we don't create queries with invalid type */
361 static const struct nvc0_query_funcs hw_query_funcs
= {
362 .destroy_query
= nvc0_hw_destroy_query
,
363 .begin_query
= nvc0_hw_begin_query
,
364 .end_query
= nvc0_hw_end_query
,
365 .get_query_result
= nvc0_hw_get_query_result
,
369 nvc0_hw_create_query(struct nvc0_context
*nvc0
, unsigned type
, unsigned index
)
371 struct nvc0_hw_query
*hq
;
372 struct nvc0_query
*q
;
373 unsigned space
= NVC0_HW_QUERY_ALLOC_SPACE
;
375 hq
= nvc0_hw_sm_create_query(nvc0
, type
);
377 hq
->base
.funcs
= &hw_query_funcs
;
378 return (struct nvc0_query
*)hq
;
381 hq
= nvc0_hw_metric_create_query(nvc0
, type
);
383 hq
->base
.funcs
= &hw_query_funcs
;
384 return (struct nvc0_query
*)hq
;
387 hq
= CALLOC_STRUCT(nvc0_hw_query
);
392 q
->funcs
= &hw_query_funcs
;
396 case PIPE_QUERY_OCCLUSION_COUNTER
:
397 case PIPE_QUERY_OCCLUSION_PREDICATE
:
399 space
= NVC0_HW_QUERY_ALLOC_SPACE
;
401 case PIPE_QUERY_PIPELINE_STATISTICS
:
405 case PIPE_QUERY_SO_STATISTICS
:
406 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
410 case PIPE_QUERY_PRIMITIVES_GENERATED
:
411 case PIPE_QUERY_PRIMITIVES_EMITTED
:
416 case PIPE_QUERY_TIME_ELAPSED
:
417 case PIPE_QUERY_TIMESTAMP
:
418 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
419 case PIPE_QUERY_GPU_FINISHED
:
422 case NVC0_HW_QUERY_TFB_BUFFER_OFFSET
:
426 debug_printf("invalid query type: %u\n", type
);
431 if (!nvc0_hw_query_allocate(nvc0
, q
, space
)) {
437 /* we advance before query_begin ! */
438 hq
->offset
-= hq
->rotate
;
439 hq
->data
-= hq
->rotate
/ sizeof(*hq
->data
);
442 hq
->data
[0] = 0; /* initialize sequence */
448 nvc0_hw_get_driver_query_info(struct nvc0_screen
*screen
, unsigned id
,
449 struct pipe_driver_query_info
*info
)
451 int num_hw_sm_queries
= 0, num_hw_metric_queries
= 0;
453 num_hw_sm_queries
= nvc0_hw_sm_get_driver_query_info(screen
, 0, NULL
);
454 num_hw_metric_queries
=
455 nvc0_hw_metric_get_driver_query_info(screen
, 0, NULL
);
458 return num_hw_sm_queries
+ num_hw_metric_queries
;
460 if (id
< num_hw_sm_queries
)
461 return nvc0_hw_sm_get_driver_query_info(screen
, id
, info
);
463 return nvc0_hw_metric_get_driver_query_info(screen
,
464 id
- num_hw_sm_queries
, info
);
468 nvc0_hw_query_pushbuf_submit(struct nouveau_pushbuf
*push
,
469 struct nvc0_query
*q
, unsigned result_offset
)
471 struct nvc0_hw_query
*hq
= nvc0_hw_query(q
);
473 PUSH_REFN(push
, hq
->bo
, NOUVEAU_BO_RD
| NOUVEAU_BO_GART
);
474 nouveau_pushbuf_data(push
, hq
->bo
, hq
->offset
+ result_offset
, 4 |
475 NVC0_IB_ENTRY_1_NO_PREFETCH
);
479 nvc0_hw_query_fifo_wait(struct nouveau_pushbuf
*push
, struct nvc0_query
*q
)
481 struct nvc0_hw_query
*hq
= nvc0_hw_query(q
);
482 unsigned offset
= hq
->offset
;
484 if (q
->type
== PIPE_QUERY_SO_OVERFLOW_PREDICATE
) offset
+= 0x20;
487 PUSH_REFN (push
, hq
->bo
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
);
488 BEGIN_NVC0(push
, SUBC_3D(NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH
), 4);
489 PUSH_DATAh(push
, hq
->bo
->offset
+ offset
);
490 PUSH_DATA (push
, hq
->bo
->offset
+ offset
);
491 PUSH_DATA (push
, hq
->sequence
);
492 PUSH_DATA (push
, (1 << 12) |
493 NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL
);