openfirm.c revision 256932
1/* $NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $ */ 2 3/*- 4 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 5 * Copyright (C) 1995, 1996 TooLs GmbH. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by TooLs GmbH. 19 * 4. The name of TooLs GmbH may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33/*- 34 * Copyright (C) 2000 Benno Rice. 35 * All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 46 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 49 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 52 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 53 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 54 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 55 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57 58#include <sys/cdefs.h> 59__FBSDID("$FreeBSD: head/sys/dev/ofw/openfirm.c 256932 2013-10-22 20:57:24Z nwhitehorn $"); 60 61#include "opt_platform.h" 62 63#include <sys/param.h> 64#include <sys/kernel.h> 65#include <sys/malloc.h> 66#include <sys/systm.h> 67#include <sys/endian.h> 68 69#include <machine/stdarg.h> 70 71#include <dev/ofw/ofwvar.h> 72#include <dev/ofw/openfirm.h> 73 74#include "ofw_if.h" 75 76static void OF_putchar(int c, void *arg); 77 78MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties"); 79 80static ihandle_t stdout; 81 82static ofw_def_t *ofw_def_impl = NULL; 83static ofw_t ofw_obj; 84static struct ofw_kobj ofw_kernel_obj; 85static struct kobj_ops ofw_kernel_kops; 86 87/* 88 * OFW install routines. Highest priority wins, equal priority also 89 * overrides allowing last-set to win. 90 */ 91SET_DECLARE(ofw_set, ofw_def_t); 92 93boolean_t 94OF_install(char *name, int prio) 95{ 96 ofw_def_t *ofwp, **ofwpp; 97 static int curr_prio = 0; 98 99 /* 100 * Try and locate the OFW kobj corresponding to the name. 101 */ 102 SET_FOREACH(ofwpp, ofw_set) { 103 ofwp = *ofwpp; 104 105 if (ofwp->name && 106 !strcmp(ofwp->name, name) && 107 prio >= curr_prio) { 108 curr_prio = prio; 109 ofw_def_impl = ofwp; 110 return (TRUE); 111 } 112 } 113 114 return (FALSE); 115} 116 117/* Initializer */ 118int 119OF_init(void *cookie) 120{ 121 phandle_t chosen; 122 int rv; 123 124 if (ofw_def_impl == NULL) 125 return (-1); 126 127 ofw_obj = &ofw_kernel_obj; 128 /* 129 * Take care of compiling the selected class, and 130 * then statically initialize the OFW object. 131 */ 132 kobj_class_compile_static(ofw_def_impl, &ofw_kernel_kops); 133 kobj_init_static((kobj_t)ofw_obj, ofw_def_impl); 134 135 rv = OFW_INIT(ofw_obj, cookie); 136 137 if ((chosen = OF_finddevice("/chosen")) != -1) 138 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) 139 stdout = -1; 140 141 return (rv); 142} 143 144static void 145OF_putchar(int c, void *arg __unused) 146{ 147 char cbuf; 148 149 if (c == '\n') { 150 cbuf = '\r'; 151 OF_write(stdout, &cbuf, 1); 152 } 153 154 cbuf = c; 155 OF_write(stdout, &cbuf, 1); 156} 157 158void 159OF_printf(const char *fmt, ...) 160{ 161 va_list va; 162 163 va_start(va, fmt); 164 (void)kvprintf(fmt, OF_putchar, NULL, 10, va); 165 va_end(va); 166} 167 168/* 169 * Generic functions 170 */ 171 172/* Test to see if a service exists. */ 173int 174OF_test(const char *name) 175{ 176 177 if (ofw_def_impl == NULL) 178 return (-1); 179 180 return (OFW_TEST(ofw_obj, name)); 181} 182 183int 184OF_interpret(const char *cmd, int nreturns, ...) 185{ 186 va_list ap; 187 cell_t slots[16]; 188 int i = 0; 189 int status; 190 191 if (ofw_def_impl == NULL) 192 return (-1); 193 194 status = OFW_INTERPRET(ofw_obj, cmd, nreturns, slots); 195 if (status == -1) 196 return (status); 197 198 va_start(ap, nreturns); 199 while (i < nreturns) 200 *va_arg(ap, cell_t *) = slots[i++]; 201 va_end(ap); 202 203 return (status); 204} 205 206/* 207 * Device tree functions 208 */ 209 210/* Return the next sibling of this node or 0. */ 211phandle_t 212OF_peer(phandle_t node) 213{ 214 215 if (ofw_def_impl == NULL) 216 return (0); 217 218 return (OFW_PEER(ofw_obj, node)); 219} 220 221/* Return the first child of this node or 0. */ 222phandle_t 223OF_child(phandle_t node) 224{ 225 226 if (ofw_def_impl == NULL) 227 return (0); 228 229 return (OFW_CHILD(ofw_obj, node)); 230} 231 232/* Return the parent of this node or 0. */ 233phandle_t 234OF_parent(phandle_t node) 235{ 236 237 if (ofw_def_impl == NULL) 238 return (0); 239 240 return (OFW_PARENT(ofw_obj, node)); 241} 242 243/* Return the package handle that corresponds to an instance handle. */ 244phandle_t 245OF_instance_to_package(ihandle_t instance) 246{ 247 248 if (ofw_def_impl == NULL) 249 return (-1); 250 251 return (OFW_INSTANCE_TO_PACKAGE(ofw_obj, instance)); 252} 253 254/* Get the length of a property of a package. */ 255ssize_t 256OF_getproplen(phandle_t package, const char *propname) 257{ 258 259 if (ofw_def_impl == NULL) 260 return (-1); 261 262 return (OFW_GETPROPLEN(ofw_obj, package, propname)); 263} 264 265/* Check existence of a property of a package. */ 266int 267OF_hasprop(phandle_t package, const char *propname) 268{ 269 270 return (OF_getproplen(package, propname) >= 0 ? 1 : 0); 271} 272 273/* Get the value of a property of a package. */ 274ssize_t 275OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen) 276{ 277 278 if (ofw_def_impl == NULL) 279 return (-1); 280 281 return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen)); 282} 283 284ssize_t 285OF_getencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len) 286{ 287 ssize_t retval; 288 int i; 289 290 KASSERT(len % 4 == 0, "Need a multiple of 4 bytes"); 291 292 retval = OF_getprop(node, propname, buf, len); 293 for (i = 0; i < len/4; i++) 294 buf[i] = be32toh(buf[i]); 295 296 return (retval); 297} 298 299/* 300 * Recursively search the node and its parent for the given property, working 301 * downward from the node to the device tree root. Returns the value of the 302 * first match. 303 */ 304ssize_t 305OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len) 306{ 307 ssize_t rv; 308 309 for (; node != 0; node = OF_parent(node)) 310 if ((rv = OF_getprop(node, propname, buf, len)) != -1) 311 return (rv); 312 return (-1); 313} 314 315/* 316 * Store the value of a property of a package into newly allocated memory 317 * (using the M_OFWPROP malloc pool and M_WAITOK). elsz is the size of a 318 * single element, the number of elements is return in number. 319 */ 320ssize_t 321OF_getprop_alloc(phandle_t package, const char *propname, int elsz, void **buf) 322{ 323 int len; 324 325 *buf = NULL; 326 if ((len = OF_getproplen(package, propname)) == -1 || 327 len % elsz != 0) 328 return (-1); 329 330 *buf = malloc(len, M_OFWPROP, M_WAITOK); 331 if (OF_getprop(package, propname, *buf, len) == -1) { 332 free(*buf, M_OFWPROP); 333 *buf = NULL; 334 return (-1); 335 } 336 return (len / elsz); 337} 338 339/* Get the next property of a package. */ 340int 341OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size) 342{ 343 344 if (ofw_def_impl == NULL) 345 return (-1); 346 347 return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size)); 348} 349 350/* Set the value of a property of a package. */ 351int 352OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len) 353{ 354 355 if (ofw_def_impl == NULL) 356 return (-1); 357 358 return (OFW_SETPROP(ofw_obj, package, propname, buf,len)); 359} 360 361/* Convert a device specifier to a fully qualified pathname. */ 362ssize_t 363OF_canon(const char *device, char *buf, size_t len) 364{ 365 366 if (ofw_def_impl == NULL) 367 return (-1); 368 369 return (OFW_CANON(ofw_obj, device, buf, len)); 370} 371 372/* Return a package handle for the specified device. */ 373phandle_t 374OF_finddevice(const char *device) 375{ 376 377 if (ofw_def_impl == NULL) 378 return (-1); 379 380 return (OFW_FINDDEVICE(ofw_obj, device)); 381} 382 383/* Return the fully qualified pathname corresponding to an instance. */ 384ssize_t 385OF_instance_to_path(ihandle_t instance, char *buf, size_t len) 386{ 387 388 if (ofw_def_impl == NULL) 389 return (-1); 390 391 return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len)); 392} 393 394/* Return the fully qualified pathname corresponding to a package. */ 395ssize_t 396OF_package_to_path(phandle_t package, char *buf, size_t len) 397{ 398 399 if (ofw_def_impl == NULL) 400 return (-1); 401 402 return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len)); 403} 404 405/* Look up effective phandle (see FDT/PAPR spec) */ 406static phandle_t 407OF_child_xref_phandle(phandle_t parent, phandle_t xref) 408{ 409 phandle_t child, rxref; 410 411 /* 412 * Recursively descend from parent, looking for a node with a property 413 * named either "phandle", "ibm,phandle", or "linux,phandle" that 414 * matches the xref we are looking for. 415 */ 416 417 for (child = OF_child(parent); child != 0; child = OF_peer(child)) { 418 rxref = OF_child_xref_phandle(child, xref); 419 if (rxref != -1) 420 return (rxref); 421 422 if (OF_getprop(child, "phandle", &rxref, sizeof(rxref)) == -1 && 423 OF_getprop(child, "ibm,phandle", &rxref, 424 sizeof(rxref)) == -1 && OF_getprop(child, 425 "linux,phandle", &rxref, sizeof(rxref)) == -1) 426 continue; 427 428 if (rxref == xref) 429 return (child); 430 } 431 432 return (-1); 433} 434 435phandle_t 436OF_xref_phandle(phandle_t xref) 437{ 438 phandle_t node; 439 440 node = OF_child_xref_phandle(OF_peer(0), xref); 441 if (node == -1) 442 return (xref); 443 444 return (node); 445} 446 447/* Call the method in the scope of a given instance. */ 448int 449OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns, 450 ...) 451{ 452 va_list ap; 453 cell_t args_n_results[12]; 454 int n, status; 455 456 if (nargs > 6 || ofw_def_impl == NULL) 457 return (-1); 458 va_start(ap, nreturns); 459 for (n = 0; n < nargs; n++) 460 args_n_results[n] = va_arg(ap, cell_t); 461 462 status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns, 463 args_n_results); 464 if (status != 0) 465 return (status); 466 467 for (; n < nargs + nreturns; n++) 468 *va_arg(ap, cell_t *) = args_n_results[n]; 469 va_end(ap); 470 return (0); 471} 472 473/* 474 * Device I/O functions 475 */ 476 477/* Open an instance for a device. */ 478ihandle_t 479OF_open(const char *device) 480{ 481 482 if (ofw_def_impl == NULL) 483 return (0); 484 485 return (OFW_OPEN(ofw_obj, device)); 486} 487 488/* Close an instance. */ 489void 490OF_close(ihandle_t instance) 491{ 492 493 if (ofw_def_impl == NULL) 494 return; 495 496 OFW_CLOSE(ofw_obj, instance); 497} 498 499/* Read from an instance. */ 500ssize_t 501OF_read(ihandle_t instance, void *addr, size_t len) 502{ 503 504 if (ofw_def_impl == NULL) 505 return (-1); 506 507 return (OFW_READ(ofw_obj, instance, addr, len)); 508} 509 510/* Write to an instance. */ 511ssize_t 512OF_write(ihandle_t instance, const void *addr, size_t len) 513{ 514 515 if (ofw_def_impl == NULL) 516 return (-1); 517 518 return (OFW_WRITE(ofw_obj, instance, addr, len)); 519} 520 521/* Seek to a position. */ 522int 523OF_seek(ihandle_t instance, uint64_t pos) 524{ 525 526 if (ofw_def_impl == NULL) 527 return (-1); 528 529 return (OFW_SEEK(ofw_obj, instance, pos)); 530} 531 532/* 533 * Memory functions 534 */ 535 536/* Claim an area of memory. */ 537void * 538OF_claim(void *virt, size_t size, u_int align) 539{ 540 541 if (ofw_def_impl == NULL) 542 return ((void *)-1); 543 544 return (OFW_CLAIM(ofw_obj, virt, size, align)); 545} 546 547/* Release an area of memory. */ 548void 549OF_release(void *virt, size_t size) 550{ 551 552 if (ofw_def_impl == NULL) 553 return; 554 555 OFW_RELEASE(ofw_obj, virt, size); 556} 557 558/* 559 * Control transfer functions 560 */ 561 562/* Suspend and drop back to the Open Firmware interface. */ 563void 564OF_enter() 565{ 566 567 if (ofw_def_impl == NULL) 568 return; 569 570 OFW_ENTER(ofw_obj); 571} 572 573/* Shut down and drop back to the Open Firmware interface. */ 574void 575OF_exit() 576{ 577 578 if (ofw_def_impl == NULL) 579 panic("OF_exit: Open Firmware not available"); 580 581 /* Should not return */ 582 OFW_EXIT(ofw_obj); 583 584 for (;;) /* just in case */ 585 ; 586} 587