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