KVM: arm64: Support FEAT_PMUv3 on Apple hardware
Subsystems (3)
v2 cover KVM: arm64: Support FEAT_PMUv3 on Apple hardware
This series adds support for PMUv3 emulation on Apple hardware. See the v1 cover letter for a more detailed explanation.
Tested on an M2 Pro Mac Mini w/ Debian + Windows guests. Also tested using kvm-unit-tests, with a small test fix [*].
[*]: https://lore.kernel.org/kvmarm/[email protected]/
v1: https://lore.kernel.org/kvmarm/[email protected]/
v1 -> v2:
- Rebase to 6.14-rc1
- Fix CONFIG_GUEST_PERF_EVENTS=n compilation error
- Enroll M1 parts for PMUv3 emulation (Janne)
- Collect Janne's Tested-by
Oliver Upton (14):
drivers/perf: apple_m1: Refactor event select/filter configuration drivers/perf: apple_m1: Support host/guest event filtering drivers/perf: apple_m1: Provide helper for mapping PMUv3 events
KVM: arm64: Compute PMCEID from arm_pmu's event bitmaps
KVM: arm64: Always support SW_INCR PMU event
KVM: arm64: Remap PMUv3 events onto hardware
KVM: arm64: Use a cpucap to determine if system supports FEAT_PMUv3
KVM: arm64: Drop kvm_arm_pmu_available static key
KVM: arm64: Use guard() to cleanup usage of arm_pmus_lock
KVM: arm64: Move PMUVer filtering into KVM code
KVM: arm64: Compute synthetic sysreg ESR for Apple PMUv3 traps
KVM: arm64: Advertise PMUv3 if IMPDEF traps are present
KVM: arm64: Provide 1 event counter on IMPDEF hardware
arm64: Enable IMP DEF PMUv3 traps on Apple M*
arch/arm64/include/asm/apple_m1_pmu.h | 1 + arch/arm64/include/asm/cpufeature.h | 28 +----- arch/arm64/kernel/cpu_errata.c | 44 ++++++++ arch/arm64/kernel/cpufeature.c | 19 ++++ arch/arm64/kernel/image-vars.h | 5 - arch/arm64/kvm/arm.c | 4 +- arch/arm64/kvm/hyp/include/hyp/switch.h | 4 +- arch/arm64/kvm/hyp/vhe/switch.c | 22 ++++ arch/arm64/kvm/pmu-emul.c | 127 +++++++++++++++++------- arch/arm64/kvm/pmu.c | 10 +- arch/arm64/tools/cpucaps | 2 + drivers/perf/apple_m1_cpu_pmu.c | 101 +++++++++++++++---- include/kvm/arm_pmu.h | 12 +-- include/linux/perf/arm_pmu.h | 1 + 14 files changed, 276 insertions(+), 104 deletions(-)
base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b
Signature · 1 line
2.39.5
PATCH v2 1/14 drivers/perf: apple_m1: Refactor event select/filter configuration
Supporting guest mode events will necessitate programming two event filters. Prepare by splitting up the programming of the event selector + event filter into separate headers.
Opportunistically replace RMW patterns with sysreg_clear_set_s().
--- arch/arm64/include/asm/apple_m1_pmu.h | 1 + drivers/perf/apple_m1_cpu_pmu.c | 52 ++++++++++++++++----------- 2 files changed, 33 insertions(+), 20 deletions(-)
arch/arm64/include/asm/apple_m1_pmu.h +1 -0
@@ -37,6 +37,7 @@#define PMCR0_PMI_ENABLE_8_9 GENMASK(45, 44)#define SYS_IMP_APL_PMCR1_EL1 sys_reg(3, 1, 15, 1, 0) +#define SYS_IMP_APL_PMCR1_EL12 sys_reg(3, 1, 15, 7, 2)#define PMCR1_COUNT_A64_EL0_0_7 GENMASK(15, 8)#define PMCR1_COUNT_A64_EL1_0_7 GENMASK(23, 16)#define PMCR1_COUNT_A64_EL0_8_9 GENMASK(41, 40)
drivers/perf/apple_m1_cpu_pmu.c +32 -20
90 lines not on this page · raw message
Signature · 1 line
2.39.5
Marc Zyngier
maintainerRe: [PATCH v2 01/14] drivers/perf: apple_m1: Refactor event select/filter configuration
Quoted 4 lines · Oliver Upton
Supporting guest mode events will necessitate programming two event filters. Prepare by splitting up the programming of the event selector + event filter into separate headers.
s/headers/helpers/ ?
Quoted 15 lines
Opportunistically replace RMW patterns with
sysreg_clear_set_s().Tested-by Janne Grunau <[email protected]>Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/include/asm/apple_m1_pmu.h | 1 + drivers/perf/apple_m1_cpu_pmu.c | 52 ++++++++++++++++----------- 2 files changed, 33 insertions(+), 20 deletions(-)arch/arm64/include/asm/apple_m1_pmu.h +0
-0
arch/arm64/include/asm/apple_m1_pmu.h line 40
Quoted 4 lines
arch/arm64/include/asm/apple_m1_pmu.h +1
-0@@ -37,6 +37,7 @@#define PMCR0_PMI_ENABLE_8_9 GENMASK(45, 44)#define SYS_IMP_APL_PMCR1_EL1 sys_reg(3, 1, 15, 1, 0) +#define SYS_IMP_APL_PMCR1_EL12 sys_reg(3, 1, 15, 7, 2)
nit: this should be moved to the following patch.
Otherwise looks good to me.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
PATCH v2 2/14 drivers/perf: apple_m1: Support host/guest event filtering
In reply to Oliver UptonThe PMU appears to have a separate register for filtering 'guest' exception levels (i.e. EL1 and !ELIsInHost(EL0)) which has the same layout as PMCR1_EL1. Conveniently, there exists a VHE register alias (PMCR1_EL12) that can be used to configure it.
Support guest events by programming the EL12 register with the intended guest kernel/userspace filters. Limit support for guest events to VHE (i.e. kernel running at EL2), as it avoids involving KVM to context switch PMU registers. VHE is the only supported mode on M* parts anyway, so this isn't an actual feature limitation.
--- drivers/perf/apple_m1_cpu_pmu.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/perf/apple_m1_cpu_pmu.c +16 -4
@@ -120,6 +120,8 @@ enum m1_pmu_events {*/ M1_PMU_CFG_COUNT_USER =BIT (8 ),M1_PMU_CFG_COUNT_KERNEL =BIT (9 ), +M1_PMU_CFG_COUNT_HOST =BIT (10 ), + M1_PMU_CFG_COUNT_GUEST = BIT(11), };/*
@@ -328,7 +330,7 @@ static void m1_pmu_disable_counter_interrupt(unsigned int index) }static void __m1_pmu_configure_event_filter (unsigned int index,bool user, -bool kernel) +bool kernel,bool host) {u64 clear, set, user_bit, kernel_bit;
@@ -356,7 +358,10 @@ static void __m1_pmu_configure_event_filter(unsigned int index, bool user,else clear |= kernel_bit; -sysreg_clear_set_s (SYS_IMP_APL_PMCR1_EL1 , clear, set); +if (host) +sysreg_clear_set_s (SYS_IMP_APL_PMCR1_EL1 , clear, set); +else if (is_kernel_in_hyp_mode ()) +sysreg_clear_set_s (SYS_IMP_APL_PMCR1_EL12 , clear, set); }static void __m1_pmu_configure_eventsel (unsigned int index,u8 event)
@@ -391,10 +396,13 @@ static void __m1_pmu_configure_eventsel(unsigned int index, u8 event)static void m1_pmu_configure_counter (unsigned int index,unsigned long config_base) {bool kernel = config_base &M1_PMU_CFG_COUNT_KERNEL ; +bool guest = config_base &M1_PMU_CFG_COUNT_GUEST ; +bool host = config_base &M1_PMU_CFG_COUNT_HOST ;bool user = config_base &M1_PMU_CFG_COUNT_USER ;u8 evt = config_base &M1_PMU_CFG_EVENT ; -__m1_pmu_configure_event_filter (index, user, kernel); +__m1_pmu_configure_event_filter (index, user && host, kernel && host, true); +__m1_pmu_configure_event_filter (index, user && guest, kernel && guest, false);__m1_pmu_configure_eventsel (index, evt); }
@@ -570,7 +578,7 @@ static int m1_pmu_set_event_filter(struct hw_perf_event *event, {unsigned long config_base =0 ; -if (!attr->exclude_guest) { +if (!attr->exclude_guest && !is_kernel_in_hyp_mode ()) {pr_debug ("ARM performance counters do not support mode exclusion\n" );return -EOPNOTSUPP ; }
@@ -578,6 +586,10 @@ static int m1_pmu_set_event_filter(struct hw_perf_event *event, config_base |=M1_PMU_CFG_COUNT_KERNEL ;if (!attr->exclude_user) config_base |=M1_PMU_CFG_COUNT_USER ; +if (!attr->exclude_host) + config_base |=M1_PMU_CFG_COUNT_HOST ; +if (!attr->exclude_guest) + config_base |=M1_PMU_CFG_COUNT_GUEST ; event->config_base = config_base;
Signature · 1 line
2.39.5
PATCH v2 3/14 drivers/perf: apple_m1: Provide helper for mapping PMUv3 events
In reply to Oliver UptonApple M* parts carry some IMP DEF traps for guest accesses to PMUv3 registers, even though the underlying hardware doesn't implement PMUv3. This means it is possible to virtualize PMUv3 for KVM guests.
Add a helper for mapping common PMUv3 event IDs onto hardware event IDs, keeping the implementation-specific crud in the PMU driver rather than KVM proper.
--- drivers/perf/apple_m1_cpu_pmu.c | 35 +++++++++++++++++++++++++++++++++ include/linux/perf/arm_pmu.h | 1 + 2 files changed, 36 insertions(+)
drivers/perf/apple_m1_cpu_pmu.c +35 -0
@@ -12,6 +12,7 @@#include <linux/of.h> #include <linux/perf/arm_pmu.h> +#include <linux/perf/arm_pmuv3.h> #include <linux/platform_device.h> #include <asm/apple_m1_pmu.h>
@@ -174,6 +175,17 @@ static const unsigned m1_pmu_perf_map[PERF_COUNT_HW_MAX] = { [PERF_COUNT_HW_BRANCH_MISSES] = M1_PMU_PERFCTR_BRANCH_MISPRED_NONSPEC, }; +#define M1_PMUV3_EVENT_MAP (pmuv3_event, m1_event) \ + [ARMV8_PMUV3_PERFCTR_##pmuv3_event] = M1_PMU_PERFCTR_##m1_event + +static const unsigned int m1_pmu_pmceid_map[ARMV8_PMUV3_MAX_COMMON_EVENTS ] = { + [0 ...ARMV8_PMUV3_MAX_COMMON_EVENTS -1 ] =HW_OP_UNSUPPORTED , +M1_PMUV3_EVENT_MAP (INST_RETIRED ,INST_ALL ), +M1_PMUV3_EVENT_MAP (CPU_CYCLES ,CORE_ACTIVE_CYCLE ), +M1_PMUV3_EVENT_MAP (BR_RETIRED ,INST_BRANCH ), +M1_PMUV3_EVENT_MAP (BR_MIS_PRED_RETIRED ,BRANCH_MISPRED_NONSPEC ), +}; +/* sysfs definitions */ static ssize_t m1_pmu_events_sysfs_show(struct device *dev, struct device_attribute *attr,
@@ -558,6 +570,26 @@ 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 m1_pmu_map_pmuv3_event (unsigned int eventsel) +{ +int m1_event =HW_OP_UNSUPPORTED ; + +if (eventsel <ARMV8_PMUV3_MAX_COMMON_EVENTS ) + m1_event = m1_pmu_pmceid_map[eventsel]; + +return m1_event ==HW_OP_UNSUPPORTED ? -EOPNOTSUPP : m1_event; +} + +static void m1_pmu_init_pmceid (struct arm_pmu *pmu) +{ +unsigned int event; + +for (event =0 ; event <ARMV8_PMUV3_MAX_COMMON_EVENTS ; event++) { +if (m1_pmu_map_pmuv3_event (event) >=0 ) +set_bit (event, pmu->pmceid_bitmap); + } +} +static void m1_pmu_reset (void *info) {int i;
@@ -618,6 +650,9 @@ static int m1_pmu_init(struct arm_pmu *cpu_pmu, u32 flags) cpu_pmu->reset = m1_pmu_reset; cpu_pmu->set_event_filter = m1_pmu_set_event_filter; + cpu_pmu->map_pmuv3_event = m1_pmu_map_pmuv3_event; +m1_pmu_init_pmceid (cpu_pmu); +bitmap_set (cpu_pmu->cntr_mask,0 ,M1_PMU_NR_COUNTERS ); cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS ] = &m1_pmu_events_attr_group; cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS ] = &m1_pmu_format_attr_group;
include/linux/perf/arm_pmu.h +1 -0
@@ -100,6 +100,7 @@ struct arm_pmu {void (*stop)(struct arm_pmu *);void (*reset)(void *);int (*map_event)(struct perf_event *event); +int (*map_pmuv3_event)(unsigned int eventsel);DECLARE_BITMAP (cntr_mask,ARMPMU_MAX_HWEVENTS );bool secure_access;/* 32-bit ARM only */ #define ARMV8_PMUV3_MAX_COMMON_EVENTS 0x40
Signature · 1 line
2.39.5
Marc Zyngier
maintainerRe: [PATCH v2 03/14] drivers/perf: apple_m1: Provide helper for mapping PMUv3 events
Quoted 29 lines · Oliver Upton
Apple M* parts carry some IMP DEF traps for guest accesses to PMUv3 registers, even though the underlying hardware doesn't implement PMUv3. This means it is possible to virtualize PMUv3 for KVM guests.
Add a helper for mapping common PMUv3 event IDs onto hardware event IDs, keeping the implementation-specific crud in the PMU driver rather than KVM proper.
Tested-by Janne Grunau <[email protected]>Signed-off-by Oliver Upton <[email protected]>--- drivers/perf/apple_m1_cpu_pmu.c | 35 +++++++++++++++++++++++++++++++++ include/linux/perf/arm_pmu.h | 1 + 2 files changed, 36 insertions(+)drivers/perf/apple_m1_cpu_pmu.c +1
-0@@ -12,6 +12,7 @@#include <linux/of.h> #include <linux/perf/arm_pmu.h> +#include <linux/perf/arm_pmuv3.h> #include <linux/platform_device.h> #include <asm/apple_m1_pmu.h>
drivers/perf/apple_m1_cpu_pmu.c static const unsigned m1_pmu_perf_map[PERF_COUNT_HW_MAX] = { · line 181
Quoted 7 lines
drivers/perf/apple_m1_cpu_pmu.c +4
-0@@ -174,6 +175,17 @@ static const unsigned m1_pmu_perf_map[PERF_COUNT_HW_MAX] = { [PERF_COUNT_HW_BRANCH_MISSES] = M1_PMU_PERFCTR_BRANCH_MISPRED_NONSPEC, }; +#define M1_PMUV3_EVENT_MAP (pmuv3_event, m1_event) \ + [ARMV8_PMUV3_PERFCTR_##pmuv3_event] = M1_PMU_PERFCTR_##m1_event + +static const unsigned int m1_pmu_pmceid_map[ARMV8_PMUV3_MAX_COMMON_EVENTS ] = {
nit: M1_PMU_PERFCTR* are limited to 8bit quantities, and HW_OP_UNSUPPORTED is a 16bit constant. Probably worth changing the type to u16 and save a whole 128 bytes! ;-)
Quoted 11 lines
drivers/perf/apple_m1_cpu_pmu.c +7
-0+ [0 ... ARMV8_PMUV3_MAX_COMMON_EVENTS - 1] = HW_OP_UNSUPPORTED, +M1_PMUV3_EVENT_MAP (INST_RETIRED ,INST_ALL ), +M1_PMUV3_EVENT_MAP (CPU_CYCLES ,CORE_ACTIVE_CYCLE ), +M1_PMUV3_EVENT_MAP (BR_RETIRED ,INST_BRANCH ), + M1_PMUV3_EVENT_MAP(BR_MIS_PRED_RETIRED, BRANCH_MISPRED_NONSPEC), +}; +/* sysfs definitions */ static ssize_t m1_pmu_events_sysfs_show(struct device *dev, struct device_attribute *attr,
drivers/perf/apple_m1_cpu_pmu.c static int m2_pmu_map_event(struct perf_event *event) · line 575
Quoted 6 lines
drivers/perf/apple_m1_cpu_pmu.c +3
-0@@ -558,6 +570,26 @@ 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 m1_pmu_map_pmuv3_event (unsigned int eventsel) +{ +int m1_event =HW_OP_UNSUPPORTED ;
unsigned?
Quoted 35 lines
drivers/perf/apple_m1_cpu_pmu.c +20
-0+ +if (eventsel <ARMV8_PMUV3_MAX_COMMON_EVENTS ) + m1_event = m1_pmu_pmceid_map[eventsel]; + +return m1_event ==HW_OP_UNSUPPORTED ? -EOPNOTSUPP : m1_event; +} + +static void m1_pmu_init_pmceid (struct arm_pmu *pmu) +{ +unsigned int event; + +for (event =0 ; event <ARMV8_PMUV3_MAX_COMMON_EVENTS ; event++) { +if (m1_pmu_map_pmuv3_event (event) >=0 ) +set_bit (event, pmu->pmceid_bitmap); + } +} +static void m1_pmu_reset (void *info) {int i;@@ -618,6 +650,9 @@ static int m1_pmu_init(struct arm_pmu *cpu_pmu, u32 flags) cpu_pmu->reset = m1_pmu_reset; cpu_pmu->set_event_filter = m1_pmu_set_event_filter; + cpu_pmu->map_pmuv3_event = m1_pmu_map_pmuv3_event; +m1_pmu_init_pmceid (cpu_pmu); +bitmap_set (cpu_pmu->cntr_mask,0 ,M1_PMU_NR_COUNTERS ); cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS ] = &m1_pmu_events_attr_group; cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS ] = &m1_pmu_format_attr_group;include/linux/perf/arm_pmu.h +0
-0
include/linux/perf/arm_pmu.h struct arm_pmu { · line 106
Quoted 7 lines
include/linux/perf/arm_pmu.h +1
-0@@ -100,6 +100,7 @@ struct arm_pmu {void (*stop)(struct arm_pmu *);void (*reset)(void *);int (*map_event)(struct perf_event *event); +int (*map_pmuv3_event)(unsigned int eventsel);DECLARE_BITMAP (cntr_mask,ARMPMU_MAX_HWEVENTS );bool secure_access;/* 32-bit ARM only */ #define ARMV8_PMUV3_MAX_COMMON_EVENTS 0x40
I think it may be more logical to introduce this new callback first, with its usage in KVM, and only then plug it in the PMU driver. Not a big deal though.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
PATCH v2 4/14 KVM: arm64: Compute PMCEID from arm_pmu's event bitmaps
In reply to Oliver UptonThe PMUv3 driver populates a couple of bitmaps with the values of PMCEID{0,1}, from which the guest's PMCEID{0,1} can be derived. This is particularly convenient when virtualizing PMUv3 on IMP DEF hardware, as reading the nonexistent PMCEID registers leads to a rather unpleasant UNDEF.
--- arch/arm64/kvm/pmu-emul.c | 47 ++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 11 deletions(-)
arch/arm64/kvm/pmu-emul.c +36 -11
@@ -842,8 +842,42 @@ static struct arm_pmu *kvm_pmu_probe_armpmu(void)return pmu; } +static u64 __compute_pmceid (struct arm_pmu *pmu,bool pmceid1) +{ +u32 hi[2 ], lo[2 ]; + +bitmap_to_arr32 (lo, pmu->pmceid_bitmap,ARMV8_PMUV3_MAX_COMMON_EVENTS ); +bitmap_to_arr32 (hi, pmu->pmceid_ext_bitmap,ARMV8_PMUV3_MAX_COMMON_EVENTS ); + +return ((u64 )hi[pmceid1] <<32 ) | lo[pmceid1]; +} + +static u64 compute_pmceid0 (struct arm_pmu *pmu) +{ +u64 val =__compute_pmceid (pmu,0 ); + +/* always support CHAIN */ + val |=BIT (ARMV8_PMUV3_PERFCTR_CHAIN ); +return val; +} + +static u64 compute_pmceid1 (struct arm_pmu *pmu) +{ +u64 val =__compute_pmceid (pmu,1 ); + +/* +* Don't advertise STALL_SLOT*, as PMMIR_EL0 is handled +* as RAZ +*/ + val &= ~(BIT_ULL (ARMV8_PMUV3_PERFCTR_STALL_SLOT -32 ) | +BIT_ULL (ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND -32 ) | +BIT_ULL (ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND -32 )); +return val; +} +u64 kvm_pmu_get_pmceid (struct kvm_vcpu *vcpu,bool pmceid1) { +struct arm_pmu *cpu_pmu = vcpu->kvm->arch.arm_pmu;unsigned long *bmap = vcpu->kvm->arch.pmu_filter;u64 val, mask =0 ;int base, i, nr_events;
@@ -852,19 +886,10 @@ u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)return 0 ;if (!pmceid1) { - val =read_sysreg (pmceid0_el0); -/* always support CHAIN */ - val |=BIT (ARMV8_PMUV3_PERFCTR_CHAIN ); + val =compute_pmceid0 (cpu_pmu); base =0 ; }else { - val =read_sysreg (pmceid1_el0); -/* -* Don't advertise STALL_SLOT*, as PMMIR_EL0 is handled -* as RAZ -*/ - val &= ~(BIT_ULL (ARMV8_PMUV3_PERFCTR_STALL_SLOT -32 ) | -BIT_ULL (ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND -32 ) | -BIT_ULL (ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND -32 )); + val =compute_pmceid1 (cpu_pmu); base =32 ; }
Signature · 1 line
2.39.5
PATCH v2 5/14 KVM: arm64: Always support SW_INCR PMU event
In reply to Oliver UptonSupport for SW_INCR is unconditional, as KVM traps accesses to PMSWINC_EL0 and emulates the intended event increment. While it is expected that ~all PMUv3 implementations already advertise this event, non-PMUv3 hardware may not.
--- arch/arm64/kvm/pmu-emul.c | 2 ++ 1 file changed, 2 insertions(+)
arch/arm64/kvm/pmu-emul.c +2 -0
@@ -856,6 +856,8 @@ static u64 compute_pmceid0(struct arm_pmu *pmu) {u64 val =__compute_pmceid (pmu,0 ); +/* always support SW_INCR */ + val |=BIT (ARMV8_PMUV3_PERFCTR_SW_INCR );/* always support CHAIN */ val |=BIT (ARMV8_PMUV3_PERFCTR_CHAIN );return val;
Signature · 1 line
2.39.5
PATCH v2 6/14 KVM: arm64: Remap PMUv3 events onto hardware
In reply to Oliver UptonUse the provided helper to map PMUv3 event IDs onto hardware, if the driver exposes such a helper. This is expected to be quite rare, and only useful for non-PMUv3 hardware.
--- arch/arm64/kvm/pmu-emul.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
arch/arm64/kvm/pmu-emul.c +13 -1
@@ -673,6 +673,18 @@ static bool kvm_pmc_counts_at_el2(struct kvm_pmc *pmc)return kvm_pmc_read_evtreg (pmc) &ARMV8_PMU_INCLUDE_EL2 ; } +static u64 kvm_map_pmu_event (struct kvm *kvm,u64 eventsel) +{ +struct arm_pmu *pmu = kvm->arch.arm_pmu; +int hw_event; + +if (!pmu->map_pmuv3_event) +return eventsel; + + hw_event = pmu->map_pmuv3_event (eventsel); +return (hw_event <0 ) ? eventsel : hw_event; +} +/** * kvm_pmu_create_perf_event - create a perf event for a counter * @pmc: Counter context
@@ -711,13 +723,13 @@ static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc)memset (&attr,0 ,sizeof (struct perf_event_attr )); attr.type = arm_pmu->pmu.type; + attr.config =kvm_map_pmu_event (vcpu->kvm, eventsel); attr.size =sizeof (attr); attr.pinned =1 ; attr.disabled = !kvm_pmu_counter_is_enabled (pmc); attr.exclude_user = !kvm_pmc_counts_at_el0 (pmc); attr.exclude_hv =1 ;/* Don't count EL2 events */ attr.exclude_host =1 ;/* Don't count host events */ - attr.config = eventsel;/* * Filter events at EL1 (i.e. vEL2) when in a hyp context based on the
Signature · 1 line
2.39.5
Marc Zyngier
maintainerRe: [PATCH v2 06/14] KVM: arm64: Remap PMUv3 events onto hardware
Quoted 19 lines · Oliver Upton
Use the provided helper to map PMUv3 event IDs onto hardware, if the driver exposes such a helper. This is expected to be quite rare, and only useful for non-PMUv3 hardware.
Tested-by Janne Grunau <[email protected]>Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/pmu-emul.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)arch/arm64/kvm/pmu-emul.c +0
-0@@ -673,6 +673,18 @@ static bool kvm_pmc_counts_at_el2(struct kvm_pmc *pmc)return kvm_pmc_read_evtreg (pmc) &ARMV8_PMU_INCLUDE_EL2 ; }
arch/arm64/kvm/pmu-emul.c static bool kvm_pmc_counts_at_el2(struct kvm_pmc *pmc) · line 685
Quoted 10 lines
arch/arm64/kvm/pmu-emul.c +10
-0+static u64 kvm_map_pmu_event (struct kvm *kvm,u64 eventsel) +{ +struct arm_pmu *pmu = kvm->arch.arm_pmu; +int hw_event; + +if (!pmu->map_pmuv3_event) +return eventsel; + + hw_event = pmu->map_pmuv3_event (eventsel); +return (hw_event <0 ) ? eventsel : hw_event;
I find this a bit odd. If we can translate events, but failed to do so, we still install the originally requested event, and we have no idea what this maps to on the HW.
I'd rather we just don't install that event at all rather than counting something random.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v2 06/14] KVM: arm64: Remap PMUv3 events onto hardware
Quoted 36 lines · Marc Zyngier
Quoted 19 lines · Oliver Upton
Use the provided helper to map PMUv3 event IDs onto hardware, if the driver exposes such a helper. This is expected to be quite rare, and only useful for non-PMUv3 hardware.
Tested-by Janne Grunau <[email protected]>Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/pmu-emul.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)arch/arm64/kvm/pmu-emul.c +0
-0@@ -673,6 +673,18 @@ static bool kvm_pmc_counts_at_el2(struct kvm_pmc *pmc)return kvm_pmc_read_evtreg (pmc) &ARMV8_PMU_INCLUDE_EL2 ; }arch/arm64/kvm/pmu-emul.c static bool kvm_pmc_counts_at_el2(struct kvm_pmc *pmc) · line 685
Quoted 10 lines
arch/arm64/kvm/pmu-emul.c +10
-0+static u64 kvm_map_pmu_event (struct kvm *kvm,u64 eventsel) +{ +struct arm_pmu *pmu = kvm->arch.arm_pmu; +int hw_event; + +if (!pmu->map_pmuv3_event) +return eventsel; + + hw_event = pmu->map_pmuv3_event (eventsel); +return (hw_event <0 ) ? eventsel : hw_event;I find this a bit odd. If we can translate events, but failed to do so, we still install the originally requested event, and we have no idea what this maps to on the HW.
Quoted 2 lines
I'd rather we just don't install that event at all rather than counting something random.
Heh, this was a leftover party trick that I was using to try "raw" events from inside a VM. Happy to limit things to the PMUv3 event space though.
Thanks,
Oliver
PATCH v2 7/14 KVM: arm64: Use a cpucap to determine if system supports FEAT_PMUv3
In reply to Oliver UptonKVM is about to learn some new tricks to virtualize PMUv3 on IMPDEF hardware. As part of that, we now need to differentiate host support from guest support for PMUv3.
Add a cpucap to determine if an architectural PMUv3 is present to guard host usage of PMUv3 controls.
--- arch/arm64/include/asm/cpufeature.h | 5 +++++ arch/arm64/kernel/cpufeature.c | 19 +++++++++++++++++++ arch/arm64/kvm/hyp/include/hyp/switch.h | 4 ++-- arch/arm64/kvm/pmu.c | 10 +++++----- arch/arm64/tools/cpucaps | 1 + include/kvm/arm_pmu.h | 2 +- 6 files changed, 33 insertions(+), 8 deletions(-)
arch/arm64/include/asm/cpufeature.h +5 -0
@@ -866,6 +866,11 @@ static __always_inline bool system_supports_mpam_hcr(void)return alternative_has_cap_unlikely (ARM64_MPAM_HCR ); } +static inline bool system_supports_pmuv3 (void ) +{ +return cpus_have_final_cap (ARM64_HAS_PMUV3 ); +} +int do_emulate_mrs (struct pt_regs *regs,u32 sys_reg,u32 rt);bool try_emulate_mrs (struct pt_regs *regs,u32 isn);
arch/arm64/kernel/cpufeature.c +19 -0
@@ -1898,6 +1898,19 @@ static bool has_lpa2(const struct arm64_cpu_capabilities *entry, int scope) }#endif +static bool has_pmuv3 (const struct arm64_cpu_capabilities *entry,int scope) +{ +u64 dfr0 =read_sanitised_ftr_reg (SYS_ID_AA64DFR0_EL1 ); +unsigned int pmuver; + + pmuver =cpuid_feature_extract_unsigned_field (dfr0, + ID_AA64DFR0_EL1_PMUVer_SHIFT); +if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) +return false; + +return pmuver >= ID_AA64DFR0_EL1_PMUVer_IMP; +} +#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 #define KPTI_NG_TEMP_VA (-(1UL << PMD_SHIFT))
@@ -2999,6 +3012,12 @@ static const struct arm64_cpu_capabilities arm64_features[] = {ARM64_CPUID_FIELDS (ID_AA64PFR1_EL1 ,GCS ,IMP ) }, #endif + { + .desc = "PMUv3", + .capability = ARM64_HAS_PMUV3, + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .matches = has_pmuv3, + }, {}, };
arch/arm64/kvm/hyp/include/hyp/switch.h +2 -2
@@ -244,7 +244,7 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)* counter, which could make a PMXEVCNTR_EL0 access UNDEF at * EL1 instead of being trapped to EL2. */ -if (kvm_arm_support_pmu_v3 ()) { +if (system_supports_pmuv3 ()) {struct kvm_cpu_context *hctxt;write_sysreg (0 , pmselr_el0);
@@ -281,7 +281,7 @@ static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)write_sysreg (*host_data_ptr (host_debug_state.mdcr_el2), mdcr_el2);write_sysreg (0 , hstr_el2); -if (kvm_arm_support_pmu_v3 ()) { +if (system_supports_pmuv3 ()) {struct kvm_cpu_context *hctxt; hctxt =host_data_ptr (host_ctxt);
arch/arm64/kvm/pmu.c +5 -5
@@ -41,7 +41,7 @@ void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr) {struct kvm_pmu_events *pmu =kvm_get_pmu_events (); -if (!kvm_arm_support_pmu_v3 () || !kvm_pmu_switch_needed (attr)) +if (!system_supports_pmuv3 () || !kvm_pmu_switch_needed (attr))return ;if (!attr->exclude_host)
@@ -57,7 +57,7 @@ void kvm_clr_pmu_events(u64 clr) {struct kvm_pmu_events *pmu =kvm_get_pmu_events (); -if (!kvm_arm_support_pmu_v3 ()) +if (!system_supports_pmuv3 ())return ; pmu->events_host &= ~clr;
@@ -133,7 +133,7 @@ void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu)struct kvm_pmu_events *pmu;u64 events_guest, events_host; -if (!kvm_arm_support_pmu_v3 () || !has_vhe ()) +if (!system_supports_pmuv3 () || !has_vhe ())return ;preempt_disable ();
@@ -154,7 +154,7 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu)struct kvm_pmu_events *pmu;u64 events_guest, events_host; -if (!kvm_arm_support_pmu_v3 () || !has_vhe ()) +if (!system_supports_pmuv3 () || !has_vhe ())return ; pmu =kvm_get_pmu_events ();
@@ -180,7 +180,7 @@ bool kvm_set_pmuserenr(u64 val)struct kvm_cpu_context *hctxt;struct kvm_vcpu *vcpu; -if (!kvm_arm_support_pmu_v3 () || !has_vhe ()) +if (!system_supports_pmuv3 () || !has_vhe ())return false; vcpu =kvm_get_running_vcpu ();
arch/arm64/tools/cpucaps +1 -0
@@ -45,6 +45,7 @@ HAS_LSE_ATOMICS HAS_MOPS HAS_NESTED_VIRT HAS_PAN +HAS_PMUV3 HAS_S1PIE HAS_S1POE HAS_RAS_EXTN
include/kvm/arm_pmu.h +1 -1
@@ -86,7 +86,7 @@ void kvm_vcpu_pmu_resync_el0(void);*/ #define kvm_pmu_update_vcpu_events (vcpu) \ do { \ - if (!has_vhe() && kvm_arm_support_pmu_v3()) \ + if (!has_vhe() && system_supports_pmuv3()) \ vcpu->arch.pmu.events = *kvm_get_pmu_events(); \ } while (0)
Signature · 1 line
2.39.5
Marc Zyngier
maintainerRe: [PATCH v2 07/14] KVM: arm64: Use a cpucap to determine if system supports FEAT_PMUv3
Quoted 44 lines · Oliver Upton
KVM is about to learn some new tricks to virtualize PMUv3 on IMPDEF hardware. As part of that, we now need to differentiate host support from guest support for PMUv3.
Add a cpucap to determine if an architectural PMUv3 is present to guard host usage of PMUv3 controls.
Tested-by Janne Grunau <[email protected]>Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/include/asm/cpufeature.h | 5 +++++ arch/arm64/kernel/cpufeature.c | 19 +++++++++++++++++++ arch/arm64/kvm/hyp/include/hyp/switch.h | 4 ++-- arch/arm64/kvm/pmu.c | 10 +++++----- arch/arm64/tools/cpucaps | 1 + include/kvm/arm_pmu.h | 2 +- 6 files changed, 33 insertions(+), 8 deletions(-)arch/arm64/include/asm/cpufeature.h +5
-0@@ -866,6 +866,11 @@ static __always_inline bool system_supports_mpam_hcr(void)return alternative_has_cap_unlikely (ARM64_MPAM_HCR ); } +static inline bool system_supports_pmuv3 (void ) +{ +return cpus_have_final_cap (ARM64_HAS_PMUV3 ); +} +int do_emulate_mrs (struct pt_regs *regs,u32 sys_reg,u32 rt);bool try_emulate_mrs (struct pt_regs *regs,u32 isn);arch/arm64/kernel/cpufeature.c +1
-0@@ -1898,6 +1898,19 @@ static bool has_lpa2(const struct arm64_cpu_capabilities *entry, int scope) }#endif +static bool has_pmuv3 (const struct arm64_cpu_capabilities *entry,int scope)
arch/arm64/kernel/cpufeature.c static bool has_lpa2(const struct arm64_cpu_capabilities *entry, int scope) · line 1911
Quoted 10 lines
arch/arm64/kernel/cpufeature.c +10
-0+{ +u64 dfr0 =read_sanitised_ftr_reg (SYS_ID_AA64DFR0_EL1 ); +unsigned int pmuver; + + pmuver =cpuid_feature_extract_unsigned_field (dfr0, + ID_AA64DFR0_EL1_PMUVer_SHIFT); +if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) +return false; + +return pmuver >= ID_AA64DFR0_EL1_PMUVer_IMP;
Given that PMUVer is a signed field, how about using cpuid_feature_extract_signed_field() and do a signed comparison instead?
Quoted 6 lines
arch/arm64/kernel/cpufeature.c +2
-0+} +#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 #define KPTI_NG_TEMP_VA (-(1UL << PMD_SHIFT))
arch/arm64/kernel/cpufeature.c static const struct arm64_cpu_capabilities arm64_features[] = { · line 3020
Quoted 9 lines
arch/arm64/kernel/cpufeature.c +6
-0@@ -2999,6 +3012,12 @@ static const struct arm64_cpu_capabilities arm64_features[] = {ARM64_CPUID_FIELDS (ID_AA64PFR1_EL1 ,GCS ,IMP ) }, #endif + { + .desc = "PMUv3", + .capability = ARM64_HAS_PMUV3, + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .matches = has_pmuv3, + },
This cap is probed unconditionally (without any configuration dependency)...
arch/arm64/kvm/hyp/include/hyp/switch.h static inline void __activate_traps_common(struct kvm_vcpu *vcpu) · line 247
Quoted 13 lines
arch/arm64/kernel/cpufeature.c +0
-0{}, };arch/arm64/kvm/hyp/include/hyp/switch.h +1
-1@@ -244,7 +244,7 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)* counter, which could make a PMXEVCNTR_EL0 access UNDEF at * EL1 instead of being trapped to EL2. */ -if (kvm_arm_support_pmu_v3 ()) { +if (system_supports_pmuv3 ()) {
... but kvm_arm_support_pmu_v3() is conditional on CONFIG_HW_PERF_EVENTS. Doesn't this create some sort of new code path that we didn't expect?
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v2 07/14] KVM: arm64: Use a cpucap to determine if system supports FEAT_PMUv3
Quoted 6 lines · Marc Zyngier
Quoted 6 lines
arch/arm64/kernel/cpufeature.c +6
-0+static bool has_pmuv3 (const struct arm64_cpu_capabilities *entry,int scope) +{ +u64 dfr0 =read_sanitised_ftr_reg (SYS_ID_AA64DFR0_EL1 ); +unsigned int pmuver; + + pmuver = cpuid_feature_extract_unsigned_field(dfr0,
Quoted 8 lines
arch/arm64/kernel/cpufeature.c static bool has_lpa2(const struct arm64_cpu_capabilities *entry, int scope) · line 1911
Quoted 5 lines
arch/arm64/kernel/cpufeature.c +5
-0+ ID_AA64DFR0_EL1_PMUVer_SHIFT); +if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) +return false; + +return pmuver >= ID_AA64DFR0_EL1_PMUVer_IMP;Given that PMUVer is a signed field, how about using
cpuid_feature_extract_signed_field()and do a signed comparison instead?
I'm happy to include a comment, but the PMUVer field is not signed. Any value other than 0xF is meant to be treated as an unsigned quantity.
DDI047L.a D24.1.3.2 is where this is coming from.
Quoted 22 lines
Quoted 6 lines
arch/arm64/kernel/cpufeature.c +2
-0+} +#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 #define KPTI_NG_TEMP_VA (-(1UL << PMD_SHIFT))arch/arm64/kernel/cpufeature.c static const struct arm64_cpu_capabilities arm64_features[] = { · line 3020
Quoted 9 lines
arch/arm64/kernel/cpufeature.c +6
-0@@ -2999,6 +3012,12 @@ static const struct arm64_cpu_capabilities arm64_features[] = {ARM64_CPUID_FIELDS (ID_AA64PFR1_EL1 ,GCS ,IMP ) }, #endif + { + .desc = "PMUv3", + .capability = ARM64_HAS_PMUV3, + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .matches = has_pmuv3, + },This cap is probed unconditionally (without any configuration dependency)...
Quoted 3 lines
(quoted diff) +0
-0{}, };
Quoted 14 lines
arch/arm64/kvm/hyp/include/hyp/switch.h static inline void __activate_traps_common(struct kvm_vcpu *vcpu) · line 247
Quoted 10 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +1
-1@@ -244,7 +244,7 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)* counter, which could make a PMXEVCNTR_EL0 access UNDEF at * EL1 instead of being trapped to EL2. */ -if (kvm_arm_support_pmu_v3 ()) { +if (system_supports_pmuv3 ()) {... but
kvm_arm_support_pmu_v3()is conditional onCONFIG_HW_PERF_EVENTS. Doesn't this create some sort of new code path that we didn't expect?
Yep. It ought to be benign, but pointless. I'll condition this correctly next time around.
Thanks,
Oliver
Marc Zyngier
maintainerRe: [PATCH v2 07/14] KVM: arm64: Use a cpucap to determine if system supports FEAT_PMUv3
Quoted 17 lines · Oliver Upton
Quoted 6 lines · Marc Zyngier
Quoted 6 lines
arch/arm64/kernel/cpufeature.c +6
-0+static bool has_pmuv3 (const struct arm64_cpu_capabilities *entry,int scope) +{ +u64 dfr0 =read_sanitised_ftr_reg (SYS_ID_AA64DFR0_EL1 ); +unsigned int pmuver; + + pmuver = cpuid_feature_extract_unsigned_field(dfr0,Quoted 8 lines
arch/arm64/kernel/cpufeature.c static bool has_lpa2(const struct arm64_cpu_capabilities *entry, int scope) · line 1911
Quoted 5 lines
arch/arm64/kernel/cpufeature.c +5
-0+ ID_AA64DFR0_EL1_PMUVer_SHIFT); +if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) +return false; + +return pmuver >= ID_AA64DFR0_EL1_PMUVer_IMP;Given that PMUVer is a signed field, how about using
cpuid_feature_extract_signed_field()and do a signed comparison instead?
Quoted 4 lines
I'm happy to include a comment, but the PMUVer field is not signed. Any value other than
0xFis meant to be treated as an unsigned quantity.DDI047L.a D24.1.3.2 is where this is coming from.
Duh, you're of course correct. Ignore me.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
PATCH v2 8/14 KVM: arm64: Drop kvm_arm_pmu_available static key
In reply to Oliver UptonWith the PMUv3 cpucap, kvm_arm_pmu_available is no longer used in the hot path of guest entry/exit. On top of that, guest support for PMUv3 may not correlate with host support for the feature, e.g. on IMPDEF hardware.
Throw out the static key and just inspect the list of PMUs to determine if PMUv3 is supported for KVM guests.
--- arch/arm64/kernel/image-vars.h | 5 ----- arch/arm64/kvm/arm.c | 4 ++-- arch/arm64/kvm/pmu-emul.c | 11 ++++++----- include/kvm/arm_pmu.h | 10 ++-------- 4 files changed, 10 insertions(+), 20 deletions(-)
arch/arm64/kernel/image-vars.h +0 -5
@@ -112,11 +112,6 @@ KVM_NVHE_ALIAS(broken_cntvoff_key);KVM_NVHE_ALIAS (__start___kvm_ex_table);KVM_NVHE_ALIAS (__stop___kvm_ex_table); -/* PMU available static key */ -#ifdef CONFIG_HW_PERF_EVENTS -KVM_NVHE_ALIAS (kvm_arm_pmu_available); -#endif -/* Position-independent library routines */ KVM_NVHE_ALIAS_HYP (clear_page, __pi_clear_page);KVM_NVHE_ALIAS_HYP (copy_page, __pi_copy_page);
arch/arm64/kvm/arm.c +2 -2
@@ -366,7 +366,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =get_num_wrps ();break ;case KVM_CAP_ARM_PMU_V3 : - r =kvm_arm_support_pmu_v3 (); + r =kvm_supports_guest_pmuv3 ();break ;case KVM_CAP_ARM_INJECT_SERROR_ESR : r =cpus_have_final_cap (ARM64_HAS_RAS_EXTN );
@@ -1388,7 +1388,7 @@ static unsigned long system_supported_vcpu_features(void)if (!cpus_have_final_cap (ARM64_HAS_32BIT_EL1 ))clear_bit (KVM_ARM_VCPU_EL1_32BIT , &features); -if (!kvm_arm_support_pmu_v3 ()) +if (!kvm_supports_guest_pmuv3 ())clear_bit (KVM_ARM_VCPU_PMU_V3 , &features);if (!system_supports_sve ())
arch/arm64/kvm/pmu-emul.c +6 -5
@@ -17,8 +17,6 @@#define PERF_ATTR_CFG1_COUNTER_64BIT BIT(0) -DEFINE_STATIC_KEY_FALSE (kvm_arm_pmu_available); -static LIST_HEAD (arm_pmus);static DEFINE_MUTEX (arm_pmus_lock);
@@ -26,6 +24,12 @@ static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc);static void kvm_pmu_release_perf_event (struct kvm_pmc *pmc);static bool kvm_pmu_counter_is_enabled (struct kvm_pmc *pmc); +bool kvm_supports_guest_pmuv3 (void ) +{ +guard (mutex)(&arm_pmus_lock); +return !list_empty (&arm_pmus); +} +static struct kvm_vcpu *kvm_pmc_to_vcpu (const struct kvm_pmc *pmc) { return container_of(pmc, struct kvm_vcpu, arch.pmu.pmc[pmc->idx]);
@@ -807,9 +811,6 @@ void kvm_host_pmu_init(struct arm_pmu *pmu) entry->arm_pmu = pmu;list_add_tail (&entry->entry, &arm_pmus); -if (list_is_singular (&arm_pmus)) -static_branch_enable (&kvm_arm_pmu_available); - out_unlock:mutex_unlock (&arm_pmus_lock); }
include/kvm/arm_pmu.h +2 -8
@@ -37,13 +37,7 @@ struct arm_pmu_entry {struct arm_pmu *arm_pmu; }; -DECLARE_STATIC_KEY_FALSE (kvm_arm_pmu_available); - -static __always_inline bool kvm_arm_support_pmu_v3(void) -{ -return static_branch_likely (&kvm_arm_pmu_available); -} - +bool kvm_supports_guest_pmuv3 (void );#define kvm_arm_pmu_irq_initialized (v) ((v)->arch.pmu.irq_num >= VGIC_NR_SGIS)u64 kvm_pmu_get_counter_value (struct kvm_vcpu *vcpu,u64 select_idx);void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val);
@@ -102,7 +96,7 @@ void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu);struct kvm_pmu { }; -static inline bool kvm_arm_support_pmu_v3 (void ) +static inline bool kvm_supports_guest_pmuv3 (void ) {return false; }
Signature · 1 line
2.39.5
PATCH v2 9/14 KVM: arm64: Use guard() to cleanup usage of arm_pmus_lock
In reply to Oliver UptonGet rid of some goto label patterns by using guard() to drop the arm_pmus_lock when returning from a function.
--- arch/arm64/kvm/pmu-emul.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-)
arch/arm64/kvm/pmu-emul.c +8 -15
@@ -802,26 +802,23 @@ void kvm_host_pmu_init(struct arm_pmu *pmu)if (!pmuv3_implemented (kvm_arm_pmu_get_pmuver_limit ()))return ; -mutex_lock (&arm_pmus_lock); +guard (mutex)(&arm_pmus_lock); entry =kmalloc (sizeof (*entry),GFP_KERNEL );if (!entry) - goto out_unlock; +return ; entry->arm_pmu = pmu;list_add_tail (&entry->entry, &arm_pmus); - -out_unlock: -mutex_unlock (&arm_pmus_lock); }static struct arm_pmu *kvm_pmu_probe_armpmu (void ) { -struct arm_pmu *tmp, *pmu =NULL ;struct arm_pmu_entry *entry; +struct arm_pmu *pmu;int cpu; -mutex_lock (&arm_pmus_lock); +guard (mutex)(&arm_pmus_lock);/* * It is safe to use a stale cpu to iterate the list of PMUs so long as
@@ -842,17 +839,13 @@ static struct arm_pmu *kvm_pmu_probe_armpmu(void)*/ cpu =raw_smp_processor_id ();list_for_each_entry (entry, &arm_pmus, entry) { - tmp = entry->arm_pmu; + pmu = entry->arm_pmu; -if (cpumask_test_cpu (cpu, &tmp->supported_cpus)) { - pmu = tmp; -break ; - } +if (cpumask_test_cpu (cpu, &pmu->supported_cpus)) +return pmu; } -mutex_unlock (&arm_pmus_lock); - -return pmu; +return NULL ; }static u64 __compute_pmceid (struct arm_pmu *pmu,bool pmceid1)
Signature · 1 line
2.39.5
PATCH v2 10/14 KVM: arm64: Move PMUVer filtering into KVM code
In reply to Oliver UptonThe supported guest PMU version on a particular platform is ultimately a KVM decision. Move PMUVer filtering into KVM code.
--- arch/arm64/include/asm/cpufeature.h | 23 ----------------------- arch/arm64/kvm/pmu-emul.c | 15 +++++++++------ 2 files changed, 9 insertions(+), 29 deletions(-)
arch/arm64/include/asm/cpufeature.h +0 -23
@@ -525,29 +525,6 @@ cpuid_feature_extract_unsigned_field(u64 features, int field)return cpuid_feature_extract_unsigned_field_width (features, field,4 ); } -/* -* Fields that identify the version of the Performance Monitors Extension do -* not follow the standard ID scheme. See ARM DDI 0487E.a page D13-2825, -* "Alternative ID scheme used for the Performance Monitors Extension version". -*/ -static inline u64 __attribute_const__ -cpuid_feature_cap_perfmon_field (u64 features,int field,u64 cap) -{ -u64 val =cpuid_feature_extract_unsigned_field (features, field); -u64 mask =GENMASK_ULL (field +3 , field); - -/* Treat IMPLEMENTATION DEFINED functionality as unimplemented */ -if (val == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) - val =0 ; - -if (val > cap) { - features &= ~mask; - features |= (cap << field) & mask; - } - -return features; -} -static inline u64 arm64_ftr_mask (const struct arm64_ftr_bits *ftrp) {return (u64 )GENMASK (ftrp->shift + ftrp->width -1 , ftrp->shift);
arch/arm64/kvm/pmu-emul.c +9 -6
@@ -1238,13 +1238,16 @@ int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)u8 kvm_arm_pmu_get_pmuver_limit (void ) { -u64 tmp; +unsigned int pmuver; - tmp =read_sanitised_ftr_reg (SYS_ID_AA64DFR0_EL1 ); - tmp =cpuid_feature_cap_perfmon_field (tmp, - ID_AA64DFR0_EL1_PMUVer_SHIFT, - ID_AA64DFR0_EL1_PMUVer_V3P5); -return FIELD_GET (ARM64_FEATURE_MASK (ID_AA64DFR0_EL1_PMUVer), tmp); + pmuver =SYS_FIELD_GET (ID_AA64DFR0_EL1 , PMUVer, +read_sanitised_ftr_reg (SYS_ID_AA64DFR0_EL1 )); + +/* Treat IMPLEMENTATION DEFINED functionality as unimplemented */ +if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) +return 0 ; + +return min (pmuver, ID_AA64DFR0_EL1_PMUVer_V3P5); }/**
Signature · 1 line
2.39.5
Marc Zyngier
maintainerRe: [PATCH v2 10/14] KVM: arm64: Move PMUVer filtering into KVM code
Quoted 18 lines · Oliver Upton
The supported guest PMU version on a particular platform is ultimately a KVM decision. Move PMUVer filtering into KVM code.
Tested-by Janne Grunau <[email protected]>Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/include/asm/cpufeature.h | 23 ----------------------- arch/arm64/kvm/pmu-emul.c | 15 +++++++++------ 2 files changed, 9 insertions(+), 29 deletions(-)arch/arm64/include/asm/cpufeature.h +0
-0@@ -525,29 +525,6 @@ cpuid_feature_extract_unsigned_field(u64 features, int field)return cpuid_feature_extract_unsigned_field_width (features, field,4 ); }
arch/arm64/include/asm/cpufeature.h cpuid_feature_extract_unsigned_field(u64 features, int field) · line 536
Quoted 10 lines
arch/arm64/include/asm/cpufeature.h +0
-9-/* -* Fields that identify the version of the Performance Monitors Extension do -* not follow the standard ID scheme. See ARM DDI 0487E.a page D13-2825, -* "Alternative ID scheme used for the Performance Monitors Extension version". -*/ -static inline u64 __attribute_const__ -cpuid_feature_cap_perfmon_field (u64 features,int field,u64 cap) -{ -u64 val =cpuid_feature_extract_unsigned_field (features, field);
I guess this is where this idiom is coming from. I think it'd be worth revisiting it here as well as in the last patch.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
PATCH v2 11/14 KVM: arm64: Compute synthetic sysreg ESR for Apple PMUv3 traps
In reply to Oliver UptonApple M* CPUs provide an IMPDEF trap for PMUv3 sysregs, where ESR_EL2.EC is a reserved value (0x3F) and a sysreg-like ISS is reported in AFSR1_EL2.
Compute a synthetic ESR for these PMUv3 traps, giving the illusion of something architectural to the rest of KVM.
--- arch/arm64/kvm/hyp/vhe/switch.c | 22 ++++++++++++++++++++++ arch/arm64/tools/cpucaps | 1 + 2 files changed, 23 insertions(+)
arch/arm64/kvm/hyp/vhe/switch.c +22 -0
@@ -525,6 +525,25 @@ static bool kvm_hyp_handle_sysreg_vhe(struct kvm_vcpu *vcpu, u64 *exit_code)return kvm_hyp_handle_sysreg (vcpu, exit_code); } +static bool kvm_hyp_handle_impdef (struct kvm_vcpu *vcpu,u64 *exit_code) +{ +u64 iss; + +if (!cpus_have_final_cap (ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS )) +return false; + +/* +* Compute a synthetic ESR for a sysreg trap. Conveniently, AFSR1_EL2 +* is populated with a correct ISS for a sysreg trap. These fruity +* parts are 64bit only, so unconditionally set IL. +*/ + iss =ESR_ELx_ISS (read_sysreg_s (SYS_AFSR1_EL2 )); + vcpu->arch.fault.esr_el2 =FIELD_PREP (ESR_ELx_EC_MASK, ESR_ELx_EC_SYS64) | +FIELD_PREP (ESR_ELx_ISS_MASK, iss) | + ESR_ELx_IL; +return false; +} +static const exit_handler_fn hyp_exit_handlers[] = { [0 ... ESR_ELx_EC_MAX] =NULL , [ESR_ELx_EC_CP15_32] = kvm_hyp_handle_cp15_32,
@@ -536,6 +555,9 @@ static const exit_handler_fn hyp_exit_handlers[] = { [ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low, [ESR_ELx_EC_ERET] = kvm_hyp_handle_eret, [ESR_ELx_EC_MOPS] = kvm_hyp_handle_mops, + +/* Apple shenanigans */ + [0x3F] = kvm_hyp_handle_impdef, };static const exit_handler_fn *kvm_get_exit_handler_array (struct kvm_vcpu *vcpu)
arch/arm64/tools/cpucaps +1 -0
@@ -105,6 +105,7 @@ WORKAROUND_CAVIUM_TX2_219_TVM WORKAROUND_CLEAN_CACHE WORKAROUND_DEVICE_LOAD_ACQUIRE WORKAROUND_NVIDIA_CARMEL_CNP +WORKAROUND_PMUV3_IMPDEF_TRAPS WORKAROUND_QCOM_FALKOR_E1003 WORKAROUND_QCOM_ORYON_CNTVOFF WORKAROUND_REPEAT_TLBI
Signature · 1 line
2.39.5
PATCH v2 12/14 KVM: arm64: Advertise PMUv3 if IMPDEF traps are present
In reply to Oliver UptonAdvertise a baseline PMUv3 implementation when running on hardware with IMPDEF traps of the PMUv3 sysregs.
--- arch/arm64/kvm/pmu-emul.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
arch/arm64/kvm/pmu-emul.c +11 -1
@@ -1243,7 +1243,17 @@ u8 kvm_arm_pmu_get_pmuver_limit(void) pmuver =SYS_FIELD_GET (ID_AA64DFR0_EL1 , PMUVer,read_sanitised_ftr_reg (SYS_ID_AA64DFR0_EL1 )); -/* Treat IMPLEMENTATION DEFINED functionality as unimplemented */ +/* +* Spoof a barebones PMUv3 implementation if the system supports IMPDEF +* traps of the PMUv3 sysregs +*/ +if (cpus_have_final_cap (ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS )) +return ID_AA64DFR0_EL1_PMUVer_IMP; + +/* +* Otherwise, treat IMPLEMENTATION DEFINED functionality as +* unimplemented +*/ if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF)return 0 ;
Signature · 1 line
2.39.5
PATCH v2 13/14 KVM: arm64: Provide 1 event counter on IMPDEF hardware
In reply to Oliver UptonPMUv3 requires that all programmable event counters are capable of counting any event. The Apple M* PMU is quite a bit different, and events have affinities for particular PMCs.
Expose 1 event counter on IMPDEF hardware, allowing the guest to do something useful with its PMU while also upholding the requirements of the architecture.
--- arch/arm64/kvm/pmu-emul.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/arm64/kvm/pmu-emul.c +7 -0
@@ -1027,6 +1027,13 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm) {struct arm_pmu *arm_pmu = kvm->arch.arm_pmu; +/* +* PMUv3 requires that all event counters are capable of counting any +* event, though the same may not be true of non-PMUv3 hardware. +*/ +if (cpus_have_final_cap (ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS )) +return 1 ; +/* * The arm_pmu->cntr_mask considers the fixed counter(s) as well. * Ignore those and return only the general-purpose counters.
Signature · 1 line
2.39.5
PATCH v2 14/14 arm64: Enable IMP DEF PMUv3 traps on Apple M*
In reply to Oliver UptonApple M1 and M2 CPUs support IMPDEF traps of the PMUv3 sysregs, allowing a hypervisor to virtualize an architectural PMU for a VM. Flip the appropriate bit in HACR_EL2 on supporting hardware.
--- arch/arm64/kernel/cpu_errata.c | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
arch/arm64/kernel/cpu_errata.c +44 -0
@@ -194,6 +194,43 @@ has_neoverse_n1_erratum_1542419(const struct arm64_cpu_capabilities *entry,return is_midr_in_range (midr, &range) && has_dic; } +static const struct midr_range impdef_pmuv3_cpus[] = { +MIDR_ALL_VERSIONS (MIDR_APPLE_M1_ICESTORM ), +MIDR_ALL_VERSIONS (MIDR_APPLE_M1_FIRESTORM ), +MIDR_ALL_VERSIONS (MIDR_APPLE_M1_ICESTORM_PRO ), +MIDR_ALL_VERSIONS (MIDR_APPLE_M1_FIRESTORM_PRO ), +MIDR_ALL_VERSIONS (MIDR_APPLE_M1_ICESTORM_MAX ), +MIDR_ALL_VERSIONS (MIDR_APPLE_M1_FIRESTORM_MAX ), +MIDR_ALL_VERSIONS (MIDR_APPLE_M2_BLIZZARD ), +MIDR_ALL_VERSIONS (MIDR_APPLE_M2_AVALANCHE ), +MIDR_ALL_VERSIONS (MIDR_APPLE_M2_BLIZZARD_PRO ), +MIDR_ALL_VERSIONS (MIDR_APPLE_M2_AVALANCHE_PRO ), +MIDR_ALL_VERSIONS (MIDR_APPLE_M2_BLIZZARD_MAX ), +MIDR_ALL_VERSIONS (MIDR_APPLE_M2_AVALANCHE_MAX ), + {}, +}; + +static bool has_impdef_pmuv3 (const struct arm64_cpu_capabilities *entry,int scope) +{ +u64 dfr0 =read_sanitised_ftr_reg (SYS_ID_AA64DFR0_EL1 ); +unsigned int pmuver; + +if (!is_kernel_in_hyp_mode ()) +return false; + + pmuver =cpuid_feature_extract_unsigned_field (dfr0, + ID_AA64DFR0_EL1_PMUVer_SHIFT); +if (pmuver != ID_AA64DFR0_EL1_PMUVer_IMP_DEF) +return false; + +return is_midr_in_range_list (read_cpuid_id (), impdef_pmuv3_cpus); +} + +static void cpu_enable_impdef_pmuv3_traps (const struct arm64_cpu_capabilities *__unused) +{ +sysreg_clear_set_s (SYS_HACR_EL2 ,0 ,BIT (56 )); +} +#ifdef CONFIG_ARM64_WORKAROUND_REPEAT_TLBI static const struct arm64_cpu_capabilities arm64_repeat_tlbi_list[] = { #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1009
@@ -794,6 +831,13 @@ const struct arm64_cpu_capabilities arm64_errata[] = { {} })), }, + { + .desc = "Apple IMPDEF PMUv3 Traps", + .capability = ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS, + .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, + .matches = has_impdef_pmuv3, + .cpu_enable = cpu_enable_impdef_pmuv3_traps, + }, { } };
Signature · 1 line
2.39.5