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 * 16 */
48 struct nouveau_mm_allocation
*mm
;
51 #define NV50_QUERY_ALLOC_SPACE 128
53 static INLINE
struct nv50_query
*
54 nv50_query(struct pipe_query
*pipe
)
56 return (struct nv50_query
*)pipe
;
60 nv50_query_allocate(struct nv50_context
*nv50
, struct nv50_query
*q
, int size
)
62 struct nv50_screen
*screen
= nv50
->screen
;
66 nouveau_bo_ref(NULL
, &q
->bo
);
69 nouveau_mm_free(q
->mm
);
71 nouveau_fence_work(screen
->base
.fence
.current
, nouveau_mm_free_work
,
76 q
->mm
= nouveau_mm_allocate(screen
->base
.mm_GART
, size
, &q
->bo
, &q
->base
);
81 ret
= nouveau_bo_map(q
->bo
, 0, screen
->base
.client
);
83 nv50_query_allocate(nv50
, q
, 0);
86 q
->data
= (uint32_t *)((uint8_t *)q
->bo
->map
+ q
->base
);
92 nv50_query_destroy(struct pipe_context
*pipe
, struct pipe_query
*pq
)
94 nv50_query_allocate(nv50_context(pipe
), nv50_query(pq
), 0);
98 static struct pipe_query
*
99 nv50_query_create(struct pipe_context
*pipe
, unsigned type
, unsigned index
)
101 struct nv50_context
*nv50
= nv50_context(pipe
);
102 struct nv50_query
*q
;
104 q
= CALLOC_STRUCT(nv50_query
);
108 if (!nv50_query_allocate(nv50
, q
, NV50_QUERY_ALLOC_SPACE
)) {
113 q
->is64bit
= (type
== PIPE_QUERY_PRIMITIVES_GENERATED
||
114 type
== PIPE_QUERY_PRIMITIVES_EMITTED
||
115 type
== PIPE_QUERY_SO_STATISTICS
);
118 if (q
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
120 q
->data
-= 16 / sizeof(*q
->data
); /* we advance before query_begin ! */
123 return (struct pipe_query
*)q
;
127 nv50_query_get(struct nouveau_pushbuf
*push
, struct nv50_query
*q
,
128 unsigned offset
, uint32_t get
)
133 PUSH_REFN (push
, q
->bo
, NOUVEAU_BO_GART
| NOUVEAU_BO_WR
);
134 BEGIN_NV04(push
, NV50_3D(QUERY_ADDRESS_HIGH
), 4);
135 PUSH_DATAh(push
, q
->bo
->offset
+ offset
);
136 PUSH_DATA (push
, q
->bo
->offset
+ offset
);
137 PUSH_DATA (push
, q
->sequence
);
138 PUSH_DATA (push
, get
);
142 nv50_query_begin(struct pipe_context
*pipe
, struct pipe_query
*pq
)
144 struct nv50_context
*nv50
= nv50_context(pipe
);
145 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
146 struct nv50_query
*q
= nv50_query(pq
);
148 /* For occlusion queries we have to change the storage, because a previous
149 * query might set the initial render conition to FALSE even *after* we re-
150 * initialized it to TRUE.
152 if (q
->type
== PIPE_QUERY_OCCLUSION_COUNTER
) {
154 q
->data
+= 16 / sizeof(*q
->data
);
155 if (q
->offset
- q
->base
== NV50_QUERY_ALLOC_SPACE
)
156 nv50_query_allocate(nv50
, q
, NV50_QUERY_ALLOC_SPACE
);
158 /* XXX: can we do this with the GPU, and sync with respect to a previous
161 q
->data
[1] = 1; /* initial render condition = TRUE */
164 q
->data
[0] = q
->sequence
++; /* the previously used one */
167 case PIPE_QUERY_OCCLUSION_COUNTER
:
169 BEGIN_NV04(push
, NV50_3D(COUNTER_RESET
), 1);
170 PUSH_DATA (push
, NV50_3D_COUNTER_RESET_SAMPLECNT
);
171 BEGIN_NV04(push
, NV50_3D(SAMPLECNT_ENABLE
), 1);
174 case PIPE_QUERY_PRIMITIVES_GENERATED
:
175 nv50_query_get(push
, q
, 0x10, 0x06805002);
177 case PIPE_QUERY_PRIMITIVES_EMITTED
:
178 nv50_query_get(push
, q
, 0x10, 0x05805002);
180 case PIPE_QUERY_SO_STATISTICS
:
181 nv50_query_get(push
, q
, 0x20, 0x05805002);
182 nv50_query_get(push
, q
, 0x30, 0x06805002);
184 case PIPE_QUERY_TIME_ELAPSED
:
185 nv50_query_get(push
, q
, 0x10, 0x00005002);
194 nv50_query_end(struct pipe_context
*pipe
, struct pipe_query
*pq
)
196 struct nv50_context
*nv50
= nv50_context(pipe
);
197 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
198 struct nv50_query
*q
= nv50_query(pq
);
201 case PIPE_QUERY_OCCLUSION_COUNTER
:
202 nv50_query_get(push
, q
, 0, 0x0100f002);
204 BEGIN_NV04(push
, NV50_3D(SAMPLECNT_ENABLE
), 1);
207 case PIPE_QUERY_PRIMITIVES_GENERATED
:
208 nv50_query_get(push
, q
, 0, 0x06805002);
210 case PIPE_QUERY_PRIMITIVES_EMITTED
:
211 nv50_query_get(push
, q
, 0, 0x05805002);
213 case PIPE_QUERY_SO_STATISTICS
:
214 nv50_query_get(push
, q
, 0x00, 0x05805002);
215 nv50_query_get(push
, q
, 0x10, 0x06805002);
217 case PIPE_QUERY_TIMESTAMP
:
220 case PIPE_QUERY_TIME_ELAPSED
:
221 nv50_query_get(push
, q
, 0, 0x00005002);
223 case PIPE_QUERY_GPU_FINISHED
:
225 nv50_query_get(push
, q
, 0, 0x1000f010);
227 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET
:
228 nv50_query_get(push
, q
, 0, 0x0d005002 | (q
->index
<< 5));
230 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
236 q
->ready
= q
->flushed
= FALSE
;
239 static INLINE boolean
240 nv50_query_ready(struct nv50_query
*q
)
242 return q
->ready
|| (!q
->is64bit
&& (q
->data
[0] == q
->sequence
));
246 nv50_query_result(struct pipe_context
*pipe
, struct pipe_query
*pq
,
247 boolean wait
, union pipe_query_result
*result
)
249 struct nv50_context
*nv50
= nv50_context(pipe
);
250 struct nv50_query
*q
= nv50_query(pq
);
251 uint64_t *res64
= (uint64_t *)result
;
252 uint32_t *res32
= (uint32_t *)result
;
253 boolean
*res8
= (boolean
*)result
;
254 uint64_t *data64
= (uint64_t *)q
->data
;
256 if (!q
->ready
) /* update ? */
257 q
->ready
= nv50_query_ready(q
);
260 /* for broken apps that spin on GL_QUERY_RESULT_AVAILABLE */
263 PUSH_KICK(nv50
->base
.pushbuf
);
267 if (nouveau_bo_wait(q
->bo
, NOUVEAU_BO_RD
, nv50
->screen
->base
.client
))
273 case PIPE_QUERY_GPU_FINISHED
:
276 case PIPE_QUERY_OCCLUSION_COUNTER
: /* u32 sequence, u32 count, u64 time */
277 res64
[0] = q
->data
[1];
279 case PIPE_QUERY_PRIMITIVES_GENERATED
: /* u64 count, u64 time */
280 case PIPE_QUERY_PRIMITIVES_EMITTED
: /* u64 count, u64 time */
281 res64
[0] = data64
[0] - data64
[2];
283 case PIPE_QUERY_SO_STATISTICS
:
284 res64
[0] = data64
[0] - data64
[4];
285 res64
[1] = data64
[2] - data64
[6];
287 case PIPE_QUERY_TIMESTAMP
:
288 res64
[0] = data64
[1];
290 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
291 res64
[0] = 1000000000;
294 case PIPE_QUERY_TIME_ELAPSED
:
295 res64
[0] = data64
[1] - data64
[3];
297 case NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET
:
298 res32
[0] = q
->data
[1];
308 nv84_query_fifo_wait(struct nouveau_pushbuf
*push
, struct pipe_query
*pq
)
310 struct nv50_query
*q
= nv50_query(pq
);
311 unsigned offset
= q
->offset
;
314 PUSH_REFN (push
, q
->bo
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
);
315 BEGIN_NV04(push
, SUBC_3D(NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH
), 4);
316 PUSH_DATAh(push
, q
->bo
->offset
+ offset
);
317 PUSH_DATA (push
, q
->bo
->offset
+ offset
);
318 PUSH_DATA (push
, q
->sequence
);
319 PUSH_DATA (push
, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL
);
323 nv50_render_condition(struct pipe_context
*pipe
,
324 struct pipe_query
*pq
,
325 boolean condition
, uint mode
)
327 struct nv50_context
*nv50
= nv50_context(pipe
);
328 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
329 struct nv50_query
*q
;
331 nv50
->cond_query
= pq
;
332 nv50
->cond_cond
= condition
;
333 nv50
->cond_mode
= mode
;
338 BEGIN_NV04(push
, NV50_3D(COND_MODE
), 1);
339 PUSH_DATA (push
, NV50_3D_COND_MODE_ALWAYS
);
344 if (mode
== PIPE_RENDER_COND_WAIT
||
345 mode
== PIPE_RENDER_COND_BY_REGION_WAIT
) {
346 BEGIN_NV04(push
, SUBC_3D(NV50_GRAPH_SERIALIZE
), 1);
350 BEGIN_NV04(push
, NV50_3D(COND_ADDRESS_HIGH
), 3);
351 PUSH_DATAh(push
, q
->bo
->offset
+ q
->offset
);
352 PUSH_DATA (push
, q
->bo
->offset
+ q
->offset
);
353 PUSH_DATA (push
, NV50_3D_COND_MODE_RES_NON_ZERO
);
355 BEGIN_NV04(push
, NV50_2D(COND_ADDRESS_HIGH
), 2);
356 PUSH_DATAh(push
, q
->bo
->offset
+ q
->offset
);
357 PUSH_DATA (push
, q
->bo
->offset
+ q
->offset
);
361 nv50_query_pushbuf_submit(struct nouveau_pushbuf
*push
,
362 struct pipe_query
*pq
, unsigned result_offset
)
364 struct nv50_query
*q
= nv50_query(pq
);
366 /* XXX: does this exist ? */
367 #define NV50_IB_ENTRY_1_NO_PREFETCH (0 << (31 - 8))
369 nouveau_pushbuf_space(push
, 0, 0, 1);
370 nouveau_pushbuf_data(push
, q
->bo
, q
->offset
+ result_offset
, 4 |
371 NV50_IB_ENTRY_1_NO_PREFETCH
);
375 nva0_so_target_save_offset(struct pipe_context
*pipe
,
376 struct pipe_stream_output_target
*ptarg
,
377 unsigned index
, boolean serialize
)
379 struct nv50_so_target
*targ
= nv50_so_target(ptarg
);
382 struct nouveau_pushbuf
*push
= nv50_context(pipe
)->base
.pushbuf
;
384 BEGIN_NV04(push
, SUBC_3D(NV50_GRAPH_SERIALIZE
), 1);
388 nv50_query(targ
->pq
)->index
= index
;
389 nv50_query_end(pipe
, targ
->pq
);
393 nv50_init_query_functions(struct nv50_context
*nv50
)
395 struct pipe_context
*pipe
= &nv50
->base
.pipe
;
397 pipe
->create_query
= nv50_query_create
;
398 pipe
->destroy_query
= nv50_query_destroy
;
399 pipe
->begin_query
= nv50_query_begin
;
400 pipe
->end_query
= nv50_query_end
;
401 pipe
->get_query_result
= nv50_query_result
;
402 pipe
->render_condition
= nv50_render_condition
;