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 #ifdef CONFIG_HAVE_DYN_ARRAY
38 kstat_irqs_this_cpu(desc)++;
40 kstat_irqs_this_cpu(irq)++;
46 * Linux has a controller-independent interrupt architecture.
47 * Every controller has a 'controller-template', that is used
48 * by the main code to do the right thing. Each driver-visible
49 * interrupt source is transparently wired to the appropriate
50 * controller. Thus drivers need not be aware of the
51 * interrupt-controller.
53 * The code is designed to be easily extended with new/different
54 * interrupt controllers, without having to do assembly magic or
55 * having to touch the generic code.
57 * Controller mappings for all interrupt sources:
59 int nr_irqs = NR_IRQS;
60 EXPORT_SYMBOL_GPL(nr_irqs);
62 #ifdef CONFIG_HAVE_DYN_ARRAY
63 static struct irq_desc irq_desc_init = {
65 .status = IRQ_DISABLED,
67 .handle_irq = handle_bad_irq,
69 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
71 .affinity = CPU_MASK_ALL
76 static void init_one_irq_desc(struct irq_desc *desc)
78 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
79 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
82 extern int after_bootmem;
83 extern void *__alloc_bootmem_nopanic(unsigned long size,
87 static void init_kstat_irqs(struct irq_desc *desc, int nr_desc, int nr)
89 unsigned long bytes, total_bytes;
94 /* Compute how many bytes we need per irq and allocate them */
95 bytes = nr * sizeof(unsigned int);
96 total_bytes = bytes * nr_desc;
98 ptr = kzalloc(total_bytes, GFP_ATOMIC);
100 ptr = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
103 panic(" can not allocate kstat_irqs\n");
106 printk(KERN_DEBUG "kstat_irqs ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
108 for (i = 0; i < nr_desc; i++) {
109 desc[i].kstat_irqs = (unsigned int *)ptr;
114 static void __init init_work(void *data)
116 struct dyn_array *da = data;
118 struct irq_desc *desc;
122 for (i = 0; i < *da->nr; i++) {
123 init_one_irq_desc(&desc[i]);
127 /* init kstat_irqs, nr_cpu_ids is ready already */
128 init_kstat_irqs(desc, *da->nr, nr_cpu_ids);
131 struct irq_desc *irq_desc;
132 DEFINE_DYN_ARRAY(irq_desc, sizeof(struct irq_desc), nr_irqs, PAGE_SIZE, init_work);
136 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
137 [0 ... NR_IRQS-1] = {
138 .status = IRQ_DISABLED,
139 .chip = &no_irq_chip,
140 .handle_irq = handle_bad_irq,
142 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
144 .affinity = CPU_MASK_ALL
152 * What should we do if we get a hw irq event on an illegal vector?
153 * Each architecture has to answer this themself.
155 static void ack_bad(unsigned int irq)
157 struct irq_desc *desc;
159 desc = irq_to_desc(irq);
160 print_irq_desc(irq, desc);
167 static void noop(unsigned int irq)
171 static unsigned int noop_ret(unsigned int irq)
177 * Generic no controller implementation
179 struct irq_chip no_irq_chip = {
190 * Generic dummy implementation which can be used for
191 * real dumb interrupt sources
193 struct irq_chip dummy_irq_chip = {
206 * Special, empty irq handler:
208 irqreturn_t no_action(int cpl, void *dev_id)
214 * handle_IRQ_event - irq action chain handler
215 * @irq: the interrupt number
216 * @action: the interrupt action chain for this irq
218 * Handles the action chain of an irq event
220 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
222 irqreturn_t ret, retval = IRQ_NONE;
223 unsigned int status = 0;
225 if (!(action->flags & IRQF_DISABLED))
226 local_irq_enable_in_hardirq();
229 ret = action->handler(irq, action->dev_id);
230 if (ret == IRQ_HANDLED)
231 status |= action->flags;
233 action = action->next;
236 if (status & IRQF_SAMPLE_RANDOM)
237 add_interrupt_randomness(irq);
243 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
245 * __do_IRQ - original all in one highlevel IRQ handler
246 * @irq: the interrupt number
248 * __do_IRQ handles all normal device IRQ's (the special
249 * SMP cross-CPU interrupts have their own specific
252 * This is the original x86 implementation which is used for every
255 unsigned int __do_IRQ(unsigned int irq)
257 struct irq_desc *desc = irq_to_desc(irq);
258 struct irqaction *action;
261 #ifdef CONFIG_HAVE_DYN_ARRAY
262 kstat_irqs_this_cpu(desc)++;
264 kstat_irqs_this_cpu(irq)++;
266 if (CHECK_IRQ_PER_CPU(desc->status)) {
267 irqreturn_t action_ret;
270 * No locking required for CPU-local interrupts:
273 desc->chip->ack(irq);
274 if (likely(!(desc->status & IRQ_DISABLED))) {
275 action_ret = handle_IRQ_event(irq, desc->action);
277 note_interrupt(irq, desc, action_ret);
279 desc->chip->end(irq);
283 spin_lock(&desc->lock);
285 desc->chip->ack(irq);
287 * REPLAY is when Linux resends an IRQ that was dropped earlier
288 * WAITING is used by probe to mark irqs that are being tested
290 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
291 status |= IRQ_PENDING; /* we _want_ to handle it */
294 * If the IRQ is disabled for whatever reason, we cannot
295 * use the action we have.
298 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
299 action = desc->action;
300 status &= ~IRQ_PENDING; /* we commit to handling */
301 status |= IRQ_INPROGRESS; /* we are handling it */
303 desc->status = status;
306 * If there is no IRQ handler or it was disabled, exit early.
307 * Since we set PENDING, if another processor is handling
308 * a different instance of this same irq, the other processor
309 * will take care of it.
311 if (unlikely(!action))
315 * Edge triggered interrupts need to remember
317 * This applies to any hw interrupts that allow a second
318 * instance of the same irq to arrive while we are in do_IRQ
319 * or in the handler. But the code here only handles the _second_
320 * instance of the irq, not the third or fourth. So it is mostly
321 * useful for irq hardware that does not mask cleanly in an
325 irqreturn_t action_ret;
327 spin_unlock(&desc->lock);
329 action_ret = handle_IRQ_event(irq, action);
331 note_interrupt(irq, desc, action_ret);
333 spin_lock(&desc->lock);
334 if (likely(!(desc->status & IRQ_PENDING)))
336 desc->status &= ~IRQ_PENDING;
338 desc->status &= ~IRQ_INPROGRESS;
342 * The ->end() handler has to deal with interrupts which got
343 * disabled while the handler was running.
345 desc->chip->end(irq);
346 spin_unlock(&desc->lock);
353 #ifdef CONFIG_TRACE_IRQFLAGS
354 void early_init_irq_lock_class(void)
356 #ifndef CONFIG_HAVE_DYN_ARRAY
359 for (i = 0; i < nr_irqs; i++)
360 lockdep_set_class(&irq_desc[i].lock, &irq_desc_lock_class);
365 #ifdef CONFIG_HAVE_DYN_ARRAY
366 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
368 struct irq_desc *desc = irq_to_desc(irq);
369 return desc->kstat_irqs[cpu];
372 EXPORT_SYMBOL(kstat_irqs_cpu);