Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions Documentation/ABI/testing/sysfs-driver-asahi-agx-stats
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,30 @@ Description:
and shows suspension, queued-record discard and quarantine state.
Transport admission does not imply semantic validity: the decoder
still checks each percentage, selector and timestamp.

M3 GPUs (T8122 via the `m3/` backend and T6030/T6031/T6034 via
the `g15/` backend) also export `agx_stats`, `gpu_memory` and
`gpu_mem_limit_pct` using the qualified schema above:

- On RTKit 2419 (`m3/` legacy profile and `g15/`), the 256-slot
statistics ring emits 0x40-byte records using tags `0x02`
(PowerOn), `0x03` (PowerOff), `0x04` (Utilization) and `0x0b`
(PowerState). `utilisation_source` is
`firmware-statistics-tag-0x04`. `util_tiler_percent`,
`util_renderer_percent`, `util_device_percent` and cumulative
`busy_ns` are decoded from tag `0x04` (invalidated if any
field exceeds 100%, marked `stale` after 2.5 s without a new
sample while active, and `valid` 0% while power-gated).
`pstate` and `achieved_frequency_hz` come from tag `0x0b` or
`HardwareDataA` against the board's DVFS table (0 Hz while
power-gated). `requested_frequency_hz`, `power_mw` and
`temperature_raw` are `unsupported`.
- On the extended M3 profile (`m3/` macOS 26 firmware), `pstate`
and `achieved_frequency_hz` are reported from `PerformanceControl`
and the board's DVFS table, while `utilisation_status`,
`busy_ns` and `utilisation_source` report `unsupported` until
its statistics ring tags are qualified.
- Reads acquire the backend's device mutex, drain only the
statistics ring without waking the GPU, and respect
`asahi.stats_export=0`, fault/crash states and system suspend
boundaries.
1 change: 1 addition & 0 deletions drivers/gpu/drm/asahi/asahi_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod retain;
mod slotalloc;
mod stats;
mod stats_busy;
mod stats_m3;
mod stats_raw;
mod sysfs_exports;
mod timestamp;
Expand Down
37 changes: 30 additions & 7 deletions drivers/gpu/drm/asahi/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ impl AsahiPlatformDriver {
cfg: &'static crate::g15::Config,
) -> Result<AsahiPlatformDriverData<'bound>> {
sleep_pd_init(pdev.as_ref())?;
crate::memacct::LEDGER.configure();
// SAFETY: Admission checks the SoC and the UAT output width before DMA starts.
unsafe { pdev.dma_set_mask_and_coherent(DmaMask::try_new(cfg.uat_oas)?)? };
let resources = regs::Resources::new(pdev)?;
Expand All @@ -597,13 +598,21 @@ impl AsahiPlatformDriver {
// probe errors drop this local owner while the bound callback is live.
let binding = unsafe { crate::binding::Owner::new(pdev.as_ref(), &drm)? };
let gpu = crate::g15::Gpu::new(pdev, &drm, cfg)?;
let stats = gpu::Gpu::stats_snapshot(&*gpu);
let data = try_pin_init!(AsahiDrmRegistrationData {
gpu: gpu as Arc<dyn gpu::Gpu>,
resources,
});
// SAFETY: The registration is stored and unregistered by platform data destruction.
// G15 retains its firmware-visible state independently until reboot, even on error.
let reg = unsafe { drm::Registration::new(pdev.as_ref(), drm, data, 0)? };
let raw_dev = pdev.as_ref().as_raw();
let snapshot = stats.as_ref().map_or(core::ptr::null(), Arc::as_ptr);
let raw_stats = crate::sysfs_exports::register_qualified(
raw_dev,
snapshot,
stats.is_some() && module_parameters::stats_export.value() != 0,
)?;
Ok(AsahiPlatformDriverData {
binding,
_device: reg.device().into(),
Expand All @@ -614,9 +623,9 @@ impl AsahiPlatformDriver {
g14_v27_2: None,
#[cfg(CONFIG_DRM_ASAHI_G16)]
g16: None,
raw_dev: core::ptr::null_mut(),
raw_stats: core::ptr::null_mut(),
stats: None,
raw_dev,
raw_stats,
stats,
})
}

Expand Down Expand Up @@ -645,6 +654,20 @@ impl AsahiPlatformDriver {
return Err(error);
}
};
let stats = gpu::Gpu::stats_snapshot(&*backend);
let raw_dev = pdev.as_ref().as_raw();
let snapshot = stats.as_ref().map_or(core::ptr::null(), Arc::as_ptr);
let raw_stats = match crate::sysfs_exports::register_qualified(
raw_dev,
snapshot,
stats.is_some() && module_parameters::stats_export.value() != 0,
) {
Ok(handle) => handle,
Err(error) => {
backend.stop();
return Err(error);
}
};
dev_info!(pdev.as_ref(), "M3: firmware ready; common DRM GEM/VM frontend registered\n");
Ok(AsahiPlatformDriverData {
binding,
Expand All @@ -656,9 +679,9 @@ impl AsahiPlatformDriver {
m3: Some(backend),
#[cfg(CONFIG_DRM_ASAHI_G14_V27_2)]
g14_v27_2: None,
raw_dev: core::ptr::null_mut(),
raw_stats: core::ptr::null_mut(),
stats: None,
raw_dev,
raw_stats,
stats,
})
}

