2 * arch/v850/kernel/irq.c -- High-level interrupt handling
4 * Copyright (C) 2001,02,03 NEC Electronics Corporation
5 * Copyright (C) 2001,02,03 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) { }
46 * Generic no controller code
49 static void enable_none(unsigned int irq) { }
50 static unsigned int startup_none(unsigned int irq) { return 0; }
51 static void disable_none(unsigned int irq) { }
52 static void ack_none(unsigned int irq)
55 * 'what should we do if we get a hw irq event on an illegal vector'.
56 * each architecture has to answer this themselves, it doesn't deserve
57 * a generic callback i think.
59 printk("received IRQ %d with unknown interrupt type\n", irq);
62 /* startup is the same as "enable", shutdown is same as "disable" */
63 #define shutdown_none disable_none
64 #define end_none enable_none
66 struct hw_interrupt_type no_irq_type = {
76 volatile unsigned long irq_err_count, spurious_count;
79 * Generic, controller-independent functions:
82 int show_interrupts(struct seq_file *p, void *v)
84 int i = *(loff_t *) v;
85 struct irqaction * action;
90 for (i=0; i < 1 /*smp_num_cpus*/; i++)
91 seq_printf(p, "CPU%d ", i);
97 const char *type_name = irq_desc[i].handler->typename;
98 spin_lock_irqsave(&irq_desc[j].lock, flags);
99 action = irq_desc[i].action;
105 for (j = 0; j < NR_IRQS; j++)
106 if (irq_desc[j].handler->typename == type_name) {
112 seq_printf(p, "%3d: ",i);
113 seq_printf(p, "%10u ", kstat_irqs(i));
115 int prec = (num >= 100 ? 3 : num >= 10 ? 2 : 1);
116 seq_printf(p, " %*s%d", 14 - prec, type_name, num);
118 seq_printf(p, " %14s", type_name);
120 seq_printf(p, " %s", action->name);
121 for (action=action->next; action; action = action->next)
122 seq_printf(p, ", %s", action->name);
125 spin_unlock_irqrestore(&irq_desc[j].lock, flags);
126 } else if (i == NR_IRQS)
127 seq_printf(p, "ERR: %10lu\n", irq_err_count);
132 * This should really return information about whether
133 * we should do bottom half handling etc. Right now we
134 * end up _always_ checking the bottom half, which is a
135 * waste of time and is not what some drivers would
138 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
140 int status = 1; /* Force the "do bottom halves" bit */
142 if (!(action->flags & SA_INTERRUPT))
146 status |= action->flags;
147 action->handler(irq, action->dev_id, regs);
148 action = action->next;
150 if (status & SA_SAMPLE_RANDOM)
151 add_interrupt_randomness(irq);
158 * Generic enable/disable code: this just calls
159 * down into the PIC-specific version for the actual
160 * hardware disable after having gotten the irq
165 * disable_irq_nosync - disable an irq without waiting
166 * @irq: Interrupt to disable
168 * Disable the selected interrupt line. Disables of an interrupt
169 * stack. Unlike disable_irq(), this function does not ensure existing
170 * instances of the IRQ handler have completed before returning.
172 * This function may be called from IRQ context.
175 void inline disable_irq_nosync(unsigned int irq)
177 irq_desc_t *desc = irq_desc + irq;
180 spin_lock_irqsave(&desc->lock, flags);
181 if (!desc->depth++) {
182 desc->status |= IRQ_DISABLED;
183 desc->handler->disable(irq);
185 spin_unlock_irqrestore(&desc->lock, flags);
189 * disable_irq - disable an irq and wait for completion
190 * @irq: Interrupt to disable
192 * Disable the selected interrupt line. Disables of an interrupt
193 * stack. That is for two disables you need two enables. This
194 * function waits for any pending IRQ handlers for this interrupt
195 * to complete before returning. If you use this function while
196 * holding a resource the IRQ handler may need you will deadlock.
198 * This function may be called - with care - from IRQ context.
201 void disable_irq(unsigned int irq)
203 disable_irq_nosync(irq);
204 synchronize_irq(irq);
208 * enable_irq - enable interrupt handling on an irq
209 * @irq: Interrupt to enable
211 * Re-enables the processing of interrupts on this IRQ line
212 * providing no disable_irq calls are now in effect.
214 * This function may be called from IRQ context.
217 void enable_irq(unsigned int irq)
219 irq_desc_t *desc = irq_desc + irq;
222 spin_lock_irqsave(&desc->lock, flags);
223 switch (desc->depth) {
225 unsigned int status = desc->status & ~IRQ_DISABLED;
226 desc->status = status;
227 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
228 desc->status = status | IRQ_REPLAY;
229 hw_resend_irq(desc->handler,irq);
231 desc->handler->enable(irq);
238 printk("enable_irq(%u) unbalanced from %p\n", irq,
239 __builtin_return_address(0));
241 spin_unlock_irqrestore(&desc->lock, flags);
244 /* Handle interrupt IRQ. REGS are the registers at the time of ther
246 unsigned int handle_irq (int irq, struct pt_regs *regs)
249 * We ack quickly, we don't want the irq controller
250 * thinking we're snobs just because some other CPU has
251 * disabled global interrupts (we have already done the
252 * INT_ACK cycles, it's too late to try to pretend to the
253 * controller that we aren't taking the interrupt).
255 * 0 return value means that this irq is already being
256 * handled by some other CPU. (or is disabled)
258 int cpu = smp_processor_id();
259 irq_desc_t *desc = irq_desc + irq;
260 struct irqaction * action;
264 kstat_cpu(cpu).irqs[irq]++;
265 spin_lock(&desc->lock);
266 desc->handler->ack(irq);
268 REPLAY is when Linux resends an IRQ that was dropped earlier
269 WAITING is used by probe to mark irqs that are being tested
271 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
272 status |= IRQ_PENDING; /* we _want_ to handle it */
275 * If the IRQ is disabled for whatever reason, we cannot
276 * use the action we have.
279 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
280 action = desc->action;
281 status &= ~IRQ_PENDING; /* we commit to handling */
282 status |= IRQ_INPROGRESS; /* we are handling it */
284 desc->status = status;
287 * If there is no IRQ handler or it was disabled, exit early.
288 Since we set PENDING, if another processor is handling
289 a different instance of this same irq, the other processor
290 will take care of it.
292 if (unlikely(!action))
296 * Edge triggered interrupts need to remember
298 * This applies to any hw interrupts that allow a second
299 * instance of the same irq to arrive while we are in handle_irq
300 * or in the handler. But the code here only handles the _second_
301 * instance of the irq, not the third or fourth. So it is mostly
302 * useful for irq hardware that does not mask cleanly in an
306 spin_unlock(&desc->lock);
307 handle_IRQ_event(irq, regs, action);
308 spin_lock(&desc->lock);
310 if (likely(!(desc->status & IRQ_PENDING)))
312 desc->status &= ~IRQ_PENDING;
314 desc->status &= ~IRQ_INPROGRESS;
318 * The ->end() handler has to deal with interrupts which got
319 * disabled while the handler was running.
321 desc->handler->end(irq);
322 spin_unlock(&desc->lock);
330 * request_irq - allocate an interrupt line
331 * @irq: Interrupt line to allocate
332 * @handler: Function to be called when the IRQ occurs
333 * @irqflags: Interrupt type flags
334 * @devname: An ascii name for the claiming device
335 * @dev_id: A cookie passed back to the handler function
337 * This call allocates interrupt resources and enables the
338 * interrupt line and IRQ handling. From the point this
339 * call is made your handler function may be invoked. Since
340 * your handler function must clear any interrupt the board
341 * raises, you must take care both to initialise your hardware
342 * and to set up the interrupt handler in the right order.
344 * Dev_id must be globally unique. Normally the address of the
345 * device data structure is used as the cookie. Since the handler
346 * receives this value it makes sense to use it.
348 * If your interrupt is shared you must pass a non NULL dev_id
349 * as this is required when freeing the interrupt.
353 * SA_SHIRQ Interrupt is shared
355 * SA_INTERRUPT Disable local interrupts while processing
357 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
361 int request_irq(unsigned int irq,
362 irqreturn_t (*handler)(int, void *, struct pt_regs *),
363 unsigned long irqflags,
364 const char * devname,
368 struct irqaction * action;
372 * Sanity-check: shared interrupts should REALLY pass in
373 * a real dev-ID, otherwise we'll have trouble later trying
374 * to figure out which interrupt is which (messes up the
375 * interrupt freeing logic etc).
377 if (irqflags & SA_SHIRQ) {
379 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
388 action = (struct irqaction *)
389 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
393 action->handler = handler;
394 action->flags = irqflags;
396 action->name = devname;
398 action->dev_id = dev_id;
400 retval = setup_irq(irq, action);
406 EXPORT_SYMBOL(request_irq);
409 * free_irq - free an interrupt
410 * @irq: Interrupt line to free
411 * @dev_id: Device identity to free
413 * Remove an interrupt handler. The handler is removed and if the
414 * interrupt line is no longer in use by any driver it is disabled.
415 * On a shared IRQ the caller must ensure the interrupt is disabled
416 * on the card it drives before calling this function. The function
417 * does not return until any executing interrupts for this IRQ
420 * This function may be called from interrupt context.
422 * Bugs: Attempting to free an irq in a handler for the same irq hangs
426 void free_irq(unsigned int irq, void *dev_id)
429 struct irqaction **p;
435 desc = irq_desc + irq;
436 spin_lock_irqsave(&desc->lock,flags);
439 struct irqaction * action = *p;
441 struct irqaction **pp = p;
443 if (action->dev_id != dev_id)
446 /* Found it - now remove it from the list of entries */
449 desc->status |= IRQ_DISABLED;
450 desc->handler->shutdown(irq);
452 spin_unlock_irqrestore(&desc->lock,flags);
454 synchronize_irq(irq);
458 printk("Trying to free free IRQ%d\n",irq);
459 spin_unlock_irqrestore(&desc->lock,flags);
464 EXPORT_SYMBOL(free_irq);
467 * IRQ autodetection code..
469 * This depends on the fact that any interrupt that
470 * comes in on to an unassigned handler will get stuck
471 * with "IRQ_WAITING" cleared and the interrupt
475 static DECLARE_MUTEX(probe_sem);
478 * probe_irq_on - begin an interrupt autodetect
480 * Commence probing for an interrupt. The interrupts are scanned
481 * and a mask of potential interrupt lines is returned.
485 unsigned long probe_irq_on(void)
494 * something may have generated an irq long ago and we want to
495 * flush such a longstanding irq before considering it as spurious.
497 for (i = NR_IRQS-1; i > 0; i--) {
500 spin_lock_irq(&desc->lock);
501 if (!irq_desc[i].action)
502 irq_desc[i].handler->startup(i);
503 spin_unlock_irq(&desc->lock);
506 /* Wait for longstanding interrupts to trigger. */
507 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
508 /* about 20ms delay */ barrier();
511 * enable any unassigned irqs
512 * (we must startup again here because if a longstanding irq
513 * happened in the previous stage, it may have masked itself)
515 for (i = NR_IRQS-1; i > 0; i--) {
518 spin_lock_irq(&desc->lock);
520 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
521 if (desc->handler->startup(i))
522 desc->status |= IRQ_PENDING;
524 spin_unlock_irq(&desc->lock);
528 * Wait for spurious interrupts to trigger
530 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
531 /* about 100ms delay */ barrier();
534 * Now filter out any obviously spurious interrupts
537 for (i = 0; i < NR_IRQS; i++) {
538 irq_desc_t *desc = irq_desc + i;
541 spin_lock_irq(&desc->lock);
542 status = desc->status;
544 if (status & IRQ_AUTODETECT) {
545 /* It triggered already - consider it spurious. */
546 if (!(status & IRQ_WAITING)) {
547 desc->status = status & ~IRQ_AUTODETECT;
548 desc->handler->shutdown(i);
553 spin_unlock_irq(&desc->lock);
559 EXPORT_SYMBOL(probe_irq_on);
562 * Return a mask of triggered interrupts (this
563 * can handle only legacy ISA interrupts).
567 * probe_irq_mask - scan a bitmap of interrupt lines
568 * @val: mask of interrupts to consider
570 * Scan the ISA bus interrupt lines and return a bitmap of
571 * active interrupts. The interrupt probe logic state is then
572 * returned to its previous value.
574 * Note: we need to scan all the irq's even though we will
575 * only return ISA irq numbers - just so that we reset them
576 * all to a known state.
578 unsigned int probe_irq_mask(unsigned long val)
584 for (i = 0; i < NR_IRQS; i++) {
585 irq_desc_t *desc = irq_desc + i;
588 spin_lock_irq(&desc->lock);
589 status = desc->status;
591 if (status & IRQ_AUTODETECT) {
592 if (i < 16 && !(status & IRQ_WAITING))
595 desc->status = status & ~IRQ_AUTODETECT;
596 desc->handler->shutdown(i);
598 spin_unlock_irq(&desc->lock);
606 * Return the one interrupt that triggered (this can
607 * handle any interrupt source).
611 * probe_irq_off - end an interrupt autodetect
612 * @val: mask of potential interrupts (unused)
614 * Scans the unused interrupt lines and returns the line which
615 * appears to have triggered the interrupt. If no interrupt was
616 * found then zero is returned. If more than one interrupt is
617 * found then minus the first candidate is returned to indicate
620 * The interrupt probe logic state is returned to its previous
623 * BUGS: When used in a module (which arguably shouldnt happen)
624 * nothing prevents two IRQ probe callers from overlapping. The
625 * results of this are non-optimal.
628 int probe_irq_off(unsigned long val)
630 int i, irq_found, nr_irqs;
634 for (i = 0; i < NR_IRQS; i++) {
635 irq_desc_t *desc = irq_desc + i;
638 spin_lock_irq(&desc->lock);
639 status = desc->status;
641 if (status & IRQ_AUTODETECT) {
642 if (!(status & IRQ_WAITING)) {
647 desc->status = status & ~IRQ_AUTODETECT;
648 desc->handler->shutdown(i);
650 spin_unlock_irq(&desc->lock);
655 irq_found = -irq_found;
659 EXPORT_SYMBOL(probe_irq_off);
661 /* this was setup_x86_irq but it seems pretty generic */
662 int setup_irq(unsigned int irq, struct irqaction * new)
666 struct irqaction *old, **p;
667 irq_desc_t *desc = irq_desc + irq;
670 * Some drivers like serial.c use request_irq() heavily,
671 * so we have to be careful not to interfere with a
674 if (new->flags & SA_SAMPLE_RANDOM) {
676 * This function might sleep, we want to call it first,
677 * outside of the atomic block.
678 * Yes, this might clear the entropy pool if the wrong
679 * driver is attempted to be loaded, without actually
680 * installing a new handler, but is this really a problem,
681 * only the sysadmin is able to do this.
683 rand_initialize_irq(irq);
687 * The following block of code has to be executed atomically
689 spin_lock_irqsave(&desc->lock,flags);
691 if ((old = *p) != NULL) {
692 /* Can't share interrupts unless both agree to */
693 if (!(old->flags & new->flags & SA_SHIRQ)) {
694 spin_unlock_irqrestore(&desc->lock,flags);
698 /* add new interrupt at end of irq queue */
710 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
711 desc->handler->startup(irq);
713 spin_unlock_irqrestore(&desc->lock,flags);
715 /* register_irq_proc(irq); */
719 /* Initialize irq handling for IRQs.
720 BASE_IRQ, BASE_IRQ+INTERVAL, ..., BASE_IRQ+NUM*INTERVAL
721 to IRQ_TYPE. An IRQ_TYPE of 0 means to use a generic interrupt type. */
723 init_irq_handlers (int base_irq, int num, int interval,
724 struct hw_interrupt_type *irq_type)
727 irq_desc[base_irq].status = IRQ_DISABLED;
728 irq_desc[base_irq].action = NULL;
729 irq_desc[base_irq].depth = 1;
730 irq_desc[base_irq].handler = irq_type;
731 base_irq += interval;
735 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
736 void init_irq_proc(void)
739 #endif /* CONFIG_PROC_FS && CONFIG_SYSCTL */