2 * Copyright 2011 Christoph Bumiller
3 * Copyright 2015 Samuel Pitoiset
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 #define NV50_PUSH_EXPLICIT_SPACE_CHECKING
26 #include "nv50/nv50_context.h"
27 #include "nv50/nv50_query_hw.h"
28 #include "nv_object.xml.h"
30 #define NV50_HW_QUERY_STATE_READY 0
31 #define NV50_HW_QUERY_STATE_ACTIVE 1
32 #define NV50_HW_QUERY_STATE_ENDED 2
33 #define NV50_HW_QUERY_STATE_FLUSHED 3
35 /* XXX: Nested queries, and simultaneous queries on multiple gallium contexts
36 * (since we use only a single GPU channel per screen) will not work properly.
38 * The first is not that big of an issue because OpenGL does not allow nested
42 #define NV50_HW_QUERY_ALLOC_SPACE 256
45 nv50_hw_query_allocate(struct nv50_context
*nv50
, struct nv50_query
*q
,
48 struct nv50_screen
*screen
= nv50
->screen
;
49 struct nv50_hw_query
*hq
= nv50_hw_query(q
);
53 nouveau_bo_ref(NULL
, &hq
->bo
);
55 if (hq
->state
== NV50_HW_QUERY_STATE_READY
)
56 nouveau_mm_free(hq
->mm
);
58 nouveau_fence_work(screen
->base
.fence
.current
,
59 nouveau_mm_free_work
, hq
->mm
);
63 hq
->mm
= nouveau_mm_allocate(screen
->base
.mm_GART
, size
,
64 &hq
->bo
, &hq
->base_offset
);
67 hq
->offset
= hq
->base_offset
;
69 ret
= nouveau_bo_map(hq
->bo
, 0, screen
->base
.client
);
71 nv50_hw_query_allocate(nv50
, q
, 0);
74 hq
->data
= (uint32_t *)((uint8_t *)hq
->bo
->map
+ hq
->base_offset
);
80 nv50_hw_query_get(struct nouveau_pushbuf
*push
, struct nv50_query
*q
,
81 unsigned offset
, uint32_t get
)
83 struct nv50_hw_query
*hq
= nv50_hw_query(q
);
88 PUSH_REFN (push
, hq
->bo
, NOUVEAU_BO_GART
| NOUVEAU_BO_WR
);
89 BEGIN_NV04(push
, NV50_3D(QUERY_ADDRESS_HIGH
), 4);
90 PUSH_DATAh(push
, hq
->bo
->offset
+ offset
);
91 PUSH_DATA (push
, hq
->bo
->offset
+ offset
);
92 PUSH_DATA (push
, hq
->sequence
);
93 PUSH_DATA (push
, get
);
97 nv50_hw_query_update(struct nv50_query
*q
)
99 struct nv50_hw_query
*hq
= nv50_hw_query(q
);
102 if (nouveau_fence_signalled(hq
->fence
))
103 hq
->state
= NV50_HW_QUERY_STATE_READY
;
105 if (hq
->data
[0] == hq
->sequence
)
106 hq
->state
= NV50_HW_QUERY_STATE_READY
;
111 nv50_hw_destroy_query(struct nv50_context
*nv50
, struct nv50_query
*q
)
113 struct nv50_hw_query
*hq
= nv50_hw_query(q
);
114 nv50_hw_query_allocate(nv50
, q
, 0);
115 nouveau_fence_ref(NULL
, &hq
->fence
);
120 nv50_hw_begin_query(struct nv50_context
*nv50
, struct nv50_query
*q
)
122 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
123 struct nv50_hw_query
*hq
= nv50_hw_query(q
);
125 /* For occlusion queries we have to change the storage, because a previous
126 * query might set the initial render condition to false even *after* we re-
127 * initialized it to true.
130 hq
->offset
+= hq
->rotate
;
131 hq
->data
+= hq
->rotate
/ sizeof(*hq
->data
);
132 if (hq
->offset
- hq
->base_offset
== NV50_HW_QUERY_ALLOC_SPACE
)
133 nv50_hw_query_allocate(nv50
, q
, NV50_HW_QUERY_ALLOC_SPACE
);
135 /* XXX: can we do this with the GPU, and sync with respect to a previous
138 hq
->data
[0] = hq
->sequence
; /* initialize sequence */
139 hq
->data
[1] = 1; /* initial render condition = true */
140 hq
->data
[4] = hq
->sequence
+ 1; /* for comparison COND_MODE */
144 hq
->data
[0] = hq
->sequence
++; /* the previously used one */
147 case PIPE_QUERY_OCCLUSION_COUNTER
:
148 hq
->nesting
= nv50
->screen
->num_occlusion_queries_active
++;
150 nv50_hw_query_get(push
, q
, 0x10, 0x0100f002);
153 BEGIN_NV04(push
, NV50_3D(COUNTER_RESET
), 1);
154 PUSH_DATA (push
, NV50_3D_COUNTER_RESET_SAMPLECNT
);
155 BEGIN_NV04(push
, NV50_3D(SAMPLECNT_ENABLE
), 1);
159 case PIPE_QUERY_PRIMITIVES_GENERATED
:
160 nv50_hw_query_get(push
, q
, 0x10, 0x06805002);
162 case PIPE_QUERY_PRIMITIVES_EMITTED
:
163 nv50_hw_query_get(push
, q
, 0x10, 0x05805002);
165 case PIPE_QUERY_SO_STATISTICS
:
166 nv50_hw_query_get(push
, q
, 0x20, 0x05805002);
167 nv50_hw_query_get(push
, q
, 0x30, 0x06805002);
169 case PIPE_QUERY_PIPELINE_STATISTICS
:
170 nv50_hw_query_get(push
, q
, 0x80, 0x00801002); /* VFETCH, VERTICES */
171 nv50_hw_query_get(push
, q
, 0x90, 0x01801002); /* VFETCH, PRIMS */
172 nv50_hw_query_get(push
, q
, 0xa0, 0x02802002); /* VP, LAUNCHES */
173 nv50_hw_query_get(push
, q
, 0xb0, 0x03806002); /* GP, LAUNCHES */
174 nv50_hw_query_get(push
, q
, 0xc0, 0x04806002); /* GP, PRIMS_OUT */
175 nv50_hw_query_get(push
, q
, 0xd0, 0x07804002); /* RAST, PRIMS_IN */
176 nv50_hw_query_get(push
, q
, 0xe0, 0x08804002); /* RAST, PRIMS_OUT */
177 nv50_hw_query_get(push
, q
, 0xf0, 0x0980a002); /* ROP, PIXELS */
179 case PIPE_QUERY_TIME_ELAPSED
:
180 nv50_hw_query_get(push
, q
, 0x10, 0x00005002);
186 hq
->state
= NV50_HW_QUERY_STATE_ACTIVE
;
191 nv50_hw_end_query(struct nv50_context
*nv50
, struct nv50_query
*q
)
193 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
194 struct nv50_hw_query
*hq
= nv50_hw_query(q
);
196 hq
->state
= NV50_HW_QUERY_STATE_ENDED
;
199 case PIPE_QUERY_OCCLUSION_COUNTER
:
200 nv50_hw_query_get(push
, q
, 0, 0x0100f002);
201 if (--nv50
->screen
->num_occlusion_queries_active
== 0) {
203 BEGIN_NV04(push
, NV50_3D(SAMPLECNT_ENABLE
), 1);
207 case PIPE_QUERY_PRIMITIVES_GENERATED
:
208 nv50_hw_query_get(push
, q
, 0, 0x06805002);
210 case PIPE_QUERY_PRIMITIVES_EMITTED
:
211 nv50_hw_query_get(push
, q
, 0, 0x05805002);
213 case PIPE_QUERY_SO_STATISTICS
:
214 nv50_hw_query_get(push
, q
, 0x00, 0x05805002);
215 nv50_hw_query_get(push
, q
, 0x10, 0x06805002);
217 case PIPE_QUERY_PIPELINE_STATISTICS
:
218 nv50_hw_query_get(push
, q
, 0x00, 0x00801002); /* VFETCH, VERTICES */
219 nv50_hw_query_get(push
, q
, 0x10, 0x01801002); /* VFETCH, PRIMS */
220 nv50_hw_query_get(push
, q
, 0x20, 0x02802002); /* VP, LAUNCHES */
221 nv50_hw_query_get(push
, q
, 0x30, 0x03806002); /* GP, LAUNCHES */
222 nv50_hw_query_get(push
, q
, 0x40, 0x04806002); /* GP, PRIMS_OUT */
223 nv50_hw_query_get(push
, q
, 0x50, 0x07804002); /* RAST, PRIMS_IN */
224 nv50_hw_query_get(push
, q
, 0x60, 0x08804002); /* RAST, PRIMS_OUT */
225 nv50_hw_query_get(push
, q
, 0x70, 0x0980a002); /* ROP, PIXELS */
227 case PIPE_QUERY_TIMESTAMP
:
230 case PIPE_QUERY_TIME_ELAPSED
:
231 nv50_hw_query_get(push
, q
, 0, 0x00005002);
233 case PIPE_QUERY_GPU_FINISHED
:
235 nv50_hw_query_get(push
, q
, 0, 0x1000f010);
237 case NVA0_HW_QUERY_STREAM_OUTPUT_BUFFER_OFFSET
:
239 nv50_hw_query_get(push
, q
, 0, 0x0d005002 | (q
->index
<< 5));
241 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
242 /* This query is not issued on GPU because disjoint is forced to false */
243 hq
->state
= NV50_HW_QUERY_STATE_READY
;
250 nouveau_fence_ref(nv50
->screen
->base
.fence
.current
, &hq
->fence
);
254 nv50_hw_get_query_result(struct nv50_context
*nv50
, struct nv50_query
*q
,
255 boolean wait
, union pipe_query_result
*result
)
257 struct nv50_hw_query
*hq
= nv50_hw_query(q
);
258 uint64_t *res64
= (uint64_t *)result
;
259 uint32_t *res32
= (uint32_t *)result
;
260 uint8_t *res8
= (uint8_t *)result
;
261 uint64_t *data64
= (uint64_t *)hq
->data
;
264 if (hq
->state
!= NV50_HW_QUERY_STATE_READY
)
265 nv50_hw_query_update(q
);
267 if (hq
->state
!= NV50_HW_QUERY_STATE_READY
) {
269 /* for broken apps that spin on GL_QUERY_RESULT_AVAILABLE */
270 if (hq
->state
!= NV50_HW_QUERY_STATE_FLUSHED
) {
271 hq
->state
= NV50_HW_QUERY_STATE_FLUSHED
;
272 PUSH_KICK(nv50
->base
.pushbuf
);
276 if (nouveau_bo_wait(hq
->bo
, NOUVEAU_BO_RD
, nv50
->screen
->base
.client
))
279 hq
->state
= NV50_HW_QUERY_STATE_READY
;
282 case PIPE_QUERY_GPU_FINISHED
:
285 case PIPE_QUERY_OCCLUSION_COUNTER
: /* u32 sequence, u32 count, u64 time */
286 res64
[0] = hq
->data
[1] - hq
->data
[5];
288 case PIPE_QUERY_PRIMITIVES_GENERATED
: /* u64 count, u64 time */
289 case PIPE_QUERY_PRIMITIVES_EMITTED
: /* u64 count, u64 time */
290 res64
[0] = data64
[0] - data64
[2];
292 case PIPE_QUERY_SO_STATISTICS
:
293 res64
[0] = data64
[0] - data64
[4];
294 res64
[1] = data64
[2] - data64
[6];
296 case PIPE_QUERY_PIPELINE_STATISTICS
:
297 for (i
= 0; i
< 8; ++i
)
298 res64
[i
] = data64
[i
* 2] - data64
[16 + i
* 2];
300 case PIPE_QUERY_TIMESTAMP
:
301 res64
[0] = data64
[1];
303 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
304 res64
[0] = 1000000000;
307 case PIPE_QUERY_TIME_ELAPSED
:
308 res64
[0] = data64
[1] - data64
[3];
310 case NVA0_HW_QUERY_STREAM_OUTPUT_BUFFER_OFFSET
:
311 res32
[0] = hq
->data
[1];
321 static const struct nv50_query_funcs hw_query_funcs
= {
322 .destroy_query
= nv50_hw_destroy_query
,
323 .begin_query
= nv50_hw_begin_query
,
324 .end_query
= nv50_hw_end_query
,
325 .get_query_result
= nv50_hw_get_query_result
,
329 nv50_hw_create_query(struct nv50_context
*nv50
, unsigned type
, unsigned index
)
331 struct nv50_hw_query
*hq
;
332 struct nv50_query
*q
;
334 hq
= CALLOC_STRUCT(nv50_hw_query
);
339 q
->funcs
= &hw_query_funcs
;
343 case PIPE_QUERY_OCCLUSION_COUNTER
:
346 case PIPE_QUERY_PRIMITIVES_GENERATED
:
347 case PIPE_QUERY_PRIMITIVES_EMITTED
:
348 case PIPE_QUERY_SO_STATISTICS
:
349 case PIPE_QUERY_PIPELINE_STATISTICS
:
352 case PIPE_QUERY_TIME_ELAPSED
:
353 case PIPE_QUERY_TIMESTAMP
:
354 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
355 case PIPE_QUERY_GPU_FINISHED
:
356 case NVA0_HW_QUERY_STREAM_OUTPUT_BUFFER_OFFSET
:
359 debug_printf("invalid query type: %u\n", type
);
364 if (!nv50_hw_query_allocate(nv50
, q
, NV50_HW_QUERY_ALLOC_SPACE
)) {
370 /* we advance before query_begin ! */
371 hq
->offset
-= hq
->rotate
;
372 hq
->data
-= hq
->rotate
/ sizeof(*hq
->data
);
379 nv50_hw_query_pushbuf_submit(struct nouveau_pushbuf
*push
, uint16_t method
,
380 struct nv50_query
*q
, unsigned result_offset
)
382 struct nv50_hw_query
*hq
= nv50_hw_query(q
);
384 nv50_hw_query_update(q
);
385 if (hq
->state
!= NV50_HW_QUERY_STATE_READY
)
386 nouveau_bo_wait(hq
->bo
, NOUVEAU_BO_RD
, push
->client
);
387 hq
->state
= NV50_HW_QUERY_STATE_READY
;
389 BEGIN_NV04(push
, SUBC_3D(method
), 1);
390 PUSH_DATA (push
, hq
->data
[result_offset
/ 4]);
394 nv84_hw_query_fifo_wait(struct nouveau_pushbuf
*push
, struct nv50_query
*q
)
396 struct nv50_hw_query
*hq
= nv50_hw_query(q
);
397 unsigned offset
= hq
->offset
;
400 PUSH_REFN (push
, hq
->bo
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
);
401 BEGIN_NV04(push
, SUBC_3D(NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH
), 4);
402 PUSH_DATAh(push
, hq
->bo
->offset
+ offset
);
403 PUSH_DATA (push
, hq
->bo
->offset
+ offset
);
404 PUSH_DATA (push
, hq
->sequence
);
405 PUSH_DATA (push
, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL
);