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 /* IRQ <-> VIRQ mapping. */
60 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
62 /* IRQ <-> IPI mapping */
63 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
65 /* Interrupt types. */
75 * Packed IRQ information:
76 * type - enum xen_irq_type
77 * event channel - irq->event channel mapping
78 * cpu - cpu this event channel is bound to
79 * index - type-specific information:
80 * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
81 * guest, or GSI (real passthrough IRQ) of the device.
88 enum xen_irq_type type; /* type */
89 unsigned short evtchn; /* event channel */
90 unsigned short cpu; /* cpu bound */
103 #define PIRQ_NEEDS_EOI (1 << 0)
104 #define PIRQ_SHAREABLE (1 << 1)
106 static struct irq_info *irq_info;
107 static int *pirq_to_irq;
109 static int *evtchn_to_irq;
111 static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
114 /* Xen will never allocate port zero for any purpose. */
115 #define VALID_EVTCHN(chn) ((chn) != 0)
117 static struct irq_chip xen_dynamic_chip;
118 static struct irq_chip xen_percpu_chip;
119 static struct irq_chip xen_pirq_chip;
121 /* Get info for IRQ */
122 static struct irq_info *info_for_irq(unsigned irq)
124 return &irq_info[irq];
127 /* Constructors for packed IRQ information. */
128 static void xen_irq_info_common_init(struct irq_info *info,
129 enum xen_irq_type type,
130 unsigned short evtchn,
134 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
137 info->evtchn = evtchn;
141 static void xen_irq_info_evtchn_init(unsigned irq,
142 unsigned short evtchn)
144 struct irq_info *info = info_for_irq(irq);
146 xen_irq_info_common_init(info, IRQT_EVTCHN, evtchn, 0);
149 static void xen_irq_info_ipi_init(unsigned irq,
150 unsigned short evtchn,
153 struct irq_info *info = info_for_irq(irq);
155 xen_irq_info_common_init(info, IRQT_IPI, evtchn, 0);
160 static void xen_irq_info_virq_init(unsigned irq,
161 unsigned short evtchn,
164 struct irq_info *info = info_for_irq(irq);
166 xen_irq_info_common_init(info, IRQT_VIRQ, evtchn, 0);
171 static void xen_irq_info_pirq_init(unsigned irq,
172 unsigned short evtchn,
175 unsigned short vector,
178 struct irq_info *info = info_for_irq(irq);
180 xen_irq_info_common_init(info, IRQT_PIRQ, evtchn, 0);
182 info->u.pirq.pirq = pirq;
183 info->u.pirq.gsi = gsi;
184 info->u.pirq.vector = vector;
185 info->u.pirq.flags = flags;
189 * Accessors for packed IRQ information.
191 static unsigned int evtchn_from_irq(unsigned irq)
193 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
196 return info_for_irq(irq)->evtchn;
199 unsigned irq_from_evtchn(unsigned int evtchn)
201 return evtchn_to_irq[evtchn];
203 EXPORT_SYMBOL_GPL(irq_from_evtchn);
205 static enum ipi_vector ipi_from_irq(unsigned irq)
207 struct irq_info *info = info_for_irq(irq);
209 BUG_ON(info == NULL);
210 BUG_ON(info->type != IRQT_IPI);
215 static unsigned virq_from_irq(unsigned irq)
217 struct irq_info *info = info_for_irq(irq);
219 BUG_ON(info == NULL);
220 BUG_ON(info->type != IRQT_VIRQ);
225 static unsigned pirq_from_irq(unsigned irq)
227 struct irq_info *info = info_for_irq(irq);
229 BUG_ON(info == NULL);
230 BUG_ON(info->type != IRQT_PIRQ);
232 return info->u.pirq.pirq;
235 static unsigned gsi_from_irq(unsigned irq)
237 struct irq_info *info = info_for_irq(irq);
239 BUG_ON(info == NULL);
240 BUG_ON(info->type != IRQT_PIRQ);
242 return info->u.pirq.gsi;
245 static enum xen_irq_type type_from_irq(unsigned irq)
247 return info_for_irq(irq)->type;
250 static unsigned cpu_from_irq(unsigned irq)
252 return info_for_irq(irq)->cpu;
255 static unsigned int cpu_from_evtchn(unsigned int evtchn)
257 int irq = evtchn_to_irq[evtchn];
261 ret = cpu_from_irq(irq);
266 static bool pirq_needs_eoi(unsigned irq)
268 struct irq_info *info = info_for_irq(irq);
270 BUG_ON(info->type != IRQT_PIRQ);
272 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
275 static inline unsigned long active_evtchns(unsigned int cpu,
276 struct shared_info *sh,
279 return (sh->evtchn_pending[idx] &
280 per_cpu(cpu_evtchn_mask, cpu)[idx] &
281 ~sh->evtchn_mask[idx]);
284 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
286 int irq = evtchn_to_irq[chn];
290 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
293 clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
294 set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
296 irq_info[irq].cpu = cpu;
299 static void init_evtchn_cpu_bindings(void)
303 struct irq_desc *desc;
305 /* By default all event channels notify CPU#0. */
306 for_each_irq_desc(i, desc) {
307 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
311 for_each_possible_cpu(i)
312 memset(per_cpu(cpu_evtchn_mask, i),
313 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
317 static inline void clear_evtchn(int port)
319 struct shared_info *s = HYPERVISOR_shared_info;
320 sync_clear_bit(port, &s->evtchn_pending[0]);
323 static inline void set_evtchn(int port)
325 struct shared_info *s = HYPERVISOR_shared_info;
326 sync_set_bit(port, &s->evtchn_pending[0]);
329 static inline int test_evtchn(int port)
331 struct shared_info *s = HYPERVISOR_shared_info;
332 return sync_test_bit(port, &s->evtchn_pending[0]);
337 * notify_remote_via_irq - send event to remote end of event channel via irq
338 * @irq: irq of event channel to send event to
340 * Unlike notify_remote_via_evtchn(), this is safe to use across
341 * save/restore. Notifications on a broken connection are silently
344 void notify_remote_via_irq(int irq)
346 int evtchn = evtchn_from_irq(irq);
348 if (VALID_EVTCHN(evtchn))
349 notify_remote_via_evtchn(evtchn);
351 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
353 static void mask_evtchn(int port)
355 struct shared_info *s = HYPERVISOR_shared_info;
356 sync_set_bit(port, &s->evtchn_mask[0]);
359 static void unmask_evtchn(int port)
361 struct shared_info *s = HYPERVISOR_shared_info;
362 unsigned int cpu = get_cpu();
364 BUG_ON(!irqs_disabled());
366 /* Slow path (hypercall) if this is a non-local port. */
367 if (unlikely(cpu != cpu_from_evtchn(port))) {
368 struct evtchn_unmask unmask = { .port = port };
369 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
371 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
373 sync_clear_bit(port, &s->evtchn_mask[0]);
376 * The following is basically the equivalent of
377 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
378 * the interrupt edge' if the channel is masked.
380 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
381 !sync_test_and_set_bit(port / BITS_PER_LONG,
382 &vcpu_info->evtchn_pending_sel))
383 vcpu_info->evtchn_upcall_pending = 1;
389 static int xen_allocate_irq_dynamic(void)
394 #ifdef CONFIG_X86_IO_APIC
396 * For an HVM guest or domain 0 which see "real" (emulated or
397 * actual repectively) GSIs we allocate dynamic IRQs
398 * e.g. those corresponding to event channels or MSIs
399 * etc. from the range above those "real" GSIs to avoid
402 if (xen_initial_domain() || xen_hvm_domain())
403 first = get_nr_irqs_gsi();
407 irq = irq_alloc_desc_from(first, -1);
409 if (irq == -ENOMEM && first > NR_IRQS_LEGACY) {
410 printk(KERN_ERR "Out of dynamic IRQ space and eating into GSI space. You should increase nr_irqs\n");
411 first = max(NR_IRQS_LEGACY, first - NR_IRQS_LEGACY);
416 panic("No available IRQ to bind to: increase nr_irqs!\n");
421 static int xen_allocate_irq_gsi(unsigned gsi)
426 * A PV guest has no concept of a GSI (since it has no ACPI
427 * nor access to/knowledge of the physical APICs). Therefore
428 * all IRQs are dynamically allocated from the entire IRQ
431 if (xen_pv_domain() && !xen_initial_domain())
432 return xen_allocate_irq_dynamic();
434 /* Legacy IRQ descriptors are already allocated by the arch. */
435 if (gsi < NR_IRQS_LEGACY)
438 irq = irq_alloc_desc_at(gsi, -1);
440 panic("Unable to allocate to IRQ%d (%d)\n", gsi, irq);
445 static void xen_free_irq(unsigned irq)
447 irq_info[irq].type = IRQT_UNBOUND;
449 /* Legacy IRQ descriptors are managed by the arch. */
450 if (irq < NR_IRQS_LEGACY)
456 static void pirq_unmask_notify(int irq)
458 struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
460 if (unlikely(pirq_needs_eoi(irq))) {
461 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
466 static void pirq_query_unmask(int irq)
468 struct physdev_irq_status_query irq_status;
469 struct irq_info *info = info_for_irq(irq);
471 BUG_ON(info->type != IRQT_PIRQ);
473 irq_status.irq = pirq_from_irq(irq);
474 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
475 irq_status.flags = 0;
477 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
478 if (irq_status.flags & XENIRQSTAT_needs_eoi)
479 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
482 static bool probing_irq(int irq)
484 struct irq_desc *desc = irq_to_desc(irq);
486 return desc && desc->action == NULL;
489 static unsigned int __startup_pirq(unsigned int irq)
491 struct evtchn_bind_pirq bind_pirq;
492 struct irq_info *info = info_for_irq(irq);
493 int evtchn = evtchn_from_irq(irq);
496 BUG_ON(info->type != IRQT_PIRQ);
498 if (VALID_EVTCHN(evtchn))
501 bind_pirq.pirq = pirq_from_irq(irq);
502 /* NB. We are happy to share unless we are probing. */
503 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
504 BIND_PIRQ__WILL_SHARE : 0;
505 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
507 if (!probing_irq(irq))
508 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
512 evtchn = bind_pirq.port;
514 pirq_query_unmask(irq);
516 evtchn_to_irq[evtchn] = irq;
517 bind_evtchn_to_cpu(evtchn, 0);
518 info->evtchn = evtchn;
521 unmask_evtchn(evtchn);
522 pirq_unmask_notify(irq);
527 static unsigned int startup_pirq(struct irq_data *data)
529 return __startup_pirq(data->irq);
532 static void shutdown_pirq(struct irq_data *data)
534 struct evtchn_close close;
535 unsigned int irq = data->irq;
536 struct irq_info *info = info_for_irq(irq);
537 int evtchn = evtchn_from_irq(irq);
539 BUG_ON(info->type != IRQT_PIRQ);
541 if (!VALID_EVTCHN(evtchn))
547 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
550 bind_evtchn_to_cpu(evtchn, 0);
551 evtchn_to_irq[evtchn] = -1;
555 static void enable_pirq(struct irq_data *data)
560 static void disable_pirq(struct irq_data *data)
564 static void ack_pirq(struct irq_data *data)
566 int evtchn = evtchn_from_irq(data->irq);
568 move_native_irq(data->irq);
570 if (VALID_EVTCHN(evtchn)) {
572 clear_evtchn(evtchn);
576 static int find_irq_by_gsi(unsigned gsi)
580 for (irq = 0; irq < nr_irqs; irq++) {
581 struct irq_info *info = info_for_irq(irq);
583 if (info == NULL || info->type != IRQT_PIRQ)
586 if (gsi_from_irq(irq) == gsi)
593 int xen_allocate_pirq_gsi(unsigned gsi)
599 * Do not make any assumptions regarding the relationship between the
600 * IRQ number returned here and the Xen pirq argument.
602 * Note: We don't assign an event channel until the irq actually started
603 * up. Return an existing irq if we've already got one for the gsi.
605 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
606 unsigned pirq, int shareable, char *name)
609 struct physdev_irq irq_op;
611 spin_lock(&irq_mapping_update_lock);
613 if ((pirq > nr_irqs) || (gsi > nr_irqs)) {
614 printk(KERN_WARNING "xen_map_pirq_gsi: %s %s is incorrect!\n",
615 pirq > nr_irqs ? "pirq" :"",
616 gsi > nr_irqs ? "gsi" : "");
620 irq = find_irq_by_gsi(gsi);
622 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
624 goto out; /* XXX need refcount? */
627 irq = xen_allocate_irq_gsi(gsi);
629 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
630 handle_level_irq, name);
635 /* Only the privileged domain can do this. For non-priv, the pcifront
636 * driver provides a PCI bus that does the call to do exactly
637 * this in the priv domain. */
638 if (xen_initial_domain() &&
639 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
645 xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector,
646 shareable ? PIRQ_SHAREABLE : 0);
647 pirq_to_irq[pirq] = irq;
650 spin_unlock(&irq_mapping_update_lock);
655 #ifdef CONFIG_PCI_MSI
656 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
659 struct physdev_get_free_pirq op_get_free_pirq;
661 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
662 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
664 WARN_ONCE(rc == -ENOSYS,
665 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
667 return rc ? -1 : op_get_free_pirq.pirq;
670 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
671 int pirq, int vector, const char *name)
675 spin_lock(&irq_mapping_update_lock);
677 irq = xen_allocate_irq_dynamic();
681 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
682 handle_level_irq, name);
684 xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, 0);
685 pirq_to_irq[pirq] = irq;
686 ret = set_irq_msi(irq, msidesc);
690 spin_unlock(&irq_mapping_update_lock);
693 spin_unlock(&irq_mapping_update_lock);
699 int xen_destroy_irq(int irq)
701 struct irq_desc *desc;
702 struct physdev_unmap_pirq unmap_irq;
703 struct irq_info *info = info_for_irq(irq);
706 spin_lock(&irq_mapping_update_lock);
708 desc = irq_to_desc(irq);
712 if (xen_initial_domain()) {
713 unmap_irq.pirq = info->u.pirq.pirq;
714 unmap_irq.domid = DOMID_SELF;
715 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
717 printk(KERN_WARNING "unmap irq failed %d\n", rc);
721 pirq_to_irq[info->u.pirq.pirq] = -1;
726 spin_unlock(&irq_mapping_update_lock);
730 int xen_irq_from_pirq(unsigned pirq)
732 return pirq_to_irq[pirq];
735 int bind_evtchn_to_irq(unsigned int evtchn)
739 spin_lock(&irq_mapping_update_lock);
741 irq = evtchn_to_irq[evtchn];
744 irq = xen_allocate_irq_dynamic();
746 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
747 handle_fasteoi_irq, "event");
749 evtchn_to_irq[evtchn] = irq;
750 xen_irq_info_evtchn_init(irq, evtchn);
753 spin_unlock(&irq_mapping_update_lock);
757 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
759 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
761 struct evtchn_bind_ipi bind_ipi;
764 spin_lock(&irq_mapping_update_lock);
766 irq = per_cpu(ipi_to_irq, cpu)[ipi];
769 irq = xen_allocate_irq_dynamic();
773 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
774 handle_percpu_irq, "ipi");
777 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
780 evtchn = bind_ipi.port;
782 evtchn_to_irq[evtchn] = irq;
783 xen_irq_info_ipi_init(irq, evtchn, ipi);
784 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
786 bind_evtchn_to_cpu(evtchn, cpu);
790 spin_unlock(&irq_mapping_update_lock);
795 int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
797 struct evtchn_bind_virq bind_virq;
800 spin_lock(&irq_mapping_update_lock);
802 irq = per_cpu(virq_to_irq, cpu)[virq];
805 irq = xen_allocate_irq_dynamic();
807 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
808 handle_percpu_irq, "virq");
810 bind_virq.virq = virq;
811 bind_virq.vcpu = cpu;
812 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
815 evtchn = bind_virq.port;
817 evtchn_to_irq[evtchn] = irq;
818 xen_irq_info_virq_init(irq, evtchn, virq);
820 per_cpu(virq_to_irq, cpu)[virq] = irq;
822 bind_evtchn_to_cpu(evtchn, cpu);
825 spin_unlock(&irq_mapping_update_lock);
830 static void unbind_from_irq(unsigned int irq)
832 struct evtchn_close close;
833 int evtchn = evtchn_from_irq(irq);
835 spin_lock(&irq_mapping_update_lock);
837 if (VALID_EVTCHN(evtchn)) {
839 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
842 switch (type_from_irq(irq)) {
844 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
845 [virq_from_irq(irq)] = -1;
848 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
849 [ipi_from_irq(irq)] = -1;
855 /* Closed ports are implicitly re-bound to VCPU0. */
856 bind_evtchn_to_cpu(evtchn, 0);
858 evtchn_to_irq[evtchn] = -1;
861 BUG_ON(irq_info[irq].type == IRQT_UNBOUND);
865 spin_unlock(&irq_mapping_update_lock);
868 int bind_evtchn_to_irqhandler(unsigned int evtchn,
869 irq_handler_t handler,
870 unsigned long irqflags,
871 const char *devname, void *dev_id)
876 irq = bind_evtchn_to_irq(evtchn);
877 retval = request_irq(irq, handler, irqflags, devname, dev_id);
879 unbind_from_irq(irq);
885 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
887 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
888 irq_handler_t handler,
889 unsigned long irqflags, const char *devname, void *dev_id)
894 irq = bind_virq_to_irq(virq, cpu);
895 retval = request_irq(irq, handler, irqflags, devname, dev_id);
897 unbind_from_irq(irq);
903 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
905 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
907 irq_handler_t handler,
908 unsigned long irqflags,
914 irq = bind_ipi_to_irq(ipi, cpu);
918 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
919 retval = request_irq(irq, handler, irqflags, devname, dev_id);
921 unbind_from_irq(irq);
928 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
930 free_irq(irq, dev_id);
931 unbind_from_irq(irq);
933 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
935 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
937 int irq = per_cpu(ipi_to_irq, cpu)[vector];
939 notify_remote_via_irq(irq);
942 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
944 struct shared_info *sh = HYPERVISOR_shared_info;
945 int cpu = smp_processor_id();
946 unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
949 static DEFINE_SPINLOCK(debug_lock);
952 spin_lock_irqsave(&debug_lock, flags);
954 printk("\nvcpu %d\n ", cpu);
956 for_each_online_cpu(i) {
958 v = per_cpu(xen_vcpu, i);
959 pending = (get_irq_regs() && i == cpu)
960 ? xen_irqs_disabled(get_irq_regs())
961 : v->evtchn_upcall_mask;
962 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
963 pending, v->evtchn_upcall_pending,
964 (int)(sizeof(v->evtchn_pending_sel)*2),
965 v->evtchn_pending_sel);
967 v = per_cpu(xen_vcpu, cpu);
969 printk("\npending:\n ");
970 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
971 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
972 sh->evtchn_pending[i],
973 i % 8 == 0 ? "\n " : " ");
974 printk("\nglobal mask:\n ");
975 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
977 (int)(sizeof(sh->evtchn_mask[0])*2),
979 i % 8 == 0 ? "\n " : " ");
981 printk("\nglobally unmasked:\n ");
982 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
983 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
984 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
985 i % 8 == 0 ? "\n " : " ");
987 printk("\nlocal cpu%d mask:\n ", cpu);
988 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
989 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
991 i % 8 == 0 ? "\n " : " ");
993 printk("\nlocally unmasked:\n ");
994 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
995 unsigned long pending = sh->evtchn_pending[i]
996 & ~sh->evtchn_mask[i]
998 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
999 pending, i % 8 == 0 ? "\n " : " ");
1002 printk("\npending list:\n");
1003 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
1004 if (sync_test_bit(i, sh->evtchn_pending)) {
1005 int word_idx = i / BITS_PER_LONG;
1006 printk(" %d: event %d -> irq %d%s%s%s\n",
1007 cpu_from_evtchn(i), i,
1009 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1011 !sync_test_bit(i, sh->evtchn_mask)
1012 ? "" : " globally-masked",
1013 sync_test_bit(i, cpu_evtchn)
1014 ? "" : " locally-masked");
1018 spin_unlock_irqrestore(&debug_lock, flags);
1023 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1026 * Search the CPUs pending events bitmasks. For each one found, map
1027 * the event number to an irq, and feed it into do_IRQ() for
1030 * Xen uses a two-level bitmap to speed searching. The first level is
1031 * a bitset of words which contain pending event bits. The second
1032 * level is a bitset of pending events themselves.
1034 static void __xen_evtchn_do_upcall(void)
1036 int cpu = get_cpu();
1037 struct shared_info *s = HYPERVISOR_shared_info;
1038 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1042 unsigned long pending_words;
1044 vcpu_info->evtchn_upcall_pending = 0;
1046 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1049 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1050 /* Clear master flag /before/ clearing selector flag. */
1053 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
1054 while (pending_words != 0) {
1055 unsigned long pending_bits;
1056 int word_idx = __ffs(pending_words);
1057 pending_words &= ~(1UL << word_idx);
1059 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
1060 int bit_idx = __ffs(pending_bits);
1061 int port = (word_idx * BITS_PER_LONG) + bit_idx;
1062 int irq = evtchn_to_irq[port];
1063 struct irq_desc *desc;
1069 desc = irq_to_desc(irq);
1071 generic_handle_irq_desc(irq, desc);
1076 BUG_ON(!irqs_disabled());
1078 count = __this_cpu_read(xed_nesting_count);
1079 __this_cpu_write(xed_nesting_count, 0);
1080 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1087 void xen_evtchn_do_upcall(struct pt_regs *regs)
1089 struct pt_regs *old_regs = set_irq_regs(regs);
1094 __xen_evtchn_do_upcall();
1097 set_irq_regs(old_regs);
1100 void xen_hvm_evtchn_do_upcall(void)
1102 __xen_evtchn_do_upcall();
1104 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1106 /* Rebind a new event channel to an existing irq. */
1107 void rebind_evtchn_irq(int evtchn, int irq)
1109 struct irq_info *info = info_for_irq(irq);
1111 /* Make sure the irq is masked, since the new event channel
1112 will also be masked. */
1115 spin_lock(&irq_mapping_update_lock);
1117 /* After resume the irq<->evtchn mappings are all cleared out */
1118 BUG_ON(evtchn_to_irq[evtchn] != -1);
1119 /* Expect irq to have been bound before,
1120 so there should be a proper type */
1121 BUG_ON(info->type == IRQT_UNBOUND);
1123 evtchn_to_irq[evtchn] = irq;
1124 xen_irq_info_evtchn_init(irq, evtchn);
1126 spin_unlock(&irq_mapping_update_lock);
1128 /* new event channels are always bound to cpu 0 */
1129 irq_set_affinity(irq, cpumask_of(0));
1131 /* Unmask the event channel. */
1135 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1136 static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
1138 struct evtchn_bind_vcpu bind_vcpu;
1139 int evtchn = evtchn_from_irq(irq);
1141 if (!VALID_EVTCHN(evtchn))
1145 * Events delivered via platform PCI interrupts are always
1146 * routed to vcpu 0 and hence cannot be rebound.
1148 if (xen_hvm_domain() && !xen_have_vector_callback)
1151 /* Send future instances of this interrupt to other vcpu. */
1152 bind_vcpu.port = evtchn;
1153 bind_vcpu.vcpu = tcpu;
1156 * If this fails, it usually just indicates that we're dealing with a
1157 * virq or IPI channel, which don't actually need to be rebound. Ignore
1158 * it, but don't do the xenlinux-level rebind in that case.
1160 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1161 bind_evtchn_to_cpu(evtchn, tcpu);
1166 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1169 unsigned tcpu = cpumask_first(dest);
1171 return rebind_irq_to_cpu(data->irq, tcpu);
1174 int resend_irq_on_evtchn(unsigned int irq)
1176 int masked, evtchn = evtchn_from_irq(irq);
1177 struct shared_info *s = HYPERVISOR_shared_info;
1179 if (!VALID_EVTCHN(evtchn))
1182 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1183 sync_set_bit(evtchn, s->evtchn_pending);
1185 unmask_evtchn(evtchn);
1190 static void enable_dynirq(struct irq_data *data)
1192 int evtchn = evtchn_from_irq(data->irq);
1194 if (VALID_EVTCHN(evtchn))
1195 unmask_evtchn(evtchn);
1198 static void disable_dynirq(struct irq_data *data)
1200 int evtchn = evtchn_from_irq(data->irq);
1202 if (VALID_EVTCHN(evtchn))
1203 mask_evtchn(evtchn);
1206 static void ack_dynirq(struct irq_data *data)
1208 int evtchn = evtchn_from_irq(data->irq);
1210 move_masked_irq(data->irq);
1212 if (VALID_EVTCHN(evtchn))
1213 unmask_evtchn(evtchn);
1216 static int retrigger_dynirq(struct irq_data *data)
1218 int evtchn = evtchn_from_irq(data->irq);
1219 struct shared_info *sh = HYPERVISOR_shared_info;
1222 if (VALID_EVTCHN(evtchn)) {
1225 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1226 sync_set_bit(evtchn, sh->evtchn_pending);
1228 unmask_evtchn(evtchn);
1235 static void restore_pirqs(void)
1237 int pirq, rc, irq, gsi;
1238 struct physdev_map_pirq map_irq;
1240 for (pirq = 0; pirq < nr_irqs; pirq++) {
1241 irq = pirq_to_irq[pirq];
1245 /* save/restore of PT devices doesn't work, so at this point the
1246 * only devices present are GSI based emulated devices */
1247 gsi = gsi_from_irq(irq);
1251 map_irq.domid = DOMID_SELF;
1252 map_irq.type = MAP_PIRQ_TYPE_GSI;
1253 map_irq.index = gsi;
1254 map_irq.pirq = pirq;
1256 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1258 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1259 gsi, irq, pirq, rc);
1261 pirq_to_irq[pirq] = -1;
1265 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1267 __startup_pirq(irq);
1271 static void restore_cpu_virqs(unsigned int cpu)
1273 struct evtchn_bind_virq bind_virq;
1274 int virq, irq, evtchn;
1276 for (virq = 0; virq < NR_VIRQS; virq++) {
1277 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1280 BUG_ON(virq_from_irq(irq) != virq);
1282 /* Get a new binding from Xen. */
1283 bind_virq.virq = virq;
1284 bind_virq.vcpu = cpu;
1285 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1288 evtchn = bind_virq.port;
1290 /* Record the new mapping. */
1291 evtchn_to_irq[evtchn] = irq;
1292 xen_irq_info_virq_init(irq, evtchn, virq);
1293 bind_evtchn_to_cpu(evtchn, cpu);
1297 static void restore_cpu_ipis(unsigned int cpu)
1299 struct evtchn_bind_ipi bind_ipi;
1300 int ipi, irq, evtchn;
1302 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1303 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1306 BUG_ON(ipi_from_irq(irq) != ipi);
1308 /* Get a new binding from Xen. */
1309 bind_ipi.vcpu = cpu;
1310 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1313 evtchn = bind_ipi.port;
1315 /* Record the new mapping. */
1316 evtchn_to_irq[evtchn] = irq;
1317 xen_irq_info_ipi_init(irq, evtchn, ipi);
1318 bind_evtchn_to_cpu(evtchn, cpu);
1322 /* Clear an irq's pending state, in preparation for polling on it */
1323 void xen_clear_irq_pending(int irq)
1325 int evtchn = evtchn_from_irq(irq);
1327 if (VALID_EVTCHN(evtchn))
1328 clear_evtchn(evtchn);
1330 EXPORT_SYMBOL(xen_clear_irq_pending);
1331 void xen_set_irq_pending(int irq)
1333 int evtchn = evtchn_from_irq(irq);
1335 if (VALID_EVTCHN(evtchn))
1339 bool xen_test_irq_pending(int irq)
1341 int evtchn = evtchn_from_irq(irq);
1344 if (VALID_EVTCHN(evtchn))
1345 ret = test_evtchn(evtchn);
1350 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1351 * the irq will be disabled so it won't deliver an interrupt. */
1352 void xen_poll_irq_timeout(int irq, u64 timeout)
1354 evtchn_port_t evtchn = evtchn_from_irq(irq);
1356 if (VALID_EVTCHN(evtchn)) {
1357 struct sched_poll poll;
1360 poll.timeout = timeout;
1361 set_xen_guest_handle(poll.ports, &evtchn);
1363 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1367 EXPORT_SYMBOL(xen_poll_irq_timeout);
1368 /* Poll waiting for an irq to become pending. In the usual case, the
1369 * irq will be disabled so it won't deliver an interrupt. */
1370 void xen_poll_irq(int irq)
1372 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1375 void xen_irq_resume(void)
1377 unsigned int cpu, irq, evtchn;
1379 init_evtchn_cpu_bindings();
1381 /* New event-channel space is not 'live' yet. */
1382 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1383 mask_evtchn(evtchn);
1385 /* No IRQ <-> event-channel mappings. */
1386 for (irq = 0; irq < nr_irqs; irq++)
1387 irq_info[irq].evtchn = 0; /* zap event-channel binding */
1389 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1390 evtchn_to_irq[evtchn] = -1;
1392 for_each_possible_cpu(cpu) {
1393 restore_cpu_virqs(cpu);
1394 restore_cpu_ipis(cpu);
1400 static struct irq_chip xen_dynamic_chip __read_mostly = {
1403 .irq_disable = disable_dynirq,
1404 .irq_mask = disable_dynirq,
1405 .irq_unmask = enable_dynirq,
1407 .irq_eoi = ack_dynirq,
1408 .irq_set_affinity = set_affinity_irq,
1409 .irq_retrigger = retrigger_dynirq,
1412 static struct irq_chip xen_pirq_chip __read_mostly = {
1415 .irq_startup = startup_pirq,
1416 .irq_shutdown = shutdown_pirq,
1418 .irq_enable = enable_pirq,
1419 .irq_unmask = enable_pirq,
1421 .irq_disable = disable_pirq,
1422 .irq_mask = disable_pirq,
1424 .irq_ack = ack_pirq,
1426 .irq_set_affinity = set_affinity_irq,
1428 .irq_retrigger = retrigger_dynirq,
1431 static struct irq_chip xen_percpu_chip __read_mostly = {
1432 .name = "xen-percpu",
1434 .irq_disable = disable_dynirq,
1435 .irq_mask = disable_dynirq,
1436 .irq_unmask = enable_dynirq,
1438 .irq_ack = ack_dynirq,
1441 int xen_set_callback_via(uint64_t via)
1443 struct xen_hvm_param a;
1444 a.domid = DOMID_SELF;
1445 a.index = HVM_PARAM_CALLBACK_IRQ;
1447 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1449 EXPORT_SYMBOL_GPL(xen_set_callback_via);
1451 #ifdef CONFIG_XEN_PVHVM
1452 /* Vector callbacks are better than PCI interrupts to receive event
1453 * channel notifications because we can receive vector callbacks on any
1454 * vcpu and we don't need PCI support or APIC interactions. */
1455 void xen_callback_vector(void)
1458 uint64_t callback_via;
1459 if (xen_have_vector_callback) {
1460 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1461 rc = xen_set_callback_via(callback_via);
1463 printk(KERN_ERR "Request for Xen HVM callback vector"
1465 xen_have_vector_callback = 0;
1468 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1470 /* in the restore case the vector has already been allocated */
1471 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1472 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1476 void xen_callback_vector(void) {}
1479 void __init xen_init_IRQ(void)
1483 irq_info = kcalloc(nr_irqs, sizeof(*irq_info), GFP_KERNEL);
1485 /* We are using nr_irqs as the maximum number of pirq available but
1486 * that number is actually chosen by Xen and we don't know exactly
1487 * what it is. Be careful choosing high pirq numbers. */
1488 pirq_to_irq = kcalloc(nr_irqs, sizeof(*pirq_to_irq), GFP_KERNEL);
1489 for (i = 0; i < nr_irqs; i++)
1490 pirq_to_irq[i] = -1;
1492 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1494 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1495 evtchn_to_irq[i] = -1;
1497 init_evtchn_cpu_bindings();
1499 /* No event channels are 'live' right now. */
1500 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1503 if (xen_hvm_domain()) {
1504 xen_callback_vector();
1506 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1507 * __acpi_register_gsi can point at the right function */
1510 irq_ctx_init(smp_processor_id());
1511 if (xen_initial_domain())