ia64/xen-unstable

view linux-2.6.10-xen-sparse/kernel/irq/manage.c @ 3377:4c2c41071357

bitkeeper revision 1.1159.212.3 (41da81905Ftkk0Mw5Oh9JMpyAopNkQ)

Makefile:
Remove extraneous text after `endif'.
author cl349@arcadians.cl.cam.ac.uk
date Tue Jan 04 11:44:16 2005 +0000 (2005-01-04)
parents 39a7a74fd6f9
children 0a4b76b6b5a0
line source
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 */
9 #include <linux/irq.h>
10 #include <linux/module.h>
11 #include <linux/random.h>
12 #include <linux/interrupt.h>
14 #include "internals.h"
16 #ifdef CONFIG_SMP
18 /**
19 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
20 *
21 * This function waits for any pending IRQ handlers for this interrupt
22 * to complete before returning. If you use this function while
23 * holding a resource the IRQ handler may need you will deadlock.
24 *
25 * This function may be called - with care - from IRQ context.
26 */
27 void synchronize_irq(unsigned int irq)
28 {
29 struct irq_desc *desc = irq_desc + irq;
31 while (desc->status & IRQ_INPROGRESS)
32 cpu_relax();
33 }
35 EXPORT_SYMBOL(synchronize_irq);
37 #endif
39 /**
40 * disable_irq_nosync - disable an irq without waiting
41 * @irq: Interrupt to disable
42 *
43 * Disable the selected interrupt line. Disables and Enables are
44 * nested.
45 * Unlike disable_irq(), this function does not ensure existing
46 * instances of the IRQ handler have completed before returning.
47 *
48 * This function may be called from IRQ context.
49 */
50 void disable_irq_nosync(unsigned int irq)
51 {
52 irq_desc_t *desc = irq_desc + irq;
53 unsigned long flags;
55 spin_lock_irqsave(&desc->lock, flags);
56 if (!desc->depth++) {
57 desc->status |= IRQ_DISABLED;
58 desc->handler->disable(irq);
59 }
60 spin_unlock_irqrestore(&desc->lock, flags);
61 }
63 EXPORT_SYMBOL(disable_irq_nosync);
65 /**
66 * disable_irq - disable an irq and wait for completion
67 * @irq: Interrupt to disable
68 *
69 * Disable the selected interrupt line. Enables and Disables are
70 * nested.
71 * This function waits for any pending IRQ handlers for this interrupt
72 * to complete before returning. If you use this function while
73 * holding a resource the IRQ handler may need you will deadlock.
74 *
75 * This function may be called - with care - from IRQ context.
76 */
77 void disable_irq(unsigned int irq)
78 {
79 irq_desc_t *desc = irq_desc + irq;
81 disable_irq_nosync(irq);
82 if (desc->action)
83 synchronize_irq(irq);
84 }
86 EXPORT_SYMBOL(disable_irq);
88 /**
89 * enable_irq - enable handling of an irq
90 * @irq: Interrupt to enable
91 *
92 * Undoes the effect of one call to disable_irq(). If this
93 * matches the last disable, processing of interrupts on this
94 * IRQ line is re-enabled.
95 *
96 * This function may be called from IRQ context.
97 */
98 void enable_irq(unsigned int irq)
99 {
100 irq_desc_t *desc = irq_desc + irq;
101 unsigned long flags;
103 spin_lock_irqsave(&desc->lock, flags);
104 switch (desc->depth) {
105 case 0:
106 WARN_ON(1);
107 break;
108 case 1: {
109 unsigned int status = desc->status & ~IRQ_DISABLED;
111 desc->status = status;
112 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
113 desc->status = status | IRQ_REPLAY;
114 hw_resend_irq(desc->handler,irq);
115 }
116 desc->handler->enable(irq);
117 /* fall-through */
118 }
119 default:
120 desc->depth--;
121 }
122 spin_unlock_irqrestore(&desc->lock, flags);
123 }
125 EXPORT_SYMBOL(enable_irq);
127 /*
128 * Internal function that tells the architecture code whether a
129 * particular irq has been exclusively allocated or is available
130 * for driver use.
131 */
132 int can_request_irq(unsigned int irq, unsigned long irqflags)
133 {
134 struct irqaction *action;
136 if (irq >= NR_IRQS)
137 return 0;
139 action = irq_desc[irq].action;
140 if (action)
141 if (irqflags & action->flags & SA_SHIRQ)
142 action = NULL;
144 return !action;
145 }
147 /**
148 * setup_irq - register an irqaction structure
149 * @irq: Interrupt to register
150 * @irqaction: The irqaction structure to be registered
151 *
152 * Normally called by request_irq, this function can be used
153 * directly to allocate special interrupts that are part of the
154 * architecture.
155 */
156 int setup_irq(unsigned int irq, struct irqaction * new)
157 {
158 struct irq_desc *desc = irq_desc + irq;
159 struct irqaction *old, **p;
160 unsigned long flags;
161 int shared = 0;
163 if (desc->handler == &no_irq_type)
164 return -ENOSYS;
165 /*
166 * Some drivers like serial.c use request_irq() heavily,
167 * so we have to be careful not to interfere with a
168 * running system.
169 */
170 if (new->flags & SA_SAMPLE_RANDOM) {
171 /*
172 * This function might sleep, we want to call it first,
173 * outside of the atomic block.
174 * Yes, this might clear the entropy pool if the wrong
175 * driver is attempted to be loaded, without actually
176 * installing a new handler, but is this really a problem,
177 * only the sysadmin is able to do this.
178 */
179 rand_initialize_irq(irq);
180 }
182 /*
183 * The following block of code has to be executed atomically
184 */
185 spin_lock_irqsave(&desc->lock,flags);
186 p = &desc->action;
187 if ((old = *p) != NULL) {
188 /* Can't share interrupts unless both agree to */
189 if (!(old->flags & new->flags & SA_SHIRQ)) {
190 spin_unlock_irqrestore(&desc->lock,flags);
191 return -EBUSY;
192 }
194 /* add new interrupt at end of irq queue */
195 do {
196 p = &old->next;
197 old = *p;
198 } while (old);
199 shared = 1;
200 }
202 *p = new;
204 if (!shared) {
205 desc->depth = 0;
206 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT |
207 IRQ_WAITING | IRQ_INPROGRESS);
208 if (desc->handler->startup)
209 desc->handler->startup(irq);
210 else
211 desc->handler->enable(irq);
212 }
213 spin_unlock_irqrestore(&desc->lock,flags);
215 new->irq = irq;
216 register_irq_proc(irq);
217 new->dir = NULL;
218 register_handler_proc(irq, new);
220 return 0;
221 }
223 /*
224 * teardown_irq - unregister an irqaction
225 * @irq: Interrupt line being freed
226 * @old: Pointer to the irqaction that is to be unregistered
227 *
228 * This function is called by free_irq and does the actual
229 * business of unregistering the handler. It exists as a
230 * seperate function to enable handlers to be unregistered
231 * for irqactions that have been allocated statically at
232 * boot time.
233 *
234 * This function must not be called from interrupt context.
235 */
236 int teardown_irq(unsigned int irq, struct irqaction * old)
237 {
238 struct irq_desc *desc;
239 struct irqaction **p;
240 unsigned long flags;
242 if (irq >= NR_IRQS)
243 return -ENOENT;
245 desc = irq_desc + irq;
246 spin_lock_irqsave(&desc->lock,flags);
247 p = &desc->action;
248 for (;;) {
249 struct irqaction * action = *p;
251 if (action) {
252 struct irqaction **pp = p;
254 p = &action->next;
255 if (action != old)
256 continue;
258 /* Found it - now remove it from the list of entries */
259 *pp = action->next;
260 if (!desc->action) {
261 desc->status |= IRQ_DISABLED;
262 if (desc->handler->shutdown)
263 desc->handler->shutdown(irq);
264 else
265 desc->handler->disable(irq);
266 }
267 spin_unlock_irqrestore(&desc->lock,flags);
268 unregister_handler_proc(irq, action);
270 /* Make sure it's not being used on another CPU */
271 synchronize_irq(irq);
272 return 0;
273 }
274 printk(KERN_ERR "Trying to teardown free IRQ%d\n",irq);
275 spin_unlock_irqrestore(&desc->lock,flags);
276 return -ENOENT;
277 }
278 }
280 /**
281 * free_irq - free an interrupt
282 * @irq: Interrupt line to free
283 * @dev_id: Device identity to free
284 *
285 * Remove an interrupt handler. The handler is removed and if the
286 * interrupt line is no longer in use by any driver it is disabled.
287 * On a shared IRQ the caller must ensure the interrupt is disabled
288 * on the card it drives before calling this function. The function
289 * does not return until any executing interrupts for this IRQ
290 * have completed.
291 *
292 * This function must not be called from interrupt context.
293 */
294 void free_irq(unsigned int irq, void *dev_id)
295 {
296 struct irq_desc *desc;
297 struct irqaction *action;
298 unsigned long flags;
300 if (irq >= NR_IRQS)
301 return;
303 desc = irq_desc + irq;
304 spin_lock_irqsave(&desc->lock,flags);
305 for (action = desc->action; action != NULL; action = action->next) {
306 if (action->dev_id != dev_id)
307 continue;
309 spin_unlock_irqrestore(&desc->lock,flags);
311 if (teardown_irq(irq, action) == 0)
312 kfree(action);
313 return;
314 }
315 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
316 spin_unlock_irqrestore(&desc->lock,flags);
317 return;
318 }
320 EXPORT_SYMBOL(free_irq);
322 /**
323 * request_irq - allocate an interrupt line
324 * @irq: Interrupt line to allocate
325 * @handler: Function to be called when the IRQ occurs
326 * @irqflags: Interrupt type flags
327 * @devname: An ascii name for the claiming device
328 * @dev_id: A cookie passed back to the handler function
329 *
330 * This call allocates interrupt resources and enables the
331 * interrupt line and IRQ handling. From the point this
332 * call is made your handler function may be invoked. Since
333 * your handler function must clear any interrupt the board
334 * raises, you must take care both to initialise your hardware
335 * and to set up the interrupt handler in the right order.
336 *
337 * Dev_id must be globally unique. Normally the address of the
338 * device data structure is used as the cookie. Since the handler
339 * receives this value it makes sense to use it.
340 *
341 * If your interrupt is shared you must pass a non NULL dev_id
342 * as this is required when freeing the interrupt.
343 *
344 * Flags:
345 *
346 * SA_SHIRQ Interrupt is shared
347 * SA_INTERRUPT Disable local interrupts while processing
348 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
349 *
350 */
351 int request_irq(unsigned int irq,
352 irqreturn_t (*handler)(int, void *, struct pt_regs *),
353 unsigned long irqflags, const char * devname, void *dev_id)
354 {
355 struct irqaction * action;
356 int retval;
358 /*
359 * Sanity-check: shared interrupts must pass in a real dev-ID,
360 * otherwise we'll have trouble later trying to figure out
361 * which interrupt is which (messes up the interrupt freeing
362 * logic etc).
363 */
364 if ((irqflags & SA_SHIRQ) && !dev_id)
365 return -EINVAL;
366 if (irq >= NR_IRQS)
367 return -EINVAL;
368 if (!handler)
369 return -EINVAL;
371 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
372 if (!action)
373 return -ENOMEM;
375 action->handler = handler;
376 action->flags = irqflags;
377 cpus_clear(action->mask);
378 action->name = devname;
379 action->next = NULL;
380 action->dev_id = dev_id;
382 retval = setup_irq(irq, action);
383 if (retval)
384 kfree(action);
386 return retval;
387 }
389 EXPORT_SYMBOL(request_irq);