blob: ae60cae24e9aa3db1a6ac950c474e83854f73b37 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
4 *
5 * This file contains the interrupt probing code and driver APIs.
6 */
7
8#include <linux/irq.h>
9#include <linux/module.h>
10#include <linux/interrupt.h>
Luca Falavigna47f176f2005-06-28 20:44:42 -070011#include <linux/delay.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080012#include <linux/async.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
Thomas Gleixner163ef302011-02-08 11:39:15 +010019 * "IRQS_WAITING" cleared and the interrupt disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
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;
Thomas Gleixner10e58082008-10-16 14:19:04 +020034 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Arjan van de Ven22a9d642009-01-07 08:45:46 -080036 /*
37 * quiesce the kernel, or at least the asynchronous portion
38 */
39 async_synchronize_full();
Ingo Molnar74ffd552006-06-29 02:24:37 -070040 mutex_lock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 /*
42 * something may have generated an irq long ago and we want to
43 * flush such a longstanding irq before considering it as spurious.
44 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020045 for_each_irq_desc_reverse(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +010046 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +010047 if (!desc->action && irq_settings_can_probe(desc)) {
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070048 /*
49 * Some chips need to know about probing in
50 * progress:
51 */
Thomas Gleixnerb2ba2c32010-09-27 12:45:47 +000052 if (desc->irq_data.chip->irq_set_type)
53 desc->irq_data.chip->irq_set_type(&desc->irq_data,
54 IRQ_TYPE_PROBE);
Thomas Gleixnerc942cee2017-09-13 23:29:09 +020055 irq_activate_and_startup(desc, IRQ_NORESEND);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070056 }
Thomas Gleixner239007b2009-11-17 16:46:45 +010057 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
59
60 /* Wait for longstanding interrupts to trigger. */
Luca Falavigna47f176f2005-06-28 20:44:42 -070061 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 /*
64 * enable any unassigned irqs
65 * (we must startup again here because if a longstanding irq
66 * happened in the previous stage, it may have masked itself)
67 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020068 for_each_irq_desc_reverse(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +010069 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +010070 if (!desc->action && irq_settings_can_probe(desc)) {
Thomas Gleixner163ef302011-02-08 11:39:15 +010071 desc->istate |= IRQS_AUTODETECT | IRQS_WAITING;
Thomas Gleixner1beaeac2018-01-30 19:36:32 +010072 if (irq_activate_and_startup(desc, IRQ_NORESEND))
Thomas Gleixner2a0d6fb2011-02-08 12:17:57 +010073 desc->istate |= IRQS_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
Thomas Gleixner239007b2009-11-17 16:46:45 +010075 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
78 /*
79 * Wait for spurious interrupts to trigger
80 */
Luca Falavigna47f176f2005-06-28 20:44:42 -070081 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /*
84 * Now filter out any obviously spurious interrupts
85 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020086 for_each_irq_desc(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +010087 raw_spin_lock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Thomas Gleixnerbd062e72011-02-07 20:25:25 +010089 if (desc->istate & IRQS_AUTODETECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /* It triggered already - consider it spurious. */
Thomas Gleixner163ef302011-02-08 11:39:15 +010091 if (!(desc->istate & IRQS_WAITING)) {
Thomas Gleixnerbd062e72011-02-07 20:25:25 +010092 desc->istate &= ~IRQS_AUTODETECT;
Thomas Gleixner4001d8e2019-06-28 13:11:49 +020093 irq_shutdown_and_deactivate(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 } else
95 if (i < 32)
Ingo Molnar06fcb0c2006-06-29 02:24:40 -070096 mask |= 1 << i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
Thomas Gleixner239007b2009-11-17 16:46:45 +010098 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700101 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103EXPORT_SYMBOL(probe_irq_on);
104
105/**
106 * probe_irq_mask - scan a bitmap of interrupt lines
107 * @val: mask of interrupts to consider
108 *
109 * Scan the interrupt lines and return a bitmap of active
110 * autodetect interrupts. The interrupt probe logic state
111 * is then returned to its previous value.
112 *
113 * Note: we need to scan all the irq's even though we will
114 * only return autodetect irq numbers - just so that we reset
115 * them all to a known state.
116 */
117unsigned int probe_irq_mask(unsigned long val)
118{
Thomas Gleixnerbd062e72011-02-07 20:25:25 +0100119 unsigned int mask = 0;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200120 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int i;
122
Thomas Gleixner10e58082008-10-16 14:19:04 +0200123 for_each_irq_desc(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +0100124 raw_spin_lock_irq(&desc->lock);
Thomas Gleixnerbd062e72011-02-07 20:25:25 +0100125 if (desc->istate & IRQS_AUTODETECT) {
Thomas Gleixner163ef302011-02-08 11:39:15 +0100126 if (i < 16 && !(desc->istate & IRQS_WAITING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 mask |= 1 << i;
128
Thomas Gleixnerbd062e72011-02-07 20:25:25 +0100129 desc->istate &= ~IRQS_AUTODETECT;
Thomas Gleixner4001d8e2019-06-28 13:11:49 +0200130 irq_shutdown_and_deactivate(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Thomas Gleixner239007b2009-11-17 16:46:45 +0100132 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700134 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 return mask & val;
137}
138EXPORT_SYMBOL(probe_irq_mask);
139
140/**
141 * probe_irq_off - end an interrupt autodetect
142 * @val: mask of potential interrupts (unused)
143 *
144 * Scans the unused interrupt lines and returns the line which
145 * appears to have triggered the interrupt. If no interrupt was
146 * found then zero is returned. If more than one interrupt is
147 * found then minus the first candidate is returned to indicate
148 * their is doubt.
149 *
150 * The interrupt probe logic state is returned to its previous
151 * value.
152 *
153 * BUGS: When used in a module (which arguably shouldn't happen)
154 * nothing prevents two IRQ probe callers from overlapping. The
155 * results of this are non-optimal.
156 */
157int probe_irq_off(unsigned long val)
158{
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200159 int i, irq_found = 0, nr_of_irqs = 0;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200160 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Thomas Gleixner10e58082008-10-16 14:19:04 +0200162 for_each_irq_desc(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +0100163 raw_spin_lock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Thomas Gleixnerbd062e72011-02-07 20:25:25 +0100165 if (desc->istate & IRQS_AUTODETECT) {
Thomas Gleixner163ef302011-02-08 11:39:15 +0100166 if (!(desc->istate & IRQS_WAITING)) {
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200167 if (!nr_of_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 irq_found = i;
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200169 nr_of_irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
Thomas Gleixnerbd062e72011-02-07 20:25:25 +0100171 desc->istate &= ~IRQS_AUTODETECT;
Thomas Gleixner4001d8e2019-06-28 13:11:49 +0200172 irq_shutdown_and_deactivate(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
Thomas Gleixner239007b2009-11-17 16:46:45 +0100174 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700176 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200178 if (nr_of_irqs > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 irq_found = -irq_found;
Ingo Molnar74ffd552006-06-29 02:24:37 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return irq_found;
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183EXPORT_SYMBOL(probe_irq_off);
184