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 * Internal function to register an irqaction - typically used to
151 * allocate special interrupts that are part of the architecture.
153 int setup_irq(unsigned int irq
, struct irqaction
* new)
155 struct irq_desc
*desc
= irq_desc
+ irq
;
156 struct irqaction
*old
, **p
;
160 if (desc
->handler
== &no_irq_type
)
163 * Some drivers like serial.c use request_irq() heavily,
164 * so we have to be careful not to interfere with a
167 if (new->flags
& SA_SAMPLE_RANDOM
) {
169 * This function might sleep, we want to call it first,
170 * outside of the atomic block.
171 * Yes, this might clear the entropy pool if the wrong
172 * driver is attempted to be loaded, without actually
173 * installing a new handler, but is this really a problem,
174 * only the sysadmin is able to do this.
176 rand_initialize_irq(irq
);
180 * The following block of code has to be executed atomically
182 spin_lock_irqsave(&desc
->lock
,flags
);
184 if ((old
= *p
) != NULL
) {
185 /* Can't share interrupts unless both agree to */
186 if (!(old
->flags
& new->flags
& SA_SHIRQ
)) {
187 spin_unlock_irqrestore(&desc
->lock
,flags
);
191 /* add new interrupt at end of irq queue */
203 desc
->status
&= ~(IRQ_DISABLED
| IRQ_AUTODETECT
|
204 IRQ_WAITING
| IRQ_INPROGRESS
);
205 if (desc
->handler
->startup
)
206 desc
->handler
->startup(irq
);
208 desc
->handler
->enable(irq
);
210 spin_unlock_irqrestore(&desc
->lock
,flags
);
213 register_irq_proc(irq
);
215 register_handler_proc(irq
, new);
221 * free_irq - free an interrupt
222 * @irq: Interrupt line to free
223 * @dev_id: Device identity to free
225 * Remove an interrupt handler. The handler is removed and if the
226 * interrupt line is no longer in use by any driver it is disabled.
227 * On a shared IRQ the caller must ensure the interrupt is disabled
228 * on the card it drives before calling this function. The function
229 * does not return until any executing interrupts for this IRQ
232 * This function must not be called from interrupt context.
234 void free_irq(unsigned int irq
, void *dev_id
)
236 struct irq_desc
*desc
;
237 struct irqaction
**p
;
243 desc
= irq_desc
+ irq
;
244 spin_lock_irqsave(&desc
->lock
,flags
);
247 struct irqaction
* action
= *p
;
250 struct irqaction
**pp
= p
;
253 if (action
->dev_id
!= dev_id
)
256 /* Found it - now remove it from the list of entries */
259 desc
->status
|= IRQ_DISABLED
;
260 if (desc
->handler
->shutdown
)
261 desc
->handler
->shutdown(irq
);
263 desc
->handler
->disable(irq
);
265 spin_unlock_irqrestore(&desc
->lock
,flags
);
266 unregister_handler_proc(irq
, action
);
268 /* Make sure it's not being used on another CPU */
269 synchronize_irq(irq
);
273 printk(KERN_ERR
"Trying to free free IRQ%d\n",irq
);
274 spin_unlock_irqrestore(&desc
->lock
,flags
);
279 EXPORT_SYMBOL(free_irq
);
282 * request_irq - allocate an interrupt line
283 * @irq: Interrupt line to allocate
284 * @handler: Function to be called when the IRQ occurs
285 * @irqflags: Interrupt type flags
286 * @devname: An ascii name for the claiming device
287 * @dev_id: A cookie passed back to the handler function
289 * This call allocates interrupt resources and enables the
290 * interrupt line and IRQ handling. From the point this
291 * call is made your handler function may be invoked. Since
292 * your handler function must clear any interrupt the board
293 * raises, you must take care both to initialise your hardware
294 * and to set up the interrupt handler in the right order.
296 * Dev_id must be globally unique. Normally the address of the
297 * device data structure is used as the cookie. Since the handler
298 * receives this value it makes sense to use it.
300 * If your interrupt is shared you must pass a non NULL dev_id
301 * as this is required when freeing the interrupt.
305 * SA_SHIRQ Interrupt is shared
306 * SA_INTERRUPT Disable local interrupts while processing
307 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
310 int request_irq(unsigned int irq
,
311 irqreturn_t (*handler
)(int, void *, struct pt_regs
*),
312 unsigned long irqflags
, const char * devname
, void *dev_id
)
314 struct irqaction
* action
;
318 * Sanity-check: shared interrupts must pass in a real dev-ID,
319 * otherwise we'll have trouble later trying to figure out
320 * which interrupt is which (messes up the interrupt freeing
323 if ((irqflags
& SA_SHIRQ
) && !dev_id
)
330 action
= kmalloc(sizeof(struct irqaction
), GFP_ATOMIC
);
334 action
->handler
= handler
;
335 action
->flags
= irqflags
;
336 cpus_clear(action
->mask
);
337 action
->name
= devname
;
339 action
->dev_id
= dev_id
;
341 retval
= setup_irq(irq
, action
);
348 EXPORT_SYMBOL(request_irq
);