spurious.c 12 KB

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