2 * linux/arch/alpha/kernel/irq.c
4 * Copyright (C) 1995 Linus Torvalds
6 * This file contains the code used by various IRQ handling routines:
7 * asking for different IRQ's should be done through these routines
8 * instead of just grabbing them. Thus setups with different IRQ numbers
9 * shouldn't result in any weird surprises, and installing new handlers
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/signal.h>
19 #include <linux/sched.h>
20 #include <linux/ptrace.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
23 #include <linux/random.h>
24 #include <linux/init.h>
25 #include <linux/irq.h>
26 #include <linux/proc_fs.h>
27 #include <linux/seq_file.h>
28 #include <linux/profile.h>
29 #include <linux/bitops.h>
31 #include <asm/system.h>
33 #include <asm/uaccess.h>
36 * Controller mappings for all interrupt sources:
38 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
40 .handler = &no_irq_type,
41 .lock = SPIN_LOCK_UNLOCKED
45 static void register_irq_proc(unsigned int irq);
47 volatile unsigned long irq_err_count;
50 * Special irq handlers.
53 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
59 * Generic no controller code
62 static void no_irq_enable_disable(unsigned int irq) { }
63 static unsigned int no_irq_startup(unsigned int irq) { return 0; }
66 no_irq_ack(unsigned int irq)
69 printk(KERN_CRIT "Unexpected IRQ trap at vector %u\n", irq);
72 struct hw_interrupt_type no_irq_type = {
74 .startup = no_irq_startup,
75 .shutdown = no_irq_enable_disable,
76 .enable = no_irq_enable_disable,
77 .disable = no_irq_enable_disable,
79 .end = no_irq_enable_disable,
83 handle_IRQ_event(unsigned int irq, struct pt_regs *regs,
84 struct irqaction *action)
86 int status = 1; /* Force the "do bottom halves" bit */
90 if (!(action->flags & SA_INTERRUPT))
95 ret = action->handler(irq, action->dev_id, regs);
96 if (ret == IRQ_HANDLED)
97 status |= action->flags;
98 action = action->next;
100 if (status & SA_SAMPLE_RANDOM)
101 add_interrupt_randomness(irq);
108 * Generic enable/disable code: this just calls
109 * down into the PIC-specific version for the actual
110 * hardware disable after having gotten the irq
114 disable_irq_nosync(unsigned int irq)
116 irq_desc_t *desc = irq_desc + irq;
119 spin_lock_irqsave(&desc->lock, flags);
120 if (!desc->depth++) {
121 desc->status |= IRQ_DISABLED;
122 desc->handler->disable(irq);
124 spin_unlock_irqrestore(&desc->lock, flags);
128 * Synchronous version of the above, making sure the IRQ is
129 * no longer running on any other IRQ..
132 disable_irq(unsigned int irq)
134 disable_irq_nosync(irq);
135 synchronize_irq(irq);
139 enable_irq(unsigned int irq)
141 irq_desc_t *desc = irq_desc + irq;
144 spin_lock_irqsave(&desc->lock, flags);
145 switch (desc->depth) {
147 unsigned int status = desc->status & ~IRQ_DISABLED;
148 desc->status = status;
149 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
150 desc->status = status | IRQ_REPLAY;
151 hw_resend_irq(desc->handler,irq);
153 desc->handler->enable(irq);
160 printk(KERN_ERR "enable_irq() unbalanced from %p\n",
161 __builtin_return_address(0));
163 spin_unlock_irqrestore(&desc->lock, flags);
167 setup_irq(unsigned int irq, struct irqaction * new)
170 struct irqaction *old, **p;
172 irq_desc_t *desc = irq_desc + irq;
174 if (desc->handler == &no_irq_type)
178 * Some drivers like serial.c use request_irq() heavily,
179 * so we have to be careful not to interfere with a
182 if (new->flags & SA_SAMPLE_RANDOM) {
184 * This function might sleep, we want to call it first,
185 * outside of the atomic block.
186 * Yes, this might clear the entropy pool if the wrong
187 * driver is attempted to be loaded, without actually
188 * installing a new handler, but is this really a problem,
189 * only the sysadmin is able to do this.
191 rand_initialize_irq(irq);
195 * The following block of code has to be executed atomically
197 spin_lock_irqsave(&desc->lock,flags);
199 if ((old = *p) != NULL) {
200 /* Can't share interrupts unless both agree to */
201 if (!(old->flags & new->flags & SA_SHIRQ)) {
202 spin_unlock_irqrestore(&desc->lock,flags);
206 /* add new interrupt at end of irq queue */
219 ~(IRQ_DISABLED|IRQ_AUTODETECT|IRQ_WAITING|IRQ_INPROGRESS);
220 desc->handler->startup(irq);
222 spin_unlock_irqrestore(&desc->lock,flags);
227 static struct proc_dir_entry * root_irq_dir;
228 static struct proc_dir_entry * irq_dir[NR_IRQS];
231 static struct proc_dir_entry * smp_affinity_entry[NR_IRQS];
232 static char irq_user_affinity[NR_IRQS];
233 static cpumask_t irq_affinity[NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
236 select_smp_affinity(int irq)
239 int cpu = last_cpu + 1;
241 if (! irq_desc[irq].handler->set_affinity || irq_user_affinity[irq])
244 while (!cpu_possible(cpu))
245 cpu = (cpu < (NR_CPUS-1) ? cpu + 1 : 0);
248 irq_affinity[irq] = cpumask_of_cpu(cpu);
249 irq_desc[irq].handler->set_affinity(irq, cpumask_of_cpu(cpu));
253 irq_affinity_read_proc (char *page, char **start, off_t off,
254 int count, int *eof, void *data)
256 int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
259 len += sprintf(page + len, "\n");
264 irq_affinity_write_proc(struct file *file, const char __user *buffer,
265 unsigned long count, void *data)
267 int irq = (long) data, full_count = count, err;
270 if (!irq_desc[irq].handler->set_affinity)
273 err = cpumask_parse(buffer, count, new_value);
275 /* The special value 0 means release control of the
276 affinity to kernel. */
277 cpus_and(new_value, new_value, cpu_online_map);
278 if (cpus_empty(new_value)) {
279 irq_user_affinity[irq] = 0;
280 select_smp_affinity(irq);
282 /* Do not allow disabling IRQs completely - it's a too easy
283 way to make the system unusable accidentally :-) At least
284 one online CPU still has to be targeted. */
286 irq_affinity[irq] = new_value;
287 irq_user_affinity[irq] = 1;
288 irq_desc[irq].handler->set_affinity(irq, new_value);
294 #endif /* CONFIG_SMP */
296 #define MAX_NAMELEN 10
299 register_irq_proc (unsigned int irq)
301 char name [MAX_NAMELEN];
303 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
307 memset(name, 0, MAX_NAMELEN);
308 sprintf(name, "%d", irq);
310 /* create /proc/irq/1234 */
311 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
314 if (irq_desc[irq].handler->set_affinity) {
315 struct proc_dir_entry *entry;
316 /* create /proc/irq/1234/smp_affinity */
317 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
321 entry->data = (void *)(long)irq;
322 entry->read_proc = irq_affinity_read_proc;
323 entry->write_proc = irq_affinity_write_proc;
326 smp_affinity_entry[irq] = entry;
336 /* create /proc/irq */
337 root_irq_dir = proc_mkdir("irq", NULL);
340 /* create /proc/irq/prof_cpu_mask */
341 create_prof_cpu_mask(root_irq_dir);
345 * Create entries for all existing IRQs.
347 for (i = 0; i < ACTUAL_NR_IRQS; i++) {
348 if (irq_desc[i].handler == &no_irq_type)
350 register_irq_proc(i);
355 request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
356 unsigned long irqflags, const char * devname, void *dev_id)
359 struct irqaction * action;
361 if (irq >= ACTUAL_NR_IRQS)
368 * Sanity-check: shared interrupts should REALLY pass in
369 * a real dev-ID, otherwise we'll have trouble later trying
370 * to figure out which interrupt is which (messes up the
371 * interrupt freeing logic etc).
373 if ((irqflags & SA_SHIRQ) && !dev_id) {
375 "Bad boy: %s (at %p) called us without a dev_id!\n",
376 devname, __builtin_return_address(0));
380 action = (struct irqaction *)
381 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
385 action->handler = handler;
386 action->flags = irqflags;
387 cpus_clear(action->mask);
388 action->name = devname;
390 action->dev_id = dev_id;
393 select_smp_affinity(irq);
396 retval = setup_irq(irq, action);
402 EXPORT_SYMBOL(request_irq);
405 free_irq(unsigned int irq, void *dev_id)
408 struct irqaction **p;
411 if (irq >= ACTUAL_NR_IRQS) {
412 printk(KERN_CRIT "Trying to free IRQ%d\n", irq);
416 desc = irq_desc + irq;
417 spin_lock_irqsave(&desc->lock,flags);
420 struct irqaction * action = *p;
422 struct irqaction **pp = p;
424 if (action->dev_id != dev_id)
427 /* Found - now remove it from the list of entries. */
430 desc->status |= IRQ_DISABLED;
431 desc->handler->shutdown(irq);
433 spin_unlock_irqrestore(&desc->lock,flags);
436 /* Wait to make sure it's not being used on
438 while (desc->status & IRQ_INPROGRESS)
444 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
445 spin_unlock_irqrestore(&desc->lock,flags);
450 EXPORT_SYMBOL(free_irq);
453 show_interrupts(struct seq_file *p, void *v)
458 int i = *(loff_t *) v;
459 struct irqaction * action;
465 for (i = 0; i < NR_CPUS; i++)
467 seq_printf(p, "CPU%d ", i);
472 if (i < ACTUAL_NR_IRQS) {
473 spin_lock_irqsave(&irq_desc[i].lock, flags);
474 action = irq_desc[i].action;
477 seq_printf(p, "%3d: ",i);
479 seq_printf(p, "%10u ", kstat_irqs(i));
481 for (j = 0; j < NR_CPUS; j++)
483 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
485 seq_printf(p, " %14s", irq_desc[i].handler->typename);
486 seq_printf(p, " %c%s",
487 (action->flags & SA_INTERRUPT)?'+':' ',
490 for (action=action->next; action; action = action->next) {
491 seq_printf(p, ", %c%s",
492 (action->flags & SA_INTERRUPT)?'+':' ',
498 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
499 } else if (i == ACTUAL_NR_IRQS) {
501 seq_puts(p, "IPI: ");
502 for (i = 0; i < NR_CPUS; i++)
504 seq_printf(p, "%10lu ", cpu_data[i].ipi_count);
507 seq_printf(p, "ERR: %10lu\n", irq_err_count);
514 * handle_irq handles all normal device IRQ's (the special
515 * SMP cross-CPU interrupts have their own specific
519 #define MAX_ILLEGAL_IRQS 16
522 handle_irq(int irq, struct pt_regs * regs)
525 * We ack quickly, we don't want the irq controller
526 * thinking we're snobs just because some other CPU has
527 * disabled global interrupts (we have already done the
528 * INT_ACK cycles, it's too late to try to pretend to the
529 * controller that we aren't taking the interrupt).
531 * 0 return value means that this irq is already being
532 * handled by some other CPU. (or is disabled)
534 int cpu = smp_processor_id();
535 irq_desc_t *desc = irq_desc + irq;
536 struct irqaction * action;
538 static unsigned int illegal_count=0;
540 if ((unsigned) irq > ACTUAL_NR_IRQS && illegal_count < MAX_ILLEGAL_IRQS ) {
543 printk(KERN_CRIT "device_interrupt: invalid interrupt %d\n",
549 kstat_cpu(cpu).irqs[irq]++;
550 spin_lock_irq(&desc->lock); /* mask also the higher prio events */
551 desc->handler->ack(irq);
553 * REPLAY is when Linux resends an IRQ that was dropped earlier.
554 * WAITING is used by probe to mark irqs that are being tested.
556 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
557 status |= IRQ_PENDING; /* we _want_ to handle it */
560 * If the IRQ is disabled for whatever reason, we cannot
561 * use the action we have.
564 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
565 action = desc->action;
566 status &= ~IRQ_PENDING; /* we commit to handling */
567 status |= IRQ_INPROGRESS; /* we are handling it */
569 desc->status = status;
572 * If there is no IRQ handler or it was disabled, exit early.
573 * Since we set PENDING, if another processor is handling
574 * a different instance of this same irq, the other processor
575 * will take care of it.
581 * Edge triggered interrupts need to remember pending events.
582 * This applies to any hw interrupts that allow a second
583 * instance of the same irq to arrive while we are in handle_irq
584 * or in the handler. But the code here only handles the _second_
585 * instance of the irq, not the third or fourth. So it is mostly
586 * useful for irq hardware that does not mask cleanly in an
590 spin_unlock(&desc->lock);
591 handle_IRQ_event(irq, regs, action);
592 spin_lock(&desc->lock);
594 if (!(desc->status & IRQ_PENDING)
595 || (desc->status & IRQ_LEVEL))
597 desc->status &= ~IRQ_PENDING;
599 desc->status &= ~IRQ_INPROGRESS;
602 * The ->end() handler has to deal with interrupts which got
603 * disabled while the handler was running.
605 desc->handler->end(irq);
606 spin_unlock(&desc->lock);
612 * IRQ autodetection code..
614 * This depends on the fact that any interrupt that
615 * comes in on to an unassigned handler will get stuck
616 * with "IRQ_WAITING" cleared and the interrupt
627 /* Something may have generated an irq long ago and we want to
628 flush such a longstanding irq before considering it as spurious. */
629 for (i = NR_IRQS-1; i >= 0; i--) {
632 spin_lock_irq(&desc->lock);
633 if (!irq_desc[i].action)
634 irq_desc[i].handler->startup(i);
635 spin_unlock_irq(&desc->lock);
638 /* Wait for longstanding interrupts to trigger. */
639 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
640 /* about 20ms delay */ barrier();
642 /* enable any unassigned irqs (we must startup again here because
643 if a longstanding irq happened in the previous stage, it may have
644 masked itself) first, enable any unassigned irqs. */
645 for (i = NR_IRQS-1; i >= 0; i--) {
648 spin_lock_irq(&desc->lock);
650 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
651 if (desc->handler->startup(i))
652 desc->status |= IRQ_PENDING;
654 spin_unlock_irq(&desc->lock);
658 * Wait for spurious interrupts to trigger
660 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
661 /* about 100ms delay */ barrier();
664 * Now filter out any obviously spurious interrupts
667 for (i=0; i<NR_IRQS; i++) {
668 irq_desc_t *desc = irq_desc + i;
671 spin_lock_irq(&desc->lock);
672 status = desc->status;
674 if (status & IRQ_AUTODETECT) {
675 /* It triggered already - consider it spurious. */
676 if (!(status & IRQ_WAITING)) {
677 desc->status = status & ~IRQ_AUTODETECT;
678 desc->handler->shutdown(i);
683 spin_unlock_irq(&desc->lock);
689 EXPORT_SYMBOL(probe_irq_on);
692 * Return a mask of triggered interrupts (this
693 * can handle only legacy ISA interrupts).
696 probe_irq_mask(unsigned long val)
702 for (i = 0; i < NR_IRQS; i++) {
703 irq_desc_t *desc = irq_desc + i;
706 spin_lock_irq(&desc->lock);
707 status = desc->status;
709 if (status & IRQ_AUTODETECT) {
710 /* We only react to ISA interrupts */
711 if (!(status & IRQ_WAITING)) {
716 desc->status = status & ~IRQ_AUTODETECT;
717 desc->handler->shutdown(i);
719 spin_unlock_irq(&desc->lock);
726 * Get the result of the IRQ probe.. A negative result means that
727 * we have several candidates (but we return the lowest-numbered
732 probe_irq_off(unsigned long val)
734 int i, irq_found, nr_irqs;
738 for (i=0; i<NR_IRQS; i++) {
739 irq_desc_t *desc = irq_desc + i;
742 spin_lock_irq(&desc->lock);
743 status = desc->status;
745 if (status & IRQ_AUTODETECT) {
746 if (!(status & IRQ_WAITING)) {
751 desc->status = status & ~IRQ_AUTODETECT;
752 desc->handler->shutdown(i);
754 spin_unlock_irq(&desc->lock);
758 irq_found = -irq_found;
762 EXPORT_SYMBOL(probe_irq_off);
765 void synchronize_irq(unsigned int irq)
767 /* is there anything to synchronize with? */
768 if (!irq_desc[irq].action)
771 while (irq_desc[irq].status & IRQ_INPROGRESS)