Skip to content

drm/asahi: g17: let the firmware write render fragment end timestamps - #1229

Merged
RyanTheTide merged 4 commits into
aurora-silicon:integration/aurorafrom
jgoyvaerts:pr/g17-fw-end-timestamps
Nov 10, 2026
Merged

RyanTheTide merged 4 commits into
aurora-silicon:integration/aurorafrom
jgoyvaerts:pr/g17-fw-end-timestamps

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

The G17 firmware can write a render's fragment end timestamp itself, the way macOS has it do. Today the kernel converts the pass's internal stamp and writes it into the user's buffer at retirement. No uAPI change.

1. drm/asahi: g17: keep dynamic kernel objects below the MMIO aperture

  • The upper window for dynamic kernel objects ran from KERNEL_OFFSET to the end of the kernel range, past the firmware MMIO aperture at 0xfffffc2180000000. The firmware gives addresses from the aperture up a fixed meaning (register windows and the gart ranges HwData names), so an object allocated there could land on one of them.
  • The bounds move into one helper, dynamic::upper(), used by every allocation from that window. Commit 2 needs this, because its window lies inside the aperture.

2. drm/asahi: g17: let the firmware write render end timestamps

  • What macOS does (measured there with DTrace and Metal counter sample buffers):
    • It maps a timestamp buffer a second time, into the gart range HwData.unk_28 names ([0xfffffc2181400000, +64 MiB)).
    • It puts window addresses into each render's second stamp pair: TA +0x90e (vertex start/end) and 3D +0x21a8 (fragment start/end).
    • The firmware then writes those stamps in nanoseconds, equal to the host's conversion of its internal stamps.
  • This commit:
    • Timestamp buffers are mapped into that window, firmware-only like the existing alias.
    • The firmware gets the window address of each render's fragment end only. The other three stay host-written:
      • Start stamps: the firmware latches them before the command's barriers resolve, so a reset of the same query ordered before the command can still land on top of the stamp.
      • The vertex end: handed to the firmware, it does not hold the host's value by the end of the wait below. With hk's frame profiler, which also stamps every render's vertex end, exactly half of 312,088 window stamps fell back to the host. hk also puts occlusion availability on that slot.
    • At retirement the kernel polls the destination, read-only, for the expected value, for at most 200 µs. It never writes a stamp the firmware delivered: a late write could undo a reset that userspace may make once the fence has signalled. If the value has not landed in time, the host writes it, still before the fence.
    • Compute commands, end-stamp copies and buffers that don't fit in the window keep host-written stamps.
    • asahi-g17/capacity gains a timestamp_window firmware=<kept> host_fallbacks=<host-written> line.

Testing (J616s, M4 Pro, firmware 25G83)

  • On hardware, in our daily kernel m4.148 (the series on an older integration base, with both end stamps windowed):
    • A tsprobe stress test (reset, then stamp, 1,200 draws at top/bottom of pipe): 0 stamps lost.
    • dEQP-VK.pipeline.monolithic.timestamp.* in one process: 252 pass, 26 not supported, 0 fail.
    • Astro Bot under KytyPS5 overnight: over 8 million firmware-written fragment end stamps, 0 fallbacks, no GPU recoveries.
    • fps unchanged.
  • The fragment-end-only change (window_slot() in completion.rs) came from the fallback count above. On this branch it is checked by compile and KUnit, not yet booted: on hardware so far, fragment end stamps never needed the fallback.
  • This branch: clean on integration/aurora 27c200da5d28. rust/ and drivers/gpu/drm/asahi/ build with no new warnings. The asahi KUnit suites (arm64 qemu): 231 tests, 229 pass, 2 skipped, including the new asahi_g17_timestamp_window.

A related but independent Mesa change, in its own aurora-silicon/mesa PR: hk serves every G17P timestamp from a command's end, because start stamps can't be ordered behind a reset on G17P whoever writes them.

🤖 Generated with Claude Code

