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) {
148 desc->affinity = cpumask_of_cpu(0);
152 memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
153 memset(cpu_evtchn_mask[0], ~0, sizeof(cpu_evtchn_mask[0]));
156 static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
158 return cpu_evtchn[evtchn];
161 static inline void clear_evtchn(int port)
163 struct shared_info *s = HYPERVISOR_shared_info;
164 sync_clear_bit(port, &s->evtchn_pending[0]);
167 static inline void set_evtchn(int port)
169 struct shared_info *s = HYPERVISOR_shared_info;
170 sync_set_bit(port, &s->evtchn_pending[0]);
173 static inline int test_evtchn(int port)
175 struct shared_info *s = HYPERVISOR_shared_info;
176 return sync_test_bit(port, &s->evtchn_pending[0]);
181 * notify_remote_via_irq - send event to remote end of event channel via irq
182 * @irq: irq of event channel to send event to
184 * Unlike notify_remote_via_evtchn(), this is safe to use across
185 * save/restore. Notifications on a broken connection are silently
188 void notify_remote_via_irq(int irq)
190 int evtchn = evtchn_from_irq(irq);
192 if (VALID_EVTCHN(evtchn))
193 notify_remote_via_evtchn(evtchn);
195 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
197 static void mask_evtchn(int port)
199 struct shared_info *s = HYPERVISOR_shared_info;
200 sync_set_bit(port, &s->evtchn_mask[0]);
203 static void unmask_evtchn(int port)
205 struct shared_info *s = HYPERVISOR_shared_info;
206 unsigned int cpu = get_cpu();
208 BUG_ON(!irqs_disabled());
210 /* Slow path (hypercall) if this is a non-local port. */
211 if (unlikely(cpu != cpu_from_evtchn(port))) {
212 struct evtchn_unmask unmask = { .port = port };
213 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
215 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
217 sync_clear_bit(port, &s->evtchn_mask[0]);
220 * The following is basically the equivalent of
221 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
222 * the interrupt edge' if the channel is masked.
224 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
225 !sync_test_and_set_bit(port / BITS_PER_LONG,
226 &vcpu_info->evtchn_pending_sel))
227 vcpu_info->evtchn_upcall_pending = 1;
233 static int find_unbound_irq(void)
237 /* Only allocate from dynirq range */
238 for (irq = 0; irq < nr_irqs; irq++)
239 if (irq_bindcount[irq] == 0)
243 panic("No available IRQ to bind to: increase nr_irqs!\n");
248 int bind_evtchn_to_irq(unsigned int evtchn)
252 spin_lock(&irq_mapping_update_lock);
254 irq = evtchn_to_irq[evtchn];
257 irq = find_unbound_irq();
259 dynamic_irq_init(irq);
260 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
261 handle_level_irq, "event");
263 evtchn_to_irq[evtchn] = irq;
264 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
267 irq_bindcount[irq]++;
269 spin_unlock(&irq_mapping_update_lock);
273 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
275 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
277 struct evtchn_bind_ipi bind_ipi;
280 spin_lock(&irq_mapping_update_lock);
282 irq = per_cpu(ipi_to_irq, cpu)[ipi];
284 irq = find_unbound_irq();
288 dynamic_irq_init(irq);
289 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
290 handle_level_irq, "ipi");
293 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
296 evtchn = bind_ipi.port;
298 evtchn_to_irq[evtchn] = irq;
299 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
301 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
303 bind_evtchn_to_cpu(evtchn, cpu);
306 irq_bindcount[irq]++;
309 spin_unlock(&irq_mapping_update_lock);
314 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
316 struct evtchn_bind_virq bind_virq;
319 spin_lock(&irq_mapping_update_lock);
321 irq = per_cpu(virq_to_irq, cpu)[virq];
324 bind_virq.virq = virq;
325 bind_virq.vcpu = cpu;
326 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
329 evtchn = bind_virq.port;
331 irq = find_unbound_irq();
333 dynamic_irq_init(irq);
334 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
335 handle_level_irq, "virq");
337 evtchn_to_irq[evtchn] = irq;
338 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
340 per_cpu(virq_to_irq, cpu)[virq] = irq;
342 bind_evtchn_to_cpu(evtchn, cpu);
345 irq_bindcount[irq]++;
347 spin_unlock(&irq_mapping_update_lock);
352 static void unbind_from_irq(unsigned int irq)
354 struct evtchn_close close;
355 int evtchn = evtchn_from_irq(irq);
357 spin_lock(&irq_mapping_update_lock);
359 if ((--irq_bindcount[irq] == 0) && VALID_EVTCHN(evtchn)) {
361 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
364 switch (type_from_irq(irq)) {
366 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
367 [index_from_irq(irq)] = -1;
370 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
371 [index_from_irq(irq)] = -1;
377 /* Closed ports are implicitly re-bound to VCPU0. */
378 bind_evtchn_to_cpu(evtchn, 0);
380 evtchn_to_irq[evtchn] = -1;
381 irq_info[irq] = IRQ_UNBOUND;
383 dynamic_irq_cleanup(irq);
386 spin_unlock(&irq_mapping_update_lock);
389 int bind_evtchn_to_irqhandler(unsigned int evtchn,
390 irq_handler_t handler,
391 unsigned long irqflags,
392 const char *devname, void *dev_id)
397 irq = bind_evtchn_to_irq(evtchn);
398 retval = request_irq(irq, handler, irqflags, devname, dev_id);
400 unbind_from_irq(irq);
406 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
408 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
409 irq_handler_t handler,
410 unsigned long irqflags, const char *devname, void *dev_id)
415 irq = bind_virq_to_irq(virq, cpu);
416 retval = request_irq(irq, handler, irqflags, devname, dev_id);
418 unbind_from_irq(irq);
424 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
426 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
428 irq_handler_t handler,
429 unsigned long irqflags,
435 irq = bind_ipi_to_irq(ipi, cpu);
439 retval = request_irq(irq, handler, irqflags, devname, dev_id);
441 unbind_from_irq(irq);
448 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
450 free_irq(irq, dev_id);
451 unbind_from_irq(irq);
453 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
455 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
457 int irq = per_cpu(ipi_to_irq, cpu)[vector];
459 notify_remote_via_irq(irq);
462 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
464 struct shared_info *sh = HYPERVISOR_shared_info;
465 int cpu = smp_processor_id();
468 static DEFINE_SPINLOCK(debug_lock);
470 spin_lock_irqsave(&debug_lock, flags);
472 printk("vcpu %d\n ", cpu);
474 for_each_online_cpu(i) {
475 struct vcpu_info *v = per_cpu(xen_vcpu, i);
476 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
477 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
478 v->evtchn_upcall_pending,
479 v->evtchn_pending_sel);
481 printk("pending:\n ");
482 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
483 printk("%08lx%s", sh->evtchn_pending[i],
484 i % 8 == 0 ? "\n " : " ");
485 printk("\nmasks:\n ");
486 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
487 printk("%08lx%s", sh->evtchn_mask[i],
488 i % 8 == 0 ? "\n " : " ");
490 printk("\nunmasked:\n ");
491 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
492 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
493 i % 8 == 0 ? "\n " : " ");
495 printk("\npending list:\n");
496 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
497 if (sync_test_bit(i, sh->evtchn_pending)) {
498 printk(" %d: event %d -> irq %d\n",
504 spin_unlock_irqrestore(&debug_lock, flags);
511 * Search the CPUs pending events bitmasks. For each one found, map
512 * the event number to an irq, and feed it into do_IRQ() for
515 * Xen uses a two-level bitmap to speed searching. The first level is
516 * a bitset of words which contain pending event bits. The second
517 * level is a bitset of pending events themselves.
519 void xen_evtchn_do_upcall(struct pt_regs *regs)
522 struct shared_info *s = HYPERVISOR_shared_info;
523 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
524 static DEFINE_PER_CPU(unsigned, nesting_count);
528 unsigned long pending_words;
530 vcpu_info->evtchn_upcall_pending = 0;
532 if (__get_cpu_var(nesting_count)++)
535 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
536 /* Clear master flag /before/ clearing selector flag. */
539 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
540 while (pending_words != 0) {
541 unsigned long pending_bits;
542 int word_idx = __ffs(pending_words);
543 pending_words &= ~(1UL << word_idx);
545 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
546 int bit_idx = __ffs(pending_bits);
547 int port = (word_idx * BITS_PER_LONG) + bit_idx;
548 int irq = evtchn_to_irq[port];
551 xen_do_IRQ(irq, regs);
555 BUG_ON(!irqs_disabled());
557 count = __get_cpu_var(nesting_count);
558 __get_cpu_var(nesting_count) = 0;
565 /* Rebind a new event channel to an existing irq. */
566 void rebind_evtchn_irq(int evtchn, int irq)
568 /* Make sure the irq is masked, since the new event channel
569 will also be masked. */
572 spin_lock(&irq_mapping_update_lock);
574 /* After resume the irq<->evtchn mappings are all cleared out */
575 BUG_ON(evtchn_to_irq[evtchn] != -1);
576 /* Expect irq to have been bound before,
577 so the bindcount should be non-0 */
578 BUG_ON(irq_bindcount[irq] == 0);
580 evtchn_to_irq[evtchn] = irq;
581 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
583 spin_unlock(&irq_mapping_update_lock);
585 /* new event channels are always bound to cpu 0 */
586 irq_set_affinity(irq, cpumask_of(0));
588 /* Unmask the event channel. */
592 /* Rebind an evtchn so that it gets delivered to a specific cpu */
593 static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
595 struct evtchn_bind_vcpu bind_vcpu;
596 int evtchn = evtchn_from_irq(irq);
598 if (!VALID_EVTCHN(evtchn))
601 /* Send future instances of this interrupt to other vcpu. */
602 bind_vcpu.port = evtchn;
603 bind_vcpu.vcpu = tcpu;
606 * If this fails, it usually just indicates that we're dealing with a
607 * virq or IPI channel, which don't actually need to be rebound. Ignore
608 * it, but don't do the xenlinux-level rebind in that case.
610 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
611 bind_evtchn_to_cpu(evtchn, tcpu);
615 static void set_affinity_irq(unsigned irq, const struct cpumask *dest)
617 unsigned tcpu = cpumask_first(dest);
618 rebind_irq_to_cpu(irq, tcpu);
621 int resend_irq_on_evtchn(unsigned int irq)
623 int masked, evtchn = evtchn_from_irq(irq);
624 struct shared_info *s = HYPERVISOR_shared_info;
626 if (!VALID_EVTCHN(evtchn))
629 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
630 sync_set_bit(evtchn, s->evtchn_pending);
632 unmask_evtchn(evtchn);
637 static void enable_dynirq(unsigned int irq)
639 int evtchn = evtchn_from_irq(irq);
641 if (VALID_EVTCHN(evtchn))
642 unmask_evtchn(evtchn);
645 static void disable_dynirq(unsigned int irq)
647 int evtchn = evtchn_from_irq(irq);
649 if (VALID_EVTCHN(evtchn))
653 static void ack_dynirq(unsigned int irq)
655 int evtchn = evtchn_from_irq(irq);
657 move_native_irq(irq);
659 if (VALID_EVTCHN(evtchn))
660 clear_evtchn(evtchn);
663 static int retrigger_dynirq(unsigned int irq)
665 int evtchn = evtchn_from_irq(irq);
666 struct shared_info *sh = HYPERVISOR_shared_info;
669 if (VALID_EVTCHN(evtchn)) {
672 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
673 sync_set_bit(evtchn, sh->evtchn_pending);
675 unmask_evtchn(evtchn);
682 static void restore_cpu_virqs(unsigned int cpu)
684 struct evtchn_bind_virq bind_virq;
685 int virq, irq, evtchn;
687 for (virq = 0; virq < NR_VIRQS; virq++) {
688 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
691 BUG_ON(irq_info[irq].type != IRQT_VIRQ);
692 BUG_ON(irq_info[irq].index != virq);
694 /* Get a new binding from Xen. */
695 bind_virq.virq = virq;
696 bind_virq.vcpu = cpu;
697 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
700 evtchn = bind_virq.port;
702 /* Record the new mapping. */
703 evtchn_to_irq[evtchn] = irq;
704 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
705 bind_evtchn_to_cpu(evtchn, cpu);
708 unmask_evtchn(evtchn);
712 static void restore_cpu_ipis(unsigned int cpu)
714 struct evtchn_bind_ipi bind_ipi;
715 int ipi, irq, evtchn;
717 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
718 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
721 BUG_ON(irq_info[irq].type != IRQT_IPI);
722 BUG_ON(irq_info[irq].index != ipi);
724 /* Get a new binding from Xen. */
726 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
729 evtchn = bind_ipi.port;
731 /* Record the new mapping. */
732 evtchn_to_irq[evtchn] = irq;
733 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
734 bind_evtchn_to_cpu(evtchn, cpu);
737 unmask_evtchn(evtchn);
742 /* Clear an irq's pending state, in preparation for polling on it */
743 void xen_clear_irq_pending(int irq)
745 int evtchn = evtchn_from_irq(irq);
747 if (VALID_EVTCHN(evtchn))
748 clear_evtchn(evtchn);
751 void xen_set_irq_pending(int irq)
753 int evtchn = evtchn_from_irq(irq);
755 if (VALID_EVTCHN(evtchn))
759 bool xen_test_irq_pending(int irq)
761 int evtchn = evtchn_from_irq(irq);
764 if (VALID_EVTCHN(evtchn))
765 ret = test_evtchn(evtchn);
770 /* Poll waiting for an irq to become pending. In the usual case, the
771 irq will be disabled so it won't deliver an interrupt. */
772 void xen_poll_irq(int irq)
774 evtchn_port_t evtchn = evtchn_from_irq(irq);
776 if (VALID_EVTCHN(evtchn)) {
777 struct sched_poll poll;
781 set_xen_guest_handle(poll.ports, &evtchn);
783 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
788 void xen_irq_resume(void)
790 unsigned int cpu, irq, evtchn;
792 init_evtchn_cpu_bindings();
794 /* New event-channel space is not 'live' yet. */
795 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
798 /* No IRQ <-> event-channel mappings. */
799 for (irq = 0; irq < nr_irqs; irq++)
800 irq_info[irq].evtchn = 0; /* zap event-channel binding */
802 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
803 evtchn_to_irq[evtchn] = -1;
805 for_each_possible_cpu(cpu) {
806 restore_cpu_virqs(cpu);
807 restore_cpu_ipis(cpu);
811 static struct irq_chip xen_dynamic_chip __read_mostly = {
813 .mask = disable_dynirq,
814 .unmask = enable_dynirq,
816 .set_affinity = set_affinity_irq,
817 .retrigger = retrigger_dynirq,
820 void __init xen_init_IRQ(void)
824 init_evtchn_cpu_bindings();
826 /* No event channels are 'live' right now. */
827 for (i = 0; i < NR_EVENT_CHANNELS; i++)
830 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
831 for (i = 0; i < nr_irqs; i++)
832 irq_bindcount[i] = 0;
834 irq_ctx_init(smp_processor_id());