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