Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
47f176fd | 12 | #include <linux/delay.h> |
1da177e4 | 13 | |
7a55713a IM |
14 | #include "internals.h" |
15 | ||
1da177e4 LT |
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 | */ | |
74ffd553 | 21 | static DEFINE_MUTEX(probing_active); |
1da177e4 LT |
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 | { | |
34ffdb72 | 32 | struct irq_desc *desc; |
10e58084 TG |
33 | unsigned long mask = 0; |
34 | unsigned int status; | |
35 | int i; | |
1da177e4 | 36 | |
74ffd553 | 37 | mutex_lock(&probing_active); |
1da177e4 LT |
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 | */ | |
10e58084 | 42 | for_each_irq_desc_reverse(i, desc) { |
1da177e4 | 43 | spin_lock_irq(&desc->lock); |
6a6de9ef | 44 | if (!desc->action && !(desc->status & IRQ_NOPROBE)) { |
7a55713a IM |
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 | ||
6a6de9ef TG |
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); | |
06fcb0c6 | 57 | desc->chip->startup(i); |
6a6de9ef | 58 | } |
1da177e4 LT |
59 | spin_unlock_irq(&desc->lock); |
60 | } | |
61 | ||
62 | /* Wait for longstanding interrupts to trigger. */ | |
47f176fd | 63 | msleep(20); |
1da177e4 LT |
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 | */ | |
10e58084 | 70 | for_each_irq_desc_reverse(i, desc) { |
1da177e4 | 71 | spin_lock_irq(&desc->lock); |
3418d724 | 72 | if (!desc->action && !(desc->status & IRQ_NOPROBE)) { |
1da177e4 | 73 | desc->status |= IRQ_AUTODETECT | IRQ_WAITING; |
d1bef4ed | 74 | if (desc->chip->startup(i)) |
1da177e4 LT |
75 | desc->status |= IRQ_PENDING; |
76 | } | |
77 | spin_unlock_irq(&desc->lock); | |
78 | } | |
79 | ||
80 | /* | |
81 | * Wait for spurious interrupts to trigger | |
82 | */ | |
47f176fd | 83 | msleep(100); |
1da177e4 LT |
84 | |
85 | /* | |
86 | * Now filter out any obviously spurious interrupts | |
87 | */ | |
10e58084 | 88 | for_each_irq_desc(i, desc) { |
1da177e4 LT |
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; | |
d1bef4ed | 96 | desc->chip->shutdown(i); |
1da177e4 LT |
97 | } else |
98 | if (i < 32) | |
06fcb0c6 | 99 | mask |= 1 << i; |
1da177e4 LT |
100 | } |
101 | spin_unlock_irq(&desc->lock); | |
102 | } | |
103 | ||
06fcb0c6 | 104 | return mask; |
1da177e4 | 105 | } |
1da177e4 LT |
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 | { | |
10e58084 TG |
122 | unsigned int status, mask = 0; |
123 | struct irq_desc *desc; | |
1da177e4 LT |
124 | int i; |
125 | ||
10e58084 | 126 | for_each_irq_desc(i, desc) { |
1da177e4 LT |
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; | |
d1bef4ed | 135 | desc->chip->shutdown(i); |
1da177e4 LT |
136 | } |
137 | spin_unlock_irq(&desc->lock); | |
138 | } | |
74ffd553 | 139 | mutex_unlock(&probing_active); |
1da177e4 LT |
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 | { | |
63d659d5 | 164 | int i, irq_found = 0, nr_of_irqs = 0; |
10e58084 TG |
165 | struct irq_desc *desc; |
166 | unsigned int status; | |
1da177e4 | 167 | |
10e58084 | 168 | for_each_irq_desc(i, desc) { |
1da177e4 LT |
169 | spin_lock_irq(&desc->lock); |
170 | status = desc->status; | |
171 | ||
172 | if (status & IRQ_AUTODETECT) { | |
173 | if (!(status & IRQ_WAITING)) { | |
63d659d5 | 174 | if (!nr_of_irqs) |
1da177e4 | 175 | irq_found = i; |
63d659d5 | 176 | nr_of_irqs++; |
1da177e4 LT |
177 | } |
178 | desc->status = status & ~IRQ_AUTODETECT; | |
d1bef4ed | 179 | desc->chip->shutdown(i); |
1da177e4 LT |
180 | } |
181 | spin_unlock_irq(&desc->lock); | |
182 | } | |
74ffd553 | 183 | mutex_unlock(&probing_active); |
1da177e4 | 184 | |
63d659d5 | 185 | if (nr_of_irqs > 1) |
1da177e4 | 186 | irq_found = -irq_found; |
74ffd553 | 187 | |
1da177e4 LT |
188 | return irq_found; |
189 | } | |
1da177e4 LT |
190 | EXPORT_SYMBOL(probe_irq_off); |
191 |