2 * linux/arch/m32r/kernel/irq.c
4 * Copyright (c) 2003, 2004 Hitoshi Yamamoto
6 * Taken from i386 2.6.4 version.
10 * linux/arch/i386/kernel/irq.c
12 * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
14 * This file contains the code used by various IRQ handling routines:
15 * asking for different IRQ's should be done through these routines
16 * instead of just grabbing them. Thus setups with different IRQ numbers
17 * shouldn't result in any weird surprises, and installing new handlers
22 * (mostly architecture independent, will move to kernel/irq.c in 2.5.)
24 * IRQs are in fact implemented a bit like signal handlers for the kernel.
25 * Naturally it's not a 1:1 relation, but there are similarities.
28 #include <linux/config.h>
29 #include <linux/errno.h>
30 #include <linux/module.h>
31 #include <linux/signal.h>
32 #include <linux/sched.h>
33 #include <linux/ioport.h>
34 #include <linux/interrupt.h>
35 #include <linux/timex.h>
36 #include <linux/slab.h>
37 #include <linux/random.h>
38 #include <linux/smp_lock.h>
39 #include <linux/init.h>
40 #include <linux/kernel_stat.h>
41 #include <linux/irq.h>
42 #include <linux/proc_fs.h>
43 #include <linux/seq_file.h>
44 #include <linux/kallsyms.h>
45 #include <linux/bitops.h>
47 #include <asm/atomic.h>
50 #include <asm/system.h>
51 #include <asm/uaccess.h>
52 #include <asm/delay.h>
56 * Linux has a controller-independent x86 interrupt architecture.
57 * every controller has a 'controller-template', that is used
58 * by the main code to do the right thing. Each driver-visible
59 * interrupt source is transparently wired to the apropriate
60 * controller. Thus drivers need not be aware of the
61 * interrupt-controller.
63 * Various interrupt controllers we handle: 8259 PIC, SMP IO-APIC,
64 * PIIX4's internal 8259 PIC and SGI's Visual Workstation Cobalt (IO-)APIC.
65 * (IO-APICs assumed to be messaging to Pentium local-APICs)
67 * the code is designed to be easily extended with new/different
68 * interrupt controllers, without having to do assembly magic.
72 * Controller mappings for all interrupt sources:
74 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
76 .handler = &no_irq_type,
77 .lock = SPIN_LOCK_UNLOCKED
81 static void register_irq_proc (unsigned int irq);
84 * Special irq handlers.
87 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
91 * Generic no controller code
94 static void enable_none(unsigned int irq) { }
95 static unsigned int startup_none(unsigned int irq) { return 0; }
96 static void disable_none(unsigned int irq) { }
97 static void ack_none(unsigned int irq)
100 * 'what should we do if we get a hw irq event on an illegal vector'.
101 * each architecture has to answer this themselves, it doesn't deserve
102 * a generic callback i think.
104 printk("unexpected IRQ trap at vector %02x\n", irq);
107 /* startup is the same as "enable", shutdown is same as "disable" */
108 #define shutdown_none disable_none
109 #define end_none enable_none
111 struct hw_interrupt_type no_irq_type = {
121 atomic_t irq_err_count;
122 atomic_t irq_mis_count;
125 * Generic, controller-independent functions:
128 int show_interrupts(struct seq_file *p, void *v)
130 int i = *(loff_t *) v, j;
131 struct irqaction * action;
136 for (j=0; j<NR_CPUS; j++)
138 seq_printf(p, "CPU%d ",j);
143 spin_lock_irqsave(&irq_desc[i].lock, flags);
144 action = irq_desc[i].action;
147 seq_printf(p, "%3d: ",i);
149 seq_printf(p, "%10u ", kstat_irqs(i));
151 for (j = 0; j < NR_CPUS; j++)
153 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
155 seq_printf(p, " %14s", irq_desc[i].handler->typename);
156 seq_printf(p, " %s", action->name);
158 for (action=action->next; action; action = action->next)
159 seq_printf(p, ", %s", action->name);
163 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
164 } else if (i == NR_IRQS) {
165 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
166 seq_printf(p, "MIS: %10u\n", atomic_read(&irq_mis_count));
172 inline void synchronize_irq(unsigned int irq)
174 while (irq_desc[irq].status & IRQ_INPROGRESS)
180 * This should really return information about whether
181 * we should do bottom half handling etc. Right now we
182 * end up _always_ checking the bottom half, which is a
183 * waste of time and is not what some drivers would
186 int handle_IRQ_event(unsigned int irq,
187 struct pt_regs *regs, struct irqaction *action)
189 int status = 1; /* Force the "do bottom halves" bit */
192 if (!(action->flags & SA_INTERRUPT))
196 ret = action->handler(irq, action->dev_id, regs);
197 if (ret == IRQ_HANDLED)
198 status |= action->flags;
199 action = action->next;
202 if (status & SA_SAMPLE_RANDOM)
203 add_interrupt_randomness(irq);
208 static void __report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
210 struct irqaction *action;
212 if (action_ret != IRQ_HANDLED && action_ret != IRQ_NONE) {
213 printk(KERN_ERR "irq event %d: bogus return value %x\n",
216 printk(KERN_ERR "irq %d: nobody cared!\n", irq);
219 printk(KERN_ERR "handlers:\n");
220 action = desc->action;
222 printk(KERN_ERR "[<%p>]", action->handler);
223 print_symbol(" (%s)",
224 (unsigned long)action->handler);
226 action = action->next;
230 static void report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
232 static int count = 100;
236 __report_bad_irq(irq, desc, action_ret);
240 static int noirqdebug;
242 static int __init noirqdebug_setup(char *str)
245 printk("IRQ lockup detection disabled\n");
249 __setup("noirqdebug", noirqdebug_setup);
252 * If 99,900 of the previous 100,000 interrupts have not been handled then
253 * assume that the IRQ is stuck in some manner. Drop a diagnostic and try to
256 * (The other 100-of-100,000 interrupts may have been a correctly-functioning
257 * device sharing an IRQ with the failing one)
259 * Called under desc->lock
261 static void note_interrupt(int irq, irq_desc_t *desc, irqreturn_t action_ret)
263 if (action_ret != IRQ_HANDLED) {
264 desc->irqs_unhandled++;
265 if (action_ret != IRQ_NONE)
266 report_bad_irq(irq, desc, action_ret);
270 if (desc->irq_count < 100000)
274 if (desc->irqs_unhandled > 99900) {
276 * The interrupt is stuck
278 __report_bad_irq(irq, desc, action_ret);
282 printk(KERN_EMERG "Disabling IRQ #%d\n", irq);
283 desc->status |= IRQ_DISABLED;
284 desc->handler->disable(irq);
286 desc->irqs_unhandled = 0;
290 * Generic enable/disable code: this just calls
291 * down into the PIC-specific version for the actual
292 * hardware disable after having gotten the irq
297 * disable_irq_nosync - disable an irq without waiting
298 * @irq: Interrupt to disable
300 * Disable the selected interrupt line. Disables and Enables are
302 * Unlike disable_irq(), this function does not ensure existing
303 * instances of the IRQ handler have completed before returning.
305 * This function may be called from IRQ context.
308 inline void disable_irq_nosync(unsigned int irq)
310 irq_desc_t *desc = irq_desc + irq;
313 spin_lock_irqsave(&desc->lock, flags);
314 if (!desc->depth++) {
315 desc->status |= IRQ_DISABLED;
316 desc->handler->disable(irq);
318 spin_unlock_irqrestore(&desc->lock, flags);
322 * disable_irq - disable an irq and wait for completion
323 * @irq: Interrupt to disable
325 * Disable the selected interrupt line. Enables and Disables are
327 * This function waits for any pending IRQ handlers for this interrupt
328 * to complete before returning. If you use this function while
329 * holding a resource the IRQ handler may need you will deadlock.
331 * This function may be called - with care - from IRQ context.
334 void disable_irq(unsigned int irq)
336 irq_desc_t *desc = irq_desc + irq;
337 disable_irq_nosync(irq);
339 synchronize_irq(irq);
343 * enable_irq - enable handling of an irq
344 * @irq: Interrupt to enable
346 * Undoes the effect of one call to disable_irq(). If this
347 * matches the last disable, processing of interrupts on this
348 * IRQ line is re-enabled.
350 * This function may be called from IRQ context.
353 void enable_irq(unsigned int irq)
355 irq_desc_t *desc = irq_desc + irq;
358 spin_lock_irqsave(&desc->lock, flags);
359 switch (desc->depth) {
361 unsigned int status = desc->status & ~IRQ_DISABLED;
362 desc->status = status;
363 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
364 desc->status = status | IRQ_REPLAY;
365 hw_resend_irq(desc->handler,irq);
367 desc->handler->enable(irq);
374 printk("enable_irq(%u) unbalanced from %p\n", irq,
375 __builtin_return_address(0));
377 spin_unlock_irqrestore(&desc->lock, flags);
381 * do_IRQ handles all normal device IRQ's (the special
382 * SMP cross-CPU interrupts have their own specific
385 asmlinkage unsigned int do_IRQ(int irq, struct pt_regs *regs)
388 * We ack quickly, we don't want the irq controller
389 * thinking we're snobs just because some other CPU has
390 * disabled global interrupts (we have already done the
391 * INT_ACK cycles, it's too late to try to pretend to the
392 * controller that we aren't taking the interrupt).
394 * 0 return value means that this irq is already being
395 * handled by some other CPU. (or is disabled)
397 irq_desc_t *desc = irq_desc + irq;
398 struct irqaction * action;
403 #ifdef CONFIG_DEBUG_STACKOVERFLOW
406 kstat_this_cpu.irqs[irq]++;
407 spin_lock(&desc->lock);
408 desc->handler->ack(irq);
410 REPLAY is when Linux resends an IRQ that was dropped earlier
411 WAITING is used by probe to mark irqs that are being tested
413 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
414 status |= IRQ_PENDING; /* we _want_ to handle it */
417 * If the IRQ is disabled for whatever reason, we cannot
418 * use the action we have.
421 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
422 action = desc->action;
423 status &= ~IRQ_PENDING; /* we commit to handling */
424 status |= IRQ_INPROGRESS; /* we are handling it */
426 desc->status = status;
429 * If there is no IRQ handler or it was disabled, exit early.
430 Since we set PENDING, if another processor is handling
431 a different instance of this same irq, the other processor
432 will take care of it.
434 if (unlikely(!action))
438 * Edge triggered interrupts need to remember
440 * This applies to any hw interrupts that allow a second
441 * instance of the same irq to arrive while we are in do_IRQ
442 * or in the handler. But the code here only handles the _second_
443 * instance of the irq, not the third or fourth. So it is mostly
444 * useful for irq hardware that does not mask cleanly in an
448 irqreturn_t action_ret;
450 spin_unlock(&desc->lock);
451 action_ret = handle_IRQ_event(irq, regs, action);
452 spin_lock(&desc->lock);
454 note_interrupt(irq, desc, action_ret);
455 if (likely(!(desc->status & IRQ_PENDING)))
457 desc->status &= ~IRQ_PENDING;
459 desc->status &= ~IRQ_INPROGRESS;
463 * The ->end() handler has to deal with interrupts which got
464 * disabled while the handler was running.
466 desc->handler->end(irq);
467 spin_unlock(&desc->lock);
471 #if defined(CONFIG_SMP)
472 if (irq == M32R_IRQ_MFT2)
474 #endif /* CONFIG_SMP */
479 int can_request_irq(unsigned int irq, unsigned long irqflags)
481 struct irqaction *action;
485 action = irq_desc[irq].action;
487 if (irqflags & action->flags & SA_SHIRQ)
494 * request_irq - allocate an interrupt line
495 * @irq: Interrupt line to allocate
496 * @handler: Function to be called when the IRQ occurs
497 * @irqflags: Interrupt type flags
498 * @devname: An ascii name for the claiming device
499 * @dev_id: A cookie passed back to the handler function
501 * This call allocates interrupt resources and enables the
502 * interrupt line and IRQ handling. From the point this
503 * call is made your handler function may be invoked. Since
504 * your handler function must clear any interrupt the board
505 * raises, you must take care both to initialise your hardware
506 * and to set up the interrupt handler in the right order.
508 * Dev_id must be globally unique. Normally the address of the
509 * device data structure is used as the cookie. Since the handler
510 * receives this value it makes sense to use it.
512 * If your interrupt is shared you must pass a non NULL dev_id
513 * as this is required when freeing the interrupt.
517 * SA_SHIRQ Interrupt is shared
519 * SA_INTERRUPT Disable local interrupts while processing
521 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
525 int request_irq(unsigned int irq,
526 irqreturn_t (*handler)(int, void *, struct pt_regs *),
527 unsigned long irqflags,
528 const char * devname,
532 struct irqaction * action;
536 * Sanity-check: shared interrupts should REALLY pass in
537 * a real dev-ID, otherwise we'll have trouble later trying
538 * to figure out which interrupt is which (messes up the
539 * interrupt freeing logic etc).
541 if (irqflags & SA_SHIRQ) {
543 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
552 action = (struct irqaction *)
553 kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
557 action->handler = handler;
558 action->flags = irqflags;
559 cpus_clear(action->mask);
560 action->name = devname;
562 action->dev_id = dev_id;
564 retval = setup_irq(irq, action);
570 EXPORT_SYMBOL(request_irq);
573 * free_irq - free an interrupt
574 * @irq: Interrupt line to free
575 * @dev_id: Device identity to free
577 * Remove an interrupt handler. The handler is removed and if the
578 * interrupt line is no longer in use by any driver it is disabled.
579 * On a shared IRQ the caller must ensure the interrupt is disabled
580 * on the card it drives before calling this function. The function
581 * does not return until any executing interrupts for this IRQ
584 * This function must not be called from interrupt context.
587 void free_irq(unsigned int irq, void *dev_id)
590 struct irqaction **p;
596 desc = irq_desc + irq;
597 spin_lock_irqsave(&desc->lock,flags);
600 struct irqaction * action = *p;
602 struct irqaction **pp = p;
604 if (action->dev_id != dev_id)
607 /* Found it - now remove it from the list of entries */
610 desc->status |= IRQ_DISABLED;
611 desc->handler->shutdown(irq);
613 spin_unlock_irqrestore(&desc->lock,flags);
615 /* Wait to make sure it's not being used on another CPU */
616 synchronize_irq(irq);
620 printk("Trying to free free IRQ%d\n",irq);
621 spin_unlock_irqrestore(&desc->lock,flags);
626 EXPORT_SYMBOL(free_irq);
629 * IRQ autodetection code..
631 * This depends on the fact that any interrupt that
632 * comes in on to an unassigned handler will get stuck
633 * with "IRQ_WAITING" cleared and the interrupt
637 static DECLARE_MUTEX(probe_sem);
640 * probe_irq_on - begin an interrupt autodetect
642 * Commence probing for an interrupt. The interrupts are scanned
643 * and a mask of potential interrupt lines is returned.
647 unsigned long probe_irq_on(void)
656 * something may have generated an irq long ago and we want to
657 * flush such a longstanding irq before considering it as spurious.
659 for (i = NR_IRQS-1; i > 0; i--) {
662 spin_lock_irq(&desc->lock);
663 if (!irq_desc[i].action)
664 irq_desc[i].handler->startup(i);
665 spin_unlock_irq(&desc->lock);
668 /* Wait for longstanding interrupts to trigger. */
669 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
670 /* about 20ms delay */ barrier();
673 * enable any unassigned irqs
674 * (we must startup again here because if a longstanding irq
675 * happened in the previous stage, it may have masked itself)
677 for (i = NR_IRQS-1; i > 0; i--) {
680 spin_lock_irq(&desc->lock);
682 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
683 if (desc->handler->startup(i))
684 desc->status |= IRQ_PENDING;
686 spin_unlock_irq(&desc->lock);
690 * Wait for spurious interrupts to trigger
692 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
693 /* about 100ms delay */ barrier();
696 * Now filter out any obviously spurious interrupts
699 for (i = 0; i < NR_IRQS; i++) {
700 irq_desc_t *desc = irq_desc + i;
703 spin_lock_irq(&desc->lock);
704 status = desc->status;
706 if (status & IRQ_AUTODETECT) {
707 /* It triggered already - consider it spurious. */
708 if (!(status & IRQ_WAITING)) {
709 desc->status = status & ~IRQ_AUTODETECT;
710 desc->handler->shutdown(i);
715 spin_unlock_irq(&desc->lock);
721 EXPORT_SYMBOL(probe_irq_on);
724 * Return a mask of triggered interrupts (this
725 * can handle only legacy ISA interrupts).
729 * probe_irq_mask - scan a bitmap of interrupt lines
730 * @val: mask of interrupts to consider
732 * Scan the ISA bus interrupt lines and return a bitmap of
733 * active interrupts. The interrupt probe logic state is then
734 * returned to its previous value.
736 * Note: we need to scan all the irq's even though we will
737 * only return ISA irq numbers - just so that we reset them
738 * all to a known state.
740 unsigned int probe_irq_mask(unsigned long val)
746 for (i = 0; i < NR_IRQS; i++) {
747 irq_desc_t *desc = irq_desc + i;
750 spin_lock_irq(&desc->lock);
751 status = desc->status;
753 if (status & IRQ_AUTODETECT) {
754 if (i < 16 && !(status & IRQ_WAITING))
757 desc->status = status & ~IRQ_AUTODETECT;
758 desc->handler->shutdown(i);
760 spin_unlock_irq(&desc->lock);
768 * Return the one interrupt that triggered (this can
769 * handle any interrupt source).
773 * probe_irq_off - end an interrupt autodetect
774 * @val: mask of potential interrupts (unused)
776 * Scans the unused interrupt lines and returns the line which
777 * appears to have triggered the interrupt. If no interrupt was
778 * found then zero is returned. If more than one interrupt is
779 * found then minus the first candidate is returned to indicate
782 * The interrupt probe logic state is returned to its previous
785 * BUGS: When used in a module (which arguably shouldnt happen)
786 * nothing prevents two IRQ probe callers from overlapping. The
787 * results of this are non-optimal.
790 int probe_irq_off(unsigned long val)
792 int i, irq_found, nr_irqs;
796 for (i = 0; i < NR_IRQS; i++) {
797 irq_desc_t *desc = irq_desc + i;
800 spin_lock_irq(&desc->lock);
801 status = desc->status;
803 if (status & IRQ_AUTODETECT) {
804 if (!(status & IRQ_WAITING)) {
809 desc->status = status & ~IRQ_AUTODETECT;
810 desc->handler->shutdown(i);
812 spin_unlock_irq(&desc->lock);
817 irq_found = -irq_found;
821 EXPORT_SYMBOL(probe_irq_off);
823 /* this was setup_x86_irq but it seems pretty generic */
824 int setup_irq(unsigned int irq, struct irqaction * new)
828 struct irqaction *old, **p;
829 irq_desc_t *desc = irq_desc + irq;
831 if (desc->handler == &no_irq_type)
834 * Some drivers like serial.c use request_irq() heavily,
835 * so we have to be careful not to interfere with a
838 if (new->flags & SA_SAMPLE_RANDOM) {
840 * This function might sleep, we want to call it first,
841 * outside of the atomic block.
842 * Yes, this might clear the entropy pool if the wrong
843 * driver is attempted to be loaded, without actually
844 * installing a new handler, but is this really a problem,
845 * only the sysadmin is able to do this.
847 rand_initialize_irq(irq);
851 * The following block of code has to be executed atomically
853 spin_lock_irqsave(&desc->lock,flags);
855 if ((old = *p) != NULL) {
856 /* Can't share interrupts unless both agree to */
857 if (!(old->flags & new->flags & SA_SHIRQ)) {
858 spin_unlock_irqrestore(&desc->lock,flags);
862 /* add new interrupt at end of irq queue */
874 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
875 desc->handler->startup(irq);
877 spin_unlock_irqrestore(&desc->lock,flags);
879 register_irq_proc(irq);
883 static struct proc_dir_entry * root_irq_dir;
884 static struct proc_dir_entry * irq_dir [NR_IRQS];
888 static struct proc_dir_entry *smp_affinity_entry[NR_IRQS];
890 cpumask_t irq_affinity[NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
892 static int irq_affinity_read_proc(char *page, char **start, off_t off,
893 int count, int *eof, void *data)
895 int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
898 len += sprintf(page + len, "\n");
902 static int irq_affinity_write_proc(struct file *file, const char __user *buffer,
903 unsigned long count, void *data)
905 int irq = (long)data, full_count = count, err;
906 cpumask_t new_value, tmp;
908 if (!irq_desc[irq].handler->set_affinity)
911 err = cpumask_parse(buffer, count, new_value);
916 * Do not allow disabling IRQs completely - it's a too easy
917 * way to make the system unusable accidentally :-) At least
918 * one online CPU still has to be targeted.
920 cpus_and(tmp, new_value, cpu_online_map);
924 irq_affinity[irq] = new_value;
925 irq_desc[irq].handler->set_affinity(irq,
926 cpumask_of_cpu(first_cpu(new_value)));
933 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
934 int count, int *eof, void *data)
936 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
939 len += sprintf(page + len, "\n");
943 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
944 unsigned long count, void *data)
946 cpumask_t *mask = (cpumask_t *)data;
947 unsigned long full_count = count, err;
950 err = cpumask_parse(buffer, count, new_value);
958 #define MAX_NAMELEN 10
960 static void register_irq_proc (unsigned int irq)
962 char name [MAX_NAMELEN];
964 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
968 memset(name, 0, MAX_NAMELEN);
969 sprintf(name, "%d", irq);
971 /* create /proc/irq/1234 */
972 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
976 struct proc_dir_entry *entry;
978 /* create /proc/irq/1234/smp_affinity */
979 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
983 entry->data = (void *)(long)irq;
984 entry->read_proc = irq_affinity_read_proc;
985 entry->write_proc = irq_affinity_write_proc;
988 smp_affinity_entry[irq] = entry;
993 unsigned long prof_cpu_mask = -1;
995 void init_irq_proc (void)
997 struct proc_dir_entry *entry;
1000 /* create /proc/irq */
1001 root_irq_dir = proc_mkdir("irq", NULL);
1003 /* create /proc/irq/prof_cpu_mask */
1004 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
1010 entry->data = (void *)&prof_cpu_mask;
1011 entry->read_proc = prof_cpu_mask_read_proc;
1012 entry->write_proc = prof_cpu_mask_write_proc;
1015 * Create entries for all existing IRQs.
1017 for (i = 0; i < NR_IRQS; i++)
1018 register_irq_proc(i);