irqdesc.c 22 KB

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