GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / kernel / irq / handle.c
blob6b8db0fdbeb840f38f7f81cf9b1e675eb6e3766a
1 /* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */
2 /*
3 * linux/kernel/irq/handle.c
5 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
6 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
8 * This file contains the core interrupt handling code.
10 * Detailed information is available in Documentation/DocBook/genericirq
14 #include <linux/irq.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/random.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/rculist.h>
22 #include <linux/hash.h>
23 #include <linux/radix-tree.h>
24 #include <trace/events/irq.h>
26 #if defined(CONFIG_BUZZZ)
27 #include <asm/buzzz.h>
28 #endif /* CONFIG_BUZZZ */
30 #include "internals.h"
32 #include <typedefs.h>
33 #include <bcmdefs.h>
36 * lockdep: we want to handle all irq_desc locks as a single lock-class:
38 struct lock_class_key irq_desc_lock_class;
40 /**
41 * handle_bad_irq - handle spurious and unhandled irqs
42 * @irq: the interrupt number
43 * @desc: description of the interrupt
45 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
47 void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
49 #if defined(BUZZZ_KEVT_LVL) && (BUZZZ_KEVT_LVL >= 1)
50 buzzz_kevt_log1(BUZZZ_KEVT_ID_IRQ_BAD, irq);
51 #endif /* BUZZZ_KEVT_LVL */
53 print_irq_desc(irq, desc);
54 kstat_incr_irqs_this_cpu(irq, desc);
55 ack_bad_irq(irq);
58 #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
59 static void __init init_irq_default_affinity(void)
61 alloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
62 cpumask_setall(irq_default_affinity);
64 #else
65 static void __init init_irq_default_affinity(void)
68 #endif
71 * Linux has a controller-independent interrupt architecture.
72 * Every controller has a 'controller-template', that is used
73 * by the main code to do the right thing. Each driver-visible
74 * interrupt source is transparently wired to the appropriate
75 * controller. Thus drivers need not be aware of the
76 * interrupt-controller.
78 * The code is designed to be easily extended with new/different
79 * interrupt controllers, without having to do assembly magic or
80 * having to touch the generic code.
82 * Controller mappings for all interrupt sources:
84 int nr_irqs = NR_IRQS;
85 EXPORT_SYMBOL_GPL(nr_irqs);
87 #ifdef CONFIG_SPARSE_IRQ
89 static struct irq_desc irq_desc_init = {
90 .irq = -1,
91 .status = IRQ_DISABLED,
92 .chip = &no_irq_chip,
93 .handle_irq = handle_bad_irq,
94 .depth = 1,
95 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
98 void __ref init_kstat_irqs(struct irq_desc *desc, int node, int nr)
100 void *ptr;
102 ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs),
103 GFP_ATOMIC, node);
106 * don't overwite if can not get new one
107 * init_copy_kstat_irqs() could still use old one
109 if (ptr) {
110 printk(KERN_DEBUG " alloc kstat_irqs on node %d\n", node);
111 desc->kstat_irqs = ptr;
115 static void init_one_irq_desc(int irq, struct irq_desc *desc, int node)
117 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
119 raw_spin_lock_init(&desc->lock);
120 desc->irq = irq;
121 #ifdef CONFIG_SMP
122 desc->node = node;
123 #endif
124 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
125 init_kstat_irqs(desc, node, nr_cpu_ids);
126 if (!desc->kstat_irqs) {
127 printk(KERN_ERR "can not alloc kstat_irqs\n");
128 BUG_ON(1);
130 if (!alloc_desc_masks(desc, node, false)) {
131 printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
132 BUG_ON(1);
134 init_desc_masks(desc);
135 arch_init_chip_data(desc, node);
139 * Protect the sparse_irqs:
141 DEFINE_RAW_SPINLOCK(sparse_irq_lock);
143 static RADIX_TREE(irq_desc_tree, GFP_ATOMIC);
145 static void set_irq_desc(unsigned int irq, struct irq_desc *desc)
147 radix_tree_insert(&irq_desc_tree, irq, desc);
150 struct irq_desc *irq_to_desc(unsigned int irq)
152 return radix_tree_lookup(&irq_desc_tree, irq);
155 void replace_irq_desc(unsigned int irq, struct irq_desc *desc)
157 void **ptr;
159 ptr = radix_tree_lookup_slot(&irq_desc_tree, irq);
160 if (ptr)
161 radix_tree_replace_slot(ptr, desc);
164 static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
165 [0 ... NR_IRQS_LEGACY-1] = {
166 .irq = -1,
167 .status = IRQ_DISABLED,
168 .chip = &no_irq_chip,
169 .handle_irq = handle_bad_irq,
170 .depth = 1,
171 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
175 static unsigned int *kstat_irqs_legacy;
177 int __init early_irq_init(void)
179 struct irq_desc *desc;
180 int legacy_count;
181 int node;
182 int i;
184 init_irq_default_affinity();
186 /* initialize nr_irqs based on nr_cpu_ids */
187 arch_probe_nr_irqs();
188 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
190 desc = irq_desc_legacy;
191 legacy_count = ARRAY_SIZE(irq_desc_legacy);
192 node = first_online_node;
194 /* allocate based on nr_cpu_ids */
195 kstat_irqs_legacy = kzalloc_node(NR_IRQS_LEGACY * nr_cpu_ids *
196 sizeof(int), GFP_NOWAIT, node);
198 for (i = 0; i < legacy_count; i++) {
199 desc[i].irq = i;
200 #ifdef CONFIG_SMP
201 desc[i].node = node;
202 #endif
203 desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
204 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
205 alloc_desc_masks(&desc[i], node, true);
206 init_desc_masks(&desc[i]);
207 set_irq_desc(i, &desc[i]);
210 return arch_early_irq_init();
213 struct irq_desc * __ref irq_to_desc_alloc_node(unsigned int irq, int node)
215 struct irq_desc *desc;
216 unsigned long flags;
218 if (irq >= nr_irqs) {
219 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
220 irq, nr_irqs);
221 return NULL;
224 desc = irq_to_desc(irq);
225 if (desc)
226 return desc;
228 raw_spin_lock_irqsave(&sparse_irq_lock, flags);
230 /* We have to check it to avoid races with another CPU */
231 desc = irq_to_desc(irq);
232 if (desc)
233 goto out_unlock;
235 desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
237 printk(KERN_DEBUG " alloc irq_desc for %d on node %d\n", irq, node);
238 if (!desc) {
239 printk(KERN_ERR "can not alloc irq_desc\n");
240 BUG_ON(1);
242 init_one_irq_desc(irq, desc, node);
244 set_irq_desc(irq, desc);
246 out_unlock:
247 raw_spin_unlock_irqrestore(&sparse_irq_lock, flags);
249 return desc;
252 #else /* !CONFIG_SPARSE_IRQ */
254 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
255 [0 ... NR_IRQS-1] = {
256 .status = IRQ_DISABLED,
257 .chip = &no_irq_chip,
258 .handle_irq = handle_bad_irq,
259 .depth = 1,
260 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
264 static unsigned int kstat_irqs_all[NR_IRQS][NR_CPUS];
265 int __init early_irq_init(void)
267 struct irq_desc *desc;
268 int count;
269 int i;
271 init_irq_default_affinity();
273 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
275 desc = irq_desc;
276 count = ARRAY_SIZE(irq_desc);
278 for (i = 0; i < count; i++) {
279 desc[i].irq = i;
280 alloc_desc_masks(&desc[i], 0, true);
281 init_desc_masks(&desc[i]);
282 desc[i].kstat_irqs = kstat_irqs_all[i];
284 return arch_early_irq_init();
287 struct irq_desc *irq_to_desc(unsigned int irq)
289 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
292 struct irq_desc *irq_to_desc_alloc_node(unsigned int irq, int node)
294 return irq_to_desc(irq);
296 #endif /* !CONFIG_SPARSE_IRQ */
298 void clear_kstat_irqs(struct irq_desc *desc)
300 memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
304 * What should we do if we get a hw irq event on an illegal vector?
305 * Each architecture has to answer this themself.
307 static void ack_bad(unsigned int irq)
309 struct irq_desc *desc = irq_to_desc(irq);
311 #if defined(BUZZZ_KEVT_LVL) && (BUZZZ_KEVT_LVL >= 1)
312 buzzz_kevt_log1(BUZZZ_KEVT_ID_IRQ_ACK_BAD, irq);
313 #endif /* BUZZZ_KEVT_LVL */
315 print_irq_desc(irq, desc);
316 ack_bad_irq(irq);
320 * NOP functions
322 static void noop(unsigned int irq)
326 static unsigned int noop_ret(unsigned int irq)
328 return 0;
332 * Generic no controller implementation
334 struct irq_chip no_irq_chip = {
335 .name = "none",
336 .startup = noop_ret,
337 .shutdown = noop,
338 .enable = noop,
339 .disable = noop,
340 .ack = ack_bad,
341 .end = noop,
345 * Generic dummy implementation which can be used for
346 * real dumb interrupt sources
348 struct irq_chip dummy_irq_chip = {
349 .name = "dummy",
350 .startup = noop_ret,
351 .shutdown = noop,
352 .enable = noop,
353 .disable = noop,
354 .ack = noop,
355 .mask = noop,
356 .unmask = noop,
357 .end = noop,
361 * Special, empty irq handler:
363 irqreturn_t no_action(int cpl, void *dev_id)
365 return IRQ_NONE;
368 static void warn_no_thread(unsigned int irq, struct irqaction *action)
370 if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags))
371 return;
373 printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD "
374 "but no thread function available.", irq, action->name);
378 * handle_IRQ_event - irq action chain handler
379 * @irq: the interrupt number
380 * @action: the interrupt action chain for this irq
382 * Handles the action chain of an irq event
384 irqreturn_t BCMFASTPATH handle_IRQ_event(unsigned int irq, struct irqaction *action)
386 irqreturn_t ret, retval = IRQ_NONE;
387 unsigned int status = 0;
389 do {
390 trace_irq_handler_entry(irq, action);
392 #if defined(BUZZZ_KEVT_LVL) && (BUZZZ_KEVT_LVL >= 1)
393 buzzz_kevt_log2(BUZZZ_KEVT_ID_IRQ_ENTRY, irq, (int)(action->handler));
394 #endif /* BUZZZ_KEVT_LVL */
396 ret = action->handler(irq, action->dev_id);
398 #if defined(BUZZZ_KEVT_LVL) && (BUZZZ_KEVT_LVL >= 1)
399 buzzz_kevt_log2(BUZZZ_KEVT_ID_IRQ_EXIT, irq, (int)(action->handler));
400 #endif /* BUZZZ_KEVT_LVL */
402 trace_irq_handler_exit(irq, action, ret);
404 switch (ret) {
405 case IRQ_WAKE_THREAD:
407 * Set result to handled so the spurious check
408 * does not trigger.
410 ret = IRQ_HANDLED;
413 * Catch drivers which return WAKE_THREAD but
414 * did not set up a thread function
416 if (unlikely(!action->thread_fn)) {
417 warn_no_thread(irq, action);
418 break;
422 * Wake up the handler thread for this
423 * action. In case the thread crashed and was
424 * killed we just pretend that we handled the
425 * interrupt. The hardirq handler above has
426 * disabled the device interrupt, so no irq
427 * storm is lurking.
429 if (likely(!test_bit(IRQTF_DIED,
430 &action->thread_flags))) {
431 set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
432 wake_up_process(action->thread);
435 /* Fall through to add to randomness */
436 case IRQ_HANDLED:
437 status |= action->flags;
438 break;
440 default:
441 break;
444 retval |= ret;
445 action = action->next;
446 } while (action);
448 if (status & IRQF_SAMPLE_RANDOM)
449 add_interrupt_randomness(irq);
450 local_irq_disable();
452 return retval;
455 #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
457 #ifdef CONFIG_ENABLE_WARN_DEPRECATED
458 # warning __do_IRQ is deprecated. Please convert to proper flow handlers
459 #endif
462 * __do_IRQ - original all in one highlevel IRQ handler
463 * @irq: the interrupt number
465 * __do_IRQ handles all normal device IRQ's (the special
466 * SMP cross-CPU interrupts have their own specific
467 * handlers).
469 * This is the original x86 implementation which is used for every
470 * interrupt type.
472 unsigned int BCMFASTPATH __do_IRQ(unsigned int irq)
474 struct irq_desc *desc = irq_to_desc(irq);
475 struct irqaction *action;
476 unsigned int status;
478 kstat_incr_irqs_this_cpu(irq, desc);
480 if (CHECK_IRQ_PER_CPU(desc->status)) {
481 irqreturn_t action_ret;
484 * No locking required for CPU-local interrupts:
486 if (desc->chip->ack)
487 desc->chip->ack(irq);
488 if (likely(!(desc->status & IRQ_DISABLED))) {
489 action_ret = handle_IRQ_event(irq, desc->action);
490 if (!noirqdebug)
491 note_interrupt(irq, desc, action_ret);
493 desc->chip->end(irq);
494 return 1;
497 raw_spin_lock(&desc->lock);
498 if (desc->chip->ack)
499 desc->chip->ack(irq);
501 * REPLAY is when Linux resends an IRQ that was dropped earlier
502 * WAITING is used by probe to mark irqs that are being tested
504 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
505 status |= IRQ_PENDING; /* we _want_ to handle it */
508 * If the IRQ is disabled for whatever reason, we cannot
509 * use the action we have.
511 action = NULL;
512 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
513 action = desc->action;
514 status &= ~IRQ_PENDING; /* we commit to handling */
515 status |= IRQ_INPROGRESS; /* we are handling it */
517 desc->status = status;
520 * If there is no IRQ handler or it was disabled, exit early.
521 * Since we set PENDING, if another processor is handling
522 * a different instance of this same irq, the other processor
523 * will take care of it.
525 if (unlikely(!action))
526 goto out;
529 * Edge triggered interrupts need to remember
530 * pending events.
531 * This applies to any hw interrupts that allow a second
532 * instance of the same irq to arrive while we are in do_IRQ
533 * or in the handler. But the code here only handles the _second_
534 * instance of the irq, not the third or fourth. So it is mostly
535 * useful for irq hardware that does not mask cleanly in an
536 * SMP environment.
538 for (;;) {
539 irqreturn_t action_ret;
541 raw_spin_unlock(&desc->lock);
543 action_ret = handle_IRQ_event(irq, action);
544 if (!noirqdebug)
545 note_interrupt(irq, desc, action_ret);
547 raw_spin_lock(&desc->lock);
548 if (likely(!(desc->status & IRQ_PENDING)))
549 break;
550 desc->status &= ~IRQ_PENDING;
552 desc->status &= ~IRQ_INPROGRESS;
554 out:
556 * The ->end() handler has to deal with interrupts which got
557 * disabled while the handler was running.
559 desc->chip->end(irq);
560 raw_spin_unlock(&desc->lock);
562 return 1;
564 #endif
566 void early_init_irq_lock_class(void)
568 struct irq_desc *desc;
569 int i;
571 for_each_irq_desc(i, desc) {
572 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
576 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
578 struct irq_desc *desc = irq_to_desc(irq);
579 return desc ? desc->kstat_irqs[cpu] : 0;
581 EXPORT_SYMBOL(kstat_irqs_cpu);