acpi-dma.c 13 KB

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