Skip to content

Apple M4 Pro and A18 Pro CPU performance counters (one PMU per SoC) - #865

Merged
RyanTheTide merged 7 commits into
integration/aurorafrom
gaps/apple-pmu-m4-a18
Nov 3, 2026
Merged

RyanTheTide merged 7 commits into
integration/aurorafrom
gaps/apple-pmu-m4-a18

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

CPU performance counters for the M4 Pro (T6040, Brava cores) and the A18 Pro (T8140, Tahiti cores). Until now neither machine had a hardware PMU in perf, so perf stat and perf record only had software events.

What the hardware does

These were measured with a throwaway kernel module. It programs the PMU on one CPU of each core type and counts five reference loops with a known instruction mix, with interrupts off:

  • A: add, subs, b.ne, 100000 iterations
  • B: random tbz from xorshift, 100000 iterations
  • C: loads with a 64-byte stride over 1 MiB, 16384 iterations
  • D: loads that hit L1 in 4 KiB, 16384 iterations
  • E: random indirect calls, 100000 iterations

The results are identical on Brava E/P and Tahiti E/P:

item result
PMC0 / PMC1 cycles / instructions; PMC1 is exact (loop A 300018-300023)
counter width 64 bits on all ten (all-ones reads back)
overflow PMSR sets when a counter crosses bit 63, not bit 47 (M2 behaviour)
PMCR1 M1 EL0/EL1 filter layout
PMESR0/1 16-bit event field per counter (2-5, 6-9), not M1's 8-bit. With 0x0008 in every field, PMC2-7 all read the PMC1 instruction count
PMC8/9 stay at 0 for every selection tried, so not exposed

The event numbers are new; the M1 numbers count nothing. Counts per iteration:

event A B C D E meaning
0x08 3 6.5 5 6 9.5 instructions (= PMC1, exact)
0x21 1 2 1 1 3 branches (exact)
0x22 0 0.33-0.49 0 0 0.5 mispredicted branches, conditional and indirect (0x10 counts the same)
0x04 0 0 1 1 0 loads
0x03 0 0 0.58-0.78 ~0 0 L1D load misses (0x39 counts the same)

No configurable event in 0x000-0xfff counts cycles, so cycles stay on PMC0.

One PMU per SoC

The E and P cores share the event encoding, the 16-bit fields and the working counters, as the tables above show, so one PMU instance covers both clusters. That matters for KVM. A guest PMU is backed by perf events on one host PMU. With per-cluster PMUs, those events stop counting while the vCPU thread runs on the other cluster, unless the VMM uses KVM_ARM_VCPU_PMU_V3_SET_PMU and pins the vCPUs. One PMU spanning all CPUs keeps guest counters counting across migration. #731 took this approach for the fixed counters; this PR keeps it and adds counters 2-7 and the event table.

The test is kvmpmu, a small standalone program: one vCPU with an emulated PMUv3 counts PMCCNTR and INST_RETIRED over a fixed loop, while the host moves the vCPU thread between an E and a P core after every loop. On the M1 Pro, which has per-cluster PMUs, the guest counts 2000005 instructions per phase on the P core and 2 on the E core (6 of 12 phases counting). The Neo result with this PR will be posted below.

Changes

  • dt-bindings: apple,t6040-pmu and apple,t8140-pmu. The first two commits named per-cluster compatibles; a later commit replaces them.
  • apple_m1_cpu_pmu: a PMU for these SoCs with counters 0-7, 16-bit event fields, 63-bit overflow, the event names in sysfs (format/event is config:0-15), and the PMUv3 common-event map for KVM guests (INST_RETIRED, CPU_CYCLES, BR_RETIRED). The driver's private filter flags move above the 16-bit event field. Event placement is now per PMU. M1 and M2 behave as before.
  • t6040/t8140 DT: one pmu node on the AIC P-PMU FIQ. The AIC already delivers both clusters' PMU FIQs as that interrupt.
  • tools/asahi/apple-m4-pmu-tests.py: a host test of the event field layout, counter affinity and flag placement, with four mutations that must be caught. It is registered in run-host-tests.py.

