Skip to content

Commit 3d0dba5

Browse files
author
Marc Zyngier
committed
KVM: arm64: PMU: Move the ID_AA64DFR0_EL1.PMUver limit to VM creation
As further patches will enable the selection of a PMU revision from userspace, sample the supported PMU revision at VM creation time, rather than building each time the ID_AA64DFR0_EL1 register is accessed. This shouldn't result in any change in behaviour. Reviewed-by: Reiji Watanabe <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 26d2d05 commit 3d0dba5

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

arch/arm64/include/asm/kvm_host.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ struct kvm_arch {
163163

164164
u8 pfr0_csv2;
165165
u8 pfr0_csv3;
166+
struct {
167+
u8 imp:4;
168+
u8 unimp:4;
169+
} dfr0_pmuver;
166170

167171
/* Hypercall features firmware registers' descriptor */
168172
struct kvm_smccc_features smccc_feat;

arch/arm64/kvm/arm.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
164164
set_default_spectre(kvm);
165165
kvm_arm_init_hypercalls(kvm);
166166

167+
/*
168+
* Initialise the default PMUver before there is a chance to
169+
* create an actual PMU.
170+
*/
171+
kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
172+
167173
return ret;
168174
out_free_stage2_pgd:
169175
kvm_free_stage2_pgd(&kvm->arch.mmu);

arch/arm64/kvm/pmu-emul.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,3 +1047,14 @@ int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
10471047

10481048
return -ENXIO;
10491049
}
1050+
1051+
u8 kvm_arm_pmu_get_pmuver_limit(void)
1052+
{
1053+
u64 tmp;
1054+
1055+
tmp = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1);
1056+
tmp = cpuid_feature_cap_perfmon_field(tmp,
1057+
ID_AA64DFR0_EL1_PMUVer_SHIFT,
1058+
ID_AA64DFR0_EL1_PMUVer_V3P4);
1059+
return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), tmp);
1060+
}

arch/arm64/kvm/sys_regs.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,27 @@ static bool access_arch_timer(struct kvm_vcpu *vcpu,
10621062
return true;
10631063
}
10641064

1065+
static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
1066+
{
1067+
if (kvm_vcpu_has_pmu(vcpu))
1068+
return vcpu->kvm->arch.dfr0_pmuver.imp;
1069+
1070+
return vcpu->kvm->arch.dfr0_pmuver.unimp;
1071+
}
1072+
1073+
static u8 pmuver_to_perfmon(u8 pmuver)
1074+
{
1075+
switch (pmuver) {
1076+
case ID_AA64DFR0_EL1_PMUVer_IMP:
1077+
return ID_DFR0_PERFMON_8_0;
1078+
case ID_AA64DFR0_EL1_PMUVer_IMP_DEF:
1079+
return ID_DFR0_PERFMON_IMP_DEF;
1080+
default:
1081+
/* Anything ARMv8.1+ and NI have the same value. For now. */
1082+
return pmuver;
1083+
}
1084+
}
1085+
10651086
/* Read a sanitised cpufeature ID register by sys_reg_desc */
10661087
static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r)
10671088
{
@@ -1111,18 +1132,17 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r
11111132
/* Limit debug to ARMv8.0 */
11121133
val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DebugVer);
11131134
val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_DebugVer), 6);
1114-
/* Limit guests to PMUv3 for ARMv8.4 */
1115-
val = cpuid_feature_cap_perfmon_field(val,
1116-
ID_AA64DFR0_EL1_PMUVer_SHIFT,
1117-
kvm_vcpu_has_pmu(vcpu) ? ID_AA64DFR0_EL1_PMUVer_V3P4 : 0);
1135+
/* Set PMUver to the required version */
1136+
val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
1137+
val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
1138+
vcpu_pmuver(vcpu));
11181139
/* Hide SPE from guests */
11191140
val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMSVer);
11201141
break;
11211142
case SYS_ID_DFR0_EL1:
1122-
/* Limit guests to PMUv3 for ARMv8.4 */
1123-
val = cpuid_feature_cap_perfmon_field(val,
1124-
ID_DFR0_PERFMON_SHIFT,
1125-
kvm_vcpu_has_pmu(vcpu) ? ID_DFR0_PERFMON_8_4 : 0);
1143+
val &= ~ARM64_FEATURE_MASK(ID_DFR0_PERFMON);
1144+
val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_DFR0_PERFMON),
1145+
pmuver_to_perfmon(vcpu_pmuver(vcpu)));
11261146
break;
11271147
}
11281148

include/kvm/arm_pmu.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
8989
vcpu->arch.pmu.events = *kvm_get_pmu_events(); \
9090
} while (0)
9191

92+
u8 kvm_arm_pmu_get_pmuver_limit(void);
93+
9294
#else
9395
struct kvm_pmu {
9496
};
@@ -154,6 +156,10 @@ static inline u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
154156
static inline void kvm_pmu_update_vcpu_events(struct kvm_vcpu *vcpu) {}
155157
static inline void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu) {}
156158
static inline void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu) {}
159+
static inline u8 kvm_arm_pmu_get_pmuver_limit(void)
160+
{
161+
return 0;
162+
}
157163

158164
#endif
159165

0 commit comments

Comments
 (0)