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
23 changes: 19 additions & 4 deletions drivers/gpu/drm/asahi/g17/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ impl Growth {
}
}

/// Usable memory, the input of the RAM-scaled tiler-heap limits.
pub(crate) fn memory_bytes() -> Result<u64> {
let mut info = core::mem::MaybeUninit::<kernel::bindings::sysinfo>::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<usize> {
Expand Down Expand Up @@ -768,7 +778,11 @@ 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 layout = if neo {
layout::Layout::neo(memory_bytes()?)
} else {
layout::Layout::m5(geometry)?
};
if metrics.scene_stride != layout.metric_stride {
return Err(EINVAL);
}
Expand Down Expand Up @@ -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::<fw::Graph>(0, |view| view.init(&graph_args))?;
Expand Down Expand Up @@ -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<usize> {
// 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.
Expand Down
54 changes: 53 additions & 1 deletion drivers/gpu/drm/asahi/g17/buffer/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -24,6 +24,25 @@ fn align(value: usize, alignment: usize) -> Result<usize> {
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,
Expand All @@ -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<Self> {
geometry.validate()?;
let blocks = 0x4000;
Expand Down Expand Up @@ -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);
}
}
5 changes: 4 additions & 1 deletion drivers/gpu/drm/asahi/g17/fw/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ fn single_layer_compressed_depth_zls(pass: &RenderPass) -> bool {
}

pub(crate) fn parameter_geometry(blocks: usize) -> Result<crate::g17::buffer::BlockGeometry> {
let mut info = core::mem::MaybeUninit::<kernel::bindings::sysinfo>::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)
}
Expand Down
Loading