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>
34 #include <asm/ptrace.h>
37 #include <asm/io_apic.h>
38 #include <asm/sync_bitops.h>
39 #include <asm/xen/hypercall.h>
40 #include <asm/xen/hypervisor.h>
44 #include <xen/xen-ops.h>
45 #include <xen/events.h>
46 #include <xen/interface/xen.h>
47 #include <xen/interface/event_channel.h>
48 #include <xen/interface/hvm/hvm_op.h>
49 #include <xen/interface/hvm/params.h>
52 * This lock protects updates to the following mapping and reference-count
53 * arrays. The lock does not need to be acquired to read the mapping tables.
55 static DEFINE_SPINLOCK(irq_mapping_update_lock
);
57 /* IRQ <-> VIRQ mapping. */
58 static DEFINE_PER_CPU(int [NR_VIRQS
], virq_to_irq
) = {[0 ... NR_VIRQS
-1] = -1};
60 /* IRQ <-> IPI mapping */
61 static DEFINE_PER_CPU(int [XEN_NR_IPIS
], ipi_to_irq
) = {[0 ... XEN_NR_IPIS
-1] = -1};
63 /* Interrupt types. */
73 * Packed IRQ information:
74 * type - enum xen_irq_type
75 * event channel - irq->event channel mapping
76 * cpu - cpu this event channel is bound to
77 * index - type-specific information:
78 * PIRQ - vector, with MSB being "needs EIO"
85 enum xen_irq_type type
; /* type */
86 unsigned short evtchn
; /* event channel */
87 unsigned short cpu
; /* cpu bound */
99 #define PIRQ_NEEDS_EOI (1 << 0)
101 static struct irq_info
*irq_info
;
103 static int *evtchn_to_irq
;
104 struct cpu_evtchn_s
{
105 unsigned long bits
[NR_EVENT_CHANNELS
/BITS_PER_LONG
];
108 static __initdata
struct cpu_evtchn_s init_evtchn_mask
= {
109 .bits
[0 ... (NR_EVENT_CHANNELS
/BITS_PER_LONG
)-1] = ~0ul,
111 static struct cpu_evtchn_s
*cpu_evtchn_mask_p
= &init_evtchn_mask
;
113 static inline unsigned long *cpu_evtchn_mask(int cpu
)
115 return cpu_evtchn_mask_p
[cpu
].bits
;
118 /* Xen will never allocate port zero for any purpose. */
119 #define VALID_EVTCHN(chn) ((chn) != 0)
121 static struct irq_chip xen_dynamic_chip
;
122 static struct irq_chip xen_percpu_chip
;
123 static struct irq_chip xen_pirq_chip
;
125 /* Constructor for packed IRQ information. */
126 static struct irq_info
mk_unbound_info(void)
128 return (struct irq_info
) { .type
= IRQT_UNBOUND
};
131 static struct irq_info
mk_evtchn_info(unsigned short evtchn
)
133 return (struct irq_info
) { .type
= IRQT_EVTCHN
, .evtchn
= evtchn
,
137 static struct irq_info
mk_ipi_info(unsigned short evtchn
, enum ipi_vector ipi
)
139 return (struct irq_info
) { .type
= IRQT_IPI
, .evtchn
= evtchn
,
140 .cpu
= 0, .u
.ipi
= ipi
};
143 static struct irq_info
mk_virq_info(unsigned short evtchn
, unsigned short virq
)
145 return (struct irq_info
) { .type
= IRQT_VIRQ
, .evtchn
= evtchn
,
146 .cpu
= 0, .u
.virq
= virq
};
149 static struct irq_info
mk_pirq_info(unsigned short evtchn
,
150 unsigned short gsi
, unsigned short vector
)
152 return (struct irq_info
) { .type
= IRQT_PIRQ
, .evtchn
= evtchn
,
153 .cpu
= 0, .u
.pirq
= { .gsi
= gsi
, .vector
= vector
} };
157 * Accessors for packed IRQ information.
159 static struct irq_info
*info_for_irq(unsigned irq
)
161 return &irq_info
[irq
];
164 static unsigned int evtchn_from_irq(unsigned irq
)
166 return info_for_irq(irq
)->evtchn
;
169 unsigned irq_from_evtchn(unsigned int evtchn
)
171 return evtchn_to_irq
[evtchn
];
173 EXPORT_SYMBOL_GPL(irq_from_evtchn
);
175 static enum ipi_vector
ipi_from_irq(unsigned irq
)
177 struct irq_info
*info
= info_for_irq(irq
);
179 BUG_ON(info
== NULL
);
180 BUG_ON(info
->type
!= IRQT_IPI
);
185 static unsigned virq_from_irq(unsigned irq
)
187 struct irq_info
*info
= info_for_irq(irq
);
189 BUG_ON(info
== NULL
);
190 BUG_ON(info
->type
!= IRQT_VIRQ
);
195 static unsigned gsi_from_irq(unsigned irq
)
197 struct irq_info
*info
= info_for_irq(irq
);
199 BUG_ON(info
== NULL
);
200 BUG_ON(info
->type
!= IRQT_PIRQ
);
202 return info
->u
.pirq
.gsi
;
205 static unsigned vector_from_irq(unsigned irq
)
207 struct irq_info
*info
= info_for_irq(irq
);
209 BUG_ON(info
== NULL
);
210 BUG_ON(info
->type
!= IRQT_PIRQ
);
212 return info
->u
.pirq
.vector
;
215 static enum xen_irq_type
type_from_irq(unsigned irq
)
217 return info_for_irq(irq
)->type
;
220 static unsigned cpu_from_irq(unsigned irq
)
222 return info_for_irq(irq
)->cpu
;
225 static unsigned int cpu_from_evtchn(unsigned int evtchn
)
227 int irq
= evtchn_to_irq
[evtchn
];
231 ret
= cpu_from_irq(irq
);
236 static bool pirq_needs_eoi(unsigned irq
)
238 struct irq_info
*info
= info_for_irq(irq
);
240 BUG_ON(info
->type
!= IRQT_PIRQ
);
242 return info
->u
.pirq
.flags
& PIRQ_NEEDS_EOI
;
245 static inline unsigned long active_evtchns(unsigned int cpu
,
246 struct shared_info
*sh
,
249 return (sh
->evtchn_pending
[idx
] &
250 cpu_evtchn_mask(cpu
)[idx
] &
251 ~sh
->evtchn_mask
[idx
]);
254 static void bind_evtchn_to_cpu(unsigned int chn
, unsigned int cpu
)
256 int irq
= evtchn_to_irq
[chn
];
260 cpumask_copy(irq_to_desc(irq
)->affinity
, cpumask_of(cpu
));
263 __clear_bit(chn
, cpu_evtchn_mask(cpu_from_irq(irq
)));
264 __set_bit(chn
, cpu_evtchn_mask(cpu
));
266 irq_info
[irq
].cpu
= cpu
;
269 static void init_evtchn_cpu_bindings(void)
272 struct irq_desc
*desc
;
275 /* By default all event channels notify CPU#0. */
276 for_each_irq_desc(i
, desc
) {
277 cpumask_copy(desc
->affinity
, cpumask_of(0));
281 memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0)));
284 static inline void clear_evtchn(int port
)
286 struct shared_info
*s
= HYPERVISOR_shared_info
;
287 sync_clear_bit(port
, &s
->evtchn_pending
[0]);
290 static inline void set_evtchn(int port
)
292 struct shared_info
*s
= HYPERVISOR_shared_info
;
293 sync_set_bit(port
, &s
->evtchn_pending
[0]);
296 static inline int test_evtchn(int port
)
298 struct shared_info
*s
= HYPERVISOR_shared_info
;
299 return sync_test_bit(port
, &s
->evtchn_pending
[0]);
304 * notify_remote_via_irq - send event to remote end of event channel via irq
305 * @irq: irq of event channel to send event to
307 * Unlike notify_remote_via_evtchn(), this is safe to use across
308 * save/restore. Notifications on a broken connection are silently
311 void notify_remote_via_irq(int irq
)
313 int evtchn
= evtchn_from_irq(irq
);
315 if (VALID_EVTCHN(evtchn
))
316 notify_remote_via_evtchn(evtchn
);
318 EXPORT_SYMBOL_GPL(notify_remote_via_irq
);
320 static void mask_evtchn(int port
)
322 struct shared_info
*s
= HYPERVISOR_shared_info
;
323 sync_set_bit(port
, &s
->evtchn_mask
[0]);
326 static void unmask_evtchn(int port
)
328 struct shared_info
*s
= HYPERVISOR_shared_info
;
329 unsigned int cpu
= get_cpu();
331 BUG_ON(!irqs_disabled());
333 /* Slow path (hypercall) if this is a non-local port. */
334 if (unlikely(cpu
!= cpu_from_evtchn(port
))) {
335 struct evtchn_unmask unmask
= { .port
= port
};
336 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask
, &unmask
);
338 struct vcpu_info
*vcpu_info
= __get_cpu_var(xen_vcpu
);
340 sync_clear_bit(port
, &s
->evtchn_mask
[0]);
343 * The following is basically the equivalent of
344 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
345 * the interrupt edge' if the channel is masked.
347 if (sync_test_bit(port
, &s
->evtchn_pending
[0]) &&
348 !sync_test_and_set_bit(port
/ BITS_PER_LONG
,
349 &vcpu_info
->evtchn_pending_sel
))
350 vcpu_info
->evtchn_upcall_pending
= 1;
356 static int get_nr_hw_irqs(void)
360 #ifdef CONFIG_X86_IO_APIC
361 ret
= get_nr_irqs_gsi();
367 static int find_unbound_irq(void)
369 struct irq_data
*data
;
371 int start
= get_nr_hw_irqs();
373 if (start
== nr_irqs
)
376 /* nr_irqs is a magic value. Must not use it.*/
377 for (irq
= nr_irqs
-1; irq
> start
; irq
--) {
378 data
= irq_get_irq_data(irq
);
379 /* only 0->15 have init'd desc; handle irq > 16 */
382 if (data
->chip
== &no_irq_chip
)
384 if (data
->chip
!= &xen_dynamic_chip
)
386 if (irq_info
[irq
].type
== IRQT_UNBOUND
)
393 res
= irq_alloc_desc_at(irq
, 0);
395 if (WARN_ON(res
!= irq
))
401 panic("No available IRQ to bind to: increase nr_irqs!\n");
404 static bool identity_mapped_irq(unsigned irq
)
406 /* identity map all the hardware irqs */
407 return irq
< get_nr_hw_irqs();
410 static void pirq_unmask_notify(int irq
)
412 struct physdev_eoi eoi
= { .irq
= irq
};
414 if (unlikely(pirq_needs_eoi(irq
))) {
415 int rc
= HYPERVISOR_physdev_op(PHYSDEVOP_eoi
, &eoi
);
420 static void pirq_query_unmask(int irq
)
422 struct physdev_irq_status_query irq_status
;
423 struct irq_info
*info
= info_for_irq(irq
);
425 BUG_ON(info
->type
!= IRQT_PIRQ
);
427 irq_status
.irq
= irq
;
428 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query
, &irq_status
))
429 irq_status
.flags
= 0;
431 info
->u
.pirq
.flags
&= ~PIRQ_NEEDS_EOI
;
432 if (irq_status
.flags
& XENIRQSTAT_needs_eoi
)
433 info
->u
.pirq
.flags
|= PIRQ_NEEDS_EOI
;
436 static bool probing_irq(int irq
)
438 struct irq_desc
*desc
= irq_to_desc(irq
);
440 return desc
&& desc
->action
== NULL
;
443 static unsigned int startup_pirq(unsigned int irq
)
445 struct evtchn_bind_pirq bind_pirq
;
446 struct irq_info
*info
= info_for_irq(irq
);
447 int evtchn
= evtchn_from_irq(irq
);
449 BUG_ON(info
->type
!= IRQT_PIRQ
);
451 if (VALID_EVTCHN(evtchn
))
454 bind_pirq
.pirq
= irq
;
455 /* NB. We are happy to share unless we are probing. */
456 bind_pirq
.flags
= probing_irq(irq
) ? 0 : BIND_PIRQ__WILL_SHARE
;
457 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq
, &bind_pirq
) != 0) {
458 if (!probing_irq(irq
))
459 printk(KERN_INFO
"Failed to obtain physical IRQ %d\n",
463 evtchn
= bind_pirq
.port
;
465 pirq_query_unmask(irq
);
467 evtchn_to_irq
[evtchn
] = irq
;
468 bind_evtchn_to_cpu(evtchn
, 0);
469 info
->evtchn
= evtchn
;
472 unmask_evtchn(evtchn
);
473 pirq_unmask_notify(irq
);
478 static void shutdown_pirq(unsigned int irq
)
480 struct evtchn_close close
;
481 struct irq_info
*info
= info_for_irq(irq
);
482 int evtchn
= evtchn_from_irq(irq
);
484 BUG_ON(info
->type
!= IRQT_PIRQ
);
486 if (!VALID_EVTCHN(evtchn
))
492 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
495 bind_evtchn_to_cpu(evtchn
, 0);
496 evtchn_to_irq
[evtchn
] = -1;
500 static void enable_pirq(unsigned int irq
)
505 static void disable_pirq(unsigned int irq
)
509 static void ack_pirq(unsigned int irq
)
511 int evtchn
= evtchn_from_irq(irq
);
513 move_native_irq(irq
);
515 if (VALID_EVTCHN(evtchn
)) {
517 clear_evtchn(evtchn
);
521 static void end_pirq(unsigned int irq
)
523 int evtchn
= evtchn_from_irq(irq
);
524 struct irq_desc
*desc
= irq_to_desc(irq
);
529 if ((desc
->status
& (IRQ_DISABLED
|IRQ_PENDING
)) ==
530 (IRQ_DISABLED
|IRQ_PENDING
)) {
532 } else if (VALID_EVTCHN(evtchn
)) {
533 unmask_evtchn(evtchn
);
534 pirq_unmask_notify(irq
);
538 static int find_irq_by_gsi(unsigned gsi
)
542 for (irq
= 0; irq
< nr_irqs
; irq
++) {
543 struct irq_info
*info
= info_for_irq(irq
);
545 if (info
== NULL
|| info
->type
!= IRQT_PIRQ
)
548 if (gsi_from_irq(irq
) == gsi
)
555 /* xen_allocate_irq might allocate irqs from the top down, as a
556 * consequence don't assume that the irq number returned has a low value
557 * or can be used as a pirq number unless you know otherwise.
559 * One notable exception is when xen_allocate_irq is called passing an
560 * hardware gsi as argument, in that case the irq number returned
561 * matches the gsi number passed as first argument.
563 * Note: We don't assign an
564 * event channel until the irq actually started up. Return an
565 * existing irq if we've already got one for the gsi.
567 int xen_allocate_pirq(unsigned gsi
, char *name
)
570 struct physdev_irq irq_op
;
572 spin_lock(&irq_mapping_update_lock
);
574 irq
= find_irq_by_gsi(gsi
);
576 printk(KERN_INFO
"xen_allocate_pirq: returning irq %d for gsi %u\n",
578 goto out
; /* XXX need refcount? */
581 if (identity_mapped_irq(gsi
)) {
583 irq_to_desc_alloc_node(irq
, 0);
584 dynamic_irq_init(irq
);
586 irq
= find_unbound_irq();
588 set_irq_chip_and_handler_name(irq
, &xen_pirq_chip
,
589 handle_level_irq
, name
);
592 if (HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector
, &irq_op
)) {
593 dynamic_irq_cleanup(irq
);
598 irq_info
[irq
] = mk_pirq_info(0, gsi
, irq_op
.vector
);
601 spin_unlock(&irq_mapping_update_lock
);
606 int xen_vector_from_irq(unsigned irq
)
608 return vector_from_irq(irq
);
611 int xen_gsi_from_irq(unsigned irq
)
613 return gsi_from_irq(irq
);
616 int bind_evtchn_to_irq(unsigned int evtchn
)
620 spin_lock(&irq_mapping_update_lock
);
622 irq
= evtchn_to_irq
[evtchn
];
625 irq
= find_unbound_irq();
627 set_irq_chip_and_handler_name(irq
, &xen_dynamic_chip
,
628 handle_edge_irq
, "event");
630 evtchn_to_irq
[evtchn
] = irq
;
631 irq_info
[irq
] = mk_evtchn_info(evtchn
);
634 spin_unlock(&irq_mapping_update_lock
);
638 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq
);
640 static int bind_ipi_to_irq(unsigned int ipi
, unsigned int cpu
)
642 struct evtchn_bind_ipi bind_ipi
;
645 spin_lock(&irq_mapping_update_lock
);
647 irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
];
650 irq
= find_unbound_irq();
654 set_irq_chip_and_handler_name(irq
, &xen_percpu_chip
,
655 handle_percpu_irq
, "ipi");
658 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
661 evtchn
= bind_ipi
.port
;
663 evtchn_to_irq
[evtchn
] = irq
;
664 irq_info
[irq
] = mk_ipi_info(evtchn
, ipi
);
665 per_cpu(ipi_to_irq
, cpu
)[ipi
] = irq
;
667 bind_evtchn_to_cpu(evtchn
, cpu
);
671 spin_unlock(&irq_mapping_update_lock
);
676 static int bind_virq_to_irq(unsigned int virq
, unsigned int cpu
)
678 struct evtchn_bind_virq bind_virq
;
681 spin_lock(&irq_mapping_update_lock
);
683 irq
= per_cpu(virq_to_irq
, cpu
)[virq
];
686 bind_virq
.virq
= virq
;
687 bind_virq
.vcpu
= cpu
;
688 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
691 evtchn
= bind_virq
.port
;
693 irq
= find_unbound_irq();
695 set_irq_chip_and_handler_name(irq
, &xen_percpu_chip
,
696 handle_percpu_irq
, "virq");
698 evtchn_to_irq
[evtchn
] = irq
;
699 irq_info
[irq
] = mk_virq_info(evtchn
, virq
);
701 per_cpu(virq_to_irq
, cpu
)[virq
] = irq
;
703 bind_evtchn_to_cpu(evtchn
, cpu
);
706 spin_unlock(&irq_mapping_update_lock
);
711 static void unbind_from_irq(unsigned int irq
)
713 struct evtchn_close close
;
714 int evtchn
= evtchn_from_irq(irq
);
716 spin_lock(&irq_mapping_update_lock
);
718 if (VALID_EVTCHN(evtchn
)) {
720 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
723 switch (type_from_irq(irq
)) {
725 per_cpu(virq_to_irq
, cpu_from_evtchn(evtchn
))
726 [virq_from_irq(irq
)] = -1;
729 per_cpu(ipi_to_irq
, cpu_from_evtchn(evtchn
))
730 [ipi_from_irq(irq
)] = -1;
736 /* Closed ports are implicitly re-bound to VCPU0. */
737 bind_evtchn_to_cpu(evtchn
, 0);
739 evtchn_to_irq
[evtchn
] = -1;
742 if (irq_info
[irq
].type
!= IRQT_UNBOUND
) {
743 irq_info
[irq
] = mk_unbound_info();
748 spin_unlock(&irq_mapping_update_lock
);
751 int bind_evtchn_to_irqhandler(unsigned int evtchn
,
752 irq_handler_t handler
,
753 unsigned long irqflags
,
754 const char *devname
, void *dev_id
)
759 irq
= bind_evtchn_to_irq(evtchn
);
760 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
762 unbind_from_irq(irq
);
768 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler
);
770 int bind_virq_to_irqhandler(unsigned int virq
, unsigned int cpu
,
771 irq_handler_t handler
,
772 unsigned long irqflags
, const char *devname
, void *dev_id
)
777 irq
= bind_virq_to_irq(virq
, cpu
);
778 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
780 unbind_from_irq(irq
);
786 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler
);
788 int bind_ipi_to_irqhandler(enum ipi_vector ipi
,
790 irq_handler_t handler
,
791 unsigned long irqflags
,
797 irq
= bind_ipi_to_irq(ipi
, cpu
);
801 irqflags
|= IRQF_NO_SUSPEND
;
802 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
804 unbind_from_irq(irq
);
811 void unbind_from_irqhandler(unsigned int irq
, void *dev_id
)
813 free_irq(irq
, dev_id
);
814 unbind_from_irq(irq
);
816 EXPORT_SYMBOL_GPL(unbind_from_irqhandler
);
818 void xen_send_IPI_one(unsigned int cpu
, enum ipi_vector vector
)
820 int irq
= per_cpu(ipi_to_irq
, cpu
)[vector
];
822 notify_remote_via_irq(irq
);
825 irqreturn_t
xen_debug_interrupt(int irq
, void *dev_id
)
827 struct shared_info
*sh
= HYPERVISOR_shared_info
;
828 int cpu
= smp_processor_id();
831 static DEFINE_SPINLOCK(debug_lock
);
833 spin_lock_irqsave(&debug_lock
, flags
);
835 printk("vcpu %d\n ", cpu
);
837 for_each_online_cpu(i
) {
838 struct vcpu_info
*v
= per_cpu(xen_vcpu
, i
);
839 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i
,
840 (get_irq_regs() && i
== cpu
) ? xen_irqs_disabled(get_irq_regs()) : v
->evtchn_upcall_mask
,
841 v
->evtchn_upcall_pending
,
842 v
->evtchn_pending_sel
);
844 printk("pending:\n ");
845 for(i
= ARRAY_SIZE(sh
->evtchn_pending
)-1; i
>= 0; i
--)
846 printk("%08lx%s", sh
->evtchn_pending
[i
],
847 i
% 8 == 0 ? "\n " : " ");
848 printk("\nmasks:\n ");
849 for(i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
850 printk("%08lx%s", sh
->evtchn_mask
[i
],
851 i
% 8 == 0 ? "\n " : " ");
853 printk("\nunmasked:\n ");
854 for(i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
855 printk("%08lx%s", sh
->evtchn_pending
[i
] & ~sh
->evtchn_mask
[i
],
856 i
% 8 == 0 ? "\n " : " ");
858 printk("\npending list:\n");
859 for(i
= 0; i
< NR_EVENT_CHANNELS
; i
++) {
860 if (sync_test_bit(i
, sh
->evtchn_pending
)) {
861 printk(" %d: event %d -> irq %d\n",
862 cpu_from_evtchn(i
), i
,
867 spin_unlock_irqrestore(&debug_lock
, flags
);
872 static DEFINE_PER_CPU(unsigned, xed_nesting_count
);
875 * Search the CPUs pending events bitmasks. For each one found, map
876 * the event number to an irq, and feed it into do_IRQ() for
879 * Xen uses a two-level bitmap to speed searching. The first level is
880 * a bitset of words which contain pending event bits. The second
881 * level is a bitset of pending events themselves.
883 static void __xen_evtchn_do_upcall(void)
886 struct shared_info
*s
= HYPERVISOR_shared_info
;
887 struct vcpu_info
*vcpu_info
= __get_cpu_var(xen_vcpu
);
891 unsigned long pending_words
;
893 vcpu_info
->evtchn_upcall_pending
= 0;
895 if (__get_cpu_var(xed_nesting_count
)++)
898 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
899 /* Clear master flag /before/ clearing selector flag. */
902 pending_words
= xchg(&vcpu_info
->evtchn_pending_sel
, 0);
903 while (pending_words
!= 0) {
904 unsigned long pending_bits
;
905 int word_idx
= __ffs(pending_words
);
906 pending_words
&= ~(1UL << word_idx
);
908 while ((pending_bits
= active_evtchns(cpu
, s
, word_idx
)) != 0) {
909 int bit_idx
= __ffs(pending_bits
);
910 int port
= (word_idx
* BITS_PER_LONG
) + bit_idx
;
911 int irq
= evtchn_to_irq
[port
];
912 struct irq_desc
*desc
;
915 desc
= irq_to_desc(irq
);
917 generic_handle_irq_desc(irq
, desc
);
922 BUG_ON(!irqs_disabled());
924 count
= __get_cpu_var(xed_nesting_count
);
925 __get_cpu_var(xed_nesting_count
) = 0;
926 } while (count
!= 1 || vcpu_info
->evtchn_upcall_pending
);
933 void xen_evtchn_do_upcall(struct pt_regs
*regs
)
935 struct pt_regs
*old_regs
= set_irq_regs(regs
);
940 __xen_evtchn_do_upcall();
943 set_irq_regs(old_regs
);
946 void xen_hvm_evtchn_do_upcall(void)
948 __xen_evtchn_do_upcall();
950 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall
);
952 /* Rebind a new event channel to an existing irq. */
953 void rebind_evtchn_irq(int evtchn
, int irq
)
955 struct irq_info
*info
= info_for_irq(irq
);
957 /* Make sure the irq is masked, since the new event channel
958 will also be masked. */
961 spin_lock(&irq_mapping_update_lock
);
963 /* After resume the irq<->evtchn mappings are all cleared out */
964 BUG_ON(evtchn_to_irq
[evtchn
] != -1);
965 /* Expect irq to have been bound before,
966 so there should be a proper type */
967 BUG_ON(info
->type
== IRQT_UNBOUND
);
969 evtchn_to_irq
[evtchn
] = irq
;
970 irq_info
[irq
] = mk_evtchn_info(evtchn
);
972 spin_unlock(&irq_mapping_update_lock
);
974 /* new event channels are always bound to cpu 0 */
975 irq_set_affinity(irq
, cpumask_of(0));
977 /* Unmask the event channel. */
981 /* Rebind an evtchn so that it gets delivered to a specific cpu */
982 static int rebind_irq_to_cpu(unsigned irq
, unsigned tcpu
)
984 struct evtchn_bind_vcpu bind_vcpu
;
985 int evtchn
= evtchn_from_irq(irq
);
987 /* events delivered via platform PCI interrupts are always
988 * routed to vcpu 0 */
989 if (!VALID_EVTCHN(evtchn
) ||
990 (xen_hvm_domain() && !xen_have_vector_callback
))
993 /* Send future instances of this interrupt to other vcpu. */
994 bind_vcpu
.port
= evtchn
;
995 bind_vcpu
.vcpu
= tcpu
;
998 * If this fails, it usually just indicates that we're dealing with a
999 * virq or IPI channel, which don't actually need to be rebound. Ignore
1000 * it, but don't do the xenlinux-level rebind in that case.
1002 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu
, &bind_vcpu
) >= 0)
1003 bind_evtchn_to_cpu(evtchn
, tcpu
);
1008 static int set_affinity_irq(unsigned irq
, const struct cpumask
*dest
)
1010 unsigned tcpu
= cpumask_first(dest
);
1012 return rebind_irq_to_cpu(irq
, tcpu
);
1015 int resend_irq_on_evtchn(unsigned int irq
)
1017 int masked
, evtchn
= evtchn_from_irq(irq
);
1018 struct shared_info
*s
= HYPERVISOR_shared_info
;
1020 if (!VALID_EVTCHN(evtchn
))
1023 masked
= sync_test_and_set_bit(evtchn
, s
->evtchn_mask
);
1024 sync_set_bit(evtchn
, s
->evtchn_pending
);
1026 unmask_evtchn(evtchn
);
1031 static void enable_dynirq(unsigned int irq
)
1033 int evtchn
= evtchn_from_irq(irq
);
1035 if (VALID_EVTCHN(evtchn
))
1036 unmask_evtchn(evtchn
);
1039 static void disable_dynirq(unsigned int irq
)
1041 int evtchn
= evtchn_from_irq(irq
);
1043 if (VALID_EVTCHN(evtchn
))
1044 mask_evtchn(evtchn
);
1047 static void ack_dynirq(unsigned int irq
)
1049 int evtchn
= evtchn_from_irq(irq
);
1051 move_native_irq(irq
);
1053 if (VALID_EVTCHN(evtchn
))
1054 clear_evtchn(evtchn
);
1057 static int retrigger_dynirq(unsigned int irq
)
1059 int evtchn
= evtchn_from_irq(irq
);
1060 struct shared_info
*sh
= HYPERVISOR_shared_info
;
1063 if (VALID_EVTCHN(evtchn
)) {
1066 masked
= sync_test_and_set_bit(evtchn
, sh
->evtchn_mask
);
1067 sync_set_bit(evtchn
, sh
->evtchn_pending
);
1069 unmask_evtchn(evtchn
);
1076 static void restore_cpu_virqs(unsigned int cpu
)
1078 struct evtchn_bind_virq bind_virq
;
1079 int virq
, irq
, evtchn
;
1081 for (virq
= 0; virq
< NR_VIRQS
; virq
++) {
1082 if ((irq
= per_cpu(virq_to_irq
, cpu
)[virq
]) == -1)
1085 BUG_ON(virq_from_irq(irq
) != virq
);
1087 /* Get a new binding from Xen. */
1088 bind_virq
.virq
= virq
;
1089 bind_virq
.vcpu
= cpu
;
1090 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
1093 evtchn
= bind_virq
.port
;
1095 /* Record the new mapping. */
1096 evtchn_to_irq
[evtchn
] = irq
;
1097 irq_info
[irq
] = mk_virq_info(evtchn
, virq
);
1098 bind_evtchn_to_cpu(evtchn
, cpu
);
1100 /* Ready for use. */
1101 unmask_evtchn(evtchn
);
1105 static void restore_cpu_ipis(unsigned int cpu
)
1107 struct evtchn_bind_ipi bind_ipi
;
1108 int ipi
, irq
, evtchn
;
1110 for (ipi
= 0; ipi
< XEN_NR_IPIS
; ipi
++) {
1111 if ((irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
]) == -1)
1114 BUG_ON(ipi_from_irq(irq
) != ipi
);
1116 /* Get a new binding from Xen. */
1117 bind_ipi
.vcpu
= cpu
;
1118 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
1121 evtchn
= bind_ipi
.port
;
1123 /* Record the new mapping. */
1124 evtchn_to_irq
[evtchn
] = irq
;
1125 irq_info
[irq
] = mk_ipi_info(evtchn
, ipi
);
1126 bind_evtchn_to_cpu(evtchn
, cpu
);
1128 /* Ready for use. */
1129 unmask_evtchn(evtchn
);
1134 /* Clear an irq's pending state, in preparation for polling on it */
1135 void xen_clear_irq_pending(int irq
)
1137 int evtchn
= evtchn_from_irq(irq
);
1139 if (VALID_EVTCHN(evtchn
))
1140 clear_evtchn(evtchn
);
1142 EXPORT_SYMBOL(xen_clear_irq_pending
);
1143 void xen_set_irq_pending(int irq
)
1145 int evtchn
= evtchn_from_irq(irq
);
1147 if (VALID_EVTCHN(evtchn
))
1151 bool xen_test_irq_pending(int irq
)
1153 int evtchn
= evtchn_from_irq(irq
);
1156 if (VALID_EVTCHN(evtchn
))
1157 ret
= test_evtchn(evtchn
);
1162 /* Poll waiting for an irq to become pending with timeout. In the usual case,
1163 * the irq will be disabled so it won't deliver an interrupt. */
1164 void xen_poll_irq_timeout(int irq
, u64 timeout
)
1166 evtchn_port_t evtchn
= evtchn_from_irq(irq
);
1168 if (VALID_EVTCHN(evtchn
)) {
1169 struct sched_poll poll
;
1172 poll
.timeout
= timeout
;
1173 set_xen_guest_handle(poll
.ports
, &evtchn
);
1175 if (HYPERVISOR_sched_op(SCHEDOP_poll
, &poll
) != 0)
1179 EXPORT_SYMBOL(xen_poll_irq_timeout
);
1180 /* Poll waiting for an irq to become pending. In the usual case, the
1181 * irq will be disabled so it won't deliver an interrupt. */
1182 void xen_poll_irq(int irq
)
1184 xen_poll_irq_timeout(irq
, 0 /* no timeout */);
1187 void xen_irq_resume(void)
1189 unsigned int cpu
, irq
, evtchn
;
1191 init_evtchn_cpu_bindings();
1193 /* New event-channel space is not 'live' yet. */
1194 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1195 mask_evtchn(evtchn
);
1197 /* No IRQ <-> event-channel mappings. */
1198 for (irq
= 0; irq
< nr_irqs
; irq
++)
1199 irq_info
[irq
].evtchn
= 0; /* zap event-channel binding */
1201 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1202 evtchn_to_irq
[evtchn
] = -1;
1204 for_each_possible_cpu(cpu
) {
1205 restore_cpu_virqs(cpu
);
1206 restore_cpu_ipis(cpu
);
1210 static struct irq_chip xen_dynamic_chip __read_mostly
= {
1213 .disable
= disable_dynirq
,
1214 .mask
= disable_dynirq
,
1215 .unmask
= enable_dynirq
,
1218 .set_affinity
= set_affinity_irq
,
1219 .retrigger
= retrigger_dynirq
,
1222 static struct irq_chip xen_pirq_chip __read_mostly
= {
1225 .startup
= startup_pirq
,
1226 .shutdown
= shutdown_pirq
,
1228 .enable
= enable_pirq
,
1229 .unmask
= enable_pirq
,
1231 .disable
= disable_pirq
,
1232 .mask
= disable_pirq
,
1237 .set_affinity
= set_affinity_irq
,
1239 .retrigger
= retrigger_dynirq
,
1242 static struct irq_chip xen_percpu_chip __read_mostly
= {
1243 .name
= "xen-percpu",
1245 .disable
= disable_dynirq
,
1246 .mask
= disable_dynirq
,
1247 .unmask
= enable_dynirq
,
1252 int xen_set_callback_via(uint64_t via
)
1254 struct xen_hvm_param a
;
1255 a
.domid
= DOMID_SELF
;
1256 a
.index
= HVM_PARAM_CALLBACK_IRQ
;
1258 return HYPERVISOR_hvm_op(HVMOP_set_param
, &a
);
1260 EXPORT_SYMBOL_GPL(xen_set_callback_via
);
1262 #ifdef CONFIG_XEN_PVHVM
1263 /* Vector callbacks are better than PCI interrupts to receive event
1264 * channel notifications because we can receive vector callbacks on any
1265 * vcpu and we don't need PCI support or APIC interactions. */
1266 void xen_callback_vector(void)
1269 uint64_t callback_via
;
1270 if (xen_have_vector_callback
) {
1271 callback_via
= HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK
);
1272 rc
= xen_set_callback_via(callback_via
);
1274 printk(KERN_ERR
"Request for Xen HVM callback vector"
1276 xen_have_vector_callback
= 0;
1279 printk(KERN_INFO
"Xen HVM callback vector for event delivery is "
1281 /* in the restore case the vector has already been allocated */
1282 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK
, used_vectors
))
1283 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK
, xen_hvm_callback_vector
);
1287 void xen_callback_vector(void) {}
1290 void __init
xen_init_IRQ(void)
1294 cpu_evtchn_mask_p
= kcalloc(nr_cpu_ids
, sizeof(struct cpu_evtchn_s
),
1296 irq_info
= kcalloc(nr_irqs
, sizeof(*irq_info
), GFP_KERNEL
);
1298 evtchn_to_irq
= kcalloc(NR_EVENT_CHANNELS
, sizeof(*evtchn_to_irq
),
1300 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1301 evtchn_to_irq
[i
] = -1;
1303 init_evtchn_cpu_bindings();
1305 /* No event channels are 'live' right now. */
1306 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1309 if (xen_hvm_domain()) {
1310 xen_callback_vector();
1313 irq_ctx_init(smp_processor_id());