acpi-dma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * ACPI helpers for DMA request / controller
  3. *
  4. * Based on of-dma.c
  5. *
  6. * Copyright (C) 2013, Intel Corporation
  7. * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  8. * Mika Westerberg <mika.westerberg@linux.intel.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/module.h>
  17. #include <linux/list.h>
  18. #include <linux/mutex.h>
  19. #include <linux/slab.h>
  20. #include <linux/ioport.h>
  21. #include <linux/acpi.h>
  22. #include <linux/acpi_dma.h>
  23. #include <linux/property.h>
  24. static LIST_HEAD(acpi_dma_list);
  25. static DEFINE_MUTEX(acpi_dma_lock);
  26. /**
  27. * acpi_dma_parse_resource_group - match device and parse resource group
  28. * @grp: CSRT resource group
  29. * @adev: ACPI device to match with
  30. * @adma: struct acpi_dma of the given DMA controller
  31. *
  32. * In order to match a device from DSDT table to the corresponding CSRT device
  33. * we use MMIO address and IRQ.
  34. *
  35. * Return:
  36. * 1 on success, 0 when no information is available, or appropriate errno value
  37. * on error.
  38. */
  39. static int acpi_dma_parse_resource_group(const struct acpi_csrt_group *grp,
  40. struct acpi_device *adev, struct acpi_dma *adma)
  41. {
  42. const struct acpi_csrt_shared_info *si;
  43. struct list_head resource_list;
  44. struct resource_entry *rentry;
  45. resource_size_t mem = 0, irq = 0;
  46. int ret;
  47. if (grp->shared_info_length != sizeof(struct acpi_csrt_shared_info))
  48. return -ENODEV;
  49. INIT_LIST_HEAD(&resource_list);
  50. ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  51. if (ret <= 0)
  52. return 0;
  53. list_for_each_entry(rentry, &resource_list, node) {
  54. if (resource_type(rentry->res) == IORESOURCE_MEM)
  55. mem = rentry->res->start;
  56. else if (resource_type(rentry->res) == IORESOURCE_IRQ)
  57. irq = rentry->res->start;
  58. }
  59. acpi_dev_free_resource_list(&resource_list);
  60. /* Consider initial zero values as resource not found */
  61. if (mem == 0 && irq == 0)
  62. return 0;
  63. si = (const struct acpi_csrt_shared_info *)&grp[1];
  64. /* Match device by MMIO and IRQ */
  65. if (si->mmio_base_low != mem || si->gsi_interrupt != irq)
  66. return 0;
  67. dev_dbg(&adev->dev, "matches with %.4s%04X (rev %u)\n",
  68. (char *)&grp->vendor_id, grp->device_id, grp->revision);
  69. /* Check if the request line range is available */
  70. if (si->base_request_line == 0 && si->num_handshake_signals == 0)
  71. return 0;
  72. adma->base_request_line = si->base_request_line;
  73. adma->end_request_line = si->base_request_line +
  74. si->num_handshake_signals - 1;
  75. dev_dbg(&adev->dev, "request line base: 0x%04x end: 0x%04x\n",
  76. adma->base_request_line, adma->end_request_line);
  77. return 1;
  78. }
  79. /**
  80. * acpi_dma_parse_csrt - parse CSRT to exctract additional DMA resources
  81. * @adev: ACPI device to match with
  82. * @adma: struct acpi_dma of the given DMA controller
  83. *
  84. * CSRT or Core System Resources Table is a proprietary ACPI table
  85. * introduced by Microsoft. This table can contain devices that are not in
  86. * the system DSDT table. In particular DMA controllers might be described
  87. * here.
  88. *
  89. * We are using this table to get the request line range of the specific DMA
  90. * controller to be used later.
  91. */
  92. static void acpi_dma_parse_csrt(struct acpi_device *adev, struct acpi_dma *adma)
  93. {
  94. struct acpi_csrt_group *grp, *end;
  95. struct acpi_table_csrt *csrt;
  96. acpi_status status;
  97. int ret;
  98. status = acpi_get_table(ACPI_SIG_CSRT, 0,
  99. (struct acpi_table_header **)&csrt);
  100. if (ACPI_FAILURE(status)) {
  101. if (status != AE_NOT_FOUND)
  102. dev_warn(&adev->dev, "failed to get the CSRT table\n");
  103. return;
  104. }
  105. grp = (struct acpi_csrt_group *)(csrt + 1);
  106. end = (struct acpi_csrt_group *)((void *)csrt + csrt->header.length);
  107. while (grp < end) {
  108. ret = acpi_dma_parse_resource_group(grp, adev, adma);
  109. if (ret < 0) {
  110. dev_warn(&adev->dev,
  111. "error in parsing resource group\n");
  112. return;
  113. }
  114. grp = (struct acpi_csrt_group *)((void *)grp + grp->length);
  115. }
  116. }
  117. /**
  118. * acpi_dma_controller_register - Register a DMA controller to ACPI DMA helpers
  119. * @dev: struct device of DMA controller
  120. * @acpi_dma_xlate: translation function which converts a dma specifier
  121. * into a dma_chan structure
  122. * @data pointer to controller specific data to be used by
  123. * translation function
  124. *
  125. * Allocated memory should be freed with appropriate acpi_dma_controller_free()
  126. * call.
  127. *
  128. * Return:
  129. * 0 on success or appropriate errno value on error.
  130. */
  131. int acpi_dma_controller_register(struct device *dev,
  132. struct dma_chan *(*acpi_dma_xlate)
  133. (struct acpi_dma_spec *, struct acpi_dma *),
  134. void *data)
  135. {
  136. struct acpi_device *adev;
  137. struct acpi_dma *adma;
  138. if (!dev || !acpi_dma_xlate)
  139. return -EINVAL;
  140. /* Check if the device was enumerated by ACPI */
  141. adev = ACPI_COMPANION(dev);
  142. if (!adev)
  143. return -EINVAL;
  144. adma = kzalloc(sizeof(*adma), GFP_KERNEL);
  145. if (!adma)
  146. return -ENOMEM;
  147. adma->dev = dev;
  148. adma->acpi_dma_xlate = acpi_dma_xlate;
  149. adma->data = data;
  150. acpi_dma_parse_csrt(adev, adma);
  151. /* Now queue acpi_dma controller structure in list */
  152. mutex_lock(&acpi_dma_lock);
  153. list_add_tail(&adma->dma_controllers, &acpi_dma_list);
  154. mutex_unlock(&acpi_dma_lock);
  155. return 0;
  156. }
  157. EXPORT_SYMBOL_GPL(acpi_dma_controller_register);
  158. /**
  159. * acpi_dma_controller_free - Remove a DMA controller from ACPI DMA helpers list
  160. * @dev: struct device of DMA controller
  161. *
  162. * Memory allocated by acpi_dma_controller_register() is freed here.
  163. *
  164. * Return:
  165. * 0 on success or appropriate errno value on error.
  166. */
  167. int acpi_dma_controller_free(struct device *dev)
  168. {
  169. struct acpi_dma *adma;
  170. if (!dev)
  171. return -EINVAL;
  172. mutex_lock(&acpi_dma_lock);
  173. list_for_each_entry(adma, &acpi_dma_list, dma_controllers)
  174. if (adma->dev == dev) {
  175. list_del(&adma->dma_controllers);
  176. mutex_unlock(&acpi_dma_lock);
  177. kfree(adma);
  178. return 0;
  179. }
  180. mutex_unlock(&acpi_dma_lock);
  181. return -ENODEV;
  182. }
  183. EXPORT_SYMBOL_GPL(acpi_dma_controller_free);
  184. static void devm_acpi_dma_release(struct device *dev, void *res)
  185. {
  186. acpi_dma_controller_free(dev);
  187. }
  188. /**
  189. * devm_acpi_dma_controller_register - resource managed acpi_dma_controller_register()
  190. * @dev: device that is registering this DMA controller
  191. * @acpi_dma_xlate: translation function
  192. * @data pointer to controller specific data
  193. *
  194. * Managed acpi_dma_controller_register(). DMA controller registered by this
  195. * function are automatically freed on driver detach. See
  196. * acpi_dma_controller_register() for more information.
  197. *
  198. * Return:
  199. * 0 on success or appropriate errno value on error.
  200. */
  201. int devm_acpi_dma_controller_register(struct device *dev,
  202. struct dma_chan *(*acpi_dma_xlate)
  203. (struct acpi_dma_spec *, struct acpi_dma *),
  204. void *data)
  205. {
  206. void *res;
  207. int ret;
  208. res = devres_alloc(devm_acpi_dma_release, 0, GFP_KERNEL);
  209. if (!res)
  210. return -ENOMEM;
  211. ret = acpi_dma_controller_register(dev, acpi_dma_xlate, data);
  212. if (ret) {
  213. devres_free(res);
  214. return ret;
  215. }
  216. devres_add(dev, res);
  217. return 0;
  218. }
  219. EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_register);
  220. /**
  221. * devm_acpi_dma_controller_free - resource managed acpi_dma_controller_free()
  222. *
  223. * Unregister a DMA controller registered with
  224. * devm_acpi_dma_controller_register(). Normally this function will not need to
  225. * be called and the resource management code will ensure that the resource is
  226. * freed.
  227. */
  228. void devm_acpi_dma_controller_free(struct device *dev)
  229. {
  230. WARN_ON(devres_release(dev, devm_acpi_dma_release, NULL, NULL));
  231. }
  232. EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_free);
  233. /**
  234. * acpi_dma_update_dma_spec - prepare dma specifier to pass to translation function
  235. * @adma: struct acpi_dma of DMA controller
  236. * @dma_spec: dma specifier to update
  237. *
  238. * Accordingly to ACPI 5.0 Specification Table 6-170 "Fixed DMA Resource
  239. * Descriptor":
  240. * DMA Request Line bits is a platform-relative number uniquely
  241. * identifying the request line assigned. Request line-to-Controller
  242. * mapping is done in a controller-specific OS driver.
  243. * That's why we can safely adjust slave_id when the appropriate controller is
  244. * found.
  245. *
  246. * Return:
  247. * 0, if no information is avaiable, -1 on mismatch, and 1 otherwise.
  248. */
  249. static int acpi_dma_update_dma_spec(struct acpi_dma *adma,
  250. struct acpi_dma_spec *dma_spec)
  251. {
  252. /* Set link to the DMA controller device */
  253. dma_spec->dev = adma->dev;
  254. /* Check if the request line range is available */
  255. if (adma->base_request_line == 0 && adma->end_request_line == 0)
  256. return 0;
  257. /* Check if slave_id falls to the range */
  258. if (dma_spec->slave_id < adma->base_request_line ||
  259. dma_spec->slave_id > adma->end_request_line)
  260. return -1;
  261. /*
  262. * Here we adjust slave_id. It should be a relative number to the base
  263. * request line.
  264. */
  265. dma_spec->slave_id -= adma->base_request_line;
  266. return 1;
  267. }
  268. struct acpi_dma_parser_data {
  269. struct acpi_dma_spec dma_spec;
  270. size_t index;
  271. size_t n;
  272. };
  273. /**
  274. * acpi_dma_parse_fixed_dma - Parse FixedDMA ACPI resources to a DMA specifier
  275. * @res: struct acpi_resource to get FixedDMA resources from
  276. * @data: pointer to a helper struct acpi_dma_parser_data
  277. */
  278. static int acpi_dma_parse_fixed_dma(struct acpi_resource *res, void *data)
  279. {
  280. struct acpi_dma_parser_data *pdata = data;
  281. if (res->type == ACPI_RESOURCE_TYPE_FIXED_DMA) {
  282. struct acpi_resource_fixed_dma *dma = &res->data.fixed_dma;
  283. if (pdata->n++ == pdata->index) {
  284. pdata->dma_spec.chan_id = dma->channels;
  285. pdata->dma_spec.slave_id = dma->request_lines;
  286. }
  287. }
  288. /* Tell the ACPI core to skip this resource */
  289. return 1;
  290. }
  291. /**
  292. * acpi_dma_request_slave_chan_by_index - Get the DMA slave channel
  293. * @dev: struct device to get DMA request from
  294. * @index: index of FixedDMA descriptor for @dev
  295. *
  296. * Return:
  297. * Pointer to appropriate dma channel on success or an error pointer.
  298. */
  299. struct dma_chan *acpi_dma_request_slave_chan_by_index(struct device *dev,
  300. size_t index)
  301. {
  302. struct acpi_dma_parser_data pdata;
  303. struct acpi_dma_spec *dma_spec = &pdata.dma_spec;
  304. struct list_head resource_list;
  305. struct acpi_device *adev;
  306. struct acpi_dma *adma;
  307. struct dma_chan *chan = NULL;
  308. int found;
  309. /* Check if the device was enumerated by ACPI */
  310. if (!dev)
  311. return ERR_PTR(-ENODEV);
  312. adev = ACPI_COMPANION(dev);
  313. if (!adev)
  314. return ERR_PTR(-ENODEV);
  315. memset(&pdata, 0, sizeof(pdata));
  316. pdata.index = index;
  317. /* Initial values for the request line and channel */
  318. dma_spec->chan_id = -1;
  319. dma_spec->slave_id = -1;
  320. INIT_LIST_HEAD(&resource_list);
  321. acpi_dev_get_resources(adev, &resource_list,
  322. acpi_dma_parse_fixed_dma, &pdata);
  323. acpi_dev_free_resource_list(&resource_list);
  324. if (dma_spec->slave_id < 0 || dma_spec->chan_id < 0)
  325. return ERR_PTR(-ENODEV);
  326. mutex_lock(&acpi_dma_lock);
  327. list_for_each_entry(adma, &acpi_dma_list, dma_controllers) {
  328. /*
  329. * We are not going to call translation function if slave_id
  330. * doesn't fall to the request range.
  331. */
  332. found = acpi_dma_update_dma_spec(adma, dma_spec);
  333. if (found < 0)
  334. continue;
  335. chan = adma->acpi_dma_xlate(dma_spec, adma);
  336. /*
  337. * Try to get a channel only from the DMA controller that
  338. * matches the slave_id. See acpi_dma_update_dma_spec()
  339. * description for the details.
  340. */
  341. if (found > 0 || chan)
  342. break;
  343. }
  344. mutex_unlock(&acpi_dma_lock);
  345. return chan ? chan : ERR_PTR(-EPROBE_DEFER);
  346. }
  347. EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_index);
  348. /**
  349. * acpi_dma_request_slave_chan_by_name - Get the DMA slave channel
  350. * @dev: struct device to get DMA request from
  351. * @name: represents corresponding FixedDMA descriptor for @dev
  352. *
  353. * In order to support both Device Tree and ACPI in a single driver we
  354. * translate the names "tx" and "rx" here based on the most common case where
  355. * the first FixedDMA descriptor is TX and second is RX.
  356. *
  357. * If the device has "dma-names" property the FixedDMA descriptor indices
  358. * are retrieved based on those. Otherwise the function falls back using
  359. * hardcoded indices.
  360. *
  361. * Return:
  362. * Pointer to appropriate dma channel on success or an error pointer.
  363. */
  364. struct dma_chan *acpi_dma_request_slave_chan_by_name(struct device *dev,
  365. const char *name)
  366. {
  367. int index;
  368. index = device_property_match_string(dev, "dma-names", name);
  369. if (index < 0) {
  370. if (!strcmp(name, "tx"))
  371. index = 0;
  372. else if (!strcmp(name, "rx"))
  373. index = 1;
  374. else
  375. return ERR_PTR(-ENODEV);
  376. }
  377. dev_dbg(dev, "found DMA channel \"%s\" at index %d\n", name, index);
  378. return acpi_dma_request_slave_chan_by_index(dev, index);
  379. }
  380. EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_name);
  381. /**
  382. * acpi_dma_simple_xlate - Simple ACPI DMA engine translation helper
  383. * @dma_spec: pointer to ACPI DMA specifier
  384. * @adma: pointer to ACPI DMA controller data
  385. *
  386. * A simple translation function for ACPI based devices. Passes &struct
  387. * dma_spec to the DMA controller driver provided filter function.
  388. *
  389. * Return:
  390. * Pointer to the channel if found or %NULL otherwise.
  391. */
  392. struct dma_chan *acpi_dma_simple_xlate(struct acpi_dma_spec *dma_spec,
  393. struct acpi_dma *adma)
  394. {
  395. struct acpi_dma_filter_info *info = adma->data;
  396. if (!info || !info->filter_fn)
  397. return NULL;
  398. return dma_request_channel(info->dma_cap, info->filter_fn, dma_spec);
  399. }
  400. EXPORT_SYMBOL_GPL(acpi_dma_simple_xlate);