nv50: fix PIPELINE_STATISTICS with HUD, based on nvc0
[mesa.git] / src / gallium / drivers / nouveau / nv50 / nv50_query.c
1 /*
2 * Copyright 2011 Nouveau Project
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Christoph Bumiller
23 */
24
25 #define NV50_PUSH_EXPLICIT_SPACE_CHECKING
26
27 #include "nv50/nv50_context.h"
28 #include "nv_object.xml.h"
29
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.
32 *
33 * The first is not that big of an issue because OpenGL does not allow nested
34 * queries anyway.
35 */
36
37 struct nv50_query {
38 uint32_t *data;
39 uint16_t type;
40 uint16_t index;
41 uint32_t sequence;
42 struct nouveau_bo *bo;
43 uint32_t base;
44 uint32_t offset; /* base + i * 32 */
45 boolean ready;
46 boolean flushed;
47 boolean is64bit;
48 struct nouveau_mm_allocation *mm;
49 struct nouveau_fence *fence;
50 };
51
52 #define NV50_QUERY_ALLOC_SPACE 256
53
54 static INLINE struct nv50_query *
55 nv50_query(struct pipe_query *pipe)
56 {
57 return (struct nv50_query *)pipe;
58 }
59
60 static boolean
61 nv50_query_allocate(struct nv50_context *nv50, struct nv50_query *q, int size)
62 {
63 struct nv50_screen *screen = nv50->screen;
64 int ret;
65
66 if (q->bo) {
67 nouveau_bo_ref(NULL, &q->bo);
68 if (q->mm) {
69 if (q->ready)
70 nouveau_mm_free(q->mm);
71 else
72 nouveau_fence_work(screen->base.fence.current, nouveau_mm_free_work,
73 q->mm);
74 }
75 }
76 if (size) {
77 q->mm = nouveau_mm_allocate(screen->base.mm_GART, size, &q->bo, &q->base);
78 if (!q->bo)
79 return FALSE;
80 q->offset = q->base;
81
82 ret = nouveau_bo_map(q->bo, 0, screen->base.client);
83 if (ret) {
84 nv50_query_allocate(nv50, q, 0);
85 return FALSE;
86 }
87 q->data = (uint32_t *)((uint8_t *)q->bo->map + q->base);
88 }
89 return TRUE;
90 }
91
92 static void
93 nv50_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
94 {
95 nv50_query_allocate(nv50_context(pipe), nv50_query(pq), 0);
96 nouveau_fence_ref(NULL, &nv50_query(pq)->fence);
97 FREE(nv50_query(pq));
98 }
99
100 static struct pipe_query *
101 nv50_query_create(struct pipe_context *pipe, unsigned type, unsigned index)
102 {
103 struct nv50_context *nv50 = nv50_context(pipe);
104 struct nv50_query *q;
105
106 q = CALLOC_STRUCT(nv50_query);
107 if (!q)
108 return NULL;
109
110 if (!nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE)) {
111 FREE(q);
112 return NULL;
113 }
114
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);
119 q->type = type;
120
121 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
122 q->offset -= 32;
123 q->data -= 32 / sizeof(*q->data); /* we advance before query_begin ! */
124 }
125
126 return (struct pipe_query *)q;
127 }
128
129 static void
130 nv50_query_get(struct nouveau_pushbuf *push, struct nv50_query *q,
131 unsigned offset, uint32_t get)
132 {
133 offset += q->offset;
134
135 PUSH_SPACE(push, 5);
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);
142 }
143
144 static boolean
145 nv50_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
146 {
147 struct nv50_context *nv50 = nv50_context(pipe);
148 struct nouveau_pushbuf *push = nv50->base.pushbuf;
149 struct nv50_query *q = nv50_query(pq);
150
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.
154 */
155 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
156 q->offset += 32;
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);
160
161 /* XXX: can we do this with the GPU, and sync with respect to a previous
162 * query ?
163 */
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 */
167 q->data[5] = 0;
168 }
169 if (!q->is64bit)
170 q->data[0] = q->sequence++; /* the previously used one */
171
172 switch (q->type) {
173 case PIPE_QUERY_OCCLUSION_COUNTER:
174 PUSH_SPACE(push, 4);
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);
178 PUSH_DATA (push, 1);
179 break;
180 case PIPE_QUERY_PRIMITIVES_GENERATED:
181 nv50_query_get(push, q, 0x10, 0x06805002);
182 break;
183 case PIPE_QUERY_PRIMITIVES_EMITTED:
184 nv50_query_get(push, q, 0x10, 0x05805002);
185 break;
186 case PIPE_QUERY_SO_STATISTICS:
187 nv50_query_get(push, q, 0x20, 0x05805002);
188 nv50_query_get(push, q, 0x30, 0x06805002);
189 break;
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 */
199 break;
200 case PIPE_QUERY_TIME_ELAPSED:
201 nv50_query_get(push, q, 0x10, 0x00005002);
202 break;
203 default:
204 break;
205 }
206 q->ready = FALSE;
207 return true;
208 }
209
210 static void
211 nv50_query_end(struct pipe_context *pipe, struct pipe_query *pq)
212 {
213 struct nv50_context *nv50 = nv50_context(pipe);
214 struct nouveau_pushbuf *push = nv50->base.pushbuf;
215 struct nv50_query *q = nv50_query(pq);
216
217 switch (q->type) {
218 case PIPE_QUERY_OCCLUSION_COUNTER:
219 nv50_query_get(push, q, 0, 0x0100f002);
220 PUSH_SPACE(push, 2);
221 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
222 PUSH_DATA (push, 0);
223 break;
224 case PIPE_QUERY_PRIMITIVES_GENERATED:
225 nv50_query_get(push, q, 0, 0x06805002);
226 break;
227 case PIPE_QUERY_PRIMITIVES_EMITTED:
228 nv50_query_get(push, q, 0, 0x05805002);
229 break;
230 case PIPE_QUERY_SO_STATISTICS:
231 nv50_query_get(push, q, 0x00, 0x05805002);
232 nv50_query_get(push, q, 0x10, 0x06805002);
233 break;
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 */
243 break;
244 case PIPE_QUERY_TIMESTAMP:
245 q->sequence++;
246 /* fall through */
247 case PIPE_QUERY_TIME_ELAPSED:
248 nv50_query_get(push, q, 0, 0x00005002);
249 break;
250 case PIPE_QUERY_GPU_FINISHED:
251 q->sequence++;
252 nv50_query_get(push, q, 0, 0x1000f010);
253 break;
254 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET:
255 nv50_query_get(push, q, 0, 0x0d005002 | (q->index << 5));
256 break;
257 case PIPE_QUERY_TIMESTAMP_DISJOINT:
258 /* This query is not issued on GPU because disjoint is forced to FALSE */
259 q->ready = TRUE;
260 break;
261 default:
262 assert(0);
263 break;
264 }
265 q->ready = q->flushed = FALSE;
266
267 if (q->is64bit)
268 nouveau_fence_ref(nv50->screen->base.fence.current, &q->fence);
269 }
270
271 static INLINE boolean
272 nv50_query_ready(struct nv50_query *q)
273 {
274 if (q->is64bit) {
275 if (nouveau_fence_signalled(q->fence))
276 return TRUE;
277 } else {
278 if (q->data[0] == q->sequence)
279 return TRUE;
280 }
281 return FALSE;
282 }
283
284 static boolean
285 nv50_query_result(struct pipe_context *pipe, struct pipe_query *pq,
286 boolean wait, union pipe_query_result *result)
287 {
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;
294 int i;
295
296 if (!q->ready) /* update ? */
297 q->ready = nv50_query_ready(q);
298 if (!q->ready) {
299 if (!wait) {
300 /* for broken apps that spin on GL_QUERY_RESULT_AVAILABLE */
301 if (!q->flushed) {
302 q->flushed = TRUE;
303 PUSH_KICK(nv50->base.pushbuf);
304 }
305 return FALSE;
306 }
307 if (nouveau_bo_wait(q->bo, NOUVEAU_BO_RD, nv50->screen->base.client))
308 return FALSE;
309 }
310 q->ready = TRUE;
311
312 switch (q->type) {
313 case PIPE_QUERY_GPU_FINISHED:
314 res8[0] = TRUE;
315 break;
316 case PIPE_QUERY_OCCLUSION_COUNTER: /* u32 sequence, u32 count, u64 time */
317 res64[0] = q->data[1];
318 break;
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];
322 break;
323 case PIPE_QUERY_SO_STATISTICS:
324 res64[0] = data64[0] - data64[4];
325 res64[1] = data64[2] - data64[6];
326 break;
327 case PIPE_QUERY_PIPELINE_STATISTICS:
328 for (i = 0; i < 8; ++i)
329 res64[i] = data64[i * 2] - data64[16 + i * 2];
330 break;
331 case PIPE_QUERY_TIMESTAMP:
332 res64[0] = data64[1];
333 break;
334 case PIPE_QUERY_TIMESTAMP_DISJOINT:
335 res64[0] = 1000000000;
336 res8[8] = FALSE;
337 break;
338 case PIPE_QUERY_TIME_ELAPSED:
339 res64[0] = data64[1] - data64[3];
340 break;
341 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET:
342 res32[0] = q->data[1];
343 break;
344 default:
345 return FALSE;
346 }
347
348 return TRUE;
349 }
350
351 void
352 nv84_query_fifo_wait(struct nouveau_pushbuf *push, struct pipe_query *pq)
353 {
354 struct nv50_query *q = nv50_query(pq);
355 unsigned offset = q->offset;
356
357 PUSH_SPACE(push, 5);
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);
364 }
365
366 static void
367 nv50_render_condition(struct pipe_context *pipe,
368 struct pipe_query *pq,
369 boolean condition, uint mode)
370 {
371 struct nv50_context *nv50 = nv50_context(pipe);
372 struct nouveau_pushbuf *push = nv50->base.pushbuf;
373 struct nv50_query *q;
374 uint32_t cond;
375 boolean wait =
376 mode != PIPE_RENDER_COND_NO_WAIT &&
377 mode != PIPE_RENDER_COND_BY_REGION_NO_WAIT;
378
379 if (!pq) {
380 cond = NV50_3D_COND_MODE_ALWAYS;
381 }
382 else {
383 q = nv50_query(pq);
384 /* NOTE: comparison of 2 queries only works if both have completed */
385 switch (q->type) {
386 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
387 cond = condition ? NV50_3D_COND_MODE_EQUAL :
388 NV50_3D_COND_MODE_NOT_EQUAL;
389 wait = TRUE;
390 break;
391 case PIPE_QUERY_OCCLUSION_COUNTER:
392 case PIPE_QUERY_OCCLUSION_PREDICATE:
393 if (likely(!condition)) {
394 /* XXX: Placeholder, handle nesting here if available */
395 if (unlikely(false))
396 cond = wait ? NV50_3D_COND_MODE_NOT_EQUAL :
397 NV50_3D_COND_MODE_ALWAYS;
398 else
399 cond = NV50_3D_COND_MODE_RES_NON_ZERO;
400 } else {
401 cond = wait ? NV50_3D_COND_MODE_EQUAL : NV50_3D_COND_MODE_ALWAYS;
402 }
403 break;
404 default:
405 assert(!"render condition query not a predicate");
406 cond = NV50_3D_COND_MODE_ALWAYS;
407 break;
408 }
409 }
410
411 nv50->cond_query = pq;
412 nv50->cond_cond = condition;
413 nv50->cond_condmode = cond;
414 nv50->cond_mode = mode;
415
416 if (!pq) {
417 PUSH_SPACE(push, 2);
418 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
419 PUSH_DATA (push, cond);
420 return;
421 }
422
423 PUSH_SPACE(push, 9);
424
425 if (wait) {
426 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
427 PUSH_DATA (push, 0);
428 }
429
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);
435
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);
439 }
440
441 void
442 nv50_query_pushbuf_submit(struct nouveau_pushbuf *push,
443 struct pipe_query *pq, unsigned result_offset)
444 {
445 struct nv50_query *q = nv50_query(pq);
446
447 /* XXX: does this exist ? */
448 #define NV50_IB_ENTRY_1_NO_PREFETCH (0 << (31 - 8))
449
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);
453 }
454
455 void
456 nva0_so_target_save_offset(struct pipe_context *pipe,
457 struct pipe_stream_output_target *ptarg,
458 unsigned index, boolean serialize)
459 {
460 struct nv50_so_target *targ = nv50_so_target(ptarg);
461
462 if (serialize) {
463 struct nouveau_pushbuf *push = nv50_context(pipe)->base.pushbuf;
464 PUSH_SPACE(push, 2);
465 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
466 PUSH_DATA (push, 0);
467 }
468
469 nv50_query(targ->pq)->index = index;
470 nv50_query_end(pipe, targ->pq);
471 }
472
473 void
474 nv50_init_query_functions(struct nv50_context *nv50)
475 {
476 struct pipe_context *pipe = &nv50->base.pipe;
477
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;
484 }