Skip to content

arm64: idle: stop the Apple CPU PMU counters across WFI - #3010

Merged
RyanTheTide merged 1 commit into
integration/aurorafrom
satchlj/arm64-apple-pmu-wfi-freeze
Nov 23, 2026
Merged

RyanTheTide merged 1 commit into
integration/aurorafrom
satchlj/arm64-apple-pmu-wfi-freeze

Conversation

@satchlj

@satchlj satchlj commented Nov 23, 2026

Copy link
Copy Markdown
Member

Summary

On M4 and later Apple cores, some CPU PMU cycle counters keep ticking across WFI (for example, M5 Max Sawtooth cores continue counting while idle), inflating CPU-wide perf cycle counts with idle time unlike other arm64 PMUs. Because PMU register state survives WFI intact, saving and restoring the full counter bank is unnecessary.

While perf events are active on a CPU, have the Apple CPU PMU driver maintain a per-CPU flag so the Apple WFI idle path clears the counter enable and IMODE bits in PMCR0_EL1 around WFI and restores PMCR0_EL1 on exit. When no perf events are active on the CPU, the idle path performs no extra register writes. No runtime switches or model-specific gates are added.

Verification

  • Static checks: scripts/checkpatch.pl --strict --git origin/integration/aurora..HEAD — 0 errors, 0 warnings, 0 checks; W=1 arm64 build — 0 warnings on modified files.
  • Arm64 KUnit (qemu-system-aarch64): apple-m1-cpu-pmu — 44/44 passed, 0 failed.
  • Hardware validation on lobata (Mac Studio M5 Max / J775c / T6050):
    • Provenance: Combined Wave-1 test image built from origin/integration/aurora (880a97a3dca2) + this exact commit (f0140e6ab816), Image.gz SHA256 51562a4abc14db4ab11b2f066129df645f012e4c09085751d32598b2d44fb21a, vmlinux GNU build-id d041b92777dbb0670ea0a44dda3f14ff1687164e, paired with J775c board DTB 0442bec28ef4c650d4e3395589f7001808c7e5b4a3bdd4971fc466d68f9de0ed (mixed-DTB boot because tip's t6050-j775c.dts is still a disabled stub).
    • Boot ID: fbbba97e-4464-4855-9874-d2d98550148d, default kernel cmdline (rdinit=/init nr_cpus=18 maxcpus=18 root=UUID=3da7492b-6c5e-4e84-9eac-65b89fae44c6 arm64.nomte).
    • PMU probe: hw perfevents: enabled with apple_t6050_pmu PMU driver, 10 (0,000003ff) counters available.
    • 1 s idle perf stat -a:
      • cpu0 (apple,sawtooth): 3,734,540 cycles / 3,991,717 instructions (frozen across WFI, not ticking at ~1.5 GHz).
      • cpu12 (apple,everest): 5,753,092 cycles / 4,818,667 instructions.
    • Busy workload pinned to cpu12 (0.218 s): 899,548,188 cycles / 7,594,758,125 instructions (active counting unaffected).
    • Health tuple: 0 SELinux AVC denials; 0 new PMU/idle kernel errors (5 pre-existing dmesg -l err lines and ac-power-mode.service failure are unrelated tip-vs-J775c-DTB mismatches: second SPMI Gen4 IRQ -95, apple_aop -61, apple-dcp clock/lane warnings, missing SMC ac_power_mode sysfs).

Assisted-by: LLM (Google:Gemini 4 Neon)

On M4 and later Apple cores, some cycle counters keep counting during WFI
(for example, M5 Max Sawtooth cores count at ~1.5 GHz while idle), which
inflates CPU-wide perf cycle counts with idle time unlike other arm64 PMUs.
The PMU registers themselves survive WFI unchanged, so saving and restoring
the full counter state is not needed.

While perf events are active on a CPU, have the Apple CPU PMU driver set a
per-CPU flag so the Apple WFI idle path clears the counter enable and IMODE
bits in PMCR0_EL1 before WFI and restores PMCR0_EL1 on exit. When no perf
events are active, the idle path does no extra register writes.

Signed-off-by: Satya Benson <[email protected]>

@DjDeveloperr DjDeveloperr 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.

Formal §1–§6 pre-PR checklist review at f0140e6ab816ab71e4c70943e714692905fef68d.

Clean, minimal fix:

  • apple_wfi() runs only inside cpu_do_idle() on the existing apple_wfi_needs_save() path with both IRQs and FIQs masked (arch/arm64/include/asm/irqflags.h masks IRQ and FIQ together), so no PMU FIQ can preempt between reading and restoring PMCR0_EL1.
  • pmcr0 survives apple_cpu_wfi() via AAPCS64 callee-saved registers / stack, and zero extra sysreg accesses occur when apple_pmu_wfi_freeze is false.
  • apple_pmu_wfi_freeze is properly toggled in m1_pmu_start(), m1_pmu_stop(), and m1_pmu_reset() (covering CPU offline and CPU_PM_EXIT), with clean !CONFIG_ARCH_APPLE stubs and safe behavior on non-Apple arm64.

§1–§6 Checklist

  • §1 No model gating: Pass (no new chip/SoC/board gate; hooks existing apple_wfi_needs_save() and per-CPU active-event flag).
  • §2 Production-ready, no hacks: Pass (default cmdline, no runtime switches or delays).
  • §3 Safety: Pass.
  • §4 Clean-room & attribution: Pass (human author/committer + human Signed-off-by: only, zero AI trailers in commit; PR body includes Assisted-by:).
  • §5 Evidence: Pass (44/44 arm64 QEMU KUnit apple-m1-cpu-pmu, live lobata M5 Max J775c 1 s idle and busy perf stat measurements + health tuple, CI host-tests passed).
  • §6 Naming: Pass.

Assisted-by: LLM (Gemini 4 Neon)

@RyanTheTide
RyanTheTide merged commit 9540f80 into integration/aurora Nov 23, 2026
1 check passed
@RyanTheTide
RyanTheTide deleted the satchlj/arm64-apple-pmu-wfi-freeze branch November 27, 2026 13:21
satchlj pushed a commit to satchlj/linux that referenced this pull request Dec 1, 2026
Commit 4fa08b4 ("soc: apple: mailbox: drain G17 receive interrupts
in hard IRQ") moved the ASCWrap v6 GPU receive path out of the oneshot
IRQ thread: normal completions are now drained in hard IRQ, and the
thread runs only when the receive budget saturates.

The thread was doing more than adding a wakeup. It is an RT task, so
while it runs schedutil raises its CPU cluster to the top frequency, and
the G17 driver steers that through apple_mbox_set_rx_util_min():
Port::rx_update() keeps the RT default for host-bound streams and lowers
it only while the GPU is the bottleneck. Since 4fa08b4 the
completion of every light submission runs without that boost, from
whichever CPU takes the interrupt.

On the MacBook Neo (J700, A18 Pro) gpubench-vk compute.light regressed
with that commit alone. Built on its own parent with only its merge
reverted, 6 reps each:

  row                               with   reverted   7.3.0-41
  compute.light queue/d2 (us)       52.3     35.3       35.5
    submit CPU (us)                 12.0      9.6        9.7
    latency (us)                     110       64         64
    reps that lose the overlap       6/6      0/6        0/6
  compute.light queue/d1 (us)      106.0    102.1      102.3
  render.light queue/d1 (us)       137.8    122.1      127.0

Bisected on J700 from 7.3.0-41 (6f618bc, rebuilt with the same
toolchain and config as every other point) to 7c9d9ad: the
regression first appears in 3989d21..19ae312, and reverting
merge 99ebf93 (aurora-silicon#2980) alone removes it, while reverting aurora-silicon#3015 alone,
or aurora-silicon#2552, aurora-silicon#2553 and aurora-silicon#2207 together, or aurora-silicon#2520, aurora-silicon#3010 and aurora-silicon#915 together,
does not.

Restore the threaded receive handler for ASCWrap v6 G17 mailboxes. Keep
that commit's exclusion of ASCWrap v8 (T8152) from apple_mbox_is_g17(),
so the T8152 GPU mailbox stays on apple_mbox_recv_irq() with its storm
guard as it is now, and keep a KUnit case for that exclusion.

With "drm/asahi: g17: keep G17P work nodes and compute descriptors
firmware-cached" on origin/main 16da002, a one-shot J700 boot gives
compute.light d2 34.5 us (submit CPU 9.5 us, 0/9 reps losing the
overlap), d1 104.1 us, render.light d1 124.9 us; the multi_queue CTS
family 4266/0 three times, compute CRCs 5/5, Geekbench 6 Vulkan 8/8,
and no GPU fault, WARN or AVC.

Signed-off-by: Diljit Singh <[email protected]>
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.

3 participants