blob: c53b8bf8d01386d716ebb35c470c1619e09826a7 [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;
49}
50
51static inline void svm_hv_hardware_setup(void)
52{
53 if (npt_enabled &&
54 ms_hyperv.nested_features & HV_X64_NESTED_ENLIGHTENED_TLB) {
55 pr_info("kvm: Hyper-V enlightened NPT TLB flush enabled\n");
56 svm_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
57 svm_x86_ops.tlb_remote_flush_with_range =
58 hv_remote_flush_tlb_with_range;
59 }
Vineeth Pillai11836462021-06-03 15:14:40 +000060
61 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH) {
62 int cpu;
63
64 pr_info("kvm: Hyper-V Direct TLB Flush enabled\n");
65 for_each_online_cpu(cpu) {
66 struct hv_vp_assist_page *vp_ap =
67 hv_get_vp_assist_page(cpu);
68
69 if (!vp_ap)
70 continue;
71
72 vp_ap->nested_control.features.directhypercall = 1;
73 }
74 svm_x86_ops.enable_direct_tlbflush =
75 svm_hv_enable_direct_tlbflush;
76 }
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +000077}
78
Vineeth Pillaic4327f12021-06-03 15:14:39 +000079static inline void svm_hv_vmcb_dirty_nested_enlightenments(
80 struct kvm_vcpu *vcpu)
81{
82 struct vmcb *vmcb = to_svm(vcpu)->vmcb;
83 struct hv_enlightenments *hve =
84 (struct hv_enlightenments *)vmcb->control.reserved_sw;
85
86 /*
87 * vmcb can be NULL if called during early vcpu init.
88 * And its okay not to mark vmcb dirty during vcpu init
89 * as we mark it dirty unconditionally towards end of vcpu
90 * init phase.
91 */
Paolo Bonzini3fa5e8f2021-07-26 12:39:01 -040092 if (vmcb_is_clean(vmcb, VMCB_HV_NESTED_ENLIGHTENMENTS) &&
Vineeth Pillaic4327f12021-06-03 15:14:39 +000093 hve->hv_enlightenments_control.msr_bitmap)
94 vmcb_mark_dirty(vmcb, VMCB_HV_NESTED_ENLIGHTENMENTS);
95}
Vineeth Pillai11836462021-06-03 15:14:40 +000096
97static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
98 struct kvm_vcpu *vcpu)
99{
100 struct hv_enlightenments *hve =
101 (struct hv_enlightenments *)vmcb->control.reserved_sw;
102 u32 vp_index = kvm_hv_get_vpindex(vcpu);
103
104 if (hve->hv_vp_id != vp_index) {
105 hve->hv_vp_id = vp_index;
106 vmcb_mark_dirty(vmcb, VMCB_HV_NESTED_ENLIGHTENMENTS);
107 }
108}
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +0000109#else
110
111static inline void svm_hv_init_vmcb(struct vmcb *vmcb)
112{
113}
114
115static inline void svm_hv_hardware_setup(void)
116{
117}
Vineeth Pillaic4327f12021-06-03 15:14:39 +0000118
119static inline void svm_hv_vmcb_dirty_nested_enlightenments(
120 struct kvm_vcpu *vcpu)
121{
122}
Vineeth Pillai11836462021-06-03 15:14:40 +0000123
124static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
125 struct kvm_vcpu *vcpu)
126{
127}
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +0000128#endif /* CONFIG_HYPERV */
129
130#endif /* __ARCH_X86_KVM_SVM_ONHYPERV_H__ */