Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 22 additions & 23 deletions drivers/gpu/drm/asahi/g16/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2270,19 +2270,20 @@ impl Firmware {
Ok(waits)
}

/// Once per publication pass, before anything is posted. A compute UMA manager left bound
/// under the in-flight hold keeps its allocator cursors only in the hardware
/// (`Slots::bind_compute_uma`), so a GPU power-off drops them.
/// Once per publication pass, before anything is posted. The firmware saves a compute UMA
/// manager's allocator cursors into its record when it drains (`Slots::bind_compute_uma`).
/// A GPU power-off between its jobs still makes the host rebuild the record rather than rely
/// on that save.
///
/// Under the boot policy that holds the GPU powered (`Session::idle_power_off` false),
/// the GPU cannot lose a bound manager while idle: nothing is invalidated here, and only a
/// system sleep or a recovery invalidates. When the firmware may power the GPU off on its
/// own, there is no qualified witness of a power loss yet (such as a firmware power-loss
/// generation, or a power hold spanning this check through the firmware's consumption of
/// the next kick). So every pass that finds nothing in flight, after retiring what it can,
/// with a shared manager held, invalidates them all, and their next commands rebuild their
/// records. A flight in flight keeps the GPU busy, so a pass with work in flight
/// invalidates nothing, nor does one while no shared manager is held (render-only work).
/// Under the boot policy that holds the GPU powered (`Session::idle_power_off` false)
/// nothing is invalidated here, and only a system sleep or a recovery invalidates. When the
/// firmware may power the GPU off on its own, there is no qualified witness of a power loss
/// yet (such as a firmware power-loss generation, or a power hold spanning this check through
/// the firmware's consumption of the next kick). So every pass that finds nothing in flight,
/// after retiring what it can, with a shared manager used since the last invalidation,
/// invalidates them all, and their next commands rebuild their records. A flight in flight
/// keeps the GPU busy, so a pass with work in flight invalidates nothing, nor does one
/// without shared compute (render-only work).
fn note_gpu_idle_if_any(&mut self) {
if !self.session.idle_power_off()
|| !self.engine.flights.is_empty()
Expand Down Expand Up @@ -3559,15 +3560,15 @@ impl Firmware {
if f.claims.slot_count != 0 {
let tail_sub = f.csubmitted[(f.claims.slot_count - 1) as usize];
shared.submitted.store(tail_sub, Ordering::Release);
// The in-flight hold keeps the manager bound between its jobs: *counter
// stays one past the submissions, so the firmware does not save and
// unbind the manager when this command finishes, and its next job finds
// the cursors still in the hardware (`Slots::bind_compute_uma`).
// `retire_oldest` writes the counter back once the manager drained, so
// an idle manager's counter equals COMPLETED, as in the macOS 26B123
// procs-d8 snapshot (all eight managers). `prefix_compute` reserved the
// count.
shared.counter.w64(0, tail_sub.saturating_add(1))?;
// *counter is the submission count. A later queued command keeps the
// manager bound; its last command completes with COMPLETED equal to
// *counter, so the firmware saves and unbinds it. Never hold the counter
// past the submissions: that leaves a drained manager bound after its VM
// is idle or gone, and the slot's next user (a private SCS unit, or
// another VM) then displaces it without a save (linux#3707). An idle
// manager's counter equals COMPLETED on macOS 26B123 too (procs-d8
// snapshot, all eight managers).
shared.counter.w64(0, tail_sub)?;
}
} else {
for m in 0..f.claims.slot_count as usize {
Expand Down Expand Up @@ -3803,8 +3804,6 @@ impl Firmware {
let base = shared.submitted.load(Ordering::Acquire);
pf.uma_delta = pf.uma_delta.checked_add(1).ok_or(EOVERFLOW)?;
let submitted = base.checked_add(pf.uma_delta).ok_or(EOVERFLOW)?;
// Room for the in-flight hold on top of this submission (publication below).
submitted.checked_add(1).ok_or(EOVERFLOW)?;
(
base,
submitted,
Expand Down
31 changes: 4 additions & 27 deletions drivers/gpu/drm/asahi/g16/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1662,35 +1662,12 @@ impl Firmware {
}
if f.computes != 0 {
// UMA residency: this manager's commands (or this flight's private units) are
// done. A drained manager drops `*counter` from the in-flight hold to its
// submissions, so an idle manager's counter equals COMPLETED; it stays bound
// until another record or descriptor takes the slot (`Slots::bind_compute_uma`).
let drained = f.job.resources.shared_compute_uma().is_some_and(|shared| {
// done. The firmware saves and unbinds a manager with its last command.
if let Some(shared) = f.job.resources.shared_compute_uma() {
self.slots
.retire_compute_uma(&timestamp_claims, shared, f.computes)
});
self.slots.retire_private_uma(&timestamp_claims);
let released = f
.job
.resources
.shared_compute_uma()
.filter(|_| drained)
.map(|shared| {
shared
.counter
.w64(0, shared.submitted.load(Ordering::Acquire))
});
if let Some(Err(error)) = released {
self.retain(&f.job);
complete(done, &f.job, Err(error));
for j2 in f.group.iter() {
self.retain(j2);
complete(done, j2, Err(error));
}
self.fail_stop(error, done);
core::mem::forget(f);
return;
.retire_compute_uma(&timestamp_claims, shared, f.computes);
}
self.slots.retire_private_uma(&timestamp_claims);
let p = (f.compute_pipe as usize).min(super::hw::sizes::COMPUTE_PIPES - 1);
for &stamp in &f.cstamp[..(f.computes as usize).min(f.cstamp.len())] {
self.recovery.last_retired.push(p, stamp);
Expand Down
10 changes: 4 additions & 6 deletions drivers/gpu/drm/asahi/g16/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,19 +946,17 @@ impl SharedComputeUmaInner {
self.outstanding.fetch_add(count, Ordering::AcqRel);
}

/// `count` commands retired cleanly; returns whether that drained the manager.
pub(crate) fn note_retired(&self, count: u32) -> bool {
/// `count` commands retired cleanly.
pub(crate) fn note_retired(&self, count: u32) {
let left = self.outstanding().saturating_sub(count);
self.outstanding.store(left, Ordering::Release);
left == 0
}

/// Initialises slot record `rec` for this manager, which has no command in flight: the
/// manager image with the allocator cursors at their seed and COMPLETED at the host's
/// submission count, so the firmware's next completion lines up with the next submission.
/// The firmware never writes a displaced or lost binding back (it saves a manager only at a
/// completion with COMPLETED equal to `*counter`, which the in-flight hold prevents), so the
/// hardware cursors of a manager that ran before are gone. Its pool is drained, and cursors
/// The cursors the firmware saved into a record another manager displaced since are not
/// host-readable, so they are not carried over. The pool is drained, and cursors
/// at their seed describe its freelist exactly while the freelist still holds the seed
/// ([`super::slots::uma_seed_intact`]) and the slot ledger's seed generation `seed_gen`
/// has not advanced since the last seed or check. Otherwise seed the freelist afresh with
Expand Down
54 changes: 24 additions & 30 deletions drivers/gpu/drm/asahi/g16/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,8 @@ enum UmaBound {
Private,
}

/// The shared manager bound in a slot. Its commands run under the in-flight hold, so the
/// firmware never saves it: its allocator cursors are only in the hardware, never in its record.
/// The shared manager that last ran in a slot: its record holds its allocator cursors, saved when
/// its last command completed, and nothing replaced it in the slot since.
#[derive(Copy, Clone)]
struct UmaLive {
id: u64,
Expand Down Expand Up @@ -894,17 +894,17 @@ pub(crate) struct ComputeOwners {
/// `uma_mgr_table[COMPUTE_UMA_SLOT + slot]`.
retained_uma: [Option<Arc<FwLease>>; sizes::COMPUTE_UMA_SLOTS],
/// Compute UMA residency per hardware slot ([`Slots::bind_compute_uma`]): what the firmware
/// slot table names, the shared manager bound there with its cursors in the hardware, and
/// the shared and private (SCS) commands in flight there. Only identities and counts are
/// kept: no VM storage stays alive for the ledger.
/// slot table named last, the shared manager that ran there last, and the shared and private
/// (SCS) commands in flight there. Only identities and counts are kept: no VM storage stays
/// alive for the ledger.
uma_bound: [UmaBound; sizes::COMPUTE_UMA_SLOTS],
uma_live: [Option<UmaLive>; sizes::COMPUTE_UMA_SLOTS],
uma_shared_flights: [u32; sizes::COMPUTE_UMA_SLOTS],
uma_private_flights: [u32; sizes::COMPUTE_UMA_SLOTS],
/// Advanced whenever the hardware allocator state of bound managers may be gone
/// Advanced whenever the saved cursors of shared managers are no longer used
/// ([`Slots::invalidate_compute_uma`]).
uma_live_gen: u64,
/// A shared manager was bound since the last advance of [`Self::uma_live_gen`].
/// A shared manager ran since the last advance of [`Self::uma_live_gen`].
uma_held: bool,
/// Advanced whenever no drained shared freelist is trusted to hold its seed, whatever it
/// shows ([`super::resources::SharedComputeUmaInner::init_record`]): after a system sleep
Expand Down Expand Up @@ -2069,17 +2069,16 @@ impl Slots {
/// retirement on the slot repolls publication). [`Self::note_compute_uma_published`]
/// records the use once it is visible.
///
/// Every shared command is published under the in-flight hold, so the firmware never saves a
/// shared manager: while it is bound its allocator cursors are only in the hardware, and
/// binding another record or descriptor in the slot drops them. So, in order:
/// - The manager is still bound in this residency generation (its own commands are in
/// flight, or it drained and nothing since could have dropped the hardware state: see
/// The firmware saves and unbinds a shared manager when its last command completes, so a
/// slot whose commands all retired holds no binding: its next user binds afresh. So, in order:
/// - The manager's record is current in this residency generation (its own commands are in
/// flight, or it drained and nothing since displaced or invalidated it: see
/// [`Self::invalidate_compute_uma`]): keep its record and write nothing.
/// - Commands of another user of the slot (a private SCS unit, or this manager while its
/// binding is uncertain) are in flight: wait. Another VM's manager never runs in the slot
/// at the same time (the engine's UMA conflict check).
/// - Otherwise its cursors are gone (displaced, or lost over an idle power-down, a sleep or
/// a recovery): rebuild a record the slot table does not name
/// - Otherwise its saved cursors are not used (displaced, or an idle power-down, a sleep or
/// a recovery since): rebuild a record the slot table did not name last
/// ([`super::resources::SharedComputeUmaInner::init_record`] restarts the cursors and
/// COMPLETED together, and reseeds the drained freelist only if it may no longer hold
/// its seed).
Expand Down Expand Up @@ -2145,7 +2144,7 @@ impl Slots {
}

/// Records a visible publication of private (SCS) commands in the slots `f.uma_private`:
/// each binds its own descriptor there, displacing the shared manager bound there.
/// each binds its own descriptor there, after the shared manager there was saved.
pub(crate) fn note_private_uma_published(&mut self, f: &Claims) {
let c = &mut self.compute;
let mut mask = f.uma_private & COMPUTE_UMA_MASK;
Expand All @@ -2158,26 +2157,23 @@ impl Slots {
}
}

/// Records the clean retirement of a flight whose `count` commands used `shared`. Returns
/// whether that drained the manager, so its counter has to drop from the in-flight hold to
/// its submission count. The manager stays bound, with its cursors in the hardware, until
/// another record or descriptor takes the slot. Aborted flights never come here: their slot
/// stays quarantined with the manager and their commands stay counted for good, so
/// `publish_prefix_body` fails a job naming a quarantined slot before it waits on these
/// counts, and no shared or private command publishes there again.
/// Records the clean retirement of a flight whose `count` commands used `shared`. Aborted
/// flights never come here: their slot stays quarantined with the manager and their commands
/// stay counted for good, so `publish_prefix_body` fails a job naming a quarantined slot
/// before it waits on these counts, and no shared or private command publishes there again.
pub(crate) fn retire_compute_uma(
&mut self,
f: &Claims,
shared: &super::resources::SharedComputeUma,
count: u32,
) -> bool {
) {
let (slot, rec) = (f.uma_slot as usize, f.uma_rec as usize);
if rec >= UMA_RECORDS || slot >= sizes::COMPUTE_UMA_SLOTS || slot != shared.uma_slot_idx {
return false;
return;
}
let c = &mut self.compute;
c.uma_shared_flights[slot] = c.uma_shared_flights[slot].saturating_sub(count);
shared.note_retired(count)
shared.note_retired(count);
}

/// Records the clean retirement of a flight with private (SCS) commands in the slots
Expand All @@ -2196,9 +2192,8 @@ impl Slots {
}
}

/// The hardware allocator state of bound shared managers may be gone: the GPU may have
/// powered down while idle. Trust none of them: each rebuilds a record the slot table does
/// not name once its own commands have retired.
/// The GPU may have powered down while idle. Trust no shared manager's saved cursors: each
/// rebuilds a record the slot table did not name last once its own commands have retired.
pub(crate) fn invalidate_compute_uma(&mut self) {
let c = &mut self.compute;
c.uma_live_gen = c.uma_live_gen.wrapping_add(1);
Expand All @@ -2218,8 +2213,7 @@ impl Slots {
self.compute.uma_seed_gen = self.compute.uma_seed_gen.wrapping_add(1);
}

/// Whether a shared manager may be bound with its cursors only in the hardware: one was
/// bound since the last [`Self::invalidate_compute_uma`].
/// Whether a shared manager ran since the last [`Self::invalidate_compute_uma`].
pub(crate) fn compute_uma_held(&self) -> bool {
self.compute.uma_held
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,14 @@ def item(declaration):
), 'the final boot control must follow the power policy the idle check reads'
note_gpu_idle_fn = item('fn note_gpu_idle_if_any(')
assert 'self.session.idle_power_off()' in note_gpu_idle_fn, note_gpu_idle_fn
# A shared manager runs under the in-flight hold, and no seeded freelist is trusted past a firmware
# restart or a recovery: both callers come before the firmware can run work again.
assert publish_prefix_body_fn.count('shared.counter.w64(0, tail_sub.saturating_add(1))?;') == 1
# A shared manager's counter never runs past its submissions: its last command has to complete with
# COMPLETED equal to it, so that the firmware unbinds the manager before its slot changes hands
# (linux#3707). Retirement writes no counter.
assert publish_prefix_body_fn.count('shared.counter.w64(0, ') == 1
assert publish_prefix_body_fn.count('shared.counter.w64(0, tail_sub)?;') == 1
assert '.counter' not in item_in((production.parent / 'poll.rs').read_text(), 'fn retire_oldest(')
# No seeded freelist is trusted past a firmware restart or a recovery: both callers come before the
# firmware can run work again.
sleep_restart_fn = item_in((production.parent / 'sleep.rs').read_text(), 'fn sleep_restart(')
assert (
sleep_restart_fn.index('self.slots.restart_compute_uma();')
Expand Down
Loading
Loading