4 * Xen models interrupts with abstract event channels. Because each
5 * domain gets 1024 event channels, but NR_IRQ is not that large, we
6 * must dynamically map irqs<->event channels. The event channels
7 * interface with the rest of the kernel by defining a xen interrupt
8 * chip. When an event is received, it is mapped to an irq and sent
9 * through the normal interrupt processing path.
11 * There are four kinds of events which can be mapped to an event
14 * 1. Inter-domain notifications. This includes all the virtual
15 * device events, since they're driven by front-ends in another domain
17 * 2. VIRQs, typically used for timers. These are per-cpu events.
19 * 4. PIRQs - Hardware interrupts.
21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
24 #include <linux/linkage.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/module.h>
28 #include <linux/string.h>
29 #include <linux/bootmem.h>
30 #include <linux/slab.h>
31 #include <linux/irqnr.h>
32 #include <linux/pci.h>
36 #include <asm/ptrace.h>
39 #include <asm/io_apic.h>
40 #include <asm/xen/page.h>
41 #include <asm/xen/pci.h>
43 #include <asm/sync_bitops.h>
44 #include <asm/xen/hypercall.h>
45 #include <asm/xen/hypervisor.h>
49 #include <xen/xen-ops.h>
50 #include <xen/events.h>
51 #include <xen/interface/xen.h>
52 #include <xen/interface/event_channel.h>
53 #include <xen/interface/hvm/hvm_op.h>
54 #include <xen/interface/hvm/params.h>
55 #include <xen/interface/physdev.h>
56 #include <xen/interface/sched.h>
57 #include <asm/hw_irq.h>
60 * This lock protects updates to the following mapping and reference-count
61 * arrays. The lock does not need to be acquired to read the mapping tables.
63 static DEFINE_MUTEX(irq_mapping_update_lock);
65 static LIST_HEAD(xen_irq_list_head);
67 /* IRQ <-> VIRQ mapping. */
68 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
70 /* IRQ <-> IPI mapping */
71 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
73 /* Interrupt types. */
83 * Packed IRQ information:
84 * type - enum xen_irq_type
85 * event channel - irq->event channel mapping
86 * cpu - cpu this event channel is bound to
87 * index - type-specific information:
88 * PIRQ - physical IRQ, GSI, flags, and owner domain
94 struct list_head list;
96 enum xen_irq_type type; /* type */
98 unsigned short evtchn; /* event channel */
99 unsigned short cpu; /* cpu bound */
112 #define PIRQ_NEEDS_EOI (1 << 0)
113 #define PIRQ_SHAREABLE (1 << 1)
115 static int *evtchn_to_irq;
117 static unsigned long *pirq_eoi_map;
119 static bool (*pirq_needs_eoi)(unsigned irq);
122 * Note sizeof(xen_ulong_t) can be more than sizeof(unsigned long). Be
123 * careful to only use bitops which allow for this (e.g
124 * test_bit/find_first_bit and friends but not __ffs) and to pass
125 * BITS_PER_EVTCHN_WORD as the bitmask length.
127 #define BITS_PER_EVTCHN_WORD (sizeof(xen_ulong_t)*8)
129 * Make a bitmask (i.e. unsigned long *) of a xen_ulong_t
130 * array. Primarily to avoid long lines (hence the terse name).
132 #define BM(x) (unsigned long *)(x)
133 /* Find the first set bit in a evtchn mask */
134 #define EVTCHN_FIRST_BIT(w) find_first_bit(BM(&(w)), BITS_PER_EVTCHN_WORD)
136 static DEFINE_PER_CPU(xen_ulong_t [NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD],
139 /* Xen will never allocate port zero for any purpose. */
140 #define VALID_EVTCHN(chn) ((chn) != 0)
142 static struct irq_chip xen_dynamic_chip;
143 static struct irq_chip xen_percpu_chip;
144 static struct irq_chip xen_pirq_chip;
145 static void enable_dynirq(struct irq_data *data);
146 static void disable_dynirq(struct irq_data *data);
148 /* Get info for IRQ */
149 static struct irq_info *info_for_irq(unsigned irq)
151 return irq_get_handler_data(irq);
154 /* Constructors for packed IRQ information. */
155 static void xen_irq_info_common_init(struct irq_info *info,
157 enum xen_irq_type type,
158 unsigned short evtchn,
162 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
166 info->evtchn = evtchn;
169 evtchn_to_irq[evtchn] = irq;
172 static void xen_irq_info_evtchn_init(unsigned irq,
173 unsigned short evtchn)
175 struct irq_info *info = info_for_irq(irq);
177 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
180 static void xen_irq_info_ipi_init(unsigned cpu,
182 unsigned short evtchn,
185 struct irq_info *info = info_for_irq(irq);
187 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
191 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
194 static void xen_irq_info_virq_init(unsigned cpu,
196 unsigned short evtchn,
199 struct irq_info *info = info_for_irq(irq);
201 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
205 per_cpu(virq_to_irq, cpu)[virq] = irq;
208 static void xen_irq_info_pirq_init(unsigned irq,
209 unsigned short evtchn,
215 struct irq_info *info = info_for_irq(irq);
217 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
219 info->u.pirq.pirq = pirq;
220 info->u.pirq.gsi = gsi;
221 info->u.pirq.domid = domid;
222 info->u.pirq.flags = flags;
226 * Accessors for packed IRQ information.
228 static unsigned int evtchn_from_irq(unsigned irq)
230 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
233 return info_for_irq(irq)->evtchn;
236 unsigned irq_from_evtchn(unsigned int evtchn)
238 return evtchn_to_irq[evtchn];
240 EXPORT_SYMBOL_GPL(irq_from_evtchn);
242 static enum ipi_vector ipi_from_irq(unsigned irq)
244 struct irq_info *info = info_for_irq(irq);
246 BUG_ON(info == NULL);
247 BUG_ON(info->type != IRQT_IPI);
252 static unsigned virq_from_irq(unsigned irq)
254 struct irq_info *info = info_for_irq(irq);
256 BUG_ON(info == NULL);
257 BUG_ON(info->type != IRQT_VIRQ);
262 static unsigned pirq_from_irq(unsigned irq)
264 struct irq_info *info = info_for_irq(irq);
266 BUG_ON(info == NULL);
267 BUG_ON(info->type != IRQT_PIRQ);
269 return info->u.pirq.pirq;
272 static enum xen_irq_type type_from_irq(unsigned irq)
274 return info_for_irq(irq)->type;
277 static unsigned cpu_from_irq(unsigned irq)
279 return info_for_irq(irq)->cpu;
282 static unsigned int cpu_from_evtchn(unsigned int evtchn)
284 int irq = evtchn_to_irq[evtchn];
288 ret = cpu_from_irq(irq);
294 static bool pirq_check_eoi_map(unsigned irq)
296 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
300 static bool pirq_needs_eoi_flag(unsigned irq)
302 struct irq_info *info = info_for_irq(irq);
303 BUG_ON(info->type != IRQT_PIRQ);
305 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
308 static inline xen_ulong_t active_evtchns(unsigned int cpu,
309 struct shared_info *sh,
312 return sh->evtchn_pending[idx] &
313 per_cpu(cpu_evtchn_mask, cpu)[idx] &
314 ~sh->evtchn_mask[idx];
317 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
319 int irq = evtchn_to_irq[chn];
323 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
326 clear_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu_from_irq(irq))));
327 set_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu)));
329 info_for_irq(irq)->cpu = cpu;
332 static void init_evtchn_cpu_bindings(void)
336 struct irq_info *info;
338 /* By default all event channels notify CPU#0. */
339 list_for_each_entry(info, &xen_irq_list_head, list) {
340 struct irq_desc *desc = irq_to_desc(info->irq);
341 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
345 for_each_possible_cpu(i)
346 memset(per_cpu(cpu_evtchn_mask, i),
347 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
350 static inline void clear_evtchn(int port)
352 struct shared_info *s = HYPERVISOR_shared_info;
353 sync_clear_bit(port, BM(&s->evtchn_pending[0]));
356 static inline void set_evtchn(int port)
358 struct shared_info *s = HYPERVISOR_shared_info;
359 sync_set_bit(port, BM(&s->evtchn_pending[0]));
362 static inline int test_evtchn(int port)
364 struct shared_info *s = HYPERVISOR_shared_info;
365 return sync_test_bit(port, BM(&s->evtchn_pending[0]));
370 * notify_remote_via_irq - send event to remote end of event channel via irq
371 * @irq: irq of event channel to send event to
373 * Unlike notify_remote_via_evtchn(), this is safe to use across
374 * save/restore. Notifications on a broken connection are silently
377 void notify_remote_via_irq(int irq)
379 int evtchn = evtchn_from_irq(irq);
381 if (VALID_EVTCHN(evtchn))
382 notify_remote_via_evtchn(evtchn);
384 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
386 static void mask_evtchn(int port)
388 struct shared_info *s = HYPERVISOR_shared_info;
389 sync_set_bit(port, BM(&s->evtchn_mask[0]));
392 static void unmask_evtchn(int port)
394 struct shared_info *s = HYPERVISOR_shared_info;
395 unsigned int cpu = get_cpu();
396 int do_hypercall = 0, evtchn_pending = 0;
398 BUG_ON(!irqs_disabled());
400 if (unlikely((cpu != cpu_from_evtchn(port))))
404 * Need to clear the mask before checking pending to
405 * avoid a race with an event becoming pending.
407 * EVTCHNOP_unmask will only trigger an upcall if the
408 * mask bit was set, so if a hypercall is needed
411 sync_clear_bit(port, BM(&s->evtchn_mask[0]));
412 evtchn_pending = sync_test_bit(port, BM(&s->evtchn_pending[0]));
414 if (unlikely(evtchn_pending && xen_hvm_domain())) {
415 sync_set_bit(port, BM(&s->evtchn_mask[0]));
420 /* Slow path (hypercall) if this is a non-local port or if this is
421 * an hvm domain and an event is pending (hvm domains don't have
422 * their own implementation of irq_enable). */
424 struct evtchn_unmask unmask = { .port = port };
425 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
427 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
430 * The following is basically the equivalent of
431 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
432 * the interrupt edge' if the channel is masked.
434 if (evtchn_pending &&
435 !sync_test_and_set_bit(port / BITS_PER_EVTCHN_WORD,
436 BM(&vcpu_info->evtchn_pending_sel)))
437 vcpu_info->evtchn_upcall_pending = 1;
443 static void xen_irq_init(unsigned irq)
445 struct irq_info *info;
447 struct irq_desc *desc = irq_to_desc(irq);
449 /* By default all event channels notify CPU#0. */
450 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
453 info = kzalloc(sizeof(*info), GFP_KERNEL);
455 panic("Unable to allocate metadata for IRQ%d\n", irq);
457 info->type = IRQT_UNBOUND;
460 irq_set_handler_data(irq, info);
462 list_add_tail(&info->list, &xen_irq_list_head);
465 static int __must_check xen_allocate_irq_dynamic(void)
470 #ifdef CONFIG_X86_IO_APIC
472 * For an HVM guest or domain 0 which see "real" (emulated or
473 * actual respectively) GSIs we allocate dynamic IRQs
474 * e.g. those corresponding to event channels or MSIs
475 * etc. from the range above those "real" GSIs to avoid
478 if (xen_initial_domain() || xen_hvm_domain())
479 first = get_nr_irqs_gsi();
482 irq = irq_alloc_desc_from(first, -1);
490 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
495 * A PV guest has no concept of a GSI (since it has no ACPI
496 * nor access to/knowledge of the physical APICs). Therefore
497 * all IRQs are dynamically allocated from the entire IRQ
500 if (xen_pv_domain() && !xen_initial_domain())
501 return xen_allocate_irq_dynamic();
503 /* Legacy IRQ descriptors are already allocated by the arch. */
504 if (gsi < NR_IRQS_LEGACY)
507 irq = irq_alloc_desc_at(gsi, -1);
514 static void xen_free_irq(unsigned irq)
516 struct irq_info *info = irq_get_handler_data(irq);
521 list_del(&info->list);
523 irq_set_handler_data(irq, NULL);
525 WARN_ON(info->refcnt > 0);
529 /* Legacy IRQ descriptors are managed by the arch. */
530 if (irq < NR_IRQS_LEGACY)
536 static void pirq_query_unmask(int irq)
538 struct physdev_irq_status_query irq_status;
539 struct irq_info *info = info_for_irq(irq);
541 BUG_ON(info->type != IRQT_PIRQ);
543 irq_status.irq = pirq_from_irq(irq);
544 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
545 irq_status.flags = 0;
547 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
548 if (irq_status.flags & XENIRQSTAT_needs_eoi)
549 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
552 static bool probing_irq(int irq)
554 struct irq_desc *desc = irq_to_desc(irq);
556 return desc && desc->action == NULL;
559 static void eoi_pirq(struct irq_data *data)
561 int evtchn = evtchn_from_irq(data->irq);
562 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
567 if (VALID_EVTCHN(evtchn))
568 clear_evtchn(evtchn);
570 if (pirq_needs_eoi(data->irq)) {
571 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
576 static void mask_ack_pirq(struct irq_data *data)
578 disable_dynirq(data);
582 static unsigned int __startup_pirq(unsigned int irq)
584 struct evtchn_bind_pirq bind_pirq;
585 struct irq_info *info = info_for_irq(irq);
586 int evtchn = evtchn_from_irq(irq);
589 BUG_ON(info->type != IRQT_PIRQ);
591 if (VALID_EVTCHN(evtchn))
594 bind_pirq.pirq = pirq_from_irq(irq);
595 /* NB. We are happy to share unless we are probing. */
596 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
597 BIND_PIRQ__WILL_SHARE : 0;
598 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
600 if (!probing_irq(irq))
601 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
605 evtchn = bind_pirq.port;
607 pirq_query_unmask(irq);
609 evtchn_to_irq[evtchn] = irq;
610 bind_evtchn_to_cpu(evtchn, 0);
611 info->evtchn = evtchn;
614 unmask_evtchn(evtchn);
615 eoi_pirq(irq_get_irq_data(irq));
620 static unsigned int startup_pirq(struct irq_data *data)
622 return __startup_pirq(data->irq);
625 static void shutdown_pirq(struct irq_data *data)
627 struct evtchn_close close;
628 unsigned int irq = data->irq;
629 struct irq_info *info = info_for_irq(irq);
630 int evtchn = evtchn_from_irq(irq);
632 BUG_ON(info->type != IRQT_PIRQ);
634 if (!VALID_EVTCHN(evtchn))
640 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
643 bind_evtchn_to_cpu(evtchn, 0);
644 evtchn_to_irq[evtchn] = -1;
648 static void enable_pirq(struct irq_data *data)
653 static void disable_pirq(struct irq_data *data)
655 disable_dynirq(data);
658 int xen_irq_from_gsi(unsigned gsi)
660 struct irq_info *info;
662 list_for_each_entry(info, &xen_irq_list_head, list) {
663 if (info->type != IRQT_PIRQ)
666 if (info->u.pirq.gsi == gsi)
672 EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
675 * Do not make any assumptions regarding the relationship between the
676 * IRQ number returned here and the Xen pirq argument.
678 * Note: We don't assign an event channel until the irq actually started
679 * up. Return an existing irq if we've already got one for the gsi.
681 * Shareable implies level triggered, not shareable implies edge
684 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
685 unsigned pirq, int shareable, char *name)
688 struct physdev_irq irq_op;
690 mutex_lock(&irq_mapping_update_lock);
692 irq = xen_irq_from_gsi(gsi);
694 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
699 irq = xen_allocate_irq_gsi(gsi);
706 /* Only the privileged domain can do this. For non-priv, the pcifront
707 * driver provides a PCI bus that does the call to do exactly
708 * this in the priv domain. */
709 if (xen_initial_domain() &&
710 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
716 xen_irq_info_pirq_init(irq, 0, pirq, gsi, DOMID_SELF,
717 shareable ? PIRQ_SHAREABLE : 0);
719 pirq_query_unmask(irq);
720 /* We try to use the handler with the appropriate semantic for the
721 * type of interrupt: if the interrupt is an edge triggered
722 * interrupt we use handle_edge_irq.
724 * On the other hand if the interrupt is level triggered we use
725 * handle_fasteoi_irq like the native code does for this kind of
728 * Depending on the Xen version, pirq_needs_eoi might return true
729 * not only for level triggered interrupts but for edge triggered
730 * interrupts too. In any case Xen always honors the eoi mechanism,
731 * not injecting any more pirqs of the same kind if the first one
732 * hasn't received an eoi yet. Therefore using the fasteoi handler
733 * is the right choice either way.
736 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
737 handle_fasteoi_irq, name);
739 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
740 handle_edge_irq, name);
743 mutex_unlock(&irq_mapping_update_lock);
748 #ifdef CONFIG_PCI_MSI
749 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
752 struct physdev_get_free_pirq op_get_free_pirq;
754 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
755 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
757 WARN_ONCE(rc == -ENOSYS,
758 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
760 return rc ? -1 : op_get_free_pirq.pirq;
763 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
764 int pirq, const char *name, domid_t domid)
768 mutex_lock(&irq_mapping_update_lock);
770 irq = xen_allocate_irq_dynamic();
774 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
777 xen_irq_info_pirq_init(irq, 0, pirq, 0, domid, 0);
778 ret = irq_set_msi_desc(irq, msidesc);
782 mutex_unlock(&irq_mapping_update_lock);
785 mutex_unlock(&irq_mapping_update_lock);
791 int xen_destroy_irq(int irq)
793 struct irq_desc *desc;
794 struct physdev_unmap_pirq unmap_irq;
795 struct irq_info *info = info_for_irq(irq);
798 mutex_lock(&irq_mapping_update_lock);
800 desc = irq_to_desc(irq);
804 if (xen_initial_domain()) {
805 unmap_irq.pirq = info->u.pirq.pirq;
806 unmap_irq.domid = info->u.pirq.domid;
807 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
808 /* If another domain quits without making the pci_disable_msix
809 * call, the Xen hypervisor takes care of freeing the PIRQs
810 * (free_domain_pirqs).
812 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
813 printk(KERN_INFO "domain %d does not have %d anymore\n",
814 info->u.pirq.domid, info->u.pirq.pirq);
816 printk(KERN_WARNING "unmap irq failed %d\n", rc);
824 mutex_unlock(&irq_mapping_update_lock);
828 int xen_irq_from_pirq(unsigned pirq)
832 struct irq_info *info;
834 mutex_lock(&irq_mapping_update_lock);
836 list_for_each_entry(info, &xen_irq_list_head, list) {
837 if (info->type != IRQT_PIRQ)
840 if (info->u.pirq.pirq == pirq)
845 mutex_unlock(&irq_mapping_update_lock);
851 int xen_pirq_from_irq(unsigned irq)
853 return pirq_from_irq(irq);
855 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
856 int bind_evtchn_to_irq(unsigned int evtchn)
860 mutex_lock(&irq_mapping_update_lock);
862 irq = evtchn_to_irq[evtchn];
865 irq = xen_allocate_irq_dynamic();
869 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
870 handle_edge_irq, "event");
872 xen_irq_info_evtchn_init(irq, evtchn);
874 struct irq_info *info = info_for_irq(irq);
875 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
877 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
880 mutex_unlock(&irq_mapping_update_lock);
884 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
886 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
888 struct evtchn_bind_ipi bind_ipi;
891 mutex_lock(&irq_mapping_update_lock);
893 irq = per_cpu(ipi_to_irq, cpu)[ipi];
896 irq = xen_allocate_irq_dynamic();
900 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
901 handle_percpu_irq, "ipi");
904 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
907 evtchn = bind_ipi.port;
909 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
911 bind_evtchn_to_cpu(evtchn, cpu);
913 struct irq_info *info = info_for_irq(irq);
914 WARN_ON(info == NULL || info->type != IRQT_IPI);
918 mutex_unlock(&irq_mapping_update_lock);
922 static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
923 unsigned int remote_port)
925 struct evtchn_bind_interdomain bind_interdomain;
928 bind_interdomain.remote_dom = remote_domain;
929 bind_interdomain.remote_port = remote_port;
931 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
934 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
937 static int find_virq(unsigned int virq, unsigned int cpu)
939 struct evtchn_status status;
940 int port, rc = -ENOENT;
942 memset(&status, 0, sizeof(status));
943 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
944 status.dom = DOMID_SELF;
946 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
949 if (status.status != EVTCHNSTAT_virq)
951 if (status.u.virq == virq && status.vcpu == cpu) {
959 int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
961 struct evtchn_bind_virq bind_virq;
962 int evtchn, irq, ret;
964 mutex_lock(&irq_mapping_update_lock);
966 irq = per_cpu(virq_to_irq, cpu)[virq];
969 irq = xen_allocate_irq_dynamic();
973 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
974 handle_percpu_irq, "virq");
976 bind_virq.virq = virq;
977 bind_virq.vcpu = cpu;
978 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
981 evtchn = bind_virq.port;
984 ret = find_virq(virq, cpu);
989 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
991 bind_evtchn_to_cpu(evtchn, cpu);
993 struct irq_info *info = info_for_irq(irq);
994 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
998 mutex_unlock(&irq_mapping_update_lock);
1003 static void unbind_from_irq(unsigned int irq)
1005 struct evtchn_close close;
1006 int evtchn = evtchn_from_irq(irq);
1007 struct irq_info *info = irq_get_handler_data(irq);
1012 mutex_lock(&irq_mapping_update_lock);
1014 if (info->refcnt > 0) {
1016 if (info->refcnt != 0)
1020 if (VALID_EVTCHN(evtchn)) {
1021 close.port = evtchn;
1022 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
1025 switch (type_from_irq(irq)) {
1027 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
1028 [virq_from_irq(irq)] = -1;
1031 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
1032 [ipi_from_irq(irq)] = -1;
1038 /* Closed ports are implicitly re-bound to VCPU0. */
1039 bind_evtchn_to_cpu(evtchn, 0);
1041 evtchn_to_irq[evtchn] = -1;
1044 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
1049 mutex_unlock(&irq_mapping_update_lock);
1052 int bind_evtchn_to_irqhandler(unsigned int evtchn,
1053 irq_handler_t handler,
1054 unsigned long irqflags,
1055 const char *devname, void *dev_id)
1059 irq = bind_evtchn_to_irq(evtchn);
1062 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1064 unbind_from_irq(irq);
1070 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1072 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1073 unsigned int remote_port,
1074 irq_handler_t handler,
1075 unsigned long irqflags,
1076 const char *devname,
1081 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1085 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1087 unbind_from_irq(irq);
1093 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1095 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1096 irq_handler_t handler,
1097 unsigned long irqflags, const char *devname, void *dev_id)
1101 irq = bind_virq_to_irq(virq, cpu);
1104 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1106 unbind_from_irq(irq);
1112 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1114 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1116 irq_handler_t handler,
1117 unsigned long irqflags,
1118 const char *devname,
1123 irq = bind_ipi_to_irq(ipi, cpu);
1127 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1128 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1130 unbind_from_irq(irq);
1137 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1139 struct irq_info *info = irq_get_handler_data(irq);
1143 free_irq(irq, dev_id);
1144 unbind_from_irq(irq);
1146 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1148 int evtchn_make_refcounted(unsigned int evtchn)
1150 int irq = evtchn_to_irq[evtchn];
1151 struct irq_info *info;
1156 info = irq_get_handler_data(irq);
1161 WARN_ON(info->refcnt != -1);
1167 EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1169 int evtchn_get(unsigned int evtchn)
1172 struct irq_info *info;
1175 if (evtchn >= NR_EVENT_CHANNELS)
1178 mutex_lock(&irq_mapping_update_lock);
1180 irq = evtchn_to_irq[evtchn];
1184 info = irq_get_handler_data(irq);
1190 if (info->refcnt <= 0)
1196 mutex_unlock(&irq_mapping_update_lock);
1200 EXPORT_SYMBOL_GPL(evtchn_get);
1202 void evtchn_put(unsigned int evtchn)
1204 int irq = evtchn_to_irq[evtchn];
1205 if (WARN_ON(irq == -1))
1207 unbind_from_irq(irq);
1209 EXPORT_SYMBOL_GPL(evtchn_put);
1211 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1213 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1215 notify_remote_via_irq(irq);
1218 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1220 struct shared_info *sh = HYPERVISOR_shared_info;
1221 int cpu = smp_processor_id();
1222 xen_ulong_t *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
1224 unsigned long flags;
1225 static DEFINE_SPINLOCK(debug_lock);
1226 struct vcpu_info *v;
1228 spin_lock_irqsave(&debug_lock, flags);
1230 printk("\nvcpu %d\n ", cpu);
1232 for_each_online_cpu(i) {
1234 v = per_cpu(xen_vcpu, i);
1235 pending = (get_irq_regs() && i == cpu)
1236 ? xen_irqs_disabled(get_irq_regs())
1237 : v->evtchn_upcall_mask;
1238 printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong"\n ", i,
1239 pending, v->evtchn_upcall_pending,
1240 (int)(sizeof(v->evtchn_pending_sel)*2),
1241 v->evtchn_pending_sel);
1243 v = per_cpu(xen_vcpu, cpu);
1245 printk("\npending:\n ");
1246 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1247 printk("%0*"PRI_xen_ulong"%s",
1248 (int)sizeof(sh->evtchn_pending[0])*2,
1249 sh->evtchn_pending[i],
1250 i % 8 == 0 ? "\n " : " ");
1251 printk("\nglobal mask:\n ");
1252 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1253 printk("%0*"PRI_xen_ulong"%s",
1254 (int)(sizeof(sh->evtchn_mask[0])*2),
1256 i % 8 == 0 ? "\n " : " ");
1258 printk("\nglobally unmasked:\n ");
1259 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1260 printk("%0*"PRI_xen_ulong"%s",
1261 (int)(sizeof(sh->evtchn_mask[0])*2),
1262 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1263 i % 8 == 0 ? "\n " : " ");
1265 printk("\nlocal cpu%d mask:\n ", cpu);
1266 for (i = (NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD)-1; i >= 0; i--)
1267 printk("%0*"PRI_xen_ulong"%s", (int)(sizeof(cpu_evtchn[0])*2),
1269 i % 8 == 0 ? "\n " : " ");
1271 printk("\nlocally unmasked:\n ");
1272 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1273 xen_ulong_t pending = sh->evtchn_pending[i]
1274 & ~sh->evtchn_mask[i]
1276 printk("%0*"PRI_xen_ulong"%s",
1277 (int)(sizeof(sh->evtchn_mask[0])*2),
1278 pending, i % 8 == 0 ? "\n " : " ");
1281 printk("\npending list:\n");
1282 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
1283 if (sync_test_bit(i, BM(sh->evtchn_pending))) {
1284 int word_idx = i / BITS_PER_EVTCHN_WORD;
1285 printk(" %d: event %d -> irq %d%s%s%s\n",
1286 cpu_from_evtchn(i), i,
1288 sync_test_bit(word_idx, BM(&v->evtchn_pending_sel))
1290 !sync_test_bit(i, BM(sh->evtchn_mask))
1291 ? "" : " globally-masked",
1292 sync_test_bit(i, BM(cpu_evtchn))
1293 ? "" : " locally-masked");
1297 spin_unlock_irqrestore(&debug_lock, flags);
1302 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1303 static DEFINE_PER_CPU(unsigned int, current_word_idx);
1304 static DEFINE_PER_CPU(unsigned int, current_bit_idx);
1307 * Mask out the i least significant bits of w
1309 #define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
1312 * Search the CPUs pending events bitmasks. For each one found, map
1313 * the event number to an irq, and feed it into do_IRQ() for
1316 * Xen uses a two-level bitmap to speed searching. The first level is
1317 * a bitset of words which contain pending event bits. The second
1318 * level is a bitset of pending events themselves.
1320 static void __xen_evtchn_do_upcall(void)
1322 int start_word_idx, start_bit_idx;
1323 int word_idx, bit_idx;
1325 int cpu = get_cpu();
1326 struct shared_info *s = HYPERVISOR_shared_info;
1327 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1331 xen_ulong_t pending_words;
1332 xen_ulong_t pending_bits;
1333 struct irq_desc *desc;
1335 vcpu_info->evtchn_upcall_pending = 0;
1337 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1341 * Master flag must be cleared /before/ clearing
1342 * selector flag. xchg_xen_ulong must contain an
1343 * appropriate barrier.
1345 if ((irq = per_cpu(virq_to_irq, cpu)[VIRQ_TIMER]) != -1) {
1346 int evtchn = evtchn_from_irq(irq);
1347 word_idx = evtchn / BITS_PER_LONG;
1348 pending_bits = evtchn % BITS_PER_LONG;
1349 if (active_evtchns(cpu, s, word_idx) & (1ULL << pending_bits)) {
1350 desc = irq_to_desc(irq);
1352 generic_handle_irq_desc(irq, desc);
1356 pending_words = xchg_xen_ulong(&vcpu_info->evtchn_pending_sel, 0);
1358 start_word_idx = __this_cpu_read(current_word_idx);
1359 start_bit_idx = __this_cpu_read(current_bit_idx);
1361 word_idx = start_word_idx;
1363 for (i = 0; pending_words != 0; i++) {
1366 words = MASK_LSBS(pending_words, word_idx);
1369 * If we masked out all events, wrap to beginning.
1376 word_idx = EVTCHN_FIRST_BIT(words);
1378 pending_bits = active_evtchns(cpu, s, word_idx);
1379 bit_idx = 0; /* usually scan entire word from start */
1380 if (word_idx == start_word_idx) {
1381 /* We scan the starting word in two parts */
1383 /* 1st time: start in the middle */
1384 bit_idx = start_bit_idx;
1386 /* 2nd time: mask bits done already */
1387 bit_idx &= (1UL << start_bit_idx) - 1;
1394 bits = MASK_LSBS(pending_bits, bit_idx);
1396 /* If we masked out all events, move on. */
1400 bit_idx = EVTCHN_FIRST_BIT(bits);
1403 port = (word_idx * BITS_PER_EVTCHN_WORD) + bit_idx;
1404 irq = evtchn_to_irq[port];
1407 desc = irq_to_desc(irq);
1409 generic_handle_irq_desc(irq, desc);
1412 bit_idx = (bit_idx + 1) % BITS_PER_EVTCHN_WORD;
1414 /* Next caller starts at last processed + 1 */
1415 __this_cpu_write(current_word_idx,
1416 bit_idx ? word_idx :
1417 (word_idx+1) % BITS_PER_EVTCHN_WORD);
1418 __this_cpu_write(current_bit_idx, bit_idx);
1419 } while (bit_idx != 0);
1421 /* Scan start_l1i twice; all others once. */
1422 if ((word_idx != start_word_idx) || (i != 0))
1423 pending_words &= ~(1UL << word_idx);
1425 word_idx = (word_idx + 1) % BITS_PER_EVTCHN_WORD;
1428 BUG_ON(!irqs_disabled());
1430 count = __this_cpu_read(xed_nesting_count);
1431 __this_cpu_write(xed_nesting_count, 0);
1432 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1439 void xen_evtchn_do_upcall(struct pt_regs *regs)
1441 struct pt_regs *old_regs = set_irq_regs(regs);
1448 __xen_evtchn_do_upcall();
1451 set_irq_regs(old_regs);
1454 void xen_hvm_evtchn_do_upcall(void)
1456 __xen_evtchn_do_upcall();
1458 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1460 /* Rebind a new event channel to an existing irq. */
1461 void rebind_evtchn_irq(int evtchn, int irq)
1463 struct irq_info *info = info_for_irq(irq);
1468 /* Make sure the irq is masked, since the new event channel
1469 will also be masked. */
1472 mutex_lock(&irq_mapping_update_lock);
1474 /* After resume the irq<->evtchn mappings are all cleared out */
1475 BUG_ON(evtchn_to_irq[evtchn] != -1);
1476 /* Expect irq to have been bound before,
1477 so there should be a proper type */
1478 BUG_ON(info->type == IRQT_UNBOUND);
1480 xen_irq_info_evtchn_init(irq, evtchn);
1482 mutex_unlock(&irq_mapping_update_lock);
1484 /* new event channels are always bound to cpu 0 */
1485 irq_set_affinity(irq, cpumask_of(0));
1487 /* Unmask the event channel. */
1491 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1492 static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
1494 struct evtchn_bind_vcpu bind_vcpu;
1495 int evtchn = evtchn_from_irq(irq);
1497 if (!VALID_EVTCHN(evtchn))
1501 * Events delivered via platform PCI interrupts are always
1502 * routed to vcpu 0 and hence cannot be rebound.
1504 if (xen_hvm_domain() && !xen_have_vector_callback)
1507 /* Send future instances of this interrupt to other vcpu. */
1508 bind_vcpu.port = evtchn;
1509 bind_vcpu.vcpu = tcpu;
1512 * If this fails, it usually just indicates that we're dealing with a
1513 * virq or IPI channel, which don't actually need to be rebound. Ignore
1514 * it, but don't do the xenlinux-level rebind in that case.
1516 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1517 bind_evtchn_to_cpu(evtchn, tcpu);
1522 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1525 unsigned tcpu = cpumask_first(dest);
1527 return rebind_irq_to_cpu(data->irq, tcpu);
1530 int resend_irq_on_evtchn(unsigned int irq)
1532 int masked, evtchn = evtchn_from_irq(irq);
1533 struct shared_info *s = HYPERVISOR_shared_info;
1535 if (!VALID_EVTCHN(evtchn))
1538 masked = sync_test_and_set_bit(evtchn, BM(s->evtchn_mask));
1539 sync_set_bit(evtchn, BM(s->evtchn_pending));
1541 unmask_evtchn(evtchn);
1546 static void enable_dynirq(struct irq_data *data)
1548 int evtchn = evtchn_from_irq(data->irq);
1550 if (VALID_EVTCHN(evtchn))
1551 unmask_evtchn(evtchn);
1554 static void disable_dynirq(struct irq_data *data)
1556 int evtchn = evtchn_from_irq(data->irq);
1558 if (VALID_EVTCHN(evtchn))
1559 mask_evtchn(evtchn);
1562 static void ack_dynirq(struct irq_data *data)
1564 int evtchn = evtchn_from_irq(data->irq);
1568 if (VALID_EVTCHN(evtchn))
1569 clear_evtchn(evtchn);
1572 static void mask_ack_dynirq(struct irq_data *data)
1574 disable_dynirq(data);
1578 static int retrigger_dynirq(struct irq_data *data)
1580 int evtchn = evtchn_from_irq(data->irq);
1581 struct shared_info *sh = HYPERVISOR_shared_info;
1584 if (VALID_EVTCHN(evtchn)) {
1587 masked = sync_test_and_set_bit(evtchn, BM(sh->evtchn_mask));
1588 sync_set_bit(evtchn, BM(sh->evtchn_pending));
1590 unmask_evtchn(evtchn);
1597 static void restore_pirqs(void)
1599 int pirq, rc, irq, gsi;
1600 struct physdev_map_pirq map_irq;
1601 struct irq_info *info;
1603 list_for_each_entry(info, &xen_irq_list_head, list) {
1604 if (info->type != IRQT_PIRQ)
1607 pirq = info->u.pirq.pirq;
1608 gsi = info->u.pirq.gsi;
1611 /* save/restore of PT devices doesn't work, so at this point the
1612 * only devices present are GSI based emulated devices */
1616 map_irq.domid = DOMID_SELF;
1617 map_irq.type = MAP_PIRQ_TYPE_GSI;
1618 map_irq.index = gsi;
1619 map_irq.pirq = pirq;
1621 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1623 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1624 gsi, irq, pirq, rc);
1629 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1631 __startup_pirq(irq);
1635 static void restore_cpu_virqs(unsigned int cpu)
1637 struct evtchn_bind_virq bind_virq;
1638 int virq, irq, evtchn;
1640 for (virq = 0; virq < NR_VIRQS; virq++) {
1641 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1644 BUG_ON(virq_from_irq(irq) != virq);
1646 /* Get a new binding from Xen. */
1647 bind_virq.virq = virq;
1648 bind_virq.vcpu = cpu;
1649 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1652 evtchn = bind_virq.port;
1654 /* Record the new mapping. */
1655 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
1656 bind_evtchn_to_cpu(evtchn, cpu);
1660 static void restore_cpu_ipis(unsigned int cpu)
1662 struct evtchn_bind_ipi bind_ipi;
1663 int ipi, irq, evtchn;
1665 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1666 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1669 BUG_ON(ipi_from_irq(irq) != ipi);
1671 /* Get a new binding from Xen. */
1672 bind_ipi.vcpu = cpu;
1673 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1676 evtchn = bind_ipi.port;
1678 /* Record the new mapping. */
1679 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
1680 bind_evtchn_to_cpu(evtchn, cpu);
1684 /* Clear an irq's pending state, in preparation for polling on it */
1685 void xen_clear_irq_pending(int irq)
1687 int evtchn = evtchn_from_irq(irq);
1689 if (VALID_EVTCHN(evtchn))
1690 clear_evtchn(evtchn);
1692 EXPORT_SYMBOL(xen_clear_irq_pending);
1693 void xen_set_irq_pending(int irq)
1695 int evtchn = evtchn_from_irq(irq);
1697 if (VALID_EVTCHN(evtchn))
1701 bool xen_test_irq_pending(int irq)
1703 int evtchn = evtchn_from_irq(irq);
1706 if (VALID_EVTCHN(evtchn))
1707 ret = test_evtchn(evtchn);
1712 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1713 * the irq will be disabled so it won't deliver an interrupt. */
1714 void xen_poll_irq_timeout(int irq, u64 timeout)
1716 evtchn_port_t evtchn = evtchn_from_irq(irq);
1718 if (VALID_EVTCHN(evtchn)) {
1719 struct sched_poll poll;
1722 poll.timeout = timeout;
1723 set_xen_guest_handle(poll.ports, &evtchn);
1725 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1729 EXPORT_SYMBOL(xen_poll_irq_timeout);
1730 /* Poll waiting for an irq to become pending. In the usual case, the
1731 * irq will be disabled so it won't deliver an interrupt. */
1732 void xen_poll_irq(int irq)
1734 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1737 /* Check whether the IRQ line is shared with other guests. */
1738 int xen_test_irq_shared(int irq)
1740 struct irq_info *info = info_for_irq(irq);
1741 struct physdev_irq_status_query irq_status;
1746 irq_status.irq = info->u.pirq.pirq;
1748 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1750 return !(irq_status.flags & XENIRQSTAT_shared);
1752 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1754 void xen_irq_resume(void)
1756 unsigned int cpu, evtchn;
1757 struct irq_info *info;
1759 init_evtchn_cpu_bindings();
1761 /* New event-channel space is not 'live' yet. */
1762 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1763 mask_evtchn(evtchn);
1765 /* No IRQ <-> event-channel mappings. */
1766 list_for_each_entry(info, &xen_irq_list_head, list)
1767 info->evtchn = 0; /* zap event-channel binding */
1769 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1770 evtchn_to_irq[evtchn] = -1;
1772 for_each_possible_cpu(cpu) {
1773 restore_cpu_virqs(cpu);
1774 restore_cpu_ipis(cpu);
1780 static struct irq_chip xen_dynamic_chip __read_mostly = {
1783 .irq_disable = disable_dynirq,
1784 .irq_mask = disable_dynirq,
1785 .irq_unmask = enable_dynirq,
1787 .irq_ack = ack_dynirq,
1788 .irq_mask_ack = mask_ack_dynirq,
1790 .irq_set_affinity = set_affinity_irq,
1791 .irq_retrigger = retrigger_dynirq,
1794 static struct irq_chip xen_pirq_chip __read_mostly = {
1797 .irq_startup = startup_pirq,
1798 .irq_shutdown = shutdown_pirq,
1799 .irq_enable = enable_pirq,
1800 .irq_disable = disable_pirq,
1802 .irq_mask = disable_dynirq,
1803 .irq_unmask = enable_dynirq,
1805 .irq_ack = eoi_pirq,
1806 .irq_eoi = eoi_pirq,
1807 .irq_mask_ack = mask_ack_pirq,
1809 .irq_set_affinity = set_affinity_irq,
1811 .irq_retrigger = retrigger_dynirq,
1814 static struct irq_chip xen_percpu_chip __read_mostly = {
1815 .name = "xen-percpu",
1817 .irq_disable = disable_dynirq,
1818 .irq_mask = disable_dynirq,
1819 .irq_unmask = enable_dynirq,
1821 .irq_ack = ack_dynirq,
1824 int xen_set_callback_via(uint64_t via)
1826 struct xen_hvm_param a;
1827 a.domid = DOMID_SELF;
1828 a.index = HVM_PARAM_CALLBACK_IRQ;
1830 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1832 EXPORT_SYMBOL_GPL(xen_set_callback_via);
1834 #ifdef CONFIG_XEN_PVHVM
1835 /* Vector callbacks are better than PCI interrupts to receive event
1836 * channel notifications because we can receive vector callbacks on any
1837 * vcpu and we don't need PCI support or APIC interactions. */
1838 void xen_callback_vector(void)
1841 uint64_t callback_via;
1842 if (xen_have_vector_callback) {
1843 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
1844 rc = xen_set_callback_via(callback_via);
1846 printk(KERN_ERR "Request for Xen HVM callback vector"
1848 xen_have_vector_callback = 0;
1851 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1853 /* in the restore case the vector has already been allocated */
1854 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
1855 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
1856 xen_hvm_callback_vector);
1860 void xen_callback_vector(void) {}
1863 void __init xen_init_IRQ(void)
1867 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1869 BUG_ON(!evtchn_to_irq);
1870 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1871 evtchn_to_irq[i] = -1;
1873 init_evtchn_cpu_bindings();
1875 /* No event channels are 'live' right now. */
1876 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1879 pirq_needs_eoi = pirq_needs_eoi_flag;
1882 if (xen_hvm_domain()) {
1883 xen_callback_vector();
1885 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1886 * __acpi_register_gsi can point at the right function */
1890 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1892 irq_ctx_init(smp_processor_id());
1893 if (xen_initial_domain())
1894 pci_xen_initial_domain();
1896 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1897 eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1898 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1900 free_page((unsigned long) pirq_eoi_map);
1901 pirq_eoi_map = NULL;
1903 pirq_needs_eoi = pirq_check_eoi_map;