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