1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2474a5bb9SWei Huang #ifndef __KVM_X86_PMU_H 3474a5bb9SWei Huang #define __KVM_X86_PMU_H 4474a5bb9SWei Huang 513c5183aSMarios Pomonis #include <linux/nospec.h> 613c5183aSMarios Pomonis 7474a5bb9SWei Huang #define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu) 8474a5bb9SWei Huang #define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu)) 9474a5bb9SWei Huang #define pmc_to_pmu(pmc) (&(pmc)->vcpu->arch.pmu) 10474a5bb9SWei Huang 1125462f7fSWei Huang /* retrieve the 4 bits for EN and PMI out of IA32_FIXED_CTR_CTRL */ 1225462f7fSWei Huang #define fixed_ctrl_field(ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) & 0xf) 1325462f7fSWei Huang 142d7921c4SArbel Moshe #define VMWARE_BACKDOOR_PMC_HOST_TSC 0x10000 152d7921c4SArbel Moshe #define VMWARE_BACKDOOR_PMC_REAL_TIME 0x10001 162d7921c4SArbel Moshe #define VMWARE_BACKDOOR_PMC_APPARENT_TIME 0x10002 172d7921c4SArbel Moshe 18474a5bb9SWei Huang struct kvm_event_hw_type_mapping { 19474a5bb9SWei Huang u8 eventsel; 20474a5bb9SWei Huang u8 unit_mask; 21474a5bb9SWei Huang unsigned event_type; 22474a5bb9SWei Huang }; 23474a5bb9SWei Huang 2425462f7fSWei Huang struct kvm_pmu_ops { 257aadaa98SLike Xu bool (*hw_event_available)(struct kvm_pmc *pmc); 2625462f7fSWei Huang bool (*pmc_is_enabled)(struct kvm_pmc *pmc); 2725462f7fSWei Huang struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu, int pmc_idx); 2898ff80f5SLike Xu struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu, 2998ff80f5SLike Xu unsigned int idx, u64 *mask); 30c900c156SLike Xu struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, u32 msr); 31e6cd31f1SJim Mattson bool (*is_valid_rdpmc_ecx)(struct kvm_vcpu *vcpu, unsigned int idx); 32d1c88a40SPaolo Bonzini bool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr, bool host_initiated); 33cbd71758SWei Wang int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr_info); 3425462f7fSWei Huang int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr_info); 3525462f7fSWei Huang void (*refresh)(struct kvm_vcpu *vcpu); 3625462f7fSWei Huang void (*init)(struct kvm_vcpu *vcpu); 3725462f7fSWei Huang void (*reset)(struct kvm_vcpu *vcpu); 38e6209a3bSLike Xu void (*deliver_pmi)(struct kvm_vcpu *vcpu); 399aa4f622SLike Xu void (*cleanup)(struct kvm_vcpu *vcpu); 4025462f7fSWei Huang }; 4125462f7fSWei Huang 428f969c0cSLike Xu void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops); 438f969c0cSLike Xu 4425462f7fSWei Huang static inline u64 pmc_bitmask(struct kvm_pmc *pmc) 4525462f7fSWei Huang { 4625462f7fSWei Huang struct kvm_pmu *pmu = pmc_to_pmu(pmc); 4725462f7fSWei Huang 4825462f7fSWei Huang return pmu->counter_bitmask[pmc->type]; 4925462f7fSWei Huang } 5025462f7fSWei Huang 5125462f7fSWei Huang static inline u64 pmc_read_counter(struct kvm_pmc *pmc) 5225462f7fSWei Huang { 5325462f7fSWei Huang u64 counter, enabled, running; 5425462f7fSWei Huang 5525462f7fSWei Huang counter = pmc->counter; 56e79f49c3SLike Xu if (pmc->perf_event && !pmc->is_paused) 5725462f7fSWei Huang counter += perf_event_read_value(pmc->perf_event, 5825462f7fSWei Huang &enabled, &running); 5925462f7fSWei Huang /* FIXME: Scaling needed? */ 6025462f7fSWei Huang return counter & pmc_bitmask(pmc); 6125462f7fSWei Huang } 6225462f7fSWei Huang 63a6da0d77SLike Xu static inline void pmc_release_perf_event(struct kvm_pmc *pmc) 64a6da0d77SLike Xu { 65a6da0d77SLike Xu if (pmc->perf_event) { 66a6da0d77SLike Xu perf_event_release_kernel(pmc->perf_event); 67a6da0d77SLike Xu pmc->perf_event = NULL; 68a6da0d77SLike Xu pmc->current_config = 0; 69b35e5548SLike Xu pmc_to_pmu(pmc)->event_count--; 70a6da0d77SLike Xu } 71a6da0d77SLike Xu } 72a6da0d77SLike Xu 7325462f7fSWei Huang static inline void pmc_stop_counter(struct kvm_pmc *pmc) 7425462f7fSWei Huang { 7525462f7fSWei Huang if (pmc->perf_event) { 7625462f7fSWei Huang pmc->counter = pmc_read_counter(pmc); 77a6da0d77SLike Xu pmc_release_perf_event(pmc); 7825462f7fSWei Huang } 7925462f7fSWei Huang } 8025462f7fSWei Huang 8125462f7fSWei Huang static inline bool pmc_is_gp(struct kvm_pmc *pmc) 8225462f7fSWei Huang { 8325462f7fSWei Huang return pmc->type == KVM_PMC_GP; 8425462f7fSWei Huang } 8525462f7fSWei Huang 8625462f7fSWei Huang static inline bool pmc_is_fixed(struct kvm_pmc *pmc) 8725462f7fSWei Huang { 8825462f7fSWei Huang return pmc->type == KVM_PMC_FIXED; 8925462f7fSWei Huang } 9025462f7fSWei Huang 919477f444SOliver Upton static inline bool kvm_valid_perf_global_ctrl(struct kvm_pmu *pmu, 929477f444SOliver Upton u64 data) 939477f444SOliver Upton { 949477f444SOliver Upton return !(pmu->global_ctrl_mask & data); 959477f444SOliver Upton } 969477f444SOliver Upton 9725462f7fSWei Huang /* returns general purpose PMC with the specified MSR. Note that it can be 9825462f7fSWei Huang * used for both PERFCTRn and EVNTSELn; that is why it accepts base as a 99d9f6e12fSIngo Molnar * parameter to tell them apart. 10025462f7fSWei Huang */ 10125462f7fSWei Huang static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr, 10225462f7fSWei Huang u32 base) 10325462f7fSWei Huang { 10413c5183aSMarios Pomonis if (msr >= base && msr < base + pmu->nr_arch_gp_counters) { 10513c5183aSMarios Pomonis u32 index = array_index_nospec(msr - base, 10613c5183aSMarios Pomonis pmu->nr_arch_gp_counters); 10713c5183aSMarios Pomonis 10813c5183aSMarios Pomonis return &pmu->gp_counters[index]; 10913c5183aSMarios Pomonis } 11025462f7fSWei Huang 11125462f7fSWei Huang return NULL; 11225462f7fSWei Huang } 11325462f7fSWei Huang 11425462f7fSWei Huang /* returns fixed PMC with the specified MSR */ 11525462f7fSWei Huang static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr) 11625462f7fSWei Huang { 11725462f7fSWei Huang int base = MSR_CORE_PERF_FIXED_CTR0; 11825462f7fSWei Huang 11913c5183aSMarios Pomonis if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) { 12013c5183aSMarios Pomonis u32 index = array_index_nospec(msr - base, 12113c5183aSMarios Pomonis pmu->nr_arch_fixed_counters); 12213c5183aSMarios Pomonis 12313c5183aSMarios Pomonis return &pmu->fixed_counters[index]; 12413c5183aSMarios Pomonis } 12525462f7fSWei Huang 12625462f7fSWei Huang return NULL; 12725462f7fSWei Huang } 12825462f7fSWei Huang 129168d918fSEric Hankland static inline u64 get_sample_period(struct kvm_pmc *pmc, u64 counter_value) 130168d918fSEric Hankland { 131168d918fSEric Hankland u64 sample_period = (-counter_value) & pmc_bitmask(pmc); 132168d918fSEric Hankland 133168d918fSEric Hankland if (!sample_period) 134168d918fSEric Hankland sample_period = pmc_bitmask(pmc) + 1; 135168d918fSEric Hankland return sample_period; 136168d918fSEric Hankland } 137168d918fSEric Hankland 13875189d1dSLike Xu static inline void pmc_update_sample_period(struct kvm_pmc *pmc) 13975189d1dSLike Xu { 14075189d1dSLike Xu if (!pmc->perf_event || pmc->is_paused) 14175189d1dSLike Xu return; 14275189d1dSLike Xu 14375189d1dSLike Xu perf_event_period(pmc->perf_event, 14475189d1dSLike Xu get_sample_period(pmc, pmc->counter)); 14575189d1dSLike Xu } 14675189d1dSLike Xu 14763f21f32SLike Xu static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc) 14863f21f32SLike Xu { 14963f21f32SLike Xu struct kvm_pmu *pmu = pmc_to_pmu(pmc); 15063f21f32SLike Xu 15163f21f32SLike Xu if (pmc_is_fixed(pmc)) 15263f21f32SLike Xu return fixed_ctrl_field(pmu->fixed_ctr_ctrl, 15363f21f32SLike Xu pmc->idx - INTEL_PMC_IDX_FIXED) & 0x3; 15463f21f32SLike Xu 15563f21f32SLike Xu return pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE; 15663f21f32SLike Xu } 15763f21f32SLike Xu 158968635abSLike Xu extern struct x86_pmu_capability kvm_pmu_cap; 159968635abSLike Xu 160968635abSLike Xu static inline void kvm_init_pmu_capability(void) 161968635abSLike Xu { 162*d7808f73SLike Xu bool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL; 163*d7808f73SLike Xu 164968635abSLike Xu perf_get_x86_pmu_capability(&kvm_pmu_cap); 165968635abSLike Xu 166968635abSLike Xu /* 167*d7808f73SLike Xu * For Intel, only support guest architectural pmu 168*d7808f73SLike Xu * on a host with architectural pmu. 169968635abSLike Xu */ 170*d7808f73SLike Xu if ((is_intel && !kvm_pmu_cap.version) || !kvm_pmu_cap.num_counters_gp) { 171968635abSLike Xu memset(&kvm_pmu_cap, 0, sizeof(kvm_pmu_cap)); 172*d7808f73SLike Xu enable_pmu = false; 173*d7808f73SLike Xu return; 174*d7808f73SLike Xu } 175968635abSLike Xu 176968635abSLike Xu kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2); 177968635abSLike Xu kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed, 178968635abSLike Xu KVM_PMC_MAX_FIXED); 179968635abSLike Xu } 180968635abSLike Xu 181a40239b4SLike Xu void reprogram_counter(struct kvm_pmc *pmc); 18225462f7fSWei Huang 183474a5bb9SWei Huang void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu); 184474a5bb9SWei Huang void kvm_pmu_handle_event(struct kvm_vcpu *vcpu); 185474a5bb9SWei Huang int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data); 186e6cd31f1SJim Mattson bool kvm_pmu_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx); 187d1c88a40SPaolo Bonzini bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr, bool host_initiated); 188cbd71758SWei Wang int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info); 189474a5bb9SWei Huang int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info); 190474a5bb9SWei Huang void kvm_pmu_refresh(struct kvm_vcpu *vcpu); 191474a5bb9SWei Huang void kvm_pmu_reset(struct kvm_vcpu *vcpu); 192474a5bb9SWei Huang void kvm_pmu_init(struct kvm_vcpu *vcpu); 193b35e5548SLike Xu void kvm_pmu_cleanup(struct kvm_vcpu *vcpu); 194474a5bb9SWei Huang void kvm_pmu_destroy(struct kvm_vcpu *vcpu); 19566bb8a06SEric Hankland int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp); 1969cd803d4SEric Hankland void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, u64 perf_hw_id); 197474a5bb9SWei Huang 1982d7921c4SArbel Moshe bool is_vmware_backdoor_pmc(u32 pmc_idx); 1992d7921c4SArbel Moshe 20025462f7fSWei Huang extern struct kvm_pmu_ops intel_pmu_ops; 20125462f7fSWei Huang extern struct kvm_pmu_ops amd_pmu_ops; 202474a5bb9SWei Huang #endif /* __KVM_X86_PMU_H */ 203