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>
19 #include "internals.h"
21 #ifdef CONFIG_TRACE_IRQFLAGS
24 * lockdep: we want to handle all irq_desc locks as a single lock-class:
26 static 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.
37 handle_bad_irq(unsigned int irq
, struct irq_desc
*desc
)
39 print_irq_desc(irq
, desc
);
40 kstat_irqs_this_cpu(desc
)++;
45 * Linux has a controller-independent interrupt architecture.
46 * Every controller has a 'controller-template', that is used
47 * by the main code to do the right thing. Each driver-visible
48 * interrupt source is transparently wired to the appropriate
49 * controller. Thus drivers need not be aware of the
50 * interrupt-controller.
52 * The code is designed to be easily extended with new/different
53 * interrupt controllers, without having to do assembly magic or
54 * having to touch the generic code.
56 * Controller mappings for all interrupt sources:
58 int nr_irqs
= NR_IRQS
;
59 EXPORT_SYMBOL_GPL(nr_irqs
);
61 #ifdef CONFIG_HAVE_DYN_ARRAY
62 static struct irq_desc irq_desc_init
= {
64 .status
= IRQ_DISABLED
,
66 .handle_irq
= handle_bad_irq
,
68 .lock
= __SPIN_LOCK_UNLOCKED(irq_desc_init
.lock
),
70 .affinity
= CPU_MASK_ALL
75 static void init_one_irq_desc(struct irq_desc
*desc
)
77 memcpy(desc
, &irq_desc_init
, sizeof(struct irq_desc
));
78 #ifdef CONFIG_TRACE_IRQFLAGS
79 lockdep_set_class(&desc
->lock
, &irq_desc_lock_class
);
83 extern int after_bootmem
;
84 extern void *__alloc_bootmem_nopanic(unsigned long size
,
88 static void init_kstat_irqs(struct irq_desc
*desc
, int nr_desc
, int nr
)
90 unsigned long bytes
, total_bytes
;
95 /* Compute how many bytes we need per irq and allocate them */
96 bytes
= nr
* sizeof(unsigned int);
97 total_bytes
= bytes
* nr_desc
;
99 ptr
= kzalloc(total_bytes
, GFP_ATOMIC
);
101 ptr
= __alloc_bootmem_nopanic(total_bytes
, PAGE_SIZE
, 0);
104 panic(" can not allocate kstat_irqs\n");
107 printk(KERN_DEBUG
"kstat_irqs ==> [%#lx - %#lx]\n", phys
, phys
+ total_bytes
);
109 for (i
= 0; i
< nr_desc
; i
++) {
110 desc
[i
].kstat_irqs
= (unsigned int *)ptr
;
115 #ifdef CONFIG_HAVE_SPARSE_IRQ
116 static struct irq_desc
*sparse_irqs_free
;
117 struct irq_desc
*sparse_irqs
;
120 static void __init
init_work(void *data
)
122 struct dyn_array
*da
= data
;
124 struct irq_desc
*desc
;
128 for (i
= 0; i
< *da
->nr
; i
++) {
129 init_one_irq_desc(&desc
[i
]);
130 #ifndef CONFIG_HAVE_SPARSE_IRQ
135 /* init kstat_irqs, nr_cpu_ids is ready already */
136 init_kstat_irqs(desc
, *da
->nr
, nr_cpu_ids
);
138 #ifdef CONFIG_HAVE_SPARSE_IRQ
139 for (i
= 1; i
< *da
->nr
; i
++)
140 desc
[i
-1].next
= &desc
[i
];
142 sparse_irqs_free
= sparse_irqs
;
147 #ifdef CONFIG_HAVE_SPARSE_IRQ
148 static int nr_irq_desc
= 32;
150 static int __init
parse_nr_irq_desc(char *arg
)
153 nr_irq_desc
= simple_strtoul(arg
, NULL
, 0);
157 early_param("nr_irq_desc", parse_nr_irq_desc
);
159 DEFINE_DYN_ARRAY(sparse_irqs
, sizeof(struct irq_desc
), nr_irq_desc
, PAGE_SIZE
, init_work
);
161 struct irq_desc
*irq_to_desc(unsigned int irq
)
163 struct irq_desc
*desc
;
167 if (desc
->irq
== irq
)
174 struct irq_desc
*irq_to_desc_alloc(unsigned int irq
)
176 struct irq_desc
*desc
, *desc_pri
;
180 desc_pri
= desc
= sparse_irqs
;
182 if (desc
->irq
== irq
)
191 * we run out of pre-allocate ones, allocate more
193 if (!sparse_irqs_free
) {
195 unsigned long total_bytes
;
197 printk(KERN_DEBUG
"try to get more irq_desc %d\n", nr_irq_desc
);
199 total_bytes
= sizeof(struct irq_desc
) * nr_irq_desc
;
201 desc
= kzalloc(total_bytes
, GFP_ATOMIC
);
203 desc
= __alloc_bootmem_nopanic(total_bytes
, PAGE_SIZE
, 0);
206 panic("please boot with nr_irq_desc= %d\n", count
* 2);
209 printk(KERN_DEBUG
"irq_desc ==> [%#lx - %#lx]\n", phys
, phys
+ total_bytes
);
211 for (i
= 0; i
< nr_irq_desc
; i
++)
212 init_one_irq_desc(&desc
[i
]);
214 for (i
= 1; i
< nr_irq_desc
; i
++)
215 desc
[i
-1].next
= &desc
[i
];
217 /* init kstat_irqs, nr_cpu_ids is ready already */
218 init_kstat_irqs(desc
, nr_irq_desc
, nr_cpu_ids
);
220 sparse_irqs_free
= desc
;
223 desc
= sparse_irqs_free
;
224 sparse_irqs_free
= sparse_irqs_free
->next
;
227 desc_pri
->next
= desc
;
231 printk(KERN_DEBUG
"found new irq_desc for irq %d\n", desc
->irq
);
232 #ifdef CONFIG_HAVE_SPARSE_IRQ_DEBUG
234 /* dump the results */
235 struct irq_desc
*desc
;
237 unsigned long bytes
= sizeof(struct irq_desc
);
240 printk(KERN_DEBUG
"=========================== %d\n", irq
);
241 printk(KERN_DEBUG
"irq_desc dump after get that for %d\n", irq
);
242 for_each_irq_desc(irqx
, desc
) {
244 printk(KERN_DEBUG
"irq_desc %d ==> [%#lx - %#lx]\n", irqx
, phys
, phys
+ bytes
);
246 printk(KERN_DEBUG
"===========================\n");
252 struct irq_desc
*irq_desc
;
253 DEFINE_DYN_ARRAY(irq_desc
, sizeof(struct irq_desc
), nr_irqs
, PAGE_SIZE
, init_work
);
259 struct irq_desc irq_desc
[NR_IRQS
] __cacheline_aligned_in_smp
= {
260 [0 ... NR_IRQS
-1] = {
261 .status
= IRQ_DISABLED
,
262 .chip
= &no_irq_chip
,
263 .handle_irq
= handle_bad_irq
,
265 .lock
= __SPIN_LOCK_UNLOCKED(sparse_irqs
->lock
),
267 .affinity
= CPU_MASK_ALL
274 #ifndef CONFIG_HAVE_SPARSE_IRQ
275 struct irq_desc
*irq_to_desc(unsigned int irq
)
278 return &irq_desc
[irq
];
282 struct irq_desc
*irq_to_desc_alloc(unsigned int irq
)
284 return irq_to_desc(irq
);
289 * What should we do if we get a hw irq event on an illegal vector?
290 * Each architecture has to answer this themself.
292 static void ack_bad(unsigned int irq
)
294 struct irq_desc
*desc
;
296 desc
= irq_to_desc(irq
);
297 print_irq_desc(irq
, desc
);
304 static void noop(unsigned int irq
)
308 static unsigned int noop_ret(unsigned int irq
)
314 * Generic no controller implementation
316 struct irq_chip no_irq_chip
= {
327 * Generic dummy implementation which can be used for
328 * real dumb interrupt sources
330 struct irq_chip dummy_irq_chip
= {
343 * Special, empty irq handler:
345 irqreturn_t
no_action(int cpl
, void *dev_id
)
351 * handle_IRQ_event - irq action chain handler
352 * @irq: the interrupt number
353 * @action: the interrupt action chain for this irq
355 * Handles the action chain of an irq event
357 irqreturn_t
handle_IRQ_event(unsigned int irq
, struct irqaction
*action
)
359 irqreturn_t ret
, retval
= IRQ_NONE
;
360 unsigned int status
= 0;
362 if (!(action
->flags
& IRQF_DISABLED
))
363 local_irq_enable_in_hardirq();
366 ret
= action
->handler(irq
, action
->dev_id
);
367 if (ret
== IRQ_HANDLED
)
368 status
|= action
->flags
;
370 action
= action
->next
;
373 if (status
& IRQF_SAMPLE_RANDOM
)
374 add_interrupt_randomness(irq
);
380 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
382 * __do_IRQ - original all in one highlevel IRQ handler
383 * @irq: the interrupt number
385 * __do_IRQ handles all normal device IRQ's (the special
386 * SMP cross-CPU interrupts have their own specific
389 * This is the original x86 implementation which is used for every
392 unsigned int __do_IRQ(unsigned int irq
)
394 struct irq_desc
*desc
= irq_to_desc(irq
);
395 struct irqaction
*action
;
398 kstat_irqs_this_cpu(desc
)++;
399 if (CHECK_IRQ_PER_CPU(desc
->status
)) {
400 irqreturn_t action_ret
;
403 * No locking required for CPU-local interrupts:
406 desc
->chip
->ack(irq
);
407 if (likely(!(desc
->status
& IRQ_DISABLED
))) {
408 action_ret
= handle_IRQ_event(irq
, desc
->action
);
410 note_interrupt(irq
, desc
, action_ret
);
412 desc
->chip
->end(irq
);
416 spin_lock(&desc
->lock
);
418 desc
->chip
->ack(irq
);
420 * REPLAY is when Linux resends an IRQ that was dropped earlier
421 * WAITING is used by probe to mark irqs that are being tested
423 status
= desc
->status
& ~(IRQ_REPLAY
| IRQ_WAITING
);
424 status
|= IRQ_PENDING
; /* we _want_ to handle it */
427 * If the IRQ is disabled for whatever reason, we cannot
428 * use the action we have.
431 if (likely(!(status
& (IRQ_DISABLED
| IRQ_INPROGRESS
)))) {
432 action
= desc
->action
;
433 status
&= ~IRQ_PENDING
; /* we commit to handling */
434 status
|= IRQ_INPROGRESS
; /* we are handling it */
436 desc
->status
= status
;
439 * If there is no IRQ handler or it was disabled, exit early.
440 * Since we set PENDING, if another processor is handling
441 * a different instance of this same irq, the other processor
442 * will take care of it.
444 if (unlikely(!action
))
448 * Edge triggered interrupts need to remember
450 * This applies to any hw interrupts that allow a second
451 * instance of the same irq to arrive while we are in do_IRQ
452 * or in the handler. But the code here only handles the _second_
453 * instance of the irq, not the third or fourth. So it is mostly
454 * useful for irq hardware that does not mask cleanly in an
458 irqreturn_t action_ret
;
460 spin_unlock(&desc
->lock
);
462 action_ret
= handle_IRQ_event(irq
, action
);
464 note_interrupt(irq
, desc
, action_ret
);
466 spin_lock(&desc
->lock
);
467 if (likely(!(desc
->status
& IRQ_PENDING
)))
469 desc
->status
&= ~IRQ_PENDING
;
471 desc
->status
&= ~IRQ_INPROGRESS
;
475 * The ->end() handler has to deal with interrupts which got
476 * disabled while the handler was running.
478 desc
->chip
->end(irq
);
479 spin_unlock(&desc
->lock
);
486 #ifdef CONFIG_TRACE_IRQFLAGS
487 void early_init_irq_lock_class(void)
489 #ifndef CONFIG_HAVE_DYN_ARRAY
492 for (i
= 0; i
< nr_irqs
; i
++)
493 lockdep_set_class(&irq_desc
[i
].lock
, &irq_desc_lock_class
);
498 unsigned int kstat_irqs_cpu(unsigned int irq
, int cpu
)
500 struct irq_desc
*desc
= irq_to_desc(irq
);
501 return desc
->kstat_irqs
[cpu
];
503 EXPORT_SYMBOL(kstat_irqs_cpu
);