devres.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/module.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/irqdomain.h>
  5. #include <linux/device.h>
  6. #include <linux/gfp.h>
  7. #include <linux/irq.h>
  8. #include "internals.h"
  9. /*
  10. * Device resource management aware IRQ request/free implementation.
  11. */
  12. struct irq_devres {
  13. unsigned int irq;
  14. void *dev_id;
  15. };
  16. static void devm_irq_release(struct device *dev, void *res)
  17. {
  18. struct irq_devres *this = res;
  19. free_irq(this->irq, this->dev_id);
  20. }
  21. static int devm_irq_match(struct device *dev, void *res, void *data)
  22. {
  23. struct irq_devres *this = res, *match = data;
  24. return this->irq == match->irq && this->dev_id == match->dev_id;
  25. }
  26. /**
  27. * devm_request_threaded_irq - allocate an interrupt line for a managed device
  28. * @dev: device to request interrupt for
  29. * @irq: Interrupt line to allocate
  30. * @handler: Function to be called when the IRQ occurs
  31. * @thread_fn: function to be called in a threaded interrupt context. NULL
  32. * for devices which handle everything in @handler
  33. * @irqflags: Interrupt type flags
  34. * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
  35. * @dev_id: A cookie passed back to the handler function
  36. *
  37. * Except for the extra @dev argument, this function takes the
  38. * same arguments and performs the same function as
  39. * request_threaded_irq(). IRQs requested with this function will be
  40. * automatically freed on driver detach.
  41. *
  42. * If an IRQ allocated with this function needs to be freed
  43. * separately, devm_free_irq() must be used.
  44. */
  45. int devm_request_threaded_irq(struct device *dev, unsigned int irq,
  46. irq_handler_t handler, irq_handler_t thread_fn,
  47. unsigned long irqflags, const char *devname,
  48. void *dev_id)
  49. {
  50. struct irq_devres *dr;
  51. int rc;
  52. dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
  53. GFP_KERNEL);
  54. if (!dr)
  55. return -ENOMEM;
  56. if (!devname)
  57. devname = dev_name(dev);
  58. rc = request_threaded_irq(irq, handler, thread_fn, irqflags, devname,
  59. dev_id);
  60. if (rc) {
  61. devres_free(dr);
  62. return rc;
  63. }
  64. dr->irq = irq;
  65. dr->dev_id = dev_id;
  66. devres_add(dev, dr);
  67. return 0;
  68. }
  69. EXPORT_SYMBOL(devm_request_threaded_irq);
  70. /**
  71. * devm_request_any_context_irq - allocate an interrupt line for a managed device
  72. * @dev: device to request interrupt for
  73. * @irq: Interrupt line to allocate
  74. * @handler: Function to be called when the IRQ occurs
  75. * @irqflags: Interrupt type flags
  76. * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
  77. * @dev_id: A cookie passed back to the handler function
  78. *
  79. * Except for the extra @dev argument, this function takes the
  80. * same arguments and performs the same function as
  81. * request_any_context_irq(). IRQs requested with this function will be
  82. * automatically freed on driver detach.
  83. *
  84. * If an IRQ allocated with this function needs to be freed
  85. * separately, devm_free_irq() must be used.
  86. */
  87. int devm_request_any_context_irq(struct device *dev, unsigned int irq,
  88. irq_handler_t handler, unsigned long irqflags,
  89. const char *devname, void *dev_id)
  90. {
  91. struct irq_devres *dr;
  92. int rc;
  93. dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
  94. GFP_KERNEL);
  95. if (!dr)
  96. return -ENOMEM;
  97. if (!devname)
  98. devname = dev_name(dev);
  99. rc = request_any_context_irq(irq, handler, irqflags, devname, dev_id);
  100. if (rc < 0) {
  101. devres_free(dr);
  102. return rc;
  103. }
  104. dr->irq = irq;
  105. dr->dev_id = dev_id;
  106. devres_add(dev, dr);
  107. return rc;
  108. }
  109. EXPORT_SYMBOL(devm_request_any_context_irq);
  110. /**
  111. * devm_free_irq - free an interrupt
  112. * @dev: device to free interrupt for
  113. * @irq: Interrupt line to free
  114. * @dev_id: Device identity to free
  115. *
  116. * Except for the extra @dev argument, this function takes the
  117. * same arguments and performs the same function as free_irq().
  118. * This function instead of free_irq() should be used to manually
  119. * free IRQs allocated with devm_request_irq().
  120. */
  121. void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id)
  122. {
  123. struct irq_devres match_data = { irq, dev_id };
  124. WARN_ON(devres_destroy(dev, devm_irq_release, devm_irq_match,
  125. &match_data));
  126. free_irq(irq, dev_id);
  127. }
  128. EXPORT_SYMBOL(devm_free_irq);
  129. struct irq_desc_devres {
  130. unsigned int from;
  131. unsigned int cnt;
  132. };
  133. static void devm_irq_desc_release(struct device *dev, void *res)
  134. {
  135. struct irq_desc_devres *this = res;
  136. irq_free_descs(this->from, this->cnt);
  137. }
  138. /**
  139. * __devm_irq_alloc_descs - Allocate and initialize a range of irq descriptors
  140. * for a managed device
  141. * @dev: Device to allocate the descriptors for
  142. * @irq: Allocate for specific irq number if irq >= 0
  143. * @from: Start the search from this irq number
  144. * @cnt: Number of consecutive irqs to allocate
  145. * @node: Preferred node on which the irq descriptor should be allocated
  146. * @owner: Owning module (can be NULL)
  147. * @affinity: Optional pointer to an irq_affinity_desc array of size @cnt
  148. * which hints where the irq descriptors should be allocated
  149. * and which default affinities to use
  150. *
  151. * Returns the first irq number or error code.
  152. *
  153. * Note: Use the provided wrappers (devm_irq_alloc_desc*) for simplicity.
  154. */
  155. int __devm_irq_alloc_descs(struct device *dev, int irq, unsigned int from,
  156. unsigned int cnt, int node, struct module *owner,
  157. const struct irq_affinity_desc *affinity)
  158. {
  159. struct irq_desc_devres *dr;
  160. int base;
  161. dr = devres_alloc(devm_irq_desc_release, sizeof(*dr), GFP_KERNEL);
  162. if (!dr)
  163. return -ENOMEM;
  164. base = __irq_alloc_descs(irq, from, cnt, node, owner, affinity);
  165. if (base < 0) {
  166. devres_free(dr);
  167. return base;
  168. }
  169. dr->from = base;
  170. dr->cnt = cnt;
  171. devres_add(dev, dr);
  172. return base;
  173. }
  174. EXPORT_SYMBOL_GPL(__devm_irq_alloc_descs);
  175. #ifdef CONFIG_GENERIC_IRQ_CHIP
  176. /**
  177. * devm_irq_alloc_generic_chip - Allocate and initialize a generic chip
  178. * for a managed device
  179. * @dev: Device to allocate the generic chip for
  180. * @name: Name of the irq chip
  181. * @num_ct: Number of irq_chip_type instances associated with this
  182. * @irq_base: Interrupt base nr for this chip
  183. * @reg_base: Register base address (virtual)
  184. * @handler: Default flow handler associated with this chip
  185. *
  186. * Returns an initialized irq_chip_generic structure. The chip defaults
  187. * to the primary (index 0) irq_chip_type and @handler
  188. */
  189. struct irq_chip_generic *
  190. devm_irq_alloc_generic_chip(struct device *dev, const char *name, int num_ct,
  191. unsigned int irq_base, void __iomem *reg_base,
  192. irq_flow_handler_t handler)
  193. {
  194. struct irq_chip_generic *gc;
  195. gc = devm_kzalloc(dev, struct_size(gc, chip_types, num_ct), GFP_KERNEL);
  196. if (gc)
  197. irq_init_generic_chip(gc, name, num_ct,
  198. irq_base, reg_base, handler);
  199. return gc;
  200. }
  201. EXPORT_SYMBOL_GPL(devm_irq_alloc_generic_chip);
  202. struct irq_generic_chip_devres {
  203. struct irq_chip_generic *gc;
  204. u32 msk;
  205. unsigned int clr;
  206. unsigned int set;
  207. };
  208. static void devm_irq_remove_generic_chip(struct device *dev, void *res)
  209. {
  210. struct irq_generic_chip_devres *this = res;
  211. irq_remove_generic_chip(this->gc, this->msk, this->clr, this->set);
  212. }
  213. /**
  214. * devm_irq_setup_generic_chip - Setup a range of interrupts with a generic
  215. * chip for a managed device
  216. *
  217. * @dev: Device to setup the generic chip for
  218. * @gc: Generic irq chip holding all data
  219. * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
  220. * @flags: Flags for initialization
  221. * @clr: IRQ_* bits to clear
  222. * @set: IRQ_* bits to set
  223. *
  224. * Set up max. 32 interrupts starting from gc->irq_base. Note, this
  225. * initializes all interrupts to the primary irq_chip_type and its
  226. * associated handler.
  227. */
  228. int devm_irq_setup_generic_chip(struct device *dev, struct irq_chip_generic *gc,
  229. u32 msk, enum irq_gc_flags flags,
  230. unsigned int clr, unsigned int set)
  231. {
  232. struct irq_generic_chip_devres *dr;
  233. dr = devres_alloc(devm_irq_remove_generic_chip,
  234. sizeof(*dr), GFP_KERNEL);
  235. if (!dr)
  236. return -ENOMEM;
  237. irq_setup_generic_chip(gc, msk, flags, clr, set);
  238. dr->gc = gc;
  239. dr->msk = msk;
  240. dr->clr = clr;
  241. dr->set = set;
  242. devres_add(dev, dr);
  243. return 0;
  244. }
  245. EXPORT_SYMBOL_GPL(devm_irq_setup_generic_chip);
  246. #endif /* CONFIG_GENERIC_IRQ_CHIP */
  247. #ifdef CONFIG_IRQ_DOMAIN
  248. static void devm_irq_domain_remove(struct device *dev, void *res)
  249. {
  250. struct irq_domain **domain = res;
  251. irq_domain_remove(*domain);
  252. }
  253. /**
  254. * devm_irq_domain_instantiate() - Instantiate a new irq domain data for a
  255. * managed device.
  256. * @dev: Device to instantiate the domain for
  257. * @info: Domain information pointer pointing to the information for this
  258. * domain
  259. *
  260. * Return: A pointer to the instantiated irq domain or an ERR_PTR value.
  261. */
  262. struct irq_domain *devm_irq_domain_instantiate(struct device *dev,
  263. const struct irq_domain_info *info)
  264. {
  265. struct irq_domain *domain;
  266. struct irq_domain **dr;
  267. dr = devres_alloc(devm_irq_domain_remove, sizeof(*dr), GFP_KERNEL);
  268. if (!dr)
  269. return ERR_PTR(-ENOMEM);
  270. domain = irq_domain_instantiate(info);
  271. if (!IS_ERR(domain)) {
  272. *dr = domain;
  273. devres_add(dev, dr);
  274. } else {
  275. devres_free(dr);
  276. }
  277. return domain;
  278. }
  279. EXPORT_SYMBOL_GPL(devm_irq_domain_instantiate);
  280. #endif /* CONFIG_IRQ_DOMAIN */