mshyperv.h 11 KB

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