2 * ACPI helpers for DMA request / controller
6 * Copyright (C) 2013, Intel Corporation
7 * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
8 * Mika Westerberg <mika.westerberg@linux.intel.com>
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.
15 #include <linux/device.h>
16 #include <linux/module.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
20 #include <linux/ioport.h>
21 #include <linux/acpi.h>
22 #include <linux/acpi_dma.h>
24 static LIST_HEAD(acpi_dma_list
);
25 static DEFINE_MUTEX(acpi_dma_lock
);
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
33 * Returns 1 on success, 0 when no information is available, or appropriate
34 * errno value on error.
36 * In order to match a device from DSDT table to the corresponding CSRT device
37 * we use MMIO address and IRQ.
39 static int acpi_dma_parse_resource_group(const struct acpi_csrt_group
*grp
,
40 struct acpi_device
*adev
, struct acpi_dma
*adma
)
42 const struct acpi_csrt_shared_info
*si
;
43 struct list_head resource_list
;
44 struct resource_list_entry
*rentry
;
45 resource_size_t mem
= 0, irq
= 0;
48 if (grp
->shared_info_length
!= sizeof(struct acpi_csrt_shared_info
))
51 INIT_LIST_HEAD(&resource_list
);
52 ret
= acpi_dev_get_resources(adev
, &resource_list
, NULL
, NULL
);
56 list_for_each_entry(rentry
, &resource_list
, node
) {
57 if (resource_type(&rentry
->res
) == IORESOURCE_MEM
)
58 mem
= rentry
->res
.start
;
59 else if (resource_type(&rentry
->res
) == IORESOURCE_IRQ
)
60 irq
= rentry
->res
.start
;
63 acpi_dev_free_resource_list(&resource_list
);
65 /* Consider initial zero values as resource not found */
66 if (mem
== 0 && irq
== 0)
69 si
= (const struct acpi_csrt_shared_info
*)&grp
[1];
71 /* Match device by MMIO and IRQ */
72 if (si
->mmio_base_low
!= mem
|| si
->gsi_interrupt
!= irq
)
75 dev_dbg(&adev
->dev
, "matches with %.4s%04X (rev %u)\n",
76 (char *)&grp
->vendor_id
, grp
->device_id
, grp
->revision
);
78 /* Check if the request line range is available */
79 if (si
->base_request_line
== 0 && si
->num_handshake_signals
== 0)
82 adma
->base_request_line
= si
->base_request_line
;
83 adma
->end_request_line
= si
->base_request_line
+
84 si
->num_handshake_signals
- 1;
86 dev_dbg(&adev
->dev
, "request line base: 0x%04x end: 0x%04x\n",
87 adma
->base_request_line
, adma
->end_request_line
);
93 * acpi_dma_parse_csrt - parse CSRT to exctract additional DMA resources
94 * @adev: ACPI device to match with
95 * @adma: struct acpi_dma of the given DMA controller
97 * CSRT or Core System Resources Table is a proprietary ACPI table
98 * introduced by Microsoft. This table can contain devices that are not in
99 * the system DSDT table. In particular DMA controllers might be described
102 * We are using this table to get the request line range of the specific DMA
103 * controller to be used later.
106 static void acpi_dma_parse_csrt(struct acpi_device
*adev
, struct acpi_dma
*adma
)
108 struct acpi_csrt_group
*grp
, *end
;
109 struct acpi_table_csrt
*csrt
;
113 status
= acpi_get_table(ACPI_SIG_CSRT
, 0,
114 (struct acpi_table_header
**)&csrt
);
115 if (ACPI_FAILURE(status
)) {
116 if (status
!= AE_NOT_FOUND
)
117 dev_warn(&adev
->dev
, "failed to get the CSRT table\n");
121 grp
= (struct acpi_csrt_group
*)(csrt
+ 1);
122 end
= (struct acpi_csrt_group
*)((void *)csrt
+ csrt
->header
.length
);
125 ret
= acpi_dma_parse_resource_group(grp
, adev
, adma
);
128 "error in parsing resource group\n");
132 grp
= (struct acpi_csrt_group
*)((void *)grp
+ grp
->length
);
137 * acpi_dma_controller_register - Register a DMA controller to ACPI DMA helpers
138 * @dev: struct device of DMA controller
139 * @acpi_dma_xlate: translation function which converts a dma specifier
140 * into a dma_chan structure
141 * @data pointer to controller specific data to be used by
142 * translation function
144 * Returns 0 on success or appropriate errno value on error.
146 * Allocated memory should be freed with appropriate acpi_dma_controller_free()
149 int acpi_dma_controller_register(struct device
*dev
,
150 struct dma_chan
*(*acpi_dma_xlate
)
151 (struct acpi_dma_spec
*, struct acpi_dma
*),
154 struct acpi_device
*adev
;
155 struct acpi_dma
*adma
;
157 if (!dev
|| !acpi_dma_xlate
)
160 /* Check if the device was enumerated by ACPI */
161 if (!ACPI_HANDLE(dev
))
164 if (acpi_bus_get_device(ACPI_HANDLE(dev
), &adev
))
167 adma
= kzalloc(sizeof(*adma
), GFP_KERNEL
);
172 adma
->acpi_dma_xlate
= acpi_dma_xlate
;
175 acpi_dma_parse_csrt(adev
, adma
);
177 /* Now queue acpi_dma controller structure in list */
178 mutex_lock(&acpi_dma_lock
);
179 list_add_tail(&adma
->dma_controllers
, &acpi_dma_list
);
180 mutex_unlock(&acpi_dma_lock
);
184 EXPORT_SYMBOL_GPL(acpi_dma_controller_register
);
187 * acpi_dma_controller_free - Remove a DMA controller from ACPI DMA helpers list
188 * @dev: struct device of DMA controller
190 * Memory allocated by acpi_dma_controller_register() is freed here.
192 int acpi_dma_controller_free(struct device
*dev
)
194 struct acpi_dma
*adma
;
199 mutex_lock(&acpi_dma_lock
);
201 list_for_each_entry(adma
, &acpi_dma_list
, dma_controllers
)
202 if (adma
->dev
== dev
) {
203 list_del(&adma
->dma_controllers
);
204 mutex_unlock(&acpi_dma_lock
);
209 mutex_unlock(&acpi_dma_lock
);
212 EXPORT_SYMBOL_GPL(acpi_dma_controller_free
);
214 static void devm_acpi_dma_release(struct device
*dev
, void *res
)
216 acpi_dma_controller_free(dev
);
220 * devm_acpi_dma_controller_register - resource managed acpi_dma_controller_register()
221 * @dev: device that is registering this DMA controller
222 * @acpi_dma_xlate: translation function
223 * @data pointer to controller specific data
225 * Managed acpi_dma_controller_register(). DMA controller registered by this
226 * function are automatically freed on driver detach. See
227 * acpi_dma_controller_register() for more information.
229 int devm_acpi_dma_controller_register(struct device
*dev
,
230 struct dma_chan
*(*acpi_dma_xlate
)
231 (struct acpi_dma_spec
*, struct acpi_dma
*),
237 res
= devres_alloc(devm_acpi_dma_release
, 0, GFP_KERNEL
);
241 ret
= acpi_dma_controller_register(dev
, acpi_dma_xlate
, data
);
246 devres_add(dev
, res
);
249 EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_register
);
252 * devm_acpi_dma_controller_free - resource managed acpi_dma_controller_free()
254 * Unregister a DMA controller registered with
255 * devm_acpi_dma_controller_register(). Normally this function will not need to
256 * be called and the resource management code will ensure that the resource is
259 void devm_acpi_dma_controller_free(struct device
*dev
)
261 WARN_ON(devres_destroy(dev
, devm_acpi_dma_release
, NULL
, NULL
));
263 EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_free
);
266 * acpi_dma_update_dma_spec - prepare dma specifier to pass to translation function
267 * @adma: struct acpi_dma of DMA controller
268 * @dma_spec: dma specifier to update
270 * Returns 0, if no information is avaiable, -1 on mismatch, and 1 otherwise.
272 * Accordingly to ACPI 5.0 Specification Table 6-170 "Fixed DMA Resource
274 * DMA Request Line bits is a platform-relative number uniquely
275 * identifying the request line assigned. Request line-to-Controller
276 * mapping is done in a controller-specific OS driver.
277 * That's why we can safely adjust slave_id when the appropriate controller is
280 static int acpi_dma_update_dma_spec(struct acpi_dma
*adma
,
281 struct acpi_dma_spec
*dma_spec
)
283 /* Set link to the DMA controller device */
284 dma_spec
->dev
= adma
->dev
;
286 /* Check if the request line range is available */
287 if (adma
->base_request_line
== 0 && adma
->end_request_line
== 0)
290 /* Check if slave_id falls to the range */
291 if (dma_spec
->slave_id
< adma
->base_request_line
||
292 dma_spec
->slave_id
> adma
->end_request_line
)
296 * Here we adjust slave_id. It should be a relative number to the base
299 dma_spec
->slave_id
-= adma
->base_request_line
;
304 struct acpi_dma_parser_data
{
305 struct acpi_dma_spec dma_spec
;
311 * acpi_dma_parse_fixed_dma - Parse FixedDMA ACPI resources to a DMA specifier
312 * @res: struct acpi_resource to get FixedDMA resources from
313 * @data: pointer to a helper struct acpi_dma_parser_data
315 static int acpi_dma_parse_fixed_dma(struct acpi_resource
*res
, void *data
)
317 struct acpi_dma_parser_data
*pdata
= data
;
319 if (res
->type
== ACPI_RESOURCE_TYPE_FIXED_DMA
) {
320 struct acpi_resource_fixed_dma
*dma
= &res
->data
.fixed_dma
;
322 if (pdata
->n
++ == pdata
->index
) {
323 pdata
->dma_spec
.chan_id
= dma
->channels
;
324 pdata
->dma_spec
.slave_id
= dma
->request_lines
;
328 /* Tell the ACPI core to skip this resource */
333 * acpi_dma_request_slave_chan_by_index - Get the DMA slave channel
334 * @dev: struct device to get DMA request from
335 * @index: index of FixedDMA descriptor for @dev
337 * Returns pointer to appropriate dma channel on success or NULL on error.
339 struct dma_chan
*acpi_dma_request_slave_chan_by_index(struct device
*dev
,
342 struct acpi_dma_parser_data pdata
;
343 struct acpi_dma_spec
*dma_spec
= &pdata
.dma_spec
;
344 struct list_head resource_list
;
345 struct acpi_device
*adev
;
346 struct acpi_dma
*adma
;
347 struct dma_chan
*chan
= NULL
;
350 /* Check if the device was enumerated by ACPI */
351 if (!dev
|| !ACPI_HANDLE(dev
))
354 if (acpi_bus_get_device(ACPI_HANDLE(dev
), &adev
))
357 memset(&pdata
, 0, sizeof(pdata
));
360 /* Initial values for the request line and channel */
361 dma_spec
->chan_id
= -1;
362 dma_spec
->slave_id
= -1;
364 INIT_LIST_HEAD(&resource_list
);
365 acpi_dev_get_resources(adev
, &resource_list
,
366 acpi_dma_parse_fixed_dma
, &pdata
);
367 acpi_dev_free_resource_list(&resource_list
);
369 if (dma_spec
->slave_id
< 0 || dma_spec
->chan_id
< 0)
372 mutex_lock(&acpi_dma_lock
);
374 list_for_each_entry(adma
, &acpi_dma_list
, dma_controllers
) {
376 * We are not going to call translation function if slave_id
377 * doesn't fall to the request range.
379 found
= acpi_dma_update_dma_spec(adma
, dma_spec
);
382 chan
= adma
->acpi_dma_xlate(dma_spec
, adma
);
384 * Try to get a channel only from the DMA controller that
385 * matches the slave_id. See acpi_dma_update_dma_spec()
386 * description for the details.
388 if (found
> 0 || chan
)
392 mutex_unlock(&acpi_dma_lock
);
395 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_index
);
398 * acpi_dma_request_slave_chan_by_name - Get the DMA slave channel
399 * @dev: struct device to get DMA request from
400 * @name: represents corresponding FixedDMA descriptor for @dev
402 * In order to support both Device Tree and ACPI in a single driver we
403 * translate the names "tx" and "rx" here based on the most common case where
404 * the first FixedDMA descriptor is TX and second is RX.
406 * Returns pointer to appropriate dma channel on success or NULL on error.
408 struct dma_chan
*acpi_dma_request_slave_chan_by_name(struct device
*dev
,
413 if (!strcmp(name
, "tx"))
415 else if (!strcmp(name
, "rx"))
420 return acpi_dma_request_slave_chan_by_index(dev
, index
);
422 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_name
);
425 * acpi_dma_simple_xlate - Simple ACPI DMA engine translation helper
426 * @dma_spec: pointer to ACPI DMA specifier
427 * @adma: pointer to ACPI DMA controller data
429 * A simple translation function for ACPI based devices. Passes &struct
430 * dma_spec to the DMA controller driver provided filter function. Returns
431 * pointer to the channel if found or %NULL otherwise.
433 struct dma_chan
*acpi_dma_simple_xlate(struct acpi_dma_spec
*dma_spec
,
434 struct acpi_dma
*adma
)
436 struct acpi_dma_filter_info
*info
= adma
->data
;
438 if (!info
|| !info
->filter_fn
)
441 return dma_request_channel(info
->dma_cap
, info
->filter_fn
, dma_spec
);
443 EXPORT_SYMBOL_GPL(acpi_dma_simple_xlate
);