handle.c 11 KB

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