hv.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. };
  38. #define HV_TIMER_FREQUENCY (10 * 1000 * 1000) /* 100ns period */
  39. #define HV_MAX_MAX_DELTA_TICKS 0xffffffff
  40. #define HV_MIN_DELTA_TICKS 1
  41. /*
  42. * hv_init - Main initialization routine.
  43. *
  44. * This routine must be called before any other routines in here are called
  45. */
  46. int hv_init(void)
  47. {
  48. if (!hv_is_hypercall_page_setup())
  49. return -ENOTSUPP;
  50. hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
  51. if (!hv_context.cpu_context)
  52. return -ENOMEM;
  53. return 0;
  54. }
  55. /*
  56. * hv_post_message - Post a message using the hypervisor message IPC.
  57. *
  58. * This involves a hypercall.
  59. */
  60. int hv_post_message(union hv_connection_id connection_id,
  61. enum hv_message_type message_type,
  62. void *payload, size_t payload_size)
  63. {
  64. struct hv_input_post_message *aligned_msg;
  65. struct hv_per_cpu_context *hv_cpu;
  66. u64 status;
  67. if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
  68. return -EMSGSIZE;
  69. hv_cpu = get_cpu_ptr(hv_context.cpu_context);
  70. aligned_msg = hv_cpu->post_msg_page;
  71. aligned_msg->connectionid = connection_id;
  72. aligned_msg->reserved = 0;
  73. aligned_msg->message_type = message_type;
  74. aligned_msg->payload_size = payload_size;
  75. memcpy((void *)aligned_msg->payload, payload, payload_size);
  76. status = hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL);
  77. /* Preemption must remain disabled until after the hypercall
  78. * so some other thread can't get scheduled onto this cpu and
  79. * corrupt the per-cpu post_msg_page
  80. */
  81. put_cpu_ptr(hv_cpu);
  82. return status & 0xFFFF;
  83. }
  84. static int hv_ce_set_next_event(unsigned long delta,
  85. struct clock_event_device *evt)
  86. {
  87. u64 current_tick;
  88. WARN_ON(!clockevent_state_oneshot(evt));
  89. current_tick = hyperv_cs->read(NULL);
  90. current_tick += delta;
  91. hv_init_timer(HV_X64_MSR_STIMER0_COUNT, current_tick);
  92. return 0;
  93. }
  94. static int hv_ce_shutdown(struct clock_event_device *evt)
  95. {
  96. hv_init_timer(HV_X64_MSR_STIMER0_COUNT, 0);
  97. hv_init_timer_config(HV_X64_MSR_STIMER0_CONFIG, 0);
  98. return 0;
  99. }
  100. static int hv_ce_set_oneshot(struct clock_event_device *evt)
  101. {
  102. union hv_timer_config timer_cfg;
  103. timer_cfg.enable = 1;
  104. timer_cfg.auto_enable = 1;
  105. timer_cfg.sintx = VMBUS_MESSAGE_SINT;
  106. hv_init_timer_config(HV_X64_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
  107. return 0;
  108. }
  109. static void hv_init_clockevent_device(struct clock_event_device *dev, int cpu)
  110. {
  111. dev->name = "Hyper-V clockevent";
  112. dev->features = CLOCK_EVT_FEAT_ONESHOT;
  113. dev->cpumask = cpumask_of(cpu);
  114. dev->rating = 1000;
  115. /*
  116. * Avoid settint dev->owner = THIS_MODULE deliberately as doing so will
  117. * result in clockevents_config_and_register() taking additional
  118. * references to the hv_vmbus module making it impossible to unload.
  119. */
  120. dev->set_state_shutdown = hv_ce_shutdown;
  121. dev->set_state_oneshot = hv_ce_set_oneshot;
  122. dev->set_next_event = hv_ce_set_next_event;
  123. }
  124. int hv_synic_alloc(void)
  125. {
  126. int cpu;
  127. hv_context.hv_numa_map = kzalloc(sizeof(struct cpumask) * nr_node_ids,
  128. GFP_ATOMIC);
  129. if (hv_context.hv_numa_map == NULL) {
  130. pr_err("Unable to allocate NUMA map\n");
  131. goto err;
  132. }
  133. for_each_present_cpu(cpu) {
  134. struct hv_per_cpu_context *hv_cpu
  135. = per_cpu_ptr(hv_context.cpu_context, cpu);
  136. memset(hv_cpu, 0, sizeof(*hv_cpu));
  137. tasklet_init(&hv_cpu->msg_dpc,
  138. vmbus_on_msg_dpc, (unsigned long) hv_cpu);
  139. hv_cpu->clk_evt = kzalloc(sizeof(struct clock_event_device),
  140. GFP_KERNEL);
  141. if (hv_cpu->clk_evt == NULL) {
  142. pr_err("Unable to allocate clock event device\n");
  143. goto err;
  144. }
  145. hv_init_clockevent_device(hv_cpu->clk_evt, cpu);
  146. hv_cpu->synic_message_page =
  147. (void *)get_zeroed_page(GFP_ATOMIC);
  148. if (hv_cpu->synic_message_page == NULL) {
  149. pr_err("Unable to allocate SYNIC message page\n");
  150. goto err;
  151. }
  152. hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
  153. if (hv_cpu->synic_event_page == NULL) {
  154. pr_err("Unable to allocate SYNIC event page\n");
  155. goto err;
  156. }
  157. hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
  158. if (hv_cpu->post_msg_page == NULL) {
  159. pr_err("Unable to allocate post msg page\n");
  160. goto err;
  161. }
  162. INIT_LIST_HEAD(&hv_cpu->chan_list);
  163. }
  164. return 0;
  165. err:
  166. return -ENOMEM;
  167. }
  168. void hv_synic_free(void)
  169. {
  170. int cpu;
  171. for_each_present_cpu(cpu) {
  172. struct hv_per_cpu_context *hv_cpu
  173. = per_cpu_ptr(hv_context.cpu_context, cpu);
  174. if (hv_cpu->synic_event_page)
  175. free_page((unsigned long)hv_cpu->synic_event_page);
  176. if (hv_cpu->synic_message_page)
  177. free_page((unsigned long)hv_cpu->synic_message_page);
  178. if (hv_cpu->post_msg_page)
  179. free_page((unsigned long)hv_cpu->post_msg_page);
  180. }
  181. kfree(hv_context.hv_numa_map);
  182. }
  183. /*
  184. * hv_synic_init - Initialize the Synthethic Interrupt Controller.
  185. *
  186. * If it is already initialized by another entity (ie x2v shim), we need to
  187. * retrieve the initialized message and event pages. Otherwise, we create and
  188. * initialize the message and event pages.
  189. */
  190. int hv_synic_init(unsigned int cpu)
  191. {
  192. struct hv_per_cpu_context *hv_cpu
  193. = per_cpu_ptr(hv_context.cpu_context, cpu);
  194. union hv_synic_simp simp;
  195. union hv_synic_siefp siefp;
  196. union hv_synic_sint shared_sint;
  197. union hv_synic_scontrol sctrl;
  198. /* Setup the Synic's message page */
  199. hv_get_simp(simp.as_uint64);
  200. simp.simp_enabled = 1;
  201. simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
  202. >> PAGE_SHIFT;
  203. hv_set_simp(simp.as_uint64);
  204. /* Setup the Synic's event page */
  205. hv_get_siefp(siefp.as_uint64);
  206. siefp.siefp_enabled = 1;
  207. siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
  208. >> PAGE_SHIFT;
  209. hv_set_siefp(siefp.as_uint64);
  210. /* Setup the shared SINT. */
  211. hv_get_synint_state(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT,
  212. shared_sint.as_uint64);
  213. shared_sint.as_uint64 = 0;
  214. shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
  215. shared_sint.masked = false;
  216. if (ms_hyperv.hints & HV_X64_DEPRECATING_AEOI_RECOMMENDED)
  217. shared_sint.auto_eoi = false;
  218. else
  219. shared_sint.auto_eoi = true;
  220. hv_set_synint_state(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT,
  221. shared_sint.as_uint64);
  222. /* Enable the global synic bit */
  223. hv_get_synic_state(sctrl.as_uint64);
  224. sctrl.enable = 1;
  225. hv_set_synic_state(sctrl.as_uint64);
  226. hv_context.synic_initialized = true;
  227. /*
  228. * Register the per-cpu clockevent source.
  229. */
  230. if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE)
  231. clockevents_config_and_register(hv_cpu->clk_evt,
  232. HV_TIMER_FREQUENCY,
  233. HV_MIN_DELTA_TICKS,
  234. HV_MAX_MAX_DELTA_TICKS);
  235. return 0;
  236. }
  237. /*
  238. * hv_synic_clockevents_cleanup - Cleanup clockevent devices
  239. */
  240. void hv_synic_clockevents_cleanup(void)
  241. {
  242. int cpu;
  243. if (!(ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE))
  244. return;
  245. for_each_present_cpu(cpu) {
  246. struct hv_per_cpu_context *hv_cpu
  247. = per_cpu_ptr(hv_context.cpu_context, cpu);
  248. clockevents_unbind_device(hv_cpu->clk_evt, cpu);
  249. }
  250. }
  251. /*
  252. * hv_synic_cleanup - Cleanup routine for hv_synic_init().
  253. */
  254. int hv_synic_cleanup(unsigned int cpu)
  255. {
  256. union hv_synic_sint shared_sint;
  257. union hv_synic_simp simp;
  258. union hv_synic_siefp siefp;
  259. union hv_synic_scontrol sctrl;
  260. struct vmbus_channel *channel, *sc;
  261. bool channel_found = false;
  262. unsigned long flags;
  263. if (!hv_context.synic_initialized)
  264. return -EFAULT;
  265. /*
  266. * Search for channels which are bound to the CPU we're about to
  267. * cleanup. In case we find one and vmbus is still connected we need to
  268. * fail, this will effectively prevent CPU offlining. There is no way
  269. * we can re-bind channels to different CPUs for now.
  270. */
  271. mutex_lock(&vmbus_connection.channel_mutex);
  272. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  273. if (channel->target_cpu == cpu) {
  274. channel_found = true;
  275. break;
  276. }
  277. spin_lock_irqsave(&channel->lock, flags);
  278. list_for_each_entry(sc, &channel->sc_list, sc_list) {
  279. if (sc->target_cpu == cpu) {
  280. channel_found = true;
  281. break;
  282. }
  283. }
  284. spin_unlock_irqrestore(&channel->lock, flags);
  285. if (channel_found)
  286. break;
  287. }
  288. mutex_unlock(&vmbus_connection.channel_mutex);
  289. if (channel_found && vmbus_connection.conn_state == CONNECTED)
  290. return -EBUSY;
  291. /* Turn off clockevent device */
  292. if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE) {
  293. struct hv_per_cpu_context *hv_cpu
  294. = this_cpu_ptr(hv_context.cpu_context);
  295. clockevents_unbind_device(hv_cpu->clk_evt, cpu);
  296. hv_ce_shutdown(hv_cpu->clk_evt);
  297. put_cpu_ptr(hv_cpu);
  298. }
  299. hv_get_synint_state(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT,
  300. shared_sint.as_uint64);
  301. shared_sint.masked = 1;
  302. /* Need to correctly cleanup in the case of SMP!!! */
  303. /* Disable the interrupt */
  304. hv_set_synint_state(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT,
  305. shared_sint.as_uint64);
  306. hv_get_simp(simp.as_uint64);
  307. simp.simp_enabled = 0;
  308. simp.base_simp_gpa = 0;
  309. hv_set_simp(simp.as_uint64);
  310. hv_get_siefp(siefp.as_uint64);
  311. siefp.siefp_enabled = 0;
  312. siefp.base_siefp_gpa = 0;
  313. hv_set_siefp(siefp.as_uint64);
  314. /* Disable the global synic bit */
  315. hv_get_synic_state(sctrl.as_uint64);
  316. sctrl.enable = 0;
  317. hv_set_synic_state(sctrl.as_uint64);
  318. return 0;
  319. }