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