Skip to content

drm/asahi: g16: isolate SCS compute to same-VM SCS co-residency - #3045

Merged
DjDeveloperr merged 7 commits into
mainfrom
djdev/g16-scs-compute-isolation
Nov 25, 2026
Merged

DjDeveloperr merged 7 commits into
mainfrom
djdev/g16-scs-compute-isolation

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What: Fixes data loss and the follow-on faults when G16 (M4) ray-query (SCS) compute runs beside other compute: SCS compute now shares the compute engine only with SCS compute of its own VM. Stacked on #3044.
Tested: M4 Mac mini (J773g, G16G B0, 26B123) with the full stacked series on an earlier build, not this head: vk-rt gpubench 98/98, gpubench 440/440, dEQP-VK.ray_query.* 1,382/1,382, 0 faults. Host test-publication 81/81.
Risk: two-way · M4 (J773g, J713)
Not yet: a row at the current head; CI, which runs once the base lands and this PR targets main.

Details (original description)

Summary

Fixes data corruption and subsequent unmapped write faults during concurrent ray-query (SCS) compute and non-SCS / cross-VM compute on Apple M4 (G16G):

  • Each compute work item programs the compute engine's SCS window state in its register list (enabled for ray-query/SCS items, disabled for non-SCS items).
  • Previously, while SCS compute was pinned to pipe 0 and held while pipe 1 was busy, another VM's compute or the same VM's non-SCS compute could still queue behind an SCS item on pipe 0 (and vice versa). Under concurrent ray-query + long-ALU workloads, overlapped ray-query dispatches signalled complete with ~33k–35k of 131,072 rays still holding the fill pattern, and delayed L1 writebacks faulted into the closed VM's address space (write unmapped level 3 unit 0x1), exhausting all eight fault banks and wedging firmware recovery.
  • Restricts SCS compute (ray queries and every compute command of a job with SCS scratch) to share the compute engine only with SCS compute of its own VM. Non-SCS compute and other VMs' compute wait for SCS compute to retire (and vice versa), while a single ray-query queue continues to pipeline its own commands and renders remain unaffected.

Verification

  • Host selftests: tools/testing/selftests/drivers/gpu/asahi-m4/test-publication.py — 81 passed, 0 failed (including scs_compute_shares_the_compute_engine_only_with_scs_compute_of_its_vm).
  • scripts/checkpatch.pl --strict: 0 errors, 0 warnings, 0 checks.
  • Live Apple M4 (G16G B0, 26B123) silicon:
    • Concurrent ray-query + long-ALU stress (5/5 compute.long headless): 15,467,486 ray checks, 0 wrong rays, 0 faults, 0 recoveries.
    • vk-rt gpubench: 98/98 PASS (1.127× vs Metal 4; asbuild 1.638×, rays 1.226×, scene 0.965×).
    • Full gpubench suite: 440/440 PASS across 4.00M GPU jobs (0 faults, 0 recoveries, 0 bad CRCs).
    • Vulkan ray-query CTS (dEQP-VK.ray_query.*): 1,382/1,382 PASS (26/26 stress, 516/516 builtin, 180/180 position_fetch, 480 AS ops).

Implement cleanroom G16G (M4) hardware ray tracing support on top of the
M4 production tree, advertising DRM_ASAHI_FEATURE_RENDER_RAY_QUERY and
DRM_ASAHI_FEATURE_BOUNDED_AS_BUILD:

- Add pure RT validation, Gen1 SCS sizing, and intersection table helpers
  in g16/rt.rs (with TAIL_ENABLED = 0x2c), plus UAPI flags/reservations in
  asahi_drm.h and params.rs.
- Extend command.rs stream parsing and SpanBatch VM admission for render
  ray-query stages, compute ray/SCS requests, and AS-build flags without
  reintroducing per-span VM locks.
- Add demand-sized SCS UMA pools and freelist placement (0x80000 records
  in dedicated SCS windows) across slots.rs, pools.rs, resources.rs,
  hw.rs, and fw/buffer.rs, isolating SCS vs base arenas on reuse while
  retaining firmware PB/UMA leases across closed-VM reclaim.
- Wire RT register encoding and J773g SCS bracketing (0x017e1 = 1 first,
  0x017d9 = 1 last) in encode/{compute,render}.rs and engine.rs, pinning
  RT/SCS compute jobs to pipe 0, guarding horizon batching, and applying
  the 10 s long-job deadline allowance in recovery/logic.rs. Non-RT
  submits remain byte-identical.
