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