handle.c 13 KB

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