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
114 changes: 102 additions & 12 deletions drivers/gpu/drm/asahi/g17/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ use super::{
};
use crate::mmu::{self, ExecutionContext, Vm, UAT_PGSZ};
use core::sync::atomic::{fence, AtomicBool, AtomicU32, AtomicU64, AtomicU8, AtomicUsize, Ordering};
use identity::{Order, RenderSlots, WorkStates, FREE, NODE_STRIDE, STATE_COUNT};
use identity::{Order, OrderWake, RenderSlots, WorkStates, FREE, NODE_STRIDE, STATE_COUNT};

/// Both of a context's render channels are bound to other pairs whose publications have not
/// all retired: the publisher waits for that retirement.
pub(crate) const RENDER_CHANNEL_PENDING: Error = ERECALLCONFLICT;
use super::job::Engine;
use kernel::{
new_mutex,
new_condvar, new_mutex,
prelude::*,
sync::{Arc, Mutex},
sync::{Arc, CondVar, CondVarTimeoutResult, Mutex},
};

const QOS_IDS: usize = 128;
Expand Down Expand Up @@ -241,6 +241,8 @@ pub(crate) struct Context {
compute_returns: Arc<super::availability::Availability>,
/// The order in which this context's ioctls take work-state nodes.
order: Arc<Mutex<Order>>,
/// Woken when `order`'s turn moves after a submitter waited on a full order window.
order_wait: Arc<CondVar>,
work: Arc<Mutex<WorkStorage>>,
work_base: u64,
/// How far the work storage's address reached the firmware, kept up to date by every
Expand Down Expand Up @@ -365,6 +367,8 @@ impl Context {
Arc::pin_init(new_mutex!(render_id, "G17 context render pool identity"), GFP_KERNEL)?;
let order =
Arc::pin_init(new_mutex!(Order::new(), "G17 context admission order"), GFP_KERNEL)?;
let order_wait =
Arc::pin_init(new_condvar!("G17 context admission order wait"), GFP_KERNEL)?;
Ok(Self {
m5: alloc.uat.is_m5(),
execution,
Expand All @@ -389,6 +393,7 @@ impl Context {
state_returns,
compute_returns,
order,
order_wait,
work,
work_base,
work_mark,
Expand Down Expand Up @@ -730,6 +735,83 @@ impl Context {
}
}

/// Waits interruptibly for an admission ordinal while `ORDER_WINDOW` ioctls are
/// queued ahead of the oldest waiting one, returning `EBUSY` if the turn does
/// not move within 1 s, `EIO` on VM loss, or `ERESTARTSYS` on signals.
fn assign_order(order: &Mutex<Order>, order_wait: &CondVar, status: &VmStatus) -> Result<u64> {
let mut guard = order.lock();
if let Some(ordinal) = guard.assign() {
return Ok(ordinal);
}
const ORDER_WAIT_NS: u64 = 1_000_000_000;
let deadline = super::now_ns().saturating_add(ORDER_WAIT_NS);
loop {
if status.get() != 0 {
return Err(EIO);
}
if let Some(ordinal) = guard.assign() {
return Ok(ordinal);
}
let remaining = deadline.saturating_sub(super::now_ns());
if remaining == 0 {
return Err(EBUSY);
}
let slice_ms = (remaining.div_ceil(1_000_000) as u32).clamp(1, 50);
let jiffies = kernel::time::msecs_to_jiffies(slice_ms);
if matches!(
order_wait.wait_interruptible_timeout(&mut guard, jiffies),
CondVarTimeoutResult::Signal { .. }
) {
return Err(ERESTARTSYS);
}
}
}

#[cfg(CONFIG_KUNIT = "y")]
pub(super) const ORDER_WINDOW: u64 = identity::ORDER_WINDOW;

#[cfg(CONFIG_KUNIT = "y")]
pub(super) struct TestOrderWindow {
order: core::pin::Pin<KBox<Mutex<Order>>>,
wait: core::pin::Pin<KBox<CondVar>>,
status: Arc<VmStatus>,
first: Option<u64>,
}

#[cfg(CONFIG_KUNIT = "y")]
impl TestOrderWindow {
pub(super) fn new_full() -> Result<Self> {
let order = KBox::pin_init(new_mutex!(Order::new(), "G17 test order"), GFP_KERNEL)?;
let wait = KBox::pin_init(new_condvar!("G17 test order wait"), GFP_KERNEL)?;
let status = Arc::pin_init(VmStatus::new(), GFP_KERNEL)?;
let first = {
let mut guard = order.lock();
let first = guard.assign().ok_or(EBUSY)?;
for _ in 1..identity::ORDER_WINDOW {
let _ = guard.assign().ok_or(EBUSY)?;
}
first
};
Ok(Self {
order,
wait,
status,
first: Some(first),
})
}

pub(super) fn assign(&self) -> Result<u64> {
assign_order(&self.order, &self.wait, &self.status)
}

pub(super) fn leave_oldest(&mut self) -> bool {
let Some(first) = self.first.take() else {
return false;
};
self.order.lock().leave(first).submitters
}
}

/// Shared by all engines of one ioctl. Queued work initially consumes no node.
pub(crate) struct WorkStateLease {
context: Arc<Context>,
Expand All @@ -741,11 +823,12 @@ pub(crate) struct WorkStateLease {
}
impl WorkStateLease {
pub(super) fn status(&self) -> &Arc<VmStatus> { self.context.status() }
/// The lease of a new ioctl, ordered after every earlier one of its context. EBUSY while
/// too many of the context's ioctls are queued behind its oldest waiting one
/// (see `identity::Order`).
/// The lease of a new ioctl, ordered after every earlier one of its context. Waits
/// interruptibly while too many of the context's ioctls are queued behind its oldest
/// waiting one (see `identity::Order`), returning EBUSY only if the turn does not move
/// within the backpressure window.
pub(crate) fn deferred(context: Arc<Context>) -> Result<Self> {
let ordinal = context.order.lock().assign().ok_or(EBUSY)?;
let ordinal = assign_order(&context.order, &context.order_wait, context.status())?;
Ok(Self {
context,
word: AtomicU32::new(FREE),
Expand Down Expand Up @@ -794,9 +877,7 @@ impl WorkStateLease {
let wake = order.admit(self.ordinal);
drop(order);
drop(work);
if wake {
self.context.pass_turn();
}
self.context.notify_turn(wake);
Ok(candidate)
}
/// The domain a publisher samples before acquire() and waits on after EBUSY.
Expand Down Expand Up @@ -891,8 +972,9 @@ impl Drop for WorkStateLease {
let word = self.word();
if word != FREE {
self.context.release_state(word, usize::from(self.engine.load(Ordering::Relaxed)));
} else if self.context.order.lock().leave(self.ordinal) {
self.context.pass_turn();
} else {
let wake = self.context.order.lock().leave(self.ordinal);
self.context.notify_turn(wake);
}
}
}
Expand All @@ -911,6 +993,14 @@ impl Context {
self.state_returns.notify();
self.compute_returns.notify();
}
fn notify_turn(&self, wake: OrderWake) {
if wake.publishers {
self.pass_turn();
}
if wake.submitters {
self.order_wait.notify_all();
}
}
}

impl QosIds {
Expand Down
57 changes: 36 additions & 21 deletions drivers/gpu/drm/asahi/g17/context/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,24 @@ pub(super) const ORDER_SPLIT_LIMIT: u64 = ORDER_WINDOW / 2;
/// it. Taking nodes strictly in turn keeps the words in submission order. Each engine admits
/// from its own scheduler, so the turn costs a scheduler round trip whenever the other engine's
/// ioctl is ready first.
/// Which waiters to wake after [`Order::admit`] or [`Order::leave`] advances the turn.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(super) struct OrderWake {
pub(super) publishers: bool,
pub(super) submitters: bool,
}

pub(super) struct Order {
/// The next ordinal to hand out.
next: u64,
/// The oldest ordinal that has neither taken a node nor left.
turn: u64,
/// Ordinals past the turn that left, as bits at ordinal % ORDER_WINDOW.
done: [u64; (ORDER_WINDOW / 64) as usize],
/// An ioctl was refused since the turn last moved: its publisher waits for the next move.
/// A scheduler publisher was refused since the turn last moved.
refused: bool,
/// A submitter hit the full window since the turn last moved.
submitter_waiting: bool,
}
impl Order {
pub(super) const fn new() -> Self {
Expand All @@ -139,12 +148,15 @@ impl Order {
turn: 0,
done: [0; (ORDER_WINDOW / 64) as usize],
refused: false,
submitter_waiting: false,
}
}
/// The ordinal of a new ioctl, in submission order. None while [`ORDER_WINDOW`] ioctls of
/// the context are past the oldest one still waiting for its node.
/// the context are past the oldest one still waiting for its node; remembers the refusal
/// so the next turn advance wakes blocked submitters.
pub(super) fn assign(&mut self) -> Option<u64> {
if self.next - self.turn >= ORDER_WINDOW {
self.submitter_waiting = true;
return None;
}
let ordinal = self.next;
Expand All @@ -166,28 +178,30 @@ impl Order {
self.refused |= !admits;
admits
}
/// The ioctl `ordinal` took its node at its turn. Returns whether a refused ioctl may now be
/// admitted.
pub(super) fn admit(&mut self, ordinal: u64) -> bool {
/// The ioctl `ordinal` took its node at its turn. Returns which waiters may now retry.
pub(super) fn admit(&mut self, ordinal: u64) -> OrderWake {
self.finish(ordinal)
}
/// The ioctl `ordinal` left without taking a node (it failed or was cancelled first).
/// Returns whether a refused ioctl may now be admitted.
pub(super) fn leave(&mut self, ordinal: u64) -> bool {
/// Returns which waiters may now retry.
pub(super) fn leave(&mut self, ordinal: u64) -> OrderWake {
self.finish(ordinal)
}
fn finish(&mut self, ordinal: u64) -> bool {
fn finish(&mut self, ordinal: u64) -> OrderWake {
if ordinal < self.turn {
return false;
return OrderWake::default();
}
if ordinal == self.turn {
self.pass();
return core::mem::take(&mut self.refused);
return OrderWake {
publishers: core::mem::take(&mut self.refused),
submitters: core::mem::take(&mut self.submitter_waiting),
};
}
// assign() keeps every ordinal within the window past the turn.
let bit = ordinal % ORDER_WINDOW;
self.done[(bit / 64) as usize] |= 1 << (bit % 64);
false
OrderWake::default()
}
/// Moves the turn past the current ordinal and every following one already done.
fn pass(&mut self) {
Expand Down Expand Up @@ -356,13 +370,13 @@ mod tests {
}
assert!(order.admits(ordinals[0]));
// Its admission moves the turn and wakes the refused.
assert!(order.admit(ordinals[0]));
assert_eq!(order.admit(ordinals[0]), OrderWake { publishers: true, submitters: false });
assert!(order.admits(ordinals[1]));
// Nobody was refused since, so the next admission wakes nobody.
assert!(!order.admit(ordinals[1]));
assert_eq!(order.admit(ordinals[1]), OrderWake::default());
assert!(!order.admits(ordinals[3]));
assert!(order.admits(ordinals[2]));
assert!(order.admit(ordinals[2]));
assert_eq!(order.admit(ordinals[2]), OrderWake { publishers: true, submitters: false });
assert!(order.admits(ordinals[3]));
}

Expand All @@ -376,14 +390,14 @@ mod tests {
order.assign().unwrap(),
);
// b and c fail before their turn; while a waits, d waits too.
assert!(!order.leave(b));
assert!(!order.leave(c));
assert_eq!(order.leave(b), OrderWake::default());
assert_eq!(order.leave(c), OrderWake::default());
assert!(!order.admits(d));
// Once a takes its node, the turn passes b and c as well, and the refused one may retry.
assert!(order.admit(a));
assert_eq!(order.admit(a), OrderWake { publishers: true, submitters: false });
assert!(order.admits(d));
// An ordinal already passed leaves without effect.
assert!(!order.leave(a));
assert_eq!(order.leave(a), OrderWake::default());
}

#[test]
Expand All @@ -396,9 +410,10 @@ mod tests {
}
assert!(order.assign().is_none());
// An ioctl leaving far ahead is remembered within the window.
assert!(!order.leave(last));
assert_eq!(order.leave(last), OrderWake::default());
assert!(order.admits(first));
assert!(!order.admit(first));
// Only the refused submitter waiting on the full window is woken once the turn advances.
assert_eq!(order.admit(first), OrderWake { publishers: false, submitters: true });
assert!(order.assign().is_some());
}

Expand All @@ -421,7 +436,7 @@ mod tests {
for _ in 0..24 {
let ordinal = order.assign().unwrap();
assert!(order.admits(ordinal));
assert!(!order.admit(ordinal));
assert_eq!(order.admit(ordinal), OrderWake::default());
}
}

Expand Down
Loading
Loading