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
86 changes: 79 additions & 7 deletions drivers/gpu/drm/asahi/g14_v27_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! consumes those rings (the firmware waits for space in them) and retires completed commands.
//! Completion follows the queue's retired position, which the firmware advances in order. There
//! is a delayed service run while waiting for firmware to free a pool slot or make room on
//! the device-control ring. These changes need not generate a notification.
//! a submission or device-control ring. These changes need not generate a notification.

use core::ops::Range;
use core::sync::atomic::{AtomicBool, AtomicU64, Ordering};
Expand Down Expand Up @@ -827,6 +827,7 @@ struct State {
waiting_unlink: Option<FirmwareWait>,
waiting_control: Option<FirmwareWait>,
waiting_bind: Option<Instant<Monotonic>>,
waiting_ring: Option<FirmwareWait>,
/// Publications and retirements counted on the queue's position block.
published: u64,
/// Completion stamps seen before the retired position they precede.
Expand Down Expand Up @@ -896,7 +897,7 @@ impl State {
if g14_v27_2_abi::guard_releasable(false, self.kept(packet)) {
packet.release_guard();
}
packet.finish(Err(err));
packet.fail_packet(err);
}
}

Expand Down Expand Up @@ -1088,10 +1089,12 @@ impl Runtime {
}
if front_removed {
state.waiting_bind = None;
state.waiting_ring = None;
}
if state.pending.is_empty() {
state.waiting_unlink = None;
state.waiting_bind = None;
state.waiting_ring = None;
if !state.running() {
self.idle.notify_all();
}
Expand All @@ -1105,6 +1108,7 @@ impl Runtime {
self.healthy.store(false, Ordering::Release);
self.idle.notify_all();
state.waiting_bind = None;
state.waiting_ring = None;
while let Some((packet, _)) = state.pending.pop() {
if !state.owns(&packet) {
packet.release_guard();
Expand Down Expand Up @@ -1202,6 +1206,49 @@ impl Runtime {
false
}

/// Handle transient `EBUSY` from submission-ring staging without failing the packet
/// immediately. Arms a bounded delayed retry capped at `COMMAND_TIMEOUT_MS`; returns `true`
/// once the wait expires and the front packet has been failed so `pump()` can continue.
fn handle_ring_busy(&self, state: &mut State, packet: &Arc<Packet>, paired: bool) -> bool {
match FirmwareWait::kick_due(&mut state.waiting_ring) {
Ok(true) => {
let kick_err = if paired {
[FRAGMENT, TILER]
.into_iter()
.filter(|&dm| state.root.render_queue_announced(dm))
.try_for_each(|dm| {
state
.peer
.as_mut()
.send_message(EP_DOORBELL, RootV27_2::render_kick(dm))
})
} else {
state
.peer
.as_mut()
.send_message(EP_DOORBELL, gpu::COMPUTE_KICK)
};
if let Err(err) = kick_err {
self.fail_all(state, err);
return false;
}
}
Ok(false) => {}
Err(_) => {
state.waiting_ring = None;
dev_err!(
self.dev,
"GPU: submission ring busy for {} ms\n",
COMMAND_TIMEOUT_MS
);
state.fail_publication(packet, EBUSY);
return true;
}
}
self.notify.retry();
false
}

/// Compute commands may pipeline. A render pair is a scheduling boundary so its buffer,
/// timestamps and dependencies remain owned until both data masters retire.
fn pump(&self, state: &mut State) {
Expand All @@ -1214,17 +1261,22 @@ impl Runtime {
if vm_failed(&packet) {
// Queued before its VM failed: none of it is published from now on.
state.waiting_bind = None;
state.waiting_ring = None;
state.fail_publication(&packet, EIO);
continue;
}
let command = packet.commands[index];
let paired = matches!(command, Command::Render { .. });
if paired && state.running() { return; }
if paired && state.running() {
state.waiting_ring = None;
return;
}
let count = if paired { 2 } else { 1 };
let slots = match self.free_slots(state, count) {
Ok(Some(slots)) => slots,
Ok(None) => {
state.waiting_bind = None;
state.waiting_ring = None;
return;
}
Err(err) => {
Expand Down Expand Up @@ -1252,25 +1304,31 @@ impl Runtime {
match result {
Ok(()) => {
state.waiting_bind = None;
state.waiting_ring = None;
if paired || !self.healthy() { return; }
}
Err(PublishError::BindBusy) => {
state.waiting_ring = None;
if !self.handle_bind_busy(state, &packet) {
return;
}
}
Err(PublishError::Error(EAGAIN)) => {
// Fabric/DCS bandwidth unavailable while the system sleeps.
state.waiting_bind = None;
state.waiting_ring = None;
self.notify.retry();
return;
}
Err(PublishError::Error(EBUSY)) if state.running() => {
Err(PublishError::Error(EBUSY)) => {
state.waiting_bind = None;
return;
if !self.handle_ring_busy(state, &packet, paired) {
return;
}
}
Err(PublishError::Error(err)) => {
state.waiting_bind = None;
state.waiting_ring = None;
dev_err!(self.dev, "GPU: command publication failed {:?}\n", err);
state.fail_publication(&packet, err);
}
Expand All @@ -1283,6 +1341,7 @@ impl Runtime {
control: crate::m3::agx_compute::Control, last: bool, slot: usize,
) -> core::result::Result<(), PublishError> {
let preempt = packet.preempt.as_ref().ok_or(EINVAL)?.gpu_va().get();
state.root.check_compute()?;
let bind = self.try_bind_vm(&packet.vm)?;
let stamp = state.last.next();
let job = ComputeJob {
Expand Down Expand Up @@ -1341,6 +1400,7 @@ impl Runtime {
command: &crate::m3::agx_uapi::UapiRenderCommand, usc: u64, last: bool, slots: [usize; 2],
) -> core::result::Result<(), PublishError> {
let queue = packet.render_queue.as_ref().ok_or(EINVAL)?;
state.root.check_render()?;
let bind = self.try_bind_vm(&packet.vm)?;
let scene = queue.prepare(command, usc)?;
let context = bind.slot();
Expand Down Expand Up @@ -1502,6 +1562,7 @@ impl Runtime {
let _ = state.pending.remove(i);
if i == 0 {
state.waiting_bind = None;
state.waiting_ring = None;
}
}
// Record it even if a racing scheduler timeout already claimed the fence (as fail_all).
Expand Down Expand Up @@ -1768,6 +1829,7 @@ impl Runtime {
// Work behind the stopped command waited out the recovery: restart its clocks.
let now = Instant::now();
state.waiting_bind = None;
state.waiting_ring = None;
for flight in state.inflight.iter_mut() { flight.started = now; }
if let Some(flight) = state.rendering.as_mut() { flight.started = now; }
dev_err!(self.dev,
Expand Down Expand Up @@ -1950,8 +2012,13 @@ impl Runtime {
self.fail_doomed(state, &done.packet, err);
self.keep(&mut state.kept_compute, done);
} else if let Some(err) = done.publication_error {
done.packet.release_guard();
done.packet.finish(Err(err));
if g14_v27_2_abi::guard_releasable(
state.live(&done.packet),
state.kept(&done.packet),
) {
done.packet.release_guard();
}
done.packet.fail_packet(err);
} else if done.last {
done.packet.retired();
} else if !state.owns(&done.packet) && !state.pending(&done.packet) {
Expand Down Expand Up @@ -2047,6 +2114,7 @@ impl Runtime {
self.asleep.store(true, Ordering::Release);
if let Some(state) = Option::as_mut(&mut *guard) {
state.waiting_bind = None;
state.waiting_ring = None;
}
let mut remaining = msecs_to_jiffies(STOP_DRAIN_MS);
loop {
Expand Down Expand Up @@ -2242,6 +2310,7 @@ impl Runtime {
self.asleep.store(false, Ordering::Release);
state.waiting_unlink = None;
state.waiting_bind = None;
state.waiting_ring = None;
if self.healthy() {
self.pump(state);
self.notify.wake();
Expand Down Expand Up @@ -2294,6 +2363,7 @@ impl Runtime {
state.waiting_unlink = None;
state.waiting_control = None;
state.waiting_bind = None;
state.waiting_ring = None;
match restart {
Ok(idle_off_allowed) => {
state.idle_off_allowed = idle_off_allowed;
Expand Down Expand Up @@ -2324,6 +2394,7 @@ impl Runtime {
state.root.firmware_running(), state.pending.len());
state.waiting_unlink = None;
state.waiting_bind = None;
state.waiting_ring = None;
if self.healthy() { self.pump(state); }
}

Expand Down Expand Up @@ -2500,6 +2571,7 @@ impl Backend {
waiting_unlink: None,
waiting_control: None,
waiting_bind: None,
waiting_ring: None,
published: 0,
late: 0,
busy: 0,
Expand Down
72 changes: 58 additions & 14 deletions drivers/gpu/drm/asahi/gpu/v27_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,20 @@ impl GpuManagerG14V13_5 {
}
}

/// Validated ring and queue positions ready for [`RootV27_2::stage_work`] to populate.
struct StagedRing<'a> {
head: &'a core::sync::atomic::AtomicU32,
old: u32,
capacity: u32,
target: u32,
first: bool,
producer: &'a core::sync::atomic::AtomicU32,
indices: &'a fw::types::GpuArray<u8>,
ring: &'a fw::types::GpuArray<u8>,
w: u32,
next: u32,
}

impl RootV27_2 {
fn word(a: &fw::types::GpuArray<u8>, at: usize) -> Result<u32> {
Ok(a.shared_index(at)?.load(Ordering::Acquire))
Expand Down Expand Up @@ -977,6 +991,24 @@ impl RootV27_2 {
self.render_queues[dm].announced.load(Ordering::Acquire)
}

/// Checks that both render submission rings and queues have room before preparing a scene
/// (at least 1 TA entry and 2 FS entries for own-barrier + fragment).
pub(crate) fn check_render(&self) -> Result {
for (dm, count) in [(TILER, 1), (FRAGMENT, 2)] {
let queue = &self.render_queues[dm];
self.check_work(&queue.positions, &queue.pointers, Some(&queue.announced),
RENDER_GROUP, dm, count)?;
}
Ok(())
}

/// Checks that the compute submission ring and queue have room for one command.
pub(crate) fn check_compute(&self) -> Result {
self.check_work(&self.boot.positions, &self.boot.pointers, None,
PRIORITY_GROUP, COMPUTE, 1)?;
Ok(())
}

pub(crate) fn stage_render(&self, dm: usize, commands: &[u64]) -> Result<Submission<'_>> {
let queue = self.render_queues.get(dm).ok_or(EINVAL)?;
self.stage_work(&queue.positions, &queue.pointers, queue.info(0),
Expand All @@ -988,12 +1020,11 @@ impl RootV27_2 {
None, PRIORITY_GROUP, COMPUTE, &[command])
}

#[allow(clippy::too_many_arguments)]
fn stage_work<'a>(
fn check_work<'a>(
&'a self, positions: &'a fw::types::GpuArray<u8>, pointers: &fw::types::GpuArray<u8>,
queue: QueueInfo, announced: Option<&'a core::sync::atomic::AtomicBool>,
group: usize, dm: usize, commands: &[u64],
) -> Result<Submission<'a>> {
announced: Option<&'a core::sync::atomic::AtomicBool>,
group: usize, dm: usize, count: u32,
) -> Result<StagedRing<'a>> {
let ring_index = 3 * group + dm;
let indices = &self.channels[2 * ring_index];
let ring = &self.channels[2 * ring_index + 1];
Expand All @@ -1009,7 +1040,6 @@ impl RootV27_2 {
if capacity as usize > pointers.len() / 8 || (first && old != retired) {
return Err(EIO);
}
let count = u32::try_from(commands.len()).map_err(|_| EINVAL)?;
let target = g14_v27_2_abi::queue_head(capacity, old, retired, count).map_err(|error| {
match error {
QueueError::Geometry => EIO,
Expand All @@ -1023,24 +1053,38 @@ impl RootV27_2 {
if w >= RING_RECORDS || r >= RING_RECORDS { return Err(EIO); }
let next = (w + 1) % RING_RECORDS;
if next == r { return Err(EBUSY); }
Ok(StagedRing { head, old, capacity, target, first, producer, indices, ring, w, next })
}

#[allow(clippy::too_many_arguments)]
fn stage_work<'a>(
&'a self, positions: &'a fw::types::GpuArray<u8>, pointers: &fw::types::GpuArray<u8>,
queue: QueueInfo, announced: Option<&'a core::sync::atomic::AtomicBool>,
group: usize, dm: usize, commands: &[u64],
) -> Result<Submission<'a>> {
let count = u32::try_from(commands.len()).map_err(|_| EINVAL)?;
let staged = self.check_work(positions, pointers, announced, group, dm, count)?;
let slot = u8::try_from(queue.slot).map_err(|_| EINVAL)?;
let entry = crate::m3::submission_entry(dm as u32, queue.queue,
u16::try_from(target).map_err(|_| EINVAL)?, slot, first).map_err(|_| EINVAL)?;
let base = w as usize * entry.len();
let entry = crate::m3::submission_entry(
dm as u32, queue.queue, u16::try_from(staged.target).map_err(|_| EINVAL)?,
slot, staged.first,
).map_err(|_| EINVAL)?;
let base = staged.w as usize * entry.len();
for (index, &command) in commands.iter().enumerate() {
let at = ((old as usize + index) % capacity as usize) * 8;
let at = ((staged.old as usize + index) % staged.capacity as usize) * 8;
pointers.shared_index(at)?.store(command as u32, Ordering::Relaxed);
pointers.shared_index(at + 4)?.store((command >> 32) as u32, Ordering::Relaxed);
crate::pgtable::clean_poc(pointers.as_slice()[at..].as_ptr(), 8);
}
for (index, word) in entry.chunks_exact(4).enumerate() {
ring.shared_index(base + 4 * index)?.store(
staged.ring.shared_index(base + 4 * index)?.store(
u32::from_le_bytes([word[0], word[1], word[2], word[3]]), Ordering::Relaxed);
}
crate::pgtable::clean_poc(ring.as_slice()[base..].as_ptr(), entry.len());
crate::pgtable::clean_poc(staged.ring.as_slice()[base..].as_ptr(), entry.len());
crate::pgtable::poc_barrier();
Ok(Submission { head, positions: positions.as_slice(), target, producer,
indices: indices.as_slice(), next, announced })
Ok(Submission { head: staged.head, positions: positions.as_slice(), target: staged.target,
producer: staged.producer, indices: staged.indices.as_slice(), next: staged.next,
announced })
}

/// Publishes the boot job, whose command and ring entry the root already holds. The GPU
Expand Down
Loading
Loading