hv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Copyright (c) 2009, Microsoft Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Authors:
  18. * Haiyang Zhang <haiyangz@microsoft.com>
  19. * Hank Janssen <hjanssen@microsoft.com>
  20. *
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/slab.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/hyperv.h>
  28. #include <linux/version.h>
  29. #include <asm/hyperv.h>
  30. #include "hyperv_vmbus.h"
  31. /* The one and only */
  32. struct hv_context hv_context = {
  33. .synic_initialized = false,
  34. .hypercall_page = NULL,
  35. .signal_event_param = NULL,
  36. .signal_event_buffer = NULL,
  37. };
  38. /*
  39. * query_hypervisor_info - Get version info of the windows hypervisor
  40. */
  41. static int query_hypervisor_info(void)
  42. {
  43. unsigned int eax;
  44. unsigned int ebx;
  45. unsigned int ecx;
  46. unsigned int edx;
  47. unsigned int max_leaf;
  48. unsigned int op;
  49. /*
  50. * Its assumed that this is called after confirming that Viridian
  51. * is present. Query id and revision.
  52. */
  53. eax = 0;
  54. ebx = 0;
  55. ecx = 0;
  56. edx = 0;
  57. op = HVCPUID_VENDOR_MAXFUNCTION;
  58. cpuid(op, &eax, &ebx, &ecx, &edx);
  59. max_leaf = eax;
  60. if (max_leaf >= HVCPUID_VERSION) {
  61. eax = 0;
  62. ebx = 0;
  63. ecx = 0;
  64. edx = 0;
  65. op = HVCPUID_VERSION;
  66. cpuid(op, &eax, &ebx, &ecx, &edx);
  67. pr_info("Hyper-V Host OS Build:%d-%d.%d-%d-%d.%d\n",
  68. eax,
  69. ebx >> 16,
  70. ebx & 0xFFFF,
  71. ecx,
  72. edx >> 24,
  73. edx & 0xFFFFFF);
  74. }
  75. return max_leaf;
  76. }
  77. /*
  78. * do_hypercall- Invoke the specified hypercall
  79. */
  80. static u64 do_hypercall(u64 control, void *input, void *output)
  81. {
  82. #ifdef CONFIG_X86_64
  83. u64 hv_status = 0;
  84. u64 input_address = (input) ? virt_to_phys(input) : 0;
  85. u64 output_address = (output) ? virt_to_phys(output) : 0;
  86. void *hypercall_page = hv_context.hypercall_page;
  87. __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
  88. __asm__ __volatile__("call *%3" : "=a" (hv_status) :
  89. "c" (control), "d" (input_address),
  90. "m" (hypercall_page));
  91. return hv_status;
  92. #else
  93. u32 control_hi = control >> 32;
  94. u32 control_lo = control & 0xFFFFFFFF;
  95. u32 hv_status_hi = 1;
  96. u32 hv_status_lo = 1;
  97. u64 input_address = (input) ? virt_to_phys(input) : 0;
  98. u32 input_address_hi = input_address >> 32;
  99. u32 input_address_lo = input_address & 0xFFFFFFFF;
  100. u64 output_address = (output) ? virt_to_phys(output) : 0;
  101. u32 output_address_hi = output_address >> 32;
  102. u32 output_address_lo = output_address & 0xFFFFFFFF;
  103. void *hypercall_page = hv_context.hypercall_page;
  104. __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
  105. "=a"(hv_status_lo) : "d" (control_hi),
  106. "a" (control_lo), "b" (input_address_hi),
  107. "c" (input_address_lo), "D"(output_address_hi),
  108. "S"(output_address_lo), "m" (hypercall_page));
  109. return hv_status_lo | ((u64)hv_status_hi << 32);
  110. #endif /* !x86_64 */
  111. }
  112. /*
  113. * hv_init - Main initialization routine.
  114. *
  115. * This routine must be called before any other routines in here are called
  116. */
  117. int hv_init(void)
  118. {
  119. int max_leaf;
  120. union hv_x64_msr_hypercall_contents hypercall_msr;
  121. void *virtaddr = NULL;
  122. memset(hv_context.synic_event_page, 0, sizeof(void *) * NR_CPUS);
  123. memset(hv_context.synic_message_page, 0,
  124. sizeof(void *) * NR_CPUS);
  125. max_leaf = query_hypervisor_info();
  126. /*
  127. * Write our OS ID.
  128. */
  129. hv_context.guestid = generate_guest_id(0, LINUX_VERSION_CODE, 0);
  130. wrmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
  131. /* See if the hypercall page is already set */
  132. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  133. virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
  134. if (!virtaddr)
  135. goto cleanup;
  136. hypercall_msr.enable = 1;
  137. hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
  138. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  139. /* Confirm that hypercall page did get setup. */
  140. hypercall_msr.as_uint64 = 0;
  141. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  142. if (!hypercall_msr.enable)
  143. goto cleanup;
  144. hv_context.hypercall_page = virtaddr;
  145. /* Setup the global signal event param for the signal event hypercall */
  146. hv_context.signal_event_buffer =
  147. kmalloc(sizeof(struct hv_input_signal_event_buffer),
  148. GFP_KERNEL);
  149. if (!hv_context.signal_event_buffer)
  150. goto cleanup;
  151. hv_context.signal_event_param =
  152. (struct hv_input_signal_event *)
  153. (ALIGN((unsigned long)
  154. hv_context.signal_event_buffer,
  155. HV_HYPERCALL_PARAM_ALIGN));
  156. hv_context.signal_event_param->connectionid.asu32 = 0;
  157. hv_context.signal_event_param->connectionid.u.id =
  158. VMBUS_EVENT_CONNECTION_ID;
  159. hv_context.signal_event_param->flag_number = 0;
  160. hv_context.signal_event_param->rsvdz = 0;
  161. return 0;
  162. cleanup:
  163. if (virtaddr) {
  164. if (hypercall_msr.enable) {
  165. hypercall_msr.as_uint64 = 0;
  166. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  167. }
  168. vfree(virtaddr);
  169. }
  170. return -ENOTSUPP;
  171. }
  172. /*
  173. * hv_cleanup - Cleanup routine.
  174. *
  175. * This routine is called normally during driver unloading or exiting.
  176. */
  177. void hv_cleanup(void)
  178. {
  179. union hv_x64_msr_hypercall_contents hypercall_msr;
  180. /* Reset our OS id */
  181. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  182. kfree(hv_context.signal_event_buffer);
  183. hv_context.signal_event_buffer = NULL;
  184. hv_context.signal_event_param = NULL;
  185. if (hv_context.hypercall_page) {
  186. hypercall_msr.as_uint64 = 0;
  187. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  188. vfree(hv_context.hypercall_page);
  189. hv_context.hypercall_page = NULL;
  190. }
  191. }
  192. /*
  193. * hv_post_message - Post a message using the hypervisor message IPC.
  194. *
  195. * This involves a hypercall.
  196. */
  197. int hv_post_message(union hv_connection_id connection_id,
  198. enum hv_message_type message_type,
  199. void *payload, size_t payload_size)
  200. {
  201. struct aligned_input {
  202. u64 alignment8;
  203. struct hv_input_post_message msg;
  204. };
  205. struct hv_input_post_message *aligned_msg;
  206. u16 status;
  207. unsigned long addr;
  208. if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
  209. return -EMSGSIZE;
  210. addr = (unsigned long)kmalloc(sizeof(struct aligned_input), GFP_ATOMIC);
  211. if (!addr)
  212. return -ENOMEM;
  213. aligned_msg = (struct hv_input_post_message *)
  214. (ALIGN(addr, HV_HYPERCALL_PARAM_ALIGN));
  215. aligned_msg->connectionid = connection_id;
  216. aligned_msg->message_type = message_type;
  217. aligned_msg->payload_size = payload_size;
  218. memcpy((void *)aligned_msg->payload, payload, payload_size);
  219. status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
  220. & 0xFFFF;
  221. kfree((void *)addr);
  222. return status;
  223. }
  224. /*
  225. * hv_signal_event -
  226. * Signal an event on the specified connection using the hypervisor event IPC.
  227. *
  228. * This involves a hypercall.
  229. */
  230. u16 hv_signal_event(void)
  231. {
  232. u16 status;
  233. status = do_hypercall(HVCALL_SIGNAL_EVENT,
  234. hv_context.signal_event_param,
  235. NULL) & 0xFFFF;
  236. return status;
  237. }
  238. /*
  239. * hv_synic_init - Initialize the Synthethic Interrupt Controller.
  240. *
  241. * If it is already initialized by another entity (ie x2v shim), we need to
  242. * retrieve the initialized message and event pages. Otherwise, we create and
  243. * initialize the message and event pages.
  244. */
  245. void hv_synic_init(void *irqarg)
  246. {
  247. u64 version;
  248. union hv_synic_simp simp;
  249. union hv_synic_siefp siefp;
  250. union hv_synic_sint shared_sint;
  251. union hv_synic_scontrol sctrl;
  252. u32 irq_vector = *((u32 *)(irqarg));
  253. int cpu = smp_processor_id();
  254. if (!hv_context.hypercall_page)
  255. return;
  256. /* Check the version */
  257. rdmsrl(HV_X64_MSR_SVERSION, version);
  258. hv_context.synic_message_page[cpu] =
  259. (void *)get_zeroed_page(GFP_ATOMIC);
  260. if (hv_context.synic_message_page[cpu] == NULL) {
  261. pr_err("Unable to allocate SYNIC message page\n");
  262. goto cleanup;
  263. }
  264. hv_context.synic_event_page[cpu] =
  265. (void *)get_zeroed_page(GFP_ATOMIC);
  266. if (hv_context.synic_event_page[cpu] == NULL) {
  267. pr_err("Unable to allocate SYNIC event page\n");
  268. goto cleanup;
  269. }
  270. /* Setup the Synic's message page */
  271. rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
  272. simp.simp_enabled = 1;
  273. simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
  274. >> PAGE_SHIFT;
  275. wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
  276. /* Setup the Synic's event page */
  277. rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
  278. siefp.siefp_enabled = 1;
  279. siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
  280. >> PAGE_SHIFT;
  281. wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
  282. /* Setup the shared SINT. */
  283. rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  284. shared_sint.as_uint64 = 0;
  285. shared_sint.vector = irq_vector; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
  286. shared_sint.masked = false;
  287. shared_sint.auto_eoi = false;
  288. wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  289. /* Enable the global synic bit */
  290. rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
  291. sctrl.enable = 1;
  292. wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
  293. hv_context.synic_initialized = true;
  294. return;
  295. cleanup:
  296. if (hv_context.synic_event_page[cpu])
  297. free_page((unsigned long)hv_context.synic_event_page[cpu]);
  298. if (hv_context.synic_message_page[cpu])
  299. free_page((unsigned long)hv_context.synic_message_page[cpu]);
  300. return;
  301. }
  302. /*
  303. * hv_synic_cleanup - Cleanup routine for hv_synic_init().
  304. */
  305. void hv_synic_cleanup(void *arg)
  306. {
  307. union hv_synic_sint shared_sint;
  308. union hv_synic_simp simp;
  309. union hv_synic_siefp siefp;
  310. int cpu = smp_processor_id();
  311. if (!hv_context.synic_initialized)
  312. return;
  313. rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  314. shared_sint.masked = 1;
  315. /* Need to correctly cleanup in the case of SMP!!! */
  316. /* Disable the interrupt */
  317. wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  318. rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
  319. simp.simp_enabled = 0;
  320. simp.base_simp_gpa = 0;
  321. wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
  322. rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
  323. siefp.siefp_enabled = 0;
  324. siefp.base_siefp_gpa = 0;
  325. wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
  326. free_page((unsigned long)hv_context.synic_message_page[cpu]);
  327. free_page((unsigned long)hv_context.synic_event_page[cpu]);
  328. }