events.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * Xen event channels
  3. *
  4. * Xen models interrupts with abstract event channels. Because each
  5. * domain gets 1024 event channels, but NR_IRQ is not that large, we
  6. * must dynamically map irqs<->event channels. The event channels
  7. * interface with the rest of the kernel by defining a xen interrupt
  8. * chip. When an event is recieved, it is mapped to an irq and sent
  9. * through the normal interrupt processing path.
  10. *
  11. * There are four kinds of events which can be mapped to an event
  12. * channel:
  13. *
  14. * 1. Inter-domain notifications. This includes all the virtual
  15. * device events, since they're driven by front-ends in another domain
  16. * (typically dom0).
  17. * 2. VIRQs, typically used for timers. These are per-cpu events.
  18. * 3. IPIs.
  19. * 4. Hardware interrupts. Not supported at present.
  20. *
  21. * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  22. */
  23. #include <linux/linkage.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irq.h>
  26. #include <linux/module.h>
  27. #include <linux/string.h>
  28. #include <asm/ptrace.h>
  29. #include <asm/irq.h>
  30. #include <asm/sync_bitops.h>
  31. #include <asm/xen/hypercall.h>
  32. #include <xen/events.h>
  33. #include <xen/interface/xen.h>
  34. #include <xen/interface/event_channel.h>
  35. #include "xen-ops.h"
  36. /*
  37. * This lock protects updates to the following mapping and reference-count
  38. * arrays. The lock does not need to be acquired to read the mapping tables.
  39. */
  40. static DEFINE_SPINLOCK(irq_mapping_update_lock);
  41. /* IRQ <-> VIRQ mapping. */
  42. static DEFINE_PER_CPU(int, virq_to_irq[NR_VIRQS]) = {[0 ... NR_VIRQS-1] = -1};
  43. /* IRQ <-> IPI mapping */
  44. static DEFINE_PER_CPU(int, ipi_to_irq[XEN_NR_IPIS]) = {[0 ... XEN_NR_IPIS-1] = -1};
  45. /* Packed IRQ information: binding type, sub-type index, and event channel. */
  46. struct packed_irq
  47. {
  48. unsigned short evtchn;
  49. unsigned char index;
  50. unsigned char type;
  51. };
  52. static struct packed_irq irq_info[NR_IRQS];
  53. /* Binding types. */
  54. enum {
  55. IRQT_UNBOUND,
  56. IRQT_PIRQ,
  57. IRQT_VIRQ,
  58. IRQT_IPI,
  59. IRQT_EVTCHN
  60. };
  61. /* Convenient shorthand for packed representation of an unbound IRQ. */
  62. #define IRQ_UNBOUND mk_irq_info(IRQT_UNBOUND, 0, 0)
  63. static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
  64. [0 ... NR_EVENT_CHANNELS-1] = -1
  65. };
  66. static unsigned long cpu_evtchn_mask[NR_CPUS][NR_EVENT_CHANNELS/BITS_PER_LONG];
  67. static u8 cpu_evtchn[NR_EVENT_CHANNELS];
  68. /* Reference counts for bindings to IRQs. */
  69. static int irq_bindcount[NR_IRQS];
  70. /* Xen will never allocate port zero for any purpose. */
  71. #define VALID_EVTCHN(chn) ((chn) != 0)
  72. /*
  73. * Force a proper event-channel callback from Xen after clearing the
  74. * callback mask. We do this in a very simple manner, by making a call
  75. * down into Xen. The pending flag will be checked by Xen on return.
  76. */
  77. void force_evtchn_callback(void)
  78. {
  79. (void)HYPERVISOR_xen_version(0, NULL);
  80. }
  81. EXPORT_SYMBOL_GPL(force_evtchn_callback);
  82. static struct irq_chip xen_dynamic_chip;
  83. /* Constructor for packed IRQ information. */
  84. static inline struct packed_irq mk_irq_info(u32 type, u32 index, u32 evtchn)
  85. {
  86. return (struct packed_irq) { evtchn, index, type };
  87. }
  88. /*
  89. * Accessors for packed IRQ information.
  90. */
  91. static inline unsigned int evtchn_from_irq(int irq)
  92. {
  93. return irq_info[irq].evtchn;
  94. }
  95. static inline unsigned int index_from_irq(int irq)
  96. {
  97. return irq_info[irq].index;
  98. }
  99. static inline unsigned int type_from_irq(int irq)
  100. {
  101. return irq_info[irq].type;
  102. }
  103. static inline unsigned long active_evtchns(unsigned int cpu,
  104. struct shared_info *sh,
  105. unsigned int idx)
  106. {
  107. return (sh->evtchn_pending[idx] &
  108. cpu_evtchn_mask[cpu][idx] &
  109. ~sh->evtchn_mask[idx]);
  110. }
  111. static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
  112. {
  113. int irq = evtchn_to_irq[chn];
  114. BUG_ON(irq == -1);
  115. #ifdef CONFIG_SMP
  116. irq_desc[irq].affinity = cpumask_of_cpu(cpu);
  117. #endif
  118. __clear_bit(chn, cpu_evtchn_mask[cpu_evtchn[chn]]);
  119. __set_bit(chn, cpu_evtchn_mask[cpu]);
  120. cpu_evtchn[chn] = cpu;
  121. }
  122. static void init_evtchn_cpu_bindings(void)
  123. {
  124. #ifdef CONFIG_SMP
  125. int i;
  126. /* By default all event channels notify CPU#0. */
  127. for (i = 0; i < NR_IRQS; i++)
  128. irq_desc[i].affinity = cpumask_of_cpu(0);
  129. #endif
  130. memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
  131. memset(cpu_evtchn_mask[0], ~0, sizeof(cpu_evtchn_mask[0]));
  132. }
  133. static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
  134. {
  135. return cpu_evtchn[evtchn];
  136. }
  137. static inline void clear_evtchn(int port)
  138. {
  139. struct shared_info *s = HYPERVISOR_shared_info;
  140. sync_clear_bit(port, &s->evtchn_pending[0]);
  141. }
  142. static inline void set_evtchn(int port)
  143. {
  144. struct shared_info *s = HYPERVISOR_shared_info;
  145. sync_set_bit(port, &s->evtchn_pending[0]);
  146. }
  147. /**
  148. * notify_remote_via_irq - send event to remote end of event channel via irq
  149. * @irq: irq of event channel to send event to
  150. *
  151. * Unlike notify_remote_via_evtchn(), this is safe to use across
  152. * save/restore. Notifications on a broken connection are silently
  153. * dropped.
  154. */
  155. void notify_remote_via_irq(int irq)
  156. {
  157. int evtchn = evtchn_from_irq(irq);
  158. if (VALID_EVTCHN(evtchn))
  159. notify_remote_via_evtchn(evtchn);
  160. }
  161. EXPORT_SYMBOL_GPL(notify_remote_via_irq);
  162. static void mask_evtchn(int port)
  163. {
  164. struct shared_info *s = HYPERVISOR_shared_info;
  165. sync_set_bit(port, &s->evtchn_mask[0]);
  166. }
  167. static void unmask_evtchn(int port)
  168. {
  169. struct shared_info *s = HYPERVISOR_shared_info;
  170. unsigned int cpu = get_cpu();
  171. BUG_ON(!irqs_disabled());
  172. /* Slow path (hypercall) if this is a non-local port. */
  173. if (unlikely(cpu != cpu_from_evtchn(port))) {
  174. struct evtchn_unmask unmask = { .port = port };
  175. (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
  176. } else {
  177. struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
  178. sync_clear_bit(port, &s->evtchn_mask[0]);
  179. /*
  180. * The following is basically the equivalent of
  181. * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
  182. * the interrupt edge' if the channel is masked.
  183. */
  184. if (sync_test_bit(port, &s->evtchn_pending[0]) &&
  185. !sync_test_and_set_bit(port / BITS_PER_LONG,
  186. &vcpu_info->evtchn_pending_sel))
  187. vcpu_info->evtchn_upcall_pending = 1;
  188. }
  189. put_cpu();
  190. }
  191. static int find_unbound_irq(void)
  192. {
  193. int irq;
  194. /* Only allocate from dynirq range */
  195. for (irq = 0; irq < NR_IRQS; irq++)
  196. if (irq_bindcount[irq] == 0)
  197. break;
  198. if (irq == NR_IRQS)
  199. panic("No available IRQ to bind to: increase NR_IRQS!\n");
  200. return irq;
  201. }
  202. int bind_evtchn_to_irq(unsigned int evtchn)
  203. {
  204. int irq;
  205. spin_lock(&irq_mapping_update_lock);
  206. irq = evtchn_to_irq[evtchn];
  207. if (irq == -1) {
  208. irq = find_unbound_irq();
  209. dynamic_irq_init(irq);
  210. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  211. handle_level_irq, "event");
  212. evtchn_to_irq[evtchn] = irq;
  213. irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
  214. }
  215. irq_bindcount[irq]++;
  216. spin_unlock(&irq_mapping_update_lock);
  217. return irq;
  218. }
  219. EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
  220. static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
  221. {
  222. struct evtchn_bind_ipi bind_ipi;
  223. int evtchn, irq;
  224. spin_lock(&irq_mapping_update_lock);
  225. irq = per_cpu(ipi_to_irq, cpu)[ipi];
  226. if (irq == -1) {
  227. irq = find_unbound_irq();
  228. if (irq < 0)
  229. goto out;
  230. dynamic_irq_init(irq);
  231. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  232. handle_level_irq, "ipi");
  233. bind_ipi.vcpu = cpu;
  234. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  235. &bind_ipi) != 0)
  236. BUG();
  237. evtchn = bind_ipi.port;
  238. evtchn_to_irq[evtchn] = irq;
  239. irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
  240. per_cpu(ipi_to_irq, cpu)[ipi] = irq;
  241. bind_evtchn_to_cpu(evtchn, cpu);
  242. }
  243. irq_bindcount[irq]++;
  244. out:
  245. spin_unlock(&irq_mapping_update_lock);
  246. return irq;
  247. }
  248. static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
  249. {
  250. struct evtchn_bind_virq bind_virq;
  251. int evtchn, irq;
  252. spin_lock(&irq_mapping_update_lock);
  253. irq = per_cpu(virq_to_irq, cpu)[virq];
  254. if (irq == -1) {
  255. bind_virq.virq = virq;
  256. bind_virq.vcpu = cpu;
  257. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  258. &bind_virq) != 0)
  259. BUG();
  260. evtchn = bind_virq.port;
  261. irq = find_unbound_irq();
  262. dynamic_irq_init(irq);
  263. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  264. handle_level_irq, "virq");
  265. evtchn_to_irq[evtchn] = irq;
  266. irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
  267. per_cpu(virq_to_irq, cpu)[virq] = irq;
  268. bind_evtchn_to_cpu(evtchn, cpu);
  269. }
  270. irq_bindcount[irq]++;
  271. spin_unlock(&irq_mapping_update_lock);
  272. return irq;
  273. }
  274. static void unbind_from_irq(unsigned int irq)
  275. {
  276. struct evtchn_close close;
  277. int evtchn = evtchn_from_irq(irq);
  278. spin_lock(&irq_mapping_update_lock);
  279. if (VALID_EVTCHN(evtchn) && (--irq_bindcount[irq] == 0)) {
  280. close.port = evtchn;
  281. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  282. BUG();
  283. switch (type_from_irq(irq)) {
  284. case IRQT_VIRQ:
  285. per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
  286. [index_from_irq(irq)] = -1;
  287. break;
  288. default:
  289. break;
  290. }
  291. /* Closed ports are implicitly re-bound to VCPU0. */
  292. bind_evtchn_to_cpu(evtchn, 0);
  293. evtchn_to_irq[evtchn] = -1;
  294. irq_info[irq] = IRQ_UNBOUND;
  295. dynamic_irq_init(irq);
  296. }
  297. spin_unlock(&irq_mapping_update_lock);
  298. }
  299. int bind_evtchn_to_irqhandler(unsigned int evtchn,
  300. irqreturn_t (*handler)(int, void *),
  301. unsigned long irqflags,
  302. const char *devname, void *dev_id)
  303. {
  304. unsigned int irq;
  305. int retval;
  306. irq = bind_evtchn_to_irq(evtchn);
  307. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  308. if (retval != 0) {
  309. unbind_from_irq(irq);
  310. return retval;
  311. }
  312. return irq;
  313. }
  314. EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
  315. int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
  316. irqreturn_t (*handler)(int, void *),
  317. unsigned long irqflags, const char *devname, void *dev_id)
  318. {
  319. unsigned int irq;
  320. int retval;
  321. irq = bind_virq_to_irq(virq, cpu);
  322. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  323. if (retval != 0) {
  324. unbind_from_irq(irq);
  325. return retval;
  326. }
  327. return irq;
  328. }
  329. EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
  330. int bind_ipi_to_irqhandler(enum ipi_vector ipi,
  331. unsigned int cpu,
  332. irq_handler_t handler,
  333. unsigned long irqflags,
  334. const char *devname,
  335. void *dev_id)
  336. {
  337. int irq, retval;
  338. irq = bind_ipi_to_irq(ipi, cpu);
  339. if (irq < 0)
  340. return irq;
  341. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  342. if (retval != 0) {
  343. unbind_from_irq(irq);
  344. return retval;
  345. }
  346. return irq;
  347. }
  348. void unbind_from_irqhandler(unsigned int irq, void *dev_id)
  349. {
  350. free_irq(irq, dev_id);
  351. unbind_from_irq(irq);
  352. }
  353. EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
  354. void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
  355. {
  356. int irq = per_cpu(ipi_to_irq, cpu)[vector];
  357. BUG_ON(irq < 0);
  358. notify_remote_via_irq(irq);
  359. }
  360. /*
  361. * Search the CPUs pending events bitmasks. For each one found, map
  362. * the event number to an irq, and feed it into do_IRQ() for
  363. * handling.
  364. *
  365. * Xen uses a two-level bitmap to speed searching. The first level is
  366. * a bitset of words which contain pending event bits. The second
  367. * level is a bitset of pending events themselves.
  368. */
  369. fastcall void xen_evtchn_do_upcall(struct pt_regs *regs)
  370. {
  371. int cpu = get_cpu();
  372. struct shared_info *s = HYPERVISOR_shared_info;
  373. struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
  374. unsigned long pending_words;
  375. vcpu_info->evtchn_upcall_pending = 0;
  376. /* NB. No need for a barrier here -- XCHG is a barrier on x86. */
  377. pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
  378. while (pending_words != 0) {
  379. unsigned long pending_bits;
  380. int word_idx = __ffs(pending_words);
  381. pending_words &= ~(1UL << word_idx);
  382. while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
  383. int bit_idx = __ffs(pending_bits);
  384. int port = (word_idx * BITS_PER_LONG) + bit_idx;
  385. int irq = evtchn_to_irq[port];
  386. if (irq != -1) {
  387. regs->orig_eax = ~irq;
  388. do_IRQ(regs);
  389. }
  390. }
  391. }
  392. put_cpu();
  393. }
  394. /* Rebind an evtchn so that it gets delivered to a specific cpu */
  395. static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
  396. {
  397. struct evtchn_bind_vcpu bind_vcpu;
  398. int evtchn = evtchn_from_irq(irq);
  399. if (!VALID_EVTCHN(evtchn))
  400. return;
  401. /* Send future instances of this interrupt to other vcpu. */
  402. bind_vcpu.port = evtchn;
  403. bind_vcpu.vcpu = tcpu;
  404. /*
  405. * If this fails, it usually just indicates that we're dealing with a
  406. * virq or IPI channel, which don't actually need to be rebound. Ignore
  407. * it, but don't do the xenlinux-level rebind in that case.
  408. */
  409. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
  410. bind_evtchn_to_cpu(evtchn, tcpu);
  411. }
  412. static void set_affinity_irq(unsigned irq, cpumask_t dest)
  413. {
  414. unsigned tcpu = first_cpu(dest);
  415. rebind_irq_to_cpu(irq, tcpu);
  416. }
  417. static void enable_dynirq(unsigned int irq)
  418. {
  419. int evtchn = evtchn_from_irq(irq);
  420. if (VALID_EVTCHN(evtchn))
  421. unmask_evtchn(evtchn);
  422. }
  423. static void disable_dynirq(unsigned int irq)
  424. {
  425. int evtchn = evtchn_from_irq(irq);
  426. if (VALID_EVTCHN(evtchn))
  427. mask_evtchn(evtchn);
  428. }
  429. static void ack_dynirq(unsigned int irq)
  430. {
  431. int evtchn = evtchn_from_irq(irq);
  432. move_native_irq(irq);
  433. if (VALID_EVTCHN(evtchn))
  434. clear_evtchn(evtchn);
  435. }
  436. static int retrigger_dynirq(unsigned int irq)
  437. {
  438. int evtchn = evtchn_from_irq(irq);
  439. int ret = 0;
  440. if (VALID_EVTCHN(evtchn)) {
  441. set_evtchn(evtchn);
  442. ret = 1;
  443. }
  444. return ret;
  445. }
  446. static struct irq_chip xen_dynamic_chip __read_mostly = {
  447. .name = "xen-dyn",
  448. .mask = disable_dynirq,
  449. .unmask = enable_dynirq,
  450. .ack = ack_dynirq,
  451. .set_affinity = set_affinity_irq,
  452. .retrigger = retrigger_dynirq,
  453. };
  454. void __init xen_init_IRQ(void)
  455. {
  456. int i;
  457. init_evtchn_cpu_bindings();
  458. /* No event channels are 'live' right now. */
  459. for (i = 0; i < NR_EVENT_CHANNELS; i++)
  460. mask_evtchn(i);
  461. /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
  462. for (i = 0; i < NR_IRQS; i++)
  463. irq_bindcount[i] = 0;
  464. irq_ctx_init(smp_processor_id());
  465. }