blob: 9fc33b3378e657ef821efd0afdc1d2fd24858972 [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
Yinghai Lu08678b02008-08-19 20:50:05 -070021#ifdef CONFIG_TRACE_IRQFLAGS
22
23/*
24 * lockdep: we want to handle all irq_desc locks as a single lock-class:
25 */
26static struct lock_class_key irq_desc_lock_class;
27#endif
28
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070029/**
30 * handle_bad_irq - handle spurious and unhandled irqs
Henrik Kretzschmar43a1dd52006-08-31 21:27:44 -070031 * @irq: the interrupt number
32 * @desc: description of the interrupt
Henrik Kretzschmar43a1dd52006-08-31 21:27:44 -070033 *
34 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070035 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -080036void
David Howells7d12e782006-10-05 14:55:46 +010037handle_bad_irq(unsigned int irq, struct irq_desc *desc)
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070038{
Ingo Molnar43f77752006-06-29 02:24:58 -070039 print_irq_desc(irq, desc);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070040 kstat_this_cpu.irqs[irq]++;
41 ack_bad_irq(irq);
42}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * Linux has a controller-independent interrupt architecture.
46 * Every controller has a 'controller-template', that is used
47 * by the main code to do the right thing. Each driver-visible
Ingo Molnar06fcb0c2006-06-29 02:24:40 -070048 * interrupt source is transparently wired to the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * controller. Thus drivers need not be aware of the
50 * interrupt-controller.
51 *
52 * The code is designed to be easily extended with new/different
53 * interrupt controllers, without having to do assembly magic or
54 * having to touch the generic code.
55 *
56 * Controller mappings for all interrupt sources:
57 */
Yinghai Lu85c0f902008-08-19 20:49:47 -070058int nr_irqs = NR_IRQS;
Ingo Molnarfa42d102008-08-19 20:50:30 -070059EXPORT_SYMBOL_GPL(nr_irqs);
Yinghai Lud60458b2008-08-19 20:50:00 -070060
61#ifdef CONFIG_HAVE_DYN_ARRAY
Yinghai Lu08678b02008-08-19 20:50:05 -070062static struct irq_desc irq_desc_init = {
63 .irq = -1U,
Yinghai Lud60458b2008-08-19 20:50:00 -070064 .status = IRQ_DISABLED,
65 .chip = &no_irq_chip,
66 .handle_irq = handle_bad_irq,
67 .depth = 1,
68 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
69#ifdef CONFIG_SMP
70 .affinity = CPU_MASK_ALL
71#endif
72};
73
Yinghai Lu08678b02008-08-19 20:50:05 -070074
75static void init_one_irq_desc(struct irq_desc *desc)
76{
77 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
78#ifdef CONFIG_TRACE_IRQFLAGS
79 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
80#endif
81}
82
83#ifdef CONFIG_HAVE_SPARSE_IRQ
84static int nr_irq_desc = 32;
85
86static int __init parse_nr_irq_desc(char *arg)
87{
88 if (arg)
89 nr_irq_desc = simple_strtoul(arg, NULL, 0);
90 return 0;
91}
92
93early_param("nr_irq_desc", parse_nr_irq_desc);
94
Yinghai Lud60458b2008-08-19 20:50:00 -070095static void __init init_work(void *data)
96{
97 struct dyn_array *da = data;
98 int i;
99 struct irq_desc *desc;
100
101 desc = *da->name;
102
103 for (i = 0; i < *da->nr; i++)
Yinghai Lu08678b02008-08-19 20:50:05 -0700104 init_one_irq_desc(&desc[i]);
105
106 for (i = 1; i < *da->nr; i++)
107 desc[i-1].next = &desc[i];
Yinghai Lud60458b2008-08-19 20:50:00 -0700108}
109
Yinghai Lu08678b02008-08-19 20:50:05 -0700110static struct irq_desc *sparse_irqs;
111DEFINE_DYN_ARRAY(sparse_irqs, sizeof(struct irq_desc), nr_irq_desc, PAGE_SIZE, init_work);
112
113extern int after_bootmem;
114extern void *__alloc_bootmem_nopanic(unsigned long size,
115 unsigned long align,
116 unsigned long goal);
117struct irq_desc *irq_to_desc(unsigned int irq)
118{
119 struct irq_desc *desc, *desc_pri;
120 int i;
121 int count = 0;
122
123 BUG_ON(irq == -1U);
124
125 desc_pri = desc = &sparse_irqs[0];
126 while (desc) {
127 if (desc->irq == irq)
128 return desc;
129
130 if (desc->irq == -1U) {
131 desc->irq = irq;
132 return desc;
133 }
134 desc_pri = desc;
135 desc = desc->next;
136 count++;
137 }
138
139 /*
140 * we run out of pre-allocate ones, allocate more
141 */
142 printk(KERN_DEBUG "try to get more irq_desc %d\n", nr_irq_desc);
143
144 if (after_bootmem)
145 desc = kzalloc(sizeof(struct irq_desc)*nr_irq_desc, GFP_ATOMIC);
146 else
147 desc = __alloc_bootmem_nopanic(sizeof(struct irq_desc)*nr_irq_desc, PAGE_SIZE, 0);
148
149 if (!desc)
150 panic("please boot with nr_irq_desc= %d\n", count * 2);
151
152 for (i = 0; i < nr_irq_desc; i++)
153 init_one_irq_desc(&desc[i]);
154
155 for (i = 1; i < nr_irq_desc; i++)
156 desc[i-1].next = &desc[i];
157
158 desc->irq = irq;
159 desc_pri->next = desc;
160
161 return desc;
162}
163#else
164static void __init init_work(void *data)
165{
166 struct dyn_array *da = data;
167 int i;
168 struct irq_desc *desc;
169
170 desc = *da->name;
171
172 for (i = 0; i < *da->nr; i++)
173 init_one_irq_desc(&desc[i]);
174
175}
176static struct irq_desc *irq_desc;
Yinghai Lud60458b2008-08-19 20:50:00 -0700177DEFINE_DYN_ARRAY(irq_desc, sizeof(struct irq_desc), nr_irqs, PAGE_SIZE, init_work);
178
Yinghai Lu08678b02008-08-19 20:50:05 -0700179#endif
180
Yinghai Lud60458b2008-08-19 20:50:00 -0700181#else
182
Ravikiran G Thirumalaie729aa12007-05-08 00:29:13 -0700183struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 [0 ... NR_IRQS-1] = {
Zhang, Yanmin4f167fb2005-05-16 21:53:43 -0700185 .status = IRQ_DISABLED,
Ingo Molnarf1c26622006-06-29 02:24:57 -0700186 .chip = &no_irq_chip,
Ingo Molnar7a557132006-06-29 02:24:54 -0700187 .handle_irq = handle_bad_irq,
Thomas Gleixner94d39e12006-06-29 02:24:50 -0700188 .depth = 1,
Yinghai Lu08678b02008-08-19 20:50:05 -0700189 .lock = __SPIN_LOCK_UNLOCKED(sparse_irqs->lock),
Ingo Molnara53da522006-06-29 02:24:38 -0700190#ifdef CONFIG_SMP
191 .affinity = CPU_MASK_ALL
192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194};
Yinghai Lu08678b02008-08-19 20:50:05 -0700195
196#endif
197
198#ifndef CONFIG_HAVE_SPARSE_IRQ
199struct irq_desc *irq_to_desc(unsigned int irq)
200{
201 if (irq < nr_irqs)
202 return &irq_desc[irq];
203
204 return NULL;
205}
Yinghai Lud60458b2008-08-19 20:50:00 -0700206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208/*
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700209 * What should we do if we get a hw irq event on an illegal vector?
210 * Each architecture has to answer this themself.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700212static void ack_bad(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Yinghai Lu08678b02008-08-19 20:50:05 -0700214 struct irq_desc *desc;
215
216 desc = irq_to_desc(irq);
217 print_irq_desc(irq, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 ack_bad_irq(irq);
219}
220
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700221/*
222 * NOP functions
223 */
224static void noop(unsigned int irq)
225{
226}
227
228static unsigned int noop_ret(unsigned int irq)
229{
230 return 0;
231}
232
233/*
234 * Generic no controller implementation
235 */
Ingo Molnarf1c26622006-06-29 02:24:57 -0700236struct irq_chip no_irq_chip = {
237 .name = "none",
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700238 .startup = noop_ret,
239 .shutdown = noop,
240 .enable = noop,
241 .disable = noop,
242 .ack = ack_bad,
243 .end = noop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
246/*
Thomas Gleixnerf8b54732006-07-01 22:30:08 +0100247 * Generic dummy implementation which can be used for
248 * real dumb interrupt sources
249 */
250struct irq_chip dummy_irq_chip = {
251 .name = "dummy",
252 .startup = noop_ret,
253 .shutdown = noop,
254 .enable = noop,
255 .disable = noop,
256 .ack = noop,
257 .mask = noop,
258 .unmask = noop,
259 .end = noop,
260};
261
262/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * Special, empty irq handler:
264 */
David Howells7d12e782006-10-05 14:55:46 +0100265irqreturn_t no_action(int cpl, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 return IRQ_NONE;
268}
269
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700270/**
271 * handle_IRQ_event - irq action chain handler
272 * @irq: the interrupt number
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700273 * @action: the interrupt action chain for this irq
274 *
275 * Handles the action chain of an irq event
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
David Howells7d12e782006-10-05 14:55:46 +0100277irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Jan Beulich908dcec2006-06-23 02:06:00 -0700279 irqreturn_t ret, retval = IRQ_NONE;
280 unsigned int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700282 if (!(action->flags & IRQF_DISABLED))
Ingo Molnar366c7f52006-07-03 00:25:25 -0700283 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 do {
David Howells7d12e782006-10-05 14:55:46 +0100286 ret = action->handler(irq, action->dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (ret == IRQ_HANDLED)
288 status |= action->flags;
289 retval |= ret;
290 action = action->next;
291 } while (action);
292
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700293 if (status & IRQF_SAMPLE_RANDOM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 add_interrupt_randomness(irq);
295 local_irq_disable();
296
297 return retval;
298}
299
David Howellsaf8c65b2006-09-25 23:32:07 -0700300#ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700301/**
302 * __do_IRQ - original all in one highlevel IRQ handler
303 * @irq: the interrupt number
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700304 *
305 * __do_IRQ handles all normal device IRQ's (the special
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * SMP cross-CPU interrupts have their own specific
307 * handlers).
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700308 *
309 * This is the original x86 implementation which is used for every
310 * interrupt type.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800312unsigned int __do_IRQ(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Yinghai Lu08678b02008-08-19 20:50:05 -0700314 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700315 struct irqaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 unsigned int status;
317
318 kstat_this_cpu.irqs[irq]++;
Karsten Wiesef26fdd52005-09-06 15:17:25 -0700319 if (CHECK_IRQ_PER_CPU(desc->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 irqreturn_t action_ret;
321
322 /*
323 * No locking required for CPU-local interrupts:
324 */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700325 if (desc->chip->ack)
326 desc->chip->ack(irq);
Russ Andersonc642b832007-11-14 17:00:15 -0800327 if (likely(!(desc->status & IRQ_DISABLED))) {
328 action_ret = handle_IRQ_event(irq, desc->action);
329 if (!noirqdebug)
330 note_interrupt(irq, desc, action_ret);
331 }
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700332 desc->chip->end(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 1;
334 }
335
336 spin_lock(&desc->lock);
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700337 if (desc->chip->ack)
338 desc->chip->ack(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /*
340 * REPLAY is when Linux resends an IRQ that was dropped earlier
341 * WAITING is used by probe to mark irqs that are being tested
342 */
343 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
344 status |= IRQ_PENDING; /* we _want_ to handle it */
345
346 /*
347 * If the IRQ is disabled for whatever reason, we cannot
348 * use the action we have.
349 */
350 action = NULL;
351 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
352 action = desc->action;
353 status &= ~IRQ_PENDING; /* we commit to handling */
354 status |= IRQ_INPROGRESS; /* we are handling it */
355 }
356 desc->status = status;
357
358 /*
359 * If there is no IRQ handler or it was disabled, exit early.
360 * Since we set PENDING, if another processor is handling
361 * a different instance of this same irq, the other processor
362 * will take care of it.
363 */
364 if (unlikely(!action))
365 goto out;
366
367 /*
368 * Edge triggered interrupts need to remember
369 * pending events.
370 * This applies to any hw interrupts that allow a second
371 * instance of the same irq to arrive while we are in do_IRQ
372 * or in the handler. But the code here only handles the _second_
373 * instance of the irq, not the third or fourth. So it is mostly
374 * useful for irq hardware that does not mask cleanly in an
375 * SMP environment.
376 */
377 for (;;) {
378 irqreturn_t action_ret;
379
380 spin_unlock(&desc->lock);
381
David Howells7d12e782006-10-05 14:55:46 +0100382 action_ret = handle_IRQ_event(irq, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (!noirqdebug)
David Howells7d12e782006-10-05 14:55:46 +0100384 note_interrupt(irq, desc, action_ret);
Linus Torvaldsb42172f2006-11-22 09:32:06 -0800385
386 spin_lock(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (likely(!(desc->status & IRQ_PENDING)))
388 break;
389 desc->status &= ~IRQ_PENDING;
390 }
391 desc->status &= ~IRQ_INPROGRESS;
392
393out:
394 /*
395 * The ->end() handler has to deal with interrupts which got
396 * disabled while the handler was running.
397 */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700398 desc->chip->end(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 spin_unlock(&desc->lock);
400
401 return 1;
402}
David Howellsaf8c65b2006-09-25 23:32:07 -0700403#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Yinghai Lu08678b02008-08-19 20:50:05 -0700405
Ingo Molnar243c7622006-07-03 00:25:06 -0700406#ifdef CONFIG_TRACE_IRQFLAGS
Ingo Molnar243c7622006-07-03 00:25:06 -0700407void early_init_irq_lock_class(void)
408{
Yinghai Lu08678b02008-08-19 20:50:05 -0700409#ifndef CONFIG_HAVE_DYN_ARRAY
Ingo Molnar243c7622006-07-03 00:25:06 -0700410 int i;
411
Yinghai Lu85c0f902008-08-19 20:49:47 -0700412 for (i = 0; i < nr_irqs; i++)
Ingo Molnar243c7622006-07-03 00:25:06 -0700413 lockdep_set_class(&irq_desc[i].lock, &irq_desc_lock_class);
Ingo Molnar243c7622006-07-03 00:25:06 -0700414#endif
Yinghai Lu08678b02008-08-19 20:50:05 -0700415}
416#endif
417