blob: 2295a31ef110dab62a6e665358a9cb290fbe990e [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>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080013#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Ingo Molnar7a557132006-06-29 02:24:54 -070015#include "internals.h"
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017/*
18 * Autodetection depends on the fact that any interrupt that
19 * comes in on to an unassigned handler will get stuck with
20 * "IRQ_WAITING" cleared and the interrupt disabled.
21 */
Ingo Molnar74ffd552006-06-29 02:24:37 -070022static DEFINE_MUTEX(probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/**
25 * probe_irq_on - begin an interrupt autodetect
26 *
27 * Commence probing for an interrupt. The interrupts are scanned
28 * and a mask of potential interrupt lines is returned.
29 *
30 */
31unsigned long probe_irq_on(void)
32{
Ingo Molnar34ffdb72006-06-29 02:24:40 -070033 struct irq_desc *desc;
Thomas Gleixner10e58082008-10-16 14:19:04 +020034 unsigned long mask = 0;
35 unsigned int status;
36 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Arjan van de Ven22a9d642009-01-07 08:45:46 -080038 /*
39 * quiesce the kernel, or at least the asynchronous portion
40 */
41 async_synchronize_full();
Ingo Molnar74ffd552006-06-29 02:24:37 -070042 mutex_lock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 /*
44 * something may have generated an irq long ago and we want to
45 * flush such a longstanding irq before considering it as spurious.
46 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020047 for_each_irq_desc_reverse(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +010048 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070049 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
50 /*
Ingo Molnar7a557132006-06-29 02:24:54 -070051 * An old-style architecture might still have
52 * the handle_bad_irq handler there:
53 */
54 compat_irq_chip_set_default_handler(desc);
55
56 /*
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070057 * Some chips need to know about probing in
58 * progress:
59 */
60 if (desc->chip->set_type)
61 desc->chip->set_type(i, IRQ_TYPE_PROBE);
Ingo Molnar06fcb0c2006-06-29 02:24:40 -070062 desc->chip->startup(i);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070063 }
Thomas Gleixner239007b2009-11-17 16:46:45 +010064 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66
67 /* Wait for longstanding interrupts to trigger. */
Luca Falavigna47f176f2005-06-28 20:44:42 -070068 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 /*
71 * enable any unassigned irqs
72 * (we must startup again here because if a longstanding irq
73 * happened in the previous stage, it may have masked itself)
74 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020075 for_each_irq_desc_reverse(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +010076 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner3418d722006-06-29 02:24:49 -070077 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
Ingo Molnard1bef4e2006-06-29 02:24:36 -070079 if (desc->chip->startup(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 desc->status |= IRQ_PENDING;
81 }
Thomas Gleixner239007b2009-11-17 16:46:45 +010082 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
85 /*
86 * Wait for spurious interrupts to trigger
87 */
Luca Falavigna47f176f2005-06-28 20:44:42 -070088 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 /*
91 * Now filter out any obviously spurious interrupts
92 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020093 for_each_irq_desc(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +010094 raw_spin_lock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 status = desc->status;
96
97 if (status & IRQ_AUTODETECT) {
98 /* It triggered already - consider it spurious. */
99 if (!(status & IRQ_WAITING)) {
100 desc->status = status & ~IRQ_AUTODETECT;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700101 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 } else
103 if (i < 32)
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700104 mask |= 1 << i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Thomas Gleixner239007b2009-11-17 16:46:45 +0100106 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700109 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111EXPORT_SYMBOL(probe_irq_on);
112
113/**
114 * probe_irq_mask - scan a bitmap of interrupt lines
115 * @val: mask of interrupts to consider
116 *
117 * Scan the interrupt lines and return a bitmap of active
118 * autodetect interrupts. The interrupt probe logic state
119 * is then returned to its previous value.
120 *
121 * Note: we need to scan all the irq's even though we will
122 * only return autodetect irq numbers - just so that we reset
123 * them all to a known state.
124 */
125unsigned int probe_irq_mask(unsigned long val)
126{
Thomas Gleixner10e58082008-10-16 14:19:04 +0200127 unsigned int status, mask = 0;
128 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int i;
130
Thomas Gleixner10e58082008-10-16 14:19:04 +0200131 for_each_irq_desc(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +0100132 raw_spin_lock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 status = desc->status;
134
135 if (status & IRQ_AUTODETECT) {
136 if (i < 16 && !(status & IRQ_WAITING))
137 mask |= 1 << i;
138
139 desc->status = status & ~IRQ_AUTODETECT;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700140 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Thomas Gleixner239007b2009-11-17 16:46:45 +0100142 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700144 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 return mask & val;
147}
148EXPORT_SYMBOL(probe_irq_mask);
149
150/**
151 * probe_irq_off - end an interrupt autodetect
152 * @val: mask of potential interrupts (unused)
153 *
154 * Scans the unused interrupt lines and returns the line which
155 * appears to have triggered the interrupt. If no interrupt was
156 * found then zero is returned. If more than one interrupt is
157 * found then minus the first candidate is returned to indicate
158 * their is doubt.
159 *
160 * The interrupt probe logic state is returned to its previous
161 * value.
162 *
163 * BUGS: When used in a module (which arguably shouldn't happen)
164 * nothing prevents two IRQ probe callers from overlapping. The
165 * results of this are non-optimal.
166 */
167int probe_irq_off(unsigned long val)
168{
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200169 int i, irq_found = 0, nr_of_irqs = 0;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200170 struct irq_desc *desc;
171 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Thomas Gleixner10e58082008-10-16 14:19:04 +0200173 for_each_irq_desc(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +0100174 raw_spin_lock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 status = desc->status;
176
177 if (status & IRQ_AUTODETECT) {
178 if (!(status & IRQ_WAITING)) {
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200179 if (!nr_of_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 irq_found = i;
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200181 nr_of_irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 desc->status = status & ~IRQ_AUTODETECT;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700184 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
Thomas Gleixner239007b2009-11-17 16:46:45 +0100186 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700188 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200190 if (nr_of_irqs > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 irq_found = -irq_found;
Ingo Molnar74ffd552006-06-29 02:24:37 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return irq_found;
194}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195EXPORT_SYMBOL(probe_irq_off);
196