acpi-dma.c 13 KB

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