]> rtime.felk.cvut.cz Git - linux-imx.git/blob - drivers/xen/events.c
076910891448f205bb9c3cce1a9b3b3538eca0a5
[linux-imx.git] / drivers / xen / events.c
1 /*
2  * Xen event channels
3  *
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.
10  *
11  * There are four kinds of events which can be mapped to an event
12  * channel:
13  *
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.
20  *
21  * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
22  */
23
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>
31
32 #include <asm/ptrace.h>
33 #include <asm/irq.h>
34 #include <asm/idle.h>
35 #include <asm/sync_bitops.h>
36 #include <asm/xen/hypercall.h>
37 #include <asm/xen/hypervisor.h>
38
39 #include <xen/xen-ops.h>
40 #include <xen/events.h>
41 #include <xen/interface/xen.h>
42 #include <xen/interface/event_channel.h>
43
44 /*
45  * This lock protects updates to the following mapping and reference-count
46  * arrays. The lock does not need to be acquired to read the mapping tables.
47  */
48 static DEFINE_SPINLOCK(irq_mapping_update_lock);
49
50 /* IRQ <-> VIRQ mapping. */
51 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
52
53 /* IRQ <-> IPI mapping */
54 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
55
56 /* Interrupt types. */
57 enum xen_irq_type {
58         IRQT_UNBOUND = 0,
59         IRQT_PIRQ,
60         IRQT_VIRQ,
61         IRQT_IPI,
62         IRQT_EVTCHN
63 };
64
65 /*
66  * Packed IRQ information:
67  * type - enum xen_irq_type
68  * event channel - irq->event channel mapping
69  * cpu - cpu this event channel is bound to
70  * index - type-specific information:
71  *    PIRQ - vector, with MSB being "needs EIO"
72  *    VIRQ - virq number
73  *    IPI - IPI vector
74  *    EVTCHN -
75  */
76 struct irq_info
77 {
78         enum xen_irq_type type; /* type */
79         unsigned short evtchn;  /* event channel */
80         unsigned short cpu;     /* cpu bound */
81
82         union {
83                 unsigned short virq;
84                 enum ipi_vector ipi;
85                 struct {
86                         unsigned short gsi;
87                         unsigned short vector;
88                 } pirq;
89         } u;
90 };
91
92 static struct irq_info irq_info[NR_IRQS];
93
94 static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
95         [0 ... NR_EVENT_CHANNELS-1] = -1
96 };
97 struct cpu_evtchn_s {
98         unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
99 };
100 static struct cpu_evtchn_s *cpu_evtchn_mask_p;
101 static inline unsigned long *cpu_evtchn_mask(int cpu)
102 {
103         return cpu_evtchn_mask_p[cpu].bits;
104 }
105
106 /* Xen will never allocate port zero for any purpose. */
107 #define VALID_EVTCHN(chn)       ((chn) != 0)
108
109 static struct irq_chip xen_dynamic_chip;
110 static struct irq_chip xen_percpu_chip;
111
112 /* Constructor for packed IRQ information. */
113 static struct irq_info mk_unbound_info(void)
114 {
115         return (struct irq_info) { .type = IRQT_UNBOUND };
116 }
117
118 static struct irq_info mk_evtchn_info(unsigned short evtchn)
119 {
120         return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
121                         .cpu = 0 };
122 }
123
124 static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
125 {
126         return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
127                         .cpu = 0, .u.ipi = ipi };
128 }
129
130 static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
131 {
132         return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
133                         .cpu = 0, .u.virq = virq };
134 }
135
136 static struct irq_info mk_pirq_info(unsigned short evtchn,
137                                     unsigned short gsi, unsigned short vector)
138 {
139         return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
140                         .cpu = 0, .u.pirq = { .gsi = gsi, .vector = vector } };
141 }
142
143 /*
144  * Accessors for packed IRQ information.
145  */
146 static struct irq_info *info_for_irq(unsigned irq)
147 {
148         return &irq_info[irq];
149 }
150
151 static unsigned int evtchn_from_irq(unsigned irq)
152 {
153         return info_for_irq(irq)->evtchn;
154 }
155
156 unsigned irq_from_evtchn(unsigned int evtchn)
157 {
158         return evtchn_to_irq[evtchn];
159 }
160 EXPORT_SYMBOL_GPL(irq_from_evtchn);
161
162 static enum ipi_vector ipi_from_irq(unsigned irq)
163 {
164         struct irq_info *info = info_for_irq(irq);
165
166         BUG_ON(info == NULL);
167         BUG_ON(info->type != IRQT_IPI);
168
169         return info->u.ipi;
170 }
171
172 static unsigned virq_from_irq(unsigned irq)
173 {
174         struct irq_info *info = info_for_irq(irq);
175
176         BUG_ON(info == NULL);
177         BUG_ON(info->type != IRQT_VIRQ);
178
179         return info->u.virq;
180 }
181
182 static unsigned gsi_from_irq(unsigned irq)
183 {
184         struct irq_info *info = info_for_irq(irq);
185
186         BUG_ON(info == NULL);
187         BUG_ON(info->type != IRQT_PIRQ);
188
189         return info->u.pirq.gsi;
190 }
191
192 static unsigned vector_from_irq(unsigned irq)
193 {
194         struct irq_info *info = info_for_irq(irq);
195
196         BUG_ON(info == NULL);
197         BUG_ON(info->type != IRQT_PIRQ);
198
199         return info->u.pirq.vector;
200 }
201
202 static enum xen_irq_type type_from_irq(unsigned irq)
203 {
204         return info_for_irq(irq)->type;
205 }
206
207 static unsigned cpu_from_irq(unsigned irq)
208 {
209         return info_for_irq(irq)->cpu;
210 }
211
212 static unsigned int cpu_from_evtchn(unsigned int evtchn)
213 {
214         int irq = evtchn_to_irq[evtchn];
215         unsigned ret = 0;
216
217         if (irq != -1)
218                 ret = cpu_from_irq(irq);
219
220         return ret;
221 }
222
223 static inline unsigned long active_evtchns(unsigned int cpu,
224                                            struct shared_info *sh,
225                                            unsigned int idx)
226 {
227         return (sh->evtchn_pending[idx] &
228                 cpu_evtchn_mask(cpu)[idx] &
229                 ~sh->evtchn_mask[idx]);
230 }
231
232 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
233 {
234         int irq = evtchn_to_irq[chn];
235
236         BUG_ON(irq == -1);
237 #ifdef CONFIG_SMP
238         cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
239 #endif
240
241         __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
242         __set_bit(chn, cpu_evtchn_mask(cpu));
243
244         irq_info[irq].cpu = cpu;
245 }
246
247 static void init_evtchn_cpu_bindings(void)
248 {
249 #ifdef CONFIG_SMP
250         struct irq_desc *desc;
251         int i;
252
253         /* By default all event channels notify CPU#0. */
254         for_each_irq_desc(i, desc) {
255                 cpumask_copy(desc->affinity, cpumask_of(0));
256         }
257 #endif
258
259         memset(cpu_evtchn_mask(0), ~0, sizeof(struct cpu_evtchn_s));
260 }
261
262 static inline void clear_evtchn(int port)
263 {
264         struct shared_info *s = HYPERVISOR_shared_info;
265         sync_clear_bit(port, &s->evtchn_pending[0]);
266 }
267
268 static inline void set_evtchn(int port)
269 {
270         struct shared_info *s = HYPERVISOR_shared_info;
271         sync_set_bit(port, &s->evtchn_pending[0]);
272 }
273
274 static inline int test_evtchn(int port)
275 {
276         struct shared_info *s = HYPERVISOR_shared_info;
277         return sync_test_bit(port, &s->evtchn_pending[0]);
278 }
279
280
281 /**
282  * notify_remote_via_irq - send event to remote end of event channel via irq
283  * @irq: irq of event channel to send event to
284  *
285  * Unlike notify_remote_via_evtchn(), this is safe to use across
286  * save/restore. Notifications on a broken connection are silently
287  * dropped.
288  */
289 void notify_remote_via_irq(int irq)
290 {
291         int evtchn = evtchn_from_irq(irq);
292
293         if (VALID_EVTCHN(evtchn))
294                 notify_remote_via_evtchn(evtchn);
295 }
296 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
297
298 static void mask_evtchn(int port)
299 {
300         struct shared_info *s = HYPERVISOR_shared_info;
301         sync_set_bit(port, &s->evtchn_mask[0]);
302 }
303
304 static void unmask_evtchn(int port)
305 {
306         struct shared_info *s = HYPERVISOR_shared_info;
307         unsigned int cpu = get_cpu();
308
309         BUG_ON(!irqs_disabled());
310
311         /* Slow path (hypercall) if this is a non-local port. */
312         if (unlikely(cpu != cpu_from_evtchn(port))) {
313                 struct evtchn_unmask unmask = { .port = port };
314                 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
315         } else {
316                 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
317
318                 sync_clear_bit(port, &s->evtchn_mask[0]);
319
320                 /*
321                  * The following is basically the equivalent of
322                  * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
323                  * the interrupt edge' if the channel is masked.
324                  */
325                 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
326                     !sync_test_and_set_bit(port / BITS_PER_LONG,
327                                            &vcpu_info->evtchn_pending_sel))
328                         vcpu_info->evtchn_upcall_pending = 1;
329         }
330
331         put_cpu();
332 }
333
334 static int find_unbound_irq(void)
335 {
336         int irq;
337         struct irq_desc *desc;
338
339         for (irq = 0; irq < nr_irqs; irq++)
340                 if (irq_info[irq].type == IRQT_UNBOUND)
341                         break;
342
343         if (irq == nr_irqs)
344                 panic("No available IRQ to bind to: increase nr_irqs!\n");
345
346         desc = irq_to_desc_alloc_node(irq, 0);
347         if (WARN_ON(desc == NULL))
348                 return -1;
349
350         dynamic_irq_init(irq);
351
352         return irq;
353 }
354
355 int bind_evtchn_to_irq(unsigned int evtchn)
356 {
357         int irq;
358
359         spin_lock(&irq_mapping_update_lock);
360
361         irq = evtchn_to_irq[evtchn];
362
363         if (irq == -1) {
364                 irq = find_unbound_irq();
365
366                 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
367                                               handle_edge_irq, "event");
368
369                 evtchn_to_irq[evtchn] = irq;
370                 irq_info[irq] = mk_evtchn_info(evtchn);
371         }
372
373         spin_unlock(&irq_mapping_update_lock);
374
375         return irq;
376 }
377 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
378
379 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
380 {
381         struct evtchn_bind_ipi bind_ipi;
382         int evtchn, irq;
383
384         spin_lock(&irq_mapping_update_lock);
385
386         irq = per_cpu(ipi_to_irq, cpu)[ipi];
387
388         if (irq == -1) {
389                 irq = find_unbound_irq();
390                 if (irq < 0)
391                         goto out;
392
393                 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
394                                               handle_percpu_irq, "ipi");
395
396                 bind_ipi.vcpu = cpu;
397                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
398                                                 &bind_ipi) != 0)
399                         BUG();
400                 evtchn = bind_ipi.port;
401
402                 evtchn_to_irq[evtchn] = irq;
403                 irq_info[irq] = mk_ipi_info(evtchn, ipi);
404                 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
405
406                 bind_evtchn_to_cpu(evtchn, cpu);
407         }
408
409  out:
410         spin_unlock(&irq_mapping_update_lock);
411         return irq;
412 }
413
414
415 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
416 {
417         struct evtchn_bind_virq bind_virq;
418         int evtchn, irq;
419
420         spin_lock(&irq_mapping_update_lock);
421
422         irq = per_cpu(virq_to_irq, cpu)[virq];
423
424         if (irq == -1) {
425                 bind_virq.virq = virq;
426                 bind_virq.vcpu = cpu;
427                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
428                                                 &bind_virq) != 0)
429                         BUG();
430                 evtchn = bind_virq.port;
431
432                 irq = find_unbound_irq();
433
434                 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
435                                               handle_percpu_irq, "virq");
436
437                 evtchn_to_irq[evtchn] = irq;
438                 irq_info[irq] = mk_virq_info(evtchn, virq);
439
440                 per_cpu(virq_to_irq, cpu)[virq] = irq;
441
442                 bind_evtchn_to_cpu(evtchn, cpu);
443         }
444
445         spin_unlock(&irq_mapping_update_lock);
446
447         return irq;
448 }
449
450 static void unbind_from_irq(unsigned int irq)
451 {
452         struct evtchn_close close;
453         int evtchn = evtchn_from_irq(irq);
454
455         spin_lock(&irq_mapping_update_lock);
456
457         if (VALID_EVTCHN(evtchn)) {
458                 close.port = evtchn;
459                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
460                         BUG();
461
462                 switch (type_from_irq(irq)) {
463                 case IRQT_VIRQ:
464                         per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
465                                 [virq_from_irq(irq)] = -1;
466                         break;
467                 case IRQT_IPI:
468                         per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
469                                 [ipi_from_irq(irq)] = -1;
470                         break;
471                 default:
472                         break;
473                 }
474
475                 /* Closed ports are implicitly re-bound to VCPU0. */
476                 bind_evtchn_to_cpu(evtchn, 0);
477
478                 evtchn_to_irq[evtchn] = -1;
479         }
480
481         if (irq_info[irq].type != IRQT_UNBOUND) {
482                 irq_info[irq] = mk_unbound_info();
483
484                 dynamic_irq_cleanup(irq);
485         }
486
487         spin_unlock(&irq_mapping_update_lock);
488 }
489
490 int bind_evtchn_to_irqhandler(unsigned int evtchn,
491                               irq_handler_t handler,
492                               unsigned long irqflags,
493                               const char *devname, void *dev_id)
494 {
495         unsigned int irq;
496         int retval;
497
498         irq = bind_evtchn_to_irq(evtchn);
499         retval = request_irq(irq, handler, irqflags, devname, dev_id);
500         if (retval != 0) {
501                 unbind_from_irq(irq);
502                 return retval;
503         }
504
505         return irq;
506 }
507 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
508
509 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
510                             irq_handler_t handler,
511                             unsigned long irqflags, const char *devname, void *dev_id)
512 {
513         unsigned int irq;
514         int retval;
515
516         irq = bind_virq_to_irq(virq, cpu);
517         retval = request_irq(irq, handler, irqflags, devname, dev_id);
518         if (retval != 0) {
519                 unbind_from_irq(irq);
520                 return retval;
521         }
522
523         return irq;
524 }
525 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
526
527 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
528                            unsigned int cpu,
529                            irq_handler_t handler,
530                            unsigned long irqflags,
531                            const char *devname,
532                            void *dev_id)
533 {
534         int irq, retval;
535
536         irq = bind_ipi_to_irq(ipi, cpu);
537         if (irq < 0)
538                 return irq;
539
540         irqflags |= IRQF_NO_SUSPEND;
541         retval = request_irq(irq, handler, irqflags, devname, dev_id);
542         if (retval != 0) {
543                 unbind_from_irq(irq);
544                 return retval;
545         }
546
547         return irq;
548 }
549
550 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
551 {
552         free_irq(irq, dev_id);
553         unbind_from_irq(irq);
554 }
555 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
556
557 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
558 {
559         int irq = per_cpu(ipi_to_irq, cpu)[vector];
560         BUG_ON(irq < 0);
561         notify_remote_via_irq(irq);
562 }
563
564 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
565 {
566         struct shared_info *sh = HYPERVISOR_shared_info;
567         int cpu = smp_processor_id();
568         int i;
569         unsigned long flags;
570         static DEFINE_SPINLOCK(debug_lock);
571
572         spin_lock_irqsave(&debug_lock, flags);
573
574         printk("vcpu %d\n  ", cpu);
575
576         for_each_online_cpu(i) {
577                 struct vcpu_info *v = per_cpu(xen_vcpu, i);
578                 printk("%d: masked=%d pending=%d event_sel %08lx\n  ", i,
579                         (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
580                         v->evtchn_upcall_pending,
581                         v->evtchn_pending_sel);
582         }
583         printk("pending:\n   ");
584         for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
585                 printk("%08lx%s", sh->evtchn_pending[i],
586                         i % 8 == 0 ? "\n   " : " ");
587         printk("\nmasks:\n   ");
588         for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
589                 printk("%08lx%s", sh->evtchn_mask[i],
590                         i % 8 == 0 ? "\n   " : " ");
591
592         printk("\nunmasked:\n   ");
593         for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
594                 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
595                         i % 8 == 0 ? "\n   " : " ");
596
597         printk("\npending list:\n");
598         for(i = 0; i < NR_EVENT_CHANNELS; i++) {
599                 if (sync_test_bit(i, sh->evtchn_pending)) {
600                         printk("  %d: event %d -> irq %d\n",
601                                cpu_from_evtchn(i), i,
602                                evtchn_to_irq[i]);
603                 }
604         }
605
606         spin_unlock_irqrestore(&debug_lock, flags);
607
608         return IRQ_HANDLED;
609 }
610
611 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
612
613 /*
614  * Search the CPUs pending events bitmasks.  For each one found, map
615  * the event number to an irq, and feed it into do_IRQ() for
616  * handling.
617  *
618  * Xen uses a two-level bitmap to speed searching.  The first level is
619  * a bitset of words which contain pending event bits.  The second
620  * level is a bitset of pending events themselves.
621  */
622 void xen_evtchn_do_upcall(struct pt_regs *regs)
623 {
624         int cpu = get_cpu();
625         struct pt_regs *old_regs = set_irq_regs(regs);
626         struct shared_info *s = HYPERVISOR_shared_info;
627         struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
628         unsigned count;
629
630         exit_idle();
631         irq_enter();
632
633         do {
634                 unsigned long pending_words;
635
636                 vcpu_info->evtchn_upcall_pending = 0;
637
638                 if (__get_cpu_var(xed_nesting_count)++)
639                         goto out;
640
641 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
642                 /* Clear master flag /before/ clearing selector flag. */
643                 wmb();
644 #endif
645                 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
646                 while (pending_words != 0) {
647                         unsigned long pending_bits;
648                         int word_idx = __ffs(pending_words);
649                         pending_words &= ~(1UL << word_idx);
650
651                         while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
652                                 int bit_idx = __ffs(pending_bits);
653                                 int port = (word_idx * BITS_PER_LONG) + bit_idx;
654                                 int irq = evtchn_to_irq[port];
655                                 struct irq_desc *desc;
656
657                                 if (irq != -1) {
658                                         desc = irq_to_desc(irq);
659                                         if (desc)
660                                                 generic_handle_irq_desc(irq, desc);
661                                 }
662                         }
663                 }
664
665                 BUG_ON(!irqs_disabled());
666
667                 count = __get_cpu_var(xed_nesting_count);
668                 __get_cpu_var(xed_nesting_count) = 0;
669         } while(count != 1);
670
671 out:
672         irq_exit();
673         set_irq_regs(old_regs);
674
675         put_cpu();
676 }
677
678 /* Rebind a new event channel to an existing irq. */
679 void rebind_evtchn_irq(int evtchn, int irq)
680 {
681         struct irq_info *info = info_for_irq(irq);
682
683         /* Make sure the irq is masked, since the new event channel
684            will also be masked. */
685         disable_irq(irq);
686
687         spin_lock(&irq_mapping_update_lock);
688
689         /* After resume the irq<->evtchn mappings are all cleared out */
690         BUG_ON(evtchn_to_irq[evtchn] != -1);
691         /* Expect irq to have been bound before,
692            so there should be a proper type */
693         BUG_ON(info->type == IRQT_UNBOUND);
694
695         evtchn_to_irq[evtchn] = irq;
696         irq_info[irq] = mk_evtchn_info(evtchn);
697
698         spin_unlock(&irq_mapping_update_lock);
699
700         /* new event channels are always bound to cpu 0 */
701         irq_set_affinity(irq, cpumask_of(0));
702
703         /* Unmask the event channel. */
704         enable_irq(irq);
705 }
706
707 /* Rebind an evtchn so that it gets delivered to a specific cpu */
708 static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
709 {
710         struct evtchn_bind_vcpu bind_vcpu;
711         int evtchn = evtchn_from_irq(irq);
712
713         if (!VALID_EVTCHN(evtchn))
714                 return -1;
715
716         /* Send future instances of this interrupt to other vcpu. */
717         bind_vcpu.port = evtchn;
718         bind_vcpu.vcpu = tcpu;
719
720         /*
721          * If this fails, it usually just indicates that we're dealing with a
722          * virq or IPI channel, which don't actually need to be rebound. Ignore
723          * it, but don't do the xenlinux-level rebind in that case.
724          */
725         if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
726                 bind_evtchn_to_cpu(evtchn, tcpu);
727
728         return 0;
729 }
730
731 static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
732 {
733         unsigned tcpu = cpumask_first(dest);
734
735         return rebind_irq_to_cpu(irq, tcpu);
736 }
737
738 int resend_irq_on_evtchn(unsigned int irq)
739 {
740         int masked, evtchn = evtchn_from_irq(irq);
741         struct shared_info *s = HYPERVISOR_shared_info;
742
743         if (!VALID_EVTCHN(evtchn))
744                 return 1;
745
746         masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
747         sync_set_bit(evtchn, s->evtchn_pending);
748         if (!masked)
749                 unmask_evtchn(evtchn);
750
751         return 1;
752 }
753
754 static void enable_dynirq(unsigned int irq)
755 {
756         int evtchn = evtchn_from_irq(irq);
757
758         if (VALID_EVTCHN(evtchn))
759                 unmask_evtchn(evtchn);
760 }
761
762 static void disable_dynirq(unsigned int irq)
763 {
764         int evtchn = evtchn_from_irq(irq);
765
766         if (VALID_EVTCHN(evtchn))
767                 mask_evtchn(evtchn);
768 }
769
770 static void ack_dynirq(unsigned int irq)
771 {
772         int evtchn = evtchn_from_irq(irq);
773
774         move_native_irq(irq);
775
776         if (VALID_EVTCHN(evtchn))
777                 clear_evtchn(evtchn);
778 }
779
780 static int retrigger_dynirq(unsigned int irq)
781 {
782         int evtchn = evtchn_from_irq(irq);
783         struct shared_info *sh = HYPERVISOR_shared_info;
784         int ret = 0;
785
786         if (VALID_EVTCHN(evtchn)) {
787                 int masked;
788
789                 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
790                 sync_set_bit(evtchn, sh->evtchn_pending);
791                 if (!masked)
792                         unmask_evtchn(evtchn);
793                 ret = 1;
794         }
795
796         return ret;
797 }
798
799 static void restore_cpu_virqs(unsigned int cpu)
800 {
801         struct evtchn_bind_virq bind_virq;
802         int virq, irq, evtchn;
803
804         for (virq = 0; virq < NR_VIRQS; virq++) {
805                 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
806                         continue;
807
808                 BUG_ON(virq_from_irq(irq) != virq);
809
810                 /* Get a new binding from Xen. */
811                 bind_virq.virq = virq;
812                 bind_virq.vcpu = cpu;
813                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
814                                                 &bind_virq) != 0)
815                         BUG();
816                 evtchn = bind_virq.port;
817
818                 /* Record the new mapping. */
819                 evtchn_to_irq[evtchn] = irq;
820                 irq_info[irq] = mk_virq_info(evtchn, virq);
821                 bind_evtchn_to_cpu(evtchn, cpu);
822
823                 /* Ready for use. */
824                 unmask_evtchn(evtchn);
825         }
826 }
827
828 static void restore_cpu_ipis(unsigned int cpu)
829 {
830         struct evtchn_bind_ipi bind_ipi;
831         int ipi, irq, evtchn;
832
833         for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
834                 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
835                         continue;
836
837                 BUG_ON(ipi_from_irq(irq) != ipi);
838
839                 /* Get a new binding from Xen. */
840                 bind_ipi.vcpu = cpu;
841                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
842                                                 &bind_ipi) != 0)
843                         BUG();
844                 evtchn = bind_ipi.port;
845
846                 /* Record the new mapping. */
847                 evtchn_to_irq[evtchn] = irq;
848                 irq_info[irq] = mk_ipi_info(evtchn, ipi);
849                 bind_evtchn_to_cpu(evtchn, cpu);
850
851                 /* Ready for use. */
852                 unmask_evtchn(evtchn);
853
854         }
855 }
856
857 /* Clear an irq's pending state, in preparation for polling on it */
858 void xen_clear_irq_pending(int irq)
859 {
860         int evtchn = evtchn_from_irq(irq);
861
862         if (VALID_EVTCHN(evtchn))
863                 clear_evtchn(evtchn);
864 }
865
866 void xen_set_irq_pending(int irq)
867 {
868         int evtchn = evtchn_from_irq(irq);
869
870         if (VALID_EVTCHN(evtchn))
871                 set_evtchn(evtchn);
872 }
873
874 bool xen_test_irq_pending(int irq)
875 {
876         int evtchn = evtchn_from_irq(irq);
877         bool ret = false;
878
879         if (VALID_EVTCHN(evtchn))
880                 ret = test_evtchn(evtchn);
881
882         return ret;
883 }
884
885 /* Poll waiting for an irq to become pending.  In the usual case, the
886    irq will be disabled so it won't deliver an interrupt. */
887 void xen_poll_irq(int irq)
888 {
889         evtchn_port_t evtchn = evtchn_from_irq(irq);
890
891         if (VALID_EVTCHN(evtchn)) {
892                 struct sched_poll poll;
893
894                 poll.nr_ports = 1;
895                 poll.timeout = 0;
896                 set_xen_guest_handle(poll.ports, &evtchn);
897
898                 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
899                         BUG();
900         }
901 }
902
903 void xen_irq_resume(void)
904 {
905         unsigned int cpu, irq, evtchn;
906
907         init_evtchn_cpu_bindings();
908
909         /* New event-channel space is not 'live' yet. */
910         for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
911                 mask_evtchn(evtchn);
912
913         /* No IRQ <-> event-channel mappings. */
914         for (irq = 0; irq < nr_irqs; irq++)
915                 irq_info[irq].evtchn = 0; /* zap event-channel binding */
916
917         for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
918                 evtchn_to_irq[evtchn] = -1;
919
920         for_each_possible_cpu(cpu) {
921                 restore_cpu_virqs(cpu);
922                 restore_cpu_ipis(cpu);
923         }
924 }
925
926 static struct irq_chip xen_dynamic_chip __read_mostly = {
927         .name           = "xen-dyn",
928
929         .disable        = disable_dynirq,
930         .mask           = disable_dynirq,
931         .unmask         = enable_dynirq,
932
933         .ack            = ack_dynirq,
934         .set_affinity   = set_affinity_irq,
935         .retrigger      = retrigger_dynirq,
936 };
937
938 static struct irq_chip xen_percpu_chip __read_mostly = {
939         .name           = "xen-percpu",
940
941         .disable        = disable_dynirq,
942         .mask           = disable_dynirq,
943         .unmask         = enable_dynirq,
944
945         .ack            = ack_dynirq,
946 };
947
948 void __init xen_init_IRQ(void)
949 {
950         int i;
951
952         cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
953                                     GFP_KERNEL);
954         BUG_ON(cpu_evtchn_mask_p == NULL);
955
956         init_evtchn_cpu_bindings();
957
958         /* No event channels are 'live' right now. */
959         for (i = 0; i < NR_EVENT_CHANNELS; i++)
960                 mask_evtchn(i);
961
962         irq_ctx_init(smp_processor_id());
963 }