blob: 650ce4102a6333d9ddf097692583e55659a25333 [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;
Thomas Gleixner10e58082008-10-16 14:19:04 +020033 unsigned long mask = 0;
34 unsigned int status;
35 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Ingo Molnar74ffd552006-06-29 02:24:37 -070037 mutex_lock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 /*
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 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020042 for_each_irq_desc_reverse(i, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080043 if (!desc)
44 continue;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 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 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020073 for_each_irq_desc_reverse(i, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080074 if (!desc)
75 continue;
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 spin_lock_irq(&desc->lock);
Thomas Gleixner3418d722006-06-29 02:24:49 -070078 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
Ingo Molnard1bef4e2006-06-29 02:24:36 -070080 if (desc->chip->startup(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 desc->status |= IRQ_PENDING;
82 }
83 spin_unlock_irq(&desc->lock);
84 }
85
86 /*
87 * Wait for spurious interrupts to trigger
88 */
Luca Falavigna47f176f2005-06-28 20:44:42 -070089 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 /*
92 * Now filter out any obviously spurious interrupts
93 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020094 for_each_irq_desc(i, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080095 if (!desc)
96 continue;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 spin_lock_irq(&desc->lock);
99 status = desc->status;
100
101 if (status & IRQ_AUTODETECT) {
102 /* It triggered already - consider it spurious. */
103 if (!(status & IRQ_WAITING)) {
104 desc->status = status & ~IRQ_AUTODETECT;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700105 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 } else
107 if (i < 32)
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700108 mask |= 1 << i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110 spin_unlock_irq(&desc->lock);
111 }
112
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700113 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115EXPORT_SYMBOL(probe_irq_on);
116
117/**
118 * probe_irq_mask - scan a bitmap of interrupt lines
119 * @val: mask of interrupts to consider
120 *
121 * Scan the interrupt lines and return a bitmap of active
122 * autodetect interrupts. The interrupt probe logic state
123 * is then returned to its previous value.
124 *
125 * Note: we need to scan all the irq's even though we will
126 * only return autodetect irq numbers - just so that we reset
127 * them all to a known state.
128 */
129unsigned int probe_irq_mask(unsigned long val)
130{
Thomas Gleixner10e58082008-10-16 14:19:04 +0200131 unsigned int status, mask = 0;
132 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int i;
134
Thomas Gleixner10e58082008-10-16 14:19:04 +0200135 for_each_irq_desc(i, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800136 if (!desc)
137 continue;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 spin_lock_irq(&desc->lock);
140 status = desc->status;
141
142 if (status & IRQ_AUTODETECT) {
143 if (i < 16 && !(status & IRQ_WAITING))
144 mask |= 1 << i;
145
146 desc->status = status & ~IRQ_AUTODETECT;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700147 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149 spin_unlock_irq(&desc->lock);
150 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700151 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 return mask & val;
154}
155EXPORT_SYMBOL(probe_irq_mask);
156
157/**
158 * probe_irq_off - end an interrupt autodetect
159 * @val: mask of potential interrupts (unused)
160 *
161 * Scans the unused interrupt lines and returns the line which
162 * appears to have triggered the interrupt. If no interrupt was
163 * found then zero is returned. If more than one interrupt is
164 * found then minus the first candidate is returned to indicate
165 * their is doubt.
166 *
167 * The interrupt probe logic state is returned to its previous
168 * value.
169 *
170 * BUGS: When used in a module (which arguably shouldn't happen)
171 * nothing prevents two IRQ probe callers from overlapping. The
172 * results of this are non-optimal.
173 */
174int probe_irq_off(unsigned long val)
175{
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200176 int i, irq_found = 0, nr_of_irqs = 0;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200177 struct irq_desc *desc;
178 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Thomas Gleixner10e58082008-10-16 14:19:04 +0200180 for_each_irq_desc(i, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800181 if (!desc)
182 continue;
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 spin_lock_irq(&desc->lock);
185 status = desc->status;
186
187 if (status & IRQ_AUTODETECT) {
188 if (!(status & IRQ_WAITING)) {
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200189 if (!nr_of_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 irq_found = i;
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200191 nr_of_irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193 desc->status = status & ~IRQ_AUTODETECT;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700194 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 spin_unlock_irq(&desc->lock);
197 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700198 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200200 if (nr_of_irqs > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 irq_found = -irq_found;
Ingo Molnar74ffd552006-06-29 02:24:37 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return irq_found;
204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205EXPORT_SYMBOL(probe_irq_off);
206