Merge branch 'tracing-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6/kvm.git] / kernel / irq / handle.c
bloba60018402f4228b46d5870ee93f9e86ae417137a
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/slab.h>
15 #include <linux/module.h>
16 #include <linux/random.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/rculist.h>
20 #include <linux/hash.h>
21 #include <linux/bootmem.h>
22 #include <trace/events/irq.h>
24 #include "internals.h"
27 * lockdep: we want to handle all irq_desc locks as a single lock-class:
29 struct lock_class_key irq_desc_lock_class;
31 /**
32 * handle_bad_irq - handle spurious and unhandled irqs
33 * @irq: the interrupt number
34 * @desc: description of the interrupt
36 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
38 void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
40 print_irq_desc(irq, desc);
41 kstat_incr_irqs_this_cpu(irq, desc);
42 ack_bad_irq(irq);
45 #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
46 static void __init init_irq_default_affinity(void)
48 alloc_bootmem_cpumask_var(&irq_default_affinity);
49 cpumask_setall(irq_default_affinity);
51 #else
52 static void __init init_irq_default_affinity(void)
55 #endif
58 * Linux has a controller-independent interrupt architecture.
59 * Every controller has a 'controller-template', that is used
60 * by the main code to do the right thing. Each driver-visible
61 * interrupt source is transparently wired to the appropriate
62 * controller. Thus drivers need not be aware of the
63 * interrupt-controller.
65 * The code is designed to be easily extended with new/different
66 * interrupt controllers, without having to do assembly magic or
67 * having to touch the generic code.
69 * Controller mappings for all interrupt sources:
71 int nr_irqs = NR_IRQS;
72 EXPORT_SYMBOL_GPL(nr_irqs);
74 #ifdef CONFIG_SPARSE_IRQ
76 static struct irq_desc irq_desc_init = {
77 .irq = -1,
78 .status = IRQ_DISABLED,
79 .chip = &no_irq_chip,
80 .handle_irq = handle_bad_irq,
81 .depth = 1,
82 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
85 void __ref init_kstat_irqs(struct irq_desc *desc, int node, int nr)
87 void *ptr;
89 if (slab_is_available())
90 ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs),
91 GFP_ATOMIC, node);
92 else
93 ptr = alloc_bootmem_node(NODE_DATA(node),
94 nr * sizeof(*desc->kstat_irqs));
97 * don't overwite if can not get new one
98 * init_copy_kstat_irqs() could still use old one
100 if (ptr) {
101 printk(KERN_DEBUG " alloc kstat_irqs on node %d\n", node);
102 desc->kstat_irqs = ptr;
106 static void init_one_irq_desc(int irq, struct irq_desc *desc, int node)
108 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
110 spin_lock_init(&desc->lock);
111 desc->irq = irq;
112 #ifdef CONFIG_SMP
113 desc->node = node;
114 #endif
115 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
116 init_kstat_irqs(desc, node, nr_cpu_ids);
117 if (!desc->kstat_irqs) {
118 printk(KERN_ERR "can not alloc kstat_irqs\n");
119 BUG_ON(1);
121 if (!alloc_desc_masks(desc, node, false)) {
122 printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
123 BUG_ON(1);
125 init_desc_masks(desc);
126 arch_init_chip_data(desc, node);
130 * Protect the sparse_irqs:
132 DEFINE_SPINLOCK(sparse_irq_lock);
134 struct irq_desc **irq_desc_ptrs __read_mostly;
136 static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
137 [0 ... NR_IRQS_LEGACY-1] = {
138 .irq = -1,
139 .status = IRQ_DISABLED,
140 .chip = &no_irq_chip,
141 .handle_irq = handle_bad_irq,
142 .depth = 1,
143 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
147 static unsigned int *kstat_irqs_legacy;
149 int __init early_irq_init(void)
151 struct irq_desc *desc;
152 int legacy_count;
153 int i;
155 init_irq_default_affinity();
157 /* initialize nr_irqs based on nr_cpu_ids */
158 arch_probe_nr_irqs();
159 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
161 desc = irq_desc_legacy;
162 legacy_count = ARRAY_SIZE(irq_desc_legacy);
164 /* allocate irq_desc_ptrs array based on nr_irqs */
165 irq_desc_ptrs = alloc_bootmem(nr_irqs * sizeof(void *));
167 /* allocate based on nr_cpu_ids */
168 /* FIXME: invert kstat_irgs, and it'd be a per_cpu_alloc'd thing */
169 kstat_irqs_legacy = alloc_bootmem(NR_IRQS_LEGACY * nr_cpu_ids *
170 sizeof(int));
172 for (i = 0; i < legacy_count; i++) {
173 desc[i].irq = i;
174 desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
175 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
176 alloc_desc_masks(&desc[i], 0, true);
177 init_desc_masks(&desc[i]);
178 irq_desc_ptrs[i] = desc + i;
181 for (i = legacy_count; i < nr_irqs; i++)
182 irq_desc_ptrs[i] = NULL;
184 return arch_early_irq_init();
187 struct irq_desc *irq_to_desc(unsigned int irq)
189 if (irq_desc_ptrs && irq < nr_irqs)
190 return irq_desc_ptrs[irq];
192 return NULL;
195 struct irq_desc * __ref irq_to_desc_alloc_node(unsigned int irq, int node)
197 struct irq_desc *desc;
198 unsigned long flags;
200 if (irq >= nr_irqs) {
201 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
202 irq, nr_irqs);
203 return NULL;
206 desc = irq_desc_ptrs[irq];
207 if (desc)
208 return desc;
210 spin_lock_irqsave(&sparse_irq_lock, flags);
212 /* We have to check it to avoid races with another CPU */
213 desc = irq_desc_ptrs[irq];
214 if (desc)
215 goto out_unlock;
217 if (slab_is_available())
218 desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
219 else
220 desc = alloc_bootmem_node(NODE_DATA(node), sizeof(*desc));
222 printk(KERN_DEBUG " alloc irq_desc for %d on node %d\n", irq, node);
223 if (!desc) {
224 printk(KERN_ERR "can not alloc irq_desc\n");
225 BUG_ON(1);
227 init_one_irq_desc(irq, desc, node);
229 irq_desc_ptrs[irq] = desc;
231 out_unlock:
232 spin_unlock_irqrestore(&sparse_irq_lock, flags);
234 return desc;
237 #else /* !CONFIG_SPARSE_IRQ */
239 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
240 [0 ... NR_IRQS-1] = {
241 .status = IRQ_DISABLED,
242 .chip = &no_irq_chip,
243 .handle_irq = handle_bad_irq,
244 .depth = 1,
245 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
249 static unsigned int kstat_irqs_all[NR_IRQS][NR_CPUS];
250 int __init early_irq_init(void)
252 struct irq_desc *desc;
253 int count;
254 int i;
256 init_irq_default_affinity();
258 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
260 desc = irq_desc;
261 count = ARRAY_SIZE(irq_desc);
263 for (i = 0; i < count; i++) {
264 desc[i].irq = i;
265 alloc_desc_masks(&desc[i], 0, true);
266 init_desc_masks(&desc[i]);
267 desc[i].kstat_irqs = kstat_irqs_all[i];
269 return arch_early_irq_init();
272 struct irq_desc *irq_to_desc(unsigned int irq)
274 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
277 struct irq_desc *irq_to_desc_alloc_node(unsigned int irq, int node)
279 return irq_to_desc(irq);
281 #endif /* !CONFIG_SPARSE_IRQ */
283 void clear_kstat_irqs(struct irq_desc *desc)
285 memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
289 * What should we do if we get a hw irq event on an illegal vector?
290 * Each architecture has to answer this themself.
292 static void ack_bad(unsigned int irq)
294 struct irq_desc *desc = irq_to_desc(irq);
296 print_irq_desc(irq, desc);
297 ack_bad_irq(irq);
301 * NOP functions
303 static void noop(unsigned int irq)
307 static unsigned int noop_ret(unsigned int irq)
309 return 0;
313 * Generic no controller implementation
315 struct irq_chip no_irq_chip = {
316 .name = "none",
317 .startup = noop_ret,
318 .shutdown = noop,
319 .enable = noop,
320 .disable = noop,
321 .ack = ack_bad,
322 .end = noop,
326 * Generic dummy implementation which can be used for
327 * real dumb interrupt sources
329 struct irq_chip dummy_irq_chip = {
330 .name = "dummy",
331 .startup = noop_ret,
332 .shutdown = noop,
333 .enable = noop,
334 .disable = noop,
335 .ack = noop,
336 .mask = noop,
337 .unmask = noop,
338 .end = noop,
342 * Special, empty irq handler:
344 irqreturn_t no_action(int cpl, void *dev_id)
346 return IRQ_NONE;
349 static void warn_no_thread(unsigned int irq, struct irqaction *action)
351 if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags))
352 return;
354 printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD "
355 "but no thread function available.", irq, action->name);
359 * handle_IRQ_event - irq action chain handler
360 * @irq: the interrupt number
361 * @action: the interrupt action chain for this irq
363 * Handles the action chain of an irq event
365 irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
367 irqreturn_t ret, retval = IRQ_NONE;
368 unsigned int status = 0;
370 if (!(action->flags & IRQF_DISABLED))
371 local_irq_enable_in_hardirq();
373 do {
374 trace_irq_handler_entry(irq, action);
375 ret = action->handler(irq, action->dev_id);
376 trace_irq_handler_exit(irq, action, ret);
378 switch (ret) {
379 case IRQ_WAKE_THREAD:
381 * Set result to handled so the spurious check
382 * does not trigger.
384 ret = IRQ_HANDLED;
387 * Catch drivers which return WAKE_THREAD but
388 * did not set up a thread function
390 if (unlikely(!action->thread_fn)) {
391 warn_no_thread(irq, action);
392 break;
396 * Wake up the handler thread for this
397 * action. In case the thread crashed and was
398 * killed we just pretend that we handled the
399 * interrupt. The hardirq handler above has
400 * disabled the device interrupt, so no irq
401 * storm is lurking.
403 if (likely(!test_bit(IRQTF_DIED,
404 &action->thread_flags))) {
405 set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
406 wake_up_process(action->thread);
409 /* Fall through to add to randomness */
410 case IRQ_HANDLED:
411 status |= action->flags;
412 break;
414 default:
415 break;
418 retval |= ret;
419 action = action->next;
420 } while (action);
422 if (status & IRQF_SAMPLE_RANDOM)
423 add_interrupt_randomness(irq);
424 local_irq_disable();
426 return retval;
429 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
431 #ifdef CONFIG_ENABLE_WARN_DEPRECATED
432 # warning __do_IRQ is deprecated. Please convert to proper flow handlers
433 #endif
436 * __do_IRQ - original all in one highlevel IRQ handler
437 * @irq: the interrupt number
439 * __do_IRQ handles all normal device IRQ's (the special
440 * SMP cross-CPU interrupts have their own specific
441 * handlers).
443 * This is the original x86 implementation which is used for every
444 * interrupt type.
446 unsigned int __do_IRQ(unsigned int irq)
448 struct irq_desc *desc = irq_to_desc(irq);
449 struct irqaction *action;
450 unsigned int status;
452 kstat_incr_irqs_this_cpu(irq, desc);
454 if (CHECK_IRQ_PER_CPU(desc->status)) {
455 irqreturn_t action_ret;
458 * No locking required for CPU-local interrupts:
460 if (desc->chip->ack)
461 desc->chip->ack(irq);
462 if (likely(!(desc->status & IRQ_DISABLED))) {
463 action_ret = handle_IRQ_event(irq, desc->action);
464 if (!noirqdebug)
465 note_interrupt(irq, desc, action_ret);
467 desc->chip->end(irq);
468 return 1;
471 spin_lock(&desc->lock);
472 if (desc->chip->ack)
473 desc->chip->ack(irq);
475 * REPLAY is when Linux resends an IRQ that was dropped earlier
476 * WAITING is used by probe to mark irqs that are being tested
478 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
479 status |= IRQ_PENDING; /* we _want_ to handle it */
482 * If the IRQ is disabled for whatever reason, we cannot
483 * use the action we have.
485 action = NULL;
486 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
487 action = desc->action;
488 status &= ~IRQ_PENDING; /* we commit to handling */
489 status |= IRQ_INPROGRESS; /* we are handling it */
491 desc->status = status;
494 * If there is no IRQ handler or it was disabled, exit early.
495 * Since we set PENDING, if another processor is handling
496 * a different instance of this same irq, the other processor
497 * will take care of it.
499 if (unlikely(!action))
500 goto out;
503 * Edge triggered interrupts need to remember
504 * pending events.
505 * This applies to any hw interrupts that allow a second
506 * instance of the same irq to arrive while we are in do_IRQ
507 * or in the handler. But the code here only handles the _second_
508 * instance of the irq, not the third or fourth. So it is mostly
509 * useful for irq hardware that does not mask cleanly in an
510 * SMP environment.
512 for (;;) {
513 irqreturn_t action_ret;
515 spin_unlock(&desc->lock);
517 action_ret = handle_IRQ_event(irq, action);
518 if (!noirqdebug)
519 note_interrupt(irq, desc, action_ret);
521 spin_lock(&desc->lock);
522 if (likely(!(desc->status & IRQ_PENDING)))
523 break;
524 desc->status &= ~IRQ_PENDING;
526 desc->status &= ~IRQ_INPROGRESS;
528 out:
530 * The ->end() handler has to deal with interrupts which got
531 * disabled while the handler was running.
533 desc->chip->end(irq);
534 spin_unlock(&desc->lock);
536 return 1;
538 #endif
540 void early_init_irq_lock_class(void)
542 struct irq_desc *desc;
543 int i;
545 for_each_irq_desc(i, desc) {
546 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
550 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
552 struct irq_desc *desc = irq_to_desc(irq);
553 return desc ? desc->kstat_irqs[cpu] : 0;
555 EXPORT_SYMBOL(kstat_irqs_cpu);