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