PM / Suspend: Off by one in pm_suspend()
[linux/fpc-iii.git] / drivers / xen / events.c
blob009ca4e78df5868c7f904b959d7e96bc99e4b90c
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>
29 #include <linux/bootmem.h>
31 #include <asm/ptrace.h>
32 #include <asm/irq.h>
33 #include <asm/idle.h>
34 #include <asm/sync_bitops.h>
35 #include <asm/xen/hypercall.h>
36 #include <asm/xen/hypervisor.h>
38 #include <xen/xen-ops.h>
39 #include <xen/events.h>
40 #include <xen/interface/xen.h>
41 #include <xen/interface/event_channel.h>
44 * This lock protects updates to the following mapping and reference-count
45 * arrays. The lock does not need to be acquired to read the mapping tables.
47 static DEFINE_SPINLOCK(irq_mapping_update_lock);
49 /* IRQ <-> VIRQ mapping. */
50 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
52 /* IRQ <-> IPI mapping */
53 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
55 /* Interrupt types. */
56 enum xen_irq_type {
57 IRQT_UNBOUND = 0,
58 IRQT_PIRQ,
59 IRQT_VIRQ,
60 IRQT_IPI,
61 IRQT_EVTCHN
65 * Packed IRQ information:
66 * type - enum xen_irq_type
67 * event channel - irq->event channel mapping
68 * cpu - cpu this event channel is bound to
69 * index - type-specific information:
70 * PIRQ - vector, with MSB being "needs EIO"
71 * VIRQ - virq number
72 * IPI - IPI vector
73 * EVTCHN -
75 struct irq_info
77 enum xen_irq_type type; /* type */
78 unsigned short evtchn; /* event channel */
79 unsigned short cpu; /* cpu bound */
81 union {
82 unsigned short virq;
83 enum ipi_vector ipi;
84 struct {
85 unsigned short gsi;
86 unsigned short vector;
87 } pirq;
88 } u;
91 static struct irq_info irq_info[NR_IRQS];
93 static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
94 [0 ... NR_EVENT_CHANNELS-1] = -1
96 struct cpu_evtchn_s {
97 unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
99 static struct cpu_evtchn_s *cpu_evtchn_mask_p;
100 static inline unsigned long *cpu_evtchn_mask(int cpu)
102 return cpu_evtchn_mask_p[cpu].bits;
105 /* Xen will never allocate port zero for any purpose. */
106 #define VALID_EVTCHN(chn) ((chn) != 0)
108 static struct irq_chip xen_dynamic_chip;
109 static struct irq_chip xen_percpu_chip;
111 /* Constructor for packed IRQ information. */
112 static struct irq_info mk_unbound_info(void)
114 return (struct irq_info) { .type = IRQT_UNBOUND };
117 static struct irq_info mk_evtchn_info(unsigned short evtchn)
119 return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
120 .cpu = 0 };
123 static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
125 return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
126 .cpu = 0, .u.ipi = ipi };
129 static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
131 return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
132 .cpu = 0, .u.virq = virq };
135 static struct irq_info mk_pirq_info(unsigned short evtchn,
136 unsigned short gsi, unsigned short vector)
138 return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
139 .cpu = 0, .u.pirq = { .gsi = gsi, .vector = vector } };
143 * Accessors for packed IRQ information.
145 static struct irq_info *info_for_irq(unsigned irq)
147 return &irq_info[irq];
150 static unsigned int evtchn_from_irq(unsigned irq)
152 return info_for_irq(irq)->evtchn;
155 unsigned irq_from_evtchn(unsigned int evtchn)
157 return evtchn_to_irq[evtchn];
159 EXPORT_SYMBOL_GPL(irq_from_evtchn);
161 static enum ipi_vector ipi_from_irq(unsigned irq)
163 struct irq_info *info = info_for_irq(irq);
165 BUG_ON(info == NULL);
166 BUG_ON(info->type != IRQT_IPI);
168 return info->u.ipi;
171 static unsigned virq_from_irq(unsigned irq)
173 struct irq_info *info = info_for_irq(irq);
175 BUG_ON(info == NULL);
176 BUG_ON(info->type != IRQT_VIRQ);
178 return info->u.virq;
181 static unsigned gsi_from_irq(unsigned irq)
183 struct irq_info *info = info_for_irq(irq);
185 BUG_ON(info == NULL);
186 BUG_ON(info->type != IRQT_PIRQ);
188 return info->u.pirq.gsi;
191 static unsigned vector_from_irq(unsigned irq)
193 struct irq_info *info = info_for_irq(irq);
195 BUG_ON(info == NULL);
196 BUG_ON(info->type != IRQT_PIRQ);
198 return info->u.pirq.vector;
201 static enum xen_irq_type type_from_irq(unsigned irq)
203 return info_for_irq(irq)->type;
206 static unsigned cpu_from_irq(unsigned irq)
208 return info_for_irq(irq)->cpu;
211 static unsigned int cpu_from_evtchn(unsigned int evtchn)
213 int irq = evtchn_to_irq[evtchn];
214 unsigned ret = 0;
216 if (irq != -1)
217 ret = cpu_from_irq(irq);
219 return ret;
222 static inline unsigned long active_evtchns(unsigned int cpu,
223 struct shared_info *sh,
224 unsigned int idx)
226 return (sh->evtchn_pending[idx] &
227 cpu_evtchn_mask(cpu)[idx] &
228 ~sh->evtchn_mask[idx]);
231 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
233 int irq = evtchn_to_irq[chn];
235 BUG_ON(irq == -1);
236 #ifdef CONFIG_SMP
237 cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
238 #endif
240 __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
241 __set_bit(chn, cpu_evtchn_mask(cpu));
243 irq_info[irq].cpu = cpu;
246 static void init_evtchn_cpu_bindings(void)
248 #ifdef CONFIG_SMP
249 struct irq_desc *desc;
250 int i;
252 /* By default all event channels notify CPU#0. */
253 for_each_irq_desc(i, desc) {
254 cpumask_copy(desc->affinity, cpumask_of(0));
256 #endif
258 memset(cpu_evtchn_mask(0), ~0, sizeof(struct cpu_evtchn_s));
261 static inline void clear_evtchn(int port)
263 struct shared_info *s = HYPERVISOR_shared_info;
264 sync_clear_bit(port, &s->evtchn_pending[0]);
267 static inline void set_evtchn(int port)
269 struct shared_info *s = HYPERVISOR_shared_info;
270 sync_set_bit(port, &s->evtchn_pending[0]);
273 static inline int test_evtchn(int port)
275 struct shared_info *s = HYPERVISOR_shared_info;
276 return sync_test_bit(port, &s->evtchn_pending[0]);
281 * notify_remote_via_irq - send event to remote end of event channel via irq
282 * @irq: irq of event channel to send event to
284 * Unlike notify_remote_via_evtchn(), this is safe to use across
285 * save/restore. Notifications on a broken connection are silently
286 * dropped.
288 void notify_remote_via_irq(int irq)
290 int evtchn = evtchn_from_irq(irq);
292 if (VALID_EVTCHN(evtchn))
293 notify_remote_via_evtchn(evtchn);
295 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
297 static void mask_evtchn(int port)
299 struct shared_info *s = HYPERVISOR_shared_info;
300 sync_set_bit(port, &s->evtchn_mask[0]);
303 static void unmask_evtchn(int port)
305 struct shared_info *s = HYPERVISOR_shared_info;
306 unsigned int cpu = get_cpu();
308 BUG_ON(!irqs_disabled());
310 /* Slow path (hypercall) if this is a non-local port. */
311 if (unlikely(cpu != cpu_from_evtchn(port))) {
312 struct evtchn_unmask unmask = { .port = port };
313 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
314 } else {
315 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
317 sync_clear_bit(port, &s->evtchn_mask[0]);
320 * The following is basically the equivalent of
321 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
322 * the interrupt edge' if the channel is masked.
324 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
325 !sync_test_and_set_bit(port / BITS_PER_LONG,
326 &vcpu_info->evtchn_pending_sel))
327 vcpu_info->evtchn_upcall_pending = 1;
330 put_cpu();
333 static int find_unbound_irq(void)
335 int irq;
336 struct irq_desc *desc;
338 for (irq = 0; irq < nr_irqs; irq++)
339 if (irq_info[irq].type == IRQT_UNBOUND)
340 break;
342 if (irq == nr_irqs)
343 panic("No available IRQ to bind to: increase nr_irqs!\n");
345 desc = irq_to_desc_alloc_node(irq, 0);
346 if (WARN_ON(desc == NULL))
347 return -1;
349 dynamic_irq_init(irq);
351 return irq;
354 int bind_evtchn_to_irq(unsigned int evtchn)
356 int irq;
358 spin_lock(&irq_mapping_update_lock);
360 irq = evtchn_to_irq[evtchn];
362 if (irq == -1) {
363 irq = find_unbound_irq();
365 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
366 handle_edge_irq, "event");
368 evtchn_to_irq[evtchn] = irq;
369 irq_info[irq] = mk_evtchn_info(evtchn);
372 spin_unlock(&irq_mapping_update_lock);
374 return irq;
376 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
378 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
380 struct evtchn_bind_ipi bind_ipi;
381 int evtchn, irq;
383 spin_lock(&irq_mapping_update_lock);
385 irq = per_cpu(ipi_to_irq, cpu)[ipi];
387 if (irq == -1) {
388 irq = find_unbound_irq();
389 if (irq < 0)
390 goto out;
392 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
393 handle_percpu_irq, "ipi");
395 bind_ipi.vcpu = cpu;
396 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
397 &bind_ipi) != 0)
398 BUG();
399 evtchn = bind_ipi.port;
401 evtchn_to_irq[evtchn] = irq;
402 irq_info[irq] = mk_ipi_info(evtchn, ipi);
403 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
405 bind_evtchn_to_cpu(evtchn, cpu);
408 out:
409 spin_unlock(&irq_mapping_update_lock);
410 return irq;
414 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
416 struct evtchn_bind_virq bind_virq;
417 int evtchn, irq;
419 spin_lock(&irq_mapping_update_lock);
421 irq = per_cpu(virq_to_irq, cpu)[virq];
423 if (irq == -1) {
424 bind_virq.virq = virq;
425 bind_virq.vcpu = cpu;
426 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
427 &bind_virq) != 0)
428 BUG();
429 evtchn = bind_virq.port;
431 irq = find_unbound_irq();
433 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
434 handle_percpu_irq, "virq");
436 evtchn_to_irq[evtchn] = irq;
437 irq_info[irq] = mk_virq_info(evtchn, virq);
439 per_cpu(virq_to_irq, cpu)[virq] = irq;
441 bind_evtchn_to_cpu(evtchn, cpu);
444 spin_unlock(&irq_mapping_update_lock);
446 return irq;
449 static void unbind_from_irq(unsigned int irq)
451 struct evtchn_close close;
452 int evtchn = evtchn_from_irq(irq);
454 spin_lock(&irq_mapping_update_lock);
456 if (VALID_EVTCHN(evtchn)) {
457 close.port = evtchn;
458 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
459 BUG();
461 switch (type_from_irq(irq)) {
462 case IRQT_VIRQ:
463 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
464 [virq_from_irq(irq)] = -1;
465 break;
466 case IRQT_IPI:
467 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
468 [ipi_from_irq(irq)] = -1;
469 break;
470 default:
471 break;
474 /* Closed ports are implicitly re-bound to VCPU0. */
475 bind_evtchn_to_cpu(evtchn, 0);
477 evtchn_to_irq[evtchn] = -1;
480 if (irq_info[irq].type != IRQT_UNBOUND) {
481 irq_info[irq] = mk_unbound_info();
483 dynamic_irq_cleanup(irq);
486 spin_unlock(&irq_mapping_update_lock);
489 int bind_evtchn_to_irqhandler(unsigned int evtchn,
490 irq_handler_t handler,
491 unsigned long irqflags,
492 const char *devname, void *dev_id)
494 unsigned int irq;
495 int retval;
497 irq = bind_evtchn_to_irq(evtchn);
498 retval = request_irq(irq, handler, irqflags, devname, dev_id);
499 if (retval != 0) {
500 unbind_from_irq(irq);
501 return retval;
504 return irq;
506 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
508 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
509 irq_handler_t handler,
510 unsigned long irqflags, const char *devname, void *dev_id)
512 unsigned int irq;
513 int retval;
515 irq = bind_virq_to_irq(virq, cpu);
516 retval = request_irq(irq, handler, irqflags, devname, dev_id);
517 if (retval != 0) {
518 unbind_from_irq(irq);
519 return retval;
522 return irq;
524 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
526 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
527 unsigned int cpu,
528 irq_handler_t handler,
529 unsigned long irqflags,
530 const char *devname,
531 void *dev_id)
533 int irq, retval;
535 irq = bind_ipi_to_irq(ipi, cpu);
536 if (irq < 0)
537 return irq;
539 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
540 retval = request_irq(irq, handler, irqflags, devname, dev_id);
541 if (retval != 0) {
542 unbind_from_irq(irq);
543 return retval;
546 return irq;
549 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
551 free_irq(irq, dev_id);
552 unbind_from_irq(irq);
554 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
556 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
558 int irq = per_cpu(ipi_to_irq, cpu)[vector];
559 BUG_ON(irq < 0);
560 notify_remote_via_irq(irq);
563 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
565 struct shared_info *sh = HYPERVISOR_shared_info;
566 int cpu = smp_processor_id();
567 int i;
568 unsigned long flags;
569 static DEFINE_SPINLOCK(debug_lock);
571 spin_lock_irqsave(&debug_lock, flags);
573 printk("vcpu %d\n ", cpu);
575 for_each_online_cpu(i) {
576 struct vcpu_info *v = per_cpu(xen_vcpu, i);
577 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
578 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
579 v->evtchn_upcall_pending,
580 v->evtchn_pending_sel);
582 printk("pending:\n ");
583 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
584 printk("%08lx%s", sh->evtchn_pending[i],
585 i % 8 == 0 ? "\n " : " ");
586 printk("\nmasks:\n ");
587 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
588 printk("%08lx%s", sh->evtchn_mask[i],
589 i % 8 == 0 ? "\n " : " ");
591 printk("\nunmasked:\n ");
592 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
593 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
594 i % 8 == 0 ? "\n " : " ");
596 printk("\npending list:\n");
597 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
598 if (sync_test_bit(i, sh->evtchn_pending)) {
599 printk(" %d: event %d -> irq %d\n",
600 cpu_from_evtchn(i), i,
601 evtchn_to_irq[i]);
605 spin_unlock_irqrestore(&debug_lock, flags);
607 return IRQ_HANDLED;
610 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
613 * Search the CPUs pending events bitmasks. For each one found, map
614 * the event number to an irq, and feed it into do_IRQ() for
615 * handling.
617 * Xen uses a two-level bitmap to speed searching. The first level is
618 * a bitset of words which contain pending event bits. The second
619 * level is a bitset of pending events themselves.
621 void xen_evtchn_do_upcall(struct pt_regs *regs)
623 int cpu = get_cpu();
624 struct pt_regs *old_regs = set_irq_regs(regs);
625 struct shared_info *s = HYPERVISOR_shared_info;
626 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
627 unsigned count;
629 exit_idle();
630 irq_enter();
632 do {
633 unsigned long pending_words;
635 vcpu_info->evtchn_upcall_pending = 0;
637 if (__get_cpu_var(xed_nesting_count)++)
638 goto out;
640 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
641 /* Clear master flag /before/ clearing selector flag. */
642 wmb();
643 #endif
644 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
645 while (pending_words != 0) {
646 unsigned long pending_bits;
647 int word_idx = __ffs(pending_words);
648 pending_words &= ~(1UL << word_idx);
650 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
651 int bit_idx = __ffs(pending_bits);
652 int port = (word_idx * BITS_PER_LONG) + bit_idx;
653 int irq = evtchn_to_irq[port];
655 if (irq != -1)
656 handle_irq(irq, regs);
660 BUG_ON(!irqs_disabled());
662 count = __get_cpu_var(xed_nesting_count);
663 __get_cpu_var(xed_nesting_count) = 0;
664 } while(count != 1);
666 out:
667 irq_exit();
668 set_irq_regs(old_regs);
670 put_cpu();
673 /* Rebind a new event channel to an existing irq. */
674 void rebind_evtchn_irq(int evtchn, int irq)
676 struct irq_info *info = info_for_irq(irq);
678 /* Make sure the irq is masked, since the new event channel
679 will also be masked. */
680 disable_irq(irq);
682 spin_lock(&irq_mapping_update_lock);
684 /* After resume the irq<->evtchn mappings are all cleared out */
685 BUG_ON(evtchn_to_irq[evtchn] != -1);
686 /* Expect irq to have been bound before,
687 so there should be a proper type */
688 BUG_ON(info->type == IRQT_UNBOUND);
690 evtchn_to_irq[evtchn] = irq;
691 irq_info[irq] = mk_evtchn_info(evtchn);
693 spin_unlock(&irq_mapping_update_lock);
695 /* new event channels are always bound to cpu 0 */
696 irq_set_affinity(irq, cpumask_of(0));
698 /* Unmask the event channel. */
699 enable_irq(irq);
702 /* Rebind an evtchn so that it gets delivered to a specific cpu */
703 static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
705 struct evtchn_bind_vcpu bind_vcpu;
706 int evtchn = evtchn_from_irq(irq);
708 if (!VALID_EVTCHN(evtchn))
709 return -1;
711 /* Send future instances of this interrupt to other vcpu. */
712 bind_vcpu.port = evtchn;
713 bind_vcpu.vcpu = tcpu;
716 * If this fails, it usually just indicates that we're dealing with a
717 * virq or IPI channel, which don't actually need to be rebound. Ignore
718 * it, but don't do the xenlinux-level rebind in that case.
720 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
721 bind_evtchn_to_cpu(evtchn, tcpu);
723 return 0;
726 static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
728 unsigned tcpu = cpumask_first(dest);
730 return rebind_irq_to_cpu(irq, tcpu);
733 int resend_irq_on_evtchn(unsigned int irq)
735 int masked, evtchn = evtchn_from_irq(irq);
736 struct shared_info *s = HYPERVISOR_shared_info;
738 if (!VALID_EVTCHN(evtchn))
739 return 1;
741 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
742 sync_set_bit(evtchn, s->evtchn_pending);
743 if (!masked)
744 unmask_evtchn(evtchn);
746 return 1;
749 static void enable_dynirq(unsigned int irq)
751 int evtchn = evtchn_from_irq(irq);
753 if (VALID_EVTCHN(evtchn))
754 unmask_evtchn(evtchn);
757 static void disable_dynirq(unsigned int irq)
759 int evtchn = evtchn_from_irq(irq);
761 if (VALID_EVTCHN(evtchn))
762 mask_evtchn(evtchn);
765 static void ack_dynirq(unsigned int irq)
767 int evtchn = evtchn_from_irq(irq);
769 move_native_irq(irq);
771 if (VALID_EVTCHN(evtchn))
772 clear_evtchn(evtchn);
775 static int retrigger_dynirq(unsigned int irq)
777 int evtchn = evtchn_from_irq(irq);
778 struct shared_info *sh = HYPERVISOR_shared_info;
779 int ret = 0;
781 if (VALID_EVTCHN(evtchn)) {
782 int masked;
784 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
785 sync_set_bit(evtchn, sh->evtchn_pending);
786 if (!masked)
787 unmask_evtchn(evtchn);
788 ret = 1;
791 return ret;
794 static void restore_cpu_virqs(unsigned int cpu)
796 struct evtchn_bind_virq bind_virq;
797 int virq, irq, evtchn;
799 for (virq = 0; virq < NR_VIRQS; virq++) {
800 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
801 continue;
803 BUG_ON(virq_from_irq(irq) != virq);
805 /* Get a new binding from Xen. */
806 bind_virq.virq = virq;
807 bind_virq.vcpu = cpu;
808 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
809 &bind_virq) != 0)
810 BUG();
811 evtchn = bind_virq.port;
813 /* Record the new mapping. */
814 evtchn_to_irq[evtchn] = irq;
815 irq_info[irq] = mk_virq_info(evtchn, virq);
816 bind_evtchn_to_cpu(evtchn, cpu);
820 static void restore_cpu_ipis(unsigned int cpu)
822 struct evtchn_bind_ipi bind_ipi;
823 int ipi, irq, evtchn;
825 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
826 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
827 continue;
829 BUG_ON(ipi_from_irq(irq) != ipi);
831 /* Get a new binding from Xen. */
832 bind_ipi.vcpu = cpu;
833 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
834 &bind_ipi) != 0)
835 BUG();
836 evtchn = bind_ipi.port;
838 /* Record the new mapping. */
839 evtchn_to_irq[evtchn] = irq;
840 irq_info[irq] = mk_ipi_info(evtchn, ipi);
841 bind_evtchn_to_cpu(evtchn, cpu);
845 /* Clear an irq's pending state, in preparation for polling on it */
846 void xen_clear_irq_pending(int irq)
848 int evtchn = evtchn_from_irq(irq);
850 if (VALID_EVTCHN(evtchn))
851 clear_evtchn(evtchn);
854 void xen_set_irq_pending(int irq)
856 int evtchn = evtchn_from_irq(irq);
858 if (VALID_EVTCHN(evtchn))
859 set_evtchn(evtchn);
862 bool xen_test_irq_pending(int irq)
864 int evtchn = evtchn_from_irq(irq);
865 bool ret = false;
867 if (VALID_EVTCHN(evtchn))
868 ret = test_evtchn(evtchn);
870 return ret;
873 /* Poll waiting for an irq to become pending. In the usual case, the
874 irq will be disabled so it won't deliver an interrupt. */
875 void xen_poll_irq(int irq)
877 evtchn_port_t evtchn = evtchn_from_irq(irq);
879 if (VALID_EVTCHN(evtchn)) {
880 struct sched_poll poll;
882 poll.nr_ports = 1;
883 poll.timeout = 0;
884 set_xen_guest_handle(poll.ports, &evtchn);
886 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
887 BUG();
891 void xen_irq_resume(void)
893 unsigned int cpu, irq, evtchn;
895 init_evtchn_cpu_bindings();
897 /* New event-channel space is not 'live' yet. */
898 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
899 mask_evtchn(evtchn);
901 /* No IRQ <-> event-channel mappings. */
902 for (irq = 0; irq < nr_irqs; irq++)
903 irq_info[irq].evtchn = 0; /* zap event-channel binding */
905 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
906 evtchn_to_irq[evtchn] = -1;
908 for_each_possible_cpu(cpu) {
909 restore_cpu_virqs(cpu);
910 restore_cpu_ipis(cpu);
914 static struct irq_chip xen_dynamic_chip __read_mostly = {
915 .name = "xen-dyn",
917 .disable = disable_dynirq,
918 .mask = disable_dynirq,
919 .unmask = enable_dynirq,
921 .ack = ack_dynirq,
922 .set_affinity = set_affinity_irq,
923 .retrigger = retrigger_dynirq,
926 static struct irq_chip xen_percpu_chip __read_mostly = {
927 .name = "xen-percpu",
929 .disable = disable_dynirq,
930 .mask = disable_dynirq,
931 .unmask = enable_dynirq,
933 .ack = ack_dynirq,
936 void __init xen_init_IRQ(void)
938 int i;
940 cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
941 GFP_KERNEL);
942 BUG_ON(cpu_evtchn_mask_p == NULL);
944 init_evtchn_cpu_bindings();
946 /* No event channels are 'live' right now. */
947 for (i = 0; i < NR_EVENT_CHANNELS; i++)
948 mask_evtchn(i);
950 irq_ctx_init(smp_processor_id());