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