hv.c 11 KB

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