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 * arch/sh64/kernel/irq.c
8 * Copyright (C) 2000, 2001 Paolo Alberelli
9 * Copyright (C) 2003 Paul Mundt
14 * IRQs are in fact implemented a bit like signal handlers for the kernel.
15 * Naturally it's not a 1:1 relation, but there are similarities.
18 #include <linux/config.h>
19 #include <linux/errno.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/signal.h>
22 #include <linux/rwsem.h>
23 #include <linux/sched.h>
24 #include <linux/ioport.h>
25 #include <linux/interrupt.h>
26 #include <linux/timex.h>
27 #include <linux/slab.h>
28 #include <linux/random.h>
29 #include <linux/smp.h>
30 #include <linux/smp_lock.h>
31 #include <linux/init.h>
32 #include <linux/seq_file.h>
33 #include <asm/system.h>
35 #include <asm/bitops.h>
37 #include <asm/pgalloc.h>
38 #include <asm/delay.h>
40 #include <linux/irq.h>
43 * Controller mappings for all interrupt sources:
45 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
47 .handler = &no_irq_type,
48 .lock = SPIN_LOCK_UNLOCKED
54 * Special irq handlers.
57 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
63 * Generic no controller code
66 static void enable_none(unsigned int irq) { }
67 static unsigned int startup_none(unsigned int irq) { return 0; }
68 static void disable_none(unsigned int irq) { }
69 static void ack_none(unsigned int irq)
72 * 'what should we do if we get a hw irq event on an illegal vector'.
73 * each architecture has to answer this themselves, it doesnt deserve
74 * a generic callback i think.
76 printk("unexpected IRQ trap at irq %02x\n", irq);
79 /* startup is the same as "enable", shutdown is same as "disable" */
80 #define shutdown_none disable_none
81 #define end_none enable_none
83 struct hw_interrupt_type no_irq_type = {
93 #if defined(CONFIG_PROC_FS)
94 int show_interrupts(struct seq_file *p, void *v)
96 int i = *(loff_t *) v, j;
97 struct irqaction * action;
102 for (j=0; j<NR_CPUS; j++)
104 seq_printf(p, "CPU%d ",j);
109 spin_lock_irqsave(&irq_desc[i].lock, flags);
110 action = irq_desc[i].action;
113 seq_printf(p, "%3d: ",i);
114 seq_printf(p, "%10u ", kstat_irqs(i));
115 seq_printf(p, " %14s", irq_desc[i].handler->typename);
116 seq_printf(p, " %s", action->name);
118 for (action=action->next; action; action = action->next)
119 seq_printf(p, ", %s", action->name);
122 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
129 * do_NMI handles all Non-Maskable Interrupts.
131 asmlinkage void do_NMI(unsigned long vector_num, struct pt_regs * regs)
133 if (regs->sr & 0x40000000)
134 printk("unexpected NMI trap in system mode\n");
136 printk("unexpected NMI trap in user mode\n");
142 * This should really return information about whether
143 * we should do bottom half handling etc. Right now we
144 * end up _always_ checking the bottom half, which is a
145 * waste of time and is not what some drivers would
148 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
152 status = 1; /* Force the "do bottom halves" bit */
154 if (!(action->flags & SA_INTERRUPT))
158 status |= action->flags;
159 action->handler(irq, action->dev_id, regs);
160 action = action->next;
162 if (status & SA_SAMPLE_RANDOM)
163 add_interrupt_randomness(irq);
171 * Generic enable/disable code: this just calls
172 * down into the PIC-specific version for the actual
173 * hardware disable after having gotten the irq
178 * disable_irq_nosync - disable an irq without waiting
179 * @irq: Interrupt to disable
181 * Disable the selected interrupt line. Disables of an interrupt
182 * stack. Unlike disable_irq(), this function does not ensure existing
183 * instances of the IRQ handler have completed before returning.
185 * This function may be called from IRQ context.
187 void disable_irq_nosync(unsigned int irq)
189 irq_desc_t *desc = irq_desc + irq;
192 spin_lock_irqsave(&desc->lock, flags);
193 if (!desc->depth++) {
194 desc->status |= IRQ_DISABLED;
195 desc->handler->disable(irq);
197 spin_unlock_irqrestore(&desc->lock, flags);
201 * disable_irq - disable an irq and wait for completion
202 * @irq: Interrupt to disable
204 * Disable the selected interrupt line. Disables of an interrupt
205 * stack. That is for two disables you need two enables. This
206 * function waits for any pending IRQ handlers for this interrupt
207 * to complete before returning. If you use this function while
208 * holding a resource the IRQ handler may need you will deadlock.
210 * This function may be called - with care - from IRQ context.
212 void disable_irq(unsigned int irq)
214 disable_irq_nosync(irq);
215 synchronize_irq(irq);
219 * enable_irq - enable interrupt handling on an irq
220 * @irq: Interrupt to enable
222 * Re-enables the processing of interrupts on this IRQ line
223 * providing no disable_irq calls are now in effect.
225 * This function may be called from IRQ context.
227 void enable_irq(unsigned int irq)
229 irq_desc_t *desc = irq_desc + irq;
232 spin_lock_irqsave(&desc->lock, flags);
233 switch (desc->depth) {
235 unsigned int status = desc->status & ~IRQ_DISABLED;
236 desc->status = status;
237 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
238 desc->status = status | IRQ_REPLAY;
239 hw_resend_irq(desc->handler,irq);
241 desc->handler->enable(irq);
248 printk("enable_irq() unbalanced from %p\n",
249 __builtin_return_address(0));
251 spin_unlock_irqrestore(&desc->lock, flags);
255 * do_IRQ handles all normal device IRQ's.
257 asmlinkage int do_IRQ(unsigned long vector_num, struct pt_regs * regs)
260 * We ack quickly, we don't want the irq controller
261 * thinking we're snobs just because some other CPU has
262 * disabled global interrupts (we have already done the
263 * INT_ACK cycles, it's too late to try to pretend to the
264 * controller that we aren't taking the interrupt).
266 * 0 return value means that this irq is already being
267 * handled by some other CPU. (or is disabled)
270 int cpu = smp_processor_id();
271 irq_desc_t *desc = NULL;
272 struct irqaction * action;
277 #ifdef CONFIG_PREEMPT
279 * At this point we're now about to actually call handlers,
280 * and interrupts might get reenabled during them... bump
281 * preempt_count to prevent any preemption while the handler
282 * called here is pending...
287 irq = irq_demux(vector_num);
290 * Should never happen, if it does check
291 * vectorN_to_IRQ[] against trap_jtable[].
294 printk("unexpected IRQ trap at vector %03lx\n", vector_num);
298 desc = irq_desc + irq;
300 kstat_cpu(cpu).irqs[irq]++;
301 spin_lock(&desc->lock);
302 desc->handler->ack(irq);
304 REPLAY is when Linux resends an IRQ that was dropped earlier
305 WAITING is used by probe to mark irqs that are being tested
307 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING | IRQ_INPROGRESS);
308 status |= IRQ_PENDING; /* we _want_ to handle it */
311 * If the IRQ is disabled for whatever reason, we cannot
312 * use the action we have.
315 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
316 action = desc->action;
317 status &= ~IRQ_PENDING; /* we commit to handling */
318 status |= IRQ_INPROGRESS; /* we are handling it */
320 desc->status = status;
323 * If there is no IRQ handler or it was disabled, exit early.
324 Since we set PENDING, if another processor is handling
325 a different instance of this same irq, the other processor
326 will take care of it.
332 * Edge triggered interrupts need to remember
334 * This applies to any hw interrupts that allow a second
335 * instance of the same irq to arrive while we are in do_IRQ
336 * or in the handler. But the code here only handles the _second_
337 * instance of the irq, not the third or fourth. So it is mostly
338 * useful for irq hardware that does not mask cleanly in an
342 spin_unlock(&desc->lock);
343 handle_IRQ_event(irq, regs, action);
344 spin_lock(&desc->lock);
346 if (!(desc->status & IRQ_PENDING))
348 desc->status &= ~IRQ_PENDING;
350 desc->status &= ~IRQ_INPROGRESS;
353 * The ->end() handler has to deal with interrupts which got
354 * disabled while the handler was running.
357 desc->handler->end(irq);
358 spin_unlock(&desc->lock);
363 #ifdef CONFIG_PREEMPT
365 * We're done with the handlers, interrupts should be
366 * currently disabled; decrement preempt_count now so
367 * as we return preemption may be allowed...
369 preempt_enable_no_resched();
376 * request_irq - allocate an interrupt line
377 * @irq: Interrupt line to allocate
378 * @handler: Function to be called when the IRQ occurs
379 * @irqflags: Interrupt type flags
380 * @devname: An ascii name for the claiming device
381 * @dev_id: A cookie passed back to the handler function
383 * This call allocates interrupt resources and enables the
384 * interrupt line and IRQ handling. From the point this
385 * call is made your handler function may be invoked. Since
386 * your handler function must clear any interrupt the board
387 * raises, you must take care both to initialise your hardware
388 * and to set up the interrupt handler in the right order.
390 * Dev_id must be globally unique. Normally the address of the
391 * device data structure is used as the cookie. Since the handler
392 * receives this value it makes sense to use it.
394 * If your interrupt is shared you must pass a non NULL dev_id
395 * as this is required when freeing the interrupt.
399 * SA_SHIRQ Interrupt is shared
401 * SA_INTERRUPT Disable local interrupts while processing
403 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
406 int request_irq(unsigned int irq,
407 irqreturn_t (*handler)(int, void *, struct pt_regs *),
408 unsigned long irqflags,
409 const char * devname,
413 struct irqaction * action;
417 * Sanity-check: shared interrupts should REALLY pass in
418 * a real dev-ID, otherwise we'll have trouble later trying
419 * to figure out which interrupt is which (messes up the
420 * interrupt freeing logic etc).
422 if (irqflags & SA_SHIRQ) {
424 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
433 action = (struct irqaction *)
434 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
438 action->handler = handler;
439 action->flags = irqflags;
440 cpus_clear(action->mask);
441 action->name = devname;
443 action->dev_id = dev_id;
445 retval = setup_irq(irq, action);
452 * free_irq - free an interrupt
453 * @irq: Interrupt line to free
454 * @dev_id: Device identity to free
456 * Remove an interrupt handler. The handler is removed and if the
457 * interrupt line is no longer in use by any driver it is disabled.
458 * On a shared IRQ the caller must ensure the interrupt is disabled
459 * on the card it drives before calling this function. The function
460 * does not return until any executing interrupts for this IRQ
463 * This function may be called from interrupt context.
465 * Bugs: Attempting to free an irq in a handler for the same irq hangs
468 void free_irq(unsigned int irq, void *dev_id)
471 struct irqaction **p;
477 desc = irq_desc + irq;
478 spin_lock_irqsave(&desc->lock,flags);
481 struct irqaction * action = *p;
483 struct irqaction **pp = p;
485 if (action->dev_id != dev_id)
488 /* Found it - now remove it from the list of entries */
491 desc->status |= IRQ_DISABLED;
492 desc->handler->shutdown(irq);
494 spin_unlock_irqrestore(&desc->lock,flags);
498 printk("Trying to free free IRQ%d\n",irq);
499 spin_unlock_irqrestore(&desc->lock,flags);
505 * IRQ autodetection code..
507 * This depends on the fact that any interrupt that
508 * comes in on to an unassigned handler will get stuck
509 * with "IRQ_WAITING" cleared and the interrupt
514 * probe_irq_on - begin an interrupt autodetect
516 * Commence probing for an interrupt. The interrupts are scanned
517 * and a mask of potential interrupt lines is returned.
520 unsigned long probe_irq_on(void)
528 * something may have generated an irq long ago and we want to
529 * flush such a longstanding irq before considering it as spurious.
531 for (i = NR_IRQS-1; i >= 0; i--) {
534 spin_lock_irq(&desc->lock);
535 if (!irq_desc[i].action) {
536 irq_desc[i].handler->startup(i);
538 spin_unlock_irq(&desc->lock);
541 /* Wait for longstanding interrupts to trigger. */
542 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
543 /* about 20ms delay */ synchronize_irq();
546 * enable any unassigned irqs
547 * (we must startup again here because if a longstanding irq
548 * happened in the previous stage, it may have masked itself)
550 for (i = NR_IRQS-1; i >= 0; i--) {
553 spin_lock_irq(&desc->lock);
555 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
556 if (desc->handler->startup(i))
557 desc->status |= IRQ_PENDING;
559 spin_unlock_irq(&desc->lock);
563 * Wait for spurious interrupts to trigger
565 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
566 /* about 100ms delay */ synchronize_irq();
569 * Now filter out any obviously spurious interrupts
572 for (i = 0; i < NR_IRQS; i++) {
573 irq_desc_t *desc = irq_desc + i;
576 spin_lock_irq(&desc->lock);
577 status = desc->status;
579 if (status & IRQ_AUTODETECT) {
580 /* It triggered already - consider it spurious. */
581 if (!(status & IRQ_WAITING)) {
582 desc->status = status & ~IRQ_AUTODETECT;
583 desc->handler->shutdown(i);
588 spin_unlock_irq(&desc->lock);
595 * Return the one interrupt that triggered (this can
596 * handle any interrupt source).
600 * probe_irq_off - end an interrupt autodetect
601 * @val: mask of potential interrupts (unused)
603 * Scans the unused interrupt lines and returns the line which
604 * appears to have triggered the interrupt. If no interrupt was
605 * found then zero is returned. If more than one interrupt is
606 * found then minus the first candidate is returned to indicate
609 * The interrupt probe logic state is returned to its previous
612 * BUGS: When used in a module (which arguably shouldnt happen)
613 * nothing prevents two IRQ probe callers from overlapping. The
614 * results of this are non-optimal.
616 int probe_irq_off(unsigned long val)
618 int i, irq_found, nr_irqs;
622 for (i=0; i<NR_IRQS; i++) {
623 irq_desc_t *desc = irq_desc + i;
626 spin_lock_irq(&desc->lock);
627 status = desc->status;
628 if (!(status & IRQ_AUTODETECT))
631 if (status & IRQ_AUTODETECT) {
632 if (!(status & IRQ_WAITING)) {
638 desc->status = status & ~IRQ_AUTODETECT;
639 desc->handler->shutdown(i);
641 spin_unlock_irq(&desc->lock);
645 irq_found = -irq_found;
649 int setup_irq(unsigned int irq, struct irqaction * new)
653 struct irqaction *old, **p;
654 irq_desc_t *desc = irq_desc + irq;
657 * Some drivers like serial.c use request_irq() heavily,
658 * so we have to be careful not to interfere with a
661 if (new->flags & SA_SAMPLE_RANDOM) {
663 * This function might sleep, we want to call it first,
664 * outside of the atomic block.
665 * Yes, this might clear the entropy pool if the wrong
666 * driver is attempted to be loaded, without actually
667 * installing a new handler, but is this really a problem,
668 * only the sysadmin is able to do this.
670 rand_initialize_irq(irq);
674 * The following block of code has to be executed atomically
676 spin_lock_irqsave(&desc->lock,flags);
678 if ((old = *p) != NULL) {
679 /* Can't share interrupts unless both agree to */
680 if (!(old->flags & new->flags & SA_SHIRQ)) {
681 spin_unlock_irqrestore(&desc->lock,flags);
685 /* add new interrupt at end of irq queue */
697 desc->status &= ~IRQ_DISABLED;
698 desc->handler->startup(irq);
700 spin_unlock_irqrestore(&desc->lock,flags);
703 * No PROC FS support for interrupts.
704 * For improvements in this area please check
710 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
712 void init_irq_proc(void)
715 * No PROC FS support for interrupts.
716 * For improvements in this area please check