blob: b3a5549ea81eddf22cdb444e97e96377e27e2ba0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Luca Falavigna47f176f2005-06-28 20:44:42 -070012#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Ingo Molnar7a557132006-06-29 02:24:54 -070014#include "internals.h"
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016/*
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 */
Ingo Molnar74ffd552006-06-29 02:24:37 -070021static DEFINE_MUTEX(probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
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 */
30unsigned long probe_irq_on(void)
31{
Ingo Molnar34ffdb72006-06-29 02:24:40 -070032 struct irq_desc *desc;
Ingo Molnar06fcb0c2006-06-29 02:24:40 -070033 unsigned long mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 unsigned int i;
35
Ingo Molnar74ffd552006-06-29 02:24:37 -070036 mutex_lock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 /*
38 * something may have generated an irq long ago and we want to
39 * flush such a longstanding irq before considering it as spurious.
40 */
Yinghai Lu85c0f902008-08-19 20:49:47 -070041 for (i = nr_irqs-1; i > 0; i--) {
Yinghai Lu08678b02008-08-19 20:50:05 -070042 desc = irq_to_desc(i);
Ingo Molnar3bf52a42008-08-19 20:50:29 -070043 if (!desc)
44 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 spin_lock_irq(&desc->lock);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070047 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
48 /*
Ingo Molnar7a557132006-06-29 02:24:54 -070049 * An old-style architecture might still have
50 * the handle_bad_irq handler there:
51 */
52 compat_irq_chip_set_default_handler(desc);
53
54 /*
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070055 * Some chips need to know about probing in
56 * progress:
57 */
58 if (desc->chip->set_type)
59 desc->chip->set_type(i, IRQ_TYPE_PROBE);
Ingo Molnar06fcb0c2006-06-29 02:24:40 -070060 desc->chip->startup(i);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 spin_unlock_irq(&desc->lock);
63 }
64
65 /* Wait for longstanding interrupts to trigger. */
Luca Falavigna47f176f2005-06-28 20:44:42 -070066 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 /*
69 * enable any unassigned irqs
70 * (we must startup again here because if a longstanding irq
71 * happened in the previous stage, it may have masked itself)
72 */
Yinghai Lu85c0f902008-08-19 20:49:47 -070073 for (i = nr_irqs-1; i > 0; i--) {
Yinghai Lu08678b02008-08-19 20:50:05 -070074 desc = irq_to_desc(i);
Ingo Molnar3bf52a42008-08-19 20:50:29 -070075 if (!desc)
76 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 spin_lock_irq(&desc->lock);
Thomas Gleixner3418d722006-06-29 02:24:49 -070079 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
Ingo Molnard1bef4e2006-06-29 02:24:36 -070081 if (desc->chip->startup(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 desc->status |= IRQ_PENDING;
83 }
84 spin_unlock_irq(&desc->lock);
85 }
86
87 /*
88 * Wait for spurious interrupts to trigger
89 */
Luca Falavigna47f176f2005-06-28 20:44:42 -070090 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /*
93 * Now filter out any obviously spurious interrupts
94 */
Ingo Molnar06fcb0c2006-06-29 02:24:40 -070095 mask = 0;
Yinghai Lu85c0f902008-08-19 20:49:47 -070096 for (i = 0; i < nr_irqs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 unsigned int status;
98
Yinghai Lu08678b02008-08-19 20:50:05 -070099 desc = irq_to_desc(i);
Ingo Molnar3bf52a42008-08-19 20:50:29 -0700100 if (!desc)
101 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 spin_lock_irq(&desc->lock);
103 status = desc->status;
104
105 if (status & IRQ_AUTODETECT) {
106 /* It triggered already - consider it spurious. */
107 if (!(status & IRQ_WAITING)) {
108 desc->status = status & ~IRQ_AUTODETECT;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700109 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 } else
111 if (i < 32)
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700112 mask |= 1 << i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114 spin_unlock_irq(&desc->lock);
115 }
116
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700117 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119EXPORT_SYMBOL(probe_irq_on);
120
121/**
122 * probe_irq_mask - scan a bitmap of interrupt lines
123 * @val: mask of interrupts to consider
124 *
125 * Scan the interrupt lines and return a bitmap of active
126 * autodetect interrupts. The interrupt probe logic state
127 * is then returned to its previous value.
128 *
129 * Note: we need to scan all the irq's even though we will
130 * only return autodetect irq numbers - just so that we reset
131 * them all to a known state.
132 */
133unsigned int probe_irq_mask(unsigned long val)
134{
135 unsigned int mask;
136 int i;
137
138 mask = 0;
Yinghai Lu85c0f902008-08-19 20:49:47 -0700139 for (i = 0; i < nr_irqs; i++) {
Yinghai Lu08678b02008-08-19 20:50:05 -0700140 struct irq_desc *desc = irq_to_desc(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 unsigned int status;
142
Ingo Molnar3bf52a42008-08-19 20:50:29 -0700143 if (!desc)
144 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 spin_lock_irq(&desc->lock);
146 status = desc->status;
147
148 if (status & IRQ_AUTODETECT) {
149 if (i < 16 && !(status & IRQ_WAITING))
150 mask |= 1 << i;
151
152 desc->status = status & ~IRQ_AUTODETECT;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700153 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 spin_unlock_irq(&desc->lock);
156 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700157 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 return mask & val;
160}
161EXPORT_SYMBOL(probe_irq_mask);
162
163/**
164 * probe_irq_off - end an interrupt autodetect
165 * @val: mask of potential interrupts (unused)
166 *
167 * Scans the unused interrupt lines and returns the line which
168 * appears to have triggered the interrupt. If no interrupt was
169 * found then zero is returned. If more than one interrupt is
170 * found then minus the first candidate is returned to indicate
171 * their is doubt.
172 *
173 * The interrupt probe logic state is returned to its previous
174 * value.
175 *
176 * BUGS: When used in a module (which arguably shouldn't happen)
177 * nothing prevents two IRQ probe callers from overlapping. The
178 * results of this are non-optimal.
179 */
180int probe_irq_off(unsigned long val)
181{
182 int i, irq_found = 0, nr_irqs = 0;
183
Yinghai Lu85c0f902008-08-19 20:49:47 -0700184 for (i = 0; i < nr_irqs; i++) {
Yinghai Lu08678b02008-08-19 20:50:05 -0700185 struct irq_desc *desc = irq_to_desc(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 unsigned int status;
187
Ingo Molnar3bf52a42008-08-19 20:50:29 -0700188 if (!desc)
189 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 spin_lock_irq(&desc->lock);
191 status = desc->status;
192
193 if (status & IRQ_AUTODETECT) {
194 if (!(status & IRQ_WAITING)) {
195 if (!nr_irqs)
196 irq_found = i;
197 nr_irqs++;
198 }
199 desc->status = status & ~IRQ_AUTODETECT;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700200 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 spin_unlock_irq(&desc->lock);
203 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700204 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (nr_irqs > 1)
207 irq_found = -irq_found;
Ingo Molnar74ffd552006-06-29 02:24:37 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return irq_found;
210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211EXPORT_SYMBOL(probe_irq_off);
212