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.flush_remote_tlbs = hv_flush_remote_tlbs; 49 svm_x86_ops.flush_remote_tlbs_range = hv_flush_remote_tlbs_range; 50 } 51 52 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH) { 53 int cpu; 54 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); 59 60 if (!vp_ap) 61 continue; 62 63 vp_ap->nested_control.features.directhypercall = 1; 64 } 65 svm_x86_ops.enable_l2_tlb_flush = 66 svm_hv_enable_l2_tlb_flush; 67 } 68 } 69 70 static inline void svm_hv_vmcb_dirty_nested_enlightenments( 71 struct kvm_vcpu *vcpu) 72 { 73 struct vmcb *vmcb = to_svm(vcpu)->vmcb; 74 struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments; 75 76 if (hve->hv_enlightenments_control.msr_bitmap) 77 vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS); 78 } 79 80 static inline void svm_hv_update_vp_id(struct vmcb *vmcb, struct kvm_vcpu *vcpu) 81 { 82 struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments; 83 u32 vp_index = kvm_hv_get_vpindex(vcpu); 84 85 if (hve->hv_vp_id != vp_index) { 86 hve->hv_vp_id = vp_index; 87 vmcb_mark_dirty(vmcb, HV_VMCB_NESTED_ENLIGHTENMENTS); 88 } 89 } 90 #else 91 92 static inline bool svm_hv_is_enlightened_tlb_enabled(struct kvm_vcpu *vcpu) 93 { 94 return false; 95 } 96 97 static inline void svm_hv_init_vmcb(struct vmcb *vmcb) 98 { 99 } 100 101 static inline __init void svm_hv_hardware_setup(void) 102 { 103 } 104 105 static inline void svm_hv_vmcb_dirty_nested_enlightenments( 106 struct kvm_vcpu *vcpu) 107 { 108 } 109 110 static inline void svm_hv_update_vp_id(struct vmcb *vmcb, 111 struct kvm_vcpu *vcpu) 112 { 113 } 114 #endif /* CONFIG_HYPERV */ 115 116 #endif /* __ARCH_X86_KVM_SVM_ONHYPERV_H__ */ 117