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>
29 #include <asm/system.h>
31 #include <asm/bitops.h>
32 #include <asm/uaccess.h>
35 * Controller mappings for all interrupt sources:
37 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
39 .handler = &no_irq_type,
40 .lock = SPIN_LOCK_UNLOCKED
44 static void register_irq_proc(unsigned int irq);
46 volatile unsigned long irq_err_count;
49 * Special irq handlers.
52 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
58 * Generic no controller code
61 static void no_irq_enable_disable(unsigned int irq) { }
62 static unsigned int no_irq_startup(unsigned int irq) { return 0; }
65 no_irq_ack(unsigned int irq)
68 printk(KERN_CRIT "Unexpected IRQ trap at vector %u\n", irq);
71 struct hw_interrupt_type no_irq_type = {
73 .startup = no_irq_startup,
74 .shutdown = no_irq_enable_disable,
75 .enable = no_irq_enable_disable,
76 .disable = no_irq_enable_disable,
78 .end = no_irq_enable_disable,
82 handle_IRQ_event(unsigned int irq, struct pt_regs *regs,
83 struct irqaction *action)
85 int status = 1; /* Force the "do bottom halves" bit */
88 if (!(action->flags & SA_INTERRUPT))
93 status |= action->flags;
94 action->handler(irq, action->dev_id, regs);
95 action = action->next;
97 if (status & SA_SAMPLE_RANDOM)
98 add_interrupt_randomness(irq);
105 * Generic enable/disable code: this just calls
106 * down into the PIC-specific version for the actual
107 * hardware disable after having gotten the irq
111 disable_irq_nosync(unsigned int irq)
113 irq_desc_t *desc = irq_desc + irq;
116 spin_lock_irqsave(&desc->lock, flags);
117 if (!desc->depth++) {
118 desc->status |= IRQ_DISABLED;
119 desc->handler->disable(irq);
121 spin_unlock_irqrestore(&desc->lock, flags);
125 * Synchronous version of the above, making sure the IRQ is
126 * no longer running on any other IRQ..
129 disable_irq(unsigned int irq)
131 disable_irq_nosync(irq);
132 synchronize_irq(irq);
136 enable_irq(unsigned int irq)
138 irq_desc_t *desc = irq_desc + irq;
141 spin_lock_irqsave(&desc->lock, flags);
142 switch (desc->depth) {
144 unsigned int status = desc->status & ~IRQ_DISABLED;
145 desc->status = status;
146 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
147 desc->status = status | IRQ_REPLAY;
148 hw_resend_irq(desc->handler,irq);
150 desc->handler->enable(irq);
157 printk(KERN_ERR "enable_irq() unbalanced from %p\n",
158 __builtin_return_address(0));
160 spin_unlock_irqrestore(&desc->lock, flags);
164 setup_irq(unsigned int irq, struct irqaction * new)
167 struct irqaction *old, **p;
169 irq_desc_t *desc = irq_desc + irq;
171 if (desc->handler == &no_irq_type)
175 * Some drivers like serial.c use request_irq() heavily,
176 * so we have to be careful not to interfere with a
179 if (new->flags & SA_SAMPLE_RANDOM) {
181 * This function might sleep, we want to call it first,
182 * outside of the atomic block.
183 * Yes, this might clear the entropy pool if the wrong
184 * driver is attempted to be loaded, without actually
185 * installing a new handler, but is this really a problem,
186 * only the sysadmin is able to do this.
188 rand_initialize_irq(irq);
192 * The following block of code has to be executed atomically
194 spin_lock_irqsave(&desc->lock,flags);
196 if ((old = *p) != NULL) {
197 /* Can't share interrupts unless both agree to */
198 if (!(old->flags & new->flags & SA_SHIRQ)) {
199 spin_unlock_irqrestore(&desc->lock,flags);
203 /* add new interrupt at end of irq queue */
216 ~(IRQ_DISABLED|IRQ_AUTODETECT|IRQ_WAITING|IRQ_INPROGRESS);
217 desc->handler->startup(irq);
219 spin_unlock_irqrestore(&desc->lock,flags);
224 static struct proc_dir_entry * root_irq_dir;
225 static struct proc_dir_entry * irq_dir[NR_IRQS];
228 static struct proc_dir_entry * smp_affinity_entry[NR_IRQS];
229 static char irq_user_affinity[NR_IRQS];
230 static unsigned long irq_affinity[NR_IRQS] = { [0 ... NR_IRQS-1] = ~0UL };
233 select_smp_affinity(int irq)
236 int cpu = last_cpu + 1;
238 if (! irq_desc[irq].handler->set_affinity || irq_user_affinity[irq])
241 while (((cpu_present_mask >> cpu) & 1) == 0)
242 cpu = (cpu < (NR_CPUS-1) ? cpu + 1 : 0);
245 irq_affinity[irq] = 1UL << cpu;
246 irq_desc[irq].handler->set_affinity(irq, 1UL << cpu);
249 #define HEX_DIGITS 16
252 irq_affinity_read_proc (char *page, char **start, off_t off,
253 int count, int *eof, void *data)
255 int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
258 len += sprintf(page + len, "\n");
263 parse_hex_value (const char __user *buffer,
264 unsigned long count, unsigned long *ret)
266 unsigned char hexnum [HEX_DIGITS];
272 if (count > HEX_DIGITS)
274 if (copy_from_user(hexnum, buffer, count))
278 * Parse the first 8 characters as a hex string, any non-hex char
279 * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
283 for (i = 0; i < count; i++) {
284 unsigned int c = hexnum[i];
287 case '0' ... '9': c -= '0'; break;
288 case 'a' ... 'f': c -= 'a'-10; break;
289 case 'A' ... 'F': c -= 'A'-10; break;
293 value = (value << 4) | c;
301 irq_affinity_write_proc(struct file *file, const char __user *buffer,
302 unsigned long count, void *data)
304 int irq = (long) data, full_count = count, err;
305 unsigned long new_value;
307 if (!irq_desc[irq].handler->set_affinity)
310 err = parse_hex_value(buffer, count, &new_value);
312 /* The special value 0 means release control of the
313 affinity to kernel. */
314 if (new_value == 0) {
315 irq_user_affinity[irq] = 0;
316 select_smp_affinity(irq);
318 /* Do not allow disabling IRQs completely - it's a too easy
319 way to make the system unusable accidentally :-) At least
320 one online CPU still has to be targeted. */
321 else if (!(new_value & cpu_present_mask))
324 irq_affinity[irq] = new_value;
325 irq_user_affinity[irq] = 1;
326 irq_desc[irq].handler->set_affinity(irq, new_value);
333 prof_cpu_mask_read_proc(char *page, char **start, off_t off,
334 int count, int *eof, void *data)
336 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
339 len += sprintf(page + len, "\n");
344 prof_cpu_mask_write_proc(struct file *file, const char __user *buffer,
345 unsigned long count, void *data)
347 unsigned long *mask = (unsigned long *) data, full_count = count, err;
348 unsigned long new_value;
350 err = parse_hex_value(buffer, count, &new_value);
357 #endif /* CONFIG_SMP */
359 #define MAX_NAMELEN 10
362 register_irq_proc (unsigned int irq)
364 char name [MAX_NAMELEN];
366 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
370 memset(name, 0, MAX_NAMELEN);
371 sprintf(name, "%d", irq);
373 /* create /proc/irq/1234 */
374 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
377 if (irq_desc[irq].handler->set_affinity) {
378 struct proc_dir_entry *entry;
379 /* create /proc/irq/1234/smp_affinity */
380 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
384 entry->data = (void *)(long)irq;
385 entry->read_proc = irq_affinity_read_proc;
386 entry->write_proc = irq_affinity_write_proc;
389 smp_affinity_entry[irq] = entry;
394 unsigned long prof_cpu_mask = ~0UL;
400 struct proc_dir_entry *entry;
404 /* create /proc/irq */
405 root_irq_dir = proc_mkdir("irq", 0);
408 /* create /proc/irq/prof_cpu_mask */
409 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
412 entry->data = (void *)&prof_cpu_mask;
413 entry->read_proc = prof_cpu_mask_read_proc;
414 entry->write_proc = prof_cpu_mask_write_proc;
418 * Create entries for all existing IRQs.
420 for (i = 0; i < ACTUAL_NR_IRQS; i++) {
421 if (irq_desc[i].handler == &no_irq_type)
423 register_irq_proc(i);
428 request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
429 unsigned long irqflags, const char * devname, void *dev_id)
432 struct irqaction * action;
434 if (irq >= ACTUAL_NR_IRQS)
441 * Sanity-check: shared interrupts should REALLY pass in
442 * a real dev-ID, otherwise we'll have trouble later trying
443 * to figure out which interrupt is which (messes up the
444 * interrupt freeing logic etc).
446 if ((irqflags & SA_SHIRQ) && !dev_id) {
448 "Bad boy: %s (at %p) called us without a dev_id!\n",
449 devname, __builtin_return_address(0));
453 action = (struct irqaction *)
454 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
458 action->handler = handler;
459 action->flags = irqflags;
461 action->name = devname;
463 action->dev_id = dev_id;
466 select_smp_affinity(irq);
469 retval = setup_irq(irq, action);
475 EXPORT_SYMBOL(request_irq);
478 free_irq(unsigned int irq, void *dev_id)
481 struct irqaction **p;
484 if (irq >= ACTUAL_NR_IRQS) {
485 printk(KERN_CRIT "Trying to free IRQ%d\n", irq);
489 desc = irq_desc + irq;
490 spin_lock_irqsave(&desc->lock,flags);
493 struct irqaction * action = *p;
495 struct irqaction **pp = p;
497 if (action->dev_id != dev_id)
500 /* Found - now remove it from the list of entries. */
503 desc->status |= IRQ_DISABLED;
504 desc->handler->shutdown(irq);
506 spin_unlock_irqrestore(&desc->lock,flags);
509 /* Wait to make sure it's not being used on
511 while (desc->status & IRQ_INPROGRESS)
517 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
518 spin_unlock_irqrestore(&desc->lock,flags);
523 EXPORT_SYMBOL(free_irq);
526 show_interrupts(struct seq_file *p, void *v)
531 int i = *(loff_t *) v;
532 struct irqaction * action;
538 for (i = 0; i < NR_CPUS; i++)
540 seq_printf(p, "CPU%d ", i);
545 if (i < ACTUAL_NR_IRQS) {
546 spin_lock_irqsave(&irq_desc[i].lock, flags);
547 action = irq_desc[i].action;
550 seq_printf(p, "%3d: ",i);
552 seq_printf(p, "%10u ", kstat_irqs(i));
554 for (j = 0; j < NR_CPUS; j++)
556 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
558 seq_printf(p, " %14s", irq_desc[i].handler->typename);
559 seq_printf(p, " %c%s",
560 (action->flags & SA_INTERRUPT)?'+':' ',
563 for (action=action->next; action; action = action->next) {
564 seq_printf(p, ", %c%s",
565 (action->flags & SA_INTERRUPT)?'+':' ',
571 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
572 } else if (i == ACTUAL_NR_IRQS) {
574 seq_puts(p, "IPI: ");
575 for (i = 0; i < NR_CPUS; i++)
577 seq_printf(p, "%10lu ", cpu_data[i].ipi_count);
580 seq_printf(p, "ERR: %10lu\n", irq_err_count);
587 * handle_irq handles all normal device IRQ's (the special
588 * SMP cross-CPU interrupts have their own specific
592 #define MAX_ILLEGAL_IRQS 16
595 handle_irq(int irq, struct pt_regs * regs)
598 * We ack quickly, we don't want the irq controller
599 * thinking we're snobs just because some other CPU has
600 * disabled global interrupts (we have already done the
601 * INT_ACK cycles, it's too late to try to pretend to the
602 * controller that we aren't taking the interrupt).
604 * 0 return value means that this irq is already being
605 * handled by some other CPU. (or is disabled)
607 int cpu = smp_processor_id();
608 irq_desc_t *desc = irq_desc + irq;
609 struct irqaction * action;
611 static unsigned int illegal_count=0;
613 if ((unsigned) irq > ACTUAL_NR_IRQS && illegal_count < MAX_ILLEGAL_IRQS ) {
616 printk(KERN_CRIT "device_interrupt: invalid interrupt %d\n",
622 kstat_cpu(cpu).irqs[irq]++;
623 spin_lock_irq(&desc->lock); /* mask also the higher prio events */
624 desc->handler->ack(irq);
626 * REPLAY is when Linux resends an IRQ that was dropped earlier.
627 * WAITING is used by probe to mark irqs that are being tested.
629 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
630 status |= IRQ_PENDING; /* we _want_ to handle it */
633 * If the IRQ is disabled for whatever reason, we cannot
634 * use the action we have.
637 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
638 action = desc->action;
639 status &= ~IRQ_PENDING; /* we commit to handling */
640 status |= IRQ_INPROGRESS; /* we are handling it */
642 desc->status = status;
645 * If there is no IRQ handler or it was disabled, exit early.
646 * Since we set PENDING, if another processor is handling
647 * a different instance of this same irq, the other processor
648 * will take care of it.
654 * Edge triggered interrupts need to remember pending events.
655 * This applies to any hw interrupts that allow a second
656 * instance of the same irq to arrive while we are in handle_irq
657 * or in the handler. But the code here only handles the _second_
658 * instance of the irq, not the third or fourth. So it is mostly
659 * useful for irq hardware that does not mask cleanly in an
663 spin_unlock(&desc->lock);
664 handle_IRQ_event(irq, regs, action);
665 spin_lock(&desc->lock);
667 if (!(desc->status & IRQ_PENDING)
668 || (desc->status & IRQ_LEVEL))
670 desc->status &= ~IRQ_PENDING;
672 desc->status &= ~IRQ_INPROGRESS;
675 * The ->end() handler has to deal with interrupts which got
676 * disabled while the handler was running.
678 desc->handler->end(irq);
679 spin_unlock(&desc->lock);
685 * IRQ autodetection code..
687 * This depends on the fact that any interrupt that
688 * comes in on to an unassigned handler will get stuck
689 * with "IRQ_WAITING" cleared and the interrupt
700 /* Something may have generated an irq long ago and we want to
701 flush such a longstanding irq before considering it as spurious. */
702 for (i = NR_IRQS-1; i >= 0; i--) {
705 spin_lock_irq(&desc->lock);
706 if (!irq_desc[i].action)
707 irq_desc[i].handler->startup(i);
708 spin_unlock_irq(&desc->lock);
711 /* Wait for longstanding interrupts to trigger. */
712 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
713 /* about 20ms delay */ barrier();
715 /* enable any unassigned irqs (we must startup again here because
716 if a longstanding irq happened in the previous stage, it may have
717 masked itself) first, enable any unassigned irqs. */
718 for (i = NR_IRQS-1; i >= 0; i--) {
721 spin_lock_irq(&desc->lock);
723 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
724 if (desc->handler->startup(i))
725 desc->status |= IRQ_PENDING;
727 spin_unlock_irq(&desc->lock);
731 * Wait for spurious interrupts to trigger
733 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
734 /* about 100ms delay */ barrier();
737 * Now filter out any obviously spurious interrupts
740 for (i=0; i<NR_IRQS; i++) {
741 irq_desc_t *desc = irq_desc + i;
744 spin_lock_irq(&desc->lock);
745 status = desc->status;
747 if (status & IRQ_AUTODETECT) {
748 /* It triggered already - consider it spurious. */
749 if (!(status & IRQ_WAITING)) {
750 desc->status = status & ~IRQ_AUTODETECT;
751 desc->handler->shutdown(i);
756 spin_unlock_irq(&desc->lock);
762 EXPORT_SYMBOL(probe_irq_on);
765 * Return a mask of triggered interrupts (this
766 * can handle only legacy ISA interrupts).
769 probe_irq_mask(unsigned long val)
775 for (i = 0; i < NR_IRQS; i++) {
776 irq_desc_t *desc = irq_desc + i;
779 spin_lock_irq(&desc->lock);
780 status = desc->status;
782 if (status & IRQ_AUTODETECT) {
783 /* We only react to ISA interrupts */
784 if (!(status & IRQ_WAITING)) {
789 desc->status = status & ~IRQ_AUTODETECT;
790 desc->handler->shutdown(i);
792 spin_unlock_irq(&desc->lock);
799 * Get the result of the IRQ probe.. A negative result means that
800 * we have several candidates (but we return the lowest-numbered
805 probe_irq_off(unsigned long val)
807 int i, irq_found, nr_irqs;
811 for (i=0; i<NR_IRQS; i++) {
812 irq_desc_t *desc = irq_desc + i;
815 spin_lock_irq(&desc->lock);
816 status = desc->status;
818 if (status & IRQ_AUTODETECT) {
819 if (!(status & IRQ_WAITING)) {
824 desc->status = status & ~IRQ_AUTODETECT;
825 desc->handler->shutdown(i);
827 spin_unlock_irq(&desc->lock);
831 irq_found = -irq_found;
835 EXPORT_SYMBOL(probe_irq_off);
838 void synchronize_irq(unsigned int irq)
840 /* is there anything to synchronize with? */
841 if (!irq_desc[irq].action)
844 while (irq_desc[irq].status & IRQ_INPROGRESS)