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 - vector, with MSB being "needs EIO", or physical IRQ of the HVM
89 * guest, or GSI (real passthrough IRQ) of the device.
95 struct list_head list
;
97 enum xen_irq_type type
; /* type */
99 unsigned short evtchn
; /* event channel */
100 unsigned short cpu
; /* cpu bound */
108 unsigned char vector
;
114 #define PIRQ_NEEDS_EOI (1 << 0)
115 #define PIRQ_SHAREABLE (1 << 1)
117 static int *evtchn_to_irq
;
118 static unsigned long *pirq_eoi_map
;
119 static bool (*pirq_needs_eoi
)(unsigned irq
);
121 static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS
/BITS_PER_LONG
],
124 /* Xen will never allocate port zero for any purpose. */
125 #define VALID_EVTCHN(chn) ((chn) != 0)
127 static struct irq_chip xen_dynamic_chip
;
128 static struct irq_chip xen_percpu_chip
;
129 static struct irq_chip xen_pirq_chip
;
130 static void enable_dynirq(struct irq_data
*data
);
131 static void disable_dynirq(struct irq_data
*data
);
133 /* Get info for IRQ */
134 static struct irq_info
*info_for_irq(unsigned irq
)
136 return irq_get_handler_data(irq
);
139 /* Constructors for packed IRQ information. */
140 static void xen_irq_info_common_init(struct irq_info
*info
,
142 enum xen_irq_type type
,
143 unsigned short evtchn
,
147 BUG_ON(info
->type
!= IRQT_UNBOUND
&& info
->type
!= type
);
151 info
->evtchn
= evtchn
;
154 evtchn_to_irq
[evtchn
] = irq
;
157 static void xen_irq_info_evtchn_init(unsigned irq
,
158 unsigned short evtchn
)
160 struct irq_info
*info
= info_for_irq(irq
);
162 xen_irq_info_common_init(info
, irq
, IRQT_EVTCHN
, evtchn
, 0);
165 static void xen_irq_info_ipi_init(unsigned cpu
,
167 unsigned short evtchn
,
170 struct irq_info
*info
= info_for_irq(irq
);
172 xen_irq_info_common_init(info
, irq
, IRQT_IPI
, evtchn
, 0);
176 per_cpu(ipi_to_irq
, cpu
)[ipi
] = irq
;
179 static void xen_irq_info_virq_init(unsigned cpu
,
181 unsigned short evtchn
,
184 struct irq_info
*info
= info_for_irq(irq
);
186 xen_irq_info_common_init(info
, irq
, IRQT_VIRQ
, evtchn
, 0);
190 per_cpu(virq_to_irq
, cpu
)[virq
] = irq
;
193 static void xen_irq_info_pirq_init(unsigned irq
,
194 unsigned short evtchn
,
197 unsigned short vector
,
201 struct irq_info
*info
= info_for_irq(irq
);
203 xen_irq_info_common_init(info
, irq
, IRQT_PIRQ
, evtchn
, 0);
205 info
->u
.pirq
.pirq
= pirq
;
206 info
->u
.pirq
.gsi
= gsi
;
207 info
->u
.pirq
.vector
= vector
;
208 info
->u
.pirq
.domid
= domid
;
209 info
->u
.pirq
.flags
= flags
;
213 * Accessors for packed IRQ information.
215 static unsigned int evtchn_from_irq(unsigned irq
)
217 if (unlikely(WARN(irq
< 0 || irq
>= nr_irqs
, "Invalid irq %d!\n", irq
)))
220 return info_for_irq(irq
)->evtchn
;
223 unsigned irq_from_evtchn(unsigned int evtchn
)
225 return evtchn_to_irq
[evtchn
];
227 EXPORT_SYMBOL_GPL(irq_from_evtchn
);
229 static enum ipi_vector
ipi_from_irq(unsigned irq
)
231 struct irq_info
*info
= info_for_irq(irq
);
233 BUG_ON(info
== NULL
);
234 BUG_ON(info
->type
!= IRQT_IPI
);
239 static unsigned virq_from_irq(unsigned irq
)
241 struct irq_info
*info
= info_for_irq(irq
);
243 BUG_ON(info
== NULL
);
244 BUG_ON(info
->type
!= IRQT_VIRQ
);
249 static unsigned pirq_from_irq(unsigned irq
)
251 struct irq_info
*info
= info_for_irq(irq
);
253 BUG_ON(info
== NULL
);
254 BUG_ON(info
->type
!= IRQT_PIRQ
);
256 return info
->u
.pirq
.pirq
;
259 static enum xen_irq_type
type_from_irq(unsigned irq
)
261 return info_for_irq(irq
)->type
;
264 static unsigned cpu_from_irq(unsigned irq
)
266 return info_for_irq(irq
)->cpu
;
269 static unsigned int cpu_from_evtchn(unsigned int evtchn
)
271 int irq
= evtchn_to_irq
[evtchn
];
275 ret
= cpu_from_irq(irq
);
280 static bool pirq_check_eoi_map(unsigned irq
)
282 return test_bit(pirq_from_irq(irq
), pirq_eoi_map
);
285 static bool pirq_needs_eoi_flag(unsigned irq
)
287 struct irq_info
*info
= info_for_irq(irq
);
288 BUG_ON(info
->type
!= IRQT_PIRQ
);
290 return info
->u
.pirq
.flags
& PIRQ_NEEDS_EOI
;
293 static inline unsigned long active_evtchns(unsigned int cpu
,
294 struct shared_info
*sh
,
297 return sh
->evtchn_pending
[idx
] &
298 per_cpu(cpu_evtchn_mask
, cpu
)[idx
] &
299 ~sh
->evtchn_mask
[idx
];
302 static void bind_evtchn_to_cpu(unsigned int chn
, unsigned int cpu
)
304 int irq
= evtchn_to_irq
[chn
];
308 cpumask_copy(irq_to_desc(irq
)->irq_data
.affinity
, cpumask_of(cpu
));
311 clear_bit(chn
, per_cpu(cpu_evtchn_mask
, cpu_from_irq(irq
)));
312 set_bit(chn
, per_cpu(cpu_evtchn_mask
, cpu
));
314 info_for_irq(irq
)->cpu
= cpu
;
317 static void init_evtchn_cpu_bindings(void)
321 struct irq_info
*info
;
323 /* By default all event channels notify CPU#0. */
324 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
325 struct irq_desc
*desc
= irq_to_desc(info
->irq
);
326 cpumask_copy(desc
->irq_data
.affinity
, cpumask_of(0));
330 for_each_possible_cpu(i
)
331 memset(per_cpu(cpu_evtchn_mask
, i
),
332 (i
== 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask
, i
)));
335 static inline void clear_evtchn(int port
)
337 struct shared_info
*s
= HYPERVISOR_shared_info
;
338 sync_clear_bit(port
, &s
->evtchn_pending
[0]);
341 static inline void set_evtchn(int port
)
343 struct shared_info
*s
= HYPERVISOR_shared_info
;
344 sync_set_bit(port
, &s
->evtchn_pending
[0]);
347 static inline int test_evtchn(int port
)
349 struct shared_info
*s
= HYPERVISOR_shared_info
;
350 return sync_test_bit(port
, &s
->evtchn_pending
[0]);
355 * notify_remote_via_irq - send event to remote end of event channel via irq
356 * @irq: irq of event channel to send event to
358 * Unlike notify_remote_via_evtchn(), this is safe to use across
359 * save/restore. Notifications on a broken connection are silently
362 void notify_remote_via_irq(int irq
)
364 int evtchn
= evtchn_from_irq(irq
);
366 if (VALID_EVTCHN(evtchn
))
367 notify_remote_via_evtchn(evtchn
);
369 EXPORT_SYMBOL_GPL(notify_remote_via_irq
);
371 static void mask_evtchn(int port
)
373 struct shared_info
*s
= HYPERVISOR_shared_info
;
374 sync_set_bit(port
, &s
->evtchn_mask
[0]);
377 static void unmask_evtchn(int port
)
379 struct shared_info
*s
= HYPERVISOR_shared_info
;
380 unsigned int cpu
= get_cpu();
381 int do_hypercall
= 0, evtchn_pending
= 0;
383 BUG_ON(!irqs_disabled());
385 if (unlikely((cpu
!= cpu_from_evtchn(port
))))
388 evtchn_pending
= sync_test_bit(port
, &s
->evtchn_pending
[0]);
390 if (unlikely(evtchn_pending
&& xen_hvm_domain()))
393 /* Slow path (hypercall) if this is a non-local port or if this is
394 * an hvm domain and an event is pending (hvm domains don't have
395 * their own implementation of irq_enable). */
397 struct evtchn_unmask unmask
= { .port
= port
};
398 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask
, &unmask
);
400 struct vcpu_info
*vcpu_info
= __this_cpu_read(xen_vcpu
);
402 sync_clear_bit(port
, &s
->evtchn_mask
[0]);
405 * The following is basically the equivalent of
406 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
407 * the interrupt edge' if the channel is masked.
409 if (evtchn_pending
&&
410 !sync_test_and_set_bit(port
/ BITS_PER_LONG
,
411 &vcpu_info
->evtchn_pending_sel
))
412 vcpu_info
->evtchn_upcall_pending
= 1;
418 static void xen_irq_init(unsigned irq
)
420 struct irq_info
*info
;
422 struct irq_desc
*desc
= irq_to_desc(irq
);
424 /* By default all event channels notify CPU#0. */
425 cpumask_copy(desc
->irq_data
.affinity
, cpumask_of(0));
428 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
430 panic("Unable to allocate metadata for IRQ%d\n", irq
);
432 info
->type
= IRQT_UNBOUND
;
435 irq_set_handler_data(irq
, info
);
437 list_add_tail(&info
->list
, &xen_irq_list_head
);
440 static int __must_check
xen_allocate_irq_dynamic(void)
445 #ifdef CONFIG_X86_IO_APIC
447 * For an HVM guest or domain 0 which see "real" (emulated or
448 * actual respectively) GSIs we allocate dynamic IRQs
449 * e.g. those corresponding to event channels or MSIs
450 * etc. from the range above those "real" GSIs to avoid
453 if (xen_initial_domain() || xen_hvm_domain())
454 first
= get_nr_irqs_gsi();
457 irq
= irq_alloc_desc_from(first
, -1);
465 static int __must_check
xen_allocate_irq_gsi(unsigned gsi
)
470 * A PV guest has no concept of a GSI (since it has no ACPI
471 * nor access to/knowledge of the physical APICs). Therefore
472 * all IRQs are dynamically allocated from the entire IRQ
475 if (xen_pv_domain() && !xen_initial_domain())
476 return xen_allocate_irq_dynamic();
478 /* Legacy IRQ descriptors are already allocated by the arch. */
479 if (gsi
< NR_IRQS_LEGACY
)
482 irq
= irq_alloc_desc_at(gsi
, -1);
489 static void xen_free_irq(unsigned irq
)
491 struct irq_info
*info
= irq_get_handler_data(irq
);
493 list_del(&info
->list
);
495 irq_set_handler_data(irq
, NULL
);
497 WARN_ON(info
->refcnt
> 0);
501 /* Legacy IRQ descriptors are managed by the arch. */
502 if (irq
< NR_IRQS_LEGACY
)
508 static void pirq_query_unmask(int irq
)
510 struct physdev_irq_status_query irq_status
;
511 struct irq_info
*info
= info_for_irq(irq
);
513 BUG_ON(info
->type
!= IRQT_PIRQ
);
515 irq_status
.irq
= pirq_from_irq(irq
);
516 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query
, &irq_status
))
517 irq_status
.flags
= 0;
519 info
->u
.pirq
.flags
&= ~PIRQ_NEEDS_EOI
;
520 if (irq_status
.flags
& XENIRQSTAT_needs_eoi
)
521 info
->u
.pirq
.flags
|= PIRQ_NEEDS_EOI
;
524 static bool probing_irq(int irq
)
526 struct irq_desc
*desc
= irq_to_desc(irq
);
528 return desc
&& desc
->action
== NULL
;
531 static void eoi_pirq(struct irq_data
*data
)
533 int evtchn
= evtchn_from_irq(data
->irq
);
534 struct physdev_eoi eoi
= { .irq
= pirq_from_irq(data
->irq
) };
539 if (VALID_EVTCHN(evtchn
))
540 clear_evtchn(evtchn
);
542 if (pirq_needs_eoi(data
->irq
)) {
543 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_eoi
, &eoi
);
548 static void mask_ack_pirq(struct irq_data
*data
)
550 disable_dynirq(data
);
554 static unsigned int __startup_pirq(unsigned int irq
)
556 struct evtchn_bind_pirq bind_pirq
;
557 struct irq_info
*info
= info_for_irq(irq
);
558 int evtchn
= evtchn_from_irq(irq
);
561 BUG_ON(info
->type
!= IRQT_PIRQ
);
563 if (VALID_EVTCHN(evtchn
))
566 bind_pirq
.pirq
= pirq_from_irq(irq
);
567 /* NB. We are happy to share unless we are probing. */
568 bind_pirq
.flags
= info
->u
.pirq
.flags
& PIRQ_SHAREABLE
?
569 BIND_PIRQ__WILL_SHARE
: 0;
570 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq
, &bind_pirq
);
572 if (!probing_irq(irq
))
573 printk(KERN_INFO
"Failed to obtain physical IRQ %d\n",
577 evtchn
= bind_pirq
.port
;
579 pirq_query_unmask(irq
);
581 evtchn_to_irq
[evtchn
] = irq
;
582 bind_evtchn_to_cpu(evtchn
, 0);
583 info
->evtchn
= evtchn
;
586 unmask_evtchn(evtchn
);
587 eoi_pirq(irq_get_irq_data(irq
));
592 static unsigned int startup_pirq(struct irq_data
*data
)
594 return __startup_pirq(data
->irq
);
597 static void shutdown_pirq(struct irq_data
*data
)
599 struct evtchn_close close
;
600 unsigned int irq
= data
->irq
;
601 struct irq_info
*info
= info_for_irq(irq
);
602 int evtchn
= evtchn_from_irq(irq
);
604 BUG_ON(info
->type
!= IRQT_PIRQ
);
606 if (!VALID_EVTCHN(evtchn
))
612 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
615 bind_evtchn_to_cpu(evtchn
, 0);
616 evtchn_to_irq
[evtchn
] = -1;
620 static void enable_pirq(struct irq_data
*data
)
625 static void disable_pirq(struct irq_data
*data
)
627 disable_dynirq(data
);
630 int xen_irq_from_gsi(unsigned gsi
)
632 struct irq_info
*info
;
634 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
635 if (info
->type
!= IRQT_PIRQ
)
638 if (info
->u
.pirq
.gsi
== gsi
)
644 EXPORT_SYMBOL_GPL(xen_irq_from_gsi
);
647 * Do not make any assumptions regarding the relationship between the
648 * IRQ number returned here and the Xen pirq argument.
650 * Note: We don't assign an event channel until the irq actually started
651 * up. Return an existing irq if we've already got one for the gsi.
653 * Shareable implies level triggered, not shareable implies edge
656 int xen_bind_pirq_gsi_to_irq(unsigned gsi
,
657 unsigned pirq
, int shareable
, char *name
)
660 struct physdev_irq irq_op
;
662 mutex_lock(&irq_mapping_update_lock
);
664 irq
= xen_irq_from_gsi(gsi
);
666 printk(KERN_INFO
"xen_map_pirq_gsi: returning irq %d for gsi %u\n",
671 irq
= xen_allocate_irq_gsi(gsi
);
678 /* Only the privileged domain can do this. For non-priv, the pcifront
679 * driver provides a PCI bus that does the call to do exactly
680 * this in the priv domain. */
681 if (xen_initial_domain() &&
682 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector
, &irq_op
)) {
688 xen_irq_info_pirq_init(irq
, 0, pirq
, gsi
, irq_op
.vector
, DOMID_SELF
,
689 shareable
? PIRQ_SHAREABLE
: 0);
691 pirq_query_unmask(irq
);
692 /* We try to use the handler with the appropriate semantic for the
693 * type of interrupt: if the interrupt is an edge triggered
694 * interrupt we use handle_edge_irq.
696 * On the other hand if the interrupt is level triggered we use
697 * handle_fasteoi_irq like the native code does for this kind of
700 * Depending on the Xen version, pirq_needs_eoi might return true
701 * not only for level triggered interrupts but for edge triggered
702 * interrupts too. In any case Xen always honors the eoi mechanism,
703 * not injecting any more pirqs of the same kind if the first one
704 * hasn't received an eoi yet. Therefore using the fasteoi handler
705 * is the right choice either way.
708 irq_set_chip_and_handler_name(irq
, &xen_pirq_chip
,
709 handle_fasteoi_irq
, name
);
711 irq_set_chip_and_handler_name(irq
, &xen_pirq_chip
,
712 handle_edge_irq
, name
);
715 mutex_unlock(&irq_mapping_update_lock
);
720 #ifdef CONFIG_PCI_MSI
721 int xen_allocate_pirq_msi(struct pci_dev
*dev
, struct msi_desc
*msidesc
)
724 struct physdev_get_free_pirq op_get_free_pirq
;
726 op_get_free_pirq
.type
= MAP_PIRQ_TYPE_MSI
;
727 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq
, &op_get_free_pirq
);
729 WARN_ONCE(rc
== -ENOSYS
,
730 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
732 return rc
? -1 : op_get_free_pirq
.pirq
;
735 int xen_bind_pirq_msi_to_irq(struct pci_dev
*dev
, struct msi_desc
*msidesc
,
736 int pirq
, int vector
, const char *name
,
741 mutex_lock(&irq_mapping_update_lock
);
743 irq
= xen_allocate_irq_dynamic();
747 irq_set_chip_and_handler_name(irq
, &xen_pirq_chip
, handle_edge_irq
,
750 xen_irq_info_pirq_init(irq
, 0, pirq
, 0, vector
, domid
, 0);
751 ret
= irq_set_msi_desc(irq
, msidesc
);
755 mutex_unlock(&irq_mapping_update_lock
);
758 mutex_unlock(&irq_mapping_update_lock
);
764 int xen_destroy_irq(int irq
)
766 struct irq_desc
*desc
;
767 struct physdev_unmap_pirq unmap_irq
;
768 struct irq_info
*info
= info_for_irq(irq
);
771 mutex_lock(&irq_mapping_update_lock
);
773 desc
= irq_to_desc(irq
);
777 if (xen_initial_domain()) {
778 unmap_irq
.pirq
= info
->u
.pirq
.pirq
;
779 unmap_irq
.domid
= info
->u
.pirq
.domid
;
780 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq
, &unmap_irq
);
781 /* If another domain quits without making the pci_disable_msix
782 * call, the Xen hypervisor takes care of freeing the PIRQs
783 * (free_domain_pirqs).
785 if ((rc
== -ESRCH
&& info
->u
.pirq
.domid
!= DOMID_SELF
))
786 printk(KERN_INFO
"domain %d does not have %d anymore\n",
787 info
->u
.pirq
.domid
, info
->u
.pirq
.pirq
);
789 printk(KERN_WARNING
"unmap irq failed %d\n", rc
);
797 mutex_unlock(&irq_mapping_update_lock
);
801 int xen_irq_from_pirq(unsigned pirq
)
805 struct irq_info
*info
;
807 mutex_lock(&irq_mapping_update_lock
);
809 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
810 if (info
->type
!= IRQT_PIRQ
)
813 if (info
->u
.pirq
.pirq
== pirq
)
818 mutex_unlock(&irq_mapping_update_lock
);
824 int xen_pirq_from_irq(unsigned irq
)
826 return pirq_from_irq(irq
);
828 EXPORT_SYMBOL_GPL(xen_pirq_from_irq
);
829 int bind_evtchn_to_irq(unsigned int evtchn
)
833 mutex_lock(&irq_mapping_update_lock
);
835 irq
= evtchn_to_irq
[evtchn
];
838 irq
= xen_allocate_irq_dynamic();
842 irq_set_chip_and_handler_name(irq
, &xen_dynamic_chip
,
843 handle_edge_irq
, "event");
845 xen_irq_info_evtchn_init(irq
, evtchn
);
847 struct irq_info
*info
= info_for_irq(irq
);
848 WARN_ON(info
== NULL
|| info
->type
!= IRQT_EVTCHN
);
850 irq_clear_status_flags(irq
, IRQ_NOREQUEST
|IRQ_NOAUTOEN
);
853 mutex_unlock(&irq_mapping_update_lock
);
857 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq
);
859 static int bind_ipi_to_irq(unsigned int ipi
, unsigned int cpu
)
861 struct evtchn_bind_ipi bind_ipi
;
864 mutex_lock(&irq_mapping_update_lock
);
866 irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
];
869 irq
= xen_allocate_irq_dynamic();
873 irq_set_chip_and_handler_name(irq
, &xen_percpu_chip
,
874 handle_percpu_irq
, "ipi");
877 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
880 evtchn
= bind_ipi
.port
;
882 xen_irq_info_ipi_init(cpu
, irq
, evtchn
, ipi
);
884 bind_evtchn_to_cpu(evtchn
, cpu
);
886 struct irq_info
*info
= info_for_irq(irq
);
887 WARN_ON(info
== NULL
|| info
->type
!= IRQT_IPI
);
891 mutex_unlock(&irq_mapping_update_lock
);
895 static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain
,
896 unsigned int remote_port
)
898 struct evtchn_bind_interdomain bind_interdomain
;
901 bind_interdomain
.remote_dom
= remote_domain
;
902 bind_interdomain
.remote_port
= remote_port
;
904 err
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain
,
907 return err
? : bind_evtchn_to_irq(bind_interdomain
.local_port
);
910 static int find_virq(unsigned int virq
, unsigned int cpu
)
912 struct evtchn_status status
;
913 int port
, rc
= -ENOENT
;
915 memset(&status
, 0, sizeof(status
));
916 for (port
= 0; port
<= NR_EVENT_CHANNELS
; port
++) {
917 status
.dom
= DOMID_SELF
;
919 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_status
, &status
);
922 if (status
.status
!= EVTCHNSTAT_virq
)
924 if (status
.u
.virq
== virq
&& status
.vcpu
== cpu
) {
932 int bind_virq_to_irq(unsigned int virq
, unsigned int cpu
)
934 struct evtchn_bind_virq bind_virq
;
935 int evtchn
, irq
, ret
;
937 mutex_lock(&irq_mapping_update_lock
);
939 irq
= per_cpu(virq_to_irq
, cpu
)[virq
];
942 irq
= xen_allocate_irq_dynamic();
946 irq_set_chip_and_handler_name(irq
, &xen_percpu_chip
,
947 handle_percpu_irq
, "virq");
949 bind_virq
.virq
= virq
;
950 bind_virq
.vcpu
= cpu
;
951 ret
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
954 evtchn
= bind_virq
.port
;
957 ret
= find_virq(virq
, cpu
);
962 xen_irq_info_virq_init(cpu
, irq
, evtchn
, virq
);
964 bind_evtchn_to_cpu(evtchn
, cpu
);
966 struct irq_info
*info
= info_for_irq(irq
);
967 WARN_ON(info
== NULL
|| info
->type
!= IRQT_VIRQ
);
971 mutex_unlock(&irq_mapping_update_lock
);
976 static void unbind_from_irq(unsigned int irq
)
978 struct evtchn_close close
;
979 int evtchn
= evtchn_from_irq(irq
);
980 struct irq_info
*info
= irq_get_handler_data(irq
);
982 mutex_lock(&irq_mapping_update_lock
);
984 if (info
->refcnt
> 0) {
986 if (info
->refcnt
!= 0)
990 if (VALID_EVTCHN(evtchn
)) {
992 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
995 switch (type_from_irq(irq
)) {
997 per_cpu(virq_to_irq
, cpu_from_evtchn(evtchn
))
998 [virq_from_irq(irq
)] = -1;
1001 per_cpu(ipi_to_irq
, cpu_from_evtchn(evtchn
))
1002 [ipi_from_irq(irq
)] = -1;
1008 /* Closed ports are implicitly re-bound to VCPU0. */
1009 bind_evtchn_to_cpu(evtchn
, 0);
1011 evtchn_to_irq
[evtchn
] = -1;
1014 BUG_ON(info_for_irq(irq
)->type
== IRQT_UNBOUND
);
1019 mutex_unlock(&irq_mapping_update_lock
);
1022 int bind_evtchn_to_irqhandler(unsigned int evtchn
,
1023 irq_handler_t handler
,
1024 unsigned long irqflags
,
1025 const char *devname
, void *dev_id
)
1029 irq
= bind_evtchn_to_irq(evtchn
);
1032 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1034 unbind_from_irq(irq
);
1040 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler
);
1042 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain
,
1043 unsigned int remote_port
,
1044 irq_handler_t handler
,
1045 unsigned long irqflags
,
1046 const char *devname
,
1051 irq
= bind_interdomain_evtchn_to_irq(remote_domain
, remote_port
);
1055 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1057 unbind_from_irq(irq
);
1063 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler
);
1065 int bind_virq_to_irqhandler(unsigned int virq
, unsigned int cpu
,
1066 irq_handler_t handler
,
1067 unsigned long irqflags
, const char *devname
, void *dev_id
)
1071 irq
= bind_virq_to_irq(virq
, cpu
);
1074 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1076 unbind_from_irq(irq
);
1082 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler
);
1084 int bind_ipi_to_irqhandler(enum ipi_vector ipi
,
1086 irq_handler_t handler
,
1087 unsigned long irqflags
,
1088 const char *devname
,
1093 irq
= bind_ipi_to_irq(ipi
, cpu
);
1097 irqflags
|= IRQF_NO_SUSPEND
| IRQF_FORCE_RESUME
| IRQF_EARLY_RESUME
;
1098 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1100 unbind_from_irq(irq
);
1107 void unbind_from_irqhandler(unsigned int irq
, void *dev_id
)
1109 free_irq(irq
, dev_id
);
1110 unbind_from_irq(irq
);
1112 EXPORT_SYMBOL_GPL(unbind_from_irqhandler
);
1114 int evtchn_make_refcounted(unsigned int evtchn
)
1116 int irq
= evtchn_to_irq
[evtchn
];
1117 struct irq_info
*info
;
1122 info
= irq_get_handler_data(irq
);
1127 WARN_ON(info
->refcnt
!= -1);
1133 EXPORT_SYMBOL_GPL(evtchn_make_refcounted
);
1135 int evtchn_get(unsigned int evtchn
)
1138 struct irq_info
*info
;
1141 if (evtchn
>= NR_EVENT_CHANNELS
)
1144 mutex_lock(&irq_mapping_update_lock
);
1146 irq
= evtchn_to_irq
[evtchn
];
1150 info
= irq_get_handler_data(irq
);
1156 if (info
->refcnt
<= 0)
1162 mutex_unlock(&irq_mapping_update_lock
);
1166 EXPORT_SYMBOL_GPL(evtchn_get
);
1168 void evtchn_put(unsigned int evtchn
)
1170 int irq
= evtchn_to_irq
[evtchn
];
1171 if (WARN_ON(irq
== -1))
1173 unbind_from_irq(irq
);
1175 EXPORT_SYMBOL_GPL(evtchn_put
);
1177 void xen_send_IPI_one(unsigned int cpu
, enum ipi_vector vector
)
1179 int irq
= per_cpu(ipi_to_irq
, cpu
)[vector
];
1181 notify_remote_via_irq(irq
);
1184 irqreturn_t
xen_debug_interrupt(int irq
, void *dev_id
)
1186 struct shared_info
*sh
= HYPERVISOR_shared_info
;
1187 int cpu
= smp_processor_id();
1188 unsigned long *cpu_evtchn
= per_cpu(cpu_evtchn_mask
, cpu
);
1190 unsigned long flags
;
1191 static DEFINE_SPINLOCK(debug_lock
);
1192 struct vcpu_info
*v
;
1194 spin_lock_irqsave(&debug_lock
, flags
);
1196 printk("\nvcpu %d\n ", cpu
);
1198 for_each_online_cpu(i
) {
1200 v
= per_cpu(xen_vcpu
, i
);
1201 pending
= (get_irq_regs() && i
== cpu
)
1202 ? xen_irqs_disabled(get_irq_regs())
1203 : v
->evtchn_upcall_mask
;
1204 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i
,
1205 pending
, v
->evtchn_upcall_pending
,
1206 (int)(sizeof(v
->evtchn_pending_sel
)*2),
1207 v
->evtchn_pending_sel
);
1209 v
= per_cpu(xen_vcpu
, cpu
);
1211 printk("\npending:\n ");
1212 for (i
= ARRAY_SIZE(sh
->evtchn_pending
)-1; i
>= 0; i
--)
1213 printk("%0*lx%s", (int)sizeof(sh
->evtchn_pending
[0])*2,
1214 sh
->evtchn_pending
[i
],
1215 i
% 8 == 0 ? "\n " : " ");
1216 printk("\nglobal mask:\n ");
1217 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
1219 (int)(sizeof(sh
->evtchn_mask
[0])*2),
1221 i
% 8 == 0 ? "\n " : " ");
1223 printk("\nglobally unmasked:\n ");
1224 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
1225 printk("%0*lx%s", (int)(sizeof(sh
->evtchn_mask
[0])*2),
1226 sh
->evtchn_pending
[i
] & ~sh
->evtchn_mask
[i
],
1227 i
% 8 == 0 ? "\n " : " ");
1229 printk("\nlocal cpu%d mask:\n ", cpu
);
1230 for (i
= (NR_EVENT_CHANNELS
/BITS_PER_LONG
)-1; i
>= 0; i
--)
1231 printk("%0*lx%s", (int)(sizeof(cpu_evtchn
[0])*2),
1233 i
% 8 == 0 ? "\n " : " ");
1235 printk("\nlocally unmasked:\n ");
1236 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--) {
1237 unsigned long pending
= sh
->evtchn_pending
[i
]
1238 & ~sh
->evtchn_mask
[i
]
1240 printk("%0*lx%s", (int)(sizeof(sh
->evtchn_mask
[0])*2),
1241 pending
, i
% 8 == 0 ? "\n " : " ");
1244 printk("\npending list:\n");
1245 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++) {
1246 if (sync_test_bit(i
, sh
->evtchn_pending
)) {
1247 int word_idx
= i
/ BITS_PER_LONG
;
1248 printk(" %d: event %d -> irq %d%s%s%s\n",
1249 cpu_from_evtchn(i
), i
,
1251 sync_test_bit(word_idx
, &v
->evtchn_pending_sel
)
1253 !sync_test_bit(i
, sh
->evtchn_mask
)
1254 ? "" : " globally-masked",
1255 sync_test_bit(i
, cpu_evtchn
)
1256 ? "" : " locally-masked");
1260 spin_unlock_irqrestore(&debug_lock
, flags
);
1265 static DEFINE_PER_CPU(unsigned, xed_nesting_count
);
1266 static DEFINE_PER_CPU(unsigned int, current_word_idx
);
1267 static DEFINE_PER_CPU(unsigned int, current_bit_idx
);
1270 * Mask out the i least significant bits of w
1272 #define MASK_LSBS(w, i) (w & ((~0UL) << i))
1275 * Search the CPUs pending events bitmasks. For each one found, map
1276 * the event number to an irq, and feed it into do_IRQ() for
1279 * Xen uses a two-level bitmap to speed searching. The first level is
1280 * a bitset of words which contain pending event bits. The second
1281 * level is a bitset of pending events themselves.
1283 static void __xen_evtchn_do_upcall(void)
1285 int start_word_idx
, start_bit_idx
;
1286 int word_idx
, bit_idx
;
1288 int cpu
= get_cpu();
1289 struct shared_info
*s
= HYPERVISOR_shared_info
;
1290 struct vcpu_info
*vcpu_info
= __this_cpu_read(xen_vcpu
);
1294 unsigned long pending_words
;
1296 vcpu_info
->evtchn_upcall_pending
= 0;
1298 if (__this_cpu_inc_return(xed_nesting_count
) - 1)
1301 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1302 /* Clear master flag /before/ clearing selector flag. */
1305 pending_words
= xchg(&vcpu_info
->evtchn_pending_sel
, 0);
1307 start_word_idx
= __this_cpu_read(current_word_idx
);
1308 start_bit_idx
= __this_cpu_read(current_bit_idx
);
1310 word_idx
= start_word_idx
;
1312 for (i
= 0; pending_words
!= 0; i
++) {
1313 unsigned long pending_bits
;
1314 unsigned long words
;
1316 words
= MASK_LSBS(pending_words
, word_idx
);
1319 * If we masked out all events, wrap to beginning.
1326 word_idx
= __ffs(words
);
1328 pending_bits
= active_evtchns(cpu
, s
, word_idx
);
1329 bit_idx
= 0; /* usually scan entire word from start */
1330 if (word_idx
== start_word_idx
) {
1331 /* We scan the starting word in two parts */
1333 /* 1st time: start in the middle */
1334 bit_idx
= start_bit_idx
;
1336 /* 2nd time: mask bits done already */
1337 bit_idx
&= (1UL << start_bit_idx
) - 1;
1343 struct irq_desc
*desc
;
1345 bits
= MASK_LSBS(pending_bits
, bit_idx
);
1347 /* If we masked out all events, move on. */
1351 bit_idx
= __ffs(bits
);
1354 port
= (word_idx
* BITS_PER_LONG
) + bit_idx
;
1355 irq
= evtchn_to_irq
[port
];
1358 desc
= irq_to_desc(irq
);
1360 generic_handle_irq_desc(irq
, desc
);
1363 bit_idx
= (bit_idx
+ 1) % BITS_PER_LONG
;
1365 /* Next caller starts at last processed + 1 */
1366 __this_cpu_write(current_word_idx
,
1367 bit_idx
? word_idx
:
1368 (word_idx
+1) % BITS_PER_LONG
);
1369 __this_cpu_write(current_bit_idx
, bit_idx
);
1370 } while (bit_idx
!= 0);
1372 /* Scan start_l1i twice; all others once. */
1373 if ((word_idx
!= start_word_idx
) || (i
!= 0))
1374 pending_words
&= ~(1UL << word_idx
);
1376 word_idx
= (word_idx
+ 1) % BITS_PER_LONG
;
1379 BUG_ON(!irqs_disabled());
1381 count
= __this_cpu_read(xed_nesting_count
);
1382 __this_cpu_write(xed_nesting_count
, 0);
1383 } while (count
!= 1 || vcpu_info
->evtchn_upcall_pending
);
1390 void xen_evtchn_do_upcall(struct pt_regs
*regs
)
1392 struct pt_regs
*old_regs
= set_irq_regs(regs
);
1399 __xen_evtchn_do_upcall();
1402 set_irq_regs(old_regs
);
1405 void xen_hvm_evtchn_do_upcall(void)
1407 __xen_evtchn_do_upcall();
1409 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall
);
1411 /* Rebind a new event channel to an existing irq. */
1412 void rebind_evtchn_irq(int evtchn
, int irq
)
1414 struct irq_info
*info
= info_for_irq(irq
);
1416 /* Make sure the irq is masked, since the new event channel
1417 will also be masked. */
1420 mutex_lock(&irq_mapping_update_lock
);
1422 /* After resume the irq<->evtchn mappings are all cleared out */
1423 BUG_ON(evtchn_to_irq
[evtchn
] != -1);
1424 /* Expect irq to have been bound before,
1425 so there should be a proper type */
1426 BUG_ON(info
->type
== IRQT_UNBOUND
);
1428 xen_irq_info_evtchn_init(irq
, evtchn
);
1430 mutex_unlock(&irq_mapping_update_lock
);
1432 /* new event channels are always bound to cpu 0 */
1433 irq_set_affinity(irq
, cpumask_of(0));
1435 /* Unmask the event channel. */
1439 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1440 static int rebind_irq_to_cpu(unsigned irq
, unsigned tcpu
)
1442 struct evtchn_bind_vcpu bind_vcpu
;
1443 int evtchn
= evtchn_from_irq(irq
);
1445 if (!VALID_EVTCHN(evtchn
))
1449 * Events delivered via platform PCI interrupts are always
1450 * routed to vcpu 0 and hence cannot be rebound.
1452 if (xen_hvm_domain() && !xen_have_vector_callback
)
1455 /* Send future instances of this interrupt to other vcpu. */
1456 bind_vcpu
.port
= evtchn
;
1457 bind_vcpu
.vcpu
= tcpu
;
1460 * If this fails, it usually just indicates that we're dealing with a
1461 * virq or IPI channel, which don't actually need to be rebound. Ignore
1462 * it, but don't do the xenlinux-level rebind in that case.
1464 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu
, &bind_vcpu
) >= 0)
1465 bind_evtchn_to_cpu(evtchn
, tcpu
);
1470 static int set_affinity_irq(struct irq_data
*data
, const struct cpumask
*dest
,
1473 unsigned tcpu
= cpumask_first(dest
);
1475 return rebind_irq_to_cpu(data
->irq
, tcpu
);
1478 int resend_irq_on_evtchn(unsigned int irq
)
1480 int masked
, evtchn
= evtchn_from_irq(irq
);
1481 struct shared_info
*s
= HYPERVISOR_shared_info
;
1483 if (!VALID_EVTCHN(evtchn
))
1486 masked
= sync_test_and_set_bit(evtchn
, s
->evtchn_mask
);
1487 sync_set_bit(evtchn
, s
->evtchn_pending
);
1489 unmask_evtchn(evtchn
);
1494 static void enable_dynirq(struct irq_data
*data
)
1496 int evtchn
= evtchn_from_irq(data
->irq
);
1498 if (VALID_EVTCHN(evtchn
))
1499 unmask_evtchn(evtchn
);
1502 static void disable_dynirq(struct irq_data
*data
)
1504 int evtchn
= evtchn_from_irq(data
->irq
);
1506 if (VALID_EVTCHN(evtchn
))
1507 mask_evtchn(evtchn
);
1510 static void ack_dynirq(struct irq_data
*data
)
1512 int evtchn
= evtchn_from_irq(data
->irq
);
1516 if (VALID_EVTCHN(evtchn
))
1517 clear_evtchn(evtchn
);
1520 static void mask_ack_dynirq(struct irq_data
*data
)
1522 disable_dynirq(data
);
1526 static int retrigger_dynirq(struct irq_data
*data
)
1528 int evtchn
= evtchn_from_irq(data
->irq
);
1529 struct shared_info
*sh
= HYPERVISOR_shared_info
;
1532 if (VALID_EVTCHN(evtchn
)) {
1535 masked
= sync_test_and_set_bit(evtchn
, sh
->evtchn_mask
);
1536 sync_set_bit(evtchn
, sh
->evtchn_pending
);
1538 unmask_evtchn(evtchn
);
1545 static void restore_pirqs(void)
1547 int pirq
, rc
, irq
, gsi
;
1548 struct physdev_map_pirq map_irq
;
1549 struct irq_info
*info
;
1551 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
1552 if (info
->type
!= IRQT_PIRQ
)
1555 pirq
= info
->u
.pirq
.pirq
;
1556 gsi
= info
->u
.pirq
.gsi
;
1559 /* save/restore of PT devices doesn't work, so at this point the
1560 * only devices present are GSI based emulated devices */
1564 map_irq
.domid
= DOMID_SELF
;
1565 map_irq
.type
= MAP_PIRQ_TYPE_GSI
;
1566 map_irq
.index
= gsi
;
1567 map_irq
.pirq
= pirq
;
1569 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq
, &map_irq
);
1571 printk(KERN_WARNING
"xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1572 gsi
, irq
, pirq
, rc
);
1577 printk(KERN_DEBUG
"xen: --> irq=%d, pirq=%d\n", irq
, map_irq
.pirq
);
1579 __startup_pirq(irq
);
1583 static void restore_cpu_virqs(unsigned int cpu
)
1585 struct evtchn_bind_virq bind_virq
;
1586 int virq
, irq
, evtchn
;
1588 for (virq
= 0; virq
< NR_VIRQS
; virq
++) {
1589 if ((irq
= per_cpu(virq_to_irq
, cpu
)[virq
]) == -1)
1592 BUG_ON(virq_from_irq(irq
) != virq
);
1594 /* Get a new binding from Xen. */
1595 bind_virq
.virq
= virq
;
1596 bind_virq
.vcpu
= cpu
;
1597 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
1600 evtchn
= bind_virq
.port
;
1602 /* Record the new mapping. */
1603 xen_irq_info_virq_init(cpu
, irq
, evtchn
, virq
);
1604 bind_evtchn_to_cpu(evtchn
, cpu
);
1608 static void restore_cpu_ipis(unsigned int cpu
)
1610 struct evtchn_bind_ipi bind_ipi
;
1611 int ipi
, irq
, evtchn
;
1613 for (ipi
= 0; ipi
< XEN_NR_IPIS
; ipi
++) {
1614 if ((irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
]) == -1)
1617 BUG_ON(ipi_from_irq(irq
) != ipi
);
1619 /* Get a new binding from Xen. */
1620 bind_ipi
.vcpu
= cpu
;
1621 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
1624 evtchn
= bind_ipi
.port
;
1626 /* Record the new mapping. */
1627 xen_irq_info_ipi_init(cpu
, irq
, evtchn
, ipi
);
1628 bind_evtchn_to_cpu(evtchn
, cpu
);
1632 /* Clear an irq's pending state, in preparation for polling on it */
1633 void xen_clear_irq_pending(int irq
)
1635 int evtchn
= evtchn_from_irq(irq
);
1637 if (VALID_EVTCHN(evtchn
))
1638 clear_evtchn(evtchn
);
1640 EXPORT_SYMBOL(xen_clear_irq_pending
);
1641 void xen_set_irq_pending(int irq
)
1643 int evtchn
= evtchn_from_irq(irq
);
1645 if (VALID_EVTCHN(evtchn
))
1649 bool xen_test_irq_pending(int irq
)
1651 int evtchn
= evtchn_from_irq(irq
);
1654 if (VALID_EVTCHN(evtchn
))
1655 ret
= test_evtchn(evtchn
);
1660 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1661 * the irq will be disabled so it won't deliver an interrupt. */
1662 void xen_poll_irq_timeout(int irq
, u64 timeout
)
1664 evtchn_port_t evtchn
= evtchn_from_irq(irq
);
1666 if (VALID_EVTCHN(evtchn
)) {
1667 struct sched_poll poll
;
1670 poll
.timeout
= timeout
;
1671 set_xen_guest_handle(poll
.ports
, &evtchn
);
1673 if (HYPERVISOR_sched_op(SCHEDOP_poll
, &poll
) != 0)
1677 EXPORT_SYMBOL(xen_poll_irq_timeout
);
1678 /* Poll waiting for an irq to become pending. In the usual case, the
1679 * irq will be disabled so it won't deliver an interrupt. */
1680 void xen_poll_irq(int irq
)
1682 xen_poll_irq_timeout(irq
, 0 /* no timeout */);
1685 /* Check whether the IRQ line is shared with other guests. */
1686 int xen_test_irq_shared(int irq
)
1688 struct irq_info
*info
= info_for_irq(irq
);
1689 struct physdev_irq_status_query irq_status
= { .irq
= info
->u
.pirq
.pirq
};
1691 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query
, &irq_status
))
1693 return !(irq_status
.flags
& XENIRQSTAT_shared
);
1695 EXPORT_SYMBOL_GPL(xen_test_irq_shared
);
1697 void xen_irq_resume(void)
1699 unsigned int cpu
, evtchn
;
1700 struct irq_info
*info
;
1702 init_evtchn_cpu_bindings();
1704 /* New event-channel space is not 'live' yet. */
1705 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1706 mask_evtchn(evtchn
);
1708 /* No IRQ <-> event-channel mappings. */
1709 list_for_each_entry(info
, &xen_irq_list_head
, list
)
1710 info
->evtchn
= 0; /* zap event-channel binding */
1712 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1713 evtchn_to_irq
[evtchn
] = -1;
1715 for_each_possible_cpu(cpu
) {
1716 restore_cpu_virqs(cpu
);
1717 restore_cpu_ipis(cpu
);
1723 static struct irq_chip xen_dynamic_chip __read_mostly
= {
1726 .irq_disable
= disable_dynirq
,
1727 .irq_mask
= disable_dynirq
,
1728 .irq_unmask
= enable_dynirq
,
1730 .irq_ack
= ack_dynirq
,
1731 .irq_mask_ack
= mask_ack_dynirq
,
1733 .irq_set_affinity
= set_affinity_irq
,
1734 .irq_retrigger
= retrigger_dynirq
,
1737 static struct irq_chip xen_pirq_chip __read_mostly
= {
1740 .irq_startup
= startup_pirq
,
1741 .irq_shutdown
= shutdown_pirq
,
1742 .irq_enable
= enable_pirq
,
1743 .irq_disable
= disable_pirq
,
1745 .irq_mask
= disable_dynirq
,
1746 .irq_unmask
= enable_dynirq
,
1748 .irq_ack
= eoi_pirq
,
1749 .irq_eoi
= eoi_pirq
,
1750 .irq_mask_ack
= mask_ack_pirq
,
1752 .irq_set_affinity
= set_affinity_irq
,
1754 .irq_retrigger
= retrigger_dynirq
,
1757 static struct irq_chip xen_percpu_chip __read_mostly
= {
1758 .name
= "xen-percpu",
1760 .irq_disable
= disable_dynirq
,
1761 .irq_mask
= disable_dynirq
,
1762 .irq_unmask
= enable_dynirq
,
1764 .irq_ack
= ack_dynirq
,
1767 int xen_set_callback_via(uint64_t via
)
1769 struct xen_hvm_param a
;
1770 a
.domid
= DOMID_SELF
;
1771 a
.index
= HVM_PARAM_CALLBACK_IRQ
;
1773 return HYPERVISOR_hvm_op(HVMOP_set_param
, &a
);
1775 EXPORT_SYMBOL_GPL(xen_set_callback_via
);
1777 #ifdef CONFIG_XEN_PVHVM
1778 /* Vector callbacks are better than PCI interrupts to receive event
1779 * channel notifications because we can receive vector callbacks on any
1780 * vcpu and we don't need PCI support or APIC interactions. */
1781 void xen_callback_vector(void)
1784 uint64_t callback_via
;
1785 if (xen_have_vector_callback
) {
1786 callback_via
= HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK
);
1787 rc
= xen_set_callback_via(callback_via
);
1789 printk(KERN_ERR
"Request for Xen HVM callback vector"
1791 xen_have_vector_callback
= 0;
1794 printk(KERN_INFO
"Xen HVM callback vector for event delivery is "
1796 /* in the restore case the vector has already been allocated */
1797 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK
, used_vectors
))
1798 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK
, xen_hvm_callback_vector
);
1802 void xen_callback_vector(void) {}
1805 void __init
xen_init_IRQ(void)
1809 evtchn_to_irq
= kcalloc(NR_EVENT_CHANNELS
, sizeof(*evtchn_to_irq
),
1811 BUG_ON(!evtchn_to_irq
);
1812 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1813 evtchn_to_irq
[i
] = -1;
1815 init_evtchn_cpu_bindings();
1817 /* No event channels are 'live' right now. */
1818 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1821 pirq_needs_eoi
= pirq_needs_eoi_flag
;
1824 if (xen_hvm_domain()) {
1825 xen_callback_vector();
1827 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1828 * __acpi_register_gsi can point at the right function */
1832 struct physdev_pirq_eoi_gmfn eoi_gmfn
;
1834 irq_ctx_init(smp_processor_id());
1835 if (xen_initial_domain())
1836 pci_xen_initial_domain();
1838 pirq_eoi_map
= (void *)__get_free_page(GFP_KERNEL
|__GFP_ZERO
);
1839 eoi_gmfn
.gmfn
= virt_to_mfn(pirq_eoi_map
);
1840 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2
, &eoi_gmfn
);
1842 free_page((unsigned long) pirq_eoi_map
);
1843 pirq_eoi_map
= NULL
;
1845 pirq_needs_eoi
= pirq_check_eoi_map
;