events_fifo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Xen event channels (FIFO-based ABI)
  3. *
  4. * Copyright (C) 2013 Citrix Systems R&D ltd.
  5. *
  6. * This source code is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * Or, when distributed separately from the Linux kernel or
  12. * incorporated into other software packages, subject to the following
  13. * license:
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a copy
  16. * of this source file (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy, modify,
  18. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  19. * and to permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be included in
  23. * all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  30. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  31. * IN THE SOFTWARE.
  32. */
  33. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  34. #include <linux/linkage.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/irq.h>
  37. #include <linux/module.h>
  38. #include <linux/smp.h>
  39. #include <linux/percpu.h>
  40. #include <linux/cpu.h>
  41. #include <asm/sync_bitops.h>
  42. #include <asm/xen/hypercall.h>
  43. #include <asm/xen/hypervisor.h>
  44. #include <asm/xen/page.h>
  45. #include <xen/xen.h>
  46. #include <xen/xen-ops.h>
  47. #include <xen/events.h>
  48. #include <xen/interface/xen.h>
  49. #include <xen/interface/event_channel.h>
  50. #include "events_internal.h"
  51. #define EVENT_WORDS_PER_PAGE (PAGE_SIZE / sizeof(event_word_t))
  52. #define MAX_EVENT_ARRAY_PAGES (EVTCHN_FIFO_NR_CHANNELS / EVENT_WORDS_PER_PAGE)
  53. struct evtchn_fifo_queue {
  54. uint32_t head[EVTCHN_FIFO_MAX_QUEUES];
  55. };
  56. static DEFINE_PER_CPU(struct evtchn_fifo_control_block *, cpu_control_block);
  57. static DEFINE_PER_CPU(struct evtchn_fifo_queue, cpu_queue);
  58. static event_word_t *event_array[MAX_EVENT_ARRAY_PAGES] __read_mostly;
  59. static unsigned event_array_pages __read_mostly;
  60. #define BM(w) ((unsigned long *)(w))
  61. static inline event_word_t *event_word_from_port(unsigned port)
  62. {
  63. unsigned i = port / EVENT_WORDS_PER_PAGE;
  64. return event_array[i] + port % EVENT_WORDS_PER_PAGE;
  65. }
  66. static unsigned evtchn_fifo_max_channels(void)
  67. {
  68. return EVTCHN_FIFO_NR_CHANNELS;
  69. }
  70. static unsigned evtchn_fifo_nr_channels(void)
  71. {
  72. return event_array_pages * EVENT_WORDS_PER_PAGE;
  73. }
  74. static void free_unused_array_pages(void)
  75. {
  76. unsigned i;
  77. for (i = event_array_pages; i < MAX_EVENT_ARRAY_PAGES; i++) {
  78. if (!event_array[i])
  79. break;
  80. free_page((unsigned long)event_array[i]);
  81. event_array[i] = NULL;
  82. }
  83. }
  84. static void init_array_page(event_word_t *array_page)
  85. {
  86. unsigned i;
  87. for (i = 0; i < EVENT_WORDS_PER_PAGE; i++)
  88. array_page[i] = 1 << EVTCHN_FIFO_MASKED;
  89. }
  90. static int evtchn_fifo_setup(struct irq_info *info)
  91. {
  92. unsigned port = info->evtchn;
  93. unsigned new_array_pages;
  94. int ret;
  95. new_array_pages = port / EVENT_WORDS_PER_PAGE + 1;
  96. if (new_array_pages > MAX_EVENT_ARRAY_PAGES)
  97. return -EINVAL;
  98. while (event_array_pages < new_array_pages) {
  99. void *array_page;
  100. struct evtchn_expand_array expand_array;
  101. /* Might already have a page if we've resumed. */
  102. array_page = event_array[event_array_pages];
  103. if (!array_page) {
  104. array_page = (void *)__get_free_page(GFP_KERNEL);
  105. if (array_page == NULL) {
  106. ret = -ENOMEM;
  107. goto error;
  108. }
  109. event_array[event_array_pages] = array_page;
  110. }
  111. /* Mask all events in this page before adding it. */
  112. init_array_page(array_page);
  113. expand_array.array_gfn = virt_to_mfn(array_page);
  114. ret = HYPERVISOR_event_channel_op(EVTCHNOP_expand_array, &expand_array);
  115. if (ret < 0)
  116. goto error;
  117. event_array_pages++;
  118. }
  119. return 0;
  120. error:
  121. if (event_array_pages == 0)
  122. panic("xen: unable to expand event array with initial page (%d)\n", ret);
  123. else
  124. pr_err("unable to expand event array (%d)\n", ret);
  125. free_unused_array_pages();
  126. return ret;
  127. }
  128. static void evtchn_fifo_bind_to_cpu(struct irq_info *info, unsigned cpu)
  129. {
  130. /* no-op */
  131. }
  132. static void evtchn_fifo_clear_pending(unsigned port)
  133. {
  134. event_word_t *word = event_word_from_port(port);
  135. sync_clear_bit(EVTCHN_FIFO_PENDING, BM(word));
  136. }
  137. static void evtchn_fifo_set_pending(unsigned port)
  138. {
  139. event_word_t *word = event_word_from_port(port);
  140. sync_set_bit(EVTCHN_FIFO_PENDING, BM(word));
  141. }
  142. static bool evtchn_fifo_is_pending(unsigned port)
  143. {
  144. event_word_t *word = event_word_from_port(port);
  145. return sync_test_bit(EVTCHN_FIFO_PENDING, BM(word));
  146. }
  147. static bool evtchn_fifo_test_and_set_mask(unsigned port)
  148. {
  149. event_word_t *word = event_word_from_port(port);
  150. return sync_test_and_set_bit(EVTCHN_FIFO_MASKED, BM(word));
  151. }
  152. static void evtchn_fifo_mask(unsigned port)
  153. {
  154. event_word_t *word = event_word_from_port(port);
  155. sync_set_bit(EVTCHN_FIFO_MASKED, BM(word));
  156. }
  157. /*
  158. * Clear MASKED, spinning if BUSY is set.
  159. */
  160. static void clear_masked(volatile event_word_t *word)
  161. {
  162. event_word_t new, old, w;
  163. w = *word;
  164. do {
  165. old = w & ~(1 << EVTCHN_FIFO_BUSY);
  166. new = old & ~(1 << EVTCHN_FIFO_MASKED);
  167. w = sync_cmpxchg(word, old, new);
  168. } while (w != old);
  169. }
  170. static void evtchn_fifo_unmask(unsigned port)
  171. {
  172. event_word_t *word = event_word_from_port(port);
  173. BUG_ON(!irqs_disabled());
  174. clear_masked(word);
  175. if (sync_test_bit(EVTCHN_FIFO_PENDING, BM(word))) {
  176. struct evtchn_unmask unmask = { .port = port };
  177. (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
  178. }
  179. }
  180. static uint32_t clear_linked(volatile event_word_t *word)
  181. {
  182. event_word_t new, old, w;
  183. w = *word;
  184. do {
  185. old = w;
  186. new = (w & ~((1 << EVTCHN_FIFO_LINKED)
  187. | EVTCHN_FIFO_LINK_MASK));
  188. } while ((w = sync_cmpxchg(word, old, new)) != old);
  189. return w & EVTCHN_FIFO_LINK_MASK;
  190. }
  191. static void handle_irq_for_port(unsigned port)
  192. {
  193. int irq;
  194. struct irq_desc *desc;
  195. irq = get_evtchn_to_irq(port);
  196. if (irq != -1) {
  197. desc = irq_to_desc(irq);
  198. if (desc)
  199. generic_handle_irq_desc(irq, desc);
  200. }
  201. }
  202. static void consume_one_event(unsigned cpu,
  203. struct evtchn_fifo_control_block *control_block,
  204. unsigned priority, uint32_t *ready)
  205. {
  206. struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
  207. uint32_t head;
  208. unsigned port;
  209. event_word_t *word;
  210. head = q->head[priority];
  211. /*
  212. * Reached the tail last time? Read the new HEAD from the
  213. * control block.
  214. */
  215. if (head == 0) {
  216. rmb(); /* Ensure word is up-to-date before reading head. */
  217. head = control_block->head[priority];
  218. }
  219. port = head;
  220. word = event_word_from_port(port);
  221. head = clear_linked(word);
  222. /*
  223. * If the link is non-zero, there are more events in the
  224. * queue, otherwise the queue is empty.
  225. *
  226. * If the queue is empty, clear this priority from our local
  227. * copy of the ready word.
  228. */
  229. if (head == 0)
  230. clear_bit(priority, BM(ready));
  231. if (sync_test_bit(EVTCHN_FIFO_PENDING, BM(word))
  232. && !sync_test_bit(EVTCHN_FIFO_MASKED, BM(word)))
  233. handle_irq_for_port(port);
  234. q->head[priority] = head;
  235. }
  236. static void evtchn_fifo_handle_events(unsigned cpu)
  237. {
  238. struct evtchn_fifo_control_block *control_block;
  239. uint32_t ready;
  240. unsigned q;
  241. control_block = per_cpu(cpu_control_block, cpu);
  242. ready = xchg(&control_block->ready, 0);
  243. while (ready) {
  244. q = find_first_bit(BM(&ready), EVTCHN_FIFO_MAX_QUEUES);
  245. consume_one_event(cpu, control_block, q, &ready);
  246. ready |= xchg(&control_block->ready, 0);
  247. }
  248. }
  249. static void evtchn_fifo_resume(void)
  250. {
  251. unsigned cpu;
  252. for_each_possible_cpu(cpu) {
  253. void *control_block = per_cpu(cpu_control_block, cpu);
  254. struct evtchn_init_control init_control;
  255. int ret;
  256. if (!control_block)
  257. continue;
  258. /*
  259. * If this CPU is offline, take the opportunity to
  260. * free the control block while it is not being
  261. * used.
  262. */
  263. if (!cpu_online(cpu)) {
  264. free_page((unsigned long)control_block);
  265. per_cpu(cpu_control_block, cpu) = NULL;
  266. continue;
  267. }
  268. init_control.control_gfn = virt_to_mfn(control_block);
  269. init_control.offset = 0;
  270. init_control.vcpu = cpu;
  271. ret = HYPERVISOR_event_channel_op(EVTCHNOP_init_control,
  272. &init_control);
  273. if (ret < 0)
  274. BUG();
  275. }
  276. /*
  277. * The event array starts out as empty again and is extended
  278. * as normal when events are bound. The existing pages will
  279. * be reused.
  280. */
  281. event_array_pages = 0;
  282. }
  283. static const struct evtchn_ops evtchn_ops_fifo = {
  284. .max_channels = evtchn_fifo_max_channels,
  285. .nr_channels = evtchn_fifo_nr_channels,
  286. .setup = evtchn_fifo_setup,
  287. .bind_to_cpu = evtchn_fifo_bind_to_cpu,
  288. .clear_pending = evtchn_fifo_clear_pending,
  289. .set_pending = evtchn_fifo_set_pending,
  290. .is_pending = evtchn_fifo_is_pending,
  291. .test_and_set_mask = evtchn_fifo_test_and_set_mask,
  292. .mask = evtchn_fifo_mask,
  293. .unmask = evtchn_fifo_unmask,
  294. .handle_events = evtchn_fifo_handle_events,
  295. .resume = evtchn_fifo_resume,
  296. };
  297. static int evtchn_fifo_init_control_block(unsigned cpu)
  298. {
  299. struct page *control_block = NULL;
  300. struct evtchn_init_control init_control;
  301. int ret = -ENOMEM;
  302. control_block = alloc_page(GFP_KERNEL|__GFP_ZERO);
  303. if (control_block == NULL)
  304. goto error;
  305. init_control.control_gfn = virt_to_mfn(page_address(control_block));
  306. init_control.offset = 0;
  307. init_control.vcpu = cpu;
  308. ret = HYPERVISOR_event_channel_op(EVTCHNOP_init_control, &init_control);
  309. if (ret < 0)
  310. goto error;
  311. per_cpu(cpu_control_block, cpu) = page_address(control_block);
  312. return 0;
  313. error:
  314. __free_page(control_block);
  315. return ret;
  316. }
  317. static int evtchn_fifo_cpu_notification(struct notifier_block *self,
  318. unsigned long action,
  319. void *hcpu)
  320. {
  321. int cpu = (long)hcpu;
  322. int ret = 0;
  323. switch (action) {
  324. case CPU_UP_PREPARE:
  325. if (!per_cpu(cpu_control_block, cpu))
  326. ret = evtchn_fifo_init_control_block(cpu);
  327. break;
  328. default:
  329. break;
  330. }
  331. return ret < 0 ? NOTIFY_BAD : NOTIFY_OK;
  332. }
  333. static struct notifier_block evtchn_fifo_cpu_notifier = {
  334. .notifier_call = evtchn_fifo_cpu_notification,
  335. };
  336. int __init xen_evtchn_fifo_init(void)
  337. {
  338. int cpu = get_cpu();
  339. int ret;
  340. ret = evtchn_fifo_init_control_block(cpu);
  341. if (ret < 0)
  342. goto out;
  343. pr_info("Using FIFO-based ABI\n");
  344. evtchn_ops = &evtchn_ops_fifo;
  345. register_cpu_notifier(&evtchn_fifo_cpu_notifier);
  346. out:
  347. put_cpu();
  348. return ret;
  349. }