1
2
3
4
5
6
7
8
9
10
11
12#include <linux/device.h>
13#include <linux/err.h>
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/list.h>
17#include <linux/mutex.h>
18#include <linux/slab.h>
19#include <linux/ioport.h>
20#include <linux/acpi.h>
21#include <linux/acpi_dma.h>
22#include <linux/property.h>
23
24static LIST_HEAD(acpi_dma_list);
25static DEFINE_MUTEX(acpi_dma_lock);
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40static int acpi_dma_parse_resource_group(const struct acpi_csrt_group *grp,
41 struct acpi_device *adev, struct acpi_dma *adma)
42{
43 const struct acpi_csrt_shared_info *si;
44 struct list_head resource_list;
45 struct resource_entry *rentry;
46 resource_size_t mem = 0, irq = 0;
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
67 if (mem == 0 && irq == 0)
68 return 0;
69
70 si = (const struct acpi_csrt_shared_info *)&grp[1];
71
72
73 if (si->mmio_base_low != lower_32_bits(mem) ||
74 si->mmio_base_high != upper_32_bits(mem) ||
75 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
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
97
98
99
100
101
102
103
104
105
106
107
108static 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
140
141
142
143
144
145
146
147
148
149
150
151
152int 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
164 adev = ACPI_COMPANION(dev);
165 if (!adev)
166 return -EINVAL;
167
168 adma = kzalloc(sizeof(*adma), GFP_KERNEL);
169 if (!adma)
170 return -ENOMEM;
171
172 adma->dev = dev;
173 adma->acpi_dma_xlate = acpi_dma_xlate;
174 adma->data = data;
175
176 acpi_dma_parse_csrt(adev, adma);
177
178
179 mutex_lock(&acpi_dma_lock);
180 list_add_tail(&adma->dma_controllers, &acpi_dma_list);
181 mutex_unlock(&acpi_dma_lock);
182
183 return 0;
184}
185EXPORT_SYMBOL_GPL(acpi_dma_controller_register);
186
187
188
189
190
191
192
193
194
195
196int acpi_dma_controller_free(struct device *dev)
197{
198 struct acpi_dma *adma;
199
200 if (!dev)
201 return -EINVAL;
202
203 mutex_lock(&acpi_dma_lock);
204
205 list_for_each_entry(adma, &acpi_dma_list, dma_controllers)
206 if (adma->dev == dev) {
207 list_del(&adma->dma_controllers);
208 mutex_unlock(&acpi_dma_lock);
209 kfree(adma);
210 return 0;
211 }
212
213 mutex_unlock(&acpi_dma_lock);
214 return -ENODEV;
215}
216EXPORT_SYMBOL_GPL(acpi_dma_controller_free);
217
218static void devm_acpi_dma_release(struct device *dev, void *res)
219{
220 acpi_dma_controller_free(dev);
221}
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236int devm_acpi_dma_controller_register(struct device *dev,
237 struct dma_chan *(*acpi_dma_xlate)
238 (struct acpi_dma_spec *, struct acpi_dma *),
239 void *data)
240{
241 void *res;
242 int ret;
243
244 res = devres_alloc(devm_acpi_dma_release, 0, GFP_KERNEL);
245 if (!res)
246 return -ENOMEM;
247
248 ret = acpi_dma_controller_register(dev, acpi_dma_xlate, data);
249 if (ret) {
250 devres_free(res);
251 return ret;
252 }
253 devres_add(dev, res);
254 return 0;
255}
256EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_register);
257
258
259
260
261
262
263
264
265
266void devm_acpi_dma_controller_free(struct device *dev)
267{
268 WARN_ON(devres_release(dev, devm_acpi_dma_release, NULL, NULL));
269}
270EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_free);
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288static int acpi_dma_update_dma_spec(struct acpi_dma *adma,
289 struct acpi_dma_spec *dma_spec)
290{
291
292 dma_spec->dev = adma->dev;
293
294
295 if (adma->base_request_line == 0 && adma->end_request_line == 0)
296 return 0;
297
298
299 if (dma_spec->slave_id < adma->base_request_line ||
300 dma_spec->slave_id > adma->end_request_line)
301 return -1;
302
303
304
305
306
307 dma_spec->slave_id -= adma->base_request_line;
308
309 return 1;
310}
311
312struct acpi_dma_parser_data {
313 struct acpi_dma_spec dma_spec;
314 size_t index;
315 size_t n;
316};
317
318
319
320
321
322
323static int acpi_dma_parse_fixed_dma(struct acpi_resource *res, void *data)
324{
325 struct acpi_dma_parser_data *pdata = data;
326
327 if (res->type == ACPI_RESOURCE_TYPE_FIXED_DMA) {
328 struct acpi_resource_fixed_dma *dma = &res->data.fixed_dma;
329
330 if (pdata->n++ == pdata->index) {
331 pdata->dma_spec.chan_id = dma->channels;
332 pdata->dma_spec.slave_id = dma->request_lines;
333 }
334 }
335
336
337 return 1;
338}
339
340
341
342
343
344
345
346
347
348struct dma_chan *acpi_dma_request_slave_chan_by_index(struct device *dev,
349 size_t index)
350{
351 struct acpi_dma_parser_data pdata;
352 struct acpi_dma_spec *dma_spec = &pdata.dma_spec;
353 struct list_head resource_list;
354 struct acpi_device *adev;
355 struct acpi_dma *adma;
356 struct dma_chan *chan = NULL;
357 int found;
358
359
360 if (!dev)
361 return ERR_PTR(-ENODEV);
362
363 adev = ACPI_COMPANION(dev);
364 if (!adev)
365 return ERR_PTR(-ENODEV);
366
367 memset(&pdata, 0, sizeof(pdata));
368 pdata.index = index;
369
370
371 dma_spec->chan_id = -1;
372 dma_spec->slave_id = -1;
373
374 INIT_LIST_HEAD(&resource_list);
375 acpi_dev_get_resources(adev, &resource_list,
376 acpi_dma_parse_fixed_dma, &pdata);
377 acpi_dev_free_resource_list(&resource_list);
378
379 if (dma_spec->slave_id < 0 || dma_spec->chan_id < 0)
380 return ERR_PTR(-ENODEV);
381
382 mutex_lock(&acpi_dma_lock);
383
384 list_for_each_entry(adma, &acpi_dma_list, dma_controllers) {
385
386
387
388
389 found = acpi_dma_update_dma_spec(adma, dma_spec);
390 if (found < 0)
391 continue;
392 chan = adma->acpi_dma_xlate(dma_spec, adma);
393
394
395
396
397
398 if (found > 0 || chan)
399 break;
400 }
401
402 mutex_unlock(&acpi_dma_lock);
403 return chan ? chan : ERR_PTR(-EPROBE_DEFER);
404}
405EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_index);
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423struct dma_chan *acpi_dma_request_slave_chan_by_name(struct device *dev,
424 const char *name)
425{
426 int index;
427
428 index = device_property_match_string(dev, "dma-names", name);
429 if (index < 0) {
430 if (!strcmp(name, "tx"))
431 index = 0;
432 else if (!strcmp(name, "rx"))
433 index = 1;
434 else
435 return ERR_PTR(-ENODEV);
436 }
437
438 dev_dbg(dev, "Looking for DMA channel \"%s\" at index %d...\n", name, index);
439 return acpi_dma_request_slave_chan_by_index(dev, index);
440}
441EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_name);
442
443
444
445
446
447
448
449
450
451
452
453
454struct dma_chan *acpi_dma_simple_xlate(struct acpi_dma_spec *dma_spec,
455 struct acpi_dma *adma)
456{
457 struct acpi_dma_filter_info *info = adma->data;
458
459 if (!info || !info->filter_fn)
460 return NULL;
461
462 return dma_request_channel(info->dma_cap, info->filter_fn, dma_spec);
463}
464EXPORT_SYMBOL_GPL(acpi_dma_simple_xlate);
465