handle.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 "internals.h"
  18. #ifdef CONFIG_TRACE_IRQFLAGS
  19. /*
  20. * lockdep: we want to handle all irq_desc locks as a single lock-class:
  21. */
  22. static struct lock_class_key irq_desc_lock_class;
  23. #endif
  24. /**
  25. * handle_bad_irq - handle spurious and unhandled irqs
  26. * @irq: the interrupt number
  27. * @desc: description of the interrupt
  28. *
  29. * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
  30. */
  31. void
  32. handle_bad_irq(unsigned int irq, struct irq_desc *desc)
  33. {
  34. print_irq_desc(irq, desc);
  35. kstat_irqs_this_cpu(desc)++;
  36. ack_bad_irq(irq);
  37. }
  38. /*
  39. * Linux has a controller-independent interrupt architecture.
  40. * Every controller has a 'controller-template', that is used
  41. * by the main code to do the right thing. Each driver-visible
  42. * interrupt source is transparently wired to the appropriate
  43. * controller. Thus drivers need not be aware of the
  44. * interrupt-controller.
  45. *
  46. * The code is designed to be easily extended with new/different
  47. * interrupt controllers, without having to do assembly magic or
  48. * having to touch the generic code.
  49. *
  50. * Controller mappings for all interrupt sources:
  51. */
  52. int nr_irqs = NR_IRQS;
  53. EXPORT_SYMBOL_GPL(nr_irqs);
  54. #ifdef CONFIG_HAVE_DYN_ARRAY
  55. static struct irq_desc irq_desc_init = {
  56. .irq = -1U,
  57. .status = IRQ_DISABLED,
  58. .chip = &no_irq_chip,
  59. .handle_irq = handle_bad_irq,
  60. .depth = 1,
  61. .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
  62. #ifdef CONFIG_SMP
  63. .affinity = CPU_MASK_ALL
  64. #endif
  65. };
  66. static void init_one_irq_desc(struct irq_desc *desc)
  67. {
  68. memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
  69. #ifdef CONFIG_TRACE_IRQFLAGS
  70. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  71. #endif
  72. }
  73. extern int after_bootmem;
  74. extern void *__alloc_bootmem_nopanic(unsigned long size,
  75. unsigned long align,
  76. unsigned long goal);
  77. static void init_kstat_irqs(struct irq_desc *desc, int nr_desc, int nr)
  78. {
  79. unsigned long bytes, total_bytes;
  80. char *ptr;
  81. int i;
  82. unsigned long phys;
  83. /* Compute how many bytes we need per irq and allocate them */
  84. bytes = nr * sizeof(unsigned int);
  85. total_bytes = bytes * nr_desc;
  86. if (after_bootmem)
  87. ptr = kzalloc(total_bytes, GFP_ATOMIC);
  88. else
  89. ptr = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
  90. if (!ptr)
  91. panic(" can not allocate kstat_irqs\n");
  92. phys = __pa(ptr);
  93. printk(KERN_DEBUG "kstat_irqs ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
  94. for (i = 0; i < nr_desc; i++) {
  95. desc[i].kstat_irqs = (unsigned int *)ptr;
  96. ptr += bytes;
  97. }
  98. }
  99. static void __init init_work(void *data)
  100. {
  101. struct dyn_array *da = data;
  102. int i;
  103. struct irq_desc *desc;
  104. desc = *da->name;
  105. for (i = 0; i < *da->nr; i++) {
  106. init_one_irq_desc(&desc[i]);
  107. #ifndef CONFIG_HAVE_SPARSE_IRQ
  108. desc[i].irq = i;
  109. #endif
  110. }
  111. #ifdef CONFIG_HAVE_SPARSE_IRQ
  112. for (i = 1; i < *da->nr; i++)
  113. desc[i-1].next = &desc[i];
  114. #endif
  115. /* init kstat_irqs, nr_cpu_ids is ready already */
  116. init_kstat_irqs(desc, *da->nr, nr_cpu_ids);
  117. }
  118. #ifdef CONFIG_HAVE_SPARSE_IRQ
  119. static int nr_irq_desc = 32;
  120. static int __init parse_nr_irq_desc(char *arg)
  121. {
  122. if (arg)
  123. nr_irq_desc = simple_strtoul(arg, NULL, 0);
  124. return 0;
  125. }
  126. early_param("nr_irq_desc", parse_nr_irq_desc);
  127. struct irq_desc *sparse_irqs;
  128. DEFINE_DYN_ARRAY(sparse_irqs, sizeof(struct irq_desc), nr_irq_desc, PAGE_SIZE, init_work);
  129. struct irq_desc *__irq_to_desc(unsigned int irq)
  130. {
  131. struct irq_desc *desc;
  132. BUG_ON(irq == -1U);
  133. desc = &sparse_irqs[0];
  134. while (desc) {
  135. if (desc->irq == irq)
  136. return desc;
  137. if (desc->irq == -1U)
  138. return NULL;
  139. desc = desc->next;
  140. }
  141. return NULL;
  142. }
  143. struct irq_desc *irq_to_desc(unsigned int irq)
  144. {
  145. struct irq_desc *desc, *desc_pri;
  146. int i;
  147. int count = 0;
  148. unsigned long phys;
  149. unsigned long total_bytes;
  150. BUG_ON(irq == -1U);
  151. desc_pri = desc = &sparse_irqs[0];
  152. while (desc) {
  153. if (desc->irq == irq)
  154. return desc;
  155. if (desc->irq == -1U) {
  156. desc->irq = irq;
  157. return desc;
  158. }
  159. desc_pri = desc;
  160. desc = desc->next;
  161. count++;
  162. }
  163. /*
  164. * we run out of pre-allocate ones, allocate more
  165. */
  166. printk(KERN_DEBUG "try to get more irq_desc %d\n", nr_irq_desc);
  167. total_bytes = sizeof(struct irq_desc) * nr_irq_desc;
  168. if (after_bootmem)
  169. desc = kzalloc(total_bytes, GFP_ATOMIC);
  170. else
  171. desc = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
  172. if (!desc)
  173. panic("please boot with nr_irq_desc= %d\n", count * 2);
  174. phys = __pa(desc);
  175. printk(KERN_DEBUG "irq_desc ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
  176. for (i = 0; i < nr_irq_desc; i++)
  177. init_one_irq_desc(&desc[i]);
  178. for (i = 1; i < nr_irq_desc; i++)
  179. desc[i-1].next = &desc[i];
  180. /* init kstat_irqs, nr_cpu_ids is ready already */
  181. init_kstat_irqs(desc, nr_irq_desc, nr_cpu_ids);
  182. desc->irq = irq;
  183. desc_pri->next = desc;
  184. return desc;
  185. }
  186. #else
  187. struct irq_desc *irq_desc;
  188. DEFINE_DYN_ARRAY(irq_desc, sizeof(struct irq_desc), nr_irqs, PAGE_SIZE, init_work);
  189. #endif
  190. #else
  191. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  192. [0 ... NR_IRQS-1] = {
  193. .status = IRQ_DISABLED,
  194. .chip = &no_irq_chip,
  195. .handle_irq = handle_bad_irq,
  196. .depth = 1,
  197. .lock = __SPIN_LOCK_UNLOCKED(sparse_irqs->lock),
  198. #ifdef CONFIG_SMP
  199. .affinity = CPU_MASK_ALL
  200. #endif
  201. }
  202. };
  203. #endif
  204. #ifndef CONFIG_HAVE_SPARSE_IRQ
  205. struct irq_desc *irq_to_desc(unsigned int irq)
  206. {
  207. if (irq < nr_irqs)
  208. return &irq_desc[irq];
  209. return NULL;
  210. }
  211. struct irq_desc *__irq_to_desc(unsigned int irq)
  212. {
  213. return irq_to_desc(irq);
  214. }
  215. #endif
  216. /*
  217. * What should we do if we get a hw irq event on an illegal vector?
  218. * Each architecture has to answer this themself.
  219. */
  220. static void ack_bad(unsigned int irq)
  221. {
  222. struct irq_desc *desc;
  223. desc = irq_to_desc(irq);
  224. print_irq_desc(irq, desc);
  225. ack_bad_irq(irq);
  226. }
  227. /*
  228. * NOP functions
  229. */
  230. static void noop(unsigned int irq)
  231. {
  232. }
  233. static unsigned int noop_ret(unsigned int irq)
  234. {
  235. return 0;
  236. }
  237. /*
  238. * Generic no controller implementation
  239. */
  240. struct irq_chip no_irq_chip = {
  241. .name = "none",
  242. .startup = noop_ret,
  243. .shutdown = noop,
  244. .enable = noop,
  245. .disable = noop,
  246. .ack = ack_bad,
  247. .end = noop,
  248. };
  249. /*
  250. * Generic dummy implementation which can be used for
  251. * real dumb interrupt sources
  252. */
  253. struct irq_chip dummy_irq_chip = {
  254. .name = "dummy",
  255. .startup = noop_ret,
  256. .shutdown = noop,
  257. .enable = noop,
  258. .disable = noop,
  259. .ack = noop,
  260. .mask = noop,
  261. .unmask = noop,
  262. .end = noop,
  263. };
  264. /*
  265. * Special, empty irq handler:
  266. */
  267. irqreturn_t no_action(int cpl, void *dev_id)
  268. {
  269. return IRQ_NONE;
  270. }
  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. ret = action->handler(irq, action->dev_id);
  286. if (ret == IRQ_HANDLED)
  287. status |= action->flags;
  288. retval |= ret;
  289. action = action->next;
  290. } while (action);
  291. if (status & IRQF_SAMPLE_RANDOM)
  292. add_interrupt_randomness(irq);
  293. local_irq_disable();
  294. return retval;
  295. }
  296. #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
  297. /**
  298. * __do_IRQ - original all in one highlevel IRQ handler
  299. * @irq: the interrupt number
  300. *
  301. * __do_IRQ handles all normal device IRQ's (the special
  302. * SMP cross-CPU interrupts have their own specific
  303. * handlers).
  304. *
  305. * This is the original x86 implementation which is used for every
  306. * interrupt type.
  307. */
  308. unsigned int __do_IRQ(unsigned int irq)
  309. {
  310. struct irq_desc *desc = irq_to_desc(irq);
  311. struct irqaction *action;
  312. unsigned int status;
  313. kstat_irqs_this_cpu(desc)++;
  314. if (CHECK_IRQ_PER_CPU(desc->status)) {
  315. irqreturn_t action_ret;
  316. /*
  317. * No locking required for CPU-local interrupts:
  318. */
  319. if (desc->chip->ack)
  320. desc->chip->ack(irq);
  321. if (likely(!(desc->status & IRQ_DISABLED))) {
  322. action_ret = handle_IRQ_event(irq, desc->action);
  323. if (!noirqdebug)
  324. note_interrupt(irq, desc, action_ret);
  325. }
  326. desc->chip->end(irq);
  327. return 1;
  328. }
  329. spin_lock(&desc->lock);
  330. if (desc->chip->ack)
  331. desc->chip->ack(irq);
  332. /*
  333. * REPLAY is when Linux resends an IRQ that was dropped earlier
  334. * WAITING is used by probe to mark irqs that are being tested
  335. */
  336. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  337. status |= IRQ_PENDING; /* we _want_ to handle it */
  338. /*
  339. * If the IRQ is disabled for whatever reason, we cannot
  340. * use the action we have.
  341. */
  342. action = NULL;
  343. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  344. action = desc->action;
  345. status &= ~IRQ_PENDING; /* we commit to handling */
  346. status |= IRQ_INPROGRESS; /* we are handling it */
  347. }
  348. desc->status = status;
  349. /*
  350. * If there is no IRQ handler or it was disabled, exit early.
  351. * Since we set PENDING, if another processor is handling
  352. * a different instance of this same irq, the other processor
  353. * will take care of it.
  354. */
  355. if (unlikely(!action))
  356. goto out;
  357. /*
  358. * Edge triggered interrupts need to remember
  359. * pending events.
  360. * This applies to any hw interrupts that allow a second
  361. * instance of the same irq to arrive while we are in do_IRQ
  362. * or in the handler. But the code here only handles the _second_
  363. * instance of the irq, not the third or fourth. So it is mostly
  364. * useful for irq hardware that does not mask cleanly in an
  365. * SMP environment.
  366. */
  367. for (;;) {
  368. irqreturn_t action_ret;
  369. spin_unlock(&desc->lock);
  370. action_ret = handle_IRQ_event(irq, action);
  371. if (!noirqdebug)
  372. note_interrupt(irq, desc, action_ret);
  373. spin_lock(&desc->lock);
  374. if (likely(!(desc->status & IRQ_PENDING)))
  375. break;
  376. desc->status &= ~IRQ_PENDING;
  377. }
  378. desc->status &= ~IRQ_INPROGRESS;
  379. out:
  380. /*
  381. * The ->end() handler has to deal with interrupts which got
  382. * disabled while the handler was running.
  383. */
  384. desc->chip->end(irq);
  385. spin_unlock(&desc->lock);
  386. return 1;
  387. }
  388. #endif
  389. #ifdef CONFIG_TRACE_IRQFLAGS
  390. void early_init_irq_lock_class(void)
  391. {
  392. #ifndef CONFIG_HAVE_DYN_ARRAY
  393. int i;
  394. for (i = 0; i < nr_irqs; i++)
  395. lockdep_set_class(&irq_desc[i].lock, &irq_desc_lock_class);
  396. #endif
  397. }
  398. #endif
  399. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  400. {
  401. struct irq_desc *desc = irq_to_desc(irq);
  402. return desc->kstat_irqs[cpu];
  403. }
  404. EXPORT_SYMBOL(kstat_irqs_cpu);