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 unsigned long *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, unsigned long *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; 533 unsigned long *ap; 534 int n; 535 536 if (nargs > 6) 537 return (-1); 538 args.nargs = nargs + 2; 539 args.nreturns = nreturns + 1; 540 args.method = (cell_t)method; 541 args.instance = instance; 542 543 ap = args_and_returns; 544 for (cp = args.args_n_results + (n = nargs); --n >= 0;) 545 *--cp = *(ap++); 546 if (openfirmware(&args) == -1) 547 return (-1); 548 if (args.args_n_results[nargs]) 549 return (args.args_n_results[nargs]); 550 for (cp = args.args_n_results + nargs + (n = args.nreturns); --n > 0;) 551 *(ap++) = *--cp; 552 return (0); 553 } 554 555 /* 556 * Device I/O functions 557 */ 558 559 /* Open an instance for a device. */ 560 static ihandle_t 561 ofw_std_open(ofw_t ofw, const char *device) 562 { 563 struct { 564 cell_t name; 565 cell_t nargs; 566 cell_t nreturns; 567 cell_t device; 568 cell_t instance; 569 } args = { 570 (cell_t)"open", 571 1, 572 1, 573 }; 574 575 args.device = (cell_t)device; 576 if (openfirmware(&args) == -1 || args.instance == 0) 577 return (-1); 578 return (args.instance); 579 } 580 581 /* Close an instance. */ 582 static void 583 ofw_std_close(ofw_t ofw, ihandle_t instance) 584 { 585 struct { 586 cell_t name; 587 cell_t nargs; 588 cell_t nreturns; 589 cell_t instance; 590 } args = { 591 (cell_t)"close", 592 1, 593 0, 594 }; 595 596 args.instance = instance; 597 openfirmware(&args); 598 } 599 600 /* Read from an instance. */ 601 static ssize_t 602 ofw_std_read(ofw_t ofw, ihandle_t instance, void *addr, size_t len) 603 { 604 struct { 605 cell_t name; 606 cell_t nargs; 607 cell_t nreturns; 608 cell_t instance; 609 cell_t addr; 610 cell_t len; 611 cell_t actual; 612 } args = { 613 (cell_t)"read", 614 3, 615 1, 616 }; 617 618 args.instance = instance; 619 args.addr = (cell_t)addr; 620 args.len = len; 621 if (openfirmware(&args) == -1) 622 return (-1); 623 624 return (args.actual); 625 } 626 627 /* Write to an instance. */ 628 static ssize_t 629 ofw_std_write(ofw_t ofw, ihandle_t instance, const void *addr, size_t len) 630 { 631 struct { 632 cell_t name; 633 cell_t nargs; 634 cell_t nreturns; 635 cell_t instance; 636 cell_t addr; 637 cell_t len; 638 cell_t actual; 639 } args = { 640 (cell_t)"write", 641 3, 642 1, 643 }; 644 645 args.instance = instance; 646 args.addr = (cell_t)addr; 647 args.len = len; 648 if (openfirmware(&args) == -1) 649 return (-1); 650 return (args.actual); 651 } 652 653 /* Seek to a position. */ 654 static int 655 ofw_std_seek(ofw_t ofw, ihandle_t instance, uint64_t pos) 656 { 657 struct { 658 cell_t name; 659 cell_t nargs; 660 cell_t nreturns; 661 cell_t instance; 662 cell_t poshi; 663 cell_t poslo; 664 cell_t status; 665 } args = { 666 (cell_t)"seek", 667 3, 668 1, 669 }; 670 671 args.instance = instance; 672 args.poshi = pos >> 32; 673 args.poslo = pos; 674 if (openfirmware(&args) == -1) 675 return (-1); 676 return (args.status); 677 } 678 679 /* 680 * Memory functions 681 */ 682 683 /* Claim an area of memory. */ 684 static caddr_t 685 ofw_std_claim(ofw_t ofw, void *virt, size_t size, u_int align) 686 { 687 struct { 688 cell_t name; 689 cell_t nargs; 690 cell_t nreturns; 691 cell_t virt; 692 cell_t size; 693 cell_t align; 694 cell_t baseaddr; 695 } args = { 696 (cell_t)"claim", 697 3, 698 1, 699 }; 700 701 args.virt = (cell_t)virt; 702 args.size = size; 703 args.align = align; 704 if (openfirmware(&args) == -1) 705 return ((void *)-1); 706 return ((void *)args.baseaddr); 707 } 708 709 /* Release an area of memory. */ 710 static void 711 ofw_std_release(ofw_t ofw, void *virt, size_t size) 712 { 713 struct { 714 cell_t name; 715 cell_t nargs; 716 cell_t nreturns; 717 cell_t virt; 718 cell_t size; 719 } args = { 720 (cell_t)"release", 721 2, 722 0, 723 }; 724 725 args.virt = (cell_t)virt; 726 args.size = size; 727 openfirmware(&args); 728 } 729 730 /* 731 * Control transfer functions 732 */ 733 734 /* Suspend and drop back to the Open Firmware interface. */ 735 static void 736 ofw_std_enter(ofw_t ofw) 737 { 738 struct { 739 cell_t name; 740 cell_t nargs; 741 cell_t nreturns; 742 } args = { 743 (cell_t)"enter", 744 0, 745 0, 746 }; 747 748 openfirmware(&args); 749 /* We may come back. */ 750 } 751 752 /* Shut down and drop back to the Open Firmware interface. */ 753 static void 754 ofw_std_exit(ofw_t ofw) 755 { 756 struct { 757 cell_t name; 758 cell_t nargs; 759 cell_t nreturns; 760 } args = { 761 (cell_t)"exit", 762 0, 763 0, 764 }; 765 766 openfirmware(&args); 767 for (;;) /* just in case */ 768 ; 769 } 770