Tizen 2.0 Release
[profile/ivi/osmesa.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 #include "nv50_context.h"
26 #include "nouveau/nv_object.xml.h"
27
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.
30  *
31  * The first is not that big of an issue because OpenGL does not allow nested
32  * queries anyway.
33  */
34
35 struct nv50_query {
36    uint32_t *data;
37    uint32_t type;
38    uint32_t sequence;
39    struct nouveau_bo *bo;
40    uint32_t base;
41    uint32_t offset; /* base + i * 16 */
42    boolean ready;
43    boolean is64bit;
44    struct nouveau_mm_allocation *mm;
45 };
46
47 #define NV50_QUERY_ALLOC_SPACE 128
48
49 static INLINE struct nv50_query *
50 nv50_query(struct pipe_query *pipe)
51 {
52    return (struct nv50_query *)pipe;
53 }
54
55 static boolean
56 nv50_query_allocate(struct nv50_context *nv50, struct nv50_query *q, int size)
57 {
58    struct nv50_screen *screen = nv50->screen;
59    int ret;
60
61    if (q->bo) {
62       nouveau_bo_ref(NULL, &q->bo);
63       if (q->mm) {
64          if (q->ready)
65             nouveau_mm_free(q->mm);
66          else
67             nouveau_fence_work(screen->base.fence.current, nouveau_mm_free_work, q->mm);
68       }
69    }
70    if (size) {
71       q->mm = nouveau_mm_allocate(screen->base.mm_GART, size, &q->bo, &q->base);
72       if (!q->bo)
73          return FALSE;
74       q->offset = q->base;
75
76       ret = nouveau_bo_map_range(q->bo, q->base, size, NOUVEAU_BO_RD |
77                                  NOUVEAU_BO_NOSYNC);
78       if (ret) {
79          nv50_query_allocate(nv50, q, 0);
80          return FALSE;
81       }
82       q->data = q->bo->map;
83       nouveau_bo_unmap(q->bo);
84    }
85    return TRUE;
86 }
87
88 static void
89 nv50_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
90 {
91    nv50_query_allocate(nv50_context(pipe), nv50_query(pq), 0);
92    FREE(nv50_query(pq));
93 }
94
95 static struct pipe_query *
96 nv50_query_create(struct pipe_context *pipe, unsigned type)
97 {
98    struct nv50_context *nv50 = nv50_context(pipe);
99    struct nv50_query *q;
100
101    q = CALLOC_STRUCT(nv50_query);
102    if (!q)
103       return NULL;
104
105    if (!nv50_query_allocate(nv50, q, NV50_QUERY_ALLOC_SPACE)) {
106       FREE(q);
107       return NULL;
108    }
109
110    q->is64bit = (type == PIPE_QUERY_PRIMITIVES_GENERATED ||
111                  type == PIPE_QUERY_PRIMITIVES_EMITTED ||
112                  type == PIPE_QUERY_SO_STATISTICS);
113    q->type = type;
114
115    if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
116       q->offset -= 16;
117       q->data -= 16 / sizeof(*q->data); /* we advance before query_begin ! */
118    }
119
120    return (struct pipe_query *)q;
121 }
122
123 static void
124 nv50_query_get(struct nouveau_channel *chan, struct nv50_query *q,
125                unsigned offset, uint32_t get)
126 {
127    offset += q->offset;
128
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);
135 }
136
137 static void
138 nv50_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
139 {
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);
143
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.
147     */
148    if (q->type == PIPE_QUERY_OCCLUSION_COUNTER) {
149       q->offset += 16;
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);
153
154       /* XXX: can we do this with the GPU, and sync with respect to a previous
155        *  query ?
156        */
157       q->data[1] = 1; /* initial render condition = TRUE */
158    }
159    if (!q->is64bit)
160       q->data[0] = q->sequence++; /* the previously used one */
161
162    switch (q->type) {
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);
167       OUT_RING  (chan, 1);
168       break;
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);
172       break;
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);
176       break;
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);
181       break;
182    case PIPE_QUERY_TIMESTAMP_DISJOINT:
183    case PIPE_QUERY_TIME_ELAPSED:
184       nv50_query_get(chan, q, 0x10, 0x00005002);
185       break;
186    default:
187       break;
188    }
189    q->ready = FALSE;
190 }
191
192 static void
193 nv50_query_end(struct pipe_context *pipe, struct pipe_query *pq)
194 {
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);
198
199    switch (q->type) {
200    case PIPE_QUERY_OCCLUSION_COUNTER:
201       nv50_query_get(chan, q, 0, 0x0100f002);
202       BEGIN_RING(chan, RING_3D(SAMPLECNT_ENABLE), 1);
203       OUT_RING  (chan, 0);
204       break;
205    case PIPE_QUERY_PRIMITIVES_GENERATED:
206       nv50_query_get(chan, q, 0, 0x06805002);
207       break;
208    case PIPE_QUERY_PRIMITIVES_EMITTED:
209       nv50_query_get(chan, q, 0, 0x05805002);
210       break;
211    case PIPE_QUERY_SO_STATISTICS:
212       nv50_query_get(chan, q, 0x00, 0x05805002);
213       nv50_query_get(chan, q, 0x10, 0x06805002);
214       break;
215    case PIPE_QUERY_TIMESTAMP_DISJOINT:
216    case PIPE_QUERY_TIME_ELAPSED:
217       nv50_query_get(chan, q, 0, 0x00005002);
218       break;
219    case PIPE_QUERY_GPU_FINISHED:
220       nv50_query_get(chan, q, 0, 0x1000f010);
221       break;
222    default:
223       assert(0);
224       break;
225    }
226 }
227
228 static INLINE boolean
229 nv50_query_ready(struct nv50_query *q)
230 {
231    return q->ready || (!q->is64bit && (q->data[0] == q->sequence));
232 }
233
234 static INLINE boolean
235 nv50_query_wait(struct nv50_query *q)
236 {
237    int ret = nouveau_bo_map(q->bo, NOUVEAU_BO_RD);
238    if (ret)
239       return FALSE;
240    nouveau_bo_unmap(q->bo);
241    return TRUE;
242 }
243
244 static boolean
245 nv50_query_result(struct pipe_context *pipe, struct pipe_query *pq,
246                   boolean wait, void *result)
247 {
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;
252
253    if (q->type == PIPE_QUERY_GPU_FINISHED) {
254       res8[0] = nv50_query_ready(q);
255       return TRUE;
256    }
257
258    if (!q->ready) /* update ? */
259       q->ready = nv50_query_ready(q);
260    if (!q->ready) {
261       struct nouveau_channel *chan = nv50_context(pipe)->screen->base.channel;
262       if (!wait) {
263          if (nouveau_bo_pending(q->bo) & NOUVEAU_BO_WR) /* for daft apps */
264             FIRE_RING(chan);
265          return FALSE;
266       }
267       if (!nv50_query_wait(q))
268          return FALSE;
269    }
270    q->ready = TRUE;
271
272    switch (q->type) {
273    case PIPE_QUERY_OCCLUSION_COUNTER: /* u32 sequence, u32 count, u64 time */
274       res64[0] = q->data[1];
275       break;
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];
279       break;
280    case PIPE_QUERY_SO_STATISTICS:
281       res64[0] = data64[0];
282       res64[1] = data64[1];
283       break;
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;
287       break;
288    case PIPE_QUERY_TIME_ELAPSED:
289       res64[0] = data64[1] - data64[3];
290       break;
291    default:
292       return FALSE;
293    }
294
295    return TRUE;
296 }
297
298 static void
299 nv50_render_condition(struct pipe_context *pipe,
300                       struct pipe_query *pq, uint mode)
301 {
302    struct nv50_context *nv50 = nv50_context(pipe);
303    struct nouveau_channel *chan = nv50->screen->base.channel;
304    struct nv50_query *q;
305
306    if (!pq) {
307       BEGIN_RING(chan, RING_3D(COND_MODE), 1);
308       OUT_RING  (chan, NV50_3D_COND_MODE_ALWAYS);
309       return;
310    }
311    q = nv50_query(pq);
312
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);
316       OUT_RING  (chan, 0);
317    }
318
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);
324 }
325
326 void
327 nv50_init_query_functions(struct nv50_context *nv50)
328 {
329    struct pipe_context *pipe = &nv50->base.pipe;
330
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;
337 }