2 * linux/kernel/irq/manage.c
4 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
6 * This file contains driver APIs to the irq subsystem.
9 #include <linux/config.h>
10 #include <linux/irq.h>
11 #include <linux/module.h>
12 #include <linux/random.h>
13 #include <linux/interrupt.h>
15 #include "internals.h"
19 cpumask_t irq_affinity[NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
22 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
24 * This function waits for any pending IRQ handlers for this interrupt
25 * to complete before returning. If you use this function while
26 * holding a resource the IRQ handler may need you will deadlock.
28 * This function may be called - with care - from IRQ context.
30 void synchronize_irq(unsigned int irq)
32 struct irq_desc *desc = irq_desc + irq;
34 while (desc->status & IRQ_INPROGRESS)
38 EXPORT_SYMBOL(synchronize_irq);
43 * disable_irq_nosync - disable an irq without waiting
44 * @irq: Interrupt to disable
46 * Disable the selected interrupt line. Disables and Enables are
48 * Unlike disable_irq(), this function does not ensure existing
49 * instances of the IRQ handler have completed before returning.
51 * This function may be called from IRQ context.
53 void disable_irq_nosync(unsigned int irq)
55 irq_desc_t *desc = irq_desc + irq;
58 spin_lock_irqsave(&desc->lock, flags);
60 desc->status |= IRQ_DISABLED;
61 desc->handler->disable(irq);
63 spin_unlock_irqrestore(&desc->lock, flags);
66 EXPORT_SYMBOL(disable_irq_nosync);
69 * disable_irq - disable an irq and wait for completion
70 * @irq: Interrupt to disable
72 * Disable the selected interrupt line. Enables and Disables are
74 * This function waits for any pending IRQ handlers for this interrupt
75 * to complete before returning. If you use this function while
76 * holding a resource the IRQ handler may need you will deadlock.
78 * This function may be called - with care - from IRQ context.
80 void disable_irq(unsigned int irq)
82 irq_desc_t *desc = irq_desc + irq;
84 disable_irq_nosync(irq);
89 EXPORT_SYMBOL(disable_irq);
92 * enable_irq - enable handling of an irq
93 * @irq: Interrupt to enable
95 * Undoes the effect of one call to disable_irq(). If this
96 * matches the last disable, processing of interrupts on this
97 * IRQ line is re-enabled.
99 * This function may be called from IRQ context.
101 void enable_irq(unsigned int irq)
103 irq_desc_t *desc = irq_desc + irq;
106 spin_lock_irqsave(&desc->lock, flags);
107 switch (desc->depth) {
112 unsigned int status = desc->status & ~IRQ_DISABLED;
114 desc->status = status;
115 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
116 desc->status = status | IRQ_REPLAY;
117 hw_resend_irq(desc->handler,irq);
119 desc->handler->enable(irq);
125 spin_unlock_irqrestore(&desc->lock, flags);
128 EXPORT_SYMBOL(enable_irq);
131 * Internal function that tells the architecture code whether a
132 * particular irq has been exclusively allocated or is available
135 int can_request_irq(unsigned int irq, unsigned long irqflags)
137 struct irqaction *action;
142 action = irq_desc[irq].action;
144 if (irqflags & action->flags & SA_SHIRQ)
151 * Internal function to register an irqaction - typically used to
152 * allocate special interrupts that are part of the architecture.
154 int setup_irq(unsigned int irq, struct irqaction * new)
156 struct irq_desc *desc = irq_desc + irq;
157 struct irqaction *old, **p;
161 if (desc->handler == &no_irq_type)
164 * Some drivers like serial.c use request_irq() heavily,
165 * so we have to be careful not to interfere with a
168 if (new->flags & SA_SAMPLE_RANDOM) {
170 * This function might sleep, we want to call it first,
171 * outside of the atomic block.
172 * Yes, this might clear the entropy pool if the wrong
173 * driver is attempted to be loaded, without actually
174 * installing a new handler, but is this really a problem,
175 * only the sysadmin is able to do this.
177 rand_initialize_irq(irq);
181 * The following block of code has to be executed atomically
183 spin_lock_irqsave(&desc->lock,flags);
185 if ((old = *p) != NULL) {
186 /* Can't share interrupts unless both agree to */
187 if (!(old->flags & new->flags & SA_SHIRQ)) {
188 spin_unlock_irqrestore(&desc->lock,flags);
192 /* add new interrupt at end of irq queue */
204 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT |
205 IRQ_WAITING | IRQ_INPROGRESS);
206 if (desc->handler->startup)
207 desc->handler->startup(irq);
209 desc->handler->enable(irq);
211 spin_unlock_irqrestore(&desc->lock,flags);
214 register_irq_proc(irq);
216 register_handler_proc(irq, new);
222 * free_irq - free an interrupt
223 * @irq: Interrupt line to free
224 * @dev_id: Device identity to free
226 * Remove an interrupt handler. The handler is removed and if the
227 * interrupt line is no longer in use by any driver it is disabled.
228 * On a shared IRQ the caller must ensure the interrupt is disabled
229 * on the card it drives before calling this function. The function
230 * does not return until any executing interrupts for this IRQ
233 * This function must not be called from interrupt context.
235 void free_irq(unsigned int irq, void *dev_id)
237 struct irq_desc *desc;
238 struct irqaction **p;
244 desc = irq_desc + irq;
245 spin_lock_irqsave(&desc->lock,flags);
248 struct irqaction * action = *p;
251 struct irqaction **pp = p;
254 if (action->dev_id != dev_id)
257 /* Found it - now remove it from the list of entries */
260 /* Currently used only by UML, might disappear one day.*/
261 #ifdef CONFIG_IRQ_RELEASE_METHOD
262 if (desc->handler->release)
263 desc->handler->release(irq, dev_id);
267 desc->status |= IRQ_DISABLED;
268 if (desc->handler->shutdown)
269 desc->handler->shutdown(irq);
271 desc->handler->disable(irq);
273 spin_unlock_irqrestore(&desc->lock,flags);
274 unregister_handler_proc(irq, action);
276 /* Make sure it's not being used on another CPU */
277 synchronize_irq(irq);
281 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
282 spin_unlock_irqrestore(&desc->lock,flags);
287 EXPORT_SYMBOL(free_irq);
290 * request_irq - allocate an interrupt line
291 * @irq: Interrupt line to allocate
292 * @handler: Function to be called when the IRQ occurs
293 * @irqflags: Interrupt type flags
294 * @devname: An ascii name for the claiming device
295 * @dev_id: A cookie passed back to the handler function
297 * This call allocates interrupt resources and enables the
298 * interrupt line and IRQ handling. From the point this
299 * call is made your handler function may be invoked. Since
300 * your handler function must clear any interrupt the board
301 * raises, you must take care both to initialise your hardware
302 * and to set up the interrupt handler in the right order.
304 * Dev_id must be globally unique. Normally the address of the
305 * device data structure is used as the cookie. Since the handler
306 * receives this value it makes sense to use it.
308 * If your interrupt is shared you must pass a non NULL dev_id
309 * as this is required when freeing the interrupt.
313 * SA_SHIRQ Interrupt is shared
314 * SA_INTERRUPT Disable local interrupts while processing
315 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
318 int request_irq(unsigned int irq,
319 irqreturn_t (*handler)(int, void *, struct pt_regs *),
320 unsigned long irqflags, const char * devname, void *dev_id)
322 struct irqaction * action;
326 * Sanity-check: shared interrupts must pass in a real dev-ID,
327 * otherwise we'll have trouble later trying to figure out
328 * which interrupt is which (messes up the interrupt freeing
331 if ((irqflags & SA_SHIRQ) && !dev_id)
338 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
342 action->handler = handler;
343 action->flags = irqflags;
344 cpus_clear(action->mask);
345 action->name = devname;
347 action->dev_id = dev_id;
349 retval = setup_irq(irq, action);
356 EXPORT_SYMBOL(request_irq);