xref: /openbmc/linux/arch/x86/kvm/svm/svm_onhyperv.h (revision c4327f15)
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 #if IS_ENABLED(CONFIG_HYPERV)
101e0c7d40SVineeth Pillai #include <asm/mshyperv.h>
111e0c7d40SVineeth Pillai 
121e0c7d40SVineeth Pillai #include "hyperv.h"
131e0c7d40SVineeth Pillai #include "kvm_onhyperv.h"
141e0c7d40SVineeth Pillai 
151e0c7d40SVineeth Pillai static struct kvm_x86_ops svm_x86_ops;
161e0c7d40SVineeth Pillai 
171e0c7d40SVineeth Pillai /*
181e0c7d40SVineeth Pillai  * Hyper-V uses the software reserved 32 bytes in VMCB
191e0c7d40SVineeth Pillai  * control area to expose SVM enlightenments to guests.
201e0c7d40SVineeth Pillai  */
211e0c7d40SVineeth Pillai struct hv_enlightenments {
221e0c7d40SVineeth Pillai 	struct __packed hv_enlightenments_control {
231e0c7d40SVineeth Pillai 		u32 nested_flush_hypercall:1;
241e0c7d40SVineeth Pillai 		u32 msr_bitmap:1;
251e0c7d40SVineeth Pillai 		u32 enlightened_npt_tlb: 1;
261e0c7d40SVineeth Pillai 		u32 reserved:29;
271e0c7d40SVineeth Pillai 	} __packed hv_enlightenments_control;
281e0c7d40SVineeth Pillai 	u32 hv_vp_id;
291e0c7d40SVineeth Pillai 	u64 hv_vm_id;
301e0c7d40SVineeth Pillai 	u64 partition_assist_page;
311e0c7d40SVineeth Pillai 	u64 reserved;
321e0c7d40SVineeth Pillai } __packed;
331e0c7d40SVineeth Pillai 
34*c4327f15SVineeth Pillai /*
35*c4327f15SVineeth Pillai  * Hyper-V uses the software reserved clean bit in VMCB
36*c4327f15SVineeth Pillai  */
37*c4327f15SVineeth Pillai #define VMCB_HV_NESTED_ENLIGHTENMENTS VMCB_SW
38*c4327f15SVineeth Pillai 
391e0c7d40SVineeth Pillai static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
401e0c7d40SVineeth Pillai {
411e0c7d40SVineeth Pillai 	struct hv_enlightenments *hve =
421e0c7d40SVineeth Pillai 		(struct hv_enlightenments *)vmcb->control.reserved_sw;
431e0c7d40SVineeth Pillai 
441e0c7d40SVineeth Pillai 	if (npt_enabled &&
451e0c7d40SVineeth Pillai 	    ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB)
461e0c7d40SVineeth Pillai 		hve->hv_enlightenments_control.enlightened_npt_tlb = 1;
471e0c7d40SVineeth Pillai }
481e0c7d40SVineeth Pillai 
491e0c7d40SVineeth Pillai static inline void svm_hv_hardware_setup(void)
501e0c7d40SVineeth Pillai {
511e0c7d40SVineeth Pillai 	if (npt_enabled &&
521e0c7d40SVineeth Pillai 	    ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB) {
531e0c7d40SVineeth Pillai 		pr_info("kvm: Hyper-V enlightened NPT TLB flush enabled\n");
541e0c7d40SVineeth Pillai 		svm_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
551e0c7d40SVineeth Pillai 		svm_x86_ops.tlb_remote_flush_with_range =
561e0c7d40SVineeth Pillai 				hv_remote_flush_tlb_with_range;
571e0c7d40SVineeth Pillai 	}
581e0c7d40SVineeth Pillai }
591e0c7d40SVineeth Pillai 
60*c4327f15SVineeth Pillai static inline void svm_hv_vmcb_dirty_nested_enlightenments(
61*c4327f15SVineeth Pillai 		struct kvm_vcpu *vcpu)
62*c4327f15SVineeth Pillai {
63*c4327f15SVineeth Pillai 	struct vmcb *vmcb = to_svm(vcpu)->vmcb;
64*c4327f15SVineeth Pillai 	struct hv_enlightenments *hve =
65*c4327f15SVineeth Pillai 		(struct hv_enlightenments *)vmcb->control.reserved_sw;
66*c4327f15SVineeth Pillai 
67*c4327f15SVineeth Pillai 	/*
68*c4327f15SVineeth Pillai 	 * vmcb can be NULL if called during early vcpu init.
69*c4327f15SVineeth Pillai 	 * And its okay not to mark vmcb dirty during vcpu init
70*c4327f15SVineeth Pillai 	 * as we mark it dirty unconditionally towards end of vcpu
71*c4327f15SVineeth Pillai 	 * init phase.
72*c4327f15SVineeth Pillai 	 */
73*c4327f15SVineeth Pillai 	if (vmcb && vmcb_is_clean(vmcb, VMCB_HV_NESTED_ENLIGHTENMENTS) &&
74*c4327f15SVineeth Pillai 	    hve->hv_enlightenments_control.msr_bitmap)
75*c4327f15SVineeth Pillai 		vmcb_mark_dirty(vmcb, VMCB_HV_NESTED_ENLIGHTENMENTS);
76*c4327f15SVineeth Pillai }
771e0c7d40SVineeth Pillai #else
781e0c7d40SVineeth Pillai 
791e0c7d40SVineeth Pillai static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
801e0c7d40SVineeth Pillai {
811e0c7d40SVineeth Pillai }
821e0c7d40SVineeth Pillai 
831e0c7d40SVineeth Pillai static inline void svm_hv_hardware_setup(void)
841e0c7d40SVineeth Pillai {
851e0c7d40SVineeth Pillai }
86*c4327f15SVineeth Pillai 
87*c4327f15SVineeth Pillai static inline void svm_hv_vmcb_dirty_nested_enlightenments(
88*c4327f15SVineeth Pillai 		struct kvm_vcpu *vcpu)
89*c4327f15SVineeth Pillai {
90*c4327f15SVineeth Pillai }
911e0c7d40SVineeth Pillai #endif /* CONFIG_HYPERV */
921e0c7d40SVineeth Pillai 
931e0c7d40SVineeth Pillai #endif /* __ARCH_X86_KVM_SVM_ONHYPERV_H__ */
94