mshyperv.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 specified in the Hyper-V Top Level Functional
  9. * Spec (TLFS) should not go in this file, but should instead go in
  10. * hyperv-tlfs.h.
  11. *
  12. * Copyright (C) 2019, Microsoft, Inc.
  13. *
  14. * Author : Michael Kelley <mikelley@microsoft.com>
  15. */
  16. #ifndef _ASM_GENERIC_MSHYPERV_H
  17. #define _ASM_GENERIC_MSHYPERV_H
  18. #include <linux/types.h>
  19. #include <linux/atomic.h>
  20. #include <linux/bitops.h>
  21. #include <linux/cpumask.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/hyperv-tlfs.h>
  24. struct ms_hyperv_info {
  25. u32 features;
  26. u32 misc_features;
  27. u32 hints;
  28. u32 nested_features;
  29. u32 max_vp_index;
  30. u32 max_lp_index;
  31. };
  32. extern struct ms_hyperv_info ms_hyperv;
  33. extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr);
  34. extern u64 hv_do_fast_hypercall8(u16 control, u64 input8);
  35. /* Generate the guest OS identifier as described in the Hyper-V TLFS */
  36. static inline __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version,
  37. __u64 d_info2)
  38. {
  39. __u64 guest_id = 0;
  40. guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48);
  41. guest_id |= (d_info1 << 48);
  42. guest_id |= (kernel_version << 16);
  43. guest_id |= d_info2;
  44. return guest_id;
  45. }
  46. /* Free the message slot and signal end-of-message if required */
  47. static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
  48. {
  49. /*
  50. * On crash we're reading some other CPU's message page and we need
  51. * to be careful: this other CPU may already had cleared the header
  52. * and the host may already had delivered some other message there.
  53. * In case we blindly write msg->header.message_type we're going
  54. * to lose it. We can still lose a message of the same type but
  55. * we count on the fact that there can only be one
  56. * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
  57. * on crash.
  58. */
  59. if (cmpxchg(&msg->header.message_type, old_msg_type,
  60. HVMSG_NONE) != old_msg_type)
  61. return;
  62. /*
  63. * The cmxchg() above does an implicit memory barrier to
  64. * ensure the write to MessageType (ie set to
  65. * HVMSG_NONE) happens before we read the
  66. * MessagePending and EOMing. Otherwise, the EOMing
  67. * will not deliver any more messages since there is
  68. * no empty slot
  69. */
  70. if (msg->header.message_flags.msg_pending) {
  71. /*
  72. * This will cause message queue rescan to
  73. * possibly deliver another msg from the
  74. * hypervisor
  75. */
  76. hv_signal_eom();
  77. }
  78. }
  79. int hv_setup_vmbus_irq(int irq, void (*handler)(void));
  80. void hv_remove_vmbus_irq(void);
  81. void hv_enable_vmbus_irq(void);
  82. void hv_disable_vmbus_irq(void);
  83. void hv_setup_kexec_handler(void (*handler)(void));
  84. void hv_remove_kexec_handler(void);
  85. void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
  86. void hv_remove_crash_handler(void);
  87. extern int vmbus_interrupt;
  88. #if IS_ENABLED(CONFIG_HYPERV)
  89. /*
  90. * Hypervisor's notion of virtual processor ID is different from
  91. * Linux' notion of CPU ID. This information can only be retrieved
  92. * in the context of the calling CPU. Setup a map for easy access
  93. * to this information.
  94. */
  95. extern u32 *hv_vp_index;
  96. extern u32 hv_max_vp_index;
  97. /* Sentinel value for an uninitialized entry in hv_vp_index array */
  98. #define VP_INVAL U32_MAX
  99. /**
  100. * hv_cpu_number_to_vp_number() - Map CPU to VP.
  101. * @cpu_number: CPU number in Linux terms
  102. *
  103. * This function returns the mapping between the Linux processor
  104. * number and the hypervisor's virtual processor number, useful
  105. * in making hypercalls and such that talk about specific
  106. * processors.
  107. *
  108. * Return: Virtual processor number in Hyper-V terms
  109. */
  110. static inline int hv_cpu_number_to_vp_number(int cpu_number)
  111. {
  112. return hv_vp_index[cpu_number];
  113. }
  114. static inline int cpumask_to_vpset(struct hv_vpset *vpset,
  115. const struct cpumask *cpus)
  116. {
  117. int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1;
  118. /* valid_bank_mask can represent up to 64 banks */
  119. if (hv_max_vp_index / 64 >= 64)
  120. return 0;
  121. /*
  122. * Clear all banks up to the maximum possible bank as hv_tlb_flush_ex
  123. * structs are not cleared between calls, we risk flushing unneeded
  124. * vCPUs otherwise.
  125. */
  126. for (vcpu_bank = 0; vcpu_bank <= hv_max_vp_index / 64; vcpu_bank++)
  127. vpset->bank_contents[vcpu_bank] = 0;
  128. /*
  129. * Some banks may end up being empty but this is acceptable.
  130. */
  131. for_each_cpu(cpu, cpus) {
  132. vcpu = hv_cpu_number_to_vp_number(cpu);
  133. if (vcpu == VP_INVAL)
  134. return -1;
  135. vcpu_bank = vcpu / 64;
  136. vcpu_offset = vcpu % 64;
  137. __set_bit(vcpu_offset, (unsigned long *)
  138. &vpset->bank_contents[vcpu_bank]);
  139. if (vcpu_bank >= nr_bank)
  140. nr_bank = vcpu_bank + 1;
  141. }
  142. vpset->valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0);
  143. return nr_bank;
  144. }
  145. void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
  146. void hyperv_report_panic_msg(phys_addr_t pa, size_t size);
  147. bool hv_is_hyperv_initialized(void);
  148. bool hv_is_hibernation_supported(void);
  149. void hyperv_cleanup(void);
  150. #else /* CONFIG_HYPERV */
  151. static inline bool hv_is_hyperv_initialized(void) { return false; }
  152. static inline bool hv_is_hibernation_supported(void) { return false; }
  153. static inline void hyperv_cleanup(void) {}
  154. #endif /* CONFIG_HYPERV */
  155. #if IS_ENABLED(CONFIG_HYPERV)
  156. extern int hv_setup_stimer0_irq(int *irq, int *vector, void (*handler)(void));
  157. extern void hv_remove_stimer0_irq(int irq);
  158. #endif
  159. #endif