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>
24 #include <asm/atomic.h>
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
29 * Controller mappings for all interrupt sources:
31 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned =
32 { [0 ... NR_IRQS-1] = { 0, &no_irq_type, NULL, 0, SPIN_LOCK_UNLOCKED}};
34 static void register_irq_proc (unsigned int irq);
37 * Special irq handlers.
40 void no_action(int cpl, void *dev_id, struct pt_regs *regs) { }
43 * Generic no controller code
46 static void enable_none(unsigned int irq) { }
47 static unsigned int startup_none(unsigned int irq) { return 0; }
48 static void disable_none(unsigned int irq) { }
49 static void ack_none(unsigned int irq)
52 * 'what should we do if we get a hw irq event on an illegal vector'.
53 * each architecture has to answer this themselves, it doesn't deserve
54 * a generic callback i think.
56 printk("unexpected interrupt %d\n", irq);
59 /* startup is the same as "enable", shutdown is same as "disable" */
60 #define shutdown_none disable_none
61 #define end_none enable_none
63 struct hw_interrupt_type no_irq_type = {
73 atomic_t irq_err_count;
76 * Generic, controller-independent functions:
79 int get_irq_list(char *buf)
82 struct irqaction * action;
86 for (j=0; j<smp_num_cpus; j++)
87 p += sprintf(p, "CPU%d ",j);
90 for (i = 0 ; i < NR_IRQS ; i++) {
91 action = irq_desc[i].action;
94 p += sprintf(p, "%3d: ",i);
96 p += sprintf(p, "%10u ", kstat_irqs(i));
98 for (j = 0; j < smp_num_cpus; j++)
99 p += sprintf(p, "%10u ",
100 kstat.irqs[cpu_logical_map(j)][i]);
102 p += sprintf(p, " %14s", irq_desc[i].handler->typename);
103 p += sprintf(p, " %s", action->name);
105 for (action=action->next; action; action = action->next)
106 p += sprintf(p, ", %s", action->name);
109 p += sprintf(p, "\n");
110 p += sprintf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
115 int global_irq_holder = NO_PROC_ID;
116 spinlock_t global_irq_lock = SPIN_LOCK_UNLOCKED;
119 * Most of this code is take from the mips64 tree (ip27-irq.c). It's virtually
120 * identical to the i386 implentation in arh/i386/irq.c, with translations for
121 * the interrupt enable bit
124 #define MAXCOUNT 100000000
125 #define SYNC_OTHER_CORES(x) udelay(x+1)
127 static inline void wait_on_irq(int cpu)
129 int count = MAXCOUNT;
134 * Wait until all interrupts are gone. Wait
135 * for bottom half handlers unless we're
136 * already executing in one..
139 if (local_bh_count(cpu) || !spin_is_locked(&global_bh_lock))
142 /* Duh, we have to loop. Release the lock to avoid deadlocks */
143 spin_unlock(&global_irq_lock);
147 printk("Count spun out. Huh?\n");
151 SYNC_OTHER_CORES(cpu);
155 if (spin_is_locked(&global_irq_lock))
157 if (!local_bh_count(cpu) && spin_is_locked(&global_bh_lock))
159 if (spin_trylock(&global_irq_lock))
166 * This is called when we want to synchronize with
167 * interrupts. We may for example tell a device to
168 * stop sending interrupts: but to make sure there
169 * are no interrupts that are executing on another
170 * CPU we need to call this function.
172 void synchronize_irq(void)
174 if (irqs_running()) {
175 /* Stupid approach */
181 static inline void get_irqlock(int cpu)
183 if (!spin_trylock(&global_irq_lock)) {
184 /* do we already hold the lock? */
185 if ((unsigned char) cpu == global_irq_holder)
187 /* Uhhuh.. Somebody else got it. Wait.. */
188 spin_lock(&global_irq_lock);
191 * We also to make sure that nobody else is running
192 * in an interrupt context.
199 global_irq_holder = cpu;
203 * A global "cli()" while in an interrupt context turns into just a local
204 * cli(). Interrupts should use spinlocks for the (very unlikely) case that
205 * they ever want to protect against each other.
207 * If we already have local interrupts disabled, this will not turn a local
208 * disable into a global one (problems with spinlocks: this makes
209 * save_flags+cli+sti usable inside a spinlock).
212 void __global_cli(void)
217 if (flags & ST0_IE) {
218 int cpu = smp_processor_id();
220 if (!local_irq_count(cpu))
225 void __global_sti(void)
227 int cpu = smp_processor_id();
229 if (!local_irq_count(cpu))
230 release_irqlock(cpu);
235 * SMP flags value to restore to:
241 unsigned long __global_save_flags(void)
246 int cpu = smp_processor_id();
249 local_enabled = (flags & ST0_IE);
250 /* default to local */
251 retval = 2 + local_enabled;
253 /* check for global flags if we're not in an interrupt */
254 if (!local_irq_count(cpu)) {
257 if (global_irq_holder == cpu)
264 void __global_restore_flags(unsigned long flags)
280 printk("global_restore_flags: %08lx\n", flags);
283 #endif /* CONFIG_SMP */
286 * This should really return information about whether
287 * we should do bottom half handling etc. Right now we
288 * end up _always_ checking the bottom half, which is a
289 * waste of time and is not what some drivers would
292 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
295 int cpu = smp_processor_id();
299 status = 1; /* Force the "do bottom halves" bit */
301 if (!(action->flags & SA_INTERRUPT))
305 status |= action->flags;
306 action->handler(irq, action->dev_id, regs);
307 action = action->next;
309 if (status & SA_SAMPLE_RANDOM)
310 add_interrupt_randomness(irq);
319 * Generic enable/disable code: this just calls
320 * down into the PIC-specific version for the actual
321 * hardware disable after having gotten the irq
326 * disable_irq_nosync - disable an irq without waiting
327 * @irq: Interrupt to disable
329 * Disable the selected interrupt line. Disables of an interrupt
330 * stack. Unlike disable_irq(), this function does not ensure existing
331 * instances of the IRQ handler have completed before returning.
333 * This function may be called from IRQ context.
336 void inline disable_irq_nosync(unsigned int irq)
338 irq_desc_t *desc = irq_desc + irq;
341 spin_lock_irqsave(&desc->lock, flags);
342 if (!desc->depth++) {
343 desc->status |= IRQ_DISABLED;
344 desc->handler->disable(irq);
346 spin_unlock_irqrestore(&desc->lock, flags);
350 * disable_irq - disable an irq and wait for completion
351 * @irq: Interrupt to disable
353 * Disable the selected interrupt line. Disables of an interrupt
354 * stack. That is for two disables you need two enables. This
355 * function waits for any pending IRQ handlers for this interrupt
356 * to complete before returning. If you use this function while
357 * holding a resource the IRQ handler may need you will deadlock.
359 * This function may be called - with care - from IRQ context.
362 void disable_irq(unsigned int irq)
364 disable_irq_nosync(irq);
366 if (!local_irq_count(smp_processor_id())) {
369 } while (irq_desc[irq].status & IRQ_INPROGRESS);
374 * enable_irq - enable interrupt handling on an irq
375 * @irq: Interrupt to enable
377 * Re-enables the processing of interrupts on this IRQ line
378 * providing no disable_irq calls are now in effect.
380 * This function may be called from IRQ context.
383 void enable_irq(unsigned int irq)
385 irq_desc_t *desc = irq_desc + irq;
388 spin_lock_irqsave(&desc->lock, flags);
389 switch (desc->depth) {
391 unsigned int status = desc->status & ~IRQ_DISABLED;
392 desc->status = status;
393 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
394 desc->status = status | IRQ_REPLAY;
395 hw_resend_irq(desc->handler,irq);
397 desc->handler->enable(irq);
404 printk("enable_irq(%u) unbalanced from %p\n", irq,
405 __builtin_return_address(0));
407 spin_unlock_irqrestore(&desc->lock, flags);
411 * do_IRQ handles all normal device IRQ's (the special
412 * SMP cross-CPU interrupts have their own specific
415 asmlinkage unsigned int do_IRQ(int irq, struct pt_regs *regs)
418 * We ack quickly, we don't want the irq controller
419 * thinking we're snobs just because some other CPU has
420 * disabled global interrupts (we have already done the
421 * INT_ACK cycles, it's too late to try to pretend to the
422 * controller that we aren't taking the interrupt).
424 * 0 return value means that this irq is already being
425 * handled by some other CPU. (or is disabled)
427 int cpu = smp_processor_id();
428 irq_desc_t *desc = irq_desc + irq;
429 struct irqaction * action;
432 kstat.irqs[cpu][irq]++;
433 spin_lock(&desc->lock);
434 desc->handler->ack(irq);
436 REPLAY is when Linux resends an IRQ that was dropped earlier
437 WAITING is used by probe to mark irqs that are being tested
439 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
440 status |= IRQ_PENDING; /* we _want_ to handle it */
443 * If the IRQ is disabled for whatever reason, we cannot
444 * use the action we have.
447 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
448 action = desc->action;
449 status &= ~IRQ_PENDING; /* we commit to handling */
450 status |= IRQ_INPROGRESS; /* we are handling it */
452 desc->status = status;
455 * If there is no IRQ handler or it was disabled, exit early.
456 Since we set PENDING, if another processor is handling
457 a different instance of this same irq, the other processor
458 will take care of it.
464 * Edge triggered interrupts need to remember
466 * This applies to any hw interrupts that allow a second
467 * instance of the same irq to arrive while we are in do_IRQ
468 * or in the handler. But the code here only handles the _second_
469 * instance of the irq, not the third or fourth. So it is mostly
470 * useful for irq hardware that does not mask cleanly in an
474 spin_unlock(&desc->lock);
475 handle_IRQ_event(irq, regs, action);
476 spin_lock(&desc->lock);
478 if (!(desc->status & IRQ_PENDING))
480 desc->status &= ~IRQ_PENDING;
482 desc->status &= ~IRQ_INPROGRESS;
485 * The ->end() handler has to deal with interrupts which got
486 * disabled while the handler was running.
488 desc->handler->end(irq);
489 spin_unlock(&desc->lock);
491 if (softirq_pending(cpu))
497 * request_irq - allocate an interrupt line
498 * @irq: Interrupt line to allocate
499 * @handler: Function to be called when the IRQ occurs
500 * @irqflags: Interrupt type flags
501 * @devname: An ascii name for the claiming device
502 * @dev_id: A cookie passed back to the handler function
504 * This call allocates interrupt resources and enables the
505 * interrupt line and IRQ handling. From the point this
506 * call is made your handler function may be invoked. Since
507 * your handler function must clear any interrupt the board
508 * raises, you must take care both to initialise your hardware
509 * and to set up the interrupt handler in the right order.
511 * Dev_id must be globally unique. Normally the address of the
512 * device data structure is used as the cookie. Since the handler
513 * receives this value it makes sense to use it.
515 * If your interrupt is shared you must pass a non NULL dev_id
516 * as this is required when freeing the interrupt.
520 * SA_SHIRQ Interrupt is shared
522 * SA_INTERRUPT Disable local interrupts while processing
524 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
528 int request_irq(unsigned int irq,
529 void (*handler)(int, void *, struct pt_regs *),
530 unsigned long irqflags,
531 const char * devname,
535 struct irqaction * action;
539 * Sanity-check: shared interrupts should REALLY pass in
540 * a real dev-ID, otherwise we'll have trouble later trying
541 * to figure out which interrupt is which (messes up the
542 * interrupt freeing logic etc).
544 if (irqflags & SA_SHIRQ) {
546 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
555 action = (struct irqaction *)
556 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
560 action->handler = handler;
561 action->flags = irqflags;
563 action->name = devname;
565 action->dev_id = dev_id;
567 retval = setup_irq(irq, action);
574 * free_irq - free an interrupt
575 * @irq: Interrupt line to free
576 * @dev_id: Device identity to free
578 * Remove an interrupt handler. The handler is removed and if the
579 * interrupt line is no longer in use by any driver it is disabled.
580 * On a shared IRQ the caller must ensure the interrupt is disabled
581 * on the card it drives before calling this function. The function
582 * does not return until any executing interrupts for this IRQ
585 * This function may be called from interrupt context.
587 * Bugs: Attempting to free an irq in a handler for the same irq hangs
591 void free_irq(unsigned int irq, void *dev_id)
594 struct irqaction **p;
600 desc = irq_desc + irq;
601 spin_lock_irqsave(&desc->lock,flags);
604 struct irqaction * action = *p;
606 struct irqaction **pp = p;
608 if (action->dev_id != dev_id)
611 /* Found it - now remove it from the list of entries */
614 desc->status |= IRQ_DISABLED;
615 desc->handler->shutdown(irq);
617 spin_unlock_irqrestore(&desc->lock,flags);
620 /* Wait to make sure it's not being used on another CPU */
621 while (desc->status & IRQ_INPROGRESS)
627 printk("Trying to free free IRQ%d\n",irq);
628 spin_unlock_irqrestore(&desc->lock,flags);
634 * IRQ autodetection code..
636 * This depends on the fact that any interrupt that
637 * comes in on to an unassigned handler will get stuck
638 * with "IRQ_WAITING" cleared and the interrupt
642 static DECLARE_MUTEX(probe_sem);
645 * probe_irq_on - begin an interrupt autodetect
647 * Commence probing for an interrupt. The interrupts are scanned
648 * and a mask of potential interrupt lines is returned.
652 unsigned long probe_irq_on(void)
661 * something may have generated an irq long ago and we want to
662 * flush such a longstanding irq before considering it as spurious.
664 for (i = NR_IRQS-1; i > 0; i--) {
667 spin_lock_irq(&desc->lock);
668 if (!irq_desc[i].action)
669 irq_desc[i].handler->startup(i);
670 spin_unlock_irq(&desc->lock);
673 /* Wait for longstanding interrupts to trigger. */
674 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
675 /* about 20ms delay */ synchronize_irq();
678 * enable any unassigned irqs
679 * (we must startup again here because if a longstanding irq
680 * happened in the previous stage, it may have masked itself)
682 for (i = NR_IRQS-1; i > 0; i--) {
685 spin_lock_irq(&desc->lock);
687 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
688 if (desc->handler->startup(i))
689 desc->status |= IRQ_PENDING;
691 spin_unlock_irq(&desc->lock);
695 * Wait for spurious interrupts to trigger
697 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
698 /* about 100ms delay */ synchronize_irq();
701 * Now filter out any obviously spurious interrupts
704 for (i = 0; i < NR_IRQS; i++) {
705 irq_desc_t *desc = irq_desc + i;
708 spin_lock_irq(&desc->lock);
709 status = desc->status;
711 if (status & IRQ_AUTODETECT) {
712 /* It triggered already - consider it spurious. */
713 if (!(status & IRQ_WAITING)) {
714 desc->status = status & ~IRQ_AUTODETECT;
715 desc->handler->shutdown(i);
720 spin_unlock_irq(&desc->lock);
727 * Return a mask of triggered interrupts (this
728 * can handle only legacy ISA interrupts).
732 * probe_irq_mask - scan a bitmap of interrupt lines
733 * @val: mask of interrupts to consider
735 * Scan the ISA bus interrupt lines and return a bitmap of
736 * active interrupts. The interrupt probe logic state is then
737 * returned to its previous value.
739 * Note: we need to scan all the irq's even though we will
740 * only return ISA irq numbers - just so that we reset them
741 * all to a known state.
743 unsigned int probe_irq_mask(unsigned long val)
749 for (i = 0; i < NR_IRQS; i++) {
750 irq_desc_t *desc = irq_desc + i;
753 spin_lock_irq(&desc->lock);
754 status = desc->status;
756 if (status & IRQ_AUTODETECT) {
757 if (i < 16 && !(status & IRQ_WAITING))
760 desc->status = status & ~IRQ_AUTODETECT;
761 desc->handler->shutdown(i);
763 spin_unlock_irq(&desc->lock);
771 * Return the one interrupt that triggered (this can
772 * handle any interrupt source).
776 * probe_irq_off - end an interrupt autodetect
777 * @val: mask of potential interrupts (unused)
779 * Scans the unused interrupt lines and returns the line which
780 * appears to have triggered the interrupt. If no interrupt was
781 * found then zero is returned. If more than one interrupt is
782 * found then minus the first candidate is returned to indicate
785 * The interrupt probe logic state is returned to its previous
788 * BUGS: When used in a module (which arguably shouldnt happen)
789 * nothing prevents two IRQ probe callers from overlapping. The
790 * results of this are non-optimal.
793 int probe_irq_off(unsigned long val)
795 int i, irq_found, nr_irqs;
799 for (i = 0; i < NR_IRQS; i++) {
800 irq_desc_t *desc = irq_desc + i;
803 spin_lock_irq(&desc->lock);
804 status = desc->status;
806 if (status & IRQ_AUTODETECT) {
807 if (!(status & IRQ_WAITING)) {
812 desc->status = status & ~IRQ_AUTODETECT;
813 desc->handler->shutdown(i);
815 spin_unlock_irq(&desc->lock);
820 irq_found = -irq_found;
824 /* this was setup_x86_irq but it seems pretty generic */
825 int setup_irq(unsigned int irq, struct irqaction * new)
829 struct irqaction *old, **p;
830 irq_desc_t *desc = irq_desc + irq;
833 * Some drivers like serial.c use request_irq() heavily,
834 * so we have to be careful not to interfere with a
837 if (new->flags & SA_SAMPLE_RANDOM) {
839 * This function might sleep, we want to call it first,
840 * outside of the atomic block.
841 * Yes, this might clear the entropy pool if the wrong
842 * driver is attempted to be loaded, without actually
843 * installing a new handler, but is this really a problem,
844 * only the sysadmin is able to do this.
846 rand_initialize_irq(irq);
850 * The following block of code has to be executed atomically
852 spin_lock_irqsave(&desc->lock,flags);
854 if ((old = *p) != NULL) {
855 /* Can't share interrupts unless both agree to */
856 if (!(old->flags & new->flags & SA_SHIRQ)) {
857 spin_unlock_irqrestore(&desc->lock,flags);
861 /* add new interrupt at end of irq queue */
873 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
874 desc->handler->startup(irq);
876 spin_unlock_irqrestore(&desc->lock,flags);
878 register_irq_proc(irq);
882 void __init init_generic_irq(void)
886 for (i = 0; i < NR_IRQS; i++) {
887 irq_desc[i].status = IRQ_DISABLED;
888 irq_desc[i].action = NULL;
889 irq_desc[i].depth = 1;
890 irq_desc[i].handler = &no_irq_type;
894 EXPORT_SYMBOL(disable_irq_nosync);
895 EXPORT_SYMBOL(disable_irq);
896 EXPORT_SYMBOL(enable_irq);
897 EXPORT_SYMBOL(probe_irq_mask);
899 static struct proc_dir_entry * root_irq_dir;
900 static struct proc_dir_entry * irq_dir [NR_IRQS];
904 static unsigned int parse_hex_value (const char *buffer,
905 unsigned long count, unsigned long *ret)
907 unsigned char hexnum [HEX_DIGITS];
913 if (count > HEX_DIGITS)
915 if (copy_from_user(hexnum, buffer, count))
919 * Parse the first HEX_DIGITS characters as a hex string, any non-hex
920 * char is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
924 for (i = 0; i < count; i++) {
925 unsigned int c = hexnum[i];
928 case '0' ... '9': c -= '0'; break;
929 case 'a' ... 'f': c -= 'a'-10; break;
930 case 'A' ... 'F': c -= 'A'-10; break;
934 value = (value << 4) | c;
943 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
945 static unsigned long irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = ~0UL };
946 static int irq_affinity_read_proc (char *page, char **start, off_t off,
947 int count, int *eof, void *data)
949 if (count < HEX_DIGITS+1)
951 return sprintf (page, "%08lx\n", irq_affinity[(long)data]);
954 static int irq_affinity_write_proc (struct file *file, const char *buffer,
955 unsigned long count, void *data)
957 int irq = (long) data, full_count = count, err;
958 unsigned long new_value;
960 if (!irq_desc[irq].handler->set_affinity)
963 err = parse_hex_value(buffer, count, &new_value);
966 * Do not allow disabling IRQs completely - it's a too easy
967 * way to make the system unusable accidentally :-) At least
968 * one online CPU still has to be targeted.
970 if (!(new_value & cpu_online_map))
973 irq_affinity[irq] = new_value;
974 irq_desc[irq].handler->set_affinity(irq, new_value);
981 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
982 int count, int *eof, void *data)
984 unsigned long *mask = (unsigned long *) data;
985 if (count < HEX_DIGITS+1)
987 return sprintf (page, "%08lx\n", *mask);
990 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
991 unsigned long count, void *data)
993 unsigned long *mask = (unsigned long *) data, full_count = count, err;
994 unsigned long new_value;
996 err = parse_hex_value(buffer, count, &new_value);
1004 #define MAX_NAMELEN 10
1006 static void register_irq_proc (unsigned int irq)
1008 char name [MAX_NAMELEN];
1010 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
1014 memset(name, 0, MAX_NAMELEN);
1015 sprintf(name, "%d", irq);
1017 /* create /proc/irq/1234 */
1018 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
1022 struct proc_dir_entry *entry;
1024 /* create /proc/irq/1234/smp_affinity */
1025 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
1029 entry->data = (void *)(long)irq;
1030 entry->read_proc = irq_affinity_read_proc;
1031 entry->write_proc = irq_affinity_write_proc;
1034 smp_affinity_entry[irq] = entry;
1039 unsigned long prof_cpu_mask = -1;
1041 void init_irq_proc (void)
1043 struct proc_dir_entry *entry;
1046 /* create /proc/irq */
1047 root_irq_dir = proc_mkdir("irq", 0);
1049 /* create /proc/irq/prof_cpu_mask */
1050 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
1056 entry->data = (void *)&prof_cpu_mask;
1057 entry->read_proc = prof_cpu_mask_read_proc;
1058 entry->write_proc = prof_cpu_mask_write_proc;
1061 * Create entries for all existing IRQs.
1063 for (i = 0; i < NR_IRQS; i++)
1064 register_irq_proc(i);