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