]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - kernel/irq/manage.c
Linux-2.6.12-rc2
[mirror_ubuntu-artful-kernel.git] / kernel / irq / manage.c
1 /*
2 * linux/kernel/irq/manage.c
3 *
4 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
5 *
6 * This file contains driver APIs to the irq subsystem.
7 */
8
9 #include <linux/irq.h>
10 #include <linux/module.h>
11 #include <linux/random.h>
12 #include <linux/interrupt.h>
13
14 #include "internals.h"
15
16 #ifdef CONFIG_SMP
17
18 cpumask_t irq_affinity[NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
19
20 /**
21 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
22 *
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.
26 *
27 * This function may be called - with care - from IRQ context.
28 */
29 void synchronize_irq(unsigned int irq)
30 {
31 struct irq_desc *desc = irq_desc + irq;
32
33 while (desc->status & IRQ_INPROGRESS)
34 cpu_relax();
35 }
36
37 EXPORT_SYMBOL(synchronize_irq);
38
39 #endif
40
41 /**
42 * disable_irq_nosync - disable an irq without waiting
43 * @irq: Interrupt to disable
44 *
45 * Disable the selected interrupt line. Disables and Enables are
46 * nested.
47 * Unlike disable_irq(), this function does not ensure existing
48 * instances of the IRQ handler have completed before returning.
49 *
50 * This function may be called from IRQ context.
51 */
52 void disable_irq_nosync(unsigned int irq)
53 {
54 irq_desc_t *desc = irq_desc + irq;
55 unsigned long flags;
56
57 spin_lock_irqsave(&desc->lock, flags);
58 if (!desc->depth++) {
59 desc->status |= IRQ_DISABLED;
60 desc->handler->disable(irq);
61 }
62 spin_unlock_irqrestore(&desc->lock, flags);
63 }
64
65 EXPORT_SYMBOL(disable_irq_nosync);
66
67 /**
68 * disable_irq - disable an irq and wait for completion
69 * @irq: Interrupt to disable
70 *
71 * Disable the selected interrupt line. Enables and Disables are
72 * nested.
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.
76 *
77 * This function may be called - with care - from IRQ context.
78 */
79 void disable_irq(unsigned int irq)
80 {
81 irq_desc_t *desc = irq_desc + irq;
82
83 disable_irq_nosync(irq);
84 if (desc->action)
85 synchronize_irq(irq);
86 }
87
88 EXPORT_SYMBOL(disable_irq);
89
90 /**
91 * enable_irq - enable handling of an irq
92 * @irq: Interrupt to enable
93 *
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.
97 *
98 * This function may be called from IRQ context.
99 */
100 void enable_irq(unsigned int irq)
101 {
102 irq_desc_t *desc = irq_desc + irq;
103 unsigned long flags;
104
105 spin_lock_irqsave(&desc->lock, flags);
106 switch (desc->depth) {
107 case 0:
108 WARN_ON(1);
109 break;
110 case 1: {
111 unsigned int status = desc->status & ~IRQ_DISABLED;
112
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);
117 }
118 desc->handler->enable(irq);
119 /* fall-through */
120 }
121 default:
122 desc->depth--;
123 }
124 spin_unlock_irqrestore(&desc->lock, flags);
125 }
126
127 EXPORT_SYMBOL(enable_irq);
128
129 /*
130 * Internal function that tells the architecture code whether a
131 * particular irq has been exclusively allocated or is available
132 * for driver use.
133 */
134 int can_request_irq(unsigned int irq, unsigned long irqflags)
135 {
136 struct irqaction *action;
137
138 if (irq >= NR_IRQS)
139 return 0;
140
141 action = irq_desc[irq].action;
142 if (action)
143 if (irqflags & action->flags & SA_SHIRQ)
144 action = NULL;
145
146 return !action;
147 }
148
149 /*
150 * Internal function to register an irqaction - typically used to
151 * allocate special interrupts that are part of the architecture.
152 */
153 int setup_irq(unsigned int irq, struct irqaction * new)
154 {
155 struct irq_desc *desc = irq_desc + irq;
156 struct irqaction *old, **p;
157 unsigned long flags;
158 int shared = 0;
159
160 if (desc->handler == &no_irq_type)
161 return -ENOSYS;
162 /*
163 * Some drivers like serial.c use request_irq() heavily,
164 * so we have to be careful not to interfere with a
165 * running system.
166 */
167 if (new->flags & SA_SAMPLE_RANDOM) {
168 /*
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.
175 */
176 rand_initialize_irq(irq);
177 }
178
179 /*
180 * The following block of code has to be executed atomically
181 */
182 spin_lock_irqsave(&desc->lock,flags);
183 p = &desc->action;
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);
188 return -EBUSY;
189 }
190
191 /* add new interrupt at end of irq queue */
192 do {
193 p = &old->next;
194 old = *p;
195 } while (old);
196 shared = 1;
197 }
198
199 *p = new;
200
201 if (!shared) {
202 desc->depth = 0;
203 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT |
204 IRQ_WAITING | IRQ_INPROGRESS);
205 if (desc->handler->startup)
206 desc->handler->startup(irq);
207 else
208 desc->handler->enable(irq);
209 }
210 spin_unlock_irqrestore(&desc->lock,flags);
211
212 new->irq = irq;
213 register_irq_proc(irq);
214 new->dir = NULL;
215 register_handler_proc(irq, new);
216
217 return 0;
218 }
219
220 /**
221 * free_irq - free an interrupt
222 * @irq: Interrupt line to free
223 * @dev_id: Device identity to free
224 *
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
230 * have completed.
231 *
232 * This function must not be called from interrupt context.
233 */
234 void free_irq(unsigned int irq, void *dev_id)
235 {
236 struct irq_desc *desc;
237 struct irqaction **p;
238 unsigned long flags;
239
240 if (irq >= NR_IRQS)
241 return;
242
243 desc = irq_desc + irq;
244 spin_lock_irqsave(&desc->lock,flags);
245 p = &desc->action;
246 for (;;) {
247 struct irqaction * action = *p;
248
249 if (action) {
250 struct irqaction **pp = p;
251
252 p = &action->next;
253 if (action->dev_id != dev_id)
254 continue;
255
256 /* Found it - now remove it from the list of entries */
257 *pp = action->next;
258 if (!desc->action) {
259 desc->status |= IRQ_DISABLED;
260 if (desc->handler->shutdown)
261 desc->handler->shutdown(irq);
262 else
263 desc->handler->disable(irq);
264 }
265 spin_unlock_irqrestore(&desc->lock,flags);
266 unregister_handler_proc(irq, action);
267
268 /* Make sure it's not being used on another CPU */
269 synchronize_irq(irq);
270 kfree(action);
271 return;
272 }
273 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
274 spin_unlock_irqrestore(&desc->lock,flags);
275 return;
276 }
277 }
278
279 EXPORT_SYMBOL(free_irq);
280
281 /**
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
288 *
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.
295 *
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.
299 *
300 * If your interrupt is shared you must pass a non NULL dev_id
301 * as this is required when freeing the interrupt.
302 *
303 * Flags:
304 *
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
308 *
309 */
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)
313 {
314 struct irqaction * action;
315 int retval;
316
317 /*
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
321 * logic etc).
322 */
323 if ((irqflags & SA_SHIRQ) && !dev_id)
324 return -EINVAL;
325 if (irq >= NR_IRQS)
326 return -EINVAL;
327 if (!handler)
328 return -EINVAL;
329
330 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
331 if (!action)
332 return -ENOMEM;
333
334 action->handler = handler;
335 action->flags = irqflags;
336 cpus_clear(action->mask);
337 action->name = devname;
338 action->next = NULL;
339 action->dev_id = dev_id;
340
341 retval = setup_irq(irq, action);
342 if (retval)
343 kfree(action);
344
345 return retval;
346 }
347
348 EXPORT_SYMBOL(request_irq);
349