Skip to content

[follow-up to #168] drm/asahi: tie stats registration to a per-device snapshot owner - #197

Merged
RyanTheTide merged 18 commits into
aurora-wipfrom
fix/pr168-review
Oct 8, 2026
Merged

RyanTheTide merged 18 commits into
aurora-wipfrom
fix/pr168-review

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

Keep a snapshot Arc in the successfully bound driver and pass its
address to a per-device C attribute wrapper. Register after GPU
initialization and drain sysfs readers before dropping the owner. Remove
early global pointer publication so competing or failing probes cannot
replace another device snapshot.

Follow-up to #168; original contributor commits are preserved. These fixes are based on public Linux/PR source and Linux-side tests.

Validation: targeted arm64 driver build passed (object checks only); strict checkpatch passed for the final code changes with only the required Assisted-by trailer diagnostic excluded.

Hardware qualification on aurora-edge is pending. No boot, suspend, fault-injection or firmware recovery pass is claimed. The publication gate is separate from runtime acceptance.

Any outstanding contributor certification on the original PR remains outstanding; no contributor sign-off was added by this follow-up.

🤖 Generated with Claude Code

joshuaswarren and others added 18 commits October 4, 2026 05:51
Opt-out toggle for the AGX firmware stats sysfs export. When 1
(the default), StatsChannel::poll retains the values it already decodes
into the StatsSnapshot exposed via /sys/class/drm/card*/device/agx_stats.
When 0, the export is suppressed and the file is not created — matching
today's behaviour exactly (the channel is still polled, the messages
are still debug-logged).

Signed-off-by: Joshua Warren <[email protected]>
Add the per-GpuManager StatsSnapshot type and an update_from(&StatsMsg)
that mirrors StatsChannel::poll's debug path. The type holds the latest
util1..util4, pstate, avg_power_mw, temperature (raw, scale, tmin, tmax)
and a cumulative busy_ns derived from successive FwBusy timestamps.
A module-level SNAPSHOT_PTR (AtomicU64) is published by GpuManager::make
so the C-side sysfs shim can find the live snapshot without walking the
kernel object graph. StatsChannel::poll is not yet wired up; that comes
in the next commit alongside the module-param read.

Signed-off-by: Joshua Warren <[email protected]>
Construct the StatsSnapshot in GpuManager::make_mgr, publish its raw
pointer to the module-level SNAPSHOT_PTR, and clone a reference into the
new StatsChannel field. StatsChannel::poll now calls snap.update_from()
on every decoded message when module_parameters::stats_export is non-zero.
No change to scheduling or power behaviour — the firmware already sends
the messages and the channel already polls them; we only retain the
values.

Signed-off-by: Joshua Warren <[email protected]>
Add the C sidecar drivers/gpu/drm/asahi/sysfs.c that owns the
device_attribute and the formatted show output. Bindgen for this kernel
tree does not include <linux/sysfs.h>, so device_create_file is
unavailable to Rust — the shim is the lowest-risk way to expose the
file. Rust binds to asahi_sysfs_register/unregister via
drivers/gpu/drm/asahi/sysfs_exports.rs. The Makefile adds sysfs.o to
asahi-y so both objects link into asahi.ko (and vmlinux when
CONFIG_DRM_ASAHI=y). All writes are field-keyed through AtomicU*
so the IRQ-side update_from and the sysfs show never need a lock.

Signed-off-by: Joshua Warren <[email protected]>
Stash the platform device's raw struct device pointer in
AsahiDriver::raw_dev and call asahi_sysfs_register() after
drm::driver::Registration::new_foreign_owned. The Drop impl calls
asahi_sysfs_unregister() which both removes the attribute and clears
SNAPSHOT_PTR — the lifetime of the StatsSnapshot is owned by
GpuManager::make_mgr, which outlives the AsahiDriver by construction.

Signed-off-by: Joshua Warren <[email protected]>
Add the description for /sys/class/drm/card*/device/agx_stats. The
file is the consumer contract for the AGX firmware stats export; this
doc describes each key, the stats_export module parameter, and the
pending validation of util1..util4 semantics against a controlled MLX
load (T6001 13.5 firmware). busy_ns is documented as firmware-timestamp
units, with saturation at u64::MAX. jobs starts at 0 on T6001 and is
reserved for the submission-completion landing.

