handle.c 11 KB

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