events.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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. retval = request_irq(irq, handler, irqflags, devname, dev_id);
  428. if (retval != 0) {
  429. unbind_from_irq(irq);
  430. return retval;
  431. }
  432. return irq;
  433. }
  434. void unbind_from_irqhandler(unsigned int irq, void *dev_id)
  435. {
  436. free_irq(irq, dev_id);
  437. unbind_from_irq(irq);
  438. }
  439. EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
  440. void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
  441. {
  442. int irq = per_cpu(ipi_to_irq, cpu)[vector];
  443. BUG_ON(irq < 0);
  444. notify_remote_via_irq(irq);
  445. }
  446. irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
  447. {
  448. struct shared_info *sh = HYPERVISOR_shared_info;
  449. int cpu = smp_processor_id();
  450. int i;
  451. unsigned long flags;
  452. static DEFINE_SPINLOCK(debug_lock);
  453. spin_lock_irqsave(&debug_lock, flags);
  454. printk("vcpu %d\n ", cpu);
  455. for_each_online_cpu(i) {
  456. struct vcpu_info *v = per_cpu(xen_vcpu, i);
  457. printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
  458. (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
  459. v->evtchn_upcall_pending,
  460. v->evtchn_pending_sel);
  461. }
  462. printk("pending:\n ");
  463. for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
  464. printk("%08lx%s", sh->evtchn_pending[i],
  465. i % 8 == 0 ? "\n " : " ");
  466. printk("\nmasks:\n ");
  467. for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  468. printk("%08lx%s", sh->evtchn_mask[i],
  469. i % 8 == 0 ? "\n " : " ");
  470. printk("\nunmasked:\n ");
  471. for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
  472. printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
  473. i % 8 == 0 ? "\n " : " ");
  474. printk("\npending list:\n");
  475. for(i = 0; i < NR_EVENT_CHANNELS; i++) {
  476. if (sync_test_bit(i, sh->evtchn_pending)) {
  477. printk(" %d: event %d -> irq %d\n",
  478. cpu_from_evtchn(i), i,
  479. evtchn_to_irq[i]);
  480. }
  481. }
  482. spin_unlock_irqrestore(&debug_lock, flags);
  483. return IRQ_HANDLED;
  484. }
  485. static DEFINE_PER_CPU(unsigned, xed_nesting_count);
  486. /*
  487. * Search the CPUs pending events bitmasks. For each one found, map
  488. * the event number to an irq, and feed it into do_IRQ() for
  489. * handling.
  490. *
  491. * Xen uses a two-level bitmap to speed searching. The first level is
  492. * a bitset of words which contain pending event bits. The second
  493. * level is a bitset of pending events themselves.
  494. */
  495. void xen_evtchn_do_upcall(struct pt_regs *regs)
  496. {
  497. int cpu = get_cpu();
  498. struct pt_regs *old_regs = set_irq_regs(regs);
  499. struct shared_info *s = HYPERVISOR_shared_info;
  500. struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
  501. unsigned count;
  502. exit_idle();
  503. irq_enter();
  504. do {
  505. unsigned long pending_words;
  506. vcpu_info->evtchn_upcall_pending = 0;
  507. if (__get_cpu_var(xed_nesting_count)++)
  508. goto out;
  509. #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
  510. /* Clear master flag /before/ clearing selector flag. */
  511. wmb();
  512. #endif
  513. pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
  514. while (pending_words != 0) {
  515. unsigned long pending_bits;
  516. int word_idx = __ffs(pending_words);
  517. pending_words &= ~(1UL << word_idx);
  518. while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
  519. int bit_idx = __ffs(pending_bits);
  520. int port = (word_idx * BITS_PER_LONG) + bit_idx;
  521. int irq = evtchn_to_irq[port];
  522. struct irq_desc *desc;
  523. if (irq != -1) {
  524. desc = irq_to_desc(irq);
  525. if (desc)
  526. generic_handle_irq_desc(irq, desc);
  527. }
  528. }
  529. }
  530. BUG_ON(!irqs_disabled());
  531. count = __get_cpu_var(xed_nesting_count);
  532. __get_cpu_var(xed_nesting_count) = 0;
  533. } while(count != 1);
  534. out:
  535. irq_exit();
  536. set_irq_regs(old_regs);
  537. put_cpu();
  538. }
  539. /* Rebind a new event channel to an existing irq. */
  540. void rebind_evtchn_irq(int evtchn, int irq)
  541. {
  542. struct irq_info *info = info_for_irq(irq);
  543. /* Make sure the irq is masked, since the new event channel
  544. will also be masked. */
  545. disable_irq(irq);
  546. spin_lock(&irq_mapping_update_lock);
  547. /* After resume the irq<->evtchn mappings are all cleared out */
  548. BUG_ON(evtchn_to_irq[evtchn] != -1);
  549. /* Expect irq to have been bound before,
  550. so there should be a proper type */
  551. BUG_ON(info->type == IRQT_UNBOUND);
  552. evtchn_to_irq[evtchn] = irq;
  553. irq_info[irq] = mk_evtchn_info(evtchn);
  554. spin_unlock(&irq_mapping_update_lock);
  555. /* new event channels are always bound to cpu 0 */
  556. irq_set_affinity(irq, cpumask_of(0));
  557. /* Unmask the event channel. */
  558. enable_irq(irq);
  559. }
  560. /* Rebind an evtchn so that it gets delivered to a specific cpu */
  561. static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
  562. {
  563. struct evtchn_bind_vcpu bind_vcpu;
  564. int evtchn = evtchn_from_irq(irq);
  565. if (!VALID_EVTCHN(evtchn))
  566. return -1;
  567. /* Send future instances of this interrupt to other vcpu. */
  568. bind_vcpu.port = evtchn;
  569. bind_vcpu.vcpu = tcpu;
  570. /*
  571. * If this fails, it usually just indicates that we're dealing with a
  572. * virq or IPI channel, which don't actually need to be rebound. Ignore
  573. * it, but don't do the xenlinux-level rebind in that case.
  574. */
  575. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
  576. bind_evtchn_to_cpu(evtchn, tcpu);
  577. return 0;
  578. }
  579. static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
  580. {
  581. unsigned tcpu = cpumask_first(dest);
  582. return rebind_irq_to_cpu(irq, tcpu);
  583. }
  584. int resend_irq_on_evtchn(unsigned int irq)
  585. {
  586. int masked, evtchn = evtchn_from_irq(irq);
  587. struct shared_info *s = HYPERVISOR_shared_info;
  588. if (!VALID_EVTCHN(evtchn))
  589. return 1;
  590. masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
  591. sync_set_bit(evtchn, s->evtchn_pending);
  592. if (!masked)
  593. unmask_evtchn(evtchn);
  594. return 1;
  595. }
  596. static void enable_dynirq(unsigned int irq)
  597. {
  598. int evtchn = evtchn_from_irq(irq);
  599. if (VALID_EVTCHN(evtchn))
  600. unmask_evtchn(evtchn);
  601. }
  602. static void disable_dynirq(unsigned int irq)
  603. {
  604. int evtchn = evtchn_from_irq(irq);
  605. if (VALID_EVTCHN(evtchn))
  606. mask_evtchn(evtchn);
  607. }
  608. static void ack_dynirq(unsigned int irq)
  609. {
  610. int evtchn = evtchn_from_irq(irq);
  611. move_native_irq(irq);
  612. if (VALID_EVTCHN(evtchn))
  613. clear_evtchn(evtchn);
  614. }
  615. static int retrigger_dynirq(unsigned int irq)
  616. {
  617. int evtchn = evtchn_from_irq(irq);
  618. struct shared_info *sh = HYPERVISOR_shared_info;
  619. int ret = 0;
  620. if (VALID_EVTCHN(evtchn)) {
  621. int masked;
  622. masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
  623. sync_set_bit(evtchn, sh->evtchn_pending);
  624. if (!masked)
  625. unmask_evtchn(evtchn);
  626. ret = 1;
  627. }
  628. return ret;
  629. }
  630. static void restore_cpu_virqs(unsigned int cpu)
  631. {
  632. struct evtchn_bind_virq bind_virq;
  633. int virq, irq, evtchn;
  634. for (virq = 0; virq < NR_VIRQS; virq++) {
  635. if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
  636. continue;
  637. BUG_ON(virq_from_irq(irq) != virq);
  638. /* Get a new binding from Xen. */
  639. bind_virq.virq = virq;
  640. bind_virq.vcpu = cpu;
  641. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  642. &bind_virq) != 0)
  643. BUG();
  644. evtchn = bind_virq.port;
  645. /* Record the new mapping. */
  646. evtchn_to_irq[evtchn] = irq;
  647. irq_info[irq] = mk_virq_info(evtchn, virq);
  648. bind_evtchn_to_cpu(evtchn, cpu);
  649. /* Ready for use. */
  650. unmask_evtchn(evtchn);
  651. }
  652. }
  653. static void restore_cpu_ipis(unsigned int cpu)
  654. {
  655. struct evtchn_bind_ipi bind_ipi;
  656. int ipi, irq, evtchn;
  657. for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
  658. if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
  659. continue;
  660. BUG_ON(ipi_from_irq(irq) != ipi);
  661. /* Get a new binding from Xen. */
  662. bind_ipi.vcpu = cpu;
  663. if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
  664. &bind_ipi) != 0)
  665. BUG();
  666. evtchn = bind_ipi.port;
  667. /* Record the new mapping. */
  668. evtchn_to_irq[evtchn] = irq;
  669. irq_info[irq] = mk_ipi_info(evtchn, ipi);
  670. bind_evtchn_to_cpu(evtchn, cpu);
  671. /* Ready for use. */
  672. unmask_evtchn(evtchn);
  673. }
  674. }
  675. /* Clear an irq's pending state, in preparation for polling on it */
  676. void xen_clear_irq_pending(int irq)
  677. {
  678. int evtchn = evtchn_from_irq(irq);
  679. if (VALID_EVTCHN(evtchn))
  680. clear_evtchn(evtchn);
  681. }
  682. void xen_set_irq_pending(int irq)
  683. {
  684. int evtchn = evtchn_from_irq(irq);
  685. if (VALID_EVTCHN(evtchn))
  686. set_evtchn(evtchn);
  687. }
  688. bool xen_test_irq_pending(int irq)
  689. {
  690. int evtchn = evtchn_from_irq(irq);
  691. bool ret = false;
  692. if (VALID_EVTCHN(evtchn))
  693. ret = test_evtchn(evtchn);
  694. return ret;
  695. }
  696. /* Poll waiting for an irq to become pending. In the usual case, the
  697. irq will be disabled so it won't deliver an interrupt. */
  698. void xen_poll_irq(int irq)
  699. {
  700. evtchn_port_t evtchn = evtchn_from_irq(irq);
  701. if (VALID_EVTCHN(evtchn)) {
  702. struct sched_poll poll;
  703. poll.nr_ports = 1;
  704. poll.timeout = 0;
  705. set_xen_guest_handle(poll.ports, &evtchn);
  706. if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
  707. BUG();
  708. }
  709. }
  710. void xen_irq_resume(void)
  711. {
  712. unsigned int cpu, irq, evtchn;
  713. init_evtchn_cpu_bindings();
  714. /* New event-channel space is not 'live' yet. */
  715. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  716. mask_evtchn(evtchn);
  717. /* No IRQ <-> event-channel mappings. */
  718. for (irq = 0; irq < nr_irqs; irq++)
  719. irq_info[irq].evtchn = 0; /* zap event-channel binding */
  720. for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
  721. evtchn_to_irq[evtchn] = -1;
  722. for_each_possible_cpu(cpu) {
  723. restore_cpu_virqs(cpu);
  724. restore_cpu_ipis(cpu);
  725. }
  726. }
  727. static struct irq_chip xen_dynamic_chip __read_mostly = {
  728. .name = "xen-dyn",
  729. .disable = disable_dynirq,
  730. .mask = disable_dynirq,
  731. .unmask = enable_dynirq,
  732. .ack = ack_dynirq,
  733. .set_affinity = set_affinity_irq,
  734. .retrigger = retrigger_dynirq,
  735. };
  736. void __init xen_init_IRQ(void)
  737. {
  738. int i;
  739. cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
  740. GFP_KERNEL);
  741. BUG_ON(cpu_evtchn_mask_p == NULL);
  742. init_evtchn_cpu_bindings();
  743. /* No event channels are 'live' right now. */
  744. for (i = 0; i < NR_EVENT_CHANNELS; i++)
  745. mask_evtchn(i);
  746. irq_ctx_init(smp_processor_id());
  747. }