Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / kernel / irq / handle.c
1 /*
2  * linux/kernel/irq/handle.c
3  *
4  * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5  * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
6  *
7  * This file contains the core interrupt handling code.
8  *
9  * Detailed information is available in Documentation/DocBook/genericirq
10  *
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 #include <linux/rculist.h>
19 #include <linux/hash.h>
20 #include <linux/bootmem.h>
21
22 #include "internals.h"
23
24 /*
25  * lockdep: we want to handle all irq_desc locks as a single lock-class:
26  */
27 struct lock_class_key irq_desc_lock_class;
28
29 /**
30  * handle_bad_irq - handle spurious and unhandled irqs
31  * @irq:       the interrupt number
32  * @desc:      description of the interrupt
33  *
34  * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
35  */
36 void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
37 {
38         print_irq_desc(irq, desc);
39         kstat_incr_irqs_this_cpu(irq, desc);
40         ack_bad_irq(irq);
41 }
42
43 #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
44 static 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
50 static void __init init_irq_default_affinity(void)
51 {
52 }
53 #endif
54
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
59  * interrupt source is transparently wired to the appropriate
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  */
69 int nr_irqs = NR_IRQS;
70 EXPORT_SYMBOL_GPL(nr_irqs);
71
72 #ifdef CONFIG_SPARSE_IRQ
73
74 static 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),
81 };
82
83 void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
84 {
85         int node;
86         void *ptr;
87
88         node = cpu_to_node(cpu);
89         ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
90
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         }
100 }
101
102 static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
103 {
104         memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
105
106         spin_lock_init(&desc->lock);
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         }
117         if (!init_alloc_desc_masks(desc, cpu, false)) {
118                 printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
119                 BUG_ON(1);
120         }
121         arch_init_chip_data(desc, cpu);
122 }
123
124 /*
125  * Protect the sparse_irqs:
126  */
127 DEFINE_SPINLOCK(sparse_irq_lock);
128
129 struct irq_desc **irq_desc_ptrs __read_mostly;
130
131 static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
132         [0 ... NR_IRQS_LEGACY-1] = {
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),
139         }
140 };
141
142 static unsigned int *kstat_irqs_legacy;
143
144 int __init early_irq_init(void)
145 {
146         struct irq_desc *desc;
147         int legacy_count;
148         int i;
149
150         init_irq_default_affinity();
151
152          /* initialize nr_irqs based on nr_cpu_ids */
153         arch_probe_nr_irqs();
154         printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
155
156         desc = irq_desc_legacy;
157         legacy_count = ARRAY_SIZE(irq_desc_legacy);
158
159         /* allocate irq_desc_ptrs array based on nr_irqs */
160         irq_desc_ptrs = alloc_bootmem(nr_irqs * sizeof(void *));
161
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
167         for (i = 0; i < legacy_count; i++) {
168                 desc[i].irq = i;
169                 desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
170                 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
171                 init_alloc_desc_masks(&desc[i], 0, true);
172                 irq_desc_ptrs[i] = desc + i;
173         }
174
175         for (i = legacy_count; i < nr_irqs; i++)
176                 irq_desc_ptrs[i] = NULL;
177
178         return arch_early_irq_init();
179 }
180
181 struct irq_desc *irq_to_desc(unsigned int irq)
182 {
183         if (irq_desc_ptrs && irq < nr_irqs)
184                 return irq_desc_ptrs[irq];
185
186         return NULL;
187 }
188
189 struct 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
195         if (irq >= nr_irqs) {
196                 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
197                         irq, nr_irqs);
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
224 out_unlock:
225         spin_unlock_irqrestore(&sparse_irq_lock, flags);
226
227         return desc;
228 }
229
230 #else /* !CONFIG_SPARSE_IRQ */
231
232 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
233         [0 ... NR_IRQS-1] = {
234                 .status = IRQ_DISABLED,
235                 .chip = &no_irq_chip,
236                 .handle_irq = handle_bad_irq,
237                 .depth = 1,
238                 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
239         }
240 };
241
242 static unsigned int kstat_irqs_all[NR_IRQS][NR_CPUS];
243 int __init early_irq_init(void)
244 {
245         struct irq_desc *desc;
246         int count;
247         int i;
248
249         init_irq_default_affinity();
250
251         printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
252
253         desc = irq_desc;
254         count = ARRAY_SIZE(irq_desc);
255
256         for (i = 0; i < count; i++) {
257                 desc[i].irq = i;
258                 init_alloc_desc_masks(&desc[i], 0, true);
259                 desc[i].kstat_irqs = kstat_irqs_all[i];
260         }
261         return arch_early_irq_init();
262 }
263
264 struct irq_desc *irq_to_desc(unsigned int irq)
265 {
266         return (irq < NR_IRQS) ? irq_desc + irq : NULL;
267 }
268
269 struct 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 */
274
275 void clear_kstat_irqs(struct irq_desc *desc)
276 {
277         memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
278 }
279
280 /*
281  * What should we do if we get a hw irq event on an illegal vector?
282  * Each architecture has to answer this themself.
283  */
284 static void ack_bad(unsigned int irq)
285 {
286         struct irq_desc *desc = irq_to_desc(irq);
287
288         print_irq_desc(irq, desc);
289         ack_bad_irq(irq);
290 }
291
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  */
307 struct irq_chip no_irq_chip = {
308         .name           = "none",
309         .startup        = noop_ret,
310         .shutdown       = noop,
311         .enable         = noop,
312         .disable        = noop,
313         .ack            = ack_bad,
314         .end            = noop,
315 };
316
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
333 /*
334  * Special, empty irq handler:
335  */
336 irqreturn_t no_action(int cpl, void *dev_id)
337 {
338         return IRQ_NONE;
339 }
340
341 /**
342  * handle_IRQ_event - irq action chain handler
343  * @irq:        the interrupt number
344  * @action:     the interrupt action chain for this irq
345  *
346  * Handles the action chain of an irq event
347  */
348 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
349 {
350         irqreturn_t ret, retval = IRQ_NONE;
351         unsigned int status = 0;
352
353         WARN_ONCE(!in_irq(), "BUG: IRQ handler called from non-hardirq context!");
354
355         if (!(action->flags & IRQF_DISABLED))
356                 local_irq_enable_in_hardirq();
357
358         do {
359                 ret = action->handler(irq, action->dev_id);
360                 if (ret == IRQ_HANDLED)
361                         status |= action->flags;
362                 retval |= ret;
363                 action = action->next;
364         } while (action);
365
366         if (status & IRQF_SAMPLE_RANDOM)
367                 add_interrupt_randomness(irq);
368         local_irq_disable();
369
370         return retval;
371 }
372
373 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
374
375 #ifdef CONFIG_ENABLE_WARN_DEPRECATED
376 # warning __do_IRQ is deprecated. Please convert to proper flow handlers
377 #endif
378
379 /**
380  * __do_IRQ - original all in one highlevel IRQ handler
381  * @irq:        the interrupt number
382  *
383  * __do_IRQ handles all normal device IRQ's (the special
384  * SMP cross-CPU interrupts have their own specific
385  * handlers).
386  *
387  * This is the original x86 implementation which is used for every
388  * interrupt type.
389  */
390 unsigned int __do_IRQ(unsigned int irq)
391 {
392         struct irq_desc *desc = irq_to_desc(irq);
393         struct irqaction *action;
394         unsigned int status;
395
396         kstat_incr_irqs_this_cpu(irq, desc);
397
398         if (CHECK_IRQ_PER_CPU(desc->status)) {
399                 irqreturn_t action_ret;
400
401                 /*
402                  * No locking required for CPU-local interrupts:
403                  */
404                 if (desc->chip->ack) {
405                         desc->chip->ack(irq);
406                         /* get new one */
407                         desc = irq_remap_to_desc(irq, desc);
408                 }
409                 if (likely(!(desc->status & IRQ_DISABLED))) {
410                         action_ret = handle_IRQ_event(irq, desc->action);
411                         if (!noirqdebug)
412                                 note_interrupt(irq, desc, action_ret);
413                 }
414                 desc->chip->end(irq);
415                 return 1;
416         }
417
418         spin_lock(&desc->lock);
419         if (desc->chip->ack) {
420                 desc->chip->ack(irq);
421                 desc = irq_remap_to_desc(irq, desc);
422         }
423         /*
424          * REPLAY is when Linux resends an IRQ that was dropped earlier
425          * WAITING is used by probe to mark irqs that are being tested
426          */
427         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
428         status |= IRQ_PENDING; /* we _want_ to handle it */
429
430         /*
431          * If the IRQ is disabled for whatever reason, we cannot
432          * use the action we have.
433          */
434         action = NULL;
435         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
436                 action = desc->action;
437                 status &= ~IRQ_PENDING; /* we commit to handling */
438                 status |= IRQ_INPROGRESS; /* we are handling it */
439         }
440         desc->status = status;
441
442         /*
443          * If there is no IRQ handler or it was disabled, exit early.
444          * Since we set PENDING, if another processor is handling
445          * a different instance of this same irq, the other processor
446          * will take care of it.
447          */
448         if (unlikely(!action))
449                 goto out;
450
451         /*
452          * Edge triggered interrupts need to remember
453          * pending events.
454          * This applies to any hw interrupts that allow a second
455          * instance of the same irq to arrive while we are in do_IRQ
456          * or in the handler. But the code here only handles the _second_
457          * instance of the irq, not the third or fourth. So it is mostly
458          * useful for irq hardware that does not mask cleanly in an
459          * SMP environment.
460          */
461         for (;;) {
462                 irqreturn_t action_ret;
463
464                 spin_unlock(&desc->lock);
465
466                 action_ret = handle_IRQ_event(irq, action);
467                 if (!noirqdebug)
468                         note_interrupt(irq, desc, action_ret);
469
470                 spin_lock(&desc->lock);
471                 if (likely(!(desc->status & IRQ_PENDING)))
472                         break;
473                 desc->status &= ~IRQ_PENDING;
474         }
475         desc->status &= ~IRQ_INPROGRESS;
476
477 out:
478         /*
479          * The ->end() handler has to deal with interrupts which got
480          * disabled while the handler was running.
481          */
482         desc->chip->end(irq);
483         spin_unlock(&desc->lock);
484
485         return 1;
486 }
487 #endif
488
489 void early_init_irq_lock_class(void)
490 {
491         struct irq_desc *desc;
492         int i;
493
494         for_each_irq_desc(i, desc) {
495                 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
496         }
497 }
498
499 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
500 {
501         struct irq_desc *desc = irq_to_desc(irq);
502         return desc ? desc->kstat_irqs[cpu] : 0;
503 }
504 EXPORT_SYMBOL(kstat_irqs_cpu);
505