Signed-off-by: Joshua Warren <[email protected]>
The producer contract for agx_stats promises a cumulative completed-
submissions counter, but the snapshot never counted anything. Count a
submission when its last command completes: JobFence::command_complete
bumps the snapshot counter right before the fence signals, via an Arc
carried on the fence (Queue -> JobFence). One Relaxed fetch_add per
completed submission; no change to the submission or completion path
ordering.

Signed-off-by: Joshua Warren <[email protected]>
The file prints busy_ns, jobs, pstate, power_mw, util1..util4,
temperature_raw and temperature_scale; drop the temperature_tmin /
temperature_tmax keys the code never prints, state the u64 wrap
semantics of busy_ns honestly instead of a saturation claim, and
describe jobs as the host-side completed-submission counter it now is.

Signed-off-by: Joshua Warren <[email protected]>
`asahi-y := sysfs.o` made Kbuild treat asahi.o as a composite module
whose only part was sysfs.o: the composite link overwrote the object
rustc emits for the crate root (both are named asahi.o), so the module
— and any kernel built with it — contained the sysfs shim alone and no
GPU driver at all. A stock-config boot of such a kernel loses the GPU
entirely (observed: no AGX platform driver, apple-dart -16 probe
failures on the display darts, dcp missing).

Rename the crate root to asahi_core.rs and list both objects:
asahi-y := asahi_core.o sysfs.o. The module (or built-in vmlinux) now
links the crate and the shim; module metadata keeps name "asahi".

Signed-off-by: Joshua Warren <[email protected]>
Arc::new returns Result<Arc<T>, AllocError> in this tree; the stats
snapshot creation ignored it, so the crate did not compile once the
Makefile actually linked it (the previous shim-only module never
compiled the driver). Propagate with ?.

Signed-off-by: Joshua Warren <[email protected]>
StatsMsg is a versions-macro type: per-version generics exist, a bare
crate::fw::channels::StatsMsg does not, so the snapshot's update_from
could not compile (E0432). The first full compile of the driver crate
exposed this — the earlier shim-only module never compiled the Rust
side at all.

Move the decode into StatsChannel::poll, which already runs in the
versioned context with fw::channels::* in scope, and write the matched
fields straight into the snapshot (last_busy_ts becomes pub(crate) for
the FwBusy integration). Timestamps are this tree's fw U64 tuple type,
read as .0. StatsSnapshot keeps the plain unversioned struct and
note_job.

Signed-off-by: Joshua Warren <[email protected]>
StatsMsg is versions-macro generated; the per-version enum is only
reachable as StatsMsg::ver inside a versioned item (the macro rewrites
it to StatsMsg<VER>). Plain variant paths are for non-versioned enums
like EventMsg. Qualify the five match arms.

Signed-off-by: Joshua Warren <[email protected]>
The series declared two unconnected statics: Rust wrote its own
stats::SNAPSHOT_PTR while the C show callback read the never-written
asahi_stats_snapshot_ptr, so agx_stats would print 'unsupported'
forever (rustc flagged the C static as unused from the Rust side).

Make the C static the single storage location: sysfs.c grows
asahi_stats_set_snapshot_ptr() (WRITE_ONCE), sysfs_exports.rs exposes
set_snapshot_ptr(), and GpuManager publishes the Arc address through it
at probe; asahi_sysfs_unregister keeps clearing it. The unused extern
static declaration and the stats::SNAPSHOT_PTR duplicate are gone.

Signed-off-by: Joshua Warren <[email protected]>
Window B on the M2 measured three defects against the first verified
kernel, all rooted in the cross-language contract:

