xen: implement pirq type event channels
[linux-2.6.git] / drivers / xen / events.c
blobbc69a9d92abc4c77783e65fa868fc77e3de89e4b
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. 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>
32 #include <asm/desc.h>
33 #include <asm/ptrace.h>
34 #include <asm/irq.h>
35 #include <asm/idle.h>
36 #include <asm/sync_bitops.h>
37 #include <asm/xen/hypercall.h>
38 #include <asm/xen/hypervisor.h>
40 #include <xen/xen.h>
41 #include <xen/hvm.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. */
65 enum xen_irq_type {
66 IRQT_UNBOUND = 0,
67 IRQT_PIRQ,
68 IRQT_VIRQ,
69 IRQT_IPI,
70 IRQT_EVTCHN
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"
80 * VIRQ - virq number
81 * IPI - IPI vector
82 * EVTCHN -
84 struct irq_info
86 enum xen_irq_type type; /* type */
87 unsigned short evtchn; /* event channel */
88 unsigned short cpu; /* cpu bound */
90 union {
91 unsigned short virq;
92 enum ipi_vector ipi;
93 struct {
94 unsigned short gsi;
95 unsigned char vector;
96 unsigned char flags;
97 } pirq;
98 } u;
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,
132 .cpu = 0 };
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);
180 return info->u.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);
190 return info->u.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];
226 unsigned ret = 0;
228 if (irq != -1)
229 ret = cpu_from_irq(irq);
231 return ret;
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,
245 unsigned int idx)
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];
256 BUG_ON(irq == -1);
257 #ifdef CONFIG_SMP
258 cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
259 #endif
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)
269 #ifdef CONFIG_SMP
270 struct irq_desc *desc;
271 int i;
273 /* By default all event channels notify CPU#0. */
274 for_each_irq_desc(i, desc) {
275 cpumask_copy(desc->affinity, cpumask_of(0));
277 #endif
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
307 * dropped.
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);
335 } else {
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;
351 put_cpu();
354 static int find_unbound_irq(void)
356 struct irq_data *data;
357 int irq, res;
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 */
362 if (!data)
363 break;
364 if (data->chip == &no_irq_chip)
365 break;
366 if (data->chip != &xen_dynamic_chip)
367 continue;
368 if (irq_info[irq].type == IRQT_UNBOUND)
369 return irq;
372 if (irq == nr_irqs)
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))
378 return -1;
380 return 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);
395 WARN_ON(rc);
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))
431 goto out;
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",
439 irq);
440 return 0;
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;
450 out:
451 unmask_evtchn(evtchn);
452 pirq_unmask_notify(irq);
454 return 0;
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))
466 return;
468 mask_evtchn(evtchn);
470 close.port = evtchn;
471 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
472 BUG();
474 bind_evtchn_to_cpu(evtchn, 0);
475 evtchn_to_irq[evtchn] = -1;
476 info->evtchn = 0;
479 static void enable_pirq(unsigned int irq)
481 startup_pirq(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)) {
495 mask_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);
505 if (WARN_ON(!desc))
506 return;
508 if ((desc->status & (IRQ_DISABLED|IRQ_PENDING)) ==
509 (IRQ_DISABLED|IRQ_PENDING)) {
510 shutdown_pirq(irq);
511 } else if (VALID_EVTCHN(evtchn)) {
512 unmask_evtchn(evtchn);
513 pirq_unmask_notify(irq);
517 static int find_irq_by_gsi(unsigned gsi)
519 int irq;
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)
525 continue;
527 if (gsi_from_irq(irq) == gsi)
528 return irq;
531 return -1;
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)
541 int irq;
542 struct physdev_irq irq_op;
544 spin_lock(&irq_mapping_update_lock);
546 irq = find_irq_by_gsi(gsi);
547 if (irq != -1) {
548 printk(KERN_INFO "xen_allocate_pirq: returning irq %d for gsi %u\n",
549 irq, gsi);
550 goto out; /* XXX need refcount? */
553 if (identity_mapped_irq(gsi)) {
554 irq = gsi;
555 dynamic_irq_init(irq);
556 } else
557 irq = find_unbound_irq();
559 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
560 handle_level_irq, "pirq");
562 irq_op.irq = irq;
563 if (HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
564 dynamic_irq_cleanup(irq);
565 irq = -ENOSPC;
566 goto out;
569 irq_info[irq] = mk_pirq_info(0, gsi, irq_op.vector);
571 out:
572 spin_unlock(&irq_mapping_update_lock);
574 return irq;
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)
589 int irq;
591 spin_lock(&irq_mapping_update_lock);
593 irq = evtchn_to_irq[evtchn];
595 if (irq == -1) {
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);
607 return irq;
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;
614 int evtchn, irq;
616 spin_lock(&irq_mapping_update_lock);
618 irq = per_cpu(ipi_to_irq, cpu)[ipi];
620 if (irq == -1) {
621 irq = find_unbound_irq();
622 if (irq < 0)
623 goto out;
625 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
626 handle_percpu_irq, "ipi");
628 bind_ipi.vcpu = cpu;
629 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
630 &bind_ipi) != 0)
631 BUG();
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);
641 out:
642 spin_unlock(&irq_mapping_update_lock);
643 return irq;
647 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
649 struct evtchn_bind_virq bind_virq;
650 int evtchn, irq;
652 spin_lock(&irq_mapping_update_lock);
654 irq = per_cpu(virq_to_irq, cpu)[virq];
656 if (irq == -1) {
657 bind_virq.virq = virq;
658 bind_virq.vcpu = cpu;
659 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
660 &bind_virq) != 0)
661 BUG();
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);
679 return irq;
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)) {
690 close.port = evtchn;
691 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
692 BUG();
694 switch (type_from_irq(irq)) {
695 case IRQT_VIRQ:
696 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
697 [virq_from_irq(irq)] = -1;
698 break;
699 case IRQT_IPI:
700 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
701 [ipi_from_irq(irq)] = -1;
702 break;
703 default:
704 break;
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();
716 irq_free_desc(irq);
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)
727 unsigned int irq;
728 int retval;
730 irq = bind_evtchn_to_irq(evtchn);
731 retval = request_irq(irq, handler, irqflags, devname, dev_id);
732 if (retval != 0) {
733 unbind_from_irq(irq);
734 return retval;
737 return 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)
745 unsigned int irq;
746 int retval;
748 irq = bind_virq_to_irq(virq, cpu);
749 retval = request_irq(irq, handler, irqflags, devname, dev_id);
750 if (retval != 0) {
751 unbind_from_irq(irq);
752 return retval;
755 return irq;
757 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
759 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
760 unsigned int cpu,
761 irq_handler_t handler,
762 unsigned long irqflags,
763 const char *devname,
764 void *dev_id)
766 int irq, retval;
768 irq = bind_ipi_to_irq(ipi, cpu);
769 if (irq < 0)
770 return irq;
772 irqflags |= IRQF_NO_SUSPEND;
773 retval = request_irq(irq, handler, irqflags, devname, dev_id);
774 if (retval != 0) {
775 unbind_from_irq(irq);
776 return retval;
779 return 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];
792 BUG_ON(irq < 0);
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();
800 int i;
801 unsigned long flags;
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,
834 evtchn_to_irq[i]);
838 spin_unlock_irqrestore(&debug_lock, flags);
840 return IRQ_HANDLED;
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
848 * handling.
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)
856 int cpu = get_cpu();
857 struct shared_info *s = HYPERVISOR_shared_info;
858 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
859 unsigned count;
861 do {
862 unsigned long pending_words;
864 vcpu_info->evtchn_upcall_pending = 0;
866 if (__get_cpu_var(xed_nesting_count)++)
867 goto out;
869 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
870 /* Clear master flag /before/ clearing selector flag. */
871 wmb();
872 #endif
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;
885 if (irq != -1) {
886 desc = irq_to_desc(irq);
887 if (desc)
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);
899 out:
901 put_cpu();
904 void xen_evtchn_do_upcall(struct pt_regs *regs)
906 struct pt_regs *old_regs = set_irq_regs(regs);
908 exit_idle();
909 irq_enter();
911 __xen_evtchn_do_upcall();
913 irq_exit();
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. */
930 disable_irq(irq);
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. */
949 enable_irq(irq);
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))
962 return -1;
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);
976 return 0;
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))
992 return 1;
994 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
995 sync_set_bit(evtchn, s->evtchn_pending);
996 if (!masked)
997 unmask_evtchn(evtchn);
999 return 1;
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;
1032 int ret = 0;
1034 if (VALID_EVTCHN(evtchn)) {
1035 int masked;
1037 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1038 sync_set_bit(evtchn, sh->evtchn_pending);
1039 if (!masked)
1040 unmask_evtchn(evtchn);
1041 ret = 1;
1044 return ret;
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)
1054 continue;
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,
1062 &bind_virq) != 0)
1063 BUG();
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)
1083 continue;
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,
1090 &bind_ipi) != 0)
1091 BUG();
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))
1119 set_evtchn(evtchn);
1122 bool xen_test_irq_pending(int irq)
1124 int evtchn = evtchn_from_irq(irq);
1125 bool ret = false;
1127 if (VALID_EVTCHN(evtchn))
1128 ret = test_evtchn(evtchn);
1130 return ret;
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;
1142 poll.nr_ports = 1;
1143 poll.timeout = 0;
1144 set_xen_guest_handle(poll.ports, &evtchn);
1146 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1147 BUG();
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 = {
1175 .name = "xen-dyn",
1177 .disable = disable_dynirq,
1178 .mask = disable_dynirq,
1179 .unmask = enable_dynirq,
1181 .ack = ack_dynirq,
1182 .set_affinity = set_affinity_irq,
1183 .retrigger = retrigger_dynirq,
1186 static struct irq_chip xen_pirq_chip __read_mostly = {
1187 .name = "xen-pirq",
1189 .startup = startup_pirq,
1190 .shutdown = shutdown_pirq,
1192 .enable = enable_pirq,
1193 .unmask = enable_pirq,
1195 .disable = disable_pirq,
1196 .mask = disable_pirq,
1198 .ack = ack_pirq,
1199 .end = end_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,
1213 .ack = ack_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;
1221 a.value = via;
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)
1232 int rc;
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);
1237 if (rc) {
1238 printk(KERN_ERR "Request for Xen HVM callback vector"
1239 " failed.\n");
1240 xen_have_vector_callback = 0;
1241 return;
1243 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1244 "enabled\n");
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);
1250 #else
1251 void xen_callback_vector(void) {}
1252 #endif
1254 void __init xen_init_IRQ(void)
1256 int i;
1258 cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
1259 GFP_KERNEL);
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++)
1266 mask_evtchn(i);
1268 if (xen_hvm_domain()) {
1269 xen_callback_vector();
1270 native_init_IRQ();
1271 } else {
1272 irq_ctx_init(smp_processor_id());