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>
36 #include <asm/system.h>
37 #include <asm/mach/irq.h>
40 * Maximum IRQ count. Currently, this is arbitary. However, it should
41 * not be set too low to prevent false triggering. Conversely, if it
42 * is set too high, then you could miss a stuck IRQ.
44 * Maybe we ought to set a timer and re-enable the IRQ at a later time?
46 #define MAX_IRQ_CNT 100000
48 static volatile unsigned long irq_err_count;
49 static spinlock_t irq_controller_lock;
50 static LIST_HEAD(irq_pending);
52 struct irqdesc irq_desc[NR_IRQS];
53 void (*init_arch_irq)(void) __initdata = NULL;
56 * Dummy mask/unmask handler
58 void dummy_mask_unmask_irq(unsigned int irq)
62 irqreturn_t no_action(int irq, void *dev_id, struct pt_regs *regs)
67 void do_bad_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
70 printk(KERN_ERR "IRQ: spurious interrupt %d\n", irq);
73 static struct irqchip bad_chip = {
74 .ack = dummy_mask_unmask_irq,
75 .mask = dummy_mask_unmask_irq,
76 .unmask = dummy_mask_unmask_irq,
79 static struct irqdesc bad_irq_desc = {
82 .pend = LIST_HEAD_INIT(bad_irq_desc.pend),
87 * disable_irq - disable an irq and wait for completion
88 * @irq: Interrupt to disable
90 * Disable the selected interrupt line. Enables and disables
91 * are nested. We do this lazily.
93 * This function may be called from IRQ context.
95 void disable_irq(unsigned int irq)
97 struct irqdesc *desc = irq_desc + irq;
100 spin_lock_irqsave(&irq_controller_lock, flags);
101 desc->disable_depth++;
102 list_del_init(&desc->pend);
103 spin_unlock_irqrestore(&irq_controller_lock, flags);
107 * enable_irq - enable interrupt handling on an irq
108 * @irq: Interrupt to enable
110 * Re-enables the processing of interrupts on this IRQ line.
111 * Note that this may call the interrupt handler, so you may
112 * get unexpected results if you hold IRQs disabled.
114 * This function may be called from IRQ context.
116 void enable_irq(unsigned int irq)
118 struct irqdesc *desc = irq_desc + irq;
121 spin_lock_irqsave(&irq_controller_lock, flags);
122 if (unlikely(!desc->disable_depth)) {
123 printk("enable_irq(%u) unbalanced from %p\n", irq,
124 __builtin_return_address(0));
125 } else if (!--desc->disable_depth) {
127 desc->chip->unmask(irq);
130 * If the interrupt is waiting to be processed,
131 * try to re-run it. We can't directly run it
132 * from here since the caller might be in an
133 * interrupt-protected region.
135 if (desc->pending && list_empty(&desc->pend)) {
137 if (!desc->chip->retrigger ||
138 desc->chip->retrigger(irq))
139 list_add(&desc->pend, &irq_pending);
142 spin_unlock_irqrestore(&irq_controller_lock, flags);
146 * Enable wake on selected irq
148 void enable_irq_wake(unsigned int irq)
150 struct irqdesc *desc = irq_desc + irq;
153 spin_lock_irqsave(&irq_controller_lock, flags);
154 if (desc->chip->wake)
155 desc->chip->wake(irq, 1);
156 spin_unlock_irqrestore(&irq_controller_lock, flags);
159 void disable_irq_wake(unsigned int irq)
161 struct irqdesc *desc = irq_desc + irq;
164 spin_lock_irqsave(&irq_controller_lock, flags);
165 if (desc->chip->wake)
166 desc->chip->wake(irq, 0);
167 spin_unlock_irqrestore(&irq_controller_lock, flags);
170 int show_interrupts(struct seq_file *p, void *v)
173 struct irqaction * action;
176 for (i = 0 ; i < NR_IRQS ; i++) {
177 spin_lock_irqsave(&irq_controller_lock, flags);
178 action = irq_desc[i].action;
182 seq_printf(p, "%3d: %10u ", i, kstat_irqs(i));
183 seq_printf(p, " %s", action->name);
184 for (action = action->next; action; action = action->next)
185 seq_printf(p, ", %s", action->name);
189 spin_unlock_irqrestore(&irq_controller_lock, flags);
192 #ifdef CONFIG_ARCH_ACORN
195 seq_printf(p, "Err: %10lu\n", irq_err_count);
200 * IRQ lock detection.
202 * Hopefully, this should get us out of a few locked situations.
203 * However, it may take a while for this to happen, since we need
204 * a large number if IRQs to appear in the same jiffie with the
205 * same instruction pointer (or within 2 instructions).
207 static int check_irq_lock(struct irqdesc *desc, int irq, struct pt_regs *regs)
209 unsigned long instr_ptr = instruction_pointer(regs);
211 if (desc->lck_jif == jiffies &&
212 desc->lck_pc >= instr_ptr && desc->lck_pc < instr_ptr + 8) {
215 if (desc->lck_cnt > MAX_IRQ_CNT) {
216 printk(KERN_ERR "IRQ LOCK: IRQ%d is locking the system, disabled\n", irq);
221 desc->lck_pc = instruction_pointer(regs);
222 desc->lck_jif = jiffies;
228 __do_irq(unsigned int irq, struct irqaction *action, struct pt_regs *regs)
233 spin_unlock(&irq_controller_lock);
235 if (!(action->flags & SA_INTERRUPT))
240 status |= action->flags;
241 retval |= action->handler(irq, action->dev_id, regs);
242 action = action->next;
245 if (status & SA_SAMPLE_RANDOM)
246 add_interrupt_randomness(irq);
248 spin_lock_irq(&irq_controller_lock);
251 static int count = 100;
255 printk("irq event %d: bogus retval mask %x\n",
258 printk("irq %d: nobody cared\n", irq);
265 * This is for software-decoded IRQs. The caller is expected to
266 * handle the ack, clear, mask and unmask issues.
269 do_simple_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
271 struct irqaction *action;
272 const int cpu = smp_processor_id();
276 kstat_cpu(cpu).irqs[irq]++;
278 action = desc->action;
280 __do_irq(irq, desc->action, regs);
284 * Most edge-triggered IRQ implementations seem to take a broken
285 * approach to this. Hence the complexity.
288 do_edge_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
290 const int cpu = smp_processor_id();
295 * If we're currently running this IRQ, or its disabled,
296 * we shouldn't process the IRQ. Instead, turn on the
299 if (unlikely(desc->running || desc->disable_depth))
303 * Acknowledge and clear the IRQ, but don't mask it.
305 desc->chip->ack(irq);
308 * Mark the IRQ currently in progress.
312 kstat_cpu(cpu).irqs[irq]++;
315 struct irqaction *action;
317 action = desc->action;
321 if (desc->pending && !desc->disable_depth) {
323 desc->chip->unmask(irq);
326 __do_irq(irq, action, regs);
327 } while (desc->pending && !desc->disable_depth);
332 * If we were disabled or freed, shut down the handler.
334 if (likely(desc->action && !check_irq_lock(desc, irq, regs)))
339 * We got another IRQ while this one was masked or
340 * currently running. Delay it.
343 desc->chip->mask(irq);
344 desc->chip->ack(irq);
348 * Level-based IRQ handler. Nice and simple.
351 do_level_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
353 struct irqaction *action;
354 const int cpu = smp_processor_id();
359 * Acknowledge, clear _AND_ disable the interrupt.
361 desc->chip->ack(irq);
363 if (likely(!desc->disable_depth)) {
364 kstat_cpu(cpu).irqs[irq]++;
367 * Return with this interrupt masked if no action
369 action = desc->action;
371 __do_irq(irq, desc->action, regs);
373 if (likely(!desc->disable_depth &&
374 !check_irq_lock(desc, irq, regs)))
375 desc->chip->unmask(irq);
380 static void do_pending_irqs(struct pt_regs *regs)
382 struct list_head head, *l, *n;
385 struct irqdesc *desc;
388 * First, take the pending interrupts off the list.
389 * The act of calling the handlers may add some IRQs
390 * back onto the list.
393 INIT_LIST_HEAD(&irq_pending);
394 head.next->prev = &head;
395 head.prev->next = &head;
398 * Now run each entry. We must delete it from our
399 * list before calling the handler.
401 list_for_each_safe(l, n, &head) {
402 desc = list_entry(l, struct irqdesc, pend);
403 list_del_init(&desc->pend);
404 desc->handle(desc - irq_desc, desc, regs);
408 * The list must be empty.
410 BUG_ON(!list_empty(&head));
411 } while (!list_empty(&irq_pending));
415 * do_IRQ handles all hardware IRQ's. Decoded IRQs should not
416 * come via this function. Instead, they should provide their
419 asmlinkage void asm_do_IRQ(int irq, struct pt_regs *regs)
421 struct irqdesc *desc = irq_desc + irq;
424 * Some hardware gives randomly wrong interrupts. Rather
425 * than crashing, do something sensible.
428 desc = &bad_irq_desc;
431 spin_lock(&irq_controller_lock);
432 desc->handle(irq, desc, regs);
435 * Now re-run any pending interrupts.
437 if (!list_empty(&irq_pending))
438 do_pending_irqs(regs);
440 spin_unlock(&irq_controller_lock);
444 void __set_irq_handler(unsigned int irq, irq_handler_t handle, int is_chained)
446 struct irqdesc *desc;
449 if (irq >= NR_IRQS) {
450 printk(KERN_ERR "Trying to install handler for IRQ%d\n", irq);
457 desc = irq_desc + irq;
459 if (is_chained && desc->chip == &bad_chip)
460 printk(KERN_WARNING "Trying to install chained handler for IRQ%d\n", irq);
462 spin_lock_irqsave(&irq_controller_lock, flags);
463 if (handle == do_bad_IRQ) {
464 desc->chip->mask(irq);
465 desc->chip->ack(irq);
466 desc->disable_depth = 1;
468 desc->handle = handle;
469 if (handle != do_bad_IRQ && is_chained) {
472 desc->disable_depth = 0;
473 desc->chip->unmask(irq);
475 spin_unlock_irqrestore(&irq_controller_lock, flags);
478 void set_irq_chip(unsigned int irq, struct irqchip *chip)
480 struct irqdesc *desc;
483 if (irq >= NR_IRQS) {
484 printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
491 desc = irq_desc + irq;
492 spin_lock_irqsave(&irq_controller_lock, flags);
494 spin_unlock_irqrestore(&irq_controller_lock, flags);
497 int set_irq_type(unsigned int irq, unsigned int type)
499 struct irqdesc *desc;
503 if (irq >= NR_IRQS) {
504 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
508 desc = irq_desc + irq;
509 if (desc->chip->type) {
510 spin_lock_irqsave(&irq_controller_lock, flags);
511 ret = desc->chip->type(irq, type);
512 spin_unlock_irqrestore(&irq_controller_lock, flags);
518 void set_irq_flags(unsigned int irq, unsigned int iflags)
520 struct irqdesc *desc;
523 if (irq >= NR_IRQS) {
524 printk(KERN_ERR "Trying to set irq flags for IRQ%d\n", irq);
528 desc = irq_desc + irq;
529 spin_lock_irqsave(&irq_controller_lock, flags);
530 desc->valid = (iflags & IRQF_VALID) != 0;
531 desc->probe_ok = (iflags & IRQF_PROBE) != 0;
532 desc->noautoenable = (iflags & IRQF_NOAUTOEN) != 0;
533 spin_unlock_irqrestore(&irq_controller_lock, flags);
536 int setup_irq(unsigned int irq, struct irqaction *new)
539 struct irqaction *old, **p;
541 struct irqdesc *desc;
544 * Some drivers like serial.c use request_irq() heavily,
545 * so we have to be careful not to interfere with a
548 if (new->flags & SA_SAMPLE_RANDOM) {
550 * This function might sleep, we want to call it first,
551 * outside of the atomic block.
552 * Yes, this might clear the entropy pool if the wrong
553 * driver is attempted to be loaded, without actually
554 * installing a new handler, but is this really a problem,
555 * only the sysadmin is able to do this.
557 rand_initialize_irq(irq);
561 * The following block of code has to be executed atomically
563 desc = irq_desc + irq;
564 spin_lock_irqsave(&irq_controller_lock, flags);
566 if ((old = *p) != NULL) {
567 /* Can't share interrupts unless both agree to */
568 if (!(old->flags & new->flags & SA_SHIRQ)) {
569 spin_unlock_irqrestore(&irq_controller_lock, flags);
573 /* add new interrupt at end of irq queue */
587 desc->disable_depth = 1;
588 if (!desc->noautoenable) {
589 desc->disable_depth = 0;
590 desc->chip->unmask(irq);
594 spin_unlock_irqrestore(&irq_controller_lock, flags);
599 * request_irq - allocate an interrupt line
600 * @irq: Interrupt line to allocate
601 * @handler: Function to be called when the IRQ occurs
602 * @irqflags: Interrupt type flags
603 * @devname: An ascii name for the claiming device
604 * @dev_id: A cookie passed back to the handler function
606 * This call allocates interrupt resources and enables the
607 * interrupt line and IRQ handling. From the point this
608 * call is made your handler function may be invoked. Since
609 * your handler function must clear any interrupt the board
610 * raises, you must take care both to initialise your hardware
611 * and to set up the interrupt handler in the right order.
613 * Dev_id must be globally unique. Normally the address of the
614 * device data structure is used as the cookie. Since the handler
615 * receives this value it makes sense to use it.
617 * If your interrupt is shared you must pass a non NULL dev_id
618 * as this is required when freeing the interrupt.
622 * SA_SHIRQ Interrupt is shared
624 * SA_INTERRUPT Disable local interrupts while processing
626 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
629 int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
630 unsigned long irq_flags, const char * devname, void *dev_id)
632 unsigned long retval;
633 struct irqaction *action;
635 if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
636 (irq_flags & SA_SHIRQ && !dev_id))
639 action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
643 action->handler = handler;
644 action->flags = irq_flags;
646 action->name = devname;
648 action->dev_id = dev_id;
650 retval = setup_irq(irq, action);
657 EXPORT_SYMBOL(request_irq);
660 * free_irq - free an interrupt
661 * @irq: Interrupt line to free
662 * @dev_id: Device identity to free
664 * Remove an interrupt handler. The handler is removed and if the
665 * interrupt line is no longer in use by any driver it is disabled.
666 * On a shared IRQ the caller must ensure the interrupt is disabled
667 * on the card it drives before calling this function.
669 * This function must not be called from interrupt context.
671 void free_irq(unsigned int irq, void *dev_id)
673 struct irqaction * action, **p;
676 if (irq >= NR_IRQS || !irq_desc[irq].valid) {
677 printk(KERN_ERR "Trying to free IRQ%d\n",irq);
682 spin_lock_irqsave(&irq_controller_lock, flags);
683 for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
684 if (action->dev_id != dev_id)
687 /* Found it - now free it */
691 spin_unlock_irqrestore(&irq_controller_lock, flags);
694 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
697 synchronize_irq(irq);
702 EXPORT_SYMBOL(free_irq);
704 static DECLARE_MUTEX(probe_sem);
706 /* Start the interrupt probing. Unlike other architectures,
707 * we don't return a mask of interrupts from probe_irq_on,
708 * but return the number of interrupts enabled for the probe.
709 * The interrupts which have been enabled for probing is
710 * instead recorded in the irq_desc structure.
712 unsigned long probe_irq_on(void)
714 unsigned int i, irqs = 0;
720 * first snaffle up any unassigned but
721 * probe-able interrupts
723 spin_lock_irq(&irq_controller_lock);
724 for (i = 0; i < NR_IRQS; i++) {
725 if (!irq_desc[i].probe_ok || irq_desc[i].action)
728 irq_desc[i].probing = 1;
729 irq_desc[i].triggered = 0;
730 if (irq_desc[i].chip->type)
731 irq_desc[i].chip->type(i, IRQT_PROBE);
732 irq_desc[i].chip->unmask(i);
735 spin_unlock_irq(&irq_controller_lock);
738 * wait for spurious interrupts to mask themselves out again
740 for (delay = jiffies + HZ/10; time_before(jiffies, delay); )
741 /* min 100ms delay */;
744 * now filter out any obviously spurious interrupts
746 spin_lock_irq(&irq_controller_lock);
747 for (i = 0; i < NR_IRQS; i++) {
748 if (irq_desc[i].probing && irq_desc[i].triggered) {
749 irq_desc[i].probing = 0;
753 spin_unlock_irq(&irq_controller_lock);
758 EXPORT_SYMBOL(probe_irq_on);
760 unsigned int probe_irq_mask(unsigned long irqs)
762 unsigned int mask = 0, i;
764 spin_lock_irq(&irq_controller_lock);
765 for (i = 0; i < 16 && i < NR_IRQS; i++)
766 if (irq_desc[i].probing && irq_desc[i].triggered)
768 spin_unlock_irq(&irq_controller_lock);
776 * Possible return values:
777 * >= 0 - interrupt number
778 * -1 - no interrupt/many interrupts
780 int probe_irq_off(unsigned long irqs)
783 int irq_found = NO_IRQ;
786 * look at the interrupts, and find exactly one
787 * that we were probing has been triggered
789 spin_lock_irq(&irq_controller_lock);
790 for (i = 0; i < NR_IRQS; i++) {
791 if (irq_desc[i].probing &&
792 irq_desc[i].triggered) {
793 if (irq_found != NO_IRQ) {
804 spin_unlock_irq(&irq_controller_lock);
811 EXPORT_SYMBOL(probe_irq_off);
813 void __init init_irq_proc(void)
817 void __init init_IRQ(void)
819 struct irqdesc *desc;
820 extern void init_dma(void);
823 for (irq = 0, desc = irq_desc; irq < NR_IRQS; irq++, desc++) {
824 *desc = bad_irq_desc;
825 INIT_LIST_HEAD(&desc->pend);