- Add test-ray-tracing.py and update asahi-m4 host selftests.

Signed-off-by: Diljit Singh <[email protected]>
…d slots

After a firmware compute recovery permanently quarantines compute unit 0,
brand-new VMs using the 2-unit SCS compute layout previously pinned
themselves to hardware slots 0..2. Their first compute submission claimed
and retired on unit 1 (the sole healthy slot in 0..2), leaving unit 1 as
pipe 0's undisplaced latest retired lease so the second compute submission
saw compute_exhausted_within(2) == true and immediately failed with
ECANCELED, poisoning every subsequent fresh process.

Track the live quarantined compute mask in Shared::quarantined_compute_mask
and route ComputeUnitSet::new_scs onto two healthy hardware slots,
preferring the pipe-0-reserved 0..PIPE1_COMPUTE_START range so pipe 1
cannot pin an undisplaced lease on SCS slots, then falling back across the
remaining units. Apply the matching quarantined-slot skip when picking base
warm PB slots for new render arenas.

Signed-off-by: Diljit Singh <[email protected]>
Commit 7a8472835197 ("drm/asahi: g16: route SCS compute and base PB
slots around quarantined slots") replaced the [home, home + 1] warm PB
pair of a new render arena with a quarantine-aware picker.

Verify that with nothing quarantined the picker returns the contiguous
[home, home + 1] pair for every start, and that sixteen render-only VMs
use every warm slot once as home and once as alternate regardless of
earlier round-robin cursor motion.

Signed-off-by: Diljit Singh <[email protected]>
The 26B123 G16G firmware learns which slots run shader-context scratch
(SCS) work from a u32 in each start command: StartCompute +0x40,
StartTA +0x60 and StartFS +0x78. It ORs the slot into a per-engine SCS
mask, appends 0x017d9 = 1 to that kick's register list for masked
slots, and when its scheduler goes idle it tears the SCS state down
(0x017e0 = 0, the compute and TA windows 0x107a0/0x10798 = 0xff0000,
0x100e8/0x100f0/0x10408/0x10410 = 0x100) unless a masked slot is still
busy.

The G16 encoders always left the three words zero, so the teardown ran
at the first idle after every ray-query kick, while the dispatch was
still executing. Compute and ray-generation ray queries then lost
traversal mid-dispatch and returned misses without a fault, and
fragment and tiling ray queries stalled their stage until the firmware
recovered it (events 0x71 and 0x70).

macOS 27.2 on Apple M4 sets StartCompute +0x40 on every compute item
whose register list opens with 0x017e1 and leaves it clear on all
others. This is the M4 counterpart of the G17P kick header's SCS bit
(bit 61), which g17 sets for compute and per render stage. Set each
word exactly when the stage's register list enables SCS.

Non-ray-tracing work is byte-identical. The host check now also requires
the flag on ray-query compute, TA and FS sequences and nothing else to
change there.

Signed-off-by: Diljit Singh <[email protected]>
A render pass slot index is device-global and claimed by one flight at
a time, so render microsequences and barrier records need not be
allocated per VM. Mapping them with firmware-cached leaf 0x443 inside a
per-VM render arena is unsafe on client teardown because
KernelMapping::drop() fail-stops an ASC-cached M4 mapping that has no
qualified teardown.

Allocate render microsequences and barrier records once per pass slot
index, on first use, from a device-lifetime allocator that is never
dropped (resources::RenderCmds, reached through Factory), and let every
VM's PassSlot of that index share them. Each microsequence keeps its
own page and the barrier records share one page, matching compute unit
barriers: 48 KiB per index (at most 2.25 MiB per boot) instead of
128 KiB per pass slot of every VM.

Class::FwCoherent (firmware leaf 0x443 behind a cacheable host mapping,
matching macOS 26B123 per-queue command objects) is accepted only by
this device-lifetime allocator and never with a client or lower alias.
Per-VM arenas and the session allocator reject it, so closing a client
can never drop a firmware-cached mapping.

Extend the host resource tests to verify that two VMs' pass slot k
share one set, no VM arena owns a firmware-cached object, dropping a VM
retains the set, and other allocators reject Class::FwCoherent. Pass
slot backing drops from 608 to 480 KiB of metadata per VM.

Signed-off-by: Diljit Singh <[email protected]>
Move the TA and FS work images, the larger part of what the firmware
reads per render, into the per-index device-lifetime RenderCmds
records. macOS 26B123 maps its per-queue command objects, work items
included, with the firmware-cached leaf 0x443 and maps the same pages
into GPU context 0 with the uncached leaf 0xc8b. The work images keep
that uncached kernel GPU task lower alias.

Because the host writes to the work images through a cacheable mapping
while the GPU reads their register lists through the uncached alias,
FwObject cleans each store to such an object to the point of coherency
(dc cvac, then dsb sy). Only a firmware-cached object with a lower
alias performs this clean; the microsequences and barriers, which only
the firmware reads, are unchanged.

Each pass slot index now holds five device-lifetime pages (80 KiB, at
most 3.75 MiB per boot) and every VM pass slot sheds two more pages
(448 KiB of metadata). Compute command objects are unchanged because
the host reads firmware-written descriptor counters from the compute
work image.

Signed-off-by: Diljit Singh <[email protected]>
Ray-query compute on G16G runs with shader-context scratch (SCS). Each
compute work item sets the compute engine's SCS state in its register
list: a ray-query item enables the SCS window, and every other item
writes it disabled.

The engine pins ray-query compute to pipe 0 and holds it while compute
runs on pipe 1. It still lets another VM's compute, or the same VM's
non-SCS compute, queue behind a ray-query item on pipe 0, and lets a
ray-query item queue behind such compute.

Such an overlapped ray-query dispatch is reported complete before its
results reach memory. With a ray-query process and a long-ALU process
sharing the GPU, 33,091 to 35,419 of 131,072 rays still held the
buffer's fill pattern when the fence signalled, with every Mesa build
tried. Once the ray-query process exited, the rest of its stores were
written back from the shader cores' L1 cache into its closed VM:

  fault bank 4: status 0x283a03a1 va 0x5bf27c5180
    (write unmapped level 3 unit 0x1 ctx 29)
  attribution: busy=1 ctx=29 flight_matches=0 live_vm=None flights=9

When those writes filled all eight fault banks, the firmware did not
finish the recovery and the GPU stopped.

Let SCS compute (ray queries, and every compute command of a job with
SCS scratch) share the compute engine only with SCS compute of its own
VM. Other compute neither queues behind it nor runs while it is
published; each waits for the other to retire. One ray-query queue
still pipelines its own commands, and renders are not affected.

Signed-off-by: Diljit Singh <[email protected]>
@DjDeveloperr
DjDeveloperr force-pushed the djdev/g16-rt-scs-flags branch from ee2bc3a to e6d8e0a Compare November 24, 2026 07:34
@DjDeveloperr
DjDeveloperr force-pushed the djdev/g16-scs-compute-isolation branch from 5134fa7 to d1e55b9 Compare November 24, 2026 07:34
@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026
@github-actions github-actions Bot added blast:m4 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) and removed needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at d1e55b93: not ready yet

