Merge branch 'iommu-fixes-2.6.28' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6] / kernel / irq / autoprobe.c
1 /*
2  * linux/kernel/irq/autoprobe.c
3  *
4  * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
5  *
6  * This file contains the interrupt probing code and driver APIs.
7  */
8
9 #include <linux/irq.h>
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/delay.h>
13
14 #include "internals.h"
15
16 /*
17  * Autodetection depends on the fact that any interrupt that
18  * comes in on to an unassigned handler will get stuck with
19  * "IRQ_WAITING" cleared and the interrupt disabled.
20  */
21 static DEFINE_MUTEX(probing_active);
22
23 /**
24  *      probe_irq_on    - begin an interrupt autodetect
25  *
26  *      Commence probing for an interrupt. The interrupts are scanned
27  *      and a mask of potential interrupt lines is returned.
28  *
29  */
30 unsigned long probe_irq_on(void)
31 {
32         struct irq_desc *desc;
33         unsigned long mask = 0;
34         unsigned int status;
35         int i;
36
37         mutex_lock(&probing_active);
38         /*
39          * something may have generated an irq long ago and we want to
40          * flush such a longstanding irq before considering it as spurious.
41          */
42         for_each_irq_desc_reverse(i, desc) {
43                 spin_lock_irq(&desc->lock);
44                 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
45                         /*
46                          * An old-style architecture might still have
47                          * the handle_bad_irq handler there:
48                          */
49                         compat_irq_chip_set_default_handler(desc);
50
51                         /*
52                          * Some chips need to know about probing in
53                          * progress:
54                          */
55                         if (desc->chip->set_type)
56                                 desc->chip->set_type(i, IRQ_TYPE_PROBE);
57                         desc->chip->startup(i);
58                 }
59                 spin_unlock_irq(&desc->lock);
60         }
61
62         /* Wait for longstanding interrupts to trigger. */
63         msleep(20);
64
65         /*
66          * enable any unassigned irqs
67          * (we must startup again here because if a longstanding irq
68          * happened in the previous stage, it may have masked itself)
69          */
70         for_each_irq_desc_reverse(i, desc) {
71                 spin_lock_irq(&desc->lock);
72                 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
73                         desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
74                         if (desc->chip->startup(i))
75                                 desc->status |= IRQ_PENDING;
76                 }
77                 spin_unlock_irq(&desc->lock);
78         }
79
80         /*
81          * Wait for spurious interrupts to trigger
82          */
83         msleep(100);
84
85         /*
86          * Now filter out any obviously spurious interrupts
87          */
88         for_each_irq_desc(i, desc) {
89                 spin_lock_irq(&desc->lock);
90                 status = desc->status;
91
92                 if (status & IRQ_AUTODETECT) {
93                         /* It triggered already - consider it spurious. */
94                         if (!(status & IRQ_WAITING)) {
95                                 desc->status = status & ~IRQ_AUTODETECT;
96                                 desc->chip->shutdown(i);
97                         } else
98                                 if (i < 32)
99                                         mask |= 1 << i;
100                 }
101                 spin_unlock_irq(&desc->lock);
102         }
103
104         return mask;
105 }
106 EXPORT_SYMBOL(probe_irq_on);
107
108 /**
109  *      probe_irq_mask - scan a bitmap of interrupt lines
110  *      @val:   mask of interrupts to consider
111  *
112  *      Scan the interrupt lines and return a bitmap of active
113  *      autodetect interrupts. The interrupt probe logic state
114  *      is then returned to its previous value.
115  *
116  *      Note: we need to scan all the irq's even though we will
117  *      only return autodetect irq numbers - just so that we reset
118  *      them all to a known state.
119  */
120 unsigned int probe_irq_mask(unsigned long val)
121 {
122         unsigned int status, mask = 0;
123         struct irq_desc *desc;
124         int i;
125
126         for_each_irq_desc(i, desc) {
127                 spin_lock_irq(&desc->lock);
128                 status = desc->status;
129
130                 if (status & IRQ_AUTODETECT) {
131                         if (i < 16 && !(status & IRQ_WAITING))
132                                 mask |= 1 << i;
133
134                         desc->status = status & ~IRQ_AUTODETECT;
135                         desc->chip->shutdown(i);
136                 }
137                 spin_unlock_irq(&desc->lock);
138         }
139         mutex_unlock(&probing_active);
140
141         return mask & val;
142 }
143 EXPORT_SYMBOL(probe_irq_mask);
144
145 /**
146  *      probe_irq_off   - end an interrupt autodetect
147  *      @val: mask of potential interrupts (unused)
148  *
149  *      Scans the unused interrupt lines and returns the line which
150  *      appears to have triggered the interrupt. If no interrupt was
151  *      found then zero is returned. If more than one interrupt is
152  *      found then minus the first candidate is returned to indicate
153  *      their is doubt.
154  *
155  *      The interrupt probe logic state is returned to its previous
156  *      value.
157  *
158  *      BUGS: When used in a module (which arguably shouldn't happen)
159  *      nothing prevents two IRQ probe callers from overlapping. The
160  *      results of this are non-optimal.
161  */
162 int probe_irq_off(unsigned long val)
163 {
164         int i, irq_found = 0, nr_of_irqs = 0;
165         struct irq_desc *desc;
166         unsigned int status;
167
168         for_each_irq_desc(i, desc) {
169                 spin_lock_irq(&desc->lock);
170                 status = desc->status;
171
172                 if (status & IRQ_AUTODETECT) {
173                         if (!(status & IRQ_WAITING)) {
174                                 if (!nr_of_irqs)
175                                         irq_found = i;
176                                 nr_of_irqs++;
177                         }
178                         desc->status = status & ~IRQ_AUTODETECT;
179                         desc->chip->shutdown(i);
180                 }
181                 spin_unlock_irq(&desc->lock);
182         }
183         mutex_unlock(&probing_active);
184
185         if (nr_of_irqs > 1)
186                 irq_found = -irq_found;
187
188         return irq_found;
189 }
190 EXPORT_SYMBOL(probe_irq_off);
191