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 <linux/bootmem.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
74 static struct irq_desc irq_desc_init
= {
76 .status
= IRQ_DISABLED
,
78 .handle_irq
= handle_bad_irq
,
80 .lock
= __SPIN_LOCK_UNLOCKED(irq_desc_init
.lock
),
83 void init_kstat_irqs(struct irq_desc
*desc
, int cpu
, int nr
)
88 node
= cpu_to_node(cpu
);
89 ptr
= kzalloc_node(nr
* sizeof(*desc
->kstat_irqs
), GFP_ATOMIC
, node
);
92 * don't overwite if can not get new one
93 * init_copy_kstat_irqs() could still use old one
96 printk(KERN_DEBUG
" alloc kstat_irqs on cpu %d node %d\n",
98 desc
->kstat_irqs
= 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 if (!init_alloc_desc_masks(desc
, cpu
, false)) {
118 printk(KERN_ERR
"can not alloc irq_desc cpumasks\n");
121 arch_init_chip_data(desc
, cpu
);
125 * Protect the sparse_irqs:
127 DEFINE_SPINLOCK(sparse_irq_lock
);
129 struct irq_desc
**irq_desc_ptrs __read_mostly
;
131 static struct irq_desc irq_desc_legacy
[NR_IRQS_LEGACY
] __cacheline_aligned_in_smp
= {
132 [0 ... NR_IRQS_LEGACY
-1] = {
134 .status
= IRQ_DISABLED
,
135 .chip
= &no_irq_chip
,
136 .handle_irq
= handle_bad_irq
,
138 .lock
= __SPIN_LOCK_UNLOCKED(irq_desc_init
.lock
),
142 static unsigned int *kstat_irqs_legacy
;
144 int __init
early_irq_init(void)
146 struct irq_desc
*desc
;
150 init_irq_default_affinity();
152 /* initialize nr_irqs based on nr_cpu_ids */
153 arch_probe_nr_irqs();
154 printk(KERN_INFO
"NR_IRQS:%d nr_irqs:%d\n", NR_IRQS
, nr_irqs
);
156 desc
= irq_desc_legacy
;
157 legacy_count
= ARRAY_SIZE(irq_desc_legacy
);
159 /* allocate irq_desc_ptrs array based on nr_irqs */
160 irq_desc_ptrs
= alloc_bootmem(nr_irqs
* sizeof(void *));
162 /* allocate based on nr_cpu_ids */
163 /* FIXME: invert kstat_irgs, and it'd be a per_cpu_alloc'd thing */
164 kstat_irqs_legacy
= alloc_bootmem(NR_IRQS_LEGACY
* nr_cpu_ids
*
167 for (i
= 0; i
< legacy_count
; i
++) {
169 desc
[i
].kstat_irqs
= kstat_irqs_legacy
+ i
* nr_cpu_ids
;
170 lockdep_set_class(&desc
[i
].lock
, &irq_desc_lock_class
);
171 init_alloc_desc_masks(&desc
[i
], 0, true);
172 irq_desc_ptrs
[i
] = desc
+ i
;
175 for (i
= legacy_count
; i
< nr_irqs
; i
++)
176 irq_desc_ptrs
[i
] = NULL
;
178 return arch_early_irq_init();
181 struct irq_desc
*irq_to_desc(unsigned int irq
)
183 if (irq_desc_ptrs
&& irq
< nr_irqs
)
184 return irq_desc_ptrs
[irq
];
189 struct irq_desc
*irq_to_desc_alloc_cpu(unsigned int irq
, int cpu
)
191 struct irq_desc
*desc
;
195 if (irq
>= nr_irqs
) {
196 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
201 desc
= irq_desc_ptrs
[irq
];
205 spin_lock_irqsave(&sparse_irq_lock
, flags
);
207 /* We have to check it to avoid races with another CPU */
208 desc
= irq_desc_ptrs
[irq
];
212 node
= cpu_to_node(cpu
);
213 desc
= kzalloc_node(sizeof(*desc
), GFP_ATOMIC
, node
);
214 printk(KERN_DEBUG
" alloc irq_desc for %d on cpu %d node %d\n",
217 printk(KERN_ERR
"can not alloc irq_desc\n");
220 init_one_irq_desc(irq
, desc
, cpu
);
222 irq_desc_ptrs
[irq
] = desc
;
225 spin_unlock_irqrestore(&sparse_irq_lock
, flags
);
230 #else /* !CONFIG_SPARSE_IRQ */
232 struct irq_desc irq_desc
[NR_IRQS
] __cacheline_aligned_in_smp
= {
233 [0 ... NR_IRQS
-1] = {
234 .status
= IRQ_DISABLED
,
235 .chip
= &no_irq_chip
,
236 .handle_irq
= handle_bad_irq
,
238 .lock
= __SPIN_LOCK_UNLOCKED(irq_desc
->lock
),
242 static unsigned int kstat_irqs_all
[NR_IRQS
][NR_CPUS
];
243 int __init
early_irq_init(void)
245 struct irq_desc
*desc
;
249 init_irq_default_affinity();
251 printk(KERN_INFO
"NR_IRQS:%d\n", NR_IRQS
);
254 count
= ARRAY_SIZE(irq_desc
);
256 for (i
= 0; i
< count
; i
++) {
258 init_alloc_desc_masks(&desc
[i
], 0, true);
259 desc
[i
].kstat_irqs
= kstat_irqs_all
[i
];
261 return arch_early_irq_init();
264 struct irq_desc
*irq_to_desc(unsigned int irq
)
266 return (irq
< NR_IRQS
) ? irq_desc
+ irq
: NULL
;
269 struct irq_desc
*irq_to_desc_alloc_cpu(unsigned int irq
, int cpu
)
271 return irq_to_desc(irq
);
273 #endif /* !CONFIG_SPARSE_IRQ */
275 void clear_kstat_irqs(struct irq_desc
*desc
)
277 memset(desc
->kstat_irqs
, 0, nr_cpu_ids
* sizeof(*(desc
->kstat_irqs
)));
281 * What should we do if we get a hw irq event on an illegal vector?
282 * Each architecture has to answer this themself.
284 static void ack_bad(unsigned int irq
)
286 struct irq_desc
*desc
= irq_to_desc(irq
);
288 print_irq_desc(irq
, desc
);
295 static void noop(unsigned int irq
)
299 static unsigned int noop_ret(unsigned int irq
)
305 * Generic no controller implementation
307 struct irq_chip no_irq_chip
= {
318 * Generic dummy implementation which can be used for
319 * real dumb interrupt sources
321 struct irq_chip dummy_irq_chip
= {
334 * Special, empty irq handler:
336 irqreturn_t
no_action(int cpl
, void *dev_id
)
342 * handle_IRQ_event - irq action chain handler
343 * @irq: the interrupt number
344 * @action: the interrupt action chain for this irq
346 * Handles the action chain of an irq event
348 irqreturn_t
handle_IRQ_event(unsigned int irq
, struct irqaction
*action
)
350 irqreturn_t ret
, retval
= IRQ_NONE
;
351 unsigned int status
= 0;
353 WARN_ONCE(!in_irq(), "BUG: IRQ handler called from non-hardirq context!");
355 if (!(action
->flags
& IRQF_DISABLED
))
356 local_irq_enable_in_hardirq();
359 ret
= action
->handler(irq
, action
->dev_id
);
362 case IRQ_WAKE_THREAD
:
364 * Wake up the handler thread for this
365 * action. In case the thread crashed and was
366 * killed we just pretend that we handled the
367 * interrupt. The hardirq handler above has
368 * disabled the device interrupt, so no irq
371 if (likely(!test_bit(IRQTF_DIED
,
372 &action
->thread_flags
))) {
373 set_bit(IRQTF_RUNTHREAD
, &action
->thread_flags
);
374 wake_up_process(action
->thread
);
378 * Set it to handled so the spurious check
382 /* Fall through to add to randomness */
384 status
|= action
->flags
;
392 action
= action
->next
;
395 if (status
& IRQF_SAMPLE_RANDOM
)
396 add_interrupt_randomness(irq
);
402 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
404 #ifdef CONFIG_ENABLE_WARN_DEPRECATED
405 # warning __do_IRQ is deprecated. Please convert to proper flow handlers
409 * __do_IRQ - original all in one highlevel IRQ handler
410 * @irq: the interrupt number
412 * __do_IRQ handles all normal device IRQ's (the special
413 * SMP cross-CPU interrupts have their own specific
416 * This is the original x86 implementation which is used for every
419 unsigned int __do_IRQ(unsigned int irq
)
421 struct irq_desc
*desc
= irq_to_desc(irq
);
422 struct irqaction
*action
;
425 kstat_incr_irqs_this_cpu(irq
, desc
);
427 if (CHECK_IRQ_PER_CPU(desc
->status
)) {
428 irqreturn_t action_ret
;
431 * No locking required for CPU-local interrupts:
433 if (desc
->chip
->ack
) {
434 desc
->chip
->ack(irq
);
436 desc
= irq_remap_to_desc(irq
, desc
);
438 if (likely(!(desc
->status
& IRQ_DISABLED
))) {
439 action_ret
= handle_IRQ_event(irq
, desc
->action
);
441 note_interrupt(irq
, desc
, action_ret
);
443 desc
->chip
->end(irq
);
447 spin_lock(&desc
->lock
);
448 if (desc
->chip
->ack
) {
449 desc
->chip
->ack(irq
);
450 desc
= irq_remap_to_desc(irq
, desc
);
453 * REPLAY is when Linux resends an IRQ that was dropped earlier
454 * WAITING is used by probe to mark irqs that are being tested
456 status
= desc
->status
& ~(IRQ_REPLAY
| IRQ_WAITING
);
457 status
|= IRQ_PENDING
; /* we _want_ to handle it */
460 * If the IRQ is disabled for whatever reason, we cannot
461 * use the action we have.
464 if (likely(!(status
& (IRQ_DISABLED
| IRQ_INPROGRESS
)))) {
465 action
= desc
->action
;
466 status
&= ~IRQ_PENDING
; /* we commit to handling */
467 status
|= IRQ_INPROGRESS
; /* we are handling it */
469 desc
->status
= status
;
472 * If there is no IRQ handler or it was disabled, exit early.
473 * Since we set PENDING, if another processor is handling
474 * a different instance of this same irq, the other processor
475 * will take care of it.
477 if (unlikely(!action
))
481 * Edge triggered interrupts need to remember
483 * This applies to any hw interrupts that allow a second
484 * instance of the same irq to arrive while we are in do_IRQ
485 * or in the handler. But the code here only handles the _second_
486 * instance of the irq, not the third or fourth. So it is mostly
487 * useful for irq hardware that does not mask cleanly in an
491 irqreturn_t action_ret
;
493 spin_unlock(&desc
->lock
);
495 action_ret
= handle_IRQ_event(irq
, action
);
497 note_interrupt(irq
, desc
, action_ret
);
499 spin_lock(&desc
->lock
);
500 if (likely(!(desc
->status
& IRQ_PENDING
)))
502 desc
->status
&= ~IRQ_PENDING
;
504 desc
->status
&= ~IRQ_INPROGRESS
;
508 * The ->end() handler has to deal with interrupts which got
509 * disabled while the handler was running.
511 desc
->chip
->end(irq
);
512 spin_unlock(&desc
->lock
);
518 void early_init_irq_lock_class(void)
520 struct irq_desc
*desc
;
523 for_each_irq_desc(i
, desc
) {
524 lockdep_set_class(&desc
->lock
, &irq_desc_lock_class
);
528 unsigned int kstat_irqs_cpu(unsigned int irq
, int cpu
)
530 struct irq_desc
*desc
= irq_to_desc(irq
);
531 return desc
? desc
->kstat_irqs
[cpu
] : 0;
533 EXPORT_SYMBOL(kstat_irqs_cpu
);