1 /* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */
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"
36 * lockdep: we want to handle all irq_desc locks as a single lock-class:
38 struct lock_class_key irq_desc_lock_class
;
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
);
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
);
65 static void __init
init_irq_default_affinity(void)
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
= {
91 .status
= IRQ_DISABLED
,
93 .handle_irq
= handle_bad_irq
,
95 .lock
= __RAW_SPIN_LOCK_UNLOCKED(irq_desc_init
.lock
),
98 void __ref
init_kstat_irqs(struct irq_desc
*desc
, int node
, int nr
)
102 ptr
= kzalloc_node(nr
* sizeof(*desc
->kstat_irqs
),
106 * don't overwite if can not get new one
107 * init_copy_kstat_irqs() could still use old one
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
);
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");
130 if (!alloc_desc_masks(desc
, node
, false)) {
131 printk(KERN_ERR
"can not alloc irq_desc cpumasks\n");
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
)
159 ptr
= radix_tree_lookup_slot(&irq_desc_tree
, irq
);
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] = {
167 .status
= IRQ_DISABLED
,
168 .chip
= &no_irq_chip
,
169 .handle_irq
= handle_bad_irq
,
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
;
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
++) {
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
;
218 if (irq
>= nr_irqs
) {
219 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
224 desc
= irq_to_desc(irq
);
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
);
235 desc
= kzalloc_node(sizeof(*desc
), GFP_ATOMIC
, node
);
237 printk(KERN_DEBUG
" alloc irq_desc for %d on node %d\n", irq
, node
);
239 printk(KERN_ERR
"can not alloc irq_desc\n");
242 init_one_irq_desc(irq
, desc
, node
);
244 set_irq_desc(irq
, desc
);
247 raw_spin_unlock_irqrestore(&sparse_irq_lock
, flags
);
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
,
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
;
271 init_irq_default_affinity();
273 printk(KERN_INFO
"NR_IRQS:%d\n", NR_IRQS
);
276 count
= ARRAY_SIZE(irq_desc
);
278 for (i
= 0; i
< count
; 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
);
322 static void noop(unsigned int irq
)
326 static unsigned int noop_ret(unsigned int irq
)
332 * Generic no controller implementation
334 struct irq_chip no_irq_chip
= {
345 * Generic dummy implementation which can be used for
346 * real dumb interrupt sources
348 struct irq_chip dummy_irq_chip
= {
361 * Special, empty irq handler:
363 irqreturn_t
no_action(int cpl
, void *dev_id
)
368 static void warn_no_thread(unsigned int irq
, struct irqaction
*action
)
370 if (test_and_set_bit(IRQTF_WARNED
, &action
->thread_flags
))
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;
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
);
405 case IRQ_WAKE_THREAD
:
407 * Set result to handled so the spurious check
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
);
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
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 */
437 status
|= action
->flags
;
445 action
= action
->next
;
448 if (status
& IRQF_SAMPLE_RANDOM
)
449 add_interrupt_randomness(irq
);
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
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
469 * This is the original x86 implementation which is used for every
472 unsigned int BCMFASTPATH
__do_IRQ(unsigned int irq
)
474 struct irq_desc
*desc
= irq_to_desc(irq
);
475 struct irqaction
*action
;
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:
487 desc
->chip
->ack(irq
);
488 if (likely(!(desc
->status
& IRQ_DISABLED
))) {
489 action_ret
= handle_IRQ_event(irq
, desc
->action
);
491 note_interrupt(irq
, desc
, action_ret
);
493 desc
->chip
->end(irq
);
497 raw_spin_lock(&desc
->lock
);
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.
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
))
529 * Edge triggered interrupts need to remember
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
539 irqreturn_t action_ret
;
541 raw_spin_unlock(&desc
->lock
);
543 action_ret
= handle_IRQ_event(irq
, action
);
545 note_interrupt(irq
, desc
, action_ret
);
547 raw_spin_lock(&desc
->lock
);
548 if (likely(!(desc
->status
& IRQ_PENDING
)))
550 desc
->status
&= ~IRQ_PENDING
;
552 desc
->status
&= ~IRQ_INPROGRESS
;
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
);
566 void early_init_irq_lock_class(void)
568 struct irq_desc
*desc
;
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
);