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$"); 60 61 #include <sys/param.h> 62 #include <sys/kernel.h> 63 #include <sys/malloc.h> 64 #include <sys/systm.h> 65 66 #include <machine/stdarg.h> 67 68 #include <dev/ofw/ofwvar.h> 69 #include <dev/ofw/openfirm.h> 70 71 #include "ofw_if.h" 72 73 static int ofw_std_init(ofw_t ofw, void *openfirm); 74 static int ofw_std_test(ofw_t ofw, const char *name); 75 static int ofw_std_interpret(ofw_t ofw, const char *cmd, int nreturns, 76 unsigned long *returns); 77 static phandle_t ofw_std_peer(ofw_t ofw, phandle_t node); 78 static phandle_t ofw_std_child(ofw_t ofw, phandle_t node); 79 static phandle_t ofw_std_parent(ofw_t ofw, phandle_t node); 80 static phandle_t ofw_std_instance_to_package(ofw_t ofw, ihandle_t instance); 81 static ssize_t ofw_std_getproplen(ofw_t ofw, phandle_t package, 82 const char *propname); 83 static ssize_t ofw_std_getprop(ofw_t ofw, phandle_t package, 84 const char *propname, void *buf, size_t buflen); 85 static int ofw_std_nextprop(ofw_t ofw, phandle_t package, const char *previous, 86 char *buf, size_t); 87 static int ofw_std_setprop(ofw_t ofw, phandle_t package, const char *propname, 88 const void *buf, size_t len); 89 static ssize_t ofw_std_canon(ofw_t ofw, const char *device, char *buf, 90 size_t len); 91 static phandle_t ofw_std_finddevice(ofw_t ofw, const char *device); 92 static ssize_t ofw_std_instance_to_path(ofw_t ofw, ihandle_t instance, 93 char *buf, size_t len); 94 static ssize_t ofw_std_package_to_path(ofw_t ofw, phandle_t package, char *buf, 95 size_t len); 96 static int ofw_std_call_method(ofw_t ofw, ihandle_t instance, 97 const char *method, int nargs, int nreturns, 98 cell_t *args_and_returns); 99 static ihandle_t ofw_std_open(ofw_t ofw, const char *device); 100 static void ofw_std_close(ofw_t ofw, ihandle_t instance); 101 static ssize_t ofw_std_read(ofw_t ofw, ihandle_t instance, void *addr, 102 size_t len); 103 static ssize_t ofw_std_write(ofw_t ofw, ihandle_t instance, const void *addr, 104 size_t len); 105 static int ofw_std_seek(ofw_t ofw, ihandle_t instance, uint64_t pos); 106 static caddr_t ofw_std_claim(ofw_t ofw, void *virt, size_t size, u_int align); 107 static void ofw_std_release(ofw_t ofw, void *virt, size_t size); 108 static void ofw_std_enter(ofw_t ofw); 109 static void ofw_std_exit(ofw_t ofw); 110 111 static ofw_method_t ofw_std_methods[] = { 112 OFWMETHOD(ofw_init, ofw_std_init), 113 OFWMETHOD(ofw_peer, ofw_std_peer), 114 OFWMETHOD(ofw_child, ofw_std_child), 115 OFWMETHOD(ofw_parent, ofw_std_parent), 116 OFWMETHOD(ofw_instance_to_package, ofw_std_instance_to_package), 117 OFWMETHOD(ofw_getproplen, ofw_std_getproplen), 118 OFWMETHOD(ofw_getprop, ofw_std_getprop), 119 OFWMETHOD(ofw_nextprop, ofw_std_nextprop), 120 OFWMETHOD(ofw_setprop, ofw_std_setprop), 121 OFWMETHOD(ofw_canon, ofw_std_canon), 122 OFWMETHOD(ofw_finddevice, ofw_std_finddevice), 123 OFWMETHOD(ofw_instance_to_path, ofw_std_instance_to_path), 124 OFWMETHOD(ofw_package_to_path, ofw_std_package_to_path), 125 126 OFWMETHOD(ofw_test, ofw_std_test), 127 OFWMETHOD(ofw_call_method, ofw_std_call_method), 128 OFWMETHOD(ofw_interpret, ofw_std_interpret), 129 OFWMETHOD(ofw_open, ofw_std_open), 130 OFWMETHOD(ofw_close, ofw_std_close), 131 OFWMETHOD(ofw_read, ofw_std_read), 132 OFWMETHOD(ofw_write, ofw_std_write), 133 OFWMETHOD(ofw_seek, ofw_std_seek), 134 OFWMETHOD(ofw_claim, ofw_std_claim), 135 OFWMETHOD(ofw_release, ofw_std_release), 136 OFWMETHOD(ofw_enter, ofw_std_enter), 137 OFWMETHOD(ofw_exit, ofw_std_exit), 138 139 { 0, 0 } 140 }; 141 142 static ofw_def_t ofw_std = { 143 OFW_STD_DIRECT, 144 ofw_std_methods, 145 0 146 }; 147 OFW_DEF(ofw_std); 148 149 static int (*openfirmware)(void *); 150 151 /* Initializer */ 152 153 static int 154 ofw_std_init(ofw_t ofw, void *openfirm) 155 { 156 157 openfirmware = (int (*)(void *))openfirm; 158 return (0); 159 } 160 161 /* 162 * Generic functions 163 */ 164 165 /* Test to see if a service exists. */ 166 static int 167 ofw_std_test(ofw_t ofw, const char *name) 168 { 169 struct { 170 cell_t name; 171 cell_t nargs; 172 cell_t nreturns; 173 cell_t service; 174 cell_t missing; 175 } args = { 176 (cell_t)"test", 177 1, 178 1, 179 }; 180 181 args.service = (cell_t)name; 182 if (openfirmware(&args) == -1) 183 return (-1); 184 return (args.missing); 185 } 186 187 static int 188 ofw_std_interpret(ofw_t ofw, const char *cmd, int nreturns, 189 unsigned long *returns) 190 { 191 struct { 192 cell_t name; 193 cell_t nargs; 194 cell_t nreturns; 195 cell_t slot[16]; 196 } args = { 197 (cell_t)"interpret", 198 1, 199 }; 200 cell_t status; 201 int i = 0, j = 0; 202 203 args.nreturns = ++nreturns; 204 args.slot[i++] = (cell_t)cmd; 205 if (openfirmware(&args) == -1) 206 return (-1); 207 status = args.slot[i++]; 208 while (i < 1 + nreturns) 209 returns[j++] = args.slot[i++]; 210 return (status); 211 } 212 213 /* 214 * Device tree functions 215 */ 216 217 /* Return the next sibling of this node or 0. */ 218 static phandle_t 219 ofw_std_peer(ofw_t ofw, phandle_t node) 220 { 221 struct { 222 cell_t name; 223 cell_t nargs; 224 cell_t nreturns; 225 cell_t node; 226 cell_t next; 227 } args = { 228 (cell_t)"peer", 229 1, 230 1, 231 }; 232 233 args.node = node; 234 if (openfirmware(&args) == -1) 235 return (-1); 236 return (args.next); 237 } 238 239 /* Return the first child of this node or 0. */ 240 static phandle_t 241 ofw_std_child(ofw_t ofw, phandle_t node) 242 { 243 struct { 244 cell_t name; 245 cell_t nargs; 246 cell_t nreturns; 247 cell_t node; 248 cell_t child; 249 } args = { 250 (cell_t)"child", 251 1, 252 1, 253 }; 254 255 args.node = node; 256 if (openfirmware(&args) == -1) 257 return (-1); 258 return (args.child); 259 } 260 261 /* Return the parent of this node or 0. */ 262 static phandle_t 263 ofw_std_parent(ofw_t ofw, phandle_t node) 264 { 265 struct { 266 cell_t name; 267 cell_t nargs; 268 cell_t nreturns; 269 cell_t node; 270 cell_t parent; 271 } args = { 272 (cell_t)"parent", 273 1, 274 1, 275 }; 276 277 args.node = node; 278 if (openfirmware(&args) == -1) 279 return (-1); 280 return (args.parent); 281 } 282 283 /* Return the package handle that corresponds to an instance handle. */ 284 static phandle_t 285 ofw_std_instance_to_package(ofw_t ofw, ihandle_t instance) 286 { 287 struct { 288 cell_t name; 289 cell_t nargs; 290 cell_t nreturns; 291 cell_t instance; 292 cell_t package; 293 } args = { 294 (cell_t)"instance-to-package", 295 1, 296 1, 297 }; 298 299 args.instance = instance; 300 if (openfirmware(&args) == -1) 301 return (-1); 302 return (args.package); 303 } 304 305 /* Get the length of a property of a package. */ 306 static ssize_t 307 ofw_std_getproplen(ofw_t ofw, phandle_t package, const char *propname) 308 { 309 struct { 310 cell_t name; 311 cell_t nargs; 312 cell_t nreturns; 313 cell_t package; 314 cell_t propname; 315 cell_t proplen; 316 } args = { 317 (cell_t)"getproplen", 318 2, 319 1, 320 }; 321 322 args.package = package; 323 args.propname = (cell_t)propname; 324 if (openfirmware(&args) == -1) 325 return (-1); 326 return (args.proplen); 327 } 328 329 /* Get the value of a property of a package. */ 330 static ssize_t 331 ofw_std_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf, 332 size_t buflen) 333 { 334 struct { 335 cell_t name; 336 cell_t nargs; 337 cell_t nreturns; 338 cell_t package; 339 cell_t propname; 340 cell_t buf; 341 cell_t buflen; 342 cell_t size; 343 } args = { 344 (cell_t)"getprop", 345 4, 346 1, 347 }; 348 349 args.package = package; 350 args.propname = (cell_t)propname; 351 args.buf = (cell_t)buf; 352 args.buflen = buflen; 353 if (openfirmware(&args) == -1) 354 return (-1); 355 return (args.size); 356 } 357 358 /* Get the next property of a package. */ 359 static int 360 ofw_std_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf, 361 size_t size) 362 { 363 struct { 364 cell_t name; 365 cell_t nargs; 366 cell_t nreturns; 367 cell_t package; 368 cell_t previous; 369 cell_t buf; 370 cell_t flag; 371 } args = { 372 (cell_t)"nextprop", 373 3, 374 1, 375 }; 376 377 args.package = package; 378 args.previous = (cell_t)previous; 379 args.buf = (cell_t)buf; 380 if (openfirmware(&args) == -1) 381 return (-1); 382 return (args.flag); 383 } 384 385 /* Set the value of a property of a package. */ 386 /* XXX Has a bug on FirePower */ 387 static int 388 ofw_std_setprop(ofw_t ofw, phandle_t package, const char *propname, 389 const void *buf, size_t len) 390 { 391 struct { 392 cell_t name; 393 cell_t nargs; 394 cell_t nreturns; 395 cell_t package; 396 cell_t propname; 397 cell_t buf; 398 cell_t len; 399 cell_t size; 400 } args = { 401 (cell_t)"setprop", 402 4, 403 1, 404 }; 405 406 args.package = package; 407 args.propname = (cell_t)propname; 408 args.buf = (cell_t)buf; 409 args.len = len; 410 if (openfirmware(&args) == -1) 411 return (-1); 412 return (args.size); 413 } 414 415 /* Convert a device specifier to a fully qualified pathname. */ 416 static ssize_t 417 ofw_std_canon(ofw_t ofw, const char *device, char *buf, size_t len) 418 { 419 struct { 420 cell_t name; 421 cell_t nargs; 422 cell_t nreturns; 423 cell_t device; 424 cell_t buf; 425 cell_t len; 426 cell_t size; 427 } args = { 428 (cell_t)"canon", 429 3, 430 1, 431 }; 432 433 args.device = (cell_t)device; 434 args.buf = (cell_t)buf; 435 args.len = len; 436 if (openfirmware(&args) == -1) 437 return (-1); 438 return (args.size); 439 } 440 441 /* Return a package handle for the specified device. */ 442 static phandle_t 443 ofw_std_finddevice(ofw_t ofw, const char *device) 444 { 445 struct { 446 cell_t name; 447 cell_t nargs; 448 cell_t nreturns; 449 cell_t device; 450 cell_t package; 451 } args = { 452 (cell_t)"finddevice", 453 1, 454 1, 455 }; 456 457 args.device = (cell_t)device; 458 if (openfirmware(&args) == -1) 459 return (-1); 460 return (args.package); 461 } 462 463 /* Return the fully qualified pathname corresponding to an instance. */ 464 static ssize_t 465 ofw_std_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len) 466 { 467 struct { 468 cell_t name; 469 cell_t nargs; 470 cell_t nreturns; 471 cell_t instance; 472 cell_t buf; 473 cell_t len; 474 cell_t size; 475 } args = { 476 (cell_t)"instance-to-path", 477 3, 478 1, 479 }; 480 481 args.instance = instance; 482 args.buf = (cell_t)buf; 483 args.len = len; 484 if (openfirmware(&args) == -1) 485 return (-1); 486 return (args.size); 487 } 488 489 /* Return the fully qualified pathname corresponding to a package. */ 490 static ssize_t 491 ofw_std_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len) 492 { 493 struct { 494 cell_t name; 495 cell_t nargs; 496 cell_t nreturns; 497 cell_t package; 498 cell_t buf; 499 cell_t len; 500 cell_t size; 501 } args = { 502 (cell_t)"package-to-path", 503 3, 504 1, 505 }; 506 507 args.package = package; 508 args.buf = (cell_t)buf; 509 args.len = len; 510 if (openfirmware(&args) == -1) 511 return (-1); 512 return (args.size); 513 } 514 515 /* Call the method in the scope of a given instance. */ 516 static int 517 ofw_std_call_method(ofw_t ofw, ihandle_t instance, const char *method, 518 int nargs, int nreturns, cell_t *args_and_returns) 519 { 520 struct { 521 cell_t name; 522 cell_t nargs; 523 cell_t nreturns; 524 cell_t method; 525 cell_t instance; 526 cell_t args_n_results[12]; 527 } args = { 528 (cell_t)"call-method", 529 2, 530 1, 531 }; 532 cell_t *cp, *ap; 533 int n; 534 535 if (nargs > 6) 536 return (-1); 537 args.nargs = nargs + 2; 538 args.nreturns = nreturns + 1; 539 args.method = (cell_t)method; 540 args.instance = instance; 541 542 ap = args_and_returns; 543 for (cp = args.args_n_results + (n = nargs); --n >= 0;) 544 *--cp = *(ap++); 545 if (openfirmware(&args) == -1) 546 return (-1); 547 if (args.args_n_results[nargs]) 548 return (args.args_n_results[nargs]); 549 for (cp = args.args_n_results + nargs + (n = args.nreturns); --n > 0;) 550 *(ap++) = *--cp; 551 return (0); 552 } 553 554 /* 555 * Device I/O functions 556 */ 557 558 /* Open an instance for a device. */ 559 static ihandle_t 560 ofw_std_open(ofw_t ofw, const char *device) 561 { 562 struct { 563 cell_t name; 564 cell_t nargs; 565 cell_t nreturns; 566 cell_t device; 567 cell_t instance; 568 } args = { 569 (cell_t)"open", 570 1, 571 1, 572 }; 573 574 args.device = (cell_t)device; 575 if (openfirmware(&args) == -1 || args.instance == 0) 576 return (-1); 577 return (args.instance); 578 } 579 580 /* Close an instance. */ 581 static void 582 ofw_std_close(ofw_t ofw, ihandle_t instance) 583 { 584 struct { 585 cell_t name; 586 cell_t nargs; 587 cell_t nreturns; 588 cell_t instance; 589 } args = { 590 (cell_t)"close", 591 1, 592 0, 593 }; 594 595 args.instance = instance; 596 openfirmware(&args); 597 } 598 599 /* Read from an instance. */ 600 static ssize_t 601 ofw_std_read(ofw_t ofw, ihandle_t instance, void *addr, size_t len) 602 { 603 struct { 604 cell_t name; 605 cell_t nargs; 606 cell_t nreturns; 607 cell_t instance; 608 cell_t addr; 609 cell_t len; 610 cell_t actual; 611 } args = { 612 (cell_t)"read", 613 3, 614 1, 615 }; 616 617 args.instance = instance; 618 args.addr = (cell_t)addr; 619 args.len = len; 620 if (openfirmware(&args) == -1) 621 return (-1); 622 623 return (args.actual); 624 } 625 626 /* Write to an instance. */ 627 static ssize_t 628 ofw_std_write(ofw_t ofw, ihandle_t instance, const void *addr, size_t len) 629 { 630 struct { 631 cell_t name; 632 cell_t nargs; 633 cell_t nreturns; 634 cell_t instance; 635 cell_t addr; 636 cell_t len; 637 cell_t actual; 638 } args = { 639 (cell_t)"write", 640 3, 641 1, 642 }; 643 644 args.instance = instance; 645 args.addr = (cell_t)addr; 646 args.len = len; 647 if (openfirmware(&args) == -1) 648 return (-1); 649 return (args.actual); 650 } 651 652 /* Seek to a position. */ 653 static int 654 ofw_std_seek(ofw_t ofw, ihandle_t instance, uint64_t pos) 655 { 656 struct { 657 cell_t name; 658 cell_t nargs; 659 cell_t nreturns; 660 cell_t instance; 661 cell_t poshi; 662 cell_t poslo; 663 cell_t status; 664 } args = { 665 (cell_t)"seek", 666 3, 667 1, 668 }; 669 670 args.instance = instance; 671 args.poshi = pos >> 32; 672 args.poslo = pos; 673 if (openfirmware(&args) == -1) 674 return (-1); 675 return (args.status); 676 } 677 678 /* 679 * Memory functions 680 */ 681 682 /* Claim an area of memory. */ 683 static caddr_t 684 ofw_std_claim(ofw_t ofw, void *virt, size_t size, u_int align) 685 { 686 struct { 687 cell_t name; 688 cell_t nargs; 689 cell_t nreturns; 690 cell_t virt; 691 cell_t size; 692 cell_t align; 693 cell_t baseaddr; 694 } args = { 695 (cell_t)"claim", 696 3, 697 1, 698 }; 699 700 args.virt = (cell_t)virt; 701 args.size = size; 702 args.align = align; 703 if (openfirmware(&args) == -1) 704 return ((void *)-1); 705 return ((void *)args.baseaddr); 706 } 707 708 /* Release an area of memory. */ 709 static void 710 ofw_std_release(ofw_t ofw, void *virt, size_t size) 711 { 712 struct { 713 cell_t name; 714 cell_t nargs; 715 cell_t nreturns; 716 cell_t virt; 717 cell_t size; 718 } args = { 719 (cell_t)"release", 720 2, 721 0, 722 }; 723 724 args.virt = (cell_t)virt; 725 args.size = size; 726 openfirmware(&args); 727 } 728 729 /* 730 * Control transfer functions 731 */ 732 733 /* Suspend and drop back to the Open Firmware interface. */ 734 static void 735 ofw_std_enter(ofw_t ofw) 736 { 737 struct { 738 cell_t name; 739 cell_t nargs; 740 cell_t nreturns; 741 } args = { 742 (cell_t)"enter", 743 0, 744 0, 745 }; 746 747 openfirmware(&args); 748 /* We may come back. */ 749 } 750 751 /* Shut down and drop back to the Open Firmware interface. */ 752 static void 753 ofw_std_exit(ofw_t ofw) 754 { 755 struct { 756 cell_t name; 757 cell_t nargs; 758 cell_t nreturns; 759 } args = { 760 (cell_t)"exit", 761 0, 762 0, 763 }; 764 765 openfirmware(&args); 766 for (;;) /* just in case */ 767 ; 768 } 769