2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Code to handle x86 style IRQs plus some generic interrupt stuff.
8 * Copyright (C) 1992 Linus Torvalds
9 * Copyright (C) 1994 - 2000 Ralf Baechle
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel_stat.h>
17 #include <linux/module.h>
18 #include <linux/proc_fs.h>
19 #include <linux/slab.h>
21 #include <linux/random.h>
22 #include <linux/sched.h>
23 #include <linux/seq_file.h>
24 #include <linux/kallsyms.h>
26 #include <asm/atomic.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
31 * Controller mappings for all interrupt sources:
33 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
35 .handler = &no_irq_type,
36 .lock = SPIN_LOCK_UNLOCKED
40 static void register_irq_proc (unsigned int irq);
43 * Special irq handlers.
46 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
50 * Generic no controller code
53 static void enable_none(unsigned int irq) { }
54 static unsigned int startup_none(unsigned int irq) { return 0; }
55 static void disable_none(unsigned int irq) { }
56 static void ack_none(unsigned int irq)
59 * 'what should we do if we get a hw irq event on an illegal vector'.
60 * each architecture has to answer this themselves, it doesn't deserve
61 * a generic callback i think.
63 printk("unexpected interrupt %d\n", irq);
66 /* startup is the same as "enable", shutdown is same as "disable" */
67 #define shutdown_none disable_none
68 #define end_none enable_none
70 struct hw_interrupt_type no_irq_type = {
80 atomic_t irq_err_count;
83 * Generic, controller-independent functions:
86 int show_interrupts(struct seq_file *p, void *v)
88 int i = *(loff_t *) v, j;
89 struct irqaction * action;
94 for (j=0; j<NR_CPUS; j++)
96 seq_printf(p, "CPU%d ",j);
101 spin_lock_irqsave(&irq_desc[i].lock, flags);
102 action = irq_desc[i].action;
105 seq_printf(p, "%3d: ",i);
107 seq_printf(p, "%10u ", kstat_irqs(i));
109 for (j = 0; j < NR_CPUS; j++)
111 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
113 seq_printf(p, " %14s", irq_desc[i].handler->typename);
114 seq_printf(p, " %s", action->name);
116 for (action=action->next; action; action = action->next)
117 seq_printf(p, ", %s", action->name);
121 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
122 } else if (i == NR_IRQS) {
124 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
130 inline void synchronize_irq(unsigned int irq)
132 while (irq_desc[irq].status & IRQ_INPROGRESS)
138 * This should really return information about whether
139 * we should do bottom half handling etc. Right now we
140 * end up _always_ checking the bottom half, which is a
141 * waste of time and is not what some drivers would
144 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
146 int status = 1; /* Force the "do bottom halves" bit */
149 if (!(action->flags & SA_INTERRUPT))
153 status |= action->flags;
154 retval |= action->handler(irq, action->dev_id, regs);
155 action = action->next;
157 if (status & SA_SAMPLE_RANDOM)
158 add_interrupt_randomness(irq);
164 static void __report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
166 struct irqaction *action;
168 if (action_ret != IRQ_HANDLED && action_ret != IRQ_NONE) {
169 printk(KERN_ERR "irq event %d: bogus return value %x\n",
172 printk(KERN_ERR "irq %d: nobody cared!\n", irq);
175 printk(KERN_ERR "handlers:\n");
176 action = desc->action;
178 printk(KERN_ERR "[<%p>]", action->handler);
179 print_symbol(" (%s)",
180 (unsigned long)action->handler);
182 action = action->next;
186 static void report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
188 static int count = 100;
192 __report_bad_irq(irq, desc, action_ret);
196 static int noirqdebug;
198 static int __init noirqdebug_setup(char *str)
201 printk("IRQ lockup detection disabled\n");
205 __setup("noirqdebug", noirqdebug_setup);
208 * If 99,900 of the previous 100,000 interrupts have not been handled then
209 * assume that the IRQ is stuck in some manner. Drop a diagnostic and try to
212 * (The other 100-of-100,000 interrupts may have been a correctly-functioning
213 * device sharing an IRQ with the failing one)
215 * Called under desc->lock
217 static void note_interrupt(int irq, irq_desc_t *desc, irqreturn_t action_ret)
219 if (action_ret != IRQ_HANDLED) {
220 desc->irqs_unhandled++;
221 if (action_ret != IRQ_NONE)
222 report_bad_irq(irq, desc, action_ret);
226 if (desc->irq_count < 100000)
230 if (desc->irqs_unhandled > 99900) {
232 * The interrupt is stuck
234 __report_bad_irq(irq, desc, action_ret);
238 printk(KERN_EMERG "Disabling IRQ #%d\n", irq);
239 desc->status |= IRQ_DISABLED;
240 desc->handler->disable(irq);
242 desc->irqs_unhandled = 0;
246 * Generic enable/disable code: this just calls
247 * down into the PIC-specific version for the actual
248 * hardware disable after having gotten the irq
253 * disable_irq_nosync - disable an irq without waiting
254 * @irq: Interrupt to disable
256 * Disable the selected interrupt line. Disables of an interrupt
257 * stack. Unlike disable_irq(), this function does not ensure existing
258 * instances of the IRQ handler have completed before returning.
260 * This function may be called from IRQ context.
263 void inline disable_irq_nosync(unsigned int irq)
265 irq_desc_t *desc = irq_desc + irq;
268 spin_lock_irqsave(&desc->lock, flags);
269 if (!desc->depth++) {
270 desc->status |= IRQ_DISABLED;
271 desc->handler->disable(irq);
273 spin_unlock_irqrestore(&desc->lock, flags);
277 * disable_irq - disable an irq and wait for completion
278 * @irq: Interrupt to disable
280 * Disable the selected interrupt line. Disables of an interrupt
281 * stack. That is for two disables you need two enables. This
282 * function waits for any pending IRQ handlers for this interrupt
283 * to complete before returning. If you use this function while
284 * holding a resource the IRQ handler may need you will deadlock.
286 * This function may be called - with care - from IRQ context.
289 void disable_irq(unsigned int irq)
291 disable_irq_nosync(irq);
292 synchronize_irq(irq);
296 * enable_irq - enable interrupt handling on an irq
297 * @irq: Interrupt to enable
299 * Re-enables the processing of interrupts on this IRQ line
300 * providing no disable_irq calls are now in effect.
302 * This function may be called from IRQ context.
305 void enable_irq(unsigned int irq)
307 irq_desc_t *desc = irq_desc + irq;
310 spin_lock_irqsave(&desc->lock, flags);
311 switch (desc->depth) {
313 unsigned int status = desc->status & ~IRQ_DISABLED;
314 desc->status = status;
315 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
316 desc->status = status | IRQ_REPLAY;
317 hw_resend_irq(desc->handler,irq);
319 desc->handler->enable(irq);
326 printk("enable_irq(%u) unbalanced from %p\n", irq,
327 __builtin_return_address(0));
329 spin_unlock_irqrestore(&desc->lock, flags);
333 * do_IRQ handles all normal device IRQ's (the special
334 * SMP cross-CPU interrupts have their own specific
337 asmlinkage unsigned int do_IRQ(int irq, struct pt_regs *regs)
340 * We ack quickly, we don't want the irq controller
341 * thinking we're snobs just because some other CPU has
342 * disabled global interrupts (we have already done the
343 * INT_ACK cycles, it's too late to try to pretend to the
344 * controller that we aren't taking the interrupt).
346 * 0 return value means that this irq is already being
347 * handled by some other CPU. (or is disabled)
349 irq_desc_t *desc = irq_desc + irq;
350 struct irqaction * action;
354 kstat_this_cpu.irqs[irq]++;
355 spin_lock(&desc->lock);
356 desc->handler->ack(irq);
358 REPLAY is when Linux resends an IRQ that was dropped earlier
359 WAITING is used by probe to mark irqs that are being tested
361 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
362 status |= IRQ_PENDING; /* we _want_ to handle it */
365 * If the IRQ is disabled for whatever reason, we cannot
366 * use the action we have.
369 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
370 action = desc->action;
371 status &= ~IRQ_PENDING; /* we commit to handling */
372 status |= IRQ_INPROGRESS; /* we are handling it */
374 desc->status = status;
377 * If there is no IRQ handler or it was disabled, exit early.
378 Since we set PENDING, if another processor is handling
379 a different instance of this same irq, the other processor
380 will take care of it.
382 if (unlikely(!action))
386 * Edge triggered interrupts need to remember
388 * This applies to any hw interrupts that allow a second
389 * instance of the same irq to arrive while we are in do_IRQ
390 * or in the handler. But the code here only handles the _second_
391 * instance of the irq, not the third or fourth. So it is mostly
392 * useful for irq hardware that does not mask cleanly in an
396 irqreturn_t action_ret;
398 spin_unlock(&desc->lock);
399 action_ret = handle_IRQ_event(irq, regs, action);
400 spin_lock(&desc->lock);
402 note_interrupt(irq, desc, action_ret);
403 if (likely(!(desc->status & IRQ_PENDING)))
405 desc->status &= ~IRQ_PENDING;
407 desc->status &= ~IRQ_INPROGRESS;
411 * The ->end() handler has to deal with interrupts which got
412 * disabled while the handler was running.
414 desc->handler->end(irq);
415 spin_unlock(&desc->lock);
423 * request_irq - allocate an interrupt line
424 * @irq: Interrupt line to allocate
425 * @handler: Function to be called when the IRQ occurs
426 * @irqflags: Interrupt type flags
427 * @devname: An ascii name for the claiming device
428 * @dev_id: A cookie passed back to the handler function
430 * This call allocates interrupt resources and enables the
431 * interrupt line and IRQ handling. From the point this
432 * call is made your handler function may be invoked. Since
433 * your handler function must clear any interrupt the board
434 * raises, you must take care both to initialise your hardware
435 * and to set up the interrupt handler in the right order.
437 * Dev_id must be globally unique. Normally the address of the
438 * device data structure is used as the cookie. Since the handler
439 * receives this value it makes sense to use it.
441 * If your interrupt is shared you must pass a non NULL dev_id
442 * as this is required when freeing the interrupt.
446 * SA_SHIRQ Interrupt is shared
448 * SA_INTERRUPT Disable local interrupts while processing
450 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
454 int request_irq(unsigned int irq,
455 irqreturn_t (*handler)(int, void *, struct pt_regs *),
456 unsigned long irqflags,
457 const char * devname,
461 struct irqaction * action;
465 * Sanity-check: shared interrupts should REALLY pass in
466 * a real dev-ID, otherwise we'll have trouble later trying
467 * to figure out which interrupt is which (messes up the
468 * interrupt freeing logic etc).
470 if (irqflags & SA_SHIRQ) {
472 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
481 action = (struct irqaction *)
482 kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
486 action->handler = handler;
487 action->flags = irqflags;
489 action->name = devname;
491 action->dev_id = dev_id;
493 retval = setup_irq(irq, action);
499 EXPORT_SYMBOL(request_irq);
502 * free_irq - free an interrupt
503 * @irq: Interrupt line to free
504 * @dev_id: Device identity to free
506 * Remove an interrupt handler. The handler is removed and if the
507 * interrupt line is no longer in use by any driver it is disabled.
508 * On a shared IRQ the caller must ensure the interrupt is disabled
509 * on the card it drives before calling this function. The function
510 * does not return until any executing interrupts for this IRQ
513 * This function must not be called from interrupt context.
516 void free_irq(unsigned int irq, void *dev_id)
519 struct irqaction **p;
525 desc = irq_desc + irq;
526 spin_lock_irqsave(&desc->lock,flags);
529 struct irqaction * action = *p;
531 struct irqaction **pp = p;
533 if (action->dev_id != dev_id)
536 /* Found it - now remove it from the list of entries */
539 desc->status |= IRQ_DISABLED;
540 desc->handler->shutdown(irq);
542 spin_unlock_irqrestore(&desc->lock,flags);
544 /* Wait to make sure it's not being used on another CPU */
545 synchronize_irq(irq);
549 printk("Trying to free free IRQ%d\n",irq);
550 spin_unlock_irqrestore(&desc->lock,flags);
555 EXPORT_SYMBOL(free_irq);
558 * IRQ autodetection code..
560 * This depends on the fact that any interrupt that
561 * comes in on to an unassigned handler will get stuck
562 * with "IRQ_WAITING" cleared and the interrupt
566 static DECLARE_MUTEX(probe_sem);
569 * probe_irq_on - begin an interrupt autodetect
571 * Commence probing for an interrupt. The interrupts are scanned
572 * and a mask of potential interrupt lines is returned.
576 unsigned long probe_irq_on(void)
585 * something may have generated an irq long ago and we want to
586 * flush such a longstanding irq before considering it as spurious.
588 for (i = NR_IRQS-1; i > 0; i--) {
591 spin_lock_irq(&desc->lock);
592 if (!irq_desc[i].action)
593 irq_desc[i].handler->startup(i);
594 spin_unlock_irq(&desc->lock);
597 /* Wait for longstanding interrupts to trigger. */
598 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
599 /* about 20ms delay */ barrier();
602 * enable any unassigned irqs
603 * (we must startup again here because if a longstanding irq
604 * happened in the previous stage, it may have masked itself)
606 for (i = NR_IRQS-1; i > 0; i--) {
609 spin_lock_irq(&desc->lock);
611 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
612 if (desc->handler->startup(i))
613 desc->status |= IRQ_PENDING;
615 spin_unlock_irq(&desc->lock);
619 * Wait for spurious interrupts to trigger
621 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
622 /* about 100ms delay */ barrier();
625 * Now filter out any obviously spurious interrupts
628 for (i = 0; i < NR_IRQS; i++) {
629 irq_desc_t *desc = irq_desc + i;
632 spin_lock_irq(&desc->lock);
633 status = desc->status;
635 if (status & IRQ_AUTODETECT) {
636 /* It triggered already - consider it spurious. */
637 if (!(status & IRQ_WAITING)) {
638 desc->status = status & ~IRQ_AUTODETECT;
639 desc->handler->shutdown(i);
644 spin_unlock_irq(&desc->lock);
650 EXPORT_SYMBOL(probe_irq_on);
653 * Return a mask of triggered interrupts (this
654 * can handle only legacy ISA interrupts).
658 * probe_irq_mask - scan a bitmap of interrupt lines
659 * @val: mask of interrupts to consider
661 * Scan the ISA bus interrupt lines and return a bitmap of
662 * active interrupts. The interrupt probe logic state is then
663 * returned to its previous value.
665 * Note: we need to scan all the irq's even though we will
666 * only return ISA irq numbers - just so that we reset them
667 * all to a known state.
669 unsigned int probe_irq_mask(unsigned long val)
675 for (i = 0; i < NR_IRQS; i++) {
676 irq_desc_t *desc = irq_desc + i;
679 spin_lock_irq(&desc->lock);
680 status = desc->status;
682 if (status & IRQ_AUTODETECT) {
683 if (i < 16 && !(status & IRQ_WAITING))
686 desc->status = status & ~IRQ_AUTODETECT;
687 desc->handler->shutdown(i);
689 spin_unlock_irq(&desc->lock);
697 * Return the one interrupt that triggered (this can
698 * handle any interrupt source).
702 * probe_irq_off - end an interrupt autodetect
703 * @val: mask of potential interrupts (unused)
705 * Scans the unused interrupt lines and returns the line which
706 * appears to have triggered the interrupt. If no interrupt was
707 * found then zero is returned. If more than one interrupt is
708 * found then minus the first candidate is returned to indicate
711 * The interrupt probe logic state is returned to its previous
714 * BUGS: When used in a module (which arguably shouldnt happen)
715 * nothing prevents two IRQ probe callers from overlapping. The
716 * results of this are non-optimal.
719 int probe_irq_off(unsigned long val)
721 int i, irq_found, nr_irqs;
725 for (i = 0; i < NR_IRQS; i++) {
726 irq_desc_t *desc = irq_desc + i;
729 spin_lock_irq(&desc->lock);
730 status = desc->status;
732 if (status & IRQ_AUTODETECT) {
733 if (!(status & IRQ_WAITING)) {
738 desc->status = status & ~IRQ_AUTODETECT;
739 desc->handler->shutdown(i);
741 spin_unlock_irq(&desc->lock);
746 irq_found = -irq_found;
750 EXPORT_SYMBOL(probe_irq_off);
752 /* this was setup_x86_irq but it seems pretty generic */
753 int setup_irq(unsigned int irq, struct irqaction * new)
757 struct irqaction *old, **p;
758 irq_desc_t *desc = irq_desc + irq;
761 * Some drivers like serial.c use request_irq() heavily,
762 * so we have to be careful not to interfere with a
765 if (new->flags & SA_SAMPLE_RANDOM) {
767 * This function might sleep, we want to call it first,
768 * outside of the atomic block.
769 * Yes, this might clear the entropy pool if the wrong
770 * driver is attempted to be loaded, without actually
771 * installing a new handler, but is this really a problem,
772 * only the sysadmin is able to do this.
774 rand_initialize_irq(irq);
778 * The following block of code has to be executed atomically
780 spin_lock_irqsave(&desc->lock,flags);
782 if ((old = *p) != NULL) {
783 /* Can't share interrupts unless both agree to */
784 if (!(old->flags & new->flags & SA_SHIRQ)) {
785 spin_unlock_irqrestore(&desc->lock,flags);
789 /* add new interrupt at end of irq queue */
801 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
802 desc->handler->startup(irq);
804 spin_unlock_irqrestore(&desc->lock,flags);
806 register_irq_proc(irq);
810 void __init init_generic_irq(void)
814 for (i = 0; i < NR_IRQS; i++) {
815 irq_desc[i].status = IRQ_DISABLED;
816 irq_desc[i].action = NULL;
817 irq_desc[i].depth = 1;
818 irq_desc[i].handler = &no_irq_type;
822 EXPORT_SYMBOL(disable_irq_nosync);
823 EXPORT_SYMBOL(disable_irq);
824 EXPORT_SYMBOL(enable_irq);
825 EXPORT_SYMBOL(probe_irq_mask);
827 static struct proc_dir_entry * root_irq_dir;
828 static struct proc_dir_entry * irq_dir [NR_IRQS];
832 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
834 static cpumask_t irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = ~0UL };
835 static int irq_affinity_read_proc (char *page, char **start, off_t off,
836 int count, int *eof, void *data)
838 int len = cpumask_snprintf(page, count, irq_affinity[(long)data]);
841 len += sprintf(page + len, "\n");
845 static int irq_affinity_write_proc (struct file *file, const char *buffer,
846 unsigned long count, void *data)
848 int irq = (long) data, full_count = count, err;
849 cpumask_t new_value, tmp;
851 if (!irq_desc[irq].handler->set_affinity)
854 err = cpumask_parse(buffer, count, new_value);
857 * Do not allow disabling IRQs completely - it's a too easy
858 * way to make the system unusable accidentally :-) At least
859 * one online CPU still has to be targeted.
861 cpus_and(tmp, tmp, cpu_online_map);
865 irq_affinity[irq] = new_value;
866 irq_desc[irq].handler->set_affinity(irq, new_value);
873 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
874 int count, int *eof, void *data)
876 int len = cpumask_snprintf(page, count, *(cpumask_t *)data);
879 len += sprintf(page + len, "\n");
883 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
884 unsigned long count, void *data)
886 cpumask_t *mask = (cpumask_t *)data, new_value;
887 unsigned long full_count = count, err;
889 err = cpumask_parse(buffer, count, new_value);
897 #define MAX_NAMELEN 10
899 static void register_irq_proc (unsigned int irq)
901 char name [MAX_NAMELEN];
903 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
907 memset(name, 0, MAX_NAMELEN);
908 sprintf(name, "%d", irq);
910 /* create /proc/irq/1234 */
911 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
915 struct proc_dir_entry *entry;
917 /* create /proc/irq/1234/smp_affinity */
918 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
922 entry->data = (void *)(long)irq;
923 entry->read_proc = irq_affinity_read_proc;
924 entry->write_proc = irq_affinity_write_proc;
927 smp_affinity_entry[irq] = entry;
932 unsigned long prof_cpu_mask = -1;
934 void init_irq_proc (void)
936 struct proc_dir_entry *entry;
939 /* create /proc/irq */
940 root_irq_dir = proc_mkdir("irq", 0);
942 /* create /proc/irq/prof_cpu_mask */
943 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
949 entry->data = (void *)&prof_cpu_mask;
950 entry->read_proc = prof_cpu_mask_read_proc;
951 entry->write_proc = prof_cpu_mask_write_proc;
954 * Create entries for all existing IRQs.
956 for (i = 0; i < NR_IRQS; i++)
957 register_irq_proc(i);