events.c 19 KB

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