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);
43 #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
44 static void __init init_irq_default_affinity(void)
46 alloc_bootmem_cpumask_var(&irq_default_affinity);
47 cpumask_setall(irq_default_affinity);
50 static void __init init_irq_default_affinity(void)
56 * Linux has a controller-independent interrupt architecture.
57 * Every controller has a 'controller-template', that is used
58 * by the main code to do the right thing. Each driver-visible
59 * interrupt source is transparently wired to the appropriate
60 * controller. Thus drivers need not be aware of the
61 * interrupt-controller.
63 * The code is designed to be easily extended with new/different
64 * interrupt controllers, without having to do assembly magic or
65 * having to touch the generic code.
67 * Controller mappings for all interrupt sources:
69 int nr_irqs = NR_IRQS;
70 EXPORT_SYMBOL_GPL(nr_irqs);
72 #ifdef CONFIG_SPARSE_IRQ
74 static struct irq_desc irq_desc_init = {
76 .status = IRQ_DISABLED,
78 .handle_irq = handle_bad_irq,
80 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
83 void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
89 /* Compute how many bytes we need per irq and allocate them */
90 bytes = nr * sizeof(unsigned int);
92 node = cpu_to_node(cpu);
93 ptr = kzalloc_node(bytes, GFP_ATOMIC, node);
94 printk(KERN_DEBUG " alloc kstat_irqs on cpu %d node %d\n", cpu, node);
97 desc->kstat_irqs = (unsigned int *)ptr;
100 static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
102 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
104 spin_lock_init(&desc->lock);
109 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
110 init_kstat_irqs(desc, cpu, nr_cpu_ids);
111 if (!desc->kstat_irqs) {
112 printk(KERN_ERR "can not alloc kstat_irqs\n");
115 if (!init_alloc_desc_masks(desc, cpu, false)) {
116 printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
119 arch_init_chip_data(desc, cpu);
123 * Protect the sparse_irqs:
125 DEFINE_SPINLOCK(sparse_irq_lock);
127 struct irq_desc **irq_desc_ptrs __read_mostly;
129 static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
130 [0 ... NR_IRQS_LEGACY-1] = {
132 .status = IRQ_DISABLED,
133 .chip = &no_irq_chip,
134 .handle_irq = handle_bad_irq,
136 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
140 static unsigned int *kstat_irqs_legacy;
142 int __init early_irq_init(void)
144 struct irq_desc *desc;
148 init_irq_default_affinity();
150 /* initialize nr_irqs based on nr_cpu_ids */
151 arch_probe_nr_irqs();
152 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
154 desc = irq_desc_legacy;
155 legacy_count = ARRAY_SIZE(irq_desc_legacy);
157 /* allocate irq_desc_ptrs array based on nr_irqs */
158 irq_desc_ptrs = alloc_bootmem(nr_irqs * sizeof(void *));
160 /* allocate based on nr_cpu_ids */
161 /* FIXME: invert kstat_irgs, and it'd be a per_cpu_alloc'd thing */
162 kstat_irqs_legacy = alloc_bootmem(NR_IRQS_LEGACY * nr_cpu_ids *
165 for (i = 0; i < legacy_count; i++) {
167 desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
168 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
169 init_alloc_desc_masks(&desc[i], 0, true);
170 irq_desc_ptrs[i] = desc + i;
173 for (i = legacy_count; i < nr_irqs; i++)
174 irq_desc_ptrs[i] = NULL;
176 return arch_early_irq_init();
179 struct irq_desc *irq_to_desc(unsigned int irq)
181 if (irq_desc_ptrs && irq < nr_irqs)
182 return irq_desc_ptrs[irq];
187 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
189 struct irq_desc *desc;
193 if (irq >= nr_irqs) {
194 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
199 desc = irq_desc_ptrs[irq];
203 spin_lock_irqsave(&sparse_irq_lock, flags);
205 /* We have to check it to avoid races with another CPU */
206 desc = irq_desc_ptrs[irq];
210 node = cpu_to_node(cpu);
211 desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
212 printk(KERN_DEBUG " alloc irq_desc for %d on cpu %d node %d\n",
215 printk(KERN_ERR "can not alloc irq_desc\n");
218 init_one_irq_desc(irq, desc, cpu);
220 irq_desc_ptrs[irq] = desc;
223 spin_unlock_irqrestore(&sparse_irq_lock, flags);
228 #else /* !CONFIG_SPARSE_IRQ */
230 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
231 [0 ... NR_IRQS-1] = {
232 .status = IRQ_DISABLED,
233 .chip = &no_irq_chip,
234 .handle_irq = handle_bad_irq,
236 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
240 int __init early_irq_init(void)
242 struct irq_desc *desc;
246 init_irq_default_affinity();
248 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
251 count = ARRAY_SIZE(irq_desc);
253 for (i = 0; i < count; i++) {
255 init_alloc_desc_masks(&desc[i], 0, true);
257 return arch_early_irq_init();
260 struct irq_desc *irq_to_desc(unsigned int irq)
262 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
265 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
267 return irq_to_desc(irq);
269 #endif /* !CONFIG_SPARSE_IRQ */
272 * What should we do if we get a hw irq event on an illegal vector?
273 * Each architecture has to answer this themself.
275 static void ack_bad(unsigned int irq)
277 struct irq_desc *desc = irq_to_desc(irq);
279 print_irq_desc(irq, desc);
286 static void noop(unsigned int irq)
290 static unsigned int noop_ret(unsigned int irq)
296 * Generic no controller implementation
298 struct irq_chip no_irq_chip = {
309 * Generic dummy implementation which can be used for
310 * real dumb interrupt sources
312 struct irq_chip dummy_irq_chip = {
325 * Special, empty irq handler:
327 irqreturn_t no_action(int cpl, void *dev_id)
333 * handle_IRQ_event - irq action chain handler
334 * @irq: the interrupt number
335 * @action: the interrupt action chain for this irq
337 * Handles the action chain of an irq event
339 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
341 irqreturn_t ret, retval = IRQ_NONE;
342 unsigned int status = 0;
344 if (!(action->flags & IRQF_DISABLED))
345 local_irq_enable_in_hardirq();
348 ret = action->handler(irq, action->dev_id);
349 if (ret == IRQ_HANDLED)
350 status |= action->flags;
352 action = action->next;
355 if (status & IRQF_SAMPLE_RANDOM)
356 add_interrupt_randomness(irq);
362 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
364 * __do_IRQ - original all in one highlevel IRQ handler
365 * @irq: the interrupt number
367 * __do_IRQ handles all normal device IRQ's (the special
368 * SMP cross-CPU interrupts have their own specific
371 * This is the original x86 implementation which is used for every
374 unsigned int __do_IRQ(unsigned int irq)
376 struct irq_desc *desc = irq_to_desc(irq);
377 struct irqaction *action;
380 kstat_incr_irqs_this_cpu(irq, desc);
382 if (CHECK_IRQ_PER_CPU(desc->status)) {
383 irqreturn_t action_ret;
386 * No locking required for CPU-local interrupts:
388 if (desc->chip->ack) {
389 desc->chip->ack(irq);
391 desc = irq_remap_to_desc(irq, desc);
393 if (likely(!(desc->status & IRQ_DISABLED))) {
394 action_ret = handle_IRQ_event(irq, desc->action);
396 note_interrupt(irq, desc, action_ret);
398 desc->chip->end(irq);
402 spin_lock(&desc->lock);
403 if (desc->chip->ack) {
404 desc->chip->ack(irq);
405 desc = irq_remap_to_desc(irq, desc);
408 * REPLAY is when Linux resends an IRQ that was dropped earlier
409 * WAITING is used by probe to mark irqs that are being tested
411 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
412 status |= IRQ_PENDING; /* we _want_ to handle it */
415 * If the IRQ is disabled for whatever reason, we cannot
416 * use the action we have.
419 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
420 action = desc->action;
421 status &= ~IRQ_PENDING; /* we commit to handling */
422 status |= IRQ_INPROGRESS; /* we are handling it */
424 desc->status = status;
427 * If there is no IRQ handler or it was disabled, exit early.
428 * Since we set PENDING, if another processor is handling
429 * a different instance of this same irq, the other processor
430 * will take care of it.
432 if (unlikely(!action))
436 * Edge triggered interrupts need to remember
438 * This applies to any hw interrupts that allow a second
439 * instance of the same irq to arrive while we are in do_IRQ
440 * or in the handler. But the code here only handles the _second_
441 * instance of the irq, not the third or fourth. So it is mostly
442 * useful for irq hardware that does not mask cleanly in an
446 irqreturn_t action_ret;
448 spin_unlock(&desc->lock);
450 action_ret = handle_IRQ_event(irq, action);
452 note_interrupt(irq, desc, action_ret);
454 spin_lock(&desc->lock);
455 if (likely(!(desc->status & IRQ_PENDING)))
457 desc->status &= ~IRQ_PENDING;
459 desc->status &= ~IRQ_INPROGRESS;
463 * The ->end() handler has to deal with interrupts which got
464 * disabled while the handler was running.
466 desc->chip->end(irq);
467 spin_unlock(&desc->lock);
473 void early_init_irq_lock_class(void)
475 struct irq_desc *desc;
478 for_each_irq_desc(i, desc) {
479 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
483 #ifdef CONFIG_SPARSE_IRQ
484 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
486 struct irq_desc *desc = irq_to_desc(irq);
487 return desc ? desc->kstat_irqs[cpu] : 0;
490 EXPORT_SYMBOL(kstat_irqs_cpu);