Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / arch / arm26 / kernel / irq.c
1 /*
2  *  linux/arch/arm/kernel/irq.c
3  *
4  *  Copyright (C) 1992 Linus Torvalds
5  *  Modifications for ARM processor Copyright (C) 1995-2000 Russell King.
6  *  'Borrowed' for ARM26 and (C) 2003 Ian Molton.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  *  This file contains the code used by various IRQ handling routines:
13  *  asking for different IRQ's should be done through these routines
14  *  instead of just grabbing them. Thus setups with different IRQ numbers
15  *  shouldn't result in any weird surprises, and installing new handlers
16  *  should be easier.
17  *
18  *  IRQ's are in fact implemented a bit like signal handlers for the kernel.
19  *  Naturally it's not a 1:1 relation, but there are similarities.
20  */
21 #include <linux/module.h>
22 #include <linux/ptrace.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/ioport.h>
27 #include <linux/interrupt.h>
28 #include <linux/slab.h>
29 #include <linux/random.h>
30 #include <linux/smp.h>
31 #include <linux/init.h>
32 #include <linux/seq_file.h>
33 #include <linux/errno.h>
34 #include <linux/vs_context.h>
35
36 #include <asm/irq.h>
37 #include <asm/system.h>
38 #include <asm/irqchip.h>
39
40 //FIXME - this ought to be in a header IMO
41 void __init arc_init_irq(void);
42
43 /*
44  * Maximum IRQ count.  Currently, this is arbitary.  However, it should
45  * not be set too low to prevent false triggering.  Conversely, if it
46  * is set too high, then you could miss a stuck IRQ.
47  *
48  * FIXME Maybe we ought to set a timer and re-enable the IRQ at a later time?
49  */
50 #define MAX_IRQ_CNT     100000
51
52 static volatile unsigned long irq_err_count;
53 static DEFINE_SPINLOCK(irq_controller_lock);
54
55 struct irqdesc irq_desc[NR_IRQS];
56
57 /*
58  * Dummy mask/unmask handler
59  */
60 void dummy_mask_unmask_irq(unsigned int irq)
61 {
62 }
63
64 void do_bad_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
65 {
66         irq_err_count += 1;
67         printk(KERN_ERR "IRQ: spurious interrupt %d\n", irq);
68 }
69
70 static struct irqchip bad_chip = {
71         .ack    = dummy_mask_unmask_irq,
72         .mask   = dummy_mask_unmask_irq,
73         .unmask = dummy_mask_unmask_irq,
74 };
75
76 static struct irqdesc bad_irq_desc = {
77         .chip   = &bad_chip,
78         .handle = do_bad_IRQ,
79         .depth  = 1,
80 };
81
82 /**
83  *      disable_irq - disable an irq and wait for completion
84  *      @irq: Interrupt to disable
85  *
86  *      Disable the selected interrupt line.  We do this lazily.
87  *
88  *      This function may be called from IRQ context.
89  */
90 void disable_irq(unsigned int irq)
91 {
92         struct irqdesc *desc = irq_desc + irq;
93         unsigned long flags;
94         spin_lock_irqsave(&irq_controller_lock, flags);
95         if (!desc->depth++)
96                 desc->enabled = 0;
97         spin_unlock_irqrestore(&irq_controller_lock, flags);
98 }
99
100 /**
101  *      enable_irq - enable interrupt handling on an irq
102  *      @irq: Interrupt to enable
103  *
104  *      Re-enables the processing of interrupts on this IRQ line.
105  *      Note that this may call the interrupt handler, so you may
106  *      get unexpected results if you hold IRQs disabled.
107  *
108  *      This function may be called from IRQ context.
109  */
110 void enable_irq(unsigned int irq)
111 {
112         struct irqdesc *desc = irq_desc + irq;
113         unsigned long flags;
114         int pending = 0;
115
116         spin_lock_irqsave(&irq_controller_lock, flags);
117         if (unlikely(!desc->depth)) {
118                 printk("enable_irq(%u) unbalanced from %p\n", irq,
119                         __builtin_return_address(0)); //FIXME bum addresses reported - why?
120         } else if (!--desc->depth) {
121                 desc->probing = 0;
122                 desc->enabled = 1;
123                 desc->chip->unmask(irq);
124                 pending = desc->pending;
125                 desc->pending = 0;
126                 /*
127                  * If the interrupt was waiting to be processed,
128                  * retrigger it.
129                  */
130                 if (pending)
131                         desc->chip->rerun(irq);
132         }
133         spin_unlock_irqrestore(&irq_controller_lock, flags);
134 }
135
136 int show_interrupts(struct seq_file *p, void *v)
137 {
138         int i = *(loff_t *) v;
139         struct irqaction * action;
140
141         if (i < NR_IRQS) {
142                 action = irq_desc[i].action;
143                 if (!action)
144                         goto out;
145                 seq_printf(p, "%3d: %10u ", i, kstat_irqs(i));
146                 seq_printf(p, "  %s", action->name);
147                 for (action = action->next; action; action = action->next) {
148                         seq_printf(p, ", %s", action->name);
149                 }
150                 seq_putc(p, '\n');
151         } else if (i == NR_IRQS) {
152                 show_fiq_list(p, v);
153                 seq_printf(p, "Err: %10lu\n", irq_err_count);
154         }
155 out:
156         return 0;
157 }
158
159 /*
160  * IRQ lock detection.
161  *
162  * Hopefully, this should get us out of a few locked situations.
163  * However, it may take a while for this to happen, since we need
164  * a large number if IRQs to appear in the same jiffie with the
165  * same instruction pointer (or within 2 instructions).
166  */
167 static int check_irq_lock(struct irqdesc *desc, int irq, struct pt_regs *regs)
168 {
169         unsigned long instr_ptr = instruction_pointer(regs);
170
171         if (desc->lck_jif == jiffies &&
172             desc->lck_pc >= instr_ptr && desc->lck_pc < instr_ptr + 8) {
173                 desc->lck_cnt += 1;
174
175                 if (desc->lck_cnt > MAX_IRQ_CNT) {
176                         printk(KERN_ERR "IRQ LOCK: IRQ%d is locking the system, disabled\n", irq);
177                         return 1;
178                 }
179         } else {
180                 desc->lck_cnt = 0;
181                 desc->lck_pc  = instruction_pointer(regs);
182                 desc->lck_jif = jiffies;
183         }
184         return 0;
185 }
186
187 static void
188 __do_irq(unsigned int irq, struct irqaction *action, struct pt_regs *regs)
189 {
190         unsigned int status;
191         int ret;
192
193         spin_unlock(&irq_controller_lock);
194         if (!(action->flags & IRQF_DISABLED))
195                 local_irq_enable();
196
197         status = 0;
198         do {
199                 ret = action->handler(irq, action->dev_id, regs);
200                 if (ret == IRQ_HANDLED)
201                         status |= action->flags;
202                 action = action->next;
203         } while (action);
204
205         if (status & IRQF_SAMPLE_RANDOM)
206                 add_interrupt_randomness(irq);
207
208         spin_lock_irq(&irq_controller_lock);
209 }
210
211 /*
212  * This is for software-decoded IRQs.  The caller is expected to
213  * handle the ack, clear, mask and unmask issues.
214  */
215 void
216 do_simple_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
217 {
218         struct irqaction *action;
219         const int cpu = smp_processor_id();
220
221         desc->triggered = 1;
222
223         kstat_cpu(cpu).irqs[irq]++;
224
225         action = desc->action;
226         if (action)
227                 __do_irq(irq, desc->action, regs);
228 }
229
230 /*
231  * Most edge-triggered IRQ implementations seem to take a broken
232  * approach to this.  Hence the complexity.
233  */
234 void
235 do_edge_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
236 {
237         const int cpu = smp_processor_id();
238
239         desc->triggered = 1;
240
241         /*
242          * If we're currently running this IRQ, or its disabled,
243          * we shouldn't process the IRQ.  Instead, turn on the
244          * hardware masks.
245          */
246         if (unlikely(desc->running || !desc->enabled))
247                 goto running;
248
249         /*
250          * Acknowledge and clear the IRQ, but don't mask it.
251          */
252         desc->chip->ack(irq);
253
254         /*
255          * Mark the IRQ currently in progress.
256          */
257         desc->running = 1;
258
259         kstat_cpu(cpu).irqs[irq]++;
260
261         do {
262                 struct irqaction *action;
263
264                 action = desc->action;
265                 if (!action)
266                         break;
267
268                 if (desc->pending && desc->enabled) {
269                         desc->pending = 0;
270                         desc->chip->unmask(irq);
271                 }
272
273                 __do_irq(irq, action, regs);
274         } while (desc->pending);
275
276         desc->running = 0;
277
278         /*
279          * If we were disabled or freed, shut down the handler.
280          */
281         if (likely(desc->action && !check_irq_lock(desc, irq, regs)))
282                 return;
283
284  running:
285         /*
286          * We got another IRQ while this one was masked or
287          * currently running.  Delay it.
288          */
289         desc->pending = 1;
290         desc->chip->mask(irq);
291         desc->chip->ack(irq);
292 }
293
294 /*
295  * Level-based IRQ handler.  Nice and simple.
296  */
297 void
298 do_level_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
299 {
300         struct irqaction *action;
301         const int cpu = smp_processor_id();
302
303         desc->triggered = 1;
304
305         /*
306          * Acknowledge, clear _AND_ disable the interrupt.
307          */
308         desc->chip->ack(irq);
309
310         if (likely(desc->enabled)) {
311                 kstat_cpu(cpu).irqs[irq]++;
312
313                 /*
314                  * Return with this interrupt masked if no action
315                  */
316                 action = desc->action;
317                 if (action) {
318                         __do_irq(irq, desc->action, regs);
319
320                         if (likely(desc->enabled &&
321                                    !check_irq_lock(desc, irq, regs)))
322                                 desc->chip->unmask(irq);
323                 }
324         }
325 }
326
327 /*
328  * do_IRQ handles all hardware IRQ's.  Decoded IRQs should not
329  * come via this function.  Instead, they should provide their
330  * own 'handler'
331  */
332 asmlinkage void asm_do_IRQ(int irq, struct pt_regs *regs)
333 {
334         struct irqdesc *desc = irq_desc + irq;
335         struct vx_info_save vxis;
336
337         /*
338          * Some hardware gives randomly wrong interrupts.  Rather
339          * than crashing, do something sensible.
340          */
341         if (irq >= NR_IRQS)
342                 desc = &bad_irq_desc;
343
344         irq_enter();
345         spin_lock(&irq_controller_lock);
346         __enter_vx_admin(&vxis);
347         desc->handle(irq, desc, regs);
348         __leave_vx_admin(&vxis);
349         spin_unlock(&irq_controller_lock);
350         irq_exit();
351 }
352
353 void __set_irq_handler(unsigned int irq, irq_handler_t handle, int is_chained)
354 {
355         struct irqdesc *desc;
356         unsigned long flags;
357
358         if (irq >= NR_IRQS) {
359                 printk(KERN_ERR "Trying to install handler for IRQ%d\n", irq);
360                 return;
361         }
362
363         if (handle == NULL)
364                 handle = do_bad_IRQ;
365
366         desc = irq_desc + irq;
367
368         if (is_chained && desc->chip == &bad_chip)
369                 printk(KERN_WARNING "Trying to install chained handler for IRQ%d\n", irq);
370
371         spin_lock_irqsave(&irq_controller_lock, flags);
372         if (handle == do_bad_IRQ) {
373                 desc->chip->mask(irq);
374                 desc->chip->ack(irq);
375                 desc->depth = 1;
376                 desc->enabled = 0;
377         }
378         desc->handle = handle;
379         if (handle != do_bad_IRQ && is_chained) {
380                 desc->valid = 0;
381                 desc->probe_ok = 0;
382                 desc->depth = 0;
383                 desc->chip->unmask(irq);
384         }
385         spin_unlock_irqrestore(&irq_controller_lock, flags);
386 }
387
388 void set_irq_chip(unsigned int irq, struct irqchip *chip)
389 {
390         struct irqdesc *desc;
391         unsigned long flags;
392
393         if (irq >= NR_IRQS) {
394                 printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
395                 return;
396         }
397
398         if (chip == NULL)
399                 chip = &bad_chip;
400
401         desc = irq_desc + irq;
402         spin_lock_irqsave(&irq_controller_lock, flags);
403         desc->chip = chip;
404         spin_unlock_irqrestore(&irq_controller_lock, flags);
405 }
406
407 int set_irq_type(unsigned int irq, unsigned int type)
408 {
409         struct irqdesc *desc;
410         unsigned long flags;
411         int ret = -ENXIO;
412
413         if (irq >= NR_IRQS) {
414                 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
415                 return -ENODEV;
416         }
417
418         desc = irq_desc + irq;
419         if (desc->chip->type) {
420                 spin_lock_irqsave(&irq_controller_lock, flags);
421                 ret = desc->chip->type(irq, type);
422                 spin_unlock_irqrestore(&irq_controller_lock, flags);
423         }
424
425         return ret;
426 }
427
428 void set_irq_flags(unsigned int irq, unsigned int iflags)
429 {
430         struct irqdesc *desc;
431         unsigned long flags;
432
433         if (irq >= NR_IRQS) {
434                 printk(KERN_ERR "Trying to set irq flags for IRQ%d\n", irq);
435                 return;
436         }
437
438         desc = irq_desc + irq;
439         spin_lock_irqsave(&irq_controller_lock, flags);
440         desc->valid = (iflags & IRQF_VALID) != 0;
441         desc->probe_ok = (iflags & IRQF_PROBE) != 0;
442         desc->noautoenable = (iflags & IRQF_NOAUTOEN) != 0;
443         spin_unlock_irqrestore(&irq_controller_lock, flags);
444 }
445
446 int setup_irq(unsigned int irq, struct irqaction *new)
447 {
448         int shared = 0;
449         struct irqaction *old, **p;
450         unsigned long flags;
451         struct irqdesc *desc;
452
453         /*
454          * Some drivers like serial.c use request_irq() heavily,
455          * so we have to be careful not to interfere with a
456          * running system.
457          */
458         if (new->flags & IRQF_SAMPLE_RANDOM) {
459                 /*
460                  * This function might sleep, we want to call it first,
461                  * outside of the atomic block.
462                  * Yes, this might clear the entropy pool if the wrong
463                  * driver is attempted to be loaded, without actually
464                  * installing a new handler, but is this really a problem,
465                  * only the sysadmin is able to do this.
466                  */
467                 rand_initialize_irq(irq);
468         }
469
470         /*
471          * The following block of code has to be executed atomically
472          */
473         desc = irq_desc + irq;
474         spin_lock_irqsave(&irq_controller_lock, flags);
475         p = &desc->action;
476         if ((old = *p) != NULL) {
477                 /* Can't share interrupts unless both agree to */
478                 if (!(old->flags & new->flags & IRQF_SHARED)) {
479                         spin_unlock_irqrestore(&irq_controller_lock, flags);
480                         return -EBUSY;
481                 }
482
483                 /* add new interrupt at end of irq queue */
484                 do {
485                         p = &old->next;
486                         old = *p;
487                 } while (old);
488                 shared = 1;
489         }
490
491         *p = new;
492
493         if (!shared) {
494                 desc->probing = 0;
495                 desc->running = 0;
496                 desc->pending = 0;
497                 desc->depth = 1;
498                 if (!desc->noautoenable) {
499                         desc->depth = 0;
500                         desc->enabled = 1;
501                         desc->chip->unmask(irq);
502                 }
503         }
504
505         spin_unlock_irqrestore(&irq_controller_lock, flags);
506         return 0;
507 }
508
509 /**
510  *      request_irq - allocate an interrupt line
511  *      @irq: Interrupt line to allocate
512  *      @handler: Function to be called when the IRQ occurs
513  *      @irqflags: Interrupt type flags
514  *      @devname: An ascii name for the claiming device
515  *      @dev_id: A cookie passed back to the handler function
516  *
517  *      This call allocates interrupt resources and enables the
518  *      interrupt line and IRQ handling. From the point this
519  *      call is made your handler function may be invoked. Since
520  *      your handler function must clear any interrupt the board
521  *      raises, you must take care both to initialise your hardware
522  *      and to set up the interrupt handler in the right order.
523  *
524  *      Dev_id must be globally unique. Normally the address of the
525  *      device data structure is used as the cookie. Since the handler
526  *      receives this value it makes sense to use it.
527  *
528  *      If your interrupt is shared you must pass a non NULL dev_id
529  *      as this is required when freeing the interrupt.
530  *
531  *      Flags:
532  *
533  *      IRQF_SHARED             Interrupt is shared
534  *
535  *      IRQF_DISABLED   Disable local interrupts while processing
536  *
537  *      IRQF_SAMPLE_RANDOM      The interrupt can be used for entropy
538  *
539  */
540
541 //FIXME - handler used to return void - whats the significance of the change?
542 int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
543                  unsigned long irq_flags, const char * devname, void *dev_id)
544 {
545         unsigned long retval;
546         struct irqaction *action;
547
548         if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
549             (irq_flags & IRQF_SHARED && !dev_id))
550                 return -EINVAL;
551
552         action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
553         if (!action)
554                 return -ENOMEM;
555
556         action->handler = handler;
557         action->flags = irq_flags;
558         cpus_clear(action->mask);
559         action->name = devname;
560         action->next = NULL;
561         action->dev_id = dev_id;
562
563         retval = setup_irq(irq, action);
564
565         if (retval)
566                 kfree(action);
567         return retval;
568 }
569
570 EXPORT_SYMBOL(request_irq);
571
572 /**
573  *      free_irq - free an interrupt
574  *      @irq: Interrupt line to free
575  *      @dev_id: Device identity to free
576  *
577  *      Remove an interrupt handler. The handler is removed and if the
578  *      interrupt line is no longer in use by any driver it is disabled.
579  *      On a shared IRQ the caller must ensure the interrupt is disabled
580  *      on the card it drives before calling this function.
581  *
582  *      This function may be called from interrupt context.
583  */
584 void free_irq(unsigned int irq, void *dev_id)
585 {
586         struct irqaction * action, **p;
587         unsigned long flags;
588
589         if (irq >= NR_IRQS || !irq_desc[irq].valid) {
590                 printk(KERN_ERR "Trying to free IRQ%d\n",irq);
591 #ifdef CONFIG_DEBUG_ERRORS
592                 __backtrace();
593 #endif
594                 return;
595         }
596
597         spin_lock_irqsave(&irq_controller_lock, flags);
598         for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
599                 if (action->dev_id != dev_id)
600                         continue;
601
602                 /* Found it - now free it */
603                 *p = action->next;
604                 kfree(action);
605                 goto out;
606         }
607         printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
608 #ifdef CONFIG_DEBUG_ERRORS
609         __backtrace();
610 #endif
611 out:
612         spin_unlock_irqrestore(&irq_controller_lock, flags);
613 }
614
615 EXPORT_SYMBOL(free_irq);
616
617 /* Start the interrupt probing.  Unlike other architectures,
618  * we don't return a mask of interrupts from probe_irq_on,
619  * but return the number of interrupts enabled for the probe.
620  * The interrupts which have been enabled for probing is
621  * instead recorded in the irq_desc structure.
622  */
623 unsigned long probe_irq_on(void)
624 {
625         unsigned int i, irqs = 0;
626         unsigned long delay;
627
628         /*
629          * first snaffle up any unassigned but
630          * probe-able interrupts
631          */
632         spin_lock_irq(&irq_controller_lock);
633         for (i = 0; i < NR_IRQS; i++) {
634                 if (!irq_desc[i].probe_ok || irq_desc[i].action)
635                         continue;
636
637                 irq_desc[i].probing = 1;
638                 irq_desc[i].triggered = 0;
639                 if (irq_desc[i].chip->type)
640                         irq_desc[i].chip->type(i, IRQT_PROBE);
641                 irq_desc[i].chip->unmask(i);
642                 irqs += 1;
643         }
644         spin_unlock_irq(&irq_controller_lock);
645
646         /*
647          * wait for spurious interrupts to mask themselves out again
648          */
649         for (delay = jiffies + HZ/10; time_before(jiffies, delay); )
650                 /* min 100ms delay */;
651
652         /*
653          * now filter out any obviously spurious interrupts
654          */
655         spin_lock_irq(&irq_controller_lock);
656         for (i = 0; i < NR_IRQS; i++) {
657                 if (irq_desc[i].probing && irq_desc[i].triggered) {
658                         irq_desc[i].probing = 0;
659                         irqs -= 1;
660                 }
661         }
662         spin_unlock_irq(&irq_controller_lock);
663
664         return irqs;
665 }
666
667 EXPORT_SYMBOL(probe_irq_on);
668
669 /*
670  * Possible return values:
671  *  >= 0 - interrupt number
672  *    -1 - no interrupt/many interrupts
673  */
674 int probe_irq_off(unsigned long irqs)
675 {
676         unsigned int i;
677         int irq_found = NO_IRQ;
678
679         /*
680          * look at the interrupts, and find exactly one
681          * that we were probing has been triggered
682          */
683         spin_lock_irq(&irq_controller_lock);
684         for (i = 0; i < NR_IRQS; i++) {
685                 if (irq_desc[i].probing &&
686                     irq_desc[i].triggered) {
687                         if (irq_found != NO_IRQ) {
688                                 irq_found = NO_IRQ;
689                                 goto out;
690                         }
691                         irq_found = i;
692                 }
693         }
694
695         if (irq_found == -1)
696                 irq_found = NO_IRQ;
697 out:
698         spin_unlock_irq(&irq_controller_lock);
699
700         return irq_found;
701 }
702
703 EXPORT_SYMBOL(probe_irq_off);
704
705 void __init init_irq_proc(void)
706 {
707 }
708
709 void __init init_IRQ(void)
710 {
711         struct irqdesc *desc;
712         extern void init_dma(void);
713         int irq;
714
715         for (irq = 0, desc = irq_desc; irq < NR_IRQS; irq++, desc++)
716                 *desc = bad_irq_desc;
717
718         arc_init_irq();
719         init_dma();
720 }