146a010ddSJoerg Roedel // SPDX-License-Identifier: GPL-2.0-only 246a010ddSJoerg Roedel /* 346a010ddSJoerg Roedel * KVM PMU support for AMD 446a010ddSJoerg Roedel * 546a010ddSJoerg Roedel * Copyright 2015, Red Hat, Inc. and/or its affiliates. 646a010ddSJoerg Roedel * 746a010ddSJoerg Roedel * Author: 846a010ddSJoerg Roedel * Wei Huang <wei@redhat.com> 946a010ddSJoerg Roedel * 1046a010ddSJoerg Roedel * Implementation is based on pmu_intel.c file 1146a010ddSJoerg Roedel */ 1246a010ddSJoerg Roedel #include <linux/types.h> 1346a010ddSJoerg Roedel #include <linux/kvm_host.h> 1446a010ddSJoerg Roedel #include <linux/perf_event.h> 1546a010ddSJoerg Roedel #include "x86.h" 1646a010ddSJoerg Roedel #include "cpuid.h" 1746a010ddSJoerg Roedel #include "lapic.h" 1846a010ddSJoerg Roedel #include "pmu.h" 19b1d66dadSLike Xu #include "svm.h" 2046a010ddSJoerg Roedel 2146a010ddSJoerg Roedel enum pmu_type { 2246a010ddSJoerg Roedel PMU_TYPE_COUNTER = 0, 2346a010ddSJoerg Roedel PMU_TYPE_EVNTSEL, 2446a010ddSJoerg Roedel }; 2546a010ddSJoerg Roedel 2646a010ddSJoerg Roedel enum index { 2746a010ddSJoerg Roedel INDEX_ZERO = 0, 2846a010ddSJoerg Roedel INDEX_ONE, 2946a010ddSJoerg Roedel INDEX_TWO, 3046a010ddSJoerg Roedel INDEX_THREE, 3146a010ddSJoerg Roedel INDEX_FOUR, 3246a010ddSJoerg Roedel INDEX_FIVE, 3346a010ddSJoerg Roedel INDEX_ERROR, 3446a010ddSJoerg Roedel }; 3546a010ddSJoerg Roedel 3646a010ddSJoerg Roedel static enum index msr_to_index(u32 msr) 3746a010ddSJoerg Roedel { 3846a010ddSJoerg Roedel switch (msr) { 3946a010ddSJoerg Roedel case MSR_F15H_PERF_CTL0: 4046a010ddSJoerg Roedel case MSR_F15H_PERF_CTR0: 4146a010ddSJoerg Roedel case MSR_K7_EVNTSEL0: 4246a010ddSJoerg Roedel case MSR_K7_PERFCTR0: 4346a010ddSJoerg Roedel return INDEX_ZERO; 4446a010ddSJoerg Roedel case MSR_F15H_PERF_CTL1: 4546a010ddSJoerg Roedel case MSR_F15H_PERF_CTR1: 4646a010ddSJoerg Roedel case MSR_K7_EVNTSEL1: 4746a010ddSJoerg Roedel case MSR_K7_PERFCTR1: 4846a010ddSJoerg Roedel return INDEX_ONE; 4946a010ddSJoerg Roedel case MSR_F15H_PERF_CTL2: 5046a010ddSJoerg Roedel case MSR_F15H_PERF_CTR2: 5146a010ddSJoerg Roedel case MSR_K7_EVNTSEL2: 5246a010ddSJoerg Roedel case MSR_K7_PERFCTR2: 5346a010ddSJoerg Roedel return INDEX_TWO; 5446a010ddSJoerg Roedel case MSR_F15H_PERF_CTL3: 5546a010ddSJoerg Roedel case MSR_F15H_PERF_CTR3: 5646a010ddSJoerg Roedel case MSR_K7_EVNTSEL3: 5746a010ddSJoerg Roedel case MSR_K7_PERFCTR3: 5846a010ddSJoerg Roedel return INDEX_THREE; 5946a010ddSJoerg Roedel case MSR_F15H_PERF_CTL4: 6046a010ddSJoerg Roedel case MSR_F15H_PERF_CTR4: 6146a010ddSJoerg Roedel return INDEX_FOUR; 6246a010ddSJoerg Roedel case MSR_F15H_PERF_CTL5: 6346a010ddSJoerg Roedel case MSR_F15H_PERF_CTR5: 6446a010ddSJoerg Roedel return INDEX_FIVE; 6546a010ddSJoerg Roedel default: 6646a010ddSJoerg Roedel return INDEX_ERROR; 6746a010ddSJoerg Roedel } 6846a010ddSJoerg Roedel } 6946a010ddSJoerg Roedel 7046a010ddSJoerg Roedel static inline struct kvm_pmc *get_gp_pmc_amd(struct kvm_pmu *pmu, u32 msr, 7146a010ddSJoerg Roedel enum pmu_type type) 7246a010ddSJoerg Roedel { 731973caddSVitaly Kuznetsov struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu); 741973caddSVitaly Kuznetsov 75ba7bb663SDavid Dunn if (!vcpu->kvm->arch.enable_pmu) 76b1d66dadSLike Xu return NULL; 77b1d66dadSLike Xu 7846a010ddSJoerg Roedel switch (msr) { 7946a010ddSJoerg Roedel case MSR_F15H_PERF_CTL0: 8046a010ddSJoerg Roedel case MSR_F15H_PERF_CTL1: 8146a010ddSJoerg Roedel case MSR_F15H_PERF_CTL2: 8246a010ddSJoerg Roedel case MSR_F15H_PERF_CTL3: 8346a010ddSJoerg Roedel case MSR_F15H_PERF_CTL4: 8446a010ddSJoerg Roedel case MSR_F15H_PERF_CTL5: 851973caddSVitaly Kuznetsov if (!guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) 861973caddSVitaly Kuznetsov return NULL; 871973caddSVitaly Kuznetsov fallthrough; 8846a010ddSJoerg Roedel case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: 8946a010ddSJoerg Roedel if (type != PMU_TYPE_EVNTSEL) 9046a010ddSJoerg Roedel return NULL; 9146a010ddSJoerg Roedel break; 9246a010ddSJoerg Roedel case MSR_F15H_PERF_CTR0: 9346a010ddSJoerg Roedel case MSR_F15H_PERF_CTR1: 9446a010ddSJoerg Roedel case MSR_F15H_PERF_CTR2: 9546a010ddSJoerg Roedel case MSR_F15H_PERF_CTR3: 9646a010ddSJoerg Roedel case MSR_F15H_PERF_CTR4: 9746a010ddSJoerg Roedel case MSR_F15H_PERF_CTR5: 981973caddSVitaly Kuznetsov if (!guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) 991973caddSVitaly Kuznetsov return NULL; 1001973caddSVitaly Kuznetsov fallthrough; 10146a010ddSJoerg Roedel case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: 10246a010ddSJoerg Roedel if (type != PMU_TYPE_COUNTER) 10346a010ddSJoerg Roedel return NULL; 10446a010ddSJoerg Roedel break; 10546a010ddSJoerg Roedel default: 10646a010ddSJoerg Roedel return NULL; 10746a010ddSJoerg Roedel } 10846a010ddSJoerg Roedel 10946a010ddSJoerg Roedel return &pmu->gp_counters[msr_to_index(msr)]; 11046a010ddSJoerg Roedel } 11146a010ddSJoerg Roedel 1127aadaa98SLike Xu static bool amd_hw_event_available(struct kvm_pmc *pmc) 11346a010ddSJoerg Roedel { 1147aadaa98SLike Xu return true; 11546a010ddSJoerg Roedel } 11646a010ddSJoerg Roedel 11746a010ddSJoerg Roedel /* check if a PMC is enabled by comparing it against global_ctrl bits. Because 11846a010ddSJoerg Roedel * AMD CPU doesn't have global_ctrl MSR, all PMCs are enabled (return TRUE). 11946a010ddSJoerg Roedel */ 12046a010ddSJoerg Roedel static bool amd_pmc_is_enabled(struct kvm_pmc *pmc) 12146a010ddSJoerg Roedel { 12246a010ddSJoerg Roedel return true; 12346a010ddSJoerg Roedel } 12446a010ddSJoerg Roedel 12546a010ddSJoerg Roedel static struct kvm_pmc *amd_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx) 12646a010ddSJoerg Roedel { 127*5c6a67f4SLike Xu unsigned int num_counters = pmu->nr_arch_gp_counters; 12846a010ddSJoerg Roedel 129*5c6a67f4SLike Xu if (pmc_idx >= num_counters) 130*5c6a67f4SLike Xu return NULL; 13146a010ddSJoerg Roedel 132*5c6a67f4SLike Xu return &pmu->gp_counters[array_index_nospec(pmc_idx, num_counters)]; 13346a010ddSJoerg Roedel } 13446a010ddSJoerg Roedel 135e6cd31f1SJim Mattson static bool amd_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx) 13646a010ddSJoerg Roedel { 13746a010ddSJoerg Roedel struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 13846a010ddSJoerg Roedel 13946a010ddSJoerg Roedel idx &= ~(3u << 30); 14046a010ddSJoerg Roedel 141e6cd31f1SJim Mattson return idx < pmu->nr_arch_gp_counters; 14246a010ddSJoerg Roedel } 14346a010ddSJoerg Roedel 14446a010ddSJoerg Roedel /* idx is the ECX register of RDPMC instruction */ 14546a010ddSJoerg Roedel static struct kvm_pmc *amd_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, 14646a010ddSJoerg Roedel unsigned int idx, u64 *mask) 14746a010ddSJoerg Roedel { 148*5c6a67f4SLike Xu return amd_pmc_idx_to_pmc(vcpu_to_pmu(vcpu), idx & ~(3u << 30)); 14946a010ddSJoerg Roedel } 15046a010ddSJoerg Roedel 151545feb96SSean Christopherson static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) 15246a010ddSJoerg Roedel { 15346a010ddSJoerg Roedel /* All MSRs refer to exactly one PMC, so msr_idx_to_pmc is enough. */ 15446a010ddSJoerg Roedel return false; 15546a010ddSJoerg Roedel } 15646a010ddSJoerg Roedel 15746a010ddSJoerg Roedel static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, u32 msr) 15846a010ddSJoerg Roedel { 15946a010ddSJoerg Roedel struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 16046a010ddSJoerg Roedel struct kvm_pmc *pmc; 16146a010ddSJoerg Roedel 16246a010ddSJoerg Roedel pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER); 16346a010ddSJoerg Roedel pmc = pmc ? pmc : get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL); 16446a010ddSJoerg Roedel 16546a010ddSJoerg Roedel return pmc; 16646a010ddSJoerg Roedel } 16746a010ddSJoerg Roedel 168cbd71758SWei Wang static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) 16946a010ddSJoerg Roedel { 17046a010ddSJoerg Roedel struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 17146a010ddSJoerg Roedel struct kvm_pmc *pmc; 172cbd71758SWei Wang u32 msr = msr_info->index; 17346a010ddSJoerg Roedel 17446a010ddSJoerg Roedel /* MSR_PERFCTRn */ 17546a010ddSJoerg Roedel pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER); 17646a010ddSJoerg Roedel if (pmc) { 177cbd71758SWei Wang msr_info->data = pmc_read_counter(pmc); 17846a010ddSJoerg Roedel return 0; 17946a010ddSJoerg Roedel } 18046a010ddSJoerg Roedel /* MSR_EVNTSELn */ 18146a010ddSJoerg Roedel pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL); 18246a010ddSJoerg Roedel if (pmc) { 183cbd71758SWei Wang msr_info->data = pmc->eventsel; 18446a010ddSJoerg Roedel return 0; 18546a010ddSJoerg Roedel } 18646a010ddSJoerg Roedel 18746a010ddSJoerg Roedel return 1; 18846a010ddSJoerg Roedel } 18946a010ddSJoerg Roedel 19046a010ddSJoerg Roedel static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) 19146a010ddSJoerg Roedel { 19246a010ddSJoerg Roedel struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 19346a010ddSJoerg Roedel struct kvm_pmc *pmc; 19446a010ddSJoerg Roedel u32 msr = msr_info->index; 19546a010ddSJoerg Roedel u64 data = msr_info->data; 19646a010ddSJoerg Roedel 19746a010ddSJoerg Roedel /* MSR_PERFCTRn */ 19846a010ddSJoerg Roedel pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER); 19946a010ddSJoerg Roedel if (pmc) { 20046a010ddSJoerg Roedel pmc->counter += data - pmc_read_counter(pmc); 20175189d1dSLike Xu pmc_update_sample_period(pmc); 20246a010ddSJoerg Roedel return 0; 20346a010ddSJoerg Roedel } 20446a010ddSJoerg Roedel /* MSR_EVNTSELn */ 20546a010ddSJoerg Roedel pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL); 20646a010ddSJoerg Roedel if (pmc) { 2079b026073SJim Mattson data &= ~pmu->reserved_bits; 208fb121aafSLike Xu if (data != pmc->eventsel) { 209fb121aafSLike Xu pmc->eventsel = data; 210e99fae6eSPaolo Bonzini reprogram_counter(pmc); 211fb121aafSLike Xu } 21246a010ddSJoerg Roedel return 0; 21346a010ddSJoerg Roedel } 21446a010ddSJoerg Roedel 21546a010ddSJoerg Roedel return 1; 21646a010ddSJoerg Roedel } 21746a010ddSJoerg Roedel 21846a010ddSJoerg Roedel static void amd_pmu_refresh(struct kvm_vcpu *vcpu) 21946a010ddSJoerg Roedel { 22046a010ddSJoerg Roedel struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 22146a010ddSJoerg Roedel 22246a010ddSJoerg Roedel if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) 22346a010ddSJoerg Roedel pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE; 22446a010ddSJoerg Roedel else 22546a010ddSJoerg Roedel pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS; 22646a010ddSJoerg Roedel 22746a010ddSJoerg Roedel pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << 48) - 1; 228cb1d220dSLike Xu pmu->reserved_bits = 0xfffffff000280000ull; 22995b065bfSJim Mattson pmu->raw_event_mask = AMD64_RAW_EVENT_MASK; 23046a010ddSJoerg Roedel pmu->version = 1; 23146a010ddSJoerg Roedel /* not applicable to AMD; but clean them to prevent any fall out */ 23246a010ddSJoerg Roedel pmu->counter_bitmask[KVM_PMC_FIXED] = 0; 23346a010ddSJoerg Roedel pmu->nr_arch_fixed_counters = 0; 23446a010ddSJoerg Roedel pmu->global_status = 0; 23546a010ddSJoerg Roedel bitmap_set(pmu->all_valid_pmc_idx, 0, pmu->nr_arch_gp_counters); 23646a010ddSJoerg Roedel } 23746a010ddSJoerg Roedel 23846a010ddSJoerg Roedel static void amd_pmu_init(struct kvm_vcpu *vcpu) 23946a010ddSJoerg Roedel { 24046a010ddSJoerg Roedel struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 24146a010ddSJoerg Roedel int i; 24246a010ddSJoerg Roedel 24346a010ddSJoerg Roedel BUILD_BUG_ON(AMD64_NUM_COUNTERS_CORE > INTEL_PMC_MAX_GENERIC); 24446a010ddSJoerg Roedel 24546a010ddSJoerg Roedel for (i = 0; i < AMD64_NUM_COUNTERS_CORE ; i++) { 24646a010ddSJoerg Roedel pmu->gp_counters[i].type = KVM_PMC_GP; 24746a010ddSJoerg Roedel pmu->gp_counters[i].vcpu = vcpu; 24846a010ddSJoerg Roedel pmu->gp_counters[i].idx = i; 24946a010ddSJoerg Roedel pmu->gp_counters[i].current_config = 0; 25046a010ddSJoerg Roedel } 25146a010ddSJoerg Roedel } 25246a010ddSJoerg Roedel 25346a010ddSJoerg Roedel static void amd_pmu_reset(struct kvm_vcpu *vcpu) 25446a010ddSJoerg Roedel { 25546a010ddSJoerg Roedel struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 25646a010ddSJoerg Roedel int i; 25746a010ddSJoerg Roedel 25846a010ddSJoerg Roedel for (i = 0; i < AMD64_NUM_COUNTERS_CORE; i++) { 25946a010ddSJoerg Roedel struct kvm_pmc *pmc = &pmu->gp_counters[i]; 26046a010ddSJoerg Roedel 26146a010ddSJoerg Roedel pmc_stop_counter(pmc); 26246a010ddSJoerg Roedel pmc->counter = pmc->eventsel = 0; 26346a010ddSJoerg Roedel } 26446a010ddSJoerg Roedel } 26546a010ddSJoerg Roedel 26634886e79SLike Xu struct kvm_pmu_ops amd_pmu_ops __initdata = { 2677aadaa98SLike Xu .hw_event_available = amd_hw_event_available, 26846a010ddSJoerg Roedel .pmc_is_enabled = amd_pmc_is_enabled, 26946a010ddSJoerg Roedel .pmc_idx_to_pmc = amd_pmc_idx_to_pmc, 27046a010ddSJoerg Roedel .rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc, 27146a010ddSJoerg Roedel .msr_idx_to_pmc = amd_msr_idx_to_pmc, 27246a010ddSJoerg Roedel .is_valid_rdpmc_ecx = amd_is_valid_rdpmc_ecx, 27346a010ddSJoerg Roedel .is_valid_msr = amd_is_valid_msr, 27446a010ddSJoerg Roedel .get_msr = amd_pmu_get_msr, 27546a010ddSJoerg Roedel .set_msr = amd_pmu_set_msr, 27646a010ddSJoerg Roedel .refresh = amd_pmu_refresh, 27746a010ddSJoerg Roedel .init = amd_pmu_init, 27846a010ddSJoerg Roedel .reset = amd_pmu_reset, 27946a010ddSJoerg Roedel }; 280