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