Skip to content

Commit 94cdeeb

Browse files
Like Xusean-jc
authored andcommitted
KVM: x86/cpuid: Add AMD CPUID ExtPerfMonAndDbg leaf 0x80000022
CPUID leaf 0x80000022 i.e. ExtPerfMonAndDbg advertises some new performance monitoring features for AMD processors. Bit 0 of EAX indicates support for Performance Monitoring Version 2 (PerfMonV2) features. If found to be set during PMU initialization, the EBX bits of the same CPUID function can be used to determine the number of available PMCs for different PMU types. Expose the relevant bits via KVM_GET_SUPPORTED_CPUID so that guests can make use of the PerfMonV2 features. Co-developed-by: Sandipan Das <sandipan.das@amd.com> Signed-off-by: Sandipan Das <sandipan.das@amd.com> Signed-off-by: Like Xu <likexu@tencent.com> Link: https://lore.kernel.org/r/20230603011058.1038821-13-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 4a27718 commit 94cdeeb

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

arch/x86/kvm/cpuid.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,10 @@ void kvm_set_cpu_caps(void)
734734
F(NULL_SEL_CLR_BASE) | F(AUTOIBRS) | 0 /* PrefetchCtlMsr */
735735
);
736736

737+
kvm_cpu_cap_init_kvm_defined(CPUID_8000_0022_EAX,
738+
F(PERFMON_V2)
739+
);
740+
737741
/*
738742
* Synthesize "LFENCE is serializing" into the AMD-defined entry in
739743
* KVM's supported CPUID if the feature is reported as supported by the
@@ -1128,7 +1132,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
11281132
entry->edx = 0;
11291133
break;
11301134
case 0x80000000:
1131-
entry->eax = min(entry->eax, 0x80000021);
1135+
entry->eax = min(entry->eax, 0x80000022);
11321136
/*
11331137
* Serializing LFENCE is reported in a multitude of ways, and
11341138
* NullSegClearsBase is not reported in CPUID on Zen2; help
@@ -1233,6 +1237,28 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
12331237
entry->ebx = entry->ecx = entry->edx = 0;
12341238
cpuid_entry_override(entry, CPUID_8000_0021_EAX);
12351239
break;
1240+
/* AMD Extended Performance Monitoring and Debug */
1241+
case 0x80000022: {
1242+
union cpuid_0x80000022_ebx ebx;
1243+
1244+
entry->ecx = entry->edx = 0;
1245+
if (!enable_pmu || !kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2)) {
1246+
entry->eax = entry->ebx;
1247+
break;
1248+
}
1249+
1250+
cpuid_entry_override(entry, CPUID_8000_0022_EAX);
1251+
1252+
if (kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2))
1253+
ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;
1254+
else if (kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE))
1255+
ebx.split.num_core_pmc = AMD64_NUM_COUNTERS_CORE;
1256+
else
1257+
ebx.split.num_core_pmc = AMD64_NUM_COUNTERS;
1258+
1259+
entry->ebx = ebx.full;
1260+
break;
1261+
}
12361262
/*Add support for Centaur's CPUID instruction*/
12371263
case 0xC0000000:
12381264
/*Just support up to 0xC0000004 now*/

arch/x86/kvm/svm/svm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5036,6 +5036,10 @@ static __init void svm_set_cpu_caps(void)
50365036
kvm_pmu_cap.num_counters_gp);
50375037
else
50385038
kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
5039+
5040+
if (kvm_pmu_cap.version != 2 ||
5041+
!kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE))
5042+
kvm_cpu_cap_clear(X86_FEATURE_PERFMON_V2);
50395043
}
50405044

50415045
/* CPUID 0x8000001F (SME/SEV features) */

0 commit comments

Comments
 (0)