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 recieved, 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 /* IRQ <-> VIRQ mapping. */
60 static DEFINE_PER_CPU(int [NR_VIRQS
], virq_to_irq
) = {[0 ... NR_VIRQS
-1] = -1};
62 /* IRQ <-> IPI mapping */
63 static DEFINE_PER_CPU(int [XEN_NR_IPIS
], ipi_to_irq
) = {[0 ... XEN_NR_IPIS
-1] = -1};
65 /* Interrupt types. */
75 * Packed IRQ information:
76 * type - enum xen_irq_type
77 * event channel - irq->event channel mapping
78 * cpu - cpu this event channel is bound to
79 * index - type-specific information:
80 * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
81 * guest, or GSI (real passthrough IRQ) of the device.
88 enum xen_irq_type type
; /* type */
89 unsigned short evtchn
; /* event channel */
90 unsigned short cpu
; /* cpu bound */
103 #define PIRQ_NEEDS_EOI (1 << 0)
104 #define PIRQ_SHAREABLE (1 << 1)
106 static struct irq_info
*irq_info
;
107 static int *pirq_to_irq
;
110 static int *evtchn_to_irq
;
111 struct cpu_evtchn_s
{
112 unsigned long bits
[NR_EVENT_CHANNELS
/BITS_PER_LONG
];
115 static __initdata
struct cpu_evtchn_s init_evtchn_mask
= {
116 .bits
[0 ... (NR_EVENT_CHANNELS
/BITS_PER_LONG
)-1] = ~0ul,
118 static struct cpu_evtchn_s
*cpu_evtchn_mask_p
= &init_evtchn_mask
;
120 static inline unsigned long *cpu_evtchn_mask(int cpu
)
122 return cpu_evtchn_mask_p
[cpu
].bits
;
125 /* Xen will never allocate port zero for any purpose. */
126 #define VALID_EVTCHN(chn) ((chn) != 0)
128 static struct irq_chip xen_dynamic_chip
;
129 static struct irq_chip xen_percpu_chip
;
130 static struct irq_chip xen_pirq_chip
;
132 /* Constructor for packed IRQ information. */
133 static struct irq_info
mk_unbound_info(void)
135 return (struct irq_info
) { .type
= IRQT_UNBOUND
};
138 static struct irq_info
mk_evtchn_info(unsigned short evtchn
)
140 return (struct irq_info
) { .type
= IRQT_EVTCHN
, .evtchn
= evtchn
,
144 static struct irq_info
mk_ipi_info(unsigned short evtchn
, enum ipi_vector ipi
)
146 return (struct irq_info
) { .type
= IRQT_IPI
, .evtchn
= evtchn
,
147 .cpu
= 0, .u
.ipi
= ipi
};
150 static struct irq_info
mk_virq_info(unsigned short evtchn
, unsigned short virq
)
152 return (struct irq_info
) { .type
= IRQT_VIRQ
, .evtchn
= evtchn
,
153 .cpu
= 0, .u
.virq
= virq
};
156 static struct irq_info
mk_pirq_info(unsigned short evtchn
, unsigned short pirq
,
157 unsigned short gsi
, unsigned short vector
)
159 return (struct irq_info
) { .type
= IRQT_PIRQ
, .evtchn
= evtchn
,
161 .u
.pirq
= { .pirq
= pirq
, .gsi
= gsi
, .vector
= vector
} };
165 * Accessors for packed IRQ information.
167 static struct irq_info
*info_for_irq(unsigned irq
)
169 return &irq_info
[irq
];
172 static unsigned int evtchn_from_irq(unsigned irq
)
174 return info_for_irq(irq
)->evtchn
;
177 unsigned irq_from_evtchn(unsigned int evtchn
)
179 return evtchn_to_irq
[evtchn
];
181 EXPORT_SYMBOL_GPL(irq_from_evtchn
);
183 static enum ipi_vector
ipi_from_irq(unsigned irq
)
185 struct irq_info
*info
= info_for_irq(irq
);
187 BUG_ON(info
== NULL
);
188 BUG_ON(info
->type
!= IRQT_IPI
);
193 static unsigned virq_from_irq(unsigned irq
)
195 struct irq_info
*info
= info_for_irq(irq
);
197 BUG_ON(info
== NULL
);
198 BUG_ON(info
->type
!= IRQT_VIRQ
);
203 static unsigned pirq_from_irq(unsigned irq
)
205 struct irq_info
*info
= info_for_irq(irq
);
207 BUG_ON(info
== NULL
);
208 BUG_ON(info
->type
!= IRQT_PIRQ
);
210 return info
->u
.pirq
.pirq
;
213 static unsigned gsi_from_irq(unsigned irq
)
215 struct irq_info
*info
= info_for_irq(irq
);
217 BUG_ON(info
== NULL
);
218 BUG_ON(info
->type
!= IRQT_PIRQ
);
220 return info
->u
.pirq
.gsi
;
223 static unsigned vector_from_irq(unsigned irq
)
225 struct irq_info
*info
= info_for_irq(irq
);
227 BUG_ON(info
== NULL
);
228 BUG_ON(info
->type
!= IRQT_PIRQ
);
230 return info
->u
.pirq
.vector
;
233 static enum xen_irq_type
type_from_irq(unsigned irq
)
235 return info_for_irq(irq
)->type
;
238 static unsigned cpu_from_irq(unsigned irq
)
240 return info_for_irq(irq
)->cpu
;
243 static unsigned int cpu_from_evtchn(unsigned int evtchn
)
245 int irq
= evtchn_to_irq
[evtchn
];
249 ret
= cpu_from_irq(irq
);
254 static bool pirq_needs_eoi(unsigned irq
)
256 struct irq_info
*info
= info_for_irq(irq
);
258 BUG_ON(info
->type
!= IRQT_PIRQ
);
260 return info
->u
.pirq
.flags
& PIRQ_NEEDS_EOI
;
263 static inline unsigned long active_evtchns(unsigned int cpu
,
264 struct shared_info
*sh
,
267 return (sh
->evtchn_pending
[idx
] &
268 cpu_evtchn_mask(cpu
)[idx
] &
269 ~sh
->evtchn_mask
[idx
]);
272 static void bind_evtchn_to_cpu(unsigned int chn
, unsigned int cpu
)
274 int irq
= evtchn_to_irq
[chn
];
278 cpumask_copy(irq_to_desc(irq
)->affinity
, cpumask_of(cpu
));
281 clear_bit(chn
, cpu_evtchn_mask(cpu_from_irq(irq
)));
282 set_bit(chn
, cpu_evtchn_mask(cpu
));
284 irq_info
[irq
].cpu
= cpu
;
287 static void init_evtchn_cpu_bindings(void)
291 struct irq_desc
*desc
;
293 /* By default all event channels notify CPU#0. */
294 for_each_irq_desc(i
, desc
) {
295 cpumask_copy(desc
->affinity
, cpumask_of(0));
299 for_each_possible_cpu(i
)
300 memset(cpu_evtchn_mask(i
),
301 (i
== 0) ? ~0 : 0, sizeof(struct cpu_evtchn_s
));
305 static inline void clear_evtchn(int port
)
307 struct shared_info
*s
= HYPERVISOR_shared_info
;
308 sync_clear_bit(port
, &s
->evtchn_pending
[0]);
311 static inline void set_evtchn(int port
)
313 struct shared_info
*s
= HYPERVISOR_shared_info
;
314 sync_set_bit(port
, &s
->evtchn_pending
[0]);
317 static inline int test_evtchn(int port
)
319 struct shared_info
*s
= HYPERVISOR_shared_info
;
320 return sync_test_bit(port
, &s
->evtchn_pending
[0]);
325 * notify_remote_via_irq - send event to remote end of event channel via irq
326 * @irq: irq of event channel to send event to
328 * Unlike notify_remote_via_evtchn(), this is safe to use across
329 * save/restore. Notifications on a broken connection are silently
332 void notify_remote_via_irq(int irq
)
334 int evtchn
= evtchn_from_irq(irq
);
336 if (VALID_EVTCHN(evtchn
))
337 notify_remote_via_evtchn(evtchn
);
339 EXPORT_SYMBOL_GPL(notify_remote_via_irq
);
341 static void mask_evtchn(int port
)
343 struct shared_info
*s
= HYPERVISOR_shared_info
;
344 sync_set_bit(port
, &s
->evtchn_mask
[0]);
347 static void unmask_evtchn(int port
)
349 struct shared_info
*s
= HYPERVISOR_shared_info
;
350 unsigned int cpu
= get_cpu();
352 BUG_ON(!irqs_disabled());
354 /* Slow path (hypercall) if this is a non-local port. */
355 if (unlikely(cpu
!= cpu_from_evtchn(port
))) {
356 struct evtchn_unmask unmask
= { .port
= port
};
357 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask
, &unmask
);
359 struct vcpu_info
*vcpu_info
= __get_cpu_var(xen_vcpu
);
361 sync_clear_bit(port
, &s
->evtchn_mask
[0]);
364 * The following is basically the equivalent of
365 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
366 * the interrupt edge' if the channel is masked.
368 if (sync_test_bit(port
, &s
->evtchn_pending
[0]) &&
369 !sync_test_and_set_bit(port
/ BITS_PER_LONG
,
370 &vcpu_info
->evtchn_pending_sel
))
371 vcpu_info
->evtchn_upcall_pending
= 1;
377 static int get_nr_hw_irqs(void)
381 #ifdef CONFIG_X86_IO_APIC
382 ret
= get_nr_irqs_gsi();
388 /* callers of this function should make sure that PHYSDEVOP_get_nr_pirqs
389 * succeeded otherwise nr_pirqs won't hold the right value */
390 static int find_unbound_pirq(void)
393 for (i
= nr_pirqs
-1; i
>= 0; i
--) {
394 if (pirq_to_irq
[i
] < 0)
400 static int find_unbound_irq(void)
402 struct irq_data
*data
;
404 int start
= get_nr_hw_irqs();
406 if (start
== nr_irqs
)
409 /* nr_irqs is a magic value. Must not use it.*/
410 for (irq
= nr_irqs
-1; irq
> start
; irq
--) {
411 data
= irq_get_irq_data(irq
);
412 /* only 0->15 have init'd desc; handle irq > 16 */
415 if (data
->chip
== &no_irq_chip
)
417 if (data
->chip
!= &xen_dynamic_chip
)
419 if (irq_info
[irq
].type
== IRQT_UNBOUND
)
426 res
= irq_alloc_desc_at(irq
, 0);
428 if (WARN_ON(res
!= irq
))
434 panic("No available IRQ to bind to: increase nr_irqs!\n");
437 static bool identity_mapped_irq(unsigned irq
)
439 /* identity map all the hardware irqs */
440 return irq
< get_nr_hw_irqs();
443 static void pirq_unmask_notify(int irq
)
445 struct physdev_eoi eoi
= { .irq
= pirq_from_irq(irq
) };
447 if (unlikely(pirq_needs_eoi(irq
))) {
448 int rc
= HYPERVISOR_physdev_op(PHYSDEVOP_eoi
, &eoi
);
453 static void pirq_query_unmask(int irq
)
455 struct physdev_irq_status_query irq_status
;
456 struct irq_info
*info
= info_for_irq(irq
);
458 BUG_ON(info
->type
!= IRQT_PIRQ
);
460 irq_status
.irq
= pirq_from_irq(irq
);
461 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query
, &irq_status
))
462 irq_status
.flags
= 0;
464 info
->u
.pirq
.flags
&= ~PIRQ_NEEDS_EOI
;
465 if (irq_status
.flags
& XENIRQSTAT_needs_eoi
)
466 info
->u
.pirq
.flags
|= PIRQ_NEEDS_EOI
;
469 static bool probing_irq(int irq
)
471 struct irq_desc
*desc
= irq_to_desc(irq
);
473 return desc
&& desc
->action
== NULL
;
476 static unsigned int startup_pirq(unsigned int irq
)
478 struct evtchn_bind_pirq bind_pirq
;
479 struct irq_info
*info
= info_for_irq(irq
);
480 int evtchn
= evtchn_from_irq(irq
);
483 BUG_ON(info
->type
!= IRQT_PIRQ
);
485 if (VALID_EVTCHN(evtchn
))
488 bind_pirq
.pirq
= pirq_from_irq(irq
);
489 /* NB. We are happy to share unless we are probing. */
490 bind_pirq
.flags
= info
->u
.pirq
.flags
& PIRQ_SHAREABLE
?
491 BIND_PIRQ__WILL_SHARE
: 0;
492 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq
, &bind_pirq
);
494 if (!probing_irq(irq
))
495 printk(KERN_INFO
"Failed to obtain physical IRQ %d\n",
499 evtchn
= bind_pirq
.port
;
501 pirq_query_unmask(irq
);
503 evtchn_to_irq
[evtchn
] = irq
;
504 bind_evtchn_to_cpu(evtchn
, 0);
505 info
->evtchn
= evtchn
;
508 unmask_evtchn(evtchn
);
509 pirq_unmask_notify(irq
);
514 static void shutdown_pirq(unsigned int irq
)
516 struct evtchn_close close
;
517 struct irq_info
*info
= info_for_irq(irq
);
518 int evtchn
= evtchn_from_irq(irq
);
520 BUG_ON(info
->type
!= IRQT_PIRQ
);
522 if (!VALID_EVTCHN(evtchn
))
528 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
531 bind_evtchn_to_cpu(evtchn
, 0);
532 evtchn_to_irq
[evtchn
] = -1;
536 static void enable_pirq(unsigned int irq
)
541 static void disable_pirq(unsigned int irq
)
545 static void ack_pirq(unsigned int irq
)
547 int evtchn
= evtchn_from_irq(irq
);
549 move_native_irq(irq
);
551 if (VALID_EVTCHN(evtchn
)) {
553 clear_evtchn(evtchn
);
557 static void end_pirq(unsigned int irq
)
559 int evtchn
= evtchn_from_irq(irq
);
560 struct irq_desc
*desc
= irq_to_desc(irq
);
565 if ((desc
->status
& (IRQ_DISABLED
|IRQ_PENDING
)) ==
566 (IRQ_DISABLED
|IRQ_PENDING
)) {
568 } else if (VALID_EVTCHN(evtchn
)) {
569 unmask_evtchn(evtchn
);
570 pirq_unmask_notify(irq
);
574 static int find_irq_by_gsi(unsigned gsi
)
578 for (irq
= 0; irq
< nr_irqs
; irq
++) {
579 struct irq_info
*info
= info_for_irq(irq
);
581 if (info
== NULL
|| info
->type
!= IRQT_PIRQ
)
584 if (gsi_from_irq(irq
) == gsi
)
591 int xen_allocate_pirq(unsigned gsi
, int shareable
, char *name
)
593 return xen_map_pirq_gsi(gsi
, gsi
, shareable
, name
);
596 /* xen_map_pirq_gsi might allocate irqs from the top down, as a
597 * consequence don't assume that the irq number returned has a low value
598 * or can be used as a pirq number unless you know otherwise.
600 * One notable exception is when xen_map_pirq_gsi is called passing an
601 * hardware gsi as argument, in that case the irq number returned
602 * matches the gsi number passed as second argument.
604 * Note: We don't assign an event channel until the irq actually started
605 * up. Return an existing irq if we've already got one for the gsi.
607 int xen_map_pirq_gsi(unsigned pirq
, unsigned gsi
, int shareable
, char *name
)
610 struct physdev_irq irq_op
;
612 spin_lock(&irq_mapping_update_lock
);
614 if ((pirq
> nr_pirqs
) || (gsi
> nr_irqs
)) {
615 printk(KERN_WARNING
"xen_map_pirq_gsi: %s %s is incorrect!\n",
616 pirq
> nr_pirqs
? "nr_pirqs" :"",
617 gsi
> nr_irqs
? "nr_irqs" : "");
621 irq
= find_irq_by_gsi(gsi
);
623 printk(KERN_INFO
"xen_map_pirq_gsi: returning irq %d for gsi %u\n",
625 goto out
; /* XXX need refcount? */
628 /* If we are a PV guest, we don't have GSIs (no ACPI passed). Therefore
629 * we are using the !xen_initial_domain() to drop in the function.*/
630 if (identity_mapped_irq(gsi
) || (!xen_initial_domain() &&
633 irq_alloc_desc_at(irq
, 0);
635 irq
= find_unbound_irq();
637 set_irq_chip_and_handler_name(irq
, &xen_pirq_chip
,
638 handle_level_irq
, name
);
643 /* Only the privileged domain can do this. For non-priv, the pcifront
644 * driver provides a PCI bus that does the call to do exactly
645 * this in the priv domain. */
646 if (xen_initial_domain() &&
647 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector
, &irq_op
)) {
653 irq_info
[irq
] = mk_pirq_info(0, pirq
, gsi
, irq_op
.vector
);
654 irq_info
[irq
].u
.pirq
.flags
|= shareable
? PIRQ_SHAREABLE
: 0;
655 pirq_to_irq
[pirq
] = irq
;
658 spin_unlock(&irq_mapping_update_lock
);
663 #ifdef CONFIG_PCI_MSI
664 #include <linux/msi.h>
665 #include "../pci/msi.h"
667 void xen_allocate_pirq_msi(char *name
, int *irq
, int *pirq
)
669 spin_lock(&irq_mapping_update_lock
);
671 *irq
= find_unbound_irq();
675 *pirq
= find_unbound_pirq();
679 set_irq_chip_and_handler_name(*irq
, &xen_pirq_chip
,
680 handle_level_irq
, name
);
682 irq_info
[*irq
] = mk_pirq_info(0, *pirq
, 0, 0);
683 pirq_to_irq
[*pirq
] = *irq
;
686 spin_unlock(&irq_mapping_update_lock
);
689 int xen_create_msi_irq(struct pci_dev
*dev
, struct msi_desc
*msidesc
, int type
)
692 struct physdev_map_pirq map_irq
;
695 u32 table_offset
, bir
;
697 memset(&map_irq
, 0, sizeof(map_irq
));
698 map_irq
.domid
= DOMID_SELF
;
699 map_irq
.type
= MAP_PIRQ_TYPE_MSI
;
702 map_irq
.bus
= dev
->bus
->number
;
703 map_irq
.devfn
= dev
->devfn
;
705 if (type
== PCI_CAP_ID_MSIX
) {
706 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
708 pci_read_config_dword(dev
, msix_table_offset_reg(pos
),
710 bir
= (u8
)(table_offset
& PCI_MSIX_FLAGS_BIRMASK
);
712 map_irq
.table_base
= pci_resource_start(dev
, bir
);
713 map_irq
.entry_nr
= msidesc
->msi_attrib
.entry_nr
;
716 spin_lock(&irq_mapping_update_lock
);
718 irq
= find_unbound_irq();
723 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq
, &map_irq
);
725 printk(KERN_WARNING
"xen map irq failed %d\n", rc
);
732 irq_info
[irq
] = mk_pirq_info(0, map_irq
.pirq
, 0, map_irq
.index
);
734 set_irq_chip_and_handler_name(irq
, &xen_pirq_chip
,
736 (type
== PCI_CAP_ID_MSIX
) ? "msi-x":"msi");
739 spin_unlock(&irq_mapping_update_lock
);
744 int xen_destroy_irq(int irq
)
746 struct irq_desc
*desc
;
747 struct physdev_unmap_pirq unmap_irq
;
748 struct irq_info
*info
= info_for_irq(irq
);
751 spin_lock(&irq_mapping_update_lock
);
753 desc
= irq_to_desc(irq
);
757 if (xen_initial_domain()) {
758 unmap_irq
.pirq
= info
->u
.pirq
.pirq
;
759 unmap_irq
.domid
= DOMID_SELF
;
760 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq
, &unmap_irq
);
762 printk(KERN_WARNING
"unmap irq failed %d\n", rc
);
766 irq_info
[irq
] = mk_unbound_info();
771 spin_unlock(&irq_mapping_update_lock
);
775 int xen_vector_from_irq(unsigned irq
)
777 return vector_from_irq(irq
);
780 int xen_gsi_from_irq(unsigned irq
)
782 return gsi_from_irq(irq
);
785 int bind_evtchn_to_irq(unsigned int evtchn
)
789 spin_lock(&irq_mapping_update_lock
);
791 irq
= evtchn_to_irq
[evtchn
];
794 irq
= find_unbound_irq();
796 set_irq_chip_and_handler_name(irq
, &xen_dynamic_chip
,
797 handle_fasteoi_irq
, "event");
799 evtchn_to_irq
[evtchn
] = irq
;
800 irq_info
[irq
] = mk_evtchn_info(evtchn
);
803 spin_unlock(&irq_mapping_update_lock
);
807 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq
);
809 static int bind_ipi_to_irq(unsigned int ipi
, unsigned int cpu
)
811 struct evtchn_bind_ipi bind_ipi
;
814 spin_lock(&irq_mapping_update_lock
);
816 irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
];
819 irq
= find_unbound_irq();
823 set_irq_chip_and_handler_name(irq
, &xen_percpu_chip
,
824 handle_percpu_irq
, "ipi");
827 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
830 evtchn
= bind_ipi
.port
;
832 evtchn_to_irq
[evtchn
] = irq
;
833 irq_info
[irq
] = mk_ipi_info(evtchn
, ipi
);
834 per_cpu(ipi_to_irq
, cpu
)[ipi
] = irq
;
836 bind_evtchn_to_cpu(evtchn
, cpu
);
840 spin_unlock(&irq_mapping_update_lock
);
845 int bind_virq_to_irq(unsigned int virq
, unsigned int cpu
)
847 struct evtchn_bind_virq bind_virq
;
850 spin_lock(&irq_mapping_update_lock
);
852 irq
= per_cpu(virq_to_irq
, cpu
)[virq
];
855 irq
= find_unbound_irq();
857 set_irq_chip_and_handler_name(irq
, &xen_percpu_chip
,
858 handle_percpu_irq
, "virq");
860 bind_virq
.virq
= virq
;
861 bind_virq
.vcpu
= cpu
;
862 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
865 evtchn
= bind_virq
.port
;
867 evtchn_to_irq
[evtchn
] = irq
;
868 irq_info
[irq
] = mk_virq_info(evtchn
, virq
);
870 per_cpu(virq_to_irq
, cpu
)[virq
] = irq
;
872 bind_evtchn_to_cpu(evtchn
, cpu
);
875 spin_unlock(&irq_mapping_update_lock
);
880 static void unbind_from_irq(unsigned int irq
)
882 struct evtchn_close close
;
883 int evtchn
= evtchn_from_irq(irq
);
885 spin_lock(&irq_mapping_update_lock
);
887 if (VALID_EVTCHN(evtchn
)) {
889 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
892 switch (type_from_irq(irq
)) {
894 per_cpu(virq_to_irq
, cpu_from_evtchn(evtchn
))
895 [virq_from_irq(irq
)] = -1;
898 per_cpu(ipi_to_irq
, cpu_from_evtchn(evtchn
))
899 [ipi_from_irq(irq
)] = -1;
905 /* Closed ports are implicitly re-bound to VCPU0. */
906 bind_evtchn_to_cpu(evtchn
, 0);
908 evtchn_to_irq
[evtchn
] = -1;
911 if (irq_info
[irq
].type
!= IRQT_UNBOUND
) {
912 irq_info
[irq
] = mk_unbound_info();
917 spin_unlock(&irq_mapping_update_lock
);
920 int bind_evtchn_to_irqhandler(unsigned int evtchn
,
921 irq_handler_t handler
,
922 unsigned long irqflags
,
923 const char *devname
, void *dev_id
)
928 irq
= bind_evtchn_to_irq(evtchn
);
929 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
931 unbind_from_irq(irq
);
937 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler
);
939 int bind_virq_to_irqhandler(unsigned int virq
, unsigned int cpu
,
940 irq_handler_t handler
,
941 unsigned long irqflags
, const char *devname
, void *dev_id
)
946 irq
= bind_virq_to_irq(virq
, cpu
);
947 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
949 unbind_from_irq(irq
);
955 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler
);
957 int bind_ipi_to_irqhandler(enum ipi_vector ipi
,
959 irq_handler_t handler
,
960 unsigned long irqflags
,
966 irq
= bind_ipi_to_irq(ipi
, cpu
);
970 irqflags
|= IRQF_NO_SUSPEND
;
971 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
973 unbind_from_irq(irq
);
980 void unbind_from_irqhandler(unsigned int irq
, void *dev_id
)
982 free_irq(irq
, dev_id
);
983 unbind_from_irq(irq
);
985 EXPORT_SYMBOL_GPL(unbind_from_irqhandler
);
987 void xen_send_IPI_one(unsigned int cpu
, enum ipi_vector vector
)
989 int irq
= per_cpu(ipi_to_irq
, cpu
)[vector
];
991 notify_remote_via_irq(irq
);
994 irqreturn_t
xen_debug_interrupt(int irq
, void *dev_id
)
996 struct shared_info
*sh
= HYPERVISOR_shared_info
;
997 int cpu
= smp_processor_id();
998 unsigned long *cpu_evtchn
= cpu_evtchn_mask(cpu
);
1000 unsigned long flags
;
1001 static DEFINE_SPINLOCK(debug_lock
);
1002 struct vcpu_info
*v
;
1004 spin_lock_irqsave(&debug_lock
, flags
);
1006 printk("\nvcpu %d\n ", cpu
);
1008 for_each_online_cpu(i
) {
1010 v
= per_cpu(xen_vcpu
, i
);
1011 pending
= (get_irq_regs() && i
== cpu
)
1012 ? xen_irqs_disabled(get_irq_regs())
1013 : v
->evtchn_upcall_mask
;
1014 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i
,
1015 pending
, v
->evtchn_upcall_pending
,
1016 (int)(sizeof(v
->evtchn_pending_sel
)*2),
1017 v
->evtchn_pending_sel
);
1019 v
= per_cpu(xen_vcpu
, cpu
);
1021 printk("\npending:\n ");
1022 for (i
= ARRAY_SIZE(sh
->evtchn_pending
)-1; i
>= 0; i
--)
1023 printk("%0*lx%s", (int)sizeof(sh
->evtchn_pending
[0])*2,
1024 sh
->evtchn_pending
[i
],
1025 i
% 8 == 0 ? "\n " : " ");
1026 printk("\nglobal mask:\n ");
1027 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
1029 (int)(sizeof(sh
->evtchn_mask
[0])*2),
1031 i
% 8 == 0 ? "\n " : " ");
1033 printk("\nglobally unmasked:\n ");
1034 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
1035 printk("%0*lx%s", (int)(sizeof(sh
->evtchn_mask
[0])*2),
1036 sh
->evtchn_pending
[i
] & ~sh
->evtchn_mask
[i
],
1037 i
% 8 == 0 ? "\n " : " ");
1039 printk("\nlocal cpu%d mask:\n ", cpu
);
1040 for (i
= (NR_EVENT_CHANNELS
/BITS_PER_LONG
)-1; i
>= 0; i
--)
1041 printk("%0*lx%s", (int)(sizeof(cpu_evtchn
[0])*2),
1043 i
% 8 == 0 ? "\n " : " ");
1045 printk("\nlocally unmasked:\n ");
1046 for (i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--) {
1047 unsigned long pending
= sh
->evtchn_pending
[i
]
1048 & ~sh
->evtchn_mask
[i
]
1050 printk("%0*lx%s", (int)(sizeof(sh
->evtchn_mask
[0])*2),
1051 pending
, i
% 8 == 0 ? "\n " : " ");
1054 printk("\npending list:\n");
1055 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++) {
1056 if (sync_test_bit(i
, sh
->evtchn_pending
)) {
1057 int word_idx
= i
/ BITS_PER_LONG
;
1058 printk(" %d: event %d -> irq %d%s%s%s\n",
1059 cpu_from_evtchn(i
), i
,
1061 sync_test_bit(word_idx
, &v
->evtchn_pending_sel
)
1063 !sync_test_bit(i
, sh
->evtchn_mask
)
1064 ? "" : " globally-masked",
1065 sync_test_bit(i
, cpu_evtchn
)
1066 ? "" : " locally-masked");
1070 spin_unlock_irqrestore(&debug_lock
, flags
);
1075 static DEFINE_PER_CPU(unsigned, xed_nesting_count
);
1078 * Search the CPUs pending events bitmasks. For each one found, map
1079 * the event number to an irq, and feed it into do_IRQ() for
1082 * Xen uses a two-level bitmap to speed searching. The first level is
1083 * a bitset of words which contain pending event bits. The second
1084 * level is a bitset of pending events themselves.
1086 static void __xen_evtchn_do_upcall(void)
1088 int cpu
= get_cpu();
1089 struct shared_info
*s
= HYPERVISOR_shared_info
;
1090 struct vcpu_info
*vcpu_info
= __get_cpu_var(xen_vcpu
);
1094 unsigned long pending_words
;
1096 vcpu_info
->evtchn_upcall_pending
= 0;
1098 if (__get_cpu_var(xed_nesting_count
)++)
1101 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1102 /* Clear master flag /before/ clearing selector flag. */
1105 pending_words
= xchg(&vcpu_info
->evtchn_pending_sel
, 0);
1106 while (pending_words
!= 0) {
1107 unsigned long pending_bits
;
1108 int word_idx
= __ffs(pending_words
);
1109 pending_words
&= ~(1UL << word_idx
);
1111 while ((pending_bits
= active_evtchns(cpu
, s
, word_idx
)) != 0) {
1112 int bit_idx
= __ffs(pending_bits
);
1113 int port
= (word_idx
* BITS_PER_LONG
) + bit_idx
;
1114 int irq
= evtchn_to_irq
[port
];
1115 struct irq_desc
*desc
;
1121 desc
= irq_to_desc(irq
);
1123 generic_handle_irq_desc(irq
, desc
);
1128 BUG_ON(!irqs_disabled());
1130 count
= __get_cpu_var(xed_nesting_count
);
1131 __get_cpu_var(xed_nesting_count
) = 0;
1132 } while (count
!= 1 || vcpu_info
->evtchn_upcall_pending
);
1139 void xen_evtchn_do_upcall(struct pt_regs
*regs
)
1141 struct pt_regs
*old_regs
= set_irq_regs(regs
);
1146 __xen_evtchn_do_upcall();
1149 set_irq_regs(old_regs
);
1152 void xen_hvm_evtchn_do_upcall(void)
1154 __xen_evtchn_do_upcall();
1156 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall
);
1158 /* Rebind a new event channel to an existing irq. */
1159 void rebind_evtchn_irq(int evtchn
, int irq
)
1161 struct irq_info
*info
= info_for_irq(irq
);
1163 /* Make sure the irq is masked, since the new event channel
1164 will also be masked. */
1167 spin_lock(&irq_mapping_update_lock
);
1169 /* After resume the irq<->evtchn mappings are all cleared out */
1170 BUG_ON(evtchn_to_irq
[evtchn
] != -1);
1171 /* Expect irq to have been bound before,
1172 so there should be a proper type */
1173 BUG_ON(info
->type
== IRQT_UNBOUND
);
1175 evtchn_to_irq
[evtchn
] = irq
;
1176 irq_info
[irq
] = mk_evtchn_info(evtchn
);
1178 spin_unlock(&irq_mapping_update_lock
);
1180 /* new event channels are always bound to cpu 0 */
1181 irq_set_affinity(irq
, cpumask_of(0));
1183 /* Unmask the event channel. */
1187 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1188 static int rebind_irq_to_cpu(unsigned irq
, unsigned tcpu
)
1190 struct evtchn_bind_vcpu bind_vcpu
;
1191 int evtchn
= evtchn_from_irq(irq
);
1193 /* events delivered via platform PCI interrupts are always
1194 * routed to vcpu 0 */
1195 if (!VALID_EVTCHN(evtchn
) ||
1196 (xen_hvm_domain() && !xen_have_vector_callback
))
1199 /* Send future instances of this interrupt to other vcpu. */
1200 bind_vcpu
.port
= evtchn
;
1201 bind_vcpu
.vcpu
= tcpu
;
1204 * If this fails, it usually just indicates that we're dealing with a
1205 * virq or IPI channel, which don't actually need to be rebound. Ignore
1206 * it, but don't do the xenlinux-level rebind in that case.
1208 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu
, &bind_vcpu
) >= 0)
1209 bind_evtchn_to_cpu(evtchn
, tcpu
);
1214 static int set_affinity_irq(unsigned irq
, const struct cpumask
*dest
)
1216 unsigned tcpu
= cpumask_first(dest
);
1218 return rebind_irq_to_cpu(irq
, tcpu
);
1221 int resend_irq_on_evtchn(unsigned int irq
)
1223 int masked
, evtchn
= evtchn_from_irq(irq
);
1224 struct shared_info
*s
= HYPERVISOR_shared_info
;
1226 if (!VALID_EVTCHN(evtchn
))
1229 masked
= sync_test_and_set_bit(evtchn
, s
->evtchn_mask
);
1230 sync_set_bit(evtchn
, s
->evtchn_pending
);
1232 unmask_evtchn(evtchn
);
1237 static void enable_dynirq(unsigned int irq
)
1239 int evtchn
= evtchn_from_irq(irq
);
1241 if (VALID_EVTCHN(evtchn
))
1242 unmask_evtchn(evtchn
);
1245 static void disable_dynirq(unsigned int irq
)
1247 int evtchn
= evtchn_from_irq(irq
);
1249 if (VALID_EVTCHN(evtchn
))
1250 mask_evtchn(evtchn
);
1253 static void ack_dynirq(unsigned int irq
)
1255 int evtchn
= evtchn_from_irq(irq
);
1257 move_masked_irq(irq
);
1259 if (VALID_EVTCHN(evtchn
))
1260 unmask_evtchn(evtchn
);
1263 static int retrigger_dynirq(unsigned int irq
)
1265 int evtchn
= evtchn_from_irq(irq
);
1266 struct shared_info
*sh
= HYPERVISOR_shared_info
;
1269 if (VALID_EVTCHN(evtchn
)) {
1272 masked
= sync_test_and_set_bit(evtchn
, sh
->evtchn_mask
);
1273 sync_set_bit(evtchn
, sh
->evtchn_pending
);
1275 unmask_evtchn(evtchn
);
1282 static void restore_cpu_virqs(unsigned int cpu
)
1284 struct evtchn_bind_virq bind_virq
;
1285 int virq
, irq
, evtchn
;
1287 for (virq
= 0; virq
< NR_VIRQS
; virq
++) {
1288 if ((irq
= per_cpu(virq_to_irq
, cpu
)[virq
]) == -1)
1291 BUG_ON(virq_from_irq(irq
) != virq
);
1293 /* Get a new binding from Xen. */
1294 bind_virq
.virq
= virq
;
1295 bind_virq
.vcpu
= cpu
;
1296 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
1299 evtchn
= bind_virq
.port
;
1301 /* Record the new mapping. */
1302 evtchn_to_irq
[evtchn
] = irq
;
1303 irq_info
[irq
] = mk_virq_info(evtchn
, virq
);
1304 bind_evtchn_to_cpu(evtchn
, cpu
);
1308 static void restore_cpu_ipis(unsigned int cpu
)
1310 struct evtchn_bind_ipi bind_ipi
;
1311 int ipi
, irq
, evtchn
;
1313 for (ipi
= 0; ipi
< XEN_NR_IPIS
; ipi
++) {
1314 if ((irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
]) == -1)
1317 BUG_ON(ipi_from_irq(irq
) != ipi
);
1319 /* Get a new binding from Xen. */
1320 bind_ipi
.vcpu
= cpu
;
1321 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
1324 evtchn
= bind_ipi
.port
;
1326 /* Record the new mapping. */
1327 evtchn_to_irq
[evtchn
] = irq
;
1328 irq_info
[irq
] = mk_ipi_info(evtchn
, ipi
);
1329 bind_evtchn_to_cpu(evtchn
, cpu
);
1333 /* Clear an irq's pending state, in preparation for polling on it */
1334 void xen_clear_irq_pending(int irq
)
1336 int evtchn
= evtchn_from_irq(irq
);
1338 if (VALID_EVTCHN(evtchn
))
1339 clear_evtchn(evtchn
);
1341 EXPORT_SYMBOL(xen_clear_irq_pending
);
1342 void xen_set_irq_pending(int irq
)
1344 int evtchn
= evtchn_from_irq(irq
);
1346 if (VALID_EVTCHN(evtchn
))
1350 bool xen_test_irq_pending(int irq
)
1352 int evtchn
= evtchn_from_irq(irq
);
1355 if (VALID_EVTCHN(evtchn
))
1356 ret
= test_evtchn(evtchn
);
1361 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1362 * the irq will be disabled so it won't deliver an interrupt. */
1363 void xen_poll_irq_timeout(int irq
, u64 timeout
)
1365 evtchn_port_t evtchn
= evtchn_from_irq(irq
);
1367 if (VALID_EVTCHN(evtchn
)) {
1368 struct sched_poll poll
;
1371 poll
.timeout
= timeout
;
1372 set_xen_guest_handle(poll
.ports
, &evtchn
);
1374 if (HYPERVISOR_sched_op(SCHEDOP_poll
, &poll
) != 0)
1378 EXPORT_SYMBOL(xen_poll_irq_timeout
);
1379 /* Poll waiting for an irq to become pending. In the usual case, the
1380 * irq will be disabled so it won't deliver an interrupt. */
1381 void xen_poll_irq(int irq
)
1383 xen_poll_irq_timeout(irq
, 0 /* no timeout */);
1386 void xen_irq_resume(void)
1388 unsigned int cpu
, irq
, evtchn
;
1389 struct irq_desc
*desc
;
1391 init_evtchn_cpu_bindings();
1393 /* New event-channel space is not 'live' yet. */
1394 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1395 mask_evtchn(evtchn
);
1397 /* No IRQ <-> event-channel mappings. */
1398 for (irq
= 0; irq
< nr_irqs
; irq
++)
1399 irq_info
[irq
].evtchn
= 0; /* zap event-channel binding */
1401 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1402 evtchn_to_irq
[evtchn
] = -1;
1404 for_each_possible_cpu(cpu
) {
1405 restore_cpu_virqs(cpu
);
1406 restore_cpu_ipis(cpu
);
1410 * Unmask any IRQF_NO_SUSPEND IRQs which are enabled. These
1411 * are not handled by the IRQ core.
1413 for_each_irq_desc(irq
, desc
) {
1414 if (!desc
->action
|| !(desc
->action
->flags
& IRQF_NO_SUSPEND
))
1416 if (desc
->status
& IRQ_DISABLED
)
1419 evtchn
= evtchn_from_irq(irq
);
1423 unmask_evtchn(evtchn
);
1427 static struct irq_chip xen_dynamic_chip __read_mostly
= {
1430 .disable
= disable_dynirq
,
1431 .mask
= disable_dynirq
,
1432 .unmask
= enable_dynirq
,
1435 .set_affinity
= set_affinity_irq
,
1436 .retrigger
= retrigger_dynirq
,
1439 static struct irq_chip xen_pirq_chip __read_mostly
= {
1442 .startup
= startup_pirq
,
1443 .shutdown
= shutdown_pirq
,
1445 .enable
= enable_pirq
,
1446 .unmask
= enable_pirq
,
1448 .disable
= disable_pirq
,
1449 .mask
= disable_pirq
,
1454 .set_affinity
= set_affinity_irq
,
1456 .retrigger
= retrigger_dynirq
,
1459 static struct irq_chip xen_percpu_chip __read_mostly
= {
1460 .name
= "xen-percpu",
1462 .disable
= disable_dynirq
,
1463 .mask
= disable_dynirq
,
1464 .unmask
= enable_dynirq
,
1469 int xen_set_callback_via(uint64_t via
)
1471 struct xen_hvm_param a
;
1472 a
.domid
= DOMID_SELF
;
1473 a
.index
= HVM_PARAM_CALLBACK_IRQ
;
1475 return HYPERVISOR_hvm_op(HVMOP_set_param
, &a
);
1477 EXPORT_SYMBOL_GPL(xen_set_callback_via
);
1479 #ifdef CONFIG_XEN_PVHVM
1480 /* Vector callbacks are better than PCI interrupts to receive event
1481 * channel notifications because we can receive vector callbacks on any
1482 * vcpu and we don't need PCI support or APIC interactions. */
1483 void xen_callback_vector(void)
1486 uint64_t callback_via
;
1487 if (xen_have_vector_callback
) {
1488 callback_via
= HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK
);
1489 rc
= xen_set_callback_via(callback_via
);
1491 printk(KERN_ERR
"Request for Xen HVM callback vector"
1493 xen_have_vector_callback
= 0;
1496 printk(KERN_INFO
"Xen HVM callback vector for event delivery is "
1498 /* in the restore case the vector has already been allocated */
1499 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK
, used_vectors
))
1500 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK
, xen_hvm_callback_vector
);
1504 void xen_callback_vector(void) {}
1507 void __init
xen_init_IRQ(void)
1510 struct physdev_nr_pirqs op_nr_pirqs
;
1512 cpu_evtchn_mask_p
= kcalloc(nr_cpu_ids
, sizeof(struct cpu_evtchn_s
),
1514 irq_info
= kcalloc(nr_irqs
, sizeof(*irq_info
), GFP_KERNEL
);
1516 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_get_nr_pirqs
, &op_nr_pirqs
);
1520 printk(KERN_WARNING
"PHYSDEVOP_get_nr_pirqs returned rc=%d\n", rc
);
1522 if (xen_pv_domain() && !xen_initial_domain())
1523 nr_pirqs
= max((int)op_nr_pirqs
.nr_pirqs
, nr_irqs
);
1525 nr_pirqs
= op_nr_pirqs
.nr_pirqs
;
1527 pirq_to_irq
= kcalloc(nr_pirqs
, sizeof(*pirq_to_irq
), GFP_KERNEL
);
1528 for (i
= 0; i
< nr_pirqs
; i
++)
1529 pirq_to_irq
[i
] = -1;
1531 evtchn_to_irq
= kcalloc(NR_EVENT_CHANNELS
, sizeof(*evtchn_to_irq
),
1533 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1534 evtchn_to_irq
[i
] = -1;
1536 init_evtchn_cpu_bindings();
1538 /* No event channels are 'live' right now. */
1539 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1542 if (xen_hvm_domain()) {
1543 xen_callback_vector();
1545 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1546 * __acpi_register_gsi can point at the right function */
1549 irq_ctx_init(smp_processor_id());
1550 if (xen_initial_domain())