• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "util/debug.h"
28 #include "pipe/p_state.h"
29 #include "util/hash_table.h"
30 #include "util/u_dump.h"
31 #include "util/u_string.h"
32 #include "util/u_memory.h"
33 #include "util/u_inlines.h"
34 #include "util/format/u_format.h"
35 
36 #include "freedreno_gmem.h"
37 #include "freedreno_context.h"
38 #include "freedreno_fence.h"
39 #include "freedreno_log.h"
40 #include "freedreno_resource.h"
41 #include "freedreno_query_hw.h"
42 #include "freedreno_util.h"
43 
44 /*
45  * GMEM is the small (ie. 256KiB for a200, 512KiB for a220, etc) tile buffer
46  * inside the GPU.  All rendering happens to GMEM.  Larger render targets
47  * are split into tiles that are small enough for the color (and depth and/or
48  * stencil, if enabled) buffers to fit within GMEM.  Before rendering a tile,
49  * if there was not a clear invalidating the previous tile contents, we need
50  * to restore the previous tiles contents (system mem -> GMEM), and after all
51  * the draw calls, before moving to the next tile, we need to save the tile
52  * contents (GMEM -> system mem).
53  *
54  * The code in this file handles dealing with GMEM and tiling.
55  *
56  * The structure of the ringbuffer ends up being:
57  *
58  *     +--<---<-- IB ---<---+---<---+---<---<---<--+
59  *     |                    |       |              |
60  *     v                    ^       ^              ^
61  *   ------------------------------------------------------
62  *     | clear/draw cmds | Tile0 | Tile1 | .... | TileN |
63  *   ------------------------------------------------------
64  *                       ^
65  *                       |
66  *                       address submitted in issueibcmds
67  *
68  * Where the per-tile section handles scissor setup, mem2gmem restore (if
69  * needed), IB to draw cmds earlier in the ringbuffer, and then gmem2mem
70  * resolve.
71  */
72 
73 #ifndef BIN_DEBUG
74 #  define BIN_DEBUG 0
75 #endif
76 
77 /*
78  * GMEM Cache:
79  *
80  * Caches GMEM state based on a given framebuffer state.  The key is
81  * meant to be the minimal set of data that results in a unique gmem
82  * configuration, avoiding multiple keys arriving at the same gmem
83  * state.  For example, the render target format is not part of the
84  * key, only the size per pixel.  And the max_scissor bounds is not
85  * part of they key, only the minx/miny (after clamping to tile
86  * alignment) and width/height.  This ensures that slightly different
87  * max_scissor which would result in the same gmem state, do not
88  * become different keys that map to the same state.
89  */
90 
91 struct gmem_key {
92 	uint16_t minx, miny;
93 	uint16_t width, height;
94 	uint8_t gmem_page_align;      /* alignment in multiples of 0x1000 to reduce key size */
95 	uint8_t nr_cbufs;
96 	uint8_t cbuf_cpp[MAX_RENDER_TARGETS];
97 	uint8_t zsbuf_cpp[2];
98 };
99 
100 static uint32_t
gmem_key_hash(const void * _key)101 gmem_key_hash(const void *_key)
102 {
103 	const struct gmem_key *key = _key;
104 	return _mesa_hash_data(key, sizeof(*key));
105 }
106 
107 static bool
gmem_key_equals(const void * _a,const void * _b)108 gmem_key_equals(const void *_a, const void *_b)
109 {
110 	const struct gmem_key *a = _a;
111 	const struct gmem_key *b = _b;
112 	return memcmp(a, b, sizeof(*a)) == 0;
113 }
114 
115 static void
dump_gmem_key(const struct gmem_key * key)116 dump_gmem_key(const struct gmem_key *key)
117 {
118 	printf("{ .minx=%u, .miny=%u, .width=%u, .height=%u",
119 			key->minx, key->miny, key->width, key->height);
120 	printf(", .gmem_page_align=%u, .nr_cbufs=%u",
121 			key->gmem_page_align, key->nr_cbufs);
122 	printf(", .cbuf_cpp = {");
123 	for (unsigned i = 0; i < ARRAY_SIZE(key->cbuf_cpp); i++)
124 		printf("%u,", key->cbuf_cpp[i]);
125 	printf("}, .zsbuf_cpp = {");
126 	for (unsigned i = 0; i < ARRAY_SIZE(key->zsbuf_cpp); i++)
127 		printf("%u,", key->zsbuf_cpp[i]);
128 	printf("}},\n");
129 }
130 
131 static void
dump_gmem_state(const struct fd_gmem_stateobj * gmem)132 dump_gmem_state(const struct fd_gmem_stateobj *gmem)
133 {
134 	unsigned total = 0;
135 	printf("GMEM LAYOUT: bin=%ux%u, nbins=%ux%u\n",
136 			gmem->bin_w, gmem->bin_h, gmem->nbins_x, gmem->nbins_y);
137 	for (int i = 0; i < ARRAY_SIZE(gmem->cbuf_base); i++) {
138 		if (!gmem->cbuf_cpp[i])
139 			continue;
140 
141 		unsigned size = gmem->cbuf_cpp[i] * gmem->bin_w * gmem->bin_h;
142 		printf("  cbuf[%d]: base=0x%06x, size=0x%x, cpp=%u\n", i,
143 				gmem->cbuf_base[i], size, gmem->cbuf_cpp[i]);
144 
145 		total = gmem->cbuf_base[i] + size;
146 	}
147 
148 	for (int i = 0; i < ARRAY_SIZE(gmem->zsbuf_base); i++) {
149 		if (!gmem->zsbuf_cpp[i])
150 			continue;
151 
152 		unsigned size = gmem->zsbuf_cpp[i] * gmem->bin_w * gmem->bin_h;
153 		printf("  zsbuf[%d]: base=0x%06x, size=0x%x, cpp=%u\n", i,
154 				gmem->zsbuf_base[i], size, gmem->zsbuf_cpp[i]);
155 
156 		total = gmem->zsbuf_base[i] + size;
157 	}
158 
159 	printf("total: 0x%06x (of 0x%06x)\n", total,
160 			gmem->screen->gmemsize_bytes);
161 }
162 
163 static unsigned
div_align(unsigned num,unsigned denom,unsigned al)164 div_align(unsigned num, unsigned denom, unsigned al)
165 {
166 	return util_align_npot(DIV_ROUND_UP(num, denom), al);
167 }
168 
169 static bool
layout_gmem(struct gmem_key * key,uint32_t nbins_x,uint32_t nbins_y,struct fd_gmem_stateobj * gmem)170 layout_gmem(struct gmem_key *key, uint32_t nbins_x, uint32_t nbins_y,
171 		struct fd_gmem_stateobj *gmem)
172 {
173 	struct fd_screen *screen = gmem->screen;
174 	uint32_t gmem_align = key->gmem_page_align * 0x1000;
175 	uint32_t total = 0, i;
176 
177 	if ((nbins_x == 0) || (nbins_y == 0))
178 		return false;
179 
180 	uint32_t bin_w, bin_h;
181 	bin_w = div_align(key->width, nbins_x, screen->info.tile_align_w);
182 	bin_h = div_align(key->height, nbins_y, screen->info.tile_align_h);
183 
184 	if (bin_w > screen->info.tile_max_w)
185 		return false;
186 
187 	if (bin_h > screen->info.tile_max_h)
188 		return false;
189 
190 	gmem->bin_w = bin_w;
191 	gmem->bin_h = bin_h;
192 
193 	/* due to aligning bin_w/h, we could end up with one too
194 	 * many bins in either dimension, so recalculate:
195 	 */
196 	gmem->nbins_x = DIV_ROUND_UP(key->width, bin_w);
197 	gmem->nbins_y = DIV_ROUND_UP(key->height, bin_h);
198 
199 	for (i = 0; i < MAX_RENDER_TARGETS; i++) {
200 		if (key->cbuf_cpp[i]) {
201 			gmem->cbuf_base[i] = util_align_npot(total, gmem_align);
202 			total = gmem->cbuf_base[i] + key->cbuf_cpp[i] * bin_w * bin_h;
203 		}
204 	}
205 
206 	if (key->zsbuf_cpp[0]) {
207 		gmem->zsbuf_base[0] = util_align_npot(total, gmem_align);
208 		total = gmem->zsbuf_base[0] + key->zsbuf_cpp[0] * bin_w * bin_h;
209 	}
210 
211 	if (key->zsbuf_cpp[1]) {
212 		gmem->zsbuf_base[1] = util_align_npot(total, gmem_align);
213 		total = gmem->zsbuf_base[1] + key->zsbuf_cpp[1] * bin_w * bin_h;
214 	}
215 
216 	return total <= screen->gmemsize_bytes;
217 }
218 
219 static void
calc_nbins(struct gmem_key * key,struct fd_gmem_stateobj * gmem)220 calc_nbins(struct gmem_key *key, struct fd_gmem_stateobj *gmem)
221 {
222 	struct fd_screen *screen = gmem->screen;
223 	uint32_t nbins_x = 1, nbins_y = 1;
224 	uint32_t max_width = screen->info.tile_max_w;
225 	uint32_t max_height = screen->info.tile_max_h;
226 
227 	if (fd_mesa_debug & FD_DBG_MSGS) {
228 		debug_printf("binning input: cbuf cpp:");
229 		for (unsigned i = 0; i < key->nr_cbufs; i++)
230 			debug_printf(" %d", key->cbuf_cpp[i]);
231 		debug_printf(", zsbuf cpp: %d; %dx%d\n",
232 				key->zsbuf_cpp[0], key->width, key->height);
233 	}
234 
235 	/* first, find a bin size that satisfies the maximum width/
236 	 * height restrictions:
237 	 */
238 	while (div_align(key->width, nbins_x, screen->info.tile_align_w) > max_width) {
239 		nbins_x++;
240 	}
241 
242 	while (div_align(key->height, nbins_y, screen->info.tile_align_h) > max_height) {
243 		nbins_y++;
244 	}
245 
246 	/* then find a bin width/height that satisfies the memory
247 	 * constraints:
248 	 */
249 	while (!layout_gmem(key, nbins_x, nbins_y, gmem)) {
250 		if (nbins_y > nbins_x) {
251 			nbins_x++;
252 		} else {
253 			nbins_y++;
254 		}
255 	}
256 
257 	/* Lets see if we can tweak the layout a bit and come up with
258 	 * something better:
259 	 */
260 	if ((((nbins_x - 1) * (nbins_y + 1)) < (nbins_x * nbins_y)) &&
261 			layout_gmem(key, nbins_x - 1, nbins_y + 1, gmem)) {
262 		nbins_x--;
263 		nbins_y++;
264 	} else if ((((nbins_x + 1) * (nbins_y - 1)) < (nbins_x * nbins_y)) &&
265 			layout_gmem(key, nbins_x + 1, nbins_y - 1, gmem)) {
266 		nbins_x++;
267 		nbins_y--;
268 	}
269 
270 	layout_gmem(key, nbins_x, nbins_y, gmem);
271 }
272 
273 static struct fd_gmem_stateobj *
gmem_stateobj_init(struct fd_screen * screen,struct gmem_key * key)274 gmem_stateobj_init(struct fd_screen *screen, struct gmem_key *key)
275 {
276 	struct fd_gmem_stateobj *gmem =
277 			rzalloc(screen->gmem_cache.ht, struct fd_gmem_stateobj);
278 	pipe_reference_init(&gmem->reference, 1);
279 	gmem->screen = screen;
280 	gmem->key = key;
281 	list_inithead(&gmem->node);
282 
283 	const unsigned npipes = screen->info.num_vsc_pipes;
284 	uint32_t i, j, t, xoff, yoff;
285 	uint32_t tpp_x, tpp_y;
286 	int tile_n[npipes];
287 
288 	calc_nbins(key, gmem);
289 
290 	DBG("using %d bins of size %dx%d", gmem->nbins_x * gmem->nbins_y,
291 			gmem->bin_w, gmem->bin_h);
292 
293 	memcpy(gmem->cbuf_cpp, key->cbuf_cpp, sizeof(key->cbuf_cpp));
294 	memcpy(gmem->zsbuf_cpp, key->zsbuf_cpp, sizeof(key->zsbuf_cpp));
295 	gmem->minx = key->minx;
296 	gmem->miny = key->miny;
297 	gmem->width = key->width;
298 	gmem->height = key->height;
299 
300 	if (BIN_DEBUG) {
301 		dump_gmem_state(gmem);
302 		dump_gmem_key(key);
303 	}
304 
305 	/*
306 	 * Assign tiles and pipes:
307 	 *
308 	 * At some point it might be worth playing with different
309 	 * strategies and seeing if that makes much impact on
310 	 * performance.
311 	 */
312 
313 #define div_round_up(v, a)  (((v) + (a) - 1) / (a))
314 	/* figure out number of tiles per pipe: */
315 	if (is_a20x(screen)) {
316 		/* for a20x we want to minimize the number of "pipes"
317 		 * binning data has 3 bits for x/y (8x8) but the edges are used to
318 		 * cull off-screen vertices with hw binning, so we have 6x6 pipes
319 		 */
320 		tpp_x = 6;
321 		tpp_y = 6;
322 	} else {
323 		tpp_x = tpp_y = 1;
324 		while (div_round_up(gmem->nbins_y, tpp_y) > npipes)
325 			tpp_y += 2;
326 		while ((div_round_up(gmem->nbins_y, tpp_y) *
327 				div_round_up(gmem->nbins_x, tpp_x)) > npipes)
328 			tpp_x += 1;
329 	}
330 
331 #ifdef DEBUG
332 	tpp_x = env_var_as_unsigned("TPP_X", tpp_x);
333 	tpp_y = env_var_as_unsigned("TPP_Y", tpp_x);
334 #endif
335 
336 	gmem->maxpw = tpp_x;
337 	gmem->maxph = tpp_y;
338 
339 	/* configure pipes: */
340 	xoff = yoff = 0;
341 	for (i = 0; i < npipes; i++) {
342 		struct fd_vsc_pipe *pipe = &gmem->vsc_pipe[i];
343 
344 		if (xoff >= gmem->nbins_x) {
345 			xoff = 0;
346 			yoff += tpp_y;
347 		}
348 
349 		if (yoff >= gmem->nbins_y) {
350 			break;
351 		}
352 
353 		pipe->x = xoff;
354 		pipe->y = yoff;
355 		pipe->w = MIN2(tpp_x, gmem->nbins_x - xoff);
356 		pipe->h = MIN2(tpp_y, gmem->nbins_y - yoff);
357 
358 		xoff += tpp_x;
359 	}
360 
361 	/* number of pipes to use for a20x */
362 	gmem->num_vsc_pipes = MAX2(1, i);
363 
364 	for (; i < npipes; i++) {
365 		struct fd_vsc_pipe *pipe = &gmem->vsc_pipe[i];
366 		pipe->x = pipe->y = pipe->w = pipe->h = 0;
367 	}
368 
369 	if (BIN_DEBUG) {
370 		printf("%dx%d ... tpp=%dx%d\n", gmem->nbins_x, gmem->nbins_y, tpp_x, tpp_y);
371 		for (i = 0; i < ARRAY_SIZE(gmem->vsc_pipe); i++) {
372 			struct fd_vsc_pipe *pipe = &gmem->vsc_pipe[i];
373 			printf("pipe[%d]: %ux%u @ %u,%u\n", i,
374 					pipe->w, pipe->h, pipe->x, pipe->y);
375 		}
376 	}
377 
378 	/* configure tiles: */
379 	t = 0;
380 	yoff = key->miny;
381 	memset(tile_n, 0, sizeof(tile_n));
382 	for (i = 0; i < gmem->nbins_y; i++) {
383 		int bw, bh;
384 
385 		xoff = key->minx;
386 
387 		/* clip bin height: */
388 		bh = MIN2(gmem->bin_h, key->miny + key->height - yoff);
389 		assert(bh > 0);
390 
391 		for (j = 0; j < gmem->nbins_x; j++) {
392 			struct fd_tile *tile = &gmem->tile[t];
393 			uint32_t p;
394 
395 			assert(t < ARRAY_SIZE(gmem->tile));
396 
397 			/* pipe number: */
398 			p = ((i / tpp_y) * div_round_up(gmem->nbins_x, tpp_x)) + (j / tpp_x);
399 			assert(p < gmem->num_vsc_pipes);
400 
401 			/* clip bin width: */
402 			bw = MIN2(gmem->bin_w, key->minx + key->width - xoff);
403 			assert(bw > 0);
404 
405 			tile->n = !is_a20x(screen) ? tile_n[p]++ :
406 				((i % tpp_y + 1) << 3 | (j % tpp_x + 1));
407 			tile->p = p;
408 			tile->bin_w = bw;
409 			tile->bin_h = bh;
410 			tile->xoff = xoff;
411 			tile->yoff = yoff;
412 
413 			if (BIN_DEBUG) {
414 				printf("tile[%d]: p=%u, bin=%ux%u+%u+%u\n", t,
415 						p, bw, bh, xoff, yoff);
416 			}
417 
418 			t++;
419 
420 			xoff += bw;
421 		}
422 
423 		yoff += bh;
424 	}
425 
426 	if (BIN_DEBUG) {
427 		t = 0;
428 		for (i = 0; i < gmem->nbins_y; i++) {
429 			for (j = 0; j < gmem->nbins_x; j++) {
430 				struct fd_tile *tile = &gmem->tile[t++];
431 				printf("|p:%u n:%u|", tile->p, tile->n);
432 			}
433 			printf("\n");
434 		}
435 	}
436 
437 	return gmem;
438 }
439 
440 void
__fd_gmem_destroy(struct fd_gmem_stateobj * gmem)441 __fd_gmem_destroy(struct fd_gmem_stateobj *gmem)
442 {
443 	struct fd_gmem_cache *cache = &gmem->screen->gmem_cache;
444 
445 	fd_screen_assert_locked(gmem->screen);
446 
447 	_mesa_hash_table_remove_key(cache->ht, gmem->key);
448 	list_del(&gmem->node);
449 
450 	ralloc_free(gmem->key);
451 	ralloc_free(gmem);
452 }
453 
454 static struct gmem_key *
gmem_key_init(struct fd_batch * batch,bool assume_zs,bool no_scis_opt)455 gmem_key_init(struct fd_batch *batch, bool assume_zs, bool no_scis_opt)
456 {
457 	struct fd_screen *screen = batch->ctx->screen;
458 	struct pipe_framebuffer_state *pfb = &batch->framebuffer;
459 	bool has_zs = pfb->zsbuf && !!(batch->gmem_reason & (FD_GMEM_DEPTH_ENABLED |
460 		FD_GMEM_STENCIL_ENABLED | FD_GMEM_CLEARS_DEPTH_STENCIL));
461 	struct gmem_key *key = rzalloc(screen->gmem_cache.ht, struct gmem_key);
462 
463 	if (has_zs || assume_zs) {
464 		struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
465 		key->zsbuf_cpp[0] = rsc->layout.cpp;
466 		if (rsc->stencil)
467 			key->zsbuf_cpp[1] = rsc->stencil->layout.cpp;
468 	} else {
469 		/* we might have a zsbuf, but it isn't used */
470 		batch->restore &= ~(FD_BUFFER_DEPTH | FD_BUFFER_STENCIL);
471 		batch->resolve &= ~(FD_BUFFER_DEPTH | FD_BUFFER_STENCIL);
472 	}
473 
474 	key->nr_cbufs = pfb->nr_cbufs;
475 	for (unsigned i = 0; i < pfb->nr_cbufs; i++) {
476 		if (pfb->cbufs[i])
477 			key->cbuf_cpp[i] = util_format_get_blocksize(pfb->cbufs[i]->format);
478 		else
479 			key->cbuf_cpp[i] = 4;
480 		/* if MSAA, color buffers are super-sampled in GMEM: */
481 		key->cbuf_cpp[i] *= pfb->samples;
482 	}
483 
484 	/* NOTE: on a6xx, the max-scissor-rect is handled in fd6_gmem, and
485 	 * we just rely on CP_COND_EXEC to skip bins with no geometry.
486 	 */
487 	if (no_scis_opt || is_a6xx(screen)) {
488 		key->minx = 0;
489 		key->miny = 0;
490 		key->width = pfb->width;
491 		key->height = pfb->height;
492 	} else {
493 		struct pipe_scissor_state *scissor = &batch->max_scissor;
494 
495 		if (fd_mesa_debug & FD_DBG_NOSCIS) {
496 			scissor->minx = 0;
497 			scissor->miny = 0;
498 			scissor->maxx = pfb->width;
499 			scissor->maxy = pfb->height;
500 		}
501 
502 		/* round down to multiple of alignment: */
503 		key->minx = scissor->minx & ~(screen->info.gmem_align_w - 1);
504 		key->miny = scissor->miny & ~(screen->info.gmem_align_h - 1);
505 		key->width = scissor->maxx - key->minx;
506 		key->height = scissor->maxy - key->miny;
507 	}
508 
509 	if (is_a20x(screen) && batch->cleared) {
510 		/* under normal circumstances the requirement would be 4K
511 		 * but the fast clear path requires an alignment of 32K
512 		 */
513 		key->gmem_page_align = 8;
514 	} else if (is_a6xx(screen)) {
515 		key->gmem_page_align = is_a650(screen) ? 3 : 1;
516 	} else {
517 		// TODO re-check this across gens.. maybe it should only
518 		// be a single page in some cases:
519 		key->gmem_page_align = 4;
520 	}
521 
522 	return key;
523 }
524 
525 static struct fd_gmem_stateobj *
lookup_gmem_state(struct fd_batch * batch,bool assume_zs,bool no_scis_opt)526 lookup_gmem_state(struct fd_batch *batch, bool assume_zs, bool no_scis_opt)
527 {
528 	struct fd_screen *screen = batch->ctx->screen;
529 	struct fd_gmem_cache *cache = &screen->gmem_cache;
530 	struct fd_gmem_stateobj *gmem = NULL;
531 
532 	/* Lock before allocating gmem_key, since that a screen-wide
533 	 * ralloc pool and ralloc itself is not thread-safe.
534 	 */
535 	fd_screen_lock(screen);
536 
537 	struct gmem_key *key = gmem_key_init(batch, assume_zs, no_scis_opt);
538 	uint32_t hash = gmem_key_hash(key);
539 
540 	struct hash_entry *entry =
541 		_mesa_hash_table_search_pre_hashed(cache->ht, hash, key);
542 	if (entry) {
543 		ralloc_free(key);
544 		goto found;
545 	}
546 
547 	/* limit the # of cached gmem states, discarding the least
548 	 * recently used state if needed:
549 	 */
550 	if (cache->ht->entries >= 20) {
551 		struct fd_gmem_stateobj *last =
552 			list_last_entry(&cache->lru, struct fd_gmem_stateobj, node);
553 		fd_gmem_reference(&last, NULL);
554 	}
555 
556 	entry = _mesa_hash_table_insert_pre_hashed(cache->ht,
557 			hash, key, gmem_stateobj_init(screen, key));
558 
559 found:
560 	fd_gmem_reference(&gmem, entry->data);
561 	/* Move to the head of the LRU: */
562 	list_delinit(&gmem->node);
563 	list_add(&gmem->node, &cache->lru);
564 
565 	fd_screen_unlock(screen);
566 
567 	return gmem;
568 }
569 
570 /*
571  * GMEM render pass
572  */
573 
574 static void
render_tiles(struct fd_batch * batch,struct fd_gmem_stateobj * gmem)575 render_tiles(struct fd_batch *batch, struct fd_gmem_stateobj *gmem)
576 {
577 	struct fd_context *ctx = batch->ctx;
578 	int i;
579 
580 	mtx_lock(&ctx->gmem_lock);
581 
582 	ctx->emit_tile_init(batch);
583 
584 	if (batch->restore)
585 		ctx->stats.batch_restore++;
586 
587 	for (i = 0; i < (gmem->nbins_x * gmem->nbins_y); i++) {
588 		struct fd_tile *tile = &gmem->tile[i];
589 
590 		fd_log(batch, "bin_h=%d, yoff=%d, bin_w=%d, xoff=%d",
591 			tile->bin_h, tile->yoff, tile->bin_w, tile->xoff);
592 
593 		ctx->emit_tile_prep(batch, tile);
594 
595 		if (batch->restore) {
596 			ctx->emit_tile_mem2gmem(batch, tile);
597 		}
598 
599 		ctx->emit_tile_renderprep(batch, tile);
600 
601 		if (ctx->query_prepare_tile)
602 			ctx->query_prepare_tile(batch, i, batch->gmem);
603 
604 		/* emit IB to drawcmds: */
605 		fd_log(batch, "TILE[%d]: START DRAW IB", i);
606 		if (ctx->emit_tile) {
607 			ctx->emit_tile(batch, tile);
608 		} else {
609 			ctx->screen->emit_ib(batch->gmem, batch->draw);
610 		}
611 
612 		fd_log(batch, "TILE[%d]: END DRAW IB", i);
613 		fd_reset_wfi(batch);
614 
615 		/* emit gmem2mem to transfer tile back to system memory: */
616 		ctx->emit_tile_gmem2mem(batch, tile);
617 	}
618 
619 	if (ctx->emit_tile_fini)
620 		ctx->emit_tile_fini(batch);
621 
622 	mtx_unlock(&ctx->gmem_lock);
623 }
624 
625 static void
render_sysmem(struct fd_batch * batch)626 render_sysmem(struct fd_batch *batch)
627 {
628 	struct fd_context *ctx = batch->ctx;
629 
630 	ctx->emit_sysmem_prep(batch);
631 
632 	if (ctx->query_prepare_tile)
633 		ctx->query_prepare_tile(batch, 0, batch->gmem);
634 
635 	/* emit IB to drawcmds: */
636 	fd_log(batch, "SYSMEM: START DRAW IB");
637 	ctx->screen->emit_ib(batch->gmem, batch->draw);
638 	fd_log(batch, "SYSMEM: END DRAW IB");
639 	fd_reset_wfi(batch);
640 
641 	if (ctx->emit_sysmem_fini)
642 		ctx->emit_sysmem_fini(batch);
643 }
644 
645 static void
flush_ring(struct fd_batch * batch)646 flush_ring(struct fd_batch *batch)
647 {
648 	uint32_t timestamp;
649 	int out_fence_fd = -1;
650 
651 	if (unlikely(fd_mesa_debug & FD_DBG_NOHW))
652 		return;
653 
654 	fd_submit_flush(batch->submit, batch->in_fence_fd,
655 			batch->needs_out_fence_fd ? &out_fence_fd : NULL,
656 			&timestamp);
657 
658 	fd_fence_populate(batch->fence, timestamp, out_fence_fd);
659 	fd_log_flush(batch);
660 }
661 
662 void
fd_gmem_render_tiles(struct fd_batch * batch)663 fd_gmem_render_tiles(struct fd_batch *batch)
664 {
665 	struct fd_context *ctx = batch->ctx;
666 	struct pipe_framebuffer_state *pfb = &batch->framebuffer;
667 	bool sysmem = false;
668 
669 	if (ctx->emit_sysmem_prep && !batch->nondraw) {
670 		if (batch->cleared || batch->gmem_reason ||
671 				((batch->num_draws > 5) && !batch->blit) ||
672 				(pfb->samples > 1)) {
673 			fd_log(batch, "GMEM: cleared=%x, gmem_reason=%x, num_draws=%u, samples=%u",
674 				batch->cleared, batch->gmem_reason, batch->num_draws,
675 				pfb->samples);
676 		} else if (!(fd_mesa_debug & FD_DBG_NOBYPASS)) {
677 			sysmem = true;
678 		}
679 
680 		/* For ARB_framebuffer_no_attachments: */
681 		if ((pfb->nr_cbufs == 0) && !pfb->zsbuf) {
682 			sysmem = true;
683 		}
684 	}
685 
686 	if (fd_mesa_debug & FD_DBG_NOGMEM)
687 		sysmem = true;
688 
689 	/* Layered rendering always needs bypass. */
690 	for (unsigned i = 0; i < pfb->nr_cbufs; i++) {
691 		struct pipe_surface *psurf = pfb->cbufs[i];
692 		if (!psurf)
693 			continue;
694 		if (psurf->u.tex.first_layer < psurf->u.tex.last_layer)
695 			sysmem = true;
696 	}
697 
698 	/* Tessellation doesn't seem to support tiled rendering so fall back to
699 	 * bypass.
700 	 */
701 	if (batch->tessellation) {
702 		debug_assert(ctx->emit_sysmem_prep);
703 		sysmem = true;
704 	}
705 
706 	fd_reset_wfi(batch);
707 
708 	ctx->stats.batch_total++;
709 
710 	if (unlikely(fd_mesa_debug & FD_DBG_LOG) && !batch->nondraw) {
711 		fd_log_stream(batch, stream, util_dump_framebuffer_state(stream, pfb));
712 		for (unsigned i = 0; i < pfb->nr_cbufs; i++) {
713 			fd_log_stream(batch, stream, util_dump_surface(stream, pfb->cbufs[i]));
714 		}
715 		fd_log_stream(batch, stream, util_dump_surface(stream, pfb->zsbuf));
716 	}
717 
718 	if (batch->nondraw) {
719 		DBG("%p: rendering non-draw", batch);
720 		render_sysmem(batch);
721 		ctx->stats.batch_nondraw++;
722 	} else if (sysmem) {
723 		fd_log(batch, "%p: rendering sysmem %ux%u (%s/%s), num_draws=%u",
724 			batch, pfb->width, pfb->height,
725 			util_format_short_name(pipe_surface_format(pfb->cbufs[0])),
726 			util_format_short_name(pipe_surface_format(pfb->zsbuf)),
727 			batch->num_draws);
728 		if (ctx->query_prepare)
729 			ctx->query_prepare(batch, 1);
730 		render_sysmem(batch);
731 		ctx->stats.batch_sysmem++;
732 	} else {
733 		struct fd_gmem_stateobj *gmem = lookup_gmem_state(batch, false, false);
734 		batch->gmem_state = gmem;
735 		fd_log(batch, "%p: rendering %dx%d tiles %ux%u (%s/%s)",
736 			batch, pfb->width, pfb->height, gmem->nbins_x, gmem->nbins_y,
737 			util_format_short_name(pipe_surface_format(pfb->cbufs[0])),
738 			util_format_short_name(pipe_surface_format(pfb->zsbuf)));
739 		if (ctx->query_prepare)
740 			ctx->query_prepare(batch, gmem->nbins_x * gmem->nbins_y);
741 		render_tiles(batch, gmem);
742 		batch->gmem_state = NULL;
743 
744 		fd_screen_lock(ctx->screen);
745 		fd_gmem_reference(&gmem, NULL);
746 		fd_screen_unlock(ctx->screen);
747 
748 		ctx->stats.batch_gmem++;
749 	}
750 
751 	flush_ring(batch);
752 }
753 
754 /* Determine a worst-case estimate (ie. assuming we don't eliminate an
755  * unused depth/stencil) number of bins per vsc pipe.
756  */
757 unsigned
fd_gmem_estimate_bins_per_pipe(struct fd_batch * batch)758 fd_gmem_estimate_bins_per_pipe(struct fd_batch *batch)
759 {
760 	struct pipe_framebuffer_state *pfb = &batch->framebuffer;
761 	struct fd_screen *screen = batch->ctx->screen;
762 	struct fd_gmem_stateobj *gmem = lookup_gmem_state(batch, !!pfb->zsbuf, true);
763 	unsigned nbins = gmem->maxpw * gmem->maxph;
764 
765 	fd_screen_lock(screen);
766 	fd_gmem_reference(&gmem, NULL);
767 	fd_screen_unlock(screen);
768 
769 	return nbins;
770 }
771 
772 /* When deciding whether a tile needs mem2gmem, we need to take into
773  * account the scissor rect(s) that were cleared.  To simplify we only
774  * consider the last scissor rect for each buffer, since the common
775  * case would be a single clear.
776  */
777 bool
fd_gmem_needs_restore(struct fd_batch * batch,const struct fd_tile * tile,uint32_t buffers)778 fd_gmem_needs_restore(struct fd_batch *batch, const struct fd_tile *tile,
779 		uint32_t buffers)
780 {
781 	if (!(batch->restore & buffers))
782 		return false;
783 
784 	return true;
785 }
786 
787 static inline unsigned
max_bitfield_val(unsigned high,unsigned low,unsigned shift)788 max_bitfield_val(unsigned high, unsigned low, unsigned shift)
789 {
790 	return BITFIELD_MASK(high - low) << shift;
791 }
792 
793 void
fd_gmem_screen_init(struct pipe_screen * pscreen)794 fd_gmem_screen_init(struct pipe_screen *pscreen)
795 {
796 	struct fd_gmem_cache *cache = &fd_screen(pscreen)->gmem_cache;
797 
798 	cache->ht = _mesa_hash_table_create(NULL, gmem_key_hash, gmem_key_equals);
799 	list_inithead(&cache->lru);
800 }
801 
802 void
fd_gmem_screen_fini(struct pipe_screen * pscreen)803 fd_gmem_screen_fini(struct pipe_screen *pscreen)
804 {
805 	struct fd_gmem_cache *cache = &fd_screen(pscreen)->gmem_cache;
806 
807 	_mesa_hash_table_destroy(cache->ht, NULL);
808 }
809