handle.c 13 KB

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