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> |
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 |
| 19 | * "IRQ_WAITING" cleared and the interrupt disabled. |
| 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; |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 33 | unsigned long mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | unsigned int i; |
| 35 | |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 36 | mutex_lock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | /* |
| 38 | * something may have generated an irq long ago and we want to |
| 39 | * flush such a longstanding irq before considering it as spurious. |
| 40 | */ |
Yinghai Lu | 85c0f90 | 2008-08-19 20:49:47 -0700 | [diff] [blame] | 41 | for (i = nr_irqs-1; i > 0; i--) { |
Yinghai Lu | 08678b0 | 2008-08-19 20:50:05 -0700 | [diff] [blame] | 42 | desc = irq_to_desc(i); |
Ingo Molnar | 3bf52a4 | 2008-08-19 20:50:29 -0700 | [diff] [blame] | 43 | if (!desc) |
| 44 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | spin_lock_irq(&desc->lock); |
Thomas Gleixner | 6a6de9e | 2006-06-29 02:24:51 -0700 | [diff] [blame] | 47 | if (!desc->action && !(desc->status & IRQ_NOPROBE)) { |
| 48 | /* |
Ingo Molnar | 7a55713 | 2006-06-29 02:24:54 -0700 | [diff] [blame] | 49 | * An old-style architecture might still have |
| 50 | * the handle_bad_irq handler there: |
| 51 | */ |
| 52 | compat_irq_chip_set_default_handler(desc); |
| 53 | |
| 54 | /* |
Thomas Gleixner | 6a6de9e | 2006-06-29 02:24:51 -0700 | [diff] [blame] | 55 | * Some chips need to know about probing in |
| 56 | * progress: |
| 57 | */ |
| 58 | if (desc->chip->set_type) |
| 59 | desc->chip->set_type(i, IRQ_TYPE_PROBE); |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 60 | desc->chip->startup(i); |
Thomas Gleixner | 6a6de9e | 2006-06-29 02:24:51 -0700 | [diff] [blame] | 61 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | spin_unlock_irq(&desc->lock); |
| 63 | } |
| 64 | |
| 65 | /* Wait for longstanding interrupts to trigger. */ |
Luca Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 66 | msleep(20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * enable any unassigned irqs |
| 70 | * (we must startup again here because if a longstanding irq |
| 71 | * happened in the previous stage, it may have masked itself) |
| 72 | */ |
Yinghai Lu | 85c0f90 | 2008-08-19 20:49:47 -0700 | [diff] [blame] | 73 | for (i = nr_irqs-1; i > 0; i--) { |
Yinghai Lu | 08678b0 | 2008-08-19 20:50:05 -0700 | [diff] [blame] | 74 | desc = irq_to_desc(i); |
Ingo Molnar | 3bf52a4 | 2008-08-19 20:50:29 -0700 | [diff] [blame] | 75 | if (!desc) |
| 76 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | spin_lock_irq(&desc->lock); |
Thomas Gleixner | 3418d72 | 2006-06-29 02:24:49 -0700 | [diff] [blame] | 79 | if (!desc->action && !(desc->status & IRQ_NOPROBE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | desc->status |= IRQ_AUTODETECT | IRQ_WAITING; |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 81 | if (desc->chip->startup(i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | desc->status |= IRQ_PENDING; |
| 83 | } |
| 84 | spin_unlock_irq(&desc->lock); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Wait for spurious interrupts to trigger |
| 89 | */ |
Luca Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 90 | msleep(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * Now filter out any obviously spurious interrupts |
| 94 | */ |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 95 | mask = 0; |
Yinghai Lu | 85c0f90 | 2008-08-19 20:49:47 -0700 | [diff] [blame] | 96 | for (i = 0; i < nr_irqs; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | unsigned int status; |
| 98 | |
Yinghai Lu | 08678b0 | 2008-08-19 20:50:05 -0700 | [diff] [blame] | 99 | desc = irq_to_desc(i); |
Ingo Molnar | 3bf52a4 | 2008-08-19 20:50:29 -0700 | [diff] [blame] | 100 | if (!desc) |
| 101 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | spin_lock_irq(&desc->lock); |
| 103 | status = desc->status; |
| 104 | |
| 105 | if (status & IRQ_AUTODETECT) { |
| 106 | /* It triggered already - consider it spurious. */ |
| 107 | if (!(status & IRQ_WAITING)) { |
| 108 | desc->status = status & ~IRQ_AUTODETECT; |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 109 | desc->chip->shutdown(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } else |
| 111 | if (i < 32) |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 112 | mask |= 1 << i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | spin_unlock_irq(&desc->lock); |
| 115 | } |
| 116 | |
Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 117 | return mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | EXPORT_SYMBOL(probe_irq_on); |
| 120 | |
| 121 | /** |
| 122 | * probe_irq_mask - scan a bitmap of interrupt lines |
| 123 | * @val: mask of interrupts to consider |
| 124 | * |
| 125 | * Scan the interrupt lines and return a bitmap of active |
| 126 | * autodetect interrupts. The interrupt probe logic state |
| 127 | * is then returned to its previous value. |
| 128 | * |
| 129 | * Note: we need to scan all the irq's even though we will |
| 130 | * only return autodetect irq numbers - just so that we reset |
| 131 | * them all to a known state. |
| 132 | */ |
| 133 | unsigned int probe_irq_mask(unsigned long val) |
| 134 | { |
| 135 | unsigned int mask; |
| 136 | int i; |
| 137 | |
| 138 | mask = 0; |
Yinghai Lu | 85c0f90 | 2008-08-19 20:49:47 -0700 | [diff] [blame] | 139 | for (i = 0; i < nr_irqs; i++) { |
Yinghai Lu | 08678b0 | 2008-08-19 20:50:05 -0700 | [diff] [blame] | 140 | struct irq_desc *desc = irq_to_desc(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | unsigned int status; |
| 142 | |
Ingo Molnar | 3bf52a4 | 2008-08-19 20:50:29 -0700 | [diff] [blame] | 143 | if (!desc) |
| 144 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | spin_lock_irq(&desc->lock); |
| 146 | status = desc->status; |
| 147 | |
| 148 | if (status & IRQ_AUTODETECT) { |
| 149 | if (i < 16 && !(status & IRQ_WAITING)) |
| 150 | mask |= 1 << i; |
| 151 | |
| 152 | desc->status = status & ~IRQ_AUTODETECT; |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 153 | desc->chip->shutdown(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | spin_unlock_irq(&desc->lock); |
| 156 | } |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 157 | mutex_unlock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | return mask & val; |
| 160 | } |
| 161 | EXPORT_SYMBOL(probe_irq_mask); |
| 162 | |
| 163 | /** |
| 164 | * probe_irq_off - end an interrupt autodetect |
| 165 | * @val: mask of potential interrupts (unused) |
| 166 | * |
| 167 | * Scans the unused interrupt lines and returns the line which |
| 168 | * appears to have triggered the interrupt. If no interrupt was |
| 169 | * found then zero is returned. If more than one interrupt is |
| 170 | * found then minus the first candidate is returned to indicate |
| 171 | * their is doubt. |
| 172 | * |
| 173 | * The interrupt probe logic state is returned to its previous |
| 174 | * value. |
| 175 | * |
| 176 | * BUGS: When used in a module (which arguably shouldn't happen) |
| 177 | * nothing prevents two IRQ probe callers from overlapping. The |
| 178 | * results of this are non-optimal. |
| 179 | */ |
| 180 | int probe_irq_off(unsigned long val) |
| 181 | { |
| 182 | int i, irq_found = 0, nr_irqs = 0; |
| 183 | |
Yinghai Lu | 85c0f90 | 2008-08-19 20:49:47 -0700 | [diff] [blame] | 184 | for (i = 0; i < nr_irqs; i++) { |
Yinghai Lu | 08678b0 | 2008-08-19 20:50:05 -0700 | [diff] [blame] | 185 | struct irq_desc *desc = irq_to_desc(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | unsigned int status; |
| 187 | |
Ingo Molnar | 3bf52a4 | 2008-08-19 20:50:29 -0700 | [diff] [blame] | 188 | if (!desc) |
| 189 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | spin_lock_irq(&desc->lock); |
| 191 | status = desc->status; |
| 192 | |
| 193 | if (status & IRQ_AUTODETECT) { |
| 194 | if (!(status & IRQ_WAITING)) { |
| 195 | if (!nr_irqs) |
| 196 | irq_found = i; |
| 197 | nr_irqs++; |
| 198 | } |
| 199 | desc->status = status & ~IRQ_AUTODETECT; |
Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 200 | desc->chip->shutdown(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | spin_unlock_irq(&desc->lock); |
| 203 | } |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 204 | mutex_unlock(&probing_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | if (nr_irqs > 1) |
| 207 | irq_found = -irq_found; |
Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return irq_found; |
| 210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | EXPORT_SYMBOL(probe_irq_off); |
| 212 | |