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