1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/string.h>
3*4882a593Smuzhiyun #include <linux/kernel.h>
4*4882a593Smuzhiyun #include <linux/of.h>
5*4882a593Smuzhiyun #include <linux/of_device.h>
6*4882a593Smuzhiyun #include <linux/of_address.h>
7*4882a593Smuzhiyun #include <linux/of_iommu.h>
8*4882a593Smuzhiyun #include <linux/dma-direct.h> /* for bus_dma_region */
9*4882a593Smuzhiyun #include <linux/dma-map-ops.h>
10*4882a593Smuzhiyun #include <linux/init.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <linux/platform_device.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <asm/errno.h>
17*4882a593Smuzhiyun #include "of_private.h"
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /**
20*4882a593Smuzhiyun * of_match_device - Tell if a struct device matches an of_device_id list
21*4882a593Smuzhiyun * @matches: array of of device match structures to search in
22*4882a593Smuzhiyun * @dev: the of device structure to match against
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Used by a driver to check whether an platform_device present in the
25*4882a593Smuzhiyun * system is in its list of supported devices.
26*4882a593Smuzhiyun */
of_match_device(const struct of_device_id * matches,const struct device * dev)27*4882a593Smuzhiyun const struct of_device_id *of_match_device(const struct of_device_id *matches,
28*4882a593Smuzhiyun const struct device *dev)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun if ((!matches) || (!dev->of_node))
31*4882a593Smuzhiyun return NULL;
32*4882a593Smuzhiyun return of_match_node(matches, dev->of_node);
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun EXPORT_SYMBOL(of_match_device);
35*4882a593Smuzhiyun
of_dev_get(struct platform_device * dev)36*4882a593Smuzhiyun struct platform_device *of_dev_get(struct platform_device *dev)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun struct device *tmp;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun if (!dev)
41*4882a593Smuzhiyun return NULL;
42*4882a593Smuzhiyun tmp = get_device(&dev->dev);
43*4882a593Smuzhiyun if (tmp)
44*4882a593Smuzhiyun return to_platform_device(tmp);
45*4882a593Smuzhiyun else
46*4882a593Smuzhiyun return NULL;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun EXPORT_SYMBOL(of_dev_get);
49*4882a593Smuzhiyun
of_dev_put(struct platform_device * dev)50*4882a593Smuzhiyun void of_dev_put(struct platform_device *dev)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun if (dev)
53*4882a593Smuzhiyun put_device(&dev->dev);
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun EXPORT_SYMBOL(of_dev_put);
56*4882a593Smuzhiyun
of_device_add(struct platform_device * ofdev)57*4882a593Smuzhiyun int of_device_add(struct platform_device *ofdev)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun BUG_ON(ofdev->dev.of_node == NULL);
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /* name and id have to be set so that the platform bus doesn't get
62*4882a593Smuzhiyun * confused on matching */
63*4882a593Smuzhiyun ofdev->name = dev_name(&ofdev->dev);
64*4882a593Smuzhiyun ofdev->id = PLATFORM_DEVID_NONE;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun * If this device has not binding numa node in devicetree, that is
68*4882a593Smuzhiyun * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
69*4882a593Smuzhiyun * device is on the same node as the parent.
70*4882a593Smuzhiyun */
71*4882a593Smuzhiyun set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun return device_add(&ofdev->dev);
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /**
77*4882a593Smuzhiyun * of_dma_configure - Setup DMA configuration
78*4882a593Smuzhiyun * @dev: Device to apply DMA configuration
79*4882a593Smuzhiyun * @np: Pointer to OF node having DMA configuration
80*4882a593Smuzhiyun * @force_dma: Whether device is to be set up by of_dma_configure() even if
81*4882a593Smuzhiyun * DMA capability is not explicitly described by firmware.
82*4882a593Smuzhiyun * @id: Optional const pointer value input id
83*4882a593Smuzhiyun *
84*4882a593Smuzhiyun * Try to get devices's DMA configuration from DT and update it
85*4882a593Smuzhiyun * accordingly.
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * If platform code needs to use its own special DMA configuration, it
88*4882a593Smuzhiyun * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
89*4882a593Smuzhiyun * to fix up DMA configuration.
90*4882a593Smuzhiyun */
of_dma_configure_id(struct device * dev,struct device_node * np,bool force_dma,const u32 * id)91*4882a593Smuzhiyun int of_dma_configure_id(struct device *dev, struct device_node *np,
92*4882a593Smuzhiyun bool force_dma, const u32 *id)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun const struct iommu_ops *iommu;
95*4882a593Smuzhiyun const struct bus_dma_region *map = NULL;
96*4882a593Smuzhiyun u64 dma_start = 0;
97*4882a593Smuzhiyun u64 mask, end, size = 0;
98*4882a593Smuzhiyun bool coherent;
99*4882a593Smuzhiyun int ret;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun ret = of_dma_get_range(np, &map);
102*4882a593Smuzhiyun if (ret < 0) {
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * For legacy reasons, we have to assume some devices need
105*4882a593Smuzhiyun * DMA configuration regardless of whether "dma-ranges" is
106*4882a593Smuzhiyun * correctly specified or not.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun if (!force_dma)
109*4882a593Smuzhiyun return ret == -ENODEV ? 0 : ret;
110*4882a593Smuzhiyun } else {
111*4882a593Smuzhiyun const struct bus_dma_region *r = map;
112*4882a593Smuzhiyun u64 dma_end = 0;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* Determine the overall bounds of all DMA regions */
115*4882a593Smuzhiyun for (dma_start = ~0; r->size; r++) {
116*4882a593Smuzhiyun /* Take lower and upper limits */
117*4882a593Smuzhiyun if (r->dma_start < dma_start)
118*4882a593Smuzhiyun dma_start = r->dma_start;
119*4882a593Smuzhiyun if (r->dma_start + r->size > dma_end)
120*4882a593Smuzhiyun dma_end = r->dma_start + r->size;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun size = dma_end - dma_start;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /*
125*4882a593Smuzhiyun * Add a work around to treat the size as mask + 1 in case
126*4882a593Smuzhiyun * it is defined in DT as a mask.
127*4882a593Smuzhiyun */
128*4882a593Smuzhiyun if (size & 1) {
129*4882a593Smuzhiyun dev_warn(dev, "Invalid size 0x%llx for dma-range(s)\n",
130*4882a593Smuzhiyun size);
131*4882a593Smuzhiyun size = size + 1;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun if (!size) {
135*4882a593Smuzhiyun dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
136*4882a593Smuzhiyun kfree(map);
137*4882a593Smuzhiyun return -EINVAL;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun * If @dev is expected to be DMA-capable then the bus code that created
143*4882a593Smuzhiyun * it should have initialised its dma_mask pointer by this point. For
144*4882a593Smuzhiyun * now, we'll continue the legacy behaviour of coercing it to the
145*4882a593Smuzhiyun * coherent mask if not, but we'll no longer do so quietly.
146*4882a593Smuzhiyun */
147*4882a593Smuzhiyun if (!dev->dma_mask) {
148*4882a593Smuzhiyun dev_warn(dev, "DMA mask not set\n");
149*4882a593Smuzhiyun dev->dma_mask = &dev->coherent_dma_mask;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun if (!size && dev->coherent_dma_mask)
153*4882a593Smuzhiyun size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
154*4882a593Smuzhiyun else if (!size)
155*4882a593Smuzhiyun size = 1ULL << 32;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /*
158*4882a593Smuzhiyun * Limit coherent and dma mask based on size and default mask
159*4882a593Smuzhiyun * set by the driver.
160*4882a593Smuzhiyun */
161*4882a593Smuzhiyun end = dma_start + size - 1;
162*4882a593Smuzhiyun mask = DMA_BIT_MASK(ilog2(end) + 1);
163*4882a593Smuzhiyun dev->coherent_dma_mask &= mask;
164*4882a593Smuzhiyun *dev->dma_mask &= mask;
165*4882a593Smuzhiyun /* ...but only set bus limit and range map if we found valid dma-ranges earlier */
166*4882a593Smuzhiyun if (!ret) {
167*4882a593Smuzhiyun dev->bus_dma_limit = end;
168*4882a593Smuzhiyun dev->dma_range_map = map;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun coherent = of_dma_is_coherent(np);
172*4882a593Smuzhiyun dev_dbg(dev, "device is%sdma coherent\n",
173*4882a593Smuzhiyun coherent ? " " : " not ");
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun iommu = of_iommu_configure(dev, np, id);
176*4882a593Smuzhiyun if (PTR_ERR(iommu) == -EPROBE_DEFER) {
177*4882a593Smuzhiyun /* Don't touch range map if it wasn't set from a valid dma-ranges */
178*4882a593Smuzhiyun if (!ret)
179*4882a593Smuzhiyun dev->dma_range_map = NULL;
180*4882a593Smuzhiyun kfree(map);
181*4882a593Smuzhiyun return -EPROBE_DEFER;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun dev_dbg(dev, "device is%sbehind an iommu\n",
185*4882a593Smuzhiyun iommu ? " " : " not ");
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun return 0;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_dma_configure_id);
192*4882a593Smuzhiyun
of_device_register(struct platform_device * pdev)193*4882a593Smuzhiyun int of_device_register(struct platform_device *pdev)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun device_initialize(&pdev->dev);
196*4882a593Smuzhiyun return of_device_add(pdev);
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun EXPORT_SYMBOL(of_device_register);
199*4882a593Smuzhiyun
of_device_unregister(struct platform_device * ofdev)200*4882a593Smuzhiyun void of_device_unregister(struct platform_device *ofdev)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun device_unregister(&ofdev->dev);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun EXPORT_SYMBOL(of_device_unregister);
205*4882a593Smuzhiyun
of_device_get_match_data(const struct device * dev)206*4882a593Smuzhiyun const void *of_device_get_match_data(const struct device *dev)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun const struct of_device_id *match;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun match = of_match_device(dev->driver->of_match_table, dev);
211*4882a593Smuzhiyun if (!match)
212*4882a593Smuzhiyun return NULL;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun return match->data;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun EXPORT_SYMBOL(of_device_get_match_data);
217*4882a593Smuzhiyun
of_device_get_modalias(struct device * dev,char * str,ssize_t len)218*4882a593Smuzhiyun static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun const char *compat;
221*4882a593Smuzhiyun char *c;
222*4882a593Smuzhiyun struct property *p;
223*4882a593Smuzhiyun ssize_t csize;
224*4882a593Smuzhiyun ssize_t tsize;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun if ((!dev) || (!dev->of_node))
227*4882a593Smuzhiyun return -ENODEV;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /* Name & Type */
230*4882a593Smuzhiyun /* %p eats all alphanum characters, so %c must be used here */
231*4882a593Smuzhiyun csize = snprintf(str, len, "of:N%pOFn%c%s", dev->of_node, 'T',
232*4882a593Smuzhiyun of_node_get_device_type(dev->of_node));
233*4882a593Smuzhiyun tsize = csize;
234*4882a593Smuzhiyun len -= csize;
235*4882a593Smuzhiyun if (str)
236*4882a593Smuzhiyun str += csize;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun of_property_for_each_string(dev->of_node, "compatible", p, compat) {
239*4882a593Smuzhiyun csize = strlen(compat) + 1;
240*4882a593Smuzhiyun tsize += csize;
241*4882a593Smuzhiyun if (csize > len)
242*4882a593Smuzhiyun continue;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun csize = snprintf(str, len, "C%s", compat);
245*4882a593Smuzhiyun for (c = str; c; ) {
246*4882a593Smuzhiyun c = strchr(c, ' ');
247*4882a593Smuzhiyun if (c)
248*4882a593Smuzhiyun *c++ = '_';
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun len -= csize;
251*4882a593Smuzhiyun str += csize;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun return tsize;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun
of_device_request_module(struct device * dev)257*4882a593Smuzhiyun int of_device_request_module(struct device *dev)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun char *str;
260*4882a593Smuzhiyun ssize_t size;
261*4882a593Smuzhiyun int ret;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun size = of_device_get_modalias(dev, NULL, 0);
264*4882a593Smuzhiyun if (size < 0)
265*4882a593Smuzhiyun return size;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun str = kmalloc(size + 1, GFP_KERNEL);
268*4882a593Smuzhiyun if (!str)
269*4882a593Smuzhiyun return -ENOMEM;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun of_device_get_modalias(dev, str, size);
272*4882a593Smuzhiyun str[size] = '\0';
273*4882a593Smuzhiyun ret = request_module(str);
274*4882a593Smuzhiyun kfree(str);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun return ret;
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_device_request_module);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun /**
281*4882a593Smuzhiyun * of_device_modalias - Fill buffer with newline terminated modalias string
282*4882a593Smuzhiyun */
of_device_modalias(struct device * dev,char * str,ssize_t len)283*4882a593Smuzhiyun ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun ssize_t sl = of_device_get_modalias(dev, str, len - 2);
286*4882a593Smuzhiyun if (sl < 0)
287*4882a593Smuzhiyun return sl;
288*4882a593Smuzhiyun if (sl > len - 2)
289*4882a593Smuzhiyun return -ENOMEM;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun str[sl++] = '\n';
292*4882a593Smuzhiyun str[sl] = 0;
293*4882a593Smuzhiyun return sl;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_device_modalias);
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun /**
298*4882a593Smuzhiyun * of_device_uevent - Display OF related uevent information
299*4882a593Smuzhiyun */
of_device_uevent(struct device * dev,struct kobj_uevent_env * env)300*4882a593Smuzhiyun void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun const char *compat, *type;
303*4882a593Smuzhiyun struct alias_prop *app;
304*4882a593Smuzhiyun struct property *p;
305*4882a593Smuzhiyun int seen = 0;
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun if ((!dev) || (!dev->of_node))
308*4882a593Smuzhiyun return;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node);
311*4882a593Smuzhiyun add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
312*4882a593Smuzhiyun type = of_node_get_device_type(dev->of_node);
313*4882a593Smuzhiyun if (type)
314*4882a593Smuzhiyun add_uevent_var(env, "OF_TYPE=%s", type);
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun /* Since the compatible field can contain pretty much anything
317*4882a593Smuzhiyun * it's not really legal to split it out with commas. We split it
318*4882a593Smuzhiyun * up using a number of environment variables instead. */
319*4882a593Smuzhiyun of_property_for_each_string(dev->of_node, "compatible", p, compat) {
320*4882a593Smuzhiyun add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
321*4882a593Smuzhiyun seen++;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun seen = 0;
326*4882a593Smuzhiyun mutex_lock(&of_mutex);
327*4882a593Smuzhiyun list_for_each_entry(app, &aliases_lookup, link) {
328*4882a593Smuzhiyun if (dev->of_node == app->np) {
329*4882a593Smuzhiyun add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
330*4882a593Smuzhiyun app->alias);
331*4882a593Smuzhiyun seen++;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun mutex_unlock(&of_mutex);
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
of_device_uevent_modalias(struct device * dev,struct kobj_uevent_env * env)337*4882a593Smuzhiyun int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun int sl;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun if ((!dev) || (!dev->of_node))
342*4882a593Smuzhiyun return -ENODEV;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /* Devicetree modalias is tricky, we add it in 2 steps */
345*4882a593Smuzhiyun if (add_uevent_var(env, "MODALIAS="))
346*4882a593Smuzhiyun return -ENOMEM;
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
349*4882a593Smuzhiyun sizeof(env->buf) - env->buflen);
350*4882a593Smuzhiyun if (sl >= (sizeof(env->buf) - env->buflen))
351*4882a593Smuzhiyun return -ENOMEM;
352*4882a593Smuzhiyun env->buflen += sl;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun return 0;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_device_uevent_modalias);
357