irqdesc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 <linux/sysfs.h>
  19. #include "internals.h"
  20. /*
  21. * lockdep: we want to handle all irq_desc locks as a single lock-class:
  22. */
  23. static struct lock_class_key irq_desc_lock_class;
  24. #if defined(CONFIG_SMP)
  25. static int __init irq_affinity_setup(char *str)
  26. {
  27. zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  28. cpulist_parse(str, irq_default_affinity);
  29. /*
  30. * Set at least the boot cpu. We don't want to end up with
  31. * bugreports caused by random comandline masks
  32. */
  33. cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
  34. return 1;
  35. }
  36. __setup("irqaffinity=", irq_affinity_setup);
  37. static void __init init_irq_default_affinity(void)
  38. {
  39. #ifdef CONFIG_CPUMASK_OFFSTACK
  40. if (!irq_default_affinity)
  41. zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  42. #endif
  43. if (cpumask_empty(irq_default_affinity))
  44. cpumask_setall(irq_default_affinity);
  45. }
  46. #else
  47. static void __init init_irq_default_affinity(void)
  48. {
  49. }
  50. #endif
  51. #ifdef CONFIG_SMP
  52. static int alloc_masks(struct irq_desc *desc, gfp_t gfp, int node)
  53. {
  54. if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity,
  55. gfp, node))
  56. return -ENOMEM;
  57. #ifdef CONFIG_GENERIC_PENDING_IRQ
  58. if (!zalloc_cpumask_var_node(&desc->pending_mask, gfp, node)) {
  59. free_cpumask_var(desc->irq_common_data.affinity);
  60. return -ENOMEM;
  61. }
  62. #endif
  63. return 0;
  64. }
  65. static void desc_smp_init(struct irq_desc *desc, int node,
  66. const struct cpumask *affinity)
  67. {
  68. if (!affinity)
  69. affinity = irq_default_affinity;
  70. cpumask_copy(desc->irq_common_data.affinity, affinity);
  71. #ifdef CONFIG_GENERIC_PENDING_IRQ
  72. cpumask_clear(desc->pending_mask);
  73. #endif
  74. #ifdef CONFIG_NUMA
  75. desc->irq_common_data.node = node;
  76. #endif
  77. }
  78. #else
  79. static inline int
  80. alloc_masks(struct irq_desc *desc, gfp_t gfp, int node) { return 0; }
  81. static inline void
  82. desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { }
  83. #endif
  84. static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
  85. const struct cpumask *affinity, struct module *owner)
  86. {
  87. int cpu;
  88. desc->irq_common_data.handler_data = NULL;
  89. desc->irq_common_data.msi_desc = NULL;
  90. desc->irq_data.common = &desc->irq_common_data;
  91. desc->irq_data.irq = irq;
  92. desc->irq_data.chip = &no_irq_chip;
  93. desc->irq_data.chip_data = NULL;
  94. irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
  95. irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
  96. desc->handle_irq = handle_bad_irq;
  97. desc->depth = 1;
  98. desc->irq_count = 0;
  99. desc->irqs_unhandled = 0;
  100. desc->name = NULL;
  101. desc->owner = owner;
  102. for_each_possible_cpu(cpu)
  103. *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
  104. desc_smp_init(desc, node, affinity);
  105. }
  106. int nr_irqs = NR_IRQS;
  107. EXPORT_SYMBOL_GPL(nr_irqs);
  108. static DEFINE_MUTEX(sparse_irq_lock);
  109. static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
  110. #ifdef CONFIG_SPARSE_IRQ
  111. static void irq_kobj_release(struct kobject *kobj);
  112. #ifdef CONFIG_SYSFS
  113. static struct kobject *irq_kobj_base;
  114. #define IRQ_ATTR_RO(_name) \
  115. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  116. static ssize_t per_cpu_count_show(struct kobject *kobj,
  117. struct kobj_attribute *attr, char *buf)
  118. {
  119. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  120. int cpu, irq = desc->irq_data.irq;
  121. ssize_t ret = 0;
  122. char *p = "";
  123. for_each_possible_cpu(cpu) {
  124. unsigned int c = kstat_irqs_cpu(irq, cpu);
  125. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
  126. p = ",";
  127. }
  128. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
  129. return ret;
  130. }
  131. IRQ_ATTR_RO(per_cpu_count);
  132. static ssize_t chip_name_show(struct kobject *kobj,
  133. struct kobj_attribute *attr, char *buf)
  134. {
  135. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  136. ssize_t ret = 0;
  137. raw_spin_lock_irq(&desc->lock);
  138. if (desc->irq_data.chip && desc->irq_data.chip->name) {
  139. ret = scnprintf(buf, PAGE_SIZE, "%s\n",
  140. desc->irq_data.chip->name);
  141. }
  142. raw_spin_unlock_irq(&desc->lock);
  143. return ret;
  144. }
  145. IRQ_ATTR_RO(chip_name);
  146. static ssize_t hwirq_show(struct kobject *kobj,
  147. struct kobj_attribute *attr, char *buf)
  148. {
  149. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  150. ssize_t ret = 0;
  151. raw_spin_lock_irq(&desc->lock);
  152. if (desc->irq_data.domain)
  153. ret = sprintf(buf, "%d\n", (int)desc->irq_data.hwirq);
  154. raw_spin_unlock_irq(&desc->lock);
  155. return ret;
  156. }
  157. IRQ_ATTR_RO(hwirq);
  158. static ssize_t type_show(struct kobject *kobj,
  159. struct kobj_attribute *attr, char *buf)
  160. {
  161. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  162. ssize_t ret = 0;
  163. raw_spin_lock_irq(&desc->lock);
  164. ret = sprintf(buf, "%s\n",
  165. irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
  166. raw_spin_unlock_irq(&desc->lock);
  167. return ret;
  168. }
  169. IRQ_ATTR_RO(type);
  170. static ssize_t name_show(struct kobject *kobj,
  171. struct kobj_attribute *attr, char *buf)
  172. {
  173. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  174. ssize_t ret = 0;
  175. raw_spin_lock_irq(&desc->lock);
  176. if (desc->name)
  177. ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
  178. raw_spin_unlock_irq(&desc->lock);
  179. return ret;
  180. }
  181. IRQ_ATTR_RO(name);
  182. static ssize_t actions_show(struct kobject *kobj,
  183. struct kobj_attribute *attr, char *buf)
  184. {
  185. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  186. struct irqaction *action;
  187. ssize_t ret = 0;
  188. char *p = "";
  189. raw_spin_lock_irq(&desc->lock);
  190. for (action = desc->action; action != NULL; action = action->next) {
  191. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
  192. p, action->name);
  193. p = ",";
  194. }
  195. raw_spin_unlock_irq(&desc->lock);
  196. if (ret)
  197. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
  198. return ret;
  199. }
  200. IRQ_ATTR_RO(actions);
  201. static struct attribute *irq_attrs[] = {
  202. &per_cpu_count_attr.attr,
  203. &chip_name_attr.attr,
  204. &hwirq_attr.attr,
  205. &type_attr.attr,
  206. &name_attr.attr,
  207. &actions_attr.attr,
  208. NULL
  209. };
  210. static struct kobj_type irq_kobj_type = {
  211. .release = irq_kobj_release,
  212. .sysfs_ops = &kobj_sysfs_ops,
  213. .default_attrs = irq_attrs,
  214. };
  215. static void irq_sysfs_add(int irq, struct irq_desc *desc)
  216. {
  217. if (irq_kobj_base) {
  218. /*
  219. * Continue even in case of failure as this is nothing
  220. * crucial.
  221. */
  222. if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
  223. pr_warn("Failed to add kobject for irq %d\n", irq);
  224. }
  225. }
  226. static int __init irq_sysfs_init(void)
  227. {
  228. struct irq_desc *desc;
  229. int irq;
  230. /* Prevent concurrent irq alloc/free */
  231. irq_lock_sparse();
  232. irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
  233. if (!irq_kobj_base) {
  234. irq_unlock_sparse();
  235. return -ENOMEM;
  236. }
  237. /* Add the already allocated interrupts */
  238. for_each_irq_desc(irq, desc)
  239. irq_sysfs_add(irq, desc);
  240. irq_unlock_sparse();
  241. return 0;
  242. }
  243. postcore_initcall(irq_sysfs_init);
  244. #else /* !CONFIG_SYSFS */
  245. static struct kobj_type irq_kobj_type = {
  246. .release = irq_kobj_release,
  247. };
  248. static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
  249. #endif /* CONFIG_SYSFS */
  250. static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
  251. static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
  252. {
  253. radix_tree_insert(&irq_desc_tree, irq, desc);
  254. }
  255. struct irq_desc *irq_to_desc(unsigned int irq)
  256. {
  257. return radix_tree_lookup(&irq_desc_tree, irq);
  258. }
  259. EXPORT_SYMBOL(irq_to_desc);
  260. static void delete_irq_desc(unsigned int irq)
  261. {
  262. radix_tree_delete(&irq_desc_tree, irq);
  263. }
  264. #ifdef CONFIG_SMP
  265. static void free_masks(struct irq_desc *desc)
  266. {
  267. #ifdef CONFIG_GENERIC_PENDING_IRQ
  268. free_cpumask_var(desc->pending_mask);
  269. #endif
  270. free_cpumask_var(desc->irq_common_data.affinity);
  271. }
  272. #else
  273. static inline void free_masks(struct irq_desc *desc) { }
  274. #endif
  275. void irq_lock_sparse(void)
  276. {
  277. mutex_lock(&sparse_irq_lock);
  278. }
  279. void irq_unlock_sparse(void)
  280. {
  281. mutex_unlock(&sparse_irq_lock);
  282. }
  283. static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
  284. const struct cpumask *affinity,
  285. struct module *owner)
  286. {
  287. struct irq_desc *desc;
  288. gfp_t gfp = GFP_KERNEL;
  289. desc = kzalloc_node(sizeof(*desc), gfp, node);
  290. if (!desc)
  291. return NULL;
  292. /* allocate based on nr_cpu_ids */
  293. desc->kstat_irqs = alloc_percpu(unsigned int);
  294. if (!desc->kstat_irqs)
  295. goto err_desc;
  296. if (alloc_masks(desc, gfp, node))
  297. goto err_kstat;
  298. raw_spin_lock_init(&desc->lock);
  299. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  300. init_rcu_head(&desc->rcu);
  301. desc_set_defaults(irq, desc, node, affinity, owner);
  302. irqd_set(&desc->irq_data, flags);
  303. kobject_init(&desc->kobj, &irq_kobj_type);
  304. return desc;
  305. err_kstat:
  306. free_percpu(desc->kstat_irqs);
  307. err_desc:
  308. kfree(desc);
  309. return NULL;
  310. }
  311. static void irq_kobj_release(struct kobject *kobj)
  312. {
  313. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  314. free_masks(desc);
  315. free_percpu(desc->kstat_irqs);
  316. kfree(desc);
  317. }
  318. static void delayed_free_desc(struct rcu_head *rhp)
  319. {
  320. struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
  321. kobject_put(&desc->kobj);
  322. }
  323. static void free_desc(unsigned int irq)
  324. {
  325. struct irq_desc *desc = irq_to_desc(irq);
  326. unregister_irq_proc(irq, desc);
  327. /*
  328. * sparse_irq_lock protects also show_interrupts() and
  329. * kstat_irq_usr(). Once we deleted the descriptor from the
  330. * sparse tree we can free it. Access in proc will fail to
  331. * lookup the descriptor.
  332. *
  333. * The sysfs entry must be serialized against a concurrent
  334. * irq_sysfs_init() as well.
  335. */
  336. kobject_del(&desc->kobj);
  337. delete_irq_desc(irq);
  338. /*
  339. * We free the descriptor, masks and stat fields via RCU. That
  340. * allows demultiplex interrupts to do rcu based management of
  341. * the child interrupts.
  342. */
  343. call_rcu(&desc->rcu, delayed_free_desc);
  344. }
  345. static int alloc_descs(unsigned int start, unsigned int cnt, int node,
  346. const struct cpumask *affinity, struct module *owner)
  347. {
  348. const struct cpumask *mask = NULL;
  349. struct irq_desc *desc;
  350. unsigned int flags;
  351. int i;
  352. /* Validate affinity mask(s) */
  353. if (affinity) {
  354. for (i = 0, mask = affinity; i < cnt; i++, mask++) {
  355. if (cpumask_empty(mask))
  356. return -EINVAL;
  357. }
  358. }
  359. flags = affinity ? IRQD_AFFINITY_MANAGED : 0;
  360. mask = NULL;
  361. for (i = 0; i < cnt; i++) {
  362. if (affinity) {
  363. node = cpu_to_node(cpumask_first(affinity));
  364. mask = affinity;
  365. affinity++;
  366. }
  367. desc = alloc_desc(start + i, node, flags, mask, owner);
  368. if (!desc)
  369. goto err;
  370. irq_insert_desc(start + i, desc);
  371. irq_sysfs_add(start + i, desc);
  372. }
  373. bitmap_set(allocated_irqs, start, cnt);
  374. return start;
  375. err:
  376. for (i--; i >= 0; i--)
  377. free_desc(start + i);
  378. return -ENOMEM;
  379. }
  380. static int irq_expand_nr_irqs(unsigned int nr)
  381. {
  382. if (nr > IRQ_BITMAP_BITS)
  383. return -ENOMEM;
  384. nr_irqs = nr;
  385. return 0;
  386. }
  387. int __init early_irq_init(void)
  388. {
  389. int i, initcnt, node = first_online_node;
  390. struct irq_desc *desc;
  391. init_irq_default_affinity();
  392. /* Let arch update nr_irqs and return the nr of preallocated irqs */
  393. initcnt = arch_probe_nr_irqs();
  394. printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt);
  395. if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
  396. nr_irqs = IRQ_BITMAP_BITS;
  397. if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
  398. initcnt = IRQ_BITMAP_BITS;
  399. if (initcnt > nr_irqs)
  400. nr_irqs = initcnt;
  401. for (i = 0; i < initcnt; i++) {
  402. desc = alloc_desc(i, node, 0, NULL, NULL);
  403. set_bit(i, allocated_irqs);
  404. irq_insert_desc(i, desc);
  405. }
  406. return arch_early_irq_init();
  407. }
  408. #else /* !CONFIG_SPARSE_IRQ */
  409. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  410. [0 ... NR_IRQS-1] = {
  411. .handle_irq = handle_bad_irq,
  412. .depth = 1,
  413. .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
  414. }
  415. };
  416. int __init early_irq_init(void)
  417. {
  418. int count, i, node = first_online_node;
  419. struct irq_desc *desc;
  420. init_irq_default_affinity();
  421. printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
  422. desc = irq_desc;
  423. count = ARRAY_SIZE(irq_desc);
  424. for (i = 0; i < count; i++) {
  425. desc[i].kstat_irqs = alloc_percpu(unsigned int);
  426. alloc_masks(&desc[i], GFP_KERNEL, node);
  427. raw_spin_lock_init(&desc[i].lock);
  428. lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
  429. desc_set_defaults(i, &desc[i], node, NULL, NULL);
  430. }
  431. return arch_early_irq_init();
  432. }
  433. struct irq_desc *irq_to_desc(unsigned int irq)
  434. {
  435. return (irq < NR_IRQS) ? irq_desc + irq : NULL;
  436. }
  437. EXPORT_SYMBOL(irq_to_desc);
  438. static void free_desc(unsigned int irq)
  439. {
  440. struct irq_desc *desc = irq_to_desc(irq);
  441. unsigned long flags;
  442. raw_spin_lock_irqsave(&desc->lock, flags);
  443. desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
  444. raw_spin_unlock_irqrestore(&desc->lock, flags);
  445. }
  446. static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
  447. const struct cpumask *affinity,
  448. struct module *owner)
  449. {
  450. u32 i;
  451. for (i = 0; i < cnt; i++) {
  452. struct irq_desc *desc = irq_to_desc(start + i);
  453. desc->owner = owner;
  454. }
  455. bitmap_set(allocated_irqs, start, cnt);
  456. return start;
  457. }
  458. static int irq_expand_nr_irqs(unsigned int nr)
  459. {
  460. return -ENOMEM;
  461. }
  462. void irq_mark_irq(unsigned int irq)
  463. {
  464. mutex_lock(&sparse_irq_lock);
  465. bitmap_set(allocated_irqs, irq, 1);
  466. mutex_unlock(&sparse_irq_lock);
  467. }
  468. #ifdef CONFIG_GENERIC_IRQ_LEGACY
  469. void irq_init_desc(unsigned int irq)
  470. {
  471. free_desc(irq);
  472. }
  473. #endif
  474. #endif /* !CONFIG_SPARSE_IRQ */
  475. /**
  476. * generic_handle_irq - Invoke the handler for a particular irq
  477. * @irq: The irq number to handle
  478. *
  479. */
  480. int generic_handle_irq(unsigned int irq)
  481. {
  482. struct irq_desc *desc = irq_to_desc(irq);
  483. if (!desc)
  484. return -EINVAL;
  485. generic_handle_irq_desc(desc);
  486. return 0;
  487. }
  488. EXPORT_SYMBOL_GPL(generic_handle_irq);
  489. #ifdef CONFIG_HANDLE_DOMAIN_IRQ
  490. /**
  491. * __handle_domain_irq - Invoke the handler for a HW irq belonging to a domain
  492. * @domain: The domain where to perform the lookup
  493. * @hwirq: The HW irq number to convert to a logical one
  494. * @lookup: Whether to perform the domain lookup or not
  495. * @regs: Register file coming from the low-level handling code
  496. *
  497. * Returns: 0 on success, or -EINVAL if conversion has failed
  498. */
  499. int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,
  500. bool lookup, struct pt_regs *regs)
  501. {
  502. struct pt_regs *old_regs = set_irq_regs(regs);
  503. unsigned int irq = hwirq;
  504. int ret = 0;
  505. irq_enter();
  506. #ifdef CONFIG_IRQ_DOMAIN
  507. if (lookup)
  508. irq = irq_find_mapping(domain, hwirq);
  509. #endif
  510. /*
  511. * Some hardware gives randomly wrong interrupts. Rather
  512. * than crashing, do something sensible.
  513. */
  514. if (unlikely(!irq || irq >= nr_irqs)) {
  515. ack_bad_irq(irq);
  516. ret = -EINVAL;
  517. } else {
  518. generic_handle_irq(irq);
  519. }
  520. irq_exit();
  521. set_irq_regs(old_regs);
  522. return ret;
  523. }
  524. #endif
  525. /* Dynamic interrupt handling */
  526. /**
  527. * irq_free_descs - free irq descriptors
  528. * @from: Start of descriptor range
  529. * @cnt: Number of consecutive irqs to free
  530. */
  531. void irq_free_descs(unsigned int from, unsigned int cnt)
  532. {
  533. int i;
  534. if (from >= nr_irqs || (from + cnt) > nr_irqs)
  535. return;
  536. mutex_lock(&sparse_irq_lock);
  537. for (i = 0; i < cnt; i++)
  538. free_desc(from + i);
  539. bitmap_clear(allocated_irqs, from, cnt);
  540. mutex_unlock(&sparse_irq_lock);
  541. }
  542. EXPORT_SYMBOL_GPL(irq_free_descs);
  543. /**
  544. * irq_alloc_descs - allocate and initialize a range of irq descriptors
  545. * @irq: Allocate for specific irq number if irq >= 0
  546. * @from: Start the search from this irq number
  547. * @cnt: Number of consecutive irqs to allocate.
  548. * @node: Preferred node on which the irq descriptor should be allocated
  549. * @owner: Owning module (can be NULL)
  550. * @affinity: Optional pointer to an affinity mask array of size @cnt which
  551. * hints where the irq descriptors should be allocated and which
  552. * default affinities to use
  553. *
  554. * Returns the first irq number or error code
  555. */
  556. int __ref
  557. __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
  558. struct module *owner, const struct cpumask *affinity)
  559. {
  560. int start, ret;
  561. if (!cnt)
  562. return -EINVAL;
  563. if (irq >= 0) {
  564. if (from > irq)
  565. return -EINVAL;
  566. from = irq;
  567. } else {
  568. /*
  569. * For interrupts which are freely allocated the
  570. * architecture can force a lower bound to the @from
  571. * argument. x86 uses this to exclude the GSI space.
  572. */
  573. from = arch_dynirq_lower_bound(from);
  574. }
  575. mutex_lock(&sparse_irq_lock);
  576. start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
  577. from, cnt, 0);
  578. ret = -EEXIST;
  579. if (irq >=0 && start != irq)
  580. goto unlock;
  581. if (start + cnt > nr_irqs) {
  582. ret = irq_expand_nr_irqs(start + cnt);
  583. if (ret)
  584. goto unlock;
  585. }
  586. ret = alloc_descs(start, cnt, node, affinity, owner);
  587. unlock:
  588. mutex_unlock(&sparse_irq_lock);
  589. return ret;
  590. }
  591. EXPORT_SYMBOL_GPL(__irq_alloc_descs);
  592. #ifdef CONFIG_GENERIC_IRQ_LEGACY_ALLOC_HWIRQ
  593. /**
  594. * irq_alloc_hwirqs - Allocate an irq descriptor and initialize the hardware
  595. * @cnt: number of interrupts to allocate
  596. * @node: node on which to allocate
  597. *
  598. * Returns an interrupt number > 0 or 0, if the allocation fails.
  599. */
  600. unsigned int irq_alloc_hwirqs(int cnt, int node)
  601. {
  602. int i, irq = __irq_alloc_descs(-1, 0, cnt, node, NULL, NULL);
  603. if (irq < 0)
  604. return 0;
  605. for (i = irq; cnt > 0; i++, cnt--) {
  606. if (arch_setup_hwirq(i, node))
  607. goto err;
  608. irq_clear_status_flags(i, _IRQ_NOREQUEST);
  609. }
  610. return irq;
  611. err:
  612. for (i--; i >= irq; i--) {
  613. irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
  614. arch_teardown_hwirq(i);
  615. }
  616. irq_free_descs(irq, cnt);
  617. return 0;
  618. }
  619. EXPORT_SYMBOL_GPL(irq_alloc_hwirqs);
  620. /**
  621. * irq_free_hwirqs - Free irq descriptor and cleanup the hardware
  622. * @from: Free from irq number
  623. * @cnt: number of interrupts to free
  624. *
  625. */
  626. void irq_free_hwirqs(unsigned int from, int cnt)
  627. {
  628. int i, j;
  629. for (i = from, j = cnt; j > 0; i++, j--) {
  630. irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
  631. arch_teardown_hwirq(i);
  632. }
  633. irq_free_descs(from, cnt);
  634. }
  635. EXPORT_SYMBOL_GPL(irq_free_hwirqs);
  636. #endif
  637. /**
  638. * irq_get_next_irq - get next allocated irq number
  639. * @offset: where to start the search
  640. *
  641. * Returns next irq number after offset or nr_irqs if none is found.
  642. */
  643. unsigned int irq_get_next_irq(unsigned int offset)
  644. {
  645. return find_next_bit(allocated_irqs, nr_irqs, offset);
  646. }
  647. struct irq_desc *
  648. __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
  649. unsigned int check)
  650. {
  651. struct irq_desc *desc = irq_to_desc(irq);
  652. if (desc) {
  653. if (check & _IRQ_DESC_CHECK) {
  654. if ((check & _IRQ_DESC_PERCPU) &&
  655. !irq_settings_is_per_cpu_devid(desc))
  656. return NULL;
  657. if (!(check & _IRQ_DESC_PERCPU) &&
  658. irq_settings_is_per_cpu_devid(desc))
  659. return NULL;
  660. }
  661. if (bus)
  662. chip_bus_lock(desc);
  663. raw_spin_lock_irqsave(&desc->lock, *flags);
  664. }
  665. return desc;
  666. }
  667. void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
  668. {
  669. raw_spin_unlock_irqrestore(&desc->lock, flags);
  670. if (bus)
  671. chip_bus_sync_unlock(desc);
  672. }
  673. int irq_set_percpu_devid_partition(unsigned int irq,
  674. const struct cpumask *affinity)
  675. {
  676. struct irq_desc *desc = irq_to_desc(irq);
  677. if (!desc)
  678. return -EINVAL;
  679. if (desc->percpu_enabled)
  680. return -EINVAL;
  681. desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
  682. if (!desc->percpu_enabled)
  683. return -ENOMEM;
  684. if (affinity)
  685. desc->percpu_affinity = affinity;
  686. else
  687. desc->percpu_affinity = cpu_possible_mask;
  688. irq_set_percpu_devid_flags(irq);
  689. return 0;
  690. }
  691. int irq_set_percpu_devid(unsigned int irq)
  692. {
  693. return irq_set_percpu_devid_partition(irq, NULL);
  694. }
  695. int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
  696. {
  697. struct irq_desc *desc = irq_to_desc(irq);
  698. if (!desc || !desc->percpu_enabled)
  699. return -EINVAL;
  700. if (affinity)
  701. cpumask_copy(affinity, desc->percpu_affinity);
  702. return 0;
  703. }
  704. void kstat_incr_irq_this_cpu(unsigned int irq)
  705. {
  706. kstat_incr_irqs_this_cpu(irq_to_desc(irq));
  707. }
  708. /**
  709. * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
  710. * @irq: The interrupt number
  711. * @cpu: The cpu number
  712. *
  713. * Returns the sum of interrupt counts on @cpu since boot for
  714. * @irq. The caller must ensure that the interrupt is not removed
  715. * concurrently.
  716. */
  717. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  718. {
  719. struct irq_desc *desc = irq_to_desc(irq);
  720. return desc && desc->kstat_irqs ?
  721. *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
  722. }
  723. /**
  724. * kstat_irqs - Get the statistics for an interrupt
  725. * @irq: The interrupt number
  726. *
  727. * Returns the sum of interrupt counts on all cpus since boot for
  728. * @irq. The caller must ensure that the interrupt is not removed
  729. * concurrently.
  730. */
  731. unsigned int kstat_irqs(unsigned int irq)
  732. {
  733. struct irq_desc *desc = irq_to_desc(irq);
  734. int cpu;
  735. unsigned int sum = 0;
  736. if (!desc || !desc->kstat_irqs)
  737. return 0;
  738. for_each_possible_cpu(cpu)
  739. sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
  740. return sum;
  741. }
  742. /**
  743. * kstat_irqs_usr - Get the statistics for an interrupt
  744. * @irq: The interrupt number
  745. *
  746. * Returns the sum of interrupt counts on all cpus since boot for
  747. * @irq. Contrary to kstat_irqs() this can be called from any
  748. * preemptible context. It's protected against concurrent removal of
  749. * an interrupt descriptor when sparse irqs are enabled.
  750. */
  751. unsigned int kstat_irqs_usr(unsigned int irq)
  752. {
  753. unsigned int sum;
  754. irq_lock_sparse();
  755. sum = kstat_irqs(irq);
  756. irq_unlock_sparse();
  757. return sum;
  758. }