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