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