events.c 23 KB

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