2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2014-2017 Broadcom
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:
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
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
28 #include "drm-uapi/panfrost_drm.h"
31 #include "pan_context.h"
32 #include "util/hash_table.h"
33 #include "util/ralloc.h"
34 #include "util/format/u_format.h"
35 #include "util/u_pack_color.h"
36 #include "util/rounding.h"
38 #include "pandecode/decode.h"
39 #include "panfrost-quirks.h"
41 /* panfrost_bo_access is here to help us keep track of batch accesses to BOs
42 * and build a proper dependency graph such that batches can be pipelined for
43 * better GPU utilization.
45 * Each accessed BO has a corresponding entry in the ->accessed_bos hash table.
46 * A BO is either being written or read at any time, that's what the type field
48 * When the last access is a write, the batch writing the BO might have read
49 * dependencies (readers that have not been executed yet and want to read the
50 * previous BO content), and when the last access is a read, all readers might
51 * depend on another batch to push its results to memory. That's what the
52 * readers/writers keep track off.
53 * There can only be one writer at any given time, if a new batch wants to
54 * write to the same BO, a dependency will be added between the new writer and
55 * the old writer (at the batch level), and panfrost_bo_access->writer will be
56 * updated to point to the new writer.
58 struct panfrost_bo_access
{
60 struct util_dynarray readers
;
61 struct panfrost_batch_fence
*writer
;
64 static struct panfrost_batch_fence
*
65 panfrost_create_batch_fence(struct panfrost_batch
*batch
)
67 struct panfrost_batch_fence
*fence
;
70 fence
= rzalloc(NULL
, struct panfrost_batch_fence
);
72 pipe_reference_init(&fence
->reference
, 1);
73 fence
->ctx
= batch
->ctx
;
75 ret
= drmSyncobjCreate(pan_device(batch
->ctx
->base
.screen
)->fd
, 0,
83 panfrost_free_batch_fence(struct panfrost_batch_fence
*fence
)
85 drmSyncobjDestroy(pan_device(fence
->ctx
->base
.screen
)->fd
,
91 panfrost_batch_fence_unreference(struct panfrost_batch_fence
*fence
)
93 if (pipe_reference(&fence
->reference
, NULL
))
94 panfrost_free_batch_fence(fence
);
98 panfrost_batch_fence_reference(struct panfrost_batch_fence
*fence
)
100 pipe_reference(NULL
, &fence
->reference
);
103 static struct panfrost_batch
*
104 panfrost_create_batch(struct panfrost_context
*ctx
,
105 const struct pipe_framebuffer_state
*key
)
107 struct panfrost_batch
*batch
= rzalloc(ctx
, struct panfrost_batch
);
111 batch
->bos
= _mesa_hash_table_create(batch
, _mesa_hash_pointer
,
112 _mesa_key_pointer_equal
);
114 batch
->minx
= batch
->miny
= ~0;
115 batch
->maxx
= batch
->maxy
= 0;
116 batch
->transient_offset
= 0;
118 batch
->out_sync
= panfrost_create_batch_fence(batch
);
119 util_copy_framebuffer_state(&batch
->key
, key
);
125 panfrost_freeze_batch(struct panfrost_batch
*batch
)
127 struct panfrost_context
*ctx
= batch
->ctx
;
128 struct hash_entry
*entry
;
130 /* Remove the entry in the FBO -> batch hash table if the batch
131 * matches. This way, next draws/clears targeting this FBO will trigger
132 * the creation of a new batch.
134 entry
= _mesa_hash_table_search(ctx
->batches
, &batch
->key
);
135 if (entry
&& entry
->data
== batch
)
136 _mesa_hash_table_remove(ctx
->batches
, entry
);
138 /* If this is the bound batch, the panfrost_context parameters are
139 * relevant so submitting it invalidates those parameters, but if it's
140 * not bound, the context parameters are for some other batch so we
141 * can't invalidate them.
143 if (ctx
->batch
== batch
) {
144 panfrost_invalidate_frame(ctx
);
149 #ifdef PAN_BATCH_DEBUG
150 static bool panfrost_batch_is_frozen(struct panfrost_batch
*batch
)
152 struct panfrost_context
*ctx
= batch
->ctx
;
153 struct hash_entry
*entry
;
155 entry
= _mesa_hash_table_search(ctx
->batches
, &batch
->key
);
156 if (entry
&& entry
->data
== batch
)
159 if (ctx
->batch
== batch
)
167 panfrost_free_batch(struct panfrost_batch
*batch
)
172 #ifdef PAN_BATCH_DEBUG
173 assert(panfrost_batch_is_frozen(batch
));
176 hash_table_foreach(batch
->bos
, entry
)
177 panfrost_bo_unreference((struct panfrost_bo
*)entry
->key
);
179 util_dynarray_foreach(&batch
->dependencies
,
180 struct panfrost_batch_fence
*, dep
) {
181 panfrost_batch_fence_unreference(*dep
);
184 /* The out_sync fence lifetime is different from the the batch one
185 * since other batches might want to wait on a fence of already
186 * submitted/signaled batch. All we need to do here is make sure the
187 * fence does not point to an invalid batch, which the core will
188 * interpret as 'batch is already submitted'.
190 batch
->out_sync
->batch
= NULL
;
191 panfrost_batch_fence_unreference(batch
->out_sync
);
193 util_unreference_framebuffer_state(&batch
->key
);
197 #ifdef PAN_BATCH_DEBUG
199 panfrost_dep_graph_contains_batch(struct panfrost_batch
*root
,
200 struct panfrost_batch
*batch
)
205 util_dynarray_foreach(&root
->dependencies
,
206 struct panfrost_batch_fence
*, dep
) {
207 if ((*dep
)->batch
== batch
||
208 panfrost_dep_graph_contains_batch((*dep
)->batch
, batch
))
217 panfrost_batch_add_dep(struct panfrost_batch
*batch
,
218 struct panfrost_batch_fence
*newdep
)
220 if (batch
== newdep
->batch
)
223 /* We might want to turn ->dependencies into a set if the number of
224 * deps turns out to be big enough to make this 'is dep already there'
225 * search inefficient.
227 util_dynarray_foreach(&batch
->dependencies
,
228 struct panfrost_batch_fence
*, dep
) {
233 #ifdef PAN_BATCH_DEBUG
234 /* Make sure the dependency graph is acyclic. */
235 assert(!panfrost_dep_graph_contains_batch(newdep
->batch
, batch
));
238 panfrost_batch_fence_reference(newdep
);
239 util_dynarray_append(&batch
->dependencies
,
240 struct panfrost_batch_fence
*, newdep
);
242 /* We now have a batch depending on us, let's make sure new draw/clear
243 * calls targeting the same FBO use a new batch object.
246 panfrost_freeze_batch(newdep
->batch
);
249 static struct panfrost_batch
*
250 panfrost_get_batch(struct panfrost_context
*ctx
,
251 const struct pipe_framebuffer_state
*key
)
253 /* Lookup the job first */
254 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->batches
, key
);
259 /* Otherwise, let's create a job */
261 struct panfrost_batch
*batch
= panfrost_create_batch(ctx
, key
);
263 /* Save the created job */
264 _mesa_hash_table_insert(ctx
->batches
, &batch
->key
, batch
);
269 /* Get the job corresponding to the FBO we're currently rendering into */
271 struct panfrost_batch
*
272 panfrost_get_batch_for_fbo(struct panfrost_context
*ctx
)
274 /* If we're wallpapering, we special case to workaround
277 if (ctx
->wallpaper_batch
)
278 return ctx
->wallpaper_batch
;
280 /* If we already began rendering, use that */
283 assert(util_framebuffer_state_equal(&ctx
->batch
->key
,
284 &ctx
->pipe_framebuffer
));
288 /* If not, look up the job */
289 struct panfrost_batch
*batch
= panfrost_get_batch(ctx
,
290 &ctx
->pipe_framebuffer
);
292 /* Set this job as the current FBO job. Will be reset when updating the
293 * FB state and when submitting or releasing a job.
299 struct panfrost_batch
*
300 panfrost_get_fresh_batch_for_fbo(struct panfrost_context
*ctx
)
302 struct panfrost_batch
*batch
;
304 batch
= panfrost_get_batch(ctx
, &ctx
->pipe_framebuffer
);
306 /* The batch has no draw/clear queued, let's return it directly.
307 * Note that it's perfectly fine to re-use a batch with an
308 * existing clear, we'll just update it with the new clear request.
310 if (!batch
->first_job
)
313 /* Otherwise, we need to freeze the existing one and instantiate a new
316 panfrost_freeze_batch(batch
);
317 return panfrost_get_batch(ctx
, &ctx
->pipe_framebuffer
);
321 panfrost_batch_fence_is_signaled(struct panfrost_batch_fence
*fence
)
326 /* Batch has not been submitted yet. */
330 int ret
= drmSyncobjWait(pan_device(fence
->ctx
->base
.screen
)->fd
,
331 &fence
->syncobj
, 1, 0, 0, NULL
);
333 /* Cache whether the fence was signaled */
334 fence
->signaled
= ret
>= 0;
335 return fence
->signaled
;
339 panfrost_bo_access_gc_fences(struct panfrost_context
*ctx
,
340 struct panfrost_bo_access
*access
,
341 const struct panfrost_bo
*bo
)
343 if (access
->writer
&& panfrost_batch_fence_is_signaled(access
->writer
)) {
344 panfrost_batch_fence_unreference(access
->writer
);
345 access
->writer
= NULL
;
348 struct panfrost_batch_fence
**readers_array
= util_dynarray_begin(&access
->readers
);
349 struct panfrost_batch_fence
**new_readers
= readers_array
;
351 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
356 if (panfrost_batch_fence_is_signaled(*reader
)) {
357 panfrost_batch_fence_unreference(*reader
);
360 /* Build a new array of only unsignaled fences in-place */
361 *(new_readers
++) = *reader
;
365 if (!util_dynarray_resize(&access
->readers
, struct panfrost_batch_fence
*,
366 new_readers
- readers_array
) &&
367 new_readers
!= readers_array
)
368 unreachable("Invalid dynarray access->readers");
371 /* Collect signaled fences to keep the kernel-side syncobj-map small. The
372 * idea is to collect those signaled fences at the end of each flush_all
373 * call. This function is likely to collect only fences from previous
374 * batch flushes not the one that have just have just been submitted and
375 * are probably still in flight when we trigger the garbage collection.
376 * Anyway, we need to do this garbage collection at some point if we don't
377 * want the BO access map to keep invalid entries around and retain
381 panfrost_gc_fences(struct panfrost_context
*ctx
)
383 hash_table_foreach(ctx
->accessed_bos
, entry
) {
384 struct panfrost_bo_access
*access
= entry
->data
;
387 panfrost_bo_access_gc_fences(ctx
, access
, entry
->key
);
388 if (!util_dynarray_num_elements(&access
->readers
,
389 struct panfrost_batch_fence
*) &&
392 _mesa_hash_table_remove(ctx
->accessed_bos
, entry
);
397 #ifdef PAN_BATCH_DEBUG
399 panfrost_batch_in_readers(struct panfrost_batch
*batch
,
400 struct panfrost_bo_access
*access
)
402 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
404 if (*reader
&& (*reader
)->batch
== batch
)
413 panfrost_batch_update_bo_access(struct panfrost_batch
*batch
,
414 struct panfrost_bo
*bo
, uint32_t access_type
,
415 bool already_accessed
)
417 struct panfrost_context
*ctx
= batch
->ctx
;
418 struct panfrost_bo_access
*access
;
419 uint32_t old_access_type
;
420 struct hash_entry
*entry
;
422 assert(access_type
== PAN_BO_ACCESS_WRITE
||
423 access_type
== PAN_BO_ACCESS_READ
);
425 entry
= _mesa_hash_table_search(ctx
->accessed_bos
, bo
);
426 access
= entry
? entry
->data
: NULL
;
428 old_access_type
= access
->type
;
430 access
= rzalloc(ctx
, struct panfrost_bo_access
);
431 util_dynarray_init(&access
->readers
, access
);
432 _mesa_hash_table_insert(ctx
->accessed_bos
, bo
, access
);
433 /* We are the first to access this BO, let's initialize
434 * old_access_type to our own access type in that case.
436 old_access_type
= access_type
;
437 access
->type
= access_type
;
442 if (access_type
== PAN_BO_ACCESS_WRITE
&&
443 old_access_type
== PAN_BO_ACCESS_READ
) {
444 /* Previous access was a read and we want to write this BO.
445 * We first need to add explicit deps between our batch and
446 * the previous readers.
448 util_dynarray_foreach(&access
->readers
,
449 struct panfrost_batch_fence
*, reader
) {
450 /* We were already reading the BO, no need to add a dep
451 * on ourself (the acyclic check would complain about
454 if (!(*reader
) || (*reader
)->batch
== batch
)
457 panfrost_batch_add_dep(batch
, *reader
);
459 panfrost_batch_fence_reference(batch
->out_sync
);
461 /* We now are the new writer. */
462 access
->writer
= batch
->out_sync
;
463 access
->type
= access_type
;
465 /* Release the previous readers and reset the readers array. */
466 util_dynarray_foreach(&access
->readers
,
467 struct panfrost_batch_fence
*,
471 panfrost_batch_fence_unreference(*reader
);
474 util_dynarray_clear(&access
->readers
);
475 } else if (access_type
== PAN_BO_ACCESS_WRITE
&&
476 old_access_type
== PAN_BO_ACCESS_WRITE
) {
477 /* Previous access was a write and we want to write this BO.
478 * First check if we were the previous writer, in that case
479 * there's nothing to do. Otherwise we need to add a
480 * dependency between the new writer and the old one.
482 if (access
->writer
!= batch
->out_sync
) {
483 if (access
->writer
) {
484 panfrost_batch_add_dep(batch
, access
->writer
);
485 panfrost_batch_fence_unreference(access
->writer
);
487 panfrost_batch_fence_reference(batch
->out_sync
);
488 access
->writer
= batch
->out_sync
;
490 } else if (access_type
== PAN_BO_ACCESS_READ
&&
491 old_access_type
== PAN_BO_ACCESS_WRITE
) {
492 /* Previous access was a write and we want to read this BO.
493 * First check if we were the previous writer, in that case
494 * we want to keep the access type unchanged, as a write is
495 * more constraining than a read.
497 if (access
->writer
!= batch
->out_sync
) {
498 /* Add a dependency on the previous writer. */
499 panfrost_batch_add_dep(batch
, access
->writer
);
501 /* The previous access was a write, there's no reason
502 * to have entries in the readers array.
504 assert(!util_dynarray_num_elements(&access
->readers
,
505 struct panfrost_batch_fence
*));
507 /* Add ourselves to the readers array. */
508 panfrost_batch_fence_reference(batch
->out_sync
);
509 util_dynarray_append(&access
->readers
,
510 struct panfrost_batch_fence
*,
512 access
->type
= PAN_BO_ACCESS_READ
;
515 /* We already accessed this BO before, so we should already be
516 * in the reader array.
518 #ifdef PAN_BATCH_DEBUG
519 if (already_accessed
) {
520 assert(panfrost_batch_in_readers(batch
, access
));
525 /* Previous access was a read and we want to read this BO.
526 * Add ourselves to the readers array and add a dependency on
527 * the previous writer if any.
529 panfrost_batch_fence_reference(batch
->out_sync
);
530 util_dynarray_append(&access
->readers
,
531 struct panfrost_batch_fence
*,
535 panfrost_batch_add_dep(batch
, access
->writer
);
540 panfrost_batch_add_bo(struct panfrost_batch
*batch
, struct panfrost_bo
*bo
,
546 struct hash_entry
*entry
;
547 uint32_t old_flags
= 0;
549 entry
= _mesa_hash_table_search(batch
->bos
, bo
);
551 entry
= _mesa_hash_table_insert(batch
->bos
, bo
,
552 (void *)(uintptr_t)flags
);
553 panfrost_bo_reference(bo
);
555 old_flags
= (uintptr_t)entry
->data
;
557 /* All batches have to agree on the shared flag. */
558 assert((old_flags
& PAN_BO_ACCESS_SHARED
) ==
559 (flags
& PAN_BO_ACCESS_SHARED
));
564 if (old_flags
== flags
)
568 entry
->data
= (void *)(uintptr_t)flags
;
570 /* If this is not a shared BO, we don't really care about dependency
573 if (!(flags
& PAN_BO_ACCESS_SHARED
))
576 /* All dependencies should have been flushed before we execute the
577 * wallpaper draw, so it should be harmless to skip the
578 * update_bo_access() call.
580 if (batch
== batch
->ctx
->wallpaper_batch
)
583 /* Only pass R/W flags to the dep tracking logic. */
584 assert(flags
& PAN_BO_ACCESS_RW
);
585 flags
= (flags
& PAN_BO_ACCESS_WRITE
) ?
586 PAN_BO_ACCESS_WRITE
: PAN_BO_ACCESS_READ
;
587 panfrost_batch_update_bo_access(batch
, bo
, flags
, old_flags
!= 0);
591 panfrost_batch_add_resource_bos(struct panfrost_batch
*batch
,
592 struct panfrost_resource
*rsrc
,
595 panfrost_batch_add_bo(batch
, rsrc
->bo
, flags
);
597 for (unsigned i
= 0; i
< MAX_MIP_LEVELS
; i
++)
598 if (rsrc
->slices
[i
].checksum_bo
)
599 panfrost_batch_add_bo(batch
, rsrc
->slices
[i
].checksum_bo
, flags
);
602 void panfrost_batch_add_fbo_bos(struct panfrost_batch
*batch
)
604 uint32_t flags
= PAN_BO_ACCESS_SHARED
| PAN_BO_ACCESS_WRITE
|
605 PAN_BO_ACCESS_VERTEX_TILER
|
606 PAN_BO_ACCESS_FRAGMENT
;
608 for (unsigned i
= 0; i
< batch
->key
.nr_cbufs
; ++i
) {
609 struct panfrost_resource
*rsrc
= pan_resource(batch
->key
.cbufs
[i
]->texture
);
610 panfrost_batch_add_resource_bos(batch
, rsrc
, flags
);
613 if (batch
->key
.zsbuf
) {
614 struct panfrost_resource
*rsrc
= pan_resource(batch
->key
.zsbuf
->texture
);
615 panfrost_batch_add_resource_bos(batch
, rsrc
, flags
);
620 panfrost_batch_create_bo(struct panfrost_batch
*batch
, size_t size
,
621 uint32_t create_flags
, uint32_t access_flags
)
623 struct panfrost_bo
*bo
;
625 bo
= pan_bo_create(pan_device(batch
->ctx
->base
.screen
), size
,
627 panfrost_batch_add_bo(batch
, bo
, access_flags
);
629 /* panfrost_batch_add_bo() has retained a reference and
630 * pan_bo_create() initialize the refcnt to 1, so let's
631 * unreference the BO here so it gets released when the batch is
632 * destroyed (unless it's retained by someone else in the meantime).
634 panfrost_bo_unreference(bo
);
638 /* Returns the polygon list's GPU address if available, or otherwise allocates
639 * the polygon list. It's perfectly fast to use allocate/free BO directly,
640 * since we'll hit the BO cache and this is one-per-batch anyway. */
643 panfrost_batch_get_polygon_list(struct panfrost_batch
*batch
, unsigned size
)
645 if (batch
->polygon_list
) {
646 assert(batch
->polygon_list
->size
>= size
);
648 /* Create the BO as invisible, as there's no reason to map */
649 size
= util_next_power_of_two(size
);
651 batch
->polygon_list
= panfrost_batch_create_bo(batch
, size
,
653 PAN_BO_ACCESS_PRIVATE
|
655 PAN_BO_ACCESS_VERTEX_TILER
|
656 PAN_BO_ACCESS_FRAGMENT
);
659 return batch
->polygon_list
->gpu
;
663 panfrost_batch_get_scratchpad(struct panfrost_batch
*batch
,
665 unsigned thread_tls_alloc
,
668 unsigned size
= panfrost_get_total_stack_size(shift
,
672 if (batch
->scratchpad
) {
673 assert(batch
->scratchpad
->size
>= size
);
675 batch
->scratchpad
= panfrost_batch_create_bo(batch
, size
,
677 PAN_BO_ACCESS_PRIVATE
|
679 PAN_BO_ACCESS_VERTEX_TILER
|
680 PAN_BO_ACCESS_FRAGMENT
);
683 return batch
->scratchpad
;
687 panfrost_batch_get_shared_memory(struct panfrost_batch
*batch
,
689 unsigned workgroup_count
)
691 if (batch
->shared_memory
) {
692 assert(batch
->shared_memory
->size
>= size
);
694 batch
->shared_memory
= panfrost_batch_create_bo(batch
, size
,
696 PAN_BO_ACCESS_PRIVATE
|
698 PAN_BO_ACCESS_VERTEX_TILER
);
701 return batch
->shared_memory
;
705 panfrost_batch_get_tiler_heap(struct panfrost_batch
*batch
)
707 if (batch
->tiler_heap
)
708 return batch
->tiler_heap
;
710 batch
->tiler_heap
= panfrost_batch_create_bo(batch
, 4096 * 4096,
713 PAN_BO_ACCESS_PRIVATE
|
715 PAN_BO_ACCESS_VERTEX_TILER
|
716 PAN_BO_ACCESS_FRAGMENT
);
717 assert(batch
->tiler_heap
);
718 return batch
->tiler_heap
;
722 panfrost_batch_get_tiler_meta(struct panfrost_batch
*batch
, unsigned vertex_count
)
727 if (batch
->tiler_meta
)
728 return batch
->tiler_meta
;
730 struct panfrost_bo
*tiler_heap
;
731 tiler_heap
= panfrost_batch_get_tiler_heap(batch
);
733 struct bifrost_tiler_heap_meta tiler_heap_meta
= {
734 .heap_size
= tiler_heap
->size
,
735 .tiler_heap_start
= tiler_heap
->gpu
,
736 .tiler_heap_free
= tiler_heap
->gpu
,
737 .tiler_heap_end
= tiler_heap
->gpu
+ tiler_heap
->size
,
739 .unk7e007e
= 0x7e007e,
742 struct bifrost_tiler_meta tiler_meta
= {
743 .hierarchy_mask
= 0x28,
745 .width
= MALI_POSITIVE(batch
->key
.width
),
746 .height
= MALI_POSITIVE(batch
->key
.height
),
747 .tiler_heap_meta
= panfrost_upload_transient(batch
, &tiler_heap_meta
, sizeof(tiler_heap_meta
)),
750 batch
->tiler_meta
= panfrost_upload_transient(batch
, &tiler_meta
, sizeof(tiler_meta
));
751 return batch
->tiler_meta
;
755 panfrost_batch_get_tiler_dummy(struct panfrost_batch
*batch
)
757 struct panfrost_device
*dev
= pan_device(batch
->ctx
->base
.screen
);
759 uint32_t create_flags
= 0;
761 if (batch
->tiler_dummy
)
762 return batch
->tiler_dummy
;
764 if (!(dev
->quirks
& MIDGARD_NO_HIER_TILING
))
765 create_flags
= PAN_BO_INVISIBLE
;
767 batch
->tiler_dummy
= panfrost_batch_create_bo(batch
, 4096,
769 PAN_BO_ACCESS_PRIVATE
|
771 PAN_BO_ACCESS_VERTEX_TILER
|
772 PAN_BO_ACCESS_FRAGMENT
);
773 assert(batch
->tiler_dummy
);
774 return batch
->tiler_dummy
;
778 panfrost_batch_draw_wallpaper(struct panfrost_batch
*batch
)
780 /* Color 0 is cleared, no need to draw the wallpaper.
781 * TODO: MRT wallpapers.
783 if (batch
->clear
& PIPE_CLEAR_COLOR0
)
786 /* Nothing to reload? TODO: MRT wallpapers */
787 if (batch
->key
.cbufs
[0] == NULL
)
790 /* No draw calls, and no clear on the depth/stencil bufs.
791 * Drawing the wallpaper would be useless.
793 if (!batch
->tiler_dep
&&
794 !(batch
->clear
& PIPE_CLEAR_DEPTHSTENCIL
))
797 /* Check if the buffer has any content on it worth preserving */
799 struct pipe_surface
*surf
= batch
->key
.cbufs
[0];
800 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
801 unsigned level
= surf
->u
.tex
.level
;
803 if (!rsrc
->slices
[level
].initialized
)
806 batch
->ctx
->wallpaper_batch
= batch
;
808 /* Clamp the rendering area to the damage extent. The
809 * KHR_partial_update() spec states that trying to render outside of
810 * the damage region is "undefined behavior", so we should be safe.
812 unsigned damage_width
= (rsrc
->damage
.extent
.maxx
- rsrc
->damage
.extent
.minx
);
813 unsigned damage_height
= (rsrc
->damage
.extent
.maxy
- rsrc
->damage
.extent
.miny
);
815 if (damage_width
&& damage_height
) {
816 panfrost_batch_intersection_scissor(batch
,
817 rsrc
->damage
.extent
.minx
,
818 rsrc
->damage
.extent
.miny
,
819 rsrc
->damage
.extent
.maxx
,
820 rsrc
->damage
.extent
.maxy
);
823 /* FIXME: Looks like aligning on a tile is not enough, but
824 * aligning on twice the tile size seems to works. We don't
825 * know exactly what happens here but this deserves extra
826 * investigation to figure it out.
828 batch
->minx
= batch
->minx
& ~((MALI_TILE_LENGTH
* 2) - 1);
829 batch
->miny
= batch
->miny
& ~((MALI_TILE_LENGTH
* 2) - 1);
830 batch
->maxx
= MIN2(ALIGN_POT(batch
->maxx
, MALI_TILE_LENGTH
* 2),
832 batch
->maxy
= MIN2(ALIGN_POT(batch
->maxy
, MALI_TILE_LENGTH
* 2),
835 struct pipe_scissor_state damage
;
836 struct pipe_box rects
[4];
838 /* Clamp the damage box to the rendering area. */
839 damage
.minx
= MAX2(batch
->minx
, rsrc
->damage
.biggest_rect
.x
);
840 damage
.miny
= MAX2(batch
->miny
, rsrc
->damage
.biggest_rect
.y
);
841 damage
.maxx
= MIN2(batch
->maxx
,
842 rsrc
->damage
.biggest_rect
.x
+
843 rsrc
->damage
.biggest_rect
.width
);
844 damage
.maxx
= MAX2(damage
.maxx
, damage
.minx
);
845 damage
.maxy
= MIN2(batch
->maxy
,
846 rsrc
->damage
.biggest_rect
.y
+
847 rsrc
->damage
.biggest_rect
.height
);
848 damage
.maxy
= MAX2(damage
.maxy
, damage
.miny
);
850 /* One damage rectangle means we can end up with at most 4 reload
852 * 1: left region, only exists if damage.x > 0
853 * 2: right region, only exists if damage.x + damage.width < fb->width
854 * 3: top region, only exists if damage.y > 0. The intersection with
855 * the left and right regions are dropped
856 * 4: bottom region, only exists if damage.y + damage.height < fb->height.
857 * The intersection with the left and right regions are dropped
859 * ____________________________
866 * |_______|___________|______|
868 u_box_2d(batch
->minx
, batch
->miny
, damage
.minx
- batch
->minx
,
869 batch
->maxy
- batch
->miny
, &rects
[0]);
870 u_box_2d(damage
.maxx
, batch
->miny
, batch
->maxx
- damage
.maxx
,
871 batch
->maxy
- batch
->miny
, &rects
[1]);
872 u_box_2d(damage
.minx
, batch
->miny
, damage
.maxx
- damage
.minx
,
873 damage
.miny
- batch
->miny
, &rects
[2]);
874 u_box_2d(damage
.minx
, damage
.maxy
, damage
.maxx
- damage
.minx
,
875 batch
->maxy
- damage
.maxy
, &rects
[3]);
877 for (unsigned i
= 0; i
< 4; i
++) {
878 /* Width and height are always >= 0 even if width is declared as a
879 * signed integer: u_box_2d() helper takes unsigned args and
880 * panfrost_set_damage_region() is taking care of clamping
883 if (!rects
[i
].width
|| !rects
[i
].height
)
886 /* Blit the wallpaper in */
887 panfrost_blit_wallpaper(batch
->ctx
, &rects
[i
]);
889 batch
->ctx
->wallpaper_batch
= NULL
;
893 panfrost_batch_submit_ioctl(struct panfrost_batch
*batch
,
894 mali_ptr first_job_desc
,
897 struct panfrost_context
*ctx
= batch
->ctx
;
898 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
899 struct panfrost_device
*dev
= pan_device(gallium
->screen
);
900 struct drm_panfrost_submit submit
= {0,};
901 uint32_t *bo_handles
, *in_syncs
= NULL
;
902 bool is_fragment_shader
;
905 is_fragment_shader
= (reqs
& PANFROST_JD_REQ_FS
) && batch
->first_job
;
906 if (is_fragment_shader
)
907 submit
.in_sync_count
= 1;
909 submit
.in_sync_count
= util_dynarray_num_elements(&batch
->dependencies
,
910 struct panfrost_batch_fence
*);
912 if (submit
.in_sync_count
) {
913 in_syncs
= calloc(submit
.in_sync_count
, sizeof(*in_syncs
));
917 /* The fragment job always depends on the vertex/tiler job if there's
920 if (is_fragment_shader
) {
921 in_syncs
[0] = batch
->out_sync
->syncobj
;
925 util_dynarray_foreach(&batch
->dependencies
,
926 struct panfrost_batch_fence
*, dep
)
927 in_syncs
[i
++] = (*dep
)->syncobj
;
930 submit
.in_syncs
= (uintptr_t)in_syncs
;
931 submit
.out_sync
= batch
->out_sync
->syncobj
;
932 submit
.jc
= first_job_desc
;
933 submit
.requirements
= reqs
;
935 bo_handles
= calloc(batch
->bos
->entries
, sizeof(*bo_handles
));
938 hash_table_foreach(batch
->bos
, entry
) {
939 struct panfrost_bo
*bo
= (struct panfrost_bo
*)entry
->key
;
940 uint32_t flags
= (uintptr_t)entry
->data
;
942 assert(bo
->gem_handle
> 0);
943 bo_handles
[submit
.bo_handle_count
++] = bo
->gem_handle
;
945 /* Update the BO access flags so that panfrost_bo_wait() knows
946 * about all pending accesses.
947 * We only keep the READ/WRITE info since this is all the BO
948 * wait logic cares about.
949 * We also preserve existing flags as this batch might not
950 * be the first one to access the BO.
952 bo
->gpu_access
|= flags
& (PAN_BO_ACCESS_RW
);
955 submit
.bo_handles
= (u64
) (uintptr_t) bo_handles
;
956 ret
= drmIoctl(dev
->fd
, DRM_IOCTL_PANFROST_SUBMIT
, &submit
);
961 DBG("Error submitting: %m\n");
965 /* Trace the job if we're doing that */
966 if (pan_debug
& (PAN_DBG_TRACE
| PAN_DBG_SYNC
)) {
967 /* Wait so we can get errors reported back */
968 drmSyncobjWait(dev
->fd
, &batch
->out_sync
->syncobj
, 1,
971 /* Trace gets priority over sync */
972 bool minimal
= !(pan_debug
& PAN_DBG_TRACE
);
973 pandecode_jc(submit
.jc
, dev
->quirks
& IS_BIFROST
, dev
->gpu_id
, minimal
);
980 panfrost_batch_submit_jobs(struct panfrost_batch
*batch
)
982 bool has_draws
= batch
->first_job
;
986 ret
= panfrost_batch_submit_ioctl(batch
, batch
->first_job
, 0);
990 if (batch
->tiler_dep
|| batch
->clear
) {
991 mali_ptr fragjob
= panfrost_fragment_job(batch
, has_draws
);
992 ret
= panfrost_batch_submit_ioctl(batch
, fragjob
, PANFROST_JD_REQ_FS
);
1000 panfrost_batch_submit(struct panfrost_batch
*batch
)
1004 /* Submit the dependencies first. */
1005 util_dynarray_foreach(&batch
->dependencies
,
1006 struct panfrost_batch_fence
*, dep
) {
1008 panfrost_batch_submit((*dep
)->batch
);
1013 /* Nothing to do! */
1014 if (!batch
->first_job
&& !batch
->clear
) {
1015 /* Mark the fence as signaled so the fence logic does not try
1018 batch
->out_sync
->signaled
= true;
1022 panfrost_batch_draw_wallpaper(batch
);
1024 /* Now that all draws are in, we can finally prepare the
1025 * FBD for the batch */
1027 if (batch
->framebuffer
.gpu
&& batch
->first_job
) {
1028 struct panfrost_context
*ctx
= batch
->ctx
;
1029 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
1030 struct panfrost_device
*dev
= pan_device(gallium
->screen
);
1032 if (dev
->quirks
& MIDGARD_SFBD
)
1033 panfrost_attach_sfbd(batch
, ~0);
1035 panfrost_attach_mfbd(batch
, ~0);
1038 panfrost_scoreboard_initialize_tiler(batch
);
1040 ret
= panfrost_batch_submit_jobs(batch
);
1043 DBG("panfrost_batch_submit failed: %d\n", ret
);
1045 /* We must reset the damage info of our render targets here even
1046 * though a damage reset normally happens when the DRI layer swaps
1047 * buffers. That's because there can be implicit flushes the GL
1048 * app is not aware of, and those might impact the damage region: if
1049 * part of the damaged portion is drawn during those implicit flushes,
1050 * you have to reload those areas before next draws are pushed, and
1051 * since the driver can't easily know what's been modified by the draws
1052 * it flushed, the easiest solution is to reload everything.
1054 for (unsigned i
= 0; i
< batch
->key
.nr_cbufs
; i
++) {
1055 struct panfrost_resource
*res
;
1057 if (!batch
->key
.cbufs
[i
])
1060 res
= pan_resource(batch
->key
.cbufs
[i
]->texture
);
1061 panfrost_resource_reset_damage(res
);
1065 panfrost_freeze_batch(batch
);
1066 panfrost_free_batch(batch
);
1070 panfrost_flush_all_batches(struct panfrost_context
*ctx
, bool wait
)
1072 struct util_dynarray fences
, syncobjs
;
1075 util_dynarray_init(&fences
, NULL
);
1076 util_dynarray_init(&syncobjs
, NULL
);
1079 hash_table_foreach(ctx
->batches
, hentry
) {
1080 struct panfrost_batch
*batch
= hentry
->data
;
1085 panfrost_batch_fence_reference(batch
->out_sync
);
1086 util_dynarray_append(&fences
, struct panfrost_batch_fence
*,
1088 util_dynarray_append(&syncobjs
, uint32_t,
1089 batch
->out_sync
->syncobj
);
1092 panfrost_batch_submit(batch
);
1095 assert(!ctx
->batches
->entries
);
1097 /* Collect batch fences before returning */
1098 panfrost_gc_fences(ctx
);
1103 drmSyncobjWait(pan_device(ctx
->base
.screen
)->fd
,
1104 util_dynarray_begin(&syncobjs
),
1105 util_dynarray_num_elements(&syncobjs
, uint32_t),
1106 INT64_MAX
, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL
, NULL
);
1108 util_dynarray_foreach(&fences
, struct panfrost_batch_fence
*, fence
)
1109 panfrost_batch_fence_unreference(*fence
);
1111 util_dynarray_fini(&fences
);
1112 util_dynarray_fini(&syncobjs
);
1116 panfrost_pending_batches_access_bo(struct panfrost_context
*ctx
,
1117 const struct panfrost_bo
*bo
)
1119 struct panfrost_bo_access
*access
;
1120 struct hash_entry
*hentry
;
1122 hentry
= _mesa_hash_table_search(ctx
->accessed_bos
, bo
);
1123 access
= hentry
? hentry
->data
: NULL
;
1127 if (access
->writer
&& access
->writer
->batch
)
1130 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
1132 if (*reader
&& (*reader
)->batch
)
1140 panfrost_flush_batches_accessing_bo(struct panfrost_context
*ctx
,
1141 struct panfrost_bo
*bo
,
1142 uint32_t access_type
)
1144 struct panfrost_bo_access
*access
;
1145 struct hash_entry
*hentry
;
1147 /* It doesn't make any to flush only the readers. */
1148 assert(access_type
== PAN_BO_ACCESS_WRITE
||
1149 access_type
== PAN_BO_ACCESS_RW
);
1151 hentry
= _mesa_hash_table_search(ctx
->accessed_bos
, bo
);
1152 access
= hentry
? hentry
->data
: NULL
;
1156 if (access_type
& PAN_BO_ACCESS_WRITE
&& access
->writer
&&
1157 access
->writer
->batch
)
1158 panfrost_batch_submit(access
->writer
->batch
);
1160 if (!(access_type
& PAN_BO_ACCESS_READ
))
1163 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
1165 if (*reader
&& (*reader
)->batch
)
1166 panfrost_batch_submit((*reader
)->batch
);
1171 panfrost_batch_set_requirements(struct panfrost_batch
*batch
)
1173 struct panfrost_context
*ctx
= batch
->ctx
;
1175 if (ctx
->rasterizer
&& ctx
->rasterizer
->base
.multisample
)
1176 batch
->requirements
|= PAN_REQ_MSAA
;
1178 if (ctx
->depth_stencil
&& ctx
->depth_stencil
->depth
.writemask
)
1179 batch
->requirements
|= PAN_REQ_DEPTH_WRITE
;
1183 panfrost_batch_adjust_stack_size(struct panfrost_batch
*batch
)
1185 struct panfrost_context
*ctx
= batch
->ctx
;
1187 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; ++i
) {
1188 struct panfrost_shader_state
*ss
;
1190 ss
= panfrost_get_shader_state(ctx
, i
);
1194 batch
->stack_size
= MAX2(batch
->stack_size
, ss
->stack_size
);
1198 /* Helper to smear a 32-bit color across 128-bit components */
1201 pan_pack_color_32(uint32_t *packed
, uint32_t v
)
1203 for (unsigned i
= 0; i
< 4; ++i
)
1208 pan_pack_color_64(uint32_t *packed
, uint32_t lo
, uint32_t hi
)
1210 for (unsigned i
= 0; i
< 4; i
+= 2) {
1217 pan_pack_color(uint32_t *packed
, const union pipe_color_union
*color
, enum pipe_format format
)
1219 /* Alpha magicked to 1.0 if there is no alpha */
1221 bool has_alpha
= util_format_has_alpha(format
);
1222 float clear_alpha
= has_alpha
? color
->f
[3] : 1.0f
;
1224 /* Packed color depends on the framebuffer format */
1226 const struct util_format_description
*desc
=
1227 util_format_description(format
);
1229 if (util_format_is_rgba8_variant(desc
)) {
1230 pan_pack_color_32(packed
,
1231 ((uint32_t) float_to_ubyte(clear_alpha
) << 24) |
1232 ((uint32_t) float_to_ubyte(color
->f
[2]) << 16) |
1233 ((uint32_t) float_to_ubyte(color
->f
[1]) << 8) |
1234 ((uint32_t) float_to_ubyte(color
->f
[0]) << 0));
1235 } else if (format
== PIPE_FORMAT_B5G6R5_UNORM
) {
1236 /* First, we convert the components to R5, G6, B5 separately */
1237 unsigned r5
= _mesa_roundevenf(CLAMP(color
->f
[0], 0.0, 1.0) * 31.0);
1238 unsigned g6
= _mesa_roundevenf(CLAMP(color
->f
[1], 0.0, 1.0) * 63.0);
1239 unsigned b5
= _mesa_roundevenf(CLAMP(color
->f
[2], 0.0, 1.0) * 31.0);
1241 /* Then we pack into a sparse u32. TODO: Why these shifts? */
1242 pan_pack_color_32(packed
, (b5
<< 25) | (g6
<< 14) | (r5
<< 5));
1243 } else if (format
== PIPE_FORMAT_B4G4R4A4_UNORM
) {
1244 /* Convert to 4-bits */
1245 unsigned r4
= _mesa_roundevenf(CLAMP(color
->f
[0], 0.0, 1.0) * 15.0);
1246 unsigned g4
= _mesa_roundevenf(CLAMP(color
->f
[1], 0.0, 1.0) * 15.0);
1247 unsigned b4
= _mesa_roundevenf(CLAMP(color
->f
[2], 0.0, 1.0) * 15.0);
1248 unsigned a4
= _mesa_roundevenf(CLAMP(clear_alpha
, 0.0, 1.0) * 15.0);
1250 /* Pack on *byte* intervals */
1251 pan_pack_color_32(packed
, (a4
<< 28) | (b4
<< 20) | (g4
<< 12) | (r4
<< 4));
1252 } else if (format
== PIPE_FORMAT_B5G5R5A1_UNORM
) {
1253 /* Scale as expected but shift oddly */
1254 unsigned r5
= _mesa_roundevenf(CLAMP(color
->f
[0], 0.0, 1.0) * 31.0);
1255 unsigned g5
= _mesa_roundevenf(CLAMP(color
->f
[1], 0.0, 1.0) * 31.0);
1256 unsigned b5
= _mesa_roundevenf(CLAMP(color
->f
[2], 0.0, 1.0) * 31.0);
1257 unsigned a1
= _mesa_roundevenf(CLAMP(clear_alpha
, 0.0, 1.0) * 1.0);
1259 pan_pack_color_32(packed
, (a1
<< 31) | (b5
<< 25) | (g5
<< 15) | (r5
<< 5));
1261 /* Try Gallium's generic default path. Doesn't work for all
1262 * formats but it's a good guess. */
1264 union util_color out
;
1266 if (util_format_is_pure_integer(format
)) {
1267 memcpy(out
.ui
, color
->ui
, 16);
1269 util_pack_color(color
->f
, format
, &out
);
1272 unsigned size
= util_format_get_blocksize(format
);
1275 unsigned b
= out
.ui
[0];
1276 unsigned s
= b
| (b
<< 8);
1277 pan_pack_color_32(packed
, s
| (s
<< 16));
1278 } else if (size
== 2)
1279 pan_pack_color_32(packed
, out
.ui
[0] | (out
.ui
[0] << 16));
1280 else if (size
== 3 || size
== 4)
1281 pan_pack_color_32(packed
, out
.ui
[0]);
1283 pan_pack_color_64(packed
, out
.ui
[0], out
.ui
[1] | (out
.ui
[1] << 16)); /* RGB16F -- RGBB */
1285 pan_pack_color_64(packed
, out
.ui
[0], out
.ui
[1]);
1286 else if (size
== 16)
1287 memcpy(packed
, out
.ui
, 16);
1289 unreachable("Unknown generic format size packing clear colour");
1294 panfrost_batch_clear(struct panfrost_batch
*batch
,
1296 const union pipe_color_union
*color
,
1297 double depth
, unsigned stencil
)
1299 struct panfrost_context
*ctx
= batch
->ctx
;
1301 if (buffers
& PIPE_CLEAR_COLOR
) {
1302 for (unsigned i
= 0; i
< PIPE_MAX_COLOR_BUFS
; ++i
) {
1303 if (!(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
1306 enum pipe_format format
= ctx
->pipe_framebuffer
.cbufs
[i
]->format
;
1307 pan_pack_color(batch
->clear_color
[i
], color
, format
);
1311 if (buffers
& PIPE_CLEAR_DEPTH
) {
1312 batch
->clear_depth
= depth
;
1315 if (buffers
& PIPE_CLEAR_STENCIL
) {
1316 batch
->clear_stencil
= stencil
;
1319 batch
->clear
|= buffers
;
1321 /* Clearing affects the entire framebuffer (by definition -- this is
1322 * the Gallium clear callback, which clears the whole framebuffer. If
1323 * the scissor test were enabled from the GL side, the gallium frontend
1324 * would emit a quad instead and we wouldn't go down this code path) */
1326 panfrost_batch_union_scissor(batch
, 0, 0,
1327 ctx
->pipe_framebuffer
.width
,
1328 ctx
->pipe_framebuffer
.height
);
1332 panfrost_batch_compare(const void *a
, const void *b
)
1334 return util_framebuffer_state_equal(a
, b
);
1338 panfrost_batch_hash(const void *key
)
1340 return _mesa_hash_data(key
, sizeof(struct pipe_framebuffer_state
));
1343 /* Given a new bounding rectangle (scissor), let the job cover the union of the
1344 * new and old bounding rectangles */
1347 panfrost_batch_union_scissor(struct panfrost_batch
*batch
,
1348 unsigned minx
, unsigned miny
,
1349 unsigned maxx
, unsigned maxy
)
1351 batch
->minx
= MIN2(batch
->minx
, minx
);
1352 batch
->miny
= MIN2(batch
->miny
, miny
);
1353 batch
->maxx
= MAX2(batch
->maxx
, maxx
);
1354 batch
->maxy
= MAX2(batch
->maxy
, maxy
);
1358 panfrost_batch_intersection_scissor(struct panfrost_batch
*batch
,
1359 unsigned minx
, unsigned miny
,
1360 unsigned maxx
, unsigned maxy
)
1362 batch
->minx
= MAX2(batch
->minx
, minx
);
1363 batch
->miny
= MAX2(batch
->miny
, miny
);
1364 batch
->maxx
= MIN2(batch
->maxx
, maxx
);
1365 batch
->maxy
= MIN2(batch
->maxy
, maxy
);
1368 /* Are we currently rendering to the dev (rather than an FBO)? */
1371 panfrost_batch_is_scanout(struct panfrost_batch
*batch
)
1373 /* If there is no color buffer, it's an FBO */
1374 if (batch
->key
.nr_cbufs
!= 1)
1377 /* If we're too early that no framebuffer was sent, it's scanout */
1378 if (!batch
->key
.cbufs
[0])
1381 return batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_DISPLAY_TARGET
||
1382 batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_SCANOUT
||
1383 batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_SHARED
;
1387 panfrost_batch_init(struct panfrost_context
*ctx
)
1389 ctx
->batches
= _mesa_hash_table_create(ctx
,
1390 panfrost_batch_hash
,
1391 panfrost_batch_compare
);
1392 ctx
->accessed_bos
= _mesa_hash_table_create(ctx
, _mesa_hash_pointer
,
1393 _mesa_key_pointer_equal
);