Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * 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 Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 11 | #include <linux/delay.h> |
Arjan van de Ven | 22a9d64 | 2009-01-07 08:45:46 -0800 | [diff] [blame] | 12 | #include <linux/async.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Ingo Molnar | 7a55713 | 2006-06-29 02:24:54 -0700 | [diff] [blame] | 14 | #include "internals.h" |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | /* |
| 17 | * Autodetection depends on the fact that any interrupt that |
| 18 | * comes in on to an unassigned handler will get stuck with |
Thomas Gleixner | 163ef30 | 2011-02-08 11:39:15 +0100 | [diff] [blame] | 19 | * "IRQS_WAITING" cleared and the interrupt disabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | */ |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 21 | static DEFINE_MUTEX(probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 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 | */ |
| 30 | unsigned long probe_irq_on(void) |
| 31 | { |
Ingo Molnar | 34ffdb7 | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 32 | struct irq_desc *desc; |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 33 | unsigned long mask = 0; |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 34 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Arjan van de Ven | 22a9d64 | 2009-01-07 08:45:46 -0800 | [diff] [blame] | 36 | /* |
| 37 | * quiesce the kernel, or at least the asynchronous portion |
| 38 | */ |
| 39 | async_synchronize_full(); |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 40 | mutex_lock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* |
| 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 Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 45 | for_each_irq_desc_reverse(i, desc) { |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 46 | raw_spin_lock_irq(&desc->lock); |
Thomas Gleixner | 1ccb4e6 | 2011-02-09 14:44:17 +0100 | [diff] [blame] | 47 | if (!desc->action && irq_settings_can_probe(desc)) { |
Thomas Gleixner | 6a6de9e | 2006-06-29 02:24:51 -0700 | [diff] [blame] | 48 | /* |
| 49 | * Some chips need to know about probing in |
| 50 | * progress: |
| 51 | */ |
Thomas Gleixner | b2ba2c3 | 2010-09-27 12:45:47 +0000 | [diff] [blame] | 52 | if (desc->irq_data.chip->irq_set_type) |
| 53 | desc->irq_data.chip->irq_set_type(&desc->irq_data, |
| 54 | IRQ_TYPE_PROBE); |
Thomas Gleixner | c942cee | 2017-09-13 23:29:09 +0200 | [diff] [blame] | 55 | irq_activate_and_startup(desc, IRQ_NORESEND); |
Thomas Gleixner | 6a6de9e | 2006-06-29 02:24:51 -0700 | [diff] [blame] | 56 | } |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 57 | raw_spin_unlock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* Wait for longstanding interrupts to trigger. */ |
Luca Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 61 | msleep(20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 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 Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 68 | for_each_irq_desc_reverse(i, desc) { |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 69 | raw_spin_lock_irq(&desc->lock); |
Thomas Gleixner | 1ccb4e6 | 2011-02-09 14:44:17 +0100 | [diff] [blame] | 70 | if (!desc->action && irq_settings_can_probe(desc)) { |
Thomas Gleixner | 163ef30 | 2011-02-08 11:39:15 +0100 | [diff] [blame] | 71 | desc->istate |= IRQS_AUTODETECT | IRQS_WAITING; |
Thomas Gleixner | 1beaeac | 2018-01-30 19:36:32 +0100 | [diff] [blame] | 72 | if (irq_activate_and_startup(desc, IRQ_NORESEND)) |
Thomas Gleixner | 2a0d6fb | 2011-02-08 12:17:57 +0100 | [diff] [blame] | 73 | desc->istate |= IRQS_PENDING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 75 | raw_spin_unlock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Wait for spurious interrupts to trigger |
| 80 | */ |
Luca Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 81 | msleep(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * Now filter out any obviously spurious interrupts |
| 85 | */ |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 86 | for_each_irq_desc(i, desc) { |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 87 | raw_spin_lock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 89 | if (desc->istate & IRQS_AUTODETECT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* It triggered already - consider it spurious. */ |
Thomas Gleixner | 163ef30 | 2011-02-08 11:39:15 +0100 | [diff] [blame] | 91 | if (!(desc->istate & IRQS_WAITING)) { |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 92 | desc->istate &= ~IRQS_AUTODETECT; |
Thomas Gleixner | 4001d8e | 2019-06-28 13:11:49 +0200 | [diff] [blame] | 93 | irq_shutdown_and_deactivate(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } else |
| 95 | if (i < 32) |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 96 | mask |= 1 << i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 98 | raw_spin_unlock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 101 | return mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | EXPORT_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 | */ |
| 117 | unsigned int probe_irq_mask(unsigned long val) |
| 118 | { |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 119 | unsigned int mask = 0; |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 120 | struct irq_desc *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | int i; |
| 122 | |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 123 | for_each_irq_desc(i, desc) { |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 124 | raw_spin_lock_irq(&desc->lock); |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 125 | if (desc->istate & IRQS_AUTODETECT) { |
Thomas Gleixner | 163ef30 | 2011-02-08 11:39:15 +0100 | [diff] [blame] | 126 | if (i < 16 && !(desc->istate & IRQS_WAITING)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | mask |= 1 << i; |
| 128 | |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 129 | desc->istate &= ~IRQS_AUTODETECT; |
Thomas Gleixner | 4001d8e | 2019-06-28 13:11:49 +0200 | [diff] [blame] | 130 | irq_shutdown_and_deactivate(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 132 | raw_spin_unlock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 134 | mutex_unlock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | return mask & val; |
| 137 | } |
| 138 | EXPORT_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 | */ |
| 157 | int probe_irq_off(unsigned long val) |
| 158 | { |
Thomas Gleixner | 63d659d | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 159 | int i, irq_found = 0, nr_of_irqs = 0; |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 160 | struct irq_desc *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 162 | for_each_irq_desc(i, desc) { |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 163 | raw_spin_lock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 165 | if (desc->istate & IRQS_AUTODETECT) { |
Thomas Gleixner | 163ef30 | 2011-02-08 11:39:15 +0100 | [diff] [blame] | 166 | if (!(desc->istate & IRQS_WAITING)) { |
Thomas Gleixner | 63d659d | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 167 | if (!nr_of_irqs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | irq_found = i; |
Thomas Gleixner | 63d659d | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 169 | nr_of_irqs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 171 | desc->istate &= ~IRQS_AUTODETECT; |
Thomas Gleixner | 4001d8e | 2019-06-28 13:11:49 +0200 | [diff] [blame] | 172 | irq_shutdown_and_deactivate(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 174 | raw_spin_unlock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 176 | mutex_unlock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Thomas Gleixner | 63d659d | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 178 | if (nr_of_irqs > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | irq_found = -irq_found; |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | return irq_found; |
| 182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | EXPORT_SYMBOL(probe_irq_off); |
| 184 | |