nouveau: switch to libdrm_nouveau-2.0
[mesa.git] / src / gallium / drivers / 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 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
20 * SOFTWARE.
21 *
22 * Authors: Christoph Bumiller
23 */
24
25 #define NV50_PUSH_EXPLICIT_SPACE_CHECKING
26
27 #include "nv50_context.h"
28 #include "nouveau/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 uint32_t type;
40 uint32_t sequence;
41 struct nouveau_bo *bo;
42 uint32_t base;
43 uint32_t offset; /* base + i * 16 */
44 boolean ready;
45 boolean is64bit;
46 struct nouveau_mm_allocation *mm;
47 };
48
49 #define NV50_QUERY_ALLOC_SPACE 128
50
51 static INLINE struct nv50_query *
52 nv50_query(struct pipe_query *pipe)
53 {
54 return (struct nv50_query *)pipe;
55 }
56
57 static boolean
58 nv50_query_allocate(struct nv50_context *nv50, struct nv50_query *q, int size)
59 {
60 struct nv50_screen *screen = nv50->screen;
61 int ret;
62
63 if (q->bo) {
64 nouveau_bo_ref(NULL, &q->bo);
65 if (q->mm) {
66 if (q->ready)
67 nouveau_mm_free(q->mm);
68 else
69 nouveau_fence_work(screen->base.fence.current, nouveau_mm_free_work,
70 q->mm);
71 }
72 }
73 if (size) {
74 q->mm = nouveau_mm_allocate(screen->base.mm_GART, size, &q->bo, &q->base);
75 if (!q->bo)
76 return FALSE;
77 q->offset = q->base;
78
79 ret = nouveau_bo_map(q->bo, 0, screen->base.client);
80 if (ret) {
81 nv50_query_allocate(nv50, q, 0);
82 return FALSE;
83 }
84 q->data = (uint32_t *)((uint8_t *)q->bo->map + q->base);
85 }
86 return TRUE;
87 }
88
89 static void
90 nv50_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
91 {
92 nv50_query_allocate(nv50_context(pipe), nv50_query(pq), 0);
93 FREE(nv50_query(pq));
94 }
95
96 static struct pipe_query *
97 nv50_query_create(struct pipe_context *pipe, unsigned type)
98 {
99 struct nv50_context *nv50 = nv50_context(pipe);
100 struct nv50_query *q;
101
102 q = CALLOC_STRUCT(nv50_query);
103 if (!q)
104 return NULL;
105
106 if (!nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE)) {
107 FREE(q);
108 return NULL;
109 }
110
111 q->is64bit = (type == PIPE_QUERY_PRIMITIVES_GENERATED ||
112 type == PIPE_QUERY_PRIMITIVES_EMITTED ||
113 type == PIPE_QUERY_SO_STATISTICS);
114 q->type = type;
115
116 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
117 q->offset -= 16;
118 q->data -= 16 / sizeof(*q->data); /* we advance before query_begin ! */
119 }
120
121 return (struct pipe_query *)q;
122 }
123
124 static void
125 nv50_query_get(struct nouveau_pushbuf *push, struct nv50_query *q,
126 unsigned offset, uint32_t get)
127 {
128 offset += q->offset;
129
130 PUSH_SPACE(push, 5);
131 PUSH_REFN (push, q->bo, NOUVEAU_BO_GART | NOUVEAU_BO_WR);
132 BEGIN_NV04(push, NV50_3D(QUERY_ADDRESS_HIGH), 4);
133 PUSH_DATAh(push, q->bo->offset + offset);
134 PUSH_DATA (push, q->bo->offset + offset);
135 PUSH_DATA (push, q->sequence);
136 PUSH_DATA (push, get);
137 }
138
139 static void
140 nv50_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
141 {
142 struct nv50_context *nv50 = nv50_context(pipe);
143 struct nouveau_pushbuf *push = nv50->base.pushbuf;
144 struct nv50_query *q = nv50_query(pq);
145
146 /* For occlusion queries we have to change the storage, because a previous
147 * query might set the initial render conition to FALSE even *after* we re-
148 * initialized it to TRUE.
149 */
150 if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
151 q->offset += 16;
152 q->data += 16 / sizeof(*q->data);
153 if (q->offset - q->base == NV50_QUERY_ALLOC_SPACE)
154 nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE);
155
156 /* XXX: can we do this with the GPU, and sync with respect to a previous
157 * query ?
158 */
159 q->data[1] = 1; /* initial render condition = TRUE */
160 }
161 if (!q->is64bit)
162 q->data[0] = q->sequence++; /* the previously used one */
163
164 switch (q->type) {
165 case PIPE_QUERY_OCCLUSION_COUNTER:
166 PUSH_SPACE(push, 4);
167 BEGIN_NV04(push, NV50_3D(COUNTER_RESET), 1);
168 PUSH_DATA (push, NV50_3D_COUNTER_RESET_SAMPLECNT);
169 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
170 PUSH_DATA (push, 1);
171 break;
172 case PIPE_QUERY_PRIMITIVES_GENERATED: /* store before & after instead ? */
173 PUSH_SPACE(push, 2);
174 BEGIN_NV04(push, NV50_3D(COUNTER_RESET), 1);
175 PUSH_DATA (push, NV50_3D_COUNTER_RESET_GENERATED_PRIMITIVES);
176 break;
177 case PIPE_QUERY_PRIMITIVES_EMITTED:
178 PUSH_SPACE(push, 2);
179 BEGIN_NV04(push, NV50_3D(COUNTER_RESET), 1);
180 PUSH_DATA (push, NV50_3D_COUNTER_RESET_TRANSFORM_FEEDBACK);
181 break;
182 case PIPE_QUERY_SO_STATISTICS:
183 PUSH_SPACE(push, 3);
184 BEGIN_NI04(push, NV50_3D(COUNTER_RESET), 2);
185 PUSH_DATA (push, NV50_3D_COUNTER_RESET_TRANSFORM_FEEDBACK);
186 PUSH_DATA (push, NV50_3D_COUNTER_RESET_GENERATED_PRIMITIVES);
187 break;
188 case PIPE_QUERY_TIMESTAMP_DISJOINT:
189 case PIPE_QUERY_TIME_ELAPSED:
190 nv50_query_get(push, q, 0x10, 0x00005002);
191 break;
192 default:
193 break;
194 }
195 q->ready = FALSE;
196 }
197
198 static void
199 nv50_query_end(struct pipe_context *pipe, struct pipe_query *pq)
200 {
201 struct nv50_context *nv50 = nv50_context(pipe);
202 struct nouveau_pushbuf *push = nv50->base.pushbuf;
203 struct nv50_query *q = nv50_query(pq);
204
205 switch (q->type) {
206 case PIPE_QUERY_OCCLUSION_COUNTER:
207 nv50_query_get(push, q, 0, 0x0100f002);
208 PUSH_SPACE(push, 2);
209 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
210 PUSH_DATA (push, 0);
211 break;
212 case PIPE_QUERY_PRIMITIVES_GENERATED:
213 nv50_query_get(push, q, 0, 0x06805002);
214 break;
215 case PIPE_QUERY_PRIMITIVES_EMITTED:
216 nv50_query_get(push, q, 0, 0x05805002);
217 break;
218 case PIPE_QUERY_SO_STATISTICS:
219 nv50_query_get(push, q, 0x00, 0x05805002);
220 nv50_query_get(push, q, 0x10, 0x06805002);
221 break;
222 case PIPE_QUERY_TIMESTAMP_DISJOINT:
223 case PIPE_QUERY_TIME_ELAPSED:
224 nv50_query_get(push, q, 0, 0x00005002);
225 break;
226 case PIPE_QUERY_GPU_FINISHED:
227 nv50_query_get(push, q, 0, 0x1000f010);
228 break;
229 default:
230 assert(0);
231 break;
232 }
233 }
234
235 static INLINE boolean
236 nv50_query_ready(struct nv50_query *q)
237 {
238 return q->ready || (!q->is64bit && (q->data[0] == q->sequence));
239 }
240
241 static boolean
242 nv50_query_result(struct pipe_context *pipe, struct pipe_query *pq,
243 boolean wait, union pipe_query_result *result)
244 {
245 struct nv50_context *nv50 = nv50_context(pipe);
246 struct nv50_query *q = nv50_query(pq);
247 uint64_t *res64 = (uint64_t *)result;
248 boolean *res8 = (boolean *)result;
249 uint64_t *data64 = (uint64_t *)q->data;
250
251 if (!q->ready) /* update ? */
252 q->ready = nv50_query_ready(q);
253 if (!q->ready) {
254 if (!wait) {
255 /* for broken apps that spin on GL_QUERY_RESULT_AVAILABLE */
256 PUSH_KICK(nv50->base.pushbuf);
257 return FALSE;
258 }
259 if (nouveau_bo_wait(q->bo, NOUVEAU_BO_RD, nv50->screen->base.client))
260 return FALSE;
261 }
262 q->ready = TRUE;
263
264 switch (q->type) {
265 case PIPE_QUERY_GPU_FINISHED:
266 res8[0] = TRUE;
267 break;
268 case PIPE_QUERY_OCCLUSION_COUNTER: /* u32 sequence, u32 count, u64 time */
269 res64[0] = q->data[1];
270 break;
271 case PIPE_QUERY_PRIMITIVES_GENERATED: /* u64 count, u64 time */
272 case PIPE_QUERY_PRIMITIVES_EMITTED: /* u64 count, u64 time */
273 res64[0] = data64[0];
274 break;
275 case PIPE_QUERY_SO_STATISTICS:
276 res64[0] = data64[0];
277 res64[1] = data64[1];
278 break;
279 case PIPE_QUERY_TIMESTAMP_DISJOINT: /* u32 sequence, u32 0, u64 time */
280 res64[0] = 1000000000;
281 res8[8] = (data64[0] == data64[2]) ? FALSE : TRUE;
282 break;
283 case PIPE_QUERY_TIME_ELAPSED:
284 res64[0] = data64[1] - data64[3];
285 break;
286 default:
287 return FALSE;
288 }
289
290 return TRUE;
291 }
292
293 static void
294 nv50_render_condition(struct pipe_context *pipe,
295 struct pipe_query *pq, uint mode)
296 {
297 struct nv50_context *nv50 = nv50_context(pipe);
298 struct nouveau_pushbuf *push = nv50->base.pushbuf;
299 struct nv50_query *q;
300
301 PUSH_SPACE(push, 6);
302
303 if (!pq) {
304 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
305 PUSH_DATA (push, NV50_3D_COND_MODE_ALWAYS);
306 return;
307 }
308 q = nv50_query(pq);
309
310 if (mode == PIPE_RENDER_COND_WAIT ||
311 mode == PIPE_RENDER_COND_BY_REGION_WAIT) {
312 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
313 PUSH_DATA (push, 0);
314 }
315
316 BEGIN_NV04(push, NV50_3D(COND_ADDRESS_HIGH), 3);
317 PUSH_DATAh(push, q->bo->offset + q->offset);
318 PUSH_DATA (push, q->bo->offset + q->offset);
319 PUSH_DATA (push, NV50_3D_COND_MODE_RES_NON_ZERO);
320 }
321
322 void
323 nv50_init_query_functions(struct nv50_context *nv50)
324 {
325 struct pipe_context *pipe = &nv50->base.pipe;
326
327 pipe->create_query = nv50_query_create;
328 pipe->destroy_query = nv50_query_destroy;
329 pipe->begin_query = nv50_query_begin;
330 pipe->end_query = nv50_query_end;
331 pipe->get_query_result = nv50_query_result;
332 pipe->render_condition = nv50_render_condition;
333 }