autoprobe.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/kernel/irq/autoprobe.c
  4. *
  5. * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
  6. *
  7. * This file contains the interrupt probing code and driver APIs.
  8. */
  9. #include <linux/irq.h>
  10. #include <linux/module.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/delay.h>
  13. #include <linux/async.h>
  14. #include "internals.h"
  15. /*
  16. * Autodetection depends on the fact that any interrupt that
  17. * comes in on to an unassigned handler will get stuck with
  18. * "IRQS_WAITING" cleared and the interrupt disabled.
  19. */
  20. static DEFINE_MUTEX(probing_active);
  21. /**
  22. * probe_irq_on - begin an interrupt autodetect
  23. *
  24. * Commence probing for an interrupt. The interrupts are scanned
  25. * and a mask of potential interrupt lines is returned.
  26. *
  27. */
  28. unsigned long probe_irq_on(void)
  29. {
  30. struct irq_desc *desc;
  31. unsigned long mask = 0;
  32. int i;
  33. /*
  34. * quiesce the kernel, or at least the asynchronous portion
  35. */
  36. async_synchronize_full();
  37. mutex_lock(&probing_active);
  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. */
  42. for_each_irq_desc_reverse(i, desc) {
  43. raw_spin_lock_irq(&desc->lock);
  44. if (!desc->action && irq_settings_can_probe(desc)) {
  45. /*
  46. * Some chips need to know about probing in
  47. * progress:
  48. */
  49. if (desc->irq_data.chip->irq_set_type)
  50. desc->irq_data.chip->irq_set_type(&desc->irq_data,
  51. IRQ_TYPE_PROBE);
  52. irq_startup(desc, IRQ_NORESEND, IRQ_START_FORCE);
  53. }
  54. raw_spin_unlock_irq(&desc->lock);
  55. }
  56. /* Wait for longstanding interrupts to trigger. */
  57. msleep(20);
  58. /*
  59. * enable any unassigned irqs
  60. * (we must startup again here because if a longstanding irq
  61. * happened in the previous stage, it may have masked itself)
  62. */
  63. for_each_irq_desc_reverse(i, desc) {
  64. raw_spin_lock_irq(&desc->lock);
  65. if (!desc->action && irq_settings_can_probe(desc)) {
  66. desc->istate |= IRQS_AUTODETECT | IRQS_WAITING;
  67. if (irq_startup(desc, IRQ_NORESEND, IRQ_START_FORCE))
  68. desc->istate |= IRQS_PENDING;
  69. }
  70. raw_spin_unlock_irq(&desc->lock);
  71. }
  72. /*
  73. * Wait for spurious interrupts to trigger
  74. */
  75. msleep(100);
  76. /*
  77. * Now filter out any obviously spurious interrupts
  78. */
  79. for_each_irq_desc(i, desc) {
  80. raw_spin_lock_irq(&desc->lock);
  81. if (desc->istate & IRQS_AUTODETECT) {
  82. /* It triggered already - consider it spurious. */
  83. if (!(desc->istate & IRQS_WAITING)) {
  84. desc->istate &= ~IRQS_AUTODETECT;
  85. irq_shutdown(desc);
  86. } else
  87. if (i < 32)
  88. mask |= 1 << i;
  89. }
  90. raw_spin_unlock_irq(&desc->lock);
  91. }
  92. return mask;
  93. }
  94. EXPORT_SYMBOL(probe_irq_on);
  95. /**
  96. * probe_irq_mask - scan a bitmap of interrupt lines
  97. * @val: mask of interrupts to consider
  98. *
  99. * Scan the interrupt lines and return a bitmap of active
  100. * autodetect interrupts. The interrupt probe logic state
  101. * is then returned to its previous value.
  102. *
  103. * Note: we need to scan all the irq's even though we will
  104. * only return autodetect irq numbers - just so that we reset
  105. * them all to a known state.
  106. */
  107. unsigned int probe_irq_mask(unsigned long val)
  108. {
  109. unsigned int mask = 0;
  110. struct irq_desc *desc;
  111. int i;
  112. for_each_irq_desc(i, desc) {
  113. raw_spin_lock_irq(&desc->lock);
  114. if (desc->istate & IRQS_AUTODETECT) {
  115. if (i < 16 && !(desc->istate & IRQS_WAITING))
  116. mask |= 1 << i;
  117. desc->istate &= ~IRQS_AUTODETECT;
  118. irq_shutdown(desc);
  119. }
  120. raw_spin_unlock_irq(&desc->lock);
  121. }
  122. mutex_unlock(&probing_active);
  123. return mask & val;
  124. }
  125. EXPORT_SYMBOL(probe_irq_mask);
  126. /**
  127. * probe_irq_off - end an interrupt autodetect
  128. * @val: mask of potential interrupts (unused)
  129. *
  130. * Scans the unused interrupt lines and returns the line which
  131. * appears to have triggered the interrupt. If no interrupt was
  132. * found then zero is returned. If more than one interrupt is
  133. * found then minus the first candidate is returned to indicate
  134. * their is doubt.
  135. *
  136. * The interrupt probe logic state is returned to its previous
  137. * value.
  138. *
  139. * BUGS: When used in a module (which arguably shouldn't happen)
  140. * nothing prevents two IRQ probe callers from overlapping. The
  141. * results of this are non-optimal.
  142. */
  143. int probe_irq_off(unsigned long val)
  144. {
  145. int i, irq_found = 0, nr_of_irqs = 0;
  146. struct irq_desc *desc;
  147. for_each_irq_desc(i, desc) {
  148. raw_spin_lock_irq(&desc->lock);
  149. if (desc->istate & IRQS_AUTODETECT) {
  150. if (!(desc->istate & IRQS_WAITING)) {
  151. if (!nr_of_irqs)
  152. irq_found = i;
  153. nr_of_irqs++;
  154. }
  155. desc->istate &= ~IRQS_AUTODETECT;
  156. irq_shutdown(desc);
  157. }
  158. raw_spin_unlock_irq(&desc->lock);
  159. }
  160. mutex_unlock(&probing_active);
  161. if (nr_of_irqs > 1)
  162. irq_found = -irq_found;
  163. return irq_found;
  164. }
  165. EXPORT_SYMBOL(probe_irq_off);