xref: /linux/arch/x86/kvm/svm/svm_onhyperv.h (revision dd093fb0)
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 #if IS_ENABLED(CONFIG_HYPERV)
10 
11 #include "kvm_onhyperv.h"
12 #include "svm/hyperv.h"
13 
14 static struct kvm_x86_ops svm_x86_ops;
15 
16 int svm_hv_enable_l2_tlb_flush(struct kvm_vcpu *vcpu);
17 
18 static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
19 {
20 	struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
21 
22 	BUILD_BUG_ON(sizeof(vmcb->control.hv_enlightenments) !=
23 		     sizeof(vmcb->control.reserved_sw));
24 
25 	if (npt_enabled &&
26 	    ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB)
27 		hve->hv_enlightenments_control.enlightened_npt_tlb = 1;
28 
29 	if (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)
30 		hve->hv_enlightenments_control.msr_bitmap = 1;
31 }
32 
33 static inline void svm_hv_hardware_setup(void)
34 {
35 	if (npt_enabled &&
36 	    ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB) {
37 		pr_info("kvm: Hyper-V enlightened NPT TLB flush enabled\n");
38 		svm_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
39 		svm_x86_ops.tlb_remote_flush_with_range =
40 				hv_remote_flush_tlb_with_range;
41 	}
42 
43 	if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH) {
44 		int cpu;
45 
46 		pr_info("kvm: Hyper-V Direct TLB Flush enabled\n");
47 		for_each_online_cpu(cpu) {
48 			struct hv_vp_assist_page *vp_ap =
49 				hv_get_vp_assist_page(cpu);
50 
51 			if (!vp_ap)
52 				continue;
53 
54 			vp_ap->nested_control.features.directhypercall = 1;
55 		}
56 		svm_x86_ops.enable_l2_tlb_flush =
57 				svm_hv_enable_l2_tlb_flush;
58 	}
59 }
60 
61 static inline void svm_hv_vmcb_dirty_nested_enlightenments(
62 		struct kvm_vcpu *vcpu)
63 {
64 	struct vmcb *vmcb = to_svm(vcpu)->vmcb;
65 	struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
66 
67 	if (hve->hv_enlightenments_control.msr_bitmap)
68 		vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
69 }
70 
71 static inline void svm_hv_update_vp_id(struct vmcb *vmcb, struct kvm_vcpu *vcpu)
72 {
73 	struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
74 	u32 vp_index = kvm_hv_get_vpindex(vcpu);
75 
76 	if (hve->hv_vp_id != vp_index) {
77 		hve->hv_vp_id = vp_index;
78 		vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
79 	}
80 }
81 #else
82 
83 static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
84 {
85 }
86 
87 static inline void svm_hv_hardware_setup(void)
88 {
89 }
90 
91 static inline void svm_hv_vmcb_dirty_nested_enlightenments(
92 		struct kvm_vcpu *vcpu)
93 {
94 }
95 
96 static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
97 		struct kvm_vcpu *vcpu)
98 {
99 }
100 #endif /* CONFIG_HYPERV */
101 
102 #endif /* __ARCH_X86_KVM_SVM_ONHYPERV_H__ */
103