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