events.c 23 KB

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