1 /* $NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-4-Clause 5 * 6 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 7 * Copyright (C) 1995, 1996 TooLs GmbH. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by TooLs GmbH. 21 * 4. The name of TooLs GmbH may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 /*- 36 * Copyright (C) 2000 Benno Rice. 37 * All rights reserved. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 48 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 51 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 53 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 54 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 55 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 56 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 57 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 58 */ 59 60 #include <sys/cdefs.h> 61 #include "opt_platform.h" 62 63 #include <sys/param.h> 64 #include <sys/kernel.h> 65 #include <sys/lock.h> 66 #include <sys/malloc.h> 67 #include <sys/mutex.h> 68 #include <sys/queue.h> 69 #include <sys/systm.h> 70 #include <sys/endian.h> 71 #include <sys/stdarg.h> 72 73 #include <dev/ofw/ofwvar.h> 74 #include <dev/ofw/openfirm.h> 75 76 #include "ofw_if.h" 77 78 static void OF_putchar(int c, void *arg); 79 80 MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties"); 81 82 static ihandle_t stdout; 83 84 static ofw_def_t *ofw_def_impl = NULL; 85 static ofw_t ofw_obj; 86 static struct ofw_kobj ofw_kernel_obj; 87 static struct kobj_ops ofw_kernel_kops; 88 89 struct xrefinfo { 90 phandle_t xref; 91 phandle_t node; 92 device_t dev; 93 SLIST_ENTRY(xrefinfo) next_entry; 94 }; 95 96 static SLIST_HEAD(, xrefinfo) xreflist = SLIST_HEAD_INITIALIZER(xreflist); 97 static struct mtx xreflist_lock; 98 static bool xref_init_done; 99 100 #define FIND_BY_XREF 0 101 #define FIND_BY_NODE 1 102 #define FIND_BY_DEV 2 103 104 /* 105 * xref-phandle-device lookup helper routines. 106 * 107 * As soon as we are able to use malloc(), walk the node tree and build a list 108 * of info that cross-references node handles, xref handles, and device_t 109 * instances. This list exists primarily to allow association of a device_t 110 * with an xref handle, but it is also used to speed up translation between xref 111 * and node handles. Before malloc() is available we have to recursively search 112 * the node tree each time we want to translate between a node and xref handle. 113 * Afterwards we can do the translations by searching this much shorter list. 114 */ 115 static void 116 xrefinfo_create(phandle_t node) 117 { 118 struct xrefinfo * xi; 119 phandle_t child, xref; 120 121 /* 122 * Recursively descend from parent, looking for nodes with a property 123 * named either "phandle", "ibm,phandle", or "linux,phandle". For each 124 * such node found create an entry in the xreflist. 125 */ 126 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 127 xrefinfo_create(child); 128 if (OF_getencprop(child, "phandle", &xref, sizeof(xref)) == 129 -1 && OF_getencprop(child, "ibm,phandle", &xref, 130 sizeof(xref)) == -1 && OF_getencprop(child, 131 "linux,phandle", &xref, sizeof(xref)) == -1) 132 continue; 133 xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK | M_ZERO); 134 xi->node = child; 135 xi->xref = xref; 136 SLIST_INSERT_HEAD(&xreflist, xi, next_entry); 137 } 138 } 139 140 static void 141 xrefinfo_init(void *unsed) 142 { 143 144 /* 145 * There is no locking during this init because it runs much earlier 146 * than any of the clients/consumers of the xref list data, but we do 147 * initialize the mutex that will be used for access later. 148 */ 149 mtx_init(&xreflist_lock, "OF xreflist lock", NULL, MTX_DEF); 150 xrefinfo_create(OF_peer(0)); 151 xref_init_done = true; 152 } 153 SYSINIT(xrefinfo, SI_SUB_KMEM, SI_ORDER_ANY, xrefinfo_init, NULL); 154 155 static struct xrefinfo * 156 xrefinfo_find(uintptr_t key, int find_by) 157 { 158 struct xrefinfo *rv, *xi; 159 160 rv = NULL; 161 mtx_lock(&xreflist_lock); 162 SLIST_FOREACH(xi, &xreflist, next_entry) { 163 if ((find_by == FIND_BY_XREF && (phandle_t)key == xi->xref) || 164 (find_by == FIND_BY_NODE && (phandle_t)key == xi->node) || 165 (find_by == FIND_BY_DEV && key == (uintptr_t)xi->dev)) { 166 rv = xi; 167 break; 168 } 169 } 170 mtx_unlock(&xreflist_lock); 171 return (rv); 172 } 173 174 static struct xrefinfo * 175 xrefinfo_add(phandle_t node, phandle_t xref, device_t dev) 176 { 177 struct xrefinfo *xi; 178 179 xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK); 180 xi->node = node; 181 xi->xref = xref; 182 xi->dev = dev; 183 mtx_lock(&xreflist_lock); 184 SLIST_INSERT_HEAD(&xreflist, xi, next_entry); 185 mtx_unlock(&xreflist_lock); 186 return (xi); 187 } 188 189 static void 190 xrefinfo_remove(struct xrefinfo *xi) 191 { 192 193 mtx_lock(&xreflist_lock); 194 SLIST_REMOVE(&xreflist, xi, xrefinfo, next_entry); 195 mtx_unlock(&xreflist_lock); 196 } 197 198 /* 199 * OFW install routines. Highest priority wins, equal priority also 200 * overrides allowing last-set to win. 201 */ 202 SET_DECLARE(ofw_set, ofw_def_t); 203 204 bool 205 OF_install(char *name, int prio) 206 { 207 ofw_def_t *ofwp, **ofwpp; 208 static int curr_prio = 0; 209 210 /* Allow OF layer to be uninstalled */ 211 if (name == NULL) { 212 ofw_def_impl = NULL; 213 return (false); 214 } 215 216 /* 217 * Try and locate the OFW kobj corresponding to the name. 218 */ 219 SET_FOREACH(ofwpp, ofw_set) { 220 ofwp = *ofwpp; 221 222 if (ofwp->name && 223 !strcmp(ofwp->name, name) && 224 prio >= curr_prio) { 225 curr_prio = prio; 226 ofw_def_impl = ofwp; 227 return (true); 228 } 229 } 230 231 return (false); 232 } 233 234 /* Initializer */ 235 int 236 OF_init(void *cookie) 237 { 238 phandle_t chosen; 239 int rv; 240 241 if (ofw_def_impl == NULL) 242 return (-1); 243 244 ofw_obj = &ofw_kernel_obj; 245 /* 246 * Take care of compiling the selected class, and 247 * then statically initialize the OFW object. 248 */ 249 kobj_class_compile_static(ofw_def_impl, &ofw_kernel_kops); 250 kobj_init_static((kobj_t)ofw_obj, ofw_def_impl); 251 252 rv = OFW_INIT(ofw_obj, cookie); 253 254 if ((chosen = OF_finddevice("/chosen")) != -1) 255 if (OF_getencprop(chosen, "stdout", &stdout, 256 sizeof(stdout)) == -1) 257 stdout = -1; 258 259 return (rv); 260 } 261 262 static void 263 OF_putchar(int c, void *arg __unused) 264 { 265 char cbuf; 266 267 if (c == '\n') { 268 cbuf = '\r'; 269 OF_write(stdout, &cbuf, 1); 270 } 271 272 cbuf = c; 273 OF_write(stdout, &cbuf, 1); 274 } 275 276 void 277 OF_printf(const char *fmt, ...) 278 { 279 va_list va; 280 281 va_start(va, fmt); 282 (void)kvprintf(fmt, OF_putchar, NULL, 10, va); 283 va_end(va); 284 } 285 286 /* 287 * Generic functions 288 */ 289 290 /* Test to see if a service exists. */ 291 int 292 OF_test(const char *name) 293 { 294 295 if (ofw_def_impl == NULL) 296 return (-1); 297 298 return (OFW_TEST(ofw_obj, name)); 299 } 300 301 int 302 OF_interpret(const char *cmd, int nreturns, ...) 303 { 304 va_list ap; 305 cell_t slots[16]; 306 int i = 0; 307 int status; 308 309 if (ofw_def_impl == NULL) 310 return (-1); 311 312 status = OFW_INTERPRET(ofw_obj, cmd, nreturns, slots); 313 if (status == -1) 314 return (status); 315 316 va_start(ap, nreturns); 317 while (i < nreturns) 318 *va_arg(ap, cell_t *) = slots[i++]; 319 va_end(ap); 320 321 return (status); 322 } 323 324 /* 325 * Device tree functions 326 */ 327 328 /* Return the next sibling of this node or 0. */ 329 phandle_t 330 OF_peer(phandle_t node) 331 { 332 333 if (ofw_def_impl == NULL) 334 return (0); 335 336 return (OFW_PEER(ofw_obj, node)); 337 } 338 339 /* Return the first child of this node or 0. */ 340 phandle_t 341 OF_child(phandle_t node) 342 { 343 344 if (ofw_def_impl == NULL) 345 return (0); 346 347 return (OFW_CHILD(ofw_obj, node)); 348 } 349 350 /* Return the parent of this node or 0. */ 351 phandle_t 352 OF_parent(phandle_t node) 353 { 354 355 if (ofw_def_impl == NULL) 356 return (0); 357 358 return (OFW_PARENT(ofw_obj, node)); 359 } 360 361 /* Return the package handle that corresponds to an instance handle. */ 362 phandle_t 363 OF_instance_to_package(ihandle_t instance) 364 { 365 366 if (ofw_def_impl == NULL) 367 return (-1); 368 369 return (OFW_INSTANCE_TO_PACKAGE(ofw_obj, instance)); 370 } 371 372 /* Get the length of a property of a package. */ 373 ssize_t 374 OF_getproplen(phandle_t package, const char *propname) 375 { 376 377 if (ofw_def_impl == NULL) 378 return (-1); 379 380 return (OFW_GETPROPLEN(ofw_obj, package, propname)); 381 } 382 383 /* Check existence of a property of a package. */ 384 int 385 OF_hasprop(phandle_t package, const char *propname) 386 { 387 388 return (OF_getproplen(package, propname) >= 0 ? 1 : 0); 389 } 390 391 /* Get the value of a property of a package. */ 392 ssize_t 393 OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen) 394 { 395 396 if (ofw_def_impl == NULL) 397 return (-1); 398 399 return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen)); 400 } 401 402 ssize_t 403 OF_getencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len) 404 { 405 ssize_t retval; 406 int i; 407 408 KASSERT(len % 4 == 0, ("Need a multiple of 4 bytes")); 409 410 retval = OF_getprop(node, propname, buf, len); 411 if (retval <= 0) 412 return (retval); 413 414 for (i = 0; i < len/4; i++) 415 buf[i] = be32toh(buf[i]); 416 417 return (retval); 418 } 419 420 /* 421 * Recursively search the node and its parent for the given property, working 422 * downward from the node to the device tree root. Returns the value of the 423 * first match. 424 */ 425 ssize_t 426 OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len) 427 { 428 ssize_t rv; 429 430 for (; node != 0; node = OF_parent(node)) 431 if ((rv = OF_getprop(node, propname, buf, len)) != -1) 432 return (rv); 433 return (-1); 434 } 435 436 ssize_t 437 OF_searchencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len) 438 { 439 ssize_t rv; 440 441 for (; node != 0; node = OF_parent(node)) 442 if ((rv = OF_getencprop(node, propname, buf, len)) != -1) 443 return (rv); 444 return (-1); 445 } 446 447 /* 448 * Store the value of a property of a package into newly allocated memory 449 * (using the M_OFWPROP malloc pool and M_WAITOK). 450 */ 451 ssize_t 452 OF_getprop_alloc(phandle_t package, const char *propname, void **buf) 453 { 454 int len; 455 456 *buf = NULL; 457 if ((len = OF_getproplen(package, propname)) == -1) 458 return (-1); 459 460 if (len > 0) { 461 *buf = malloc(len, M_OFWPROP, M_WAITOK); 462 if (OF_getprop(package, propname, *buf, len) == -1) { 463 free(*buf, M_OFWPROP); 464 *buf = NULL; 465 return (-1); 466 } 467 } 468 return (len); 469 } 470 471 /* 472 * Store the value of a property of a package into newly allocated memory 473 * (using the M_OFWPROP malloc pool and M_WAITOK). elsz is the size of a 474 * single element, the number of elements is return in number. 475 */ 476 ssize_t 477 OF_getprop_alloc_multi(phandle_t package, const char *propname, int elsz, void **buf) 478 { 479 int len; 480 481 *buf = NULL; 482 if ((len = OF_getproplen(package, propname)) == -1 || 483 len % elsz != 0) 484 return (-1); 485 486 if (len > 0) { 487 *buf = malloc(len, M_OFWPROP, M_WAITOK); 488 if (OF_getprop(package, propname, *buf, len) == -1) { 489 free(*buf, M_OFWPROP); 490 *buf = NULL; 491 return (-1); 492 } 493 } 494 return (len / elsz); 495 } 496 497 ssize_t 498 OF_getencprop_alloc(phandle_t package, const char *name, void **buf) 499 { 500 ssize_t ret; 501 502 ret = OF_getencprop_alloc_multi(package, name, sizeof(pcell_t), 503 buf); 504 if (ret < 0) 505 return (ret); 506 else 507 return (ret * sizeof(pcell_t)); 508 } 509 510 ssize_t 511 OF_getencprop_alloc_multi(phandle_t package, const char *name, int elsz, 512 void **buf) 513 { 514 ssize_t retval; 515 pcell_t *cell; 516 int i; 517 518 retval = OF_getprop_alloc_multi(package, name, elsz, buf); 519 if (retval == -1) 520 return (-1); 521 522 cell = *buf; 523 for (i = 0; i < retval * elsz / 4; i++) 524 cell[i] = be32toh(cell[i]); 525 526 return (retval); 527 } 528 529 /* Free buffer allocated by OF_getencprop_alloc or OF_getprop_alloc */ 530 void OF_prop_free(void *buf) 531 { 532 533 free(buf, M_OFWPROP); 534 } 535 536 /* Get the next property of a package. */ 537 int 538 OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size) 539 { 540 541 if (ofw_def_impl == NULL) 542 return (-1); 543 544 return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size)); 545 } 546 547 /* Set the value of a property of a package. */ 548 int 549 OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len) 550 { 551 552 if (ofw_def_impl == NULL) 553 return (-1); 554 555 return (OFW_SETPROP(ofw_obj, package, propname, buf,len)); 556 } 557 558 /* Convert a device specifier to a fully qualified pathname. */ 559 ssize_t 560 OF_canon(const char *device, char *buf, size_t len) 561 { 562 563 if (ofw_def_impl == NULL) 564 return (-1); 565 566 return (OFW_CANON(ofw_obj, device, buf, len)); 567 } 568 569 /* Return a package handle for the specified device. */ 570 phandle_t 571 OF_finddevice(const char *device) 572 { 573 574 if (ofw_def_impl == NULL) 575 return (-1); 576 577 return (OFW_FINDDEVICE(ofw_obj, device)); 578 } 579 580 /* Return the fully qualified pathname corresponding to an instance. */ 581 ssize_t 582 OF_instance_to_path(ihandle_t instance, char *buf, size_t len) 583 { 584 585 if (ofw_def_impl == NULL) 586 return (-1); 587 588 return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len)); 589 } 590 591 /* Return the fully qualified pathname corresponding to a package. */ 592 ssize_t 593 OF_package_to_path(phandle_t package, char *buf, size_t len) 594 { 595 596 if (ofw_def_impl == NULL) 597 return (-1); 598 599 return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len)); 600 } 601 602 /* Look up effective phandle (see FDT/PAPR spec) */ 603 static phandle_t 604 OF_child_xref_phandle(phandle_t parent, phandle_t xref) 605 { 606 phandle_t child, rxref; 607 608 /* 609 * Recursively descend from parent, looking for a node with a property 610 * named either "phandle", "ibm,phandle", or "linux,phandle" that 611 * matches the xref we are looking for. 612 */ 613 614 for (child = OF_child(parent); child != 0; child = OF_peer(child)) { 615 rxref = OF_child_xref_phandle(child, xref); 616 if (rxref != -1) 617 return (rxref); 618 619 if (OF_getencprop(child, "phandle", &rxref, sizeof(rxref)) == 620 -1 && OF_getencprop(child, "ibm,phandle", &rxref, 621 sizeof(rxref)) == -1 && OF_getencprop(child, 622 "linux,phandle", &rxref, sizeof(rxref)) == -1) 623 continue; 624 625 if (rxref == xref) 626 return (child); 627 } 628 629 return (-1); 630 } 631 632 phandle_t 633 OF_node_from_xref(phandle_t xref) 634 { 635 struct xrefinfo *xi; 636 phandle_t node; 637 638 if (xref_init_done) { 639 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL) 640 return (xref); 641 return (xi->node); 642 } 643 644 if ((node = OF_child_xref_phandle(OF_peer(0), xref)) == -1) 645 return (xref); 646 return (node); 647 } 648 649 phandle_t 650 OF_xref_from_node(phandle_t node) 651 { 652 struct xrefinfo *xi; 653 phandle_t xref; 654 655 if (xref_init_done) { 656 if ((xi = xrefinfo_find(node, FIND_BY_NODE)) == NULL) 657 return (node); 658 return (xi->xref); 659 } 660 661 if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) == -1 && 662 OF_getencprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 && 663 OF_getencprop(node, "linux,phandle", &xref, sizeof(xref)) == -1) 664 return (node); 665 return (xref); 666 } 667 668 device_t 669 OF_device_from_xref(phandle_t xref) 670 { 671 struct xrefinfo *xi; 672 673 if (xref_init_done) { 674 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL) 675 return (NULL); 676 return (xi->dev); 677 } 678 panic("Attempt to find device before xreflist_init"); 679 } 680 681 phandle_t 682 OF_xref_from_device(device_t dev) 683 { 684 struct xrefinfo *xi; 685 686 if (xref_init_done) { 687 if ((xi = xrefinfo_find((uintptr_t)dev, FIND_BY_DEV)) == NULL) 688 return (0); 689 return (xi->xref); 690 } 691 panic("Attempt to find xref before xreflist_init"); 692 } 693 694 int 695 OF_device_register_xref(phandle_t xref, device_t dev) 696 { 697 struct xrefinfo *xi; 698 699 /* 700 * If the given xref handle doesn't already exist in the list then we 701 * add a list entry. In theory this can only happen on a system where 702 * nodes don't contain phandle properties and xref and node handles are 703 * synonymous, so the xref handle is added as the node handle as well. 704 */ 705 if (xref_init_done) { 706 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL) 707 xrefinfo_add(xref, xref, dev); 708 else 709 xi->dev = dev; 710 return (0); 711 } 712 panic("Attempt to register device before xreflist_init"); 713 } 714 715 void 716 OF_device_unregister_xref(phandle_t xref, device_t dev) 717 { 718 struct xrefinfo *xi; 719 720 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL) 721 return; 722 xrefinfo_remove(xi); 723 } 724 725 /* Call the method in the scope of a given instance. */ 726 int 727 OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns, 728 ...) 729 { 730 va_list ap; 731 cell_t args_n_results[12]; 732 int n, status; 733 734 if (nargs > 6 || ofw_def_impl == NULL) 735 return (-1); 736 va_start(ap, nreturns); 737 for (n = 0; n < nargs; n++) 738 args_n_results[n] = va_arg(ap, cell_t); 739 740 status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns, 741 args_n_results); 742 if (status != 0) 743 return (status); 744 745 for (; n < nargs + nreturns; n++) 746 *va_arg(ap, cell_t *) = args_n_results[n]; 747 va_end(ap); 748 return (0); 749 } 750 751 /* 752 * Device I/O functions 753 */ 754 755 /* Open an instance for a device. */ 756 ihandle_t 757 OF_open(const char *device) 758 { 759 760 if (ofw_def_impl == NULL) 761 return (0); 762 763 return (OFW_OPEN(ofw_obj, device)); 764 } 765 766 /* Close an instance. */ 767 void 768 OF_close(ihandle_t instance) 769 { 770 771 if (ofw_def_impl == NULL) 772 return; 773 774 OFW_CLOSE(ofw_obj, instance); 775 } 776 777 /* Read from an instance. */ 778 ssize_t 779 OF_read(ihandle_t instance, void *addr, size_t len) 780 { 781 782 if (ofw_def_impl == NULL) 783 return (-1); 784 785 return (OFW_READ(ofw_obj, instance, addr, len)); 786 } 787 788 /* Write to an instance. */ 789 ssize_t 790 OF_write(ihandle_t instance, const void *addr, size_t len) 791 { 792 793 if (ofw_def_impl == NULL) 794 return (-1); 795 796 return (OFW_WRITE(ofw_obj, instance, addr, len)); 797 } 798 799 /* Seek to a position. */ 800 int 801 OF_seek(ihandle_t instance, uint64_t pos) 802 { 803 804 if (ofw_def_impl == NULL) 805 return (-1); 806 807 return (OFW_SEEK(ofw_obj, instance, pos)); 808 } 809 810 /* 811 * Memory functions 812 */ 813 814 /* Claim an area of memory. */ 815 void * 816 OF_claim(void *virt, size_t size, u_int align) 817 { 818 819 if (ofw_def_impl == NULL) 820 return ((void *)-1); 821 822 return (OFW_CLAIM(ofw_obj, virt, size, align)); 823 } 824 825 /* Release an area of memory. */ 826 void 827 OF_release(void *virt, size_t size) 828 { 829 830 if (ofw_def_impl == NULL) 831 return; 832 833 OFW_RELEASE(ofw_obj, virt, size); 834 } 835 836 /* 837 * Control transfer functions 838 */ 839 840 /* Suspend and drop back to the Open Firmware interface. */ 841 void 842 OF_enter(void) 843 { 844 845 if (ofw_def_impl == NULL) 846 return; 847 848 OFW_ENTER(ofw_obj); 849 } 850 851 /* Shut down and drop back to the Open Firmware interface. */ 852 void 853 OF_exit(void) 854 { 855 856 if (ofw_def_impl == NULL) 857 panic("OF_exit: Open Firmware not available"); 858 859 /* Should not return */ 860 OFW_EXIT(ofw_obj); 861 862 for (;;) /* just in case */ 863 ; 864 } 865