acpi-dma.c 12 KB

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