autoprobe.c 4.6 KB

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