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/err.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/list.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/ioport.h>
23 #include <linux/acpi.h>
24 #include <linux/acpi_dma.h>
25 #include <linux/property.h>
27 static LIST_HEAD(acpi_dma_list);
28 static DEFINE_MUTEX(acpi_dma_lock);
31 * acpi_dma_parse_resource_group - match device and parse resource group
32 * @grp: CSRT resource group
33 * @adev: ACPI device to match with
34 * @adma: struct acpi_dma of the given DMA controller
36 * In order to match a device from DSDT table to the corresponding CSRT device
37 * we use MMIO address and IRQ.
40 * 1 on success, 0 when no information is available, or appropriate errno value
43 static int acpi_dma_parse_resource_group(const struct acpi_csrt_group *grp,
44 struct acpi_device *adev, struct acpi_dma *adma)
46 const struct acpi_csrt_shared_info *si;
47 struct list_head resource_list;
48 struct resource_entry *rentry;
49 resource_size_t mem = 0, irq = 0;
52 if (grp->shared_info_length != sizeof(struct acpi_csrt_shared_info))
55 INIT_LIST_HEAD(&resource_list);
56 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
60 list_for_each_entry(rentry, &resource_list, node) {
61 if (resource_type(rentry->res) == IORESOURCE_MEM)
62 mem = rentry->res->start;
63 else if (resource_type(rentry->res) == IORESOURCE_IRQ)
64 irq = rentry->res->start;
67 acpi_dev_free_resource_list(&resource_list);
69 /* Consider initial zero values as resource not found */
70 if (mem == 0 && irq == 0)
73 si = (const struct acpi_csrt_shared_info *)&grp[1];
75 /* Match device by MMIO */
76 if (si->mmio_base_low != lower_32_bits(mem) ||
77 si->mmio_base_high != upper_32_bits(mem))
80 /* Match device by Linux vIRQ */
81 ret = acpi_register_gsi(NULL, si->gsi_interrupt, si->interrupt_mode, si->interrupt_polarity);
85 dev_dbg(&adev->dev, "matches with %.4s%04X (rev %u)\n",
86 (char *)&grp->vendor_id, grp->device_id, grp->revision);
88 /* Check if the request line range is available */
89 if (si->base_request_line == 0 && si->num_handshake_signals == 0)
92 adma->base_request_line = si->base_request_line;
93 adma->end_request_line = si->base_request_line +
94 si->num_handshake_signals - 1;
96 dev_dbg(&adev->dev, "request line base: 0x%04x end: 0x%04x\n",
97 adma->base_request_line, adma->end_request_line);
103 * acpi_dma_parse_csrt - parse CSRT to exctract additional DMA resources
104 * @adev: ACPI device to match with
105 * @adma: struct acpi_dma of the given DMA controller
107 * CSRT or Core System Resources Table is a proprietary ACPI table
108 * introduced by Microsoft. This table can contain devices that are not in
109 * the system DSDT table. In particular DMA controllers might be described
112 * We are using this table to get the request line range of the specific DMA
113 * controller to be used later.
115 static void acpi_dma_parse_csrt(struct acpi_device *adev, struct acpi_dma *adma)
117 struct acpi_csrt_group *grp, *end;
118 struct acpi_table_csrt *csrt;
122 status = acpi_get_table(ACPI_SIG_CSRT, 0,
123 (struct acpi_table_header **)&csrt);
124 if (ACPI_FAILURE(status)) {
125 if (status != AE_NOT_FOUND)
126 dev_warn(&adev->dev, "failed to get the CSRT table\n");
130 grp = (struct acpi_csrt_group *)(csrt + 1);
131 end = (struct acpi_csrt_group *)((void *)csrt + csrt->header.length);
134 ret = acpi_dma_parse_resource_group(grp, adev, adma);
137 "error in parsing resource group\n");
141 grp = (struct acpi_csrt_group *)((void *)grp + grp->length);
144 acpi_put_table((struct acpi_table_header *)csrt);
148 * acpi_dma_controller_register - Register a DMA controller to ACPI DMA helpers
149 * @dev: struct device of DMA controller
150 * @acpi_dma_xlate: translation function which converts a dma specifier
151 * into a dma_chan structure
152 * @data pointer to controller specific data to be used by
153 * translation function
155 * Allocated memory should be freed with appropriate acpi_dma_controller_free()
159 * 0 on success or appropriate errno value on error.
161 int acpi_dma_controller_register(struct device *dev,
162 struct dma_chan *(*acpi_dma_xlate)
163 (struct acpi_dma_spec *, struct acpi_dma *),
166 struct acpi_device *adev;
167 struct acpi_dma *adma;
169 if (!dev || !acpi_dma_xlate)
172 /* Check if the device was enumerated by ACPI */
173 adev = ACPI_COMPANION(dev);
177 adma = kzalloc(sizeof(*adma), GFP_KERNEL);
182 adma->acpi_dma_xlate = acpi_dma_xlate;
185 acpi_dma_parse_csrt(adev, adma);
187 /* Now queue acpi_dma controller structure in list */
188 mutex_lock(&acpi_dma_lock);
189 list_add_tail(&adma->dma_controllers, &acpi_dma_list);
190 mutex_unlock(&acpi_dma_lock);
194 EXPORT_SYMBOL_GPL(acpi_dma_controller_register);
197 * acpi_dma_controller_free - Remove a DMA controller from ACPI DMA helpers list
198 * @dev: struct device of DMA controller
200 * Memory allocated by acpi_dma_controller_register() is freed here.
203 * 0 on success or appropriate errno value on error.
205 int acpi_dma_controller_free(struct device *dev)
207 struct acpi_dma *adma;
212 mutex_lock(&acpi_dma_lock);
214 list_for_each_entry(adma, &acpi_dma_list, dma_controllers)
215 if (adma->dev == dev) {
216 list_del(&adma->dma_controllers);
217 mutex_unlock(&acpi_dma_lock);
222 mutex_unlock(&acpi_dma_lock);
225 EXPORT_SYMBOL_GPL(acpi_dma_controller_free);
227 static void devm_acpi_dma_release(struct device *dev, void *res)
229 acpi_dma_controller_free(dev);
233 * devm_acpi_dma_controller_register - resource managed acpi_dma_controller_register()
234 * @dev: device that is registering this DMA controller
235 * @acpi_dma_xlate: translation function
236 * @data pointer to controller specific data
238 * Managed acpi_dma_controller_register(). DMA controller registered by this
239 * function are automatically freed on driver detach. See
240 * acpi_dma_controller_register() for more information.
243 * 0 on success or appropriate errno value on error.
245 int devm_acpi_dma_controller_register(struct device *dev,
246 struct dma_chan *(*acpi_dma_xlate)
247 (struct acpi_dma_spec *, struct acpi_dma *),
253 res = devres_alloc(devm_acpi_dma_release, 0, GFP_KERNEL);
257 ret = acpi_dma_controller_register(dev, acpi_dma_xlate, data);
262 devres_add(dev, res);
265 EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_register);
268 * devm_acpi_dma_controller_free - resource managed acpi_dma_controller_free()
270 * Unregister a DMA controller registered with
271 * devm_acpi_dma_controller_register(). Normally this function will not need to
272 * be called and the resource management code will ensure that the resource is
275 void devm_acpi_dma_controller_free(struct device *dev)
277 WARN_ON(devres_release(dev, devm_acpi_dma_release, NULL, NULL));
279 EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_free);
282 * acpi_dma_update_dma_spec - prepare dma specifier to pass to translation function
283 * @adma: struct acpi_dma of DMA controller
284 * @dma_spec: dma specifier to update
286 * Accordingly to ACPI 5.0 Specification Table 6-170 "Fixed DMA Resource
288 * DMA Request Line bits is a platform-relative number uniquely
289 * identifying the request line assigned. Request line-to-Controller
290 * mapping is done in a controller-specific OS driver.
291 * That's why we can safely adjust slave_id when the appropriate controller is
295 * 0, if no information is avaiable, -1 on mismatch, and 1 otherwise.
297 static int acpi_dma_update_dma_spec(struct acpi_dma *adma,
298 struct acpi_dma_spec *dma_spec)
300 /* Set link to the DMA controller device */
301 dma_spec->dev = adma->dev;
303 /* Check if the request line range is available */
304 if (adma->base_request_line == 0 && adma->end_request_line == 0)
307 /* Check if slave_id falls to the range */
308 if (dma_spec->slave_id < adma->base_request_line ||
309 dma_spec->slave_id > adma->end_request_line)
313 * Here we adjust slave_id. It should be a relative number to the base
316 dma_spec->slave_id -= adma->base_request_line;
321 struct acpi_dma_parser_data {
322 struct acpi_dma_spec dma_spec;
328 * acpi_dma_parse_fixed_dma - Parse FixedDMA ACPI resources to a DMA specifier
329 * @res: struct acpi_resource to get FixedDMA resources from
330 * @data: pointer to a helper struct acpi_dma_parser_data
332 static int acpi_dma_parse_fixed_dma(struct acpi_resource *res, void *data)
334 struct acpi_dma_parser_data *pdata = data;
336 if (res->type == ACPI_RESOURCE_TYPE_FIXED_DMA) {
337 struct acpi_resource_fixed_dma *dma = &res->data.fixed_dma;
339 if (pdata->n++ == pdata->index) {
340 pdata->dma_spec.chan_id = dma->channels;
341 pdata->dma_spec.slave_id = dma->request_lines;
345 /* Tell the ACPI core to skip this resource */
350 * acpi_dma_request_slave_chan_by_index - Get the DMA slave channel
351 * @dev: struct device to get DMA request from
352 * @index: index of FixedDMA descriptor for @dev
355 * Pointer to appropriate dma channel on success or an error pointer.
357 struct dma_chan *acpi_dma_request_slave_chan_by_index(struct device *dev,
360 struct acpi_dma_parser_data pdata;
361 struct acpi_dma_spec *dma_spec = &pdata.dma_spec;
362 struct list_head resource_list;
363 struct acpi_device *adev;
364 struct acpi_dma *adma;
365 struct dma_chan *chan = NULL;
368 /* Check if the device was enumerated by ACPI */
370 return ERR_PTR(-ENODEV);
372 adev = ACPI_COMPANION(dev);
374 return ERR_PTR(-ENODEV);
376 memset(&pdata, 0, sizeof(pdata));
379 /* Initial values for the request line and channel */
380 dma_spec->chan_id = -1;
381 dma_spec->slave_id = -1;
383 INIT_LIST_HEAD(&resource_list);
384 acpi_dev_get_resources(adev, &resource_list,
385 acpi_dma_parse_fixed_dma, &pdata);
386 acpi_dev_free_resource_list(&resource_list);
388 if (dma_spec->slave_id < 0 || dma_spec->chan_id < 0)
389 return ERR_PTR(-ENODEV);
391 mutex_lock(&acpi_dma_lock);
393 list_for_each_entry(adma, &acpi_dma_list, dma_controllers) {
395 * We are not going to call translation function if slave_id
396 * doesn't fall to the request range.
398 found = acpi_dma_update_dma_spec(adma, dma_spec);
401 chan = adma->acpi_dma_xlate(dma_spec, adma);
403 * Try to get a channel only from the DMA controller that
404 * matches the slave_id. See acpi_dma_update_dma_spec()
405 * description for the details.
407 if (found > 0 || chan)
411 mutex_unlock(&acpi_dma_lock);
412 return chan ? chan : ERR_PTR(-EPROBE_DEFER);
414 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_index);
417 * acpi_dma_request_slave_chan_by_name - Get the DMA slave channel
418 * @dev: struct device to get DMA request from
419 * @name: represents corresponding FixedDMA descriptor for @dev
421 * In order to support both Device Tree and ACPI in a single driver we
422 * translate the names "tx" and "rx" here based on the most common case where
423 * the first FixedDMA descriptor is TX and second is RX.
425 * If the device has "dma-names" property the FixedDMA descriptor indices
426 * are retrieved based on those. Otherwise the function falls back using
430 * Pointer to appropriate dma channel on success or an error pointer.
432 struct dma_chan *acpi_dma_request_slave_chan_by_name(struct device *dev,
437 index = device_property_match_string(dev, "dma-names", name);
439 if (!strcmp(name, "tx"))
441 else if (!strcmp(name, "rx"))
444 return ERR_PTR(-ENODEV);
447 dev_dbg(dev, "Looking for DMA channel \"%s\" at index %d...\n", name, index);
448 return acpi_dma_request_slave_chan_by_index(dev, index);
450 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_name);
453 * acpi_dma_simple_xlate - Simple ACPI DMA engine translation helper
454 * @dma_spec: pointer to ACPI DMA specifier
455 * @adma: pointer to ACPI DMA controller data
457 * A simple translation function for ACPI based devices. Passes &struct
458 * dma_spec to the DMA controller driver provided filter function.
461 * Pointer to the channel if found or %NULL otherwise.
463 struct dma_chan *acpi_dma_simple_xlate(struct acpi_dma_spec *dma_spec,
464 struct acpi_dma *adma)
466 struct acpi_dma_filter_info *info = adma->data;
468 if (!info || !info->filter_fn)
471 return dma_request_channel(info->dma_cap, info->filter_fn, dma_spec);
473 EXPORT_SYMBOL_GPL(acpi_dma_simple_xlate);