spurious.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * linux/kernel/irq/spurious.c
  3. *
  4. * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
  5. *
  6. * This file contains spurious interrupt handling.
  7. */
  8. #include <linux/jiffies.h>
  9. #include <linux/irq.h>
  10. #include <linux/module.h>
  11. #include <linux/kallsyms.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/timer.h>
  15. #include "internals.h"
  16. static int irqfixup __read_mostly;
  17. #define POLL_SPURIOUS_IRQ_INTERVAL (HZ/10)
  18. static void poll_spurious_irqs(unsigned long dummy);
  19. static DEFINE_TIMER(poll_spurious_irq_timer, poll_spurious_irqs, 0, 0);
  20. static int irq_poll_cpu;
  21. static atomic_t irq_poll_active;
  22. /*
  23. * We wait here for a poller to finish.
  24. *
  25. * If the poll runs on this CPU, then we yell loudly and return
  26. * false. That will leave the interrupt line disabled in the worst
  27. * case, but it should never happen.
  28. *
  29. * We wait until the poller is done and then recheck disabled and
  30. * action (about to be disabled). Only if it's still active, we return
  31. * true and let the handler run.
  32. */
  33. bool irq_wait_for_poll(struct irq_desc *desc)
  34. {
  35. if (WARN_ONCE(irq_poll_cpu == smp_processor_id(),
  36. "irq poll in progress on cpu %d for irq %d\n",
  37. smp_processor_id(), desc->irq_data.irq))
  38. return false;
  39. #ifdef CONFIG_SMP
  40. do {
  41. raw_spin_unlock(&desc->lock);
  42. while (irqd_irq_inprogress(&desc->irq_data))
  43. cpu_relax();
  44. raw_spin_lock(&desc->lock);
  45. } while (irqd_irq_inprogress(&desc->irq_data));
  46. /* Might have been disabled in meantime */
  47. return !irqd_irq_disabled(&desc->irq_data) && desc->action;
  48. #else
  49. return false;
  50. #endif
  51. }
  52. /*
  53. * Recovery handler for misrouted interrupts.
  54. */
  55. static int try_one_irq(int irq, struct irq_desc *desc, bool force)
  56. {
  57. irqreturn_t ret = IRQ_NONE;
  58. struct irqaction *action;
  59. raw_spin_lock(&desc->lock);
  60. /* PER_CPU and nested thread interrupts are never polled */
  61. if (irq_settings_is_per_cpu(desc) || irq_settings_is_nested_thread(desc))
  62. goto out;
  63. /*
  64. * Do not poll disabled interrupts unless the spurious
  65. * disabled poller asks explicitely.
  66. */
  67. if (irqd_irq_disabled(&desc->irq_data) && !force)
  68. goto out;
  69. /*
  70. * All handlers must agree on IRQF_SHARED, so we test just the
  71. * first. Check for action->next as well.
  72. */
  73. action = desc->action;
  74. if (!action || !(action->flags & IRQF_SHARED) ||
  75. (action->flags & __IRQF_TIMER) || !action->next)
  76. goto out;
  77. /* Already running on another processor */
  78. if (irqd_irq_inprogress(&desc->irq_data)) {
  79. /*
  80. * Already running: If it is shared get the other
  81. * CPU to go looking for our mystery interrupt too
  82. */
  83. desc->istate |= IRQS_PENDING;
  84. goto out;
  85. }
  86. /* Mark it poll in progress */
  87. desc->istate |= IRQS_POLL_INPROGRESS;
  88. do {
  89. if (handle_irq_event(desc) == IRQ_HANDLED)
  90. ret = IRQ_HANDLED;
  91. action = desc->action;
  92. } while ((desc->istate & IRQS_PENDING) && action);
  93. desc->istate &= ~IRQS_POLL_INPROGRESS;
  94. out:
  95. raw_spin_unlock(&desc->lock);
  96. return ret == IRQ_HANDLED;
  97. }
  98. static int misrouted_irq(int irq)
  99. {
  100. struct irq_desc *desc;
  101. int i, ok = 0;
  102. if (atomic_inc_return(&irq_poll_active) == 1)
  103. goto out;
  104. irq_poll_cpu = smp_processor_id();
  105. for_each_irq_desc(i, desc) {
  106. if (!i)
  107. continue;
  108. if (i == irq) /* Already tried */
  109. continue;
  110. if (try_one_irq(i, desc, false))
  111. ok = 1;
  112. }
  113. out:
  114. atomic_dec(&irq_poll_active);
  115. /* So the caller can adjust the irq error counts */
  116. return ok;
  117. }
  118. static void poll_spurious_irqs(unsigned long dummy)
  119. {
  120. struct irq_desc *desc;
  121. int i;
  122. if (atomic_inc_return(&irq_poll_active) != 1)
  123. goto out;
  124. irq_poll_cpu = smp_processor_id();
  125. for_each_irq_desc(i, desc) {
  126. unsigned int state;
  127. if (!i)
  128. continue;
  129. /* Racy but it doesn't matter */
  130. state = desc->istate;
  131. barrier();
  132. if (!(state & IRQS_SPURIOUS_DISABLED))
  133. continue;
  134. local_irq_disable();
  135. try_one_irq(i, desc, true);
  136. local_irq_enable();
  137. }
  138. out:
  139. atomic_dec(&irq_poll_active);
  140. mod_timer(&poll_spurious_irq_timer,
  141. jiffies + POLL_SPURIOUS_IRQ_INTERVAL);
  142. }
  143. static inline int bad_action_ret(irqreturn_t action_ret)
  144. {
  145. if (likely(action_ret <= (IRQ_HANDLED | IRQ_WAKE_THREAD)))
  146. return 0;
  147. return 1;
  148. }
  149. /*
  150. * If 99,900 of the previous 100,000 interrupts have not been handled
  151. * then assume that the IRQ is stuck in some manner. Drop a diagnostic
  152. * and try to turn the IRQ off.
  153. *
  154. * (The other 100-of-100,000 interrupts may have been a correctly
  155. * functioning device sharing an IRQ with the failing one)
  156. */
  157. static void
  158. __report_bad_irq(unsigned int irq, struct irq_desc *desc,
  159. irqreturn_t action_ret)
  160. {
  161. struct irqaction *action;
  162. unsigned long flags;
  163. if (bad_action_ret(action_ret)) {
  164. printk(KERN_ERR "irq event %d: bogus return value %x\n",
  165. irq, action_ret);
  166. } else {
  167. printk(KERN_ERR "irq %d: nobody cared (try booting with "
  168. "the \"irqpoll\" option)\n", irq);
  169. }
  170. dump_stack();
  171. printk(KERN_ERR "handlers:\n");
  172. /*
  173. * We need to take desc->lock here. note_interrupt() is called
  174. * w/o desc->lock held, but IRQ_PROGRESS set. We might race
  175. * with something else removing an action. It's ok to take
  176. * desc->lock here. See synchronize_irq().
  177. */
  178. raw_spin_lock_irqsave(&desc->lock, flags);
  179. action = desc->action;
  180. while (action) {
  181. printk(KERN_ERR "[<%p>] %pf", action->handler, action->handler);
  182. if (action->thread_fn)
  183. printk(KERN_CONT " threaded [<%p>] %pf",
  184. action->thread_fn, action->thread_fn);
  185. printk(KERN_CONT "\n");
  186. action = action->next;
  187. }
  188. raw_spin_unlock_irqrestore(&desc->lock, flags);
  189. }
  190. static void
  191. report_bad_irq(unsigned int irq, struct irq_desc *desc, irqreturn_t action_ret)
  192. {
  193. static int count = 100;
  194. if (count > 0) {
  195. count--;
  196. __report_bad_irq(irq, desc, action_ret);
  197. }
  198. }
  199. static inline int
  200. try_misrouted_irq(unsigned int irq, struct irq_desc *desc,
  201. irqreturn_t action_ret)
  202. {
  203. struct irqaction *action;
  204. if (!irqfixup)
  205. return 0;
  206. /* We didn't actually handle the IRQ - see if it was misrouted? */
  207. if (action_ret == IRQ_NONE)
  208. return 1;
  209. /*
  210. * But for 'irqfixup == 2' we also do it for handled interrupts if
  211. * they are marked as IRQF_IRQPOLL (or for irq zero, which is the
  212. * traditional PC timer interrupt.. Legacy)
  213. */
  214. if (irqfixup < 2)
  215. return 0;
  216. if (!irq)
  217. return 1;
  218. /*
  219. * Since we don't get the descriptor lock, "action" can
  220. * change under us. We don't really care, but we don't
  221. * want to follow a NULL pointer. So tell the compiler to
  222. * just load it once by using a barrier.
  223. */
  224. action = desc->action;
  225. barrier();
  226. return action && (action->flags & IRQF_IRQPOLL);
  227. }
  228. void note_interrupt(unsigned int irq, struct irq_desc *desc,
  229. irqreturn_t action_ret)
  230. {
  231. if (desc->istate & IRQS_POLL_INPROGRESS)
  232. return;
  233. /* we get here again via the threaded handler */
  234. if (action_ret == IRQ_WAKE_THREAD)
  235. return;
  236. if (bad_action_ret(action_ret)) {
  237. report_bad_irq(irq, desc, action_ret);
  238. return;
  239. }
  240. if (unlikely(action_ret == IRQ_NONE)) {
  241. /*
  242. * If we are seeing only the odd spurious IRQ caused by
  243. * bus asynchronicity then don't eventually trigger an error,
  244. * otherwise the counter becomes a doomsday timer for otherwise
  245. * working systems
  246. */
  247. if (time_after(jiffies, desc->last_unhandled + HZ/10))
  248. desc->irqs_unhandled = 1;
  249. else
  250. desc->irqs_unhandled++;
  251. desc->last_unhandled = jiffies;
  252. }
  253. if (unlikely(try_misrouted_irq(irq, desc, action_ret))) {
  254. int ok = misrouted_irq(irq);
  255. if (action_ret == IRQ_NONE)
  256. desc->irqs_unhandled -= ok;
  257. }
  258. desc->irq_count++;
  259. if (likely(desc->irq_count < 100000))
  260. return;
  261. desc->irq_count = 0;
  262. if (unlikely(desc->irqs_unhandled > 99900)) {
  263. /*
  264. * The interrupt is stuck
  265. */
  266. __report_bad_irq(irq, desc, action_ret);
  267. /*
  268. * Now kill the IRQ
  269. */
  270. printk(KERN_EMERG "Disabling IRQ #%d\n", irq);
  271. desc->istate |= IRQS_SPURIOUS_DISABLED;
  272. desc->depth++;
  273. irq_disable(desc);
  274. mod_timer(&poll_spurious_irq_timer,
  275. jiffies + POLL_SPURIOUS_IRQ_INTERVAL);
  276. }
  277. desc->irqs_unhandled = 0;
  278. }
  279. int noirqdebug __read_mostly;
  280. int noirqdebug_setup(char *str)
  281. {
  282. noirqdebug = 1;
  283. printk(KERN_INFO "IRQ lockup detection disabled\n");
  284. return 1;
  285. }
  286. __setup("noirqdebug", noirqdebug_setup);
  287. module_param(noirqdebug, bool, 0644);
  288. MODULE_PARM_DESC(noirqdebug, "Disable irq lockup detection when true");
  289. static int __init irqfixup_setup(char *str)
  290. {
  291. irqfixup = 1;
  292. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  293. printk(KERN_WARNING "This may impact system performance.\n");
  294. return 1;
  295. }
  296. __setup("irqfixup", irqfixup_setup);
  297. module_param(irqfixup, int, 0644);
  298. static int __init irqpoll_setup(char *str)
  299. {
  300. irqfixup = 2;
  301. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  302. "enabled\n");
  303. printk(KERN_WARNING "This may significantly impact system "
  304. "performance\n");
  305. return 1;
  306. }
  307. __setup("irqpoll", irqpoll_setup);