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