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
10 changes: 7 additions & 3 deletions drivers/gpu/drm/asahi/g17/initdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,15 +681,17 @@ impl InitData {
}

/// M5 TA, fragment and compute share the compute outer ring per priority.
/// The physical queue claim prevents reassignment while this credit lives.
/// The caller must hold `Port::m5_outer`; the physical queue claim prevents
/// reassignment while this credit lives.
pub(crate) fn reserve_outer(&self, priority: u8, count: u32) -> Result<OuterClaim> {
if !self.m5 { return Err(EINVAL); }
let cursors = self.work_cursors(priority, super::fw::queue::DataMaster::Compute)?;
self.outer_credits.as_ref().ok_or(EIO)?.reserve(priority, count, cursors)
}

/// Hold the device mutex for this scope, including its Drop. The result
/// borrows only the claim, allowing the caller to publish through Firmware.
/// Hold the device mutex for this scope, including its Drop, and serialize entry
/// and Drop under `Port::m5_outer` via `Port::outer_scope`. The result borrows
/// only the claim, allowing the caller to publish through Firmware.
pub(crate) fn outer_scope<'a>(&self, claim: &'a mut OuterClaim) -> Result<OuterScope<'a>> {
self.outer_credits.as_ref().ok_or(EINVAL)?.scope(claim)
}
Expand Down Expand Up @@ -1074,6 +1076,8 @@ impl InitData {
// The word recovery_state() reads: the status block moves with the firmware build.
recovery: self.object(self.cluster)?.word(self.recovery_state_offset())?,
mailbox,
m5_credits: self.outer_credits.clone(),
m5_history: self.m5_history_view()?,
})
}

Expand Down
26 changes: 26 additions & 0 deletions drivers/gpu/drm/asahi/g17/initdata/m5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ impl InitData {
ring(0), ring(1), ring(2), ring(3), page(canonical.start)]
}

/// Direct-port view of the M5 submission-history table (none off M5 or on common 26A428).
pub(super) fn m5_history_view(&self) -> Result<Option<crate::g17::port::M5HistoryView>> {
if !self.m5 || self.graph.is_common26() {
return Ok(None);
}
let private = self.object(self.cluster)?;
let base = self.graph.private.status_config;
let field = |offset| {
self.m5_status_config
.relocate(offset)
.map(|at| base + at)
.ok_or(EINVAL)
};
let history = field(m::M5_STATUS_CONFIG_HISTORY)?;
Ok(Some(crate::g17::port::M5HistoryView {
generation: private.word(field(m::M5_STATUS_CONFIG_HISTORY_GENERATION)?)?,
config: [
private.word(history)?,
private.word(history + 4)?,
private.word(history + 8)?,
private.word(history + 12)?,
],
slots: private.pointer(field(m::M5_STATUS_CONFIG_HISTORY_SLOTS)?, 256)?,
}))
}

/// Selects the power-policy layout of the M5 firmware build `build_m5` encodes for.
pub(in crate::g17) fn set_m5_policy_layouts(
&mut self,
Expand Down
147 changes: 141 additions & 6 deletions drivers/gpu/drm/asahi/g17/initdata/outer_credit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use kernel::{prelude::*, sync::Arc};

const PRIORITIES: usize = 4;

pub(super) struct Credits {
pub(crate) struct Credits {
reserved: [AtomicU32; PRIORITIES],
published: [AtomicU64; PRIORITIES],
consumers: [AtomicU64; PRIORITIES],
Expand All @@ -24,7 +24,8 @@ pub(crate) struct OuterClaim {
}

/// Borrows the claim only. The caller must hold the device mutex from entry
/// through Drop: its temporarily lent credits are available to that publisher.
/// through Drop and serialize entry and Drop under `Port::m5_outer` (via
/// `Port::outer_scope`): its temporarily lent credits are available to that publisher.
pub(crate) struct OuterScope<'a> {
claim: &'a mut OuterClaim,
before: u64,
Expand All @@ -49,19 +50,23 @@ impl Credits {
}, GFP_KERNEL)?)
}

pub(super) fn available(&self) -> Arc<Availability> { self.available.clone() }
pub(crate) fn available(&self) -> Arc<Availability> { self.available.clone() }

pub(super) fn waiting(&self) -> bool { self.available.has_waiters() }

