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>
35 #include <asm/ptrace.h>
38 #include <asm/io_apic.h>
39 #include <asm/sync_bitops.h>
40 #include <asm/xen/pci.h>
41 #include <asm/xen/hypercall.h>
42 #include <asm/xen/hypervisor.h>
46 #include <xen/xen-ops.h>
47 #include <xen/events.h>
48 #include <xen/interface/xen.h>
49 #include <xen/interface/event_channel.h>
50 #include <xen/interface/hvm/hvm_op.h>
51 #include <xen/interface/hvm/params.h>
54 * This lock protects updates to the following mapping and reference-count
55 * arrays. The lock does not need to be acquired to read the mapping tables.
57 static DEFINE_SPINLOCK(irq_mapping_update_lock
);
59 static LIST_HEAD(xen_irq_list_head
);
61 /* IRQ <-> VIRQ mapping. */
62 static DEFINE_PER_CPU(int [NR_VIRQS
], virq_to_irq
) = {[0 ... NR_VIRQS
-1] = -1};
64 /* IRQ <-> IPI mapping */
65 static DEFINE_PER_CPU(int [XEN_NR_IPIS
], ipi_to_irq
) = {[0 ... XEN_NR_IPIS
-1] = -1};
67 /* Interrupt types. */
77 * Packed IRQ information:
78 * type - enum xen_irq_type
79 * event channel - irq->event channel mapping
80 * cpu - cpu this event channel is bound to
81 * index - type-specific information:
82 * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
83 * guest, or GSI (real passthrough IRQ) of the device.
90 struct list_head list
;
91 enum xen_irq_type type
; /* type */
93 unsigned short evtchn
; /* event channel */
94 unsigned short cpu
; /* cpu bound */
102 unsigned char vector
;
107 #define PIRQ_NEEDS_EOI (1 << 0)
108 #define PIRQ_SHAREABLE (1 << 1)
110 static int *evtchn_to_irq
;
112 static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS
/BITS_PER_LONG
],
115 /* Xen will never allocate port zero for any purpose. */
116 #define VALID_EVTCHN(chn) ((chn) != 0)
118 static struct irq_chip xen_dynamic_chip
;
119 static struct irq_chip xen_percpu_chip
;
120 static struct irq_chip xen_pirq_chip
;
122 /* Get info for IRQ */
123 static struct irq_info
*info_for_irq(unsigned irq
)
125 return irq_get_handler_data(irq
);
128 /* Constructors for packed IRQ information. */
129 static void xen_irq_info_common_init(struct irq_info
*info
,
131 enum xen_irq_type type
,
132 unsigned short evtchn
,
136 BUG_ON(info
->type
!= IRQT_UNBOUND
&& info
->type
!= type
);
140 info
->evtchn
= evtchn
;
143 evtchn_to_irq
[evtchn
] = irq
;
146 static void xen_irq_info_evtchn_init(unsigned irq
,
147 unsigned short evtchn
)
149 struct irq_info
*info
= info_for_irq(irq
);
151 xen_irq_info_common_init(info
, irq
, IRQT_EVTCHN
, evtchn
, 0);
154 static void xen_irq_info_ipi_init(unsigned cpu
,
156 unsigned short evtchn
,
159 struct irq_info
*info
= info_for_irq(irq
);
161 xen_irq_info_common_init(info
, irq
, IRQT_IPI
, evtchn
, 0);
165 per_cpu(ipi_to_irq
, cpu
)[ipi
] = irq
;
168 static void xen_irq_info_virq_init(unsigned cpu
,
170 unsigned short evtchn
,
173 struct irq_info
*info
= info_for_irq(irq
);
175 xen_irq_info_common_init(info
, irq
, IRQT_VIRQ
, evtchn
, 0);
179 per_cpu(virq_to_irq
, cpu
)[virq
] = irq
;
182 static void xen_irq_info_pirq_init(unsigned irq
,
183 unsigned short evtchn
,
186 unsigned short vector
,
189 struct irq_info
*info
= info_for_irq(irq
);
191 xen_irq_info_common_init(info
, irq
, IRQT_PIRQ
, evtchn
, 0);
193 info
->u
.pirq
.pirq
= pirq
;
194 info
->u
.pirq
.gsi
= gsi
;
195 info
->u
.pirq
.vector
= vector
;
196 info
->u
.pirq
.flags
= flags
;
200 * Accessors for packed IRQ information.
202 static unsigned int evtchn_from_irq(unsigned irq
)
204 if (unlikely(WARN(irq
< 0 || irq
>= nr_irqs
, "Invalid irq %d!\n", irq
)))
207 return info_for_irq(irq
)->evtchn
;
210 unsigned irq_from_evtchn(unsigned int evtchn
)
212 return evtchn_to_irq
[evtchn
];
214 EXPORT_SYMBOL_GPL(irq_from_evtchn
);
216 static enum ipi_vector
ipi_from_irq(unsigned irq
)
218 struct irq_info
*info
= info_for_irq(irq
);
220 BUG_ON(info
== NULL
);
221 BUG_ON(info
->type
!= IRQT_IPI
);
226 static unsigned virq_from_irq(unsigned irq
)
228 struct irq_info
*info
= info_for_irq(irq
);
230 BUG_ON(info
== NULL
);
231 BUG_ON(info
->type
!= IRQT_VIRQ
);
236 static unsigned pirq_from_irq(unsigned irq
)
238 struct irq_info
*info
= info_for_irq(irq
);
240 BUG_ON(info
== NULL
);
241 BUG_ON(info
->type
!= IRQT_PIRQ
);
243 return info
->u
.pirq
.pirq
;
246 static enum xen_irq_type
type_from_irq(unsigned irq
)
248 return info_for_irq(irq
)->type
;
251 static unsigned cpu_from_irq(unsigned irq
)
253 return info_for_irq(irq
)->cpu
;
256 static unsigned int cpu_from_evtchn(unsigned int evtchn
)
258 int irq
= evtchn_to_irq
[evtchn
];
262 ret
= cpu_from_irq(irq
);
267 static bool pirq_needs_eoi(unsigned irq
)
269 struct irq_info
*info
= info_for_irq(irq
);
271 BUG_ON(info
->type
!= IRQT_PIRQ
);
273 return info
->u
.pirq
.flags
& PIRQ_NEEDS_EOI
;
276 static inline unsigned long active_evtchns(unsigned int cpu
,
277 struct shared_info
*sh
,
280 return (sh
->evtchn_pending
[idx
] &
281 per_cpu(cpu_evtchn_mask
, cpu
)[idx
] &
282 ~sh
->evtchn_mask
[idx
]);
285 static void bind_evtchn_to_cpu(unsigned int chn
, unsigned int cpu
)
287 int irq
= evtchn_to_irq
[chn
];
291 cpumask_copy(irq_to_desc(irq
)->irq_data
.affinity
, cpumask_of(cpu
));
294 clear_bit(chn
, per_cpu(cpu_evtchn_mask
, cpu_from_irq(irq
)));
295 set_bit(chn
, per_cpu(cpu_evtchn_mask
, cpu
));
297 info_for_irq(irq
)->cpu
= cpu
;
300 static void init_evtchn_cpu_bindings(void)
304 struct irq_info
*info
;
306 /* By default all event channels notify CPU#0. */
307 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
308 struct irq_desc
*desc
= irq_to_desc(info
->irq
);
309 cpumask_copy(desc
->irq_data
.affinity
, cpumask_of(0));
313 for_each_possible_cpu(i
)
314 memset(per_cpu(cpu_evtchn_mask
, i
),
315 (i
== 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask
, i
)));
318 static inline void clear_evtchn(int port
)
320 struct shared_info
*s
= HYPERVISOR_shared_info
;
321 sync_clear_bit(port
, &s
->evtchn_pending
[0]);
324 static inline void set_evtchn(int port
)
326 struct shared_info
*s
= HYPERVISOR_shared_info
;
327 sync_set_bit(port
, &s
->evtchn_pending
[0]);
330 static inline int test_evtchn(int port
)
332 struct shared_info
*s
= HYPERVISOR_shared_info
;
333 return sync_test_bit(port
, &s
->evtchn_pending
[0]);
338 * notify_remote_via_irq - send event to remote end of event channel via irq
339 * @irq: irq of event channel to send event to
341 * Unlike notify_remote_via_evtchn(), this is safe to use across
342 * save/restore. Notifications on a broken connection are silently
345 void notify_remote_via_irq(int irq
)
347 int evtchn
= evtchn_from_irq(irq
);
349 if (VALID_EVTCHN(evtchn
))
350 notify_remote_via_evtchn(evtchn
);
352 EXPORT_SYMBOL_GPL(notify_remote_via_irq
);
354 static void mask_evtchn(int port
)
356 struct shared_info
*s
= HYPERVISOR_shared_info
;
357 sync_set_bit(port
, &s
->evtchn_mask
[0]);
360 static void unmask_evtchn(int port
)
362 struct shared_info
*s
= HYPERVISOR_shared_info
;
363 unsigned int cpu
= get_cpu();
365 BUG_ON(!irqs_disabled());
367 /* Slow path (hypercall) if this is a non-local port. */
368 if (unlikely(cpu
!= cpu_from_evtchn(port
))) {
369 struct evtchn_unmask unmask
= { .port
= port
};
370 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask
, &unmask
);
372 struct vcpu_info
*vcpu_info
= __this_cpu_read(xen_vcpu
);
374 sync_clear_bit(port
, &s
->evtchn_mask
[0]);
377 * The following is basically the equivalent of
378 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
379 * the interrupt edge' if the channel is masked.
381 if (sync_test_bit(port
, &s
->evtchn_pending
[0]) &&
382 !sync_test_and_set_bit(port
/ BITS_PER_LONG
,
383 &vcpu_info
->evtchn_pending_sel
))
384 vcpu_info
->evtchn_upcall_pending
= 1;
390 static void xen_irq_init(unsigned irq
)
392 struct irq_info
*info
;
393 struct irq_desc
*desc
= irq_to_desc(irq
);
396 /* By default all event channels notify CPU#0. */
397 cpumask_copy(desc
->irq_data
.affinity
, cpumask_of(0));
400 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
402 panic("Unable to allocate metadata for IRQ%d\n", irq
);
404 info
->type
= IRQT_UNBOUND
;
406 irq_set_handler_data(irq
, info
);
408 list_add_tail(&info
->list
, &xen_irq_list_head
);
411 static int __must_check
xen_allocate_irq_dynamic(void)
416 #ifdef CONFIG_X86_IO_APIC
418 * For an HVM guest or domain 0 which see "real" (emulated or
419 * actual respectively) GSIs we allocate dynamic IRQs
420 * e.g. those corresponding to event channels or MSIs
421 * etc. from the range above those "real" GSIs to avoid
424 if (xen_initial_domain() || xen_hvm_domain())
425 first
= get_nr_irqs_gsi();
428 irq
= irq_alloc_desc_from(first
, -1);
435 static int __must_check
xen_allocate_irq_gsi(unsigned gsi
)
440 * A PV guest has no concept of a GSI (since it has no ACPI
441 * nor access to/knowledge of the physical APICs). Therefore
442 * all IRQs are dynamically allocated from the entire IRQ
445 if (xen_pv_domain() && !xen_initial_domain())
446 return xen_allocate_irq_dynamic();
448 /* Legacy IRQ descriptors are already allocated by the arch. */
449 if (gsi
< NR_IRQS_LEGACY
)
452 irq
= irq_alloc_desc_at(gsi
, -1);
459 static void xen_free_irq(unsigned irq
)
461 struct irq_info
*info
= irq_get_handler_data(irq
);
463 list_del(&info
->list
);
465 irq_set_handler_data(irq
, NULL
);
469 /* Legacy IRQ descriptors are managed by the arch. */
470 if (irq
< NR_IRQS_LEGACY
)
476 static void pirq_unmask_notify(int irq
)
478 struct physdev_eoi eoi
= { .irq
= pirq_from_irq(irq
) };
480 if (unlikely(pirq_needs_eoi(irq
))) {
481 int rc
= HYPERVISOR_physdev_op(PHYSDEVOP_eoi
, &eoi
);
486 static void pirq_query_unmask(int irq
)
488 struct physdev_irq_status_query irq_status
;
489 struct irq_info
*info
= info_for_irq(irq
);
491 BUG_ON(info
->type
!= IRQT_PIRQ
);
493 irq_status
.irq
= pirq_from_irq(irq
);
494 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query
, &irq_status
))
495 irq_status
.flags
= 0;
497 info
->u
.pirq
.flags
&= ~PIRQ_NEEDS_EOI
;
498 if (irq_status
.flags
& XENIRQSTAT_needs_eoi
)
499 info
->u
.pirq
.flags
|= PIRQ_NEEDS_EOI
;
502 static bool probing_irq(int irq
)
504 struct irq_desc
*desc
= irq_to_desc(irq
);
506 return desc
&& desc
->action
== NULL
;
509 static unsigned int __startup_pirq(unsigned int irq
)
511 struct evtchn_bind_pirq bind_pirq
;
512 struct irq_info
*info
= info_for_irq(irq
);
513 int evtchn
= evtchn_from_irq(irq
);
516 BUG_ON(info
->type
!= IRQT_PIRQ
);
518 if (VALID_EVTCHN(evtchn
))
521 bind_pirq
.pirq
= pirq_from_irq(irq
);
522 /* NB. We are happy to share unless we are probing. */
523 bind_pirq
.flags
= info
->u
.pirq
.flags
& PIRQ_SHAREABLE
?
524 BIND_PIRQ__WILL_SHARE
: 0;
525 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq
, &bind_pirq
);
527 if (!probing_irq(irq
))
528 printk(KERN_INFO
"Failed to obtain physical IRQ %d\n",
532 evtchn
= bind_pirq
.port
;
534 pirq_query_unmask(irq
);
536 evtchn_to_irq
[evtchn
] = irq
;
537 bind_evtchn_to_cpu(evtchn
, 0);
538 info
->evtchn
= evtchn
;
541 unmask_evtchn(evtchn
);
542 pirq_unmask_notify(irq
);
547 static unsigned int startup_pirq(struct irq_data
*data
)
549 return __startup_pirq(data
->irq
);
552 static void shutdown_pirq(struct irq_data
*data
)
554 struct evtchn_close close
;
555 unsigned int irq
= data
->irq
;
556 struct irq_info
*info
= info_for_irq(irq
);
557 int evtchn
= evtchn_from_irq(irq
);
559 BUG_ON(info
->type
!= IRQT_PIRQ
);
561 if (!VALID_EVTCHN(evtchn
))
567 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
570 bind_evtchn_to_cpu(evtchn
, 0);
571 evtchn_to_irq
[evtchn
] = -1;
575 static void enable_pirq(struct irq_data
*data
)
580 static void disable_pirq(struct irq_data
*data
)
584 static void ack_pirq(struct irq_data
*data
)
586 int evtchn
= evtchn_from_irq(data
->irq
);
590 if (VALID_EVTCHN(evtchn
)) {
592 clear_evtchn(evtchn
);
596 static int find_irq_by_gsi(unsigned gsi
)
598 struct irq_info
*info
;
600 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
601 if (info
->type
!= IRQT_PIRQ
)
604 if (info
->u
.pirq
.gsi
== gsi
)
611 int xen_allocate_pirq_gsi(unsigned gsi
)
617 * Do not make any assumptions regarding the relationship between the
618 * IRQ number returned here and the Xen pirq argument.
620 * Note: We don't assign an event channel until the irq actually started
621 * up. Return an existing irq if we've already got one for the gsi.
623 int xen_bind_pirq_gsi_to_irq(unsigned gsi
,
624 unsigned pirq
, int shareable
, char *name
)
627 struct physdev_irq irq_op
;
629 spin_lock(&irq_mapping_update_lock
);
631 irq
= find_irq_by_gsi(gsi
);
633 printk(KERN_INFO
"xen_map_pirq_gsi: returning irq %d for gsi %u\n",
635 goto out
; /* XXX need refcount? */
638 irq
= xen_allocate_irq_gsi(gsi
);
642 irq_set_chip_and_handler_name(irq
, &xen_pirq_chip
, handle_level_irq
,
648 /* Only the privileged domain can do this. For non-priv, the pcifront
649 * driver provides a PCI bus that does the call to do exactly
650 * this in the priv domain. */
651 if (xen_initial_domain() &&
652 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector
, &irq_op
)) {
658 xen_irq_info_pirq_init(irq
, 0, pirq
, gsi
, irq_op
.vector
,
659 shareable
? PIRQ_SHAREABLE
: 0);
662 spin_unlock(&irq_mapping_update_lock
);
667 #ifdef CONFIG_PCI_MSI
668 int xen_allocate_pirq_msi(struct pci_dev
*dev
, struct msi_desc
*msidesc
)
671 struct physdev_get_free_pirq op_get_free_pirq
;
673 op_get_free_pirq
.type
= MAP_PIRQ_TYPE_MSI
;
674 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq
, &op_get_free_pirq
);
676 WARN_ONCE(rc
== -ENOSYS
,
677 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
679 return rc
? -1 : op_get_free_pirq
.pirq
;
682 int xen_bind_pirq_msi_to_irq(struct pci_dev
*dev
, struct msi_desc
*msidesc
,
683 int pirq
, int vector
, const char *name
)
687 spin_lock(&irq_mapping_update_lock
);
689 irq
= xen_allocate_irq_dynamic();
693 irq_set_chip_and_handler_name(irq
, &xen_pirq_chip
, handle_level_irq
,
696 xen_irq_info_pirq_init(irq
, 0, pirq
, 0, vector
, 0);
697 ret
= irq_set_msi_desc(irq
, msidesc
);
701 spin_unlock(&irq_mapping_update_lock
);
704 spin_unlock(&irq_mapping_update_lock
);
710 int xen_destroy_irq(int irq
)
712 struct irq_desc
*desc
;
713 struct physdev_unmap_pirq unmap_irq
;
714 struct irq_info
*info
= info_for_irq(irq
);
717 spin_lock(&irq_mapping_update_lock
);
719 desc
= irq_to_desc(irq
);
723 if (xen_initial_domain()) {
724 unmap_irq
.pirq
= info
->u
.pirq
.pirq
;
725 unmap_irq
.domid
= DOMID_SELF
;
726 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq
, &unmap_irq
);
728 printk(KERN_WARNING
"unmap irq failed %d\n", rc
);
736 spin_unlock(&irq_mapping_update_lock
);
740 int xen_irq_from_pirq(unsigned pirq
)
744 struct irq_info
*info
;
746 spin_lock(&irq_mapping_update_lock
);
748 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
749 if (info
== NULL
|| info
->type
!= IRQT_PIRQ
)
752 if (info
->u
.pirq
.pirq
== pirq
)
757 spin_unlock(&irq_mapping_update_lock
);
762 int bind_evtchn_to_irq(unsigned int evtchn
)
766 spin_lock(&irq_mapping_update_lock
);
768 irq
= evtchn_to_irq
[evtchn
];
771 irq
= xen_allocate_irq_dynamic();
775 irq_set_chip_and_handler_name(irq
, &xen_dynamic_chip
,
776 handle_fasteoi_irq
, "event");
778 xen_irq_info_evtchn_init(irq
, evtchn
);
782 spin_unlock(&irq_mapping_update_lock
);
786 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq
);
788 static int bind_ipi_to_irq(unsigned int ipi
, unsigned int cpu
)
790 struct evtchn_bind_ipi bind_ipi
;
793 spin_lock(&irq_mapping_update_lock
);
795 irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
];
798 irq
= xen_allocate_irq_dynamic();
802 irq_set_chip_and_handler_name(irq
, &xen_percpu_chip
,
803 handle_percpu_irq
, "ipi");
806 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
809 evtchn
= bind_ipi
.port
;
811 xen_irq_info_ipi_init(cpu
, irq
, evtchn
, ipi
);
813 bind_evtchn_to_cpu(evtchn
, cpu
);
817 spin_unlock(&irq_mapping_update_lock
);
821 static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain
,
822 unsigned int remote_port
)
824 struct evtchn_bind_interdomain bind_interdomain
;
827 bind_interdomain
.remote_dom
= remote_domain
;
828 bind_interdomain
.remote_port
= remote_port
;
830 err
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain
,
833 return err
? : bind_evtchn_to_irq(bind_interdomain
.local_port
);
837 int bind_virq_to_irq(unsigned int virq
, unsigned int cpu
)
839 struct evtchn_bind_virq bind_virq
;
842 spin_lock(&irq_mapping_update_lock
);
844 irq
= per_cpu(virq_to_irq
, cpu
)[virq
];
847 irq
= xen_allocate_irq_dynamic();
851 irq_set_chip_and_handler_name(irq
, &xen_percpu_chip
,
852 handle_percpu_irq
, "virq");
854 bind_virq
.virq
= virq
;
855 bind_virq
.vcpu
= cpu
;
856 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
859 evtchn
= bind_virq
.port
;
861 xen_irq_info_virq_init(cpu
, irq
, evtchn
, virq
);
863 bind_evtchn_to_cpu(evtchn
, cpu
);
867 spin_unlock(&irq_mapping_update_lock
);
872 static void unbind_from_irq(unsigned int irq
)
874 struct evtchn_close close
;
875 int evtchn
= evtchn_from_irq(irq
);
877 spin_lock(&irq_mapping_update_lock
);
879 if (VALID_EVTCHN(evtchn
)) {
881 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
884 switch (type_from_irq(irq
)) {
886 per_cpu(virq_to_irq
, cpu_from_evtchn(evtchn
))
887 [virq_from_irq(irq
)] = -1;
890 per_cpu(ipi_to_irq
, cpu_from_evtchn(evtchn
))
891 [ipi_from_irq(irq
)] = -1;
897 /* Closed ports are implicitly re-bound to VCPU0. */
898 bind_evtchn_to_cpu(evtchn
, 0);
900 evtchn_to_irq
[evtchn
] = -1;
903 BUG_ON(info_for_irq(irq
)->type
== IRQT_UNBOUND
);
907 spin_unlock(&irq_mapping_update_lock
);
910 int bind_evtchn_to_irqhandler(unsigned int evtchn
,
911 irq_handler_t handler
,
912 unsigned long irqflags
,
913 const char *devname
, void *dev_id
)
918 irq
= bind_evtchn_to_irq(evtchn
);
921 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
923 unbind_from_irq(irq
);
929 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler
);
931 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain
,
932 unsigned int remote_port
,
933 irq_handler_t handler
,
934 unsigned long irqflags
,
940 irq
= bind_interdomain_evtchn_to_irq(remote_domain
, remote_port
);
944 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
946 unbind_from_irq(irq
);
952 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler
);
954 int bind_virq_to_irqhandler(unsigned int virq
, unsigned int cpu
,
955 irq_handler_t handler
,
956 unsigned long irqflags
, const char *devname
, void *dev_id
)
961 irq
= bind_virq_to_irq(virq
, cpu
);
964 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
966 unbind_from_irq(irq
);
972 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler
);
974 int bind_ipi_to_irqhandler(enum ipi_vector ipi
,
976 irq_handler_t handler
,
977 unsigned long irqflags
,
983 irq
= bind_ipi_to_irq(ipi
, cpu
);
987 irqflags
|= IRQF_NO_SUSPEND
| IRQF_FORCE_RESUME
;
988 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
990 unbind_from_irq(irq
);
997 void unbind_from_irqhandler(unsigned int irq
, void *dev_id
)
999 free_irq(irq
, dev_id
);
1000 unbind_from_irq(irq
);
1002 EXPORT_SYMBOL_GPL(unbind_from_irqhandler
);
1004 void xen_send_IPI_one(unsigned int cpu
, enum ipi_vector vector
)
1006 int irq
= per_cpu(ipi_to_irq
, cpu
)[vector
];
1008 notify_remote_via_irq(irq
);
1011 irqreturn_t
xen_debug_interrupt(int irq
, void *dev_id
)
1013 struct shared_info
*sh
= HYPERVISOR_shared_info
;
1014 int cpu
= smp_processor_id();
1015 unsigned long *cpu_evtchn
= per_cpu(cpu_evtchn_mask
, cpu
);
1017 unsigned long flags
;
1018 static DEFINE_SPINLOCK(debug_lock
);
1019 struct vcpu_info
*v
;
1021 spin_lock_irqsave(&debug_lock
, flags
);
1023 printk("\nvcpu %d\n ", cpu
);
1025 for_each_online_cpu(i
) {
1027 v
= per_cpu(xen_vcpu
, i
);
1028 pending
= (get_irq_regs() && i
== cpu
)
1029 ? xen_irqs_disabled(get_irq_regs())
1030 : v
->evtchn_upcall_mask
;
1031 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i
,
1032 pending
, v
->evtchn_upcall_pending
,
1033 (int)(sizeof(v
->evtchn_pending_sel
)*2),
1034 v
->evtchn_pending_sel
);
1036 v
= per_cpu(xen_vcpu
, cpu
);
1038 printk("\npending:\n ");
1039 for (i
= ARRAY_SIZE(sh
->evtchn_pending
)-1; i
>= 0; i
--)
1040 printk("%0*lx%s", (int)sizeof(sh
->evtchn_pending
[0])*2,
1041 sh
->evtchn_pending
[i
],
1042 i
% 8 == 0 ? "\n " : " ");
1043 printk("\nglobal mask:\n ");
1044 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
1046 (int)(sizeof(sh
->evtchn_mask
[0])*2),
1048 i
% 8 == 0 ? "\n " : " ");
1050 printk("\nglobally unmasked:\n ");
1051 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
1052 printk("%0*lx%s", (int)(sizeof(sh
->evtchn_mask
[0])*2),
1053 sh
->evtchn_pending
[i
] & ~sh
->evtchn_mask
[i
],
1054 i
% 8 == 0 ? "\n " : " ");
1056 printk("\nlocal cpu%d mask:\n ", cpu
);
1057 for (i
= (NR_EVENT_CHANNELS
/BITS_PER_LONG
)-1; i
>= 0; i
--)
1058 printk("%0*lx%s", (int)(sizeof(cpu_evtchn
[0])*2),
1060 i
% 8 == 0 ? "\n " : " ");
1062 printk("\nlocally unmasked:\n ");
1063 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--) {
1064 unsigned long pending
= sh
->evtchn_pending
[i
]
1065 & ~sh
->evtchn_mask
[i
]
1067 printk("%0*lx%s", (int)(sizeof(sh
->evtchn_mask
[0])*2),
1068 pending
, i
% 8 == 0 ? "\n " : " ");
1071 printk("\npending list:\n");
1072 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++) {
1073 if (sync_test_bit(i
, sh
->evtchn_pending
)) {
1074 int word_idx
= i
/ BITS_PER_LONG
;
1075 printk(" %d: event %d -> irq %d%s%s%s\n",
1076 cpu_from_evtchn(i
), i
,
1078 sync_test_bit(word_idx
, &v
->evtchn_pending_sel
)
1080 !sync_test_bit(i
, sh
->evtchn_mask
)
1081 ? "" : " globally-masked",
1082 sync_test_bit(i
, cpu_evtchn
)
1083 ? "" : " locally-masked");
1087 spin_unlock_irqrestore(&debug_lock
, flags
);
1092 static DEFINE_PER_CPU(unsigned, xed_nesting_count
);
1093 static DEFINE_PER_CPU(unsigned int, current_word_idx
);
1094 static DEFINE_PER_CPU(unsigned int, current_bit_idx
);
1097 * Mask out the i least significant bits of w
1099 #define MASK_LSBS(w, i) (w & ((~0UL) << i))
1102 * Search the CPUs pending events bitmasks. For each one found, map
1103 * the event number to an irq, and feed it into do_IRQ() for
1106 * Xen uses a two-level bitmap to speed searching. The first level is
1107 * a bitset of words which contain pending event bits. The second
1108 * level is a bitset of pending events themselves.
1110 static void __xen_evtchn_do_upcall(void)
1112 int start_word_idx
, start_bit_idx
;
1113 int word_idx
, bit_idx
;
1115 int cpu
= get_cpu();
1116 struct shared_info
*s
= HYPERVISOR_shared_info
;
1117 struct vcpu_info
*vcpu_info
= __this_cpu_read(xen_vcpu
);
1121 unsigned long pending_words
;
1123 vcpu_info
->evtchn_upcall_pending
= 0;
1125 if (__this_cpu_inc_return(xed_nesting_count
) - 1)
1128 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1129 /* Clear master flag /before/ clearing selector flag. */
1132 pending_words
= xchg(&vcpu_info
->evtchn_pending_sel
, 0);
1134 start_word_idx
= __this_cpu_read(current_word_idx
);
1135 start_bit_idx
= __this_cpu_read(current_bit_idx
);
1137 word_idx
= start_word_idx
;
1139 for (i
= 0; pending_words
!= 0; i
++) {
1140 unsigned long pending_bits
;
1141 unsigned long words
;
1143 words
= MASK_LSBS(pending_words
, word_idx
);
1146 * If we masked out all events, wrap to beginning.
1153 word_idx
= __ffs(words
);
1155 pending_bits
= active_evtchns(cpu
, s
, word_idx
);
1156 bit_idx
= 0; /* usually scan entire word from start */
1157 if (word_idx
== start_word_idx
) {
1158 /* We scan the starting word in two parts */
1160 /* 1st time: start in the middle */
1161 bit_idx
= start_bit_idx
;
1163 /* 2nd time: mask bits done already */
1164 bit_idx
&= (1UL << start_bit_idx
) - 1;
1170 struct irq_desc
*desc
;
1172 bits
= MASK_LSBS(pending_bits
, bit_idx
);
1174 /* If we masked out all events, move on. */
1178 bit_idx
= __ffs(bits
);
1181 port
= (word_idx
* BITS_PER_LONG
) + bit_idx
;
1182 irq
= evtchn_to_irq
[port
];
1188 desc
= irq_to_desc(irq
);
1190 generic_handle_irq_desc(irq
, desc
);
1193 bit_idx
= (bit_idx
+ 1) % BITS_PER_LONG
;
1195 /* Next caller starts at last processed + 1 */
1196 __this_cpu_write(current_word_idx
,
1197 bit_idx
? word_idx
:
1198 (word_idx
+1) % BITS_PER_LONG
);
1199 __this_cpu_write(current_bit_idx
, bit_idx
);
1200 } while (bit_idx
!= 0);
1202 /* Scan start_l1i twice; all others once. */
1203 if ((word_idx
!= start_word_idx
) || (i
!= 0))
1204 pending_words
&= ~(1UL << word_idx
);
1206 word_idx
= (word_idx
+ 1) % BITS_PER_LONG
;
1209 BUG_ON(!irqs_disabled());
1211 count
= __this_cpu_read(xed_nesting_count
);
1212 __this_cpu_write(xed_nesting_count
, 0);
1213 } while (count
!= 1 || vcpu_info
->evtchn_upcall_pending
);
1220 void xen_evtchn_do_upcall(struct pt_regs
*regs
)
1222 struct pt_regs
*old_regs
= set_irq_regs(regs
);
1227 __xen_evtchn_do_upcall();
1230 set_irq_regs(old_regs
);
1233 void xen_hvm_evtchn_do_upcall(void)
1235 __xen_evtchn_do_upcall();
1237 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall
);
1239 /* Rebind a new event channel to an existing irq. */
1240 void rebind_evtchn_irq(int evtchn
, int irq
)
1242 struct irq_info
*info
= info_for_irq(irq
);
1244 /* Make sure the irq is masked, since the new event channel
1245 will also be masked. */
1248 spin_lock(&irq_mapping_update_lock
);
1250 /* After resume the irq<->evtchn mappings are all cleared out */
1251 BUG_ON(evtchn_to_irq
[evtchn
] != -1);
1252 /* Expect irq to have been bound before,
1253 so there should be a proper type */
1254 BUG_ON(info
->type
== IRQT_UNBOUND
);
1256 xen_irq_info_evtchn_init(irq
, evtchn
);
1258 spin_unlock(&irq_mapping_update_lock
);
1260 /* new event channels are always bound to cpu 0 */
1261 irq_set_affinity(irq
, cpumask_of(0));
1263 /* Unmask the event channel. */
1267 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1268 static int rebind_irq_to_cpu(unsigned irq
, unsigned tcpu
)
1270 struct evtchn_bind_vcpu bind_vcpu
;
1271 int evtchn
= evtchn_from_irq(irq
);
1273 if (!VALID_EVTCHN(evtchn
))
1277 * Events delivered via platform PCI interrupts are always
1278 * routed to vcpu 0 and hence cannot be rebound.
1280 if (xen_hvm_domain() && !xen_have_vector_callback
)
1283 /* Send future instances of this interrupt to other vcpu. */
1284 bind_vcpu
.port
= evtchn
;
1285 bind_vcpu
.vcpu
= tcpu
;
1288 * If this fails, it usually just indicates that we're dealing with a
1289 * virq or IPI channel, which don't actually need to be rebound. Ignore
1290 * it, but don't do the xenlinux-level rebind in that case.
1292 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu
, &bind_vcpu
) >= 0)
1293 bind_evtchn_to_cpu(evtchn
, tcpu
);
1298 static int set_affinity_irq(struct irq_data
*data
, const struct cpumask
*dest
,
1301 unsigned tcpu
= cpumask_first(dest
);
1303 return rebind_irq_to_cpu(data
->irq
, tcpu
);
1306 int resend_irq_on_evtchn(unsigned int irq
)
1308 int masked
, evtchn
= evtchn_from_irq(irq
);
1309 struct shared_info
*s
= HYPERVISOR_shared_info
;
1311 if (!VALID_EVTCHN(evtchn
))
1314 masked
= sync_test_and_set_bit(evtchn
, s
->evtchn_mask
);
1315 sync_set_bit(evtchn
, s
->evtchn_pending
);
1317 unmask_evtchn(evtchn
);
1322 static void enable_dynirq(struct irq_data
*data
)
1324 int evtchn
= evtchn_from_irq(data
->irq
);
1326 if (VALID_EVTCHN(evtchn
))
1327 unmask_evtchn(evtchn
);
1330 static void disable_dynirq(struct irq_data
*data
)
1332 int evtchn
= evtchn_from_irq(data
->irq
);
1334 if (VALID_EVTCHN(evtchn
))
1335 mask_evtchn(evtchn
);
1338 static void ack_dynirq(struct irq_data
*data
)
1340 int evtchn
= evtchn_from_irq(data
->irq
);
1342 irq_move_masked_irq(data
);
1344 if (VALID_EVTCHN(evtchn
))
1345 unmask_evtchn(evtchn
);
1348 static int retrigger_dynirq(struct irq_data
*data
)
1350 int evtchn
= evtchn_from_irq(data
->irq
);
1351 struct shared_info
*sh
= HYPERVISOR_shared_info
;
1354 if (VALID_EVTCHN(evtchn
)) {
1357 masked
= sync_test_and_set_bit(evtchn
, sh
->evtchn_mask
);
1358 sync_set_bit(evtchn
, sh
->evtchn_pending
);
1360 unmask_evtchn(evtchn
);
1367 static void restore_pirqs(void)
1369 int pirq
, rc
, irq
, gsi
;
1370 struct physdev_map_pirq map_irq
;
1371 struct irq_info
*info
;
1373 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
1374 if (info
->type
!= IRQT_PIRQ
)
1377 pirq
= info
->u
.pirq
.pirq
;
1378 gsi
= info
->u
.pirq
.gsi
;
1381 /* save/restore of PT devices doesn't work, so at this point the
1382 * only devices present are GSI based emulated devices */
1386 map_irq
.domid
= DOMID_SELF
;
1387 map_irq
.type
= MAP_PIRQ_TYPE_GSI
;
1388 map_irq
.index
= gsi
;
1389 map_irq
.pirq
= pirq
;
1391 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq
, &map_irq
);
1393 printk(KERN_WARNING
"xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1394 gsi
, irq
, pirq
, rc
);
1399 printk(KERN_DEBUG
"xen: --> irq=%d, pirq=%d\n", irq
, map_irq
.pirq
);
1401 __startup_pirq(irq
);
1405 static void restore_cpu_virqs(unsigned int cpu
)
1407 struct evtchn_bind_virq bind_virq
;
1408 int virq
, irq
, evtchn
;
1410 for (virq
= 0; virq
< NR_VIRQS
; virq
++) {
1411 if ((irq
= per_cpu(virq_to_irq
, cpu
)[virq
]) == -1)
1414 BUG_ON(virq_from_irq(irq
) != virq
);
1416 /* Get a new binding from Xen. */
1417 bind_virq
.virq
= virq
;
1418 bind_virq
.vcpu
= cpu
;
1419 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
1422 evtchn
= bind_virq
.port
;
1424 /* Record the new mapping. */
1425 xen_irq_info_virq_init(cpu
, irq
, evtchn
, virq
);
1426 bind_evtchn_to_cpu(evtchn
, cpu
);
1430 static void restore_cpu_ipis(unsigned int cpu
)
1432 struct evtchn_bind_ipi bind_ipi
;
1433 int ipi
, irq
, evtchn
;
1435 for (ipi
= 0; ipi
< XEN_NR_IPIS
; ipi
++) {
1436 if ((irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
]) == -1)
1439 BUG_ON(ipi_from_irq(irq
) != ipi
);
1441 /* Get a new binding from Xen. */
1442 bind_ipi
.vcpu
= cpu
;
1443 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
1446 evtchn
= bind_ipi
.port
;
1448 /* Record the new mapping. */
1449 xen_irq_info_ipi_init(cpu
, irq
, evtchn
, ipi
);
1450 bind_evtchn_to_cpu(evtchn
, cpu
);
1454 /* Clear an irq's pending state, in preparation for polling on it */
1455 void xen_clear_irq_pending(int irq
)
1457 int evtchn
= evtchn_from_irq(irq
);
1459 if (VALID_EVTCHN(evtchn
))
1460 clear_evtchn(evtchn
);
1462 EXPORT_SYMBOL(xen_clear_irq_pending
);
1463 void xen_set_irq_pending(int irq
)
1465 int evtchn
= evtchn_from_irq(irq
);
1467 if (VALID_EVTCHN(evtchn
))
1471 bool xen_test_irq_pending(int irq
)
1473 int evtchn
= evtchn_from_irq(irq
);
1476 if (VALID_EVTCHN(evtchn
))
1477 ret
= test_evtchn(evtchn
);
1482 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1483 * the irq will be disabled so it won't deliver an interrupt. */
1484 void xen_poll_irq_timeout(int irq
, u64 timeout
)
1486 evtchn_port_t evtchn
= evtchn_from_irq(irq
);
1488 if (VALID_EVTCHN(evtchn
)) {
1489 struct sched_poll poll
;
1492 poll
.timeout
= timeout
;
1493 set_xen_guest_handle(poll
.ports
, &evtchn
);
1495 if (HYPERVISOR_sched_op(SCHEDOP_poll
, &poll
) != 0)
1499 EXPORT_SYMBOL(xen_poll_irq_timeout
);
1500 /* Poll waiting for an irq to become pending. In the usual case, the
1501 * irq will be disabled so it won't deliver an interrupt. */
1502 void xen_poll_irq(int irq
)
1504 xen_poll_irq_timeout(irq
, 0 /* no timeout */);
1507 void xen_irq_resume(void)
1509 unsigned int cpu
, evtchn
;
1510 struct irq_info
*info
;
1512 init_evtchn_cpu_bindings();
1514 /* New event-channel space is not 'live' yet. */
1515 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1516 mask_evtchn(evtchn
);
1518 /* No IRQ <-> event-channel mappings. */
1519 list_for_each_entry(info
, &xen_irq_list_head
, list
)
1520 info
->evtchn
= 0; /* zap event-channel binding */
1522 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1523 evtchn_to_irq
[evtchn
] = -1;
1525 for_each_possible_cpu(cpu
) {
1526 restore_cpu_virqs(cpu
);
1527 restore_cpu_ipis(cpu
);
1533 static struct irq_chip xen_dynamic_chip __read_mostly
= {
1536 .irq_disable
= disable_dynirq
,
1537 .irq_mask
= disable_dynirq
,
1538 .irq_unmask
= enable_dynirq
,
1540 .irq_eoi
= ack_dynirq
,
1541 .irq_set_affinity
= set_affinity_irq
,
1542 .irq_retrigger
= retrigger_dynirq
,
1545 static struct irq_chip xen_pirq_chip __read_mostly
= {
1548 .irq_startup
= startup_pirq
,
1549 .irq_shutdown
= shutdown_pirq
,
1551 .irq_enable
= enable_pirq
,
1552 .irq_unmask
= enable_pirq
,
1554 .irq_disable
= disable_pirq
,
1555 .irq_mask
= disable_pirq
,
1557 .irq_ack
= ack_pirq
,
1559 .irq_set_affinity
= set_affinity_irq
,
1561 .irq_retrigger
= retrigger_dynirq
,
1564 static struct irq_chip xen_percpu_chip __read_mostly
= {
1565 .name
= "xen-percpu",
1567 .irq_disable
= disable_dynirq
,
1568 .irq_mask
= disable_dynirq
,
1569 .irq_unmask
= enable_dynirq
,
1571 .irq_ack
= ack_dynirq
,
1574 int xen_set_callback_via(uint64_t via
)
1576 struct xen_hvm_param a
;
1577 a
.domid
= DOMID_SELF
;
1578 a
.index
= HVM_PARAM_CALLBACK_IRQ
;
1580 return HYPERVISOR_hvm_op(HVMOP_set_param
, &a
);
1582 EXPORT_SYMBOL_GPL(xen_set_callback_via
);
1584 #ifdef CONFIG_XEN_PVHVM
1585 /* Vector callbacks are better than PCI interrupts to receive event
1586 * channel notifications because we can receive vector callbacks on any
1587 * vcpu and we don't need PCI support or APIC interactions. */
1588 void xen_callback_vector(void)
1591 uint64_t callback_via
;
1592 if (xen_have_vector_callback
) {
1593 callback_via
= HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK
);
1594 rc
= xen_set_callback_via(callback_via
);
1596 printk(KERN_ERR
"Request for Xen HVM callback vector"
1598 xen_have_vector_callback
= 0;
1601 printk(KERN_INFO
"Xen HVM callback vector for event delivery is "
1603 /* in the restore case the vector has already been allocated */
1604 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK
, used_vectors
))
1605 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK
, xen_hvm_callback_vector
);
1609 void xen_callback_vector(void) {}
1612 void __init
xen_init_IRQ(void)
1616 evtchn_to_irq
= kcalloc(NR_EVENT_CHANNELS
, sizeof(*evtchn_to_irq
),
1618 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1619 evtchn_to_irq
[i
] = -1;
1621 init_evtchn_cpu_bindings();
1623 /* No event channels are 'live' right now. */
1624 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1627 if (xen_hvm_domain()) {
1628 xen_callback_vector();
1630 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1631 * __acpi_register_gsi can point at the right function */
1634 irq_ctx_init(smp_processor_id());
1635 if (xen_initial_domain())