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