pub(super) fn capacity(&self, priority: u8, cursors: [u32; 3]) -> Result<u32> {
pub(crate) fn is_scoped(&self) -> bool {
self.scoped.load(Ordering::Acquire)
}

pub(crate) fn capacity(&self, priority: u8, cursors: [u32; 3]) -> Result<u32> {
let pending = self.reserved.get(usize::from(priority)).ok_or(EINVAL)?
.load(Ordering::Acquire);
Ok(free(cursors)?.saturating_sub(pending))
}

/// Reserve under the sole publisher mutex. Off-lock cancellation may only
/// Reserve under the outer-ring lock. Off-lock cancellation may only
/// reduce reserved counts, so the sampled capacity is conservative.
pub(super) fn reserve(self: &Arc<Self>, priority: u8, count: u32,
pub(crate) fn reserve(self: &Arc<Self>, priority: u8, count: u32,
cursors: [u32; 3]) -> Result<OuterClaim>
{
if count == 0 || count >= WORK_RING_SLOTS { return Err(EINVAL); }
Expand All @@ -75,6 +80,26 @@ impl Credits {
Ok(OuterClaim { domain: self.clone(), priority: index, remaining: count })
}

/// Consumes one reserved slot for a direct M5 publication without touching
/// `published[]`, so an enclosing device-mutex `OuterScope` never debits a direct slot.
pub(crate) fn consume_direct(&self, claim: &mut OuterClaim) -> Result {
self.consume_direct_count(claim, 1)
}

/// Consumes `count` reserved slots for a direct M5 publication without touching
/// `published[]`, so an enclosing device-mutex `OuterScope` never debits direct slots.
pub(crate) fn consume_direct_count(&self, claim: &mut OuterClaim, count: u32) -> Result {
if count == 0 || !core::ptr::eq(self, &*claim.domain) {
return Err(EINVAL);
}
if claim.remaining < count {
return Err(EIO);
}
claim.remaining -= count;
self.reserved[claim.priority].fetch_sub(count, Ordering::AcqRel);
Ok(())
}

pub(super) fn scope<'a>(self: &Arc<Self>, claim: &'a mut OuterClaim) -> Result<OuterScope<'a>> {
if !Arc::ptr_eq(self, &claim.domain) { return Err(EINVAL); }
if self.scoped.swap(true, Ordering::AcqRel) { return Err(EBUSY); }
Expand Down Expand Up @@ -116,6 +141,11 @@ impl OuterClaim {
pub(crate) fn remaining(&self) -> u32 {
self.remaining
}

/// Priority class of the reserved outer work ring.
pub(crate) fn priority(&self) -> usize {
self.priority
}
}

impl Drop for OuterClaim {
Expand All @@ -128,3 +158,108 @@ impl Drop for OuterClaim {
}
}
}

#[kernel::macros::kunit_tests(asahi_g17_outer_credit)]
mod tests {
use super::*;

#[test]
fn direct_consumption_does_not_debit_outer_scope() {
let credits = Credits::new().unwrap();
let cursors = [0, 0, 0];
let full = free(cursors).unwrap();
let mut scoped_claim = credits.reserve(1, 2, cursors).unwrap();
let mut direct_claim = credits.reserve(1, 1, cursors).unwrap();
let mut direct_pair = credits.reserve(1, 2, cursors).unwrap();
assert_eq!(credits.capacity(1, cursors).unwrap(), full - 5);

// Direct publish consumes only its own reserved slots and leaves published[] untouched.
assert!(!credits.is_scoped());
credits.consume_direct(&mut direct_claim).unwrap();
assert_eq!(direct_claim.remaining(), 0);
credits.consume_direct_count(&mut direct_pair, 2).unwrap();
assert_eq!(direct_pair.remaining(), 0);
assert_eq!(credits.capacity(1, cursors).unwrap(), full - 2);
assert_eq!(credits.counter(1).unwrap().load(Ordering::Acquire), 0);

// An OuterScope taken afterward keeps its full 2 credits when nothing published via it.
{
let _scope = credits.scope(&mut scoped_claim).unwrap();
assert!(credits.is_scoped());
assert!(credits.reserve(1, 1, cursors).is_err());
}
assert!(!credits.is_scoped());
assert_eq!(scoped_claim.remaining(), 2);
drop(direct_claim);
drop(direct_pair);
drop(scoped_claim);
assert_eq!(credits.capacity(1, cursors).unwrap(), full);
}

#[test]
fn mixed_render_compute_wrap_and_generation_change_with_queued_claim() {
let credits = Credits::new().unwrap();
let n = WORK_RING_SLOTS;
// Producer sits two slots before wrap while both consumers lag 4 slots behind
// (free = WORK_RING_SLOTS - 5).
let mut consumer_a = n - 6;
let mut consumer_b = n - 6;
let mut producer = n - 2;
let cursors = [consumer_a, consumer_b, producer];
let initial_free = free(cursors).unwrap();
assert_eq!(initial_free, n - 5);

// Interleave a queued 2-slot render claim with 1-slot compute claims across ring wrap.
let mut queued_render = credits.reserve(2, 2, cursors).unwrap();
let mut compute_a = credits.reserve(2, 1, cursors).unwrap();
credits.consume_direct(&mut compute_a).unwrap();
producer = (producer + 1) % n; // n - 1
assert_eq!(
credits.capacity(2, [consumer_a, consumer_b, producer]).unwrap(),
initial_free - 3,
);

let mut compute_b = credits.reserve(2, 1, [consumer_a, consumer_b, producer]).unwrap();
credits.consume_direct(&mut compute_b).unwrap();
producer = (producer + 1) % n; // 0 (wrapped!)
assert_eq!(producer, 0);

// Fill remaining capacity so exactly 1 free slot remains; 2-slot render must fail EBUSY
// while 1-slot compute still succeeds across the wrap boundary.
let rem = credits.capacity(2, [consumer_a, consumer_b, producer]).unwrap();
let filler = credits.reserve(2, rem - 1, [consumer_a, consumer_b, producer]).unwrap();
assert_eq!(credits.capacity(2, [consumer_a, consumer_b, producer]).unwrap(), 1);
assert_eq!(credits.reserve(2, 2, [consumer_a, consumer_b, producer]).err(), Some(EBUSY));
let mut edge_compute = credits.reserve(2, 1, [consumer_a, consumer_b, producer]).unwrap();
credits.consume_direct(&mut edge_compute).unwrap();
producer = (producer + 1) % n; // 1
assert_eq!(credits.capacity(2, [consumer_a, consumer_b, producer]).unwrap(), 0);
drop(filler);

// Consumers advance across the wrap boundary and report progress.
consumer_a = 1;
consumer_b = 1;
let wrapped = [consumer_a, consumer_b, producer];
assert!(credits.progressed(2, wrapped).unwrap());
assert_eq!(credits.capacity(2, wrapped).unwrap(), (n - 1) - 2);

// Simulate locked render / recovery entering OuterScope while a queued compute claim
// waits across a generation change: new reserves fail EBUSY and direct consumption never
// debits OuterScope's published counter; dropping the queued claim after the generation
// change releases its reserved credits cleanly.
let queued_compute = credits.reserve(2, 1, wrapped).unwrap();
{
let _scope = credits.scope(&mut queued_render).unwrap();
assert!(credits.is_scoped());
assert_eq!(credits.reserve(2, 1, wrapped).err(), Some(EBUSY));
credits.counter(2).unwrap().fetch_add(2, Ordering::AcqRel);
}
assert!(!credits.is_scoped());
assert_eq!(queued_render.remaining(), 0);
// Generation changed before queued_compute published: dropping it returns its credit.
assert_eq!(queued_compute.remaining(), 1);
drop(queued_compute);
drop(queued_render);
assert_eq!(credits.capacity(2, wrapped).unwrap(), n - 1);
}
}
19 changes: 19 additions & 0 deletions drivers/gpu/drm/asahi/g17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,13 @@ struct Shared {
/// wherever the completion IRQ thread first ran (see `queue_event_records`). On M5 it is
/// unbound, so a worker of that CPU's affinity pod (its L2) runs it; elsewhere it is per-CPU.
events: Arc<workqueue::OwnedQueue>,
/// Preallocated completion collector owned exclusively by `run_runtime_events`.
#[pin]
worker_deferred: Mutex<Option<runtime::DeferredBatch>>,
/// Restrict each scheduler's ordered submit queue to the creating CPU's cluster, beside the
/// event work. Single-die M5 parts leave schedulers unrestricted so queues created on one
/// thread do not contend on a single cluster under multi-queue load.
pin_scheduler_cluster: bool,
/// The CPU of the last publication, `u32::MAX` before the first.
submitter: AtomicU32,
#[pin]
Expand Down Expand Up @@ -1640,6 +1647,12 @@ impl Shared {
self.events.clone()
}

/// Whether each scheduler's submit queue should be restricted to the creating CPU's cluster
/// (all but single-die M5).
pub(super) fn pin_scheduler_cluster(&self) -> bool {
self.pin_scheduler_cluster
}

/// Records the publishing CPU for the event work's placement.
fn note_submitter(&self) {
let cpu = kernel::cpu::CpuId::current().as_u32();
Expand Down Expand Up @@ -1949,6 +1962,7 @@ impl Gpu {
let max_pstate = frequencies
.as_ref()
.map_or(0, stats::StateFrequencies::max_pstate);
let worker_deferred = runtime::DeferredBatch::worker()?;
let shared = Arc::pin_init(
pin_init!(Shared {
drm <- new_mutex!(Some(dev.into()), "g17::Shared::drm"),
Expand Down Expand Up @@ -1992,6 +2006,11 @@ impl Gpu {
governor_armed: AtomicBool::new(false),
governor_activity_ns: AtomicU64::new(0),
events,
worker_deferred <- new_mutex!(
Some(worker_deferred),
"g17::Shared::worker_deferred"
),
pin_scheduler_cluster: !m5 || id.num_dies > 1,
submitter: AtomicU32::new(u32::MAX),
grow <- new_work!("g17::Shared::grow"),
reclaim_work <- new_work!("g17::Shared::reclaim_work"),
Expand Down
Loading
Loading