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/ptrace.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/signal.h>
24 #include <linux/sched.h>
25 #include <linux/ioport.h>
26 #include <linux/interrupt.h>
27 #include <linux/slab.h>
28 #include <linux/random.h>
29 #include <linux/smp.h>
30 #include <linux/init.h>
33 #include <asm/system.h>
34 #include <asm/mach/irq.h>
36 #include <asm/arch/irq.h> /* pick up fixup_irq definition */
39 * Maximum IRQ count. Currently, this is arbitary. However, it should
40 * not be set too low to prevent false triggering. Conversely, if it
41 * is set too high, then you could miss a stuck IRQ.
43 * Maybe we ought to set a timer and re-enable the IRQ at a later time?
45 #define MAX_IRQ_CNT 100000
47 static volatile unsigned long irq_err_count;
48 static spinlock_t irq_controller_lock;
50 struct irqdesc irq_desc[NR_IRQS];
51 void (*init_arch_irq)(void) __initdata = NULL;
54 * Dummy mask/unmask handler
56 static void dummy_mask_unmask_irq(unsigned int irq)
61 * disable_irq - disable an irq and wait for completion
62 * @irq: Interrupt to disable
64 * Disable the selected interrupt line.
66 * This function may be called - with care - from IRQ context.
68 void disable_irq(unsigned int irq)
72 spin_lock_irqsave(&irq_controller_lock, flags);
73 irq_desc[irq].enabled = 0;
74 irq_desc[irq].mask(irq);
75 spin_unlock_irqrestore(&irq_controller_lock, flags);
79 * enable_irq - enable interrupt handling on an irq
80 * @irq: Interrupt to enable
82 * Re-enables the processing of interrupts on this IRQ line
84 * This function may be called from IRQ context.
86 void enable_irq(unsigned int irq)
90 spin_lock_irqsave(&irq_controller_lock, flags);
91 irq_desc[irq].probing = 0;
92 irq_desc[irq].triggered = 0;
93 irq_desc[irq].enabled = 1;
94 irq_desc[irq].unmask(irq);
95 spin_unlock_irqrestore(&irq_controller_lock, flags);
98 int get_irq_list(char *buf)
101 struct irqaction * action;
104 for (i = 0 ; i < NR_IRQS ; i++) {
105 action = irq_desc[i].action;
108 p += sprintf(p, "%3d: %10u ", i, kstat_irqs(i));
109 p += sprintf(p, " %s", action->name);
110 for (action = action->next; action; action = action->next) {
111 p += sprintf(p, ", %s", action->name);
116 #ifdef CONFIG_ARCH_ACORN
117 p += get_fiq_list(p);
119 p += sprintf(p, "Err: %10lu\n", irq_err_count);
124 * IRQ lock detection.
126 * Hopefully, this should get us out of a few locked situations.
127 * However, it may take a while for this to happen, since we need
128 * a large number if IRQs to appear in the same jiffie with the
129 * same instruction pointer (or within 2 instructions).
131 static void check_irq_lock(struct irqdesc *desc, int irq, struct pt_regs *regs)
133 unsigned long instr_ptr = instruction_pointer(regs);
135 if (desc->lck_jif == jiffies &&
136 desc->lck_pc >= instr_ptr && desc->lck_pc < instr_ptr + 8) {
139 if (desc->lck_cnt > MAX_IRQ_CNT) {
140 printk(KERN_ERR "IRQ LOCK: IRQ%d is locking the system, disabled\n", irq);
145 desc->lck_pc = instruction_pointer(regs);
146 desc->lck_jif = jiffies;
151 * do_IRQ handles all normal device IRQ's
153 asmlinkage void do_IRQ(int irq, struct pt_regs * regs)
155 struct irqdesc * desc;
156 struct irqaction * action;
159 irq = fixup_irq(irq);
162 * Some hardware gives randomly wrong interrupts. Rather
163 * than crashing, do something sensible.
168 desc = irq_desc + irq;
170 spin_lock(&irq_controller_lock);
172 spin_unlock(&irq_controller_lock);
174 cpu = smp_processor_id();
176 kstat.irqs[cpu][irq]++;
179 /* Return with this interrupt masked if no action */
180 action = desc->action;
186 spin_lock(&irq_controller_lock);
188 spin_unlock(&irq_controller_lock);
191 if (!(action->flags & SA_INTERRUPT))
195 status |= action->flags;
196 action->handler(irq, action->dev_id, regs);
197 action = action->next;
200 if (status & SA_SAMPLE_RANDOM)
201 add_interrupt_randomness(irq);
204 if (!desc->nomask && desc->enabled) {
205 spin_lock(&irq_controller_lock);
207 spin_unlock(&irq_controller_lock);
212 * Debug measure - hopefully we can continue if an
213 * IRQ lockup problem occurs...
215 check_irq_lock(desc, irq, regs);
219 if (softirq_pending(cpu))
225 printk(KERN_ERR "IRQ: spurious interrupt %d\n", irq);
229 #ifdef CONFIG_ARCH_ACORN
230 void do_ecard_IRQ(int irq, struct pt_regs *regs)
232 struct irqdesc * desc;
233 struct irqaction * action;
236 desc = irq_desc + irq;
238 cpu = smp_processor_id();
239 kstat.irqs[cpu][irq]++;
242 action = desc->action;
246 action->handler(irq, action->dev_id, regs);
247 action = action->next;
250 spin_lock(&irq_controller_lock);
252 spin_unlock(&irq_controller_lock);
257 int setup_arm_irq(int irq, struct irqaction * new)
260 struct irqaction *old, **p;
262 struct irqdesc *desc;
265 * Some drivers like serial.c use request_irq() heavily,
266 * so we have to be careful not to interfere with a
269 if (new->flags & SA_SAMPLE_RANDOM) {
271 * This function might sleep, we want to call it first,
272 * outside of the atomic block.
273 * Yes, this might clear the entropy pool if the wrong
274 * driver is attempted to be loaded, without actually
275 * installing a new handler, but is this really a problem,
276 * only the sysadmin is able to do this.
278 rand_initialize_irq(irq);
282 * The following block of code has to be executed atomically
284 desc = irq_desc + irq;
285 spin_lock_irqsave(&irq_controller_lock, flags);
287 if ((old = *p) != NULL) {
288 /* Can't share interrupts unless both agree to */
289 if (!(old->flags & new->flags & SA_SHIRQ)) {
290 spin_unlock_irqrestore(&irq_controller_lock, flags);
294 /* add new interrupt at end of irq queue */
305 desc->nomask = (new->flags & SA_IRQNOMASK) ? 1 : 0;
307 if (!desc->noautoenable) {
313 spin_unlock_irqrestore(&irq_controller_lock, flags);
318 * request_irq - allocate an interrupt line
319 * @irq: Interrupt line to allocate
320 * @handler: Function to be called when the IRQ occurs
321 * @irqflags: Interrupt type flags
322 * @devname: An ascii name for the claiming device
323 * @dev_id: A cookie passed back to the handler function
325 * This call allocates interrupt resources and enables the
326 * interrupt line and IRQ handling. From the point this
327 * call is made your handler function may be invoked. Since
328 * your handler function must clear any interrupt the board
329 * raises, you must take care both to initialise your hardware
330 * and to set up the interrupt handler in the right order.
332 * Dev_id must be globally unique. Normally the address of the
333 * device data structure is used as the cookie. Since the handler
334 * receives this value it makes sense to use it.
336 * If your interrupt is shared you must pass a non NULL dev_id
337 * as this is required when freeing the interrupt.
341 * SA_SHIRQ Interrupt is shared
343 * SA_INTERRUPT Disable local interrupts while processing
345 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
348 int request_irq(unsigned int irq, void (*handler)(int, void *, struct pt_regs *),
349 unsigned long irq_flags, const char * devname, void *dev_id)
351 unsigned long retval;
352 struct irqaction *action;
354 if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
355 (irq_flags & SA_SHIRQ && !dev_id))
358 action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
362 action->handler = handler;
363 action->flags = irq_flags;
365 action->name = devname;
367 action->dev_id = dev_id;
369 retval = setup_arm_irq(irq, action);
377 * free_irq - free an interrupt
378 * @irq: Interrupt line to free
379 * @dev_id: Device identity to free
381 * Remove an interrupt handler. The handler is removed and if the
382 * interrupt line is no longer in use by any driver it is disabled.
383 * On a shared IRQ the caller must ensure the interrupt is disabled
384 * on the card it drives before calling this function.
386 * This function may be called from interrupt context.
388 void free_irq(unsigned int irq, void *dev_id)
390 struct irqaction * action, **p;
393 if (irq >= NR_IRQS || !irq_desc[irq].valid) {
394 printk(KERN_ERR "Trying to free IRQ%d\n",irq);
395 #ifdef CONFIG_DEBUG_ERRORS
401 spin_lock_irqsave(&irq_controller_lock, flags);
402 for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
403 if (action->dev_id != dev_id)
406 /* Found it - now free it */
411 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
412 #ifdef CONFIG_DEBUG_ERRORS
416 spin_unlock_irqrestore(&irq_controller_lock, flags);
419 /* Start the interrupt probing. Unlike other architectures,
420 * we don't return a mask of interrupts from probe_irq_on,
421 * but return the number of interrupts enabled for the probe.
422 * The interrupts which have been enabled for probing is
423 * instead recorded in the irq_desc structure.
425 unsigned long probe_irq_on(void)
427 unsigned int i, irqs = 0;
431 * first snaffle up any unassigned but
432 * probe-able interrupts
434 spin_lock_irq(&irq_controller_lock);
435 for (i = 0; i < NR_IRQS; i++) {
436 if (!irq_desc[i].valid ||
437 !irq_desc[i].probe_ok ||
441 irq_desc[i].probing = 1;
442 irq_desc[i].triggered = 0;
443 irq_desc[i].unmask(i);
446 spin_unlock_irq(&irq_controller_lock);
449 * wait for spurious interrupts to mask themselves out again
451 for (delay = jiffies + HZ/10; time_before(jiffies, delay); )
452 /* min 100ms delay */;
455 * now filter out any obviously spurious interrupts
457 spin_lock_irq(&irq_controller_lock);
458 for (i = 0; i < NR_IRQS; i++) {
459 if (irq_desc[i].probing &&
460 irq_desc[i].triggered) {
461 irq_desc[i].probing = 0;
465 spin_unlock_irq(&irq_controller_lock);
467 /* now filter out any obviously spurious interrupts */
472 * Possible return values:
473 * >= 0 - interrupt number
474 * -1 - no interrupt/many interrupts
476 int probe_irq_off(unsigned long irqs)
479 int irq_found = NO_IRQ;
482 * look at the interrupts, and find exactly one
483 * that we were probing has been triggered
485 spin_lock_irq(&irq_controller_lock);
486 for (i = 0; i < NR_IRQS; i++) {
487 if (irq_desc[i].probing &&
488 irq_desc[i].triggered) {
489 if (irq_found != NO_IRQ) {
500 spin_unlock_irq(&irq_controller_lock);
505 void __init init_irq_proc(void)
509 void __init init_IRQ(void)
511 extern void init_dma(void);
514 for (irq = 0; irq < NR_IRQS; irq++) {
515 irq_desc[irq].probe_ok = 0;
516 irq_desc[irq].valid = 0;
517 irq_desc[irq].noautoenable = 0;
518 irq_desc[irq].mask_ack = dummy_mask_unmask_irq;
519 irq_desc[irq].mask = dummy_mask_unmask_irq;
520 irq_desc[irq].unmask = dummy_mask_unmask_irq;