hv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 <linux/clockchips.h>
  31. #include <asm/hyperv.h>
  32. #include <asm/mshyperv.h>
  33. #include "hyperv_vmbus.h"
  34. /* The one and only */
  35. struct hv_context hv_context = {
  36. .synic_initialized = false,
  37. .hypercall_page = NULL,
  38. };
  39. #define HV_TIMER_FREQUENCY (10 * 1000 * 1000) /* 100ns period */
  40. #define HV_MAX_MAX_DELTA_TICKS 0xffffffff
  41. #define HV_MIN_DELTA_TICKS 1
  42. /*
  43. * query_hypervisor_info - Get version info of the windows hypervisor
  44. */
  45. unsigned int host_info_eax;
  46. unsigned int host_info_ebx;
  47. unsigned int host_info_ecx;
  48. unsigned int host_info_edx;
  49. static int query_hypervisor_info(void)
  50. {
  51. unsigned int eax;
  52. unsigned int ebx;
  53. unsigned int ecx;
  54. unsigned int edx;
  55. unsigned int max_leaf;
  56. unsigned int op;
  57. /*
  58. * Its assumed that this is called after confirming that Viridian
  59. * is present. Query id and revision.
  60. */
  61. eax = 0;
  62. ebx = 0;
  63. ecx = 0;
  64. edx = 0;
  65. op = HVCPUID_VENDOR_MAXFUNCTION;
  66. cpuid(op, &eax, &ebx, &ecx, &edx);
  67. max_leaf = eax;
  68. if (max_leaf >= HVCPUID_VERSION) {
  69. eax = 0;
  70. ebx = 0;
  71. ecx = 0;
  72. edx = 0;
  73. op = HVCPUID_VERSION;
  74. cpuid(op, &eax, &ebx, &ecx, &edx);
  75. host_info_eax = eax;
  76. host_info_ebx = ebx;
  77. host_info_ecx = ecx;
  78. host_info_edx = edx;
  79. }
  80. return max_leaf;
  81. }
  82. /*
  83. * do_hypercall- Invoke the specified hypercall
  84. */
  85. static u64 do_hypercall(u64 control, void *input, void *output)
  86. {
  87. #ifdef CONFIG_X86_64
  88. u64 hv_status = 0;
  89. u64 input_address = (input) ? virt_to_phys(input) : 0;
  90. u64 output_address = (output) ? virt_to_phys(output) : 0;
  91. void *hypercall_page = hv_context.hypercall_page;
  92. __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
  93. __asm__ __volatile__("call *%3" : "=a" (hv_status) :
  94. "c" (control), "d" (input_address),
  95. "m" (hypercall_page));
  96. return hv_status;
  97. #else
  98. u32 control_hi = control >> 32;
  99. u32 control_lo = control & 0xFFFFFFFF;
  100. u32 hv_status_hi = 1;
  101. u32 hv_status_lo = 1;
  102. u64 input_address = (input) ? virt_to_phys(input) : 0;
  103. u32 input_address_hi = input_address >> 32;
  104. u32 input_address_lo = input_address & 0xFFFFFFFF;
  105. u64 output_address = (output) ? virt_to_phys(output) : 0;
  106. u32 output_address_hi = output_address >> 32;
  107. u32 output_address_lo = output_address & 0xFFFFFFFF;
  108. void *hypercall_page = hv_context.hypercall_page;
  109. __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
  110. "=a"(hv_status_lo) : "d" (control_hi),
  111. "a" (control_lo), "b" (input_address_hi),
  112. "c" (input_address_lo), "D"(output_address_hi),
  113. "S"(output_address_lo), "m" (hypercall_page));
  114. return hv_status_lo | ((u64)hv_status_hi << 32);
  115. #endif /* !x86_64 */
  116. }
  117. /*
  118. * hv_init - Main initialization routine.
  119. *
  120. * This routine must be called before any other routines in here are called
  121. */
  122. int hv_init(void)
  123. {
  124. int max_leaf;
  125. union hv_x64_msr_hypercall_contents hypercall_msr;
  126. void *virtaddr = NULL;
  127. memset(hv_context.synic_event_page, 0, sizeof(void *) * NR_CPUS);
  128. memset(hv_context.synic_message_page, 0,
  129. sizeof(void *) * NR_CPUS);
  130. memset(hv_context.post_msg_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. memset(hv_context.clk_evt, 0,
  137. sizeof(void *) * NR_CPUS);
  138. max_leaf = query_hypervisor_info();
  139. /*
  140. * Write our OS ID.
  141. */
  142. hv_context.guestid = generate_guest_id(0, LINUX_VERSION_CODE, 0);
  143. wrmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
  144. /* See if the hypercall page is already set */
  145. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  146. virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
  147. if (!virtaddr)
  148. goto cleanup;
  149. hypercall_msr.enable = 1;
  150. hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
  151. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  152. /* Confirm that hypercall page did get setup. */
  153. hypercall_msr.as_uint64 = 0;
  154. rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  155. if (!hypercall_msr.enable)
  156. goto cleanup;
  157. hv_context.hypercall_page = virtaddr;
  158. return 0;
  159. cleanup:
  160. if (virtaddr) {
  161. if (hypercall_msr.enable) {
  162. hypercall_msr.as_uint64 = 0;
  163. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  164. }
  165. vfree(virtaddr);
  166. }
  167. return -ENOTSUPP;
  168. }
  169. /*
  170. * hv_cleanup - Cleanup routine.
  171. *
  172. * This routine is called normally during driver unloading or exiting.
  173. */
  174. void hv_cleanup(void)
  175. {
  176. union hv_x64_msr_hypercall_contents hypercall_msr;
  177. /* Reset our OS id */
  178. wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
  179. if (hv_context.hypercall_page) {
  180. hypercall_msr.as_uint64 = 0;
  181. wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  182. vfree(hv_context.hypercall_page);
  183. hv_context.hypercall_page = NULL;
  184. }
  185. }
  186. /*
  187. * hv_post_message - Post a message using the hypervisor message IPC.
  188. *
  189. * This involves a hypercall.
  190. */
  191. int hv_post_message(union hv_connection_id connection_id,
  192. enum hv_message_type message_type,
  193. void *payload, size_t payload_size)
  194. {
  195. struct hv_input_post_message *aligned_msg;
  196. u16 status;
  197. if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
  198. return -EMSGSIZE;
  199. aligned_msg = (struct hv_input_post_message *)
  200. hv_context.post_msg_page[get_cpu()];
  201. aligned_msg->connectionid = connection_id;
  202. aligned_msg->reserved = 0;
  203. aligned_msg->message_type = message_type;
  204. aligned_msg->payload_size = payload_size;
  205. memcpy((void *)aligned_msg->payload, payload, payload_size);
  206. status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
  207. & 0xFFFF;
  208. put_cpu();
  209. return status;
  210. }
  211. /*
  212. * hv_signal_event -
  213. * Signal an event on the specified connection using the hypervisor event IPC.
  214. *
  215. * This involves a hypercall.
  216. */
  217. u16 hv_signal_event(void *con_id)
  218. {
  219. u16 status;
  220. status = (do_hypercall(HVCALL_SIGNAL_EVENT, con_id, NULL) & 0xFFFF);
  221. return status;
  222. }
  223. static int hv_ce_set_next_event(unsigned long delta,
  224. struct clock_event_device *evt)
  225. {
  226. cycle_t current_tick;
  227. WARN_ON(evt->mode != CLOCK_EVT_MODE_ONESHOT);
  228. rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  229. current_tick += delta;
  230. wrmsrl(HV_X64_MSR_STIMER0_COUNT, current_tick);
  231. return 0;
  232. }
  233. static void hv_ce_setmode(enum clock_event_mode mode,
  234. struct clock_event_device *evt)
  235. {
  236. union hv_timer_config timer_cfg;
  237. switch (mode) {
  238. case CLOCK_EVT_MODE_PERIODIC:
  239. /* unsupported */
  240. break;
  241. case CLOCK_EVT_MODE_ONESHOT:
  242. timer_cfg.enable = 1;
  243. timer_cfg.auto_enable = 1;
  244. timer_cfg.sintx = VMBUS_MESSAGE_SINT;
  245. wrmsrl(HV_X64_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
  246. break;
  247. case CLOCK_EVT_MODE_UNUSED:
  248. case CLOCK_EVT_MODE_SHUTDOWN:
  249. wrmsrl(HV_X64_MSR_STIMER0_COUNT, 0);
  250. wrmsrl(HV_X64_MSR_STIMER0_CONFIG, 0);
  251. break;
  252. case CLOCK_EVT_MODE_RESUME:
  253. break;
  254. }
  255. }
  256. static void hv_init_clockevent_device(struct clock_event_device *dev, int cpu)
  257. {
  258. dev->name = "Hyper-V clockevent";
  259. dev->features = CLOCK_EVT_FEAT_ONESHOT;
  260. dev->cpumask = cpumask_of(cpu);
  261. dev->rating = 1000;
  262. /*
  263. * Avoid settint dev->owner = THIS_MODULE deliberately as doing so will
  264. * result in clockevents_config_and_register() taking additional
  265. * references to the hv_vmbus module making it impossible to unload.
  266. */
  267. dev->set_mode = hv_ce_setmode;
  268. dev->set_next_event = hv_ce_set_next_event;
  269. }
  270. int hv_synic_alloc(void)
  271. {
  272. size_t size = sizeof(struct tasklet_struct);
  273. size_t ced_size = sizeof(struct clock_event_device);
  274. int cpu;
  275. for_each_online_cpu(cpu) {
  276. hv_context.event_dpc[cpu] = kmalloc(size, GFP_ATOMIC);
  277. if (hv_context.event_dpc[cpu] == NULL) {
  278. pr_err("Unable to allocate event dpc\n");
  279. goto err;
  280. }
  281. tasklet_init(hv_context.event_dpc[cpu], vmbus_on_event, cpu);
  282. hv_context.clk_evt[cpu] = kzalloc(ced_size, GFP_ATOMIC);
  283. if (hv_context.clk_evt[cpu] == NULL) {
  284. pr_err("Unable to allocate clock event device\n");
  285. goto err;
  286. }
  287. hv_init_clockevent_device(hv_context.clk_evt[cpu], cpu);
  288. hv_context.synic_message_page[cpu] =
  289. (void *)get_zeroed_page(GFP_ATOMIC);
  290. if (hv_context.synic_message_page[cpu] == NULL) {
  291. pr_err("Unable to allocate SYNIC message page\n");
  292. goto err;
  293. }
  294. hv_context.synic_event_page[cpu] =
  295. (void *)get_zeroed_page(GFP_ATOMIC);
  296. if (hv_context.synic_event_page[cpu] == NULL) {
  297. pr_err("Unable to allocate SYNIC event page\n");
  298. goto err;
  299. }
  300. hv_context.post_msg_page[cpu] =
  301. (void *)get_zeroed_page(GFP_ATOMIC);
  302. if (hv_context.post_msg_page[cpu] == NULL) {
  303. pr_err("Unable to allocate post msg page\n");
  304. goto err;
  305. }
  306. }
  307. return 0;
  308. err:
  309. return -ENOMEM;
  310. }
  311. static void hv_synic_free_cpu(int cpu)
  312. {
  313. kfree(hv_context.event_dpc[cpu]);
  314. kfree(hv_context.clk_evt[cpu]);
  315. if (hv_context.synic_event_page[cpu])
  316. free_page((unsigned long)hv_context.synic_event_page[cpu]);
  317. if (hv_context.synic_message_page[cpu])
  318. free_page((unsigned long)hv_context.synic_message_page[cpu]);
  319. if (hv_context.post_msg_page[cpu])
  320. free_page((unsigned long)hv_context.post_msg_page[cpu]);
  321. }
  322. void hv_synic_free(void)
  323. {
  324. int cpu;
  325. for_each_online_cpu(cpu)
  326. hv_synic_free_cpu(cpu);
  327. }
  328. /*
  329. * hv_synic_init - Initialize the Synthethic Interrupt Controller.
  330. *
  331. * If it is already initialized by another entity (ie x2v shim), we need to
  332. * retrieve the initialized message and event pages. Otherwise, we create and
  333. * initialize the message and event pages.
  334. */
  335. void hv_synic_init(void *arg)
  336. {
  337. u64 version;
  338. union hv_synic_simp simp;
  339. union hv_synic_siefp siefp;
  340. union hv_synic_sint shared_sint;
  341. union hv_synic_scontrol sctrl;
  342. u64 vp_index;
  343. int cpu = smp_processor_id();
  344. if (!hv_context.hypercall_page)
  345. return;
  346. /* Check the version */
  347. rdmsrl(HV_X64_MSR_SVERSION, version);
  348. /* Setup the Synic's message page */
  349. rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
  350. simp.simp_enabled = 1;
  351. simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
  352. >> PAGE_SHIFT;
  353. wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
  354. /* Setup the Synic's event page */
  355. rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
  356. siefp.siefp_enabled = 1;
  357. siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
  358. >> PAGE_SHIFT;
  359. wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
  360. /* Setup the shared SINT. */
  361. rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  362. shared_sint.as_uint64 = 0;
  363. shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
  364. shared_sint.masked = false;
  365. shared_sint.auto_eoi = true;
  366. wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  367. /* Enable the global synic bit */
  368. rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
  369. sctrl.enable = 1;
  370. wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
  371. hv_context.synic_initialized = true;
  372. /*
  373. * Setup the mapping between Hyper-V's notion
  374. * of cpuid and Linux' notion of cpuid.
  375. * This array will be indexed using Linux cpuid.
  376. */
  377. rdmsrl(HV_X64_MSR_VP_INDEX, vp_index);
  378. hv_context.vp_index[cpu] = (u32)vp_index;
  379. INIT_LIST_HEAD(&hv_context.percpu_list[cpu]);
  380. /*
  381. * Register the per-cpu clockevent source.
  382. */
  383. if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE)
  384. clockevents_config_and_register(hv_context.clk_evt[cpu],
  385. HV_TIMER_FREQUENCY,
  386. HV_MIN_DELTA_TICKS,
  387. HV_MAX_MAX_DELTA_TICKS);
  388. return;
  389. }
  390. /*
  391. * hv_synic_clockevents_cleanup - Cleanup clockevent devices
  392. */
  393. void hv_synic_clockevents_cleanup(void)
  394. {
  395. int cpu;
  396. if (!(ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE))
  397. return;
  398. for_each_online_cpu(cpu)
  399. clockevents_unbind_device(hv_context.clk_evt[cpu], cpu);
  400. }
  401. /*
  402. * hv_synic_cleanup - Cleanup routine for hv_synic_init().
  403. */
  404. void hv_synic_cleanup(void *arg)
  405. {
  406. union hv_synic_sint shared_sint;
  407. union hv_synic_simp simp;
  408. union hv_synic_siefp siefp;
  409. union hv_synic_scontrol sctrl;
  410. int cpu = smp_processor_id();
  411. if (!hv_context.synic_initialized)
  412. return;
  413. /* Turn off clockevent device */
  414. if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE)
  415. hv_ce_setmode(CLOCK_EVT_MODE_SHUTDOWN,
  416. hv_context.clk_evt[cpu]);
  417. rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  418. shared_sint.masked = 1;
  419. /* Need to correctly cleanup in the case of SMP!!! */
  420. /* Disable the interrupt */
  421. wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  422. rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
  423. simp.simp_enabled = 0;
  424. simp.base_simp_gpa = 0;
  425. wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
  426. rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
  427. siefp.siefp_enabled = 0;
  428. siefp.base_siefp_gpa = 0;
  429. wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
  430. /* Disable the global synic bit */
  431. rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
  432. sctrl.enable = 0;
  433. wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
  434. hv_synic_free_cpu(cpu);
  435. }