From 2cc18ec4a537d9eaae2ca43546b504abcab76faf Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sat, 31 Oct 2026 11:17:30 -0400 Subject: [PATCH 1/3] arm64: errata: apply the Apple vGIC (broken SEIS) workaround to T8140 KVM guests on the MacBook Neo (T8140, Tahiti cores) livelock as soon as two private interrupts are pending while the guest has IRQs masked: the vCPU takes the vGIC maintenance interrupt on every entry and never retires an instruction. This is the T6040 problem again: the Tahiti MIDRs are missing from apple_broken_seis_cpus, so kvm_patch_ich_vtr_el2() leaves vgic_ich_vtr() at its placeholder and KVM uses nr_lr = 1 although ICH_VTR_EL2 reads 0x90580007 (8 LRs, SEIS=1). With one LR, KVM sets ICH_HCR_EL2.UIE and the underflow maintenance interrupt fires the moment the guest is entered. Measured under aura's FEAT_NV2 monitor: at every exit of a 2-vCPU guest the hardware reported ICH_MISR_EL2.U with ICH_HCR_EL2 = 0x449b and only LR0 in use (alternately vINTID 27 and SGI 1). Assisted-by: Claude Opus 5.5 --- arch/arm64/kernel/cpu_errata.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/arm64/kernel/cpu_errata.c b/arch/arm64/kernel/cpu_errata.c index 6a12d12effacb..f8116630c743b 100644 --- a/arch/arm64/kernel/cpu_errata.c +++ b/arch/arm64/kernel/cpu_errata.c @@ -314,6 +314,8 @@ static bool has_impdef_pmuv3(const struct arm64_cpu_capabilities *entry, int sco static const struct midr_range apple_broken_seis_cpus[] = { MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_E), MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_P), + MIDR_ALL_VERSIONS(MIDR_APPLE_TAHITI_E), + MIDR_ALL_VERSIONS(MIDR_APPLE_TAHITI_P), {}, }; From 726afd822ee74c8412f65ad206f7bf619176c75e Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sat, 31 Oct 2026 15:46:05 -0400 Subject: [PATCH 2/3] perf: add fixed-counter PMU support for T8140 Expose the verified cycle and instruction counters through one PMU instance spanning both Tahiti clusters. Keep unknown programmable events rejected while enabling KVM-backed perf migration across P and E cores. Include the T8140 PMU DT binding and IMPDEF PMUv3 capability. Assisted-by: GPT-6.1 Sol --- .../devicetree/bindings/arm/pmu.yaml | 1 + arch/arm64/boot/dts/apple/t8140.dtsi | 6 ++ arch/arm64/kernel/cpu_errata.c | 9 +- drivers/perf/apple_m1_cpu_pmu.c | 82 ++++++++++++++++++- 4 files changed, 89 insertions(+), 9 deletions(-) diff --git a/Documentation/devicetree/bindings/arm/pmu.yaml b/Documentation/devicetree/bindings/arm/pmu.yaml index f47baaefcdacf..e7468d5fcef5e 100644 --- a/Documentation/devicetree/bindings/arm/pmu.yaml +++ b/Documentation/devicetree/bindings/arm/pmu.yaml @@ -24,6 +24,7 @@ properties: - apple,blizzard-pmu - apple,firestorm-pmu - apple,icestorm-pmu + - apple,t8140-pmu - arm,armv8-pmuv3 # Only for s/w models - arm,arm1136-pmu - arm,arm1176-pmu diff --git a/arch/arm64/boot/dts/apple/t8140.dtsi b/arch/arm64/boot/dts/apple/t8140.dtsi index 9fdd91a302412..82c3847768eba 100644 --- a/arch/arm64/boot/dts/apple/t8140.dtsi +++ b/arch/arm64/boot/dts/apple/t8140.dtsi @@ -382,6 +382,12 @@ ; }; + pmu { + compatible = "apple,t8140-pmu"; + interrupt-parent = <&aic>; + interrupts = ; + }; + clkref: clock-ref { compatible = "fixed-clock"; #clock-cells = <0>; diff --git a/arch/arm64/kernel/cpu_errata.c b/arch/arm64/kernel/cpu_errata.c index f8116630c743b..b631d1a95d41d 100644 --- a/arch/arm64/kernel/cpu_errata.c +++ b/arch/arm64/kernel/cpu_errata.c @@ -288,6 +288,8 @@ static const struct midr_range apple_cpus[] = { MIDR_ALL_VERSIONS(MIDR_APPLE_M2_AVALANCHE_PRO), MIDR_ALL_VERSIONS(MIDR_APPLE_M2_BLIZZARD_MAX), MIDR_ALL_VERSIONS(MIDR_APPLE_M2_AVALANCHE_MAX), + MIDR_ALL_VERSIONS(MIDR_APPLE_TAHITI_E), + MIDR_ALL_VERSIONS(MIDR_APPLE_TAHITI_P), {}, }; @@ -307,15 +309,10 @@ static bool has_impdef_pmuv3(const struct arm64_cpu_capabilities *entry, int sco return is_midr_in_range_list(apple_cpus); } -/* - * Apple cores with the same vGIC as M1/M2 that aren't in apple_cpus yet: - * that list also turns on the IMPDEF PMUv3 traps, unverified here. - */ +/* Apple cores with the same vGIC as M1/M2 that are not in apple_cpus. */ static const struct midr_range apple_broken_seis_cpus[] = { MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_E), MIDR_ALL_VERSIONS(MIDR_APPLE_T6040_P), - MIDR_ALL_VERSIONS(MIDR_APPLE_TAHITI_E), - MIDR_ALL_VERSIONS(MIDR_APPLE_TAHITI_P), {}, }; diff --git a/drivers/perf/apple_m1_cpu_pmu.c b/drivers/perf/apple_m1_cpu_pmu.c index 81b6f1a623499..92aae0c574a88 100644 --- a/drivers/perf/apple_m1_cpu_pmu.c +++ b/drivers/perf/apple_m1_cpu_pmu.c @@ -186,6 +186,26 @@ static const u16 m1_pmu_pmceid_map[ARMV8_PMUV3_MAX_COMMON_EVENTS] = { M1_PMUV3_EVENT_MAP(BR_MIS_PRED_RETIRED, BRANCH_MISPRED_NONSPEC), }; +/* + * A18 Pro (T8140) cores: PMC0 and PMC1 count cycles and instructions like + * the M1/M2 fixed counters, all ten counters are 64 bits wide and PMCR0 has + * the M2 layout (measured on hardware). The event numbers of the + * programmable counters are not known for these cores, so only the two fixed + * counters are offered. A single PMU instance covers both clusters so perf + * events (including KVM's backing events) keep counting when a task migrates. + * This does not establish compatibility for any programmable events. + */ +static const u16 tahiti_pmu_event_affinity[M1_PMU_PERFCTR_LAST + 1] = { + [M1_PMU_PERFCTR_CORE_ACTIVE_CYCLE] = BIT(0), + [M1_PMU_PERFCTR_INST_ALL] = BIT(1), +}; + +static const unsigned int tahiti_pmu_perf_map[PERF_COUNT_HW_MAX] = { + PERF_MAP_ALL_UNSUPPORTED, + [PERF_COUNT_HW_CPU_CYCLES] = M1_PMU_PERFCTR_CORE_ACTIVE_CYCLE, + [PERF_COUNT_HW_INSTRUCTIONS] = M1_PMU_PERFCTR_INST_ALL, +}; + /* sysfs definitions */ static ssize_t m1_pmu_events_sysfs_show(struct device *dev, struct device_attribute *attr, @@ -493,11 +513,12 @@ static void m1_pmu_write_counter(struct perf_event *event, u64 value) isb(); } -static int m1_pmu_get_event_idx(struct pmu_hw_events *cpuc, - struct perf_event *event) +static int __m1_pmu_get_event_idx(struct pmu_hw_events *cpuc, + struct perf_event *event, + const u16 *event_affinity) { unsigned long evtype = event->hw.config_base & M1_PMU_CFG_EVENT; - unsigned long affinity = m1_pmu_event_affinity[evtype]; + unsigned long affinity = event_affinity[evtype]; int idx; /* @@ -516,6 +537,18 @@ static int m1_pmu_get_event_idx(struct pmu_hw_events *cpuc, return -EAGAIN; } +static int m1_pmu_get_event_idx(struct pmu_hw_events *cpuc, + struct perf_event *event) +{ + return __m1_pmu_get_event_idx(cpuc, event, m1_pmu_event_affinity); +} + +static int tahiti_pmu_get_event_idx(struct pmu_hw_events *cpuc, + struct perf_event *event) +{ + return __m1_pmu_get_event_idx(cpuc, event, tahiti_pmu_event_affinity); +} + static void m1_pmu_clear_event_idx(struct pmu_hw_events *cpuc, struct perf_event *event) { @@ -565,6 +598,18 @@ static int m2_pmu_map_event(struct perf_event *event) return armpmu_map_event(event, &m1_pmu_perf_map, NULL, M1_PMU_CFG_EVENT); } +static int tahiti_pmu_map_event(struct perf_event *event) +{ + int ret; + + event->hw.flags |= ARMPMU_EVT_63BIT; + ret = armpmu_map_event(event, &tahiti_pmu_perf_map, NULL, M1_PMU_CFG_EVENT); + if (ret >= 0 && !tahiti_pmu_event_affinity[ret]) + return -ENOENT; + + return ret; +} + static int m1_pmu_map_pmuv3_event(unsigned int eventsel) { u16 m1_event = HW_OP_UNSUPPORTED; @@ -575,6 +620,18 @@ static int m1_pmu_map_pmuv3_event(unsigned int eventsel) return m1_event == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : m1_event; } +static int tahiti_pmu_map_pmuv3_event(unsigned int eventsel) +{ + switch (eventsel) { + case ARMV8_PMUV3_PERFCTR_CPU_CYCLES: + return M1_PMU_PERFCTR_CORE_ACTIVE_CYCLE; + case ARMV8_PMUV3_PERFCTR_INST_RETIRED: + return M1_PMU_PERFCTR_INST_ALL; + default: + return -EOPNOTSUPP; + } +} + static void m1_pmu_init_pmceid(struct arm_pmu *pmu) { unsigned int event; @@ -679,7 +736,26 @@ static int m2_pmu_blizzard_init(struct arm_pmu *cpu_pmu) return m1_pmu_init(cpu_pmu, ARMPMU_EVT_63BIT); } +static int tahiti_pmu_init(struct arm_pmu *cpu_pmu) +{ + int ret = m1_pmu_init(cpu_pmu, ARMPMU_EVT_63BIT); + + if (ret) + return ret; + + /* Both clusters share only the verified fixed-counter interface. */ + cpu_pmu->name = "apple_t8140_pmu"; + cpu_pmu->get_event_idx = tahiti_pmu_get_event_idx; + cpu_pmu->map_event = tahiti_pmu_map_event; + cpu_pmu->map_pmuv3_event = tahiti_pmu_map_pmuv3_event; + bitmap_zero(cpu_pmu->pmceid_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS); + set_bit(ARMV8_PMUV3_PERFCTR_CPU_CYCLES, cpu_pmu->pmceid_bitmap); + set_bit(ARMV8_PMUV3_PERFCTR_INST_RETIRED, cpu_pmu->pmceid_bitmap); + return 0; +} + static const struct of_device_id m1_pmu_of_device_ids[] = { + { .compatible = "apple,t8140-pmu", .data = tahiti_pmu_init, }, { .compatible = "apple,avalanche-pmu", .data = m2_pmu_avalanche_init, }, { .compatible = "apple,blizzard-pmu", .data = m2_pmu_blizzard_init, }, { .compatible = "apple,icestorm-pmu", .data = m1_pmu_ice_init, }, From a7f505866bddae111da6bebdaa87f31531f7d72f Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sat, 31 Oct 2026 15:46:12 -0400 Subject: [PATCH 3/3] KVM: arm64: bound deferred stage-2 TLB invalidation Track the actual valid leaf span cleared by a stage-2 unmap and flush only that range after the walk. Sparse or empty unmaps no longer invalidate the caller's full IPA range, while table retirement keeps its immediate TTL_UNKNOWN flush. Assisted-by: GPT-6.1 Sol --- arch/arm64/kvm/hyp/pgtable.c | 40 +++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index b74dd5ce1efd3..bcf000b229d28 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -891,11 +891,19 @@ static bool stage2_unmap_defer_tlb_flush(struct kvm_pgtable *pgt) return system_supports_tlb_range() && cpus_have_final_cap(ARM64_HAS_STAGE2_FWB); } +struct stage2_unmap_data { + struct kvm_pgtable *pgt; + + /* IPA span of the leaves whose TLB invalidation was deferred */ + u64 flush_start; + u64 flush_end; +}; + static void stage2_unmap_put_pte(const struct kvm_pgtable_visit_ctx *ctx, struct kvm_s2_mmu *mmu, struct kvm_pgtable_mm_ops *mm_ops) { - struct kvm_pgtable *pgt = ctx->arg; + struct stage2_unmap_data *data = ctx->arg; /* * Clear the existing PTE, and perform break-before-make if it was @@ -908,9 +916,15 @@ static void stage2_unmap_put_pte(const struct kvm_pgtable_visit_ctx *ctx, if (kvm_pte_table(ctx->old, ctx->level)) { kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, ctx->addr, TLBI_TTL_UNKNOWN); - } else if (!stage2_unmap_defer_tlb_flush(pgt)) { + } else if (!stage2_unmap_defer_tlb_flush(data->pgt)) { kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, ctx->addr, ctx->level); + } else { + u64 granule = kvm_granule_size(ctx->level); + u64 start = ALIGN_DOWN(ctx->addr, granule); + + data->flush_start = min(data->flush_start, start); + data->flush_end = max(data->flush_end, start + granule); } } @@ -1155,7 +1169,8 @@ int kvm_pgtable_stage2_annotate(struct kvm_pgtable *pgt, u64 addr, u64 size, static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx, enum kvm_pgtable_walk_flags visit) { - struct kvm_pgtable *pgt = ctx->arg; + struct stage2_unmap_data *data = ctx->arg; + struct kvm_pgtable *pgt = data->pgt; struct kvm_s2_mmu *mmu = pgt->mmu; struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops; kvm_pte_t *childp = NULL; @@ -1198,16 +1213,27 @@ static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx, int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size) { int ret; + struct stage2_unmap_data data = { + .pgt = pgt, + .flush_start = U64_MAX, + }; struct kvm_pgtable_walker walker = { .cb = stage2_unmap_walker, - .arg = pgt, + .arg = &data, .flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST, }; ret = kvm_pgtable_walk(pgt, addr, size, &walker); - if (stage2_unmap_defer_tlb_flush(pgt)) - /* Perform the deferred TLB invalidations */ - kvm_tlb_flush_vmid_range(pgt->mmu, addr, size); + + /* + * Perform the deferred TLB invalidations, for the leaves the walk + * actually cleared: a walk over an unmapped range, such as the + * whole-IPA-space unmap of a sparse nested stage 2 on every MMU + * notifier event, needs none. + */ + if (data.flush_end > data.flush_start) + kvm_tlb_flush_vmid_range(pgt->mmu, data.flush_start, + data.flush_end - data.flush_start); return ret; }