]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - kernel/irq/handle.c
cpumask: remove cpumask allocation from idle_balance, fix
[linux-2.6-omap-h63xx.git] / 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         unsigned long bytes;
86         char *ptr;
87         int node;
88
89         /* Compute how many bytes we need per irq and allocate them */
90         bytes = nr * sizeof(unsigned int);
91
92         node = cpu_to_node(cpu);
93         ptr = kzalloc_node(bytes, GFP_ATOMIC, node);
94         printk(KERN_DEBUG "  alloc kstat_irqs on cpu %d node %d\n", cpu, node);
95
96         if (ptr)
97                 desc->kstat_irqs = (unsigned int *)ptr;
98 }
99
100 static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
101 {
102         memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
103
104         spin_lock_init(&desc->lock);
105         desc->irq = irq;
106 #ifdef CONFIG_SMP
107         desc->cpu = cpu;
108 #endif
109         lockdep_set_class(&desc->lock, &irq_desc_lock_class);
110         init_kstat_irqs(desc, cpu, nr_cpu_ids);
111         if (!desc->kstat_irqs) {
112                 printk(KERN_ERR "can not alloc kstat_irqs\n");
113                 BUG_ON(1);
114         }
115         if (!init_alloc_desc_masks(desc, cpu, false)) {
116                 printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
117                 BUG_ON(1);
118         }
119         arch_init_chip_data(desc, cpu);
120 }
121
122 /*
123  * Protect the sparse_irqs:
124  */
125 DEFINE_SPINLOCK(sparse_irq_lock);
126
127 struct irq_desc **irq_desc_ptrs __read_mostly;
128
129 static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
130         [0 ... NR_IRQS_LEGACY-1] = {
131                 .irq        = -1,
132                 .status     = IRQ_DISABLED,
133                 .chip       = &no_irq_chip,
134                 .handle_irq = handle_bad_irq,
135                 .depth      = 1,
136                 .lock       = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
137         }
138 };
139
140 static unsigned int *kstat_irqs_legacy;
141
142 int __init early_irq_init(void)
143 {
144         struct irq_desc *desc;
145         int legacy_count;
146         int i;
147
148         init_irq_default_affinity();
149
150          /* initialize nr_irqs based on nr_cpu_ids */
151         arch_probe_nr_irqs();
152         printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
153
154         desc = irq_desc_legacy;
155         legacy_count = ARRAY_SIZE(irq_desc_legacy);
156
157         /* allocate irq_desc_ptrs array based on nr_irqs */
158         irq_desc_ptrs = alloc_bootmem(nr_irqs * sizeof(void *));
159
160         /* allocate based on nr_cpu_ids */
161         /* FIXME: invert kstat_irgs, and it'd be a per_cpu_alloc'd thing */
162         kstat_irqs_legacy = alloc_bootmem(NR_IRQS_LEGACY * nr_cpu_ids *
163                                           sizeof(int));
164
165         for (i = 0; i < legacy_count; i++) {
166                 desc[i].irq = i;
167                 desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
168                 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
169                 init_alloc_desc_masks(&desc[i], 0, true);
170                 irq_desc_ptrs[i] = desc + i;
171         }
172
173         for (i = legacy_count; i < nr_irqs; i++)
174                 irq_desc_ptrs[i] = NULL;
175
176         return arch_early_irq_init();
177 }
178
179 struct irq_desc *irq_to_desc(unsigned int irq)
180 {
181         if (irq_desc_ptrs && irq < nr_irqs)
182                 return irq_desc_ptrs[irq];
183
184         return NULL;
185 }
186
187 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
188 {
189         struct irq_desc *desc;
190         unsigned long flags;
191         int node;
192
193         if (irq >= nr_irqs) {
194                 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
195                         irq, nr_irqs);
196                 return NULL;
197         }
198
199         desc = irq_desc_ptrs[irq];
200         if (desc)
201                 return desc;
202
203         spin_lock_irqsave(&sparse_irq_lock, flags);
204
205         /* We have to check it to avoid races with another CPU */
206         desc = irq_desc_ptrs[irq];
207         if (desc)
208                 goto out_unlock;
209
210         node = cpu_to_node(cpu);
211         desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
212         printk(KERN_DEBUG "  alloc irq_desc for %d on cpu %d node %d\n",
213                  irq, cpu, node);
214         if (!desc) {
215                 printk(KERN_ERR "can not alloc irq_desc\n");
216                 BUG_ON(1);
217         }
218         init_one_irq_desc(irq, desc, cpu);
219
220         irq_desc_ptrs[irq] = desc;
221
222 out_unlock:
223         spin_unlock_irqrestore(&sparse_irq_lock, flags);
224
225         return desc;
226 }
227
228 #else /* !CONFIG_SPARSE_IRQ */
229
230 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
231         [0 ... NR_IRQS-1] = {
232                 .status = IRQ_DISABLED,
233                 .chip = &no_irq_chip,
234                 .handle_irq = handle_bad_irq,
235                 .depth = 1,
236                 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
237         }
238 };
239
240 int __init early_irq_init(void)
241 {
242         struct irq_desc *desc;
243         int count;
244         int i;
245
246         init_irq_default_affinity();
247
248         printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
249
250         desc = irq_desc;
251         count = ARRAY_SIZE(irq_desc);
252
253         for (i = 0; i < count; i++) {
254                 desc[i].irq = i;
255                 init_alloc_desc_masks(&desc[i], 0, true);
256         }
257         return arch_early_irq_init();
258 }
259
260 struct irq_desc *irq_to_desc(unsigned int irq)
261 {
262         return (irq < NR_IRQS) ? irq_desc + irq : NULL;
263 }
264
265 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
266 {
267         return irq_to_desc(irq);
268 }
269 #endif /* !CONFIG_SPARSE_IRQ */
270
271 /*
272  * What should we do if we get a hw irq event on an illegal vector?
273  * Each architecture has to answer this themself.
274  */
275 static void ack_bad(unsigned int irq)
276 {
277         struct irq_desc *desc = irq_to_desc(irq);
278
279         print_irq_desc(irq, desc);
280         ack_bad_irq(irq);
281 }
282
283 /*
284  * NOP functions
285  */
286 static void noop(unsigned int irq)
287 {
288 }
289
290 static unsigned int noop_ret(unsigned int irq)
291 {
292         return 0;
293 }
294
295 /*
296  * Generic no controller implementation
297  */
298 struct irq_chip no_irq_chip = {
299         .name           = "none",
300         .startup        = noop_ret,
301         .shutdown       = noop,
302         .enable         = noop,
303         .disable        = noop,
304         .ack            = ack_bad,
305         .end            = noop,
306 };
307
308 /*
309  * Generic dummy implementation which can be used for
310  * real dumb interrupt sources
311  */
312 struct irq_chip dummy_irq_chip = {
313         .name           = "dummy",
314         .startup        = noop_ret,
315         .shutdown       = noop,
316         .enable         = noop,
317         .disable        = noop,
318         .ack            = noop,
319         .mask           = noop,
320         .unmask         = noop,
321         .end            = noop,
322 };
323
324 /*
325  * Special, empty irq handler:
326  */
327 irqreturn_t no_action(int cpl, void *dev_id)
328 {
329         return IRQ_NONE;
330 }
331
332 /**
333  * handle_IRQ_event - irq action chain handler
334  * @irq:        the interrupt number
335  * @action:     the interrupt action chain for this irq
336  *
337  * Handles the action chain of an irq event
338  */
339 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
340 {
341         irqreturn_t ret, retval = IRQ_NONE;
342         unsigned int status = 0;
343
344         if (!(action->flags & IRQF_DISABLED))
345                 local_irq_enable_in_hardirq();
346
347         do {
348                 ret = action->handler(irq, action->dev_id);
349                 if (ret == IRQ_HANDLED)
350                         status |= action->flags;
351                 retval |= ret;
352                 action = action->next;
353         } while (action);
354
355         if (status & IRQF_SAMPLE_RANDOM)
356                 add_interrupt_randomness(irq);
357         local_irq_disable();
358
359         return retval;
360 }
361
362 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
363 /**
364  * __do_IRQ - original all in one highlevel IRQ handler
365  * @irq:        the interrupt number
366  *
367  * __do_IRQ handles all normal device IRQ's (the special
368  * SMP cross-CPU interrupts have their own specific
369  * handlers).
370  *
371  * This is the original x86 implementation which is used for every
372  * interrupt type.
373  */
374 unsigned int __do_IRQ(unsigned int irq)
375 {
376         struct irq_desc *desc = irq_to_desc(irq);
377         struct irqaction *action;
378         unsigned int status;
379
380         kstat_incr_irqs_this_cpu(irq, desc);
381
382         if (CHECK_IRQ_PER_CPU(desc->status)) {
383                 irqreturn_t action_ret;
384
385                 /*
386                  * No locking required for CPU-local interrupts:
387                  */
388                 if (desc->chip->ack) {
389                         desc->chip->ack(irq);
390                         /* get new one */
391                         desc = irq_remap_to_desc(irq, desc);
392                 }
393                 if (likely(!(desc->status & IRQ_DISABLED))) {
394                         action_ret = handle_IRQ_event(irq, desc->action);
395                         if (!noirqdebug)
396                                 note_interrupt(irq, desc, action_ret);
397                 }
398                 desc->chip->end(irq);
399                 return 1;
400         }
401
402         spin_lock(&desc->lock);
403         if (desc->chip->ack) {
404                 desc->chip->ack(irq);
405                 desc = irq_remap_to_desc(irq, desc);
406         }
407         /*
408          * REPLAY is when Linux resends an IRQ that was dropped earlier
409          * WAITING is used by probe to mark irqs that are being tested
410          */
411         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
412         status |= IRQ_PENDING; /* we _want_ to handle it */
413
414         /*
415          * If the IRQ is disabled for whatever reason, we cannot
416          * use the action we have.
417          */
418         action = NULL;
419         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
420                 action = desc->action;
421                 status &= ~IRQ_PENDING; /* we commit to handling */
422                 status |= IRQ_INPROGRESS; /* we are handling it */
423         }
424         desc->status = status;
425
426         /*
427          * If there is no IRQ handler or it was disabled, exit early.
428          * Since we set PENDING, if another processor is handling
429          * a different instance of this same irq, the other processor
430          * will take care of it.
431          */
432         if (unlikely(!action))
433                 goto out;
434
435         /*
436          * Edge triggered interrupts need to remember
437          * pending events.
438          * This applies to any hw interrupts that allow a second
439          * instance of the same irq to arrive while we are in do_IRQ
440          * or in the handler. But the code here only handles the _second_
441          * instance of the irq, not the third or fourth. So it is mostly
442          * useful for irq hardware that does not mask cleanly in an
443          * SMP environment.
444          */
445         for (;;) {
446                 irqreturn_t action_ret;
447
448                 spin_unlock(&desc->lock);
449
450                 action_ret = handle_IRQ_event(irq, action);
451                 if (!noirqdebug)
452                         note_interrupt(irq, desc, action_ret);
453
454                 spin_lock(&desc->lock);
455                 if (likely(!(desc->status & IRQ_PENDING)))
456                         break;
457                 desc->status &= ~IRQ_PENDING;
458         }
459         desc->status &= ~IRQ_INPROGRESS;
460
461 out:
462         /*
463          * The ->end() handler has to deal with interrupts which got
464          * disabled while the handler was running.
465          */
466         desc->chip->end(irq);
467         spin_unlock(&desc->lock);
468
469         return 1;
470 }
471 #endif
472
473 void early_init_irq_lock_class(void)
474 {
475         struct irq_desc *desc;
476         int i;
477
478         for_each_irq_desc(i, desc) {
479                 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
480         }
481 }
482
483 #ifdef CONFIG_SPARSE_IRQ
484 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
485 {
486         struct irq_desc *desc = irq_to_desc(irq);
487         return desc ? desc->kstat_irqs[cpu] : 0;
488 }
489 #endif
490 EXPORT_SYMBOL(kstat_irqs_cpu);
491