Testing

  • Host test: 5/5 passing (control plus 4 mutations killed). checkpatch is clean apart from the usual MAINTAINERS note for a new tools/asahi file.
  • M1 Pro regression check of the shared M1/M2 path: passed (see the comment below).
  • J616s (M4 Pro), a one-shot boot of integration/aurora f153499 with this series and arm64: dts: apple: enable the SD reader on the J616s too #867 (7.3.0-aurora-fleet14p-gaps2):
    • hw perfevents: enabled with apple_t6040_pmu PMU driver, 8 (0,000000ff) counters available, one PMU with cpus=0-13. Its sysfs events are cycles, instructions, branches, branch-misses, loads and l1d-load-misses.
    • perf stat on the reference loops, 100 reps, P core (cpu4) and E core (cpu0). Loop a, P: 30348599 instructions, 10076400 branches, 4778 branch-misses. Loop b, P: 65345262 instructions, 20076604 branches, 4868396 branch-misses. The E core gives the same instruction and branch counts (30348611 and 10076405 on loop a).
    • Loop c raw events: loads 1826476 and l1d-load-misses 1273463 (16384 loads per pass, 100 passes, plus process startup).
    • perf record -e cycles: 12K samples on P and 10K on E, 0 lost, about 99.6% in the hot loop. This exercises the overflow FIQ.
    • perf stat -a over 5 s idle counts normally.
    • journalctl -p3 empty, 0 WARN/BUG/Oops, SELinux enforcing with 0 AVCs. NVMe, Wi-Fi/BT and DCPs unaffected.
  • Neo (A18 Pro): still to do, including kvmpmu across P and E cores. Results will be posted here.

🤖 Generated with Claude Code

Add the core PMU compatibles for the M4 Pro (Brava E and P cores) and
the A18 Pro (Tahiti E and P cores). The names follow the CPU node
compatibles these SoCs already use (apple,brava-e, apple,tahiti-p and
so on).

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The Brava (M4 Pro) and Tahiti (A18 Pro) cores keep the M1 PMU register
interface, but select events differently. Measured on both SoCs, on the
E and P cores, by counting reference loops with a known instruction mix
with interrupts disabled:

- PMC0 counts core cycles and PMC1 retired instructions, exactly (a loop
  of 100000 iterations of three instructions reads 300018 on PMC1).
- All ten counters are 64 bits wide, and the overflow flag in PMSR sets
  when a counter crosses bit 63, not bit 47. This is the M2 behaviour.
- PMCR1 keeps the M1 EL0/EL1 filter layout.
- PMESR0 and PMESR1 hold one 16-bit event field per counter (counters
  2-5 and 6-9) instead of the M1's 8-bit fields. With event 0x08 in
  every field, counters 2-7 all count the same instructions as PMC1.
  Counters 8 and 9 stay at zero, so they are not exposed.
- The event numbers are new: 0x08 instructions, 0x21 branches (both
  exact on every loop), 0x22 mispredicted branches (conditional and
  indirect), 0x04 loads, 0x03 L1D load misses. The M1 numbers do not
  apply.

Add PMUs for these cores that expose counters 0-7, program 16-bit event
fields, and handle 63-bit overflow. Cycles keep the M1 event number and
go to the fixed counter only, because no configurable event is known to
count cycles. Move the driver's private filter flags in config_base
above the 16-bit event field. Event placement is now a per-PMU choice,
so M1 and M2 behave as before.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Describe the E and P core PMUs on the M4 Pro and A18 Pro. Each one takes
its FIQ from the AIC, and the AIC affinities route each PMU FIQ to its
own cluster's CPUs, as on t8112.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Build the driver's event placement code on the host against a model of
PMESR0/1. The test checks that:

- M4/A18 events land in one 16-bit field per counter;
- the M1 8-bit layout is unchanged;
- cycles go only to PMC0 and instructions to PMC1 first;
- counters 8 and 9 are never used;
- the private filter flags stay clear of the 16-bit event field.

Four mutations, one per rule, must each be caught.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

M1 Pro (T6000, Firestorm/Icestorm) regression check of the shared M1/M2 path, 7.3.0-aurora-fleet14o-pmu2 one-shot (boot f6d24f06) against the fleet14o default on the same machine:

  • Both PMUs still register with 10 counters (hw perfevents: enabled with apple_firestorm_pmu PMU driver, 10 (0,000003ff) counters available).
  • perf stat cycles, instructions, branches and branch-misses over the reference loops (100 reps) on the P core, before and after:
    • loop a: instructions 30339352 / 30339551, branches 10073927 / 10074095, cycles 10273439 / 10276438
    • loop b: instructions 65335979 / 65336205, branch-misses 4900707 / 4923520
    • The E core matches within run noise as well.
  • A 12-event multiplexed group lands on the same counters as before.
  • perf record cycle sampling works: 12K samples, 99.98% in the hot loop.
  • SELinux enforcing with 0 AVCs.

