genirq: remove sparse irq code
[linux-2.6.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 /*
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 static void __init init_work(void *data)
115 {
116         struct dyn_array *da = data;
117         int i;
118         struct  irq_desc *desc;
119
120         desc = *da->name;
121
122         for (i = 0; i < *da->nr; i++) {
123                 init_one_irq_desc(&desc[i]);
124                 desc[i].irq = i;
125         }
126
127         /* init kstat_irqs, nr_cpu_ids is ready already */
128         init_kstat_irqs(desc, *da->nr, nr_cpu_ids);
129 }
130
131 struct irq_desc *irq_desc;
132 DEFINE_DYN_ARRAY(irq_desc, sizeof(struct irq_desc), nr_irqs, PAGE_SIZE, init_work);
133
134 #else
135
136 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
137         [0 ... NR_IRQS-1] = {
138                 .status = IRQ_DISABLED,
139                 .chip = &no_irq_chip,
140                 .handle_irq = handle_bad_irq,
141                 .depth = 1,
142                 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
143 #ifdef CONFIG_SMP
144                 .affinity = CPU_MASK_ALL
145 #endif
146         }
147 };
148
149 #endif
150
151 /*
152  * What should we do if we get a hw irq event on an illegal vector?
153  * Each architecture has to answer this themself.
154  */
155 static void ack_bad(unsigned int irq)
156 {
157         struct irq_desc *desc;
158
159         desc = irq_to_desc(irq);
160         print_irq_desc(irq, desc);
161         ack_bad_irq(irq);
162 }
163
164 /*
165  * NOP functions
166  */
167 static void noop(unsigned int irq)
168 {
169 }
170
171 static unsigned int noop_ret(unsigned int irq)
172 {
173         return 0;
174 }
175
176 /*
177  * Generic no controller implementation
178  */
179 struct irq_chip no_irq_chip = {
180         .name           = "none",
181         .startup        = noop_ret,
182         .shutdown       = noop,
183         .enable         = noop,
184         .disable        = noop,
185         .ack            = ack_bad,
186         .end            = noop,
187 };
188
189 /*
190  * Generic dummy implementation which can be used for
191  * real dumb interrupt sources
192  */
193 struct irq_chip dummy_irq_chip = {
194         .name           = "dummy",
195         .startup        = noop_ret,
196         .shutdown       = noop,
197         .enable         = noop,
198         .disable        = noop,
199         .ack            = noop,
200         .mask           = noop,
201         .unmask         = noop,
202         .end            = noop,
203 };
204
205 /*
206  * Special, empty irq handler:
207  */
208 irqreturn_t no_action(int cpl, void *dev_id)
209 {
210         return IRQ_NONE;
211 }
212
213 /**
214  * handle_IRQ_event - irq action chain handler
215  * @irq:        the interrupt number
216  * @action:     the interrupt action chain for this irq
217  *
218  * Handles the action chain of an irq event
219  */
220 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
221 {
222         irqreturn_t ret, retval = IRQ_NONE;
223         unsigned int status = 0;
224
225         if (!(action->flags & IRQF_DISABLED))
226                 local_irq_enable_in_hardirq();
227
228         do {
229                 ret = action->handler(irq, action->dev_id);
230                 if (ret == IRQ_HANDLED)
231                         status |= action->flags;
232                 retval |= ret;
233                 action = action->next;
234         } while (action);
235
236         if (status & IRQF_SAMPLE_RANDOM)
237                 add_interrupt_randomness(irq);
238         local_irq_disable();
239
240         return retval;
241 }
242
243 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
244 /**
245  * __do_IRQ - original all in one highlevel IRQ handler
246  * @irq:        the interrupt number
247  *
248  * __do_IRQ handles all normal device IRQ's (the special
249  * SMP cross-CPU interrupts have their own specific
250  * handlers).
251  *
252  * This is the original x86 implementation which is used for every
253  * interrupt type.
254  */
255 unsigned int __do_IRQ(unsigned int irq)
256 {
257         struct irq_desc *desc = irq_to_desc(irq);
258         struct irqaction *action;
259         unsigned int status;
260
261 #ifdef CONFIG_HAVE_DYN_ARRAY
262         kstat_irqs_this_cpu(desc)++;
263 #else
264         kstat_irqs_this_cpu(irq)++;
265 #endif
266         if (CHECK_IRQ_PER_CPU(desc->status)) {
267                 irqreturn_t action_ret;
268
269                 /*
270                  * No locking required for CPU-local interrupts:
271                  */
272                 if (desc->chip->ack)
273                         desc->chip->ack(irq);
274                 if (likely(!(desc->status & IRQ_DISABLED))) {
275                         action_ret = handle_IRQ_event(irq, desc->action);
276                         if (!noirqdebug)
277                                 note_interrupt(irq, desc, action_ret);
278                 }
279                 desc->chip->end(irq);
280                 return 1;
281         }
282
283         spin_lock(&desc->lock);
284         if (desc->chip->ack)
285                 desc->chip->ack(irq);
286         /*
287          * REPLAY is when Linux resends an IRQ that was dropped earlier
288          * WAITING is used by probe to mark irqs that are being tested
289          */
290         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
291         status |= IRQ_PENDING; /* we _want_ to handle it */
292
293         /*
294          * If the IRQ is disabled for whatever reason, we cannot
295          * use the action we have.
296          */
297         action = NULL;
298         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
299                 action = desc->action;
300                 status &= ~IRQ_PENDING; /* we commit to handling */
301                 status |= IRQ_INPROGRESS; /* we are handling it */
302         }
303         desc->status = status;
304
305         /*
306          * If there is no IRQ handler or it was disabled, exit early.
307          * Since we set PENDING, if another processor is handling
308          * a different instance of this same irq, the other processor
309          * will take care of it.
310          */
311         if (unlikely(!action))
312                 goto out;
313
314         /*
315          * Edge triggered interrupts need to remember
316          * pending events.
317          * This applies to any hw interrupts that allow a second
318          * instance of the same irq to arrive while we are in do_IRQ
319          * or in the handler. But the code here only handles the _second_
320          * instance of the irq, not the third or fourth. So it is mostly
321          * useful for irq hardware that does not mask cleanly in an
322          * SMP environment.
323          */
324         for (;;) {
325                 irqreturn_t action_ret;
326
327                 spin_unlock(&desc->lock);
328
329                 action_ret = handle_IRQ_event(irq, action);
330                 if (!noirqdebug)
331                         note_interrupt(irq, desc, action_ret);
332
333                 spin_lock(&desc->lock);
334                 if (likely(!(desc->status & IRQ_PENDING)))
335                         break;
336                 desc->status &= ~IRQ_PENDING;
337         }
338         desc->status &= ~IRQ_INPROGRESS;
339
340 out:
341         /*
342          * The ->end() handler has to deal with interrupts which got
343          * disabled while the handler was running.
344          */
345         desc->chip->end(irq);
346         spin_unlock(&desc->lock);
347
348         return 1;
349 }
350 #endif
351
352
353 #ifdef CONFIG_TRACE_IRQFLAGS
354 void early_init_irq_lock_class(void)
355 {
356 #ifndef CONFIG_HAVE_DYN_ARRAY
357         int i;
358
359         for (i = 0; i < nr_irqs; i++)
360                 lockdep_set_class(&irq_desc[i].lock, &irq_desc_lock_class);
361 #endif
362 }
363 #endif
364
365 #ifdef CONFIG_HAVE_DYN_ARRAY
366 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
367 {
368         struct irq_desc *desc = irq_to_desc(irq);
369         return desc->kstat_irqs[cpu];
370 }
371 #endif
372 EXPORT_SYMBOL(kstat_irqs_cpu);
373