2 * arch/v850/kernel/irq.c -- High-level interrupt handling
4 * Copyright (C) 2001,02,03,04 NEC Electronics Corporation
5 * Copyright (C) 2001,02,03,04 Miles Bader <miles@gnu.org>
6 * Copyright (C) 1994-2000 Ralf Baechle
7 * Copyright (C) 1992 Linus Torvalds
9 * This file is subject to the terms and conditions of the GNU General
10 * Public License. See the file COPYING in the main directory of this
11 * archive for more details.
13 * This file was was derived from the mips version, arch/mips/kernel/irq.c
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/irq.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/slab.h>
24 #include <linux/random.h>
25 #include <linux/seq_file.h>
27 #include <asm/system.h>
30 * Controller mappings for all interrupt sources:
32 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
34 .handler = &no_irq_type,
35 .lock = SPIN_LOCK_UNLOCKED
40 * Special irq handlers.
43 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
49 * Generic no controller code
52 static void enable_none(unsigned int irq) { }
53 static unsigned int startup_none(unsigned int irq) { return 0; }
54 static void disable_none(unsigned int irq) { }
55 static void ack_none(unsigned int irq)
58 * 'what should we do if we get a hw irq event on an illegal vector'.
59 * each architecture has to answer this themselves, it doesn't deserve
60 * a generic callback i think.
62 printk("received IRQ %d with unknown interrupt type\n", irq);
65 /* startup is the same as "enable", shutdown is same as "disable" */
66 #define shutdown_none disable_none
67 #define end_none enable_none
69 struct hw_interrupt_type no_irq_type = {
71 .startup = startup_none,
72 .shutdown = shutdown_none,
73 .enable = enable_none,
74 .disable = disable_none,
79 volatile unsigned long irq_err_count, spurious_count;
82 * Generic, controller-independent functions:
85 int show_interrupts(struct seq_file *p, void *v)
87 int i = *(loff_t *) v;
88 struct irqaction * action;
93 for (i=0; i < 1 /*smp_num_cpus*/; i++)
94 seq_printf(p, "CPU%d ", i);
100 const char *type_name = irq_desc[i].handler->typename;
101 spin_lock_irqsave(&irq_desc[j].lock, flags);
102 action = irq_desc[i].action;
108 for (j = 0; j < NR_IRQS; j++)
109 if (irq_desc[j].handler->typename == type_name) {
115 seq_printf(p, "%3d: ",i);
116 seq_printf(p, "%10u ", kstat_irqs(i));
118 int prec = (num >= 100 ? 3 : num >= 10 ? 2 : 1);
119 seq_printf(p, " %*s%d", 14 - prec, type_name, num);
121 seq_printf(p, " %14s", type_name);
123 seq_printf(p, " %s", action->name);
124 for (action=action->next; action; action = action->next)
125 seq_printf(p, ", %s", action->name);
128 spin_unlock_irqrestore(&irq_desc[j].lock, flags);
129 } else if (i == NR_IRQS)
130 seq_printf(p, "ERR: %10lu\n", irq_err_count);
135 * This should really return information about whether
136 * we should do bottom half handling etc. Right now we
137 * end up _always_ checking the bottom half, which is a
138 * waste of time and is not what some drivers would
141 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
143 int status = 1; /* Force the "do bottom halves" bit */
146 if (!(action->flags & SA_INTERRUPT))
150 ret = action->handler(irq, action->dev_id, regs);
151 if (ret == IRQ_HANDLED)
152 status |= action->flags;
153 action = action->next;
155 if (status & SA_SAMPLE_RANDOM)
156 add_interrupt_randomness(irq);
163 * Generic enable/disable code: this just calls
164 * down into the PIC-specific version for the actual
165 * hardware disable after having gotten the irq
170 * disable_irq_nosync - disable an irq without waiting
171 * @irq: Interrupt to disable
173 * Disable the selected interrupt line. Disables of an interrupt
174 * stack. Unlike disable_irq(), this function does not ensure existing
175 * instances of the IRQ handler have completed before returning.
177 * This function may be called from IRQ context.
180 void inline disable_irq_nosync(unsigned int irq)
182 irq_desc_t *desc = irq_desc + irq;
185 spin_lock_irqsave(&desc->lock, flags);
186 if (!desc->depth++) {
187 desc->status |= IRQ_DISABLED;
188 desc->handler->disable(irq);
190 spin_unlock_irqrestore(&desc->lock, flags);
194 * disable_irq - disable an irq and wait for completion
195 * @irq: Interrupt to disable
197 * Disable the selected interrupt line. Disables of an interrupt
198 * stack. That is for two disables you need two enables. This
199 * function waits for any pending IRQ handlers for this interrupt
200 * to complete before returning. If you use this function while
201 * holding a resource the IRQ handler may need you will deadlock.
203 * This function may be called - with care - from IRQ context.
206 void disable_irq(unsigned int irq)
208 disable_irq_nosync(irq);
209 synchronize_irq(irq);
213 * enable_irq - enable interrupt handling on an irq
214 * @irq: Interrupt to enable
216 * Re-enables the processing of interrupts on this IRQ line
217 * providing no disable_irq calls are now in effect.
219 * This function may be called from IRQ context.
222 void enable_irq(unsigned int irq)
224 irq_desc_t *desc = irq_desc + irq;
227 spin_lock_irqsave(&desc->lock, flags);
228 switch (desc->depth) {
230 unsigned int status = desc->status & ~IRQ_DISABLED;
231 desc->status = status;
232 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
233 desc->status = status | IRQ_REPLAY;
234 hw_resend_irq(desc->handler,irq);
236 desc->handler->enable(irq);
243 printk("enable_irq(%u) unbalanced from %p\n", irq,
244 __builtin_return_address(0));
246 spin_unlock_irqrestore(&desc->lock, flags);
249 /* Handle interrupt IRQ. REGS are the registers at the time of ther
251 unsigned int handle_irq (int irq, struct pt_regs *regs)
254 * We ack quickly, we don't want the irq controller
255 * thinking we're snobs just because some other CPU has
256 * disabled global interrupts (we have already done the
257 * INT_ACK cycles, it's too late to try to pretend to the
258 * controller that we aren't taking the interrupt).
260 * 0 return value means that this irq is already being
261 * handled by some other CPU. (or is disabled)
263 int cpu = smp_processor_id();
264 irq_desc_t *desc = irq_desc + irq;
265 struct irqaction * action;
269 kstat_cpu(cpu).irqs[irq]++;
270 spin_lock(&desc->lock);
271 desc->handler->ack(irq);
273 REPLAY is when Linux resends an IRQ that was dropped earlier
274 WAITING is used by probe to mark irqs that are being tested
276 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
277 status |= IRQ_PENDING; /* we _want_ to handle it */
280 * If the IRQ is disabled for whatever reason, we cannot
281 * use the action we have.
284 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
285 action = desc->action;
286 status &= ~IRQ_PENDING; /* we commit to handling */
287 status |= IRQ_INPROGRESS; /* we are handling it */
289 desc->status = status;
292 * If there is no IRQ handler or it was disabled, exit early.
293 Since we set PENDING, if another processor is handling
294 a different instance of this same irq, the other processor
295 will take care of it.
297 if (unlikely(!action))
301 * Edge triggered interrupts need to remember
303 * This applies to any hw interrupts that allow a second
304 * instance of the same irq to arrive while we are in handle_irq
305 * or in the handler. But the code here only handles the _second_
306 * instance of the irq, not the third or fourth. So it is mostly
307 * useful for irq hardware that does not mask cleanly in an
311 spin_unlock(&desc->lock);
312 handle_IRQ_event(irq, regs, action);
313 spin_lock(&desc->lock);
315 if (likely(!(desc->status & IRQ_PENDING)))
317 desc->status &= ~IRQ_PENDING;
319 desc->status &= ~IRQ_INPROGRESS;
323 * The ->end() handler has to deal with interrupts which got
324 * disabled while the handler was running.
326 desc->handler->end(irq);
327 spin_unlock(&desc->lock);
335 * request_irq - allocate an interrupt line
336 * @irq: Interrupt line to allocate
337 * @handler: Function to be called when the IRQ occurs
338 * @irqflags: Interrupt type flags
339 * @devname: An ascii name for the claiming device
340 * @dev_id: A cookie passed back to the handler function
342 * This call allocates interrupt resources and enables the
343 * interrupt line and IRQ handling. From the point this
344 * call is made your handler function may be invoked. Since
345 * your handler function must clear any interrupt the board
346 * raises, you must take care both to initialise your hardware
347 * and to set up the interrupt handler in the right order.
349 * Dev_id must be globally unique. Normally the address of the
350 * device data structure is used as the cookie. Since the handler
351 * receives this value it makes sense to use it.
353 * If your interrupt is shared you must pass a non NULL dev_id
354 * as this is required when freeing the interrupt.
358 * SA_SHIRQ Interrupt is shared
360 * SA_INTERRUPT Disable local interrupts while processing
362 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
366 int request_irq(unsigned int irq,
367 irqreturn_t (*handler)(int, void *, struct pt_regs *),
368 unsigned long irqflags,
369 const char * devname,
373 struct irqaction * action;
377 * Sanity-check: shared interrupts should REALLY pass in
378 * a real dev-ID, otherwise we'll have trouble later trying
379 * to figure out which interrupt is which (messes up the
380 * interrupt freeing logic etc).
382 if (irqflags & SA_SHIRQ) {
384 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
393 action = (struct irqaction *)
394 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
398 action->handler = handler;
399 action->flags = irqflags;
400 cpus_clear(action->mask);
401 action->name = devname;
403 action->dev_id = dev_id;
405 retval = setup_irq(irq, action);
411 EXPORT_SYMBOL(request_irq);
414 * free_irq - free an interrupt
415 * @irq: Interrupt line to free
416 * @dev_id: Device identity to free
418 * Remove an interrupt handler. The handler is removed and if the
419 * interrupt line is no longer in use by any driver it is disabled.
420 * On a shared IRQ the caller must ensure the interrupt is disabled
421 * on the card it drives before calling this function. The function
422 * does not return until any executing interrupts for this IRQ
425 * This function may be called from interrupt context.
427 * Bugs: Attempting to free an irq in a handler for the same irq hangs
431 void free_irq(unsigned int irq, void *dev_id)
434 struct irqaction **p;
440 desc = irq_desc + irq;
441 spin_lock_irqsave(&desc->lock,flags);
444 struct irqaction * action = *p;
446 struct irqaction **pp = p;
448 if (action->dev_id != dev_id)
451 /* Found it - now remove it from the list of entries */
454 desc->status |= IRQ_DISABLED;
455 desc->handler->shutdown(irq);
457 spin_unlock_irqrestore(&desc->lock,flags);
459 synchronize_irq(irq);
463 printk("Trying to free free IRQ%d\n",irq);
464 spin_unlock_irqrestore(&desc->lock,flags);
469 EXPORT_SYMBOL(free_irq);
472 * IRQ autodetection code..
474 * This depends on the fact that any interrupt that
475 * comes in on to an unassigned handler will get stuck
476 * with "IRQ_WAITING" cleared and the interrupt
480 static DECLARE_MUTEX(probe_sem);
483 * probe_irq_on - begin an interrupt autodetect
485 * Commence probing for an interrupt. The interrupts are scanned
486 * and a mask of potential interrupt lines is returned.
490 unsigned long probe_irq_on(void)
499 * something may have generated an irq long ago and we want to
500 * flush such a longstanding irq before considering it as spurious.
502 for (i = NR_IRQS-1; i > 0; i--) {
505 spin_lock_irq(&desc->lock);
506 if (!irq_desc[i].action)
507 irq_desc[i].handler->startup(i);
508 spin_unlock_irq(&desc->lock);
511 /* Wait for longstanding interrupts to trigger. */
512 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
513 /* about 20ms delay */ barrier();
516 * enable any unassigned irqs
517 * (we must startup again here because if a longstanding irq
518 * happened in the previous stage, it may have masked itself)
520 for (i = NR_IRQS-1; i > 0; i--) {
523 spin_lock_irq(&desc->lock);
525 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
526 if (desc->handler->startup(i))
527 desc->status |= IRQ_PENDING;
529 spin_unlock_irq(&desc->lock);
533 * Wait for spurious interrupts to trigger
535 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
536 /* about 100ms delay */ barrier();
539 * Now filter out any obviously spurious interrupts
542 for (i = 0; i < NR_IRQS; i++) {
543 irq_desc_t *desc = irq_desc + i;
546 spin_lock_irq(&desc->lock);
547 status = desc->status;
549 if (status & IRQ_AUTODETECT) {
550 /* It triggered already - consider it spurious. */
551 if (!(status & IRQ_WAITING)) {
552 desc->status = status & ~IRQ_AUTODETECT;
553 desc->handler->shutdown(i);
558 spin_unlock_irq(&desc->lock);
564 EXPORT_SYMBOL(probe_irq_on);
567 * Return a mask of triggered interrupts (this
568 * can handle only legacy ISA interrupts).
572 * probe_irq_mask - scan a bitmap of interrupt lines
573 * @val: mask of interrupts to consider
575 * Scan the ISA bus interrupt lines and return a bitmap of
576 * active interrupts. The interrupt probe logic state is then
577 * returned to its previous value.
579 * Note: we need to scan all the irq's even though we will
580 * only return ISA irq numbers - just so that we reset them
581 * all to a known state.
583 unsigned int probe_irq_mask(unsigned long val)
589 for (i = 0; i < NR_IRQS; i++) {
590 irq_desc_t *desc = irq_desc + i;
593 spin_lock_irq(&desc->lock);
594 status = desc->status;
596 if (status & IRQ_AUTODETECT) {
597 if (i < 16 && !(status & IRQ_WAITING))
600 desc->status = status & ~IRQ_AUTODETECT;
601 desc->handler->shutdown(i);
603 spin_unlock_irq(&desc->lock);
611 * Return the one interrupt that triggered (this can
612 * handle any interrupt source).
616 * probe_irq_off - end an interrupt autodetect
617 * @val: mask of potential interrupts (unused)
619 * Scans the unused interrupt lines and returns the line which
620 * appears to have triggered the interrupt. If no interrupt was
621 * found then zero is returned. If more than one interrupt is
622 * found then minus the first candidate is returned to indicate
625 * The interrupt probe logic state is returned to its previous
628 * BUGS: When used in a module (which arguably shouldnt happen)
629 * nothing prevents two IRQ probe callers from overlapping. The
630 * results of this are non-optimal.
633 int probe_irq_off(unsigned long val)
635 int i, irq_found, nr_irqs;
639 for (i = 0; i < NR_IRQS; i++) {
640 irq_desc_t *desc = irq_desc + i;
643 spin_lock_irq(&desc->lock);
644 status = desc->status;
646 if (status & IRQ_AUTODETECT) {
647 if (!(status & IRQ_WAITING)) {
652 desc->status = status & ~IRQ_AUTODETECT;
653 desc->handler->shutdown(i);
655 spin_unlock_irq(&desc->lock);
660 irq_found = -irq_found;
664 EXPORT_SYMBOL(probe_irq_off);
666 /* this was setup_x86_irq but it seems pretty generic */
667 int setup_irq(unsigned int irq, struct irqaction * new)
671 struct irqaction *old, **p;
672 irq_desc_t *desc = irq_desc + irq;
675 * Some drivers like serial.c use request_irq() heavily,
676 * so we have to be careful not to interfere with a
679 if (new->flags & SA_SAMPLE_RANDOM) {
681 * This function might sleep, we want to call it first,
682 * outside of the atomic block.
683 * Yes, this might clear the entropy pool if the wrong
684 * driver is attempted to be loaded, without actually
685 * installing a new handler, but is this really a problem,
686 * only the sysadmin is able to do this.
688 rand_initialize_irq(irq);
692 * The following block of code has to be executed atomically
694 spin_lock_irqsave(&desc->lock,flags);
696 if ((old = *p) != NULL) {
697 /* Can't share interrupts unless both agree to */
698 if (!(old->flags & new->flags & SA_SHIRQ)) {
699 spin_unlock_irqrestore(&desc->lock,flags);
703 /* add new interrupt at end of irq queue */
715 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
716 desc->handler->startup(irq);
718 spin_unlock_irqrestore(&desc->lock,flags);
720 /* register_irq_proc(irq); */
724 /* Initialize irq handling for IRQs.
725 BASE_IRQ, BASE_IRQ+INTERVAL, ..., BASE_IRQ+NUM*INTERVAL
726 to IRQ_TYPE. An IRQ_TYPE of 0 means to use a generic interrupt type. */
728 init_irq_handlers (int base_irq, int num, int interval,
729 struct hw_interrupt_type *irq_type)
732 irq_desc[base_irq].status = IRQ_DISABLED;
733 irq_desc[base_irq].action = NULL;
734 irq_desc[base_irq].depth = 1;
735 irq_desc[base_irq].handler = irq_type;
736 base_irq += interval;
740 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
741 void init_irq_proc(void)
744 #endif /* CONFIG_PROC_FS && CONFIG_SYSCTL */