2 * linux/arch/arm/kernel/irq.c
4 * Copyright (C) 1992 Linus Torvalds
5 * Modifications for ARM processor Copyright (C) 1995-2000 Russell King.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This file contains the code used by various IRQ handling routines:
12 * asking for different IRQ's should be done through these routines
13 * instead of just grabbing them. Thus setups with different IRQ numbers
14 * shouldn't result in any weird surprises, and installing new handlers
17 * IRQ's are in fact implemented a bit like signal handlers for the kernel.
18 * Naturally it's not a 1:1 relation, but there are similarities.
20 #include <linux/config.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/module.h>
23 #include <linux/signal.h>
24 #include <linux/ioport.h>
25 #include <linux/interrupt.h>
26 #include <linux/ptrace.h>
27 #include <linux/slab.h>
28 #include <linux/random.h>
29 #include <linux/smp.h>
30 #include <linux/init.h>
31 #include <linux/seq_file.h>
32 #include <linux/errno.h>
33 #include <linux/list.h>
34 #include <linux/kallsyms.h>
37 #include <asm/system.h>
38 #include <asm/mach/irq.h>
39 #ifdef CONFIG_ARCH_MOXACPU
40 #include <asm/arch/irq.h>
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.
48 * Maybe we ought to set a timer and re-enable the IRQ at a later time?
50 #define MAX_IRQ_CNT 100000
52 static int noirqdebug
;
53 static volatile unsigned long irq_err_count
;
54 static spinlock_t irq_controller_lock
= SPIN_LOCK_UNLOCKED
;
55 static LIST_HEAD(irq_pending
);
57 struct irqdesc irq_desc
[NR_IRQS
];
58 void (*init_arch_irq
)(void) __initdata
= NULL
;
61 * Dummy mask/unmask handler
63 void dummy_mask_unmask_irq(unsigned int irq
)
67 irqreturn_t
no_action(int irq
, void *dev_id
, struct pt_regs
*regs
)
72 void do_bad_IRQ(unsigned int irq
, struct irqdesc
*desc
, struct pt_regs
*regs
)
75 printk(KERN_ERR
"IRQ: spurious interrupt %d\n", irq
);
78 static struct irqchip bad_chip
= {
79 .ack
= dummy_mask_unmask_irq
,
80 .mask
= dummy_mask_unmask_irq
,
81 .unmask
= dummy_mask_unmask_irq
,
84 static struct irqdesc bad_irq_desc
= {
87 .pend
= LIST_HEAD_INIT(bad_irq_desc
.pend
),
92 * disable_irq - disable an irq and wait for completion
93 * @irq: Interrupt to disable
95 * Disable the selected interrupt line. Enables and disables
96 * are nested. We do this lazily.
98 * This function may be called from IRQ context.
100 void disable_irq(unsigned int irq
)
102 struct irqdesc
*desc
= irq_desc
+ irq
;
105 spin_lock_irqsave(&irq_controller_lock
, flags
);
106 desc
->disable_depth
++;
107 list_del_init(&desc
->pend
);
108 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
110 EXPORT_SYMBOL(disable_irq
);
113 * enable_irq - enable interrupt handling on an irq
114 * @irq: Interrupt to enable
116 * Re-enables the processing of interrupts on this IRQ line.
117 * Note that this may call the interrupt handler, so you may
118 * get unexpected results if you hold IRQs disabled.
120 * This function may be called from IRQ context.
122 void enable_irq(unsigned int irq
)
124 struct irqdesc
*desc
= irq_desc
+ irq
;
127 spin_lock_irqsave(&irq_controller_lock
, flags
);
128 if (unlikely(!desc
->disable_depth
)) {
129 printk("enable_irq(%u) unbalanced from %p\n", irq
,
130 __builtin_return_address(0));
131 } else if (!--desc
->disable_depth
) {
133 desc
->chip
->unmask(irq
);
136 * If the interrupt is waiting to be processed,
137 * try to re-run it. We can't directly run it
138 * from here since the caller might be in an
139 * interrupt-protected region.
141 if (desc
->pending
&& list_empty(&desc
->pend
)) {
143 if (!desc
->chip
->retrigger
||
144 desc
->chip
->retrigger(irq
))
145 list_add(&desc
->pend
, &irq_pending
);
148 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
150 EXPORT_SYMBOL(enable_irq
);
153 * Enable wake on selected irq
155 void enable_irq_wake(unsigned int irq
)
157 struct irqdesc
*desc
= irq_desc
+ irq
;
160 spin_lock_irqsave(&irq_controller_lock
, flags
);
161 if (desc
->chip
->wake
)
162 desc
->chip
->wake(irq
, 1);
163 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
165 EXPORT_SYMBOL(enable_irq_wake
);
167 void disable_irq_wake(unsigned int irq
)
169 struct irqdesc
*desc
= irq_desc
+ irq
;
172 spin_lock_irqsave(&irq_controller_lock
, flags
);
173 if (desc
->chip
->wake
)
174 desc
->chip
->wake(irq
, 0);
175 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
177 EXPORT_SYMBOL(disable_irq_wake
);
179 int show_interrupts(struct seq_file
*p
, void *v
)
181 int i
= *(loff_t
*) v
;
182 struct irqaction
* action
;
186 spin_lock_irqsave(&irq_controller_lock
, flags
);
187 action
= irq_desc
[i
].action
;
191 seq_printf(p
, "%3d: %10u ", i
, kstat_irqs(i
));
192 seq_printf(p
, " %s", action
->name
);
193 for (action
= action
->next
; action
; action
= action
->next
)
194 seq_printf(p
, ", %s", action
->name
);
198 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
199 } else if (i
== NR_IRQS
) {
200 #ifdef CONFIG_ARCH_ACORN
203 seq_printf(p
, "Err: %10lu\n", irq_err_count
);
209 * IRQ lock detection.
211 * Hopefully, this should get us out of a few locked situations.
212 * However, it may take a while for this to happen, since we need
213 * a large number if IRQs to appear in the same jiffie with the
214 * same instruction pointer (or within 2 instructions).
216 static int check_irq_lock(struct irqdesc
*desc
, int irq
, struct pt_regs
*regs
)
218 unsigned long instr_ptr
= instruction_pointer(regs
);
220 if (desc
->lck_jif
== jiffies
&&
221 desc
->lck_pc
>= instr_ptr
&& desc
->lck_pc
< instr_ptr
+ 8) {
224 if (desc
->lck_cnt
> MAX_IRQ_CNT
) {
225 printk(KERN_ERR
"IRQ LOCK: IRQ%d is locking the system, disabled\n", irq
);
230 desc
->lck_pc
= instruction_pointer(regs
);
231 desc
->lck_jif
= jiffies
;
237 report_bad_irq(unsigned int irq
, struct pt_regs
*regs
, struct irqdesc
*desc
, int ret
)
239 static int count
= 100;
240 struct irqaction
*action
;
242 if (!count
|| noirqdebug
)
247 if (ret
!= IRQ_HANDLED
&& ret
!= IRQ_NONE
) {
248 printk("irq%u: bogus retval mask %x\n", irq
, ret
);
250 printk("irq%u: nobody cared\n", irq
);
254 printk(KERN_ERR
"handlers:");
255 action
= desc
->action
;
257 printk("\n" KERN_ERR
"[<%p>]", action
->handler
);
258 print_symbol(" (%s)", (unsigned long)action
->handler
);
259 action
= action
->next
;
265 __do_irq(unsigned int irq
, struct irqaction
*action
, struct pt_regs
*regs
)
270 spin_unlock(&irq_controller_lock
);
272 if (!(action
->flags
& SA_INTERRUPT
)) {
278 ret
= action
->handler(irq
, action
->dev_id
, regs
);
279 if (ret
== IRQ_HANDLED
) {
280 status
|= action
->flags
;
283 action
= action
->next
;
286 if (status
& SA_SAMPLE_RANDOM
) {
287 add_interrupt_randomness(irq
);
290 spin_lock_irq(&irq_controller_lock
);
296 * This is for software-decoded IRQs. The caller is expected to
297 * handle the ack, clear, mask and unmask issues.
300 do_simple_IRQ(unsigned int irq
, struct irqdesc
*desc
, struct pt_regs
*regs
)
302 struct irqaction
*action
;
303 const int cpu
= smp_processor_id();
307 kstat_cpu(cpu
).irqs
[irq
]++;
309 action
= desc
->action
;
311 int ret
= __do_irq(irq
, action
, regs
);
312 if (ret
!= IRQ_HANDLED
)
313 report_bad_irq(irq
, regs
, desc
, ret
);
318 * Most edge-triggered IRQ implementations seem to take a broken
319 * approach to this. Hence the complexity.
322 do_edge_IRQ(unsigned int irq
, struct irqdesc
*desc
, struct pt_regs
*regs
)
324 const int cpu
= smp_processor_id();
329 * If we're currently running this IRQ, or its disabled,
330 * we shouldn't process the IRQ. Instead, turn on the
333 if (unlikely(desc
->running
|| desc
->disable_depth
))
337 * Acknowledge and clear the IRQ, but don't mask it.
339 desc
->chip
->ack(irq
);
342 * Mark the IRQ currently in progress.
346 kstat_cpu(cpu
).irqs
[irq
]++;
349 struct irqaction
*action
;
352 action
= desc
->action
;
356 if (desc
->pending
&& !desc
->disable_depth
) {
358 desc
->chip
->unmask(irq
);
361 ret
= __do_irq(irq
, action
, regs
);
362 if (ret
!= IRQ_HANDLED
)
363 report_bad_irq(irq
, regs
, desc
, ret
);
364 } while (desc
->pending
&& !desc
->disable_depth
);
369 * If we were disabled or freed, shut down the handler.
371 if (likely(desc
->action
&& !check_irq_lock(desc
, irq
, regs
)))
376 * We got another IRQ while this one was masked or
377 * currently running. Delay it.
380 desc
->chip
->mask(irq
);
381 desc
->chip
->ack(irq
);
385 * Level-based IRQ handler. Nice and simple.
388 do_level_IRQ(unsigned int irq
, struct irqdesc
*desc
, struct pt_regs
*regs
)
390 struct irqaction
*action
;
391 const int cpu
= smp_processor_id();
396 * Acknowledge, clear _AND_ disable the interrupt.
398 desc
->chip
->ack(irq
);
400 if (likely(!desc
->disable_depth
)) {
401 kstat_cpu(cpu
).irqs
[irq
]++;
404 * Return with this interrupt masked if no action
406 action
= desc
->action
;
408 int ret
= __do_irq(irq
, desc
->action
, regs
);
410 if (ret
!= IRQ_HANDLED
) {
411 report_bad_irq(irq
, regs
, desc
, ret
);
414 if (likely(!desc
->disable_depth
&&
415 !check_irq_lock(desc
, irq
, regs
))) {
416 desc
->chip
->unmask(irq
);
422 static void do_pending_irqs(struct pt_regs
*regs
)
424 struct list_head head
, *l
, *n
;
427 struct irqdesc
*desc
;
430 * First, take the pending interrupts off the list.
431 * The act of calling the handlers may add some IRQs
432 * back onto the list.
435 INIT_LIST_HEAD(&irq_pending
);
436 head
.next
->prev
= &head
;
437 head
.prev
->next
= &head
;
440 * Now run each entry. We must delete it from our
441 * list before calling the handler.
443 list_for_each_safe(l
, n
, &head
) {
444 desc
= list_entry(l
, struct irqdesc
, pend
);
445 list_del_init(&desc
->pend
);
446 desc
->handle(desc
- irq_desc
, desc
, regs
);
450 * The list must be empty.
452 BUG_ON(!list_empty(&head
));
453 } while (!list_empty(&irq_pending
));
457 * do_IRQ handles all hardware IRQ's. Decoded IRQs should not
458 * come via this function. Instead, they should provide their
461 asmlinkage
void asm_do_IRQ(unsigned int irq
, struct pt_regs
*regs
)
463 #ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 11-02-2005
464 struct irqdesc
*desc
;
466 struct irqdesc
*desc
= irq_desc
+ irq
;
469 #ifdef CONFIG_ARCH_MOXACPU
470 irq
= fixup_irq(irq
);
471 desc
= irq_desc
+ irq
;
474 * Some hardware gives randomly wrong interrupts. Rather
475 * than crashing, do something sensible.
478 desc
= &bad_irq_desc
;
481 spin_lock(&irq_controller_lock
);
482 desc
->handle(irq
, desc
, regs
);
485 * Now re-run any pending interrupts.
487 if (!list_empty(&irq_pending
))
488 do_pending_irqs(regs
);
490 spin_unlock(&irq_controller_lock
);
494 void __set_irq_handler(unsigned int irq
, irq_handler_t handle
, int is_chained
)
496 struct irqdesc
*desc
;
499 if (irq
>= NR_IRQS
) {
500 printk(KERN_ERR
"Trying to install handler for IRQ%d\n", irq
);
507 desc
= irq_desc
+ irq
;
509 if (is_chained
&& desc
->chip
== &bad_chip
)
510 printk(KERN_WARNING
"Trying to install chained handler for IRQ%d\n", irq
);
512 spin_lock_irqsave(&irq_controller_lock
, flags
);
513 if (handle
== do_bad_IRQ
) {
514 desc
->chip
->mask(irq
);
515 desc
->chip
->ack(irq
);
516 desc
->disable_depth
= 1;
518 desc
->handle
= handle
;
519 if (handle
!= do_bad_IRQ
&& is_chained
) {
522 desc
->disable_depth
= 0;
523 desc
->chip
->unmask(irq
);
525 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
528 void set_irq_chip(unsigned int irq
, struct irqchip
*chip
)
530 struct irqdesc
*desc
;
533 if (irq
>= NR_IRQS
) {
534 printk(KERN_ERR
"Trying to install chip for IRQ%d\n", irq
);
541 desc
= irq_desc
+ irq
;
542 spin_lock_irqsave(&irq_controller_lock
, flags
);
544 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
547 int set_irq_type(unsigned int irq
, unsigned int type
)
549 struct irqdesc
*desc
;
553 if (irq
>= NR_IRQS
) {
554 printk(KERN_ERR
"Trying to set irq type for IRQ%d\n", irq
);
558 desc
= irq_desc
+ irq
;
559 if (desc
->chip
->type
) {
560 spin_lock_irqsave(&irq_controller_lock
, flags
);
561 ret
= desc
->chip
->type(irq
, type
);
562 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
567 EXPORT_SYMBOL(set_irq_type
);
569 void set_irq_flags(unsigned int irq
, unsigned int iflags
)
571 struct irqdesc
*desc
;
574 if (irq
>= NR_IRQS
) {
575 printk(KERN_ERR
"Trying to set irq flags for IRQ%d\n", irq
);
579 desc
= irq_desc
+ irq
;
580 spin_lock_irqsave(&irq_controller_lock
, flags
);
581 desc
->valid
= (iflags
& IRQF_VALID
) != 0;
582 desc
->probe_ok
= (iflags
& IRQF_PROBE
) != 0;
583 desc
->noautoenable
= (iflags
& IRQF_NOAUTOEN
) != 0;
584 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
587 int setup_irq(unsigned int irq
, struct irqaction
*new)
590 struct irqaction
*old
, **p
;
592 struct irqdesc
*desc
;
595 * Some drivers like serial.c use request_irq() heavily,
596 * so we have to be careful not to interfere with a
599 if (new->flags
& SA_SAMPLE_RANDOM
) {
601 * This function might sleep, we want to call it first,
602 * outside of the atomic block.
603 * Yes, this might clear the entropy pool if the wrong
604 * driver is attempted to be loaded, without actually
605 * installing a new handler, but is this really a problem,
606 * only the sysadmin is able to do this.
608 rand_initialize_irq(irq
);
612 * The following block of code has to be executed atomically
614 desc
= irq_desc
+ irq
;
615 spin_lock_irqsave(&irq_controller_lock
, flags
);
617 if ((old
= *p
) != NULL
) {
618 /* Can't share interrupts unless both agree to */
619 if (!(old
->flags
& new->flags
& SA_SHIRQ
)) {
620 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
624 /* add new interrupt at end of irq queue */
638 desc
->disable_depth
= 1;
639 if (!desc
->noautoenable
) {
640 desc
->disable_depth
= 0;
641 desc
->chip
->unmask(irq
);
645 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
650 * request_irq - allocate an interrupt line
651 * @irq: Interrupt line to allocate
652 * @handler: Function to be called when the IRQ occurs
653 * @irqflags: Interrupt type flags
654 * @devname: An ascii name for the claiming device
655 * @dev_id: A cookie passed back to the handler function
657 * This call allocates interrupt resources and enables the
658 * interrupt line and IRQ handling. From the point this
659 * call is made your handler function may be invoked. Since
660 * your handler function must clear any interrupt the board
661 * raises, you must take care both to initialise your hardware
662 * and to set up the interrupt handler in the right order.
664 * Dev_id must be globally unique. Normally the address of the
665 * device data structure is used as the cookie. Since the handler
666 * receives this value it makes sense to use it.
668 * If your interrupt is shared you must pass a non NULL dev_id
669 * as this is required when freeing the interrupt.
673 * SA_SHIRQ Interrupt is shared
675 * SA_INTERRUPT Disable local interrupts while processing
677 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
680 int request_irq(unsigned int irq
, irqreturn_t (*handler
)(int, void *, struct pt_regs
*),
681 unsigned long irq_flags
, const char * devname
, void *dev_id
)
683 unsigned long retval
;
684 struct irqaction
*action
;
686 if (irq
>= NR_IRQS
|| !irq_desc
[irq
].valid
|| !handler
||
687 (irq_flags
& SA_SHIRQ
&& !dev_id
))
690 action
= (struct irqaction
*)kmalloc(sizeof(struct irqaction
), GFP_KERNEL
);
694 action
->handler
= handler
;
695 action
->flags
= irq_flags
;
696 cpus_clear(action
->mask
);
697 action
->name
= devname
;
699 action
->dev_id
= dev_id
;
701 retval
= setup_irq(irq
, action
);
708 EXPORT_SYMBOL(request_irq
);
711 * free_irq - free an interrupt
712 * @irq: Interrupt line to free
713 * @dev_id: Device identity to free
715 * Remove an interrupt handler. The handler is removed and if the
716 * interrupt line is no longer in use by any driver it is disabled.
717 * On a shared IRQ the caller must ensure the interrupt is disabled
718 * on the card it drives before calling this function.
720 * This function must not be called from interrupt context.
722 void free_irq(unsigned int irq
, void *dev_id
)
724 struct irqaction
* action
, **p
;
727 if (irq
>= NR_IRQS
|| !irq_desc
[irq
].valid
) {
728 printk(KERN_ERR
"Trying to free IRQ%d\n",irq
);
733 spin_lock_irqsave(&irq_controller_lock
, flags
);
734 for (p
= &irq_desc
[irq
].action
; (action
= *p
) != NULL
; p
= &action
->next
) {
735 if (action
->dev_id
!= dev_id
)
738 /* Found it - now free it */
742 spin_unlock_irqrestore(&irq_controller_lock
, flags
);
745 printk(KERN_ERR
"Trying to free free IRQ%d\n",irq
);
748 synchronize_irq(irq
);
753 EXPORT_SYMBOL(free_irq
);
755 static DECLARE_MUTEX(probe_sem
);
757 /* Start the interrupt probing. Unlike other architectures,
758 * we don't return a mask of interrupts from probe_irq_on,
759 * but return the number of interrupts enabled for the probe.
760 * The interrupts which have been enabled for probing is
761 * instead recorded in the irq_desc structure.
763 unsigned long probe_irq_on(void)
765 unsigned int i
, irqs
= 0;
771 * first snaffle up any unassigned but
772 * probe-able interrupts
774 spin_lock_irq(&irq_controller_lock
);
775 for (i
= 0; i
< NR_IRQS
; i
++) {
776 if (!irq_desc
[i
].probe_ok
|| irq_desc
[i
].action
)
779 irq_desc
[i
].probing
= 1;
780 irq_desc
[i
].triggered
= 0;
781 if (irq_desc
[i
].chip
->type
)
782 irq_desc
[i
].chip
->type(i
, IRQT_PROBE
);
783 irq_desc
[i
].chip
->unmask(i
);
786 spin_unlock_irq(&irq_controller_lock
);
789 * wait for spurious interrupts to mask themselves out again
791 for (delay
= jiffies
+ HZ
/10; time_before(jiffies
, delay
); )
792 /* min 100ms delay */;
795 * now filter out any obviously spurious interrupts
797 spin_lock_irq(&irq_controller_lock
);
798 for (i
= 0; i
< NR_IRQS
; i
++) {
799 if (irq_desc
[i
].probing
&& irq_desc
[i
].triggered
) {
800 irq_desc
[i
].probing
= 0;
804 spin_unlock_irq(&irq_controller_lock
);
809 EXPORT_SYMBOL(probe_irq_on
);
811 unsigned int probe_irq_mask(unsigned long irqs
)
813 unsigned int mask
= 0, i
;
815 spin_lock_irq(&irq_controller_lock
);
816 for (i
= 0; i
< 16 && i
< NR_IRQS
; i
++)
817 if (irq_desc
[i
].probing
&& irq_desc
[i
].triggered
)
819 spin_unlock_irq(&irq_controller_lock
);
825 EXPORT_SYMBOL(probe_irq_mask
);
828 * Possible return values:
829 * >= 0 - interrupt number
830 * -1 - no interrupt/many interrupts
832 int probe_irq_off(unsigned long irqs
)
835 int irq_found
= NO_IRQ
;
838 * look at the interrupts, and find exactly one
839 * that we were probing has been triggered
841 spin_lock_irq(&irq_controller_lock
);
842 for (i
= 0; i
< NR_IRQS
; i
++) {
843 if (irq_desc
[i
].probing
&&
844 irq_desc
[i
].triggered
) {
845 if (irq_found
!= NO_IRQ
) {
856 spin_unlock_irq(&irq_controller_lock
);
863 EXPORT_SYMBOL(probe_irq_off
);
865 void __init
init_irq_proc(void)
869 void __init
init_IRQ(void)
871 struct irqdesc
*desc
;
872 extern void init_dma(void);
875 for (irq
= 0, desc
= irq_desc
; irq
< NR_IRQS
; irq
++, desc
++) {
876 *desc
= bad_irq_desc
;
877 INIT_LIST_HEAD(&desc
->pend
);
884 static int __init
noirqdebug_setup(char *str
)
890 __setup("noirqdebug", noirqdebug_setup
);