xref: /freebsd/sys/dev/ofw/ofw_fdt.c (revision f0cfa1b168014f56c02b83e5f28412cc5f78d117)
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 static void *fdtp = NULL;
109 
110 static int
111 sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)
112 {
113 
114         return (sysctl_handle_opaque(oidp, fdtp, fdt_totalsize(fdtp), req));
115 }
116 
117 static void
118 sysctl_register_fdt_oid(void *arg)
119 {
120 
121 	/* If there is no FDT registered, skip adding the sysctl */
122 	if (fdtp == NULL)
123 		return;
124 
125 	SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt), OID_AUTO, "dtb",
126 	    CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0, sysctl_handle_dtb, "",
127 	    "Device Tree Blob");
128 }
129 SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, 0);
130 
131 static int
132 ofw_fdt_init(ofw_t ofw, void *data)
133 {
134 	int err;
135 
136 	/* Check FDT blob integrity */
137 	if ((err = fdt_check_header(data)) != 0)
138 		return (err);
139 
140 	fdtp = data;
141 	return (0);
142 }
143 
144 /*
145  * Device tree functions.
146  *
147  * We use the offset from fdtp to the node as the 'phandle' in OF interface.
148  *
149  * phandle is a u32 value, therefore we cannot use the pointer to node as
150  * phandle in 64 bit. We also do not use the usual fdt offset as phandle,
151  * as it can be 0, and the OF interface has special meaning for phandle 0.
152  */
153 
154 static phandle_t
155 fdt_offset_phandle(int offset)
156 {
157 	if (offset < 0)
158 		return (0);
159 	return ((phandle_t)offset + fdt_off_dt_struct(fdtp));
160 }
161 
162 static int
163 fdt_phandle_offset(phandle_t p)
164 {
165 	int pint = (int)p;
166 	int dtoff = fdt_off_dt_struct(fdtp);
167 
168 	if (pint < dtoff)
169 		return (-1);
170 	return (pint - dtoff);
171 }
172 
173 /* Return the next sibling of this node or 0. */
174 static phandle_t
175 ofw_fdt_peer(ofw_t ofw, phandle_t node)
176 {
177 	int depth, offset;
178 
179 	if (node == 0) {
180 		/* Find root node */
181 		offset = fdt_path_offset(fdtp, "/");
182 
183 		return (fdt_offset_phandle(offset));
184 	}
185 
186 	offset = fdt_phandle_offset(node);
187 	if (offset < 0)
188 		return (0);
189 
190 	for (depth = 1, offset = fdt_next_node(fdtp, offset, &depth);
191 	    offset >= 0;
192 	    offset = fdt_next_node(fdtp, offset, &depth)) {
193 		if (depth < 0)
194 			return (0);
195 		if (depth == 1)
196 			return (fdt_offset_phandle(offset));
197 	}
198 
199 	return (0);
200 }
201 
202 /* Return the first child of this node or 0. */
203 static phandle_t
204 ofw_fdt_child(ofw_t ofw, phandle_t node)
205 {
206 	int depth, offset;
207 
208 	offset = fdt_phandle_offset(node);
209 	if (offset < 0)
210 		return (0);
211 
212 	for (depth = 0, offset = fdt_next_node(fdtp, offset, &depth);
213 	    (offset >= 0) && (depth > 0);
214 	    offset = fdt_next_node(fdtp, offset, &depth)) {
215 		if (depth < 0)
216 			return (0);
217 		if (depth == 1)
218 			return (fdt_offset_phandle(offset));
219 	}
220 
221 	return (0);
222 }
223 
224 /* Return the parent of this node or 0. */
225 static phandle_t
226 ofw_fdt_parent(ofw_t ofw, phandle_t node)
227 {
228 	int offset, paroffset;
229 
230 	offset = fdt_phandle_offset(node);
231 	if (offset < 0)
232 		return (0);
233 
234 	paroffset = fdt_parent_offset(fdtp, offset);
235 	return (fdt_offset_phandle(paroffset));
236 }
237 
238 /* Return the package handle that corresponds to an instance handle. */
239 static phandle_t
240 ofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)
241 {
242 
243 	/* Where real OF uses ihandles in the tree, FDT uses xref phandles */
244 	return (OF_node_from_xref(instance));
245 }
246 
247 /* Get the length of a property of a package. */
248 static ssize_t
249 ofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)
250 {
251 	const struct fdt_property *prop;
252 	int offset, len;
253 
254 	offset = fdt_phandle_offset(package);
255 	if (offset < 0)
256 		return (-1);
257 
258 	len = -1;
259 	prop = fdt_get_property(fdtp, offset, propname, &len);
260 
261 	if (prop == NULL && strcmp(propname, "name") == 0) {
262 		/* Emulate the 'name' property */
263 		fdt_get_name(fdtp, offset, &len);
264 		return (len + 1);
265 	}
266 
267 	if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
268 		if (strcmp(propname, "fdtbootcpu") == 0)
269 			return (sizeof(cell_t));
270 		if (strcmp(propname, "fdtmemreserv") == 0)
271 			return (sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp));
272 	}
273 
274 	if (prop == NULL)
275 		return (-1);
276 
277 	return (len);
278 }
279 
280 /* Get the value of a property of a package. */
281 static ssize_t
282 ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
283     size_t buflen)
284 {
285 	const void *prop;
286 	const char *name;
287 	int len, offset;
288 	uint32_t cpuid;
289 
290 	offset = fdt_phandle_offset(package);
291 	if (offset < 0)
292 		return (-1);
293 
294 	prop = fdt_getprop(fdtp, offset, propname, &len);
295 
296 	if (prop == NULL && strcmp(propname, "name") == 0) {
297 		/* Emulate the 'name' property */
298 		name = fdt_get_name(fdtp, offset, &len);
299 		strncpy(buf, name, buflen);
300 		if (len + 1 > buflen)
301 			len = buflen;
302 		return (len + 1);
303 	}
304 
305 	if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {
306 		if (strcmp(propname, "fdtbootcpu") == 0) {
307 			cpuid = cpu_to_fdt32(fdt_boot_cpuid_phys(fdtp));
308 			len = sizeof(cpuid);
309 			prop = &cpuid;
310 		}
311 		if (strcmp(propname, "fdtmemreserv") == 0) {
312 			prop = (char *)fdtp + fdt_off_mem_rsvmap(fdtp);
313 			len = sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp);
314 		}
315 	}
316 
317 	if (prop == NULL)
318 		return (-1);
319 
320 	if (len > buflen)
321 		len = buflen;
322 	bcopy(prop, buf, len);
323 	return (len);
324 }
325 
326 /*
327  * Get the next property of a package. Return values:
328  *  -1: package or previous property does not exist
329  *   0: no more properties
330  *   1: success
331  */
332 static int
333 ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
334     size_t size)
335 {
336 	const struct fdt_property *prop;
337 	const char *name;
338 	int offset;
339 
340 	offset = fdt_phandle_offset(package);
341 	if (offset < 0)
342 		return (-1);
343 
344 	/* Find the first prop in the node */
345 	offset = fdt_first_property_offset(fdtp, offset);
346 	if (offset < 0)
347 		return (0); /* No properties */
348 
349 	if (previous != NULL) {
350 		while (offset >= 0) {
351 			prop = fdt_get_property_by_offset(fdtp, offset, NULL);
352 			if (prop == NULL)
353 				return (-1); /* Internal error */
354 
355 			offset = fdt_next_property_offset(fdtp, offset);
356 			if (offset < 0)
357 				return (0); /* No more properties */
358 
359 			/* Check if the last one was the one we wanted */
360 			name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
361 			if (strcmp(name, previous) == 0)
362 				break;
363 		}
364 	}
365 
366 	prop = fdt_get_property_by_offset(fdtp, offset, &offset);
367 	if (prop == NULL)
368 		return (-1); /* Internal error */
369 
370 	strncpy(buf, fdt_string(fdtp, fdt32_to_cpu(prop->nameoff)), size);
371 
372 	return (1);
373 }
374 
375 /* Set the value of a property of a package. */
376 static int
377 ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
378     const void *buf, size_t len)
379 {
380 	int offset;
381 
382 	offset = fdt_phandle_offset(package);
383 	if (offset < 0)
384 		return (-1);
385 
386 	if (fdt_setprop_inplace(fdtp, offset, propname, buf, len) != 0)
387 		/* Try to add property, when setting value inplace failed */
388 		return (fdt_setprop(fdtp, offset, propname, buf, len));
389 
390 	return (0);
391 }
392 
393 /* Convert a device specifier to a fully qualified pathname. */
394 static ssize_t
395 ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
396 {
397 
398 	return (-1);
399 }
400 
401 /* Return a package handle for the specified device. */
402 static phandle_t
403 ofw_fdt_finddevice(ofw_t ofw, const char *device)
404 {
405 	int offset;
406 
407 	offset = fdt_path_offset(fdtp, device);
408 	if (offset < 0)
409 		return (-1);
410 	return (fdt_offset_phandle(offset));
411 }
412 
413 /* Return the fully qualified pathname corresponding to an instance. */
414 static ssize_t
415 ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
416 {
417 	phandle_t phandle;
418 
419 	phandle = OF_instance_to_package(instance);
420 	if (phandle == -1)
421 		return (-1);
422 
423 	return (OF_package_to_path(phandle, buf, len));
424 }
425 
426 /* Return the fully qualified pathname corresponding to a package. */
427 static ssize_t
428 ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
429 {
430 
431 	return (-1);
432 }
433 
434 #if defined(FDT_MARVELL) || defined(__powerpc__)
435 static int
436 ofw_fdt_fixup(ofw_t ofw)
437 {
438 #define FDT_MODEL_LEN	80
439 	char model[FDT_MODEL_LEN];
440 	phandle_t root;
441 	ssize_t len;
442 	int i;
443 
444 	if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)
445 		return (ENODEV);
446 
447 	if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
448 		return (0);
449 
450 	bzero(model, FDT_MODEL_LEN);
451 	if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
452 		return (0);
453 
454 	/*
455 	 * Search fixup table and call handler if appropriate.
456 	 */
457 	for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
458 		if (strncmp(model, fdt_fixup_table[i].model,
459 		    FDT_MODEL_LEN) != 0)
460 			/*
461 			 * Sometimes it's convenient to provide one
462 			 * fixup entry that refers to many boards.
463 			 * To handle this case, simply check if model
464 			 * is compatible parameter
465 			 */
466 			if(!ofw_bus_node_is_compatible(root,
467 			    fdt_fixup_table[i].model))
468 				continue;
469 
470 		if (fdt_fixup_table[i].handler != NULL)
471 			(*fdt_fixup_table[i].handler)(root);
472 	}
473 
474 	return (0);
475 }
476 #endif
477 
478 static int
479 ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)
480 {
481 #if defined(FDT_MARVELL) || defined(__powerpc__)
482 	int rv;
483 
484 	/*
485 	 * Note: FDT does not have the possibility to 'interpret' commands,
486 	 * but we abuse the interface a bit to use it for doing non-standard
487 	 * operations on the device tree blob.
488 	 *
489 	 * Currently the only supported 'command' is to trigger performing
490 	 * fixups.
491 	 */
492 	if (strncmp("perform-fixup", cmd, 13) != 0)
493 		return (0);
494 
495 	rv = ofw_fdt_fixup(ofw);
496 	if (nret > 0)
497 		retvals[0] = rv;
498 
499 	return (rv);
500 #else
501 	return (0);
502 #endif
503 }
504