events_fifo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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/smp.h>
  38. #include <linux/percpu.h>
  39. #include <linux/cpu.h>
  40. #include <asm/barrier.h>
  41. #include <asm/sync_bitops.h>
  42. #include <asm/xen/hypercall.h>
  43. #include <asm/xen/hypervisor.h>
  44. #include <xen/xen.h>
  45. #include <xen/xen-ops.h>
  46. #include <xen/events.h>
  47. #include <xen/interface/xen.h>
  48. #include <xen/interface/event_channel.h>
  49. #include <xen/page.h>
  50. #include "events_internal.h"
  51. #define EVENT_WORDS_PER_PAGE (XEN_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. /*
  61. * sync_set_bit() and friends must be unsigned long aligned.
  62. */
  63. #if BITS_PER_LONG > 32
  64. #define BM(w) (unsigned long *)((unsigned long)w & ~0x7UL)
  65. #define EVTCHN_FIFO_BIT(b, w) \
  66. (((unsigned long)w & 0x4UL) ? (EVTCHN_FIFO_ ##b + 32) : EVTCHN_FIFO_ ##b)
  67. #else
  68. #define BM(w) ((unsigned long *)(w))
  69. #define EVTCHN_FIFO_BIT(b, w) EVTCHN_FIFO_ ##b
  70. #endif
  71. static inline event_word_t *event_word_from_port(evtchn_port_t port)
  72. {
  73. unsigned i = port / EVENT_WORDS_PER_PAGE;
  74. return event_array[i] + port % EVENT_WORDS_PER_PAGE;
  75. }
  76. static unsigned evtchn_fifo_max_channels(void)
  77. {
  78. return EVTCHN_FIFO_NR_CHANNELS;
  79. }
  80. static unsigned evtchn_fifo_nr_channels(void)
  81. {
  82. return event_array_pages * EVENT_WORDS_PER_PAGE;
  83. }
  84. static int init_control_block(int cpu,
  85. struct evtchn_fifo_control_block *control_block)
  86. {
  87. struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
  88. struct evtchn_init_control init_control;
  89. unsigned int i;
  90. /* Reset the control block and the local HEADs. */
  91. clear_page(control_block);
  92. for (i = 0; i < EVTCHN_FIFO_MAX_QUEUES; i++)
  93. q->head[i] = 0;
  94. init_control.control_gfn = virt_to_gfn(control_block);
  95. init_control.offset = 0;
  96. init_control.vcpu = xen_vcpu_nr(cpu);
  97. return HYPERVISOR_event_channel_op(EVTCHNOP_init_control, &init_control);
  98. }
  99. static void free_unused_array_pages(void)
  100. {
  101. unsigned i;
  102. for (i = event_array_pages; i < MAX_EVENT_ARRAY_PAGES; i++) {
  103. if (!event_array[i])
  104. break;
  105. free_page((unsigned long)event_array[i]);
  106. event_array[i] = NULL;
  107. }
  108. }
  109. static void init_array_page(event_word_t *array_page)
  110. {
  111. unsigned i;
  112. for (i = 0; i < EVENT_WORDS_PER_PAGE; i++)
  113. array_page[i] = 1 << EVTCHN_FIFO_MASKED;
  114. }
  115. static int evtchn_fifo_setup(evtchn_port_t port)
  116. {
  117. unsigned new_array_pages;
  118. int ret;
  119. new_array_pages = port / EVENT_WORDS_PER_PAGE + 1;
  120. if (new_array_pages > MAX_EVENT_ARRAY_PAGES)
  121. return -EINVAL;
  122. while (event_array_pages < new_array_pages) {
  123. void *array_page;
  124. struct evtchn_expand_array expand_array;
  125. /* Might already have a page if we've resumed. */
  126. array_page = event_array[event_array_pages];
  127. if (!array_page) {
  128. array_page = (void *)__get_free_page(GFP_KERNEL);
  129. if (array_page == NULL) {
  130. ret = -ENOMEM;
  131. goto error;
  132. }
  133. event_array[event_array_pages] = array_page;
  134. }
  135. /* Mask all events in this page before adding it. */
  136. init_array_page(array_page);
  137. expand_array.array_gfn = virt_to_gfn(array_page);
  138. ret = HYPERVISOR_event_channel_op(EVTCHNOP_expand_array, &expand_array);
  139. if (ret < 0)
  140. goto error;
  141. event_array_pages++;
  142. }
  143. return 0;
  144. error:
  145. if (event_array_pages == 0)
  146. panic("xen: unable to expand event array with initial page (%d)\n", ret);
  147. else
  148. pr_err("unable to expand event array (%d)\n", ret);
  149. free_unused_array_pages();
  150. return ret;
  151. }
  152. static void evtchn_fifo_bind_to_cpu(evtchn_port_t evtchn, unsigned int cpu,
  153. unsigned int old_cpu)
  154. {
  155. /* no-op */
  156. }
  157. static void evtchn_fifo_clear_pending(evtchn_port_t port)
  158. {
  159. event_word_t *word = event_word_from_port(port);
  160. sync_clear_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
  161. }
  162. static void evtchn_fifo_set_pending(evtchn_port_t port)
  163. {
  164. event_word_t *word = event_word_from_port(port);
  165. sync_set_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
  166. }
  167. static bool evtchn_fifo_is_pending(evtchn_port_t port)
  168. {
  169. event_word_t *word = event_word_from_port(port);
  170. return sync_test_bit(EVTCHN_FIFO_BIT(PENDING, word), BM(word));
  171. }
  172. static void evtchn_fifo_mask(evtchn_port_t port)
  173. {
  174. event_word_t *word = event_word_from_port(port);
  175. sync_set_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
  176. }
  177. static bool evtchn_fifo_is_masked(evtchn_port_t port)
  178. {
  179. event_word_t *word = event_word_from_port(port);
  180. return sync_test_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
  181. }
  182. /*
  183. * Clear MASKED if not PENDING, spinning if BUSY is set.
  184. * Return true if mask was cleared.
  185. */
  186. static bool clear_masked_cond(volatile event_word_t *word)
  187. {
  188. event_word_t new, old, w;
  189. w = *word;
  190. do {
  191. if (!(w & (1 << EVTCHN_FIFO_MASKED)))
  192. return true;
  193. if (w & (1 << EVTCHN_FIFO_PENDING))
  194. return false;
  195. old = w & ~(1 << EVTCHN_FIFO_BUSY);
  196. new = old & ~(1 << EVTCHN_FIFO_MASKED);
  197. w = sync_cmpxchg(word, old, new);
  198. } while (w != old);
  199. return true;
  200. }
  201. static void evtchn_fifo_unmask(evtchn_port_t port)
  202. {
  203. event_word_t *word = event_word_from_port(port);
  204. BUG_ON(!irqs_disabled());
  205. if (!clear_masked_cond(word)) {
  206. struct evtchn_unmask unmask = { .port = port };
  207. (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
  208. }
  209. }
  210. static uint32_t clear_linked(volatile event_word_t *word)
  211. {
  212. event_word_t new, old, w;
  213. w = *word;
  214. do {
  215. old = w;
  216. new = (w & ~((1 << EVTCHN_FIFO_LINKED)
  217. | EVTCHN_FIFO_LINK_MASK));
  218. } while ((w = sync_cmpxchg(word, old, new)) != old);
  219. return w & EVTCHN_FIFO_LINK_MASK;
  220. }
  221. static void consume_one_event(unsigned cpu, struct evtchn_loop_ctrl *ctrl,
  222. struct evtchn_fifo_control_block *control_block,
  223. unsigned priority, unsigned long *ready)
  224. {
  225. struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
  226. uint32_t head;
  227. evtchn_port_t port;
  228. event_word_t *word;
  229. head = q->head[priority];
  230. /*
  231. * Reached the tail last time? Read the new HEAD from the
  232. * control block.
  233. */
  234. if (head == 0) {
  235. virt_rmb(); /* Ensure word is up-to-date before reading head. */
  236. head = control_block->head[priority];
  237. }
  238. port = head;
  239. word = event_word_from_port(port);
  240. head = clear_linked(word);
  241. /*
  242. * If the link is non-zero, there are more events in the
  243. * queue, otherwise the queue is empty.
  244. *
  245. * If the queue is empty, clear this priority from our local
  246. * copy of the ready word.
  247. */
  248. if (head == 0)
  249. clear_bit(priority, ready);
  250. if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port)) {
  251. if (unlikely(!ctrl))
  252. pr_warn("Dropping pending event for port %u\n", port);
  253. else
  254. handle_irq_for_port(port, ctrl);
  255. }
  256. q->head[priority] = head;
  257. }
  258. static void __evtchn_fifo_handle_events(unsigned cpu,
  259. struct evtchn_loop_ctrl *ctrl)
  260. {
  261. struct evtchn_fifo_control_block *control_block;
  262. unsigned long ready;
  263. unsigned q;
  264. control_block = per_cpu(cpu_control_block, cpu);
  265. ready = xchg(&control_block->ready, 0);
  266. while (ready) {
  267. q = find_first_bit(&ready, EVTCHN_FIFO_MAX_QUEUES);
  268. consume_one_event(cpu, ctrl, control_block, q, &ready);
  269. ready |= xchg(&control_block->ready, 0);
  270. }
  271. }
  272. static void evtchn_fifo_handle_events(unsigned cpu,
  273. struct evtchn_loop_ctrl *ctrl)
  274. {
  275. __evtchn_fifo_handle_events(cpu, ctrl);
  276. }
  277. static void evtchn_fifo_resume(void)
  278. {
  279. unsigned cpu;
  280. for_each_possible_cpu(cpu) {
  281. void *control_block = per_cpu(cpu_control_block, cpu);
  282. int ret;
  283. if (!control_block)
  284. continue;
  285. /*
  286. * If this CPU is offline, take the opportunity to
  287. * free the control block while it is not being
  288. * used.
  289. */
  290. if (!cpu_online(cpu)) {
  291. free_page((unsigned long)control_block);
  292. per_cpu(cpu_control_block, cpu) = NULL;
  293. continue;
  294. }
  295. ret = init_control_block(cpu, control_block);
  296. BUG_ON(ret < 0);
  297. }
  298. /*
  299. * The event array starts out as empty again and is extended
  300. * as normal when events are bound. The existing pages will
  301. * be reused.
  302. */
  303. event_array_pages = 0;
  304. }
  305. static int evtchn_fifo_alloc_control_block(unsigned cpu)
  306. {
  307. void *control_block = NULL;
  308. int ret = -ENOMEM;
  309. control_block = (void *)__get_free_page(GFP_KERNEL);
  310. if (control_block == NULL)
  311. goto error;
  312. ret = init_control_block(cpu, control_block);
  313. if (ret < 0)
  314. goto error;
  315. per_cpu(cpu_control_block, cpu) = control_block;
  316. return 0;
  317. error:
  318. free_page((unsigned long)control_block);
  319. return ret;
  320. }
  321. static int evtchn_fifo_percpu_init(unsigned int cpu)
  322. {
  323. if (!per_cpu(cpu_control_block, cpu))
  324. return evtchn_fifo_alloc_control_block(cpu);
  325. return 0;
  326. }
  327. static int evtchn_fifo_percpu_deinit(unsigned int cpu)
  328. {
  329. __evtchn_fifo_handle_events(cpu, NULL);
  330. return 0;
  331. }
  332. static const struct evtchn_ops evtchn_ops_fifo = {
  333. .max_channels = evtchn_fifo_max_channels,
  334. .nr_channels = evtchn_fifo_nr_channels,
  335. .setup = evtchn_fifo_setup,
  336. .bind_to_cpu = evtchn_fifo_bind_to_cpu,
  337. .clear_pending = evtchn_fifo_clear_pending,
  338. .set_pending = evtchn_fifo_set_pending,
  339. .is_pending = evtchn_fifo_is_pending,
  340. .mask = evtchn_fifo_mask,
  341. .unmask = evtchn_fifo_unmask,
  342. .handle_events = evtchn_fifo_handle_events,
  343. .resume = evtchn_fifo_resume,
  344. .percpu_init = evtchn_fifo_percpu_init,
  345. .percpu_deinit = evtchn_fifo_percpu_deinit,
  346. };
  347. int __init xen_evtchn_fifo_init(void)
  348. {
  349. int cpu = smp_processor_id();
  350. int ret;
  351. ret = evtchn_fifo_alloc_control_block(cpu);
  352. if (ret < 0)
  353. return ret;
  354. pr_info("Using FIFO-based ABI\n");
  355. evtchn_ops = &evtchn_ops_fifo;
  356. return ret;
  357. }