xref: /openbmc/linux/arch/x86/kvm/svm/svm_onhyperv.h (revision 8a1300ff)
11e0c7d40SVineeth Pillai /* SPDX-License-Identifier: GPL-2.0-only */
21e0c7d40SVineeth Pillai /*
31e0c7d40SVineeth Pillai  * KVM L1 hypervisor optimizations on Hyper-V for SVM.
41e0c7d40SVineeth Pillai  */
51e0c7d40SVineeth Pillai 
61e0c7d40SVineeth Pillai #ifndef __ARCH_X86_KVM_SVM_ONHYPERV_H__
71e0c7d40SVineeth Pillai #define __ARCH_X86_KVM_SVM_ONHYPERV_H__
81e0c7d40SVineeth Pillai 
91e0c7d40SVineeth Pillai #include <asm/mshyperv.h>
101e0c7d40SVineeth Pillai 
111e0c7d40SVineeth Pillai #if IS_ENABLED(CONFIG_HYPERV)
129e083ec7SVitaly Kuznetsov 
131e0c7d40SVineeth Pillai #include "kvm_onhyperv.h"
141e0c7d40SVineeth Pillai #include "svm/hyperv.h"
151e0c7d40SVineeth Pillai 
16b83237adSVitaly Kuznetsov static struct kvm_x86_ops svm_x86_ops;
171183646aSVineeth Pillai 
181e0c7d40SVineeth Pillai int svm_hv_enable_l2_tlb_flush(struct kvm_vcpu *vcpu);
191e0c7d40SVineeth Pillai 
svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu * vcpu)2026b516bbSSean Christopherson static inline bool svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu *vcpu)
2168ae7c7bSSean Christopherson {
2268ae7c7bSSean Christopherson 	struct hv_vmcb_enlightenments *hve = &to_svm(vcpu)->vmcb->control.hv_enlightenments;
2368ae7c7bSSean Christopherson 
241e0c7d40SVineeth Pillai 	return ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB &&
251e0c7d40SVineeth Pillai 	       !!hve->hv_enlightenments_control.enlightened_npt_tlb;
261e0c7d40SVineeth Pillai }
271e0c7d40SVineeth Pillai 
svm_hv_init_vmcb(struct vmcb * vmcb)2838dfa830SVitaly Kuznetsov static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
2938dfa830SVitaly Kuznetsov {
3038dfa830SVitaly Kuznetsov 	struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
311e0c7d40SVineeth Pillai 
321e0c7d40SVineeth Pillai 	BUILD_BUG_ON(sizeof(vmcb->control.hv_enlightenments) !=
3345dd9bc7SRandy Dunlap 		     sizeof(vmcb->control.reserved_sw));
341e0c7d40SVineeth Pillai 
351e0c7d40SVineeth Pillai 	if (npt_enabled &&
361e0c7d40SVineeth Pillai 	    ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB)
378d20bd63SSean Christopherson 		hve->hv_enlightenments_control.enlightened_npt_tlb = 1;
38*8a1300ffSSean Christopherson 
39*8a1300ffSSean Christopherson 	if (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)
401e0c7d40SVineeth Pillai 		hve->hv_enlightenments_control.msr_bitmap = 1;
411183646aSVineeth Pillai }
421183646aSVineeth Pillai 
svm_hv_hardware_setup(void)431183646aSVineeth Pillai static inline __init void svm_hv_hardware_setup(void)
441183646aSVineeth Pillai {
458d20bd63SSean Christopherson 	if (npt_enabled &&
461183646aSVineeth Pillai 	    ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB) {
471183646aSVineeth Pillai 		pr_info(KBUILD_MODNAME ": Hyper-V enlightened NPT TLB flush enabled\n");
481183646aSVineeth Pillai 		svm_x86_ops.flush_remote_tlbs = hv_flush_remote_tlbs;
491183646aSVineeth Pillai 		svm_x86_ops.flush_remote_tlbs_range = hv_flush_remote_tlbs_range;
501183646aSVineeth Pillai 	}
511183646aSVineeth Pillai 
521183646aSVineeth Pillai 	if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH) {
531183646aSVineeth Pillai 		int cpu;
541183646aSVineeth Pillai 
55b83237adSVitaly Kuznetsov 		pr_info(KBUILD_MODNAME ": Hyper-V Direct TLB Flush enabled\n");
56b83237adSVitaly Kuznetsov 		for_each_online_cpu(cpu) {
571183646aSVineeth Pillai 			struct hv_vp_assist_page *vp_ap =
581e0c7d40SVineeth Pillai 				hv_get_vp_assist_page(cpu);
591e0c7d40SVineeth Pillai 
60c4327f15SVineeth Pillai 			if (!vp_ap)
61c4327f15SVineeth Pillai 				continue;
62c4327f15SVineeth Pillai 
63c4327f15SVineeth Pillai 			vp_ap->nested_control.features.directhypercall = 1;
6426b516bbSSean Christopherson 		}
65c4327f15SVineeth Pillai 		svm_x86_ops.enable_l2_tlb_flush =
66aa3b39f3SVitaly Kuznetsov 				svm_hv_enable_l2_tlb_flush;
67089fe572SSean Christopherson 	}
68c4327f15SVineeth Pillai }
691183646aSVineeth Pillai 
svm_hv_vmcb_dirty_nested_enlightenments(struct kvm_vcpu * vcpu)7068ae7c7bSSean Christopherson static inline void svm_hv_vmcb_dirty_nested_enlightenments(
711183646aSVineeth Pillai 		struct kvm_vcpu *vcpu)
7226b516bbSSean Christopherson {
731183646aSVineeth Pillai 	struct vmcb *vmcb = to_svm(vcpu)->vmcb;
741183646aSVineeth Pillai 	struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
751183646aSVineeth Pillai 
761183646aSVineeth Pillai 	if (hve->hv_enlightenments_control.msr_bitmap)
77089fe572SSean Christopherson 		vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
781183646aSVineeth Pillai }
791183646aSVineeth Pillai 
svm_hv_update_vp_id(struct vmcb * vmcb,struct kvm_vcpu * vcpu)801e0c7d40SVineeth Pillai static inline void svm_hv_update_vp_id(struct vmcb *vmcb, struct kvm_vcpu *vcpu)
811e0c7d40SVineeth Pillai {
821e0c7d40SVineeth Pillai 	struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
831e0c7d40SVineeth Pillai 	u32 vp_index = kvm_hv_get_vpindex(vcpu);
841e0c7d40SVineeth Pillai 
851e0c7d40SVineeth Pillai 	if (hve->hv_vp_id != vp_index) {
8645dd9bc7SRandy Dunlap 		hve->hv_vp_id = vp_index;
871e0c7d40SVineeth Pillai 		vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
881e0c7d40SVineeth Pillai 	}
89c4327f15SVineeth Pillai }
90c4327f15SVineeth Pillai #else
91c4327f15SVineeth Pillai 
svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu * vcpu)92c4327f15SVineeth Pillai static inline bool svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu *vcpu)
93c4327f15SVineeth Pillai {
941183646aSVineeth Pillai 	return false;
951183646aSVineeth Pillai }
961183646aSVineeth Pillai 
svm_hv_init_vmcb(struct vmcb * vmcb)971183646aSVineeth Pillai static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
981183646aSVineeth Pillai {
991e0c7d40SVineeth Pillai }
1001e0c7d40SVineeth Pillai 
svm_hv_hardware_setup(void)1011e0c7d40SVineeth Pillai static inline __init void svm_hv_hardware_setup(void)
102 {
103 }
104 
svm_hv_vmcb_dirty_nested_enlightenments(struct kvm_vcpu * vcpu)105 static inline void svm_hv_vmcb_dirty_nested_enlightenments(
106 		struct kvm_vcpu *vcpu)
107 {
108 }
109 
svm_hv_update_vp_id(struct vmcb * vmcb,struct kvm_vcpu * vcpu)110 static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
111 		struct kvm_vcpu *vcpu)
112 {
113 }
114 #endif /* CONFIG_HYPERV */
115 
116 #endif /* __ARCH_X86_KVM_SVM_ONHYPERV_H__ */
117