handle.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * linux/kernel/irq/handle.c
  3. *
  4. * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
  5. * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
  6. *
  7. * This file contains the core interrupt handling code.
  8. *
  9. * Detailed information is available in Documentation/DocBook/genericirq
  10. *
  11. */
  12. #include <linux/irq.h>
  13. #include <linux/module.h>
  14. #include <linux/random.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/rculist.h>
  18. #include <linux/hash.h>
  19. #include <trace/irq.h>
  20. #include <linux/bootmem.h>
  21. #include "internals.h"
  22. /*
  23. * lockdep: we want to handle all irq_desc locks as a single lock-class:
  24. */
  25. struct lock_class_key irq_desc_lock_class;
  26. /**
  27. * handle_bad_irq - handle spurious and unhandled irqs
  28. * @irq: the interrupt number
  29. * @desc: description of the interrupt
  30. *
  31. * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
  32. */
  33. void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
  34. {
  35. print_irq_desc(irq, desc);
  36. kstat_incr_irqs_this_cpu(irq, desc);
  37. ack_bad_irq(irq);
  38. }
  39. #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
  40. static void __init init_irq_default_affinity(void)
  41. {
  42. alloc_bootmem_cpumask_var(&irq_default_affinity);
  43. cpumask_setall(irq_default_affinity);
  44. }
  45. #else
  46. static void __init init_irq_default_affinity(void)
  47. {
  48. }
  49. #endif
  50. /*
  51. * Linux has a controller-independent interrupt architecture.
  52. * Every controller has a 'controller-template', that is used
  53. * by the main code to do the right thing. Each driver-visible
  54. * interrupt source is transparently wired to the appropriate
  55. * controller. Thus drivers need not be aware of the
  56. * interrupt-controller.
  57. *
  58. * The code is designed to be easily extended with new/different
  59. * interrupt controllers, without having to do assembly magic or
  60. * having to touch the generic code.
  61. *
  62. * Controller mappings for all interrupt sources:
  63. */
  64. int nr_irqs = NR_IRQS;
  65. EXPORT_SYMBOL_GPL(nr_irqs);
  66. #ifdef CONFIG_SPARSE_IRQ
  67. static struct irq_desc irq_desc_init = {
  68. .irq = -1,
  69. .status = IRQ_DISABLED,
  70. .chip = &no_irq_chip,
  71. .handle_irq = handle_bad_irq,
  72. .depth = 1,
  73. .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
  74. };
  75. void init_kstat_irqs(struct irq_desc *desc, int node, int nr)
  76. {
  77. void *ptr;
  78. ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
  79. /*
  80. * don't overwite if can not get new one
  81. * init_copy_kstat_irqs() could still use old one
  82. */
  83. if (ptr) {
  84. printk(KERN_DEBUG " alloc kstat_irqs on node %d\n", node);
  85. desc->kstat_irqs = ptr;
  86. }
  87. }
  88. static void init_one_irq_desc(int irq, struct irq_desc *desc, int node)
  89. {
  90. memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
  91. spin_lock_init(&desc->lock);
  92. desc->irq = irq;
  93. #ifdef CONFIG_SMP
  94. desc->node = node;
  95. #endif
  96. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  97. init_kstat_irqs(desc, node, nr_cpu_ids);
  98. if (!desc->kstat_irqs) {
  99. printk(KERN_ERR "can not alloc kstat_irqs\n");
  100. BUG_ON(1);
  101. }
  102. if (!alloc_desc_masks(desc, node, false)) {
  103. printk(KERN_ERR "can not alloc irq_desc cpumasks\n");
  104. BUG_ON(1);
  105. }
  106. init_desc_masks(desc);
  107. arch_init_chip_data(desc, node);
  108. }
  109. /*
  110. * Protect the sparse_irqs:
  111. */
  112. DEFINE_SPINLOCK(sparse_irq_lock);
  113. struct irq_desc **irq_desc_ptrs __read_mostly;
  114. static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
  115. [0 ... NR_IRQS_LEGACY-1] = {
  116. .irq = -1,
  117. .status = IRQ_DISABLED,
  118. .chip = &no_irq_chip,
  119. .handle_irq = handle_bad_irq,
  120. .depth = 1,
  121. .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
  122. }
  123. };
  124. static unsigned int *kstat_irqs_legacy;
  125. int __init early_irq_init(void)
  126. {
  127. struct irq_desc *desc;
  128. int legacy_count;
  129. int i;
  130. init_irq_default_affinity();
  131. /* initialize nr_irqs based on nr_cpu_ids */
  132. arch_probe_nr_irqs();
  133. printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d\n", NR_IRQS, nr_irqs);
  134. desc = irq_desc_legacy;
  135. legacy_count = ARRAY_SIZE(irq_desc_legacy);
  136. /* allocate irq_desc_ptrs array based on nr_irqs */
  137. irq_desc_ptrs = alloc_bootmem(nr_irqs * sizeof(void *));
  138. /* allocate based on nr_cpu_ids */
  139. /* FIXME: invert kstat_irgs, and it'd be a per_cpu_alloc'd thing */
  140. kstat_irqs_legacy = alloc_bootmem(NR_IRQS_LEGACY * nr_cpu_ids *
  141. sizeof(int));
  142. for (i = 0; i < legacy_count; i++) {
  143. desc[i].irq = i;
  144. desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
  145. lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
  146. alloc_desc_masks(&desc[i], 0, true);
  147. init_desc_masks(&desc[i]);
  148. irq_desc_ptrs[i] = desc + i;
  149. }
  150. for (i = legacy_count; i < nr_irqs; i++)
  151. irq_desc_ptrs[i] = NULL;
  152. return arch_early_irq_init();
  153. }
  154. struct irq_desc *irq_to_desc(unsigned int irq)
  155. {
  156. if (irq_desc_ptrs && irq < nr_irqs)
  157. return irq_desc_ptrs[irq];
  158. return NULL;
  159. }
  160. struct irq_desc *irq_to_desc_alloc_node(unsigned int irq, int node)
  161. {
  162. struct irq_desc *desc;
  163. unsigned long flags;
  164. if (irq >= nr_irqs) {
  165. WARN(1, "irq (%d) >= nr_irqs (%d) in irq_to_desc_alloc\n",
  166. irq, nr_irqs);
  167. return NULL;
  168. }
  169. desc = irq_desc_ptrs[irq];
  170. if (desc)
  171. return desc;
  172. spin_lock_irqsave(&sparse_irq_lock, flags);
  173. /* We have to check it to avoid races with another CPU */
  174. desc = irq_desc_ptrs[irq];
  175. if (desc)
  176. goto out_unlock;
  177. desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
  178. printk(KERN_DEBUG " alloc irq_desc for %d on node %d\n", irq, node);
  179. if (!desc) {
  180. printk(KERN_ERR "can not alloc irq_desc\n");
  181. BUG_ON(1);
  182. }
  183. init_one_irq_desc(irq, desc, node);
  184. irq_desc_ptrs[irq] = desc;
  185. out_unlock:
  186. spin_unlock_irqrestore(&sparse_irq_lock, flags);
  187. return desc;
  188. }
  189. #else /* !CONFIG_SPARSE_IRQ */
  190. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  191. [0 ... NR_IRQS-1] = {
  192. .status = IRQ_DISABLED,
  193. .chip = &no_irq_chip,
  194. .handle_irq = handle_bad_irq,
  195. .depth = 1,
  196. .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
  197. }
  198. };
  199. static unsigned int kstat_irqs_all[NR_IRQS][NR_CPUS];
  200. int __init early_irq_init(void)
  201. {
  202. struct irq_desc *desc;
  203. int count;
  204. int i;
  205. init_irq_default_affinity();
  206. printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
  207. desc = irq_desc;
  208. count = ARRAY_SIZE(irq_desc);
  209. for (i = 0; i < count; i++) {
  210. desc[i].irq = i;
  211. alloc_desc_masks(&desc[i], 0, true);
  212. init_desc_masks(&desc[i]);
  213. desc[i].kstat_irqs = kstat_irqs_all[i];
  214. }
  215. return arch_early_irq_init();
  216. }
  217. struct irq_desc *irq_to_desc(unsigned int irq)
  218. {
  219. return (irq < NR_IRQS) ? irq_desc + irq : NULL;
  220. }
  221. struct irq_desc *irq_to_desc_alloc_node(unsigned int irq, int node)
  222. {
  223. return irq_to_desc(irq);
  224. }
  225. #endif /* !CONFIG_SPARSE_IRQ */
  226. void clear_kstat_irqs(struct irq_desc *desc)
  227. {
  228. memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
  229. }
  230. /*
  231. * What should we do if we get a hw irq event on an illegal vector?
  232. * Each architecture has to answer this themself.
  233. */
  234. static void ack_bad(unsigned int irq)
  235. {
  236. struct irq_desc *desc = irq_to_desc(irq);
  237. print_irq_desc(irq, desc);
  238. ack_bad_irq(irq);
  239. }
  240. /*
  241. * NOP functions
  242. */
  243. static void noop(unsigned int irq)
  244. {
  245. }
  246. static unsigned int noop_ret(unsigned int irq)
  247. {
  248. return 0;
  249. }
  250. /*
  251. * Generic no controller implementation
  252. */
  253. struct irq_chip no_irq_chip = {
  254. .name = "none",
  255. .startup = noop_ret,
  256. .shutdown = noop,
  257. .enable = noop,
  258. .disable = noop,
  259. .ack = ack_bad,
  260. .end = noop,
  261. };
  262. /*
  263. * Generic dummy implementation which can be used for
  264. * real dumb interrupt sources
  265. */
  266. struct irq_chip dummy_irq_chip = {
  267. .name = "dummy",
  268. .startup = noop_ret,
  269. .shutdown = noop,
  270. .enable = noop,
  271. .disable = noop,
  272. .ack = noop,
  273. .mask = noop,
  274. .unmask = noop,
  275. .end = noop,
  276. };
  277. /*
  278. * Special, empty irq handler:
  279. */
  280. irqreturn_t no_action(int cpl, void *dev_id)
  281. {
  282. return IRQ_NONE;
  283. }
  284. static void warn_no_thread(unsigned int irq, struct irqaction *action)
  285. {
  286. if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags))
  287. return;
  288. printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD "
  289. "but no thread function available.", irq, action->name);
  290. }
  291. DEFINE_TRACE(irq_handler_entry);
  292. DEFINE_TRACE(irq_handler_exit);
  293. /**
  294. * handle_IRQ_event - irq action chain handler
  295. * @irq: the interrupt number
  296. * @action: the interrupt action chain for this irq
  297. *
  298. * Handles the action chain of an irq event
  299. */
  300. irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
  301. {
  302. irqreturn_t ret, retval = IRQ_NONE;
  303. unsigned int status = 0;
  304. WARN_ONCE(!in_irq(), "BUG: IRQ handler called from non-hardirq context!");
  305. if (!(action->flags & IRQF_DISABLED))
  306. local_irq_enable_in_hardirq();
  307. do {
  308. trace_irq_handler_entry(irq, action);
  309. ret = action->handler(irq, action->dev_id);
  310. trace_irq_handler_exit(irq, action, ret);
  311. switch (ret) {
  312. case IRQ_WAKE_THREAD:
  313. /*
  314. * Set result to handled so the spurious check
  315. * does not trigger.
  316. */
  317. ret = IRQ_HANDLED;
  318. /*
  319. * Catch drivers which return WAKE_THREAD but
  320. * did not set up a thread function
  321. */
  322. if (unlikely(!action->thread_fn)) {
  323. warn_no_thread(irq, action);
  324. break;
  325. }
  326. /*
  327. * Wake up the handler thread for this
  328. * action. In case the thread crashed and was
  329. * killed we just pretend that we handled the
  330. * interrupt. The hardirq handler above has
  331. * disabled the device interrupt, so no irq
  332. * storm is lurking.
  333. */
  334. if (likely(!test_bit(IRQTF_DIED,
  335. &action->thread_flags))) {
  336. set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
  337. wake_up_process(action->thread);
  338. }
  339. /* Fall through to add to randomness */
  340. case IRQ_HANDLED:
  341. status |= action->flags;
  342. break;
  343. default:
  344. break;
  345. }
  346. retval |= ret;
  347. action = action->next;
  348. } while (action);
  349. if (status & IRQF_SAMPLE_RANDOM)
  350. add_interrupt_randomness(irq);
  351. local_irq_disable();
  352. return retval;
  353. }
  354. #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
  355. #ifdef CONFIG_ENABLE_WARN_DEPRECATED
  356. # warning __do_IRQ is deprecated. Please convert to proper flow handlers
  357. #endif
  358. /**
  359. * __do_IRQ - original all in one highlevel IRQ handler
  360. * @irq: the interrupt number
  361. *
  362. * __do_IRQ handles all normal device IRQ's (the special
  363. * SMP cross-CPU interrupts have their own specific
  364. * handlers).
  365. *
  366. * This is the original x86 implementation which is used for every
  367. * interrupt type.
  368. */
  369. unsigned int __do_IRQ(unsigned int irq)
  370. {
  371. struct irq_desc *desc = irq_to_desc(irq);
  372. struct irqaction *action;
  373. unsigned int status;
  374. kstat_incr_irqs_this_cpu(irq, desc);
  375. if (CHECK_IRQ_PER_CPU(desc->status)) {
  376. irqreturn_t action_ret;
  377. /*
  378. * No locking required for CPU-local interrupts:
  379. */
  380. if (desc->chip->ack)
  381. desc->chip->ack(irq);
  382. if (likely(!(desc->status & IRQ_DISABLED))) {
  383. action_ret = handle_IRQ_event(irq, desc->action);
  384. if (!noirqdebug)
  385. note_interrupt(irq, desc, action_ret);
  386. }
  387. desc->chip->end(irq);
  388. return 1;
  389. }
  390. spin_lock(&desc->lock);
  391. if (desc->chip->ack)
  392. desc->chip->ack(irq);
  393. /*
  394. * REPLAY is when Linux resends an IRQ that was dropped earlier
  395. * WAITING is used by probe to mark irqs that are being tested
  396. */
  397. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  398. status |= IRQ_PENDING; /* we _want_ to handle it */
  399. /*
  400. * If the IRQ is disabled for whatever reason, we cannot
  401. * use the action we have.
  402. */
  403. action = NULL;
  404. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  405. action = desc->action;
  406. status &= ~IRQ_PENDING; /* we commit to handling */
  407. status |= IRQ_INPROGRESS; /* we are handling it */
  408. }
  409. desc->status = status;
  410. /*
  411. * If there is no IRQ handler or it was disabled, exit early.
  412. * Since we set PENDING, if another processor is handling
  413. * a different instance of this same irq, the other processor
  414. * will take care of it.
  415. */
  416. if (unlikely(!action))
  417. goto out;
  418. /*
  419. * Edge triggered interrupts need to remember
  420. * pending events.
  421. * This applies to any hw interrupts that allow a second
  422. * instance of the same irq to arrive while we are in do_IRQ
  423. * or in the handler. But the code here only handles the _second_
  424. * instance of the irq, not the third or fourth. So it is mostly
  425. * useful for irq hardware that does not mask cleanly in an
  426. * SMP environment.
  427. */
  428. for (;;) {
  429. irqreturn_t action_ret;
  430. spin_unlock(&desc->lock);
  431. action_ret = handle_IRQ_event(irq, action);
  432. if (!noirqdebug)
  433. note_interrupt(irq, desc, action_ret);
  434. spin_lock(&desc->lock);
  435. if (likely(!(desc->status & IRQ_PENDING)))
  436. break;
  437. desc->status &= ~IRQ_PENDING;
  438. }
  439. desc->status &= ~IRQ_INPROGRESS;
  440. out:
  441. /*
  442. * The ->end() handler has to deal with interrupts which got
  443. * disabled while the handler was running.
  444. */
  445. desc->chip->end(irq);
  446. spin_unlock(&desc->lock);
  447. return 1;
  448. }
  449. #endif
  450. void early_init_irq_lock_class(void)
  451. {
  452. struct irq_desc *desc;
  453. int i;
  454. for_each_irq_desc(i, desc) {
  455. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  456. }
  457. }
  458. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  459. {
  460. struct irq_desc *desc = irq_to_desc(irq);
  461. return desc ? desc->kstat_irqs[cpu] : 0;
  462. }
  463. EXPORT_SYMBOL(kstat_irqs_cpu);