2 * linux/kernel/irq/handle.c
4 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
7 * This file contains the core interrupt handling code.
9 * Detailed information is available in Documentation/DocBook/genericirq
13 #include <linux/irq.h>
14 #include <linux/module.h>
15 #include <linux/random.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel_stat.h>
19 #include "internals.h"
22 * lockdep: we want to handle all irq_desc locks as a single lock-class:
24 static struct lock_class_key irq_desc_lock_class;
27 * handle_bad_irq - handle spurious and unhandled irqs
28 * @irq: the interrupt number
29 * @desc: description of the interrupt
31 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
34 handle_bad_irq(unsigned int irq, struct irq_desc *desc)
36 print_irq_desc(irq, desc);
37 kstat_irqs_this_cpu(desc)++;
42 * Linux has a controller-independent interrupt architecture.
43 * Every controller has a 'controller-template', that is used
44 * by the main code to do the right thing. Each driver-visible
45 * interrupt source is transparently wired to the appropriate
46 * controller. Thus drivers need not be aware of the
47 * interrupt-controller.
49 * The code is designed to be easily extended with new/different
50 * interrupt controllers, without having to do assembly magic or
51 * having to touch the generic code.
53 * Controller mappings for all interrupt sources:
55 int nr_irqs = NR_IRQS;
56 EXPORT_SYMBOL_GPL(nr_irqs);
58 #ifdef CONFIG_HAVE_DYN_ARRAY
59 static struct irq_desc irq_desc_init = {
61 .status = IRQ_DISABLED,
63 .handle_irq = handle_bad_irq,
65 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
67 .affinity = CPU_MASK_ALL
72 static void init_one_irq_desc(struct irq_desc *desc)
74 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
75 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
78 extern int after_bootmem;
79 extern void *__alloc_bootmem_nopanic(unsigned long size,
83 static void init_kstat_irqs(struct irq_desc *desc, int nr_desc, int nr)
85 unsigned long bytes, total_bytes;
90 /* Compute how many bytes we need per irq and allocate them */
91 bytes = nr * sizeof(unsigned int);
92 total_bytes = bytes * nr_desc;
94 ptr = kzalloc(total_bytes, GFP_ATOMIC);
96 ptr = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
99 panic(" can not allocate kstat_irqs\n");
102 printk(KERN_DEBUG "kstat_irqs ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
104 for (i = 0; i < nr_desc; i++) {
105 desc[i].kstat_irqs = (unsigned int *)ptr;
110 #ifdef CONFIG_HAVE_SPARSE_IRQ
111 static struct irq_desc *sparse_irqs_free;
112 struct irq_desc *sparse_irqs;
115 static void __init init_work(void *data)
117 struct dyn_array *da = data;
119 struct irq_desc *desc;
123 for (i = 0; i < *da->nr; i++) {
124 init_one_irq_desc(&desc[i]);
125 #ifndef CONFIG_HAVE_SPARSE_IRQ
130 /* init kstat_irqs, nr_cpu_ids is ready already */
131 init_kstat_irqs(desc, *da->nr, nr_cpu_ids);
133 #ifdef CONFIG_HAVE_SPARSE_IRQ
134 for (i = 1; i < *da->nr; i++)
135 desc[i-1].next = &desc[i];
137 sparse_irqs_free = sparse_irqs;
142 #ifdef CONFIG_HAVE_SPARSE_IRQ
143 static int nr_irq_desc = 32;
145 static int __init parse_nr_irq_desc(char *arg)
148 nr_irq_desc = simple_strtoul(arg, NULL, 0);
152 early_param("nr_irq_desc", parse_nr_irq_desc);
154 DEFINE_DYN_ARRAY(sparse_irqs, sizeof(struct irq_desc), nr_irq_desc, PAGE_SIZE, init_work);
156 struct irq_desc *irq_to_desc(unsigned int irq)
158 struct irq_desc *desc;
162 if (desc->irq == irq)
169 struct irq_desc *irq_to_desc_alloc(unsigned int irq)
171 struct irq_desc *desc, *desc_pri;
175 desc_pri = desc = sparse_irqs;
177 if (desc->irq == irq)
186 * we run out of pre-allocate ones, allocate more
188 if (!sparse_irqs_free) {
190 unsigned long total_bytes;
192 printk(KERN_DEBUG "try to get more irq_desc %d\n", nr_irq_desc);
194 total_bytes = sizeof(struct irq_desc) * nr_irq_desc;
196 desc = kzalloc(total_bytes, GFP_ATOMIC);
198 desc = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
201 panic("please boot with nr_irq_desc= %d\n", count * 2);
204 printk(KERN_DEBUG "irq_desc ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
206 for (i = 0; i < nr_irq_desc; i++)
207 init_one_irq_desc(&desc[i]);
209 for (i = 1; i < nr_irq_desc; i++)
210 desc[i-1].next = &desc[i];
212 /* init kstat_irqs, nr_cpu_ids is ready already */
213 init_kstat_irqs(desc, nr_irq_desc, nr_cpu_ids);
215 sparse_irqs_free = desc;
218 desc = sparse_irqs_free;
219 sparse_irqs_free = sparse_irqs_free->next;
222 desc_pri->next = desc;
226 printk(KERN_DEBUG "found new irq_desc for irq %d\n", desc->irq);
227 #ifdef CONFIG_HAVE_SPARSE_IRQ_DEBUG
229 /* dump the results */
230 struct irq_desc *desc;
232 unsigned long bytes = sizeof(struct irq_desc);
235 printk(KERN_DEBUG "=========================== %d\n", irq);
236 printk(KERN_DEBUG "irq_desc dump after get that for %d\n", irq);
237 for_each_irq_desc(irqx, desc) {
239 printk(KERN_DEBUG "irq_desc %d ==> [%#lx - %#lx]\n", irqx, phys, phys + bytes);
241 printk(KERN_DEBUG "===========================\n");
247 struct irq_desc *irq_desc;
248 DEFINE_DYN_ARRAY(irq_desc, sizeof(struct irq_desc), nr_irqs, PAGE_SIZE, init_work);
254 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
255 [0 ... NR_IRQS-1] = {
256 .status = IRQ_DISABLED,
257 .chip = &no_irq_chip,
258 .handle_irq = handle_bad_irq,
260 .lock = __SPIN_LOCK_UNLOCKED(sparse_irqs->lock),
262 .affinity = CPU_MASK_ALL
269 #ifndef CONFIG_HAVE_SPARSE_IRQ
270 struct irq_desc *irq_to_desc(unsigned int irq)
273 return &irq_desc[irq];
277 struct irq_desc *irq_to_desc_alloc(unsigned int irq)
279 return irq_to_desc(irq);
284 * What should we do if we get a hw irq event on an illegal vector?
285 * Each architecture has to answer this themself.
287 static void ack_bad(unsigned int irq)
289 struct irq_desc *desc;
291 desc = irq_to_desc(irq);
292 print_irq_desc(irq, desc);
299 static void noop(unsigned int irq)
303 static unsigned int noop_ret(unsigned int irq)
309 * Generic no controller implementation
311 struct irq_chip no_irq_chip = {
322 * Generic dummy implementation which can be used for
323 * real dumb interrupt sources
325 struct irq_chip dummy_irq_chip = {
338 * Special, empty irq handler:
340 irqreturn_t no_action(int cpl, void *dev_id)
346 * handle_IRQ_event - irq action chain handler
347 * @irq: the interrupt number
348 * @action: the interrupt action chain for this irq
350 * Handles the action chain of an irq event
352 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
354 irqreturn_t ret, retval = IRQ_NONE;
355 unsigned int status = 0;
357 if (!(action->flags & IRQF_DISABLED))
358 local_irq_enable_in_hardirq();
361 ret = action->handler(irq, action->dev_id);
362 if (ret == IRQ_HANDLED)
363 status |= action->flags;
365 action = action->next;
368 if (status & IRQF_SAMPLE_RANDOM)
369 add_interrupt_randomness(irq);
375 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
377 * __do_IRQ - original all in one highlevel IRQ handler
378 * @irq: the interrupt number
380 * __do_IRQ handles all normal device IRQ's (the special
381 * SMP cross-CPU interrupts have their own specific
384 * This is the original x86 implementation which is used for every
387 unsigned int __do_IRQ(unsigned int irq)
389 struct irq_desc *desc = irq_to_desc(irq);
390 struct irqaction *action;
393 kstat_irqs_this_cpu(desc)++;
394 if (CHECK_IRQ_PER_CPU(desc->status)) {
395 irqreturn_t action_ret;
398 * No locking required for CPU-local interrupts:
401 desc->chip->ack(irq);
402 if (likely(!(desc->status & IRQ_DISABLED))) {
403 action_ret = handle_IRQ_event(irq, desc->action);
405 note_interrupt(irq, desc, action_ret);
407 desc->chip->end(irq);
411 spin_lock(&desc->lock);
413 desc->chip->ack(irq);
415 * REPLAY is when Linux resends an IRQ that was dropped earlier
416 * WAITING is used by probe to mark irqs that are being tested
418 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
419 status |= IRQ_PENDING; /* we _want_ to handle it */
422 * If the IRQ is disabled for whatever reason, we cannot
423 * use the action we have.
426 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
427 action = desc->action;
428 status &= ~IRQ_PENDING; /* we commit to handling */
429 status |= IRQ_INPROGRESS; /* we are handling it */
431 desc->status = status;
434 * If there is no IRQ handler or it was disabled, exit early.
435 * Since we set PENDING, if another processor is handling
436 * a different instance of this same irq, the other processor
437 * will take care of it.
439 if (unlikely(!action))
443 * Edge triggered interrupts need to remember
445 * This applies to any hw interrupts that allow a second
446 * instance of the same irq to arrive while we are in do_IRQ
447 * or in the handler. But the code here only handles the _second_
448 * instance of the irq, not the third or fourth. So it is mostly
449 * useful for irq hardware that does not mask cleanly in an
453 irqreturn_t action_ret;
455 spin_unlock(&desc->lock);
457 action_ret = handle_IRQ_event(irq, action);
459 note_interrupt(irq, desc, action_ret);
461 spin_lock(&desc->lock);
462 if (likely(!(desc->status & IRQ_PENDING)))
464 desc->status &= ~IRQ_PENDING;
466 desc->status &= ~IRQ_INPROGRESS;
470 * The ->end() handler has to deal with interrupts which got
471 * disabled while the handler was running.
473 desc->chip->end(irq);
474 spin_unlock(&desc->lock);
481 #ifdef CONFIG_TRACE_IRQFLAGS
482 void early_init_irq_lock_class(void)
484 #ifndef CONFIG_HAVE_DYN_ARRAY
487 for (i = 0; i < nr_irqs; i++)
488 lockdep_set_class(&irq_desc[i].lock, &irq_desc_lock_class);
493 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
495 struct irq_desc *desc = irq_to_desc(irq);
496 return desc->kstat_irqs[cpu];
498 EXPORT_SYMBOL(kstat_irqs_cpu);