The upper window for dynamic kernel objects ran from KERNEL_OFFSET to
the end of the kernel range, past the firmware MMIO aperture at
0xfffffc2180000000. The firmware gives the addresses from the aperture
on a fixed meaning: the register windows, and the gart ranges HwData
names. An object allocated there would take an address the firmware
treats as something else.

Put the bounds in one helper, dynamic::upper(), and use it in every
place that allocated from that window.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
Assisted-by: LLM
Render end timestamps are written by the host at retirement: the
kernel converts the pass's internal stamps to nanoseconds and stores
them into the user's timestamp buffer.

The firmware can do that itself. macOS aliases timestamp buffers into
the gart range HwData.unk_28 names ([0xfffffc2181400000, +64 MiB)) and
puts window addresses into each render's second stamp pair, TA +0x90e
(vertex start/end) and 3D +0x21a8 (fragment start/end). The firmware
then writes those stamps in nanoseconds, equal to the host's conversion
of its own internal stamps.

Bind timestamp buffers into that window, firmware-only like the other
alias, and hand the firmware the window address of each render's
fragment end destination. The other three stay host-written:
- Start stamps: the firmware latches a start stamp before the command's
  barriers resolve, so a reset of the same query ordered before the
  command can still land on top of it.
- The vertex end: handed to the firmware, it does not hold the host's
  value by the end of the wait below. Astro Bot under KytyPS5 with
  hk's frame profiler, which also stamps every render's vertex end,
  sent 312,088 stamps through the window; exactly half (156,044) fell
  back to the host, each after the full wait. Without the profiler
  none did.

At retirement the kernel reads the fragment end destination, without
writing, until it holds the expected value (at most 200 us), and does
not write it. A host write, or a firmware write landing after the fence,
could undo a reset userspace may make once the fence has signalled. A
value that does not land in time is written by the host, still before
the fence. On an M4 Pro every fragment end stamp had landed by the first
read.

Compute commands, end-stamp copies and buffers that do not fit in the
window keep host-written stamps too. asahi-g17/capacity counts kept and
host-written window stamps. No uAPI change.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
Assisted-by: LLM

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

host-tests fails on this head in g17-t6041-initdata-tests and t6040-calibration-tests. Both pass on integration/aurora without this branch. Those harnesses compile fw/initdata.rs on its own, and HwData::init now names crate::hw::t8140::dynamic::TIMESTAMP_WINDOW, which isn't available there:

error[E0433]: cannot find `hw` in `crate`
1054 |         self.unk_28 = crate::hw::t8140::dynamic::TIMESTAMP_WINDOW.start;

Either add a hw::t8140::dynamic stub with TIMESTAMP_WINDOW to those two harnesses, or keep the literal in fw/initdata.rs and v25g83.rs with a const_assert!/comment tying it to dynamic::TIMESTAMP_WINDOW.start.

One thing to check in the code: Destination::write_window() busy-waits with udelay for up to 40 x 5 us for each windowed fragment end. That is fine while the stamp has always landed by the first read, as reported. But if the firmware ever stops writing through the window (window mapping refused, firmware variant), every render with a fragment end stamp pays the full 200 us in retirement. A counter-based fallback might be worth it: stop handing out window addresses after N consecutive host fallbacks.

The host tests g17-t6041-initdata-tests and t6040-calibration-tests
compile fw/initdata.rs and v25g83.rs on their own, without crate::hw, so
naming hw::t8140::dynamic::TIMESTAMP_WINDOW in HwData::init broke both.

Write the base as a literal again, and assert in hw/t8140.rs that
TIMESTAMP_WINDOW still starts there, so the two cannot drift apart.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
Assisted-by: LLM
…write

Destination::write_window() waits up to 40 x 5 us for the firmware's
fragment end stamp before the host writes it. The stamp has always
landed by the first read, but if the firmware ever stops writing through
the window (the mapping refused, another firmware), every render with a
fragment end stamp would pay the full 200 us in retirement.

After 8 host fallbacks in a row, hand no render a window address again;
its stamps then go the host path with no wait. A stamp the firmware
writes resets the count.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
Assisted-by: LLM
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