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