of-dma.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. chan->router = ofdma->dma_router;
  72. chan->route_data = route_data;
  73. }
  74. err:
  75. /*
  76. * Need to put the node back since the ofdma->of_dma_route_allocate
  77. * has taken it for generating the new, translated dma_spec
  78. */
  79. of_node_put(dma_spec_target.np);
  80. return chan;
  81. }
  82. /**
  83. * of_dma_controller_register - Register a DMA controller to DT DMA helpers
  84. * @np: device node of DMA controller
  85. * @of_dma_xlate: translation function which converts a phandle
  86. * arguments list into a dma_chan structure
  87. * @data: pointer to controller specific data to be used by
  88. * translation function
  89. *
  90. * Returns 0 on success or appropriate errno value on error.
  91. *
  92. * Allocated memory should be freed with appropriate of_dma_controller_free()
  93. * call.
  94. */
  95. int of_dma_controller_register(struct device_node *np,
  96. struct dma_chan *(*of_dma_xlate)
  97. (struct of_phandle_args *, struct of_dma *),
  98. void *data)
  99. {
  100. struct of_dma *ofdma;
  101. if (!np || !of_dma_xlate) {
  102. pr_err("%s: not enough information provided\n", __func__);
  103. return -EINVAL;
  104. }
  105. ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
  106. if (!ofdma)
  107. return -ENOMEM;
  108. ofdma->of_node = np;
  109. ofdma->of_dma_xlate = of_dma_xlate;
  110. ofdma->of_dma_data = data;
  111. /* Now queue of_dma controller structure in list */
  112. mutex_lock(&of_dma_lock);
  113. list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
  114. mutex_unlock(&of_dma_lock);
  115. return 0;
  116. }
  117. EXPORT_SYMBOL_GPL(of_dma_controller_register);
  118. /**
  119. * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list
  120. * @np: device node of DMA controller
  121. *
  122. * Memory allocated by of_dma_controller_register() is freed here.
  123. */
  124. void of_dma_controller_free(struct device_node *np)
  125. {
  126. struct of_dma *ofdma;
  127. mutex_lock(&of_dma_lock);
  128. list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
  129. if (ofdma->of_node == np) {
  130. list_del(&ofdma->of_dma_controllers);
  131. kfree(ofdma);
  132. break;
  133. }
  134. mutex_unlock(&of_dma_lock);
  135. }
  136. EXPORT_SYMBOL_GPL(of_dma_controller_free);
  137. /**
  138. * of_dma_router_register - Register a DMA router to DT DMA helpers as a
  139. * controller
  140. * @np: device node of DMA router
  141. * @of_dma_route_allocate: setup function for the router which need to
  142. * modify the dma_spec for the DMA controller to
  143. * use and to set up the requested route.
  144. * @dma_router: pointer to dma_router structure to be used when
  145. * the route need to be free up.
  146. *
  147. * Returns 0 on success or appropriate errno value on error.
  148. *
  149. * Allocated memory should be freed with appropriate of_dma_controller_free()
  150. * call.
  151. */
  152. int of_dma_router_register(struct device_node *np,
  153. void *(*of_dma_route_allocate)
  154. (struct of_phandle_args *, struct of_dma *),
  155. struct dma_router *dma_router)
  156. {
  157. struct of_dma *ofdma;
  158. if (!np || !of_dma_route_allocate || !dma_router) {
  159. pr_err("%s: not enough information provided\n", __func__);
  160. return -EINVAL;
  161. }
  162. ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
  163. if (!ofdma)
  164. return -ENOMEM;
  165. ofdma->of_node = np;
  166. ofdma->of_dma_xlate = of_dma_router_xlate;
  167. ofdma->of_dma_route_allocate = of_dma_route_allocate;
  168. ofdma->dma_router = dma_router;
  169. /* Now queue of_dma controller structure in list */
  170. mutex_lock(&of_dma_lock);
  171. list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
  172. mutex_unlock(&of_dma_lock);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL_GPL(of_dma_router_register);
  176. /**
  177. * of_dma_match_channel - Check if a DMA specifier matches name
  178. * @np: device node to look for DMA channels
  179. * @name: channel name to be matched
  180. * @index: index of DMA specifier in list of DMA specifiers
  181. * @dma_spec: pointer to DMA specifier as found in the device tree
  182. *
  183. * Check if the DMA specifier pointed to by the index in a list of DMA
  184. * specifiers, matches the name provided. Returns 0 if the name matches and
  185. * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
  186. */
  187. static int of_dma_match_channel(struct device_node *np, const char *name,
  188. int index, struct of_phandle_args *dma_spec)
  189. {
  190. const char *s;
  191. if (of_property_read_string_index(np, "dma-names", index, &s))
  192. return -ENODEV;
  193. if (strcmp(name, s))
  194. return -ENODEV;
  195. if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
  196. dma_spec))
  197. return -ENODEV;
  198. return 0;
  199. }
  200. /**
  201. * of_dma_request_slave_channel - Get the DMA slave channel
  202. * @np: device node to get DMA request from
  203. * @name: name of desired channel
  204. *
  205. * Returns pointer to appropriate DMA channel on success or an error pointer.
  206. */
  207. struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
  208. const char *name)
  209. {
  210. struct of_phandle_args dma_spec;
  211. struct of_dma *ofdma;
  212. struct dma_chan *chan;
  213. int count, i, start;
  214. int ret_no_channel = -ENODEV;
  215. static atomic_t last_index;
  216. if (!np || !name) {
  217. pr_err("%s: not enough information provided\n", __func__);
  218. return ERR_PTR(-ENODEV);
  219. }
  220. /* Silently fail if there is not even the "dmas" property */
  221. if (!of_find_property(np, "dmas", NULL))
  222. return ERR_PTR(-ENODEV);
  223. count = of_property_count_strings(np, "dma-names");
  224. if (count < 0) {
  225. pr_err("%s: dma-names property of node '%pOF' missing or empty\n",
  226. __func__, np);
  227. return ERR_PTR(-ENODEV);
  228. }
  229. /*
  230. * approximate an average distribution across multiple
  231. * entries with the same name
  232. */
  233. start = atomic_inc_return(&last_index);
  234. for (i = 0; i < count; i++) {
  235. if (of_dma_match_channel(np, name,
  236. (i + start) % count,
  237. &dma_spec))
  238. continue;
  239. mutex_lock(&of_dma_lock);
  240. ofdma = of_dma_find_controller(&dma_spec);
  241. if (ofdma) {
  242. chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
  243. } else {
  244. ret_no_channel = -EPROBE_DEFER;
  245. chan = NULL;
  246. }
  247. mutex_unlock(&of_dma_lock);
  248. of_node_put(dma_spec.np);
  249. if (chan)
  250. return chan;
  251. }
  252. return ERR_PTR(ret_no_channel);
  253. }
  254. EXPORT_SYMBOL_GPL(of_dma_request_slave_channel);
  255. /**
  256. * of_dma_simple_xlate - Simple DMA engine translation function
  257. * @dma_spec: pointer to DMA specifier as found in the device tree
  258. * @ofdma: pointer to DMA controller data
  259. *
  260. * A simple translation function for devices that use a 32-bit value for the
  261. * filter_param when calling the DMA engine dma_request_channel() function.
  262. * Note that this translation function requires that #dma-cells is equal to 1
  263. * and the argument of the dma specifier is the 32-bit filter_param. Returns
  264. * pointer to appropriate dma channel on success or NULL on error.
  265. */
  266. struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
  267. struct of_dma *ofdma)
  268. {
  269. int count = dma_spec->args_count;
  270. struct of_dma_filter_info *info = ofdma->of_dma_data;
  271. if (!info || !info->filter_fn)
  272. return NULL;
  273. if (count != 1)
  274. return NULL;
  275. return __dma_request_channel(&info->dma_cap, info->filter_fn,
  276. &dma_spec->args[0], dma_spec->np);
  277. }
  278. EXPORT_SYMBOL_GPL(of_dma_simple_xlate);
  279. /**
  280. * of_dma_xlate_by_chan_id - Translate dt property to DMA channel by channel id
  281. * @dma_spec: pointer to DMA specifier as found in the device tree
  282. * @ofdma: pointer to DMA controller data
  283. *
  284. * This function can be used as the of xlate callback for DMA driver which wants
  285. * to match the channel based on the channel id. When using this xlate function
  286. * the #dma-cells propety of the DMA controller dt node needs to be set to 1.
  287. * The data parameter of of_dma_controller_register must be a pointer to the
  288. * dma_device struct the function should match upon.
  289. *
  290. * Returns pointer to appropriate dma channel on success or NULL on error.
  291. */
  292. struct dma_chan *of_dma_xlate_by_chan_id(struct of_phandle_args *dma_spec,
  293. struct of_dma *ofdma)
  294. {
  295. struct dma_device *dev = ofdma->of_dma_data;
  296. struct dma_chan *chan, *candidate = NULL;
  297. if (!dev || dma_spec->args_count != 1)
  298. return NULL;
  299. list_for_each_entry(chan, &dev->channels, device_node)
  300. if (chan->chan_id == dma_spec->args[0]) {
  301. candidate = chan;
  302. break;
  303. }
  304. if (!candidate)
  305. return NULL;
  306. return dma_get_slave_channel(candidate);
  307. }
  308. EXPORT_SYMBOL_GPL(of_dma_xlate_by_chan_id);