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
))))
408 * Need to clear the mask before checking pending to
409 * avoid a race with an event becoming pending.
411 * EVTCHNOP_unmask will only trigger an upcall if the
412 * mask bit was set, so if a hypercall is needed
415 sync_clear_bit(port
, BM(&s
->evtchn_mask
[0]));
416 evtchn_pending
= sync_test_bit(port
, BM(&s
->evtchn_pending
[0]));
418 if (unlikely(evtchn_pending
&& xen_hvm_domain())) {
419 sync_set_bit(port
, BM(&s
->evtchn_mask
[0]));
424 /* Slow path (hypercall) if this is a non-local port or if this is
425 * an hvm domain and an event is pending (hvm domains don't have
426 * their own implementation of irq_enable). */
428 struct evtchn_unmask unmask
= { .port
= port
};
429 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask
, &unmask
);
431 struct vcpu_info
*vcpu_info
= __this_cpu_read(xen_vcpu
);
434 * The following is basically the equivalent of
435 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
436 * the interrupt edge' if the channel is masked.
438 if (evtchn_pending
&&
439 !sync_test_and_set_bit(port
/ BITS_PER_EVTCHN_WORD
,
440 BM(&vcpu_info
->evtchn_pending_sel
)))
441 vcpu_info
->evtchn_upcall_pending
= 1;
447 static void xen_irq_init(unsigned irq
)
449 struct irq_info
*info
;
451 struct irq_desc
*desc
= irq_to_desc(irq
);
453 /* By default all event channels notify CPU#0. */
454 cpumask_copy(desc
->irq_data
.affinity
, cpumask_of(0));
457 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
459 panic("Unable to allocate metadata for IRQ%d\n", irq
);
461 info
->type
= IRQT_UNBOUND
;
464 irq_set_handler_data(irq
, info
);
466 list_add_tail(&info
->list
, &xen_irq_list_head
);
469 static int __must_check
xen_allocate_irq_dynamic(void)
474 #ifdef CONFIG_X86_IO_APIC
476 * For an HVM guest or domain 0 which see "real" (emulated or
477 * actual respectively) GSIs we allocate dynamic IRQs
478 * e.g. those corresponding to event channels or MSIs
479 * etc. from the range above those "real" GSIs to avoid
482 if (xen_initial_domain() || xen_hvm_domain())
483 first
= get_nr_irqs_gsi();
486 irq
= irq_alloc_desc_from(first
, -1);
494 static int __must_check
xen_allocate_irq_gsi(unsigned gsi
)
499 * A PV guest has no concept of a GSI (since it has no ACPI
500 * nor access to/knowledge of the physical APICs). Therefore
501 * all IRQs are dynamically allocated from the entire IRQ
504 if (xen_pv_domain() && !xen_initial_domain())
505 return xen_allocate_irq_dynamic();
507 /* Legacy IRQ descriptors are already allocated by the arch. */
508 if (gsi
< NR_IRQS_LEGACY
)
511 irq
= irq_alloc_desc_at(gsi
, -1);
518 static void xen_free_irq(unsigned irq
)
520 struct irq_info
*info
= irq_get_handler_data(irq
);
522 list_del(&info
->list
);
524 irq_set_handler_data(irq
, NULL
);
526 WARN_ON(info
->refcnt
> 0);
530 /* Legacy IRQ descriptors are managed by the arch. */
531 if (irq
< NR_IRQS_LEGACY
)
537 static void pirq_query_unmask(int irq
)
539 struct physdev_irq_status_query irq_status
;
540 struct irq_info
*info
= info_for_irq(irq
);
542 BUG_ON(info
->type
!= IRQT_PIRQ
);
544 irq_status
.irq
= pirq_from_irq(irq
);
545 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query
, &irq_status
))
546 irq_status
.flags
= 0;
548 info
->u
.pirq
.flags
&= ~PIRQ_NEEDS_EOI
;
549 if (irq_status
.flags
& XENIRQSTAT_needs_eoi
)
550 info
->u
.pirq
.flags
|= PIRQ_NEEDS_EOI
;
553 static bool probing_irq(int irq
)
555 struct irq_desc
*desc
= irq_to_desc(irq
);
557 return desc
&& desc
->action
== NULL
;
560 static void eoi_pirq(struct irq_data
*data
)
562 int evtchn
= evtchn_from_irq(data
->irq
);
563 struct physdev_eoi eoi
= { .irq
= pirq_from_irq(data
->irq
) };
568 if (VALID_EVTCHN(evtchn
))
569 clear_evtchn(evtchn
);
571 if (pirq_needs_eoi(data
->irq
)) {
572 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_eoi
, &eoi
);
577 static void mask_ack_pirq(struct irq_data
*data
)
579 disable_dynirq(data
);
583 static unsigned int __startup_pirq(unsigned int irq
)
585 struct evtchn_bind_pirq bind_pirq
;
586 struct irq_info
*info
= info_for_irq(irq
);
587 int evtchn
= evtchn_from_irq(irq
);
590 BUG_ON(info
->type
!= IRQT_PIRQ
);
592 if (VALID_EVTCHN(evtchn
))
595 bind_pirq
.pirq
= pirq_from_irq(irq
);
596 /* NB. We are happy to share unless we are probing. */
597 bind_pirq
.flags
= info
->u
.pirq
.flags
& PIRQ_SHAREABLE
?
598 BIND_PIRQ__WILL_SHARE
: 0;
599 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq
, &bind_pirq
);
601 if (!probing_irq(irq
))
602 printk(KERN_INFO
"Failed to obtain physical IRQ %d\n",
606 evtchn
= bind_pirq
.port
;
608 pirq_query_unmask(irq
);
610 evtchn_to_irq
[evtchn
] = irq
;
611 bind_evtchn_to_cpu(evtchn
, 0);
612 info
->evtchn
= evtchn
;
615 unmask_evtchn(evtchn
);
616 eoi_pirq(irq_get_irq_data(irq
));
621 static unsigned int startup_pirq(struct irq_data
*data
)
623 return __startup_pirq(data
->irq
);
626 static void shutdown_pirq(struct irq_data
*data
)
628 struct evtchn_close close
;
629 unsigned int irq
= data
->irq
;
630 struct irq_info
*info
= info_for_irq(irq
);
631 int evtchn
= evtchn_from_irq(irq
);
633 BUG_ON(info
->type
!= IRQT_PIRQ
);
635 if (!VALID_EVTCHN(evtchn
))
641 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
644 bind_evtchn_to_cpu(evtchn
, 0);
645 evtchn_to_irq
[evtchn
] = -1;
649 static void enable_pirq(struct irq_data
*data
)
654 static void disable_pirq(struct irq_data
*data
)
656 disable_dynirq(data
);
659 int xen_irq_from_gsi(unsigned gsi
)
661 struct irq_info
*info
;
663 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
664 if (info
->type
!= IRQT_PIRQ
)
667 if (info
->u
.pirq
.gsi
== gsi
)
673 EXPORT_SYMBOL_GPL(xen_irq_from_gsi
);
676 * Do not make any assumptions regarding the relationship between the
677 * IRQ number returned here and the Xen pirq argument.
679 * Note: We don't assign an event channel until the irq actually started
680 * up. Return an existing irq if we've already got one for the gsi.
682 * Shareable implies level triggered, not shareable implies edge
685 int xen_bind_pirq_gsi_to_irq(unsigned gsi
,
686 unsigned pirq
, int shareable
, char *name
)
689 struct physdev_irq irq_op
;
691 mutex_lock(&irq_mapping_update_lock
);
693 irq
= xen_irq_from_gsi(gsi
);
695 printk(KERN_INFO
"xen_map_pirq_gsi: returning irq %d for gsi %u\n",
700 irq
= xen_allocate_irq_gsi(gsi
);
707 /* Only the privileged domain can do this. For non-priv, the pcifront
708 * driver provides a PCI bus that does the call to do exactly
709 * this in the priv domain. */
710 if (xen_initial_domain() &&
711 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector
, &irq_op
)) {
717 xen_irq_info_pirq_init(irq
, 0, pirq
, gsi
, irq_op
.vector
, DOMID_SELF
,
718 shareable
? PIRQ_SHAREABLE
: 0);
720 pirq_query_unmask(irq
);
721 /* We try to use the handler with the appropriate semantic for the
722 * type of interrupt: if the interrupt is an edge triggered
723 * interrupt we use handle_edge_irq.
725 * On the other hand if the interrupt is level triggered we use
726 * handle_fasteoi_irq like the native code does for this kind of
729 * Depending on the Xen version, pirq_needs_eoi might return true
730 * not only for level triggered interrupts but for edge triggered
731 * interrupts too. In any case Xen always honors the eoi mechanism,
732 * not injecting any more pirqs of the same kind if the first one
733 * hasn't received an eoi yet. Therefore using the fasteoi handler
734 * is the right choice either way.
737 irq_set_chip_and_handler_name(irq
, &xen_pirq_chip
,
738 handle_fasteoi_irq
, name
);
740 irq_set_chip_and_handler_name(irq
, &xen_pirq_chip
,
741 handle_edge_irq
, name
);
744 mutex_unlock(&irq_mapping_update_lock
);
749 #ifdef CONFIG_PCI_MSI
750 int xen_allocate_pirq_msi(struct pci_dev
*dev
, struct msi_desc
*msidesc
)
753 struct physdev_get_free_pirq op_get_free_pirq
;
755 op_get_free_pirq
.type
= MAP_PIRQ_TYPE_MSI
;
756 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq
, &op_get_free_pirq
);
758 WARN_ONCE(rc
== -ENOSYS
,
759 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
761 return rc
? -1 : op_get_free_pirq
.pirq
;
764 int xen_bind_pirq_msi_to_irq(struct pci_dev
*dev
, struct msi_desc
*msidesc
,
765 int pirq
, int vector
, const char *name
,
770 mutex_lock(&irq_mapping_update_lock
);
772 irq
= xen_allocate_irq_dynamic();
776 irq_set_chip_and_handler_name(irq
, &xen_pirq_chip
, handle_edge_irq
,
779 xen_irq_info_pirq_init(irq
, 0, pirq
, 0, vector
, domid
, 0);
780 ret
= irq_set_msi_desc(irq
, msidesc
);
784 mutex_unlock(&irq_mapping_update_lock
);
787 mutex_unlock(&irq_mapping_update_lock
);
793 int xen_destroy_irq(int irq
)
795 struct irq_desc
*desc
;
796 struct physdev_unmap_pirq unmap_irq
;
797 struct irq_info
*info
= info_for_irq(irq
);
800 mutex_lock(&irq_mapping_update_lock
);
802 desc
= irq_to_desc(irq
);
806 if (xen_initial_domain()) {
807 unmap_irq
.pirq
= info
->u
.pirq
.pirq
;
808 unmap_irq
.domid
= info
->u
.pirq
.domid
;
809 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq
, &unmap_irq
);
810 /* If another domain quits without making the pci_disable_msix
811 * call, the Xen hypervisor takes care of freeing the PIRQs
812 * (free_domain_pirqs).
814 if ((rc
== -ESRCH
&& info
->u
.pirq
.domid
!= DOMID_SELF
))
815 printk(KERN_INFO
"domain %d does not have %d anymore\n",
816 info
->u
.pirq
.domid
, info
->u
.pirq
.pirq
);
818 printk(KERN_WARNING
"unmap irq failed %d\n", rc
);
826 mutex_unlock(&irq_mapping_update_lock
);
830 int xen_irq_from_pirq(unsigned pirq
)
834 struct irq_info
*info
;
836 mutex_lock(&irq_mapping_update_lock
);
838 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
839 if (info
->type
!= IRQT_PIRQ
)
842 if (info
->u
.pirq
.pirq
== pirq
)
847 mutex_unlock(&irq_mapping_update_lock
);
853 int xen_pirq_from_irq(unsigned irq
)
855 return pirq_from_irq(irq
);
857 EXPORT_SYMBOL_GPL(xen_pirq_from_irq
);
858 int bind_evtchn_to_irq(unsigned int evtchn
)
862 mutex_lock(&irq_mapping_update_lock
);
864 irq
= evtchn_to_irq
[evtchn
];
867 irq
= xen_allocate_irq_dynamic();
871 irq_set_chip_and_handler_name(irq
, &xen_dynamic_chip
,
872 handle_edge_irq
, "event");
874 xen_irq_info_evtchn_init(irq
, evtchn
);
876 struct irq_info
*info
= info_for_irq(irq
);
877 WARN_ON(info
== NULL
|| info
->type
!= IRQT_EVTCHN
);
879 irq_clear_status_flags(irq
, IRQ_NOREQUEST
|IRQ_NOAUTOEN
);
882 mutex_unlock(&irq_mapping_update_lock
);
886 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq
);
888 static int bind_ipi_to_irq(unsigned int ipi
, unsigned int cpu
)
890 struct evtchn_bind_ipi bind_ipi
;
893 mutex_lock(&irq_mapping_update_lock
);
895 irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
];
898 irq
= xen_allocate_irq_dynamic();
902 irq_set_chip_and_handler_name(irq
, &xen_percpu_chip
,
903 handle_percpu_irq
, "ipi");
906 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
909 evtchn
= bind_ipi
.port
;
911 xen_irq_info_ipi_init(cpu
, irq
, evtchn
, ipi
);
913 bind_evtchn_to_cpu(evtchn
, cpu
);
915 struct irq_info
*info
= info_for_irq(irq
);
916 WARN_ON(info
== NULL
|| info
->type
!= IRQT_IPI
);
920 mutex_unlock(&irq_mapping_update_lock
);
924 static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain
,
925 unsigned int remote_port
)
927 struct evtchn_bind_interdomain bind_interdomain
;
930 bind_interdomain
.remote_dom
= remote_domain
;
931 bind_interdomain
.remote_port
= remote_port
;
933 err
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain
,
936 return err
? : bind_evtchn_to_irq(bind_interdomain
.local_port
);
939 static int find_virq(unsigned int virq
, unsigned int cpu
)
941 struct evtchn_status status
;
942 int port
, rc
= -ENOENT
;
944 memset(&status
, 0, sizeof(status
));
945 for (port
= 0; port
<= NR_EVENT_CHANNELS
; port
++) {
946 status
.dom
= DOMID_SELF
;
948 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_status
, &status
);
951 if (status
.status
!= EVTCHNSTAT_virq
)
953 if (status
.u
.virq
== virq
&& status
.vcpu
== cpu
) {
961 int bind_virq_to_irq(unsigned int virq
, unsigned int cpu
)
963 struct evtchn_bind_virq bind_virq
;
964 int evtchn
, irq
, ret
;
966 mutex_lock(&irq_mapping_update_lock
);
968 irq
= per_cpu(virq_to_irq
, cpu
)[virq
];
971 irq
= xen_allocate_irq_dynamic();
975 irq_set_chip_and_handler_name(irq
, &xen_percpu_chip
,
976 handle_percpu_irq
, "virq");
978 bind_virq
.virq
= virq
;
979 bind_virq
.vcpu
= cpu
;
980 ret
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
983 evtchn
= bind_virq
.port
;
986 ret
= find_virq(virq
, cpu
);
991 xen_irq_info_virq_init(cpu
, irq
, evtchn
, virq
);
993 bind_evtchn_to_cpu(evtchn
, cpu
);
995 struct irq_info
*info
= info_for_irq(irq
);
996 WARN_ON(info
== NULL
|| info
->type
!= IRQT_VIRQ
);
1000 mutex_unlock(&irq_mapping_update_lock
);
1005 static void unbind_from_irq(unsigned int irq
)
1007 struct evtchn_close close
;
1008 int evtchn
= evtchn_from_irq(irq
);
1009 struct irq_info
*info
= irq_get_handler_data(irq
);
1011 mutex_lock(&irq_mapping_update_lock
);
1013 if (info
->refcnt
> 0) {
1015 if (info
->refcnt
!= 0)
1019 if (VALID_EVTCHN(evtchn
)) {
1020 close
.port
= evtchn
;
1021 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
1024 switch (type_from_irq(irq
)) {
1026 per_cpu(virq_to_irq
, cpu_from_evtchn(evtchn
))
1027 [virq_from_irq(irq
)] = -1;
1030 per_cpu(ipi_to_irq
, cpu_from_evtchn(evtchn
))
1031 [ipi_from_irq(irq
)] = -1;
1037 /* Closed ports are implicitly re-bound to VCPU0. */
1038 bind_evtchn_to_cpu(evtchn
, 0);
1040 evtchn_to_irq
[evtchn
] = -1;
1043 BUG_ON(info_for_irq(irq
)->type
== IRQT_UNBOUND
);
1048 mutex_unlock(&irq_mapping_update_lock
);
1051 int bind_evtchn_to_irqhandler(unsigned int evtchn
,
1052 irq_handler_t handler
,
1053 unsigned long irqflags
,
1054 const char *devname
, void *dev_id
)
1058 irq
= bind_evtchn_to_irq(evtchn
);
1061 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1063 unbind_from_irq(irq
);
1069 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler
);
1071 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain
,
1072 unsigned int remote_port
,
1073 irq_handler_t handler
,
1074 unsigned long irqflags
,
1075 const char *devname
,
1080 irq
= bind_interdomain_evtchn_to_irq(remote_domain
, remote_port
);
1084 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1086 unbind_from_irq(irq
);
1092 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler
);
1094 int bind_virq_to_irqhandler(unsigned int virq
, unsigned int cpu
,
1095 irq_handler_t handler
,
1096 unsigned long irqflags
, const char *devname
, void *dev_id
)
1100 irq
= bind_virq_to_irq(virq
, cpu
);
1103 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1105 unbind_from_irq(irq
);
1111 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler
);
1113 int bind_ipi_to_irqhandler(enum ipi_vector ipi
,
1115 irq_handler_t handler
,
1116 unsigned long irqflags
,
1117 const char *devname
,
1122 irq
= bind_ipi_to_irq(ipi
, cpu
);
1126 irqflags
|= IRQF_NO_SUSPEND
| IRQF_FORCE_RESUME
| IRQF_EARLY_RESUME
;
1127 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
1129 unbind_from_irq(irq
);
1136 void unbind_from_irqhandler(unsigned int irq
, void *dev_id
)
1138 free_irq(irq
, dev_id
);
1139 unbind_from_irq(irq
);
1141 EXPORT_SYMBOL_GPL(unbind_from_irqhandler
);
1143 int evtchn_make_refcounted(unsigned int evtchn
)
1145 int irq
= evtchn_to_irq
[evtchn
];
1146 struct irq_info
*info
;
1151 info
= irq_get_handler_data(irq
);
1156 WARN_ON(info
->refcnt
!= -1);
1162 EXPORT_SYMBOL_GPL(evtchn_make_refcounted
);
1164 int evtchn_get(unsigned int evtchn
)
1167 struct irq_info
*info
;
1170 if (evtchn
>= NR_EVENT_CHANNELS
)
1173 mutex_lock(&irq_mapping_update_lock
);
1175 irq
= evtchn_to_irq
[evtchn
];
1179 info
= irq_get_handler_data(irq
);
1185 if (info
->refcnt
<= 0)
1191 mutex_unlock(&irq_mapping_update_lock
);
1195 EXPORT_SYMBOL_GPL(evtchn_get
);
1197 void evtchn_put(unsigned int evtchn
)
1199 int irq
= evtchn_to_irq
[evtchn
];
1200 if (WARN_ON(irq
== -1))
1202 unbind_from_irq(irq
);
1204 EXPORT_SYMBOL_GPL(evtchn_put
);
1206 void xen_send_IPI_one(unsigned int cpu
, enum ipi_vector vector
)
1208 int irq
= per_cpu(ipi_to_irq
, cpu
)[vector
];
1210 notify_remote_via_irq(irq
);
1213 irqreturn_t
xen_debug_interrupt(int irq
, void *dev_id
)
1215 struct shared_info
*sh
= HYPERVISOR_shared_info
;
1216 int cpu
= smp_processor_id();
1217 xen_ulong_t
*cpu_evtchn
= per_cpu(cpu_evtchn_mask
, cpu
);
1219 unsigned long flags
;
1220 static DEFINE_SPINLOCK(debug_lock
);
1221 struct vcpu_info
*v
;
1223 spin_lock_irqsave(&debug_lock
, flags
);
1225 printk("\nvcpu %d\n ", cpu
);
1227 for_each_online_cpu(i
) {
1229 v
= per_cpu(xen_vcpu
, i
);
1230 pending
= (get_irq_regs() && i
== cpu
)
1231 ? xen_irqs_disabled(get_irq_regs())
1232 : v
->evtchn_upcall_mask
;
1233 printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong
"\n ", i
,
1234 pending
, v
->evtchn_upcall_pending
,
1235 (int)(sizeof(v
->evtchn_pending_sel
)*2),
1236 v
->evtchn_pending_sel
);
1238 v
= per_cpu(xen_vcpu
, cpu
);
1240 printk("\npending:\n ");
1241 for (i
= ARRAY_SIZE(sh
->evtchn_pending
)-1; i
>= 0; i
--)
1242 printk("%0*"PRI_xen_ulong
"%s",
1243 (int)sizeof(sh
->evtchn_pending
[0])*2,
1244 sh
->evtchn_pending
[i
],
1245 i
% 8 == 0 ? "\n " : " ");
1246 printk("\nglobal mask:\n ");
1247 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
1248 printk("%0*"PRI_xen_ulong
"%s",
1249 (int)(sizeof(sh
->evtchn_mask
[0])*2),
1251 i
% 8 == 0 ? "\n " : " ");
1253 printk("\nglobally unmasked:\n ");
1254 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
1255 printk("%0*"PRI_xen_ulong
"%s",
1256 (int)(sizeof(sh
->evtchn_mask
[0])*2),
1257 sh
->evtchn_pending
[i
] & ~sh
->evtchn_mask
[i
],
1258 i
% 8 == 0 ? "\n " : " ");
1260 printk("\nlocal cpu%d mask:\n ", cpu
);
1261 for (i
= (NR_EVENT_CHANNELS
/BITS_PER_EVTCHN_WORD
)-1; i
>= 0; i
--)
1262 printk("%0*"PRI_xen_ulong
"%s", (int)(sizeof(cpu_evtchn
[0])*2),
1264 i
% 8 == 0 ? "\n " : " ");
1266 printk("\nlocally unmasked:\n ");
1267 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--) {
1268 xen_ulong_t pending
= sh
->evtchn_pending
[i
]
1269 & ~sh
->evtchn_mask
[i
]
1271 printk("%0*"PRI_xen_ulong
"%s",
1272 (int)(sizeof(sh
->evtchn_mask
[0])*2),
1273 pending
, i
% 8 == 0 ? "\n " : " ");
1276 printk("\npending list:\n");
1277 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++) {
1278 if (sync_test_bit(i
, BM(sh
->evtchn_pending
))) {
1279 int word_idx
= i
/ BITS_PER_EVTCHN_WORD
;
1280 printk(" %d: event %d -> irq %d%s%s%s\n",
1281 cpu_from_evtchn(i
), i
,
1283 sync_test_bit(word_idx
, BM(&v
->evtchn_pending_sel
))
1285 !sync_test_bit(i
, BM(sh
->evtchn_mask
))
1286 ? "" : " globally-masked",
1287 sync_test_bit(i
, BM(cpu_evtchn
))
1288 ? "" : " locally-masked");
1292 spin_unlock_irqrestore(&debug_lock
, flags
);
1297 static DEFINE_PER_CPU(unsigned, xed_nesting_count
);
1298 static DEFINE_PER_CPU(unsigned int, current_word_idx
);
1299 static DEFINE_PER_CPU(unsigned int, current_bit_idx
);
1302 * Mask out the i least significant bits of w
1304 #define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
1307 * Search the CPUs pending events bitmasks. For each one found, map
1308 * the event number to an irq, and feed it into do_IRQ() for
1311 * Xen uses a two-level bitmap to speed searching. The first level is
1312 * a bitset of words which contain pending event bits. The second
1313 * level is a bitset of pending events themselves.
1315 static void __xen_evtchn_do_upcall(void)
1317 int start_word_idx
, start_bit_idx
;
1318 int word_idx
, bit_idx
;
1320 int cpu
= get_cpu();
1321 struct shared_info
*s
= HYPERVISOR_shared_info
;
1322 struct vcpu_info
*vcpu_info
= __this_cpu_read(xen_vcpu
);
1326 xen_ulong_t pending_words
;
1328 vcpu_info
->evtchn_upcall_pending
= 0;
1330 if (__this_cpu_inc_return(xed_nesting_count
) - 1)
1334 * Master flag must be cleared /before/ clearing
1335 * selector flag. xchg_xen_ulong must contain an
1336 * appropriate barrier.
1338 pending_words
= xchg_xen_ulong(&vcpu_info
->evtchn_pending_sel
, 0);
1340 start_word_idx
= __this_cpu_read(current_word_idx
);
1341 start_bit_idx
= __this_cpu_read(current_bit_idx
);
1343 word_idx
= start_word_idx
;
1345 for (i
= 0; pending_words
!= 0; i
++) {
1346 xen_ulong_t pending_bits
;
1349 words
= MASK_LSBS(pending_words
, word_idx
);
1352 * If we masked out all events, wrap to beginning.
1359 word_idx
= EVTCHN_FIRST_BIT(words
);
1361 pending_bits
= active_evtchns(cpu
, s
, word_idx
);
1362 bit_idx
= 0; /* usually scan entire word from start */
1363 if (word_idx
== start_word_idx
) {
1364 /* We scan the starting word in two parts */
1366 /* 1st time: start in the middle */
1367 bit_idx
= start_bit_idx
;
1369 /* 2nd time: mask bits done already */
1370 bit_idx
&= (1UL << start_bit_idx
) - 1;
1376 struct irq_desc
*desc
;
1378 bits
= MASK_LSBS(pending_bits
, bit_idx
);
1380 /* If we masked out all events, move on. */
1384 bit_idx
= EVTCHN_FIRST_BIT(bits
);
1387 port
= (word_idx
* BITS_PER_EVTCHN_WORD
) + bit_idx
;
1388 irq
= evtchn_to_irq
[port
];
1391 desc
= irq_to_desc(irq
);
1393 generic_handle_irq_desc(irq
, desc
);
1396 bit_idx
= (bit_idx
+ 1) % BITS_PER_EVTCHN_WORD
;
1398 /* Next caller starts at last processed + 1 */
1399 __this_cpu_write(current_word_idx
,
1400 bit_idx
? word_idx
:
1401 (word_idx
+1) % BITS_PER_EVTCHN_WORD
);
1402 __this_cpu_write(current_bit_idx
, bit_idx
);
1403 } while (bit_idx
!= 0);
1405 /* Scan start_l1i twice; all others once. */
1406 if ((word_idx
!= start_word_idx
) || (i
!= 0))
1407 pending_words
&= ~(1UL << word_idx
);
1409 word_idx
= (word_idx
+ 1) % BITS_PER_EVTCHN_WORD
;
1412 BUG_ON(!irqs_disabled());
1414 count
= __this_cpu_read(xed_nesting_count
);
1415 __this_cpu_write(xed_nesting_count
, 0);
1416 } while (count
!= 1 || vcpu_info
->evtchn_upcall_pending
);
1423 void xen_evtchn_do_upcall(struct pt_regs
*regs
)
1425 struct pt_regs
*old_regs
= set_irq_regs(regs
);
1432 __xen_evtchn_do_upcall();
1435 set_irq_regs(old_regs
);
1438 void xen_hvm_evtchn_do_upcall(void)
1440 __xen_evtchn_do_upcall();
1442 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall
);
1444 /* Rebind a new event channel to an existing irq. */
1445 void rebind_evtchn_irq(int evtchn
, int irq
)
1447 struct irq_info
*info
= info_for_irq(irq
);
1449 /* Make sure the irq is masked, since the new event channel
1450 will also be masked. */
1453 mutex_lock(&irq_mapping_update_lock
);
1455 /* After resume the irq<->evtchn mappings are all cleared out */
1456 BUG_ON(evtchn_to_irq
[evtchn
] != -1);
1457 /* Expect irq to have been bound before,
1458 so there should be a proper type */
1459 BUG_ON(info
->type
== IRQT_UNBOUND
);
1461 xen_irq_info_evtchn_init(irq
, evtchn
);
1463 mutex_unlock(&irq_mapping_update_lock
);
1465 /* new event channels are always bound to cpu 0 */
1466 irq_set_affinity(irq
, cpumask_of(0));
1468 /* Unmask the event channel. */
1472 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1473 static int rebind_irq_to_cpu(unsigned irq
, unsigned tcpu
)
1475 struct evtchn_bind_vcpu bind_vcpu
;
1476 int evtchn
= evtchn_from_irq(irq
);
1478 if (!VALID_EVTCHN(evtchn
))
1482 * Events delivered via platform PCI interrupts are always
1483 * routed to vcpu 0 and hence cannot be rebound.
1485 if (xen_hvm_domain() && !xen_have_vector_callback
)
1488 /* Send future instances of this interrupt to other vcpu. */
1489 bind_vcpu
.port
= evtchn
;
1490 bind_vcpu
.vcpu
= tcpu
;
1493 * If this fails, it usually just indicates that we're dealing with a
1494 * virq or IPI channel, which don't actually need to be rebound. Ignore
1495 * it, but don't do the xenlinux-level rebind in that case.
1497 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu
, &bind_vcpu
) >= 0)
1498 bind_evtchn_to_cpu(evtchn
, tcpu
);
1503 static int set_affinity_irq(struct irq_data
*data
, const struct cpumask
*dest
,
1506 unsigned tcpu
= cpumask_first(dest
);
1508 return rebind_irq_to_cpu(data
->irq
, tcpu
);
1511 int resend_irq_on_evtchn(unsigned int irq
)
1513 int masked
, evtchn
= evtchn_from_irq(irq
);
1514 struct shared_info
*s
= HYPERVISOR_shared_info
;
1516 if (!VALID_EVTCHN(evtchn
))
1519 masked
= sync_test_and_set_bit(evtchn
, BM(s
->evtchn_mask
));
1520 sync_set_bit(evtchn
, BM(s
->evtchn_pending
));
1522 unmask_evtchn(evtchn
);
1527 static void enable_dynirq(struct irq_data
*data
)
1529 int evtchn
= evtchn_from_irq(data
->irq
);
1531 if (VALID_EVTCHN(evtchn
))
1532 unmask_evtchn(evtchn
);
1535 static void disable_dynirq(struct irq_data
*data
)
1537 int evtchn
= evtchn_from_irq(data
->irq
);
1539 if (VALID_EVTCHN(evtchn
))
1540 mask_evtchn(evtchn
);
1543 static void ack_dynirq(struct irq_data
*data
)
1545 int evtchn
= evtchn_from_irq(data
->irq
);
1549 if (VALID_EVTCHN(evtchn
))
1550 clear_evtchn(evtchn
);
1553 static void mask_ack_dynirq(struct irq_data
*data
)
1555 disable_dynirq(data
);
1559 static int retrigger_dynirq(struct irq_data
*data
)
1561 int evtchn
= evtchn_from_irq(data
->irq
);
1562 struct shared_info
*sh
= HYPERVISOR_shared_info
;
1565 if (VALID_EVTCHN(evtchn
)) {
1568 masked
= sync_test_and_set_bit(evtchn
, BM(sh
->evtchn_mask
));
1569 sync_set_bit(evtchn
, BM(sh
->evtchn_pending
));
1571 unmask_evtchn(evtchn
);
1578 static void restore_pirqs(void)
1580 int pirq
, rc
, irq
, gsi
;
1581 struct physdev_map_pirq map_irq
;
1582 struct irq_info
*info
;
1584 list_for_each_entry(info
, &xen_irq_list_head
, list
) {
1585 if (info
->type
!= IRQT_PIRQ
)
1588 pirq
= info
->u
.pirq
.pirq
;
1589 gsi
= info
->u
.pirq
.gsi
;
1592 /* save/restore of PT devices doesn't work, so at this point the
1593 * only devices present are GSI based emulated devices */
1597 map_irq
.domid
= DOMID_SELF
;
1598 map_irq
.type
= MAP_PIRQ_TYPE_GSI
;
1599 map_irq
.index
= gsi
;
1600 map_irq
.pirq
= pirq
;
1602 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq
, &map_irq
);
1604 printk(KERN_WARNING
"xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1605 gsi
, irq
, pirq
, rc
);
1610 printk(KERN_DEBUG
"xen: --> irq=%d, pirq=%d\n", irq
, map_irq
.pirq
);
1612 __startup_pirq(irq
);
1616 static void restore_cpu_virqs(unsigned int cpu
)
1618 struct evtchn_bind_virq bind_virq
;
1619 int virq
, irq
, evtchn
;
1621 for (virq
= 0; virq
< NR_VIRQS
; virq
++) {
1622 if ((irq
= per_cpu(virq_to_irq
, cpu
)[virq
]) == -1)
1625 BUG_ON(virq_from_irq(irq
) != virq
);
1627 /* Get a new binding from Xen. */
1628 bind_virq
.virq
= virq
;
1629 bind_virq
.vcpu
= cpu
;
1630 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
1633 evtchn
= bind_virq
.port
;
1635 /* Record the new mapping. */
1636 xen_irq_info_virq_init(cpu
, irq
, evtchn
, virq
);
1637 bind_evtchn_to_cpu(evtchn
, cpu
);
1641 static void restore_cpu_ipis(unsigned int cpu
)
1643 struct evtchn_bind_ipi bind_ipi
;
1644 int ipi
, irq
, evtchn
;
1646 for (ipi
= 0; ipi
< XEN_NR_IPIS
; ipi
++) {
1647 if ((irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
]) == -1)
1650 BUG_ON(ipi_from_irq(irq
) != ipi
);
1652 /* Get a new binding from Xen. */
1653 bind_ipi
.vcpu
= cpu
;
1654 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
1657 evtchn
= bind_ipi
.port
;
1659 /* Record the new mapping. */
1660 xen_irq_info_ipi_init(cpu
, irq
, evtchn
, ipi
);
1661 bind_evtchn_to_cpu(evtchn
, cpu
);
1665 /* Clear an irq's pending state, in preparation for polling on it */
1666 void xen_clear_irq_pending(int irq
)
1668 int evtchn
= evtchn_from_irq(irq
);
1670 if (VALID_EVTCHN(evtchn
))
1671 clear_evtchn(evtchn
);
1673 EXPORT_SYMBOL(xen_clear_irq_pending
);
1674 void xen_set_irq_pending(int irq
)
1676 int evtchn
= evtchn_from_irq(irq
);
1678 if (VALID_EVTCHN(evtchn
))
1682 bool xen_test_irq_pending(int irq
)
1684 int evtchn
= evtchn_from_irq(irq
);
1687 if (VALID_EVTCHN(evtchn
))
1688 ret
= test_evtchn(evtchn
);
1693 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1694 * the irq will be disabled so it won't deliver an interrupt. */
1695 void xen_poll_irq_timeout(int irq
, u64 timeout
)
1697 evtchn_port_t evtchn
= evtchn_from_irq(irq
);
1699 if (VALID_EVTCHN(evtchn
)) {
1700 struct sched_poll poll
;
1703 poll
.timeout
= timeout
;
1704 set_xen_guest_handle(poll
.ports
, &evtchn
);
1706 if (HYPERVISOR_sched_op(SCHEDOP_poll
, &poll
) != 0)
1710 EXPORT_SYMBOL(xen_poll_irq_timeout
);
1711 /* Poll waiting for an irq to become pending. In the usual case, the
1712 * irq will be disabled so it won't deliver an interrupt. */
1713 void xen_poll_irq(int irq
)
1715 xen_poll_irq_timeout(irq
, 0 /* no timeout */);
1718 /* Check whether the IRQ line is shared with other guests. */
1719 int xen_test_irq_shared(int irq
)
1721 struct irq_info
*info
= info_for_irq(irq
);
1722 struct physdev_irq_status_query irq_status
= { .irq
= info
->u
.pirq
.pirq
};
1724 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query
, &irq_status
))
1726 return !(irq_status
.flags
& XENIRQSTAT_shared
);
1728 EXPORT_SYMBOL_GPL(xen_test_irq_shared
);
1730 void xen_irq_resume(void)
1732 unsigned int cpu
, evtchn
;
1733 struct irq_info
*info
;
1735 init_evtchn_cpu_bindings();
1737 /* New event-channel space is not 'live' yet. */
1738 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1739 mask_evtchn(evtchn
);
1741 /* No IRQ <-> event-channel mappings. */
1742 list_for_each_entry(info
, &xen_irq_list_head
, list
)
1743 info
->evtchn
= 0; /* zap event-channel binding */
1745 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1746 evtchn_to_irq
[evtchn
] = -1;
1748 for_each_possible_cpu(cpu
) {
1749 restore_cpu_virqs(cpu
);
1750 restore_cpu_ipis(cpu
);
1756 static struct irq_chip xen_dynamic_chip __read_mostly
= {
1759 .irq_disable
= disable_dynirq
,
1760 .irq_mask
= disable_dynirq
,
1761 .irq_unmask
= enable_dynirq
,
1763 .irq_ack
= ack_dynirq
,
1764 .irq_mask_ack
= mask_ack_dynirq
,
1766 .irq_set_affinity
= set_affinity_irq
,
1767 .irq_retrigger
= retrigger_dynirq
,
1770 static struct irq_chip xen_pirq_chip __read_mostly
= {
1773 .irq_startup
= startup_pirq
,
1774 .irq_shutdown
= shutdown_pirq
,
1775 .irq_enable
= enable_pirq
,
1776 .irq_disable
= disable_pirq
,
1778 .irq_mask
= disable_dynirq
,
1779 .irq_unmask
= enable_dynirq
,
1781 .irq_ack
= eoi_pirq
,
1782 .irq_eoi
= eoi_pirq
,
1783 .irq_mask_ack
= mask_ack_pirq
,
1785 .irq_set_affinity
= set_affinity_irq
,
1787 .irq_retrigger
= retrigger_dynirq
,
1790 static struct irq_chip xen_percpu_chip __read_mostly
= {
1791 .name
= "xen-percpu",
1793 .irq_disable
= disable_dynirq
,
1794 .irq_mask
= disable_dynirq
,
1795 .irq_unmask
= enable_dynirq
,
1797 .irq_ack
= ack_dynirq
,
1800 int xen_set_callback_via(uint64_t via
)
1802 struct xen_hvm_param a
;
1803 a
.domid
= DOMID_SELF
;
1804 a
.index
= HVM_PARAM_CALLBACK_IRQ
;
1806 return HYPERVISOR_hvm_op(HVMOP_set_param
, &a
);
1808 EXPORT_SYMBOL_GPL(xen_set_callback_via
);
1810 #ifdef CONFIG_XEN_PVHVM
1811 /* Vector callbacks are better than PCI interrupts to receive event
1812 * channel notifications because we can receive vector callbacks on any
1813 * vcpu and we don't need PCI support or APIC interactions. */
1814 void xen_callback_vector(void)
1817 uint64_t callback_via
;
1818 if (xen_have_vector_callback
) {
1819 callback_via
= HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR
);
1820 rc
= xen_set_callback_via(callback_via
);
1822 printk(KERN_ERR
"Request for Xen HVM callback vector"
1824 xen_have_vector_callback
= 0;
1827 printk(KERN_INFO
"Xen HVM callback vector for event delivery is "
1829 /* in the restore case the vector has already been allocated */
1830 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR
, used_vectors
))
1831 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR
,
1832 xen_hvm_callback_vector
);
1836 void xen_callback_vector(void) {}
1839 void __init
xen_init_IRQ(void)
1843 evtchn_to_irq
= kcalloc(NR_EVENT_CHANNELS
, sizeof(*evtchn_to_irq
),
1845 BUG_ON(!evtchn_to_irq
);
1846 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1847 evtchn_to_irq
[i
] = -1;
1849 init_evtchn_cpu_bindings();
1851 /* No event channels are 'live' right now. */
1852 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1855 pirq_needs_eoi
= pirq_needs_eoi_flag
;
1858 if (xen_hvm_domain()) {
1859 xen_callback_vector();
1861 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1862 * __acpi_register_gsi can point at the right function */
1866 struct physdev_pirq_eoi_gmfn eoi_gmfn
;
1868 irq_ctx_init(smp_processor_id());
1869 if (xen_initial_domain())
1870 pci_xen_initial_domain();
1872 pirq_eoi_map
= (void *)__get_free_page(GFP_KERNEL
|__GFP_ZERO
);
1873 eoi_gmfn
.gmfn
= virt_to_mfn(pirq_eoi_map
);
1874 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2
, &eoi_gmfn
);
1876 free_page((unsigned long) pirq_eoi_map
);
1877 pirq_eoi_map
= NULL
;
1879 pirq_needs_eoi
= pirq_check_eoi_map
;