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