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