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
48 changes: 30 additions & 18 deletions drivers/gpu/drm/asahi/g17/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use super::fw::buffer as fw;
use crate::hw::t8140::resources as cfg;
pub(crate) mod target;
pub(crate) mod compact;
mod geometry;
pub(crate) use geometry::BlockGeometry;
mod layout;
Expand Down Expand Up @@ -414,6 +415,18 @@ impl Tvb {
}
geometry.backing_bytes(blocks)
}
/// Backing for `blocks` blocks to be mapped into `vm`. M5 blocks are mapped one by one in
/// the compact window, which is checked for room for all of them first, and the client's
/// passes and geometry size them, so they are charged to the client.
fn allocate(dev: &AsahiDrmDevice, vm: &mmu::Vm, geometry: BlockGeometry, blocks: usize)
-> Result<KernelObject> {
let bytes = Self::backing_bytes(geometry, blocks)?;
if geometry == BlockGeometry::NEO {
return KernelObject::backing(dev, bytes, CpuMap::WriteCombined);
}
compact::PairObject::ParameterBlock.check_room(vm, geometry.block_bytes, blocks)?;
KernelObject::client_backing(dev, bytes, vm.memory_client())
}
fn map_block(
object: &KernelObject,
vm: &mmu::Vm,
Expand All @@ -423,9 +436,8 @@ impl Tvb {
if geometry != BlockGeometry::NEO {
let start = relative.checked_mul(geometry.backing_stride).ok_or(EOVERFLOW)?;
let source = start..start.checked_add(geometry.block_bytes).ok_or(EOVERFLOW)?;
let guard = geometry.stride - geometry.block_bytes;
let mapping = object.map_range(vm, source, geometry.base..geometry.end,
BlockGeometry::PAGE as u64, mmu::PROT_GPU_SHARED_RW, guard)?;
let mapping = compact::PairObject::ParameterBlock.map(object, vm, source,
mmu::PROT_GPU_SHARED_RW)?;
geometry.compact_page(mapping.iova())?;
return Ok(mapping);
}
Expand Down Expand Up @@ -458,8 +470,7 @@ impl Tvb {
if blocks == 0 || blocks > geometry.max_blocks() {
return Err(ERANGE);
}
let backing = KernelObject::backing(dev, Self::backing_bytes(geometry, blocks)?,
CpuMap::WriteCombined)?;
let backing = Self::allocate(dev, vm, geometry, blocks)?;
let mut mappings = KVec::with_capacity(blocks, GFP_KERNEL)?;
let mut addresses = KVec::with_capacity(blocks, GFP_KERNEL)?;
for index in 0..blocks {
Expand Down Expand Up @@ -511,8 +522,7 @@ impl Tvb {
let added = target - self.blocks();
self.extensions.reserve(1, GFP_KERNEL)?;
self.addresses.reserve(added, GFP_KERNEL)?;
let backing = KernelObject::backing(dev, Self::backing_bytes(self.geometry, added)?,
CpuMap::WriteCombined)?;
let backing = Self::allocate(dev, vm, self.geometry, added)?;
let mut mappings = KVec::with_capacity(added, GFP_KERNEL)?;
let mut addresses = KVec::with_capacity(added, GFP_KERNEL)?;
for relative in 0..added {
Expand Down Expand Up @@ -568,15 +578,18 @@ impl MetricsIds {
/// backing each lease is taken from.
pub(crate) fn with_stride(scene_stride: usize) -> Result<Self> {
if scene_stride < 4 || scene_stride % 4 != 0 { return Err(EINVAL); }
let entry_pitch = scene_stride.checked_mul(fw::SCENES)
.and_then(|bytes| bytes.checked_next_multiple_of(mmu::UAT_PGSZ))
.ok_or(EOVERFLOW)?;
Ok(Self {
used: [AtomicU64::new(0), AtomicU64::new(0)],
scene_stride,
entry_pitch,
entry_pitch: Self::entry_pitch(scene_stride)?,
})
}
/// The pitch of entries of `scene_stride`-byte scenes: each starts its own pages.
pub(crate) fn entry_pitch(scene_stride: usize) -> Result<usize> {
scene_stride.checked_mul(fw::SCENES)
.and_then(|bytes| bytes.checked_next_multiple_of(mmu::UAT_PGSZ))
.ok_or(EOVERFLOW)
}
/// Backing required for every entry owned by this allocator.
pub(crate) fn storage_bytes(&self) -> Result<usize> {
self.entry_pitch.checked_mul(self.used.len() * u64::BITS as usize)
Expand Down Expand Up @@ -607,14 +620,14 @@ impl MetricsLease {
// window's start (InitData::bind_m5_metrics), so place the entry where its offset puts
// it there.
let fixed = super::J813_FORMS.load(Ordering::Relaxed).then(|| {
let at = BlockGeometry::M5_COMPACT.start + offset as u64;
self.backing.map_range(vm, offset..offset + pitch, at..at + pitch as u64,
mmu::UAT_PGSZ as u64, mmu::PROT_GPU_SHARED_RW, 0).ok()
let at = compact::WINDOW.start + offset as u64;
compact::PairObject::Metrics.map_at(&self.backing, vm, offset..offset + pitch, at,
mmu::PROT_GPU_SHARED_RW).ok()
}).flatten();
let client = match fixed {
Some(client) => client,
None => self.backing.map_range(vm, offset..offset + pitch, BlockGeometry::M5_COMPACT,
mmu::UAT_PGSZ as u64, mmu::PROT_GPU_SHARED_RW, 0)?,
None => compact::PairObject::Metrics.map(&self.backing, vm, offset..offset + pitch,
mmu::PROT_GPU_SHARED_RW)?,
};
self.addresses.client = client.iova();
self.client = Some(client);
Expand Down Expand Up @@ -822,8 +835,7 @@ impl Manager {
mmu::UAT_PGSZ,
)?
} else {
graph.map_range(vm, source, geometry.base..geometry.end,
cfg::TVB_PAGE, mmu::PROT_GPU_FW_SHARED_RW, mmu::UAT_PGSZ)?
compact::PairObject::PageList.map(&graph, vm, source, mmu::PROT_GPU_FW_SHARED_RW)?
};
let address = graph.gpu_va();
let graph_args = fw::GraphArgs {
Expand Down
266 changes: 266 additions & 0 deletions drivers/gpu/drm/asahi/g17/buffer/compact.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! The objects a render pair maps in its client's M5 compact window.
//!
//! The firmware addresses the window with 32-bit offsets, so it is 4 GiB, and every render pair
//! of the client maps its objects there. [`PairObject`] names each such object and fixes how it
//! is placed: every mapping of one goes through [`PairObject::map`] (or
//! [`PairObject::placement`]), and every room check before allocating one through
//! [`PairObject::check_room`]. SUBMIT's bound ([`PairSizes::window_bytes`]) counts the same
//! objects with the same placement, so an object cannot be mapped there without a size in the
//! bound, and the bound cannot disagree with a mapping on its alignment or guard.

use super::BlockGeometry;
use crate::g17::object::{KernelObject, Placement};
use crate::{
hw::t8140::{queues, resources as cfg},
mmu,
};
use core::ops::Range;
use kernel::prelude::*;

/// The client's M5 compact window.
pub(crate) const WINDOW: Range<u64> = BlockGeometry::M5_COMPACT;
/// Its size: 4 GiB.
pub(crate) const WINDOW_BYTES: u64 = WINDOW.end - WINDOW.start;

const DEPTH: usize = queues::RENDER_DEPTH as usize;

/// The task-stage mesh storage chunk: chunk bases are 64 KiB aligned, and the storage register
/// counts 64 KiB chunks.
pub(crate) const MESH_STORAGE_CHUNK: usize = 0x10000;

/// The task-stage mesh storage a render pair maps on its first task-stage kick and keeps, as
/// (chunks, bytes); `None` when the module parameter disables the binding.
pub(crate) fn mesh_storage() -> Result<Option<(u32, usize)>> {
let chunks = crate::module_parameters::m5_mesh_storage_chunks.value();
if chunks == 0 {
return Ok(None);
}
if chunks > 0xffff {
return Err(EINVAL);
}
let bytes = (chunks as usize).checked_mul(MESH_STORAGE_CHUNK).ok_or(EOVERFLOW)?;
Ok(Some((chunks, bytes)))
}

/// Bytes of a pair's parameter-buffer scene scratch for `geometry`.
pub(crate) fn scratch_bytes(geometry: BlockGeometry) -> Result<usize> {
use crate::g17::fw::m5::pool;
pool::parameter_scratch_stride(geometry.capacity, 0)
.map_err(|_| EINVAL)?
.checked_mul(pool::SCENE_TABLE_ENTRIES)
.ok_or(EOVERFLOW)
}

/// Bytes of the page list of a pair's block manager for `geometry`.
pub(crate) fn page_list_bytes(geometry: BlockGeometry) -> Result<usize> {
Ok(super::layout::Layout::m5(geometry)?.page_bytes)
}

/// Bytes of a pair's metrics entry for `geometry`.
fn metrics_bytes(geometry: BlockGeometry) -> Result<usize> {
super::MetricsIds::entry_pitch(super::layout::Layout::m5(geometry)?.metric_stride)
}

/// An object a render pair maps in its client's M5 compact window.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum PairObject {
/// The target records: one record stride for each command slot.
Records = 0,
/// The TPC object.
Tpc,
/// One parameter block, initial or grown, mapped at the M5 block stride.
ParameterBlock,
/// The context state of one command slot.
SlotState,
/// The parameter buffer's scene scratch.
Scratch,
/// The parameter buffer's discard.
Discard,
/// The block manager's page list.
PageList,
/// The pair's metrics entry.
Metrics,
/// The task-stage mesh storage, mapped on the pair's first task-stage kick and kept for the
/// pair's lifetime.
MeshStorage,
}

impl PairObject {
/// Every object, in declaration order.
pub(crate) const ALL: [Self; 9] = [
Self::Records,
Self::Tpc,
Self::ParameterBlock,
Self::SlotState,
Self::Scratch,
Self::Discard,
Self::PageList,
Self::Metrics,
Self::MeshStorage,
];

/// The alignment it is placed at.
pub(crate) const fn align(self) -> u64 {
match self {
Self::MeshStorage => MESH_STORAGE_CHUNK as u64,
Self::Metrics => mmu::UAT_PGSZ as u64,
Self::ParameterBlock => BlockGeometry::PAGE as u64,
Self::Records
| Self::Tpc
| Self::SlotState
| Self::Scratch
| Self::Discard
| Self::PageList => cfg::TVB_PAGE,
}
}

/// The unmapped guard that follows it.
pub(crate) const fn guard(self) -> usize {
match self {
Self::Metrics => 0,
Self::ParameterBlock => BlockGeometry::M5_BLOCK_STRIDE - BlockGeometry::M5_BLOCK_BYTES,
Self::Records
| Self::Tpc
| Self::SlotState
| Self::Scratch
| Self::Discard
| Self::PageList
| Self::MeshStorage => mmu::UAT_PGSZ,
}
}

/// The bytes one mapping of an object of `bytes` takes in one hole: its pages and its guard.
fn mapped_bytes(self, bytes: usize) -> Result<usize> {
bytes
.checked_next_multiple_of(mmu::UAT_PGSZ)
.and_then(|n| n.checked_add(self.guard()))
.ok_or(EOVERFLOW)
}

/// The window bytes an object of `bytes` can take: its pages and its guard, rounded up to
/// its alignment and at least to the 32 KiB most of these objects are placed at, plus, for
/// an object aligned beyond 32 KiB, the hole that alignment can leave before it.
pub(crate) fn window_bytes(self, bytes: usize) -> Result<usize> {
let base = cfg::TVB_PAGE as usize;
let align = (self.align() as usize).max(base);
self.mapped_bytes(bytes)?
.checked_next_multiple_of(align)
.and_then(|n| n.checked_add(align - base))
.ok_or(EOVERFLOW)
}

/// Maps `source` of `object` into `vm`'s window as this object.
pub(crate) fn map(
self,
object: &KernelObject,
vm: &mmu::Vm,
source: Range<usize>,
prot: mmu::Prot,
) -> Result<mmu::KernelMapping> {
let mapping = object.map_range(vm, source, WINDOW, self.align(), prot, self.guard())?;
if mapping.iova() % self.align() != 0 {
return Err(EINVAL);
}
Ok(mapping)
}

/// [`Self::map`] at `at`, where the firmware expects this object.
pub(crate) fn map_at(
self,
object: &KernelObject,
vm: &mmu::Vm,
source: Range<usize>,
at: u64,
prot: mmu::Prot,
) -> Result<mmu::KernelMapping> {
let end = at.checked_add(self.mapped_bytes(source.len())? as u64).ok_or(EOVERFLOW)?;
if at < WINDOW.start || end > WINDOW.end {
return Err(EINVAL);
}
object.map_range(vm, source, at..end, self.align(), prot, self.guard())
}

/// The [`Placement`] of a [`KernelObject::new`] of this object in the window, whose guard is
/// one page.
pub(crate) fn placement(self) -> Result<Placement> {
if self.guard() != mmu::UAT_PGSZ {
return Err(EINVAL);
}
Ok(Placement::In(WINDOW, self.align()))
}

/// Refuses with ENOSPC, as the mapping would and before any backing is allocated, `count`
/// of this object of `bytes` each that `vm`'s window has no room for: a hole for one, and
/// room for all of them. The window is shared, so room seen here is no placement promise:
/// the mapping can still fail, but only after allocating backing that could fit.
pub(crate) fn check_room(self, vm: &mmu::Vm, bytes: usize, count: usize) -> Result {
let (used, largest) = vm.capacity(WINDOW)?;
let one = self.mapped_bytes(bytes)?;
let all = one.checked_mul(count).ok_or(EOVERFLOW)?;
if one as u64 > largest || all as u64 > WINDOW_BYTES.saturating_sub(used) {
return Err(ENOSPC);
}
Ok(())
}
}

// `PairObject::ALL` holds every object once, in order.
const _: () = {
let mut index = 0;
while index < PairObject::ALL.len() {
assert!(PairObject::ALL[index] as usize == index);
index += 1;
}
assert!(PairObject::ALL.len() == PairObject::MeshStorage as usize + 1);
};

/// The sizes of the objects a render pair built for one layout maps in the window.
pub(crate) struct PairSizes {
/// The target records object: one record stride for each command slot.
pub(crate) records: usize,
/// The TPC object.
pub(crate) tpc: usize,
/// The initial parameter blocks, of `geometry`'s block size.
pub(crate) blocks: usize,
/// The parameter-buffer geometry, which sizes the blocks, scratch, page list and metrics.
pub(crate) geometry: BlockGeometry,
/// One command slot's context state.
pub(crate) state: usize,
/// The task-stage mesh storage the pair may map ([`mesh_storage`]).
pub(crate) mesh_storage: Option<usize>,
}

impl PairSizes {
/// How many of `object` the pair maps, and the bytes of each.
fn of(&self, object: PairObject) -> Result<(usize, usize)> {
Ok(match object {
PairObject::Records => (1, self.records),
PairObject::Tpc => (1, self.tpc),
PairObject::ParameterBlock => (self.blocks, self.geometry.block_bytes),
PairObject::SlotState => (DEPTH, self.state),
PairObject::Scratch => (1, scratch_bytes(self.geometry)?),
PairObject::Discard => (1, crate::g17::fw::m5::pool::DISCARD_BYTES),
PairObject::PageList => (1, page_list_bytes(self.geometry)?),
PairObject::Metrics => (1, metrics_bytes(self.geometry)?),
PairObject::MeshStorage => match self.mesh_storage {
Some(bytes) => (1, bytes),
None => (0, 0),
},
})
}

/// The window bytes of `object` in the pair.
pub(crate) fn object_bytes(&self, object: PairObject) -> Result<usize> {
let (count, bytes) = self.of(object)?;
object.window_bytes(bytes)?.checked_mul(count).ok_or(EOVERFLOW)
}

/// The window bytes of every object of the pair.
pub(crate) fn window_bytes(&self) -> Result<usize> {
PairObject::ALL.into_iter().try_fold(0usize, |sum, object| {
sum.checked_add(self.object_bytes(object)?).ok_or(EOVERFLOW)
})
}
}
Loading
Loading