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 182e8cd7a3SLike Xu #define MAX_FIXED_COUNTERS 3 192e8cd7a3SLike Xu 20474a5bb9SWei Huang struct kvm_event_hw_type_mapping { 21474a5bb9SWei Huang u8 eventsel; 22474a5bb9SWei Huang u8 unit_mask; 23474a5bb9SWei Huang unsigned event_type; 24474a5bb9SWei Huang }; 25474a5bb9SWei Huang 2625462f7fSWei Huang struct kvm_pmu_ops { 2725462f7fSWei Huang unsigned (*find_arch_event)(struct kvm_pmu *pmu, u8 event_select, 2825462f7fSWei Huang u8 unit_mask); 2925462f7fSWei Huang unsigned (*find_fixed_event)(int idx); 3025462f7fSWei Huang bool (*pmc_is_enabled)(struct kvm_pmc *pmc); 3125462f7fSWei Huang struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu, int pmc_idx); 3298ff80f5SLike Xu struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu, 3398ff80f5SLike Xu unsigned int idx, u64 *mask); 34c900c156SLike Xu struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, u32 msr); 3598ff80f5SLike Xu int (*is_valid_rdpmc_ecx)(struct kvm_vcpu *vcpu, unsigned int idx); 3625462f7fSWei Huang bool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr); 37cbd71758SWei Wang int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr_info); 3825462f7fSWei Huang int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr_info); 3925462f7fSWei Huang void (*refresh)(struct kvm_vcpu *vcpu); 4025462f7fSWei Huang void (*init)(struct kvm_vcpu *vcpu); 4125462f7fSWei Huang void (*reset)(struct kvm_vcpu *vcpu); 42e6209a3bSLike Xu void (*deliver_pmi)(struct kvm_vcpu *vcpu); 439aa4f622SLike Xu void (*cleanup)(struct kvm_vcpu *vcpu); 4425462f7fSWei Huang }; 4525462f7fSWei Huang 4625462f7fSWei Huang static inline u64 pmc_bitmask(struct kvm_pmc *pmc) 4725462f7fSWei Huang { 4825462f7fSWei Huang struct kvm_pmu *pmu = pmc_to_pmu(pmc); 4925462f7fSWei Huang 5025462f7fSWei Huang return pmu->counter_bitmask[pmc->type]; 5125462f7fSWei Huang } 5225462f7fSWei Huang 5325462f7fSWei Huang static inline u64 pmc_read_counter(struct kvm_pmc *pmc) 5425462f7fSWei Huang { 5525462f7fSWei Huang u64 counter, enabled, running; 5625462f7fSWei Huang 5725462f7fSWei Huang counter = pmc->counter; 58*e79f49c3SLike Xu if (pmc->perf_event && !pmc->is_paused) 5925462f7fSWei Huang counter += perf_event_read_value(pmc->perf_event, 6025462f7fSWei Huang &enabled, &running); 6125462f7fSWei Huang /* FIXME: Scaling needed? */ 6225462f7fSWei Huang return counter & pmc_bitmask(pmc); 6325462f7fSWei Huang } 6425462f7fSWei Huang 65a6da0d77SLike Xu static inline void pmc_release_perf_event(struct kvm_pmc *pmc) 66a6da0d77SLike Xu { 67a6da0d77SLike Xu if (pmc->perf_event) { 68a6da0d77SLike Xu perf_event_release_kernel(pmc->perf_event); 69a6da0d77SLike Xu pmc->perf_event = NULL; 70a6da0d77SLike Xu pmc->current_config = 0; 71b35e5548SLike Xu pmc_to_pmu(pmc)->event_count--; 72a6da0d77SLike Xu } 73a6da0d77SLike Xu } 74a6da0d77SLike Xu 7525462f7fSWei Huang static inline void pmc_stop_counter(struct kvm_pmc *pmc) 7625462f7fSWei Huang { 7725462f7fSWei Huang if (pmc->perf_event) { 7825462f7fSWei Huang pmc->counter = pmc_read_counter(pmc); 79a6da0d77SLike Xu pmc_release_perf_event(pmc); 8025462f7fSWei Huang } 8125462f7fSWei Huang } 8225462f7fSWei Huang 8325462f7fSWei Huang static inline bool pmc_is_gp(struct kvm_pmc *pmc) 8425462f7fSWei Huang { 8525462f7fSWei Huang return pmc->type == KVM_PMC_GP; 8625462f7fSWei Huang } 8725462f7fSWei Huang 8825462f7fSWei Huang static inline bool pmc_is_fixed(struct kvm_pmc *pmc) 8925462f7fSWei Huang { 9025462f7fSWei Huang return pmc->type == KVM_PMC_FIXED; 9125462f7fSWei Huang } 9225462f7fSWei Huang 9325462f7fSWei Huang static inline bool pmc_is_enabled(struct kvm_pmc *pmc) 9425462f7fSWei Huang { 95afaf0b2fSSean Christopherson return kvm_x86_ops.pmu_ops->pmc_is_enabled(pmc); 9625462f7fSWei Huang } 9725462f7fSWei Huang 989477f444SOliver Upton static inline bool kvm_valid_perf_global_ctrl(struct kvm_pmu *pmu, 999477f444SOliver Upton u64 data) 1009477f444SOliver Upton { 1019477f444SOliver Upton return !(pmu->global_ctrl_mask & data); 1029477f444SOliver Upton } 1039477f444SOliver Upton 10425462f7fSWei Huang /* returns general purpose PMC with the specified MSR. Note that it can be 10525462f7fSWei Huang * used for both PERFCTRn and EVNTSELn; that is why it accepts base as a 106d9f6e12fSIngo Molnar * parameter to tell them apart. 10725462f7fSWei Huang */ 10825462f7fSWei Huang static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr, 10925462f7fSWei Huang u32 base) 11025462f7fSWei Huang { 11113c5183aSMarios Pomonis if (msr >= base && msr < base + pmu->nr_arch_gp_counters) { 11213c5183aSMarios Pomonis u32 index = array_index_nospec(msr - base, 11313c5183aSMarios Pomonis pmu->nr_arch_gp_counters); 11413c5183aSMarios Pomonis 11513c5183aSMarios Pomonis return &pmu->gp_counters[index]; 11613c5183aSMarios Pomonis } 11725462f7fSWei Huang 11825462f7fSWei Huang return NULL; 11925462f7fSWei Huang } 12025462f7fSWei Huang 12125462f7fSWei Huang /* returns fixed PMC with the specified MSR */ 12225462f7fSWei Huang static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr) 12325462f7fSWei Huang { 12425462f7fSWei Huang int base = MSR_CORE_PERF_FIXED_CTR0; 12525462f7fSWei Huang 12613c5183aSMarios Pomonis if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) { 12713c5183aSMarios Pomonis u32 index = array_index_nospec(msr - base, 12813c5183aSMarios Pomonis pmu->nr_arch_fixed_counters); 12913c5183aSMarios Pomonis 13013c5183aSMarios Pomonis return &pmu->fixed_counters[index]; 13113c5183aSMarios Pomonis } 13225462f7fSWei Huang 13325462f7fSWei Huang return NULL; 13425462f7fSWei Huang } 13525462f7fSWei Huang 136168d918fSEric Hankland static inline u64 get_sample_period(struct kvm_pmc *pmc, u64 counter_value) 137168d918fSEric Hankland { 138168d918fSEric Hankland u64 sample_period = (-counter_value) & pmc_bitmask(pmc); 139168d918fSEric Hankland 140168d918fSEric Hankland if (!sample_period) 141168d918fSEric Hankland sample_period = pmc_bitmask(pmc) + 1; 142168d918fSEric Hankland return sample_period; 143168d918fSEric Hankland } 144168d918fSEric Hankland 14525462f7fSWei Huang void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel); 14625462f7fSWei Huang void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int fixed_idx); 14725462f7fSWei Huang void reprogram_counter(struct kvm_pmu *pmu, int pmc_idx); 14825462f7fSWei Huang 149474a5bb9SWei Huang void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu); 150474a5bb9SWei Huang void kvm_pmu_handle_event(struct kvm_vcpu *vcpu); 151474a5bb9SWei Huang int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data); 15298ff80f5SLike Xu int kvm_pmu_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx); 153474a5bb9SWei Huang bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr); 154cbd71758SWei Wang int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info); 155474a5bb9SWei Huang int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info); 156474a5bb9SWei Huang void kvm_pmu_refresh(struct kvm_vcpu *vcpu); 157474a5bb9SWei Huang void kvm_pmu_reset(struct kvm_vcpu *vcpu); 158474a5bb9SWei Huang void kvm_pmu_init(struct kvm_vcpu *vcpu); 159b35e5548SLike Xu void kvm_pmu_cleanup(struct kvm_vcpu *vcpu); 160474a5bb9SWei Huang void kvm_pmu_destroy(struct kvm_vcpu *vcpu); 16166bb8a06SEric Hankland int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp); 162474a5bb9SWei Huang 1632d7921c4SArbel Moshe bool is_vmware_backdoor_pmc(u32 pmc_idx); 1642d7921c4SArbel Moshe 16525462f7fSWei Huang extern struct kvm_pmu_ops intel_pmu_ops; 16625462f7fSWei Huang extern struct kvm_pmu_ops amd_pmu_ops; 167474a5bb9SWei Huang #endif /* __KVM_X86_PMU_H */ 168