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
46 changes: 29 additions & 17 deletions drivers/gpu/drm/asahi/g17/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,35 +871,42 @@ impl Completion {
if let Some(sources) = &self.sources {
sources.release();
}
drop(self.render.lock().take());
if !self.is_compute {
drop(self.render.lock().take());
}
drop(self.vm_job.lock().take());
}

/// Exact retirement permits the physical lease and VM pin to be released
/// before waking userspace. A failure fence alone never establishes this.
/// Returns true if this call claimed signalling and released healthy pins.
#[track_caller]
pub(crate) fn complete(&self, result: Result<[u64; 4]>) {
pub(crate) fn complete(&self, result: Result<[u64; 4]>) -> bool {
// M5 compute feeds the refill detector only from its exact execution record
// (RetiredCompute); M5 compute is the compute without pooled timestamps.
let execution = if self.is_compute && self.compute.is_none() {
None
} else {
result.as_ref().ok().map(|ticks| [ticks[0], ticks[1]])
};
self.complete_with_compute(result, execution);
self.complete_with_compute(result, execution)
}

/// Compute feedback is separate from public timestamps, which may contain
/// a synthesized retirement envelope when the execution record is absent.
#[track_caller]
fn complete_with_compute(&self, mut result: Result<[u64; 4]>, compute: Option<[u64; 2]>) {
fn complete_with_compute(
&self,
mut result: Result<[u64; 4]>,
compute: Option<[u64; 2]>,
) -> bool {
if result.is_ok() {
if let Err(error) = self.check_source_inputs() {
result = Err(error);
}
}
if self.signalled.swap(true, Ordering::AcqRel) {
return;
return false;
}
if result.is_ok() && self.interrupted() {
result = Err(ECANCELED);
Expand All @@ -914,6 +921,15 @@ impl Completion {
self.usage.add(Engine::Fragment, ticks[2], ticks[3], hz);
}
}
if let Err(error) = result {
// Spared work cancelled after another VM's recovery is not this client's device loss.
self.report(error);
}
self.release_healthy();
self.signal_terminal(result);
self.output.finish(result);
// Feed detector and busy-run tracking are internal host heuristics: update them
// after signalling the fence so their spinlocks stay off the wakeup critical path.
if let (Some(feed), Ok(ticks)) = (&self.feed, &result) {
// Compute reports its dispatch envelope in the first pair, a
// render its fragment pass in the second.
Expand All @@ -929,13 +945,7 @@ impl Completion {
feed.note_work(self.context_id, start.unwrap_or(0), ticks[1].max(ticks[3]));
}
}
if let Err(error) = result {
// Spared work cancelled after another VM's recovery is not this client's device loss.
self.report(error);
}
self.release_healthy();
self.signal_terminal(result);
self.output.finish(result);
true
}

/// A rejected, never-published command cannot poison the VM for contention.
Expand Down Expand Up @@ -999,18 +1009,20 @@ impl Deferred {
pub(crate) fn finish(self) {
match self {
Self::RetiredCompute(completion, ticks, execution) => {
completion.complete_with_compute(Ok(ticks), execution);
completion.release_after_retirement();
if !completion.complete_with_compute(Ok(ticks), execution) {
completion.release_after_retirement();
}
}
Self::Retired(completion, result) => {
completion.complete(result);
// An earlier failure may already have signalled this fence while
// retaining the pin. Retirement is a separate release witness.
completion.release_after_retirement();
if !completion.complete(result) {
completion.release_after_retirement();
}
}
Self::Drained(completion, result) => {
completion.release_after_retirement();
completion.complete(result);
let _ = completion.complete(result);
}
Self::FailedOwned(completion, error) => completion.fail_while_owned(error),
Self::Unpublished(completion, error) => completion.fail_unpublished(error),
Expand Down
47 changes: 45 additions & 2 deletions drivers/gpu/drm/asahi/g17/queue/compute/retirement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,14 @@ pub(crate) struct Batch {
/// ([`NO_FRONTIER`] for a QID without a staged queue). A record at or past it belongs
/// to a command published during the scan, which a later batch stages.
frontiers: [u64; QIDS],
/// Bitmasks of QIDs touched in `slots` and `frontiers` so `clear()` touches only active QIDs.
touched_slots: u128,
touched_frontiers: u128,
}

const NO_FRONTIER: u64 = u64::MAX;
const KICK_MASK: u64 = (1 << 40) - 1;
const _: () = assert!(QIDS <= 128);

impl Batch {
pub(crate) fn new() -> Result<Self> {
Expand All @@ -311,19 +315,34 @@ impl Batch {
unobserved: 0,
covered: 0,
frontiers: [NO_FRONTIER; QIDS],
touched_slots: 0,
touched_frontiers: 0,
})
}
pub(crate) fn clear(&mut self) {
self.requests.clear();
self.slots.fill((0, 0));
let mut slots = self.touched_slots;
while slots != 0 {
let qid = slots.trailing_zeros() as usize;
slots &= slots - 1;
self.slots[qid] = (0, 0);
}
self.touched_slots = 0;
self.open = None;
self.unobserved = 0;
self.covered = 0;
self.frontiers.fill(NO_FRONTIER);
let mut frontiers = self.touched_frontiers;
while frontiers != 0 {
let qid = frontiers.trailing_zeros() as usize;
frontiers &= frontiers - 1;
self.frontiers[qid] = NO_FRONTIER;
}
self.touched_frontiers = 0;
}
/// Records the first kick `qid`'s queue had not yet published when this batch was staged.
pub(crate) fn stage_frontier(&mut self, qid: u8, next: u64) -> Result {
*self.frontiers.get_mut(usize::from(qid)).ok_or(EINVAL)? = next & KICK_MASK;
self.touched_frontiers |= 1u128 << qid;
Ok(())
}
/// A record of `qid` with `kick` names a command published after this batch was staged.
Expand Down Expand Up @@ -372,6 +391,7 @@ impl Batch {
.map_err(|_| ENOSPC)?;
*self.heads.get_mut(bucket).ok_or(EIO)? = index;
*slot = (start, slot.1 + 1);
self.touched_slots |= 1u128 << ticket.qid;
self.open = Some(ticket.qid);
self.unobserved += 1;
self.covered += usize::from(covered);
Expand Down Expand Up @@ -813,4 +833,27 @@ mod index_tests {
assert!(batch.fold_reader(|word| Ok(other[word])).unwrap());
assert!(!batch.complete() && !batch.covered_pending());
}
#[test]
fn clear_resets_touched_slots_and_frontiers_across_batches() {
let mut batch = Batch::new().unwrap();
let low = ticket(0, 11, 0x1000);
let high = ticket((QIDS - 1) as u8, 22, 0x2000);
batch.stage_frontier(0, 12).unwrap();
batch.stage_frontier((QIDS - 1) as u8, 23).unwrap();
batch.request(&low, true).unwrap();
batch.request(&high, false).unwrap();
batch.clear();
assert!(batch.complete() && !batch.covered_pending());
assert_eq!(batch.touched_slots, 0);
assert_eq!(batch.touched_frontiers, 0);
assert!(!batch.published_after_staging(0, 12));
assert!(!batch.published_after_staging(QIDS - 1, 23));
// Re-staging the same QIDs with new kicks succeeds cleanly after clear().
let next = ticket((QIDS - 1) as u8, 25, 0x3000);
batch.request(&next, true).unwrap();
let words = record(&next, [50, 60]);
assert!(batch.fold_reader(|word| Ok(words[word])).unwrap());
assert!(batch.complete());
assert_eq!(batch.take(&next).unwrap().map(|obs| obs.timestamps), Some([50, 60]));
}
}
11 changes: 7 additions & 4 deletions tools/asahi/g17-quarantine-drain-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def fixture():
'pair.retire(None, generation, state, host,', 'pair.fail_after_observation(packet, error, host,')
# A first signal gives the lease back without ending a quarantine (the `complete` mock).
ordered(item(completion, 'fn complete_with_compute('),
'if self.signalled.swap(true, Ordering::AcqRel) {\n return;\n }',
'if self.signalled.swap(true, Ordering::AcqRel) {\n return false;\n }',
'self.release_healthy();')
# The drain release runs once the drain proof, event service and accounting passed.
ordered(item(settle, 'fn drain_render_slot('), 'pair.firmware_drained(host)', 'self.service_runtime_events(deferred)',
Expand Down Expand Up @@ -178,6 +178,7 @@ def fixture():
sources: Option<Arc<Sources>>,
vm_job: Mutex<Option<VmJobGuard>>,
render: Mutex<Option<RenderLease>>,
is_compute: bool,
signalled: AtomicBool,
accepted: AtomicBool,
spared: AtomicBool,
Expand All @@ -191,13 +192,14 @@ def fixture():
fn report(&self, _: Error) {}
fn signal_terminal(&self, result: Result<[u64; 4]>) { *self.result.lock() = Some(result); }
/// The lease handling of the real complete_with_compute (checked against it above).
fn complete(&self, result: Result<[u64; 4]>) { self.complete_with_compute(result, None) }
fn complete_with_compute(&self, result: Result<[u64; 4]>, _: Option<[u64; 2]>) {
fn complete(&self, result: Result<[u64; 4]>) -> bool { self.complete_with_compute(result, None) }
fn complete_with_compute(&self, result: Result<[u64; 4]>, _: Option<[u64; 2]>) -> bool {
if self.signalled.swap(true, Ordering::AcqRel) {
return;
return false;
}
self.release_healthy();
self.signal_terminal(result);
true
}
''' + command + '\n}\n' + item(completion, 'pub(crate) enum Deferred {') + '\n' + item(completion, 'impl Deferred {') + r'''

Expand Down Expand Up @@ -293,6 +295,7 @@ def fixture():
let completion = Arc::new(Completion {
status: Arc::new(Status), output: Output, sources: Some(Arc::new(Sources)),
vm_job: Mutex::new(Some(guard)), render: Mutex::new(Some(lease)),
is_compute: false,
signalled: AtomicBool::new(false), accepted: AtomicBool::new(true), spared: AtomicBool::new(false),
result: Mutex::new(None),
});
Expand Down
Loading