upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / arch / ppc64 / kernel / irq.c
1 /*
2  *  arch/ppc/kernel/irq.c
3  *
4  *  Derived from arch/i386/kernel/irq.c
5  *    Copyright (C) 1992 Linus Torvalds
6  *  Adapted from arch/i386 by Gary Thomas
7  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
8  *  Updated and modified by Cort Dougan (cort@cs.nmt.edu)
9  *    Copyright (C) 1996 Cort Dougan
10  *  Adapted for Power Macintosh by Paul Mackerras
11  *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
12  *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
13  * 
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version
17  * 2 of the License, or (at your option) any later version.
18  *
19  * This file contains the code used by various IRQ handling routines:
20  * asking for different IRQ's should be done through these routines
21  * instead of just grabbing them. Thus setups with different IRQ numbers
22  * shouldn't result in any weird surprises, and installing new handlers
23  * should be easier.
24  */
25
26 #include <linux/errno.h>
27 #include <linux/module.h>
28 #include <linux/threads.h>
29 #include <linux/kernel_stat.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/ioport.h>
33 #include <linux/interrupt.h>
34 #include <linux/timex.h>
35 #include <linux/config.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/pci.h>
39 #include <linux/delay.h>
40 #include <linux/irq.h>
41 #include <linux/proc_fs.h>
42 #include <linux/random.h>
43 #include <linux/kallsyms.h>
44 #include <linux/profile.h>
45 #include <linux/bitops.h>
46
47 #include <asm/uaccess.h>
48 #include <asm/system.h>
49 #include <asm/io.h>
50 #include <asm/pgtable.h>
51 #include <asm/irq.h>
52 #include <asm/cache.h>
53 #include <asm/prom.h>
54 #include <asm/ptrace.h>
55 #include <asm/iSeries/LparData.h>
56 #include <asm/machdep.h>
57 #include <asm/paca.h>
58
59 #ifdef CONFIG_SMP
60 extern void iSeries_smp_message_recv( struct pt_regs * );
61 #endif
62
63 extern irq_desc_t irq_desc[NR_IRQS];
64
65 int __irq_offset_value;
66 int ppc_spurious_interrupts;
67 unsigned long lpevent_count;
68
69 int show_interrupts(struct seq_file *p, void *v)
70 {
71         int i = *(loff_t *) v, j;
72         struct irqaction * action;
73         irq_desc_t *desc;
74         unsigned long flags;
75
76         if (i == 0) {
77                 seq_printf(p, "           ");
78                 for (j=0; j<NR_CPUS; j++) {
79                         if (cpu_online(j))
80                                 seq_printf(p, "CPU%d       ",j);
81                 }
82                 seq_putc(p, '\n');
83         }
84
85         if (i < NR_IRQS) {
86                 desc = get_irq_desc(i);
87                 spin_lock_irqsave(&desc->lock, flags);
88                 action = desc->action;
89                 if (!action || !action->handler)
90                         goto skip;
91                 seq_printf(p, "%3d: ", i);
92 #ifdef CONFIG_SMP
93                 for (j = 0; j < NR_CPUS; j++) {
94                         if (cpu_online(j))
95                                 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
96                 }
97 #else
98                 seq_printf(p, "%10u ", kstat_irqs(i));
99 #endif /* CONFIG_SMP */
100                 if (desc->handler)
101                         seq_printf(p, " %s ", desc->handler->typename );
102                 else
103                         seq_printf(p, "  None      ");
104                 seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge  ");
105                 seq_printf(p, "    %s",action->name);
106                 for (action=action->next; action; action = action->next)
107                         seq_printf(p, ", %s", action->name);
108                 seq_putc(p, '\n');
109 skip:
110                 spin_unlock_irqrestore(&desc->lock, flags);
111         } else if (i == NR_IRQS)
112                 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
113         return 0;
114 }
115
116 extern int noirqdebug;
117
118 /*
119  * Eventually, this should take an array of interrupts and an array size
120  * so it can dispatch multiple interrupts.
121  */
122 void ppc_irq_dispatch_handler(struct pt_regs *regs, int irq)
123 {
124         int status;
125         struct irqaction *action;
126         int cpu = smp_processor_id();
127         irq_desc_t *desc = get_irq_desc(irq);
128         irqreturn_t action_ret;
129 #ifdef CONFIG_IRQSTACKS
130         struct thread_info *curtp, *irqtp;
131 #endif
132
133         kstat_cpu(cpu).irqs[irq]++;
134
135         if (desc->status & IRQ_PER_CPU) {
136                 /* no locking required for CPU-local interrupts: */
137                 ack_irq(irq);
138                 action_ret = handle_IRQ_event(irq, regs, desc->action);
139                 desc->handler->end(irq);
140                 return;
141         }
142
143         spin_lock(&desc->lock);
144         ack_irq(irq);   
145         /*
146            REPLAY is when Linux resends an IRQ that was dropped earlier
147            WAITING is used by probe to mark irqs that are being tested
148            */
149         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
150         status |= IRQ_PENDING; /* we _want_ to handle it */
151
152         /*
153          * If the IRQ is disabled for whatever reason, we cannot
154          * use the action we have.
155          */
156         action = NULL;
157         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
158                 action = desc->action;
159                 if (!action || !action->handler) {
160                         ppc_spurious_interrupts++;
161                         printk(KERN_DEBUG "Unhandled interrupt %x, disabled\n", irq);
162                         /* We can't call disable_irq here, it would deadlock */
163                         if (!desc->depth)
164                                 desc->depth = 1;
165                         desc->status |= IRQ_DISABLED;
166                         /* This is not a real spurrious interrupt, we
167                          * have to eoi it, so we jump to out
168                          */
169                         mask_irq(irq);
170                         goto out;
171                 }
172                 status &= ~IRQ_PENDING; /* we commit to handling */
173                 status |= IRQ_INPROGRESS; /* we are handling it */
174         }
175         desc->status = status;
176
177         /*
178          * If there is no IRQ handler or it was disabled, exit early.
179            Since we set PENDING, if another processor is handling
180            a different instance of this same irq, the other processor
181            will take care of it.
182          */
183         if (unlikely(!action))
184                 goto out;
185
186         /*
187          * Edge triggered interrupts need to remember
188          * pending events.
189          * This applies to any hw interrupts that allow a second
190          * instance of the same irq to arrive while we are in do_IRQ
191          * or in the handler. But the code here only handles the _second_
192          * instance of the irq, not the third or fourth. So it is mostly
193          * useful for irq hardware that does not mask cleanly in an
194          * SMP environment.
195          */
196         for (;;) {
197                 spin_unlock(&desc->lock);
198
199 #ifdef CONFIG_IRQSTACKS
200                 /* Switch to the irq stack to handle this */
201                 curtp = current_thread_info();
202                 irqtp = hardirq_ctx[smp_processor_id()];
203                 if (curtp != irqtp) {
204                         irqtp->task = curtp->task;
205                         irqtp->flags = 0;
206                         action_ret = call_handle_IRQ_event(irq, regs, action, irqtp);
207                         irqtp->task = NULL;
208                         if (irqtp->flags)
209                                 set_bits(irqtp->flags, &curtp->flags);
210                 } else
211 #endif
212                         action_ret = handle_IRQ_event(irq, regs, action);
213
214                 spin_lock(&desc->lock);
215                 if (!noirqdebug)
216                         note_interrupt(irq, desc, action_ret, regs);
217                 if (likely(!(desc->status & IRQ_PENDING)))
218                         break;
219                 desc->status &= ~IRQ_PENDING;
220         }
221 out:
222         desc->status &= ~IRQ_INPROGRESS;
223         /*
224          * The ->end() handler has to deal with interrupts which got
225          * disabled while the handler was running.
226          */
227         if (desc->handler) {
228                 if (desc->handler->end)
229                         desc->handler->end(irq);
230                 else if (desc->handler->enable)
231                         desc->handler->enable(irq);
232         }
233         spin_unlock(&desc->lock);
234 }
235
236 #ifdef CONFIG_PPC_ISERIES
237 void do_IRQ(struct pt_regs *regs)
238 {
239         struct paca_struct *lpaca;
240         struct ItLpQueue *lpq;
241
242         irq_enter();
243
244 #ifdef CONFIG_DEBUG_STACKOVERFLOW
245         /* Debugging check for stack overflow: is there less than 2KB free? */
246         {
247                 long sp;
248
249                 sp = __get_SP() & (THREAD_SIZE-1);
250
251                 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
252                         printk("do_IRQ: stack overflow: %ld\n",
253                                 sp - sizeof(struct thread_info));
254                         dump_stack();
255                 }
256         }
257 #endif
258
259         lpaca = get_paca();
260 #ifdef CONFIG_SMP
261         if (lpaca->lppaca.xIntDword.xFields.xIpiCnt) {
262                 lpaca->lppaca.xIntDword.xFields.xIpiCnt = 0;
263                 iSeries_smp_message_recv(regs);
264         }
265 #endif /* CONFIG_SMP */
266         lpq = lpaca->lpqueue_ptr;
267         if (lpq && ItLpQueue_isLpIntPending(lpq))
268                 lpevent_count += ItLpQueue_process(lpq, regs);
269
270         irq_exit();
271
272         if (lpaca->lppaca.xIntDword.xFields.xDecrInt) {
273                 lpaca->lppaca.xIntDword.xFields.xDecrInt = 0;
274                 /* Signal a fake decrementer interrupt */
275                 timer_interrupt(regs);
276         }
277 }
278
279 #else   /* CONFIG_PPC_ISERIES */
280
281 void do_IRQ(struct pt_regs *regs)
282 {
283         int irq;
284
285         irq_enter();
286
287 #ifdef CONFIG_DEBUG_STACKOVERFLOW
288         /* Debugging check for stack overflow: is there less than 2KB free? */
289         {
290                 long sp;
291
292                 sp = __get_SP() & (THREAD_SIZE-1);
293
294                 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
295                         printk("do_IRQ: stack overflow: %ld\n",
296                                 sp - sizeof(struct thread_info));
297                         dump_stack();
298                 }
299         }
300 #endif
301
302         irq = ppc_md.get_irq(regs);
303
304         if (irq >= 0)
305                 ppc_irq_dispatch_handler(regs, irq);
306         else
307                 /* That's not SMP safe ... but who cares ? */
308                 ppc_spurious_interrupts++;
309
310         irq_exit();
311 }
312 #endif  /* CONFIG_PPC_ISERIES */
313
314 void __init init_IRQ(void)
315 {
316         static int once = 0;
317
318         if (once)
319                 return;
320
321         once++;
322
323         ppc_md.init_IRQ();
324         irq_ctx_init();
325 }
326
327 #ifndef CONFIG_PPC_ISERIES
328 /*
329  * Virtual IRQ mapping code, used on systems with XICS interrupt controllers.
330  */
331
332 #define UNDEFINED_IRQ 0xffffffff
333 unsigned int virt_irq_to_real_map[NR_IRQS];
334
335 /*
336  * Don't use virtual irqs 0, 1, 2 for devices.
337  * The pcnet32 driver considers interrupt numbers < 2 to be invalid,
338  * and 2 is the XICS IPI interrupt.
339  * We limit virtual irqs to 17 less than NR_IRQS so that when we
340  * offset them by 16 (to reserve the first 16 for ISA interrupts)
341  * we don't end up with an interrupt number >= NR_IRQS.
342  */
343 #define MIN_VIRT_IRQ    3
344 #define MAX_VIRT_IRQ    (NR_IRQS - NUM_ISA_INTERRUPTS - 1)
345 #define NR_VIRT_IRQS    (MAX_VIRT_IRQ - MIN_VIRT_IRQ + 1)
346
347 void
348 virt_irq_init(void)
349 {
350         int i;
351         for (i = 0; i < NR_IRQS; i++)
352                 virt_irq_to_real_map[i] = UNDEFINED_IRQ;
353 }
354
355 /* Create a mapping for a real_irq if it doesn't already exist.
356  * Return the virtual irq as a convenience.
357  */
358 int virt_irq_create_mapping(unsigned int real_irq)
359 {
360         unsigned int virq, first_virq;
361         static int warned;
362
363         if (naca->interrupt_controller == IC_OPEN_PIC)
364                 return real_irq;        /* no mapping for openpic (for now) */
365
366         /* don't map interrupts < MIN_VIRT_IRQ */
367         if (real_irq < MIN_VIRT_IRQ) {
368                 virt_irq_to_real_map[real_irq] = real_irq;
369                 return real_irq;
370         }
371
372         /* map to a number between MIN_VIRT_IRQ and MAX_VIRT_IRQ */
373         virq = real_irq;
374         if (virq > MAX_VIRT_IRQ)
375                 virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
376
377         /* search for this number or a free slot */
378         first_virq = virq;
379         while (virt_irq_to_real_map[virq] != UNDEFINED_IRQ) {
380                 if (virt_irq_to_real_map[virq] == real_irq)
381                         return virq;
382                 if (++virq > MAX_VIRT_IRQ)
383                         virq = MIN_VIRT_IRQ;
384                 if (virq == first_virq)
385                         goto nospace;   /* oops, no free slots */
386         }
387
388         virt_irq_to_real_map[virq] = real_irq;
389         return virq;
390
391  nospace:
392         if (!warned) {
393                 printk(KERN_CRIT "Interrupt table is full\n");
394                 printk(KERN_CRIT "Increase NR_IRQS (currently %d) "
395                        "in your kernel sources and rebuild.\n", NR_IRQS);
396                 warned = 1;
397         }
398         return NO_IRQ;
399 }
400
401 /*
402  * In most cases will get a hit on the very first slot checked in the
403  * virt_irq_to_real_map.  Only when there are a large number of
404  * IRQs will this be expensive.
405  */
406 unsigned int real_irq_to_virt_slowpath(unsigned int real_irq)
407 {
408         unsigned int virq;
409         unsigned int first_virq;
410
411         virq = real_irq;
412
413         if (virq > MAX_VIRT_IRQ)
414                 virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
415
416         first_virq = virq;
417
418         do {
419                 if (virt_irq_to_real_map[virq] == real_irq)
420                         return virq;
421
422                 virq++;
423
424                 if (virq >= MAX_VIRT_IRQ)
425                         virq = 0;
426
427         } while (first_virq != virq);
428
429         return NO_IRQ;
430
431 }
432
433 #endif /* CONFIG_PPC_ISERIES */
434
435 #ifdef CONFIG_IRQSTACKS
436 struct thread_info *softirq_ctx[NR_CPUS];
437 struct thread_info *hardirq_ctx[NR_CPUS];
438
439 void irq_ctx_init(void)
440 {
441         struct thread_info *tp;
442         int i;
443
444         for_each_cpu(i) {
445                 memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
446                 tp = softirq_ctx[i];
447                 tp->cpu = i;
448                 tp->preempt_count = SOFTIRQ_OFFSET;
449
450                 memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
451                 tp = hardirq_ctx[i];
452                 tp->cpu = i;
453                 tp->preempt_count = HARDIRQ_OFFSET;
454         }
455 }
456
457 void do_softirq(void)
458 {
459         unsigned long flags;
460         struct thread_info *curtp, *irqtp;
461
462         if (in_interrupt())
463                 return;
464
465         local_irq_save(flags);
466
467         if (local_softirq_pending()) {
468                 curtp = current_thread_info();
469                 irqtp = softirq_ctx[smp_processor_id()];
470                 irqtp->task = curtp->task;
471                 call_do_softirq(irqtp);
472                 irqtp->task = NULL;
473         }
474
475         local_irq_restore(flags);
476 }
477 EXPORT_SYMBOL(do_softirq);
478
479 #endif /* CONFIG_IRQSTACKS */
480