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
;
119 static unsigned long *pirq_eoi_map
;
121 static bool (*pirq_needs_eoi
)(unsigned irq
);
124 * Note sizeof(xen_ulong_t) can be more than sizeof(unsigned long). Be
125 * careful to only use bitops which allow for this (e.g
126 * test_bit/find_first_bit and friends but not __ffs) and to pass
127 * BITS_PER_EVTCHN_WORD as the bitmask length.
129 #define BITS_PER_EVTCHN_WORD (sizeof(xen_ulong_t)*8)
131 * Make a bitmask (i.e. unsigned long *) of a xen_ulong_t
132 * array. Primarily to avoid long lines (hence the terse name).
134 #define BM(x) (unsigned long *)(x)
135 /* Find the first set bit in a evtchn mask */
136 #define EVTCHN_FIRST_BIT(w) find_first_bit(BM(&(w)), BITS_PER_EVTCHN_WORD)
138 static DEFINE_PER_CPU(xen_ulong_t
[NR_EVENT_CHANNELS
/BITS_PER_EVTCHN_WORD
],
141 /* Xen will never allocate port zero for any purpose. */
142 #define VALID_EVTCHN(chn) ((chn) != 0)
144 static struct irq_chip xen_dynamic_chip
;
145 static struct irq_chip xen_percpu_chip
;
146 static struct irq_chip xen_pirq_chip
;
147 static void enable_dynirq(struct irq_data
*data
);
148 static void disable_dynirq(struct irq_data
*data
);
150 /* Get info for IRQ */
151 static struct irq_info
*info_for_irq(unsigned irq
)
153 return irq_get_handler_data(irq
);
156 /* Constructors for packed IRQ information. */
157 static void xen_irq_info_common_init(struct irq_info
*info
,
159 enum xen_irq_type type
,
160 unsigned short evtchn
,
164 BUG_ON(info
->type
!= IRQT_UNBOUND
&& info
->type
!= type
);
168 info
->evtchn
= evtchn
;
171 evtchn_to_irq
[evtchn
] = irq
;
174 static void xen_irq_info_evtchn_init(unsigned irq
,
175 unsigned short evtchn
)
177 struct irq_info
*info
= info_for_irq(irq
);
179 xen_irq_info_common_init(info
, irq
, IRQT_EVTCHN
, evtchn
, 0);
182 static void xen_irq_info_ipi_init(unsigned cpu
,
184 unsigned short evtchn
,
187 struct irq_info
*info
= info_for_irq(irq
);
189 xen_irq_info_common_init(info
, irq
, IRQT_IPI
, evtchn
, 0);
193 per_cpu(ipi_to_irq
, cpu
)[ipi
] = irq
;
196 static void xen_irq_info_virq_init(unsigned cpu
,
198 unsigned short evtchn
,
201 struct irq_info
*info
= info_for_irq(irq
);
203 xen_irq_info_common_init(info
, irq
, IRQT_VIRQ
, evtchn
, 0);
207 per_cpu(virq_to_irq
, cpu
)[virq
] = irq
;
210 static void xen_irq_info_pirq_init(unsigned irq
,
211 unsigned short evtchn
,
214 unsigned short vector
,
218 struct irq_info
*info
= info_for_irq(irq
);
220 xen_irq_info_common_init(info
, irq
, IRQT_PIRQ
, evtchn
, 0);
222 info
->u
.pirq
.pirq
= pirq
;
223 info
->u
.pirq
.gsi
= gsi
;
224 info
->u
.pirq
.vector
= vector
;
225 info
->u
.pirq
.domid
= domid
;
226 info
->u
.pirq
.flags
= flags
;
230 * Accessors for packed IRQ information.
232 static unsigned int evtchn_from_irq(unsigned irq
)
234 if (unlikely(WARN(irq
< 0 || irq
>= nr_irqs
, "Invalid irq %d!\n", irq
)))
237 return info_for_irq(irq
)->evtchn
;
240 unsigned irq_from_evtchn(unsigned int evtchn
)
242 return evtchn_to_irq
[evtchn
];
244 EXPORT_SYMBOL_GPL(irq_from_evtchn
);
246 static enum ipi_vector
ipi_from_irq(unsigned irq
)
248 struct irq_info
*info
= info_for_irq(irq
);
250 BUG_ON(info
== NULL
);
251 BUG_ON(info
->type
!= IRQT_IPI
);
256 static unsigned virq_from_irq(unsigned irq
)
258 struct irq_info
*info
= info_for_irq(irq
);
260 BUG_ON(info
== NULL
);
261 BUG_ON(info
->type
!= IRQT_VIRQ
);
266 static unsigned pirq_from_irq(unsigned irq
)
268 struct irq_info
*info
= info_for_irq(irq
);
270 BUG_ON(info
== NULL
);
271 BUG_ON(info
->type
!= IRQT_PIRQ
);
273 return info
->u
.pirq
.pirq
;
276 static enum xen_irq_type
type_from_irq(unsigned irq
)
278 return info_for_irq(irq
)->type
;
281 static unsigned cpu_from_irq(unsigned irq
)
283 return info_for_irq(irq
)->cpu
;
286 static unsigned int cpu_from_evtchn(unsigned int evtchn
)
288 int irq
= evtchn_to_irq
[evtchn
];
292 ret
= cpu_from_irq(irq
);
298 static bool pirq_check_eoi_map(unsigned irq
)
300 return test_bit(pirq_from_irq(irq
), pirq_eoi_map
);
304 static bool pirq_needs_eoi_flag(unsigned irq
)
306 struct irq_info
*info
= info_for_irq(irq
);
307 BUG_ON(info
->type
!= IRQT_PIRQ
);
309 return info
->u
.pirq
.flags
& PIRQ_NEEDS_EOI
;
312 static inline xen_ulong_t
active_evtchns(unsigned int cpu
,
313 struct shared_info
*sh
,
316 return sh
->evtchn_pending
[idx
] &
317 per_cpu(cpu_evtchn_mask
, cpu
)[idx
] &
318 ~sh
->evtchn_mask
[idx
];
321 static void bind_evtchn_to_cpu(unsigned int chn
, unsigned int cpu
)
323 int irq
= evtchn_to_irq
[chn
];
327 cpumask_copy(irq_to_desc(irq
)->irq_data
.affinity
, cpumask_of(cpu
));
330 clear_bit(chn
, BM(per_cpu(cpu_evtchn_mask
, cpu_from_irq(irq
))));
331 set_bit(chn
, BM(per_cpu(cpu_evtchn_mask
, cpu
)));
333 info_for_irq(irq
)->cpu
= cpu
;
336 static void init_evtchn_cpu_bindings(void)
340 struct irq_info
*info
;
342 /* By default all event channels notify CPU#0. */
343 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
344 struct irq_desc
*desc
= irq_to_desc(info
->irq
);
345 cpumask_copy(desc
->irq_data
.affinity
, cpumask_of(0));
349 for_each_possible_cpu(i
)
350 memset(per_cpu(cpu_evtchn_mask
, i
),
351 (i
== 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask
, i
)));
354 static inline void clear_evtchn(int port
)
356 struct shared_info
*s
= HYPERVISOR_shared_info
;
357 sync_clear_bit(port
, BM(&s
->evtchn_pending
[0]));
360 static inline void set_evtchn(int port
)
362 struct shared_info
*s
= HYPERVISOR_shared_info
;
363 sync_set_bit(port
, BM(&s
->evtchn_pending
[0]));
366 static inline int test_evtchn(int port
)
368 struct shared_info
*s
= HYPERVISOR_shared_info
;
369 return sync_test_bit(port
, BM(&s
->evtchn_pending
[0]));
374 * notify_remote_via_irq - send event to remote end of event channel via irq
375 * @irq: irq of event channel to send event to
377 * Unlike notify_remote_via_evtchn(), this is safe to use across
378 * save/restore. Notifications on a broken connection are silently
381 void notify_remote_via_irq(int irq
)
383 int evtchn
= evtchn_from_irq(irq
);
385 if (VALID_EVTCHN(evtchn
))
386 notify_remote_via_evtchn(evtchn
);
388 EXPORT_SYMBOL_GPL(notify_remote_via_irq
);
390 static void mask_evtchn(int port
)
392 struct shared_info
*s
= HYPERVISOR_shared_info
;
393 sync_set_bit(port
, BM(&s
->evtchn_mask
[0]));
396 static void unmask_evtchn(int port
)
398 struct shared_info
*s
= HYPERVISOR_shared_info
;
399 unsigned int cpu
= get_cpu();
400 int do_hypercall
= 0, evtchn_pending
= 0;
402 BUG_ON(!irqs_disabled());
404 if (unlikely((cpu
!= cpu_from_evtchn(port
))))
407 evtchn_pending
= sync_test_bit(port
, BM(&s
->evtchn_pending
[0]));
409 if (unlikely(evtchn_pending
&& xen_hvm_domain()))
412 /* Slow path (hypercall) if this is a non-local port or if this is
413 * an hvm domain and an event is pending (hvm domains don't have
414 * their own implementation of irq_enable). */
416 struct evtchn_unmask unmask
= { .port
= port
};
417 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask
, &unmask
);
419 struct vcpu_info
*vcpu_info
= __this_cpu_read(xen_vcpu
);
421 sync_clear_bit(port
, BM(&s
->evtchn_mask
[0]));
424 * The following is basically the equivalent of
425 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
426 * the interrupt edge' if the channel is masked.
428 if (evtchn_pending
&&
429 !sync_test_and_set_bit(port
/ BITS_PER_EVTCHN_WORD
,
430 BM(&vcpu_info
->evtchn_pending_sel
)))
431 vcpu_info
->evtchn_upcall_pending
= 1;
437 static void xen_irq_init(unsigned irq
)
439 struct irq_info
*info
;
441 struct irq_desc
*desc
= irq_to_desc(irq
);
443 /* By default all event channels notify CPU#0. */
444 cpumask_copy(desc
->irq_data
.affinity
, cpumask_of(0));
447 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
449 panic("Unable to allocate metadata for IRQ%d\n", irq
);
451 info
->type
= IRQT_UNBOUND
;
454 irq_set_handler_data(irq
, info
);
456 list_add_tail(&info
->list
, &xen_irq_list_head
);
459 static int __must_check
xen_allocate_irq_dynamic(void)
464 #ifdef CONFIG_X86_IO_APIC
466 * For an HVM guest or domain 0 which see "real" (emulated or
467 * actual respectively) GSIs we allocate dynamic IRQs
468 * e.g. those corresponding to event channels or MSIs
469 * etc. from the range above those "real" GSIs to avoid
472 if (xen_initial_domain() || xen_hvm_domain())
473 first
= get_nr_irqs_gsi();
476 irq
= irq_alloc_desc_from(first
, -1);
484 static int __must_check
xen_allocate_irq_gsi(unsigned gsi
)
489 * A PV guest has no concept of a GSI (since it has no ACPI
490 * nor access to/knowledge of the physical APICs). Therefore
491 * all IRQs are dynamically allocated from the entire IRQ
494 if (xen_pv_domain() && !xen_initial_domain())
495 return xen_allocate_irq_dynamic();
497 /* Legacy IRQ descriptors are already allocated by the arch. */
498 if (gsi
< NR_IRQS_LEGACY
)
501 irq
= irq_alloc_desc_at(gsi
, -1);
508 static void xen_free_irq(unsigned irq
)
510 struct irq_info
*info
= irq_get_handler_data(irq
);
512 list_del(&info
->list
);
514 irq_set_handler_data(irq
, NULL
);
516 WARN_ON(info
->refcnt
> 0);
520 /* Legacy IRQ descriptors are managed by the arch. */
521 if (irq
< NR_IRQS_LEGACY
)
527 static void pirq_query_unmask(int irq
)
529 struct physdev_irq_status_query irq_status
;
530 struct irq_info
*info
= info_for_irq(irq
);
532 BUG_ON(info
->type
!= IRQT_PIRQ
);
534 irq_status
.irq
= pirq_from_irq(irq
);
535 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query
, &irq_status
))
536 irq_status
.flags
= 0;
538 info
->u
.pirq
.flags
&= ~PIRQ_NEEDS_EOI
;
539 if (irq_status
.flags
& XENIRQSTAT_needs_eoi
)
540 info
->u
.pirq
.flags
|= PIRQ_NEEDS_EOI
;
543 static bool probing_irq(int irq
)
545 struct irq_desc
*desc
= irq_to_desc(irq
);
547 return desc
&& desc
->action
== NULL
;
550 static void eoi_pirq(struct irq_data
*data
)
552 int evtchn
= evtchn_from_irq(data
->irq
);
553 struct physdev_eoi eoi
= { .irq
= pirq_from_irq(data
->irq
) };
558 if (VALID_EVTCHN(evtchn
))
559 clear_evtchn(evtchn
);
561 if (pirq_needs_eoi(data
->irq
)) {
562 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_eoi
, &eoi
);
567 static void mask_ack_pirq(struct irq_data
*data
)
569 disable_dynirq(data
);
573 static unsigned int __startup_pirq(unsigned int irq
)
575 struct evtchn_bind_pirq bind_pirq
;
576 struct irq_info
*info
= info_for_irq(irq
);
577 int evtchn
= evtchn_from_irq(irq
);
580 BUG_ON(info
->type
!= IRQT_PIRQ
);
582 if (VALID_EVTCHN(evtchn
))
585 bind_pirq
.pirq
= pirq_from_irq(irq
);
586 /* NB. We are happy to share unless we are probing. */
587 bind_pirq
.flags
= info
->u
.pirq
.flags
& PIRQ_SHAREABLE
?
588 BIND_PIRQ__WILL_SHARE
: 0;
589 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq
, &bind_pirq
);
591 if (!probing_irq(irq
))
592 printk(KERN_INFO
"Failed to obtain physical IRQ %d\n",
596 evtchn
= bind_pirq
.port
;
598 pirq_query_unmask(irq
);
600 evtchn_to_irq
[evtchn
] = irq
;
601 bind_evtchn_to_cpu(evtchn
, 0);
602 info
->evtchn
= evtchn
;
605 unmask_evtchn(evtchn
);
606 eoi_pirq(irq_get_irq_data(irq
));
611 static unsigned int startup_pirq(struct irq_data
*data
)
613 return __startup_pirq(data
->irq
);
616 static void shutdown_pirq(struct irq_data
*data
)
618 struct evtchn_close close
;
619 unsigned int irq
= data
->irq
;
620 struct irq_info
*info
= info_for_irq(irq
);
621 int evtchn
= evtchn_from_irq(irq
);
623 BUG_ON(info
->type
!= IRQT_PIRQ
);
625 if (!VALID_EVTCHN(evtchn
))
631 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
634 bind_evtchn_to_cpu(evtchn
, 0);
635 evtchn_to_irq
[evtchn
] = -1;
639 static void enable_pirq(struct irq_data
*data
)
644 static void disable_pirq(struct irq_data
*data
)
646 disable_dynirq(data
);
649 int xen_irq_from_gsi(unsigned gsi
)
651 struct irq_info
*info
;
653 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
654 if (info
->type
!= IRQT_PIRQ
)
657 if (info
->u
.pirq
.gsi
== gsi
)
663 EXPORT_SYMBOL_GPL(xen_irq_from_gsi
);
666 * Do not make any assumptions regarding the relationship between the
667 * IRQ number returned here and the Xen pirq argument.
669 * Note: We don't assign an event channel until the irq actually started
670 * up. Return an existing irq if we've already got one for the gsi.
672 * Shareable implies level triggered, not shareable implies edge
675 int xen_bind_pirq_gsi_to_irq(unsigned gsi
,
676 unsigned pirq
, int shareable
, char *name
)
679 struct physdev_irq irq_op
;
681 mutex_lock(&irq_mapping_update_lock
);
683 irq
= xen_irq_from_gsi(gsi
);
685 printk(KERN_INFO
"xen_map_pirq_gsi: returning irq %d for gsi %u\n",
690 irq
= xen_allocate_irq_gsi(gsi
);
697 /* Only the privileged domain can do this. For non-priv, the pcifront
698 * driver provides a PCI bus that does the call to do exactly
699 * this in the priv domain. */
700 if (xen_initial_domain() &&
701 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector
, &irq_op
)) {
707 xen_irq_info_pirq_init(irq
, 0, pirq
, gsi
, irq_op
.vector
, DOMID_SELF
,
708 shareable
? PIRQ_SHAREABLE
: 0);
710 pirq_query_unmask(irq
);
711 /* We try to use the handler with the appropriate semantic for the
712 * type of interrupt: if the interrupt is an edge triggered
713 * interrupt we use handle_edge_irq.
715 * On the other hand if the interrupt is level triggered we use
716 * handle_fasteoi_irq like the native code does for this kind of
719 * Depending on the Xen version, pirq_needs_eoi might return true
720 * not only for level triggered interrupts but for edge triggered
721 * interrupts too. In any case Xen always honors the eoi mechanism,
722 * not injecting any more pirqs of the same kind if the first one
723 * hasn't received an eoi yet. Therefore using the fasteoi handler
724 * is the right choice either way.
727 irq_set_chip_and_handler_name(irq
, &xen_pirq_chip
,
728 handle_fasteoi_irq
, name
);
730 irq_set_chip_and_handler_name(irq
, &xen_pirq_chip
,
731 handle_edge_irq
, name
);
734 mutex_unlock(&irq_mapping_update_lock
);
739 #ifdef CONFIG_PCI_MSI
740 int xen_allocate_pirq_msi(struct pci_dev
*dev
, struct msi_desc
*msidesc
)
743 struct physdev_get_free_pirq op_get_free_pirq
;
745 op_get_free_pirq
.type
= MAP_PIRQ_TYPE_MSI
;
746 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq
, &op_get_free_pirq
);
748 WARN_ONCE(rc
== -ENOSYS
,
749 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
751 return rc
? -1 : op_get_free_pirq
.pirq
;
754 int xen_bind_pirq_msi_to_irq(struct pci_dev
*dev
, struct msi_desc
*msidesc
,
755 int pirq
, int vector
, const char *name
,
760 mutex_lock(&irq_mapping_update_lock
);
762 irq
= xen_allocate_irq_dynamic();
766 irq_set_chip_and_handler_name(irq
, &xen_pirq_chip
, handle_edge_irq
,
769 xen_irq_info_pirq_init(irq
, 0, pirq
, 0, vector
, domid
, 0);
770 ret
= irq_set_msi_desc(irq
, msidesc
);
774 mutex_unlock(&irq_mapping_update_lock
);
777 mutex_unlock(&irq_mapping_update_lock
);
783 int xen_destroy_irq(int irq
)
785 struct irq_desc
*desc
;
786 struct physdev_unmap_pirq unmap_irq
;
787 struct irq_info
*info
= info_for_irq(irq
);
790 mutex_lock(&irq_mapping_update_lock
);
792 desc
= irq_to_desc(irq
);
796 if (xen_initial_domain()) {
797 unmap_irq
.pirq
= info
->u
.pirq
.pirq
;
798 unmap_irq
.domid
= info
->u
.pirq
.domid
;
799 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq
, &unmap_irq
);
800 /* If another domain quits without making the pci_disable_msix
801 * call, the Xen hypervisor takes care of freeing the PIRQs
802 * (free_domain_pirqs).
804 if ((rc
== -ESRCH
&& info
->u
.pirq
.domid
!= DOMID_SELF
))
805 printk(KERN_INFO
"domain %d does not have %d anymore\n",
806 info
->u
.pirq
.domid
, info
->u
.pirq
.pirq
);
808 printk(KERN_WARNING
"unmap irq failed %d\n", rc
);
816 mutex_unlock(&irq_mapping_update_lock
);
820 int xen_irq_from_pirq(unsigned pirq
)
824 struct irq_info
*info
;
826 mutex_lock(&irq_mapping_update_lock
);
828 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
829 if (info
->type
!= IRQT_PIRQ
)
832 if (info
->u
.pirq
.pirq
== pirq
)
837 mutex_unlock(&irq_mapping_update_lock
);
843 int xen_pirq_from_irq(unsigned irq
)
845 return pirq_from_irq(irq
);
847 EXPORT_SYMBOL_GPL(xen_pirq_from_irq
);
848 int bind_evtchn_to_irq(unsigned int evtchn
)
852 mutex_lock(&irq_mapping_update_lock
);
854 irq
= evtchn_to_irq
[evtchn
];
857 irq
= xen_allocate_irq_dynamic();
861 irq_set_chip_and_handler_name(irq
, &xen_dynamic_chip
,
862 handle_edge_irq
, "event");
864 xen_irq_info_evtchn_init(irq
, evtchn
);
866 struct irq_info
*info
= info_for_irq(irq
);
867 WARN_ON(info
== NULL
|| info
->type
!= IRQT_EVTCHN
);
869 irq_clear_status_flags(irq
, IRQ_NOREQUEST
|IRQ_NOAUTOEN
);
872 mutex_unlock(&irq_mapping_update_lock
);
876 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq
);
878 static int bind_ipi_to_irq(unsigned int ipi
, unsigned int cpu
)
880 struct evtchn_bind_ipi bind_ipi
;
883 mutex_lock(&irq_mapping_update_lock
);
885 irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
];
888 irq
= xen_allocate_irq_dynamic();
892 irq_set_chip_and_handler_name(irq
, &xen_percpu_chip
,
893 handle_percpu_irq
, "ipi");
896 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
899 evtchn
= bind_ipi
.port
;
901 xen_irq_info_ipi_init(cpu
, irq
, evtchn
, ipi
);
903 bind_evtchn_to_cpu(evtchn
, cpu
);
905 struct irq_info
*info
= info_for_irq(irq
);
906 WARN_ON(info
== NULL
|| info
->type
!= IRQT_IPI
);
910 mutex_unlock(&irq_mapping_update_lock
);
914 static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain
,
915 unsigned int remote_port
)
917 struct evtchn_bind_interdomain bind_interdomain
;
920 bind_interdomain
.remote_dom
= remote_domain
;
921 bind_interdomain
.remote_port
= remote_port
;
923 err
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain
,
926 return err
? : bind_evtchn_to_irq(bind_interdomain
.local_port
);
929 static int find_virq(unsigned int virq
, unsigned int cpu
)
931 struct evtchn_status status
;
932 int port
, rc
= -ENOENT
;
934 memset(&status
, 0, sizeof(status
));
935 for (port
= 0; port
<= NR_EVENT_CHANNELS
; port
++) {
936 status
.dom
= DOMID_SELF
;
938 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_status
, &status
);
941 if (status
.status
!= EVTCHNSTAT_virq
)
943 if (status
.u
.virq
== virq
&& status
.vcpu
== cpu
) {
951 int bind_virq_to_irq(unsigned int virq
, unsigned int cpu
)
953 struct evtchn_bind_virq bind_virq
;
954 int evtchn
, irq
, ret
;
956 mutex_lock(&irq_mapping_update_lock
);
958 irq
= per_cpu(virq_to_irq
, cpu
)[virq
];
961 irq
= xen_allocate_irq_dynamic();
965 irq_set_chip_and_handler_name(irq
, &xen_percpu_chip
,
966 handle_percpu_irq
, "virq");
968 bind_virq
.virq
= virq
;
969 bind_virq
.vcpu
= cpu
;
970 ret
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
973 evtchn
= bind_virq
.port
;
976 ret
= find_virq(virq
, cpu
);
981 xen_irq_info_virq_init(cpu
, irq
, evtchn
, virq
);
983 bind_evtchn_to_cpu(evtchn
, cpu
);
985 struct irq_info
*info
= info_for_irq(irq
);
986 WARN_ON(info
== NULL
|| info
->type
!= IRQT_VIRQ
);
990 mutex_unlock(&irq_mapping_update_lock
);
995 static void unbind_from_irq(unsigned int irq
)
997 struct evtchn_close close
;
998 int evtchn
= evtchn_from_irq(irq
);
999 struct irq_info
*info
= irq_get_handler_data(irq
);
1001 mutex_lock(&irq_mapping_update_lock
);
1003 if (info
->refcnt
> 0) {
1005 if (info
->refcnt
!= 0)
1009 if (VALID_EVTCHN(evtchn
)) {
1010 close
.port
= evtchn
;
1011 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
1014 switch (type_from_irq(irq
)) {
1016 per_cpu(virq_to_irq
, cpu_from_evtchn(evtchn
))
1017 [virq_from_irq(irq
)] = -1;
1020 per_cpu(ipi_to_irq
, cpu_from_evtchn(evtchn
))
1021 [ipi_from_irq(irq
)] = -1;
1027 /* Closed ports are implicitly re-bound to VCPU0. */
1028 bind_evtchn_to_cpu(evtchn
, 0);
1030 evtchn_to_irq
[evtchn
] = -1;
1033 BUG_ON(info_for_irq(irq
)->type
== IRQT_UNBOUND
);
1038 mutex_unlock(&irq_mapping_update_lock
);
1041 int bind_evtchn_to_irqhandler(unsigned int evtchn
,
1042 irq_handler_t handler
,
1043 unsigned long irqflags
,
1044 const char *devname
, void *dev_id
)
1048 irq
= bind_evtchn_to_irq(evtchn
);
1051 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1053 unbind_from_irq(irq
);
1059 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler
);
1061 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain
,
1062 unsigned int remote_port
,
1063 irq_handler_t handler
,
1064 unsigned long irqflags
,
1065 const char *devname
,
1070 irq
= bind_interdomain_evtchn_to_irq(remote_domain
, remote_port
);
1074 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1076 unbind_from_irq(irq
);
1082 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler
);
1084 int bind_virq_to_irqhandler(unsigned int virq
, unsigned int cpu
,
1085 irq_handler_t handler
,
1086 unsigned long irqflags
, const char *devname
, void *dev_id
)
1090 irq
= bind_virq_to_irq(virq
, cpu
);
1093 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1095 unbind_from_irq(irq
);
1101 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler
);
1103 int bind_ipi_to_irqhandler(enum ipi_vector ipi
,
1105 irq_handler_t handler
,
1106 unsigned long irqflags
,
1107 const char *devname
,
1112 irq
= bind_ipi_to_irq(ipi
, cpu
);
1116 irqflags
|= IRQF_NO_SUSPEND
| IRQF_FORCE_RESUME
| IRQF_EARLY_RESUME
;
1117 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1119 unbind_from_irq(irq
);
1126 void unbind_from_irqhandler(unsigned int irq
, void *dev_id
)
1128 free_irq(irq
, dev_id
);
1129 unbind_from_irq(irq
);
1131 EXPORT_SYMBOL_GPL(unbind_from_irqhandler
);
1133 int evtchn_make_refcounted(unsigned int evtchn
)
1135 int irq
= evtchn_to_irq
[evtchn
];
1136 struct irq_info
*info
;
1141 info
= irq_get_handler_data(irq
);
1146 WARN_ON(info
->refcnt
!= -1);
1152 EXPORT_SYMBOL_GPL(evtchn_make_refcounted
);
1154 int evtchn_get(unsigned int evtchn
)
1157 struct irq_info
*info
;
1160 if (evtchn
>= NR_EVENT_CHANNELS
)
1163 mutex_lock(&irq_mapping_update_lock
);
1165 irq
= evtchn_to_irq
[evtchn
];
1169 info
= irq_get_handler_data(irq
);
1175 if (info
->refcnt
<= 0)
1181 mutex_unlock(&irq_mapping_update_lock
);
1185 EXPORT_SYMBOL_GPL(evtchn_get
);
1187 void evtchn_put(unsigned int evtchn
)
1189 int irq
= evtchn_to_irq
[evtchn
];
1190 if (WARN_ON(irq
== -1))
1192 unbind_from_irq(irq
);
1194 EXPORT_SYMBOL_GPL(evtchn_put
);
1196 void xen_send_IPI_one(unsigned int cpu
, enum ipi_vector vector
)
1198 int irq
= per_cpu(ipi_to_irq
, cpu
)[vector
];
1200 notify_remote_via_irq(irq
);
1203 irqreturn_t
xen_debug_interrupt(int irq
, void *dev_id
)
1205 struct shared_info
*sh
= HYPERVISOR_shared_info
;
1206 int cpu
= smp_processor_id();
1207 xen_ulong_t
*cpu_evtchn
= per_cpu(cpu_evtchn_mask
, cpu
);
1209 unsigned long flags
;
1210 static DEFINE_SPINLOCK(debug_lock
);
1211 struct vcpu_info
*v
;
1213 spin_lock_irqsave(&debug_lock
, flags
);
1215 printk("\nvcpu %d\n ", cpu
);
1217 for_each_online_cpu(i
) {
1219 v
= per_cpu(xen_vcpu
, i
);
1220 pending
= (get_irq_regs() && i
== cpu
)
1221 ? xen_irqs_disabled(get_irq_regs())
1222 : v
->evtchn_upcall_mask
;
1223 printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong
"\n ", i
,
1224 pending
, v
->evtchn_upcall_pending
,
1225 (int)(sizeof(v
->evtchn_pending_sel
)*2),
1226 v
->evtchn_pending_sel
);
1228 v
= per_cpu(xen_vcpu
, cpu
);
1230 printk("\npending:\n ");
1231 for (i
= ARRAY_SIZE(sh
->evtchn_pending
)-1; i
>= 0; i
--)
1232 printk("%0*"PRI_xen_ulong
"%s",
1233 (int)sizeof(sh
->evtchn_pending
[0])*2,
1234 sh
->evtchn_pending
[i
],
1235 i
% 8 == 0 ? "\n " : " ");
1236 printk("\nglobal mask:\n ");
1237 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
1238 printk("%0*"PRI_xen_ulong
"%s",
1239 (int)(sizeof(sh
->evtchn_mask
[0])*2),
1241 i
% 8 == 0 ? "\n " : " ");
1243 printk("\nglobally unmasked:\n ");
1244 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
1245 printk("%0*"PRI_xen_ulong
"%s",
1246 (int)(sizeof(sh
->evtchn_mask
[0])*2),
1247 sh
->evtchn_pending
[i
] & ~sh
->evtchn_mask
[i
],
1248 i
% 8 == 0 ? "\n " : " ");
1250 printk("\nlocal cpu%d mask:\n ", cpu
);
1251 for (i
= (NR_EVENT_CHANNELS
/BITS_PER_EVTCHN_WORD
)-1; i
>= 0; i
--)
1252 printk("%0*"PRI_xen_ulong
"%s", (int)(sizeof(cpu_evtchn
[0])*2),
1254 i
% 8 == 0 ? "\n " : " ");
1256 printk("\nlocally unmasked:\n ");
1257 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--) {
1258 xen_ulong_t pending
= sh
->evtchn_pending
[i
]
1259 & ~sh
->evtchn_mask
[i
]
1261 printk("%0*"PRI_xen_ulong
"%s",
1262 (int)(sizeof(sh
->evtchn_mask
[0])*2),
1263 pending
, i
% 8 == 0 ? "\n " : " ");
1266 printk("\npending list:\n");
1267 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++) {
1268 if (sync_test_bit(i
, BM(sh
->evtchn_pending
))) {
1269 int word_idx
= i
/ BITS_PER_EVTCHN_WORD
;
1270 printk(" %d: event %d -> irq %d%s%s%s\n",
1271 cpu_from_evtchn(i
), i
,
1273 sync_test_bit(word_idx
, BM(&v
->evtchn_pending_sel
))
1275 !sync_test_bit(i
, BM(sh
->evtchn_mask
))
1276 ? "" : " globally-masked",
1277 sync_test_bit(i
, BM(cpu_evtchn
))
1278 ? "" : " locally-masked");
1282 spin_unlock_irqrestore(&debug_lock
, flags
);
1287 static DEFINE_PER_CPU(unsigned, xed_nesting_count
);
1288 static DEFINE_PER_CPU(unsigned int, current_word_idx
);
1289 static DEFINE_PER_CPU(unsigned int, current_bit_idx
);
1292 * Mask out the i least significant bits of w
1294 #define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
1297 * Search the CPUs pending events bitmasks. For each one found, map
1298 * the event number to an irq, and feed it into do_IRQ() for
1301 * Xen uses a two-level bitmap to speed searching. The first level is
1302 * a bitset of words which contain pending event bits. The second
1303 * level is a bitset of pending events themselves.
1305 static void __xen_evtchn_do_upcall(void)
1307 int start_word_idx
, start_bit_idx
;
1308 int word_idx
, bit_idx
;
1310 int cpu
= get_cpu();
1311 struct shared_info
*s
= HYPERVISOR_shared_info
;
1312 struct vcpu_info
*vcpu_info
= __this_cpu_read(xen_vcpu
);
1316 xen_ulong_t pending_words
;
1318 vcpu_info
->evtchn_upcall_pending
= 0;
1320 if (__this_cpu_inc_return(xed_nesting_count
) - 1)
1324 * Master flag must be cleared /before/ clearing
1325 * selector flag. xchg_xen_ulong must contain an
1326 * appropriate barrier.
1328 pending_words
= xchg_xen_ulong(&vcpu_info
->evtchn_pending_sel
, 0);
1330 start_word_idx
= __this_cpu_read(current_word_idx
);
1331 start_bit_idx
= __this_cpu_read(current_bit_idx
);
1333 word_idx
= start_word_idx
;
1335 for (i
= 0; pending_words
!= 0; i
++) {
1336 xen_ulong_t pending_bits
;
1339 words
= MASK_LSBS(pending_words
, word_idx
);
1342 * If we masked out all events, wrap to beginning.
1349 word_idx
= EVTCHN_FIRST_BIT(words
);
1351 pending_bits
= active_evtchns(cpu
, s
, word_idx
);
1352 bit_idx
= 0; /* usually scan entire word from start */
1353 if (word_idx
== start_word_idx
) {
1354 /* We scan the starting word in two parts */
1356 /* 1st time: start in the middle */
1357 bit_idx
= start_bit_idx
;
1359 /* 2nd time: mask bits done already */
1360 bit_idx
&= (1UL << start_bit_idx
) - 1;
1366 struct irq_desc
*desc
;
1368 bits
= MASK_LSBS(pending_bits
, bit_idx
);
1370 /* If we masked out all events, move on. */
1374 bit_idx
= EVTCHN_FIRST_BIT(bits
);
1377 port
= (word_idx
* BITS_PER_EVTCHN_WORD
) + bit_idx
;
1378 irq
= evtchn_to_irq
[port
];
1381 desc
= irq_to_desc(irq
);
1383 generic_handle_irq_desc(irq
, desc
);
1386 bit_idx
= (bit_idx
+ 1) % BITS_PER_EVTCHN_WORD
;
1388 /* Next caller starts at last processed + 1 */
1389 __this_cpu_write(current_word_idx
,
1390 bit_idx
? word_idx
:
1391 (word_idx
+1) % BITS_PER_EVTCHN_WORD
);
1392 __this_cpu_write(current_bit_idx
, bit_idx
);
1393 } while (bit_idx
!= 0);
1395 /* Scan start_l1i twice; all others once. */
1396 if ((word_idx
!= start_word_idx
) || (i
!= 0))
1397 pending_words
&= ~(1UL << word_idx
);
1399 word_idx
= (word_idx
+ 1) % BITS_PER_EVTCHN_WORD
;
1402 BUG_ON(!irqs_disabled());
1404 count
= __this_cpu_read(xed_nesting_count
);
1405 __this_cpu_write(xed_nesting_count
, 0);
1406 } while (count
!= 1 || vcpu_info
->evtchn_upcall_pending
);
1413 void xen_evtchn_do_upcall(struct pt_regs
*regs
)
1415 struct pt_regs
*old_regs
= set_irq_regs(regs
);
1422 __xen_evtchn_do_upcall();
1425 set_irq_regs(old_regs
);
1428 void xen_hvm_evtchn_do_upcall(void)
1430 __xen_evtchn_do_upcall();
1432 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall
);
1434 /* Rebind a new event channel to an existing irq. */
1435 void rebind_evtchn_irq(int evtchn
, int irq
)
1437 struct irq_info
*info
= info_for_irq(irq
);
1439 /* Make sure the irq is masked, since the new event channel
1440 will also be masked. */
1443 mutex_lock(&irq_mapping_update_lock
);
1445 /* After resume the irq<->evtchn mappings are all cleared out */
1446 BUG_ON(evtchn_to_irq
[evtchn
] != -1);
1447 /* Expect irq to have been bound before,
1448 so there should be a proper type */
1449 BUG_ON(info
->type
== IRQT_UNBOUND
);
1451 xen_irq_info_evtchn_init(irq
, evtchn
);
1453 mutex_unlock(&irq_mapping_update_lock
);
1455 /* new event channels are always bound to cpu 0 */
1456 irq_set_affinity(irq
, cpumask_of(0));
1458 /* Unmask the event channel. */
1462 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1463 static int rebind_irq_to_cpu(unsigned irq
, unsigned tcpu
)
1465 struct evtchn_bind_vcpu bind_vcpu
;
1466 int evtchn
= evtchn_from_irq(irq
);
1468 if (!VALID_EVTCHN(evtchn
))
1472 * Events delivered via platform PCI interrupts are always
1473 * routed to vcpu 0 and hence cannot be rebound.
1475 if (xen_hvm_domain() && !xen_have_vector_callback
)
1478 /* Send future instances of this interrupt to other vcpu. */
1479 bind_vcpu
.port
= evtchn
;
1480 bind_vcpu
.vcpu
= tcpu
;
1483 * If this fails, it usually just indicates that we're dealing with a
1484 * virq or IPI channel, which don't actually need to be rebound. Ignore
1485 * it, but don't do the xenlinux-level rebind in that case.
1487 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu
, &bind_vcpu
) >= 0)
1488 bind_evtchn_to_cpu(evtchn
, tcpu
);
1493 static int set_affinity_irq(struct irq_data
*data
, const struct cpumask
*dest
,
1496 unsigned tcpu
= cpumask_first(dest
);
1498 return rebind_irq_to_cpu(data
->irq
, tcpu
);
1501 int resend_irq_on_evtchn(unsigned int irq
)
1503 int masked
, evtchn
= evtchn_from_irq(irq
);
1504 struct shared_info
*s
= HYPERVISOR_shared_info
;
1506 if (!VALID_EVTCHN(evtchn
))
1509 masked
= sync_test_and_set_bit(evtchn
, BM(s
->evtchn_mask
));
1510 sync_set_bit(evtchn
, BM(s
->evtchn_pending
));
1512 unmask_evtchn(evtchn
);
1517 static void enable_dynirq(struct irq_data
*data
)
1519 int evtchn
= evtchn_from_irq(data
->irq
);
1521 if (VALID_EVTCHN(evtchn
))
1522 unmask_evtchn(evtchn
);
1525 static void disable_dynirq(struct irq_data
*data
)
1527 int evtchn
= evtchn_from_irq(data
->irq
);
1529 if (VALID_EVTCHN(evtchn
))
1530 mask_evtchn(evtchn
);
1533 static void ack_dynirq(struct irq_data
*data
)
1535 int evtchn
= evtchn_from_irq(data
->irq
);
1539 if (VALID_EVTCHN(evtchn
))
1540 clear_evtchn(evtchn
);
1543 static void mask_ack_dynirq(struct irq_data
*data
)
1545 disable_dynirq(data
);
1549 static int retrigger_dynirq(struct irq_data
*data
)
1551 int evtchn
= evtchn_from_irq(data
->irq
);
1552 struct shared_info
*sh
= HYPERVISOR_shared_info
;
1555 if (VALID_EVTCHN(evtchn
)) {
1558 masked
= sync_test_and_set_bit(evtchn
, BM(sh
->evtchn_mask
));
1559 sync_set_bit(evtchn
, BM(sh
->evtchn_pending
));
1561 unmask_evtchn(evtchn
);
1568 static void restore_pirqs(void)
1570 int pirq
, rc
, irq
, gsi
;
1571 struct physdev_map_pirq map_irq
;
1572 struct irq_info
*info
;
1574 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
1575 if (info
->type
!= IRQT_PIRQ
)
1578 pirq
= info
->u
.pirq
.pirq
;
1579 gsi
= info
->u
.pirq
.gsi
;
1582 /* save/restore of PT devices doesn't work, so at this point the
1583 * only devices present are GSI based emulated devices */
1587 map_irq
.domid
= DOMID_SELF
;
1588 map_irq
.type
= MAP_PIRQ_TYPE_GSI
;
1589 map_irq
.index
= gsi
;
1590 map_irq
.pirq
= pirq
;
1592 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq
, &map_irq
);
1594 printk(KERN_WARNING
"xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1595 gsi
, irq
, pirq
, rc
);
1600 printk(KERN_DEBUG
"xen: --> irq=%d, pirq=%d\n", irq
, map_irq
.pirq
);
1602 __startup_pirq(irq
);
1606 static void restore_cpu_virqs(unsigned int cpu
)
1608 struct evtchn_bind_virq bind_virq
;
1609 int virq
, irq
, evtchn
;
1611 for (virq
= 0; virq
< NR_VIRQS
; virq
++) {
1612 if ((irq
= per_cpu(virq_to_irq
, cpu
)[virq
]) == -1)
1615 BUG_ON(virq_from_irq(irq
) != virq
);
1617 /* Get a new binding from Xen. */
1618 bind_virq
.virq
= virq
;
1619 bind_virq
.vcpu
= cpu
;
1620 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
1623 evtchn
= bind_virq
.port
;
1625 /* Record the new mapping. */
1626 xen_irq_info_virq_init(cpu
, irq
, evtchn
, virq
);
1627 bind_evtchn_to_cpu(evtchn
, cpu
);
1631 static void restore_cpu_ipis(unsigned int cpu
)
1633 struct evtchn_bind_ipi bind_ipi
;
1634 int ipi
, irq
, evtchn
;
1636 for (ipi
= 0; ipi
< XEN_NR_IPIS
; ipi
++) {
1637 if ((irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
]) == -1)
1640 BUG_ON(ipi_from_irq(irq
) != ipi
);
1642 /* Get a new binding from Xen. */
1643 bind_ipi
.vcpu
= cpu
;
1644 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
1647 evtchn
= bind_ipi
.port
;
1649 /* Record the new mapping. */
1650 xen_irq_info_ipi_init(cpu
, irq
, evtchn
, ipi
);
1651 bind_evtchn_to_cpu(evtchn
, cpu
);
1655 /* Clear an irq's pending state, in preparation for polling on it */
1656 void xen_clear_irq_pending(int irq
)
1658 int evtchn
= evtchn_from_irq(irq
);
1660 if (VALID_EVTCHN(evtchn
))
1661 clear_evtchn(evtchn
);
1663 EXPORT_SYMBOL(xen_clear_irq_pending
);
1664 void xen_set_irq_pending(int irq
)
1666 int evtchn
= evtchn_from_irq(irq
);
1668 if (VALID_EVTCHN(evtchn
))
1672 bool xen_test_irq_pending(int irq
)
1674 int evtchn
= evtchn_from_irq(irq
);
1677 if (VALID_EVTCHN(evtchn
))
1678 ret
= test_evtchn(evtchn
);
1683 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1684 * the irq will be disabled so it won't deliver an interrupt. */
1685 void xen_poll_irq_timeout(int irq
, u64 timeout
)
1687 evtchn_port_t evtchn
= evtchn_from_irq(irq
);
1689 if (VALID_EVTCHN(evtchn
)) {
1690 struct sched_poll poll
;
1693 poll
.timeout
= timeout
;
1694 set_xen_guest_handle(poll
.ports
, &evtchn
);
1696 if (HYPERVISOR_sched_op(SCHEDOP_poll
, &poll
) != 0)
1700 EXPORT_SYMBOL(xen_poll_irq_timeout
);
1701 /* Poll waiting for an irq to become pending. In the usual case, the
1702 * irq will be disabled so it won't deliver an interrupt. */
1703 void xen_poll_irq(int irq
)
1705 xen_poll_irq_timeout(irq
, 0 /* no timeout */);
1708 /* Check whether the IRQ line is shared with other guests. */
1709 int xen_test_irq_shared(int irq
)
1711 struct irq_info
*info
= info_for_irq(irq
);
1712 struct physdev_irq_status_query irq_status
= { .irq
= info
->u
.pirq
.pirq
};
1714 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query
, &irq_status
))
1716 return !(irq_status
.flags
& XENIRQSTAT_shared
);
1718 EXPORT_SYMBOL_GPL(xen_test_irq_shared
);
1720 void xen_irq_resume(void)
1722 unsigned int cpu
, evtchn
;
1723 struct irq_info
*info
;
1725 init_evtchn_cpu_bindings();
1727 /* New event-channel space is not 'live' yet. */
1728 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1729 mask_evtchn(evtchn
);
1731 /* No IRQ <-> event-channel mappings. */
1732 list_for_each_entry(info
, &xen_irq_list_head
, list
)
1733 info
->evtchn
= 0; /* zap event-channel binding */
1735 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1736 evtchn_to_irq
[evtchn
] = -1;
1738 for_each_possible_cpu(cpu
) {
1739 restore_cpu_virqs(cpu
);
1740 restore_cpu_ipis(cpu
);
1746 static struct irq_chip xen_dynamic_chip __read_mostly
= {
1749 .irq_disable
= disable_dynirq
,
1750 .irq_mask
= disable_dynirq
,
1751 .irq_unmask
= enable_dynirq
,
1753 .irq_ack
= ack_dynirq
,
1754 .irq_mask_ack
= mask_ack_dynirq
,
1756 .irq_set_affinity
= set_affinity_irq
,
1757 .irq_retrigger
= retrigger_dynirq
,
1760 static struct irq_chip xen_pirq_chip __read_mostly
= {
1763 .irq_startup
= startup_pirq
,
1764 .irq_shutdown
= shutdown_pirq
,
1765 .irq_enable
= enable_pirq
,
1766 .irq_disable
= disable_pirq
,
1768 .irq_mask
= disable_dynirq
,
1769 .irq_unmask
= enable_dynirq
,
1771 .irq_ack
= eoi_pirq
,
1772 .irq_eoi
= eoi_pirq
,
1773 .irq_mask_ack
= mask_ack_pirq
,
1775 .irq_set_affinity
= set_affinity_irq
,
1777 .irq_retrigger
= retrigger_dynirq
,
1780 static struct irq_chip xen_percpu_chip __read_mostly
= {
1781 .name
= "xen-percpu",
1783 .irq_disable
= disable_dynirq
,
1784 .irq_mask
= disable_dynirq
,
1785 .irq_unmask
= enable_dynirq
,
1787 .irq_ack
= ack_dynirq
,
1790 int xen_set_callback_via(uint64_t via
)
1792 struct xen_hvm_param a
;
1793 a
.domid
= DOMID_SELF
;
1794 a
.index
= HVM_PARAM_CALLBACK_IRQ
;
1796 return HYPERVISOR_hvm_op(HVMOP_set_param
, &a
);
1798 EXPORT_SYMBOL_GPL(xen_set_callback_via
);
1800 #ifdef CONFIG_XEN_PVHVM
1801 /* Vector callbacks are better than PCI interrupts to receive event
1802 * channel notifications because we can receive vector callbacks on any
1803 * vcpu and we don't need PCI support or APIC interactions. */
1804 void xen_callback_vector(void)
1807 uint64_t callback_via
;
1808 if (xen_have_vector_callback
) {
1809 callback_via
= HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR
);
1810 rc
= xen_set_callback_via(callback_via
);
1812 printk(KERN_ERR
"Request for Xen HVM callback vector"
1814 xen_have_vector_callback
= 0;
1817 printk(KERN_INFO
"Xen HVM callback vector for event delivery is "
1819 /* in the restore case the vector has already been allocated */
1820 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR
, used_vectors
))
1821 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR
,
1822 xen_hvm_callback_vector
);
1826 void xen_callback_vector(void) {}
1829 void __init
xen_init_IRQ(void)
1833 evtchn_to_irq
= kcalloc(NR_EVENT_CHANNELS
, sizeof(*evtchn_to_irq
),
1835 BUG_ON(!evtchn_to_irq
);
1836 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1837 evtchn_to_irq
[i
] = -1;
1839 init_evtchn_cpu_bindings();
1841 /* No event channels are 'live' right now. */
1842 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1845 pirq_needs_eoi
= pirq_needs_eoi_flag
;
1848 if (xen_hvm_domain()) {
1849 xen_callback_vector();
1851 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1852 * __acpi_register_gsi can point at the right function */
1856 struct physdev_pirq_eoi_gmfn eoi_gmfn
;
1858 irq_ctx_init(smp_processor_id());
1859 if (xen_initial_domain())
1860 pci_xen_initial_domain();
1862 pirq_eoi_map
= (void *)__get_free_page(GFP_KERNEL
|__GFP_ZERO
);
1863 eoi_gmfn
.gmfn
= virt_to_mfn(pirq_eoi_map
);
1864 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2
, &eoi_gmfn
);
1866 free_page((unsigned long) pirq_eoi_map
);
1867 pirq_eoi_map
= NULL
;
1869 pirq_needs_eoi
= pirq_check_eoi_map
;