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
);
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 assert(panfrost_batch_is_frozen(batch
));
174 hash_table_foreach(batch
->bos
, entry
)
175 panfrost_bo_unreference((struct panfrost_bo
*)entry
->key
);
177 util_dynarray_foreach(&batch
->dependencies
,
178 struct panfrost_batch_fence
*, dep
) {
179 panfrost_batch_fence_unreference(*dep
);
182 /* The out_sync fence lifetime is different from the the batch one
183 * since other batches might want to wait on a fence of already
184 * submitted/signaled batch. All we need to do here is make sure the
185 * fence does not point to an invalid batch, which the core will
186 * interpret as 'batch is already submitted'.
188 batch
->out_sync
->batch
= NULL
;
189 panfrost_batch_fence_unreference(batch
->out_sync
);
191 util_unreference_framebuffer_state(&batch
->key
);
197 panfrost_dep_graph_contains_batch(struct panfrost_batch
*root
,
198 struct panfrost_batch
*batch
)
203 util_dynarray_foreach(&root
->dependencies
,
204 struct panfrost_batch_fence
*, dep
) {
205 if ((*dep
)->batch
== batch
||
206 panfrost_dep_graph_contains_batch((*dep
)->batch
, batch
))
215 panfrost_batch_add_dep(struct panfrost_batch
*batch
,
216 struct panfrost_batch_fence
*newdep
)
218 if (batch
== newdep
->batch
)
221 /* We might want to turn ->dependencies into a set if the number of
222 * deps turns out to be big enough to make this 'is dep already there'
223 * search inefficient.
225 util_dynarray_foreach(&batch
->dependencies
,
226 struct panfrost_batch_fence
*, dep
) {
231 /* Make sure the dependency graph is acyclic. */
232 assert(!panfrost_dep_graph_contains_batch(newdep
->batch
, batch
));
234 panfrost_batch_fence_reference(newdep
);
235 util_dynarray_append(&batch
->dependencies
,
236 struct panfrost_batch_fence
*, newdep
);
238 /* We now have a batch depending on us, let's make sure new draw/clear
239 * calls targeting the same FBO use a new batch object.
242 panfrost_freeze_batch(newdep
->batch
);
245 static struct panfrost_batch
*
246 panfrost_get_batch(struct panfrost_context
*ctx
,
247 const struct pipe_framebuffer_state
*key
)
249 /* Lookup the job first */
250 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->batches
, key
);
255 /* Otherwise, let's create a job */
257 struct panfrost_batch
*batch
= panfrost_create_batch(ctx
, key
);
259 /* Save the created job */
260 _mesa_hash_table_insert(ctx
->batches
, &batch
->key
, batch
);
265 /* Get the job corresponding to the FBO we're currently rendering into */
267 struct panfrost_batch
*
268 panfrost_get_batch_for_fbo(struct panfrost_context
*ctx
)
270 /* If we're wallpapering, we special case to workaround
273 if (ctx
->wallpaper_batch
)
274 return ctx
->wallpaper_batch
;
276 /* If we already began rendering, use that */
279 assert(util_framebuffer_state_equal(&ctx
->batch
->key
,
280 &ctx
->pipe_framebuffer
));
284 /* If not, look up the job */
285 struct panfrost_batch
*batch
= panfrost_get_batch(ctx
,
286 &ctx
->pipe_framebuffer
);
288 /* Set this job as the current FBO job. Will be reset when updating the
289 * FB state and when submitting or releasing a job.
295 struct panfrost_batch
*
296 panfrost_get_fresh_batch_for_fbo(struct panfrost_context
*ctx
)
298 struct panfrost_batch
*batch
;
300 batch
= panfrost_get_batch(ctx
, &ctx
->pipe_framebuffer
);
302 /* The batch has no draw/clear queued, let's return it directly.
303 * Note that it's perfectly fine to re-use a batch with an
304 * existing clear, we'll just update it with the new clear request.
306 if (!batch
->first_job
)
309 /* Otherwise, we need to freeze the existing one and instantiate a new
312 panfrost_freeze_batch(batch
);
313 return panfrost_get_batch(ctx
, &ctx
->pipe_framebuffer
);
317 panfrost_batch_fence_is_signaled(struct panfrost_batch_fence
*fence
)
322 /* Batch has not been submitted yet. */
326 int ret
= drmSyncobjWait(pan_device(fence
->ctx
->base
.screen
)->fd
,
327 &fence
->syncobj
, 1, 0, 0, NULL
);
329 /* Cache whether the fence was signaled */
330 fence
->signaled
= ret
>= 0;
331 return fence
->signaled
;
335 panfrost_bo_access_gc_fences(struct panfrost_context
*ctx
,
336 struct panfrost_bo_access
*access
,
337 const struct panfrost_bo
*bo
)
339 if (access
->writer
&& panfrost_batch_fence_is_signaled(access
->writer
)) {
340 panfrost_batch_fence_unreference(access
->writer
);
341 access
->writer
= NULL
;
344 struct panfrost_batch_fence
**readers_array
= util_dynarray_begin(&access
->readers
);
345 struct panfrost_batch_fence
**new_readers
= readers_array
;
347 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
352 if (panfrost_batch_fence_is_signaled(*reader
)) {
353 panfrost_batch_fence_unreference(*reader
);
356 /* Build a new array of only unsignaled fences in-place */
357 *(new_readers
++) = *reader
;
361 if (!util_dynarray_resize(&access
->readers
, struct panfrost_batch_fence
*,
362 new_readers
- readers_array
) &&
363 new_readers
!= readers_array
)
364 unreachable("Invalid dynarray access->readers");
367 /* Collect signaled fences to keep the kernel-side syncobj-map small. The
368 * idea is to collect those signaled fences at the end of each flush_all
369 * call. This function is likely to collect only fences from previous
370 * batch flushes not the one that have just have just been submitted and
371 * are probably still in flight when we trigger the garbage collection.
372 * Anyway, we need to do this garbage collection at some point if we don't
373 * want the BO access map to keep invalid entries around and retain
377 panfrost_gc_fences(struct panfrost_context
*ctx
)
379 hash_table_foreach(ctx
->accessed_bos
, entry
) {
380 struct panfrost_bo_access
*access
= entry
->data
;
383 panfrost_bo_access_gc_fences(ctx
, access
, entry
->key
);
384 if (!util_dynarray_num_elements(&access
->readers
,
385 struct panfrost_batch_fence
*) &&
388 _mesa_hash_table_remove(ctx
->accessed_bos
, entry
);
395 panfrost_batch_in_readers(struct panfrost_batch
*batch
,
396 struct panfrost_bo_access
*access
)
398 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
400 if (*reader
&& (*reader
)->batch
== batch
)
409 panfrost_batch_update_bo_access(struct panfrost_batch
*batch
,
410 struct panfrost_bo
*bo
, uint32_t access_type
,
411 bool already_accessed
)
413 struct panfrost_context
*ctx
= batch
->ctx
;
414 struct panfrost_bo_access
*access
;
415 uint32_t old_access_type
;
416 struct hash_entry
*entry
;
418 assert(access_type
== PAN_BO_ACCESS_WRITE
||
419 access_type
== PAN_BO_ACCESS_READ
);
421 entry
= _mesa_hash_table_search(ctx
->accessed_bos
, bo
);
422 access
= entry
? entry
->data
: NULL
;
424 old_access_type
= access
->type
;
426 access
= rzalloc(ctx
, struct panfrost_bo_access
);
427 util_dynarray_init(&access
->readers
, access
);
428 _mesa_hash_table_insert(ctx
->accessed_bos
, bo
, access
);
429 /* We are the first to access this BO, let's initialize
430 * old_access_type to our own access type in that case.
432 old_access_type
= access_type
;
433 access
->type
= access_type
;
438 if (access_type
== PAN_BO_ACCESS_WRITE
&&
439 old_access_type
== PAN_BO_ACCESS_READ
) {
440 /* Previous access was a read and we want to write this BO.
441 * We first need to add explicit deps between our batch and
442 * the previous readers.
444 util_dynarray_foreach(&access
->readers
,
445 struct panfrost_batch_fence
*, reader
) {
446 /* We were already reading the BO, no need to add a dep
447 * on ourself (the acyclic check would complain about
450 if (!(*reader
) || (*reader
)->batch
== batch
)
453 panfrost_batch_add_dep(batch
, *reader
);
455 panfrost_batch_fence_reference(batch
->out_sync
);
457 /* We now are the new writer. */
458 access
->writer
= batch
->out_sync
;
459 access
->type
= access_type
;
461 /* Release the previous readers and reset the readers array. */
462 util_dynarray_foreach(&access
->readers
,
463 struct panfrost_batch_fence
*,
467 panfrost_batch_fence_unreference(*reader
);
470 util_dynarray_clear(&access
->readers
);
471 } else if (access_type
== PAN_BO_ACCESS_WRITE
&&
472 old_access_type
== PAN_BO_ACCESS_WRITE
) {
473 /* Previous access was a write and we want to write this BO.
474 * First check if we were the previous writer, in that case
475 * there's nothing to do. Otherwise we need to add a
476 * dependency between the new writer and the old one.
478 if (access
->writer
!= batch
->out_sync
) {
479 if (access
->writer
) {
480 panfrost_batch_add_dep(batch
, access
->writer
);
481 panfrost_batch_fence_unreference(access
->writer
);
483 panfrost_batch_fence_reference(batch
->out_sync
);
484 access
->writer
= batch
->out_sync
;
486 } else if (access_type
== PAN_BO_ACCESS_READ
&&
487 old_access_type
== PAN_BO_ACCESS_WRITE
) {
488 /* Previous access was a write and we want to read this BO.
489 * First check if we were the previous writer, in that case
490 * we want to keep the access type unchanged, as a write is
491 * more constraining than a read.
493 if (access
->writer
!= batch
->out_sync
) {
494 /* Add a dependency on the previous writer. */
495 panfrost_batch_add_dep(batch
, access
->writer
);
497 /* The previous access was a write, there's no reason
498 * to have entries in the readers array.
500 assert(!util_dynarray_num_elements(&access
->readers
,
501 struct panfrost_batch_fence
*));
503 /* Add ourselves to the readers array. */
504 panfrost_batch_fence_reference(batch
->out_sync
);
505 util_dynarray_append(&access
->readers
,
506 struct panfrost_batch_fence
*,
508 access
->type
= PAN_BO_ACCESS_READ
;
511 /* We already accessed this BO before, so we should already be
512 * in the reader array.
514 if (already_accessed
) {
515 assert(panfrost_batch_in_readers(batch
, access
));
519 /* Previous access was a read and we want to read this BO.
520 * Add ourselves to the readers array and add a dependency on
521 * the previous writer if any.
523 panfrost_batch_fence_reference(batch
->out_sync
);
524 util_dynarray_append(&access
->readers
,
525 struct panfrost_batch_fence
*,
529 panfrost_batch_add_dep(batch
, access
->writer
);
534 panfrost_batch_add_bo(struct panfrost_batch
*batch
, struct panfrost_bo
*bo
,
540 struct hash_entry
*entry
;
541 uint32_t old_flags
= 0;
543 entry
= _mesa_hash_table_search(batch
->bos
, bo
);
545 entry
= _mesa_hash_table_insert(batch
->bos
, bo
,
546 (void *)(uintptr_t)flags
);
547 panfrost_bo_reference(bo
);
549 old_flags
= (uintptr_t)entry
->data
;
551 /* All batches have to agree on the shared flag. */
552 assert((old_flags
& PAN_BO_ACCESS_SHARED
) ==
553 (flags
& PAN_BO_ACCESS_SHARED
));
558 if (old_flags
== flags
)
562 entry
->data
= (void *)(uintptr_t)flags
;
564 /* If this is not a shared BO, we don't really care about dependency
567 if (!(flags
& PAN_BO_ACCESS_SHARED
))
570 /* All dependencies should have been flushed before we execute the
571 * wallpaper draw, so it should be harmless to skip the
572 * update_bo_access() call.
574 if (batch
== batch
->ctx
->wallpaper_batch
)
577 /* Only pass R/W flags to the dep tracking logic. */
578 assert(flags
& PAN_BO_ACCESS_RW
);
579 flags
= (flags
& PAN_BO_ACCESS_WRITE
) ?
580 PAN_BO_ACCESS_WRITE
: PAN_BO_ACCESS_READ
;
581 panfrost_batch_update_bo_access(batch
, bo
, flags
, old_flags
!= 0);
585 panfrost_batch_add_resource_bos(struct panfrost_batch
*batch
,
586 struct panfrost_resource
*rsrc
,
589 panfrost_batch_add_bo(batch
, rsrc
->bo
, flags
);
591 for (unsigned i
= 0; i
< MAX_MIP_LEVELS
; i
++)
592 if (rsrc
->slices
[i
].checksum_bo
)
593 panfrost_batch_add_bo(batch
, rsrc
->slices
[i
].checksum_bo
, flags
);
596 void panfrost_batch_add_fbo_bos(struct panfrost_batch
*batch
)
598 uint32_t flags
= PAN_BO_ACCESS_SHARED
| PAN_BO_ACCESS_WRITE
|
599 PAN_BO_ACCESS_VERTEX_TILER
|
600 PAN_BO_ACCESS_FRAGMENT
;
602 for (unsigned i
= 0; i
< batch
->key
.nr_cbufs
; ++i
) {
603 struct panfrost_resource
*rsrc
= pan_resource(batch
->key
.cbufs
[i
]->texture
);
604 panfrost_batch_add_resource_bos(batch
, rsrc
, flags
);
607 if (batch
->key
.zsbuf
) {
608 struct panfrost_resource
*rsrc
= pan_resource(batch
->key
.zsbuf
->texture
);
609 panfrost_batch_add_resource_bos(batch
, rsrc
, flags
);
614 panfrost_batch_create_bo(struct panfrost_batch
*batch
, size_t size
,
615 uint32_t create_flags
, uint32_t access_flags
)
617 struct panfrost_bo
*bo
;
619 bo
= pan_bo_create(pan_device(batch
->ctx
->base
.screen
), size
,
621 panfrost_batch_add_bo(batch
, bo
, access_flags
);
623 /* panfrost_batch_add_bo() has retained a reference and
624 * pan_bo_create() initialize the refcnt to 1, so let's
625 * unreference the BO here so it gets released when the batch is
626 * destroyed (unless it's retained by someone else in the meantime).
628 panfrost_bo_unreference(bo
);
632 /* Returns the polygon list's GPU address if available, or otherwise allocates
633 * the polygon list. It's perfectly fast to use allocate/free BO directly,
634 * since we'll hit the BO cache and this is one-per-batch anyway. */
637 panfrost_batch_get_polygon_list(struct panfrost_batch
*batch
, unsigned size
)
639 if (batch
->polygon_list
) {
640 assert(batch
->polygon_list
->size
>= size
);
642 /* Create the BO as invisible, as there's no reason to map */
643 size
= util_next_power_of_two(size
);
645 batch
->polygon_list
= panfrost_batch_create_bo(batch
, size
,
647 PAN_BO_ACCESS_PRIVATE
|
649 PAN_BO_ACCESS_VERTEX_TILER
|
650 PAN_BO_ACCESS_FRAGMENT
);
653 return batch
->polygon_list
->gpu
;
657 panfrost_batch_get_scratchpad(struct panfrost_batch
*batch
,
659 unsigned thread_tls_alloc
,
662 unsigned size
= panfrost_get_total_stack_size(shift
,
666 if (batch
->scratchpad
) {
667 assert(batch
->scratchpad
->size
>= size
);
669 batch
->scratchpad
= panfrost_batch_create_bo(batch
, size
,
671 PAN_BO_ACCESS_PRIVATE
|
673 PAN_BO_ACCESS_VERTEX_TILER
|
674 PAN_BO_ACCESS_FRAGMENT
);
677 return batch
->scratchpad
;
681 panfrost_batch_get_shared_memory(struct panfrost_batch
*batch
,
683 unsigned workgroup_count
)
685 if (batch
->shared_memory
) {
686 assert(batch
->shared_memory
->size
>= size
);
688 batch
->shared_memory
= panfrost_batch_create_bo(batch
, size
,
690 PAN_BO_ACCESS_PRIVATE
|
692 PAN_BO_ACCESS_VERTEX_TILER
);
695 return batch
->shared_memory
;
699 panfrost_batch_get_tiler_heap(struct panfrost_batch
*batch
)
701 if (batch
->tiler_heap
)
702 return batch
->tiler_heap
;
704 batch
->tiler_heap
= panfrost_batch_create_bo(batch
, 4096 * 4096,
707 PAN_BO_ACCESS_PRIVATE
|
709 PAN_BO_ACCESS_VERTEX_TILER
|
710 PAN_BO_ACCESS_FRAGMENT
);
711 assert(batch
->tiler_heap
);
712 return batch
->tiler_heap
;
716 panfrost_batch_get_tiler_meta(struct panfrost_batch
*batch
, unsigned vertex_count
)
721 if (batch
->tiler_meta
)
722 return batch
->tiler_meta
;
724 struct panfrost_bo
*tiler_heap
;
725 tiler_heap
= panfrost_batch_get_tiler_heap(batch
);
727 struct bifrost_tiler_heap_meta tiler_heap_meta
= {
728 .heap_size
= tiler_heap
->size
,
729 .tiler_heap_start
= tiler_heap
->gpu
,
730 .tiler_heap_free
= tiler_heap
->gpu
,
731 .tiler_heap_end
= tiler_heap
->gpu
+ tiler_heap
->size
,
733 .unk7e007e
= 0x7e007e,
736 struct bifrost_tiler_meta tiler_meta
= {
737 .hierarchy_mask
= 0x28,
739 .width
= MALI_POSITIVE(batch
->key
.width
),
740 .height
= MALI_POSITIVE(batch
->key
.height
),
741 .tiler_heap_meta
= panfrost_upload_transient(batch
, &tiler_heap_meta
, sizeof(tiler_heap_meta
)),
744 batch
->tiler_meta
= panfrost_upload_transient(batch
, &tiler_meta
, sizeof(tiler_meta
));
745 return batch
->tiler_meta
;
749 panfrost_batch_get_tiler_dummy(struct panfrost_batch
*batch
)
751 struct panfrost_device
*dev
= pan_device(batch
->ctx
->base
.screen
);
753 uint32_t create_flags
= 0;
755 if (batch
->tiler_dummy
)
756 return batch
->tiler_dummy
;
758 if (!(dev
->quirks
& MIDGARD_NO_HIER_TILING
))
759 create_flags
= PAN_BO_INVISIBLE
;
761 batch
->tiler_dummy
= panfrost_batch_create_bo(batch
, 4096,
763 PAN_BO_ACCESS_PRIVATE
|
765 PAN_BO_ACCESS_VERTEX_TILER
|
766 PAN_BO_ACCESS_FRAGMENT
);
767 assert(batch
->tiler_dummy
);
768 return batch
->tiler_dummy
;
772 panfrost_batch_draw_wallpaper(struct panfrost_batch
*batch
)
774 /* Color 0 is cleared, no need to draw the wallpaper.
775 * TODO: MRT wallpapers.
777 if (batch
->clear
& PIPE_CLEAR_COLOR0
)
780 /* Nothing to reload? TODO: MRT wallpapers */
781 if (batch
->key
.cbufs
[0] == NULL
)
784 /* No draw calls, and no clear on the depth/stencil bufs.
785 * Drawing the wallpaper would be useless.
787 if (!batch
->tiler_dep
&&
788 !(batch
->clear
& PIPE_CLEAR_DEPTHSTENCIL
))
791 /* Check if the buffer has any content on it worth preserving */
793 struct pipe_surface
*surf
= batch
->key
.cbufs
[0];
794 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
795 unsigned level
= surf
->u
.tex
.level
;
797 if (!rsrc
->slices
[level
].initialized
)
800 batch
->ctx
->wallpaper_batch
= batch
;
802 /* Clamp the rendering area to the damage extent. The
803 * KHR_partial_update() spec states that trying to render outside of
804 * the damage region is "undefined behavior", so we should be safe.
806 unsigned damage_width
= (rsrc
->damage
.extent
.maxx
- rsrc
->damage
.extent
.minx
);
807 unsigned damage_height
= (rsrc
->damage
.extent
.maxy
- rsrc
->damage
.extent
.miny
);
809 if (damage_width
&& damage_height
) {
810 panfrost_batch_intersection_scissor(batch
,
811 rsrc
->damage
.extent
.minx
,
812 rsrc
->damage
.extent
.miny
,
813 rsrc
->damage
.extent
.maxx
,
814 rsrc
->damage
.extent
.maxy
);
817 /* FIXME: Looks like aligning on a tile is not enough, but
818 * aligning on twice the tile size seems to works. We don't
819 * know exactly what happens here but this deserves extra
820 * investigation to figure it out.
822 batch
->minx
= batch
->minx
& ~((MALI_TILE_LENGTH
* 2) - 1);
823 batch
->miny
= batch
->miny
& ~((MALI_TILE_LENGTH
* 2) - 1);
824 batch
->maxx
= MIN2(ALIGN_POT(batch
->maxx
, MALI_TILE_LENGTH
* 2),
826 batch
->maxy
= MIN2(ALIGN_POT(batch
->maxy
, MALI_TILE_LENGTH
* 2),
829 struct pipe_scissor_state damage
;
830 struct pipe_box rects
[4];
832 /* Clamp the damage box to the rendering area. */
833 damage
.minx
= MAX2(batch
->minx
, rsrc
->damage
.biggest_rect
.x
);
834 damage
.miny
= MAX2(batch
->miny
, rsrc
->damage
.biggest_rect
.y
);
835 damage
.maxx
= MIN2(batch
->maxx
,
836 rsrc
->damage
.biggest_rect
.x
+
837 rsrc
->damage
.biggest_rect
.width
);
838 damage
.maxx
= MAX2(damage
.maxx
, damage
.minx
);
839 damage
.maxy
= MIN2(batch
->maxy
,
840 rsrc
->damage
.biggest_rect
.y
+
841 rsrc
->damage
.biggest_rect
.height
);
842 damage
.maxy
= MAX2(damage
.maxy
, damage
.miny
);
844 /* One damage rectangle means we can end up with at most 4 reload
846 * 1: left region, only exists if damage.x > 0
847 * 2: right region, only exists if damage.x + damage.width < fb->width
848 * 3: top region, only exists if damage.y > 0. The intersection with
849 * the left and right regions are dropped
850 * 4: bottom region, only exists if damage.y + damage.height < fb->height.
851 * The intersection with the left and right regions are dropped
853 * ____________________________
860 * |_______|___________|______|
862 u_box_2d(batch
->minx
, batch
->miny
, damage
.minx
- batch
->minx
,
863 batch
->maxy
- batch
->miny
, &rects
[0]);
864 u_box_2d(damage
.maxx
, batch
->miny
, batch
->maxx
- damage
.maxx
,
865 batch
->maxy
- batch
->miny
, &rects
[1]);
866 u_box_2d(damage
.minx
, batch
->miny
, damage
.maxx
- damage
.minx
,
867 damage
.miny
- batch
->miny
, &rects
[2]);
868 u_box_2d(damage
.minx
, damage
.maxy
, damage
.maxx
- damage
.minx
,
869 batch
->maxy
- damage
.maxy
, &rects
[3]);
871 for (unsigned i
= 0; i
< 4; i
++) {
872 /* Width and height are always >= 0 even if width is declared as a
873 * signed integer: u_box_2d() helper takes unsigned args and
874 * panfrost_set_damage_region() is taking care of clamping
877 if (!rects
[i
].width
|| !rects
[i
].height
)
880 /* Blit the wallpaper in */
881 panfrost_blit_wallpaper(batch
->ctx
, &rects
[i
]);
883 batch
->ctx
->wallpaper_batch
= NULL
;
887 panfrost_batch_submit_ioctl(struct panfrost_batch
*batch
,
888 mali_ptr first_job_desc
,
891 struct panfrost_context
*ctx
= batch
->ctx
;
892 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
893 struct panfrost_device
*dev
= pan_device(gallium
->screen
);
894 struct drm_panfrost_submit submit
= {0,};
895 uint32_t *bo_handles
, *in_syncs
= NULL
;
896 bool is_fragment_shader
;
899 is_fragment_shader
= (reqs
& PANFROST_JD_REQ_FS
) && batch
->first_job
;
900 if (is_fragment_shader
)
901 submit
.in_sync_count
= 1;
903 submit
.in_sync_count
= util_dynarray_num_elements(&batch
->dependencies
,
904 struct panfrost_batch_fence
*);
906 if (submit
.in_sync_count
) {
907 in_syncs
= calloc(submit
.in_sync_count
, sizeof(*in_syncs
));
911 /* The fragment job always depends on the vertex/tiler job if there's
914 if (is_fragment_shader
) {
915 in_syncs
[0] = batch
->out_sync
->syncobj
;
919 util_dynarray_foreach(&batch
->dependencies
,
920 struct panfrost_batch_fence
*, dep
)
921 in_syncs
[i
++] = (*dep
)->syncobj
;
924 submit
.in_syncs
= (uintptr_t)in_syncs
;
925 submit
.out_sync
= batch
->out_sync
->syncobj
;
926 submit
.jc
= first_job_desc
;
927 submit
.requirements
= reqs
;
929 bo_handles
= calloc(batch
->bos
->entries
, sizeof(*bo_handles
));
932 hash_table_foreach(batch
->bos
, entry
) {
933 struct panfrost_bo
*bo
= (struct panfrost_bo
*)entry
->key
;
934 uint32_t flags
= (uintptr_t)entry
->data
;
936 assert(bo
->gem_handle
> 0);
937 bo_handles
[submit
.bo_handle_count
++] = bo
->gem_handle
;
939 /* Update the BO access flags so that panfrost_bo_wait() knows
940 * about all pending accesses.
941 * We only keep the READ/WRITE info since this is all the BO
942 * wait logic cares about.
943 * We also preserve existing flags as this batch might not
944 * be the first one to access the BO.
946 bo
->gpu_access
|= flags
& (PAN_BO_ACCESS_RW
);
949 submit
.bo_handles
= (u64
) (uintptr_t) bo_handles
;
950 ret
= drmIoctl(dev
->fd
, DRM_IOCTL_PANFROST_SUBMIT
, &submit
);
955 DBG("Error submitting: %m\n");
959 /* Trace the job if we're doing that */
960 if (pan_debug
& (PAN_DBG_TRACE
| PAN_DBG_SYNC
)) {
961 /* Wait so we can get errors reported back */
962 drmSyncobjWait(dev
->fd
, &batch
->out_sync
->syncobj
, 1,
965 /* Trace gets priority over sync */
966 bool minimal
= !(pan_debug
& PAN_DBG_TRACE
);
967 pandecode_jc(submit
.jc
, dev
->quirks
& IS_BIFROST
, dev
->gpu_id
, minimal
);
974 panfrost_batch_submit_jobs(struct panfrost_batch
*batch
)
976 bool has_draws
= batch
->first_job
;
980 ret
= panfrost_batch_submit_ioctl(batch
, batch
->first_job
, 0);
984 if (batch
->tiler_dep
|| batch
->clear
) {
985 mali_ptr fragjob
= panfrost_fragment_job(batch
, has_draws
);
986 ret
= panfrost_batch_submit_ioctl(batch
, fragjob
, PANFROST_JD_REQ_FS
);
994 panfrost_batch_submit(struct panfrost_batch
*batch
)
998 /* Submit the dependencies first. */
999 util_dynarray_foreach(&batch
->dependencies
,
1000 struct panfrost_batch_fence
*, dep
) {
1002 panfrost_batch_submit((*dep
)->batch
);
1007 /* Nothing to do! */
1008 if (!batch
->first_job
&& !batch
->clear
) {
1009 /* Mark the fence as signaled so the fence logic does not try
1012 batch
->out_sync
->signaled
= true;
1016 panfrost_batch_draw_wallpaper(batch
);
1018 /* Now that all draws are in, we can finally prepare the
1019 * FBD for the batch */
1021 if (batch
->framebuffer
.gpu
&& batch
->first_job
) {
1022 struct panfrost_context
*ctx
= batch
->ctx
;
1023 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
1024 struct panfrost_device
*dev
= pan_device(gallium
->screen
);
1026 if (dev
->quirks
& MIDGARD_SFBD
)
1027 panfrost_attach_sfbd(batch
, ~0);
1029 panfrost_attach_mfbd(batch
, ~0);
1032 panfrost_scoreboard_initialize_tiler(batch
);
1034 ret
= panfrost_batch_submit_jobs(batch
);
1037 DBG("panfrost_batch_submit failed: %d\n", ret
);
1039 /* We must reset the damage info of our render targets here even
1040 * though a damage reset normally happens when the DRI layer swaps
1041 * buffers. That's because there can be implicit flushes the GL
1042 * app is not aware of, and those might impact the damage region: if
1043 * part of the damaged portion is drawn during those implicit flushes,
1044 * you have to reload those areas before next draws are pushed, and
1045 * since the driver can't easily know what's been modified by the draws
1046 * it flushed, the easiest solution is to reload everything.
1048 for (unsigned i
= 0; i
< batch
->key
.nr_cbufs
; i
++) {
1049 struct panfrost_resource
*res
;
1051 if (!batch
->key
.cbufs
[i
])
1054 res
= pan_resource(batch
->key
.cbufs
[i
]->texture
);
1055 panfrost_resource_reset_damage(res
);
1059 panfrost_freeze_batch(batch
);
1060 panfrost_free_batch(batch
);
1064 panfrost_flush_all_batches(struct panfrost_context
*ctx
, bool wait
)
1066 struct util_dynarray fences
, syncobjs
;
1069 util_dynarray_init(&fences
, NULL
);
1070 util_dynarray_init(&syncobjs
, NULL
);
1073 hash_table_foreach(ctx
->batches
, hentry
) {
1074 struct panfrost_batch
*batch
= hentry
->data
;
1079 panfrost_batch_fence_reference(batch
->out_sync
);
1080 util_dynarray_append(&fences
, struct panfrost_batch_fence
*,
1082 util_dynarray_append(&syncobjs
, uint32_t,
1083 batch
->out_sync
->syncobj
);
1086 panfrost_batch_submit(batch
);
1089 assert(!ctx
->batches
->entries
);
1091 /* Collect batch fences before returning */
1092 panfrost_gc_fences(ctx
);
1097 drmSyncobjWait(pan_device(ctx
->base
.screen
)->fd
,
1098 util_dynarray_begin(&syncobjs
),
1099 util_dynarray_num_elements(&syncobjs
, uint32_t),
1100 INT64_MAX
, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL
, NULL
);
1102 util_dynarray_foreach(&fences
, struct panfrost_batch_fence
*, fence
)
1103 panfrost_batch_fence_unreference(*fence
);
1105 util_dynarray_fini(&fences
);
1106 util_dynarray_fini(&syncobjs
);
1110 panfrost_pending_batches_access_bo(struct panfrost_context
*ctx
,
1111 const struct panfrost_bo
*bo
)
1113 struct panfrost_bo_access
*access
;
1114 struct hash_entry
*hentry
;
1116 hentry
= _mesa_hash_table_search(ctx
->accessed_bos
, bo
);
1117 access
= hentry
? hentry
->data
: NULL
;
1121 if (access
->writer
&& access
->writer
->batch
)
1124 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
1126 if (*reader
&& (*reader
)->batch
)
1134 panfrost_flush_batches_accessing_bo(struct panfrost_context
*ctx
,
1135 struct panfrost_bo
*bo
,
1136 uint32_t access_type
)
1138 struct panfrost_bo_access
*access
;
1139 struct hash_entry
*hentry
;
1141 /* It doesn't make any to flush only the readers. */
1142 assert(access_type
== PAN_BO_ACCESS_WRITE
||
1143 access_type
== PAN_BO_ACCESS_RW
);
1145 hentry
= _mesa_hash_table_search(ctx
->accessed_bos
, bo
);
1146 access
= hentry
? hentry
->data
: NULL
;
1150 if (access_type
& PAN_BO_ACCESS_WRITE
&& access
->writer
&&
1151 access
->writer
->batch
)
1152 panfrost_batch_submit(access
->writer
->batch
);
1154 if (!(access_type
& PAN_BO_ACCESS_READ
))
1157 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
1159 if (*reader
&& (*reader
)->batch
)
1160 panfrost_batch_submit((*reader
)->batch
);
1165 panfrost_batch_set_requirements(struct panfrost_batch
*batch
)
1167 struct panfrost_context
*ctx
= batch
->ctx
;
1169 if (ctx
->rasterizer
&& ctx
->rasterizer
->base
.multisample
)
1170 batch
->requirements
|= PAN_REQ_MSAA
;
1172 if (ctx
->depth_stencil
&& ctx
->depth_stencil
->depth
.writemask
)
1173 batch
->requirements
|= PAN_REQ_DEPTH_WRITE
;
1177 panfrost_batch_adjust_stack_size(struct panfrost_batch
*batch
)
1179 struct panfrost_context
*ctx
= batch
->ctx
;
1181 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; ++i
) {
1182 struct panfrost_shader_state
*ss
;
1184 ss
= panfrost_get_shader_state(ctx
, i
);
1188 batch
->stack_size
= MAX2(batch
->stack_size
, ss
->stack_size
);
1192 /* Helper to smear a 32-bit color across 128-bit components */
1195 pan_pack_color_32(uint32_t *packed
, uint32_t v
)
1197 for (unsigned i
= 0; i
< 4; ++i
)
1202 pan_pack_color_64(uint32_t *packed
, uint32_t lo
, uint32_t hi
)
1204 for (unsigned i
= 0; i
< 4; i
+= 2) {
1211 pan_pack_color(uint32_t *packed
, const union pipe_color_union
*color
, enum pipe_format format
)
1213 /* Alpha magicked to 1.0 if there is no alpha */
1215 bool has_alpha
= util_format_has_alpha(format
);
1216 float clear_alpha
= has_alpha
? color
->f
[3] : 1.0f
;
1218 /* Packed color depends on the framebuffer format */
1220 const struct util_format_description
*desc
=
1221 util_format_description(format
);
1223 if (util_format_is_rgba8_variant(desc
)) {
1224 pan_pack_color_32(packed
,
1225 ((uint32_t) float_to_ubyte(clear_alpha
) << 24) |
1226 ((uint32_t) float_to_ubyte(color
->f
[2]) << 16) |
1227 ((uint32_t) float_to_ubyte(color
->f
[1]) << 8) |
1228 ((uint32_t) float_to_ubyte(color
->f
[0]) << 0));
1229 } else if (format
== PIPE_FORMAT_B5G6R5_UNORM
) {
1230 /* First, we convert the components to R5, G6, B5 separately */
1231 unsigned r5
= _mesa_roundevenf(CLAMP(color
->f
[0], 0.0, 1.0) * 31.0);
1232 unsigned g6
= _mesa_roundevenf(CLAMP(color
->f
[1], 0.0, 1.0) * 63.0);
1233 unsigned b5
= _mesa_roundevenf(CLAMP(color
->f
[2], 0.0, 1.0) * 31.0);
1235 /* Then we pack into a sparse u32. TODO: Why these shifts? */
1236 pan_pack_color_32(packed
, (b5
<< 25) | (g6
<< 14) | (r5
<< 5));
1237 } else if (format
== PIPE_FORMAT_B4G4R4A4_UNORM
) {
1238 /* Convert to 4-bits */
1239 unsigned r4
= _mesa_roundevenf(CLAMP(color
->f
[0], 0.0, 1.0) * 15.0);
1240 unsigned g4
= _mesa_roundevenf(CLAMP(color
->f
[1], 0.0, 1.0) * 15.0);
1241 unsigned b4
= _mesa_roundevenf(CLAMP(color
->f
[2], 0.0, 1.0) * 15.0);
1242 unsigned a4
= _mesa_roundevenf(CLAMP(clear_alpha
, 0.0, 1.0) * 15.0);
1244 /* Pack on *byte* intervals */
1245 pan_pack_color_32(packed
, (a4
<< 28) | (b4
<< 20) | (g4
<< 12) | (r4
<< 4));
1246 } else if (format
== PIPE_FORMAT_B5G5R5A1_UNORM
) {
1247 /* Scale as expected but shift oddly */
1248 unsigned r5
= _mesa_roundevenf(CLAMP(color
->f
[0], 0.0, 1.0) * 31.0);
1249 unsigned g5
= _mesa_roundevenf(CLAMP(color
->f
[1], 0.0, 1.0) * 31.0);
1250 unsigned b5
= _mesa_roundevenf(CLAMP(color
->f
[2], 0.0, 1.0) * 31.0);
1251 unsigned a1
= _mesa_roundevenf(CLAMP(clear_alpha
, 0.0, 1.0) * 1.0);
1253 pan_pack_color_32(packed
, (a1
<< 31) | (b5
<< 25) | (g5
<< 15) | (r5
<< 5));
1255 /* Try Gallium's generic default path. Doesn't work for all
1256 * formats but it's a good guess. */
1258 union util_color out
;
1260 if (util_format_is_pure_integer(format
)) {
1261 memcpy(out
.ui
, color
->ui
, 16);
1263 util_pack_color(color
->f
, format
, &out
);
1266 unsigned size
= util_format_get_blocksize(format
);
1269 unsigned b
= out
.ui
[0];
1270 unsigned s
= b
| (b
<< 8);
1271 pan_pack_color_32(packed
, s
| (s
<< 16));
1272 } else if (size
== 2)
1273 pan_pack_color_32(packed
, out
.ui
[0] | (out
.ui
[0] << 16));
1274 else if (size
== 3 || size
== 4)
1275 pan_pack_color_32(packed
, out
.ui
[0]);
1277 pan_pack_color_64(packed
, out
.ui
[0], out
.ui
[1] | (out
.ui
[1] << 16)); /* RGB16F -- RGBB */
1279 pan_pack_color_64(packed
, out
.ui
[0], out
.ui
[1]);
1280 else if (size
== 16)
1281 memcpy(packed
, out
.ui
, 16);
1283 unreachable("Unknown generic format size packing clear colour");
1288 panfrost_batch_clear(struct panfrost_batch
*batch
,
1290 const union pipe_color_union
*color
,
1291 double depth
, unsigned stencil
)
1293 struct panfrost_context
*ctx
= batch
->ctx
;
1295 if (buffers
& PIPE_CLEAR_COLOR
) {
1296 for (unsigned i
= 0; i
< PIPE_MAX_COLOR_BUFS
; ++i
) {
1297 if (!(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
1300 enum pipe_format format
= ctx
->pipe_framebuffer
.cbufs
[i
]->format
;
1301 pan_pack_color(batch
->clear_color
[i
], color
, format
);
1305 if (buffers
& PIPE_CLEAR_DEPTH
) {
1306 batch
->clear_depth
= depth
;
1309 if (buffers
& PIPE_CLEAR_STENCIL
) {
1310 batch
->clear_stencil
= stencil
;
1313 batch
->clear
|= buffers
;
1315 /* Clearing affects the entire framebuffer (by definition -- this is
1316 * the Gallium clear callback, which clears the whole framebuffer. If
1317 * the scissor test were enabled from the GL side, the gallium frontend
1318 * would emit a quad instead and we wouldn't go down this code path) */
1320 panfrost_batch_union_scissor(batch
, 0, 0,
1321 ctx
->pipe_framebuffer
.width
,
1322 ctx
->pipe_framebuffer
.height
);
1326 panfrost_batch_compare(const void *a
, const void *b
)
1328 return util_framebuffer_state_equal(a
, b
);
1332 panfrost_batch_hash(const void *key
)
1334 return _mesa_hash_data(key
, sizeof(struct pipe_framebuffer_state
));
1337 /* Given a new bounding rectangle (scissor), let the job cover the union of the
1338 * new and old bounding rectangles */
1341 panfrost_batch_union_scissor(struct panfrost_batch
*batch
,
1342 unsigned minx
, unsigned miny
,
1343 unsigned maxx
, unsigned maxy
)
1345 batch
->minx
= MIN2(batch
->minx
, minx
);
1346 batch
->miny
= MIN2(batch
->miny
, miny
);
1347 batch
->maxx
= MAX2(batch
->maxx
, maxx
);
1348 batch
->maxy
= MAX2(batch
->maxy
, maxy
);
1352 panfrost_batch_intersection_scissor(struct panfrost_batch
*batch
,
1353 unsigned minx
, unsigned miny
,
1354 unsigned maxx
, unsigned maxy
)
1356 batch
->minx
= MAX2(batch
->minx
, minx
);
1357 batch
->miny
= MAX2(batch
->miny
, miny
);
1358 batch
->maxx
= MIN2(batch
->maxx
, maxx
);
1359 batch
->maxy
= MIN2(batch
->maxy
, maxy
);
1362 /* Are we currently rendering to the dev (rather than an FBO)? */
1365 panfrost_batch_is_scanout(struct panfrost_batch
*batch
)
1367 /* If there is no color buffer, it's an FBO */
1368 if (batch
->key
.nr_cbufs
!= 1)
1371 /* If we're too early that no framebuffer was sent, it's scanout */
1372 if (!batch
->key
.cbufs
[0])
1375 return batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_DISPLAY_TARGET
||
1376 batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_SCANOUT
||
1377 batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_SHARED
;
1381 panfrost_batch_init(struct panfrost_context
*ctx
)
1383 ctx
->batches
= _mesa_hash_table_create(ctx
,
1384 panfrost_batch_hash
,
1385 panfrost_batch_compare
);
1386 ctx
->accessed_bos
= _mesa_hash_table_create(ctx
, _mesa_hash_pointer
,
1387 _mesa_key_pointer_equal
);