| 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; | 
| Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 33 | unsigned long mask = 0; | 
|  | 34 | unsigned int status; | 
|  | 35 | int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 |  | 
| Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 37 | mutex_lock(&probing_active); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* | 
|  | 39 | * something may have generated an irq long ago and we want to | 
|  | 40 | * flush such a longstanding irq before considering it as spurious. | 
|  | 41 | */ | 
| Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 42 | for_each_irq_desc_reverse(i, desc) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | spin_lock_irq(&desc->lock); | 
| Thomas Gleixner | 6a6de9e | 2006-06-29 02:24:51 -0700 | [diff] [blame] | 44 | if (!desc->action && !(desc->status & IRQ_NOPROBE)) { | 
|  | 45 | /* | 
| Ingo Molnar | 7a55713 | 2006-06-29 02:24:54 -0700 | [diff] [blame] | 46 | * An old-style architecture might still have | 
|  | 47 | * the handle_bad_irq handler there: | 
|  | 48 | */ | 
|  | 49 | compat_irq_chip_set_default_handler(desc); | 
|  | 50 |  | 
|  | 51 | /* | 
| Thomas Gleixner | 6a6de9e | 2006-06-29 02:24:51 -0700 | [diff] [blame] | 52 | * Some chips need to know about probing in | 
|  | 53 | * progress: | 
|  | 54 | */ | 
|  | 55 | if (desc->chip->set_type) | 
|  | 56 | desc->chip->set_type(i, IRQ_TYPE_PROBE); | 
| Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 57 | desc->chip->startup(i); | 
| Thomas Gleixner | 6a6de9e | 2006-06-29 02:24:51 -0700 | [diff] [blame] | 58 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | spin_unlock_irq(&desc->lock); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | /* Wait for longstanding interrupts to trigger. */ | 
| Luca Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 63 | msleep(20); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | /* | 
|  | 66 | * enable any unassigned irqs | 
|  | 67 | * (we must startup again here because if a longstanding irq | 
|  | 68 | * happened in the previous stage, it may have masked itself) | 
|  | 69 | */ | 
| Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 70 | for_each_irq_desc_reverse(i, desc) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | spin_lock_irq(&desc->lock); | 
| Thomas Gleixner | 3418d72 | 2006-06-29 02:24:49 -0700 | [diff] [blame] | 72 | if (!desc->action && !(desc->status & IRQ_NOPROBE)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | desc->status |= IRQ_AUTODETECT | IRQ_WAITING; | 
| Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 74 | if (desc->chip->startup(i)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | desc->status |= IRQ_PENDING; | 
|  | 76 | } | 
|  | 77 | spin_unlock_irq(&desc->lock); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | /* | 
|  | 81 | * Wait for spurious interrupts to trigger | 
|  | 82 | */ | 
| Luca Falavigna | 47f176f | 2005-06-28 20:44:42 -0700 | [diff] [blame] | 83 | msleep(100); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 |  | 
|  | 85 | /* | 
|  | 86 | * Now filter out any obviously spurious interrupts | 
|  | 87 | */ | 
| Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 88 | for_each_irq_desc(i, desc) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | spin_lock_irq(&desc->lock); | 
|  | 90 | status = desc->status; | 
|  | 91 |  | 
|  | 92 | if (status & IRQ_AUTODETECT) { | 
|  | 93 | /* It triggered already - consider it spurious. */ | 
|  | 94 | if (!(status & IRQ_WAITING)) { | 
|  | 95 | desc->status = status & ~IRQ_AUTODETECT; | 
| Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 96 | desc->chip->shutdown(i); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } else | 
|  | 98 | if (i < 32) | 
| Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 99 | mask |= 1 << i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } | 
|  | 101 | spin_unlock_irq(&desc->lock); | 
|  | 102 | } | 
|  | 103 |  | 
| Ingo Molnar | 06fcb0c | 2006-06-29 02:24:40 -0700 | [diff] [blame] | 104 | return mask; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | EXPORT_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 | */ | 
|  | 120 | unsigned int probe_irq_mask(unsigned long val) | 
|  | 121 | { | 
| Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 122 | unsigned int status, mask = 0; | 
|  | 123 | struct irq_desc *desc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | int i; | 
|  | 125 |  | 
| Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 126 | for_each_irq_desc(i, desc) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | spin_lock_irq(&desc->lock); | 
|  | 128 | status = desc->status; | 
|  | 129 |  | 
|  | 130 | if (status & IRQ_AUTODETECT) { | 
|  | 131 | if (i < 16 && !(status & IRQ_WAITING)) | 
|  | 132 | mask |= 1 << i; | 
|  | 133 |  | 
|  | 134 | desc->status = status & ~IRQ_AUTODETECT; | 
| Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 135 | desc->chip->shutdown(i); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } | 
|  | 137 | spin_unlock_irq(&desc->lock); | 
|  | 138 | } | 
| Ingo Molnar | 74ffd55 | 2006-06-29 02:24:37 -0700 | [diff] [blame] | 139 | mutex_unlock(&probing_active); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 |  | 
|  | 141 | return mask & val; | 
|  | 142 | } | 
|  | 143 | EXPORT_SYMBOL(probe_irq_mask); | 
|  | 144 |  | 
|  | 145 | /** | 
|  | 146 | *	probe_irq_off	- end an interrupt autodetect | 
|  | 147 | *	@val: mask of potential interrupts (unused) | 
|  | 148 | * | 
|  | 149 | *	Scans the unused interrupt lines and returns the line which | 
|  | 150 | *	appears to have triggered the interrupt. If no interrupt was | 
|  | 151 | *	found then zero is returned. If more than one interrupt is | 
|  | 152 | *	found then minus the first candidate is returned to indicate | 
|  | 153 | *	their is doubt. | 
|  | 154 | * | 
|  | 155 | *	The interrupt probe logic state is returned to its previous | 
|  | 156 | *	value. | 
|  | 157 | * | 
|  | 158 | *	BUGS: When used in a module (which arguably shouldn't happen) | 
|  | 159 | *	nothing prevents two IRQ probe callers from overlapping. The | 
|  | 160 | *	results of this are non-optimal. | 
|  | 161 | */ | 
|  | 162 | int probe_irq_off(unsigned long val) | 
|  | 163 | { | 
| Thomas Gleixner | 63d659d | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 164 | int i, irq_found = 0, nr_of_irqs = 0; | 
| Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 165 | struct irq_desc *desc; | 
|  | 166 | unsigned int status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 |  | 
| Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 168 | for_each_irq_desc(i, desc) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | spin_lock_irq(&desc->lock); | 
|  | 170 | status = desc->status; | 
|  | 171 |  | 
|  | 172 | if (status & IRQ_AUTODETECT) { | 
|  | 173 | if (!(status & IRQ_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 | } | 
|  | 178 | desc->status = status & ~IRQ_AUTODETECT; | 
| Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 179 | desc->chip->shutdown(i); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } | 
|  | 181 | spin_unlock_irq(&desc->lock); | 
|  | 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 |  |