Skip to content

drm/asahi: Export AGX firmware stats to /sys/class/drm/card*/device/agx_stats - #157

Closed
joshuaswarren wants to merge 6 commits into
aurora-silicon:aurora-wipfrom
joshuaswarren:agent/jw16-agx-stats2
Closed

joshuaswarren wants to merge 6 commits into
aurora-silicon:aurora-wipfrom
joshuaswarren:agent/jw16-agx-stats2

Conversation

@joshuaswarren

Copy link
Copy Markdown
Contributor

drm/asahi: Export AGX firmware stats to /sys/class/drm/card*/device/agx_stats

Motivation

The AGX firmware already sends firmware statistics (StatsMsg:
Utilization, PowerState, PowerOn, PowerOff, FwBusy, AvgPower,
Temperature) over the statistics ring channel. drm/asahi decodes
them in drivers/gpu/drm/asahi/channel.rs::StatsChannel::poll and only
debug-logs them. The producer contract in
~/src/coreglass/docs/DESIGN.md (coreglass GPU item 1) calls for these
to be exported as a key value file on the DRM platform device so the
coreglass dashboard / live sampler can read them at 10 Hz.

What this does

Six small commits on top of aurora-wip:

  1. drm/asahi: Add stats_export module parameter — opt-out toggle
    (asahi.stats_export, default 1). When 0, no values are retained
    and no file is registered.
  2. drm/asahi: stats: Add StatsSnapshot and firmware-stats decoder —
    the per-GpuManager StatsSnapshot (latest util1..util4, pstate,
    avg_power_mw, temperature, cumulative busy_ns, jobs) and an
    update_from(&StatsMsg) decoder. A module-level SNAPSHOT_PTR
    (AtomicU64) is published for the C-side show callback.
  3. drm/asahi: Wire StatsSnapshot into StatsChannel and GpuManager —
    the snapshot is built in GpuManager::make_mgr, its raw pointer is
    published to SNAPSHOT_PTR, a clone is held by StatsChannel.
    StatsChannel::poll calls snap.update_from() on every decoded
    message when module_parameters::stats_export is non-zero.
  4. drm/asahi: Add sysfs shim for agx_stats — C sidecar
    drivers/gpu/drm/asahi/sysfs.c that owns the device_attribute
    and the formatted key value show callback. Bindgen for this
    kernel tree does not pull in <linux/sysfs.h>, so device_create_file is
    not available to Rust — the shim is the lowest-risk bridge. The
    Rust side declares the FFI in 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 atomic, so
    IRQ-side update_from and sysfs show never need a lock.
  5. drm/asahi: Register agx_stats sysfs file on probe and unregister on
    drop
    — stash the platform device's raw struct device * 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.
  6. Documentation/ABI: Document sysfs-driver-asahi-agx-stats —
    description for /sys/class/drm/card*/device/agx_stats, one key
    per line, all unit meanings and saturation rules.

What it does NOT do

  • No new uAPI.
  • No change to scheduling or power behaviour (the firmware already
    sends the messages and the channel already polls them).
  • No change to the firmware handshake.
  • No allocation on hot path; the snapshot is a stack of AtomicU*
    fields.
  • The default stats_export=1 keeps today's debug-log behaviour
    intact and adds the retention file alongside.

Producer contract

The file format matches the coreglass producer contract exactly:

busy_ns <u64>
jobs <u64>
pstate <u32>
power_mw <u32>
util1 <u32>
util2 <u32>
util3 <u32>
util4 <u32>
temperature_raw <u32>
temperature_scale <u32>

Validation

Field semantics are documented per-key in
Documentation/ABI/testing/sysfs-driver-asahi-agx-stats and in
drivers/gpu/drm/asahi/stats.rs. The firmware field meanings
(units of util1..util4, the units of FwBusy timestamps) are
unconfirmed as of right now and need validation against a controlled
MLX load:

  • On T6001 (M1 Max, 13.5): the firmware FwBusy timestamp unit is
    reported by the firmware as nanoseconds; convert via delta against
    the prior FwBusy message. Validate by reading busy_ns under
    idle vs an MLX 4096×4096 fp16 matmul loop — expect flat under
    idle and a steady delta under load.
  • The four Utilization u32 fields are reported raw, as the
    firmware does not pre-scale them. We surface them unscaled per
    the producer contract. Per-chip scaling will land in a follow-up
    once validated on M1 (T8103), M2 (T6021), and M2 Ultra (T8140).

The assignment's hardware feasibility check on jw16 found
CONFIG_DRM_ASAHI=y (built-in) on the stock kernel, so the OOT
module-swap gate that the PR155 ane driver used is not available
on jw16
. The patch is therefore staged for a one-shot kernel boot
on a separate machine (T6021 M2 Max, jw14m2) where the w73 lane has
a proven boot/fallback recipe. A module swap is also impossible on
jw14m2's stock kernel; the field-semantics validation will land on
a dedicated one-shot boot. Module swap is only viable in places where
the asahi driver is built as a loadable module — currently none in
the lab fleet.

The hw-validation milestones on jw16 are reduced to: rustfmt clean,
checkpatch-clean on the patches (this tree has no scripts/checkpatch
copy; manual review against Documentation/CodingStyle follows). The
in-tree patch keeps the unsocketed part of the validation gated on
the M2 lane handoff.

Out of scope (separate PRs)

  • Producer-contract item 1 — the cumulative busy_ns and jobs keys
    for the ANE engine (omarchy-ane, not linux-aurora). That lands
    in omarchy-ane per DESIGN.md and is a separate entry.
  • Mesa-side per-core work assignment (DESIGN.md GPU item 2).
  • macOS-side validation of util1..util4 scaling.

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).
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.
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.
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.
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.
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.
@joshuaswarren

Copy link
Copy Markdown
Contributor Author

Superseded by #168 (same base, rebuilt series).

Reasons for the replacement, factual only:

  1. The six commits on this branch carry no Signed-off-by lines. Adding them
    requires rewriting history, i.e. a force-push to an open PR branch, which
    we do not do.
  2. The series as committed does not compile: DEVICE_ATTR_RO(agx_stats)
    expands .show to agx_stats_show, but the callback was named
    asahi_agx_stats_show (drivers/gpu/drm/asahi/sysfs.c).
  3. The replacement branch also gives jobs a real call site (submission
    completion in JobFence::command_complete) and makes the ABI document
    match exactly the keys the file prints (no temperature_tmin/tmax, u64
    wrap semantics for busy_ns).

Branch: joshuaswarren/aurorasilicon-linux agent/jw16-agx-stats4.

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.

1 participant