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
2 changes: 1 addition & 1 deletion drivers/gpu/drm/asahi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ aurora-$(CONFIG_DRM_ASAHI_G15) += g15/glue.o

aurora-$(CONFIG_DRM_ASAHI_G16) += g16/glue.o

aurora-y += retain.o cpu_capacity.o
aurora-y += retain.o cpu_capacity.o g17/busy_tvb_grow.o
102 changes: 101 additions & 1 deletion drivers/gpu/drm/asahi/g17/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,36 @@ fn automatic_target_for(used_pages: usize, current: usize, pages: usize, maximum
)
}

unsafe extern "C" {
/// `aurora.g17_busy_tvb_grow` (g17/busy_tvb_grow.c).
fn asahi_g17_busy_tvb_grow() -> u32;
}

/// Whether the host grows a busy pair's heap ahead of a render
/// (`aurora.g17_busy_tvb_grow`, read on every use).
pub(crate) fn busy_growth_enabled() -> bool {
// SAFETY: Reads a module parameter; no other effect.
let value = unsafe { asahi_g17_busy_tvb_grow() };
value != 0
}

/// Whether `bytes` more kernel memory keeps the device's GPU memory more than an
/// eighth below the device limit, and `client`'s committed memory more than an
/// eighth below the per-client limit (`memacct`). A busy pair's growth is only
/// ahead of need, so it never takes the device or a client close to a limit;
/// firmware grow requests still serve the pair.
fn busy_growth_admitted(bytes: u64, client: Option<&Arc<crate::memacct::Client>>) -> bool {
let ledger = &crate::memacct::LEDGER;
let within = |used: u64, limit: u64| limit == 0 || used.saturating_add(bytes) <= limit - limit / 8;
within(
ledger.user_committed.load(Ordering::Relaxed)
.saturating_add(ledger.kernel_resident.load(Ordering::Relaxed)),
ledger.limit.load(Ordering::Relaxed),
) && client.is_none_or(|client| {
within(client.committed(), ledger.client_limit.load(Ordering::Relaxed))
})
}

use super::object::{CpuMap, KernelObject};
use crate::{driver::AsahiDrmDevice, mmu};
use core::ops::Range;
Expand Down Expand Up @@ -741,6 +771,14 @@ struct Shrink {
window: [u32; SHRINK_WINDOW],
window_next: usize,
busy_peak: u32,
/// Blocks before the first growth the host made while the pair was busy,
/// since the last idle growth or the shrink that gave it back (0: none).
/// An idle shrink may return to this size: that growth only serves
/// renders overlapping in flight.
busy_base: usize,
/// Host ns before which a busy pair is not grown again after a growth
/// was refused or failed.
busy_retry: u64,
}

