panfrost: Add a panfrost_flush_all_batches() helper
[mesa.git] / src / gallium / drivers / panfrost / pan_job.c
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2014-2017 Broadcom
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #include <assert.h>
27
28 #include "drm-uapi/panfrost_drm.h"
29
30 #include "pan_bo.h"
31 #include "pan_context.h"
32 #include "util/hash_table.h"
33 #include "util/ralloc.h"
34 #include "util/u_format.h"
35 #include "util/u_pack_color.h"
36 #include "pan_util.h"
37 #include "pandecode/decode.h"
38
39 /* panfrost_bo_access is here to help us keep track of batch accesses to BOs
40 * and build a proper dependency graph such that batches can be pipelined for
41 * better GPU utilization.
42 *
43 * Each accessed BO has a corresponding entry in the ->accessed_bos hash table.
44 * A BO is either being written or read at any time, that's what the type field
45 * encodes.
46 * When the last access is a write, the batch writing the BO might have read
47 * dependencies (readers that have not been executed yet and want to read the
48 * previous BO content), and when the last access is a read, all readers might
49 * depend on another batch to push its results to memory. That's what the
50 * readers/writers keep track off.
51 * There can only be one writer at any given time, if a new batch wants to
52 * write to the same BO, a dependency will be added between the new writer and
53 * the old writer (at the batch level), and panfrost_bo_access->writer will be
54 * updated to point to the new writer.
55 */
56 struct panfrost_bo_access {
57 uint32_t type;
58 struct util_dynarray readers;
59 struct panfrost_batch_fence *writer;
60 };
61
62 static struct panfrost_batch_fence *
63 panfrost_create_batch_fence(struct panfrost_batch *batch)
64 {
65 struct panfrost_batch_fence *fence;
66 ASSERTED int ret;
67
68 fence = rzalloc(NULL, struct panfrost_batch_fence);
69 assert(fence);
70 pipe_reference_init(&fence->reference, 1);
71 fence->ctx = batch->ctx;
72 fence->batch = batch;
73 ret = drmSyncobjCreate(pan_screen(batch->ctx->base.screen)->fd, 0,
74 &fence->syncobj);
75 assert(!ret);
76
77 return fence;
78 }
79
80 static void
81 panfrost_free_batch_fence(struct panfrost_batch_fence *fence)
82 {
83 drmSyncobjDestroy(pan_screen(fence->ctx->base.screen)->fd,
84 fence->syncobj);
85 ralloc_free(fence);
86 }
87
88 void
89 panfrost_batch_fence_unreference(struct panfrost_batch_fence *fence)
90 {
91 if (pipe_reference(&fence->reference, NULL))
92 panfrost_free_batch_fence(fence);
93 }
94
95 void
96 panfrost_batch_fence_reference(struct panfrost_batch_fence *fence)
97 {
98 pipe_reference(NULL, &fence->reference);
99 }
100
101 static struct panfrost_batch *
102 panfrost_create_batch(struct panfrost_context *ctx,
103 const struct pipe_framebuffer_state *key)
104 {
105 struct panfrost_batch *batch = rzalloc(ctx, struct panfrost_batch);
106
107 batch->ctx = ctx;
108
109 batch->bos = _mesa_hash_table_create(batch, _mesa_hash_pointer,
110 _mesa_key_pointer_equal);
111
112 batch->minx = batch->miny = ~0;
113 batch->maxx = batch->maxy = 0;
114 batch->transient_offset = 0;
115
116 util_dynarray_init(&batch->headers, batch);
117 util_dynarray_init(&batch->gpu_headers, batch);
118 util_dynarray_init(&batch->dependencies, batch);
119 batch->out_sync = panfrost_create_batch_fence(batch);
120 util_copy_framebuffer_state(&batch->key, key);
121
122 return batch;
123 }
124
125 static void
126 panfrost_freeze_batch(struct panfrost_batch *batch)
127 {
128 struct panfrost_context *ctx = batch->ctx;
129 struct hash_entry *entry;
130
131 /* Remove the entry in the FBO -> batch hash table if the batch
132 * matches. This way, next draws/clears targeting this FBO will trigger
133 * the creation of a new batch.
134 */
135 entry = _mesa_hash_table_search(ctx->batches, &batch->key);
136 if (entry && entry->data == batch)
137 _mesa_hash_table_remove(ctx->batches, entry);
138
139 /* If this is the bound batch, the panfrost_context parameters are
140 * relevant so submitting it invalidates those parameters, but if it's
141 * not bound, the context parameters are for some other batch so we
142 * can't invalidate them.
143 */
144 if (ctx->batch == batch) {
145 panfrost_invalidate_frame(ctx);
146 ctx->batch = NULL;
147 }
148 }
149
150 #ifndef NDEBUG
151 static bool panfrost_batch_is_frozen(struct panfrost_batch *batch)
152 {
153 struct panfrost_context *ctx = batch->ctx;
154 struct hash_entry *entry;
155
156 entry = _mesa_hash_table_search(ctx->batches, &batch->key);
157 if (entry && entry->data == batch)
158 return false;
159
160 if (ctx->batch == batch)
161 return false;
162
163 return true;
164 }
165 #endif
166
167 static void
168 panfrost_free_batch(struct panfrost_batch *batch)
169 {
170 if (!batch)
171 return;
172
173 assert(panfrost_batch_is_frozen(batch));
174
175 hash_table_foreach(batch->bos, entry)
176 panfrost_bo_unreference((struct panfrost_bo *)entry->key);
177
178 util_dynarray_foreach(&batch->dependencies,
179 struct panfrost_batch_fence *, dep) {
180 panfrost_batch_fence_unreference(*dep);
181 }
182
183 /* The out_sync fence lifetime is different from the the batch one
184 * since other batches might want to wait on a fence of already
185 * submitted/signaled batch. All we need to do here is make sure the
186 * fence does not point to an invalid batch, which the core will
187 * interpret as 'batch is already submitted'.
188 */
189 batch->out_sync->batch = NULL;
190 panfrost_batch_fence_unreference(batch->out_sync);
191
192 util_unreference_framebuffer_state(&batch->key);
193 ralloc_free(batch);
194 }
195
196 #ifndef NDEBUG
197 static bool
198 panfrost_dep_graph_contains_batch(struct panfrost_batch *root,
199 struct panfrost_batch *batch)
200 {
201 if (!root)
202 return false;
203
204 util_dynarray_foreach(&root->dependencies,
205 struct panfrost_batch_fence *, dep) {
206 if ((*dep)->batch == batch ||
207 panfrost_dep_graph_contains_batch((*dep)->batch, batch))
208 return true;
209 }
210
211 return false;
212 }
213 #endif
214
215 static void
216 panfrost_batch_add_dep(struct panfrost_batch *batch,
217 struct panfrost_batch_fence *newdep)
218 {
219 if (batch == newdep->batch)
220 return;
221
222 /* We might want to turn ->dependencies into a set if the number of
223 * deps turns out to be big enough to make this 'is dep already there'
224 * search inefficient.
225 */
226 util_dynarray_foreach(&batch->dependencies,
227 struct panfrost_batch_fence *, dep) {
228 if (*dep == newdep)
229 return;
230 }
231
232 /* Make sure the dependency graph is acyclic. */
233 assert(!panfrost_dep_graph_contains_batch(newdep->batch, batch));
234
235 panfrost_batch_fence_reference(newdep);
236 util_dynarray_append(&batch->dependencies,
237 struct panfrost_batch_fence *, newdep);
238
239 /* We now have a batch depending on us, let's make sure new draw/clear
240 * calls targeting the same FBO use a new batch object.
241 */
242 if (newdep->batch)
243 panfrost_freeze_batch(newdep->batch);
244 }
245
246 static struct panfrost_batch *
247 panfrost_get_batch(struct panfrost_context *ctx,
248 const struct pipe_framebuffer_state *key)
249 {
250 /* Lookup the job first */
251 struct hash_entry *entry = _mesa_hash_table_search(ctx->batches, key);
252
253 if (entry)
254 return entry->data;
255
256 /* Otherwise, let's create a job */
257
258 struct panfrost_batch *batch = panfrost_create_batch(ctx, key);
259
260 /* Save the created job */
261 _mesa_hash_table_insert(ctx->batches, &batch->key, batch);
262
263 return batch;
264 }
265
266 /* Get the job corresponding to the FBO we're currently rendering into */
267
268 struct panfrost_batch *
269 panfrost_get_batch_for_fbo(struct panfrost_context *ctx)
270 {
271 /* If we're wallpapering, we special case to workaround
272 * u_blitter abuse */
273
274 if (ctx->wallpaper_batch)
275 return ctx->wallpaper_batch;
276
277 /* If we already began rendering, use that */
278
279 if (ctx->batch) {
280 assert(util_framebuffer_state_equal(&ctx->batch->key,
281 &ctx->pipe_framebuffer));
282 return ctx->batch;
283 }
284
285 /* If not, look up the job */
286 struct panfrost_batch *batch = panfrost_get_batch(ctx,
287 &ctx->pipe_framebuffer);
288
289 /* Set this job as the current FBO job. Will be reset when updating the
290 * FB state and when submitting or releasing a job.
291 */
292 ctx->batch = batch;
293 return batch;
294 }
295
296 static bool
297 panfrost_batch_fence_is_signaled(struct panfrost_batch_fence *fence)
298 {
299 if (fence->signaled)
300 return true;
301
302 /* Batch has not been submitted yet. */
303 if (fence->batch)
304 return false;
305
306 int ret = drmSyncobjWait(pan_screen(fence->ctx->base.screen)->fd,
307 &fence->syncobj, 1, 0, 0, NULL);
308
309 /* Cache whether the fence was signaled */
310 fence->signaled = ret >= 0;
311 return fence->signaled;
312 }
313
314 static void
315 panfrost_bo_access_gc_fences(struct panfrost_context *ctx,
316 struct panfrost_bo_access *access,
317 const struct panfrost_bo *bo)
318 {
319 if (access->writer && panfrost_batch_fence_is_signaled(access->writer)) {
320 panfrost_batch_fence_unreference(access->writer);
321 access->writer = NULL;
322 }
323
324 unsigned nreaders = 0;
325 util_dynarray_foreach(&access->readers, struct panfrost_batch_fence *,
326 reader) {
327 if (!(*reader))
328 continue;
329
330 if (panfrost_batch_fence_is_signaled(*reader)) {
331 panfrost_batch_fence_unreference(*reader);
332 *reader = NULL;
333 } else {
334 nreaders++;
335 }
336 }
337
338 if (!nreaders)
339 util_dynarray_clear(&access->readers);
340 }
341
342 /* Collect signaled fences to keep the kernel-side syncobj-map small. The
343 * idea is to collect those signaled fences at the end of each flush_all
344 * call. This function is likely to collect only fences from previous
345 * batch flushes not the one that have just have just been submitted and
346 * are probably still in flight when we trigger the garbage collection.
347 * Anyway, we need to do this garbage collection at some point if we don't
348 * want the BO access map to keep invalid entries around and retain
349 * syncobjs forever.
350 */
351 static void
352 panfrost_gc_fences(struct panfrost_context *ctx)
353 {
354 hash_table_foreach(ctx->accessed_bos, entry) {
355 struct panfrost_bo_access *access = entry->data;
356
357 assert(access);
358 panfrost_bo_access_gc_fences(ctx, access, entry->key);
359 if (!util_dynarray_num_elements(&access->readers,
360 struct panfrost_batch_fence *) &&
361 !access->writer)
362 _mesa_hash_table_remove(ctx->accessed_bos, entry);
363 }
364 }
365
366 #ifndef NDEBUG
367 static bool
368 panfrost_batch_in_readers(struct panfrost_batch *batch,
369 struct panfrost_bo_access *access)
370 {
371 util_dynarray_foreach(&access->readers, struct panfrost_batch_fence *,
372 reader) {
373 if (*reader && (*reader)->batch == batch)
374 return true;
375 }
376
377 return false;
378 }
379 #endif
380
381 static void
382 panfrost_batch_update_bo_access(struct panfrost_batch *batch,
383 struct panfrost_bo *bo, uint32_t access_type,
384 bool already_accessed)
385 {
386 struct panfrost_context *ctx = batch->ctx;
387 struct panfrost_bo_access *access;
388 uint32_t old_access_type;
389 struct hash_entry *entry;
390
391 assert(access_type == PAN_BO_ACCESS_WRITE ||
392 access_type == PAN_BO_ACCESS_READ);
393
394 entry = _mesa_hash_table_search(ctx->accessed_bos, bo);
395 access = entry ? entry->data : NULL;
396 if (access) {
397 old_access_type = access->type;
398 } else {
399 access = rzalloc(ctx, struct panfrost_bo_access);
400 util_dynarray_init(&access->readers, access);
401 _mesa_hash_table_insert(ctx->accessed_bos, bo, access);
402 /* We are the first to access this BO, let's initialize
403 * old_access_type to our own access type in that case.
404 */
405 old_access_type = access_type;
406 access->type = access_type;
407 }
408
409 assert(access);
410
411 if (access_type == PAN_BO_ACCESS_WRITE &&
412 old_access_type == PAN_BO_ACCESS_READ) {
413 /* Previous access was a read and we want to write this BO.
414 * We first need to add explicit deps between our batch and
415 * the previous readers.
416 */
417 util_dynarray_foreach(&access->readers,
418 struct panfrost_batch_fence *, reader) {
419 /* We were already reading the BO, no need to add a dep
420 * on ourself (the acyclic check would complain about
421 * that).
422 */
423 if (!(*reader) || (*reader)->batch == batch)
424 continue;
425
426 panfrost_batch_add_dep(batch, *reader);
427 }
428 panfrost_batch_fence_reference(batch->out_sync);
429
430 /* We now are the new writer. */
431 access->writer = batch->out_sync;
432 access->type = access_type;
433
434 /* Release the previous readers and reset the readers array. */
435 util_dynarray_foreach(&access->readers,
436 struct panfrost_batch_fence *,
437 reader) {
438 if (!*reader)
439 continue;
440 panfrost_batch_fence_unreference(*reader);
441 }
442
443 util_dynarray_clear(&access->readers);
444 } else if (access_type == PAN_BO_ACCESS_WRITE &&
445 old_access_type == PAN_BO_ACCESS_WRITE) {
446 /* Previous access was a write and we want to write this BO.
447 * First check if we were the previous writer, in that case
448 * there's nothing to do. Otherwise we need to add a
449 * dependency between the new writer and the old one.
450 */
451 if (access->writer != batch->out_sync) {
452 if (access->writer) {
453 panfrost_batch_add_dep(batch, access->writer);
454 panfrost_batch_fence_unreference(access->writer);
455 }
456 panfrost_batch_fence_reference(batch->out_sync);
457 access->writer = batch->out_sync;
458 }
459 } else if (access_type == PAN_BO_ACCESS_READ &&
460 old_access_type == PAN_BO_ACCESS_WRITE) {
461 /* Previous access was a write and we want to read this BO.
462 * First check if we were the previous writer, in that case
463 * we want to keep the access type unchanged, as a write is
464 * more constraining than a read.
465 */
466 if (access->writer != batch->out_sync) {
467 /* Add a dependency on the previous writer. */
468 panfrost_batch_add_dep(batch, access->writer);
469
470 /* The previous access was a write, there's no reason
471 * to have entries in the readers array.
472 */
473 assert(!util_dynarray_num_elements(&access->readers,
474 struct panfrost_batch_fence *));
475
476 /* Add ourselves to the readers array. */
477 panfrost_batch_fence_reference(batch->out_sync);
478 util_dynarray_append(&access->readers,
479 struct panfrost_batch_fence *,
480 batch->out_sync);
481 access->type = PAN_BO_ACCESS_READ;
482 }
483 } else {
484 /* We already accessed this BO before, so we should already be
485 * in the reader array.
486 */
487 if (already_accessed) {
488 assert(panfrost_batch_in_readers(batch, access));
489 return;
490 }
491
492 /* Previous access was a read and we want to read this BO.
493 * Add ourselves to the readers array and add a dependency on
494 * the previous writer if any.
495 */
496 panfrost_batch_fence_reference(batch->out_sync);
497 util_dynarray_append(&access->readers,
498 struct panfrost_batch_fence *,
499 batch->out_sync);
500
501 if (access->writer)
502 panfrost_batch_add_dep(batch, access->writer);
503 }
504 }
505
506 void
507 panfrost_batch_add_bo(struct panfrost_batch *batch, struct panfrost_bo *bo,
508 uint32_t flags)
509 {
510 if (!bo)
511 return;
512
513 struct hash_entry *entry;
514 uint32_t old_flags = 0;
515
516 entry = _mesa_hash_table_search(batch->bos, bo);
517 if (!entry) {
518 entry = _mesa_hash_table_insert(batch->bos, bo,
519 (void *)(uintptr_t)flags);
520 panfrost_bo_reference(bo);
521 } else {
522 old_flags = (uintptr_t)entry->data;
523
524 /* All batches have to agree on the shared flag. */
525 assert((old_flags & PAN_BO_ACCESS_SHARED) ==
526 (flags & PAN_BO_ACCESS_SHARED));
527 }
528
529 assert(entry);
530
531 if (old_flags == flags)
532 return;
533
534 flags |= old_flags;
535 entry->data = (void *)(uintptr_t)flags;
536
537 /* If this is not a shared BO, we don't really care about dependency
538 * tracking.
539 */
540 if (!(flags & PAN_BO_ACCESS_SHARED))
541 return;
542
543 /* All dependencies should have been flushed before we execute the
544 * wallpaper draw, so it should be harmless to skip the
545 * update_bo_access() call.
546 */
547 if (batch == batch->ctx->wallpaper_batch)
548 return;
549
550 /* Only pass R/W flags to the dep tracking logic. */
551 assert(flags & PAN_BO_ACCESS_RW);
552 flags = (flags & PAN_BO_ACCESS_WRITE) ?
553 PAN_BO_ACCESS_WRITE : PAN_BO_ACCESS_READ;
554 panfrost_batch_update_bo_access(batch, bo, flags, old_flags != 0);
555 }
556
557 void panfrost_batch_add_fbo_bos(struct panfrost_batch *batch)
558 {
559 uint32_t flags = PAN_BO_ACCESS_SHARED | PAN_BO_ACCESS_WRITE |
560 PAN_BO_ACCESS_VERTEX_TILER |
561 PAN_BO_ACCESS_FRAGMENT;
562
563 for (unsigned i = 0; i < batch->key.nr_cbufs; ++i) {
564 struct panfrost_resource *rsrc = pan_resource(batch->key.cbufs[i]->texture);
565 panfrost_batch_add_bo(batch, rsrc->bo, flags);
566 }
567
568 if (batch->key.zsbuf) {
569 struct panfrost_resource *rsrc = pan_resource(batch->key.zsbuf->texture);
570 panfrost_batch_add_bo(batch, rsrc->bo, flags);
571 }
572 }
573
574 struct panfrost_bo *
575 panfrost_batch_create_bo(struct panfrost_batch *batch, size_t size,
576 uint32_t create_flags, uint32_t access_flags)
577 {
578 struct panfrost_bo *bo;
579
580 bo = panfrost_bo_create(pan_screen(batch->ctx->base.screen), size,
581 create_flags);
582 panfrost_batch_add_bo(batch, bo, access_flags);
583
584 /* panfrost_batch_add_bo() has retained a reference and
585 * panfrost_bo_create() initialize the refcnt to 1, so let's
586 * unreference the BO here so it gets released when the batch is
587 * destroyed (unless it's retained by someone else in the meantime).
588 */
589 panfrost_bo_unreference(bo);
590 return bo;
591 }
592
593 /* Returns the polygon list's GPU address if available, or otherwise allocates
594 * the polygon list. It's perfectly fast to use allocate/free BO directly,
595 * since we'll hit the BO cache and this is one-per-batch anyway. */
596
597 mali_ptr
598 panfrost_batch_get_polygon_list(struct panfrost_batch *batch, unsigned size)
599 {
600 if (batch->polygon_list) {
601 assert(batch->polygon_list->size >= size);
602 } else {
603 /* Create the BO as invisible, as there's no reason to map */
604
605 batch->polygon_list = panfrost_batch_create_bo(batch, size,
606 PAN_BO_INVISIBLE,
607 PAN_BO_ACCESS_PRIVATE |
608 PAN_BO_ACCESS_RW |
609 PAN_BO_ACCESS_VERTEX_TILER |
610 PAN_BO_ACCESS_FRAGMENT);
611 }
612
613 return batch->polygon_list->gpu;
614 }
615
616 struct panfrost_bo *
617 panfrost_batch_get_scratchpad(struct panfrost_batch *batch)
618 {
619 if (batch->scratchpad)
620 return batch->scratchpad;
621
622 batch->scratchpad = panfrost_batch_create_bo(batch, 64 * 4 * 4096,
623 PAN_BO_INVISIBLE,
624 PAN_BO_ACCESS_PRIVATE |
625 PAN_BO_ACCESS_RW |
626 PAN_BO_ACCESS_VERTEX_TILER |
627 PAN_BO_ACCESS_FRAGMENT);
628 assert(batch->scratchpad);
629 return batch->scratchpad;
630 }
631
632 struct panfrost_bo *
633 panfrost_batch_get_tiler_heap(struct panfrost_batch *batch)
634 {
635 if (batch->tiler_heap)
636 return batch->tiler_heap;
637
638 batch->tiler_heap = panfrost_batch_create_bo(batch, 4096 * 4096,
639 PAN_BO_INVISIBLE |
640 PAN_BO_GROWABLE,
641 PAN_BO_ACCESS_PRIVATE |
642 PAN_BO_ACCESS_RW |
643 PAN_BO_ACCESS_VERTEX_TILER |
644 PAN_BO_ACCESS_FRAGMENT);
645 assert(batch->tiler_heap);
646 return batch->tiler_heap;
647 }
648
649 struct panfrost_bo *
650 panfrost_batch_get_tiler_dummy(struct panfrost_batch *batch)
651 {
652 if (batch->tiler_dummy)
653 return batch->tiler_dummy;
654
655 batch->tiler_dummy = panfrost_batch_create_bo(batch, 4096,
656 PAN_BO_INVISIBLE,
657 PAN_BO_ACCESS_PRIVATE |
658 PAN_BO_ACCESS_RW |
659 PAN_BO_ACCESS_VERTEX_TILER |
660 PAN_BO_ACCESS_FRAGMENT);
661 assert(batch->tiler_dummy);
662 return batch->tiler_dummy;
663 }
664
665 static void
666 panfrost_batch_draw_wallpaper(struct panfrost_batch *batch)
667 {
668 /* Nothing to reload? TODO: MRT wallpapers */
669 if (batch->key.cbufs[0] == NULL)
670 return;
671
672 /* Check if the buffer has any content on it worth preserving */
673
674 struct pipe_surface *surf = batch->key.cbufs[0];
675 struct panfrost_resource *rsrc = pan_resource(surf->texture);
676 unsigned level = surf->u.tex.level;
677
678 if (!rsrc->slices[level].initialized)
679 return;
680
681 batch->ctx->wallpaper_batch = batch;
682
683 /* Clamp the rendering area to the damage extent. The
684 * KHR_partial_update() spec states that trying to render outside of
685 * the damage region is "undefined behavior", so we should be safe.
686 */
687 unsigned damage_width = (rsrc->damage.extent.maxx - rsrc->damage.extent.minx);
688 unsigned damage_height = (rsrc->damage.extent.maxy - rsrc->damage.extent.miny);
689
690 if (damage_width && damage_height) {
691 panfrost_batch_intersection_scissor(batch,
692 rsrc->damage.extent.minx,
693 rsrc->damage.extent.miny,
694 rsrc->damage.extent.maxx,
695 rsrc->damage.extent.maxy);
696 }
697
698 /* FIXME: Looks like aligning on a tile is not enough, but
699 * aligning on twice the tile size seems to works. We don't
700 * know exactly what happens here but this deserves extra
701 * investigation to figure it out.
702 */
703 batch->minx = batch->minx & ~((MALI_TILE_LENGTH * 2) - 1);
704 batch->miny = batch->miny & ~((MALI_TILE_LENGTH * 2) - 1);
705 batch->maxx = MIN2(ALIGN_POT(batch->maxx, MALI_TILE_LENGTH * 2),
706 rsrc->base.width0);
707 batch->maxy = MIN2(ALIGN_POT(batch->maxy, MALI_TILE_LENGTH * 2),
708 rsrc->base.height0);
709
710 struct pipe_scissor_state damage;
711 struct pipe_box rects[4];
712
713 /* Clamp the damage box to the rendering area. */
714 damage.minx = MAX2(batch->minx, rsrc->damage.biggest_rect.x);
715 damage.miny = MAX2(batch->miny, rsrc->damage.biggest_rect.y);
716 damage.maxx = MIN2(batch->maxx,
717 rsrc->damage.biggest_rect.x +
718 rsrc->damage.biggest_rect.width);
719 damage.maxy = MIN2(batch->maxy,
720 rsrc->damage.biggest_rect.y +
721 rsrc->damage.biggest_rect.height);
722
723 /* One damage rectangle means we can end up with at most 4 reload
724 * regions:
725 * 1: left region, only exists if damage.x > 0
726 * 2: right region, only exists if damage.x + damage.width < fb->width
727 * 3: top region, only exists if damage.y > 0. The intersection with
728 * the left and right regions are dropped
729 * 4: bottom region, only exists if damage.y + damage.height < fb->height.
730 * The intersection with the left and right regions are dropped
731 *
732 * ____________________________
733 * | | 3 | |
734 * | |___________| |
735 * | | damage | |
736 * | 1 | rect | 2 |
737 * | |___________| |
738 * | | 4 | |
739 * |_______|___________|______|
740 */
741 u_box_2d(batch->minx, batch->miny, damage.minx - batch->minx,
742 batch->maxy - batch->miny, &rects[0]);
743 u_box_2d(damage.maxx, batch->miny, batch->maxx - damage.maxx,
744 batch->maxy - batch->miny, &rects[1]);
745 u_box_2d(damage.minx, batch->miny, damage.maxx - damage.minx,
746 damage.miny - batch->miny, &rects[2]);
747 u_box_2d(damage.minx, damage.maxy, damage.maxx - damage.minx,
748 batch->maxy - damage.maxy, &rects[3]);
749
750 for (unsigned i = 0; i < 4; i++) {
751 /* Width and height are always >= 0 even if width is declared as a
752 * signed integer: u_box_2d() helper takes unsigned args and
753 * panfrost_set_damage_region() is taking care of clamping
754 * negative values.
755 */
756 if (!rects[i].width || !rects[i].height)
757 continue;
758
759 /* Blit the wallpaper in */
760 panfrost_blit_wallpaper(batch->ctx, &rects[i]);
761 }
762 batch->ctx->wallpaper_batch = NULL;
763 }
764
765 static int
766 panfrost_batch_submit_ioctl(struct panfrost_batch *batch,
767 mali_ptr first_job_desc,
768 uint32_t reqs)
769 {
770 struct panfrost_context *ctx = batch->ctx;
771 struct pipe_context *gallium = (struct pipe_context *) ctx;
772 struct panfrost_screen *screen = pan_screen(gallium->screen);
773 struct drm_panfrost_submit submit = {0,};
774 uint32_t *bo_handles, *in_syncs = NULL;
775 bool is_fragment_shader;
776 int ret;
777
778 is_fragment_shader = (reqs & PANFROST_JD_REQ_FS) && batch->first_job.gpu;
779 if (is_fragment_shader)
780 submit.in_sync_count = 1;
781 else
782 submit.in_sync_count = util_dynarray_num_elements(&batch->dependencies,
783 struct panfrost_batch_fence *);
784
785 if (submit.in_sync_count) {
786 in_syncs = calloc(submit.in_sync_count, sizeof(*in_syncs));
787 assert(in_syncs);
788 }
789
790 /* The fragment job always depends on the vertex/tiler job if there's
791 * one
792 */
793 if (is_fragment_shader) {
794 in_syncs[0] = batch->out_sync->syncobj;
795 } else {
796 unsigned int i = 0;
797
798 util_dynarray_foreach(&batch->dependencies,
799 struct panfrost_batch_fence *, dep)
800 in_syncs[i++] = (*dep)->syncobj;
801 }
802
803 submit.in_syncs = (uintptr_t)in_syncs;
804 submit.out_sync = batch->out_sync->syncobj;
805 submit.jc = first_job_desc;
806 submit.requirements = reqs;
807
808 bo_handles = calloc(batch->bos->entries, sizeof(*bo_handles));
809 assert(bo_handles);
810
811 hash_table_foreach(batch->bos, entry) {
812 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
813 assert(bo->gem_handle > 0);
814 bo_handles[submit.bo_handle_count++] = bo->gem_handle;
815 }
816
817 submit.bo_handles = (u64) (uintptr_t) bo_handles;
818 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit);
819 free(bo_handles);
820 free(in_syncs);
821
822 if (ret) {
823 fprintf(stderr, "Error submitting: %m\n");
824 return errno;
825 }
826
827 /* Trace the job if we're doing that */
828 if (pan_debug & PAN_DBG_TRACE) {
829 /* Wait so we can get errors reported back */
830 drmSyncobjWait(screen->fd, &batch->out_sync->syncobj, 1,
831 INT64_MAX, 0, NULL);
832 pandecode_jc(submit.jc, FALSE);
833 }
834
835 return 0;
836 }
837
838 static int
839 panfrost_batch_submit_jobs(struct panfrost_batch *batch)
840 {
841 bool has_draws = batch->first_job.gpu;
842 int ret = 0;
843
844 if (has_draws) {
845 ret = panfrost_batch_submit_ioctl(batch, batch->first_job.gpu, 0);
846 assert(!ret);
847 }
848
849 if (batch->first_tiler.gpu || batch->clear) {
850 mali_ptr fragjob = panfrost_fragment_job(batch, has_draws);
851
852 ret = panfrost_batch_submit_ioctl(batch, fragjob, PANFROST_JD_REQ_FS);
853 assert(!ret);
854 }
855
856 return ret;
857 }
858
859 static void
860 panfrost_batch_submit(struct panfrost_batch *batch)
861 {
862 assert(batch);
863
864 /* Submit the dependencies first. */
865 util_dynarray_foreach(&batch->dependencies,
866 struct panfrost_batch_fence *, dep) {
867 if ((*dep)->batch)
868 panfrost_batch_submit((*dep)->batch);
869 }
870
871 struct panfrost_context *ctx = batch->ctx;
872 int ret;
873
874 /* Nothing to do! */
875 if (!batch->last_job.gpu && !batch->clear) {
876 /* Mark the fence as signaled so the fence logic does not try
877 * to wait on it.
878 */
879 batch->out_sync->signaled = true;
880 goto out;
881 }
882
883 if (!batch->clear && batch->last_tiler.gpu)
884 panfrost_batch_draw_wallpaper(batch);
885
886 panfrost_scoreboard_link_batch(batch);
887
888 ret = panfrost_batch_submit_jobs(batch);
889
890 if (ret)
891 fprintf(stderr, "panfrost_batch_submit failed: %d\n", ret);
892
893 out:
894 panfrost_freeze_batch(batch);
895
896 /* We always stall the pipeline for correct results since pipelined
897 * rendering is quite broken right now (to be fixed by the panfrost_job
898 * refactor, just take the perf hit for correctness)
899 */
900 if (!batch->out_sync->signaled)
901 drmSyncobjWait(pan_screen(ctx->base.screen)->fd,
902 &batch->out_sync->syncobj, 1, INT64_MAX, 0,
903 NULL);
904
905 panfrost_free_batch(batch);
906
907 }
908
909 void
910 panfrost_flush_all_batches(struct panfrost_context *ctx, bool wait)
911 {
912 struct util_dynarray fences, syncobjs;
913
914 if (wait) {
915 util_dynarray_init(&fences, NULL);
916 util_dynarray_init(&syncobjs, NULL);
917 }
918
919 hash_table_foreach(ctx->batches, hentry) {
920 struct panfrost_batch *batch = hentry->data;
921
922 assert(batch);
923
924 if (wait) {
925 panfrost_batch_fence_reference(batch->out_sync);
926 util_dynarray_append(&fences, struct panfrost_batch_fence *,
927 batch->out_sync);
928 util_dynarray_append(&syncobjs, uint32_t,
929 batch->out_sync->syncobj);
930 }
931
932 panfrost_batch_submit(batch);
933 }
934
935 assert(!ctx->batches->entries);
936
937 /* Collect batch fences before returning */
938 panfrost_gc_fences(ctx);
939
940 if (!wait)
941 return;
942
943 drmSyncobjWait(pan_screen(ctx->base.screen)->fd,
944 util_dynarray_begin(&syncobjs),
945 util_dynarray_num_elements(&syncobjs, uint32_t),
946 INT64_MAX, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL, NULL);
947
948 util_dynarray_foreach(&fences, struct panfrost_batch_fence *, fence)
949 panfrost_batch_fence_unreference(*fence);
950
951 util_dynarray_fini(&fences);
952 util_dynarray_fini(&syncobjs);
953 }
954
955 void
956 panfrost_batch_set_requirements(struct panfrost_batch *batch)
957 {
958 struct panfrost_context *ctx = batch->ctx;
959
960 if (ctx->rasterizer && ctx->rasterizer->base.multisample)
961 batch->requirements |= PAN_REQ_MSAA;
962
963 if (ctx->depth_stencil && ctx->depth_stencil->depth.writemask)
964 batch->requirements |= PAN_REQ_DEPTH_WRITE;
965 }
966
967 /* Helper to smear a 32-bit color across 128-bit components */
968
969 static void
970 pan_pack_color_32(uint32_t *packed, uint32_t v)
971 {
972 for (unsigned i = 0; i < 4; ++i)
973 packed[i] = v;
974 }
975
976 static void
977 pan_pack_color_64(uint32_t *packed, uint32_t lo, uint32_t hi)
978 {
979 for (unsigned i = 0; i < 4; i += 2) {
980 packed[i + 0] = lo;
981 packed[i + 1] = hi;
982 }
983 }
984
985 static void
986 pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
987 {
988 /* Alpha magicked to 1.0 if there is no alpha */
989
990 bool has_alpha = util_format_has_alpha(format);
991 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
992
993 /* Packed color depends on the framebuffer format */
994
995 const struct util_format_description *desc =
996 util_format_description(format);
997
998 if (util_format_is_rgba8_variant(desc)) {
999 pan_pack_color_32(packed,
1000 (float_to_ubyte(clear_alpha) << 24) |
1001 (float_to_ubyte(color->f[2]) << 16) |
1002 (float_to_ubyte(color->f[1]) << 8) |
1003 (float_to_ubyte(color->f[0]) << 0));
1004 } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
1005 /* First, we convert the components to R5, G6, B5 separately */
1006 unsigned r5 = CLAMP(color->f[0], 0.0, 1.0) * 31.0;
1007 unsigned g6 = CLAMP(color->f[1], 0.0, 1.0) * 63.0;
1008 unsigned b5 = CLAMP(color->f[2], 0.0, 1.0) * 31.0;
1009
1010 /* Then we pack into a sparse u32. TODO: Why these shifts? */
1011 pan_pack_color_32(packed, (b5 << 25) | (g6 << 14) | (r5 << 5));
1012 } else if (format == PIPE_FORMAT_B4G4R4A4_UNORM) {
1013 /* We scale the components against 0xF0 (=240.0), rather than 0xFF */
1014 unsigned r4 = CLAMP(color->f[0], 0.0, 1.0) * 240.0;
1015 unsigned g4 = CLAMP(color->f[1], 0.0, 1.0) * 240.0;
1016 unsigned b4 = CLAMP(color->f[2], 0.0, 1.0) * 240.0;
1017 unsigned a4 = CLAMP(clear_alpha, 0.0, 1.0) * 240.0;
1018
1019 /* Pack on *byte* intervals */
1020 pan_pack_color_32(packed, (a4 << 24) | (b4 << 16) | (g4 << 8) | r4);
1021 } else if (format == PIPE_FORMAT_B5G5R5A1_UNORM) {
1022 /* Scale as expected but shift oddly */
1023 unsigned r5 = round(CLAMP(color->f[0], 0.0, 1.0)) * 31.0;
1024 unsigned g5 = round(CLAMP(color->f[1], 0.0, 1.0)) * 31.0;
1025 unsigned b5 = round(CLAMP(color->f[2], 0.0, 1.0)) * 31.0;
1026 unsigned a1 = round(CLAMP(clear_alpha, 0.0, 1.0)) * 1.0;
1027
1028 pan_pack_color_32(packed, (a1 << 31) | (b5 << 25) | (g5 << 15) | (r5 << 5));
1029 } else {
1030 /* Try Gallium's generic default path. Doesn't work for all
1031 * formats but it's a good guess. */
1032
1033 union util_color out;
1034
1035 if (util_format_is_pure_integer(format)) {
1036 memcpy(out.ui, color->ui, 16);
1037 } else {
1038 util_pack_color(color->f, format, &out);
1039 }
1040
1041 unsigned size = util_format_get_blocksize(format);
1042
1043 if (size == 1) {
1044 unsigned b = out.ui[0];
1045 unsigned s = b | (b << 8);
1046 pan_pack_color_32(packed, s | (s << 16));
1047 } else if (size == 2)
1048 pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
1049 else if (size == 4)
1050 pan_pack_color_32(packed, out.ui[0]);
1051 else if (size == 8)
1052 pan_pack_color_64(packed, out.ui[0], out.ui[1]);
1053 else if (size == 16)
1054 memcpy(packed, out.ui, 16);
1055 else
1056 unreachable("Unknown generic format size packing clear colour");
1057 }
1058 }
1059
1060 void
1061 panfrost_batch_clear(struct panfrost_batch *batch,
1062 unsigned buffers,
1063 const union pipe_color_union *color,
1064 double depth, unsigned stencil)
1065 {
1066 struct panfrost_context *ctx = batch->ctx;
1067
1068 if (buffers & PIPE_CLEAR_COLOR) {
1069 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
1070 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
1071 continue;
1072
1073 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
1074 pan_pack_color(batch->clear_color[i], color, format);
1075 }
1076 }
1077
1078 if (buffers & PIPE_CLEAR_DEPTH) {
1079 batch->clear_depth = depth;
1080 }
1081
1082 if (buffers & PIPE_CLEAR_STENCIL) {
1083 batch->clear_stencil = stencil;
1084 }
1085
1086 batch->clear |= buffers;
1087
1088 /* Clearing affects the entire framebuffer (by definition -- this is
1089 * the Gallium clear callback, which clears the whole framebuffer. If
1090 * the scissor test were enabled from the GL side, the state tracker
1091 * would emit a quad instead and we wouldn't go down this code path) */
1092
1093 panfrost_batch_union_scissor(batch, 0, 0,
1094 ctx->pipe_framebuffer.width,
1095 ctx->pipe_framebuffer.height);
1096 }
1097
1098 static bool
1099 panfrost_batch_compare(const void *a, const void *b)
1100 {
1101 return util_framebuffer_state_equal(a, b);
1102 }
1103
1104 static uint32_t
1105 panfrost_batch_hash(const void *key)
1106 {
1107 return _mesa_hash_data(key, sizeof(struct pipe_framebuffer_state));
1108 }
1109
1110 /* Given a new bounding rectangle (scissor), let the job cover the union of the
1111 * new and old bounding rectangles */
1112
1113 void
1114 panfrost_batch_union_scissor(struct panfrost_batch *batch,
1115 unsigned minx, unsigned miny,
1116 unsigned maxx, unsigned maxy)
1117 {
1118 batch->minx = MIN2(batch->minx, minx);
1119 batch->miny = MIN2(batch->miny, miny);
1120 batch->maxx = MAX2(batch->maxx, maxx);
1121 batch->maxy = MAX2(batch->maxy, maxy);
1122 }
1123
1124 void
1125 panfrost_batch_intersection_scissor(struct panfrost_batch *batch,
1126 unsigned minx, unsigned miny,
1127 unsigned maxx, unsigned maxy)
1128 {
1129 batch->minx = MAX2(batch->minx, minx);
1130 batch->miny = MAX2(batch->miny, miny);
1131 batch->maxx = MIN2(batch->maxx, maxx);
1132 batch->maxy = MIN2(batch->maxy, maxy);
1133 }
1134
1135 /* Are we currently rendering to the screen (rather than an FBO)? */
1136
1137 bool
1138 panfrost_batch_is_scanout(struct panfrost_batch *batch)
1139 {
1140 /* If there is no color buffer, it's an FBO */
1141 if (batch->key.nr_cbufs != 1)
1142 return false;
1143
1144 /* If we're too early that no framebuffer was sent, it's scanout */
1145 if (!batch->key.cbufs[0])
1146 return true;
1147
1148 return batch->key.cbufs[0]->texture->bind & PIPE_BIND_DISPLAY_TARGET ||
1149 batch->key.cbufs[0]->texture->bind & PIPE_BIND_SCANOUT ||
1150 batch->key.cbufs[0]->texture->bind & PIPE_BIND_SHARED;
1151 }
1152
1153 void
1154 panfrost_batch_init(struct panfrost_context *ctx)
1155 {
1156 ctx->batches = _mesa_hash_table_create(ctx,
1157 panfrost_batch_hash,
1158 panfrost_batch_compare);
1159 ctx->accessed_bos = _mesa_hash_table_create(ctx, _mesa_hash_pointer,
1160 _mesa_key_pointer_equal);
1161 }