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 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
26 #include <linux/linkage.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/module.h>
30 #include <linux/string.h>
31 #include <linux/bootmem.h>
32 #include <linux/slab.h>
33 #include <linux/irqnr.h>
34 #include <linux/pci.h>
38 #include <asm/ptrace.h>
41 #include <asm/io_apic.h>
42 #include <asm/xen/page.h>
43 #include <asm/xen/pci.h>
45 #include <asm/sync_bitops.h>
46 #include <asm/xen/hypercall.h>
47 #include <asm/xen/hypervisor.h>
51 #include <xen/xen-ops.h>
52 #include <xen/events.h>
53 #include <xen/interface/xen.h>
54 #include <xen/interface/event_channel.h>
55 #include <xen/interface/hvm/hvm_op.h>
56 #include <xen/interface/hvm/params.h>
57 #include <xen/interface/physdev.h>
58 #include <xen/interface/sched.h>
59 #include <xen/interface/vcpu.h>
60 #include <asm/hw_irq.h>
62 #include "events_internal.h"
64 const struct evtchn_ops *evtchn_ops;
67 * This lock protects updates to the following mapping and reference-count
68 * arrays. The lock does not need to be acquired to read the mapping tables.
70 static DEFINE_MUTEX(irq_mapping_update_lock);
72 static LIST_HEAD(xen_irq_list_head);
74 /* IRQ <-> VIRQ mapping. */
75 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
77 /* IRQ <-> IPI mapping */
78 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
82 static unsigned long *pirq_eoi_map;
84 static bool (*pirq_needs_eoi)(unsigned irq);
86 /* Xen will never allocate port zero for any purpose. */
87 #define VALID_EVTCHN(chn) ((chn) != 0)
89 static struct irq_chip xen_dynamic_chip;
90 static struct irq_chip xen_percpu_chip;
91 static struct irq_chip xen_pirq_chip;
92 static void enable_dynirq(struct irq_data *data);
93 static void disable_dynirq(struct irq_data *data);
95 /* Get info for IRQ */
96 struct irq_info *info_for_irq(unsigned irq)
98 return irq_get_handler_data(irq);
101 /* Constructors for packed IRQ information. */
102 static int xen_irq_info_common_setup(struct irq_info *info,
104 enum xen_irq_type type,
105 unsigned short evtchn,
109 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
113 info->evtchn = evtchn;
116 evtchn_to_irq[evtchn] = irq;
118 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
120 return xen_evtchn_port_setup(info);
123 static int xen_irq_info_evtchn_setup(unsigned irq,
124 unsigned short evtchn)
126 struct irq_info *info = info_for_irq(irq);
128 return xen_irq_info_common_setup(info, irq, IRQT_EVTCHN, evtchn, 0);
131 static int xen_irq_info_ipi_setup(unsigned cpu,
133 unsigned short evtchn,
136 struct irq_info *info = info_for_irq(irq);
140 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
142 return xen_irq_info_common_setup(info, irq, IRQT_IPI, evtchn, 0);
145 static int xen_irq_info_virq_setup(unsigned cpu,
147 unsigned short evtchn,
150 struct irq_info *info = info_for_irq(irq);
154 per_cpu(virq_to_irq, cpu)[virq] = irq;
156 return xen_irq_info_common_setup(info, irq, IRQT_VIRQ, evtchn, 0);
159 static int xen_irq_info_pirq_setup(unsigned irq,
160 unsigned short evtchn,
166 struct irq_info *info = info_for_irq(irq);
168 info->u.pirq.pirq = pirq;
169 info->u.pirq.gsi = gsi;
170 info->u.pirq.domid = domid;
171 info->u.pirq.flags = flags;
173 return xen_irq_info_common_setup(info, irq, IRQT_PIRQ, evtchn, 0);
177 * Accessors for packed IRQ information.
179 unsigned int evtchn_from_irq(unsigned irq)
181 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
184 return info_for_irq(irq)->evtchn;
187 unsigned irq_from_evtchn(unsigned int evtchn)
189 return evtchn_to_irq[evtchn];
191 EXPORT_SYMBOL_GPL(irq_from_evtchn);
193 int irq_from_virq(unsigned int cpu, unsigned int virq)
195 return per_cpu(virq_to_irq, cpu)[virq];
198 static enum ipi_vector ipi_from_irq(unsigned irq)
200 struct irq_info *info = info_for_irq(irq);
202 BUG_ON(info == NULL);
203 BUG_ON(info->type != IRQT_IPI);
208 static unsigned virq_from_irq(unsigned irq)
210 struct irq_info *info = info_for_irq(irq);
212 BUG_ON(info == NULL);
213 BUG_ON(info->type != IRQT_VIRQ);
218 static unsigned pirq_from_irq(unsigned irq)
220 struct irq_info *info = info_for_irq(irq);
222 BUG_ON(info == NULL);
223 BUG_ON(info->type != IRQT_PIRQ);
225 return info->u.pirq.pirq;
228 static enum xen_irq_type type_from_irq(unsigned irq)
230 return info_for_irq(irq)->type;
233 unsigned cpu_from_irq(unsigned irq)
235 return info_for_irq(irq)->cpu;
238 unsigned int cpu_from_evtchn(unsigned int evtchn)
240 int irq = evtchn_to_irq[evtchn];
244 ret = cpu_from_irq(irq);
250 static bool pirq_check_eoi_map(unsigned irq)
252 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
256 static bool pirq_needs_eoi_flag(unsigned irq)
258 struct irq_info *info = info_for_irq(irq);
259 BUG_ON(info->type != IRQT_PIRQ);
261 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
264 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
266 int irq = evtchn_to_irq[chn];
267 struct irq_info *info = info_for_irq(irq);
271 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
274 xen_evtchn_port_bind_to_cpu(info, cpu);
280 * notify_remote_via_irq - send event to remote end of event channel via irq
281 * @irq: irq of event channel to send event to
283 * Unlike notify_remote_via_evtchn(), this is safe to use across
284 * save/restore. Notifications on a broken connection are silently
287 void notify_remote_via_irq(int irq)
289 int evtchn = evtchn_from_irq(irq);
291 if (VALID_EVTCHN(evtchn))
292 notify_remote_via_evtchn(evtchn);
294 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
296 static void xen_irq_init(unsigned irq)
298 struct irq_info *info;
300 struct irq_desc *desc = irq_to_desc(irq);
302 /* By default all event channels notify CPU#0. */
303 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
306 info = kzalloc(sizeof(*info), GFP_KERNEL);
308 panic("Unable to allocate metadata for IRQ%d\n", irq);
310 info->type = IRQT_UNBOUND;
313 irq_set_handler_data(irq, info);
315 list_add_tail(&info->list, &xen_irq_list_head);
318 static int __must_check xen_allocate_irq_dynamic(void)
323 #ifdef CONFIG_X86_IO_APIC
325 * For an HVM guest or domain 0 which see "real" (emulated or
326 * actual respectively) GSIs we allocate dynamic IRQs
327 * e.g. those corresponding to event channels or MSIs
328 * etc. from the range above those "real" GSIs to avoid
331 if (xen_initial_domain() || xen_hvm_domain())
332 first = get_nr_irqs_gsi();
335 irq = irq_alloc_desc_from(first, -1);
343 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
348 * A PV guest has no concept of a GSI (since it has no ACPI
349 * nor access to/knowledge of the physical APICs). Therefore
350 * all IRQs are dynamically allocated from the entire IRQ
353 if (xen_pv_domain() && !xen_initial_domain())
354 return xen_allocate_irq_dynamic();
356 /* Legacy IRQ descriptors are already allocated by the arch. */
357 if (gsi < NR_IRQS_LEGACY)
360 irq = irq_alloc_desc_at(gsi, -1);
367 static void xen_free_irq(unsigned irq)
369 struct irq_info *info = irq_get_handler_data(irq);
374 list_del(&info->list);
376 irq_set_handler_data(irq, NULL);
378 WARN_ON(info->refcnt > 0);
382 /* Legacy IRQ descriptors are managed by the arch. */
383 if (irq < NR_IRQS_LEGACY)
389 static void pirq_query_unmask(int irq)
391 struct physdev_irq_status_query irq_status;
392 struct irq_info *info = info_for_irq(irq);
394 BUG_ON(info->type != IRQT_PIRQ);
396 irq_status.irq = pirq_from_irq(irq);
397 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
398 irq_status.flags = 0;
400 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
401 if (irq_status.flags & XENIRQSTAT_needs_eoi)
402 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
405 static bool probing_irq(int irq)
407 struct irq_desc *desc = irq_to_desc(irq);
409 return desc && desc->action == NULL;
412 static void eoi_pirq(struct irq_data *data)
414 int evtchn = evtchn_from_irq(data->irq);
415 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
420 if (VALID_EVTCHN(evtchn))
421 clear_evtchn(evtchn);
423 if (pirq_needs_eoi(data->irq)) {
424 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
429 static void mask_ack_pirq(struct irq_data *data)
431 disable_dynirq(data);
435 static unsigned int __startup_pirq(unsigned int irq)
437 struct evtchn_bind_pirq bind_pirq;
438 struct irq_info *info = info_for_irq(irq);
439 int evtchn = evtchn_from_irq(irq);
442 BUG_ON(info->type != IRQT_PIRQ);
444 if (VALID_EVTCHN(evtchn))
447 bind_pirq.pirq = pirq_from_irq(irq);
448 /* NB. We are happy to share unless we are probing. */
449 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
450 BIND_PIRQ__WILL_SHARE : 0;
451 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
453 if (!probing_irq(irq))
454 pr_info("Failed to obtain physical IRQ %d\n", irq);
457 evtchn = bind_pirq.port;
459 pirq_query_unmask(irq);
461 evtchn_to_irq[evtchn] = irq;
462 bind_evtchn_to_cpu(evtchn, 0);
463 info->evtchn = evtchn;
466 unmask_evtchn(evtchn);
467 eoi_pirq(irq_get_irq_data(irq));
472 static unsigned int startup_pirq(struct irq_data *data)
474 return __startup_pirq(data->irq);
477 static void shutdown_pirq(struct irq_data *data)
479 struct evtchn_close close;
480 unsigned int irq = data->irq;
481 struct irq_info *info = info_for_irq(irq);
482 int evtchn = evtchn_from_irq(irq);
484 BUG_ON(info->type != IRQT_PIRQ);
486 if (!VALID_EVTCHN(evtchn))
492 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
495 bind_evtchn_to_cpu(evtchn, 0);
496 evtchn_to_irq[evtchn] = -1;
500 static void enable_pirq(struct irq_data *data)
505 static void disable_pirq(struct irq_data *data)
507 disable_dynirq(data);
510 int xen_irq_from_gsi(unsigned gsi)
512 struct irq_info *info;
514 list_for_each_entry(info, &xen_irq_list_head, list) {
515 if (info->type != IRQT_PIRQ)
518 if (info->u.pirq.gsi == gsi)
524 EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
526 static void __unbind_from_irq(unsigned int irq)
528 struct evtchn_close close;
529 int evtchn = evtchn_from_irq(irq);
530 struct irq_info *info = irq_get_handler_data(irq);
532 if (info->refcnt > 0) {
534 if (info->refcnt != 0)
538 if (VALID_EVTCHN(evtchn)) {
540 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
543 switch (type_from_irq(irq)) {
545 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
546 [virq_from_irq(irq)] = -1;
549 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
550 [ipi_from_irq(irq)] = -1;
556 /* Closed ports are implicitly re-bound to VCPU0. */
557 bind_evtchn_to_cpu(evtchn, 0);
559 evtchn_to_irq[evtchn] = -1;
562 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
568 * Do not make any assumptions regarding the relationship between the
569 * IRQ number returned here and the Xen pirq argument.
571 * Note: We don't assign an event channel until the irq actually started
572 * up. Return an existing irq if we've already got one for the gsi.
574 * Shareable implies level triggered, not shareable implies edge
577 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
578 unsigned pirq, int shareable, char *name)
581 struct physdev_irq irq_op;
584 mutex_lock(&irq_mapping_update_lock);
586 irq = xen_irq_from_gsi(gsi);
588 pr_info("%s: returning irq %d for gsi %u\n",
593 irq = xen_allocate_irq_gsi(gsi);
600 /* Only the privileged domain can do this. For non-priv, the pcifront
601 * driver provides a PCI bus that does the call to do exactly
602 * this in the priv domain. */
603 if (xen_initial_domain() &&
604 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
610 ret = xen_irq_info_pirq_setup(irq, 0, pirq, gsi, DOMID_SELF,
611 shareable ? PIRQ_SHAREABLE : 0);
613 __unbind_from_irq(irq);
618 pirq_query_unmask(irq);
619 /* We try to use the handler with the appropriate semantic for the
620 * type of interrupt: if the interrupt is an edge triggered
621 * interrupt we use handle_edge_irq.
623 * On the other hand if the interrupt is level triggered we use
624 * handle_fasteoi_irq like the native code does for this kind of
627 * Depending on the Xen version, pirq_needs_eoi might return true
628 * not only for level triggered interrupts but for edge triggered
629 * interrupts too. In any case Xen always honors the eoi mechanism,
630 * not injecting any more pirqs of the same kind if the first one
631 * hasn't received an eoi yet. Therefore using the fasteoi handler
632 * is the right choice either way.
635 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
636 handle_fasteoi_irq, name);
638 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
639 handle_edge_irq, name);
642 mutex_unlock(&irq_mapping_update_lock);
647 #ifdef CONFIG_PCI_MSI
648 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
651 struct physdev_get_free_pirq op_get_free_pirq;
653 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
654 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
656 WARN_ONCE(rc == -ENOSYS,
657 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
659 return rc ? -1 : op_get_free_pirq.pirq;
662 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
663 int pirq, const char *name, domid_t domid)
667 mutex_lock(&irq_mapping_update_lock);
669 irq = xen_allocate_irq_dynamic();
673 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
676 ret = xen_irq_info_pirq_setup(irq, 0, pirq, 0, domid, 0);
679 ret = irq_set_msi_desc(irq, msidesc);
683 mutex_unlock(&irq_mapping_update_lock);
686 __unbind_from_irq(irq);
687 mutex_unlock(&irq_mapping_update_lock);
692 int xen_destroy_irq(int irq)
694 struct irq_desc *desc;
695 struct physdev_unmap_pirq unmap_irq;
696 struct irq_info *info = info_for_irq(irq);
699 mutex_lock(&irq_mapping_update_lock);
701 desc = irq_to_desc(irq);
705 if (xen_initial_domain()) {
706 unmap_irq.pirq = info->u.pirq.pirq;
707 unmap_irq.domid = info->u.pirq.domid;
708 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
709 /* If another domain quits without making the pci_disable_msix
710 * call, the Xen hypervisor takes care of freeing the PIRQs
711 * (free_domain_pirqs).
713 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
714 pr_info("domain %d does not have %d anymore\n",
715 info->u.pirq.domid, info->u.pirq.pirq);
717 pr_warn("unmap irq failed %d\n", rc);
725 mutex_unlock(&irq_mapping_update_lock);
729 int xen_irq_from_pirq(unsigned pirq)
733 struct irq_info *info;
735 mutex_lock(&irq_mapping_update_lock);
737 list_for_each_entry(info, &xen_irq_list_head, list) {
738 if (info->type != IRQT_PIRQ)
741 if (info->u.pirq.pirq == pirq)
746 mutex_unlock(&irq_mapping_update_lock);
752 int xen_pirq_from_irq(unsigned irq)
754 return pirq_from_irq(irq);
756 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
758 int bind_evtchn_to_irq(unsigned int evtchn)
763 mutex_lock(&irq_mapping_update_lock);
765 irq = evtchn_to_irq[evtchn];
768 irq = xen_allocate_irq_dynamic();
772 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
773 handle_edge_irq, "event");
775 ret = xen_irq_info_evtchn_setup(irq, evtchn);
777 __unbind_from_irq(irq);
782 struct irq_info *info = info_for_irq(irq);
783 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
787 mutex_unlock(&irq_mapping_update_lock);
791 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
793 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
795 struct evtchn_bind_ipi bind_ipi;
799 mutex_lock(&irq_mapping_update_lock);
801 irq = per_cpu(ipi_to_irq, cpu)[ipi];
804 irq = xen_allocate_irq_dynamic();
808 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
809 handle_percpu_irq, "ipi");
812 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
815 evtchn = bind_ipi.port;
817 ret = xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
819 __unbind_from_irq(irq);
823 bind_evtchn_to_cpu(evtchn, cpu);
825 struct irq_info *info = info_for_irq(irq);
826 WARN_ON(info == NULL || info->type != IRQT_IPI);
830 mutex_unlock(&irq_mapping_update_lock);
834 static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
835 unsigned int remote_port)
837 struct evtchn_bind_interdomain bind_interdomain;
840 bind_interdomain.remote_dom = remote_domain;
841 bind_interdomain.remote_port = remote_port;
843 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
846 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
849 static int find_virq(unsigned int virq, unsigned int cpu)
851 struct evtchn_status status;
852 int port, rc = -ENOENT;
854 memset(&status, 0, sizeof(status));
855 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
856 status.dom = DOMID_SELF;
858 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
861 if (status.status != EVTCHNSTAT_virq)
863 if (status.u.virq == virq && status.vcpu == cpu) {
871 int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
873 struct evtchn_bind_virq bind_virq;
874 int evtchn, irq, ret;
876 mutex_lock(&irq_mapping_update_lock);
878 irq = per_cpu(virq_to_irq, cpu)[virq];
881 irq = xen_allocate_irq_dynamic();
885 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
886 handle_percpu_irq, "virq");
888 bind_virq.virq = virq;
889 bind_virq.vcpu = cpu;
890 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
893 evtchn = bind_virq.port;
896 ret = find_virq(virq, cpu);
901 ret = xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
903 __unbind_from_irq(irq);
908 bind_evtchn_to_cpu(evtchn, cpu);
910 struct irq_info *info = info_for_irq(irq);
911 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
915 mutex_unlock(&irq_mapping_update_lock);
920 static void unbind_from_irq(unsigned int irq)
922 mutex_lock(&irq_mapping_update_lock);
923 __unbind_from_irq(irq);
924 mutex_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 | IRQF_EARLY_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 struct irq_info *info = irq_get_handler_data(irq);
1018 free_irq(irq, dev_id);
1019 unbind_from_irq(irq);
1021 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1023 int evtchn_make_refcounted(unsigned int evtchn)
1025 int irq = evtchn_to_irq[evtchn];
1026 struct irq_info *info;
1031 info = irq_get_handler_data(irq);
1036 WARN_ON(info->refcnt != -1);
1042 EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1044 int evtchn_get(unsigned int evtchn)
1047 struct irq_info *info;
1050 if (evtchn >= NR_EVENT_CHANNELS)
1053 mutex_lock(&irq_mapping_update_lock);
1055 irq = evtchn_to_irq[evtchn];
1059 info = irq_get_handler_data(irq);
1065 if (info->refcnt <= 0)
1071 mutex_unlock(&irq_mapping_update_lock);
1075 EXPORT_SYMBOL_GPL(evtchn_get);
1077 void evtchn_put(unsigned int evtchn)
1079 int irq = evtchn_to_irq[evtchn];
1080 if (WARN_ON(irq == -1))
1082 unbind_from_irq(irq);
1084 EXPORT_SYMBOL_GPL(evtchn_put);
1086 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1091 if (unlikely(vector == XEN_NMI_VECTOR)) {
1092 int rc = HYPERVISOR_vcpu_op(VCPUOP_send_nmi, cpu, NULL);
1094 printk(KERN_WARNING "Sending nmi to CPU%d failed (rc:%d)\n", cpu, rc);
1098 irq = per_cpu(ipi_to_irq, cpu)[vector];
1100 notify_remote_via_irq(irq);
1103 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1105 static void __xen_evtchn_do_upcall(void)
1107 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1108 int cpu = get_cpu();
1112 vcpu_info->evtchn_upcall_pending = 0;
1114 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1117 xen_evtchn_handle_events(cpu);
1119 BUG_ON(!irqs_disabled());
1121 count = __this_cpu_read(xed_nesting_count);
1122 __this_cpu_write(xed_nesting_count, 0);
1123 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1130 void xen_evtchn_do_upcall(struct pt_regs *regs)
1132 struct pt_regs *old_regs = set_irq_regs(regs);
1139 __xen_evtchn_do_upcall();
1142 set_irq_regs(old_regs);
1145 void xen_hvm_evtchn_do_upcall(void)
1147 __xen_evtchn_do_upcall();
1149 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1151 /* Rebind a new event channel to an existing irq. */
1152 void rebind_evtchn_irq(int evtchn, int irq)
1154 struct irq_info *info = info_for_irq(irq);
1159 /* Make sure the irq is masked, since the new event channel
1160 will also be masked. */
1163 mutex_lock(&irq_mapping_update_lock);
1165 /* After resume the irq<->evtchn mappings are all cleared out */
1166 BUG_ON(evtchn_to_irq[evtchn] != -1);
1167 /* Expect irq to have been bound before,
1168 so there should be a proper type */
1169 BUG_ON(info->type == IRQT_UNBOUND);
1171 (void)xen_irq_info_evtchn_setup(irq, evtchn);
1173 mutex_unlock(&irq_mapping_update_lock);
1175 /* new event channels are always bound to cpu 0 */
1176 irq_set_affinity(irq, cpumask_of(0));
1178 /* Unmask the event channel. */
1182 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1183 static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
1185 struct evtchn_bind_vcpu bind_vcpu;
1186 int evtchn = evtchn_from_irq(irq);
1189 if (!VALID_EVTCHN(evtchn))
1193 * Events delivered via platform PCI interrupts are always
1194 * routed to vcpu 0 and hence cannot be rebound.
1196 if (xen_hvm_domain() && !xen_have_vector_callback)
1199 /* Send future instances of this interrupt to other vcpu. */
1200 bind_vcpu.port = evtchn;
1201 bind_vcpu.vcpu = tcpu;
1204 * Mask the event while changing the VCPU binding to prevent
1205 * it being delivered on an unexpected VCPU.
1207 masked = test_and_set_mask(evtchn);
1210 * If this fails, it usually just indicates that we're dealing with a
1211 * virq or IPI channel, which don't actually need to be rebound. Ignore
1212 * it, but don't do the xenlinux-level rebind in that case.
1214 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1215 bind_evtchn_to_cpu(evtchn, tcpu);
1218 unmask_evtchn(evtchn);
1223 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1226 unsigned tcpu = cpumask_first(dest);
1228 return rebind_irq_to_cpu(data->irq, tcpu);
1231 static int retrigger_evtchn(int evtchn)
1235 if (!VALID_EVTCHN(evtchn))
1238 masked = test_and_set_mask(evtchn);
1241 unmask_evtchn(evtchn);
1246 int resend_irq_on_evtchn(unsigned int irq)
1248 return retrigger_evtchn(evtchn_from_irq(irq));
1251 static void enable_dynirq(struct irq_data *data)
1253 int evtchn = evtchn_from_irq(data->irq);
1255 if (VALID_EVTCHN(evtchn))
1256 unmask_evtchn(evtchn);
1259 static void disable_dynirq(struct irq_data *data)
1261 int evtchn = evtchn_from_irq(data->irq);
1263 if (VALID_EVTCHN(evtchn))
1264 mask_evtchn(evtchn);
1267 static void ack_dynirq(struct irq_data *data)
1269 int evtchn = evtchn_from_irq(data->irq);
1273 if (VALID_EVTCHN(evtchn))
1274 clear_evtchn(evtchn);
1277 static void mask_ack_dynirq(struct irq_data *data)
1279 disable_dynirq(data);
1283 static int retrigger_dynirq(struct irq_data *data)
1285 return retrigger_evtchn(evtchn_from_irq(data->irq));
1288 static void restore_pirqs(void)
1290 int pirq, rc, irq, gsi;
1291 struct physdev_map_pirq map_irq;
1292 struct irq_info *info;
1294 list_for_each_entry(info, &xen_irq_list_head, list) {
1295 if (info->type != IRQT_PIRQ)
1298 pirq = info->u.pirq.pirq;
1299 gsi = info->u.pirq.gsi;
1302 /* save/restore of PT devices doesn't work, so at this point the
1303 * only devices present are GSI based emulated devices */
1307 map_irq.domid = DOMID_SELF;
1308 map_irq.type = MAP_PIRQ_TYPE_GSI;
1309 map_irq.index = gsi;
1310 map_irq.pirq = pirq;
1312 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1314 pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1315 gsi, irq, pirq, rc);
1320 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1322 __startup_pirq(irq);
1326 static void restore_cpu_virqs(unsigned int cpu)
1328 struct evtchn_bind_virq bind_virq;
1329 int virq, irq, evtchn;
1331 for (virq = 0; virq < NR_VIRQS; virq++) {
1332 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1335 BUG_ON(virq_from_irq(irq) != virq);
1337 /* Get a new binding from Xen. */
1338 bind_virq.virq = virq;
1339 bind_virq.vcpu = cpu;
1340 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1343 evtchn = bind_virq.port;
1345 /* Record the new mapping. */
1346 (void)xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1347 bind_evtchn_to_cpu(evtchn, cpu);
1351 static void restore_cpu_ipis(unsigned int cpu)
1353 struct evtchn_bind_ipi bind_ipi;
1354 int ipi, irq, evtchn;
1356 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1357 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1360 BUG_ON(ipi_from_irq(irq) != ipi);
1362 /* Get a new binding from Xen. */
1363 bind_ipi.vcpu = cpu;
1364 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1367 evtchn = bind_ipi.port;
1369 /* Record the new mapping. */
1370 (void)xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1371 bind_evtchn_to_cpu(evtchn, cpu);
1375 /* Clear an irq's pending state, in preparation for polling on it */
1376 void xen_clear_irq_pending(int irq)
1378 int evtchn = evtchn_from_irq(irq);
1380 if (VALID_EVTCHN(evtchn))
1381 clear_evtchn(evtchn);
1383 EXPORT_SYMBOL(xen_clear_irq_pending);
1384 void xen_set_irq_pending(int irq)
1386 int evtchn = evtchn_from_irq(irq);
1388 if (VALID_EVTCHN(evtchn))
1392 bool xen_test_irq_pending(int irq)
1394 int evtchn = evtchn_from_irq(irq);
1397 if (VALID_EVTCHN(evtchn))
1398 ret = test_evtchn(evtchn);
1403 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1404 * the irq will be disabled so it won't deliver an interrupt. */
1405 void xen_poll_irq_timeout(int irq, u64 timeout)
1407 evtchn_port_t evtchn = evtchn_from_irq(irq);
1409 if (VALID_EVTCHN(evtchn)) {
1410 struct sched_poll poll;
1413 poll.timeout = timeout;
1414 set_xen_guest_handle(poll.ports, &evtchn);
1416 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1420 EXPORT_SYMBOL(xen_poll_irq_timeout);
1421 /* Poll waiting for an irq to become pending. In the usual case, the
1422 * irq will be disabled so it won't deliver an interrupt. */
1423 void xen_poll_irq(int irq)
1425 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1428 /* Check whether the IRQ line is shared with other guests. */
1429 int xen_test_irq_shared(int irq)
1431 struct irq_info *info = info_for_irq(irq);
1432 struct physdev_irq_status_query irq_status;
1437 irq_status.irq = info->u.pirq.pirq;
1439 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1441 return !(irq_status.flags & XENIRQSTAT_shared);
1443 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1445 void xen_irq_resume(void)
1447 unsigned int cpu, evtchn;
1448 struct irq_info *info;
1450 /* New event-channel space is not 'live' yet. */
1451 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1452 mask_evtchn(evtchn);
1454 /* No IRQ <-> event-channel mappings. */
1455 list_for_each_entry(info, &xen_irq_list_head, list)
1456 info->evtchn = 0; /* zap event-channel binding */
1458 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1459 evtchn_to_irq[evtchn] = -1;
1461 for_each_possible_cpu(cpu) {
1462 restore_cpu_virqs(cpu);
1463 restore_cpu_ipis(cpu);
1469 static struct irq_chip xen_dynamic_chip __read_mostly = {
1472 .irq_disable = disable_dynirq,
1473 .irq_mask = disable_dynirq,
1474 .irq_unmask = enable_dynirq,
1476 .irq_ack = ack_dynirq,
1477 .irq_mask_ack = mask_ack_dynirq,
1479 .irq_set_affinity = set_affinity_irq,
1480 .irq_retrigger = retrigger_dynirq,
1483 static struct irq_chip xen_pirq_chip __read_mostly = {
1486 .irq_startup = startup_pirq,
1487 .irq_shutdown = shutdown_pirq,
1488 .irq_enable = enable_pirq,
1489 .irq_disable = disable_pirq,
1491 .irq_mask = disable_dynirq,
1492 .irq_unmask = enable_dynirq,
1494 .irq_ack = eoi_pirq,
1495 .irq_eoi = eoi_pirq,
1496 .irq_mask_ack = mask_ack_pirq,
1498 .irq_set_affinity = set_affinity_irq,
1500 .irq_retrigger = retrigger_dynirq,
1503 static struct irq_chip xen_percpu_chip __read_mostly = {
1504 .name = "xen-percpu",
1506 .irq_disable = disable_dynirq,
1507 .irq_mask = disable_dynirq,
1508 .irq_unmask = enable_dynirq,
1510 .irq_ack = ack_dynirq,
1513 int xen_set_callback_via(uint64_t via)
1515 struct xen_hvm_param a;
1516 a.domid = DOMID_SELF;
1517 a.index = HVM_PARAM_CALLBACK_IRQ;
1519 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1521 EXPORT_SYMBOL_GPL(xen_set_callback_via);
1523 #ifdef CONFIG_XEN_PVHVM
1524 /* Vector callbacks are better than PCI interrupts to receive event
1525 * channel notifications because we can receive vector callbacks on any
1526 * vcpu and we don't need PCI support or APIC interactions. */
1527 void xen_callback_vector(void)
1530 uint64_t callback_via;
1531 if (xen_have_vector_callback) {
1532 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
1533 rc = xen_set_callback_via(callback_via);
1535 pr_err("Request for Xen HVM callback vector failed\n");
1536 xen_have_vector_callback = 0;
1539 pr_info("Xen HVM callback vector for event delivery is enabled\n");
1540 /* in the restore case the vector has already been allocated */
1541 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
1542 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
1543 xen_hvm_callback_vector);
1547 void xen_callback_vector(void) {}
1550 void __init xen_init_IRQ(void)
1554 xen_evtchn_2l_init();
1556 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1558 BUG_ON(!evtchn_to_irq);
1559 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1560 evtchn_to_irq[i] = -1;
1562 /* No event channels are 'live' right now. */
1563 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1566 pirq_needs_eoi = pirq_needs_eoi_flag;
1569 if (xen_hvm_domain()) {
1570 xen_callback_vector();
1572 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1573 * __acpi_register_gsi can point at the right function */
1577 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1579 irq_ctx_init(smp_processor_id());
1580 if (xen_initial_domain())
1581 pci_xen_initial_domain();
1583 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1584 eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1585 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1587 free_page((unsigned long) pirq_eoi_map);
1588 pirq_eoi_map = NULL;
1590 pirq_needs_eoi = pirq_check_eoi_map;