blob: 04257432c482c7b2d0bd6bdadefaff0d1323050d [file] [log] [blame]
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +03001/*
2 * ACPI helpers for DMA request / controller
3 *
4 * Based on of-dma.c
5 *
6 * Copyright (C) 2013, Intel Corporation
Andy Shevchenkoee8209f2013-05-08 11:55:48 +03007 * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
8 * Mika Westerberg <mika.westerberg@linux.intel.com>
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +03009 *
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>
Andy Shevchenko0f6a9282014-02-06 13:25:40 +020016#include <linux/err.h>
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +030017#include <linux/module.h>
18#include <linux/list.h>
19#include <linux/mutex.h>
20#include <linux/slab.h>
Andy Shevchenkoee8209f2013-05-08 11:55:48 +030021#include <linux/ioport.h>
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +030022#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/**
Andy Shevchenkoee8209f2013-05-08 11:55:48 +030029 * 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 Shevchenkoee8209f2013-05-08 11:55:48 +030034 * In order to match a device from DSDT table to the corresponding CSRT device
35 * we use MMIO address and IRQ.
Andy Shevchenko39d14472013-12-02 15:16:28 +020036 *
37 * Return:
38 * 1 on success, 0 when no information is available, or appropriate errno value
39 * on error.
Andy Shevchenkoee8209f2013-05-08 11:55:48 +030040 */
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;
Jiang Liu90e97822015-02-05 13:44:43 +080046 struct resource_entry *rentry;
Andy Shevchenkoee8209f2013-05-08 11:55:48 +030047 resource_size_t mem = 0, irq = 0;
Andy Shevchenkoee8209f2013-05-08 11:55:48 +030048 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 Liu90e97822015-02-05 13:44:43 +080059 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 Shevchenkoee8209f2013-05-08 11:55:48 +030063 }
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
Andy Shevchenkoee8209f2013-05-08 11:55:48 +030077 dev_dbg(&adev->dev, "matches with %.4s%04X (rev %u)\n",
Andy Shevchenkob4d6d332013-08-21 14:27:06 +030078 (char *)&grp->vendor_id, grp->device_id, grp->revision);
Andy Shevchenkoee8209f2013-05-08 11:55:48 +030079
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.
Andy Shevchenkoee8209f2013-05-08 11:55:48 +0300106 */
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/**
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300138 * 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 *
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300145 * Allocated memory should be freed with appropriate acpi_dma_controller_free()
146 * call.
Andy Shevchenko39d14472013-12-02 15:16:28 +0200147 *
148 * Return:
149 * 0 on success or appropriate errno value on error.
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300150 */
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 */
Jarkko Nikulaaff1e0c2015-09-04 16:12:30 +0300163 adev = ACPI_COMPANION(dev);
164 if (!adev)
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300165 return -EINVAL;
166
167 adma = kzalloc(sizeof(*adma), GFP_KERNEL);
168 if (!adma)
169 return -ENOMEM;
170
171 adma->dev = dev;
172 adma->acpi_dma_xlate = acpi_dma_xlate;
173 adma->data = data;
174
Andy Shevchenkoee8209f2013-05-08 11:55:48 +0300175 acpi_dma_parse_csrt(adev, adma);
176
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300177 /* 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);
181
182 return 0;
183}
184EXPORT_SYMBOL_GPL(acpi_dma_controller_register);
185
186/**
187 * acpi_dma_controller_free - Remove a DMA controller from ACPI DMA helpers list
188 * @dev: struct device of DMA controller
189 *
190 * Memory allocated by acpi_dma_controller_register() is freed here.
Andy Shevchenko39d14472013-12-02 15:16:28 +0200191 *
192 * Return:
193 * 0 on success or appropriate errno value on error.
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300194 */
195int acpi_dma_controller_free(struct device *dev)
196{
197 struct acpi_dma *adma;
198
199 if (!dev)
200 return -EINVAL;
201
202 mutex_lock(&acpi_dma_lock);
203
204 list_for_each_entry(adma, &acpi_dma_list, dma_controllers)
205 if (adma->dev == dev) {
206 list_del(&adma->dma_controllers);
207 mutex_unlock(&acpi_dma_lock);
208 kfree(adma);
209 return 0;
210 }
211
212 mutex_unlock(&acpi_dma_lock);
213 return -ENODEV;
214}
215EXPORT_SYMBOL_GPL(acpi_dma_controller_free);
216
217static void devm_acpi_dma_release(struct device *dev, void *res)
218{
219 acpi_dma_controller_free(dev);
220}
221
222/**
223 * devm_acpi_dma_controller_register - resource managed acpi_dma_controller_register()
224 * @dev: device that is registering this DMA controller
225 * @acpi_dma_xlate: translation function
226 * @data pointer to controller specific data
227 *
228 * Managed acpi_dma_controller_register(). DMA controller registered by this
229 * function are automatically freed on driver detach. See
230 * acpi_dma_controller_register() for more information.
Andy Shevchenko39d14472013-12-02 15:16:28 +0200231 *
232 * Return:
233 * 0 on success or appropriate errno value on error.
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300234 */
235int devm_acpi_dma_controller_register(struct device *dev,
236 struct dma_chan *(*acpi_dma_xlate)
237 (struct acpi_dma_spec *, struct acpi_dma *),
238 void *data)
239{
240 void *res;
241 int ret;
242
243 res = devres_alloc(devm_acpi_dma_release, 0, GFP_KERNEL);
244 if (!res)
245 return -ENOMEM;
246
247 ret = acpi_dma_controller_register(dev, acpi_dma_xlate, data);
248 if (ret) {
249 devres_free(res);
250 return ret;
251 }
252 devres_add(dev, res);
253 return 0;
254}
255EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_register);
256
257/**
258 * devm_acpi_dma_controller_free - resource managed acpi_dma_controller_free()
259 *
260 * Unregister a DMA controller registered with
261 * devm_acpi_dma_controller_register(). Normally this function will not need to
262 * be called and the resource management code will ensure that the resource is
263 * freed.
264 */
265void devm_acpi_dma_controller_free(struct device *dev)
266{
Andy Shevchenko8f012582014-02-06 13:25:39 +0200267 WARN_ON(devres_release(dev, devm_acpi_dma_release, NULL, NULL));
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300268}
269EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_free);
270
Andy Shevchenkoee8209f2013-05-08 11:55:48 +0300271/**
272 * acpi_dma_update_dma_spec - prepare dma specifier to pass to translation function
273 * @adma: struct acpi_dma of DMA controller
274 * @dma_spec: dma specifier to update
275 *
Andy Shevchenkoee8209f2013-05-08 11:55:48 +0300276 * Accordingly to ACPI 5.0 Specification Table 6-170 "Fixed DMA Resource
277 * Descriptor":
278 * DMA Request Line bits is a platform-relative number uniquely
279 * identifying the request line assigned. Request line-to-Controller
280 * mapping is done in a controller-specific OS driver.
281 * That's why we can safely adjust slave_id when the appropriate controller is
282 * found.
Andy Shevchenko39d14472013-12-02 15:16:28 +0200283 *
284 * Return:
285 * 0, if no information is avaiable, -1 on mismatch, and 1 otherwise.
Andy Shevchenkoee8209f2013-05-08 11:55:48 +0300286 */
287static int acpi_dma_update_dma_spec(struct acpi_dma *adma,
288 struct acpi_dma_spec *dma_spec)
289{
290 /* Set link to the DMA controller device */
291 dma_spec->dev = adma->dev;
292
293 /* Check if the request line range is available */
294 if (adma->base_request_line == 0 && adma->end_request_line == 0)
295 return 0;
296
297 /* Check if slave_id falls to the range */
298 if (dma_spec->slave_id < adma->base_request_line ||
299 dma_spec->slave_id > adma->end_request_line)
300 return -1;
301
302 /*
303 * Here we adjust slave_id. It should be a relative number to the base
304 * request line.
305 */
306 dma_spec->slave_id -= adma->base_request_line;
307
308 return 1;
309}
310
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300311struct acpi_dma_parser_data {
312 struct acpi_dma_spec dma_spec;
313 size_t index;
314 size_t n;
315};
316
317/**
318 * acpi_dma_parse_fixed_dma - Parse FixedDMA ACPI resources to a DMA specifier
319 * @res: struct acpi_resource to get FixedDMA resources from
320 * @data: pointer to a helper struct acpi_dma_parser_data
321 */
322static int acpi_dma_parse_fixed_dma(struct acpi_resource *res, void *data)
323{
324 struct acpi_dma_parser_data *pdata = data;
325
326 if (res->type == ACPI_RESOURCE_TYPE_FIXED_DMA) {
327 struct acpi_resource_fixed_dma *dma = &res->data.fixed_dma;
328
329 if (pdata->n++ == pdata->index) {
330 pdata->dma_spec.chan_id = dma->channels;
331 pdata->dma_spec.slave_id = dma->request_lines;
332 }
333 }
334
335 /* Tell the ACPI core to skip this resource */
336 return 1;
337}
338
339/**
340 * acpi_dma_request_slave_chan_by_index - Get the DMA slave channel
341 * @dev: struct device to get DMA request from
342 * @index: index of FixedDMA descriptor for @dev
343 *
Andy Shevchenko39d14472013-12-02 15:16:28 +0200344 * Return:
Andy Shevchenko0f6a9282014-02-06 13:25:40 +0200345 * Pointer to appropriate dma channel on success or an error pointer.
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300346 */
347struct dma_chan *acpi_dma_request_slave_chan_by_index(struct device *dev,
348 size_t index)
349{
350 struct acpi_dma_parser_data pdata;
351 struct acpi_dma_spec *dma_spec = &pdata.dma_spec;
352 struct list_head resource_list;
353 struct acpi_device *adev;
354 struct acpi_dma *adma;
355 struct dma_chan *chan = NULL;
Andy Shevchenkoee8209f2013-05-08 11:55:48 +0300356 int found;
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300357
358 /* Check if the device was enumerated by ACPI */
Jarkko Nikulaaff1e0c2015-09-04 16:12:30 +0300359 if (!dev)
Andy Shevchenko0f6a9282014-02-06 13:25:40 +0200360 return ERR_PTR(-ENODEV);
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300361
Jarkko Nikulaaff1e0c2015-09-04 16:12:30 +0300362 adev = ACPI_COMPANION(dev);
363 if (!adev)
Andy Shevchenko0f6a9282014-02-06 13:25:40 +0200364 return ERR_PTR(-ENODEV);
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300365
366 memset(&pdata, 0, sizeof(pdata));
367 pdata.index = index;
368
369 /* Initial values for the request line and channel */
370 dma_spec->chan_id = -1;
371 dma_spec->slave_id = -1;
372
373 INIT_LIST_HEAD(&resource_list);
374 acpi_dev_get_resources(adev, &resource_list,
375 acpi_dma_parse_fixed_dma, &pdata);
376 acpi_dev_free_resource_list(&resource_list);
377
378 if (dma_spec->slave_id < 0 || dma_spec->chan_id < 0)
Andy Shevchenko0f6a9282014-02-06 13:25:40 +0200379 return ERR_PTR(-ENODEV);
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300380
381 mutex_lock(&acpi_dma_lock);
382
383 list_for_each_entry(adma, &acpi_dma_list, dma_controllers) {
Andy Shevchenkoee8209f2013-05-08 11:55:48 +0300384 /*
385 * We are not going to call translation function if slave_id
386 * doesn't fall to the request range.
387 */
388 found = acpi_dma_update_dma_spec(adma, dma_spec);
389 if (found < 0)
390 continue;
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300391 chan = adma->acpi_dma_xlate(dma_spec, adma);
Andy Shevchenkoee8209f2013-05-08 11:55:48 +0300392 /*
393 * Try to get a channel only from the DMA controller that
394 * matches the slave_id. See acpi_dma_update_dma_spec()
395 * description for the details.
396 */
397 if (found > 0 || chan)
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300398 break;
399 }
400
401 mutex_unlock(&acpi_dma_lock);
Andy Shevchenko0f6a9282014-02-06 13:25:40 +0200402 return chan ? chan : ERR_PTR(-EPROBE_DEFER);
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300403}
404EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_index);
405
406/**
407 * acpi_dma_request_slave_chan_by_name - Get the DMA slave channel
408 * @dev: struct device to get DMA request from
409 * @name: represents corresponding FixedDMA descriptor for @dev
410 *
411 * In order to support both Device Tree and ACPI in a single driver we
412 * translate the names "tx" and "rx" here based on the most common case where
413 * the first FixedDMA descriptor is TX and second is RX.
414 *
Andy Shevchenko39d14472013-12-02 15:16:28 +0200415 * Return:
Andy Shevchenko0f6a9282014-02-06 13:25:40 +0200416 * Pointer to appropriate dma channel on success or an error pointer.
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300417 */
418struct dma_chan *acpi_dma_request_slave_chan_by_name(struct device *dev,
419 const char *name)
420{
421 size_t index;
422
423 if (!strcmp(name, "tx"))
424 index = 0;
425 else if (!strcmp(name, "rx"))
426 index = 1;
427 else
Andy Shevchenko0f6a9282014-02-06 13:25:40 +0200428 return ERR_PTR(-ENODEV);
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300429
430 return acpi_dma_request_slave_chan_by_index(dev, index);
431}
432EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_name);
433
434/**
435 * acpi_dma_simple_xlate - Simple ACPI DMA engine translation helper
436 * @dma_spec: pointer to ACPI DMA specifier
437 * @adma: pointer to ACPI DMA controller data
438 *
439 * A simple translation function for ACPI based devices. Passes &struct
Andy Shevchenko39d14472013-12-02 15:16:28 +0200440 * dma_spec to the DMA controller driver provided filter function.
441 *
442 * Return:
443 * Pointer to the channel if found or %NULL otherwise.
Andy Shevchenko1b2e98b2013-04-09 14:05:43 +0300444 */
445struct dma_chan *acpi_dma_simple_xlate(struct acpi_dma_spec *dma_spec,
446 struct acpi_dma *adma)
447{
448 struct acpi_dma_filter_info *info = adma->data;
449
450 if (!info || !info->filter_fn)
451 return NULL;
452
453 return dma_request_channel(info->dma_cap, info->filter_fn, dma_spec);
454}
455EXPORT_SYMBOL_GPL(acpi_dma_simple_xlate);