genirq: add support for threaded interrupts to devres
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / irq / handle.c
CommitLineData
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>
0b8f1efa
YL
18#include <linux/rculist.h>
19#include <linux/hash.h>
0fa0ebbf 20#include <linux/bootmem.h>
1da177e4
LT
21
22#include "internals.h"
23
0b8f1efa
YL
24/*
25 * lockdep: we want to handle all irq_desc locks as a single lock-class:
26 */
48a1b10a 27struct lock_class_key irq_desc_lock_class;
0b8f1efa 28
6a6de9ef
TG
29/**
30 * handle_bad_irq - handle spurious and unhandled irqs
43a1dd50
HK
31 * @irq: the interrupt number
32 * @desc: description of the interrupt
43a1dd50
HK
33 *
34 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
6a6de9ef 35 */
d6c88a50 36void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
6a6de9ef 37{
43f77759 38 print_irq_desc(irq, desc);
d6c88a50 39 kstat_incr_irqs_this_cpu(irq, desc);
6a6de9ef
TG
40 ack_bad_irq(irq);
41}
42
97179fd4
DD
43#if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
44static void __init init_irq_default_affinity(void)
45{
46 alloc_bootmem_cpumask_var(&irq_default_affinity);
47 cpumask_setall(irq_default_affinity);
48}
49#else
50static void __init init_irq_default_affinity(void)
51{
52}
53#endif
54
1da177e4
LT
55/*
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
06fcb0c6 59 * interrupt source is transparently wired to the appropriate
1da177e4
LT
60 * controller. Thus drivers need not be aware of the
61 * interrupt-controller.
62 *
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.
66 *
67 * Controller mappings for all interrupt sources:
68 */
85c0f909 69int nr_irqs = NR_IRQS;
fa42d10d 70EXPORT_SYMBOL_GPL(nr_irqs);
d60458b2 71
0b8f1efa 72#ifdef CONFIG_SPARSE_IRQ
92296c6d 73
0b8f1efa
YL
74static struct irq_desc irq_desc_init = {
75 .irq = -1,
76 .status = IRQ_DISABLED,
77 .chip = &no_irq_chip,
78 .handle_irq = handle_bad_irq,
79 .depth = 1,
80 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
0b8f1efa
YL
81};
82
48a1b10a 83void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
0b8f1efa 84{
0b8f1efa 85 int node;
005bf0e6 86 void *ptr;
0b8f1efa
YL
87
88 node = cpu_to_node(cpu);
005bf0e6 89 ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
0b8f1efa 90
005bf0e6
YL
91 /*
92 * don't overwite if can not get new one
93 * init_copy_kstat_irqs() could still use old one
94 */
95 if (ptr) {
96 printk(KERN_DEBUG " alloc kstat_irqs on cpu %d node %d\n",
97 cpu, node);
98 desc->kstat_irqs = ptr;
99 }
0b8f1efa
YL
100}
101
0b8f1efa
YL
102static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
103{
104 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
793f7b12
IM
105
106 spin_lock_init(&desc->lock);
0b8f1efa
YL
107 desc->irq = irq;
108#ifdef CONFIG_SMP
109 desc->cpu = cpu;
110#endif
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");
115 BUG_ON(1);
116 }
802bf931 117 if (!init_alloc_desc_masks(desc, cpu, false)) {
7f7ace0c
MT
118 printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
119 BUG_ON(1);
120 }
0b8f1efa
YL
121 arch_init_chip_data(desc, cpu);
122}
123
124/*
125 * Protect the sparse_irqs:
126 */
48a1b10a 127DEFINE_SPINLOCK(sparse_irq_lock);
0b8f1efa 128
0fa0ebbf 129struct irq_desc **irq_desc_ptrs __read_mostly;
0b8f1efa 130
99d093d1
YL
131static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
132 [0 ... NR_IRQS_LEGACY-1] = {
0b8f1efa
YL
133 .irq = -1,
134 .status = IRQ_DISABLED,
135 .chip = &no_irq_chip,
136 .handle_irq = handle_bad_irq,
137 .depth = 1,
138 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
0b8f1efa
YL
139 }
140};
141
542d865b 142static unsigned int *kstat_irqs_legacy;
0b8f1efa 143
13a0c3c2 144int __init early_irq_init(void)
0b8f1efa
YL
145{
146 struct irq_desc *desc;
147 int legacy_count;
148 int i;
149
97179fd4
DD
150 init_irq_default_affinity();
151
4a046d17
YL
152 /* initialize nr_irqs based on nr_cpu_ids */
153 arch_probe_nr_irqs();
9594949b
MT
154 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
155
0b8f1efa
YL
156 desc = irq_desc_legacy;
157 legacy_count = ARRAY_SIZE(irq_desc_legacy);
158
0fa0ebbf
MT
159 /* allocate irq_desc_ptrs array based on nr_irqs */
160 irq_desc_ptrs = alloc_bootmem(nr_irqs * sizeof(void *));
161
542d865b
MT
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 *
165 sizeof(int));
166
0b8f1efa
YL
167 for (i = 0; i < legacy_count; i++) {
168 desc[i].irq = i;
542d865b 169 desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
fa6beb37 170 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
7f7ace0c 171 init_alloc_desc_masks(&desc[i], 0, true);
0b8f1efa
YL
172 irq_desc_ptrs[i] = desc + i;
173 }
174
9594949b 175 for (i = legacy_count; i < nr_irqs; i++)
0b8f1efa
YL
176 irq_desc_ptrs[i] = NULL;
177
13a0c3c2 178 return arch_early_irq_init();
0b8f1efa
YL
179}
180
181struct irq_desc *irq_to_desc(unsigned int irq)
182{
0fa0ebbf
MT
183 if (irq_desc_ptrs && irq < nr_irqs)
184 return irq_desc_ptrs[irq];
185
186 return NULL;
0b8f1efa
YL
187}
188
189struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
190{
191 struct irq_desc *desc;
192 unsigned long flags;
193 int node;
194
9594949b 195 if (irq >= nr_irqs) {
e2f4d065
MT
196 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
197 irq, nr_irqs);
0b8f1efa
YL
198 return NULL;
199 }
200
201 desc = irq_desc_ptrs[irq];
202 if (desc)
203 return desc;
204
205 spin_lock_irqsave(&sparse_irq_lock, flags);
206
207 /* We have to check it to avoid races with another CPU */
208 desc = irq_desc_ptrs[irq];
209 if (desc)
210 goto out_unlock;
211
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",
215 irq, cpu, node);
216 if (!desc) {
217 printk(KERN_ERR "can not alloc irq_desc\n");
218 BUG_ON(1);
219 }
220 init_one_irq_desc(irq, desc, cpu);
221
222 irq_desc_ptrs[irq] = desc;
223
224out_unlock:
225 spin_unlock_irqrestore(&sparse_irq_lock, flags);
226
227 return desc;
228}
229
f9af0e70 230#else /* !CONFIG_SPARSE_IRQ */
0b8f1efa 231
e729aa16 232struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
1da177e4 233 [0 ... NR_IRQS-1] = {
4f167fb4 234 .status = IRQ_DISABLED,
f1c2662c 235 .chip = &no_irq_chip,
7a55713a 236 .handle_irq = handle_bad_irq,
94d39e1f 237 .depth = 1,
aac3f2b6 238 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
1da177e4
LT
239 }
240};
08678b08 241
d7e51e66 242static unsigned int kstat_irqs_all[NR_IRQS][NR_CPUS];
12026ea1
YL
243int __init early_irq_init(void)
244{
245 struct irq_desc *desc;
246 int count;
247 int i;
248
97179fd4
DD
249 init_irq_default_affinity();
250
9594949b
MT
251 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
252
12026ea1
YL
253 desc = irq_desc;
254 count = ARRAY_SIZE(irq_desc);
255
d7e51e66 256 for (i = 0; i < count; i++) {
12026ea1 257 desc[i].irq = i;
7f7ace0c 258 init_alloc_desc_masks(&desc[i], 0, true);
d7e51e66
YL
259 desc[i].kstat_irqs = kstat_irqs_all[i];
260 }
12026ea1
YL
261 return arch_early_irq_init();
262}
263
f9af0e70
KM
264struct irq_desc *irq_to_desc(unsigned int irq)
265{
266 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
267}
268
269struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
270{
271 return irq_to_desc(irq);
272}
273#endif /* !CONFIG_SPARSE_IRQ */
0b8f1efa 274
0f3c2a89
YL
275void clear_kstat_irqs(struct irq_desc *desc)
276{
277 memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
278}
279
1da177e4 280/*
77a5afec
IM
281 * What should we do if we get a hw irq event on an illegal vector?
282 * Each architecture has to answer this themself.
1da177e4 283 */
77a5afec 284static void ack_bad(unsigned int irq)
1da177e4 285{
d3c60047 286 struct irq_desc *desc = irq_to_desc(irq);
08678b08 287
08678b08 288 print_irq_desc(irq, desc);
1da177e4
LT
289 ack_bad_irq(irq);
290}
291
77a5afec
IM
292/*
293 * NOP functions
294 */
295static void noop(unsigned int irq)
296{
297}
298
299static unsigned int noop_ret(unsigned int irq)
300{
301 return 0;
302}
303
304/*
305 * Generic no controller implementation
306 */
f1c2662c
IM
307struct 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 */
321struct 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 336irqreturn_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 348irqreturn_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
044d4084
PZ
353 WARN_ONCE(!in_irq(), "BUG: IRQ handler called from non-hardirq context!");
354
3cca53b0 355 if (!(action->flags & IRQF_DISABLED))
366c7f55 356 local_irq_enable_in_hardirq();
1da177e4
LT
357
358 do {
7d12e780 359 ret = action->handler(irq, action->dev_id);
3aa551c9
TG
360
361 switch (ret) {
362 case IRQ_WAKE_THREAD:
363 /*
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
369 * storm is lurking.
370 */
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);
375 }
376
377 /*
378 * Set it to handled so the spurious check
379 * does not trigger.
380 */
381 ret = IRQ_HANDLED;
382 /* Fall through to add to randomness */
383 case IRQ_HANDLED:
1da177e4 384 status |= action->flags;
3aa551c9
TG
385 break;
386
387 default:
388 break;
389 }
390
1da177e4
LT
391 retval |= ret;
392 action = action->next;
393 } while (action);
394
3cca53b0 395 if (status & IRQF_SAMPLE_RANDOM)
1da177e4
LT
396 add_interrupt_randomness(irq);
397 local_irq_disable();
398
399 return retval;
400}
401
af8c65b5 402#ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
0e57aa11
TG
403
404#ifdef CONFIG_ENABLE_WARN_DEPRECATED
405# warning __do_IRQ is deprecated. Please convert to proper flow handlers
406#endif
407
8d28bc75
IM
408/**
409 * __do_IRQ - original all in one highlevel IRQ handler
410 * @irq: the interrupt number
8d28bc75
IM
411 *
412 * __do_IRQ handles all normal device IRQ's (the special
1da177e4
LT
413 * SMP cross-CPU interrupts have their own specific
414 * handlers).
8d28bc75
IM
415 *
416 * This is the original x86 implementation which is used for every
417 * interrupt type.
1da177e4 418 */
7ad5b3a5 419unsigned int __do_IRQ(unsigned int irq)
1da177e4 420{
08678b08 421 struct irq_desc *desc = irq_to_desc(irq);
06fcb0c6 422 struct irqaction *action;
1da177e4
LT
423 unsigned int status;
424
d6c88a50
TG
425 kstat_incr_irqs_this_cpu(irq, desc);
426
f26fdd59 427 if (CHECK_IRQ_PER_CPU(desc->status)) {
1da177e4
LT
428 irqreturn_t action_ret;
429
430 /*
431 * No locking required for CPU-local interrupts:
432 */
48a1b10a 433 if (desc->chip->ack) {
d1bef4ed 434 desc->chip->ack(irq);
48a1b10a
YL
435 /* get new one */
436 desc = irq_remap_to_desc(irq, desc);
437 }
c642b839
RA
438 if (likely(!(desc->status & IRQ_DISABLED))) {
439 action_ret = handle_IRQ_event(irq, desc->action);
440 if (!noirqdebug)
441 note_interrupt(irq, desc, action_ret);
442 }
d1bef4ed 443 desc->chip->end(irq);
1da177e4
LT
444 return 1;
445 }
446
447 spin_lock(&desc->lock);
48a1b10a 448 if (desc->chip->ack) {
d1bef4ed 449 desc->chip->ack(irq);
48a1b10a
YL
450 desc = irq_remap_to_desc(irq, desc);
451 }
1da177e4
LT
452 /*
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
455 */
456 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
457 status |= IRQ_PENDING; /* we _want_ to handle it */
458
459 /*
460 * If the IRQ is disabled for whatever reason, we cannot
461 * use the action we have.
462 */
463 action = NULL;
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 */
468 }
469 desc->status = status;
470
471 /*
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.
476 */
477 if (unlikely(!action))
478 goto out;
479
480 /*
481 * Edge triggered interrupts need to remember
482 * pending events.
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
488 * SMP environment.
489 */
490 for (;;) {
491 irqreturn_t action_ret;
492
493 spin_unlock(&desc->lock);
494
7d12e780 495 action_ret = handle_IRQ_event(irq, action);
1da177e4 496 if (!noirqdebug)
7d12e780 497 note_interrupt(irq, desc, action_ret);
b42172fc
LT
498
499 spin_lock(&desc->lock);
1da177e4
LT
500 if (likely(!(desc->status & IRQ_PENDING)))
501 break;
502 desc->status &= ~IRQ_PENDING;
503 }
504 desc->status &= ~IRQ_INPROGRESS;
505
506out:
507 /*
508 * The ->end() handler has to deal with interrupts which got
509 * disabled while the handler was running.
510 */
d1bef4ed 511 desc->chip->end(irq);
1da177e4
LT
512 spin_unlock(&desc->lock);
513
514 return 1;
515}
af8c65b5 516#endif
1da177e4 517
243c7621
IM
518void early_init_irq_lock_class(void)
519{
10e58084 520 struct irq_desc *desc;
243c7621
IM
521 int i;
522
0b8f1efa 523 for_each_irq_desc(i, desc) {
10e58084 524 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
0b8f1efa 525 }
0b8f1efa 526}
0b8f1efa 527
0b8f1efa
YL
528unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
529{
530 struct irq_desc *desc = irq_to_desc(irq);
26ddd8d5 531 return desc ? desc->kstat_irqs[cpu] : 0;
243c7621 532}
0b8f1efa
YL
533EXPORT_SYMBOL(kstat_irqs_cpu);
534