Skip to content
48 changes: 38 additions & 10 deletions drivers/gpu/drm/asahi/g17/fence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,8 @@ mod tests {
let mut restart_outputs = Outputs::new(&contexts, restart_syncs, 0, &allocation_failure)?;
let aggregate = restart_outputs.publish_with(None, false, false)?;
let member = aggregate.member();
// Queue::submit() installs the outputs once the first command is queued.
restart_outputs.install();
let point2 = syncobj.fence_get().unwrap().chain_find_seqno(2)?.unwrap();
assert_eq!(super::super::job::fence_status(&point2), 0);
// SAFETY: `point2` owns a live dma_fence reference for this zero-timeout poll.
Expand Down Expand Up @@ -1071,7 +1073,8 @@ mod tests {
let s1 = independent(&contexts)?;
let items1 = [cumulative_item(anchor.clone(), 1), binary_item(out.clone())];
let mut b1 = super::super::sync_output::SignalBundle::new(&items1, 0)?;
b1.publish(&Fence::from_fence(&s1), true)?;
b1.prepare(&Fence::from_fence(&s1), true)?;
b1.commit();
let scope1 = out.fence_get().unwrap();
// SAFETY: `scope1` owns a live dma_fence reference.
assert!(unsafe { asahi_g17_is_scope(scope1.raw()) });
Expand All @@ -1085,7 +1088,8 @@ mod tests {
let s2 = independent(&contexts)?;
let items2 = [cumulative_item(anchor.clone(), 2), binary_item(out.clone())];
let mut b2 = super::super::sync_output::SignalBundle::new(&items2, 0)?;
b2.publish(&Fence::from_fence(&s2), true)?;
b2.prepare(&Fence::from_fence(&s2), true)?;
b2.commit();
let scope2 = out.fence_get().unwrap();
// SAFETY: `scope2` owns a live dma_fence reference.
assert!(unsafe { asahi_g17_is_scope(scope2.raw()) });
Expand All @@ -1109,7 +1113,8 @@ mod tests {
let s3 = independent(&contexts)?;
let items3 = [cumulative_item(anchor.clone(), 3), binary_item(out.clone())];
let mut b3 = super::super::sync_output::SignalBundle::new(&items3, 0)?;
b3.publish(&Fence::from_fence(&s3), true)?;
b3.prepare(&Fence::from_fence(&s3), true)?;
b3.commit();
let scope3 = out.fence_get().unwrap();
// SAFETY: `scope3` owns a live dma_fence reference.
assert!(unsafe { asahi_g17_is_scope(scope3.raw()) });
Expand Down Expand Up @@ -1173,6 +1178,7 @@ pub(crate) struct Outputs {
chunks: KVec<Arc<ChunkWork>>,
result: Result,
cumulative: Option<super::sync_output::SignalBundle>,
/// The submission's fence is in the outputs ([`Self::install`]).
installed: bool,
}
impl Outputs {
Expand Down Expand Up @@ -1215,6 +1221,23 @@ impl Outputs {
pub(crate) fn published(&self) -> bool {
self.installed
}
/// Stores the submission's fence, created by [`Self::publish`] or
/// [`Self::publish_with_status`], in every output. Cannot fail.
pub(crate) fn install(&mut self) {
let Some(submission) = &self.submission else {
return;
};
if self.installed {
return;
}
self.installed = true;
if let Some(bundle) = &mut self.cumulative {
bundle.commit();
self.syncs.truncate(self.input_count);
} else {
install(self.syncs.drain(self.input_count..), &submission.fence());
}
}
pub(crate) fn inputs(&self) -> &[SyncItem] {
&self.syncs[..self.input_count]
}
Expand Down Expand Up @@ -1262,13 +1285,10 @@ impl Outputs {
fence.compute_only = compute_only;
let submission = Aggregate(fence.into());
self.submission = Some(submission.clone());
// Outputs get the fence only at `install()`, once the commands are queued.
if let Some(bundle) = &mut self.cumulative {
bundle.publish(&submission.fence(), compute_only)?;
self.syncs.truncate(self.input_count);
} else {
install(self.syncs.drain(self.input_count..), &submission.fence());
bundle.prepare(&submission.fence(), compute_only)?;
}
self.installed = true;
Ok(submission)
}
pub(crate) fn set_result(&mut self, result: Result) {
Expand All @@ -1280,8 +1300,16 @@ impl Drop for Outputs {
for chunk in &self.chunks {
chunk.finish_enqueue(self.result);
}
if let Some(submission) = self.submission.take() {
if !untouched_refusal(self.installed, &self.result) {
if self.submission.is_some() {
if !self.installed && !untouched_refusal(false, &self.result) {
self.install();
}
let Some(submission) = self.submission.take() else {
return;
};
// A prepared cumulative signal that could not be committed falls back to the
// submission's own fence; this does nothing once committed.
if self.installed {
if let Some(bundle) = &mut self.cumulative {
bundle.fail(&submission.fence());
}
Expand Down
38 changes: 26 additions & 12 deletions drivers/gpu/drm/asahi/g17/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,14 @@ impl<B: Backend> Queue<B> {
fences.timestamp_waits = inherited_timestamp_waits;
fences.producers = producer_check;
self.sequence[index] = order.sequence;
// The command is queued: from here on no failure may read as a refusal (the room was
// reserved before the first command, so these cannot fail).
if fences.publishes() {
self.publishers[index].push_within_capacity(order.sequence, fences.clone())?;
self.publishers[index]
.push_within_capacity(order.sequence, fences.clone())
.map_err(|_| EIO)?;
}
self.frontiers[index].push_within_capacity(order.sequence, fences)?;
self.frontiers[index].push_within_capacity(order.sequence, fences).map_err(|_| EIO)?;
Ok(())
}
/// Finish parsing, validation and VM pinning before entering publication, into the
Expand Down Expand Up @@ -664,6 +668,8 @@ impl<B: Backend> crate::queue::Queue for Queue<B> {
}
};
let mut scratch = core::mem::take(&mut self.scratch);
// Commands queued so far; until the first is, nothing of the submission is visible.
let mut enqueued = 0usize;
let result = (|| {
if !self.backend.supports_submission() {
return Err(ENOTSUPP);
Expand Down Expand Up @@ -779,16 +785,6 @@ impl<B: Backend> crate::queue::Queue for Queue<B> {
}
outputs.publish(work, firmware_source, compute_only)?
};
let output_fence = aggregate.fence();
super::trace::submit(
1,
id,
self.context.id(),
input_count,
output_count,
0,
Some(&output_fence),
);
let shared_inputs =
Inputs::new(outputs.inputs().iter().filter_map(|sync| sync.fence.as_ref()))?;
let mut work_states = work_states.into_iter();
Expand All @@ -804,9 +800,27 @@ impl<B: Backend> crate::queue::Queue for Queue<B> {
shared_inputs.as_ref(),
batch_renders,
)?;
enqueued += 1;
}
Ok(())
})();
// Queued commands need their outputs to carry the submission's fence. A refusal before
// the first one was queued leaves them untouched.
if !super::fence::untouched_refusal(enqueued != 0, &result) {
outputs.install();
if outputs.published() {
let output_fence = outputs.output_fence();
super::trace::submit(
1,
id,
self.context.id(),
input_count,
output_count,
0,
output_fence.as_ref(),
);
}
}
// What an error left unsubmitted is released here, as before the scratch was kept.
scratch.pending.clear();
scratch.guards.clear();
Expand Down
37 changes: 27 additions & 10 deletions drivers/gpu/drm/asahi/g17/sync_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ struct asahi_g17_signal_bundle {
size_t count;
size_t anchor;
bool published;
/* The anchor's scope, prepared and not committed yet. */
struct dma_fence *scope;
struct asahi_g17_signal_target targets[];
};

Expand All @@ -25,6 +27,7 @@ void asahi_g17_signals_free(struct asahi_g17_signal_bundle *bundle)

if (!bundle)
return;
dma_fence_put(bundle->scope);
for (i = 0; i < bundle->count; ++i) {
struct asahi_g17_signal_target *target = &bundle->targets[i];
if (target->chain)
Expand Down Expand Up @@ -95,21 +98,36 @@ static void asahi_g17_signal_install(struct asahi_g17_signal_target *target,
}
}

int asahi_g17_signals_publish(struct asahi_g17_signal_bundle *bundle,
/*
* Allocate the anchor's scope over its current fence and @submitted. Nothing is
* stored in a sync object until asahi_g17_signals_commit(), so a failure here, or a
* bundle freed after it, leaves every output as it was.
*/
int asahi_g17_signals_prepare(struct asahi_g17_signal_bundle *bundle,
struct dma_fence *submitted, bool compute_only)
{
struct dma_fence *previous, *scope;
size_t i;
struct dma_fence *previous;

if (bundle->published)
if (bundle->published || bundle->scope)
return -EINVAL;
previous = drm_syncobj_fence_get(bundle->targets[bundle->anchor].syncobj);
scope = asahi_g17_scope_create(previous, submitted,
bundle->targets[bundle->anchor].point,
compute_only);
bundle->scope = asahi_g17_scope_create(previous, submitted,
bundle->targets[bundle->anchor].point,
compute_only);
dma_fence_put(previous);
if (!scope)
return -ENOMEM;
return bundle->scope ? 0 : -ENOMEM;
}

/* Install the prepared scope in every output. Cannot fail. */
void asahi_g17_signals_commit(struct asahi_g17_signal_bundle *bundle)
{
struct dma_fence *previous, *scope = bundle->scope;
size_t i;

if (bundle->published || !scope)
return;
bundle->scope = NULL;
asahi_g17_scope_arm(scope);
/* Only exported completion includes earlier work. GPU execution gets no new wait. */
asahi_g17_signal_install(&bundle->targets[bundle->anchor], scope);
previous = drm_syncobj_fence_get(bundle->targets[bundle->anchor].syncobj);
Expand All @@ -120,7 +138,6 @@ int asahi_g17_signals_publish(struct asahi_g17_signal_bundle *bundle,
asahi_g17_signal_install(&bundle->targets[i], scope);
bundle->published = true;
dma_fence_put(scope);
return 0;
}

/* Allocation or validation failed before enqueue: install an error promise without allocating. */
Expand Down
3 changes: 2 additions & 1 deletion drivers/gpu/drm/asahi/g17/sync_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ struct asahi_g17_signal_bundle *
asahi_g17_signals_new(const struct asahi_g17_signal_spec *specs,
size_t count, size_t anchor, int *error);
void asahi_g17_signals_free(struct asahi_g17_signal_bundle *bundle);
int asahi_g17_signals_publish(struct asahi_g17_signal_bundle *bundle,
int asahi_g17_signals_prepare(struct asahi_g17_signal_bundle *bundle,
struct dma_fence *submitted, bool compute_only);
void asahi_g17_signals_commit(struct asahi_g17_signal_bundle *bundle);
void asahi_g17_signals_fail(struct asahi_g17_signal_bundle *bundle,
struct dma_fence *failure);
#ifdef CONFIG_KUNIT
Expand Down
25 changes: 21 additions & 4 deletions drivers/gpu/drm/asahi/g17/sync_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ unsafe extern "C" {
fn asahi_g17_signals_new(syncs: *const SignalSpec, count: usize, anchor: usize,
error: *mut i32) -> *mut core::ffi::c_void;
fn asahi_g17_signals_free(bundle: *mut core::ffi::c_void);
fn asahi_g17_signals_publish(bundle: *mut core::ffi::c_void,
fn asahi_g17_signals_prepare(bundle: *mut core::ffi::c_void,
current: *mut bindings::dma_fence, compute_only: bool) -> i32;
fn asahi_g17_signals_commit(bundle: *mut core::ffi::c_void);
fn asahi_g17_signals_fail(bundle: *mut core::ffi::c_void,
failure: *mut bindings::dma_fence);
}
Expand Down Expand Up @@ -74,13 +75,29 @@ impl SignalBundle {
Ok(Self(raw))
}

pub(crate) fn publish(&mut self, current: &Fence, compute_only: bool) -> Result {
/// Allocates what publishing `current` needs, storing nothing in an output yet.
pub(crate) fn prepare(&mut self, current: &Fence, compute_only: bool) -> Result {
// SAFETY: The bundle and current fence are owned for this call; C takes
// its own references on publication and reports failure before any store.
let error = unsafe { asahi_g17_signals_publish(self.0, current.raw(), compute_only) };
// its own references and reports failure before any store.
let error = unsafe { asahi_g17_signals_prepare(self.0, current.raw(), compute_only) };
if error == 0 { Ok(()) } else { Err(Error::from_errno(error)) }
}

/// [`Self::prepare`] and [`Self::commit`] at once: fails before any store. G16 publishes
/// this way; G17 commits only once its commands are queued.
#[cfg(CONFIG_DRM_ASAHI_G16)]
pub(crate) fn publish(&mut self, current: &Fence, compute_only: bool) -> Result {
self.prepare(current, compute_only)?;
self.commit();
Ok(())
}

/// Publishes what [`Self::prepare`] allocated in every output.
pub(crate) fn commit(&mut self) {
// SAFETY: The bundle is owned; C stores only what prepare allocated.
unsafe { asahi_g17_signals_commit(self.0) };
}

pub(crate) fn fail(&mut self, failure: &Fence) {
// SAFETY: Live owned arguments; this only consumes uninstalled chain nodes.
unsafe { asahi_g17_signals_fail(self.0, failure.raw()) };
Expand Down
14 changes: 11 additions & 3 deletions drivers/gpu/drm/asahi/g17/sync_scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ struct dma_fence *asahi_g17_scope_create(struct dma_fence *previous,
struct dma_fence *parent = NULL;
struct asahi_g17_scope *scope, *prior = NULL;
u64 context;
unsigned int i;

if (asahi_g17_is_scope(contained)) {
prior = to_scope(contained);
Expand Down Expand Up @@ -380,7 +379,17 @@ struct dma_fence *asahi_g17_scope_create(struct dma_fence *previous,
/* The constructor guard prevents completion while callbacks are being installed. */
atomic_set(&scope->pending, parent ? 3 : 2);
scope->base.error = 1;
for (i = parent ? 0 : 1; i < ARRAY_SIZE(scope->children); ++i) {
return &scope->base;
}

/* The bundle owns the only reference until commit. */
void asahi_g17_scope_arm(struct dma_fence *fence)
{
struct asahi_g17_scope *scope = to_scope(fence);
unsigned int i;

/* children[0] is NULL without a previous point, or when it already succeeded. */
for (i = scope->children[0] ? 0 : 1; i < ARRAY_SIZE(scope->children); ++i) {
struct dma_fence *child = scope->children[i];
int err;

Expand All @@ -398,5 +407,4 @@ struct dma_fence *asahi_g17_scope_create(struct dma_fence *previous,
}
}
scope_arrived(scope, 0);
return &scope->base;
}
1 change: 1 addition & 0 deletions drivers/gpu/drm/asahi/g17/sync_scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ int asahi_g17_completion_fence_status(struct dma_fence *fence);
struct dma_fence *asahi_g17_scope_create(struct dma_fence *previous,
struct dma_fence *submitted, u64 point,
bool compute_only);
void asahi_g17_scope_arm(struct dma_fence *scope);
void asahi_g17_scope_bind(struct dma_fence *scope, struct dma_fence *root);
bool asahi_g17_is_scope(struct dma_fence *fence);
/* Returns owned child references, or zero once the scope is signalled. */
Expand Down
Loading
Loading