Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / xen / core / evtchn.c
1 /******************************************************************************
2  * evtchn.c
3  * 
4  * Communication via Xen event channels.
5  * 
6  * Copyright (c) 2002-2005, K A Fraser
7  * 
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation; or, when distributed
11  * separately from the Linux kernel or incorporated into other
12  * software packages, subject to the following license:
13  * 
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  * 
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  * 
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  */
32
33 #include <linux/module.h>
34 #include <linux/irq.h>
35 #include <linux/interrupt.h>
36 #include <linux/sched.h>
37 #include <linux/kernel_stat.h>
38 #include <linux/version.h>
39 #include <asm/atomic.h>
40 #include <asm/system.h>
41 #include <asm/ptrace.h>
42 #include <asm/synch_bitops.h>
43 #include <xen/evtchn.h>
44 #include <xen/interface/event_channel.h>
45 #include <xen/interface/physdev.h>
46 #include <asm/hypervisor.h>
47 #include <linux/mc146818rtc.h> /* RTC_IRQ */
48
49 /*
50  * This lock protects updates to the following mapping and reference-count
51  * arrays. The lock does not need to be acquired to read the mapping tables.
52  */
53 static DEFINE_SPINLOCK(irq_mapping_update_lock);
54
55 /* IRQ <-> event-channel mappings. */
56 static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
57         [0 ...  NR_EVENT_CHANNELS-1] = -1 };
58
59 /* Packed IRQ information: binding type, sub-type index, and event channel. */
60 static u32 irq_info[NR_IRQS];
61
62 /* Binding types. */
63 enum { IRQT_UNBOUND, IRQT_PIRQ, IRQT_VIRQ, IRQT_IPI, IRQT_EVTCHN };
64
65 /* Constructor for packed IRQ information. */
66 static inline u32 mk_irq_info(u32 type, u32 index, u32 evtchn)
67 {
68         return ((type << 24) | (index << 16) | evtchn);
69 }
70
71 /* Convenient shorthand for packed representation of an unbound IRQ. */
72 #define IRQ_UNBOUND     mk_irq_info(IRQT_UNBOUND, 0, 0)
73
74 /*
75  * Accessors for packed IRQ information.
76  */
77
78 static inline unsigned int evtchn_from_irq(int irq)
79 {
80         return (u16)(irq_info[irq]);
81 }
82
83 static inline unsigned int index_from_irq(int irq)
84 {
85         return (u8)(irq_info[irq] >> 16);
86 }
87
88 static inline unsigned int type_from_irq(int irq)
89 {
90         return (u8)(irq_info[irq] >> 24);
91 }
92
93 /* IRQ <-> VIRQ mapping. */
94 DEFINE_PER_CPU(int, virq_to_irq[NR_VIRQS]) = {[0 ... NR_VIRQS-1] = -1};
95
96 /* IRQ <-> IPI mapping. */
97 #ifndef NR_IPIS
98 #define NR_IPIS 1
99 #endif
100 DEFINE_PER_CPU(int, ipi_to_irq[NR_IPIS]) = {[0 ... NR_IPIS-1] = -1};
101
102 /* Reference counts for bindings to IRQs. */
103 static int irq_bindcount[NR_IRQS];
104
105 /* Bitmap indicating which PIRQs require Xen to be notified on unmask. */
106 static unsigned long pirq_needs_eoi[NR_PIRQS/sizeof(unsigned long)];
107
108 #ifdef CONFIG_SMP
109
110 static u8 cpu_evtchn[NR_EVENT_CHANNELS];
111 static unsigned long cpu_evtchn_mask[NR_CPUS][NR_EVENT_CHANNELS/BITS_PER_LONG];
112
113 static inline unsigned long active_evtchns(unsigned int cpu, shared_info_t *sh,
114                                            unsigned int idx)
115 {
116         return (sh->evtchn_pending[idx] &
117                 cpu_evtchn_mask[cpu][idx] &
118                 ~sh->evtchn_mask[idx]);
119 }
120
121 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
122 {
123         int irq = evtchn_to_irq[chn];
124
125         BUG_ON(irq == -1);
126         set_native_irq_info(irq, cpumask_of_cpu(cpu));
127
128         clear_bit(chn, (unsigned long *)cpu_evtchn_mask[cpu_evtchn[chn]]);
129         set_bit(chn, (unsigned long *)cpu_evtchn_mask[cpu]);
130         cpu_evtchn[chn] = cpu;
131 }
132
133 static void init_evtchn_cpu_bindings(void)
134 {
135         int i;
136
137         /* By default all event channels notify CPU#0. */
138         for (i = 0; i < NR_IRQS; i++)
139                 set_native_irq_info(i, cpumask_of_cpu(0));
140
141         memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
142         memset(cpu_evtchn_mask[0], ~0, sizeof(cpu_evtchn_mask[0]));
143 }
144
145 static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
146 {
147         return cpu_evtchn[evtchn];
148 }
149
150 #else
151
152 static inline unsigned long active_evtchns(unsigned int cpu, shared_info_t *sh,
153                                            unsigned int idx)
154 {
155         return (sh->evtchn_pending[idx] & ~sh->evtchn_mask[idx]);
156 }
157
158 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
159 {
160 }
161
162 static void init_evtchn_cpu_bindings(void)
163 {
164 }
165
166 static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
167 {
168         return 0;
169 }
170
171 #endif
172
173 /* Upcall to generic IRQ layer. */
174 #ifdef CONFIG_X86
175 extern fastcall unsigned int do_IRQ(struct pt_regs *regs);
176 void __init xen_init_IRQ(void);
177 void __init init_IRQ(void)
178 {
179         irq_ctx_init(0);
180         xen_init_IRQ();
181 }
182 #if defined (__i386__)
183 static inline void exit_idle(void) {}
184 #define IRQ_REG orig_eax
185 #elif defined (__x86_64__)
186 #include <asm/idle.h>
187 #define IRQ_REG orig_rax
188 #endif
189 #define do_IRQ(irq, regs) do {          \
190         (regs)->IRQ_REG = ~(irq);       \
191         do_IRQ((regs));                 \
192 } while (0)
193 #endif
194
195 /* Xen will never allocate port zero for any purpose. */
196 #define VALID_EVTCHN(chn)       ((chn) != 0)
197
198 /*
199  * Force a proper event-channel callback from Xen after clearing the
200  * callback mask. We do this in a very simple manner, by making a call
201  * down into Xen. The pending flag will be checked by Xen on return.
202  */
203 void force_evtchn_callback(void)
204 {
205         (void)HYPERVISOR_xen_version(0, NULL);
206 }
207 /* Not a GPL symbol: used in ubiquitous macros, so too restrictive. */
208 EXPORT_SYMBOL(force_evtchn_callback);
209
210 /* NB. Interrupts are disabled on entry. */
211 asmlinkage void evtchn_do_upcall(struct pt_regs *regs)
212 {
213         unsigned long  l1, l2;
214         unsigned int   l1i, l2i, port;
215         int            irq, cpu = smp_processor_id();
216         shared_info_t *s = HYPERVISOR_shared_info;
217         vcpu_info_t   *vcpu_info = &s->vcpu_info[cpu];
218
219         vcpu_info->evtchn_upcall_pending = 0;
220
221 #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
222         /* Clear master pending flag /before/ clearing selector flag. */
223         rmb();
224 #endif
225         l1 = xchg(&vcpu_info->evtchn_pending_sel, 0);
226         while (l1 != 0) {
227                 l1i = __ffs(l1);
228                 l1 &= ~(1UL << l1i);
229
230                 while ((l2 = active_evtchns(cpu, s, l1i)) != 0) {
231                         l2i = __ffs(l2);
232
233                         port = (l1i * BITS_PER_LONG) + l2i;
234                         if ((irq = evtchn_to_irq[port]) != -1)
235                                 do_IRQ(irq, regs);
236                         else {
237                                 exit_idle();
238                                 evtchn_device_upcall(port);
239                         }
240                 }
241         }
242 }
243
244 static int find_unbound_irq(void)
245 {
246         int irq;
247
248         /* Only allocate from dynirq range */
249         for (irq = DYNIRQ_BASE; irq < NR_IRQS; irq++)
250                 if (irq_bindcount[irq] == 0)
251                         break;
252
253         if (irq == NR_IRQS)
254                 panic("No available IRQ to bind to: increase NR_IRQS!\n");
255
256         return irq;
257 }
258
259 static int bind_evtchn_to_irq(unsigned int evtchn)
260 {
261         int irq;
262
263         spin_lock(&irq_mapping_update_lock);
264
265         if ((irq = evtchn_to_irq[evtchn]) == -1) {
266                 irq = find_unbound_irq();
267                 evtchn_to_irq[evtchn] = irq;
268                 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
269         }
270
271         irq_bindcount[irq]++;
272
273         spin_unlock(&irq_mapping_update_lock);
274
275         return irq;
276 }
277
278 static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
279 {
280         struct evtchn_bind_virq bind_virq;
281         int evtchn, irq;
282
283         spin_lock(&irq_mapping_update_lock);
284
285         if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1) {
286                 bind_virq.virq = virq;
287                 bind_virq.vcpu = cpu;
288                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
289                                                 &bind_virq) != 0)
290                         BUG();
291                 evtchn = bind_virq.port;
292
293                 irq = find_unbound_irq();
294                 evtchn_to_irq[evtchn] = irq;
295                 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
296
297                 per_cpu(virq_to_irq, cpu)[virq] = irq;
298
299                 bind_evtchn_to_cpu(evtchn, cpu);
300         }
301
302         irq_bindcount[irq]++;
303
304         spin_unlock(&irq_mapping_update_lock);
305
306         return irq;
307 }
308
309 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
310 {
311         struct evtchn_bind_ipi bind_ipi;
312         int evtchn, irq;
313
314         spin_lock(&irq_mapping_update_lock);
315
316         if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1) {
317                 bind_ipi.vcpu = cpu;
318                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
319                                                 &bind_ipi) != 0)
320                         BUG();
321                 evtchn = bind_ipi.port;
322
323                 irq = find_unbound_irq();
324                 evtchn_to_irq[evtchn] = irq;
325                 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
326
327                 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
328
329                 bind_evtchn_to_cpu(evtchn, cpu);
330         }
331
332         irq_bindcount[irq]++;
333
334         spin_unlock(&irq_mapping_update_lock);
335
336         return irq;
337 }
338
339 static void unbind_from_irq(unsigned int irq)
340 {
341         struct evtchn_close close;
342         int evtchn = evtchn_from_irq(irq);
343
344         spin_lock(&irq_mapping_update_lock);
345
346         if ((--irq_bindcount[irq] == 0) && VALID_EVTCHN(evtchn)) {
347                 close.port = evtchn;
348                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
349                         BUG();
350
351                 switch (type_from_irq(irq)) {
352                 case IRQT_VIRQ:
353                         per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
354                                 [index_from_irq(irq)] = -1;
355                         break;
356                 case IRQT_IPI:
357                         per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
358                                 [index_from_irq(irq)] = -1;
359                         break;
360                 default:
361                         break;
362                 }
363
364                 /* Closed ports are implicitly re-bound to VCPU0. */
365                 bind_evtchn_to_cpu(evtchn, 0);
366
367                 evtchn_to_irq[evtchn] = -1;
368                 irq_info[irq] = IRQ_UNBOUND;
369         }
370
371         spin_unlock(&irq_mapping_update_lock);
372 }
373
374 int bind_evtchn_to_irqhandler(
375         unsigned int evtchn,
376         irqreturn_t (*handler)(int, void *, struct pt_regs *),
377         unsigned long irqflags,
378         const char *devname,
379         void *dev_id)
380 {
381         unsigned int irq;
382         int retval;
383
384         irq = bind_evtchn_to_irq(evtchn);
385         retval = request_irq(irq, handler, irqflags, devname, dev_id);
386         if (retval != 0) {
387                 unbind_from_irq(irq);
388                 return retval;
389         }
390
391         return irq;
392 }
393 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
394
395 int bind_virq_to_irqhandler(
396         unsigned int virq,
397         unsigned int cpu,
398         irqreturn_t (*handler)(int, void *, struct pt_regs *),
399         unsigned long irqflags,
400         const char *devname,
401         void *dev_id)
402 {
403         unsigned int irq;
404         int retval;
405
406         irq = bind_virq_to_irq(virq, cpu);
407         retval = request_irq(irq, handler, irqflags, devname, dev_id);
408         if (retval != 0) {
409                 unbind_from_irq(irq);
410                 return retval;
411         }
412
413         return irq;
414 }
415 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
416
417 int bind_ipi_to_irqhandler(
418         unsigned int ipi,
419         unsigned int cpu,
420         irqreturn_t (*handler)(int, void *, struct pt_regs *),
421         unsigned long irqflags,
422         const char *devname,
423         void *dev_id)
424 {
425         unsigned int irq;
426         int retval;
427
428         irq = bind_ipi_to_irq(ipi, cpu);
429         retval = request_irq(irq, handler, irqflags, devname, dev_id);
430         if (retval != 0) {
431                 unbind_from_irq(irq);
432                 return retval;
433         }
434
435         return irq;
436 }
437 EXPORT_SYMBOL_GPL(bind_ipi_to_irqhandler);
438
439 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
440 {
441         free_irq(irq, dev_id);
442         unbind_from_irq(irq);
443 }
444 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
445
446 /* Rebind an evtchn so that it gets delivered to a specific cpu */
447 static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
448 {
449         struct evtchn_bind_vcpu bind_vcpu;
450         int evtchn = evtchn_from_irq(irq);
451
452         if (!VALID_EVTCHN(evtchn))
453                 return;
454
455         /* Send future instances of this interrupt to other vcpu. */
456         bind_vcpu.port = evtchn;
457         bind_vcpu.vcpu = tcpu;
458
459         /*
460          * If this fails, it usually just indicates that we're dealing with a 
461          * virq or IPI channel, which don't actually need to be rebound. Ignore
462          * it, but don't do the xenlinux-level rebind in that case.
463          */
464         if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
465                 bind_evtchn_to_cpu(evtchn, tcpu);
466 }
467
468
469 static void set_affinity_irq(unsigned irq, cpumask_t dest)
470 {
471         unsigned tcpu = first_cpu(dest);
472         rebind_irq_to_cpu(irq, tcpu);
473 }
474
475 static int retrigger(unsigned int irq)
476 {
477         int evtchn = evtchn_from_irq(irq);
478         shared_info_t *s = HYPERVISOR_shared_info;
479         if (!VALID_EVTCHN(evtchn))
480                 return 1;
481         BUG_ON(!synch_test_bit(evtchn, &s->evtchn_mask[0]));
482         synch_set_bit(evtchn, &s->evtchn_pending[0]);
483         return 1;
484 }
485
486 /*
487  * Interface to generic handling in irq.c
488  */
489
490 static unsigned int startup_dynirq(unsigned int irq)
491 {
492         int evtchn = evtchn_from_irq(irq);
493
494         if (VALID_EVTCHN(evtchn))
495                 unmask_evtchn(evtchn);
496         return 0;
497 }
498
499 static void shutdown_dynirq(unsigned int irq)
500 {
501         int evtchn = evtchn_from_irq(irq);
502
503         if (VALID_EVTCHN(evtchn))
504                 mask_evtchn(evtchn);
505 }
506
507 static void enable_dynirq(unsigned int irq)
508 {
509         int evtchn = evtchn_from_irq(irq);
510
511         if (VALID_EVTCHN(evtchn))
512                 unmask_evtchn(evtchn);
513 }
514
515 static void disable_dynirq(unsigned int irq)
516 {
517         int evtchn = evtchn_from_irq(irq);
518
519         if (VALID_EVTCHN(evtchn))
520                 mask_evtchn(evtchn);
521 }
522
523 static void ack_dynirq(unsigned int irq)
524 {
525         int evtchn = evtchn_from_irq(irq);
526
527         move_native_irq(irq);
528
529         if (VALID_EVTCHN(evtchn)) {
530                 mask_evtchn(evtchn);
531                 clear_evtchn(evtchn);
532         }
533 }
534
535 static void end_dynirq(unsigned int irq)
536 {
537         int evtchn = evtchn_from_irq(irq);
538
539         if (VALID_EVTCHN(evtchn) && !(irq_desc[irq].status & IRQ_DISABLED))
540                 unmask_evtchn(evtchn);
541 }
542
543 static struct hw_interrupt_type dynirq_type = {
544         .typename       = "Dynamic-irq",
545         .startup        = startup_dynirq,
546         .shutdown       = shutdown_dynirq,
547         .enable         = enable_dynirq,
548         .disable        = disable_dynirq,
549         .ack            = ack_dynirq,
550         .end            = end_dynirq,
551 #ifdef CONFIG_SMP
552         .set_affinity   = set_affinity_irq,
553 #endif
554         .retrigger      = retrigger,
555 };
556
557 static inline void pirq_unmask_notify(int pirq)
558 {
559         struct physdev_eoi eoi = { .irq = pirq };
560         if (unlikely(test_bit(pirq, &pirq_needs_eoi[0])))
561                 (void)HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
562 }
563
564 static inline void pirq_query_unmask(int pirq)
565 {
566         struct physdev_irq_status_query irq_status;
567         irq_status.irq = pirq;
568         (void)HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status);
569         clear_bit(pirq, &pirq_needs_eoi[0]);
570         if (irq_status.flags & XENIRQSTAT_needs_eoi)
571                 set_bit(pirq, &pirq_needs_eoi[0]);
572 }
573
574 /*
575  * On startup, if there is no action associated with the IRQ then we are
576  * probing. In this case we should not share with others as it will confuse us.
577  */
578 #define probing_irq(_irq) (irq_desc[(_irq)].action == NULL)
579
580 static unsigned int startup_pirq(unsigned int irq)
581 {
582         struct evtchn_bind_pirq bind_pirq;
583         int evtchn = evtchn_from_irq(irq);
584
585         if (VALID_EVTCHN(evtchn))
586                 goto out;
587
588         bind_pirq.pirq  = irq;
589         /* NB. We are happy to share unless we are probing. */
590         bind_pirq.flags = probing_irq(irq) ? 0 : BIND_PIRQ__WILL_SHARE;
591         if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq) != 0) {
592                 if (!probing_irq(irq))
593                         printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
594                                irq);
595                 return 0;
596         }
597         evtchn = bind_pirq.port;
598
599         pirq_query_unmask(irq_to_pirq(irq));
600
601         evtchn_to_irq[evtchn] = irq;
602         bind_evtchn_to_cpu(evtchn, 0);
603         irq_info[irq] = mk_irq_info(IRQT_PIRQ, irq, evtchn);
604
605  out:
606         unmask_evtchn(evtchn);
607         pirq_unmask_notify(irq_to_pirq(irq));
608
609         return 0;
610 }
611
612 static void shutdown_pirq(unsigned int irq)
613 {
614         struct evtchn_close close;
615         int evtchn = evtchn_from_irq(irq);
616
617         if (!VALID_EVTCHN(evtchn))
618                 return;
619
620         mask_evtchn(evtchn);
621
622         close.port = evtchn;
623         if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
624                 BUG();
625
626         bind_evtchn_to_cpu(evtchn, 0);
627         evtchn_to_irq[evtchn] = -1;
628         irq_info[irq] = IRQ_UNBOUND;
629 }
630
631 static void enable_pirq(unsigned int irq)
632 {
633         int evtchn = evtchn_from_irq(irq);
634
635         if (VALID_EVTCHN(evtchn)) {
636                 unmask_evtchn(evtchn);
637                 pirq_unmask_notify(irq_to_pirq(irq));
638         }
639 }
640
641 static void disable_pirq(unsigned int irq)
642 {
643         int evtchn = evtchn_from_irq(irq);
644
645         if (VALID_EVTCHN(evtchn))
646                 mask_evtchn(evtchn);
647 }
648
649 static void ack_pirq(unsigned int irq)
650 {
651         int evtchn = evtchn_from_irq(irq);
652
653         move_native_irq(irq);
654
655         if (VALID_EVTCHN(evtchn)) {
656                 mask_evtchn(evtchn);
657                 clear_evtchn(evtchn);
658         }
659 }
660
661 static void end_pirq(unsigned int irq)
662 {
663         int evtchn = evtchn_from_irq(irq);
664
665         if (VALID_EVTCHN(evtchn) && !(irq_desc[irq].status & IRQ_DISABLED)) {
666                 unmask_evtchn(evtchn);
667                 pirq_unmask_notify(irq_to_pirq(irq));
668         }
669 }
670
671 static struct hw_interrupt_type pirq_type = {
672         .typename       = "Phys-irq",
673         .startup        = startup_pirq,
674         .shutdown       = shutdown_pirq,
675         .enable         = enable_pirq,
676         .disable        = disable_pirq,
677         .ack            = ack_pirq,
678         .end            = end_pirq,
679 #ifdef CONFIG_SMP
680         .set_affinity   = set_affinity_irq,
681 #endif
682         .retrigger      = retrigger,
683 };
684
685 int irq_ignore_unhandled(unsigned int irq)
686 {
687         struct physdev_irq_status_query irq_status = { .irq = irq };
688
689         if (!is_running_on_xen())
690                 return 0;
691
692         (void)HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status);
693         return !!(irq_status.flags & XENIRQSTAT_shared);
694 }
695
696 void resend_irq_on_evtchn(unsigned int i)
697 {
698         int evtchn = evtchn_from_irq(i);
699         shared_info_t *s = HYPERVISOR_shared_info;
700         if (!VALID_EVTCHN(evtchn))
701                 return;
702         BUG_ON(!synch_test_bit(evtchn, &s->evtchn_mask[0]));
703         synch_set_bit(evtchn, &s->evtchn_pending[0]);
704 }
705
706 void notify_remote_via_irq(int irq)
707 {
708         int evtchn = evtchn_from_irq(irq);
709
710         if (VALID_EVTCHN(evtchn))
711                 notify_remote_via_evtchn(evtchn);
712 }
713 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
714
715 void mask_evtchn(int port)
716 {
717         shared_info_t *s = HYPERVISOR_shared_info;
718         synch_set_bit(port, &s->evtchn_mask[0]);
719 }
720 EXPORT_SYMBOL_GPL(mask_evtchn);
721
722 void unmask_evtchn(int port)
723 {
724         shared_info_t *s = HYPERVISOR_shared_info;
725         unsigned int cpu = smp_processor_id();
726         vcpu_info_t *vcpu_info = &s->vcpu_info[cpu];
727
728         BUG_ON(!irqs_disabled());
729
730         /* Slow path (hypercall) if this is a non-local port. */
731         if (unlikely(cpu != cpu_from_evtchn(port))) {
732                 struct evtchn_unmask unmask = { .port = port };
733                 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
734                 return;
735         }
736
737         synch_clear_bit(port, &s->evtchn_mask[0]);
738
739         /*
740          * The following is basically the equivalent of 'hw_resend_irq'. Just
741          * like a real IO-APIC we 'lose the interrupt edge' if the channel is
742          * masked.
743          */
744         if (synch_test_bit(port, &s->evtchn_pending[0]) &&
745             !synch_test_and_set_bit(port / BITS_PER_LONG,
746                                     &vcpu_info->evtchn_pending_sel))
747                 vcpu_info->evtchn_upcall_pending = 1;
748 }
749 EXPORT_SYMBOL_GPL(unmask_evtchn);
750
751 void irq_resume(void)
752 {
753         struct evtchn_bind_virq bind_virq;
754         struct evtchn_bind_ipi  bind_ipi;
755         int cpu, pirq, virq, ipi, irq, evtchn;
756
757         init_evtchn_cpu_bindings();
758
759         /* New event-channel space is not 'live' yet. */
760         for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
761                 mask_evtchn(evtchn);
762
763         /* Check that no PIRQs are still bound. */
764         for (pirq = 0; pirq < NR_PIRQS; pirq++)
765                 BUG_ON(irq_info[pirq_to_irq(pirq)] != IRQ_UNBOUND);
766
767         /* Secondary CPUs must have no VIRQ or IPI bindings. */
768         for_each_possible_cpu(cpu) {
769                 if (cpu == 0)
770                         continue;
771                 for (virq = 0; virq < NR_VIRQS; virq++)
772                         BUG_ON(per_cpu(virq_to_irq, cpu)[virq] != -1);
773                 for (ipi = 0; ipi < NR_IPIS; ipi++)
774                         BUG_ON(per_cpu(ipi_to_irq, cpu)[ipi] != -1);
775         }
776
777         /* No IRQ <-> event-channel mappings. */
778         for (irq = 0; irq < NR_IRQS; irq++)
779                 irq_info[irq] &= ~0xFFFF; /* zap event-channel binding */
780         for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
781                 evtchn_to_irq[evtchn] = -1;
782
783         /* Primary CPU: rebind VIRQs automatically. */
784         for (virq = 0; virq < NR_VIRQS; virq++) {
785                 if ((irq = per_cpu(virq_to_irq, 0)[virq]) == -1)
786                         continue;
787
788                 BUG_ON(irq_info[irq] != mk_irq_info(IRQT_VIRQ, virq, 0));
789
790                 /* Get a new binding from Xen. */
791                 bind_virq.virq = virq;
792                 bind_virq.vcpu = 0;
793                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
794                                                 &bind_virq) != 0)
795                         BUG();
796                 evtchn = bind_virq.port;
797
798                 /* Record the new mapping. */
799                 evtchn_to_irq[evtchn] = irq;
800                 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
801
802                 /* Ready for use. */
803                 unmask_evtchn(evtchn);
804         }
805
806         /* Primary CPU: rebind IPIs automatically. */
807         for (ipi = 0; ipi < NR_IPIS; ipi++) {
808                 if ((irq = per_cpu(ipi_to_irq, 0)[ipi]) == -1)
809                         continue;
810
811                 BUG_ON(irq_info[irq] != mk_irq_info(IRQT_IPI, ipi, 0));
812
813                 /* Get a new binding from Xen. */
814                 bind_ipi.vcpu = 0;
815                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
816                                                 &bind_ipi) != 0)
817                         BUG();
818                 evtchn = bind_ipi.port;
819
820                 /* Record the new mapping. */
821                 evtchn_to_irq[evtchn] = irq;
822                 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
823
824                 /* Ready for use. */
825                 unmask_evtchn(evtchn);
826         }
827 }
828
829 void __init xen_init_IRQ(void)
830 {
831         int i;
832
833         init_evtchn_cpu_bindings();
834
835         /* No event channels are 'live' right now. */
836         for (i = 0; i < NR_EVENT_CHANNELS; i++)
837                 mask_evtchn(i);
838
839         /* No IRQ -> event-channel mappings. */
840         for (i = 0; i < NR_IRQS; i++)
841                 irq_info[i] = IRQ_UNBOUND;
842
843         /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
844         for (i = 0; i < NR_DYNIRQS; i++) {
845                 irq_bindcount[dynirq_to_irq(i)] = 0;
846
847                 irq_desc[dynirq_to_irq(i)].status  = IRQ_DISABLED;
848                 irq_desc[dynirq_to_irq(i)].action  = NULL;
849                 irq_desc[dynirq_to_irq(i)].depth   = 1;
850                 irq_desc[dynirq_to_irq(i)].chip    = &dynirq_type;
851         }
852
853         /* Phys IRQ space is statically bound (1:1 mapping). Nail refcnts. */
854         for (i = 0; i < NR_PIRQS; i++) {
855                 irq_bindcount[pirq_to_irq(i)] = 1;
856
857 #ifdef RTC_IRQ
858                 /* If not domain 0, force our RTC driver to fail its probe. */
859                 if ((i == RTC_IRQ) && !is_initial_xendomain())
860                         continue;
861 #endif
862
863                 irq_desc[pirq_to_irq(i)].status  = IRQ_DISABLED;
864                 irq_desc[pirq_to_irq(i)].action  = NULL;
865                 irq_desc[pirq_to_irq(i)].depth   = 1;
866                 irq_desc[pirq_to_irq(i)].chip    = &pirq_type;
867         }
868 }