xref: /linux-6.15/include/asm-generic/mshyperv.h (revision feba84c2)
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 derived from Hyper-V code or headers should not go in
10  * this file, but should instead go in the relevant files in include/hyperv.
11  *
12  * Copyright (C) 2019, Microsoft, Inc.
13  *
14  * Author : Michael Kelley <[email protected]>
15  */
16 
17 #ifndef _ASM_GENERIC_MSHYPERV_H
18 #define _ASM_GENERIC_MSHYPERV_H
19 
20 #include <linux/types.h>
21 #include <linux/atomic.h>
22 #include <linux/bitops.h>
23 #include <acpi/acpi_numa.h>
24 #include <linux/cpumask.h>
25 #include <linux/nmi.h>
26 #include <asm/ptrace.h>
27 #include <hyperv/hvhdk.h>
28 
29 #define VTPM_BASE_ADDRESS 0xfed40000
30 
31 enum hv_partition_type {
32 	HV_PARTITION_TYPE_GUEST,
33 	HV_PARTITION_TYPE_ROOT,
34 };
35 
36 struct ms_hyperv_info {
37 	u32 features;
38 	u32 priv_high;
39 	u32 ext_features;
40 	u32 misc_features;
41 	u32 hints;
42 	u32 nested_features;
43 	u32 max_vp_index;
44 	u32 max_lp_index;
45 	u8 vtl;
46 	union {
47 		u32 isolation_config_a;
48 		struct {
49 			u32 paravisor_present : 1;
50 			u32 reserved_a1 : 31;
51 		};
52 	};
53 	union {
54 		u32 isolation_config_b;
55 		struct {
56 			u32 cvm_type : 4;
57 			u32 reserved_b1 : 1;
58 			u32 shared_gpa_boundary_active : 1;
59 			u32 shared_gpa_boundary_bits : 6;
60 			u32 reserved_b2 : 20;
61 		};
62 	};
63 	u64 shared_gpa_boundary;
64 };
65 extern struct ms_hyperv_info ms_hyperv;
66 extern bool hv_nested;
67 extern u64 hv_current_partition_id;
68 extern enum hv_partition_type hv_curr_partition_type;
69 
70 extern void * __percpu *hyperv_pcpu_input_arg;
71 extern void * __percpu *hyperv_pcpu_output_arg;
72 
73 u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr);
74 u64 hv_do_fast_hypercall8(u16 control, u64 input8);
75 u64 hv_do_fast_hypercall16(u16 control, u64 input1, u64 input2);
76 
77 bool hv_isolation_type_snp(void);
78 bool hv_isolation_type_tdx(void);
79 
80 static inline struct hv_proximity_domain_info hv_numa_node_to_pxm_info(int node)
81 {
82 	struct hv_proximity_domain_info pxm_info = {};
83 
84 	if (node != NUMA_NO_NODE) {
85 		pxm_info.domain_id = node_to_pxm(node);
86 		pxm_info.flags.proximity_info_valid = 1;
87 		pxm_info.flags.proximity_preferred = 1;
88 	}
89 
90 	return pxm_info;
91 }
92 
93 /* Helper functions that provide a consistent pattern for checking Hyper-V hypercall status. */
94 static inline int hv_result(u64 status)
95 {
96 	return status & HV_HYPERCALL_RESULT_MASK;
97 }
98 
99 static inline bool hv_result_success(u64 status)
100 {
101 	return hv_result(status) == HV_STATUS_SUCCESS;
102 }
103 
104 static inline unsigned int hv_repcomp(u64 status)
105 {
106 	/* Bits [43:32] of status have 'Reps completed' data. */
107 	return (status & HV_HYPERCALL_REP_COMP_MASK) >>
108 			 HV_HYPERCALL_REP_COMP_OFFSET;
109 }
110 
111 /*
112  * Rep hypercalls. Callers of this functions are supposed to ensure that
113  * rep_count and varhead_size comply with Hyper-V hypercall definition.
114  */
115 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
116 				      void *input, void *output)
117 {
118 	u64 control = code;
119 	u64 status;
120 	u16 rep_comp;
121 
122 	control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
123 	control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
124 
125 	do {
126 		status = hv_do_hypercall(control, input, output);
127 		if (!hv_result_success(status))
128 			return status;
129 
130 		rep_comp = hv_repcomp(status);
131 
132 		control &= ~HV_HYPERCALL_REP_START_MASK;
133 		control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
134 
135 		touch_nmi_watchdog();
136 	} while (rep_comp < rep_count);
137 
138 	return status;
139 }
140 
141 /* Generate the guest OS identifier as described in the Hyper-V TLFS */
142 static inline u64 hv_generate_guest_id(u64 kernel_version)
143 {
144 	u64 guest_id;
145 
146 	guest_id = (((u64)HV_LINUX_VENDOR_ID) << 48);
147 	guest_id |= (kernel_version << 16);
148 
149 	return guest_id;
150 }
151 
152 /* Free the message slot and signal end-of-message if required */
153 static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
154 {
155 	/*
156 	 * On crash we're reading some other CPU's message page and we need
157 	 * to be careful: this other CPU may already had cleared the header
158 	 * and the host may already had delivered some other message there.
159 	 * In case we blindly write msg->header.message_type we're going
160 	 * to lose it. We can still lose a message of the same type but
161 	 * we count on the fact that there can only be one
162 	 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
163 	 * on crash.
164 	 */
165 	if (cmpxchg(&msg->header.message_type, old_msg_type,
166 		    HVMSG_NONE) != old_msg_type)
167 		return;
168 
169 	/*
170 	 * The cmxchg() above does an implicit memory barrier to
171 	 * ensure the write to MessageType (ie set to
172 	 * HVMSG_NONE) happens before we read the
173 	 * MessagePending and EOMing. Otherwise, the EOMing
174 	 * will not deliver any more messages since there is
175 	 * no empty slot
176 	 */
177 	if (msg->header.message_flags.msg_pending) {
178 		/*
179 		 * This will cause message queue rescan to
180 		 * possibly deliver another msg from the
181 		 * hypervisor
182 		 */
183 		hv_set_msr(HV_MSR_EOM, 0);
184 	}
185 }
186 
187 int hv_get_hypervisor_version(union hv_hypervisor_version_info *info);
188 
189 void hv_setup_vmbus_handler(void (*handler)(void));
190 void hv_remove_vmbus_handler(void);
191 void hv_setup_stimer0_handler(void (*handler)(void));
192 void hv_remove_stimer0_handler(void);
193 
194 void hv_setup_kexec_handler(void (*handler)(void));
195 void hv_remove_kexec_handler(void);
196 void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
197 void hv_remove_crash_handler(void);
198 
199 extern int vmbus_interrupt;
200 extern int vmbus_irq;
201 
202 #if IS_ENABLED(CONFIG_HYPERV)
203 /*
204  * Hypervisor's notion of virtual processor ID is different from
205  * Linux' notion of CPU ID. This information can only be retrieved
206  * in the context of the calling CPU. Setup a map for easy access
207  * to this information.
208  */
209 extern u32 *hv_vp_index;
210 extern u32 hv_max_vp_index;
211 
212 extern u64 (*hv_read_reference_counter)(void);
213 
214 /* Sentinel value for an uninitialized entry in hv_vp_index array */
215 #define VP_INVAL	U32_MAX
216 
217 int __init hv_common_init(void);
218 void __init hv_get_partition_id(void);
219 void __init hv_common_free(void);
220 void __init ms_hyperv_late_init(void);
221 int hv_common_cpu_init(unsigned int cpu);
222 int hv_common_cpu_die(unsigned int cpu);
223 void hv_identify_partition_type(void);
224 
225 void *hv_alloc_hyperv_page(void);
226 void *hv_alloc_hyperv_zeroed_page(void);
227 void hv_free_hyperv_page(void *addr);
228 
229 /**
230  * hv_cpu_number_to_vp_number() - Map CPU to VP.
231  * @cpu_number: CPU number in Linux terms
232  *
233  * This function returns the mapping between the Linux processor
234  * number and the hypervisor's virtual processor number, useful
235  * in making hypercalls and such that talk about specific
236  * processors.
237  *
238  * Return: Virtual processor number in Hyper-V terms
239  */
240 static inline int hv_cpu_number_to_vp_number(int cpu_number)
241 {
242 	return hv_vp_index[cpu_number];
243 }
244 
245 static inline int __cpumask_to_vpset(struct hv_vpset *vpset,
246 				    const struct cpumask *cpus,
247 				    bool (*func)(int cpu))
248 {
249 	int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1;
250 	int max_vcpu_bank = hv_max_vp_index / HV_VCPUS_PER_SPARSE_BANK;
251 
252 	/* vpset.valid_bank_mask can represent up to HV_MAX_SPARSE_VCPU_BANKS banks */
253 	if (max_vcpu_bank >= HV_MAX_SPARSE_VCPU_BANKS)
254 		return 0;
255 
256 	/*
257 	 * Clear all banks up to the maximum possible bank as hv_tlb_flush_ex
258 	 * structs are not cleared between calls, we risk flushing unneeded
259 	 * vCPUs otherwise.
260 	 */
261 	for (vcpu_bank = 0; vcpu_bank <= max_vcpu_bank; vcpu_bank++)
262 		vpset->bank_contents[vcpu_bank] = 0;
263 
264 	/*
265 	 * Some banks may end up being empty but this is acceptable.
266 	 */
267 	for_each_cpu(cpu, cpus) {
268 		if (func && func(cpu))
269 			continue;
270 		vcpu = hv_cpu_number_to_vp_number(cpu);
271 		if (vcpu == VP_INVAL)
272 			return -1;
273 		vcpu_bank = vcpu / HV_VCPUS_PER_SPARSE_BANK;
274 		vcpu_offset = vcpu % HV_VCPUS_PER_SPARSE_BANK;
275 		__set_bit(vcpu_offset, (unsigned long *)
276 			  &vpset->bank_contents[vcpu_bank]);
277 		if (vcpu_bank >= nr_bank)
278 			nr_bank = vcpu_bank + 1;
279 	}
280 	vpset->valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0);
281 	return nr_bank;
282 }
283 
284 /*
285  * Convert a Linux cpumask into a Hyper-V VPset. In the _skip variant,
286  * 'func' is called for each CPU present in cpumask.  If 'func' returns
287  * true, that CPU is skipped -- i.e., that CPU from cpumask is *not*
288  * added to the Hyper-V VPset. If 'func' is NULL, no CPUs are
289  * skipped.
290  */
291 static inline int cpumask_to_vpset(struct hv_vpset *vpset,
292 				    const struct cpumask *cpus)
293 {
294 	return __cpumask_to_vpset(vpset, cpus, NULL);
295 }
296 
297 static inline int cpumask_to_vpset_skip(struct hv_vpset *vpset,
298 				    const struct cpumask *cpus,
299 				    bool (*func)(int cpu))
300 {
301 	return __cpumask_to_vpset(vpset, cpus, func);
302 }
303 
304 #define _hv_status_fmt(fmt) "%s: Hyper-V status: %#x = %s: " fmt
305 #define hv_status_printk(level, status, fmt, ...) \
306 do { \
307 	u64 __status = (status); \
308 	pr_##level(_hv_status_fmt(fmt), __func__, hv_result(__status), \
309 		   hv_result_to_string(__status), ##__VA_ARGS__); \
310 } while (0)
311 #define hv_status_err(status, fmt, ...) \
312 	hv_status_printk(err, status, fmt, ##__VA_ARGS__)
313 #define hv_status_debug(status, fmt, ...) \
314 	hv_status_printk(debug, status, fmt, ##__VA_ARGS__)
315 
316 const char *hv_result_to_string(u64 hv_status);
317 int hv_result_to_errno(u64 status);
318 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
319 bool hv_is_hyperv_initialized(void);
320 bool hv_is_hibernation_supported(void);
321 enum hv_isolation_type hv_get_isolation_type(void);
322 bool hv_is_isolation_supported(void);
323 bool hv_isolation_type_snp(void);
324 u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size);
325 u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2);
326 void hyperv_cleanup(void);
327 bool hv_query_ext_cap(u64 cap_query);
328 void hv_setup_dma_ops(struct device *dev, bool coherent);
329 #else /* CONFIG_HYPERV */
330 static inline void hv_identify_partition_type(void) {}
331 static inline bool hv_is_hyperv_initialized(void) { return false; }
332 static inline bool hv_is_hibernation_supported(void) { return false; }
333 static inline void hyperv_cleanup(void) {}
334 static inline void ms_hyperv_late_init(void) {}
335 static inline bool hv_is_isolation_supported(void) { return false; }
336 static inline enum hv_isolation_type hv_get_isolation_type(void)
337 {
338 	return HV_ISOLATION_TYPE_NONE;
339 }
340 #endif /* CONFIG_HYPERV */
341 
342 #if IS_ENABLED(CONFIG_MSHV_ROOT)
343 static inline bool hv_root_partition(void)
344 {
345 	return hv_curr_partition_type == HV_PARTITION_TYPE_ROOT;
346 }
347 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
348 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
349 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
350 
351 #else /* CONFIG_MSHV_ROOT */
352 static inline bool hv_root_partition(void) { return false; }
353 static inline int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
354 {
355 	return -EOPNOTSUPP;
356 }
357 static inline int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id)
358 {
359 	return -EOPNOTSUPP;
360 }
361 static inline int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags)
362 {
363 	return -EOPNOTSUPP;
364 }
365 #endif /* CONFIG_MSHV_ROOT */
366 
367 #endif
368