Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
15 changes: 15 additions & 0 deletions drivers/gpu/drm/asahi/g16/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ pub(crate) struct Session {
handoff: Option<regs::Mapping>,
/// True once cold boot (`START`) has completed and the firmware may idle-power-off the GPU.
booted: bool,
/// The boot power policy sent in [`run`] lets the firmware power the GPU off on its own
/// while idle; false while it holds the GPU powered.
idle_power_off: bool,
}

impl Session {
Expand Down Expand Up @@ -536,6 +539,13 @@ impl Session {
read(&self.regs).map(Some)
}

/// Whether the boot power policy lets the firmware power the GPU off on its own while
/// idle. False while the firmware holds the GPU powered, so it cannot cycle power between
/// commands.
pub(crate) fn idle_power_off(&self) -> bool {
self.idle_power_off
}

/// The engine receipts of a running firmware, or `None` while the GPU is powered down.
pub(crate) fn engine_state(&self) -> Result<Option<regs::EngineState>> {
self.while_powered(regs::Regs::engine_state)
Expand Down Expand Up @@ -904,6 +914,10 @@ fn run(
scheduler.w32(scheduler_word, 1)?;
}
regs::wmb();
// START with a zero argument asks the firmware to wake the GPU and hold it powered, so it
// never powers the GPU off on its own while idle. Held compute UMA managers rely on that
// (`Firmware::note_gpu_idle_if_any`): a policy that releases the hold must say so here.
session.idle_power_off = false;
devctl_op(session, channels, events, init, devctl.start)?;
wait_boot_control(session, channels, events, init, base + 2, 2000)?;
let receive_kick = if g15s_channels {
Expand Down Expand Up @@ -1095,6 +1109,7 @@ pub(crate) fn start(
uat,
handoff,
booted: false,
idle_power_off: false,
};
let mut events = Events::new();
let mut built = None;
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpu/drm/asahi/g16/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ use compute_encoder::put_generation;
#[allow(unused_imports)] // Pure encoder utilities are exercised by the host ABI harness.
pub(crate) use compute_encoder::{
encode_compute, encode_compute_for, encode_compute_rt, init_compute_pool,
preserve_compute_descriptor, validate_compute_descriptor, validate_shared_compute_descriptor,
ComputeEncodeError, ComputeRefs, ComputeRt, COMPUTE_SCS_DRIVER_WORD,
preserve_compute_descriptor, validate_compute_descriptor, ComputeEncodeError, ComputeRefs,
ComputeRt, COMPUTE_SCS_DRIVER_WORD,
};

/// Tiling geometry and register words of a render (accelerator count 2).
Expand Down
11 changes: 0 additions & 11 deletions drivers/gpu/drm/asahi/g16/encode/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,6 @@ pub(crate) fn validate_compute_descriptor(
validate_compute_descriptor_inner(r, old, submitted_before, true, r.work)
}

/// Validates a retained VM-shared compute UMA manager descriptor at an idle boundary before reuse.
/// Unlike a per-unit descriptor whose `WORK` header holds the unit's work image VA, a VM-shared
/// descriptor anchors `WORK` to its own `descriptor_fw` VA (matching render's shared UMA manager).
pub(crate) fn validate_shared_compute_descriptor(
r: &ComputeRefs,
old: &[u8; compute::DESCRIPTOR_SIZE],
submitted_before: u64,
) -> Result<u64, ComputeEncodeError> {
validate_compute_descriptor_inner(r, old, submitted_before, true, r.descriptor_fw)
}

/// Restores the complete allocator descriptor into a newly encoded, unpublished work after
/// the engine has proved A retired, distinct B displaced A in the same UMA slot, and B retired.
/// Numeric checks here do not establish that ownership proof. The old descriptor's completion
Expand Down
105 changes: 84 additions & 21 deletions drivers/gpu/drm/asahi/g16/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,9 @@ pub(crate) struct Stats {
pub(crate) behind: u64,
/// Most flights in flight at once.
pub(crate) max_flights: u64,
/// Publication passes that found the GPU quiescent with a compute UMA manager held while
/// the firmware may power the GPU off, and invalidated the held managers.
pub(crate) uma_idle_invalidations: u64,
}

/// Host-side phase timers of publication and polling, on with `debug_flags` bit 49
Expand Down Expand Up @@ -1095,6 +1098,9 @@ impl Firmware {
self.engine.held_wait = false;
self.engine.handoff_avoid_pipes = 0;
self.engine.coalesce_deadline_ns = 0;
if !self.engine.pending.is_empty() {
self.note_gpu_idle_if_any();
}
while self.recovery.failed().is_none()
&& !self.engine.frozen
&& !self.engine.pending.is_empty()
Expand Down Expand Up @@ -2120,6 +2126,31 @@ 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.
///
/// 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).
fn note_gpu_idle_if_any(&mut self) {
if !self.session.idle_power_off()
|| !self.engine.flights.is_empty()
|| !self.slots.compute_uma_held()
{
return;
}
self.slots.invalidate_compute_uma();
let s = &mut self.engine.stats;
s.uma_idle_invalidations = s.uma_idle_invalidations.saturating_add(1);
}

/// Whether `queue`'s recent or active compute work is heavy enough to benefit from 2-pipe
/// CDM fanout rather than 1-pipe affinity and horizon folding:
/// - per-queue retired CDM GPU duration EWMA `>= COMPUTE_HEAVY_EMA_US`, or
Expand Down Expand Up @@ -2659,6 +2690,15 @@ impl Firmware {
} {
return Err(EIO);
}
// An aborted flight never retires, so its commands stay counted in the compute UMA
// ledger of its quarantined slot, and the waits on that ledger in `prefix_compute`
// would never end. Fail before any of them.
if !render {
let uma_mask = job.resources.compute_uma_mask();
if self.slots.compute_uma_quarantined(uma_mask) {
return Err(EIO);
}
}
let channels: &[Channel] = if render {
&[Channel::Fs, Channel::Ta]
} else {
Expand Down Expand Up @@ -3291,9 +3331,6 @@ impl Firmware {
if self.recovery_pending()? {
return Err(EAGAIN);
}
if !render && self.slots.compute_uma_quarantined(f.compute_uma_mask) {
return Err(EIO);
}
if render {
f.pb.as_ref()
.ok_or(EINVAL)?
Expand Down Expand Up @@ -3337,7 +3374,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);
shared.counter.w64(0, tail_sub)?;
// 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))?;
}
} else {
for m in 0..f.claims.slot_count as usize {
Expand Down Expand Up @@ -3411,6 +3456,11 @@ impl Firmware {
})();
if publication.is_ok() {
self.slots.note_published(&f.claims);
if let Some(shared) = job.resources.shared_compute_uma() {
self.slots
.note_compute_uma_published(&f.claims, shared, f.computes);
}
self.slots.note_private_uma_published(&f.claims);
}
// From the first counter/WRITE onward an error is uncertain. Keep the complete
// retained flight even when a later channel store or doorbell fails.
Expand Down Expand Up @@ -3540,12 +3590,29 @@ impl Firmware {
uma_slot,
seeds,
) = if let Some(shared) = resources.shared_compute_uma() {
// The slot record this flight's commands name (`Slots::bind_compute_uma`, once per
// flight: every attempt starts from fresh claims); none yet: wait (a retirement on
// the slot repolls publication).
let rec = if f.claims.uma_rec != super::slots::NO_UMA_RECORD {
f.claims.uma_rec as usize
} else {
let Some(rec) = self.slots.bind_compute_uma(shared)? else {
self.engine.slot_wait = true;
return Err(EAGAIN);
};
rec
};
f.claims.uma_rec = rec as u8;
f.claims.uma_slot = shared.uma_slot_idx as u8;
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,
base.checked_add(pf.uma_delta).ok_or(EOVERFLOW)?,
shared.descriptor.va,
submitted,
shared.records[rec].va,
shared.counter.va,
shared.pool_state.gva.ok_or(EINVAL)?,
shared.page_list.gva.ok_or(EINVAL)?,
Expand All @@ -3554,6 +3621,13 @@ impl Firmware {
)
} else {
let u = s.private_uma()?;
// Its own descriptor replaces whatever the slot holds: never while a shared
// manager's commands run there.
if !self.slots.compute_uma_private_ready(u.uma_slot_idx()) {
self.engine.slot_wait = true;
return Err(EAGAIN);
}
f.claims.uma_private |= 1u32 << u.uma_slot_idx();
let b = u.counter.r64(0)?;
(
b,
Expand Down Expand Up @@ -3626,21 +3700,10 @@ impl Firmware {
let full_work = self.slots.compute_needs_full_work(index) || before == 0;
if full_work {
if before != 0 {
if let Some(shared) = resources.shared_compute_uma() {
// Validate the VM-shared UMA descriptor at idle boundaries (when no other
// flight of this VM is mutating `COMPLETED`).
if pf.uma_delta == 1 && before == shared.retired.load(Ordering::Acquire) {
let mut descriptor = [0u8; work::compute::DESCRIPTOR_SIZE];
shared.descriptor.read(0, &mut descriptor)?;
if encode::validate_shared_compute_descriptor(&refs, &descriptor, before)
.is_err()
{
self.slots.quarantine(&mut f.claims);
self.sync_quarantine_hints();
return Err(EIO);
}
}
} else {
// A VM-shared manager is a slot record the host initialised itself
// (`Slots::bind_compute_uma`) whose firmware-owned words are ASC-cached and not
// host-readable: only per-unit descriptors are validated in place.
if resources.shared_compute_uma().is_none() {
// Slots::claim_compute proved a different fully retired unit displaced this
// descriptor in the same hardware slot. Validate it in place.
let mut descriptor = [0u8; work::compute::DESCRIPTOR_SIZE];
Expand Down
9 changes: 9 additions & 0 deletions drivers/gpu/drm/asahi/g16/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,15 @@ impl Factory {
pub(crate) fn compute_cmds(&self, k: usize) -> Result<super::slots::UnitCmds> {
self.compute_cmds.get(self, k)
}

/// The device-lifetime UMA manager records of hardware compute UMA slot `k`, shared by every
/// VM whose shared compute UMA uses slot `k`.
pub(crate) fn compute_uma_records(
&self,
k: usize,
) -> Result<[Arc<FwObject>; super::slots::UMA_RECORDS]> {
self.compute_cmds.uma_records(self, k)
}
}

/// The firmware object allocator and the set of retained objects.
Expand Down
53 changes: 42 additions & 11 deletions drivers/gpu/drm/asahi/g16/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,16 +1304,10 @@ impl Firmware {
full &= count != 0 && count == f.computes as usize;
if full {
if f.job.resources.has_shared_compute_uma() {
let last_vm_flight = !self.engine.flights.iter().enumerate().any(|(idx, o)| {
idx != index && o.computes != 0 && Arc::ptr_eq(&o.job.vm, &f.job.vm)
});
if !f
.job
.resources
.compute_uma_retired(f.computes, last_vm_flight)?
{
full = false;
}
// The VM-shared manager record is ASC-cached: its COMPLETED word is not
// host-readable. The service stamp above, stored after the firmware's last
// access to the command's work, microsequence and manager, and the GPU's end
// tick below are the receipt.
if full {
for m in 0..count {
let k = f.claims.slot[m] as usize;
Expand Down Expand Up @@ -1563,7 +1557,36 @@ impl Firmware {
return;
}
if f.computes != 0 {
f.job.resources.note_compute_uma_retired(f.computes);
// 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| {
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;
}
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 Expand Up @@ -2230,6 +2253,14 @@ impl Firmware {
"resources: compute_cross_vm {} render_cross_vm {}",
slots.cross_vm_retargets[0], slots.cross_vm_retargets[1]
)?;
writeln!(
f,
"compute_uma: rebuilds {} invalidations {} idle_invalidations {} reseeds {}",
slots.uma_rebuilds,
slots.uma_invalidations,
s.uma_idle_invalidations,
slots.uma_reseeds
)?;
writeln!(
f,
"recovery_receipts: affected_full {} aborted {}",
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/asahi/g16/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ impl Firmware {
// writes the fault-bank selector so it must not run against active FW).
self.pools.invalidate();
self.slots.invalidate_scrub();
self.slots.restart_compute_uma();
self.events.grow.clear();
let stage = self.recovery_snapshot()?;
// Powered down, the GPU holds no fault to read.
Expand Down
Loading
Loading