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_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)
141 /* By default all event channels notify CPU#0. */
142 for (i = 0; i < NR_IRQS; i++)
143 irq_desc[i].affinity = cpumask_of_cpu(0);
146 memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
147 memset(cpu_evtchn_mask[0], ~0, sizeof(cpu_evtchn_mask[0]));
150 static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
152 return cpu_evtchn[evtchn];
155 static inline void clear_evtchn(int port)
157 struct shared_info *s = HYPERVISOR_shared_info;
158 sync_clear_bit(port, &s->evtchn_pending[0]);
161 static inline void set_evtchn(int port)
163 struct shared_info *s = HYPERVISOR_shared_info;
164 sync_set_bit(port, &s->evtchn_pending[0]);
167 static inline int test_evtchn(int port)
169 struct shared_info *s = HYPERVISOR_shared_info;
170 return sync_test_bit(port, &s->evtchn_pending[0]);
175 * notify_remote_via_irq - send event to remote end of event channel via irq
176 * @irq: irq of event channel to send event to
178 * Unlike notify_remote_via_evtchn(), this is safe to use across
179 * save/restore. Notifications on a broken connection are silently
182 void notify_remote_via_irq(int irq)
184 int evtchn = evtchn_from_irq(irq);
186 if (VALID_EVTCHN(evtchn))
187 notify_remote_via_evtchn(evtchn);
189 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
191 static void mask_evtchn(int port)
193 struct shared_info *s = HYPERVISOR_shared_info;
194 sync_set_bit(port, &s->evtchn_mask[0]);
197 static void unmask_evtchn(int port)
199 struct shared_info *s = HYPERVISOR_shared_info;
200 unsigned int cpu = get_cpu();
202 BUG_ON(!irqs_disabled());
204 /* Slow path (hypercall) if this is a non-local port. */
205 if (unlikely(cpu != cpu_from_evtchn(port))) {
206 struct evtchn_unmask unmask = { .port = port };
207 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
209 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
211 sync_clear_bit(port, &s->evtchn_mask[0]);
214 * The following is basically the equivalent of
215 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
216 * the interrupt edge' if the channel is masked.
218 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
219 !sync_test_and_set_bit(port / BITS_PER_LONG,
220 &vcpu_info->evtchn_pending_sel))
221 vcpu_info->evtchn_upcall_pending = 1;
227 static int find_unbound_irq(void)
231 /* Only allocate from dynirq range */
232 for (irq = 0; irq < NR_IRQS; irq++)
233 if (irq_bindcount[irq] == 0)
237 panic("No available IRQ to bind to: increase NR_IRQS!\n");
242 int bind_evtchn_to_irq(unsigned int evtchn)
246 spin_lock(&irq_mapping_update_lock);
248 irq = evtchn_to_irq[evtchn];
251 irq = find_unbound_irq();
253 dynamic_irq_init(irq);
254 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
255 handle_level_irq, "event");
257 evtchn_to_irq[evtchn] = irq;
258 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
261 irq_bindcount[irq]++;
263 spin_unlock(&irq_mapping_update_lock);
267 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
269 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
271 struct evtchn_bind_ipi bind_ipi;
274 spin_lock(&irq_mapping_update_lock);
276 irq = per_cpu(ipi_to_irq, cpu)[ipi];
278 irq = find_unbound_irq();
282 dynamic_irq_init(irq);
283 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
284 handle_level_irq, "ipi");
287 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
290 evtchn = bind_ipi.port;
292 evtchn_to_irq[evtchn] = irq;
293 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
295 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
297 bind_evtchn_to_cpu(evtchn, cpu);
300 irq_bindcount[irq]++;
303 spin_unlock(&irq_mapping_update_lock);
308 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
310 struct evtchn_bind_virq bind_virq;
313 spin_lock(&irq_mapping_update_lock);
315 irq = per_cpu(virq_to_irq, cpu)[virq];
318 bind_virq.virq = virq;
319 bind_virq.vcpu = cpu;
320 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
323 evtchn = bind_virq.port;
325 irq = find_unbound_irq();
327 dynamic_irq_init(irq);
328 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
329 handle_level_irq, "virq");
331 evtchn_to_irq[evtchn] = irq;
332 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
334 per_cpu(virq_to_irq, cpu)[virq] = irq;
336 bind_evtchn_to_cpu(evtchn, cpu);
339 irq_bindcount[irq]++;
341 spin_unlock(&irq_mapping_update_lock);
346 static void unbind_from_irq(unsigned int irq)
348 struct evtchn_close close;
349 int evtchn = evtchn_from_irq(irq);
351 spin_lock(&irq_mapping_update_lock);
353 if ((--irq_bindcount[irq] == 0) && VALID_EVTCHN(evtchn)) {
355 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
358 switch (type_from_irq(irq)) {
360 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
361 [index_from_irq(irq)] = -1;
367 /* Closed ports are implicitly re-bound to VCPU0. */
368 bind_evtchn_to_cpu(evtchn, 0);
370 evtchn_to_irq[evtchn] = -1;
371 irq_info[irq] = IRQ_UNBOUND;
373 dynamic_irq_cleanup(irq);
376 spin_unlock(&irq_mapping_update_lock);
379 int bind_evtchn_to_irqhandler(unsigned int evtchn,
380 irq_handler_t handler,
381 unsigned long irqflags,
382 const char *devname, void *dev_id)
387 irq = bind_evtchn_to_irq(evtchn);
388 retval = request_irq(irq, handler, irqflags, devname, dev_id);
390 unbind_from_irq(irq);
396 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
398 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
399 irq_handler_t handler,
400 unsigned long irqflags, const char *devname, void *dev_id)
405 irq = bind_virq_to_irq(virq, cpu);
406 retval = request_irq(irq, handler, irqflags, devname, dev_id);
408 unbind_from_irq(irq);
414 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
416 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
418 irq_handler_t handler,
419 unsigned long irqflags,
425 irq = bind_ipi_to_irq(ipi, cpu);
429 retval = request_irq(irq, handler, irqflags, devname, dev_id);
431 unbind_from_irq(irq);
438 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
440 free_irq(irq, dev_id);
441 unbind_from_irq(irq);
443 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
445 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
447 int irq = per_cpu(ipi_to_irq, cpu)[vector];
449 notify_remote_via_irq(irq);
452 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
454 struct shared_info *sh = HYPERVISOR_shared_info;
455 int cpu = smp_processor_id();
458 static DEFINE_SPINLOCK(debug_lock);
460 spin_lock_irqsave(&debug_lock, flags);
462 printk("vcpu %d\n ", cpu);
464 for_each_online_cpu(i) {
465 struct vcpu_info *v = per_cpu(xen_vcpu, i);
466 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
467 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
468 v->evtchn_upcall_pending,
469 v->evtchn_pending_sel);
471 printk("pending:\n ");
472 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
473 printk("%08lx%s", sh->evtchn_pending[i],
474 i % 8 == 0 ? "\n " : " ");
475 printk("\nmasks:\n ");
476 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
477 printk("%08lx%s", sh->evtchn_mask[i],
478 i % 8 == 0 ? "\n " : " ");
480 printk("\nunmasked:\n ");
481 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
482 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
483 i % 8 == 0 ? "\n " : " ");
485 printk("\npending list:\n");
486 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
487 if (sync_test_bit(i, sh->evtchn_pending)) {
488 printk(" %d: event %d -> irq %d\n",
494 spin_unlock_irqrestore(&debug_lock, flags);
501 * Search the CPUs pending events bitmasks. For each one found, map
502 * the event number to an irq, and feed it into do_IRQ() for
505 * Xen uses a two-level bitmap to speed searching. The first level is
506 * a bitset of words which contain pending event bits. The second
507 * level is a bitset of pending events themselves.
509 void xen_evtchn_do_upcall(struct pt_regs *regs)
512 struct shared_info *s = HYPERVISOR_shared_info;
513 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
514 static DEFINE_PER_CPU(unsigned, nesting_count);
518 unsigned long pending_words;
520 vcpu_info->evtchn_upcall_pending = 0;
522 if (__get_cpu_var(nesting_count)++)
525 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
526 /* Clear master flag /before/ clearing selector flag. */
529 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
530 while (pending_words != 0) {
531 unsigned long pending_bits;
532 int word_idx = __ffs(pending_words);
533 pending_words &= ~(1UL << word_idx);
535 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
536 int bit_idx = __ffs(pending_bits);
537 int port = (word_idx * BITS_PER_LONG) + bit_idx;
538 int irq = evtchn_to_irq[port];
541 xen_do_IRQ(irq, regs);
545 BUG_ON(!irqs_disabled());
547 count = __get_cpu_var(nesting_count);
548 __get_cpu_var(nesting_count) = 0;
555 /* Rebind a new event channel to an existing irq. */
556 void rebind_evtchn_irq(int evtchn, int irq)
558 /* Make sure the irq is masked, since the new event channel
559 will also be masked. */
562 spin_lock(&irq_mapping_update_lock);
564 /* After resume the irq<->evtchn mappings are all cleared out */
565 BUG_ON(evtchn_to_irq[evtchn] != -1);
566 /* Expect irq to have been bound before,
567 so the bindcount should be non-0 */
568 BUG_ON(irq_bindcount[irq] == 0);
570 evtchn_to_irq[evtchn] = irq;
571 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
573 spin_unlock(&irq_mapping_update_lock);
575 /* new event channels are always bound to cpu 0 */
576 irq_set_affinity(irq, cpumask_of_cpu(0));
578 /* Unmask the event channel. */
582 /* Rebind an evtchn so that it gets delivered to a specific cpu */
583 static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
585 struct evtchn_bind_vcpu bind_vcpu;
586 int evtchn = evtchn_from_irq(irq);
588 if (!VALID_EVTCHN(evtchn))
591 /* Send future instances of this interrupt to other vcpu. */
592 bind_vcpu.port = evtchn;
593 bind_vcpu.vcpu = tcpu;
596 * If this fails, it usually just indicates that we're dealing with a
597 * virq or IPI channel, which don't actually need to be rebound. Ignore
598 * it, but don't do the xenlinux-level rebind in that case.
600 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
601 bind_evtchn_to_cpu(evtchn, tcpu);
605 static void set_affinity_irq(unsigned irq, cpumask_t dest)
607 unsigned tcpu = first_cpu(dest);
608 rebind_irq_to_cpu(irq, tcpu);
611 int resend_irq_on_evtchn(unsigned int irq)
613 int masked, evtchn = evtchn_from_irq(irq);
614 struct shared_info *s = HYPERVISOR_shared_info;
616 if (!VALID_EVTCHN(evtchn))
619 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
620 sync_set_bit(evtchn, s->evtchn_pending);
622 unmask_evtchn(evtchn);
627 static void enable_dynirq(unsigned int irq)
629 int evtchn = evtchn_from_irq(irq);
631 if (VALID_EVTCHN(evtchn))
632 unmask_evtchn(evtchn);
635 static void disable_dynirq(unsigned int irq)
637 int evtchn = evtchn_from_irq(irq);
639 if (VALID_EVTCHN(evtchn))
643 static void ack_dynirq(unsigned int irq)
645 int evtchn = evtchn_from_irq(irq);
647 move_native_irq(irq);
649 if (VALID_EVTCHN(evtchn))
650 clear_evtchn(evtchn);
653 static int retrigger_dynirq(unsigned int irq)
655 int evtchn = evtchn_from_irq(irq);
656 struct shared_info *sh = HYPERVISOR_shared_info;
659 if (VALID_EVTCHN(evtchn)) {
662 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
663 sync_set_bit(evtchn, sh->evtchn_pending);
665 unmask_evtchn(evtchn);
672 static void restore_cpu_virqs(unsigned int cpu)
674 struct evtchn_bind_virq bind_virq;
675 int virq, irq, evtchn;
677 for (virq = 0; virq < NR_VIRQS; virq++) {
678 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
681 BUG_ON(irq_info[irq].type != IRQT_VIRQ);
682 BUG_ON(irq_info[irq].index != virq);
684 /* Get a new binding from Xen. */
685 bind_virq.virq = virq;
686 bind_virq.vcpu = cpu;
687 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
690 evtchn = bind_virq.port;
692 /* Record the new mapping. */
693 evtchn_to_irq[evtchn] = irq;
694 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
695 bind_evtchn_to_cpu(evtchn, cpu);
698 unmask_evtchn(evtchn);
702 static void restore_cpu_ipis(unsigned int cpu)
704 struct evtchn_bind_ipi bind_ipi;
705 int ipi, irq, evtchn;
707 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
708 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
711 BUG_ON(irq_info[irq].type != IRQT_IPI);
712 BUG_ON(irq_info[irq].index != ipi);
714 /* Get a new binding from Xen. */
716 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
719 evtchn = bind_ipi.port;
721 /* Record the new mapping. */
722 evtchn_to_irq[evtchn] = irq;
723 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
724 bind_evtchn_to_cpu(evtchn, cpu);
727 unmask_evtchn(evtchn);
732 /* Clear an irq's pending state, in preparation for polling on it */
733 void xen_clear_irq_pending(int irq)
735 int evtchn = evtchn_from_irq(irq);
737 if (VALID_EVTCHN(evtchn))
738 clear_evtchn(evtchn);
741 void xen_set_irq_pending(int irq)
743 int evtchn = evtchn_from_irq(irq);
745 if (VALID_EVTCHN(evtchn))
749 bool xen_test_irq_pending(int irq)
751 int evtchn = evtchn_from_irq(irq);
754 if (VALID_EVTCHN(evtchn))
755 ret = test_evtchn(evtchn);
760 /* Poll waiting for an irq to become pending. In the usual case, the
761 irq will be disabled so it won't deliver an interrupt. */
762 void xen_poll_irq(int irq)
764 evtchn_port_t evtchn = evtchn_from_irq(irq);
766 if (VALID_EVTCHN(evtchn)) {
767 struct sched_poll poll;
771 poll.ports = &evtchn;
773 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
778 void xen_irq_resume(void)
780 unsigned int cpu, irq, evtchn;
782 init_evtchn_cpu_bindings();
784 /* New event-channel space is not 'live' yet. */
785 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
788 /* No IRQ <-> event-channel mappings. */
789 for (irq = 0; irq < NR_IRQS; irq++)
790 irq_info[irq].evtchn = 0; /* zap event-channel binding */
792 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
793 evtchn_to_irq[evtchn] = -1;
795 for_each_possible_cpu(cpu) {
796 restore_cpu_virqs(cpu);
797 restore_cpu_ipis(cpu);
801 static struct irq_chip xen_dynamic_chip __read_mostly = {
803 .mask = disable_dynirq,
804 .unmask = enable_dynirq,
806 .set_affinity = set_affinity_irq,
807 .retrigger = retrigger_dynirq,
810 void __init xen_init_IRQ(void)
814 init_evtchn_cpu_bindings();
816 /* No event channels are 'live' right now. */
817 for (i = 0; i < NR_EVENT_CHANNELS; i++)
820 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
821 for (i = 0; i < NR_IRQS; i++)
822 irq_bindcount[i] = 0;
824 irq_ctx_init(smp_processor_id());