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