blob: 3aba8d12f328ec91e59f5c72217ceb0c0d2fd0a1 [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>
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080018#include <linux/rculist.h>
19#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "internals.h"
22
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080023/*
24 * lockdep: we want to handle all irq_desc locks as a single lock-class:
25 */
Yinghai Lu48a1b102008-12-11 00:15:01 -080026struct lock_class_key irq_desc_lock_class;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080027
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070028/**
29 * handle_bad_irq - handle spurious and unhandled irqs
Henrik Kretzschmar43a1dd52006-08-31 21:27:44 -070030 * @irq: the interrupt number
31 * @desc: description of the interrupt
Henrik Kretzschmar43a1dd52006-08-31 21:27:44 -070032 *
33 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070034 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +020035void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070036{
Ingo Molnar43f77752006-06-29 02:24:58 -070037 print_irq_desc(irq, desc);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +020038 kstat_incr_irqs_this_cpu(irq, desc);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070039 ack_bad_irq(irq);
40}
41
David Daney97179fd2009-01-27 09:53:22 -080042#if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
43static void __init init_irq_default_affinity(void)
44{
45 alloc_bootmem_cpumask_var(&irq_default_affinity);
46 cpumask_setall(irq_default_affinity);
47}
48#else
49static void __init init_irq_default_affinity(void)
50{
51}
52#endif
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * Linux has a controller-independent interrupt architecture.
56 * Every controller has a 'controller-template', that is used
57 * by the main code to do the right thing. Each driver-visible
Ingo Molnar06fcb0c2006-06-29 02:24:40 -070058 * interrupt source is transparently wired to the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * controller. Thus drivers need not be aware of the
60 * interrupt-controller.
61 *
62 * The code is designed to be easily extended with new/different
63 * interrupt controllers, without having to do assembly magic or
64 * having to touch the generic code.
65 *
66 * Controller mappings for all interrupt sources:
67 */
Yinghai Lu85c0f902008-08-19 20:49:47 -070068int nr_irqs = NR_IRQS;
Ingo Molnarfa42d102008-08-19 20:50:30 -070069EXPORT_SYMBOL_GPL(nr_irqs);
Yinghai Lud60458b2008-08-19 20:50:00 -070070
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080071#ifdef CONFIG_SPARSE_IRQ
72static struct irq_desc irq_desc_init = {
73 .irq = -1,
74 .status = IRQ_DISABLED,
75 .chip = &no_irq_chip,
76 .handle_irq = handle_bad_irq,
77 .depth = 1,
78 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
79#ifdef CONFIG_SMP
80 .affinity = CPU_MASK_ALL
81#endif
82};
83
Yinghai Lu48a1b102008-12-11 00:15:01 -080084void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080085{
86 unsigned long bytes;
87 char *ptr;
88 int node;
89
90 /* Compute how many bytes we need per irq and allocate them */
91 bytes = nr * sizeof(unsigned int);
92
93 node = cpu_to_node(cpu);
94 ptr = kzalloc_node(bytes, GFP_ATOMIC, node);
95 printk(KERN_DEBUG " alloc kstat_irqs on cpu %d node %d\n", cpu, node);
96
97 if (ptr)
98 desc->kstat_irqs = (unsigned int *)ptr;
99}
100
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800101static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
102{
103 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
Ingo Molnar793f7b12008-12-26 19:02:20 +0100104
105 spin_lock_init(&desc->lock);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800106 desc->irq = irq;
107#ifdef CONFIG_SMP
108 desc->cpu = cpu;
109#endif
110 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
111 init_kstat_irqs(desc, cpu, nr_cpu_ids);
112 if (!desc->kstat_irqs) {
113 printk(KERN_ERR "can not alloc kstat_irqs\n");
114 BUG_ON(1);
115 }
116 arch_init_chip_data(desc, cpu);
117}
118
119/*
120 * Protect the sparse_irqs:
121 */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800122DEFINE_SPINLOCK(sparse_irq_lock);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800123
124struct irq_desc *irq_desc_ptrs[NR_IRQS] __read_mostly;
125
Yinghai Lu99d093d2008-12-05 18:58:32 -0800126static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
127 [0 ... NR_IRQS_LEGACY-1] = {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800128 .irq = -1,
129 .status = IRQ_DISABLED,
130 .chip = &no_irq_chip,
131 .handle_irq = handle_bad_irq,
132 .depth = 1,
133 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
134#ifdef CONFIG_SMP
135 .affinity = CPU_MASK_ALL
136#endif
137 }
138};
139
140/* FIXME: use bootmem alloc ...*/
Yinghai Lu99d093d2008-12-05 18:58:32 -0800141static unsigned int kstat_irqs_legacy[NR_IRQS_LEGACY][NR_CPUS];
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800142
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800143int __init early_irq_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800144{
145 struct irq_desc *desc;
146 int legacy_count;
147 int i;
148
David Daney97179fd2009-01-27 09:53:22 -0800149 init_irq_default_affinity();
150
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800151 desc = irq_desc_legacy;
152 legacy_count = ARRAY_SIZE(irq_desc_legacy);
153
154 for (i = 0; i < legacy_count; i++) {
155 desc[i].irq = i;
156 desc[i].kstat_irqs = kstat_irqs_legacy[i];
Yinghai Lufa6beb32008-12-22 20:24:09 -0800157 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800158
159 irq_desc_ptrs[i] = desc + i;
160 }
161
162 for (i = legacy_count; i < NR_IRQS; i++)
163 irq_desc_ptrs[i] = NULL;
164
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800165 return arch_early_irq_init();
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800166}
167
168struct irq_desc *irq_to_desc(unsigned int irq)
169{
170 return (irq < NR_IRQS) ? irq_desc_ptrs[irq] : NULL;
171}
172
173struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
174{
175 struct irq_desc *desc;
176 unsigned long flags;
177 int node;
178
179 if (irq >= NR_IRQS) {
180 printk(KERN_WARNING "irq >= NR_IRQS in irq_to_desc_alloc: %d %d\n",
181 irq, NR_IRQS);
182 WARN_ON(1);
183 return NULL;
184 }
185
186 desc = irq_desc_ptrs[irq];
187 if (desc)
188 return desc;
189
190 spin_lock_irqsave(&sparse_irq_lock, flags);
191
192 /* We have to check it to avoid races with another CPU */
193 desc = irq_desc_ptrs[irq];
194 if (desc)
195 goto out_unlock;
196
197 node = cpu_to_node(cpu);
198 desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
199 printk(KERN_DEBUG " alloc irq_desc for %d on cpu %d node %d\n",
200 irq, cpu, node);
201 if (!desc) {
202 printk(KERN_ERR "can not alloc irq_desc\n");
203 BUG_ON(1);
204 }
205 init_one_irq_desc(irq, desc, cpu);
206
207 irq_desc_ptrs[irq] = desc;
208
209out_unlock:
210 spin_unlock_irqrestore(&sparse_irq_lock, flags);
211
212 return desc;
213}
214
KOSAKI Motohirof9af0e72008-12-26 12:24:24 +0900215#else /* !CONFIG_SPARSE_IRQ */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800216
Ravikiran G Thirumalaie729aa12007-05-08 00:29:13 -0700217struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 [0 ... NR_IRQS-1] = {
Zhang, Yanmin4f167fb2005-05-16 21:53:43 -0700219 .status = IRQ_DISABLED,
Ingo Molnarf1c26622006-06-29 02:24:57 -0700220 .chip = &no_irq_chip,
Ingo Molnar7a557132006-06-29 02:24:54 -0700221 .handle_irq = handle_bad_irq,
Thomas Gleixner94d39e12006-06-29 02:24:50 -0700222 .depth = 1,
Yinghai Luaac3f2b2008-09-24 19:04:35 -0700223 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
Ingo Molnara53da522006-06-29 02:24:38 -0700224#ifdef CONFIG_SMP
225 .affinity = CPU_MASK_ALL
226#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228};
Yinghai Lu08678b02008-08-19 20:50:05 -0700229
Yinghai Lu12026ea2008-12-26 22:38:15 -0800230int __init early_irq_init(void)
231{
232 struct irq_desc *desc;
233 int count;
234 int i;
235
David Daney97179fd2009-01-27 09:53:22 -0800236 init_irq_default_affinity();
237
Yinghai Lu12026ea2008-12-26 22:38:15 -0800238 desc = irq_desc;
239 count = ARRAY_SIZE(irq_desc);
240
241 for (i = 0; i < count; i++)
242 desc[i].irq = i;
243
244 return arch_early_irq_init();
245}
246
KOSAKI Motohirof9af0e72008-12-26 12:24:24 +0900247struct irq_desc *irq_to_desc(unsigned int irq)
248{
249 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
250}
251
252struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
253{
254 return irq_to_desc(irq);
255}
256#endif /* !CONFIG_SPARSE_IRQ */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700259 * What should we do if we get a hw irq event on an illegal vector?
260 * Each architecture has to answer this themself.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700262static void ack_bad(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Thomas Gleixnerd3c60042008-10-16 09:55:00 +0200264 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -0700265
Yinghai Lu08678b02008-08-19 20:50:05 -0700266 print_irq_desc(irq, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 ack_bad_irq(irq);
268}
269
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700270/*
271 * NOP functions
272 */
273static void noop(unsigned int irq)
274{
275}
276
277static unsigned int noop_ret(unsigned int irq)
278{
279 return 0;
280}
281
282/*
283 * Generic no controller implementation
284 */
Ingo Molnarf1c26622006-06-29 02:24:57 -0700285struct irq_chip no_irq_chip = {
286 .name = "none",
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700287 .startup = noop_ret,
288 .shutdown = noop,
289 .enable = noop,
290 .disable = noop,
291 .ack = ack_bad,
292 .end = noop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
295/*
Thomas Gleixnerf8b54732006-07-01 22:30:08 +0100296 * Generic dummy implementation which can be used for
297 * real dumb interrupt sources
298 */
299struct irq_chip dummy_irq_chip = {
300 .name = "dummy",
301 .startup = noop_ret,
302 .shutdown = noop,
303 .enable = noop,
304 .disable = noop,
305 .ack = noop,
306 .mask = noop,
307 .unmask = noop,
308 .end = noop,
309};
310
311/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * Special, empty irq handler:
313 */
David Howells7d12e782006-10-05 14:55:46 +0100314irqreturn_t no_action(int cpl, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 return IRQ_NONE;
317}
318
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700319/**
320 * handle_IRQ_event - irq action chain handler
321 * @irq: the interrupt number
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700322 * @action: the interrupt action chain for this irq
323 *
324 * Handles the action chain of an irq event
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
David Howells7d12e782006-10-05 14:55:46 +0100326irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Jan Beulich908dcec2006-06-23 02:06:00 -0700328 irqreturn_t ret, retval = IRQ_NONE;
329 unsigned int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700331 if (!(action->flags & IRQF_DISABLED))
Ingo Molnar366c7f52006-07-03 00:25:25 -0700332 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 do {
David Howells7d12e782006-10-05 14:55:46 +0100335 ret = action->handler(irq, action->dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (ret == IRQ_HANDLED)
337 status |= action->flags;
338 retval |= ret;
339 action = action->next;
340 } while (action);
341
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700342 if (status & IRQF_SAMPLE_RANDOM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 add_interrupt_randomness(irq);
344 local_irq_disable();
345
346 return retval;
347}
348
David Howellsaf8c65b2006-09-25 23:32:07 -0700349#ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700350/**
351 * __do_IRQ - original all in one highlevel IRQ handler
352 * @irq: the interrupt number
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700353 *
354 * __do_IRQ handles all normal device IRQ's (the special
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * SMP cross-CPU interrupts have their own specific
356 * handlers).
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700357 *
358 * This is the original x86 implementation which is used for every
359 * interrupt type.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800361unsigned int __do_IRQ(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Yinghai Lu08678b02008-08-19 20:50:05 -0700363 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700364 struct irqaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 unsigned int status;
366
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200367 kstat_incr_irqs_this_cpu(irq, desc);
368
Karsten Wiesef26fdd52005-09-06 15:17:25 -0700369 if (CHECK_IRQ_PER_CPU(desc->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 irqreturn_t action_ret;
371
372 /*
373 * No locking required for CPU-local interrupts:
374 */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800375 if (desc->chip->ack) {
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700376 desc->chip->ack(irq);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800377 /* get new one */
378 desc = irq_remap_to_desc(irq, desc);
379 }
Russ Andersonc642b832007-11-14 17:00:15 -0800380 if (likely(!(desc->status & IRQ_DISABLED))) {
381 action_ret = handle_IRQ_event(irq, desc->action);
382 if (!noirqdebug)
383 note_interrupt(irq, desc, action_ret);
384 }
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700385 desc->chip->end(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 1;
387 }
388
389 spin_lock(&desc->lock);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800390 if (desc->chip->ack) {
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700391 desc->chip->ack(irq);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800392 desc = irq_remap_to_desc(irq, desc);
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /*
395 * REPLAY is when Linux resends an IRQ that was dropped earlier
396 * WAITING is used by probe to mark irqs that are being tested
397 */
398 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
399 status |= IRQ_PENDING; /* we _want_ to handle it */
400
401 /*
402 * If the IRQ is disabled for whatever reason, we cannot
403 * use the action we have.
404 */
405 action = NULL;
406 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
407 action = desc->action;
408 status &= ~IRQ_PENDING; /* we commit to handling */
409 status |= IRQ_INPROGRESS; /* we are handling it */
410 }
411 desc->status = status;
412
413 /*
414 * If there is no IRQ handler or it was disabled, exit early.
415 * Since we set PENDING, if another processor is handling
416 * a different instance of this same irq, the other processor
417 * will take care of it.
418 */
419 if (unlikely(!action))
420 goto out;
421
422 /*
423 * Edge triggered interrupts need to remember
424 * pending events.
425 * This applies to any hw interrupts that allow a second
426 * instance of the same irq to arrive while we are in do_IRQ
427 * or in the handler. But the code here only handles the _second_
428 * instance of the irq, not the third or fourth. So it is mostly
429 * useful for irq hardware that does not mask cleanly in an
430 * SMP environment.
431 */
432 for (;;) {
433 irqreturn_t action_ret;
434
435 spin_unlock(&desc->lock);
436
David Howells7d12e782006-10-05 14:55:46 +0100437 action_ret = handle_IRQ_event(irq, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!noirqdebug)
David Howells7d12e782006-10-05 14:55:46 +0100439 note_interrupt(irq, desc, action_ret);
Linus Torvaldsb42172f2006-11-22 09:32:06 -0800440
441 spin_lock(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (likely(!(desc->status & IRQ_PENDING)))
443 break;
444 desc->status &= ~IRQ_PENDING;
445 }
446 desc->status &= ~IRQ_INPROGRESS;
447
448out:
449 /*
450 * The ->end() handler has to deal with interrupts which got
451 * disabled while the handler was running.
452 */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700453 desc->chip->end(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 spin_unlock(&desc->lock);
455
456 return 1;
457}
David Howellsaf8c65b2006-09-25 23:32:07 -0700458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Ingo Molnar243c7622006-07-03 00:25:06 -0700460void early_init_irq_lock_class(void)
461{
Thomas Gleixner10e58082008-10-16 14:19:04 +0200462 struct irq_desc *desc;
Ingo Molnar243c7622006-07-03 00:25:06 -0700463 int i;
464
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800465 for_each_irq_desc(i, desc) {
Thomas Gleixner10e58082008-10-16 14:19:04 +0200466 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800467 }
Yinghai Lu08678b02008-08-19 20:50:05 -0700468}
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800469
470#ifdef CONFIG_SPARSE_IRQ
471unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
472{
473 struct irq_desc *desc = irq_to_desc(irq);
KOSAKI Motohiro26ddd8d2008-12-26 14:24:10 +0900474 return desc ? desc->kstat_irqs[cpu] : 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800475}
476#endif
477EXPORT_SYMBOL(kstat_irqs_cpu);
478