diff --git a/src/gallium/drivers/asahi/agx_batch.c b/src/gallium/drivers/asahi/agx_batch.c index 302f2807c195..ec9d0e4b6e91 100644 --- a/src/gallium/drivers/asahi/agx_batch.c +++ b/src/gallium/drivers/asahi/agx_batch.c @@ -18,6 +18,7 @@ #include "util/u_dynarray.h" #include "util/u_range.h" #include "agx_device.h" +#include "agx_fence.h" #include "agx_state.h" #include "vdrm.h" @@ -539,6 +540,11 @@ agx_batch_cleanup(struct agx_context *ctx, struct agx_batch *batch, bool reset) struct agx_device *dev = agx_device(ctx->base.screen); struct agx_screen *screen = agx_screen(ctx->base.screen); + if (ctx->last_submit_batch == batch) + ctx->last_submit_batch = NULL; + if (batch->exported_fence) + agx_fence_reference(ctx->base.screen, &batch->exported_fence, NULL); + assert(batch->ctx == ctx); assert(!batch->submit_quarantined); assert(agx_batch_is_submitted(batch)); @@ -678,6 +684,22 @@ agx_check_batch_fence(struct agx_context *ctx, struct agx_batch *batch) ctx->reset.reset(ctx->reset.data, PIPE_GUILTY_CONTEXT_RESET); } +static void +agx_retire_batch(struct agx_context *ctx, struct agx_batch *batch, + bool completed) +{ + agx_check_batch_fence(ctx, batch); + + if (completed) { + if (batch->exported_fence) + p_atomic_set(&batch->exported_fence->signaled, true); + if (ctx->last_submit_batch == batch) + ctx->last_submit_complete = true; + } + + agx_batch_cleanup(ctx, batch, false); +} + int agx_cleanup_batches(struct agx_context *ctx) { @@ -693,8 +715,14 @@ agx_cleanup_batches(struct agx_context *ctx) if (ctx->batches.slots[i].submit_quarantined) continue; - batches[count] = &ctx->batches.slots[i]; - syncobjs[count++] = ctx->batches.slots[i].syncobj; + struct agx_batch *batch = &ctx->batches.slots[i]; + if (batch->exported_fence && p_atomic_read(&batch->exported_fence->signaled)) { + agx_retire_batch(ctx, batch, true); + return i; + } + + batches[count] = batch; + syncobjs[count++] = batch->syncobj; } if (!count) @@ -706,8 +734,7 @@ agx_cleanup_batches(struct agx_context *ctx) return -1; assert(first < AGX_MAX_BATCHES); - agx_check_batch_fence(ctx, batches[first]); - agx_batch_cleanup(ctx, batches[first], false); + agx_retire_batch(ctx, batches[first], true); return agx_batch_idx(batches[first]); } @@ -2094,7 +2121,7 @@ agx_batch_submit(struct agx_context *ctx, struct agx_batch *batch, */ uint32_t compute_syncobj = 0; uint32_t submit_syncobj = 0; - int create_ret = drmSyncobjCreate(dev->fd, 0, &submit_syncobj); + int create_ret = agx_acquire_submit_syncobj(screen, &submit_syncobj); if (create_ret) { const int error = create_ret < -1 ? -create_ret : (errno ? errno : EIO); u_rwlock_rdunlock(&screen->destroy_lock); @@ -2781,8 +2808,10 @@ agx_batch_submit(struct agx_context *ctx, struct agx_batch *batch, /* Record the last syncobj for fence creation */ ctx->syncobj = batch->syncobj; if (ctx->last_submit_fence) - drmSyncobjDestroy(dev->fd, ctx->last_submit_fence); + agx_recycle_submit_syncobj(screen, ctx->last_submit_fence); ctx->last_submit_fence = submit_syncobj; + ctx->last_submit_batch = batch; + ctx->last_submit_complete = false; /* Update the last seqid in the context (must only happen if the submit * succeeded, otherwise the timeline point would not be valid). @@ -2815,8 +2844,7 @@ agx_poll_batch(struct agx_context *ctx, struct agx_batch *batch) if (ret) return false; - agx_check_batch_fence(ctx, batch); - agx_batch_cleanup(ctx, batch, false); + agx_retire_batch(ctx, batch, true); return true; } @@ -2838,8 +2866,7 @@ agx_sync_batch(struct agx_context *ctx, struct agx_batch *batch) assert(batch->syncobj); int ret = drmSyncobjWait(dev->fd, &batch->syncobj, 1, INT64_MAX, 0, NULL); assert(!ret); - agx_check_batch_fence(ctx, batch); - agx_batch_cleanup(ctx, batch, false); + agx_retire_batch(ctx, batch, !ret); } void diff --git a/src/gallium/drivers/asahi/agx_fence.c b/src/gallium/drivers/asahi/agx_fence.c index 5d2519f7492f..06807f3a2074 100644 --- a/src/gallium/drivers/asahi/agx_fence.c +++ b/src/gallium/drivers/asahi/agx_fence.c @@ -22,6 +22,50 @@ #include "util/os_time.h" #include "util/u_inlines.h" +int +agx_acquire_submit_syncobj(struct agx_screen *screen, uint32_t *handle) +{ + const unsigned capacity = ARRAY_SIZE(screen->submit_syncobjs.handles); + uint32_t *handles = screen->submit_syncobjs.handles; + + simple_mtx_lock(&screen->submit_syncobjs.lock); + if (screen->submit_syncobjs.returned == capacity) { + /* An empty output distinguishes failure before publication from a + * newly published fence. Reset all returned handles before reuse. + */ + if (drmSyncobjReset(screen->dev.fd, handles, capacity) == 0) { + screen->submit_syncobjs.ready = capacity; + } else { + for (unsigned i = 0; i < capacity; ++i) + drmSyncobjDestroy(screen->dev.fd, handles[i]); + } + screen->submit_syncobjs.returned = 0; + } + + if (screen->submit_syncobjs.ready) { + *handle = handles[--screen->submit_syncobjs.ready]; + simple_mtx_unlock(&screen->submit_syncobjs.lock); + return 0; + } + simple_mtx_unlock(&screen->submit_syncobjs.lock); + return drmSyncobjCreate(screen->dev.fd, 0, handle); +} + +void +agx_recycle_submit_syncobj(struct agx_screen *screen, uint32_t handle) +{ + const unsigned capacity = ARRAY_SIZE(screen->submit_syncobjs.handles); + simple_mtx_lock(&screen->submit_syncobjs.lock); + if (screen->submit_syncobjs.ready + screen->submit_syncobjs.returned < capacity) { + unsigned index = capacity - ++screen->submit_syncobjs.returned; + screen->submit_syncobjs.handles[index] = handle; + simple_mtx_unlock(&screen->submit_syncobjs.lock); + return; + } + simple_mtx_unlock(&screen->submit_syncobjs.lock); + drmSyncobjDestroy(screen->dev.fd, handle); +} + void agx_fence_reference(struct pipe_screen *pscreen, struct pipe_fence_handle **ptr, struct pipe_fence_handle *fence) @@ -33,7 +77,9 @@ agx_fence_reference(struct pipe_screen *pscreen, struct pipe_fence_handle **ptr, fence ? &fence->reference : NULL)) { tc_unflushed_batch_token_reference(&old->tc_token, NULL); util_queue_fence_destroy(&old->ready); - if (old->syncobj) + if (old->owns_private_handle) + agx_recycle_submit_syncobj(agx_screen(pscreen), old->syncobj); + else if (old->syncobj) drmSyncobjDestroy(dev->fd, old->syncobj); free(old); } @@ -165,7 +211,7 @@ agx_fence_from_fd(struct agx_context *ctx, int fd, enum pipe_fd_type type) } static bool -agx_fence_snapshot_syncobj(struct agx_context *ctx, uint32_t *out_syncobj) +agx_fence_snapshot_syncobj(struct agx_context *ctx, struct pipe_fence_handle *f) { struct agx_device *dev = agx_device(ctx->base.screen); int fd = -1, ret; @@ -180,7 +226,17 @@ agx_fence_snapshot_syncobj(struct agx_context *ctx, uint32_t *out_syncobj) /* The private submit output already contains an immutable snapshot. * Further fences on the same batch can snapshot its reusable syncobj. */ if (ctx->last_submit_fence) { - *out_syncobj = ctx->last_submit_fence; + f->syncobj = ctx->last_submit_fence; + /* Both outputs were attached to the same successful submit. + * Only its private snapshot may share this batch's completion. + */ + if (ctx->last_submit_batch) { + agx_fence_reference(ctx->base.screen, + &ctx->last_submit_batch->exported_fence, f); + } else if (ctx->last_submit_complete) { + p_atomic_set(&f->signaled, true); + } + ctx->last_submit_batch = NULL; ctx->last_submit_fence = 0; return true; } @@ -195,7 +251,7 @@ agx_fence_snapshot_syncobj(struct agx_context *ctx, uint32_t *out_syncobj) ret = drmSyncobjTransfer(dev->fd, syncobj, 0, ctx->syncobj, 0, 0); if (!ret) { - *out_syncobj = syncobj; + f->syncobj = syncobj; return true; } @@ -275,26 +331,26 @@ agx_fence_snapshot_syncobj(struct agx_context *ctx, uint32_t *out_syncobj) return false; } - *out_syncobj = syncobj; + f->syncobj = syncobj; return true; } struct pipe_fence_handle * agx_fence_create(struct agx_context *ctx) { - uint32_t syncobj = 0; - if (!agx_fence_snapshot_syncobj(ctx, &syncobj)) - return NULL; - struct pipe_fence_handle *f = calloc(1, sizeof(*f)); - if (!f) { - drmSyncobjDestroy(agx_device(ctx->base.screen)->fd, syncobj); + if (!f) return NULL; - } pipe_reference_init(&f->reference, 1); util_queue_fence_init(&f->ready); - f->syncobj = syncobj; + if (!agx_fence_snapshot_syncobj(ctx, f)) { + util_queue_fence_destroy(&f->ready); + free(f); + return NULL; + } + + f->owns_private_handle = true; return f; } @@ -316,11 +372,11 @@ agx_fence_create_unflushed(struct pipe_context *pctx, void agx_fence_populate(struct agx_context *ctx, struct pipe_fence_handle *f) { - uint32_t syncobj = 0; - if (agx_fence_snapshot_syncobj(ctx, &syncobj)) - f->syncobj = syncobj; - else + if (agx_fence_snapshot_syncobj(ctx, f)) { + f->owns_private_handle = true; + } else { f->faulted = true; + } util_queue_fence_signal(&f->ready); } diff --git a/src/gallium/drivers/asahi/agx_fence.h b/src/gallium/drivers/asahi/agx_fence.h index 2f38cebb8909..6fe5c58d4fee 100644 --- a/src/gallium/drivers/asahi/agx_fence.h +++ b/src/gallium/drivers/asahi/agx_fence.h @@ -11,6 +11,7 @@ #include "util/u_queue.h" struct agx_context; +struct agx_screen; struct tc_unflushed_batch_token; struct pipe_fence_handle { @@ -18,10 +19,14 @@ struct pipe_fence_handle { struct util_queue_fence ready; struct tc_unflushed_batch_token *tc_token; uint32_t syncobj; + bool owns_private_handle; bool signaled; bool faulted; }; +int agx_acquire_submit_syncobj(struct agx_screen *screen, uint32_t *handle); +void agx_recycle_submit_syncobj(struct agx_screen *screen, uint32_t handle); + void agx_fence_reference(struct pipe_screen *pscreen, struct pipe_fence_handle **ptr, struct pipe_fence_handle *fence); diff --git a/src/gallium/drivers/asahi/agx_pipe.c b/src/gallium/drivers/asahi/agx_pipe.c index 11858214c02e..368022530ce4 100644 --- a/src/gallium/drivers/asahi/agx_pipe.c +++ b/src/gallium/drivers/asahi/agx_pipe.c @@ -3761,6 +3761,13 @@ agx_destroy_screen(struct pipe_screen *pscreen) if (util_queue_is_initialized(&screen->shader_compiler_queue)) util_queue_destroy(&screen->shader_compiler_queue); + const unsigned capacity = ARRAY_SIZE(screen->submit_syncobjs.handles); + for (unsigned i = 0; i < capacity; ++i) { + if (i < screen->submit_syncobjs.ready || + i >= capacity - screen->submit_syncobjs.returned) + drmSyncobjDestroy(screen->dev.fd, screen->submit_syncobjs.handles[i]); + } + simple_mtx_destroy(&screen->submit_syncobjs.lock); drmSyncobjDestroy(screen->dev.fd, screen->flush_syncobj); if (screen->dev.ro) @@ -3926,6 +3933,7 @@ agx_screen_create(int fd, struct renderonly *ro, drmSyncobjCreate(agx_device(screen)->fd, 0, &agx_screen->flush_syncobj); assert(!ret); + simple_mtx_init(&agx_screen->submit_syncobjs.lock, mtx_plain); simple_mtx_init(&agx_screen->flush_seqid_lock, mtx_plain); slab_create_parent(&agx_screen->transfer_pool, sizeof(struct agx_transfer), 16); diff --git a/src/gallium/drivers/asahi/agx_state.h b/src/gallium/drivers/asahi/agx_state.h index 6b30e9bb3487..7cda9de9b4f7 100644 --- a/src/gallium/drivers/asahi/agx_state.h +++ b/src/gallium/drivers/asahi/agx_state.h @@ -7,6 +7,7 @@ #pragma once #include +#include "util/u_memory.h" #include "asahi/compiler/agx3_graphics_program.h" #include "asahi/compiler/agx_compile.h" #include "asahi/genxml/agx_pack.h" @@ -625,6 +626,8 @@ struct agx_batch { struct pipe_framebuffer_state key; uint64_t seqnum; uint32_t syncobj; + /* A GL fence that took the private output of this exact submission. */ + struct pipe_fence_handle *exported_fence; /* Failed waits retain these fences and all batch resources until exit. */ uint32_t failed_syncobjs[2]; bool submit_quarantined; @@ -1324,6 +1327,8 @@ struct agx_context { /* Private output of the last successful submit, owned until a GL fence * takes it or another submission replaces it. */ uint32_t last_submit_fence; + struct agx_batch *last_submit_batch; + bool last_submit_complete; int in_sync_fd; uint32_t in_sync_obj; uint64_t flush_last_seqid; @@ -1597,6 +1602,13 @@ struct agx_screen { bool submit_quarantined; struct agx_bo *rodata; + /* Empty handles occupy the front and returned handles the back. */ + struct { + simple_mtx_t lock; + uint32_t handles[MESA_CACHE_LINE_SIZE / sizeof(uint32_t)]; + unsigned ready, returned; + } submit_syncobjs; + /* Shared timeline syncobj and value to serialize flushes across contexts */ uint32_t flush_syncobj; uint64_t flush_cur_seqid;