events.c 19 KB

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