Merge to Fedora kernel-2.6.18-1.2255_FC5-vs2.0.2.2-rc9 patched with stable patch...
[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         synch_set_bit(evtchn, &s->evtchn_pending[0]);
482         return 1;
483 }
484
485 /*
486  * Interface to generic handling in irq.c
487  */
488
489 static unsigned int startup_dynirq(unsigned int irq)
490 {
491         int evtchn = evtchn_from_irq(irq);
492
493         if (VALID_EVTCHN(evtchn))
494                 unmask_evtchn(evtchn);
495         return 0;
496 }
497
498 static void shutdown_dynirq(unsigned int irq)
499 {
500         int evtchn = evtchn_from_irq(irq);
501
502         if (VALID_EVTCHN(evtchn))
503                 mask_evtchn(evtchn);
504 }
505
506 static void enable_dynirq(unsigned int irq)
507 {
508         int evtchn = evtchn_from_irq(irq);
509
510         if (VALID_EVTCHN(evtchn))
511                 unmask_evtchn(evtchn);
512 }
513
514 static void disable_dynirq(unsigned int irq)
515 {
516         int evtchn = evtchn_from_irq(irq);
517
518         if (VALID_EVTCHN(evtchn))
519                 mask_evtchn(evtchn);
520 }
521
522 static void ack_dynirq(unsigned int irq)
523 {
524         int evtchn = evtchn_from_irq(irq);
525
526         move_native_irq(irq);
527
528         if (VALID_EVTCHN(evtchn)) {
529                 mask_evtchn(evtchn);
530                 clear_evtchn(evtchn);
531         }
532 }
533
534 static void end_dynirq(unsigned int irq)
535 {
536         int evtchn = evtchn_from_irq(irq);
537
538         if (VALID_EVTCHN(evtchn) && !(irq_desc[irq].status & IRQ_DISABLED))
539                 unmask_evtchn(evtchn);
540 }
541
542 static struct hw_interrupt_type dynirq_type = {
543         .typename       = "Dynamic-irq",
544         .startup        = startup_dynirq,
545         .shutdown       = shutdown_dynirq,
546         .enable         = enable_dynirq,
547         .disable        = disable_dynirq,
548         .ack            = ack_dynirq,
549         .end            = end_dynirq,
550 #ifdef CONFIG_SMP
551         .set_affinity   = set_affinity_irq,
552 #endif
553         .retrigger      = retrigger,
554 };
555
556 static inline void pirq_unmask_notify(int pirq)
557 {
558         struct physdev_eoi eoi = { .irq = pirq };
559         if (unlikely(test_bit(pirq, &pirq_needs_eoi[0])))
560                 (void)HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
561 }
562
563 static inline void pirq_query_unmask(int pirq)
564 {
565         struct physdev_irq_status_query irq_status;
566         irq_status.irq = pirq;
567         (void)HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status);
568         clear_bit(pirq, &pirq_needs_eoi[0]);
569         if (irq_status.flags & XENIRQSTAT_needs_eoi)
570                 set_bit(pirq, &pirq_needs_eoi[0]);
571 }
572
573 /*
574  * On startup, if there is no action associated with the IRQ then we are
575  * probing. In this case we should not share with others as it will confuse us.
576  */
577 #define probing_irq(_irq) (irq_desc[(_irq)].action == NULL)
578
579 static unsigned int startup_pirq(unsigned int irq)
580 {
581         struct evtchn_bind_pirq bind_pirq;
582         int evtchn = evtchn_from_irq(irq);
583
584         if (VALID_EVTCHN(evtchn))
585                 goto out;
586
587         bind_pirq.pirq  = irq;
588         /* NB. We are happy to share unless we are probing. */
589         bind_pirq.flags = probing_irq(irq) ? 0 : BIND_PIRQ__WILL_SHARE;
590         if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq) != 0) {
591                 if (!probing_irq(irq))
592                         printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
593                                irq);
594                 return 0;
595         }
596         evtchn = bind_pirq.port;
597
598         pirq_query_unmask(irq_to_pirq(irq));
599
600         evtchn_to_irq[evtchn] = irq;
601         bind_evtchn_to_cpu(evtchn, 0);
602         irq_info[irq] = mk_irq_info(IRQT_PIRQ, irq, evtchn);
603
604  out:
605         unmask_evtchn(evtchn);
606         pirq_unmask_notify(irq_to_pirq(irq));
607
608         return 0;
609 }
610
611 static void shutdown_pirq(unsigned int irq)
612 {
613         struct evtchn_close close;
614         int evtchn = evtchn_from_irq(irq);
615
616         if (!VALID_EVTCHN(evtchn))
617                 return;
618
619         mask_evtchn(evtchn);
620
621         close.port = evtchn;
622         if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
623                 BUG();
624
625         bind_evtchn_to_cpu(evtchn, 0);
626         evtchn_to_irq[evtchn] = -1;
627         irq_info[irq] = IRQ_UNBOUND;
628 }
629
630 static void enable_pirq(unsigned int irq)
631 {
632         int evtchn = evtchn_from_irq(irq);
633
634         if (VALID_EVTCHN(evtchn)) {
635                 unmask_evtchn(evtchn);
636                 pirq_unmask_notify(irq_to_pirq(irq));
637         }
638 }
639
640 static void disable_pirq(unsigned int irq)
641 {
642         int evtchn = evtchn_from_irq(irq);
643
644         if (VALID_EVTCHN(evtchn))
645                 mask_evtchn(evtchn);
646 }
647
648 static void ack_pirq(unsigned int irq)
649 {
650         int evtchn = evtchn_from_irq(irq);
651
652         move_native_irq(irq);
653
654         if (VALID_EVTCHN(evtchn)) {
655                 mask_evtchn(evtchn);
656                 clear_evtchn(evtchn);
657         }
658 }
659
660 static void end_pirq(unsigned int irq)
661 {
662         int evtchn = evtchn_from_irq(irq);
663
664         if (VALID_EVTCHN(evtchn) && !(irq_desc[irq].status & IRQ_DISABLED)) {
665                 unmask_evtchn(evtchn);
666                 pirq_unmask_notify(irq_to_pirq(irq));
667         }
668 }
669
670 static struct hw_interrupt_type pirq_type = {
671         .typename       = "Phys-irq",
672         .startup        = startup_pirq,
673         .shutdown       = shutdown_pirq,
674         .enable         = enable_pirq,
675         .disable        = disable_pirq,
676         .ack            = ack_pirq,
677         .end            = end_pirq,
678 #ifdef CONFIG_SMP
679         .set_affinity   = set_affinity_irq,
680 #endif
681         .retrigger      = retrigger,
682 };
683
684 int irq_ignore_unhandled(unsigned int irq)
685 {
686         struct physdev_irq_status_query irq_status = { .irq = irq };
687
688         if (!is_running_on_xen())
689                 return 0;
690
691         (void)HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status);
692         return !!(irq_status.flags & XENIRQSTAT_shared);
693 }
694
695 void resend_irq_on_evtchn(unsigned int i)
696 {
697         int evtchn = evtchn_from_irq(i);
698         shared_info_t *s = HYPERVISOR_shared_info;
699         if (!VALID_EVTCHN(evtchn))
700                 return;
701         BUG_ON(!synch_test_bit(evtchn, &s->evtchn_mask[0]));
702         synch_set_bit(evtchn, &s->evtchn_pending[0]);
703 }
704
705 void notify_remote_via_irq(int irq)
706 {
707         int evtchn = evtchn_from_irq(irq);
708
709         if (VALID_EVTCHN(evtchn))
710                 notify_remote_via_evtchn(evtchn);
711 }
712 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
713
714 void mask_evtchn(int port)
715 {
716         shared_info_t *s = HYPERVISOR_shared_info;
717         synch_set_bit(port, &s->evtchn_mask[0]);
718 }
719 EXPORT_SYMBOL_GPL(mask_evtchn);
720
721 void unmask_evtchn(int port)
722 {
723         shared_info_t *s = HYPERVISOR_shared_info;
724         unsigned int cpu = smp_processor_id();
725         vcpu_info_t *vcpu_info = &s->vcpu_info[cpu];
726
727         BUG_ON(!irqs_disabled());
728
729         /* Slow path (hypercall) if this is a non-local port. */
730         if (unlikely(cpu != cpu_from_evtchn(port))) {
731                 struct evtchn_unmask unmask = { .port = port };
732                 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
733                 return;
734         }
735
736         synch_clear_bit(port, &s->evtchn_mask[0]);
737
738         /*
739          * The following is basically the equivalent of 'hw_resend_irq'. Just
740          * like a real IO-APIC we 'lose the interrupt edge' if the channel is
741          * masked.
742          */
743         if (synch_test_bit(port, &s->evtchn_pending[0]) &&
744             !synch_test_and_set_bit(port / BITS_PER_LONG,
745                                     &vcpu_info->evtchn_pending_sel))
746                 vcpu_info->evtchn_upcall_pending = 1;
747 }
748 EXPORT_SYMBOL_GPL(unmask_evtchn);
749
750 void irq_resume(void)
751 {
752         struct evtchn_bind_virq bind_virq;
753         struct evtchn_bind_ipi  bind_ipi;
754         int cpu, pirq, virq, ipi, irq, evtchn;
755
756         init_evtchn_cpu_bindings();
757
758         /* New event-channel space is not 'live' yet. */
759         for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
760                 mask_evtchn(evtchn);
761
762         /* Check that no PIRQs are still bound. */
763         for (pirq = 0; pirq < NR_PIRQS; pirq++)
764                 BUG_ON(irq_info[pirq_to_irq(pirq)] != IRQ_UNBOUND);
765
766         /* Secondary CPUs must have no VIRQ or IPI bindings. */
767         for_each_possible_cpu(cpu) {
768                 if (cpu == 0)
769                         continue;
770                 for (virq = 0; virq < NR_VIRQS; virq++)
771                         BUG_ON(per_cpu(virq_to_irq, cpu)[virq] != -1);
772                 for (ipi = 0; ipi < NR_IPIS; ipi++)
773                         BUG_ON(per_cpu(ipi_to_irq, cpu)[ipi] != -1);
774         }
775
776         /* No IRQ <-> event-channel mappings. */
777         for (irq = 0; irq < NR_IRQS; irq++)
778                 irq_info[irq] &= ~0xFFFF; /* zap event-channel binding */
779         for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
780                 evtchn_to_irq[evtchn] = -1;
781
782         /* Primary CPU: rebind VIRQs automatically. */
783         for (virq = 0; virq < NR_VIRQS; virq++) {
784                 if ((irq = per_cpu(virq_to_irq, 0)[virq]) == -1)
785                         continue;
786
787                 BUG_ON(irq_info[irq] != mk_irq_info(IRQT_VIRQ, virq, 0));
788
789                 /* Get a new binding from Xen. */
790                 bind_virq.virq = virq;
791                 bind_virq.vcpu = 0;
792                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
793                                                 &bind_virq) != 0)
794                         BUG();
795                 evtchn = bind_virq.port;
796
797                 /* Record the new mapping. */
798                 evtchn_to_irq[evtchn] = irq;
799                 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
800
801                 /* Ready for use. */
802                 unmask_evtchn(evtchn);
803         }
804
805         /* Primary CPU: rebind IPIs automatically. */
806         for (ipi = 0; ipi < NR_IPIS; ipi++) {
807                 if ((irq = per_cpu(ipi_to_irq, 0)[ipi]) == -1)
808                         continue;
809
810                 BUG_ON(irq_info[irq] != mk_irq_info(IRQT_IPI, ipi, 0));
811
812                 /* Get a new binding from Xen. */
813                 bind_ipi.vcpu = 0;
814                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
815                                                 &bind_ipi) != 0)
816                         BUG();
817                 evtchn = bind_ipi.port;
818
819                 /* Record the new mapping. */
820                 evtchn_to_irq[evtchn] = irq;
821                 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
822
823                 /* Ready for use. */
824                 unmask_evtchn(evtchn);
825         }
826 }
827
828 void __init xen_init_IRQ(void)
829 {
830         int i;
831
832         init_evtchn_cpu_bindings();
833
834         /* No event channels are 'live' right now. */
835         for (i = 0; i < NR_EVENT_CHANNELS; i++)
836                 mask_evtchn(i);
837
838         /* No IRQ -> event-channel mappings. */
839         for (i = 0; i < NR_IRQS; i++)
840                 irq_info[i] = IRQ_UNBOUND;
841
842         /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
843         for (i = 0; i < NR_DYNIRQS; i++) {
844                 irq_bindcount[dynirq_to_irq(i)] = 0;
845
846                 irq_desc[dynirq_to_irq(i)].status  = IRQ_DISABLED;
847                 irq_desc[dynirq_to_irq(i)].action  = NULL;
848                 irq_desc[dynirq_to_irq(i)].depth   = 1;
849                 irq_desc[dynirq_to_irq(i)].chip    = &dynirq_type;
850         }
851
852         /* Phys IRQ space is statically bound (1:1 mapping). Nail refcnts. */
853         for (i = 0; i < NR_PIRQS; i++) {
854                 irq_bindcount[pirq_to_irq(i)] = 1;
855
856 #ifdef RTC_IRQ
857                 /* If not domain 0, force our RTC driver to fail its probe. */
858                 if ((i == RTC_IRQ) && !is_initial_xendomain())
859                         continue;
860 #endif
861
862                 irq_desc[pirq_to_irq(i)].status  = IRQ_DISABLED;
863                 irq_desc[pirq_to_irq(i)].action  = NULL;
864                 irq_desc[pirq_to_irq(i)].depth   = 1;
865                 irq_desc[pirq_to_irq(i)].chip    = &pirq_type;
866         }
867 }