1 /* $Id: irq.c,v 1.20 2004/01/13 05:52:11 kkojima Exp $
3 * linux/arch/sh/kernel/irq.c
5 * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
8 * SuperH version: Copyright (C) 1999 Niibe Yutaka
12 * IRQs are in fact implemented a bit like signal handlers for the kernel.
13 * Naturally it's not a 1:1 relation, but there are similarities.
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/ptrace.h>
19 #include <linux/errno.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/ioport.h>
24 #include <linux/interrupt.h>
25 #include <linux/timex.h>
27 #include <linux/slab.h>
28 #include <linux/random.h>
29 #include <linux/smp.h>
30 #include <linux/smp_lock.h>
31 #include <linux/init.h>
32 #include <linux/seq_file.h>
33 #include <linux/kallsyms.h>
35 #include <asm/system.h>
37 #include <asm/bitops.h>
38 #include <asm/pgalloc.h>
39 #include <asm/delay.h>
41 #include <linux/irq.h>
44 * Controller mappings for all interrupt sources:
46 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
48 .handler = &no_irq_type,
49 .lock = SPIN_LOCK_UNLOCKED
54 * Special irq handlers.
57 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
61 * Generic no controller code
64 static void enable_none(unsigned int irq) { }
65 static unsigned int startup_none(unsigned int irq) { return 0; }
66 static void disable_none(unsigned int irq) { }
67 static void ack_none(unsigned int irq)
70 * 'what should we do if we get a hw irq event on an illegal vector'.
71 * each architecture has to answer this themselves, it doesn't deserve
72 * a generic callback i think.
74 printk("unexpected IRQ trap at vector %02x\n", irq);
77 /* startup is the same as "enable", shutdown is same as "disable" */
78 #define shutdown_none disable_none
79 #define end_none enable_none
81 struct hw_interrupt_type no_irq_type = {
92 * Generic, controller-independent functions:
95 #if defined(CONFIG_PROC_FS)
96 int show_interrupts(struct seq_file *p, void *v)
98 int i = *(loff_t *) v, j;
99 struct irqaction * action;
104 for (j=0; j<NR_CPUS; j++)
106 seq_printf(p, "CPU%d ",j);
110 if (i < ACTUAL_NR_IRQS) {
111 spin_lock_irqsave(&irq_desc[i].lock, flags);
112 action = irq_desc[i].action;
115 seq_printf(p, "%3d: ",i);
116 seq_printf(p, "%10u ", kstat_irqs(i));
117 seq_printf(p, " %14s", irq_desc[i].handler->typename);
118 seq_printf(p, " %s", action->name);
120 for (action=action->next; action; action = action->next)
121 seq_printf(p, ", %s", action->name);
124 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
131 * This should really return information about whether
132 * we should do bottom half handling etc. Right now we
133 * end up _always_ checking the bottom half, which is a
134 * waste of time and is not what some drivers would
137 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
139 int status = 1; /* Force the "do bottom halves" bit */
142 if (!(action->flags & SA_INTERRUPT))
146 status |= action->flags;
147 retval |= action->handler(irq, action->dev_id, regs);
148 action = action->next;
151 if (status & SA_SAMPLE_RANDOM)
152 add_interrupt_randomness(irq);
158 static void __report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
160 struct irqaction *action;
162 if (action_ret != IRQ_HANDLED && action_ret != IRQ_NONE) {
163 printk(KERN_ERR "irq event %d: bogus return value %x\n",
166 printk(KERN_ERR "irq %d: nobody cared!\n", irq);
169 printk(KERN_ERR "handlers:\n");
170 action = desc->action;
172 printk(KERN_ERR "[<%p>]", action->handler);
173 print_symbol(" (%s)",
174 (unsigned long)action->handler);
176 action = action->next;
180 static void report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
182 static int count = 100;
186 __report_bad_irq(irq, desc, action_ret);
190 static int noirqdebug;
192 static int __init noirqdebug_setup(char *str)
195 printk("IRQ lockup detection disabled\n");
199 __setup("noirqdebug", noirqdebug_setup);
202 * If 99,900 of the previous 100,000 interrupts have not been handled then
203 * assume that the IRQ is stuck in some manner. Drop a diagnostic and try to
206 * (The other 100-of-100,000 interrupts may have been a correctly-functioning
207 * device sharing an IRQ with the failing one)
209 * Called under desc->lock
211 static void note_interrupt(int irq, irq_desc_t *desc, irqreturn_t action_ret)
213 if (action_ret != IRQ_HANDLED) {
214 desc->irqs_unhandled++;
215 if (action_ret != IRQ_NONE)
216 report_bad_irq(irq, desc, action_ret);
220 if (desc->irq_count < 100000)
224 if (desc->irqs_unhandled > 99900) {
226 * The interrupt is stuck
228 __report_bad_irq(irq, desc, action_ret);
232 printk(KERN_EMERG "Disabling IRQ #%d\n", irq);
233 desc->status |= IRQ_DISABLED;
234 desc->handler->disable(irq);
236 desc->irqs_unhandled = 0;
240 * Generic enable/disable code: this just calls
241 * down into the PIC-specific version for the actual
242 * hardware disable after having gotten the irq
245 inline void disable_irq_nosync(unsigned int irq)
247 irq_desc_t *desc = irq_desc + irq;
250 spin_lock_irqsave(&desc->lock, flags);
251 if (!desc->depth++) {
252 desc->status |= IRQ_DISABLED;
253 desc->handler->disable(irq);
255 spin_unlock_irqrestore(&desc->lock, flags);
259 * Synchronous version of the above, making sure the IRQ is
260 * no longer running on any other IRQ..
262 void disable_irq(unsigned int irq)
264 irq_desc_t *desc = irq_desc + irq;
265 disable_irq_nosync(irq);
267 synchronize_irq(irq);
270 void enable_irq(unsigned int irq)
272 irq_desc_t *desc = irq_desc + irq;
275 spin_lock_irqsave(&desc->lock, flags);
276 switch (desc->depth) {
278 unsigned int status = desc->status & ~(IRQ_DISABLED | IRQ_INPROGRESS);
279 desc->status = status;
280 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
281 desc->status = status | IRQ_REPLAY;
282 hw_resend_irq(desc->handler,irq);
284 desc->handler->enable(irq);
291 printk("enable_irq() unbalanced from %p\n",
292 __builtin_return_address(0));
294 spin_unlock_irqrestore(&desc->lock, flags);
298 * do_IRQ handles all normal device IRQ's.
300 asmlinkage int do_IRQ(unsigned long r4, unsigned long r5,
301 unsigned long r6, unsigned long r7,
305 * We ack quickly, we don't want the irq controller
306 * thinking we're snobs just because some other CPU has
307 * disabled global interrupts (we have already done the
308 * INT_ACK cycles, it's too late to try to pretend to the
309 * controller that we aren't taking the interrupt).
311 * 0 return value means that this irq is already being
312 * handled by some other CPU. (or is disabled)
316 struct irqaction * action;
321 #ifdef CONFIG_PREEMPT
323 * At this point we're now about to actually call handlers,
324 * and interrupts might get reenabled during them... bump
325 * preempt_count to prevent any preemption while the handler
326 * called here is pending...
332 asm volatile("stc r2_bank, %0\n\t"
338 irq = irq_demux(irq);
340 kstat_this_cpu.irqs[irq]++;
341 desc = irq_desc + irq;
342 spin_lock(&desc->lock);
343 desc->handler->ack(irq);
345 REPLAY is when Linux resends an IRQ that was dropped earlier
346 WAITING is used by probe to mark irqs that are being tested
348 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
349 status |= IRQ_PENDING; /* we _want_ to handle it */
352 * If the IRQ is disabled for whatever reason, we cannot
353 * use the action we have.
356 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
357 action = desc->action;
358 status &= ~IRQ_PENDING; /* we commit to handling */
359 status |= IRQ_INPROGRESS; /* we are handling it */
361 desc->status = status;
364 * If there is no IRQ handler or it was disabled, exit early.
365 Since we set PENDING, if another processor is handling
366 a different instance of this same irq, the other processor
367 will take care of it.
369 if (unlikely(!action))
373 * Edge triggered interrupts need to remember
375 * This applies to any hw interrupts that allow a second
376 * instance of the same irq to arrive while we are in do_IRQ
377 * or in the handler. But the code here only handles the _second_
378 * instance of the irq, not the third or fourth. So it is mostly
379 * useful for irq hardware that does not mask cleanly in an
383 irqreturn_t action_ret;
385 spin_unlock(&desc->lock);
386 action_ret = handle_IRQ_event(irq, ®s, action);
387 spin_lock(&desc->lock);
389 note_interrupt(irq, desc, action_ret);
390 if (likely(!(desc->status & IRQ_PENDING)))
392 desc->status &= ~IRQ_PENDING;
394 desc->status &= ~IRQ_INPROGRESS;
398 * The ->end() handler has to deal with interrupts which got
399 * disabled while the handler was running.
401 desc->handler->end(irq);
402 spin_unlock(&desc->lock);
406 #ifdef CONFIG_PREEMPT
408 * We're done with the handlers, interrupts should be
409 * currently disabled; decrement preempt_count now so
410 * as we return preemption may be allowed...
412 preempt_enable_no_resched();
418 int request_irq(unsigned int irq,
419 irqreturn_t (*handler)(int, void *, struct pt_regs *),
420 unsigned long irqflags,
421 const char * devname,
425 struct irqaction * action;
427 if (irq >= ACTUAL_NR_IRQS)
432 action = (struct irqaction *)
433 kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
437 action->handler = handler;
438 action->flags = irqflags;
439 cpus_clear(action->mask);
440 action->name = devname;
442 action->dev_id = dev_id;
444 retval = setup_irq(irq, action);
450 EXPORT_SYMBOL(request_irq);
452 void free_irq(unsigned int irq, void *dev_id)
455 struct irqaction **p;
458 if (irq >= ACTUAL_NR_IRQS)
461 desc = irq_desc + irq;
462 spin_lock_irqsave(&desc->lock,flags);
465 struct irqaction * action = *p;
467 struct irqaction **pp = p;
469 if (action->dev_id != dev_id)
472 /* Found it - now remove it from the list of entries */
475 desc->status |= IRQ_DISABLED;
476 desc->handler->shutdown(irq);
478 spin_unlock_irqrestore(&desc->lock,flags);
479 synchronize_irq(irq);
483 printk("Trying to free free IRQ%d\n",irq);
484 spin_unlock_irqrestore(&desc->lock,flags);
489 EXPORT_SYMBOL(free_irq);
491 static DECLARE_MUTEX(probe_sem);
494 * IRQ autodetection code..
496 * This depends on the fact that any interrupt that
497 * comes in on to an unassigned handler will get stuck
498 * with "IRQ_WAITING" cleared and the interrupt
501 unsigned long probe_irq_on(void)
510 * something may have generated an irq long ago and we want to
511 * flush such a longstanding irq before considering it as spurious.
513 for (i = NR_IRQS-1; i > 0; i--) {
516 spin_lock_irq(&desc->lock);
518 desc->handler->startup(i);
519 spin_unlock_irq(&desc->lock);
522 /* Wait for longstanding interrupts to trigger. */
523 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
524 /* about 20ms delay */ barrier();
527 * enable any unassigned irqs
528 * (we must startup again here because if a longstanding irq
529 * happened in the previous stage, it may have masked itself)
531 for (i = NR_IRQS-1; i > 0; i--) {
534 spin_lock_irq(&desc->lock);
536 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
537 if (desc->handler->startup(i))
538 desc->status |= IRQ_PENDING;
540 spin_unlock_irq(&desc->lock);
544 * Wait for spurious interrupts to trigger
546 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
547 /* about 100ms delay */ barrier();
550 * Now filter out any obviously spurious interrupts
553 for (i=0; i<NR_IRQS; i++) {
558 spin_lock_irq(&desc->lock);
559 status = desc->status;
561 if (status & IRQ_AUTODETECT) {
562 /* It triggered already - consider it spurious. */
563 if (!(status & IRQ_WAITING)) {
564 desc->status = status & ~IRQ_AUTODETECT;
565 desc->handler->shutdown(i);
570 spin_unlock_irq(&desc->lock);
576 EXPORT_SYMBOL(probe_irq_on);
578 /* Return a mask of triggered interrupts (this
579 * can handle only legacy ISA interrupts).
583 * probe_irq_mask - scan a bitmap of interrupt lines
584 * @val: mask of interrupts to consider
586 * Scan the ISA bus interrupt lines and return a bitmap of
587 * active interrupts. The interrupt probe logic state is then
588 * returned to its previous value.
590 * Note: we need to scan all the irq's even though we will
591 * only return ISA irq numbers - just so that we reset them
592 * all to a known state.
594 unsigned int probe_irq_mask(unsigned long val)
600 for (i = 0; i < NR_IRQS; i++) {
601 irq_desc_t *desc = irq_desc + i;
604 spin_lock_irq(&desc->lock);
605 status = desc->status;
607 if (status & IRQ_AUTODETECT) {
608 if (i < 16 && !(status & IRQ_WAITING))
611 desc->status = status & ~IRQ_AUTODETECT;
612 desc->handler->shutdown(i);
614 spin_unlock_irq(&desc->lock);
621 int probe_irq_off(unsigned long val)
623 int i, irq_found, nr_irqs;
627 for (i=0; i<NR_IRQS; i++) {
628 irq_desc_t *desc = irq_desc + i;
631 spin_lock_irq(&desc->lock);
632 status = desc->status;
634 if (status & IRQ_AUTODETECT) {
635 if (!(status & IRQ_WAITING)) {
640 desc->status = status & ~IRQ_AUTODETECT;
641 desc->handler->shutdown(i);
643 spin_unlock_irq(&desc->lock);
648 irq_found = -irq_found;
652 EXPORT_SYMBOL(probe_irq_off);
654 int setup_irq(unsigned int irq, struct irqaction * new)
657 struct irqaction *old, **p;
659 irq_desc_t *desc = irq_desc + irq;
661 if (desc->handler == &no_irq_type)
664 * Some drivers like serial.c use request_irq() heavily,
665 * so we have to be careful not to interfere with a
668 if (new->flags & SA_SAMPLE_RANDOM) {
670 * This function might sleep, we want to call it first,
671 * outside of the atomic block.
672 * Yes, this might clear the entropy pool if the wrong
673 * driver is attempted to be loaded, without actually
674 * installing a new handler, but is this really a problem,
675 * only the sysadmin is able to do this.
677 rand_initialize_irq(irq);
681 * The following block of code has to be executed atomically
683 spin_lock_irqsave(&desc->lock,flags);
685 if ((old = *p) != NULL) {
686 /* Can't share interrupts unless both agree to */
687 if (!(old->flags & new->flags & SA_SHIRQ)) {
688 spin_unlock_irqrestore(&desc->lock,flags);
692 /* add new interrupt at end of irq queue */
704 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
705 desc->handler->startup(irq);
707 spin_unlock_irqrestore(&desc->lock,flags);
711 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
713 void init_irq_proc(void)
718 /* Taken from the 2.5 alpha port */
720 void synchronize_irq(unsigned int irq)
722 /* is there anything to synchronize with? */
723 if (!irq_desc[irq].action)
726 while (irq_desc[irq].status & IRQ_INPROGRESS)