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/sched.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/random.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/rculist.h>
21 #include <linux/hash.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 = __RAW_SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
85 void __ref init_kstat_irqs(struct irq_desc *desc, int node, int nr)
89 ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs),
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 node %d\n", node);
98 desc->kstat_irqs = ptr;
102 static void init_one_irq_desc(int irq, struct irq_desc *desc, int node)
104 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
106 raw_spin_lock_init(&desc->lock);
111 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
112 init_kstat_irqs(desc, node, nr_cpu_ids);
113 if (!desc->kstat_irqs) {
114 printk(KERN_ERR "can not alloc kstat_irqs\n");
117 if (!alloc_desc_masks(desc, node, false)) {
118 printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
121 init_desc_masks(desc);
122 arch_init_chip_data(desc, node);
126 * Protect the sparse_irqs:
128 DEFINE_RAW_SPINLOCK(sparse_irq_lock);
130 struct irq_desc **irq_desc_ptrs __read_mostly;
132 static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
133 [0 ... NR_IRQS_LEGACY-1] = {
135 .status = IRQ_DISABLED,
136 .chip = &no_irq_chip,
137 .handle_irq = handle_bad_irq,
139 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
143 static unsigned int *kstat_irqs_legacy;
145 int __init early_irq_init(void)
147 struct irq_desc *desc;
152 init_irq_default_affinity();
154 /* initialize nr_irqs based on nr_cpu_ids */
155 arch_probe_nr_irqs();
156 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
158 desc = irq_desc_legacy;
159 legacy_count = ARRAY_SIZE(irq_desc_legacy);
160 node = first_online_node;
162 /* allocate irq_desc_ptrs array based on nr_irqs */
163 irq_desc_ptrs = kcalloc(nr_irqs, sizeof(void *), GFP_NOWAIT);
165 /* allocate based on nr_cpu_ids */
166 kstat_irqs_legacy = kzalloc_node(NR_IRQS_LEGACY * nr_cpu_ids *
167 sizeof(int), GFP_NOWAIT, node);
169 for (i = 0; i < legacy_count; i++) {
174 desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
175 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
176 alloc_desc_masks(&desc[i], node, true);
177 init_desc_masks(&desc[i]);
178 irq_desc_ptrs[i] = desc + i;
181 for (i = legacy_count; i < nr_irqs; i++)
182 irq_desc_ptrs[i] = NULL;
184 return arch_early_irq_init();
187 struct irq_desc *irq_to_desc(unsigned int irq)
189 if (irq_desc_ptrs && irq < nr_irqs)
190 return irq_desc_ptrs[irq];
195 struct irq_desc * __ref irq_to_desc_alloc_node(unsigned int irq, int node)
197 struct irq_desc *desc;
200 if (irq >= nr_irqs) {
201 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
206 desc = irq_desc_ptrs[irq];
210 raw_spin_lock_irqsave(&sparse_irq_lock, flags);
212 /* We have to check it to avoid races with another CPU */
213 desc = irq_desc_ptrs[irq];
217 desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
219 printk(KERN_DEBUG " alloc irq_desc for %d on node %d\n", irq, node);
221 printk(KERN_ERR "can not alloc irq_desc\n");
224 init_one_irq_desc(irq, desc, node);
226 irq_desc_ptrs[irq] = desc;
229 raw_spin_unlock_irqrestore(&sparse_irq_lock, flags);
234 #else /* !CONFIG_SPARSE_IRQ */
236 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
237 [0 ... NR_IRQS-1] = {
238 .status = IRQ_DISABLED,
239 .chip = &no_irq_chip,
240 .handle_irq = handle_bad_irq,
242 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
246 static unsigned int kstat_irqs_all[NR_IRQS][NR_CPUS];
247 int __init early_irq_init(void)
249 struct irq_desc *desc;
253 init_irq_default_affinity();
255 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
258 count = ARRAY_SIZE(irq_desc);
260 for (i = 0; i < count; i++) {
262 alloc_desc_masks(&desc[i], 0, true);
263 init_desc_masks(&desc[i]);
264 desc[i].kstat_irqs = kstat_irqs_all[i];
266 return arch_early_irq_init();
269 struct irq_desc *irq_to_desc(unsigned int irq)
271 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
274 struct irq_desc *irq_to_desc_alloc_node(unsigned int irq, int node)
276 return irq_to_desc(irq);
278 #endif /* !CONFIG_SPARSE_IRQ */
280 void clear_kstat_irqs(struct irq_desc *desc)
282 memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
286 * What should we do if we get a hw irq event on an illegal vector?
287 * Each architecture has to answer this themself.
289 static void ack_bad(unsigned int irq)
291 struct irq_desc *desc = irq_to_desc(irq);
293 print_irq_desc(irq, desc);
300 static void noop(unsigned int irq)
304 static unsigned int noop_ret(unsigned int irq)
310 * Generic no controller implementation
312 struct irq_chip no_irq_chip = {
323 * Generic dummy implementation which can be used for
324 * real dumb interrupt sources
326 struct irq_chip dummy_irq_chip = {
339 * Special, empty irq handler:
341 irqreturn_t no_action(int cpl, void *dev_id)
346 static void warn_no_thread(unsigned int irq, struct irqaction *action)
348 if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags))
351 printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD "
352 "but no thread function available.", irq, action->name);
356 * handle_IRQ_event - irq action chain handler
357 * @irq: the interrupt number
358 * @action: the interrupt action chain for this irq
360 * Handles the action chain of an irq event
362 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
364 irqreturn_t ret, retval = IRQ_NONE;
365 unsigned int status = 0;
367 if (!(action->flags & IRQF_DISABLED))
368 local_irq_enable_in_hardirq();
371 trace_irq_handler_entry(irq, action);
372 ret = action->handler(irq, action->dev_id);
373 trace_irq_handler_exit(irq, action, ret);
376 case IRQ_WAKE_THREAD:
378 * Set result to handled so the spurious check
384 * Catch drivers which return WAKE_THREAD but
385 * did not set up a thread function
387 if (unlikely(!action->thread_fn)) {
388 warn_no_thread(irq, action);
393 * Wake up the handler thread for this
394 * action. In case the thread crashed and was
395 * killed we just pretend that we handled the
396 * interrupt. The hardirq handler above has
397 * disabled the device interrupt, so no irq
400 if (likely(!test_bit(IRQTF_DIED,
401 &action->thread_flags))) {
402 set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
403 wake_up_process(action->thread);
406 /* Fall through to add to randomness */
408 status |= action->flags;
416 action = action->next;
419 if (status & IRQF_SAMPLE_RANDOM)
420 add_interrupt_randomness(irq);
426 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
428 #ifdef CONFIG_ENABLE_WARN_DEPRECATED
429 # warning __do_IRQ is deprecated. Please convert to proper flow handlers
433 * __do_IRQ - original all in one highlevel IRQ handler
434 * @irq: the interrupt number
436 * __do_IRQ handles all normal device IRQ's (the special
437 * SMP cross-CPU interrupts have their own specific
440 * This is the original x86 implementation which is used for every
443 unsigned int __do_IRQ(unsigned int irq)
445 struct irq_desc *desc = irq_to_desc(irq);
446 struct irqaction *action;
449 kstat_incr_irqs_this_cpu(irq, desc);
451 if (CHECK_IRQ_PER_CPU(desc->status)) {
452 irqreturn_t action_ret;
455 * No locking required for CPU-local interrupts:
458 desc->chip->ack(irq);
459 if (likely(!(desc->status & IRQ_DISABLED))) {
460 action_ret = handle_IRQ_event(irq, desc->action);
462 note_interrupt(irq, desc, action_ret);
464 desc->chip->end(irq);
468 raw_spin_lock(&desc->lock);
470 desc->chip->ack(irq);
472 * REPLAY is when Linux resends an IRQ that was dropped earlier
473 * WAITING is used by probe to mark irqs that are being tested
475 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
476 status |= IRQ_PENDING; /* we _want_ to handle it */
479 * If the IRQ is disabled for whatever reason, we cannot
480 * use the action we have.
483 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
484 action = desc->action;
485 status &= ~IRQ_PENDING; /* we commit to handling */
486 status |= IRQ_INPROGRESS; /* we are handling it */
488 desc->status = status;
491 * If there is no IRQ handler or it was disabled, exit early.
492 * Since we set PENDING, if another processor is handling
493 * a different instance of this same irq, the other processor
494 * will take care of it.
496 if (unlikely(!action))
500 * Edge triggered interrupts need to remember
502 * This applies to any hw interrupts that allow a second
503 * instance of the same irq to arrive while we are in do_IRQ
504 * or in the handler. But the code here only handles the _second_
505 * instance of the irq, not the third or fourth. So it is mostly
506 * useful for irq hardware that does not mask cleanly in an
510 irqreturn_t action_ret;
512 raw_spin_unlock(&desc->lock);
514 action_ret = handle_IRQ_event(irq, action);
516 note_interrupt(irq, desc, action_ret);
518 raw_spin_lock(&desc->lock);
519 if (likely(!(desc->status & IRQ_PENDING)))
521 desc->status &= ~IRQ_PENDING;
523 desc->status &= ~IRQ_INPROGRESS;
527 * The ->end() handler has to deal with interrupts which got
528 * disabled while the handler was running.
530 desc->chip->end(irq);
531 raw_spin_unlock(&desc->lock);
537 void early_init_irq_lock_class(void)
539 struct irq_desc *desc;
542 for_each_irq_desc(i, desc) {
543 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
547 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
549 struct irq_desc *desc = irq_to_desc(irq);
550 return desc ? desc->kstat_irqs[cpu] : 0;
552 EXPORT_SYMBOL(kstat_irqs_cpu);