Skip to content
Draft
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
11 changes: 10 additions & 1 deletion drivers/gpu/drm/asahi/g17/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ pub(crate) struct ManagerArgs {
pub(crate) buffer_id: u8,
pub(crate) generation: u64,
pub(crate) first_ordinal: u64,
/// The heap's normal maximum in blocks (`Variant::tvb_normal_blocks`); NEO heaps only.
pub(crate) normal_blocks: usize,
}

/// A retained parameter-buffer graph and all of its sparse backing. Client
Expand Down Expand Up @@ -793,7 +795,13 @@ impl Manager {
) -> Result<Self> {
let geometry = tvb.geometry;
let neo = geometry == BlockGeometry::NEO;
let layout = if neo { layout::Layout::NEO } else { layout::Layout::m5(geometry)? };
let mut layout = if neo { layout::Layout::NEO } else { layout::Layout::m5(geometry)? };
if neo {
if args.normal_blocks == 0 || args.normal_blocks > geometry.max_blocks() {
return Err(EINVAL);
}
layout.normal_blocks = args.normal_blocks;
}
if metrics.scene_stride != layout.metric_stride {
return Err(EINVAL);
}
Expand Down Expand Up @@ -827,6 +835,7 @@ impl Manager {
discard: args.discard,
buffer_id: args.buffer_id.into(),
generation: args.generation,
normal_blocks: layout.normal_blocks,
};
if neo {
graph.initialize::<fw::Graph>(0, |view| view.init(&graph_args))?;
Expand Down
230 changes: 220 additions & 10 deletions drivers/gpu/drm/asahi/g17/buffer/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ const GTP_FIXED_SIZES: [usize; 4] = [0xc00, 0xa00, GTP6_SIZE, 0x80];
const GTP_SLOTS: usize = 64;
const GTP6_SIZE: usize = GTP_SLOTS
* (((cfg::TVB_MAX_BLOCKS * cfg::TVB_BLOCK_SIZE).div_ceil(0x8000) + 0x17ff) / 24 * 8 + 8);
/// Clustering metadata 2-4 (0x1c948, 0x1c888, 0x16c41/0x1ca40) on GPUs with more than two
/// clusters, at the sizes the M2 Pro/Max path gives them (`hw/t602x.rs` clustering config).
/// Metadata 1 (0x1c920) is sized per target there: [`META1_BLOCK_SIZE`] per block of tiles.
///
/// macOS 26.6.2 on the J616C spaces a clustered pass's metadata 2, 3 and 4 0x1180, 0x1400
/// and 0x1600 apart: metadata 2 needs 0x1180, more than the M2 Pro/Max 0xc00.
const GTP_MULTI_CLUSTER_SIZES: [usize; 3] = [0x1180, 0x280 * 16, 0x10 * 128];
/// Floor for four-cluster metadata 1 (`0x1c920`, `gtp[7]`).
///
/// The M2 Pro/Max per-block formula yields 0x100 (32x32 utiles) or 0x180 (16x16) on a
/// 3456x2234 Retina frame (Minecraft RD32) and 0x1100 on 10240x10240 (`point_size_10000`,
/// 16x16 utiles). Because metadata 1 is the last sub-buffer in the TPC object, neither
/// macOS's allocation size nor the hardware's written high-water mark was visible in the
/// J616C 26.6.2 capture; keep a conservative 16 KiB floor until a capture or hardware
/// high-water measurement shows the true upper bound.
const META1_MIN_SIZE: usize = 0x4000;
const META1_BLOCK_SIZE: usize = 0x44;

/// Per-command geometry, independent of the capacity already allocated.
#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -116,15 +133,38 @@ impl Layout {
let mut gtp = [None; 8];
let mut tpc_end = tpc_size;
if programs == RegisterPrograms::V25G83 {
// The fixed sizes are the two-cluster M4 Pro's. With four clusters (M4 Max) clustered
// tiling outgrows them: the firmware then never starts a heavy render and kills it as
// no-progress (reason 0). Size them as the M2 Pro/Max path does.
let [meta2, meta3, meta4, meta1] = if clusters > 2 {
let meta1_blocks = aligned(tiles_x, 2)?
.checked_mul(aligned(tiles_y, 4)?)
.and_then(|v| v.checked_mul(utiles))
.ok_or(EOVERFLOW)?
.div_ceil(0x1980);
[
GTP_MULTI_CLUSTER_SIZES[0],
GTP_MULTI_CLUSTER_SIZES[1],
// Partial renders need the larger size here too.
GTP_MULTI_CLUSTER_SIZES[2].max(GTP6_SIZE),
aligned(
meta1_blocks.checked_mul(META1_BLOCK_SIZE).ok_or(EOVERFLOW)?,
0x80,
)?
.max(META1_MIN_SIZE),
]
} else {
GTP_FIXED_SIZES
};
let sizes = [
tpc_size,
tilemap_size.checked_mul(64).ok_or(EOVERFLOW)?,
if layers > 1 { 0x4000 } else { 0 },
0,
GTP_FIXED_SIZES[0],
GTP_FIXED_SIZES[1],
GTP_FIXED_SIZES[2],
GTP_FIXED_SIZES[3],
meta2,
meta3,
meta4,
meta1,
];
let mut position = 0;
for ((offset, size), alignment) in gtp.iter_mut().zip(sizes).zip(GTP_ALIGNMENTS) {
Expand Down Expand Up @@ -183,7 +223,31 @@ impl Layout {
{
return Err(ERANGE);
}
Ok(aligned(self.tpc_end, mmu::UAT_PGSZ)?.max(cfg::TPC_MIN))
Ok(aligned(self.shared_end(), mmu::UAT_PGSZ)?.max(cfg::TPC_MIN))
}
/// End of the shared part, the TPC and GTP 1-3: GTP 4, where the metadata starts. GTP 4
/// need not be page aligned, and the tile maps run up to it.
fn shared_end(&self) -> usize {
match (self.m5, self.gtp[4]) {
(None, Some(start)) => start,
_ => self.tpc_end,
}
}
/// One render's clustering-metadata copy, page aligned; zero when there is none.
fn meta_capacity(&self) -> Result<usize> {
aligned(self.meta_bytes(), mmu::UAT_PGSZ)
}
/// Start of the per-render part: clustering metadata (GTP 4-7) begins at GTP 4, rounded
/// down to a page so every sub-buffer keeps its alignment in a page-aligned copy.
fn meta_base(&self) -> usize {
match (self.m5, self.gtp[4]) {
(None, Some(start)) => start / mmu::UAT_PGSZ * mmu::UAT_PGSZ,
_ => self.tpc_end,
}
}
/// Bytes of one render's metadata copy, zero without clustering metadata.
fn meta_bytes(&self) -> usize {
self.tpc_end - self.meta_base()
}
pub(crate) fn record_stride(&self) -> Result<usize> {
let bytes = aligned(self.used, mmu::UAT_PGSZ)?;
Expand Down Expand Up @@ -227,7 +291,14 @@ pub(crate) struct Addresses {

pub(crate) struct Target {
records: Object,
/// The TPC and tile-map sub-buffers, shared by every render of the target.
tpc: Object,
tpc_stride: usize,
/// One zeroed clustering-metadata copy (GTP 4-7) per in-flight render, like the records.
/// Only these are copied: a copy of the whole TPC object per render (32 in flight) filled
/// the client's target window once a game kept several large targets.
meta: Option<Object>,
meta_stride: usize,
stride: usize,
slots: [Option<u64>; DEPTH],
retired: KVec<Retired>,
Expand All @@ -242,11 +313,21 @@ impl Target {
layout.check_capacity(stride)?;
let m5 = layout.m5.is_some();
let records = Object::new(dev, vm, stride * DEPTH, m5)?;
let tpc_stride = tpc;
let tpc = Object::new(dev, vm, tpc, m5)?;
let meta_stride = layout.meta_capacity()?;
let meta = if meta_stride == 0 {
None
} else {
Some(Object::new(dev, vm, meta_stride.checked_mul(DEPTH).ok_or(EOVERFLOW)?, m5)?)
};
KBox::new(
Self {
records,
tpc,
tpc_stride,
meta,
meta_stride,
stride,
slots: [None; DEPTH],
retired: KVec::new(),
Expand All @@ -256,6 +337,10 @@ impl Target {
)
.map_err(Into::into)
}
fn meta_offset(&self, ordinal: u64) -> usize {
(ordinal as usize % DEPTH) * self.meta_stride
}

pub(crate) fn tpc_va(&self) -> u64 {
self.tpc.address
}
Expand Down Expand Up @@ -283,14 +368,24 @@ impl Target {

/// GPU addresses of the tiler parameter sub-buffers of `layout` (25G83), zero for an empty
/// or absent one. The TPC object holds them after the TPC.
pub(crate) fn gtp_va(&self, layout: &Layout) -> Result<[u64; 8]> {
if layout.tpc_end > self.tpc.backing.size() {
pub(crate) fn gtp_va(&self, ordinal: u64, layout: &Layout) -> Result<[u64; 8]> {
let split = layout.meta_base();
if layout.shared_end() > self.tpc_stride || layout.meta_bytes() > self.meta_stride {
return Err(ERANGE);
}
let meta = match &self.meta {
Some(meta) => meta.address.checked_add(self.meta_offset(ordinal) as u64).ok_or(EOVERFLOW)?,
None => 0,
};
let mut addresses = [0; 8];
for (address, offset) in addresses.iter_mut().zip(layout.gtp) {
if let Some(offset) = offset {
*address = self.tpc.address.checked_add(offset as u64).ok_or(EOVERFLOW)?;
*address = if offset < split {
self.tpc.address.checked_add(offset as u64).ok_or(EOVERFLOW)?
} else {
if meta == 0 { return Err(ERANGE); }
meta.checked_add((offset - split) as u64).ok_or(EOVERFLOW)?
};
}
}
Ok(addresses)
Expand All @@ -315,6 +410,25 @@ impl Target {
.get_mut(offset..offset + span)
.ok_or(ERANGE)?
.fill(0);
// Clustered tiling expects zeroed clustering metadata (GTP 4-7), as G15 gives every
// clustered pass a zeroed buffer. Zero this render's own copy.
if !self.m5 {
if let Some(start) = layout.gtp[4] {
let split = layout.meta_base();
if layout.meta_bytes() > self.meta_stride {
return Err(ERANGE);
}
let base = self.meta_offset(ordinal);
self.meta
.as_mut()
.ok_or(ERANGE)?
.backing
.bytes_mut()
.get_mut(base + start - split..base + layout.tpc_end - split)
.ok_or(ERANGE)?
.fill(0);
}
}
self.slots[slot] = Some(ordinal);
let tilemap = self
.records
Expand Down Expand Up @@ -346,8 +460,19 @@ impl Target {
if self.m5 != layout.m5.is_some() { return Err(EINVAL); }
layout.check_capacity(stride)?;
let mut grew = false;
if tpc > self.tpc.backing.size() {
if tpc > self.tpc_stride {
Self::replace(&mut self.retired, &mut self.tpc, dev, vm, tpc, owners, self.m5)?;
self.tpc_stride = tpc;
grew = true;
}
let meta = layout.meta_capacity()?;
if meta > self.meta_stride {
let size = meta.checked_mul(DEPTH).ok_or(EOVERFLOW)?;
match self.meta.as_mut() {
Some(old) => Self::replace(&mut self.retired, old, dev, vm, size, owners, self.m5)?,
None => self.meta = Some(Object::new(dev, vm, size, self.m5)?),
}
self.meta_stride = meta;
grew = true;
}
if stride > self.stride {
Expand Down Expand Up @@ -448,9 +573,70 @@ mod tests {
assert!(layout.tpc_size < cfg::TPC_MAX);
assert!(layout.record_stride().unwrap() < cfg::TARGET_RECORD_MAX);
assert!(layout.tpc_end > cfg::TPC_MAX);
assert!(layout.tpc_capacity().unwrap() >= layout.tpc_end);
assert_eq!(layout.gtp[0], Some(0));
assert!(layout.gtp[1].unwrap() >= layout.tpc_size);
fits(&layout);
}
}

/// Every sub-buffer of `layout` lies whole in its object with its alignment: the TPC and
/// GTP 1-3 in the shared TPC object, GTP 4-7 in a render's metadata copy.
fn fits(layout: &Layout) {
let shared = layout.tpc_capacity().unwrap();
let split = layout.meta_base();
// GTP 0-3 sizes, as Layout::new gives them; GTP 2 exists for layered targets only.
let sizes = [layout.tpc_size, layout.tilemap_size * 64, 0x4000, 0];
for (index, size) in sizes.into_iter().enumerate() {
if let Some(offset) = layout.gtp[index] {
assert!(offset < split);
assert!(offset + size <= shared);
}
}
assert!(layout.meta_capacity().unwrap() >= layout.tpc_end - split);
for (offset, alignment) in layout.gtp.iter().zip(GTP_ALIGNMENTS).skip(4) {
assert!(offset.unwrap() >= split);
assert_eq!((offset.unwrap() - split) % alignment, 0);
}
}

#[test]
fn only_clustering_metadata_is_copied_per_render() {
// 1728x1117 on the four-cluster M4 Max: the per-render copy is the metadata alone,
// not the TPC and the megabyte of tile maps.
let mut p = pass(1728, 32);
p.height = 1117;
let layout = Layout::new(&p, 4, RegisterPrograms::V25G83).unwrap();
let start = layout.gtp[4].unwrap();
assert!(layout.meta_base() <= start && start - layout.meta_base() < mmu::UAT_PGSZ);
// Metadata 4 (GTP 6) has room for 64 partial renders: 0x65e00 bytes.
assert!(layout.meta_capacity().unwrap() <= 0x8_0000);
assert!(layout.meta_capacity().unwrap() < layout.tilemap_size * 64);
fits(&layout);
}

#[test]
fn the_shared_object_holds_the_last_tile_map() {
// With 32x32 utiles at 3456x2234 the TPC is 0x46000 bytes, so GTP 4 is not page
// aligned and the tile maps end past GTP 4 rounded down to a page.
let mut p = pass(3456, 32);
p.height = 2234;
let layout = Layout::new(&p, 4, RegisterPrograms::V25G83).unwrap();
assert_ne!(layout.gtp[4].unwrap() % mmu::UAT_PGSZ, 0);
let end = layout.gtp[1].unwrap() + layout.tilemap_size * 64;
assert!(end > aligned(layout.meta_base(), mmu::UAT_PGSZ).unwrap());
assert!(layout.tpc_capacity().unwrap() >= end);
for (width, height) in [(1728, 1117), (3456, 2234), (2048, 2048), (1280, 720)] {
for (utile_width, utile_height) in [(32, 32), (32, 16), (16, 16)] {
for layers in [1, 2] {
for clusters in [2, 4] {
let mut p = pass(width, utile_width);
p.height = height;
p.utile_height = utile_height;
p.layers = layers;
fits(&Layout::new(&p, clusters, RegisterPrograms::V25G83).unwrap());
}
}
}
}
}

Expand All @@ -476,4 +662,28 @@ mod tests {
layout.tpc_end = layout.tpc_size;
assert_eq!(layout.tpc_capacity(), Err(ERANGE));
}

#[test]
fn four_cluster_v25g83_metadata_layout() {
let mut retina = pass(3456, 32);
retina.height = 2234;
for (target, utile) in [(retina, 32), (retina, 16), (pass(10240, 16), 16)] {
let mut p = target;
p.utile_width = utile;
p.utile_height = utile;
let layout = Layout::new(&p, 4, RegisterPrograms::V25G83).unwrap();
let gtp4 = layout.gtp[4].unwrap();
let gtp5 = layout.gtp[5].unwrap();
let gtp6 = layout.gtp[6].unwrap();
let gtp7 = layout.gtp[7].unwrap();
assert_eq!(gtp5 - gtp4, GTP_MULTI_CLUSTER_SIZES[0]);
assert!(gtp6 - gtp5 >= GTP_MULTI_CLUSTER_SIZES[1]);
assert!(gtp7 - gtp6 >= GTP_MULTI_CLUSTER_SIZES[2].max(GTP6_SIZE));
assert_eq!(layout.tpc_end - gtp7, META1_MIN_SIZE);
}
// Two-cluster V25G83 (M4 Pro) keeps its original fixed metadata sizes.
let two = Layout::new(&retina, 2, RegisterPrograms::V25G83).unwrap();
assert_eq!(two.gtp[5].unwrap() - two.gtp[4].unwrap(), GTP_FIXED_SIZES[0]);
assert_eq!(two.tpc_end - two.gtp[7].unwrap(), GTP_FIXED_SIZES[3]);
}
}
6 changes: 5 additions & 1 deletion drivers/gpu/drm/asahi/g17/fw/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub(crate) const SIZE: usize = 0x48000;
pub(crate) const METRICS_STRIDE: usize = SCENES * size_of::<u32>();
pub(crate) const METRICS_SLOTS: usize = cfg::METRICS_SIZE as usize / METRICS_STRIDE;
pub(crate) const NORMAL_MAX_BLOCKS: usize = 0x45e;
/// The normal maximum of a four-cluster G17P heap: every slot of the block table.
pub(crate) const NORMAL_MAX_BLOCKS_FOUR_CLUSTERS: usize = cfg::TVB_MAX_BLOCKS;
pub(crate) const GROW_BLOCKS: usize = 10;

/// Firmware's active parameter-buffer state. Pointers at +0x44, +0x4c,
Expand Down Expand Up @@ -136,6 +138,8 @@ pub(crate) struct GraphArgs<'a> {
pub(crate) discard: u64,
pub(crate) buffer_id: u32,
pub(crate) generation: u64,
/// The heap's normal maximum in blocks (`Variant::tvb_normal_blocks`).
pub(crate) normal_blocks: usize,
}

pub(crate) fn compact_page(address: u64) -> Result<u32> {
Expand Down Expand Up @@ -197,7 +201,7 @@ impl Graph {
self.state.block_size = cfg::TVB_BLOCK_SIZE as u32;
self.state.counter = offset_va(args.fw_va, COUNTER as u64)?;
self.state.maximum_size = (cfg::TVB_BLOCK_SLOTS * 4) as u32;
self.state.normal_maximum_size = (NORMAL_MAX_BLOCKS * 4) as u32;
self.state.normal_maximum_size = (args.normal_blocks * 4) as u32;
self.state.discard = discard;
Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions drivers/gpu/drm/asahi/g17/fw/freelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ pub(crate) const RUN_LIST_SIZE: usize = 0x1_0000;
pub(crate) const TABLE_SIZE: usize = PAGE_LIST_SIZE + RUN_LIST_SIZE;
pub(crate) const CONTROL_SIZE: usize = 0x4000;
pub(crate) const RENDER_BLOCKS: usize = 44;
/// Minimum render pool per active USC core (in 1 MiB blocks) on GPUs with more
/// than two clusters. With clustered tiling running vertex work across all
/// clusters concurrently with fragment work, 44 blocks run out under Vulkan
/// vertex load (reason-0 no-progress recoveries); three blocks per enabled core
/// yield 96 blocks on 32-core T6041 and scale linearly on larger parts.
pub(crate) const MULTI_CLUSTER_RENDER_BLOCKS_PER_CORE: usize = 3;
pub(crate) const COMPUTE_BLOCKS: usize = 21;

/// Render reserves eight run words per block; compute packs runs consecutively.
Expand Down
Loading