1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * KVM L1 hypervisor optimizations on Hyper-V for SVM.
6 #ifndef __ARCH_X86_KVM_SVM_ONHYPERV_H__
7 #define __ARCH_X86_KVM_SVM_ONHYPERV_H__
9 #include <asm/mshyperv.h>
11 #if IS_ENABLED(CONFIG_HYPERV)
13 #include "kvm_onhyperv.h"
14 #include "svm/hyperv.h"
16 static struct kvm_x86_ops svm_x86_ops;
18 int svm_hv_enable_l2_tlb_flush(struct kvm_vcpu *vcpu);
20 static inline bool svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu *vcpu)
22 struct hv_vmcb_enlightenments *hve = &to_svm(vcpu)->vmcb->control.hv_enlightenments;
24 return ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB &&
25 !!hve->hv_enlightenments_control.enlightened_npt_tlb;
28 static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
30 struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
32 BUILD_BUG_ON(sizeof(vmcb->control.hv_enlightenments) !=
33 sizeof(vmcb->control.reserved_sw));
36 ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB)
37 hve->hv_enlightenments_control.enlightened_npt_tlb = 1;
39 if (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)
40 hve->hv_enlightenments_control.msr_bitmap = 1;
43 static inline __init void svm_hv_hardware_setup(void)
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;
53 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH) {
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);
64 vp_ap->nested_control.features.directhypercall = 1;
66 svm_x86_ops.enable_l2_tlb_flush =
67 svm_hv_enable_l2_tlb_flush;
71 static inline void svm_hv_vmcb_dirty_nested_enlightenments(
72 struct kvm_vcpu *vcpu)
74 struct vmcb *vmcb = to_svm(vcpu)->vmcb;
75 struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
77 if (hve->hv_enlightenments_control.msr_bitmap)
78 vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
81 static inline void svm_hv_update_vp_id(struct vmcb *vmcb, struct kvm_vcpu *vcpu)
83 struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
84 u32 vp_index = kvm_hv_get_vpindex(vcpu);
86 if (hve->hv_vp_id != vp_index) {
87 hve->hv_vp_id = vp_index;
88 vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
93 static inline bool svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu *vcpu)
98 static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
102 static inline __init void svm_hv_hardware_setup(void)
106 static inline void svm_hv_vmcb_dirty_nested_enlightenments(
107 struct kvm_vcpu *vcpu)
111 static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
112 struct kvm_vcpu *vcpu)
115 #endif /* CONFIG_HYPERV */
117 #endif /* __ARCH_X86_KVM_SVM_ONHYPERV_H__ */