events.c 21 KB

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