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