VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / arch / ppc / 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@fsmlabs.com>
9  *    Copyright (C) 1996-2001 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 file contains the code used by various IRQ handling routines:
15  * asking for different IRQ's should be done through these routines
16  * instead of just grabbing them. Thus setups with different IRQ numbers
17  * shouldn't result in any weird surprises, and installing new handlers
18  * should be easier.
19  *
20  * The MPC8xx has an interrupt mask in the SIU.  If a bit is set, the
21  * interrupt is _enabled_.  As expected, IRQ0 is bit 0 in the 32-bit
22  * mask register (of which only 16 are defined), hence the weird shifting
23  * and complement of the cached_irq_mask.  I want to be able to stuff
24  * this right into the SIU SMASK register.
25  * Many of the prep/chrp functions are conditional compiled on CONFIG_8xx
26  * to reduce code space and undefined function references.
27  */
28
29 #include <linux/errno.h>
30 #include <linux/module.h>
31 #include <linux/threads.h>
32 #include <linux/kernel_stat.h>
33 #include <linux/signal.h>
34 #include <linux/sched.h>
35 #include <linux/ptrace.h>
36 #include <linux/ioport.h>
37 #include <linux/interrupt.h>
38 #include <linux/timex.h>
39 #include <linux/config.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/pci.h>
43 #include <linux/delay.h>
44 #include <linux/irq.h>
45 #include <linux/proc_fs.h>
46 #include <linux/random.h>
47 #include <linux/seq_file.h>
48 #include <linux/cpumask.h>
49
50 #include <asm/uaccess.h>
51 #include <asm/bitops.h>
52 #include <asm/system.h>
53 #include <asm/io.h>
54 #include <asm/pgtable.h>
55 #include <asm/irq.h>
56 #include <asm/cache.h>
57 #include <asm/prom.h>
58 #include <asm/ptrace.h>
59
60 #define NR_MASK_WORDS   ((NR_IRQS + 31) / 32)
61
62 extern atomic_t ipi_recv;
63 extern atomic_t ipi_sent;
64 void enable_irq(unsigned int irq_nr);
65 void disable_irq(unsigned int irq_nr);
66
67 static void register_irq_proc (unsigned int irq);
68
69 #define MAXCOUNT 10000000
70
71 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
72         [0 ... NR_IRQS-1] = {
73                 .lock = SPIN_LOCK_UNLOCKED
74         }
75 };
76
77 int ppc_spurious_interrupts = 0;
78 struct irqaction *ppc_irq_action[NR_IRQS];
79 unsigned long ppc_cached_irq_mask[NR_MASK_WORDS];
80 unsigned long ppc_lost_interrupts[NR_MASK_WORDS];
81 atomic_t ppc_n_lost_interrupts;
82
83 /* nasty hack for shared irq's since we need to do kmalloc calls but
84  * can't very early in the boot when we need to do a request irq.
85  * this needs to be removed.
86  * -- Cort
87  */
88 #define IRQ_KMALLOC_ENTRIES 8
89 static int cache_bitmask = 0;
90 static struct irqaction malloc_cache[IRQ_KMALLOC_ENTRIES];
91 extern int mem_init_done;
92
93 #if defined(CONFIG_TAU_INT)
94 extern int tau_interrupts(unsigned long cpu);
95 extern int tau_initialized;
96 #endif
97
98 void *irq_kmalloc(size_t size, int pri)
99 {
100         unsigned int i;
101         if ( mem_init_done )
102                 return kmalloc(size,pri);
103         for ( i = 0; i < IRQ_KMALLOC_ENTRIES ; i++ )
104                 if ( ! ( cache_bitmask & (1<<i) ) )
105                 {
106                         cache_bitmask |= (1<<i);
107                         return (void *)(&malloc_cache[i]);
108                 }
109         return NULL;
110 }
111
112 void irq_kfree(void *ptr)
113 {
114         unsigned int i;
115         for ( i = 0 ; i < IRQ_KMALLOC_ENTRIES ; i++ )
116                 if ( ptr == &malloc_cache[i] )
117                 {
118                         cache_bitmask &= ~(1<<i);
119                         return;
120                 }
121         kfree(ptr);
122 }
123
124 int
125 setup_irq(unsigned int irq, struct irqaction * new)
126 {
127         int shared = 0;
128         unsigned long flags;
129         struct irqaction *old, **p;
130         irq_desc_t *desc = irq_desc + irq;
131
132         /*
133          * Some drivers like serial.c use request_irq() heavily,
134          * so we have to be careful not to interfere with a
135          * running system.
136          */
137         if (new->flags & SA_SAMPLE_RANDOM) {
138                 /*
139                  * This function might sleep, we want to call it first,
140                  * outside of the atomic block.
141                  * Yes, this might clear the entropy pool if the wrong
142                  * driver is attempted to be loaded, without actually
143                  * installing a new handler, but is this really a problem,
144                  * only the sysadmin is able to do this.
145                  */
146                 rand_initialize_irq(irq);
147         }
148
149         /*
150          * The following block of code has to be executed atomically
151          */
152         spin_lock_irqsave(&desc->lock,flags);
153         p = &desc->action;
154         if ((old = *p) != NULL) {
155                 /* Can't share interrupts unless both agree to */
156                 if (!(old->flags & new->flags & SA_SHIRQ)) {
157                         spin_unlock_irqrestore(&desc->lock,flags);
158                         return -EBUSY;
159                 }
160
161                 /* add new interrupt at end of irq queue */
162                 do {
163                         p = &old->next;
164                         old = *p;
165                 } while (old);
166                 shared = 1;
167         }
168
169         *p = new;
170
171         if (!shared) {
172                 desc->depth = 0;
173                 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING);
174                 if (desc->handler) {
175                         if (desc->handler->startup)
176                                 desc->handler->startup(irq);
177                         else if (desc->handler->enable)
178                                 desc->handler->enable(irq);
179                 }
180         }
181         spin_unlock_irqrestore(&desc->lock,flags);
182
183         register_irq_proc(irq);
184         return 0;
185 }
186
187 void free_irq(unsigned int irq, void* dev_id)
188 {
189         irq_desc_t *desc;
190         struct irqaction **p;
191         unsigned long flags;
192
193         desc = irq_desc + irq;
194         spin_lock_irqsave(&desc->lock,flags);
195         p = &desc->action;
196         for (;;) {
197                 struct irqaction * action = *p;
198                 if (action) {
199                         struct irqaction **pp = p;
200                         p = &action->next;
201                         if (action->dev_id != dev_id)
202                                 continue;
203
204                         /* Found it - now remove it from the list of entries */
205                         *pp = action->next;
206                         if (!desc->action) {
207                                 desc->status |= IRQ_DISABLED;
208                                 mask_irq(irq);
209                         }
210                         spin_unlock_irqrestore(&desc->lock,flags);
211
212                         synchronize_irq(irq);
213                         irq_kfree(action);
214                         return;
215                 }
216                 printk("Trying to free free IRQ%d\n",irq);
217                 spin_unlock_irqrestore(&desc->lock,flags);
218                 break;
219         }
220         return;
221 }
222
223 EXPORT_SYMBOL(free_irq);
224
225 int request_irq(unsigned int irq,
226         irqreturn_t (*handler)(int, void *, struct pt_regs *),
227         unsigned long irqflags, const char * devname, void *dev_id)
228 {
229         struct irqaction *action;
230         int retval;
231
232         if (irq >= NR_IRQS)
233                 return -EINVAL;
234         if (!handler) {
235                 printk(KERN_ERR "request_irq called with NULL handler!\n");
236                 dump_stack();
237                 return 0;
238         }
239
240         action = (struct irqaction *)
241                 irq_kmalloc(sizeof(struct irqaction), GFP_KERNEL);
242         if (!action) {
243                 printk(KERN_ERR "irq_kmalloc() failed for irq %d !\n", irq);
244                 return -ENOMEM;
245         }
246
247         action->handler = handler;
248         action->flags = irqflags;                       
249         cpus_clear(action->mask);
250         action->name = devname;
251         action->dev_id = dev_id;
252         action->next = NULL;
253
254         retval = setup_irq(irq, action);
255         if (retval) {
256                 kfree(action);
257                 return retval;
258         }
259
260         return 0;
261 }
262
263 EXPORT_SYMBOL(request_irq);
264
265 /*
266  * Generic enable/disable code: this just calls
267  * down into the PIC-specific version for the actual
268  * hardware disable after having gotten the irq
269  * controller lock.
270  */
271
272 /**
273  *      disable_irq_nosync - disable an irq without waiting
274  *      @irq: Interrupt to disable
275  *
276  *      Disable the selected interrupt line. Disables of an interrupt
277  *      stack. Unlike disable_irq(), this function does not ensure existing
278  *      instances of the IRQ handler have completed before returning.
279  *
280  *      This function may be called from IRQ context.
281  */
282
283 void disable_irq_nosync(unsigned int irq)
284 {
285         irq_desc_t *desc = irq_desc + irq;
286         unsigned long flags;
287
288         spin_lock_irqsave(&desc->lock, flags);
289         if (!desc->depth++) {
290                 if (!(desc->status & IRQ_PER_CPU))
291                         desc->status |= IRQ_DISABLED;
292                 mask_irq(irq);
293         }
294         spin_unlock_irqrestore(&desc->lock, flags);
295 }
296
297 /**
298  *      disable_irq - disable an irq and wait for completion
299  *      @irq: Interrupt to disable
300  *
301  *      Disable the selected interrupt line. Disables of an interrupt
302  *      stack. That is for two disables you need two enables. This
303  *      function waits for any pending IRQ handlers for this interrupt
304  *      to complete before returning. If you use this function while
305  *      holding a resource the IRQ handler may need you will deadlock.
306  *
307  *      This function may be called - with care - from IRQ context.
308  */
309
310 void disable_irq(unsigned int irq)
311 {
312         irq_desc_t *desc = irq_desc + irq;
313         disable_irq_nosync(irq);
314         if (desc->action)
315                 synchronize_irq(irq);
316 }
317
318 /**
319  *      enable_irq - enable interrupt handling on an irq
320  *      @irq: Interrupt to enable
321  *
322  *      Re-enables the processing of interrupts on this IRQ line
323  *      providing no disable_irq calls are now in effect.
324  *
325  *      This function may be called from IRQ context.
326  */
327
328 void enable_irq(unsigned int irq)
329 {
330         irq_desc_t *desc = irq_desc + irq;
331         unsigned long flags;
332
333         spin_lock_irqsave(&desc->lock, flags);
334         switch (desc->depth) {
335         case 1: {
336                 unsigned int status = desc->status & ~IRQ_DISABLED;
337                 desc->status = status;
338                 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
339                         desc->status = status | IRQ_REPLAY;
340                         hw_resend_irq(desc->handler,irq);
341                 }
342                 unmask_irq(irq);
343                 /* fall-through */
344         }
345         default:
346                 desc->depth--;
347                 break;
348         case 0:
349                 printk("enable_irq(%u) unbalanced\n", irq);
350         }
351         spin_unlock_irqrestore(&desc->lock, flags);
352 }
353
354 int show_interrupts(struct seq_file *p, void *v)
355 {
356         int i = *(loff_t *) v, j;
357         struct irqaction * action;
358         unsigned long flags;
359
360         if (i == 0) {
361                 seq_puts(p, "           ");
362                 for (j=0; j<NR_CPUS; j++)
363                         if (cpu_online(j))
364                                 seq_printf(p, "CPU%d       ", j);
365                 seq_putc(p, '\n');
366         }
367
368         if (i < NR_IRQS) {
369                 spin_lock_irqsave(&irq_desc[i].lock, flags);
370                 action = irq_desc[i].action;
371                 if ( !action || !action->handler )
372                         goto skip;
373                 seq_printf(p, "%3d: ", i);
374 #ifdef CONFIG_SMP
375                 for (j = 0; j < NR_CPUS; j++)
376                         if (cpu_online(j))
377                                 seq_printf(p, "%10u ",
378                                            kstat_cpu(j).irqs[i]);
379 #else
380                 seq_printf(p, "%10u ", kstat_irqs(i));
381 #endif /* CONFIG_SMP */
382                 if (irq_desc[i].handler)
383                         seq_printf(p, " %s ", irq_desc[i].handler->typename);
384                 else
385                         seq_puts(p, "  None      ");
386                 seq_printf(p, "%s", (irq_desc[i].status & IRQ_LEVEL) ? "Level " : "Edge  ");
387                 seq_printf(p, "    %s", action->name);
388                 for (action = action->next; action; action = action->next)
389                         seq_printf(p, ", %s", action->name);
390                 seq_putc(p, '\n');
391 skip:
392                 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
393         } else if (i == NR_IRQS) {
394 #ifdef CONFIG_TAU_INT
395                 if (tau_initialized){
396                         seq_puts(p, "TAU: ");
397                         for (j = 0; j < NR_CPUS; j++)
398                                 if (cpu_online(j))
399                                         seq_printf(p, "%10u ", tau_interrupts(j));
400                         seq_puts(p, "  PowerPC             Thermal Assist (cpu temp)\n");
401                 }
402 #endif
403 #ifdef CONFIG_SMP
404                 /* should this be per processor send/receive? */
405                 seq_printf(p, "IPI (recv/sent): %10u/%u\n",
406                                 atomic_read(&ipi_recv), atomic_read(&ipi_sent));
407 #endif
408                 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
409         }
410         return 0;
411 }
412
413 static inline void
414 handle_irq_event(int irq, struct pt_regs *regs, struct irqaction *action)
415 {
416         int status = 0;
417
418         if (!(action->flags & SA_INTERRUPT))
419                 local_irq_enable();
420
421         do {
422                 status |= action->flags;
423                 action->handler(irq, action->dev_id, regs);
424                 action = action->next;
425         } while (action);
426         if (status & SA_SAMPLE_RANDOM)
427                 add_interrupt_randomness(irq);
428         local_irq_disable();
429 }
430
431 /*
432  * Eventually, this should take an array of interrupts and an array size
433  * so it can dispatch multiple interrupts.
434  */
435 void ppc_irq_dispatch_handler(struct pt_regs *regs, int irq)
436 {
437         int status;
438         struct irqaction *action;
439         irq_desc_t *desc = irq_desc + irq;
440
441         kstat_this_cpu.irqs[irq]++;
442         spin_lock(&desc->lock);
443         ack_irq(irq);
444         /*
445            REPLAY is when Linux resends an IRQ that was dropped earlier
446            WAITING is used by probe to mark irqs that are being tested
447            */
448         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
449         if (!(status & IRQ_PER_CPU))
450                 status |= IRQ_PENDING; /* we _want_ to handle it */
451
452         /*
453          * If the IRQ is disabled for whatever reason, we cannot
454          * use the action we have.
455          */
456         action = NULL;
457         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
458                 action = desc->action;
459                 if (!action || !action->handler) {
460                         ppc_spurious_interrupts++;
461                         printk(KERN_DEBUG "Unhandled interrupt %x, disabled\n", irq);
462                         /* We can't call disable_irq here, it would deadlock */
463                         ++desc->depth;
464                         desc->status |= IRQ_DISABLED;
465                         mask_irq(irq);
466                         /* This is a real interrupt, we have to eoi it,
467                            so we jump to out */
468                         goto out;
469                 }
470                 status &= ~IRQ_PENDING; /* we commit to handling */
471                 if (!(status & IRQ_PER_CPU))
472                         status |= IRQ_INPROGRESS; /* we are handling it */
473         }
474         desc->status = status;
475
476         /*
477          * If there is no IRQ handler or it was disabled, exit early.
478            Since we set PENDING, if another processor is handling
479            a different instance of this same irq, the other processor
480            will take care of it.
481          */
482         if (unlikely(!action))
483                 goto out;
484
485
486         /*
487          * Edge triggered interrupts need to remember
488          * pending events.
489          * This applies to any hw interrupts that allow a second
490          * instance of the same irq to arrive while we are in do_IRQ
491          * or in the handler. But the code here only handles the _second_
492          * instance of the irq, not the third or fourth. So it is mostly
493          * useful for irq hardware that does not mask cleanly in an
494          * SMP environment.
495          */
496         for (;;) {
497                 spin_unlock(&desc->lock);
498                 handle_irq_event(irq, regs, action);
499                 spin_lock(&desc->lock);
500
501                 if (likely(!(desc->status & IRQ_PENDING)))
502                         break;
503                 desc->status &= ~IRQ_PENDING;
504         }
505 out:
506         desc->status &= ~IRQ_INPROGRESS;
507         /*
508          * The ->end() handler has to deal with interrupts which got
509          * disabled while the handler was running.
510          */
511         if (irq_desc[irq].handler) {
512                 if (irq_desc[irq].handler->end)
513                         irq_desc[irq].handler->end(irq);
514                 else if (irq_desc[irq].handler->enable)
515                         irq_desc[irq].handler->enable(irq);
516         }
517         spin_unlock(&desc->lock);
518 }
519
520 void do_IRQ(struct pt_regs *regs)
521 {
522         int irq, first = 1;
523         irq_enter();
524
525         /*
526          * Every platform is required to implement ppc_md.get_irq.
527          * This function will either return an irq number or -1 to
528          * indicate there are no more pending.  But the first time
529          * through the loop this means there wasn't and IRQ pending.
530          * The value -2 is for buggy hardware and means that this IRQ
531          * has already been handled. -- Tom
532          */
533         while ((irq = ppc_md.get_irq(regs)) >= 0) {
534                 ppc_irq_dispatch_handler(regs, irq);
535                 first = 0;
536         }
537         if (irq != -2 && first)
538                 /* That's not SMP safe ... but who cares ? */
539                 ppc_spurious_interrupts++;
540         irq_exit();
541 }
542
543 unsigned long probe_irq_on (void)
544 {
545         return 0;
546 }
547
548 EXPORT_SYMBOL(probe_irq_on);
549
550 int probe_irq_off (unsigned long irqs)
551 {
552         return 0;
553 }
554
555 EXPORT_SYMBOL(probe_irq_off);
556
557 unsigned int probe_irq_mask(unsigned long irqs)
558 {
559         return 0;
560 }
561
562 #ifdef CONFIG_SMP
563 void synchronize_irq(unsigned int irq)
564 {
565         while (irq_desc[irq].status & IRQ_INPROGRESS)
566                 barrier();
567 }
568 #endif /* CONFIG_SMP */
569
570 static struct proc_dir_entry *root_irq_dir;
571 static struct proc_dir_entry *irq_dir[NR_IRQS];
572 static struct proc_dir_entry *smp_affinity_entry[NR_IRQS];
573
574 #ifdef CONFIG_IRQ_ALL_CPUS
575 #define DEFAULT_CPU_AFFINITY CPU_MASK_ALL
576 #else
577 #define DEFAULT_CPU_AFFINITY cpumask_of_cpu(0)
578 #endif
579
580 cpumask_t irq_affinity [NR_IRQS];
581
582 static int irq_affinity_read_proc (char *page, char **start, off_t off,
583                         int count, int *eof, void *data)
584 {
585         int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
586         if (count - len < 2)
587                 return -EINVAL;
588         len += sprintf(page + len, "\n");
589         return len;
590 }
591
592 static int irq_affinity_write_proc (struct file *file, const char __user *buffer,
593                                         unsigned long count, void *data)
594 {
595         int irq = (int) data, full_count = count, err;
596         cpumask_t new_value, tmp;
597
598         if (!irq_desc[irq].handler->set_affinity)
599                 return -EIO;
600
601         err = cpumask_parse(buffer, count, new_value);
602
603         /*
604          * Do not allow disabling IRQs completely - it's a too easy
605          * way to make the system unusable accidentally :-) At least
606          * one online CPU still has to be targeted.
607          *
608          * We assume a 1-1 logical<->physical cpu mapping here.  If
609          * we assume that the cpu indices in /proc/irq/../smp_affinity
610          * are actually logical cpu #'s then we have no problem.
611          *  -- Cort <cort@fsmlabs.com>
612          */
613         cpus_and(tmp, new_value, cpu_online_map);
614         if (cpus_empty(tmp))
615                 return -EINVAL;
616
617         irq_affinity[irq] = new_value;
618         irq_desc[irq].handler->set_affinity(irq, new_value);
619
620         return full_count;
621 }
622
623 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
624                         int count, int *eof, void *data)
625 {
626         int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
627         if (count - len < 2)
628                 return -EINVAL;
629         len += sprintf(page + len, "\n");
630         return len;
631 }
632
633 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
634                                         unsigned long count, void *data)
635 {
636         int err;
637         int full_count = count;
638         cpumask_t *mask = (cpumask_t *)data;
639         cpumask_t new_value;
640
641         err = cpumask_parse(buffer, count, new_value);
642         if (err)
643                 return err;
644
645         *mask = new_value;
646         return full_count;
647 }
648
649 #define MAX_NAMELEN 10
650
651 static void register_irq_proc (unsigned int irq)
652 {
653         struct proc_dir_entry *entry;
654         char name [MAX_NAMELEN];
655
656         if (!root_irq_dir || (irq_desc[irq].handler == NULL) || irq_dir[irq])
657                 return;
658
659         memset(name, 0, MAX_NAMELEN);
660         sprintf(name, "%d", irq);
661
662         /* create /proc/irq/1234 */
663         irq_dir[irq] = proc_mkdir(name, root_irq_dir);
664
665         /* create /proc/irq/1234/smp_affinity */
666         entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
667
668         entry->nlink = 1;
669         entry->data = (void *)irq;
670         entry->read_proc = irq_affinity_read_proc;
671         entry->write_proc = irq_affinity_write_proc;
672
673         smp_affinity_entry[irq] = entry;
674 }
675
676 unsigned long prof_cpu_mask = -1;
677
678 void init_irq_proc (void)
679 {
680         struct proc_dir_entry *entry;
681         int i;
682
683         /* create /proc/irq */
684         root_irq_dir = proc_mkdir("irq", NULL);
685
686         /* create /proc/irq/prof_cpu_mask */
687         entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
688
689         entry->nlink = 1;
690         entry->data = (void *)&prof_cpu_mask;
691         entry->read_proc = prof_cpu_mask_read_proc;
692         entry->write_proc = prof_cpu_mask_write_proc;
693
694         /*
695          * Create entries for all existing IRQs.
696          */
697         for (i = 0; i < NR_IRQS; i++) {
698                 if (irq_desc[i].handler == NULL)
699                         continue;
700                 register_irq_proc(i);
701         }
702 }
703
704 irqreturn_t no_action(int irq, void *dev, struct pt_regs *regs)
705 {
706         return IRQ_NONE;
707 }
708
709 void __init init_IRQ(void)
710 {
711         int i;
712
713         for (i = 0; i < NR_IRQS; ++i)
714                 irq_affinity[i] = DEFAULT_CPU_AFFINITY;
715
716         ppc_md.init_IRQ();
717 }