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