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 <linux/radix-tree.h>
23 #include <trace/events/irq.h>
25 #include "internals.h"
28 * lockdep: we want to handle all irq_desc locks as a single lock-class:
30 struct lock_class_key irq_desc_lock_class
;
33 * handle_bad_irq - handle spurious and unhandled irqs
34 * @irq: the interrupt number
35 * @desc: description of the interrupt
37 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
39 void handle_bad_irq(unsigned int irq
, struct irq_desc
*desc
)
41 print_irq_desc(irq
, desc
);
42 kstat_incr_irqs_this_cpu(irq
, desc
);
46 #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
47 static void __init
init_irq_default_affinity(void)
49 alloc_cpumask_var(&irq_default_affinity
, GFP_NOWAIT
);
50 cpumask_setall(irq_default_affinity
);
53 static void __init
init_irq_default_affinity(void)
59 * Linux has a controller-independent interrupt architecture.
60 * Every controller has a 'controller-template', that is used
61 * by the main code to do the right thing. Each driver-visible
62 * interrupt source is transparently wired to the appropriate
63 * controller. Thus drivers need not be aware of the
64 * interrupt-controller.
66 * The code is designed to be easily extended with new/different
67 * interrupt controllers, without having to do assembly magic or
68 * having to touch the generic code.
70 * Controller mappings for all interrupt sources:
72 int nr_irqs
= NR_IRQS
;
73 EXPORT_SYMBOL_GPL(nr_irqs
);
75 #ifdef CONFIG_SPARSE_IRQ
77 static struct irq_desc irq_desc_init
= {
79 .status
= IRQ_DISABLED
,
81 .handle_irq
= handle_bad_irq
,
83 .lock
= __RAW_SPIN_LOCK_UNLOCKED(irq_desc_init
.lock
),
86 void __ref
init_kstat_irqs(struct irq_desc
*desc
, int node
, int nr
)
90 ptr
= kzalloc_node(nr
* sizeof(*desc
->kstat_irqs
),
94 * don't overwite if can not get new one
95 * init_copy_kstat_irqs() could still use old one
98 printk(KERN_DEBUG
" alloc kstat_irqs on node %d\n", node
);
99 desc
->kstat_irqs
= ptr
;
103 static void init_one_irq_desc(int irq
, struct irq_desc
*desc
, int node
)
105 memcpy(desc
, &irq_desc_init
, sizeof(struct irq_desc
));
107 raw_spin_lock_init(&desc
->lock
);
112 lockdep_set_class(&desc
->lock
, &irq_desc_lock_class
);
113 init_kstat_irqs(desc
, node
, nr_cpu_ids
);
114 if (!desc
->kstat_irqs
) {
115 printk(KERN_ERR
"can not alloc kstat_irqs\n");
118 if (!alloc_desc_masks(desc
, node
, false)) {
119 printk(KERN_ERR
"can not alloc irq_desc cpumasks\n");
122 init_desc_masks(desc
);
123 arch_init_chip_data(desc
, node
);
127 * Protect the sparse_irqs:
129 DEFINE_RAW_SPINLOCK(sparse_irq_lock
);
131 static RADIX_TREE(irq_desc_tree
, GFP_ATOMIC
);
133 static void set_irq_desc(unsigned int irq
, struct irq_desc
*desc
)
135 radix_tree_insert(&irq_desc_tree
, irq
, desc
);
138 struct irq_desc
*irq_to_desc(unsigned int irq
)
140 return radix_tree_lookup(&irq_desc_tree
, irq
);
143 void replace_irq_desc(unsigned int irq
, struct irq_desc
*desc
)
147 ptr
= radix_tree_lookup_slot(&irq_desc_tree
, irq
);
149 radix_tree_replace_slot(ptr
, desc
);
152 static struct irq_desc irq_desc_legacy
[NR_IRQS_LEGACY
] __cacheline_aligned_in_smp
= {
153 [0 ... NR_IRQS_LEGACY
-1] = {
155 .status
= IRQ_DISABLED
,
156 .chip
= &no_irq_chip
,
157 .handle_irq
= handle_bad_irq
,
159 .lock
= __RAW_SPIN_LOCK_UNLOCKED(irq_desc_init
.lock
),
163 static unsigned int *kstat_irqs_legacy
;
165 int __init
early_irq_init(void)
167 struct irq_desc
*desc
;
172 init_irq_default_affinity();
174 /* initialize nr_irqs based on nr_cpu_ids */
175 arch_probe_nr_irqs();
176 printk(KERN_INFO
"NR_IRQS:%d nr_irqs:%d\n", NR_IRQS
, nr_irqs
);
178 desc
= irq_desc_legacy
;
179 legacy_count
= ARRAY_SIZE(irq_desc_legacy
);
180 node
= first_online_node
;
182 /* allocate based on nr_cpu_ids */
183 kstat_irqs_legacy
= kzalloc_node(NR_IRQS_LEGACY
* nr_cpu_ids
*
184 sizeof(int), GFP_NOWAIT
, node
);
186 for (i
= 0; i
< legacy_count
; i
++) {
191 desc
[i
].kstat_irqs
= kstat_irqs_legacy
+ i
* nr_cpu_ids
;
192 lockdep_set_class(&desc
[i
].lock
, &irq_desc_lock_class
);
193 alloc_desc_masks(&desc
[i
], node
, true);
194 init_desc_masks(&desc
[i
]);
195 set_irq_desc(i
, &desc
[i
]);
198 return arch_early_irq_init();
201 struct irq_desc
* __ref
irq_to_desc_alloc_node(unsigned int irq
, int node
)
203 struct irq_desc
*desc
;
206 if (irq
>= nr_irqs
) {
207 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
212 desc
= irq_to_desc(irq
);
216 raw_spin_lock_irqsave(&sparse_irq_lock
, flags
);
218 /* We have to check it to avoid races with another CPU */
219 desc
= irq_to_desc(irq
);
223 desc
= kzalloc_node(sizeof(*desc
), GFP_ATOMIC
, node
);
225 printk(KERN_DEBUG
" alloc irq_desc for %d on node %d\n", irq
, node
);
227 printk(KERN_ERR
"can not alloc irq_desc\n");
230 init_one_irq_desc(irq
, desc
, node
);
232 set_irq_desc(irq
, desc
);
235 raw_spin_unlock_irqrestore(&sparse_irq_lock
, flags
);
240 #else /* !CONFIG_SPARSE_IRQ */
242 struct irq_desc irq_desc
[NR_IRQS
] __cacheline_aligned_in_smp
= {
243 [0 ... NR_IRQS
-1] = {
244 .status
= IRQ_DISABLED
,
245 .chip
= &no_irq_chip
,
246 .handle_irq
= handle_bad_irq
,
248 .lock
= __RAW_SPIN_LOCK_UNLOCKED(irq_desc
->lock
),
252 static unsigned int kstat_irqs_all
[NR_IRQS
][NR_CPUS
];
253 int __init
early_irq_init(void)
255 struct irq_desc
*desc
;
259 init_irq_default_affinity();
261 printk(KERN_INFO
"NR_IRQS:%d\n", NR_IRQS
);
264 count
= ARRAY_SIZE(irq_desc
);
266 for (i
= 0; i
< count
; i
++) {
268 alloc_desc_masks(&desc
[i
], 0, true);
269 init_desc_masks(&desc
[i
]);
270 desc
[i
].kstat_irqs
= kstat_irqs_all
[i
];
272 return arch_early_irq_init();
275 struct irq_desc
*irq_to_desc(unsigned int irq
)
277 return (irq
< NR_IRQS
) ? irq_desc
+ irq
: NULL
;
280 struct irq_desc
*irq_to_desc_alloc_node(unsigned int irq
, int node
)
282 return irq_to_desc(irq
);
284 #endif /* !CONFIG_SPARSE_IRQ */
286 void clear_kstat_irqs(struct irq_desc
*desc
)
288 memset(desc
->kstat_irqs
, 0, nr_cpu_ids
* sizeof(*(desc
->kstat_irqs
)));
292 * What should we do if we get a hw irq event on an illegal vector?
293 * Each architecture has to answer this themself.
295 static void ack_bad(unsigned int irq
)
297 struct irq_desc
*desc
= irq_to_desc(irq
);
299 print_irq_desc(irq
, desc
);
306 static void noop(unsigned int irq
)
310 static unsigned int noop_ret(unsigned int irq
)
316 * Generic no controller implementation
318 struct irq_chip no_irq_chip
= {
329 * Generic dummy implementation which can be used for
330 * real dumb interrupt sources
332 struct irq_chip dummy_irq_chip
= {
345 * Special, empty irq handler:
347 irqreturn_t
no_action(int cpl
, void *dev_id
)
352 static void warn_no_thread(unsigned int irq
, struct irqaction
*action
)
354 if (test_and_set_bit(IRQTF_WARNED
, &action
->thread_flags
))
357 printk(KERN_WARNING
"IRQ %d device %s returned IRQ_WAKE_THREAD "
358 "but no thread function available.", irq
, action
->name
);
362 * handle_IRQ_event - irq action chain handler
363 * @irq: the interrupt number
364 * @action: the interrupt action chain for this irq
366 * Handles the action chain of an irq event
368 irqreturn_t
handle_IRQ_event(unsigned int irq
, struct irqaction
*action
)
370 irqreturn_t ret
, retval
= IRQ_NONE
;
371 unsigned int status
= 0;
373 if (!(action
->flags
& IRQF_DISABLED
))
374 local_irq_enable_in_hardirq();
377 trace_irq_handler_entry(irq
, action
);
378 ret
= action
->handler(irq
, action
->dev_id
);
379 trace_irq_handler_exit(irq
, action
, ret
);
382 case IRQ_WAKE_THREAD
:
384 * Set result to handled so the spurious check
390 * Catch drivers which return WAKE_THREAD but
391 * did not set up a thread function
393 if (unlikely(!action
->thread_fn
)) {
394 warn_no_thread(irq
, action
);
399 * Wake up the handler thread for this
400 * action. In case the thread crashed and was
401 * killed we just pretend that we handled the
402 * interrupt. The hardirq handler above has
403 * disabled the device interrupt, so no irq
406 if (likely(!test_bit(IRQTF_DIED
,
407 &action
->thread_flags
))) {
408 set_bit(IRQTF_RUNTHREAD
, &action
->thread_flags
);
409 wake_up_process(action
->thread
);
412 /* Fall through to add to randomness */
414 status
|= action
->flags
;
422 action
= action
->next
;
425 if (status
& IRQF_SAMPLE_RANDOM
)
426 add_interrupt_randomness(irq
);
432 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
434 #ifdef CONFIG_ENABLE_WARN_DEPRECATED
435 # warning __do_IRQ is deprecated. Please convert to proper flow handlers
439 * __do_IRQ - original all in one highlevel IRQ handler
440 * @irq: the interrupt number
442 * __do_IRQ handles all normal device IRQ's (the special
443 * SMP cross-CPU interrupts have their own specific
446 * This is the original x86 implementation which is used for every
449 unsigned int __do_IRQ(unsigned int irq
)
451 struct irq_desc
*desc
= irq_to_desc(irq
);
452 struct irqaction
*action
;
455 kstat_incr_irqs_this_cpu(irq
, desc
);
457 if (CHECK_IRQ_PER_CPU(desc
->status
)) {
458 irqreturn_t action_ret
;
461 * No locking required for CPU-local interrupts:
464 desc
->chip
->ack(irq
);
465 if (likely(!(desc
->status
& IRQ_DISABLED
))) {
466 action_ret
= handle_IRQ_event(irq
, desc
->action
);
468 note_interrupt(irq
, desc
, action_ret
);
470 desc
->chip
->end(irq
);
474 raw_spin_lock(&desc
->lock
);
476 desc
->chip
->ack(irq
);
478 * REPLAY is when Linux resends an IRQ that was dropped earlier
479 * WAITING is used by probe to mark irqs that are being tested
481 status
= desc
->status
& ~(IRQ_REPLAY
| IRQ_WAITING
);
482 status
|= IRQ_PENDING
; /* we _want_ to handle it */
485 * If the IRQ is disabled for whatever reason, we cannot
486 * use the action we have.
489 if (likely(!(status
& (IRQ_DISABLED
| IRQ_INPROGRESS
)))) {
490 action
= desc
->action
;
491 status
&= ~IRQ_PENDING
; /* we commit to handling */
492 status
|= IRQ_INPROGRESS
; /* we are handling it */
494 desc
->status
= status
;
497 * If there is no IRQ handler or it was disabled, exit early.
498 * Since we set PENDING, if another processor is handling
499 * a different instance of this same irq, the other processor
500 * will take care of it.
502 if (unlikely(!action
))
506 * Edge triggered interrupts need to remember
508 * This applies to any hw interrupts that allow a second
509 * instance of the same irq to arrive while we are in do_IRQ
510 * or in the handler. But the code here only handles the _second_
511 * instance of the irq, not the third or fourth. So it is mostly
512 * useful for irq hardware that does not mask cleanly in an
516 irqreturn_t action_ret
;
518 raw_spin_unlock(&desc
->lock
);
520 action_ret
= handle_IRQ_event(irq
, action
);
522 note_interrupt(irq
, desc
, action_ret
);
524 raw_spin_lock(&desc
->lock
);
525 if (likely(!(desc
->status
& IRQ_PENDING
)))
527 desc
->status
&= ~IRQ_PENDING
;
529 desc
->status
&= ~IRQ_INPROGRESS
;
533 * The ->end() handler has to deal with interrupts which got
534 * disabled while the handler was running.
536 desc
->chip
->end(irq
);
537 raw_spin_unlock(&desc
->lock
);
543 void early_init_irq_lock_class(void)
545 struct irq_desc
*desc
;
548 for_each_irq_desc(i
, desc
) {
549 lockdep_set_class(&desc
->lock
, &irq_desc_lock_class
);
553 unsigned int kstat_irqs_cpu(unsigned int irq
, int cpu
)
555 struct irq_desc
*desc
= irq_to_desc(irq
);
556 return desc
? desc
->kstat_irqs
[cpu
] : 0;
558 EXPORT_SYMBOL(kstat_irqs_cpu
);