acpi-dma.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * ACPI helpers for DMA request / controller
  3. *
  4. * Based on of-dma.c
  5. *
  6. * Copyright (C) 2013, Intel Corporation
  7. * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include <linux/list.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. #include <linux/acpi.h>
  19. #include <linux/acpi_dma.h>
  20. static LIST_HEAD(acpi_dma_list);
  21. static DEFINE_MUTEX(acpi_dma_lock);
  22. /**
  23. * acpi_dma_controller_register - Register a DMA controller to ACPI DMA helpers
  24. * @dev: struct device of DMA controller
  25. * @acpi_dma_xlate: translation function which converts a dma specifier
  26. * into a dma_chan structure
  27. * @data pointer to controller specific data to be used by
  28. * translation function
  29. *
  30. * Returns 0 on success or appropriate errno value on error.
  31. *
  32. * Allocated memory should be freed with appropriate acpi_dma_controller_free()
  33. * call.
  34. */
  35. int acpi_dma_controller_register(struct device *dev,
  36. struct dma_chan *(*acpi_dma_xlate)
  37. (struct acpi_dma_spec *, struct acpi_dma *),
  38. void *data)
  39. {
  40. struct acpi_device *adev;
  41. struct acpi_dma *adma;
  42. if (!dev || !acpi_dma_xlate)
  43. return -EINVAL;
  44. /* Check if the device was enumerated by ACPI */
  45. if (!ACPI_HANDLE(dev))
  46. return -EINVAL;
  47. if (acpi_bus_get_device(ACPI_HANDLE(dev), &adev))
  48. return -EINVAL;
  49. adma = kzalloc(sizeof(*adma), GFP_KERNEL);
  50. if (!adma)
  51. return -ENOMEM;
  52. adma->dev = dev;
  53. adma->acpi_dma_xlate = acpi_dma_xlate;
  54. adma->data = data;
  55. /* Now queue acpi_dma controller structure in list */
  56. mutex_lock(&acpi_dma_lock);
  57. list_add_tail(&adma->dma_controllers, &acpi_dma_list);
  58. mutex_unlock(&acpi_dma_lock);
  59. return 0;
  60. }
  61. EXPORT_SYMBOL_GPL(acpi_dma_controller_register);
  62. /**
  63. * acpi_dma_controller_free - Remove a DMA controller from ACPI DMA helpers list
  64. * @dev: struct device of DMA controller
  65. *
  66. * Memory allocated by acpi_dma_controller_register() is freed here.
  67. */
  68. int acpi_dma_controller_free(struct device *dev)
  69. {
  70. struct acpi_dma *adma;
  71. if (!dev)
  72. return -EINVAL;
  73. mutex_lock(&acpi_dma_lock);
  74. list_for_each_entry(adma, &acpi_dma_list, dma_controllers)
  75. if (adma->dev == dev) {
  76. list_del(&adma->dma_controllers);
  77. mutex_unlock(&acpi_dma_lock);
  78. kfree(adma);
  79. return 0;
  80. }
  81. mutex_unlock(&acpi_dma_lock);
  82. return -ENODEV;
  83. }
  84. EXPORT_SYMBOL_GPL(acpi_dma_controller_free);
  85. static void devm_acpi_dma_release(struct device *dev, void *res)
  86. {
  87. acpi_dma_controller_free(dev);
  88. }
  89. /**
  90. * devm_acpi_dma_controller_register - resource managed acpi_dma_controller_register()
  91. * @dev: device that is registering this DMA controller
  92. * @acpi_dma_xlate: translation function
  93. * @data pointer to controller specific data
  94. *
  95. * Managed acpi_dma_controller_register(). DMA controller registered by this
  96. * function are automatically freed on driver detach. See
  97. * acpi_dma_controller_register() for more information.
  98. */
  99. int devm_acpi_dma_controller_register(struct device *dev,
  100. struct dma_chan *(*acpi_dma_xlate)
  101. (struct acpi_dma_spec *, struct acpi_dma *),
  102. void *data)
  103. {
  104. void *res;
  105. int ret;
  106. res = devres_alloc(devm_acpi_dma_release, 0, GFP_KERNEL);
  107. if (!res)
  108. return -ENOMEM;
  109. ret = acpi_dma_controller_register(dev, acpi_dma_xlate, data);
  110. if (ret) {
  111. devres_free(res);
  112. return ret;
  113. }
  114. devres_add(dev, res);
  115. return 0;
  116. }
  117. EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_register);
  118. /**
  119. * devm_acpi_dma_controller_free - resource managed acpi_dma_controller_free()
  120. *
  121. * Unregister a DMA controller registered with
  122. * devm_acpi_dma_controller_register(). Normally this function will not need to
  123. * be called and the resource management code will ensure that the resource is
  124. * freed.
  125. */
  126. void devm_acpi_dma_controller_free(struct device *dev)
  127. {
  128. WARN_ON(devres_destroy(dev, devm_acpi_dma_release, NULL, NULL));
  129. }
  130. EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_free);
  131. struct acpi_dma_parser_data {
  132. struct acpi_dma_spec dma_spec;
  133. size_t index;
  134. size_t n;
  135. };
  136. /**
  137. * acpi_dma_parse_fixed_dma - Parse FixedDMA ACPI resources to a DMA specifier
  138. * @res: struct acpi_resource to get FixedDMA resources from
  139. * @data: pointer to a helper struct acpi_dma_parser_data
  140. */
  141. static int acpi_dma_parse_fixed_dma(struct acpi_resource *res, void *data)
  142. {
  143. struct acpi_dma_parser_data *pdata = data;
  144. if (res->type == ACPI_RESOURCE_TYPE_FIXED_DMA) {
  145. struct acpi_resource_fixed_dma *dma = &res->data.fixed_dma;
  146. if (pdata->n++ == pdata->index) {
  147. pdata->dma_spec.chan_id = dma->channels;
  148. pdata->dma_spec.slave_id = dma->request_lines;
  149. }
  150. }
  151. /* Tell the ACPI core to skip this resource */
  152. return 1;
  153. }
  154. /**
  155. * acpi_dma_request_slave_chan_by_index - Get the DMA slave channel
  156. * @dev: struct device to get DMA request from
  157. * @index: index of FixedDMA descriptor for @dev
  158. *
  159. * Returns pointer to appropriate dma channel on success or NULL on error.
  160. */
  161. struct dma_chan *acpi_dma_request_slave_chan_by_index(struct device *dev,
  162. size_t index)
  163. {
  164. struct acpi_dma_parser_data pdata;
  165. struct acpi_dma_spec *dma_spec = &pdata.dma_spec;
  166. struct list_head resource_list;
  167. struct acpi_device *adev;
  168. struct acpi_dma *adma;
  169. struct dma_chan *chan = NULL;
  170. /* Check if the device was enumerated by ACPI */
  171. if (!dev || !ACPI_HANDLE(dev))
  172. return NULL;
  173. if (acpi_bus_get_device(ACPI_HANDLE(dev), &adev))
  174. return NULL;
  175. memset(&pdata, 0, sizeof(pdata));
  176. pdata.index = index;
  177. /* Initial values for the request line and channel */
  178. dma_spec->chan_id = -1;
  179. dma_spec->slave_id = -1;
  180. INIT_LIST_HEAD(&resource_list);
  181. acpi_dev_get_resources(adev, &resource_list,
  182. acpi_dma_parse_fixed_dma, &pdata);
  183. acpi_dev_free_resource_list(&resource_list);
  184. if (dma_spec->slave_id < 0 || dma_spec->chan_id < 0)
  185. return NULL;
  186. mutex_lock(&acpi_dma_lock);
  187. list_for_each_entry(adma, &acpi_dma_list, dma_controllers) {
  188. dma_spec->dev = adma->dev;
  189. chan = adma->acpi_dma_xlate(dma_spec, adma);
  190. if (chan)
  191. break;
  192. }
  193. mutex_unlock(&acpi_dma_lock);
  194. return chan;
  195. }
  196. EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_index);
  197. /**
  198. * acpi_dma_request_slave_chan_by_name - Get the DMA slave channel
  199. * @dev: struct device to get DMA request from
  200. * @name: represents corresponding FixedDMA descriptor for @dev
  201. *
  202. * In order to support both Device Tree and ACPI in a single driver we
  203. * translate the names "tx" and "rx" here based on the most common case where
  204. * the first FixedDMA descriptor is TX and second is RX.
  205. *
  206. * Returns pointer to appropriate dma channel on success or NULL on error.
  207. */
  208. struct dma_chan *acpi_dma_request_slave_chan_by_name(struct device *dev,
  209. const char *name)
  210. {
  211. size_t index;
  212. if (!strcmp(name, "tx"))
  213. index = 0;
  214. else if (!strcmp(name, "rx"))
  215. index = 1;
  216. else
  217. return NULL;
  218. return acpi_dma_request_slave_chan_by_index(dev, index);
  219. }
  220. EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_name);
  221. /**
  222. * acpi_dma_simple_xlate - Simple ACPI DMA engine translation helper
  223. * @dma_spec: pointer to ACPI DMA specifier
  224. * @adma: pointer to ACPI DMA controller data
  225. *
  226. * A simple translation function for ACPI based devices. Passes &struct
  227. * dma_spec to the DMA controller driver provided filter function. Returns
  228. * pointer to the channel if found or %NULL otherwise.
  229. */
  230. struct dma_chan *acpi_dma_simple_xlate(struct acpi_dma_spec *dma_spec,
  231. struct acpi_dma *adma)
  232. {
  233. struct acpi_dma_filter_info *info = adma->data;
  234. if (!info || !info->filter_fn)
  235. return NULL;
  236. return dma_request_channel(info->dma_cap, info->filter_fn, dma_spec);
  237. }
  238. EXPORT_SYMBOL_GPL(acpi_dma_simple_xlate);