blob: 6ce3bcc2b8f73895fbf7b6dfeecca22e37cd90c8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/irq/handle.c
3 *
Ingo Molnara34db9b2006-06-29 02:24:50 -07004 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This file contains the core interrupt handling code.
Ingo Molnara34db9b2006-06-29 02:24:50 -07008 *
9 * Detailed information is available in Documentation/DocBook/genericirq
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
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
19#include "internals.h"
20
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070021/**
22 * handle_bad_irq - handle spurious and unhandled irqs
Henrik Kretzschmar43a1dd52006-08-31 21:27:44 -070023 * @irq: the interrupt number
24 * @desc: description of the interrupt
Henrik Kretzschmar43a1dd52006-08-31 21:27:44 -070025 *
26 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070027 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -080028void
David Howells7d12e782006-10-05 14:55:46 +010029handle_bad_irq(unsigned int irq, struct irq_desc *desc)
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070030{
Ingo Molnar43f77752006-06-29 02:24:58 -070031 print_irq_desc(irq, desc);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070032 kstat_this_cpu.irqs[irq]++;
33 ack_bad_irq(irq);
34}
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * Linux has a controller-independent interrupt architecture.
38 * Every controller has a 'controller-template', that is used
39 * by the main code to do the right thing. Each driver-visible
Ingo Molnar06fcb0c2006-06-29 02:24:40 -070040 * interrupt source is transparently wired to the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * controller. Thus drivers need not be aware of the
42 * interrupt-controller.
43 *
44 * The code is designed to be easily extended with new/different
45 * interrupt controllers, without having to do assembly magic or
46 * having to touch the generic code.
47 *
48 * Controller mappings for all interrupt sources:
49 */
Yinghai Lu85c0f902008-08-19 20:49:47 -070050int nr_irqs = NR_IRQS;
Ingo Molnarfa42d102008-08-19 20:50:30 -070051EXPORT_SYMBOL_GPL(nr_irqs);
Yinghai Lud60458b2008-08-19 20:50:00 -070052
53#ifdef CONFIG_HAVE_DYN_ARRAY
54static struct irq_desc irq_desc_init __initdata = {
55 .status = IRQ_DISABLED,
56 .chip = &no_irq_chip,
57 .handle_irq = handle_bad_irq,
58 .depth = 1,
59 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
60#ifdef CONFIG_SMP
61 .affinity = CPU_MASK_ALL
62#endif
63};
64
65static void __init init_work(void *data)
66{
67 struct dyn_array *da = data;
68 int i;
69 struct irq_desc *desc;
70
71 desc = *da->name;
72
73 for (i = 0; i < *da->nr; i++)
74 memcpy(&desc[i], &irq_desc_init, sizeof(struct irq_desc));
75}
76
77struct irq_desc *irq_desc;
78DEFINE_DYN_ARRAY(irq_desc, sizeof(struct irq_desc), nr_irqs, PAGE_SIZE, init_work);
79
80#else
81
Ravikiran G Thirumalaie729aa12007-05-08 00:29:13 -070082struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 [0 ... NR_IRQS-1] = {
Zhang, Yanmin4f167fb2005-05-16 21:53:43 -070084 .status = IRQ_DISABLED,
Ingo Molnarf1c26622006-06-29 02:24:57 -070085 .chip = &no_irq_chip,
Ingo Molnar7a557132006-06-29 02:24:54 -070086 .handle_irq = handle_bad_irq,
Thomas Gleixner94d39e12006-06-29 02:24:50 -070087 .depth = 1,
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080088 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
Ingo Molnara53da522006-06-29 02:24:38 -070089#ifdef CONFIG_SMP
90 .affinity = CPU_MASK_ALL
91#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93};
Yinghai Lud60458b2008-08-19 20:50:00 -070094#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
Ingo Molnar77a5afe2006-06-29 02:24:46 -070097 * What should we do if we get a hw irq event on an illegal vector?
98 * Each architecture has to answer this themself.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700100static void ack_bad(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Ingo Molnar43f77752006-06-29 02:24:58 -0700102 print_irq_desc(irq, irq_desc + irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 ack_bad_irq(irq);
104}
105
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700106/*
107 * NOP functions
108 */
109static void noop(unsigned int irq)
110{
111}
112
113static unsigned int noop_ret(unsigned int irq)
114{
115 return 0;
116}
117
118/*
119 * Generic no controller implementation
120 */
Ingo Molnarf1c26622006-06-29 02:24:57 -0700121struct irq_chip no_irq_chip = {
122 .name = "none",
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700123 .startup = noop_ret,
124 .shutdown = noop,
125 .enable = noop,
126 .disable = noop,
127 .ack = ack_bad,
128 .end = noop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131/*
Thomas Gleixnerf8b54732006-07-01 22:30:08 +0100132 * Generic dummy implementation which can be used for
133 * real dumb interrupt sources
134 */
135struct irq_chip dummy_irq_chip = {
136 .name = "dummy",
137 .startup = noop_ret,
138 .shutdown = noop,
139 .enable = noop,
140 .disable = noop,
141 .ack = noop,
142 .mask = noop,
143 .unmask = noop,
144 .end = noop,
145};
146
147/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * Special, empty irq handler:
149 */
David Howells7d12e782006-10-05 14:55:46 +0100150irqreturn_t no_action(int cpl, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 return IRQ_NONE;
153}
154
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700155/**
156 * handle_IRQ_event - irq action chain handler
157 * @irq: the interrupt number
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700158 * @action: the interrupt action chain for this irq
159 *
160 * Handles the action chain of an irq event
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
David Howells7d12e782006-10-05 14:55:46 +0100162irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Jan Beulich908dcec2006-06-23 02:06:00 -0700164 irqreturn_t ret, retval = IRQ_NONE;
165 unsigned int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700167 if (!(action->flags & IRQF_DISABLED))
Ingo Molnar366c7f52006-07-03 00:25:25 -0700168 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 do {
David Howells7d12e782006-10-05 14:55:46 +0100171 ret = action->handler(irq, action->dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (ret == IRQ_HANDLED)
173 status |= action->flags;
174 retval |= ret;
175 action = action->next;
176 } while (action);
177
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700178 if (status & IRQF_SAMPLE_RANDOM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 add_interrupt_randomness(irq);
180 local_irq_disable();
181
182 return retval;
183}
184
David Howellsaf8c65b2006-09-25 23:32:07 -0700185#ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700186/**
187 * __do_IRQ - original all in one highlevel IRQ handler
188 * @irq: the interrupt number
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700189 *
190 * __do_IRQ handles all normal device IRQ's (the special
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * SMP cross-CPU interrupts have their own specific
192 * handlers).
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700193 *
194 * This is the original x86 implementation which is used for every
195 * interrupt type.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800197unsigned int __do_IRQ(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Ingo Molnar34ffdb72006-06-29 02:24:40 -0700199 struct irq_desc *desc = irq_desc + irq;
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700200 struct irqaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 unsigned int status;
202
203 kstat_this_cpu.irqs[irq]++;
Karsten Wiesef26fdd52005-09-06 15:17:25 -0700204 if (CHECK_IRQ_PER_CPU(desc->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 irqreturn_t action_ret;
206
207 /*
208 * No locking required for CPU-local interrupts:
209 */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700210 if (desc->chip->ack)
211 desc->chip->ack(irq);
Russ Andersonc642b832007-11-14 17:00:15 -0800212 if (likely(!(desc->status & IRQ_DISABLED))) {
213 action_ret = handle_IRQ_event(irq, desc->action);
214 if (!noirqdebug)
215 note_interrupt(irq, desc, action_ret);
216 }
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700217 desc->chip->end(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 1;
219 }
220
221 spin_lock(&desc->lock);
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700222 if (desc->chip->ack)
223 desc->chip->ack(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /*
225 * REPLAY is when Linux resends an IRQ that was dropped earlier
226 * WAITING is used by probe to mark irqs that are being tested
227 */
228 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
229 status |= IRQ_PENDING; /* we _want_ to handle it */
230
231 /*
232 * If the IRQ is disabled for whatever reason, we cannot
233 * use the action we have.
234 */
235 action = NULL;
236 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
237 action = desc->action;
238 status &= ~IRQ_PENDING; /* we commit to handling */
239 status |= IRQ_INPROGRESS; /* we are handling it */
240 }
241 desc->status = status;
242
243 /*
244 * If there is no IRQ handler or it was disabled, exit early.
245 * Since we set PENDING, if another processor is handling
246 * a different instance of this same irq, the other processor
247 * will take care of it.
248 */
249 if (unlikely(!action))
250 goto out;
251
252 /*
253 * Edge triggered interrupts need to remember
254 * pending events.
255 * This applies to any hw interrupts that allow a second
256 * instance of the same irq to arrive while we are in do_IRQ
257 * or in the handler. But the code here only handles the _second_
258 * instance of the irq, not the third or fourth. So it is mostly
259 * useful for irq hardware that does not mask cleanly in an
260 * SMP environment.
261 */
262 for (;;) {
263 irqreturn_t action_ret;
264
265 spin_unlock(&desc->lock);
266
David Howells7d12e782006-10-05 14:55:46 +0100267 action_ret = handle_IRQ_event(irq, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!noirqdebug)
David Howells7d12e782006-10-05 14:55:46 +0100269 note_interrupt(irq, desc, action_ret);
Linus Torvaldsb42172f2006-11-22 09:32:06 -0800270
271 spin_lock(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (likely(!(desc->status & IRQ_PENDING)))
273 break;
274 desc->status &= ~IRQ_PENDING;
275 }
276 desc->status &= ~IRQ_INPROGRESS;
277
278out:
279 /*
280 * The ->end() handler has to deal with interrupts which got
281 * disabled while the handler was running.
282 */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700283 desc->chip->end(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 spin_unlock(&desc->lock);
285
286 return 1;
287}
David Howellsaf8c65b2006-09-25 23:32:07 -0700288#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Ingo Molnar243c7622006-07-03 00:25:06 -0700290#ifdef CONFIG_TRACE_IRQFLAGS
291
292/*
293 * lockdep: we want to handle all irq_desc locks as a single lock-class:
294 */
295static struct lock_class_key irq_desc_lock_class;
296
297void early_init_irq_lock_class(void)
298{
299 int i;
300
Yinghai Lu85c0f902008-08-19 20:49:47 -0700301 for (i = 0; i < nr_irqs; i++)
Ingo Molnar243c7622006-07-03 00:25:06 -0700302 lockdep_set_class(&irq_desc[i].lock, &irq_desc_lock_class);
303}
304
305#endif