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