4 * Xen models interrupts with abstract event channels. Because each
5 * domain gets 1024 event channels, but NR_IRQ is not that large, we
6 * must dynamically map irqs<->event channels. The event channels
7 * interface with the rest of the kernel by defining a xen interrupt
8 * chip. When an event is recieved, it is mapped to an irq and sent
9 * through the normal interrupt processing path.
11 * There are four kinds of events which can be mapped to an event
14 * 1. Inter-domain notifications. This includes all the virtual
15 * device events, since they're driven by front-ends in another domain
17 * 2. VIRQs, typically used for timers. These are per-cpu events.
19 * 4. 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 *pirq_to_irq;
112 static int *evtchn_to_irq;
114 static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
117 /* Xen will never allocate port zero for any purpose. */
118 #define VALID_EVTCHN(chn) ((chn) != 0)
120 static struct irq_chip xen_dynamic_chip;
121 static struct irq_chip xen_percpu_chip;
122 static struct irq_chip xen_pirq_chip;
124 /* Get info for IRQ */
125 static struct irq_info *info_for_irq(unsigned irq)
127 return get_irq_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;
200 pirq_to_irq[pirq] = irq;
204 * Accessors for packed IRQ information.
206 static unsigned int evtchn_from_irq(unsigned irq)
208 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
211 return info_for_irq(irq)->evtchn;
214 unsigned irq_from_evtchn(unsigned int evtchn)
216 return evtchn_to_irq[evtchn];
218 EXPORT_SYMBOL_GPL(irq_from_evtchn);
220 static enum ipi_vector ipi_from_irq(unsigned irq)
222 struct irq_info *info = info_for_irq(irq);
224 BUG_ON(info == NULL);
225 BUG_ON(info->type != IRQT_IPI);
230 static unsigned virq_from_irq(unsigned irq)
232 struct irq_info *info = info_for_irq(irq);
234 BUG_ON(info == NULL);
235 BUG_ON(info->type != IRQT_VIRQ);
240 static unsigned pirq_from_irq(unsigned irq)
242 struct irq_info *info = info_for_irq(irq);
244 BUG_ON(info == NULL);
245 BUG_ON(info->type != IRQT_PIRQ);
247 return info->u.pirq.pirq;
250 static unsigned gsi_from_irq(unsigned irq)
252 struct irq_info *info = info_for_irq(irq);
254 BUG_ON(info == NULL);
255 BUG_ON(info->type != IRQT_PIRQ);
257 return info->u.pirq.gsi;
260 static enum xen_irq_type type_from_irq(unsigned irq)
262 return info_for_irq(irq)->type;
265 static unsigned cpu_from_irq(unsigned irq)
267 return info_for_irq(irq)->cpu;
270 static unsigned int cpu_from_evtchn(unsigned int evtchn)
272 int irq = evtchn_to_irq[evtchn];
276 ret = cpu_from_irq(irq);
281 static bool pirq_needs_eoi(unsigned irq)
283 struct irq_info *info = info_for_irq(irq);
285 BUG_ON(info->type != IRQT_PIRQ);
287 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
290 static inline unsigned long active_evtchns(unsigned int cpu,
291 struct shared_info *sh,
294 return (sh->evtchn_pending[idx] &
295 per_cpu(cpu_evtchn_mask, cpu)[idx] &
296 ~sh->evtchn_mask[idx]);
299 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
301 int irq = evtchn_to_irq[chn];
305 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
308 clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
309 set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
311 info_for_irq(irq)->cpu = cpu;
314 static void init_evtchn_cpu_bindings(void)
318 struct irq_info *info;
320 /* By default all event channels notify CPU#0. */
321 list_for_each_entry(info, &xen_irq_list_head, list) {
322 struct irq_desc *desc = irq_to_desc(info->irq);
323 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
327 for_each_possible_cpu(i)
328 memset(per_cpu(cpu_evtchn_mask, i),
329 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
332 static inline void clear_evtchn(int port)
334 struct shared_info *s = HYPERVISOR_shared_info;
335 sync_clear_bit(port, &s->evtchn_pending[0]);
338 static inline void set_evtchn(int port)
340 struct shared_info *s = HYPERVISOR_shared_info;
341 sync_set_bit(port, &s->evtchn_pending[0]);
344 static inline int test_evtchn(int port)
346 struct shared_info *s = HYPERVISOR_shared_info;
347 return sync_test_bit(port, &s->evtchn_pending[0]);
352 * notify_remote_via_irq - send event to remote end of event channel via irq
353 * @irq: irq of event channel to send event to
355 * Unlike notify_remote_via_evtchn(), this is safe to use across
356 * save/restore. Notifications on a broken connection are silently
359 void notify_remote_via_irq(int irq)
361 int evtchn = evtchn_from_irq(irq);
363 if (VALID_EVTCHN(evtchn))
364 notify_remote_via_evtchn(evtchn);
366 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
368 static void mask_evtchn(int port)
370 struct shared_info *s = HYPERVISOR_shared_info;
371 sync_set_bit(port, &s->evtchn_mask[0]);
374 static void unmask_evtchn(int port)
376 struct shared_info *s = HYPERVISOR_shared_info;
377 unsigned int cpu = get_cpu();
379 BUG_ON(!irqs_disabled());
381 /* Slow path (hypercall) if this is a non-local port. */
382 if (unlikely(cpu != cpu_from_evtchn(port))) {
383 struct evtchn_unmask unmask = { .port = port };
384 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
386 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
388 sync_clear_bit(port, &s->evtchn_mask[0]);
391 * The following is basically the equivalent of
392 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
393 * the interrupt edge' if the channel is masked.
395 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
396 !sync_test_and_set_bit(port / BITS_PER_LONG,
397 &vcpu_info->evtchn_pending_sel))
398 vcpu_info->evtchn_upcall_pending = 1;
404 static void xen_irq_init(unsigned irq)
406 struct irq_info *info;
407 struct irq_desc *desc = irq_to_desc(irq);
409 /* By default all event channels notify CPU#0. */
410 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
412 info = kzalloc(sizeof(*info), GFP_KERNEL);
414 panic("Unable to allocate metadata for IRQ%d\n", irq);
416 info->type = IRQT_UNBOUND;
418 set_irq_data(irq, info);
420 list_add_tail(&info->list, &xen_irq_list_head);
423 static int xen_allocate_irq_dynamic(void)
428 #ifdef CONFIG_X86_IO_APIC
430 * For an HVM guest or domain 0 which see "real" (emulated or
431 * actual repectively) GSIs we allocate dynamic IRQs
432 * e.g. those corresponding to event channels or MSIs
433 * etc. from the range above those "real" GSIs to avoid
436 if (xen_initial_domain() || xen_hvm_domain())
437 first = get_nr_irqs_gsi();
441 irq = irq_alloc_desc_from(first, -1);
443 if (irq == -ENOMEM && first > NR_IRQS_LEGACY) {
444 printk(KERN_ERR "Out of dynamic IRQ space and eating into GSI space. You should increase nr_irqs\n");
445 first = max(NR_IRQS_LEGACY, first - NR_IRQS_LEGACY);
450 panic("No available IRQ to bind to: increase nr_irqs!\n");
457 static int xen_allocate_irq_gsi(unsigned gsi)
462 * A PV guest has no concept of a GSI (since it has no ACPI
463 * nor access to/knowledge of the physical APICs). Therefore
464 * all IRQs are dynamically allocated from the entire IRQ
467 if (xen_pv_domain() && !xen_initial_domain())
468 return xen_allocate_irq_dynamic();
470 /* Legacy IRQ descriptors are already allocated by the arch. */
471 if (gsi < NR_IRQS_LEGACY)
474 irq = irq_alloc_desc_at(gsi, -1);
477 panic("Unable to allocate to IRQ%d (%d)\n", gsi, irq);
484 static void xen_free_irq(unsigned irq)
486 struct irq_info *info = get_irq_data(irq);
488 list_del(&info->list);
490 set_irq_data(irq, NULL);
494 /* Legacy IRQ descriptors are managed by the arch. */
495 if (irq < NR_IRQS_LEGACY)
501 static void pirq_unmask_notify(int irq)
503 struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
505 if (unlikely(pirq_needs_eoi(irq))) {
506 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
511 static void pirq_query_unmask(int irq)
513 struct physdev_irq_status_query irq_status;
514 struct irq_info *info = info_for_irq(irq);
516 BUG_ON(info->type != IRQT_PIRQ);
518 irq_status.irq = pirq_from_irq(irq);
519 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
520 irq_status.flags = 0;
522 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
523 if (irq_status.flags & XENIRQSTAT_needs_eoi)
524 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
527 static bool probing_irq(int irq)
529 struct irq_desc *desc = irq_to_desc(irq);
531 return desc && desc->action == NULL;
534 static unsigned int __startup_pirq(unsigned int irq)
536 struct evtchn_bind_pirq bind_pirq;
537 struct irq_info *info = info_for_irq(irq);
538 int evtchn = evtchn_from_irq(irq);
541 BUG_ON(info->type != IRQT_PIRQ);
543 if (VALID_EVTCHN(evtchn))
546 bind_pirq.pirq = pirq_from_irq(irq);
547 /* NB. We are happy to share unless we are probing. */
548 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
549 BIND_PIRQ__WILL_SHARE : 0;
550 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
552 if (!probing_irq(irq))
553 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
557 evtchn = bind_pirq.port;
559 pirq_query_unmask(irq);
561 evtchn_to_irq[evtchn] = irq;
562 bind_evtchn_to_cpu(evtchn, 0);
563 info->evtchn = evtchn;
566 unmask_evtchn(evtchn);
567 pirq_unmask_notify(irq);
572 static unsigned int startup_pirq(struct irq_data *data)
574 return __startup_pirq(data->irq);
577 static void shutdown_pirq(struct irq_data *data)
579 struct evtchn_close close;
580 unsigned int irq = data->irq;
581 struct irq_info *info = info_for_irq(irq);
582 int evtchn = evtchn_from_irq(irq);
584 BUG_ON(info->type != IRQT_PIRQ);
586 if (!VALID_EVTCHN(evtchn))
592 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
595 bind_evtchn_to_cpu(evtchn, 0);
596 evtchn_to_irq[evtchn] = -1;
600 static void enable_pirq(struct irq_data *data)
605 static void disable_pirq(struct irq_data *data)
609 static void ack_pirq(struct irq_data *data)
611 int evtchn = evtchn_from_irq(data->irq);
613 move_native_irq(data->irq);
615 if (VALID_EVTCHN(evtchn)) {
617 clear_evtchn(evtchn);
621 static int find_irq_by_gsi(unsigned gsi)
623 struct irq_info *info;
625 list_for_each_entry(info, &xen_irq_list_head, list) {
626 if (info->type != IRQT_PIRQ)
629 if (info->u.pirq.gsi == gsi)
636 int xen_allocate_pirq_gsi(unsigned gsi)
642 * Do not make any assumptions regarding the relationship between the
643 * IRQ number returned here and the Xen pirq argument.
645 * Note: We don't assign an event channel until the irq actually started
646 * up. Return an existing irq if we've already got one for the gsi.
648 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
649 unsigned pirq, int shareable, char *name)
652 struct physdev_irq irq_op;
654 spin_lock(&irq_mapping_update_lock);
656 if (pirq > nr_irqs) {
657 printk(KERN_WARNING "xen_map_pirq_gsi: pirq %d > nr_irqs %d!\n",
662 irq = find_irq_by_gsi(gsi);
664 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
666 goto out; /* XXX need refcount? */
669 irq = xen_allocate_irq_gsi(gsi);
671 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
672 handle_level_irq, name);
677 /* Only the privileged domain can do this. For non-priv, the pcifront
678 * driver provides a PCI bus that does the call to do exactly
679 * this in the priv domain. */
680 if (xen_initial_domain() &&
681 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
687 xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector,
688 shareable ? PIRQ_SHAREABLE : 0);
691 spin_unlock(&irq_mapping_update_lock);
696 #ifdef CONFIG_PCI_MSI
697 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
700 struct physdev_get_free_pirq op_get_free_pirq;
702 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
703 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
705 WARN_ONCE(rc == -ENOSYS,
706 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
708 return rc ? -1 : op_get_free_pirq.pirq;
711 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
712 int pirq, int vector, const char *name)
716 spin_lock(&irq_mapping_update_lock);
718 irq = xen_allocate_irq_dynamic();
722 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
723 handle_level_irq, name);
725 xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, 0);
726 ret = set_irq_msi(irq, msidesc);
730 spin_unlock(&irq_mapping_update_lock);
733 spin_unlock(&irq_mapping_update_lock);
739 int xen_destroy_irq(int irq)
741 struct irq_desc *desc;
742 struct physdev_unmap_pirq unmap_irq;
743 struct irq_info *info = info_for_irq(irq);
746 spin_lock(&irq_mapping_update_lock);
748 desc = irq_to_desc(irq);
752 if (xen_initial_domain()) {
753 unmap_irq.pirq = info->u.pirq.pirq;
754 unmap_irq.domid = DOMID_SELF;
755 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
757 printk(KERN_WARNING "unmap irq failed %d\n", rc);
761 pirq_to_irq[info->u.pirq.pirq] = -1;
766 spin_unlock(&irq_mapping_update_lock);
770 int xen_irq_from_pirq(unsigned pirq)
772 return pirq_to_irq[pirq];
775 int bind_evtchn_to_irq(unsigned int evtchn)
779 spin_lock(&irq_mapping_update_lock);
781 irq = evtchn_to_irq[evtchn];
784 irq = xen_allocate_irq_dynamic();
786 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
787 handle_fasteoi_irq, "event");
789 xen_irq_info_evtchn_init(irq, evtchn);
792 spin_unlock(&irq_mapping_update_lock);
796 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
798 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
800 struct evtchn_bind_ipi bind_ipi;
803 spin_lock(&irq_mapping_update_lock);
805 irq = per_cpu(ipi_to_irq, cpu)[ipi];
808 irq = xen_allocate_irq_dynamic();
812 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
813 handle_percpu_irq, "ipi");
816 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
819 evtchn = bind_ipi.port;
821 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
823 bind_evtchn_to_cpu(evtchn, cpu);
827 spin_unlock(&irq_mapping_update_lock);
832 int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
834 struct evtchn_bind_virq bind_virq;
837 spin_lock(&irq_mapping_update_lock);
839 irq = per_cpu(virq_to_irq, cpu)[virq];
842 irq = xen_allocate_irq_dynamic();
844 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
845 handle_percpu_irq, "virq");
847 bind_virq.virq = virq;
848 bind_virq.vcpu = cpu;
849 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
852 evtchn = bind_virq.port;
854 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
856 bind_evtchn_to_cpu(evtchn, cpu);
859 spin_unlock(&irq_mapping_update_lock);
864 static void unbind_from_irq(unsigned int irq)
866 struct evtchn_close close;
867 int evtchn = evtchn_from_irq(irq);
869 spin_lock(&irq_mapping_update_lock);
871 if (VALID_EVTCHN(evtchn)) {
873 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
876 switch (type_from_irq(irq)) {
878 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
879 [virq_from_irq(irq)] = -1;
882 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
883 [ipi_from_irq(irq)] = -1;
889 /* Closed ports are implicitly re-bound to VCPU0. */
890 bind_evtchn_to_cpu(evtchn, 0);
892 evtchn_to_irq[evtchn] = -1;
895 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
899 spin_unlock(&irq_mapping_update_lock);
902 int bind_evtchn_to_irqhandler(unsigned int evtchn,
903 irq_handler_t handler,
904 unsigned long irqflags,
905 const char *devname, void *dev_id)
910 irq = bind_evtchn_to_irq(evtchn);
911 retval = request_irq(irq, handler, irqflags, devname, dev_id);
913 unbind_from_irq(irq);
919 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
921 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
922 irq_handler_t handler,
923 unsigned long irqflags, const char *devname, void *dev_id)
928 irq = bind_virq_to_irq(virq, cpu);
929 retval = request_irq(irq, handler, irqflags, devname, dev_id);
931 unbind_from_irq(irq);
937 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
939 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
941 irq_handler_t handler,
942 unsigned long irqflags,
948 irq = bind_ipi_to_irq(ipi, cpu);
952 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
953 retval = request_irq(irq, handler, irqflags, devname, dev_id);
955 unbind_from_irq(irq);
962 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
964 free_irq(irq, dev_id);
965 unbind_from_irq(irq);
967 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
969 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
971 int irq = per_cpu(ipi_to_irq, cpu)[vector];
973 notify_remote_via_irq(irq);
976 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
978 struct shared_info *sh = HYPERVISOR_shared_info;
979 int cpu = smp_processor_id();
980 unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
983 static DEFINE_SPINLOCK(debug_lock);
986 spin_lock_irqsave(&debug_lock, flags);
988 printk("\nvcpu %d\n ", cpu);
990 for_each_online_cpu(i) {
992 v = per_cpu(xen_vcpu, i);
993 pending = (get_irq_regs() && i == cpu)
994 ? xen_irqs_disabled(get_irq_regs())
995 : v->evtchn_upcall_mask;
996 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
997 pending, v->evtchn_upcall_pending,
998 (int)(sizeof(v->evtchn_pending_sel)*2),
999 v->evtchn_pending_sel);
1001 v = per_cpu(xen_vcpu, cpu);
1003 printk("\npending:\n ");
1004 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1005 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
1006 sh->evtchn_pending[i],
1007 i % 8 == 0 ? "\n " : " ");
1008 printk("\nglobal mask:\n ");
1009 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1011 (int)(sizeof(sh->evtchn_mask[0])*2),
1013 i % 8 == 0 ? "\n " : " ");
1015 printk("\nglobally unmasked:\n ");
1016 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1017 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1018 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1019 i % 8 == 0 ? "\n " : " ");
1021 printk("\nlocal cpu%d mask:\n ", cpu);
1022 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
1023 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
1025 i % 8 == 0 ? "\n " : " ");
1027 printk("\nlocally unmasked:\n ");
1028 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1029 unsigned long pending = sh->evtchn_pending[i]
1030 & ~sh->evtchn_mask[i]
1032 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1033 pending, i % 8 == 0 ? "\n " : " ");
1036 printk("\npending list:\n");
1037 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
1038 if (sync_test_bit(i, sh->evtchn_pending)) {
1039 int word_idx = i / BITS_PER_LONG;
1040 printk(" %d: event %d -> irq %d%s%s%s\n",
1041 cpu_from_evtchn(i), i,
1043 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1045 !sync_test_bit(i, sh->evtchn_mask)
1046 ? "" : " globally-masked",
1047 sync_test_bit(i, cpu_evtchn)
1048 ? "" : " locally-masked");
1052 spin_unlock_irqrestore(&debug_lock, flags);
1057 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1060 * Search the CPUs pending events bitmasks. For each one found, map
1061 * the event number to an irq, and feed it into do_IRQ() for
1064 * Xen uses a two-level bitmap to speed searching. The first level is
1065 * a bitset of words which contain pending event bits. The second
1066 * level is a bitset of pending events themselves.
1068 static void __xen_evtchn_do_upcall(void)
1070 int cpu = get_cpu();
1071 struct shared_info *s = HYPERVISOR_shared_info;
1072 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1076 unsigned long pending_words;
1078 vcpu_info->evtchn_upcall_pending = 0;
1080 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1083 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1084 /* Clear master flag /before/ clearing selector flag. */
1087 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
1088 while (pending_words != 0) {
1089 unsigned long pending_bits;
1090 int word_idx = __ffs(pending_words);
1091 pending_words &= ~(1UL << word_idx);
1093 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
1094 int bit_idx = __ffs(pending_bits);
1095 int port = (word_idx * BITS_PER_LONG) + bit_idx;
1096 int irq = evtchn_to_irq[port];
1097 struct irq_desc *desc;
1103 desc = irq_to_desc(irq);
1105 generic_handle_irq_desc(irq, desc);
1110 BUG_ON(!irqs_disabled());
1112 count = __this_cpu_read(xed_nesting_count);
1113 __this_cpu_write(xed_nesting_count, 0);
1114 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1121 void xen_evtchn_do_upcall(struct pt_regs *regs)
1123 struct pt_regs *old_regs = set_irq_regs(regs);
1128 __xen_evtchn_do_upcall();
1131 set_irq_regs(old_regs);
1134 void xen_hvm_evtchn_do_upcall(void)
1136 __xen_evtchn_do_upcall();
1138 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1140 /* Rebind a new event channel to an existing irq. */
1141 void rebind_evtchn_irq(int evtchn, int irq)
1143 struct irq_info *info = info_for_irq(irq);
1145 /* Make sure the irq is masked, since the new event channel
1146 will also be masked. */
1149 spin_lock(&irq_mapping_update_lock);
1151 /* After resume the irq<->evtchn mappings are all cleared out */
1152 BUG_ON(evtchn_to_irq[evtchn] != -1);
1153 /* Expect irq to have been bound before,
1154 so there should be a proper type */
1155 BUG_ON(info->type == IRQT_UNBOUND);
1157 xen_irq_info_evtchn_init(irq, evtchn);
1159 spin_unlock(&irq_mapping_update_lock);
1161 /* new event channels are always bound to cpu 0 */
1162 irq_set_affinity(irq, cpumask_of(0));
1164 /* Unmask the event channel. */
1168 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1169 static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
1171 struct evtchn_bind_vcpu bind_vcpu;
1172 int evtchn = evtchn_from_irq(irq);
1174 if (!VALID_EVTCHN(evtchn))
1178 * Events delivered via platform PCI interrupts are always
1179 * routed to vcpu 0 and hence cannot be rebound.
1181 if (xen_hvm_domain() && !xen_have_vector_callback)
1184 /* Send future instances of this interrupt to other vcpu. */
1185 bind_vcpu.port = evtchn;
1186 bind_vcpu.vcpu = tcpu;
1189 * If this fails, it usually just indicates that we're dealing with a
1190 * virq or IPI channel, which don't actually need to be rebound. Ignore
1191 * it, but don't do the xenlinux-level rebind in that case.
1193 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1194 bind_evtchn_to_cpu(evtchn, tcpu);
1199 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1202 unsigned tcpu = cpumask_first(dest);
1204 return rebind_irq_to_cpu(data->irq, tcpu);
1207 int resend_irq_on_evtchn(unsigned int irq)
1209 int masked, evtchn = evtchn_from_irq(irq);
1210 struct shared_info *s = HYPERVISOR_shared_info;
1212 if (!VALID_EVTCHN(evtchn))
1215 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1216 sync_set_bit(evtchn, s->evtchn_pending);
1218 unmask_evtchn(evtchn);
1223 static void enable_dynirq(struct irq_data *data)
1225 int evtchn = evtchn_from_irq(data->irq);
1227 if (VALID_EVTCHN(evtchn))
1228 unmask_evtchn(evtchn);
1231 static void disable_dynirq(struct irq_data *data)
1233 int evtchn = evtchn_from_irq(data->irq);
1235 if (VALID_EVTCHN(evtchn))
1236 mask_evtchn(evtchn);
1239 static void ack_dynirq(struct irq_data *data)
1241 int evtchn = evtchn_from_irq(data->irq);
1243 move_masked_irq(data->irq);
1245 if (VALID_EVTCHN(evtchn))
1246 unmask_evtchn(evtchn);
1249 static int retrigger_dynirq(struct irq_data *data)
1251 int evtchn = evtchn_from_irq(data->irq);
1252 struct shared_info *sh = HYPERVISOR_shared_info;
1255 if (VALID_EVTCHN(evtchn)) {
1258 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1259 sync_set_bit(evtchn, sh->evtchn_pending);
1261 unmask_evtchn(evtchn);
1268 static void restore_pirqs(void)
1270 int pirq, rc, irq, gsi;
1271 struct physdev_map_pirq map_irq;
1273 for (pirq = 0; pirq < nr_irqs; pirq++) {
1274 irq = pirq_to_irq[pirq];
1278 /* save/restore of PT devices doesn't work, so at this point the
1279 * only devices present are GSI based emulated devices */
1280 gsi = gsi_from_irq(irq);
1284 map_irq.domid = DOMID_SELF;
1285 map_irq.type = MAP_PIRQ_TYPE_GSI;
1286 map_irq.index = gsi;
1287 map_irq.pirq = pirq;
1289 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1291 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1292 gsi, irq, pirq, rc);
1294 pirq_to_irq[pirq] = -1;
1298 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1300 __startup_pirq(irq);
1304 static void restore_cpu_virqs(unsigned int cpu)
1306 struct evtchn_bind_virq bind_virq;
1307 int virq, irq, evtchn;
1309 for (virq = 0; virq < NR_VIRQS; virq++) {
1310 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1313 BUG_ON(virq_from_irq(irq) != virq);
1315 /* Get a new binding from Xen. */
1316 bind_virq.virq = virq;
1317 bind_virq.vcpu = cpu;
1318 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1321 evtchn = bind_virq.port;
1323 /* Record the new mapping. */
1324 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
1325 bind_evtchn_to_cpu(evtchn, cpu);
1329 static void restore_cpu_ipis(unsigned int cpu)
1331 struct evtchn_bind_ipi bind_ipi;
1332 int ipi, irq, evtchn;
1334 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1335 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1338 BUG_ON(ipi_from_irq(irq) != ipi);
1340 /* Get a new binding from Xen. */
1341 bind_ipi.vcpu = cpu;
1342 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1345 evtchn = bind_ipi.port;
1347 /* Record the new mapping. */
1348 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
1349 bind_evtchn_to_cpu(evtchn, cpu);
1353 /* Clear an irq's pending state, in preparation for polling on it */
1354 void xen_clear_irq_pending(int irq)
1356 int evtchn = evtchn_from_irq(irq);
1358 if (VALID_EVTCHN(evtchn))
1359 clear_evtchn(evtchn);
1361 EXPORT_SYMBOL(xen_clear_irq_pending);
1362 void xen_set_irq_pending(int irq)
1364 int evtchn = evtchn_from_irq(irq);
1366 if (VALID_EVTCHN(evtchn))
1370 bool xen_test_irq_pending(int irq)
1372 int evtchn = evtchn_from_irq(irq);
1375 if (VALID_EVTCHN(evtchn))
1376 ret = test_evtchn(evtchn);
1381 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1382 * the irq will be disabled so it won't deliver an interrupt. */
1383 void xen_poll_irq_timeout(int irq, u64 timeout)
1385 evtchn_port_t evtchn = evtchn_from_irq(irq);
1387 if (VALID_EVTCHN(evtchn)) {
1388 struct sched_poll poll;
1391 poll.timeout = timeout;
1392 set_xen_guest_handle(poll.ports, &evtchn);
1394 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1398 EXPORT_SYMBOL(xen_poll_irq_timeout);
1399 /* Poll waiting for an irq to become pending. In the usual case, the
1400 * irq will be disabled so it won't deliver an interrupt. */
1401 void xen_poll_irq(int irq)
1403 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1406 void xen_irq_resume(void)
1408 unsigned int cpu, evtchn;
1409 struct irq_info *info;
1411 init_evtchn_cpu_bindings();
1413 /* New event-channel space is not 'live' yet. */
1414 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1415 mask_evtchn(evtchn);
1417 /* No IRQ <-> event-channel mappings. */
1418 list_for_each_entry(info, &xen_irq_list_head, list)
1419 info->evtchn = 0; /* zap event-channel binding */
1421 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1422 evtchn_to_irq[evtchn] = -1;
1424 for_each_possible_cpu(cpu) {
1425 restore_cpu_virqs(cpu);
1426 restore_cpu_ipis(cpu);
1432 static struct irq_chip xen_dynamic_chip __read_mostly = {
1435 .irq_disable = disable_dynirq,
1436 .irq_mask = disable_dynirq,
1437 .irq_unmask = enable_dynirq,
1439 .irq_eoi = ack_dynirq,
1440 .irq_set_affinity = set_affinity_irq,
1441 .irq_retrigger = retrigger_dynirq,
1444 static struct irq_chip xen_pirq_chip __read_mostly = {
1447 .irq_startup = startup_pirq,
1448 .irq_shutdown = shutdown_pirq,
1450 .irq_enable = enable_pirq,
1451 .irq_unmask = enable_pirq,
1453 .irq_disable = disable_pirq,
1454 .irq_mask = disable_pirq,
1456 .irq_ack = ack_pirq,
1458 .irq_set_affinity = set_affinity_irq,
1460 .irq_retrigger = retrigger_dynirq,
1463 static struct irq_chip xen_percpu_chip __read_mostly = {
1464 .name = "xen-percpu",
1466 .irq_disable = disable_dynirq,
1467 .irq_mask = disable_dynirq,
1468 .irq_unmask = enable_dynirq,
1470 .irq_ack = ack_dynirq,
1473 int xen_set_callback_via(uint64_t via)
1475 struct xen_hvm_param a;
1476 a.domid = DOMID_SELF;
1477 a.index = HVM_PARAM_CALLBACK_IRQ;
1479 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1481 EXPORT_SYMBOL_GPL(xen_set_callback_via);
1483 #ifdef CONFIG_XEN_PVHVM
1484 /* Vector callbacks are better than PCI interrupts to receive event
1485 * channel notifications because we can receive vector callbacks on any
1486 * vcpu and we don't need PCI support or APIC interactions. */
1487 void xen_callback_vector(void)
1490 uint64_t callback_via;
1491 if (xen_have_vector_callback) {
1492 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1493 rc = xen_set_callback_via(callback_via);
1495 printk(KERN_ERR "Request for Xen HVM callback vector"
1497 xen_have_vector_callback = 0;
1500 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1502 /* in the restore case the vector has already been allocated */
1503 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1504 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1508 void xen_callback_vector(void) {}
1511 void __init xen_init_IRQ(void)
1515 /* We are using nr_irqs as the maximum number of pirq available but
1516 * that number is actually chosen by Xen and we don't know exactly
1517 * what it is. Be careful choosing high pirq numbers. */
1518 pirq_to_irq = kcalloc(nr_irqs, sizeof(*pirq_to_irq), GFP_KERNEL);
1519 for (i = 0; i < nr_irqs; i++)
1520 pirq_to_irq[i] = -1;
1522 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1524 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1525 evtchn_to_irq[i] = -1;
1527 init_evtchn_cpu_bindings();
1529 /* No event channels are 'live' right now. */
1530 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1533 if (xen_hvm_domain()) {
1534 xen_callback_vector();
1536 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1537 * __acpi_register_gsi can point at the right function */
1540 irq_ctx_init(smp_processor_id());
1541 if (xen_initial_domain())