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;
108 #define PIRQ_NEEDS_EOI (1 << 0)
109 #define PIRQ_SHAREABLE (1 << 1)
111 static int *evtchn_to_irq;
113 static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
116 /* Xen will never allocate port zero for any purpose. */
117 #define VALID_EVTCHN(chn) ((chn) != 0)
119 static struct irq_chip xen_dynamic_chip;
120 static struct irq_chip xen_percpu_chip;
121 static struct irq_chip xen_pirq_chip;
123 /* Get info for IRQ */
124 static struct irq_info *info_for_irq(unsigned irq)
126 return irq_get_handler_data(irq);
129 /* Constructors for packed IRQ information. */
130 static void xen_irq_info_common_init(struct irq_info *info,
132 enum xen_irq_type type,
133 unsigned short evtchn,
137 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
141 info->evtchn = evtchn;
144 evtchn_to_irq[evtchn] = irq;
147 static void xen_irq_info_evtchn_init(unsigned irq,
148 unsigned short evtchn)
150 struct irq_info *info = info_for_irq(irq);
152 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
155 static void xen_irq_info_ipi_init(unsigned cpu,
157 unsigned short evtchn,
160 struct irq_info *info = info_for_irq(irq);
162 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
166 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
169 static void xen_irq_info_virq_init(unsigned cpu,
171 unsigned short evtchn,
174 struct irq_info *info = info_for_irq(irq);
176 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
180 per_cpu(virq_to_irq, cpu)[virq] = irq;
183 static void xen_irq_info_pirq_init(unsigned irq,
184 unsigned short evtchn,
187 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.domid = domid;
199 info->u.pirq.flags = flags;
203 * Accessors for packed IRQ information.
205 static unsigned int evtchn_from_irq(unsigned irq)
207 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
210 return info_for_irq(irq)->evtchn;
213 unsigned irq_from_evtchn(unsigned int evtchn)
215 return evtchn_to_irq[evtchn];
217 EXPORT_SYMBOL_GPL(irq_from_evtchn);
219 static enum ipi_vector ipi_from_irq(unsigned irq)
221 struct irq_info *info = info_for_irq(irq);
223 BUG_ON(info == NULL);
224 BUG_ON(info->type != IRQT_IPI);
229 static unsigned virq_from_irq(unsigned irq)
231 struct irq_info *info = info_for_irq(irq);
233 BUG_ON(info == NULL);
234 BUG_ON(info->type != IRQT_VIRQ);
239 static unsigned pirq_from_irq(unsigned irq)
241 struct irq_info *info = info_for_irq(irq);
243 BUG_ON(info == NULL);
244 BUG_ON(info->type != IRQT_PIRQ);
246 return info->u.pirq.pirq;
249 static enum xen_irq_type type_from_irq(unsigned irq)
251 return info_for_irq(irq)->type;
254 static unsigned cpu_from_irq(unsigned irq)
256 return info_for_irq(irq)->cpu;
259 static unsigned int cpu_from_evtchn(unsigned int evtchn)
261 int irq = evtchn_to_irq[evtchn];
265 ret = cpu_from_irq(irq);
270 static bool pirq_needs_eoi(unsigned irq)
272 struct irq_info *info = info_for_irq(irq);
274 BUG_ON(info->type != IRQT_PIRQ);
276 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
279 static inline unsigned long active_evtchns(unsigned int cpu,
280 struct shared_info *sh,
283 return (sh->evtchn_pending[idx] &
284 per_cpu(cpu_evtchn_mask, cpu)[idx] &
285 ~sh->evtchn_mask[idx]);
288 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
290 int irq = evtchn_to_irq[chn];
294 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
297 clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
298 set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
300 info_for_irq(irq)->cpu = cpu;
303 static void init_evtchn_cpu_bindings(void)
307 struct irq_info *info;
309 /* By default all event channels notify CPU#0. */
310 list_for_each_entry(info, &xen_irq_list_head, list) {
311 struct irq_desc *desc = irq_to_desc(info->irq);
312 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
316 for_each_possible_cpu(i)
317 memset(per_cpu(cpu_evtchn_mask, i),
318 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
321 static inline void clear_evtchn(int port)
323 struct shared_info *s = HYPERVISOR_shared_info;
324 sync_clear_bit(port, &s->evtchn_pending[0]);
327 static inline void set_evtchn(int port)
329 struct shared_info *s = HYPERVISOR_shared_info;
330 sync_set_bit(port, &s->evtchn_pending[0]);
333 static inline int test_evtchn(int port)
335 struct shared_info *s = HYPERVISOR_shared_info;
336 return sync_test_bit(port, &s->evtchn_pending[0]);
341 * notify_remote_via_irq - send event to remote end of event channel via irq
342 * @irq: irq of event channel to send event to
344 * Unlike notify_remote_via_evtchn(), this is safe to use across
345 * save/restore. Notifications on a broken connection are silently
348 void notify_remote_via_irq(int irq)
350 int evtchn = evtchn_from_irq(irq);
352 if (VALID_EVTCHN(evtchn))
353 notify_remote_via_evtchn(evtchn);
355 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
357 static void mask_evtchn(int port)
359 struct shared_info *s = HYPERVISOR_shared_info;
360 sync_set_bit(port, &s->evtchn_mask[0]);
363 static void unmask_evtchn(int port)
365 struct shared_info *s = HYPERVISOR_shared_info;
366 unsigned int cpu = get_cpu();
368 BUG_ON(!irqs_disabled());
370 /* Slow path (hypercall) if this is a non-local port. */
371 if (unlikely(cpu != cpu_from_evtchn(port))) {
372 struct evtchn_unmask unmask = { .port = port };
373 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
375 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
377 sync_clear_bit(port, &s->evtchn_mask[0]);
380 * The following is basically the equivalent of
381 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
382 * the interrupt edge' if the channel is masked.
384 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
385 !sync_test_and_set_bit(port / BITS_PER_LONG,
386 &vcpu_info->evtchn_pending_sel))
387 vcpu_info->evtchn_upcall_pending = 1;
393 static void xen_irq_init(unsigned irq)
395 struct irq_info *info;
396 struct irq_desc *desc = irq_to_desc(irq);
399 /* By default all event channels notify CPU#0. */
400 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
403 info = kzalloc(sizeof(*info), GFP_KERNEL);
405 panic("Unable to allocate metadata for IRQ%d\n", irq);
407 info->type = IRQT_UNBOUND;
409 irq_set_handler_data(irq, info);
411 list_add_tail(&info->list, &xen_irq_list_head);
414 static int __must_check xen_allocate_irq_dynamic(void)
419 #ifdef CONFIG_X86_IO_APIC
421 * For an HVM guest or domain 0 which see "real" (emulated or
422 * actual respectively) GSIs we allocate dynamic IRQs
423 * e.g. those corresponding to event channels or MSIs
424 * etc. from the range above those "real" GSIs to avoid
427 if (xen_initial_domain() || xen_hvm_domain())
428 first = get_nr_irqs_gsi();
431 irq = irq_alloc_desc_from(first, -1);
438 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
443 * A PV guest has no concept of a GSI (since it has no ACPI
444 * nor access to/knowledge of the physical APICs). Therefore
445 * all IRQs are dynamically allocated from the entire IRQ
448 if (xen_pv_domain() && !xen_initial_domain())
449 return xen_allocate_irq_dynamic();
451 /* Legacy IRQ descriptors are already allocated by the arch. */
452 if (gsi < NR_IRQS_LEGACY)
455 irq = irq_alloc_desc_at(gsi, -1);
462 static void xen_free_irq(unsigned irq)
464 struct irq_info *info = irq_get_handler_data(irq);
466 list_del(&info->list);
468 irq_set_handler_data(irq, NULL);
472 /* Legacy IRQ descriptors are managed by the arch. */
473 if (irq < NR_IRQS_LEGACY)
479 static void pirq_unmask_notify(int irq)
481 struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
483 if (unlikely(pirq_needs_eoi(irq))) {
484 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
489 static void pirq_query_unmask(int irq)
491 struct physdev_irq_status_query irq_status;
492 struct irq_info *info = info_for_irq(irq);
494 BUG_ON(info->type != IRQT_PIRQ);
496 irq_status.irq = pirq_from_irq(irq);
497 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
498 irq_status.flags = 0;
500 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
501 if (irq_status.flags & XENIRQSTAT_needs_eoi)
502 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
505 static bool probing_irq(int irq)
507 struct irq_desc *desc = irq_to_desc(irq);
509 return desc && desc->action == NULL;
512 static unsigned int __startup_pirq(unsigned int irq)
514 struct evtchn_bind_pirq bind_pirq;
515 struct irq_info *info = info_for_irq(irq);
516 int evtchn = evtchn_from_irq(irq);
519 BUG_ON(info->type != IRQT_PIRQ);
521 if (VALID_EVTCHN(evtchn))
524 bind_pirq.pirq = pirq_from_irq(irq);
525 /* NB. We are happy to share unless we are probing. */
526 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
527 BIND_PIRQ__WILL_SHARE : 0;
528 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
530 if (!probing_irq(irq))
531 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
535 evtchn = bind_pirq.port;
537 pirq_query_unmask(irq);
539 evtchn_to_irq[evtchn] = irq;
540 bind_evtchn_to_cpu(evtchn, 0);
541 info->evtchn = evtchn;
544 unmask_evtchn(evtchn);
545 pirq_unmask_notify(irq);
550 static unsigned int startup_pirq(struct irq_data *data)
552 return __startup_pirq(data->irq);
555 static void shutdown_pirq(struct irq_data *data)
557 struct evtchn_close close;
558 unsigned int irq = data->irq;
559 struct irq_info *info = info_for_irq(irq);
560 int evtchn = evtchn_from_irq(irq);
562 BUG_ON(info->type != IRQT_PIRQ);
564 if (!VALID_EVTCHN(evtchn))
570 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
573 bind_evtchn_to_cpu(evtchn, 0);
574 evtchn_to_irq[evtchn] = -1;
578 static void enable_pirq(struct irq_data *data)
583 static void disable_pirq(struct irq_data *data)
587 static void ack_pirq(struct irq_data *data)
589 int evtchn = evtchn_from_irq(data->irq);
593 if (VALID_EVTCHN(evtchn)) {
595 clear_evtchn(evtchn);
599 static int find_irq_by_gsi(unsigned gsi)
601 struct irq_info *info;
603 list_for_each_entry(info, &xen_irq_list_head, list) {
604 if (info->type != IRQT_PIRQ)
607 if (info->u.pirq.gsi == gsi)
614 int xen_allocate_pirq_gsi(unsigned gsi)
620 * Do not make any assumptions regarding the relationship between the
621 * IRQ number returned here and the Xen pirq argument.
623 * Note: We don't assign an event channel until the irq actually started
624 * up. Return an existing irq if we've already got one for the gsi.
626 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
627 unsigned pirq, int shareable, char *name)
630 struct physdev_irq irq_op;
632 spin_lock(&irq_mapping_update_lock);
634 irq = find_irq_by_gsi(gsi);
636 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
638 goto out; /* XXX need refcount? */
641 irq = xen_allocate_irq_gsi(gsi);
645 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_level_irq,
651 /* Only the privileged domain can do this. For non-priv, the pcifront
652 * driver provides a PCI bus that does the call to do exactly
653 * this in the priv domain. */
654 if (xen_initial_domain() &&
655 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
661 xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector, DOMID_SELF,
662 shareable ? PIRQ_SHAREABLE : 0);
665 spin_unlock(&irq_mapping_update_lock);
670 #ifdef CONFIG_PCI_MSI
671 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
674 struct physdev_get_free_pirq op_get_free_pirq;
676 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
677 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
679 WARN_ONCE(rc == -ENOSYS,
680 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
682 return rc ? -1 : op_get_free_pirq.pirq;
685 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
686 int pirq, int vector, const char *name,
691 spin_lock(&irq_mapping_update_lock);
693 irq = xen_allocate_irq_dynamic();
697 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_level_irq,
700 xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, domid, 0);
701 ret = irq_set_msi_desc(irq, msidesc);
705 spin_unlock(&irq_mapping_update_lock);
708 spin_unlock(&irq_mapping_update_lock);
714 int xen_destroy_irq(int irq)
716 struct irq_desc *desc;
717 struct physdev_unmap_pirq unmap_irq;
718 struct irq_info *info = info_for_irq(irq);
721 spin_lock(&irq_mapping_update_lock);
723 desc = irq_to_desc(irq);
727 if (xen_initial_domain()) {
728 unmap_irq.pirq = info->u.pirq.pirq;
729 unmap_irq.domid = info->u.pirq.domid;
730 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
731 /* If another domain quits without making the pci_disable_msix
732 * call, the Xen hypervisor takes care of freeing the PIRQs
733 * (free_domain_pirqs).
735 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
736 printk(KERN_INFO "domain %d does not have %d anymore\n",
737 info->u.pirq.domid, info->u.pirq.pirq);
739 printk(KERN_WARNING "unmap irq failed %d\n", rc);
747 spin_unlock(&irq_mapping_update_lock);
751 int xen_irq_from_pirq(unsigned pirq)
755 struct irq_info *info;
757 spin_lock(&irq_mapping_update_lock);
759 list_for_each_entry(info, &xen_irq_list_head, list) {
760 if (info == NULL || info->type != IRQT_PIRQ)
763 if (info->u.pirq.pirq == pirq)
768 spin_unlock(&irq_mapping_update_lock);
774 int xen_pirq_from_irq(unsigned irq)
776 return pirq_from_irq(irq);
778 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
779 int bind_evtchn_to_irq(unsigned int evtchn)
783 spin_lock(&irq_mapping_update_lock);
785 irq = evtchn_to_irq[evtchn];
788 irq = xen_allocate_irq_dynamic();
792 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
793 handle_fasteoi_irq, "event");
795 xen_irq_info_evtchn_init(irq, evtchn);
799 spin_unlock(&irq_mapping_update_lock);
803 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
805 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
807 struct evtchn_bind_ipi bind_ipi;
810 spin_lock(&irq_mapping_update_lock);
812 irq = per_cpu(ipi_to_irq, cpu)[ipi];
815 irq = xen_allocate_irq_dynamic();
819 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
820 handle_percpu_irq, "ipi");
823 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
826 evtchn = bind_ipi.port;
828 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
830 bind_evtchn_to_cpu(evtchn, cpu);
834 spin_unlock(&irq_mapping_update_lock);
838 static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
839 unsigned int remote_port)
841 struct evtchn_bind_interdomain bind_interdomain;
844 bind_interdomain.remote_dom = remote_domain;
845 bind_interdomain.remote_port = remote_port;
847 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
850 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
854 int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
856 struct evtchn_bind_virq bind_virq;
859 spin_lock(&irq_mapping_update_lock);
861 irq = per_cpu(virq_to_irq, cpu)[virq];
864 irq = xen_allocate_irq_dynamic();
868 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
869 handle_percpu_irq, "virq");
871 bind_virq.virq = virq;
872 bind_virq.vcpu = cpu;
873 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
876 evtchn = bind_virq.port;
878 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
880 bind_evtchn_to_cpu(evtchn, cpu);
884 spin_unlock(&irq_mapping_update_lock);
889 static void unbind_from_irq(unsigned int irq)
891 struct evtchn_close close;
892 int evtchn = evtchn_from_irq(irq);
894 spin_lock(&irq_mapping_update_lock);
896 if (VALID_EVTCHN(evtchn)) {
898 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
901 switch (type_from_irq(irq)) {
903 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
904 [virq_from_irq(irq)] = -1;
907 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
908 [ipi_from_irq(irq)] = -1;
914 /* Closed ports are implicitly re-bound to VCPU0. */
915 bind_evtchn_to_cpu(evtchn, 0);
917 evtchn_to_irq[evtchn] = -1;
920 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
924 spin_unlock(&irq_mapping_update_lock);
927 int bind_evtchn_to_irqhandler(unsigned int evtchn,
928 irq_handler_t handler,
929 unsigned long irqflags,
930 const char *devname, void *dev_id)
934 irq = bind_evtchn_to_irq(evtchn);
937 retval = request_irq(irq, handler, irqflags, devname, dev_id);
939 unbind_from_irq(irq);
945 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
947 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
948 unsigned int remote_port,
949 irq_handler_t handler,
950 unsigned long irqflags,
956 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
960 retval = request_irq(irq, handler, irqflags, devname, dev_id);
962 unbind_from_irq(irq);
968 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
970 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
971 irq_handler_t handler,
972 unsigned long irqflags, const char *devname, void *dev_id)
976 irq = bind_virq_to_irq(virq, cpu);
979 retval = request_irq(irq, handler, irqflags, devname, dev_id);
981 unbind_from_irq(irq);
987 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
989 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
991 irq_handler_t handler,
992 unsigned long irqflags,
998 irq = bind_ipi_to_irq(ipi, cpu);
1002 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
1003 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1005 unbind_from_irq(irq);
1012 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1014 free_irq(irq, dev_id);
1015 unbind_from_irq(irq);
1017 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1019 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1021 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1023 notify_remote_via_irq(irq);
1026 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1028 struct shared_info *sh = HYPERVISOR_shared_info;
1029 int cpu = smp_processor_id();
1030 unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
1032 unsigned long flags;
1033 static DEFINE_SPINLOCK(debug_lock);
1034 struct vcpu_info *v;
1036 spin_lock_irqsave(&debug_lock, flags);
1038 printk("\nvcpu %d\n ", cpu);
1040 for_each_online_cpu(i) {
1042 v = per_cpu(xen_vcpu, i);
1043 pending = (get_irq_regs() && i == cpu)
1044 ? xen_irqs_disabled(get_irq_regs())
1045 : v->evtchn_upcall_mask;
1046 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
1047 pending, v->evtchn_upcall_pending,
1048 (int)(sizeof(v->evtchn_pending_sel)*2),
1049 v->evtchn_pending_sel);
1051 v = per_cpu(xen_vcpu, cpu);
1053 printk("\npending:\n ");
1054 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1055 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
1056 sh->evtchn_pending[i],
1057 i % 8 == 0 ? "\n " : " ");
1058 printk("\nglobal mask:\n ");
1059 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1061 (int)(sizeof(sh->evtchn_mask[0])*2),
1063 i % 8 == 0 ? "\n " : " ");
1065 printk("\nglobally unmasked:\n ");
1066 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1067 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1068 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1069 i % 8 == 0 ? "\n " : " ");
1071 printk("\nlocal cpu%d mask:\n ", cpu);
1072 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
1073 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
1075 i % 8 == 0 ? "\n " : " ");
1077 printk("\nlocally unmasked:\n ");
1078 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1079 unsigned long pending = sh->evtchn_pending[i]
1080 & ~sh->evtchn_mask[i]
1082 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1083 pending, i % 8 == 0 ? "\n " : " ");
1086 printk("\npending list:\n");
1087 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
1088 if (sync_test_bit(i, sh->evtchn_pending)) {
1089 int word_idx = i / BITS_PER_LONG;
1090 printk(" %d: event %d -> irq %d%s%s%s\n",
1091 cpu_from_evtchn(i), i,
1093 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1095 !sync_test_bit(i, sh->evtchn_mask)
1096 ? "" : " globally-masked",
1097 sync_test_bit(i, cpu_evtchn)
1098 ? "" : " locally-masked");
1102 spin_unlock_irqrestore(&debug_lock, flags);
1107 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1108 static DEFINE_PER_CPU(unsigned int, current_word_idx);
1109 static DEFINE_PER_CPU(unsigned int, current_bit_idx);
1112 * Mask out the i least significant bits of w
1114 #define MASK_LSBS(w, i) (w & ((~0UL) << i))
1117 * Search the CPUs pending events bitmasks. For each one found, map
1118 * the event number to an irq, and feed it into do_IRQ() for
1121 * Xen uses a two-level bitmap to speed searching. The first level is
1122 * a bitset of words which contain pending event bits. The second
1123 * level is a bitset of pending events themselves.
1125 static void __xen_evtchn_do_upcall(void)
1127 int start_word_idx, start_bit_idx;
1128 int word_idx, bit_idx;
1130 int cpu = get_cpu();
1131 struct shared_info *s = HYPERVISOR_shared_info;
1132 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1136 unsigned long pending_words;
1138 vcpu_info->evtchn_upcall_pending = 0;
1140 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1143 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1144 /* Clear master flag /before/ clearing selector flag. */
1147 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
1149 start_word_idx = __this_cpu_read(current_word_idx);
1150 start_bit_idx = __this_cpu_read(current_bit_idx);
1152 word_idx = start_word_idx;
1154 for (i = 0; pending_words != 0; i++) {
1155 unsigned long pending_bits;
1156 unsigned long words;
1158 words = MASK_LSBS(pending_words, word_idx);
1161 * If we masked out all events, wrap to beginning.
1168 word_idx = __ffs(words);
1170 pending_bits = active_evtchns(cpu, s, word_idx);
1171 bit_idx = 0; /* usually scan entire word from start */
1172 if (word_idx == start_word_idx) {
1173 /* We scan the starting word in two parts */
1175 /* 1st time: start in the middle */
1176 bit_idx = start_bit_idx;
1178 /* 2nd time: mask bits done already */
1179 bit_idx &= (1UL << start_bit_idx) - 1;
1185 struct irq_desc *desc;
1187 bits = MASK_LSBS(pending_bits, bit_idx);
1189 /* If we masked out all events, move on. */
1193 bit_idx = __ffs(bits);
1196 port = (word_idx * BITS_PER_LONG) + bit_idx;
1197 irq = evtchn_to_irq[port];
1203 desc = irq_to_desc(irq);
1205 generic_handle_irq_desc(irq, desc);
1208 bit_idx = (bit_idx + 1) % BITS_PER_LONG;
1210 /* Next caller starts at last processed + 1 */
1211 __this_cpu_write(current_word_idx,
1212 bit_idx ? word_idx :
1213 (word_idx+1) % BITS_PER_LONG);
1214 __this_cpu_write(current_bit_idx, bit_idx);
1215 } while (bit_idx != 0);
1217 /* Scan start_l1i twice; all others once. */
1218 if ((word_idx != start_word_idx) || (i != 0))
1219 pending_words &= ~(1UL << word_idx);
1221 word_idx = (word_idx + 1) % BITS_PER_LONG;
1224 BUG_ON(!irqs_disabled());
1226 count = __this_cpu_read(xed_nesting_count);
1227 __this_cpu_write(xed_nesting_count, 0);
1228 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1235 void xen_evtchn_do_upcall(struct pt_regs *regs)
1237 struct pt_regs *old_regs = set_irq_regs(regs);
1242 __xen_evtchn_do_upcall();
1245 set_irq_regs(old_regs);
1248 void xen_hvm_evtchn_do_upcall(void)
1250 __xen_evtchn_do_upcall();
1252 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1254 /* Rebind a new event channel to an existing irq. */
1255 void rebind_evtchn_irq(int evtchn, int irq)
1257 struct irq_info *info = info_for_irq(irq);
1259 /* Make sure the irq is masked, since the new event channel
1260 will also be masked. */
1263 spin_lock(&irq_mapping_update_lock);
1265 /* After resume the irq<->evtchn mappings are all cleared out */
1266 BUG_ON(evtchn_to_irq[evtchn] != -1);
1267 /* Expect irq to have been bound before,
1268 so there should be a proper type */
1269 BUG_ON(info->type == IRQT_UNBOUND);
1271 xen_irq_info_evtchn_init(irq, evtchn);
1273 spin_unlock(&irq_mapping_update_lock);
1275 /* new event channels are always bound to cpu 0 */
1276 irq_set_affinity(irq, cpumask_of(0));
1278 /* Unmask the event channel. */
1282 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1283 static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
1285 struct evtchn_bind_vcpu bind_vcpu;
1286 int evtchn = evtchn_from_irq(irq);
1288 if (!VALID_EVTCHN(evtchn))
1292 * Events delivered via platform PCI interrupts are always
1293 * routed to vcpu 0 and hence cannot be rebound.
1295 if (xen_hvm_domain() && !xen_have_vector_callback)
1298 /* Send future instances of this interrupt to other vcpu. */
1299 bind_vcpu.port = evtchn;
1300 bind_vcpu.vcpu = tcpu;
1303 * If this fails, it usually just indicates that we're dealing with a
1304 * virq or IPI channel, which don't actually need to be rebound. Ignore
1305 * it, but don't do the xenlinux-level rebind in that case.
1307 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1308 bind_evtchn_to_cpu(evtchn, tcpu);
1313 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1316 unsigned tcpu = cpumask_first(dest);
1318 return rebind_irq_to_cpu(data->irq, tcpu);
1321 int resend_irq_on_evtchn(unsigned int irq)
1323 int masked, evtchn = evtchn_from_irq(irq);
1324 struct shared_info *s = HYPERVISOR_shared_info;
1326 if (!VALID_EVTCHN(evtchn))
1329 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1330 sync_set_bit(evtchn, s->evtchn_pending);
1332 unmask_evtchn(evtchn);
1337 static void enable_dynirq(struct irq_data *data)
1339 int evtchn = evtchn_from_irq(data->irq);
1341 if (VALID_EVTCHN(evtchn))
1342 unmask_evtchn(evtchn);
1345 static void disable_dynirq(struct irq_data *data)
1347 int evtchn = evtchn_from_irq(data->irq);
1349 if (VALID_EVTCHN(evtchn))
1350 mask_evtchn(evtchn);
1353 static void ack_dynirq(struct irq_data *data)
1355 int evtchn = evtchn_from_irq(data->irq);
1357 irq_move_masked_irq(data);
1359 if (VALID_EVTCHN(evtchn))
1360 unmask_evtchn(evtchn);
1363 static int retrigger_dynirq(struct irq_data *data)
1365 int evtchn = evtchn_from_irq(data->irq);
1366 struct shared_info *sh = HYPERVISOR_shared_info;
1369 if (VALID_EVTCHN(evtchn)) {
1372 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1373 sync_set_bit(evtchn, sh->evtchn_pending);
1375 unmask_evtchn(evtchn);
1382 static void restore_pirqs(void)
1384 int pirq, rc, irq, gsi;
1385 struct physdev_map_pirq map_irq;
1386 struct irq_info *info;
1388 list_for_each_entry(info, &xen_irq_list_head, list) {
1389 if (info->type != IRQT_PIRQ)
1392 pirq = info->u.pirq.pirq;
1393 gsi = info->u.pirq.gsi;
1396 /* save/restore of PT devices doesn't work, so at this point the
1397 * only devices present are GSI based emulated devices */
1401 map_irq.domid = DOMID_SELF;
1402 map_irq.type = MAP_PIRQ_TYPE_GSI;
1403 map_irq.index = gsi;
1404 map_irq.pirq = pirq;
1406 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1408 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1409 gsi, irq, pirq, rc);
1414 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1416 __startup_pirq(irq);
1420 static void restore_cpu_virqs(unsigned int cpu)
1422 struct evtchn_bind_virq bind_virq;
1423 int virq, irq, evtchn;
1425 for (virq = 0; virq < NR_VIRQS; virq++) {
1426 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1429 BUG_ON(virq_from_irq(irq) != virq);
1431 /* Get a new binding from Xen. */
1432 bind_virq.virq = virq;
1433 bind_virq.vcpu = cpu;
1434 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1437 evtchn = bind_virq.port;
1439 /* Record the new mapping. */
1440 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
1441 bind_evtchn_to_cpu(evtchn, cpu);
1445 static void restore_cpu_ipis(unsigned int cpu)
1447 struct evtchn_bind_ipi bind_ipi;
1448 int ipi, irq, evtchn;
1450 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1451 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1454 BUG_ON(ipi_from_irq(irq) != ipi);
1456 /* Get a new binding from Xen. */
1457 bind_ipi.vcpu = cpu;
1458 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1461 evtchn = bind_ipi.port;
1463 /* Record the new mapping. */
1464 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
1465 bind_evtchn_to_cpu(evtchn, cpu);
1469 /* Clear an irq's pending state, in preparation for polling on it */
1470 void xen_clear_irq_pending(int irq)
1472 int evtchn = evtchn_from_irq(irq);
1474 if (VALID_EVTCHN(evtchn))
1475 clear_evtchn(evtchn);
1477 EXPORT_SYMBOL(xen_clear_irq_pending);
1478 void xen_set_irq_pending(int irq)
1480 int evtchn = evtchn_from_irq(irq);
1482 if (VALID_EVTCHN(evtchn))
1486 bool xen_test_irq_pending(int irq)
1488 int evtchn = evtchn_from_irq(irq);
1491 if (VALID_EVTCHN(evtchn))
1492 ret = test_evtchn(evtchn);
1497 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1498 * the irq will be disabled so it won't deliver an interrupt. */
1499 void xen_poll_irq_timeout(int irq, u64 timeout)
1501 evtchn_port_t evtchn = evtchn_from_irq(irq);
1503 if (VALID_EVTCHN(evtchn)) {
1504 struct sched_poll poll;
1507 poll.timeout = timeout;
1508 set_xen_guest_handle(poll.ports, &evtchn);
1510 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1514 EXPORT_SYMBOL(xen_poll_irq_timeout);
1515 /* Poll waiting for an irq to become pending. In the usual case, the
1516 * irq will be disabled so it won't deliver an interrupt. */
1517 void xen_poll_irq(int irq)
1519 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1522 /* Check whether the IRQ line is shared with other guests. */
1523 int xen_test_irq_shared(int irq)
1525 struct irq_info *info = info_for_irq(irq);
1526 struct physdev_irq_status_query irq_status = { .irq = info->u.pirq.pirq };
1528 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1530 return !(irq_status.flags & XENIRQSTAT_shared);
1532 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1534 void xen_irq_resume(void)
1536 unsigned int cpu, evtchn;
1537 struct irq_info *info;
1539 init_evtchn_cpu_bindings();
1541 /* New event-channel space is not 'live' yet. */
1542 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1543 mask_evtchn(evtchn);
1545 /* No IRQ <-> event-channel mappings. */
1546 list_for_each_entry(info, &xen_irq_list_head, list)
1547 info->evtchn = 0; /* zap event-channel binding */
1549 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1550 evtchn_to_irq[evtchn] = -1;
1552 for_each_possible_cpu(cpu) {
1553 restore_cpu_virqs(cpu);
1554 restore_cpu_ipis(cpu);
1560 static struct irq_chip xen_dynamic_chip __read_mostly = {
1563 .irq_disable = disable_dynirq,
1564 .irq_mask = disable_dynirq,
1565 .irq_unmask = enable_dynirq,
1567 .irq_eoi = ack_dynirq,
1568 .irq_set_affinity = set_affinity_irq,
1569 .irq_retrigger = retrigger_dynirq,
1572 static struct irq_chip xen_pirq_chip __read_mostly = {
1575 .irq_startup = startup_pirq,
1576 .irq_shutdown = shutdown_pirq,
1578 .irq_enable = enable_pirq,
1579 .irq_unmask = enable_pirq,
1581 .irq_disable = disable_pirq,
1582 .irq_mask = disable_pirq,
1584 .irq_ack = ack_pirq,
1586 .irq_set_affinity = set_affinity_irq,
1588 .irq_retrigger = retrigger_dynirq,
1591 static struct irq_chip xen_percpu_chip __read_mostly = {
1592 .name = "xen-percpu",
1594 .irq_disable = disable_dynirq,
1595 .irq_mask = disable_dynirq,
1596 .irq_unmask = enable_dynirq,
1598 .irq_ack = ack_dynirq,
1601 int xen_set_callback_via(uint64_t via)
1603 struct xen_hvm_param a;
1604 a.domid = DOMID_SELF;
1605 a.index = HVM_PARAM_CALLBACK_IRQ;
1607 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1609 EXPORT_SYMBOL_GPL(xen_set_callback_via);
1611 #ifdef CONFIG_XEN_PVHVM
1612 /* Vector callbacks are better than PCI interrupts to receive event
1613 * channel notifications because we can receive vector callbacks on any
1614 * vcpu and we don't need PCI support or APIC interactions. */
1615 void xen_callback_vector(void)
1618 uint64_t callback_via;
1619 if (xen_have_vector_callback) {
1620 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1621 rc = xen_set_callback_via(callback_via);
1623 printk(KERN_ERR "Request for Xen HVM callback vector"
1625 xen_have_vector_callback = 0;
1628 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1630 /* in the restore case the vector has already been allocated */
1631 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1632 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1636 void xen_callback_vector(void) {}
1639 void __init xen_init_IRQ(void)
1643 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1645 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1646 evtchn_to_irq[i] = -1;
1648 init_evtchn_cpu_bindings();
1650 /* No event channels are 'live' right now. */
1651 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1654 if (xen_hvm_domain()) {
1655 xen_callback_vector();
1657 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1658 * __acpi_register_gsi can point at the right function */
1661 irq_ctx_init(smp_processor_id());
1662 if (xen_initial_domain())