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.
11 * There are four kinds of events which can be mapped to an event
14 * 1. Inter-domain notifications. This includes all the virtual
15 * device events, since they're driven by front-ends in another domain
17 * 2. VIRQs, typically used for timers. These are per-cpu events.
19 * 4. Hardware interrupts. Not supported at present.
21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
24 #include <linux/linkage.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/module.h>
28 #include <linux/string.h>
30 #include <asm/ptrace.h>
32 #include <asm/sync_bitops.h>
33 #include <asm/xen/hypercall.h>
34 #include <asm/xen/hypervisor.h>
36 #include <xen/xen-ops.h>
37 #include <xen/events.h>
38 #include <xen/interface/xen.h>
39 #include <xen/interface/event_channel.h>
42 * This lock protects updates to the following mapping and reference-count
43 * arrays. The lock does not need to be acquired to read the mapping tables.
45 static DEFINE_SPINLOCK(irq_mapping_update_lock);
47 /* IRQ <-> VIRQ mapping. */
48 static DEFINE_PER_CPU(int, virq_to_irq[NR_VIRQS]) = {[0 ... NR_VIRQS-1] = -1};
50 /* IRQ <-> IPI mapping */
51 static DEFINE_PER_CPU(int, ipi_to_irq[XEN_NR_IPIS]) = {[0 ... XEN_NR_IPIS-1] = -1};
53 /* Packed IRQ information: binding type, sub-type index, and event channel. */
56 unsigned short evtchn;
61 static struct packed_irq irq_info[NR_IRQS];
72 /* Convenient shorthand for packed representation of an unbound IRQ. */
73 #define IRQ_UNBOUND mk_irq_info(IRQT_UNBOUND, 0, 0)
75 static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
76 [0 ... NR_EVENT_CHANNELS-1] = -1
78 static unsigned long cpu_evtchn_mask[NR_CPUS][NR_EVENT_CHANNELS/BITS_PER_LONG];
79 static u8 cpu_evtchn[NR_EVENT_CHANNELS];
81 /* Reference counts for bindings to IRQs. */
82 static int irq_bindcount[NR_IRQS];
84 /* Xen will never allocate port zero for any purpose. */
85 #define VALID_EVTCHN(chn) ((chn) != 0)
87 static struct irq_chip xen_dynamic_chip;
89 /* Constructor for packed IRQ information. */
90 static inline struct packed_irq mk_irq_info(u32 type, u32 index, u32 evtchn)
92 return (struct packed_irq) { evtchn, index, type };
96 * Accessors for packed IRQ information.
98 static inline unsigned int evtchn_from_irq(int irq)
100 return irq_info[irq].evtchn;
103 static inline unsigned int index_from_irq(int irq)
105 return irq_info[irq].index;
108 static inline unsigned int type_from_irq(int irq)
110 return irq_info[irq].type;
113 static inline unsigned long active_evtchns(unsigned int cpu,
114 struct shared_info *sh,
117 return (sh->evtchn_pending[idx] &
118 cpu_evtchn_mask[cpu][idx] &
119 ~sh->evtchn_mask[idx]);
122 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
124 int irq = evtchn_to_irq[chn];
128 irq_to_desc(irq)->affinity = cpumask_of_cpu(cpu);
131 __clear_bit(chn, cpu_evtchn_mask[cpu_evtchn[chn]]);
132 __set_bit(chn, cpu_evtchn_mask[cpu]);
134 cpu_evtchn[chn] = cpu;
137 static void init_evtchn_cpu_bindings(void)
140 struct irq_desc *desc;
143 /* By default all event channels notify CPU#0. */
144 for_each_irq_desc(i, desc) {
145 desc->affinity = cpumask_of_cpu(0);
149 memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
150 memset(cpu_evtchn_mask[0], ~0, sizeof(cpu_evtchn_mask[0]));
153 static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
155 return cpu_evtchn[evtchn];
158 static inline void clear_evtchn(int port)
160 struct shared_info *s = HYPERVISOR_shared_info;
161 sync_clear_bit(port, &s->evtchn_pending[0]);
164 static inline void set_evtchn(int port)
166 struct shared_info *s = HYPERVISOR_shared_info;
167 sync_set_bit(port, &s->evtchn_pending[0]);
170 static inline int test_evtchn(int port)
172 struct shared_info *s = HYPERVISOR_shared_info;
173 return sync_test_bit(port, &s->evtchn_pending[0]);
178 * notify_remote_via_irq - send event to remote end of event channel via irq
179 * @irq: irq of event channel to send event to
181 * Unlike notify_remote_via_evtchn(), this is safe to use across
182 * save/restore. Notifications on a broken connection are silently
185 void notify_remote_via_irq(int irq)
187 int evtchn = evtchn_from_irq(irq);
189 if (VALID_EVTCHN(evtchn))
190 notify_remote_via_evtchn(evtchn);
192 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
194 static void mask_evtchn(int port)
196 struct shared_info *s = HYPERVISOR_shared_info;
197 sync_set_bit(port, &s->evtchn_mask[0]);
200 static void unmask_evtchn(int port)
202 struct shared_info *s = HYPERVISOR_shared_info;
203 unsigned int cpu = get_cpu();
205 BUG_ON(!irqs_disabled());
207 /* Slow path (hypercall) if this is a non-local port. */
208 if (unlikely(cpu != cpu_from_evtchn(port))) {
209 struct evtchn_unmask unmask = { .port = port };
210 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
212 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
214 sync_clear_bit(port, &s->evtchn_mask[0]);
217 * The following is basically the equivalent of
218 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
219 * the interrupt edge' if the channel is masked.
221 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
222 !sync_test_and_set_bit(port / BITS_PER_LONG,
223 &vcpu_info->evtchn_pending_sel))
224 vcpu_info->evtchn_upcall_pending = 1;
230 static int find_unbound_irq(void)
233 struct irq_desc *desc;
235 /* Only allocate from dynirq range */
236 for (irq = 0; irq < nr_irqs; irq++)
237 if (irq_bindcount[irq] == 0)
241 panic("No available IRQ to bind to: increase nr_irqs!\n");
243 desc = irq_to_desc_alloc_cpu(irq, 0);
244 if (WARN_ON(desc == NULL))
250 int bind_evtchn_to_irq(unsigned int evtchn)
254 spin_lock(&irq_mapping_update_lock);
256 irq = evtchn_to_irq[evtchn];
259 irq = find_unbound_irq();
261 dynamic_irq_init(irq);
262 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
263 handle_level_irq, "event");
265 evtchn_to_irq[evtchn] = irq;
266 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
269 irq_bindcount[irq]++;
271 spin_unlock(&irq_mapping_update_lock);
275 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
277 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
279 struct evtchn_bind_ipi bind_ipi;
282 spin_lock(&irq_mapping_update_lock);
284 irq = per_cpu(ipi_to_irq, cpu)[ipi];
286 irq = find_unbound_irq();
290 dynamic_irq_init(irq);
291 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
292 handle_level_irq, "ipi");
295 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
298 evtchn = bind_ipi.port;
300 evtchn_to_irq[evtchn] = irq;
301 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
303 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
305 bind_evtchn_to_cpu(evtchn, cpu);
308 irq_bindcount[irq]++;
311 spin_unlock(&irq_mapping_update_lock);
316 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
318 struct evtchn_bind_virq bind_virq;
321 spin_lock(&irq_mapping_update_lock);
323 irq = per_cpu(virq_to_irq, cpu)[virq];
326 bind_virq.virq = virq;
327 bind_virq.vcpu = cpu;
328 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
331 evtchn = bind_virq.port;
333 irq = find_unbound_irq();
335 dynamic_irq_init(irq);
336 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
337 handle_level_irq, "virq");
339 evtchn_to_irq[evtchn] = irq;
340 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
342 per_cpu(virq_to_irq, cpu)[virq] = irq;
344 bind_evtchn_to_cpu(evtchn, cpu);
347 irq_bindcount[irq]++;
349 spin_unlock(&irq_mapping_update_lock);
354 static void unbind_from_irq(unsigned int irq)
356 struct evtchn_close close;
357 int evtchn = evtchn_from_irq(irq);
359 spin_lock(&irq_mapping_update_lock);
361 if ((--irq_bindcount[irq] == 0) && VALID_EVTCHN(evtchn)) {
363 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
366 switch (type_from_irq(irq)) {
368 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
369 [index_from_irq(irq)] = -1;
372 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
373 [index_from_irq(irq)] = -1;
379 /* Closed ports are implicitly re-bound to VCPU0. */
380 bind_evtchn_to_cpu(evtchn, 0);
382 evtchn_to_irq[evtchn] = -1;
383 irq_info[irq] = IRQ_UNBOUND;
385 dynamic_irq_cleanup(irq);
388 spin_unlock(&irq_mapping_update_lock);
391 int bind_evtchn_to_irqhandler(unsigned int evtchn,
392 irq_handler_t handler,
393 unsigned long irqflags,
394 const char *devname, void *dev_id)
399 irq = bind_evtchn_to_irq(evtchn);
400 retval = request_irq(irq, handler, irqflags, devname, dev_id);
402 unbind_from_irq(irq);
408 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
410 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
411 irq_handler_t handler,
412 unsigned long irqflags, const char *devname, void *dev_id)
417 irq = bind_virq_to_irq(virq, cpu);
418 retval = request_irq(irq, handler, irqflags, devname, dev_id);
420 unbind_from_irq(irq);
426 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
428 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
430 irq_handler_t handler,
431 unsigned long irqflags,
437 irq = bind_ipi_to_irq(ipi, cpu);
441 retval = request_irq(irq, handler, irqflags, devname, dev_id);
443 unbind_from_irq(irq);
450 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
452 free_irq(irq, dev_id);
453 unbind_from_irq(irq);
455 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
457 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
459 int irq = per_cpu(ipi_to_irq, cpu)[vector];
461 notify_remote_via_irq(irq);
464 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
466 struct shared_info *sh = HYPERVISOR_shared_info;
467 int cpu = smp_processor_id();
470 static DEFINE_SPINLOCK(debug_lock);
472 spin_lock_irqsave(&debug_lock, flags);
474 printk("vcpu %d\n ", cpu);
476 for_each_online_cpu(i) {
477 struct vcpu_info *v = per_cpu(xen_vcpu, i);
478 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
479 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
480 v->evtchn_upcall_pending,
481 v->evtchn_pending_sel);
483 printk("pending:\n ");
484 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
485 printk("%08lx%s", sh->evtchn_pending[i],
486 i % 8 == 0 ? "\n " : " ");
487 printk("\nmasks:\n ");
488 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
489 printk("%08lx%s", sh->evtchn_mask[i],
490 i % 8 == 0 ? "\n " : " ");
492 printk("\nunmasked:\n ");
493 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
494 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
495 i % 8 == 0 ? "\n " : " ");
497 printk("\npending list:\n");
498 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
499 if (sync_test_bit(i, sh->evtchn_pending)) {
500 printk(" %d: event %d -> irq %d\n",
506 spin_unlock_irqrestore(&debug_lock, flags);
513 * Search the CPUs pending events bitmasks. For each one found, map
514 * the event number to an irq, and feed it into do_IRQ() for
517 * Xen uses a two-level bitmap to speed searching. The first level is
518 * a bitset of words which contain pending event bits. The second
519 * level is a bitset of pending events themselves.
521 void xen_evtchn_do_upcall(struct pt_regs *regs)
524 struct shared_info *s = HYPERVISOR_shared_info;
525 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
526 static DEFINE_PER_CPU(unsigned, nesting_count);
530 unsigned long pending_words;
532 vcpu_info->evtchn_upcall_pending = 0;
534 if (__get_cpu_var(nesting_count)++)
537 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
538 /* Clear master flag /before/ clearing selector flag. */
541 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
542 while (pending_words != 0) {
543 unsigned long pending_bits;
544 int word_idx = __ffs(pending_words);
545 pending_words &= ~(1UL << word_idx);
547 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
548 int bit_idx = __ffs(pending_bits);
549 int port = (word_idx * BITS_PER_LONG) + bit_idx;
550 int irq = evtchn_to_irq[port];
553 xen_do_IRQ(irq, regs);
557 BUG_ON(!irqs_disabled());
559 count = __get_cpu_var(nesting_count);
560 __get_cpu_var(nesting_count) = 0;
567 /* Rebind a new event channel to an existing irq. */
568 void rebind_evtchn_irq(int evtchn, int irq)
570 /* Make sure the irq is masked, since the new event channel
571 will also be masked. */
574 spin_lock(&irq_mapping_update_lock);
576 /* After resume the irq<->evtchn mappings are all cleared out */
577 BUG_ON(evtchn_to_irq[evtchn] != -1);
578 /* Expect irq to have been bound before,
579 so the bindcount should be non-0 */
580 BUG_ON(irq_bindcount[irq] == 0);
582 evtchn_to_irq[evtchn] = irq;
583 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
585 spin_unlock(&irq_mapping_update_lock);
587 /* new event channels are always bound to cpu 0 */
588 irq_set_affinity(irq, cpumask_of(0));
590 /* Unmask the event channel. */
594 /* Rebind an evtchn so that it gets delivered to a specific cpu */
595 static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
597 struct evtchn_bind_vcpu bind_vcpu;
598 int evtchn = evtchn_from_irq(irq);
600 if (!VALID_EVTCHN(evtchn))
603 /* Send future instances of this interrupt to other vcpu. */
604 bind_vcpu.port = evtchn;
605 bind_vcpu.vcpu = tcpu;
608 * If this fails, it usually just indicates that we're dealing with a
609 * virq or IPI channel, which don't actually need to be rebound. Ignore
610 * it, but don't do the xenlinux-level rebind in that case.
612 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
613 bind_evtchn_to_cpu(evtchn, tcpu);
617 static void set_affinity_irq(unsigned irq, const struct cpumask *dest)
619 unsigned tcpu = cpumask_first(dest);
620 rebind_irq_to_cpu(irq, tcpu);
623 int resend_irq_on_evtchn(unsigned int irq)
625 int masked, evtchn = evtchn_from_irq(irq);
626 struct shared_info *s = HYPERVISOR_shared_info;
628 if (!VALID_EVTCHN(evtchn))
631 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
632 sync_set_bit(evtchn, s->evtchn_pending);
634 unmask_evtchn(evtchn);
639 static void enable_dynirq(unsigned int irq)
641 int evtchn = evtchn_from_irq(irq);
643 if (VALID_EVTCHN(evtchn))
644 unmask_evtchn(evtchn);
647 static void disable_dynirq(unsigned int irq)
649 int evtchn = evtchn_from_irq(irq);
651 if (VALID_EVTCHN(evtchn))
655 static void ack_dynirq(unsigned int irq)
657 int evtchn = evtchn_from_irq(irq);
659 move_native_irq(irq);
661 if (VALID_EVTCHN(evtchn))
662 clear_evtchn(evtchn);
665 static int retrigger_dynirq(unsigned int irq)
667 int evtchn = evtchn_from_irq(irq);
668 struct shared_info *sh = HYPERVISOR_shared_info;
671 if (VALID_EVTCHN(evtchn)) {
674 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
675 sync_set_bit(evtchn, sh->evtchn_pending);
677 unmask_evtchn(evtchn);
684 static void restore_cpu_virqs(unsigned int cpu)
686 struct evtchn_bind_virq bind_virq;
687 int virq, irq, evtchn;
689 for (virq = 0; virq < NR_VIRQS; virq++) {
690 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
693 BUG_ON(irq_info[irq].type != IRQT_VIRQ);
694 BUG_ON(irq_info[irq].index != virq);
696 /* Get a new binding from Xen. */
697 bind_virq.virq = virq;
698 bind_virq.vcpu = cpu;
699 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
702 evtchn = bind_virq.port;
704 /* Record the new mapping. */
705 evtchn_to_irq[evtchn] = irq;
706 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
707 bind_evtchn_to_cpu(evtchn, cpu);
710 unmask_evtchn(evtchn);
714 static void restore_cpu_ipis(unsigned int cpu)
716 struct evtchn_bind_ipi bind_ipi;
717 int ipi, irq, evtchn;
719 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
720 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
723 BUG_ON(irq_info[irq].type != IRQT_IPI);
724 BUG_ON(irq_info[irq].index != ipi);
726 /* Get a new binding from Xen. */
728 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
731 evtchn = bind_ipi.port;
733 /* Record the new mapping. */
734 evtchn_to_irq[evtchn] = irq;
735 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
736 bind_evtchn_to_cpu(evtchn, cpu);
739 unmask_evtchn(evtchn);
744 /* Clear an irq's pending state, in preparation for polling on it */
745 void xen_clear_irq_pending(int irq)
747 int evtchn = evtchn_from_irq(irq);
749 if (VALID_EVTCHN(evtchn))
750 clear_evtchn(evtchn);
753 void xen_set_irq_pending(int irq)
755 int evtchn = evtchn_from_irq(irq);
757 if (VALID_EVTCHN(evtchn))
761 bool xen_test_irq_pending(int irq)
763 int evtchn = evtchn_from_irq(irq);
766 if (VALID_EVTCHN(evtchn))
767 ret = test_evtchn(evtchn);
772 /* Poll waiting for an irq to become pending. In the usual case, the
773 irq will be disabled so it won't deliver an interrupt. */
774 void xen_poll_irq(int irq)
776 evtchn_port_t evtchn = evtchn_from_irq(irq);
778 if (VALID_EVTCHN(evtchn)) {
779 struct sched_poll poll;
783 set_xen_guest_handle(poll.ports, &evtchn);
785 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
790 void xen_irq_resume(void)
792 unsigned int cpu, irq, evtchn;
794 init_evtchn_cpu_bindings();
796 /* New event-channel space is not 'live' yet. */
797 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
800 /* No IRQ <-> event-channel mappings. */
801 for (irq = 0; irq < nr_irqs; irq++)
802 irq_info[irq].evtchn = 0; /* zap event-channel binding */
804 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
805 evtchn_to_irq[evtchn] = -1;
807 for_each_possible_cpu(cpu) {
808 restore_cpu_virqs(cpu);
809 restore_cpu_ipis(cpu);
813 static struct irq_chip xen_dynamic_chip __read_mostly = {
815 .mask = disable_dynirq,
816 .unmask = enable_dynirq,
818 .set_affinity = set_affinity_irq,
819 .retrigger = retrigger_dynirq,
822 void __init xen_init_IRQ(void)
826 init_evtchn_cpu_bindings();
828 /* No event channels are 'live' right now. */
829 for (i = 0; i < NR_EVENT_CHANNELS; i++)
832 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
833 for (i = 0; i < nr_irqs; i++)
834 irq_bindcount[i] = 0;
836 irq_ctx_init(smp_processor_id());