Merge tag 'spdx_identifiers-4.14-rc8' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / kernel / irq / autoprobe.c
1 // SPDX-License-Identifier: GPL-2.0
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>
13 #include <linux/delay.h>
14 #include <linux/async.h>
15
16 #include "internals.h"
17
18 /*
19  * Autodetection depends on the fact that any interrupt that
20  * comes in on to an unassigned handler will get stuck with
21  * "IRQS_WAITING" cleared and the interrupt disabled.
22  */
23 static DEFINE_MUTEX(probing_active);
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  */
32 unsigned long probe_irq_on(void)
33 {
34         struct irq_desc *desc;
35         unsigned long mask = 0;
36         int i;
37
38         /*
39          * quiesce the kernel, or at least the asynchronous portion
40          */
41         async_synchronize_full();
42         mutex_lock(&probing_active);
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          */
47         for_each_irq_desc_reverse(i, desc) {
48                 raw_spin_lock_irq(&desc->lock);
49                 if (!desc->action && irq_settings_can_probe(desc)) {
50                         /*
51                          * Some chips need to know about probing in
52                          * progress:
53                          */
54                         if (desc->irq_data.chip->irq_set_type)
55                                 desc->irq_data.chip->irq_set_type(&desc->irq_data,
56                                                          IRQ_TYPE_PROBE);
57                         irq_startup(desc, IRQ_NORESEND, IRQ_START_FORCE);
58                 }
59                 raw_spin_unlock_irq(&desc->lock);
60         }
61
62         /* Wait for longstanding interrupts to trigger. */
63         msleep(20);
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          */
70         for_each_irq_desc_reverse(i, desc) {
71                 raw_spin_lock_irq(&desc->lock);
72                 if (!desc->action && irq_settings_can_probe(desc)) {
73                         desc->istate |= IRQS_AUTODETECT | IRQS_WAITING;
74                         if (irq_startup(desc, IRQ_NORESEND, IRQ_START_FORCE))
75                                 desc->istate |= IRQS_PENDING;
76                 }
77                 raw_spin_unlock_irq(&desc->lock);
78         }
79
80         /*
81          * Wait for spurious interrupts to trigger
82          */
83         msleep(100);
84
85         /*
86          * Now filter out any obviously spurious interrupts
87          */
88         for_each_irq_desc(i, desc) {
89                 raw_spin_lock_irq(&desc->lock);
90
91                 if (desc->istate & IRQS_AUTODETECT) {
92                         /* It triggered already - consider it spurious. */
93                         if (!(desc->istate & IRQS_WAITING)) {
94                                 desc->istate &= ~IRQS_AUTODETECT;
95                                 irq_shutdown(desc);
96                         } else
97                                 if (i < 32)
98                                         mask |= 1 << i;
99                 }
100                 raw_spin_unlock_irq(&desc->lock);
101         }
102
103         return mask;
104 }
105 EXPORT_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  */
119 unsigned int probe_irq_mask(unsigned long val)
120 {
121         unsigned int mask = 0;
122         struct irq_desc *desc;
123         int i;
124
125         for_each_irq_desc(i, desc) {
126                 raw_spin_lock_irq(&desc->lock);
127                 if (desc->istate & IRQS_AUTODETECT) {
128                         if (i < 16 && !(desc->istate & IRQS_WAITING))
129                                 mask |= 1 << i;
130
131                         desc->istate &= ~IRQS_AUTODETECT;
132                         irq_shutdown(desc);
133                 }
134                 raw_spin_unlock_irq(&desc->lock);
135         }
136         mutex_unlock(&probing_active);
137
138         return mask & val;
139 }
140 EXPORT_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  */
159 int probe_irq_off(unsigned long val)
160 {
161         int i, irq_found = 0, nr_of_irqs = 0;
162         struct irq_desc *desc;
163
164         for_each_irq_desc(i, desc) {
165                 raw_spin_lock_irq(&desc->lock);
166
167                 if (desc->istate & IRQS_AUTODETECT) {
168                         if (!(desc->istate & IRQS_WAITING)) {
169                                 if (!nr_of_irqs)
170                                         irq_found = i;
171                                 nr_of_irqs++;
172                         }
173                         desc->istate &= ~IRQS_AUTODETECT;
174                         irq_shutdown(desc);
175                 }
176                 raw_spin_unlock_irq(&desc->lock);
177         }
178         mutex_unlock(&probing_active);
179
180         if (nr_of_irqs > 1)
181                 irq_found = -irq_found;
182
183         return irq_found;
184 }
185 EXPORT_SYMBOL(probe_irq_off);
186