blob: c53662edc73d9275085077787cb3a7319bb1119b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Paolo 'Blaisorblade' Giarrussob77d6ad2005-06-21 17:16:24 -07009#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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
Ashok Raj54d5d422005-09-06 15:16:15 -070019#if defined (CONFIG_GENERIC_PENDING_IRQ) || defined (CONFIG_IRQBALANCE)
20cpumask_t __cacheline_aligned pending_irq_cpumask[NR_IRQS];
21#endif
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/**
24 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
Randy Dunlap1e5d5332005-11-07 01:01:06 -080025 * @irq: interrupt number to wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
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 */
33void synchronize_irq(unsigned int irq)
34{
35 struct irq_desc *desc = irq_desc + irq;
36
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -070037 if (irq >= NR_IRQS)
38 return;
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 while (desc->status & IRQ_INPROGRESS)
41 cpu_relax();
42}
43
44EXPORT_SYMBOL(synchronize_irq);
45
46#endif
47
48/**
49 * disable_irq_nosync - disable an irq without waiting
50 * @irq: Interrupt to disable
51 *
52 * Disable the selected interrupt line. Disables and Enables are
53 * nested.
54 * Unlike disable_irq(), this function does not ensure existing
55 * instances of the IRQ handler have completed before returning.
56 *
57 * This function may be called from IRQ context.
58 */
59void disable_irq_nosync(unsigned int irq)
60{
61 irq_desc_t *desc = irq_desc + irq;
62 unsigned long flags;
63
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -070064 if (irq >= NR_IRQS)
65 return;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 spin_lock_irqsave(&desc->lock, flags);
68 if (!desc->depth++) {
69 desc->status |= IRQ_DISABLED;
Ingo Molnard1bef4e2006-06-29 02:24:36 -070070 desc->chip->disable(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72 spin_unlock_irqrestore(&desc->lock, flags);
73}
74
75EXPORT_SYMBOL(disable_irq_nosync);
76
77/**
78 * disable_irq - disable an irq and wait for completion
79 * @irq: Interrupt to disable
80 *
81 * Disable the selected interrupt line. Enables and Disables are
82 * nested.
83 * This function waits for any pending IRQ handlers for this interrupt
84 * to complete before returning. If you use this function while
85 * holding a resource the IRQ handler may need you will deadlock.
86 *
87 * This function may be called - with care - from IRQ context.
88 */
89void disable_irq(unsigned int irq)
90{
91 irq_desc_t *desc = irq_desc + irq;
92
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -070093 if (irq >= NR_IRQS)
94 return;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 disable_irq_nosync(irq);
97 if (desc->action)
98 synchronize_irq(irq);
99}
100
101EXPORT_SYMBOL(disable_irq);
102
103/**
104 * enable_irq - enable handling of an irq
105 * @irq: Interrupt to enable
106 *
107 * Undoes the effect of one call to disable_irq(). If this
108 * matches the last disable, processing of interrupts on this
109 * IRQ line is re-enabled.
110 *
111 * This function may be called from IRQ context.
112 */
113void enable_irq(unsigned int irq)
114{
115 irq_desc_t *desc = irq_desc + irq;
116 unsigned long flags;
117
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -0700118 if (irq >= NR_IRQS)
119 return;
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 spin_lock_irqsave(&desc->lock, flags);
122 switch (desc->depth) {
123 case 0:
124 WARN_ON(1);
125 break;
126 case 1: {
127 unsigned int status = desc->status & ~IRQ_DISABLED;
128
129 desc->status = status;
130 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
131 desc->status = status | IRQ_REPLAY;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700132 hw_resend_irq(desc->chip,irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700134 desc->chip->enable(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /* fall-through */
136 }
137 default:
138 desc->depth--;
139 }
140 spin_unlock_irqrestore(&desc->lock, flags);
141}
142
143EXPORT_SYMBOL(enable_irq);
144
145/*
146 * Internal function that tells the architecture code whether a
147 * particular irq has been exclusively allocated or is available
148 * for driver use.
149 */
150int can_request_irq(unsigned int irq, unsigned long irqflags)
151{
152 struct irqaction *action;
153
154 if (irq >= NR_IRQS)
155 return 0;
156
157 action = irq_desc[irq].action;
158 if (action)
159 if (irqflags & action->flags & SA_SHIRQ)
160 action = NULL;
161
162 return !action;
163}
164
165/*
166 * Internal function to register an irqaction - typically used to
167 * allocate special interrupts that are part of the architecture.
168 */
169int setup_irq(unsigned int irq, struct irqaction * new)
170{
171 struct irq_desc *desc = irq_desc + irq;
172 struct irqaction *old, **p;
173 unsigned long flags;
174 int shared = 0;
175
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -0700176 if (irq >= NR_IRQS)
177 return -EINVAL;
178
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700179 if (desc->chip == &no_irq_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return -ENOSYS;
181 /*
182 * Some drivers like serial.c use request_irq() heavily,
183 * so we have to be careful not to interfere with a
184 * running system.
185 */
186 if (new->flags & SA_SAMPLE_RANDOM) {
187 /*
188 * This function might sleep, we want to call it first,
189 * outside of the atomic block.
190 * Yes, this might clear the entropy pool if the wrong
191 * driver is attempted to be loaded, without actually
192 * installing a new handler, but is this really a problem,
193 * only the sysadmin is able to do this.
194 */
195 rand_initialize_irq(irq);
196 }
197
198 /*
199 * The following block of code has to be executed atomically
200 */
201 spin_lock_irqsave(&desc->lock,flags);
202 p = &desc->action;
203 if ((old = *p) != NULL) {
204 /* Can't share interrupts unless both agree to */
Dimitri Sivanichf5163422006-03-25 03:08:23 -0800205 if (!(old->flags & new->flags & SA_SHIRQ))
206 goto mismatch;
207
208#if defined(ARCH_HAS_IRQ_PER_CPU) && defined(SA_PERCPU_IRQ)
209 /* All handlers must agree on per-cpuness */
210 if ((old->flags & IRQ_PER_CPU) != (new->flags & IRQ_PER_CPU))
211 goto mismatch;
212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* add new interrupt at end of irq queue */
215 do {
216 p = &old->next;
217 old = *p;
218 } while (old);
219 shared = 1;
220 }
221
222 *p = new;
Dimitri Sivanichf5163422006-03-25 03:08:23 -0800223#if defined(ARCH_HAS_IRQ_PER_CPU) && defined(SA_PERCPU_IRQ)
224 if (new->flags & SA_PERCPU_IRQ)
225 desc->status |= IRQ_PER_CPU;
226#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!shared) {
228 desc->depth = 0;
229 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT |
230 IRQ_WAITING | IRQ_INPROGRESS);
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700231 if (desc->chip->startup)
232 desc->chip->startup(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 else
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700234 desc->chip->enable(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 spin_unlock_irqrestore(&desc->lock,flags);
237
238 new->irq = irq;
239 register_irq_proc(irq);
240 new->dir = NULL;
241 register_handler_proc(irq, new);
242
243 return 0;
Dimitri Sivanichf5163422006-03-25 03:08:23 -0800244
245mismatch:
246 spin_unlock_irqrestore(&desc->lock, flags);
Andrew Morton13e87ec2006-04-27 18:39:18 -0700247 if (!(new->flags & SA_PROBEIRQ)) {
248 printk(KERN_ERR "%s: irq handler mismatch\n", __FUNCTION__);
249 dump_stack();
250 }
Dimitri Sivanichf5163422006-03-25 03:08:23 -0800251 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254/**
255 * free_irq - free an interrupt
256 * @irq: Interrupt line to free
257 * @dev_id: Device identity to free
258 *
259 * Remove an interrupt handler. The handler is removed and if the
260 * interrupt line is no longer in use by any driver it is disabled.
261 * On a shared IRQ the caller must ensure the interrupt is disabled
262 * on the card it drives before calling this function. The function
263 * does not return until any executing interrupts for this IRQ
264 * have completed.
265 *
266 * This function must not be called from interrupt context.
267 */
268void free_irq(unsigned int irq, void *dev_id)
269{
270 struct irq_desc *desc;
271 struct irqaction **p;
272 unsigned long flags;
273
Ingo Molnarcd7b24b2006-03-26 01:36:54 -0800274 WARN_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (irq >= NR_IRQS)
276 return;
277
278 desc = irq_desc + irq;
279 spin_lock_irqsave(&desc->lock,flags);
280 p = &desc->action;
281 for (;;) {
282 struct irqaction * action = *p;
283
284 if (action) {
285 struct irqaction **pp = p;
286
287 p = &action->next;
288 if (action->dev_id != dev_id)
289 continue;
290
291 /* Found it - now remove it from the list of entries */
292 *pp = action->next;
Paolo 'Blaisorblade' Giarrussodbce7062005-06-21 17:16:19 -0700293
Paolo 'Blaisorblade' Giarrussob77d6ad2005-06-21 17:16:24 -0700294 /* Currently used only by UML, might disappear one day.*/
295#ifdef CONFIG_IRQ_RELEASE_METHOD
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700296 if (desc->chip->release)
297 desc->chip->release(irq, dev_id);
Paolo 'Blaisorblade' Giarrussob77d6ad2005-06-21 17:16:24 -0700298#endif
Paolo 'Blaisorblade' Giarrussodbce7062005-06-21 17:16:19 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (!desc->action) {
301 desc->status |= IRQ_DISABLED;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700302 if (desc->chip->shutdown)
303 desc->chip->shutdown(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 else
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700305 desc->chip->disable(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307 spin_unlock_irqrestore(&desc->lock,flags);
308 unregister_handler_proc(irq, action);
309
310 /* Make sure it's not being used on another CPU */
311 synchronize_irq(irq);
312 kfree(action);
313 return;
314 }
315 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
316 spin_unlock_irqrestore(&desc->lock,flags);
317 return;
318 }
319}
320
321EXPORT_SYMBOL(free_irq);
322
323/**
324 * request_irq - allocate an interrupt line
325 * @irq: Interrupt line to allocate
326 * @handler: Function to be called when the IRQ occurs
327 * @irqflags: Interrupt type flags
328 * @devname: An ascii name for the claiming device
329 * @dev_id: A cookie passed back to the handler function
330 *
331 * This call allocates interrupt resources and enables the
332 * interrupt line and IRQ handling. From the point this
333 * call is made your handler function may be invoked. Since
334 * your handler function must clear any interrupt the board
335 * raises, you must take care both to initialise your hardware
336 * and to set up the interrupt handler in the right order.
337 *
338 * Dev_id must be globally unique. Normally the address of the
339 * device data structure is used as the cookie. Since the handler
340 * receives this value it makes sense to use it.
341 *
342 * If your interrupt is shared you must pass a non NULL dev_id
343 * as this is required when freeing the interrupt.
344 *
345 * Flags:
346 *
347 * SA_SHIRQ Interrupt is shared
348 * SA_INTERRUPT Disable local interrupts while processing
349 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
350 *
351 */
352int request_irq(unsigned int irq,
353 irqreturn_t (*handler)(int, void *, struct pt_regs *),
354 unsigned long irqflags, const char * devname, void *dev_id)
355{
356 struct irqaction * action;
357 int retval;
358
359 /*
360 * Sanity-check: shared interrupts must pass in a real dev-ID,
361 * otherwise we'll have trouble later trying to figure out
362 * which interrupt is which (messes up the interrupt freeing
363 * logic etc).
364 */
365 if ((irqflags & SA_SHIRQ) && !dev_id)
366 return -EINVAL;
367 if (irq >= NR_IRQS)
368 return -EINVAL;
369 if (!handler)
370 return -EINVAL;
371
372 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
373 if (!action)
374 return -ENOMEM;
375
376 action->handler = handler;
377 action->flags = irqflags;
378 cpus_clear(action->mask);
379 action->name = devname;
380 action->next = NULL;
381 action->dev_id = dev_id;
382
Ivan Kokshayskyeee45262006-01-06 00:12:21 -0800383 select_smp_affinity(irq);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 retval = setup_irq(irq, action);
386 if (retval)
387 kfree(action);
388
389 return retval;
390}
391
392EXPORT_SYMBOL(request_irq);
393