blob: 489ca56212c617c0225ea226fea5530ea0a1edc9 [file] [log] [blame]
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00001/* 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#include <asm/mshyperv.h>
11
12#include "hyperv.h"
13#include "kvm_onhyperv.h"
14
15static struct kvm_x86_ops svm_x86_ops;
16
17/*
18 * Hyper-V uses the software reserved 32 bytes in VMCB
19 * control area to expose SVM enlightenments to guests.
20 */
21struct hv_enlightenments {
22 struct __packed hv_enlightenments_control {
23 u32 nested_flush_hypercall:1;
24 u32 msr_bitmap:1;
25 u32 enlightened_npt_tlb: 1;
26 u32 reserved:29;
27 } __packed hv_enlightenments_control;
28 u32 hv_vp_id;
29 u64 hv_vm_id;
30 u64 partition_assist_page;
31 u64 reserved;
32} __packed;
33
Vineeth Pillaic4327f12021-06-03 15:14:39 +000034/*
35 * Hyper-V uses the software reserved clean bit in VMCB
36 */
37#define VMCB_HV_NESTED_ENLIGHTENMENTS VMCB_SW
38
Vineeth Pillai11836462021-06-03 15:14:40 +000039int svm_hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu);
40
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +000041static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
42{
43 struct hv_enlightenments *hve =
44 (struct hv_enlightenments *)vmcb->control.reserved_sw;
45
46 if (npt_enabled &&
47 ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB)
48 hve->hv_enlightenments_control.enlightened_npt_tlb = 1;
Vitaly Kuznetsov38dfa832021-12-20 16:21:36 +010049
50 if (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)
51 hve->hv_enlightenments_control.msr_bitmap = 1;
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +000052}
53
54static inline void svm_hv_hardware_setup(void)
55{
56 if (npt_enabled &&
57 ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB) {
58 pr_info("kvm: Hyper-V enlightened NPT TLB flush enabled\n");
59 svm_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
60 svm_x86_ops.tlb_remote_flush_with_range =
61 hv_remote_flush_tlb_with_range;
62 }
Vineeth Pillai11836462021-06-03 15:14:40 +000063
64 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH) {
65 int cpu;
66
67 pr_info("kvm: Hyper-V Direct TLB Flush enabled\n");
68 for_each_online_cpu(cpu) {
69 struct hv_vp_assist_page *vp_ap =
70 hv_get_vp_assist_page(cpu);
71
72 if (!vp_ap)
73 continue;
74
75 vp_ap->nested_control.features.directhypercall = 1;
76 }
77 svm_x86_ops.enable_direct_tlbflush =
78 svm_hv_enable_direct_tlbflush;
79 }
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +000080}
81
Vineeth Pillaic4327f12021-06-03 15:14:39 +000082static inline void svm_hv_vmcb_dirty_nested_enlightenments(
83 struct kvm_vcpu *vcpu)
84{
85 struct vmcb *vmcb = to_svm(vcpu)->vmcb;
86 struct hv_enlightenments *hve =
87 (struct hv_enlightenments *)vmcb->control.reserved_sw;
88
Vitaly Kuznetsovaa3b39f2021-12-20 16:21:35 +010089 if (hve->hv_enlightenments_control.msr_bitmap)
Vineeth Pillaic4327f12021-06-03 15:14:39 +000090 vmcb_mark_dirty(vmcb, VMCB_HV_NESTED_ENLIGHTENMENTS);
91}
Vineeth Pillai11836462021-06-03 15:14:40 +000092
93static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
94 struct kvm_vcpu *vcpu)
95{
96 struct hv_enlightenments *hve =
97 (struct hv_enlightenments *)vmcb->control.reserved_sw;
98 u32 vp_index = kvm_hv_get_vpindex(vcpu);
99
100 if (hve->hv_vp_id != vp_index) {
101 hve->hv_vp_id = vp_index;
102 vmcb_mark_dirty(vmcb, VMCB_HV_NESTED_ENLIGHTENMENTS);
103 }
104}
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +0000105#else
106
107static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
108{
109}
110
111static inline void svm_hv_hardware_setup(void)
112{
113}
Vineeth Pillaic4327f12021-06-03 15:14:39 +0000114
115static inline void svm_hv_vmcb_dirty_nested_enlightenments(
116 struct kvm_vcpu *vcpu)
117{
118}
Vineeth Pillai11836462021-06-03 15:14:40 +0000119
120static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
121 struct kvm_vcpu *vcpu)
122{
123}
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +0000124#endif /* CONFIG_HYPERV */
125
126#endif /* __ARCH_X86_KVM_SVM_ONHYPERV_H__ */