handle.c 13 KB

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