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 "nv_object.xml.h"
30 /* XXX: Nested queries, and simultaneous queries on multiple gallium contexts
31 * (since we use only a single GPU channel per screen) will not work properly.
33 * The first is not that big of an issue because OpenGL does not allow nested
42 struct nouveau_bo *bo;
44 uint32_t offset; /* base + i * 32 */
48 struct nouveau_mm_allocation *mm;
49 struct nouveau_fence *fence;
52 #define NV50_QUERY_ALLOC_SPACE 256
54 static INLINE struct nv50_query *
55 nv50_query(struct pipe_query *pipe)
57 return (struct nv50_query *)pipe;
61 nv50_query_allocate(struct nv50_context *nv50, struct nv50_query *q, int size)
63 struct nv50_screen *screen = nv50->screen;
67 nouveau_bo_ref(NULL, &q->bo);
70 nouveau_mm_free(q->mm);
72 nouveau_fence_work(screen->base.fence.current, nouveau_mm_free_work,
77 q->mm = nouveau_mm_allocate(screen->base.mm_GART, size, &q->bo, &q->base);
82 ret = nouveau_bo_map(q->bo, 0, screen->base.client);
84 nv50_query_allocate(nv50, q, 0);
87 q->data = (uint32_t *)((uint8_t *)q->bo->map + q->base);
93 nv50_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
95 nv50_query_allocate(nv50_context(pipe), nv50_query(pq), 0);
96 nouveau_fence_ref(NULL, &nv50_query(pq)->fence);
100 static struct pipe_query *
101 nv50_query_create(struct pipe_context *pipe, unsigned type, unsigned index)
103 struct nv50_context *nv50 = nv50_context(pipe);
104 struct nv50_query *q;
106 q = CALLOC_STRUCT(nv50_query);
110 if (!nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE)) {
115 q->is64bit = (type == PIPE_QUERY_PRIMITIVES_GENERATED ||
116 type == PIPE_QUERY_PRIMITIVES_EMITTED ||
117 type == PIPE_QUERY_SO_STATISTICS ||
118 type == PIPE_QUERY_PIPELINE_STATISTICS);
121 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
123 q->data -= 32 / sizeof(*q->data); /* we advance before query_begin ! */
126 return (struct pipe_query *)q;
130 nv50_query_get(struct nouveau_pushbuf *push, struct nv50_query *q,
131 unsigned offset, uint32_t get)
136 PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_WR);
137 BEGIN_NV04(push, NV50_3D(QUERY_ADDRESS_HIGH), 4);
138 PUSH_DATAh(push, q->bo->offset + offset);
139 PUSH_DATA (push, q->bo->offset + offset);
140 PUSH_DATA (push, q->sequence);
141 PUSH_DATA (push, get);
145 nv50_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
147 struct nv50_context *nv50 = nv50_context(pipe);
148 struct nouveau_pushbuf *push = nv50->base.pushbuf;
149 struct nv50_query *q = nv50_query(pq);
151 /* For occlusion queries we have to change the storage, because a previous
152 * query might set the initial render conition to FALSE even *after* we re-
153 * initialized it to TRUE.
155 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
157 q->data += 32 / sizeof(*q->data);
158 if (q->offset - q->base == NV50_QUERY_ALLOC_SPACE)
159 nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE);
161 /* XXX: can we do this with the GPU, and sync with respect to a previous
164 q->data[0] = q->sequence; /* initialize sequence */
165 q->data[1] = 1; /* initial render condition = TRUE */
166 q->data[4] = q->sequence + 1; /* for comparison COND_MODE */
170 q->data[0] = q->sequence++; /* the previously used one */
173 case PIPE_QUERY_OCCLUSION_COUNTER:
175 BEGIN_NV04(push, NV50_3D(COUNTER_RESET), 1);
176 PUSH_DATA (push, NV50_3D_COUNTER_RESET_SAMPLECNT);
177 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
180 case PIPE_QUERY_PRIMITIVES_GENERATED:
181 nv50_query_get(push, q, 0x10, 0x06805002);
183 case PIPE_QUERY_PRIMITIVES_EMITTED:
184 nv50_query_get(push, q, 0x10, 0x05805002);
186 case PIPE_QUERY_SO_STATISTICS:
187 nv50_query_get(push, q, 0x20, 0x05805002);
188 nv50_query_get(push, q, 0x30, 0x06805002);
190 case PIPE_QUERY_PIPELINE_STATISTICS:
191 nv50_query_get(push, q, 0x80, 0x00801002); /* VFETCH, VERTICES */
192 nv50_query_get(push, q, 0x90, 0x01801002); /* VFETCH, PRIMS */
193 nv50_query_get(push, q, 0xa0, 0x02802002); /* VP, LAUNCHES */
194 nv50_query_get(push, q, 0xb0, 0x03806002); /* GP, LAUNCHES */
195 nv50_query_get(push, q, 0xc0, 0x04806002); /* GP, PRIMS_OUT */
196 nv50_query_get(push, q, 0xd0, 0x07804002); /* RAST, PRIMS_IN */
197 nv50_query_get(push, q, 0xe0, 0x08804002); /* RAST, PRIMS_OUT */
198 nv50_query_get(push, q, 0xf0, 0x0980a002); /* ROP, PIXELS */
200 case PIPE_QUERY_TIME_ELAPSED:
201 nv50_query_get(push, q, 0x10, 0x00005002);
211 nv50_query_end(struct pipe_context *pipe, struct pipe_query *pq)
213 struct nv50_context *nv50 = nv50_context(pipe);
214 struct nouveau_pushbuf *push = nv50->base.pushbuf;
215 struct nv50_query *q = nv50_query(pq);
218 case PIPE_QUERY_OCCLUSION_COUNTER:
219 nv50_query_get(push, q, 0, 0x0100f002);
221 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
224 case PIPE_QUERY_PRIMITIVES_GENERATED:
225 nv50_query_get(push, q, 0, 0x06805002);
227 case PIPE_QUERY_PRIMITIVES_EMITTED:
228 nv50_query_get(push, q, 0, 0x05805002);
230 case PIPE_QUERY_SO_STATISTICS:
231 nv50_query_get(push, q, 0x00, 0x05805002);
232 nv50_query_get(push, q, 0x10, 0x06805002);
234 case PIPE_QUERY_PIPELINE_STATISTICS:
235 nv50_query_get(push, q, 0x00, 0x00801002); /* VFETCH, VERTICES */
236 nv50_query_get(push, q, 0x10, 0x01801002); /* VFETCH, PRIMS */
237 nv50_query_get(push, q, 0x20, 0x02802002); /* VP, LAUNCHES */
238 nv50_query_get(push, q, 0x30, 0x03806002); /* GP, LAUNCHES */
239 nv50_query_get(push, q, 0x40, 0x04806002); /* GP, PRIMS_OUT */
240 nv50_query_get(push, q, 0x50, 0x07804002); /* RAST, PRIMS_IN */
241 nv50_query_get(push, q, 0x60, 0x08804002); /* RAST, PRIMS_OUT */
242 nv50_query_get(push, q, 0x70, 0x0980a002); /* ROP, PIXELS */
244 case PIPE_QUERY_TIMESTAMP:
247 case PIPE_QUERY_TIME_ELAPSED:
248 nv50_query_get(push, q, 0, 0x00005002);
250 case PIPE_QUERY_GPU_FINISHED:
252 nv50_query_get(push, q, 0, 0x1000f010);
254 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET:
255 nv50_query_get(push, q, 0, 0x0d005002 | (q->index << 5));
257 case PIPE_QUERY_TIMESTAMP_DISJOINT:
258 /* This query is not issued on GPU because disjoint is forced to FALSE */
265 q->ready = q->flushed = FALSE;
268 nouveau_fence_ref(nv50->screen->base.fence.current, &q->fence);
271 static INLINE boolean
272 nv50_query_ready(struct nv50_query *q)
275 if (nouveau_fence_signalled(q->fence))
278 if (q->data[0] == q->sequence)
285 nv50_query_result(struct pipe_context *pipe, struct pipe_query *pq,
286 boolean wait, union pipe_query_result *result)
288 struct nv50_context *nv50 = nv50_context(pipe);
289 struct nv50_query *q = nv50_query(pq);
290 uint64_t *res64 = (uint64_t *)result;
291 uint32_t *res32 = (uint32_t *)result;
292 boolean *res8 = (boolean *)result;
293 uint64_t *data64 = (uint64_t *)q->data;
296 if (!q->ready) /* update ? */
297 q->ready = nv50_query_ready(q);
300 /* for broken apps that spin on GL_QUERY_RESULT_AVAILABLE */
303 PUSH_KICK(nv50->base.pushbuf);
307 if (nouveau_bo_wait(q->bo, NOUVEAU_BO_RD, nv50->screen->base.client))
313 case PIPE_QUERY_GPU_FINISHED:
316 case PIPE_QUERY_OCCLUSION_COUNTER: /* u32 sequence, u32 count, u64 time */
317 res64[0] = q->data[1];
319 case PIPE_QUERY_PRIMITIVES_GENERATED: /* u64 count, u64 time */
320 case PIPE_QUERY_PRIMITIVES_EMITTED: /* u64 count, u64 time */
321 res64[0] = data64[0] - data64[2];
323 case PIPE_QUERY_SO_STATISTICS:
324 res64[0] = data64[0] - data64[4];
325 res64[1] = data64[2] - data64[6];
327 case PIPE_QUERY_PIPELINE_STATISTICS:
328 for (i = 0; i < 8; ++i)
329 res64[i] = data64[i * 2] - data64[16 + i * 2];
331 case PIPE_QUERY_TIMESTAMP:
332 res64[0] = data64[1];
334 case PIPE_QUERY_TIMESTAMP_DISJOINT:
335 res64[0] = 1000000000;
338 case PIPE_QUERY_TIME_ELAPSED:
339 res64[0] = data64[1] - data64[3];
341 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET:
342 res32[0] = q->data[1];
352 nv84_query_fifo_wait(struct nouveau_pushbuf *push, struct pipe_query *pq)
354 struct nv50_query *q = nv50_query(pq);
355 unsigned offset = q->offset;
358 PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
359 BEGIN_NV04(push, SUBC_3D(NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH), 4);
360 PUSH_DATAh(push, q->bo->offset + offset);
361 PUSH_DATA (push, q->bo->offset + offset);
362 PUSH_DATA (push, q->sequence);
363 PUSH_DATA (push, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
367 nv50_render_condition(struct pipe_context *pipe,
368 struct pipe_query *pq,
369 boolean condition, uint mode)
371 struct nv50_context *nv50 = nv50_context(pipe);
372 struct nouveau_pushbuf *push = nv50->base.pushbuf;
373 struct nv50_query *q;
376 mode != PIPE_RENDER_COND_NO_WAIT &&
377 mode != PIPE_RENDER_COND_BY_REGION_NO_WAIT;
380 cond = NV50_3D_COND_MODE_ALWAYS;
384 /* NOTE: comparison of 2 queries only works if both have completed */
386 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
387 cond = condition ? NV50_3D_COND_MODE_EQUAL :
388 NV50_3D_COND_MODE_NOT_EQUAL;
391 case PIPE_QUERY_OCCLUSION_COUNTER:
392 case PIPE_QUERY_OCCLUSION_PREDICATE:
393 if (likely(!condition)) {
394 /* XXX: Placeholder, handle nesting here if available */
396 cond = wait ? NV50_3D_COND_MODE_NOT_EQUAL :
397 NV50_3D_COND_MODE_ALWAYS;
399 cond = NV50_3D_COND_MODE_RES_NON_ZERO;
401 cond = wait ? NV50_3D_COND_MODE_EQUAL : NV50_3D_COND_MODE_ALWAYS;
405 assert(!"render condition query not a predicate");
406 cond = NV50_3D_COND_MODE_ALWAYS;
411 nv50->cond_query = pq;
412 nv50->cond_cond = condition;
413 nv50->cond_condmode = cond;
414 nv50->cond_mode = mode;
418 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
419 PUSH_DATA (push, cond);
426 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
430 PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
431 BEGIN_NV04(push, NV50_3D(COND_ADDRESS_HIGH), 3);
432 PUSH_DATAh(push, q->bo->offset + q->offset);
433 PUSH_DATA (push, q->bo->offset + q->offset);
434 PUSH_DATA (push, cond);
436 BEGIN_NV04(push, NV50_2D(COND_ADDRESS_HIGH), 2);
437 PUSH_DATAh(push, q->bo->offset + q->offset);
438 PUSH_DATA (push, q->bo->offset + q->offset);
442 nv50_query_pushbuf_submit(struct nouveau_pushbuf *push,
443 struct pipe_query *pq, unsigned result_offset)
445 struct nv50_query *q = nv50_query(pq);
447 /* XXX: does this exist ? */
448 #define NV50_IB_ENTRY_1_NO_PREFETCH (0 << (31 - 8))
450 nouveau_pushbuf_space(push, 0, 0, 1);
451 nouveau_pushbuf_data(push, q->bo, q->offset + result_offset, 4 |
452 NV50_IB_ENTRY_1_NO_PREFETCH);
456 nva0_so_target_save_offset(struct pipe_context *pipe,
457 struct pipe_stream_output_target *ptarg,
458 unsigned index, boolean serialize)
460 struct nv50_so_target *targ = nv50_so_target(ptarg);
463 struct nouveau_pushbuf *push = nv50_context(pipe)->base.pushbuf;
465 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
469 nv50_query(targ->pq)->index = index;
470 nv50_query_end(pipe, targ->pq);
474 nv50_init_query_functions(struct nv50_context *nv50)
476 struct pipe_context *pipe = &nv50->base.pipe;
478 pipe->create_query = nv50_query_create;
479 pipe->destroy_query = nv50_query_destroy;
480 pipe->begin_query = nv50_query_begin;
481 pipe->end_query = nv50_query_end;
482 pipe->get_query_result = nv50_query_result;
483 pipe->render_condition = nv50_render_condition;