1 // SPDX-License-Identifier: GPL-2.0-only
3 * ACPI helpers for DMA request / controller
7 * Copyright (C) 2013, Intel Corporation
8 * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
9 * Mika Westerberg <mika.westerberg@linux.intel.com>
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>
28 static LIST_HEAD(acpi_dma_list
);
29 static DEFINE_MUTEX(acpi_dma_lock
);
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
37 * In order to match a device from DSDT table to the corresponding CSRT device
38 * we use MMIO address and IRQ.
41 * 1 on success, 0 when no information is available, or appropriate errno value
44 static int acpi_dma_parse_resource_group(const struct acpi_csrt_group
*grp
,
45 struct acpi_device
*adev
, struct acpi_dma
*adma
)
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;
53 if (grp
->shared_info_length
!= sizeof(struct acpi_csrt_shared_info
))
56 INIT_LIST_HEAD(&resource_list
);
57 ret
= acpi_dev_get_resources(adev
, &resource_list
, NULL
, NULL
);
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
;
68 acpi_dev_free_resource_list(&resource_list
);
70 /* Consider initial zero values as resource not found */
71 if (mem
== 0 && irq
== 0)
74 si
= (const struct acpi_csrt_shared_info
*)&grp
[1];
76 /* Match device by MMIO */
77 if (si
->mmio_base_low
!= lower_32_bits(mem
) ||
78 si
->mmio_base_high
!= upper_32_bits(mem
))
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.
86 ret
= acpi_register_gsi(NULL
, si
->gsi_interrupt
, si
->interrupt_mode
, si
->interrupt_polarity
);
90 /* Match device by Linux vIRQ */
94 dev_dbg(&adev
->dev
, "matches with %.4s%04X (rev %u)\n",
95 (char *)&grp
->vendor_id
, grp
->device_id
, grp
->revision
);
97 /* Check if the request line range is available */
98 if (si
->base_request_line
== 0 && si
->num_handshake_signals
== 0)
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
));
107 adma
->base_request_line
= si
->base_request_line
;
108 adma
->end_request_line
= si
->base_request_line
+
109 si
->num_handshake_signals
- 1;
111 dev_dbg(&adev
->dev
, "request line base: 0x%04x end: 0x%04x\n",
112 adma
->base_request_line
, adma
->end_request_line
);
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
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
127 * We are using this table to get the request line range of the specific DMA
128 * controller to be used later.
130 static void acpi_dma_parse_csrt(struct acpi_device
*adev
, struct acpi_dma
*adma
)
132 struct acpi_csrt_group
*grp
, *end
;
133 struct acpi_table_csrt
*csrt
;
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");
145 grp
= (struct acpi_csrt_group
*)(csrt
+ 1);
146 end
= (struct acpi_csrt_group
*)((void *)csrt
+ csrt
->header
.length
);
149 ret
= acpi_dma_parse_resource_group(grp
, adev
, adma
);
152 "error in parsing resource group\n");
156 grp
= (struct acpi_csrt_group
*)((void *)grp
+ grp
->length
);
159 acpi_put_table((struct acpi_table_header
*)csrt
);
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
170 * Allocated memory should be freed with appropriate acpi_dma_controller_free()
174 * 0 on success or appropriate errno value on error.
176 int acpi_dma_controller_register(struct device
*dev
,
177 struct dma_chan
*(*acpi_dma_xlate
)
178 (struct acpi_dma_spec
*, struct acpi_dma
*),
181 struct acpi_device
*adev
;
182 struct acpi_dma
*adma
;
184 if (!dev
|| !acpi_dma_xlate
)
187 /* Check if the device was enumerated by ACPI */
188 adev
= ACPI_COMPANION(dev
);
192 adma
= kzalloc(sizeof(*adma
), GFP_KERNEL
);
197 adma
->acpi_dma_xlate
= acpi_dma_xlate
;
200 acpi_dma_parse_csrt(adev
, adma
);
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
);
209 EXPORT_SYMBOL_GPL(acpi_dma_controller_register
);
212 * acpi_dma_controller_free - Remove a DMA controller from ACPI DMA helpers list
213 * @dev: struct device of DMA controller
215 * Memory allocated by acpi_dma_controller_register() is freed here.
218 * 0 on success or appropriate errno value on error.
220 int acpi_dma_controller_free(struct device
*dev
)
222 struct acpi_dma
*adma
;
227 mutex_lock(&acpi_dma_lock
);
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
);
237 mutex_unlock(&acpi_dma_lock
);
240 EXPORT_SYMBOL_GPL(acpi_dma_controller_free
);
242 static void devm_acpi_dma_free(void *dev
)
244 acpi_dma_controller_free(dev
);
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
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.
258 * 0 on success or appropriate errno value on error.
260 int devm_acpi_dma_controller_register(struct device
*dev
,
261 struct dma_chan
*(*acpi_dma_xlate
)
262 (struct acpi_dma_spec
*, struct acpi_dma
*),
267 ret
= acpi_dma_controller_register(dev
, acpi_dma_xlate
, data
);
271 return devm_add_action_or_reset(dev
, devm_acpi_dma_free
, dev
);
273 EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_register
);
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
280 * Accordingly to ACPI 5.0 Specification Table 6-170 "Fixed DMA Resource
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
289 * 0, if no information is available, -1 on mismatch, and 1 otherwise.
291 static int acpi_dma_update_dma_spec(struct acpi_dma
*adma
,
292 struct acpi_dma_spec
*dma_spec
)
294 /* Set link to the DMA controller device */
295 dma_spec
->dev
= adma
->dev
;
297 /* Check if the request line range is available */
298 if (adma
->base_request_line
== 0 && adma
->end_request_line
== 0)
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
)
307 * Here we adjust slave_id. It should be a relative number to the base
310 dma_spec
->slave_id
-= adma
->base_request_line
;
315 struct acpi_dma_parser_data
{
316 struct acpi_dma_spec dma_spec
;
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
326 static int acpi_dma_parse_fixed_dma(struct acpi_resource
*res
, void *data
)
328 struct acpi_dma_parser_data
*pdata
= data
;
330 if (res
->type
== ACPI_RESOURCE_TYPE_FIXED_DMA
) {
331 struct acpi_resource_fixed_dma
*dma
= &res
->data
.fixed_dma
;
333 if (pdata
->n
++ == pdata
->index
) {
334 pdata
->dma_spec
.chan_id
= dma
->channels
;
335 pdata
->dma_spec
.slave_id
= dma
->request_lines
;
339 /* Tell the ACPI core to skip this resource */
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
349 * Pointer to appropriate dma channel on success or an error pointer.
351 struct dma_chan
*acpi_dma_request_slave_chan_by_index(struct device
*dev
,
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
;
363 memset(&pdata
, 0, sizeof(pdata
));
366 /* Initial values for the request line and channel */
367 dma_spec
->chan_id
= -1;
368 dma_spec
->slave_id
= -1;
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
);
377 if (dma_spec
->slave_id
< 0 || dma_spec
->chan_id
< 0)
378 return ERR_PTR(-ENODEV
);
380 mutex_lock(&acpi_dma_lock
);
382 list_for_each_entry(adma
, &acpi_dma_list
, dma_controllers
) {
384 * We are not going to call translation function if slave_id
385 * doesn't fall to the request range.
387 found
= acpi_dma_update_dma_spec(adma
, dma_spec
);
390 chan
= adma
->acpi_dma_xlate(dma_spec
, adma
);
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.
396 if (found
> 0 || chan
)
400 mutex_unlock(&acpi_dma_lock
);
401 return chan
? chan
: ERR_PTR(-EPROBE_DEFER
);
403 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_index
);
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
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.
414 * If the device has "dma-names" property the FixedDMA descriptor indices
415 * are retrieved based on those. Otherwise the function falls back using
419 * Pointer to appropriate dma channel on success or an error pointer.
421 struct dma_chan
*acpi_dma_request_slave_chan_by_name(struct device
*dev
,
426 index
= device_property_match_string(dev
, "dma-names", name
);
428 if (!strcmp(name
, "tx"))
430 else if (!strcmp(name
, "rx"))
433 return ERR_PTR(-ENODEV
);
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
);
439 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_name
);
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
446 * A simple translation function for ACPI based devices. Passes &struct
447 * dma_spec to the DMA controller driver provided filter function.
450 * Pointer to the channel if found or %NULL otherwise.
452 struct dma_chan
*acpi_dma_simple_xlate(struct acpi_dma_spec
*dma_spec
,
453 struct acpi_dma
*adma
)
455 struct acpi_dma_filter_info
*info
= adma
->data
;
457 if (!info
|| !info
->filter_fn
)
460 return dma_request_channel(info
->dma_cap
, info
->filter_fn
, dma_spec
);
462 EXPORT_SYMBOL_GPL(acpi_dma_simple_xlate
);