irqdesc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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/export.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. #ifdef CONFIG_SEC_DEBUG
  19. #include <mach/sec_debug.h>
  20. #endif
  21. /*
  22. * lockdep: we want to handle all irq_desc locks as a single lock-class:
  23. */
  24. static struct lock_class_key irq_desc_lock_class;
  25. #if defined(CONFIG_SMP)
  26. static void __init init_irq_default_affinity(void)
  27. {
  28. alloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  29. cpumask_setall(irq_default_affinity);
  30. }
  31. #else
  32. static void __init init_irq_default_affinity(void)
  33. {
  34. }
  35. #endif
  36. #ifdef CONFIG_SMP
  37. static int alloc_masks(struct irq_desc *desc, gfp_t gfp, int node)
  38. {
  39. if (!zalloc_cpumask_var_node(&desc->irq_data.affinity, gfp, node))
  40. return -ENOMEM;
  41. #ifdef CONFIG_GENERIC_PENDING_IRQ
  42. if (!zalloc_cpumask_var_node(&desc->pending_mask, gfp, node)) {
  43. free_cpumask_var(desc->irq_data.affinity);
  44. return -ENOMEM;
  45. }
  46. #endif
  47. return 0;
  48. }
  49. static void desc_smp_init(struct irq_desc *desc, int node)
  50. {
  51. desc->irq_data.node = node;
  52. cpumask_copy(desc->irq_data.affinity, irq_default_affinity);
  53. #ifdef CONFIG_GENERIC_PENDING_IRQ
  54. cpumask_clear(desc->pending_mask);
  55. #endif
  56. }
  57. static inline int desc_node(struct irq_desc *desc)
  58. {
  59. return desc->irq_data.node;
  60. }
  61. #else
  62. static inline int
  63. alloc_masks(struct irq_desc *desc, gfp_t gfp, int node) { return 0; }
  64. static inline void desc_smp_init(struct irq_desc *desc, int node) { }
  65. static inline int desc_node(struct irq_desc *desc) { return 0; }
  66. #endif
  67. static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
  68. struct module *owner)
  69. {
  70. int cpu;
  71. desc->irq_data.irq = irq;
  72. desc->irq_data.chip = &no_irq_chip;
  73. desc->irq_data.chip_data = NULL;
  74. desc->irq_data.handler_data = NULL;
  75. desc->irq_data.msi_desc = NULL;
  76. irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
  77. irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
  78. desc->handle_irq = handle_bad_irq;
  79. desc->depth = 1;
  80. desc->irq_count = 0;
  81. desc->irqs_unhandled = 0;
  82. desc->name = NULL;
  83. desc->owner = owner;
  84. for_each_possible_cpu(cpu)
  85. *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
  86. desc_smp_init(desc, node);
  87. }
  88. int nr_irqs = NR_IRQS;
  89. EXPORT_SYMBOL_GPL(nr_irqs);
  90. static DEFINE_MUTEX(sparse_irq_lock);
  91. static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
  92. #ifdef CONFIG_SPARSE_IRQ
  93. static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
  94. static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
  95. {
  96. radix_tree_insert(&irq_desc_tree, irq, desc);
  97. }
  98. struct irq_desc *irq_to_desc(unsigned int irq)
  99. {
  100. return radix_tree_lookup(&irq_desc_tree, irq);
  101. }
  102. EXPORT_SYMBOL(irq_to_desc);
  103. static void delete_irq_desc(unsigned int irq)
  104. {
  105. radix_tree_delete(&irq_desc_tree, irq);
  106. }
  107. #ifdef CONFIG_SMP
  108. static void free_masks(struct irq_desc *desc)
  109. {
  110. #ifdef CONFIG_GENERIC_PENDING_IRQ
  111. free_cpumask_var(desc->pending_mask);
  112. #endif
  113. free_cpumask_var(desc->irq_data.affinity);
  114. }
  115. #else
  116. static inline void free_masks(struct irq_desc *desc) { }
  117. #endif
  118. void irq_lock_sparse(void)
  119. {
  120. mutex_lock(&sparse_irq_lock);
  121. }
  122. void irq_unlock_sparse(void)
  123. {
  124. mutex_unlock(&sparse_irq_lock);
  125. }
  126. static struct irq_desc *alloc_desc(int irq, int node, struct module *owner)
  127. {
  128. struct irq_desc *desc;
  129. gfp_t gfp = GFP_KERNEL;
  130. desc = kzalloc_node(sizeof(*desc), gfp, node);
  131. if (!desc)
  132. return NULL;
  133. /* allocate based on nr_cpu_ids */
  134. desc->kstat_irqs = alloc_percpu(unsigned int);
  135. if (!desc->kstat_irqs)
  136. goto err_desc;
  137. if (alloc_masks(desc, gfp, node))
  138. goto err_kstat;
  139. raw_spin_lock_init(&desc->lock);
  140. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  141. desc_set_defaults(irq, desc, node, owner);
  142. return desc;
  143. err_kstat:
  144. free_percpu(desc->kstat_irqs);
  145. err_desc:
  146. kfree(desc);
  147. return NULL;
  148. }
  149. static void free_desc(unsigned int irq)
  150. {
  151. struct irq_desc *desc = irq_to_desc(irq);
  152. unregister_irq_proc(irq, desc);
  153. /*
  154. * sparse_irq_lock protects also show_interrupts() and
  155. * kstat_irq_usr(). Once we deleted the descriptor from the
  156. * sparse tree we can free it. Access in proc will fail to
  157. * lookup the descriptor.
  158. */
  159. mutex_lock(&sparse_irq_lock);
  160. delete_irq_desc(irq);
  161. mutex_unlock(&sparse_irq_lock);
  162. free_masks(desc);
  163. free_percpu(desc->kstat_irqs);
  164. kfree(desc);
  165. }
  166. static int alloc_descs(unsigned int start, unsigned int cnt, int node,
  167. struct module *owner)
  168. {
  169. struct irq_desc *desc;
  170. int i;
  171. for (i = 0; i < cnt; i++) {
  172. desc = alloc_desc(start + i, node, owner);
  173. if (!desc)
  174. goto err;
  175. mutex_lock(&sparse_irq_lock);
  176. irq_insert_desc(start + i, desc);
  177. mutex_unlock(&sparse_irq_lock);
  178. }
  179. return start;
  180. err:
  181. for (i--; i >= 0; i--)
  182. free_desc(start + i);
  183. mutex_lock(&sparse_irq_lock);
  184. bitmap_clear(allocated_irqs, start, cnt);
  185. mutex_unlock(&sparse_irq_lock);
  186. return -ENOMEM;
  187. }
  188. static int irq_expand_nr_irqs(unsigned int nr)
  189. {
  190. if (nr > IRQ_BITMAP_BITS)
  191. return -ENOMEM;
  192. nr_irqs = nr;
  193. return 0;
  194. }
  195. int __init early_irq_init(void)
  196. {
  197. int i, initcnt, node = first_online_node;
  198. struct irq_desc *desc;
  199. init_irq_default_affinity();
  200. /* Let arch update nr_irqs and return the nr of preallocated irqs */
  201. initcnt = arch_probe_nr_irqs();
  202. printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt);
  203. if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
  204. nr_irqs = IRQ_BITMAP_BITS;
  205. if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
  206. initcnt = IRQ_BITMAP_BITS;
  207. if (initcnt > nr_irqs)
  208. nr_irqs = initcnt;
  209. for (i = 0; i < initcnt; i++) {
  210. desc = alloc_desc(i, node, NULL);
  211. set_bit(i, allocated_irqs);
  212. irq_insert_desc(i, desc);
  213. }
  214. return arch_early_irq_init();
  215. }
  216. #else /* !CONFIG_SPARSE_IRQ */
  217. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  218. [0 ... NR_IRQS-1] = {
  219. .handle_irq = handle_bad_irq,
  220. .depth = 1,
  221. .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
  222. }
  223. };
  224. int __init early_irq_init(void)
  225. {
  226. int count, i, node = first_online_node;
  227. struct irq_desc *desc;
  228. init_irq_default_affinity();
  229. printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
  230. desc = irq_desc;
  231. count = ARRAY_SIZE(irq_desc);
  232. for (i = 0; i < count; i++) {
  233. desc[i].kstat_irqs = alloc_percpu(unsigned int);
  234. alloc_masks(&desc[i], GFP_KERNEL, node);
  235. raw_spin_lock_init(&desc[i].lock);
  236. lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
  237. desc_set_defaults(i, &desc[i], node, NULL);
  238. }
  239. return arch_early_irq_init();
  240. }
  241. struct irq_desc *irq_to_desc(unsigned int irq)
  242. {
  243. return (irq < NR_IRQS) ? irq_desc + irq : NULL;
  244. }
  245. EXPORT_SYMBOL(irq_to_desc);
  246. static void free_desc(unsigned int irq)
  247. {
  248. dynamic_irq_cleanup(irq);
  249. }
  250. static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
  251. struct module *owner)
  252. {
  253. u32 i;
  254. for (i = 0; i < cnt; i++) {
  255. struct irq_desc *desc = irq_to_desc(start + i);
  256. desc->owner = owner;
  257. }
  258. return start;
  259. }
  260. static int irq_expand_nr_irqs(unsigned int nr)
  261. {
  262. return -ENOMEM;
  263. }
  264. #endif /* !CONFIG_SPARSE_IRQ */
  265. /**
  266. * generic_handle_irq - Invoke the handler for a particular irq
  267. * @irq: The irq number to handle
  268. *
  269. */
  270. int generic_handle_irq(unsigned int irq)
  271. {
  272. struct irq_desc *desc = irq_to_desc(irq);
  273. if (!desc)
  274. return -EINVAL;
  275. #ifdef CONFIG_SEC_DEBUG
  276. if (desc->action)
  277. sec_debug_irq_sched_log(irq, (void *)desc->action->handler,
  278. irqs_disabled());
  279. else
  280. sec_debug_irq_sched_log(irq, (void *)desc->handle_irq,
  281. irqs_disabled());
  282. #endif
  283. generic_handle_irq_desc(irq, desc);
  284. return 0;
  285. }
  286. EXPORT_SYMBOL_GPL(generic_handle_irq);
  287. /* Dynamic interrupt handling */
  288. /**
  289. * irq_free_descs - free irq descriptors
  290. * @from: Start of descriptor range
  291. * @cnt: Number of consecutive irqs to free
  292. */
  293. void irq_free_descs(unsigned int from, unsigned int cnt)
  294. {
  295. int i;
  296. if (from >= nr_irqs || (from + cnt) > nr_irqs)
  297. return;
  298. for (i = 0; i < cnt; i++)
  299. free_desc(from + i);
  300. mutex_lock(&sparse_irq_lock);
  301. bitmap_clear(allocated_irqs, from, cnt);
  302. mutex_unlock(&sparse_irq_lock);
  303. }
  304. EXPORT_SYMBOL_GPL(irq_free_descs);
  305. /**
  306. * irq_alloc_descs - allocate and initialize a range of irq descriptors
  307. * @irq: Allocate for specific irq number if irq >= 0
  308. * @from: Start the search from this irq number
  309. * @cnt: Number of consecutive irqs to allocate.
  310. * @node: Preferred node on which the irq descriptor should be allocated
  311. * @owner: Owning module (can be NULL)
  312. *
  313. * Returns the first irq number or error code
  314. */
  315. int __ref
  316. __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
  317. struct module *owner)
  318. {
  319. int start, ret;
  320. if (!cnt)
  321. return -EINVAL;
  322. if (irq >= 0) {
  323. if (from > irq)
  324. return -EINVAL;
  325. from = irq;
  326. }
  327. mutex_lock(&sparse_irq_lock);
  328. start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
  329. from, cnt, 0);
  330. ret = -EEXIST;
  331. if (irq >=0 && start != irq)
  332. goto err;
  333. if (start + cnt > nr_irqs) {
  334. ret = irq_expand_nr_irqs(start + cnt);
  335. if (ret)
  336. goto err;
  337. }
  338. bitmap_set(allocated_irqs, start, cnt);
  339. mutex_unlock(&sparse_irq_lock);
  340. return alloc_descs(start, cnt, node, owner);
  341. err:
  342. mutex_unlock(&sparse_irq_lock);
  343. return ret;
  344. }
  345. EXPORT_SYMBOL_GPL(__irq_alloc_descs);
  346. /**
  347. * irq_reserve_irqs - mark irqs allocated
  348. * @from: mark from irq number
  349. * @cnt: number of irqs to mark
  350. *
  351. * Returns 0 on success or an appropriate error code
  352. */
  353. int irq_reserve_irqs(unsigned int from, unsigned int cnt)
  354. {
  355. unsigned int start;
  356. int ret = 0;
  357. if (!cnt || (from + cnt) > nr_irqs)
  358. return -EINVAL;
  359. mutex_lock(&sparse_irq_lock);
  360. start = bitmap_find_next_zero_area(allocated_irqs, nr_irqs, from, cnt, 0);
  361. if (start == from)
  362. bitmap_set(allocated_irqs, start, cnt);
  363. else
  364. ret = -EEXIST;
  365. mutex_unlock(&sparse_irq_lock);
  366. return ret;
  367. }
  368. /**
  369. * irq_get_next_irq - get next allocated irq number
  370. * @offset: where to start the search
  371. *
  372. * Returns next irq number after offset or nr_irqs if none is found.
  373. */
  374. unsigned int irq_get_next_irq(unsigned int offset)
  375. {
  376. return find_next_bit(allocated_irqs, nr_irqs, offset);
  377. }
  378. struct irq_desc *
  379. __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
  380. unsigned int check)
  381. {
  382. struct irq_desc *desc = irq_to_desc(irq);
  383. if (desc) {
  384. if (check & _IRQ_DESC_CHECK) {
  385. if ((check & _IRQ_DESC_PERCPU) &&
  386. !irq_settings_is_per_cpu_devid(desc))
  387. return NULL;
  388. if (!(check & _IRQ_DESC_PERCPU) &&
  389. irq_settings_is_per_cpu_devid(desc))
  390. return NULL;
  391. }
  392. if (bus)
  393. chip_bus_lock(desc);
  394. raw_spin_lock_irqsave(&desc->lock, *flags);
  395. }
  396. return desc;
  397. }
  398. void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
  399. {
  400. raw_spin_unlock_irqrestore(&desc->lock, flags);
  401. if (bus)
  402. chip_bus_sync_unlock(desc);
  403. }
  404. int irq_set_percpu_devid(unsigned int irq)
  405. {
  406. struct irq_desc *desc = irq_to_desc(irq);
  407. if (!desc)
  408. return -EINVAL;
  409. if (desc->percpu_enabled)
  410. return -EINVAL;
  411. desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
  412. if (!desc->percpu_enabled)
  413. return -ENOMEM;
  414. irq_set_percpu_devid_flags(irq);
  415. return 0;
  416. }
  417. /**
  418. * dynamic_irq_cleanup - cleanup a dynamically allocated irq
  419. * @irq: irq number to initialize
  420. */
  421. void dynamic_irq_cleanup(unsigned int irq)
  422. {
  423. struct irq_desc *desc = irq_to_desc(irq);
  424. unsigned long flags;
  425. raw_spin_lock_irqsave(&desc->lock, flags);
  426. desc_set_defaults(irq, desc, desc_node(desc), NULL);
  427. raw_spin_unlock_irqrestore(&desc->lock, flags);
  428. }
  429. /**
  430. * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
  431. * @irq: The interrupt number
  432. * @cpu: The cpu number
  433. *
  434. * Returns the sum of interrupt counts on @cpu since boot for
  435. * @irq. The caller must ensure that the interrupt is not removed
  436. * concurrently.
  437. */
  438. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  439. {
  440. struct irq_desc *desc = irq_to_desc(irq);
  441. return desc && desc->kstat_irqs ?
  442. *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
  443. }
  444. /**
  445. * kstat_irqs - Get the statistics for an interrupt
  446. * @irq: The interrupt number
  447. *
  448. * Returns the sum of interrupt counts on all cpus since boot for
  449. * @irq. The caller must ensure that the interrupt is not removed
  450. * concurrently.
  451. */
  452. unsigned int kstat_irqs(unsigned int irq)
  453. {
  454. struct irq_desc *desc = irq_to_desc(irq);
  455. int cpu;
  456. int sum = 0;
  457. if (!desc || !desc->kstat_irqs)
  458. return 0;
  459. for_each_possible_cpu(cpu)
  460. sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
  461. return sum;
  462. }
  463. /**
  464. * kstat_irqs_usr - Get the statistics for an interrupt
  465. * @irq: The interrupt number
  466. *
  467. * Returns the sum of interrupt counts on all cpus since boot for
  468. * @irq. Contrary to kstat_irqs() this can be called from any
  469. * preemptible context. It's protected against concurrent removal of
  470. * an interrupt descriptor when sparse irqs are enabled.
  471. */
  472. unsigned int kstat_irqs_usr(unsigned int irq)
  473. {
  474. int sum;
  475. irq_lock_sparse();
  476. sum = kstat_irqs(irq);
  477. irq_unlock_sparse();
  478. return sum;
  479. }