Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/irq/handle.c |
| 3 | * |
| 4 | * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar |
| 5 | * |
| 6 | * This file contains the core interrupt handling code. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/irq.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/random.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/kernel_stat.h> |
| 14 | |
| 15 | #include "internals.h" |
| 16 | |
| 17 | /* |
| 18 | * Linux has a controller-independent interrupt architecture. |
| 19 | * Every controller has a 'controller-template', that is used |
| 20 | * by the main code to do the right thing. Each driver-visible |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 21 | * interrupt source is transparently wired to the appropriate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * controller. Thus drivers need not be aware of the |
| 23 | * interrupt-controller. |
| 24 | * |
| 25 | * The code is designed to be easily extended with new/different |
| 26 | * interrupt controllers, without having to do assembly magic or |
| 27 | * having to touch the generic code. |
| 28 | * |
| 29 | * Controller mappings for all interrupt sources: |
| 30 | */ |
Ingo Molnar | 34ffdb7 | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 31 | struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | [0 ... NR_IRQS-1] = { |
Zhang, Yanmin | 4f167fb | 2005-05-16 21:53:43 -0700 | [diff] [blame] | 33 | .status = IRQ_DISABLED, |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 34 | .chip = &no_irq_type, |
Ingo Molnar | a53da52 | 2006-06-29 02:24:38 -0700 | [diff] [blame] | 35 | .lock = SPIN_LOCK_UNLOCKED, |
| 36 | #ifdef CONFIG_SMP |
| 37 | .affinity = CPU_MASK_ALL |
| 38 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | }; |
| 41 | |
| 42 | /* |
Ingo Molnar | 77a5afe | 2006-06-29 02:24:46 -0700 | [diff] [blame^] | 43 | * What should we do if we get a hw irq event on an illegal vector? |
| 44 | * Each architecture has to answer this themself. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | */ |
Ingo Molnar | 77a5afe | 2006-06-29 02:24:46 -0700 | [diff] [blame^] | 46 | static void ack_bad(unsigned int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | ack_bad_irq(irq); |
| 49 | } |
| 50 | |
Ingo Molnar | 77a5afe | 2006-06-29 02:24:46 -0700 | [diff] [blame^] | 51 | /* |
| 52 | * NOP functions |
| 53 | */ |
| 54 | static void noop(unsigned int irq) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | static unsigned int noop_ret(unsigned int irq) |
| 59 | { |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Generic no controller implementation |
| 65 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | struct hw_interrupt_type no_irq_type = { |
Ingo Molnar | 77a5afe | 2006-06-29 02:24:46 -0700 | [diff] [blame^] | 67 | .typename = "none", |
| 68 | .startup = noop_ret, |
| 69 | .shutdown = noop, |
| 70 | .enable = noop, |
| 71 | .disable = noop, |
| 72 | .ack = ack_bad, |
| 73 | .end = noop, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /* |
| 77 | * Special, empty irq handler: |
| 78 | */ |
| 79 | irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs) |
| 80 | { |
| 81 | return IRQ_NONE; |
| 82 | } |
| 83 | |
Ingo Molnar | 8d28bc7 | 2006-06-29 02:24:46 -0700 | [diff] [blame] | 84 | /** |
| 85 | * handle_IRQ_event - irq action chain handler |
| 86 | * @irq: the interrupt number |
| 87 | * @regs: pointer to a register structure |
| 88 | * @action: the interrupt action chain for this irq |
| 89 | * |
| 90 | * Handles the action chain of an irq event |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | */ |
Ingo Molnar | 2e60bbb | 2006-06-29 02:24:39 -0700 | [diff] [blame] | 92 | irqreturn_t handle_IRQ_event(unsigned int irq, struct pt_regs *regs, |
| 93 | struct irqaction *action) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Jan Beulich | 908dcec | 2006-06-23 02:06:00 -0700 | [diff] [blame] | 95 | irqreturn_t ret, retval = IRQ_NONE; |
| 96 | unsigned int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | if (!(action->flags & SA_INTERRUPT)) |
| 99 | local_irq_enable(); |
| 100 | |
| 101 | do { |
| 102 | ret = action->handler(irq, action->dev_id, regs); |
| 103 | if (ret == IRQ_HANDLED) |
| 104 | status |= action->flags; |
| 105 | retval |= ret; |
| 106 | action = action->next; |
| 107 | } while (action); |
| 108 | |
| 109 | if (status & SA_SAMPLE_RANDOM) |
| 110 | add_interrupt_randomness(irq); |
| 111 | local_irq_disable(); |
| 112 | |
| 113 | return retval; |
| 114 | } |
| 115 | |
Ingo Molnar | 8d28bc7 | 2006-06-29 02:24:46 -0700 | [diff] [blame] | 116 | /** |
| 117 | * __do_IRQ - original all in one highlevel IRQ handler |
| 118 | * @irq: the interrupt number |
| 119 | * @regs: pointer to a register structure |
| 120 | * |
| 121 | * __do_IRQ handles all normal device IRQ's (the special |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * SMP cross-CPU interrupts have their own specific |
| 123 | * handlers). |
Ingo Molnar | 8d28bc7 | 2006-06-29 02:24:46 -0700 | [diff] [blame] | 124 | * |
| 125 | * This is the original x86 implementation which is used for every |
| 126 | * interrupt type. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | */ |
| 128 | fastcall unsigned int __do_IRQ(unsigned int irq, struct pt_regs *regs) |
| 129 | { |
Ingo Molnar | 34ffdb7 | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 130 | struct irq_desc *desc = irq_desc + irq; |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 131 | struct irqaction *action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | unsigned int status; |
| 133 | |
| 134 | kstat_this_cpu.irqs[irq]++; |
Karsten Wiese | f26fdd5 | 2005-09-06 15:17:25 -0700 | [diff] [blame] | 135 | if (CHECK_IRQ_PER_CPU(desc->status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | irqreturn_t action_ret; |
| 137 | |
| 138 | /* |
| 139 | * No locking required for CPU-local interrupts: |
| 140 | */ |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 141 | if (desc->chip->ack) |
| 142 | desc->chip->ack(irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | action_ret = handle_IRQ_event(irq, regs, desc->action); |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 144 | desc->chip->end(irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | spin_lock(&desc->lock); |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 149 | if (desc->chip->ack) |
| 150 | desc->chip->ack(irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* |
| 152 | * REPLAY is when Linux resends an IRQ that was dropped earlier |
| 153 | * WAITING is used by probe to mark irqs that are being tested |
| 154 | */ |
| 155 | status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING); |
| 156 | status |= IRQ_PENDING; /* we _want_ to handle it */ |
| 157 | |
| 158 | /* |
| 159 | * If the IRQ is disabled for whatever reason, we cannot |
| 160 | * use the action we have. |
| 161 | */ |
| 162 | action = NULL; |
| 163 | if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) { |
| 164 | action = desc->action; |
| 165 | status &= ~IRQ_PENDING; /* we commit to handling */ |
| 166 | status |= IRQ_INPROGRESS; /* we are handling it */ |
| 167 | } |
| 168 | desc->status = status; |
| 169 | |
| 170 | /* |
| 171 | * If there is no IRQ handler or it was disabled, exit early. |
| 172 | * Since we set PENDING, if another processor is handling |
| 173 | * a different instance of this same irq, the other processor |
| 174 | * will take care of it. |
| 175 | */ |
| 176 | if (unlikely(!action)) |
| 177 | goto out; |
| 178 | |
| 179 | /* |
| 180 | * Edge triggered interrupts need to remember |
| 181 | * pending events. |
| 182 | * This applies to any hw interrupts that allow a second |
| 183 | * instance of the same irq to arrive while we are in do_IRQ |
| 184 | * or in the handler. But the code here only handles the _second_ |
| 185 | * instance of the irq, not the third or fourth. So it is mostly |
| 186 | * useful for irq hardware that does not mask cleanly in an |
| 187 | * SMP environment. |
| 188 | */ |
| 189 | for (;;) { |
| 190 | irqreturn_t action_ret; |
| 191 | |
| 192 | spin_unlock(&desc->lock); |
| 193 | |
| 194 | action_ret = handle_IRQ_event(irq, regs, action); |
| 195 | |
| 196 | spin_lock(&desc->lock); |
| 197 | if (!noirqdebug) |
Alan Cox | 200803d | 2005-06-28 20:45:18 -0700 | [diff] [blame] | 198 | note_interrupt(irq, desc, action_ret, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | if (likely(!(desc->status & IRQ_PENDING))) |
| 200 | break; |
| 201 | desc->status &= ~IRQ_PENDING; |
| 202 | } |
| 203 | desc->status &= ~IRQ_INPROGRESS; |
| 204 | |
| 205 | out: |
| 206 | /* |
| 207 | * The ->end() handler has to deal with interrupts which got |
| 208 | * disabled while the handler was running. |
| 209 | */ |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 210 | desc->chip->end(irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | spin_unlock(&desc->lock); |
| 212 | |
| 213 | return 1; |
| 214 | } |
| 215 | |