[PATCH] Kconfig fix (IRQ_ALL_CPUS vs. MV64360)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / irq / manage.c
blobac670098570579e980e6b7bba35a235f7be885a9
1 /*
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.
7 */
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"
17 #ifdef CONFIG_SMP
19 cpumask_t irq_affinity[NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
21 /**
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)
35 cpu_relax();
38 EXPORT_SYMBOL(synchronize_irq);
40 #endif
42 /**
43 * disable_irq_nosync - disable an irq without waiting
44 * @irq: Interrupt to disable
46 * Disable the selected interrupt line. Disables and Enables are
47 * nested.
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;
56 unsigned long flags;
58 spin_lock_irqsave(&desc->lock, flags);
59 if (!desc->depth++) {
60 desc->status |= IRQ_DISABLED;
61 desc->handler->disable(irq);
63 spin_unlock_irqrestore(&desc->lock, flags);
66 EXPORT_SYMBOL(disable_irq_nosync);
68 /**
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
73 * nested.
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);
85 if (desc->action)
86 synchronize_irq(irq);
89 EXPORT_SYMBOL(disable_irq);
91 /**
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;
104 unsigned long flags;
106 spin_lock_irqsave(&desc->lock, flags);
107 switch (desc->depth) {
108 case 0:
109 WARN_ON(1);
110 break;
111 case 1: {
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);
120 /* fall-through */
122 default:
123 desc->depth--;
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
133 * for driver use.
135 int can_request_irq(unsigned int irq, unsigned long irqflags)
137 struct irqaction *action;
139 if (irq >= NR_IRQS)
140 return 0;
142 action = irq_desc[irq].action;
143 if (action)
144 if (irqflags & action->flags & SA_SHIRQ)
145 action = NULL;
147 return !action;
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;
158 unsigned long flags;
159 int shared = 0;
161 if (desc->handler == &no_irq_type)
162 return -ENOSYS;
164 * Some drivers like serial.c use request_irq() heavily,
165 * so we have to be careful not to interfere with a
166 * running system.
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);
184 p = &desc->action;
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);
189 return -EBUSY;
192 /* add new interrupt at end of irq queue */
193 do {
194 p = &old->next;
195 old = *p;
196 } while (old);
197 shared = 1;
200 *p = new;
202 if (!shared) {
203 desc->depth = 0;
204 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT |
205 IRQ_WAITING | IRQ_INPROGRESS);
206 if (desc->handler->startup)
207 desc->handler->startup(irq);
208 else
209 desc->handler->enable(irq);
211 spin_unlock_irqrestore(&desc->lock,flags);
213 new->irq = irq;
214 register_irq_proc(irq);
215 new->dir = NULL;
216 register_handler_proc(irq, new);
218 return 0;
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
231 * have completed.
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;
239 unsigned long flags;
241 if (irq >= NR_IRQS)
242 return;
244 desc = irq_desc + irq;
245 spin_lock_irqsave(&desc->lock,flags);
246 p = &desc->action;
247 for (;;) {
248 struct irqaction * action = *p;
250 if (action) {
251 struct irqaction **pp = p;
253 p = &action->next;
254 if (action->dev_id != dev_id)
255 continue;
257 /* Found it - now remove it from the list of entries */
258 *pp = action->next;
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);
264 #endif
266 if (!desc->action) {
267 desc->status |= IRQ_DISABLED;
268 if (desc->handler->shutdown)
269 desc->handler->shutdown(irq);
270 else
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);
278 kfree(action);
279 return;
281 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
282 spin_unlock_irqrestore(&desc->lock,flags);
283 return;
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.
311 * Flags:
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;
323 int retval;
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
329 * logic etc).
331 if ((irqflags & SA_SHIRQ) && !dev_id)
332 return -EINVAL;
333 if (irq >= NR_IRQS)
334 return -EINVAL;
335 if (!handler)
336 return -EINVAL;
338 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
339 if (!action)
340 return -ENOMEM;
342 action->handler = handler;
343 action->flags = irqflags;
344 cpus_clear(action->mask);
345 action->name = devname;
346 action->next = NULL;
347 action->dev_id = dev_id;
349 retval = setup_irq(irq, action);
350 if (retval)
351 kfree(action);
353 return retval;
356 EXPORT_SYMBOL(request_irq);