irqdesc.c 14 KB

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