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 <linux/bitops.h>
34 #include <asm/system.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)
153 status = 1; /* Force the "do bottom halves" bit */
155 if (!(action->flags & SA_INTERRUPT))
159 ret = action->handler(irq, action->dev_id, regs);
160 if (ret == IRQ_HANDLED)
161 status |= action->flags;
162 action = action->next;
164 if (status & SA_SAMPLE_RANDOM)
165 add_interrupt_randomness(irq);
173 * Generic enable/disable code: this just calls
174 * down into the PIC-specific version for the actual
175 * hardware disable after having gotten the irq
180 * disable_irq_nosync - disable an irq without waiting
181 * @irq: Interrupt to disable
183 * Disable the selected interrupt line. Disables of an interrupt
184 * stack. Unlike disable_irq(), this function does not ensure existing
185 * instances of the IRQ handler have completed before returning.
187 * This function may be called from IRQ context.
189 void disable_irq_nosync(unsigned int irq)
191 irq_desc_t *desc = irq_desc + irq;
194 spin_lock_irqsave(&desc->lock, flags);
195 if (!desc->depth++) {
196 desc->status |= IRQ_DISABLED;
197 desc->handler->disable(irq);
199 spin_unlock_irqrestore(&desc->lock, flags);
203 * disable_irq - disable an irq and wait for completion
204 * @irq: Interrupt to disable
206 * Disable the selected interrupt line. Disables of an interrupt
207 * stack. That is for two disables you need two enables. This
208 * function waits for any pending IRQ handlers for this interrupt
209 * to complete before returning. If you use this function while
210 * holding a resource the IRQ handler may need you will deadlock.
212 * This function may be called - with care - from IRQ context.
214 void disable_irq(unsigned int irq)
216 disable_irq_nosync(irq);
217 synchronize_irq(irq);
221 * enable_irq - enable interrupt handling on an irq
222 * @irq: Interrupt to enable
224 * Re-enables the processing of interrupts on this IRQ line
225 * providing no disable_irq calls are now in effect.
227 * This function may be called from IRQ context.
229 void enable_irq(unsigned int irq)
231 irq_desc_t *desc = irq_desc + irq;
234 spin_lock_irqsave(&desc->lock, flags);
235 switch (desc->depth) {
237 unsigned int status = desc->status & ~IRQ_DISABLED;
238 desc->status = status;
239 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
240 desc->status = status | IRQ_REPLAY;
241 hw_resend_irq(desc->handler,irq);
243 desc->handler->enable(irq);
250 printk("enable_irq() unbalanced from %p\n",
251 __builtin_return_address(0));
253 spin_unlock_irqrestore(&desc->lock, flags);
257 * do_IRQ handles all normal device IRQ's.
259 asmlinkage int do_IRQ(unsigned long vector_num, struct pt_regs * regs)
262 * We ack quickly, we don't want the irq controller
263 * thinking we're snobs just because some other CPU has
264 * disabled global interrupts (we have already done the
265 * INT_ACK cycles, it's too late to try to pretend to the
266 * controller that we aren't taking the interrupt).
268 * 0 return value means that this irq is already being
269 * handled by some other CPU. (or is disabled)
272 int cpu = smp_processor_id();
273 irq_desc_t *desc = NULL;
274 struct irqaction * action;
279 #ifdef CONFIG_PREEMPT
281 * At this point we're now about to actually call handlers,
282 * and interrupts might get reenabled during them... bump
283 * preempt_count to prevent any preemption while the handler
284 * called here is pending...
289 irq = irq_demux(vector_num);
292 * Should never happen, if it does check
293 * vectorN_to_IRQ[] against trap_jtable[].
296 printk("unexpected IRQ trap at vector %03lx\n", vector_num);
300 desc = irq_desc + irq;
302 kstat_cpu(cpu).irqs[irq]++;
303 spin_lock(&desc->lock);
304 desc->handler->ack(irq);
306 REPLAY is when Linux resends an IRQ that was dropped earlier
307 WAITING is used by probe to mark irqs that are being tested
309 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING | IRQ_INPROGRESS);
310 status |= IRQ_PENDING; /* we _want_ to handle it */
313 * If the IRQ is disabled for whatever reason, we cannot
314 * use the action we have.
317 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
318 action = desc->action;
319 status &= ~IRQ_PENDING; /* we commit to handling */
320 status |= IRQ_INPROGRESS; /* we are handling it */
322 desc->status = status;
325 * If there is no IRQ handler or it was disabled, exit early.
326 Since we set PENDING, if another processor is handling
327 a different instance of this same irq, the other processor
328 will take care of it.
334 * Edge triggered interrupts need to remember
336 * This applies to any hw interrupts that allow a second
337 * instance of the same irq to arrive while we are in do_IRQ
338 * or in the handler. But the code here only handles the _second_
339 * instance of the irq, not the third or fourth. So it is mostly
340 * useful for irq hardware that does not mask cleanly in an
344 spin_unlock(&desc->lock);
345 handle_IRQ_event(irq, regs, action);
346 spin_lock(&desc->lock);
348 if (!(desc->status & IRQ_PENDING))
350 desc->status &= ~IRQ_PENDING;
352 desc->status &= ~IRQ_INPROGRESS;
355 * The ->end() handler has to deal with interrupts which got
356 * disabled while the handler was running.
359 desc->handler->end(irq);
360 spin_unlock(&desc->lock);
365 #ifdef CONFIG_PREEMPT
367 * We're done with the handlers, interrupts should be
368 * currently disabled; decrement preempt_count now so
369 * as we return preemption may be allowed...
371 preempt_enable_no_resched();
378 * request_irq - allocate an interrupt line
379 * @irq: Interrupt line to allocate
380 * @handler: Function to be called when the IRQ occurs
381 * @irqflags: Interrupt type flags
382 * @devname: An ascii name for the claiming device
383 * @dev_id: A cookie passed back to the handler function
385 * This call allocates interrupt resources and enables the
386 * interrupt line and IRQ handling. From the point this
387 * call is made your handler function may be invoked. Since
388 * your handler function must clear any interrupt the board
389 * raises, you must take care both to initialise your hardware
390 * and to set up the interrupt handler in the right order.
392 * Dev_id must be globally unique. Normally the address of the
393 * device data structure is used as the cookie. Since the handler
394 * receives this value it makes sense to use it.
396 * If your interrupt is shared you must pass a non NULL dev_id
397 * as this is required when freeing the interrupt.
401 * SA_SHIRQ Interrupt is shared
403 * SA_INTERRUPT Disable local interrupts while processing
405 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
408 int request_irq(unsigned int irq,
409 irqreturn_t (*handler)(int, void *, struct pt_regs *),
410 unsigned long irqflags,
411 const char * devname,
415 struct irqaction * action;
419 * Sanity-check: shared interrupts should REALLY pass in
420 * a real dev-ID, otherwise we'll have trouble later trying
421 * to figure out which interrupt is which (messes up the
422 * interrupt freeing logic etc).
424 if (irqflags & SA_SHIRQ) {
426 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
435 action = (struct irqaction *)
436 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
440 action->handler = handler;
441 action->flags = irqflags;
442 cpus_clear(action->mask);
443 action->name = devname;
445 action->dev_id = dev_id;
447 retval = setup_irq(irq, action);
454 * free_irq - free an interrupt
455 * @irq: Interrupt line to free
456 * @dev_id: Device identity to free
458 * Remove an interrupt handler. The handler is removed and if the
459 * interrupt line is no longer in use by any driver it is disabled.
460 * On a shared IRQ the caller must ensure the interrupt is disabled
461 * on the card it drives before calling this function. The function
462 * does not return until any executing interrupts for this IRQ
465 * This function may be called from interrupt context.
467 * Bugs: Attempting to free an irq in a handler for the same irq hangs
470 void free_irq(unsigned int irq, void *dev_id)
473 struct irqaction **p;
479 desc = irq_desc + irq;
480 spin_lock_irqsave(&desc->lock,flags);
483 struct irqaction * action = *p;
485 struct irqaction **pp = p;
487 if (action->dev_id != dev_id)
490 /* Found it - now remove it from the list of entries */
493 desc->status |= IRQ_DISABLED;
494 desc->handler->shutdown(irq);
496 spin_unlock_irqrestore(&desc->lock,flags);
500 printk("Trying to free free IRQ%d\n",irq);
501 spin_unlock_irqrestore(&desc->lock,flags);
507 * IRQ autodetection code..
509 * This depends on the fact that any interrupt that
510 * comes in on to an unassigned handler will get stuck
511 * with "IRQ_WAITING" cleared and the interrupt
516 * probe_irq_on - begin an interrupt autodetect
518 * Commence probing for an interrupt. The interrupts are scanned
519 * and a mask of potential interrupt lines is returned.
522 unsigned long probe_irq_on(void)
530 * something may have generated an irq long ago and we want to
531 * flush such a longstanding irq before considering it as spurious.
533 for (i = NR_IRQS-1; i >= 0; i--) {
536 spin_lock_irq(&desc->lock);
537 if (!irq_desc[i].action) {
538 irq_desc[i].handler->startup(i);
540 spin_unlock_irq(&desc->lock);
543 /* Wait for longstanding interrupts to trigger. */
544 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
545 /* about 20ms delay */ synchronize_irq();
548 * enable any unassigned irqs
549 * (we must startup again here because if a longstanding irq
550 * happened in the previous stage, it may have masked itself)
552 for (i = NR_IRQS-1; i >= 0; i--) {
555 spin_lock_irq(&desc->lock);
557 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
558 if (desc->handler->startup(i))
559 desc->status |= IRQ_PENDING;
561 spin_unlock_irq(&desc->lock);
565 * Wait for spurious interrupts to trigger
567 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
568 /* about 100ms delay */ synchronize_irq();
571 * Now filter out any obviously spurious interrupts
574 for (i = 0; i < NR_IRQS; i++) {
575 irq_desc_t *desc = irq_desc + i;
578 spin_lock_irq(&desc->lock);
579 status = desc->status;
581 if (status & IRQ_AUTODETECT) {
582 /* It triggered already - consider it spurious. */
583 if (!(status & IRQ_WAITING)) {
584 desc->status = status & ~IRQ_AUTODETECT;
585 desc->handler->shutdown(i);
590 spin_unlock_irq(&desc->lock);
597 * Return the one interrupt that triggered (this can
598 * handle any interrupt source).
602 * probe_irq_off - end an interrupt autodetect
603 * @val: mask of potential interrupts (unused)
605 * Scans the unused interrupt lines and returns the line which
606 * appears to have triggered the interrupt. If no interrupt was
607 * found then zero is returned. If more than one interrupt is
608 * found then minus the first candidate is returned to indicate
611 * The interrupt probe logic state is returned to its previous
614 * BUGS: When used in a module (which arguably shouldnt happen)
615 * nothing prevents two IRQ probe callers from overlapping. The
616 * results of this are non-optimal.
618 int probe_irq_off(unsigned long val)
620 int i, irq_found, nr_irqs;
624 for (i=0; i<NR_IRQS; i++) {
625 irq_desc_t *desc = irq_desc + i;
628 spin_lock_irq(&desc->lock);
629 status = desc->status;
630 if (!(status & IRQ_AUTODETECT))
633 if (status & IRQ_AUTODETECT) {
634 if (!(status & IRQ_WAITING)) {
640 desc->status = status & ~IRQ_AUTODETECT;
641 desc->handler->shutdown(i);
643 spin_unlock_irq(&desc->lock);
647 irq_found = -irq_found;
651 int setup_irq(unsigned int irq, struct irqaction * new)
655 struct irqaction *old, **p;
656 irq_desc_t *desc = irq_desc + irq;
659 * Some drivers like serial.c use request_irq() heavily,
660 * so we have to be careful not to interfere with a
663 if (new->flags & SA_SAMPLE_RANDOM) {
665 * This function might sleep, we want to call it first,
666 * outside of the atomic block.
667 * Yes, this might clear the entropy pool if the wrong
668 * driver is attempted to be loaded, without actually
669 * installing a new handler, but is this really a problem,
670 * only the sysadmin is able to do this.
672 rand_initialize_irq(irq);
676 * The following block of code has to be executed atomically
678 spin_lock_irqsave(&desc->lock,flags);
680 if ((old = *p) != NULL) {
681 /* Can't share interrupts unless both agree to */
682 if (!(old->flags & new->flags & SA_SHIRQ)) {
683 spin_unlock_irqrestore(&desc->lock,flags);
687 /* add new interrupt at end of irq queue */
699 desc->status &= ~IRQ_DISABLED;
700 desc->handler->startup(irq);
702 spin_unlock_irqrestore(&desc->lock,flags);
705 * No PROC FS support for interrupts.
706 * For improvements in this area please check
712 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
714 void init_irq_proc(void)
717 * No PROC FS support for interrupts.
718 * For improvements in this area please check