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 = {
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 */
145 if (!(action->flags & SA_INTERRUPT))
149 status |= action->flags;
150 action->handler(irq, action->dev_id, regs);
151 action = action->next;
153 if (status & SA_SAMPLE_RANDOM)
154 add_interrupt_randomness(irq);
161 * Generic enable/disable code: this just calls
162 * down into the PIC-specific version for the actual
163 * hardware disable after having gotten the irq
168 * disable_irq_nosync - disable an irq without waiting
169 * @irq: Interrupt to disable
171 * Disable the selected interrupt line. Disables of an interrupt
172 * stack. Unlike disable_irq(), this function does not ensure existing
173 * instances of the IRQ handler have completed before returning.
175 * This function may be called from IRQ context.
178 void inline disable_irq_nosync(unsigned int irq)
180 irq_desc_t *desc = irq_desc + irq;
183 spin_lock_irqsave(&desc->lock, flags);
184 if (!desc->depth++) {
185 desc->status |= IRQ_DISABLED;
186 desc->handler->disable(irq);
188 spin_unlock_irqrestore(&desc->lock, flags);
192 * disable_irq - disable an irq and wait for completion
193 * @irq: Interrupt to disable
195 * Disable the selected interrupt line. Disables of an interrupt
196 * stack. That is for two disables you need two enables. This
197 * function waits for any pending IRQ handlers for this interrupt
198 * to complete before returning. If you use this function while
199 * holding a resource the IRQ handler may need you will deadlock.
201 * This function may be called - with care - from IRQ context.
204 void disable_irq(unsigned int irq)
206 disable_irq_nosync(irq);
207 synchronize_irq(irq);
211 * enable_irq - enable interrupt handling on an irq
212 * @irq: Interrupt to enable
214 * Re-enables the processing of interrupts on this IRQ line
215 * providing no disable_irq calls are now in effect.
217 * This function may be called from IRQ context.
220 void enable_irq(unsigned int irq)
222 irq_desc_t *desc = irq_desc + irq;
225 spin_lock_irqsave(&desc->lock, flags);
226 switch (desc->depth) {
228 unsigned int status = desc->status & ~IRQ_DISABLED;
229 desc->status = status;
230 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
231 desc->status = status | IRQ_REPLAY;
232 hw_resend_irq(desc->handler,irq);
234 desc->handler->enable(irq);
241 printk("enable_irq(%u) unbalanced from %p\n", irq,
242 __builtin_return_address(0));
244 spin_unlock_irqrestore(&desc->lock, flags);
247 /* Handle interrupt IRQ. REGS are the registers at the time of ther
249 unsigned int handle_irq (int irq, struct pt_regs *regs)
252 * We ack quickly, we don't want the irq controller
253 * thinking we're snobs just because some other CPU has
254 * disabled global interrupts (we have already done the
255 * INT_ACK cycles, it's too late to try to pretend to the
256 * controller that we aren't taking the interrupt).
258 * 0 return value means that this irq is already being
259 * handled by some other CPU. (or is disabled)
261 int cpu = smp_processor_id();
262 irq_desc_t *desc = irq_desc + irq;
263 struct irqaction * action;
267 kstat_cpu(cpu).irqs[irq]++;
268 spin_lock(&desc->lock);
269 desc->handler->ack(irq);
271 REPLAY is when Linux resends an IRQ that was dropped earlier
272 WAITING is used by probe to mark irqs that are being tested
274 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
275 status |= IRQ_PENDING; /* we _want_ to handle it */
278 * If the IRQ is disabled for whatever reason, we cannot
279 * use the action we have.
282 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
283 action = desc->action;
284 status &= ~IRQ_PENDING; /* we commit to handling */
285 status |= IRQ_INPROGRESS; /* we are handling it */
287 desc->status = status;
290 * If there is no IRQ handler or it was disabled, exit early.
291 Since we set PENDING, if another processor is handling
292 a different instance of this same irq, the other processor
293 will take care of it.
295 if (unlikely(!action))
299 * Edge triggered interrupts need to remember
301 * This applies to any hw interrupts that allow a second
302 * instance of the same irq to arrive while we are in handle_irq
303 * or in the handler. But the code here only handles the _second_
304 * instance of the irq, not the third or fourth. So it is mostly
305 * useful for irq hardware that does not mask cleanly in an
309 spin_unlock(&desc->lock);
310 handle_IRQ_event(irq, regs, action);
311 spin_lock(&desc->lock);
313 if (likely(!(desc->status & IRQ_PENDING)))
315 desc->status &= ~IRQ_PENDING;
317 desc->status &= ~IRQ_INPROGRESS;
321 * The ->end() handler has to deal with interrupts which got
322 * disabled while the handler was running.
324 desc->handler->end(irq);
325 spin_unlock(&desc->lock);
333 * request_irq - allocate an interrupt line
334 * @irq: Interrupt line to allocate
335 * @handler: Function to be called when the IRQ occurs
336 * @irqflags: Interrupt type flags
337 * @devname: An ascii name for the claiming device
338 * @dev_id: A cookie passed back to the handler function
340 * This call allocates interrupt resources and enables the
341 * interrupt line and IRQ handling. From the point this
342 * call is made your handler function may be invoked. Since
343 * your handler function must clear any interrupt the board
344 * raises, you must take care both to initialise your hardware
345 * and to set up the interrupt handler in the right order.
347 * Dev_id must be globally unique. Normally the address of the
348 * device data structure is used as the cookie. Since the handler
349 * receives this value it makes sense to use it.
351 * If your interrupt is shared you must pass a non NULL dev_id
352 * as this is required when freeing the interrupt.
356 * SA_SHIRQ Interrupt is shared
358 * SA_INTERRUPT Disable local interrupts while processing
360 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
364 int request_irq(unsigned int irq,
365 irqreturn_t (*handler)(int, void *, struct pt_regs *),
366 unsigned long irqflags,
367 const char * devname,
371 struct irqaction * action;
375 * Sanity-check: shared interrupts should REALLY pass in
376 * a real dev-ID, otherwise we'll have trouble later trying
377 * to figure out which interrupt is which (messes up the
378 * interrupt freeing logic etc).
380 if (irqflags & SA_SHIRQ) {
382 printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
391 action = (struct irqaction *)
392 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
396 action->handler = handler;
397 action->flags = irqflags;
398 cpus_clear(action->mask);
399 action->name = devname;
401 action->dev_id = dev_id;
403 retval = setup_irq(irq, action);
409 EXPORT_SYMBOL(request_irq);
412 * free_irq - free an interrupt
413 * @irq: Interrupt line to free
414 * @dev_id: Device identity to free
416 * Remove an interrupt handler. The handler is removed and if the
417 * interrupt line is no longer in use by any driver it is disabled.
418 * On a shared IRQ the caller must ensure the interrupt is disabled
419 * on the card it drives before calling this function. The function
420 * does not return until any executing interrupts for this IRQ
423 * This function may be called from interrupt context.
425 * Bugs: Attempting to free an irq in a handler for the same irq hangs
429 void free_irq(unsigned int irq, void *dev_id)
432 struct irqaction **p;
438 desc = irq_desc + irq;
439 spin_lock_irqsave(&desc->lock,flags);
442 struct irqaction * action = *p;
444 struct irqaction **pp = p;
446 if (action->dev_id != dev_id)
449 /* Found it - now remove it from the list of entries */
452 desc->status |= IRQ_DISABLED;
453 desc->handler->shutdown(irq);
455 spin_unlock_irqrestore(&desc->lock,flags);
457 synchronize_irq(irq);
461 printk("Trying to free free IRQ%d\n",irq);
462 spin_unlock_irqrestore(&desc->lock,flags);
467 EXPORT_SYMBOL(free_irq);
470 * IRQ autodetection code..
472 * This depends on the fact that any interrupt that
473 * comes in on to an unassigned handler will get stuck
474 * with "IRQ_WAITING" cleared and the interrupt
478 static DECLARE_MUTEX(probe_sem);
481 * probe_irq_on - begin an interrupt autodetect
483 * Commence probing for an interrupt. The interrupts are scanned
484 * and a mask of potential interrupt lines is returned.
488 unsigned long probe_irq_on(void)
497 * something may have generated an irq long ago and we want to
498 * flush such a longstanding irq before considering it as spurious.
500 for (i = NR_IRQS-1; i > 0; i--) {
503 spin_lock_irq(&desc->lock);
504 if (!irq_desc[i].action)
505 irq_desc[i].handler->startup(i);
506 spin_unlock_irq(&desc->lock);
509 /* Wait for longstanding interrupts to trigger. */
510 for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
511 /* about 20ms delay */ barrier();
514 * enable any unassigned irqs
515 * (we must startup again here because if a longstanding irq
516 * happened in the previous stage, it may have masked itself)
518 for (i = NR_IRQS-1; i > 0; i--) {
521 spin_lock_irq(&desc->lock);
523 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
524 if (desc->handler->startup(i))
525 desc->status |= IRQ_PENDING;
527 spin_unlock_irq(&desc->lock);
531 * Wait for spurious interrupts to trigger
533 for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
534 /* about 100ms delay */ barrier();
537 * Now filter out any obviously spurious interrupts
540 for (i = 0; i < NR_IRQS; i++) {
541 irq_desc_t *desc = irq_desc + i;
544 spin_lock_irq(&desc->lock);
545 status = desc->status;
547 if (status & IRQ_AUTODETECT) {
548 /* It triggered already - consider it spurious. */
549 if (!(status & IRQ_WAITING)) {
550 desc->status = status & ~IRQ_AUTODETECT;
551 desc->handler->shutdown(i);
556 spin_unlock_irq(&desc->lock);
562 EXPORT_SYMBOL(probe_irq_on);
565 * Return a mask of triggered interrupts (this
566 * can handle only legacy ISA interrupts).
570 * probe_irq_mask - scan a bitmap of interrupt lines
571 * @val: mask of interrupts to consider
573 * Scan the ISA bus interrupt lines and return a bitmap of
574 * active interrupts. The interrupt probe logic state is then
575 * returned to its previous value.
577 * Note: we need to scan all the irq's even though we will
578 * only return ISA irq numbers - just so that we reset them
579 * all to a known state.
581 unsigned int probe_irq_mask(unsigned long val)
587 for (i = 0; i < NR_IRQS; i++) {
588 irq_desc_t *desc = irq_desc + i;
591 spin_lock_irq(&desc->lock);
592 status = desc->status;
594 if (status & IRQ_AUTODETECT) {
595 if (i < 16 && !(status & IRQ_WAITING))
598 desc->status = status & ~IRQ_AUTODETECT;
599 desc->handler->shutdown(i);
601 spin_unlock_irq(&desc->lock);
609 * Return the one interrupt that triggered (this can
610 * handle any interrupt source).
614 * probe_irq_off - end an interrupt autodetect
615 * @val: mask of potential interrupts (unused)
617 * Scans the unused interrupt lines and returns the line which
618 * appears to have triggered the interrupt. If no interrupt was
619 * found then zero is returned. If more than one interrupt is
620 * found then minus the first candidate is returned to indicate
623 * The interrupt probe logic state is returned to its previous
626 * BUGS: When used in a module (which arguably shouldnt happen)
627 * nothing prevents two IRQ probe callers from overlapping. The
628 * results of this are non-optimal.
631 int probe_irq_off(unsigned long val)
633 int i, irq_found, nr_irqs;
637 for (i = 0; i < NR_IRQS; i++) {
638 irq_desc_t *desc = irq_desc + i;
641 spin_lock_irq(&desc->lock);
642 status = desc->status;
644 if (status & IRQ_AUTODETECT) {
645 if (!(status & IRQ_WAITING)) {
650 desc->status = status & ~IRQ_AUTODETECT;
651 desc->handler->shutdown(i);
653 spin_unlock_irq(&desc->lock);
658 irq_found = -irq_found;
662 EXPORT_SYMBOL(probe_irq_off);
664 /* this was setup_x86_irq but it seems pretty generic */
665 int setup_irq(unsigned int irq, struct irqaction * new)
669 struct irqaction *old, **p;
670 irq_desc_t *desc = irq_desc + irq;
673 * Some drivers like serial.c use request_irq() heavily,
674 * so we have to be careful not to interfere with a
677 if (new->flags & SA_SAMPLE_RANDOM) {
679 * This function might sleep, we want to call it first,
680 * outside of the atomic block.
681 * Yes, this might clear the entropy pool if the wrong
682 * driver is attempted to be loaded, without actually
683 * installing a new handler, but is this really a problem,
684 * only the sysadmin is able to do this.
686 rand_initialize_irq(irq);
690 * The following block of code has to be executed atomically
692 spin_lock_irqsave(&desc->lock,flags);
694 if ((old = *p) != NULL) {
695 /* Can't share interrupts unless both agree to */
696 if (!(old->flags & new->flags & SA_SHIRQ)) {
697 spin_unlock_irqrestore(&desc->lock,flags);
701 /* add new interrupt at end of irq queue */
713 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
714 desc->handler->startup(irq);
716 spin_unlock_irqrestore(&desc->lock,flags);
718 /* register_irq_proc(irq); */
722 /* Initialize irq handling for IRQs.
723 BASE_IRQ, BASE_IRQ+INTERVAL, ..., BASE_IRQ+NUM*INTERVAL
724 to IRQ_TYPE. An IRQ_TYPE of 0 means to use a generic interrupt type. */
726 init_irq_handlers (int base_irq, int num, int interval,
727 struct hw_interrupt_type *irq_type)
730 irq_desc[base_irq].status = IRQ_DISABLED;
731 irq_desc[base_irq].action = NULL;
732 irq_desc[base_irq].depth = 1;
733 irq_desc[base_irq].handler = irq_type;
734 base_irq += interval;
738 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
739 void init_irq_proc(void)
742 #endif /* CONFIG_PROC_FS && CONFIG_SYSCTL */