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
};
21 #if defined (CONFIG_GENERIC_PENDING_IRQ) || defined (CONFIG_IRQBALANCE)
22 cpumask_t __cacheline_aligned pending_irq_cpumask
[NR_IRQS
];
26 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
27 * @irq: interrupt number to wait for
29 * This function waits for any pending IRQ handlers for this interrupt
30 * to complete before returning. If you use this function while
31 * holding a resource the IRQ handler may need you will deadlock.
33 * This function may be called - with care - from IRQ context.
35 void synchronize_irq(unsigned int irq
)
37 struct irq_desc
*desc
= irq_desc
+ irq
;
39 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 irq_desc_t
*desc
= irq_desc
+ irq
;
63 spin_lock_irqsave(&desc
->lock
, flags
);
65 desc
->status
|= IRQ_DISABLED
;
66 desc
->handler
->disable(irq
);
68 spin_unlock_irqrestore(&desc
->lock
, flags
);
71 EXPORT_SYMBOL(disable_irq_nosync
);
74 * disable_irq - disable an irq and wait for completion
75 * @irq: Interrupt to disable
77 * Disable the selected interrupt line. Enables and Disables are
79 * This function waits for any pending IRQ handlers for this interrupt
80 * to complete before returning. If you use this function while
81 * holding a resource the IRQ handler may need you will deadlock.
83 * This function may be called - with care - from IRQ context.
85 void disable_irq(unsigned int irq
)
87 irq_desc_t
*desc
= irq_desc
+ irq
;
89 disable_irq_nosync(irq
);
94 EXPORT_SYMBOL(disable_irq
);
97 * enable_irq - enable handling of an irq
98 * @irq: Interrupt to enable
100 * Undoes the effect of one call to disable_irq(). If this
101 * matches the last disable, processing of interrupts on this
102 * IRQ line is re-enabled.
104 * This function may be called from IRQ context.
106 void enable_irq(unsigned int irq
)
108 irq_desc_t
*desc
= irq_desc
+ irq
;
111 spin_lock_irqsave(&desc
->lock
, flags
);
112 switch (desc
->depth
) {
117 unsigned int status
= desc
->status
& ~IRQ_DISABLED
;
119 desc
->status
= status
;
120 if ((status
& (IRQ_PENDING
| IRQ_REPLAY
)) == IRQ_PENDING
) {
121 desc
->status
= status
| IRQ_REPLAY
;
122 hw_resend_irq(desc
->handler
,irq
);
124 desc
->handler
->enable(irq
);
130 spin_unlock_irqrestore(&desc
->lock
, flags
);
133 EXPORT_SYMBOL(enable_irq
);
136 * Internal function that tells the architecture code whether a
137 * particular irq has been exclusively allocated or is available
140 int can_request_irq(unsigned int irq
, unsigned long irqflags
)
142 struct irqaction
*action
;
147 action
= irq_desc
[irq
].action
;
149 if (irqflags
& action
->flags
& SA_SHIRQ
)
156 * Internal function to register an irqaction - typically used to
157 * allocate special interrupts that are part of the architecture.
159 int setup_irq(unsigned int irq
, struct irqaction
* new)
161 struct irq_desc
*desc
= irq_desc
+ irq
;
162 struct irqaction
*old
, **p
;
166 if (desc
->handler
== &no_irq_type
)
169 * Some drivers like serial.c use request_irq() heavily,
170 * so we have to be careful not to interfere with a
173 if (new->flags
& SA_SAMPLE_RANDOM
) {
175 * This function might sleep, we want to call it first,
176 * outside of the atomic block.
177 * Yes, this might clear the entropy pool if the wrong
178 * driver is attempted to be loaded, without actually
179 * installing a new handler, but is this really a problem,
180 * only the sysadmin is able to do this.
182 rand_initialize_irq(irq
);
186 * The following block of code has to be executed atomically
188 spin_lock_irqsave(&desc
->lock
,flags
);
190 if ((old
= *p
) != NULL
) {
191 /* Can't share interrupts unless both agree to */
192 if (!(old
->flags
& new->flags
& SA_SHIRQ
)) {
193 spin_unlock_irqrestore(&desc
->lock
,flags
);
197 /* add new interrupt at end of irq queue */
209 desc
->status
&= ~(IRQ_DISABLED
| IRQ_AUTODETECT
|
210 IRQ_WAITING
| IRQ_INPROGRESS
);
211 if (desc
->handler
->startup
)
212 desc
->handler
->startup(irq
);
214 desc
->handler
->enable(irq
);
216 spin_unlock_irqrestore(&desc
->lock
,flags
);
219 register_irq_proc(irq
);
221 register_handler_proc(irq
, new);
227 * free_irq - free an interrupt
228 * @irq: Interrupt line to free
229 * @dev_id: Device identity to free
231 * Remove an interrupt handler. The handler is removed and if the
232 * interrupt line is no longer in use by any driver it is disabled.
233 * On a shared IRQ the caller must ensure the interrupt is disabled
234 * on the card it drives before calling this function. The function
235 * does not return until any executing interrupts for this IRQ
238 * This function must not be called from interrupt context.
240 void free_irq(unsigned int irq
, void *dev_id
)
242 struct irq_desc
*desc
;
243 struct irqaction
**p
;
249 desc
= irq_desc
+ irq
;
250 spin_lock_irqsave(&desc
->lock
,flags
);
253 struct irqaction
* action
= *p
;
256 struct irqaction
**pp
= p
;
259 if (action
->dev_id
!= dev_id
)
262 /* Found it - now remove it from the list of entries */
265 /* Currently used only by UML, might disappear one day.*/
266 #ifdef CONFIG_IRQ_RELEASE_METHOD
267 if (desc
->handler
->release
)
268 desc
->handler
->release(irq
, dev_id
);
272 desc
->status
|= IRQ_DISABLED
;
273 if (desc
->handler
->shutdown
)
274 desc
->handler
->shutdown(irq
);
276 desc
->handler
->disable(irq
);
278 spin_unlock_irqrestore(&desc
->lock
,flags
);
279 unregister_handler_proc(irq
, action
);
281 /* Make sure it's not being used on another CPU */
282 synchronize_irq(irq
);
286 printk(KERN_ERR
"Trying to free free IRQ%d\n",irq
);
287 spin_unlock_irqrestore(&desc
->lock
,flags
);
292 EXPORT_SYMBOL(free_irq
);
295 * request_irq - allocate an interrupt line
296 * @irq: Interrupt line to allocate
297 * @handler: Function to be called when the IRQ occurs
298 * @irqflags: Interrupt type flags
299 * @devname: An ascii name for the claiming device
300 * @dev_id: A cookie passed back to the handler function
302 * This call allocates interrupt resources and enables the
303 * interrupt line and IRQ handling. From the point this
304 * call is made your handler function may be invoked. Since
305 * your handler function must clear any interrupt the board
306 * raises, you must take care both to initialise your hardware
307 * and to set up the interrupt handler in the right order.
309 * Dev_id must be globally unique. Normally the address of the
310 * device data structure is used as the cookie. Since the handler
311 * receives this value it makes sense to use it.
313 * If your interrupt is shared you must pass a non NULL dev_id
314 * as this is required when freeing the interrupt.
318 * SA_SHIRQ Interrupt is shared
319 * SA_INTERRUPT Disable local interrupts while processing
320 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
323 int request_irq(unsigned int irq
,
324 irqreturn_t (*handler
)(int, void *, struct pt_regs
*),
325 unsigned long irqflags
, const char * devname
, void *dev_id
)
327 struct irqaction
* action
;
331 * Sanity-check: shared interrupts must pass in a real dev-ID,
332 * otherwise we'll have trouble later trying to figure out
333 * which interrupt is which (messes up the interrupt freeing
336 if ((irqflags
& SA_SHIRQ
) && !dev_id
)
343 action
= kmalloc(sizeof(struct irqaction
), GFP_ATOMIC
);
347 action
->handler
= handler
;
348 action
->flags
= irqflags
;
349 cpus_clear(action
->mask
);
350 action
->name
= devname
;
352 action
->dev_id
= dev_id
;
354 retval
= setup_irq(irq
, action
);
361 EXPORT_SYMBOL(request_irq
);