events_2l.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xen event channels (2-level ABI)
  4. *
  5. * Jeremy Fitzhardinge <[email protected]>, XenSource Inc, 2007
  6. */
  7. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  8. #include <linux/linkage.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irq.h>
  11. #include <asm/sync_bitops.h>
  12. #include <asm/xen/hypercall.h>
  13. #include <asm/xen/hypervisor.h>
  14. #include <xen/xen.h>
  15. #include <xen/xen-ops.h>
  16. #include <xen/events.h>
  17. #include <xen/interface/xen.h>
  18. #include <xen/interface/event_channel.h>
  19. #include "events_internal.h"
  20. /*
  21. * Note sizeof(xen_ulong_t) can be more than sizeof(unsigned long). Be
  22. * careful to only use bitops which allow for this (e.g
  23. * test_bit/find_first_bit and friends but not __ffs) and to pass
  24. * BITS_PER_EVTCHN_WORD as the bitmask length.
  25. */
  26. #define BITS_PER_EVTCHN_WORD (sizeof(xen_ulong_t)*8)
  27. /*
  28. * Make a bitmask (i.e. unsigned long *) of a xen_ulong_t
  29. * array. Primarily to avoid long lines (hence the terse name).
  30. */
  31. #define BM(x) (unsigned long *)(x)
  32. /* Find the first set bit in a evtchn mask */
  33. #define EVTCHN_FIRST_BIT(w) find_first_bit(BM(&(w)), BITS_PER_EVTCHN_WORD)
  34. #define EVTCHN_MASK_SIZE (EVTCHN_2L_NR_CHANNELS/BITS_PER_EVTCHN_WORD)
  35. static DEFINE_PER_CPU(xen_ulong_t [EVTCHN_MASK_SIZE], cpu_evtchn_mask);
  36. static unsigned evtchn_2l_max_channels(void)
  37. {
  38. return EVTCHN_2L_NR_CHANNELS;
  39. }
  40. static void evtchn_2l_remove(evtchn_port_t evtchn, unsigned int cpu)
  41. {
  42. clear_bit(evtchn, BM(per_cpu(cpu_evtchn_mask, cpu)));
  43. }
  44. static void evtchn_2l_bind_to_cpu(evtchn_port_t evtchn, unsigned int cpu,
  45. unsigned int old_cpu)
  46. {
  47. clear_bit(evtchn, BM(per_cpu(cpu_evtchn_mask, old_cpu)));
  48. set_bit(evtchn, BM(per_cpu(cpu_evtchn_mask, cpu)));
  49. }
  50. static void evtchn_2l_clear_pending(evtchn_port_t port)
  51. {
  52. struct shared_info *s = HYPERVISOR_shared_info;
  53. sync_clear_bit(port, BM(&s->evtchn_pending[0]));
  54. }
  55. static void evtchn_2l_set_pending(evtchn_port_t port)
  56. {
  57. struct shared_info *s = HYPERVISOR_shared_info;
  58. sync_set_bit(port, BM(&s->evtchn_pending[0]));
  59. }
  60. static bool evtchn_2l_is_pending(evtchn_port_t port)
  61. {
  62. struct shared_info *s = HYPERVISOR_shared_info;
  63. return sync_test_bit(port, BM(&s->evtchn_pending[0]));
  64. }
  65. static void evtchn_2l_mask(evtchn_port_t port)
  66. {
  67. struct shared_info *s = HYPERVISOR_shared_info;
  68. sync_set_bit(port, BM(&s->evtchn_mask[0]));
  69. }
  70. static void evtchn_2l_unmask(evtchn_port_t port)
  71. {
  72. struct shared_info *s = HYPERVISOR_shared_info;
  73. unsigned int cpu = get_cpu();
  74. int do_hypercall = 0, evtchn_pending = 0;
  75. BUG_ON(!irqs_disabled());
  76. smp_wmb(); /* All writes before unmask must be visible. */
  77. if (unlikely((cpu != cpu_from_evtchn(port))))
  78. do_hypercall = 1;
  79. else {
  80. /*
  81. * Need to clear the mask before checking pending to
  82. * avoid a race with an event becoming pending.
  83. *
  84. * EVTCHNOP_unmask will only trigger an upcall if the
  85. * mask bit was set, so if a hypercall is needed
  86. * remask the event.
  87. */
  88. sync_clear_bit(port, BM(&s->evtchn_mask[0]));
  89. evtchn_pending = sync_test_bit(port, BM(&s->evtchn_pending[0]));
  90. if (unlikely(evtchn_pending && xen_hvm_domain())) {
  91. sync_set_bit(port, BM(&s->evtchn_mask[0]));
  92. do_hypercall = 1;
  93. }
  94. }
  95. /* Slow path (hypercall) if this is a non-local port or if this is
  96. * an hvm domain and an event is pending (hvm domains don't have
  97. * their own implementation of irq_enable). */
  98. if (do_hypercall) {
  99. struct evtchn_unmask unmask = { .port = port };
  100. (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
  101. } else {
  102. struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
  103. /*
  104. * The following is basically the equivalent of
  105. * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
  106. * the interrupt edge' if the channel is masked.
  107. */
  108. if (evtchn_pending &&
  109. !sync_test_and_set_bit(port / BITS_PER_EVTCHN_WORD,
  110. BM(&vcpu_info->evtchn_pending_sel)))
  111. vcpu_info->evtchn_upcall_pending = 1;
  112. }
  113. put_cpu();
  114. }
  115. static DEFINE_PER_CPU(unsigned int, current_word_idx);
  116. static DEFINE_PER_CPU(unsigned int, current_bit_idx);
  117. /*
  118. * Mask out the i least significant bits of w
  119. */
  120. #define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
  121. static inline xen_ulong_t active_evtchns(unsigned int cpu,
  122. struct shared_info *sh,
  123. unsigned int idx)
  124. {
  125. return sh->evtchn_pending[idx] &
  126. per_cpu(cpu_evtchn_mask, cpu)[idx] &
  127. ~sh->evtchn_mask[idx];
  128. }
  129. /*
  130. * Search the CPU's pending events bitmasks. For each one found, map
  131. * the event number to an irq, and feed it into do_IRQ() for handling.
  132. *
  133. * Xen uses a two-level bitmap to speed searching. The first level is
  134. * a bitset of words which contain pending event bits. The second
  135. * level is a bitset of pending events themselves.
  136. */
  137. static void evtchn_2l_handle_events(unsigned cpu, struct evtchn_loop_ctrl *ctrl)
  138. {
  139. int irq;
  140. xen_ulong_t pending_words;
  141. xen_ulong_t pending_bits;
  142. int start_word_idx, start_bit_idx;
  143. int word_idx, bit_idx;
  144. int i;
  145. struct shared_info *s = HYPERVISOR_shared_info;
  146. struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
  147. /* Timer interrupt has highest priority. */
  148. irq = irq_from_virq(cpu, VIRQ_TIMER);
  149. if (irq != -1) {
  150. evtchn_port_t evtchn = evtchn_from_irq(irq);
  151. word_idx = evtchn / BITS_PER_LONG;
  152. bit_idx = evtchn % BITS_PER_LONG;
  153. if (active_evtchns(cpu, s, word_idx) & (1ULL << bit_idx))
  154. generic_handle_irq(irq);
  155. }
  156. /*
  157. * Master flag must be cleared /before/ clearing
  158. * selector flag. xchg_xen_ulong must contain an
  159. * appropriate barrier.
  160. */
  161. pending_words = xchg_xen_ulong(&vcpu_info->evtchn_pending_sel, 0);
  162. start_word_idx = __this_cpu_read(current_word_idx);
  163. start_bit_idx = __this_cpu_read(current_bit_idx);
  164. word_idx = start_word_idx;
  165. for (i = 0; pending_words != 0; i++) {
  166. xen_ulong_t words;
  167. words = MASK_LSBS(pending_words, word_idx);
  168. /*
  169. * If we masked out all events, wrap to beginning.
  170. */
  171. if (words == 0) {
  172. word_idx = 0;
  173. bit_idx = 0;
  174. continue;
  175. }
  176. word_idx = EVTCHN_FIRST_BIT(words);
  177. pending_bits = active_evtchns(cpu, s, word_idx);
  178. bit_idx = 0; /* usually scan entire word from start */
  179. /*
  180. * We scan the starting word in two parts.
  181. *
  182. * 1st time: start in the middle, scanning the
  183. * upper bits.
  184. *
  185. * 2nd time: scan the whole word (not just the
  186. * parts skipped in the first pass) -- if an
  187. * event in the previously scanned bits is
  188. * pending again it would just be scanned on
  189. * the next loop anyway.
  190. */
  191. if (word_idx == start_word_idx) {
  192. if (i == 0)
  193. bit_idx = start_bit_idx;
  194. }
  195. do {
  196. xen_ulong_t bits;
  197. evtchn_port_t port;
  198. bits = MASK_LSBS(pending_bits, bit_idx);
  199. /* If we masked out all events, move on. */
  200. if (bits == 0)
  201. break;
  202. bit_idx = EVTCHN_FIRST_BIT(bits);
  203. /* Process port. */
  204. port = (word_idx * BITS_PER_EVTCHN_WORD) + bit_idx;
  205. handle_irq_for_port(port, ctrl);
  206. bit_idx = (bit_idx + 1) % BITS_PER_EVTCHN_WORD;
  207. /* Next caller starts at last processed + 1 */
  208. __this_cpu_write(current_word_idx,
  209. bit_idx ? word_idx :
  210. (word_idx+1) % BITS_PER_EVTCHN_WORD);
  211. __this_cpu_write(current_bit_idx, bit_idx);
  212. } while (bit_idx != 0);
  213. /* Scan start_l1i twice; all others once. */
  214. if ((word_idx != start_word_idx) || (i != 0))
  215. pending_words &= ~(1UL << word_idx);
  216. word_idx = (word_idx + 1) % BITS_PER_EVTCHN_WORD;
  217. }
  218. }
  219. irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
  220. {
  221. struct shared_info *sh = HYPERVISOR_shared_info;
  222. int cpu = smp_processor_id();
  223. xen_ulong_t *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
  224. int i;
  225. unsigned long flags;
  226. static DEFINE_SPINLOCK(debug_lock);
  227. struct vcpu_info *v;
  228. spin_lock_irqsave(&debug_lock, flags);
  229. printk("\nvcpu %d\n ", cpu);
  230. for_each_online_cpu(i) {
  231. int pending;
  232. v = per_cpu(xen_vcpu, i);
  233. pending = (get_irq_regs() && i == cpu)
  234. ? xen_irqs_disabled(get_irq_regs())
  235. : v->evtchn_upcall_mask;
  236. printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong"\n ", i,
  237. pending, v->evtchn_upcall_pending,
  238. (int)(sizeof(v->evtchn_pending_sel)*2),
  239. v->evtchn_pending_sel);
  240. }
  241. v = per_cpu(xen_vcpu, cpu);
  242. printk("\npending:\n ");
  243. for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
  244. printk("%0*"PRI_xen_ulong"%s",
  245. (int)sizeof(sh->evtchn_pending[0])*2,
  246. sh->evtchn_pending[i],
  247. i % 8 == 0 ? "\n " : " ");
  248. printk("\nglobal mask:\n ");
  249. for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  250. printk("%0*"PRI_xen_ulong"%s",
  251. (int)(sizeof(sh->evtchn_mask[0])*2),
  252. sh->evtchn_mask[i],
  253. i % 8 == 0 ? "\n " : " ");
  254. printk("\nglobally unmasked:\n ");
  255. for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  256. printk("%0*"PRI_xen_ulong"%s",
  257. (int)(sizeof(sh->evtchn_mask[0])*2),
  258. sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
  259. i % 8 == 0 ? "\n " : " ");
  260. printk("\nlocal cpu%d mask:\n ", cpu);
  261. for (i = (EVTCHN_2L_NR_CHANNELS/BITS_PER_EVTCHN_WORD)-1; i >= 0; i--)
  262. printk("%0*"PRI_xen_ulong"%s", (int)(sizeof(cpu_evtchn[0])*2),
  263. cpu_evtchn[i],
  264. i % 8 == 0 ? "\n " : " ");
  265. printk("\nlocally unmasked:\n ");
  266. for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
  267. xen_ulong_t pending = sh->evtchn_pending[i]
  268. & ~sh->evtchn_mask[i]
  269. & cpu_evtchn[i];
  270. printk("%0*"PRI_xen_ulong"%s",
  271. (int)(sizeof(sh->evtchn_mask[0])*2),
  272. pending, i % 8 == 0 ? "\n " : " ");
  273. }
  274. printk("\npending list:\n");
  275. for (i = 0; i < EVTCHN_2L_NR_CHANNELS; i++) {
  276. if (sync_test_bit(i, BM(sh->evtchn_pending))) {
  277. int word_idx = i / BITS_PER_EVTCHN_WORD;
  278. printk(" %d: event %d -> irq %d%s%s%s\n",
  279. cpu_from_evtchn(i), i,
  280. get_evtchn_to_irq(i),
  281. sync_test_bit(word_idx, BM(&v->evtchn_pending_sel))
  282. ? "" : " l2-clear",
  283. !sync_test_bit(i, BM(sh->evtchn_mask))
  284. ? "" : " globally-masked",
  285. sync_test_bit(i, BM(cpu_evtchn))
  286. ? "" : " locally-masked");
  287. }
  288. }
  289. spin_unlock_irqrestore(&debug_lock, flags);
  290. return IRQ_HANDLED;
  291. }
  292. static void evtchn_2l_resume(void)
  293. {
  294. int i;
  295. for_each_online_cpu(i)
  296. memset(per_cpu(cpu_evtchn_mask, i), 0, sizeof(xen_ulong_t) *
  297. EVTCHN_2L_NR_CHANNELS/BITS_PER_EVTCHN_WORD);
  298. }
  299. static int evtchn_2l_percpu_deinit(unsigned int cpu)
  300. {
  301. memset(per_cpu(cpu_evtchn_mask, cpu), 0, sizeof(xen_ulong_t) *
  302. EVTCHN_2L_NR_CHANNELS/BITS_PER_EVTCHN_WORD);
  303. return 0;
  304. }
  305. static const struct evtchn_ops evtchn_ops_2l = {
  306. .max_channels = evtchn_2l_max_channels,
  307. .nr_channels = evtchn_2l_max_channels,
  308. .remove = evtchn_2l_remove,
  309. .bind_to_cpu = evtchn_2l_bind_to_cpu,
  310. .clear_pending = evtchn_2l_clear_pending,
  311. .set_pending = evtchn_2l_set_pending,
  312. .is_pending = evtchn_2l_is_pending,
  313. .mask = evtchn_2l_mask,
  314. .unmask = evtchn_2l_unmask,
  315. .handle_events = evtchn_2l_handle_events,
  316. .resume = evtchn_2l_resume,
  317. .percpu_deinit = evtchn_2l_percpu_deinit,
  318. };
  319. void __init xen_evtchn_2l_init(void)
  320. {
  321. pr_info("Using 2-level ABI\n");
  322. evtchn_ops = &evtchn_ops_2l;
  323. }