xref: /freebsd/sys/dev/ofw/ofw_fdt.c (revision 716fd348e01c5f2ba125f878a634a753436c2994)
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/ctype.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39 
40 #include <contrib/libfdt/libfdt.h>
41 
42 #include <machine/stdarg.h>
43 
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/ofwvar.h>
46 #include <dev/ofw/openfirm.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include "ofw_if.h"
50 
51 #ifdef DEBUG
52 #define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
53     printf(fmt,##args); } while (0)
54 #else
55 #define debugf(fmt, args...)
56 #endif
57 
58 #if defined(__arm__)
59 #if defined(SOC_MV_ARMADAXP) || defined(SOC_MV_ARMADA38X) || \
60     defined(SOC_MV_DISCOVERY) || defined(SOC_MV_DOVE) || \
61     defined(SOC_MV_FREY) || defined(SOC_MV_KIRKWOOD) || \
62     defined(SOC_MV_LOKIPLUS) || defined(SOC_MV_ORION)
63 #define FDT_MARVELL
64 #endif
65 #endif
66 
67 static int ofw_fdt_init(ofw_t, void *);
68 static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
69 static phandle_t ofw_fdt_child(ofw_t, phandle_t);
70 static phandle_t ofw_fdt_parent(ofw_t, phandle_t);
71 static phandle_t ofw_fdt_instance_to_package(ofw_t, ihandle_t);
72 static ssize_t ofw_fdt_getproplen(ofw_t, phandle_t, const char *);
73 static ssize_t ofw_fdt_getprop(ofw_t, phandle_t, const char *, void *, size_t);
74 static int ofw_fdt_nextprop(ofw_t, phandle_t, const char *, char *, size_t);
75 static int ofw_fdt_setprop(ofw_t, phandle_t, const char *, const void *,
76     size_t);
77 static ssize_t ofw_fdt_canon(ofw_t, const char *, char *, size_t);
78 static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
79 static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
80 static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
81 static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
82 
83 static ofw_method_t ofw_fdt_methods[] = {
84 	OFWMETHOD(ofw_init,			ofw_fdt_init),
85 	OFWMETHOD(ofw_peer,			ofw_fdt_peer),
86 	OFWMETHOD(ofw_child,			ofw_fdt_child),
87 	OFWMETHOD(ofw_parent,			ofw_fdt_parent),
88 	OFWMETHOD(ofw_instance_to_package,	ofw_fdt_instance_to_package),
89 	OFWMETHOD(ofw_getproplen,		ofw_fdt_getproplen),
90 	OFWMETHOD(ofw_getprop,			ofw_fdt_getprop),
91 	OFWMETHOD(ofw_nextprop,			ofw_fdt_nextprop),
92 	OFWMETHOD(ofw_setprop,			ofw_fdt_setprop),
93 	OFWMETHOD(ofw_canon,			ofw_fdt_canon),
94 	OFWMETHOD(ofw_finddevice,		ofw_fdt_finddevice),
95 	OFWMETHOD(ofw_instance_to_path,		ofw_fdt_instance_to_path),
96 	OFWMETHOD(ofw_package_to_path,		ofw_fdt_package_to_path),
97 	OFWMETHOD(ofw_interpret,		ofw_fdt_interpret),
98 	{ 0, 0 }
99 };
100 
101 static ofw_def_t ofw_fdt = {
102 	OFW_FDT,
103 	ofw_fdt_methods,
104 	0
105 };
106 OFW_DEF(ofw_fdt);
107 
108 #define	FDT_FBSDVER_LEN	16
109 #define	FDT_MODEL_LEN	80
110 #define	FDT_COMPAT_LEN	255
111 #define	FDT_SERIAL_LEN	32
112 
113 static void *fdtp = NULL;
114 static char fdt_model[FDT_MODEL_LEN];
115 static char fdt_compatible[FDT_COMPAT_LEN];
116 static char fdt_fbsd_version[FDT_FBSDVER_LEN];
117 static char fdt_serial[FDT_SERIAL_LEN];
118 
119 static int
120 sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
121 {
122 
123         return (sysctl_handle_opaque(oidp, fdtp, fdt_totalsize(fdtp), req));
124 }
125 
126 static void
127 sysctl_register_fdt_oid(void *arg)
128 {
129 
130 	/* If there is no FDT registered, skip adding the sysctl */
131 	if (fdtp == NULL)
132 		return;
133 
134 	SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt), OID_AUTO, "dtb",
135 	    CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
136 	    sysctl_handle_dtb, "", "Device Tree Blob");
137 	if (fdt_model[0] != '\0')
138 		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
139 		    OID_AUTO, "model", CTLFLAG_RD, fdt_model,
140 		    FDT_MODEL_LEN, "System board model");
141 	if (fdt_compatible[0] != '\0')
142 		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
143 		    OID_AUTO, "compatible", CTLFLAG_RD, fdt_compatible,
144 		    FDT_COMPAT_LEN, "Compatible platforms");
145 	if (fdt_fbsd_version[0] != '\0')
146 		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
147 		    OID_AUTO, "freebsd-version", CTLFLAG_RD, fdt_fbsd_version,
148 		    FDT_FBSDVER_LEN, "FreeBSD DTS branding version");
149 	if (fdt_serial[0] != '\0')
150 		SYSCTL_ADD_STRING(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt),
151 		    OID_AUTO, "serial-number", CTLFLAG_RD, fdt_serial,
152 		    FDT_SERIAL_LEN, "Serial number");
153 }
154 SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, NULL);
155 
156 static int
157 ofw_fdt_init(ofw_t ofw, void *data)
158 {
159 	phandle_t root;
160 	ssize_t len;
161 	int err;
162 	int i;
163 
164 	/* Check FDT blob integrity */
165 	if ((err = fdt_check_header(data)) != 0)
166 		return (err);
167 
168 	fdtp = data;
169 	root = ofw_fdt_finddevice(NULL, "/");
170 	len = ofw_fdt_getproplen(NULL, root, "model");
171 	if (len > 0 && len <= FDT_MODEL_LEN) {
172 		bzero(fdt_model, FDT_MODEL_LEN);
173 		ofw_fdt_getprop(NULL, root, "model", fdt_model, FDT_MODEL_LEN);
174 	}
175 	len = ofw_fdt_getproplen(NULL, root, "compatible");
176 	if (len > 0 && len <= FDT_COMPAT_LEN) {
177 		bzero(fdt_compatible, FDT_COMPAT_LEN);
178 		ofw_fdt_getprop(NULL, root, "compatible", fdt_compatible,
179 		    FDT_COMPAT_LEN);
180 		/* Replace the middle '\0' with ' ' */
181 		for (i = 0; i < len - 1; i++)
182 			if (fdt_compatible[i] == '\0')
183 				fdt_compatible[i] = ' ';
184 	}
185 	len = ofw_fdt_getproplen(NULL, root, "freebsd,dts-version");
186 	if (len > 0 && len <= FDT_FBSDVER_LEN) {
187 		bzero(fdt_fbsd_version, FDT_FBSDVER_LEN);
188 		ofw_fdt_getprop(NULL, root, "freebsd,dts-version",
189 		  fdt_fbsd_version, FDT_FBSDVER_LEN);
190 	}
191 	len = ofw_fdt_getproplen(NULL, root, "serial-number");
192 	if (len > 0 && len <= FDT_SERIAL_LEN) {
193 		bzero(fdt_serial, FDT_SERIAL_LEN);
194 		ofw_fdt_getprop(NULL, root, "serial-number",
195 		    fdt_serial, FDT_SERIAL_LEN);
196 		/*
197 		 * Non-standard property; check for NUL-terminated
198 		 * printable string.
199 		 */
200 		for (i = 0; i < len - 1; i++) {
201 			if (!isprint(fdt_serial[i])) {
202 				fdt_serial[0] = '\0';
203 				break;
204 			}
205 		}
206 		if (fdt_serial[len - 1] != '\0')
207 			fdt_serial[0] = '\0';
208 	}
209 	return (0);
210 }
211 
212 /*
213  * Device tree functions.
214  *
215  * We use the offset from fdtp to the node as the 'phandle' in OF interface.
216  *
217  * phandle is a u32 value, therefore we cannot use the pointer to node as
218  * phandle in 64 bit. We also do not use the usual fdt offset as phandle,
219  * as it can be 0, and the OF interface has special meaning for phandle 0.
220  */
221 
222 static phandle_t
223 fdt_offset_phandle(int offset)
224 {
225 	if (offset < 0)
226 		return (0);
227 	return ((phandle_t)offset + fdt_off_dt_struct(fdtp));
228 }
229 
230 static int
231 fdt_phandle_offset(phandle_t p)
232 {
233 	int pint = (int)p;
234 	int dtoff = fdt_off_dt_struct(fdtp);
235 
236 	if (pint < dtoff)
237 		return (-1);
238 	return (pint - dtoff);
239 }
240 
241 /* Return the next sibling of this node or 0. */
242 static phandle_t
243 ofw_fdt_peer(ofw_t ofw, phandle_t node)
244 {
245 	int offset;
246 
247 	if (node == 0) {
248 		/* Find root node */
249 		offset = fdt_path_offset(fdtp, "/");
250 
251 		return (fdt_offset_phandle(offset));
252 	}
253 
254 	offset = fdt_phandle_offset(node);
255 	if (offset < 0)
256 		return (0);
257 	offset = fdt_next_subnode(fdtp, offset);
258 	return (fdt_offset_phandle(offset));
259 }
260 
261 /* Return the first child of this node or 0. */
262 static phandle_t
263 ofw_fdt_child(ofw_t ofw, phandle_t node)
264 {
265 	int offset;
266 
267 	offset = fdt_phandle_offset(node);
268 	if (offset < 0)
269 		return (0);
270 	offset = fdt_first_subnode(fdtp, offset);
271 	return (fdt_offset_phandle(offset));
272 }
273 
274 /* Return the parent of this node or 0. */
275 static phandle_t
276 ofw_fdt_parent(ofw_t ofw, phandle_t node)
277 {
278 	int offset, paroffset;
279 
280 	offset = fdt_phandle_offset(node);
281 	if (offset < 0)
282 		return (0);
283 
284 	paroffset = fdt_parent_offset(fdtp, offset);
285 	return (fdt_offset_phandle(paroffset));
286 }
287 
288 /* Return the package handle that corresponds to an instance handle. */
289 static phandle_t
290 ofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)
291 {
292 
293 	/* Where real OF uses ihandles in the tree, FDT uses xref phandles */
294 	return (OF_node_from_xref(instance));
295 }
296 
297 /* Get the length of a property of a package. */
298 static ssize_t
299 ofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)
300 {
301 	const void *prop;
302 	int offset, len;
303 
304 	offset = fdt_phandle_offset(package);
305 	if (offset < 0)
306 		return (-1);
307 
308 	len = -1;
309 	prop = fdt_getprop(fdtp, offset, propname, &len);
310 
311 	if (prop == NULL && strcmp(propname, "name") == 0) {
312 		/* Emulate the 'name' property */
313 		fdt_get_name(fdtp, offset, &len);
314 		return (len + 1);
315 	}
316 
317 	if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
318 		if (strcmp(propname, "fdtbootcpu") == 0)
319 			return (sizeof(cell_t));
320 		if (strcmp(propname, "fdtmemreserv") == 0)
321 			return (sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp));
322 	}
323 
324 	if (prop == NULL)
325 		return (-1);
326 
327 	return (len);
328 }
329 
330 /* Get the value of a property of a package. */
331 static ssize_t
332 ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
333     size_t buflen)
334 {
335 	const void *prop;
336 	const char *name;
337 	int len, offset;
338 	uint32_t cpuid;
339 
340 	offset = fdt_phandle_offset(package);
341 	if (offset < 0)
342 		return (-1);
343 
344 	prop = fdt_getprop(fdtp, offset, propname, &len);
345 
346 	if (prop == NULL && strcmp(propname, "name") == 0) {
347 		/* Emulate the 'name' property */
348 		name = fdt_get_name(fdtp, offset, &len);
349 		strncpy(buf, name, buflen);
350 		return (len + 1);
351 	}
352 
353 	if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
354 		if (strcmp(propname, "fdtbootcpu") == 0) {
355 			cpuid = cpu_to_fdt32(fdt_boot_cpuid_phys(fdtp));
356 			len = sizeof(cpuid);
357 			prop = &cpuid;
358 		}
359 		if (strcmp(propname, "fdtmemreserv") == 0) {
360 			prop = (char *)fdtp + fdt_off_mem_rsvmap(fdtp);
361 			len = sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp);
362 		}
363 	}
364 
365 	if (prop == NULL)
366 		return (-1);
367 
368 	bcopy(prop, buf, min(len, buflen));
369 
370 	return (len);
371 }
372 
373 /*
374  * Get the next property of a package. Return values:
375  *  -1: package or previous property does not exist
376  *   0: no more properties
377  *   1: success
378  */
379 static int
380 ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
381     size_t size)
382 {
383 	const void *prop;
384 	const char *name;
385 	int offset;
386 
387 	offset = fdt_phandle_offset(package);
388 	if (offset < 0)
389 		return (-1);
390 
391 	if (previous == NULL)
392 		/* Find the first prop in the node */
393 		offset = fdt_first_property_offset(fdtp, offset);
394 	else {
395 		fdt_for_each_property_offset(offset, fdtp, offset) {
396 			prop = fdt_getprop_by_offset(fdtp, offset, &name, NULL);
397 			if (prop == NULL)
398 				return (-1); /* Internal error */
399 			/* Skip until we find 'previous', then bail out */
400 			if (strcmp(name, previous) != 0)
401 				continue;
402 			offset = fdt_next_property_offset(fdtp, offset);
403 			break;
404 		}
405 	}
406 
407 	if (offset < 0)
408 		return (0); /* No properties */
409 
410 	prop = fdt_getprop_by_offset(fdtp, offset, &name, &offset);
411 	if (prop == NULL)
412 		return (-1); /* Internal error */
413 
414 	strncpy(buf, name, size);
415 
416 	return (1);
417 }
418 
419 /* Set the value of a property of a package. */
420 static int
421 ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
422     const void *buf, size_t len)
423 {
424 	int offset;
425 
426 	offset = fdt_phandle_offset(package);
427 	if (offset < 0)
428 		return (-1);
429 
430 	if (fdt_setprop_inplace(fdtp, offset, propname, buf, len) != 0)
431 		/* Try to add property, when setting value inplace failed */
432 		return (fdt_setprop(fdtp, offset, propname, buf, len));
433 
434 	return (0);
435 }
436 
437 /* Convert a device specifier to a fully qualified pathname. */
438 static ssize_t
439 ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
440 {
441 
442 	return (-1);
443 }
444 
445 /* Return a package handle for the specified device. */
446 static phandle_t
447 ofw_fdt_finddevice(ofw_t ofw, const char *device)
448 {
449 	int offset;
450 
451 	offset = fdt_path_offset(fdtp, device);
452 	if (offset < 0)
453 		return (-1);
454 	return (fdt_offset_phandle(offset));
455 }
456 
457 /* Return the fully qualified pathname corresponding to an instance. */
458 static ssize_t
459 ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
460 {
461 	phandle_t phandle;
462 
463 	phandle = OF_instance_to_package(instance);
464 	if (phandle == -1)
465 		return (-1);
466 
467 	return (OF_package_to_path(phandle, buf, len));
468 }
469 
470 /* Return the fully qualified pathname corresponding to a package. */
471 static ssize_t
472 ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
473 {
474 
475 	return (-1);
476 }
477 
478 #if defined(FDT_MARVELL)
479 static int
480 ofw_fdt_fixup(ofw_t ofw)
481 {
482 	char model[FDT_MODEL_LEN];
483 	phandle_t root;
484 	ssize_t len;
485 	int i;
486 
487 	if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)
488 		return (ENODEV);
489 
490 	if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
491 		return (0);
492 
493 	bzero(model, FDT_MODEL_LEN);
494 	if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
495 		return (0);
496 
497 	/*
498 	 * Search fixup table and call handler if appropriate.
499 	 */
500 	for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
501 		if (strncmp(model, fdt_fixup_table[i].model,
502 		    FDT_MODEL_LEN) != 0)
503 			/*
504 			 * Sometimes it's convenient to provide one
505 			 * fixup entry that refers to many boards.
506 			 * To handle this case, simply check if model
507 			 * is compatible parameter
508 			 */
509 			if(!ofw_bus_node_is_compatible(root,
510 			    fdt_fixup_table[i].model))
511 				continue;
512 
513 		if (fdt_fixup_table[i].handler != NULL)
514 			(*fdt_fixup_table[i].handler)(root);
515 	}
516 
517 	return (0);
518 }
519 #endif
520 
521 static int
522 ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)
523 {
524 #if defined(FDT_MARVELL)
525 	int rv;
526 
527 	/*
528 	 * Note: FDT does not have the possibility to 'interpret' commands,
529 	 * but we abuse the interface a bit to use it for doing non-standard
530 	 * operations on the device tree blob.
531 	 *
532 	 * Currently the only supported 'command' is to trigger performing
533 	 * fixups.
534 	 */
535 	if (strncmp("perform-fixup", cmd, 13) != 0)
536 		return (0);
537 
538 	rv = ofw_fdt_fixup(ofw);
539 	if (nret > 0)
540 		retvals[0] = rv;
541 
542 	return (rv);
543 #else
544 	return (0);
545 #endif
546 }
547