Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 12 | #include <linux/delay.h> |
Arjan van de Ven | 22a9d64 | 2009-01-07 08:45:46 -0800 | [diff] [blame] | 13 | #include <linux/async.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Ingo Molnar | 7a55713 | 2006-06-29 02:24:54 -0700 | [diff] [blame] | 15 | #include "internals.h" |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | /* |
| 18 | * Autodetection depends on the fact that any interrupt that |
| 19 | * comes in on to an unassigned handler will get stuck with |
Thomas Gleixner | 163ef30 | 2011-02-08 11:39:15 +0100 | [diff] [blame^] | 20 | * "IRQS_WAITING" cleared and the interrupt disabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | */ |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 22 | static DEFINE_MUTEX(probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 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 | */ |
| 31 | unsigned long probe_irq_on(void) |
| 32 | { |
Ingo Molnar | 34ffdb7 | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 33 | struct irq_desc *desc; |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 34 | unsigned long mask = 0; |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 35 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Arjan van de Ven | 22a9d64 | 2009-01-07 08:45:46 -0800 | [diff] [blame] | 37 | /* |
| 38 | * quiesce the kernel, or at least the asynchronous portion |
| 39 | */ |
| 40 | async_synchronize_full(); |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 41 | mutex_lock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | /* |
| 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 Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 46 | for_each_irq_desc_reverse(i, desc) { |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 47 | raw_spin_lock_irq(&desc->lock); |
Thomas Gleixner | 6a6de9e | 2006-06-29 02:24:51 -0700 | [diff] [blame] | 48 | if (!desc->action && !(desc->status & IRQ_NOPROBE)) { |
| 49 | /* |
Ingo Molnar | 7a55713 | 2006-06-29 02:24:54 -0700 | [diff] [blame] | 50 | * An old-style architecture might still have |
| 51 | * the handle_bad_irq handler there: |
| 52 | */ |
| 53 | compat_irq_chip_set_default_handler(desc); |
| 54 | |
| 55 | /* |
Thomas Gleixner | 6a6de9e | 2006-06-29 02:24:51 -0700 | [diff] [blame] | 56 | * Some chips need to know about probing in |
| 57 | * progress: |
| 58 | */ |
Thomas Gleixner | b2ba2c3 | 2010-09-27 12:45:47 +0000 | [diff] [blame] | 59 | if (desc->irq_data.chip->irq_set_type) |
| 60 | desc->irq_data.chip->irq_set_type(&desc->irq_data, |
| 61 | IRQ_TYPE_PROBE); |
Thomas Gleixner | 4699923 | 2011-02-02 21:41:14 +0000 | [diff] [blame] | 62 | irq_startup(desc); |
Thomas Gleixner | 6a6de9e | 2006-06-29 02:24:51 -0700 | [diff] [blame] | 63 | } |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 64 | raw_spin_unlock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* Wait for longstanding interrupts to trigger. */ |
Luca Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 68 | msleep(20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * enable any unassigned irqs |
| 72 | * (we must startup again here because if a longstanding irq |
| 73 | * happened in the previous stage, it may have masked itself) |
| 74 | */ |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 75 | for_each_irq_desc_reverse(i, desc) { |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 76 | raw_spin_lock_irq(&desc->lock); |
Thomas Gleixner | 3418d72 | 2006-06-29 02:24:49 -0700 | [diff] [blame] | 77 | if (!desc->action && !(desc->status & IRQ_NOPROBE)) { |
Thomas Gleixner | 163ef30 | 2011-02-08 11:39:15 +0100 | [diff] [blame^] | 78 | desc->istate |= IRQS_AUTODETECT | IRQS_WAITING; |
Thomas Gleixner | 4699923 | 2011-02-02 21:41:14 +0000 | [diff] [blame] | 79 | if (irq_startup(desc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | desc->status |= IRQ_PENDING; |
| 81 | } |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 82 | raw_spin_unlock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Wait for spurious interrupts to trigger |
| 87 | */ |
Luca Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 88 | msleep(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Now filter out any obviously spurious interrupts |
| 92 | */ |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 93 | for_each_irq_desc(i, desc) { |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 94 | raw_spin_lock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 96 | if (desc->istate & IRQS_AUTODETECT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* It triggered already - consider it spurious. */ |
Thomas Gleixner | 163ef30 | 2011-02-08 11:39:15 +0100 | [diff] [blame^] | 98 | if (!(desc->istate & IRQS_WAITING)) { |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 99 | desc->istate &= ~IRQS_AUTODETECT; |
Thomas Gleixner | 4699923 | 2011-02-02 21:41:14 +0000 | [diff] [blame] | 100 | irq_shutdown(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } else |
| 102 | if (i < 32) |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 103 | mask |= 1 << i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 105 | raw_spin_unlock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 108 | return mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | EXPORT_SYMBOL(probe_irq_on); |
| 111 | |
| 112 | /** |
| 113 | * probe_irq_mask - scan a bitmap of interrupt lines |
| 114 | * @val: mask of interrupts to consider |
| 115 | * |
| 116 | * Scan the interrupt lines and return a bitmap of active |
| 117 | * autodetect interrupts. The interrupt probe logic state |
| 118 | * is then returned to its previous value. |
| 119 | * |
| 120 | * Note: we need to scan all the irq's even though we will |
| 121 | * only return autodetect irq numbers - just so that we reset |
| 122 | * them all to a known state. |
| 123 | */ |
| 124 | unsigned int probe_irq_mask(unsigned long val) |
| 125 | { |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 126 | unsigned int mask = 0; |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 127 | struct irq_desc *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | int i; |
| 129 | |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 130 | for_each_irq_desc(i, desc) { |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 131 | raw_spin_lock_irq(&desc->lock); |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 132 | if (desc->istate & IRQS_AUTODETECT) { |
Thomas Gleixner | 163ef30 | 2011-02-08 11:39:15 +0100 | [diff] [blame^] | 133 | if (i < 16 && !(desc->istate & IRQS_WAITING)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | mask |= 1 << i; |
| 135 | |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 136 | desc->istate &= ~IRQS_AUTODETECT; |
Thomas Gleixner | 4699923 | 2011-02-02 21:41:14 +0000 | [diff] [blame] | 137 | irq_shutdown(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 139 | raw_spin_unlock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 141 | mutex_unlock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | return mask & val; |
| 144 | } |
| 145 | EXPORT_SYMBOL(probe_irq_mask); |
| 146 | |
| 147 | /** |
| 148 | * probe_irq_off - end an interrupt autodetect |
| 149 | * @val: mask of potential interrupts (unused) |
| 150 | * |
| 151 | * Scans the unused interrupt lines and returns the line which |
| 152 | * appears to have triggered the interrupt. If no interrupt was |
| 153 | * found then zero is returned. If more than one interrupt is |
| 154 | * found then minus the first candidate is returned to indicate |
| 155 | * their is doubt. |
| 156 | * |
| 157 | * The interrupt probe logic state is returned to its previous |
| 158 | * value. |
| 159 | * |
| 160 | * BUGS: When used in a module (which arguably shouldn't happen) |
| 161 | * nothing prevents two IRQ probe callers from overlapping. The |
| 162 | * results of this are non-optimal. |
| 163 | */ |
| 164 | int probe_irq_off(unsigned long val) |
| 165 | { |
Thomas Gleixner | 63d659d | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 166 | int i, irq_found = 0, nr_of_irqs = 0; |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 167 | struct irq_desc *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 169 | for_each_irq_desc(i, desc) { |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 170 | raw_spin_lock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 172 | if (desc->istate & IRQS_AUTODETECT) { |
Thomas Gleixner | 163ef30 | 2011-02-08 11:39:15 +0100 | [diff] [blame^] | 173 | if (!(desc->istate & IRQS_WAITING)) { |
Thomas Gleixner | 63d659d | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 174 | if (!nr_of_irqs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | irq_found = i; |
Thomas Gleixner | 63d659d | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 176 | nr_of_irqs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
Thomas Gleixner | bd062e7 | 2011-02-07 20:25:25 +0100 | [diff] [blame] | 178 | desc->istate &= ~IRQS_AUTODETECT; |
Thomas Gleixner | 4699923 | 2011-02-02 21:41:14 +0000 | [diff] [blame] | 179 | irq_shutdown(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
Thomas Gleixner | 239007b | 2009-11-17 16:46:45 +0100 | [diff] [blame] | 181 | raw_spin_unlock_irq(&desc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 183 | mutex_unlock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Thomas Gleixner | 63d659d | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 185 | if (nr_of_irqs > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | irq_found = -irq_found; |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | return irq_found; |
| 189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | EXPORT_SYMBOL(probe_irq_off); |
| 191 | |