2 * linux/arch/arm/kernel/irq.c
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.
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.
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
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.
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/ptrace.h>
24 #include <linux/kernel_stat.h>
25 #include <linux/signal.h>
26 #include <linux/sched.h>
27 #include <linux/ioport.h>
28 #include <linux/interrupt.h>
29 #include <linux/slab.h>
30 #include <linux/random.h>
31 #include <linux/smp.h>
32 #include <linux/init.h>
33 #include <linux/seq_file.h>
34 #include <linux/errno.h>
37 #include <asm/system.h>
38 #include <asm/irqchip.h>
40 //FIXME - this ought to be in a header IMO
41 void __init arc_init_irq(void);
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 * FIXME Maybe we ought to set a timer and re-enable the IRQ at a later time?
50 #define MAX_IRQ_CNT 100000
52 static volatile unsigned long irq_err_count;
53 static DEFINE_SPINLOCK(irq_controller_lock);
55 struct irqdesc irq_desc[NR_IRQS];
58 * Dummy mask/unmask handler
60 void dummy_mask_unmask_irq(unsigned int irq)
64 void do_bad_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
67 printk(KERN_ERR "IRQ: spurious interrupt %d\n", irq);
70 static struct irqchip bad_chip = {
71 .ack = dummy_mask_unmask_irq,
72 .mask = dummy_mask_unmask_irq,
73 .unmask = dummy_mask_unmask_irq,
76 static struct irqdesc bad_irq_desc = {
83 * disable_irq - disable an irq and wait for completion
84 * @irq: Interrupt to disable
86 * Disable the selected interrupt line. We do this lazily.
88 * This function may be called from IRQ context.
90 void disable_irq(unsigned int irq)
92 struct irqdesc *desc = irq_desc + irq;
94 spin_lock_irqsave(&irq_controller_lock, flags);
97 spin_unlock_irqrestore(&irq_controller_lock, flags);
101 * enable_irq - enable interrupt handling on an irq
102 * @irq: Interrupt to enable
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.
108 * This function may be called from IRQ context.
110 void enable_irq(unsigned int irq)
112 struct irqdesc *desc = irq_desc + irq;
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) {
123 desc->chip->unmask(irq);
124 pending = desc->pending;
127 * If the interrupt was waiting to be processed,
131 desc->chip->rerun(irq);
133 spin_unlock_irqrestore(&irq_controller_lock, flags);
136 int show_interrupts(struct seq_file *p, void *v)
138 int i = *(loff_t *) v;
139 struct irqaction * action;
142 action = irq_desc[i].action;
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);
151 } else if (i == NR_IRQS) {
153 seq_printf(p, "Err: %10lu\n", irq_err_count);
160 * IRQ lock detection.
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).
167 static int check_irq_lock(struct irqdesc *desc, int irq, struct pt_regs *regs)
169 unsigned long instr_ptr = instruction_pointer(regs);
171 if (desc->lck_jif == jiffies &&
172 desc->lck_pc >= instr_ptr && desc->lck_pc < instr_ptr + 8) {
175 if (desc->lck_cnt > MAX_IRQ_CNT) {
176 printk(KERN_ERR "IRQ LOCK: IRQ%d is locking the system, disabled\n", irq);
181 desc->lck_pc = instruction_pointer(regs);
182 desc->lck_jif = jiffies;
188 __do_irq(unsigned int irq, struct irqaction *action, struct pt_regs *regs)
193 spin_unlock(&irq_controller_lock);
194 if (!(action->flags & SA_INTERRUPT))
199 ret = action->handler(irq, action->dev_id, regs);
200 if (ret == IRQ_HANDLED)
201 status |= action->flags;
202 action = action->next;
205 if (status & SA_SAMPLE_RANDOM)
206 add_interrupt_randomness(irq);
208 spin_lock_irq(&irq_controller_lock);
212 * This is for software-decoded IRQs. The caller is expected to
213 * handle the ack, clear, mask and unmask issues.
216 do_simple_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
218 struct irqaction *action;
219 const int cpu = smp_processor_id();
223 kstat_cpu(cpu).irqs[irq]++;
225 action = desc->action;
227 __do_irq(irq, desc->action, regs);
231 * Most edge-triggered IRQ implementations seem to take a broken
232 * approach to this. Hence the complexity.
235 do_edge_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
237 const int cpu = smp_processor_id();
242 * If we're currently running this IRQ, or its disabled,
243 * we shouldn't process the IRQ. Instead, turn on the
246 if (unlikely(desc->running || !desc->enabled))
250 * Acknowledge and clear the IRQ, but don't mask it.
252 desc->chip->ack(irq);
255 * Mark the IRQ currently in progress.
259 kstat_cpu(cpu).irqs[irq]++;
262 struct irqaction *action;
264 action = desc->action;
268 if (desc->pending && desc->enabled) {
270 desc->chip->unmask(irq);
273 __do_irq(irq, action, regs);
274 } while (desc->pending);
279 * If we were disabled or freed, shut down the handler.
281 if (likely(desc->action && !check_irq_lock(desc, irq, regs)))
286 * We got another IRQ while this one was masked or
287 * currently running. Delay it.
290 desc->chip->mask(irq);
291 desc->chip->ack(irq);
295 * Level-based IRQ handler. Nice and simple.
298 do_level_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
300 struct irqaction *action;
301 const int cpu = smp_processor_id();
306 * Acknowledge, clear _AND_ disable the interrupt.
308 desc->chip->ack(irq);
310 if (likely(desc->enabled)) {
311 kstat_cpu(cpu).irqs[irq]++;
314 * Return with this interrupt masked if no action
316 action = desc->action;
318 __do_irq(irq, desc->action, regs);
320 if (likely(desc->enabled &&
321 !check_irq_lock(desc, irq, regs)))
322 desc->chip->unmask(irq);
328 * do_IRQ handles all hardware IRQ's. Decoded IRQs should not
329 * come via this function. Instead, they should provide their
332 asmlinkage void asm_do_IRQ(int irq, struct pt_regs *regs)
334 struct irqdesc *desc = irq_desc + irq;
337 * Some hardware gives randomly wrong interrupts. Rather
338 * than crashing, do something sensible.
341 desc = &bad_irq_desc;
344 spin_lock(&irq_controller_lock);
345 desc->handle(irq, desc, regs);
346 spin_unlock(&irq_controller_lock);
350 void __set_irq_handler(unsigned int irq, irq_handler_t handle, int is_chained)
352 struct irqdesc *desc;
355 if (irq >= NR_IRQS) {
356 printk(KERN_ERR "Trying to install handler for IRQ%d\n", irq);
363 desc = irq_desc + irq;
365 if (is_chained && desc->chip == &bad_chip)
366 printk(KERN_WARNING "Trying to install chained handler for IRQ%d\n", irq);
368 spin_lock_irqsave(&irq_controller_lock, flags);
369 if (handle == do_bad_IRQ) {
370 desc->chip->mask(irq);
371 desc->chip->ack(irq);
375 desc->handle = handle;
376 if (handle != do_bad_IRQ && is_chained) {
380 desc->chip->unmask(irq);
382 spin_unlock_irqrestore(&irq_controller_lock, flags);
385 void set_irq_chip(unsigned int irq, struct irqchip *chip)
387 struct irqdesc *desc;
390 if (irq >= NR_IRQS) {
391 printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
398 desc = irq_desc + irq;
399 spin_lock_irqsave(&irq_controller_lock, flags);
401 spin_unlock_irqrestore(&irq_controller_lock, flags);
404 int set_irq_type(unsigned int irq, unsigned int type)
406 struct irqdesc *desc;
410 if (irq >= NR_IRQS) {
411 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
415 desc = irq_desc + irq;
416 if (desc->chip->type) {
417 spin_lock_irqsave(&irq_controller_lock, flags);
418 ret = desc->chip->type(irq, type);
419 spin_unlock_irqrestore(&irq_controller_lock, flags);
425 void set_irq_flags(unsigned int irq, unsigned int iflags)
427 struct irqdesc *desc;
430 if (irq >= NR_IRQS) {
431 printk(KERN_ERR "Trying to set irq flags for IRQ%d\n", irq);
435 desc = irq_desc + irq;
436 spin_lock_irqsave(&irq_controller_lock, flags);
437 desc->valid = (iflags & IRQF_VALID) != 0;
438 desc->probe_ok = (iflags & IRQF_PROBE) != 0;
439 desc->noautoenable = (iflags & IRQF_NOAUTOEN) != 0;
440 spin_unlock_irqrestore(&irq_controller_lock, flags);
443 int setup_irq(unsigned int irq, struct irqaction *new)
446 struct irqaction *old, **p;
448 struct irqdesc *desc;
451 * Some drivers like serial.c use request_irq() heavily,
452 * so we have to be careful not to interfere with a
455 if (new->flags & SA_SAMPLE_RANDOM) {
457 * This function might sleep, we want to call it first,
458 * outside of the atomic block.
459 * Yes, this might clear the entropy pool if the wrong
460 * driver is attempted to be loaded, without actually
461 * installing a new handler, but is this really a problem,
462 * only the sysadmin is able to do this.
464 rand_initialize_irq(irq);
468 * The following block of code has to be executed atomically
470 desc = irq_desc + irq;
471 spin_lock_irqsave(&irq_controller_lock, flags);
473 if ((old = *p) != NULL) {
474 /* Can't share interrupts unless both agree to */
475 if (!(old->flags & new->flags & SA_SHIRQ)) {
476 spin_unlock_irqrestore(&irq_controller_lock, flags);
480 /* add new interrupt at end of irq queue */
495 if (!desc->noautoenable) {
498 desc->chip->unmask(irq);
502 spin_unlock_irqrestore(&irq_controller_lock, flags);
507 * request_irq - allocate an interrupt line
508 * @irq: Interrupt line to allocate
509 * @handler: Function to be called when the IRQ occurs
510 * @irqflags: Interrupt type flags
511 * @devname: An ascii name for the claiming device
512 * @dev_id: A cookie passed back to the handler function
514 * This call allocates interrupt resources and enables the
515 * interrupt line and IRQ handling. From the point this
516 * call is made your handler function may be invoked. Since
517 * your handler function must clear any interrupt the board
518 * raises, you must take care both to initialise your hardware
519 * and to set up the interrupt handler in the right order.
521 * Dev_id must be globally unique. Normally the address of the
522 * device data structure is used as the cookie. Since the handler
523 * receives this value it makes sense to use it.
525 * If your interrupt is shared you must pass a non NULL dev_id
526 * as this is required when freeing the interrupt.
530 * SA_SHIRQ Interrupt is shared
532 * SA_INTERRUPT Disable local interrupts while processing
534 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
538 //FIXME - handler used to return void - whats the significance of the change?
539 int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
540 unsigned long irq_flags, const char * devname, void *dev_id)
542 unsigned long retval;
543 struct irqaction *action;
545 if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
546 (irq_flags & SA_SHIRQ && !dev_id))
549 action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
553 action->handler = handler;
554 action->flags = irq_flags;
555 cpus_clear(action->mask);
556 action->name = devname;
558 action->dev_id = dev_id;
560 retval = setup_irq(irq, action);
567 EXPORT_SYMBOL(request_irq);
570 * free_irq - free an interrupt
571 * @irq: Interrupt line to free
572 * @dev_id: Device identity to free
574 * Remove an interrupt handler. The handler is removed and if the
575 * interrupt line is no longer in use by any driver it is disabled.
576 * On a shared IRQ the caller must ensure the interrupt is disabled
577 * on the card it drives before calling this function.
579 * This function may be called from interrupt context.
581 void free_irq(unsigned int irq, void *dev_id)
583 struct irqaction * action, **p;
586 if (irq >= NR_IRQS || !irq_desc[irq].valid) {
587 printk(KERN_ERR "Trying to free IRQ%d\n",irq);
588 #ifdef CONFIG_DEBUG_ERRORS
594 spin_lock_irqsave(&irq_controller_lock, flags);
595 for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
596 if (action->dev_id != dev_id)
599 /* Found it - now free it */
604 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
605 #ifdef CONFIG_DEBUG_ERRORS
609 spin_unlock_irqrestore(&irq_controller_lock, flags);
612 EXPORT_SYMBOL(free_irq);
614 /* Start the interrupt probing. Unlike other architectures,
615 * we don't return a mask of interrupts from probe_irq_on,
616 * but return the number of interrupts enabled for the probe.
617 * The interrupts which have been enabled for probing is
618 * instead recorded in the irq_desc structure.
620 unsigned long probe_irq_on(void)
622 unsigned int i, irqs = 0;
626 * first snaffle up any unassigned but
627 * probe-able interrupts
629 spin_lock_irq(&irq_controller_lock);
630 for (i = 0; i < NR_IRQS; i++) {
631 if (!irq_desc[i].probe_ok || irq_desc[i].action)
634 irq_desc[i].probing = 1;
635 irq_desc[i].triggered = 0;
636 if (irq_desc[i].chip->type)
637 irq_desc[i].chip->type(i, IRQT_PROBE);
638 irq_desc[i].chip->unmask(i);
641 spin_unlock_irq(&irq_controller_lock);
644 * wait for spurious interrupts to mask themselves out again
646 for (delay = jiffies + HZ/10; time_before(jiffies, delay); )
647 /* min 100ms delay */;
650 * now filter out any obviously spurious interrupts
652 spin_lock_irq(&irq_controller_lock);
653 for (i = 0; i < NR_IRQS; i++) {
654 if (irq_desc[i].probing && irq_desc[i].triggered) {
655 irq_desc[i].probing = 0;
659 spin_unlock_irq(&irq_controller_lock);
664 EXPORT_SYMBOL(probe_irq_on);
667 * Possible return values:
668 * >= 0 - interrupt number
669 * -1 - no interrupt/many interrupts
671 int probe_irq_off(unsigned long irqs)
674 int irq_found = NO_IRQ;
677 * look at the interrupts, and find exactly one
678 * that we were probing has been triggered
680 spin_lock_irq(&irq_controller_lock);
681 for (i = 0; i < NR_IRQS; i++) {
682 if (irq_desc[i].probing &&
683 irq_desc[i].triggered) {
684 if (irq_found != NO_IRQ) {
695 spin_unlock_irq(&irq_controller_lock);
700 EXPORT_SYMBOL(probe_irq_off);
702 void __init init_irq_proc(void)
706 void __init init_IRQ(void)
708 struct irqdesc *desc;
709 extern void init_dma(void);
712 for (irq = 0, desc = irq_desc; irq < NR_IRQS; irq++, desc++)
713 *desc = bad_irq_desc;