of-dma.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Device tree helpers for DMA request / controller
  4. *
  5. * Based on of_gpio.c
  6. *
  7. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  8. */
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/slab.h>
  14. #include <linux/of.h>
  15. #include <linux/of_dma.h>
  16. #include "dmaengine.h"
  17. static LIST_HEAD(of_dma_list);
  18. static DEFINE_MUTEX(of_dma_lock);
  19. /**
  20. * of_dma_find_controller - Get a DMA controller in DT DMA helpers list
  21. * @dma_spec: pointer to DMA specifier as found in the device tree
  22. *
  23. * Finds a DMA controller with matching device node and number for dma cells
  24. * in a list of registered DMA controllers. If a match is found a valid pointer
  25. * to the DMA data stored is retuned. A NULL pointer is returned if no match is
  26. * found.
  27. */
  28. static struct of_dma *of_dma_find_controller(struct of_phandle_args *dma_spec)
  29. {
  30. struct of_dma *ofdma;
  31. list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
  32. if (ofdma->of_node == dma_spec->np)
  33. return ofdma;
  34. pr_debug("%s: can't find DMA controller %pOF\n", __func__,
  35. dma_spec->np);
  36. return NULL;
  37. }
  38. /**
  39. * of_dma_router_xlate - translation function for router devices
  40. * @dma_spec: pointer to DMA specifier as found in the device tree
  41. * @ofdma: pointer to DMA controller data (router information)
  42. *
  43. * The function creates new dma_spec to be passed to the router driver's
  44. * of_dma_route_allocate() function to prepare a dma_spec which will be used
  45. * to request channel from the real DMA controller.
  46. */
  47. static struct dma_chan *of_dma_router_xlate(struct of_phandle_args *dma_spec,
  48. struct of_dma *ofdma)
  49. {
  50. struct dma_chan *chan;
  51. struct of_dma *ofdma_target;
  52. struct of_phandle_args dma_spec_target;
  53. void *route_data;
  54. /* translate the request for the real DMA controller */
  55. memcpy(&dma_spec_target, dma_spec, sizeof(dma_spec_target));
  56. route_data = ofdma->of_dma_route_allocate(&dma_spec_target, ofdma);
  57. if (IS_ERR(route_data))
  58. return NULL;
  59. ofdma_target = of_dma_find_controller(&dma_spec_target);
  60. if (!ofdma_target) {
  61. ofdma->dma_router->route_free(ofdma->dma_router->dev,
  62. route_data);
  63. chan = ERR_PTR(-EPROBE_DEFER);
  64. goto err;
  65. }
  66. chan = ofdma_target->of_dma_xlate(&dma_spec_target, ofdma_target);
  67. if (IS_ERR_OR_NULL(chan)) {
  68. ofdma->dma_router->route_free(ofdma->dma_router->dev,
  69. route_data);
  70. } else {
  71. int ret = 0;
  72. chan->router = ofdma->dma_router;
  73. chan->route_data = route_data;
  74. if (chan->device->device_router_config)
  75. ret = chan->device->device_router_config(chan);
  76. if (ret) {
  77. dma_release_channel(chan);
  78. chan = ERR_PTR(ret);
  79. }
  80. }
  81. err:
  82. /*
  83. * Need to put the node back since the ofdma->of_dma_route_allocate
  84. * has taken it for generating the new, translated dma_spec
  85. */
  86. of_node_put(dma_spec_target.np);
  87. return chan;
  88. }
  89. /**
  90. * of_dma_controller_register - Register a DMA controller to DT DMA helpers
  91. * @np: device node of DMA controller
  92. * @of_dma_xlate: translation function which converts a phandle
  93. * arguments list into a dma_chan structure
  94. * @data: pointer to controller specific data to be used by
  95. * translation function
  96. *
  97. * Returns 0 on success or appropriate errno value on error.
  98. *
  99. * Allocated memory should be freed with appropriate of_dma_controller_free()
  100. * call.
  101. */
  102. int of_dma_controller_register(struct device_node *np,
  103. struct dma_chan *(*of_dma_xlate)
  104. (struct of_phandle_args *, struct of_dma *),
  105. void *data)
  106. {
  107. struct of_dma *ofdma;
  108. if (!np || !of_dma_xlate) {
  109. pr_err("%s: not enough information provided\n", __func__);
  110. return -EINVAL;
  111. }
  112. ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
  113. if (!ofdma)
  114. return -ENOMEM;
  115. ofdma->of_node = np;
  116. ofdma->of_dma_xlate = of_dma_xlate;
  117. ofdma->of_dma_data = data;
  118. /* Now queue of_dma controller structure in list */
  119. mutex_lock(&of_dma_lock);
  120. list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
  121. mutex_unlock(&of_dma_lock);
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(of_dma_controller_register);
  125. /**
  126. * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list
  127. * @np: device node of DMA controller
  128. *
  129. * Memory allocated by of_dma_controller_register() is freed here.
  130. */
  131. void of_dma_controller_free(struct device_node *np)
  132. {
  133. struct of_dma *ofdma;
  134. mutex_lock(&of_dma_lock);
  135. list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
  136. if (ofdma->of_node == np) {
  137. list_del(&ofdma->of_dma_controllers);
  138. kfree(ofdma);
  139. break;
  140. }
  141. mutex_unlock(&of_dma_lock);
  142. }
  143. EXPORT_SYMBOL_GPL(of_dma_controller_free);
  144. /**
  145. * of_dma_router_register - Register a DMA router to DT DMA helpers as a
  146. * controller
  147. * @np: device node of DMA router
  148. * @of_dma_route_allocate: setup function for the router which need to
  149. * modify the dma_spec for the DMA controller to
  150. * use and to set up the requested route.
  151. * @dma_router: pointer to dma_router structure to be used when
  152. * the route need to be free up.
  153. *
  154. * Returns 0 on success or appropriate errno value on error.
  155. *
  156. * Allocated memory should be freed with appropriate of_dma_controller_free()
  157. * call.
  158. */
  159. int of_dma_router_register(struct device_node *np,
  160. void *(*of_dma_route_allocate)
  161. (struct of_phandle_args *, struct of_dma *),
  162. struct dma_router *dma_router)
  163. {
  164. struct of_dma *ofdma;
  165. if (!np || !of_dma_route_allocate || !dma_router) {
  166. pr_err("%s: not enough information provided\n", __func__);
  167. return -EINVAL;
  168. }
  169. ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
  170. if (!ofdma)
  171. return -ENOMEM;
  172. ofdma->of_node = np;
  173. ofdma->of_dma_xlate = of_dma_router_xlate;
  174. ofdma->of_dma_route_allocate = of_dma_route_allocate;
  175. ofdma->dma_router = dma_router;
  176. /* Now queue of_dma controller structure in list */
  177. mutex_lock(&of_dma_lock);
  178. list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
  179. mutex_unlock(&of_dma_lock);
  180. return 0;
  181. }
  182. EXPORT_SYMBOL_GPL(of_dma_router_register);
  183. /**
  184. * of_dma_match_channel - Check if a DMA specifier matches name
  185. * @np: device node to look for DMA channels
  186. * @name: channel name to be matched
  187. * @index: index of DMA specifier in list of DMA specifiers
  188. * @dma_spec: pointer to DMA specifier as found in the device tree
  189. *
  190. * Check if the DMA specifier pointed to by the index in a list of DMA
  191. * specifiers, matches the name provided. Returns 0 if the name matches and
  192. * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
  193. */
  194. static int of_dma_match_channel(struct device_node *np, const char *name,
  195. int index, struct of_phandle_args *dma_spec)
  196. {
  197. const char *s;
  198. if (of_property_read_string_index(np, "dma-names", index, &s))
  199. return -ENODEV;
  200. if (strcmp(name, s))
  201. return -ENODEV;
  202. if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
  203. dma_spec))
  204. return -ENODEV;
  205. return 0;
  206. }
  207. /**
  208. * of_dma_request_slave_channel - Get the DMA slave channel
  209. * @np: device node to get DMA request from
  210. * @name: name of desired channel
  211. *
  212. * Returns pointer to appropriate DMA channel on success or an error pointer.
  213. */
  214. struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
  215. const char *name)
  216. {
  217. struct of_phandle_args dma_spec;
  218. struct of_dma *ofdma;
  219. struct dma_chan *chan;
  220. int count, i, start;
  221. int ret_no_channel = -ENODEV;
  222. static atomic_t last_index;
  223. if (!np || !name) {
  224. pr_err("%s: not enough information provided\n", __func__);
  225. return ERR_PTR(-ENODEV);
  226. }
  227. /* Silently fail if there is not even the "dmas" property */
  228. if (!of_find_property(np, "dmas", NULL))
  229. return ERR_PTR(-ENODEV);
  230. count = of_property_count_strings(np, "dma-names");
  231. if (count < 0) {
  232. pr_err("%s: dma-names property of node '%pOF' missing or empty\n",
  233. __func__, np);
  234. return ERR_PTR(-ENODEV);
  235. }
  236. /*
  237. * approximate an average distribution across multiple
  238. * entries with the same name
  239. */
  240. start = atomic_inc_return(&last_index);
  241. for (i = 0; i < count; i++) {
  242. if (of_dma_match_channel(np, name,
  243. (i + start) % count,
  244. &dma_spec))
  245. continue;
  246. mutex_lock(&of_dma_lock);
  247. ofdma = of_dma_find_controller(&dma_spec);
  248. if (ofdma) {
  249. chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
  250. } else {
  251. ret_no_channel = -EPROBE_DEFER;
  252. chan = NULL;
  253. }
  254. mutex_unlock(&of_dma_lock);
  255. of_node_put(dma_spec.np);
  256. if (chan)
  257. return chan;
  258. }
  259. return ERR_PTR(ret_no_channel);
  260. }
  261. EXPORT_SYMBOL_GPL(of_dma_request_slave_channel);
  262. /**
  263. * of_dma_simple_xlate - Simple DMA engine translation function
  264. * @dma_spec: pointer to DMA specifier as found in the device tree
  265. * @ofdma: pointer to DMA controller data
  266. *
  267. * A simple translation function for devices that use a 32-bit value for the
  268. * filter_param when calling the DMA engine dma_request_channel() function.
  269. * Note that this translation function requires that #dma-cells is equal to 1
  270. * and the argument of the dma specifier is the 32-bit filter_param. Returns
  271. * pointer to appropriate dma channel on success or NULL on error.
  272. */
  273. struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
  274. struct of_dma *ofdma)
  275. {
  276. int count = dma_spec->args_count;
  277. struct of_dma_filter_info *info = ofdma->of_dma_data;
  278. if (!info || !info->filter_fn)
  279. return NULL;
  280. if (count != 1)
  281. return NULL;
  282. return __dma_request_channel(&info->dma_cap, info->filter_fn,
  283. &dma_spec->args[0], dma_spec->np);
  284. }
  285. EXPORT_SYMBOL_GPL(of_dma_simple_xlate);
  286. /**
  287. * of_dma_xlate_by_chan_id - Translate dt property to DMA channel by channel id
  288. * @dma_spec: pointer to DMA specifier as found in the device tree
  289. * @ofdma: pointer to DMA controller data
  290. *
  291. * This function can be used as the of xlate callback for DMA driver which wants
  292. * to match the channel based on the channel id. When using this xlate function
  293. * the #dma-cells propety of the DMA controller dt node needs to be set to 1.
  294. * The data parameter of of_dma_controller_register must be a pointer to the
  295. * dma_device struct the function should match upon.
  296. *
  297. * Returns pointer to appropriate dma channel on success or NULL on error.
  298. */
  299. struct dma_chan *of_dma_xlate_by_chan_id(struct of_phandle_args *dma_spec,
  300. struct of_dma *ofdma)
  301. {
  302. struct dma_device *dev = ofdma->of_dma_data;
  303. struct dma_chan *chan, *candidate = NULL;
  304. if (!dev || dma_spec->args_count != 1)
  305. return NULL;
  306. list_for_each_entry(chan, &dev->channels, device_node)
  307. if (chan->chan_id == dma_spec->args[0]) {
  308. candidate = chan;
  309. break;
  310. }
  311. if (!candidate)
  312. return NULL;
  313. return dma_get_slave_channel(candidate);
  314. }
  315. EXPORT_SYMBOL_GPL(of_dma_xlate_by_chan_id);