]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/xen/events.c
bna: add missing iounmap() on error in bnad_init()
[karo-tx-linux.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 received, 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. PIRQs - Hardware interrupts.
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 #include <linux/irqnr.h>
32 #include <linux/pci.h>
33
34 #ifdef CONFIG_X86
35 #include <asm/desc.h>
36 #include <asm/ptrace.h>
37 #include <asm/irq.h>
38 #include <asm/idle.h>
39 #include <asm/io_apic.h>
40 #include <asm/xen/page.h>
41 #include <asm/xen/pci.h>
42 #endif
43 #include <asm/sync_bitops.h>
44 #include <asm/xen/hypercall.h>
45 #include <asm/xen/hypervisor.h>
46
47 #include <xen/xen.h>
48 #include <xen/hvm.h>
49 #include <xen/xen-ops.h>
50 #include <xen/events.h>
51 #include <xen/interface/xen.h>
52 #include <xen/interface/event_channel.h>
53 #include <xen/interface/hvm/hvm_op.h>
54 #include <xen/interface/hvm/params.h>
55 #include <xen/interface/physdev.h>
56 #include <xen/interface/sched.h>
57 #include <asm/hw_irq.h>
58
59 /*
60  * This lock protects updates to the following mapping and reference-count
61  * arrays. The lock does not need to be acquired to read the mapping tables.
62  */
63 static DEFINE_MUTEX(irq_mapping_update_lock);
64
65 static LIST_HEAD(xen_irq_list_head);
66
67 /* IRQ <-> VIRQ mapping. */
68 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
69
70 /* IRQ <-> IPI mapping */
71 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
72
73 /* Interrupt types. */
74 enum xen_irq_type {
75         IRQT_UNBOUND = 0,
76         IRQT_PIRQ,
77         IRQT_VIRQ,
78         IRQT_IPI,
79         IRQT_EVTCHN
80 };
81
82 /*
83  * Packed IRQ information:
84  * type - enum xen_irq_type
85  * event channel - irq->event channel mapping
86  * cpu - cpu this event channel is bound to
87  * index - type-specific information:
88  *    PIRQ - physical IRQ, GSI, flags, and owner domain
89  *    VIRQ - virq number
90  *    IPI - IPI vector
91  *    EVTCHN -
92  */
93 struct irq_info {
94         struct list_head list;
95         int refcnt;
96         enum xen_irq_type type; /* type */
97         unsigned irq;
98         unsigned short evtchn;  /* event channel */
99         unsigned short cpu;     /* cpu bound */
100
101         union {
102                 unsigned short virq;
103                 enum ipi_vector ipi;
104                 struct {
105                         unsigned short pirq;
106                         unsigned short gsi;
107                         unsigned char flags;
108                         uint16_t domid;
109                 } pirq;
110         } u;
111 };
112 #define PIRQ_NEEDS_EOI  (1 << 0)
113 #define PIRQ_SHAREABLE  (1 << 1)
114
115 static int *evtchn_to_irq;
116 #ifdef CONFIG_X86
117 static unsigned long *pirq_eoi_map;
118 #endif
119 static bool (*pirq_needs_eoi)(unsigned irq);
120
121 /*
122  * Note sizeof(xen_ulong_t) can be more than sizeof(unsigned long). Be
123  * careful to only use bitops which allow for this (e.g
124  * test_bit/find_first_bit and friends but not __ffs) and to pass
125  * BITS_PER_EVTCHN_WORD as the bitmask length.
126  */
127 #define BITS_PER_EVTCHN_WORD (sizeof(xen_ulong_t)*8)
128 /*
129  * Make a bitmask (i.e. unsigned long *) of a xen_ulong_t
130  * array. Primarily to avoid long lines (hence the terse name).
131  */
132 #define BM(x) (unsigned long *)(x)
133 /* Find the first set bit in a evtchn mask */
134 #define EVTCHN_FIRST_BIT(w) find_first_bit(BM(&(w)), BITS_PER_EVTCHN_WORD)
135
136 static DEFINE_PER_CPU(xen_ulong_t [NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD],
137                       cpu_evtchn_mask);
138
139 /* Xen will never allocate port zero for any purpose. */
140 #define VALID_EVTCHN(chn)       ((chn) != 0)
141
142 static struct irq_chip xen_dynamic_chip;
143 static struct irq_chip xen_percpu_chip;
144 static struct irq_chip xen_pirq_chip;
145 static void enable_dynirq(struct irq_data *data);
146 static void disable_dynirq(struct irq_data *data);
147
148 /* Get info for IRQ */
149 static struct irq_info *info_for_irq(unsigned irq)
150 {
151         return irq_get_handler_data(irq);
152 }
153
154 /* Constructors for packed IRQ information. */
155 static void xen_irq_info_common_init(struct irq_info *info,
156                                      unsigned irq,
157                                      enum xen_irq_type type,
158                                      unsigned short evtchn,
159                                      unsigned short cpu)
160 {
161
162         BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
163
164         info->type = type;
165         info->irq = irq;
166         info->evtchn = evtchn;
167         info->cpu = cpu;
168
169         evtchn_to_irq[evtchn] = irq;
170 }
171
172 static void xen_irq_info_evtchn_init(unsigned irq,
173                                      unsigned short evtchn)
174 {
175         struct irq_info *info = info_for_irq(irq);
176
177         xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
178 }
179
180 static void xen_irq_info_ipi_init(unsigned cpu,
181                                   unsigned irq,
182                                   unsigned short evtchn,
183                                   enum ipi_vector ipi)
184 {
185         struct irq_info *info = info_for_irq(irq);
186
187         xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
188
189         info->u.ipi = ipi;
190
191         per_cpu(ipi_to_irq, cpu)[ipi] = irq;
192 }
193
194 static void xen_irq_info_virq_init(unsigned cpu,
195                                    unsigned irq,
196                                    unsigned short evtchn,
197                                    unsigned short virq)
198 {
199         struct irq_info *info = info_for_irq(irq);
200
201         xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
202
203         info->u.virq = virq;
204
205         per_cpu(virq_to_irq, cpu)[virq] = irq;
206 }
207
208 static void xen_irq_info_pirq_init(unsigned irq,
209                                    unsigned short evtchn,
210                                    unsigned short pirq,
211                                    unsigned short gsi,
212                                    uint16_t domid,
213                                    unsigned char flags)
214 {
215         struct irq_info *info = info_for_irq(irq);
216
217         xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
218
219         info->u.pirq.pirq = pirq;
220         info->u.pirq.gsi = gsi;
221         info->u.pirq.domid = domid;
222         info->u.pirq.flags = flags;
223 }
224
225 /*
226  * Accessors for packed IRQ information.
227  */
228 static unsigned int evtchn_from_irq(unsigned irq)
229 {
230         if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
231                 return 0;
232
233         return info_for_irq(irq)->evtchn;
234 }
235
236 unsigned irq_from_evtchn(unsigned int evtchn)
237 {
238         return evtchn_to_irq[evtchn];
239 }
240 EXPORT_SYMBOL_GPL(irq_from_evtchn);
241
242 static enum ipi_vector ipi_from_irq(unsigned irq)
243 {
244         struct irq_info *info = info_for_irq(irq);
245
246         BUG_ON(info == NULL);
247         BUG_ON(info->type != IRQT_IPI);
248
249         return info->u.ipi;
250 }
251
252 static unsigned virq_from_irq(unsigned irq)
253 {
254         struct irq_info *info = info_for_irq(irq);
255
256         BUG_ON(info == NULL);
257         BUG_ON(info->type != IRQT_VIRQ);
258
259         return info->u.virq;
260 }
261
262 static unsigned pirq_from_irq(unsigned irq)
263 {
264         struct irq_info *info = info_for_irq(irq);
265
266         BUG_ON(info == NULL);
267         BUG_ON(info->type != IRQT_PIRQ);
268
269         return info->u.pirq.pirq;
270 }
271
272 static enum xen_irq_type type_from_irq(unsigned irq)
273 {
274         return info_for_irq(irq)->type;
275 }
276
277 static unsigned cpu_from_irq(unsigned irq)
278 {
279         return info_for_irq(irq)->cpu;
280 }
281
282 static unsigned int cpu_from_evtchn(unsigned int evtchn)
283 {
284         int irq = evtchn_to_irq[evtchn];
285         unsigned ret = 0;
286
287         if (irq != -1)
288                 ret = cpu_from_irq(irq);
289
290         return ret;
291 }
292
293 #ifdef CONFIG_X86
294 static bool pirq_check_eoi_map(unsigned irq)
295 {
296         return test_bit(pirq_from_irq(irq), pirq_eoi_map);
297 }
298 #endif
299
300 static bool pirq_needs_eoi_flag(unsigned irq)
301 {
302         struct irq_info *info = info_for_irq(irq);
303         BUG_ON(info->type != IRQT_PIRQ);
304
305         return info->u.pirq.flags & PIRQ_NEEDS_EOI;
306 }
307
308 static inline xen_ulong_t active_evtchns(unsigned int cpu,
309                                          struct shared_info *sh,
310                                          unsigned int idx)
311 {
312         return sh->evtchn_pending[idx] &
313                 per_cpu(cpu_evtchn_mask, cpu)[idx] &
314                 ~sh->evtchn_mask[idx];
315 }
316
317 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
318 {
319         int irq = evtchn_to_irq[chn];
320
321         BUG_ON(irq == -1);
322 #ifdef CONFIG_SMP
323         cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
324 #endif
325
326         clear_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu_from_irq(irq))));
327         set_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu)));
328
329         info_for_irq(irq)->cpu = cpu;
330 }
331
332 static void init_evtchn_cpu_bindings(void)
333 {
334         int i;
335 #ifdef CONFIG_SMP
336         struct irq_info *info;
337
338         /* By default all event channels notify CPU#0. */
339         list_for_each_entry(info, &xen_irq_list_head, list) {
340                 struct irq_desc *desc = irq_to_desc(info->irq);
341                 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
342         }
343 #endif
344
345         for_each_possible_cpu(i)
346                 memset(per_cpu(cpu_evtchn_mask, i),
347                        (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
348 }
349
350 static inline void clear_evtchn(int port)
351 {
352         struct shared_info *s = HYPERVISOR_shared_info;
353         sync_clear_bit(port, BM(&s->evtchn_pending[0]));
354 }
355
356 static inline void set_evtchn(int port)
357 {
358         struct shared_info *s = HYPERVISOR_shared_info;
359         sync_set_bit(port, BM(&s->evtchn_pending[0]));
360 }
361
362 static inline int test_evtchn(int port)
363 {
364         struct shared_info *s = HYPERVISOR_shared_info;
365         return sync_test_bit(port, BM(&s->evtchn_pending[0]));
366 }
367
368
369 /**
370  * notify_remote_via_irq - send event to remote end of event channel via irq
371  * @irq: irq of event channel to send event to
372  *
373  * Unlike notify_remote_via_evtchn(), this is safe to use across
374  * save/restore. Notifications on a broken connection are silently
375  * dropped.
376  */
377 void notify_remote_via_irq(int irq)
378 {
379         int evtchn = evtchn_from_irq(irq);
380
381         if (VALID_EVTCHN(evtchn))
382                 notify_remote_via_evtchn(evtchn);
383 }
384 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
385
386 static void mask_evtchn(int port)
387 {
388         struct shared_info *s = HYPERVISOR_shared_info;
389         sync_set_bit(port, BM(&s->evtchn_mask[0]));
390 }
391
392 static void unmask_evtchn(int port)
393 {
394         struct shared_info *s = HYPERVISOR_shared_info;
395         unsigned int cpu = get_cpu();
396         int do_hypercall = 0, evtchn_pending = 0;
397
398         BUG_ON(!irqs_disabled());
399
400         if (unlikely((cpu != cpu_from_evtchn(port))))
401                 do_hypercall = 1;
402         else {
403                 /*
404                  * Need to clear the mask before checking pending to
405                  * avoid a race with an event becoming pending.
406                  *
407                  * EVTCHNOP_unmask will only trigger an upcall if the
408                  * mask bit was set, so if a hypercall is needed
409                  * remask the event.
410                  */
411                 sync_clear_bit(port, BM(&s->evtchn_mask[0]));
412                 evtchn_pending = sync_test_bit(port, BM(&s->evtchn_pending[0]));
413
414                 if (unlikely(evtchn_pending && xen_hvm_domain())) {
415                         sync_set_bit(port, BM(&s->evtchn_mask[0]));
416                         do_hypercall = 1;
417                 }
418         }
419
420         /* Slow path (hypercall) if this is a non-local port or if this is
421          * an hvm domain and an event is pending (hvm domains don't have
422          * their own implementation of irq_enable). */
423         if (do_hypercall) {
424                 struct evtchn_unmask unmask = { .port = port };
425                 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
426         } else {
427                 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
428
429                 /*
430                  * The following is basically the equivalent of
431                  * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
432                  * the interrupt edge' if the channel is masked.
433                  */
434                 if (evtchn_pending &&
435                     !sync_test_and_set_bit(port / BITS_PER_EVTCHN_WORD,
436                                            BM(&vcpu_info->evtchn_pending_sel)))
437                         vcpu_info->evtchn_upcall_pending = 1;
438         }
439
440         put_cpu();
441 }
442
443 static void xen_irq_init(unsigned irq)
444 {
445         struct irq_info *info;
446 #ifdef CONFIG_SMP
447         struct irq_desc *desc = irq_to_desc(irq);
448
449         /* By default all event channels notify CPU#0. */
450         cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
451 #endif
452
453         info = kzalloc(sizeof(*info), GFP_KERNEL);
454         if (info == NULL)
455                 panic("Unable to allocate metadata for IRQ%d\n", irq);
456
457         info->type = IRQT_UNBOUND;
458         info->refcnt = -1;
459
460         irq_set_handler_data(irq, info);
461
462         list_add_tail(&info->list, &xen_irq_list_head);
463 }
464
465 static int __must_check xen_allocate_irq_dynamic(void)
466 {
467         int first = 0;
468         int irq;
469
470 #ifdef CONFIG_X86_IO_APIC
471         /*
472          * For an HVM guest or domain 0 which see "real" (emulated or
473          * actual respectively) GSIs we allocate dynamic IRQs
474          * e.g. those corresponding to event channels or MSIs
475          * etc. from the range above those "real" GSIs to avoid
476          * collisions.
477          */
478         if (xen_initial_domain() || xen_hvm_domain())
479                 first = get_nr_irqs_gsi();
480 #endif
481
482         irq = irq_alloc_desc_from(first, -1);
483
484         if (irq >= 0)
485                 xen_irq_init(irq);
486
487         return irq;
488 }
489
490 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
491 {
492         int irq;
493
494         /*
495          * A PV guest has no concept of a GSI (since it has no ACPI
496          * nor access to/knowledge of the physical APICs). Therefore
497          * all IRQs are dynamically allocated from the entire IRQ
498          * space.
499          */
500         if (xen_pv_domain() && !xen_initial_domain())
501                 return xen_allocate_irq_dynamic();
502
503         /* Legacy IRQ descriptors are already allocated by the arch. */
504         if (gsi < NR_IRQS_LEGACY)
505                 irq = gsi;
506         else
507                 irq = irq_alloc_desc_at(gsi, -1);
508
509         xen_irq_init(irq);
510
511         return irq;
512 }
513
514 static void xen_free_irq(unsigned irq)
515 {
516         struct irq_info *info = irq_get_handler_data(irq);
517
518         if (WARN_ON(!info))
519                 return;
520
521         list_del(&info->list);
522
523         irq_set_handler_data(irq, NULL);
524
525         WARN_ON(info->refcnt > 0);
526
527         kfree(info);
528
529         /* Legacy IRQ descriptors are managed by the arch. */
530         if (irq < NR_IRQS_LEGACY)
531                 return;
532
533         irq_free_desc(irq);
534 }
535
536 static void pirq_query_unmask(int irq)
537 {
538         struct physdev_irq_status_query irq_status;
539         struct irq_info *info = info_for_irq(irq);
540
541         BUG_ON(info->type != IRQT_PIRQ);
542
543         irq_status.irq = pirq_from_irq(irq);
544         if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
545                 irq_status.flags = 0;
546
547         info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
548         if (irq_status.flags & XENIRQSTAT_needs_eoi)
549                 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
550 }
551
552 static bool probing_irq(int irq)
553 {
554         struct irq_desc *desc = irq_to_desc(irq);
555
556         return desc && desc->action == NULL;
557 }
558
559 static void eoi_pirq(struct irq_data *data)
560 {
561         int evtchn = evtchn_from_irq(data->irq);
562         struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
563         int rc = 0;
564
565         irq_move_irq(data);
566
567         if (VALID_EVTCHN(evtchn))
568                 clear_evtchn(evtchn);
569
570         if (pirq_needs_eoi(data->irq)) {
571                 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
572                 WARN_ON(rc);
573         }
574 }
575
576 static void mask_ack_pirq(struct irq_data *data)
577 {
578         disable_dynirq(data);
579         eoi_pirq(data);
580 }
581
582 static unsigned int __startup_pirq(unsigned int irq)
583 {
584         struct evtchn_bind_pirq bind_pirq;
585         struct irq_info *info = info_for_irq(irq);
586         int evtchn = evtchn_from_irq(irq);
587         int rc;
588
589         BUG_ON(info->type != IRQT_PIRQ);
590
591         if (VALID_EVTCHN(evtchn))
592                 goto out;
593
594         bind_pirq.pirq = pirq_from_irq(irq);
595         /* NB. We are happy to share unless we are probing. */
596         bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
597                                         BIND_PIRQ__WILL_SHARE : 0;
598         rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
599         if (rc != 0) {
600                 if (!probing_irq(irq))
601                         printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
602                                irq);
603                 return 0;
604         }
605         evtchn = bind_pirq.port;
606
607         pirq_query_unmask(irq);
608
609         evtchn_to_irq[evtchn] = irq;
610         bind_evtchn_to_cpu(evtchn, 0);
611         info->evtchn = evtchn;
612
613 out:
614         unmask_evtchn(evtchn);
615         eoi_pirq(irq_get_irq_data(irq));
616
617         return 0;
618 }
619
620 static unsigned int startup_pirq(struct irq_data *data)
621 {
622         return __startup_pirq(data->irq);
623 }
624
625 static void shutdown_pirq(struct irq_data *data)
626 {
627         struct evtchn_close close;
628         unsigned int irq = data->irq;
629         struct irq_info *info = info_for_irq(irq);
630         int evtchn = evtchn_from_irq(irq);
631
632         BUG_ON(info->type != IRQT_PIRQ);
633
634         if (!VALID_EVTCHN(evtchn))
635                 return;
636
637         mask_evtchn(evtchn);
638
639         close.port = evtchn;
640         if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
641                 BUG();
642
643         bind_evtchn_to_cpu(evtchn, 0);
644         evtchn_to_irq[evtchn] = -1;
645         info->evtchn = 0;
646 }
647
648 static void enable_pirq(struct irq_data *data)
649 {
650         startup_pirq(data);
651 }
652
653 static void disable_pirq(struct irq_data *data)
654 {
655         disable_dynirq(data);
656 }
657
658 int xen_irq_from_gsi(unsigned gsi)
659 {
660         struct irq_info *info;
661
662         list_for_each_entry(info, &xen_irq_list_head, list) {
663                 if (info->type != IRQT_PIRQ)
664                         continue;
665
666                 if (info->u.pirq.gsi == gsi)
667                         return info->irq;
668         }
669
670         return -1;
671 }
672 EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
673
674 /*
675  * Do not make any assumptions regarding the relationship between the
676  * IRQ number returned here and the Xen pirq argument.
677  *
678  * Note: We don't assign an event channel until the irq actually started
679  * up.  Return an existing irq if we've already got one for the gsi.
680  *
681  * Shareable implies level triggered, not shareable implies edge
682  * triggered here.
683  */
684 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
685                              unsigned pirq, int shareable, char *name)
686 {
687         int irq = -1;
688         struct physdev_irq irq_op;
689
690         mutex_lock(&irq_mapping_update_lock);
691
692         irq = xen_irq_from_gsi(gsi);
693         if (irq != -1) {
694                 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
695                        irq, gsi);
696                 goto out;
697         }
698
699         irq = xen_allocate_irq_gsi(gsi);
700         if (irq < 0)
701                 goto out;
702
703         irq_op.irq = irq;
704         irq_op.vector = 0;
705
706         /* Only the privileged domain can do this. For non-priv, the pcifront
707          * driver provides a PCI bus that does the call to do exactly
708          * this in the priv domain. */
709         if (xen_initial_domain() &&
710             HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
711                 xen_free_irq(irq);
712                 irq = -ENOSPC;
713                 goto out;
714         }
715
716         xen_irq_info_pirq_init(irq, 0, pirq, gsi, DOMID_SELF,
717                                shareable ? PIRQ_SHAREABLE : 0);
718
719         pirq_query_unmask(irq);
720         /* We try to use the handler with the appropriate semantic for the
721          * type of interrupt: if the interrupt is an edge triggered
722          * interrupt we use handle_edge_irq.
723          *
724          * On the other hand if the interrupt is level triggered we use
725          * handle_fasteoi_irq like the native code does for this kind of
726          * interrupts.
727          *
728          * Depending on the Xen version, pirq_needs_eoi might return true
729          * not only for level triggered interrupts but for edge triggered
730          * interrupts too. In any case Xen always honors the eoi mechanism,
731          * not injecting any more pirqs of the same kind if the first one
732          * hasn't received an eoi yet. Therefore using the fasteoi handler
733          * is the right choice either way.
734          */
735         if (shareable)
736                 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
737                                 handle_fasteoi_irq, name);
738         else
739                 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
740                                 handle_edge_irq, name);
741
742 out:
743         mutex_unlock(&irq_mapping_update_lock);
744
745         return irq;
746 }
747
748 #ifdef CONFIG_PCI_MSI
749 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
750 {
751         int rc;
752         struct physdev_get_free_pirq op_get_free_pirq;
753
754         op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
755         rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
756
757         WARN_ONCE(rc == -ENOSYS,
758                   "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
759
760         return rc ? -1 : op_get_free_pirq.pirq;
761 }
762
763 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
764                              int pirq, const char *name, domid_t domid)
765 {
766         int irq, ret;
767
768         mutex_lock(&irq_mapping_update_lock);
769
770         irq = xen_allocate_irq_dynamic();
771         if (irq < 0)
772                 goto out;
773
774         irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
775                         name);
776
777         xen_irq_info_pirq_init(irq, 0, pirq, 0, domid, 0);
778         ret = irq_set_msi_desc(irq, msidesc);
779         if (ret < 0)
780                 goto error_irq;
781 out:
782         mutex_unlock(&irq_mapping_update_lock);
783         return irq;
784 error_irq:
785         mutex_unlock(&irq_mapping_update_lock);
786         xen_free_irq(irq);
787         return ret;
788 }
789 #endif
790
791 int xen_destroy_irq(int irq)
792 {
793         struct irq_desc *desc;
794         struct physdev_unmap_pirq unmap_irq;
795         struct irq_info *info = info_for_irq(irq);
796         int rc = -ENOENT;
797
798         mutex_lock(&irq_mapping_update_lock);
799
800         desc = irq_to_desc(irq);
801         if (!desc)
802                 goto out;
803
804         if (xen_initial_domain()) {
805                 unmap_irq.pirq = info->u.pirq.pirq;
806                 unmap_irq.domid = info->u.pirq.domid;
807                 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
808                 /* If another domain quits without making the pci_disable_msix
809                  * call, the Xen hypervisor takes care of freeing the PIRQs
810                  * (free_domain_pirqs).
811                  */
812                 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
813                         printk(KERN_INFO "domain %d does not have %d anymore\n",
814                                 info->u.pirq.domid, info->u.pirq.pirq);
815                 else if (rc) {
816                         printk(KERN_WARNING "unmap irq failed %d\n", rc);
817                         goto out;
818                 }
819         }
820
821         xen_free_irq(irq);
822
823 out:
824         mutex_unlock(&irq_mapping_update_lock);
825         return rc;
826 }
827
828 int xen_irq_from_pirq(unsigned pirq)
829 {
830         int irq;
831
832         struct irq_info *info;
833
834         mutex_lock(&irq_mapping_update_lock);
835
836         list_for_each_entry(info, &xen_irq_list_head, list) {
837                 if (info->type != IRQT_PIRQ)
838                         continue;
839                 irq = info->irq;
840                 if (info->u.pirq.pirq == pirq)
841                         goto out;
842         }
843         irq = -1;
844 out:
845         mutex_unlock(&irq_mapping_update_lock);
846
847         return irq;
848 }
849
850
851 int xen_pirq_from_irq(unsigned irq)
852 {
853         return pirq_from_irq(irq);
854 }
855 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
856 int bind_evtchn_to_irq(unsigned int evtchn)
857 {
858         int irq;
859
860         mutex_lock(&irq_mapping_update_lock);
861
862         irq = evtchn_to_irq[evtchn];
863
864         if (irq == -1) {
865                 irq = xen_allocate_irq_dynamic();
866                 if (irq < 0)
867                         goto out;
868
869                 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
870                                               handle_edge_irq, "event");
871
872                 xen_irq_info_evtchn_init(irq, evtchn);
873         } else {
874                 struct irq_info *info = info_for_irq(irq);
875                 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
876         }
877         irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
878
879 out:
880         mutex_unlock(&irq_mapping_update_lock);
881
882         return irq;
883 }
884 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
885
886 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
887 {
888         struct evtchn_bind_ipi bind_ipi;
889         int evtchn, irq;
890
891         mutex_lock(&irq_mapping_update_lock);
892
893         irq = per_cpu(ipi_to_irq, cpu)[ipi];
894
895         if (irq == -1) {
896                 irq = xen_allocate_irq_dynamic();
897                 if (irq < 0)
898                         goto out;
899
900                 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
901                                               handle_percpu_irq, "ipi");
902
903                 bind_ipi.vcpu = cpu;
904                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
905                                                 &bind_ipi) != 0)
906                         BUG();
907                 evtchn = bind_ipi.port;
908
909                 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
910
911                 bind_evtchn_to_cpu(evtchn, cpu);
912         } else {
913                 struct irq_info *info = info_for_irq(irq);
914                 WARN_ON(info == NULL || info->type != IRQT_IPI);
915         }
916
917  out:
918         mutex_unlock(&irq_mapping_update_lock);
919         return irq;
920 }
921
922 static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
923                                           unsigned int remote_port)
924 {
925         struct evtchn_bind_interdomain bind_interdomain;
926         int err;
927
928         bind_interdomain.remote_dom  = remote_domain;
929         bind_interdomain.remote_port = remote_port;
930
931         err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
932                                           &bind_interdomain);
933
934         return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
935 }
936
937 static int find_virq(unsigned int virq, unsigned int cpu)
938 {
939         struct evtchn_status status;
940         int port, rc = -ENOENT;
941
942         memset(&status, 0, sizeof(status));
943         for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
944                 status.dom = DOMID_SELF;
945                 status.port = port;
946                 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
947                 if (rc < 0)
948                         continue;
949                 if (status.status != EVTCHNSTAT_virq)
950                         continue;
951                 if (status.u.virq == virq && status.vcpu == cpu) {
952                         rc = port;
953                         break;
954                 }
955         }
956         return rc;
957 }
958
959 int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
960 {
961         struct evtchn_bind_virq bind_virq;
962         int evtchn, irq, ret;
963
964         mutex_lock(&irq_mapping_update_lock);
965
966         irq = per_cpu(virq_to_irq, cpu)[virq];
967
968         if (irq == -1) {
969                 irq = xen_allocate_irq_dynamic();
970                 if (irq < 0)
971                         goto out;
972
973                 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
974                                               handle_percpu_irq, "virq");
975
976                 bind_virq.virq = virq;
977                 bind_virq.vcpu = cpu;
978                 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
979                                                 &bind_virq);
980                 if (ret == 0)
981                         evtchn = bind_virq.port;
982                 else {
983                         if (ret == -EEXIST)
984                                 ret = find_virq(virq, cpu);
985                         BUG_ON(ret < 0);
986                         evtchn = ret;
987                 }
988
989                 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
990
991                 bind_evtchn_to_cpu(evtchn, cpu);
992         } else {
993                 struct irq_info *info = info_for_irq(irq);
994                 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
995         }
996
997 out:
998         mutex_unlock(&irq_mapping_update_lock);
999
1000         return irq;
1001 }
1002
1003 static void unbind_from_irq(unsigned int irq)
1004 {
1005         struct evtchn_close close;
1006         int evtchn = evtchn_from_irq(irq);
1007         struct irq_info *info = irq_get_handler_data(irq);
1008
1009         if (WARN_ON(!info))
1010                 return;
1011
1012         mutex_lock(&irq_mapping_update_lock);
1013
1014         if (info->refcnt > 0) {
1015                 info->refcnt--;
1016                 if (info->refcnt != 0)
1017                         goto done;
1018         }
1019
1020         if (VALID_EVTCHN(evtchn)) {
1021                 close.port = evtchn;
1022                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
1023                         BUG();
1024
1025                 switch (type_from_irq(irq)) {
1026                 case IRQT_VIRQ:
1027                         per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
1028                                 [virq_from_irq(irq)] = -1;
1029                         break;
1030                 case IRQT_IPI:
1031                         per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
1032                                 [ipi_from_irq(irq)] = -1;
1033                         break;
1034                 default:
1035                         break;
1036                 }
1037
1038                 /* Closed ports are implicitly re-bound to VCPU0. */
1039                 bind_evtchn_to_cpu(evtchn, 0);
1040
1041                 evtchn_to_irq[evtchn] = -1;
1042         }
1043
1044         BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
1045
1046         xen_free_irq(irq);
1047
1048  done:
1049         mutex_unlock(&irq_mapping_update_lock);
1050 }
1051
1052 int bind_evtchn_to_irqhandler(unsigned int evtchn,
1053                               irq_handler_t handler,
1054                               unsigned long irqflags,
1055                               const char *devname, void *dev_id)
1056 {
1057         int irq, retval;
1058
1059         irq = bind_evtchn_to_irq(evtchn);
1060         if (irq < 0)
1061                 return irq;
1062         retval = request_irq(irq, handler, irqflags, devname, dev_id);
1063         if (retval != 0) {
1064                 unbind_from_irq(irq);
1065                 return retval;
1066         }
1067
1068         return irq;
1069 }
1070 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1071
1072 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1073                                           unsigned int remote_port,
1074                                           irq_handler_t handler,
1075                                           unsigned long irqflags,
1076                                           const char *devname,
1077                                           void *dev_id)
1078 {
1079         int irq, retval;
1080
1081         irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1082         if (irq < 0)
1083                 return irq;
1084
1085         retval = request_irq(irq, handler, irqflags, devname, dev_id);
1086         if (retval != 0) {
1087                 unbind_from_irq(irq);
1088                 return retval;
1089         }
1090
1091         return irq;
1092 }
1093 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1094
1095 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1096                             irq_handler_t handler,
1097                             unsigned long irqflags, const char *devname, void *dev_id)
1098 {
1099         int irq, retval;
1100
1101         irq = bind_virq_to_irq(virq, cpu);
1102         if (irq < 0)
1103                 return irq;
1104         retval = request_irq(irq, handler, irqflags, devname, dev_id);
1105         if (retval != 0) {
1106                 unbind_from_irq(irq);
1107                 return retval;
1108         }
1109
1110         return irq;
1111 }
1112 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1113
1114 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1115                            unsigned int cpu,
1116                            irq_handler_t handler,
1117                            unsigned long irqflags,
1118                            const char *devname,
1119                            void *dev_id)
1120 {
1121         int irq, retval;
1122
1123         irq = bind_ipi_to_irq(ipi, cpu);
1124         if (irq < 0)
1125                 return irq;
1126
1127         irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1128         retval = request_irq(irq, handler, irqflags, devname, dev_id);
1129         if (retval != 0) {
1130                 unbind_from_irq(irq);
1131                 return retval;
1132         }
1133
1134         return irq;
1135 }
1136
1137 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1138 {
1139         struct irq_info *info = irq_get_handler_data(irq);
1140
1141         if (WARN_ON(!info))
1142                 return;
1143         free_irq(irq, dev_id);
1144         unbind_from_irq(irq);
1145 }
1146 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1147
1148 int evtchn_make_refcounted(unsigned int evtchn)
1149 {
1150         int irq = evtchn_to_irq[evtchn];
1151         struct irq_info *info;
1152
1153         if (irq == -1)
1154                 return -ENOENT;
1155
1156         info = irq_get_handler_data(irq);
1157
1158         if (!info)
1159                 return -ENOENT;
1160
1161         WARN_ON(info->refcnt != -1);
1162
1163         info->refcnt = 1;
1164
1165         return 0;
1166 }
1167 EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1168
1169 int evtchn_get(unsigned int evtchn)
1170 {
1171         int irq;
1172         struct irq_info *info;
1173         int err = -ENOENT;
1174
1175         if (evtchn >= NR_EVENT_CHANNELS)
1176                 return -EINVAL;
1177
1178         mutex_lock(&irq_mapping_update_lock);
1179
1180         irq = evtchn_to_irq[evtchn];
1181         if (irq == -1)
1182                 goto done;
1183
1184         info = irq_get_handler_data(irq);
1185
1186         if (!info)
1187                 goto done;
1188
1189         err = -EINVAL;
1190         if (info->refcnt <= 0)
1191                 goto done;
1192
1193         info->refcnt++;
1194         err = 0;
1195  done:
1196         mutex_unlock(&irq_mapping_update_lock);
1197
1198         return err;
1199 }
1200 EXPORT_SYMBOL_GPL(evtchn_get);
1201
1202 void evtchn_put(unsigned int evtchn)
1203 {
1204         int irq = evtchn_to_irq[evtchn];
1205         if (WARN_ON(irq == -1))
1206                 return;
1207         unbind_from_irq(irq);
1208 }
1209 EXPORT_SYMBOL_GPL(evtchn_put);
1210
1211 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1212 {
1213         int irq = per_cpu(ipi_to_irq, cpu)[vector];
1214         BUG_ON(irq < 0);
1215         notify_remote_via_irq(irq);
1216 }
1217
1218 irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1219 {
1220         struct shared_info *sh = HYPERVISOR_shared_info;
1221         int cpu = smp_processor_id();
1222         xen_ulong_t *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
1223         int i;
1224         unsigned long flags;
1225         static DEFINE_SPINLOCK(debug_lock);
1226         struct vcpu_info *v;
1227
1228         spin_lock_irqsave(&debug_lock, flags);
1229
1230         printk("\nvcpu %d\n  ", cpu);
1231
1232         for_each_online_cpu(i) {
1233                 int pending;
1234                 v = per_cpu(xen_vcpu, i);
1235                 pending = (get_irq_regs() && i == cpu)
1236                         ? xen_irqs_disabled(get_irq_regs())
1237                         : v->evtchn_upcall_mask;
1238                 printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong"\n  ", i,
1239                        pending, v->evtchn_upcall_pending,
1240                        (int)(sizeof(v->evtchn_pending_sel)*2),
1241                        v->evtchn_pending_sel);
1242         }
1243         v = per_cpu(xen_vcpu, cpu);
1244
1245         printk("\npending:\n   ");
1246         for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1247                 printk("%0*"PRI_xen_ulong"%s",
1248                        (int)sizeof(sh->evtchn_pending[0])*2,
1249                        sh->evtchn_pending[i],
1250                        i % 8 == 0 ? "\n   " : " ");
1251         printk("\nglobal mask:\n   ");
1252         for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1253                 printk("%0*"PRI_xen_ulong"%s",
1254                        (int)(sizeof(sh->evtchn_mask[0])*2),
1255                        sh->evtchn_mask[i],
1256                        i % 8 == 0 ? "\n   " : " ");
1257
1258         printk("\nglobally unmasked:\n   ");
1259         for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1260                 printk("%0*"PRI_xen_ulong"%s",
1261                        (int)(sizeof(sh->evtchn_mask[0])*2),
1262                        sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1263                        i % 8 == 0 ? "\n   " : " ");
1264
1265         printk("\nlocal cpu%d mask:\n   ", cpu);
1266         for (i = (NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD)-1; i >= 0; i--)
1267                 printk("%0*"PRI_xen_ulong"%s", (int)(sizeof(cpu_evtchn[0])*2),
1268                        cpu_evtchn[i],
1269                        i % 8 == 0 ? "\n   " : " ");
1270
1271         printk("\nlocally unmasked:\n   ");
1272         for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1273                 xen_ulong_t pending = sh->evtchn_pending[i]
1274                         & ~sh->evtchn_mask[i]
1275                         & cpu_evtchn[i];
1276                 printk("%0*"PRI_xen_ulong"%s",
1277                        (int)(sizeof(sh->evtchn_mask[0])*2),
1278                        pending, i % 8 == 0 ? "\n   " : " ");
1279         }
1280
1281         printk("\npending list:\n");
1282         for (i = 0; i < NR_EVENT_CHANNELS; i++) {
1283                 if (sync_test_bit(i, BM(sh->evtchn_pending))) {
1284                         int word_idx = i / BITS_PER_EVTCHN_WORD;
1285                         printk("  %d: event %d -> irq %d%s%s%s\n",
1286                                cpu_from_evtchn(i), i,
1287                                evtchn_to_irq[i],
1288                                sync_test_bit(word_idx, BM(&v->evtchn_pending_sel))
1289                                              ? "" : " l2-clear",
1290                                !sync_test_bit(i, BM(sh->evtchn_mask))
1291                                              ? "" : " globally-masked",
1292                                sync_test_bit(i, BM(cpu_evtchn))
1293                                              ? "" : " locally-masked");
1294                 }
1295         }
1296
1297         spin_unlock_irqrestore(&debug_lock, flags);
1298
1299         return IRQ_HANDLED;
1300 }
1301
1302 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1303 static DEFINE_PER_CPU(unsigned int, current_word_idx);
1304 static DEFINE_PER_CPU(unsigned int, current_bit_idx);
1305
1306 /*
1307  * Mask out the i least significant bits of w
1308  */
1309 #define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
1310
1311 /*
1312  * Search the CPUs pending events bitmasks.  For each one found, map
1313  * the event number to an irq, and feed it into do_IRQ() for
1314  * handling.
1315  *
1316  * Xen uses a two-level bitmap to speed searching.  The first level is
1317  * a bitset of words which contain pending event bits.  The second
1318  * level is a bitset of pending events themselves.
1319  */
1320 static void __xen_evtchn_do_upcall(void)
1321 {
1322         int start_word_idx, start_bit_idx;
1323         int word_idx, bit_idx;
1324         int i, irq;
1325         int cpu = get_cpu();
1326         struct shared_info *s = HYPERVISOR_shared_info;
1327         struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1328         unsigned count;
1329
1330         do {
1331                 xen_ulong_t pending_words;
1332                 xen_ulong_t pending_bits;
1333                 struct irq_desc *desc;
1334
1335                 vcpu_info->evtchn_upcall_pending = 0;
1336
1337                 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1338                         goto out;
1339
1340                 /*
1341                  * Master flag must be cleared /before/ clearing
1342                  * selector flag. xchg_xen_ulong must contain an
1343                  * appropriate barrier.
1344                  */
1345                 if ((irq = per_cpu(virq_to_irq, cpu)[VIRQ_TIMER]) != -1) {
1346                         int evtchn = evtchn_from_irq(irq);
1347                         word_idx = evtchn / BITS_PER_LONG;
1348                         pending_bits = evtchn % BITS_PER_LONG;
1349                         if (active_evtchns(cpu, s, word_idx) & (1ULL << pending_bits)) {
1350                                 desc = irq_to_desc(irq);
1351                                 if (desc)
1352                                         generic_handle_irq_desc(irq, desc);
1353                         }
1354                 }
1355
1356                 pending_words = xchg_xen_ulong(&vcpu_info->evtchn_pending_sel, 0);
1357
1358                 start_word_idx = __this_cpu_read(current_word_idx);
1359                 start_bit_idx = __this_cpu_read(current_bit_idx);
1360
1361                 word_idx = start_word_idx;
1362
1363                 for (i = 0; pending_words != 0; i++) {
1364                         xen_ulong_t words;
1365
1366                         words = MASK_LSBS(pending_words, word_idx);
1367
1368                         /*
1369                          * If we masked out all events, wrap to beginning.
1370                          */
1371                         if (words == 0) {
1372                                 word_idx = 0;
1373                                 bit_idx = 0;
1374                                 continue;
1375                         }
1376                         word_idx = EVTCHN_FIRST_BIT(words);
1377
1378                         pending_bits = active_evtchns(cpu, s, word_idx);
1379                         bit_idx = 0; /* usually scan entire word from start */
1380                         if (word_idx == start_word_idx) {
1381                                 /* We scan the starting word in two parts */
1382                                 if (i == 0)
1383                                         /* 1st time: start in the middle */
1384                                         bit_idx = start_bit_idx;
1385                                 else
1386                                         /* 2nd time: mask bits done already */
1387                                         bit_idx &= (1UL << start_bit_idx) - 1;
1388                         }
1389
1390                         do {
1391                                 xen_ulong_t bits;
1392                                 int port;
1393
1394                                 bits = MASK_LSBS(pending_bits, bit_idx);
1395
1396                                 /* If we masked out all events, move on. */
1397                                 if (bits == 0)
1398                                         break;
1399
1400                                 bit_idx = EVTCHN_FIRST_BIT(bits);
1401
1402                                 /* Process port. */
1403                                 port = (word_idx * BITS_PER_EVTCHN_WORD) + bit_idx;
1404                                 irq = evtchn_to_irq[port];
1405
1406                                 if (irq != -1) {
1407                                         desc = irq_to_desc(irq);
1408                                         if (desc)
1409                                                 generic_handle_irq_desc(irq, desc);
1410                                 }
1411
1412                                 bit_idx = (bit_idx + 1) % BITS_PER_EVTCHN_WORD;
1413
1414                                 /* Next caller starts at last processed + 1 */
1415                                 __this_cpu_write(current_word_idx,
1416                                                  bit_idx ? word_idx :
1417                                                  (word_idx+1) % BITS_PER_EVTCHN_WORD);
1418                                 __this_cpu_write(current_bit_idx, bit_idx);
1419                         } while (bit_idx != 0);
1420
1421                         /* Scan start_l1i twice; all others once. */
1422                         if ((word_idx != start_word_idx) || (i != 0))
1423                                 pending_words &= ~(1UL << word_idx);
1424
1425                         word_idx = (word_idx + 1) % BITS_PER_EVTCHN_WORD;
1426                 }
1427
1428                 BUG_ON(!irqs_disabled());
1429
1430                 count = __this_cpu_read(xed_nesting_count);
1431                 __this_cpu_write(xed_nesting_count, 0);
1432         } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1433
1434 out:
1435
1436         put_cpu();
1437 }
1438
1439 void xen_evtchn_do_upcall(struct pt_regs *regs)
1440 {
1441         struct pt_regs *old_regs = set_irq_regs(regs);
1442
1443         irq_enter();
1444 #ifdef CONFIG_X86
1445         exit_idle();
1446 #endif
1447
1448         __xen_evtchn_do_upcall();
1449
1450         irq_exit();
1451         set_irq_regs(old_regs);
1452 }
1453
1454 void xen_hvm_evtchn_do_upcall(void)
1455 {
1456         __xen_evtchn_do_upcall();
1457 }
1458 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1459
1460 /* Rebind a new event channel to an existing irq. */
1461 void rebind_evtchn_irq(int evtchn, int irq)
1462 {
1463         struct irq_info *info = info_for_irq(irq);
1464
1465         if (WARN_ON(!info))
1466                 return;
1467
1468         /* Make sure the irq is masked, since the new event channel
1469            will also be masked. */
1470         disable_irq(irq);
1471
1472         mutex_lock(&irq_mapping_update_lock);
1473
1474         /* After resume the irq<->evtchn mappings are all cleared out */
1475         BUG_ON(evtchn_to_irq[evtchn] != -1);
1476         /* Expect irq to have been bound before,
1477            so there should be a proper type */
1478         BUG_ON(info->type == IRQT_UNBOUND);
1479
1480         xen_irq_info_evtchn_init(irq, evtchn);
1481
1482         mutex_unlock(&irq_mapping_update_lock);
1483
1484         /* new event channels are always bound to cpu 0 */
1485         irq_set_affinity(irq, cpumask_of(0));
1486
1487         /* Unmask the event channel. */
1488         enable_irq(irq);
1489 }
1490
1491 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1492 static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
1493 {
1494         struct evtchn_bind_vcpu bind_vcpu;
1495         int evtchn = evtchn_from_irq(irq);
1496
1497         if (!VALID_EVTCHN(evtchn))
1498                 return -1;
1499
1500         /*
1501          * Events delivered via platform PCI interrupts are always
1502          * routed to vcpu 0 and hence cannot be rebound.
1503          */
1504         if (xen_hvm_domain() && !xen_have_vector_callback)
1505                 return -1;
1506
1507         /* Send future instances of this interrupt to other vcpu. */
1508         bind_vcpu.port = evtchn;
1509         bind_vcpu.vcpu = tcpu;
1510
1511         /*
1512          * If this fails, it usually just indicates that we're dealing with a
1513          * virq or IPI channel, which don't actually need to be rebound. Ignore
1514          * it, but don't do the xenlinux-level rebind in that case.
1515          */
1516         if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1517                 bind_evtchn_to_cpu(evtchn, tcpu);
1518
1519         return 0;
1520 }
1521
1522 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1523                             bool force)
1524 {
1525         unsigned tcpu = cpumask_first(dest);
1526
1527         return rebind_irq_to_cpu(data->irq, tcpu);
1528 }
1529
1530 int resend_irq_on_evtchn(unsigned int irq)
1531 {
1532         int masked, evtchn = evtchn_from_irq(irq);
1533         struct shared_info *s = HYPERVISOR_shared_info;
1534
1535         if (!VALID_EVTCHN(evtchn))
1536                 return 1;
1537
1538         masked = sync_test_and_set_bit(evtchn, BM(s->evtchn_mask));
1539         sync_set_bit(evtchn, BM(s->evtchn_pending));
1540         if (!masked)
1541                 unmask_evtchn(evtchn);
1542
1543         return 1;
1544 }
1545
1546 static void enable_dynirq(struct irq_data *data)
1547 {
1548         int evtchn = evtchn_from_irq(data->irq);
1549
1550         if (VALID_EVTCHN(evtchn))
1551                 unmask_evtchn(evtchn);
1552 }
1553
1554 static void disable_dynirq(struct irq_data *data)
1555 {
1556         int evtchn = evtchn_from_irq(data->irq);
1557
1558         if (VALID_EVTCHN(evtchn))
1559                 mask_evtchn(evtchn);
1560 }
1561
1562 static void ack_dynirq(struct irq_data *data)
1563 {
1564         int evtchn = evtchn_from_irq(data->irq);
1565
1566         irq_move_irq(data);
1567
1568         if (VALID_EVTCHN(evtchn))
1569                 clear_evtchn(evtchn);
1570 }
1571
1572 static void mask_ack_dynirq(struct irq_data *data)
1573 {
1574         disable_dynirq(data);
1575         ack_dynirq(data);
1576 }
1577
1578 static int retrigger_dynirq(struct irq_data *data)
1579 {
1580         int evtchn = evtchn_from_irq(data->irq);
1581         struct shared_info *sh = HYPERVISOR_shared_info;
1582         int ret = 0;
1583
1584         if (VALID_EVTCHN(evtchn)) {
1585                 int masked;
1586
1587                 masked = sync_test_and_set_bit(evtchn, BM(sh->evtchn_mask));
1588                 sync_set_bit(evtchn, BM(sh->evtchn_pending));
1589                 if (!masked)
1590                         unmask_evtchn(evtchn);
1591                 ret = 1;
1592         }
1593
1594         return ret;
1595 }
1596
1597 static void restore_pirqs(void)
1598 {
1599         int pirq, rc, irq, gsi;
1600         struct physdev_map_pirq map_irq;
1601         struct irq_info *info;
1602
1603         list_for_each_entry(info, &xen_irq_list_head, list) {
1604                 if (info->type != IRQT_PIRQ)
1605                         continue;
1606
1607                 pirq = info->u.pirq.pirq;
1608                 gsi = info->u.pirq.gsi;
1609                 irq = info->irq;
1610
1611                 /* save/restore of PT devices doesn't work, so at this point the
1612                  * only devices present are GSI based emulated devices */
1613                 if (!gsi)
1614                         continue;
1615
1616                 map_irq.domid = DOMID_SELF;
1617                 map_irq.type = MAP_PIRQ_TYPE_GSI;
1618                 map_irq.index = gsi;
1619                 map_irq.pirq = pirq;
1620
1621                 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1622                 if (rc) {
1623                         printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1624                                         gsi, irq, pirq, rc);
1625                         xen_free_irq(irq);
1626                         continue;
1627                 }
1628
1629                 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1630
1631                 __startup_pirq(irq);
1632         }
1633 }
1634
1635 static void restore_cpu_virqs(unsigned int cpu)
1636 {
1637         struct evtchn_bind_virq bind_virq;
1638         int virq, irq, evtchn;
1639
1640         for (virq = 0; virq < NR_VIRQS; virq++) {
1641                 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1642                         continue;
1643
1644                 BUG_ON(virq_from_irq(irq) != virq);
1645
1646                 /* Get a new binding from Xen. */
1647                 bind_virq.virq = virq;
1648                 bind_virq.vcpu = cpu;
1649                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1650                                                 &bind_virq) != 0)
1651                         BUG();
1652                 evtchn = bind_virq.port;
1653
1654                 /* Record the new mapping. */
1655                 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
1656                 bind_evtchn_to_cpu(evtchn, cpu);
1657         }
1658 }
1659
1660 static void restore_cpu_ipis(unsigned int cpu)
1661 {
1662         struct evtchn_bind_ipi bind_ipi;
1663         int ipi, irq, evtchn;
1664
1665         for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1666                 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1667                         continue;
1668
1669                 BUG_ON(ipi_from_irq(irq) != ipi);
1670
1671                 /* Get a new binding from Xen. */
1672                 bind_ipi.vcpu = cpu;
1673                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1674                                                 &bind_ipi) != 0)
1675                         BUG();
1676                 evtchn = bind_ipi.port;
1677
1678                 /* Record the new mapping. */
1679                 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
1680                 bind_evtchn_to_cpu(evtchn, cpu);
1681         }
1682 }
1683
1684 /* Clear an irq's pending state, in preparation for polling on it */
1685 void xen_clear_irq_pending(int irq)
1686 {
1687         int evtchn = evtchn_from_irq(irq);
1688
1689         if (VALID_EVTCHN(evtchn))
1690                 clear_evtchn(evtchn);
1691 }
1692 EXPORT_SYMBOL(xen_clear_irq_pending);
1693 void xen_set_irq_pending(int irq)
1694 {
1695         int evtchn = evtchn_from_irq(irq);
1696
1697         if (VALID_EVTCHN(evtchn))
1698                 set_evtchn(evtchn);
1699 }
1700
1701 bool xen_test_irq_pending(int irq)
1702 {
1703         int evtchn = evtchn_from_irq(irq);
1704         bool ret = false;
1705
1706         if (VALID_EVTCHN(evtchn))
1707                 ret = test_evtchn(evtchn);
1708
1709         return ret;
1710 }
1711
1712 /* Poll waiting for an irq to become pending with timeout.  In the usual case,
1713  * the irq will be disabled so it won't deliver an interrupt. */
1714 void xen_poll_irq_timeout(int irq, u64 timeout)
1715 {
1716         evtchn_port_t evtchn = evtchn_from_irq(irq);
1717
1718         if (VALID_EVTCHN(evtchn)) {
1719                 struct sched_poll poll;
1720
1721                 poll.nr_ports = 1;
1722                 poll.timeout = timeout;
1723                 set_xen_guest_handle(poll.ports, &evtchn);
1724
1725                 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1726                         BUG();
1727         }
1728 }
1729 EXPORT_SYMBOL(xen_poll_irq_timeout);
1730 /* Poll waiting for an irq to become pending.  In the usual case, the
1731  * irq will be disabled so it won't deliver an interrupt. */
1732 void xen_poll_irq(int irq)
1733 {
1734         xen_poll_irq_timeout(irq, 0 /* no timeout */);
1735 }
1736
1737 /* Check whether the IRQ line is shared with other guests. */
1738 int xen_test_irq_shared(int irq)
1739 {
1740         struct irq_info *info = info_for_irq(irq);
1741         struct physdev_irq_status_query irq_status;
1742
1743         if (WARN_ON(!info))
1744                 return -ENOENT;
1745
1746         irq_status.irq = info->u.pirq.pirq;
1747
1748         if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1749                 return 0;
1750         return !(irq_status.flags & XENIRQSTAT_shared);
1751 }
1752 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1753
1754 void xen_irq_resume(void)
1755 {
1756         unsigned int cpu, evtchn;
1757         struct irq_info *info;
1758
1759         init_evtchn_cpu_bindings();
1760
1761         /* New event-channel space is not 'live' yet. */
1762         for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1763                 mask_evtchn(evtchn);
1764
1765         /* No IRQ <-> event-channel mappings. */
1766         list_for_each_entry(info, &xen_irq_list_head, list)
1767                 info->evtchn = 0; /* zap event-channel binding */
1768
1769         for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1770                 evtchn_to_irq[evtchn] = -1;
1771
1772         for_each_possible_cpu(cpu) {
1773                 restore_cpu_virqs(cpu);
1774                 restore_cpu_ipis(cpu);
1775         }
1776
1777         restore_pirqs();
1778 }
1779
1780 static struct irq_chip xen_dynamic_chip __read_mostly = {
1781         .name                   = "xen-dyn",
1782
1783         .irq_disable            = disable_dynirq,
1784         .irq_mask               = disable_dynirq,
1785         .irq_unmask             = enable_dynirq,
1786
1787         .irq_ack                = ack_dynirq,
1788         .irq_mask_ack           = mask_ack_dynirq,
1789
1790         .irq_set_affinity       = set_affinity_irq,
1791         .irq_retrigger          = retrigger_dynirq,
1792 };
1793
1794 static struct irq_chip xen_pirq_chip __read_mostly = {
1795         .name                   = "xen-pirq",
1796
1797         .irq_startup            = startup_pirq,
1798         .irq_shutdown           = shutdown_pirq,
1799         .irq_enable             = enable_pirq,
1800         .irq_disable            = disable_pirq,
1801
1802         .irq_mask               = disable_dynirq,
1803         .irq_unmask             = enable_dynirq,
1804
1805         .irq_ack                = eoi_pirq,
1806         .irq_eoi                = eoi_pirq,
1807         .irq_mask_ack           = mask_ack_pirq,
1808
1809         .irq_set_affinity       = set_affinity_irq,
1810
1811         .irq_retrigger          = retrigger_dynirq,
1812 };
1813
1814 static struct irq_chip xen_percpu_chip __read_mostly = {
1815         .name                   = "xen-percpu",
1816
1817         .irq_disable            = disable_dynirq,
1818         .irq_mask               = disable_dynirq,
1819         .irq_unmask             = enable_dynirq,
1820
1821         .irq_ack                = ack_dynirq,
1822 };
1823
1824 int xen_set_callback_via(uint64_t via)
1825 {
1826         struct xen_hvm_param a;
1827         a.domid = DOMID_SELF;
1828         a.index = HVM_PARAM_CALLBACK_IRQ;
1829         a.value = via;
1830         return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1831 }
1832 EXPORT_SYMBOL_GPL(xen_set_callback_via);
1833
1834 #ifdef CONFIG_XEN_PVHVM
1835 /* Vector callbacks are better than PCI interrupts to receive event
1836  * channel notifications because we can receive vector callbacks on any
1837  * vcpu and we don't need PCI support or APIC interactions. */
1838 void xen_callback_vector(void)
1839 {
1840         int rc;
1841         uint64_t callback_via;
1842         if (xen_have_vector_callback) {
1843                 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
1844                 rc = xen_set_callback_via(callback_via);
1845                 if (rc) {
1846                         printk(KERN_ERR "Request for Xen HVM callback vector"
1847                                         " failed.\n");
1848                         xen_have_vector_callback = 0;
1849                         return;
1850                 }
1851                 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1852                                 "enabled\n");
1853                 /* in the restore case the vector has already been allocated */
1854                 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
1855                         alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
1856                                         xen_hvm_callback_vector);
1857         }
1858 }
1859 #else
1860 void xen_callback_vector(void) {}
1861 #endif
1862
1863 void __init xen_init_IRQ(void)
1864 {
1865         int i;
1866
1867         evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1868                                     GFP_KERNEL);
1869         BUG_ON(!evtchn_to_irq);
1870         for (i = 0; i < NR_EVENT_CHANNELS; i++)
1871                 evtchn_to_irq[i] = -1;
1872
1873         init_evtchn_cpu_bindings();
1874
1875         /* No event channels are 'live' right now. */
1876         for (i = 0; i < NR_EVENT_CHANNELS; i++)
1877                 mask_evtchn(i);
1878
1879         pirq_needs_eoi = pirq_needs_eoi_flag;
1880
1881 #ifdef CONFIG_X86
1882         if (xen_hvm_domain()) {
1883                 xen_callback_vector();
1884                 native_init_IRQ();
1885                 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1886                  * __acpi_register_gsi can point at the right function */
1887                 pci_xen_hvm_init();
1888         } else {
1889                 int rc;
1890                 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1891
1892                 irq_ctx_init(smp_processor_id());
1893                 if (xen_initial_domain())
1894                         pci_xen_initial_domain();
1895
1896                 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1897                 eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1898                 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1899                 if (rc != 0) {
1900                         free_page((unsigned long) pirq_eoi_map);
1901                         pirq_eoi_map = NULL;
1902                 } else
1903                         pirq_needs_eoi = pirq_check_eoi_map;
1904         }
1905 #endif
1906 }