@RyanTheTide
RyanTheTide changed the base branch from aurora-wip to integration/aurora November 2, 2026 16:46
The E and P cores of the T6040 (M4 Pro) and T8140 (A18 Pro) share one
PMU event encoding and counter layout, so a single PMU instance covers
both clusters. Replace the per-cluster brava/tahiti compatibles with
apple,t6040-pmu and apple,t8140-pmu.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Counting the same reference loops on both core types of each SoC gives
the same set of nonzero event numbers, the same 16-bit PMESR fields and
the same working counters (0-7) on the E and P cores. Only the
microarchitectural counts differ. One arm_pmu instance can therefore
drive both clusters.

A single instance matters for KVM. Its default guest PMU is backed by
one host PMU, and with per-cluster PMUs those backing events stop
counting whenever the vCPU thread runs on the other cluster. The only
way around that is KVM_ARM_VCPU_PMU_V3_SET_PMU plus pinning in the VMM.
With one PMU spanning all CPUs, guest counters keep counting across
migration.

Register the PMU as apple,t6040-pmu or apple,t8140-pmu, named after the
SoC. The event table, counters and overflow handling are unchanged.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Replace the per-cluster pmu-e/pmu-p nodes and their AIC affinities with
one pmu node per SoC on the P-cluster PMU FIQ. The AIC already delivers
the PMU FIQ of either cluster as that interrupt, and with no affinity
partition the interrupt covers every CPU.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide RyanTheTide changed the title Apple M4 Pro and A18 Pro CPU performance counters (perf) Apple M4 Pro and A18 Pro CPU performance counters (one PMU per SoC) Nov 2, 2026
@RyanTheTide
RyanTheTide marked this pull request as ready for review November 2, 2026 17:38
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Neo (J700, A18 Pro) hardware test of the gaps2 build (7.3.0-aurora-fleet14p-gaps2, source afdbf9b8, J700 DTB 805f9b1d with the new pmu node), one-shot Stage 2 trial with the old Stage 2 restored on the trial boot itself.

Passes:

  • One apple_t8140_pmu covering cpus 0-5, 8 counters, probed at boot with no warnings.
  • perf stat counts on cpu0 (P) and cpu1 (E) match the expected per-iteration numbers for all three loops (loop a 30.35M instructions and 10.08M branches, loop b 65.35M and 20.08M, loop c 8.54M and 1.72M). Raw l1d-load-misses and loads count.
  • Sampling through the overflow IRQ, 10K and 12K samples with 0 lost, 99%+ in loop_b.
  • System-wide idle counting works, and counting still works after one s2idle cycle on rtc1 (rc 0, woke on time).
  • journalctl -p3 empty, no WARN or Oops, SELinux enforcing with 0 AVCs.

Fails:

  • kvmpmu 13 0,1 (and the all-cpus run) stop at KVM_ARM_VCPU_INIT with PMUv3 requested: "vcpu init (PMUv3): Invalid argument". So there is no counting=12 stopped=0 result.
  • Likely cause: KVM only registers a host PMU when kvm_arm_pmu_get_pmuver_limit() reports PMUv3, and for IMPDEF PMUs that needs the ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS cap. has_impdef_pmuv3() matches apple_cpus, and the Neo's cores (MIDR 0x611f0601 and 0x611f0611, parts 0x060/0x061) are not in it. The comment above apple_broken_seis_cpus says the same for T6040, so the J616s should hit this too (its trial did not run kvmpmu). With arm_pmus empty, kvm_supports_guest_pmuv3() is false and the vCPU init is refused.

Logs are in the aurora-ctl reports under gaps-20261103/neo-gaps2-trial. The Neo is back on fleet14p with Stage 2 4015aaff.

@RyanTheTide
RyanTheTide merged commit c1d6900 into integration/aurora Nov 3, 2026
@RyanTheTide
RyanTheTide deleted the gaps/apple-pmu-m4-a18 branch November 7, 2026 12:07
DjDeveloperr added a commit that referenced this pull request Dec 1, 2026
The T8140 PMU description and driver support landed on main with #865, so take main's pmu.yaml, t8140.dtsi and apple_m1_cpu_pmu.c. Also take main's cpu_errata.c: main keeps T8140 out of apple_cpus while its IMPDEF PMUv3 traps are unverified and already treats its vGIC SEIS as broken. What this branch still adds is the bounded deferred stage-2 TLB invalidation in kvm_pgtable_stage2_unmap().

Signed-off-by: DjDeveloperr <[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.

1 participant