irqdesc.c 22 KB

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