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