irqdesc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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->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. int cpu, irq = desc->irq_data.irq;
  131. ssize_t ret = 0;
  132. char *p = "";
  133. for_each_possible_cpu(cpu) {
  134. unsigned int c = kstat_irqs_cpu(irq, 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, "%d\n", (int)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 (action = desc->action; action != NULL; action = action->next) {
  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. static struct kobj_type irq_kobj_type = {
  234. .release = irq_kobj_release,
  235. .sysfs_ops = &kobj_sysfs_ops,
  236. .default_attrs = irq_attrs,
  237. };
  238. static void irq_sysfs_add(int irq, struct irq_desc *desc)
  239. {
  240. if (irq_kobj_base) {
  241. /*
  242. * Continue even in case of failure as this is nothing
  243. * crucial.
  244. */
  245. if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
  246. pr_warn("Failed to add kobject for irq %d\n", irq);
  247. }
  248. }
  249. static void irq_sysfs_del(struct irq_desc *desc)
  250. {
  251. /*
  252. * If irq_sysfs_init() has not yet been invoked (early boot), then
  253. * irq_kobj_base is NULL and the descriptor was never added.
  254. * kobject_del() complains about a object with no parent, so make
  255. * it conditional.
  256. */
  257. if (irq_kobj_base)
  258. kobject_del(&desc->kobj);
  259. }
  260. static int __init irq_sysfs_init(void)
  261. {
  262. struct irq_desc *desc;
  263. int irq;
  264. /* Prevent concurrent irq alloc/free */
  265. irq_lock_sparse();
  266. irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
  267. if (!irq_kobj_base) {
  268. irq_unlock_sparse();
  269. return -ENOMEM;
  270. }
  271. /* Add the already allocated interrupts */
  272. for_each_irq_desc(irq, desc)
  273. irq_sysfs_add(irq, desc);
  274. irq_unlock_sparse();
  275. return 0;
  276. }
  277. postcore_initcall(irq_sysfs_init);
  278. #else /* !CONFIG_SYSFS */
  279. static struct kobj_type irq_kobj_type = {
  280. .release = irq_kobj_release,
  281. };
  282. static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
  283. static void irq_sysfs_del(struct irq_desc *desc) {}
  284. #endif /* CONFIG_SYSFS */
  285. static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
  286. static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
  287. {
  288. radix_tree_insert(&irq_desc_tree, irq, desc);
  289. }
  290. struct irq_desc *irq_to_desc(unsigned int irq)
  291. {
  292. return radix_tree_lookup(&irq_desc_tree, irq);
  293. }
  294. EXPORT_SYMBOL(irq_to_desc);
  295. static void delete_irq_desc(unsigned int irq)
  296. {
  297. radix_tree_delete(&irq_desc_tree, irq);
  298. }
  299. #ifdef CONFIG_SMP
  300. static void free_masks(struct irq_desc *desc)
  301. {
  302. #ifdef CONFIG_GENERIC_PENDING_IRQ
  303. free_cpumask_var(desc->pending_mask);
  304. #endif
  305. free_cpumask_var(desc->irq_common_data.affinity);
  306. #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
  307. free_cpumask_var(desc->irq_common_data.effective_affinity);
  308. #endif
  309. }
  310. #else
  311. static inline void free_masks(struct irq_desc *desc) { }
  312. #endif
  313. void irq_lock_sparse(void)
  314. {
  315. mutex_lock(&sparse_irq_lock);
  316. }
  317. void irq_unlock_sparse(void)
  318. {
  319. mutex_unlock(&sparse_irq_lock);
  320. }
  321. static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
  322. const struct cpumask *affinity,
  323. struct module *owner)
  324. {
  325. struct irq_desc *desc;
  326. desc = kzalloc_node(sizeof(*desc), GFP_KERNEL, node);
  327. if (!desc)
  328. return NULL;
  329. /* allocate based on nr_cpu_ids */
  330. desc->kstat_irqs = alloc_percpu(unsigned int);
  331. if (!desc->kstat_irqs)
  332. goto err_desc;
  333. if (alloc_masks(desc, node))
  334. goto err_kstat;
  335. raw_spin_lock_init(&desc->lock);
  336. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  337. mutex_init(&desc->request_mutex);
  338. init_rcu_head(&desc->rcu);
  339. desc_set_defaults(irq, desc, node, affinity, owner);
  340. irqd_set(&desc->irq_data, flags);
  341. kobject_init(&desc->kobj, &irq_kobj_type);
  342. return desc;
  343. err_kstat:
  344. free_percpu(desc->kstat_irqs);
  345. err_desc:
  346. kfree(desc);
  347. return NULL;
  348. }
  349. static void irq_kobj_release(struct kobject *kobj)
  350. {
  351. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  352. free_masks(desc);
  353. free_percpu(desc->kstat_irqs);
  354. kfree(desc);
  355. }
  356. static void delayed_free_desc(struct rcu_head *rhp)
  357. {
  358. struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
  359. kobject_put(&desc->kobj);
  360. }
  361. static void free_desc(unsigned int irq)
  362. {
  363. struct irq_desc *desc = irq_to_desc(irq);
  364. irq_remove_debugfs_entry(desc);
  365. unregister_irq_proc(irq, desc);
  366. /*
  367. * sparse_irq_lock protects also show_interrupts() and
  368. * kstat_irq_usr(). Once we deleted the descriptor from the
  369. * sparse tree we can free it. Access in proc will fail to
  370. * lookup the descriptor.
  371. *
  372. * The sysfs entry must be serialized against a concurrent
  373. * irq_sysfs_init() as well.
  374. */
  375. irq_sysfs_del(desc);
  376. delete_irq_desc(irq);
  377. /*
  378. * We free the descriptor, masks and stat fields via RCU. That
  379. * allows demultiplex interrupts to do rcu based management of
  380. * the child interrupts.
  381. * This also allows us to use rcu in kstat_irqs_usr().
  382. */
  383. call_rcu(&desc->rcu, delayed_free_desc);
  384. }
  385. static int alloc_descs(unsigned int start, unsigned int cnt, int node,
  386. const struct cpumask *affinity, struct module *owner)
  387. {
  388. const struct cpumask *mask = NULL;
  389. struct irq_desc *desc;
  390. unsigned int flags;
  391. int i;
  392. /* Validate affinity mask(s) */
  393. if (affinity) {
  394. for (i = 0, mask = affinity; i < cnt; i++, mask++) {
  395. if (cpumask_empty(mask))
  396. return -EINVAL;
  397. }
  398. }
  399. flags = affinity ? IRQD_AFFINITY_MANAGED | IRQD_MANAGED_SHUTDOWN : 0;
  400. mask = NULL;
  401. for (i = 0; i < cnt; i++) {
  402. if (affinity) {
  403. node = cpu_to_node(cpumask_first(affinity));
  404. mask = affinity;
  405. affinity++;
  406. }
  407. desc = alloc_desc(start + i, node, flags, mask, owner);
  408. if (!desc)
  409. goto err;
  410. irq_insert_desc(start + i, desc);
  411. irq_sysfs_add(start + i, desc);
  412. irq_add_debugfs_entry(start + i, desc);
  413. }
  414. bitmap_set(allocated_irqs, start, cnt);
  415. return start;
  416. err:
  417. for (i--; i >= 0; i--)
  418. free_desc(start + i);
  419. return -ENOMEM;
  420. }
  421. static int irq_expand_nr_irqs(unsigned int nr)
  422. {
  423. if (nr > IRQ_BITMAP_BITS)
  424. return -ENOMEM;
  425. nr_irqs = nr;
  426. return 0;
  427. }
  428. int __init early_irq_init(void)
  429. {
  430. int i, initcnt, node = first_online_node;
  431. struct irq_desc *desc;
  432. init_irq_default_affinity();
  433. /* Let arch update nr_irqs and return the nr of preallocated irqs */
  434. initcnt = arch_probe_nr_irqs();
  435. printk(KERN_INFO "NR_IRQS: %d, nr_irqs: %d, preallocated irqs: %d\n",
  436. NR_IRQS, nr_irqs, initcnt);
  437. if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
  438. nr_irqs = IRQ_BITMAP_BITS;
  439. if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
  440. initcnt = IRQ_BITMAP_BITS;
  441. if (initcnt > nr_irqs)
  442. nr_irqs = initcnt;
  443. for (i = 0; i < initcnt; i++) {
  444. desc = alloc_desc(i, node, 0, NULL, NULL);
  445. set_bit(i, allocated_irqs);
  446. irq_insert_desc(i, desc);
  447. }
  448. return arch_early_irq_init();
  449. }
  450. #else /* !CONFIG_SPARSE_IRQ */
  451. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  452. [0 ... NR_IRQS-1] = {
  453. .handle_irq = handle_bad_irq,
  454. .depth = 1,
  455. .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
  456. }
  457. };
  458. int __init early_irq_init(void)
  459. {
  460. int count, i, node = first_online_node;
  461. struct irq_desc *desc;
  462. init_irq_default_affinity();
  463. printk(KERN_INFO "NR_IRQS: %d\n", NR_IRQS);
  464. desc = irq_desc;
  465. count = ARRAY_SIZE(irq_desc);
  466. for (i = 0; i < count; i++) {
  467. desc[i].kstat_irqs = alloc_percpu(unsigned int);
  468. alloc_masks(&desc[i], node);
  469. raw_spin_lock_init(&desc[i].lock);
  470. lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
  471. mutex_init(&desc[i].request_mutex);
  472. desc_set_defaults(i, &desc[i], node, NULL, NULL);
  473. }
  474. return arch_early_irq_init();
  475. }
  476. struct irq_desc *irq_to_desc(unsigned int irq)
  477. {
  478. return (irq < NR_IRQS) ? irq_desc + irq : NULL;
  479. }
  480. EXPORT_SYMBOL(irq_to_desc);
  481. static void free_desc(unsigned int irq)
  482. {
  483. struct irq_desc *desc = irq_to_desc(irq);
  484. unsigned long flags;
  485. raw_spin_lock_irqsave(&desc->lock, flags);
  486. desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
  487. raw_spin_unlock_irqrestore(&desc->lock, flags);
  488. }
  489. static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
  490. const struct cpumask *affinity,
  491. struct module *owner)
  492. {
  493. u32 i;
  494. for (i = 0; i < cnt; i++) {
  495. struct irq_desc *desc = irq_to_desc(start + i);
  496. desc->owner = owner;
  497. }
  498. bitmap_set(allocated_irqs, start, cnt);
  499. return start;
  500. }
  501. static int irq_expand_nr_irqs(unsigned int nr)
  502. {
  503. return -ENOMEM;
  504. }
  505. void irq_mark_irq(unsigned int irq)
  506. {
  507. mutex_lock(&sparse_irq_lock);
  508. bitmap_set(allocated_irqs, irq, 1);
  509. mutex_unlock(&sparse_irq_lock);
  510. }
  511. #ifdef CONFIG_GENERIC_IRQ_LEGACY
  512. void irq_init_desc(unsigned int irq)
  513. {
  514. free_desc(irq);
  515. }
  516. #endif
  517. #endif /* !CONFIG_SPARSE_IRQ */
  518. /**
  519. * generic_handle_irq - Invoke the handler for a particular irq
  520. * @irq: The irq number to handle
  521. *
  522. */
  523. int generic_handle_irq(unsigned int irq)
  524. {
  525. struct irq_desc *desc = irq_to_desc(irq);
  526. if (!desc)
  527. return -EINVAL;
  528. generic_handle_irq_desc(desc);
  529. return 0;
  530. }
  531. EXPORT_SYMBOL_GPL(generic_handle_irq);
  532. #ifdef CONFIG_HANDLE_DOMAIN_IRQ
  533. /**
  534. * __handle_domain_irq - Invoke the handler for a HW irq belonging to a domain
  535. * @domain: The domain where to perform the lookup
  536. * @hwirq: The HW irq number to convert to a logical one
  537. * @lookup: Whether to perform the domain lookup or not
  538. * @regs: Register file coming from the low-level handling code
  539. *
  540. * Returns: 0 on success, or -EINVAL if conversion has failed
  541. */
  542. int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,
  543. bool lookup, struct pt_regs *regs)
  544. {
  545. struct pt_regs *old_regs = set_irq_regs(regs);
  546. unsigned int irq = hwirq;
  547. int ret = 0;
  548. irq_enter();
  549. #ifdef CONFIG_IRQ_DOMAIN
  550. if (lookup)
  551. irq = irq_find_mapping(domain, hwirq);
  552. #endif
  553. /*
  554. * Some hardware gives randomly wrong interrupts. Rather
  555. * than crashing, do something sensible.
  556. */
  557. if (unlikely(!irq || irq >= nr_irqs)) {
  558. ack_bad_irq(irq);
  559. ret = -EINVAL;
  560. } else {
  561. generic_handle_irq(irq);
  562. }
  563. irq_exit();
  564. set_irq_regs(old_regs);
  565. return ret;
  566. }
  567. #endif
  568. /* Dynamic interrupt handling */
  569. /**
  570. * irq_free_descs - free irq descriptors
  571. * @from: Start of descriptor range
  572. * @cnt: Number of consecutive irqs to free
  573. */
  574. void irq_free_descs(unsigned int from, unsigned int cnt)
  575. {
  576. int i;
  577. if (from >= nr_irqs || (from + cnt) > nr_irqs)
  578. return;
  579. mutex_lock(&sparse_irq_lock);
  580. for (i = 0; i < cnt; i++)
  581. free_desc(from + i);
  582. bitmap_clear(allocated_irqs, from, cnt);
  583. mutex_unlock(&sparse_irq_lock);
  584. }
  585. EXPORT_SYMBOL_GPL(irq_free_descs);
  586. /**
  587. * irq_alloc_descs - allocate and initialize a range of irq descriptors
  588. * @irq: Allocate for specific irq number if irq >= 0
  589. * @from: Start the search from this irq number
  590. * @cnt: Number of consecutive irqs to allocate.
  591. * @node: Preferred node on which the irq descriptor should be allocated
  592. * @owner: Owning module (can be NULL)
  593. * @affinity: Optional pointer to an affinity mask array of size @cnt which
  594. * hints where the irq descriptors should be allocated and which
  595. * default affinities to use
  596. *
  597. * Returns the first irq number or error code
  598. */
  599. int __ref
  600. __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
  601. struct module *owner, const struct cpumask *affinity)
  602. {
  603. int start, ret;
  604. if (!cnt)
  605. return -EINVAL;
  606. if (irq >= 0) {
  607. if (from > irq)
  608. return -EINVAL;
  609. from = irq;
  610. } else {
  611. /*
  612. * For interrupts which are freely allocated the
  613. * architecture can force a lower bound to the @from
  614. * argument. x86 uses this to exclude the GSI space.
  615. */
  616. from = arch_dynirq_lower_bound(from);
  617. }
  618. mutex_lock(&sparse_irq_lock);
  619. start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
  620. from, cnt, 0);
  621. ret = -EEXIST;
  622. if (irq >=0 && start != irq)
  623. goto unlock;
  624. if (start + cnt > nr_irqs) {
  625. ret = irq_expand_nr_irqs(start + cnt);
  626. if (ret)
  627. goto unlock;
  628. }
  629. ret = alloc_descs(start, cnt, node, affinity, owner);
  630. unlock:
  631. mutex_unlock(&sparse_irq_lock);
  632. return ret;
  633. }
  634. EXPORT_SYMBOL_GPL(__irq_alloc_descs);
  635. #ifdef CONFIG_GENERIC_IRQ_LEGACY_ALLOC_HWIRQ
  636. /**
  637. * irq_alloc_hwirqs - Allocate an irq descriptor and initialize the hardware
  638. * @cnt: number of interrupts to allocate
  639. * @node: node on which to allocate
  640. *
  641. * Returns an interrupt number > 0 or 0, if the allocation fails.
  642. */
  643. unsigned int irq_alloc_hwirqs(int cnt, int node)
  644. {
  645. int i, irq = __irq_alloc_descs(-1, 0, cnt, node, NULL, NULL);
  646. if (irq < 0)
  647. return 0;
  648. for (i = irq; cnt > 0; i++, cnt--) {
  649. if (arch_setup_hwirq(i, node))
  650. goto err;
  651. irq_clear_status_flags(i, _IRQ_NOREQUEST);
  652. }
  653. return irq;
  654. err:
  655. for (i--; i >= irq; i--) {
  656. irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
  657. arch_teardown_hwirq(i);
  658. }
  659. irq_free_descs(irq, cnt);
  660. return 0;
  661. }
  662. EXPORT_SYMBOL_GPL(irq_alloc_hwirqs);
  663. /**
  664. * irq_free_hwirqs - Free irq descriptor and cleanup the hardware
  665. * @from: Free from irq number
  666. * @cnt: number of interrupts to free
  667. *
  668. */
  669. void irq_free_hwirqs(unsigned int from, int cnt)
  670. {
  671. int i, j;
  672. for (i = from, j = cnt; j > 0; i++, j--) {
  673. irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
  674. arch_teardown_hwirq(i);
  675. }
  676. irq_free_descs(from, cnt);
  677. }
  678. EXPORT_SYMBOL_GPL(irq_free_hwirqs);
  679. #endif
  680. /**
  681. * irq_get_next_irq - get next allocated irq number
  682. * @offset: where to start the search
  683. *
  684. * Returns next irq number after offset or nr_irqs if none is found.
  685. */
  686. unsigned int irq_get_next_irq(unsigned int offset)
  687. {
  688. return find_next_bit(allocated_irqs, nr_irqs, offset);
  689. }
  690. struct irq_desc *
  691. __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
  692. unsigned int check)
  693. {
  694. struct irq_desc *desc = irq_to_desc(irq);
  695. if (desc) {
  696. if (check & _IRQ_DESC_CHECK) {
  697. if ((check & _IRQ_DESC_PERCPU) &&
  698. !irq_settings_is_per_cpu_devid(desc))
  699. return NULL;
  700. if (!(check & _IRQ_DESC_PERCPU) &&
  701. irq_settings_is_per_cpu_devid(desc))
  702. return NULL;
  703. }
  704. if (bus)
  705. chip_bus_lock(desc);
  706. raw_spin_lock_irqsave(&desc->lock, *flags);
  707. }
  708. return desc;
  709. }
  710. void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
  711. {
  712. raw_spin_unlock_irqrestore(&desc->lock, flags);
  713. if (bus)
  714. chip_bus_sync_unlock(desc);
  715. }
  716. int irq_set_percpu_devid_partition(unsigned int irq,
  717. const struct cpumask *affinity)
  718. {
  719. struct irq_desc *desc = irq_to_desc(irq);
  720. if (!desc)
  721. return -EINVAL;
  722. if (desc->percpu_enabled)
  723. return -EINVAL;
  724. desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
  725. if (!desc->percpu_enabled)
  726. return -ENOMEM;
  727. if (affinity)
  728. desc->percpu_affinity = affinity;
  729. else
  730. desc->percpu_affinity = cpu_possible_mask;
  731. irq_set_percpu_devid_flags(irq);
  732. return 0;
  733. }
  734. int irq_set_percpu_devid(unsigned int irq)
  735. {
  736. return irq_set_percpu_devid_partition(irq, NULL);
  737. }
  738. int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
  739. {
  740. struct irq_desc *desc = irq_to_desc(irq);
  741. if (!desc || !desc->percpu_enabled)
  742. return -EINVAL;
  743. if (affinity)
  744. cpumask_copy(affinity, desc->percpu_affinity);
  745. return 0;
  746. }
  747. EXPORT_SYMBOL_GPL(irq_get_percpu_devid_partition);
  748. void kstat_incr_irq_this_cpu(unsigned int irq)
  749. {
  750. kstat_incr_irqs_this_cpu(irq_to_desc(irq));
  751. }
  752. /**
  753. * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
  754. * @irq: The interrupt number
  755. * @cpu: The cpu number
  756. *
  757. * Returns the sum of interrupt counts on @cpu since boot for
  758. * @irq. The caller must ensure that the interrupt is not removed
  759. * concurrently.
  760. */
  761. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  762. {
  763. struct irq_desc *desc = irq_to_desc(irq);
  764. return desc && desc->kstat_irqs ?
  765. *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
  766. }
  767. /**
  768. * kstat_irqs - Get the statistics for an interrupt
  769. * @irq: The interrupt number
  770. *
  771. * Returns the sum of interrupt counts on all cpus since boot for
  772. * @irq. The caller must ensure that the interrupt is not removed
  773. * concurrently.
  774. */
  775. unsigned int kstat_irqs(unsigned int irq)
  776. {
  777. struct irq_desc *desc = irq_to_desc(irq);
  778. unsigned int sum = 0;
  779. int cpu;
  780. if (!desc || !desc->kstat_irqs)
  781. return 0;
  782. if (!irq_settings_is_per_cpu_devid(desc) &&
  783. !irq_settings_is_per_cpu(desc))
  784. return desc->tot_count;
  785. for_each_possible_cpu(cpu)
  786. sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
  787. return sum;
  788. }
  789. /**
  790. * kstat_irqs_usr - Get the statistics for an interrupt
  791. * @irq: The interrupt number
  792. *
  793. * Returns the sum of interrupt counts on all cpus since boot for @irq.
  794. * Contrary to kstat_irqs() this can be called from any context.
  795. * It uses rcu since a concurrent removal of an interrupt descriptor is
  796. * observing an rcu grace period before delayed_free_desc()/irq_kobj_release().
  797. */
  798. unsigned int kstat_irqs_usr(unsigned int irq)
  799. {
  800. unsigned int sum;
  801. rcu_read_lock();
  802. sum = kstat_irqs(irq);
  803. rcu_read_unlock();
  804. return sum;
  805. }