From f090d650702bda926fdc3831c6086ec5e7b4e0a2 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 17 Nov 2026 19:10:06 -0300 Subject: [PATCH] drm/asahi: g17: scale the tiler heap's normal maximum with memory The tiler heap of a 25G83 render grows on the host only: the driver does not let the firmware grow it mid-render, as macOS doesn't for a pass that can partial-render. The host grows it before a render to three times the last high-water, capped at the heap's normal maximum, which also goes to the firmware in BM+0x80. A scene larger than that cap partial-renders on every frame. That cap is 0x45e (1,118 blocks, 140 MiB) on every NEO-layout heap. It is macOS's value on an 8 GiB Mac: macOS 25G83 scales the limits with memory (halGetDefaultParameterBufferSizeMax). The maximum is a twentieth of the memory, at least 192 MiB, in whole groups of 129 blocks, and a queue's pre-render growth and BM+0x80 are capped at a third of it. That gives 3,354 and 1,118 blocks with 8 GiB, the A18 Pro's TVB_BLOCK_SLOTS and NORMAL_MAX_BLOCKS, and 19,737 and 6,579 with 48 GiB, the BM+0x7c and BM+0x80 macOS writes on a 48 GiB M4 Pro (J616s). Derive the normal maximum from memory the same way, up to the block table's TVB_MAX_BLOCKS. The memory is si_meminfo()'s, as for the M5 heap (shared helper). It is below the installed amount macOS reads, so the 8 GiB value stays the floor and the A18 Pro keeps 1,118. A 24 GiB Mac gets 3,311, 32 GiB or more 3,353, the value #2021 sets for the M4 Max by hand. The A18 Pro's mesh floor keeps 1,118. On the J616s (M4 Pro, 48 GiB, 25G83), an off-screen GLES test draws N screen-spanning triangles into a stored 3456x2234 RGBA8 + D24S8 target and waits for each frame. Before, a triangle cost 3.0 us up to N = 14,500 and 9.7 us from N = 15,000, on every frame. With this change, 3.0 us holds up to N = 40,000: N = 20,000 takes 55 ms a frame instead of 193 ms, N = 30,000 83 ms instead of 281 ms. Above 3,353 blocks it partial-renders as before. macOS on the same Mac keeps its 2.9 us up to its 6,579-block cap. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM --- drivers/gpu/drm/asahi/g17/buffer.rs | 23 ++++++-- drivers/gpu/drm/asahi/g17/buffer/layout.rs | 54 ++++++++++++++++++- drivers/gpu/drm/asahi/g17/fw/buffer.rs | 5 +- .../g17/queue/render/m5_render_submission.rs | 7 +-- 4 files changed, 77 insertions(+), 12 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/buffer.rs b/drivers/gpu/drm/asahi/g17/buffer.rs index 2a3eadaafef40..9dbf5e5152980 100644 --- a/drivers/gpu/drm/asahi/g17/buffer.rs +++ b/drivers/gpu/drm/asahi/g17/buffer.rs @@ -315,6 +315,16 @@ impl Growth { } } +/// Usable memory, the input of the RAM-scaled tiler-heap limits. +pub(crate) fn memory_bytes() -> Result { + let mut info = core::mem::MaybeUninit::::zeroed(); + // SAFETY: The complete sysinfo is initialized and writable. + unsafe { kernel::bindings::si_meminfo(info.as_mut_ptr()); } + // SAFETY: The object was initialized before si_meminfo filled it. + let info = unsafe { info.assume_init() }; + (info.totalram as u64).checked_mul(u64::from(info.mem_unit)).ok_or(EOVERFLOW) +} + /// The high-water sample estimates capacity, not current occupancy. A growth /// reserves three times the demand only once twice that demand no longer fits. fn automatic_target_for(used_pages: usize, current: usize, pages: usize, maximum: usize) -> Option { @@ -768,7 +778,11 @@ impl Manager { ) -> Result { let geometry = tvb.geometry; let neo = geometry == BlockGeometry::NEO; - let layout = if neo { layout::Layout::NEO } else { layout::Layout::m5(geometry)? }; + let layout = if neo { + layout::Layout::neo(memory_bytes()?) + } else { + layout::Layout::m5(geometry)? + }; if metrics.scene_stride != layout.metric_stride { return Err(EINVAL); } @@ -802,6 +816,7 @@ impl Manager { discard: args.discard, buffer_id: args.buffer_id.into(), generation: args.generation, + normal_blocks: layout.normal_blocks, }; if neo { graph.initialize::(0, |view| view.init(&graph_args))?; @@ -854,12 +869,12 @@ impl Manager { self.tvb.geometry.max_blocks() } /// The blocks an A18 Pro heap holds ahead of a render with mesh shading - /// (`aurora.g17_mesh_tvb_floor`): the size its automatic growth reaches. - /// None on other heaps, or with the switch off. + /// (`aurora.g17_mesh_tvb_floor`): 1,118 blocks, the normal maximum of the 8 GiB A18 Pro + /// (the size its automatic growth reaches). None on other heaps, or with the switch off. pub(crate) fn mesh_floor(&self) -> Option { // SAFETY: Reads a module parameter; no other effect. let enabled = unsafe { asahi_g17_mesh_tvb_floor() } != 0; - (enabled && self.tvb.geometry == BlockGeometry::NEO).then_some(self.layout.normal_blocks) + (enabled && self.tvb.geometry == BlockGeometry::NEO).then_some(fw::NORMAL_MAX_BLOCKS) } /// Read-only diagnostic while firmware is halted. In particular this /// does not reset the high-water counter as automatic_target() does. diff --git a/drivers/gpu/drm/asahi/g17/buffer/layout.rs b/drivers/gpu/drm/asahi/g17/buffer/layout.rs index ca4ad54750658..1f0e241540358 100644 --- a/drivers/gpu/drm/asahi/g17/buffer/layout.rs +++ b/drivers/gpu/drm/asahi/g17/buffer/layout.rs @@ -2,7 +2,7 @@ //! Wire geometry for the shared parameter-buffer owner and growth machinery. -use super::{fw, BlockGeometry}; +use super::{cfg, fw, BlockGeometry}; use kernel::prelude::*; #[derive(Clone, Copy)] @@ -24,6 +24,25 @@ fn align(value: usize, alignment: usize) -> Result { value.checked_add(alignment - 1).map(|n| n & !(alignment - 1)).ok_or(EOVERFLOW) } +/// The tiler-heap maximum macOS 25G83 gives a 128 KiB-block heap +/// (`halGetDefaultParameterBufferSizeMax`): a twentieth of the memory, at least 192 MiB, +/// rounded up to whole groups of 129 blocks. 3,354 blocks with 8 GiB, 19,737 with 48 GiB. +fn macos_maximum_blocks(memory_bytes: u64) -> u64 { + const GROUP_BLOCKS: u64 = 129; + const GROUP_BYTES: u64 = GROUP_BLOCKS * cfg::TVB_BLOCK_SIZE as u64; + (memory_bytes / 20).max(0xc00_0000).div_ceil(GROUP_BYTES) * GROUP_BLOCKS +} + +/// The normal maximum of a 128 KiB-block heap, in blocks. macOS gives a queue a third of +/// its maximum until the queue renders memoryless (`BM+0x80`, and the cap of its pre-render +/// growth): 1,118 blocks with 8 GiB, 6,579 with 48 GiB. The block table holds +/// `TVB_MAX_BLOCKS`. `memory_bytes` is usable memory, below the installed amount macOS +/// reads, so never go below the 8 GiB value of the smallest G17 Mac. +pub(super) fn neo_normal_blocks(memory_bytes: u64) -> usize { + let normal = macos_maximum_blocks(memory_bytes).div_ceil(3); + (normal.min(cfg::TVB_MAX_BLOCKS as u64) as usize).max(fw::NORMAL_MAX_BLOCKS) +} + impl Layout { pub(super) const NEO: Self = Self { pages: fw::PAGE_LIST, page_bytes: fw::PAGE_LIST_BYTES, @@ -33,6 +52,11 @@ impl Layout { metric_stride: 4, normal_blocks: fw::NORMAL_MAX_BLOCKS, }; + /// The NEO layout of a device with `memory_bytes` of memory. + pub(super) fn neo(memory_bytes: u64) -> Self { + Self { normal_blocks: neo_normal_blocks(memory_bytes), ..Self::NEO } + } + pub(super) fn m5(geometry: BlockGeometry) -> Result { geometry.validate()?; let blocks = 0x4000; @@ -103,3 +127,31 @@ impl Layout { Ok(()) } } + +#[kernel::macros::kunit_tests(asahi_g17_tvb_limits)] +mod tests { + use super::*; + + const GIB: u64 = 1 << 30; + + #[test] + fn maximum_matches_macos() { + // The maxima macOS 25G83 reports (AGXParameterBufferMaxSize), in blocks. + assert_eq!(macos_maximum_blocks(8 * GIB), 3354); + assert_eq!(macos_maximum_blocks(16 * GIB), 6579); + assert_eq!(macos_maximum_blocks(48 * GIB), 19737); + // The 192 MiB floor. + assert_eq!(macos_maximum_blocks(GIB), 1548); + } + + #[test] + fn normal_maximum_scales_with_memory_up_to_the_block_table() { + assert_eq!(neo_normal_blocks(8 * GIB), fw::NORMAL_MAX_BLOCKS); + // Usable memory of an 8 GiB Mac keeps the 8 GiB value. + assert_eq!(neo_normal_blocks(7 * GIB), fw::NORMAL_MAX_BLOCKS); + assert_eq!(neo_normal_blocks(16 * GIB), 2193); + assert_eq!(neo_normal_blocks(24 * GIB), 3311); + assert_eq!(neo_normal_blocks(48 * GIB), cfg::TVB_MAX_BLOCKS); + assert_eq!(neo_normal_blocks(u64::MAX), cfg::TVB_MAX_BLOCKS); + } +} diff --git a/drivers/gpu/drm/asahi/g17/fw/buffer.rs b/drivers/gpu/drm/asahi/g17/fw/buffer.rs index 9d430d50db365..c450b76d66ebd 100644 --- a/drivers/gpu/drm/asahi/g17/fw/buffer.rs +++ b/drivers/gpu/drm/asahi/g17/fw/buffer.rs @@ -136,6 +136,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 (`BM+0x80`). + pub(crate) normal_blocks: usize, } pub(crate) fn compact_page(address: u64) -> Result { @@ -156,6 +158,7 @@ impl Graph { if args.generation == 0 || args.blocks.len() < cfg::TVB_INITIAL_PAGES.len() || args.blocks.len() > cfg::TVB_MAX_BLOCKS + || !(NORMAL_MAX_BLOCKS..=cfg::TVB_MAX_BLOCKS).contains(&args.normal_blocks) { return Err(EINVAL); } @@ -197,7 +200,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 * cfg::TVB_PAGES_PER_BLOCK) as u32; self.state.discard = discard; Ok(()) } diff --git a/drivers/gpu/drm/asahi/g17/queue/render/m5_render_submission.rs b/drivers/gpu/drm/asahi/g17/queue/render/m5_render_submission.rs index 333846d3f83c6..fe771355dcc1e 100644 --- a/drivers/gpu/drm/asahi/g17/queue/render/m5_render_submission.rs +++ b/drivers/gpu/drm/asahi/g17/queue/render/m5_render_submission.rs @@ -114,12 +114,7 @@ fn single_layer_compressed_depth_zls(pass: &RenderPass) -> bool { } pub(crate) fn parameter_geometry(blocks: usize) -> Result { - let mut info = core::mem::MaybeUninit::::zeroed(); - // SAFETY: The complete sysinfo is initialized and writable. - unsafe { kernel::bindings::si_meminfo(info.as_mut_ptr()); } - // SAFETY: The object was initialized before si_meminfo filled it. - let info = unsafe { info.assume_init() }; - let ram = (info.totalram as u64).checked_mul(u64::from(info.mem_unit)).ok_or(EOVERFLOW)?; + let ram = crate::g17::buffer::memory_bytes()?; let initial = blocks.checked_mul(0x80000).ok_or(EOVERFLOW)?; crate::g17::buffer::BlockGeometry::m5(ram, initial as u64) }