irqdesc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
  3. * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
  4. *
  5. * This file contains the interrupt descriptor management code
  6. *
  7. * Detailed information is available in Documentation/DocBook/genericirq
  8. *
  9. */
  10. #include <linux/irq.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/radix-tree.h>
  16. #include <linux/bitmap.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. #if defined(CONFIG_SMP)
  23. static void __init init_irq_default_affinity(void)
  24. {
  25. alloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  26. cpumask_setall(irq_default_affinity);
  27. }
  28. #else
  29. static void __init init_irq_default_affinity(void)
  30. {
  31. }
  32. #endif
  33. #ifdef CONFIG_SMP
  34. static int alloc_masks(struct irq_desc *desc, gfp_t gfp, int node)
  35. {
  36. if (!zalloc_cpumask_var_node(&desc->irq_data.affinity, gfp, node))
  37. return -ENOMEM;
  38. #ifdef CONFIG_GENERIC_PENDING_IRQ
  39. if (!zalloc_cpumask_var_node(&desc->pending_mask, gfp, node)) {
  40. free_cpumask_var(desc->irq_data.affinity);
  41. return -ENOMEM;
  42. }
  43. #endif
  44. return 0;
  45. }
  46. static void desc_smp_init(struct irq_desc *desc, int node)
  47. {
  48. desc->irq_data.node = node;
  49. cpumask_copy(desc->irq_data.affinity, irq_default_affinity);
  50. #ifdef CONFIG_GENERIC_PENDING_IRQ
  51. cpumask_clear(desc->pending_mask);
  52. #endif
  53. }
  54. static inline int desc_node(struct irq_desc *desc)
  55. {
  56. return desc->irq_data.node;
  57. }
  58. #else
  59. static inline int
  60. alloc_masks(struct irq_desc *desc, gfp_t gfp, int node) { return 0; }
  61. static inline void desc_smp_init(struct irq_desc *desc, int node) { }
  62. static inline int desc_node(struct irq_desc *desc) { return 0; }
  63. #endif
  64. static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node)
  65. {
  66. int cpu;
  67. desc->irq_data.irq = irq;
  68. desc->irq_data.chip = &no_irq_chip;
  69. desc->irq_data.chip_data = NULL;
  70. desc->irq_data.handler_data = NULL;
  71. desc->irq_data.msi_desc = NULL;
  72. irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
  73. irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
  74. desc->handle_irq = handle_bad_irq;
  75. desc->depth = 1;
  76. desc->irq_count = 0;
  77. desc->irqs_unhandled = 0;
  78. desc->name = NULL;
  79. for_each_possible_cpu(cpu)
  80. *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
  81. desc_smp_init(desc, node);
  82. }
  83. int nr_irqs = NR_IRQS;
  84. EXPORT_SYMBOL_GPL(nr_irqs);
  85. static DEFINE_MUTEX(sparse_irq_lock);
  86. static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
  87. #ifdef CONFIG_SPARSE_IRQ
  88. static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
  89. static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
  90. {
  91. radix_tree_insert(&irq_desc_tree, irq, desc);
  92. }
  93. struct irq_desc *irq_to_desc(unsigned int irq)
  94. {
  95. return radix_tree_lookup(&irq_desc_tree, irq);
  96. }
  97. static void delete_irq_desc(unsigned int irq)
  98. {
  99. radix_tree_delete(&irq_desc_tree, irq);
  100. }
  101. #ifdef CONFIG_SMP
  102. static void free_masks(struct irq_desc *desc)
  103. {
  104. #ifdef CONFIG_GENERIC_PENDING_IRQ
  105. free_cpumask_var(desc->pending_mask);
  106. #endif
  107. free_cpumask_var(desc->irq_data.affinity);
  108. }
  109. #else
  110. static inline void free_masks(struct irq_desc *desc) { }
  111. #endif
  112. static struct irq_desc *alloc_desc(int irq, int node)
  113. {
  114. struct irq_desc *desc;
  115. gfp_t gfp = GFP_KERNEL;
  116. desc = kzalloc_node(sizeof(*desc), gfp, node);
  117. if (!desc)
  118. return NULL;
  119. /* allocate based on nr_cpu_ids */
  120. desc->kstat_irqs = alloc_percpu(unsigned int);
  121. if (!desc->kstat_irqs)
  122. goto err_desc;
  123. if (alloc_masks(desc, gfp, node))
  124. goto err_kstat;
  125. raw_spin_lock_init(&desc->lock);
  126. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  127. desc_set_defaults(irq, desc, node);
  128. return desc;
  129. err_kstat:
  130. free_percpu(desc->kstat_irqs);
  131. err_desc:
  132. kfree(desc);
  133. return NULL;
  134. }
  135. static void free_desc(unsigned int irq)
  136. {
  137. struct irq_desc *desc = irq_to_desc(irq);
  138. unregister_irq_proc(irq, desc);
  139. mutex_lock(&sparse_irq_lock);
  140. delete_irq_desc(irq);
  141. mutex_unlock(&sparse_irq_lock);
  142. free_masks(desc);
  143. free_percpu(desc->kstat_irqs);
  144. kfree(desc);
  145. }
  146. static int alloc_descs(unsigned int start, unsigned int cnt, int node)
  147. {
  148. struct irq_desc *desc;
  149. int i;
  150. for (i = 0; i < cnt; i++) {
  151. desc = alloc_desc(start + i, node);
  152. if (!desc)
  153. goto err;
  154. mutex_lock(&sparse_irq_lock);
  155. irq_insert_desc(start + i, desc);
  156. mutex_unlock(&sparse_irq_lock);
  157. }
  158. return start;
  159. err:
  160. for (i--; i >= 0; i--)
  161. free_desc(start + i);
  162. mutex_lock(&sparse_irq_lock);
  163. bitmap_clear(allocated_irqs, start, cnt);
  164. mutex_unlock(&sparse_irq_lock);
  165. return -ENOMEM;
  166. }
  167. static int irq_expand_nr_irqs(unsigned int nr)
  168. {
  169. if (nr > IRQ_BITMAP_BITS)
  170. return -ENOMEM;
  171. nr_irqs = nr;
  172. return 0;
  173. }
  174. int __init early_irq_init(void)
  175. {
  176. int i, initcnt, node = first_online_node;
  177. struct irq_desc *desc;
  178. init_irq_default_affinity();
  179. /* Let arch update nr_irqs and return the nr of preallocated irqs */
  180. initcnt = arch_probe_nr_irqs();
  181. printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt);
  182. if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
  183. nr_irqs = IRQ_BITMAP_BITS;
  184. if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
  185. initcnt = IRQ_BITMAP_BITS;
  186. if (initcnt > nr_irqs)
  187. nr_irqs = initcnt;
  188. for (i = 0; i < initcnt; i++) {
  189. desc = alloc_desc(i, node);
  190. set_bit(i, allocated_irqs);
  191. irq_insert_desc(i, desc);
  192. }
  193. return arch_early_irq_init();
  194. }
  195. #else /* !CONFIG_SPARSE_IRQ */
  196. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  197. [0 ... NR_IRQS-1] = {
  198. .handle_irq = handle_bad_irq,
  199. .depth = 1,
  200. .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
  201. }
  202. };
  203. int __init early_irq_init(void)
  204. {
  205. int count, i, node = first_online_node;
  206. struct irq_desc *desc;
  207. init_irq_default_affinity();
  208. printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
  209. desc = irq_desc;
  210. count = ARRAY_SIZE(irq_desc);
  211. for (i = 0; i < count; i++) {
  212. desc[i].kstat_irqs = alloc_percpu(unsigned int);
  213. alloc_masks(&desc[i], GFP_KERNEL, node);
  214. raw_spin_lock_init(&desc[i].lock);
  215. lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
  216. desc_set_defaults(i, &desc[i], node);
  217. }
  218. return arch_early_irq_init();
  219. }
  220. struct irq_desc *irq_to_desc(unsigned int irq)
  221. {
  222. return (irq < NR_IRQS) ? irq_desc + irq : NULL;
  223. }
  224. static void free_desc(unsigned int irq)
  225. {
  226. dynamic_irq_cleanup(irq);
  227. }
  228. static inline int alloc_descs(unsigned int start, unsigned int cnt, int node)
  229. {
  230. return start;
  231. }
  232. static int irq_expand_nr_irqs(unsigned int nr)
  233. {
  234. return -ENOMEM;
  235. }
  236. #endif /* !CONFIG_SPARSE_IRQ */
  237. /**
  238. * generic_handle_irq - Invoke the handler for a particular irq
  239. * @irq: The irq number to handle
  240. *
  241. */
  242. int generic_handle_irq(unsigned int irq)
  243. {
  244. struct irq_desc *desc = irq_to_desc(irq);
  245. if (!desc)
  246. return -EINVAL;
  247. generic_handle_irq_desc(irq, desc);
  248. return 0;
  249. }
  250. EXPORT_SYMBOL_GPL(generic_handle_irq);
  251. /* Dynamic interrupt handling */
  252. /**
  253. * irq_free_descs - free irq descriptors
  254. * @from: Start of descriptor range
  255. * @cnt: Number of consecutive irqs to free
  256. */
  257. void irq_free_descs(unsigned int from, unsigned int cnt)
  258. {
  259. int i;
  260. if (from >= nr_irqs || (from + cnt) > nr_irqs)
  261. return;
  262. for (i = 0; i < cnt; i++)
  263. free_desc(from + i);
  264. mutex_lock(&sparse_irq_lock);
  265. bitmap_clear(allocated_irqs, from, cnt);
  266. mutex_unlock(&sparse_irq_lock);
  267. }
  268. EXPORT_SYMBOL_GPL(irq_free_descs);
  269. /**
  270. * irq_alloc_descs - allocate and initialize a range of irq descriptors
  271. * @irq: Allocate for specific irq number if irq >= 0
  272. * @from: Start the search from this irq number
  273. * @cnt: Number of consecutive irqs to allocate.
  274. * @node: Preferred node on which the irq descriptor should be allocated
  275. *
  276. * Returns the first irq number or error code
  277. */
  278. int __ref
  279. irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)
  280. {
  281. int start, ret;
  282. if (!cnt)
  283. return -EINVAL;
  284. if (irq >= 0) {
  285. if (from > irq)
  286. return -EINVAL;
  287. from = irq;
  288. }
  289. mutex_lock(&sparse_irq_lock);
  290. start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
  291. from, cnt, 0);
  292. ret = -EEXIST;
  293. if (irq >=0 && start != irq)
  294. goto err;
  295. if (start + cnt > nr_irqs) {
  296. ret = irq_expand_nr_irqs(start + cnt);
  297. if (ret)
  298. goto err;
  299. }
  300. bitmap_set(allocated_irqs, start, cnt);
  301. mutex_unlock(&sparse_irq_lock);
  302. return alloc_descs(start, cnt, node);
  303. err:
  304. mutex_unlock(&sparse_irq_lock);
  305. return ret;
  306. }
  307. EXPORT_SYMBOL_GPL(irq_alloc_descs);
  308. /**
  309. * irq_reserve_irqs - mark irqs allocated
  310. * @from: mark from irq number
  311. * @cnt: number of irqs to mark
  312. *
  313. * Returns 0 on success or an appropriate error code
  314. */
  315. int irq_reserve_irqs(unsigned int from, unsigned int cnt)
  316. {
  317. unsigned int start;
  318. int ret = 0;
  319. if (!cnt || (from + cnt) > nr_irqs)
  320. return -EINVAL;
  321. mutex_lock(&sparse_irq_lock);
  322. start = bitmap_find_next_zero_area(allocated_irqs, nr_irqs, from, cnt, 0);
  323. if (start == from)
  324. bitmap_set(allocated_irqs, start, cnt);
  325. else
  326. ret = -EEXIST;
  327. mutex_unlock(&sparse_irq_lock);
  328. return ret;
  329. }
  330. /**
  331. * irq_get_next_irq - get next allocated irq number
  332. * @offset: where to start the search
  333. *
  334. * Returns next irq number after offset or nr_irqs if none is found.
  335. */
  336. unsigned int irq_get_next_irq(unsigned int offset)
  337. {
  338. return find_next_bit(allocated_irqs, nr_irqs, offset);
  339. }
  340. struct irq_desc *
  341. __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus)
  342. {
  343. struct irq_desc *desc = irq_to_desc(irq);
  344. if (desc) {
  345. if (bus)
  346. chip_bus_lock(desc);
  347. raw_spin_lock_irqsave(&desc->lock, *flags);
  348. }
  349. return desc;
  350. }
  351. void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
  352. {
  353. raw_spin_unlock_irqrestore(&desc->lock, flags);
  354. if (bus)
  355. chip_bus_sync_unlock(desc);
  356. }
  357. /**
  358. * dynamic_irq_cleanup - cleanup a dynamically allocated irq
  359. * @irq: irq number to initialize
  360. */
  361. void dynamic_irq_cleanup(unsigned int irq)
  362. {
  363. struct irq_desc *desc = irq_to_desc(irq);
  364. unsigned long flags;
  365. raw_spin_lock_irqsave(&desc->lock, flags);
  366. desc_set_defaults(irq, desc, desc_node(desc));
  367. raw_spin_unlock_irqrestore(&desc->lock, flags);
  368. }
  369. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  370. {
  371. struct irq_desc *desc = irq_to_desc(irq);
  372. return desc && desc->kstat_irqs ?
  373. *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
  374. }
  375. unsigned int kstat_irqs(unsigned int irq)
  376. {
  377. struct irq_desc *desc = irq_to_desc(irq);
  378. int cpu;
  379. int sum = 0;
  380. if (!desc || !desc->kstat_irqs)
  381. return 0;
  382. for_each_possible_cpu(cpu)
  383. sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
  384. return sum;
  385. }