1. The C sysfs readout crossed field boundaries. StatsSnapshot was
   repr(Rust): rustc legally reordered it (empirically, rustc 1.98 puts
   the AtomicU64s first: busy_ns at +8, jobs at +16), while sysfs.c read
   the declared offsets (busy_ns +48, jobs +56). The file therefore
   showed busy_ns bouncing in 2^32 steps across reads and jobs stuck at
   0 while the counter incremented on every completed submission (the
   JobFence::command_complete site fires; decode fences must signal for
   decode to complete at all). Make the layout a contract: #[repr(C)] on
   the Rust struct and BUILD_BUG_ON offset/size asserts in sysfs.c.

2. stats_export=0 kept the file visible with a zeroed key set. The ABI
   document promises exactly "unsupported": asahi_sysfs_register() now
   takes the export flag and the show callback prints "unsupported"
   when the export is disabled.

3. Document the monotonicity and the layout contract in the ABI entry.

The 3 apple-dart -EPROBE_DEFER and 3 dcp supplier-miss lines on the test
boot are display-path and out of this series' scope: the series touches
only drm/asahi and the ABI document (429 lines, no dcp/dart/display
files); the defers come from the 7.1.12 aurora base pairing with the
stock 7.1.13-era device tree supplied by the untouched boot.bin.

Measured on the retry window (jw14m2, artifacts 20261004T1500Z):
decode digests bit-identical on all arms, reader overhead 0.27%,
export-off 0.32%.
FwBusy timestamp deltas do not measure compute on T6021: the round-3
window measured a busy fraction of 0.0017 across a saturated 30 s
matmul (the FwBusy stream is near-silent under compute), while the
Utilization message (tag 0x04) reads 0-100 per-subqueue percentages
that track it exactly (100/100/100/100 across the matmul, 0-2 idle,
decode on subqueue 3). No T6001 hardware validation of the FwBusy
producer exists either; the old ns-magnitude claims were aspirational.

busy_ns now integrates utilization-weighted time: each Utilization
window adds (ts - prev) / 100 * max(util1..4).min(100), with the last
timestamp kept channel-local. Monotonic, bounded by elapsed wall time,
and invariant to the firmware tick rate. The FwBusy arm and the
snapshot's last_busy_ts field are gone; the C mirror and its offset
asserts shrink with it (busy_ns 40, jobs 48, size 56).

Signed-off-by: Joshua Warren <[email protected]>
Stats timestamps are 24 MHz ticks, not ns: HwConfig.base_clock_hz is
24_000_000 on every supported SoC (t600x, t602x, t8103, t8112), and the
driver chooses ns only for user command timestamps (file.rs), which it
converts into base-clock ticks. Window C measured the v1 producer at
22.5e6 raw ticks/s across a saturated matmul = 0.9375 of the base
clock: the load was fully tracked, only the unit was missing (the
round-3 idle 0.024 fraction was full-time coverage misread as ns,
1e9/24e6 = 1/41.667 exactly).

busy_ns now converts once, in one place: StatsChannel carries ts_hz
(plumbed from cfg.base_clock_hz at the gpu.rs call site) and each
Utilization window adds (ts - prev) * util * 1e9 / (100 * ts_hz) in
u128. The ABI document states the tick conversion; expected T6021
matmul fraction 0.94-1.0, idle 0.0002-0.01.

Signed-off-by: Joshua Warren <[email protected]>
The first tick-conversion attempt divided a u128 intermediate, and the
kernel runtime has no __udivti3: the vmlinux link failed with an
undefined reference. Split the window instead: whole seconds contribute
util x 1e7 ns each (1e9 / 100 exactly), the tick remainder contributes
rem x util x 1e9 / (100 x ts_hz), both in plain 64-bit arithmetic and
exact against the u128 formula. Also drop an unnecessary unsafe that
rustc flags in the sysfs exports wrapper.

Signed-off-by: Joshua Warren <[email protected]>
Keep a snapshot Arc in the successfully bound driver and pass its
address to a per-device C attribute wrapper. Register after GPU
initialization and drain sysfs readers before dropping the owner. Remove
early global pointer publication so competing or failing probes cannot
replace another device snapshot.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide
RyanTheTide merged commit c6a66bb into aurora-wip Oct 8, 2026
@RyanTheTide
RyanTheTide deleted the fix/pr168-review branch October 8, 2026 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants