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