76c225cf4b263ee60bfb88c8dca468312daa7133
[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/msi.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel_stat.h>
18
19 #include "internals.h"
20
21 /**
22  *      dynamic_irq_init - initialize a dynamically allocated irq
23  *      @irq:   irq number to initialize
24  */
25 void dynamic_irq_init(unsigned int irq)
26 {
27         struct irq_desc *desc;
28         unsigned long flags;
29
30         if (irq >= nr_irqs) {
31                 WARN(1, KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
32                 return;
33         }
34
35         /* Ensure we don't have left over values from a previous use of this irq */
36         desc = irq_to_desc(irq);
37         spin_lock_irqsave(&desc->lock, flags);
38         desc->status = IRQ_DISABLED;
39         desc->chip = &no_irq_chip;
40         desc->handle_irq = handle_bad_irq;
41         desc->depth = 1;
42         desc->msi_desc = NULL;
43         desc->handler_data = NULL;
44         desc->chip_data = NULL;
45         desc->action = NULL;
46         desc->irq_count = 0;
47         desc->irqs_unhandled = 0;
48 #ifdef CONFIG_SMP
49         cpus_setall(desc->affinity);
50 #endif
51         spin_unlock_irqrestore(&desc->lock, flags);
52 }
53
54 /**
55  *      dynamic_irq_cleanup - cleanup a dynamically allocated irq
56  *      @irq:   irq number to initialize
57  */
58 void dynamic_irq_cleanup(unsigned int irq)
59 {
60         struct irq_desc *desc;
61         unsigned long flags;
62
63         if (irq >= nr_irqs) {
64                 WARN(1, KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
65                 return;
66         }
67
68         desc = irq_to_desc(irq);
69         spin_lock_irqsave(&desc->lock, flags);
70         if (desc->action) {
71                 spin_unlock_irqrestore(&desc->lock, flags);
72                 WARN(1, KERN_ERR "Destroying IRQ%d without calling free_irq\n",
73                         irq);
74                 return;
75         }
76         desc->msi_desc = NULL;
77         desc->handler_data = NULL;
78         desc->chip_data = NULL;
79         desc->handle_irq = handle_bad_irq;
80         desc->chip = &no_irq_chip;
81         spin_unlock_irqrestore(&desc->lock, flags);
82 }
83
84
85 /**
86  *      set_irq_chip - set the irq chip for an irq
87  *      @irq:   irq number
88  *      @chip:  pointer to irq chip description structure
89  */
90 int set_irq_chip(unsigned int irq, struct irq_chip *chip)
91 {
92         struct irq_desc *desc;
93         unsigned long flags;
94
95         if (irq >= nr_irqs) {
96                 WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
97                 return -EINVAL;
98         }
99
100         if (!chip)
101                 chip = &no_irq_chip;
102
103         desc = irq_to_desc(irq);
104         spin_lock_irqsave(&desc->lock, flags);
105         irq_chip_set_defaults(chip);
106         desc->chip = chip;
107         spin_unlock_irqrestore(&desc->lock, flags);
108
109         return 0;
110 }
111 EXPORT_SYMBOL(set_irq_chip);
112
113 /**
114  *      set_irq_type - set the irq trigger type for an irq
115  *      @irq:   irq number
116  *      @type:  IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
117  */
118 int set_irq_type(unsigned int irq, unsigned int type)
119 {
120         struct irq_desc *desc;
121         unsigned long flags;
122         int ret = -ENXIO;
123
124         if (irq >= nr_irqs) {
125                 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
126                 return -ENODEV;
127         }
128
129         desc = irq_to_desc(irq);
130         if (type == IRQ_TYPE_NONE)
131                 return 0;
132
133         spin_lock_irqsave(&desc->lock, flags);
134         ret = __irq_set_trigger(desc, irq, flags);
135         spin_unlock_irqrestore(&desc->lock, flags);
136         return ret;
137 }
138 EXPORT_SYMBOL(set_irq_type);
139
140 /**
141  *      set_irq_data - set irq type data for an irq
142  *      @irq:   Interrupt number
143  *      @data:  Pointer to interrupt specific data
144  *
145  *      Set the hardware irq controller data for an irq
146  */
147 int set_irq_data(unsigned int irq, void *data)
148 {
149         struct irq_desc *desc;
150         unsigned long flags;
151
152         if (irq >= nr_irqs) {
153                 printk(KERN_ERR
154                        "Trying to install controller data for IRQ%d\n", irq);
155                 return -EINVAL;
156         }
157
158         desc = irq_to_desc(irq);
159         spin_lock_irqsave(&desc->lock, flags);
160         desc->handler_data = data;
161         spin_unlock_irqrestore(&desc->lock, flags);
162         return 0;
163 }
164 EXPORT_SYMBOL(set_irq_data);
165
166 /**
167  *      set_irq_data - set irq type data for an irq
168  *      @irq:   Interrupt number
169  *      @entry: Pointer to MSI descriptor data
170  *
171  *      Set the hardware irq controller data for an irq
172  */
173 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
174 {
175         struct irq_desc *desc;
176         unsigned long flags;
177
178         if (irq >= nr_irqs) {
179                 printk(KERN_ERR
180                        "Trying to install msi data for IRQ%d\n", irq);
181                 return -EINVAL;
182         }
183         desc = irq_to_desc(irq);
184         spin_lock_irqsave(&desc->lock, flags);
185         desc->msi_desc = entry;
186         if (entry)
187                 entry->irq = irq;
188         spin_unlock_irqrestore(&desc->lock, flags);
189         return 0;
190 }
191
192 /**
193  *      set_irq_chip_data - set irq chip data for an irq
194  *      @irq:   Interrupt number
195  *      @data:  Pointer to chip specific data
196  *
197  *      Set the hardware irq chip data for an irq
198  */
199 int set_irq_chip_data(unsigned int irq, void *data)
200 {
201         struct irq_desc *desc;
202         unsigned long flags;
203
204         desc = irq_to_desc(irq);
205         if (irq >= nr_irqs || !desc->chip) {
206                 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
207                 return -EINVAL;
208         }
209
210         spin_lock_irqsave(&desc->lock, flags);
211         desc->chip_data = data;
212         spin_unlock_irqrestore(&desc->lock, flags);
213
214         return 0;
215 }
216 EXPORT_SYMBOL(set_irq_chip_data);
217
218 /*
219  * default enable function
220  */
221 static void default_enable(unsigned int irq)
222 {
223         struct irq_desc *desc;
224
225         desc = irq_to_desc(irq);
226         desc->chip->unmask(irq);
227         desc->status &= ~IRQ_MASKED;
228 }
229
230 /*
231  * default disable function
232  */
233 static void default_disable(unsigned int irq)
234 {
235 }
236
237 /*
238  * default startup function
239  */
240 static unsigned int default_startup(unsigned int irq)
241 {
242         struct irq_desc *desc;
243
244         desc = irq_to_desc(irq);
245         desc->chip->enable(irq);
246
247         return 0;
248 }
249
250 /*
251  * default shutdown function
252  */
253 static void default_shutdown(unsigned int irq)
254 {
255         struct irq_desc *desc;
256
257         desc = irq_to_desc(irq);
258         desc->chip->mask(irq);
259         desc->status |= IRQ_MASKED;
260 }
261
262 /*
263  * Fixup enable/disable function pointers
264  */
265 void irq_chip_set_defaults(struct irq_chip *chip)
266 {
267         if (!chip->enable)
268                 chip->enable = default_enable;
269         if (!chip->disable)
270                 chip->disable = default_disable;
271         if (!chip->startup)
272                 chip->startup = default_startup;
273         /*
274          * We use chip->disable, when the user provided its own. When
275          * we have default_disable set for chip->disable, then we need
276          * to use default_shutdown, otherwise the irq line is not
277          * disabled on free_irq():
278          */
279         if (!chip->shutdown)
280                 chip->shutdown = chip->disable != default_disable ?
281                         chip->disable : default_shutdown;
282         if (!chip->name)
283                 chip->name = chip->typename;
284         if (!chip->end)
285                 chip->end = dummy_irq_chip.end;
286 }
287
288 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
289 {
290         if (desc->chip->mask_ack)
291                 desc->chip->mask_ack(irq);
292         else {
293                 desc->chip->mask(irq);
294                 desc->chip->ack(irq);
295         }
296 }
297
298 /**
299  *      handle_simple_irq - Simple and software-decoded IRQs.
300  *      @irq:   the interrupt number
301  *      @desc:  the interrupt description structure for this irq
302  *
303  *      Simple interrupts are either sent from a demultiplexing interrupt
304  *      handler or come from hardware, where no interrupt hardware control
305  *      is necessary.
306  *
307  *      Note: The caller is expected to handle the ack, clear, mask and
308  *      unmask issues if necessary.
309  */
310 void
311 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
312 {
313         struct irqaction *action;
314         irqreturn_t action_ret;
315         const unsigned int cpu = smp_processor_id();
316
317         spin_lock(&desc->lock);
318
319         if (unlikely(desc->status & IRQ_INPROGRESS))
320                 goto out_unlock;
321         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
322         kstat_cpu(cpu).irqs[irq]++;
323
324         action = desc->action;
325         if (unlikely(!action || (desc->status & IRQ_DISABLED)))
326                 goto out_unlock;
327
328         desc->status |= IRQ_INPROGRESS;
329         spin_unlock(&desc->lock);
330
331         action_ret = handle_IRQ_event(irq, action);
332         if (!noirqdebug)
333                 note_interrupt(irq, desc, action_ret);
334
335         spin_lock(&desc->lock);
336         desc->status &= ~IRQ_INPROGRESS;
337 out_unlock:
338         spin_unlock(&desc->lock);
339 }
340
341 /**
342  *      handle_level_irq - Level type irq handler
343  *      @irq:   the interrupt number
344  *      @desc:  the interrupt description structure for this irq
345  *
346  *      Level type interrupts are active as long as the hardware line has
347  *      the active level. This may require to mask the interrupt and unmask
348  *      it after the associated handler has acknowledged the device, so the
349  *      interrupt line is back to inactive.
350  */
351 void
352 handle_level_irq(unsigned int irq, struct irq_desc *desc)
353 {
354         unsigned int cpu = smp_processor_id();
355         struct irqaction *action;
356         irqreturn_t action_ret;
357
358         spin_lock(&desc->lock);
359         mask_ack_irq(desc, irq);
360
361         if (unlikely(desc->status & IRQ_INPROGRESS))
362                 goto out_unlock;
363         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
364         kstat_cpu(cpu).irqs[irq]++;
365
366         /*
367          * If its disabled or no action available
368          * keep it masked and get out of here
369          */
370         action = desc->action;
371         if (unlikely(!action || (desc->status & IRQ_DISABLED)))
372                 goto out_unlock;
373
374         desc->status |= IRQ_INPROGRESS;
375         spin_unlock(&desc->lock);
376
377         action_ret = handle_IRQ_event(irq, action);
378         if (!noirqdebug)
379                 note_interrupt(irq, desc, action_ret);
380
381         spin_lock(&desc->lock);
382         desc->status &= ~IRQ_INPROGRESS;
383         if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
384                 desc->chip->unmask(irq);
385 out_unlock:
386         spin_unlock(&desc->lock);
387 }
388
389 /**
390  *      handle_fasteoi_irq - irq handler for transparent controllers
391  *      @irq:   the interrupt number
392  *      @desc:  the interrupt description structure for this irq
393  *
394  *      Only a single callback will be issued to the chip: an ->eoi()
395  *      call when the interrupt has been serviced. This enables support
396  *      for modern forms of interrupt handlers, which handle the flow
397  *      details in hardware, transparently.
398  */
399 void
400 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
401 {
402         unsigned int cpu = smp_processor_id();
403         struct irqaction *action;
404         irqreturn_t action_ret;
405
406         spin_lock(&desc->lock);
407
408         if (unlikely(desc->status & IRQ_INPROGRESS))
409                 goto out;
410
411         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
412         kstat_cpu(cpu).irqs[irq]++;
413
414         /*
415          * If its disabled or no action available
416          * then mask it and get out of here:
417          */
418         action = desc->action;
419         if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
420                 desc->status |= IRQ_PENDING;
421                 if (desc->chip->mask)
422                         desc->chip->mask(irq);
423                 goto out;
424         }
425
426         desc->status |= IRQ_INPROGRESS;
427         desc->status &= ~IRQ_PENDING;
428         spin_unlock(&desc->lock);
429
430         action_ret = handle_IRQ_event(irq, action);
431         if (!noirqdebug)
432                 note_interrupt(irq, desc, action_ret);
433
434         spin_lock(&desc->lock);
435         desc->status &= ~IRQ_INPROGRESS;
436 out:
437         desc->chip->eoi(irq);
438
439         spin_unlock(&desc->lock);
440 }
441
442 /**
443  *      handle_edge_irq - edge type IRQ handler
444  *      @irq:   the interrupt number
445  *      @desc:  the interrupt description structure for this irq
446  *
447  *      Interrupt occures on the falling and/or rising edge of a hardware
448  *      signal. The occurence is latched into the irq controller hardware
449  *      and must be acked in order to be reenabled. After the ack another
450  *      interrupt can happen on the same source even before the first one
451  *      is handled by the assosiacted event handler. If this happens it
452  *      might be necessary to disable (mask) the interrupt depending on the
453  *      controller hardware. This requires to reenable the interrupt inside
454  *      of the loop which handles the interrupts which have arrived while
455  *      the handler was running. If all pending interrupts are handled, the
456  *      loop is left.
457  */
458 void
459 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
460 {
461         const unsigned int cpu = smp_processor_id();
462
463         spin_lock(&desc->lock);
464
465         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
466
467         /*
468          * If we're currently running this IRQ, or its disabled,
469          * we shouldn't process the IRQ. Mark it pending, handle
470          * the necessary masking and go out
471          */
472         if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
473                     !desc->action)) {
474                 desc->status |= (IRQ_PENDING | IRQ_MASKED);
475                 mask_ack_irq(desc, irq);
476                 goto out_unlock;
477         }
478
479         kstat_cpu(cpu).irqs[irq]++;
480
481         /* Start handling the irq */
482         desc->chip->ack(irq);
483
484         /* Mark the IRQ currently in progress.*/
485         desc->status |= IRQ_INPROGRESS;
486
487         do {
488                 struct irqaction *action = desc->action;
489                 irqreturn_t action_ret;
490
491                 if (unlikely(!action)) {
492                         desc->chip->mask(irq);
493                         goto out_unlock;
494                 }
495
496                 /*
497                  * When another irq arrived while we were handling
498                  * one, we could have masked the irq.
499                  * Renable it, if it was not disabled in meantime.
500                  */
501                 if (unlikely((desc->status &
502                                (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
503                               (IRQ_PENDING | IRQ_MASKED))) {
504                         desc->chip->unmask(irq);
505                         desc->status &= ~IRQ_MASKED;
506                 }
507
508                 desc->status &= ~IRQ_PENDING;
509                 spin_unlock(&desc->lock);
510                 action_ret = handle_IRQ_event(irq, action);
511                 if (!noirqdebug)
512                         note_interrupt(irq, desc, action_ret);
513                 spin_lock(&desc->lock);
514
515         } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
516
517         desc->status &= ~IRQ_INPROGRESS;
518 out_unlock:
519         spin_unlock(&desc->lock);
520 }
521
522 /**
523  *      handle_percpu_IRQ - Per CPU local irq handler
524  *      @irq:   the interrupt number
525  *      @desc:  the interrupt description structure for this irq
526  *
527  *      Per CPU interrupts on SMP machines without locking requirements
528  */
529 void
530 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
531 {
532         irqreturn_t action_ret;
533
534         kstat_this_cpu.irqs[irq]++;
535
536         if (desc->chip->ack)
537                 desc->chip->ack(irq);
538
539         action_ret = handle_IRQ_event(irq, desc->action);
540         if (!noirqdebug)
541                 note_interrupt(irq, desc, action_ret);
542
543         if (desc->chip->eoi)
544                 desc->chip->eoi(irq);
545 }
546
547 void
548 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
549                   const char *name)
550 {
551         struct irq_desc *desc;
552         unsigned long flags;
553
554         if (irq >= nr_irqs) {
555                 printk(KERN_ERR
556                        "Trying to install type control for IRQ%d\n", irq);
557                 return;
558         }
559
560         desc = irq_to_desc(irq);
561
562         if (!handle)
563                 handle = handle_bad_irq;
564         else if (desc->chip == &no_irq_chip) {
565                 printk(KERN_WARNING "Trying to install %sinterrupt handler "
566                        "for IRQ%d\n", is_chained ? "chained " : "", irq);
567                 /*
568                  * Some ARM implementations install a handler for really dumb
569                  * interrupt hardware without setting an irq_chip. This worked
570                  * with the ARM no_irq_chip but the check in setup_irq would
571                  * prevent us to setup the interrupt at all. Switch it to
572                  * dummy_irq_chip for easy transition.
573                  */
574                 desc->chip = &dummy_irq_chip;
575         }
576
577         spin_lock_irqsave(&desc->lock, flags);
578
579         /* Uninstall? */
580         if (handle == handle_bad_irq) {
581                 if (desc->chip != &no_irq_chip)
582                         mask_ack_irq(desc, irq);
583                 desc->status |= IRQ_DISABLED;
584                 desc->depth = 1;
585         }
586         desc->handle_irq = handle;
587         desc->name = name;
588
589         if (handle != handle_bad_irq && is_chained) {
590                 desc->status &= ~IRQ_DISABLED;
591                 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
592                 desc->depth = 0;
593                 desc->chip->startup(irq);
594         }
595         spin_unlock_irqrestore(&desc->lock, flags);
596 }
597
598 void
599 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
600                          irq_flow_handler_t handle)
601 {
602         set_irq_chip(irq, chip);
603         __set_irq_handler(irq, handle, 0, NULL);
604 }
605
606 void
607 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
608                               irq_flow_handler_t handle, const char *name)
609 {
610         set_irq_chip(irq, chip);
611         __set_irq_handler(irq, handle, 0, name);
612 }
613
614 void __init set_irq_noprobe(unsigned int irq)
615 {
616         struct irq_desc *desc;
617         unsigned long flags;
618
619         if (irq >= nr_irqs) {
620                 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
621
622                 return;
623         }
624
625         desc = irq_to_desc(irq);
626
627         spin_lock_irqsave(&desc->lock, flags);
628         desc->status |= IRQ_NOPROBE;
629         spin_unlock_irqrestore(&desc->lock, flags);
630 }
631
632 void __init set_irq_probe(unsigned int irq)
633 {
634         struct irq_desc *desc;
635         unsigned long flags;
636
637         if (irq >= nr_irqs) {
638                 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
639
640                 return;
641         }
642
643         desc = irq_to_desc(irq);
644
645         spin_lock_irqsave(&desc->lock, flags);
646         desc->status &= ~IRQ_NOPROBE;
647         spin_unlock_irqrestore(&desc->lock, flags);
648 }