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