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. Hardware interrupts. Not supported at present.
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>
30 #include <asm/ptrace.h>
32 #include <asm/sync_bitops.h>
33 #include <asm/xen/hypercall.h>
34 #include <asm/xen/hypervisor.h>
36 #include <xen/xen-ops.h>
37 #include <xen/events.h>
38 #include <xen/interface/xen.h>
39 #include <xen/interface/event_channel.h>
42 * This lock protects updates to the following mapping and reference-count
43 * arrays. The lock does not need to be acquired to read the mapping tables.
45 static DEFINE_SPINLOCK(irq_mapping_update_lock
);
47 /* IRQ <-> VIRQ mapping. */
48 static DEFINE_PER_CPU(int, virq_to_irq
[NR_VIRQS
]) = {[0 ... NR_VIRQS
-1] = -1};
50 /* IRQ <-> IPI mapping */
51 static DEFINE_PER_CPU(int, ipi_to_irq
[XEN_NR_IPIS
]) = {[0 ... XEN_NR_IPIS
-1] = -1};
53 /* Packed IRQ information: binding type, sub-type index, and event channel. */
56 unsigned short evtchn
;
61 static struct packed_irq irq_info
[NR_IRQS
];
72 /* Convenient shorthand for packed representation of an unbound IRQ. */
73 #define IRQ_UNBOUND mk_irq_info(IRQT_UNBOUND, 0, 0)
75 static int evtchn_to_irq
[NR_EVENT_CHANNELS
] = {
76 [0 ... NR_EVENT_CHANNELS
-1] = -1
78 static unsigned long cpu_evtchn_mask
[NR_CPUS
][NR_EVENT_CHANNELS
/BITS_PER_LONG
];
79 static u8 cpu_evtchn
[NR_EVENT_CHANNELS
];
81 /* Reference counts for bindings to IRQs. */
82 static int irq_bindcount
[NR_IRQS
];
84 /* Xen will never allocate port zero for any purpose. */
85 #define VALID_EVTCHN(chn) ((chn) != 0)
88 * Force a proper event-channel callback from Xen after clearing the
89 * callback mask. We do this in a very simple manner, by making a call
90 * down into Xen. The pending flag will be checked by Xen on return.
92 void force_evtchn_callback(void)
94 (void)HYPERVISOR_xen_version(0, NULL
);
96 EXPORT_SYMBOL_GPL(force_evtchn_callback
);
98 static struct irq_chip xen_dynamic_chip
;
100 /* Constructor for packed IRQ information. */
101 static inline struct packed_irq
mk_irq_info(u32 type
, u32 index
, u32 evtchn
)
103 return (struct packed_irq
) { evtchn
, index
, type
};
107 * Accessors for packed IRQ information.
109 static inline unsigned int evtchn_from_irq(int irq
)
111 return irq_info
[irq
].evtchn
;
114 static inline unsigned int index_from_irq(int irq
)
116 return irq_info
[irq
].index
;
119 static inline unsigned int type_from_irq(int irq
)
121 return irq_info
[irq
].type
;
124 static inline unsigned long active_evtchns(unsigned int cpu
,
125 struct shared_info
*sh
,
128 return (sh
->evtchn_pending
[idx
] &
129 cpu_evtchn_mask
[cpu
][idx
] &
130 ~sh
->evtchn_mask
[idx
]);
133 static void bind_evtchn_to_cpu(unsigned int chn
, unsigned int cpu
)
135 int irq
= evtchn_to_irq
[chn
];
139 irq_desc
[irq
].affinity
= cpumask_of_cpu(cpu
);
142 __clear_bit(chn
, cpu_evtchn_mask
[cpu_evtchn
[chn
]]);
143 __set_bit(chn
, cpu_evtchn_mask
[cpu
]);
145 cpu_evtchn
[chn
] = cpu
;
148 static void init_evtchn_cpu_bindings(void)
152 /* By default all event channels notify CPU#0. */
153 for (i
= 0; i
< NR_IRQS
; i
++)
154 irq_desc
[i
].affinity
= cpumask_of_cpu(0);
157 memset(cpu_evtchn
, 0, sizeof(cpu_evtchn
));
158 memset(cpu_evtchn_mask
[0], ~0, sizeof(cpu_evtchn_mask
[0]));
161 static inline unsigned int cpu_from_evtchn(unsigned int evtchn
)
163 return cpu_evtchn
[evtchn
];
166 static inline void clear_evtchn(int port
)
168 struct shared_info
*s
= HYPERVISOR_shared_info
;
169 sync_clear_bit(port
, &s
->evtchn_pending
[0]);
172 static inline void set_evtchn(int port
)
174 struct shared_info
*s
= HYPERVISOR_shared_info
;
175 sync_set_bit(port
, &s
->evtchn_pending
[0]);
180 * notify_remote_via_irq - send event to remote end of event channel via irq
181 * @irq: irq of event channel to send event to
183 * Unlike notify_remote_via_evtchn(), this is safe to use across
184 * save/restore. Notifications on a broken connection are silently
187 void notify_remote_via_irq(int irq
)
189 int evtchn
= evtchn_from_irq(irq
);
191 if (VALID_EVTCHN(evtchn
))
192 notify_remote_via_evtchn(evtchn
);
194 EXPORT_SYMBOL_GPL(notify_remote_via_irq
);
196 static void mask_evtchn(int port
)
198 struct shared_info
*s
= HYPERVISOR_shared_info
;
199 sync_set_bit(port
, &s
->evtchn_mask
[0]);
202 static void unmask_evtchn(int port
)
204 struct shared_info
*s
= HYPERVISOR_shared_info
;
205 unsigned int cpu
= get_cpu();
207 BUG_ON(!irqs_disabled());
209 /* Slow path (hypercall) if this is a non-local port. */
210 if (unlikely(cpu
!= cpu_from_evtchn(port
))) {
211 struct evtchn_unmask unmask
= { .port
= port
};
212 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask
, &unmask
);
214 struct vcpu_info
*vcpu_info
= __get_cpu_var(xen_vcpu
);
216 sync_clear_bit(port
, &s
->evtchn_mask
[0]);
219 * The following is basically the equivalent of
220 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
221 * the interrupt edge' if the channel is masked.
223 if (sync_test_bit(port
, &s
->evtchn_pending
[0]) &&
224 !sync_test_and_set_bit(port
/ BITS_PER_LONG
,
225 &vcpu_info
->evtchn_pending_sel
))
226 vcpu_info
->evtchn_upcall_pending
= 1;
232 static int find_unbound_irq(void)
236 /* Only allocate from dynirq range */
237 for (irq
= 0; irq
< NR_IRQS
; irq
++)
238 if (irq_bindcount
[irq
] == 0)
242 panic("No available IRQ to bind to: increase NR_IRQS!\n");
247 int bind_evtchn_to_irq(unsigned int evtchn
)
251 spin_lock(&irq_mapping_update_lock
);
253 irq
= evtchn_to_irq
[evtchn
];
256 irq
= find_unbound_irq();
258 dynamic_irq_init(irq
);
259 set_irq_chip_and_handler_name(irq
, &xen_dynamic_chip
,
260 handle_level_irq
, "event");
262 evtchn_to_irq
[evtchn
] = irq
;
263 irq_info
[irq
] = mk_irq_info(IRQT_EVTCHN
, 0, evtchn
);
266 irq_bindcount
[irq
]++;
268 spin_unlock(&irq_mapping_update_lock
);
272 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq
);
274 static int bind_ipi_to_irq(unsigned int ipi
, unsigned int cpu
)
276 struct evtchn_bind_ipi bind_ipi
;
279 spin_lock(&irq_mapping_update_lock
);
281 irq
= per_cpu(ipi_to_irq
, cpu
)[ipi
];
283 irq
= find_unbound_irq();
287 dynamic_irq_init(irq
);
288 set_irq_chip_and_handler_name(irq
, &xen_dynamic_chip
,
289 handle_level_irq
, "ipi");
292 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi
,
295 evtchn
= bind_ipi
.port
;
297 evtchn_to_irq
[evtchn
] = irq
;
298 irq_info
[irq
] = mk_irq_info(IRQT_IPI
, ipi
, evtchn
);
300 per_cpu(ipi_to_irq
, cpu
)[ipi
] = irq
;
302 bind_evtchn_to_cpu(evtchn
, cpu
);
305 irq_bindcount
[irq
]++;
308 spin_unlock(&irq_mapping_update_lock
);
313 static int bind_virq_to_irq(unsigned int virq
, unsigned int cpu
)
315 struct evtchn_bind_virq bind_virq
;
318 spin_lock(&irq_mapping_update_lock
);
320 irq
= per_cpu(virq_to_irq
, cpu
)[virq
];
323 bind_virq
.virq
= virq
;
324 bind_virq
.vcpu
= cpu
;
325 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
328 evtchn
= bind_virq
.port
;
330 irq
= find_unbound_irq();
332 dynamic_irq_init(irq
);
333 set_irq_chip_and_handler_name(irq
, &xen_dynamic_chip
,
334 handle_level_irq
, "virq");
336 evtchn_to_irq
[evtchn
] = irq
;
337 irq_info
[irq
] = mk_irq_info(IRQT_VIRQ
, virq
, evtchn
);
339 per_cpu(virq_to_irq
, cpu
)[virq
] = irq
;
341 bind_evtchn_to_cpu(evtchn
, cpu
);
344 irq_bindcount
[irq
]++;
346 spin_unlock(&irq_mapping_update_lock
);
351 static void unbind_from_irq(unsigned int irq
)
353 struct evtchn_close close
;
354 int evtchn
= evtchn_from_irq(irq
);
356 spin_lock(&irq_mapping_update_lock
);
358 if (VALID_EVTCHN(evtchn
) && (--irq_bindcount
[irq
] == 0)) {
360 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
363 switch (type_from_irq(irq
)) {
365 per_cpu(virq_to_irq
, cpu_from_evtchn(evtchn
))
366 [index_from_irq(irq
)] = -1;
372 /* Closed ports are implicitly re-bound to VCPU0. */
373 bind_evtchn_to_cpu(evtchn
, 0);
375 evtchn_to_irq
[evtchn
] = -1;
376 irq_info
[irq
] = IRQ_UNBOUND
;
378 dynamic_irq_init(irq
);
381 spin_unlock(&irq_mapping_update_lock
);
384 int bind_evtchn_to_irqhandler(unsigned int evtchn
,
385 irq_handler_t handler
,
386 unsigned long irqflags
,
387 const char *devname
, void *dev_id
)
392 irq
= bind_evtchn_to_irq(evtchn
);
393 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
395 unbind_from_irq(irq
);
401 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler
);
403 int bind_virq_to_irqhandler(unsigned int virq
, unsigned int cpu
,
404 irq_handler_t handler
,
405 unsigned long irqflags
, const char *devname
, void *dev_id
)
410 irq
= bind_virq_to_irq(virq
, cpu
);
411 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
413 unbind_from_irq(irq
);
419 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler
);
421 int bind_ipi_to_irqhandler(enum ipi_vector ipi
,
423 irq_handler_t handler
,
424 unsigned long irqflags
,
430 irq
= bind_ipi_to_irq(ipi
, cpu
);
434 retval
= request_irq(irq
, handler
, irqflags
, devname
, dev_id
);
436 unbind_from_irq(irq
);
443 void unbind_from_irqhandler(unsigned int irq
, void *dev_id
)
445 free_irq(irq
, dev_id
);
446 unbind_from_irq(irq
);
448 EXPORT_SYMBOL_GPL(unbind_from_irqhandler
);
450 void xen_send_IPI_one(unsigned int cpu
, enum ipi_vector vector
)
452 int irq
= per_cpu(ipi_to_irq
, cpu
)[vector
];
454 notify_remote_via_irq(irq
);
457 irqreturn_t
xen_debug_interrupt(int irq
, void *dev_id
)
459 struct shared_info
*sh
= HYPERVISOR_shared_info
;
460 int cpu
= smp_processor_id();
463 static DEFINE_SPINLOCK(debug_lock
);
465 spin_lock_irqsave(&debug_lock
, flags
);
467 printk("vcpu %d\n ", cpu
);
469 for_each_online_cpu(i
) {
470 struct vcpu_info
*v
= per_cpu(xen_vcpu
, i
);
471 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i
,
472 (get_irq_regs() && i
== cpu
) ? xen_irqs_disabled(get_irq_regs()) : v
->evtchn_upcall_mask
,
473 v
->evtchn_upcall_pending
,
474 v
->evtchn_pending_sel
);
476 printk("pending:\n ");
477 for(i
= ARRAY_SIZE(sh
->evtchn_pending
)-1; i
>= 0; i
--)
478 printk("%08lx%s", sh
->evtchn_pending
[i
],
479 i
% 8 == 0 ? "\n " : " ");
480 printk("\nmasks:\n ");
481 for(i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
482 printk("%08lx%s", sh
->evtchn_mask
[i
],
483 i
% 8 == 0 ? "\n " : " ");
485 printk("\nunmasked:\n ");
486 for(i
= ARRAY_SIZE(sh
->evtchn_mask
)-1; i
>= 0; i
--)
487 printk("%08lx%s", sh
->evtchn_pending
[i
] & ~sh
->evtchn_mask
[i
],
488 i
% 8 == 0 ? "\n " : " ");
490 printk("\npending list:\n");
491 for(i
= 0; i
< NR_EVENT_CHANNELS
; i
++) {
492 if (sync_test_bit(i
, sh
->evtchn_pending
)) {
493 printk(" %d: event %d -> irq %d\n",
499 spin_unlock_irqrestore(&debug_lock
, flags
);
506 * Search the CPUs pending events bitmasks. For each one found, map
507 * the event number to an irq, and feed it into do_IRQ() for
510 * Xen uses a two-level bitmap to speed searching. The first level is
511 * a bitset of words which contain pending event bits. The second
512 * level is a bitset of pending events themselves.
514 void xen_evtchn_do_upcall(struct pt_regs
*regs
)
517 struct shared_info
*s
= HYPERVISOR_shared_info
;
518 struct vcpu_info
*vcpu_info
= __get_cpu_var(xen_vcpu
);
519 static DEFINE_PER_CPU(unsigned, nesting_count
);
523 unsigned long pending_words
;
525 vcpu_info
->evtchn_upcall_pending
= 0;
527 if (__get_cpu_var(nesting_count
)++)
530 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
531 /* Clear master flag /before/ clearing selector flag. */
534 pending_words
= xchg(&vcpu_info
->evtchn_pending_sel
, 0);
535 while (pending_words
!= 0) {
536 unsigned long pending_bits
;
537 int word_idx
= __ffs(pending_words
);
538 pending_words
&= ~(1UL << word_idx
);
540 while ((pending_bits
= active_evtchns(cpu
, s
, word_idx
)) != 0) {
541 int bit_idx
= __ffs(pending_bits
);
542 int port
= (word_idx
* BITS_PER_LONG
) + bit_idx
;
543 int irq
= evtchn_to_irq
[port
];
546 xen_do_IRQ(irq
, regs
);
550 BUG_ON(!irqs_disabled());
552 count
= __get_cpu_var(nesting_count
);
553 __get_cpu_var(nesting_count
) = 0;
560 /* Rebind an evtchn so that it gets delivered to a specific cpu */
561 static void rebind_irq_to_cpu(unsigned irq
, unsigned tcpu
)
563 struct evtchn_bind_vcpu bind_vcpu
;
564 int evtchn
= evtchn_from_irq(irq
);
566 if (!VALID_EVTCHN(evtchn
))
569 /* Send future instances of this interrupt to other vcpu. */
570 bind_vcpu
.port
= evtchn
;
571 bind_vcpu
.vcpu
= tcpu
;
574 * If this fails, it usually just indicates that we're dealing with a
575 * virq or IPI channel, which don't actually need to be rebound. Ignore
576 * it, but don't do the xenlinux-level rebind in that case.
578 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu
, &bind_vcpu
) >= 0)
579 bind_evtchn_to_cpu(evtchn
, tcpu
);
583 static void set_affinity_irq(unsigned irq
, cpumask_t dest
)
585 unsigned tcpu
= first_cpu(dest
);
586 rebind_irq_to_cpu(irq
, tcpu
);
589 int resend_irq_on_evtchn(unsigned int irq
)
591 int masked
, evtchn
= evtchn_from_irq(irq
);
592 struct shared_info
*s
= HYPERVISOR_shared_info
;
594 if (!VALID_EVTCHN(evtchn
))
597 masked
= sync_test_and_set_bit(evtchn
, s
->evtchn_mask
);
598 sync_set_bit(evtchn
, s
->evtchn_pending
);
600 unmask_evtchn(evtchn
);
605 static void enable_dynirq(unsigned int irq
)
607 int evtchn
= evtchn_from_irq(irq
);
609 if (VALID_EVTCHN(evtchn
))
610 unmask_evtchn(evtchn
);
613 static void disable_dynirq(unsigned int irq
)
615 int evtchn
= evtchn_from_irq(irq
);
617 if (VALID_EVTCHN(evtchn
))
621 static void ack_dynirq(unsigned int irq
)
623 int evtchn
= evtchn_from_irq(irq
);
625 move_native_irq(irq
);
627 if (VALID_EVTCHN(evtchn
))
628 clear_evtchn(evtchn
);
631 static int retrigger_dynirq(unsigned int irq
)
633 int evtchn
= evtchn_from_irq(irq
);
634 struct shared_info
*sh
= HYPERVISOR_shared_info
;
637 if (VALID_EVTCHN(evtchn
)) {
640 masked
= sync_test_and_set_bit(evtchn
, sh
->evtchn_mask
);
641 sync_set_bit(evtchn
, sh
->evtchn_pending
);
643 unmask_evtchn(evtchn
);
650 static struct irq_chip xen_dynamic_chip __read_mostly
= {
652 .mask
= disable_dynirq
,
653 .unmask
= enable_dynirq
,
655 .set_affinity
= set_affinity_irq
,
656 .retrigger
= retrigger_dynirq
,
659 void __init
xen_init_IRQ(void)
663 init_evtchn_cpu_bindings();
665 /* No event channels are 'live' right now. */
666 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++)
669 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
670 for (i
= 0; i
< NR_IRQS
; i
++)
671 irq_bindcount
[i
] = 0;
673 irq_ctx_init(smp_processor_id());