events.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 <linux/bootmem.h>
  29. #include <linux/slab.h>
  30. #include <asm/ptrace.h>
  31. #include <asm/irq.h>
  32. #include <asm/idle.h>
  33. #include <asm/sync_bitops.h>
  34. #include <asm/xen/hypercall.h>
  35. #include <asm/xen/hypervisor.h>
  36. #include <xen/xen-ops.h>
  37. #include <xen/events.h>
  38. #include <xen/interface/xen.h>
  39. #include <xen/interface/event_channel.h>
  40. /*
  41. * This lock protects updates to the following mapping and reference-count
  42. * arrays. The lock does not need to be acquired to read the mapping tables.
  43. */
  44. static DEFINE_SPINLOCK(irq_mapping_update_lock);
  45. /* IRQ <-> VIRQ mapping. */
  46. static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
  47. /* IRQ <-> IPI mapping */
  48. static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
  49. /* Interrupt types. */
  50. enum xen_irq_type {
  51. IRQT_UNBOUND = 0,
  52. IRQT_PIRQ,
  53. IRQT_VIRQ,
  54. IRQT_IPI,
  55. IRQT_EVTCHN
  56. };
  57. /*
  58. * Packed IRQ information:
  59. * type - enum xen_irq_type
  60. * event channel - irq->event channel mapping
  61. * cpu - cpu this event channel is bound to
  62. * index - type-specific information:
  63. * PIRQ - vector, with MSB being "needs EIO"
  64. * VIRQ - virq number
  65. * IPI - IPI vector
  66. * EVTCHN -
  67. */
  68. struct irq_info
  69. {
  70. enum xen_irq_type type; /* type */
  71. unsigned short evtchn; /* event channel */
  72. unsigned short cpu; /* cpu bound */
  73. union {
  74. unsigned short virq;
  75. enum ipi_vector ipi;
  76. struct {
  77. unsigned short gsi;
  78. unsigned short vector;
  79. } pirq;
  80. } u;
  81. };
  82. static struct irq_info irq_info[NR_IRQS];
  83. static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
  84. [0 ... NR_EVENT_CHANNELS-1] = -1
  85. };
  86. struct cpu_evtchn_s {
  87. unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
  88. };
  89. static struct cpu_evtchn_s *cpu_evtchn_mask_p;
  90. static inline unsigned long *cpu_evtchn_mask(int cpu)
  91. {
  92. return cpu_evtchn_mask_p[cpu].bits;
  93. }
  94. /* Xen will never allocate port zero for any purpose. */
  95. #define VALID_EVTCHN(chn) ((chn) != 0)
  96. static struct irq_chip xen_dynamic_chip;
  97. /* Constructor for packed IRQ information. */
  98. static struct irq_info mk_unbound_info(void)
  99. {
  100. return (struct irq_info) { .type = IRQT_UNBOUND };
  101. }
  102. static struct irq_info mk_evtchn_info(unsigned short evtchn)
  103. {
  104. return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
  105. .cpu = 0 };
  106. }
  107. static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
  108. {
  109. return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
  110. .cpu = 0, .u.ipi = ipi };
  111. }
  112. static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
  113. {
  114. return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
  115. .cpu = 0, .u.virq = virq };
  116. }
  117. static struct irq_info mk_pirq_info(unsigned short evtchn,
  118. unsigned short gsi, unsigned short vector)
  119. {
  120. return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
  121. .cpu = 0, .u.pirq = { .gsi = gsi, .vector = vector } };
  122. }
  123. /*
  124. * Accessors for packed IRQ information.
  125. */
  126. static struct irq_info *info_for_irq(unsigned irq)
  127. {
  128. return &irq_info[irq];
  129. }
  130. static unsigned int evtchn_from_irq(unsigned irq)
  131. {
  132. return info_for_irq(irq)->evtchn;
  133. }
  134. unsigned irq_from_evtchn(unsigned int evtchn)
  135. {
  136. return evtchn_to_irq[evtchn];
  137. }
  138. EXPORT_SYMBOL_GPL(irq_from_evtchn);
  139. static enum ipi_vector ipi_from_irq(unsigned irq)
  140. {
  141. struct irq_info *info = info_for_irq(irq);
  142. BUG_ON(info == NULL);
  143. BUG_ON(info->type != IRQT_IPI);
  144. return info->u.ipi;
  145. }
  146. static unsigned virq_from_irq(unsigned irq)
  147. {
  148. struct irq_info *info = info_for_irq(irq);
  149. BUG_ON(info == NULL);
  150. BUG_ON(info->type != IRQT_VIRQ);
  151. return info->u.virq;
  152. }
  153. static unsigned gsi_from_irq(unsigned irq)
  154. {
  155. struct irq_info *info = info_for_irq(irq);
  156. BUG_ON(info == NULL);
  157. BUG_ON(info->type != IRQT_PIRQ);
  158. return info->u.pirq.gsi;
  159. }
  160. static unsigned vector_from_irq(unsigned irq)
  161. {
  162. struct irq_info *info = info_for_irq(irq);
  163. BUG_ON(info == NULL);
  164. BUG_ON(info->type != IRQT_PIRQ);
  165. return info->u.pirq.vector;
  166. }
  167. static enum xen_irq_type type_from_irq(unsigned irq)
  168. {
  169. return info_for_irq(irq)->type;
  170. }
  171. static unsigned cpu_from_irq(unsigned irq)
  172. {
  173. return info_for_irq(irq)->cpu;
  174. }
  175. static unsigned int cpu_from_evtchn(unsigned int evtchn)
  176. {
  177. int irq = evtchn_to_irq[evtchn];
  178. unsigned ret = 0;
  179. if (irq != -1)
  180. ret = cpu_from_irq(irq);
  181. return ret;
  182. }
  183. static inline unsigned long active_evtchns(unsigned int cpu,
  184. struct shared_info *sh,
  185. unsigned int idx)
  186. {
  187. return (sh->evtchn_pending[idx] &
  188. cpu_evtchn_mask(cpu)[idx] &
  189. ~sh->evtchn_mask[idx]);
  190. }
  191. static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
  192. {
  193. int irq = evtchn_to_irq[chn];
  194. BUG_ON(irq == -1);
  195. #ifdef CONFIG_SMP
  196. cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
  197. #endif
  198. __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
  199. __set_bit(chn, cpu_evtchn_mask(cpu));
  200. irq_info[irq].cpu = cpu;
  201. }
  202. static void init_evtchn_cpu_bindings(void)
  203. {
  204. #ifdef CONFIG_SMP
  205. struct irq_desc *desc;
  206. int i;
  207. /* By default all event channels notify CPU#0. */
  208. for_each_irq_desc(i, desc) {
  209. cpumask_copy(desc->affinity, cpumask_of(0));
  210. }
  211. #endif
  212. memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0)));
  213. }
  214. static inline void clear_evtchn(int port)
  215. {
  216. struct shared_info *s = HYPERVISOR_shared_info;
  217. sync_clear_bit(port, &s->evtchn_pending[0]);
  218. }
  219. static inline void set_evtchn(int port)
  220. {
  221. struct shared_info *s = HYPERVISOR_shared_info;
  222. sync_set_bit(port, &s->evtchn_pending[0]);
  223. }
  224. static inline int test_evtchn(int port)
  225. {
  226. struct shared_info *s = HYPERVISOR_shared_info;
  227. return sync_test_bit(port, &s->evtchn_pending[0]);
  228. }
  229. /**
  230. * notify_remote_via_irq - send event to remote end of event channel via irq
  231. * @irq: irq of event channel to send event to
  232. *
  233. * Unlike notify_remote_via_evtchn(), this is safe to use across
  234. * save/restore. Notifications on a broken connection are silently
  235. * dropped.
  236. */
  237. void notify_remote_via_irq(int irq)
  238. {
  239. int evtchn = evtchn_from_irq(irq);
  240. if (VALID_EVTCHN(evtchn))
  241. notify_remote_via_evtchn(evtchn);
  242. }
  243. EXPORT_SYMBOL_GPL(notify_remote_via_irq);
  244. static void mask_evtchn(int port)
  245. {
  246. struct shared_info *s = HYPERVISOR_shared_info;
  247. sync_set_bit(port, &s->evtchn_mask[0]);
  248. }
  249. static void unmask_evtchn(int port)
  250. {
  251. struct shared_info *s = HYPERVISOR_shared_info;
  252. unsigned int cpu = get_cpu();
  253. BUG_ON(!irqs_disabled());
  254. /* Slow path (hypercall) if this is a non-local port. */
  255. if (unlikely(cpu != cpu_from_evtchn(port))) {
  256. struct evtchn_unmask unmask = { .port = port };
  257. (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
  258. } else {
  259. struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
  260. sync_clear_bit(port, &s->evtchn_mask[0]);
  261. /*
  262. * The following is basically the equivalent of
  263. * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
  264. * the interrupt edge' if the channel is masked.
  265. */
  266. if (sync_test_bit(port, &s->evtchn_pending[0]) &&
  267. !sync_test_and_set_bit(port / BITS_PER_LONG,
  268. &vcpu_info->evtchn_pending_sel))
  269. vcpu_info->evtchn_upcall_pending = 1;
  270. }
  271. put_cpu();
  272. }
  273. static int find_unbound_irq(void)
  274. {
  275. int irq;
  276. struct irq_desc *desc;
  277. for (irq = 0; irq < nr_irqs; irq++)
  278. if (irq_info[irq].type == IRQT_UNBOUND)
  279. break;
  280. if (irq == nr_irqs)
  281. panic("No available IRQ to bind to: increase nr_irqs!\n");
  282. desc = irq_to_desc_alloc_node(irq, 0);
  283. if (WARN_ON(desc == NULL))
  284. return -1;
  285. dynamic_irq_init(irq);
  286. return irq;
  287. }
  288. int bind_evtchn_to_irq(unsigned int evtchn)
  289. {
  290. int irq;
  291. spin_lock(&irq_mapping_update_lock);
  292. irq = evtchn_to_irq[evtchn];
  293. if (irq == -1) {
  294. irq = find_unbound_irq();
  295. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  296. handle_level_irq, "event");
  297. evtchn_to_irq[evtchn] = irq;
  298. irq_info[irq] = mk_evtchn_info(evtchn);
  299. }
  300. spin_unlock(&irq_mapping_update_lock);
  301. return irq;
  302. }
  303. EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
  304. static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
  305. {
  306. struct evtchn_bind_ipi bind_ipi;
  307. int evtchn, irq;
  308. spin_lock(&irq_mapping_update_lock);
  309. irq = per_cpu(ipi_to_irq, cpu)[ipi];
  310. if (irq == -1) {
  311. irq = find_unbound_irq();
  312. if (irq < 0)
  313. goto out;
  314. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  315. handle_level_irq, "ipi");
  316. bind_ipi.vcpu = cpu;
  317. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  318. &bind_ipi) != 0)
  319. BUG();
  320. evtchn = bind_ipi.port;
  321. evtchn_to_irq[evtchn] = irq;
  322. irq_info[irq] = mk_ipi_info(evtchn, ipi);
  323. per_cpu(ipi_to_irq, cpu)[ipi] = irq;
  324. bind_evtchn_to_cpu(evtchn, cpu);
  325. }
  326. out:
  327. spin_unlock(&irq_mapping_update_lock);
  328. return irq;
  329. }
  330. static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
  331. {
  332. struct evtchn_bind_virq bind_virq;
  333. int evtchn, irq;
  334. spin_lock(&irq_mapping_update_lock);
  335. irq = per_cpu(virq_to_irq, cpu)[virq];
  336. if (irq == -1) {
  337. bind_virq.virq = virq;
  338. bind_virq.vcpu = cpu;
  339. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  340. &bind_virq) != 0)
  341. BUG();
  342. evtchn = bind_virq.port;
  343. irq = find_unbound_irq();
  344. set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
  345. handle_level_irq, "virq");
  346. evtchn_to_irq[evtchn] = irq;
  347. irq_info[irq] = mk_virq_info(evtchn, virq);
  348. per_cpu(virq_to_irq, cpu)[virq] = irq;
  349. bind_evtchn_to_cpu(evtchn, cpu);
  350. }
  351. spin_unlock(&irq_mapping_update_lock);
  352. return irq;
  353. }
  354. static void unbind_from_irq(unsigned int irq)
  355. {
  356. struct evtchn_close close;
  357. int evtchn = evtchn_from_irq(irq);
  358. spin_lock(&irq_mapping_update_lock);
  359. if (VALID_EVTCHN(evtchn)) {
  360. close.port = evtchn;
  361. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  362. BUG();
  363. switch (type_from_irq(irq)) {
  364. case IRQT_VIRQ:
  365. per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
  366. [virq_from_irq(irq)] = -1;
  367. break;
  368. case IRQT_IPI:
  369. per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
  370. [ipi_from_irq(irq)] = -1;
  371. break;
  372. default:
  373. break;
  374. }
  375. /* Closed ports are implicitly re-bound to VCPU0. */
  376. bind_evtchn_to_cpu(evtchn, 0);
  377. evtchn_to_irq[evtchn] = -1;
  378. }
  379. if (irq_info[irq].type != IRQT_UNBOUND) {
  380. irq_info[irq] = mk_unbound_info();
  381. dynamic_irq_cleanup(irq);
  382. }
  383. spin_unlock(&irq_mapping_update_lock);
  384. }
  385. int bind_evtchn_to_irqhandler(unsigned int evtchn,
  386. irq_handler_t handler,
  387. unsigned long irqflags,
  388. const char *devname, void *dev_id)
  389. {
  390. unsigned int irq;
  391. int retval;
  392. irq = bind_evtchn_to_irq(evtchn);
  393. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  394. if (retval != 0) {
  395. unbind_from_irq(irq);
  396. return retval;
  397. }
  398. return irq;
  399. }
  400. EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
  401. int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
  402. irq_handler_t handler,
  403. unsigned long irqflags, const char *devname, void *dev_id)
  404. {
  405. unsigned int irq;
  406. int retval;
  407. irq = bind_virq_to_irq(virq, cpu);
  408. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  409. if (retval != 0) {
  410. unbind_from_irq(irq);
  411. return retval;
  412. }
  413. return irq;
  414. }
  415. EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
  416. int bind_ipi_to_irqhandler(enum ipi_vector ipi,
  417. unsigned int cpu,
  418. irq_handler_t handler,
  419. unsigned long irqflags,
  420. const char *devname,
  421. void *dev_id)
  422. {
  423. int irq, retval;
  424. irq = bind_ipi_to_irq(ipi, cpu);
  425. if (irq < 0)
  426. return irq;
  427. irqflags |= IRQF_NO_SUSPEND;
  428. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  429. if (retval != 0) {
  430. unbind_from_irq(irq);
  431. return retval;
  432. }
  433. return irq;
  434. }
  435. void unbind_from_irqhandler(unsigned int irq, void *dev_id)
  436. {
  437. free_irq(irq, dev_id);
  438. unbind_from_irq(irq);
  439. }
  440. EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
  441. void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
  442. {
  443. int irq = per_cpu(ipi_to_irq, cpu)[vector];
  444. BUG_ON(irq < 0);
  445. notify_remote_via_irq(irq);
  446. }
  447. irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
  448. {
  449. struct shared_info *sh = HYPERVISOR_shared_info;
  450. int cpu = smp_processor_id();
  451. int i;
  452. unsigned long flags;
  453. static DEFINE_SPINLOCK(debug_lock);
  454. spin_lock_irqsave(&debug_lock, flags);
  455. printk("vcpu %d\n ", cpu);
  456. for_each_online_cpu(i) {
  457. struct vcpu_info *v = per_cpu(xen_vcpu, i);
  458. printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
  459. (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
  460. v->evtchn_upcall_pending,
  461. v->evtchn_pending_sel);
  462. }
  463. printk("pending:\n ");
  464. for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
  465. printk("%08lx%s", sh->evtchn_pending[i],
  466. i % 8 == 0 ? "\n " : " ");
  467. printk("\nmasks:\n ");
  468. for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  469. printk("%08lx%s", sh->evtchn_mask[i],
  470. i % 8 == 0 ? "\n " : " ");
  471. printk("\nunmasked:\n ");
  472. for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  473. printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
  474. i % 8 == 0 ? "\n " : " ");
  475. printk("\npending list:\n");
  476. for(i = 0; i < NR_EVENT_CHANNELS; i++) {
  477. if (sync_test_bit(i, sh->evtchn_pending)) {
  478. printk(" %d: event %d -> irq %d\n",
  479. cpu_from_evtchn(i), i,
  480. evtchn_to_irq[i]);
  481. }
  482. }
  483. spin_unlock_irqrestore(&debug_lock, flags);
  484. return IRQ_HANDLED;
  485. }
  486. static DEFINE_PER_CPU(unsigned, xed_nesting_count);
  487. /*
  488. * Search the CPUs pending events bitmasks. For each one found, map
  489. * the event number to an irq, and feed it into do_IRQ() for
  490. * handling.
  491. *
  492. * Xen uses a two-level bitmap to speed searching. The first level is
  493. * a bitset of words which contain pending event bits. The second
  494. * level is a bitset of pending events themselves.
  495. */
  496. void xen_evtchn_do_upcall(struct pt_regs *regs)
  497. {
  498. int cpu = get_cpu();
  499. struct pt_regs *old_regs = set_irq_regs(regs);
  500. struct shared_info *s = HYPERVISOR_shared_info;
  501. struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
  502. unsigned count;
  503. exit_idle();
  504. irq_enter();
  505. do {
  506. unsigned long pending_words;
  507. vcpu_info->evtchn_upcall_pending = 0;
  508. if (__get_cpu_var(xed_nesting_count)++)
  509. goto out;
  510. #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
  511. /* Clear master flag /before/ clearing selector flag. */
  512. wmb();
  513. #endif
  514. pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
  515. while (pending_words != 0) {
  516. unsigned long pending_bits;
  517. int word_idx = __ffs(pending_words);
  518. pending_words &= ~(1UL << word_idx);
  519. while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
  520. int bit_idx = __ffs(pending_bits);
  521. int port = (word_idx * BITS_PER_LONG) + bit_idx;
  522. int irq = evtchn_to_irq[port];
  523. struct irq_desc *desc;
  524. if (irq != -1) {
  525. desc = irq_to_desc(irq);
  526. if (desc)
  527. generic_handle_irq_desc(irq, desc);
  528. }
  529. }
  530. }
  531. BUG_ON(!irqs_disabled());
  532. count = __get_cpu_var(xed_nesting_count);
  533. __get_cpu_var(xed_nesting_count) = 0;
  534. } while(count != 1);
  535. out:
  536. irq_exit();
  537. set_irq_regs(old_regs);
  538. put_cpu();
  539. }
  540. /* Rebind a new event channel to an existing irq. */
  541. void rebind_evtchn_irq(int evtchn, int irq)
  542. {
  543. struct irq_info *info = info_for_irq(irq);
  544. /* Make sure the irq is masked, since the new event channel
  545. will also be masked. */
  546. disable_irq(irq);
  547. spin_lock(&irq_mapping_update_lock);
  548. /* After resume the irq<->evtchn mappings are all cleared out */
  549. BUG_ON(evtchn_to_irq[evtchn] != -1);
  550. /* Expect irq to have been bound before,
  551. so there should be a proper type */
  552. BUG_ON(info->type == IRQT_UNBOUND);
  553. evtchn_to_irq[evtchn] = irq;
  554. irq_info[irq] = mk_evtchn_info(evtchn);
  555. spin_unlock(&irq_mapping_update_lock);
  556. /* new event channels are always bound to cpu 0 */
  557. irq_set_affinity(irq, cpumask_of(0));
  558. /* Unmask the event channel. */
  559. enable_irq(irq);
  560. }
  561. /* Rebind an evtchn so that it gets delivered to a specific cpu */
  562. static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
  563. {
  564. struct evtchn_bind_vcpu bind_vcpu;
  565. int evtchn = evtchn_from_irq(irq);
  566. if (!VALID_EVTCHN(evtchn))
  567. return -1;
  568. /* Send future instances of this interrupt to other vcpu. */
  569. bind_vcpu.port = evtchn;
  570. bind_vcpu.vcpu = tcpu;
  571. /*
  572. * If this fails, it usually just indicates that we're dealing with a
  573. * virq or IPI channel, which don't actually need to be rebound. Ignore
  574. * it, but don't do the xenlinux-level rebind in that case.
  575. */
  576. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
  577. bind_evtchn_to_cpu(evtchn, tcpu);
  578. return 0;
  579. }
  580. static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
  581. {
  582. unsigned tcpu = cpumask_first(dest);
  583. return rebind_irq_to_cpu(irq, tcpu);
  584. }
  585. int resend_irq_on_evtchn(unsigned int irq)
  586. {
  587. int masked, evtchn = evtchn_from_irq(irq);
  588. struct shared_info *s = HYPERVISOR_shared_info;
  589. if (!VALID_EVTCHN(evtchn))
  590. return 1;
  591. masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
  592. sync_set_bit(evtchn, s->evtchn_pending);
  593. if (!masked)
  594. unmask_evtchn(evtchn);
  595. return 1;
  596. }
  597. static void enable_dynirq(unsigned int irq)
  598. {
  599. int evtchn = evtchn_from_irq(irq);
  600. if (VALID_EVTCHN(evtchn))
  601. unmask_evtchn(evtchn);
  602. }
  603. static void disable_dynirq(unsigned int irq)
  604. {
  605. int evtchn = evtchn_from_irq(irq);
  606. if (VALID_EVTCHN(evtchn))
  607. mask_evtchn(evtchn);
  608. }
  609. static void ack_dynirq(unsigned int irq)
  610. {
  611. int evtchn = evtchn_from_irq(irq);
  612. move_native_irq(irq);
  613. if (VALID_EVTCHN(evtchn))
  614. clear_evtchn(evtchn);
  615. }
  616. static int retrigger_dynirq(unsigned int irq)
  617. {
  618. int evtchn = evtchn_from_irq(irq);
  619. struct shared_info *sh = HYPERVISOR_shared_info;
  620. int ret = 0;
  621. if (VALID_EVTCHN(evtchn)) {
  622. int masked;
  623. masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
  624. sync_set_bit(evtchn, sh->evtchn_pending);
  625. if (!masked)
  626. unmask_evtchn(evtchn);
  627. ret = 1;
  628. }
  629. return ret;
  630. }
  631. static void restore_cpu_virqs(unsigned int cpu)
  632. {
  633. struct evtchn_bind_virq bind_virq;
  634. int virq, irq, evtchn;
  635. for (virq = 0; virq < NR_VIRQS; virq++) {
  636. if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
  637. continue;
  638. BUG_ON(virq_from_irq(irq) != virq);
  639. /* Get a new binding from Xen. */
  640. bind_virq.virq = virq;
  641. bind_virq.vcpu = cpu;
  642. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  643. &bind_virq) != 0)
  644. BUG();
  645. evtchn = bind_virq.port;
  646. /* Record the new mapping. */
  647. evtchn_to_irq[evtchn] = irq;
  648. irq_info[irq] = mk_virq_info(evtchn, virq);
  649. bind_evtchn_to_cpu(evtchn, cpu);
  650. /* Ready for use. */
  651. unmask_evtchn(evtchn);
  652. }
  653. }
  654. static void restore_cpu_ipis(unsigned int cpu)
  655. {
  656. struct evtchn_bind_ipi bind_ipi;
  657. int ipi, irq, evtchn;
  658. for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
  659. if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
  660. continue;
  661. BUG_ON(ipi_from_irq(irq) != ipi);
  662. /* Get a new binding from Xen. */
  663. bind_ipi.vcpu = cpu;
  664. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  665. &bind_ipi) != 0)
  666. BUG();
  667. evtchn = bind_ipi.port;
  668. /* Record the new mapping. */
  669. evtchn_to_irq[evtchn] = irq;
  670. irq_info[irq] = mk_ipi_info(evtchn, ipi);
  671. bind_evtchn_to_cpu(evtchn, cpu);
  672. /* Ready for use. */
  673. unmask_evtchn(evtchn);
  674. }
  675. }
  676. /* Clear an irq's pending state, in preparation for polling on it */
  677. void xen_clear_irq_pending(int irq)
  678. {
  679. int evtchn = evtchn_from_irq(irq);
  680. if (VALID_EVTCHN(evtchn))
  681. clear_evtchn(evtchn);
  682. }
  683. void xen_set_irq_pending(int irq)
  684. {
  685. int evtchn = evtchn_from_irq(irq);
  686. if (VALID_EVTCHN(evtchn))
  687. set_evtchn(evtchn);
  688. }
  689. bool xen_test_irq_pending(int irq)
  690. {
  691. int evtchn = evtchn_from_irq(irq);
  692. bool ret = false;
  693. if (VALID_EVTCHN(evtchn))
  694. ret = test_evtchn(evtchn);
  695. return ret;
  696. }
  697. /* Poll waiting for an irq to become pending. In the usual case, the
  698. irq will be disabled so it won't deliver an interrupt. */
  699. void xen_poll_irq(int irq)
  700. {
  701. evtchn_port_t evtchn = evtchn_from_irq(irq);
  702. if (VALID_EVTCHN(evtchn)) {
  703. struct sched_poll poll;
  704. poll.nr_ports = 1;
  705. poll.timeout = 0;
  706. set_xen_guest_handle(poll.ports, &evtchn);
  707. if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
  708. BUG();
  709. }
  710. }
  711. void xen_irq_resume(void)
  712. {
  713. unsigned int cpu, irq, evtchn;
  714. init_evtchn_cpu_bindings();
  715. /* New event-channel space is not 'live' yet. */
  716. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  717. mask_evtchn(evtchn);
  718. /* No IRQ <-> event-channel mappings. */
  719. for (irq = 0; irq < nr_irqs; irq++)
  720. irq_info[irq].evtchn = 0; /* zap event-channel binding */
  721. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  722. evtchn_to_irq[evtchn] = -1;
  723. for_each_possible_cpu(cpu) {
  724. restore_cpu_virqs(cpu);
  725. restore_cpu_ipis(cpu);
  726. }
  727. }
  728. static struct irq_chip xen_dynamic_chip __read_mostly = {
  729. .name = "xen-dyn",
  730. .disable = disable_dynirq,
  731. .mask = disable_dynirq,
  732. .unmask = enable_dynirq,
  733. .ack = ack_dynirq,
  734. .set_affinity = set_affinity_irq,
  735. .retrigger = retrigger_dynirq,
  736. };
  737. void __init xen_init_IRQ(void)
  738. {
  739. int i;
  740. cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
  741. GFP_KERNEL);
  742. BUG_ON(cpu_evtchn_mask_p == NULL);
  743. init_evtchn_cpu_bindings();
  744. /* No event channels are 'live' right now. */
  745. for (i = 0; i < NR_EVENT_CHANNELS; i++)
  746. mask_evtchn(i);
  747. irq_ctx_init(smp_processor_id());
  748. }