File Index
Files and directories
Sign in
FILE

mshyperv.h

File preview with syntax highlighting.

linux-6.19.14/include/asm-generic/mshyperv.h C
  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 <mikelley@microsoft.com>
 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
 31enum hv_partition_type {
 32	HV_PARTITION_TYPE_GUEST,
 33	HV_PARTITION_TYPE_ROOT,
 34	HV_PARTITION_TYPE_L1VH,
 35};
 36
 37struct ms_hyperv_info {
 38	u32 features;
 39	u32 priv_high;
 40	u32 ext_features;
 41	u32 misc_features;
 42	u32 hints;
 43	u32 nested_features;
 44	u32 max_vp_index;
 45	u32 max_lp_index;
 46	u8 vtl;
 47	union {
 48		u32 isolation_config_a;
 49		struct {
 50			u32 paravisor_present : 1;
 51			u32 reserved_a1 : 31;
 52		};
 53	};
 54	union {
 55		u32 isolation_config_b;
 56		struct {
 57			u32 cvm_type : 4;
 58			u32 reserved_b1 : 1;
 59			u32 shared_gpa_boundary_active : 1;
 60			u32 shared_gpa_boundary_bits : 6;
 61			u32 reserved_b2 : 20;
 62		};
 63	};
 64	u64 shared_gpa_boundary;
 65	bool msi_ext_dest_id;
 66	bool confidential_vmbus_available;
 67};
 68extern struct ms_hyperv_info ms_hyperv;
 69extern bool hv_nested;
 70extern u64 hv_current_partition_id;
 71extern enum hv_partition_type hv_curr_partition_type;
 72
 73extern void * __percpu *hyperv_pcpu_input_arg;
 74extern void * __percpu *hyperv_pcpu_output_arg;
 75
 76u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr);
 77u64 hv_do_fast_hypercall8(u16 control, u64 input8);
 78u64 hv_do_fast_hypercall16(u16 control, u64 input1, u64 input2);
 79
 80bool hv_isolation_type_snp(void);
 81bool hv_isolation_type_tdx(void);
 82
 83/*
 84 * On architectures where Hyper-V doesn't support AEOI (e.g., ARM64),
 85 * it doesn't provide a recommendation flag and AEOI must be disabled.
 86 */
 87static inline bool hv_recommend_using_aeoi(void)
 88{
 89#ifdef HV_DEPRECATING_AEOI_RECOMMENDED
 90	return !(ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED);
 91#else
 92	return false;
 93#endif
 94}
 95
 96static inline struct hv_proximity_domain_info hv_numa_node_to_pxm_info(int node)
 97{
 98	struct hv_proximity_domain_info pxm_info = {};
 99
100	if (node != NUMA_NO_NODE) {
101		pxm_info.domain_id = node_to_pxm(node);
102		pxm_info.flags.proximity_info_valid = 1;
103		pxm_info.flags.proximity_preferred = 1;
104	}
105
106	return pxm_info;
107}
108
109/* Helper functions that provide a consistent pattern for checking Hyper-V hypercall status. */
110static inline int hv_result(u64 status)
111{
112	return status & HV_HYPERCALL_RESULT_MASK;
113}
114
115static inline bool hv_result_success(u64 status)
116{
117	return hv_result(status) == HV_STATUS_SUCCESS;
118}
119
120static inline unsigned int hv_repcomp(u64 status)
121{
122	/* Bits [43:32] of status have 'Reps completed' data. */
123	return (status & HV_HYPERCALL_REP_COMP_MASK) >>
124			 HV_HYPERCALL_REP_COMP_OFFSET;
125}
126
127/*
128 * Rep hypercalls. Callers of this functions are supposed to ensure that
129 * rep_count, varhead_size, and rep_start comply with Hyper-V hypercall
130 * definition.
131 */
132static inline u64 hv_do_rep_hypercall_ex(u16 code, u16 rep_count,
133					 u16 varhead_size, u16 rep_start,
134					 void *input, void *output)
135{
136	u64 control = code;
137	u64 status;
138	u16 rep_comp;
139
140	control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
141	control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
142	control |= (u64)rep_start << HV_HYPERCALL_REP_START_OFFSET;
143
144	do {
145		status = hv_do_hypercall(control, input, output);
146		if (!hv_result_success(status))
147			return status;
148
149		rep_comp = hv_repcomp(status);
150
151		control &= ~HV_HYPERCALL_REP_START_MASK;
152		control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
153
154		touch_nmi_watchdog();
155	} while (rep_comp < rep_count);
156
157	return status;
158}
159
160/* For the typical case where rep_start is 0 */
161static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
162				      void *input, void *output)
163{
164	return hv_do_rep_hypercall_ex(code, rep_count, varhead_size, 0,
165				      input, output);
166}
167
168/* Generate the guest OS identifier as described in the Hyper-V TLFS */
169static inline u64 hv_generate_guest_id(u64 kernel_version)
170{
171	u64 guest_id;
172
173	guest_id = (((u64)HV_LINUX_VENDOR_ID) << 48);
174	guest_id |= (kernel_version << 16);
175
176	return guest_id;
177}
178
179int hv_get_hypervisor_version(union hv_hypervisor_version_info *info);
180
181void hv_setup_vmbus_handler(void (*handler)(void));
182void hv_remove_vmbus_handler(void);
183void hv_setup_stimer0_handler(void (*handler)(void));
184void hv_remove_stimer0_handler(void);
185
186void hv_setup_kexec_handler(void (*handler)(void));
187void hv_remove_kexec_handler(void);
188void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
189void hv_remove_crash_handler(void);
190void hv_setup_mshv_handler(void (*handler)(void));
191
192#if IS_ENABLED(CONFIG_HYPERV)
193/*
194 * Hypervisor's notion of virtual processor ID is different from
195 * Linux' notion of CPU ID. This information can only be retrieved
196 * in the context of the calling CPU. Setup a map for easy access
197 * to this information.
198 */
199extern u32 *hv_vp_index;
200extern u32 hv_max_vp_index;
201
202extern u64 (*hv_read_reference_counter)(void);
203
204/* Sentinel value for an uninitialized entry in hv_vp_index array */
205#define VP_INVAL	U32_MAX
206
207int __init hv_common_init(void);
208void __init hv_get_partition_id(void);
209void __init hv_common_free(void);
210void __init ms_hyperv_late_init(void);
211int hv_common_cpu_init(unsigned int cpu);
212int hv_common_cpu_die(unsigned int cpu);
213void hv_identify_partition_type(void);
214
215/**
216 * hv_cpu_number_to_vp_number() - Map CPU to VP.
217 * @cpu_number: CPU number in Linux terms
218 *
219 * This function returns the mapping between the Linux processor
220 * number and the hypervisor's virtual processor number, useful
221 * in making hypercalls and such that talk about specific
222 * processors.
223 *
224 * Return: Virtual processor number in Hyper-V terms
225 */
226static inline int hv_cpu_number_to_vp_number(int cpu_number)
227{
228	return hv_vp_index[cpu_number];
229}
230
231static inline int __cpumask_to_vpset(struct hv_vpset *vpset,
232				    const struct cpumask *cpus,
233				    bool (*func)(int cpu))
234{
235	int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1;
236	int max_vcpu_bank = hv_max_vp_index / HV_VCPUS_PER_SPARSE_BANK;
237
238	/* vpset.valid_bank_mask can represent up to HV_MAX_SPARSE_VCPU_BANKS banks */
239	if (max_vcpu_bank >= HV_MAX_SPARSE_VCPU_BANKS)
240		return 0;
241
242	/*
243	 * Clear all banks up to the maximum possible bank as hv_tlb_flush_ex
244	 * structs are not cleared between calls, we risk flushing unneeded
245	 * vCPUs otherwise.
246	 */
247	for (vcpu_bank = 0; vcpu_bank <= max_vcpu_bank; vcpu_bank++)
248		vpset->bank_contents[vcpu_bank] = 0;
249
250	/*
251	 * Some banks may end up being empty but this is acceptable.
252	 */
253	for_each_cpu(cpu, cpus) {
254		if (func && func(cpu))
255			continue;
256		vcpu = hv_cpu_number_to_vp_number(cpu);
257		if (vcpu == VP_INVAL)
258			return -1;
259		vcpu_bank = vcpu / HV_VCPUS_PER_SPARSE_BANK;
260		vcpu_offset = vcpu % HV_VCPUS_PER_SPARSE_BANK;
261		__set_bit(vcpu_offset, (unsigned long *)
262			  &vpset->bank_contents[vcpu_bank]);
263		if (vcpu_bank >= nr_bank)
264			nr_bank = vcpu_bank + 1;
265	}
266	vpset->valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0);
267	return nr_bank;
268}
269
270/*
271 * Convert a Linux cpumask into a Hyper-V VPset. In the _skip variant,
272 * 'func' is called for each CPU present in cpumask.  If 'func' returns
273 * true, that CPU is skipped -- i.e., that CPU from cpumask is *not*
274 * added to the Hyper-V VPset. If 'func' is NULL, no CPUs are
275 * skipped.
276 */
277static inline int cpumask_to_vpset(struct hv_vpset *vpset,
278				    const struct cpumask *cpus)
279{
280	return __cpumask_to_vpset(vpset, cpus, NULL);
281}
282
283static inline int cpumask_to_vpset_skip(struct hv_vpset *vpset,
284				    const struct cpumask *cpus,
285				    bool (*func)(int cpu))
286{
287	return __cpumask_to_vpset(vpset, cpus, func);
288}
289
290#define _hv_status_fmt(fmt) "%s: Hyper-V status: %#x = %s: " fmt
291#define hv_status_printk(level, status, fmt, ...) \
292do { \
293	u64 __status = (status); \
294	pr_##level(_hv_status_fmt(fmt), __func__, hv_result(__status), \
295		   hv_result_to_string(__status), ##__VA_ARGS__); \
296} while (0)
297#define hv_status_err(status, fmt, ...) \
298	hv_status_printk(err, status, fmt, ##__VA_ARGS__)
299#define hv_status_debug(status, fmt, ...) \
300	hv_status_printk(debug, status, fmt, ##__VA_ARGS__)
301
302const char *hv_result_to_string(u64 hv_status);
303int hv_result_to_errno(u64 status);
304void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
305bool hv_is_hyperv_initialized(void);
306bool hv_is_hibernation_supported(void);
307enum hv_isolation_type hv_get_isolation_type(void);
308bool hv_is_isolation_supported(void);
309bool hv_isolation_type_snp(void);
310u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size);
311u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2);
312void hv_enable_coco_interrupt(unsigned int cpu, unsigned int vector, bool set);
313void hv_para_set_sint_proxy(bool enable);
314u64 hv_para_get_synic_register(unsigned int reg);
315void hv_para_set_synic_register(unsigned int reg, u64 val);
316void hyperv_cleanup(void);
317bool hv_query_ext_cap(u64 cap_query);
318void hv_setup_dma_ops(struct device *dev, bool coherent);
319#else /* CONFIG_HYPERV */
320static inline void hv_identify_partition_type(void) {}
321static inline bool hv_is_hyperv_initialized(void) { return false; }
322static inline bool hv_is_hibernation_supported(void) { return false; }
323static inline void hyperv_cleanup(void) {}
324static inline void ms_hyperv_late_init(void) {}
325static inline bool hv_is_isolation_supported(void) { return false; }
326static inline enum hv_isolation_type hv_get_isolation_type(void)
327{
328	return HV_ISOLATION_TYPE_NONE;
329}
330#endif /* CONFIG_HYPERV */
331
332#if IS_ENABLED(CONFIG_MSHV_ROOT)
333static inline bool hv_root_partition(void)
334{
335	return hv_curr_partition_type == HV_PARTITION_TYPE_ROOT;
336}
337static inline bool hv_l1vh_partition(void)
338{
339	return hv_curr_partition_type == HV_PARTITION_TYPE_L1VH;
340}
341static inline bool hv_parent_partition(void)
342{
343	return hv_root_partition() || hv_l1vh_partition();
344}
345int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
346int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
347int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
348
349#else /* CONFIG_MSHV_ROOT */
350static inline bool hv_root_partition(void) { return false; }
351static inline bool hv_l1vh_partition(void) { return false; }
352static inline bool hv_parent_partition(void) { return false; }
353static inline int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
354{
355	return -EOPNOTSUPP;
356}
357static inline int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id)
358{
359	return -EOPNOTSUPP;
360}
361static 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#if IS_ENABLED(CONFIG_HYPERV_VTL_MODE)
368u8 __init get_vtl(void);
369#else
370static inline u8 get_vtl(void) { return 0; }
371#endif
372
373#endif
~/ Read-only file preview.