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