hv.c 9.9 KB

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