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 received, 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. PIRQs - Hardware interrupts.
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>
30 #include <linux/slab.h>
31 #include <linux/irqnr.h>
32 #include <linux/pci.h>
35 #include <asm/ptrace.h>
38 #include <asm/io_apic.h>
39 #include <asm/sync_bitops.h>
40 #include <asm/xen/pci.h>
41 #include <asm/xen/hypercall.h>
42 #include <asm/xen/hypervisor.h>
46 #include <xen/xen-ops.h>
47 #include <xen/events.h>
48 #include <xen/interface/xen.h>
49 #include <xen/interface/event_channel.h>
50 #include <xen/interface/hvm/hvm_op.h>
51 #include <xen/interface/hvm/params.h>
54 * This lock protects updates to the following mapping and reference-count
55 * arrays. The lock does not need to be acquired to read the mapping tables.
57 static DEFINE_SPINLOCK(irq_mapping_update_lock);
59 static LIST_HEAD(xen_irq_list_head);
61 /* IRQ <-> VIRQ mapping. */
62 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
64 /* IRQ <-> IPI mapping */
65 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
67 /* Interrupt types. */
77 * Packed IRQ information:
78 * type - enum xen_irq_type
79 * event channel - irq->event channel mapping
80 * cpu - cpu this event channel is bound to
81 * index - type-specific information:
82 * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
83 * guest, or GSI (real passthrough IRQ) of the device.
90 struct list_head list;
91 enum xen_irq_type type; /* type */
93 unsigned short evtchn; /* event channel */
94 unsigned short cpu; /* cpu bound */
102 unsigned char vector;
107 #define PIRQ_NEEDS_EOI (1 << 0)
108 #define PIRQ_SHAREABLE (1 << 1)
110 static int *evtchn_to_irq;
112 static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
115 /* Xen will never allocate port zero for any purpose. */
116 #define VALID_EVTCHN(chn) ((chn) != 0)
118 static struct irq_chip xen_dynamic_chip;
119 static struct irq_chip xen_percpu_chip;
120 static struct irq_chip xen_pirq_chip;
121 static void enable_dynirq(struct irq_data *data);
122 static void disable_dynirq(struct irq_data *data);
124 /* Get info for IRQ */
125 static struct irq_info *info_for_irq(unsigned irq)
127 return irq_get_handler_data(irq);
130 /* Constructors for packed IRQ information. */
131 static void xen_irq_info_common_init(struct irq_info *info,
133 enum xen_irq_type type,
134 unsigned short evtchn,
138 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
142 info->evtchn = evtchn;
145 evtchn_to_irq[evtchn] = irq;
148 static void xen_irq_info_evtchn_init(unsigned irq,
149 unsigned short evtchn)
151 struct irq_info *info = info_for_irq(irq);
153 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
156 static void xen_irq_info_ipi_init(unsigned cpu,
158 unsigned short evtchn,
161 struct irq_info *info = info_for_irq(irq);
163 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
167 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
170 static void xen_irq_info_virq_init(unsigned cpu,
172 unsigned short evtchn,
175 struct irq_info *info = info_for_irq(irq);
177 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
181 per_cpu(virq_to_irq, cpu)[virq] = irq;
184 static void xen_irq_info_pirq_init(unsigned irq,
185 unsigned short evtchn,
188 unsigned short vector,
191 struct irq_info *info = info_for_irq(irq);
193 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
195 info->u.pirq.pirq = pirq;
196 info->u.pirq.gsi = gsi;
197 info->u.pirq.vector = vector;
198 info->u.pirq.flags = flags;
202 * Accessors for packed IRQ information.
204 static unsigned int evtchn_from_irq(unsigned irq)
206 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
209 return info_for_irq(irq)->evtchn;
212 unsigned irq_from_evtchn(unsigned int evtchn)
214 return evtchn_to_irq[evtchn];
216 EXPORT_SYMBOL_GPL(irq_from_evtchn);
218 static enum ipi_vector ipi_from_irq(unsigned irq)
220 struct irq_info *info = info_for_irq(irq);
222 BUG_ON(info == NULL);
223 BUG_ON(info->type != IRQT_IPI);
228 static unsigned virq_from_irq(unsigned irq)
230 struct irq_info *info = info_for_irq(irq);
232 BUG_ON(info == NULL);
233 BUG_ON(info->type != IRQT_VIRQ);
238 static unsigned pirq_from_irq(unsigned irq)
240 struct irq_info *info = info_for_irq(irq);
242 BUG_ON(info == NULL);
243 BUG_ON(info->type != IRQT_PIRQ);
245 return info->u.pirq.pirq;
248 static enum xen_irq_type type_from_irq(unsigned irq)
250 return info_for_irq(irq)->type;
253 static unsigned cpu_from_irq(unsigned irq)
255 return info_for_irq(irq)->cpu;
258 static unsigned int cpu_from_evtchn(unsigned int evtchn)
260 int irq = evtchn_to_irq[evtchn];
264 ret = cpu_from_irq(irq);
269 static bool pirq_needs_eoi(unsigned irq)
271 struct irq_info *info = info_for_irq(irq);
273 BUG_ON(info->type != IRQT_PIRQ);
275 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
278 static inline unsigned long active_evtchns(unsigned int cpu,
279 struct shared_info *sh,
282 return (sh->evtchn_pending[idx] &
283 per_cpu(cpu_evtchn_mask, cpu)[idx] &
284 ~sh->evtchn_mask[idx]);
287 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
289 int irq = evtchn_to_irq[chn];
293 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
296 clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
297 set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
299 info_for_irq(irq)->cpu = cpu;
302 static void init_evtchn_cpu_bindings(void)
306 struct irq_info *info;
308 /* By default all event channels notify CPU#0. */
309 list_for_each_entry(info, &xen_irq_list_head, list) {
310 struct irq_desc *desc = irq_to_desc(info->irq);
311 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
315 for_each_possible_cpu(i)
316 memset(per_cpu(cpu_evtchn_mask, i),
317 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
320 static inline void clear_evtchn(int port)
322 struct shared_info *s = HYPERVISOR_shared_info;
323 sync_clear_bit(port, &s->evtchn_pending[0]);
326 static inline void set_evtchn(int port)
328 struct shared_info *s = HYPERVISOR_shared_info;
329 sync_set_bit(port, &s->evtchn_pending[0]);
332 static inline int test_evtchn(int port)
334 struct shared_info *s = HYPERVISOR_shared_info;
335 return sync_test_bit(port, &s->evtchn_pending[0]);
340 * notify_remote_via_irq - send event to remote end of event channel via irq
341 * @irq: irq of event channel to send event to
343 * Unlike notify_remote_via_evtchn(), this is safe to use across
344 * save/restore. Notifications on a broken connection are silently
347 void notify_remote_via_irq(int irq)
349 int evtchn = evtchn_from_irq(irq);
351 if (VALID_EVTCHN(evtchn))
352 notify_remote_via_evtchn(evtchn);
354 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
356 static void mask_evtchn(int port)
358 struct shared_info *s = HYPERVISOR_shared_info;
359 sync_set_bit(port, &s->evtchn_mask[0]);
362 static void unmask_evtchn(int port)
364 struct shared_info *s = HYPERVISOR_shared_info;
365 unsigned int cpu = get_cpu();
367 BUG_ON(!irqs_disabled());
369 /* Slow path (hypercall) if this is a non-local port. */
370 if (unlikely(cpu != cpu_from_evtchn(port))) {
371 struct evtchn_unmask unmask = { .port = port };
372 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
374 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
376 sync_clear_bit(port, &s->evtchn_mask[0]);
379 * The following is basically the equivalent of
380 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
381 * the interrupt edge' if the channel is masked.
383 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
384 !sync_test_and_set_bit(port / BITS_PER_LONG,
385 &vcpu_info->evtchn_pending_sel))
386 vcpu_info->evtchn_upcall_pending = 1;
392 static void xen_irq_init(unsigned irq)
394 struct irq_info *info;
395 struct irq_desc *desc = irq_to_desc(irq);
398 /* By default all event channels notify CPU#0. */
399 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
402 info = kzalloc(sizeof(*info), GFP_KERNEL);
404 panic("Unable to allocate metadata for IRQ%d\n", irq);
406 info->type = IRQT_UNBOUND;
408 irq_set_handler_data(irq, info);
410 list_add_tail(&info->list, &xen_irq_list_head);
413 static int __must_check xen_allocate_irq_dynamic(void)
418 #ifdef CONFIG_X86_IO_APIC
420 * For an HVM guest or domain 0 which see "real" (emulated or
421 * actual respectively) GSIs we allocate dynamic IRQs
422 * e.g. those corresponding to event channels or MSIs
423 * etc. from the range above those "real" GSIs to avoid
426 if (xen_initial_domain() || xen_hvm_domain())
427 first = get_nr_irqs_gsi();
430 irq = irq_alloc_desc_from(first, -1);
437 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
442 * A PV guest has no concept of a GSI (since it has no ACPI
443 * nor access to/knowledge of the physical APICs). Therefore
444 * all IRQs are dynamically allocated from the entire IRQ
447 if (xen_pv_domain() && !xen_initial_domain())
448 return xen_allocate_irq_dynamic();
450 /* Legacy IRQ descriptors are already allocated by the arch. */
451 if (gsi < NR_IRQS_LEGACY)
454 irq = irq_alloc_desc_at(gsi, -1);
461 static void xen_free_irq(unsigned irq)
463 struct irq_info *info = irq_get_handler_data(irq);
465 list_del(&info->list);
467 irq_set_handler_data(irq, NULL);
471 /* Legacy IRQ descriptors are managed by the arch. */
472 if (irq < NR_IRQS_LEGACY)
478 static void pirq_query_unmask(int irq)
480 struct physdev_irq_status_query irq_status;
481 struct irq_info *info = info_for_irq(irq);
483 BUG_ON(info->type != IRQT_PIRQ);
485 irq_status.irq = pirq_from_irq(irq);
486 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
487 irq_status.flags = 0;
489 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
490 if (irq_status.flags & XENIRQSTAT_needs_eoi)
491 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
494 static bool probing_irq(int irq)
496 struct irq_desc *desc = irq_to_desc(irq);
498 return desc && desc->action == NULL;
501 static void eoi_pirq(struct irq_data *data)
503 int evtchn = evtchn_from_irq(data->irq);
504 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
509 if (VALID_EVTCHN(evtchn))
510 clear_evtchn(evtchn);
512 if (pirq_needs_eoi(data->irq)) {
513 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
518 static void mask_ack_pirq(struct irq_data *data)
520 disable_dynirq(data);
524 static unsigned int __startup_pirq(unsigned int irq)
526 struct evtchn_bind_pirq bind_pirq;
527 struct irq_info *info = info_for_irq(irq);
528 int evtchn = evtchn_from_irq(irq);
531 BUG_ON(info->type != IRQT_PIRQ);
533 if (VALID_EVTCHN(evtchn))
536 bind_pirq.pirq = pirq_from_irq(irq);
537 /* NB. We are happy to share unless we are probing. */
538 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
539 BIND_PIRQ__WILL_SHARE : 0;
540 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
542 if (!probing_irq(irq))
543 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
547 evtchn = bind_pirq.port;
549 pirq_query_unmask(irq);
551 evtchn_to_irq[evtchn] = irq;
552 bind_evtchn_to_cpu(evtchn, 0);
553 info->evtchn = evtchn;
556 unmask_evtchn(evtchn);
557 eoi_pirq(irq_get_irq_data(irq));
562 static unsigned int startup_pirq(struct irq_data *data)
564 return __startup_pirq(data->irq);
567 static void shutdown_pirq(struct irq_data *data)
569 struct evtchn_close close;
570 unsigned int irq = data->irq;
571 struct irq_info *info = info_for_irq(irq);
572 int evtchn = evtchn_from_irq(irq);
574 BUG_ON(info->type != IRQT_PIRQ);
576 if (!VALID_EVTCHN(evtchn))
582 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
585 bind_evtchn_to_cpu(evtchn, 0);
586 evtchn_to_irq[evtchn] = -1;
590 static void enable_pirq(struct irq_data *data)
595 static void disable_pirq(struct irq_data *data)
597 disable_dynirq(data);
600 static int find_irq_by_gsi(unsigned gsi)
602 struct irq_info *info;
604 list_for_each_entry(info, &xen_irq_list_head, list) {
605 if (info->type != IRQT_PIRQ)
608 if (info->u.pirq.gsi == gsi)
615 int xen_allocate_pirq_gsi(unsigned gsi)
621 * Do not make any assumptions regarding the relationship between the
622 * IRQ number returned here and the Xen pirq argument.
624 * Note: We don't assign an event channel until the irq actually started
625 * up. Return an existing irq if we've already got one for the gsi.
627 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
628 unsigned pirq, int shareable, char *name)
631 struct physdev_irq irq_op;
633 spin_lock(&irq_mapping_update_lock);
635 irq = find_irq_by_gsi(gsi);
637 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
639 goto out; /* XXX need refcount? */
642 irq = xen_allocate_irq_gsi(gsi);
649 /* Only the privileged domain can do this. For non-priv, the pcifront
650 * driver provides a PCI bus that does the call to do exactly
651 * this in the priv domain. */
652 if (xen_initial_domain() &&
653 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
659 xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector,
660 shareable ? PIRQ_SHAREABLE : 0);
662 pirq_query_unmask(irq);
663 /* We try to use the handler with the appropriate semantic for the
664 * type of interrupt: if the interrupt doesn't need an eoi
665 * (pirq_needs_eoi returns false), we treat it like an edge
666 * triggered interrupt so we use handle_edge_irq.
667 * As a matter of fact this only happens when the corresponding
668 * physical interrupt is edge triggered or an msi.
670 * On the other hand if the interrupt needs an eoi (pirq_needs_eoi
671 * returns true) we treat it like a level triggered interrupt so we
672 * use handle_fasteoi_irq like the native code does for this kind of
674 * Depending on the Xen version, pirq_needs_eoi might return true
675 * not only for level triggered interrupts but for edge triggered
676 * interrupts too. In any case Xen always honors the eoi mechanism,
677 * not injecting any more pirqs of the same kind if the first one
678 * hasn't received an eoi yet. Therefore using the fasteoi handler
679 * is the right choice either way.
681 if (pirq_needs_eoi(irq))
682 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
683 handle_fasteoi_irq, name);
685 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
686 handle_edge_irq, name);
689 spin_unlock(&irq_mapping_update_lock);
694 #ifdef CONFIG_PCI_MSI
695 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
698 struct physdev_get_free_pirq op_get_free_pirq;
700 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
701 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
703 WARN_ONCE(rc == -ENOSYS,
704 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
706 return rc ? -1 : op_get_free_pirq.pirq;
709 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
710 int pirq, int vector, const char *name)
714 spin_lock(&irq_mapping_update_lock);
716 irq = xen_allocate_irq_dynamic();
720 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
723 xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, 0);
724 ret = irq_set_msi_desc(irq, msidesc);
728 spin_unlock(&irq_mapping_update_lock);
731 spin_unlock(&irq_mapping_update_lock);
737 int xen_destroy_irq(int irq)
739 struct irq_desc *desc;
740 struct physdev_unmap_pirq unmap_irq;
741 struct irq_info *info = info_for_irq(irq);
744 spin_lock(&irq_mapping_update_lock);
746 desc = irq_to_desc(irq);
750 if (xen_initial_domain()) {
751 unmap_irq.pirq = info->u.pirq.pirq;
752 unmap_irq.domid = DOMID_SELF;
753 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
755 printk(KERN_WARNING "unmap irq failed %d\n", rc);
763 spin_unlock(&irq_mapping_update_lock);
767 int xen_irq_from_pirq(unsigned pirq)
771 struct irq_info *info;
773 spin_lock(&irq_mapping_update_lock);
775 list_for_each_entry(info, &xen_irq_list_head, list) {
776 if (info == NULL || info->type != IRQT_PIRQ)
779 if (info->u.pirq.pirq == pirq)
784 spin_unlock(&irq_mapping_update_lock);
789 int bind_evtchn_to_irq(unsigned int evtchn)
793 spin_lock(&irq_mapping_update_lock);
795 irq = evtchn_to_irq[evtchn];
798 irq = xen_allocate_irq_dynamic();
802 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
803 handle_edge_irq, "event");
805 xen_irq_info_evtchn_init(irq, evtchn);
809 spin_unlock(&irq_mapping_update_lock);
813 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
815 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
817 struct evtchn_bind_ipi bind_ipi;
820 spin_lock(&irq_mapping_update_lock);
822 irq = per_cpu(ipi_to_irq, cpu)[ipi];
825 irq = xen_allocate_irq_dynamic();
829 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
830 handle_percpu_irq, "ipi");
833 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
836 evtchn = bind_ipi.port;
838 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
840 bind_evtchn_to_cpu(evtchn, cpu);
844 spin_unlock(&irq_mapping_update_lock);
848 static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
849 unsigned int remote_port)
851 struct evtchn_bind_interdomain bind_interdomain;
854 bind_interdomain.remote_dom = remote_domain;
855 bind_interdomain.remote_port = remote_port;
857 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
860 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
864 int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
866 struct evtchn_bind_virq bind_virq;
869 spin_lock(&irq_mapping_update_lock);
871 irq = per_cpu(virq_to_irq, cpu)[virq];
874 irq = xen_allocate_irq_dynamic();
878 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
879 handle_percpu_irq, "virq");
881 bind_virq.virq = virq;
882 bind_virq.vcpu = cpu;
883 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
886 evtchn = bind_virq.port;
888 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
890 bind_evtchn_to_cpu(evtchn, cpu);
894 spin_unlock(&irq_mapping_update_lock);
899 static void unbind_from_irq(unsigned int irq)
901 struct evtchn_close close;
902 int evtchn = evtchn_from_irq(irq);
904 spin_lock(&irq_mapping_update_lock);
906 if (VALID_EVTCHN(evtchn)) {
908 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
911 switch (type_from_irq(irq)) {
913 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
914 [virq_from_irq(irq)] = -1;
917 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
918 [ipi_from_irq(irq)] = -1;
924 /* Closed ports are implicitly re-bound to VCPU0. */
925 bind_evtchn_to_cpu(evtchn, 0);
927 evtchn_to_irq[evtchn] = -1;
930 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
934 spin_unlock(&irq_mapping_update_lock);
937 int bind_evtchn_to_irqhandler(unsigned int evtchn,
938 irq_handler_t handler,
939 unsigned long irqflags,
940 const char *devname, void *dev_id)
944 irq = bind_evtchn_to_irq(evtchn);
947 retval = request_irq(irq, handler, irqflags, devname, dev_id);
949 unbind_from_irq(irq);
955 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
957 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
958 unsigned int remote_port,
959 irq_handler_t handler,
960 unsigned long irqflags,
966 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
970 retval = request_irq(irq, handler, irqflags, devname, dev_id);
972 unbind_from_irq(irq);
978 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
980 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
981 irq_handler_t handler,
982 unsigned long irqflags, const char *devname, void *dev_id)
986 irq = bind_virq_to_irq(virq, cpu);
989 retval = request_irq(irq, handler, irqflags, devname, dev_id);
991 unbind_from_irq(irq);
997 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
999 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1001 irq_handler_t handler,
1002 unsigned long irqflags,
1003 const char *devname,
1008 irq = bind_ipi_to_irq(ipi, cpu);
1012 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
1013 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1015 unbind_from_irq(irq);
1022 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1024 free_irq(irq, dev_id);
1025 unbind_from_irq(irq);
1027 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1029 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1031 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1033 notify_remote_via_irq(irq);
1036 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1038 struct shared_info *sh = HYPERVISOR_shared_info;
1039 int cpu = smp_processor_id();
1040 unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
1042 unsigned long flags;
1043 static DEFINE_SPINLOCK(debug_lock);
1044 struct vcpu_info *v;
1046 spin_lock_irqsave(&debug_lock, flags);
1048 printk("\nvcpu %d\n ", cpu);
1050 for_each_online_cpu(i) {
1052 v = per_cpu(xen_vcpu, i);
1053 pending = (get_irq_regs() && i == cpu)
1054 ? xen_irqs_disabled(get_irq_regs())
1055 : v->evtchn_upcall_mask;
1056 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
1057 pending, v->evtchn_upcall_pending,
1058 (int)(sizeof(v->evtchn_pending_sel)*2),
1059 v->evtchn_pending_sel);
1061 v = per_cpu(xen_vcpu, cpu);
1063 printk("\npending:\n ");
1064 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1065 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
1066 sh->evtchn_pending[i],
1067 i % 8 == 0 ? "\n " : " ");
1068 printk("\nglobal mask:\n ");
1069 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1071 (int)(sizeof(sh->evtchn_mask[0])*2),
1073 i % 8 == 0 ? "\n " : " ");
1075 printk("\nglobally unmasked:\n ");
1076 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1077 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1078 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1079 i % 8 == 0 ? "\n " : " ");
1081 printk("\nlocal cpu%d mask:\n ", cpu);
1082 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
1083 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
1085 i % 8 == 0 ? "\n " : " ");
1087 printk("\nlocally unmasked:\n ");
1088 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1089 unsigned long pending = sh->evtchn_pending[i]
1090 & ~sh->evtchn_mask[i]
1092 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1093 pending, i % 8 == 0 ? "\n " : " ");
1096 printk("\npending list:\n");
1097 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
1098 if (sync_test_bit(i, sh->evtchn_pending)) {
1099 int word_idx = i / BITS_PER_LONG;
1100 printk(" %d: event %d -> irq %d%s%s%s\n",
1101 cpu_from_evtchn(i), i,
1103 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1105 !sync_test_bit(i, sh->evtchn_mask)
1106 ? "" : " globally-masked",
1107 sync_test_bit(i, cpu_evtchn)
1108 ? "" : " locally-masked");
1112 spin_unlock_irqrestore(&debug_lock, flags);
1117 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1118 static DEFINE_PER_CPU(unsigned int, current_word_idx);
1119 static DEFINE_PER_CPU(unsigned int, current_bit_idx);
1122 * Mask out the i least significant bits of w
1124 #define MASK_LSBS(w, i) (w & ((~0UL) << i))
1127 * Search the CPUs pending events bitmasks. For each one found, map
1128 * the event number to an irq, and feed it into do_IRQ() for
1131 * Xen uses a two-level bitmap to speed searching. The first level is
1132 * a bitset of words which contain pending event bits. The second
1133 * level is a bitset of pending events themselves.
1135 static void __xen_evtchn_do_upcall(void)
1137 int start_word_idx, start_bit_idx;
1138 int word_idx, bit_idx;
1140 int cpu = get_cpu();
1141 struct shared_info *s = HYPERVISOR_shared_info;
1142 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1146 unsigned long pending_words;
1148 vcpu_info->evtchn_upcall_pending = 0;
1150 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1153 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1154 /* Clear master flag /before/ clearing selector flag. */
1157 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
1159 start_word_idx = __this_cpu_read(current_word_idx);
1160 start_bit_idx = __this_cpu_read(current_bit_idx);
1162 word_idx = start_word_idx;
1164 for (i = 0; pending_words != 0; i++) {
1165 unsigned long pending_bits;
1166 unsigned long words;
1168 words = MASK_LSBS(pending_words, word_idx);
1171 * If we masked out all events, wrap to beginning.
1178 word_idx = __ffs(words);
1180 pending_bits = active_evtchns(cpu, s, word_idx);
1181 bit_idx = 0; /* usually scan entire word from start */
1182 if (word_idx == start_word_idx) {
1183 /* We scan the starting word in two parts */
1185 /* 1st time: start in the middle */
1186 bit_idx = start_bit_idx;
1188 /* 2nd time: mask bits done already */
1189 bit_idx &= (1UL << start_bit_idx) - 1;
1195 struct irq_desc *desc;
1197 bits = MASK_LSBS(pending_bits, bit_idx);
1199 /* If we masked out all events, move on. */
1203 bit_idx = __ffs(bits);
1206 port = (word_idx * BITS_PER_LONG) + bit_idx;
1207 irq = evtchn_to_irq[port];
1210 desc = irq_to_desc(irq);
1212 generic_handle_irq_desc(irq, desc);
1215 bit_idx = (bit_idx + 1) % BITS_PER_LONG;
1217 /* Next caller starts at last processed + 1 */
1218 __this_cpu_write(current_word_idx,
1219 bit_idx ? word_idx :
1220 (word_idx+1) % BITS_PER_LONG);
1221 __this_cpu_write(current_bit_idx, bit_idx);
1222 } while (bit_idx != 0);
1224 /* Scan start_l1i twice; all others once. */
1225 if ((word_idx != start_word_idx) || (i != 0))
1226 pending_words &= ~(1UL << word_idx);
1228 word_idx = (word_idx + 1) % BITS_PER_LONG;
1231 BUG_ON(!irqs_disabled());
1233 count = __this_cpu_read(xed_nesting_count);
1234 __this_cpu_write(xed_nesting_count, 0);
1235 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1242 void xen_evtchn_do_upcall(struct pt_regs *regs)
1244 struct pt_regs *old_regs = set_irq_regs(regs);
1249 __xen_evtchn_do_upcall();
1252 set_irq_regs(old_regs);
1255 void xen_hvm_evtchn_do_upcall(void)
1257 __xen_evtchn_do_upcall();
1259 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1261 /* Rebind a new event channel to an existing irq. */
1262 void rebind_evtchn_irq(int evtchn, int irq)
1264 struct irq_info *info = info_for_irq(irq);
1266 /* Make sure the irq is masked, since the new event channel
1267 will also be masked. */
1270 spin_lock(&irq_mapping_update_lock);
1272 /* After resume the irq<->evtchn mappings are all cleared out */
1273 BUG_ON(evtchn_to_irq[evtchn] != -1);
1274 /* Expect irq to have been bound before,
1275 so there should be a proper type */
1276 BUG_ON(info->type == IRQT_UNBOUND);
1278 xen_irq_info_evtchn_init(irq, evtchn);
1280 spin_unlock(&irq_mapping_update_lock);
1282 /* new event channels are always bound to cpu 0 */
1283 irq_set_affinity(irq, cpumask_of(0));
1285 /* Unmask the event channel. */
1289 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1290 static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
1292 struct evtchn_bind_vcpu bind_vcpu;
1293 int evtchn = evtchn_from_irq(irq);
1295 if (!VALID_EVTCHN(evtchn))
1299 * Events delivered via platform PCI interrupts are always
1300 * routed to vcpu 0 and hence cannot be rebound.
1302 if (xen_hvm_domain() && !xen_have_vector_callback)
1305 /* Send future instances of this interrupt to other vcpu. */
1306 bind_vcpu.port = evtchn;
1307 bind_vcpu.vcpu = tcpu;
1310 * If this fails, it usually just indicates that we're dealing with a
1311 * virq or IPI channel, which don't actually need to be rebound. Ignore
1312 * it, but don't do the xenlinux-level rebind in that case.
1314 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1315 bind_evtchn_to_cpu(evtchn, tcpu);
1320 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1323 unsigned tcpu = cpumask_first(dest);
1325 return rebind_irq_to_cpu(data->irq, tcpu);
1328 int resend_irq_on_evtchn(unsigned int irq)
1330 int masked, evtchn = evtchn_from_irq(irq);
1331 struct shared_info *s = HYPERVISOR_shared_info;
1333 if (!VALID_EVTCHN(evtchn))
1336 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1337 sync_set_bit(evtchn, s->evtchn_pending);
1339 unmask_evtchn(evtchn);
1344 static void enable_dynirq(struct irq_data *data)
1346 int evtchn = evtchn_from_irq(data->irq);
1348 if (VALID_EVTCHN(evtchn))
1349 unmask_evtchn(evtchn);
1352 static void disable_dynirq(struct irq_data *data)
1354 int evtchn = evtchn_from_irq(data->irq);
1356 if (VALID_EVTCHN(evtchn))
1357 mask_evtchn(evtchn);
1360 static void ack_dynirq(struct irq_data *data)
1362 int evtchn = evtchn_from_irq(data->irq);
1366 if (VALID_EVTCHN(evtchn))
1367 clear_evtchn(evtchn);
1370 static void mask_ack_dynirq(struct irq_data *data)
1372 disable_dynirq(data);
1376 static int retrigger_dynirq(struct irq_data *data)
1378 int evtchn = evtchn_from_irq(data->irq);
1379 struct shared_info *sh = HYPERVISOR_shared_info;
1382 if (VALID_EVTCHN(evtchn)) {
1385 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1386 sync_set_bit(evtchn, sh->evtchn_pending);
1388 unmask_evtchn(evtchn);
1395 static void restore_pirqs(void)
1397 int pirq, rc, irq, gsi;
1398 struct physdev_map_pirq map_irq;
1399 struct irq_info *info;
1401 list_for_each_entry(info, &xen_irq_list_head, list) {
1402 if (info->type != IRQT_PIRQ)
1405 pirq = info->u.pirq.pirq;
1406 gsi = info->u.pirq.gsi;
1409 /* save/restore of PT devices doesn't work, so at this point the
1410 * only devices present are GSI based emulated devices */
1414 map_irq.domid = DOMID_SELF;
1415 map_irq.type = MAP_PIRQ_TYPE_GSI;
1416 map_irq.index = gsi;
1417 map_irq.pirq = pirq;
1419 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1421 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1422 gsi, irq, pirq, rc);
1427 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1429 __startup_pirq(irq);
1433 static void restore_cpu_virqs(unsigned int cpu)
1435 struct evtchn_bind_virq bind_virq;
1436 int virq, irq, evtchn;
1438 for (virq = 0; virq < NR_VIRQS; virq++) {
1439 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1442 BUG_ON(virq_from_irq(irq) != virq);
1444 /* Get a new binding from Xen. */
1445 bind_virq.virq = virq;
1446 bind_virq.vcpu = cpu;
1447 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1450 evtchn = bind_virq.port;
1452 /* Record the new mapping. */
1453 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
1454 bind_evtchn_to_cpu(evtchn, cpu);
1458 static void restore_cpu_ipis(unsigned int cpu)
1460 struct evtchn_bind_ipi bind_ipi;
1461 int ipi, irq, evtchn;
1463 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1464 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1467 BUG_ON(ipi_from_irq(irq) != ipi);
1469 /* Get a new binding from Xen. */
1470 bind_ipi.vcpu = cpu;
1471 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1474 evtchn = bind_ipi.port;
1476 /* Record the new mapping. */
1477 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
1478 bind_evtchn_to_cpu(evtchn, cpu);
1482 /* Clear an irq's pending state, in preparation for polling on it */
1483 void xen_clear_irq_pending(int irq)
1485 int evtchn = evtchn_from_irq(irq);
1487 if (VALID_EVTCHN(evtchn))
1488 clear_evtchn(evtchn);
1490 EXPORT_SYMBOL(xen_clear_irq_pending);
1491 void xen_set_irq_pending(int irq)
1493 int evtchn = evtchn_from_irq(irq);
1495 if (VALID_EVTCHN(evtchn))
1499 bool xen_test_irq_pending(int irq)
1501 int evtchn = evtchn_from_irq(irq);
1504 if (VALID_EVTCHN(evtchn))
1505 ret = test_evtchn(evtchn);
1510 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1511 * the irq will be disabled so it won't deliver an interrupt. */
1512 void xen_poll_irq_timeout(int irq, u64 timeout)
1514 evtchn_port_t evtchn = evtchn_from_irq(irq);
1516 if (VALID_EVTCHN(evtchn)) {
1517 struct sched_poll poll;
1520 poll.timeout = timeout;
1521 set_xen_guest_handle(poll.ports, &evtchn);
1523 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1527 EXPORT_SYMBOL(xen_poll_irq_timeout);
1528 /* Poll waiting for an irq to become pending. In the usual case, the
1529 * irq will be disabled so it won't deliver an interrupt. */
1530 void xen_poll_irq(int irq)
1532 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1535 void xen_irq_resume(void)
1537 unsigned int cpu, evtchn;
1538 struct irq_info *info;
1540 init_evtchn_cpu_bindings();
1542 /* New event-channel space is not 'live' yet. */
1543 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1544 mask_evtchn(evtchn);
1546 /* No IRQ <-> event-channel mappings. */
1547 list_for_each_entry(info, &xen_irq_list_head, list)
1548 info->evtchn = 0; /* zap event-channel binding */
1550 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1551 evtchn_to_irq[evtchn] = -1;
1553 for_each_possible_cpu(cpu) {
1554 restore_cpu_virqs(cpu);
1555 restore_cpu_ipis(cpu);
1561 static struct irq_chip xen_dynamic_chip __read_mostly = {
1564 .irq_disable = disable_dynirq,
1565 .irq_mask = disable_dynirq,
1566 .irq_unmask = enable_dynirq,
1568 .irq_ack = ack_dynirq,
1569 .irq_mask_ack = mask_ack_dynirq,
1571 .irq_set_affinity = set_affinity_irq,
1572 .irq_retrigger = retrigger_dynirq,
1575 static struct irq_chip xen_pirq_chip __read_mostly = {
1578 .irq_startup = startup_pirq,
1579 .irq_shutdown = shutdown_pirq,
1580 .irq_enable = enable_pirq,
1581 .irq_disable = disable_pirq,
1583 .irq_mask = disable_dynirq,
1584 .irq_unmask = enable_dynirq,
1586 .irq_ack = eoi_pirq,
1587 .irq_eoi = eoi_pirq,
1588 .irq_mask_ack = mask_ack_pirq,
1590 .irq_set_affinity = set_affinity_irq,
1592 .irq_retrigger = retrigger_dynirq,
1595 static struct irq_chip xen_percpu_chip __read_mostly = {
1596 .name = "xen-percpu",
1598 .irq_disable = disable_dynirq,
1599 .irq_mask = disable_dynirq,
1600 .irq_unmask = enable_dynirq,
1602 .irq_ack = ack_dynirq,
1605 int xen_set_callback_via(uint64_t via)
1607 struct xen_hvm_param a;
1608 a.domid = DOMID_SELF;
1609 a.index = HVM_PARAM_CALLBACK_IRQ;
1611 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1613 EXPORT_SYMBOL_GPL(xen_set_callback_via);
1615 #ifdef CONFIG_XEN_PVHVM
1616 /* Vector callbacks are better than PCI interrupts to receive event
1617 * channel notifications because we can receive vector callbacks on any
1618 * vcpu and we don't need PCI support or APIC interactions. */
1619 void xen_callback_vector(void)
1622 uint64_t callback_via;
1623 if (xen_have_vector_callback) {
1624 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1625 rc = xen_set_callback_via(callback_via);
1627 printk(KERN_ERR "Request for Xen HVM callback vector"
1629 xen_have_vector_callback = 0;
1632 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1634 /* in the restore case the vector has already been allocated */
1635 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1636 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1640 void xen_callback_vector(void) {}
1643 void __init xen_init_IRQ(void)
1647 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1649 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1650 evtchn_to_irq[i] = -1;
1652 init_evtchn_cpu_bindings();
1654 /* No event channels are 'live' right now. */
1655 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1658 if (xen_hvm_domain()) {
1659 xen_callback_vector();
1661 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1662 * __acpi_register_gsi can point at the right function */
1665 irq_ctx_init(smp_processor_id());
1666 if (xen_initial_domain())