xref: /linux/arch/x86/kvm/svm/svm_onhyperv.h (revision 68a052239fc4b351e961f698b824f7654a346091)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * KVM L1 hypervisor optimizations on Hyper-V for SVM.
4  */
5 
6 #ifndef __ARCH_X86_KVM_SVM_ONHYPERV_H__
7 #define __ARCH_X86_KVM_SVM_ONHYPERV_H__
8 
9 #include <asm/mshyperv.h>
10 
11 #if IS_ENABLED(CONFIG_HYPERV)
12 
13 #include "kvm_onhyperv.h"
14 #include "svm/hyperv.h"
15 
16 __init void svm_hv_hardware_setup(void);
17 
18 static inline bool svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu *vcpu)
19 {
20 	struct hv_vmcb_enlightenments *hve = &to_svm(vcpu)->vmcb->control.hv_enlightenments;
21 
22 	return ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB &&
23 	       !!hve->hv_enlightenments_control.enlightened_npt_tlb;
24 }
25 
26 static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
27 {
28 	struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
29 
30 	BUILD_BUG_ON(sizeof(vmcb->control.hv_enlightenments) !=
31 		     sizeof(vmcb->control.reserved_sw));
32 
33 	if (npt_enabled &&
34 	    ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB)
35 		hve->hv_enlightenments_control.enlightened_npt_tlb = 1;
36 
37 	if (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)
38 		hve->hv_enlightenments_control.msr_bitmap = 1;
39 }
40 
41 static inline void svm_hv_vmcb_dirty_nested_enlightenments(
42 		struct kvm_vcpu *vcpu)
43 {
44 	struct vmcb *vmcb = to_svm(vcpu)->vmcb;
45 	struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
46 
47 	if (hve->hv_enlightenments_control.msr_bitmap)
48 		vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
49 }
50 
51 static inline void svm_hv_update_vp_id(struct vmcb *vmcb, struct kvm_vcpu *vcpu)
52 {
53 	struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
54 	u32 vp_index = kvm_hv_get_vpindex(vcpu);
55 
56 	if (hve->hv_vp_id != vp_index) {
57 		hve->hv_vp_id = vp_index;
58 		vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
59 	}
60 }
61 #else
62 
63 static inline bool svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu *vcpu)
64 {
65 	return false;
66 }
67 
68 static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
69 {
70 }
71 
72 static inline __init void svm_hv_hardware_setup(void)
73 {
74 }
75 
76 static inline void svm_hv_vmcb_dirty_nested_enlightenments(
77 		struct kvm_vcpu *vcpu)
78 {
79 }
80 
81 static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
82 		struct kvm_vcpu *vcpu)
83 {
84 }
85 #endif /* CONFIG_HYPERV */
86 
87 #endif /* __ARCH_X86_KVM_SVM_ONHYPERV_H__ */
88