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
);
601 if (rsrc
->separate_stencil
)
602 panfrost_batch_add_bo(batch
, rsrc
->separate_stencil
->bo
, flags
);
605 void panfrost_batch_add_fbo_bos(struct panfrost_batch
*batch
)
607 uint32_t flags
= PAN_BO_ACCESS_SHARED
| PAN_BO_ACCESS_WRITE
|
608 PAN_BO_ACCESS_VERTEX_TILER
|
609 PAN_BO_ACCESS_FRAGMENT
;
611 for (unsigned i
= 0; i
< batch
->key
.nr_cbufs
; ++i
) {
612 struct panfrost_resource
*rsrc
= pan_resource(batch
->key
.cbufs
[i
]->texture
);
613 panfrost_batch_add_resource_bos(batch
, rsrc
, flags
);
616 if (batch
->key
.zsbuf
) {
617 struct panfrost_resource
*rsrc
= pan_resource(batch
->key
.zsbuf
->texture
);
618 panfrost_batch_add_resource_bos(batch
, rsrc
, flags
);
623 panfrost_batch_create_bo(struct panfrost_batch
*batch
, size_t size
,
624 uint32_t create_flags
, uint32_t access_flags
)
626 struct panfrost_bo
*bo
;
628 bo
= pan_bo_create(pan_device(batch
->ctx
->base
.screen
), size
,
630 panfrost_batch_add_bo(batch
, bo
, access_flags
);
632 /* panfrost_batch_add_bo() has retained a reference and
633 * pan_bo_create() initialize the refcnt to 1, so let's
634 * unreference the BO here so it gets released when the batch is
635 * destroyed (unless it's retained by someone else in the meantime).
637 panfrost_bo_unreference(bo
);
641 /* Returns the polygon list's GPU address if available, or otherwise allocates
642 * the polygon list. It's perfectly fast to use allocate/free BO directly,
643 * since we'll hit the BO cache and this is one-per-batch anyway. */
646 panfrost_batch_get_polygon_list(struct panfrost_batch
*batch
, unsigned size
)
648 if (batch
->polygon_list
) {
649 assert(batch
->polygon_list
->size
>= size
);
651 /* Create the BO as invisible, as there's no reason to map */
652 size
= util_next_power_of_two(size
);
654 batch
->polygon_list
= panfrost_batch_create_bo(batch
, size
,
656 PAN_BO_ACCESS_PRIVATE
|
658 PAN_BO_ACCESS_VERTEX_TILER
|
659 PAN_BO_ACCESS_FRAGMENT
);
662 return batch
->polygon_list
->gpu
;
666 panfrost_batch_get_scratchpad(struct panfrost_batch
*batch
,
668 unsigned thread_tls_alloc
,
671 unsigned size
= panfrost_get_total_stack_size(shift
,
675 if (batch
->scratchpad
) {
676 assert(batch
->scratchpad
->size
>= size
);
678 batch
->scratchpad
= panfrost_batch_create_bo(batch
, size
,
680 PAN_BO_ACCESS_PRIVATE
|
682 PAN_BO_ACCESS_VERTEX_TILER
|
683 PAN_BO_ACCESS_FRAGMENT
);
686 return batch
->scratchpad
;
690 panfrost_batch_get_shared_memory(struct panfrost_batch
*batch
,
692 unsigned workgroup_count
)
694 if (batch
->shared_memory
) {
695 assert(batch
->shared_memory
->size
>= size
);
697 batch
->shared_memory
= panfrost_batch_create_bo(batch
, size
,
699 PAN_BO_ACCESS_PRIVATE
|
701 PAN_BO_ACCESS_VERTEX_TILER
);
704 return batch
->shared_memory
;
708 panfrost_batch_get_tiler_heap(struct panfrost_batch
*batch
)
710 if (batch
->tiler_heap
)
711 return batch
->tiler_heap
;
713 batch
->tiler_heap
= panfrost_batch_create_bo(batch
, 4096 * 4096,
716 PAN_BO_ACCESS_PRIVATE
|
718 PAN_BO_ACCESS_VERTEX_TILER
|
719 PAN_BO_ACCESS_FRAGMENT
);
720 assert(batch
->tiler_heap
);
721 return batch
->tiler_heap
;
725 panfrost_batch_get_tiler_meta(struct panfrost_batch
*batch
, unsigned vertex_count
)
730 if (batch
->tiler_meta
)
731 return batch
->tiler_meta
;
733 struct panfrost_bo
*tiler_heap
;
734 tiler_heap
= panfrost_batch_get_tiler_heap(batch
);
736 struct bifrost_tiler_heap_meta tiler_heap_meta
= {
737 .heap_size
= tiler_heap
->size
,
738 .tiler_heap_start
= tiler_heap
->gpu
,
739 .tiler_heap_free
= tiler_heap
->gpu
,
740 .tiler_heap_end
= tiler_heap
->gpu
+ tiler_heap
->size
,
742 .unk7e007e
= 0x7e007e,
745 struct bifrost_tiler_meta tiler_meta
= {
746 .hierarchy_mask
= 0x28,
748 .width
= MALI_POSITIVE(batch
->key
.width
),
749 .height
= MALI_POSITIVE(batch
->key
.height
),
750 .tiler_heap_meta
= panfrost_upload_transient(batch
, &tiler_heap_meta
, sizeof(tiler_heap_meta
)),
753 batch
->tiler_meta
= panfrost_upload_transient(batch
, &tiler_meta
, sizeof(tiler_meta
));
754 return batch
->tiler_meta
;
758 panfrost_batch_get_tiler_dummy(struct panfrost_batch
*batch
)
760 struct panfrost_device
*dev
= pan_device(batch
->ctx
->base
.screen
);
762 uint32_t create_flags
= 0;
764 if (batch
->tiler_dummy
)
765 return batch
->tiler_dummy
;
767 if (!(dev
->quirks
& MIDGARD_NO_HIER_TILING
))
768 create_flags
= PAN_BO_INVISIBLE
;
770 batch
->tiler_dummy
= panfrost_batch_create_bo(batch
, 4096,
772 PAN_BO_ACCESS_PRIVATE
|
774 PAN_BO_ACCESS_VERTEX_TILER
|
775 PAN_BO_ACCESS_FRAGMENT
);
776 assert(batch
->tiler_dummy
);
777 return batch
->tiler_dummy
;
781 panfrost_batch_draw_wallpaper(struct panfrost_batch
*batch
)
783 /* Color 0 is cleared, no need to draw the wallpaper.
784 * TODO: MRT wallpapers.
786 if (batch
->clear
& PIPE_CLEAR_COLOR0
)
789 /* Nothing to reload? TODO: MRT wallpapers */
790 if (batch
->key
.cbufs
[0] == NULL
)
793 /* No draw calls, and no clear on the depth/stencil bufs.
794 * Drawing the wallpaper would be useless.
796 if (!batch
->tiler_dep
&&
797 !(batch
->clear
& PIPE_CLEAR_DEPTHSTENCIL
))
800 /* Check if the buffer has any content on it worth preserving */
802 struct pipe_surface
*surf
= batch
->key
.cbufs
[0];
803 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
804 unsigned level
= surf
->u
.tex
.level
;
806 if (!rsrc
->slices
[level
].initialized
)
809 batch
->ctx
->wallpaper_batch
= batch
;
811 /* Clamp the rendering area to the damage extent. The
812 * KHR_partial_update() spec states that trying to render outside of
813 * the damage region is "undefined behavior", so we should be safe.
815 unsigned damage_width
= (rsrc
->damage
.extent
.maxx
- rsrc
->damage
.extent
.minx
);
816 unsigned damage_height
= (rsrc
->damage
.extent
.maxy
- rsrc
->damage
.extent
.miny
);
818 if (damage_width
&& damage_height
) {
819 panfrost_batch_intersection_scissor(batch
,
820 rsrc
->damage
.extent
.minx
,
821 rsrc
->damage
.extent
.miny
,
822 rsrc
->damage
.extent
.maxx
,
823 rsrc
->damage
.extent
.maxy
);
826 /* FIXME: Looks like aligning on a tile is not enough, but
827 * aligning on twice the tile size seems to works. We don't
828 * know exactly what happens here but this deserves extra
829 * investigation to figure it out.
831 batch
->minx
= batch
->minx
& ~((MALI_TILE_LENGTH
* 2) - 1);
832 batch
->miny
= batch
->miny
& ~((MALI_TILE_LENGTH
* 2) - 1);
833 batch
->maxx
= MIN2(ALIGN_POT(batch
->maxx
, MALI_TILE_LENGTH
* 2),
835 batch
->maxy
= MIN2(ALIGN_POT(batch
->maxy
, MALI_TILE_LENGTH
* 2),
838 struct pipe_scissor_state damage
;
839 struct pipe_box rects
[4];
841 /* Clamp the damage box to the rendering area. */
842 damage
.minx
= MAX2(batch
->minx
, rsrc
->damage
.biggest_rect
.x
);
843 damage
.miny
= MAX2(batch
->miny
, rsrc
->damage
.biggest_rect
.y
);
844 damage
.maxx
= MIN2(batch
->maxx
,
845 rsrc
->damage
.biggest_rect
.x
+
846 rsrc
->damage
.biggest_rect
.width
);
847 damage
.maxx
= MAX2(damage
.maxx
, damage
.minx
);
848 damage
.maxy
= MIN2(batch
->maxy
,
849 rsrc
->damage
.biggest_rect
.y
+
850 rsrc
->damage
.biggest_rect
.height
);
851 damage
.maxy
= MAX2(damage
.maxy
, damage
.miny
);
853 /* One damage rectangle means we can end up with at most 4 reload
855 * 1: left region, only exists if damage.x > 0
856 * 2: right region, only exists if damage.x + damage.width < fb->width
857 * 3: top region, only exists if damage.y > 0. The intersection with
858 * the left and right regions are dropped
859 * 4: bottom region, only exists if damage.y + damage.height < fb->height.
860 * The intersection with the left and right regions are dropped
862 * ____________________________
869 * |_______|___________|______|
871 u_box_2d(batch
->minx
, batch
->miny
, damage
.minx
- batch
->minx
,
872 batch
->maxy
- batch
->miny
, &rects
[0]);
873 u_box_2d(damage
.maxx
, batch
->miny
, batch
->maxx
- damage
.maxx
,
874 batch
->maxy
- batch
->miny
, &rects
[1]);
875 u_box_2d(damage
.minx
, batch
->miny
, damage
.maxx
- damage
.minx
,
876 damage
.miny
- batch
->miny
, &rects
[2]);
877 u_box_2d(damage
.minx
, damage
.maxy
, damage
.maxx
- damage
.minx
,
878 batch
->maxy
- damage
.maxy
, &rects
[3]);
880 for (unsigned i
= 0; i
< 4; i
++) {
881 /* Width and height are always >= 0 even if width is declared as a
882 * signed integer: u_box_2d() helper takes unsigned args and
883 * panfrost_set_damage_region() is taking care of clamping
886 if (!rects
[i
].width
|| !rects
[i
].height
)
889 /* Blit the wallpaper in */
890 panfrost_blit_wallpaper(batch
->ctx
, &rects
[i
]);
892 batch
->ctx
->wallpaper_batch
= NULL
;
896 panfrost_batch_submit_ioctl(struct panfrost_batch
*batch
,
897 mali_ptr first_job_desc
,
900 struct panfrost_context
*ctx
= batch
->ctx
;
901 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
902 struct panfrost_device
*dev
= pan_device(gallium
->screen
);
903 struct drm_panfrost_submit submit
= {0,};
904 uint32_t *bo_handles
, *in_syncs
= NULL
;
905 bool is_fragment_shader
;
908 is_fragment_shader
= (reqs
& PANFROST_JD_REQ_FS
) && batch
->first_job
;
909 if (is_fragment_shader
)
910 submit
.in_sync_count
= 1;
912 submit
.in_sync_count
= util_dynarray_num_elements(&batch
->dependencies
,
913 struct panfrost_batch_fence
*);
915 if (submit
.in_sync_count
) {
916 in_syncs
= calloc(submit
.in_sync_count
, sizeof(*in_syncs
));
920 /* The fragment job always depends on the vertex/tiler job if there's
923 if (is_fragment_shader
) {
924 in_syncs
[0] = batch
->out_sync
->syncobj
;
928 util_dynarray_foreach(&batch
->dependencies
,
929 struct panfrost_batch_fence
*, dep
)
930 in_syncs
[i
++] = (*dep
)->syncobj
;
933 submit
.in_syncs
= (uintptr_t)in_syncs
;
934 submit
.out_sync
= batch
->out_sync
->syncobj
;
935 submit
.jc
= first_job_desc
;
936 submit
.requirements
= reqs
;
938 bo_handles
= calloc(batch
->bos
->entries
, sizeof(*bo_handles
));
941 hash_table_foreach(batch
->bos
, entry
) {
942 struct panfrost_bo
*bo
= (struct panfrost_bo
*)entry
->key
;
943 uint32_t flags
= (uintptr_t)entry
->data
;
945 assert(bo
->gem_handle
> 0);
946 bo_handles
[submit
.bo_handle_count
++] = bo
->gem_handle
;
948 /* Update the BO access flags so that panfrost_bo_wait() knows
949 * about all pending accesses.
950 * We only keep the READ/WRITE info since this is all the BO
951 * wait logic cares about.
952 * We also preserve existing flags as this batch might not
953 * be the first one to access the BO.
955 bo
->gpu_access
|= flags
& (PAN_BO_ACCESS_RW
);
958 submit
.bo_handles
= (u64
) (uintptr_t) bo_handles
;
959 ret
= drmIoctl(dev
->fd
, DRM_IOCTL_PANFROST_SUBMIT
, &submit
);
964 DBG("Error submitting: %m\n");
968 /* Trace the job if we're doing that */
969 if (pan_debug
& (PAN_DBG_TRACE
| PAN_DBG_SYNC
)) {
970 /* Wait so we can get errors reported back */
971 drmSyncobjWait(dev
->fd
, &batch
->out_sync
->syncobj
, 1,
974 /* Trace gets priority over sync */
975 bool minimal
= !(pan_debug
& PAN_DBG_TRACE
);
976 pandecode_jc(submit
.jc
, dev
->quirks
& IS_BIFROST
, dev
->gpu_id
, minimal
);
983 panfrost_batch_submit_jobs(struct panfrost_batch
*batch
)
985 bool has_draws
= batch
->first_job
;
989 ret
= panfrost_batch_submit_ioctl(batch
, batch
->first_job
, 0);
993 if (batch
->tiler_dep
|| batch
->clear
) {
994 mali_ptr fragjob
= panfrost_fragment_job(batch
, has_draws
);
995 ret
= panfrost_batch_submit_ioctl(batch
, fragjob
, PANFROST_JD_REQ_FS
);
1003 panfrost_batch_submit(struct panfrost_batch
*batch
)
1007 /* Submit the dependencies first. */
1008 util_dynarray_foreach(&batch
->dependencies
,
1009 struct panfrost_batch_fence
*, dep
) {
1011 panfrost_batch_submit((*dep
)->batch
);
1016 /* Nothing to do! */
1017 if (!batch
->first_job
&& !batch
->clear
) {
1018 /* Mark the fence as signaled so the fence logic does not try
1021 batch
->out_sync
->signaled
= true;
1025 panfrost_batch_draw_wallpaper(batch
);
1027 /* Now that all draws are in, we can finally prepare the
1028 * FBD for the batch */
1030 if (batch
->framebuffer
.gpu
&& batch
->first_job
) {
1031 struct panfrost_context
*ctx
= batch
->ctx
;
1032 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
1033 struct panfrost_device
*dev
= pan_device(gallium
->screen
);
1035 if (dev
->quirks
& MIDGARD_SFBD
)
1036 panfrost_attach_sfbd(batch
, ~0);
1038 panfrost_attach_mfbd(batch
, ~0);
1041 panfrost_scoreboard_initialize_tiler(batch
);
1043 ret
= panfrost_batch_submit_jobs(batch
);
1046 DBG("panfrost_batch_submit failed: %d\n", ret
);
1048 /* We must reset the damage info of our render targets here even
1049 * though a damage reset normally happens when the DRI layer swaps
1050 * buffers. That's because there can be implicit flushes the GL
1051 * app is not aware of, and those might impact the damage region: if
1052 * part of the damaged portion is drawn during those implicit flushes,
1053 * you have to reload those areas before next draws are pushed, and
1054 * since the driver can't easily know what's been modified by the draws
1055 * it flushed, the easiest solution is to reload everything.
1057 for (unsigned i
= 0; i
< batch
->key
.nr_cbufs
; i
++) {
1058 struct panfrost_resource
*res
;
1060 if (!batch
->key
.cbufs
[i
])
1063 res
= pan_resource(batch
->key
.cbufs
[i
]->texture
);
1064 panfrost_resource_reset_damage(res
);
1068 panfrost_freeze_batch(batch
);
1069 panfrost_free_batch(batch
);
1073 panfrost_flush_all_batches(struct panfrost_context
*ctx
, bool wait
)
1075 struct util_dynarray fences
, syncobjs
;
1078 util_dynarray_init(&fences
, NULL
);
1079 util_dynarray_init(&syncobjs
, NULL
);
1082 hash_table_foreach(ctx
->batches
, hentry
) {
1083 struct panfrost_batch
*batch
= hentry
->data
;
1088 panfrost_batch_fence_reference(batch
->out_sync
);
1089 util_dynarray_append(&fences
, struct panfrost_batch_fence
*,
1091 util_dynarray_append(&syncobjs
, uint32_t,
1092 batch
->out_sync
->syncobj
);
1095 panfrost_batch_submit(batch
);
1098 assert(!ctx
->batches
->entries
);
1100 /* Collect batch fences before returning */
1101 panfrost_gc_fences(ctx
);
1106 drmSyncobjWait(pan_device(ctx
->base
.screen
)->fd
,
1107 util_dynarray_begin(&syncobjs
),
1108 util_dynarray_num_elements(&syncobjs
, uint32_t),
1109 INT64_MAX
, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL
, NULL
);
1111 util_dynarray_foreach(&fences
, struct panfrost_batch_fence
*, fence
)
1112 panfrost_batch_fence_unreference(*fence
);
1114 util_dynarray_fini(&fences
);
1115 util_dynarray_fini(&syncobjs
);
1119 panfrost_pending_batches_access_bo(struct panfrost_context
*ctx
,
1120 const struct panfrost_bo
*bo
)
1122 struct panfrost_bo_access
*access
;
1123 struct hash_entry
*hentry
;
1125 hentry
= _mesa_hash_table_search(ctx
->accessed_bos
, bo
);
1126 access
= hentry
? hentry
->data
: NULL
;
1130 if (access
->writer
&& access
->writer
->batch
)
1133 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
1135 if (*reader
&& (*reader
)->batch
)
1143 panfrost_flush_batches_accessing_bo(struct panfrost_context
*ctx
,
1144 struct panfrost_bo
*bo
,
1145 uint32_t access_type
)
1147 struct panfrost_bo_access
*access
;
1148 struct hash_entry
*hentry
;
1150 /* It doesn't make any to flush only the readers. */
1151 assert(access_type
== PAN_BO_ACCESS_WRITE
||
1152 access_type
== PAN_BO_ACCESS_RW
);
1154 hentry
= _mesa_hash_table_search(ctx
->accessed_bos
, bo
);
1155 access
= hentry
? hentry
->data
: NULL
;
1159 if (access_type
& PAN_BO_ACCESS_WRITE
&& access
->writer
&&
1160 access
->writer
->batch
)
1161 panfrost_batch_submit(access
->writer
->batch
);
1163 if (!(access_type
& PAN_BO_ACCESS_READ
))
1166 util_dynarray_foreach(&access
->readers
, struct panfrost_batch_fence
*,
1168 if (*reader
&& (*reader
)->batch
)
1169 panfrost_batch_submit((*reader
)->batch
);
1174 panfrost_batch_set_requirements(struct panfrost_batch
*batch
)
1176 struct panfrost_context
*ctx
= batch
->ctx
;
1178 if (ctx
->rasterizer
&& ctx
->rasterizer
->base
.multisample
)
1179 batch
->requirements
|= PAN_REQ_MSAA
;
1181 if (ctx
->depth_stencil
&& ctx
->depth_stencil
->depth
.writemask
)
1182 batch
->requirements
|= PAN_REQ_DEPTH_WRITE
;
1186 panfrost_batch_adjust_stack_size(struct panfrost_batch
*batch
)
1188 struct panfrost_context
*ctx
= batch
->ctx
;
1190 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; ++i
) {
1191 struct panfrost_shader_state
*ss
;
1193 ss
= panfrost_get_shader_state(ctx
, i
);
1197 batch
->stack_size
= MAX2(batch
->stack_size
, ss
->stack_size
);
1201 /* Helper to smear a 32-bit color across 128-bit components */
1204 pan_pack_color_32(uint32_t *packed
, uint32_t v
)
1206 for (unsigned i
= 0; i
< 4; ++i
)
1211 pan_pack_color_64(uint32_t *packed
, uint32_t lo
, uint32_t hi
)
1213 for (unsigned i
= 0; i
< 4; i
+= 2) {
1220 pan_pack_color(uint32_t *packed
, const union pipe_color_union
*color
, enum pipe_format format
)
1222 /* Alpha magicked to 1.0 if there is no alpha */
1224 bool has_alpha
= util_format_has_alpha(format
);
1225 float clear_alpha
= has_alpha
? color
->f
[3] : 1.0f
;
1227 /* Packed color depends on the framebuffer format */
1229 const struct util_format_description
*desc
=
1230 util_format_description(format
);
1232 if (util_format_is_rgba8_variant(desc
)) {
1233 pan_pack_color_32(packed
,
1234 ((uint32_t) float_to_ubyte(clear_alpha
) << 24) |
1235 ((uint32_t) float_to_ubyte(color
->f
[2]) << 16) |
1236 ((uint32_t) float_to_ubyte(color
->f
[1]) << 8) |
1237 ((uint32_t) float_to_ubyte(color
->f
[0]) << 0));
1238 } else if (format
== PIPE_FORMAT_B5G6R5_UNORM
) {
1239 /* First, we convert the components to R5, G6, B5 separately */
1240 unsigned r5
= _mesa_roundevenf(SATURATE(color
->f
[0]) * 31.0);
1241 unsigned g6
= _mesa_roundevenf(SATURATE(color
->f
[1]) * 63.0);
1242 unsigned b5
= _mesa_roundevenf(SATURATE(color
->f
[2]) * 31.0);
1244 /* Then we pack into a sparse u32. TODO: Why these shifts? */
1245 pan_pack_color_32(packed
, (b5
<< 25) | (g6
<< 14) | (r5
<< 5));
1246 } else if (format
== PIPE_FORMAT_B4G4R4A4_UNORM
) {
1247 /* Convert to 4-bits */
1248 unsigned r4
= _mesa_roundevenf(SATURATE(color
->f
[0]) * 15.0);
1249 unsigned g4
= _mesa_roundevenf(SATURATE(color
->f
[1]) * 15.0);
1250 unsigned b4
= _mesa_roundevenf(SATURATE(color
->f
[2]) * 15.0);
1251 unsigned a4
= _mesa_roundevenf(SATURATE(clear_alpha
) * 15.0);
1253 /* Pack on *byte* intervals */
1254 pan_pack_color_32(packed
, (a4
<< 28) | (b4
<< 20) | (g4
<< 12) | (r4
<< 4));
1255 } else if (format
== PIPE_FORMAT_B5G5R5A1_UNORM
) {
1256 /* Scale as expected but shift oddly */
1257 unsigned r5
= _mesa_roundevenf(SATURATE(color
->f
[0]) * 31.0);
1258 unsigned g5
= _mesa_roundevenf(SATURATE(color
->f
[1]) * 31.0);
1259 unsigned b5
= _mesa_roundevenf(SATURATE(color
->f
[2]) * 31.0);
1260 unsigned a1
= _mesa_roundevenf(SATURATE(clear_alpha
) * 1.0);
1262 pan_pack_color_32(packed
, (a1
<< 31) | (b5
<< 25) | (g5
<< 15) | (r5
<< 5));
1264 /* Otherwise, it's generic subject to replication */
1266 union util_color out
= { 0 };
1267 unsigned size
= util_format_get_blocksize(format
);
1269 util_pack_color(color
->f
, format
, &out
);
1272 unsigned b
= out
.ui
[0];
1273 unsigned s
= b
| (b
<< 8);
1274 pan_pack_color_32(packed
, s
| (s
<< 16));
1275 } else if (size
== 2)
1276 pan_pack_color_32(packed
, out
.ui
[0] | (out
.ui
[0] << 16));
1277 else if (size
== 3 || size
== 4)
1278 pan_pack_color_32(packed
, out
.ui
[0]);
1280 pan_pack_color_64(packed
, out
.ui
[0], out
.ui
[1] | (out
.ui
[1] << 16)); /* RGB16F -- RGBB */
1282 pan_pack_color_64(packed
, out
.ui
[0], out
.ui
[1]);
1283 else if (size
== 16)
1284 memcpy(packed
, out
.ui
, 16);
1286 unreachable("Unknown generic format size packing clear colour");
1291 panfrost_batch_clear(struct panfrost_batch
*batch
,
1293 const union pipe_color_union
*color
,
1294 double depth
, unsigned stencil
)
1296 struct panfrost_context
*ctx
= batch
->ctx
;
1298 if (buffers
& PIPE_CLEAR_COLOR
) {
1299 for (unsigned i
= 0; i
< PIPE_MAX_COLOR_BUFS
; ++i
) {
1300 if (!(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
1303 enum pipe_format format
= ctx
->pipe_framebuffer
.cbufs
[i
]->format
;
1304 pan_pack_color(batch
->clear_color
[i
], color
, format
);
1308 if (buffers
& PIPE_CLEAR_DEPTH
) {
1309 batch
->clear_depth
= depth
;
1312 if (buffers
& PIPE_CLEAR_STENCIL
) {
1313 batch
->clear_stencil
= stencil
;
1316 batch
->clear
|= buffers
;
1318 /* Clearing affects the entire framebuffer (by definition -- this is
1319 * the Gallium clear callback, which clears the whole framebuffer. If
1320 * the scissor test were enabled from the GL side, the gallium frontend
1321 * would emit a quad instead and we wouldn't go down this code path) */
1323 panfrost_batch_union_scissor(batch
, 0, 0,
1324 ctx
->pipe_framebuffer
.width
,
1325 ctx
->pipe_framebuffer
.height
);
1329 panfrost_batch_compare(const void *a
, const void *b
)
1331 return util_framebuffer_state_equal(a
, b
);
1335 panfrost_batch_hash(const void *key
)
1337 return _mesa_hash_data(key
, sizeof(struct pipe_framebuffer_state
));
1340 /* Given a new bounding rectangle (scissor), let the job cover the union of the
1341 * new and old bounding rectangles */
1344 panfrost_batch_union_scissor(struct panfrost_batch
*batch
,
1345 unsigned minx
, unsigned miny
,
1346 unsigned maxx
, unsigned maxy
)
1348 batch
->minx
= MIN2(batch
->minx
, minx
);
1349 batch
->miny
= MIN2(batch
->miny
, miny
);
1350 batch
->maxx
= MAX2(batch
->maxx
, maxx
);
1351 batch
->maxy
= MAX2(batch
->maxy
, maxy
);
1355 panfrost_batch_intersection_scissor(struct panfrost_batch
*batch
,
1356 unsigned minx
, unsigned miny
,
1357 unsigned maxx
, unsigned maxy
)
1359 batch
->minx
= MAX2(batch
->minx
, minx
);
1360 batch
->miny
= MAX2(batch
->miny
, miny
);
1361 batch
->maxx
= MIN2(batch
->maxx
, maxx
);
1362 batch
->maxy
= MIN2(batch
->maxy
, maxy
);
1365 /* Are we currently rendering to the dev (rather than an FBO)? */
1368 panfrost_batch_is_scanout(struct panfrost_batch
*batch
)
1370 /* If there is no color buffer, it's an FBO */
1371 if (batch
->key
.nr_cbufs
!= 1)
1374 /* If we're too early that no framebuffer was sent, it's scanout */
1375 if (!batch
->key
.cbufs
[0])
1378 return batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_DISPLAY_TARGET
||
1379 batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_SCANOUT
||
1380 batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_SHARED
;
1384 panfrost_batch_init(struct panfrost_context
*ctx
)
1386 ctx
->batches
= _mesa_hash_table_create(ctx
,
1387 panfrost_batch_hash
,
1388 panfrost_batch_compare
);
1389 ctx
->accessed_bos
= _mesa_hash_table_create(ctx
, _mesa_hash_pointer
,
1390 _mesa_key_pointer_equal
);