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 #if defined (CONFIG_GENERIC_PENDING_IRQ) || defined (CONFIG_IRQBALANCE)
20 cpumask_t __cacheline_aligned pending_irq_cpumask[NR_IRQS];
24 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
25 * @irq: interrupt number to wait for
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.
31 * This function may be called - with care - from IRQ context.
33 void synchronize_irq(unsigned int irq)
35 struct irq_desc *desc = irq_desc + irq;
40 while (desc->status & IRQ_INPROGRESS)
43 EXPORT_SYMBOL(synchronize_irq);
48 * disable_irq_nosync - disable an irq without waiting
49 * @irq: Interrupt to disable
51 * Disable the selected interrupt line. Disables and Enables are
53 * Unlike disable_irq(), this function does not ensure existing
54 * instances of the IRQ handler have completed before returning.
56 * This function may be called from IRQ context.
58 void disable_irq_nosync(unsigned int irq)
60 struct irq_desc *desc = irq_desc + irq;
66 spin_lock_irqsave(&desc->lock, flags);
68 desc->status |= IRQ_DISABLED;
69 desc->chip->disable(irq);
71 spin_unlock_irqrestore(&desc->lock, flags);
73 EXPORT_SYMBOL(disable_irq_nosync);
76 * disable_irq - disable an irq and wait for completion
77 * @irq: Interrupt to disable
79 * Disable the selected interrupt line. Enables and Disables are
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.
85 * This function may be called - with care - from IRQ context.
87 void disable_irq(unsigned int irq)
89 struct irq_desc *desc = irq_desc + irq;
94 disable_irq_nosync(irq);
98 EXPORT_SYMBOL(disable_irq);
101 * enable_irq - enable handling of an irq
102 * @irq: Interrupt to enable
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.
108 * This function may be called from IRQ context.
110 void enable_irq(unsigned int irq)
112 struct irq_desc *desc = irq_desc + irq;
118 spin_lock_irqsave(&desc->lock, flags);
119 switch (desc->depth) {
124 unsigned int status = desc->status & ~IRQ_DISABLED;
126 desc->status = status;
127 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
128 desc->status = status | IRQ_REPLAY;
129 hw_resend_irq(desc->chip,irq);
131 desc->chip->enable(irq);
137 spin_unlock_irqrestore(&desc->lock, flags);
139 EXPORT_SYMBOL(enable_irq);
142 * Internal function that tells the architecture code whether a
143 * particular irq has been exclusively allocated or is available
146 int can_request_irq(unsigned int irq, unsigned long irqflags)
148 struct irqaction *action;
153 action = irq_desc[irq].action;
155 if (irqflags & action->flags & SA_SHIRQ)
162 * Internal function to register an irqaction - typically used to
163 * allocate special interrupts that are part of the architecture.
165 int setup_irq(unsigned int irq, struct irqaction *new)
167 struct irq_desc *desc = irq_desc + irq;
168 struct irqaction *old, **p;
175 if (desc->chip == &no_irq_type)
178 * Some drivers like serial.c use request_irq() heavily,
179 * so we have to be careful not to interfere with a
182 if (new->flags & SA_SAMPLE_RANDOM) {
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.
191 rand_initialize_irq(irq);
195 * The following block of code has to be executed atomically
197 spin_lock_irqsave(&desc->lock, flags);
201 /* Can't share interrupts unless both agree to */
202 if (!(old->flags & new->flags & SA_SHIRQ))
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))
211 /* add new interrupt at end of irq queue */
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;
226 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT |
227 IRQ_WAITING | IRQ_INPROGRESS);
228 if (desc->chip->startup)
229 desc->chip->startup(irq);
231 desc->chip->enable(irq);
233 spin_unlock_irqrestore(&desc->lock, flags);
236 register_irq_proc(irq);
238 register_handler_proc(irq, new);
243 spin_unlock_irqrestore(&desc->lock, flags);
244 if (!(new->flags & SA_PROBEIRQ)) {
245 printk(KERN_ERR "%s: irq handler mismatch\n", __FUNCTION__);
252 * free_irq - free an interrupt
253 * @irq: Interrupt line to free
254 * @dev_id: Device identity to free
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
263 * This function must not be called from interrupt context.
265 void free_irq(unsigned int irq, void *dev_id)
267 struct irq_desc *desc;
268 struct irqaction **p;
271 WARN_ON(in_interrupt());
275 desc = irq_desc + irq;
276 spin_lock_irqsave(&desc->lock, flags);
279 struct irqaction *action = *p;
282 struct irqaction **pp = p;
285 if (action->dev_id != dev_id)
288 /* Found it - now remove it from the list of entries */
291 /* Currently used only by UML, might disappear one day.*/
292 #ifdef CONFIG_IRQ_RELEASE_METHOD
293 if (desc->chip->release)
294 desc->chip->release(irq, dev_id);
298 desc->status |= IRQ_DISABLED;
299 if (desc->chip->shutdown)
300 desc->chip->shutdown(irq);
302 desc->chip->disable(irq);
304 spin_unlock_irqrestore(&desc->lock, flags);
305 unregister_handler_proc(irq, action);
307 /* Make sure it's not being used on another CPU */
308 synchronize_irq(irq);
312 printk(KERN_ERR "Trying to free free IRQ%d\n", irq);
313 spin_unlock_irqrestore(&desc->lock, flags);
317 EXPORT_SYMBOL(free_irq);
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
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.
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.
338 * If your interrupt is shared you must pass a non NULL dev_id
339 * as this is required when freeing the interrupt.
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
348 int request_irq(unsigned int irq,
349 irqreturn_t (*handler)(int, void *, struct pt_regs *),
350 unsigned long irqflags, const char *devname, void *dev_id)
352 struct irqaction *action;
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
361 if ((irqflags & SA_SHIRQ) && !dev_id)
368 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
372 action->handler = handler;
373 action->flags = irqflags;
374 cpus_clear(action->mask);
375 action->name = devname;
377 action->dev_id = dev_id;
379 select_smp_affinity(irq);
381 retval = setup_irq(irq, action);
387 EXPORT_SYMBOL(request_irq);