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>
18 #include <linux/rculist.h>
19 #include <linux/hash.h>
20 #include <linux/bootmem.h>
22 #include "internals.h"
25 * lockdep: we want to handle all irq_desc locks as a single lock-class:
27 struct lock_class_key irq_desc_lock_class;
30 * handle_bad_irq - handle spurious and unhandled irqs
31 * @irq: the interrupt number
32 * @desc: description of the interrupt
34 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
36 void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
38 print_irq_desc(irq, desc);
39 kstat_incr_irqs_this_cpu(irq, desc);
44 * Linux has a controller-independent interrupt architecture.
45 * Every controller has a 'controller-template', that is used
46 * by the main code to do the right thing. Each driver-visible
47 * interrupt source is transparently wired to the appropriate
48 * controller. Thus drivers need not be aware of the
49 * interrupt-controller.
51 * The code is designed to be easily extended with new/different
52 * interrupt controllers, without having to do assembly magic or
53 * having to touch the generic code.
55 * Controller mappings for all interrupt sources:
57 int nr_irqs = NR_IRQS;
58 EXPORT_SYMBOL_GPL(nr_irqs);
60 #ifdef CONFIG_SPARSE_IRQ
62 static struct irq_desc irq_desc_init = {
64 .status = IRQ_DISABLED,
66 .handle_irq = handle_bad_irq,
68 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
71 void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
77 /* Compute how many bytes we need per irq and allocate them */
78 bytes = nr * sizeof(unsigned int);
80 node = cpu_to_node(cpu);
81 ptr = kzalloc_node(bytes, GFP_ATOMIC, node);
82 printk(KERN_DEBUG " alloc kstat_irqs on cpu %d node %d\n", cpu, node);
85 desc->kstat_irqs = (unsigned int *)ptr;
88 static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
90 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
92 spin_lock_init(&desc->lock);
97 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
98 init_kstat_irqs(desc, cpu, nr_cpu_ids);
99 if (!desc->kstat_irqs) {
100 printk(KERN_ERR "can not alloc kstat_irqs\n");
103 if (!init_alloc_desc_masks(desc, cpu, false)) {
104 printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
107 arch_init_chip_data(desc, cpu);
111 * Protect the sparse_irqs:
113 DEFINE_SPINLOCK(sparse_irq_lock);
115 struct irq_desc **irq_desc_ptrs __read_mostly;
117 static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
118 [0 ... NR_IRQS_LEGACY-1] = {
120 .status = IRQ_DISABLED,
121 .chip = &no_irq_chip,
122 .handle_irq = handle_bad_irq,
124 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
128 static unsigned int *kstat_irqs_legacy;
130 int __init early_irq_init(void)
132 struct irq_desc *desc;
136 /* initialize nr_irqs based on nr_cpu_ids */
137 arch_probe_nr_irqs();
138 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
140 desc = irq_desc_legacy;
141 legacy_count = ARRAY_SIZE(irq_desc_legacy);
143 /* allocate irq_desc_ptrs array based on nr_irqs */
144 irq_desc_ptrs = alloc_bootmem(nr_irqs * sizeof(void *));
146 /* allocate based on nr_cpu_ids */
147 /* FIXME: invert kstat_irgs, and it'd be a per_cpu_alloc'd thing */
148 kstat_irqs_legacy = alloc_bootmem(NR_IRQS_LEGACY * nr_cpu_ids *
151 for (i = 0; i < legacy_count; i++) {
153 desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
154 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
155 init_alloc_desc_masks(&desc[i], 0, true);
156 irq_desc_ptrs[i] = desc + i;
159 for (i = legacy_count; i < nr_irqs; i++)
160 irq_desc_ptrs[i] = NULL;
162 return arch_early_irq_init();
165 struct irq_desc *irq_to_desc(unsigned int irq)
167 if (irq_desc_ptrs && irq < nr_irqs)
168 return irq_desc_ptrs[irq];
173 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
175 struct irq_desc *desc;
179 if (irq >= nr_irqs) {
180 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
185 desc = irq_desc_ptrs[irq];
189 spin_lock_irqsave(&sparse_irq_lock, flags);
191 /* We have to check it to avoid races with another CPU */
192 desc = irq_desc_ptrs[irq];
196 node = cpu_to_node(cpu);
197 desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
198 printk(KERN_DEBUG " alloc irq_desc for %d on cpu %d node %d\n",
201 printk(KERN_ERR "can not alloc irq_desc\n");
204 init_one_irq_desc(irq, desc, cpu);
206 irq_desc_ptrs[irq] = desc;
209 spin_unlock_irqrestore(&sparse_irq_lock, flags);
214 #else /* !CONFIG_SPARSE_IRQ */
216 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
217 [0 ... NR_IRQS-1] = {
218 .status = IRQ_DISABLED,
219 .chip = &no_irq_chip,
220 .handle_irq = handle_bad_irq,
222 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
226 int __init early_irq_init(void)
228 struct irq_desc *desc;
232 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
235 count = ARRAY_SIZE(irq_desc);
237 for (i = 0; i < count; i++) {
239 init_alloc_desc_masks(&desc[i], 0, true);
241 return arch_early_irq_init();
244 struct irq_desc *irq_to_desc(unsigned int irq)
246 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
249 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
251 return irq_to_desc(irq);
253 #endif /* !CONFIG_SPARSE_IRQ */
256 * What should we do if we get a hw irq event on an illegal vector?
257 * Each architecture has to answer this themself.
259 static void ack_bad(unsigned int irq)
261 struct irq_desc *desc = irq_to_desc(irq);
263 print_irq_desc(irq, desc);
270 static void noop(unsigned int irq)
274 static unsigned int noop_ret(unsigned int irq)
280 * Generic no controller implementation
282 struct irq_chip no_irq_chip = {
293 * Generic dummy implementation which can be used for
294 * real dumb interrupt sources
296 struct irq_chip dummy_irq_chip = {
309 * Special, empty irq handler:
311 irqreturn_t no_action(int cpl, void *dev_id)
317 * handle_IRQ_event - irq action chain handler
318 * @irq: the interrupt number
319 * @action: the interrupt action chain for this irq
321 * Handles the action chain of an irq event
323 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
325 irqreturn_t ret, retval = IRQ_NONE;
326 unsigned int status = 0;
328 if (!(action->flags & IRQF_DISABLED))
329 local_irq_enable_in_hardirq();
332 ret = action->handler(irq, action->dev_id);
333 if (ret == IRQ_HANDLED)
334 status |= action->flags;
336 action = action->next;
339 if (status & IRQF_SAMPLE_RANDOM)
340 add_interrupt_randomness(irq);
346 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
348 * __do_IRQ - original all in one highlevel IRQ handler
349 * @irq: the interrupt number
351 * __do_IRQ handles all normal device IRQ's (the special
352 * SMP cross-CPU interrupts have their own specific
355 * This is the original x86 implementation which is used for every
358 unsigned int __do_IRQ(unsigned int irq)
360 struct irq_desc *desc = irq_to_desc(irq);
361 struct irqaction *action;
364 kstat_incr_irqs_this_cpu(irq, desc);
366 if (CHECK_IRQ_PER_CPU(desc->status)) {
367 irqreturn_t action_ret;
370 * No locking required for CPU-local interrupts:
372 if (desc->chip->ack) {
373 desc->chip->ack(irq);
375 desc = irq_remap_to_desc(irq, desc);
377 if (likely(!(desc->status & IRQ_DISABLED))) {
378 action_ret = handle_IRQ_event(irq, desc->action);
380 note_interrupt(irq, desc, action_ret);
382 desc->chip->end(irq);
386 spin_lock(&desc->lock);
387 if (desc->chip->ack) {
388 desc->chip->ack(irq);
389 desc = irq_remap_to_desc(irq, desc);
392 * REPLAY is when Linux resends an IRQ that was dropped earlier
393 * WAITING is used by probe to mark irqs that are being tested
395 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
396 status |= IRQ_PENDING; /* we _want_ to handle it */
399 * If the IRQ is disabled for whatever reason, we cannot
400 * use the action we have.
403 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
404 action = desc->action;
405 status &= ~IRQ_PENDING; /* we commit to handling */
406 status |= IRQ_INPROGRESS; /* we are handling it */
408 desc->status = status;
411 * If there is no IRQ handler or it was disabled, exit early.
412 * Since we set PENDING, if another processor is handling
413 * a different instance of this same irq, the other processor
414 * will take care of it.
416 if (unlikely(!action))
420 * Edge triggered interrupts need to remember
422 * This applies to any hw interrupts that allow a second
423 * instance of the same irq to arrive while we are in do_IRQ
424 * or in the handler. But the code here only handles the _second_
425 * instance of the irq, not the third or fourth. So it is mostly
426 * useful for irq hardware that does not mask cleanly in an
430 irqreturn_t action_ret;
432 spin_unlock(&desc->lock);
434 action_ret = handle_IRQ_event(irq, action);
436 note_interrupt(irq, desc, action_ret);
438 spin_lock(&desc->lock);
439 if (likely(!(desc->status & IRQ_PENDING)))
441 desc->status &= ~IRQ_PENDING;
443 desc->status &= ~IRQ_INPROGRESS;
447 * The ->end() handler has to deal with interrupts which got
448 * disabled while the handler was running.
450 desc->chip->end(irq);
451 spin_unlock(&desc->lock);
457 void early_init_irq_lock_class(void)
459 struct irq_desc *desc;
462 for_each_irq_desc(i, desc) {
463 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
467 #ifdef CONFIG_SPARSE_IRQ
468 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
470 struct irq_desc *desc = irq_to_desc(irq);
471 return desc ? desc->kstat_irqs[cpu] : 0;
474 EXPORT_SYMBOL(kstat_irqs_cpu);