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