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
11b9181c8eSLike Xu #define MSR_IA32_MISC_ENABLE_PMU_RO_MASK (MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | \
12b9181c8eSLike Xu MSR_IA32_MISC_ENABLE_BTS_UNAVAIL)
13b9181c8eSLike Xu
1425462f7fSWei Huang /* retrieve the 4 bits for EN and PMI out of IA32_FIXED_CTR_CTRL */
1525462f7fSWei Huang #define fixed_ctrl_field(ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) & 0xf)
1625462f7fSWei Huang
172d7921c4SArbel Moshe #define VMWARE_BACKDOOR_PMC_HOST_TSC 0x10000
182d7921c4SArbel Moshe #define VMWARE_BACKDOOR_PMC_REAL_TIME 0x10001
192d7921c4SArbel Moshe #define VMWARE_BACKDOOR_PMC_APPARENT_TIME 0x10002
202d7921c4SArbel Moshe
2125462f7fSWei Huang struct kvm_pmu_ops {
227aadaa98SLike Xu bool (*hw_event_available)(struct kvm_pmc *pmc);
2325462f7fSWei Huang struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu, int pmc_idx);
2498ff80f5SLike Xu struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu,
2598ff80f5SLike Xu unsigned int idx, u64 *mask);
26c900c156SLike Xu struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, u32 msr);
27e6cd31f1SJim Mattson bool (*is_valid_rdpmc_ecx)(struct kvm_vcpu *vcpu, unsigned int idx);
28545feb96SSean Christopherson bool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr);
29cbd71758SWei Wang int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
3025462f7fSWei Huang int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
3125462f7fSWei Huang void (*refresh)(struct kvm_vcpu *vcpu);
3225462f7fSWei Huang void (*init)(struct kvm_vcpu *vcpu);
3325462f7fSWei Huang void (*reset)(struct kvm_vcpu *vcpu);
34e6209a3bSLike Xu void (*deliver_pmi)(struct kvm_vcpu *vcpu);
359aa4f622SLike Xu void (*cleanup)(struct kvm_vcpu *vcpu);
366a5cba7bSAaron Lewis
376a5cba7bSAaron Lewis const u64 EVENTSEL_EVENT;
388911ce66SSean Christopherson const int MAX_NR_GP_COUNTERS;
396a08083fSLike Xu const int MIN_NR_GP_COUNTERS;
4025462f7fSWei Huang };
4125462f7fSWei Huang
428f969c0cSLike Xu void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops);
438f969c0cSLike Xu
kvm_pmu_has_perf_global_ctrl(struct kvm_pmu * pmu)44c85cdc1cSLike Xu static inline bool kvm_pmu_has_perf_global_ctrl(struct kvm_pmu *pmu)
45c85cdc1cSLike Xu {
46c85cdc1cSLike Xu /*
47c85cdc1cSLike Xu * Architecturally, Intel's SDM states that IA32_PERF_GLOBAL_CTRL is
48c85cdc1cSLike Xu * supported if "CPUID.0AH: EAX[7:0] > 0", i.e. if the PMU version is
49c85cdc1cSLike Xu * greater than zero. However, KVM only exposes and emulates the MSR
50c85cdc1cSLike Xu * to/for the guest if the guest PMU supports at least "Architectural
51c85cdc1cSLike Xu * Performance Monitoring Version 2".
52c85cdc1cSLike Xu *
53c85cdc1cSLike Xu * AMD's version of PERF_GLOBAL_CTRL conveniently shows up with v2.
54c85cdc1cSLike Xu */
55c85cdc1cSLike Xu return pmu->version > 1;
56c85cdc1cSLike Xu }
57c85cdc1cSLike Xu
pmc_bitmask(struct kvm_pmc * pmc)5825462f7fSWei Huang static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
5925462f7fSWei Huang {
6025462f7fSWei Huang struct kvm_pmu *pmu = pmc_to_pmu(pmc);
6125462f7fSWei Huang
6225462f7fSWei Huang return pmu->counter_bitmask[pmc->type];
6325462f7fSWei Huang }
6425462f7fSWei Huang
pmc_read_counter(struct kvm_pmc * pmc)6525462f7fSWei Huang static inline u64 pmc_read_counter(struct kvm_pmc *pmc)
6625462f7fSWei Huang {
6725462f7fSWei Huang u64 counter, enabled, running;
6825462f7fSWei Huang
6925462f7fSWei Huang counter = pmc->counter;
70e79f49c3SLike Xu if (pmc->perf_event && !pmc->is_paused)
7125462f7fSWei Huang counter += perf_event_read_value(pmc->perf_event,
7225462f7fSWei Huang &enabled, &running);
7325462f7fSWei Huang /* FIXME: Scaling needed? */
7425462f7fSWei Huang return counter & pmc_bitmask(pmc);
7525462f7fSWei Huang }
7625462f7fSWei Huang
pmc_write_counter(struct kvm_pmc * pmc,u64 val)77*b29a2acdSRoman Kagan static inline void pmc_write_counter(struct kvm_pmc *pmc, u64 val)
78*b29a2acdSRoman Kagan {
79*b29a2acdSRoman Kagan pmc->counter += val - pmc_read_counter(pmc);
80*b29a2acdSRoman Kagan pmc->counter &= pmc_bitmask(pmc);
81*b29a2acdSRoman Kagan }
82*b29a2acdSRoman Kagan
pmc_is_gp(struct kvm_pmc * pmc)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
pmc_is_fixed(struct kvm_pmc * pmc)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
kvm_valid_perf_global_ctrl(struct kvm_pmu * pmu,u64 data)939477f444SOliver Upton static inline bool kvm_valid_perf_global_ctrl(struct kvm_pmu *pmu,
949477f444SOliver Upton u64 data)
959477f444SOliver Upton {
969477f444SOliver Upton return !(pmu->global_ctrl_mask & data);
979477f444SOliver Upton }
989477f444SOliver Upton
9925462f7fSWei Huang /* returns general purpose PMC with the specified MSR. Note that it can be
10025462f7fSWei Huang * used for both PERFCTRn and EVNTSELn; that is why it accepts base as a
101d9f6e12fSIngo Molnar * parameter to tell them apart.
10225462f7fSWei Huang */
get_gp_pmc(struct kvm_pmu * pmu,u32 msr,u32 base)10325462f7fSWei Huang static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,
10425462f7fSWei Huang u32 base)
10525462f7fSWei Huang {
10613c5183aSMarios Pomonis if (msr >= base && msr < base + pmu->nr_arch_gp_counters) {
10713c5183aSMarios Pomonis u32 index = array_index_nospec(msr - base,
10813c5183aSMarios Pomonis pmu->nr_arch_gp_counters);
10913c5183aSMarios Pomonis
11013c5183aSMarios Pomonis return &pmu->gp_counters[index];
11113c5183aSMarios Pomonis }
11225462f7fSWei Huang
11325462f7fSWei Huang return NULL;
11425462f7fSWei Huang }
11525462f7fSWei Huang
11625462f7fSWei Huang /* returns fixed PMC with the specified MSR */
get_fixed_pmc(struct kvm_pmu * pmu,u32 msr)11725462f7fSWei Huang static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)
11825462f7fSWei Huang {
11925462f7fSWei Huang int base = MSR_CORE_PERF_FIXED_CTR0;
12025462f7fSWei Huang
12113c5183aSMarios Pomonis if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) {
12213c5183aSMarios Pomonis u32 index = array_index_nospec(msr - base,
12313c5183aSMarios Pomonis pmu->nr_arch_fixed_counters);
12413c5183aSMarios Pomonis
12513c5183aSMarios Pomonis return &pmu->fixed_counters[index];
12613c5183aSMarios Pomonis }
12725462f7fSWei Huang
12825462f7fSWei Huang return NULL;
12925462f7fSWei Huang }
13025462f7fSWei Huang
get_sample_period(struct kvm_pmc * pmc,u64 counter_value)131168d918fSEric Hankland static inline u64 get_sample_period(struct kvm_pmc *pmc, u64 counter_value)
132168d918fSEric Hankland {
133168d918fSEric Hankland u64 sample_period = (-counter_value) & pmc_bitmask(pmc);
134168d918fSEric Hankland
135168d918fSEric Hankland if (!sample_period)
136168d918fSEric Hankland sample_period = pmc_bitmask(pmc) + 1;
137168d918fSEric Hankland return sample_period;
138168d918fSEric Hankland }
139168d918fSEric Hankland
pmc_update_sample_period(struct kvm_pmc * pmc)14075189d1dSLike Xu static inline void pmc_update_sample_period(struct kvm_pmc *pmc)
14175189d1dSLike Xu {
14255c590adSLike Xu if (!pmc->perf_event || pmc->is_paused ||
14355c590adSLike Xu !is_sampling_event(pmc->perf_event))
14475189d1dSLike Xu return;
14575189d1dSLike Xu
14675189d1dSLike Xu perf_event_period(pmc->perf_event,
14775189d1dSLike Xu get_sample_period(pmc, pmc->counter));
14875189d1dSLike Xu }
14975189d1dSLike Xu
pmc_speculative_in_use(struct kvm_pmc * pmc)15063f21f32SLike Xu static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc)
15163f21f32SLike Xu {
15263f21f32SLike Xu struct kvm_pmu *pmu = pmc_to_pmu(pmc);
15363f21f32SLike Xu
15463f21f32SLike Xu if (pmc_is_fixed(pmc))
15563f21f32SLike Xu return fixed_ctrl_field(pmu->fixed_ctr_ctrl,
15663f21f32SLike Xu pmc->idx - INTEL_PMC_IDX_FIXED) & 0x3;
15763f21f32SLike Xu
15863f21f32SLike Xu return pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE;
15963f21f32SLike Xu }
16063f21f32SLike Xu
161968635abSLike Xu extern struct x86_pmu_capability kvm_pmu_cap;
162968635abSLike Xu
kvm_init_pmu_capability(const struct kvm_pmu_ops * pmu_ops)1638911ce66SSean Christopherson static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
164968635abSLike Xu {
165d7808f73SLike Xu bool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL;
1666a08083fSLike Xu int min_nr_gp_ctrs = pmu_ops->MIN_NR_GP_COUNTERS;
167d7808f73SLike Xu
1684d7404e5SSean Christopherson /*
1694d7404e5SSean Christopherson * Hybrid PMUs don't play nice with virtualization without careful
1704d7404e5SSean Christopherson * configuration by userspace, and KVM's APIs for reporting supported
1714d7404e5SSean Christopherson * vPMU features do not account for hybrid PMUs. Disable vPMU support
1724d7404e5SSean Christopherson * for hybrid PMUs until KVM gains a way to let userspace opt-in.
1734d7404e5SSean Christopherson */
1744d7404e5SSean Christopherson if (cpu_feature_enabled(X86_FEATURE_HYBRID_CPU))
1754d7404e5SSean Christopherson enable_pmu = false;
1764d7404e5SSean Christopherson
1774d7404e5SSean Christopherson if (enable_pmu) {
178968635abSLike Xu perf_get_x86_pmu_capability(&kvm_pmu_cap);
179968635abSLike Xu
180968635abSLike Xu /*
1816a08083fSLike Xu * WARN if perf did NOT disable hardware PMU if the number of
1826a08083fSLike Xu * architecturally required GP counters aren't present, i.e. if
1836a08083fSLike Xu * there are a non-zero number of counters, but fewer than what
1846a08083fSLike Xu * is architecturally required.
185968635abSLike Xu */
1866a08083fSLike Xu if (!kvm_pmu_cap.num_counters_gp ||
1876a08083fSLike Xu WARN_ON_ONCE(kvm_pmu_cap.num_counters_gp < min_nr_gp_ctrs))
1886a08083fSLike Xu enable_pmu = false;
1896a08083fSLike Xu else if (is_intel && !kvm_pmu_cap.version)
190d7808f73SLike Xu enable_pmu = false;
1914d7404e5SSean Christopherson }
1926ef25aa0SLike Xu
1936ef25aa0SLike Xu if (!enable_pmu) {
1946ef25aa0SLike Xu memset(&kvm_pmu_cap, 0, sizeof(kvm_pmu_cap));
195d7808f73SLike Xu return;
196d7808f73SLike Xu }
197968635abSLike Xu
198968635abSLike Xu kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
1998911ce66SSean Christopherson kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp,
2008911ce66SSean Christopherson pmu_ops->MAX_NR_GP_COUNTERS);
201968635abSLike Xu kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed,
202968635abSLike Xu KVM_PMC_MAX_FIXED);
203968635abSLike Xu }
204968635abSLike Xu
kvm_pmu_request_counter_reprogram(struct kvm_pmc * pmc)2054fa5843dSLike Xu static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc)
20668fb4757SLike Xu {
20768fb4757SLike Xu set_bit(pmc->idx, pmc_to_pmu(pmc)->reprogram_pmi);
20868fb4757SLike Xu kvm_make_request(KVM_REQ_PMU, pmc->vcpu);
20968fb4757SLike Xu }
21025462f7fSWei Huang
reprogram_counters(struct kvm_pmu * pmu,u64 diff)2118de18543SLike Xu static inline void reprogram_counters(struct kvm_pmu *pmu, u64 diff)
2128de18543SLike Xu {
2138de18543SLike Xu int bit;
2148de18543SLike Xu
2158de18543SLike Xu if (!diff)
2168de18543SLike Xu return;
2178de18543SLike Xu
2188de18543SLike Xu for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX)
2198de18543SLike Xu set_bit(bit, pmu->reprogram_pmi);
2208de18543SLike Xu kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu));
2218de18543SLike Xu }
2228de18543SLike Xu
22313afa29aSLike Xu /*
22413afa29aSLike Xu * Check if a PMC is enabled by comparing it against global_ctrl bits.
22513afa29aSLike Xu *
22613afa29aSLike Xu * If the vPMU doesn't have global_ctrl MSR, all vPMCs are enabled.
22713afa29aSLike Xu */
pmc_is_globally_enabled(struct kvm_pmc * pmc)22813afa29aSLike Xu static inline bool pmc_is_globally_enabled(struct kvm_pmc *pmc)
22913afa29aSLike Xu {
23013afa29aSLike Xu struct kvm_pmu *pmu = pmc_to_pmu(pmc);
23113afa29aSLike Xu
23213afa29aSLike Xu if (!kvm_pmu_has_perf_global_ctrl(pmu))
23313afa29aSLike Xu return true;
23413afa29aSLike Xu
23513afa29aSLike Xu return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl);
23613afa29aSLike Xu }
23713afa29aSLike Xu
238474a5bb9SWei Huang void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu);
239474a5bb9SWei Huang void kvm_pmu_handle_event(struct kvm_vcpu *vcpu);
240474a5bb9SWei Huang int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
241e6cd31f1SJim Mattson bool kvm_pmu_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx);
242545feb96SSean Christopherson bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr);
243cbd71758SWei Wang int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
244474a5bb9SWei Huang int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
245474a5bb9SWei Huang void kvm_pmu_refresh(struct kvm_vcpu *vcpu);
246474a5bb9SWei Huang void kvm_pmu_reset(struct kvm_vcpu *vcpu);
247474a5bb9SWei Huang void kvm_pmu_init(struct kvm_vcpu *vcpu);
248b35e5548SLike Xu void kvm_pmu_cleanup(struct kvm_vcpu *vcpu);
249474a5bb9SWei Huang void kvm_pmu_destroy(struct kvm_vcpu *vcpu);
25066bb8a06SEric Hankland int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp);
2519cd803d4SEric Hankland void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, u64 perf_hw_id);
252474a5bb9SWei Huang
2532d7921c4SArbel Moshe bool is_vmware_backdoor_pmc(u32 pmc_idx);
2542d7921c4SArbel Moshe
25525462f7fSWei Huang extern struct kvm_pmu_ops intel_pmu_ops;
25625462f7fSWei Huang extern struct kvm_pmu_ops amd_pmu_ops;
257474a5bb9SWei Huang #endif /* __KVM_X86_PMU_H */
258