1
2
3
4
5
6
7
8
9
10
11
12
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
25static LIST_HEAD(acpi_dma_list);
26static DEFINE_MUTEX(acpi_dma_lock);
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
46 struct resource_list_entry *rentry;
47 resource_size_t mem = 0, irq = 0;
48 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) {
59 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;
63 }
64
65 acpi_dev_free_resource_list(&resource_list);
66
67
68 if (mem == 0 && irq == 0)
69 return 0;
70
71 si = (const struct acpi_csrt_shared_info *)&grp[1];
72
73
74 if (si->mmio_base_low != mem || si->gsi_interrupt != irq)
75 return 0;
76
77 dev_dbg(&adev->dev, "matches with %.4s%04X (rev %u)\n",
78 (char *)&grp->vendor_id, grp->device_id, grp->revision);
79
80
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
96
97
98
99
100
101
102
103
104
105
106
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
138
139
140
141
142
143
144
145
146
147
148
149
150
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
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
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}
186EXPORT_SYMBOL_GPL(acpi_dma_controller_register);
187
188
189
190
191
192
193
194
195
196
197int acpi_dma_controller_free(struct device *dev)
198{
199 struct acpi_dma *adma;
200
201 if (!dev)
202 return -EINVAL;
203
204 mutex_lock(&acpi_dma_lock);
205
206 list_for_each_entry(adma, &acpi_dma_list, dma_controllers)
207 if (adma->dev == dev) {
208 list_del(&adma->dma_controllers);
209 mutex_unlock(&acpi_dma_lock);
210 kfree(adma);
211 return 0;
212 }
213
214 mutex_unlock(&acpi_dma_lock);
215 return -ENODEV;
216}
217EXPORT_SYMBOL_GPL(acpi_dma_controller_free);
218
219static void devm_acpi_dma_release(struct device *dev, void *res)
220{
221 acpi_dma_controller_free(dev);
222}
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237int devm_acpi_dma_controller_register(struct device *dev,
238 struct dma_chan *(*acpi_dma_xlate)
239 (struct acpi_dma_spec *, struct acpi_dma *),
240 void *data)
241{
242 void *res;
243 int ret;
244
245 res = devres_alloc(devm_acpi_dma_release, 0, GFP_KERNEL);
246 if (!res)
247 return -ENOMEM;
248
249 ret = acpi_dma_controller_register(dev, acpi_dma_xlate, data);
250 if (ret) {
251 devres_free(res);
252 return ret;
253 }
254 devres_add(dev, res);
255 return 0;
256}
257EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_register);
258
259
260
261
262
263
264
265
266
267void devm_acpi_dma_controller_free(struct device *dev)
268{
269 WARN_ON(devres_release(dev, devm_acpi_dma_release, NULL, NULL));
270}
271EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_free);
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289static int acpi_dma_update_dma_spec(struct acpi_dma *adma,
290 struct acpi_dma_spec *dma_spec)
291{
292
293 dma_spec->dev = adma->dev;
294
295
296 if (adma->base_request_line == 0 && adma->end_request_line == 0)
297 return 0;
298
299
300 if (dma_spec->slave_id < adma->base_request_line ||
301 dma_spec->slave_id > adma->end_request_line)
302 return -1;
303
304
305
306
307
308 dma_spec->slave_id -= adma->base_request_line;
309
310 return 1;
311}
312
313struct acpi_dma_parser_data {
314 struct acpi_dma_spec dma_spec;
315 size_t index;
316 size_t n;
317};
318
319
320
321
322
323
324static int acpi_dma_parse_fixed_dma(struct acpi_resource *res, void *data)
325{
326 struct acpi_dma_parser_data *pdata = data;
327
328 if (res->type == ACPI_RESOURCE_TYPE_FIXED_DMA) {
329 struct acpi_resource_fixed_dma *dma = &res->data.fixed_dma;
330
331 if (pdata->n++ == pdata->index) {
332 pdata->dma_spec.chan_id = dma->channels;
333 pdata->dma_spec.slave_id = dma->request_lines;
334 }
335 }
336
337
338 return 1;
339}
340
341
342
343
344
345
346
347
348
349struct dma_chan *acpi_dma_request_slave_chan_by_index(struct device *dev,
350 size_t index)
351{
352 struct acpi_dma_parser_data pdata;
353 struct acpi_dma_spec *dma_spec = &pdata.dma_spec;
354 struct list_head resource_list;
355 struct acpi_device *adev;
356 struct acpi_dma *adma;
357 struct dma_chan *chan = NULL;
358 int found;
359
360
361 if (!dev || !ACPI_HANDLE(dev))
362 return ERR_PTR(-ENODEV);
363
364 if (acpi_bus_get_device(ACPI_HANDLE(dev), &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
419struct dma_chan *acpi_dma_request_slave_chan_by_name(struct device *dev,
420 const char *name)
421{
422 size_t index;
423
424 if (!strcmp(name, "tx"))
425 index = 0;
426 else if (!strcmp(name, "rx"))
427 index = 1;
428 else
429 return ERR_PTR(-ENODEV);
430
431 return acpi_dma_request_slave_chan_by_index(dev, index);
432}
433EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_name);
434
435
436
437
438
439
440
441
442
443
444
445
446struct dma_chan *acpi_dma_simple_xlate(struct acpi_dma_spec *dma_spec,
447 struct acpi_dma *adma)
448{
449 struct acpi_dma_filter_info *info = adma->data;
450
451 if (!info || !info->filter_fn)
452 return NULL;
453
454 return dma_request_channel(info->dma_cap, info->filter_fn, dma_spec);
455}
456EXPORT_SYMBOL_GPL(acpi_dma_simple_xlate);
457