Check Detail
CI at head ok no CI for this change (the hardware row is the gate)
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head no no line with the head SHA d1e55b93, a Mac and a result in the PR body or a comment
Conflicts ok not computed yet
PR text ok Risk: two-way · M4 (J773g, J713)

Areas: area:gpu

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at d1e55b93c9: FINISH, then merge. No code blockers; one row is missing.

Blockers:

  1. No hardware row at this head. The rows in this PR ran the full stack on an earlier build, not this branch as it is now. Fix: one J773g run at this head, or on a candidate that contains this head unchanged (see below).

Checked: the one commit against its base: scs_compute_conflict() keeps SCS and non-SCS compute apart in both directions and keeps other VMs' SCS compute out, while one VM's ray-query queue still pipelines; it is used in both the drain and the queue-behind tests; no Apple material in code or commit messages; no chip or board gates; no new parameters; only bounded waits; commits carry human authors and Signed-off-by, no AI attribution; CI does not run on a stacked base and will run once this PR targets main. This is the fix #3044 needs, so the two should land together. I reformatted the PR body to What / Tested / Risk / Not yet, with the original kept folded and its attribution line removed.

Row

One J773g run on a candidate that contains the heads of #3087, #3057 and #3046 unchanged (boot, idle/wake, deep sleep, vk-mustpass-short, gpubench vk-full, compute.long, 0 GPU faults) counts for every non-draft PR in this stack at once.

@DjDeveloperr
DjDeveloperr changed the base branch from djdev/g16-rt-scs-flags to main November 25, 2026 01:37
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 25, 2026 01:37
@DjDeveloperr
DjDeveloperr merged commit 9abf757 into main Nov 25, 2026
3 checks passed
@RyanTheTide
RyanTheTide deleted the djdev/g16-scs-compute-isolation branch November 27, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:gpu Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant