irqdesc.c 21 KB

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