handle.c 13 KB

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