2 * Copyright 2011 Nouveau Project
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Authors: Christoph Bumiller
25 #define NV50_PUSH_EXPLICIT_SPACE_CHECKING
27 #include "nv50/nv50_context.h"
28 #include "nv50/nv50_query.h"
29 #include "nv_object.xml.h"
31 #define NV50_QUERY_STATE_READY 0
32 #define NV50_QUERY_STATE_ACTIVE 1
33 #define NV50_QUERY_STATE_ENDED 2
34 #define NV50_QUERY_STATE_FLUSHED 3
36 /* XXX: Nested queries, and simultaneous queries on multiple gallium contexts
37 * (since we use only a single GPU channel per screen) will not work properly.
39 * The first is not that big of an issue because OpenGL does not allow nested
43 #define NV50_QUERY_ALLOC_SPACE 256
46 nv50_query_allocate(struct nv50_context
*nv50
, struct nv50_query
*q
, int size
)
48 struct nv50_screen
*screen
= nv50
->screen
;
52 nouveau_bo_ref(NULL
, &q
->bo
);
54 if (q
->state
== NV50_QUERY_STATE_READY
)
55 nouveau_mm_free(q
->mm
);
57 nouveau_fence_work(screen
->base
.fence
.current
, nouveau_mm_free_work
,
62 q
->mm
= nouveau_mm_allocate(screen
->base
.mm_GART
, size
, &q
->bo
, &q
->base
);
67 ret
= nouveau_bo_map(q
->bo
, 0, screen
->base
.client
);
69 nv50_query_allocate(nv50
, q
, 0);
72 q
->data
= (uint32_t *)((uint8_t *)q
->bo
->map
+ q
->base
);
78 nv50_query_destroy(struct pipe_context
*pipe
, struct pipe_query
*pq
)
80 nv50_query_allocate(nv50_context(pipe
), nv50_query(pq
), 0);
81 nouveau_fence_ref(NULL
, &nv50_query(pq
)->fence
);
85 static struct pipe_query
*
86 nv50_query_create(struct pipe_context
*pipe
, unsigned type
, unsigned index
)
88 struct nv50_context
*nv50
= nv50_context(pipe
);
91 q
= CALLOC_STRUCT(nv50_query
);
95 if (!nv50_query_allocate(nv50
, q
, NV50_QUERY_ALLOC_SPACE
)) {
100 q
->is64bit
= (type
== PIPE_QUERY_PRIMITIVES_GENERATED
||
101 type
== PIPE_QUERY_PRIMITIVES_EMITTED
||
102 type
== PIPE_QUERY_SO_STATISTICS
||
103 type
== PIPE_QUERY_PIPELINE_STATISTICS
);
106 if (q
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
108 q
->data
-= 32 / sizeof(*q
->data
); /* we advance before query_begin ! */
111 return (struct pipe_query
*)q
;
115 nv50_query_get(struct nouveau_pushbuf
*push
, struct nv50_query
*q
,
116 unsigned offset
, uint32_t get
)
121 PUSH_REFN (push
, q
->bo
, NOUVEAU_BO_GART
| NOUVEAU_BO_WR
);
122 BEGIN_NV04(push
, NV50_3D(QUERY_ADDRESS_HIGH
), 4);
123 PUSH_DATAh(push
, q
->bo
->offset
+ offset
);
124 PUSH_DATA (push
, q
->bo
->offset
+ offset
);
125 PUSH_DATA (push
, q
->sequence
);
126 PUSH_DATA (push
, get
);
130 nv50_query_begin(struct pipe_context
*pipe
, struct pipe_query
*pq
)
132 struct nv50_context
*nv50
= nv50_context(pipe
);
133 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
134 struct nv50_query
*q
= nv50_query(pq
);
136 /* For occlusion queries we have to change the storage, because a previous
137 * query might set the initial render conition to false even *after* we re-
138 * initialized it to true.
140 if (q
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
142 q
->data
+= 32 / sizeof(*q
->data
);
143 if (q
->offset
- q
->base
== NV50_QUERY_ALLOC_SPACE
)
144 nv50_query_allocate(nv50
, q
, NV50_QUERY_ALLOC_SPACE
);
146 /* XXX: can we do this with the GPU, and sync with respect to a previous
149 q
->data
[0] = q
->sequence
; /* initialize sequence */
150 q
->data
[1] = 1; /* initial render condition = true */
151 q
->data
[4] = q
->sequence
+ 1; /* for comparison COND_MODE */
155 q
->data
[0] = q
->sequence
++; /* the previously used one */
158 case PIPE_QUERY_OCCLUSION_COUNTER
:
159 q
->nesting
= nv50
->screen
->num_occlusion_queries_active
++;
161 nv50_query_get(push
, q
, 0x10, 0x0100f002);
164 BEGIN_NV04(push
, NV50_3D(COUNTER_RESET
), 1);
165 PUSH_DATA (push
, NV50_3D_COUNTER_RESET_SAMPLECNT
);
166 BEGIN_NV04(push
, NV50_3D(SAMPLECNT_ENABLE
), 1);
170 case PIPE_QUERY_PRIMITIVES_GENERATED
:
171 nv50_query_get(push
, q
, 0x10, 0x06805002);
173 case PIPE_QUERY_PRIMITIVES_EMITTED
:
174 nv50_query_get(push
, q
, 0x10, 0x05805002);
176 case PIPE_QUERY_SO_STATISTICS
:
177 nv50_query_get(push
, q
, 0x20, 0x05805002);
178 nv50_query_get(push
, q
, 0x30, 0x06805002);
180 case PIPE_QUERY_PIPELINE_STATISTICS
:
181 nv50_query_get(push
, q
, 0x80, 0x00801002); /* VFETCH, VERTICES */
182 nv50_query_get(push
, q
, 0x90, 0x01801002); /* VFETCH, PRIMS */
183 nv50_query_get(push
, q
, 0xa0, 0x02802002); /* VP, LAUNCHES */
184 nv50_query_get(push
, q
, 0xb0, 0x03806002); /* GP, LAUNCHES */
185 nv50_query_get(push
, q
, 0xc0, 0x04806002); /* GP, PRIMS_OUT */
186 nv50_query_get(push
, q
, 0xd0, 0x07804002); /* RAST, PRIMS_IN */
187 nv50_query_get(push
, q
, 0xe0, 0x08804002); /* RAST, PRIMS_OUT */
188 nv50_query_get(push
, q
, 0xf0, 0x0980a002); /* ROP, PIXELS */
190 case PIPE_QUERY_TIME_ELAPSED
:
191 nv50_query_get(push
, q
, 0x10, 0x00005002);
196 q
->state
= NV50_QUERY_STATE_ACTIVE
;
201 nv50_query_end(struct pipe_context
*pipe
, struct pipe_query
*pq
)
203 struct nv50_context
*nv50
= nv50_context(pipe
);
204 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
205 struct nv50_query
*q
= nv50_query(pq
);
207 q
->state
= NV50_QUERY_STATE_ENDED
;
210 case PIPE_QUERY_OCCLUSION_COUNTER
:
211 nv50_query_get(push
, q
, 0, 0x0100f002);
212 if (--nv50
->screen
->num_occlusion_queries_active
== 0) {
214 BEGIN_NV04(push
, NV50_3D(SAMPLECNT_ENABLE
), 1);
218 case PIPE_QUERY_PRIMITIVES_GENERATED
:
219 nv50_query_get(push
, q
, 0, 0x06805002);
221 case PIPE_QUERY_PRIMITIVES_EMITTED
:
222 nv50_query_get(push
, q
, 0, 0x05805002);
224 case PIPE_QUERY_SO_STATISTICS
:
225 nv50_query_get(push
, q
, 0x00, 0x05805002);
226 nv50_query_get(push
, q
, 0x10, 0x06805002);
228 case PIPE_QUERY_PIPELINE_STATISTICS
:
229 nv50_query_get(push
, q
, 0x00, 0x00801002); /* VFETCH, VERTICES */
230 nv50_query_get(push
, q
, 0x10, 0x01801002); /* VFETCH, PRIMS */
231 nv50_query_get(push
, q
, 0x20, 0x02802002); /* VP, LAUNCHES */
232 nv50_query_get(push
, q
, 0x30, 0x03806002); /* GP, LAUNCHES */
233 nv50_query_get(push
, q
, 0x40, 0x04806002); /* GP, PRIMS_OUT */
234 nv50_query_get(push
, q
, 0x50, 0x07804002); /* RAST, PRIMS_IN */
235 nv50_query_get(push
, q
, 0x60, 0x08804002); /* RAST, PRIMS_OUT */
236 nv50_query_get(push
, q
, 0x70, 0x0980a002); /* ROP, PIXELS */
238 case PIPE_QUERY_TIMESTAMP
:
241 case PIPE_QUERY_TIME_ELAPSED
:
242 nv50_query_get(push
, q
, 0, 0x00005002);
244 case PIPE_QUERY_GPU_FINISHED
:
246 nv50_query_get(push
, q
, 0, 0x1000f010);
248 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET
:
250 nv50_query_get(push
, q
, 0, 0x0d005002 | (q
->index
<< 5));
252 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
253 /* This query is not issued on GPU because disjoint is forced to false */
254 q
->state
= NV50_QUERY_STATE_READY
;
262 nouveau_fence_ref(nv50
->screen
->base
.fence
.current
, &q
->fence
);
266 nv50_query_update(struct nv50_query
*q
)
269 if (nouveau_fence_signalled(q
->fence
))
270 q
->state
= NV50_QUERY_STATE_READY
;
272 if (q
->data
[0] == q
->sequence
)
273 q
->state
= NV50_QUERY_STATE_READY
;
278 nv50_query_result(struct pipe_context
*pipe
, struct pipe_query
*pq
,
279 boolean wait
, union pipe_query_result
*result
)
281 struct nv50_context
*nv50
= nv50_context(pipe
);
282 struct nv50_query
*q
= nv50_query(pq
);
283 uint64_t *res64
= (uint64_t *)result
;
284 uint32_t *res32
= (uint32_t *)result
;
285 uint8_t *res8
= (uint8_t *)result
;
286 uint64_t *data64
= (uint64_t *)q
->data
;
289 if (q
->state
!= NV50_QUERY_STATE_READY
)
290 nv50_query_update(q
);
292 if (q
->state
!= NV50_QUERY_STATE_READY
) {
294 /* for broken apps that spin on GL_QUERY_RESULT_AVAILABLE */
295 if (q
->state
!= NV50_QUERY_STATE_FLUSHED
) {
296 q
->state
= NV50_QUERY_STATE_FLUSHED
;
297 PUSH_KICK(nv50
->base
.pushbuf
);
301 if (nouveau_bo_wait(q
->bo
, NOUVEAU_BO_RD
, nv50
->screen
->base
.client
))
304 q
->state
= NV50_QUERY_STATE_READY
;
307 case PIPE_QUERY_GPU_FINISHED
:
310 case PIPE_QUERY_OCCLUSION_COUNTER
: /* u32 sequence, u32 count, u64 time */
311 res64
[0] = q
->data
[1] - q
->data
[5];
313 case PIPE_QUERY_PRIMITIVES_GENERATED
: /* u64 count, u64 time */
314 case PIPE_QUERY_PRIMITIVES_EMITTED
: /* u64 count, u64 time */
315 res64
[0] = data64
[0] - data64
[2];
317 case PIPE_QUERY_SO_STATISTICS
:
318 res64
[0] = data64
[0] - data64
[4];
319 res64
[1] = data64
[2] - data64
[6];
321 case PIPE_QUERY_PIPELINE_STATISTICS
:
322 for (i
= 0; i
< 8; ++i
)
323 res64
[i
] = data64
[i
* 2] - data64
[16 + i
* 2];
325 case PIPE_QUERY_TIMESTAMP
:
326 res64
[0] = data64
[1];
328 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
329 res64
[0] = 1000000000;
332 case PIPE_QUERY_TIME_ELAPSED
:
333 res64
[0] = data64
[1] - data64
[3];
335 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET
:
336 res32
[0] = q
->data
[1];
346 nv84_query_fifo_wait(struct nouveau_pushbuf
*push
, struct nv50_query
*q
)
348 unsigned offset
= q
->offset
;
351 PUSH_REFN (push
, q
->bo
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
);
352 BEGIN_NV04(push
, SUBC_3D(NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH
), 4);
353 PUSH_DATAh(push
, q
->bo
->offset
+ offset
);
354 PUSH_DATA (push
, q
->bo
->offset
+ offset
);
355 PUSH_DATA (push
, q
->sequence
);
356 PUSH_DATA (push
, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL
);
360 nv50_render_condition(struct pipe_context
*pipe
,
361 struct pipe_query
*pq
,
362 boolean condition
, uint mode
)
364 struct nv50_context
*nv50
= nv50_context(pipe
);
365 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
366 struct nv50_query
*q
;
369 mode
!= PIPE_RENDER_COND_NO_WAIT
&&
370 mode
!= PIPE_RENDER_COND_BY_REGION_NO_WAIT
;
373 cond
= NV50_3D_COND_MODE_ALWAYS
;
377 /* NOTE: comparison of 2 queries only works if both have completed */
379 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
380 cond
= condition
? NV50_3D_COND_MODE_EQUAL
:
381 NV50_3D_COND_MODE_NOT_EQUAL
;
384 case PIPE_QUERY_OCCLUSION_COUNTER
:
385 case PIPE_QUERY_OCCLUSION_PREDICATE
:
386 if (likely(!condition
)) {
387 if (unlikely(q
->nesting
))
388 cond
= wait
? NV50_3D_COND_MODE_NOT_EQUAL
:
389 NV50_3D_COND_MODE_ALWAYS
;
391 cond
= NV50_3D_COND_MODE_RES_NON_ZERO
;
393 cond
= wait
? NV50_3D_COND_MODE_EQUAL
: NV50_3D_COND_MODE_ALWAYS
;
397 assert(!"render condition query not a predicate");
398 cond
= NV50_3D_COND_MODE_ALWAYS
;
403 nv50
->cond_query
= pq
;
404 nv50
->cond_cond
= condition
;
405 nv50
->cond_condmode
= cond
;
406 nv50
->cond_mode
= mode
;
410 BEGIN_NV04(push
, NV50_3D(COND_MODE
), 1);
411 PUSH_DATA (push
, cond
);
418 BEGIN_NV04(push
, SUBC_3D(NV50_GRAPH_SERIALIZE
), 1);
422 PUSH_REFN (push
, q
->bo
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
);
423 BEGIN_NV04(push
, NV50_3D(COND_ADDRESS_HIGH
), 3);
424 PUSH_DATAh(push
, q
->bo
->offset
+ q
->offset
);
425 PUSH_DATA (push
, q
->bo
->offset
+ q
->offset
);
426 PUSH_DATA (push
, cond
);
428 BEGIN_NV04(push
, NV50_2D(COND_ADDRESS_HIGH
), 2);
429 PUSH_DATAh(push
, q
->bo
->offset
+ q
->offset
);
430 PUSH_DATA (push
, q
->bo
->offset
+ q
->offset
);
434 nv50_query_pushbuf_submit(struct nouveau_pushbuf
*push
, uint16_t method
,
435 struct nv50_query
*q
, unsigned result_offset
)
437 nv50_query_update(q
);
438 if (q
->state
!= NV50_QUERY_STATE_READY
)
439 nouveau_bo_wait(q
->bo
, NOUVEAU_BO_RD
, push
->client
);
440 q
->state
= NV50_QUERY_STATE_READY
;
442 BEGIN_NV04(push
, SUBC_3D(method
), 1);
443 PUSH_DATA (push
, q
->data
[result_offset
/ 4]);
447 nv50_init_query_functions(struct nv50_context
*nv50
)
449 struct pipe_context
*pipe
= &nv50
->base
.pipe
;
451 pipe
->create_query
= nv50_query_create
;
452 pipe
->destroy_query
= nv50_query_destroy
;
453 pipe
->begin_query
= nv50_query_begin
;
454 pipe
->end_query
= nv50_query_end
;
455 pipe
->get_query_result
= nv50_query_result
;
456 pipe
->render_condition
= nv50_render_condition
;