spurious.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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.
  72. */
  73. action = desc->action;
  74. if (!action || !(action->flags & IRQF_SHARED) ||
  75. (action->flags & __IRQF_TIMER))
  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. /* Make sure that there is still a valid action */
  92. action = desc->action;
  93. } while ((desc->istate & IRQS_PENDING) && action);
  94. desc->istate &= ~IRQS_POLL_INPROGRESS;
  95. out:
  96. raw_spin_unlock(&desc->lock);
  97. return ret == IRQ_HANDLED;
  98. }
  99. static int misrouted_irq(int irq)
  100. {
  101. struct irq_desc *desc;
  102. int i, ok = 0;
  103. if (atomic_inc_return(&irq_poll_active) != 1)
  104. goto out;
  105. irq_poll_cpu = smp_processor_id();
  106. for_each_irq_desc(i, desc) {
  107. if (!i)
  108. continue;
  109. if (i == irq) /* Already tried */
  110. continue;
  111. if (try_one_irq(i, desc, false))
  112. ok = 1;
  113. }
  114. out:
  115. atomic_dec(&irq_poll_active);
  116. /* So the caller can adjust the irq error counts */
  117. return ok;
  118. }
  119. static void poll_spurious_irqs(unsigned long dummy)
  120. {
  121. struct irq_desc *desc;
  122. int i;
  123. if (atomic_inc_return(&irq_poll_active) != 1)
  124. goto out;
  125. irq_poll_cpu = smp_processor_id();
  126. for_each_irq_desc(i, desc) {
  127. unsigned int state;
  128. if (!i)
  129. continue;
  130. /* Racy but it doesn't matter */
  131. state = desc->istate;
  132. barrier();
  133. if (!(state & IRQS_SPURIOUS_DISABLED))
  134. continue;
  135. local_irq_disable();
  136. try_one_irq(i, desc, true);
  137. local_irq_enable();
  138. }
  139. out:
  140. atomic_dec(&irq_poll_active);
  141. mod_timer(&poll_spurious_irq_timer,
  142. jiffies + POLL_SPURIOUS_IRQ_INTERVAL);
  143. }
  144. static inline int bad_action_ret(irqreturn_t action_ret)
  145. {
  146. if (likely(action_ret <= (IRQ_HANDLED | IRQ_WAKE_THREAD)))
  147. return 0;
  148. return 1;
  149. }
  150. /*
  151. * If 99,900 of the previous 100,000 interrupts have not been handled
  152. * then assume that the IRQ is stuck in some manner. Drop a diagnostic
  153. * and try to turn the IRQ off.
  154. *
  155. * (The other 100-of-100,000 interrupts may have been a correctly
  156. * functioning device sharing an IRQ with the failing one)
  157. */
  158. static void
  159. __report_bad_irq(unsigned int irq, struct irq_desc *desc,
  160. irqreturn_t action_ret)
  161. {
  162. struct irqaction *action;
  163. unsigned long flags;
  164. if (bad_action_ret(action_ret)) {
  165. printk(KERN_ERR "irq event %d: bogus return value %x\n",
  166. irq, action_ret);
  167. } else {
  168. printk(KERN_ERR "irq %d: nobody cared (try booting with "
  169. "the \"irqpoll\" option)\n", irq);
  170. }
  171. dump_stack();
  172. printk(KERN_ERR "handlers:\n");
  173. /*
  174. * We need to take desc->lock here. note_interrupt() is called
  175. * w/o desc->lock held, but IRQ_PROGRESS set. We might race
  176. * with something else removing an action. It's ok to take
  177. * desc->lock here. See synchronize_irq().
  178. */
  179. raw_spin_lock_irqsave(&desc->lock, flags);
  180. action = desc->action;
  181. while (action) {
  182. printk(KERN_ERR "[<%p>] %pf", action->handler, action->handler);
  183. if (action->thread_fn)
  184. printk(KERN_CONT " threaded [<%p>] %pf",
  185. action->thread_fn, action->thread_fn);
  186. printk(KERN_CONT "\n");
  187. action = action->next;
  188. }
  189. raw_spin_unlock_irqrestore(&desc->lock, flags);
  190. }
  191. static void
  192. report_bad_irq(unsigned int irq, struct irq_desc *desc, irqreturn_t action_ret)
  193. {
  194. static int count = 100;
  195. if (count > 0) {
  196. count--;
  197. __report_bad_irq(irq, desc, action_ret);
  198. }
  199. }
  200. static inline int
  201. try_misrouted_irq(unsigned int irq, struct irq_desc *desc,
  202. irqreturn_t action_ret)
  203. {
  204. struct irqaction *action;
  205. if (!irqfixup)
  206. return 0;
  207. /* We didn't actually handle the IRQ - see if it was misrouted? */
  208. if (action_ret == IRQ_NONE)
  209. return 1;
  210. /*
  211. * But for 'irqfixup == 2' we also do it for handled interrupts if
  212. * they are marked as IRQF_IRQPOLL (or for irq zero, which is the
  213. * traditional PC timer interrupt.. Legacy)
  214. */
  215. if (irqfixup < 2)
  216. return 0;
  217. if (!irq)
  218. return 1;
  219. /*
  220. * Since we don't get the descriptor lock, "action" can
  221. * change under us. We don't really care, but we don't
  222. * want to follow a NULL pointer. So tell the compiler to
  223. * just load it once by using a barrier.
  224. */
  225. action = desc->action;
  226. barrier();
  227. return action && (action->flags & IRQF_IRQPOLL);
  228. }
  229. #define SPURIOUS_DEFERRED 0x80000000
  230. void note_interrupt(unsigned int irq, struct irq_desc *desc,
  231. irqreturn_t action_ret)
  232. {
  233. if (desc->istate & IRQS_POLL_INPROGRESS)
  234. return;
  235. if (bad_action_ret(action_ret)) {
  236. report_bad_irq(irq, desc, action_ret);
  237. return;
  238. }
  239. /*
  240. * We cannot call note_interrupt from the threaded handler
  241. * because we need to look at the compound of all handlers
  242. * (primary and threaded). Aside of that in the threaded
  243. * shared case we have no serialization against an incoming
  244. * hardware interrupt while we are dealing with a threaded
  245. * result.
  246. *
  247. * So in case a thread is woken, we just note the fact and
  248. * defer the analysis to the next hardware interrupt.
  249. *
  250. * The threaded handlers store whether they sucessfully
  251. * handled an interrupt and we check whether that number
  252. * changed versus the last invocation.
  253. *
  254. * We could handle all interrupts with the delayed by one
  255. * mechanism, but for the non forced threaded case we'd just
  256. * add pointless overhead to the straight hardirq interrupts
  257. * for the sake of a few lines less code.
  258. */
  259. if (action_ret & IRQ_WAKE_THREAD) {
  260. /*
  261. * There is a thread woken. Check whether one of the
  262. * shared primary handlers returned IRQ_HANDLED. If
  263. * not we defer the spurious detection to the next
  264. * interrupt.
  265. */
  266. if (action_ret == IRQ_WAKE_THREAD) {
  267. int handled;
  268. /*
  269. * We use bit 31 of thread_handled_last to
  270. * denote the deferred spurious detection
  271. * active. No locking necessary as
  272. * thread_handled_last is only accessed here
  273. * and we have the guarantee that hard
  274. * interrupts are not reentrant.
  275. */
  276. if (!(desc->threads_handled_last & SPURIOUS_DEFERRED)) {
  277. desc->threads_handled_last |= SPURIOUS_DEFERRED;
  278. return;
  279. }
  280. /*
  281. * Check whether one of the threaded handlers
  282. * returned IRQ_HANDLED since the last
  283. * interrupt happened.
  284. *
  285. * For simplicity we just set bit 31, as it is
  286. * set in threads_handled_last as well. So we
  287. * avoid extra masking. And we really do not
  288. * care about the high bits of the handled
  289. * count. We just care about the count being
  290. * different than the one we saw before.
  291. */
  292. handled = atomic_read(&desc->threads_handled);
  293. handled |= SPURIOUS_DEFERRED;
  294. if (handled != desc->threads_handled_last) {
  295. action_ret = IRQ_HANDLED;
  296. /*
  297. * Note: We keep the SPURIOUS_DEFERRED
  298. * bit set. We are handling the
  299. * previous invocation right now.
  300. * Keep it for the current one, so the
  301. * next hardware interrupt will
  302. * account for it.
  303. */
  304. desc->threads_handled_last = handled;
  305. } else {
  306. /*
  307. * None of the threaded handlers felt
  308. * responsible for the last interrupt
  309. *
  310. * We keep the SPURIOUS_DEFERRED bit
  311. * set in threads_handled_last as we
  312. * need to account for the current
  313. * interrupt as well.
  314. */
  315. action_ret = IRQ_NONE;
  316. }
  317. } else {
  318. /*
  319. * One of the primary handlers returned
  320. * IRQ_HANDLED. So we don't care about the
  321. * threaded handlers on the same line. Clear
  322. * the deferred detection bit.
  323. *
  324. * In theory we could/should check whether the
  325. * deferred bit is set and take the result of
  326. * the previous run into account here as
  327. * well. But it's really not worth the
  328. * trouble. If every other interrupt is
  329. * handled we never trigger the spurious
  330. * detector. And if this is just the one out
  331. * of 100k unhandled ones which is handled
  332. * then we merily delay the spurious detection
  333. * by one hard interrupt. Not a real problem.
  334. */
  335. desc->threads_handled_last &= ~SPURIOUS_DEFERRED;
  336. }
  337. }
  338. if (unlikely(action_ret == IRQ_NONE)) {
  339. /*
  340. * If we are seeing only the odd spurious IRQ caused by
  341. * bus asynchronicity then don't eventually trigger an error,
  342. * otherwise the counter becomes a doomsday timer for otherwise
  343. * working systems
  344. */
  345. if (time_after(jiffies, desc->last_unhandled + HZ/10))
  346. desc->irqs_unhandled = 1;
  347. else
  348. desc->irqs_unhandled++;
  349. desc->last_unhandled = jiffies;
  350. }
  351. if (unlikely(try_misrouted_irq(irq, desc, action_ret))) {
  352. int ok = misrouted_irq(irq);
  353. if (action_ret == IRQ_NONE)
  354. desc->irqs_unhandled -= ok;
  355. }
  356. desc->irq_count++;
  357. if (likely(desc->irq_count < 100000))
  358. return;
  359. desc->irq_count = 0;
  360. if (unlikely(desc->irqs_unhandled > 99900)) {
  361. /*
  362. * The interrupt is stuck
  363. */
  364. __report_bad_irq(irq, desc, action_ret);
  365. /*
  366. * Now kill the IRQ
  367. */
  368. printk(KERN_EMERG "Disabling IRQ #%d\n", irq);
  369. desc->istate |= IRQS_SPURIOUS_DISABLED;
  370. desc->depth++;
  371. irq_disable(desc);
  372. mod_timer(&poll_spurious_irq_timer,
  373. jiffies + POLL_SPURIOUS_IRQ_INTERVAL);
  374. }
  375. desc->irqs_unhandled = 0;
  376. }
  377. bool noirqdebug __read_mostly;
  378. int noirqdebug_setup(char *str)
  379. {
  380. noirqdebug = 1;
  381. printk(KERN_INFO "IRQ lockup detection disabled\n");
  382. return 1;
  383. }
  384. __setup("noirqdebug", noirqdebug_setup);
  385. module_param(noirqdebug, bool, 0644);
  386. MODULE_PARM_DESC(noirqdebug, "Disable irq lockup detection when true");
  387. static int __init irqfixup_setup(char *str)
  388. {
  389. irqfixup = 1;
  390. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  391. printk(KERN_WARNING "This may impact system performance.\n");
  392. return 1;
  393. }
  394. __setup("irqfixup", irqfixup_setup);
  395. module_param(irqfixup, int, 0644);
  396. static int __init irqpoll_setup(char *str)
  397. {
  398. irqfixup = 2;
  399. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  400. "enabled\n");
  401. printk(KERN_WARNING "This may significantly impact system "
  402. "performance\n");
  403. return 1;
  404. }
  405. __setup("irqpoll", irqpoll_setup);