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>
33 #include <asm/ptrace.h>
36 #include <asm/sync_bitops.h>
37 #include <asm/xen/hypercall.h>
38 #include <asm/xen/hypervisor.h>
42 #include <xen/xen-ops.h>
43 #include <xen/events.h>
44 #include <xen/interface/xen.h>
45 #include <xen/interface/event_channel.h>
46 #include <xen/interface/hvm/hvm_op.h>
47 #include <xen/interface/hvm/params.h>
49 /* Leave low irqs free for identity mapping */
50 #define LEGACY_IRQS 16
53 * This lock protects updates to the following mapping and reference-count
54 * arrays. The lock does not need to be acquired to read the mapping tables.
56 static DEFINE_SPINLOCK(irq_mapping_update_lock
);
58 /* IRQ <-> VIRQ mapping. */
59 static DEFINE_PER_CPU(int [NR_VIRQS
], virq_to_irq
) = {[0 ... NR_VIRQS
-1] = -1};
61 /* IRQ <-> IPI mapping */
62 static DEFINE_PER_CPU(int [XEN_NR_IPIS
], ipi_to_irq
) = {[0 ... XEN_NR_IPIS
-1] = -1};
64 /* Interrupt types. */
74 * Packed IRQ information:
75 * type - enum xen_irq_type
76 * event channel - irq->event channel mapping
77 * cpu - cpu this event channel is bound to
78 * index - type-specific information:
79 * PIRQ - vector, with MSB being "needs EIO"
86 enum xen_irq_type type
; /* type */
87 unsigned short evtchn
; /* event channel */
88 unsigned short cpu
; /* cpu bound */
100 #define PIRQ_NEEDS_EOI (1 << 0)
102 static struct irq_info irq_info
[NR_IRQS
];
104 static int evtchn_to_irq
[NR_EVENT_CHANNELS
] = {
105 [0 ... NR_EVENT_CHANNELS
-1] = -1
107 struct cpu_evtchn_s
{
108 unsigned long bits
[NR_EVENT_CHANNELS
/BITS_PER_LONG
];
110 static struct cpu_evtchn_s
*cpu_evtchn_mask_p
;
111 static inline unsigned long *cpu_evtchn_mask(int cpu
)
113 return cpu_evtchn_mask_p
[cpu
].bits
;
116 /* Xen will never allocate port zero for any purpose. */
117 #define VALID_EVTCHN(chn) ((chn) != 0)
119 static struct irq_chip xen_dynamic_chip
;
120 static struct irq_chip xen_percpu_chip
;
121 static struct irq_chip xen_pirq_chip
;
123 /* Constructor for packed IRQ information. */
124 static struct irq_info
mk_unbound_info(void)
126 return (struct irq_info
) { .type
= IRQT_UNBOUND
};
129 static struct irq_info
mk_evtchn_info(unsigned short evtchn
)
131 return (struct irq_info
) { .type
= IRQT_EVTCHN
, .evtchn
= evtchn
,
135 static struct irq_info
mk_ipi_info(unsigned short evtchn
, enum ipi_vector ipi
)
137 return (struct irq_info
) { .type
= IRQT_IPI
, .evtchn
= evtchn
,
138 .cpu
= 0, .u
.ipi
= ipi
};
141 static struct irq_info
mk_virq_info(unsigned short evtchn
, unsigned short virq
)
143 return (struct irq_info
) { .type
= IRQT_VIRQ
, .evtchn
= evtchn
,
144 .cpu
= 0, .u
.virq
= virq
};
147 static struct irq_info
mk_pirq_info(unsigned short evtchn
,
148 unsigned short gsi
, unsigned short vector
)
150 return (struct irq_info
) { .type
= IRQT_PIRQ
, .evtchn
= evtchn
,
151 .cpu
= 0, .u
.pirq
= { .gsi
= gsi
, .vector
= vector
} };
155 * Accessors for packed IRQ information.
157 static struct irq_info
*info_for_irq(unsigned irq
)
159 return &irq_info
[irq
];
162 static unsigned int evtchn_from_irq(unsigned irq
)
164 return info_for_irq(irq
)->evtchn
;
167 unsigned irq_from_evtchn(unsigned int evtchn
)
169 return evtchn_to_irq
[evtchn
];
171 EXPORT_SYMBOL_GPL(irq_from_evtchn
);
173 static enum ipi_vector
ipi_from_irq(unsigned irq
)
175 struct irq_info
*info
= info_for_irq(irq
);
177 BUG_ON(info
== NULL
);
178 BUG_ON(info
->type
!= IRQT_IPI
);
183 static unsigned virq_from_irq(unsigned irq
)
185 struct irq_info
*info
= info_for_irq(irq
);
187 BUG_ON(info
== NULL
);
188 BUG_ON(info
->type
!= IRQT_VIRQ
);
193 static unsigned gsi_from_irq(unsigned irq
)
195 struct irq_info
*info
= info_for_irq(irq
);
197 BUG_ON(info
== NULL
);
198 BUG_ON(info
->type
!= IRQT_PIRQ
);
200 return info
->u
.pirq
.gsi
;
203 static unsigned vector_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
.vector
;
213 static enum xen_irq_type
type_from_irq(unsigned irq
)
215 return info_for_irq(irq
)->type
;
218 static unsigned cpu_from_irq(unsigned irq
)
220 return info_for_irq(irq
)->cpu
;
223 static unsigned int cpu_from_evtchn(unsigned int evtchn
)
225 int irq
= evtchn_to_irq
[evtchn
];
229 ret
= cpu_from_irq(irq
);
234 static bool pirq_needs_eoi(unsigned irq
)
236 struct irq_info
*info
= info_for_irq(irq
);
238 BUG_ON(info
->type
!= IRQT_PIRQ
);
240 return info
->u
.pirq
.flags
& PIRQ_NEEDS_EOI
;
243 static inline unsigned long active_evtchns(unsigned int cpu
,
244 struct shared_info
*sh
,
247 return (sh
->evtchn_pending
[idx
] &
248 cpu_evtchn_mask(cpu
)[idx
] &
249 ~sh
->evtchn_mask
[idx
]);
252 static void bind_evtchn_to_cpu(unsigned int chn
, unsigned int cpu
)
254 int irq
= evtchn_to_irq
[chn
];
258 cpumask_copy(irq_to_desc(irq
)->affinity
, cpumask_of(cpu
));
261 __clear_bit(chn
, cpu_evtchn_mask(cpu_from_irq(irq
)));
262 __set_bit(chn
, cpu_evtchn_mask(cpu
));
264 irq_info
[irq
].cpu
= cpu
;
267 static void init_evtchn_cpu_bindings(void)
270 struct irq_desc
*desc
;
273 /* By default all event channels notify CPU#0. */
274 for_each_irq_desc(i
, desc
) {
275 cpumask_copy(desc
->affinity
, cpumask_of(0));
279 memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0)));
282 static inline void clear_evtchn(int port
)
284 struct shared_info
*s
= HYPERVISOR_shared_info
;
285 sync_clear_bit(port
, &s
->evtchn_pending
[0]);
288 static inline void set_evtchn(int port
)
290 struct shared_info
*s
= HYPERVISOR_shared_info
;
291 sync_set_bit(port
, &s
->evtchn_pending
[0]);
294 static inline int test_evtchn(int port
)
296 struct shared_info
*s
= HYPERVISOR_shared_info
;
297 return sync_test_bit(port
, &s
->evtchn_pending
[0]);
302 * notify_remote_via_irq - send event to remote end of event channel via irq
303 * @irq: irq of event channel to send event to
305 * Unlike notify_remote_via_evtchn(), this is safe to use across
306 * save/restore. Notifications on a broken connection are silently
309 void notify_remote_via_irq(int irq
)
311 int evtchn
= evtchn_from_irq(irq
);
313 if (VALID_EVTCHN(evtchn
))
314 notify_remote_via_evtchn(evtchn
);
316 EXPORT_SYMBOL_GPL(notify_remote_via_irq
);
318 static void mask_evtchn(int port
)
320 struct shared_info
*s
= HYPERVISOR_shared_info
;
321 sync_set_bit(port
, &s
->evtchn_mask
[0]);
324 static void unmask_evtchn(int port
)
326 struct shared_info
*s
= HYPERVISOR_shared_info
;
327 unsigned int cpu
= get_cpu();
329 BUG_ON(!irqs_disabled());
331 /* Slow path (hypercall) if this is a non-local port. */
332 if (unlikely(cpu
!= cpu_from_evtchn(port
))) {
333 struct evtchn_unmask unmask
= { .port
= port
};
334 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask
, &unmask
);
336 struct vcpu_info
*vcpu_info
= __get_cpu_var(xen_vcpu
);
338 sync_clear_bit(port
, &s
->evtchn_mask
[0]);
341 * The following is basically the equivalent of
342 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
343 * the interrupt edge' if the channel is masked.
345 if (sync_test_bit(port
, &s
->evtchn_pending
[0]) &&
346 !sync_test_and_set_bit(port
/ BITS_PER_LONG
,
347 &vcpu_info
->evtchn_pending_sel
))
348 vcpu_info
->evtchn_upcall_pending
= 1;
354 static int find_unbound_irq(void)
356 struct irq_data
*data
;
359 for (irq
= 0; irq
< nr_irqs
; irq
++) {
360 data
= irq_get_irq_data(irq
);
361 /* only 0->15 have init'd desc; handle irq > 16 */
364 if (data
->chip
== &no_irq_chip
)
366 if (data
->chip
!= &xen_dynamic_chip
)
368 if (irq_info
[irq
].type
== IRQT_UNBOUND
)
373 panic("No available IRQ to bind to: increase nr_irqs!\n");
375 res
= irq_alloc_desc_at(irq
, 0);
377 if (WARN_ON(res
!= irq
))
383 static bool identity_mapped_irq(unsigned irq
)
385 /* only identity map legacy irqs */
386 return irq
< LEGACY_IRQS
;
389 static void pirq_unmask_notify(int irq
)
391 struct physdev_eoi eoi
= { .irq
= irq
};
393 if (unlikely(pirq_needs_eoi(irq
))) {
394 int rc
= HYPERVISOR_physdev_op(PHYSDEVOP_eoi
, &eoi
);
399 static void pirq_query_unmask(int irq
)
401 struct physdev_irq_status_query irq_status
;
402 struct irq_info
*info
= info_for_irq(irq
);
404 BUG_ON(info
->type
!= IRQT_PIRQ
);
406 irq_status
.irq
= irq
;
407 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query
, &irq_status
))
408 irq_status
.flags
= 0;
410 info
->u
.pirq
.flags
&= ~PIRQ_NEEDS_EOI
;
411 if (irq_status
.flags
& XENIRQSTAT_needs_eoi
)
412 info
->u
.pirq
.flags
|= PIRQ_NEEDS_EOI
;
415 static bool probing_irq(int irq
)
417 struct irq_desc
*desc
= irq_to_desc(irq
);
419 return desc
&& desc
->action
== NULL
;
422 static unsigned int startup_pirq(unsigned int irq
)
424 struct evtchn_bind_pirq bind_pirq
;
425 struct irq_info
*info
= info_for_irq(irq
);
426 int evtchn
= evtchn_from_irq(irq
);
428 BUG_ON(info
->type
!= IRQT_PIRQ
);
430 if (VALID_EVTCHN(evtchn
))
433 bind_pirq
.pirq
= irq
;
434 /* NB. We are happy to share unless we are probing. */
435 bind_pirq
.flags
= probing_irq(irq
) ? 0 : BIND_PIRQ__WILL_SHARE
;
436 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq
, &bind_pirq
) != 0) {
437 if (!probing_irq(irq
))
438 printk(KERN_INFO
"Failed to obtain physical IRQ %d\n",
442 evtchn
= bind_pirq
.port
;
444 pirq_query_unmask(irq
);
446 evtchn_to_irq
[evtchn
] = irq
;
447 bind_evtchn_to_cpu(evtchn
, 0);
448 info
->evtchn
= evtchn
;
451 unmask_evtchn(evtchn
);
452 pirq_unmask_notify(irq
);
457 static void shutdown_pirq(unsigned int irq
)
459 struct evtchn_close close
;
460 struct irq_info
*info
= info_for_irq(irq
);
461 int evtchn
= evtchn_from_irq(irq
);
463 BUG_ON(info
->type
!= IRQT_PIRQ
);
465 if (!VALID_EVTCHN(evtchn
))
471 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
474 bind_evtchn_to_cpu(evtchn
, 0);
475 evtchn_to_irq
[evtchn
] = -1;
479 static void enable_pirq(unsigned int irq
)
484 static void disable_pirq(unsigned int irq
)
488 static void ack_pirq(unsigned int irq
)
490 int evtchn
= evtchn_from_irq(irq
);
492 move_native_irq(irq
);
494 if (VALID_EVTCHN(evtchn
)) {
496 clear_evtchn(evtchn
);
500 static void end_pirq(unsigned int irq
)
502 int evtchn
= evtchn_from_irq(irq
);
503 struct irq_desc
*desc
= irq_to_desc(irq
);
508 if ((desc
->status
& (IRQ_DISABLED
|IRQ_PENDING
)) ==
509 (IRQ_DISABLED
|IRQ_PENDING
)) {
511 } else if (VALID_EVTCHN(evtchn
)) {
512 unmask_evtchn(evtchn
);
513 pirq_unmask_notify(irq
);
517 static int find_irq_by_gsi(unsigned gsi
)
521 for (irq
= 0; irq
< NR_IRQS
; irq
++) {
522 struct irq_info
*info
= info_for_irq(irq
);
524 if (info
== NULL
|| info
->type
!= IRQT_PIRQ
)
527 if (gsi_from_irq(irq
) == gsi
)
535 * Allocate a physical irq, along with a vector. We don't assign an
536 * event channel until the irq actually started up. Return an
537 * existing irq if we've already got one for the gsi.
539 int xen_allocate_pirq(unsigned gsi
)
542 struct physdev_irq irq_op
;
544 spin_lock(&irq_mapping_update_lock
);
546 irq
= find_irq_by_gsi(gsi
);
548 printk(KERN_INFO
"xen_allocate_pirq: returning irq %d for gsi %u\n",
550 goto out
; /* XXX need refcount? */
553 if (identity_mapped_irq(gsi
)) {
555 dynamic_irq_init(irq
);
557 irq
= find_unbound_irq();
559 set_irq_chip_and_handler_name(irq
, &xen_pirq_chip
,
560 handle_level_irq
, "pirq");
563 if (HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector
, &irq_op
)) {
564 dynamic_irq_cleanup(irq
);
569 irq_info
[irq
] = mk_pirq_info(0, gsi
, irq_op
.vector
);
572 spin_unlock(&irq_mapping_update_lock
);
577 int xen_vector_from_irq(unsigned irq
)
579 return vector_from_irq(irq
);
582 int xen_gsi_from_irq(unsigned irq
)
584 return gsi_from_irq(irq
);
587 int bind_evtchn_to_irq(unsigned int evtchn
)
591 spin_lock(&irq_mapping_update_lock
);
593 irq
= evtchn_to_irq
[evtchn
];
596 irq
= find_unbound_irq();
598 set_irq_chip_and_handler_name(irq
, &xen_dynamic_chip
,
599 handle_edge_irq
, "event");
601 evtchn_to_irq
[evtchn
] = irq
;
602 irq_info
[irq
] = mk_evtchn_info(evtchn
);
605 spin_unlock(&irq_mapping_update_lock
);
609 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq
);
611 static int bind_ipi_to_irq(unsigned int ipi
, unsigned int cpu
)
613 struct evtchn_bind_ipi bind_ipi
;
616 spin_lock(&irq_mapping_update_lock
);
618 irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
];
621 irq
= find_unbound_irq();
625 set_irq_chip_and_handler_name(irq
, &xen_percpu_chip
,
626 handle_percpu_irq
, "ipi");
629 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
632 evtchn
= bind_ipi
.port
;
634 evtchn_to_irq
[evtchn
] = irq
;
635 irq_info
[irq
] = mk_ipi_info(evtchn
, ipi
);
636 per_cpu(ipi_to_irq
, cpu
)[ipi
] = irq
;
638 bind_evtchn_to_cpu(evtchn
, cpu
);
642 spin_unlock(&irq_mapping_update_lock
);
647 static int bind_virq_to_irq(unsigned int virq
, unsigned int cpu
)
649 struct evtchn_bind_virq bind_virq
;
652 spin_lock(&irq_mapping_update_lock
);
654 irq
= per_cpu(virq_to_irq
, cpu
)[virq
];
657 bind_virq
.virq
= virq
;
658 bind_virq
.vcpu
= cpu
;
659 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
662 evtchn
= bind_virq
.port
;
664 irq
= find_unbound_irq();
666 set_irq_chip_and_handler_name(irq
, &xen_percpu_chip
,
667 handle_percpu_irq
, "virq");
669 evtchn_to_irq
[evtchn
] = irq
;
670 irq_info
[irq
] = mk_virq_info(evtchn
, virq
);
672 per_cpu(virq_to_irq
, cpu
)[virq
] = irq
;
674 bind_evtchn_to_cpu(evtchn
, cpu
);
677 spin_unlock(&irq_mapping_update_lock
);
682 static void unbind_from_irq(unsigned int irq
)
684 struct evtchn_close close
;
685 int evtchn
= evtchn_from_irq(irq
);
687 spin_lock(&irq_mapping_update_lock
);
689 if (VALID_EVTCHN(evtchn
)) {
691 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
694 switch (type_from_irq(irq
)) {
696 per_cpu(virq_to_irq
, cpu_from_evtchn(evtchn
))
697 [virq_from_irq(irq
)] = -1;
700 per_cpu(ipi_to_irq
, cpu_from_evtchn(evtchn
))
701 [ipi_from_irq(irq
)] = -1;
707 /* Closed ports are implicitly re-bound to VCPU0. */
708 bind_evtchn_to_cpu(evtchn
, 0);
710 evtchn_to_irq
[evtchn
] = -1;
713 if (irq_info
[irq
].type
!= IRQT_UNBOUND
) {
714 irq_info
[irq
] = mk_unbound_info();
719 spin_unlock(&irq_mapping_update_lock
);
722 int bind_evtchn_to_irqhandler(unsigned int evtchn
,
723 irq_handler_t handler
,
724 unsigned long irqflags
,
725 const char *devname
, void *dev_id
)
730 irq
= bind_evtchn_to_irq(evtchn
);
731 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
733 unbind_from_irq(irq
);
739 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler
);
741 int bind_virq_to_irqhandler(unsigned int virq
, unsigned int cpu
,
742 irq_handler_t handler
,
743 unsigned long irqflags
, const char *devname
, void *dev_id
)
748 irq
= bind_virq_to_irq(virq
, cpu
);
749 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
751 unbind_from_irq(irq
);
757 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler
);
759 int bind_ipi_to_irqhandler(enum ipi_vector ipi
,
761 irq_handler_t handler
,
762 unsigned long irqflags
,
768 irq
= bind_ipi_to_irq(ipi
, cpu
);
772 irqflags
|= IRQF_NO_SUSPEND
;
773 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
775 unbind_from_irq(irq
);
782 void unbind_from_irqhandler(unsigned int irq
, void *dev_id
)
784 free_irq(irq
, dev_id
);
785 unbind_from_irq(irq
);
787 EXPORT_SYMBOL_GPL(unbind_from_irqhandler
);
789 void xen_send_IPI_one(unsigned int cpu
, enum ipi_vector vector
)
791 int irq
= per_cpu(ipi_to_irq
, cpu
)[vector
];
793 notify_remote_via_irq(irq
);
796 irqreturn_t
xen_debug_interrupt(int irq
, void *dev_id
)
798 struct shared_info
*sh
= HYPERVISOR_shared_info
;
799 int cpu
= smp_processor_id();
802 static DEFINE_SPINLOCK(debug_lock
);
804 spin_lock_irqsave(&debug_lock
, flags
);
806 printk("vcpu %d\n ", cpu
);
808 for_each_online_cpu(i
) {
809 struct vcpu_info
*v
= per_cpu(xen_vcpu
, i
);
810 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i
,
811 (get_irq_regs() && i
== cpu
) ? xen_irqs_disabled(get_irq_regs()) : v
->evtchn_upcall_mask
,
812 v
->evtchn_upcall_pending
,
813 v
->evtchn_pending_sel
);
815 printk("pending:\n ");
816 for(i
= ARRAY_SIZE(sh
->evtchn_pending
)-1; i
>= 0; i
--)
817 printk("%08lx%s", sh
->evtchn_pending
[i
],
818 i
% 8 == 0 ? "\n " : " ");
819 printk("\nmasks:\n ");
820 for(i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
821 printk("%08lx%s", sh
->evtchn_mask
[i
],
822 i
% 8 == 0 ? "\n " : " ");
824 printk("\nunmasked:\n ");
825 for(i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
826 printk("%08lx%s", sh
->evtchn_pending
[i
] & ~sh
->evtchn_mask
[i
],
827 i
% 8 == 0 ? "\n " : " ");
829 printk("\npending list:\n");
830 for(i
= 0; i
< NR_EVENT_CHANNELS
; i
++) {
831 if (sync_test_bit(i
, sh
->evtchn_pending
)) {
832 printk(" %d: event %d -> irq %d\n",
833 cpu_from_evtchn(i
), i
,
838 spin_unlock_irqrestore(&debug_lock
, flags
);
843 static DEFINE_PER_CPU(unsigned, xed_nesting_count
);
846 * Search the CPUs pending events bitmasks. For each one found, map
847 * the event number to an irq, and feed it into do_IRQ() for
850 * Xen uses a two-level bitmap to speed searching. The first level is
851 * a bitset of words which contain pending event bits. The second
852 * level is a bitset of pending events themselves.
854 static void __xen_evtchn_do_upcall(void)
857 struct shared_info
*s
= HYPERVISOR_shared_info
;
858 struct vcpu_info
*vcpu_info
= __get_cpu_var(xen_vcpu
);
862 unsigned long pending_words
;
864 vcpu_info
->evtchn_upcall_pending
= 0;
866 if (__get_cpu_var(xed_nesting_count
)++)
869 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
870 /* Clear master flag /before/ clearing selector flag. */
873 pending_words
= xchg(&vcpu_info
->evtchn_pending_sel
, 0);
874 while (pending_words
!= 0) {
875 unsigned long pending_bits
;
876 int word_idx
= __ffs(pending_words
);
877 pending_words
&= ~(1UL << word_idx
);
879 while ((pending_bits
= active_evtchns(cpu
, s
, word_idx
)) != 0) {
880 int bit_idx
= __ffs(pending_bits
);
881 int port
= (word_idx
* BITS_PER_LONG
) + bit_idx
;
882 int irq
= evtchn_to_irq
[port
];
883 struct irq_desc
*desc
;
886 desc
= irq_to_desc(irq
);
888 generic_handle_irq_desc(irq
, desc
);
893 BUG_ON(!irqs_disabled());
895 count
= __get_cpu_var(xed_nesting_count
);
896 __get_cpu_var(xed_nesting_count
) = 0;
897 } while (count
!= 1 || vcpu_info
->evtchn_upcall_pending
);
904 void xen_evtchn_do_upcall(struct pt_regs
*regs
)
906 struct pt_regs
*old_regs
= set_irq_regs(regs
);
911 __xen_evtchn_do_upcall();
914 set_irq_regs(old_regs
);
917 void xen_hvm_evtchn_do_upcall(void)
919 __xen_evtchn_do_upcall();
921 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall
);
923 /* Rebind a new event channel to an existing irq. */
924 void rebind_evtchn_irq(int evtchn
, int irq
)
926 struct irq_info
*info
= info_for_irq(irq
);
928 /* Make sure the irq is masked, since the new event channel
929 will also be masked. */
932 spin_lock(&irq_mapping_update_lock
);
934 /* After resume the irq<->evtchn mappings are all cleared out */
935 BUG_ON(evtchn_to_irq
[evtchn
] != -1);
936 /* Expect irq to have been bound before,
937 so there should be a proper type */
938 BUG_ON(info
->type
== IRQT_UNBOUND
);
940 evtchn_to_irq
[evtchn
] = irq
;
941 irq_info
[irq
] = mk_evtchn_info(evtchn
);
943 spin_unlock(&irq_mapping_update_lock
);
945 /* new event channels are always bound to cpu 0 */
946 irq_set_affinity(irq
, cpumask_of(0));
948 /* Unmask the event channel. */
952 /* Rebind an evtchn so that it gets delivered to a specific cpu */
953 static int rebind_irq_to_cpu(unsigned irq
, unsigned tcpu
)
955 struct evtchn_bind_vcpu bind_vcpu
;
956 int evtchn
= evtchn_from_irq(irq
);
958 /* events delivered via platform PCI interrupts are always
959 * routed to vcpu 0 */
960 if (!VALID_EVTCHN(evtchn
) ||
961 (xen_hvm_domain() && !xen_have_vector_callback
))
964 /* Send future instances of this interrupt to other vcpu. */
965 bind_vcpu
.port
= evtchn
;
966 bind_vcpu
.vcpu
= tcpu
;
969 * If this fails, it usually just indicates that we're dealing with a
970 * virq or IPI channel, which don't actually need to be rebound. Ignore
971 * it, but don't do the xenlinux-level rebind in that case.
973 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu
, &bind_vcpu
) >= 0)
974 bind_evtchn_to_cpu(evtchn
, tcpu
);
979 static int set_affinity_irq(unsigned irq
, const struct cpumask
*dest
)
981 unsigned tcpu
= cpumask_first(dest
);
983 return rebind_irq_to_cpu(irq
, tcpu
);
986 int resend_irq_on_evtchn(unsigned int irq
)
988 int masked
, evtchn
= evtchn_from_irq(irq
);
989 struct shared_info
*s
= HYPERVISOR_shared_info
;
991 if (!VALID_EVTCHN(evtchn
))
994 masked
= sync_test_and_set_bit(evtchn
, s
->evtchn_mask
);
995 sync_set_bit(evtchn
, s
->evtchn_pending
);
997 unmask_evtchn(evtchn
);
1002 static void enable_dynirq(unsigned int irq
)
1004 int evtchn
= evtchn_from_irq(irq
);
1006 if (VALID_EVTCHN(evtchn
))
1007 unmask_evtchn(evtchn
);
1010 static void disable_dynirq(unsigned int irq
)
1012 int evtchn
= evtchn_from_irq(irq
);
1014 if (VALID_EVTCHN(evtchn
))
1015 mask_evtchn(evtchn
);
1018 static void ack_dynirq(unsigned int irq
)
1020 int evtchn
= evtchn_from_irq(irq
);
1022 move_native_irq(irq
);
1024 if (VALID_EVTCHN(evtchn
))
1025 clear_evtchn(evtchn
);
1028 static int retrigger_dynirq(unsigned int irq
)
1030 int evtchn
= evtchn_from_irq(irq
);
1031 struct shared_info
*sh
= HYPERVISOR_shared_info
;
1034 if (VALID_EVTCHN(evtchn
)) {
1037 masked
= sync_test_and_set_bit(evtchn
, sh
->evtchn_mask
);
1038 sync_set_bit(evtchn
, sh
->evtchn_pending
);
1040 unmask_evtchn(evtchn
);
1047 static void restore_cpu_virqs(unsigned int cpu
)
1049 struct evtchn_bind_virq bind_virq
;
1050 int virq
, irq
, evtchn
;
1052 for (virq
= 0; virq
< NR_VIRQS
; virq
++) {
1053 if ((irq
= per_cpu(virq_to_irq
, cpu
)[virq
]) == -1)
1056 BUG_ON(virq_from_irq(irq
) != virq
);
1058 /* Get a new binding from Xen. */
1059 bind_virq
.virq
= virq
;
1060 bind_virq
.vcpu
= cpu
;
1061 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
1064 evtchn
= bind_virq
.port
;
1066 /* Record the new mapping. */
1067 evtchn_to_irq
[evtchn
] = irq
;
1068 irq_info
[irq
] = mk_virq_info(evtchn
, virq
);
1069 bind_evtchn_to_cpu(evtchn
, cpu
);
1071 /* Ready for use. */
1072 unmask_evtchn(evtchn
);
1076 static void restore_cpu_ipis(unsigned int cpu
)
1078 struct evtchn_bind_ipi bind_ipi
;
1079 int ipi
, irq
, evtchn
;
1081 for (ipi
= 0; ipi
< XEN_NR_IPIS
; ipi
++) {
1082 if ((irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
]) == -1)
1085 BUG_ON(ipi_from_irq(irq
) != ipi
);
1087 /* Get a new binding from Xen. */
1088 bind_ipi
.vcpu
= cpu
;
1089 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
1092 evtchn
= bind_ipi
.port
;
1094 /* Record the new mapping. */
1095 evtchn_to_irq
[evtchn
] = irq
;
1096 irq_info
[irq
] = mk_ipi_info(evtchn
, ipi
);
1097 bind_evtchn_to_cpu(evtchn
, cpu
);
1099 /* Ready for use. */
1100 unmask_evtchn(evtchn
);
1105 /* Clear an irq's pending state, in preparation for polling on it */
1106 void xen_clear_irq_pending(int irq
)
1108 int evtchn
= evtchn_from_irq(irq
);
1110 if (VALID_EVTCHN(evtchn
))
1111 clear_evtchn(evtchn
);
1114 void xen_set_irq_pending(int irq
)
1116 int evtchn
= evtchn_from_irq(irq
);
1118 if (VALID_EVTCHN(evtchn
))
1122 bool xen_test_irq_pending(int irq
)
1124 int evtchn
= evtchn_from_irq(irq
);
1127 if (VALID_EVTCHN(evtchn
))
1128 ret
= test_evtchn(evtchn
);
1133 /* Poll waiting for an irq to become pending. In the usual case, the
1134 irq will be disabled so it won't deliver an interrupt. */
1135 void xen_poll_irq(int irq
)
1137 evtchn_port_t evtchn
= evtchn_from_irq(irq
);
1139 if (VALID_EVTCHN(evtchn
)) {
1140 struct sched_poll poll
;
1144 set_xen_guest_handle(poll
.ports
, &evtchn
);
1146 if (HYPERVISOR_sched_op(SCHEDOP_poll
, &poll
) != 0)
1151 void xen_irq_resume(void)
1153 unsigned int cpu
, irq
, evtchn
;
1155 init_evtchn_cpu_bindings();
1157 /* New event-channel space is not 'live' yet. */
1158 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1159 mask_evtchn(evtchn
);
1161 /* No IRQ <-> event-channel mappings. */
1162 for (irq
= 0; irq
< nr_irqs
; irq
++)
1163 irq_info
[irq
].evtchn
= 0; /* zap event-channel binding */
1165 for (evtchn
= 0; evtchn
< NR_EVENT_CHANNELS
; evtchn
++)
1166 evtchn_to_irq
[evtchn
] = -1;
1168 for_each_possible_cpu(cpu
) {
1169 restore_cpu_virqs(cpu
);
1170 restore_cpu_ipis(cpu
);
1174 static struct irq_chip xen_dynamic_chip __read_mostly
= {
1177 .disable
= disable_dynirq
,
1178 .mask
= disable_dynirq
,
1179 .unmask
= enable_dynirq
,
1182 .set_affinity
= set_affinity_irq
,
1183 .retrigger
= retrigger_dynirq
,
1186 static struct irq_chip xen_pirq_chip __read_mostly
= {
1189 .startup
= startup_pirq
,
1190 .shutdown
= shutdown_pirq
,
1192 .enable
= enable_pirq
,
1193 .unmask
= enable_pirq
,
1195 .disable
= disable_pirq
,
1196 .mask
= disable_pirq
,
1201 .set_affinity
= set_affinity_irq
,
1203 .retrigger
= retrigger_dynirq
,
1206 static struct irq_chip xen_percpu_chip __read_mostly
= {
1207 .name
= "xen-percpu",
1209 .disable
= disable_dynirq
,
1210 .mask
= disable_dynirq
,
1211 .unmask
= enable_dynirq
,
1216 int xen_set_callback_via(uint64_t via
)
1218 struct xen_hvm_param a
;
1219 a
.domid
= DOMID_SELF
;
1220 a
.index
= HVM_PARAM_CALLBACK_IRQ
;
1222 return HYPERVISOR_hvm_op(HVMOP_set_param
, &a
);
1224 EXPORT_SYMBOL_GPL(xen_set_callback_via
);
1226 #ifdef CONFIG_XEN_PVHVM
1227 /* Vector callbacks are better than PCI interrupts to receive event
1228 * channel notifications because we can receive vector callbacks on any
1229 * vcpu and we don't need PCI support or APIC interactions. */
1230 void xen_callback_vector(void)
1233 uint64_t callback_via
;
1234 if (xen_have_vector_callback
) {
1235 callback_via
= HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK
);
1236 rc
= xen_set_callback_via(callback_via
);
1238 printk(KERN_ERR
"Request for Xen HVM callback vector"
1240 xen_have_vector_callback
= 0;
1243 printk(KERN_INFO
"Xen HVM callback vector for event delivery is "
1245 /* in the restore case the vector has already been allocated */
1246 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK
, used_vectors
))
1247 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK
, xen_hvm_callback_vector
);
1251 void xen_callback_vector(void) {}
1254 void __init
xen_init_IRQ(void)
1258 cpu_evtchn_mask_p
= kcalloc(nr_cpu_ids
, sizeof(struct cpu_evtchn_s
),
1260 BUG_ON(cpu_evtchn_mask_p
== NULL
);
1262 init_evtchn_cpu_bindings();
1264 /* No event channels are 'live' right now. */
1265 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
1268 if (xen_hvm_domain()) {
1269 xen_callback_vector();
1272 irq_ctx_init(smp_processor_id());