Expand Down Expand Up @@ -706,7 +729,7 @@ impl AsahiPlatformDriver {
let stats = gpu::Gpu::stats_snapshot(&*gpu);
let raw_dev = pdev.as_ref().as_raw();
let snapshot = stats.as_ref().map_or(core::ptr::null(), Arc::as_ptr);
let raw_stats = match crate::sysfs_exports::register_g17(
let raw_stats = match crate::sysfs_exports::register_qualified(
raw_dev,
snapshot,
module_parameters::stats_export.value() != 0,
Expand Down
6 changes: 6 additions & 0 deletions drivers/gpu/drm/asahi/g15/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub(crate) struct Channels {
pub(crate) stats: Ring,
/// What the stats ring carried, up to its consumer.
pub(crate) stats_seen: StatsSeen,
/// Qualified telemetry accumulator fed from the stats ring.
pub(crate) telemetry: crate::stats_m3::Telemetry,
/// Allocated for the firmware; the host never uses it.
_fwctl: Ring,
/// Host producers of channels 0 TA, 1 FS, 2 compute.
Expand Down Expand Up @@ -172,6 +174,7 @@ impl Channels {
rings::FWCTL_RING_SIZE,
)?,
stats_seen: StatsSeen::default(),
telemetry: crate::stats_m3::Telemetry::new(true),
producer: [0; 3],
devctl_producer: 0,
})
Expand Down Expand Up @@ -319,6 +322,7 @@ pub(crate) fn devctl_submit(
let expired = || start.elapsed() > Delta::from_millis(2000);
let opcode = record.opcode();
while channels.devctl.read()? != producer {
channels.drain_stats_only(super::engine::now_ns())?;
channels.drain_rings(&dev, events)?;
session.check()?;
if init.halted()? {
Expand Down Expand Up @@ -349,6 +353,7 @@ pub(crate) fn devctl_submit(
return Ok(());
}
loop {
channels.drain_stats_only(super::engine::now_ns())?;
channels.drain_rings(&dev, events)?;
session.check()?;
if channels.devctl.read()? == next && channels.devctl.completion()? == next {
Expand Down Expand Up @@ -395,6 +400,7 @@ impl Firmware {
/// Drains the receive rings and decodes events. Called first in every poll pass.
pub(crate) fn drain_rings(&mut self) -> Result {
let dev = self.session.dev.clone();
self.channels.drain_stats_only(super::engine::now_ns())?;
self.channels.drain_rings(&dev, &mut self.events)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 71867b6 (drm/asahi: m3, g15: consume the stats ring in one place): unified G15 stats consumption in Channels::note_stats() + transactional Channels::drain_stats(now_ns) called by Channels::drain_rings() and g15::stats::Reader::read(), removing the duplicate pre-drains and g15/stats.rs consumer. Also unified M3 stats consumption through Config::drain_stats_only() and extended tools/asahi/g15-stats-drain-tests.py with moving-producer exact-once telemetry checks and a no-telemetry mutation.

}
}
18 changes: 17 additions & 1 deletion drivers/gpu/drm/asahi/g15/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ mod queue;
mod recovery;
pub(crate) mod regs;
mod slots;
pub(crate) mod stats;
mod timestamp;
pub(crate) mod uat;
mod vm;
Expand Down Expand Up @@ -410,6 +411,7 @@ pub(crate) struct Gpu {
pub(crate) shared: Arc<Shared>,
/// The adopted UAT, for creating and binding user VMs without the device lock.
pub(crate) uat: Arc<mmu::Uat>,
stats: Arc<crate::stats::StatsSnapshot>,
}

impl Gpu {
Expand Down Expand Up @@ -457,11 +459,24 @@ impl Gpu {
let (id, max_frequency_khz) = (booted.id, booted.max_frequency_khz);
// The backend: graphs, slots, pools, engine, recovery and VM registry.
let firmware = Firmware::start_backend(dev, cfg, booted, &shared)?;
let table_a = board::table_a_mhz(&firmware.session.adt);
let freq_table =
crate::stats_m3::FreqTable::from_slice(&table_a, (board::DVFS_STATES - 1) as u32);
*shared.state.lock() = Some(firmware);
// The engine's statistics file (a debugfs failure does not fail the probe).
if let Err(error) = shared.start_engine() {
dev_warn!(dev.as_ref(), "G15: no aurora-g15 debugfs ({:?})\n", error);
}
let stats = Arc::new(
crate::stats::StatsSnapshot {
qualified_reader: Some(crate::stats::QualifiedReader::G15(stats::Reader(
shared.clone(),
freq_table,
))),
..Default::default()
},
GFP_KERNEL,
)?;
let gpu = Arc::new(
Gpu {
cfg,
Expand All @@ -470,6 +485,7 @@ impl Gpu {
ids: Default::default(),
shared,
uat,
stats,
},
GFP_KERNEL,
)?;
Expand Down Expand Up @@ -594,7 +610,7 @@ impl gpu::Gpu for Gpu {
}

fn stats_snapshot(&self) -> Option<Arc<crate::stats::StatsSnapshot>> {
None
Some(self.stats.clone())
}

fn as_any(&self) -> &dyn Any {
Expand Down
2 changes: 1 addition & 1 deletion drivers/gpu/drm/asahi/g15/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static_assert!(pstate::STATS_SLOTS == super::fw::channels::RING_SLOTS);

impl Firmware {
/// HardwareDataA's state words, as the firmware last wrote them.
fn pstate_words(&self) -> Result<pstate::HwWords> {
pub(crate) fn pstate_words(&self) -> Result<pstate::HwWords> {
let hw = self.init.hwdata.as_ref().ok_or(ENODEV)?;
let word = |offset: usize| hw.r32(hwdata::HWDATA_A + offset);
Ok(pstate::HwWords {
Expand Down
131 changes: 131 additions & 0 deletions drivers/gpu/drm/asahi/g15/stats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! Sysfs qualified statistics reader for the G15 (M3 Pro/Max: T6030/T6031/T6034) backend.
//!
//! Owns the shared session handle and DVFS table-A frequencies. Reads acquire
//! `Shared::state`, drain the statistics ring without waking the GPU or servicing
//! submissions, and return a qualified `Readout`.

use core::sync::atomic::Ordering;
use kernel::{prelude::*, sync::Arc};

use super::{
channel::{Channels, Ring},
engine,
fw::channels::{STATE_READ, STATE_WRITE},
power::pstate::{self, StatsSeen},
regs::{mb, rmb, wmb},
Shared,
};
use crate::{
g17::telemetry::Readout,
stats_m3::{self, FreqTable, HwPstateSnapshot, Telemetry},
};

impl Channels {
fn note_stats_telemetry(
ring: &Ring,
seen: &mut StatsSeen,
telemetry: &mut Telemetry,
now_ns: u64,
) -> Result<Option<u32>> {
let producer = ring.state.r32(STATE_WRITE)?;
let read = ring.state.r32(STATE_READ)?;
let Some(slots) = pstate::pending(read, producer) else {
return Ok(None);
};
rmb();
for slot in slots {
let at = slot as usize * pstate::STATS_RECORD;
let tag = ring.ring.r32(at + pstate::STATS_TAG)?;
let pstate_word = ring.ring.r32(at + pstate::POWER_STATE_INDEX)?;
seen.note(tag, pstate_word);
let words = match tag {
stats_m3::TAG_UTILIZATION | stats_m3::TAG_POWER_STATE => [
tag,
ring.ring.r32(at + 0x04)?,
ring.ring.r32(at + 0x08)?,
ring.ring.r32(at + 0x0c)?,
ring.ring.r32(at + 0x10)?,
ring.ring.r32(at + 0x14)?,
ring.ring.r32(at + 0x18)?,
ring.ring.r32(at + 0x1c)?,
pstate_word,
],
_ => [tag, 0, 0, 0, 0, 0, 0, 0, pstate_word],
};
telemetry.record_words(&words, now_ns, (pstate::STATES - 1) as u32);
}
Ok(Some(producer))
}

/// Drains only the statistics ring, updating both `stats_seen` and `telemetry`,
/// without touching event, log or trace rings and without waking the GPU.
pub(crate) fn drain_stats_only(&mut self, now_ns: u64) -> Result {
let mut seen = self.stats_seen;
let mut telemetry = self.telemetry.clone();
if let Some(producer) =
Self::note_stats_telemetry(&self.stats, &mut seen, &mut telemetry, now_ns)?
{
self.stats_seen = seen;
self.telemetry = telemetry;
if self.stats.read()? != producer {
mb();
self.stats.state.w32(STATE_READ, producer)?;
wmb();
}
return Ok(());
}
self.telemetry.invalidate();
let producer = self.stats.state.r32(STATE_WRITE)?;
if self.stats.state.r32(STATE_READ)? != producer {
self.stats.state.w32(STATE_READ, producer)?;
wmb();
}
Ok(())
}
}

pub(crate) struct Reader(pub(super) Arc<Shared>, pub(super) FreqTable);

impl Reader {
pub(crate) fn read(&self) -> Readout {
let mut state = self.0.state.lock();
let Some(firmware) = state.as_deref_mut() else {
return Readout {
util_source: stats_m3::UTIL_SOURCE_TAG_04,
..Readout::default()
};
};
let now_ns = engine::now_ns();
let unhealthy = self.0.crashed.load(Ordering::Acquire)
|| self.0.orphaned.load(Ordering::Acquire)
|| firmware.recovery.failed().is_some();
if unhealthy {
firmware.channels.telemetry.invalidate();
return firmware.channels.telemetry.read(now_ns, None, &self.1);
}
if firmware.channels.drain_stats_only(now_ns).is_err() {
firmware.channels.telemetry.invalidate();
return firmware.channels.telemetry.read(now_ns, None, &self.1);
}
let hw = firmware.pstate_words().ok().map(|w| HwPstateSnapshot {
actual: w.actual,
current: w.current,
freq_mhz_bits: w.freq_mhz,
sgx_pstate: None,
});
firmware.channels.telemetry.read(now_ns, hw, &self.1)
}

pub(crate) fn suspend(&self, suspended: bool) {
if let Some(firmware) = self.0.state.lock().as_deref_mut() {
firmware.channels.telemetry.suspend(suspended);
}
}

pub(crate) fn frequency(&self) -> Option<(Option<u64>, u64)> {
let view = self.read();
stats_m3::frequency_pair(&view, &self.1)
}
}
5 changes: 4 additions & 1 deletion drivers/gpu/drm/asahi/g17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2241,7 +2241,10 @@ impl Gpu {
)?;
let stats = Arc::new(
crate::stats::StatsSnapshot {
g17_reader: Some(stats::Reader(shared.clone(), frequencies)),
qualified_reader: Some(crate::stats::QualifiedReader::G17(stats::Reader(
shared.clone(),
frequencies,
))),
..Default::default()
},
GFP_KERNEL,
Expand Down
Loading
Loading