events.c 19 KB

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