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
36 changes: 20 additions & 16 deletions drivers/gpu/drm/asahi/g16/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ struct WaitTarget {
///
/// Unlike G15 (`g15/hw.rs: CROSS_ENGINE_HOLD` — "Never both type 0"), G16 (M4 25G83/26B123)
/// does not require a software-job hold on one cross-engine direction when neither side
/// carries `InitBM`. On G16, every stage (CDM, TA, FS) executes its `FINALIZE`
/// micro-sequence — flushing stage caches to the shared coherent fabric — before firmware
/// advances `fw_stamp`, so waiting on `fw_stamp` with `BarrierType::Stamp` (type 0) in both
/// directions (`fs2c` and `c2ta`/`c2fs`) avoids hold bubbles within one VM (same ASID and
/// UAT page tables). Zero-fault M4 J773g runs (13,873 `fs2c` and 20,506 `c2ta` type-0
/// handoffs) confirm no GPU wedge or hang, while CTS synchronization / pipeline-barrier
/// coverage validates producer-to-consumer memory visibility.
/// carries `InitBM`: `BarrierType::Stamp` (type 0) in both directions (`fs2c` and
/// `c2ta`/`c2fs`) avoids hold bubbles within one VM (same ASID and UAT page tables).
/// Zero-fault M4 J773g runs (13,873 `fs2c` and 20,506 `c2ta` type-0 handoffs) confirm no GPU
/// wedge or hang. Which stamp a wait uses is chosen at its site: a producer's `fw_stamp`,
/// stored at `FINALIZE`, does not yet make its writes visible to work on another firmware
/// queue, so waits on compute use `ComputeGraph::service_stamp` (linux#3349) and waits on a
/// render's FS use `RenderGraph::fs_service_stamp`.
///
/// Keep `BarrierType::Hold` (type 1) whenever either producer or consumer carries `InitBM`
/// (checked against both the waited target and any live `InitBM` flight). Cross-VM switches,
Expand Down Expand Up @@ -2523,7 +2523,7 @@ impl Firmware {
fs_notifier: r.fs_notifier.va,
legacy_empty: r.legacy_empty.va,
qinfo: [r.qinfo[0].va, r.qinfo[1].va],
user_stamp: [r.user_stamp[0].va, r.user_stamp[1].va],
user_stamp: [r.ta_service_stamp().va(), r.fs_service_stamp().va()],
fw_stamp: [r.fw_stamp[0].va, r.fw_stamp[1].va],
uma_mgr: a.uma_mgr,
uma_aux: a.uma_aux,
Expand Down Expand Up @@ -2565,7 +2565,7 @@ impl Firmware {
fs_notifier: r.fs_notifier.va,
legacy_empty: r.legacy_empty.va,
qinfo: [r.qinfo[0].va, r.qinfo[1].va],
user_stamp: [r.user_stamp[0].va, r.user_stamp[1].va],
user_stamp: [r.ta_service_stamp().va(), r.fs_service_stamp().va()],
fw_stamp: [r.fw_stamp[0].va, r.fw_stamp[1].va],
uma_mgr: a.uma_mgr,
uma_aux: a.uma_aux,
Expand Down Expand Up @@ -3568,7 +3568,7 @@ impl Firmware {
let refs = ComputeRefs {
notifier: g.notifier.va + notifier_page::COMPUTE_RECORD as u64,
qinfo: g.qinfo.va,
user_stamp: g.user_stamp.va,
user_stamp: g.service_stamp().va(),
fw_stamp: g.fw_stamp.va,
work: s.work.va,
work_gva: s.work.lower_gva.ok_or(EINVAL)?,
Expand Down Expand Up @@ -3671,14 +3671,14 @@ impl Firmware {
// Check producer InitBM explicitly in addition to the scheduler drain guard so
// FS -> CDM handoff: downgrade to `BarrierType::Stamp` (type 0) only when neither
// the waited render nor any live flight carries `InitBM` and the GPU does not need
// G15's cross-engine hold; G16 FS `FINALIZE` flushes caches before advancing
// `fw_stamp[1]` (see `cross_engine_barrier_type`).
// G15's cross-engine hold (see `cross_engine_barrier_type`). Compute may read what
// the render wrote: wait for the FS service stamp, not its FINALIZE `fw_stamp[1]`.
let producer_initbm = self.cfg.cross_engine_hold
|| wait_fs.initbm
|| self.engine.flights.iter().any(|fl| fl.initbm);
let bar_type = cross_engine_barrier_type(producer_initbm, false);
let bar = encode::barrier(
self.graph.render.fw_stamp[1].va,
self.graph.render.fs_service_stamp().va(),
wait_fs.stamp,
0x71,
target,
Expand All @@ -3699,8 +3699,10 @@ impl Firmware {
let producer_initbm =
wait_c.initbm || self.engine.flights.iter().any(|fl| fl.initbm);
let bar_type = cross_engine_barrier_type(producer_initbm, false);
// Compute on another pipe may read what the producer wrote: wait for the
// producer pipe's service stamp, not its FINALIZE fw_stamp.
let bar = encode::barrier(
self.graph.compute[p_idx].fw_stamp.va,
self.graph.compute[p_idx].service_stamp().va(),
wait_c.stamp,
work::compute::EVENTS[p_idx],
target,
Expand Down Expand Up @@ -4060,9 +4062,11 @@ impl Firmware {
}
for p_idx in 0..sizes::COMPUTE_PIPES {
let off = p_idx * work::barrier::SIZE;
// A render reads what compute wrote: wait for the producer pipe's service stamp, not
// its FINALIZE fw_stamp (see `ComputeGraph::service_stamp`).
if let Some(wait_c) = plan.ta_compute_wait[p_idx] {
let cbar = encode::barrier(
self.graph.compute[p_idx].fw_stamp.va,
self.graph.compute[p_idx].service_stamp().va(),
wait_c,
work::compute::EVENTS[p_idx],
target,
Expand All @@ -4078,7 +4082,7 @@ impl Firmware {
}
if let Some(wait_c) = plan.fs_compute_wait[p_idx] {
let fcbar = encode::barrier(
self.graph.compute[p_idx].fw_stamp.va,
self.graph.compute[p_idx].service_stamp().va(),
wait_c,
work::compute::EVENTS[p_idx],
target,
Expand Down
49 changes: 49 additions & 0 deletions drivers/gpu/drm/asahi/g16/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,29 @@ pub(crate) fn alloc_work(
})
}

/// A 32-bit stamp cell the firmware stores into: an object and a byte offset in it.
#[derive(Clone, Copy)]
pub(crate) struct StampCell<'a> {
obj: &'a FwObject,
off: usize,
}

impl<'a> StampCell<'a> {
fn new(obj: &'a FwObject, off: usize) -> Self {
Self { obj, off }
}

/// The cell's firmware VA, as work items and barriers name it.
pub(crate) fn va(&self) -> u64 {
self.obj.va + self.off as u64
}

/// Reads the cell.
pub(crate) fn read(&self) -> Result<u32> {
self.obj.r32(self.off)
}
}

pub(crate) struct ComputeGraph {
pub(crate) sched_ctx: Arc<FwObject>,
/// Whole 0x1000 page, ASC-cached; compute's record is +0x400. The firmware reads and
Expand Down Expand Up @@ -276,6 +299,17 @@ impl ComputeGraph {
)?;
self.threshold.w64(0, 1)
}

/// The pipe's terminal service-stamp cell. Each compute work item names this cell as the one
/// the firmware stores its stamp into after it has finished servicing the item's record, later
/// than [`Self::fw_stamp`], which it stores at FINALIZE. Retirement reads its completion from
/// this cell, and work on another firmware queue that reads what this pipe wrote waits on it:
/// at FINALIZE those writes are not yet visible there (linux#3349). All three go through this
/// accessor so that they can never name different cells. The cell is the queue's own
/// uncached [`Self::user_stamp`], not the ASC-cached notifier page.
pub(crate) fn service_stamp(&self) -> StampCell<'_> {
StampCell::new(&self.user_stamp, 0)
}
}

pub(crate) struct RenderGraph {
Expand Down Expand Up @@ -376,6 +410,21 @@ impl RenderGraph {
}
Ok(())
}

/// The TA queue's terminal service-stamp cell, as named by every render's TA work item and
/// read by retirement: the uncached `user_stamp[0]`.
pub(crate) fn ta_service_stamp(&self) -> StampCell<'_> {
StampCell::new(&self.user_stamp[0], 0)
}

/// The FS queue's terminal service-stamp cell, stored after the firmware has serviced a
/// pass's FS record, later than `fw_stamp[1]` (`FINALIZE`). Every render's FS work item names
/// it, retirement reads it, and work on another firmware queue that reads what a render wrote
/// waits on it, as for compute ([`ComputeGraph::service_stamp`]). It is the uncached
/// `user_stamp[1]`, not the ASC-cached FS notifier page.
pub(crate) fn fs_service_stamp(&self) -> StampCell<'_> {
StampCell::new(&self.user_stamp[1], 0)
}
}

/// Session-side queue graph allocations before sealing the session allocator and attaching
Expand Down
14 changes: 8 additions & 6 deletions drivers/gpu/drm/asahi/g16/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ impl Firmware {
// command's work item, microsequence and UMA descriptor. The cached notifier
// record's context word is written back only when the record is unlinked, so
// the host cannot read it reliably.
full &= stamp_reached(g.user_stamp.r32(0)?, f.target, tail);
full &= stamp_reached(g.service_stamp().read()?, f.target, tail);
} else {
if g.fw_stamp.r32(0)? != f.target
|| !self.queue_ring_receipt(&g.qstate, &g.qinfo, f.head)?
Expand All @@ -1298,7 +1298,7 @@ impl Firmware {
return Ok(Retirement::Pending);
}
rmb();
full &= g.user_stamp.r32(0)? == f.target;
full &= g.service_stamp().read()? == f.target;
}
let count = f.claims.slot_count as usize;
full &= count != 0 && count == f.computes as usize;
Expand Down Expand Up @@ -1378,11 +1378,13 @@ impl Firmware {
if render_behind {
let e = &self.engine;
// Check FS (terminal stage) before TA so running renders fail fast on 1 WC read.
let fs_reached = stamp_reached(g.user_stamp[1].r32(0)?, f.fs_target, e.fs_target);
let fs_reached =
stamp_reached(g.fs_service_stamp().read()?, f.fs_target, e.fs_target);
if !f.recovery_suspect && !fs_reached {
return Ok(Retirement::Pending);
}
let ta_reached = stamp_reached(g.user_stamp[0].r32(0)?, f.ta_target, e.ta_target);
let ta_reached =
stamp_reached(g.ta_service_stamp().read()?, f.ta_target, e.ta_target);
if !f.recovery_suspect && !ta_reached {
return Ok(Retirement::Pending);
}
Expand Down Expand Up @@ -1413,11 +1415,11 @@ impl Firmware {
.pb_flight_retired(a, f.uma_submitted, f.renders, false)?;
} else {
// Check FS (terminal stage) before TA so running renders fail fast on 1 WC read.
let fs_reached = g.user_stamp[1].r32(0)? == f.fs_target;
let fs_reached = g.fs_service_stamp().read()? == f.fs_target;
if !f.recovery_suspect && !fs_reached {
return Ok(Retirement::Pending);
}
let ta_reached = g.user_stamp[0].r32(0)? == f.ta_target;
let ta_reached = g.ta_service_stamp().read()? == f.ta_target;
if !f.recovery_suspect && !ta_reached {
return Ok(Retirement::Pending);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,48 @@ def item(declaration):
assert 'work::compute::EVENTS[pipe]' in prefix_compute_fn
assert 'work::compute::EVENTS[p_idx]' in c2ta_slice
assert 'work::compute::EVENTS[p_idx]' in c2fs_slice
# linux#3349: a render's barriers on a compute pipe wait on that pipe's service stamp, not FINALIZE.
assert 'self.graph.compute[p_idx].service_stamp().va(),' in c2ta_slice, c2ta_slice
assert 'self.graph.compute[p_idx].service_stamp().va(),' in c2fs_slice, c2fs_slice
assert 'fw_stamp' not in c2ta_slice and 'fw_stamp' not in c2fs_slice
graph_src = (production.parent / 'graph.rs').read_text()
# One accessor per service-stamp cell: the work item names it (where the firmware stores), retirement
# reads it, and cross-engine barriers wait on it. Nothing else may name the cell, so the three can
# never diverge (a barrier on a cell no longer written would hang the queue).
poll_src = (production.parent / 'poll.rs').read_text()
for cell, accessor in (('&self.user_stamp, 0', 'fn service_stamp(&self) -> StampCell'),
('&self.user_stamp[0], 0', 'fn ta_service_stamp(&self) -> StampCell'),
('&self.user_stamp[1], 0', 'fn fs_service_stamp(&self) -> StampCell')):
assert graph_src.count(cell) == 1, cell
body = graph_src[graph_src.index(accessor):]
body = body[:body.index('\n }\n')]
assert cell in body, (cell, body)
# Work items and barriers name the cells only through the accessors.
for va in ('user_stamp.va', 'user_stamp[0].va', 'user_stamp[1].va'):
assert va not in source, va
assert source.count('user_stamp: g.service_stamp().va(),') == 1
assert source.count('user_stamp: [r.ta_service_stamp().va(), r.fs_service_stamp().va()],') == 2
assert poll_src.count('g.service_stamp().read()?') == 2
assert poll_src.count('g.fs_service_stamp().read()?') == 2
assert poll_src.count('g.ta_service_stamp().read()?') == 2
# Cross-pipe compute->compute barriers wait on the producer pipe's service stamp as well.
c2c_slice = prefix_compute_fn[
prefix_compute_fn.index('for (p_idx, wait_opt) in c_waits.iter().enumerate()'):
prefix_compute_fn.index('s.barrier.write(off, &bar)?;')
]
assert 'self.graph.compute[p_idx].service_stamp().va(),' in c2c_slice, c2c_slice
assert 'fw_stamp.va' not in c2c_slice, c2c_slice
assert 'render_bar,' in c2ta_slice and 'BarrierType::Hold' not in c2ta_slice
assert 'render_bar,' in c2fs_slice and 'BarrierType::Hold' not in c2fs_slice
assert 'BarrierType::Stamp,' in prefix_render_fn[prefix_render_fn.index('let own = encode::barrier'):prefix_render_fn.index('p.own_barrier.write')]
assert 'cross_engine_barrier_type(producer_initbm, false)' in fs2c_slice
# Render->compute (fs2c) waits on the render FS service stamp, not FINALIZE fw_stamp[1].
fs2c_bar = prefix_compute_fn[
prefix_compute_fn.index('let fs2c_bar ='):prefix_compute_fn.index('s.barrier.write(0, &bar)?;')
]
assert 'self.graph.render.fs_service_stamp().va(),' in fs2c_bar, fs2c_bar
assert 'fw_stamp[1].va' not in fs2c_bar, fs2c_bar

assert 'bar_type,' in fs2c_slice and 'BarrierType::Hold' not in fs2c_slice
assert 'let waits_for_timestamps = c.query_access' in can_queue_behind_fn
assert 'DRM_ASAHI_FEATURE_RENDER_STAGE_PREFIXES_V2' in params_src
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4655,6 +4655,8 @@ def module(text, name):
graph_types = '\n'.join(
item(graph_source, decl)
for decl in (
"pub(crate) struct StampCell<'a> {",
"impl<'a> StampCell<'a> {",
'pub(crate) struct ComputeGraph {',
'struct QueueSession {',
'impl QueueSession {',
Expand Down
19 changes: 19 additions & 0 deletions tools/testing/selftests/drivers/gpu/asahi-m4/test-stamp-wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def function(text, name):
mod fw { pub mod graph { pub use crate::manager; } }
#[derive(Default)]struct CGraph{fw_stamp:Object,qstate:Object,qinfo:Object,notifier:Object,user_stamp:Object}
struct RGraph{fw_stamp:[Object;2],qstate:[Object;2],qinfo:[Object;2],user_stamp:[Object;2],notifier:Object}
// Production service-stamp accessors (graph.rs) over the mock objects: retirement reads the cells
// through exactly the accessors that work items and cross-engine barriers name them by.
type FwObject=Object;
__STAMP_CELL__
impl CGraph{__COMPUTE_SERVICE_STAMP__}
impl RGraph{__TA_SERVICE_STAMP__ __FS_SERVICE_STAMP__}
struct Graph{compute:[CGraph;hw::sizes::COMPUTE_PIPES],render:RGraph}
struct Vm{slot:u32}struct Job{vm:Vm,resources:Resources}
struct SharedComputeUma{retired:AtomicU64,counter:Object,descriptor:Object}
Expand Down Expand Up @@ -853,6 +859,19 @@ def function(text, name):
if token == '__COMPUTE_UMA_RETIRED__':
body = body.replace('super::fw::', 'fw::')
harness=harness.replace(token, body)
graph_text=(backend/'graph.rs').read_text()
cell_start=graph_text.index('/// A 32-bit stamp cell')
cell_impl=graph_text.index("impl<'a> StampCell<'a> {")
cell_at=graph_text.index('{',cell_impl)
depth,cell_end=1,cell_at+1
while depth:
depth+=(graph_text[cell_end]=='{')-(graph_text[cell_end]=='}')
cell_end+=1
harness=(harness
.replace('__STAMP_CELL__',graph_text[cell_start:cell_end])
.replace('__COMPUTE_SERVICE_STAMP__',function(graph_text,'service_stamp'))
.replace('__TA_SERVICE_STAMP__',function(graph_text,'ta_service_stamp'))
.replace('__FS_SERVICE_STAMP__',function(graph_text,'fs_service_stamp')))
hw_text=(backend/'hw.rs').read_text()
def mod_block(text, name):
start=text.index('pub(crate) mod '+name+' {')
Expand Down