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>
21 #include "internals.h"
24 * lockdep: we want to handle all irq_desc locks as a single lock-class:
26 struct lock_class_key irq_desc_lock_class;
29 * handle_bad_irq - handle spurious and unhandled irqs
30 * @irq: the interrupt number
31 * @desc: description of the interrupt
33 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
35 void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
37 print_irq_desc(irq, desc);
38 kstat_incr_irqs_this_cpu(irq, desc);
42 #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
43 static void __init init_irq_default_affinity(void)
45 alloc_bootmem_cpumask_var(&irq_default_affinity);
46 cpumask_setall(irq_default_affinity);
49 static void __init init_irq_default_affinity(void)
55 * Linux has a controller-independent interrupt architecture.
56 * Every controller has a 'controller-template', that is used
57 * by the main code to do the right thing. Each driver-visible
58 * interrupt source is transparently wired to the appropriate
59 * controller. Thus drivers need not be aware of the
60 * interrupt-controller.
62 * The code is designed to be easily extended with new/different
63 * interrupt controllers, without having to do assembly magic or
64 * having to touch the generic code.
66 * Controller mappings for all interrupt sources:
68 int nr_irqs = NR_IRQS;
69 EXPORT_SYMBOL_GPL(nr_irqs);
71 #ifdef CONFIG_SPARSE_IRQ
72 static struct irq_desc irq_desc_init = {
74 .status = IRQ_DISABLED,
76 .handle_irq = handle_bad_irq,
78 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
80 .affinity = CPU_MASK_ALL
84 void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
89 node = cpu_to_node(cpu);
90 ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
93 * don't overwite if can not get new one
94 * init_copy_kstat_irqs() could still use old one
97 printk(KERN_DEBUG " alloc kstat_irqs on cpu %d node %d\n",
99 desc->kstat_irqs = ptr;
103 static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
105 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
107 spin_lock_init(&desc->lock);
112 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
113 init_kstat_irqs(desc, cpu, nr_cpu_ids);
114 if (!desc->kstat_irqs) {
115 printk(KERN_ERR "can not alloc kstat_irqs\n");
118 arch_init_chip_data(desc, cpu);
122 * Protect the sparse_irqs:
124 DEFINE_SPINLOCK(sparse_irq_lock);
126 struct irq_desc *irq_desc_ptrs[NR_IRQS] __read_mostly;
128 static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
129 [0 ... NR_IRQS_LEGACY-1] = {
131 .status = IRQ_DISABLED,
132 .chip = &no_irq_chip,
133 .handle_irq = handle_bad_irq,
135 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
137 .affinity = CPU_MASK_ALL
142 /* FIXME: use bootmem alloc ...*/
143 static unsigned int kstat_irqs_legacy[NR_IRQS_LEGACY][NR_CPUS];
145 int __init early_irq_init(void)
147 struct irq_desc *desc;
151 init_irq_default_affinity();
153 desc = irq_desc_legacy;
154 legacy_count = ARRAY_SIZE(irq_desc_legacy);
156 for (i = 0; i < legacy_count; i++) {
158 desc[i].kstat_irqs = kstat_irqs_legacy[i];
159 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
161 irq_desc_ptrs[i] = desc + i;
164 for (i = legacy_count; i < NR_IRQS; i++)
165 irq_desc_ptrs[i] = NULL;
167 return arch_early_irq_init();
170 struct irq_desc *irq_to_desc(unsigned int irq)
172 return (irq < NR_IRQS) ? irq_desc_ptrs[irq] : NULL;
175 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
177 struct irq_desc *desc;
181 if (irq >= NR_IRQS) {
182 printk(KERN_WARNING "irq >= NR_IRQS in irq_to_desc_alloc: %d %d\n",
188 desc = irq_desc_ptrs[irq];
192 spin_lock_irqsave(&sparse_irq_lock, flags);
194 /* We have to check it to avoid races with another CPU */
195 desc = irq_desc_ptrs[irq];
199 node = cpu_to_node(cpu);
200 desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
201 printk(KERN_DEBUG " alloc irq_desc for %d on cpu %d node %d\n",
204 printk(KERN_ERR "can not alloc irq_desc\n");
207 init_one_irq_desc(irq, desc, cpu);
209 irq_desc_ptrs[irq] = desc;
212 spin_unlock_irqrestore(&sparse_irq_lock, flags);
217 #else /* !CONFIG_SPARSE_IRQ */
219 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
220 [0 ... NR_IRQS-1] = {
221 .status = IRQ_DISABLED,
222 .chip = &no_irq_chip,
223 .handle_irq = handle_bad_irq,
225 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
227 .affinity = CPU_MASK_ALL
232 static unsigned int kstat_irqs_all[NR_IRQS][NR_CPUS];
233 int __init early_irq_init(void)
235 struct irq_desc *desc;
239 init_irq_default_affinity();
242 count = ARRAY_SIZE(irq_desc);
244 for (i = 0; i < count; i++) {
246 desc[i].kstat_irqs = kstat_irqs_all[i];
249 return arch_early_irq_init();
252 struct irq_desc *irq_to_desc(unsigned int irq)
254 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
257 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
259 return irq_to_desc(irq);
261 #endif /* !CONFIG_SPARSE_IRQ */
263 void clear_kstat_irqs(struct irq_desc *desc)
265 memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
269 * What should we do if we get a hw irq event on an illegal vector?
270 * Each architecture has to answer this themself.
272 static void ack_bad(unsigned int irq)
274 struct irq_desc *desc = irq_to_desc(irq);
276 print_irq_desc(irq, desc);
283 static void noop(unsigned int irq)
287 static unsigned int noop_ret(unsigned int irq)
293 * Generic no controller implementation
295 struct irq_chip no_irq_chip = {
306 * Generic dummy implementation which can be used for
307 * real dumb interrupt sources
309 struct irq_chip dummy_irq_chip = {
322 * Special, empty irq handler:
324 irqreturn_t no_action(int cpl, void *dev_id)
330 * handle_IRQ_event - irq action chain handler
331 * @irq: the interrupt number
332 * @action: the interrupt action chain for this irq
334 * Handles the action chain of an irq event
336 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
338 irqreturn_t ret, retval = IRQ_NONE;
339 unsigned int status = 0;
341 WARN_ONCE(!in_irq(), "BUG: IRQ handler called from non-hardirq context!");
343 if (!(action->flags & IRQF_DISABLED))
344 local_irq_enable_in_hardirq();
347 ret = action->handler(irq, action->dev_id);
348 if (ret == IRQ_HANDLED)
349 status |= action->flags;
351 action = action->next;
354 if (status & IRQF_SAMPLE_RANDOM)
355 add_interrupt_randomness(irq);
361 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
363 #ifdef CONFIG_ENABLE_WARN_DEPRECATED
364 # warning __do_IRQ is deprecated. Please convert to proper flow handlers
368 * __do_IRQ - original all in one highlevel IRQ handler
369 * @irq: the interrupt number
371 * __do_IRQ handles all normal device IRQ's (the special
372 * SMP cross-CPU interrupts have their own specific
375 * This is the original x86 implementation which is used for every
378 unsigned int __do_IRQ(unsigned int irq)
380 struct irq_desc *desc = irq_to_desc(irq);
381 struct irqaction *action;
384 kstat_incr_irqs_this_cpu(irq, desc);
386 if (CHECK_IRQ_PER_CPU(desc->status)) {
387 irqreturn_t action_ret;
390 * No locking required for CPU-local interrupts:
392 if (desc->chip->ack) {
393 desc->chip->ack(irq);
395 desc = irq_remap_to_desc(irq, desc);
397 if (likely(!(desc->status & IRQ_DISABLED))) {
398 action_ret = handle_IRQ_event(irq, desc->action);
400 note_interrupt(irq, desc, action_ret);
402 desc->chip->end(irq);
406 spin_lock(&desc->lock);
407 if (desc->chip->ack) {
408 desc->chip->ack(irq);
409 desc = irq_remap_to_desc(irq, desc);
412 * REPLAY is when Linux resends an IRQ that was dropped earlier
413 * WAITING is used by probe to mark irqs that are being tested
415 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
416 status |= IRQ_PENDING; /* we _want_ to handle it */
419 * If the IRQ is disabled for whatever reason, we cannot
420 * use the action we have.
423 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
424 action = desc->action;
425 status &= ~IRQ_PENDING; /* we commit to handling */
426 status |= IRQ_INPROGRESS; /* we are handling it */
428 desc->status = status;
431 * If there is no IRQ handler or it was disabled, exit early.
432 * Since we set PENDING, if another processor is handling
433 * a different instance of this same irq, the other processor
434 * will take care of it.
436 if (unlikely(!action))
440 * Edge triggered interrupts need to remember
442 * This applies to any hw interrupts that allow a second
443 * instance of the same irq to arrive while we are in do_IRQ
444 * or in the handler. But the code here only handles the _second_
445 * instance of the irq, not the third or fourth. So it is mostly
446 * useful for irq hardware that does not mask cleanly in an
450 irqreturn_t action_ret;
452 spin_unlock(&desc->lock);
454 action_ret = handle_IRQ_event(irq, action);
456 note_interrupt(irq, desc, action_ret);
458 spin_lock(&desc->lock);
459 if (likely(!(desc->status & IRQ_PENDING)))
461 desc->status &= ~IRQ_PENDING;
463 desc->status &= ~IRQ_INPROGRESS;
467 * The ->end() handler has to deal with interrupts which got
468 * disabled while the handler was running.
470 desc->chip->end(irq);
471 spin_unlock(&desc->lock);
477 void early_init_irq_lock_class(void)
479 struct irq_desc *desc;
482 for_each_irq_desc(i, desc) {
483 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
487 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
489 struct irq_desc *desc = irq_to_desc(irq);
490 return desc ? desc->kstat_irqs[cpu] : 0;
492 EXPORT_SYMBOL(kstat_irqs_cpu);