]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
1da177e4 LT |
3 | * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar |
4 | * | |
5 | * This file contains the interrupt probing code and driver APIs. | |
6 | */ | |
7 | ||
8 | #include <linux/irq.h> | |
9 | #include <linux/module.h> | |
10 | #include <linux/interrupt.h> | |
47f176fd | 11 | #include <linux/delay.h> |
22a9d645 | 12 | #include <linux/async.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 | |
163ef309 | 19 | * "IRQS_WAITING" cleared and the interrupt disabled. |
1da177e4 | 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 | 33 | unsigned long mask = 0; |
10e58084 | 34 | int i; |
1da177e4 | 35 | |
22a9d645 AV |
36 | /* |
37 | * quiesce the kernel, or at least the asynchronous portion | |
38 | */ | |
39 | async_synchronize_full(); | |
74ffd553 | 40 | mutex_lock(&probing_active); |
1da177e4 LT |
41 | /* |
42 | * something may have generated an irq long ago and we want to | |
43 | * flush such a longstanding irq before considering it as spurious. | |
44 | */ | |
10e58084 | 45 | for_each_irq_desc_reverse(i, desc) { |
239007b8 | 46 | raw_spin_lock_irq(&desc->lock); |
1ccb4e61 | 47 | if (!desc->action && irq_settings_can_probe(desc)) { |
6a6de9ef TG |
48 | /* |
49 | * Some chips need to know about probing in | |
50 | * progress: | |
51 | */ | |
b2ba2c30 TG |
52 | if (desc->irq_data.chip->irq_set_type) |
53 | desc->irq_data.chip->irq_set_type(&desc->irq_data, | |
54 | IRQ_TYPE_PROBE); | |
c942cee4 | 55 | irq_activate_and_startup(desc, IRQ_NORESEND); |
6a6de9ef | 56 | } |
239007b8 | 57 | raw_spin_unlock_irq(&desc->lock); |
1da177e4 LT |
58 | } |
59 | ||
60 | /* Wait for longstanding interrupts to trigger. */ | |
47f176fd | 61 | msleep(20); |
1da177e4 LT |
62 | |
63 | /* | |
64 | * enable any unassigned irqs | |
65 | * (we must startup again here because if a longstanding irq | |
66 | * happened in the previous stage, it may have masked itself) | |
67 | */ | |
10e58084 | 68 | for_each_irq_desc_reverse(i, desc) { |
239007b8 | 69 | raw_spin_lock_irq(&desc->lock); |
1ccb4e61 | 70 | if (!desc->action && irq_settings_can_probe(desc)) { |
163ef309 | 71 | desc->istate |= IRQS_AUTODETECT | IRQS_WAITING; |
1beaeacd | 72 | if (irq_activate_and_startup(desc, IRQ_NORESEND)) |
2a0d6fb3 | 73 | desc->istate |= IRQS_PENDING; |
1da177e4 | 74 | } |
239007b8 | 75 | raw_spin_unlock_irq(&desc->lock); |
1da177e4 LT |
76 | } |
77 | ||
78 | /* | |
79 | * Wait for spurious interrupts to trigger | |
80 | */ | |
47f176fd | 81 | msleep(100); |
1da177e4 LT |
82 | |
83 | /* | |
84 | * Now filter out any obviously spurious interrupts | |
85 | */ | |
10e58084 | 86 | for_each_irq_desc(i, desc) { |
239007b8 | 87 | raw_spin_lock_irq(&desc->lock); |
1da177e4 | 88 | |
bd062e76 | 89 | if (desc->istate & IRQS_AUTODETECT) { |
1da177e4 | 90 | /* It triggered already - consider it spurious. */ |
163ef309 | 91 | if (!(desc->istate & IRQS_WAITING)) { |
bd062e76 | 92 | desc->istate &= ~IRQS_AUTODETECT; |
4001d8e8 | 93 | irq_shutdown_and_deactivate(desc); |
1da177e4 LT |
94 | } else |
95 | if (i < 32) | |
06fcb0c6 | 96 | mask |= 1 << i; |
1da177e4 | 97 | } |
239007b8 | 98 | raw_spin_unlock_irq(&desc->lock); |
1da177e4 LT |
99 | } |
100 | ||
06fcb0c6 | 101 | return mask; |
1da177e4 | 102 | } |
1da177e4 LT |
103 | EXPORT_SYMBOL(probe_irq_on); |
104 | ||
105 | /** | |
106 | * probe_irq_mask - scan a bitmap of interrupt lines | |
107 | * @val: mask of interrupts to consider | |
108 | * | |
109 | * Scan the interrupt lines and return a bitmap of active | |
110 | * autodetect interrupts. The interrupt probe logic state | |
111 | * is then returned to its previous value. | |
112 | * | |
113 | * Note: we need to scan all the irq's even though we will | |
114 | * only return autodetect irq numbers - just so that we reset | |
115 | * them all to a known state. | |
116 | */ | |
117 | unsigned int probe_irq_mask(unsigned long val) | |
118 | { | |
bd062e76 | 119 | unsigned int mask = 0; |
10e58084 | 120 | struct irq_desc *desc; |
1da177e4 LT |
121 | int i; |
122 | ||
10e58084 | 123 | for_each_irq_desc(i, desc) { |
239007b8 | 124 | raw_spin_lock_irq(&desc->lock); |
bd062e76 | 125 | if (desc->istate & IRQS_AUTODETECT) { |
163ef309 | 126 | if (i < 16 && !(desc->istate & IRQS_WAITING)) |
1da177e4 LT |
127 | mask |= 1 << i; |
128 | ||
bd062e76 | 129 | desc->istate &= ~IRQS_AUTODETECT; |
4001d8e8 | 130 | irq_shutdown_and_deactivate(desc); |
1da177e4 | 131 | } |
239007b8 | 132 | raw_spin_unlock_irq(&desc->lock); |
1da177e4 | 133 | } |
74ffd553 | 134 | mutex_unlock(&probing_active); |
1da177e4 LT |
135 | |
136 | return mask & val; | |
137 | } | |
138 | EXPORT_SYMBOL(probe_irq_mask); | |
139 | ||
140 | /** | |
141 | * probe_irq_off - end an interrupt autodetect | |
142 | * @val: mask of potential interrupts (unused) | |
143 | * | |
144 | * Scans the unused interrupt lines and returns the line which | |
145 | * appears to have triggered the interrupt. If no interrupt was | |
146 | * found then zero is returned. If more than one interrupt is | |
147 | * found then minus the first candidate is returned to indicate | |
148 | * their is doubt. | |
149 | * | |
150 | * The interrupt probe logic state is returned to its previous | |
151 | * value. | |
152 | * | |
153 | * BUGS: When used in a module (which arguably shouldn't happen) | |
154 | * nothing prevents two IRQ probe callers from overlapping. The | |
155 | * results of this are non-optimal. | |
156 | */ | |
157 | int probe_irq_off(unsigned long val) | |
158 | { | |
63d659d5 | 159 | int i, irq_found = 0, nr_of_irqs = 0; |
10e58084 | 160 | struct irq_desc *desc; |
1da177e4 | 161 | |
10e58084 | 162 | for_each_irq_desc(i, desc) { |
239007b8 | 163 | raw_spin_lock_irq(&desc->lock); |
1da177e4 | 164 | |
bd062e76 | 165 | if (desc->istate & IRQS_AUTODETECT) { |
163ef309 | 166 | if (!(desc->istate & IRQS_WAITING)) { |
63d659d5 | 167 | if (!nr_of_irqs) |
1da177e4 | 168 | irq_found = i; |
63d659d5 | 169 | nr_of_irqs++; |
1da177e4 | 170 | } |
bd062e76 | 171 | desc->istate &= ~IRQS_AUTODETECT; |
4001d8e8 | 172 | irq_shutdown_and_deactivate(desc); |
1da177e4 | 173 | } |
239007b8 | 174 | raw_spin_unlock_irq(&desc->lock); |
1da177e4 | 175 | } |
74ffd553 | 176 | mutex_unlock(&probing_active); |
1da177e4 | 177 | |
63d659d5 | 178 | if (nr_of_irqs > 1) |
1da177e4 | 179 | irq_found = -irq_found; |
74ffd553 | 180 | |
1da177e4 LT |
181 | return irq_found; |
182 | } | |
1da177e4 LT |
183 | EXPORT_SYMBOL(probe_irq_off); |
184 |