handle.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. {
  168. /* double check if some one mess up the list */
  169. struct irq_desc *desc;
  170. int count = 0;
  171. desc = &sparse_irqs[0];
  172. while (desc) {
  173. printk(KERN_DEBUG "found irq_desc for irq %d\n", desc->irq);
  174. if (desc->next)
  175. printk(KERN_DEBUG "found irq_desc for irq %d and next will be irq %d\n", desc->irq, desc->next->irq);
  176. desc = desc->next;
  177. count++;
  178. }
  179. printk(KERN_DEBUG "all preallocted %d\n", count);
  180. }
  181. total_bytes = sizeof(struct irq_desc) * nr_irq_desc;
  182. if (after_bootmem)
  183. desc = kzalloc(total_bytes, GFP_ATOMIC);
  184. else
  185. desc = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
  186. if (!desc)
  187. panic("please boot with nr_irq_desc= %d\n", count * 2);
  188. phys = __pa(desc);
  189. printk(KERN_DEBUG "irq_desc ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
  190. for (i = 0; i < nr_irq_desc; i++)
  191. init_one_irq_desc(&desc[i]);
  192. for (i = 1; i < nr_irq_desc; i++)
  193. desc[i-1].next = &desc[i];
  194. /* init kstat_irqs, nr_cpu_ids is ready already */
  195. init_kstat_irqs(desc, nr_irq_desc, nr_cpu_ids);
  196. desc->irq = irq;
  197. desc_pri->next = desc;
  198. {
  199. /* double check if some one mess up the list */
  200. struct irq_desc *desc;
  201. int count = 0;
  202. desc = &sparse_irqs[0];
  203. while (desc) {
  204. printk(KERN_DEBUG "1 found irq_desc for irq %d\n", desc->irq);
  205. if (desc->next)
  206. printk(KERN_DEBUG "1 found irq_desc for irq %d and next will be irq %d\n", desc->irq, desc->next->irq);
  207. desc = desc->next;
  208. count++;
  209. }
  210. printk(KERN_DEBUG "1 all preallocted %d\n", count);
  211. }
  212. return desc;
  213. }
  214. #else
  215. struct irq_desc *irq_desc;
  216. DEFINE_DYN_ARRAY(irq_desc, sizeof(struct irq_desc), nr_irqs, PAGE_SIZE, init_work);
  217. #endif
  218. #else
  219. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  220. [0 ... NR_IRQS-1] = {
  221. .status = IRQ_DISABLED,
  222. .chip = &no_irq_chip,
  223. .handle_irq = handle_bad_irq,
  224. .depth = 1,
  225. .lock = __SPIN_LOCK_UNLOCKED(sparse_irqs->lock),
  226. #ifdef CONFIG_SMP
  227. .affinity = CPU_MASK_ALL
  228. #endif
  229. }
  230. };
  231. #endif
  232. #ifndef CONFIG_HAVE_SPARSE_IRQ
  233. struct irq_desc *irq_to_desc(unsigned int irq)
  234. {
  235. if (irq < nr_irqs)
  236. return &irq_desc[irq];
  237. return NULL;
  238. }
  239. struct irq_desc *__irq_to_desc(unsigned int irq)
  240. {
  241. return irq_to_desc(irq);
  242. }
  243. #endif
  244. /*
  245. * What should we do if we get a hw irq event on an illegal vector?
  246. * Each architecture has to answer this themself.
  247. */
  248. static void ack_bad(unsigned int irq)
  249. {
  250. struct irq_desc *desc;
  251. desc = irq_to_desc(irq);
  252. print_irq_desc(irq, desc);
  253. ack_bad_irq(irq);
  254. }
  255. /*
  256. * NOP functions
  257. */
  258. static void noop(unsigned int irq)
  259. {
  260. }
  261. static unsigned int noop_ret(unsigned int irq)
  262. {
  263. return 0;
  264. }
  265. /*
  266. * Generic no controller implementation
  267. */
  268. struct irq_chip no_irq_chip = {
  269. .name = "none",
  270. .startup = noop_ret,
  271. .shutdown = noop,
  272. .enable = noop,
  273. .disable = noop,
  274. .ack = ack_bad,
  275. .end = noop,
  276. };
  277. /*
  278. * Generic dummy implementation which can be used for
  279. * real dumb interrupt sources
  280. */
  281. struct irq_chip dummy_irq_chip = {
  282. .name = "dummy",
  283. .startup = noop_ret,
  284. .shutdown = noop,
  285. .enable = noop,
  286. .disable = noop,
  287. .ack = noop,
  288. .mask = noop,
  289. .unmask = noop,
  290. .end = 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. /**
  300. * handle_IRQ_event - irq action chain handler
  301. * @irq: the interrupt number
  302. * @action: the interrupt action chain for this irq
  303. *
  304. * Handles the action chain of an irq event
  305. */
  306. irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
  307. {
  308. irqreturn_t ret, retval = IRQ_NONE;
  309. unsigned int status = 0;
  310. if (!(action->flags & IRQF_DISABLED))
  311. local_irq_enable_in_hardirq();
  312. do {
  313. ret = action->handler(irq, action->dev_id);
  314. if (ret == IRQ_HANDLED)
  315. status |= action->flags;
  316. retval |= ret;
  317. action = action->next;
  318. } while (action);
  319. if (status & IRQF_SAMPLE_RANDOM)
  320. add_interrupt_randomness(irq);
  321. local_irq_disable();
  322. return retval;
  323. }
  324. #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
  325. /**
  326. * __do_IRQ - original all in one highlevel IRQ handler
  327. * @irq: the interrupt number
  328. *
  329. * __do_IRQ handles all normal device IRQ's (the special
  330. * SMP cross-CPU interrupts have their own specific
  331. * handlers).
  332. *
  333. * This is the original x86 implementation which is used for every
  334. * interrupt type.
  335. */
  336. unsigned int __do_IRQ(unsigned int irq)
  337. {
  338. struct irq_desc *desc = irq_to_desc(irq);
  339. struct irqaction *action;
  340. unsigned int status;
  341. kstat_irqs_this_cpu(desc)++;
  342. if (CHECK_IRQ_PER_CPU(desc->status)) {
  343. irqreturn_t action_ret;
  344. /*
  345. * No locking required for CPU-local interrupts:
  346. */
  347. if (desc->chip->ack)
  348. desc->chip->ack(irq);
  349. if (likely(!(desc->status & IRQ_DISABLED))) {
  350. action_ret = handle_IRQ_event(irq, desc->action);
  351. if (!noirqdebug)
  352. note_interrupt(irq, desc, action_ret);
  353. }
  354. desc->chip->end(irq);
  355. return 1;
  356. }
  357. spin_lock(&desc->lock);
  358. if (desc->chip->ack)
  359. desc->chip->ack(irq);
  360. /*
  361. * REPLAY is when Linux resends an IRQ that was dropped earlier
  362. * WAITING is used by probe to mark irqs that are being tested
  363. */
  364. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  365. status |= IRQ_PENDING; /* we _want_ to handle it */
  366. /*
  367. * If the IRQ is disabled for whatever reason, we cannot
  368. * use the action we have.
  369. */
  370. action = NULL;
  371. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  372. action = desc->action;
  373. status &= ~IRQ_PENDING; /* we commit to handling */
  374. status |= IRQ_INPROGRESS; /* we are handling it */
  375. }
  376. desc->status = status;
  377. /*
  378. * If there is no IRQ handler or it was disabled, exit early.
  379. * Since we set PENDING, if another processor is handling
  380. * a different instance of this same irq, the other processor
  381. * will take care of it.
  382. */
  383. if (unlikely(!action))
  384. goto out;
  385. /*
  386. * Edge triggered interrupts need to remember
  387. * pending events.
  388. * This applies to any hw interrupts that allow a second
  389. * instance of the same irq to arrive while we are in do_IRQ
  390. * or in the handler. But the code here only handles the _second_
  391. * instance of the irq, not the third or fourth. So it is mostly
  392. * useful for irq hardware that does not mask cleanly in an
  393. * SMP environment.
  394. */
  395. for (;;) {
  396. irqreturn_t action_ret;
  397. spin_unlock(&desc->lock);
  398. action_ret = handle_IRQ_event(irq, action);
  399. if (!noirqdebug)
  400. note_interrupt(irq, desc, action_ret);
  401. spin_lock(&desc->lock);
  402. if (likely(!(desc->status & IRQ_PENDING)))
  403. break;
  404. desc->status &= ~IRQ_PENDING;
  405. }
  406. desc->status &= ~IRQ_INPROGRESS;
  407. out:
  408. /*
  409. * The ->end() handler has to deal with interrupts which got
  410. * disabled while the handler was running.
  411. */
  412. desc->chip->end(irq);
  413. spin_unlock(&desc->lock);
  414. return 1;
  415. }
  416. #endif
  417. #ifdef CONFIG_TRACE_IRQFLAGS
  418. void early_init_irq_lock_class(void)
  419. {
  420. #ifndef CONFIG_HAVE_DYN_ARRAY
  421. int i;
  422. for (i = 0; i < nr_irqs; i++)
  423. lockdep_set_class(&irq_desc[i].lock, &irq_desc_lock_class);
  424. #endif
  425. }
  426. #endif
  427. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  428. {
  429. struct irq_desc *desc = irq_to_desc(irq);
  430. return desc->kstat_irqs[cpu];
  431. }
  432. EXPORT_SYMBOL(kstat_irqs_cpu);