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)
141 /* By default all event channels notify CPU#0. */
142 for (i = 0; i < nr_irqs; i++) {
143 struct irq_desc *desc = irq_to_desc(i);
146 desc->affinity = cpumask_of_cpu(0);
150 memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
151 memset(cpu_evtchn_mask[0], ~0, sizeof(cpu_evtchn_mask[0]));
154 static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
156 return cpu_evtchn[evtchn];
159 static inline void clear_evtchn(int port)
161 struct shared_info *s = HYPERVISOR_shared_info;
162 sync_clear_bit(port, &s->evtchn_pending[0]);
165 static inline void set_evtchn(int port)
167 struct shared_info *s = HYPERVISOR_shared_info;
168 sync_set_bit(port, &s->evtchn_pending[0]);
171 static inline int test_evtchn(int port)
173 struct shared_info *s = HYPERVISOR_shared_info;
174 return sync_test_bit(port, &s->evtchn_pending[0]);
179 * notify_remote_via_irq - send event to remote end of event channel via irq
180 * @irq: irq of event channel to send event to
182 * Unlike notify_remote_via_evtchn(), this is safe to use across
183 * save/restore. Notifications on a broken connection are silently
186 void notify_remote_via_irq(int irq)
188 int evtchn = evtchn_from_irq(irq);
190 if (VALID_EVTCHN(evtchn))
191 notify_remote_via_evtchn(evtchn);
193 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
195 static void mask_evtchn(int port)
197 struct shared_info *s = HYPERVISOR_shared_info;
198 sync_set_bit(port, &s->evtchn_mask[0]);
201 static void unmask_evtchn(int port)
203 struct shared_info *s = HYPERVISOR_shared_info;
204 unsigned int cpu = get_cpu();
206 BUG_ON(!irqs_disabled());
208 /* Slow path (hypercall) if this is a non-local port. */
209 if (unlikely(cpu != cpu_from_evtchn(port))) {
210 struct evtchn_unmask unmask = { .port = port };
211 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
213 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
215 sync_clear_bit(port, &s->evtchn_mask[0]);
218 * The following is basically the equivalent of
219 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
220 * the interrupt edge' if the channel is masked.
222 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
223 !sync_test_and_set_bit(port / BITS_PER_LONG,
224 &vcpu_info->evtchn_pending_sel))
225 vcpu_info->evtchn_upcall_pending = 1;
231 static int find_unbound_irq(void)
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");
246 int bind_evtchn_to_irq(unsigned int evtchn)
250 spin_lock(&irq_mapping_update_lock);
252 irq = evtchn_to_irq[evtchn];
255 irq = find_unbound_irq();
257 dynamic_irq_init(irq);
258 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
259 handle_level_irq, "event");
261 evtchn_to_irq[evtchn] = irq;
262 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
265 irq_bindcount[irq]++;
267 spin_unlock(&irq_mapping_update_lock);
271 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
273 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
275 struct evtchn_bind_ipi bind_ipi;
278 spin_lock(&irq_mapping_update_lock);
280 irq = per_cpu(ipi_to_irq, cpu)[ipi];
282 irq = find_unbound_irq();
286 dynamic_irq_init(irq);
287 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
288 handle_level_irq, "ipi");
291 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
294 evtchn = bind_ipi.port;
296 evtchn_to_irq[evtchn] = irq;
297 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
299 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
301 bind_evtchn_to_cpu(evtchn, cpu);
304 irq_bindcount[irq]++;
307 spin_unlock(&irq_mapping_update_lock);
312 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
314 struct evtchn_bind_virq bind_virq;
317 spin_lock(&irq_mapping_update_lock);
319 irq = per_cpu(virq_to_irq, cpu)[virq];
322 bind_virq.virq = virq;
323 bind_virq.vcpu = cpu;
324 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
327 evtchn = bind_virq.port;
329 irq = find_unbound_irq();
331 dynamic_irq_init(irq);
332 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
333 handle_level_irq, "virq");
335 evtchn_to_irq[evtchn] = irq;
336 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
338 per_cpu(virq_to_irq, cpu)[virq] = irq;
340 bind_evtchn_to_cpu(evtchn, cpu);
343 irq_bindcount[irq]++;
345 spin_unlock(&irq_mapping_update_lock);
350 static void unbind_from_irq(unsigned int irq)
352 struct evtchn_close close;
353 int evtchn = evtchn_from_irq(irq);
355 spin_lock(&irq_mapping_update_lock);
357 if ((--irq_bindcount[irq] == 0) && VALID_EVTCHN(evtchn)) {
359 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
362 switch (type_from_irq(irq)) {
364 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
365 [index_from_irq(irq)] = -1;
368 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
369 [index_from_irq(irq)] = -1;
375 /* Closed ports are implicitly re-bound to VCPU0. */
376 bind_evtchn_to_cpu(evtchn, 0);
378 evtchn_to_irq[evtchn] = -1;
379 irq_info[irq] = IRQ_UNBOUND;
381 dynamic_irq_cleanup(irq);
384 spin_unlock(&irq_mapping_update_lock);
387 int bind_evtchn_to_irqhandler(unsigned int evtchn,
388 irq_handler_t handler,
389 unsigned long irqflags,
390 const char *devname, void *dev_id)
395 irq = bind_evtchn_to_irq(evtchn);
396 retval = request_irq(irq, handler, irqflags, devname, dev_id);
398 unbind_from_irq(irq);
404 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
406 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
407 irq_handler_t handler,
408 unsigned long irqflags, const char *devname, void *dev_id)
413 irq = bind_virq_to_irq(virq, cpu);
414 retval = request_irq(irq, handler, irqflags, devname, dev_id);
416 unbind_from_irq(irq);
422 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
424 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
426 irq_handler_t handler,
427 unsigned long irqflags,
433 irq = bind_ipi_to_irq(ipi, cpu);
437 retval = request_irq(irq, handler, irqflags, devname, dev_id);
439 unbind_from_irq(irq);
446 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
448 free_irq(irq, dev_id);
449 unbind_from_irq(irq);
451 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
453 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
455 int irq = per_cpu(ipi_to_irq, cpu)[vector];
457 notify_remote_via_irq(irq);
460 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
462 struct shared_info *sh = HYPERVISOR_shared_info;
463 int cpu = smp_processor_id();
466 static DEFINE_SPINLOCK(debug_lock);
468 spin_lock_irqsave(&debug_lock, flags);
470 printk("vcpu %d\n ", cpu);
472 for_each_online_cpu(i) {
473 struct vcpu_info *v = per_cpu(xen_vcpu, i);
474 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
475 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
476 v->evtchn_upcall_pending,
477 v->evtchn_pending_sel);
479 printk("pending:\n ");
480 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
481 printk("%08lx%s", sh->evtchn_pending[i],
482 i % 8 == 0 ? "\n " : " ");
483 printk("\nmasks:\n ");
484 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
485 printk("%08lx%s", sh->evtchn_mask[i],
486 i % 8 == 0 ? "\n " : " ");
488 printk("\nunmasked:\n ");
489 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
490 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
491 i % 8 == 0 ? "\n " : " ");
493 printk("\npending list:\n");
494 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
495 if (sync_test_bit(i, sh->evtchn_pending)) {
496 printk(" %d: event %d -> irq %d\n",
502 spin_unlock_irqrestore(&debug_lock, flags);
509 * Search the CPUs pending events bitmasks. For each one found, map
510 * the event number to an irq, and feed it into do_IRQ() for
513 * Xen uses a two-level bitmap to speed searching. The first level is
514 * a bitset of words which contain pending event bits. The second
515 * level is a bitset of pending events themselves.
517 void xen_evtchn_do_upcall(struct pt_regs *regs)
520 struct shared_info *s = HYPERVISOR_shared_info;
521 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
522 static DEFINE_PER_CPU(unsigned, nesting_count);
526 unsigned long pending_words;
528 vcpu_info->evtchn_upcall_pending = 0;
530 if (__get_cpu_var(nesting_count)++)
533 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
534 /* Clear master flag /before/ clearing selector flag. */
537 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
538 while (pending_words != 0) {
539 unsigned long pending_bits;
540 int word_idx = __ffs(pending_words);
541 pending_words &= ~(1UL << word_idx);
543 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
544 int bit_idx = __ffs(pending_bits);
545 int port = (word_idx * BITS_PER_LONG) + bit_idx;
546 int irq = evtchn_to_irq[port];
549 xen_do_IRQ(irq, regs);
553 BUG_ON(!irqs_disabled());
555 count = __get_cpu_var(nesting_count);
556 __get_cpu_var(nesting_count) = 0;
563 /* Rebind a new event channel to an existing irq. */
564 void rebind_evtchn_irq(int evtchn, int irq)
566 /* Make sure the irq is masked, since the new event channel
567 will also be masked. */
570 spin_lock(&irq_mapping_update_lock);
572 /* After resume the irq<->evtchn mappings are all cleared out */
573 BUG_ON(evtchn_to_irq[evtchn] != -1);
574 /* Expect irq to have been bound before,
575 so the bindcount should be non-0 */
576 BUG_ON(irq_bindcount[irq] == 0);
578 evtchn_to_irq[evtchn] = irq;
579 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
581 spin_unlock(&irq_mapping_update_lock);
583 /* new event channels are always bound to cpu 0 */
584 irq_set_affinity(irq, cpumask_of_cpu(0));
586 /* Unmask the event channel. */
590 /* Rebind an evtchn so that it gets delivered to a specific cpu */
591 static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
593 struct evtchn_bind_vcpu bind_vcpu;
594 int evtchn = evtchn_from_irq(irq);
596 if (!VALID_EVTCHN(evtchn))
599 /* Send future instances of this interrupt to other vcpu. */
600 bind_vcpu.port = evtchn;
601 bind_vcpu.vcpu = tcpu;
604 * If this fails, it usually just indicates that we're dealing with a
605 * virq or IPI channel, which don't actually need to be rebound. Ignore
606 * it, but don't do the xenlinux-level rebind in that case.
608 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
609 bind_evtchn_to_cpu(evtchn, tcpu);
613 static void set_affinity_irq(unsigned irq, cpumask_t dest)
615 unsigned tcpu = first_cpu(dest);
616 rebind_irq_to_cpu(irq, tcpu);
619 int resend_irq_on_evtchn(unsigned int irq)
621 int masked, evtchn = evtchn_from_irq(irq);
622 struct shared_info *s = HYPERVISOR_shared_info;
624 if (!VALID_EVTCHN(evtchn))
627 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
628 sync_set_bit(evtchn, s->evtchn_pending);
630 unmask_evtchn(evtchn);
635 static void enable_dynirq(unsigned int irq)
637 int evtchn = evtchn_from_irq(irq);
639 if (VALID_EVTCHN(evtchn))
640 unmask_evtchn(evtchn);
643 static void disable_dynirq(unsigned int irq)
645 int evtchn = evtchn_from_irq(irq);
647 if (VALID_EVTCHN(evtchn))
651 static void ack_dynirq(unsigned int irq)
653 int evtchn = evtchn_from_irq(irq);
655 move_native_irq(irq);
657 if (VALID_EVTCHN(evtchn))
658 clear_evtchn(evtchn);
661 static int retrigger_dynirq(unsigned int irq)
663 int evtchn = evtchn_from_irq(irq);
664 struct shared_info *sh = HYPERVISOR_shared_info;
667 if (VALID_EVTCHN(evtchn)) {
670 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
671 sync_set_bit(evtchn, sh->evtchn_pending);
673 unmask_evtchn(evtchn);
680 static void restore_cpu_virqs(unsigned int cpu)
682 struct evtchn_bind_virq bind_virq;
683 int virq, irq, evtchn;
685 for (virq = 0; virq < NR_VIRQS; virq++) {
686 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
689 BUG_ON(irq_info[irq].type != IRQT_VIRQ);
690 BUG_ON(irq_info[irq].index != virq);
692 /* Get a new binding from Xen. */
693 bind_virq.virq = virq;
694 bind_virq.vcpu = cpu;
695 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
698 evtchn = bind_virq.port;
700 /* Record the new mapping. */
701 evtchn_to_irq[evtchn] = irq;
702 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
703 bind_evtchn_to_cpu(evtchn, cpu);
706 unmask_evtchn(evtchn);
710 static void restore_cpu_ipis(unsigned int cpu)
712 struct evtchn_bind_ipi bind_ipi;
713 int ipi, irq, evtchn;
715 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
716 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
719 BUG_ON(irq_info[irq].type != IRQT_IPI);
720 BUG_ON(irq_info[irq].index != ipi);
722 /* Get a new binding from Xen. */
724 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
727 evtchn = bind_ipi.port;
729 /* Record the new mapping. */
730 evtchn_to_irq[evtchn] = irq;
731 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
732 bind_evtchn_to_cpu(evtchn, cpu);
735 unmask_evtchn(evtchn);
740 /* Clear an irq's pending state, in preparation for polling on it */
741 void xen_clear_irq_pending(int irq)
743 int evtchn = evtchn_from_irq(irq);
745 if (VALID_EVTCHN(evtchn))
746 clear_evtchn(evtchn);
749 void xen_set_irq_pending(int irq)
751 int evtchn = evtchn_from_irq(irq);
753 if (VALID_EVTCHN(evtchn))
757 bool xen_test_irq_pending(int irq)
759 int evtchn = evtchn_from_irq(irq);
762 if (VALID_EVTCHN(evtchn))
763 ret = test_evtchn(evtchn);
768 /* Poll waiting for an irq to become pending. In the usual case, the
769 irq will be disabled so it won't deliver an interrupt. */
770 void xen_poll_irq(int irq)
772 evtchn_port_t evtchn = evtchn_from_irq(irq);
774 if (VALID_EVTCHN(evtchn)) {
775 struct sched_poll poll;
779 poll.ports = &evtchn;
781 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
786 void xen_irq_resume(void)
788 unsigned int cpu, irq, evtchn;
790 init_evtchn_cpu_bindings();
792 /* New event-channel space is not 'live' yet. */
793 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
796 /* No IRQ <-> event-channel mappings. */
797 for (irq = 0; irq < nr_irqs; irq++)
798 irq_info[irq].evtchn = 0; /* zap event-channel binding */
800 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
801 evtchn_to_irq[evtchn] = -1;
803 for_each_possible_cpu(cpu) {
804 restore_cpu_virqs(cpu);
805 restore_cpu_ipis(cpu);
809 static struct irq_chip xen_dynamic_chip __read_mostly = {
811 .mask = disable_dynirq,
812 .unmask = enable_dynirq,
814 .set_affinity = set_affinity_irq,
815 .retrigger = retrigger_dynirq,
818 void __init xen_init_IRQ(void)
822 init_evtchn_cpu_bindings();
824 /* No event channels are 'live' right now. */
825 for (i = 0; i < NR_EVENT_CHANNELS; i++)
828 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
829 for (i = 0; i < nr_irqs; i++)
830 irq_bindcount[i] = 0;
832 irq_ctx_init(smp_processor_id());