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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * Authors: Christoph Bumiller
25 #include "nv50_context.h"
26 #include "nouveau/nv_object.xml.h"
28 /* XXX: Nested queries, and simultaneous queries on multiple gallium contexts
29 * (since we use only a single GPU channel per screen) will not work properly.
31 * The first is not that big of an issue because OpenGL does not allow nested
39 struct nouveau_bo *bo;
41 uint32_t offset; /* base + i * 16 */
44 struct nouveau_mm_allocation *mm;
47 #define NV50_QUERY_ALLOC_SPACE 128
49 static INLINE struct nv50_query *
50 nv50_query(struct pipe_query *pipe)
52 return (struct nv50_query *)pipe;
56 nv50_query_allocate(struct nv50_context *nv50, struct nv50_query *q, int size)
58 struct nv50_screen *screen = nv50->screen;
62 nouveau_bo_ref(NULL, &q->bo);
65 nouveau_mm_free(q->mm);
67 nouveau_fence_work(screen->base.fence.current, nouveau_mm_free_work, q->mm);
71 q->mm = nouveau_mm_allocate(screen->base.mm_GART, size, &q->bo, &q->base);
76 ret = nouveau_bo_map_range(q->bo, q->base, size, NOUVEAU_BO_RD |
79 nv50_query_allocate(nv50, q, 0);
83 nouveau_bo_unmap(q->bo);
89 nv50_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
91 nv50_query_allocate(nv50_context(pipe), nv50_query(pq), 0);
95 static struct pipe_query *
96 nv50_query_create(struct pipe_context *pipe, unsigned type)
98 struct nv50_context *nv50 = nv50_context(pipe);
101 q = CALLOC_STRUCT(nv50_query);
105 if (!nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE)) {
110 q->is64bit = (type == PIPE_QUERY_PRIMITIVES_GENERATED ||
111 type == PIPE_QUERY_PRIMITIVES_EMITTED ||
112 type == PIPE_QUERY_SO_STATISTICS);
115 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
117 q->data -= 16 / sizeof(*q->data); /* we advance before query_begin ! */
120 return (struct pipe_query *)q;
124 nv50_query_get(struct nouveau_channel *chan, struct nv50_query *q,
125 unsigned offset, uint32_t get)
129 MARK_RING (chan, 5, 2);
130 BEGIN_RING(chan, RING_3D(QUERY_ADDRESS_HIGH), 4);
131 OUT_RELOCh(chan, q->bo, offset, NOUVEAU_BO_GART | NOUVEAU_BO_WR);
132 OUT_RELOCl(chan, q->bo, offset, NOUVEAU_BO_GART | NOUVEAU_BO_WR);
133 OUT_RING (chan, q->sequence);
134 OUT_RING (chan, get);
138 nv50_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
140 struct nv50_context *nv50 = nv50_context(pipe);
141 struct nouveau_channel *chan = nv50->screen->base.channel;
142 struct nv50_query *q = nv50_query(pq);
144 /* For occlusion queries we have to change the storage, because a previous
145 * query might set the initial render conition to FALSE even *after* we re-
146 * initialized it to TRUE.
148 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
150 q->data += 16 / sizeof(*q->data);
151 if (q->offset - q->base == NV50_QUERY_ALLOC_SPACE)
152 nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE);
154 /* XXX: can we do this with the GPU, and sync with respect to a previous
157 q->data[1] = 1; /* initial render condition = TRUE */
160 q->data[0] = q->sequence++; /* the previously used one */
163 case PIPE_QUERY_OCCLUSION_COUNTER:
164 BEGIN_RING(chan, RING_3D(COUNTER_RESET), 1);
165 OUT_RING (chan, NV50_3D_COUNTER_RESET_SAMPLECNT);
166 BEGIN_RING(chan, RING_3D(SAMPLECNT_ENABLE), 1);
169 case PIPE_QUERY_PRIMITIVES_GENERATED: /* store before & after instead ? */
170 BEGIN_RING(chan, RING_3D(COUNTER_RESET), 1);
171 OUT_RING (chan, NV50_3D_COUNTER_RESET_GENERATED_PRIMITIVES);
173 case PIPE_QUERY_PRIMITIVES_EMITTED:
174 BEGIN_RING(chan, RING_3D(COUNTER_RESET), 1);
175 OUT_RING (chan, NV50_3D_COUNTER_RESET_TRANSFORM_FEEDBACK);
177 case PIPE_QUERY_SO_STATISTICS:
178 BEGIN_RING_NI(chan, RING_3D(COUNTER_RESET), 2);
179 OUT_RING (chan, NV50_3D_COUNTER_RESET_TRANSFORM_FEEDBACK);
180 OUT_RING (chan, NV50_3D_COUNTER_RESET_GENERATED_PRIMITIVES);
182 case PIPE_QUERY_TIMESTAMP_DISJOINT:
183 case PIPE_QUERY_TIME_ELAPSED:
184 nv50_query_get(chan, q, 0x10, 0x00005002);
193 nv50_query_end(struct pipe_context *pipe, struct pipe_query *pq)
195 struct nv50_context *nv50 = nv50_context(pipe);
196 struct nouveau_channel *chan = nv50->screen->base.channel;
197 struct nv50_query *q = nv50_query(pq);
200 case PIPE_QUERY_OCCLUSION_COUNTER:
201 nv50_query_get(chan, q, 0, 0x0100f002);
202 BEGIN_RING(chan, RING_3D(SAMPLECNT_ENABLE), 1);
205 case PIPE_QUERY_PRIMITIVES_GENERATED:
206 nv50_query_get(chan, q, 0, 0x06805002);
208 case PIPE_QUERY_PRIMITIVES_EMITTED:
209 nv50_query_get(chan, q, 0, 0x05805002);
211 case PIPE_QUERY_SO_STATISTICS:
212 nv50_query_get(chan, q, 0x00, 0x05805002);
213 nv50_query_get(chan, q, 0x10, 0x06805002);
215 case PIPE_QUERY_TIMESTAMP_DISJOINT:
216 case PIPE_QUERY_TIME_ELAPSED:
217 nv50_query_get(chan, q, 0, 0x00005002);
219 case PIPE_QUERY_GPU_FINISHED:
220 nv50_query_get(chan, q, 0, 0x1000f010);
228 static INLINE boolean
229 nv50_query_ready(struct nv50_query *q)
231 return q->ready || (!q->is64bit && (q->data[0] == q->sequence));
234 static INLINE boolean
235 nv50_query_wait(struct nv50_query *q)
237 int ret = nouveau_bo_map(q->bo, NOUVEAU_BO_RD);
240 nouveau_bo_unmap(q->bo);
245 nv50_query_result(struct pipe_context *pipe, struct pipe_query *pq,
246 boolean wait, void *result)
248 struct nv50_query *q = nv50_query(pq);
249 uint64_t *res64 = result;
250 boolean *res8 = result;
251 uint64_t *data64 = (uint64_t *)q->data;
253 if (q->type == PIPE_QUERY_GPU_FINISHED) {
254 res8[0] = nv50_query_ready(q);
258 if (!q->ready) /* update ? */
259 q->ready = nv50_query_ready(q);
261 struct nouveau_channel *chan = nv50_context(pipe)->screen->base.channel;
263 if (nouveau_bo_pending(q->bo) & NOUVEAU_BO_WR) /* for daft apps */
267 if (!nv50_query_wait(q))
273 case PIPE_QUERY_OCCLUSION_COUNTER: /* u32 sequence, u32 count, u64 time */
274 res64[0] = q->data[1];
276 case PIPE_QUERY_PRIMITIVES_GENERATED: /* u64 count, u64 time */
277 case PIPE_QUERY_PRIMITIVES_EMITTED: /* u64 count, u64 time */
278 res64[0] = data64[0];
280 case PIPE_QUERY_SO_STATISTICS:
281 res64[0] = data64[0];
282 res64[1] = data64[1];
284 case PIPE_QUERY_TIMESTAMP_DISJOINT: /* u32 sequence, u32 0, u64 time */
285 res64[0] = 1000000000;
286 res8[8] = (data64[0] == data64[2]) ? FALSE : TRUE;
288 case PIPE_QUERY_TIME_ELAPSED:
289 res64[0] = data64[1] - data64[3];
299 nv50_render_condition(struct pipe_context *pipe,
300 struct pipe_query *pq, uint mode)
302 struct nv50_context *nv50 = nv50_context(pipe);
303 struct nouveau_channel *chan = nv50->screen->base.channel;
304 struct nv50_query *q;
307 BEGIN_RING(chan, RING_3D(COND_MODE), 1);
308 OUT_RING (chan, NV50_3D_COND_MODE_ALWAYS);
313 if (mode == PIPE_RENDER_COND_WAIT ||
314 mode == PIPE_RENDER_COND_BY_REGION_WAIT) {
315 BEGIN_RING(chan, RING_3D_(NV50_GRAPH_WAIT_FOR_IDLE), 1);
319 MARK_RING (chan, 4, 2);
320 BEGIN_RING(chan, RING_3D(COND_ADDRESS_HIGH), 3);
321 OUT_RELOCh(chan, q->bo, q->offset, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
322 OUT_RELOCl(chan, q->bo, q->offset, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
323 OUT_RING (chan, NV50_3D_COND_MODE_RES_NON_ZERO);
327 nv50_init_query_functions(struct nv50_context *nv50)
329 struct pipe_context *pipe = &nv50->base.pipe;
331 pipe->create_query = nv50_query_create;
332 pipe->destroy_query = nv50_query_destroy;
333 pipe->begin_query = nv50_query_begin;
334 pipe->end_query = nv50_query_end;
335 pipe->get_query_result = nv50_query_result;
336 pipe->render_condition = nv50_render_condition;