irqdesc.c 25 KB

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