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