xref: /openbmc/linux/arch/x86/kvm/pmu.h (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2474a5bb9SWei Huang #ifndef __KVM_X86_PMU_H
3474a5bb9SWei Huang #define __KVM_X86_PMU_H
4474a5bb9SWei Huang 
5474a5bb9SWei Huang #define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu)
6474a5bb9SWei Huang #define pmu_to_vcpu(pmu)  (container_of((pmu), struct kvm_vcpu, arch.pmu))
7474a5bb9SWei Huang #define pmc_to_pmu(pmc)   (&(pmc)->vcpu->arch.pmu)
8474a5bb9SWei Huang 
925462f7fSWei Huang /* retrieve the 4 bits for EN and PMI out of IA32_FIXED_CTR_CTRL */
1025462f7fSWei Huang #define fixed_ctrl_field(ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) & 0xf)
1125462f7fSWei Huang 
12474a5bb9SWei Huang struct kvm_event_hw_type_mapping {
13474a5bb9SWei Huang 	u8 eventsel;
14474a5bb9SWei Huang 	u8 unit_mask;
15474a5bb9SWei Huang 	unsigned event_type;
16474a5bb9SWei Huang };
17474a5bb9SWei Huang 
1825462f7fSWei Huang struct kvm_pmu_ops {
1925462f7fSWei Huang 	unsigned (*find_arch_event)(struct kvm_pmu *pmu, u8 event_select,
2025462f7fSWei Huang 				    u8 unit_mask);
2125462f7fSWei Huang 	unsigned (*find_fixed_event)(int idx);
2225462f7fSWei Huang 	bool (*pmc_is_enabled)(struct kvm_pmc *pmc);
2325462f7fSWei Huang 	struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu, int pmc_idx);
2425462f7fSWei Huang 	struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, unsigned idx);
2525462f7fSWei Huang 	int (*is_valid_msr_idx)(struct kvm_vcpu *vcpu, unsigned idx);
2625462f7fSWei Huang 	bool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr);
2725462f7fSWei Huang 	int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
2825462f7fSWei Huang 	int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
2925462f7fSWei Huang 	void (*refresh)(struct kvm_vcpu *vcpu);
3025462f7fSWei Huang 	void (*init)(struct kvm_vcpu *vcpu);
3125462f7fSWei Huang 	void (*reset)(struct kvm_vcpu *vcpu);
3225462f7fSWei Huang };
3325462f7fSWei Huang 
3425462f7fSWei Huang static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
3525462f7fSWei Huang {
3625462f7fSWei Huang 	struct kvm_pmu *pmu = pmc_to_pmu(pmc);
3725462f7fSWei Huang 
3825462f7fSWei Huang 	return pmu->counter_bitmask[pmc->type];
3925462f7fSWei Huang }
4025462f7fSWei Huang 
4125462f7fSWei Huang static inline u64 pmc_read_counter(struct kvm_pmc *pmc)
4225462f7fSWei Huang {
4325462f7fSWei Huang 	u64 counter, enabled, running;
4425462f7fSWei Huang 
4525462f7fSWei Huang 	counter = pmc->counter;
4625462f7fSWei Huang 	if (pmc->perf_event)
4725462f7fSWei Huang 		counter += perf_event_read_value(pmc->perf_event,
4825462f7fSWei Huang 						 &enabled, &running);
4925462f7fSWei Huang 	/* FIXME: Scaling needed? */
5025462f7fSWei Huang 	return counter & pmc_bitmask(pmc);
5125462f7fSWei Huang }
5225462f7fSWei Huang 
5325462f7fSWei Huang static inline void pmc_stop_counter(struct kvm_pmc *pmc)
5425462f7fSWei Huang {
5525462f7fSWei Huang 	if (pmc->perf_event) {
5625462f7fSWei Huang 		pmc->counter = pmc_read_counter(pmc);
5725462f7fSWei Huang 		perf_event_release_kernel(pmc->perf_event);
5825462f7fSWei Huang 		pmc->perf_event = NULL;
5925462f7fSWei Huang 	}
6025462f7fSWei Huang }
6125462f7fSWei Huang 
6225462f7fSWei Huang static inline bool pmc_is_gp(struct kvm_pmc *pmc)
6325462f7fSWei Huang {
6425462f7fSWei Huang 	return pmc->type == KVM_PMC_GP;
6525462f7fSWei Huang }
6625462f7fSWei Huang 
6725462f7fSWei Huang static inline bool pmc_is_fixed(struct kvm_pmc *pmc)
6825462f7fSWei Huang {
6925462f7fSWei Huang 	return pmc->type == KVM_PMC_FIXED;
7025462f7fSWei Huang }
7125462f7fSWei Huang 
7225462f7fSWei Huang static inline bool pmc_is_enabled(struct kvm_pmc *pmc)
7325462f7fSWei Huang {
7425462f7fSWei Huang 	return kvm_x86_ops->pmu_ops->pmc_is_enabled(pmc);
7525462f7fSWei Huang }
7625462f7fSWei Huang 
7725462f7fSWei Huang /* returns general purpose PMC with the specified MSR. Note that it can be
7825462f7fSWei Huang  * used for both PERFCTRn and EVNTSELn; that is why it accepts base as a
7925462f7fSWei Huang  * paramenter to tell them apart.
8025462f7fSWei Huang  */
8125462f7fSWei Huang static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,
8225462f7fSWei Huang 					 u32 base)
8325462f7fSWei Huang {
8425462f7fSWei Huang 	if (msr >= base && msr < base + pmu->nr_arch_gp_counters)
8525462f7fSWei Huang 		return &pmu->gp_counters[msr - base];
8625462f7fSWei Huang 
8725462f7fSWei Huang 	return NULL;
8825462f7fSWei Huang }
8925462f7fSWei Huang 
9025462f7fSWei Huang /* returns fixed PMC with the specified MSR */
9125462f7fSWei Huang static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)
9225462f7fSWei Huang {
9325462f7fSWei Huang 	int base = MSR_CORE_PERF_FIXED_CTR0;
9425462f7fSWei Huang 
9525462f7fSWei Huang 	if (msr >= base && msr < base + pmu->nr_arch_fixed_counters)
9625462f7fSWei Huang 		return &pmu->fixed_counters[msr - base];
9725462f7fSWei Huang 
9825462f7fSWei Huang 	return NULL;
9925462f7fSWei Huang }
10025462f7fSWei Huang 
10125462f7fSWei Huang void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel);
10225462f7fSWei Huang void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int fixed_idx);
10325462f7fSWei Huang void reprogram_counter(struct kvm_pmu *pmu, int pmc_idx);
10425462f7fSWei Huang 
105474a5bb9SWei Huang void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu);
106474a5bb9SWei Huang void kvm_pmu_handle_event(struct kvm_vcpu *vcpu);
107474a5bb9SWei Huang int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
108474a5bb9SWei Huang int kvm_pmu_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx);
109474a5bb9SWei Huang bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr);
110474a5bb9SWei Huang int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
111474a5bb9SWei Huang int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
112474a5bb9SWei Huang void kvm_pmu_refresh(struct kvm_vcpu *vcpu);
113474a5bb9SWei Huang void kvm_pmu_reset(struct kvm_vcpu *vcpu);
114474a5bb9SWei Huang void kvm_pmu_init(struct kvm_vcpu *vcpu);
115474a5bb9SWei Huang void kvm_pmu_destroy(struct kvm_vcpu *vcpu);
116474a5bb9SWei Huang 
11725462f7fSWei Huang extern struct kvm_pmu_ops intel_pmu_ops;
11825462f7fSWei Huang extern struct kvm_pmu_ops amd_pmu_ops;
119474a5bb9SWei Huang #endif /* __KVM_X86_PMU_H */
120