ofw_fdt.c revision 265967
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: stable/10/sys/dev/ofw/ofw_fdt.c 265967 2014-05-13 17:59:17Z ian $");
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
55static int ofw_fdt_init(ofw_t, void *);
56static phandle_t ofw_fdt_peer(ofw_t, phandle_t);
57static phandle_t ofw_fdt_child(ofw_t, phandle_t);
58static phandle_t ofw_fdt_parent(ofw_t, phandle_t);
59static phandle_t ofw_fdt_instance_to_package(ofw_t, ihandle_t);
60static ssize_t ofw_fdt_getproplen(ofw_t, phandle_t, const char *);
61static ssize_t ofw_fdt_getprop(ofw_t, phandle_t, const char *, void *, size_t);
62static int ofw_fdt_nextprop(ofw_t, phandle_t, const char *, char *, size_t);
63static int ofw_fdt_setprop(ofw_t, phandle_t, const char *, const void *,
64    size_t);
65static ssize_t ofw_fdt_canon(ofw_t, const char *, char *, size_t);
66static phandle_t ofw_fdt_finddevice(ofw_t, const char *);
67static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);
68static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);
69static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);
70
71static 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
89static ofw_def_t ofw_fdt = {
90	OFW_FDT,
91	ofw_fdt_methods,
92	0
93};
94OFW_DEF(ofw_fdt);
95
96static void *fdtp = NULL;
97
98static int
99ofw_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
121static phandle_t
122fdt_offset_phandle(int offset)
123{
124	if (offset < 0)
125		return (0);
126	return ((phandle_t)offset + fdt_off_dt_struct(fdtp));
127}
128
129static int
130fdt_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
140static int
141fdt_pointer_offset(const void *ptr)
142{
143	uintptr_t dt_struct, p;
144	int offset;
145
146	p = (uintptr_t)ptr;
147	dt_struct = (uintptr_t)fdtp + fdt_off_dt_struct(fdtp);
148
149	if ((p < dt_struct) ||
150	    p > (dt_struct + fdt_size_dt_struct(fdtp)))
151		return (-1);
152
153	offset = p - dt_struct;
154	if (offset < 0)
155		return (-1);
156
157	return (offset);
158}
159
160/* Return the next sibling of this node or 0. */
161static phandle_t
162ofw_fdt_peer(ofw_t ofw, phandle_t node)
163{
164	int depth, offset;
165
166	if (node == 0) {
167		/* Find root node */
168		offset = fdt_path_offset(fdtp, "/");
169
170		return (fdt_offset_phandle(offset));
171	}
172
173	offset = fdt_phandle_offset(node);
174	if (offset < 0)
175		return (0);
176
177	for (depth = 1, offset = fdt_next_node(fdtp, offset, &depth);
178	    offset >= 0;
179	    offset = fdt_next_node(fdtp, offset, &depth)) {
180		if (depth < 0)
181			return (0);
182		if (depth == 1)
183			return (fdt_offset_phandle(offset));
184	}
185
186	return (0);
187}
188
189/* Return the first child of this node or 0. */
190static phandle_t
191ofw_fdt_child(ofw_t ofw, phandle_t node)
192{
193	int depth, offset;
194
195	offset = fdt_phandle_offset(node);
196	if (offset < 0)
197		return (0);
198
199	for (depth = 0, offset = fdt_next_node(fdtp, offset, &depth);
200	    (offset >= 0) && (depth > 0);
201	    offset = fdt_next_node(fdtp, offset, &depth)) {
202		if (depth < 0)
203			return (0);
204		if (depth == 1)
205			return (fdt_offset_phandle(offset));
206	}
207
208	return (0);
209}
210
211/* Return the parent of this node or 0. */
212static phandle_t
213ofw_fdt_parent(ofw_t ofw, phandle_t node)
214{
215	int offset, paroffset;
216
217	offset = fdt_phandle_offset(node);
218	if (offset < 0)
219		return (0);
220
221	paroffset = fdt_parent_offset(fdtp, offset);
222	return (fdt_offset_phandle(paroffset));
223}
224
225/* Return the package handle that corresponds to an instance handle. */
226static phandle_t
227ofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)
228{
229
230	return (-1);
231}
232
233/* Get the length of a property of a package. */
234static ssize_t
235ofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)
236{
237	const struct fdt_property *prop;
238	int offset, len;
239
240	offset = fdt_phandle_offset(package);
241	if (offset < 0)
242		return (-1);
243
244	if (strcmp(propname, "name") == 0) {
245		/* Emulate the 'name' property */
246		fdt_get_name(fdtp, offset, &len);
247		return (len + 1);
248	}
249
250	len = -1;
251	prop = fdt_get_property(fdtp, offset, propname, &len);
252
253	return (len);
254}
255
256/* Get the value of a property of a package. */
257static ssize_t
258ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
259    size_t buflen)
260{
261	const void *prop;
262	const char *name;
263	int len, offset;
264
265	offset = fdt_phandle_offset(package);
266	if (offset < 0)
267		return (-1);
268
269	if (strcmp(propname, "name") == 0) {
270		/* Emulate the 'name' property */
271		name = fdt_get_name(fdtp, offset, &len);
272		strncpy(buf, name, buflen);
273		if (len + 1 > buflen)
274			len = buflen;
275		return (len + 1);
276	}
277
278	prop = fdt_getprop(fdtp, offset, propname, &len);
279	if (prop == NULL)
280		return (-1);
281
282	if (len > buflen)
283		len = buflen;
284	bcopy(prop, buf, len);
285	return (len);
286}
287
288static int
289fdt_nextprop(int offset, char *buf, size_t size)
290{
291	const struct fdt_property *prop;
292	const char *name;
293	uint32_t tag;
294	int nextoffset, depth;
295
296	depth = 0;
297	tag = fdt_next_tag(fdtp, offset, &nextoffset);
298
299	/* Find the next prop */
300	do {
301		offset = nextoffset;
302		tag = fdt_next_tag(fdtp, offset, &nextoffset);
303
304		if (tag == FDT_BEGIN_NODE)
305			depth++;
306		else if (tag == FDT_END_NODE)
307			depth--;
308		else if ((tag == FDT_PROP) && (depth == 0)) {
309			prop =
310			    (const struct fdt_property *)fdt_offset_ptr(fdtp,
311			    offset, sizeof(*prop));
312			name = fdt_string(fdtp,
313			    fdt32_to_cpu(prop->nameoff));
314			strncpy(buf, name, size);
315			return (strlen(name));
316		} else
317			depth = -1;
318	} while (depth >= 0);
319
320	return (0);
321}
322
323/*
324 * Get the next property of a package. Return the actual len of retrieved
325 * prop name.
326 */
327static int
328ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
329    size_t size)
330{
331	const struct fdt_property *prop;
332	int offset, rv;
333
334	offset = fdt_phandle_offset(package);
335	if (offset < 0)
336		return (-1);
337
338	if (previous == NULL)
339		/* Find the first prop in the node */
340		return (fdt_nextprop(offset, buf, size));
341
342	/*
343	 * Advance to the previous prop
344	 */
345	prop = fdt_get_property(fdtp, offset, previous, NULL);
346	if (prop == NULL)
347		return (-1);
348
349	offset = fdt_pointer_offset(prop);
350	rv = fdt_nextprop(offset, buf, size);
351	return (rv);
352}
353
354/* Set the value of a property of a package. */
355static int
356ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,
357    const void *buf, size_t len)
358{
359	int offset;
360
361	offset = fdt_phandle_offset(package);
362	if (offset < 0)
363		return (-1);
364
365	return (fdt_setprop_inplace(fdtp, offset, propname, buf, len));
366}
367
368/* Convert a device specifier to a fully qualified pathname. */
369static ssize_t
370ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)
371{
372
373	return (-1);
374}
375
376/* Return a package handle for the specified device. */
377static phandle_t
378ofw_fdt_finddevice(ofw_t ofw, const char *device)
379{
380	int offset;
381
382	offset = fdt_path_offset(fdtp, device);
383	if (offset < 0)
384		return (-1);
385	return (fdt_offset_phandle(offset));
386}
387
388/* Return the fully qualified pathname corresponding to an instance. */
389static ssize_t
390ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
391{
392
393	return (-1);
394}
395
396/* Return the fully qualified pathname corresponding to a package. */
397static ssize_t
398ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
399{
400
401	return (-1);
402}
403
404static int
405ofw_fdt_fixup(ofw_t ofw)
406{
407#define FDT_MODEL_LEN	80
408	char model[FDT_MODEL_LEN];
409	phandle_t root;
410	ssize_t len;
411	int i;
412
413	if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)
414		return (ENODEV);
415
416	if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)
417		return (0);
418
419	bzero(model, FDT_MODEL_LEN);
420	if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)
421		return (0);
422
423	/*
424	 * Search fixup table and call handler if appropriate.
425	 */
426	for (i = 0; fdt_fixup_table[i].model != NULL; i++) {
427		if (strncmp(model, fdt_fixup_table[i].model,
428		    FDT_MODEL_LEN) != 0)
429			continue;
430
431		if (fdt_fixup_table[i].handler != NULL)
432			(*fdt_fixup_table[i].handler)(root);
433	}
434
435	return (0);
436}
437
438static int
439ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)
440{
441	int rv;
442
443	/*
444	 * Note: FDT does not have the possibility to 'interpret' commands,
445	 * but we abuse the interface a bit to use it for doing non-standard
446	 * operations on the device tree blob.
447	 *
448	 * Currently the only supported 'command' is to trigger performing
449	 * fixups.
450	 */
451	if (strncmp("perform-fixup", cmd, 13) != 0)
452		return (0);
453
454	rv = ofw_fdt_fixup(ofw);
455	if (nret > 0)
456		retvals[0] = rv;
457
458	return (rv);
459}
460