Commit | Line | Data |
---|---|---|
765e33f5 MK |
1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | ||
3 | /* | |
4 | * Linux-specific definitions for managing interactions with Microsoft's | |
5 | * Hyper-V hypervisor. The definitions in this file are architecture | |
6 | * independent. See arch/<arch>/include/asm/mshyperv.h for definitions | |
7 | * that are specific to architecture <arch>. | |
8 | * | |
9 | * Definitions that are specified in the Hyper-V Top Level Functional | |
10 | * Spec (TLFS) should not go in this file, but should instead go in | |
11 | * hyperv-tlfs.h. | |
12 | * | |
13 | * Copyright (C) 2019, Microsoft, Inc. | |
14 | * | |
15 | * Author : Michael Kelley <mikelley@microsoft.com> | |
16 | */ | |
17 | ||
18 | #ifndef _ASM_GENERIC_MSHYPERV_H | |
19 | #define _ASM_GENERIC_MSHYPERV_H | |
20 | ||
21 | #include <linux/types.h> | |
22 | #include <linux/atomic.h> | |
23 | #include <linux/bitops.h> | |
24 | #include <linux/cpumask.h> | |
ba3f5839 | 25 | #include <linux/nmi.h> |
765e33f5 MK |
26 | #include <asm/ptrace.h> |
27 | #include <asm/hyperv-tlfs.h> | |
28 | ||
812b0597 MK |
29 | #define VTPM_BASE_ADDRESS 0xfed40000 |
30 | ||
765e33f5 MK |
31 | struct ms_hyperv_info { |
32 | u32 features; | |
6dc2a774 | 33 | u32 priv_high; |
765e33f5 MK |
34 | u32 misc_features; |
35 | u32 hints; | |
36 | u32 nested_features; | |
37 | u32 max_vp_index; | |
38 | u32 max_lp_index; | |
a6c76bb0 | 39 | u32 isolation_config_a; |
af788f35 TL |
40 | union { |
41 | u32 isolation_config_b; | |
42 | struct { | |
43 | u32 cvm_type : 4; | |
44 | u32 reserved1 : 1; | |
45 | u32 shared_gpa_boundary_active : 1; | |
46 | u32 shared_gpa_boundary_bits : 6; | |
47 | u32 reserved2 : 20; | |
48 | }; | |
49 | }; | |
50 | u64 shared_gpa_boundary; | |
765e33f5 MK |
51 | }; |
52 | extern struct ms_hyperv_info ms_hyperv; | |
c4bdf94f | 53 | extern bool hv_nested; |
765e33f5 | 54 | |
db3c65bc MK |
55 | extern void * __percpu *hyperv_pcpu_input_arg; |
56 | extern void * __percpu *hyperv_pcpu_output_arg; | |
afca4d95 | 57 | |
765e33f5 MK |
58 | extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr); |
59 | extern u64 hv_do_fast_hypercall8(u16 control, u64 input8); | |
faff4406 | 60 | extern bool hv_isolation_type_snp(void); |
765e33f5 | 61 | |
753ed9c9 JS |
62 | /* Helper functions that provide a consistent pattern for checking Hyper-V hypercall status. */ |
63 | static inline int hv_result(u64 status) | |
64 | { | |
65 | return status & HV_HYPERCALL_RESULT_MASK; | |
66 | } | |
67 | ||
68 | static inline bool hv_result_success(u64 status) | |
69 | { | |
70 | return hv_result(status) == HV_STATUS_SUCCESS; | |
71 | } | |
72 | ||
73 | static inline unsigned int hv_repcomp(u64 status) | |
74 | { | |
75 | /* Bits [43:32] of status have 'Reps completed' data. */ | |
76 | return (status & HV_HYPERCALL_REP_COMP_MASK) >> | |
77 | HV_HYPERCALL_REP_COMP_OFFSET; | |
78 | } | |
79 | ||
6523592c JS |
80 | /* |
81 | * Rep hypercalls. Callers of this functions are supposed to ensure that | |
82 | * rep_count and varhead_size comply with Hyper-V hypercall definition. | |
83 | */ | |
84 | static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size, | |
85 | void *input, void *output) | |
86 | { | |
87 | u64 control = code; | |
88 | u64 status; | |
89 | u16 rep_comp; | |
90 | ||
91 | control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET; | |
92 | control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET; | |
93 | ||
94 | do { | |
95 | status = hv_do_hypercall(control, input, output); | |
753ed9c9 | 96 | if (!hv_result_success(status)) |
6523592c JS |
97 | return status; |
98 | ||
753ed9c9 | 99 | rep_comp = hv_repcomp(status); |
6523592c JS |
100 | |
101 | control &= ~HV_HYPERCALL_REP_START_MASK; | |
102 | control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET; | |
103 | ||
104 | touch_nmi_watchdog(); | |
105 | } while (rep_comp < rep_count); | |
106 | ||
107 | return status; | |
108 | } | |
765e33f5 MK |
109 | |
110 | /* Generate the guest OS identifier as described in the Hyper-V TLFS */ | |
d5ebde1e | 111 | static inline u64 hv_generate_guest_id(u64 kernel_version) |
765e33f5 | 112 | { |
d5ebde1e | 113 | u64 guest_id; |
765e33f5 | 114 | |
d5ebde1e | 115 | guest_id = (((u64)HV_LINUX_VENDOR_ID) << 48); |
765e33f5 | 116 | guest_id |= (kernel_version << 16); |
765e33f5 MK |
117 | |
118 | return guest_id; | |
119 | } | |
120 | ||
765e33f5 MK |
121 | /* Free the message slot and signal end-of-message if required */ |
122 | static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type) | |
123 | { | |
124 | /* | |
125 | * On crash we're reading some other CPU's message page and we need | |
126 | * to be careful: this other CPU may already had cleared the header | |
127 | * and the host may already had delivered some other message there. | |
128 | * In case we blindly write msg->header.message_type we're going | |
129 | * to lose it. We can still lose a message of the same type but | |
130 | * we count on the fact that there can only be one | |
131 | * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages | |
132 | * on crash. | |
133 | */ | |
134 | if (cmpxchg(&msg->header.message_type, old_msg_type, | |
135 | HVMSG_NONE) != old_msg_type) | |
136 | return; | |
137 | ||
138 | /* | |
139 | * The cmxchg() above does an implicit memory barrier to | |
140 | * ensure the write to MessageType (ie set to | |
141 | * HVMSG_NONE) happens before we read the | |
142 | * MessagePending and EOMing. Otherwise, the EOMing | |
143 | * will not deliver any more messages since there is | |
144 | * no empty slot | |
145 | */ | |
146 | if (msg->header.message_flags.msg_pending) { | |
147 | /* | |
148 | * This will cause message queue rescan to | |
149 | * possibly deliver another msg from the | |
150 | * hypervisor | |
151 | */ | |
f3c5e63c | 152 | hv_set_register(HV_REGISTER_EOM, 0); |
765e33f5 MK |
153 | } |
154 | } | |
155 | ||
d608715d MK |
156 | void hv_setup_vmbus_handler(void (*handler)(void)); |
157 | void hv_remove_vmbus_handler(void); | |
a620bbaa MK |
158 | void hv_setup_stimer0_handler(void (*handler)(void)); |
159 | void hv_remove_stimer0_handler(void); | |
765e33f5 MK |
160 | |
161 | void hv_setup_kexec_handler(void (*handler)(void)); | |
162 | void hv_remove_kexec_handler(void); | |
163 | void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs)); | |
164 | void hv_remove_crash_handler(void); | |
165 | ||
626b901f | 166 | extern int vmbus_interrupt; |
d608715d | 167 | extern int vmbus_irq; |
626b901f | 168 | |
afca4d95 MK |
169 | extern bool hv_root_partition; |
170 | ||
765e33f5 MK |
171 | #if IS_ENABLED(CONFIG_HYPERV) |
172 | /* | |
173 | * Hypervisor's notion of virtual processor ID is different from | |
174 | * Linux' notion of CPU ID. This information can only be retrieved | |
175 | * in the context of the calling CPU. Setup a map for easy access | |
176 | * to this information. | |
177 | */ | |
178 | extern u32 *hv_vp_index; | |
179 | extern u32 hv_max_vp_index; | |
180 | ||
31e5e646 MK |
181 | extern u64 (*hv_read_reference_counter)(void); |
182 | ||
765e33f5 MK |
183 | /* Sentinel value for an uninitialized entry in hv_vp_index array */ |
184 | #define VP_INVAL U32_MAX | |
185 | ||
afca4d95 MK |
186 | int __init hv_common_init(void); |
187 | void __init hv_common_free(void); | |
188 | int hv_common_cpu_init(unsigned int cpu); | |
189 | int hv_common_cpu_die(unsigned int cpu); | |
190 | ||
ca48739e MK |
191 | void *hv_alloc_hyperv_page(void); |
192 | void *hv_alloc_hyperv_zeroed_page(void); | |
193 | void hv_free_hyperv_page(unsigned long addr); | |
194 | ||
765e33f5 MK |
195 | /** |
196 | * hv_cpu_number_to_vp_number() - Map CPU to VP. | |
197 | * @cpu_number: CPU number in Linux terms | |
198 | * | |
199 | * This function returns the mapping between the Linux processor | |
200 | * number and the hypervisor's virtual processor number, useful | |
201 | * in making hypercalls and such that talk about specific | |
202 | * processors. | |
203 | * | |
204 | * Return: Virtual processor number in Hyper-V terms | |
205 | */ | |
206 | static inline int hv_cpu_number_to_vp_number(int cpu_number) | |
207 | { | |
208 | return hv_vp_index[cpu_number]; | |
209 | } | |
210 | ||
7ad9bb9d WL |
211 | static inline int __cpumask_to_vpset(struct hv_vpset *vpset, |
212 | const struct cpumask *cpus, | |
d7b6ba96 | 213 | bool (*func)(int cpu)) |
765e33f5 MK |
214 | { |
215 | int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1; | |
bd19c94a | 216 | int max_vcpu_bank = hv_max_vp_index / HV_VCPUS_PER_SPARSE_BANK; |
765e33f5 | 217 | |
bd19c94a VK |
218 | /* vpset.valid_bank_mask can represent up to HV_MAX_SPARSE_VCPU_BANKS banks */ |
219 | if (max_vcpu_bank >= HV_MAX_SPARSE_VCPU_BANKS) | |
765e33f5 MK |
220 | return 0; |
221 | ||
222 | /* | |
223 | * Clear all banks up to the maximum possible bank as hv_tlb_flush_ex | |
224 | * structs are not cleared between calls, we risk flushing unneeded | |
225 | * vCPUs otherwise. | |
226 | */ | |
bd19c94a | 227 | for (vcpu_bank = 0; vcpu_bank <= max_vcpu_bank; vcpu_bank++) |
765e33f5 MK |
228 | vpset->bank_contents[vcpu_bank] = 0; |
229 | ||
230 | /* | |
231 | * Some banks may end up being empty but this is acceptable. | |
232 | */ | |
233 | for_each_cpu(cpu, cpus) { | |
d7b6ba96 | 234 | if (func && func(cpu)) |
7ad9bb9d | 235 | continue; |
765e33f5 MK |
236 | vcpu = hv_cpu_number_to_vp_number(cpu); |
237 | if (vcpu == VP_INVAL) | |
238 | return -1; | |
bd19c94a VK |
239 | vcpu_bank = vcpu / HV_VCPUS_PER_SPARSE_BANK; |
240 | vcpu_offset = vcpu % HV_VCPUS_PER_SPARSE_BANK; | |
765e33f5 MK |
241 | __set_bit(vcpu_offset, (unsigned long *) |
242 | &vpset->bank_contents[vcpu_bank]); | |
243 | if (vcpu_bank >= nr_bank) | |
244 | nr_bank = vcpu_bank + 1; | |
245 | } | |
246 | vpset->valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0); | |
247 | return nr_bank; | |
248 | } | |
249 | ||
d7b6ba96 MK |
250 | /* |
251 | * Convert a Linux cpumask into a Hyper-V VPset. In the _skip variant, | |
252 | * 'func' is called for each CPU present in cpumask. If 'func' returns | |
253 | * true, that CPU is skipped -- i.e., that CPU from cpumask is *not* | |
254 | * added to the Hyper-V VPset. If 'func' is NULL, no CPUs are | |
255 | * skipped. | |
256 | */ | |
7ad9bb9d WL |
257 | static inline int cpumask_to_vpset(struct hv_vpset *vpset, |
258 | const struct cpumask *cpus) | |
259 | { | |
d7b6ba96 | 260 | return __cpumask_to_vpset(vpset, cpus, NULL); |
7ad9bb9d WL |
261 | } |
262 | ||
d7b6ba96 MK |
263 | static inline int cpumask_to_vpset_skip(struct hv_vpset *vpset, |
264 | const struct cpumask *cpus, | |
265 | bool (*func)(int cpu)) | |
7ad9bb9d | 266 | { |
d7b6ba96 | 267 | return __cpumask_to_vpset(vpset, cpus, func); |
7ad9bb9d WL |
268 | } |
269 | ||
f3a99e76 | 270 | void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die); |
765e33f5 | 271 | bool hv_is_hyperv_initialized(void); |
b96f8653 | 272 | bool hv_is_hibernation_supported(void); |
a6c76bb0 APM |
273 | enum hv_isolation_type hv_get_isolation_type(void); |
274 | bool hv_is_isolation_supported(void); | |
0cc4f6d9 | 275 | bool hv_isolation_type_snp(void); |
20c89a55 | 276 | u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size); |
765e33f5 | 277 | void hyperv_cleanup(void); |
6dc2a774 | 278 | bool hv_query_ext_cap(u64 cap_query); |
37200078 | 279 | void hv_setup_dma_ops(struct device *dev, bool coherent); |
765e33f5 MK |
280 | #else /* CONFIG_HYPERV */ |
281 | static inline bool hv_is_hyperv_initialized(void) { return false; } | |
b96f8653 | 282 | static inline bool hv_is_hibernation_supported(void) { return false; } |
765e33f5 | 283 | static inline void hyperv_cleanup(void) {} |
0cc4f6d9 TL |
284 | static inline bool hv_is_isolation_supported(void) { return false; } |
285 | static inline enum hv_isolation_type hv_get_isolation_type(void) | |
286 | { | |
287 | return HV_ISOLATION_TYPE_NONE; | |
288 | } | |
765e33f5 MK |
289 | #endif /* CONFIG_HYPERV */ |
290 | ||
765e33f5 | 291 | #endif |