irqdesc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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/DocBook/genericirq
  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. zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  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. #ifdef CONFIG_CPUMASK_OFFSTACK
  40. if (!irq_default_affinity)
  41. zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  42. #endif
  43. if (cpumask_empty(irq_default_affinity))
  44. cpumask_setall(irq_default_affinity);
  45. }
  46. #else
  47. static void __init init_irq_default_affinity(void)
  48. {
  49. }
  50. #endif
  51. #ifdef CONFIG_SMP
  52. static int alloc_masks(struct irq_desc *desc, gfp_t gfp, int node)
  53. {
  54. if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity,
  55. gfp, node))
  56. return -ENOMEM;
  57. #ifdef CONFIG_GENERIC_PENDING_IRQ
  58. if (!zalloc_cpumask_var_node(&desc->pending_mask, gfp, node)) {
  59. free_cpumask_var(desc->irq_common_data.affinity);
  60. return -ENOMEM;
  61. }
  62. #endif
  63. return 0;
  64. }
  65. static void desc_smp_init(struct irq_desc *desc, int node,
  66. const struct cpumask *affinity)
  67. {
  68. if (!affinity)
  69. affinity = irq_default_affinity;
  70. cpumask_copy(desc->irq_common_data.affinity, affinity);
  71. #ifdef CONFIG_GENERIC_PENDING_IRQ
  72. cpumask_clear(desc->pending_mask);
  73. #endif
  74. #ifdef CONFIG_NUMA
  75. desc->irq_common_data.node = node;
  76. #endif
  77. }
  78. #else
  79. static inline int
  80. alloc_masks(struct irq_desc *desc, gfp_t gfp, int node) { return 0; }
  81. static inline void
  82. desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { }
  83. #endif
  84. static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
  85. const struct cpumask *affinity, struct module *owner)
  86. {
  87. int cpu;
  88. desc->irq_common_data.handler_data = NULL;
  89. desc->irq_common_data.msi_desc = NULL;
  90. desc->irq_data.common = &desc->irq_common_data;
  91. desc->irq_data.irq = irq;
  92. desc->irq_data.chip = &no_irq_chip;
  93. desc->irq_data.chip_data = NULL;
  94. irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
  95. irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
  96. desc->handle_irq = handle_bad_irq;
  97. desc->depth = 1;
  98. desc->irq_count = 0;
  99. desc->irqs_unhandled = 0;
  100. desc->name = NULL;
  101. desc->owner = owner;
  102. for_each_possible_cpu(cpu)
  103. *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
  104. desc_smp_init(desc, node, affinity);
  105. }
  106. int nr_irqs = NR_IRQS;
  107. EXPORT_SYMBOL_GPL(nr_irqs);
  108. static DEFINE_MUTEX(sparse_irq_lock);
  109. static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
  110. #ifdef CONFIG_SPARSE_IRQ
  111. static void irq_kobj_release(struct kobject *kobj);
  112. #ifdef CONFIG_SYSFS
  113. static struct kobject *irq_kobj_base;
  114. #define IRQ_ATTR_RO(_name) \
  115. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  116. static ssize_t per_cpu_count_show(struct kobject *kobj,
  117. struct kobj_attribute *attr, char *buf)
  118. {
  119. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  120. int cpu, irq = desc->irq_data.irq;
  121. ssize_t ret = 0;
  122. char *p = "";
  123. for_each_possible_cpu(cpu) {
  124. unsigned int c = kstat_irqs_cpu(irq, cpu);
  125. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
  126. p = ",";
  127. }
  128. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
  129. return ret;
  130. }
  131. IRQ_ATTR_RO(per_cpu_count);
  132. static ssize_t chip_name_show(struct kobject *kobj,
  133. struct kobj_attribute *attr, char *buf)
  134. {
  135. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  136. ssize_t ret = 0;
  137. raw_spin_lock_irq(&desc->lock);
  138. if (desc->irq_data.chip && desc->irq_data.chip->name) {
  139. ret = scnprintf(buf, PAGE_SIZE, "%s\n",
  140. desc->irq_data.chip->name);
  141. }
  142. raw_spin_unlock_irq(&desc->lock);
  143. return ret;
  144. }
  145. IRQ_ATTR_RO(chip_name);
  146. static ssize_t hwirq_show(struct kobject *kobj,
  147. struct kobj_attribute *attr, char *buf)
  148. {
  149. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  150. ssize_t ret = 0;
  151. raw_spin_lock_irq(&desc->lock);
  152. if (desc->irq_data.domain)
  153. ret = sprintf(buf, "%d\n", (int)desc->irq_data.hwirq);
  154. raw_spin_unlock_irq(&desc->lock);
  155. return ret;
  156. }
  157. IRQ_ATTR_RO(hwirq);
  158. static ssize_t type_show(struct kobject *kobj,
  159. struct kobj_attribute *attr, char *buf)
  160. {
  161. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  162. ssize_t ret = 0;
  163. raw_spin_lock_irq(&desc->lock);
  164. ret = sprintf(buf, "%s\n",
  165. irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
  166. raw_spin_unlock_irq(&desc->lock);
  167. return ret;
  168. }
  169. IRQ_ATTR_RO(type);
  170. static ssize_t name_show(struct kobject *kobj,
  171. struct kobj_attribute *attr, char *buf)
  172. {
  173. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  174. ssize_t ret = 0;
  175. raw_spin_lock_irq(&desc->lock);
  176. if (desc->name)
  177. ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
  178. raw_spin_unlock_irq(&desc->lock);
  179. return ret;
  180. }
  181. IRQ_ATTR_RO(name);
  182. static ssize_t actions_show(struct kobject *kobj,
  183. struct kobj_attribute *attr, char *buf)
  184. {
  185. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  186. struct irqaction *action;
  187. ssize_t ret = 0;
  188. char *p = "";
  189. raw_spin_lock_irq(&desc->lock);
  190. for (action = desc->action; action != NULL; action = action->next) {
  191. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
  192. p, action->name);
  193. p = ",";
  194. }
  195. raw_spin_unlock_irq(&desc->lock);
  196. if (ret)
  197. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
  198. return ret;
  199. }
  200. IRQ_ATTR_RO(actions);
  201. static struct attribute *irq_attrs[] = {
  202. &per_cpu_count_attr.attr,
  203. &chip_name_attr.attr,
  204. &hwirq_attr.attr,
  205. &type_attr.attr,
  206. &name_attr.attr,
  207. &actions_attr.attr,
  208. NULL
  209. };
  210. static struct kobj_type irq_kobj_type = {
  211. .release = irq_kobj_release,
  212. .sysfs_ops = &kobj_sysfs_ops,
  213. .default_attrs = irq_attrs,
  214. };
  215. static void irq_sysfs_add(int irq, struct irq_desc *desc)
  216. {
  217. if (irq_kobj_base) {
  218. /*
  219. * Continue even in case of failure as this is nothing
  220. * crucial.
  221. */
  222. if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
  223. pr_warn("Failed to add kobject for irq %d\n", irq);
  224. }
  225. }
  226. static int __init irq_sysfs_init(void)
  227. {
  228. struct irq_desc *desc;
  229. int irq;
  230. /* Prevent concurrent irq alloc/free */
  231. irq_lock_sparse();
  232. irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
  233. if (!irq_kobj_base) {
  234. irq_unlock_sparse();
  235. return -ENOMEM;
  236. }
  237. /* Add the already allocated interrupts */
  238. for_each_irq_desc(irq, desc)
  239. irq_sysfs_add(irq, desc);
  240. irq_unlock_sparse();
  241. return 0;
  242. }
  243. postcore_initcall(irq_sysfs_init);
  244. #else /* !CONFIG_SYSFS */
  245. static struct kobj_type irq_kobj_type = {
  246. .release = irq_kobj_release,
  247. };
  248. static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
  249. #endif /* CONFIG_SYSFS */
  250. static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
  251. static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
  252. {
  253. radix_tree_insert(&irq_desc_tree, irq, desc);
  254. }
  255. struct irq_desc *irq_to_desc(unsigned int irq)
  256. {
  257. return radix_tree_lookup(&irq_desc_tree, irq);
  258. }
  259. EXPORT_SYMBOL(irq_to_desc);
  260. static void delete_irq_desc(unsigned int irq)
  261. {
  262. radix_tree_delete(&irq_desc_tree, irq);
  263. }
  264. #ifdef CONFIG_SMP
  265. static void free_masks(struct irq_desc *desc)
  266. {
  267. #ifdef CONFIG_GENERIC_PENDING_IRQ
  268. free_cpumask_var(desc->pending_mask);
  269. #endif
  270. free_cpumask_var(desc->irq_common_data.affinity);
  271. }
  272. #else
  273. static inline void free_masks(struct irq_desc *desc) { }
  274. #endif
  275. void irq_lock_sparse(void)
  276. {
  277. mutex_lock(&sparse_irq_lock);
  278. }
  279. void irq_unlock_sparse(void)
  280. {
  281. mutex_unlock(&sparse_irq_lock);
  282. }
  283. static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
  284. const struct cpumask *affinity,
  285. struct module *owner)
  286. {
  287. struct irq_desc *desc;
  288. gfp_t gfp = GFP_KERNEL;
  289. desc = kzalloc_node(sizeof(*desc), gfp, node);
  290. if (!desc)
  291. return NULL;
  292. /* allocate based on nr_cpu_ids */
  293. desc->kstat_irqs = alloc_percpu(unsigned int);
  294. if (!desc->kstat_irqs)
  295. goto err_desc;
  296. if (alloc_masks(desc, gfp, node))
  297. goto err_kstat;
  298. raw_spin_lock_init(&desc->lock);
  299. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  300. init_rcu_head(&desc->rcu);
  301. desc_set_defaults(irq, desc, node, affinity, owner);
  302. irqd_set(&desc->irq_data, flags);
  303. kobject_init(&desc->kobj, &irq_kobj_type);
  304. return desc;
  305. err_kstat:
  306. free_percpu(desc->kstat_irqs);
  307. err_desc:
  308. kfree(desc);
  309. return NULL;
  310. }
  311. static void irq_kobj_release(struct kobject *kobj)
  312. {
  313. struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
  314. free_masks(desc);
  315. free_percpu(desc->kstat_irqs);
  316. kfree(desc);
  317. }
  318. static void delayed_free_desc(struct rcu_head *rhp)
  319. {
  320. struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
  321. kobject_put(&desc->kobj);
  322. }
  323. static void free_desc(unsigned int irq)
  324. {
  325. struct irq_desc *desc = irq_to_desc(irq);
  326. unregister_irq_proc(irq, desc);
  327. /*
  328. * sparse_irq_lock protects also show_interrupts() and
  329. * kstat_irq_usr(). Once we deleted the descriptor from the
  330. * sparse tree we can free it. Access in proc will fail to
  331. * lookup the descriptor.
  332. *
  333. * The sysfs entry must be serialized against a concurrent
  334. * irq_sysfs_init() as well.
  335. */
  336. mutex_lock(&sparse_irq_lock);
  337. kobject_del(&desc->kobj);
  338. delete_irq_desc(irq);
  339. mutex_unlock(&sparse_irq_lock);
  340. /*
  341. * We free the descriptor, masks and stat fields via RCU. That
  342. * allows demultiplex interrupts to do rcu based management of
  343. * the child interrupts.
  344. */
  345. call_rcu(&desc->rcu, delayed_free_desc);
  346. }
  347. static int alloc_descs(unsigned int start, unsigned int cnt, int node,
  348. const struct cpumask *affinity, struct module *owner)
  349. {
  350. const struct cpumask *mask = NULL;
  351. struct irq_desc *desc;
  352. unsigned int flags;
  353. int i;
  354. /* Validate affinity mask(s) */
  355. if (affinity) {
  356. for (i = 0, mask = affinity; i < cnt; i++, mask++) {
  357. if (cpumask_empty(mask))
  358. return -EINVAL;
  359. }
  360. }
  361. flags = affinity ? IRQD_AFFINITY_MANAGED : 0;
  362. mask = NULL;
  363. for (i = 0; i < cnt; i++) {
  364. if (affinity) {
  365. node = cpu_to_node(cpumask_first(affinity));
  366. mask = affinity;
  367. affinity++;
  368. }
  369. desc = alloc_desc(start + i, node, flags, mask, owner);
  370. if (!desc)
  371. goto err;
  372. mutex_lock(&sparse_irq_lock);
  373. irq_insert_desc(start + i, desc);
  374. irq_sysfs_add(start + i, desc);
  375. mutex_unlock(&sparse_irq_lock);
  376. }
  377. return start;
  378. err:
  379. for (i--; i >= 0; i--)
  380. free_desc(start + i);
  381. mutex_lock(&sparse_irq_lock);
  382. bitmap_clear(allocated_irqs, start, cnt);
  383. mutex_unlock(&sparse_irq_lock);
  384. return -ENOMEM;
  385. }
  386. static int irq_expand_nr_irqs(unsigned int nr)
  387. {
  388. if (nr > IRQ_BITMAP_BITS)
  389. return -ENOMEM;
  390. nr_irqs = nr;
  391. return 0;
  392. }
  393. int __init early_irq_init(void)
  394. {
  395. int i, initcnt, node = first_online_node;
  396. struct irq_desc *desc;
  397. init_irq_default_affinity();
  398. /* Let arch update nr_irqs and return the nr of preallocated irqs */
  399. initcnt = arch_probe_nr_irqs();
  400. printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt);
  401. if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
  402. nr_irqs = IRQ_BITMAP_BITS;
  403. if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
  404. initcnt = IRQ_BITMAP_BITS;
  405. if (initcnt > nr_irqs)
  406. nr_irqs = initcnt;
  407. for (i = 0; i < initcnt; i++) {
  408. desc = alloc_desc(i, node, 0, NULL, NULL);
  409. set_bit(i, allocated_irqs);
  410. irq_insert_desc(i, desc);
  411. }
  412. return arch_early_irq_init();
  413. }
  414. #else /* !CONFIG_SPARSE_IRQ */
  415. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  416. [0 ... NR_IRQS-1] = {
  417. .handle_irq = handle_bad_irq,
  418. .depth = 1,
  419. .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
  420. }
  421. };
  422. int __init early_irq_init(void)
  423. {
  424. int count, i, node = first_online_node;
  425. struct irq_desc *desc;
  426. init_irq_default_affinity();
  427. printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
  428. desc = irq_desc;
  429. count = ARRAY_SIZE(irq_desc);
  430. for (i = 0; i < count; i++) {
  431. desc[i].kstat_irqs = alloc_percpu(unsigned int);
  432. alloc_masks(&desc[i], GFP_KERNEL, node);
  433. raw_spin_lock_init(&desc[i].lock);
  434. lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
  435. desc_set_defaults(i, &desc[i], node, NULL, NULL);
  436. }
  437. return arch_early_irq_init();
  438. }
  439. struct irq_desc *irq_to_desc(unsigned int irq)
  440. {
  441. return (irq < NR_IRQS) ? irq_desc + irq : NULL;
  442. }
  443. EXPORT_SYMBOL(irq_to_desc);
  444. static void free_desc(unsigned int irq)
  445. {
  446. struct irq_desc *desc = irq_to_desc(irq);
  447. unsigned long flags;
  448. raw_spin_lock_irqsave(&desc->lock, flags);
  449. desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
  450. raw_spin_unlock_irqrestore(&desc->lock, flags);
  451. }
  452. static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
  453. const struct cpumask *affinity,
  454. struct module *owner)
  455. {
  456. u32 i;
  457. for (i = 0; i < cnt; i++) {
  458. struct irq_desc *desc = irq_to_desc(start + i);
  459. desc->owner = owner;
  460. }
  461. return start;
  462. }
  463. static int irq_expand_nr_irqs(unsigned int nr)
  464. {
  465. return -ENOMEM;
  466. }
  467. void irq_mark_irq(unsigned int irq)
  468. {
  469. mutex_lock(&sparse_irq_lock);
  470. bitmap_set(allocated_irqs, irq, 1);
  471. mutex_unlock(&sparse_irq_lock);
  472. }
  473. #ifdef CONFIG_GENERIC_IRQ_LEGACY
  474. void irq_init_desc(unsigned int irq)
  475. {
  476. free_desc(irq);
  477. }
  478. #endif
  479. #endif /* !CONFIG_SPARSE_IRQ */
  480. /**
  481. * generic_handle_irq - Invoke the handler for a particular irq
  482. * @irq: The irq number to handle
  483. *
  484. */
  485. int generic_handle_irq(unsigned int irq)
  486. {
  487. struct irq_desc *desc = irq_to_desc(irq);
  488. if (!desc)
  489. return -EINVAL;
  490. generic_handle_irq_desc(desc);
  491. return 0;
  492. }
  493. EXPORT_SYMBOL_GPL(generic_handle_irq);
  494. #ifdef CONFIG_HANDLE_DOMAIN_IRQ
  495. /**
  496. * __handle_domain_irq - Invoke the handler for a HW irq belonging to a domain
  497. * @domain: The domain where to perform the lookup
  498. * @hwirq: The HW irq number to convert to a logical one
  499. * @lookup: Whether to perform the domain lookup or not
  500. * @regs: Register file coming from the low-level handling code
  501. *
  502. * Returns: 0 on success, or -EINVAL if conversion has failed
  503. */
  504. int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,
  505. bool lookup, struct pt_regs *regs)
  506. {
  507. struct pt_regs *old_regs = set_irq_regs(regs);
  508. unsigned int irq = hwirq;
  509. int ret = 0;
  510. irq_enter();
  511. #ifdef CONFIG_IRQ_DOMAIN
  512. if (lookup)
  513. irq = irq_find_mapping(domain, hwirq);
  514. #endif
  515. /*
  516. * Some hardware gives randomly wrong interrupts. Rather
  517. * than crashing, do something sensible.
  518. */
  519. if (unlikely(!irq || irq >= nr_irqs)) {
  520. ack_bad_irq(irq);
  521. ret = -EINVAL;
  522. } else {
  523. generic_handle_irq(irq);
  524. }
  525. irq_exit();
  526. set_irq_regs(old_regs);
  527. return ret;
  528. }
  529. #endif
  530. /* Dynamic interrupt handling */
  531. /**
  532. * irq_free_descs - free irq descriptors
  533. * @from: Start of descriptor range
  534. * @cnt: Number of consecutive irqs to free
  535. */
  536. void irq_free_descs(unsigned int from, unsigned int cnt)
  537. {
  538. int i;
  539. if (from >= nr_irqs || (from + cnt) > nr_irqs)
  540. return;
  541. for (i = 0; i < cnt; i++)
  542. free_desc(from + i);
  543. mutex_lock(&sparse_irq_lock);
  544. bitmap_clear(allocated_irqs, from, cnt);
  545. mutex_unlock(&sparse_irq_lock);
  546. }
  547. EXPORT_SYMBOL_GPL(irq_free_descs);
  548. /**
  549. * irq_alloc_descs - allocate and initialize a range of irq descriptors
  550. * @irq: Allocate for specific irq number if irq >= 0
  551. * @from: Start the search from this irq number
  552. * @cnt: Number of consecutive irqs to allocate.
  553. * @node: Preferred node on which the irq descriptor should be allocated
  554. * @owner: Owning module (can be NULL)
  555. * @affinity: Optional pointer to an affinity mask array of size @cnt which
  556. * hints where the irq descriptors should be allocated and which
  557. * default affinities to use
  558. *
  559. * Returns the first irq number or error code
  560. */
  561. int __ref
  562. __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
  563. struct module *owner, const struct cpumask *affinity)
  564. {
  565. int start, ret;
  566. if (!cnt)
  567. return -EINVAL;
  568. if (irq >= 0) {
  569. if (from > irq)
  570. return -EINVAL;
  571. from = irq;
  572. } else {
  573. /*
  574. * For interrupts which are freely allocated the
  575. * architecture can force a lower bound to the @from
  576. * argument. x86 uses this to exclude the GSI space.
  577. */
  578. from = arch_dynirq_lower_bound(from);
  579. }
  580. mutex_lock(&sparse_irq_lock);
  581. start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
  582. from, cnt, 0);
  583. ret = -EEXIST;
  584. if (irq >=0 && start != irq)
  585. goto err;
  586. if (start + cnt > nr_irqs) {
  587. ret = irq_expand_nr_irqs(start + cnt);
  588. if (ret)
  589. goto err;
  590. }
  591. bitmap_set(allocated_irqs, start, cnt);
  592. mutex_unlock(&sparse_irq_lock);
  593. return alloc_descs(start, cnt, node, affinity, owner);
  594. err:
  595. mutex_unlock(&sparse_irq_lock);
  596. return ret;
  597. }
  598. EXPORT_SYMBOL_GPL(__irq_alloc_descs);
  599. #ifdef CONFIG_GENERIC_IRQ_LEGACY_ALLOC_HWIRQ
  600. /**
  601. * irq_alloc_hwirqs - Allocate an irq descriptor and initialize the hardware
  602. * @cnt: number of interrupts to allocate
  603. * @node: node on which to allocate
  604. *
  605. * Returns an interrupt number > 0 or 0, if the allocation fails.
  606. */
  607. unsigned int irq_alloc_hwirqs(int cnt, int node)
  608. {
  609. int i, irq = __irq_alloc_descs(-1, 0, cnt, node, NULL, NULL);
  610. if (irq < 0)
  611. return 0;
  612. for (i = irq; cnt > 0; i++, cnt--) {
  613. if (arch_setup_hwirq(i, node))
  614. goto err;
  615. irq_clear_status_flags(i, _IRQ_NOREQUEST);
  616. }
  617. return irq;
  618. err:
  619. for (i--; i >= irq; i--) {
  620. irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
  621. arch_teardown_hwirq(i);
  622. }
  623. irq_free_descs(irq, cnt);
  624. return 0;
  625. }
  626. EXPORT_SYMBOL_GPL(irq_alloc_hwirqs);
  627. /**
  628. * irq_free_hwirqs - Free irq descriptor and cleanup the hardware
  629. * @from: Free from irq number
  630. * @cnt: number of interrupts to free
  631. *
  632. */
  633. void irq_free_hwirqs(unsigned int from, int cnt)
  634. {
  635. int i, j;
  636. for (i = from, j = cnt; j > 0; i++, j--) {
  637. irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
  638. arch_teardown_hwirq(i);
  639. }
  640. irq_free_descs(from, cnt);
  641. }
  642. EXPORT_SYMBOL_GPL(irq_free_hwirqs);
  643. #endif
  644. /**
  645. * irq_get_next_irq - get next allocated irq number
  646. * @offset: where to start the search
  647. *
  648. * Returns next irq number after offset or nr_irqs if none is found.
  649. */
  650. unsigned int irq_get_next_irq(unsigned int offset)
  651. {
  652. return find_next_bit(allocated_irqs, nr_irqs, offset);
  653. }
  654. struct irq_desc *
  655. __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
  656. unsigned int check)
  657. {
  658. struct irq_desc *desc = irq_to_desc(irq);
  659. if (desc) {
  660. if (check & _IRQ_DESC_CHECK) {
  661. if ((check & _IRQ_DESC_PERCPU) &&
  662. !irq_settings_is_per_cpu_devid(desc))
  663. return NULL;
  664. if (!(check & _IRQ_DESC_PERCPU) &&
  665. irq_settings_is_per_cpu_devid(desc))
  666. return NULL;
  667. }
  668. if (bus)
  669. chip_bus_lock(desc);
  670. raw_spin_lock_irqsave(&desc->lock, *flags);
  671. }
  672. return desc;
  673. }
  674. void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
  675. {
  676. raw_spin_unlock_irqrestore(&desc->lock, flags);
  677. if (bus)
  678. chip_bus_sync_unlock(desc);
  679. }
  680. int irq_set_percpu_devid_partition(unsigned int irq,
  681. const struct cpumask *affinity)
  682. {
  683. struct irq_desc *desc = irq_to_desc(irq);
  684. if (!desc)
  685. return -EINVAL;
  686. if (desc->percpu_enabled)
  687. return -EINVAL;
  688. desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
  689. if (!desc->percpu_enabled)
  690. return -ENOMEM;
  691. if (affinity)
  692. desc->percpu_affinity = affinity;
  693. else
  694. desc->percpu_affinity = cpu_possible_mask;
  695. irq_set_percpu_devid_flags(irq);
  696. return 0;
  697. }
  698. int irq_set_percpu_devid(unsigned int irq)
  699. {
  700. return irq_set_percpu_devid_partition(irq, NULL);
  701. }
  702. int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
  703. {
  704. struct irq_desc *desc = irq_to_desc(irq);
  705. if (!desc || !desc->percpu_enabled)
  706. return -EINVAL;
  707. if (affinity)
  708. cpumask_copy(affinity, desc->percpu_affinity);
  709. return 0;
  710. }
  711. void kstat_incr_irq_this_cpu(unsigned int irq)
  712. {
  713. kstat_incr_irqs_this_cpu(irq_to_desc(irq));
  714. }
  715. /**
  716. * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
  717. * @irq: The interrupt number
  718. * @cpu: The cpu number
  719. *
  720. * Returns the sum of interrupt counts on @cpu since boot for
  721. * @irq. The caller must ensure that the interrupt is not removed
  722. * concurrently.
  723. */
  724. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  725. {
  726. struct irq_desc *desc = irq_to_desc(irq);
  727. return desc && desc->kstat_irqs ?
  728. *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
  729. }
  730. /**
  731. * kstat_irqs - Get the statistics for an interrupt
  732. * @irq: The interrupt number
  733. *
  734. * Returns the sum of interrupt counts on all cpus since boot for
  735. * @irq. The caller must ensure that the interrupt is not removed
  736. * concurrently.
  737. */
  738. unsigned int kstat_irqs(unsigned int irq)
  739. {
  740. struct irq_desc *desc = irq_to_desc(irq);
  741. int cpu;
  742. unsigned int sum = 0;
  743. if (!desc || !desc->kstat_irqs)
  744. return 0;
  745. for_each_possible_cpu(cpu)
  746. sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
  747. return sum;
  748. }
  749. /**
  750. * kstat_irqs_usr - Get the statistics for an interrupt
  751. * @irq: The interrupt number
  752. *
  753. * Returns the sum of interrupt counts on all cpus since boot for
  754. * @irq. Contrary to kstat_irqs() this can be called from any
  755. * preemptible context. It's protected against concurrent removal of
  756. * an interrupt descriptor when sparse irqs are enabled.
  757. */
  758. unsigned int kstat_irqs_usr(unsigned int irq)
  759. {
  760. unsigned int sum;
  761. irq_lock_sparse();
  762. sum = kstat_irqs(irq);
  763. irq_unlock_sparse();
  764. return sum;
  765. }