Merge remote-tracking branch 'regmap/fix/cache' into regmap-linus
[firefly-linux-kernel-4.4.55.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/module.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
20 #include <linux/ioport.h>
21 #include <linux/acpi.h>
22 #include <linux/acpi_dma.h>
23
24 static LIST_HEAD(acpi_dma_list);
25 static DEFINE_MUTEX(acpi_dma_lock);
26
27 /**
28  * acpi_dma_parse_resource_group - match device and parse resource group
29  * @grp:        CSRT resource group
30  * @adev:       ACPI device to match with
31  * @adma:       struct acpi_dma of the given DMA controller
32  *
33  * Returns 1 on success, 0 when no information is available, or appropriate
34  * errno value on error.
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 static int acpi_dma_parse_resource_group(const struct acpi_csrt_group *grp,
40                 struct acpi_device *adev, struct acpi_dma *adma)
41 {
42         const struct acpi_csrt_shared_info *si;
43         struct list_head resource_list;
44         struct resource_list_entry *rentry;
45         resource_size_t mem = 0, irq = 0;
46         u32 vendor_id;
47         int ret;
48
49         if (grp->shared_info_length != sizeof(struct acpi_csrt_shared_info))
50                 return -ENODEV;
51
52         INIT_LIST_HEAD(&resource_list);
53         ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
54         if (ret <= 0)
55                 return 0;
56
57         list_for_each_entry(rentry, &resource_list, node) {
58                 if (resource_type(&rentry->res) == IORESOURCE_MEM)
59                         mem = rentry->res.start;
60                 else if (resource_type(&rentry->res) == IORESOURCE_IRQ)
61                         irq = rentry->res.start;
62         }
63
64         acpi_dev_free_resource_list(&resource_list);
65
66         /* Consider initial zero values as resource not found */
67         if (mem == 0 && irq == 0)
68                 return 0;
69
70         si = (const struct acpi_csrt_shared_info *)&grp[1];
71
72         /* Match device by MMIO and IRQ */
73         if (si->mmio_base_low != mem || si->gsi_interrupt != irq)
74                 return 0;
75
76         vendor_id = le32_to_cpu(grp->vendor_id);
77         dev_dbg(&adev->dev, "matches with %.4s%04X (rev %u)\n",
78                 (char *)&vendor_id, grp->device_id, grp->revision);
79
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.
106  *
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  * Returns 0 on success or appropriate errno value on error.
147  *
148  * Allocated memory should be freed with appropriate acpi_dma_controller_free()
149  * call.
150  */
151 int 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 */
163         if (!ACPI_HANDLE(dev))
164                 return -EINVAL;
165
166         if (acpi_bus_get_device(ACPI_HANDLE(dev), &adev))
167                 return -EINVAL;
168
169         adma = kzalloc(sizeof(*adma), GFP_KERNEL);
170         if (!adma)
171                 return -ENOMEM;
172
173         adma->dev = dev;
174         adma->acpi_dma_xlate = acpi_dma_xlate;
175         adma->data = data;
176
177         acpi_dma_parse_csrt(adev, adma);
178
179         /* Now queue acpi_dma controller structure in list */
180         mutex_lock(&acpi_dma_lock);
181         list_add_tail(&adma->dma_controllers, &acpi_dma_list);
182         mutex_unlock(&acpi_dma_lock);
183
184         return 0;
185 }
186 EXPORT_SYMBOL_GPL(acpi_dma_controller_register);
187
188 /**
189  * acpi_dma_controller_free - Remove a DMA controller from ACPI DMA helpers list
190  * @dev:        struct device of DMA controller
191  *
192  * Memory allocated by acpi_dma_controller_register() is freed here.
193  */
194 int acpi_dma_controller_free(struct device *dev)
195 {
196         struct acpi_dma *adma;
197
198         if (!dev)
199                 return -EINVAL;
200
201         mutex_lock(&acpi_dma_lock);
202
203         list_for_each_entry(adma, &acpi_dma_list, dma_controllers)
204                 if (adma->dev == dev) {
205                         list_del(&adma->dma_controllers);
206                         mutex_unlock(&acpi_dma_lock);
207                         kfree(adma);
208                         return 0;
209                 }
210
211         mutex_unlock(&acpi_dma_lock);
212         return -ENODEV;
213 }
214 EXPORT_SYMBOL_GPL(acpi_dma_controller_free);
215
216 static void devm_acpi_dma_release(struct device *dev, void *res)
217 {
218         acpi_dma_controller_free(dev);
219 }
220
221 /**
222  * devm_acpi_dma_controller_register - resource managed acpi_dma_controller_register()
223  * @dev:                device that is registering this DMA controller
224  * @acpi_dma_xlate:     translation function
225  * @data                pointer to controller specific data
226  *
227  * Managed acpi_dma_controller_register(). DMA controller registered by this
228  * function are automatically freed on driver detach. See
229  * acpi_dma_controller_register() for more information.
230  */
231 int devm_acpi_dma_controller_register(struct device *dev,
232                 struct dma_chan *(*acpi_dma_xlate)
233                 (struct acpi_dma_spec *, struct acpi_dma *),
234                 void *data)
235 {
236         void *res;
237         int ret;
238
239         res = devres_alloc(devm_acpi_dma_release, 0, GFP_KERNEL);
240         if (!res)
241                 return -ENOMEM;
242
243         ret = acpi_dma_controller_register(dev, acpi_dma_xlate, data);
244         if (ret) {
245                 devres_free(res);
246                 return ret;
247         }
248         devres_add(dev, res);
249         return 0;
250 }
251 EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_register);
252
253 /**
254  * devm_acpi_dma_controller_free - resource managed acpi_dma_controller_free()
255  *
256  * Unregister a DMA controller registered with
257  * devm_acpi_dma_controller_register(). Normally this function will not need to
258  * be called and the resource management code will ensure that the resource is
259  * freed.
260  */
261 void devm_acpi_dma_controller_free(struct device *dev)
262 {
263         WARN_ON(devres_destroy(dev, devm_acpi_dma_release, NULL, NULL));
264 }
265 EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_free);
266
267 /**
268  * acpi_dma_update_dma_spec - prepare dma specifier to pass to translation function
269  * @adma:       struct acpi_dma of DMA controller
270  * @dma_spec:   dma specifier to update
271  *
272  * Returns 0, if no information is avaiable, -1 on mismatch, and 1 otherwise.
273  *
274  * Accordingly to ACPI 5.0 Specification Table 6-170 "Fixed DMA Resource
275  * Descriptor":
276  *      DMA Request Line bits is a platform-relative number uniquely
277  *      identifying the request line assigned. Request line-to-Controller
278  *      mapping is done in a controller-specific OS driver.
279  * That's why we can safely adjust slave_id when the appropriate controller is
280  * found.
281  */
282 static int acpi_dma_update_dma_spec(struct acpi_dma *adma,
283                 struct acpi_dma_spec *dma_spec)
284 {
285         /* Set link to the DMA controller device */
286         dma_spec->dev = adma->dev;
287
288         /* Check if the request line range is available */
289         if (adma->base_request_line == 0 && adma->end_request_line == 0)
290                 return 0;
291
292         /* Check if slave_id falls to the range */
293         if (dma_spec->slave_id < adma->base_request_line ||
294             dma_spec->slave_id > adma->end_request_line)
295                 return -1;
296
297         /*
298          * Here we adjust slave_id. It should be a relative number to the base
299          * request line.
300          */
301         dma_spec->slave_id -= adma->base_request_line;
302
303         return 1;
304 }
305
306 struct acpi_dma_parser_data {
307         struct acpi_dma_spec dma_spec;
308         size_t index;
309         size_t n;
310 };
311
312 /**
313  * acpi_dma_parse_fixed_dma - Parse FixedDMA ACPI resources to a DMA specifier
314  * @res:        struct acpi_resource to get FixedDMA resources from
315  * @data:       pointer to a helper struct acpi_dma_parser_data
316  */
317 static int acpi_dma_parse_fixed_dma(struct acpi_resource *res, void *data)
318 {
319         struct acpi_dma_parser_data *pdata = data;
320
321         if (res->type == ACPI_RESOURCE_TYPE_FIXED_DMA) {
322                 struct acpi_resource_fixed_dma *dma = &res->data.fixed_dma;
323
324                 if (pdata->n++ == pdata->index) {
325                         pdata->dma_spec.chan_id = dma->channels;
326                         pdata->dma_spec.slave_id = dma->request_lines;
327                 }
328         }
329
330         /* Tell the ACPI core to skip this resource */
331         return 1;
332 }
333
334 /**
335  * acpi_dma_request_slave_chan_by_index - Get the DMA slave channel
336  * @dev:        struct device to get DMA request from
337  * @index:      index of FixedDMA descriptor for @dev
338  *
339  * Returns pointer to appropriate dma channel on success or NULL on error.
340  */
341 struct dma_chan *acpi_dma_request_slave_chan_by_index(struct device *dev,
342                 size_t index)
343 {
344         struct acpi_dma_parser_data pdata;
345         struct acpi_dma_spec *dma_spec = &pdata.dma_spec;
346         struct list_head resource_list;
347         struct acpi_device *adev;
348         struct acpi_dma *adma;
349         struct dma_chan *chan = NULL;
350         int found;
351
352         /* Check if the device was enumerated by ACPI */
353         if (!dev || !ACPI_HANDLE(dev))
354                 return NULL;
355
356         if (acpi_bus_get_device(ACPI_HANDLE(dev), &adev))
357                 return NULL;
358
359         memset(&pdata, 0, sizeof(pdata));
360         pdata.index = index;
361
362         /* Initial values for the request line and channel */
363         dma_spec->chan_id = -1;
364         dma_spec->slave_id = -1;
365
366         INIT_LIST_HEAD(&resource_list);
367         acpi_dev_get_resources(adev, &resource_list,
368                         acpi_dma_parse_fixed_dma, &pdata);
369         acpi_dev_free_resource_list(&resource_list);
370
371         if (dma_spec->slave_id < 0 || dma_spec->chan_id < 0)
372                 return NULL;
373
374         mutex_lock(&acpi_dma_lock);
375
376         list_for_each_entry(adma, &acpi_dma_list, dma_controllers) {
377                 /*
378                  * We are not going to call translation function if slave_id
379                  * doesn't fall to the request range.
380                  */
381                 found = acpi_dma_update_dma_spec(adma, dma_spec);
382                 if (found < 0)
383                         continue;
384                 chan = adma->acpi_dma_xlate(dma_spec, adma);
385                 /*
386                  * Try to get a channel only from the DMA controller that
387                  * matches the slave_id. See acpi_dma_update_dma_spec()
388                  * description for the details.
389                  */
390                 if (found > 0 || chan)
391                         break;
392         }
393
394         mutex_unlock(&acpi_dma_lock);
395         return chan;
396 }
397 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_index);
398
399 /**
400  * acpi_dma_request_slave_chan_by_name - Get the DMA slave channel
401  * @dev:        struct device to get DMA request from
402  * @name:       represents corresponding FixedDMA descriptor for @dev
403  *
404  * In order to support both Device Tree and ACPI in a single driver we
405  * translate the names "tx" and "rx" here based on the most common case where
406  * the first FixedDMA descriptor is TX and second is RX.
407  *
408  * Returns pointer to appropriate dma channel on success or NULL on error.
409  */
410 struct dma_chan *acpi_dma_request_slave_chan_by_name(struct device *dev,
411                 const char *name)
412 {
413         size_t index;
414
415         if (!strcmp(name, "tx"))
416                 index = 0;
417         else if (!strcmp(name, "rx"))
418                 index = 1;
419         else
420                 return NULL;
421
422         return acpi_dma_request_slave_chan_by_index(dev, index);
423 }
424 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_name);
425
426 /**
427  * acpi_dma_simple_xlate - Simple ACPI DMA engine translation helper
428  * @dma_spec: pointer to ACPI DMA specifier
429  * @adma: pointer to ACPI DMA controller data
430  *
431  * A simple translation function for ACPI based devices. Passes &struct
432  * dma_spec to the DMA controller driver provided filter function. Returns
433  * pointer to the channel if found or %NULL otherwise.
434  */
435 struct dma_chan *acpi_dma_simple_xlate(struct acpi_dma_spec *dma_spec,
436                 struct acpi_dma *adma)
437 {
438         struct acpi_dma_filter_info *info = adma->data;
439
440         if (!info || !info->filter_fn)
441                 return NULL;
442
443         return dma_request_channel(info->dma_cap, info->filter_fn, dma_spec);
444 }
445 EXPORT_SYMBOL_GPL(acpi_dma_simple_xlate);