genirq: deprecate __do_IRQ
[safe/jmp/linux-2.6] / 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         desc = irq_to_desc(irq);
31         if (!desc) {
32                 WARN(1, KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
33                 return;
34         }
35
36         /* Ensure we don't have left over values from a previous use of this 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         cpumask_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 = irq_to_desc(irq);
61         unsigned long flags;
62
63         if (!desc) {
64                 WARN(1, KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
65                 return;
66         }
67
68         spin_lock_irqsave(&desc->lock, flags);
69         if (desc->action) {
70                 spin_unlock_irqrestore(&desc->lock, flags);
71                 WARN(1, KERN_ERR "Destroying IRQ%d without calling free_irq\n",
72                         irq);
73                 return;
74         }
75         desc->msi_desc = NULL;
76         desc->handler_data = NULL;
77         desc->chip_data = NULL;
78         desc->handle_irq = handle_bad_irq;
79         desc->chip = &no_irq_chip;
80         desc->name = NULL;
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 = irq_to_desc(irq);
93         unsigned long flags;
94
95         if (!desc) {
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         spin_lock_irqsave(&desc->lock, flags);
104         irq_chip_set_defaults(chip);
105         desc->chip = chip;
106         spin_unlock_irqrestore(&desc->lock, flags);
107
108         return 0;
109 }
110 EXPORT_SYMBOL(set_irq_chip);
111
112 /**
113  *      set_irq_type - set the irq trigger type for an irq
114  *      @irq:   irq number
115  *      @type:  IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
116  */
117 int set_irq_type(unsigned int irq, unsigned int type)
118 {
119         struct irq_desc *desc = irq_to_desc(irq);
120         unsigned long flags;
121         int ret = -ENXIO;
122
123         if (!desc) {
124                 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
125                 return -ENODEV;
126         }
127
128         type &= IRQ_TYPE_SENSE_MASK;
129         if (type == IRQ_TYPE_NONE)
130                 return 0;
131
132         spin_lock_irqsave(&desc->lock, flags);
133         ret = __irq_set_trigger(desc, irq, type);
134         spin_unlock_irqrestore(&desc->lock, flags);
135         return ret;
136 }
137 EXPORT_SYMBOL(set_irq_type);
138
139 /**
140  *      set_irq_data - set irq type data for an irq
141  *      @irq:   Interrupt number
142  *      @data:  Pointer to interrupt specific data
143  *
144  *      Set the hardware irq controller data for an irq
145  */
146 int set_irq_data(unsigned int irq, void *data)
147 {
148         struct irq_desc *desc = irq_to_desc(irq);
149         unsigned long flags;
150
151         if (!desc) {
152                 printk(KERN_ERR
153                        "Trying to install controller data for IRQ%d\n", irq);
154                 return -EINVAL;
155         }
156
157         spin_lock_irqsave(&desc->lock, flags);
158         desc->handler_data = data;
159         spin_unlock_irqrestore(&desc->lock, flags);
160         return 0;
161 }
162 EXPORT_SYMBOL(set_irq_data);
163
164 /**
165  *      set_irq_data - set irq type data for an irq
166  *      @irq:   Interrupt number
167  *      @entry: Pointer to MSI descriptor data
168  *
169  *      Set the hardware irq controller data for an irq
170  */
171 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
172 {
173         struct irq_desc *desc = irq_to_desc(irq);
174         unsigned long flags;
175
176         if (!desc) {
177                 printk(KERN_ERR
178                        "Trying to install msi data for IRQ%d\n", irq);
179                 return -EINVAL;
180         }
181
182         spin_lock_irqsave(&desc->lock, flags);
183         desc->msi_desc = entry;
184         if (entry)
185                 entry->irq = irq;
186         spin_unlock_irqrestore(&desc->lock, flags);
187         return 0;
188 }
189
190 /**
191  *      set_irq_chip_data - set irq chip data for an irq
192  *      @irq:   Interrupt number
193  *      @data:  Pointer to chip specific data
194  *
195  *      Set the hardware irq chip data for an irq
196  */
197 int set_irq_chip_data(unsigned int irq, void *data)
198 {
199         struct irq_desc *desc = irq_to_desc(irq);
200         unsigned long flags;
201
202         if (!desc) {
203                 printk(KERN_ERR
204                        "Trying to install chip data for IRQ%d\n", irq);
205                 return -EINVAL;
206         }
207
208         if (!desc->chip) {
209                 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
210                 return -EINVAL;
211         }
212
213         spin_lock_irqsave(&desc->lock, flags);
214         desc->chip_data = data;
215         spin_unlock_irqrestore(&desc->lock, flags);
216
217         return 0;
218 }
219 EXPORT_SYMBOL(set_irq_chip_data);
220
221 /*
222  * default enable function
223  */
224 static void default_enable(unsigned int irq)
225 {
226         struct irq_desc *desc = irq_to_desc(irq);
227
228         desc->chip->unmask(irq);
229         desc->status &= ~IRQ_MASKED;
230 }
231
232 /*
233  * default disable function
234  */
235 static void default_disable(unsigned int irq)
236 {
237 }
238
239 /*
240  * default startup function
241  */
242 static unsigned int default_startup(unsigned int irq)
243 {
244         struct irq_desc *desc = irq_to_desc(irq);
245
246         desc->chip->enable(irq);
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 = irq_to_desc(irq);
256
257         desc->chip->mask(irq);
258         desc->status |= IRQ_MASKED;
259 }
260
261 /*
262  * Fixup enable/disable function pointers
263  */
264 void irq_chip_set_defaults(struct irq_chip *chip)
265 {
266         if (!chip->enable)
267                 chip->enable = default_enable;
268         if (!chip->disable)
269                 chip->disable = default_disable;
270         if (!chip->startup)
271                 chip->startup = default_startup;
272         /*
273          * We use chip->disable, when the user provided its own. When
274          * we have default_disable set for chip->disable, then we need
275          * to use default_shutdown, otherwise the irq line is not
276          * disabled on free_irq():
277          */
278         if (!chip->shutdown)
279                 chip->shutdown = chip->disable != default_disable ?
280                         chip->disable : default_shutdown;
281         if (!chip->name)
282                 chip->name = chip->typename;
283         if (!chip->end)
284                 chip->end = dummy_irq_chip.end;
285 }
286
287 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
288 {
289         if (desc->chip->mask_ack)
290                 desc->chip->mask_ack(irq);
291         else {
292                 desc->chip->mask(irq);
293                 if (desc->chip->ack)
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
316         spin_lock(&desc->lock);
317
318         if (unlikely(desc->status & IRQ_INPROGRESS))
319                 goto out_unlock;
320         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
321         kstat_incr_irqs_this_cpu(irq, desc);
322
323         action = desc->action;
324         if (unlikely(!action || (desc->status & IRQ_DISABLED)))
325                 goto out_unlock;
326
327         desc->status |= IRQ_INPROGRESS;
328         spin_unlock(&desc->lock);
329
330         action_ret = handle_IRQ_event(irq, action);
331         if (!noirqdebug)
332                 note_interrupt(irq, desc, action_ret);
333
334         spin_lock(&desc->lock);
335         desc->status &= ~IRQ_INPROGRESS;
336 out_unlock:
337         spin_unlock(&desc->lock);
338 }
339
340 /**
341  *      handle_level_irq - Level type irq handler
342  *      @irq:   the interrupt number
343  *      @desc:  the interrupt description structure for this irq
344  *
345  *      Level type interrupts are active as long as the hardware line has
346  *      the active level. This may require to mask the interrupt and unmask
347  *      it after the associated handler has acknowledged the device, so the
348  *      interrupt line is back to inactive.
349  */
350 void
351 handle_level_irq(unsigned int irq, struct irq_desc *desc)
352 {
353         struct irqaction *action;
354         irqreturn_t action_ret;
355
356         spin_lock(&desc->lock);
357         mask_ack_irq(desc, irq);
358         desc = irq_remap_to_desc(irq, desc);
359
360         if (unlikely(desc->status & IRQ_INPROGRESS))
361                 goto out_unlock;
362         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
363         kstat_incr_irqs_this_cpu(irq, desc);
364
365         /*
366          * If its disabled or no action available
367          * keep it masked and get out of here
368          */
369         action = desc->action;
370         if (unlikely(!action || (desc->status & IRQ_DISABLED)))
371                 goto out_unlock;
372
373         desc->status |= IRQ_INPROGRESS;
374         spin_unlock(&desc->lock);
375
376         action_ret = handle_IRQ_event(irq, action);
377         if (!noirqdebug)
378                 note_interrupt(irq, desc, action_ret);
379
380         spin_lock(&desc->lock);
381         desc->status &= ~IRQ_INPROGRESS;
382         if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
383                 desc->chip->unmask(irq);
384 out_unlock:
385         spin_unlock(&desc->lock);
386 }
387 EXPORT_SYMBOL_GPL(handle_level_irq);
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         struct irqaction *action;
403         irqreturn_t action_ret;
404
405         spin_lock(&desc->lock);
406
407         if (unlikely(desc->status & IRQ_INPROGRESS))
408                 goto out;
409
410         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
411         kstat_incr_irqs_this_cpu(irq, desc);
412
413         /*
414          * If its disabled or no action available
415          * then mask it and get out of here:
416          */
417         action = desc->action;
418         if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
419                 desc->status |= IRQ_PENDING;
420                 if (desc->chip->mask)
421                         desc->chip->mask(irq);
422                 goto out;
423         }
424
425         desc->status |= IRQ_INPROGRESS;
426         desc->status &= ~IRQ_PENDING;
427         spin_unlock(&desc->lock);
428
429         action_ret = handle_IRQ_event(irq, action);
430         if (!noirqdebug)
431                 note_interrupt(irq, desc, action_ret);
432
433         spin_lock(&desc->lock);
434         desc->status &= ~IRQ_INPROGRESS;
435 out:
436         desc->chip->eoi(irq);
437         desc = irq_remap_to_desc(irq, desc);
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         spin_lock(&desc->lock);
462
463         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
464
465         /*
466          * If we're currently running this IRQ, or its disabled,
467          * we shouldn't process the IRQ. Mark it pending, handle
468          * the necessary masking and go out
469          */
470         if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
471                     !desc->action)) {
472                 desc->status |= (IRQ_PENDING | IRQ_MASKED);
473                 mask_ack_irq(desc, irq);
474                 desc = irq_remap_to_desc(irq, desc);
475                 goto out_unlock;
476         }
477         kstat_incr_irqs_this_cpu(irq, desc);
478
479         /* Start handling the irq */
480         if (desc->chip->ack)
481                 desc->chip->ack(irq);
482         desc = irq_remap_to_desc(irq, desc);
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_incr_irqs_this_cpu(irq, desc);
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                 desc = irq_remap_to_desc(irq, desc);
546         }
547 }
548
549 void
550 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
551                   const char *name)
552 {
553         struct irq_desc *desc = irq_to_desc(irq);
554         unsigned long flags;
555
556         if (!desc) {
557                 printk(KERN_ERR
558                        "Trying to install type control for IRQ%d\n", irq);
559                 return;
560         }
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 = irq_remap_to_desc(irq, desc);
584                 }
585                 desc->status |= IRQ_DISABLED;
586                 desc->depth = 1;
587         }
588         desc->handle_irq = handle;
589         desc->name = name;
590
591         if (handle != handle_bad_irq && is_chained) {
592                 desc->status &= ~IRQ_DISABLED;
593                 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
594                 desc->depth = 0;
595                 desc->chip->startup(irq);
596         }
597         spin_unlock_irqrestore(&desc->lock, flags);
598 }
599 EXPORT_SYMBOL_GPL(__set_irq_handler);
600
601 void
602 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
603                          irq_flow_handler_t handle)
604 {
605         set_irq_chip(irq, chip);
606         __set_irq_handler(irq, handle, 0, NULL);
607 }
608
609 void
610 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
611                               irq_flow_handler_t handle, const char *name)
612 {
613         set_irq_chip(irq, chip);
614         __set_irq_handler(irq, handle, 0, name);
615 }
616
617 void __init set_irq_noprobe(unsigned int irq)
618 {
619         struct irq_desc *desc = irq_to_desc(irq);
620         unsigned long flags;
621
622         if (!desc) {
623                 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
624                 return;
625         }
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 = irq_to_desc(irq);
635         unsigned long flags;
636
637         if (!desc) {
638                 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
639                 return;
640         }
641
642         spin_lock_irqsave(&desc->lock, flags);
643         desc->status &= ~IRQ_NOPROBE;
644         spin_unlock_irqrestore(&desc->lock, flags);
645 }