This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.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 /**
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;
30
31         while (desc->status & IRQ_INPROGRESS)
32                 cpu_relax();
33 }
34
35 EXPORT_SYMBOL(synchronize_irq);
36
37 #endif
38
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;
54
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 }
62
63 EXPORT_SYMBOL(disable_irq_nosync);
64
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;
80
81         disable_irq_nosync(irq);
82         if (desc->action)
83                 synchronize_irq(irq);
84 }
85
86 EXPORT_SYMBOL(disable_irq);
87
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;
102
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;
110
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 }
124
125 EXPORT_SYMBOL(enable_irq);
126
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;
135
136         if (irq >= NR_IRQS)
137                 return 0;
138
139         action = irq_desc[irq].action;
140         if (action)
141                 if (irqflags & action->flags & SA_SHIRQ)
142                         action = NULL;
143
144         return !action;
145 }
146
147 /*
148  * Internal function to register an irqaction - typically used to
149  * allocate special interrupts that are part of the architecture.
150  */
151 int setup_irq(unsigned int irq, struct irqaction * new)
152 {
153         struct irq_desc *desc = irq_desc + irq;
154         struct irqaction *old, **p;
155         unsigned long flags;
156         int shared = 0;
157
158         if (desc->handler == &no_irq_type)
159                 return -ENOSYS;
160         /*
161          * Some drivers like serial.c use request_irq() heavily,
162          * so we have to be careful not to interfere with a
163          * running system.
164          */
165         if (new->flags & SA_SAMPLE_RANDOM) {
166                 /*
167                  * This function might sleep, we want to call it first,
168                  * outside of the atomic block.
169                  * Yes, this might clear the entropy pool if the wrong
170                  * driver is attempted to be loaded, without actually
171                  * installing a new handler, but is this really a problem,
172                  * only the sysadmin is able to do this.
173                  */
174                 rand_initialize_irq(irq);
175         }
176
177         /*
178          * The following block of code has to be executed atomically
179          */
180         spin_lock_irqsave(&desc->lock,flags);
181         p = &desc->action;
182         if ((old = *p) != NULL) {
183                 /* Can't share interrupts unless both agree to */
184                 if (!(old->flags & new->flags & SA_SHIRQ)) {
185                         spin_unlock_irqrestore(&desc->lock,flags);
186                         return -EBUSY;
187                 }
188
189                 /* add new interrupt at end of irq queue */
190                 do {
191                         p = &old->next;
192                         old = *p;
193                 } while (old);
194                 shared = 1;
195         }
196
197         *p = new;
198
199         if (!shared) {
200                 desc->depth = 0;
201                 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT |
202                                   IRQ_WAITING | IRQ_INPROGRESS);
203                 if (desc->handler->startup)
204                         desc->handler->startup(irq);
205                 else
206                         desc->handler->enable(irq);
207         }
208         spin_unlock_irqrestore(&desc->lock,flags);
209
210         new->irq = irq;
211         register_irq_proc(irq);
212         new->dir = NULL;
213         register_handler_proc(irq, new);
214
215         return 0;
216 }
217
218 /**
219  *      free_irq - free an interrupt
220  *      @irq: Interrupt line to free
221  *      @dev_id: Device identity to free
222  *
223  *      Remove an interrupt handler. The handler is removed and if the
224  *      interrupt line is no longer in use by any driver it is disabled.
225  *      On a shared IRQ the caller must ensure the interrupt is disabled
226  *      on the card it drives before calling this function. The function
227  *      does not return until any executing interrupts for this IRQ
228  *      have completed.
229  *
230  *      This function must not be called from interrupt context.
231  */
232 void free_irq(unsigned int irq, void *dev_id)
233 {
234         struct irq_desc *desc;
235         struct irqaction **p;
236         unsigned long flags;
237
238         if (irq >= NR_IRQS)
239                 return;
240
241         desc = irq_desc + irq;
242         spin_lock_irqsave(&desc->lock,flags);
243         p = &desc->action;
244         for (;;) {
245                 struct irqaction * action = *p;
246
247                 if (action) {
248                         struct irqaction **pp = p;
249
250                         p = &action->next;
251                         if (action->dev_id != dev_id)
252                                 continue;
253
254                         /* Found it - now remove it from the list of entries */
255                         *pp = action->next;
256                         if (!desc->action) {
257                                 desc->status |= IRQ_DISABLED;
258                                 if (desc->handler->shutdown)
259                                         desc->handler->shutdown(irq);
260                                 else
261                                         desc->handler->disable(irq);
262                         }
263                         spin_unlock_irqrestore(&desc->lock,flags);
264                         unregister_handler_proc(irq, action);
265
266                         /* Make sure it's not being used on another CPU */
267                         synchronize_irq(irq);
268                         kfree(action);
269                         return;
270                 }
271                 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
272                 spin_unlock_irqrestore(&desc->lock,flags);
273                 return;
274         }
275 }
276
277 EXPORT_SYMBOL(free_irq);
278
279 /**
280  *      request_irq - allocate an interrupt line
281  *      @irq: Interrupt line to allocate
282  *      @handler: Function to be called when the IRQ occurs
283  *      @irqflags: Interrupt type flags
284  *      @devname: An ascii name for the claiming device
285  *      @dev_id: A cookie passed back to the handler function
286  *
287  *      This call allocates interrupt resources and enables the
288  *      interrupt line and IRQ handling. From the point this
289  *      call is made your handler function may be invoked. Since
290  *      your handler function must clear any interrupt the board
291  *      raises, you must take care both to initialise your hardware
292  *      and to set up the interrupt handler in the right order.
293  *
294  *      Dev_id must be globally unique. Normally the address of the
295  *      device data structure is used as the cookie. Since the handler
296  *      receives this value it makes sense to use it.
297  *
298  *      If your interrupt is shared you must pass a non NULL dev_id
299  *      as this is required when freeing the interrupt.
300  *
301  *      Flags:
302  *
303  *      SA_SHIRQ                Interrupt is shared
304  *      SA_INTERRUPT            Disable local interrupts while processing
305  *      SA_SAMPLE_RANDOM        The interrupt can be used for entropy
306  *
307  */
308 int request_irq(unsigned int irq,
309                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
310                 unsigned long irqflags, const char * devname, void *dev_id)
311 {
312         struct irqaction * action;
313         int retval;
314
315         /*
316          * Sanity-check: shared interrupts must pass in a real dev-ID,
317          * otherwise we'll have trouble later trying to figure out
318          * which interrupt is which (messes up the interrupt freeing
319          * logic etc).
320          */
321         if ((irqflags & SA_SHIRQ) && !dev_id)
322                 return -EINVAL;
323         if (irq >= NR_IRQS)
324                 return -EINVAL;
325         if (!handler)
326                 return -EINVAL;
327
328         action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
329         if (!action)
330                 return -ENOMEM;
331
332         action->handler = handler;
333         action->flags = irqflags;
334         cpus_clear(action->mask);
335         action->name = devname;
336         action->next = NULL;
337         action->dev_id = dev_id;
338
339         retval = setup_irq(irq, action);
340         if (retval)
341                 kfree(action);
342
343         return retval;
344 }
345
346 EXPORT_SYMBOL(request_irq);
347