blob: 8e55dbe50afc759b5ff859d7a9c36336c8979d8d [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);
Yinghai Lu7f95ec92008-08-19 20:50:09 -070040 kstat_irqs_this_cpu(desc)++;
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070041 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
Yinghai Lu7f95ec92008-08-19 20:50:09 -070083extern int after_bootmem;
84extern void *__alloc_bootmem_nopanic(unsigned long size,
85 unsigned long align,
86 unsigned long goal);
87
88static void init_kstat_irqs(struct irq_desc *desc, int nr_desc, int nr)
89{
90 unsigned long bytes, total_bytes;
91 char *ptr;
92 int i;
93 unsigned long phys;
94
95 /* Compute how many bytes we need per irq and allocate them */
96 bytes = nr * sizeof(unsigned int);
97 total_bytes = bytes * nr_desc;
98 if (after_bootmem)
99 ptr = kzalloc(total_bytes, GFP_ATOMIC);
100 else
101 ptr = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
102
103 if (!ptr)
104 panic(" can not allocate kstat_irqs\n");
105
106 phys = __pa(ptr);
107 printk(KERN_DEBUG "kstat_irqs ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
108
109 for (i = 0; i < nr_desc; i++) {
110 desc[i].kstat_irqs = (unsigned int *)ptr;
111 ptr += bytes;
112 }
113}
114
Yinghai Lu7f95ec92008-08-19 20:50:09 -0700115static void __init init_work(void *data)
116{
117 struct dyn_array *da = data;
118 int i;
119 struct irq_desc *desc;
120
121 desc = *da->name;
122
123 for (i = 0; i < *da->nr; i++) {
124 init_one_irq_desc(&desc[i]);
125#ifndef CONFIG_HAVE_SPARSE_IRQ
126 desc[i].irq = i;
127#endif
128 }
129
130#ifdef CONFIG_HAVE_SPARSE_IRQ
131 for (i = 1; i < *da->nr; i++)
132 desc[i-1].next = &desc[i];
133#endif
134
135 /* init kstat_irqs, nr_cpu_ids is ready already */
136 init_kstat_irqs(desc, *da->nr, nr_cpu_ids);
137}
138
Yinghai Lu08678b02008-08-19 20:50:05 -0700139#ifdef CONFIG_HAVE_SPARSE_IRQ
140static int nr_irq_desc = 32;
141
142static int __init parse_nr_irq_desc(char *arg)
143{
144 if (arg)
145 nr_irq_desc = simple_strtoul(arg, NULL, 0);
146 return 0;
147}
148
149early_param("nr_irq_desc", parse_nr_irq_desc);
150
Yinghai Lu9059d8f2008-08-19 20:50:10 -0700151struct irq_desc *sparse_irqs;
Yinghai Lu08678b02008-08-19 20:50:05 -0700152DEFINE_DYN_ARRAY(sparse_irqs, sizeof(struct irq_desc), nr_irq_desc, PAGE_SIZE, init_work);
153
Yinghai Lu9059d8f2008-08-19 20:50:10 -0700154struct irq_desc *__irq_to_desc(unsigned int irq)
155{
156 struct irq_desc *desc;
157
158 BUG_ON(irq == -1U);
159
160 desc = &sparse_irqs[0];
161 while (desc) {
162 if (desc->irq == irq)
163 return desc;
164
165 if (desc->irq == -1U)
166 return NULL;
167
168 desc = desc->next;
169 }
170 return NULL;
171}
Yinghai Lu08678b02008-08-19 20:50:05 -0700172struct irq_desc *irq_to_desc(unsigned int irq)
173{
174 struct irq_desc *desc, *desc_pri;
175 int i;
176 int count = 0;
Yinghai Lu7f95ec92008-08-19 20:50:09 -0700177 unsigned long phys;
178 unsigned long total_bytes;
Yinghai Lu08678b02008-08-19 20:50:05 -0700179
180 BUG_ON(irq == -1U);
181
182 desc_pri = desc = &sparse_irqs[0];
183 while (desc) {
184 if (desc->irq == irq)
185 return desc;
186
187 if (desc->irq == -1U) {
188 desc->irq = irq;
189 return desc;
190 }
191 desc_pri = desc;
192 desc = desc->next;
193 count++;
194 }
195
196 /*
197 * we run out of pre-allocate ones, allocate more
198 */
199 printk(KERN_DEBUG "try to get more irq_desc %d\n", nr_irq_desc);
200
Yinghai Lu7f95ec92008-08-19 20:50:09 -0700201 total_bytes = sizeof(struct irq_desc) * nr_irq_desc;
Yinghai Lu08678b02008-08-19 20:50:05 -0700202 if (after_bootmem)
Yinghai Lu7f95ec92008-08-19 20:50:09 -0700203 desc = kzalloc(total_bytes, GFP_ATOMIC);
Yinghai Lu08678b02008-08-19 20:50:05 -0700204 else
Yinghai Lu7f95ec92008-08-19 20:50:09 -0700205 desc = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
Yinghai Lu08678b02008-08-19 20:50:05 -0700206
207 if (!desc)
208 panic("please boot with nr_irq_desc= %d\n", count * 2);
209
Yinghai Lu7f95ec92008-08-19 20:50:09 -0700210 phys = __pa(desc);
211 printk(KERN_DEBUG "irq_desc ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
212
Yinghai Lu08678b02008-08-19 20:50:05 -0700213 for (i = 0; i < nr_irq_desc; i++)
214 init_one_irq_desc(&desc[i]);
215
216 for (i = 1; i < nr_irq_desc; i++)
217 desc[i-1].next = &desc[i];
218
Yinghai Lu7f95ec92008-08-19 20:50:09 -0700219 /* init kstat_irqs, nr_cpu_ids is ready already */
220 init_kstat_irqs(desc, nr_irq_desc, nr_cpu_ids);
221
Yinghai Lu08678b02008-08-19 20:50:05 -0700222 desc->irq = irq;
223 desc_pri->next = desc;
224
225 return desc;
226}
227#else
Yinghai Lu9059d8f2008-08-19 20:50:10 -0700228struct irq_desc *irq_desc;
Yinghai Lud60458b2008-08-19 20:50:00 -0700229DEFINE_DYN_ARRAY(irq_desc, sizeof(struct irq_desc), nr_irqs, PAGE_SIZE, init_work);
230
Yinghai Lu08678b02008-08-19 20:50:05 -0700231#endif
232
Yinghai Lud60458b2008-08-19 20:50:00 -0700233#else
234
Ravikiran G Thirumalaie729aa12007-05-08 00:29:13 -0700235struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 [0 ... NR_IRQS-1] = {
Zhang, Yanmin4f167fb2005-05-16 21:53:43 -0700237 .status = IRQ_DISABLED,
Ingo Molnarf1c26622006-06-29 02:24:57 -0700238 .chip = &no_irq_chip,
Ingo Molnar7a557132006-06-29 02:24:54 -0700239 .handle_irq = handle_bad_irq,
Thomas Gleixner94d39e12006-06-29 02:24:50 -0700240 .depth = 1,
Yinghai Lu08678b02008-08-19 20:50:05 -0700241 .lock = __SPIN_LOCK_UNLOCKED(sparse_irqs->lock),
Ingo Molnara53da522006-06-29 02:24:38 -0700242#ifdef CONFIG_SMP
243 .affinity = CPU_MASK_ALL
244#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246};
Yinghai Lu08678b02008-08-19 20:50:05 -0700247
248#endif
249
250#ifndef CONFIG_HAVE_SPARSE_IRQ
251struct irq_desc *irq_to_desc(unsigned int irq)
252{
253 if (irq < nr_irqs)
254 return &irq_desc[irq];
255
256 return NULL;
257}
Yinghai Lu9059d8f2008-08-19 20:50:10 -0700258struct irq_desc *__irq_to_desc(unsigned int irq)
259{
260 return irq_to_desc(irq);
261}
Yinghai Lud60458b2008-08-19 20:50:00 -0700262#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/*
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700265 * What should we do if we get a hw irq event on an illegal vector?
266 * Each architecture has to answer this themself.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700268static void ack_bad(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Yinghai Lu08678b02008-08-19 20:50:05 -0700270 struct irq_desc *desc;
271
272 desc = irq_to_desc(irq);
273 print_irq_desc(irq, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 ack_bad_irq(irq);
275}
276
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700277/*
278 * NOP functions
279 */
280static void noop(unsigned int irq)
281{
282}
283
284static unsigned int noop_ret(unsigned int irq)
285{
286 return 0;
287}
288
289/*
290 * Generic no controller implementation
291 */
Ingo Molnarf1c26622006-06-29 02:24:57 -0700292struct irq_chip no_irq_chip = {
293 .name = "none",
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700294 .startup = noop_ret,
295 .shutdown = noop,
296 .enable = noop,
297 .disable = noop,
298 .ack = ack_bad,
299 .end = noop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
302/*
Thomas Gleixnerf8b54732006-07-01 22:30:08 +0100303 * Generic dummy implementation which can be used for
304 * real dumb interrupt sources
305 */
306struct irq_chip dummy_irq_chip = {
307 .name = "dummy",
308 .startup = noop_ret,
309 .shutdown = noop,
310 .enable = noop,
311 .disable = noop,
312 .ack = noop,
313 .mask = noop,
314 .unmask = noop,
315 .end = noop,
316};
317
318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * Special, empty irq handler:
320 */
David Howells7d12e782006-10-05 14:55:46 +0100321irqreturn_t no_action(int cpl, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 return IRQ_NONE;
324}
325
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700326/**
327 * handle_IRQ_event - irq action chain handler
328 * @irq: the interrupt number
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700329 * @action: the interrupt action chain for this irq
330 *
331 * Handles the action chain of an irq event
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
David Howells7d12e782006-10-05 14:55:46 +0100333irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Jan Beulich908dcec2006-06-23 02:06:00 -0700335 irqreturn_t ret, retval = IRQ_NONE;
336 unsigned int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700338 if (!(action->flags & IRQF_DISABLED))
Ingo Molnar366c7f52006-07-03 00:25:25 -0700339 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 do {
David Howells7d12e782006-10-05 14:55:46 +0100342 ret = action->handler(irq, action->dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (ret == IRQ_HANDLED)
344 status |= action->flags;
345 retval |= ret;
346 action = action->next;
347 } while (action);
348
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700349 if (status & IRQF_SAMPLE_RANDOM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 add_interrupt_randomness(irq);
351 local_irq_disable();
352
353 return retval;
354}
355
David Howellsaf8c65b2006-09-25 23:32:07 -0700356#ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700357/**
358 * __do_IRQ - original all in one highlevel IRQ handler
359 * @irq: the interrupt number
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700360 *
361 * __do_IRQ handles all normal device IRQ's (the special
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * SMP cross-CPU interrupts have their own specific
363 * handlers).
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700364 *
365 * This is the original x86 implementation which is used for every
366 * interrupt type.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800368unsigned int __do_IRQ(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Yinghai Lu08678b02008-08-19 20:50:05 -0700370 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700371 struct irqaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 unsigned int status;
373
Yinghai Lu7f95ec92008-08-19 20:50:09 -0700374 kstat_irqs_this_cpu(desc)++;
Karsten Wiesef26fdd52005-09-06 15:17:25 -0700375 if (CHECK_IRQ_PER_CPU(desc->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 irqreturn_t action_ret;
377
378 /*
379 * No locking required for CPU-local interrupts:
380 */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700381 if (desc->chip->ack)
382 desc->chip->ack(irq);
Russ Andersonc642b832007-11-14 17:00:15 -0800383 if (likely(!(desc->status & IRQ_DISABLED))) {
384 action_ret = handle_IRQ_event(irq, desc->action);
385 if (!noirqdebug)
386 note_interrupt(irq, desc, action_ret);
387 }
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700388 desc->chip->end(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 1;
390 }
391
392 spin_lock(&desc->lock);
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700393 if (desc->chip->ack)
394 desc->chip->ack(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /*
396 * REPLAY is when Linux resends an IRQ that was dropped earlier
397 * WAITING is used by probe to mark irqs that are being tested
398 */
399 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
400 status |= IRQ_PENDING; /* we _want_ to handle it */
401
402 /*
403 * If the IRQ is disabled for whatever reason, we cannot
404 * use the action we have.
405 */
406 action = NULL;
407 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
408 action = desc->action;
409 status &= ~IRQ_PENDING; /* we commit to handling */
410 status |= IRQ_INPROGRESS; /* we are handling it */
411 }
412 desc->status = status;
413
414 /*
415 * If there is no IRQ handler or it was disabled, exit early.
416 * Since we set PENDING, if another processor is handling
417 * a different instance of this same irq, the other processor
418 * will take care of it.
419 */
420 if (unlikely(!action))
421 goto out;
422
423 /*
424 * Edge triggered interrupts need to remember
425 * pending events.
426 * This applies to any hw interrupts that allow a second
427 * instance of the same irq to arrive while we are in do_IRQ
428 * or in the handler. But the code here only handles the _second_
429 * instance of the irq, not the third or fourth. So it is mostly
430 * useful for irq hardware that does not mask cleanly in an
431 * SMP environment.
432 */
433 for (;;) {
434 irqreturn_t action_ret;
435
436 spin_unlock(&desc->lock);
437
David Howells7d12e782006-10-05 14:55:46 +0100438 action_ret = handle_IRQ_event(irq, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (!noirqdebug)
David Howells7d12e782006-10-05 14:55:46 +0100440 note_interrupt(irq, desc, action_ret);
Linus Torvaldsb42172f2006-11-22 09:32:06 -0800441
442 spin_lock(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (likely(!(desc->status & IRQ_PENDING)))
444 break;
445 desc->status &= ~IRQ_PENDING;
446 }
447 desc->status &= ~IRQ_INPROGRESS;
448
449out:
450 /*
451 * The ->end() handler has to deal with interrupts which got
452 * disabled while the handler was running.
453 */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700454 desc->chip->end(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 spin_unlock(&desc->lock);
456
457 return 1;
458}
David Howellsaf8c65b2006-09-25 23:32:07 -0700459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Yinghai Lu08678b02008-08-19 20:50:05 -0700461
Ingo Molnar243c7622006-07-03 00:25:06 -0700462#ifdef CONFIG_TRACE_IRQFLAGS
Ingo Molnar243c7622006-07-03 00:25:06 -0700463void early_init_irq_lock_class(void)
464{
Yinghai Lu08678b02008-08-19 20:50:05 -0700465#ifndef CONFIG_HAVE_DYN_ARRAY
Ingo Molnar243c7622006-07-03 00:25:06 -0700466 int i;
467
Yinghai Lu85c0f902008-08-19 20:49:47 -0700468 for (i = 0; i < nr_irqs; i++)
Ingo Molnar243c7622006-07-03 00:25:06 -0700469 lockdep_set_class(&irq_desc[i].lock, &irq_desc_lock_class);
Ingo Molnar243c7622006-07-03 00:25:06 -0700470#endif
Yinghai Lu08678b02008-08-19 20:50:05 -0700471}
472#endif
473
Yinghai Lu7f95ec92008-08-19 20:50:09 -0700474unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
475{
476 struct irq_desc *desc = irq_to_desc(irq);
477 return desc->kstat_irqs[cpu];
478}
479EXPORT_SYMBOL(kstat_irqs_cpu);
480