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