pub(crate) struct Submitted<'a> {
Expand Down Expand Up @@ -952,6 +990,47 @@ impl Manager {
Ok(automatic_target_for(pages as usize, self.blocks(), self.tvb.geometry.pages(),
self.layout.normal_blocks))
}
/// The growth of a pair with work in flight ahead of a render that needs
/// `required` blocks at `now`: the layout's blocks, or the automatic target
/// from the page high-water since the last sample, which is read without
/// requesting its reset (the idle growth check and the shrink period own the
/// samples). None keeps the heap: the growth is not needed, waits for a
/// retry after a refusal, or would take the device or `client` within an
/// eighth of a memory limit (it is then retried a shrink period later).
pub(crate) fn busy_target(
&mut self,
required: usize,
now: u64,
client: Option<&Arc<crate::memacct::Client>>,
) -> Result<Option<usize>> {
if self.tvb.geometry != BlockGeometry::NEO || now < self.shrink.busy_retry {
return Ok(None);
}
let max_pages = self
.graph
.word(self.layout.control + core::mem::offset_of!(fw::Control, max_pages))?;
fence(Ordering::Acquire);
let pages = max_pages.load(Ordering::Relaxed) as usize;
let current = self.blocks();
let feedback = automatic_target_for(pages, current, self.tvb.geometry.pages(),
self.layout.normal_blocks).unwrap_or(0);
let target = required.max(feedback).min(self.max_blocks());
if target <= current {
return Ok(None);
}
let bytes = Tvb::backing_bytes(self.tvb.geometry, target - current)? as u64;
if !busy_growth_admitted(bytes, client) {
self.defer_busy_growth(now);
super::trace::heap(super::trace::HEAP_HOST_BUSY, false, current, current);
return Ok(None);
}
Ok(Some(target))
}
/// A busy pair's growth was refused or failed at `now`: retry a shrink
/// period later.
pub(crate) fn defer_busy_growth(&mut self, now: u64) {
self.shrink.busy_retry = now.saturating_add(SHRINK_PERIOD_NS);
}
/// Records the blocks a pass layout requires, the floor of any shrink.
/// A pass is being prepared at `now`; the heap counts as busy.
pub(crate) fn note_required(&mut self, blocks: usize, now: u64) {
Expand All @@ -965,7 +1044,22 @@ impl Manager {
let recent = (self.shrink.busy_peak as usize)
.saturating_mul(2)
.div_ceil(cfg::TVB_PAGES_PER_BLOCK);
self.tvb.primary_blocks().max(self.shrink.required).max(recent)
let minimum = self.tvb.primary_blocks().max(self.shrink.required);
// Growth made while the pair was busy goes back down to the size it had
// before, also below the recent peak, which counts renders in flight
// together.
let busy = if self.shrink.busy_base != 0 {
minimum.max(self.shrink.busy_base)
} else {
usize::MAX
};
minimum.max(recent).min(busy)
}
/// The host grew this heap from `previous` blocks while the pair was busy.
pub(crate) fn note_busy_growth(&mut self, previous: usize) {
if self.shrink.busy_base == 0 {
self.shrink.busy_base = previous;
}
}
/// Whether the heap holds growth beyond its primary backing.
/// M5 heaps are never shrunk.
Expand Down Expand Up @@ -1208,6 +1302,8 @@ impl Manager {
write(at + 4, 0);
}
if descriptor.is_some() {
// An idle growth sizes the heap for the recent load, busy growth included.
self.shrink.busy_base = 0;
for (entry, value) in rebuilt.into_iter().enumerate() {
write(self.layout.pages + entry * 4, value);
}
Expand Down Expand Up @@ -1323,6 +1419,10 @@ impl Manager {
}
}
self.shrink.credit = self.shrink.credit.saturating_sub(blocks - kept);
if kept <= self.shrink.busy_base {
self.shrink.busy_base = 0;
}
super::trace::heap(super::trace::HEAP_SHRINK, true, blocks, kept);
Ok(Some((
fw::reload_descriptor(descriptor, self.page_list_client, pages),
release,
Expand Down
22 changes: 22 additions & 0 deletions drivers/gpu/drm/asahi/g17/busy_tvb_grow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/*
* A run-time switch for growing the tiler heap of a busy A18 Pro render pair
* ahead of a render. 1 (the default) grows it by appending blocks to its
* inventory, as a firmware grow request does; 0 grows the heap ahead of a
* render only when the render is submitted to an idle pair. Read on every
* use, so it can be flipped without reloading the driver.
*/
#include <linux/compiler.h>
#include <linux/moduleparam.h>

static unsigned int g17_busy_tvb_grow = 1;
module_param(g17_busy_tvb_grow, uint, 0644);
MODULE_PARM_DESC(g17_busy_tvb_grow,
"A18 Pro: grow a busy render pair's tiler heap ahead of a render (1: on, default; 0: idle pairs only)");

unsigned int asahi_g17_busy_tvb_grow(void);

unsigned int asahi_g17_busy_tvb_grow(void)
{
return READ_ONCE(g17_busy_tvb_grow);
}
11 changes: 11 additions & 0 deletions drivers/gpu/drm/asahi/g17/fw/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ pub(crate) struct Variant {
/// Tiling descriptors report whether the fragment queue still had earlier kicks
/// outstanding (`fragment_busy`).
pub(crate) fragment_busy: bool,
/// The host may grow the tiler heap of a render pair with work in flight ahead of a
/// render, by appending blocks to its inventory as a firmware grow request does. The
/// firmware takes the new blocks without a grow reply.
pub(crate) busy_heap_growth: bool,
/// Render item rings carry lead commands: InitBufferManager before the first tiling pass
/// of a buffer manager.
pub(crate) lead_commands: bool,
Expand Down Expand Up @@ -138,6 +142,7 @@ pub(crate) const V26A428: Variant = Variant {
traversal_exclusion: true,
render_ray_query: true,
fragment_busy: true,
busy_heap_growth: true,
lead_commands: false,
programs: super::render::RegisterPrograms::G17P,
ta_mode_1: false,
Expand Down Expand Up @@ -176,6 +181,8 @@ pub(crate) const V25G83: Variant = Variant {
// When a tiling pass runs out of memory with this set, the firmware resumes it without
// adding memory and the tiler stops silently; macOS always writes 0.
fragment_busy: false,
// Taken as on 26A428: run on the J616c (T6041).
busy_heap_growth: true,
lead_commands: true,
programs: super::render::RegisterPrograms::V25G83,
ta_mode_1: false,
Expand All @@ -194,6 +201,8 @@ pub(crate) const V26A428_M5: Variant = Variant {
traversal_exclusion: false,
render_ray_query: false,
registration_power_hold: false,
// M5 heaps have their own geometry and are never shrunk.
busy_heap_growth: false,
..V26A428
};

Expand All @@ -203,6 +212,8 @@ pub(crate) const V25F84: Variant = Variant {
compute: super::compute::ComputeProgram::V25F84,
opening_record: false,
split_firmware_area: true,
// Not run on this firmware.
busy_heap_growth: false,
..V25G83
};

Expand Down
46 changes: 35 additions & 11 deletions drivers/gpu/drm/asahi/g17/queue/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ pub(crate) enum Registration {
M5(crate::g17::fw::m5::queue::HardwareSubmission),
}

/// A tiler heap growth the host makes before publishing a render, to this many blocks.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum HostGrowth {
/// The pair is idle: the page list and the PB descriptor are rebuilt.
Idle(usize),
/// The pair has work in flight: blocks are only appended to the inventory,
/// as for a firmware grow request.
Busy(usize),
}

/// Installed kick backing remains owned even when this pair has no logical
/// owner. Commands retain their exact descriptor and allocation generations.
pub(crate) struct Pair {
Expand Down Expand Up @@ -1725,17 +1735,31 @@ impl Pair {
pub(crate) fn oldest_packet(&self) -> Option<Arc<Packet>> {
self.active.front().map(|active| active.packet.clone())
}
/// Sample PM feedback only after this pair's exact retirement. Busy pairs
/// keep their current capacity and let firmware request growth as needed.
pub(crate) fn host_growth_target(&mut self, packet: &Packet) -> Result<Option<usize>> {
let required =
m5_render_submission::layout(&packet.command, self.clusters, self.variant.programs)?
.tvb_blocks;
self.memory
.manager
.note_required(required, crate::g17::now_ns());
/// Sample PM feedback only after this pair's exact retirement. A busy pair
/// grows ahead of the render only by appending blocks to its inventory, as a
/// firmware grow request does (`buffer::Manager::busy_target`), where the
/// firmware build allows it (`Variant::busy_heap_growth`); otherwise it
/// keeps its current capacity and lets firmware request growth as needed.
pub(crate) fn host_growth_target(&mut self, packet: &Packet) -> Result<Option<HostGrowth>> {
let layout =
m5_render_submission::layout(&packet.command, self.clusters, self.variant.programs)?;
let required = layout.tvb_blocks;
let now = crate::g17::now_ns();
self.memory.manager.note_required(required, now);
if self.in_flight() {
return Ok(None);
if !self.variant.busy_heap_growth
|| self.quarantined
|| layout.is_m5()
|| !buffer::busy_growth_enabled()
{
return Ok(None);
}
let client = self.memory.context.vm().memory_client().cloned();
return Ok(self
.memory
.manager
.busy_target(required, now, client.as_ref())?
.map(HostGrowth::Busy));
}
if !self.idle_for_context(self.context())? {
return Err(EBUSY);
Expand All @@ -1751,7 +1775,7 @@ impl Pair {
// ERANGE: a 16384x16384 no-attachment render after smaller ones lost the device
// (dEQP-GLES31.functional.fbo.no_attachments.maximums.*).
let required = required.max(feedback).min(self.memory.manager.max_blocks());
Ok((required > self.memory.manager.blocks()).then_some(required))
Ok((required > self.memory.manager.blocks()).then_some(HostGrowth::Idle(required)))
}

/// A quarantined pair, or a closed owner's pair that holds only groups whose stamps are
Expand Down
Loading