irqdesc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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. * This also allows us to use rcu in kstat_irqs_usr().
  369. */
  370. call_rcu(&desc->rcu, delayed_free_desc);
  371. }
  372. static int alloc_descs(unsigned int start, unsigned int cnt, int node,
  373. const struct cpumask *affinity, struct module *owner)
  374. {
  375. const struct cpumask *mask = NULL;
  376. struct irq_desc *desc;
  377. unsigned int flags;
  378. int i;
  379. /* Validate affinity mask(s) */
  380. if (affinity) {
  381. for (i = 0, mask = affinity; i < cnt; i++, mask++) {
  382. if (cpumask_empty(mask))
  383. return -EINVAL;
  384. }
  385. }
  386. flags = affinity ? IRQD_AFFINITY_MANAGED | IRQD_MANAGED_SHUTDOWN : 0;
  387. mask = NULL;
  388. for (i = 0; i < cnt; i++) {
  389. if (affinity) {
  390. node = cpu_to_node(cpumask_first(affinity));
  391. mask = affinity;
  392. affinity++;
  393. }
  394. desc = alloc_desc(start + i, node, flags, mask, owner);
  395. if (!desc)
  396. goto err;
  397. irq_insert_desc(start + i, desc);
  398. irq_sysfs_add(start + i, desc);
  399. irq_add_debugfs_entry(start + i, desc);
  400. }
  401. bitmap_set(allocated_irqs, start, cnt);
  402. return start;
  403. err:
  404. for (i--; i >= 0; i--)
  405. free_desc(start + i);
  406. return -ENOMEM;
  407. }
  408. static int irq_expand_nr_irqs(unsigned int nr)
  409. {
  410. if (nr > IRQ_BITMAP_BITS)
  411. return -ENOMEM;
  412. nr_irqs = nr;
  413. return 0;
  414. }
  415. int __init early_irq_init(void)
  416. {
  417. int i, initcnt, node = first_online_node;
  418. struct irq_desc *desc;
  419. init_irq_default_affinity();
  420. /* Let arch update nr_irqs and return the nr of preallocated irqs */
  421. initcnt = arch_probe_nr_irqs();
  422. printk(KERN_INFO "NR_IRQS: %d, nr_irqs: %d, preallocated irqs: %d\n",
  423. NR_IRQS, nr_irqs, initcnt);
  424. if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
  425. nr_irqs = IRQ_BITMAP_BITS;
  426. if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
  427. initcnt = IRQ_BITMAP_BITS;
  428. if (initcnt > nr_irqs)
  429. nr_irqs = initcnt;
  430. for (i = 0; i < initcnt; i++) {
  431. desc = alloc_desc(i, node, 0, NULL, NULL);
  432. set_bit(i, allocated_irqs);
  433. irq_insert_desc(i, desc);
  434. }
  435. return arch_early_irq_init();
  436. }
  437. #else /* !CONFIG_SPARSE_IRQ */
  438. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  439. [0 ... NR_IRQS-1] = {
  440. .handle_irq = handle_bad_irq,
  441. .depth = 1,
  442. .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
  443. }
  444. };
  445. int __init early_irq_init(void)
  446. {
  447. int count, i, node = first_online_node;
  448. struct irq_desc *desc;
  449. init_irq_default_affinity();
  450. printk(KERN_INFO "NR_IRQS: %d\n", NR_IRQS);
  451. desc = irq_desc;
  452. count = ARRAY_SIZE(irq_desc);
  453. for (i = 0; i < count; i++) {
  454. desc[i].kstat_irqs = alloc_percpu(unsigned int);
  455. alloc_masks(&desc[i], node);
  456. raw_spin_lock_init(&desc[i].lock);
  457. lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
  458. desc_set_defaults(i, &desc[i], node, NULL, NULL);
  459. }
  460. return arch_early_irq_init();
  461. }
  462. struct irq_desc *irq_to_desc(unsigned int irq)
  463. {
  464. return (irq < NR_IRQS) ? irq_desc + irq : NULL;
  465. }
  466. EXPORT_SYMBOL(irq_to_desc);
  467. static void free_desc(unsigned int irq)
  468. {
  469. struct irq_desc *desc = irq_to_desc(irq);
  470. unsigned long flags;
  471. raw_spin_lock_irqsave(&desc->lock, flags);
  472. desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
  473. raw_spin_unlock_irqrestore(&desc->lock, flags);
  474. }
  475. static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
  476. const struct cpumask *affinity,
  477. struct module *owner)
  478. {
  479. u32 i;
  480. for (i = 0; i < cnt; i++) {
  481. struct irq_desc *desc = irq_to_desc(start + i);
  482. desc->owner = owner;
  483. }
  484. bitmap_set(allocated_irqs, start, cnt);
  485. return start;
  486. }
  487. static int irq_expand_nr_irqs(unsigned int nr)
  488. {
  489. return -ENOMEM;
  490. }
  491. void irq_mark_irq(unsigned int irq)
  492. {
  493. mutex_lock(&sparse_irq_lock);
  494. bitmap_set(allocated_irqs, irq, 1);
  495. mutex_unlock(&sparse_irq_lock);
  496. }
  497. #ifdef CONFIG_GENERIC_IRQ_LEGACY
  498. void irq_init_desc(unsigned int irq)
  499. {
  500. free_desc(irq);
  501. }
  502. #endif
  503. #endif /* !CONFIG_SPARSE_IRQ */
  504. /**
  505. * generic_handle_irq - Invoke the handler for a particular irq
  506. * @irq: The irq number to handle
  507. *
  508. */
  509. int generic_handle_irq(unsigned int irq)
  510. {
  511. struct irq_desc *desc = irq_to_desc(irq);
  512. if (!desc)
  513. return -EINVAL;
  514. generic_handle_irq_desc(desc);
  515. return 0;
  516. }
  517. EXPORT_SYMBOL_GPL(generic_handle_irq);
  518. #ifdef CONFIG_HANDLE_DOMAIN_IRQ
  519. /**
  520. * __handle_domain_irq - Invoke the handler for a HW irq belonging to a domain
  521. * @domain: The domain where to perform the lookup
  522. * @hwirq: The HW irq number to convert to a logical one
  523. * @lookup: Whether to perform the domain lookup or not
  524. * @regs: Register file coming from the low-level handling code
  525. *
  526. * Returns: 0 on success, or -EINVAL if conversion has failed
  527. */
  528. int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,
  529. bool lookup, struct pt_regs *regs)
  530. {
  531. struct pt_regs *old_regs = set_irq_regs(regs);
  532. unsigned int irq = hwirq;
  533. int ret = 0;
  534. irq_enter();
  535. #ifdef CONFIG_IRQ_DOMAIN
  536. if (lookup)
  537. irq = irq_find_mapping(domain, hwirq);
  538. #endif
  539. /*
  540. * Some hardware gives randomly wrong interrupts. Rather
  541. * than crashing, do something sensible.
  542. */
  543. if (unlikely(!irq || irq >= nr_irqs)) {
  544. ack_bad_irq(irq);
  545. ret = -EINVAL;
  546. } else {
  547. generic_handle_irq(irq);
  548. }
  549. irq_exit();
  550. set_irq_regs(old_regs);
  551. return ret;
  552. }
  553. #endif
  554. /* Dynamic interrupt handling */
  555. /**
  556. * irq_free_descs - free irq descriptors
  557. * @from: Start of descriptor range
  558. * @cnt: Number of consecutive irqs to free
  559. */
  560. void irq_free_descs(unsigned int from, unsigned int cnt)
  561. {
  562. int i;
  563. if (from >= nr_irqs || (from + cnt) > nr_irqs)
  564. return;
  565. mutex_lock(&sparse_irq_lock);
  566. for (i = 0; i < cnt; i++)
  567. free_desc(from + i);
  568. bitmap_clear(allocated_irqs, from, cnt);
  569. mutex_unlock(&sparse_irq_lock);
  570. }
  571. EXPORT_SYMBOL_GPL(irq_free_descs);
  572. /**
  573. * irq_alloc_descs - allocate and initialize a range of irq descriptors
  574. * @irq: Allocate for specific irq number if irq >= 0
  575. * @from: Start the search from this irq number
  576. * @cnt: Number of consecutive irqs to allocate.
  577. * @node: Preferred node on which the irq descriptor should be allocated
  578. * @owner: Owning module (can be NULL)
  579. * @affinity: Optional pointer to an affinity mask array of size @cnt which
  580. * hints where the irq descriptors should be allocated and which
  581. * default affinities to use
  582. *
  583. * Returns the first irq number or error code
  584. */
  585. int __ref
  586. __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
  587. struct module *owner, const struct cpumask *affinity)
  588. {
  589. int start, ret;
  590. if (!cnt)
  591. return -EINVAL;
  592. if (irq >= 0) {
  593. if (from > irq)
  594. return -EINVAL;
  595. from = irq;
  596. } else {
  597. /*
  598. * For interrupts which are freely allocated the
  599. * architecture can force a lower bound to the @from
  600. * argument. x86 uses this to exclude the GSI space.
  601. */
  602. from = arch_dynirq_lower_bound(from);
  603. }
  604. mutex_lock(&sparse_irq_lock);
  605. start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
  606. from, cnt, 0);
  607. ret = -EEXIST;
  608. if (irq >=0 && start != irq)
  609. goto unlock;
  610. if (start + cnt > nr_irqs) {
  611. ret = irq_expand_nr_irqs(start + cnt);
  612. if (ret)
  613. goto unlock;
  614. }
  615. ret = alloc_descs(start, cnt, node, affinity, owner);
  616. unlock:
  617. mutex_unlock(&sparse_irq_lock);
  618. return ret;
  619. }
  620. EXPORT_SYMBOL_GPL(__irq_alloc_descs);
  621. #ifdef CONFIG_GENERIC_IRQ_LEGACY_ALLOC_HWIRQ
  622. /**
  623. * irq_alloc_hwirqs - Allocate an irq descriptor and initialize the hardware
  624. * @cnt: number of interrupts to allocate
  625. * @node: node on which to allocate
  626. *
  627. * Returns an interrupt number > 0 or 0, if the allocation fails.
  628. */
  629. unsigned int irq_alloc_hwirqs(int cnt, int node)
  630. {
  631. int i, irq = __irq_alloc_descs(-1, 0, cnt, node, NULL, NULL);
  632. if (irq < 0)
  633. return 0;
  634. for (i = irq; cnt > 0; i++, cnt--) {
  635. if (arch_setup_hwirq(i, node))
  636. goto err;
  637. irq_clear_status_flags(i, _IRQ_NOREQUEST);
  638. }
  639. return irq;
  640. err:
  641. for (i--; i >= irq; i--) {
  642. irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
  643. arch_teardown_hwirq(i);
  644. }
  645. irq_free_descs(irq, cnt);
  646. return 0;
  647. }
  648. EXPORT_SYMBOL_GPL(irq_alloc_hwirqs);
  649. /**
  650. * irq_free_hwirqs - Free irq descriptor and cleanup the hardware
  651. * @from: Free from irq number
  652. * @cnt: number of interrupts to free
  653. *
  654. */
  655. void irq_free_hwirqs(unsigned int from, int cnt)
  656. {
  657. int i, j;
  658. for (i = from, j = cnt; j > 0; i++, j--) {
  659. irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
  660. arch_teardown_hwirq(i);
  661. }
  662. irq_free_descs(from, cnt);
  663. }
  664. EXPORT_SYMBOL_GPL(irq_free_hwirqs);
  665. #endif
  666. /**
  667. * irq_get_next_irq - get next allocated irq number
  668. * @offset: where to start the search
  669. *
  670. * Returns next irq number after offset or nr_irqs if none is found.
  671. */
  672. unsigned int irq_get_next_irq(unsigned int offset)
  673. {
  674. return find_next_bit(allocated_irqs, nr_irqs, offset);
  675. }
  676. struct irq_desc *
  677. __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
  678. unsigned int check)
  679. {
  680. struct irq_desc *desc = irq_to_desc(irq);
  681. if (desc) {
  682. if (check & _IRQ_DESC_CHECK) {
  683. if ((check & _IRQ_DESC_PERCPU) &&
  684. !irq_settings_is_per_cpu_devid(desc))
  685. return NULL;
  686. if (!(check & _IRQ_DESC_PERCPU) &&
  687. irq_settings_is_per_cpu_devid(desc))
  688. return NULL;
  689. }
  690. if (bus)
  691. chip_bus_lock(desc);
  692. raw_spin_lock_irqsave(&desc->lock, *flags);
  693. }
  694. return desc;
  695. }
  696. void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
  697. {
  698. raw_spin_unlock_irqrestore(&desc->lock, flags);
  699. if (bus)
  700. chip_bus_sync_unlock(desc);
  701. }
  702. int irq_set_percpu_devid_partition(unsigned int irq,
  703. const struct cpumask *affinity)
  704. {
  705. struct irq_desc *desc = irq_to_desc(irq);
  706. if (!desc)
  707. return -EINVAL;
  708. if (desc->percpu_enabled)
  709. return -EINVAL;
  710. desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
  711. if (!desc->percpu_enabled)
  712. return -ENOMEM;
  713. if (affinity)
  714. desc->percpu_affinity = affinity;
  715. else
  716. desc->percpu_affinity = cpu_possible_mask;
  717. irq_set_percpu_devid_flags(irq);
  718. return 0;
  719. }
  720. int irq_set_percpu_devid(unsigned int irq)
  721. {
  722. return irq_set_percpu_devid_partition(irq, NULL);
  723. }
  724. int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
  725. {
  726. struct irq_desc *desc = irq_to_desc(irq);
  727. if (!desc || !desc->percpu_enabled)
  728. return -EINVAL;
  729. if (affinity)
  730. cpumask_copy(affinity, desc->percpu_affinity);
  731. return 0;
  732. }
  733. EXPORT_SYMBOL_GPL(irq_get_percpu_devid_partition);
  734. void kstat_incr_irq_this_cpu(unsigned int irq)
  735. {
  736. kstat_incr_irqs_this_cpu(irq_to_desc(irq));
  737. }
  738. /**
  739. * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
  740. * @irq: The interrupt number
  741. * @cpu: The cpu number
  742. *
  743. * Returns the sum of interrupt counts on @cpu since boot for
  744. * @irq. The caller must ensure that the interrupt is not removed
  745. * concurrently.
  746. */
  747. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  748. {
  749. struct irq_desc *desc = irq_to_desc(irq);
  750. return desc && desc->kstat_irqs ?
  751. *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
  752. }
  753. /**
  754. * kstat_irqs - Get the statistics for an interrupt
  755. * @irq: The interrupt number
  756. *
  757. * Returns the sum of interrupt counts on all cpus since boot for
  758. * @irq. The caller must ensure that the interrupt is not removed
  759. * concurrently.
  760. */
  761. unsigned int kstat_irqs(unsigned int irq)
  762. {
  763. struct irq_desc *desc = irq_to_desc(irq);
  764. int cpu;
  765. unsigned int sum = 0;
  766. if (!desc || !desc->kstat_irqs)
  767. return 0;
  768. for_each_possible_cpu(cpu)
  769. sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
  770. return sum;
  771. }
  772. /**
  773. * kstat_irqs_usr - Get the statistics for an interrupt
  774. * @irq: The interrupt number
  775. *
  776. * Returns the sum of interrupt counts on all cpus since boot for @irq.
  777. * Contrary to kstat_irqs() this can be called from any context.
  778. * It uses rcu since a concurrent removal of an interrupt descriptor is
  779. * observing an rcu grace period before delayed_free_desc()/irq_kobj_release().
  780. */
  781. unsigned int kstat_irqs_usr(unsigned int irq)
  782. {
  783. unsigned int sum;
  784. rcu_read_lock();
  785. sum = kstat_irqs(irq);
  786. rcu_read_unlock();
  787. return sum;
  788. }