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/slab.h>
15 #include <linux/module.h>
16 #include <linux/random.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/rculist.h>
20 #include <linux/hash.h>
21 #include <linux/bootmem.h>
22 #include <trace/events/irq.h>
24 #include "internals.h"
27 * lockdep: we want to handle all irq_desc locks as a single lock-class:
29 struct lock_class_key irq_desc_lock_class;
32 * handle_bad_irq - handle spurious and unhandled irqs
33 * @irq: the interrupt number
34 * @desc: description of the interrupt
36 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
38 void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
40 print_irq_desc(irq, desc);
41 kstat_incr_irqs_this_cpu(irq, desc);
45 #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
46 static void __init init_irq_default_affinity(void)
48 alloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
49 cpumask_setall(irq_default_affinity);
52 static void __init init_irq_default_affinity(void)
58 * Linux has a controller-independent interrupt architecture.
59 * Every controller has a 'controller-template', that is used
60 * by the main code to do the right thing. Each driver-visible
61 * interrupt source is transparently wired to the appropriate
62 * controller. Thus drivers need not be aware of the
63 * interrupt-controller.
65 * The code is designed to be easily extended with new/different
66 * interrupt controllers, without having to do assembly magic or
67 * having to touch the generic code.
69 * Controller mappings for all interrupt sources:
71 int nr_irqs = NR_IRQS;
72 EXPORT_SYMBOL_GPL(nr_irqs);
74 #ifdef CONFIG_SPARSE_IRQ
76 static struct irq_desc irq_desc_init = {
78 .status = IRQ_DISABLED,
80 .handle_irq = handle_bad_irq,
82 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
85 void __ref init_kstat_irqs(struct irq_desc *desc, int node, int nr)
89 if (slab_is_available())
90 ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs),
93 ptr = alloc_bootmem_node(NODE_DATA(node),
94 nr * sizeof(*desc->kstat_irqs));
97 * don't overwite if can not get new one
98 * init_copy_kstat_irqs() could still use old one
101 printk(KERN_DEBUG " alloc kstat_irqs on node %d\n", node);
102 desc->kstat_irqs = ptr;
106 static void init_one_irq_desc(int irq, struct irq_desc *desc, int node)
108 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
110 spin_lock_init(&desc->lock);
115 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
116 init_kstat_irqs(desc, node, nr_cpu_ids);
117 if (!desc->kstat_irqs) {
118 printk(KERN_ERR "can not alloc kstat_irqs\n");
121 if (!alloc_desc_masks(desc, node, false)) {
122 printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
125 init_desc_masks(desc);
126 arch_init_chip_data(desc, node);
130 * Protect the sparse_irqs:
132 DEFINE_SPINLOCK(sparse_irq_lock);
134 struct irq_desc **irq_desc_ptrs __read_mostly;
136 static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
137 [0 ... NR_IRQS_LEGACY-1] = {
139 .status = IRQ_DISABLED,
140 .chip = &no_irq_chip,
141 .handle_irq = handle_bad_irq,
143 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
147 static unsigned int *kstat_irqs_legacy;
149 int __init early_irq_init(void)
151 struct irq_desc *desc;
156 init_irq_default_affinity();
158 /* initialize nr_irqs based on nr_cpu_ids */
159 arch_probe_nr_irqs();
160 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
162 desc = irq_desc_legacy;
163 legacy_count = ARRAY_SIZE(irq_desc_legacy);
164 node = first_online_node;
166 /* allocate irq_desc_ptrs array based on nr_irqs */
167 irq_desc_ptrs = kcalloc(nr_irqs, sizeof(void *), GFP_NOWAIT);
169 /* allocate based on nr_cpu_ids */
170 kstat_irqs_legacy = kzalloc_node(NR_IRQS_LEGACY * nr_cpu_ids *
171 sizeof(int), GFP_NOWAIT, node);
173 for (i = 0; i < legacy_count; i++) {
178 desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
179 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
180 alloc_desc_masks(&desc[i], node, true);
181 init_desc_masks(&desc[i]);
182 irq_desc_ptrs[i] = desc + i;
185 for (i = legacy_count; i < nr_irqs; i++)
186 irq_desc_ptrs[i] = NULL;
188 return arch_early_irq_init();
191 struct irq_desc *irq_to_desc(unsigned int irq)
193 if (irq_desc_ptrs && irq < nr_irqs)
194 return irq_desc_ptrs[irq];
199 struct irq_desc * __ref irq_to_desc_alloc_node(unsigned int irq, int node)
201 struct irq_desc *desc;
204 if (irq >= nr_irqs) {
205 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
210 desc = irq_desc_ptrs[irq];
214 spin_lock_irqsave(&sparse_irq_lock, flags);
216 /* We have to check it to avoid races with another CPU */
217 desc = irq_desc_ptrs[irq];
221 if (slab_is_available())
222 desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
224 desc = alloc_bootmem_node(NODE_DATA(node), sizeof(*desc));
226 printk(KERN_DEBUG " alloc irq_desc for %d on node %d\n", irq, node);
228 printk(KERN_ERR "can not alloc irq_desc\n");
231 init_one_irq_desc(irq, desc, node);
233 irq_desc_ptrs[irq] = desc;
236 spin_unlock_irqrestore(&sparse_irq_lock, flags);
241 #else /* !CONFIG_SPARSE_IRQ */
243 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
244 [0 ... NR_IRQS-1] = {
245 .status = IRQ_DISABLED,
246 .chip = &no_irq_chip,
247 .handle_irq = handle_bad_irq,
249 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
253 static unsigned int kstat_irqs_all[NR_IRQS][NR_CPUS];
254 int __init early_irq_init(void)
256 struct irq_desc *desc;
260 init_irq_default_affinity();
262 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
265 count = ARRAY_SIZE(irq_desc);
267 for (i = 0; i < count; i++) {
269 alloc_desc_masks(&desc[i], 0, true);
270 init_desc_masks(&desc[i]);
271 desc[i].kstat_irqs = kstat_irqs_all[i];
273 return arch_early_irq_init();
276 struct irq_desc *irq_to_desc(unsigned int irq)
278 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
281 struct irq_desc *irq_to_desc_alloc_node(unsigned int irq, int node)
283 return irq_to_desc(irq);
285 #endif /* !CONFIG_SPARSE_IRQ */
287 void clear_kstat_irqs(struct irq_desc *desc)
289 memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
293 * What should we do if we get a hw irq event on an illegal vector?
294 * Each architecture has to answer this themself.
296 static void ack_bad(unsigned int irq)
298 struct irq_desc *desc = irq_to_desc(irq);
300 print_irq_desc(irq, desc);
307 static void noop(unsigned int irq)
311 static unsigned int noop_ret(unsigned int irq)
317 * Generic no controller implementation
319 struct irq_chip no_irq_chip = {
330 * Generic dummy implementation which can be used for
331 * real dumb interrupt sources
333 struct irq_chip dummy_irq_chip = {
346 * Special, empty irq handler:
348 irqreturn_t no_action(int cpl, void *dev_id)
353 static void warn_no_thread(unsigned int irq, struct irqaction *action)
355 if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags))
358 printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD "
359 "but no thread function available.", irq, action->name);
363 * handle_IRQ_event - irq action chain handler
364 * @irq: the interrupt number
365 * @action: the interrupt action chain for this irq
367 * Handles the action chain of an irq event
369 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
371 irqreturn_t ret, retval = IRQ_NONE;
372 unsigned int status = 0;
374 if (!(action->flags & IRQF_DISABLED))
375 local_irq_enable_in_hardirq();
378 trace_irq_handler_entry(irq, action);
379 ret = action->handler(irq, action->dev_id);
380 trace_irq_handler_exit(irq, action, ret);
383 case IRQ_WAKE_THREAD:
385 * Set result to handled so the spurious check
391 * Catch drivers which return WAKE_THREAD but
392 * did not set up a thread function
394 if (unlikely(!action->thread_fn)) {
395 warn_no_thread(irq, action);
400 * Wake up the handler thread for this
401 * action. In case the thread crashed and was
402 * killed we just pretend that we handled the
403 * interrupt. The hardirq handler above has
404 * disabled the device interrupt, so no irq
407 if (likely(!test_bit(IRQTF_DIED,
408 &action->thread_flags))) {
409 set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
410 wake_up_process(action->thread);
413 /* Fall through to add to randomness */
415 status |= action->flags;
423 action = action->next;
426 if (status & IRQF_SAMPLE_RANDOM)
427 add_interrupt_randomness(irq);
433 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
435 #ifdef CONFIG_ENABLE_WARN_DEPRECATED
436 # warning __do_IRQ is deprecated. Please convert to proper flow handlers
440 * __do_IRQ - original all in one highlevel IRQ handler
441 * @irq: the interrupt number
443 * __do_IRQ handles all normal device IRQ's (the special
444 * SMP cross-CPU interrupts have their own specific
447 * This is the original x86 implementation which is used for every
450 unsigned int __do_IRQ(unsigned int irq)
452 struct irq_desc *desc = irq_to_desc(irq);
453 struct irqaction *action;
456 kstat_incr_irqs_this_cpu(irq, desc);
458 if (CHECK_IRQ_PER_CPU(desc->status)) {
459 irqreturn_t action_ret;
462 * No locking required for CPU-local interrupts:
465 desc->chip->ack(irq);
466 if (likely(!(desc->status & IRQ_DISABLED))) {
467 action_ret = handle_IRQ_event(irq, desc->action);
469 note_interrupt(irq, desc, action_ret);
471 desc->chip->end(irq);
475 spin_lock(&desc->lock);
477 desc->chip->ack(irq);
479 * REPLAY is when Linux resends an IRQ that was dropped earlier
480 * WAITING is used by probe to mark irqs that are being tested
482 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
483 status |= IRQ_PENDING; /* we _want_ to handle it */
486 * If the IRQ is disabled for whatever reason, we cannot
487 * use the action we have.
490 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
491 action = desc->action;
492 status &= ~IRQ_PENDING; /* we commit to handling */
493 status |= IRQ_INPROGRESS; /* we are handling it */
495 desc->status = status;
498 * If there is no IRQ handler or it was disabled, exit early.
499 * Since we set PENDING, if another processor is handling
500 * a different instance of this same irq, the other processor
501 * will take care of it.
503 if (unlikely(!action))
507 * Edge triggered interrupts need to remember
509 * This applies to any hw interrupts that allow a second
510 * instance of the same irq to arrive while we are in do_IRQ
511 * or in the handler. But the code here only handles the _second_
512 * instance of the irq, not the third or fourth. So it is mostly
513 * useful for irq hardware that does not mask cleanly in an
517 irqreturn_t action_ret;
519 spin_unlock(&desc->lock);
521 action_ret = handle_IRQ_event(irq, action);
523 note_interrupt(irq, desc, action_ret);
525 spin_lock(&desc->lock);
526 if (likely(!(desc->status & IRQ_PENDING)))
528 desc->status &= ~IRQ_PENDING;
530 desc->status &= ~IRQ_INPROGRESS;
534 * The ->end() handler has to deal with interrupts which got
535 * disabled while the handler was running.
537 desc->chip->end(irq);
538 spin_unlock(&desc->lock);
544 void early_init_irq_lock_class(void)
546 struct irq_desc *desc;
549 for_each_irq_desc(i, desc) {
550 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
554 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
556 struct irq_desc *desc = irq_to_desc(irq);
557 return desc ? desc->kstat_irqs[cpu] : 0;
559 EXPORT_SYMBOL(kstat_irqs_cpu);