xref: /openbmc/linux/drivers/dma/acpi-dma.c (revision 37be287c)
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  * In order to match a device from DSDT table to the corresponding CSRT device
34  * we use MMIO address and IRQ.
35  *
36  * Return:
37  * 1 on success, 0 when no information is available, or appropriate errno value
38  * on error.
39  */
40 static 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_list_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 	/* 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 	dev_dbg(&adev->dev, "matches with %.4s%04X (rev %u)\n",
77 		(char *)&grp->vendor_id, grp->device_id, grp->revision);
78 
79 	/* Check if the request line range is available */
80 	if (si->base_request_line == 0 && si->num_handshake_signals == 0)
81 		return 0;
82 
83 	adma->base_request_line = si->base_request_line;
84 	adma->end_request_line = si->base_request_line +
85 				 si->num_handshake_signals - 1;
86 
87 	dev_dbg(&adev->dev, "request line base: 0x%04x end: 0x%04x\n",
88 		adma->base_request_line, adma->end_request_line);
89 
90 	return 1;
91 }
92 
93 /**
94  * acpi_dma_parse_csrt - parse CSRT to exctract additional DMA resources
95  * @adev:	ACPI device to match with
96  * @adma:	struct acpi_dma of the given DMA controller
97  *
98  * CSRT or Core System Resources Table is a proprietary ACPI table
99  * introduced by Microsoft. This table can contain devices that are not in
100  * the system DSDT table. In particular DMA controllers might be described
101  * here.
102  *
103  * We are using this table to get the request line range of the specific DMA
104  * controller to be used later.
105  */
106 static void acpi_dma_parse_csrt(struct acpi_device *adev, struct acpi_dma *adma)
107 {
108 	struct acpi_csrt_group *grp, *end;
109 	struct acpi_table_csrt *csrt;
110 	acpi_status status;
111 	int ret;
112 
113 	status = acpi_get_table(ACPI_SIG_CSRT, 0,
114 				(struct acpi_table_header **)&csrt);
115 	if (ACPI_FAILURE(status)) {
116 		if (status != AE_NOT_FOUND)
117 			dev_warn(&adev->dev, "failed to get the CSRT table\n");
118 		return;
119 	}
120 
121 	grp = (struct acpi_csrt_group *)(csrt + 1);
122 	end = (struct acpi_csrt_group *)((void *)csrt + csrt->header.length);
123 
124 	while (grp < end) {
125 		ret = acpi_dma_parse_resource_group(grp, adev, adma);
126 		if (ret < 0) {
127 			dev_warn(&adev->dev,
128 				 "error in parsing resource group\n");
129 			return;
130 		}
131 
132 		grp = (struct acpi_csrt_group *)((void *)grp + grp->length);
133 	}
134 }
135 
136 /**
137  * acpi_dma_controller_register - Register a DMA controller to ACPI DMA helpers
138  * @dev:		struct device of DMA controller
139  * @acpi_dma_xlate:	translation function which converts a dma specifier
140  *			into a dma_chan structure
141  * @data		pointer to controller specific data to be used by
142  *			translation function
143  *
144  * Allocated memory should be freed with appropriate acpi_dma_controller_free()
145  * call.
146  *
147  * Return:
148  * 0 on success or appropriate errno value on error.
149  */
150 int acpi_dma_controller_register(struct device *dev,
151 		struct dma_chan *(*acpi_dma_xlate)
152 		(struct acpi_dma_spec *, struct acpi_dma *),
153 		void *data)
154 {
155 	struct acpi_device *adev;
156 	struct acpi_dma	*adma;
157 
158 	if (!dev || !acpi_dma_xlate)
159 		return -EINVAL;
160 
161 	/* Check if the device was enumerated by ACPI */
162 	if (!ACPI_HANDLE(dev))
163 		return -EINVAL;
164 
165 	if (acpi_bus_get_device(ACPI_HANDLE(dev), &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 	/* Now queue acpi_dma controller structure in list */
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 }
185 EXPORT_SYMBOL_GPL(acpi_dma_controller_register);
186 
187 /**
188  * acpi_dma_controller_free - Remove a DMA controller from ACPI DMA helpers list
189  * @dev:	struct device of DMA controller
190  *
191  * Memory allocated by acpi_dma_controller_register() is freed here.
192  *
193  * Return:
194  * 0 on success or appropriate errno value on error.
195  */
196 int 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 }
216 EXPORT_SYMBOL_GPL(acpi_dma_controller_free);
217 
218 static void devm_acpi_dma_release(struct device *dev, void *res)
219 {
220 	acpi_dma_controller_free(dev);
221 }
222 
223 /**
224  * devm_acpi_dma_controller_register - resource managed acpi_dma_controller_register()
225  * @dev:		device that is registering this DMA controller
226  * @acpi_dma_xlate:	translation function
227  * @data		pointer to controller specific data
228  *
229  * Managed acpi_dma_controller_register(). DMA controller registered by this
230  * function are automatically freed on driver detach. See
231  * acpi_dma_controller_register() for more information.
232  *
233  * Return:
234  * 0 on success or appropriate errno value on error.
235  */
236 int 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 }
256 EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_register);
257 
258 /**
259  * devm_acpi_dma_controller_free - resource managed acpi_dma_controller_free()
260  *
261  * Unregister a DMA controller registered with
262  * devm_acpi_dma_controller_register(). Normally this function will not need to
263  * be called and the resource management code will ensure that the resource is
264  * freed.
265  */
266 void devm_acpi_dma_controller_free(struct device *dev)
267 {
268 	WARN_ON(devres_destroy(dev, devm_acpi_dma_release, NULL, NULL));
269 }
270 EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_free);
271 
272 /**
273  * acpi_dma_update_dma_spec - prepare dma specifier to pass to translation function
274  * @adma:	struct acpi_dma of DMA controller
275  * @dma_spec:	dma specifier to update
276  *
277  * Accordingly to ACPI 5.0 Specification Table 6-170 "Fixed DMA Resource
278  * Descriptor":
279  *	DMA Request Line bits is a platform-relative number uniquely
280  *	identifying the request line assigned. Request line-to-Controller
281  *	mapping is done in a controller-specific OS driver.
282  * That's why we can safely adjust slave_id when the appropriate controller is
283  * found.
284  *
285  * Return:
286  * 0, if no information is avaiable, -1 on mismatch, and 1 otherwise.
287  */
288 static int acpi_dma_update_dma_spec(struct acpi_dma *adma,
289 		struct acpi_dma_spec *dma_spec)
290 {
291 	/* Set link to the DMA controller device */
292 	dma_spec->dev = adma->dev;
293 
294 	/* Check if the request line range is available */
295 	if (adma->base_request_line == 0 && adma->end_request_line == 0)
296 		return 0;
297 
298 	/* Check if slave_id falls to the range */
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 	 * Here we adjust slave_id. It should be a relative number to the base
305 	 * request line.
306 	 */
307 	dma_spec->slave_id -= adma->base_request_line;
308 
309 	return 1;
310 }
311 
312 struct acpi_dma_parser_data {
313 	struct acpi_dma_spec dma_spec;
314 	size_t index;
315 	size_t n;
316 };
317 
318 /**
319  * acpi_dma_parse_fixed_dma - Parse FixedDMA ACPI resources to a DMA specifier
320  * @res:	struct acpi_resource to get FixedDMA resources from
321  * @data:	pointer to a helper struct acpi_dma_parser_data
322  */
323 static 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 	/* Tell the ACPI core to skip this resource */
337 	return 1;
338 }
339 
340 /**
341  * acpi_dma_request_slave_chan_by_index - Get the DMA slave channel
342  * @dev:	struct device to get DMA request from
343  * @index:	index of FixedDMA descriptor for @dev
344  *
345  * Return:
346  * Pointer to appropriate dma channel on success or NULL on error.
347  */
348 struct 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 	/* Check if the device was enumerated by ACPI */
360 	if (!dev || !ACPI_HANDLE(dev))
361 		return NULL;
362 
363 	if (acpi_bus_get_device(ACPI_HANDLE(dev), &adev))
364 		return NULL;
365 
366 	memset(&pdata, 0, sizeof(pdata));
367 	pdata.index = index;
368 
369 	/* Initial values for the request line and channel */
370 	dma_spec->chan_id = -1;
371 	dma_spec->slave_id = -1;
372 
373 	INIT_LIST_HEAD(&resource_list);
374 	acpi_dev_get_resources(adev, &resource_list,
375 			acpi_dma_parse_fixed_dma, &pdata);
376 	acpi_dev_free_resource_list(&resource_list);
377 
378 	if (dma_spec->slave_id < 0 || dma_spec->chan_id < 0)
379 		return NULL;
380 
381 	mutex_lock(&acpi_dma_lock);
382 
383 	list_for_each_entry(adma, &acpi_dma_list, dma_controllers) {
384 		/*
385 		 * We are not going to call translation function if slave_id
386 		 * doesn't fall to the request range.
387 		 */
388 		found = acpi_dma_update_dma_spec(adma, dma_spec);
389 		if (found < 0)
390 			continue;
391 		chan = adma->acpi_dma_xlate(dma_spec, adma);
392 		/*
393 		 * Try to get a channel only from the DMA controller that
394 		 * matches the slave_id. See acpi_dma_update_dma_spec()
395 		 * description for the details.
396 		 */
397 		if (found > 0 || chan)
398 			break;
399 	}
400 
401 	mutex_unlock(&acpi_dma_lock);
402 	return chan;
403 }
404 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_index);
405 
406 /**
407  * acpi_dma_request_slave_chan_by_name - Get the DMA slave channel
408  * @dev:	struct device to get DMA request from
409  * @name:	represents corresponding FixedDMA descriptor for @dev
410  *
411  * In order to support both Device Tree and ACPI in a single driver we
412  * translate the names "tx" and "rx" here based on the most common case where
413  * the first FixedDMA descriptor is TX and second is RX.
414  *
415  * Return:
416  * Pointer to appropriate dma channel on success or NULL on error.
417  */
418 struct dma_chan *acpi_dma_request_slave_chan_by_name(struct device *dev,
419 		const char *name)
420 {
421 	size_t index;
422 
423 	if (!strcmp(name, "tx"))
424 		index = 0;
425 	else if (!strcmp(name, "rx"))
426 		index = 1;
427 	else
428 		return NULL;
429 
430 	return acpi_dma_request_slave_chan_by_index(dev, index);
431 }
432 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_name);
433 
434 /**
435  * acpi_dma_simple_xlate - Simple ACPI DMA engine translation helper
436  * @dma_spec: pointer to ACPI DMA specifier
437  * @adma: pointer to ACPI DMA controller data
438  *
439  * A simple translation function for ACPI based devices. Passes &struct
440  * dma_spec to the DMA controller driver provided filter function.
441  *
442  * Return:
443  * Pointer to the channel if found or %NULL otherwise.
444  */
445 struct dma_chan *acpi_dma_simple_xlate(struct acpi_dma_spec *dma_spec,
446 		struct acpi_dma *adma)
447 {
448 	struct acpi_dma_filter_info *info = adma->data;
449 
450 	if (!info || !info->filter_fn)
451 		return NULL;
452 
453 	return dma_request_channel(info->dma_cap, info->filter_fn, dma_spec);
454 }
455 EXPORT_SYMBOL_GPL(acpi_dma_simple_xlate);
456