Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions src/gallium/drivers/asahi/agx_batch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand All @@ -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]);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
Expand Down
90 changes: 73 additions & 17 deletions src/gallium/drivers/asahi/agx_fence.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions src/gallium/drivers/asahi/agx_fence.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@
#include "util/u_queue.h"

struct agx_context;
struct agx_screen;
struct tc_unflushed_batch_token;

struct pipe_fence_handle {
struct pipe_reference reference;
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);
Expand Down
8 changes: 8 additions & 0 deletions src/gallium/drivers/asahi/agx_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/gallium/drivers/asahi/agx_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include <xf86drm.h>
#include "util/u_memory.h"
#include "asahi/compiler/agx3_graphics_program.h"
#include "asahi/compiler/agx_compile.h"
#include "asahi/genxml/agx_pack.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down