freedreno: add batch debugging
[mesa.git] / src / gallium / drivers / freedreno / freedreno_context.c
1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "freedreno_context.h"
28 #include "freedreno_blitter.h"
29 #include "freedreno_draw.h"
30 #include "freedreno_fence.h"
31 #include "freedreno_log.h"
32 #include "freedreno_program.h"
33 #include "freedreno_resource.h"
34 #include "freedreno_texture.h"
35 #include "freedreno_state.h"
36 #include "freedreno_gmem.h"
37 #include "freedreno_query.h"
38 #include "freedreno_query_hw.h"
39 #include "freedreno_util.h"
40 #include "util/u_upload_mgr.h"
41
42 #if DETECT_OS_ANDROID
43 #include "util/u_process.h"
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #endif
47
48 static void
49 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fencep,
50 unsigned flags)
51 {
52 struct fd_context *ctx = fd_context(pctx);
53 struct pipe_fence_handle *fence = NULL;
54 // TODO we want to lookup batch if it exists, but not create one if not.
55 struct fd_batch *batch = fd_context_batch(ctx);
56
57 DBG("%p: flush: flags=%x\n", ctx->batch, flags);
58
59 /* In some sequence of events, we can end up with a last_fence that is
60 * not an "fd" fence, which results in eglDupNativeFenceFDANDROID()
61 * errors.
62 */
63 if (flags & PIPE_FLUSH_FENCE_FD)
64 fd_fence_ref(&ctx->last_fence, NULL);
65
66 /* if no rendering since last flush, ie. app just decided it needed
67 * a fence, re-use the last one:
68 */
69 if (ctx->last_fence) {
70 fd_fence_ref(&fence, ctx->last_fence);
71 fd_bc_dump(ctx->screen, "%p: reuse last_fence, remaining:\n", ctx);
72 goto out;
73 }
74
75 if (!batch) {
76 fd_bc_dump(ctx->screen, "%p: NULL batch, remaining:\n", ctx);
77 return;
78 }
79
80 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
81 fd_fence_ref(&fence, batch->fence);
82
83 if (flags & PIPE_FLUSH_FENCE_FD)
84 batch->needs_out_fence_fd = true;
85
86 fd_bc_dump(ctx->screen, "%p: flushing %p<%u>, flags=0x%x, pending:\n",
87 ctx, batch, batch->seqno, flags);
88
89 if (!ctx->screen->reorder) {
90 fd_batch_flush(batch);
91 } else if (flags & PIPE_FLUSH_DEFERRED) {
92 fd_bc_flush_deferred(&ctx->screen->batch_cache, ctx);
93 } else {
94 fd_bc_flush(&ctx->screen->batch_cache, ctx);
95 }
96
97 fd_bc_dump(ctx->screen, "%p: remaining:\n", ctx);
98
99 out:
100 if (fencep)
101 fd_fence_ref(fencep, fence);
102
103 fd_fence_ref(&ctx->last_fence, fence);
104
105 fd_fence_ref(&fence, NULL);
106
107 if (flags & PIPE_FLUSH_END_OF_FRAME)
108 fd_log_eof(ctx);
109 }
110
111 static void
112 fd_texture_barrier(struct pipe_context *pctx, unsigned flags)
113 {
114 if (flags == PIPE_TEXTURE_BARRIER_FRAMEBUFFER) {
115 struct fd_context *ctx = fd_context(pctx);
116
117 if (ctx->framebuffer_barrier) {
118 ctx->framebuffer_barrier(ctx);
119 return;
120 }
121 }
122
123 /* On devices that could sample from GMEM we could possibly do better.
124 * Or if we knew that we were doing GMEM bypass we could just emit a
125 * cache flush, perhaps? But we don't know if future draws would cause
126 * us to use GMEM, and a flush in bypass isn't the end of the world.
127 */
128 fd_context_flush(pctx, NULL, 0);
129 }
130
131 static void
132 fd_memory_barrier(struct pipe_context *pctx, unsigned flags)
133 {
134 if (!(flags & ~PIPE_BARRIER_UPDATE))
135 return;
136
137 fd_context_flush(pctx, NULL, 0);
138 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */
139 }
140
141 /**
142 * emit marker string as payload of a no-op packet, which can be
143 * decoded by cffdump.
144 */
145 static void
146 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
147 {
148 struct fd_context *ctx = fd_context(pctx);
149 struct fd_ringbuffer *ring;
150 const uint32_t *buf = (const void *)string;
151
152 if (!ctx->batch)
153 return;
154
155 ctx->batch->needs_flush = true;
156
157 ring = ctx->batch->draw;
158
159 /* max packet size is 0x3fff dwords: */
160 len = MIN2(len, 0x3fff * 4);
161
162 if (ctx->screen->gpu_id >= 500)
163 OUT_PKT7(ring, CP_NOP, align(len, 4) / 4);
164 else
165 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
166 while (len >= 4) {
167 OUT_RING(ring, *buf);
168 buf++;
169 len -= 4;
170 }
171
172 /* copy remainder bytes without reading past end of input string: */
173 if (len > 0) {
174 uint32_t w = 0;
175 memcpy(&w, buf, len);
176 OUT_RING(ring, w);
177 }
178 }
179
180 void
181 fd_context_destroy(struct pipe_context *pctx)
182 {
183 struct fd_context *ctx = fd_context(pctx);
184 unsigned i;
185
186 DBG("");
187
188 fd_screen_lock(ctx->screen);
189 list_del(&ctx->node);
190 fd_screen_unlock(ctx->screen);
191
192 fd_log_process(ctx, true);
193 assert(list_is_empty(&ctx->log_chunks));
194
195 fd_fence_ref(&ctx->last_fence, NULL);
196
197 util_copy_framebuffer_state(&ctx->framebuffer, NULL);
198 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
199 fd_bc_invalidate_context(ctx);
200
201 fd_prog_fini(pctx);
202
203 if (ctx->blitter)
204 util_blitter_destroy(ctx->blitter);
205
206 if (pctx->stream_uploader)
207 u_upload_destroy(pctx->stream_uploader);
208
209 if (ctx->clear_rs_state)
210 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state);
211
212 if (ctx->primconvert)
213 util_primconvert_destroy(ctx->primconvert);
214
215 slab_destroy_child(&ctx->transfer_pool);
216
217 for (i = 0; i < ARRAY_SIZE(ctx->vsc_pipe_bo); i++) {
218 if (!ctx->vsc_pipe_bo[i])
219 break;
220 fd_bo_del(ctx->vsc_pipe_bo[i]);
221 }
222
223 fd_device_del(ctx->dev);
224 fd_pipe_del(ctx->pipe);
225
226 mtx_destroy(&ctx->gmem_lock);
227
228 if (fd_mesa_debug & (FD_DBG_BSTAT | FD_DBG_MSGS)) {
229 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
230 (uint32_t)ctx->stats.batch_total, (uint32_t)ctx->stats.batch_sysmem,
231 (uint32_t)ctx->stats.batch_gmem, (uint32_t)ctx->stats.batch_nondraw,
232 (uint32_t)ctx->stats.batch_restore);
233 }
234 }
235
236 static void
237 fd_set_debug_callback(struct pipe_context *pctx,
238 const struct pipe_debug_callback *cb)
239 {
240 struct fd_context *ctx = fd_context(pctx);
241
242 if (cb)
243 ctx->debug = *cb;
244 else
245 memset(&ctx->debug, 0, sizeof(ctx->debug));
246 }
247
248 static uint32_t
249 fd_get_reset_count(struct fd_context *ctx, bool per_context)
250 {
251 uint64_t val;
252 enum fd_param_id param =
253 per_context ? FD_CTX_FAULTS : FD_GLOBAL_FAULTS;
254 int ret = fd_pipe_get_param(ctx->pipe, param, &val);
255 debug_assert(!ret);
256 return val;
257 }
258
259 static enum pipe_reset_status
260 fd_get_device_reset_status(struct pipe_context *pctx)
261 {
262 struct fd_context *ctx = fd_context(pctx);
263 int context_faults = fd_get_reset_count(ctx, true);
264 int global_faults = fd_get_reset_count(ctx, false);
265 enum pipe_reset_status status;
266
267 if (context_faults != ctx->context_reset_count) {
268 status = PIPE_GUILTY_CONTEXT_RESET;
269 } else if (global_faults != ctx->global_reset_count) {
270 status = PIPE_INNOCENT_CONTEXT_RESET;
271 } else {
272 status = PIPE_NO_RESET;
273 }
274
275 ctx->context_reset_count = context_faults;
276 ctx->global_reset_count = global_faults;
277
278 return status;
279 }
280
281 /* TODO we could combine a few of these small buffers (solid_vbuf,
282 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
283 * save a tiny bit of memory
284 */
285
286 static struct pipe_resource *
287 create_solid_vertexbuf(struct pipe_context *pctx)
288 {
289 static const float init_shader_const[] = {
290 -1.000000, +1.000000, +1.000000,
291 +1.000000, -1.000000, +1.000000,
292 };
293 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
294 PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, sizeof(init_shader_const));
295 pipe_buffer_write(pctx, prsc, 0,
296 sizeof(init_shader_const), init_shader_const);
297 return prsc;
298 }
299
300 static struct pipe_resource *
301 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
302 {
303 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
304 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
305 return prsc;
306 }
307
308 void
309 fd_context_setup_common_vbos(struct fd_context *ctx)
310 {
311 struct pipe_context *pctx = &ctx->base;
312
313 ctx->solid_vbuf = create_solid_vertexbuf(pctx);
314 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
315
316 /* setup solid_vbuf_state: */
317 ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state(
318 pctx, 1, (struct pipe_vertex_element[]){{
319 .vertex_buffer_index = 0,
320 .src_offset = 0,
321 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
322 }});
323 ctx->solid_vbuf_state.vertexbuf.count = 1;
324 ctx->solid_vbuf_state.vertexbuf.vb[0].stride = 12;
325 ctx->solid_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->solid_vbuf;
326
327 /* setup blit_vbuf_state: */
328 ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state(
329 pctx, 2, (struct pipe_vertex_element[]){{
330 .vertex_buffer_index = 0,
331 .src_offset = 0,
332 .src_format = PIPE_FORMAT_R32G32_FLOAT,
333 }, {
334 .vertex_buffer_index = 1,
335 .src_offset = 0,
336 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
337 }});
338 ctx->blit_vbuf_state.vertexbuf.count = 2;
339 ctx->blit_vbuf_state.vertexbuf.vb[0].stride = 8;
340 ctx->blit_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->blit_texcoord_vbuf;
341 ctx->blit_vbuf_state.vertexbuf.vb[1].stride = 12;
342 ctx->blit_vbuf_state.vertexbuf.vb[1].buffer.resource = ctx->solid_vbuf;
343 }
344
345 void
346 fd_context_cleanup_common_vbos(struct fd_context *ctx)
347 {
348 struct pipe_context *pctx = &ctx->base;
349
350 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
351 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx);
352
353 pipe_resource_reference(&ctx->solid_vbuf, NULL);
354 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL);
355 }
356
357 struct pipe_context *
358 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
359 const uint8_t *primtypes, void *priv, unsigned flags)
360 {
361 struct fd_screen *screen = fd_screen(pscreen);
362 struct pipe_context *pctx;
363 unsigned prio = 1;
364 int i;
365
366 /* lower numerical value == higher priority: */
367 if (fd_mesa_debug & FD_DBG_HIPRIO)
368 prio = 0;
369 else if (flags & PIPE_CONTEXT_HIGH_PRIORITY)
370 prio = 0;
371 else if (flags & PIPE_CONTEXT_LOW_PRIORITY)
372 prio = 2;
373
374 ctx->screen = screen;
375 ctx->pipe = fd_pipe_new2(screen->dev, FD_PIPE_3D, prio);
376
377 if (fd_device_version(screen->dev) >= FD_VERSION_ROBUSTNESS) {
378 ctx->context_reset_count = fd_get_reset_count(ctx, true);
379 ctx->global_reset_count = fd_get_reset_count(ctx, false);
380 }
381
382 ctx->primtypes = primtypes;
383 ctx->primtype_mask = 0;
384 for (i = 0; i < PIPE_PRIM_MAX; i++)
385 if (primtypes[i])
386 ctx->primtype_mask |= (1 << i);
387
388 (void) mtx_init(&ctx->gmem_lock, mtx_plain);
389
390 /* need some sane default in case gallium frontends don't
391 * set some state:
392 */
393 ctx->sample_mask = 0xffff;
394 ctx->active_queries = true;
395
396 pctx = &ctx->base;
397 pctx->screen = pscreen;
398 pctx->priv = priv;
399 pctx->flush = fd_context_flush;
400 pctx->emit_string_marker = fd_emit_string_marker;
401 pctx->set_debug_callback = fd_set_debug_callback;
402 pctx->get_device_reset_status = fd_get_device_reset_status;
403 pctx->create_fence_fd = fd_create_fence_fd;
404 pctx->fence_server_sync = fd_fence_server_sync;
405 pctx->texture_barrier = fd_texture_barrier;
406 pctx->memory_barrier = fd_memory_barrier;
407
408 pctx->stream_uploader = u_upload_create_default(pctx);
409 if (!pctx->stream_uploader)
410 goto fail;
411 pctx->const_uploader = pctx->stream_uploader;
412
413 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
414
415 fd_draw_init(pctx);
416 fd_resource_context_init(pctx);
417 fd_query_context_init(pctx);
418 fd_texture_init(pctx);
419 fd_state_init(pctx);
420
421 ctx->blitter = util_blitter_create(pctx);
422 if (!ctx->blitter)
423 goto fail;
424
425 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
426 if (!ctx->primconvert)
427 goto fail;
428
429 list_inithead(&ctx->hw_active_queries);
430 list_inithead(&ctx->acc_active_queries);
431 list_inithead(&ctx->log_chunks);
432
433 fd_screen_lock(ctx->screen);
434 list_add(&ctx->node, &ctx->screen->context_list);
435 fd_screen_unlock(ctx->screen);
436
437 ctx->current_scissor = &ctx->disabled_scissor;
438
439 ctx->log_out = stdout;
440
441 if ((fd_mesa_debug & FD_DBG_LOG) &&
442 !(ctx->record_timestamp && ctx->ts_to_ns)) {
443 printf("logging not supported!\n");
444 fd_mesa_debug &= ~FD_DBG_LOG;
445 }
446
447 #if DETECT_OS_ANDROID
448 if (fd_mesa_debug & FD_DBG_LOG) {
449 static unsigned idx = 0;
450 char *p;
451 asprintf(&p, "/data/fdlog/%s-%d.log", util_get_process_name(), idx++);
452
453 FILE *f = fopen(p, "w");
454 if (f)
455 ctx->log_out = f;
456 }
457 #endif
458
459 return pctx;
460
461 fail:
462 pctx->destroy(pctx);
463 return NULL;
464 }