2000-10-16 10:46:22 +00:00
|
|
|
/* $NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $ */
|
|
|
|
|
2005-01-06 01:43:34 +00:00
|
|
|
/*-
|
2000-10-16 10:46:22 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2005-01-06 01:43:34 +00:00
|
|
|
/*-
|
2000-10-16 10:46:22 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-06-16 21:38:04 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2010-05-28 10:43:56 +00:00
|
|
|
#include "opt_platform.h"
|
|
|
|
|
2001-07-31 03:36:26 +00:00
|
|
|
#include <sys/param.h>
|
2001-11-18 20:38:44 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
2001-06-16 07:17:56 +00:00
|
|
|
#include <sys/systm.h>
|
2013-10-22 20:57:24 +00:00
|
|
|
#include <sys/endian.h>
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2001-06-16 07:17:56 +00:00
|
|
|
#include <machine/stdarg.h>
|
2000-11-10 06:39:58 +00:00
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
#include <dev/ofw/ofwvar.h>
|
2009-06-14 00:05:38 +00:00
|
|
|
#include <dev/ofw/openfirm.h>
|
2008-12-20 00:33:10 +00:00
|
|
|
|
|
|
|
#include "ofw_if.h"
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2012-01-27 22:29:29 +00:00
|
|
|
static void OF_putchar(int c, void *arg);
|
|
|
|
|
2004-08-16 15:45:27 +00:00
|
|
|
MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties");
|
2001-11-18 20:38:44 +00:00
|
|
|
|
2000-10-16 10:46:22 +00:00
|
|
|
static ihandle_t stdout;
|
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
static ofw_def_t *ofw_def_impl = NULL;
|
2008-12-20 00:33:10 +00:00
|
|
|
static ofw_t ofw_obj;
|
|
|
|
static struct ofw_kobj ofw_kernel_obj;
|
|
|
|
static struct kobj_ops ofw_kernel_kops;
|
|
|
|
|
|
|
|
/*
|
2012-01-27 22:29:29 +00:00
|
|
|
* OFW install routines. Highest priority wins, equal priority also
|
2008-12-20 00:33:10 +00:00
|
|
|
* overrides allowing last-set to win.
|
|
|
|
*/
|
|
|
|
SET_DECLARE(ofw_set, ofw_def_t);
|
|
|
|
|
|
|
|
boolean_t
|
|
|
|
OF_install(char *name, int prio)
|
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
ofw_def_t *ofwp, **ofwpp;
|
2008-12-20 00:33:10 +00:00
|
|
|
static int curr_prio = 0;
|
|
|
|
|
|
|
|
/*
|
2009-06-14 00:05:38 +00:00
|
|
|
* Try and locate the OFW kobj corresponding to the name.
|
|
|
|
*/
|
2008-12-20 00:33:10 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-06-14 00:05:38 +00:00
|
|
|
/* Initializer */
|
2010-05-28 10:43:56 +00:00
|
|
|
int
|
2008-12-20 00:33:10 +00:00
|
|
|
OF_init(void *cookie)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2007-06-16 21:38:04 +00:00
|
|
|
phandle_t chosen;
|
2010-05-28 10:43:56 +00:00
|
|
|
int rv;
|
2001-06-16 07:17:56 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
ofw_obj = &ofw_kernel_obj;
|
|
|
|
/*
|
|
|
|
* Take care of compiling the selected class, and
|
2009-06-14 00:05:38 +00:00
|
|
|
* then statically initialize the OFW object.
|
2008-12-20 00:33:10 +00:00
|
|
|
*/
|
|
|
|
kobj_class_compile_static(ofw_def_impl, &ofw_kernel_kops);
|
As it turns out, r186347 actually is insufficient to avoid the use of the
curthread-accessing part of mtx_{,un}lock(9) when using a r210623-style
curthread implementation on sparc64, crashing the kernel in its early
cycles as PCPU isn't set up, yet (and can't be set up as OFW is one of the
things we need for that, which leads to a chicken-and-egg problem). What
happens is that due to the fact that the idea of r210623 actually is to
allow the compiler to cache invocations of curthread, it factors out
obtaining curthread needed for both mtx_lock(9) and mtx_unlock(9) to
before the branch based on kobj_mutex_inited when compiling the kernel
without the debugging options. So change kobj_class_compile_static(9)
to just never acquire kobj_mtx, effectively restricting it to its
documented use, and add a kobj_init_static(9) for initializing objects
using a class compiled with the former and that also avoids using mutex(9)
(and malloc(9)). Also assert in both of these functions that they are
used in their intended way only.
While at it, inline kobj_register_method() and kobj_unregister_method()
as there wasn't much point for factoring them out in the first place
and so that a reader of the code has to figure out the locking for
fewer functions missing a KOBJ_ASSERT.
Tested on powerpc{,64} by andreast.
Reviewed by: nwhitehorn (earlier version), jhb
MFC after: 3 days
2011-11-15 20:11:03 +00:00
|
|
|
kobj_init_static((kobj_t)ofw_obj, ofw_def_impl);
|
2008-12-20 00:33:10 +00:00
|
|
|
|
2010-05-28 10:43:56 +00:00
|
|
|
rv = OFW_INIT(ofw_obj, cookie);
|
|
|
|
|
2011-12-02 15:24:39 +00:00
|
|
|
if ((chosen = OF_finddevice("/chosen")) != -1)
|
2013-10-23 13:55:41 +00:00
|
|
|
if (OF_getencprop(chosen, "stdout", &stdout,
|
|
|
|
sizeof(stdout)) == -1)
|
2010-05-28 10:43:56 +00:00
|
|
|
stdout = -1;
|
2008-12-20 00:33:10 +00:00
|
|
|
|
2010-05-28 10:43:56 +00:00
|
|
|
return (rv);
|
2001-06-16 07:17:56 +00:00
|
|
|
}
|
|
|
|
|
2012-01-27 22:29:29 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2001-06-16 07:17:56 +00:00
|
|
|
void
|
|
|
|
OF_printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
2012-01-27 22:29:29 +00:00
|
|
|
(void)kvprintf(fmt, OF_putchar, NULL, 10, va);
|
2001-06-16 07:17:56 +00:00
|
|
|
va_end(va);
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Test to see if a service exists. */
|
|
|
|
int
|
2008-12-20 00:33:10 +00:00
|
|
|
OF_test(const char *name)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_TEST(ofw_obj, name));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
2003-06-07 18:34:04 +00:00
|
|
|
int
|
2008-12-20 00:33:10 +00:00
|
|
|
OF_interpret(const char *cmd, int nreturns, ...)
|
2003-06-07 18:34:04 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
2010-09-11 18:55:00 +00:00
|
|
|
cell_t slots[16];
|
2003-06-07 18:34:04 +00:00
|
|
|
int i = 0;
|
2008-12-20 00:33:10 +00:00
|
|
|
int status;
|
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
status = OFW_INTERPRET(ofw_obj, cmd, nreturns, slots);
|
2009-03-29 15:10:49 +00:00
|
|
|
if (status == -1)
|
|
|
|
return (status);
|
2003-06-07 18:34:04 +00:00
|
|
|
|
2007-07-06 00:47:44 +00:00
|
|
|
va_start(ap, nreturns);
|
2009-03-29 15:10:49 +00:00
|
|
|
while (i < nreturns)
|
2008-12-20 00:33:10 +00:00
|
|
|
*va_arg(ap, cell_t *) = slots[i++];
|
2003-06-07 18:34:04 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (status);
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Device tree functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Return the next sibling of this node or 0. */
|
|
|
|
phandle_t
|
|
|
|
OF_peer(phandle_t node)
|
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (0);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_PEER(ofw_obj, node));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the first child of this node or 0. */
|
|
|
|
phandle_t
|
|
|
|
OF_child(phandle_t node)
|
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (0);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_CHILD(ofw_obj, node));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the parent of this node or 0. */
|
|
|
|
phandle_t
|
|
|
|
OF_parent(phandle_t node)
|
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (0);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_PARENT(ofw_obj, node));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the package handle that corresponds to an instance handle. */
|
|
|
|
phandle_t
|
|
|
|
OF_instance_to_package(ihandle_t instance)
|
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_INSTANCE_TO_PACKAGE(ofw_obj, instance));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the length of a property of a package. */
|
2008-12-20 00:33:10 +00:00
|
|
|
ssize_t
|
|
|
|
OF_getproplen(phandle_t package, const char *propname)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_GETPROPLEN(ofw_obj, package, propname));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
2012-08-18 11:25:07 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2000-10-16 10:46:22 +00:00
|
|
|
/* Get the value of a property of a package. */
|
2008-12-20 00:33:10 +00:00
|
|
|
ssize_t
|
|
|
|
OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
2013-10-22 20:57:24 +00:00
|
|
|
ssize_t
|
|
|
|
OF_getencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len)
|
|
|
|
{
|
|
|
|
ssize_t retval;
|
|
|
|
int i;
|
|
|
|
|
2013-10-23 03:59:51 +00:00
|
|
|
KASSERT(len % 4 == 0, ("Need a multiple of 4 bytes"));
|
2013-10-22 20:57:24 +00:00
|
|
|
|
|
|
|
retval = OF_getprop(node, propname, buf, len);
|
|
|
|
for (i = 0; i < len/4; i++)
|
|
|
|
buf[i] = be32toh(buf[i]);
|
|
|
|
|
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
|
2009-01-03 19:38:47 +00:00
|
|
|
/*
|
2010-05-28 10:43:56 +00:00
|
|
|
* Recursively search the node and its parent for the given property, working
|
2009-01-03 19:38:47 +00:00
|
|
|
* downward from the node to the device tree root. Returns the value of the
|
|
|
|
* first match.
|
|
|
|
*/
|
|
|
|
ssize_t
|
2009-06-14 00:05:38 +00:00
|
|
|
OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len)
|
2009-01-03 19:38:47 +00:00
|
|
|
{
|
|
|
|
ssize_t rv;
|
|
|
|
|
2009-06-14 00:05:38 +00:00
|
|
|
for (; node != 0; node = OF_parent(node))
|
2009-01-03 19:38:47 +00:00
|
|
|
if ((rv = OF_getprop(node, propname, buf, len)) != -1)
|
|
|
|
return (rv);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2013-10-22 21:20:05 +00:00
|
|
|
ssize_t
|
|
|
|
OF_searchencprop(phandle_t node, const char *propname, void *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);
|
|
|
|
}
|
|
|
|
|
2001-11-18 20:38:44 +00:00
|
|
|
/*
|
2004-05-22 16:45:19 +00:00
|
|
|
* Store the value of a property of a package into newly allocated memory
|
2009-06-14 00:05:38 +00:00
|
|
|
* (using the M_OFWPROP malloc pool and M_WAITOK). elsz is the size of a
|
2004-05-22 16:45:19 +00:00
|
|
|
* single element, the number of elements is return in number.
|
2001-11-18 20:38:44 +00:00
|
|
|
*/
|
2008-12-20 00:33:10 +00:00
|
|
|
ssize_t
|
|
|
|
OF_getprop_alloc(phandle_t package, const char *propname, int elsz, void **buf)
|
2001-11-18 20:38:44 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
*buf = NULL;
|
|
|
|
if ((len = OF_getproplen(package, propname)) == -1 ||
|
|
|
|
len % elsz != 0)
|
|
|
|
return (-1);
|
|
|
|
|
2003-02-19 05:47:46 +00:00
|
|
|
*buf = malloc(len, M_OFWPROP, M_WAITOK);
|
2001-11-18 20:38:44 +00:00
|
|
|
if (OF_getprop(package, propname, *buf, len) == -1) {
|
|
|
|
free(*buf, M_OFWPROP);
|
|
|
|
*buf = NULL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (len / elsz);
|
|
|
|
}
|
|
|
|
|
2013-10-22 21:20:05 +00:00
|
|
|
ssize_t
|
|
|
|
OF_getencprop_alloc(phandle_t package, const char *name, int elsz, void **buf)
|
|
|
|
{
|
|
|
|
ssize_t retval;
|
|
|
|
pcell_t *cell;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
retval = OF_getprop_alloc(package, name, elsz, buf);
|
2013-10-23 13:55:41 +00:00
|
|
|
if (retval == -1 || retval*elsz % 4 != 0)
|
|
|
|
return (-1);
|
2013-10-22 21:20:05 +00:00
|
|
|
|
|
|
|
cell = *buf;
|
|
|
|
for (i = 0; i < retval*elsz/4; i++)
|
|
|
|
cell[i] = be32toh(cell[i]);
|
|
|
|
|
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
|
2000-10-16 10:46:22 +00:00
|
|
|
/* Get the next property of a package. */
|
|
|
|
int
|
2008-12-20 00:33:10 +00:00
|
|
|
OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the value of a property of a package. */
|
|
|
|
int
|
2008-12-20 00:33:10 +00:00
|
|
|
OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_SETPROP(ofw_obj, package, propname, buf,len));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a device specifier to a fully qualified pathname. */
|
2008-12-20 00:33:10 +00:00
|
|
|
ssize_t
|
|
|
|
OF_canon(const char *device, char *buf, size_t len)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_CANON(ofw_obj, device, buf, len));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a package handle for the specified device. */
|
|
|
|
phandle_t
|
2000-11-10 06:39:58 +00:00
|
|
|
OF_finddevice(const char *device)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_FINDDEVICE(ofw_obj, device));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the fully qualified pathname corresponding to an instance. */
|
2008-12-20 00:33:10 +00:00
|
|
|
ssize_t
|
|
|
|
OF_instance_to_path(ihandle_t instance, char *buf, size_t len)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the fully qualified pathname corresponding to a package. */
|
2008-12-20 00:33:10 +00:00
|
|
|
ssize_t
|
|
|
|
OF_package_to_path(phandle_t package, char *buf, size_t len)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
2013-09-15 14:19:17 +00:00
|
|
|
/* 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);
|
|
|
|
|
2013-10-23 13:55:41 +00:00
|
|
|
if (OF_getencprop(child, "phandle", &rxref, sizeof(rxref)) ==
|
|
|
|
-1 && OF_getencprop(child, "ibm,phandle", &rxref,
|
|
|
|
sizeof(rxref)) == -1 && OF_getencprop(child,
|
2013-09-15 14:19:17 +00:00
|
|
|
"linux,phandle", &rxref, sizeof(rxref)) == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (rxref == xref)
|
|
|
|
return (child);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
phandle_t
|
|
|
|
OF_xref_phandle(phandle_t xref)
|
|
|
|
{
|
|
|
|
phandle_t node;
|
|
|
|
|
|
|
|
node = OF_child_xref_phandle(OF_peer(0), xref);
|
|
|
|
if (node == -1)
|
|
|
|
return (xref);
|
|
|
|
|
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
|
2000-10-16 10:46:22 +00:00
|
|
|
/* Call the method in the scope of a given instance. */
|
|
|
|
int
|
2008-12-20 00:33:10 +00:00
|
|
|
OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,
|
|
|
|
...)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
2010-07-08 14:29:23 +00:00
|
|
|
cell_t args_n_results[12];
|
2008-12-20 00:33:10 +00:00
|
|
|
int n, status;
|
2000-10-16 10:46:22 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (nargs > 6 || ofw_def_impl == NULL)
|
2007-06-16 21:38:04 +00:00
|
|
|
return (-1);
|
2000-10-16 10:46:22 +00:00
|
|
|
va_start(ap, nreturns);
|
2008-12-20 00:33:10 +00:00
|
|
|
for (n = 0; n < nargs; n++)
|
2010-07-08 14:29:23 +00:00
|
|
|
args_n_results[n] = va_arg(ap, cell_t);
|
2008-12-20 00:33:10 +00:00
|
|
|
|
|
|
|
status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns,
|
|
|
|
args_n_results);
|
|
|
|
if (status != 0)
|
|
|
|
return (status);
|
|
|
|
|
|
|
|
for (; n < nargs + nreturns; n++)
|
2010-07-08 14:29:23 +00:00
|
|
|
*va_arg(ap, cell_t *) = args_n_results[n];
|
2000-10-16 10:46:22 +00:00
|
|
|
va_end(ap);
|
2007-06-16 21:38:04 +00:00
|
|
|
return (0);
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-06-16 21:38:04 +00:00
|
|
|
* Device I/O functions
|
2000-10-16 10:46:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Open an instance for a device. */
|
|
|
|
ihandle_t
|
2008-12-20 00:33:10 +00:00
|
|
|
OF_open(const char *device)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (0);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_OPEN(ofw_obj, device));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Close an instance. */
|
|
|
|
void
|
|
|
|
OF_close(ihandle_t instance)
|
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return;
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
OFW_CLOSE(ofw_obj, instance);
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read from an instance. */
|
2008-12-20 00:33:10 +00:00
|
|
|
ssize_t
|
|
|
|
OF_read(ihandle_t instance, void *addr, size_t len)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_READ(ofw_obj, instance, addr, len));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write to an instance. */
|
2008-12-20 00:33:10 +00:00
|
|
|
ssize_t
|
|
|
|
OF_write(ihandle_t instance, const void *addr, size_t len)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_WRITE(ofw_obj, instance, addr, len));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Seek to a position. */
|
|
|
|
int
|
2008-12-20 00:33:10 +00:00
|
|
|
OF_seek(ihandle_t instance, uint64_t pos)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_SEEK(ofw_obj, instance, pos));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-06-16 21:38:04 +00:00
|
|
|
* Memory functions
|
2000-10-16 10:46:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Claim an area of memory. */
|
|
|
|
void *
|
2008-12-20 00:33:10 +00:00
|
|
|
OF_claim(void *virt, size_t size, u_int align)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return ((void *)-1);
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
return (OFW_CLAIM(ofw_obj, virt, size, align));
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Release an area of memory. */
|
|
|
|
void
|
2008-12-20 00:33:10 +00:00
|
|
|
OF_release(void *virt, size_t size)
|
2000-10-16 10:46:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return;
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
OFW_RELEASE(ofw_obj, virt, size);
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-06-16 21:38:04 +00:00
|
|
|
* Control transfer functions
|
2000-10-16 10:46:22 +00:00
|
|
|
*/
|
|
|
|
|
2004-08-16 15:45:27 +00:00
|
|
|
/* Suspend and drop back to the Open Firmware interface. */
|
2000-10-16 10:46:22 +00:00
|
|
|
void
|
|
|
|
OF_enter()
|
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
return;
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
OFW_ENTER(ofw_obj);
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|
|
|
|
|
2004-08-16 15:45:27 +00:00
|
|
|
/* Shut down and drop back to the Open Firmware interface. */
|
2000-11-10 06:39:58 +00:00
|
|
|
void
|
2000-10-16 10:46:22 +00:00
|
|
|
OF_exit()
|
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
|
2010-11-09 19:45:29 +00:00
|
|
|
if (ofw_def_impl == NULL)
|
|
|
|
panic("OF_exit: Open Firmware not available");
|
|
|
|
|
2008-12-20 00:33:10 +00:00
|
|
|
/* Should not return */
|
|
|
|
OFW_EXIT(ofw_obj);
|
|
|
|
|
2007-06-16 21:38:04 +00:00
|
|
|
for (;;) /* just in case */
|
|
|
|
;
|
2000-10-16 10:46:22 +00:00
|
|
|
}
|