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