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.
10 #include <linux/module.h>
11 #include <linux/random.h>
12 #include <linux/interrupt.h>
14 #include "internals.h"
18 cpumask_t irq_affinity[NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
21 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
23 * This function waits for any pending IRQ handlers for this interrupt
24 * to complete before returning. If you use this function while
25 * holding a resource the IRQ handler may need you will deadlock.
27 * This function may be called - with care - from IRQ context.
29 void synchronize_irq(unsigned int irq)
31 struct irq_desc *desc = irq_desc + irq;
33 while (desc->status & IRQ_INPROGRESS)
37 EXPORT_SYMBOL(synchronize_irq);
42 * disable_irq_nosync - disable an irq without waiting
43 * @irq: Interrupt to disable
45 * Disable the selected interrupt line. Disables and Enables are
47 * Unlike disable_irq(), this function does not ensure existing
48 * instances of the IRQ handler have completed before returning.
50 * This function may be called from IRQ context.
52 void disable_irq_nosync(unsigned int irq)
54 irq_desc_t *desc = irq_desc + irq;
57 spin_lock_irqsave(&desc->lock, flags);
59 desc->status |= IRQ_DISABLED;
60 desc->handler->disable(irq);
62 spin_unlock_irqrestore(&desc->lock, flags);
65 EXPORT_SYMBOL(disable_irq_nosync);
68 * disable_irq - disable an irq and wait for completion
69 * @irq: Interrupt to disable
71 * Disable the selected interrupt line. Enables and Disables are
73 * This function waits for any pending IRQ handlers for this interrupt
74 * to complete before returning. If you use this function while
75 * holding a resource the IRQ handler may need you will deadlock.
77 * This function may be called - with care - from IRQ context.
79 void disable_irq(unsigned int irq)
81 irq_desc_t *desc = irq_desc + irq;
83 disable_irq_nosync(irq);
88 EXPORT_SYMBOL(disable_irq);
91 * enable_irq - enable handling of an irq
92 * @irq: Interrupt to enable
94 * Undoes the effect of one call to disable_irq(). If this
95 * matches the last disable, processing of interrupts on this
96 * IRQ line is re-enabled.
98 * This function may be called from IRQ context.
100 void enable_irq(unsigned int irq)
102 irq_desc_t *desc = irq_desc + irq;
105 spin_lock_irqsave(&desc->lock, flags);
106 switch (desc->depth) {
111 unsigned int status = desc->status & ~IRQ_DISABLED;
113 desc->status = status;
114 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
115 desc->status = status | IRQ_REPLAY;
116 hw_resend_irq(desc->handler,irq);
118 desc->handler->enable(irq);
124 spin_unlock_irqrestore(&desc->lock, flags);
127 EXPORT_SYMBOL(enable_irq);
130 * Internal function that tells the architecture code whether a
131 * particular irq has been exclusively allocated or is available
134 int can_request_irq(unsigned int irq, unsigned long irqflags)
136 struct irqaction *action;
141 action = irq_desc[irq].action;
143 if (irqflags & action->flags & SA_SHIRQ)
150 * setup_irq - register an irqaction structure
151 * @irq: Interrupt to register
152 * @irqaction: The irqaction structure to be registered
154 * Normally called by request_irq, this function can be used
155 * directly to allocate special interrupts that are part of the
158 int setup_irq(unsigned int irq, struct irqaction * new)
160 struct irq_desc *desc = irq_desc + irq;
161 struct irqaction *old, **p;
165 if (desc->handler == &no_irq_type)
168 * Some drivers like serial.c use request_irq() heavily,
169 * so we have to be careful not to interfere with a
172 if (new->flags & SA_SAMPLE_RANDOM) {
174 * This function might sleep, we want to call it first,
175 * outside of the atomic block.
176 * Yes, this might clear the entropy pool if the wrong
177 * driver is attempted to be loaded, without actually
178 * installing a new handler, but is this really a problem,
179 * only the sysadmin is able to do this.
181 rand_initialize_irq(irq);
185 * The following block of code has to be executed atomically
187 spin_lock_irqsave(&desc->lock,flags);
189 if ((old = *p) != NULL) {
190 /* Can't share interrupts unless both agree to */
191 if (!(old->flags & new->flags & SA_SHIRQ)) {
192 spin_unlock_irqrestore(&desc->lock,flags);
196 /* add new interrupt at end of irq queue */
208 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT |
209 IRQ_WAITING | IRQ_INPROGRESS);
210 if (desc->handler->startup)
211 desc->handler->startup(irq);
213 desc->handler->enable(irq);
215 spin_unlock_irqrestore(&desc->lock,flags);
218 register_irq_proc(irq);
220 register_handler_proc(irq, new);
226 * teardown_irq - unregister an irqaction
227 * @irq: Interrupt line being freed
228 * @old: Pointer to the irqaction that is to be unregistered
230 * This function is called by free_irq and does the actual
231 * business of unregistering the handler. It exists as a
232 * seperate function to enable handlers to be unregistered
233 * for irqactions that have been allocated statically at
236 * This function must not be called from interrupt context.
238 int teardown_irq(unsigned int irq, struct irqaction * old)
240 struct irq_desc *desc;
241 struct irqaction **p;
247 desc = irq_desc + irq;
248 spin_lock_irqsave(&desc->lock,flags);
251 struct irqaction * action = *p;
254 struct irqaction **pp = p;
260 /* Found it - now remove it from the list of entries */
263 desc->status |= IRQ_DISABLED;
264 if (desc->handler->shutdown)
265 desc->handler->shutdown(irq);
267 desc->handler->disable(irq);
269 spin_unlock_irqrestore(&desc->lock,flags);
270 unregister_handler_proc(irq, action);
272 /* Make sure it's not being used on another CPU */
273 synchronize_irq(irq);
276 printk(KERN_ERR "Trying to teardown free IRQ%d\n",irq);
277 spin_unlock_irqrestore(&desc->lock,flags);
283 * free_irq - free an interrupt
284 * @irq: Interrupt line to free
285 * @dev_id: Device identity to free
287 * Remove an interrupt handler. The handler is removed and if the
288 * interrupt line is no longer in use by any driver it is disabled.
289 * On a shared IRQ the caller must ensure the interrupt is disabled
290 * on the card it drives before calling this function. The function
291 * does not return until any executing interrupts for this IRQ
294 * This function must not be called from interrupt context.
296 void free_irq(unsigned int irq, void *dev_id)
298 struct irq_desc *desc;
299 struct irqaction *action;
305 desc = irq_desc + irq;
306 spin_lock_irqsave(&desc->lock,flags);
307 for (action = desc->action; action != NULL; action = action->next) {
308 if (action->dev_id != dev_id)
311 spin_unlock_irqrestore(&desc->lock,flags);
313 if (teardown_irq(irq, action) == 0)
317 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
318 spin_unlock_irqrestore(&desc->lock,flags);
322 EXPORT_SYMBOL(free_irq);
325 * request_irq - allocate an interrupt line
326 * @irq: Interrupt line to allocate
327 * @handler: Function to be called when the IRQ occurs
328 * @irqflags: Interrupt type flags
329 * @devname: An ascii name for the claiming device
330 * @dev_id: A cookie passed back to the handler function
332 * This call allocates interrupt resources and enables the
333 * interrupt line and IRQ handling. From the point this
334 * call is made your handler function may be invoked. Since
335 * your handler function must clear any interrupt the board
336 * raises, you must take care both to initialise your hardware
337 * and to set up the interrupt handler in the right order.
339 * Dev_id must be globally unique. Normally the address of the
340 * device data structure is used as the cookie. Since the handler
341 * receives this value it makes sense to use it.
343 * If your interrupt is shared you must pass a non NULL dev_id
344 * as this is required when freeing the interrupt.
348 * SA_SHIRQ Interrupt is shared
349 * SA_INTERRUPT Disable local interrupts while processing
350 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
353 int request_irq(unsigned int irq,
354 irqreturn_t (*handler)(int, void *, struct pt_regs *),
355 unsigned long irqflags, const char * devname, void *dev_id)
357 struct irqaction * action;
361 * Sanity-check: shared interrupts must pass in a real dev-ID,
362 * otherwise we'll have trouble later trying to figure out
363 * which interrupt is which (messes up the interrupt freeing
366 if ((irqflags & SA_SHIRQ) && !dev_id)
373 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
377 action->handler = handler;
378 action->flags = irqflags;
379 cpus_clear(action->mask);
380 action->name = devname;
382 action->dev_id = dev_id;
384 retval = setup_irq(irq, action);
391 EXPORT_SYMBOL(request_irq);