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"
22 * lockdep: we want to handle all irq_desc locks as a single lock-class:
24 static struct lock_class_key irq_desc_lock_class
;
27 * handle_bad_irq - handle spurious and unhandled irqs
28 * @irq: the interrupt number
29 * @desc: description of the interrupt
31 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
34 handle_bad_irq(unsigned int irq
, struct irq_desc
*desc
)
36 print_irq_desc(irq
, desc
);
37 #ifdef CONFIG_HAVE_DYN_ARRAY
38 kstat_irqs_this_cpu(desc
)++;
40 kstat_irqs_this_cpu(irq
)++;
46 * Linux has a controller-independent interrupt architecture.
47 * Every controller has a 'controller-template', that is used
48 * by the main code to do the right thing. Each driver-visible
49 * interrupt source is transparently wired to the appropriate
50 * controller. Thus drivers need not be aware of the
51 * interrupt-controller.
53 * The code is designed to be easily extended with new/different
54 * interrupt controllers, without having to do assembly magic or
55 * having to touch the generic code.
57 * Controller mappings for all interrupt sources:
59 int nr_irqs
= NR_IRQS
;
60 EXPORT_SYMBOL_GPL(nr_irqs
);
62 #ifdef CONFIG_HAVE_DYN_ARRAY
63 static struct irq_desc irq_desc_init
= {
65 .status
= IRQ_DISABLED
,
67 .handle_irq
= handle_bad_irq
,
69 .lock
= __SPIN_LOCK_UNLOCKED(irq_desc_init
.lock
),
71 .affinity
= CPU_MASK_ALL
76 static void init_one_irq_desc(struct irq_desc
*desc
)
78 memcpy(desc
, &irq_desc_init
, sizeof(struct irq_desc
));
79 lockdep_set_class(&desc
->lock
, &irq_desc_lock_class
);
82 extern int after_bootmem
;
83 extern void *__alloc_bootmem_nopanic(unsigned long size
,
87 static void init_kstat_irqs(struct irq_desc
*desc
, int nr_desc
, int nr
)
89 unsigned long bytes
, total_bytes
;
94 /* Compute how many bytes we need per irq and allocate them */
95 bytes
= nr
* sizeof(unsigned int);
96 total_bytes
= bytes
* nr_desc
;
98 ptr
= kzalloc(total_bytes
, GFP_ATOMIC
);
100 ptr
= __alloc_bootmem_nopanic(total_bytes
, PAGE_SIZE
, 0);
103 panic(" can not allocate kstat_irqs\n");
106 printk(KERN_DEBUG
"kstat_irqs ==> [%#lx - %#lx]\n", phys
, phys
+ total_bytes
);
108 for (i
= 0; i
< nr_desc
; i
++) {
109 desc
[i
].kstat_irqs
= (unsigned int *)ptr
;
115 * Protect the sparse_irqs_free freelist:
117 static DEFINE_SPINLOCK(sparse_irq_lock
);
119 #ifdef CONFIG_HAVE_SPARSE_IRQ
120 static struct irq_desc
*sparse_irqs_free
;
121 struct irq_desc
*sparse_irqs
;
124 static void __init
init_work(void *data
)
126 struct dyn_array
*da
= data
;
128 struct irq_desc
*desc
;
132 for (i
= 0; i
< *da
->nr
; i
++) {
133 init_one_irq_desc(&desc
[i
]);
134 #ifndef CONFIG_HAVE_SPARSE_IRQ
139 /* init kstat_irqs, nr_cpu_ids is ready already */
140 init_kstat_irqs(desc
, *da
->nr
, nr_cpu_ids
);
142 #ifdef CONFIG_HAVE_SPARSE_IRQ
143 for (i
= 1; i
< *da
->nr
; i
++)
144 desc
[i
-1].next
= &desc
[i
];
146 sparse_irqs_free
= sparse_irqs
;
151 #ifdef CONFIG_HAVE_SPARSE_IRQ
152 static int nr_irq_desc
= 32;
154 static int __init
parse_nr_irq_desc(char *arg
)
157 nr_irq_desc
= simple_strtoul(arg
, NULL
, 0);
161 early_param("nr_irq_desc", parse_nr_irq_desc
);
163 DEFINE_DYN_ARRAY(sparse_irqs
, sizeof(struct irq_desc
), nr_irq_desc
, PAGE_SIZE
, init_work
);
165 struct irq_desc
*irq_to_desc(unsigned int irq
)
167 struct irq_desc
*desc
;
171 if (desc
->irq
== irq
)
179 struct irq_desc
*irq_to_desc_alloc(unsigned int irq
)
181 struct irq_desc
*desc
, *desc_pri
;
186 desc_pri
= desc
= sparse_irqs
;
188 if (desc
->irq
== irq
)
196 spin_lock_irqsave(&sparse_irq_lock
, flags
);
198 * we run out of pre-allocate ones, allocate more
200 if (!sparse_irqs_free
) {
202 unsigned long total_bytes
;
204 printk(KERN_DEBUG
"try to get more irq_desc %d\n", nr_irq_desc
);
206 total_bytes
= sizeof(struct irq_desc
) * nr_irq_desc
;
208 desc
= kzalloc(total_bytes
, GFP_ATOMIC
);
210 desc
= __alloc_bootmem_nopanic(total_bytes
, PAGE_SIZE
, 0);
213 panic("please boot with nr_irq_desc= %d\n", count
* 2);
216 printk(KERN_DEBUG
"irq_desc ==> [%#lx - %#lx]\n", phys
, phys
+ total_bytes
);
218 for (i
= 0; i
< nr_irq_desc
; i
++)
219 init_one_irq_desc(&desc
[i
]);
221 for (i
= 1; i
< nr_irq_desc
; i
++)
222 desc
[i
-1].next
= &desc
[i
];
224 /* init kstat_irqs, nr_cpu_ids is ready already */
225 init_kstat_irqs(desc
, nr_irq_desc
, nr_cpu_ids
);
227 sparse_irqs_free
= desc
;
230 desc
= sparse_irqs_free
;
231 sparse_irqs_free
= sparse_irqs_free
->next
;
234 desc_pri
->next
= desc
;
239 spin_unlock_irqrestore(&sparse_irq_lock
, flags
);
244 struct irq_desc
*irq_desc
;
245 DEFINE_DYN_ARRAY(irq_desc
, sizeof(struct irq_desc
), nr_irqs
, PAGE_SIZE
, init_work
);
251 struct irq_desc irq_desc
[NR_IRQS
] __cacheline_aligned_in_smp
= {
252 [0 ... NR_IRQS
-1] = {
253 .status
= IRQ_DISABLED
,
254 .chip
= &no_irq_chip
,
255 .handle_irq
= handle_bad_irq
,
257 .lock
= __SPIN_LOCK_UNLOCKED(sparse_irqs
->lock
),
259 .affinity
= CPU_MASK_ALL
266 #ifndef CONFIG_HAVE_SPARSE_IRQ
267 struct irq_desc
*irq_to_desc(unsigned int irq
)
270 return &irq_desc
[irq
];
274 struct irq_desc
*irq_to_desc_alloc(unsigned int irq
)
276 return irq_to_desc(irq
);
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
;
288 desc
= irq_to_desc(irq
);
289 print_irq_desc(irq
, desc
);
296 static void noop(unsigned int irq
)
300 static unsigned int noop_ret(unsigned int irq
)
306 * Generic no controller implementation
308 struct irq_chip no_irq_chip
= {
319 * Generic dummy implementation which can be used for
320 * real dumb interrupt sources
322 struct irq_chip dummy_irq_chip
= {
335 * Special, empty irq handler:
337 irqreturn_t
no_action(int cpl
, void *dev_id
)
343 * handle_IRQ_event - irq action chain handler
344 * @irq: the interrupt number
345 * @action: the interrupt action chain for this irq
347 * Handles the action chain of an irq event
349 irqreturn_t
handle_IRQ_event(unsigned int irq
, struct irqaction
*action
)
351 irqreturn_t ret
, retval
= IRQ_NONE
;
352 unsigned int status
= 0;
354 if (!(action
->flags
& IRQF_DISABLED
))
355 local_irq_enable_in_hardirq();
358 ret
= action
->handler(irq
, action
->dev_id
);
359 if (ret
== IRQ_HANDLED
)
360 status
|= action
->flags
;
362 action
= action
->next
;
365 if (status
& IRQF_SAMPLE_RANDOM
)
366 add_interrupt_randomness(irq
);
372 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
374 * __do_IRQ - original all in one highlevel IRQ handler
375 * @irq: the interrupt number
377 * __do_IRQ handles all normal device IRQ's (the special
378 * SMP cross-CPU interrupts have their own specific
381 * This is the original x86 implementation which is used for every
384 unsigned int __do_IRQ(unsigned int irq
)
386 struct irq_desc
*desc
= irq_to_desc(irq
);
387 struct irqaction
*action
;
390 #ifdef CONFIG_HAVE_DYN_ARRAY
391 kstat_irqs_this_cpu(desc
)++;
393 kstat_irqs_this_cpu(irq
)++;
395 if (CHECK_IRQ_PER_CPU(desc
->status
)) {
396 irqreturn_t action_ret
;
399 * No locking required for CPU-local interrupts:
402 desc
->chip
->ack(irq
);
403 if (likely(!(desc
->status
& IRQ_DISABLED
))) {
404 action_ret
= handle_IRQ_event(irq
, desc
->action
);
406 note_interrupt(irq
, desc
, action_ret
);
408 desc
->chip
->end(irq
);
412 spin_lock(&desc
->lock
);
414 desc
->chip
->ack(irq
);
416 * REPLAY is when Linux resends an IRQ that was dropped earlier
417 * WAITING is used by probe to mark irqs that are being tested
419 status
= desc
->status
& ~(IRQ_REPLAY
| IRQ_WAITING
);
420 status
|= IRQ_PENDING
; /* we _want_ to handle it */
423 * If the IRQ is disabled for whatever reason, we cannot
424 * use the action we have.
427 if (likely(!(status
& (IRQ_DISABLED
| IRQ_INPROGRESS
)))) {
428 action
= desc
->action
;
429 status
&= ~IRQ_PENDING
; /* we commit to handling */
430 status
|= IRQ_INPROGRESS
; /* we are handling it */
432 desc
->status
= status
;
435 * If there is no IRQ handler or it was disabled, exit early.
436 * Since we set PENDING, if another processor is handling
437 * a different instance of this same irq, the other processor
438 * will take care of it.
440 if (unlikely(!action
))
444 * Edge triggered interrupts need to remember
446 * This applies to any hw interrupts that allow a second
447 * instance of the same irq to arrive while we are in do_IRQ
448 * or in the handler. But the code here only handles the _second_
449 * instance of the irq, not the third or fourth. So it is mostly
450 * useful for irq hardware that does not mask cleanly in an
454 irqreturn_t action_ret
;
456 spin_unlock(&desc
->lock
);
458 action_ret
= handle_IRQ_event(irq
, action
);
460 note_interrupt(irq
, desc
, action_ret
);
462 spin_lock(&desc
->lock
);
463 if (likely(!(desc
->status
& IRQ_PENDING
)))
465 desc
->status
&= ~IRQ_PENDING
;
467 desc
->status
&= ~IRQ_INPROGRESS
;
471 * The ->end() handler has to deal with interrupts which got
472 * disabled while the handler was running.
474 desc
->chip
->end(irq
);
475 spin_unlock(&desc
->lock
);
482 #ifdef CONFIG_TRACE_IRQFLAGS
483 void early_init_irq_lock_class(void)
485 #ifndef CONFIG_HAVE_DYN_ARRAY
488 for (i
= 0; i
< nr_irqs
; i
++)
489 lockdep_set_class(&irq_desc
[i
].lock
, &irq_desc_lock_class
);
494 #ifdef CONFIG_HAVE_DYN_ARRAY
495 unsigned int kstat_irqs_cpu(unsigned int irq
, int cpu
)
497 struct irq_desc
*desc
= irq_to_desc(irq
);
498 return desc
->kstat_irqs
[cpu
];
501 EXPORT_SYMBOL(kstat_irqs_cpu
);