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
13 * IRQs are in fact implemented a bit like signal handlers for the kernel.
14 * Naturally it's not a 1:1 relation, but there are similarities.
17 #include <linux/config.h>
18 #include <linux/ptrace.h>
19 #include <linux/errno.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/ioport.h>
24 #include <linux/interrupt.h>
25 #include <linux/timex.h>
26 #include <linux/slab.h>
27 #include <linux/random.h>
28 #include <linux/smp.h>
29 #include <linux/smp_lock.h>
30 #include <linux/init.h>
32 #include <asm/system.h>
34 #include <asm/bitops.h>
36 #include <asm/pgalloc.h>
37 #include <asm/delay.h>
39 #include <linux/irq.h>
42 * Controller mappings for all interrupt sources:
44 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned =
45 { [0 ... NR_IRQS-1] = { 0, &no_irq_type, NULL, 0, SPIN_LOCK_UNLOCKED}};
48 * Special irq handlers.
51 void no_action(int cpl, void *dev_id, struct pt_regs *regs) { }
54 * Generic no controller code
57 static void enable_none(unsigned int irq) { }
58 static unsigned int startup_none(unsigned int irq) { return 0; }
59 static void disable_none(unsigned int irq) { }
60 static void ack_none(unsigned int irq)
63 * 'what should we do if we get a hw irq event on an illegal vector'.
64 * each architecture has to answer this themselves, it doesnt deserve
65 * a generic callback i think.
67 printk("unexpected IRQ trap at irq %02x\n", irq);
70 /* startup is the same as "enable", shutdown is same as "disable" */
71 #define shutdown_none disable_none
72 #define end_none enable_none
74 struct hw_interrupt_type no_irq_type = {
86 * do_NMI handles all Non-Maskable Interrupts.
88 asmlinkage void do_NMI(unsigned long vector_num, struct pt_regs * regs)
90 if (regs->sr & 0x40000000)
91 printk("unexpected NMI trap in system mode\n");
93 printk("unexpected NMI trap in user mode\n");
100 * Generic, controller-independent functions:
102 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
103 int get_irq_list(char *buf)
106 struct irqaction * action;
109 p += sprintf(p, " ");
110 for (j=0; j<smp_num_cpus; j++)
111 p += sprintf(p, "CPU%d ",j);
114 for (i = 0 ; i < NR_IRQS ; i++) {
115 action = irq_desc[i].action;
118 p += sprintf(p, "%3d: ",i);
119 p += sprintf(p, "%10u ", kstat_irqs(i));
120 p += sprintf(p, " %14s", irq_desc[i].handler->typename);
121 p += irq_describe(p, i);
122 p += sprintf(p, " %s", action->name);
124 for (action=action->next; action; action = action->next)
125 p += sprintf(p, ", %s", action->name);
129 p += sprintf(p, "NMI: ");
130 for (j = 0; j < smp_num_cpus; j++)
131 p += sprintf(p, "%10u ",
132 atomic_read(&nmi_counter(cpu_logical_map(j))));
133 p += sprintf(p, "\n");
141 * This should really return information about whether
142 * we should do bottom half handling etc. Right now we
143 * end up _always_ checking the bottom half, which is a
144 * waste of time and is not what some drivers would
147 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
150 int cpu = smp_processor_id();
154 status = 1; /* Force the "do bottom halves" bit */
156 if (!(action->flags & SA_INTERRUPT))
160 status |= action->flags;
161 action->handler(irq, action->dev_id, regs);
162 action = action->next;
164 if (status & SA_SAMPLE_RANDOM)
165 add_interrupt_randomness(irq);
175 * Generic enable/disable code: this just calls
176 * down into the PIC-specific version for the actual
177 * hardware disable after having gotten the irq
182 * disable_irq_nosync - disable an irq without waiting
183 * @irq: Interrupt to disable
185 * Disable the selected interrupt line. Disables of an interrupt
186 * stack. Unlike disable_irq(), this function does not ensure existing
187 * instances of the IRQ handler have completed before returning.
189 * This function may be called from IRQ context.
191 void disable_irq_nosync(unsigned int irq)
193 irq_desc_t *desc = irq_desc + irq;
196 spin_lock_irqsave(&desc->lock, flags);
197 if (!desc->depth++) {
198 desc->status |= IRQ_DISABLED;
199 desc->handler->disable(irq);
201 spin_unlock_irqrestore(&desc->lock, flags);
205 * disable_irq - disable an irq and wait for completion
206 * @irq: Interrupt to disable
208 * Disable the selected interrupt line. Disables of an interrupt
209 * stack. That is for two disables you need two enables. This
210 * function waits for any pending IRQ handlers for this interrupt
211 * to complete before returning. If you use this function while
212 * holding a resource the IRQ handler may need you will deadlock.
214 * This function may be called - with care - from IRQ context.
216 void disable_irq(unsigned int irq)
218 disable_irq_nosync(irq);
220 if (!local_irq_count(smp_processor_id())) {
223 } while (irq_desc[irq].status & IRQ_INPROGRESS);
228 * enable_irq - enable interrupt handling on an irq
229 * @irq: Interrupt to enable
231 * Re-enables the processing of interrupts on this IRQ line
232 * providing no disable_irq calls are now in effect.
234 * This function may be called from IRQ context.
236 void enable_irq(unsigned int irq)
238 irq_desc_t *desc = irq_desc + irq;
241 spin_lock_irqsave(&desc->lock, flags);
242 switch (desc->depth) {
244 unsigned int status = desc->status & ~IRQ_DISABLED;
245 desc->status = status;
246 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
247 desc->status = status | IRQ_REPLAY;
248 hw_resend_irq(desc->handler,irq);
250 desc->handler->enable(irq);
257 printk("enable_irq() unbalanced from %p\n",
258 __builtin_return_address(0));
260 spin_unlock_irqrestore(&desc->lock, flags);
264 * do_IRQ handles all normal device IRQ's.
266 asmlinkage int do_IRQ(unsigned long vector_num, struct pt_regs * regs)
269 * We ack quickly, we don't want the irq controller
270 * thinking we're snobs just because some other CPU has
271 * disabled global interrupts (we have already done the
272 * INT_ACK cycles, it's too late to try to pretend to the
273 * controller that we aren't taking the interrupt).
275 * 0 return value means that this irq is already being
276 * handled by some other CPU. (or is disabled)
279 int cpu = smp_processor_id();
281 struct irqaction * action;
284 irq = irq_demux(vector_num);
287 * Should never happen, if it does check
288 * vectorN_to_IRQ[] against trap_jtable[].
291 printk("unexpected IRQ trap at vector %03lx\n", vector_num);
295 desc = irq_desc + irq;
297 kstat.irqs[cpu][irq]++;
298 spin_lock(&desc->lock);
299 desc->handler->ack(irq);
301 REPLAY is when Linux resends an IRQ that was dropped earlier
302 WAITING is used by probe to mark irqs that are being tested
304 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING | IRQ_INPROGRESS);
305 status |= IRQ_PENDING; /* we _want_ to handle it */
308 * If the IRQ is disabled for whatever reason, we cannot
309 * use the action we have.
312 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
313 action = desc->action;
314 status &= ~IRQ_PENDING; /* we commit to handling */
315 status |= IRQ_INPROGRESS; /* we are handling it */
317 desc->status = status;
320 * If there is no IRQ handler or it was disabled, exit early.
321 Since we set PENDING, if another processor is handling
322 a different instance of this same irq, the other processor
323 will take care of it.
329 * Edge triggered interrupts need to remember
331 * This applies to any hw interrupts that allow a second
332 * instance of the same irq to arrive while we are in do_IRQ
333 * or in the handler. But the code here only handles the _second_
334 * instance of the irq, not the third or fourth. So it is mostly
335 * useful for irq hardware that does not mask cleanly in an
339 spin_unlock(&desc->lock);
340 handle_IRQ_event(irq, regs, action);
341 spin_lock(&desc->lock);
343 if (!(desc->status & IRQ_PENDING))
345 desc->status &= ~IRQ_PENDING;
347 desc->status &= ~IRQ_INPROGRESS;
350 * The ->end() handler has to deal with interrupts which got
351 * disabled while the handler was running.
353 desc->handler->end(irq);
354 spin_unlock(&desc->lock);
356 if (softirq_pending(cpu))
362 * request_irq - allocate an interrupt line
363 * @irq: Interrupt line to allocate
364 * @handler: Function to be called when the IRQ occurs
365 * @irqflags: Interrupt type flags
366 * @devname: An ascii name for the claiming device
367 * @dev_id: A cookie passed back to the handler function
369 * This call allocates interrupt resources and enables the
370 * interrupt line and IRQ handling. From the point this
371 * call is made your handler function may be invoked. Since
372 * your handler function must clear any interrupt the board
373 * raises, you must take care both to initialise your hardware
374 * and to set up the interrupt handler in the right order.
376 * Dev_id must be globally unique. Normally the address of the
377 * device data structure is used as the cookie. Since the handler
378 * receives this value it makes sense to use it.
380 * If your interrupt is shared you must pass a non NULL dev_id
381 * as this is required when freeing the interrupt.
385 * SA_SHIRQ Interrupt is shared
387 * SA_INTERRUPT Disable local interrupts while processing
389 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
392 int request_irq(unsigned int irq,
393 void (*handler)(int, void *, struct pt_regs *),
394 unsigned long irqflags,
395 const char * devname,
399 struct irqaction * action;
403 * Sanity-check: shared interrupts should REALLY pass in
404 * a real dev-ID, otherwise we'll have trouble later trying
405 * to figure out which interrupt is which (messes up the
406 * interrupt freeing logic etc).
408 if (irqflags & SA_SHIRQ) {
410 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
419 action = (struct irqaction *)
420 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
424 action->handler = handler;
425 action->flags = irqflags;
427 action->name = devname;
429 action->dev_id = dev_id;
431 retval = setup_irq(irq, action);
438 * free_irq - free an interrupt
439 * @irq: Interrupt line to free
440 * @dev_id: Device identity to free
442 * Remove an interrupt handler. The handler is removed and if the
443 * interrupt line is no longer in use by any driver it is disabled.
444 * On a shared IRQ the caller must ensure the interrupt is disabled
445 * on the card it drives before calling this function. The function
446 * does not return until any executing interrupts for this IRQ
449 * This function may be called from interrupt context.
451 * Bugs: Attempting to free an irq in a handler for the same irq hangs
454 void free_irq(unsigned int irq, void *dev_id)
457 struct irqaction **p;
463 desc = irq_desc + irq;
464 spin_lock_irqsave(&desc->lock,flags);
467 struct irqaction * action = *p;
469 struct irqaction **pp = p;
471 if (action->dev_id != dev_id)
474 /* Found it - now remove it from the list of entries */
477 desc->status |= IRQ_DISABLED;
478 desc->handler->shutdown(irq);
480 spin_unlock_irqrestore(&desc->lock,flags);
484 printk("Trying to free free IRQ%d\n",irq);
485 spin_unlock_irqrestore(&desc->lock,flags);
491 * IRQ autodetection code..
493 * This depends on the fact that any interrupt that
494 * comes in on to an unassigned handler will get stuck
495 * with "IRQ_WAITING" cleared and the interrupt
500 * probe_irq_on - begin an interrupt autodetect
502 * Commence probing for an interrupt. The interrupts are scanned
503 * and a mask of potential interrupt lines is returned.
506 unsigned long probe_irq_on(void)
514 * something may have generated an irq long ago and we want to
515 * flush such a longstanding irq before considering it as spurious.
517 for (i = NR_IRQS-1; i >= 0; i--) {
520 spin_lock_irq(&desc->lock);
521 if (!irq_desc[i].action) {
522 irq_desc[i].handler->startup(i);
524 spin_unlock_irq(&desc->lock);
527 /* Wait for longstanding interrupts to trigger. */
528 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
529 /* about 20ms delay */ synchronize_irq();
532 * enable any unassigned irqs
533 * (we must startup again here because if a longstanding irq
534 * happened in the previous stage, it may have masked itself)
536 for (i = NR_IRQS-1; i >= 0; i--) {
539 spin_lock_irq(&desc->lock);
541 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
542 if (desc->handler->startup(i))
543 desc->status |= IRQ_PENDING;
545 spin_unlock_irq(&desc->lock);
549 * Wait for spurious interrupts to trigger
551 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
552 /* about 100ms delay */ synchronize_irq();
555 * Now filter out any obviously spurious interrupts
558 for (i = 0; i < NR_IRQS; i++) {
559 irq_desc_t *desc = irq_desc + i;
562 spin_lock_irq(&desc->lock);
563 status = desc->status;
565 if (status & IRQ_AUTODETECT) {
566 /* It triggered already - consider it spurious. */
567 if (!(status & IRQ_WAITING)) {
568 desc->status = status & ~IRQ_AUTODETECT;
569 desc->handler->shutdown(i);
574 spin_unlock_irq(&desc->lock);
581 * Return the one interrupt that triggered (this can
582 * handle any interrupt source).
586 * probe_irq_off - end an interrupt autodetect
587 * @val: mask of potential interrupts (unused)
589 * Scans the unused interrupt lines and returns the line which
590 * appears to have triggered the interrupt. If no interrupt was
591 * found then zero is returned. If more than one interrupt is
592 * found then minus the first candidate is returned to indicate
595 * The interrupt probe logic state is returned to its previous
598 * BUGS: When used in a module (which arguably shouldnt happen)
599 * nothing prevents two IRQ probe callers from overlapping. The
600 * results of this are non-optimal.
602 int probe_irq_off(unsigned long val)
604 int i, irq_found, nr_irqs;
608 for (i=0; i<NR_IRQS; i++) {
609 irq_desc_t *desc = irq_desc + i;
612 spin_lock_irq(&desc->lock);
613 status = desc->status;
614 if (!(status & IRQ_AUTODETECT))
617 if (status & IRQ_AUTODETECT) {
618 if (!(status & IRQ_WAITING)) {
624 desc->status = status & ~IRQ_AUTODETECT;
625 desc->handler->shutdown(i);
627 spin_unlock_irq(&desc->lock);
631 irq_found = -irq_found;
635 int setup_irq(unsigned int irq, struct irqaction * new)
639 struct irqaction *old, **p;
640 irq_desc_t *desc = irq_desc + irq;
643 * Some drivers like serial.c use request_irq() heavily,
644 * so we have to be careful not to interfere with a
647 if (new->flags & SA_SAMPLE_RANDOM) {
649 * This function might sleep, we want to call it first,
650 * outside of the atomic block.
651 * Yes, this might clear the entropy pool if the wrong
652 * driver is attempted to be loaded, without actually
653 * installing a new handler, but is this really a problem,
654 * only the sysadmin is able to do this.
656 rand_initialize_irq(irq);
660 * The following block of code has to be executed atomically
662 spin_lock_irqsave(&desc->lock,flags);
664 if ((old = *p) != NULL) {
665 /* Can't share interrupts unless both agree to */
666 if (!(old->flags & new->flags & SA_SHIRQ)) {
667 spin_unlock_irqrestore(&desc->lock,flags);
671 /* add new interrupt at end of irq queue */
683 desc->status &= ~IRQ_DISABLED;
684 desc->handler->startup(irq);
686 spin_unlock_irqrestore(&desc->lock,flags);
689 * No PROC FS support for interrupts.
690 * For improvements in this area please check
696 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
698 void init_irq_proc(void)
701 * No PROC FS support for interrupts.
702 * For improvements in this area please check