Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/irq/handle.c | |
3 | * | |
a34db9b2 IM |
4 | * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar |
5 | * Copyright (C) 2005-2006, Thomas Gleixner, Russell King | |
1da177e4 LT |
6 | * |
7 | * This file contains the core interrupt handling code. | |
a34db9b2 IM |
8 | * |
9 | * Detailed information is available in Documentation/DocBook/genericirq | |
10 | * | |
1da177e4 LT |
11 | */ |
12 | ||
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 | ||
19 | #include "internals.h" | |
20 | ||
08678b08 YL |
21 | /* |
22 | * lockdep: we want to handle all irq_desc locks as a single lock-class: | |
23 | */ | |
24 | static struct lock_class_key irq_desc_lock_class; | |
08678b08 | 25 | |
6a6de9ef TG |
26 | /** |
27 | * handle_bad_irq - handle spurious and unhandled irqs | |
43a1dd50 HK |
28 | * @irq: the interrupt number |
29 | * @desc: description of the interrupt | |
43a1dd50 HK |
30 | * |
31 | * Handles spurious and unhandled IRQ's. It also prints a debugmessage. | |
6a6de9ef | 32 | */ |
7ad5b3a5 | 33 | void |
7d12e780 | 34 | handle_bad_irq(unsigned int irq, struct irq_desc *desc) |
6a6de9ef | 35 | { |
43f77759 | 36 | print_irq_desc(irq, desc); |
8c464a4b | 37 | #ifdef CONFIG_HAVE_DYN_ARRAY |
7f95ec9e | 38 | kstat_irqs_this_cpu(desc)++; |
8c464a4b YL |
39 | #else |
40 | kstat_irqs_this_cpu(irq)++; | |
41 | #endif | |
6a6de9ef TG |
42 | ack_bad_irq(irq); |
43 | } | |
44 | ||
1da177e4 LT |
45 | /* |
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 | |
06fcb0c6 | 49 | * interrupt source is transparently wired to the appropriate |
1da177e4 LT |
50 | * controller. Thus drivers need not be aware of the |
51 | * interrupt-controller. | |
52 | * | |
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. | |
56 | * | |
57 | * Controller mappings for all interrupt sources: | |
58 | */ | |
85c0f909 | 59 | int nr_irqs = NR_IRQS; |
fa42d10d | 60 | EXPORT_SYMBOL_GPL(nr_irqs); |
d60458b2 YL |
61 | |
62 | #ifdef CONFIG_HAVE_DYN_ARRAY | |
08678b08 YL |
63 | static struct irq_desc irq_desc_init = { |
64 | .irq = -1U, | |
d60458b2 YL |
65 | .status = IRQ_DISABLED, |
66 | .chip = &no_irq_chip, | |
67 | .handle_irq = handle_bad_irq, | |
68 | .depth = 1, | |
69 | .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock), | |
70 | #ifdef CONFIG_SMP | |
71 | .affinity = CPU_MASK_ALL | |
72 | #endif | |
73 | }; | |
74 | ||
08678b08 YL |
75 | |
76 | static void init_one_irq_desc(struct irq_desc *desc) | |
77 | { | |
78 | memcpy(desc, &irq_desc_init, sizeof(struct irq_desc)); | |
08678b08 | 79 | lockdep_set_class(&desc->lock, &irq_desc_lock_class); |
08678b08 YL |
80 | } |
81 | ||
7f95ec9e YL |
82 | extern int after_bootmem; |
83 | extern void *__alloc_bootmem_nopanic(unsigned long size, | |
84 | unsigned long align, | |
85 | unsigned long goal); | |
08678b08 | 86 | |
7f95ec9e | 87 | static void init_kstat_irqs(struct irq_desc *desc, int nr_desc, int nr) |
08678b08 | 88 | { |
7f95ec9e YL |
89 | unsigned long bytes, total_bytes; |
90 | char *ptr; | |
91 | int i; | |
92 | unsigned long phys; | |
93 | ||
94 | /* Compute how many bytes we need per irq and allocate them */ | |
95 | bytes = nr * sizeof(unsigned int); | |
96 | total_bytes = bytes * nr_desc; | |
97 | if (after_bootmem) | |
98 | ptr = kzalloc(total_bytes, GFP_ATOMIC); | |
99 | else | |
100 | ptr = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0); | |
101 | ||
102 | if (!ptr) | |
103 | panic(" can not allocate kstat_irqs\n"); | |
104 | ||
105 | phys = __pa(ptr); | |
106 | printk(KERN_DEBUG "kstat_irqs ==> [%#lx - %#lx]\n", phys, phys + total_bytes); | |
107 | ||
108 | for (i = 0; i < nr_desc; i++) { | |
109 | desc[i].kstat_irqs = (unsigned int *)ptr; | |
110 | ptr += bytes; | |
111 | } | |
08678b08 YL |
112 | } |
113 | ||
2976fe20 | 114 | #ifdef CONFIG_HAVE_SPARSE_IRQ |
e89eb438 YL |
115 | /* |
116 | * Protect the sparse_irqs_free freelist: | |
117 | */ | |
118 | static DEFINE_SPINLOCK(sparse_irq_lock); | |
67fb283e YL |
119 | static struct irq_desc *sparse_irqs_free; |
120 | struct irq_desc *sparse_irqs; | |
121 | #endif | |
122 | ||
d60458b2 YL |
123 | static void __init init_work(void *data) |
124 | { | |
125 | struct dyn_array *da = data; | |
126 | int i; | |
127 | struct irq_desc *desc; | |
128 | ||
129 | desc = *da->name; | |
130 | ||
7f95ec9e | 131 | for (i = 0; i < *da->nr; i++) { |
08678b08 | 132 | init_one_irq_desc(&desc[i]); |
7f95ec9e YL |
133 | #ifndef CONFIG_HAVE_SPARSE_IRQ |
134 | desc[i].irq = i; | |
135 | #endif | |
136 | } | |
08678b08 | 137 | |
67fb283e YL |
138 | /* init kstat_irqs, nr_cpu_ids is ready already */ |
139 | init_kstat_irqs(desc, *da->nr, nr_cpu_ids); | |
140 | ||
7f95ec9e | 141 | #ifdef CONFIG_HAVE_SPARSE_IRQ |
08678b08 YL |
142 | for (i = 1; i < *da->nr; i++) |
143 | desc[i-1].next = &desc[i]; | |
7f95ec9e | 144 | |
67fb283e YL |
145 | sparse_irqs_free = sparse_irqs; |
146 | sparse_irqs = NULL; | |
147 | #endif | |
d60458b2 YL |
148 | } |
149 | ||
7f95ec9e YL |
150 | #ifdef CONFIG_HAVE_SPARSE_IRQ |
151 | static int nr_irq_desc = 32; | |
152 | ||
153 | static int __init parse_nr_irq_desc(char *arg) | |
154 | { | |
155 | if (arg) | |
156 | nr_irq_desc = simple_strtoul(arg, NULL, 0); | |
157 | return 0; | |
158 | } | |
159 | ||
160 | early_param("nr_irq_desc", parse_nr_irq_desc); | |
161 | ||
08678b08 YL |
162 | DEFINE_DYN_ARRAY(sparse_irqs, sizeof(struct irq_desc), nr_irq_desc, PAGE_SIZE, init_work); |
163 | ||
cb5bc832 | 164 | struct irq_desc *irq_to_desc(unsigned int irq) |
9059d8fa YL |
165 | { |
166 | struct irq_desc *desc; | |
167 | ||
67fb283e | 168 | desc = sparse_irqs; |
9059d8fa YL |
169 | while (desc) { |
170 | if (desc->irq == irq) | |
171 | return desc; | |
172 | ||
9059d8fa YL |
173 | desc = desc->next; |
174 | } | |
175 | return NULL; | |
176 | } | |
e89eb438 | 177 | |
cb5bc832 | 178 | struct irq_desc *irq_to_desc_alloc(unsigned int irq) |
08678b08 YL |
179 | { |
180 | struct irq_desc *desc, *desc_pri; | |
e89eb438 | 181 | unsigned long flags; |
08678b08 | 182 | int count = 0; |
e89eb438 | 183 | int i; |
08678b08 | 184 | |
67fb283e | 185 | desc_pri = desc = sparse_irqs; |
08678b08 YL |
186 | while (desc) { |
187 | if (desc->irq == irq) | |
188 | return desc; | |
189 | ||
08678b08 YL |
190 | desc_pri = desc; |
191 | desc = desc->next; | |
192 | count++; | |
193 | } | |
194 | ||
e89eb438 | 195 | spin_lock_irqsave(&sparse_irq_lock, flags); |
08678b08 YL |
196 | /* |
197 | * we run out of pre-allocate ones, allocate more | |
198 | */ | |
67fb283e YL |
199 | if (!sparse_irqs_free) { |
200 | unsigned long phys; | |
201 | unsigned long total_bytes; | |
08678b08 | 202 | |
67fb283e | 203 | printk(KERN_DEBUG "try to get more irq_desc %d\n", nr_irq_desc); |
08678b08 | 204 | |
67fb283e YL |
205 | total_bytes = sizeof(struct irq_desc) * nr_irq_desc; |
206 | if (after_bootmem) | |
207 | desc = kzalloc(total_bytes, GFP_ATOMIC); | |
208 | else | |
209 | desc = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0); | |
08678b08 | 210 | |
67fb283e YL |
211 | if (!desc) |
212 | panic("please boot with nr_irq_desc= %d\n", count * 2); | |
7f95ec9e | 213 | |
67fb283e YL |
214 | phys = __pa(desc); |
215 | printk(KERN_DEBUG "irq_desc ==> [%#lx - %#lx]\n", phys, phys + total_bytes); | |
08678b08 | 216 | |
67fb283e YL |
217 | for (i = 0; i < nr_irq_desc; i++) |
218 | init_one_irq_desc(&desc[i]); | |
08678b08 | 219 | |
67fb283e YL |
220 | for (i = 1; i < nr_irq_desc; i++) |
221 | desc[i-1].next = &desc[i]; | |
7f95ec9e | 222 | |
67fb283e YL |
223 | /* init kstat_irqs, nr_cpu_ids is ready already */ |
224 | init_kstat_irqs(desc, nr_irq_desc, nr_cpu_ids); | |
08678b08 | 225 | |
67fb283e YL |
226 | sparse_irqs_free = desc; |
227 | } | |
228 | ||
229 | desc = sparse_irqs_free; | |
230 | sparse_irqs_free = sparse_irqs_free->next; | |
231 | desc->next = NULL; | |
232 | if (desc_pri) | |
233 | desc_pri->next = desc; | |
234 | else | |
235 | sparse_irqs = desc; | |
236 | desc->irq = irq; | |
e89eb438 YL |
237 | |
238 | spin_unlock_irqrestore(&sparse_irq_lock, flags); | |
239 | ||
08678b08 YL |
240 | return desc; |
241 | } | |
242 | #else | |
9059d8fa | 243 | struct irq_desc *irq_desc; |
d60458b2 YL |
244 | DEFINE_DYN_ARRAY(irq_desc, sizeof(struct irq_desc), nr_irqs, PAGE_SIZE, init_work); |
245 | ||
08678b08 YL |
246 | #endif |
247 | ||
d60458b2 YL |
248 | #else |
249 | ||
e729aa16 | 250 | struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = { |
1da177e4 | 251 | [0 ... NR_IRQS-1] = { |
4f167fb4 | 252 | .status = IRQ_DISABLED, |
f1c2662c | 253 | .chip = &no_irq_chip, |
7a55713a | 254 | .handle_irq = handle_bad_irq, |
94d39e1f | 255 | .depth = 1, |
08678b08 | 256 | .lock = __SPIN_LOCK_UNLOCKED(sparse_irqs->lock), |
a53da52f IM |
257 | #ifdef CONFIG_SMP |
258 | .affinity = CPU_MASK_ALL | |
259 | #endif | |
1da177e4 LT |
260 | } |
261 | }; | |
08678b08 YL |
262 | |
263 | #endif | |
264 | ||
265 | #ifndef CONFIG_HAVE_SPARSE_IRQ | |
266 | struct irq_desc *irq_to_desc(unsigned int irq) | |
267 | { | |
268 | if (irq < nr_irqs) | |
269 | return &irq_desc[irq]; | |
270 | ||
271 | return NULL; | |
272 | } | |
cb5bc832 | 273 | struct irq_desc *irq_to_desc_alloc(unsigned int irq) |
9059d8fa YL |
274 | { |
275 | return irq_to_desc(irq); | |
276 | } | |
d60458b2 | 277 | #endif |
1da177e4 LT |
278 | |
279 | /* | |
77a5afec IM |
280 | * What should we do if we get a hw irq event on an illegal vector? |
281 | * Each architecture has to answer this themself. | |
1da177e4 | 282 | */ |
77a5afec | 283 | static void ack_bad(unsigned int irq) |
1da177e4 | 284 | { |
08678b08 YL |
285 | struct irq_desc *desc; |
286 | ||
287 | desc = irq_to_desc(irq); | |
288 | print_irq_desc(irq, desc); | |
1da177e4 LT |
289 | ack_bad_irq(irq); |
290 | } | |
291 | ||
77a5afec IM |
292 | /* |
293 | * NOP functions | |
294 | */ | |
295 | static void noop(unsigned int irq) | |
296 | { | |
297 | } | |
298 | ||
299 | static unsigned int noop_ret(unsigned int irq) | |
300 | { | |
301 | return 0; | |
302 | } | |
303 | ||
304 | /* | |
305 | * Generic no controller implementation | |
306 | */ | |
f1c2662c IM |
307 | struct irq_chip no_irq_chip = { |
308 | .name = "none", | |
77a5afec IM |
309 | .startup = noop_ret, |
310 | .shutdown = noop, | |
311 | .enable = noop, | |
312 | .disable = noop, | |
313 | .ack = ack_bad, | |
314 | .end = noop, | |
1da177e4 LT |
315 | }; |
316 | ||
f8b5473f TG |
317 | /* |
318 | * Generic dummy implementation which can be used for | |
319 | * real dumb interrupt sources | |
320 | */ | |
321 | struct irq_chip dummy_irq_chip = { | |
322 | .name = "dummy", | |
323 | .startup = noop_ret, | |
324 | .shutdown = noop, | |
325 | .enable = noop, | |
326 | .disable = noop, | |
327 | .ack = noop, | |
328 | .mask = noop, | |
329 | .unmask = noop, | |
330 | .end = noop, | |
331 | }; | |
332 | ||
1da177e4 LT |
333 | /* |
334 | * Special, empty irq handler: | |
335 | */ | |
7d12e780 | 336 | irqreturn_t no_action(int cpl, void *dev_id) |
1da177e4 LT |
337 | { |
338 | return IRQ_NONE; | |
339 | } | |
340 | ||
8d28bc75 IM |
341 | /** |
342 | * handle_IRQ_event - irq action chain handler | |
343 | * @irq: the interrupt number | |
8d28bc75 IM |
344 | * @action: the interrupt action chain for this irq |
345 | * | |
346 | * Handles the action chain of an irq event | |
1da177e4 | 347 | */ |
7d12e780 | 348 | irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action) |
1da177e4 | 349 | { |
908dcecd JB |
350 | irqreturn_t ret, retval = IRQ_NONE; |
351 | unsigned int status = 0; | |
1da177e4 | 352 | |
3cca53b0 | 353 | if (!(action->flags & IRQF_DISABLED)) |
366c7f55 | 354 | local_irq_enable_in_hardirq(); |
1da177e4 LT |
355 | |
356 | do { | |
7d12e780 | 357 | ret = action->handler(irq, action->dev_id); |
1da177e4 LT |
358 | if (ret == IRQ_HANDLED) |
359 | status |= action->flags; | |
360 | retval |= ret; | |
361 | action = action->next; | |
362 | } while (action); | |
363 | ||
3cca53b0 | 364 | if (status & IRQF_SAMPLE_RANDOM) |
1da177e4 LT |
365 | add_interrupt_randomness(irq); |
366 | local_irq_disable(); | |
367 | ||
368 | return retval; | |
369 | } | |
370 | ||
af8c65b5 | 371 | #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ |
8d28bc75 IM |
372 | /** |
373 | * __do_IRQ - original all in one highlevel IRQ handler | |
374 | * @irq: the interrupt number | |
8d28bc75 IM |
375 | * |
376 | * __do_IRQ handles all normal device IRQ's (the special | |
1da177e4 LT |
377 | * SMP cross-CPU interrupts have their own specific |
378 | * handlers). | |
8d28bc75 IM |
379 | * |
380 | * This is the original x86 implementation which is used for every | |
381 | * interrupt type. | |
1da177e4 | 382 | */ |
7ad5b3a5 | 383 | unsigned int __do_IRQ(unsigned int irq) |
1da177e4 | 384 | { |
08678b08 | 385 | struct irq_desc *desc = irq_to_desc(irq); |
06fcb0c6 | 386 | struct irqaction *action; |
1da177e4 LT |
387 | unsigned int status; |
388 | ||
8c464a4b | 389 | #ifdef CONFIG_HAVE_DYN_ARRAY |
7f95ec9e | 390 | kstat_irqs_this_cpu(desc)++; |
8c464a4b YL |
391 | #else |
392 | kstat_irqs_this_cpu(irq)++; | |
393 | #endif | |
f26fdd59 | 394 | if (CHECK_IRQ_PER_CPU(desc->status)) { |
1da177e4 LT |
395 | irqreturn_t action_ret; |
396 | ||
397 | /* | |
398 | * No locking required for CPU-local interrupts: | |
399 | */ | |
d1bef4ed IM |
400 | if (desc->chip->ack) |
401 | desc->chip->ack(irq); | |
c642b839 RA |
402 | if (likely(!(desc->status & IRQ_DISABLED))) { |
403 | action_ret = handle_IRQ_event(irq, desc->action); | |
404 | if (!noirqdebug) | |
405 | note_interrupt(irq, desc, action_ret); | |
406 | } | |
d1bef4ed | 407 | desc->chip->end(irq); |
1da177e4 LT |
408 | return 1; |
409 | } | |
410 | ||
411 | spin_lock(&desc->lock); | |
d1bef4ed IM |
412 | if (desc->chip->ack) |
413 | desc->chip->ack(irq); | |
1da177e4 LT |
414 | /* |
415 | * REPLAY is when Linux resends an IRQ that was dropped earlier | |
416 | * WAITING is used by probe to mark irqs that are being tested | |
417 | */ | |
418 | status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING); | |
419 | status |= IRQ_PENDING; /* we _want_ to handle it */ | |
420 | ||
421 | /* | |
422 | * If the IRQ is disabled for whatever reason, we cannot | |
423 | * use the action we have. | |
424 | */ | |
425 | action = NULL; | |
426 | if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) { | |
427 | action = desc->action; | |
428 | status &= ~IRQ_PENDING; /* we commit to handling */ | |
429 | status |= IRQ_INPROGRESS; /* we are handling it */ | |
430 | } | |
431 | desc->status = status; | |
432 | ||
433 | /* | |
434 | * If there is no IRQ handler or it was disabled, exit early. | |
435 | * Since we set PENDING, if another processor is handling | |
436 | * a different instance of this same irq, the other processor | |
437 | * will take care of it. | |
438 | */ | |
439 | if (unlikely(!action)) | |
440 | goto out; | |
441 | ||
442 | /* | |
443 | * Edge triggered interrupts need to remember | |
444 | * pending events. | |
445 | * This applies to any hw interrupts that allow a second | |
446 | * instance of the same irq to arrive while we are in do_IRQ | |
447 | * or in the handler. But the code here only handles the _second_ | |
448 | * instance of the irq, not the third or fourth. So it is mostly | |
449 | * useful for irq hardware that does not mask cleanly in an | |
450 | * SMP environment. | |
451 | */ | |
452 | for (;;) { | |
453 | irqreturn_t action_ret; | |
454 | ||
455 | spin_unlock(&desc->lock); | |
456 | ||
7d12e780 | 457 | action_ret = handle_IRQ_event(irq, action); |
1da177e4 | 458 | if (!noirqdebug) |
7d12e780 | 459 | note_interrupt(irq, desc, action_ret); |
b42172fc LT |
460 | |
461 | spin_lock(&desc->lock); | |
1da177e4 LT |
462 | if (likely(!(desc->status & IRQ_PENDING))) |
463 | break; | |
464 | desc->status &= ~IRQ_PENDING; | |
465 | } | |
466 | desc->status &= ~IRQ_INPROGRESS; | |
467 | ||
468 | out: | |
469 | /* | |
470 | * The ->end() handler has to deal with interrupts which got | |
471 | * disabled while the handler was running. | |
472 | */ | |
d1bef4ed | 473 | desc->chip->end(irq); |
1da177e4 LT |
474 | spin_unlock(&desc->lock); |
475 | ||
476 | return 1; | |
477 | } | |
af8c65b5 | 478 | #endif |
1da177e4 | 479 | |
243c7621 | 480 | |
08678b08 | 481 | #ifdef CONFIG_TRACE_IRQFLAGS |
243c7621 IM |
482 | void early_init_irq_lock_class(void) |
483 | { | |
08678b08 | 484 | #ifndef CONFIG_HAVE_DYN_ARRAY |
243c7621 IM |
485 | int i; |
486 | ||
85c0f909 | 487 | for (i = 0; i < nr_irqs; i++) |
243c7621 | 488 | lockdep_set_class(&irq_desc[i].lock, &irq_desc_lock_class); |
08678b08 | 489 | #endif |
243c7621 | 490 | } |
243c7621 | 491 | #endif |
08678b08 | 492 | |
8c464a4b | 493 | #ifdef CONFIG_HAVE_DYN_ARRAY |
7f95ec9e YL |
494 | unsigned int kstat_irqs_cpu(unsigned int irq, int cpu) |
495 | { | |
496 | struct irq_desc *desc = irq_to_desc(irq); | |
497 | return desc->kstat_irqs[cpu]; | |
498 | } | |
8c464a4b | 499 | #endif |
7f95ec9e YL |
500 | EXPORT_SYMBOL(kstat_irqs_cpu); |
501 |