hv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
  167. GFP_KERNEL);
  168. if (hv_context.hv_numa_map == NULL) {
  169. pr_err("Unable to allocate NUMA map\n");
  170. goto err;
  171. }
  172. for_each_present_cpu(cpu) {
  173. struct hv_per_cpu_context *hv_cpu
  174. = per_cpu_ptr(hv_context.cpu_context, cpu);
  175. memset(hv_cpu, 0, sizeof(*hv_cpu));
  176. tasklet_init(&hv_cpu->msg_dpc,
  177. vmbus_on_msg_dpc, (unsigned long) hv_cpu);
  178. hv_cpu->clk_evt = kzalloc(sizeof(struct clock_event_device),
  179. GFP_KERNEL);
  180. if (hv_cpu->clk_evt == NULL) {
  181. pr_err("Unable to allocate clock event device\n");
  182. goto err;
  183. }
  184. hv_init_clockevent_device(hv_cpu->clk_evt, cpu);
  185. hv_cpu->synic_message_page =
  186. (void *)get_zeroed_page(GFP_ATOMIC);
  187. if (hv_cpu->synic_message_page == NULL) {
  188. pr_err("Unable to allocate SYNIC message page\n");
  189. goto err;
  190. }
  191. hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
  192. if (hv_cpu->synic_event_page == NULL) {
  193. pr_err("Unable to allocate SYNIC event page\n");
  194. goto err;
  195. }
  196. hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
  197. if (hv_cpu->post_msg_page == NULL) {
  198. pr_err("Unable to allocate post msg page\n");
  199. goto err;
  200. }
  201. INIT_LIST_HEAD(&hv_cpu->chan_list);
  202. }
  203. if (direct_mode_enabled &&
  204. hv_setup_stimer0_irq(&stimer0_irq, &stimer0_vector,
  205. hv_stimer0_isr))
  206. goto err;
  207. return 0;
  208. err:
  209. /*
  210. * Any memory allocations that succeeded will be freed when
  211. * the caller cleans up by calling hv_synic_free()
  212. */
  213. return -ENOMEM;
  214. }
  215. void hv_synic_free(void)
  216. {
  217. int cpu;
  218. for_each_present_cpu(cpu) {
  219. struct hv_per_cpu_context *hv_cpu
  220. = per_cpu_ptr(hv_context.cpu_context, cpu);
  221. kfree(hv_cpu->clk_evt);
  222. free_page((unsigned long)hv_cpu->synic_event_page);
  223. free_page((unsigned long)hv_cpu->synic_message_page);
  224. free_page((unsigned long)hv_cpu->post_msg_page);
  225. }
  226. kfree(hv_context.hv_numa_map);
  227. }
  228. /*
  229. * hv_synic_init - Initialize the Synthetic 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. int hv_synic_init(unsigned int cpu)
  236. {
  237. struct hv_per_cpu_context *hv_cpu
  238. = per_cpu_ptr(hv_context.cpu_context, cpu);
  239. union hv_synic_simp simp;
  240. union hv_synic_siefp siefp;
  241. union hv_synic_sint shared_sint;
  242. union hv_synic_scontrol sctrl;
  243. /* Setup the Synic's message page */
  244. hv_get_simp(simp.as_uint64);
  245. simp.simp_enabled = 1;
  246. simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
  247. >> PAGE_SHIFT;
  248. hv_set_simp(simp.as_uint64);
  249. /* Setup the Synic's event page */
  250. hv_get_siefp(siefp.as_uint64);
  251. siefp.siefp_enabled = 1;
  252. siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
  253. >> PAGE_SHIFT;
  254. hv_set_siefp(siefp.as_uint64);
  255. /* Setup the shared SINT. */
  256. hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  257. shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
  258. shared_sint.masked = false;
  259. if (ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED)
  260. shared_sint.auto_eoi = false;
  261. else
  262. shared_sint.auto_eoi = true;
  263. hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  264. /* Enable the global synic bit */
  265. hv_get_synic_state(sctrl.as_uint64);
  266. sctrl.enable = 1;
  267. hv_set_synic_state(sctrl.as_uint64);
  268. hv_context.synic_initialized = true;
  269. /*
  270. * Register the per-cpu clockevent source.
  271. */
  272. if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE)
  273. clockevents_config_and_register(hv_cpu->clk_evt,
  274. HV_TIMER_FREQUENCY,
  275. HV_MIN_DELTA_TICKS,
  276. HV_MAX_MAX_DELTA_TICKS);
  277. return 0;
  278. }
  279. /*
  280. * hv_synic_clockevents_cleanup - Cleanup clockevent devices
  281. */
  282. void hv_synic_clockevents_cleanup(void)
  283. {
  284. int cpu;
  285. if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE))
  286. return;
  287. if (direct_mode_enabled)
  288. hv_remove_stimer0_irq(stimer0_irq);
  289. for_each_present_cpu(cpu) {
  290. struct hv_per_cpu_context *hv_cpu
  291. = per_cpu_ptr(hv_context.cpu_context, cpu);
  292. clockevents_unbind_device(hv_cpu->clk_evt, cpu);
  293. }
  294. }
  295. /*
  296. * hv_synic_cleanup - Cleanup routine for hv_synic_init().
  297. */
  298. int hv_synic_cleanup(unsigned int cpu)
  299. {
  300. union hv_synic_sint shared_sint;
  301. union hv_synic_simp simp;
  302. union hv_synic_siefp siefp;
  303. union hv_synic_scontrol sctrl;
  304. struct vmbus_channel *channel, *sc;
  305. bool channel_found = false;
  306. unsigned long flags;
  307. if (!hv_context.synic_initialized)
  308. return -EFAULT;
  309. /*
  310. * Search for channels which are bound to the CPU we're about to
  311. * cleanup. In case we find one and vmbus is still connected we need to
  312. * fail, this will effectively prevent CPU offlining. There is no way
  313. * we can re-bind channels to different CPUs for now.
  314. */
  315. mutex_lock(&vmbus_connection.channel_mutex);
  316. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  317. if (channel->target_cpu == cpu) {
  318. channel_found = true;
  319. break;
  320. }
  321. spin_lock_irqsave(&channel->lock, flags);
  322. list_for_each_entry(sc, &channel->sc_list, sc_list) {
  323. if (sc->target_cpu == cpu) {
  324. channel_found = true;
  325. break;
  326. }
  327. }
  328. spin_unlock_irqrestore(&channel->lock, flags);
  329. if (channel_found)
  330. break;
  331. }
  332. mutex_unlock(&vmbus_connection.channel_mutex);
  333. if (channel_found && vmbus_connection.conn_state == CONNECTED)
  334. return -EBUSY;
  335. /* Turn off clockevent device */
  336. if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE) {
  337. struct hv_per_cpu_context *hv_cpu
  338. = this_cpu_ptr(hv_context.cpu_context);
  339. clockevents_unbind_device(hv_cpu->clk_evt, cpu);
  340. hv_ce_shutdown(hv_cpu->clk_evt);
  341. put_cpu_ptr(hv_cpu);
  342. }
  343. hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  344. shared_sint.masked = 1;
  345. /* Need to correctly cleanup in the case of SMP!!! */
  346. /* Disable the interrupt */
  347. hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  348. hv_get_simp(simp.as_uint64);
  349. simp.simp_enabled = 0;
  350. simp.base_simp_gpa = 0;
  351. hv_set_simp(simp.as_uint64);
  352. hv_get_siefp(siefp.as_uint64);
  353. siefp.siefp_enabled = 0;
  354. siefp.base_siefp_gpa = 0;
  355. hv_set_siefp(siefp.as_uint64);
  356. /* Disable the global synic bit */
  357. hv_get_synic_state(sctrl.as_uint64);
  358. sctrl.enable = 0;
  359. hv_set_synic_state(sctrl.as_uint64);
  360. return 0;
  361. }