Skip to content
Closed
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
16 changes: 9 additions & 7 deletions drivers/gpu/drm/asahi/g16/encode/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub(crate) struct RenderRefs {
pub(crate) uma_submitted: u64,
pub(crate) uuid: u32,
pub(crate) usc_base: u64,
/// `[TA, FS]` completion events of the render's queue pair (`work::render_pair::events`).
pub(crate) event: [u32; 2],
}

/// Addresses retained for one render pass.
Expand Down Expand Up @@ -149,7 +151,7 @@ fn metadata(w: &mut [u8], off: usize, r: &RenderRefs, engine: usize, target: u32
put64(w, off, r.user_stamp[engine]);
put64(w, off + 8, r.fw_stamp[engine]);
put32(w, off + 0x10, target);
put32(w, off + 0x14, 0x70 + engine as u32);
put32(w, off + 0x14, r.event[engine]);
put32(w, off + 0x1c, 1);
put32(w, off + 0x20, r.uuid);
// Wire sequence stays zero; host reservations are 64-bit notifier state.
Expand All @@ -163,7 +165,7 @@ fn inline_uma(q: &mut [u8], off: usize, r: &RenderRefs, engine: usize, engine_by
r.uma_gpu + if engine == 0 { 0x100 } else { 0x200 },
);
put64(q, off + 0x28, r.counter[engine]);
put32(q, off + 0x30, 0x70 + engine as u32);
put32(q, off + 0x30, r.event[engine]);
if engine_byte {
put8(q, off + 0x34, engine as u8);
}
Expand Down Expand Up @@ -452,7 +454,7 @@ pub(crate) fn encode_ta_rt(
put64(w, ta::TPC_BYTES, geo.tpc_bytes);
put64(w, 0x770, p.ta_seq);
put32(w, 0x778, ta::SEQ_SIZE as u32);
put32(w, 0x77c, 0x71);
put32(w, 0x77c, r.event[1]);
put32(w, 0x780, target);
put64(w, 0x7a8, compact(p.pre_gva[0]));
metadata(w, 0x878, r, 0, target);
Expand Down Expand Up @@ -890,12 +892,12 @@ pub(crate) fn encode_fs_rt(
put32(q, f + 0xa0, 0u32.wrapping_sub(f as u32));
put64(q, f + 0xa5, work + layout.completion as u64);
if layout.compact_finalize {
put32(q, f + 0xad, 0x71);
put32(q, f + 0xad, r.event[1]);
put32(q, f + 0xb4, work::op::END);
} else {
put32(q, f + 0xad, 1);
put8(q, f + 0xb1, 1);
put32(q, f + 0xb2, 0x71);
put32(q, f + 0xb2, r.event[1]);
put32(q, f + 0xbc, work::op::END);
}
}
Expand Down Expand Up @@ -977,7 +979,7 @@ pub(crate) fn scene_prefix(r: &RenderRefs, ctx: u32, target: u32, init: bool) ->
&barrier(
r.fw_stamp[0],
target,
0x70,
r.event[0],
target,
0,
work::BarrierType::Stamp,
Expand Down Expand Up @@ -1005,7 +1007,7 @@ pub(crate) fn scene_render(
&barrier(
r.fw_stamp[0],
target,
0x70,
r.event[0],
target,
0,
work::BarrierType::Stamp,
Expand Down
486 changes: 369 additions & 117 deletions drivers/gpu/drm/asahi/g16/engine.rs

Large diffs are not rendered by default.

29 changes: 19 additions & 10 deletions drivers/gpu/drm/asahi/g16/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ use kernel::{bindings, device, prelude::*};

use super::channel::Ring;
use super::fw::channels::{RING_SLOTS, STATE_READ, STATE_WRITE};
use super::fw::work::compute;
use super::fw::work::{compute, render_pair};
use super::hw::{rings, sizes};
use super::regs::{mb, rmb, wmb};

pub(crate) const RECORD_WORDS: usize = 18;
pub(crate) const STAGE_COUNT: usize = 2 + sizes::COMPUTE_PIPES;
pub(crate) const STAGE_COUNT: usize = sizes::STAGES;
const COMPUTE_EVENT_MASK: u32 = ((1u32 << sizes::COMPUTE_PIPES) - 1) << compute::EVENT;
/// TA/FS events of every render pair, in type-1 word 4.
const RENDER_EVENT_MASK: u32 =
((1u32 << (2 * sizes::RENDER_PAIRS)) - 1) << (render_pair::TA_EVENT % 32);
const _: () = assert!(render_pair::TA_EVENT / 32 == 3);
const _: () = assert!(sizes::RENDER_PAIRS <= render_pair::MAX_PAIRS);
const _: () = assert!(compute::EVENTS.len() == sizes::COMPUTE_PIPES);
const _: () = assert!(compute::EVENT as usize + sizes::COMPUTE_PIPES <= 32);
/// Retained only for the allocator's pending-growth container. M4 does not
Expand Down Expand Up @@ -129,7 +134,8 @@ impl Events {
match kind {
1 => {
// Type 1 carries 128 completion bits across words 1..=4; compute pipe events
// (15..15+COMPUTE_PIPES) live in word 1 and TA/FS (0x70/0x71) live in word 4.
// (15..15+COMPUTE_PIPES) live in word 1 and the TA/FS events of every render
// pair (0x70 + 2p, 0x71 + 2p) live in word 4.
let (w1, w4) = match ring.ring.r32(base + 4).and_then(|w1| {
Ok((w1, ring.ring.r32(base + 16)?))
}) {
Expand All @@ -141,14 +147,17 @@ impl Events {
return Err(err);
}
};
if w4 & (1u32 << (0x70 % 32)) != 0 {
self.event_ns[0] = now;
let mut rbits = w4 & RENDER_EVENT_MASK;
while rbits != 0 {
let bit = rbits.trailing_zeros();
rbits &= rbits - 1;
let i = (bit - render_pair::TA_EVENT % 32) as usize;
let (pair, stage) = (i / 2, i % 2);
self.event_ns[sizes::render_stage(pair, stage)] = now;
self.stage_events = self.stage_events.wrapping_add(1);
}
if w4 & (1u32 << (0x71 % 32)) != 0 {
self.event_ns[1] = now;
self.stage_events = self.stage_events.wrapping_add(1);
self.fs_events = self.fs_events.wrapping_add(1);
if stage == 1 {
self.fs_events = self.fs_events.wrapping_add(1);
}
}
let mut cbits = w1 & COMPUTE_EVENT_MASK;
while cbits != 0 {
Expand Down
26 changes: 26 additions & 0 deletions drivers/gpu/drm/asahi/g16/fw/work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,32 @@ pub(crate) mod barrier {
pub(crate) const RENDER_WAIT_UUID: u32 = 0x3d420;
}

/// Completion events of the render queue pairs. Pair `p` finishes its TA work with event
/// `0x70 + 2p` and its FS work with `0x71 + 2p`; pair 0 keeps the 0x70/0x71 of the native
/// runtime's render queues. The block `0x70..0x80` is bits 16..32 of type-1 event word 4, so it
/// names at most [`MAX_PAIRS`] pairs.
pub(crate) mod render_pair {
pub(crate) const TA_EVENT: u32 = 0x70;
pub(crate) const MAX_PAIRS: usize = 8;
const _: () = assert!(TA_EVENT % 32 == 16 && TA_EVENT % 32 + 2 * MAX_PAIRS as u32 == 32);

/// `[TA, FS]` completion events of render pair `pair`.
pub(crate) const fn events(pair: usize) -> [u32; 2] {
let ta = TA_EVENT + 2 * pair as u32;
[ta, ta + 1]
}

/// The render pair (`0..MAX_PAIRS`) and stage (0 TA, 1 FS) of a completion event.
pub(crate) const fn of_event(event: u32) -> Option<(usize, usize)> {
if event >= TA_EVENT && event - TA_EVENT < 2 * MAX_PAIRS as u32 {
let i = (event - TA_EVENT) as usize;
Some((i / 2, i % 2))
} else {
None
}
}
}

/// InitBM record (0x20 bytes).
pub(crate) mod initbm {
pub(crate) const SIZE: usize = 0x20;
Expand Down
74 changes: 50 additions & 24 deletions drivers/gpu/drm/asahi/g16/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,26 +293,41 @@ struct RenderGraphSession {
registration_barrier: Arc<FwObject>,
}
impl RenderGraphSession {
fn new(o: &mut Objects, context: Arc<FwObject>) -> Result<Self> {
/// Pairs after the first share `first`'s `legacy_empty` and `registration_barrier`: the
/// shared scheduling context registers once, through pair 0.
fn new(
o: &mut Objects,
context: Arc<FwObject>,
first: Option<&RenderGraphSession>,
) -> Result<Self> {
// Pair 0 keeps the allocation order of the single render graph.
let ta = QueueSession::new(o)?;
let fs = QueueSession::new(o)?;
let notifier = alloc(o, 0x1000, Class::Fw, None, 0, false)?;
let threshold = alloc(o, 24, Class::Fw, None, 0, false)?;
let job_list = [
alloc(o, job_list::SIZE, graph_fw(), None, 0, false)?,
alloc(o, job_list::SIZE, graph_fw(), None, 0, false)?,
];
let (legacy_empty, registration_barrier) = match first {
Some(first) => (
first.legacy_empty.clone(),
first.registration_barrier.clone(),
),
None => (
alloc(o, 0x40, Class::Shared, None, 0, false)?,
alloc(o, super::fw::work::barrier::SIZE, cmd_fw(), None, 0, false)?,
),
};
Ok(Self {
ta: QueueSession::new(o)?,
fs: QueueSession::new(o)?,
ta,
fs,
sched_ctx: context,
notifier: alloc(o, 0x1000, Class::Fw, None, 0, false)?,
threshold: alloc(o, 24, Class::Fw, None, 0, false)?,
job_list: [
alloc(o, job_list::SIZE, graph_fw(), None, 0, false)?,
alloc(o, job_list::SIZE, graph_fw(), None, 0, false)?,
],
legacy_empty: alloc(o, 0x40, Class::Shared, None, 0, false)?,
registration_barrier: alloc(
o,
super::fw::work::barrier::SIZE,
cmd_fw(),
None,
0,
false,
)?,
notifier,
threshold,
job_list,
legacy_empty,
registration_barrier,
})
}
fn finish(self, rings: &mut Objects) -> Result<RenderGraph> {
Expand All @@ -337,7 +352,7 @@ impl RenderGraphSession {
impl RenderGraph {
#[allow(dead_code)]
fn new(o: &mut Objects, rings: &mut Objects, context: Arc<FwObject>) -> Result<Self> {
RenderGraphSession::new(o, context)?.finish(rings)
RenderGraphSession::new(o, context, None)?.finish(rings)
}
fn init(&self, abi: FwAbi) -> Result {
init_notifier(&self.notifier, &self.threshold)?;
Expand All @@ -362,13 +377,14 @@ impl RenderGraph {
/// [`Factory::persistent_objects_probe`] pool.
pub(crate) struct GraphSession {
compute: KVec<ComputeGraphSession>,
render: RenderGraphSession,
render: KVec<RenderGraphSession>,
}

pub(crate) struct Graphs {
pub(crate) dev: ARef<device::Device>,
pub(crate) compute: KVec<ComputeGraph>,
pub(crate) render: RenderGraph,
/// TA/FS render queue pairs `0..RENDER_PAIRS`.
pub(crate) render: KVec<RenderGraph>,
/// Owns the firmware-cached work rings for the device lifetime (never dropped).
_ring_objects: core::mem::ManuallyDrop<Objects>,
}
Expand All @@ -382,7 +398,12 @@ impl Graphs {
for _ in 0..sizes::COMPUTE_PIPES {
compute.push(ComputeGraphSession::new(o, sched_ctx.clone())?, GFP_KERNEL)?;
}
let render = RenderGraphSession::new(o, sched_ctx)?;
let mut render: KVec<RenderGraphSession> =
KVec::with_capacity(sizes::RENDER_PAIRS, GFP_KERNEL)?;
for _ in 0..sizes::RENDER_PAIRS {
let pair = RenderGraphSession::new(o, sched_ctx.clone(), render.first())?;
render.push(pair, GFP_KERNEL)?;
}
Ok(GraphSession { compute, render })
}

Expand All @@ -398,7 +419,10 @@ impl Graphs {
for c in session.compute {
compute.push(c.finish(&mut ring_objects)?, GFP_KERNEL)?;
}
let render = session.render.finish(&mut ring_objects)?;
let mut render = KVec::with_capacity(sizes::RENDER_PAIRS, GFP_KERNEL)?;
for r in session.render {
render.push(r.finish(&mut ring_objects)?, GFP_KERNEL)?;
}
ring_objects.finish_private()?;
Ok(Self {
dev: dev.as_ref().as_ref().into(),
Expand All @@ -419,7 +443,9 @@ impl Firmware {
for (pipe, compute) in self.graph.compute.iter().enumerate() {
compute.init(self.init.abi, pipe == 0)?;
}
self.graph.render.init(self.init.abi)?;
for render in self.graph.render.iter() {
render.init(self.init.abi)?;
}
self.slots.init()?;
self.init_render_pools(false)?;
super::engine::wmb();
Expand Down
26 changes: 26 additions & 0 deletions drivers/gpu/drm/asahi/g16/hw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,32 @@ pub(crate) mod sizes {
/// Maximum concurrent compute pipes one VM may occupy at once, preserving the render
/// ring-entry bound (`MAX_RENDER_RING_ENTRIES = 5`) since cross-VM render/compute drains.
pub(crate) const MAX_VM_COMPUTE_PIPES: usize = 2;
/// Firmware TA/FS render queue pairs, each leased to one rendering VM at a time
/// (`Firmware::render_pair_for`): two VMs' renders on one pair fault (linux#3201), on two
/// pairs they run side by side, as macOS 26B123 gives every Metal process its own TA and FS
/// queue. Each pair owns a disjoint `RenderGraph` (TA and FS `qinfo`, work rings, `qstate`,
/// stamps, job lists, notifier page, threshold: fourteen objects of one 16 KiB page each,
/// 224 KiB, allocated at probe) and its own completion events (`work::render_pair::events`);
/// all pairs share `sched_ctx`, the `Channel::Ta`/`Channel::Fs` submit rings and their
/// doorbells, as the macOS processes share one doorbell.
///
/// Eight: as many as the completion-event block names (`work::render_pair::MAX_PAIRS`), so
/// a compositor and seven clients render without a pair handoff. A pair registers with the
/// firmware on its first record, and a VM takes the lowest free pair, so pairs beyond the
/// clients rendering at once stay unregistered.
pub(crate) const RENDER_PAIRS: usize = 8;
const _: () = assert!(RENDER_PAIRS >= 1 && RENDER_PAIRS <= 8);
/// Per-stage event and recovery-snapshot cells: pair 0 TA/FS at `0`/`1`, compute pipes at
/// `2..2 + COMPUTE_PIPES`, then TA/FS of pairs `1..RENDER_PAIRS` ([`render_stage`]).
pub(crate) const STAGES: usize = 2 + COMPUTE_PIPES + 2 * (RENDER_PAIRS - 1);
/// The [`STAGES`] cell of render pair `pair`'s TA (`stage` 0) or FS (`stage` 1).
pub(crate) const fn render_stage(pair: usize, stage: usize) -> usize {
if pair == 0 {
stage
} else {
2 + COMPUTE_PIPES + 2 * (pair - 1) + stage
}
}
const _: () = assert!(COMPUTE_PIPES >= 2 && COMPUTE_PIPES <= 16);
const _: () = assert!(MAX_VM_COMPUTE_PIPES >= 2 && MAX_VM_COMPUTE_PIPES <= COMPUTE_PIPES);
const _: () = assert!(COMPUTE_UNITS - PIPE1_COMPUTE_START >= (COMPUTE_PIPES - 1) * 2);
Expand Down
Loading