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>
33 #include <asm/sync_bitops.h>
34 #include <asm/xen/hypercall.h>
35 #include <asm/xen/hypervisor.h>
37 #include <xen/xen-ops.h>
38 #include <xen/events.h>
39 #include <xen/interface/xen.h>
40 #include <xen/interface/event_channel.h>
43 * This lock protects updates to the following mapping and reference-count
44 * arrays. The lock does not need to be acquired to read the mapping tables.
46 static DEFINE_SPINLOCK(irq_mapping_update_lock);
48 /* IRQ <-> VIRQ mapping. */
49 static DEFINE_PER_CPU(int, virq_to_irq[NR_VIRQS]) = {[0 ... NR_VIRQS-1] = -1};
51 /* IRQ <-> IPI mapping */
52 static DEFINE_PER_CPU(int, ipi_to_irq[XEN_NR_IPIS]) = {[0 ... XEN_NR_IPIS-1] = -1};
54 /* Packed IRQ information: binding type, sub-type index, and event channel. */
57 unsigned short evtchn;
62 static struct packed_irq irq_info[NR_IRQS];
73 /* Convenient shorthand for packed representation of an unbound IRQ. */
74 #define IRQ_UNBOUND mk_irq_info(IRQT_UNBOUND, 0, 0)
76 static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
77 [0 ... NR_EVENT_CHANNELS-1] = -1
80 unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
82 static struct cpu_evtchn_s *cpu_evtchn_mask_p;
83 static inline unsigned long *cpu_evtchn_mask(int cpu)
85 return cpu_evtchn_mask_p[cpu].bits;
87 static u8 cpu_evtchn[NR_EVENT_CHANNELS];
89 /* Reference counts for bindings to IRQs. */
90 static int irq_bindcount[NR_IRQS];
92 /* Xen will never allocate port zero for any purpose. */
93 #define VALID_EVTCHN(chn) ((chn) != 0)
95 static struct irq_chip xen_dynamic_chip;
97 /* Constructor for packed IRQ information. */
98 static inline struct packed_irq mk_irq_info(u32 type, u32 index, u32 evtchn)
100 return (struct packed_irq) { evtchn, index, type };
104 * Accessors for packed IRQ information.
106 static inline unsigned int evtchn_from_irq(int irq)
108 return irq_info[irq].evtchn;
111 static inline unsigned int index_from_irq(int irq)
113 return irq_info[irq].index;
116 static inline unsigned int type_from_irq(int irq)
118 return irq_info[irq].type;
121 static inline unsigned long active_evtchns(unsigned int cpu,
122 struct shared_info *sh,
125 return (sh->evtchn_pending[idx] &
126 cpu_evtchn_mask(cpu)[idx] &
127 ~sh->evtchn_mask[idx]);
130 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
132 int irq = evtchn_to_irq[chn];
136 cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
139 __clear_bit(chn, cpu_evtchn_mask(cpu_evtchn[chn]));
140 __set_bit(chn, cpu_evtchn_mask(cpu));
142 cpu_evtchn[chn] = cpu;
145 static void init_evtchn_cpu_bindings(void)
148 struct irq_desc *desc;
151 /* By default all event channels notify CPU#0. */
152 for_each_irq_desc(i, desc) {
153 cpumask_copy(desc->affinity, cpumask_of(0));
157 memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
158 memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0)));
161 static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
163 return cpu_evtchn[evtchn];
166 static inline void clear_evtchn(int port)
168 struct shared_info *s = HYPERVISOR_shared_info;
169 sync_clear_bit(port, &s->evtchn_pending[0]);
172 static inline void set_evtchn(int port)
174 struct shared_info *s = HYPERVISOR_shared_info;
175 sync_set_bit(port, &s->evtchn_pending[0]);
178 static inline int test_evtchn(int port)
180 struct shared_info *s = HYPERVISOR_shared_info;
181 return sync_test_bit(port, &s->evtchn_pending[0]);
186 * notify_remote_via_irq - send event to remote end of event channel via irq
187 * @irq: irq of event channel to send event to
189 * Unlike notify_remote_via_evtchn(), this is safe to use across
190 * save/restore. Notifications on a broken connection are silently
193 void notify_remote_via_irq(int irq)
195 int evtchn = evtchn_from_irq(irq);
197 if (VALID_EVTCHN(evtchn))
198 notify_remote_via_evtchn(evtchn);
200 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
202 static void mask_evtchn(int port)
204 struct shared_info *s = HYPERVISOR_shared_info;
205 sync_set_bit(port, &s->evtchn_mask[0]);
208 static void unmask_evtchn(int port)
210 struct shared_info *s = HYPERVISOR_shared_info;
211 unsigned int cpu = get_cpu();
213 BUG_ON(!irqs_disabled());
215 /* Slow path (hypercall) if this is a non-local port. */
216 if (unlikely(cpu != cpu_from_evtchn(port))) {
217 struct evtchn_unmask unmask = { .port = port };
218 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
220 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
222 sync_clear_bit(port, &s->evtchn_mask[0]);
225 * The following is basically the equivalent of
226 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
227 * the interrupt edge' if the channel is masked.
229 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
230 !sync_test_and_set_bit(port / BITS_PER_LONG,
231 &vcpu_info->evtchn_pending_sel))
232 vcpu_info->evtchn_upcall_pending = 1;
238 static int find_unbound_irq(void)
241 struct irq_desc *desc;
243 /* Only allocate from dynirq range */
244 for (irq = 0; irq < nr_irqs; irq++)
245 if (irq_bindcount[irq] == 0)
249 panic("No available IRQ to bind to: increase nr_irqs!\n");
251 desc = irq_to_desc_alloc_cpu(irq, 0);
252 if (WARN_ON(desc == NULL))
258 int bind_evtchn_to_irq(unsigned int evtchn)
262 spin_lock(&irq_mapping_update_lock);
264 irq = evtchn_to_irq[evtchn];
267 irq = find_unbound_irq();
269 dynamic_irq_init(irq);
270 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
271 handle_level_irq, "event");
273 evtchn_to_irq[evtchn] = irq;
274 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
277 irq_bindcount[irq]++;
279 spin_unlock(&irq_mapping_update_lock);
283 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
285 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
287 struct evtchn_bind_ipi bind_ipi;
290 spin_lock(&irq_mapping_update_lock);
292 irq = per_cpu(ipi_to_irq, cpu)[ipi];
294 irq = find_unbound_irq();
298 dynamic_irq_init(irq);
299 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
300 handle_level_irq, "ipi");
303 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
306 evtchn = bind_ipi.port;
308 evtchn_to_irq[evtchn] = irq;
309 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
311 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
313 bind_evtchn_to_cpu(evtchn, cpu);
316 irq_bindcount[irq]++;
319 spin_unlock(&irq_mapping_update_lock);
324 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
326 struct evtchn_bind_virq bind_virq;
329 spin_lock(&irq_mapping_update_lock);
331 irq = per_cpu(virq_to_irq, cpu)[virq];
334 bind_virq.virq = virq;
335 bind_virq.vcpu = cpu;
336 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
339 evtchn = bind_virq.port;
341 irq = find_unbound_irq();
343 dynamic_irq_init(irq);
344 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
345 handle_level_irq, "virq");
347 evtchn_to_irq[evtchn] = irq;
348 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
350 per_cpu(virq_to_irq, cpu)[virq] = irq;
352 bind_evtchn_to_cpu(evtchn, cpu);
355 irq_bindcount[irq]++;
357 spin_unlock(&irq_mapping_update_lock);
362 static void unbind_from_irq(unsigned int irq)
364 struct evtchn_close close;
365 int evtchn = evtchn_from_irq(irq);
367 spin_lock(&irq_mapping_update_lock);
369 if ((--irq_bindcount[irq] == 0) && VALID_EVTCHN(evtchn)) {
371 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
374 switch (type_from_irq(irq)) {
376 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
377 [index_from_irq(irq)] = -1;
380 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
381 [index_from_irq(irq)] = -1;
387 /* Closed ports are implicitly re-bound to VCPU0. */
388 bind_evtchn_to_cpu(evtchn, 0);
390 evtchn_to_irq[evtchn] = -1;
391 irq_info[irq] = IRQ_UNBOUND;
393 dynamic_irq_cleanup(irq);
396 spin_unlock(&irq_mapping_update_lock);
399 int bind_evtchn_to_irqhandler(unsigned int evtchn,
400 irq_handler_t handler,
401 unsigned long irqflags,
402 const char *devname, void *dev_id)
407 irq = bind_evtchn_to_irq(evtchn);
408 retval = request_irq(irq, handler, irqflags, devname, dev_id);
410 unbind_from_irq(irq);
416 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
418 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
419 irq_handler_t handler,
420 unsigned long irqflags, const char *devname, void *dev_id)
425 irq = bind_virq_to_irq(virq, cpu);
426 retval = request_irq(irq, handler, irqflags, devname, dev_id);
428 unbind_from_irq(irq);
434 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
436 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
438 irq_handler_t handler,
439 unsigned long irqflags,
445 irq = bind_ipi_to_irq(ipi, cpu);
449 retval = request_irq(irq, handler, irqflags, devname, dev_id);
451 unbind_from_irq(irq);
458 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
460 free_irq(irq, dev_id);
461 unbind_from_irq(irq);
463 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
465 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
467 int irq = per_cpu(ipi_to_irq, cpu)[vector];
469 notify_remote_via_irq(irq);
472 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
474 struct shared_info *sh = HYPERVISOR_shared_info;
475 int cpu = smp_processor_id();
478 static DEFINE_SPINLOCK(debug_lock);
480 spin_lock_irqsave(&debug_lock, flags);
482 printk("vcpu %d\n ", cpu);
484 for_each_online_cpu(i) {
485 struct vcpu_info *v = per_cpu(xen_vcpu, i);
486 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
487 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
488 v->evtchn_upcall_pending,
489 v->evtchn_pending_sel);
491 printk("pending:\n ");
492 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
493 printk("%08lx%s", sh->evtchn_pending[i],
494 i % 8 == 0 ? "\n " : " ");
495 printk("\nmasks:\n ");
496 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
497 printk("%08lx%s", sh->evtchn_mask[i],
498 i % 8 == 0 ? "\n " : " ");
500 printk("\nunmasked:\n ");
501 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
502 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
503 i % 8 == 0 ? "\n " : " ");
505 printk("\npending list:\n");
506 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
507 if (sync_test_bit(i, sh->evtchn_pending)) {
508 printk(" %d: event %d -> irq %d\n",
514 spin_unlock_irqrestore(&debug_lock, flags);
521 * Search the CPUs pending events bitmasks. For each one found, map
522 * the event number to an irq, and feed it into do_IRQ() for
525 * Xen uses a two-level bitmap to speed searching. The first level is
526 * a bitset of words which contain pending event bits. The second
527 * level is a bitset of pending events themselves.
529 void xen_evtchn_do_upcall(struct pt_regs *regs)
532 struct shared_info *s = HYPERVISOR_shared_info;
533 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
534 static DEFINE_PER_CPU(unsigned, nesting_count);
538 unsigned long pending_words;
540 vcpu_info->evtchn_upcall_pending = 0;
542 if (__get_cpu_var(nesting_count)++)
545 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
546 /* Clear master flag /before/ clearing selector flag. */
549 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
550 while (pending_words != 0) {
551 unsigned long pending_bits;
552 int word_idx = __ffs(pending_words);
553 pending_words &= ~(1UL << word_idx);
555 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
556 int bit_idx = __ffs(pending_bits);
557 int port = (word_idx * BITS_PER_LONG) + bit_idx;
558 int irq = evtchn_to_irq[port];
561 xen_do_IRQ(irq, regs);
565 BUG_ON(!irqs_disabled());
567 count = __get_cpu_var(nesting_count);
568 __get_cpu_var(nesting_count) = 0;
575 /* Rebind a new event channel to an existing irq. */
576 void rebind_evtchn_irq(int evtchn, int irq)
578 /* Make sure the irq is masked, since the new event channel
579 will also be masked. */
582 spin_lock(&irq_mapping_update_lock);
584 /* After resume the irq<->evtchn mappings are all cleared out */
585 BUG_ON(evtchn_to_irq[evtchn] != -1);
586 /* Expect irq to have been bound before,
587 so the bindcount should be non-0 */
588 BUG_ON(irq_bindcount[irq] == 0);
590 evtchn_to_irq[evtchn] = irq;
591 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
593 spin_unlock(&irq_mapping_update_lock);
595 /* new event channels are always bound to cpu 0 */
596 irq_set_affinity(irq, cpumask_of(0));
598 /* Unmask the event channel. */
602 /* Rebind an evtchn so that it gets delivered to a specific cpu */
603 static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
605 struct evtchn_bind_vcpu bind_vcpu;
606 int evtchn = evtchn_from_irq(irq);
608 if (!VALID_EVTCHN(evtchn))
611 /* Send future instances of this interrupt to other vcpu. */
612 bind_vcpu.port = evtchn;
613 bind_vcpu.vcpu = tcpu;
616 * If this fails, it usually just indicates that we're dealing with a
617 * virq or IPI channel, which don't actually need to be rebound. Ignore
618 * it, but don't do the xenlinux-level rebind in that case.
620 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
621 bind_evtchn_to_cpu(evtchn, tcpu);
625 static void set_affinity_irq(unsigned irq, const struct cpumask *dest)
627 unsigned tcpu = cpumask_first(dest);
628 rebind_irq_to_cpu(irq, tcpu);
631 int resend_irq_on_evtchn(unsigned int irq)
633 int masked, evtchn = evtchn_from_irq(irq);
634 struct shared_info *s = HYPERVISOR_shared_info;
636 if (!VALID_EVTCHN(evtchn))
639 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
640 sync_set_bit(evtchn, s->evtchn_pending);
642 unmask_evtchn(evtchn);
647 static void enable_dynirq(unsigned int irq)
649 int evtchn = evtchn_from_irq(irq);
651 if (VALID_EVTCHN(evtchn))
652 unmask_evtchn(evtchn);
655 static void disable_dynirq(unsigned int irq)
657 int evtchn = evtchn_from_irq(irq);
659 if (VALID_EVTCHN(evtchn))
663 static void ack_dynirq(unsigned int irq)
665 int evtchn = evtchn_from_irq(irq);
667 move_native_irq(irq);
669 if (VALID_EVTCHN(evtchn))
670 clear_evtchn(evtchn);
673 static int retrigger_dynirq(unsigned int irq)
675 int evtchn = evtchn_from_irq(irq);
676 struct shared_info *sh = HYPERVISOR_shared_info;
679 if (VALID_EVTCHN(evtchn)) {
682 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
683 sync_set_bit(evtchn, sh->evtchn_pending);
685 unmask_evtchn(evtchn);
692 static void restore_cpu_virqs(unsigned int cpu)
694 struct evtchn_bind_virq bind_virq;
695 int virq, irq, evtchn;
697 for (virq = 0; virq < NR_VIRQS; virq++) {
698 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
701 BUG_ON(irq_info[irq].type != IRQT_VIRQ);
702 BUG_ON(irq_info[irq].index != virq);
704 /* Get a new binding from Xen. */
705 bind_virq.virq = virq;
706 bind_virq.vcpu = cpu;
707 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
710 evtchn = bind_virq.port;
712 /* Record the new mapping. */
713 evtchn_to_irq[evtchn] = irq;
714 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
715 bind_evtchn_to_cpu(evtchn, cpu);
718 unmask_evtchn(evtchn);
722 static void restore_cpu_ipis(unsigned int cpu)
724 struct evtchn_bind_ipi bind_ipi;
725 int ipi, irq, evtchn;
727 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
728 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
731 BUG_ON(irq_info[irq].type != IRQT_IPI);
732 BUG_ON(irq_info[irq].index != ipi);
734 /* Get a new binding from Xen. */
736 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
739 evtchn = bind_ipi.port;
741 /* Record the new mapping. */
742 evtchn_to_irq[evtchn] = irq;
743 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
744 bind_evtchn_to_cpu(evtchn, cpu);
747 unmask_evtchn(evtchn);
752 /* Clear an irq's pending state, in preparation for polling on it */
753 void xen_clear_irq_pending(int irq)
755 int evtchn = evtchn_from_irq(irq);
757 if (VALID_EVTCHN(evtchn))
758 clear_evtchn(evtchn);
761 void xen_set_irq_pending(int irq)
763 int evtchn = evtchn_from_irq(irq);
765 if (VALID_EVTCHN(evtchn))
769 bool xen_test_irq_pending(int irq)
771 int evtchn = evtchn_from_irq(irq);
774 if (VALID_EVTCHN(evtchn))
775 ret = test_evtchn(evtchn);
780 /* Poll waiting for an irq to become pending. In the usual case, the
781 irq will be disabled so it won't deliver an interrupt. */
782 void xen_poll_irq(int irq)
784 evtchn_port_t evtchn = evtchn_from_irq(irq);
786 if (VALID_EVTCHN(evtchn)) {
787 struct sched_poll poll;
791 set_xen_guest_handle(poll.ports, &evtchn);
793 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
798 void xen_irq_resume(void)
800 unsigned int cpu, irq, evtchn;
802 init_evtchn_cpu_bindings();
804 /* New event-channel space is not 'live' yet. */
805 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
808 /* No IRQ <-> event-channel mappings. */
809 for (irq = 0; irq < nr_irqs; irq++)
810 irq_info[irq].evtchn = 0; /* zap event-channel binding */
812 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
813 evtchn_to_irq[evtchn] = -1;
815 for_each_possible_cpu(cpu) {
816 restore_cpu_virqs(cpu);
817 restore_cpu_ipis(cpu);
821 static struct irq_chip xen_dynamic_chip __read_mostly = {
824 .disable = disable_dynirq,
825 .mask = disable_dynirq,
826 .unmask = enable_dynirq,
829 .set_affinity = set_affinity_irq,
830 .retrigger = retrigger_dynirq,
833 void __init xen_init_IRQ(void)
836 size_t size = nr_cpu_ids * sizeof(struct cpu_evtchn_s);
838 cpu_evtchn_mask_p = alloc_bootmem(size);
839 BUG_ON(cpu_evtchn_mask_p == NULL);
841 init_evtchn_cpu_bindings();
843 /* No event channels are 'live' right now. */
844 for (i = 0; i < NR_EVENT_CHANNELS; i++)
847 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
848 for (i = 0; i < nr_irqs; i++)
849 irq_bindcount[i] = 0;
851 irq_ctx_init(smp_processor_id());