hv.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2009, Microsoft Corporation.
  4. *
  5. * Authors:
  6. * Haiyang Zhang <haiyangz@microsoft.com>
  7. * Hank Janssen <hjanssen@microsoft.com>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/slab.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/hyperv.h>
  15. #include <linux/version.h>
  16. #include <linux/random.h>
  17. #include <linux/clockchips.h>
  18. #include <clocksource/hyperv_timer.h>
  19. #include <asm/mshyperv.h>
  20. #include "hyperv_vmbus.h"
  21. /* The one and only */
  22. struct hv_context hv_context;
  23. /*
  24. * hv_init - Main initialization routine.
  25. *
  26. * This routine must be called before any other routines in here are called
  27. */
  28. int hv_init(void)
  29. {
  30. hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
  31. if (!hv_context.cpu_context)
  32. return -ENOMEM;
  33. return 0;
  34. }
  35. /*
  36. * hv_post_message - Post a message using the hypervisor message IPC.
  37. *
  38. * This involves a hypercall.
  39. */
  40. int hv_post_message(union hv_connection_id connection_id,
  41. enum hv_message_type message_type,
  42. void *payload, size_t payload_size)
  43. {
  44. struct hv_input_post_message *aligned_msg;
  45. struct hv_per_cpu_context *hv_cpu;
  46. u64 status;
  47. if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
  48. return -EMSGSIZE;
  49. hv_cpu = get_cpu_ptr(hv_context.cpu_context);
  50. aligned_msg = hv_cpu->post_msg_page;
  51. aligned_msg->connectionid = connection_id;
  52. aligned_msg->reserved = 0;
  53. aligned_msg->message_type = message_type;
  54. aligned_msg->payload_size = payload_size;
  55. memcpy((void *)aligned_msg->payload, payload, payload_size);
  56. status = hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL);
  57. /* Preemption must remain disabled until after the hypercall
  58. * so some other thread can't get scheduled onto this cpu and
  59. * corrupt the per-cpu post_msg_page
  60. */
  61. put_cpu_ptr(hv_cpu);
  62. return status & 0xFFFF;
  63. }
  64. int hv_synic_alloc(void)
  65. {
  66. int cpu;
  67. struct hv_per_cpu_context *hv_cpu;
  68. /*
  69. * First, zero all per-cpu memory areas so hv_synic_free() can
  70. * detect what memory has been allocated and cleanup properly
  71. * after any failures.
  72. */
  73. for_each_present_cpu(cpu) {
  74. hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
  75. memset(hv_cpu, 0, sizeof(*hv_cpu));
  76. }
  77. hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
  78. GFP_KERNEL);
  79. if (hv_context.hv_numa_map == NULL) {
  80. pr_err("Unable to allocate NUMA map\n");
  81. goto err;
  82. }
  83. for_each_present_cpu(cpu) {
  84. hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
  85. tasklet_init(&hv_cpu->msg_dpc,
  86. vmbus_on_msg_dpc, (unsigned long) hv_cpu);
  87. hv_cpu->synic_message_page =
  88. (void *)get_zeroed_page(GFP_ATOMIC);
  89. if (hv_cpu->synic_message_page == NULL) {
  90. pr_err("Unable to allocate SYNIC message page\n");
  91. goto err;
  92. }
  93. hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
  94. if (hv_cpu->synic_event_page == NULL) {
  95. pr_err("Unable to allocate SYNIC event page\n");
  96. goto err;
  97. }
  98. hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
  99. if (hv_cpu->post_msg_page == NULL) {
  100. pr_err("Unable to allocate post msg page\n");
  101. goto err;
  102. }
  103. }
  104. return 0;
  105. err:
  106. /*
  107. * Any memory allocations that succeeded will be freed when
  108. * the caller cleans up by calling hv_synic_free()
  109. */
  110. return -ENOMEM;
  111. }
  112. void hv_synic_free(void)
  113. {
  114. int cpu;
  115. for_each_present_cpu(cpu) {
  116. struct hv_per_cpu_context *hv_cpu
  117. = per_cpu_ptr(hv_context.cpu_context, cpu);
  118. free_page((unsigned long)hv_cpu->synic_event_page);
  119. free_page((unsigned long)hv_cpu->synic_message_page);
  120. free_page((unsigned long)hv_cpu->post_msg_page);
  121. }
  122. kfree(hv_context.hv_numa_map);
  123. }
  124. /*
  125. * hv_synic_init - Initialize the Synthetic Interrupt Controller.
  126. *
  127. * If it is already initialized by another entity (ie x2v shim), we need to
  128. * retrieve the initialized message and event pages. Otherwise, we create and
  129. * initialize the message and event pages.
  130. */
  131. void hv_synic_enable_regs(unsigned int cpu)
  132. {
  133. struct hv_per_cpu_context *hv_cpu
  134. = per_cpu_ptr(hv_context.cpu_context, cpu);
  135. union hv_synic_simp simp;
  136. union hv_synic_siefp siefp;
  137. union hv_synic_sint shared_sint;
  138. union hv_synic_scontrol sctrl;
  139. /* Setup the Synic's message page */
  140. hv_get_simp(simp.as_uint64);
  141. simp.simp_enabled = 1;
  142. simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
  143. >> HV_HYP_PAGE_SHIFT;
  144. hv_set_simp(simp.as_uint64);
  145. /* Setup the Synic's event page */
  146. hv_get_siefp(siefp.as_uint64);
  147. siefp.siefp_enabled = 1;
  148. siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
  149. >> HV_HYP_PAGE_SHIFT;
  150. hv_set_siefp(siefp.as_uint64);
  151. /* Setup the shared SINT. */
  152. hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  153. shared_sint.vector = hv_get_vector();
  154. shared_sint.masked = false;
  155. shared_sint.auto_eoi = hv_recommend_using_aeoi();
  156. hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  157. /* Enable the global synic bit */
  158. hv_get_synic_state(sctrl.as_uint64);
  159. sctrl.enable = 1;
  160. hv_set_synic_state(sctrl.as_uint64);
  161. }
  162. int hv_synic_init(unsigned int cpu)
  163. {
  164. hv_synic_enable_regs(cpu);
  165. hv_stimer_legacy_init(cpu, VMBUS_MESSAGE_SINT);
  166. return 0;
  167. }
  168. /*
  169. * hv_synic_cleanup - Cleanup routine for hv_synic_init().
  170. */
  171. void hv_synic_disable_regs(unsigned int cpu)
  172. {
  173. union hv_synic_sint shared_sint;
  174. union hv_synic_simp simp;
  175. union hv_synic_siefp siefp;
  176. union hv_synic_scontrol sctrl;
  177. hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  178. shared_sint.masked = 1;
  179. /* Need to correctly cleanup in the case of SMP!!! */
  180. /* Disable the interrupt */
  181. hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  182. hv_get_simp(simp.as_uint64);
  183. simp.simp_enabled = 0;
  184. simp.base_simp_gpa = 0;
  185. hv_set_simp(simp.as_uint64);
  186. hv_get_siefp(siefp.as_uint64);
  187. siefp.siefp_enabled = 0;
  188. siefp.base_siefp_gpa = 0;
  189. hv_set_siefp(siefp.as_uint64);
  190. /* Disable the global synic bit */
  191. hv_get_synic_state(sctrl.as_uint64);
  192. sctrl.enable = 0;
  193. hv_set_synic_state(sctrl.as_uint64);
  194. }
  195. int hv_synic_cleanup(unsigned int cpu)
  196. {
  197. struct vmbus_channel *channel, *sc;
  198. bool channel_found = false;
  199. /*
  200. * Hyper-V does not provide a way to change the connect CPU once
  201. * it is set; we must prevent the connect CPU from going offline
  202. * while the VM is running normally. But in the panic or kexec()
  203. * path where the vmbus is already disconnected, the CPU must be
  204. * allowed to shut down.
  205. */
  206. if (cpu == VMBUS_CONNECT_CPU &&
  207. vmbus_connection.conn_state == CONNECTED)
  208. return -EBUSY;
  209. /*
  210. * Search for channels which are bound to the CPU we're about to
  211. * cleanup. In case we find one and vmbus is still connected, we
  212. * fail; this will effectively prevent CPU offlining.
  213. *
  214. * TODO: Re-bind the channels to different CPUs.
  215. */
  216. mutex_lock(&vmbus_connection.channel_mutex);
  217. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  218. if (channel->target_cpu == cpu) {
  219. channel_found = true;
  220. break;
  221. }
  222. list_for_each_entry(sc, &channel->sc_list, sc_list) {
  223. if (sc->target_cpu == cpu) {
  224. channel_found = true;
  225. break;
  226. }
  227. }
  228. if (channel_found)
  229. break;
  230. }
  231. mutex_unlock(&vmbus_connection.channel_mutex);
  232. if (channel_found && vmbus_connection.conn_state == CONNECTED)
  233. return -EBUSY;
  234. hv_stimer_legacy_cleanup(cpu);
  235. hv_synic_disable_regs(cpu);
  236. return 0;
  237. }