blob: 394784c57060257e77666fdb9173697cf1a35092 [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
Thomas Gleixner163ef302011-02-08 11:39:15 +010020 * "IRQS_WAITING" cleared and the interrupt disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
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;
Thomas Gleixner10e58082008-10-16 14:19:04 +020035 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Arjan van de Ven22a9d642009-01-07 08:45:46 -080037 /*
38 * quiesce the kernel, or at least the asynchronous portion
39 */
40 async_synchronize_full();
Ingo Molnar74ffd552006-06-29 02:24:37 -070041 mutex_lock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 /*
43 * something may have generated an irq long ago and we want to
44 * flush such a longstanding irq before considering it as spurious.
45 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020046 for_each_irq_desc_reverse(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +010047 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +010048 if (!desc->action && irq_settings_can_probe(desc)) {
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070049 /*
50 * Some chips need to know about probing in
51 * progress:
52 */
Thomas Gleixnerb2ba2c32010-09-27 12:45:47 +000053 if (desc->irq_data.chip->irq_set_type)
54 desc->irq_data.chip->irq_set_type(&desc->irq_data,
55 IRQ_TYPE_PROBE);
Thomas Gleixner46999232011-02-02 21:41:14 +000056 irq_startup(desc);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070057 }
Thomas Gleixner239007b2009-11-17 16:46:45 +010058 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60
61 /* Wait for longstanding interrupts to trigger. */
Luca Falavigna47f176f2005-06-28 20:44:42 -070062 msleep(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /*
65 * enable any unassigned irqs
66 * (we must startup again here because if a longstanding irq
67 * happened in the previous stage, it may have masked itself)
68 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020069 for_each_irq_desc_reverse(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +010070 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +010071 if (!desc->action && irq_settings_can_probe(desc)) {
Thomas Gleixner163ef302011-02-08 11:39:15 +010072 desc->istate |= IRQS_AUTODETECT | IRQS_WAITING;
Thomas Gleixner2a0d6fb2011-02-08 12:17:57 +010073 if (irq_startup(desc)) {
74 irq_compat_set_pending(desc);
75 desc->istate |= IRQS_PENDING;
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
Thomas Gleixner239007b2009-11-17 16:46:45 +010078 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
80
81 /*
82 * Wait for spurious interrupts to trigger
83 */
Luca Falavigna47f176f2005-06-28 20:44:42 -070084 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /*
87 * Now filter out any obviously spurious interrupts
88 */
Thomas Gleixner10e58082008-10-16 14:19:04 +020089 for_each_irq_desc(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +010090 raw_spin_lock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Thomas Gleixnerbd062e72011-02-07 20:25:25 +010092 if (desc->istate & IRQS_AUTODETECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* It triggered already - consider it spurious. */
Thomas Gleixner163ef302011-02-08 11:39:15 +010094 if (!(desc->istate & IRQS_WAITING)) {
Thomas Gleixnerbd062e72011-02-07 20:25:25 +010095 desc->istate &= ~IRQS_AUTODETECT;
Thomas Gleixner46999232011-02-02 21:41:14 +000096 irq_shutdown(desc);
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 }
Thomas Gleixner239007b2009-11-17 16:46:45 +0100101 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
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 Gleixnerbd062e72011-02-07 20:25:25 +0100122 unsigned int mask = 0;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200123 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) {
Thomas Gleixner239007b2009-11-17 16:46:45 +0100127 raw_spin_lock_irq(&desc->lock);
Thomas Gleixnerbd062e72011-02-07 20:25:25 +0100128 if (desc->istate & IRQS_AUTODETECT) {
Thomas Gleixner163ef302011-02-08 11:39:15 +0100129 if (i < 16 && !(desc->istate & IRQS_WAITING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 mask |= 1 << i;
131
Thomas Gleixnerbd062e72011-02-07 20:25:25 +0100132 desc->istate &= ~IRQS_AUTODETECT;
Thomas Gleixner46999232011-02-02 21:41:14 +0000133 irq_shutdown(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Thomas Gleixner239007b2009-11-17 16:46:45 +0100135 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700137 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 return mask & val;
140}
141EXPORT_SYMBOL(probe_irq_mask);
142
143/**
144 * probe_irq_off - end an interrupt autodetect
145 * @val: mask of potential interrupts (unused)
146 *
147 * Scans the unused interrupt lines and returns the line which
148 * appears to have triggered the interrupt. If no interrupt was
149 * found then zero is returned. If more than one interrupt is
150 * found then minus the first candidate is returned to indicate
151 * their is doubt.
152 *
153 * The interrupt probe logic state is returned to its previous
154 * value.
155 *
156 * BUGS: When used in a module (which arguably shouldn't happen)
157 * nothing prevents two IRQ probe callers from overlapping. The
158 * results of this are non-optimal.
159 */
160int probe_irq_off(unsigned long val)
161{
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200162 int i, irq_found = 0, nr_of_irqs = 0;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200163 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Thomas Gleixner10e58082008-10-16 14:19:04 +0200165 for_each_irq_desc(i, desc) {
Thomas Gleixner239007b2009-11-17 16:46:45 +0100166 raw_spin_lock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Thomas Gleixnerbd062e72011-02-07 20:25:25 +0100168 if (desc->istate & IRQS_AUTODETECT) {
Thomas Gleixner163ef302011-02-08 11:39:15 +0100169 if (!(desc->istate & IRQS_WAITING)) {
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200170 if (!nr_of_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 irq_found = i;
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200172 nr_of_irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
Thomas Gleixnerbd062e72011-02-07 20:25:25 +0100174 desc->istate &= ~IRQS_AUTODETECT;
Thomas Gleixner46999232011-02-02 21:41:14 +0000175 irq_shutdown(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Thomas Gleixner239007b2009-11-17 16:46:45 +0100177 raw_spin_unlock_irq(&desc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Ingo Molnar74ffd552006-06-29 02:24:37 -0700179 mutex_unlock(&probing_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Thomas Gleixner63d659d2008-10-16 15:33:18 +0200181 if (nr_of_irqs > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 irq_found = -irq_found;
Ingo Molnar74ffd552006-06-29 02:24:37 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return irq_found;
185}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186EXPORT_SYMBOL(probe_irq_off);
187