blob: cc0f7321b8cede4192a4ceb9ffc97311ec9cc7d0 [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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 spin_lock_irq(&desc->lock);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070044 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
45 /*
Ingo Molnar7a557132006-06-29 02:24:54 -070046 * An old-style architecture might still have
47 * the handle_bad_irq handler there:
48 */
49 compat_irq_chip_set_default_handler(desc);
50
51 /*
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070052 * 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);
Ingo Molnar06fcb0c2006-06-29 02:24:40 -070057 desc->chip->startup(i);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 spin_unlock_irq(&desc->lock);
60 }
61
62 /* Wait for longstanding interrupts to trigger. */
Luca Falavigna47f176f2005-06-28 20:44:42 -070063 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
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 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020070 for_each_irq_desc_reverse(i, desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 spin_lock_irq(&desc->lock);
Thomas Gleixner3418d722006-06-29 02:24:49 -070072 if (!desc->action && !(desc->status & IRQ_NOPROBE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
Ingo Molnard1bef4e2006-06-29 02:24:36 -070074 if (desc->chip->startup(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 desc->status |= IRQ_PENDING;
76 }
77 spin_unlock_irq(&desc->lock);
78 }
79
80 /*
81 * Wait for spurious interrupts to trigger
82 */
Luca Falavigna47f176f2005-06-28 20:44:42 -070083 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /*
86 * Now filter out any obviously spurious interrupts
87 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020088 for_each_irq_desc(i, desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 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;
Ingo Molnard1bef4e2006-06-29 02:24:36 -070096 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 } else
98 if (i < 32)
Ingo Molnar06fcb0c2006-06-29 02:24:40 -070099 mask |= 1 << i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101 spin_unlock_irq(&desc->lock);
102 }
103
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700104 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106EXPORT_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 */
120unsigned int probe_irq_mask(unsigned long val)
121{
Thomas Gleixner10e58082008-10-16 14:19:04 +0200122 unsigned int status, mask = 0;
123 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int i;
125
Thomas Gleixner10e58082008-10-16 14:19:04 +0200126 for_each_irq_desc(i, desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 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;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700135 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 spin_unlock_irq(&desc->lock);
138 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700139 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 return mask & val;
142}
143EXPORT_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 */
162int probe_irq_off(unsigned long val)
163{
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200164 int i, irq_found = 0, nr_of_irqs = 0;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200165 struct irq_desc *desc;
166 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Thomas Gleixner10e58082008-10-16 14:19:04 +0200168 for_each_irq_desc(i, desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 spin_lock_irq(&desc->lock);
170 status = desc->status;
171
172 if (status & IRQ_AUTODETECT) {
173 if (!(status & IRQ_WAITING)) {
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200174 if (!nr_of_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 irq_found = i;
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200176 nr_of_irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 desc->status = status & ~IRQ_AUTODETECT;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700179 desc->chip->shutdown(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181 spin_unlock_irq(&desc->lock);
182 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700183 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200185 if (nr_of_irqs > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 irq_found = -irq_found;
Ingo Molnar74ffd552006-06-29 02:24:37 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return irq_found;
189}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190EXPORT_SYMBOL(probe_irq_off);
191