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