vserver 1.9.5.x5
[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 distribute_irqs = 1;
66 int __irq_offset_value;
67 int ppc_spurious_interrupts;
68 unsigned long lpevent_count;
69 u64 ppc64_interrupt_controller;
70
71 int show_interrupts(struct seq_file *p, void *v)
72 {
73         int i = *(loff_t *) v, j;
74         struct irqaction * action;
75         irq_desc_t *desc;
76         unsigned long flags;
77
78         if (i == 0) {
79                 seq_printf(p, "           ");
80                 for (j=0; j<NR_CPUS; j++) {
81                         if (cpu_online(j))
82                                 seq_printf(p, "CPU%d       ",j);
83                 }
84                 seq_putc(p, '\n');
85         }
86
87         if (i < NR_IRQS) {
88                 desc = get_irq_desc(i);
89                 spin_lock_irqsave(&desc->lock, flags);
90                 action = desc->action;
91                 if (!action || !action->handler)
92                         goto skip;
93                 seq_printf(p, "%3d: ", i);
94 #ifdef CONFIG_SMP
95                 for (j = 0; j < NR_CPUS; j++) {
96                         if (cpu_online(j))
97                                 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
98                 }
99 #else
100                 seq_printf(p, "%10u ", kstat_irqs(i));
101 #endif /* CONFIG_SMP */
102                 if (desc->handler)
103                         seq_printf(p, " %s ", desc->handler->typename );
104                 else
105                         seq_printf(p, "  None      ");
106                 seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge  ");
107                 seq_printf(p, "    %s",action->name);
108                 for (action=action->next; action; action = action->next)
109                         seq_printf(p, ", %s", action->name);
110                 seq_putc(p, '\n');
111 skip:
112                 spin_unlock_irqrestore(&desc->lock, flags);
113         } else if (i == NR_IRQS)
114                 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
115         return 0;
116 }
117
118 extern int noirqdebug;
119
120 /*
121  * Eventually, this should take an array of interrupts and an array size
122  * so it can dispatch multiple interrupts.
123  */
124 void ppc_irq_dispatch_handler(struct pt_regs *regs, int irq)
125 {
126         int status;
127         struct irqaction *action;
128         int cpu = smp_processor_id();
129         irq_desc_t *desc = get_irq_desc(irq);
130         irqreturn_t action_ret;
131 #ifdef CONFIG_IRQSTACKS
132         struct thread_info *curtp, *irqtp;
133 #endif
134
135         kstat_cpu(cpu).irqs[irq]++;
136
137         if (desc->status & IRQ_PER_CPU) {
138                 /* no locking required for CPU-local interrupts: */
139                 ack_irq(irq);
140                 action_ret = handle_IRQ_event(irq, regs, desc->action);
141                 desc->handler->end(irq);
142                 return;
143         }
144
145         spin_lock(&desc->lock);
146         ack_irq(irq);   
147         /*
148            REPLAY is when Linux resends an IRQ that was dropped earlier
149            WAITING is used by probe to mark irqs that are being tested
150            */
151         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
152         status |= IRQ_PENDING; /* we _want_ to handle it */
153
154         /*
155          * If the IRQ is disabled for whatever reason, we cannot
156          * use the action we have.
157          */
158         action = NULL;
159         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
160                 action = desc->action;
161                 if (!action || !action->handler) {
162                         ppc_spurious_interrupts++;
163                         printk(KERN_DEBUG "Unhandled interrupt %x, disabled\n", irq);
164                         /* We can't call disable_irq here, it would deadlock */
165                         if (!desc->depth)
166                                 desc->depth = 1;
167                         desc->status |= IRQ_DISABLED;
168                         /* This is not a real spurrious interrupt, we
169                          * have to eoi it, so we jump to out
170                          */
171                         mask_irq(irq);
172                         goto out;
173                 }
174                 status &= ~IRQ_PENDING; /* we commit to handling */
175                 status |= IRQ_INPROGRESS; /* we are handling it */
176         }
177         desc->status = status;
178
179         /*
180          * If there is no IRQ handler or it was disabled, exit early.
181            Since we set PENDING, if another processor is handling
182            a different instance of this same irq, the other processor
183            will take care of it.
184          */
185         if (unlikely(!action))
186                 goto out;
187
188         /*
189          * Edge triggered interrupts need to remember
190          * pending events.
191          * This applies to any hw interrupts that allow a second
192          * instance of the same irq to arrive while we are in do_IRQ
193          * or in the handler. But the code here only handles the _second_
194          * instance of the irq, not the third or fourth. So it is mostly
195          * useful for irq hardware that does not mask cleanly in an
196          * SMP environment.
197          */
198         for (;;) {
199                 spin_unlock(&desc->lock);
200
201 #ifdef CONFIG_IRQSTACKS
202                 /* Switch to the irq stack to handle this */
203                 curtp = current_thread_info();
204                 irqtp = hardirq_ctx[smp_processor_id()];
205                 if (curtp != irqtp) {
206                         irqtp->task = curtp->task;
207                         irqtp->flags = 0;
208                         action_ret = call_handle_IRQ_event(irq, regs, action, irqtp);
209                         irqtp->task = NULL;
210                         if (irqtp->flags)
211                                 set_bits(irqtp->flags, &curtp->flags);
212                 } else
213 #endif
214                         action_ret = handle_IRQ_event(irq, regs, action);
215
216                 spin_lock(&desc->lock);
217                 if (!noirqdebug)
218                         note_interrupt(irq, desc, action_ret);
219                 if (likely(!(desc->status & IRQ_PENDING)))
220                         break;
221                 desc->status &= ~IRQ_PENDING;
222         }
223 out:
224         desc->status &= ~IRQ_INPROGRESS;
225         /*
226          * The ->end() handler has to deal with interrupts which got
227          * disabled while the handler was running.
228          */
229         if (desc->handler) {
230                 if (desc->handler->end)
231                         desc->handler->end(irq);
232                 else if (desc->handler->enable)
233                         desc->handler->enable(irq);
234         }
235         spin_unlock(&desc->lock);
236 }
237
238 #ifdef CONFIG_PPC_ISERIES
239 void do_IRQ(struct pt_regs *regs)
240 {
241         struct paca_struct *lpaca;
242         struct ItLpQueue *lpq;
243
244         irq_enter();
245
246 #ifdef CONFIG_DEBUG_STACKOVERFLOW
247         /* Debugging check for stack overflow: is there less than 2KB free? */
248         {
249                 long sp;
250
251                 sp = __get_SP() & (THREAD_SIZE-1);
252
253                 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
254                         printk("do_IRQ: stack overflow: %ld\n",
255                                 sp - sizeof(struct thread_info));
256                         dump_stack();
257                 }
258         }
259 #endif
260
261         lpaca = get_paca();
262 #ifdef CONFIG_SMP
263         if (lpaca->lppaca.int_dword.fields.ipi_cnt) {
264                 lpaca->lppaca.int_dword.fields.ipi_cnt = 0;
265                 iSeries_smp_message_recv(regs);
266         }
267 #endif /* CONFIG_SMP */
268         lpq = lpaca->lpqueue_ptr;
269         if (lpq && ItLpQueue_isLpIntPending(lpq))
270                 lpevent_count += ItLpQueue_process(lpq, regs);
271
272         irq_exit();
273
274         if (lpaca->lppaca.int_dword.fields.decr_int) {
275                 lpaca->lppaca.int_dword.fields.decr_int = 0;
276                 /* Signal a fake decrementer interrupt */
277                 timer_interrupt(regs);
278         }
279 }
280
281 #else   /* CONFIG_PPC_ISERIES */
282
283 void do_IRQ(struct pt_regs *regs)
284 {
285         int irq;
286
287         irq_enter();
288
289 #ifdef CONFIG_DEBUG_STACKOVERFLOW
290         /* Debugging check for stack overflow: is there less than 2KB free? */
291         {
292                 long sp;
293
294                 sp = __get_SP() & (THREAD_SIZE-1);
295
296                 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
297                         printk("do_IRQ: stack overflow: %ld\n",
298                                 sp - sizeof(struct thread_info));
299                         dump_stack();
300                 }
301         }
302 #endif
303
304         irq = ppc_md.get_irq(regs);
305
306         if (irq >= 0)
307                 ppc_irq_dispatch_handler(regs, irq);
308         else
309                 /* That's not SMP safe ... but who cares ? */
310                 ppc_spurious_interrupts++;
311
312         irq_exit();
313 }
314 #endif  /* CONFIG_PPC_ISERIES */
315
316 void __init init_IRQ(void)
317 {
318         static int once = 0;
319
320         if (once)
321                 return;
322
323         once++;
324
325         ppc_md.init_IRQ();
326         irq_ctx_init();
327 }
328
329 #ifndef CONFIG_PPC_ISERIES
330 /*
331  * Virtual IRQ mapping code, used on systems with XICS interrupt controllers.
332  */
333
334 #define UNDEFINED_IRQ 0xffffffff
335 unsigned int virt_irq_to_real_map[NR_IRQS];
336
337 /*
338  * Don't use virtual irqs 0, 1, 2 for devices.
339  * The pcnet32 driver considers interrupt numbers < 2 to be invalid,
340  * and 2 is the XICS IPI interrupt.
341  * We limit virtual irqs to 17 less than NR_IRQS so that when we
342  * offset them by 16 (to reserve the first 16 for ISA interrupts)
343  * we don't end up with an interrupt number >= NR_IRQS.
344  */
345 #define MIN_VIRT_IRQ    3
346 #define MAX_VIRT_IRQ    (NR_IRQS - NUM_ISA_INTERRUPTS - 1)
347 #define NR_VIRT_IRQS    (MAX_VIRT_IRQ - MIN_VIRT_IRQ + 1)
348
349 void
350 virt_irq_init(void)
351 {
352         int i;
353         for (i = 0; i < NR_IRQS; i++)
354                 virt_irq_to_real_map[i] = UNDEFINED_IRQ;
355 }
356
357 /* Create a mapping for a real_irq if it doesn't already exist.
358  * Return the virtual irq as a convenience.
359  */
360 int virt_irq_create_mapping(unsigned int real_irq)
361 {
362         unsigned int virq, first_virq;
363         static int warned;
364
365         if (ppc64_interrupt_controller == IC_OPEN_PIC)
366                 return real_irq;        /* no mapping for openpic (for now) */
367
368         /* don't map interrupts < MIN_VIRT_IRQ */
369         if (real_irq < MIN_VIRT_IRQ) {
370                 virt_irq_to_real_map[real_irq] = real_irq;
371                 return real_irq;
372         }
373
374         /* map to a number between MIN_VIRT_IRQ and MAX_VIRT_IRQ */
375         virq = real_irq;
376         if (virq > MAX_VIRT_IRQ)
377                 virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
378
379         /* search for this number or a free slot */
380         first_virq = virq;
381         while (virt_irq_to_real_map[virq] != UNDEFINED_IRQ) {
382                 if (virt_irq_to_real_map[virq] == real_irq)
383                         return virq;
384                 if (++virq > MAX_VIRT_IRQ)
385                         virq = MIN_VIRT_IRQ;
386                 if (virq == first_virq)
387                         goto nospace;   /* oops, no free slots */
388         }
389
390         virt_irq_to_real_map[virq] = real_irq;
391         return virq;
392
393  nospace:
394         if (!warned) {
395                 printk(KERN_CRIT "Interrupt table is full\n");
396                 printk(KERN_CRIT "Increase NR_IRQS (currently %d) "
397                        "in your kernel sources and rebuild.\n", NR_IRQS);
398                 warned = 1;
399         }
400         return NO_IRQ;
401 }
402
403 /*
404  * In most cases will get a hit on the very first slot checked in the
405  * virt_irq_to_real_map.  Only when there are a large number of
406  * IRQs will this be expensive.
407  */
408 unsigned int real_irq_to_virt_slowpath(unsigned int real_irq)
409 {
410         unsigned int virq;
411         unsigned int first_virq;
412
413         virq = real_irq;
414
415         if (virq > MAX_VIRT_IRQ)
416                 virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
417
418         first_virq = virq;
419
420         do {
421                 if (virt_irq_to_real_map[virq] == real_irq)
422                         return virq;
423
424                 virq++;
425
426                 if (virq >= MAX_VIRT_IRQ)
427                         virq = 0;
428
429         } while (first_virq != virq);
430
431         return NO_IRQ;
432
433 }
434
435 #endif /* CONFIG_PPC_ISERIES */
436
437 #ifdef CONFIG_IRQSTACKS
438 struct thread_info *softirq_ctx[NR_CPUS];
439 struct thread_info *hardirq_ctx[NR_CPUS];
440
441 void irq_ctx_init(void)
442 {
443         struct thread_info *tp;
444         int i;
445
446         for_each_cpu(i) {
447                 memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
448                 tp = softirq_ctx[i];
449                 tp->cpu = i;
450                 tp->preempt_count = SOFTIRQ_OFFSET;
451
452                 memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
453                 tp = hardirq_ctx[i];
454                 tp->cpu = i;
455                 tp->preempt_count = HARDIRQ_OFFSET;
456         }
457 }
458
459 void do_softirq(void)
460 {
461         unsigned long flags;
462         struct thread_info *curtp, *irqtp;
463
464         if (in_interrupt())
465                 return;
466
467         local_irq_save(flags);
468
469         if (local_softirq_pending()) {
470                 curtp = current_thread_info();
471                 irqtp = softirq_ctx[smp_processor_id()];
472                 irqtp->task = curtp->task;
473                 call_do_softirq(irqtp);
474                 irqtp->task = NULL;
475         }
476
477         local_irq_restore(flags);
478 }
479 EXPORT_SYMBOL(do_softirq);
480
481 #endif /* CONFIG_IRQSTACKS */
482
483 static int __init setup_noirqdistrib(char *str)
484 {
485         distribute_irqs = 0;
486         return 1;
487 }
488
489 __setup("noirqdistrib", setup_noirqdistrib);