hv.c 10 KB

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