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