xref: /freebsd-13.1/sys/dev/ofw/ofw_fdt.c (revision eaeff159)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009-2010 The FreeBSD Foundation
5  *
6  * This software was developed by Semihalf under sponsorship from
7  * the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/systm.h>
38 
39 #include <contrib/libfdt/libfdt.h>
40 
41 #include <machine/stdarg.h>
42 
43 #include <dev/fdt/fdt_common.h>
44 #include <dev/ofw/ofwvar.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 
48 #include "ofw_if.h"
49 
50 #ifdef DEBUG
51 #define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
52     printf(fmt,##args); } while (0)
53 #else
54 #define debugf(fmt, args...)
55 #endif
56 
57 #if defined(__arm__)
58 #if defined(SOC_MV_ARMADAXP) || defined(SOC_MV_ARMADA38X) || \
59     defined(SOC_MV_DISCOVERY) || defined(SOC_MV_DOVE) || \
60     defined(SOC_MV_FREY) || defined(SOC_MV_KIRKWOOD) || \
61     defined(SOC_MV_LOKIPLUS) || defined(SOC_MV_ORION)
62 #define FDT_MARVELL
63 #endif
64 #endif
65 
66 static int ofw_fdt_init(ofw_t, void *);
67 static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
68 static phandle_t ofw_fdt_child(ofw_t, phandle_t);
69 static phandle_t ofw_fdt_parent(ofw_t, phandle_t);
70 static phandle_t ofw_fdt_instance_to_package(ofw_t, ihandle_t);
71 static ssize_t ofw_fdt_getproplen(ofw_t, phandle_t, const char *);
72 static ssize_t ofw_fdt_getprop(ofw_t, phandle_t, const char *, void *, size_t);
73 static int ofw_fdt_nextprop(ofw_t, phandle_t, const char *, char *, size_t);
74 static int ofw_fdt_setprop(ofw_t, phandle_t, const char *, const void *,
75     size_t);
76 static ssize_t ofw_fdt_canon(ofw_t, const char *, char *, size_t);
77 static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
78 static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
79 static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
80 static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
81 
82 static ofw_method_t ofw_fdt_methods[] = {
83 	OFWMETHOD(ofw_init,			ofw_fdt_init),
84 	OFWMETHOD(ofw_peer,			ofw_fdt_peer),
85 	OFWMETHOD(ofw_child,			ofw_fdt_child),
86 	OFWMETHOD(ofw_parent,			ofw_fdt_parent),
87 	OFWMETHOD(ofw_instance_to_package,	ofw_fdt_instance_to_package),
88 	OFWMETHOD(ofw_getproplen,		ofw_fdt_getproplen),
89 	OFWMETHOD(ofw_getprop,			ofw_fdt_getprop),
90 	OFWMETHOD(ofw_nextprop,			ofw_fdt_nextprop),
91 	OFWMETHOD(ofw_setprop,			ofw_fdt_setprop),
92 	OFWMETHOD(ofw_canon,			ofw_fdt_canon),
93 	OFWMETHOD(ofw_finddevice,		ofw_fdt_finddevice),
94 	OFWMETHOD(ofw_instance_to_path,		ofw_fdt_instance_to_path),
95 	OFWMETHOD(ofw_package_to_path,		ofw_fdt_package_to_path),
96 	OFWMETHOD(ofw_interpret,		ofw_fdt_interpret),
97 	{ 0, 0 }
98 };
99 
100 static ofw_def_t ofw_fdt = {
101 	OFW_FDT,
102 	ofw_fdt_methods,
103 	0
104 };
105 OFW_DEF(ofw_fdt);
106 
107 static void *fdtp = NULL;
108 
109 static int
110 sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
111 {
112 
113         return (sysctl_handle_opaque(oidp, fdtp, fdt_totalsize(fdtp), req));
114 }
115 
116 static void
117 sysctl_register_fdt_oid(void *arg)
118 {
119 
120 	/* If there is no FDT registered, skip adding the sysctl */
121 	if (fdtp == NULL)
122 		return;
123 
124 	SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt), OID_AUTO, "dtb",
125 	    CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
126 	    sysctl_handle_dtb, "", "Device Tree Blob");
127 }
128 SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, NULL);
129 
130 static int
131 ofw_fdt_init(ofw_t ofw, void *data)
132 {
133 	int err;
134 
135 	/* Check FDT blob integrity */
136 	if ((err = fdt_check_header(data)) != 0)
137 		return (err);
138 
139 	fdtp = data;
140 	return (0);
141 }
142 
143 /*
144  * Device tree functions.
145  *
146  * We use the offset from fdtp to the node as the 'phandle' in OF interface.
147  *
148  * phandle is a u32 value, therefore we cannot use the pointer to node as
149  * phandle in 64 bit. We also do not use the usual fdt offset as phandle,
150  * as it can be 0, and the OF interface has special meaning for phandle 0.
151  */
152 
153 static phandle_t
154 fdt_offset_phandle(int offset)
155 {
156 	if (offset < 0)
157 		return (0);
158 	return ((phandle_t)offset + fdt_off_dt_struct(fdtp));
159 }
160 
161 static int
162 fdt_phandle_offset(phandle_t p)
163 {
164 	int pint = (int)p;
165 	int dtoff = fdt_off_dt_struct(fdtp);
166 
167 	if (pint < dtoff)
168 		return (-1);
169 	return (pint - dtoff);
170 }
171 
172 /* Return the next sibling of this node or 0. */
173 static phandle_t
174 ofw_fdt_peer(ofw_t ofw, phandle_t node)
175 {
176 	int offset;
177 
178 	if (node == 0) {
179 		/* Find root node */
180 		offset = fdt_path_offset(fdtp, "/");
181 
182 		return (fdt_offset_phandle(offset));
183 	}
184 
185 	offset = fdt_phandle_offset(node);
186 	if (offset < 0)
187 		return (0);
188 	offset = fdt_next_subnode(fdtp, offset);
189 	return (fdt_offset_phandle(offset));
190 }
191 
192 /* Return the first child of this node or 0. */
193 static phandle_t
194 ofw_fdt_child(ofw_t ofw, phandle_t node)
195 {
196 	int offset;
197 
198 	offset = fdt_phandle_offset(node);
199 	if (offset < 0)
200 		return (0);
201 	offset = fdt_first_subnode(fdtp, offset);
202 	return (fdt_offset_phandle(offset));
203 }
204 
205 /* Return the parent of this node or 0. */
206 static phandle_t
207 ofw_fdt_parent(ofw_t ofw, phandle_t node)
208 {
209 	int offset, paroffset;
210 
211 	offset = fdt_phandle_offset(node);
212 	if (offset < 0)
213 		return (0);
214 
215 	paroffset = fdt_parent_offset(fdtp, offset);
216 	return (fdt_offset_phandle(paroffset));
217 }
218 
219 /* Return the package handle that corresponds to an instance handle. */
220 static phandle_t
221 ofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)
222 {
223 
224 	/* Where real OF uses ihandles in the tree, FDT uses xref phandles */
225 	return (OF_node_from_xref(instance));
226 }
227 
228 /* Get the length of a property of a package. */
229 static ssize_t
230 ofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)
231 {
232 	const void *prop;
233 	int offset, len;
234 
235 	offset = fdt_phandle_offset(package);
236 	if (offset < 0)
237 		return (-1);
238 
239 	len = -1;
240 	prop = fdt_getprop(fdtp, offset, propname, &len);
241 
242 	if (prop == NULL && strcmp(propname, "name") == 0) {
243 		/* Emulate the 'name' property */
244 		fdt_get_name(fdtp, offset, &len);
245 		return (len + 1);
246 	}
247 
248 	if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
249 		if (strcmp(propname, "fdtbootcpu") == 0)
250 			return (sizeof(cell_t));
251 		if (strcmp(propname, "fdtmemreserv") == 0)
252 			return (sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp));
253 	}
254 
255 	if (prop == NULL)
256 		return (-1);
257 
258 	return (len);
259 }
260 
261 /* Get the value of a property of a package. */
262 static ssize_t
263 ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
264     size_t buflen)
265 {
266 	const void *prop;
267 	const char *name;
268 	int len, offset;
269 	uint32_t cpuid;
270 
271 	offset = fdt_phandle_offset(package);
272 	if (offset < 0)
273 		return (-1);
274 
275 	prop = fdt_getprop(fdtp, offset, propname, &len);
276 
277 	if (prop == NULL && strcmp(propname, "name") == 0) {
278 		/* Emulate the 'name' property */
279 		name = fdt_get_name(fdtp, offset, &len);
280 		strncpy(buf, name, buflen);
281 		return (len + 1);
282 	}
283 
284 	if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
285 		if (strcmp(propname, "fdtbootcpu") == 0) {
286 			cpuid = cpu_to_fdt32(fdt_boot_cpuid_phys(fdtp));
287 			len = sizeof(cpuid);
288 			prop = &cpuid;
289 		}
290 		if (strcmp(propname, "fdtmemreserv") == 0) {
291 			prop = (char *)fdtp + fdt_off_mem_rsvmap(fdtp);
292 			len = sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp);
293 		}
294 	}
295 
296 	if (prop == NULL)
297 		return (-1);
298 
299 	bcopy(prop, buf, min(len, buflen));
300 
301 	return (len);
302 }
303 
304 /*
305  * Get the next property of a package. Return values:
306  *  -1: package or previous property does not exist
307  *   0: no more properties
308  *   1: success
309  */
310 static int
311 ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
312     size_t size)
313 {
314 	const void *prop;
315 	const char *name;
316 	int offset;
317 
318 	offset = fdt_phandle_offset(package);
319 	if (offset < 0)
320 		return (-1);
321 
322 	if (previous == NULL)
323 		/* Find the first prop in the node */
324 		offset = fdt_first_property_offset(fdtp, offset);
325 	else {
326 		fdt_for_each_property_offset(offset, fdtp, offset) {
327 			prop = fdt_getprop_by_offset(fdtp, offset, &name, NULL);
328 			if (prop == NULL)
329 				return (-1); /* Internal error */
330 			/* Skip until we find 'previous', then bail out */
331 			if (strcmp(name, previous) != 0)
332 				continue;
333 			offset = fdt_next_property_offset(fdtp, offset);
334 			break;
335 		}
336 	}
337 
338 	if (offset < 0)
339 		return (0); /* No properties */
340 
341 	prop = fdt_getprop_by_offset(fdtp, offset, &name, &offset);
342 	if (prop == NULL)
343 		return (-1); /* Internal error */
344 
345 	strncpy(buf, name, size);
346 
347 	return (1);
348 }
349 
350 /* Set the value of a property of a package. */
351 static int
352 ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
353     const void *buf, size_t len)
354 {
355 	int offset;
356 
357 	offset = fdt_phandle_offset(package);
358 	if (offset < 0)
359 		return (-1);
360 
361 	if (fdt_setprop_inplace(fdtp, offset, propname, buf, len) != 0)
362 		/* Try to add property, when setting value inplace failed */
363 		return (fdt_setprop(fdtp, offset, propname, buf, len));
364 
365 	return (0);
366 }
367 
368 /* Convert a device specifier to a fully qualified pathname. */
369 static ssize_t
370 ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
371 {
372 
373 	return (-1);
374 }
375 
376 /* Return a package handle for the specified device. */
377 static phandle_t
378 ofw_fdt_finddevice(ofw_t ofw, const char *device)
379 {
380 	int offset;
381 
382 	offset = fdt_path_offset(fdtp, device);
383 	if (offset < 0)
384 		return (-1);
385 	return (fdt_offset_phandle(offset));
386 }
387 
388 /* Return the fully qualified pathname corresponding to an instance. */
389 static ssize_t
390 ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
391 {
392 	phandle_t phandle;
393 
394 	phandle = OF_instance_to_package(instance);
395 	if (phandle == -1)
396 		return (-1);
397 
398 	return (OF_package_to_path(phandle, buf, len));
399 }
400 
401 /* Return the fully qualified pathname corresponding to a package. */
402 static ssize_t
403 ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
404 {
405 
406 	return (-1);
407 }
408 
409 #if defined(FDT_MARVELL)
410 static int
411 ofw_fdt_fixup(ofw_t ofw)
412 {
413 #define FDT_MODEL_LEN	80
414 	char model[FDT_MODEL_LEN];
415 	phandle_t root;
416 	ssize_t len;
417 	int i;
418 
419 	if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)
420 		return (ENODEV);
421 
422 	if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
423 		return (0);
424 
425 	bzero(model, FDT_MODEL_LEN);
426 	if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
427 		return (0);
428 
429 	/*
430 	 * Search fixup table and call handler if appropriate.
431 	 */
432 	for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
433 		if (strncmp(model, fdt_fixup_table[i].model,
434 		    FDT_MODEL_LEN) != 0)
435 			/*
436 			 * Sometimes it's convenient to provide one
437 			 * fixup entry that refers to many boards.
438 			 * To handle this case, simply check if model
439 			 * is compatible parameter
440 			 */
441 			if(!ofw_bus_node_is_compatible(root,
442 			    fdt_fixup_table[i].model))
443 				continue;
444 
445 		if (fdt_fixup_table[i].handler != NULL)
446 			(*fdt_fixup_table[i].handler)(root);
447 	}
448 
449 	return (0);
450 }
451 #endif
452 
453 static int
454 ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)
455 {
456 #if defined(FDT_MARVELL)
457 	int rv;
458 
459 	/*
460 	 * Note: FDT does not have the possibility to 'interpret' commands,
461 	 * but we abuse the interface a bit to use it for doing non-standard
462 	 * operations on the device tree blob.
463 	 *
464 	 * Currently the only supported 'command' is to trigger performing
465 	 * fixups.
466 	 */
467 	if (strncmp("perform-fixup", cmd, 13) != 0)
468 		return (0);
469 
470 	rv = ofw_fdt_fixup(ofw);
471 	if (nret > 0)
472 		retvals[0] = rv;
473 
474 	return (rv);
475 #else
476 	return (0);
477 #endif
478 }
479