hv.c 11 KB

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