Merge branch 'master' into upstream
[sfrench/cifs-2.6.git] / kernel / irq / chip.c
1 /*
2  * linux/kernel/irq/chip.c
3  *
4  * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5  * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
6  *
7  * This file contains the core interrupt handling code, for irq-chip
8  * based architectures.
9  *
10  * Detailed information is available in Documentation/DocBook/genericirq
11  */
12
13 #include <linux/irq.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel_stat.h>
17
18 #include "internals.h"
19
20 /**
21  *      set_irq_chip - set the irq chip for an irq
22  *      @irq:   irq number
23  *      @chip:  pointer to irq chip description structure
24  */
25 int set_irq_chip(unsigned int irq, struct irq_chip *chip)
26 {
27         struct irq_desc *desc;
28         unsigned long flags;
29
30         if (irq >= NR_IRQS) {
31                 printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
32                 WARN_ON(1);
33                 return -EINVAL;
34         }
35
36         if (!chip)
37                 chip = &no_irq_chip;
38
39         desc = irq_desc + irq;
40         spin_lock_irqsave(&desc->lock, flags);
41         irq_chip_set_defaults(chip);
42         desc->chip = chip;
43         spin_unlock_irqrestore(&desc->lock, flags);
44
45         return 0;
46 }
47 EXPORT_SYMBOL(set_irq_chip);
48
49 /**
50  *      set_irq_type - set the irq type for an irq
51  *      @irq:   irq number
52  *      @type:  interrupt type - see include/linux/interrupt.h
53  */
54 int set_irq_type(unsigned int irq, unsigned int type)
55 {
56         struct irq_desc *desc;
57         unsigned long flags;
58         int ret = -ENXIO;
59
60         if (irq >= NR_IRQS) {
61                 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
62                 return -ENODEV;
63         }
64
65         desc = irq_desc + irq;
66         if (desc->chip->set_type) {
67                 spin_lock_irqsave(&desc->lock, flags);
68                 ret = desc->chip->set_type(irq, type);
69                 spin_unlock_irqrestore(&desc->lock, flags);
70         }
71         return ret;
72 }
73 EXPORT_SYMBOL(set_irq_type);
74
75 /**
76  *      set_irq_data - set irq type data for an irq
77  *      @irq:   Interrupt number
78  *      @data:  Pointer to interrupt specific data
79  *
80  *      Set the hardware irq controller data for an irq
81  */
82 int set_irq_data(unsigned int irq, void *data)
83 {
84         struct irq_desc *desc;
85         unsigned long flags;
86
87         if (irq >= NR_IRQS) {
88                 printk(KERN_ERR
89                        "Trying to install controller data for IRQ%d\n", irq);
90                 return -EINVAL;
91         }
92
93         desc = irq_desc + irq;
94         spin_lock_irqsave(&desc->lock, flags);
95         desc->handler_data = data;
96         spin_unlock_irqrestore(&desc->lock, flags);
97         return 0;
98 }
99 EXPORT_SYMBOL(set_irq_data);
100
101 /**
102  *      set_irq_chip_data - set irq chip data for an irq
103  *      @irq:   Interrupt number
104  *      @data:  Pointer to chip specific data
105  *
106  *      Set the hardware irq chip data for an irq
107  */
108 int set_irq_chip_data(unsigned int irq, void *data)
109 {
110         struct irq_desc *desc = irq_desc + irq;
111         unsigned long flags;
112
113         if (irq >= NR_IRQS || !desc->chip) {
114                 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
115                 return -EINVAL;
116         }
117
118         spin_lock_irqsave(&desc->lock, flags);
119         desc->chip_data = data;
120         spin_unlock_irqrestore(&desc->lock, flags);
121
122         return 0;
123 }
124 EXPORT_SYMBOL(set_irq_chip_data);
125
126 /*
127  * default enable function
128  */
129 static void default_enable(unsigned int irq)
130 {
131         struct irq_desc *desc = irq_desc + irq;
132
133         desc->chip->unmask(irq);
134         desc->status &= ~IRQ_MASKED;
135 }
136
137 /*
138  * default disable function
139  */
140 static void default_disable(unsigned int irq)
141 {
142         struct irq_desc *desc = irq_desc + irq;
143
144         if (!(desc->status & IRQ_DELAYED_DISABLE))
145                 desc->chip->mask(irq);
146 }
147
148 /*
149  * default startup function
150  */
151 static unsigned int default_startup(unsigned int irq)
152 {
153         irq_desc[irq].chip->enable(irq);
154
155         return 0;
156 }
157
158 /*
159  * Fixup enable/disable function pointers
160  */
161 void irq_chip_set_defaults(struct irq_chip *chip)
162 {
163         if (!chip->enable)
164                 chip->enable = default_enable;
165         if (!chip->disable)
166                 chip->disable = default_disable;
167         if (!chip->startup)
168                 chip->startup = default_startup;
169         if (!chip->shutdown)
170                 chip->shutdown = chip->disable;
171         if (!chip->name)
172                 chip->name = chip->typename;
173 }
174
175 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
176 {
177         if (desc->chip->mask_ack)
178                 desc->chip->mask_ack(irq);
179         else {
180                 desc->chip->mask(irq);
181                 desc->chip->ack(irq);
182         }
183 }
184
185 /**
186  *      handle_simple_irq - Simple and software-decoded IRQs.
187  *      @irq:   the interrupt number
188  *      @desc:  the interrupt description structure for this irq
189  *      @regs:  pointer to a register structure
190  *
191  *      Simple interrupts are either sent from a demultiplexing interrupt
192  *      handler or come from hardware, where no interrupt hardware control
193  *      is necessary.
194  *
195  *      Note: The caller is expected to handle the ack, clear, mask and
196  *      unmask issues if necessary.
197  */
198 void fastcall
199 handle_simple_irq(unsigned int irq, struct irq_desc *desc, struct pt_regs *regs)
200 {
201         struct irqaction *action;
202         irqreturn_t action_ret;
203         const unsigned int cpu = smp_processor_id();
204
205         spin_lock(&desc->lock);
206
207         if (unlikely(desc->status & IRQ_INPROGRESS))
208                 goto out_unlock;
209         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
210         kstat_cpu(cpu).irqs[irq]++;
211
212         action = desc->action;
213         if (unlikely(!action || (desc->status & IRQ_DISABLED)))
214                 goto out_unlock;
215
216         desc->status |= IRQ_INPROGRESS;
217         spin_unlock(&desc->lock);
218
219         action_ret = handle_IRQ_event(irq, regs, action);
220         if (!noirqdebug)
221                 note_interrupt(irq, desc, action_ret, regs);
222
223         spin_lock(&desc->lock);
224         desc->status &= ~IRQ_INPROGRESS;
225 out_unlock:
226         spin_unlock(&desc->lock);
227 }
228
229 /**
230  *      handle_level_irq - Level type irq handler
231  *      @irq:   the interrupt number
232  *      @desc:  the interrupt description structure for this irq
233  *      @regs:  pointer to a register structure
234  *
235  *      Level type interrupts are active as long as the hardware line has
236  *      the active level. This may require to mask the interrupt and unmask
237  *      it after the associated handler has acknowledged the device, so the
238  *      interrupt line is back to inactive.
239  */
240 void fastcall
241 handle_level_irq(unsigned int irq, struct irq_desc *desc, struct pt_regs *regs)
242 {
243         unsigned int cpu = smp_processor_id();
244         struct irqaction *action;
245         irqreturn_t action_ret;
246
247         spin_lock(&desc->lock);
248         mask_ack_irq(desc, irq);
249
250         if (unlikely(desc->status & IRQ_INPROGRESS))
251                 goto out_unlock;
252         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
253         kstat_cpu(cpu).irqs[irq]++;
254
255         /*
256          * If its disabled or no action available
257          * keep it masked and get out of here
258          */
259         action = desc->action;
260         if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
261                 desc->status |= IRQ_PENDING;
262                 goto out_unlock;
263         }
264
265         desc->status |= IRQ_INPROGRESS;
266         desc->status &= ~IRQ_PENDING;
267         spin_unlock(&desc->lock);
268
269         action_ret = handle_IRQ_event(irq, regs, action);
270         if (!noirqdebug)
271                 note_interrupt(irq, desc, action_ret, regs);
272
273         spin_lock(&desc->lock);
274         desc->status &= ~IRQ_INPROGRESS;
275         if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
276                 desc->chip->unmask(irq);
277 out_unlock:
278         spin_unlock(&desc->lock);
279 }
280
281 /**
282  *      handle_fasteoi_irq - irq handler for transparent controllers
283  *      @irq:   the interrupt number
284  *      @desc:  the interrupt description structure for this irq
285  *      @regs:  pointer to a register structure
286  *
287  *      Only a single callback will be issued to the chip: an ->eoi()
288  *      call when the interrupt has been serviced. This enables support
289  *      for modern forms of interrupt handlers, which handle the flow
290  *      details in hardware, transparently.
291  */
292 void fastcall
293 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc,
294                    struct pt_regs *regs)
295 {
296         unsigned int cpu = smp_processor_id();
297         struct irqaction *action;
298         irqreturn_t action_ret;
299
300         spin_lock(&desc->lock);
301
302         if (unlikely(desc->status & IRQ_INPROGRESS))
303                 goto out;
304
305         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
306         kstat_cpu(cpu).irqs[irq]++;
307
308         /*
309          * If its disabled or no action available
310          * keep it masked and get out of here
311          */
312         action = desc->action;
313         if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
314                 desc->status |= IRQ_PENDING;
315                 goto out;
316         }
317
318         desc->status |= IRQ_INPROGRESS;
319         desc->status &= ~IRQ_PENDING;
320         spin_unlock(&desc->lock);
321
322         action_ret = handle_IRQ_event(irq, regs, action);
323         if (!noirqdebug)
324                 note_interrupt(irq, desc, action_ret, regs);
325
326         spin_lock(&desc->lock);
327         desc->status &= ~IRQ_INPROGRESS;
328 out:
329         desc->chip->eoi(irq);
330
331         spin_unlock(&desc->lock);
332 }
333
334 /**
335  *      handle_edge_irq - edge type IRQ handler
336  *      @irq:   the interrupt number
337  *      @desc:  the interrupt description structure for this irq
338  *      @regs:  pointer to a register structure
339  *
340  *      Interrupt occures on the falling and/or rising edge of a hardware
341  *      signal. The occurence is latched into the irq controller hardware
342  *      and must be acked in order to be reenabled. After the ack another
343  *      interrupt can happen on the same source even before the first one
344  *      is handled by the assosiacted event handler. If this happens it
345  *      might be necessary to disable (mask) the interrupt depending on the
346  *      controller hardware. This requires to reenable the interrupt inside
347  *      of the loop which handles the interrupts which have arrived while
348  *      the handler was running. If all pending interrupts are handled, the
349  *      loop is left.
350  */
351 void fastcall
352 handle_edge_irq(unsigned int irq, struct irq_desc *desc, struct pt_regs *regs)
353 {
354         const unsigned int cpu = smp_processor_id();
355
356         spin_lock(&desc->lock);
357
358         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
359
360         /*
361          * If we're currently running this IRQ, or its disabled,
362          * we shouldn't process the IRQ. Mark it pending, handle
363          * the necessary masking and go out
364          */
365         if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
366                     !desc->action)) {
367                 desc->status |= (IRQ_PENDING | IRQ_MASKED);
368                 mask_ack_irq(desc, irq);
369                 goto out_unlock;
370         }
371
372         kstat_cpu(cpu).irqs[irq]++;
373
374         /* Start handling the irq */
375         desc->chip->ack(irq);
376
377         /* Mark the IRQ currently in progress.*/
378         desc->status |= IRQ_INPROGRESS;
379
380         do {
381                 struct irqaction *action = desc->action;
382                 irqreturn_t action_ret;
383
384                 if (unlikely(!action)) {
385                         desc->chip->mask(irq);
386                         goto out_unlock;
387                 }
388
389                 /*
390                  * When another irq arrived while we were handling
391                  * one, we could have masked the irq.
392                  * Renable it, if it was not disabled in meantime.
393                  */
394                 if (unlikely((desc->status &
395                                (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
396                               (IRQ_PENDING | IRQ_MASKED))) {
397                         desc->chip->unmask(irq);
398                         desc->status &= ~IRQ_MASKED;
399                 }
400
401                 desc->status &= ~IRQ_PENDING;
402                 spin_unlock(&desc->lock);
403                 action_ret = handle_IRQ_event(irq, regs, action);
404                 if (!noirqdebug)
405                         note_interrupt(irq, desc, action_ret, regs);
406                 spin_lock(&desc->lock);
407
408         } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
409
410         desc->status &= ~IRQ_INPROGRESS;
411 out_unlock:
412         spin_unlock(&desc->lock);
413 }
414
415 #ifdef CONFIG_SMP
416 /**
417  *      handle_percpu_IRQ - Per CPU local irq handler
418  *      @irq:   the interrupt number
419  *      @desc:  the interrupt description structure for this irq
420  *      @regs:  pointer to a register structure
421  *
422  *      Per CPU interrupts on SMP machines without locking requirements
423  */
424 void fastcall
425 handle_percpu_irq(unsigned int irq, struct irq_desc *desc, struct pt_regs *regs)
426 {
427         irqreturn_t action_ret;
428
429         kstat_this_cpu.irqs[irq]++;
430
431         if (desc->chip->ack)
432                 desc->chip->ack(irq);
433
434         action_ret = handle_IRQ_event(irq, regs, desc->action);
435         if (!noirqdebug)
436                 note_interrupt(irq, desc, action_ret, regs);
437
438         if (desc->chip->eoi)
439                 desc->chip->eoi(irq);
440 }
441
442 #endif /* CONFIG_SMP */
443
444 void
445 __set_irq_handler(unsigned int irq,
446                   void fastcall (*handle)(unsigned int, irq_desc_t *,
447                                           struct pt_regs *),
448                   int is_chained)
449 {
450         struct irq_desc *desc;
451         unsigned long flags;
452
453         if (irq >= NR_IRQS) {
454                 printk(KERN_ERR
455                        "Trying to install type control for IRQ%d\n", irq);
456                 return;
457         }
458
459         desc = irq_desc + irq;
460
461         if (!handle)
462                 handle = handle_bad_irq;
463
464         if (desc->chip == &no_irq_chip) {
465                 printk(KERN_WARNING "Trying to install %sinterrupt handler "
466                        "for IRQ%d\n", is_chained ? "chained " : " ", irq);
467                 /*
468                  * Some ARM implementations install a handler for really dumb
469                  * interrupt hardware without setting an irq_chip. This worked
470                  * with the ARM no_irq_chip but the check in setup_irq would
471                  * prevent us to setup the interrupt at all. Switch it to
472                  * dummy_irq_chip for easy transition.
473                  */
474                 desc->chip = &dummy_irq_chip;
475         }
476
477         spin_lock_irqsave(&desc->lock, flags);
478
479         /* Uninstall? */
480         if (handle == handle_bad_irq) {
481                 if (desc->chip != &no_irq_chip) {
482                         desc->chip->mask(irq);
483                         desc->chip->ack(irq);
484                 }
485                 desc->status |= IRQ_DISABLED;
486                 desc->depth = 1;
487         }
488         desc->handle_irq = handle;
489
490         if (handle != handle_bad_irq && is_chained) {
491                 desc->status &= ~IRQ_DISABLED;
492                 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
493                 desc->depth = 0;
494                 desc->chip->unmask(irq);
495         }
496         spin_unlock_irqrestore(&desc->lock, flags);
497 }
498
499 void
500 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
501                          void fastcall (*handle)(unsigned int,
502                                                  struct irq_desc *,
503                                                  struct pt_regs *))
504 {
505         set_irq_chip(irq, chip);
506         __set_irq_handler(irq, handle, 0);
507 }
508
509 /*
510  * Get a descriptive string for the highlevel handler, for
511  * /proc/interrupts output:
512  */
513 const char *
514 handle_irq_name(void fastcall (*handle)(unsigned int, struct irq_desc *,
515                                         struct pt_regs *))
516 {
517         if (handle == handle_level_irq)
518                 return "level  ";
519         if (handle == handle_fasteoi_irq)
520                 return "fasteoi";
521         if (handle == handle_edge_irq)
522                 return "edge   ";
523         if (handle == handle_simple_irq)
524                 return "simple ";
525 #ifdef CONFIG_SMP
526         if (handle == handle_percpu_irq)
527                 return "percpu ";
528 #endif
529         if (handle == handle_bad_irq)
530                 return "bad    ";
531
532         return NULL;
533 }