xen: make sure retriggered events are set pending
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / xen / events.c
blobf73b53bd65b76991b0e53015a261a9657ce23a4c
1 /*
2 * Xen event channels
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
12 * channel:
14 * 1. Inter-domain notifications. This includes all the virtual
15 * device events, since they're driven by front-ends in another domain
16 * (typically dom0).
17 * 2. VIRQs, typically used for timers. These are per-cpu events.
18 * 3. IPIs.
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>
31 #include <asm/irq.h>
32 #include <asm/sync_bitops.h>
33 #include <asm/xen/hypercall.h>
34 #include <asm/xen/hypervisor.h>
36 #include <xen/events.h>
37 #include <xen/interface/xen.h>
38 #include <xen/interface/event_channel.h>
40 #include "xen-ops.h"
43 * This lock protects updates to the following mapping and reference-count
44 * arrays. The lock does not need to be acquired to read the mapping tables.
46 static DEFINE_SPINLOCK(irq_mapping_update_lock);
48 /* IRQ <-> VIRQ mapping. */
49 static DEFINE_PER_CPU(int, virq_to_irq[NR_VIRQS]) = {[0 ... NR_VIRQS-1] = -1};
51 /* IRQ <-> IPI mapping */
52 static DEFINE_PER_CPU(int, ipi_to_irq[XEN_NR_IPIS]) = {[0 ... XEN_NR_IPIS-1] = -1};
54 /* Packed IRQ information: binding type, sub-type index, and event channel. */
55 struct packed_irq
57 unsigned short evtchn;
58 unsigned char index;
59 unsigned char type;
62 static struct packed_irq irq_info[NR_IRQS];
64 /* Binding types. */
65 enum {
66 IRQT_UNBOUND,
67 IRQT_PIRQ,
68 IRQT_VIRQ,
69 IRQT_IPI,
70 IRQT_EVTCHN
73 /* Convenient shorthand for packed representation of an unbound IRQ. */
74 #define IRQ_UNBOUND mk_irq_info(IRQT_UNBOUND, 0, 0)
76 static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
77 [0 ... NR_EVENT_CHANNELS-1] = -1
79 static unsigned long cpu_evtchn_mask[NR_CPUS][NR_EVENT_CHANNELS/BITS_PER_LONG];
80 static u8 cpu_evtchn[NR_EVENT_CHANNELS];
82 /* Reference counts for bindings to IRQs. */
83 static int irq_bindcount[NR_IRQS];
85 /* Xen will never allocate port zero for any purpose. */
86 #define VALID_EVTCHN(chn) ((chn) != 0)
89 * Force a proper event-channel callback from Xen after clearing the
90 * callback mask. We do this in a very simple manner, by making a call
91 * down into Xen. The pending flag will be checked by Xen on return.
93 void force_evtchn_callback(void)
95 (void)HYPERVISOR_xen_version(0, NULL);
97 EXPORT_SYMBOL_GPL(force_evtchn_callback);
99 static struct irq_chip xen_dynamic_chip;
101 /* Constructor for packed IRQ information. */
102 static inline struct packed_irq mk_irq_info(u32 type, u32 index, u32 evtchn)
104 return (struct packed_irq) { evtchn, index, type };
108 * Accessors for packed IRQ information.
110 static inline unsigned int evtchn_from_irq(int irq)
112 return irq_info[irq].evtchn;
115 static inline unsigned int index_from_irq(int irq)
117 return irq_info[irq].index;
120 static inline unsigned int type_from_irq(int irq)
122 return irq_info[irq].type;
125 static inline unsigned long active_evtchns(unsigned int cpu,
126 struct shared_info *sh,
127 unsigned int idx)
129 return (sh->evtchn_pending[idx] &
130 cpu_evtchn_mask[cpu][idx] &
131 ~sh->evtchn_mask[idx]);
134 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
136 int irq = evtchn_to_irq[chn];
138 BUG_ON(irq == -1);
139 #ifdef CONFIG_SMP
140 irq_desc[irq].affinity = cpumask_of_cpu(cpu);
141 #endif
143 __clear_bit(chn, cpu_evtchn_mask[cpu_evtchn[chn]]);
144 __set_bit(chn, cpu_evtchn_mask[cpu]);
146 cpu_evtchn[chn] = cpu;
149 static void init_evtchn_cpu_bindings(void)
151 #ifdef CONFIG_SMP
152 int i;
153 /* By default all event channels notify CPU#0. */
154 for (i = 0; i < NR_IRQS; i++)
155 irq_desc[i].affinity = cpumask_of_cpu(0);
156 #endif
158 memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
159 memset(cpu_evtchn_mask[0], ~0, sizeof(cpu_evtchn_mask[0]));
162 static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
164 return cpu_evtchn[evtchn];
167 static inline void clear_evtchn(int port)
169 struct shared_info *s = HYPERVISOR_shared_info;
170 sync_clear_bit(port, &s->evtchn_pending[0]);
173 static inline void set_evtchn(int port)
175 struct shared_info *s = HYPERVISOR_shared_info;
176 sync_set_bit(port, &s->evtchn_pending[0]);
181 * notify_remote_via_irq - send event to remote end of event channel via irq
182 * @irq: irq of event channel to send event to
184 * Unlike notify_remote_via_evtchn(), this is safe to use across
185 * save/restore. Notifications on a broken connection are silently
186 * dropped.
188 void notify_remote_via_irq(int irq)
190 int evtchn = evtchn_from_irq(irq);
192 if (VALID_EVTCHN(evtchn))
193 notify_remote_via_evtchn(evtchn);
195 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
197 static void mask_evtchn(int port)
199 struct shared_info *s = HYPERVISOR_shared_info;
200 sync_set_bit(port, &s->evtchn_mask[0]);
203 static void unmask_evtchn(int port)
205 struct shared_info *s = HYPERVISOR_shared_info;
206 unsigned int cpu = get_cpu();
208 BUG_ON(!irqs_disabled());
210 /* Slow path (hypercall) if this is a non-local port. */
211 if (unlikely(cpu != cpu_from_evtchn(port))) {
212 struct evtchn_unmask unmask = { .port = port };
213 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
214 } else {
215 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
217 sync_clear_bit(port, &s->evtchn_mask[0]);
220 * The following is basically the equivalent of
221 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
222 * the interrupt edge' if the channel is masked.
224 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
225 !sync_test_and_set_bit(port / BITS_PER_LONG,
226 &vcpu_info->evtchn_pending_sel))
227 vcpu_info->evtchn_upcall_pending = 1;
230 put_cpu();
233 static int find_unbound_irq(void)
235 int irq;
237 /* Only allocate from dynirq range */
238 for (irq = 0; irq < NR_IRQS; irq++)
239 if (irq_bindcount[irq] == 0)
240 break;
242 if (irq == NR_IRQS)
243 panic("No available IRQ to bind to: increase NR_IRQS!\n");
245 return irq;
248 int bind_evtchn_to_irq(unsigned int evtchn)
250 int irq;
252 spin_lock(&irq_mapping_update_lock);
254 irq = evtchn_to_irq[evtchn];
256 if (irq == -1) {
257 irq = find_unbound_irq();
259 dynamic_irq_init(irq);
260 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
261 handle_level_irq, "event");
263 evtchn_to_irq[evtchn] = irq;
264 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
267 irq_bindcount[irq]++;
269 spin_unlock(&irq_mapping_update_lock);
271 return irq;
273 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
275 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
277 struct evtchn_bind_ipi bind_ipi;
278 int evtchn, irq;
280 spin_lock(&irq_mapping_update_lock);
282 irq = per_cpu(ipi_to_irq, cpu)[ipi];
283 if (irq == -1) {
284 irq = find_unbound_irq();
285 if (irq < 0)
286 goto out;
288 dynamic_irq_init(irq);
289 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
290 handle_level_irq, "ipi");
292 bind_ipi.vcpu = cpu;
293 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
294 &bind_ipi) != 0)
295 BUG();
296 evtchn = bind_ipi.port;
298 evtchn_to_irq[evtchn] = irq;
299 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
301 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
303 bind_evtchn_to_cpu(evtchn, cpu);
306 irq_bindcount[irq]++;
308 out:
309 spin_unlock(&irq_mapping_update_lock);
310 return irq;
314 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
316 struct evtchn_bind_virq bind_virq;
317 int evtchn, irq;
319 spin_lock(&irq_mapping_update_lock);
321 irq = per_cpu(virq_to_irq, cpu)[virq];
323 if (irq == -1) {
324 bind_virq.virq = virq;
325 bind_virq.vcpu = cpu;
326 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
327 &bind_virq) != 0)
328 BUG();
329 evtchn = bind_virq.port;
331 irq = find_unbound_irq();
333 dynamic_irq_init(irq);
334 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
335 handle_level_irq, "virq");
337 evtchn_to_irq[evtchn] = irq;
338 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
340 per_cpu(virq_to_irq, cpu)[virq] = irq;
342 bind_evtchn_to_cpu(evtchn, cpu);
345 irq_bindcount[irq]++;
347 spin_unlock(&irq_mapping_update_lock);
349 return irq;
352 static void unbind_from_irq(unsigned int irq)
354 struct evtchn_close close;
355 int evtchn = evtchn_from_irq(irq);
357 spin_lock(&irq_mapping_update_lock);
359 if (VALID_EVTCHN(evtchn) && (--irq_bindcount[irq] == 0)) {
360 close.port = evtchn;
361 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
362 BUG();
364 switch (type_from_irq(irq)) {
365 case IRQT_VIRQ:
366 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
367 [index_from_irq(irq)] = -1;
368 break;
369 default:
370 break;
373 /* Closed ports are implicitly re-bound to VCPU0. */
374 bind_evtchn_to_cpu(evtchn, 0);
376 evtchn_to_irq[evtchn] = -1;
377 irq_info[irq] = IRQ_UNBOUND;
379 dynamic_irq_init(irq);
382 spin_unlock(&irq_mapping_update_lock);
385 int bind_evtchn_to_irqhandler(unsigned int evtchn,
386 irq_handler_t handler,
387 unsigned long irqflags,
388 const char *devname, void *dev_id)
390 unsigned int irq;
391 int retval;
393 irq = bind_evtchn_to_irq(evtchn);
394 retval = request_irq(irq, handler, irqflags, devname, dev_id);
395 if (retval != 0) {
396 unbind_from_irq(irq);
397 return retval;
400 return irq;
402 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
404 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
405 irq_handler_t handler,
406 unsigned long irqflags, const char *devname, void *dev_id)
408 unsigned int irq;
409 int retval;
411 irq = bind_virq_to_irq(virq, cpu);
412 retval = request_irq(irq, handler, irqflags, devname, dev_id);
413 if (retval != 0) {
414 unbind_from_irq(irq);
415 return retval;
418 return irq;
420 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
422 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
423 unsigned int cpu,
424 irq_handler_t handler,
425 unsigned long irqflags,
426 const char *devname,
427 void *dev_id)
429 int irq, retval;
431 irq = bind_ipi_to_irq(ipi, cpu);
432 if (irq < 0)
433 return irq;
435 retval = request_irq(irq, handler, irqflags, devname, dev_id);
436 if (retval != 0) {
437 unbind_from_irq(irq);
438 return retval;
441 return irq;
444 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
446 free_irq(irq, dev_id);
447 unbind_from_irq(irq);
449 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
451 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
453 int irq = per_cpu(ipi_to_irq, cpu)[vector];
454 BUG_ON(irq < 0);
455 notify_remote_via_irq(irq);
458 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
460 struct shared_info *sh = HYPERVISOR_shared_info;
461 int cpu = smp_processor_id();
462 int i;
463 unsigned long flags;
464 static DEFINE_SPINLOCK(debug_lock);
466 spin_lock_irqsave(&debug_lock, flags);
468 printk("vcpu %d\n ", cpu);
470 for_each_online_cpu(i) {
471 struct vcpu_info *v = per_cpu(xen_vcpu, i);
472 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
473 (get_irq_regs() && i == cpu) ? !(get_irq_regs()->flags & X86_EFLAGS_IF) : v->evtchn_upcall_mask,
474 v->evtchn_upcall_pending,
475 v->evtchn_pending_sel);
477 printk("pending:\n ");
478 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
479 printk("%08lx%s", sh->evtchn_pending[i],
480 i % 8 == 0 ? "\n " : " ");
481 printk("\nmasks:\n ");
482 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
483 printk("%08lx%s", sh->evtchn_mask[i],
484 i % 8 == 0 ? "\n " : " ");
486 printk("\nunmasked:\n ");
487 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
488 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
489 i % 8 == 0 ? "\n " : " ");
491 printk("\npending list:\n");
492 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
493 if (sync_test_bit(i, sh->evtchn_pending)) {
494 printk(" %d: event %d -> irq %d\n",
495 cpu_evtchn[i], i,
496 evtchn_to_irq[i]);
500 spin_unlock_irqrestore(&debug_lock, flags);
502 return IRQ_HANDLED;
507 * Search the CPUs pending events bitmasks. For each one found, map
508 * the event number to an irq, and feed it into do_IRQ() for
509 * handling.
511 * Xen uses a two-level bitmap to speed searching. The first level is
512 * a bitset of words which contain pending event bits. The second
513 * level is a bitset of pending events themselves.
515 void xen_evtchn_do_upcall(struct pt_regs *regs)
517 int cpu = get_cpu();
518 struct shared_info *s = HYPERVISOR_shared_info;
519 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
520 unsigned long pending_words;
522 vcpu_info->evtchn_upcall_pending = 0;
524 /* NB. No need for a barrier here -- XCHG is a barrier on x86. */
525 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
526 while (pending_words != 0) {
527 unsigned long pending_bits;
528 int word_idx = __ffs(pending_words);
529 pending_words &= ~(1UL << word_idx);
531 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
532 int bit_idx = __ffs(pending_bits);
533 int port = (word_idx * BITS_PER_LONG) + bit_idx;
534 int irq = evtchn_to_irq[port];
536 if (irq != -1) {
537 regs->orig_ax = ~irq;
538 do_IRQ(regs);
543 put_cpu();
546 /* Rebind an evtchn so that it gets delivered to a specific cpu */
547 static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
549 struct evtchn_bind_vcpu bind_vcpu;
550 int evtchn = evtchn_from_irq(irq);
552 if (!VALID_EVTCHN(evtchn))
553 return;
555 /* Send future instances of this interrupt to other vcpu. */
556 bind_vcpu.port = evtchn;
557 bind_vcpu.vcpu = tcpu;
560 * If this fails, it usually just indicates that we're dealing with a
561 * virq or IPI channel, which don't actually need to be rebound. Ignore
562 * it, but don't do the xenlinux-level rebind in that case.
564 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
565 bind_evtchn_to_cpu(evtchn, tcpu);
569 static void set_affinity_irq(unsigned irq, cpumask_t dest)
571 unsigned tcpu = first_cpu(dest);
572 rebind_irq_to_cpu(irq, tcpu);
575 static void enable_dynirq(unsigned int irq)
577 int evtchn = evtchn_from_irq(irq);
579 if (VALID_EVTCHN(evtchn))
580 unmask_evtchn(evtchn);
583 static void disable_dynirq(unsigned int irq)
585 int evtchn = evtchn_from_irq(irq);
587 if (VALID_EVTCHN(evtchn))
588 mask_evtchn(evtchn);
591 static void ack_dynirq(unsigned int irq)
593 int evtchn = evtchn_from_irq(irq);
595 move_native_irq(irq);
597 if (VALID_EVTCHN(evtchn))
598 clear_evtchn(evtchn);
601 static int retrigger_dynirq(unsigned int irq)
603 int evtchn = evtchn_from_irq(irq);
604 struct shared_info *sh = HYPERVISOR_shared_info;
605 int ret = 0;
607 if (VALID_EVTCHN(evtchn)) {
608 int masked;
610 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
611 sync_set_bit(evtchn, sh->evtchn_pending);
612 if (!masked)
613 unmask_evtchn(evtchn);
614 ret = 1;
617 return ret;
620 static struct irq_chip xen_dynamic_chip __read_mostly = {
621 .name = "xen-dyn",
622 .mask = disable_dynirq,
623 .unmask = enable_dynirq,
624 .ack = ack_dynirq,
625 .set_affinity = set_affinity_irq,
626 .retrigger = retrigger_dynirq,
629 void __init xen_init_IRQ(void)
631 int i;
633 init_evtchn_cpu_bindings();
635 /* No event channels are 'live' right now. */
636 for (i = 0; i < NR_EVENT_CHANNELS; i++)
637 mask_evtchn(i);
639 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
640 for (i = 0; i < NR_IRQS; i++)
641 irq_bindcount[i] = 0;
643 irq_ctx_init(smp_processor_id());