events.c 21 KB

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