hv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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/random.h>
  30. #include <linux/clockchips.h>
  31. #include <asm/mshyperv.h>
  32. #include "hyperv_vmbus.h"
  33. /* The one and only */
  34. struct hv_context hv_context = {
  35. .synic_initialized = false,
  36. };
  37. /*
  38. * If false, we're using the old mechanism for stimer0 interrupts
  39. * where it sends a VMbus message when it expires. The old
  40. * mechanism is used when running on older versions of Hyper-V
  41. * that don't support Direct Mode. While Hyper-V provides
  42. * four stimer's per CPU, Linux uses only stimer0.
  43. */
  44. static bool direct_mode_enabled;
  45. static int stimer0_irq;
  46. static int stimer0_vector;
  47. #define HV_TIMER_FREQUENCY (10 * 1000 * 1000) /* 100ns period */
  48. #define HV_MAX_MAX_DELTA_TICKS 0xffffffff
  49. #define HV_MIN_DELTA_TICKS 1
  50. /*
  51. * hv_init - Main initialization routine.
  52. *
  53. * This routine must be called before any other routines in here are called
  54. */
  55. int hv_init(void)
  56. {
  57. hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
  58. if (!hv_context.cpu_context)
  59. return -ENOMEM;
  60. direct_mode_enabled = ms_hyperv.misc_features &
  61. HV_STIMER_DIRECT_MODE_AVAILABLE;
  62. return 0;
  63. }
  64. /*
  65. * hv_post_message - Post a message using the hypervisor message IPC.
  66. *
  67. * This involves a hypercall.
  68. */
  69. int hv_post_message(union hv_connection_id connection_id,
  70. enum hv_message_type message_type,
  71. void *payload, size_t payload_size)
  72. {
  73. struct hv_input_post_message *aligned_msg;
  74. struct hv_per_cpu_context *hv_cpu;
  75. u64 status;
  76. if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
  77. return -EMSGSIZE;
  78. hv_cpu = get_cpu_ptr(hv_context.cpu_context);
  79. aligned_msg = hv_cpu->post_msg_page;
  80. aligned_msg->connectionid = connection_id;
  81. aligned_msg->reserved = 0;
  82. aligned_msg->message_type = message_type;
  83. aligned_msg->payload_size = payload_size;
  84. memcpy((void *)aligned_msg->payload, payload, payload_size);
  85. status = hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL);
  86. /* Preemption must remain disabled until after the hypercall
  87. * so some other thread can't get scheduled onto this cpu and
  88. * corrupt the per-cpu post_msg_page
  89. */
  90. put_cpu_ptr(hv_cpu);
  91. return status & 0xFFFF;
  92. }
  93. /*
  94. * ISR for when stimer0 is operating in Direct Mode. Direct Mode
  95. * does not use VMbus or any VMbus messages, so process here and not
  96. * in the VMbus driver code.
  97. */
  98. static void hv_stimer0_isr(void)
  99. {
  100. struct hv_per_cpu_context *hv_cpu;
  101. hv_cpu = this_cpu_ptr(hv_context.cpu_context);
  102. hv_cpu->clk_evt->event_handler(hv_cpu->clk_evt);
  103. add_interrupt_randomness(stimer0_vector, 0);
  104. }
  105. static int hv_ce_set_next_event(unsigned long delta,
  106. struct clock_event_device *evt)
  107. {
  108. u64 current_tick;
  109. WARN_ON(!clockevent_state_oneshot(evt));
  110. current_tick = hyperv_cs->read(NULL);
  111. current_tick += delta;
  112. hv_init_timer(0, current_tick);
  113. return 0;
  114. }
  115. static int hv_ce_shutdown(struct clock_event_device *evt)
  116. {
  117. hv_init_timer(0, 0);
  118. hv_init_timer_config(0, 0);
  119. if (direct_mode_enabled)
  120. hv_disable_stimer0_percpu_irq(stimer0_irq);
  121. return 0;
  122. }
  123. static int hv_ce_set_oneshot(struct clock_event_device *evt)
  124. {
  125. union hv_timer_config timer_cfg;
  126. timer_cfg.as_uint64 = 0;
  127. timer_cfg.enable = 1;
  128. timer_cfg.auto_enable = 1;
  129. if (direct_mode_enabled) {
  130. /*
  131. * When it expires, the timer will directly interrupt
  132. * on the specified hardware vector/IRQ.
  133. */
  134. timer_cfg.direct_mode = 1;
  135. timer_cfg.apic_vector = stimer0_vector;
  136. hv_enable_stimer0_percpu_irq(stimer0_irq);
  137. } else {
  138. /*
  139. * When it expires, the timer will generate a VMbus message,
  140. * to be handled by the normal VMbus interrupt handler.
  141. */
  142. timer_cfg.direct_mode = 0;
  143. timer_cfg.sintx = VMBUS_MESSAGE_SINT;
  144. }
  145. hv_init_timer_config(0, timer_cfg.as_uint64);
  146. return 0;
  147. }
  148. static void hv_init_clockevent_device(struct clock_event_device *dev, int cpu)
  149. {
  150. dev->name = "Hyper-V clockevent";
  151. dev->features = CLOCK_EVT_FEAT_ONESHOT;
  152. dev->cpumask = cpumask_of(cpu);
  153. dev->rating = 1000;
  154. /*
  155. * Avoid settint dev->owner = THIS_MODULE deliberately as doing so will
  156. * result in clockevents_config_and_register() taking additional
  157. * references to the hv_vmbus module making it impossible to unload.
  158. */
  159. dev->set_state_shutdown = hv_ce_shutdown;
  160. dev->set_state_oneshot = hv_ce_set_oneshot;
  161. dev->set_next_event = hv_ce_set_next_event;
  162. }
  163. int hv_synic_alloc(void)
  164. {
  165. int cpu;
  166. struct hv_per_cpu_context *hv_cpu;
  167. /*
  168. * First, zero all per-cpu memory areas so hv_synic_free() can
  169. * detect what memory has been allocated and cleanup properly
  170. * after any failures.
  171. */
  172. for_each_present_cpu(cpu) {
  173. hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
  174. memset(hv_cpu, 0, sizeof(*hv_cpu));
  175. }
  176. hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
  177. GFP_KERNEL);
  178. if (hv_context.hv_numa_map == NULL) {
  179. pr_err("Unable to allocate NUMA map\n");
  180. goto err;
  181. }
  182. for_each_present_cpu(cpu) {
  183. hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
  184. tasklet_init(&hv_cpu->msg_dpc,
  185. vmbus_on_msg_dpc, (unsigned long) hv_cpu);
  186. hv_cpu->clk_evt = kzalloc(sizeof(struct clock_event_device),
  187. GFP_KERNEL);
  188. if (hv_cpu->clk_evt == NULL) {
  189. pr_err("Unable to allocate clock event device\n");
  190. goto err;
  191. }
  192. hv_init_clockevent_device(hv_cpu->clk_evt, cpu);
  193. hv_cpu->synic_message_page =
  194. (void *)get_zeroed_page(GFP_ATOMIC);
  195. if (hv_cpu->synic_message_page == NULL) {
  196. pr_err("Unable to allocate SYNIC message page\n");
  197. goto err;
  198. }
  199. hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
  200. if (hv_cpu->synic_event_page == NULL) {
  201. pr_err("Unable to allocate SYNIC event page\n");
  202. goto err;
  203. }
  204. hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
  205. if (hv_cpu->post_msg_page == NULL) {
  206. pr_err("Unable to allocate post msg page\n");
  207. goto err;
  208. }
  209. INIT_LIST_HEAD(&hv_cpu->chan_list);
  210. }
  211. if (direct_mode_enabled &&
  212. hv_setup_stimer0_irq(&stimer0_irq, &stimer0_vector,
  213. hv_stimer0_isr))
  214. goto err;
  215. return 0;
  216. err:
  217. /*
  218. * Any memory allocations that succeeded will be freed when
  219. * the caller cleans up by calling hv_synic_free()
  220. */
  221. return -ENOMEM;
  222. }
  223. void hv_synic_free(void)
  224. {
  225. int cpu;
  226. for_each_present_cpu(cpu) {
  227. struct hv_per_cpu_context *hv_cpu
  228. = per_cpu_ptr(hv_context.cpu_context, cpu);
  229. kfree(hv_cpu->clk_evt);
  230. free_page((unsigned long)hv_cpu->synic_event_page);
  231. free_page((unsigned long)hv_cpu->synic_message_page);
  232. free_page((unsigned long)hv_cpu->post_msg_page);
  233. }
  234. kfree(hv_context.hv_numa_map);
  235. }
  236. /*
  237. * hv_synic_init - Initialize the Synthetic Interrupt Controller.
  238. *
  239. * If it is already initialized by another entity (ie x2v shim), we need to
  240. * retrieve the initialized message and event pages. Otherwise, we create and
  241. * initialize the message and event pages.
  242. */
  243. int hv_synic_init(unsigned int cpu)
  244. {
  245. struct hv_per_cpu_context *hv_cpu
  246. = per_cpu_ptr(hv_context.cpu_context, cpu);
  247. union hv_synic_simp simp;
  248. union hv_synic_siefp siefp;
  249. union hv_synic_sint shared_sint;
  250. union hv_synic_scontrol sctrl;
  251. /* Setup the Synic's message page */
  252. hv_get_simp(simp.as_uint64);
  253. simp.simp_enabled = 1;
  254. simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
  255. >> PAGE_SHIFT;
  256. hv_set_simp(simp.as_uint64);
  257. /* Setup the Synic's event page */
  258. hv_get_siefp(siefp.as_uint64);
  259. siefp.siefp_enabled = 1;
  260. siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
  261. >> PAGE_SHIFT;
  262. hv_set_siefp(siefp.as_uint64);
  263. /* Setup the shared SINT. */
  264. hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  265. shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
  266. shared_sint.masked = false;
  267. if (ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED)
  268. shared_sint.auto_eoi = false;
  269. else
  270. shared_sint.auto_eoi = true;
  271. hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  272. /* Enable the global synic bit */
  273. hv_get_synic_state(sctrl.as_uint64);
  274. sctrl.enable = 1;
  275. hv_set_synic_state(sctrl.as_uint64);
  276. hv_context.synic_initialized = true;
  277. /*
  278. * Register the per-cpu clockevent source.
  279. */
  280. if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE)
  281. clockevents_config_and_register(hv_cpu->clk_evt,
  282. HV_TIMER_FREQUENCY,
  283. HV_MIN_DELTA_TICKS,
  284. HV_MAX_MAX_DELTA_TICKS);
  285. return 0;
  286. }
  287. /*
  288. * hv_synic_clockevents_cleanup - Cleanup clockevent devices
  289. */
  290. void hv_synic_clockevents_cleanup(void)
  291. {
  292. int cpu;
  293. if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE))
  294. return;
  295. if (direct_mode_enabled)
  296. hv_remove_stimer0_irq(stimer0_irq);
  297. for_each_present_cpu(cpu) {
  298. struct hv_per_cpu_context *hv_cpu
  299. = per_cpu_ptr(hv_context.cpu_context, cpu);
  300. clockevents_unbind_device(hv_cpu->clk_evt, cpu);
  301. }
  302. }
  303. /*
  304. * hv_synic_cleanup - Cleanup routine for hv_synic_init().
  305. */
  306. int hv_synic_cleanup(unsigned int cpu)
  307. {
  308. union hv_synic_sint shared_sint;
  309. union hv_synic_simp simp;
  310. union hv_synic_siefp siefp;
  311. union hv_synic_scontrol sctrl;
  312. struct vmbus_channel *channel, *sc;
  313. bool channel_found = false;
  314. unsigned long flags;
  315. if (!hv_context.synic_initialized)
  316. return -EFAULT;
  317. /*
  318. * Search for channels which are bound to the CPU we're about to
  319. * cleanup. In case we find one and vmbus is still connected we need to
  320. * fail, this will effectively prevent CPU offlining. There is no way
  321. * we can re-bind channels to different CPUs for now.
  322. */
  323. mutex_lock(&vmbus_connection.channel_mutex);
  324. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  325. if (channel->target_cpu == cpu) {
  326. channel_found = true;
  327. break;
  328. }
  329. spin_lock_irqsave(&channel->lock, flags);
  330. list_for_each_entry(sc, &channel->sc_list, sc_list) {
  331. if (sc->target_cpu == cpu) {
  332. channel_found = true;
  333. break;
  334. }
  335. }
  336. spin_unlock_irqrestore(&channel->lock, flags);
  337. if (channel_found)
  338. break;
  339. }
  340. mutex_unlock(&vmbus_connection.channel_mutex);
  341. if (channel_found && vmbus_connection.conn_state == CONNECTED)
  342. return -EBUSY;
  343. /* Turn off clockevent device */
  344. if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE) {
  345. struct hv_per_cpu_context *hv_cpu
  346. = this_cpu_ptr(hv_context.cpu_context);
  347. clockevents_unbind_device(hv_cpu->clk_evt, cpu);
  348. hv_ce_shutdown(hv_cpu->clk_evt);
  349. }
  350. hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  351. shared_sint.masked = 1;
  352. /* Need to correctly cleanup in the case of SMP!!! */
  353. /* Disable the interrupt */
  354. hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  355. hv_get_simp(simp.as_uint64);
  356. simp.simp_enabled = 0;
  357. simp.base_simp_gpa = 0;
  358. hv_set_simp(simp.as_uint64);
  359. hv_get_siefp(siefp.as_uint64);
  360. siefp.siefp_enabled = 0;
  361. siefp.base_siefp_gpa = 0;
  362. hv_set_siefp(siefp.as_uint64);
  363. /* Disable the global synic bit */
  364. hv_get_synic_state(sctrl.as_uint64);
  365. sctrl.enable = 0;
  366. hv_set_synic_state(sctrl.as_uint64);
  367. return 0;
  368. }