blob: ac61ecda292610aeada47dd9e20b4e7b0ecbdf9d [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jon Hunteraa3da642012-09-14 17:41:56 -05002/*
3 * Device tree helpers for DMA request / controller
4 *
5 * Based on of_gpio.c
6 *
7 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
Jon Hunteraa3da642012-09-14 17:41:56 -05008 */
9
10#include <linux/device.h>
11#include <linux/err.h>
12#include <linux/module.h>
Lars-Peter Clausende616082013-04-19 11:42:14 +020013#include <linux/mutex.h>
Jon Hunteraa3da642012-09-14 17:41:56 -050014#include <linux/slab.h>
15#include <linux/of.h>
16#include <linux/of_dma.h>
17
Geert Uytterhoevenc3c431d2020-01-21 10:33:11 +010018#include "dmaengine.h"
19
Jon Hunteraa3da642012-09-14 17:41:56 -050020static LIST_HEAD(of_dma_list);
Lars-Peter Clausende616082013-04-19 11:42:14 +020021static DEFINE_MUTEX(of_dma_lock);
Jon Hunteraa3da642012-09-14 17:41:56 -050022
23/**
Lars-Peter Clausende616082013-04-19 11:42:14 +020024 * of_dma_find_controller - Get a DMA controller in DT DMA helpers list
Jon Hunter9743a3b2012-10-11 14:43:01 -050025 * @dma_spec: pointer to DMA specifier as found in the device tree
26 *
27 * Finds a DMA controller with matching device node and number for dma cells
Lars-Peter Clausende616082013-04-19 11:42:14 +020028 * in a list of registered DMA controllers. If a match is found a valid pointer
29 * to the DMA data stored is retuned. A NULL pointer is returned if no match is
30 * found.
Jon Hunteraa3da642012-09-14 17:41:56 -050031 */
Lars-Peter Clausende616082013-04-19 11:42:14 +020032static struct of_dma *of_dma_find_controller(struct of_phandle_args *dma_spec)
Jon Hunteraa3da642012-09-14 17:41:56 -050033{
34 struct of_dma *ofdma;
35
Jon Hunter9743a3b2012-10-11 14:43:01 -050036 list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
Lars-Peter Clausen8552bb42013-04-22 10:33:33 +020037 if (ofdma->of_node == dma_spec->np)
Jon Hunteraa3da642012-09-14 17:41:56 -050038 return ofdma;
Jon Hunter9743a3b2012-10-11 14:43:01 -050039
Rob Herringc6c93042017-07-18 16:42:58 -050040 pr_debug("%s: can't find DMA controller %pOF\n", __func__,
41 dma_spec->np);
Jon Hunteraa3da642012-09-14 17:41:56 -050042
43 return NULL;
44}
45
46/**
Peter Ujfalusi56f13c02015-04-09 12:35:47 +030047 * of_dma_router_xlate - translation function for router devices
48 * @dma_spec: pointer to DMA specifier as found in the device tree
Lee Jones7d8c9142020-07-14 12:15:31 +010049 * @ofdma: pointer to DMA controller data (router information)
Peter Ujfalusi56f13c02015-04-09 12:35:47 +030050 *
51 * The function creates new dma_spec to be passed to the router driver's
52 * of_dma_route_allocate() function to prepare a dma_spec which will be used
53 * to request channel from the real DMA controller.
54 */
55static struct dma_chan *of_dma_router_xlate(struct of_phandle_args *dma_spec,
56 struct of_dma *ofdma)
57{
58 struct dma_chan *chan;
59 struct of_dma *ofdma_target;
60 struct of_phandle_args dma_spec_target;
61 void *route_data;
62
63 /* translate the request for the real DMA controller */
64 memcpy(&dma_spec_target, dma_spec, sizeof(dma_spec_target));
65 route_data = ofdma->of_dma_route_allocate(&dma_spec_target, ofdma);
66 if (IS_ERR(route_data))
67 return NULL;
68
69 ofdma_target = of_dma_find_controller(&dma_spec_target);
Peter Ujfalusieda97cb2021-07-17 22:00:21 +030070 if (!ofdma_target) {
71 ofdma->dma_router->route_free(ofdma->dma_router->dev,
72 route_data);
73 chan = ERR_PTR(-EPROBE_DEFER);
74 goto err;
75 }
Peter Ujfalusi56f13c02015-04-09 12:35:47 +030076
77 chan = ofdma_target->of_dma_xlate(&dma_spec_target, ofdma_target);
Peter Ujfalusi5b2aa9f2020-08-06 13:49:28 +030078 if (IS_ERR_OR_NULL(chan)) {
Peter Ujfalusi56f13c02015-04-09 12:35:47 +030079 ofdma->dma_router->route_free(ofdma->dma_router->dev,
80 route_data);
Peter Ujfalusi5b2aa9f2020-08-06 13:49:28 +030081 } else {
Peter Ujfalusi4f910c02020-12-08 11:04:27 +020082 int ret = 0;
83
Peter Ujfalusi5b2aa9f2020-08-06 13:49:28 +030084 chan->router = ofdma->dma_router;
85 chan->route_data = route_data;
Peter Ujfalusi4f910c02020-12-08 11:04:27 +020086
87 if (chan->device->device_router_config)
88 ret = chan->device->device_router_config(chan);
89
90 if (ret) {
91 dma_release_channel(chan);
92 chan = ERR_PTR(ret);
93 }
Peter Ujfalusi56f13c02015-04-09 12:35:47 +030094 }
95
Peter Ujfalusieda97cb2021-07-17 22:00:21 +030096err:
Peter Ujfalusi56f13c02015-04-09 12:35:47 +030097 /*
98 * Need to put the node back since the ofdma->of_dma_route_allocate
99 * has taken it for generating the new, translated dma_spec
100 */
101 of_node_put(dma_spec_target.np);
102 return chan;
103}
104
105/**
Jon Hunteraa3da642012-09-14 17:41:56 -0500106 * of_dma_controller_register - Register a DMA controller to DT DMA helpers
107 * @np: device node of DMA controller
108 * @of_dma_xlate: translation function which converts a phandle
109 * arguments list into a dma_chan structure
Lee Jones7d8c9142020-07-14 12:15:31 +0100110 * @data: pointer to controller specific data to be used by
Jon Hunteraa3da642012-09-14 17:41:56 -0500111 * translation function
112 *
113 * Returns 0 on success or appropriate errno value on error.
114 *
115 * Allocated memory should be freed with appropriate of_dma_controller_free()
116 * call.
117 */
118int of_dma_controller_register(struct device_node *np,
119 struct dma_chan *(*of_dma_xlate)
120 (struct of_phandle_args *, struct of_dma *),
121 void *data)
122{
123 struct of_dma *ofdma;
Jon Hunteraa3da642012-09-14 17:41:56 -0500124
125 if (!np || !of_dma_xlate) {
126 pr_err("%s: not enough information provided\n", __func__);
127 return -EINVAL;
128 }
129
130 ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
131 if (!ofdma)
132 return -ENOMEM;
133
Jon Hunteraa3da642012-09-14 17:41:56 -0500134 ofdma->of_node = np;
Jon Hunteraa3da642012-09-14 17:41:56 -0500135 ofdma->of_dma_xlate = of_dma_xlate;
136 ofdma->of_dma_data = data;
137
138 /* Now queue of_dma controller structure in list */
Lars-Peter Clausende616082013-04-19 11:42:14 +0200139 mutex_lock(&of_dma_lock);
Jon Hunter9743a3b2012-10-11 14:43:01 -0500140 list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
Lars-Peter Clausende616082013-04-19 11:42:14 +0200141 mutex_unlock(&of_dma_lock);
Jon Hunteraa3da642012-09-14 17:41:56 -0500142
143 return 0;
144}
145EXPORT_SYMBOL_GPL(of_dma_controller_register);
146
147/**
148 * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list
149 * @np: device node of DMA controller
150 *
151 * Memory allocated by of_dma_controller_register() is freed here.
152 */
Lars-Peter Clausende616082013-04-19 11:42:14 +0200153void of_dma_controller_free(struct device_node *np)
Jon Hunteraa3da642012-09-14 17:41:56 -0500154{
155 struct of_dma *ofdma;
156
Lars-Peter Clausende616082013-04-19 11:42:14 +0200157 mutex_lock(&of_dma_lock);
Jon Hunter9743a3b2012-10-11 14:43:01 -0500158
159 list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
160 if (ofdma->of_node == np) {
Jon Hunter9743a3b2012-10-11 14:43:01 -0500161 list_del(&ofdma->of_dma_controllers);
Jon Hunter9743a3b2012-10-11 14:43:01 -0500162 kfree(ofdma);
Lars-Peter Clausende616082013-04-19 11:42:14 +0200163 break;
Jon Hunter9743a3b2012-10-11 14:43:01 -0500164 }
165
Lars-Peter Clausende616082013-04-19 11:42:14 +0200166 mutex_unlock(&of_dma_lock);
Jon Hunteraa3da642012-09-14 17:41:56 -0500167}
168EXPORT_SYMBOL_GPL(of_dma_controller_free);
169
170/**
Peter Ujfalusi56f13c02015-04-09 12:35:47 +0300171 * of_dma_router_register - Register a DMA router to DT DMA helpers as a
172 * controller
173 * @np: device node of DMA router
174 * @of_dma_route_allocate: setup function for the router which need to
175 * modify the dma_spec for the DMA controller to
176 * use and to set up the requested route.
177 * @dma_router: pointer to dma_router structure to be used when
178 * the route need to be free up.
179 *
180 * Returns 0 on success or appropriate errno value on error.
181 *
182 * Allocated memory should be freed with appropriate of_dma_controller_free()
183 * call.
184 */
185int of_dma_router_register(struct device_node *np,
186 void *(*of_dma_route_allocate)
187 (struct of_phandle_args *, struct of_dma *),
188 struct dma_router *dma_router)
189{
190 struct of_dma *ofdma;
191
192 if (!np || !of_dma_route_allocate || !dma_router) {
193 pr_err("%s: not enough information provided\n", __func__);
194 return -EINVAL;
195 }
196
197 ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
198 if (!ofdma)
199 return -ENOMEM;
200
201 ofdma->of_node = np;
202 ofdma->of_dma_xlate = of_dma_router_xlate;
203 ofdma->of_dma_route_allocate = of_dma_route_allocate;
204 ofdma->dma_router = dma_router;
205
206 /* Now queue of_dma controller structure in list */
207 mutex_lock(&of_dma_lock);
208 list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
209 mutex_unlock(&of_dma_lock);
210
211 return 0;
212}
213EXPORT_SYMBOL_GPL(of_dma_router_register);
214
215/**
Jon Hunter5ca7c102012-09-25 13:59:31 -0500216 * of_dma_match_channel - Check if a DMA specifier matches name
Jon Hunteraa3da642012-09-14 17:41:56 -0500217 * @np: device node to look for DMA channels
Jon Hunter5ca7c102012-09-25 13:59:31 -0500218 * @name: channel name to be matched
219 * @index: index of DMA specifier in list of DMA specifiers
Jon Hunteraa3da642012-09-14 17:41:56 -0500220 * @dma_spec: pointer to DMA specifier as found in the device tree
221 *
Jon Hunter5ca7c102012-09-25 13:59:31 -0500222 * Check if the DMA specifier pointed to by the index in a list of DMA
223 * specifiers, matches the name provided. Returns 0 if the name matches and
224 * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
Jon Hunteraa3da642012-09-14 17:41:56 -0500225 */
Markus Pargmannbef29ec2013-02-24 16:36:09 +0100226static int of_dma_match_channel(struct device_node *np, const char *name,
227 int index, struct of_phandle_args *dma_spec)
Jon Hunteraa3da642012-09-14 17:41:56 -0500228{
Jon Hunteraa3da642012-09-14 17:41:56 -0500229 const char *s;
230
Jon Hunter5ca7c102012-09-25 13:59:31 -0500231 if (of_property_read_string_index(np, "dma-names", index, &s))
232 return -ENODEV;
Jon Hunteraa3da642012-09-14 17:41:56 -0500233
Jon Hunter5ca7c102012-09-25 13:59:31 -0500234 if (strcmp(name, s))
235 return -ENODEV;
Jon Hunteraa3da642012-09-14 17:41:56 -0500236
Jon Hunter5ca7c102012-09-25 13:59:31 -0500237 if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
238 dma_spec))
239 return -ENODEV;
Jon Hunteraa3da642012-09-14 17:41:56 -0500240
Jon Hunter5ca7c102012-09-25 13:59:31 -0500241 return 0;
Jon Hunteraa3da642012-09-14 17:41:56 -0500242}
243
244/**
245 * of_dma_request_slave_channel - Get the DMA slave channel
246 * @np: device node to get DMA request from
247 * @name: name of desired channel
248 *
Stephen Warren0ad7c002013-11-26 10:04:22 -0700249 * Returns pointer to appropriate DMA channel on success or an error pointer.
Jon Hunteraa3da642012-09-14 17:41:56 -0500250 */
251struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
Markus Pargmannbef29ec2013-02-24 16:36:09 +0100252 const char *name)
Jon Hunteraa3da642012-09-14 17:41:56 -0500253{
254 struct of_phandle_args dma_spec;
255 struct of_dma *ofdma;
256 struct dma_chan *chan;
Niklas Söderlund20ea6be2016-05-11 15:15:11 +0200257 int count, i, start;
Stephen Warren0ad7c002013-11-26 10:04:22 -0700258 int ret_no_channel = -ENODEV;
Niklas Söderlund20ea6be2016-05-11 15:15:11 +0200259 static atomic_t last_index;
Jon Hunteraa3da642012-09-14 17:41:56 -0500260
261 if (!np || !name) {
262 pr_err("%s: not enough information provided\n", __func__);
Stephen Warren0ad7c002013-11-26 10:04:22 -0700263 return ERR_PTR(-ENODEV);
Jon Hunteraa3da642012-09-14 17:41:56 -0500264 }
265
Wolfram Sangc9145702015-01-14 15:16:28 +0100266 /* Silently fail if there is not even the "dmas" property */
267 if (!of_find_property(np, "dmas", NULL))
268 return ERR_PTR(-ENODEV);
269
Jon Hunter5ca7c102012-09-25 13:59:31 -0500270 count = of_property_count_strings(np, "dma-names");
271 if (count < 0) {
Rob Herringc6c93042017-07-18 16:42:58 -0500272 pr_err("%s: dma-names property of node '%pOF' missing or empty\n",
273 __func__, np);
Stephen Warren0ad7c002013-11-26 10:04:22 -0700274 return ERR_PTR(-ENODEV);
Jon Hunter5ca7c102012-09-25 13:59:31 -0500275 }
276
Niklas Söderlund20ea6be2016-05-11 15:15:11 +0200277 /*
278 * approximate an average distribution across multiple
279 * entries with the same name
280 */
281 start = atomic_inc_return(&last_index);
Jon Hunter5ca7c102012-09-25 13:59:31 -0500282 for (i = 0; i < count; i++) {
Niklas Söderlund20ea6be2016-05-11 15:15:11 +0200283 if (of_dma_match_channel(np, name,
284 (i + start) % count,
285 &dma_spec))
Jon Hunter5ca7c102012-09-25 13:59:31 -0500286 continue;
Jon Hunteraa3da642012-09-14 17:41:56 -0500287
Lars-Peter Clausende616082013-04-19 11:42:14 +0200288 mutex_lock(&of_dma_lock);
289 ofdma = of_dma_find_controller(&dma_spec);
Jon Hunteraa3da642012-09-14 17:41:56 -0500290
Stephen Warren0ad7c002013-11-26 10:04:22 -0700291 if (ofdma) {
Lars-Peter Clausenf22eb142013-04-19 11:42:13 +0200292 chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
Stephen Warren0ad7c002013-11-26 10:04:22 -0700293 } else {
294 ret_no_channel = -EPROBE_DEFER;
Lars-Peter Clausenf22eb142013-04-19 11:42:13 +0200295 chan = NULL;
Stephen Warren0ad7c002013-11-26 10:04:22 -0700296 }
Lars-Peter Clausende616082013-04-19 11:42:14 +0200297
298 mutex_unlock(&of_dma_lock);
Jon Hunter9743a3b2012-10-11 14:43:01 -0500299
Jon Hunteraa3da642012-09-14 17:41:56 -0500300 of_node_put(dma_spec.np);
301
Jon Hunter5ca7c102012-09-25 13:59:31 -0500302 if (chan)
303 return chan;
304 }
Jon Hunteraa3da642012-09-14 17:41:56 -0500305
Stephen Warren0ad7c002013-11-26 10:04:22 -0700306 return ERR_PTR(ret_no_channel);
Jon Hunteraa3da642012-09-14 17:41:56 -0500307}
Kuninori Morimoto0aed1122015-02-24 00:54:16 +0000308EXPORT_SYMBOL_GPL(of_dma_request_slave_channel);
Jon Hunteraa3da642012-09-14 17:41:56 -0500309
310/**
311 * of_dma_simple_xlate - Simple DMA engine translation function
312 * @dma_spec: pointer to DMA specifier as found in the device tree
Lee Jones7d8c9142020-07-14 12:15:31 +0100313 * @ofdma: pointer to DMA controller data
Jon Hunteraa3da642012-09-14 17:41:56 -0500314 *
315 * A simple translation function for devices that use a 32-bit value for the
316 * filter_param when calling the DMA engine dma_request_channel() function.
317 * Note that this translation function requires that #dma-cells is equal to 1
318 * and the argument of the dma specifier is the 32-bit filter_param. Returns
319 * pointer to appropriate dma channel on success or NULL on error.
320 */
321struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
322 struct of_dma *ofdma)
323{
324 int count = dma_spec->args_count;
325 struct of_dma_filter_info *info = ofdma->of_dma_data;
326
327 if (!info || !info->filter_fn)
328 return NULL;
329
330 if (count != 1)
331 return NULL;
332
Baolin Wangf5151312019-05-20 19:32:14 +0800333 return __dma_request_channel(&info->dma_cap, info->filter_fn,
334 &dma_spec->args[0], dma_spec->np);
Jon Hunteraa3da642012-09-14 17:41:56 -0500335}
336EXPORT_SYMBOL_GPL(of_dma_simple_xlate);
Alexander Popov16369ef2014-06-25 14:52:59 +0400337
338/**
339 * of_dma_xlate_by_chan_id - Translate dt property to DMA channel by channel id
340 * @dma_spec: pointer to DMA specifier as found in the device tree
Lee Jones7d8c9142020-07-14 12:15:31 +0100341 * @ofdma: pointer to DMA controller data
Alexander Popov16369ef2014-06-25 14:52:59 +0400342 *
343 * This function can be used as the of xlate callback for DMA driver which wants
344 * to match the channel based on the channel id. When using this xlate function
345 * the #dma-cells propety of the DMA controller dt node needs to be set to 1.
346 * The data parameter of of_dma_controller_register must be a pointer to the
347 * dma_device struct the function should match upon.
348 *
349 * Returns pointer to appropriate dma channel on success or NULL on error.
350 */
351struct dma_chan *of_dma_xlate_by_chan_id(struct of_phandle_args *dma_spec,
352 struct of_dma *ofdma)
353{
354 struct dma_device *dev = ofdma->of_dma_data;
355 struct dma_chan *chan, *candidate = NULL;
356
357 if (!dev || dma_spec->args_count != 1)
358 return NULL;
359
360 list_for_each_entry(chan, &dev->channels, device_node)
361 if (chan->chan_id == dma_spec->args[0]) {
362 candidate = chan;
363 break;
364 }
365
366 if (!candidate)
367 return NULL;
368
369 return dma_get_slave_channel(candidate);
370}
371EXPORT_SYMBOL_GPL(of_dma_xlate_by_chan_id);