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 <trace/irq.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
73 static struct irq_desc irq_desc_init = {
75 .status = IRQ_DISABLED,
77 .handle_irq = handle_bad_irq,
79 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
81 .affinity = CPU_MASK_ALL
85 void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
91 /* Compute how many bytes we need per irq and allocate them */
92 bytes = nr * sizeof(unsigned int);
94 node = cpu_to_node(cpu);
95 ptr = kzalloc_node(bytes, GFP_ATOMIC, node);
96 printk(KERN_DEBUG " alloc kstat_irqs on cpu %d node %d\n", cpu, node);
99 desc->kstat_irqs = (unsigned int *)ptr;
102 static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
104 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
106 spin_lock_init(&desc->lock);
111 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
112 init_kstat_irqs(desc, cpu, nr_cpu_ids);
113 if (!desc->kstat_irqs) {
114 printk(KERN_ERR "can not alloc kstat_irqs\n");
117 arch_init_chip_data(desc, cpu);
121 * Protect the sparse_irqs:
123 DEFINE_SPINLOCK(sparse_irq_lock);
125 struct irq_desc *irq_desc_ptrs[NR_IRQS] __read_mostly;
127 static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
128 [0 ... NR_IRQS_LEGACY-1] = {
130 .status = IRQ_DISABLED,
131 .chip = &no_irq_chip,
132 .handle_irq = handle_bad_irq,
134 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
136 .affinity = CPU_MASK_ALL
141 /* FIXME: use bootmem alloc ...*/
142 static unsigned int kstat_irqs_legacy[NR_IRQS_LEGACY][NR_CPUS];
144 int __init early_irq_init(void)
146 struct irq_desc *desc;
150 init_irq_default_affinity();
152 desc = irq_desc_legacy;
153 legacy_count = ARRAY_SIZE(irq_desc_legacy);
155 for (i = 0; i < legacy_count; i++) {
157 desc[i].kstat_irqs = kstat_irqs_legacy[i];
158 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
160 irq_desc_ptrs[i] = desc + i;
163 for (i = legacy_count; i < NR_IRQS; i++)
164 irq_desc_ptrs[i] = NULL;
166 return arch_early_irq_init();
169 struct irq_desc *irq_to_desc(unsigned int irq)
171 return (irq < NR_IRQS) ? irq_desc_ptrs[irq] : NULL;
174 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
176 struct irq_desc *desc;
180 if (irq >= NR_IRQS) {
181 printk(KERN_WARNING "irq >= NR_IRQS in irq_to_desc_alloc: %d %d\n",
187 desc = irq_desc_ptrs[irq];
191 spin_lock_irqsave(&sparse_irq_lock, flags);
193 /* We have to check it to avoid races with another CPU */
194 desc = irq_desc_ptrs[irq];
198 node = cpu_to_node(cpu);
199 desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
200 printk(KERN_DEBUG " alloc irq_desc for %d on cpu %d node %d\n",
203 printk(KERN_ERR "can not alloc irq_desc\n");
206 init_one_irq_desc(irq, desc, cpu);
208 irq_desc_ptrs[irq] = desc;
211 spin_unlock_irqrestore(&sparse_irq_lock, flags);
216 #else /* !CONFIG_SPARSE_IRQ */
218 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
219 [0 ... NR_IRQS-1] = {
220 .status = IRQ_DISABLED,
221 .chip = &no_irq_chip,
222 .handle_irq = handle_bad_irq,
224 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
226 .affinity = CPU_MASK_ALL
231 int __init early_irq_init(void)
233 struct irq_desc *desc;
237 init_irq_default_affinity();
240 count = ARRAY_SIZE(irq_desc);
242 for (i = 0; i < count; i++)
245 return arch_early_irq_init();
248 struct irq_desc *irq_to_desc(unsigned int irq)
250 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
253 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
255 return irq_to_desc(irq);
257 #endif /* !CONFIG_SPARSE_IRQ */
260 * What should we do if we get a hw irq event on an illegal vector?
261 * Each architecture has to answer this themself.
263 static void ack_bad(unsigned int irq)
265 struct irq_desc *desc = irq_to_desc(irq);
267 print_irq_desc(irq, desc);
274 static void noop(unsigned int irq)
278 static unsigned int noop_ret(unsigned int irq)
284 * Generic no controller implementation
286 struct irq_chip no_irq_chip = {
297 * Generic dummy implementation which can be used for
298 * real dumb interrupt sources
300 struct irq_chip dummy_irq_chip = {
313 * Special, empty irq handler:
315 irqreturn_t no_action(int cpl, void *dev_id)
320 DEFINE_TRACE(irq_handler_entry);
321 DEFINE_TRACE(irq_handler_exit);
324 * handle_IRQ_event - irq action chain handler
325 * @irq: the interrupt number
326 * @action: the interrupt action chain for this irq
328 * Handles the action chain of an irq event
330 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
332 irqreturn_t ret, retval = IRQ_NONE;
333 unsigned int status = 0;
335 if (!(action->flags & IRQF_DISABLED))
336 local_irq_enable_in_hardirq();
339 trace_irq_handler_entry(irq, action);
340 ret = action->handler(irq, action->dev_id);
341 trace_irq_handler_exit(irq, action, ret);
342 if (ret == IRQ_HANDLED)
343 status |= action->flags;
345 action = action->next;
348 if (status & IRQF_SAMPLE_RANDOM)
349 add_interrupt_randomness(irq);
355 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
357 * __do_IRQ - original all in one highlevel IRQ handler
358 * @irq: the interrupt number
360 * __do_IRQ handles all normal device IRQ's (the special
361 * SMP cross-CPU interrupts have their own specific
364 * This is the original x86 implementation which is used for every
367 unsigned int __do_IRQ(unsigned int irq)
369 struct irq_desc *desc = irq_to_desc(irq);
370 struct irqaction *action;
373 kstat_incr_irqs_this_cpu(irq, desc);
375 if (CHECK_IRQ_PER_CPU(desc->status)) {
376 irqreturn_t action_ret;
379 * No locking required for CPU-local interrupts:
381 if (desc->chip->ack) {
382 desc->chip->ack(irq);
384 desc = irq_remap_to_desc(irq, desc);
386 if (likely(!(desc->status & IRQ_DISABLED))) {
387 action_ret = handle_IRQ_event(irq, desc->action);
389 note_interrupt(irq, desc, action_ret);
391 desc->chip->end(irq);
395 spin_lock(&desc->lock);
396 if (desc->chip->ack) {
397 desc->chip->ack(irq);
398 desc = irq_remap_to_desc(irq, desc);
401 * REPLAY is when Linux resends an IRQ that was dropped earlier
402 * WAITING is used by probe to mark irqs that are being tested
404 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
405 status |= IRQ_PENDING; /* we _want_ to handle it */
408 * If the IRQ is disabled for whatever reason, we cannot
409 * use the action we have.
412 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
413 action = desc->action;
414 status &= ~IRQ_PENDING; /* we commit to handling */
415 status |= IRQ_INPROGRESS; /* we are handling it */
417 desc->status = status;
420 * If there is no IRQ handler or it was disabled, exit early.
421 * Since we set PENDING, if another processor is handling
422 * a different instance of this same irq, the other processor
423 * will take care of it.
425 if (unlikely(!action))
429 * Edge triggered interrupts need to remember
431 * This applies to any hw interrupts that allow a second
432 * instance of the same irq to arrive while we are in do_IRQ
433 * or in the handler. But the code here only handles the _second_
434 * instance of the irq, not the third or fourth. So it is mostly
435 * useful for irq hardware that does not mask cleanly in an
439 irqreturn_t action_ret;
441 spin_unlock(&desc->lock);
443 action_ret = handle_IRQ_event(irq, action);
445 note_interrupt(irq, desc, action_ret);
447 spin_lock(&desc->lock);
448 if (likely(!(desc->status & IRQ_PENDING)))
450 desc->status &= ~IRQ_PENDING;
452 desc->status &= ~IRQ_INPROGRESS;
456 * The ->end() handler has to deal with interrupts which got
457 * disabled while the handler was running.
459 desc->chip->end(irq);
460 spin_unlock(&desc->lock);
466 void early_init_irq_lock_class(void)
468 struct irq_desc *desc;
471 for_each_irq_desc(i, desc) {
472 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
476 #ifdef CONFIG_SPARSE_IRQ
477 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
479 struct irq_desc *desc = irq_to_desc(irq);
480 return desc ? desc->kstat_irqs[cpu] : 0;
483 EXPORT_SYMBOL(kstat_irqs_cpu);