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>
29 #include <linux/bootmem.h>
31 #include <asm/ptrace.h>
34 #include <asm/sync_bitops.h>
35 #include <asm/xen/hypercall.h>
36 #include <asm/xen/hypervisor.h>
38 #include <xen/xen-ops.h>
39 #include <xen/events.h>
40 #include <xen/interface/xen.h>
41 #include <xen/interface/event_channel.h>
44 * This lock protects updates to the following mapping and reference-count
45 * arrays. The lock does not need to be acquired to read the mapping tables.
47 static DEFINE_SPINLOCK(irq_mapping_update_lock);
49 /* IRQ <-> VIRQ mapping. */
50 static DEFINE_PER_CPU(int, virq_to_irq[NR_VIRQS]) = {[0 ... NR_VIRQS-1] = -1};
52 /* IRQ <-> IPI mapping */
53 static DEFINE_PER_CPU(int, ipi_to_irq[XEN_NR_IPIS]) = {[0 ... XEN_NR_IPIS-1] = -1};
55 /* Packed IRQ information: binding type, sub-type index, and event channel. */
58 unsigned short evtchn;
63 static struct packed_irq irq_info[NR_IRQS];
74 /* Convenient shorthand for packed representation of an unbound IRQ. */
75 #define IRQ_UNBOUND mk_irq_info(IRQT_UNBOUND, 0, 0)
77 static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
78 [0 ... NR_EVENT_CHANNELS-1] = -1
81 unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
83 static struct cpu_evtchn_s *cpu_evtchn_mask_p;
84 static inline unsigned long *cpu_evtchn_mask(int cpu)
86 return cpu_evtchn_mask_p[cpu].bits;
88 static u8 cpu_evtchn[NR_EVENT_CHANNELS];
90 /* Reference counts for bindings to IRQs. */
91 static int irq_bindcount[NR_IRQS];
93 /* Xen will never allocate port zero for any purpose. */
94 #define VALID_EVTCHN(chn) ((chn) != 0)
96 static struct irq_chip xen_dynamic_chip;
98 /* Constructor for packed IRQ information. */
99 static inline struct packed_irq mk_irq_info(u32 type, u32 index, u32 evtchn)
101 return (struct packed_irq) { evtchn, index, type };
105 * Accessors for packed IRQ information.
107 static inline unsigned int evtchn_from_irq(int irq)
109 return irq_info[irq].evtchn;
112 static inline unsigned int index_from_irq(int irq)
114 return irq_info[irq].index;
117 static inline unsigned int type_from_irq(int irq)
119 return irq_info[irq].type;
122 static inline unsigned long active_evtchns(unsigned int cpu,
123 struct shared_info *sh,
126 return (sh->evtchn_pending[idx] &
127 cpu_evtchn_mask(cpu)[idx] &
128 ~sh->evtchn_mask[idx]);
131 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
133 int irq = evtchn_to_irq[chn];
137 cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
140 __clear_bit(chn, cpu_evtchn_mask(cpu_evtchn[chn]));
141 __set_bit(chn, cpu_evtchn_mask(cpu));
143 cpu_evtchn[chn] = cpu;
146 static void init_evtchn_cpu_bindings(void)
149 struct irq_desc *desc;
152 /* By default all event channels notify CPU#0. */
153 for_each_irq_desc(i, desc) {
154 cpumask_copy(desc->affinity, cpumask_of(0));
158 memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
159 memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0)));
162 static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
164 return cpu_evtchn[evtchn];
167 static inline void clear_evtchn(int port)
169 struct shared_info *s = HYPERVISOR_shared_info;
170 sync_clear_bit(port, &s->evtchn_pending[0]);
173 static inline void set_evtchn(int port)
175 struct shared_info *s = HYPERVISOR_shared_info;
176 sync_set_bit(port, &s->evtchn_pending[0]);
179 static inline int test_evtchn(int port)
181 struct shared_info *s = HYPERVISOR_shared_info;
182 return sync_test_bit(port, &s->evtchn_pending[0]);
187 * notify_remote_via_irq - send event to remote end of event channel via irq
188 * @irq: irq of event channel to send event to
190 * Unlike notify_remote_via_evtchn(), this is safe to use across
191 * save/restore. Notifications on a broken connection are silently
194 void notify_remote_via_irq(int irq)
196 int evtchn = evtchn_from_irq(irq);
198 if (VALID_EVTCHN(evtchn))
199 notify_remote_via_evtchn(evtchn);
201 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
203 static void mask_evtchn(int port)
205 struct shared_info *s = HYPERVISOR_shared_info;
206 sync_set_bit(port, &s->evtchn_mask[0]);
209 static void unmask_evtchn(int port)
211 struct shared_info *s = HYPERVISOR_shared_info;
212 unsigned int cpu = get_cpu();
214 BUG_ON(!irqs_disabled());
216 /* Slow path (hypercall) if this is a non-local port. */
217 if (unlikely(cpu != cpu_from_evtchn(port))) {
218 struct evtchn_unmask unmask = { .port = port };
219 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
221 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
223 sync_clear_bit(port, &s->evtchn_mask[0]);
226 * The following is basically the equivalent of
227 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
228 * the interrupt edge' if the channel is masked.
230 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
231 !sync_test_and_set_bit(port / BITS_PER_LONG,
232 &vcpu_info->evtchn_pending_sel))
233 vcpu_info->evtchn_upcall_pending = 1;
239 static int find_unbound_irq(void)
242 struct irq_desc *desc;
244 /* Only allocate from dynirq range */
245 for (irq = 0; irq < nr_irqs; irq++)
246 if (irq_bindcount[irq] == 0)
250 panic("No available IRQ to bind to: increase nr_irqs!\n");
252 desc = irq_to_desc_alloc_cpu(irq, 0);
253 if (WARN_ON(desc == NULL))
259 int bind_evtchn_to_irq(unsigned int evtchn)
263 spin_lock(&irq_mapping_update_lock);
265 irq = evtchn_to_irq[evtchn];
268 irq = find_unbound_irq();
270 dynamic_irq_init(irq);
271 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
272 handle_level_irq, "event");
274 evtchn_to_irq[evtchn] = irq;
275 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
278 irq_bindcount[irq]++;
280 spin_unlock(&irq_mapping_update_lock);
284 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
286 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
288 struct evtchn_bind_ipi bind_ipi;
291 spin_lock(&irq_mapping_update_lock);
293 irq = per_cpu(ipi_to_irq, cpu)[ipi];
295 irq = find_unbound_irq();
299 dynamic_irq_init(irq);
300 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
301 handle_level_irq, "ipi");
304 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
307 evtchn = bind_ipi.port;
309 evtchn_to_irq[evtchn] = irq;
310 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
312 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
314 bind_evtchn_to_cpu(evtchn, cpu);
317 irq_bindcount[irq]++;
320 spin_unlock(&irq_mapping_update_lock);
325 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
327 struct evtchn_bind_virq bind_virq;
330 spin_lock(&irq_mapping_update_lock);
332 irq = per_cpu(virq_to_irq, cpu)[virq];
335 bind_virq.virq = virq;
336 bind_virq.vcpu = cpu;
337 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
340 evtchn = bind_virq.port;
342 irq = find_unbound_irq();
344 dynamic_irq_init(irq);
345 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
346 handle_level_irq, "virq");
348 evtchn_to_irq[evtchn] = irq;
349 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
351 per_cpu(virq_to_irq, cpu)[virq] = irq;
353 bind_evtchn_to_cpu(evtchn, cpu);
356 irq_bindcount[irq]++;
358 spin_unlock(&irq_mapping_update_lock);
363 static void unbind_from_irq(unsigned int irq)
365 struct evtchn_close close;
366 int evtchn = evtchn_from_irq(irq);
368 spin_lock(&irq_mapping_update_lock);
370 if ((--irq_bindcount[irq] == 0) && VALID_EVTCHN(evtchn)) {
372 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
375 switch (type_from_irq(irq)) {
377 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
378 [index_from_irq(irq)] = -1;
381 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
382 [index_from_irq(irq)] = -1;
388 /* Closed ports are implicitly re-bound to VCPU0. */
389 bind_evtchn_to_cpu(evtchn, 0);
391 evtchn_to_irq[evtchn] = -1;
392 irq_info[irq] = IRQ_UNBOUND;
394 dynamic_irq_cleanup(irq);
397 spin_unlock(&irq_mapping_update_lock);
400 int bind_evtchn_to_irqhandler(unsigned int evtchn,
401 irq_handler_t handler,
402 unsigned long irqflags,
403 const char *devname, void *dev_id)
408 irq = bind_evtchn_to_irq(evtchn);
409 retval = request_irq(irq, handler, irqflags, devname, dev_id);
411 unbind_from_irq(irq);
417 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
419 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
420 irq_handler_t handler,
421 unsigned long irqflags, const char *devname, void *dev_id)
426 irq = bind_virq_to_irq(virq, cpu);
427 retval = request_irq(irq, handler, irqflags, devname, dev_id);
429 unbind_from_irq(irq);
435 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
437 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
439 irq_handler_t handler,
440 unsigned long irqflags,
446 irq = bind_ipi_to_irq(ipi, cpu);
450 retval = request_irq(irq, handler, irqflags, devname, dev_id);
452 unbind_from_irq(irq);
459 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
461 free_irq(irq, dev_id);
462 unbind_from_irq(irq);
464 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
466 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
468 int irq = per_cpu(ipi_to_irq, cpu)[vector];
470 notify_remote_via_irq(irq);
473 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
475 struct shared_info *sh = HYPERVISOR_shared_info;
476 int cpu = smp_processor_id();
479 static DEFINE_SPINLOCK(debug_lock);
481 spin_lock_irqsave(&debug_lock, flags);
483 printk("vcpu %d\n ", cpu);
485 for_each_online_cpu(i) {
486 struct vcpu_info *v = per_cpu(xen_vcpu, i);
487 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
488 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
489 v->evtchn_upcall_pending,
490 v->evtchn_pending_sel);
492 printk("pending:\n ");
493 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
494 printk("%08lx%s", sh->evtchn_pending[i],
495 i % 8 == 0 ? "\n " : " ");
496 printk("\nmasks:\n ");
497 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
498 printk("%08lx%s", sh->evtchn_mask[i],
499 i % 8 == 0 ? "\n " : " ");
501 printk("\nunmasked:\n ");
502 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
503 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
504 i % 8 == 0 ? "\n " : " ");
506 printk("\npending list:\n");
507 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
508 if (sync_test_bit(i, sh->evtchn_pending)) {
509 printk(" %d: event %d -> irq %d\n",
515 spin_unlock_irqrestore(&debug_lock, flags);
521 static void xen_do_irq(unsigned irq, struct pt_regs *regs)
523 struct pt_regs *old_regs = set_irq_regs(regs);
525 if (WARN_ON(irq == -1))
531 //printk("cpu %d handling irq %d\n", smp_processor_id(), info->irq);
532 handle_irq(irq, regs);
536 set_irq_regs(old_regs);
540 * Search the CPUs pending events bitmasks. For each one found, map
541 * the event number to an irq, and feed it into do_IRQ() for
544 * Xen uses a two-level bitmap to speed searching. The first level is
545 * a bitset of words which contain pending event bits. The second
546 * level is a bitset of pending events themselves.
548 void xen_evtchn_do_upcall(struct pt_regs *regs)
551 struct shared_info *s = HYPERVISOR_shared_info;
552 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
553 static DEFINE_PER_CPU(unsigned, nesting_count);
557 unsigned long pending_words;
559 vcpu_info->evtchn_upcall_pending = 0;
561 if (__get_cpu_var(nesting_count)++)
564 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
565 /* Clear master flag /before/ clearing selector flag. */
568 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
569 while (pending_words != 0) {
570 unsigned long pending_bits;
571 int word_idx = __ffs(pending_words);
572 pending_words &= ~(1UL << word_idx);
574 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
575 int bit_idx = __ffs(pending_bits);
576 int port = (word_idx * BITS_PER_LONG) + bit_idx;
577 int irq = evtchn_to_irq[port];
579 xen_do_irq(irq, regs);
583 BUG_ON(!irqs_disabled());
585 count = __get_cpu_var(nesting_count);
586 __get_cpu_var(nesting_count) = 0;
593 /* Rebind a new event channel to an existing irq. */
594 void rebind_evtchn_irq(int evtchn, int irq)
596 /* Make sure the irq is masked, since the new event channel
597 will also be masked. */
600 spin_lock(&irq_mapping_update_lock);
602 /* After resume the irq<->evtchn mappings are all cleared out */
603 BUG_ON(evtchn_to_irq[evtchn] != -1);
604 /* Expect irq to have been bound before,
605 so the bindcount should be non-0 */
606 BUG_ON(irq_bindcount[irq] == 0);
608 evtchn_to_irq[evtchn] = irq;
609 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
611 spin_unlock(&irq_mapping_update_lock);
613 /* new event channels are always bound to cpu 0 */
614 irq_set_affinity(irq, cpumask_of(0));
616 /* Unmask the event channel. */
620 /* Rebind an evtchn so that it gets delivered to a specific cpu */
621 static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
623 struct evtchn_bind_vcpu bind_vcpu;
624 int evtchn = evtchn_from_irq(irq);
626 if (!VALID_EVTCHN(evtchn))
629 /* Send future instances of this interrupt to other vcpu. */
630 bind_vcpu.port = evtchn;
631 bind_vcpu.vcpu = tcpu;
634 * If this fails, it usually just indicates that we're dealing with a
635 * virq or IPI channel, which don't actually need to be rebound. Ignore
636 * it, but don't do the xenlinux-level rebind in that case.
638 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
639 bind_evtchn_to_cpu(evtchn, tcpu);
643 static void set_affinity_irq(unsigned irq, const struct cpumask *dest)
645 unsigned tcpu = cpumask_first(dest);
646 rebind_irq_to_cpu(irq, tcpu);
649 int resend_irq_on_evtchn(unsigned int irq)
651 int masked, evtchn = evtchn_from_irq(irq);
652 struct shared_info *s = HYPERVISOR_shared_info;
654 if (!VALID_EVTCHN(evtchn))
657 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
658 sync_set_bit(evtchn, s->evtchn_pending);
660 unmask_evtchn(evtchn);
665 static void enable_dynirq(unsigned int irq)
667 int evtchn = evtchn_from_irq(irq);
669 if (VALID_EVTCHN(evtchn))
670 unmask_evtchn(evtchn);
673 static void disable_dynirq(unsigned int irq)
675 int evtchn = evtchn_from_irq(irq);
677 if (VALID_EVTCHN(evtchn))
681 static void ack_dynirq(unsigned int irq)
683 int evtchn = evtchn_from_irq(irq);
685 move_native_irq(irq);
687 if (VALID_EVTCHN(evtchn))
688 clear_evtchn(evtchn);
691 static int retrigger_dynirq(unsigned int irq)
693 int evtchn = evtchn_from_irq(irq);
694 struct shared_info *sh = HYPERVISOR_shared_info;
697 if (VALID_EVTCHN(evtchn)) {
700 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
701 sync_set_bit(evtchn, sh->evtchn_pending);
703 unmask_evtchn(evtchn);
710 static void restore_cpu_virqs(unsigned int cpu)
712 struct evtchn_bind_virq bind_virq;
713 int virq, irq, evtchn;
715 for (virq = 0; virq < NR_VIRQS; virq++) {
716 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
719 BUG_ON(irq_info[irq].type != IRQT_VIRQ);
720 BUG_ON(irq_info[irq].index != virq);
722 /* Get a new binding from Xen. */
723 bind_virq.virq = virq;
724 bind_virq.vcpu = cpu;
725 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
728 evtchn = bind_virq.port;
730 /* Record the new mapping. */
731 evtchn_to_irq[evtchn] = irq;
732 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
733 bind_evtchn_to_cpu(evtchn, cpu);
736 unmask_evtchn(evtchn);
740 static void restore_cpu_ipis(unsigned int cpu)
742 struct evtchn_bind_ipi bind_ipi;
743 int ipi, irq, evtchn;
745 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
746 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
749 BUG_ON(irq_info[irq].type != IRQT_IPI);
750 BUG_ON(irq_info[irq].index != ipi);
752 /* Get a new binding from Xen. */
754 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
757 evtchn = bind_ipi.port;
759 /* Record the new mapping. */
760 evtchn_to_irq[evtchn] = irq;
761 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
762 bind_evtchn_to_cpu(evtchn, cpu);
765 unmask_evtchn(evtchn);
770 /* Clear an irq's pending state, in preparation for polling on it */
771 void xen_clear_irq_pending(int irq)
773 int evtchn = evtchn_from_irq(irq);
775 if (VALID_EVTCHN(evtchn))
776 clear_evtchn(evtchn);
779 void xen_set_irq_pending(int irq)
781 int evtchn = evtchn_from_irq(irq);
783 if (VALID_EVTCHN(evtchn))
787 bool xen_test_irq_pending(int irq)
789 int evtchn = evtchn_from_irq(irq);
792 if (VALID_EVTCHN(evtchn))
793 ret = test_evtchn(evtchn);
798 /* Poll waiting for an irq to become pending. In the usual case, the
799 irq will be disabled so it won't deliver an interrupt. */
800 void xen_poll_irq(int irq)
802 evtchn_port_t evtchn = evtchn_from_irq(irq);
804 if (VALID_EVTCHN(evtchn)) {
805 struct sched_poll poll;
809 set_xen_guest_handle(poll.ports, &evtchn);
811 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
816 void xen_irq_resume(void)
818 unsigned int cpu, irq, evtchn;
820 init_evtchn_cpu_bindings();
822 /* New event-channel space is not 'live' yet. */
823 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
826 /* No IRQ <-> event-channel mappings. */
827 for (irq = 0; irq < nr_irqs; irq++)
828 irq_info[irq].evtchn = 0; /* zap event-channel binding */
830 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
831 evtchn_to_irq[evtchn] = -1;
833 for_each_possible_cpu(cpu) {
834 restore_cpu_virqs(cpu);
835 restore_cpu_ipis(cpu);
839 static struct irq_chip xen_dynamic_chip __read_mostly = {
842 .disable = disable_dynirq,
843 .mask = disable_dynirq,
844 .unmask = enable_dynirq,
847 .set_affinity = set_affinity_irq,
848 .retrigger = retrigger_dynirq,
851 void __init xen_init_IRQ(void)
854 size_t size = nr_cpu_ids * sizeof(struct cpu_evtchn_s);
856 cpu_evtchn_mask_p = alloc_bootmem(size);
857 BUG_ON(cpu_evtchn_mask_p == NULL);
859 init_evtchn_cpu_bindings();
861 /* No event channels are 'live' right now. */
862 for (i = 0; i < NR_EVENT_CHANNELS; i++)
865 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
866 for (i = 0; i < nr_irqs; i++)
867 irq_bindcount[i] = 0;
869 irq_ctx_init(smp_processor_id());