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