Linux-2.6.12-rc2
[linux-2.6/next.git] / arch / alpha / kernel / irq.c
blobb6114f5c0d2bc6d820cde8171f04d27ecde424e9
1 /*
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
10 * should be easier.
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>
32 #include <asm/io.h>
33 #include <asm/uaccess.h>
36 * Controller mappings for all interrupt sources:
38 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
39 [0 ... NR_IRQS-1] = {
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)
55 return IRQ_NONE;
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; }
65 static void
66 no_irq_ack(unsigned int irq)
68 irq_err_count++;
69 printk(KERN_CRIT "Unexpected IRQ trap at vector %u\n", irq);
72 struct hw_interrupt_type no_irq_type = {
73 .typename = "none",
74 .startup = no_irq_startup,
75 .shutdown = no_irq_enable_disable,
76 .enable = no_irq_enable_disable,
77 .disable = no_irq_enable_disable,
78 .ack = no_irq_ack,
79 .end = no_irq_enable_disable,
82 int
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 */
87 int ret;
89 do {
90 if (!(action->flags & SA_INTERRUPT))
91 local_irq_enable();
92 else
93 local_irq_disable();
95 ret = action->handler(irq, action->dev_id, regs);
96 if (ret == IRQ_HANDLED)
97 status |= action->flags;
98 action = action->next;
99 } while (action);
100 if (status & SA_SAMPLE_RANDOM)
101 add_interrupt_randomness(irq);
102 local_irq_disable();
104 return status;
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
111 * controller lock.
113 void inline
114 disable_irq_nosync(unsigned int irq)
116 irq_desc_t *desc = irq_desc + irq;
117 unsigned long flags;
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..
131 void
132 disable_irq(unsigned int irq)
134 disable_irq_nosync(irq);
135 synchronize_irq(irq);
138 void
139 enable_irq(unsigned int irq)
141 irq_desc_t *desc = irq_desc + irq;
142 unsigned long flags;
144 spin_lock_irqsave(&desc->lock, flags);
145 switch (desc->depth) {
146 case 1: {
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);
154 /* fall-through */
156 default:
157 desc->depth--;
158 break;
159 case 0:
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)
169 int shared = 0;
170 struct irqaction *old, **p;
171 unsigned long flags;
172 irq_desc_t *desc = irq_desc + irq;
174 if (desc->handler == &no_irq_type)
175 return -ENOSYS;
178 * Some drivers like serial.c use request_irq() heavily,
179 * so we have to be careful not to interfere with a
180 * running system.
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);
198 p = &desc->action;
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);
203 return -EBUSY;
206 /* add new interrupt at end of irq queue */
207 do {
208 p = &old->next;
209 old = *p;
210 } while (old);
211 shared = 1;
214 *p = new;
216 if (!shared) {
217 desc->depth = 0;
218 desc->status &=
219 ~(IRQ_DISABLED|IRQ_AUTODETECT|IRQ_WAITING|IRQ_INPROGRESS);
220 desc->handler->startup(irq);
222 spin_unlock_irqrestore(&desc->lock,flags);
224 return 0;
227 static struct proc_dir_entry * root_irq_dir;
228 static struct proc_dir_entry * irq_dir[NR_IRQS];
230 #ifdef CONFIG_SMP
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 };
235 static void
236 select_smp_affinity(int irq)
238 static int last_cpu;
239 int cpu = last_cpu + 1;
241 if (! irq_desc[irq].handler->set_affinity || irq_user_affinity[irq])
242 return;
244 while (!cpu_possible(cpu))
245 cpu = (cpu < (NR_CPUS-1) ? cpu + 1 : 0);
246 last_cpu = cpu;
248 irq_affinity[irq] = cpumask_of_cpu(cpu);
249 irq_desc[irq].handler->set_affinity(irq, cpumask_of_cpu(cpu));
252 static int
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]);
257 if (count - len < 2)
258 return -EINVAL;
259 len += sprintf(page + len, "\n");
260 return len;
263 static int
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;
268 cpumask_t new_value;
270 if (!irq_desc[irq].handler->set_affinity)
271 return -EIO;
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. */
285 else {
286 irq_affinity[irq] = new_value;
287 irq_user_affinity[irq] = 1;
288 irq_desc[irq].handler->set_affinity(irq, new_value);
291 return full_count;
294 #endif /* CONFIG_SMP */
296 #define MAX_NAMELEN 10
298 static void
299 register_irq_proc (unsigned int irq)
301 char name [MAX_NAMELEN];
303 if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
304 irq_dir[irq])
305 return;
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);
313 #ifdef CONFIG_SMP
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]);
319 if (entry) {
320 entry->nlink = 1;
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;
328 #endif
331 void
332 init_irq_proc (void)
334 int i;
336 /* create /proc/irq */
337 root_irq_dir = proc_mkdir("irq", NULL);
339 #ifdef CONFIG_SMP
340 /* create /proc/irq/prof_cpu_mask */
341 create_prof_cpu_mask(root_irq_dir);
342 #endif
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)
349 continue;
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)
358 int retval;
359 struct irqaction * action;
361 if (irq >= ACTUAL_NR_IRQS)
362 return -EINVAL;
363 if (!handler)
364 return -EINVAL;
366 #if 1
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) {
374 printk(KERN_ERR
375 "Bad boy: %s (at %p) called us without a dev_id!\n",
376 devname, __builtin_return_address(0));
378 #endif
380 action = (struct irqaction *)
381 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
382 if (!action)
383 return -ENOMEM;
385 action->handler = handler;
386 action->flags = irqflags;
387 cpus_clear(action->mask);
388 action->name = devname;
389 action->next = NULL;
390 action->dev_id = dev_id;
392 #ifdef CONFIG_SMP
393 select_smp_affinity(irq);
394 #endif
396 retval = setup_irq(irq, action);
397 if (retval)
398 kfree(action);
399 return retval;
402 EXPORT_SYMBOL(request_irq);
404 void
405 free_irq(unsigned int irq, void *dev_id)
407 irq_desc_t *desc;
408 struct irqaction **p;
409 unsigned long flags;
411 if (irq >= ACTUAL_NR_IRQS) {
412 printk(KERN_CRIT "Trying to free IRQ%d\n", irq);
413 return;
416 desc = irq_desc + irq;
417 spin_lock_irqsave(&desc->lock,flags);
418 p = &desc->action;
419 for (;;) {
420 struct irqaction * action = *p;
421 if (action) {
422 struct irqaction **pp = p;
423 p = &action->next;
424 if (action->dev_id != dev_id)
425 continue;
427 /* Found - now remove it from the list of entries. */
428 *pp = action->next;
429 if (!desc->action) {
430 desc->status |= IRQ_DISABLED;
431 desc->handler->shutdown(irq);
433 spin_unlock_irqrestore(&desc->lock,flags);
435 #ifdef CONFIG_SMP
436 /* Wait to make sure it's not being used on
437 another CPU. */
438 while (desc->status & IRQ_INPROGRESS)
439 barrier();
440 #endif
441 kfree(action);
442 return;
444 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
445 spin_unlock_irqrestore(&desc->lock,flags);
446 return;
450 EXPORT_SYMBOL(free_irq);
453 show_interrupts(struct seq_file *p, void *v)
455 #ifdef CONFIG_SMP
456 int j;
457 #endif
458 int i = *(loff_t *) v;
459 struct irqaction * action;
460 unsigned long flags;
462 #ifdef CONFIG_SMP
463 if (i == 0) {
464 seq_puts(p, " ");
465 for (i = 0; i < NR_CPUS; i++)
466 if (cpu_online(i))
467 seq_printf(p, "CPU%d ", i);
468 seq_putc(p, '\n');
470 #endif
472 if (i < ACTUAL_NR_IRQS) {
473 spin_lock_irqsave(&irq_desc[i].lock, flags);
474 action = irq_desc[i].action;
475 if (!action)
476 goto unlock;
477 seq_printf(p, "%3d: ",i);
478 #ifndef CONFIG_SMP
479 seq_printf(p, "%10u ", kstat_irqs(i));
480 #else
481 for (j = 0; j < NR_CPUS; j++)
482 if (cpu_online(j))
483 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
484 #endif
485 seq_printf(p, " %14s", irq_desc[i].handler->typename);
486 seq_printf(p, " %c%s",
487 (action->flags & SA_INTERRUPT)?'+':' ',
488 action->name);
490 for (action=action->next; action; action = action->next) {
491 seq_printf(p, ", %c%s",
492 (action->flags & SA_INTERRUPT)?'+':' ',
493 action->name);
496 seq_putc(p, '\n');
497 unlock:
498 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
499 } else if (i == ACTUAL_NR_IRQS) {
500 #ifdef CONFIG_SMP
501 seq_puts(p, "IPI: ");
502 for (i = 0; i < NR_CPUS; i++)
503 if (cpu_online(i))
504 seq_printf(p, "%10lu ", cpu_data[i].ipi_count);
505 seq_putc(p, '\n');
506 #endif
507 seq_printf(p, "ERR: %10lu\n", irq_err_count);
509 return 0;
514 * handle_irq handles all normal device IRQ's (the special
515 * SMP cross-CPU interrupts have their own specific
516 * handlers).
519 #define MAX_ILLEGAL_IRQS 16
521 void
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;
537 unsigned int status;
538 static unsigned int illegal_count=0;
540 if ((unsigned) irq > ACTUAL_NR_IRQS && illegal_count < MAX_ILLEGAL_IRQS ) {
541 irq_err_count++;
542 illegal_count++;
543 printk(KERN_CRIT "device_interrupt: invalid interrupt %d\n",
544 irq);
545 return;
548 irq_enter();
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.
563 action = NULL;
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.
577 if (!action)
578 goto out;
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
587 * SMP environment.
589 for (;;) {
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))
596 break;
597 desc->status &= ~IRQ_PENDING;
599 desc->status &= ~IRQ_INPROGRESS;
600 out:
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);
608 irq_exit();
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
617 * disabled.
619 unsigned long
620 probe_irq_on(void)
622 int i;
623 irq_desc_t *desc;
624 unsigned long delay;
625 unsigned long val;
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--) {
630 desc = irq_desc + 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--) {
646 desc = irq_desc + i;
648 spin_lock_irq(&desc->lock);
649 if (!desc->action) {
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
666 val = 0;
667 for (i=0; i<NR_IRQS; i++) {
668 irq_desc_t *desc = irq_desc + i;
669 unsigned int status;
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);
679 } else
680 if (i < 32)
681 val |= 1 << i;
683 spin_unlock_irq(&desc->lock);
686 return val;
689 EXPORT_SYMBOL(probe_irq_on);
692 * Return a mask of triggered interrupts (this
693 * can handle only legacy ISA interrupts).
695 unsigned int
696 probe_irq_mask(unsigned long val)
698 int i;
699 unsigned int mask;
701 mask = 0;
702 for (i = 0; i < NR_IRQS; i++) {
703 irq_desc_t *desc = irq_desc + i;
704 unsigned int status;
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)) {
712 if (i < 16)
713 mask |= 1 << i;
716 desc->status = status & ~IRQ_AUTODETECT;
717 desc->handler->shutdown(i);
719 spin_unlock_irq(&desc->lock);
722 return mask & val;
726 * Get the result of the IRQ probe.. A negative result means that
727 * we have several candidates (but we return the lowest-numbered
728 * one).
732 probe_irq_off(unsigned long val)
734 int i, irq_found, nr_irqs;
736 nr_irqs = 0;
737 irq_found = 0;
738 for (i=0; i<NR_IRQS; i++) {
739 irq_desc_t *desc = irq_desc + i;
740 unsigned int status;
742 spin_lock_irq(&desc->lock);
743 status = desc->status;
745 if (status & IRQ_AUTODETECT) {
746 if (!(status & IRQ_WAITING)) {
747 if (!nr_irqs)
748 irq_found = i;
749 nr_irqs++;
751 desc->status = status & ~IRQ_AUTODETECT;
752 desc->handler->shutdown(i);
754 spin_unlock_irq(&desc->lock);
757 if (nr_irqs > 1)
758 irq_found = -irq_found;
759 return irq_found;
762 EXPORT_SYMBOL(probe_irq_off);
764 #ifdef CONFIG_SMP
765 void synchronize_irq(unsigned int irq)
767 /* is there anything to synchronize with? */
768 if (!irq_desc[irq].action)
769 return;
771 while (irq_desc[irq].status & IRQ_INPROGRESS)
772 barrier();
774 #endif