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 void 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 void 154 ofw_std_init(ofw_t ofw, void *openfirm) 155 { 156 157 openfirmware = (int (*)(void *))openfirm; 158 } 159 160 /* 161 * Generic functions 162 */ 163 164 /* Test to see if a service exists. */ 165 static int 166 ofw_std_test(ofw_t ofw, const char *name) 167 { 168 static struct { 169 cell_t name; 170 cell_t nargs; 171 cell_t nreturns; 172 cell_t service; 173 cell_t missing; 174 } args = { 175 (cell_t)"test", 176 1, 177 1, 178 }; 179 180 args.service = (cell_t)name; 181 if (openfirmware(&args) == -1) 182 return (-1); 183 return (args.missing); 184 } 185 186 static int 187 ofw_std_interpret(ofw_t ofw, const char *cmd, int nreturns, 188 unsigned long *returns) 189 { 190 static struct { 191 cell_t name; 192 cell_t nargs; 193 cell_t nreturns; 194 cell_t slot[16]; 195 } args = { 196 (cell_t)"interpret", 197 1, 198 }; 199 cell_t status; 200 int i = 0, j = 0; 201 202 args.nreturns = ++nreturns; 203 args.slot[i++] = (cell_t)cmd; 204 if (openfirmware(&args) == -1) 205 return (-1); 206 status = args.slot[i++]; 207 while (i < 1 + nreturns) 208 returns[j++] = args.slot[i++]; 209 return (status); 210 } 211 212 /* 213 * Device tree functions 214 */ 215 216 /* Return the next sibling of this node or 0. */ 217 static phandle_t 218 ofw_std_peer(ofw_t ofw, phandle_t node) 219 { 220 static struct { 221 cell_t name; 222 cell_t nargs; 223 cell_t nreturns; 224 cell_t node; 225 cell_t next; 226 } args = { 227 (cell_t)"peer", 228 1, 229 1, 230 }; 231 232 args.node = node; 233 if (openfirmware(&args) == -1) 234 return (-1); 235 return (args.next); 236 } 237 238 /* Return the first child of this node or 0. */ 239 static phandle_t 240 ofw_std_child(ofw_t ofw, phandle_t node) 241 { 242 static struct { 243 cell_t name; 244 cell_t nargs; 245 cell_t nreturns; 246 cell_t node; 247 cell_t child; 248 } args = { 249 (cell_t)"child", 250 1, 251 1, 252 }; 253 254 args.node = node; 255 if (openfirmware(&args) == -1) 256 return (-1); 257 return (args.child); 258 } 259 260 /* Return the parent of this node or 0. */ 261 static phandle_t 262 ofw_std_parent(ofw_t ofw, phandle_t node) 263 { 264 static struct { 265 cell_t name; 266 cell_t nargs; 267 cell_t nreturns; 268 cell_t node; 269 cell_t parent; 270 } args = { 271 (cell_t)"parent", 272 1, 273 1, 274 }; 275 276 args.node = node; 277 if (openfirmware(&args) == -1) 278 return (-1); 279 return (args.parent); 280 } 281 282 /* Return the package handle that corresponds to an instance handle. */ 283 static phandle_t 284 ofw_std_instance_to_package(ofw_t ofw, ihandle_t instance) 285 { 286 static struct { 287 cell_t name; 288 cell_t nargs; 289 cell_t nreturns; 290 cell_t instance; 291 cell_t package; 292 } args = { 293 (cell_t)"instance-to-package", 294 1, 295 1, 296 }; 297 298 args.instance = instance; 299 if (openfirmware(&args) == -1) 300 return (-1); 301 return (args.package); 302 } 303 304 /* Get the length of a property of a package. */ 305 static ssize_t 306 ofw_std_getproplen(ofw_t ofw, phandle_t package, const char *propname) 307 { 308 static struct { 309 cell_t name; 310 cell_t nargs; 311 cell_t nreturns; 312 cell_t package; 313 cell_t propname; 314 cell_t proplen; 315 } args = { 316 (cell_t)"getproplen", 317 2, 318 1, 319 }; 320 321 args.package = package; 322 args.propname = (cell_t)propname; 323 if (openfirmware(&args) == -1) 324 return (-1); 325 return (args.proplen); 326 } 327 328 /* Get the value of a property of a package. */ 329 static ssize_t 330 ofw_std_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf, 331 size_t buflen) 332 { 333 static struct { 334 cell_t name; 335 cell_t nargs; 336 cell_t nreturns; 337 cell_t package; 338 cell_t propname; 339 cell_t buf; 340 cell_t buflen; 341 cell_t size; 342 } args = { 343 (cell_t)"getprop", 344 4, 345 1, 346 }; 347 348 args.package = package; 349 args.propname = (cell_t)propname; 350 args.buf = (cell_t)buf; 351 args.buflen = buflen; 352 if (openfirmware(&args) == -1) 353 return (-1); 354 return (args.size); 355 } 356 357 /* Get the next property of a package. */ 358 static int 359 ofw_std_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf, 360 size_t size) 361 { 362 static struct { 363 cell_t name; 364 cell_t nargs; 365 cell_t nreturns; 366 cell_t package; 367 cell_t previous; 368 cell_t buf; 369 cell_t flag; 370 } args = { 371 (cell_t)"nextprop", 372 3, 373 1, 374 }; 375 376 args.package = package; 377 args.previous = (cell_t)previous; 378 args.buf = (cell_t)buf; 379 if (openfirmware(&args) == -1) 380 return (-1); 381 return (args.flag); 382 } 383 384 /* Set the value of a property of a package. */ 385 /* XXX Has a bug on FirePower */ 386 static int 387 ofw_std_setprop(ofw_t ofw, phandle_t package, const char *propname, 388 const void *buf, size_t len) 389 { 390 static struct { 391 cell_t name; 392 cell_t nargs; 393 cell_t nreturns; 394 cell_t package; 395 cell_t propname; 396 cell_t buf; 397 cell_t len; 398 cell_t size; 399 } args = { 400 (cell_t)"setprop", 401 4, 402 1, 403 }; 404 405 args.package = package; 406 args.propname = (cell_t)propname; 407 args.buf = (cell_t)buf; 408 args.len = len; 409 if (openfirmware(&args) == -1) 410 return (-1); 411 return (args.size); 412 } 413 414 /* Convert a device specifier to a fully qualified pathname. */ 415 static ssize_t 416 ofw_std_canon(ofw_t ofw, const char *device, char *buf, size_t len) 417 { 418 static struct { 419 cell_t name; 420 cell_t nargs; 421 cell_t nreturns; 422 cell_t device; 423 cell_t buf; 424 cell_t len; 425 cell_t size; 426 } args = { 427 (cell_t)"canon", 428 3, 429 1, 430 }; 431 432 args.device = (cell_t)device; 433 args.buf = (cell_t)buf; 434 args.len = len; 435 if (openfirmware(&args) == -1) 436 return (-1); 437 return (args.size); 438 } 439 440 /* Return a package handle for the specified device. */ 441 static phandle_t 442 ofw_std_finddevice(ofw_t ofw, const char *device) 443 { 444 static struct { 445 cell_t name; 446 cell_t nargs; 447 cell_t nreturns; 448 cell_t device; 449 cell_t package; 450 } args = { 451 (cell_t)"finddevice", 452 1, 453 1, 454 }; 455 456 args.device = (cell_t)device; 457 if (openfirmware(&args) == -1) 458 return (-1); 459 return (args.package); 460 } 461 462 /* Return the fully qualified pathname corresponding to an instance. */ 463 static ssize_t 464 ofw_std_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len) 465 { 466 static struct { 467 cell_t name; 468 cell_t nargs; 469 cell_t nreturns; 470 cell_t instance; 471 cell_t buf; 472 cell_t len; 473 cell_t size; 474 } args = { 475 (cell_t)"instance-to-path", 476 3, 477 1, 478 }; 479 480 args.instance = instance; 481 args.buf = (cell_t)buf; 482 args.len = len; 483 if (openfirmware(&args) == -1) 484 return (-1); 485 return (args.size); 486 } 487 488 /* Return the fully qualified pathname corresponding to a package. */ 489 static ssize_t 490 ofw_std_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len) 491 { 492 static struct { 493 cell_t name; 494 cell_t nargs; 495 cell_t nreturns; 496 cell_t package; 497 cell_t buf; 498 cell_t len; 499 cell_t size; 500 } args = { 501 (cell_t)"package-to-path", 502 3, 503 1, 504 }; 505 506 args.package = package; 507 args.buf = (cell_t)buf; 508 args.len = len; 509 if (openfirmware(&args) == -1) 510 return (-1); 511 return (args.size); 512 } 513 514 /* Call the method in the scope of a given instance. */ 515 static int 516 ofw_std_call_method(ofw_t ofw, ihandle_t instance, const char *method, 517 int nargs, int nreturns, unsigned long *args_and_returns) 518 { 519 static struct { 520 cell_t name; 521 cell_t nargs; 522 cell_t nreturns; 523 cell_t method; 524 cell_t instance; 525 cell_t args_n_results[12]; 526 } args = { 527 (cell_t)"call-method", 528 2, 529 1, 530 }; 531 cell_t *cp; 532 unsigned long *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 static 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 static 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 }; 593 594 args.instance = instance; 595 openfirmware(&args); 596 } 597 598 /* Read from an instance. */ 599 static ssize_t 600 ofw_std_read(ofw_t ofw, ihandle_t instance, void *addr, size_t len) 601 { 602 static struct { 603 cell_t name; 604 cell_t nargs; 605 cell_t nreturns; 606 cell_t instance; 607 cell_t addr; 608 cell_t len; 609 cell_t actual; 610 } args = { 611 (cell_t)"read", 612 3, 613 1, 614 }; 615 616 args.instance = instance; 617 args.addr = (cell_t)addr; 618 args.len = len; 619 if (openfirmware(&args) == -1) 620 return (-1); 621 622 return (args.actual); 623 } 624 625 /* Write to an instance. */ 626 static ssize_t 627 ofw_std_write(ofw_t ofw, ihandle_t instance, const void *addr, size_t len) 628 { 629 static struct { 630 cell_t name; 631 cell_t nargs; 632 cell_t nreturns; 633 cell_t instance; 634 cell_t addr; 635 cell_t len; 636 cell_t actual; 637 } args = { 638 (cell_t)"write", 639 3, 640 1, 641 }; 642 643 args.instance = instance; 644 args.addr = (cell_t)addr; 645 args.len = len; 646 if (openfirmware(&args) == -1) 647 return (-1); 648 return (args.actual); 649 } 650 651 /* Seek to a position. */ 652 static int 653 ofw_std_seek(ofw_t ofw, ihandle_t instance, uint64_t pos) 654 { 655 static struct { 656 cell_t name; 657 cell_t nargs; 658 cell_t nreturns; 659 cell_t instance; 660 cell_t poshi; 661 cell_t poslo; 662 cell_t status; 663 } args = { 664 (cell_t)"seek", 665 3, 666 1, 667 }; 668 669 args.instance = instance; 670 args.poshi = pos >> 32; 671 args.poslo = pos; 672 if (openfirmware(&args) == -1) 673 return (-1); 674 return (args.status); 675 } 676 677 /* 678 * Memory functions 679 */ 680 681 /* Claim an area of memory. */ 682 static caddr_t 683 ofw_std_claim(ofw_t ofw, void *virt, size_t size, u_int align) 684 { 685 static struct { 686 cell_t name; 687 cell_t nargs; 688 cell_t nreturns; 689 cell_t virt; 690 cell_t size; 691 cell_t align; 692 cell_t baseaddr; 693 } args = { 694 (cell_t)"claim", 695 3, 696 1, 697 }; 698 699 args.virt = (cell_t)virt; 700 args.size = size; 701 args.align = align; 702 if (openfirmware(&args) == -1) 703 return ((void *)-1); 704 return ((void *)args.baseaddr); 705 } 706 707 /* Release an area of memory. */ 708 static void 709 ofw_std_release(ofw_t ofw, void *virt, size_t size) 710 { 711 static struct { 712 cell_t name; 713 cell_t nargs; 714 cell_t nreturns; 715 cell_t virt; 716 cell_t size; 717 } args = { 718 (cell_t)"release", 719 2, 720 }; 721 722 args.virt = (cell_t)virt; 723 args.size = size; 724 openfirmware(&args); 725 } 726 727 /* 728 * Control transfer functions 729 */ 730 731 /* Suspend and drop back to the Open Firmware interface. */ 732 static void 733 ofw_std_enter(ofw_t ofw) 734 { 735 static struct { 736 cell_t name; 737 cell_t nargs; 738 cell_t nreturns; 739 } args = { 740 (cell_t)"enter", 741 }; 742 743 openfirmware(&args); 744 /* We may come back. */ 745 } 746 747 /* Shut down and drop back to the Open Firmware interface. */ 748 static void 749 ofw_std_exit(ofw_t ofw) 750 { 751 static struct { 752 cell_t name; 753 cell_t nargs; 754 cell_t nreturns; 755 } args = { 756 (cell_t)"exit", 757 }; 758 759 openfirmware(&args); 760 for (;;) /* just in case */ 761 ; 762 } 763