of-dma.c 10 KB

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