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