irqdesc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 commandline 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->tot_count = 0;
  110. desc->name = NULL;
  111. desc->owner = owner;
  112. for_each_possible_cpu(cpu)
  113. *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
  114. desc_smp_init(desc, node, affinity);
  115. }
  116. int nr_irqs = NR_IRQS;
  117. EXPORT_SYMBOL_GPL(nr_irqs);
  118. static DEFINE_MUTEX(sparse_irq_lock);
  119. static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
  120. #ifdef CONFIG_SPARSE_IRQ
  121. static void irq_kobj_release(struct kobject *kobj);
  122. #ifdef CONFIG_SYSFS
  123. static struct kobject *irq_kobj_base;
  124. #define IRQ_ATTR_RO(_name) \
  125. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  126. static ssize_t per_cpu_count_show(struct kobject *kobj,
  127. struct kobj_attribute *attr, char *buf)
  128. {
  129. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  130. ssize_t ret = 0;
  131. char *p = "";
  132. int cpu;
  133. for_each_possible_cpu(cpu) {
  134. unsigned int c = irq_desc_kstat_cpu(desc, cpu);
  135. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
  136. p = ",";
  137. }
  138. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
  139. return ret;
  140. }
  141. IRQ_ATTR_RO(per_cpu_count);
  142. static ssize_t chip_name_show(struct kobject *kobj,
  143. struct kobj_attribute *attr, char *buf)
  144. {
  145. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  146. ssize_t ret = 0;
  147. raw_spin_lock_irq(&desc->lock);
  148. if (desc->irq_data.chip && desc->irq_data.chip->name) {
  149. ret = scnprintf(buf, PAGE_SIZE, "%s\n",
  150. desc->irq_data.chip->name);
  151. }
  152. raw_spin_unlock_irq(&desc->lock);
  153. return ret;
  154. }
  155. IRQ_ATTR_RO(chip_name);
  156. static ssize_t hwirq_show(struct kobject *kobj,
  157. struct kobj_attribute *attr, char *buf)
  158. {
  159. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  160. ssize_t ret = 0;
  161. raw_spin_lock_irq(&desc->lock);
  162. if (desc->irq_data.domain)
  163. ret = sprintf(buf, "%lu\n", desc->irq_data.hwirq);
  164. raw_spin_unlock_irq(&desc->lock);
  165. return ret;
  166. }
  167. IRQ_ATTR_RO(hwirq);
  168. static ssize_t type_show(struct kobject *kobj,
  169. struct kobj_attribute *attr, char *buf)
  170. {
  171. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  172. ssize_t ret = 0;
  173. raw_spin_lock_irq(&desc->lock);
  174. ret = sprintf(buf, "%s\n",
  175. irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
  176. raw_spin_unlock_irq(&desc->lock);
  177. return ret;
  178. }
  179. IRQ_ATTR_RO(type);
  180. static ssize_t wakeup_show(struct kobject *kobj,
  181. struct kobj_attribute *attr, char *buf)
  182. {
  183. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  184. ssize_t ret = 0;
  185. raw_spin_lock_irq(&desc->lock);
  186. ret = sprintf(buf, "%s\n",
  187. irqd_is_wakeup_set(&desc->irq_data) ? "enabled" : "disabled");
  188. raw_spin_unlock_irq(&desc->lock);
  189. return ret;
  190. }
  191. IRQ_ATTR_RO(wakeup);
  192. static ssize_t name_show(struct kobject *kobj,
  193. struct kobj_attribute *attr, char *buf)
  194. {
  195. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  196. ssize_t ret = 0;
  197. raw_spin_lock_irq(&desc->lock);
  198. if (desc->name)
  199. ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
  200. raw_spin_unlock_irq(&desc->lock);
  201. return ret;
  202. }
  203. IRQ_ATTR_RO(name);
  204. static ssize_t actions_show(struct kobject *kobj,
  205. struct kobj_attribute *attr, char *buf)
  206. {
  207. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  208. struct irqaction *action;
  209. ssize_t ret = 0;
  210. char *p = "";
  211. raw_spin_lock_irq(&desc->lock);
  212. for_each_action_of_desc(desc, action) {
  213. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
  214. p, action->name);
  215. p = ",";
  216. }
  217. raw_spin_unlock_irq(&desc->lock);
  218. if (ret)
  219. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
  220. return ret;
  221. }
  222. IRQ_ATTR_RO(actions);
  223. static struct attribute *irq_attrs[] = {
  224. &per_cpu_count_attr.attr,
  225. &chip_name_attr.attr,
  226. &hwirq_attr.attr,
  227. &type_attr.attr,
  228. &wakeup_attr.attr,
  229. &name_attr.attr,
  230. &actions_attr.attr,
  231. NULL
  232. };
  233. ATTRIBUTE_GROUPS(irq);
  234. static struct kobj_type irq_kobj_type = {
  235. .release = irq_kobj_release,
  236. .sysfs_ops = &kobj_sysfs_ops,
  237. .default_groups = irq_groups,
  238. };
  239. static void irq_sysfs_add(int irq, struct irq_desc *desc)
  240. {
  241. if (irq_kobj_base) {
  242. /*
  243. * Continue even in case of failure as this is nothing
  244. * crucial and failures in the late irq_sysfs_init()
  245. * cannot be rolled back.
  246. */
  247. if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
  248. pr_warn("Failed to add kobject for irq %d\n", irq);
  249. else
  250. desc->istate |= IRQS_SYSFS;
  251. }
  252. }
  253. static void irq_sysfs_del(struct irq_desc *desc)
  254. {
  255. /*
  256. * Only invoke kobject_del() when kobject_add() was successfully
  257. * invoked for the descriptor. This covers both early boot, where
  258. * sysfs is not initialized yet, and the case of a failed
  259. * kobject_add() invocation.
  260. */
  261. if (desc->istate & IRQS_SYSFS)
  262. kobject_del(&desc->kobj);
  263. }
  264. static int __init irq_sysfs_init(void)
  265. {
  266. struct irq_desc *desc;
  267. int irq;
  268. /* Prevent concurrent irq alloc/free */
  269. irq_lock_sparse();
  270. irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
  271. if (!irq_kobj_base) {
  272. irq_unlock_sparse();
  273. return -ENOMEM;
  274. }
  275. /* Add the already allocated interrupts */
  276. for_each_irq_desc(irq, desc)
  277. irq_sysfs_add(irq, desc);
  278. irq_unlock_sparse();
  279. return 0;
  280. }
  281. postcore_initcall(irq_sysfs_init);
  282. #else /* !CONFIG_SYSFS */
  283. static struct kobj_type irq_kobj_type = {
  284. .release = irq_kobj_release,
  285. };
  286. static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
  287. static void irq_sysfs_del(struct irq_desc *desc) {}
  288. #endif /* CONFIG_SYSFS */
  289. static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
  290. static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
  291. {
  292. radix_tree_insert(&irq_desc_tree, irq, desc);
  293. }
  294. struct irq_desc *irq_to_desc(unsigned int irq)
  295. {
  296. return radix_tree_lookup(&irq_desc_tree, irq);
  297. }
  298. EXPORT_SYMBOL_GPL(irq_to_desc);
  299. static void delete_irq_desc(unsigned int irq)
  300. {
  301. radix_tree_delete(&irq_desc_tree, irq);
  302. }
  303. #ifdef CONFIG_SMP
  304. static void free_masks(struct irq_desc *desc)
  305. {
  306. #ifdef CONFIG_GENERIC_PENDING_IRQ
  307. free_cpumask_var(desc->pending_mask);
  308. #endif
  309. free_cpumask_var(desc->irq_common_data.affinity);
  310. #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
  311. free_cpumask_var(desc->irq_common_data.effective_affinity);
  312. #endif
  313. }
  314. #else
  315. static inline void free_masks(struct irq_desc *desc) { }
  316. #endif
  317. void irq_lock_sparse(void)
  318. {
  319. mutex_lock(&sparse_irq_lock);
  320. }
  321. void irq_unlock_sparse(void)
  322. {
  323. mutex_unlock(&sparse_irq_lock);
  324. }
  325. static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
  326. const struct cpumask *affinity,
  327. struct module *owner)
  328. {
  329. struct irq_desc *desc;
  330. desc = kzalloc_node(sizeof(*desc), GFP_KERNEL, node);
  331. if (!desc)
  332. return NULL;
  333. /* allocate based on nr_cpu_ids */
  334. desc->kstat_irqs = alloc_percpu(unsigned int);
  335. if (!desc->kstat_irqs)
  336. goto err_desc;
  337. if (alloc_masks(desc, node))
  338. goto err_kstat;
  339. raw_spin_lock_init(&desc->lock);
  340. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  341. mutex_init(&desc->request_mutex);
  342. init_rcu_head(&desc->rcu);
  343. init_waitqueue_head(&desc->wait_for_threads);
  344. desc_set_defaults(irq, desc, node, affinity, owner);
  345. irqd_set(&desc->irq_data, flags);
  346. kobject_init(&desc->kobj, &irq_kobj_type);
  347. return desc;
  348. err_kstat:
  349. free_percpu(desc->kstat_irqs);
  350. err_desc:
  351. kfree(desc);
  352. return NULL;
  353. }
  354. static void irq_kobj_release(struct kobject *kobj)
  355. {
  356. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  357. free_masks(desc);
  358. free_percpu(desc->kstat_irqs);
  359. kfree(desc);
  360. }
  361. static void delayed_free_desc(struct rcu_head *rhp)
  362. {
  363. struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
  364. kobject_put(&desc->kobj);
  365. }
  366. static void free_desc(unsigned int irq)
  367. {
  368. struct irq_desc *desc = irq_to_desc(irq);
  369. irq_remove_debugfs_entry(desc);
  370. unregister_irq_proc(irq, desc);
  371. /*
  372. * sparse_irq_lock protects also show_interrupts() and
  373. * kstat_irq_usr(). Once we deleted the descriptor from the
  374. * sparse tree we can free it. Access in proc will fail to
  375. * lookup the descriptor.
  376. *
  377. * The sysfs entry must be serialized against a concurrent
  378. * irq_sysfs_init() as well.
  379. */
  380. irq_sysfs_del(desc);
  381. delete_irq_desc(irq);
  382. /*
  383. * We free the descriptor, masks and stat fields via RCU. That
  384. * allows demultiplex interrupts to do rcu based management of
  385. * the child interrupts.
  386. * This also allows us to use rcu in kstat_irqs_usr().
  387. */
  388. call_rcu(&desc->rcu, delayed_free_desc);
  389. }
  390. static int alloc_descs(unsigned int start, unsigned int cnt, int node,
  391. const struct irq_affinity_desc *affinity,
  392. struct module *owner)
  393. {
  394. struct irq_desc *desc;
  395. int i;
  396. /* Validate affinity mask(s) */
  397. if (affinity) {
  398. for (i = 0; i < cnt; i++) {
  399. if (cpumask_empty(&affinity[i].mask))
  400. return -EINVAL;
  401. }
  402. }
  403. for (i = 0; i < cnt; i++) {
  404. const struct cpumask *mask = NULL;
  405. unsigned int flags = 0;
  406. if (affinity) {
  407. if (affinity->is_managed) {
  408. flags = IRQD_AFFINITY_MANAGED |
  409. IRQD_MANAGED_SHUTDOWN;
  410. }
  411. mask = &affinity->mask;
  412. node = cpu_to_node(cpumask_first(mask));
  413. affinity++;
  414. }
  415. desc = alloc_desc(start + i, node, flags, mask, owner);
  416. if (!desc)
  417. goto err;
  418. irq_insert_desc(start + i, desc);
  419. irq_sysfs_add(start + i, desc);
  420. irq_add_debugfs_entry(start + i, desc);
  421. }
  422. bitmap_set(allocated_irqs, start, cnt);
  423. return start;
  424. err:
  425. for (i--; i >= 0; i--)
  426. free_desc(start + i);
  427. return -ENOMEM;
  428. }
  429. static int irq_expand_nr_irqs(unsigned int nr)
  430. {
  431. if (nr > IRQ_BITMAP_BITS)
  432. return -ENOMEM;
  433. nr_irqs = nr;
  434. return 0;
  435. }
  436. int __init early_irq_init(void)
  437. {
  438. int i, initcnt, node = first_online_node;
  439. struct irq_desc *desc;
  440. init_irq_default_affinity();
  441. /* Let arch update nr_irqs and return the nr of preallocated irqs */
  442. initcnt = arch_probe_nr_irqs();
  443. printk(KERN_INFO "NR_IRQS: %d, nr_irqs: %d, preallocated irqs: %d\n",
  444. NR_IRQS, nr_irqs, initcnt);
  445. if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
  446. nr_irqs = IRQ_BITMAP_BITS;
  447. if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
  448. initcnt = IRQ_BITMAP_BITS;
  449. if (initcnt > nr_irqs)
  450. nr_irqs = initcnt;
  451. for (i = 0; i < initcnt; i++) {
  452. desc = alloc_desc(i, node, 0, NULL, NULL);
  453. set_bit(i, allocated_irqs);
  454. irq_insert_desc(i, desc);
  455. }
  456. return arch_early_irq_init();
  457. }
  458. #else /* !CONFIG_SPARSE_IRQ */
  459. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  460. [0 ... NR_IRQS-1] = {
  461. .handle_irq = handle_bad_irq,
  462. .depth = 1,
  463. .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
  464. }
  465. };
  466. int __init early_irq_init(void)
  467. {
  468. int count, i, node = first_online_node;
  469. struct irq_desc *desc;
  470. init_irq_default_affinity();
  471. printk(KERN_INFO "NR_IRQS: %d\n", NR_IRQS);
  472. desc = irq_desc;
  473. count = ARRAY_SIZE(irq_desc);
  474. for (i = 0; i < count; i++) {
  475. desc[i].kstat_irqs = alloc_percpu(unsigned int);
  476. alloc_masks(&desc[i], node);
  477. raw_spin_lock_init(&desc[i].lock);
  478. lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
  479. mutex_init(&desc[i].request_mutex);
  480. init_waitqueue_head(&desc[i].wait_for_threads);
  481. desc_set_defaults(i, &desc[i], node, NULL, NULL);
  482. }
  483. return arch_early_irq_init();
  484. }
  485. struct irq_desc *irq_to_desc(unsigned int irq)
  486. {
  487. return (irq < NR_IRQS) ? irq_desc + irq : NULL;
  488. }
  489. EXPORT_SYMBOL(irq_to_desc);
  490. static void free_desc(unsigned int irq)
  491. {
  492. struct irq_desc *desc = irq_to_desc(irq);
  493. unsigned long flags;
  494. raw_spin_lock_irqsave(&desc->lock, flags);
  495. desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
  496. raw_spin_unlock_irqrestore(&desc->lock, flags);
  497. }
  498. static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
  499. const struct irq_affinity_desc *affinity,
  500. struct module *owner)
  501. {
  502. u32 i;
  503. for (i = 0; i < cnt; i++) {
  504. struct irq_desc *desc = irq_to_desc(start + i);
  505. desc->owner = owner;
  506. }
  507. bitmap_set(allocated_irqs, start, cnt);
  508. return start;
  509. }
  510. static int irq_expand_nr_irqs(unsigned int nr)
  511. {
  512. return -ENOMEM;
  513. }
  514. void irq_mark_irq(unsigned int irq)
  515. {
  516. mutex_lock(&sparse_irq_lock);
  517. bitmap_set(allocated_irqs, irq, 1);
  518. mutex_unlock(&sparse_irq_lock);
  519. }
  520. #ifdef CONFIG_GENERIC_IRQ_LEGACY
  521. void irq_init_desc(unsigned int irq)
  522. {
  523. free_desc(irq);
  524. }
  525. #endif
  526. #endif /* !CONFIG_SPARSE_IRQ */
  527. int handle_irq_desc(struct irq_desc *desc)
  528. {
  529. struct irq_data *data;
  530. if (!desc)
  531. return -EINVAL;
  532. data = irq_desc_get_irq_data(desc);
  533. if (WARN_ON_ONCE(!in_hardirq() && handle_enforce_irqctx(data)))
  534. return -EPERM;
  535. generic_handle_irq_desc(desc);
  536. return 0;
  537. }
  538. /**
  539. * generic_handle_irq - Invoke the handler for a particular irq
  540. * @irq: The irq number to handle
  541. *
  542. * Returns: 0 on success, or -EINVAL if conversion has failed
  543. *
  544. * This function must be called from an IRQ context with irq regs
  545. * initialized.
  546. */
  547. int generic_handle_irq(unsigned int irq)
  548. {
  549. return handle_irq_desc(irq_to_desc(irq));
  550. }
  551. EXPORT_SYMBOL_GPL(generic_handle_irq);
  552. /**
  553. * generic_handle_irq_safe - Invoke the handler for a particular irq from any
  554. * context.
  555. * @irq: The irq number to handle
  556. *
  557. * Returns: 0 on success, a negative value on error.
  558. *
  559. * This function can be called from any context (IRQ or process context). It
  560. * will report an error if not invoked from IRQ context and the irq has been
  561. * marked to enforce IRQ-context only.
  562. */
  563. int generic_handle_irq_safe(unsigned int irq)
  564. {
  565. unsigned long flags;
  566. int ret;
  567. local_irq_save(flags);
  568. ret = handle_irq_desc(irq_to_desc(irq));
  569. local_irq_restore(flags);
  570. return ret;
  571. }
  572. EXPORT_SYMBOL_GPL(generic_handle_irq_safe);
  573. #ifdef CONFIG_IRQ_DOMAIN
  574. /**
  575. * generic_handle_domain_irq - Invoke the handler for a HW irq belonging
  576. * to a domain.
  577. * @domain: The domain where to perform the lookup
  578. * @hwirq: The HW irq number to convert to a logical one
  579. *
  580. * Returns: 0 on success, or -EINVAL if conversion has failed
  581. *
  582. * This function must be called from an IRQ context with irq regs
  583. * initialized.
  584. */
  585. int generic_handle_domain_irq(struct irq_domain *domain, unsigned int hwirq)
  586. {
  587. return handle_irq_desc(irq_resolve_mapping(domain, hwirq));
  588. }
  589. EXPORT_SYMBOL_GPL(generic_handle_domain_irq);
  590. /**
  591. * generic_handle_irq_safe - Invoke the handler for a HW irq belonging
  592. * to a domain from any context.
  593. * @domain: The domain where to perform the lookup
  594. * @hwirq: The HW irq number to convert to a logical one
  595. *
  596. * Returns: 0 on success, a negative value on error.
  597. *
  598. * This function can be called from any context (IRQ or process
  599. * context). If the interrupt is marked as 'enforce IRQ-context only' then
  600. * the function must be invoked from hard interrupt context.
  601. */
  602. int generic_handle_domain_irq_safe(struct irq_domain *domain, unsigned int hwirq)
  603. {
  604. unsigned long flags;
  605. int ret;
  606. local_irq_save(flags);
  607. ret = handle_irq_desc(irq_resolve_mapping(domain, hwirq));
  608. local_irq_restore(flags);
  609. return ret;
  610. }
  611. EXPORT_SYMBOL_GPL(generic_handle_domain_irq_safe);
  612. /**
  613. * generic_handle_domain_nmi - Invoke the handler for a HW nmi belonging
  614. * to a domain.
  615. * @domain: The domain where to perform the lookup
  616. * @hwirq: The HW irq number to convert to a logical one
  617. *
  618. * Returns: 0 on success, or -EINVAL if conversion has failed
  619. *
  620. * This function must be called from an NMI context with irq regs
  621. * initialized.
  622. **/
  623. int generic_handle_domain_nmi(struct irq_domain *domain, unsigned int hwirq)
  624. {
  625. WARN_ON_ONCE(!in_nmi());
  626. return handle_irq_desc(irq_resolve_mapping(domain, hwirq));
  627. }
  628. #endif
  629. /* Dynamic interrupt handling */
  630. /**
  631. * irq_free_descs - free irq descriptors
  632. * @from: Start of descriptor range
  633. * @cnt: Number of consecutive irqs to free
  634. */
  635. void irq_free_descs(unsigned int from, unsigned int cnt)
  636. {
  637. int i;
  638. if (from >= nr_irqs || (from + cnt) > nr_irqs)
  639. return;
  640. mutex_lock(&sparse_irq_lock);
  641. for (i = 0; i < cnt; i++)
  642. free_desc(from + i);
  643. bitmap_clear(allocated_irqs, from, cnt);
  644. mutex_unlock(&sparse_irq_lock);
  645. }
  646. EXPORT_SYMBOL_GPL(irq_free_descs);
  647. /**
  648. * __irq_alloc_descs - allocate and initialize a range of irq descriptors
  649. * @irq: Allocate for specific irq number if irq >= 0
  650. * @from: Start the search from this irq number
  651. * @cnt: Number of consecutive irqs to allocate.
  652. * @node: Preferred node on which the irq descriptor should be allocated
  653. * @owner: Owning module (can be NULL)
  654. * @affinity: Optional pointer to an affinity mask array of size @cnt which
  655. * hints where the irq descriptors should be allocated and which
  656. * default affinities to use
  657. *
  658. * Returns the first irq number or error code
  659. */
  660. int __ref
  661. __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
  662. struct module *owner, const struct irq_affinity_desc *affinity)
  663. {
  664. int start, ret;
  665. if (!cnt)
  666. return -EINVAL;
  667. if (irq >= 0) {
  668. if (from > irq)
  669. return -EINVAL;
  670. from = irq;
  671. } else {
  672. /*
  673. * For interrupts which are freely allocated the
  674. * architecture can force a lower bound to the @from
  675. * argument. x86 uses this to exclude the GSI space.
  676. */
  677. from = arch_dynirq_lower_bound(from);
  678. }
  679. mutex_lock(&sparse_irq_lock);
  680. start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
  681. from, cnt, 0);
  682. ret = -EEXIST;
  683. if (irq >=0 && start != irq)
  684. goto unlock;
  685. if (start + cnt > nr_irqs) {
  686. ret = irq_expand_nr_irqs(start + cnt);
  687. if (ret)
  688. goto unlock;
  689. }
  690. ret = alloc_descs(start, cnt, node, affinity, owner);
  691. unlock:
  692. mutex_unlock(&sparse_irq_lock);
  693. return ret;
  694. }
  695. EXPORT_SYMBOL_GPL(__irq_alloc_descs);
  696. /**
  697. * irq_get_next_irq - get next allocated irq number
  698. * @offset: where to start the search
  699. *
  700. * Returns next irq number after offset or nr_irqs if none is found.
  701. */
  702. unsigned int irq_get_next_irq(unsigned int offset)
  703. {
  704. return find_next_bit(allocated_irqs, nr_irqs, offset);
  705. }
  706. struct irq_desc *
  707. __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
  708. unsigned int check)
  709. {
  710. struct irq_desc *desc = irq_to_desc(irq);
  711. if (desc) {
  712. if (check & _IRQ_DESC_CHECK) {
  713. if ((check & _IRQ_DESC_PERCPU) &&
  714. !irq_settings_is_per_cpu_devid(desc))
  715. return NULL;
  716. if (!(check & _IRQ_DESC_PERCPU) &&
  717. irq_settings_is_per_cpu_devid(desc))
  718. return NULL;
  719. }
  720. if (bus)
  721. chip_bus_lock(desc);
  722. raw_spin_lock_irqsave(&desc->lock, *flags);
  723. }
  724. return desc;
  725. }
  726. void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
  727. __releases(&desc->lock)
  728. {
  729. raw_spin_unlock_irqrestore(&desc->lock, flags);
  730. if (bus)
  731. chip_bus_sync_unlock(desc);
  732. }
  733. int irq_set_percpu_devid_partition(unsigned int irq,
  734. const struct cpumask *affinity)
  735. {
  736. struct irq_desc *desc = irq_to_desc(irq);
  737. if (!desc)
  738. return -EINVAL;
  739. if (desc->percpu_enabled)
  740. return -EINVAL;
  741. desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
  742. if (!desc->percpu_enabled)
  743. return -ENOMEM;
  744. if (affinity)
  745. desc->percpu_affinity = affinity;
  746. else
  747. desc->percpu_affinity = cpu_possible_mask;
  748. irq_set_percpu_devid_flags(irq);
  749. return 0;
  750. }
  751. int irq_set_percpu_devid(unsigned int irq)
  752. {
  753. return irq_set_percpu_devid_partition(irq, NULL);
  754. }
  755. int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
  756. {
  757. struct irq_desc *desc = irq_to_desc(irq);
  758. if (!desc || !desc->percpu_enabled)
  759. return -EINVAL;
  760. if (affinity)
  761. cpumask_copy(affinity, desc->percpu_affinity);
  762. return 0;
  763. }
  764. EXPORT_SYMBOL_GPL(irq_get_percpu_devid_partition);
  765. void kstat_incr_irq_this_cpu(unsigned int irq)
  766. {
  767. kstat_incr_irqs_this_cpu(irq_to_desc(irq));
  768. }
  769. /**
  770. * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
  771. * @irq: The interrupt number
  772. * @cpu: The cpu number
  773. *
  774. * Returns the sum of interrupt counts on @cpu since boot for
  775. * @irq. The caller must ensure that the interrupt is not removed
  776. * concurrently.
  777. */
  778. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  779. {
  780. struct irq_desc *desc = irq_to_desc(irq);
  781. return desc && desc->kstat_irqs ?
  782. *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
  783. }
  784. EXPORT_SYMBOL_GPL(kstat_irqs_cpu);
  785. static bool irq_is_nmi(struct irq_desc *desc)
  786. {
  787. return desc->istate & IRQS_NMI;
  788. }
  789. static unsigned int kstat_irqs(unsigned int irq)
  790. {
  791. struct irq_desc *desc = irq_to_desc(irq);
  792. unsigned int sum = 0;
  793. int cpu;
  794. if (!desc || !desc->kstat_irqs)
  795. return 0;
  796. if (!irq_settings_is_per_cpu_devid(desc) &&
  797. !irq_settings_is_per_cpu(desc) &&
  798. !irq_is_nmi(desc))
  799. return data_race(desc->tot_count);
  800. for_each_possible_cpu(cpu)
  801. sum += data_race(*per_cpu_ptr(desc->kstat_irqs, cpu));
  802. return sum;
  803. }
  804. /**
  805. * kstat_irqs_usr - Get the statistics for an interrupt from thread context
  806. * @irq: The interrupt number
  807. *
  808. * Returns the sum of interrupt counts on all cpus since boot for @irq.
  809. *
  810. * It uses rcu to protect the access since a concurrent removal of an
  811. * interrupt descriptor is observing an rcu grace period before
  812. * delayed_free_desc()/irq_kobj_release().
  813. */
  814. unsigned int kstat_irqs_usr(unsigned int irq)
  815. {
  816. unsigned int sum;
  817. rcu_read_lock();
  818. sum = kstat_irqs(irq);
  819. rcu_read_unlock();
  820. return sum;
  821. }
  822. #ifdef CONFIG_LOCKDEP
  823. void __irq_set_lockdep_class(unsigned int irq, struct lock_class_key *lock_class,
  824. struct lock_class_key *request_class)
  825. {
  826. struct irq_desc *desc = irq_to_desc(irq);
  827. if (desc) {
  828. lockdep_set_class(&desc->lock, lock_class);
  829. lockdep_set_class(&desc->request_mutex, request_class);
  830. }
  831. }
  832. EXPORT_SYMBOL_GPL(__irq_set_lockdep_class);
  833. #endif
  834. EXPORT_SYMBOL_GPL(kstat_irqs_usr);