blob: 37c63633e78b48ea71073d12af325ba79f0ec97d [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>
Mike Travis0fa0ebb2009-01-10 22:24:06 -080020#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Steven Rostedta8d154b2009-04-10 09:36:00 -040022#define CREATE_TRACE_POINTS
Steven Rostedtad8d75f2009-04-14 19:39:12 -040023#include <trace/events/irq.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "internals.h"
26
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080027/*
28 * lockdep: we want to handle all irq_desc locks as a single lock-class:
29 */
Yinghai Lu48a1b102008-12-11 00:15:01 -080030struct lock_class_key irq_desc_lock_class;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080031
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070032/**
33 * handle_bad_irq - handle spurious and unhandled irqs
Henrik Kretzschmar43a1dd52006-08-31 21:27:44 -070034 * @irq: the interrupt number
35 * @desc: description of the interrupt
Henrik Kretzschmar43a1dd52006-08-31 21:27:44 -070036 *
37 * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070038 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +020039void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070040{
Ingo Molnar43f77752006-06-29 02:24:58 -070041 print_irq_desc(irq, desc);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +020042 kstat_incr_irqs_this_cpu(irq, desc);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070043 ack_bad_irq(irq);
44}
45
David Daney97179fd2009-01-27 09:53:22 -080046#if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
47static void __init init_irq_default_affinity(void)
48{
49 alloc_bootmem_cpumask_var(&irq_default_affinity);
50 cpumask_setall(irq_default_affinity);
51}
52#else
53static void __init init_irq_default_affinity(void)
54{
55}
56#endif
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * Linux has a controller-independent interrupt architecture.
60 * Every controller has a 'controller-template', that is used
61 * by the main code to do the right thing. Each driver-visible
Ingo Molnar06fcb0c2006-06-29 02:24:40 -070062 * interrupt source is transparently wired to the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * controller. Thus drivers need not be aware of the
64 * interrupt-controller.
65 *
66 * The code is designed to be easily extended with new/different
67 * interrupt controllers, without having to do assembly magic or
68 * having to touch the generic code.
69 *
70 * Controller mappings for all interrupt sources:
71 */
Yinghai Lu85c0f902008-08-19 20:49:47 -070072int nr_irqs = NR_IRQS;
Ingo Molnarfa42d102008-08-19 20:50:30 -070073EXPORT_SYMBOL_GPL(nr_irqs);
Yinghai Lud60458b2008-08-19 20:50:00 -070074
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080075#ifdef CONFIG_SPARSE_IRQ
Mike Travis92296c62009-01-11 09:22:58 -080076
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080077static struct irq_desc irq_desc_init = {
78 .irq = -1,
79 .status = IRQ_DISABLED,
80 .chip = &no_irq_chip,
81 .handle_irq = handle_bad_irq,
82 .depth = 1,
83 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080084};
85
Yinghai Lu48a1b102008-12-11 00:15:01 -080086void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080087{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080088 int node;
Yinghai Lu005bf0e62009-02-08 16:18:03 -080089 void *ptr;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080090
91 node = cpu_to_node(cpu);
Yinghai Lu005bf0e62009-02-08 16:18:03 -080092 ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080093
Yinghai Lu005bf0e62009-02-08 16:18:03 -080094 /*
95 * don't overwite if can not get new one
96 * init_copy_kstat_irqs() could still use old one
97 */
98 if (ptr) {
99 printk(KERN_DEBUG " alloc kstat_irqs on cpu %d node %d\n",
100 cpu, node);
101 desc->kstat_irqs = ptr;
102 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800103}
104
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800105static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
106{
107 memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
Ingo Molnar793f7b12008-12-26 19:02:20 +0100108
109 spin_lock_init(&desc->lock);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800110 desc->irq = irq;
111#ifdef CONFIG_SMP
112 desc->cpu = cpu;
113#endif
114 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
115 init_kstat_irqs(desc, cpu, nr_cpu_ids);
116 if (!desc->kstat_irqs) {
117 printk(KERN_ERR "can not alloc kstat_irqs\n");
118 BUG_ON(1);
119 }
Mike Travis802bf932009-01-10 21:58:09 -0800120 if (!init_alloc_desc_masks(desc, cpu, false)) {
Mike Travis7f7ace02009-01-10 21:58:08 -0800121 printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
122 BUG_ON(1);
123 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800124 arch_init_chip_data(desc, cpu);
125}
126
127/*
128 * Protect the sparse_irqs:
129 */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800130DEFINE_SPINLOCK(sparse_irq_lock);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800131
Mike Travis0fa0ebb2009-01-10 22:24:06 -0800132struct irq_desc **irq_desc_ptrs __read_mostly;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800133
Yinghai Lu99d093d2008-12-05 18:58:32 -0800134static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
135 [0 ... NR_IRQS_LEGACY-1] = {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800136 .irq = -1,
137 .status = IRQ_DISABLED,
138 .chip = &no_irq_chip,
139 .handle_irq = handle_bad_irq,
140 .depth = 1,
141 .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800142 }
143};
144
Mike Travis542d8652009-01-10 22:24:07 -0800145static unsigned int *kstat_irqs_legacy;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800146
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800147int __init early_irq_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800148{
149 struct irq_desc *desc;
150 int legacy_count;
151 int i;
152
David Daney97179fd2009-01-27 09:53:22 -0800153 init_irq_default_affinity();
154
Yinghai Lu4a046d12009-01-12 17:39:24 -0800155 /* initialize nr_irqs based on nr_cpu_ids */
156 arch_probe_nr_irqs();
Mike Travis95949492009-01-10 22:24:06 -0800157 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
158
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800159 desc = irq_desc_legacy;
160 legacy_count = ARRAY_SIZE(irq_desc_legacy);
161
Mike Travis0fa0ebb2009-01-10 22:24:06 -0800162 /* allocate irq_desc_ptrs array based on nr_irqs */
163 irq_desc_ptrs = alloc_bootmem(nr_irqs * sizeof(void *));
164
Mike Travis542d8652009-01-10 22:24:07 -0800165 /* allocate based on nr_cpu_ids */
166 /* FIXME: invert kstat_irgs, and it'd be a per_cpu_alloc'd thing */
167 kstat_irqs_legacy = alloc_bootmem(NR_IRQS_LEGACY * nr_cpu_ids *
168 sizeof(int));
169
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800170 for (i = 0; i < legacy_count; i++) {
171 desc[i].irq = i;
Mike Travis542d8652009-01-10 22:24:07 -0800172 desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
Yinghai Lufa6beb32008-12-22 20:24:09 -0800173 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
Mike Travis7f7ace02009-01-10 21:58:08 -0800174 init_alloc_desc_masks(&desc[i], 0, true);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800175 irq_desc_ptrs[i] = desc + i;
176 }
177
Mike Travis95949492009-01-10 22:24:06 -0800178 for (i = legacy_count; i < nr_irqs; i++)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800179 irq_desc_ptrs[i] = NULL;
180
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800181 return arch_early_irq_init();
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800182}
183
184struct irq_desc *irq_to_desc(unsigned int irq)
185{
Mike Travis0fa0ebb2009-01-10 22:24:06 -0800186 if (irq_desc_ptrs && irq < nr_irqs)
187 return irq_desc_ptrs[irq];
188
189 return NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800190}
191
192struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
193{
194 struct irq_desc *desc;
195 unsigned long flags;
196 int node;
197
Mike Travis95949492009-01-10 22:24:06 -0800198 if (irq >= nr_irqs) {
Mike Travise2f4d062009-01-10 22:24:06 -0800199 WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
200 irq, nr_irqs);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800201 return NULL;
202 }
203
204 desc = irq_desc_ptrs[irq];
205 if (desc)
206 return desc;
207
208 spin_lock_irqsave(&sparse_irq_lock, flags);
209
210 /* We have to check it to avoid races with another CPU */
211 desc = irq_desc_ptrs[irq];
212 if (desc)
213 goto out_unlock;
214
215 node = cpu_to_node(cpu);
216 desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
217 printk(KERN_DEBUG " alloc irq_desc for %d on cpu %d node %d\n",
218 irq, cpu, node);
219 if (!desc) {
220 printk(KERN_ERR "can not alloc irq_desc\n");
221 BUG_ON(1);
222 }
223 init_one_irq_desc(irq, desc, cpu);
224
225 irq_desc_ptrs[irq] = desc;
226
227out_unlock:
228 spin_unlock_irqrestore(&sparse_irq_lock, flags);
229
230 return desc;
231}
232
KOSAKI Motohirof9af0e72008-12-26 12:24:24 +0900233#else /* !CONFIG_SPARSE_IRQ */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800234
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 Luaac3f2b2008-09-24 19:04:35 -0700241 .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243};
Yinghai Lu08678b02008-08-19 20:50:05 -0700244
Yinghai Lud7e51e62009-01-07 15:03:13 -0800245static unsigned int kstat_irqs_all[NR_IRQS][NR_CPUS];
Yinghai Lu12026ea2008-12-26 22:38:15 -0800246int __init early_irq_init(void)
247{
248 struct irq_desc *desc;
249 int count;
250 int i;
251
David Daney97179fd2009-01-27 09:53:22 -0800252 init_irq_default_affinity();
253
Mike Travis95949492009-01-10 22:24:06 -0800254 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
255
Yinghai Lu12026ea2008-12-26 22:38:15 -0800256 desc = irq_desc;
257 count = ARRAY_SIZE(irq_desc);
258
Yinghai Lud7e51e62009-01-07 15:03:13 -0800259 for (i = 0; i < count; i++) {
Yinghai Lu12026ea2008-12-26 22:38:15 -0800260 desc[i].irq = i;
Mike Travis7f7ace02009-01-10 21:58:08 -0800261 init_alloc_desc_masks(&desc[i], 0, true);
Yinghai Lud7e51e62009-01-07 15:03:13 -0800262 desc[i].kstat_irqs = kstat_irqs_all[i];
263 }
Yinghai Lu12026ea2008-12-26 22:38:15 -0800264 return arch_early_irq_init();
265}
266
KOSAKI Motohirof9af0e72008-12-26 12:24:24 +0900267struct irq_desc *irq_to_desc(unsigned int irq)
268{
269 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
270}
271
272struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
273{
274 return irq_to_desc(irq);
275}
276#endif /* !CONFIG_SPARSE_IRQ */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800277
Yinghai Lu0f3c2a82009-02-08 16:18:03 -0800278void clear_kstat_irqs(struct irq_desc *desc)
279{
280 memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/*
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700284 * What should we do if we get a hw irq event on an illegal vector?
285 * Each architecture has to answer this themself.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 */
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700287static void ack_bad(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Thomas Gleixnerd3c60042008-10-16 09:55:00 +0200289 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -0700290
Yinghai Lu08678b02008-08-19 20:50:05 -0700291 print_irq_desc(irq, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 ack_bad_irq(irq);
293}
294
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700295/*
296 * NOP functions
297 */
298static void noop(unsigned int irq)
299{
300}
301
302static unsigned int noop_ret(unsigned int irq)
303{
304 return 0;
305}
306
307/*
308 * Generic no controller implementation
309 */
Ingo Molnarf1c26622006-06-29 02:24:57 -0700310struct irq_chip no_irq_chip = {
311 .name = "none",
Ingo Molnar77a5afe2006-06-29 02:24:46 -0700312 .startup = noop_ret,
313 .shutdown = noop,
314 .enable = noop,
315 .disable = noop,
316 .ack = ack_bad,
317 .end = noop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318};
319
320/*
Thomas Gleixnerf8b54732006-07-01 22:30:08 +0100321 * Generic dummy implementation which can be used for
322 * real dumb interrupt sources
323 */
324struct irq_chip dummy_irq_chip = {
325 .name = "dummy",
326 .startup = noop_ret,
327 .shutdown = noop,
328 .enable = noop,
329 .disable = noop,
330 .ack = noop,
331 .mask = noop,
332 .unmask = noop,
333 .end = noop,
334};
335
336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * Special, empty irq handler:
338 */
David Howells7d12e782006-10-05 14:55:46 +0100339irqreturn_t no_action(int cpl, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 return IRQ_NONE;
342}
343
Thomas Gleixnerf48fe812009-03-24 11:46:22 +0100344static void warn_no_thread(unsigned int irq, struct irqaction *action)
345{
346 if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags))
347 return;
348
349 printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD "
350 "but no thread function available.", irq, action->name);
351}
352
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700353/**
354 * handle_IRQ_event - irq action chain handler
355 * @irq: the interrupt number
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700356 * @action: the interrupt action chain for this irq
357 *
358 * Handles the action chain of an irq event
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
David Howells7d12e782006-10-05 14:55:46 +0100360irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Jan Beulich908dcec2006-06-23 02:06:00 -0700362 irqreturn_t ret, retval = IRQ_NONE;
363 unsigned int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Peter Zijlstra044d4082009-03-02 16:13:32 +0100365 WARN_ONCE(!in_irq(), "BUG: IRQ handler called from non-hardirq context!");
366
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700367 if (!(action->flags & IRQF_DISABLED))
Ingo Molnar366c7f52006-07-03 00:25:25 -0700368 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 do {
Jason Baronaf392412009-02-26 10:11:05 -0500371 trace_irq_handler_entry(irq, action);
David Howells7d12e782006-10-05 14:55:46 +0100372 ret = action->handler(irq, action->dev_id);
Jason Baronaf392412009-02-26 10:11:05 -0500373 trace_irq_handler_exit(irq, action, ret);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100374
375 switch (ret) {
376 case IRQ_WAKE_THREAD:
377 /*
Thomas Gleixnerf48fe812009-03-24 11:46:22 +0100378 * Set result to handled so the spurious check
379 * does not trigger.
380 */
381 ret = IRQ_HANDLED;
382
383 /*
384 * Catch drivers which return WAKE_THREAD but
385 * did not set up a thread function
386 */
387 if (unlikely(!action->thread_fn)) {
388 warn_no_thread(irq, action);
389 break;
390 }
391
392 /*
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100393 * Wake up the handler thread for this
394 * action. In case the thread crashed and was
395 * killed we just pretend that we handled the
396 * interrupt. The hardirq handler above has
397 * disabled the device interrupt, so no irq
398 * storm is lurking.
399 */
400 if (likely(!test_bit(IRQTF_DIED,
401 &action->thread_flags))) {
402 set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
403 wake_up_process(action->thread);
404 }
405
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100406 /* Fall through to add to randomness */
407 case IRQ_HANDLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 status |= action->flags;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100409 break;
410
411 default:
412 break;
413 }
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 retval |= ret;
416 action = action->next;
417 } while (action);
418
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700419 if (status & IRQF_SAMPLE_RANDOM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 add_interrupt_randomness(irq);
421 local_irq_disable();
422
423 return retval;
424}
425
David Howellsaf8c65b2006-09-25 23:32:07 -0700426#ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
Thomas Gleixner0e57aa12009-03-13 14:34:05 +0100427
428#ifdef CONFIG_ENABLE_WARN_DEPRECATED
429# warning __do_IRQ is deprecated. Please convert to proper flow handlers
430#endif
431
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700432/**
433 * __do_IRQ - original all in one highlevel IRQ handler
434 * @irq: the interrupt number
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700435 *
436 * __do_IRQ handles all normal device IRQ's (the special
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * SMP cross-CPU interrupts have their own specific
438 * handlers).
Ingo Molnar8d28bc72006-06-29 02:24:46 -0700439 *
440 * This is the original x86 implementation which is used for every
441 * interrupt type.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800443unsigned int __do_IRQ(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Yinghai Lu08678b02008-08-19 20:50:05 -0700445 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700446 struct irqaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 unsigned int status;
448
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200449 kstat_incr_irqs_this_cpu(irq, desc);
450
Karsten Wiesef26fdd52005-09-06 15:17:25 -0700451 if (CHECK_IRQ_PER_CPU(desc->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 irqreturn_t action_ret;
453
454 /*
455 * No locking required for CPU-local interrupts:
456 */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800457 if (desc->chip->ack) {
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700458 desc->chip->ack(irq);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800459 /* get new one */
460 desc = irq_remap_to_desc(irq, desc);
461 }
Russ Andersonc642b832007-11-14 17:00:15 -0800462 if (likely(!(desc->status & IRQ_DISABLED))) {
463 action_ret = handle_IRQ_event(irq, desc->action);
464 if (!noirqdebug)
465 note_interrupt(irq, desc, action_ret);
466 }
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700467 desc->chip->end(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 1;
469 }
470
471 spin_lock(&desc->lock);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800472 if (desc->chip->ack) {
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700473 desc->chip->ack(irq);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800474 desc = irq_remap_to_desc(irq, desc);
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /*
477 * REPLAY is when Linux resends an IRQ that was dropped earlier
478 * WAITING is used by probe to mark irqs that are being tested
479 */
480 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
481 status |= IRQ_PENDING; /* we _want_ to handle it */
482
483 /*
484 * If the IRQ is disabled for whatever reason, we cannot
485 * use the action we have.
486 */
487 action = NULL;
488 if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
489 action = desc->action;
490 status &= ~IRQ_PENDING; /* we commit to handling */
491 status |= IRQ_INPROGRESS; /* we are handling it */
492 }
493 desc->status = status;
494
495 /*
496 * If there is no IRQ handler or it was disabled, exit early.
497 * Since we set PENDING, if another processor is handling
498 * a different instance of this same irq, the other processor
499 * will take care of it.
500 */
501 if (unlikely(!action))
502 goto out;
503
504 /*
505 * Edge triggered interrupts need to remember
506 * pending events.
507 * This applies to any hw interrupts that allow a second
508 * instance of the same irq to arrive while we are in do_IRQ
509 * or in the handler. But the code here only handles the _second_
510 * instance of the irq, not the third or fourth. So it is mostly
511 * useful for irq hardware that does not mask cleanly in an
512 * SMP environment.
513 */
514 for (;;) {
515 irqreturn_t action_ret;
516
517 spin_unlock(&desc->lock);
518
David Howells7d12e782006-10-05 14:55:46 +0100519 action_ret = handle_IRQ_event(irq, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (!noirqdebug)
David Howells7d12e782006-10-05 14:55:46 +0100521 note_interrupt(irq, desc, action_ret);
Linus Torvaldsb42172f2006-11-22 09:32:06 -0800522
523 spin_lock(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (likely(!(desc->status & IRQ_PENDING)))
525 break;
526 desc->status &= ~IRQ_PENDING;
527 }
528 desc->status &= ~IRQ_INPROGRESS;
529
530out:
531 /*
532 * The ->end() handler has to deal with interrupts which got
533 * disabled while the handler was running.
534 */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700535 desc->chip->end(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 spin_unlock(&desc->lock);
537
538 return 1;
539}
David Howellsaf8c65b2006-09-25 23:32:07 -0700540#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Ingo Molnar243c7622006-07-03 00:25:06 -0700542void early_init_irq_lock_class(void)
543{
Thomas Gleixner10e58082008-10-16 14:19:04 +0200544 struct irq_desc *desc;
Ingo Molnar243c7622006-07-03 00:25:06 -0700545 int i;
546
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800547 for_each_irq_desc(i, desc) {
Thomas Gleixner10e58082008-10-16 14:19:04 +0200548 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800549 }
Yinghai Lu08678b02008-08-19 20:50:05 -0700550}
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800551
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800552unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
553{
554 struct irq_desc *desc = irq_to_desc(irq);
KOSAKI Motohiro26ddd8d2008-12-26 14:24:10 +0900555 return desc ? desc->kstat_irqs[cpu] : 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800556}
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800557EXPORT_SYMBOL(kstat_irqs_cpu);
558