<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:small"><br></div></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 29, 2020 at 9:24 PM Gedare Bloom <<a href="mailto:gedare@rtems.org">gedare@rtems.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I'm a little concerned about namespace pollution.<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">Can you please explain about namespace pollution?</div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
On Wed, Jul 15, 2020 at 12:32 AM G S Niteesh Babu <<a href="mailto:niteesh.gs@gmail.com" target="_blank">niteesh.gs@gmail.com</a>> wrote:<br>
><br>
> freebsd head: b8c57b4<br>
><br>
> The following files have been imported from FreeBSD to implement<br>
> OF_* functions into RTEMS.<br>
> 1) openfirm.h<br>
> 2) openfirm.c<br>
> 3) ofw_fdt.c<br>
> ---<br>
> cpukit/include/dev/ofw/openfirm.h | 187 ++++<br>
> cpukit/libfreebsd/freebsd/dev/ofw/ofw_fdt.c | 479 +++++++++++<br>
> cpukit/libfreebsd/freebsd/dev/ofw/openfirm.c | 848 +++++++++++++++++++<br>
> 3 files changed, 1514 insertions(+)<br>
> create mode 100644 cpukit/include/dev/ofw/openfirm.h<br>
> create mode 100644 cpukit/libfreebsd/freebsd/dev/ofw/ofw_fdt.c<br>
> create mode 100644 cpukit/libfreebsd/freebsd/dev/ofw/openfirm.c<br>
><br>
> diff --git a/cpukit/include/dev/ofw/openfirm.h b/cpukit/include/dev/ofw/openfirm.h<br>
> new file mode 100644<br>
> index 0000000000..74a7075367<br>
> --- /dev/null<br>
> +++ b/cpukit/include/dev/ofw/openfirm.h<br>
> @@ -0,0 +1,187 @@<br>
> +/* $NetBSD: openfirm.h,v 1.1 1998/05/15 10:16:00 tsubai Exp $ */<br>
> +<br>
> +/*-<br>
> + * SPDX-License-Identifier: (BSD-4-Clause AND BSD-2-Clause-FreeBSD)<br>
> + *<br>
> + * Copyright (C) 1995, 1996 Wolfgang Solfrank.<br>
> + * Copyright (C) 1995, 1996 TooLs GmbH.<br>
> + * All rights reserved.<br>
> + *<br>
> + * Redistribution and use in source and binary forms, with or without<br>
> + * modification, are permitted provided that the following conditions<br>
> + * are met:<br>
> + * 1. Redistributions of source code must retain the above copyright<br>
> + * notice, this list of conditions and the following disclaimer.<br>
> + * 2. Redistributions in binary form must reproduce the above copyright<br>
> + * notice, this list of conditions and the following disclaimer in the<br>
> + * documentation and/or other materials provided with the distribution.<br>
> + * 3. All advertising materials mentioning features or use of this software<br>
> + * must display the following acknowledgement:<br>
> + * This product includes software developed by TooLs GmbH.<br>
> + * 4. The name of TooLs GmbH may not be used to endorse or promote products<br>
> + * derived from this software without specific prior written permission.<br>
> + *<br>
> + * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR<br>
> + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES<br>
> + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.<br>
> + * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,<br>
> + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,<br>
> + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;<br>
> + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,<br>
> + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR<br>
> + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF<br>
> + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br>
> + */<br>
> +/*<br>
> + * Copyright (C) 2000 Benno Rice.<br>
> + * All rights reserved.<br>
> + *<br>
> + * Redistribution and use in source and binary forms, with or without<br>
> + * modification, are permitted provided that the following conditions<br>
> + * are met:<br>
> + * 1. Redistributions of source code must retain the above copyright<br>
> + * notice, this list of conditions and the following disclaimer.<br>
> + * 2. Redistributions in binary form must reproduce the above copyright<br>
> + * notice, this list of conditions and the following disclaimer in the<br>
> + * documentation and/or other materials provided with the distribution.<br>
> + *<br>
> + * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR<br>
> + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES<br>
> + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.<br>
> + * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,<br>
> + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,<br>
> + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;<br>
> + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,<br>
> + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR<br>
> + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF<br>
> + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br>
> + *<br>
> + * $FreeBSD$<br>
> + */<br>
> +<br>
> +#ifndef _DEV_OPENFIRM_H_<br>
> +#define _DEV_OPENFIRM_H_<br>
> +<br>
> +#include <sys/types.h><br>
> +#include <machine/_bus.h><br>
> +<br>
> +/*<br>
> + * Prototypes for Open Firmware Interface Routines<br>
> + */<br>
> +<br>
> +typedef uint32_t ihandle_t;<br>
> +typedef uint32_t phandle_t;<br>
> +typedef uint32_t pcell_t;<br>
> +<br>
> +#ifdef _KERNEL<br>
> +#include <sys/malloc.h><br>
> +<br>
> +#include <machine/ofw_machdep.h><br>
> +<br>
> +MALLOC_DECLARE(M_OFWPROP);<br>
> +<br>
> +/*<br>
> + * Open Firmware interface initialization. OF_install installs the named<br>
> + * interface as the Open Firmware access mechanism, OF_init initializes it.<br>
> + */<br>
> +<br>
> +boolean_t OF_install(char *name, int prio);<br>
> +int OF_init(void *cookie);<br>
> +<br>
> +/*<br>
> + * Known Open Firmware interface names<br>
> + */<br>
> +<br>
> +#define OFW_STD_DIRECT "ofw_std" /* Standard OF interface */<br>
> +#define OFW_STD_REAL "ofw_real" /* Real-mode OF interface */<br>
> +#define OFW_STD_32BIT "ofw_32bit" /* 32-bit OF interface */<br>
> +#define OFW_FDT "ofw_fdt" /* Flattened Device Tree */<br>
> +<br>
> +/* Generic functions */<br>
> +int OF_test(const char *name);<br>
> +void OF_printf(const char *fmt, ...);<br>
> +<br>
> +/* Device tree functions */<br>
> +phandle_t OF_peer(phandle_t node);<br>
> +phandle_t OF_child(phandle_t node);<br>
> +phandle_t OF_parent(phandle_t node);<br>
> +ssize_t OF_getproplen(phandle_t node, const char *propname);<br>
> +ssize_t OF_getprop(phandle_t node, const char *propname, void *buf,<br>
> + size_t len);<br>
> +ssize_t OF_getencprop(phandle_t node, const char *prop, pcell_t *buf,<br>
> + size_t len); /* Same as getprop, but maintains endianness */<br>
> +int OF_hasprop(phandle_t node, const char *propname);<br>
> +ssize_t OF_searchprop(phandle_t node, const char *propname, void *buf,<br>
> + size_t len);<br>
> +ssize_t OF_searchencprop(phandle_t node, const char *propname,<br>
> + pcell_t *buf, size_t len);<br>
> +ssize_t OF_getprop_alloc(phandle_t node, const char *propname,<br>
> + void **buf);<br>
> +ssize_t OF_getprop_alloc_multi(phandle_t node, const char *propname,<br>
> + int elsz, void **buf);<br>
> +ssize_t OF_getencprop_alloc(phandle_t node, const char *propname,<br>
> + void **buf);<br>
> +ssize_t OF_getencprop_alloc_multi(phandle_t node, const char *propname,<br>
> + int elsz, void **buf);<br>
> +void OF_prop_free(void *buf);<br>
> +int OF_nextprop(phandle_t node, const char *propname, char *buf,<br>
> + size_t len);<br>
> +int OF_setprop(phandle_t node, const char *name, const void *buf,<br>
> + size_t len);<br>
> +ssize_t OF_canon(const char *path, char *buf, size_t len);<br>
> +phandle_t OF_finddevice(const char *path);<br>
> +ssize_t OF_package_to_path(phandle_t node, char *buf, size_t len);<br>
> +<br>
> +/*<br>
> + * Some OF implementations (IBM, FDT) have a concept of effective phandles<br>
> + * used for device-tree cross-references. Given one of these, returns the<br>
> + * real phandle. If one can't be found (or running on OF implementations<br>
> + * without this property), returns its input.<br>
> + */<br>
> +phandle_t OF_node_from_xref(phandle_t xref);<br>
> +phandle_t OF_xref_from_node(phandle_t node);<br>
> +<br>
> +/*<br>
> + * When properties contain references to other nodes using xref handles it is<br>
> + * often necessary to use interfaces provided by the driver for the referenced<br>
> + * instance. These routines allow a driver that provides such an interface to<br>
> + * register its association with an xref handle, and for other drivers to obtain<br>
> + * the device_t associated with an xref handle.<br>
> + */<br>
> +device_t OF_device_from_xref(phandle_t xref);<br>
> +phandle_t OF_xref_from_device(device_t dev);<br>
> +int OF_device_register_xref(phandle_t xref, device_t dev);<br>
> +<br>
> +/* Device I/O functions */<br>
> +ihandle_t OF_open(const char *path);<br>
> +void OF_close(ihandle_t instance);<br>
> +ssize_t OF_read(ihandle_t instance, void *buf, size_t len);<br>
> +ssize_t OF_write(ihandle_t instance, const void *buf, size_t len);<br>
> +int OF_seek(ihandle_t instance, uint64_t where);<br>
> +<br>
> +phandle_t OF_instance_to_package(ihandle_t instance);<br>
> +ssize_t OF_instance_to_path(ihandle_t instance, char *buf, size_t len);<br>
> +int OF_call_method(const char *method, ihandle_t instance,<br>
> + int nargs, int nreturns, ...);<br>
> +<br>
> +/* Memory functions */<br>
> +void *OF_claim(void *virtrequest, size_t size, u_int align);<br>
> +void OF_release(void *virt, size_t size);<br>
> +<br>
> +/* Control transfer functions */<br>
> +void OF_enter(void);<br>
> +void OF_exit(void) __attribute__((noreturn));<br>
> +<br>
> +/* User interface functions */<br>
> +int OF_interpret(const char *cmd, int nreturns, ...);<br>
> +<br>
> +/*<br>
> + * Decode the Nth register property of the given device node and create a bus<br>
> + * space tag and handle for accessing it. This is for use in setting up things<br>
> + * like early console output before newbus is available.<br>
> + */<br>
> +int OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *ptag,<br>
> + bus_space_handle_t *phandle, bus_size_t *sz);<br>
> +<br>
> +#endif /* _KERNEL */<br>
> +#endif /* _DEV_OPENFIRM_H_ */<br>
> \ No newline at end of file<br>
> diff --git a/cpukit/libfreebsd/freebsd/dev/ofw/ofw_fdt.c b/cpukit/libfreebsd/freebsd/dev/ofw/ofw_fdt.c<br>
> new file mode 100644<br>
> index 0000000000..e4f72e8142<br>
> --- /dev/null<br>
> +++ b/cpukit/libfreebsd/freebsd/dev/ofw/ofw_fdt.c<br>
> @@ -0,0 +1,479 @@<br>
> +/*-<br>
> + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD<br>
> + *<br>
> + * Copyright (c) 2009-2010 The FreeBSD Foundation<br>
> + * All rights reserved.<br>
> + *<br>
> + * This software was developed by Semihalf under sponsorship from<br>
> + * the FreeBSD Foundation.<br>
> + *<br>
> + * Redistribution and use in source and binary forms, with or without<br>
> + * modification, are permitted provided that the following conditions<br>
> + * are met:<br>
> + * 1. Redistributions of source code must retain the above copyright<br>
> + * notice, this list of conditions and the following disclaimer.<br>
> + * 2. Redistributions in binary form must reproduce the above copyright<br>
> + * notice, this list of conditions and the following disclaimer in the<br>
> + * documentation and/or other materials provided with the distribution.<br>
> + *<br>
> + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND<br>
> + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>
> + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>
> + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE<br>
> + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL<br>
> + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS<br>
> + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)<br>
> + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT<br>
> + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY<br>
> + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF<br>
> + * SUCH DAMAGE.<br>
> + */<br>
> +<br>
> +#include <sys/cdefs.h><br>
> +__FBSDID("$FreeBSD$");<br>
> +<br>
> +#include <sys/param.h><br>
> +#include <sys/kernel.h><br>
> +#include <sys/malloc.h><br>
> +#include <sys/systm.h><br>
> +<br>
> +#include <contrib/libfdt/libfdt.h><br>
> +<br>
> +#include <machine/stdarg.h><br>
> +<br>
> +#include <dev/fdt/fdt_common.h><br>
> +#include <dev/ofw/ofwvar.h><br>
> +#include <dev/ofw/openfirm.h><br>
> +#include <dev/ofw/ofw_bus_subr.h><br>
> +<br>
> +#include "ofw_if.h"<br>
> +<br>
> +#ifdef DEBUG<br>
> +#define debugf(fmt, args...) do { printf("%s(): ", __func__); \<br>
> + printf(fmt,##args); } while (0)<br>
> +#else<br>
> +#define debugf(fmt, args...)<br>
> +#endif<br>
> +<br>
> +#if defined(__arm__)<br>
> +#if defined(SOC_MV_ARMADAXP) || defined(SOC_MV_ARMADA38X) || \<br>
> + defined(SOC_MV_DISCOVERY) || defined(SOC_MV_DOVE) || \<br>
> + defined(SOC_MV_FREY) || defined(SOC_MV_KIRKWOOD) || \<br>
> + defined(SOC_MV_LOKIPLUS) || defined(SOC_MV_ORION)<br>
> +#define FDT_MARVELL<br>
> +#endif<br>
> +#endif<br>
> +<br>
> +static int ofw_fdt_init(ofw_t, void *);<br>
> +static phandle_t ofw_fdt_peer(ofw_t, phandle_t);<br>
> +static phandle_t ofw_fdt_child(ofw_t, phandle_t);<br>
> +static phandle_t ofw_fdt_parent(ofw_t, phandle_t);<br>
> +static phandle_t ofw_fdt_instance_to_package(ofw_t, ihandle_t);<br>
> +static ssize_t ofw_fdt_getproplen(ofw_t, phandle_t, const char *);<br>
> +static ssize_t ofw_fdt_getprop(ofw_t, phandle_t, const char *, void *, size_t);<br>
> +static int ofw_fdt_nextprop(ofw_t, phandle_t, const char *, char *, size_t);<br>
> +static int ofw_fdt_setprop(ofw_t, phandle_t, const char *, const void *,<br>
> + size_t);<br>
> +static ssize_t ofw_fdt_canon(ofw_t, const char *, char *, size_t);<br>
> +static phandle_t ofw_fdt_finddevice(ofw_t, const char *);<br>
> +static ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t);<br>
> +static ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t);<br>
> +static int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *);<br>
> +<br>
> +static ofw_method_t ofw_fdt_methods[] = {<br>
> + OFWMETHOD(ofw_init, ofw_fdt_init),<br>
> + OFWMETHOD(ofw_peer, ofw_fdt_peer),<br>
> + OFWMETHOD(ofw_child, ofw_fdt_child),<br>
> + OFWMETHOD(ofw_parent, ofw_fdt_parent),<br>
> + OFWMETHOD(ofw_instance_to_package, ofw_fdt_instance_to_package),<br>
> + OFWMETHOD(ofw_getproplen, ofw_fdt_getproplen),<br>
> + OFWMETHOD(ofw_getprop, ofw_fdt_getprop),<br>
> + OFWMETHOD(ofw_nextprop, ofw_fdt_nextprop),<br>
> + OFWMETHOD(ofw_setprop, ofw_fdt_setprop),<br>
> + OFWMETHOD(ofw_canon, ofw_fdt_canon),<br>
> + OFWMETHOD(ofw_finddevice, ofw_fdt_finddevice),<br>
> + OFWMETHOD(ofw_instance_to_path, ofw_fdt_instance_to_path),<br>
> + OFWMETHOD(ofw_package_to_path, ofw_fdt_package_to_path),<br>
> + OFWMETHOD(ofw_interpret, ofw_fdt_interpret),<br>
> + { 0, 0 }<br>
> +};<br>
> +<br>
> +static ofw_def_t ofw_fdt = {<br>
> + OFW_FDT,<br>
> + ofw_fdt_methods,<br>
> + 0<br>
> +};<br>
> +OFW_DEF(ofw_fdt);<br>
> +<br>
> +static void *fdtp = NULL;<br>
> +<br>
> +static int<br>
> +sysctl_handle_dtb(SYSCTL_HANDLER_ARGS)<br>
> +{<br>
> +<br>
> + return (sysctl_handle_opaque(oidp, fdtp, fdt_totalsize(fdtp), req));<br>
> +}<br>
> +<br>
> +static void<br>
> +sysctl_register_fdt_oid(void *arg)<br>
> +{<br>
> +<br>
> + /* If there is no FDT registered, skip adding the sysctl */<br>
> + if (fdtp == NULL)<br>
> + return;<br>
> +<br>
> + SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_fdt), OID_AUTO, "dtb",<br>
> + CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,<br>
> + sysctl_handle_dtb, "", "Device Tree Blob");<br>
> +}<br>
> +SYSINIT(dtb_oid, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_fdt_oid, NULL);<br>
> +<br>
> +static int<br>
> +ofw_fdt_init(ofw_t ofw, void *data)<br>
> +{<br>
> + int err;<br>
> +<br>
> + /* Check FDT blob integrity */<br>
> + if ((err = fdt_check_header(data)) != 0)<br>
> + return (err);<br>
> +<br>
> + fdtp = data;<br>
> + return (0);<br>
> +}<br>
> +<br>
> +/*<br>
> + * Device tree functions.<br>
> + *<br>
> + * We use the offset from fdtp to the node as the 'phandle' in OF interface.<br>
> + *<br>
> + * phandle is a u32 value, therefore we cannot use the pointer to node as<br>
> + * phandle in 64 bit. We also do not use the usual fdt offset as phandle,<br>
> + * as it can be 0, and the OF interface has special meaning for phandle 0.<br>
> + */<br>
> +<br>
> +static phandle_t<br>
> +fdt_offset_phandle(int offset)<br>
> +{<br>
> + if (offset < 0)<br>
> + return (0);<br>
> + return ((phandle_t)offset + fdt_off_dt_struct(fdtp));<br>
> +}<br>
> +<br>
> +static int<br>
> +fdt_phandle_offset(phandle_t p)<br>
> +{<br>
> + int pint = (int)p;<br>
> + int dtoff = fdt_off_dt_struct(fdtp);<br>
> +<br>
> + if (pint < dtoff)<br>
> + return (-1);<br>
> + return (pint - dtoff);<br>
> +}<br>
> +<br>
> +/* Return the next sibling of this node or 0. */<br>
> +static phandle_t<br>
> +ofw_fdt_peer(ofw_t ofw, phandle_t node)<br>
> +{<br>
> + int offset;<br>
> +<br>
> + if (node == 0) {<br>
> + /* Find root node */<br>
> + offset = fdt_path_offset(fdtp, "/");<br>
> +<br>
> + return (fdt_offset_phandle(offset));<br>
> + }<br>
> +<br>
> + offset = fdt_phandle_offset(node);<br>
> + if (offset < 0)<br>
> + return (0);<br>
> + offset = fdt_next_subnode(fdtp, offset);<br>
> + return (fdt_offset_phandle(offset));<br>
> +}<br>
> +<br>
> +/* Return the first child of this node or 0. */<br>
> +static phandle_t<br>
> +ofw_fdt_child(ofw_t ofw, phandle_t node)<br>
> +{<br>
> + int offset;<br>
> +<br>
> + offset = fdt_phandle_offset(node);<br>
> + if (offset < 0)<br>
> + return (0);<br>
> + offset = fdt_first_subnode(fdtp, offset);<br>
> + return (fdt_offset_phandle(offset));<br>
> +}<br>
> +<br>
> +/* Return the parent of this node or 0. */<br>
> +static phandle_t<br>
> +ofw_fdt_parent(ofw_t ofw, phandle_t node)<br>
> +{<br>
> + int offset, paroffset;<br>
> +<br>
> + offset = fdt_phandle_offset(node);<br>
> + if (offset < 0)<br>
> + return (0);<br>
> +<br>
> + paroffset = fdt_parent_offset(fdtp, offset);<br>
> + return (fdt_offset_phandle(paroffset));<br>
> +}<br>
> +<br>
> +/* Return the package handle that corresponds to an instance handle. */<br>
> +static phandle_t<br>
> +ofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance)<br>
> +{<br>
> +<br>
> + /* Where real OF uses ihandles in the tree, FDT uses xref phandles */<br>
> + return (OF_node_from_xref(instance));<br>
> +}<br>
> +<br>
> +/* Get the length of a property of a package. */<br>
> +static ssize_t<br>
> +ofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname)<br>
> +{<br>
> + const void *prop;<br>
> + int offset, len;<br>
> +<br>
> + offset = fdt_phandle_offset(package);<br>
> + if (offset < 0)<br>
> + return (-1);<br>
> +<br>
> + len = -1;<br>
> + prop = fdt_getprop(fdtp, offset, propname, &len);<br>
> +<br>
> + if (prop == NULL && strcmp(propname, "name") == 0) {<br>
> + /* Emulate the 'name' property */<br>
> + fdt_get_name(fdtp, offset, &len);<br>
> + return (len + 1);<br>
> + }<br>
> +<br>
> + if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {<br>
> + if (strcmp(propname, "fdtbootcpu") == 0)<br>
> + return (sizeof(cell_t));<br>
> + if (strcmp(propname, "fdtmemreserv") == 0)<br>
> + return (sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp));<br>
> + }<br>
> +<br>
> + if (prop == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (len);<br>
> +}<br>
> +<br>
> +/* Get the value of a property of a package. */<br>
> +static ssize_t<br>
> +ofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,<br>
> + size_t buflen)<br>
> +{<br>
> + const void *prop;<br>
> + const char *name;<br>
> + int len, offset;<br>
> + uint32_t cpuid;<br>
> +<br>
> + offset = fdt_phandle_offset(package);<br>
> + if (offset < 0)<br>
> + return (-1);<br>
> +<br>
> + prop = fdt_getprop(fdtp, offset, propname, &len);<br>
> +<br>
> + if (prop == NULL && strcmp(propname, "name") == 0) {<br>
> + /* Emulate the 'name' property */<br>
> + name = fdt_get_name(fdtp, offset, &len);<br>
> + strncpy(buf, name, buflen);<br>
> + return (len + 1);<br>
> + }<br>
> +<br>
> + if (prop == NULL && offset == fdt_path_offset(fdtp, "/chosen")) {<br>
> + if (strcmp(propname, "fdtbootcpu") == 0) {<br>
> + cpuid = cpu_to_fdt32(fdt_boot_cpuid_phys(fdtp));<br>
> + len = sizeof(cpuid);<br>
> + prop = &cpuid;<br>
> + }<br>
> + if (strcmp(propname, "fdtmemreserv") == 0) {<br>
> + prop = (char *)fdtp + fdt_off_mem_rsvmap(fdtp);<br>
> + len = sizeof(uint64_t)*2*fdt_num_mem_rsv(fdtp);<br>
> + }<br>
> + }<br>
> +<br>
> + if (prop == NULL)<br>
> + return (-1);<br>
> +<br>
> + bcopy(prop, buf, min(len, buflen));<br>
> +<br>
> + return (len);<br>
> +}<br>
> +<br>
> +/*<br>
> + * Get the next property of a package. Return values:<br>
> + * -1: package or previous property does not exist<br>
> + * 0: no more properties<br>
> + * 1: success<br>
> + */<br>
> +static int<br>
> +ofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,<br>
> + size_t size)<br>
> +{<br>
> + const void *prop;<br>
> + const char *name;<br>
> + int offset;<br>
> +<br>
> + offset = fdt_phandle_offset(package);<br>
> + if (offset < 0)<br>
> + return (-1);<br>
> +<br>
> + if (previous == NULL)<br>
> + /* Find the first prop in the node */<br>
> + offset = fdt_first_property_offset(fdtp, offset);<br>
> + else {<br>
> + fdt_for_each_property_offset(offset, fdtp, offset) {<br>
> + prop = fdt_getprop_by_offset(fdtp, offset, &name, NULL);<br>
> + if (prop == NULL)<br>
> + return (-1); /* Internal error */<br>
> + /* Skip until we find 'previous', then bail out */<br>
> + if (strcmp(name, previous) != 0)<br>
> + continue;<br>
> + offset = fdt_next_property_offset(fdtp, offset);<br>
> + break;<br>
> + }<br>
> + }<br>
> +<br>
> + if (offset < 0)<br>
> + return (0); /* No properties */<br>
> +<br>
> + prop = fdt_getprop_by_offset(fdtp, offset, &name, &offset);<br>
> + if (prop == NULL)<br>
> + return (-1); /* Internal error */<br>
> +<br>
> + strncpy(buf, name, size);<br>
> +<br>
> + return (1);<br>
> +}<br>
> +<br>
> +/* Set the value of a property of a package. */<br>
> +static int<br>
> +ofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname,<br>
> + const void *buf, size_t len)<br>
> +{<br>
> + int offset;<br>
> +<br>
> + offset = fdt_phandle_offset(package);<br>
> + if (offset < 0)<br>
> + return (-1);<br>
> +<br>
> + if (fdt_setprop_inplace(fdtp, offset, propname, buf, len) != 0)<br>
> + /* Try to add property, when setting value inplace failed */<br>
> + return (fdt_setprop(fdtp, offset, propname, buf, len));<br>
> +<br>
> + return (0);<br>
> +}<br>
> +<br>
> +/* Convert a device specifier to a fully qualified pathname. */<br>
> +static ssize_t<br>
> +ofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len)<br>
> +{<br>
> +<br>
> + return (-1);<br>
> +}<br>
> +<br>
> +/* Return a package handle for the specified device. */<br>
> +static phandle_t<br>
> +ofw_fdt_finddevice(ofw_t ofw, const char *device)<br>
> +{<br>
> + int offset;<br>
> +<br>
> + offset = fdt_path_offset(fdtp, device);<br>
> + if (offset < 0)<br>
> + return (-1);<br>
> + return (fdt_offset_phandle(offset));<br>
> +}<br>
> +<br>
> +/* Return the fully qualified pathname corresponding to an instance. */<br>
> +static ssize_t<br>
> +ofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)<br>
> +{<br>
> + phandle_t phandle;<br>
> +<br>
> + phandle = OF_instance_to_package(instance);<br>
> + if (phandle == -1)<br>
> + return (-1);<br>
> +<br>
> + return (OF_package_to_path(phandle, buf, len));<br>
> +}<br>
> +<br>
> +/* Return the fully qualified pathname corresponding to a package. */<br>
> +static ssize_t<br>
> +ofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)<br>
> +{<br>
> +<br>
> + return (-1);<br>
> +}<br>
> +<br>
> +#if defined(FDT_MARVELL)<br>
> +static int<br>
> +ofw_fdt_fixup(ofw_t ofw)<br>
> +{<br>
> +#define FDT_MODEL_LEN 80<br>
> + char model[FDT_MODEL_LEN];<br>
> + phandle_t root;<br>
> + ssize_t len;<br>
> + int i;<br>
> +<br>
> + if ((root = ofw_fdt_finddevice(ofw, "/")) == -1)<br>
> + return (ENODEV);<br>
> +<br>
> + if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0)<br>
> + return (0);<br>
> +<br>
> + bzero(model, FDT_MODEL_LEN);<br>
> + if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0)<br>
> + return (0);<br>
> +<br>
> + /*<br>
> + * Search fixup table and call handler if appropriate.<br>
> + */<br>
> + for (i = 0; fdt_fixup_table[i].model != NULL; i++) {<br>
> + if (strncmp(model, fdt_fixup_table[i].model,<br>
> + FDT_MODEL_LEN) != 0)<br>
> + /*<br>
> + * Sometimes it's convenient to provide one<br>
> + * fixup entry that refers to many boards.<br>
> + * To handle this case, simply check if model<br>
> + * is compatible parameter<br>
> + */<br>
> + if(!ofw_bus_node_is_compatible(root,<br>
> + fdt_fixup_table[i].model))<br>
> + continue;<br>
> +<br>
> + if (fdt_fixup_table[i].handler != NULL)<br>
> + (*fdt_fixup_table[i].handler)(root);<br>
> + }<br>
> +<br>
> + return (0);<br>
> +}<br>
> +#endif<br>
> +<br>
> +static int<br>
> +ofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals)<br>
> +{<br>
> +#if defined(FDT_MARVELL)<br>
> + int rv;<br>
> +<br>
> + /*<br>
> + * Note: FDT does not have the possibility to 'interpret' commands,<br>
> + * but we abuse the interface a bit to use it for doing non-standard<br>
> + * operations on the device tree blob.<br>
> + *<br>
> + * Currently the only supported 'command' is to trigger performing<br>
> + * fixups.<br>
> + */<br>
> + if (strncmp("perform-fixup", cmd, 13) != 0)<br>
> + return (0);<br>
> +<br>
> + rv = ofw_fdt_fixup(ofw);<br>
> + if (nret > 0)<br>
> + retvals[0] = rv;<br>
> +<br>
> + return (rv);<br>
> +#else<br>
> + return (0);<br>
> +#endif<br>
> +}<br>
> \ No newline at end of file<br>
> diff --git a/cpukit/libfreebsd/freebsd/dev/ofw/openfirm.c b/cpukit/libfreebsd/freebsd/dev/ofw/openfirm.c<br>
> new file mode 100644<br>
> index 0000000000..50ebf2cf16<br>
> --- /dev/null<br>
> +++ b/cpukit/libfreebsd/freebsd/dev/ofw/openfirm.c<br>
> @@ -0,0 +1,848 @@<br>
> +/* $NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $ */<br>
> +<br>
> +/*-<br>
> + * SPDX-License-Identifier: BSD-4-Clause<br>
> + *<br>
> + * Copyright (C) 1995, 1996 Wolfgang Solfrank.<br>
> + * Copyright (C) 1995, 1996 TooLs GmbH.<br>
> + * All rights reserved.<br>
> + *<br>
> + * Redistribution and use in source and binary forms, with or without<br>
> + * modification, are permitted provided that the following conditions<br>
> + * are met:<br>
> + * 1. Redistributions of source code must retain the above copyright<br>
> + * notice, this list of conditions and the following disclaimer.<br>
> + * 2. Redistributions in binary form must reproduce the above copyright<br>
> + * notice, this list of conditions and the following disclaimer in the<br>
> + * documentation and/or other materials provided with the distribution.<br>
> + * 3. All advertising materials mentioning features or use of this software<br>
> + * must display the following acknowledgement:<br>
> + * This product includes software developed by TooLs GmbH.<br>
> + * 4. The name of TooLs GmbH may not be used to endorse or promote products<br>
> + * derived from this software without specific prior written permission.<br>
> + *<br>
> + * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR<br>
> + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES<br>
> + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.<br>
> + * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,<br>
> + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,<br>
> + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;<br>
> + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,<br>
> + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR<br>
> + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF<br>
> + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br>
> + */<br>
> +/*-<br>
> + * Copyright (C) 2000 Benno Rice.<br>
> + * All rights reserved.<br>
> + *<br>
> + * Redistribution and use in source and binary forms, with or without<br>
> + * modification, are permitted provided that the following conditions<br>
> + * are met:<br>
> + * 1. Redistributions of source code must retain the above copyright<br>
> + * notice, this list of conditions and the following disclaimer.<br>
> + * 2. Redistributions in binary form must reproduce the above copyright<br>
> + * notice, this list of conditions and the following disclaimer in the<br>
> + * documentation and/or other materials provided with the distribution.<br>
> + *<br>
> + * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR<br>
> + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES<br>
> + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.<br>
> + * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,<br>
> + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,<br>
> + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;<br>
> + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,<br>
> + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR<br>
> + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF<br>
> + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br>
> + */<br>
> +<br>
> +#include <sys/cdefs.h><br>
> +__FBSDID("$FreeBSD$");<br>
> +<br>
> +#include "opt_platform.h"<br>
> +<br>
> +#include <sys/param.h><br>
> +#include <sys/kernel.h><br>
> +#include <sys/lock.h><br>
> +#include <sys/malloc.h><br>
> +#include <sys/mutex.h><br>
> +#include <sys/queue.h><br>
> +#include <sys/systm.h><br>
> +#include <sys/endian.h><br>
> +<br>
> +#include <machine/stdarg.h><br>
> +<br>
> +#include <dev/ofw/ofwvar.h><br>
> +#include <dev/ofw/openfirm.h><br>
> +<br>
> +#include "ofw_if.h"<br>
> +<br>
> +static void OF_putchar(int c, void *arg);<br>
> +<br>
> +MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties");<br>
> +<br>
> +static ihandle_t stdout;<br>
> +<br>
> +static ofw_def_t *ofw_def_impl = NULL;<br>
> +static ofw_t ofw_obj;<br>
> +static struct ofw_kobj ofw_kernel_obj;<br>
> +static struct kobj_ops ofw_kernel_kops;<br>
> +<br>
> +struct xrefinfo {<br>
> + phandle_t xref;<br>
> + phandle_t node;<br>
> + device_t dev;<br>
> + SLIST_ENTRY(xrefinfo) next_entry;<br>
> +};<br>
> +<br>
> +static SLIST_HEAD(, xrefinfo) xreflist = SLIST_HEAD_INITIALIZER(xreflist);<br>
> +static struct mtx xreflist_lock;<br>
> +static boolean_t xref_init_done;<br>
> +<br>
> +#define FIND_BY_XREF 0<br>
> +#define FIND_BY_NODE 1<br>
> +#define FIND_BY_DEV 2<br>
> +<br>
> +/*<br>
> + * xref-phandle-device lookup helper routines.<br>
> + *<br>
> + * As soon as we are able to use malloc(), walk the node tree and build a list<br>
> + * of info that cross-references node handles, xref handles, and device_t<br>
> + * instances. This list exists primarily to allow association of a device_t<br>
> + * with an xref handle, but it is also used to speed up translation between xref<br>
> + * and node handles. Before malloc() is available we have to recursively search<br>
> + * the node tree each time we want to translate between a node and xref handle.<br>
> + * Afterwards we can do the translations by searching this much shorter list.<br>
> + */<br>
> +static void<br>
> +xrefinfo_create(phandle_t node)<br>
> +{<br>
> + struct xrefinfo * xi;<br>
> + phandle_t child, xref;<br>
> +<br>
> + /*<br>
> + * Recursively descend from parent, looking for nodes with a property<br>
> + * named either "phandle", "ibm,phandle", or "linux,phandle". For each<br>
> + * such node found create an entry in the xreflist.<br>
> + */<br>
> + for (child = OF_child(node); child != 0; child = OF_peer(child)) {<br>
> + xrefinfo_create(child);<br>
> + if (OF_getencprop(child, "phandle", &xref, sizeof(xref)) ==<br>
> + -1 && OF_getencprop(child, "ibm,phandle", &xref,<br>
> + sizeof(xref)) == -1 && OF_getencprop(child,<br>
> + "linux,phandle", &xref, sizeof(xref)) == -1)<br>
> + continue;<br>
> + xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK | M_ZERO);<br>
> + xi->node = child;<br>
> + xi->xref = xref;<br>
> + SLIST_INSERT_HEAD(&xreflist, xi, next_entry);<br>
> + }<br>
> +}<br>
> +<br>
> +static void<br>
> +xrefinfo_init(void *unsed)<br>
> +{<br>
> +<br>
> + /*<br>
> + * There is no locking during this init because it runs much earlier<br>
> + * than any of the clients/consumers of the xref list data, but we do<br>
> + * initialize the mutex that will be used for access later.<br>
> + */<br>
> + mtx_init(&xreflist_lock, "OF xreflist lock", NULL, MTX_DEF);<br>
> + xrefinfo_create(OF_peer(0));<br>
> + xref_init_done = true;<br>
> +}<br>
> +SYSINIT(xrefinfo, SI_SUB_KMEM, SI_ORDER_ANY, xrefinfo_init, NULL);<br>
> +<br>
> +static struct xrefinfo *<br>
> +xrefinfo_find(uintptr_t key, int find_by)<br>
> +{<br>
> + struct xrefinfo *rv, *xi;<br>
> +<br>
> + rv = NULL;<br>
> + mtx_lock(&xreflist_lock);<br>
> + SLIST_FOREACH(xi, &xreflist, next_entry) {<br>
> + if ((find_by == FIND_BY_XREF && (phandle_t)key == xi->xref) ||<br>
> + (find_by == FIND_BY_NODE && (phandle_t)key == xi->node) ||<br>
> + (find_by == FIND_BY_DEV && key == (uintptr_t)xi->dev)) {<br>
> + rv = xi;<br>
> + break;<br>
> + }<br>
> + }<br>
> + mtx_unlock(&xreflist_lock);<br>
> + return (rv);<br>
> +}<br>
> +<br>
> +static struct xrefinfo *<br>
> +xrefinfo_add(phandle_t node, phandle_t xref, device_t dev)<br>
> +{<br>
> + struct xrefinfo *xi;<br>
> +<br>
> + xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK);<br>
> + xi->node = node;<br>
> + xi->xref = xref;<br>
> + xi->dev = dev;<br>
> + mtx_lock(&xreflist_lock);<br>
> + SLIST_INSERT_HEAD(&xreflist, xi, next_entry);<br>
> + mtx_unlock(&xreflist_lock);<br>
> + return (xi);<br>
> +}<br>
> +<br>
> +/*<br>
> + * OFW install routines. Highest priority wins, equal priority also<br>
> + * overrides allowing last-set to win.<br>
> + */<br>
> +SET_DECLARE(ofw_set, ofw_def_t);<br>
> +<br>
> +boolean_t<br>
> +OF_install(char *name, int prio)<br>
> +{<br>
> + ofw_def_t *ofwp, **ofwpp;<br>
> + static int curr_prio = 0;<br>
> +<br>
> + /* Allow OF layer to be uninstalled */<br>
> + if (name == NULL) {<br>
> + ofw_def_impl = NULL;<br>
> + return (FALSE);<br>
> + }<br>
> +<br>
> + /*<br>
> + * Try and locate the OFW kobj corresponding to the name.<br>
> + */<br>
> + SET_FOREACH(ofwpp, ofw_set) {<br>
> + ofwp = *ofwpp;<br>
> +<br>
> + if (ofwp->name &&<br>
> + !strcmp(ofwp->name, name) &&<br>
> + prio >= curr_prio) {<br>
> + curr_prio = prio;<br>
> + ofw_def_impl = ofwp;<br>
> + return (TRUE);<br>
> + }<br>
> + }<br>
> +<br>
> + return (FALSE);<br>
> +}<br>
> +<br>
> +/* Initializer */<br>
> +int<br>
> +OF_init(void *cookie)<br>
> +{<br>
> + phandle_t chosen;<br>
> + int rv;<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + ofw_obj = &ofw_kernel_obj;<br>
> + /*<br>
> + * Take care of compiling the selected class, and<br>
> + * then statically initialize the OFW object.<br>
> + */<br>
> + kobj_class_compile_static(ofw_def_impl, &ofw_kernel_kops);<br>
> + kobj_init_static((kobj_t)ofw_obj, ofw_def_impl);<br>
> +<br>
> + rv = OFW_INIT(ofw_obj, cookie);<br>
> +<br>
> + if ((chosen = OF_finddevice("/chosen")) != -1)<br>
> + if (OF_getencprop(chosen, "stdout", &stdout,<br>
> + sizeof(stdout)) == -1)<br>
> + stdout = -1;<br>
> +<br>
> + return (rv);<br>
> +}<br>
> +<br>
> +static void<br>
> +OF_putchar(int c, void *arg __unused)<br>
> +{<br>
> + char cbuf;<br>
> +<br>
> + if (c == '\n') {<br>
> + cbuf = '\r';<br>
> + OF_write(stdout, &cbuf, 1);<br>
> + }<br>
> +<br>
> + cbuf = c;<br>
> + OF_write(stdout, &cbuf, 1);<br>
> +}<br>
> +<br>
> +void<br>
> +OF_printf(const char *fmt, ...)<br>
> +{<br>
> + va_list va;<br>
> +<br>
> + va_start(va, fmt);<br>
> + (void)kvprintf(fmt, OF_putchar, NULL, 10, va);<br>
> + va_end(va);<br>
> +}<br>
> +<br>
> +/*<br>
> + * Generic functions<br>
> + */<br>
> +<br>
> +/* Test to see if a service exists. */<br>
> +int<br>
> +OF_test(const char *name)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_TEST(ofw_obj, name));<br>
> +}<br>
> +<br>
> +int<br>
> +OF_interpret(const char *cmd, int nreturns, ...)<br>
> +{<br>
> + va_list ap;<br>
> + cell_t slots[16];<br>
> + int i = 0;<br>
> + int status;<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + status = OFW_INTERPRET(ofw_obj, cmd, nreturns, slots);<br>
> + if (status == -1)<br>
> + return (status);<br>
> +<br>
> + va_start(ap, nreturns);<br>
> + while (i < nreturns)<br>
> + *va_arg(ap, cell_t *) = slots[i++];<br>
> + va_end(ap);<br>
> +<br>
> + return (status);<br>
> +}<br>
> +<br>
> +/*<br>
> + * Device tree functions<br>
> + */<br>
> +<br>
> +/* Return the next sibling of this node or 0. */<br>
> +phandle_t<br>
> +OF_peer(phandle_t node)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (0);<br>
> +<br>
> + return (OFW_PEER(ofw_obj, node));<br>
> +}<br>
> +<br>
> +/* Return the first child of this node or 0. */<br>
> +phandle_t<br>
> +OF_child(phandle_t node)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (0);<br>
> +<br>
> + return (OFW_CHILD(ofw_obj, node));<br>
> +}<br>
> +<br>
> +/* Return the parent of this node or 0. */<br>
> +phandle_t<br>
> +OF_parent(phandle_t node)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (0);<br>
> +<br>
> + return (OFW_PARENT(ofw_obj, node));<br>
> +}<br>
> +<br>
> +/* Return the package handle that corresponds to an instance handle. */<br>
> +phandle_t<br>
> +OF_instance_to_package(ihandle_t instance)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_INSTANCE_TO_PACKAGE(ofw_obj, instance));<br>
> +}<br>
> +<br>
> +/* Get the length of a property of a package. */<br>
> +ssize_t<br>
> +OF_getproplen(phandle_t package, const char *propname)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_GETPROPLEN(ofw_obj, package, propname));<br>
> +}<br>
> +<br>
> +/* Check existence of a property of a package. */<br>
> +int<br>
> +OF_hasprop(phandle_t package, const char *propname)<br>
> +{<br>
> +<br>
> + return (OF_getproplen(package, propname) >= 0 ? 1 : 0);<br>
> +}<br>
> +<br>
> +/* Get the value of a property of a package. */<br>
> +ssize_t<br>
> +OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen));<br>
> +}<br>
> +<br>
> +ssize_t<br>
> +OF_getencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len)<br>
> +{<br>
> + ssize_t retval;<br>
> + int i;<br>
> +<br>
> + KASSERT(len % 4 == 0, ("Need a multiple of 4 bytes"));<br>
> +<br>
> + retval = OF_getprop(node, propname, buf, len);<br>
> + if (retval <= 0)<br>
> + return (retval);<br>
> +<br>
> + for (i = 0; i < len/4; i++)<br>
> + buf[i] = be32toh(buf[i]);<br>
> +<br>
> + return (retval);<br>
> +}<br>
> +<br>
> +/*<br>
> + * Recursively search the node and its parent for the given property, working<br>
> + * downward from the node to the device tree root. Returns the value of the<br>
> + * first match.<br>
> + */<br>
> +ssize_t<br>
> +OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len)<br>
> +{<br>
> + ssize_t rv;<br>
> +<br>
> + for (; node != 0; node = OF_parent(node))<br>
> + if ((rv = OF_getprop(node, propname, buf, len)) != -1)<br>
> + return (rv);<br>
> + return (-1);<br>
> +}<br>
> +<br>
> +ssize_t<br>
> +OF_searchencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len)<br>
> +{<br>
> + ssize_t rv;<br>
> +<br>
> + for (; node != 0; node = OF_parent(node))<br>
> + if ((rv = OF_getencprop(node, propname, buf, len)) != -1)<br>
> + return (rv);<br>
> + return (-1);<br>
> +}<br>
> +<br>
> +/*<br>
> + * Store the value of a property of a package into newly allocated memory<br>
> + * (using the M_OFWPROP malloc pool and M_WAITOK).<br>
> + */<br>
> +ssize_t<br>
> +OF_getprop_alloc(phandle_t package, const char *propname, void **buf)<br>
> +{<br>
> + int len;<br>
> +<br>
> + *buf = NULL;<br>
> + if ((len = OF_getproplen(package, propname)) == -1)<br>
> + return (-1);<br>
> +<br>
> + if (len > 0) {<br>
> + *buf = malloc(len, M_OFWPROP, M_WAITOK);<br>
> + if (OF_getprop(package, propname, *buf, len) == -1) {<br>
> + free(*buf, M_OFWPROP);<br>
> + *buf = NULL;<br>
> + return (-1);<br>
> + }<br>
> + }<br>
> + return (len);<br>
> +}<br>
> +<br>
> +/*<br>
> + * Store the value of a property of a package into newly allocated memory<br>
> + * (using the M_OFWPROP malloc pool and M_WAITOK). elsz is the size of a<br>
> + * single element, the number of elements is return in number.<br>
> + */<br>
> +ssize_t<br>
> +OF_getprop_alloc_multi(phandle_t package, const char *propname, int elsz, void **buf)<br>
> +{<br>
> + int len;<br>
> +<br>
> + *buf = NULL;<br>
> + if ((len = OF_getproplen(package, propname)) == -1 ||<br>
> + len % elsz != 0)<br>
> + return (-1);<br>
> +<br>
> + if (len > 0) {<br>
> + *buf = malloc(len, M_OFWPROP, M_WAITOK);<br>
> + if (OF_getprop(package, propname, *buf, len) == -1) {<br>
> + free(*buf, M_OFWPROP);<br>
> + *buf = NULL;<br>
> + return (-1);<br>
> + }<br>
> + }<br>
> + return (len / elsz);<br>
> +}<br>
> +<br>
> +ssize_t<br>
> +OF_getencprop_alloc(phandle_t package, const char *name, void **buf)<br>
> +{<br>
> + ssize_t ret;<br>
> +<br>
> + ret = OF_getencprop_alloc_multi(package, name, sizeof(pcell_t),<br>
> + buf);<br>
> + if (ret < 0)<br>
> + return (ret);<br>
> + else<br>
> + return (ret * sizeof(pcell_t));<br>
> +}<br>
> +<br>
> +ssize_t<br>
> +OF_getencprop_alloc_multi(phandle_t package, const char *name, int elsz,<br>
> + void **buf)<br>
> +{<br>
> + ssize_t retval;<br>
> + pcell_t *cell;<br>
> + int i;<br>
> +<br>
> + retval = OF_getprop_alloc_multi(package, name, elsz, buf);<br>
> + if (retval == -1)<br>
> + return (-1);<br>
> +<br>
> + cell = *buf;<br>
> + for (i = 0; i < retval * elsz / 4; i++)<br>
> + cell[i] = be32toh(cell[i]);<br>
> +<br>
> + return (retval);<br>
> +}<br>
> +<br>
> +/* Free buffer allocated by OF_getencprop_alloc or OF_getprop_alloc */<br>
> +void OF_prop_free(void *buf)<br>
> +{<br>
> +<br>
> + free(buf, M_OFWPROP);<br>
> +}<br>
> +<br>
> +/* Get the next property of a package. */<br>
> +int<br>
> +OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size));<br>
> +}<br>
> +<br>
> +/* Set the value of a property of a package. */<br>
> +int<br>
> +OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_SETPROP(ofw_obj, package, propname, buf,len));<br>
> +}<br>
> +<br>
> +/* Convert a device specifier to a fully qualified pathname. */<br>
> +ssize_t<br>
> +OF_canon(const char *device, char *buf, size_t len)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_CANON(ofw_obj, device, buf, len));<br>
> +}<br>
> +<br>
> +/* Return a package handle for the specified device. */<br>
> +phandle_t<br>
> +OF_finddevice(const char *device)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_FINDDEVICE(ofw_obj, device));<br>
> +}<br>
> +<br>
> +/* Return the fully qualified pathname corresponding to an instance. */<br>
> +ssize_t<br>
> +OF_instance_to_path(ihandle_t instance, char *buf, size_t len)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len));<br>
> +}<br>
> +<br>
> +/* Return the fully qualified pathname corresponding to a package. */<br>
> +ssize_t<br>
> +OF_package_to_path(phandle_t package, char *buf, size_t len)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len));<br>
> +}<br>
> +<br>
> +/* Look up effective phandle (see FDT/PAPR spec) */<br>
> +static phandle_t<br>
> +OF_child_xref_phandle(phandle_t parent, phandle_t xref)<br>
> +{<br>
> + phandle_t child, rxref;<br>
> +<br>
> + /*<br>
> + * Recursively descend from parent, looking for a node with a property<br>
> + * named either "phandle", "ibm,phandle", or "linux,phandle" that<br>
> + * matches the xref we are looking for.<br>
> + */<br>
> +<br>
> + for (child = OF_child(parent); child != 0; child = OF_peer(child)) {<br>
> + rxref = OF_child_xref_phandle(child, xref);<br>
> + if (rxref != -1)<br>
> + return (rxref);<br>
> +<br>
> + if (OF_getencprop(child, "phandle", &rxref, sizeof(rxref)) ==<br>
> + -1 && OF_getencprop(child, "ibm,phandle", &rxref,<br>
> + sizeof(rxref)) == -1 && OF_getencprop(child,<br>
> + "linux,phandle", &rxref, sizeof(rxref)) == -1)<br>
> + continue;<br>
> +<br>
> + if (rxref == xref)<br>
> + return (child);<br>
> + }<br>
> +<br>
> + return (-1);<br>
> +}<br>
> +<br>
> +phandle_t<br>
> +OF_node_from_xref(phandle_t xref)<br>
> +{<br>
> + struct xrefinfo *xi;<br>
> + phandle_t node;<br>
> +<br>
> + if (xref_init_done) {<br>
> + if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)<br>
> + return (xref);<br>
> + return (xi->node);<br>
> + }<br>
> +<br>
> + if ((node = OF_child_xref_phandle(OF_peer(0), xref)) == -1)<br>
> + return (xref);<br>
> + return (node);<br>
> +}<br>
> +<br>
> +phandle_t<br>
> +OF_xref_from_node(phandle_t node)<br>
> +{<br>
> + struct xrefinfo *xi;<br>
> + phandle_t xref;<br>
> +<br>
> + if (xref_init_done) {<br>
> + if ((xi = xrefinfo_find(node, FIND_BY_NODE)) == NULL)<br>
> + return (node);<br>
> + return (xi->xref);<br>
> + }<br>
> +<br>
> + if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) == -1 &&<br>
> + OF_getencprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 &&<br>
> + OF_getencprop(node, "linux,phandle", &xref, sizeof(xref)) == -1)<br>
> + return (node);<br>
> + return (xref);<br>
> +}<br>
> +<br>
> +device_t<br>
> +OF_device_from_xref(phandle_t xref)<br>
> +{<br>
> + struct xrefinfo *xi;<br>
> +<br>
> + if (xref_init_done) {<br>
> + if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)<br>
> + return (NULL);<br>
> + return (xi->dev);<br>
> + }<br>
> + panic("Attempt to find device before xreflist_init");<br>
> +}<br>
> +<br>
> +phandle_t<br>
> +OF_xref_from_device(device_t dev)<br>
> +{<br>
> + struct xrefinfo *xi;<br>
> +<br>
> + if (xref_init_done) {<br>
> + if ((xi = xrefinfo_find((uintptr_t)dev, FIND_BY_DEV)) == NULL)<br>
> + return (0);<br>
> + return (xi->xref);<br>
> + }<br>
> + panic("Attempt to find xref before xreflist_init");<br>
> +}<br>
> +<br>
> +int<br>
> +OF_device_register_xref(phandle_t xref, device_t dev)<br>
> +{<br>
> + struct xrefinfo *xi;<br>
> +<br>
> + /*<br>
> + * If the given xref handle doesn't already exist in the list then we<br>
> + * add a list entry. In theory this can only happen on a system where<br>
> + * nodes don't contain phandle properties and xref and node handles are<br>
> + * synonymous, so the xref handle is added as the node handle as well.<br>
> + */<br>
> + if (xref_init_done) {<br>
> + if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)<br>
> + xrefinfo_add(xref, xref, dev);<br>
> + else<br>
> + xi->dev = dev;<br>
> + return (0);<br>
> + }<br>
> + panic("Attempt to register device before xreflist_init");<br>
> +}<br>
> +<br>
> +/* Call the method in the scope of a given instance. */<br>
> +int<br>
> +OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,<br>
> + ...)<br>
> +{<br>
> + va_list ap;<br>
> + cell_t args_n_results[12];<br>
> + int n, status;<br>
> +<br>
> + if (nargs > 6 || ofw_def_impl == NULL)<br>
> + return (-1);<br>
> + va_start(ap, nreturns);<br>
> + for (n = 0; n < nargs; n++)<br>
> + args_n_results[n] = va_arg(ap, cell_t);<br>
> +<br>
> + status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns,<br>
> + args_n_results);<br>
> + if (status != 0)<br>
> + return (status);<br>
> +<br>
> + for (; n < nargs + nreturns; n++)<br>
> + *va_arg(ap, cell_t *) = args_n_results[n];<br>
> + va_end(ap);<br>
> + return (0);<br>
> +}<br>
> +<br>
> +/*<br>
> + * Device I/O functions<br>
> + */<br>
> +<br>
> +/* Open an instance for a device. */<br>
> +ihandle_t<br>
> +OF_open(const char *device)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (0);<br>
> +<br>
> + return (OFW_OPEN(ofw_obj, device));<br>
> +}<br>
> +<br>
> +/* Close an instance. */<br>
> +void<br>
> +OF_close(ihandle_t instance)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return;<br>
> +<br>
> + OFW_CLOSE(ofw_obj, instance);<br>
> +}<br>
> +<br>
> +/* Read from an instance. */<br>
> +ssize_t<br>
> +OF_read(ihandle_t instance, void *addr, size_t len)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_READ(ofw_obj, instance, addr, len));<br>
> +}<br>
> +<br>
> +/* Write to an instance. */<br>
> +ssize_t<br>
> +OF_write(ihandle_t instance, const void *addr, size_t len)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_WRITE(ofw_obj, instance, addr, len));<br>
> +}<br>
> +<br>
> +/* Seek to a position. */<br>
> +int<br>
> +OF_seek(ihandle_t instance, uint64_t pos)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return (-1);<br>
> +<br>
> + return (OFW_SEEK(ofw_obj, instance, pos));<br>
> +}<br>
> +<br>
> +/*<br>
> + * Memory functions<br>
> + */<br>
> +<br>
> +/* Claim an area of memory. */<br>
> +void *<br>
> +OF_claim(void *virt, size_t size, u_int align)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return ((void *)-1);<br>
> +<br>
> + return (OFW_CLAIM(ofw_obj, virt, size, align));<br>
> +}<br>
> +<br>
> +/* Release an area of memory. */<br>
> +void<br>
> +OF_release(void *virt, size_t size)<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return;<br>
> +<br>
> + OFW_RELEASE(ofw_obj, virt, size);<br>
> +}<br>
> +<br>
> +/*<br>
> + * Control transfer functions<br>
> + */<br>
> +<br>
> +/* Suspend and drop back to the Open Firmware interface. */<br>
> +void<br>
> +OF_enter()<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + return;<br>
> +<br>
> + OFW_ENTER(ofw_obj);<br>
> +}<br>
> +<br>
> +/* Shut down and drop back to the Open Firmware interface. */<br>
> +void<br>
> +OF_exit()<br>
> +{<br>
> +<br>
> + if (ofw_def_impl == NULL)<br>
> + panic("OF_exit: Open Firmware not available");<br>
> +<br>
> + /* Should not return */<br>
> + OFW_EXIT(ofw_obj);<br>
> +<br>
> + for (;;) /* just in case */<br>
> + ;<br>
> +}<br>
> \ No newline at end of file<br>
> --<br>
> 2.17.1<br>
><br>
</blockquote></div></div>