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