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