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.flush_remote_tlbs = hv_flush_remote_tlbs;
49 svm_x86_ops.flush_remote_tlbs_range = hv_flush_remote_tlbs_range;
52 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH) {
55 pr_info(KBUILD_MODNAME ": Hyper-V Direct TLB Flush enabled\n");
56 for_each_online_cpu(cpu) {
57 struct hv_vp_assist_page *vp_ap =
58 hv_get_vp_assist_page(cpu);
63 vp_ap->nested_control.features.directhypercall = 1;
65 svm_x86_ops.enable_l2_tlb_flush =
66 svm_hv_enable_l2_tlb_flush;
70 static inline void svm_hv_vmcb_dirty_nested_enlightenments(
71 struct kvm_vcpu *vcpu)
73 struct vmcb *vmcb = to_svm(vcpu)->vmcb;
74 struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
76 if (hve->hv_enlightenments_control.msr_bitmap)
77 vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
80 static inline void svm_hv_update_vp_id(struct vmcb *vmcb, struct kvm_vcpu *vcpu)
82 struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
83 u32 vp_index = kvm_hv_get_vpindex(vcpu);
85 if (hve->hv_vp_id != vp_index) {
86 hve->hv_vp_id = vp_index;
87 vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS);
92 static inline bool svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu *vcpu)
97 static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
101 static inline __init void svm_hv_hardware_setup(void)
105 static inline void svm_hv_vmcb_dirty_nested_enlightenments(
106 struct kvm_vcpu *vcpu)
110 static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
111 struct kvm_vcpu *vcpu)
114 #endif /* CONFIG_HYPERV */
116 #endif /* __ARCH_X86_KVM_SVM_ONHYPERV_H__ */