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