KVM: x86/cpuid: Add AMD CPUID ExtPerfMonAndDbg leaf 0x80000022
authorLike Xu <likexu@tencent.com>
Sat, 3 Jun 2023 01:10:58 +0000 (18:10 -0700)
committerSean Christopherson <seanjc@google.com>
Wed, 7 Jun 2023 00:31:44 +0000 (17:31 -0700)
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>
arch/x86/kvm/cpuid.c
arch/x86/kvm/svm/svm.c

index 61bc71882f079e7f06ca1301c901188b41e78059..0e5584f4acd7045d3a4d2617f2a864d8989c985d 100644 (file)
@@ -734,6 +734,10 @@ void kvm_set_cpu_caps(void)
                F(NULL_SEL_CLR_BASE) | F(AUTOIBRS) | 0 /* PrefetchCtlMsr */
        );
 
+       kvm_cpu_cap_init_kvm_defined(CPUID_8000_0022_EAX,
+               F(PERFMON_V2)
+       );
+
        /*
         * Synthesize "LFENCE is serializing" into the AMD-defined entry in
         * 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)
                entry->edx = 0;
                break;
        case 0x80000000:
-               entry->eax = min(entry->eax, 0x80000021);
+               entry->eax = min(entry->eax, 0x80000022);
                /*
                 * Serializing LFENCE is reported in a multitude of ways, and
                 * 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)
                entry->ebx = entry->ecx = entry->edx = 0;
                cpuid_entry_override(entry, CPUID_8000_0021_EAX);
                break;
+       /* AMD Extended Performance Monitoring and Debug */
+       case 0x80000022: {
+               union cpuid_0x80000022_ebx ebx;
+
+               entry->ecx = entry->edx = 0;
+               if (!enable_pmu || !kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2)) {
+                       entry->eax = entry->ebx;
+                       break;
+               }
+
+               cpuid_entry_override(entry, CPUID_8000_0022_EAX);
+
+               if (kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2))
+                       ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;
+               else if (kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE))
+                       ebx.split.num_core_pmc = AMD64_NUM_COUNTERS_CORE;
+               else
+                       ebx.split.num_core_pmc = AMD64_NUM_COUNTERS;
+
+               entry->ebx = ebx.full;
+               break;
+       }
        /*Add support for Centaur's CPUID instruction*/
        case 0xC0000000:
                /*Just support up to 0xC0000004 now*/
index d9669e3cc00a7cf14dc0529828b368ca89110336..ff48cdea1fbfff884ed75dad2a841df47888f4d5 100644 (file)
@@ -5036,6 +5036,10 @@ static __init void svm_set_cpu_caps(void)
                                                          kvm_pmu_cap.num_counters_gp);
                else
                        kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
+
+               if (kvm_pmu_cap.version != 2 ||
+                   !kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE))
+                       kvm_cpu_cap_clear(X86_FEATURE_PERFMON_V2);
        }
 
        /* CPUID 0x8000001F (SME/SEV features) */