This commit was generated by cvs2svn to compensate for changes in r925,
[linux-2.6.git] / arch / xen / kernel / evtchn.c
1 /******************************************************************************
2  * evtchn.c
3  * 
4  * Communication via Xen event channels.
5  * 
6  * Copyright (c) 2002-2004, K A Fraser
7  * 
8  * This file may be distributed separately from the Linux kernel, or
9  * incorporated into other software packages, subject to the following license:
10  * 
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this source file (the "Software"), to deal in the Software without
13  * restriction, including without limitation the rights to use, copy, modify,
14  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
15  * and to permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  * 
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  * 
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27  * IN THE SOFTWARE.
28  */
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/irq.h>
33 #include <linux/interrupt.h>
34 #include <linux/sched.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/version.h>
37 #include <asm/atomic.h>
38 #include <asm/system.h>
39 #include <asm/ptrace.h>
40 #include <asm/synch_bitops.h>
41 #include <asm-xen/xen-public/event_channel.h>
42 #include <asm-xen/xen-public/physdev.h>
43 #include <asm-xen/ctrl_if.h>
44 #include <asm-xen/hypervisor.h>
45 #include <asm-xen/evtchn.h>
46
47 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
48 EXPORT_SYMBOL(force_evtchn_callback);
49 EXPORT_SYMBOL(evtchn_do_upcall);
50 #endif
51
52 /*
53  * This lock protects updates to the following mapping and reference-count
54  * arrays. The lock does not need to be acquired to read the mapping tables.
55  */
56 static spinlock_t irq_mapping_update_lock;
57
58 /* IRQ <-> event-channel mappings. */
59 static int evtchn_to_irq[NR_EVENT_CHANNELS];
60 static int irq_to_evtchn[NR_IRQS];
61
62 /* IRQ <-> VIRQ mapping. */
63 DEFINE_PER_CPU(int, virq_to_irq[NR_VIRQS]);
64
65 /* evtchn <-> IPI mapping. */
66 #ifndef NR_IPIS
67 #define NR_IPIS 1 
68 #endif
69 DEFINE_PER_CPU(int, ipi_to_evtchn[NR_IPIS]);
70
71 /* Reference counts for bindings to IRQs. */
72 static int irq_bindcount[NR_IRQS];
73
74 /* Bitmap indicating which PIRQs require Xen to be notified on unmask. */
75 static unsigned long pirq_needs_unmask_notify[NR_PIRQS/sizeof(unsigned long)];
76
77 /* Upcall to generic IRQ layer. */
78 #ifdef CONFIG_X86
79 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,9)
80 extern fastcall unsigned int do_IRQ(struct pt_regs *regs);
81 #else
82 extern asmlinkage unsigned int do_IRQ(struct pt_regs *regs);
83 #endif
84 #if defined (__i386__)
85 #define IRQ_REG orig_eax
86 #elif defined (__x86_64__)
87 #define IRQ_REG orig_rax
88 #endif
89 #define do_IRQ(irq, regs) do {          \
90     (regs)->IRQ_REG = (irq);            \
91     do_IRQ((regs));                     \
92 } while (0)
93 #endif
94
95 #define VALID_EVTCHN(_chn) ((_chn) >= 0)
96
97 /*
98  * Force a proper event-channel callback from Xen after clearing the
99  * callback mask. We do this in a very simple manner, by making a call
100  * down into Xen. The pending flag will be checked by Xen on return.
101  */
102 void force_evtchn_callback(void)
103 {
104     (void)HYPERVISOR_xen_version(0);
105 }
106
107 /* NB. Interrupts are disabled on entry. */
108 asmlinkage void evtchn_do_upcall(struct pt_regs *regs)
109 {
110     u32            l1, l2;
111     unsigned int   l1i, l2i, port;
112     int            irq;
113     shared_info_t *s = HYPERVISOR_shared_info;
114     vcpu_info_t   *vcpu_info = &s->vcpu_data[smp_processor_id()];
115
116     vcpu_info->evtchn_upcall_pending = 0;
117     
118     /* NB. No need for a barrier here -- XCHG is a barrier on x86. */
119     l1 = xchg(&vcpu_info->evtchn_pending_sel, 0);
120     while ( l1 != 0 )
121     {
122         l1i = __ffs(l1);
123         l1 &= ~(1 << l1i);
124         
125         while ( (l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i]) != 0 )
126         {
127             l2i = __ffs(l2);
128             l2 &= ~(1 << l2i);
129             
130             port = (l1i << 5) + l2i;
131             if ( (irq = evtchn_to_irq[port]) != -1 )
132                 do_IRQ(irq, regs);
133             else
134                 evtchn_device_upcall(port);
135         }
136     }
137 }
138
139 static int find_unbound_irq(void)
140 {
141     int irq;
142
143     for ( irq = 0; irq < NR_IRQS; irq++ )
144         if ( irq_bindcount[irq] == 0 )
145             break;
146
147     if ( irq == NR_IRQS )
148         panic("No available IRQ to bind to: increase NR_IRQS!\n");
149
150     return irq;
151 }
152
153 int bind_virq_to_irq(int virq)
154 {
155     evtchn_op_t op;
156     int evtchn, irq;
157     int cpu = smp_processor_id();
158
159     spin_lock(&irq_mapping_update_lock);
160
161     if ( (irq = per_cpu(virq_to_irq, cpu)[virq]) == -1 )
162     {
163         op.cmd              = EVTCHNOP_bind_virq;
164         op.u.bind_virq.virq = virq;
165         if ( HYPERVISOR_event_channel_op(&op) != 0 )
166             panic("Failed to bind virtual IRQ %d\n", virq);
167         evtchn = op.u.bind_virq.port;
168
169         irq = find_unbound_irq();
170         evtchn_to_irq[evtchn] = irq;
171         irq_to_evtchn[irq]    = evtchn;
172
173         per_cpu(virq_to_irq, cpu)[virq] = irq;
174     }
175
176     irq_bindcount[irq]++;
177
178     spin_unlock(&irq_mapping_update_lock);
179     
180     return irq;
181 }
182
183 void unbind_virq_from_irq(int virq)
184 {
185     evtchn_op_t op;
186     int cpu    = smp_processor_id();
187     int irq    = per_cpu(virq_to_irq, cpu)[virq];
188     int evtchn = irq_to_evtchn[irq];
189
190     spin_lock(&irq_mapping_update_lock);
191
192     if ( --irq_bindcount[irq] == 0 )
193     {
194         op.cmd          = EVTCHNOP_close;
195         op.u.close.dom  = DOMID_SELF;
196         op.u.close.port = evtchn;
197         if ( HYPERVISOR_event_channel_op(&op) != 0 )
198             panic("Failed to unbind virtual IRQ %d\n", virq);
199
200         evtchn_to_irq[evtchn] = -1;
201         irq_to_evtchn[irq]    = -1;
202         per_cpu(virq_to_irq, cpu)[virq]     = -1;
203     }
204
205     spin_unlock(&irq_mapping_update_lock);
206 }
207
208 int bind_ipi_on_cpu_to_irq(int cpu, int ipi)
209 {
210     evtchn_op_t op;
211     int evtchn, irq;
212
213     spin_lock(&irq_mapping_update_lock);
214
215     if ( (evtchn = per_cpu(ipi_to_evtchn, cpu)[ipi]) == 0 )
216     {
217         op.cmd                 = EVTCHNOP_bind_ipi;
218         op.u.bind_ipi.ipi_edom = cpu;
219         if ( HYPERVISOR_event_channel_op(&op) != 0 )
220             panic("Failed to bind virtual IPI %d on cpu %d\n", ipi, cpu);
221         evtchn = op.u.bind_ipi.port;
222
223         irq = find_unbound_irq();
224         evtchn_to_irq[evtchn] = irq;
225         irq_to_evtchn[irq]    = evtchn;
226
227         per_cpu(ipi_to_evtchn, cpu)[ipi] = evtchn;
228     } else
229         irq = evtchn_to_irq[evtchn];
230
231     irq_bindcount[irq]++;
232
233     spin_unlock(&irq_mapping_update_lock);
234
235     return irq;
236 }
237
238 void unbind_ipi_on_cpu_from_irq(int cpu, int ipi)
239 {
240     evtchn_op_t op;
241     int evtchn = per_cpu(ipi_to_evtchn, cpu)[ipi];
242     int irq    = irq_to_evtchn[evtchn];
243
244     spin_lock(&irq_mapping_update_lock);
245
246     if ( --irq_bindcount[irq] == 0 )
247     {
248         op.cmd          = EVTCHNOP_close;
249         op.u.close.dom  = DOMID_SELF;
250         op.u.close.port = evtchn;
251         if ( HYPERVISOR_event_channel_op(&op) != 0 )
252             panic("Failed to unbind virtual IPI %d on cpu %d\n", ipi, cpu);
253
254         evtchn_to_irq[evtchn] = -1;
255         irq_to_evtchn[irq]    = -1;
256         per_cpu(ipi_to_evtchn, cpu)[ipi] = 0;
257     }
258
259     spin_unlock(&irq_mapping_update_lock);
260 }
261
262 int bind_evtchn_to_irq(int evtchn)
263 {
264     int irq;
265
266     spin_lock(&irq_mapping_update_lock);
267
268     if ( (irq = evtchn_to_irq[evtchn]) == -1 )
269     {
270         irq = find_unbound_irq();
271         evtchn_to_irq[evtchn] = irq;
272         irq_to_evtchn[irq]    = evtchn;
273     }
274
275     irq_bindcount[irq]++;
276
277     spin_unlock(&irq_mapping_update_lock);
278     
279     return irq;
280 }
281
282 void unbind_evtchn_from_irq(int evtchn)
283 {
284     int irq = evtchn_to_irq[evtchn];
285
286     spin_lock(&irq_mapping_update_lock);
287
288     if ( --irq_bindcount[irq] == 0 )
289     {
290         evtchn_to_irq[evtchn] = -1;
291         irq_to_evtchn[irq]    = -1;
292     }
293
294     spin_unlock(&irq_mapping_update_lock);
295 }
296
297
298 /*
299  * Interface to generic handling in irq.c
300  */
301
302 static unsigned int startup_dynirq(unsigned int irq)
303 {
304     int evtchn = irq_to_evtchn[irq];
305
306     if ( !VALID_EVTCHN(evtchn) )
307         return 0;
308     unmask_evtchn(evtchn);
309     return 0;
310 }
311
312 static void shutdown_dynirq(unsigned int irq)
313 {
314     int evtchn = irq_to_evtchn[irq];
315
316     if ( !VALID_EVTCHN(evtchn) )
317         return;
318     mask_evtchn(evtchn);
319 }
320
321 static void enable_dynirq(unsigned int irq)
322 {
323     int evtchn = irq_to_evtchn[irq];
324
325     unmask_evtchn(evtchn);
326 }
327
328 static void disable_dynirq(unsigned int irq)
329 {
330     int evtchn = irq_to_evtchn[irq];
331
332     mask_evtchn(evtchn);
333 }
334
335 static void ack_dynirq(unsigned int irq)
336 {
337     int evtchn = irq_to_evtchn[irq];
338
339     mask_evtchn(evtchn);
340     clear_evtchn(evtchn);
341 }
342
343 static void end_dynirq(unsigned int irq)
344 {
345     int evtchn = irq_to_evtchn[irq];
346
347     if ( !(irq_desc[irq].status & IRQ_DISABLED) )
348         unmask_evtchn(evtchn);
349 }
350
351 static struct hw_interrupt_type dynirq_type = {
352     "Dynamic-irq",
353     startup_dynirq,
354     shutdown_dynirq,
355     enable_dynirq,
356     disable_dynirq,
357     ack_dynirq,
358     end_dynirq,
359     NULL
360 };
361
362 static inline void pirq_unmask_notify(int pirq)
363 {
364     physdev_op_t op;
365     if ( unlikely(test_bit(pirq, &pirq_needs_unmask_notify[0])) )
366     {
367         op.cmd = PHYSDEVOP_IRQ_UNMASK_NOTIFY;
368         (void)HYPERVISOR_physdev_op(&op);
369     }
370 }
371
372 static inline void pirq_query_unmask(int pirq)
373 {
374     physdev_op_t op;
375     op.cmd = PHYSDEVOP_IRQ_STATUS_QUERY;
376     op.u.irq_status_query.irq = pirq;
377     (void)HYPERVISOR_physdev_op(&op);
378     clear_bit(pirq, &pirq_needs_unmask_notify[0]);
379     if ( op.u.irq_status_query.flags & PHYSDEVOP_IRQ_NEEDS_UNMASK_NOTIFY )
380         set_bit(pirq, &pirq_needs_unmask_notify[0]);
381 }
382
383 /*
384  * On startup, if there is no action associated with the IRQ then we are
385  * probing. In this case we should not share with others as it will confuse us.
386  */
387 #define probing_irq(_irq) (irq_desc[(_irq)].action == NULL)
388
389 static unsigned int startup_pirq(unsigned int irq)
390 {
391     evtchn_op_t op;
392     int evtchn;
393
394     op.cmd               = EVTCHNOP_bind_pirq;
395     op.u.bind_pirq.pirq  = irq;
396     /* NB. We are happy to share unless we are probing. */
397     op.u.bind_pirq.flags = probing_irq(irq) ? 0 : BIND_PIRQ__WILL_SHARE;
398     if ( HYPERVISOR_event_channel_op(&op) != 0 )
399     {
400         if ( !probing_irq(irq) ) /* Some failures are expected when probing. */
401             printk(KERN_INFO "Failed to obtain physical IRQ %d\n", irq);
402         return 0;
403     }
404     evtchn = op.u.bind_pirq.port;
405
406     pirq_query_unmask(irq_to_pirq(irq));
407
408     evtchn_to_irq[evtchn] = irq;
409     irq_to_evtchn[irq]    = evtchn;
410
411     unmask_evtchn(evtchn);
412     pirq_unmask_notify(irq_to_pirq(irq));
413
414     return 0;
415 }
416
417 static void shutdown_pirq(unsigned int irq)
418 {
419     evtchn_op_t op;
420     int evtchn = irq_to_evtchn[irq];
421
422     if ( !VALID_EVTCHN(evtchn) )
423         return;
424
425     mask_evtchn(evtchn);
426
427     op.cmd          = EVTCHNOP_close;
428     op.u.close.dom  = DOMID_SELF;
429     op.u.close.port = evtchn;
430     if ( HYPERVISOR_event_channel_op(&op) != 0 )
431         panic("Failed to unbind physical IRQ %d\n", irq);
432
433     evtchn_to_irq[evtchn] = -1;
434     irq_to_evtchn[irq]    = -1;
435 }
436
437 static void enable_pirq(unsigned int irq)
438 {
439     int evtchn = irq_to_evtchn[irq];
440     if ( !VALID_EVTCHN(evtchn) )
441         return;
442     unmask_evtchn(evtchn);
443     pirq_unmask_notify(irq_to_pirq(irq));
444 }
445
446 static void disable_pirq(unsigned int irq)
447 {
448     int evtchn = irq_to_evtchn[irq];
449     if ( !VALID_EVTCHN(evtchn) )
450         return;
451     mask_evtchn(evtchn);
452 }
453
454 static void ack_pirq(unsigned int irq)
455 {
456     int evtchn = irq_to_evtchn[irq];
457     if ( !VALID_EVTCHN(evtchn) )
458         return;
459     mask_evtchn(evtchn);
460     clear_evtchn(evtchn);
461 }
462
463 static void end_pirq(unsigned int irq)
464 {
465     int evtchn = irq_to_evtchn[irq];
466     if ( !VALID_EVTCHN(evtchn) )
467         return;
468     if ( !(irq_desc[irq].status & IRQ_DISABLED) )
469     {
470         unmask_evtchn(evtchn);
471         pirq_unmask_notify(irq_to_pirq(irq));
472     }
473 }
474
475 static struct hw_interrupt_type pirq_type = {
476     "Phys-irq",
477     startup_pirq,
478     shutdown_pirq,
479     enable_pirq,
480     disable_pirq,
481     ack_pirq,
482     end_pirq,
483     NULL
484 };
485
486 void irq_suspend(void)
487 {
488     int pirq, virq, irq, evtchn;
489     int cpu = smp_processor_id(); /* XXX */
490
491     /* Unbind VIRQs from event channels. */
492     for ( virq = 0; virq < NR_VIRQS; virq++ )
493     {
494         if ( (irq = per_cpu(virq_to_irq, cpu)[virq]) == -1 )
495             continue;
496         evtchn = irq_to_evtchn[irq];
497
498         /* Mark the event channel as unused in our table. */
499         evtchn_to_irq[evtchn] = -1;
500         irq_to_evtchn[irq]    = -1;
501     }
502
503     /* Check that no PIRQs are still bound. */
504     for ( pirq = 0; pirq < NR_PIRQS; pirq++ )
505         if ( (evtchn = irq_to_evtchn[pirq_to_irq(pirq)]) != -1 )
506             panic("Suspend attempted while PIRQ %d bound to evtchn %d.\n",
507                   pirq, evtchn);
508 }
509
510 void irq_resume(void)
511 {
512     evtchn_op_t op;
513     int         virq, irq, evtchn;
514     int cpu = smp_processor_id(); /* XXX */
515
516     for ( evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++ )
517         mask_evtchn(evtchn); /* New event-channel space is not 'live' yet. */
518
519     for ( virq = 0; virq < NR_VIRQS; virq++ )
520     {
521         if ( (irq = per_cpu(virq_to_irq, cpu)[virq]) == -1 )
522             continue;
523
524         /* Get a new binding from Xen. */
525         op.cmd              = EVTCHNOP_bind_virq;
526         op.u.bind_virq.virq = virq;
527         if ( HYPERVISOR_event_channel_op(&op) != 0 )
528             panic("Failed to bind virtual IRQ %d\n", virq);
529         evtchn = op.u.bind_virq.port;
530         
531         /* Record the new mapping. */
532         evtchn_to_irq[evtchn] = irq;
533         irq_to_evtchn[irq]    = evtchn;
534
535         /* Ready for use. */
536         unmask_evtchn(evtchn);
537     }
538 }
539
540 void __init init_IRQ(void)
541 {
542     int i;
543     int cpu;
544
545     irq_ctx_init(0);
546
547     spin_lock_init(&irq_mapping_update_lock);
548
549     for ( cpu = 0; cpu < NR_CPUS; cpu++ ) {
550         /* No VIRQ -> IRQ mappings. */
551         for ( i = 0; i < NR_VIRQS; i++ )
552             per_cpu(virq_to_irq, cpu)[i] = -1;
553     }
554
555     /* No event-channel -> IRQ mappings. */
556     for ( i = 0; i < NR_EVENT_CHANNELS; i++ )
557     {
558         evtchn_to_irq[i] = -1;
559         mask_evtchn(i); /* No event channels are 'live' right now. */
560     }
561
562     /* No IRQ -> event-channel mappings. */
563     for ( i = 0; i < NR_IRQS; i++ )
564         irq_to_evtchn[i] = -1;
565
566     for ( i = 0; i < NR_DYNIRQS; i++ )
567     {
568         /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
569         irq_bindcount[dynirq_to_irq(i)] = 0;
570
571         irq_desc[dynirq_to_irq(i)].status  = IRQ_DISABLED;
572         irq_desc[dynirq_to_irq(i)].action  = 0;
573         irq_desc[dynirq_to_irq(i)].depth   = 1;
574         irq_desc[dynirq_to_irq(i)].handler = &dynirq_type;
575     }
576
577     for ( i = 0; i < NR_PIRQS; i++ )
578     {
579         /* Phys IRQ space is statically bound (1:1 mapping). Nail refcnts. */
580         irq_bindcount[pirq_to_irq(i)] = 1;
581
582         irq_desc[pirq_to_irq(i)].status  = IRQ_DISABLED;
583         irq_desc[pirq_to_irq(i)].action  = 0;
584         irq_desc[pirq_to_irq(i)].depth   = 1;
585         irq_desc[pirq_to_irq(i)].handler = &pirq_type;
586     }
587
588     /* This needs to be done early, but after the IRQ subsystem is alive. */
589     ctrl_if_init();
590 }