handle.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 "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. #ifdef CONFIG_SMP
  74. .affinity = CPU_MASK_ALL
  75. #endif
  76. };
  77. void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
  78. {
  79. unsigned long bytes;
  80. char *ptr;
  81. int node;
  82. /* Compute how many bytes we need per irq and allocate them */
  83. bytes = nr * sizeof(unsigned int);
  84. node = cpu_to_node(cpu);
  85. ptr = kzalloc_node(bytes, GFP_ATOMIC, node);
  86. printk(KERN_DEBUG " alloc kstat_irqs on cpu %d node %d\n", cpu, node);
  87. if (ptr)
  88. desc->kstat_irqs = (unsigned int *)ptr;
  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. arch_init_chip_data(desc, cpu);
  105. }
  106. /*
  107. * Protect the sparse_irqs:
  108. */
  109. DEFINE_SPINLOCK(sparse_irq_lock);
  110. struct irq_desc *irq_desc_ptrs[NR_IRQS] __read_mostly;
  111. static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
  112. [0 ... NR_IRQS_LEGACY-1] = {
  113. .irq = -1,
  114. .status = IRQ_DISABLED,
  115. .chip = &no_irq_chip,
  116. .handle_irq = handle_bad_irq,
  117. .depth = 1,
  118. .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
  119. #ifdef CONFIG_SMP
  120. .affinity = CPU_MASK_ALL
  121. #endif
  122. }
  123. };
  124. /* FIXME: use bootmem alloc ...*/
  125. static unsigned int kstat_irqs_legacy[NR_IRQS_LEGACY][NR_CPUS];
  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. desc = irq_desc_legacy;
  133. legacy_count = ARRAY_SIZE(irq_desc_legacy);
  134. for (i = 0; i < legacy_count; i++) {
  135. desc[i].irq = i;
  136. desc[i].kstat_irqs = kstat_irqs_legacy[i];
  137. lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
  138. irq_desc_ptrs[i] = desc + i;
  139. }
  140. for (i = legacy_count; i < NR_IRQS; i++)
  141. irq_desc_ptrs[i] = NULL;
  142. return arch_early_irq_init();
  143. }
  144. struct irq_desc *irq_to_desc(unsigned int irq)
  145. {
  146. return (irq < NR_IRQS) ? irq_desc_ptrs[irq] : NULL;
  147. }
  148. struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
  149. {
  150. struct irq_desc *desc;
  151. unsigned long flags;
  152. int node;
  153. if (irq >= NR_IRQS) {
  154. printk(KERN_WARNING "irq >= NR_IRQS in irq_to_desc_alloc: %d %d\n",
  155. irq, NR_IRQS);
  156. WARN_ON(1);
  157. return NULL;
  158. }
  159. desc = irq_desc_ptrs[irq];
  160. if (desc)
  161. return desc;
  162. spin_lock_irqsave(&sparse_irq_lock, flags);
  163. /* We have to check it to avoid races with another CPU */
  164. desc = irq_desc_ptrs[irq];
  165. if (desc)
  166. goto out_unlock;
  167. node = cpu_to_node(cpu);
  168. desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
  169. printk(KERN_DEBUG " alloc irq_desc for %d on cpu %d node %d\n",
  170. irq, cpu, node);
  171. if (!desc) {
  172. printk(KERN_ERR "can not alloc irq_desc\n");
  173. BUG_ON(1);
  174. }
  175. init_one_irq_desc(irq, desc, cpu);
  176. irq_desc_ptrs[irq] = desc;
  177. out_unlock:
  178. spin_unlock_irqrestore(&sparse_irq_lock, flags);
  179. return desc;
  180. }
  181. #else /* !CONFIG_SPARSE_IRQ */
  182. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  183. [0 ... NR_IRQS-1] = {
  184. .status = IRQ_DISABLED,
  185. .chip = &no_irq_chip,
  186. .handle_irq = handle_bad_irq,
  187. .depth = 1,
  188. .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
  189. #ifdef CONFIG_SMP
  190. .affinity = CPU_MASK_ALL
  191. #endif
  192. }
  193. };
  194. int __init early_irq_init(void)
  195. {
  196. struct irq_desc *desc;
  197. int count;
  198. int i;
  199. init_irq_default_affinity();
  200. desc = irq_desc;
  201. count = ARRAY_SIZE(irq_desc);
  202. for (i = 0; i < count; i++)
  203. desc[i].irq = i;
  204. return arch_early_irq_init();
  205. }
  206. struct irq_desc *irq_to_desc(unsigned int irq)
  207. {
  208. return (irq < NR_IRQS) ? irq_desc + irq : NULL;
  209. }
  210. struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
  211. {
  212. return irq_to_desc(irq);
  213. }
  214. #endif /* !CONFIG_SPARSE_IRQ */
  215. /*
  216. * What should we do if we get a hw irq event on an illegal vector?
  217. * Each architecture has to answer this themself.
  218. */
  219. static void ack_bad(unsigned int irq)
  220. {
  221. struct irq_desc *desc = irq_to_desc(irq);
  222. print_irq_desc(irq, desc);
  223. ack_bad_irq(irq);
  224. }
  225. /*
  226. * NOP functions
  227. */
  228. static void noop(unsigned int irq)
  229. {
  230. }
  231. static unsigned int noop_ret(unsigned int irq)
  232. {
  233. return 0;
  234. }
  235. /*
  236. * Generic no controller implementation
  237. */
  238. struct irq_chip no_irq_chip = {
  239. .name = "none",
  240. .startup = noop_ret,
  241. .shutdown = noop,
  242. .enable = noop,
  243. .disable = noop,
  244. .ack = ack_bad,
  245. .end = noop,
  246. };
  247. /*
  248. * Generic dummy implementation which can be used for
  249. * real dumb interrupt sources
  250. */
  251. struct irq_chip dummy_irq_chip = {
  252. .name = "dummy",
  253. .startup = noop_ret,
  254. .shutdown = noop,
  255. .enable = noop,
  256. .disable = noop,
  257. .ack = noop,
  258. .mask = noop,
  259. .unmask = noop,
  260. .end = noop,
  261. };
  262. /*
  263. * Special, empty irq handler:
  264. */
  265. irqreturn_t no_action(int cpl, void *dev_id)
  266. {
  267. return IRQ_NONE;
  268. }
  269. DEFINE_TRACE(irq_handler_entry);
  270. DEFINE_TRACE(irq_handler_exit);
  271. /**
  272. * handle_IRQ_event - irq action chain handler
  273. * @irq: the interrupt number
  274. * @action: the interrupt action chain for this irq
  275. *
  276. * Handles the action chain of an irq event
  277. */
  278. irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
  279. {
  280. irqreturn_t ret, retval = IRQ_NONE;
  281. unsigned int status = 0;
  282. if (!(action->flags & IRQF_DISABLED))
  283. local_irq_enable_in_hardirq();
  284. do {
  285. trace_irq_handler_entry(irq, action);
  286. ret = action->handler(irq, action->dev_id);
  287. trace_irq_handler_exit(irq, action, ret);
  288. if (ret == IRQ_HANDLED)
  289. status |= action->flags;
  290. retval |= ret;
  291. action = action->next;
  292. } while (action);
  293. if (status & IRQF_SAMPLE_RANDOM)
  294. add_interrupt_randomness(irq);
  295. local_irq_disable();
  296. return retval;
  297. }
  298. #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
  299. /**
  300. * __do_IRQ - original all in one highlevel IRQ handler
  301. * @irq: the interrupt number
  302. *
  303. * __do_IRQ handles all normal device IRQ's (the special
  304. * SMP cross-CPU interrupts have their own specific
  305. * handlers).
  306. *
  307. * This is the original x86 implementation which is used for every
  308. * interrupt type.
  309. */
  310. unsigned int __do_IRQ(unsigned int irq)
  311. {
  312. struct irq_desc *desc = irq_to_desc(irq);
  313. struct irqaction *action;
  314. unsigned int status;
  315. kstat_incr_irqs_this_cpu(irq, desc);
  316. if (CHECK_IRQ_PER_CPU(desc->status)) {
  317. irqreturn_t action_ret;
  318. /*
  319. * No locking required for CPU-local interrupts:
  320. */
  321. if (desc->chip->ack) {
  322. desc->chip->ack(irq);
  323. /* get new one */
  324. desc = irq_remap_to_desc(irq, desc);
  325. }
  326. if (likely(!(desc->status & IRQ_DISABLED))) {
  327. action_ret = handle_IRQ_event(irq, desc->action);
  328. if (!noirqdebug)
  329. note_interrupt(irq, desc, action_ret);
  330. }
  331. desc->chip->end(irq);
  332. return 1;
  333. }
  334. spin_lock(&desc->lock);
  335. if (desc->chip->ack) {
  336. desc->chip->ack(irq);
  337. desc = irq_remap_to_desc(irq, desc);
  338. }
  339. /*
  340. * REPLAY is when Linux resends an IRQ that was dropped earlier
  341. * WAITING is used by probe to mark irqs that are being tested
  342. */
  343. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  344. status |= IRQ_PENDING; /* we _want_ to handle it */
  345. /*
  346. * If the IRQ is disabled for whatever reason, we cannot
  347. * use the action we have.
  348. */
  349. action = NULL;
  350. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  351. action = desc->action;
  352. status &= ~IRQ_PENDING; /* we commit to handling */
  353. status |= IRQ_INPROGRESS; /* we are handling it */
  354. }
  355. desc->status = status;
  356. /*
  357. * If there is no IRQ handler or it was disabled, exit early.
  358. * Since we set PENDING, if another processor is handling
  359. * a different instance of this same irq, the other processor
  360. * will take care of it.
  361. */
  362. if (unlikely(!action))
  363. goto out;
  364. /*
  365. * Edge triggered interrupts need to remember
  366. * pending events.
  367. * This applies to any hw interrupts that allow a second
  368. * instance of the same irq to arrive while we are in do_IRQ
  369. * or in the handler. But the code here only handles the _second_
  370. * instance of the irq, not the third or fourth. So it is mostly
  371. * useful for irq hardware that does not mask cleanly in an
  372. * SMP environment.
  373. */
  374. for (;;) {
  375. irqreturn_t action_ret;
  376. spin_unlock(&desc->lock);
  377. action_ret = handle_IRQ_event(irq, action);
  378. if (!noirqdebug)
  379. note_interrupt(irq, desc, action_ret);
  380. spin_lock(&desc->lock);
  381. if (likely(!(desc->status & IRQ_PENDING)))
  382. break;
  383. desc->status &= ~IRQ_PENDING;
  384. }
  385. desc->status &= ~IRQ_INPROGRESS;
  386. out:
  387. /*
  388. * The ->end() handler has to deal with interrupts which got
  389. * disabled while the handler was running.
  390. */
  391. desc->chip->end(irq);
  392. spin_unlock(&desc->lock);
  393. return 1;
  394. }
  395. #endif
  396. void early_init_irq_lock_class(void)
  397. {
  398. struct irq_desc *desc;
  399. int i;
  400. for_each_irq_desc(i, desc) {
  401. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  402. }
  403. }
  404. #ifdef CONFIG_SPARSE_IRQ
  405. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  406. {
  407. struct irq_desc *desc = irq_to_desc(irq);
  408. return desc ? desc->kstat_irqs[cpu] : 0;
  409. }
  410. #endif
  411. EXPORT_SYMBOL(kstat_irqs_cpu);