intel: Add performance debug for some common GPU stalls.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_queryobj.c
1 /*
2 * Copyright © 2008 Intel Corporation
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
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 OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 /** @file support for ARB_query_object
29 *
30 * ARB_query_object is implemented by using the PIPE_CONTROL command to stall
31 * execution on the completion of previous depth tests, and write the
32 * current PS_DEPTH_COUNT to a buffer object.
33 *
34 * We use before and after counts when drawing during a query so that
35 * we don't pick up other clients' query data in ours. To reduce overhead,
36 * a single BO is used to record the query data for all active queries at
37 * once. This also gives us a simple bound on how much batchbuffer space is
38 * required for handling queries, so that we can be sure that we won't
39 * have to emit a batchbuffer without getting the ending PS_DEPTH_COUNT.
40 */
41 #include "main/imports.h"
42
43 #include "brw_context.h"
44 #include "brw_state.h"
45 #include "intel_batchbuffer.h"
46 #include "intel_reg.h"
47
48 static void
49 write_timestamp(struct intel_context *intel, drm_intel_bo *query_bo, int idx)
50 {
51 if (intel->gen >= 6) {
52 /* Emit workaround flushes: */
53 if (intel->gen == 6) {
54 /* The timestamp write below is a non-zero post-sync op, which on
55 * Gen6 necessitates a CS stall. CS stalls need stall at scoreboard
56 * set. See the comments for intel_emit_post_sync_nonzero_flush().
57 */
58 BEGIN_BATCH(4);
59 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
60 OUT_BATCH(PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD);
61 OUT_BATCH(0);
62 OUT_BATCH(0);
63 ADVANCE_BATCH();
64 }
65
66 BEGIN_BATCH(5);
67 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
68 OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP);
69 OUT_RELOC(query_bo,
70 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
71 PIPE_CONTROL_GLOBAL_GTT_WRITE |
72 idx * sizeof(uint64_t));
73 OUT_BATCH(0);
74 OUT_BATCH(0);
75 ADVANCE_BATCH();
76 } else {
77 BEGIN_BATCH(4);
78 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2) |
79 PIPE_CONTROL_WRITE_TIMESTAMP);
80 OUT_RELOC(query_bo,
81 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
82 PIPE_CONTROL_GLOBAL_GTT_WRITE |
83 idx * sizeof(uint64_t));
84 OUT_BATCH(0);
85 OUT_BATCH(0);
86 ADVANCE_BATCH();
87 }
88 }
89
90 static void
91 write_depth_count(struct intel_context *intel, drm_intel_bo *query_bo, int idx)
92 {
93 if (intel->gen >= 6) {
94 /* Emit Sandybridge workaround flush: */
95 if (intel->gen == 6)
96 intel_emit_post_sync_nonzero_flush(intel);
97
98 BEGIN_BATCH(5);
99 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
100 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL |
101 PIPE_CONTROL_WRITE_DEPTH_COUNT);
102 OUT_RELOC(query_bo,
103 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
104 PIPE_CONTROL_GLOBAL_GTT_WRITE |
105 (idx * sizeof(uint64_t)));
106 OUT_BATCH(0);
107 OUT_BATCH(0);
108 ADVANCE_BATCH();
109 } else {
110 BEGIN_BATCH(4);
111 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2) |
112 PIPE_CONTROL_DEPTH_STALL |
113 PIPE_CONTROL_WRITE_DEPTH_COUNT);
114 /* This object could be mapped cacheable, but we don't have an exposed
115 * mechanism to support that. Since it's going uncached, tell GEM that
116 * we're writing to it. The usual clflush should be all that's required
117 * to pick up the results.
118 */
119 OUT_RELOC(query_bo,
120 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
121 PIPE_CONTROL_GLOBAL_GTT_WRITE |
122 (idx * sizeof(uint64_t)));
123 OUT_BATCH(0);
124 OUT_BATCH(0);
125 ADVANCE_BATCH();
126 }
127 }
128
129 /** Waits on the query object's BO and totals the results for this query */
130 static void
131 brw_queryobj_get_results(struct gl_context *ctx,
132 struct brw_query_object *query)
133 {
134 struct intel_context *intel = intel_context(ctx);
135
136 int i;
137 uint64_t *results;
138
139 if (query->bo == NULL)
140 return;
141
142 if (unlikely(INTEL_DEBUG & DEBUG_PERF)) {
143 if (drm_intel_bo_busy(query->bo)) {
144 perf_debug("Stalling on the GPU waiting for a query object.\n");
145 }
146 }
147
148 drm_intel_bo_map(query->bo, false);
149 results = query->bo->virtual;
150 switch (query->Base.Target) {
151 case GL_TIME_ELAPSED_EXT:
152 if (intel->gen >= 6)
153 query->Base.Result += 80 * (results[1] - results[0]);
154 else
155 query->Base.Result += 1000 * ((results[1] >> 32) - (results[0] >> 32));
156 break;
157
158 case GL_SAMPLES_PASSED_ARB:
159 /* Map and count the pixels from the current query BO */
160 for (i = query->first_index; i <= query->last_index; i++) {
161 query->Base.Result += results[i * 2 + 1] - results[i * 2];
162 }
163 break;
164
165 case GL_PRIMITIVES_GENERATED:
166 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
167 /* We don't actually query the hardware for this value, so query->bo
168 * should always be NULL and execution should never reach here.
169 */
170 assert(!"Unreachable");
171 break;
172
173 default:
174 assert(!"Unrecognized query target in brw_queryobj_get_results()");
175 break;
176 }
177 drm_intel_bo_unmap(query->bo);
178
179 drm_intel_bo_unreference(query->bo);
180 query->bo = NULL;
181 }
182
183 static struct gl_query_object *
184 brw_new_query_object(struct gl_context *ctx, GLuint id)
185 {
186 struct brw_query_object *query;
187
188 query = calloc(1, sizeof(struct brw_query_object));
189
190 query->Base.Id = id;
191 query->Base.Result = 0;
192 query->Base.Active = false;
193 query->Base.Ready = true;
194
195 return &query->Base;
196 }
197
198 static void
199 brw_delete_query(struct gl_context *ctx, struct gl_query_object *q)
200 {
201 struct brw_query_object *query = (struct brw_query_object *)q;
202
203 drm_intel_bo_unreference(query->bo);
204 free(query);
205 }
206
207 static void
208 brw_begin_query(struct gl_context *ctx, struct gl_query_object *q)
209 {
210 struct brw_context *brw = brw_context(ctx);
211 struct intel_context *intel = intel_context(ctx);
212 struct brw_query_object *query = (struct brw_query_object *)q;
213
214 switch (query->Base.Target) {
215 case GL_TIME_ELAPSED_EXT:
216 drm_intel_bo_unreference(query->bo);
217 query->bo = drm_intel_bo_alloc(intel->bufmgr, "timer query", 4096, 4096);
218 write_timestamp(intel, query->bo, 0);
219 break;
220
221 case GL_SAMPLES_PASSED_ARB:
222 /* Reset our driver's tracking of query state. */
223 drm_intel_bo_unreference(query->bo);
224 query->bo = NULL;
225 query->first_index = -1;
226 query->last_index = -1;
227
228 brw->query.obj = query;
229 intel->stats_wm++;
230 break;
231
232 case GL_PRIMITIVES_GENERATED:
233 /* We don't actually query the hardware for this value; we keep track of
234 * it a software counter. So just reset the counter.
235 */
236 brw->sol.primitives_generated = 0;
237 brw->sol.counting_primitives_generated = true;
238 break;
239
240 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
241 /* We don't actually query the hardware for this value; we keep track of
242 * it a software counter. So just reset the counter.
243 */
244 brw->sol.primitives_written = 0;
245 brw->sol.counting_primitives_written = true;
246 break;
247
248 default:
249 assert(!"Unrecognized query target in brw_begin_query()");
250 break;
251 }
252 }
253
254 /**
255 * Begin the ARB_occlusion_query query on a query object.
256 */
257 static void
258 brw_end_query(struct gl_context *ctx, struct gl_query_object *q)
259 {
260 struct brw_context *brw = brw_context(ctx);
261 struct intel_context *intel = intel_context(ctx);
262 struct brw_query_object *query = (struct brw_query_object *)q;
263
264 switch (query->Base.Target) {
265 case GL_TIME_ELAPSED_EXT:
266 write_timestamp(intel, query->bo, 1);
267 intel_batchbuffer_flush(intel);
268 break;
269
270 case GL_SAMPLES_PASSED_ARB:
271 /* Flush the batchbuffer in case it has writes to our query BO.
272 * Have later queries write to a new query BO so that further rendering
273 * doesn't delay the collection of our results.
274 */
275 if (query->bo) {
276 brw_emit_query_end(brw);
277 intel_batchbuffer_flush(intel);
278
279 drm_intel_bo_unreference(brw->query.bo);
280 brw->query.bo = NULL;
281 }
282
283 brw->query.obj = NULL;
284
285 intel->stats_wm--;
286 break;
287
288 case GL_PRIMITIVES_GENERATED:
289 /* We don't actually query the hardware for this value; we keep track of
290 * it in a software counter. So just read the counter and store it in
291 * the query object.
292 */
293 query->Base.Result = brw->sol.primitives_generated;
294 brw->sol.counting_primitives_generated = false;
295
296 /* And set brw->query.obj to NULL so that this query won't try to wait
297 * for any rendering to complete.
298 */
299 query->bo = NULL;
300 break;
301
302 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
303 /* We don't actually query the hardware for this value; we keep track of
304 * it in a software counter. So just read the counter and store it in
305 * the query object.
306 */
307 query->Base.Result = brw->sol.primitives_written;
308 brw->sol.counting_primitives_written = false;
309
310 /* And set brw->query.obj to NULL so that this query won't try to wait
311 * for any rendering to complete.
312 */
313 query->bo = NULL;
314 break;
315
316 default:
317 assert(!"Unrecognized query target in brw_end_query()");
318 break;
319 }
320 }
321
322 static void brw_wait_query(struct gl_context *ctx, struct gl_query_object *q)
323 {
324 struct brw_query_object *query = (struct brw_query_object *)q;
325
326 brw_queryobj_get_results(ctx, query);
327 query->Base.Ready = true;
328 }
329
330 static void brw_check_query(struct gl_context *ctx, struct gl_query_object *q)
331 {
332 struct brw_query_object *query = (struct brw_query_object *)q;
333
334 if (query->bo == NULL || !drm_intel_bo_busy(query->bo)) {
335 brw_queryobj_get_results(ctx, query);
336 query->Base.Ready = true;
337 }
338 }
339
340 /** Called to set up the query BO and account for its aperture space */
341 void
342 brw_prepare_query_begin(struct brw_context *brw)
343 {
344 struct intel_context *intel = &brw->intel;
345
346 /* Skip if we're not doing any queries. */
347 if (!brw->query.obj)
348 return;
349
350 /* Get a new query BO if we're going to need it. */
351 if (brw->query.bo == NULL ||
352 brw->query.index * 2 + 1 >= 4096 / sizeof(uint64_t)) {
353 drm_intel_bo_unreference(brw->query.bo);
354 brw->query.bo = NULL;
355
356 brw->query.bo = drm_intel_bo_alloc(intel->bufmgr, "query", 4096, 1);
357
358 /* clear target buffer */
359 drm_intel_bo_map(brw->query.bo, true);
360 memset((char *)brw->query.bo->virtual, 0, 4096);
361 drm_intel_bo_unmap(brw->query.bo);
362
363 brw->query.index = 0;
364 }
365 }
366
367 /** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
368 void
369 brw_emit_query_begin(struct brw_context *brw)
370 {
371 struct intel_context *intel = &brw->intel;
372 struct gl_context *ctx = &intel->ctx;
373 struct brw_query_object *query = brw->query.obj;
374
375 /* Skip if we're not doing any queries, or we've emitted the start. */
376 if (!query || brw->query.active)
377 return;
378
379 write_depth_count(intel, brw->query.bo, brw->query.index * 2);
380
381 if (query->bo != brw->query.bo) {
382 if (query->bo != NULL)
383 brw_queryobj_get_results(ctx, query);
384 drm_intel_bo_reference(brw->query.bo);
385 query->bo = brw->query.bo;
386 query->first_index = brw->query.index;
387 }
388 query->last_index = brw->query.index;
389 brw->query.active = true;
390 }
391
392 /** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
393 void
394 brw_emit_query_end(struct brw_context *brw)
395 {
396 struct intel_context *intel = &brw->intel;
397
398 if (!brw->query.active)
399 return;
400
401 write_depth_count(intel, brw->query.bo, brw->query.index * 2 + 1);
402
403 brw->query.active = false;
404 brw->query.index++;
405 }
406
407 void brw_init_queryobj_functions(struct dd_function_table *functions)
408 {
409 functions->NewQueryObject = brw_new_query_object;
410 functions->DeleteQuery = brw_delete_query;
411 functions->BeginQuery = brw_begin_query;
412 functions->EndQuery = brw_end_query;
413 functions->CheckQuery = brw_check_query;
414 functions->WaitQuery = brw_wait_query;
415 }