xref: /linux/drivers/of/device.c (revision 995832b2cebe6969d1b42635db698803ee31294d)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/of.h>
4 #include <linux/of_device.h>
5 #include <linux/of_address.h>
6 #include <linux/of_iommu.h>
7 #include <linux/of_reserved_mem.h>
8 #include <linux/dma-direct.h> /* for bus_dma_region */
9 #include <linux/dma-map-ops.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
13 
14 #include <asm/errno.h>
15 #include "of_private.h"
16 
17 /**
18  * of_match_device - Tell if a struct device matches an of_device_id list
19  * @matches: array of of_device_id match structures to search in
20  * @dev: the OF device structure to match against
21  *
22  * Used by a driver to check whether an platform_device present in the
23  * system is in its list of supported devices.
24  */
25 const struct of_device_id *of_match_device(const struct of_device_id *matches,
26 					   const struct device *dev)
27 {
28 	if (!matches || !dev->of_node || dev_of_node_reused(dev))
29 		return NULL;
30 	return of_match_node(matches, dev->of_node);
31 }
32 EXPORT_SYMBOL(of_match_device);
33 
34 static void
35 of_dma_set_restricted_buffer(struct device *dev, struct device_node *np)
36 {
37 	struct device_node *of_node = dev->of_node;
38 	struct of_phandle_iterator it;
39 	int rc, i = 0;
40 
41 	if (!IS_ENABLED(CONFIG_DMA_RESTRICTED_POOL))
42 		return;
43 
44 	/*
45 	 * If dev->of_node doesn't exist or doesn't contain memory-region, try
46 	 * the OF node having DMA configuration.
47 	 */
48 	if (!of_property_present(of_node, "memory-region"))
49 		of_node = np;
50 
51 	of_for_each_phandle(&it, rc, of_node, "memory-region", NULL, 0) {
52 		/*
53 		 * There might be multiple memory regions, but only one
54 		 * restricted-dma-pool region is allowed.
55 		 */
56 		if (of_device_is_compatible(it.node, "restricted-dma-pool") &&
57 		    of_device_is_available(it.node)) {
58 			if (of_reserved_mem_device_init_by_idx(dev, of_node, i))
59 				dev_warn(dev, "failed to initialise \"restricted-dma-pool\" memory node\n");
60 			of_node_put(it.node);
61 			break;
62 		}
63 		i++;
64 	}
65 
66 }
67 
68 /**
69  * of_dma_configure_id - Setup DMA configuration
70  * @dev:	Device to apply DMA configuration
71  * @np:		Pointer to OF node having DMA configuration
72  * @force_dma:  Whether device is to be set up by of_dma_configure() even if
73  *		DMA capability is not explicitly described by firmware.
74  * @id:		Optional const pointer value input id
75  *
76  * Try to get devices's DMA configuration from DT and update it
77  * accordingly.
78  *
79  * If platform code needs to use its own special DMA configuration, it
80  * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
81  * to fix up DMA configuration.
82  */
83 int of_dma_configure_id(struct device *dev, struct device_node *np,
84 			bool force_dma, const u32 *id)
85 {
86 	const struct bus_dma_region *map = NULL;
87 	struct device_node *bus_np;
88 	u64 mask, end = 0;
89 	bool coherent, set_map = false;
90 	int ret;
91 
92 	if (dev->dma_range_map) {
93 		dev_dbg(dev, "dma_range_map already set\n");
94 		goto skip_map;
95 	}
96 
97 	if (np == dev->of_node)
98 		bus_np = __of_get_dma_parent(np);
99 	else
100 		bus_np = of_node_get(np);
101 
102 	ret = of_dma_get_range(bus_np, &map);
103 	of_node_put(bus_np);
104 	if (ret < 0) {
105 		/*
106 		 * For legacy reasons, we have to assume some devices need
107 		 * DMA configuration regardless of whether "dma-ranges" is
108 		 * correctly specified or not.
109 		 */
110 		if (!force_dma)
111 			return ret == -ENODEV ? 0 : ret;
112 	} else {
113 		/* Determine the overall bounds of all DMA regions */
114 		end = dma_range_map_max(map);
115 		set_map = true;
116 	}
117 skip_map:
118 	/*
119 	 * If @dev is expected to be DMA-capable then the bus code that created
120 	 * it should have initialised its dma_mask pointer by this point. For
121 	 * now, we'll continue the legacy behaviour of coercing it to the
122 	 * coherent mask if not, but we'll no longer do so quietly.
123 	 */
124 	if (!dev->dma_mask) {
125 		dev_warn(dev, "DMA mask not set\n");
126 		dev->dma_mask = &dev->coherent_dma_mask;
127 	}
128 
129 	if (!end && dev->coherent_dma_mask)
130 		end = dev->coherent_dma_mask;
131 	else if (!end)
132 		end = (1ULL << 32) - 1;
133 
134 	/*
135 	 * Limit coherent and dma mask based on size and default mask
136 	 * set by the driver.
137 	 */
138 	mask = DMA_BIT_MASK(ilog2(end) + 1);
139 	dev->coherent_dma_mask &= mask;
140 	*dev->dma_mask &= mask;
141 	/* ...but only set bus limit and range map if we found valid dma-ranges earlier */
142 	if (set_map) {
143 		dev->bus_dma_limit = end;
144 		dev->dma_range_map = map;
145 	}
146 
147 	coherent = of_dma_is_coherent(np);
148 	dev_dbg(dev, "device is%sdma coherent\n",
149 		coherent ? " " : " not ");
150 
151 	ret = of_iommu_configure(dev, np, id);
152 	if (ret == -EPROBE_DEFER) {
153 		/* Don't touch range map if it wasn't set from a valid dma-ranges */
154 		if (set_map)
155 			dev->dma_range_map = NULL;
156 		kfree(map);
157 		return -EPROBE_DEFER;
158 	}
159 	/* Take all other IOMMU errors to mean we'll just carry on without it */
160 	dev_dbg(dev, "device is%sbehind an iommu\n",
161 		!ret ? " " : " not ");
162 
163 	arch_setup_dma_ops(dev, coherent);
164 
165 	if (ret)
166 		of_dma_set_restricted_buffer(dev, np);
167 
168 	return 0;
169 }
170 EXPORT_SYMBOL_GPL(of_dma_configure_id);
171 
172 const void *of_device_get_match_data(const struct device *dev)
173 {
174 	const struct of_device_id *match;
175 
176 	match = of_match_device(dev->driver->of_match_table, dev);
177 	if (!match)
178 		return NULL;
179 
180 	return match->data;
181 }
182 EXPORT_SYMBOL(of_device_get_match_data);
183 
184 /**
185  * of_device_modalias - Fill buffer with newline terminated modalias string
186  * @dev:	Calling device
187  * @str:	Modalias string
188  * @len:	Size of @str
189  */
190 ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
191 {
192 	ssize_t sl;
193 
194 	if (!dev || !dev->of_node || dev_of_node_reused(dev))
195 		return -ENODEV;
196 
197 	sl = of_modalias(dev->of_node, str, len - 2);
198 	if (sl < 0)
199 		return sl;
200 	if (sl > len - 2)
201 		return -ENOMEM;
202 
203 	str[sl++] = '\n';
204 	str[sl] = 0;
205 	return sl;
206 }
207 EXPORT_SYMBOL_GPL(of_device_modalias);
208 
209 /**
210  * of_device_uevent - Display OF related uevent information
211  * @dev:	Device to display the uevent information for
212  * @env:	Kernel object's userspace event reference to fill up
213  */
214 void of_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
215 {
216 	const char *compat, *type;
217 	struct alias_prop *app;
218 	struct property *p;
219 	int seen = 0;
220 
221 	if ((!dev) || (!dev->of_node))
222 		return;
223 
224 	add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node);
225 	add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
226 	type = of_node_get_device_type(dev->of_node);
227 	if (type)
228 		add_uevent_var(env, "OF_TYPE=%s", type);
229 
230 	/* Since the compatible field can contain pretty much anything
231 	 * it's not really legal to split it out with commas. We split it
232 	 * up using a number of environment variables instead. */
233 	of_property_for_each_string(dev->of_node, "compatible", p, compat) {
234 		add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
235 		seen++;
236 	}
237 	add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
238 
239 	seen = 0;
240 	mutex_lock(&of_mutex);
241 	list_for_each_entry(app, &aliases_lookup, link) {
242 		if (dev->of_node == app->np) {
243 			add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
244 				       app->alias);
245 			seen++;
246 		}
247 	}
248 	mutex_unlock(&of_mutex);
249 }
250 EXPORT_SYMBOL_GPL(of_device_uevent);
251 
252 int of_device_uevent_modalias(const struct device *dev, struct kobj_uevent_env *env)
253 {
254 	int sl;
255 
256 	if ((!dev) || (!dev->of_node) || dev_of_node_reused(dev))
257 		return -ENODEV;
258 
259 	/* Devicetree modalias is tricky, we add it in 2 steps */
260 	if (add_uevent_var(env, "MODALIAS="))
261 		return -ENOMEM;
262 
263 	sl = of_modalias(dev->of_node, &env->buf[env->buflen-1],
264 			 sizeof(env->buf) - env->buflen);
265 	if (sl < 0)
266 		return sl;
267 	if (sl >= (sizeof(env->buf) - env->buflen))
268 		return -ENOMEM;
269 	env->buflen += sl;
270 
271 	return 0;
272 }
273 EXPORT_SYMBOL_GPL(of_device_uevent_modalias);
274 
275 /**
276  * of_device_make_bus_id - Use the device node data to assign a unique name
277  * @dev: pointer to device structure that is linked to a device tree node
278  *
279  * This routine will first try using the translated bus address to
280  * derive a unique name. If it cannot, then it will prepend names from
281  * parent nodes until a unique name can be derived.
282  */
283 void of_device_make_bus_id(struct device *dev)
284 {
285 	struct device_node *node = dev->of_node;
286 	const __be32 *reg;
287 	u64 addr;
288 	u32 mask;
289 
290 	/* Construct the name, using parent nodes if necessary to ensure uniqueness */
291 	while (node->parent) {
292 		/*
293 		 * If the address can be translated, then that is as much
294 		 * uniqueness as we need. Make it the first component and return
295 		 */
296 		reg = of_get_property(node, "reg", NULL);
297 		if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
298 			if (!of_property_read_u32(node, "mask", &mask))
299 				dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn",
300 					     addr, ffs(mask) - 1, node, dev_name(dev));
301 
302 			else
303 				dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn",
304 					     addr, node, dev_name(dev));
305 			return;
306 		}
307 
308 		/* format arguments only used if dev_name() resolves to NULL */
309 		dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
310 			     kbasename(node->full_name), dev_name(dev));
311 		node = node->parent;
312 	}
313 }
314 EXPORT_SYMBOL_GPL(of_device_make_bus_id);
315