handle.c 13 KB

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