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