events.c 21 KB

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