]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/irq/manage.c | |
3 | * | |
4 | * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar | |
5 | * | |
6 | * This file contains driver APIs to the irq subsystem. | |
7 | */ | |
8 | ||
b77d6adc | 9 | #include <linux/config.h> |
1da177e4 LT |
10 | #include <linux/irq.h> |
11 | #include <linux/module.h> | |
12 | #include <linux/random.h> | |
13 | #include <linux/interrupt.h> | |
14 | ||
15 | #include "internals.h" | |
16 | ||
17 | #ifdef CONFIG_SMP | |
18 | ||
54d5d424 AR |
19 | #if defined (CONFIG_GENERIC_PENDING_IRQ) || defined (CONFIG_IRQBALANCE) |
20 | cpumask_t __cacheline_aligned pending_irq_cpumask[NR_IRQS]; | |
21 | #endif | |
22 | ||
1da177e4 LT |
23 | /** |
24 | * synchronize_irq - wait for pending IRQ handlers (on other CPUs) | |
1e5d5331 | 25 | * @irq: interrupt number to wait for |
1da177e4 LT |
26 | * |
27 | * This function waits for any pending IRQ handlers for this interrupt | |
28 | * to complete before returning. If you use this function while | |
29 | * holding a resource the IRQ handler may need you will deadlock. | |
30 | * | |
31 | * This function may be called - with care - from IRQ context. | |
32 | */ | |
33 | void synchronize_irq(unsigned int irq) | |
34 | { | |
35 | struct irq_desc *desc = irq_desc + irq; | |
36 | ||
c2b5a251 MW |
37 | if (irq >= NR_IRQS) |
38 | return; | |
39 | ||
1da177e4 LT |
40 | while (desc->status & IRQ_INPROGRESS) |
41 | cpu_relax(); | |
42 | } | |
1da177e4 LT |
43 | EXPORT_SYMBOL(synchronize_irq); |
44 | ||
45 | #endif | |
46 | ||
47 | /** | |
48 | * disable_irq_nosync - disable an irq without waiting | |
49 | * @irq: Interrupt to disable | |
50 | * | |
51 | * Disable the selected interrupt line. Disables and Enables are | |
52 | * nested. | |
53 | * Unlike disable_irq(), this function does not ensure existing | |
54 | * instances of the IRQ handler have completed before returning. | |
55 | * | |
56 | * This function may be called from IRQ context. | |
57 | */ | |
58 | void disable_irq_nosync(unsigned int irq) | |
59 | { | |
34ffdb72 | 60 | struct irq_desc *desc = irq_desc + irq; |
1da177e4 LT |
61 | unsigned long flags; |
62 | ||
c2b5a251 MW |
63 | if (irq >= NR_IRQS) |
64 | return; | |
65 | ||
1da177e4 LT |
66 | spin_lock_irqsave(&desc->lock, flags); |
67 | if (!desc->depth++) { | |
68 | desc->status |= IRQ_DISABLED; | |
d1bef4ed | 69 | desc->chip->disable(irq); |
1da177e4 LT |
70 | } |
71 | spin_unlock_irqrestore(&desc->lock, flags); | |
72 | } | |
1da177e4 LT |
73 | EXPORT_SYMBOL(disable_irq_nosync); |
74 | ||
75 | /** | |
76 | * disable_irq - disable an irq and wait for completion | |
77 | * @irq: Interrupt to disable | |
78 | * | |
79 | * Disable the selected interrupt line. Enables and Disables are | |
80 | * nested. | |
81 | * This function waits for any pending IRQ handlers for this interrupt | |
82 | * to complete before returning. If you use this function while | |
83 | * holding a resource the IRQ handler may need you will deadlock. | |
84 | * | |
85 | * This function may be called - with care - from IRQ context. | |
86 | */ | |
87 | void disable_irq(unsigned int irq) | |
88 | { | |
34ffdb72 | 89 | struct irq_desc *desc = irq_desc + irq; |
1da177e4 | 90 | |
c2b5a251 MW |
91 | if (irq >= NR_IRQS) |
92 | return; | |
93 | ||
1da177e4 LT |
94 | disable_irq_nosync(irq); |
95 | if (desc->action) | |
96 | synchronize_irq(irq); | |
97 | } | |
1da177e4 LT |
98 | EXPORT_SYMBOL(disable_irq); |
99 | ||
100 | /** | |
101 | * enable_irq - enable handling of an irq | |
102 | * @irq: Interrupt to enable | |
103 | * | |
104 | * Undoes the effect of one call to disable_irq(). If this | |
105 | * matches the last disable, processing of interrupts on this | |
106 | * IRQ line is re-enabled. | |
107 | * | |
108 | * This function may be called from IRQ context. | |
109 | */ | |
110 | void enable_irq(unsigned int irq) | |
111 | { | |
34ffdb72 | 112 | struct irq_desc *desc = irq_desc + irq; |
1da177e4 LT |
113 | unsigned long flags; |
114 | ||
c2b5a251 MW |
115 | if (irq >= NR_IRQS) |
116 | return; | |
117 | ||
1da177e4 LT |
118 | spin_lock_irqsave(&desc->lock, flags); |
119 | switch (desc->depth) { | |
120 | case 0: | |
121 | WARN_ON(1); | |
122 | break; | |
123 | case 1: { | |
124 | unsigned int status = desc->status & ~IRQ_DISABLED; | |
125 | ||
126 | desc->status = status; | |
127 | if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) { | |
128 | desc->status = status | IRQ_REPLAY; | |
d1bef4ed | 129 | hw_resend_irq(desc->chip,irq); |
1da177e4 | 130 | } |
d1bef4ed | 131 | desc->chip->enable(irq); |
1da177e4 LT |
132 | /* fall-through */ |
133 | } | |
134 | default: | |
135 | desc->depth--; | |
136 | } | |
137 | spin_unlock_irqrestore(&desc->lock, flags); | |
138 | } | |
1da177e4 LT |
139 | EXPORT_SYMBOL(enable_irq); |
140 | ||
141 | /* | |
142 | * Internal function that tells the architecture code whether a | |
143 | * particular irq has been exclusively allocated or is available | |
144 | * for driver use. | |
145 | */ | |
146 | int can_request_irq(unsigned int irq, unsigned long irqflags) | |
147 | { | |
148 | struct irqaction *action; | |
149 | ||
150 | if (irq >= NR_IRQS) | |
151 | return 0; | |
152 | ||
153 | action = irq_desc[irq].action; | |
154 | if (action) | |
155 | if (irqflags & action->flags & SA_SHIRQ) | |
156 | action = NULL; | |
157 | ||
158 | return !action; | |
159 | } | |
160 | ||
161 | /* | |
162 | * Internal function to register an irqaction - typically used to | |
163 | * allocate special interrupts that are part of the architecture. | |
164 | */ | |
06fcb0c6 | 165 | int setup_irq(unsigned int irq, struct irqaction *new) |
1da177e4 LT |
166 | { |
167 | struct irq_desc *desc = irq_desc + irq; | |
168 | struct irqaction *old, **p; | |
169 | unsigned long flags; | |
170 | int shared = 0; | |
171 | ||
c2b5a251 MW |
172 | if (irq >= NR_IRQS) |
173 | return -EINVAL; | |
174 | ||
d1bef4ed | 175 | if (desc->chip == &no_irq_type) |
1da177e4 LT |
176 | return -ENOSYS; |
177 | /* | |
178 | * Some drivers like serial.c use request_irq() heavily, | |
179 | * so we have to be careful not to interfere with a | |
180 | * running system. | |
181 | */ | |
182 | if (new->flags & SA_SAMPLE_RANDOM) { | |
183 | /* | |
184 | * This function might sleep, we want to call it first, | |
185 | * outside of the atomic block. | |
186 | * Yes, this might clear the entropy pool if the wrong | |
187 | * driver is attempted to be loaded, without actually | |
188 | * installing a new handler, but is this really a problem, | |
189 | * only the sysadmin is able to do this. | |
190 | */ | |
191 | rand_initialize_irq(irq); | |
192 | } | |
193 | ||
194 | /* | |
195 | * The following block of code has to be executed atomically | |
196 | */ | |
06fcb0c6 | 197 | spin_lock_irqsave(&desc->lock, flags); |
1da177e4 | 198 | p = &desc->action; |
06fcb0c6 IM |
199 | old = *p; |
200 | if (old) { | |
1da177e4 | 201 | /* Can't share interrupts unless both agree to */ |
f5163427 DS |
202 | if (!(old->flags & new->flags & SA_SHIRQ)) |
203 | goto mismatch; | |
204 | ||
205 | #if defined(ARCH_HAS_IRQ_PER_CPU) && defined(SA_PERCPU_IRQ) | |
206 | /* All handlers must agree on per-cpuness */ | |
207 | if ((old->flags & IRQ_PER_CPU) != (new->flags & IRQ_PER_CPU)) | |
208 | goto mismatch; | |
209 | #endif | |
1da177e4 LT |
210 | |
211 | /* add new interrupt at end of irq queue */ | |
212 | do { | |
213 | p = &old->next; | |
214 | old = *p; | |
215 | } while (old); | |
216 | shared = 1; | |
217 | } | |
218 | ||
219 | *p = new; | |
f5163427 DS |
220 | #if defined(ARCH_HAS_IRQ_PER_CPU) && defined(SA_PERCPU_IRQ) |
221 | if (new->flags & SA_PERCPU_IRQ) | |
222 | desc->status |= IRQ_PER_CPU; | |
223 | #endif | |
1da177e4 LT |
224 | if (!shared) { |
225 | desc->depth = 0; | |
226 | desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | | |
227 | IRQ_WAITING | IRQ_INPROGRESS); | |
d1bef4ed IM |
228 | if (desc->chip->startup) |
229 | desc->chip->startup(irq); | |
1da177e4 | 230 | else |
d1bef4ed | 231 | desc->chip->enable(irq); |
1da177e4 | 232 | } |
06fcb0c6 | 233 | spin_unlock_irqrestore(&desc->lock, flags); |
1da177e4 LT |
234 | |
235 | new->irq = irq; | |
236 | register_irq_proc(irq); | |
237 | new->dir = NULL; | |
238 | register_handler_proc(irq, new); | |
239 | ||
240 | return 0; | |
f5163427 DS |
241 | |
242 | mismatch: | |
243 | spin_unlock_irqrestore(&desc->lock, flags); | |
13e87ec6 AM |
244 | if (!(new->flags & SA_PROBEIRQ)) { |
245 | printk(KERN_ERR "%s: irq handler mismatch\n", __FUNCTION__); | |
246 | dump_stack(); | |
247 | } | |
f5163427 | 248 | return -EBUSY; |
1da177e4 LT |
249 | } |
250 | ||
251 | /** | |
252 | * free_irq - free an interrupt | |
253 | * @irq: Interrupt line to free | |
254 | * @dev_id: Device identity to free | |
255 | * | |
256 | * Remove an interrupt handler. The handler is removed and if the | |
257 | * interrupt line is no longer in use by any driver it is disabled. | |
258 | * On a shared IRQ the caller must ensure the interrupt is disabled | |
259 | * on the card it drives before calling this function. The function | |
260 | * does not return until any executing interrupts for this IRQ | |
261 | * have completed. | |
262 | * | |
263 | * This function must not be called from interrupt context. | |
264 | */ | |
265 | void free_irq(unsigned int irq, void *dev_id) | |
266 | { | |
267 | struct irq_desc *desc; | |
268 | struct irqaction **p; | |
269 | unsigned long flags; | |
270 | ||
cd7b24bb | 271 | WARN_ON(in_interrupt()); |
1da177e4 LT |
272 | if (irq >= NR_IRQS) |
273 | return; | |
274 | ||
275 | desc = irq_desc + irq; | |
06fcb0c6 | 276 | spin_lock_irqsave(&desc->lock, flags); |
1da177e4 LT |
277 | p = &desc->action; |
278 | for (;;) { | |
06fcb0c6 | 279 | struct irqaction *action = *p; |
1da177e4 LT |
280 | |
281 | if (action) { | |
282 | struct irqaction **pp = p; | |
283 | ||
284 | p = &action->next; | |
285 | if (action->dev_id != dev_id) | |
286 | continue; | |
287 | ||
288 | /* Found it - now remove it from the list of entries */ | |
289 | *pp = action->next; | |
dbce706e | 290 | |
b77d6adc PBG |
291 | /* Currently used only by UML, might disappear one day.*/ |
292 | #ifdef CONFIG_IRQ_RELEASE_METHOD | |
d1bef4ed IM |
293 | if (desc->chip->release) |
294 | desc->chip->release(irq, dev_id); | |
b77d6adc | 295 | #endif |
dbce706e | 296 | |
1da177e4 LT |
297 | if (!desc->action) { |
298 | desc->status |= IRQ_DISABLED; | |
d1bef4ed IM |
299 | if (desc->chip->shutdown) |
300 | desc->chip->shutdown(irq); | |
1da177e4 | 301 | else |
d1bef4ed | 302 | desc->chip->disable(irq); |
1da177e4 | 303 | } |
06fcb0c6 | 304 | spin_unlock_irqrestore(&desc->lock, flags); |
1da177e4 LT |
305 | unregister_handler_proc(irq, action); |
306 | ||
307 | /* Make sure it's not being used on another CPU */ | |
308 | synchronize_irq(irq); | |
309 | kfree(action); | |
310 | return; | |
311 | } | |
06fcb0c6 IM |
312 | printk(KERN_ERR "Trying to free free IRQ%d\n", irq); |
313 | spin_unlock_irqrestore(&desc->lock, flags); | |
1da177e4 LT |
314 | return; |
315 | } | |
316 | } | |
1da177e4 LT |
317 | EXPORT_SYMBOL(free_irq); |
318 | ||
319 | /** | |
320 | * request_irq - allocate an interrupt line | |
321 | * @irq: Interrupt line to allocate | |
322 | * @handler: Function to be called when the IRQ occurs | |
323 | * @irqflags: Interrupt type flags | |
324 | * @devname: An ascii name for the claiming device | |
325 | * @dev_id: A cookie passed back to the handler function | |
326 | * | |
327 | * This call allocates interrupt resources and enables the | |
328 | * interrupt line and IRQ handling. From the point this | |
329 | * call is made your handler function may be invoked. Since | |
330 | * your handler function must clear any interrupt the board | |
331 | * raises, you must take care both to initialise your hardware | |
332 | * and to set up the interrupt handler in the right order. | |
333 | * | |
334 | * Dev_id must be globally unique. Normally the address of the | |
335 | * device data structure is used as the cookie. Since the handler | |
336 | * receives this value it makes sense to use it. | |
337 | * | |
338 | * If your interrupt is shared you must pass a non NULL dev_id | |
339 | * as this is required when freeing the interrupt. | |
340 | * | |
341 | * Flags: | |
342 | * | |
343 | * SA_SHIRQ Interrupt is shared | |
344 | * SA_INTERRUPT Disable local interrupts while processing | |
345 | * SA_SAMPLE_RANDOM The interrupt can be used for entropy | |
346 | * | |
347 | */ | |
348 | int request_irq(unsigned int irq, | |
349 | irqreturn_t (*handler)(int, void *, struct pt_regs *), | |
06fcb0c6 | 350 | unsigned long irqflags, const char *devname, void *dev_id) |
1da177e4 | 351 | { |
06fcb0c6 | 352 | struct irqaction *action; |
1da177e4 LT |
353 | int retval; |
354 | ||
355 | /* | |
356 | * Sanity-check: shared interrupts must pass in a real dev-ID, | |
357 | * otherwise we'll have trouble later trying to figure out | |
358 | * which interrupt is which (messes up the interrupt freeing | |
359 | * logic etc). | |
360 | */ | |
361 | if ((irqflags & SA_SHIRQ) && !dev_id) | |
362 | return -EINVAL; | |
363 | if (irq >= NR_IRQS) | |
364 | return -EINVAL; | |
365 | if (!handler) | |
366 | return -EINVAL; | |
367 | ||
368 | action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC); | |
369 | if (!action) | |
370 | return -ENOMEM; | |
371 | ||
372 | action->handler = handler; | |
373 | action->flags = irqflags; | |
374 | cpus_clear(action->mask); | |
375 | action->name = devname; | |
376 | action->next = NULL; | |
377 | action->dev_id = dev_id; | |
378 | ||
eee45269 IK |
379 | select_smp_affinity(irq); |
380 | ||
1da177e4 LT |
381 | retval = setup_irq(irq, action); |
382 | if (retval) | |
383 | kfree(action); | |
384 | ||
385 | return retval; | |
386 | } | |
1da177e4 LT |
387 | EXPORT_SYMBOL(request_irq); |
388 |