x86: move kstat_irqs from kstat to irq_desc
[linux-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
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_irqs_this_cpu(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
359         if (unlikely(desc->status & IRQ_INPROGRESS))
360                 goto out_unlock;
361         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
362         kstat_irqs_this_cpu(desc)++;
363
364         /*
365          * If its disabled or no action available
366          * keep it masked and get out of here
367          */
368         action = desc->action;
369         if (unlikely(!action || (desc->status & IRQ_DISABLED)))
370                 goto out_unlock;
371
372         desc->status |= IRQ_INPROGRESS;
373         spin_unlock(&desc->lock);
374
375         action_ret = handle_IRQ_event(irq, action);
376         if (!noirqdebug)
377                 note_interrupt(irq, desc, action_ret);
378
379         spin_lock(&desc->lock);
380         desc->status &= ~IRQ_INPROGRESS;
381         if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
382                 desc->chip->unmask(irq);
383 out_unlock:
384         spin_unlock(&desc->lock);
385 }
386
387 /**
388  *      handle_fasteoi_irq - irq handler for transparent controllers
389  *      @irq:   the interrupt number
390  *      @desc:  the interrupt description structure for this irq
391  *
392  *      Only a single callback will be issued to the chip: an ->eoi()
393  *      call when the interrupt has been serviced. This enables support
394  *      for modern forms of interrupt handlers, which handle the flow
395  *      details in hardware, transparently.
396  */
397 void
398 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
399 {
400         struct irqaction *action;
401         irqreturn_t action_ret;
402
403         spin_lock(&desc->lock);
404
405         if (unlikely(desc->status & IRQ_INPROGRESS))
406                 goto out;
407
408         desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
409         kstat_irqs_this_cpu(desc)++;
410
411         /*
412          * If its disabled or no action available
413          * then mask it and get out of here:
414          */
415         action = desc->action;
416         if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
417                 desc->status |= IRQ_PENDING;
418                 if (desc->chip->mask)
419                         desc->chip->mask(irq);
420                 goto out;
421         }
422
423         desc->status |= IRQ_INPROGRESS;
424         desc->status &= ~IRQ_PENDING;
425         spin_unlock(&desc->lock);
426
427         action_ret = handle_IRQ_event(irq, action);
428         if (!noirqdebug)
429                 note_interrupt(irq, desc, action_ret);
430
431         spin_lock(&desc->lock);
432         desc->status &= ~IRQ_INPROGRESS;
433 out:
434         desc->chip->eoi(irq);
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                 goto out_unlock;
472         }
473
474         kstat_irqs_this_cpu(desc)++;
475
476         /* Start handling the irq */
477         desc->chip->ack(irq);
478
479         /* Mark the IRQ currently in progress.*/
480         desc->status |= IRQ_INPROGRESS;
481
482         do {
483                 struct irqaction *action = desc->action;
484                 irqreturn_t action_ret;
485
486                 if (unlikely(!action)) {
487                         desc->chip->mask(irq);
488                         goto out_unlock;
489                 }
490
491                 /*
492                  * When another irq arrived while we were handling
493                  * one, we could have masked the irq.
494                  * Renable it, if it was not disabled in meantime.
495                  */
496                 if (unlikely((desc->status &
497                                (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
498                               (IRQ_PENDING | IRQ_MASKED))) {
499                         desc->chip->unmask(irq);
500                         desc->status &= ~IRQ_MASKED;
501                 }
502
503                 desc->status &= ~IRQ_PENDING;
504                 spin_unlock(&desc->lock);
505                 action_ret = handle_IRQ_event(irq, action);
506                 if (!noirqdebug)
507                         note_interrupt(irq, desc, action_ret);
508                 spin_lock(&desc->lock);
509
510         } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
511
512         desc->status &= ~IRQ_INPROGRESS;
513 out_unlock:
514         spin_unlock(&desc->lock);
515 }
516
517 /**
518  *      handle_percpu_IRQ - Per CPU local irq handler
519  *      @irq:   the interrupt number
520  *      @desc:  the interrupt description structure for this irq
521  *
522  *      Per CPU interrupts on SMP machines without locking requirements
523  */
524 void
525 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
526 {
527         irqreturn_t action_ret;
528
529         kstat_irqs_this_cpu(desc)++;
530
531         if (desc->chip->ack)
532                 desc->chip->ack(irq);
533
534         action_ret = handle_IRQ_event(irq, desc->action);
535         if (!noirqdebug)
536                 note_interrupt(irq, desc, action_ret);
537
538         if (desc->chip->eoi)
539                 desc->chip->eoi(irq);
540 }
541
542 void
543 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
544                   const char *name)
545 {
546         struct irq_desc *desc;
547         unsigned long flags;
548
549         if (irq >= nr_irqs) {
550                 printk(KERN_ERR
551                        "Trying to install type control for IRQ%d\n", irq);
552                 return;
553         }
554
555         desc = irq_to_desc(irq);
556
557         if (!handle)
558                 handle = handle_bad_irq;
559         else if (desc->chip == &no_irq_chip) {
560                 printk(KERN_WARNING "Trying to install %sinterrupt handler "
561                        "for IRQ%d\n", is_chained ? "chained " : "", irq);
562                 /*
563                  * Some ARM implementations install a handler for really dumb
564                  * interrupt hardware without setting an irq_chip. This worked
565                  * with the ARM no_irq_chip but the check in setup_irq would
566                  * prevent us to setup the interrupt at all. Switch it to
567                  * dummy_irq_chip for easy transition.
568                  */
569                 desc->chip = &dummy_irq_chip;
570         }
571
572         spin_lock_irqsave(&desc->lock, flags);
573
574         /* Uninstall? */
575         if (handle == handle_bad_irq) {
576                 if (desc->chip != &no_irq_chip)
577                         mask_ack_irq(desc, irq);
578                 desc->status |= IRQ_DISABLED;
579                 desc->depth = 1;
580         }
581         desc->handle_irq = handle;
582         desc->name = name;
583
584         if (handle != handle_bad_irq && is_chained) {
585                 desc->status &= ~IRQ_DISABLED;
586                 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
587                 desc->depth = 0;
588                 desc->chip->startup(irq);
589         }
590         spin_unlock_irqrestore(&desc->lock, flags);
591 }
592
593 void
594 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
595                          irq_flow_handler_t handle)
596 {
597         set_irq_chip(irq, chip);
598         __set_irq_handler(irq, handle, 0, NULL);
599 }
600
601 void
602 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
603                               irq_flow_handler_t handle, const char *name)
604 {
605         set_irq_chip(irq, chip);
606         __set_irq_handler(irq, handle, 0, name);
607 }
608
609 void __init set_irq_noprobe(unsigned int irq)
610 {
611         struct irq_desc *desc;
612         unsigned long flags;
613
614         if (irq >= nr_irqs) {
615                 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
616
617                 return;
618         }
619
620         desc = irq_to_desc(irq);
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;
630         unsigned long flags;
631
632         if (irq >= nr_irqs) {
633                 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
634
635                 return;
636         }
637
638         desc = irq_to_desc(irq);
639
640         spin_lock_irqsave(&desc->lock, flags);
641         desc->status &= ~IRQ_NOPROBE;
642         spin_unlock_irqrestore(&desc->lock, flags);
643 }