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
20 changes: 11 additions & 9 deletions drivers/gpu/drm/asahi/g16/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ pub(crate) enum HoldCause {
DrainFailed,
/// A cold `InitBM` render is in flight.
DrainInitbm,
/// Compute and render of different VMs, or an SCS compute conflict.
/// Compute behind any flight of another VM, render behind compute of another VM, or an
/// SCS compute conflict.
DrainCross,
/// No warm PB/UMA allocator slot of the VM can take the render behind active flights.
DrainPb,
Expand Down Expand Up @@ -1401,7 +1402,7 @@ impl Firmware {
|| f.recovery_suspect
|| f.initbm
|| (is_compute && scs_compute_conflict(f, &job.vm, is_rt_compute))
|| (((is_compute && f.renders != 0) || (is_render && f.computes != 0))
|| ((is_compute || (is_render && f.computes != 0))
&& !Arc::ptr_eq(&f.job.vm, &job.vm))
})
|| (is_render && !self.render_can_pipeline(job))
Expand Down Expand Up @@ -1589,8 +1590,8 @@ impl Firmware {

/// Whether `job`'s next command may be published behind the flights in flight:
///
/// - Compute queues behind healthy non-InitBM compute flights of any VM or same-VM
/// render flights (up to [`params::compute_flights`] active computes), each in its own
/// - Compute queues behind healthy non-InitBM compute and render flights of its own VM
/// (up to [`params::compute_flights`] active computes), each in its own
/// `ComputeSlot` and hardware UMA slot (`COMPUTE_UMA_SLOT + index`), unless `query_access`
/// is set and a same-queue predecessor in its compute or render barrier prefix has
/// CPU-published timestamp outputs that have not retired yet
Expand All @@ -1606,6 +1607,10 @@ impl Firmware {
/// dispatch from one client cannot starve another client's fragment shading into a
/// firmware DM1 timeout. Cold InitBM priming renders and every failure or recovery path
/// still drain the engine first.
/// - Compute never runs beside compute of another VM, on either pipe: with several clients
/// dispatching barrier-free compute, the overlapped dispatches occasionally lost their
/// results without a fault (loads read zero, readbacks came back empty or partial),
/// which one client alone never does. G15 runs one VM at a time for the same reason.
fn can_queue_behind(&self, job: &Arc<Job>) -> bool {
let mut ta_pending = None;
self.can_queue_behind_with_ta(job, &mut ta_pending)
Expand Down Expand Up @@ -1672,17 +1677,14 @@ impl Firmware {
})
})
}));
let cross_vm_render = e
.flights
.iter()
.any(|f| f.renders != 0 && !Arc::ptr_eq(&f.job.vm, &job.vm));
let cross_vm = e.flights.iter().any(|f| !Arc::ptr_eq(&f.job.vm, &job.vm));
let scs = job.resources.compute_scs() || c.is_rt();
let scs_conflict = e
.flights
.iter()
.any(|f| scs_compute_conflict(f, &job.vm, scs));
let compute_count: usize = e.flights.iter().map(|f| f.computes as usize).sum();
!cross_vm_render
!cross_vm
&& !scs_conflict
&& !waits_for_timestamps
&& compute_count < params::compute_flights()
Expand Down
5 changes: 3 additions & 2 deletions drivers/gpu/drm/asahi/g16/hw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,8 +831,9 @@ pub(crate) mod sizes {
pub(crate) const PB_BLOCKS: u32 = 2048;
pub(crate) const PB_LIST_RECORDS: u32 = 4096;
pub(crate) const PB_GROW_STEP: u32 = 10;
/// Published prefixes at most. Warm compute and render commands may pipeline across VMs
/// on disjoint hardware UMA slots while cold `InitBM` renders and recovery paths drain.
/// Published prefixes at most. Warm render commands may pipeline across VMs on disjoint
/// hardware UMA slots, compute only behind its own VM's work, while cold `InitBM` renders
/// and recovery paths drain.
pub(crate) const FLIGHTS: usize = 32;
/// Retained compute units per VM at most; each unit owns its own hardware UMA slot
/// (`COMPUTE_UMA_SLOT + index`, fitting in `1..RENDER_UMA_SLOT = 33`). A unit is reused
Expand Down
13 changes: 9 additions & 4 deletions drivers/gpu/drm/asahi/g16/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ pub(crate) struct Shared {
/// Set by external state changes (VM teardown, unstarted queue cancellation) so the next
/// `poll_step()` re-evaluates `publish_checked()` even while unrelated flights are active.
pub(crate) publish_wake: AtomicBool,
/// Set on VM close (and re-armed while any non-quarantined slot still holds a closed VM)
/// so displaced closed-VM slot leases are reclaimed without scanning on the hot path.
/// Set on VM close and when its last unpublished render reservation is released.
/// Re-armed while a closed VM still holds a lease awaiting reclaim.
pub(crate) closed_vm_reclaim: AtomicBool,
/// CPU that last submitted work, for placing the per-CPU completion worker.
pub(crate) submitter: AtomicU32,
Expand Down Expand Up @@ -507,6 +507,12 @@ impl Shared {
}
}

/// Arms and wakes the poll worker to reclaim closed VM leases.
pub(crate) fn wake_closed_vm_reclaim(self: &Arc<Self>) {
self.closed_vm_reclaim.store(true, Ordering::Release);
self.wake_publish();
}

/// Latches the fail-stop state.
pub(crate) fn fail_stop(&self, error: Error) {
self.feed.begin_shutdown();
Expand Down Expand Up @@ -904,8 +910,7 @@ impl gpu::Gpu for Gpu {
}
fn vm_closed(&self, vm: &mmu::Vm) {
self.vms.close(vm.id());
self.shared.closed_vm_reclaim.store(true, Ordering::Release);
self.shared.wake_publish();
self.shared.wake_closed_vm_reclaim();
}

fn new_queue(
Expand Down
17 changes: 9 additions & 8 deletions drivers/gpu/drm/asahi/g16/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,10 @@ impl Firmware {
let Some(shared) = self.engine.shared.as_ref() else {
return;
};
if !shared.closed_vm_reclaim.load(Ordering::Acquire)
|| !shared.closed_vm_reclaim.swap(false, Ordering::AcqRel)
{
let armed = shared.closed_vm_reclaim.load(Ordering::Acquire)
&& shared.closed_vm_reclaim.swap(false, Ordering::AcqRel);
// A closed VM's render retired and recorded its last epoch after the VM was closed.
if !self.pools.take_closed_vm_last() && !armed {
return;
}
let remaining = self.pools.reclaim_closed() | self.slots.reclaim_closed();
Expand Down Expand Up @@ -740,11 +741,10 @@ impl Firmware {
self.reclaim_closed_vm_slots();
}
// Skip redundant `publish_checked()` walks on busy polls where nothing could have
// unblocked `engine.pending`. `Prepared::drop` releases standby PB epochs only for
// `Cache::prepare` waiters (`standby_available`, which enter `submit_locked()` directly),
// while `publish_woke` covers external state changes (VM close, unstarted queue
// cancellation) and `ta_wave_ready()` wakes TA-wave-paced renders when a `0x70` TA
// pass finishes while FS is still running.
// unblocked `engine.pending`. Standby release wakes `Cache::prepare` waiters, which
// enter `submit_locked()` directly. `publish_woke` covers external state changes
// including a closed VM's final reservation release. `ta_wave_ready()` wakes
// TA-wave-paced renders when a `0x70` TA pass finishes while FS is still running.
let coalesce_due = self.engine.coalesce_deadline_ns != 0
&& (!evented || now >= self.engine.coalesce_deadline_ns);
let should_publish = !self.engine.pending.is_empty()
Expand Down Expand Up @@ -1686,6 +1686,7 @@ impl Firmware {
p.pending.store(false, Ordering::Relaxed);
self.engine.pending.retain(|j| !Arc::ptr_eq(j, &job));
p.count.store(p.sent(), Ordering::Relaxed);
job.resources.discard_unpublished_renders();
}
if receipt.owner_retired()
&& self.engine.flights.iter().all(|other| {
Expand Down
77 changes: 75 additions & 2 deletions drivers/gpu/drm/asahi/g16/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ pub(crate) struct PbStats {
/// render counts again when it is retried).
pub(crate) reused: u64,
pub(crate) fresh: u64,
/// Retirements that replaced an open VM's owner epoch still kept as that VM's last epoch.
pub(crate) kept: u64,
}

pub(crate) struct PoolSet {
Expand Down Expand Up @@ -501,7 +503,17 @@ pub(crate) struct Pools {
quarantined: [bool; sizes::PB_SLOTS],
/// Fresh PB epoch retained when an aborted render had not yet replaced `owners[k]`.
quarantined_epochs: [Option<Arc<PbEpoch>>; sizes::PB_SLOTS],
/// Per TTBAT slot, the epoch of the last render its VM retired, kept until a newer render
/// of the same VM retires. The parameter manager writes back to the page list of the
/// manager a context used last, through that context, when the context's next InitBM
/// runs, also after another VM's epoch replaced `owners[k]` on the old slot. An entry
/// holds its VM and so the TTBAT slot that indexes it.
vm_last: [Option<Arc<PbEpoch>>; VM_LAST_ENTRIES],
/// A closed VM's [`Self::vm_last`] entry was recorded since the last reclaim pass.
closed_vm_last: bool,
}
/// Entries of [`Pools::vm_last`]: one per TTBAT user slot (ASIDs `1..=MAX_VMS`).
const VM_LAST_ENTRIES: usize = sizes::MAX_VMS as usize + 1;
impl Pools {
#[allow(dead_code)]
pub(crate) fn new(dev: &AsahiDrmDevice) -> Self {
Expand All @@ -523,6 +535,8 @@ impl Pools {
inflight: [0; sizes::PB_SLOTS],
quarantined: [false; sizes::PB_SLOTS],
quarantined_epochs: [const { None }; sizes::PB_SLOTS],
vm_last: [const { None }; VM_LAST_ENTRIES],
closed_vm_last: false,
}
}
/// Allocates [`Pools`] directly on the heap so probe/backend bring-up does not stage the
Expand All @@ -549,6 +563,8 @@ impl Pools {
inflight: [0; sizes::PB_SLOTS],
quarantined: [false; sizes::PB_SLOTS],
quarantined_epochs: [const { None }; sizes::PB_SLOTS],
vm_last: [const { None }; VM_LAST_ENTRIES],
closed_vm_last: false,
}),
GFP_KERNEL,
)?)
Expand Down Expand Up @@ -962,6 +978,14 @@ impl Pools {
if self.quarantined[k] {
return Ok(());
}
if self.owners[k].as_ref().is_some_and(|old| {
!Arc::ptr_eq(&old._vm, &epoch._vm) && !old._vm.vm.is_closed() && self.is_vm_last(old)
}) {
self.stats.kept += 1;
}
if completed {
self.note_vm_last(epoch);
}
self.owners[k] = Some(epoch.clone());
self.retained_pb[k] = Some(epoch.records_lease());
if let Some(vm) = self.pending_vms[k].take() {
Expand All @@ -977,6 +1001,33 @@ impl Pools {
self.retire_epoch(k, binding, completed);
Ok(())
}
/// Whether `epoch` is the last retired epoch of its VM ([`Self::vm_last`]).
fn is_vm_last(&self, epoch: &Arc<PbEpoch>) -> bool {
self.vm_last
.get(epoch._vm.slot as usize)
.and_then(Option::as_ref)
.is_some_and(|last| Arc::ptr_eq(last, epoch))
}
/// A render of `epoch` retired normally: its VM's context has run that render's InitBM (or
/// reused the epoch), so the VM's earlier epoch is no longer written and may go. A closed
/// VM keeps an entry only until [`Self::reclaim_closed`] finds nothing of it pending.
fn note_vm_last(&mut self, epoch: &Arc<PbEpoch>) {
let vm = &epoch._vm;
let closed = vm.vm.is_closed();
let Some(entry) = self.vm_last.get_mut(vm.slot as usize) else {
return;
};
if entry.as_ref().is_some_and(|last| Arc::ptr_eq(last, epoch)) {
return;
}
*entry = Some(epoch.clone());
self.closed_vm_last |= closed;
}
/// Whether a closed VM's entry was recorded since the last call: [`Self::reclaim_closed`]
/// must run again to release it.
pub(crate) fn take_closed_vm_last(&mut self) -> bool {
core::mem::take(&mut self.closed_vm_last)
}
/// Quarantines allocator slot `k` permanently after a passed abort, retaining all leases
/// including any fresh epoch published on the aborted flight alongside `owners[k]`.
#[allow(dead_code)]
Expand Down Expand Up @@ -1042,6 +1093,7 @@ impl Pools {
/// The owner epoch itself stays until a newer epoch retires on its slot
/// ([`Self::retire_flight`]): `pb_mgr_table[k]` keeps naming its manager, and the parameter
/// manager can still write that manager's page list through the context of the epoch's VM.
/// Also releases a closed VM's [`Self::vm_last`] entry once nothing of that VM is pending.
pub(crate) fn reclaim_closed(&mut self) -> bool {
let mut remaining = false;
for k in 0..sizes::PB_SLOTS {
Expand Down Expand Up @@ -1076,6 +1128,26 @@ impl Pools {
remaining = true;
}
}
// A closed VM accepts no new render. Once none it accepted waits for publication or
// retirement, its context runs no further InitBM and its last epoch may go. A render
// left on a quarantined slot keeps its VM there anyway.
for i in 0..VM_LAST_ENTRIES {
let Some(vm) = self.vm_last[i].as_ref().map(|last| &last._vm) else {
continue;
};
if !vm.vm.is_closed() {
continue;
}
let pending = (0..sizes::PB_SLOTS).any(|k| {
!self.quarantined[k]
&& self.pending_vms[k].as_ref().is_some_and(|p| Arc::ptr_eq(p, vm))
});
if pending || vm.renders_unpublished() {
remaining = true;
} else {
self.vm_last[i] = None;
}
}
remaining
}
#[allow(dead_code)]
Expand All @@ -1087,12 +1159,13 @@ impl Pools {
pub(crate) fn write_stats(&self, f: &mut impl fmt::Write) -> fmt::Result {
writeln!(
f,
"pb: M4 private allocators blocks {} requests {} bad {} reused {} fresh {}",
"pb: M4 private allocators blocks {} requests {} bad {} reused {} fresh {} kept {}",
buffer::MAX_BLOCKS,
self.stats.requests,
self.stats.bad,
self.stats.reused,
self.stats.fresh
self.stats.fresh,
self.stats.kept
)
}
}
Expand Down
Loading