diff --git a/drivers/gpu/drm/asahi/g14_v27_2.rs b/drivers/gpu/drm/asahi/g14_v27_2.rs index 68ee7faa50efc..5a59faa3be813 100644 --- a/drivers/gpu/drm/asahi/g14_v27_2.rs +++ b/drivers/gpu/drm/asahi/g14_v27_2.rs @@ -627,6 +627,11 @@ impl Runtime { let slot = (state.next_slot + k) % POOL_SLOTS; if state.busy & (1 << slot) != 0 { continue; } if state.pool.registered(slot)? { + // The firmware can count another record for a retired slot after its cursor + // was advanced, and keeps the slot listed until the cursor matches again: + // advance it to the current count. The slot's last command stays intact in + // its buffer, so an early unlink is harmless. + state.pool.retire(slot)?; state.skipped += 1; continue; } @@ -666,7 +671,16 @@ impl Runtime { Ok(Some(slots)) => slots, Ok(None) => return, Err(err) => { - dev_err!(self.dev, "GPU: command pool stalled: {:?}\n", err); + dev_err!(self.dev, "GPU: command pool stalled: {:?} busy={:#x} unacked={:#x} inflight={} rendering={}\n", + err, state.busy, state.unacked, state.inflight.len(), state.rendering.is_some()); + for slot in 0..POOL_SLOTS { + if let Ok((count, cursor, linked)) = state.pool.describe(slot) { + if linked != 0 || count != cursor { + dev_err!(self.dev, "GPU: slot {}: records={} cursor={} linked={}\n", + slot, count, cursor, linked); + } + } + } self.fail_all(state, err); return; } @@ -715,6 +729,7 @@ impl Runtime { publication_error: None, started: Instant::now(), _bind: bind }, GFP_KERNEL)?; packet.mark_published(); submission.publish(); + state.pool.mark_published(slot); state.last = stamp; state.sequence += 1; state.unacked &= !(1 << slot); @@ -775,6 +790,9 @@ impl Runtime { // Queue the fragment dependency before kicking the tiler that satisfies it. fs_post.publish(); ta_post.publish(); + for slot in slots { + state.pool.mark_published(slot); + } for slot in slots { state.busy |= 1 << slot; state.unacked &= !(1 << slot); diff --git a/drivers/gpu/drm/asahi/g14_v27_2_abi.rs b/drivers/gpu/drm/asahi/g14_v27_2_abi.rs index 313607d3a6c70..816539f3580b1 100644 --- a/drivers/gpu/drm/asahi/g14_v27_2_abi.rs +++ b/drivers/gpu/drm/asahi/g14_v27_2_abi.rs @@ -146,11 +146,52 @@ pub(crate) fn encode_compute_timestamps(cmd: &mut [u8], cmd_va: u64, timestamps: user_timestamp_pointer(cmd_va + COMPUTE_USER_TIMESTAMPS as u64, timestamps) } +/// The command buffer a pool slot uses: each slot owns two, `2 * slot` and `2 * slot + 1`, +/// and alternates between them. The firmware may still read a retired command's JobMeta for +/// stamp stores it queued after it unlinked the slot, so a slot's next command must not +/// overwrite it. +pub(crate) fn pool_command(slot: usize, second: bool) -> usize { + 2 * slot + usize::from(second) +} + +/// Whether slot `slot`'s next command goes to its second buffer: the one it did not last +/// publish, given one bit per slot set when the last published command used the second. +/// Encoding does not change the bits, so a command that is encoded but never published is +/// replaced in the same spare buffer. +pub(crate) fn spare_buffer(published: u64, slot: usize) -> bool { + published & (1 << slot) == 0 +} + #[cfg_attr(not(test), kunit_tests(asahi_g14_fw_queues))] #[cfg_attr(test, allow(dead_code))] mod tests { use super::*; + #[test] + fn a_slot_never_rewrites_the_command_it_published_last() { + const SLOTS: usize = 48; + let mut owner = [usize::MAX; 2 * SLOTS]; + let mut published = 0u64; + let mut last = [usize::MAX; SLOTS]; + for round in 0..6 { + for slot in 0..SLOTS { + let c = pool_command(slot, spare_buffer(published, slot)); + assert!(c < 2 * SLOTS); + // Two buffers per slot, never shared with another slot. + assert!(owner[c] == usize::MAX || owner[c] == slot); + owner[c] = slot; + // Never the buffer the slot last published. + assert_ne!(c, last[slot]); + // Every third slot's publication fails once: the retry takes the same spare. + if (slot + round) % 3 == 0 { + assert_eq!(pool_command(slot, spare_buffer(published, slot)), c); + } + published ^= 1 << slot; + last[slot] = c; + } + } + } + #[test] fn head_wraps_and_keeps_one_entry_free() { assert_eq!(queue_head(8, 7, 7, 2), Ok(1)); diff --git a/drivers/gpu/drm/asahi/gpu/v27_2.rs b/drivers/gpu/drm/asahi/gpu/v27_2.rs index fc1514c61a8e4..4d16adc052a95 100644 --- a/drivers/gpu/drm/asahi/gpu/v27_2.rs +++ b/drivers/gpu/drm/asahi/gpu/v27_2.rs @@ -477,7 +477,10 @@ const COMMAND_STRIDE: usize = 0x1000; const STREAM_STRIDE: usize = 0x400; pub(crate) struct CommandPool { + /// Two command buffers per slot, used in turn (`g14_v27_2_abi::pool_command`). commands: fw::types::GpuArray, + /// Which of its two command buffers each slot last published, one bit per slot. + published: u64, streams: fw::types::GpuArray, tracking: fw::types::GpuArray, cursors: fw::types::GpuArray, @@ -487,7 +490,7 @@ pub(crate) struct CommandPool { impl CommandPool { pub(crate) fn new(alloc: &mut KernelAllocators, cfg: &'static hw::HwConfig) -> Result { - let commands = alloc.private.array_empty::(POOL_SLOTS * COMMAND_STRIDE)?; + let commands = alloc.private.array_empty::(2 * POOL_SLOTS * COMMAND_STRIDE)?; let streams = alloc.private.array_empty::(POOL_SLOTS * STREAM_STRIDE)?; let mut tracking = alloc.private.array_empty::(POOL_SLOTS * 0x100)?; // One 64-byte line per retirement cursor, so no firmware write can reach a neighbour. @@ -498,7 +501,8 @@ impl CommandPool { put64(chunk, 0, base + 0x40 * i as u64); } crate::pgtable::clean_poc(tracking.as_slice().as_ptr(), tracking.as_slice().len()); - Ok(Self { commands, streams, tracking, cursors, stamps, preempt1: cfg.compute_preempt1_size as u64 }) + Ok(Self { commands, published: 0, streams, tracking, cursors, stamps, + preempt1: cfg.compute_preempt1_size as u64 }) } /// Builds slot `i` for `job` and returns the command's firmware VA. @@ -516,7 +520,14 @@ impl CommandPool { encode: impl FnOnce(&mut [u8], &mut [u8], crate::g14_v27_2_render::Common) -> Result, ) -> Result { if i >= POOL_SLOTS { return Err(EINVAL); } - let cmd_va = self.commands.gpu_va().get() + (i * COMMAND_STRIDE) as u64; + // The firmware may read a retired command's JobMeta after it has unlinked the slot + // (for the stamp stores it still has queued), so the slot's next command goes to its + // other buffer. A buffer is rewritten only after the slot has been retired and + // unlinked once more, which the firmware does in a later pass. Encoding always + // takes the spare; `published` moves it only once the firmware can see the command, + // so a publication that fails leaves the last published buffer alone. + let c = g14_v27_2_abi::pool_command(i, g14_v27_2_abi::spare_buffer(self.published, i)); + let cmd_va = self.commands.gpu_va().get() + (c * COMMAND_STRIDE) as u64; let stream_va = self.streams.gpu_va().get() + (i * STREAM_STRIDE) as u64; let tracking_va = self.tracking.gpu_va().get() + (i * 0x100) as u64; let timestamps_va = self.stamps.gpu_va().get() + (i * 0x80) as u64; @@ -526,7 +537,7 @@ impl CommandPool { let stamps = &mut self.stamps.as_mut_slice()[i * 0x80..(i + 1) * 0x80]; stamps.fill(0); crate::pgtable::clean_poc(stamps.as_ptr(), stamps.len()); - let cmd = &mut self.commands.as_mut_slice()[i * COMMAND_STRIDE..(i + 1) * COMMAND_STRIDE]; + let cmd = &mut self.commands.as_mut_slice()[c * COMMAND_STRIDE..(c + 1) * COMMAND_STRIDE]; cmd.fill(0); let stream = &mut self.streams.as_mut_slice()[i * STREAM_STRIDE..(i + 1) * STREAM_STRIDE]; stream.fill(0); @@ -540,6 +551,12 @@ impl CommandPool { Ok(cmd_va) } + /// Slot `i`'s command, encoded in its spare buffer, is now visible to the firmware: the + /// next encode of the slot takes the other buffer. + pub(crate) fn mark_published(&mut self, i: usize) { + if i < POOL_SLOTS { self.published ^= 1 << i; } + } + /// Retirement: advance slot `i`'s cursor to the value the firmware recorded at /// tracking +0xc, so its retirement scan unlinks the slot from the queue's list. /// @@ -560,6 +577,18 @@ impl CommandPool { Ok(true) } + /// Slot `i`'s record count (+0xc), its retirement cursor and whether the firmware still + /// holds it in the queue's tracking list (+0x94), for a stall report. + pub(crate) fn describe(&self, i: usize) -> Result<(u32, u32, u32)> { + if i >= POOL_SLOTS { return Err(EINVAL); } + crate::pgtable::clean_invalidate_poc(self.tracking.as_slice()[i * 0x100..].as_ptr(), 0x100); + crate::pgtable::clean_invalidate_poc(self.cursors.as_slice()[i * 0x40..].as_ptr(), 0x40); + crate::pgtable::poc_barrier(); + Ok((self.tracking.shared_index(i * 0x100 + 0xc)?.load(Ordering::Acquire), + self.cursors.shared_index(i * 0x40)?.load(Ordering::Acquire), + self.tracking.shared_index(i * 0x100 + 0x94)?.load(Ordering::Acquire))) + } + /// Whether the firmware still holds slot `i` in the queue's tracking list (+0x94). pub(crate) fn registered(&self, i: usize) -> Result { if i >= POOL_SLOTS { return Err(EINVAL); }