hv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. struct hv_per_cpu_context *hv_cpu;
  128. /*
  129. * First, zero all per-cpu memory areas so hv_synic_free() can
  130. * detect what memory has been allocated and cleanup properly
  131. * after any failures.
  132. */
  133. for_each_present_cpu(cpu) {
  134. hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
  135. memset(hv_cpu, 0, sizeof(*hv_cpu));
  136. }
  137. hv_context.hv_numa_map = kzalloc(sizeof(struct cpumask) * nr_node_ids,
  138. GFP_ATOMIC);
  139. if (hv_context.hv_numa_map == NULL) {
  140. pr_err("Unable to allocate NUMA map\n");
  141. goto err;
  142. }
  143. for_each_present_cpu(cpu) {
  144. hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
  145. tasklet_init(&hv_cpu->msg_dpc,
  146. vmbus_on_msg_dpc, (unsigned long) hv_cpu);
  147. hv_cpu->clk_evt = kzalloc(sizeof(struct clock_event_device),
  148. GFP_KERNEL);
  149. if (hv_cpu->clk_evt == NULL) {
  150. pr_err("Unable to allocate clock event device\n");
  151. goto err;
  152. }
  153. hv_init_clockevent_device(hv_cpu->clk_evt, cpu);
  154. hv_cpu->synic_message_page =
  155. (void *)get_zeroed_page(GFP_ATOMIC);
  156. if (hv_cpu->synic_message_page == NULL) {
  157. pr_err("Unable to allocate SYNIC message page\n");
  158. goto err;
  159. }
  160. hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
  161. if (hv_cpu->synic_event_page == NULL) {
  162. pr_err("Unable to allocate SYNIC event page\n");
  163. goto err;
  164. }
  165. hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
  166. if (hv_cpu->post_msg_page == NULL) {
  167. pr_err("Unable to allocate post msg page\n");
  168. goto err;
  169. }
  170. INIT_LIST_HEAD(&hv_cpu->chan_list);
  171. }
  172. return 0;
  173. err:
  174. /*
  175. * Any memory allocations that succeeded will be freed when
  176. * the caller cleans up by calling hv_synic_free()
  177. */
  178. return -ENOMEM;
  179. }
  180. void hv_synic_free(void)
  181. {
  182. int cpu;
  183. for_each_present_cpu(cpu) {
  184. struct hv_per_cpu_context *hv_cpu
  185. = per_cpu_ptr(hv_context.cpu_context, cpu);
  186. kfree(hv_cpu->clk_evt);
  187. free_page((unsigned long)hv_cpu->synic_event_page);
  188. free_page((unsigned long)hv_cpu->synic_message_page);
  189. free_page((unsigned long)hv_cpu->post_msg_page);
  190. }
  191. kfree(hv_context.hv_numa_map);
  192. }
  193. /*
  194. * hv_synic_init - Initialize the Synthethic Interrupt Controller.
  195. *
  196. * If it is already initialized by another entity (ie x2v shim), we need to
  197. * retrieve the initialized message and event pages. Otherwise, we create and
  198. * initialize the message and event pages.
  199. */
  200. int hv_synic_init(unsigned int cpu)
  201. {
  202. struct hv_per_cpu_context *hv_cpu
  203. = per_cpu_ptr(hv_context.cpu_context, cpu);
  204. union hv_synic_simp simp;
  205. union hv_synic_siefp siefp;
  206. union hv_synic_sint shared_sint;
  207. union hv_synic_scontrol sctrl;
  208. /* Setup the Synic's message page */
  209. hv_get_simp(simp.as_uint64);
  210. simp.simp_enabled = 1;
  211. simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
  212. >> PAGE_SHIFT;
  213. hv_set_simp(simp.as_uint64);
  214. /* Setup the Synic's event page */
  215. hv_get_siefp(siefp.as_uint64);
  216. siefp.siefp_enabled = 1;
  217. siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
  218. >> PAGE_SHIFT;
  219. hv_set_siefp(siefp.as_uint64);
  220. /* Setup the shared SINT. */
  221. hv_get_synint_state(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT,
  222. shared_sint.as_uint64);
  223. shared_sint.as_uint64 = 0;
  224. shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
  225. shared_sint.masked = false;
  226. if (ms_hyperv.hints & HV_X64_DEPRECATING_AEOI_RECOMMENDED)
  227. shared_sint.auto_eoi = false;
  228. else
  229. shared_sint.auto_eoi = true;
  230. hv_set_synint_state(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT,
  231. shared_sint.as_uint64);
  232. /* Enable the global synic bit */
  233. hv_get_synic_state(sctrl.as_uint64);
  234. sctrl.enable = 1;
  235. hv_set_synic_state(sctrl.as_uint64);
  236. hv_context.synic_initialized = true;
  237. /*
  238. * Register the per-cpu clockevent source.
  239. */
  240. if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE)
  241. clockevents_config_and_register(hv_cpu->clk_evt,
  242. HV_TIMER_FREQUENCY,
  243. HV_MIN_DELTA_TICKS,
  244. HV_MAX_MAX_DELTA_TICKS);
  245. return 0;
  246. }
  247. /*
  248. * hv_synic_clockevents_cleanup - Cleanup clockevent devices
  249. */
  250. void hv_synic_clockevents_cleanup(void)
  251. {
  252. int cpu;
  253. if (!(ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE))
  254. return;
  255. for_each_present_cpu(cpu) {
  256. struct hv_per_cpu_context *hv_cpu
  257. = per_cpu_ptr(hv_context.cpu_context, cpu);
  258. clockevents_unbind_device(hv_cpu->clk_evt, cpu);
  259. }
  260. }
  261. /*
  262. * hv_synic_cleanup - Cleanup routine for hv_synic_init().
  263. */
  264. int hv_synic_cleanup(unsigned int cpu)
  265. {
  266. union hv_synic_sint shared_sint;
  267. union hv_synic_simp simp;
  268. union hv_synic_siefp siefp;
  269. union hv_synic_scontrol sctrl;
  270. struct vmbus_channel *channel, *sc;
  271. bool channel_found = false;
  272. unsigned long flags;
  273. if (!hv_context.synic_initialized)
  274. return -EFAULT;
  275. /*
  276. * Search for channels which are bound to the CPU we're about to
  277. * cleanup. In case we find one and vmbus is still connected we need to
  278. * fail, this will effectively prevent CPU offlining. There is no way
  279. * we can re-bind channels to different CPUs for now.
  280. */
  281. mutex_lock(&vmbus_connection.channel_mutex);
  282. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  283. if (channel->target_cpu == cpu) {
  284. channel_found = true;
  285. break;
  286. }
  287. spin_lock_irqsave(&channel->lock, flags);
  288. list_for_each_entry(sc, &channel->sc_list, sc_list) {
  289. if (sc->target_cpu == cpu) {
  290. channel_found = true;
  291. break;
  292. }
  293. }
  294. spin_unlock_irqrestore(&channel->lock, flags);
  295. if (channel_found)
  296. break;
  297. }
  298. mutex_unlock(&vmbus_connection.channel_mutex);
  299. if (channel_found && vmbus_connection.conn_state == CONNECTED)
  300. return -EBUSY;
  301. /* Turn off clockevent device */
  302. if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE) {
  303. struct hv_per_cpu_context *hv_cpu
  304. = this_cpu_ptr(hv_context.cpu_context);
  305. clockevents_unbind_device(hv_cpu->clk_evt, cpu);
  306. hv_ce_shutdown(hv_cpu->clk_evt);
  307. }
  308. hv_get_synint_state(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT,
  309. shared_sint.as_uint64);
  310. shared_sint.masked = 1;
  311. /* Need to correctly cleanup in the case of SMP!!! */
  312. /* Disable the interrupt */
  313. hv_set_synint_state(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT,
  314. shared_sint.as_uint64);
  315. hv_get_simp(simp.as_uint64);
  316. simp.simp_enabled = 0;
  317. simp.base_simp_gpa = 0;
  318. hv_set_simp(simp.as_uint64);
  319. hv_get_siefp(siefp.as_uint64);
  320. siefp.siefp_enabled = 0;
  321. siefp.base_siefp_gpa = 0;
  322. hv_set_siefp(siefp.as_uint64);
  323. /* Disable the global synic bit */
  324. hv_get_synic_state(sctrl.as_uint64);
  325. sctrl.enable = 0;
  326. hv_set_synic_state(sctrl.as_uint64);
  327. return 0;
  328. }