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>
41 * Maximum IRQ count. Currently, this is arbitary. However, it should
42 * not be set too low to prevent false triggering. Conversely, if it
43 * is set too high, then you could miss a stuck IRQ.
45 * Maybe we ought to set a timer and re-enable the IRQ at a later time?
47 #define MAX_IRQ_CNT 100000
49 static int noirqdebug;
50 static volatile unsigned long irq_err_count;
51 static spinlock_t irq_controller_lock = SPIN_LOCK_UNLOCKED;
52 static LIST_HEAD(irq_pending);
54 struct irqdesc irq_desc[NR_IRQS];
55 void (*init_arch_irq)(void) __initdata = NULL;
58 * Dummy mask/unmask handler
60 void dummy_mask_unmask_irq(unsigned int irq)
64 irqreturn_t no_action(int irq, void *dev_id, struct pt_regs *regs)
69 void do_bad_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
72 printk(KERN_ERR "IRQ: spurious interrupt %d\n", irq);
75 static struct irqchip bad_chip = {
76 .ack = dummy_mask_unmask_irq,
77 .mask = dummy_mask_unmask_irq,
78 .unmask = dummy_mask_unmask_irq,
81 static struct irqdesc bad_irq_desc = {
84 .pend = LIST_HEAD_INIT(bad_irq_desc.pend),
89 * disable_irq_nosync - disable an irq without waiting
90 * @irq: Interrupt to disable
92 * Disable the selected interrupt line. Enables and disables
93 * are nested. We do this lazily.
95 * This function may be called from IRQ context.
97 void disable_irq_nosync(unsigned int irq)
99 struct irqdesc *desc = irq_desc + irq;
102 spin_lock_irqsave(&irq_controller_lock, flags);
103 desc->disable_depth++;
104 list_del_init(&desc->pend);
105 spin_unlock_irqrestore(&irq_controller_lock, flags);
107 EXPORT_SYMBOL(disable_irq_nosync);
110 * disable_irq - disable an irq and wait for completion
111 * @irq: Interrupt to disable
113 * Disable the selected interrupt line. Enables and disables
114 * are nested. This functions waits for any pending IRQ
115 * handlers for this interrupt to complete before returning.
116 * If you use this function while holding a resource the IRQ
117 * handler may need you will deadlock.
119 * This function may be called - with care - from IRQ context.
121 void disable_irq(unsigned int irq)
123 struct irqdesc *desc = irq_desc + irq;
125 disable_irq_nosync(irq);
127 synchronize_irq(irq);
129 EXPORT_SYMBOL(disable_irq);
132 * enable_irq - enable interrupt handling on an irq
133 * @irq: Interrupt to enable
135 * Re-enables the processing of interrupts on this IRQ line.
136 * Note that this may call the interrupt handler, so you may
137 * get unexpected results if you hold IRQs disabled.
139 * This function may be called from IRQ context.
141 void enable_irq(unsigned int irq)
143 struct irqdesc *desc = irq_desc + irq;
146 spin_lock_irqsave(&irq_controller_lock, flags);
147 if (unlikely(!desc->disable_depth)) {
148 printk("enable_irq(%u) unbalanced from %p\n", irq,
149 __builtin_return_address(0));
150 } else if (!--desc->disable_depth) {
152 desc->chip->unmask(irq);
155 * If the interrupt is waiting to be processed,
156 * try to re-run it. We can't directly run it
157 * from here since the caller might be in an
158 * interrupt-protected region.
160 if (desc->pending && list_empty(&desc->pend)) {
162 if (!desc->chip->retrigger ||
163 desc->chip->retrigger(irq))
164 list_add(&desc->pend, &irq_pending);
167 spin_unlock_irqrestore(&irq_controller_lock, flags);
169 EXPORT_SYMBOL(enable_irq);
172 * Enable wake on selected irq
174 void enable_irq_wake(unsigned int irq)
176 struct irqdesc *desc = irq_desc + irq;
179 spin_lock_irqsave(&irq_controller_lock, flags);
180 if (desc->chip->wake)
181 desc->chip->wake(irq, 1);
182 spin_unlock_irqrestore(&irq_controller_lock, flags);
184 EXPORT_SYMBOL(enable_irq_wake);
186 void disable_irq_wake(unsigned int irq)
188 struct irqdesc *desc = irq_desc + irq;
191 spin_lock_irqsave(&irq_controller_lock, flags);
192 if (desc->chip->wake)
193 desc->chip->wake(irq, 0);
194 spin_unlock_irqrestore(&irq_controller_lock, flags);
196 EXPORT_SYMBOL(disable_irq_wake);
198 int show_interrupts(struct seq_file *p, void *v)
200 int i = *(loff_t *) v, cpu;
201 struct irqaction * action;
208 for_each_present_cpu(cpu) {
209 sprintf(cpuname, "CPU%d", cpu);
210 seq_printf(p, " %10s", cpuname);
216 spin_lock_irqsave(&irq_controller_lock, flags);
217 action = irq_desc[i].action;
221 seq_printf(p, "%3d: ", i);
222 for_each_present_cpu(cpu)
223 seq_printf(p, "%10u ", kstat_cpu(cpu).irqs[i]);
224 seq_printf(p, " %s", action->name);
225 for (action = action->next; action; action = action->next)
226 seq_printf(p, ", %s", action->name);
230 spin_unlock_irqrestore(&irq_controller_lock, flags);
231 } else if (i == NR_IRQS) {
232 #ifdef CONFIG_ARCH_ACORN
235 seq_printf(p, "Err: %10lu\n", irq_err_count);
241 * IRQ lock detection.
243 * Hopefully, this should get us out of a few locked situations.
244 * However, it may take a while for this to happen, since we need
245 * a large number if IRQs to appear in the same jiffie with the
246 * same instruction pointer (or within 2 instructions).
248 static int check_irq_lock(struct irqdesc *desc, int irq, struct pt_regs *regs)
250 unsigned long instr_ptr = instruction_pointer(regs);
252 if (desc->lck_jif == jiffies &&
253 desc->lck_pc >= instr_ptr && desc->lck_pc < instr_ptr + 8) {
256 if (desc->lck_cnt > MAX_IRQ_CNT) {
257 printk(KERN_ERR "IRQ LOCK: IRQ%d is locking the system, disabled\n", irq);
262 desc->lck_pc = instruction_pointer(regs);
263 desc->lck_jif = jiffies;
269 report_bad_irq(unsigned int irq, struct pt_regs *regs, struct irqdesc *desc, int ret)
271 static int count = 100;
272 struct irqaction *action;
274 if (!count || noirqdebug)
279 if (ret != IRQ_HANDLED && ret != IRQ_NONE) {
280 printk("irq%u: bogus retval mask %x\n", irq, ret);
282 printk("irq%u: nobody cared\n", irq);
286 printk(KERN_ERR "handlers:");
287 action = desc->action;
289 printk("\n" KERN_ERR "[<%p>]", action->handler);
290 print_symbol(" (%s)", (unsigned long)action->handler);
291 action = action->next;
297 __do_irq(unsigned int irq, struct irqaction *action, struct pt_regs *regs)
302 spin_unlock(&irq_controller_lock);
304 if (!(action->flags & SA_INTERRUPT))
309 ret = action->handler(irq, action->dev_id, regs);
310 if (ret == IRQ_HANDLED)
311 status |= action->flags;
313 action = action->next;
316 if (status & SA_SAMPLE_RANDOM)
317 add_interrupt_randomness(irq);
319 spin_lock_irq(&irq_controller_lock);
325 * This is for software-decoded IRQs. The caller is expected to
326 * handle the ack, clear, mask and unmask issues.
329 do_simple_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
331 struct irqaction *action;
332 const int cpu = smp_processor_id();
336 kstat_cpu(cpu).irqs[irq]++;
338 action = desc->action;
340 int ret = __do_irq(irq, action, regs);
341 if (ret != IRQ_HANDLED)
342 report_bad_irq(irq, regs, desc, ret);
347 * Most edge-triggered IRQ implementations seem to take a broken
348 * approach to this. Hence the complexity.
351 do_edge_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
353 const int cpu = smp_processor_id();
358 * If we're currently running this IRQ, or its disabled,
359 * we shouldn't process the IRQ. Instead, turn on the
362 if (unlikely(desc->running || desc->disable_depth))
366 * Acknowledge and clear the IRQ, but don't mask it.
368 desc->chip->ack(irq);
371 * Mark the IRQ currently in progress.
375 kstat_cpu(cpu).irqs[irq]++;
378 struct irqaction *action;
380 action = desc->action;
384 if (desc->pending && !desc->disable_depth) {
386 desc->chip->unmask(irq);
389 __do_irq(irq, action, regs);
390 } while (desc->pending && !desc->disable_depth);
395 * If we were disabled or freed, shut down the handler.
397 if (likely(desc->action && !check_irq_lock(desc, irq, regs)))
402 * We got another IRQ while this one was masked or
403 * currently running. Delay it.
406 desc->chip->mask(irq);
407 desc->chip->ack(irq);
411 * Level-based IRQ handler. Nice and simple.
414 do_level_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
416 struct irqaction *action;
417 const int cpu = smp_processor_id();
422 * Acknowledge, clear _AND_ disable the interrupt.
424 desc->chip->ack(irq);
426 if (likely(!desc->disable_depth)) {
427 kstat_cpu(cpu).irqs[irq]++;
430 * Return with this interrupt masked if no action
432 action = desc->action;
434 int ret = __do_irq(irq, desc->action, regs);
436 if (ret != IRQ_HANDLED)
437 report_bad_irq(irq, regs, desc, ret);
439 if (likely(!desc->disable_depth &&
440 !check_irq_lock(desc, irq, regs)))
441 desc->chip->unmask(irq);
446 static void do_pending_irqs(struct pt_regs *regs)
448 struct list_head head, *l, *n;
451 struct irqdesc *desc;
454 * First, take the pending interrupts off the list.
455 * The act of calling the handlers may add some IRQs
456 * back onto the list.
459 INIT_LIST_HEAD(&irq_pending);
460 head.next->prev = &head;
461 head.prev->next = &head;
464 * Now run each entry. We must delete it from our
465 * list before calling the handler.
467 list_for_each_safe(l, n, &head) {
468 desc = list_entry(l, struct irqdesc, pend);
469 list_del_init(&desc->pend);
470 desc->handle(desc - irq_desc, desc, regs);
474 * The list must be empty.
476 BUG_ON(!list_empty(&head));
477 } while (!list_empty(&irq_pending));
481 * do_IRQ handles all hardware IRQ's. Decoded IRQs should not
482 * come via this function. Instead, they should provide their
485 asmlinkage void asm_do_IRQ(unsigned int irq, struct pt_regs *regs)
487 struct irqdesc *desc = irq_desc + irq;
490 * Some hardware gives randomly wrong interrupts. Rather
491 * than crashing, do something sensible.
494 desc = &bad_irq_desc;
497 spin_lock(&irq_controller_lock);
498 desc->handle(irq, desc, regs);
501 * Now re-run any pending interrupts.
503 if (!list_empty(&irq_pending))
504 do_pending_irqs(regs);
506 spin_unlock(&irq_controller_lock);
510 void __set_irq_handler(unsigned int irq, irq_handler_t handle, int is_chained)
512 struct irqdesc *desc;
515 if (irq >= NR_IRQS) {
516 printk(KERN_ERR "Trying to install handler for IRQ%d\n", irq);
523 desc = irq_desc + irq;
525 if (is_chained && desc->chip == &bad_chip)
526 printk(KERN_WARNING "Trying to install chained handler for IRQ%d\n", irq);
528 spin_lock_irqsave(&irq_controller_lock, flags);
529 if (handle == do_bad_IRQ) {
530 desc->chip->mask(irq);
531 desc->chip->ack(irq);
532 desc->disable_depth = 1;
534 desc->handle = handle;
535 if (handle != do_bad_IRQ && is_chained) {
538 desc->disable_depth = 0;
539 desc->chip->unmask(irq);
541 spin_unlock_irqrestore(&irq_controller_lock, flags);
544 void set_irq_chip(unsigned int irq, struct irqchip *chip)
546 struct irqdesc *desc;
549 if (irq >= NR_IRQS) {
550 printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
557 desc = irq_desc + irq;
558 spin_lock_irqsave(&irq_controller_lock, flags);
560 spin_unlock_irqrestore(&irq_controller_lock, flags);
563 int set_irq_type(unsigned int irq, unsigned int type)
565 struct irqdesc *desc;
569 if (irq >= NR_IRQS) {
570 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
574 desc = irq_desc + irq;
575 if (desc->chip->type) {
576 spin_lock_irqsave(&irq_controller_lock, flags);
577 ret = desc->chip->type(irq, type);
578 spin_unlock_irqrestore(&irq_controller_lock, flags);
583 EXPORT_SYMBOL(set_irq_type);
585 void set_irq_flags(unsigned int irq, unsigned int iflags)
587 struct irqdesc *desc;
590 if (irq >= NR_IRQS) {
591 printk(KERN_ERR "Trying to set irq flags for IRQ%d\n", irq);
595 desc = irq_desc + irq;
596 spin_lock_irqsave(&irq_controller_lock, flags);
597 desc->valid = (iflags & IRQF_VALID) != 0;
598 desc->probe_ok = (iflags & IRQF_PROBE) != 0;
599 desc->noautoenable = (iflags & IRQF_NOAUTOEN) != 0;
600 spin_unlock_irqrestore(&irq_controller_lock, flags);
603 int setup_irq(unsigned int irq, struct irqaction *new)
606 struct irqaction *old, **p;
608 struct irqdesc *desc;
611 * Some drivers like serial.c use request_irq() heavily,
612 * so we have to be careful not to interfere with a
615 if (new->flags & SA_SAMPLE_RANDOM) {
617 * This function might sleep, we want to call it first,
618 * outside of the atomic block.
619 * Yes, this might clear the entropy pool if the wrong
620 * driver is attempted to be loaded, without actually
621 * installing a new handler, but is this really a problem,
622 * only the sysadmin is able to do this.
624 rand_initialize_irq(irq);
628 * The following block of code has to be executed atomically
630 desc = irq_desc + irq;
631 spin_lock_irqsave(&irq_controller_lock, flags);
633 if ((old = *p) != NULL) {
634 /* Can't share interrupts unless both agree to */
635 if (!(old->flags & new->flags & SA_SHIRQ)) {
636 spin_unlock_irqrestore(&irq_controller_lock, flags);
640 /* add new interrupt at end of irq queue */
654 desc->disable_depth = 1;
655 if (!desc->noautoenable) {
656 desc->disable_depth = 0;
657 desc->chip->unmask(irq);
661 spin_unlock_irqrestore(&irq_controller_lock, flags);
666 * request_irq - allocate an interrupt line
667 * @irq: Interrupt line to allocate
668 * @handler: Function to be called when the IRQ occurs
669 * @irqflags: Interrupt type flags
670 * @devname: An ascii name for the claiming device
671 * @dev_id: A cookie passed back to the handler function
673 * This call allocates interrupt resources and enables the
674 * interrupt line and IRQ handling. From the point this
675 * call is made your handler function may be invoked. Since
676 * your handler function must clear any interrupt the board
677 * raises, you must take care both to initialise your hardware
678 * and to set up the interrupt handler in the right order.
680 * Dev_id must be globally unique. Normally the address of the
681 * device data structure is used as the cookie. Since the handler
682 * receives this value it makes sense to use it.
684 * If your interrupt is shared you must pass a non NULL dev_id
685 * as this is required when freeing the interrupt.
689 * SA_SHIRQ Interrupt is shared
691 * SA_INTERRUPT Disable local interrupts while processing
693 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
696 int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
697 unsigned long irq_flags, const char * devname, void *dev_id)
699 unsigned long retval;
700 struct irqaction *action;
702 if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
703 (irq_flags & SA_SHIRQ && !dev_id))
706 action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
710 action->handler = handler;
711 action->flags = irq_flags;
712 cpus_clear(action->mask);
713 action->name = devname;
715 action->dev_id = dev_id;
717 retval = setup_irq(irq, action);
724 EXPORT_SYMBOL(request_irq);
727 * free_irq - free an interrupt
728 * @irq: Interrupt line to free
729 * @dev_id: Device identity to free
731 * Remove an interrupt handler. The handler is removed and if the
732 * interrupt line is no longer in use by any driver it is disabled.
733 * On a shared IRQ the caller must ensure the interrupt is disabled
734 * on the card it drives before calling this function.
736 * This function must not be called from interrupt context.
738 void free_irq(unsigned int irq, void *dev_id)
740 struct irqaction * action, **p;
743 if (irq >= NR_IRQS || !irq_desc[irq].valid) {
744 printk(KERN_ERR "Trying to free IRQ%d\n",irq);
749 spin_lock_irqsave(&irq_controller_lock, flags);
750 for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
751 if (action->dev_id != dev_id)
754 /* Found it - now free it */
758 spin_unlock_irqrestore(&irq_controller_lock, flags);
761 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
764 synchronize_irq(irq);
769 EXPORT_SYMBOL(free_irq);
771 static DECLARE_MUTEX(probe_sem);
773 /* Start the interrupt probing. Unlike other architectures,
774 * we don't return a mask of interrupts from probe_irq_on,
775 * but return the number of interrupts enabled for the probe.
776 * The interrupts which have been enabled for probing is
777 * instead recorded in the irq_desc structure.
779 unsigned long probe_irq_on(void)
781 unsigned int i, irqs = 0;
787 * first snaffle up any unassigned but
788 * probe-able interrupts
790 spin_lock_irq(&irq_controller_lock);
791 for (i = 0; i < NR_IRQS; i++) {
792 if (!irq_desc[i].probe_ok || irq_desc[i].action)
795 irq_desc[i].probing = 1;
796 irq_desc[i].triggered = 0;
797 if (irq_desc[i].chip->type)
798 irq_desc[i].chip->type(i, IRQT_PROBE);
799 irq_desc[i].chip->unmask(i);
802 spin_unlock_irq(&irq_controller_lock);
805 * wait for spurious interrupts to mask themselves out again
807 for (delay = jiffies + HZ/10; time_before(jiffies, delay); )
808 /* min 100ms delay */;
811 * now filter out any obviously spurious interrupts
813 spin_lock_irq(&irq_controller_lock);
814 for (i = 0; i < NR_IRQS; i++) {
815 if (irq_desc[i].probing && irq_desc[i].triggered) {
816 irq_desc[i].probing = 0;
820 spin_unlock_irq(&irq_controller_lock);
825 EXPORT_SYMBOL(probe_irq_on);
827 unsigned int probe_irq_mask(unsigned long irqs)
829 unsigned int mask = 0, i;
831 spin_lock_irq(&irq_controller_lock);
832 for (i = 0; i < 16 && i < NR_IRQS; i++)
833 if (irq_desc[i].probing && irq_desc[i].triggered)
835 spin_unlock_irq(&irq_controller_lock);
841 EXPORT_SYMBOL(probe_irq_mask);
844 * Possible return values:
845 * >= 0 - interrupt number
846 * -1 - no interrupt/many interrupts
848 int probe_irq_off(unsigned long irqs)
851 int irq_found = NO_IRQ;
854 * look at the interrupts, and find exactly one
855 * that we were probing has been triggered
857 spin_lock_irq(&irq_controller_lock);
858 for (i = 0; i < NR_IRQS; i++) {
859 if (irq_desc[i].probing &&
860 irq_desc[i].triggered) {
861 if (irq_found != NO_IRQ) {
872 spin_unlock_irq(&irq_controller_lock);
879 EXPORT_SYMBOL(probe_irq_off);
881 void __init init_irq_proc(void)
885 void __init init_IRQ(void)
887 struct irqdesc *desc;
888 extern void init_dma(void);
891 for (irq = 0, desc = irq_desc; irq < NR_IRQS; irq++, desc++) {
892 *desc = bad_irq_desc;
893 INIT_LIST_HEAD(&desc->pend);
900 static int __init noirqdebug_setup(char *str)
906 __setup("noirqdebug", noirqdebug_setup);