tracing: Don't assume possible cpu list have continuous numbers
[linux-2.6/btrfs-unstable.git] / kernel / irq / handle.c
blob412370ab9a34cdc8ed5ec0fac33a75b3bbeb7d0d
1 /*
2 * linux/kernel/irq/handle.c
4 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
7 * This file contains the core interrupt handling code.
9 * Detailed information is available in Documentation/DocBook/genericirq
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 #include <trace/irq.h>
21 #include <linux/bootmem.h>
23 #include "internals.h"
26 * lockdep: we want to handle all irq_desc locks as a single lock-class:
28 struct lock_class_key irq_desc_lock_class;
30 /**
31 * handle_bad_irq - handle spurious and unhandled irqs
32 * @irq: the interrupt number
33 * @desc: description of the interrupt
35 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
37 void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
39 print_irq_desc(irq, desc);
40 kstat_incr_irqs_this_cpu(irq, desc);
41 ack_bad_irq(irq);
44 #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
45 static void __init init_irq_default_affinity(void)
47 alloc_bootmem_cpumask_var(&irq_default_affinity);
48 cpumask_setall(irq_default_affinity);
50 #else
51 static void __init init_irq_default_affinity(void)
54 #endif
57 * Linux has a controller-independent interrupt architecture.
58 * Every controller has a 'controller-template', that is used
59 * by the main code to do the right thing. Each driver-visible
60 * interrupt source is transparently wired to the appropriate
61 * controller. Thus drivers need not be aware of the
62 * interrupt-controller.
64 * The code is designed to be easily extended with new/different
65 * interrupt controllers, without having to do assembly magic or
66 * having to touch the generic code.
68 * Controller mappings for all interrupt sources:
70 int nr_irqs = NR_IRQS;
71 EXPORT_SYMBOL_GPL(nr_irqs);
73 #ifdef CONFIG_SPARSE_IRQ
75 static struct irq_desc irq_desc_init = {
76 .irq = -1,
77 .status = IRQ_DISABLED,
78 .chip = &no_irq_chip,
79 .handle_irq = handle_bad_irq,
80 .depth = 1,
81 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
84 void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
86 unsigned long bytes;
87 char *ptr;
88 int node;
90 /* Compute how many bytes we need per irq and allocate them */
91 bytes = nr * sizeof(unsigned int);
93 node = cpu_to_node(cpu);
94 ptr = kzalloc_node(bytes, GFP_ATOMIC, node);
95 printk(KERN_DEBUG " alloc kstat_irqs on cpu %d node %d\n", cpu, node);
97 if (ptr)
98 desc->kstat_irqs = (unsigned int *)ptr;
101 static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
103 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
105 spin_lock_init(&desc->lock);
106 desc->irq = irq;
107 #ifdef CONFIG_SMP
108 desc->cpu = cpu;
109 #endif
110 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
111 init_kstat_irqs(desc, cpu, nr_cpu_ids);
112 if (!desc->kstat_irqs) {
113 printk(KERN_ERR "can not alloc kstat_irqs\n");
114 BUG_ON(1);
116 if (!init_alloc_desc_masks(desc, cpu, false)) {
117 printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
118 BUG_ON(1);
120 arch_init_chip_data(desc, cpu);
124 * Protect the sparse_irqs:
126 DEFINE_SPINLOCK(sparse_irq_lock);
128 struct irq_desc **irq_desc_ptrs __read_mostly;
130 static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
131 [0 ... NR_IRQS_LEGACY-1] = {
132 .irq = -1,
133 .status = IRQ_DISABLED,
134 .chip = &no_irq_chip,
135 .handle_irq = handle_bad_irq,
136 .depth = 1,
137 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
141 static unsigned int *kstat_irqs_legacy;
143 int __init early_irq_init(void)
145 struct irq_desc *desc;
146 int legacy_count;
147 int i;
149 init_irq_default_affinity();
151 /* initialize nr_irqs based on nr_cpu_ids */
152 arch_probe_nr_irqs();
153 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
155 desc = irq_desc_legacy;
156 legacy_count = ARRAY_SIZE(irq_desc_legacy);
158 /* allocate irq_desc_ptrs array based on nr_irqs */
159 irq_desc_ptrs = alloc_bootmem(nr_irqs * sizeof(void *));
161 /* allocate based on nr_cpu_ids */
162 /* FIXME: invert kstat_irgs, and it'd be a per_cpu_alloc'd thing */
163 kstat_irqs_legacy = alloc_bootmem(NR_IRQS_LEGACY * nr_cpu_ids *
164 sizeof(int));
166 for (i = 0; i < legacy_count; i++) {
167 desc[i].irq = i;
168 desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
169 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
170 init_alloc_desc_masks(&desc[i], 0, true);
171 irq_desc_ptrs[i] = desc + i;
174 for (i = legacy_count; i < nr_irqs; i++)
175 irq_desc_ptrs[i] = NULL;
177 return arch_early_irq_init();
180 struct irq_desc *irq_to_desc(unsigned int irq)
182 if (irq_desc_ptrs && irq < nr_irqs)
183 return irq_desc_ptrs[irq];
185 return NULL;
188 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
190 struct irq_desc *desc;
191 unsigned long flags;
192 int node;
194 if (irq >= nr_irqs) {
195 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
196 irq, nr_irqs);
197 return NULL;
200 desc = irq_desc_ptrs[irq];
201 if (desc)
202 return desc;
204 spin_lock_irqsave(&sparse_irq_lock, flags);
206 /* We have to check it to avoid races with another CPU */
207 desc = irq_desc_ptrs[irq];
208 if (desc)
209 goto out_unlock;
211 node = cpu_to_node(cpu);
212 desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
213 printk(KERN_DEBUG " alloc irq_desc for %d on cpu %d node %d\n",
214 irq, cpu, node);
215 if (!desc) {
216 printk(KERN_ERR "can not alloc irq_desc\n");
217 BUG_ON(1);
219 init_one_irq_desc(irq, desc, cpu);
221 irq_desc_ptrs[irq] = desc;
223 out_unlock:
224 spin_unlock_irqrestore(&sparse_irq_lock, flags);
226 return desc;
229 #else /* !CONFIG_SPARSE_IRQ */
231 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
232 [0 ... NR_IRQS-1] = {
233 .status = IRQ_DISABLED,
234 .chip = &no_irq_chip,
235 .handle_irq = handle_bad_irq,
236 .depth = 1,
237 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
241 int __init early_irq_init(void)
243 struct irq_desc *desc;
244 int count;
245 int i;
247 init_irq_default_affinity();
249 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
251 desc = irq_desc;
252 count = ARRAY_SIZE(irq_desc);
254 for (i = 0; i < count; i++) {
255 desc[i].irq = i;
256 init_alloc_desc_masks(&desc[i], 0, true);
258 return arch_early_irq_init();
261 struct irq_desc *irq_to_desc(unsigned int irq)
263 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
266 struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
268 return irq_to_desc(irq);
270 #endif /* !CONFIG_SPARSE_IRQ */
273 * What should we do if we get a hw irq event on an illegal vector?
274 * Each architecture has to answer this themself.
276 static void ack_bad(unsigned int irq)
278 struct irq_desc *desc = irq_to_desc(irq);
280 print_irq_desc(irq, desc);
281 ack_bad_irq(irq);
285 * NOP functions
287 static void noop(unsigned int irq)
291 static unsigned int noop_ret(unsigned int irq)
293 return 0;
297 * Generic no controller implementation
299 struct irq_chip no_irq_chip = {
300 .name = "none",
301 .startup = noop_ret,
302 .shutdown = noop,
303 .enable = noop,
304 .disable = noop,
305 .ack = ack_bad,
306 .end = noop,
310 * Generic dummy implementation which can be used for
311 * real dumb interrupt sources
313 struct irq_chip dummy_irq_chip = {
314 .name = "dummy",
315 .startup = noop_ret,
316 .shutdown = noop,
317 .enable = noop,
318 .disable = noop,
319 .ack = noop,
320 .mask = noop,
321 .unmask = noop,
322 .end = noop,
326 * Special, empty irq handler:
328 irqreturn_t no_action(int cpl, void *dev_id)
330 return IRQ_NONE;
333 DEFINE_TRACE(irq_handler_entry);
334 DEFINE_TRACE(irq_handler_exit);
337 * handle_IRQ_event - irq action chain handler
338 * @irq: the interrupt number
339 * @action: the interrupt action chain for this irq
341 * Handles the action chain of an irq event
343 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
345 irqreturn_t ret, retval = IRQ_NONE;
346 unsigned int status = 0;
348 if (!(action->flags & IRQF_DISABLED))
349 local_irq_enable_in_hardirq();
351 do {
352 trace_irq_handler_entry(irq, action);
353 ret = action->handler(irq, action->dev_id);
354 trace_irq_handler_exit(irq, action, ret);
355 if (ret == IRQ_HANDLED)
356 status |= action->flags;
357 retval |= ret;
358 action = action->next;
359 } while (action);
361 if (status & IRQF_SAMPLE_RANDOM)
362 add_interrupt_randomness(irq);
363 local_irq_disable();
365 return retval;
368 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
370 * __do_IRQ - original all in one highlevel IRQ handler
371 * @irq: the interrupt number
373 * __do_IRQ handles all normal device IRQ's (the special
374 * SMP cross-CPU interrupts have their own specific
375 * handlers).
377 * This is the original x86 implementation which is used for every
378 * interrupt type.
380 unsigned int __do_IRQ(unsigned int irq)
382 struct irq_desc *desc = irq_to_desc(irq);
383 struct irqaction *action;
384 unsigned int status;
386 kstat_incr_irqs_this_cpu(irq, desc);
388 if (CHECK_IRQ_PER_CPU(desc->status)) {
389 irqreturn_t action_ret;
392 * No locking required for CPU-local interrupts:
394 if (desc->chip->ack) {
395 desc->chip->ack(irq);
396 /* get new one */
397 desc = irq_remap_to_desc(irq, desc);
399 if (likely(!(desc->status & IRQ_DISABLED))) {
400 action_ret = handle_IRQ_event(irq, desc->action);
401 if (!noirqdebug)
402 note_interrupt(irq, desc, action_ret);
404 desc->chip->end(irq);
405 return 1;
408 spin_lock(&desc->lock);
409 if (desc->chip->ack) {
410 desc->chip->ack(irq);
411 desc = irq_remap_to_desc(irq, desc);
414 * REPLAY is when Linux resends an IRQ that was dropped earlier
415 * WAITING is used by probe to mark irqs that are being tested
417 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
418 status |= IRQ_PENDING; /* we _want_ to handle it */
421 * If the IRQ is disabled for whatever reason, we cannot
422 * use the action we have.
424 action = NULL;
425 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
426 action = desc->action;
427 status &= ~IRQ_PENDING; /* we commit to handling */
428 status |= IRQ_INPROGRESS; /* we are handling it */
430 desc->status = status;
433 * If there is no IRQ handler or it was disabled, exit early.
434 * Since we set PENDING, if another processor is handling
435 * a different instance of this same irq, the other processor
436 * will take care of it.
438 if (unlikely(!action))
439 goto out;
442 * Edge triggered interrupts need to remember
443 * pending events.
444 * This applies to any hw interrupts that allow a second
445 * instance of the same irq to arrive while we are in do_IRQ
446 * or in the handler. But the code here only handles the _second_
447 * instance of the irq, not the third or fourth. So it is mostly
448 * useful for irq hardware that does not mask cleanly in an
449 * SMP environment.
451 for (;;) {
452 irqreturn_t action_ret;
454 spin_unlock(&desc->lock);
456 action_ret = handle_IRQ_event(irq, action);
457 if (!noirqdebug)
458 note_interrupt(irq, desc, action_ret);
460 spin_lock(&desc->lock);
461 if (likely(!(desc->status & IRQ_PENDING)))
462 break;
463 desc->status &= ~IRQ_PENDING;
465 desc->status &= ~IRQ_INPROGRESS;
467 out:
469 * The ->end() handler has to deal with interrupts which got
470 * disabled while the handler was running.
472 desc->chip->end(irq);
473 spin_unlock(&desc->lock);
475 return 1;
477 #endif
479 void early_init_irq_lock_class(void)
481 struct irq_desc *desc;
482 int i;
484 for_each_irq_desc(i, desc) {
485 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
489 #ifdef CONFIG_SPARSE_IRQ
490 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
492 struct irq_desc *desc = irq_to_desc(irq);
493 return desc ? desc->kstat_irqs[cpu] : 0;
495 #endif
496 EXPORT_SYMBOL(kstat_irqs_cpu);