ia64/xen-unstable

view linux-2.6-xen-sparse/kernel/irq/manage.c @ 6538:84ee014ebd41

Merge xen-vtx-unstable.hg
author adsharma@los-vmm.sc.intel.com
date Wed Aug 17 12:34:38 2005 -0800 (2005-08-17)
parents 23979fb12c49 f294acb25858
children 99914b54f7bf
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 cpumask_t irq_affinity[NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
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;
33 while (desc->status & IRQ_INPROGRESS)
34 cpu_relax();
35 }
37 EXPORT_SYMBOL(synchronize_irq);
39 #endif
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;
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 }
65 EXPORT_SYMBOL(disable_irq_nosync);
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;
83 disable_irq_nosync(irq);
84 if (desc->action)
85 synchronize_irq(irq);
86 }
88 EXPORT_SYMBOL(disable_irq);
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;
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;
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 }
127 EXPORT_SYMBOL(enable_irq);
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;
138 if (irq >= NR_IRQS)
139 return 0;
141 action = irq_desc[irq].action;
142 if (action)
143 if (irqflags & action->flags & SA_SHIRQ)
144 action = NULL;
146 return !action;
147 }
149 /**
150 * setup_irq - register an irqaction structure
151 * @irq: Interrupt to register
152 * @irqaction: The irqaction structure to be registered
153 *
154 * Normally called by request_irq, this function can be used
155 * directly to allocate special interrupts that are part of the
156 * architecture.
157 */
158 int setup_irq(unsigned int irq, struct irqaction * new)
159 {
160 struct irq_desc *desc = irq_desc + irq;
161 struct irqaction *old, **p;
162 unsigned long flags;
163 int shared = 0;
165 if (desc->handler == &no_irq_type)
166 return -ENOSYS;
167 /*
168 * Some drivers like serial.c use request_irq() heavily,
169 * so we have to be careful not to interfere with a
170 * running system.
171 */
172 if (new->flags & SA_SAMPLE_RANDOM) {
173 /*
174 * This function might sleep, we want to call it first,
175 * outside of the atomic block.
176 * Yes, this might clear the entropy pool if the wrong
177 * driver is attempted to be loaded, without actually
178 * installing a new handler, but is this really a problem,
179 * only the sysadmin is able to do this.
180 */
181 rand_initialize_irq(irq);
182 }
184 /*
185 * The following block of code has to be executed atomically
186 */
187 spin_lock_irqsave(&desc->lock,flags);
188 p = &desc->action;
189 if ((old = *p) != NULL) {
190 /* Can't share interrupts unless both agree to */
191 if (!(old->flags & new->flags & SA_SHIRQ)) {
192 spin_unlock_irqrestore(&desc->lock,flags);
193 return -EBUSY;
194 }
196 /* add new interrupt at end of irq queue */
197 do {
198 p = &old->next;
199 old = *p;
200 } while (old);
201 shared = 1;
202 }
204 *p = new;
206 if (!shared) {
207 desc->depth = 0;
208 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT |
209 IRQ_WAITING | IRQ_INPROGRESS);
210 if (desc->handler->startup)
211 desc->handler->startup(irq);
212 else
213 desc->handler->enable(irq);
214 }
215 spin_unlock_irqrestore(&desc->lock,flags);
217 new->irq = irq;
218 register_irq_proc(irq);
219 new->dir = NULL;
220 register_handler_proc(irq, new);
222 return 0;
223 }
225 /*
226 * teardown_irq - unregister an irqaction
227 * @irq: Interrupt line being freed
228 * @old: Pointer to the irqaction that is to be unregistered
229 *
230 * This function is called by free_irq and does the actual
231 * business of unregistering the handler. It exists as a
232 * seperate function to enable handlers to be unregistered
233 * for irqactions that have been allocated statically at
234 * boot time.
235 *
236 * This function must not be called from interrupt context.
237 */
238 int teardown_irq(unsigned int irq, struct irqaction * old)
239 {
240 struct irq_desc *desc;
241 struct irqaction **p;
242 unsigned long flags;
244 if (irq >= NR_IRQS)
245 return -ENOENT;
247 desc = irq_desc + irq;
248 spin_lock_irqsave(&desc->lock,flags);
249 p = &desc->action;
250 for (;;) {
251 struct irqaction * action = *p;
253 if (action) {
254 struct irqaction **pp = p;
256 p = &action->next;
257 if (action != old)
258 continue;
260 /* Found it - now remove it from the list of entries */
261 *pp = action->next;
262 if (!desc->action) {
263 desc->status |= IRQ_DISABLED;
264 if (desc->handler->shutdown)
265 desc->handler->shutdown(irq);
266 else
267 desc->handler->disable(irq);
268 }
269 spin_unlock_irqrestore(&desc->lock,flags);
270 unregister_handler_proc(irq, action);
272 /* Make sure it's not being used on another CPU */
273 synchronize_irq(irq);
274 return 0;
275 }
276 printk(KERN_ERR "Trying to teardown free IRQ%d\n",irq);
277 spin_unlock_irqrestore(&desc->lock,flags);
278 return -ENOENT;
279 }
280 }
282 /**
283 * free_irq - free an interrupt
284 * @irq: Interrupt line to free
285 * @dev_id: Device identity to free
286 *
287 * Remove an interrupt handler. The handler is removed and if the
288 * interrupt line is no longer in use by any driver it is disabled.
289 * On a shared IRQ the caller must ensure the interrupt is disabled
290 * on the card it drives before calling this function. The function
291 * does not return until any executing interrupts for this IRQ
292 * have completed.
293 *
294 * This function must not be called from interrupt context.
295 */
296 void free_irq(unsigned int irq, void *dev_id)
297 {
298 struct irq_desc *desc;
299 struct irqaction *action;
300 unsigned long flags;
302 if (irq >= NR_IRQS)
303 return;
305 desc = irq_desc + irq;
306 spin_lock_irqsave(&desc->lock,flags);
307 for (action = desc->action; action != NULL; action = action->next) {
308 if (action->dev_id != dev_id)
309 continue;
311 spin_unlock_irqrestore(&desc->lock,flags);
313 if (teardown_irq(irq, action) == 0)
314 kfree(action);
315 return;
316 }
317 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
318 spin_unlock_irqrestore(&desc->lock,flags);
319 return;
320 }
322 EXPORT_SYMBOL(free_irq);
324 /**
325 * request_irq - allocate an interrupt line
326 * @irq: Interrupt line to allocate
327 * @handler: Function to be called when the IRQ occurs
328 * @irqflags: Interrupt type flags
329 * @devname: An ascii name for the claiming device
330 * @dev_id: A cookie passed back to the handler function
331 *
332 * This call allocates interrupt resources and enables the
333 * interrupt line and IRQ handling. From the point this
334 * call is made your handler function may be invoked. Since
335 * your handler function must clear any interrupt the board
336 * raises, you must take care both to initialise your hardware
337 * and to set up the interrupt handler in the right order.
338 *
339 * Dev_id must be globally unique. Normally the address of the
340 * device data structure is used as the cookie. Since the handler
341 * receives this value it makes sense to use it.
342 *
343 * If your interrupt is shared you must pass a non NULL dev_id
344 * as this is required when freeing the interrupt.
345 *
346 * Flags:
347 *
348 * SA_SHIRQ Interrupt is shared
349 * SA_INTERRUPT Disable local interrupts while processing
350 * SA_SAMPLE_RANDOM The interrupt can be used for entropy
351 *
352 */
353 int request_irq(unsigned int irq,
354 irqreturn_t (*handler)(int, void *, struct pt_regs *),
355 unsigned long irqflags, const char * devname, void *dev_id)
356 {
357 struct irqaction * action;
358 int retval;
360 /*
361 * Sanity-check: shared interrupts must pass in a real dev-ID,
362 * otherwise we'll have trouble later trying to figure out
363 * which interrupt is which (messes up the interrupt freeing
364 * logic etc).
365 */
366 if ((irqflags & SA_SHIRQ) && !dev_id)
367 return -EINVAL;
368 if (irq >= NR_IRQS)
369 return -EINVAL;
370 if (!handler)
371 return -EINVAL;
373 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
374 if (!action)
375 return -ENOMEM;
377 action->handler = handler;
378 action->flags = irqflags;
379 cpus_clear(action->mask);
380 action->name = devname;
381 action->next = NULL;
382 action->dev_id = dev_id;
384 retval = setup_irq(irq, action);
385 if (retval)
386 kfree(action);
388 return retval;
389 }
391 EXPORT_SYMBOL(request_irq);