1 /* $OpenBSD: dsdt.c,v 1.257 2020/12/17 17:57:19 kettenis Exp $ */ 2 /* 3 * Copyright (c) 2005 Jordan Hargrave <jordan@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/kernel.h> 21 #include <sys/device.h> 22 #include <sys/malloc.h> 23 #include <sys/time.h> 24 25 #include <machine/bus.h> 26 27 #ifdef DDB 28 #include <machine/db_machdep.h> 29 #endif 30 31 #include <dev/acpi/acpireg.h> 32 #include <dev/acpi/acpivar.h> 33 #include <dev/acpi/amltypes.h> 34 #include <dev/acpi/dsdt.h> 35 36 #include <dev/i2c/i2cvar.h> 37 38 #ifdef SMALL_KERNEL 39 #undef ACPI_DEBUG 40 #endif 41 42 #define opsize(opcode) (((opcode) & 0xFF00) ? 2 : 1) 43 44 #define AML_FIELD_RESERVED 0x00 45 #define AML_FIELD_ATTRIB 0x01 46 47 #define AML_REVISION 0x01 48 #define AML_INTSTRLEN 16 49 #define AML_NAMESEG_LEN 4 50 51 struct aml_value *aml_loadtable(struct acpi_softc *, const char *, 52 const char *, const char *, const char *, 53 const char *, struct aml_value *); 54 struct aml_scope *aml_load(struct acpi_softc *, struct aml_scope *, 55 struct aml_value *, struct aml_value *); 56 57 void aml_copyvalue(struct aml_value *, struct aml_value *); 58 59 void aml_setvalue(struct aml_scope *, struct aml_value *, 60 struct aml_value *, int64_t); 61 void aml_freevalue(struct aml_value *); 62 struct aml_value *aml_allocvalue(int, int64_t, const void *); 63 struct aml_value *_aml_setvalue(struct aml_value *, int, int64_t, 64 const void *); 65 66 uint64_t aml_convradix(uint64_t, int, int); 67 uint64_t aml_evalexpr(uint64_t, uint64_t, int); 68 int aml_lsb(uint64_t); 69 int aml_msb(uint64_t); 70 71 int aml_tstbit(const uint8_t *, int); 72 void aml_setbit(uint8_t *, int, int); 73 74 void aml_addref(struct aml_value *, const char *); 75 void aml_delref(struct aml_value **, const char *); 76 77 void aml_bufcpy(void *, int, const void *, int, int); 78 79 int aml_pc(uint8_t *); 80 81 struct aml_value *aml_parseop(struct aml_scope *, struct aml_value *,int); 82 struct aml_value *aml_parsetarget(struct aml_scope *, struct aml_value *, 83 struct aml_value **); 84 struct aml_value *aml_parseterm(struct aml_scope *, struct aml_value *); 85 86 struct aml_value *aml_evaltarget(struct aml_scope *scope, 87 struct aml_value *res); 88 int aml_evalterm(struct aml_scope *scope, 89 struct aml_value *raw, struct aml_value *dst); 90 91 struct aml_opcode *aml_findopcode(int); 92 93 #define acpi_os_malloc(sz) _acpi_os_malloc(sz, __FUNCTION__, __LINE__) 94 #define acpi_os_free(ptr) _acpi_os_free(ptr, __FUNCTION__, __LINE__) 95 96 void *_acpi_os_malloc(size_t, const char *, int); 97 void _acpi_os_free(void *, const char *, int); 98 void acpi_stall(int); 99 100 struct aml_value *aml_callosi(struct aml_scope *, struct aml_value *); 101 102 const char *aml_getname(const char *); 103 int64_t aml_hextoint(const char *); 104 void aml_dump(int, uint8_t *); 105 __dead void _aml_die(const char *fn, int line, const char *fmt, ...); 106 #define aml_die(x...) _aml_die(__FUNCTION__, __LINE__, x) 107 108 void aml_notify_task(void *, int); 109 void acpi_poll_notify_task(void *, int); 110 111 /* 112 * @@@: Global variables 113 */ 114 int aml_intlen = 64; 115 struct aml_node aml_root; 116 struct aml_value *aml_global_lock; 117 118 /* Perfect Hash key */ 119 #define HASH_OFF 6904 120 #define HASH_SIZE 179 121 #define HASH_KEY(k) (((k) ^ HASH_OFF) % HASH_SIZE) 122 123 /* 124 * XXX this array should be sorted, and then aml_findopcode() should 125 * do a binary search 126 */ 127 struct aml_opcode **aml_ophash; 128 struct aml_opcode aml_table[] = { 129 /* Simple types */ 130 { AMLOP_ZERO, "Zero", "c", }, 131 { AMLOP_ONE, "One", "c", }, 132 { AMLOP_ONES, "Ones", "c", }, 133 { AMLOP_REVISION, "Revision", "R", }, 134 { AMLOP_BYTEPREFIX, ".Byte", "b", }, 135 { AMLOP_WORDPREFIX, ".Word", "w", }, 136 { AMLOP_DWORDPREFIX, ".DWord", "d", }, 137 { AMLOP_QWORDPREFIX, ".QWord", "q", }, 138 { AMLOP_STRINGPREFIX, ".String", "a", }, 139 { AMLOP_DEBUG, "DebugOp", "D", }, 140 { AMLOP_BUFFER, "Buffer", "piB", }, 141 { AMLOP_PACKAGE, "Package", "pbT", }, 142 { AMLOP_VARPACKAGE, "VarPackage", "piT", }, 143 144 /* Simple objects */ 145 { AMLOP_LOCAL0, "Local0", "L", }, 146 { AMLOP_LOCAL1, "Local1", "L", }, 147 { AMLOP_LOCAL2, "Local2", "L", }, 148 { AMLOP_LOCAL3, "Local3", "L", }, 149 { AMLOP_LOCAL4, "Local4", "L", }, 150 { AMLOP_LOCAL5, "Local5", "L", }, 151 { AMLOP_LOCAL6, "Local6", "L", }, 152 { AMLOP_LOCAL7, "Local7", "L", }, 153 { AMLOP_ARG0, "Arg0", "A", }, 154 { AMLOP_ARG1, "Arg1", "A", }, 155 { AMLOP_ARG2, "Arg2", "A", }, 156 { AMLOP_ARG3, "Arg3", "A", }, 157 { AMLOP_ARG4, "Arg4", "A", }, 158 { AMLOP_ARG5, "Arg5", "A", }, 159 { AMLOP_ARG6, "Arg6", "A", }, 160 161 /* Control flow */ 162 { AMLOP_IF, "If", "piI", }, 163 { AMLOP_ELSE, "Else", "pT" }, 164 { AMLOP_WHILE, "While", "piT", }, 165 { AMLOP_BREAK, "Break", "" }, 166 { AMLOP_CONTINUE, "Continue", "" }, 167 { AMLOP_RETURN, "Return", "t", }, 168 { AMLOP_FATAL, "Fatal", "bdi", }, 169 { AMLOP_NOP, "Nop", "", }, 170 { AMLOP_BREAKPOINT, "BreakPoint", "", }, 171 172 /* Arithmetic operations */ 173 { AMLOP_INCREMENT, "Increment", "S", }, 174 { AMLOP_DECREMENT, "Decrement", "S", }, 175 { AMLOP_ADD, "Add", "iir", }, 176 { AMLOP_SUBTRACT, "Subtract", "iir", }, 177 { AMLOP_MULTIPLY, "Multiply", "iir", }, 178 { AMLOP_DIVIDE, "Divide", "iirr", }, 179 { AMLOP_SHL, "ShiftLeft", "iir", }, 180 { AMLOP_SHR, "ShiftRight", "iir", }, 181 { AMLOP_AND, "And", "iir", }, 182 { AMLOP_NAND, "Nand", "iir", }, 183 { AMLOP_OR, "Or", "iir", }, 184 { AMLOP_NOR, "Nor", "iir", }, 185 { AMLOP_XOR, "Xor", "iir", }, 186 { AMLOP_NOT, "Not", "ir", }, 187 { AMLOP_MOD, "Mod", "iir", }, 188 { AMLOP_FINDSETLEFTBIT, "FindSetLeftBit", "ir", }, 189 { AMLOP_FINDSETRIGHTBIT,"FindSetRightBit", "ir",}, 190 191 /* Logical test operations */ 192 { AMLOP_LAND, "LAnd", "ii", }, 193 { AMLOP_LOR, "LOr", "ii", }, 194 { AMLOP_LNOT, "LNot", "i", }, 195 { AMLOP_LNOTEQUAL, "LNotEqual", "tt", }, 196 { AMLOP_LLESSEQUAL, "LLessEqual", "tt", }, 197 { AMLOP_LGREATEREQUAL, "LGreaterEqual", "tt", }, 198 { AMLOP_LEQUAL, "LEqual", "tt", }, 199 { AMLOP_LGREATER, "LGreater", "tt", }, 200 { AMLOP_LLESS, "LLess", "tt", }, 201 202 /* Named objects */ 203 { AMLOP_NAMECHAR, ".NameRef", "n", }, 204 { AMLOP_ALIAS, "Alias", "nN", }, 205 { AMLOP_NAME, "Name", "Nt", }, 206 { AMLOP_EVENT, "Event", "N", }, 207 { AMLOP_MUTEX, "Mutex", "Nb", }, 208 { AMLOP_DATAREGION, "DataRegion", "Nttt", }, 209 { AMLOP_OPREGION, "OpRegion", "Nbii", }, 210 { AMLOP_SCOPE, "Scope", "pnT", }, 211 { AMLOP_DEVICE, "Device", "pNT", }, 212 { AMLOP_POWERRSRC, "Power Resource", "pNbwT",}, 213 { AMLOP_THERMALZONE, "ThermalZone", "pNT", }, 214 { AMLOP_PROCESSOR, "Processor", "pNbdbT", }, 215 { AMLOP_METHOD, "Method", "pNbM", }, 216 217 /* Field operations */ 218 { AMLOP_FIELD, "Field", "pnbF", }, 219 { AMLOP_INDEXFIELD, "IndexField", "pnnbF",}, 220 { AMLOP_BANKFIELD, "BankField", "pnnibF",}, 221 { AMLOP_CREATEFIELD, "CreateField", "tiiN", }, 222 { AMLOP_CREATEQWORDFIELD, "CreateQWordField","tiN",}, 223 { AMLOP_CREATEDWORDFIELD, "CreateDWordField","tiN",}, 224 { AMLOP_CREATEWORDFIELD, "CreateWordField", "tiN",}, 225 { AMLOP_CREATEBYTEFIELD, "CreateByteField", "tiN",}, 226 { AMLOP_CREATEBITFIELD, "CreateBitField", "tiN", }, 227 228 /* Conversion operations */ 229 { AMLOP_TOINTEGER, "ToInteger", "tr", }, 230 { AMLOP_TOBUFFER, "ToBuffer", "tr", }, 231 { AMLOP_TODECSTRING, "ToDecString", "tr", }, 232 { AMLOP_TOHEXSTRING, "ToHexString", "tr", }, 233 { AMLOP_TOSTRING, "ToString", "tir", }, 234 { AMLOP_MID, "Mid", "tiir", }, 235 { AMLOP_FROMBCD, "FromBCD", "ir", }, 236 { AMLOP_TOBCD, "ToBCD", "ir", }, 237 238 /* Mutex/Signal operations */ 239 { AMLOP_ACQUIRE, "Acquire", "Sw", }, 240 { AMLOP_RELEASE, "Release", "S", }, 241 { AMLOP_SIGNAL, "Signal", "S", }, 242 { AMLOP_WAIT, "Wait", "Si", }, 243 { AMLOP_RESET, "Reset", "S", }, 244 245 { AMLOP_INDEX, "Index", "tir", }, 246 { AMLOP_DEREFOF, "DerefOf", "t", }, 247 { AMLOP_REFOF, "RefOf", "S", }, 248 { AMLOP_CONDREFOF, "CondRef", "Sr", }, 249 250 { AMLOP_LOADTABLE, "LoadTable", "tttttt" }, 251 { AMLOP_STALL, "Stall", "i", }, 252 { AMLOP_SLEEP, "Sleep", "i", }, 253 { AMLOP_TIMER, "Timer", "", }, 254 { AMLOP_LOAD, "Load", "nS", }, 255 { AMLOP_UNLOAD, "Unload", "t" }, 256 { AMLOP_STORE, "Store", "tS", }, 257 { AMLOP_CONCAT, "Concat", "ttr", }, 258 { AMLOP_CONCATRES, "ConcatRes", "ttt" }, 259 { AMLOP_NOTIFY, "Notify", "Si", }, 260 { AMLOP_SIZEOF, "Sizeof", "S", }, 261 { AMLOP_MATCH, "Match", "tbibii", }, 262 { AMLOP_OBJECTTYPE, "ObjectType", "S", }, 263 { AMLOP_COPYOBJECT, "CopyObject", "tS", }, 264 }; 265 266 int aml_pc(uint8_t *src) 267 { 268 return src - aml_root.start; 269 } 270 271 struct aml_scope *aml_lastscope; 272 273 void 274 _aml_die(const char *fn, int line, const char *fmt, ...) 275 { 276 #ifndef SMALL_KERNEL 277 struct aml_scope *root; 278 struct aml_value *sp; 279 int idx; 280 #endif /* SMALL_KERNEL */ 281 va_list ap; 282 283 va_start(ap, fmt); 284 vprintf(fmt, ap); 285 printf("\n"); 286 va_end(ap); 287 288 #ifndef SMALL_KERNEL 289 for (root = aml_lastscope; root && root->pos; root = root->parent) { 290 printf("%.4x Called: %s\n", aml_pc(root->pos), 291 aml_nodename(root->node)); 292 for (idx = 0; idx < AML_MAX_ARG; idx++) { 293 sp = aml_getstack(root, AMLOP_ARG0+idx); 294 if (sp && sp->type) { 295 printf(" arg%d: ", idx); 296 aml_showvalue(sp); 297 } 298 } 299 for (idx = 0; idx < AML_MAX_LOCAL; idx++) { 300 sp = aml_getstack(root, AMLOP_LOCAL0+idx); 301 if (sp && sp->type) { 302 printf(" local%d: ", idx); 303 aml_showvalue(sp); 304 } 305 } 306 } 307 #endif /* SMALL_KERNEL */ 308 309 /* XXX: don't panic */ 310 panic("aml_die %s:%d", fn, line); 311 } 312 313 void 314 aml_hashopcodes(void) 315 { 316 int i; 317 318 /* Dynamically allocate hash table */ 319 aml_ophash = (struct aml_opcode **)acpi_os_malloc(HASH_SIZE * 320 sizeof(struct aml_opcode *)); 321 for (i = 0; i < sizeof(aml_table) / sizeof(aml_table[0]); i++) 322 aml_ophash[HASH_KEY(aml_table[i].opcode)] = &aml_table[i]; 323 } 324 325 struct aml_opcode * 326 aml_findopcode(int opcode) 327 { 328 struct aml_opcode *hop; 329 330 hop = aml_ophash[HASH_KEY(opcode)]; 331 if (hop && hop->opcode == opcode) 332 return hop; 333 return NULL; 334 } 335 336 #if defined(DDB) || !defined(SMALL_KERNEL) 337 const char * 338 aml_mnem(int opcode, uint8_t *pos) 339 { 340 struct aml_opcode *tab; 341 static char mnemstr[32]; 342 343 if ((tab = aml_findopcode(opcode)) != NULL) { 344 strlcpy(mnemstr, tab->mnem, sizeof(mnemstr)); 345 if (pos != NULL) { 346 switch (opcode) { 347 case AMLOP_STRINGPREFIX: 348 snprintf(mnemstr, sizeof(mnemstr), "\"%s\"", pos); 349 break; 350 case AMLOP_BYTEPREFIX: 351 snprintf(mnemstr, sizeof(mnemstr), "0x%.2x", 352 *(uint8_t *)pos); 353 break; 354 case AMLOP_WORDPREFIX: 355 snprintf(mnemstr, sizeof(mnemstr), "0x%.4x", 356 *(uint16_t *)pos); 357 break; 358 case AMLOP_DWORDPREFIX: 359 snprintf(mnemstr, sizeof(mnemstr), "0x%.4x", 360 *(uint16_t *)pos); 361 break; 362 case AMLOP_NAMECHAR: 363 strlcpy(mnemstr, aml_getname(pos), sizeof(mnemstr)); 364 break; 365 } 366 } 367 return mnemstr; 368 } 369 return ("xxx"); 370 } 371 #endif /* defined(DDB) || !defined(SMALL_KERNEL) */ 372 373 struct aml_notify_data { 374 struct aml_node *node; 375 char pnpid[20]; 376 void *cbarg; 377 int (*cbproc)(struct aml_node *, int, void *); 378 int poll; 379 380 SLIST_ENTRY(aml_notify_data) link; 381 }; 382 383 SLIST_HEAD(aml_notify_head, aml_notify_data); 384 struct aml_notify_head aml_notify_list = 385 SLIST_HEAD_INITIALIZER(aml_notify_list); 386 387 /* 388 * @@@: Memory management functions 389 */ 390 391 long acpi_nalloc; 392 393 struct acpi_memblock { 394 size_t size; 395 #ifdef ACPI_MEMDEBUG 396 const char *fn; 397 int line; 398 int sig; 399 LIST_ENTRY(acpi_memblock) link; 400 #endif 401 }; 402 403 #ifdef ACPI_MEMDEBUG 404 LIST_HEAD(, acpi_memblock) acpi_memhead; 405 int acpi_memsig; 406 407 int 408 acpi_walkmem(int sig, const char *lbl) 409 { 410 struct acpi_memblock *sptr; 411 412 printf("--- walkmem:%s %x --- %lx bytes alloced\n", lbl, sig, 413 acpi_nalloc); 414 LIST_FOREACH(sptr, &acpi_memhead, link) { 415 if (sptr->sig < sig) 416 break; 417 printf("%.4x Alloc %.8lx bytes @ %s:%d\n", 418 sptr->sig, sptr->size, sptr->fn, sptr->line); 419 } 420 return acpi_memsig; 421 } 422 #endif /* ACPI_MEMDEBUG */ 423 424 void * 425 _acpi_os_malloc(size_t size, const char *fn, int line) 426 { 427 struct acpi_memblock *sptr; 428 429 sptr = malloc(size+sizeof(*sptr), M_ACPI, M_WAITOK | M_ZERO); 430 dnprintf(99, "alloc: %p %s:%d\n", sptr, fn, line); 431 acpi_nalloc += size; 432 sptr->size = size; 433 #ifdef ACPI_MEMDEBUG 434 sptr->line = line; 435 sptr->fn = fn; 436 sptr->sig = ++acpi_memsig; 437 438 LIST_INSERT_HEAD(&acpi_memhead, sptr, link); 439 #endif 440 441 return &sptr[1]; 442 } 443 444 void 445 _acpi_os_free(void *ptr, const char *fn, int line) 446 { 447 struct acpi_memblock *sptr; 448 449 if (ptr != NULL) { 450 sptr = &(((struct acpi_memblock *)ptr)[-1]); 451 acpi_nalloc -= sptr->size; 452 453 #ifdef ACPI_MEMDEBUG 454 LIST_REMOVE(sptr, link); 455 #endif 456 457 dnprintf(99, "free: %p %s:%d\n", sptr, fn, line); 458 free(sptr, M_ACPI, sizeof(*sptr) + sptr->size); 459 } 460 } 461 462 void 463 acpi_sleep(int ms, char *reason) 464 { 465 static int acpinowait; 466 467 /* XXX ACPI integers are supposed to be unsigned. */ 468 ms = MAX(1, ms); 469 470 if (cold) 471 delay(ms * 1000); 472 else 473 tsleep_nsec(&acpinowait, PWAIT, reason, MSEC_TO_NSEC(ms)); 474 } 475 476 void 477 acpi_stall(int us) 478 { 479 delay(us); 480 } 481 482 /* 483 * @@@: Misc utility functions 484 */ 485 486 #ifdef ACPI_DEBUG 487 void 488 aml_dump(int len, uint8_t *buf) 489 { 490 int idx; 491 492 dnprintf(50, "{ "); 493 for (idx = 0; idx < len; idx++) { 494 dnprintf(50, "%s0x%.2x", idx ? ", " : "", buf[idx]); 495 } 496 dnprintf(50, " }\n"); 497 } 498 #endif 499 500 /* Bit mangling code */ 501 int 502 aml_tstbit(const uint8_t *pb, int bit) 503 { 504 pb += aml_bytepos(bit); 505 506 return (*pb & aml_bitmask(bit)); 507 } 508 509 void 510 aml_setbit(uint8_t *pb, int bit, int val) 511 { 512 pb += aml_bytepos(bit); 513 514 if (val) 515 *pb |= aml_bitmask(bit); 516 else 517 *pb &= ~aml_bitmask(bit); 518 } 519 520 /* 521 * @@@: Notify functions 522 */ 523 void 524 acpi_poll(void *arg) 525 { 526 int s; 527 528 s = splbio(); 529 acpi_addtask(acpi_softc, acpi_poll_notify_task, NULL, 0); 530 acpi_softc->sc_threadwaiting = 0; 531 wakeup(acpi_softc); 532 splx(s); 533 534 timeout_add_sec(&acpi_softc->sc_dev_timeout, 10); 535 } 536 537 void 538 aml_notify_task(void *node, int notify_value) 539 { 540 struct aml_notify_data *pdata = NULL; 541 542 dnprintf(10,"run notify: %s %x\n", aml_nodename(node), notify_value); 543 SLIST_FOREACH(pdata, &aml_notify_list, link) 544 if (pdata->node == node) 545 pdata->cbproc(pdata->node, notify_value, pdata->cbarg); 546 } 547 548 void 549 aml_register_notify(struct aml_node *node, const char *pnpid, 550 int (*proc)(struct aml_node *, int, void *), void *arg, int poll) 551 { 552 struct aml_notify_data *pdata; 553 extern int acpi_poll_enabled; 554 555 dnprintf(10, "aml_register_notify: %s %s %p\n", 556 node->name, pnpid ? pnpid : "", proc); 557 558 pdata = acpi_os_malloc(sizeof(struct aml_notify_data)); 559 pdata->node = node; 560 pdata->cbarg = arg; 561 pdata->cbproc = proc; 562 pdata->poll = poll; 563 564 if (pnpid) 565 strlcpy(pdata->pnpid, pnpid, sizeof(pdata->pnpid)); 566 567 SLIST_INSERT_HEAD(&aml_notify_list, pdata, link); 568 569 if (poll && !acpi_poll_enabled) 570 timeout_add_sec(&acpi_softc->sc_dev_timeout, 10); 571 } 572 573 void 574 aml_notify(struct aml_node *node, int notify_value) 575 { 576 if (node == NULL) 577 return; 578 579 dnprintf(10,"queue notify: %s %x\n", aml_nodename(node), notify_value); 580 acpi_addtask(acpi_softc, aml_notify_task, node, notify_value); 581 } 582 583 void 584 aml_notify_dev(const char *pnpid, int notify_value) 585 { 586 struct aml_notify_data *pdata = NULL; 587 588 if (pnpid == NULL) 589 return; 590 591 SLIST_FOREACH(pdata, &aml_notify_list, link) 592 if (strcmp(pdata->pnpid, pnpid) == 0) 593 pdata->cbproc(pdata->node, notify_value, pdata->cbarg); 594 } 595 596 void 597 acpi_poll_notify_task(void *arg0, int arg1) 598 { 599 struct aml_notify_data *pdata = NULL; 600 601 SLIST_FOREACH(pdata, &aml_notify_list, link) 602 if (pdata->cbproc && pdata->poll) 603 pdata->cbproc(pdata->node, 0, pdata->cbarg); 604 } 605 606 /* 607 * @@@: Namespace functions 608 */ 609 610 struct aml_node *__aml_search(struct aml_node *, uint8_t *, int); 611 struct aml_node *__aml_searchname(struct aml_node *, const void *, int); 612 void aml_delchildren(struct aml_node *); 613 614 615 /* Search for a name in children nodes */ 616 struct aml_node * 617 __aml_search(struct aml_node *root, uint8_t *nameseg, int create) 618 { 619 struct aml_node *node; 620 621 /* XXX: Replace with SLIST/SIMPLEQ routines */ 622 if (root == NULL) 623 return NULL; 624 SIMPLEQ_FOREACH(node, &root->son, sib) { 625 if (!strncmp(node->name, nameseg, AML_NAMESEG_LEN)) 626 return node; 627 } 628 if (create) { 629 node = acpi_os_malloc(sizeof(struct aml_node)); 630 memcpy((void *)node->name, nameseg, AML_NAMESEG_LEN); 631 node->value = aml_allocvalue(0,0,NULL); 632 node->value->node = node; 633 node->parent = root; 634 635 SIMPLEQ_INIT(&node->son); 636 SIMPLEQ_INSERT_TAIL(&root->son, node, sib); 637 } 638 return node; 639 } 640 641 /* Get absolute pathname of AML node */ 642 const char * 643 aml_nodename(struct aml_node *node) 644 { 645 static char namebuf[128]; 646 647 namebuf[0] = 0; 648 if (node) { 649 aml_nodename(node->parent); 650 if (node->parent != &aml_root) 651 strlcat(namebuf, ".", sizeof(namebuf)); 652 strlcat(namebuf, node->name, sizeof(namebuf)); 653 return namebuf+1; 654 } 655 return namebuf; 656 } 657 658 const char * 659 aml_getname(const char *name) 660 { 661 static char namebuf[128], *p; 662 int count; 663 664 p = namebuf; 665 while (*name == AMLOP_ROOTCHAR || *name == AMLOP_PARENTPREFIX) 666 *(p++) = *(name++); 667 switch (*name) { 668 case 0x00: 669 count = 0; 670 break; 671 case AMLOP_MULTINAMEPREFIX: 672 count = name[1]; 673 name += 2; 674 break; 675 case AMLOP_DUALNAMEPREFIX: 676 count = 2; 677 name += 1; 678 break; 679 default: 680 count = 1; 681 } 682 while (count--) { 683 memcpy(p, name, 4); 684 p[4] = '.'; 685 p += 5; 686 name += 4; 687 if (*name == '.') name++; 688 } 689 *(--p) = 0; 690 return namebuf; 691 } 692 693 /* Free all children nodes/values */ 694 void 695 aml_delchildren(struct aml_node *node) 696 { 697 struct aml_node *onode; 698 699 if (node == NULL) 700 return; 701 while ((onode = SIMPLEQ_FIRST(&node->son)) != NULL) { 702 SIMPLEQ_REMOVE_HEAD(&node->son, sib); 703 704 aml_delchildren(onode); 705 706 /* Don't delete values that have references */ 707 if (onode->value && onode->value->refcnt > 1) 708 onode->value->node = NULL; 709 710 /* Decrease reference count */ 711 aml_delref(&onode->value, ""); 712 713 /* Delete node */ 714 acpi_os_free(onode); 715 } 716 } 717 718 /* 719 * @@@: Value functions 720 */ 721 722 /* 723 * Field I/O code 724 */ 725 void aml_unlockfield(struct aml_scope *, struct aml_value *); 726 void aml_lockfield(struct aml_scope *, struct aml_value *); 727 728 static long global_lock_count = 0; 729 730 void 731 acpi_glk_enter(void) 732 { 733 int st = 0; 734 735 /* If lock is already ours, just continue. */ 736 if (global_lock_count++) 737 return; 738 739 /* Spin to acquire the lock. */ 740 while (!st) { 741 st = acpi_acquire_glk(&acpi_softc->sc_facs->global_lock); 742 /* XXX - yield/delay? */ 743 } 744 } 745 746 void 747 acpi_glk_leave(void) 748 { 749 int st, x; 750 751 /* If we are the last one, turn out the lights. */ 752 if (--global_lock_count) 753 return; 754 755 st = acpi_release_glk(&acpi_softc->sc_facs->global_lock); 756 if (!st) 757 return; 758 759 /* 760 * If pending, notify the BIOS that the lock was released by 761 * OSPM. No locking is needed because nobody outside the ACPI 762 * thread is supposed to touch this register. 763 */ 764 x = acpi_read_pmreg(acpi_softc, ACPIREG_PM1_CNT, 0); 765 x |= ACPI_PM1_GBL_RLS; 766 acpi_write_pmreg(acpi_softc, ACPIREG_PM1_CNT, 0, x); 767 } 768 769 void 770 aml_lockfield(struct aml_scope *scope, struct aml_value *field) 771 { 772 if (AML_FIELD_LOCK(field->v_field.flags) != AML_FIELD_LOCK_ON) 773 return; 774 775 acpi_glk_enter(); 776 } 777 778 void 779 aml_unlockfield(struct aml_scope *scope, struct aml_value *field) 780 { 781 if (AML_FIELD_LOCK(field->v_field.flags) != AML_FIELD_LOCK_ON) 782 return; 783 784 acpi_glk_leave(); 785 } 786 787 /* 788 * @@@: Value set/compare/alloc/free routines 789 */ 790 791 #ifndef SMALL_KERNEL 792 void 793 aml_showvalue(struct aml_value *val) 794 { 795 int idx; 796 797 if (val == NULL) 798 return; 799 800 if (val->node) 801 printf(" [%s]", aml_nodename(val->node)); 802 printf(" %p cnt:%.2x stk:%.2x", val, val->refcnt, val->stack); 803 switch (val->type) { 804 case AML_OBJTYPE_INTEGER: 805 printf(" integer: %llx\n", val->v_integer); 806 break; 807 case AML_OBJTYPE_STRING: 808 printf(" string: %s\n", val->v_string); 809 break; 810 case AML_OBJTYPE_METHOD: 811 printf(" method: %.2x\n", val->v_method.flags); 812 break; 813 case AML_OBJTYPE_PACKAGE: 814 printf(" package: %.2x\n", val->length); 815 for (idx = 0; idx < val->length; idx++) 816 aml_showvalue(val->v_package[idx]); 817 break; 818 case AML_OBJTYPE_BUFFER: 819 printf(" buffer: %.2x {", val->length); 820 for (idx = 0; idx < val->length; idx++) 821 printf("%s%.2x", idx ? ", " : "", val->v_buffer[idx]); 822 printf("}\n"); 823 break; 824 case AML_OBJTYPE_FIELDUNIT: 825 case AML_OBJTYPE_BUFFERFIELD: 826 printf(" field: bitpos=%.4x bitlen=%.4x ref1:%p ref2:%p [%s]\n", 827 val->v_field.bitpos, val->v_field.bitlen, 828 val->v_field.ref1, val->v_field.ref2, 829 aml_mnem(val->v_field.type, NULL)); 830 if (val->v_field.ref1) 831 printf(" ref1: %s\n", aml_nodename(val->v_field.ref1->node)); 832 if (val->v_field.ref2) 833 printf(" ref2: %s\n", aml_nodename(val->v_field.ref2->node)); 834 break; 835 case AML_OBJTYPE_MUTEX: 836 printf(" mutex: %s ref: %d\n", 837 val->v_mutex ? val->v_mutex->amt_name : "", 838 val->v_mutex ? val->v_mutex->amt_ref_count : 0); 839 break; 840 case AML_OBJTYPE_EVENT: 841 printf(" event:\n"); 842 break; 843 case AML_OBJTYPE_OPREGION: 844 printf(" opregion: %.2x,%.8llx,%x\n", 845 val->v_opregion.iospace, val->v_opregion.iobase, 846 val->v_opregion.iolen); 847 break; 848 case AML_OBJTYPE_NAMEREF: 849 printf(" nameref: %s\n", aml_getname(val->v_nameref)); 850 break; 851 case AML_OBJTYPE_DEVICE: 852 printf(" device:\n"); 853 break; 854 case AML_OBJTYPE_PROCESSOR: 855 printf(" cpu: %.2x,%.4x,%.2x\n", 856 val->v_processor.proc_id, val->v_processor.proc_addr, 857 val->v_processor.proc_len); 858 break; 859 case AML_OBJTYPE_THERMZONE: 860 printf(" thermzone:\n"); 861 break; 862 case AML_OBJTYPE_POWERRSRC: 863 printf(" pwrrsrc: %.2x,%.2x\n", 864 val->v_powerrsrc.pwr_level, val->v_powerrsrc.pwr_order); 865 break; 866 case AML_OBJTYPE_OBJREF: 867 printf(" objref: %p index:%x opcode:%s\n", val->v_objref.ref, 868 val->v_objref.index, aml_mnem(val->v_objref.type, 0)); 869 aml_showvalue(val->v_objref.ref); 870 break; 871 default: 872 printf(" !!type: %x\n", val->type); 873 } 874 } 875 #endif /* SMALL_KERNEL */ 876 877 int64_t 878 aml_val2int(struct aml_value *rval) 879 { 880 int64_t ival = 0; 881 882 if (rval == NULL) { 883 dnprintf(50, "null val2int\n"); 884 return (0); 885 } 886 switch (rval->type) { 887 case AML_OBJTYPE_INTEGER: 888 ival = rval->v_integer; 889 break; 890 case AML_OBJTYPE_BUFFER: 891 aml_bufcpy(&ival, 0, rval->v_buffer, 0, 892 min(aml_intlen, rval->length*8)); 893 break; 894 case AML_OBJTYPE_STRING: 895 ival = aml_hextoint(rval->v_string); 896 break; 897 } 898 return (ival); 899 } 900 901 /* Sets value into LHS: lhs must already be cleared */ 902 struct aml_value * 903 _aml_setvalue(struct aml_value *lhs, int type, int64_t ival, const void *bval) 904 { 905 memset(&lhs->_, 0x0, sizeof(lhs->_)); 906 907 lhs->type = type; 908 switch (lhs->type) { 909 case AML_OBJTYPE_INTEGER: 910 lhs->length = aml_intlen>>3; 911 lhs->v_integer = ival; 912 break; 913 case AML_OBJTYPE_METHOD: 914 lhs->v_method.flags = ival; 915 lhs->v_method.fneval = bval; 916 break; 917 case AML_OBJTYPE_NAMEREF: 918 lhs->v_nameref = (uint8_t *)bval; 919 break; 920 case AML_OBJTYPE_OBJREF: 921 lhs->v_objref.type = ival; 922 lhs->v_objref.ref = (struct aml_value *)bval; 923 break; 924 case AML_OBJTYPE_BUFFER: 925 lhs->length = ival; 926 lhs->v_buffer = (uint8_t *)acpi_os_malloc(ival); 927 if (bval) 928 memcpy(lhs->v_buffer, bval, ival); 929 break; 930 case AML_OBJTYPE_STRING: 931 if (ival == -1) 932 ival = strlen((const char *)bval); 933 lhs->length = ival; 934 lhs->v_string = (char *)acpi_os_malloc(ival+1); 935 if (bval) 936 strncpy(lhs->v_string, (const char *)bval, ival); 937 break; 938 case AML_OBJTYPE_PACKAGE: 939 lhs->length = ival; 940 lhs->v_package = (struct aml_value **)acpi_os_malloc(ival * 941 sizeof(struct aml_value *)); 942 for (ival = 0; ival < lhs->length; ival++) 943 lhs->v_package[ival] = aml_allocvalue( 944 AML_OBJTYPE_UNINITIALIZED, 0, NULL); 945 break; 946 } 947 return lhs; 948 } 949 950 /* Copy object to another value: lhs must already be cleared */ 951 void 952 aml_copyvalue(struct aml_value *lhs, struct aml_value *rhs) 953 { 954 int idx; 955 956 lhs->type = rhs->type; 957 switch (lhs->type) { 958 case AML_OBJTYPE_UNINITIALIZED: 959 break; 960 case AML_OBJTYPE_INTEGER: 961 lhs->length = aml_intlen>>3; 962 lhs->v_integer = rhs->v_integer; 963 break; 964 case AML_OBJTYPE_MUTEX: 965 lhs->v_mutex = rhs->v_mutex; 966 break; 967 case AML_OBJTYPE_POWERRSRC: 968 lhs->node = rhs->node; 969 lhs->v_powerrsrc = rhs->v_powerrsrc; 970 break; 971 case AML_OBJTYPE_METHOD: 972 lhs->v_method = rhs->v_method; 973 break; 974 case AML_OBJTYPE_BUFFER: 975 _aml_setvalue(lhs, rhs->type, rhs->length, rhs->v_buffer); 976 break; 977 case AML_OBJTYPE_STRING: 978 _aml_setvalue(lhs, rhs->type, rhs->length, rhs->v_string); 979 break; 980 case AML_OBJTYPE_OPREGION: 981 lhs->v_opregion = rhs->v_opregion; 982 break; 983 case AML_OBJTYPE_PROCESSOR: 984 lhs->node = rhs->node; 985 lhs->v_processor = rhs->v_processor; 986 break; 987 case AML_OBJTYPE_NAMEREF: 988 lhs->v_nameref = rhs->v_nameref; 989 break; 990 case AML_OBJTYPE_PACKAGE: 991 _aml_setvalue(lhs, rhs->type, rhs->length, NULL); 992 for (idx = 0; idx < rhs->length; idx++) 993 aml_copyvalue(lhs->v_package[idx], rhs->v_package[idx]); 994 break; 995 case AML_OBJTYPE_OBJREF: 996 lhs->v_objref = rhs->v_objref; 997 aml_addref(lhs->v_objref.ref, ""); 998 break; 999 case AML_OBJTYPE_DEVICE: 1000 case AML_OBJTYPE_THERMZONE: 1001 lhs->node = rhs->node; 1002 break; 1003 default: 1004 printf("copyvalue: %x", rhs->type); 1005 break; 1006 } 1007 } 1008 1009 /* Allocate dynamic AML value 1010 * type : Type of object to allocate (AML_OBJTYPE_XXXX) 1011 * ival : Integer value (action depends on type) 1012 * bval : Buffer value (action depends on type) 1013 */ 1014 struct aml_value * 1015 aml_allocvalue(int type, int64_t ival, const void *bval) 1016 { 1017 struct aml_value *rv; 1018 1019 rv = (struct aml_value *)acpi_os_malloc(sizeof(struct aml_value)); 1020 if (rv != NULL) { 1021 aml_addref(rv, ""); 1022 return _aml_setvalue(rv, type, ival, bval); 1023 } 1024 return NULL; 1025 } 1026 1027 void 1028 aml_freevalue(struct aml_value *val) 1029 { 1030 int idx; 1031 1032 if (val == NULL) 1033 return; 1034 switch (val->type) { 1035 case AML_OBJTYPE_STRING: 1036 acpi_os_free(val->v_string); 1037 break; 1038 case AML_OBJTYPE_BUFFER: 1039 acpi_os_free(val->v_buffer); 1040 break; 1041 case AML_OBJTYPE_PACKAGE: 1042 for (idx = 0; idx < val->length; idx++) 1043 aml_delref(&val->v_package[idx], ""); 1044 acpi_os_free(val->v_package); 1045 break; 1046 case AML_OBJTYPE_OBJREF: 1047 aml_delref(&val->v_objref.ref, ""); 1048 break; 1049 case AML_OBJTYPE_BUFFERFIELD: 1050 case AML_OBJTYPE_FIELDUNIT: 1051 aml_delref(&val->v_field.ref1, ""); 1052 aml_delref(&val->v_field.ref2, ""); 1053 break; 1054 } 1055 val->type = 0; 1056 memset(&val->_, 0, sizeof(val->_)); 1057 } 1058 1059 /* 1060 * @@@: Math eval routines 1061 */ 1062 1063 /* Convert number from one radix to another 1064 * Used in BCD conversion routines */ 1065 uint64_t 1066 aml_convradix(uint64_t val, int iradix, int oradix) 1067 { 1068 uint64_t rv = 0, pwr; 1069 1070 rv = 0; 1071 pwr = 1; 1072 while (val) { 1073 rv += (val % iradix) * pwr; 1074 val /= iradix; 1075 pwr *= oradix; 1076 } 1077 return rv; 1078 } 1079 1080 /* Calculate LSB */ 1081 int 1082 aml_lsb(uint64_t val) 1083 { 1084 int lsb; 1085 1086 if (val == 0) 1087 return (0); 1088 1089 for (lsb = 1; !(val & 0x1); lsb++) 1090 val >>= 1; 1091 1092 return (lsb); 1093 } 1094 1095 /* Calculate MSB */ 1096 int 1097 aml_msb(uint64_t val) 1098 { 1099 int msb; 1100 1101 if (val == 0) 1102 return (0); 1103 1104 for (msb = 1; val != 0x1; msb++) 1105 val >>= 1; 1106 1107 return (msb); 1108 } 1109 1110 /* Evaluate Math operands */ 1111 uint64_t 1112 aml_evalexpr(uint64_t lhs, uint64_t rhs, int opcode) 1113 { 1114 uint64_t res = 0; 1115 1116 switch (opcode) { 1117 /* Math operations */ 1118 case AMLOP_INCREMENT: 1119 case AMLOP_ADD: 1120 res = (lhs + rhs); 1121 break; 1122 case AMLOP_DECREMENT: 1123 case AMLOP_SUBTRACT: 1124 res = (lhs - rhs); 1125 break; 1126 case AMLOP_MULTIPLY: 1127 res = (lhs * rhs); 1128 break; 1129 case AMLOP_DIVIDE: 1130 res = (lhs / rhs); 1131 break; 1132 case AMLOP_MOD: 1133 res = (lhs % rhs); 1134 break; 1135 case AMLOP_SHL: 1136 res = (lhs << rhs); 1137 break; 1138 case AMLOP_SHR: 1139 res = (lhs >> rhs); 1140 break; 1141 case AMLOP_AND: 1142 res = (lhs & rhs); 1143 break; 1144 case AMLOP_NAND: 1145 res = ~(lhs & rhs); 1146 break; 1147 case AMLOP_OR: 1148 res = (lhs | rhs); 1149 break; 1150 case AMLOP_NOR: 1151 res = ~(lhs | rhs); 1152 break; 1153 case AMLOP_XOR: 1154 res = (lhs ^ rhs); 1155 break; 1156 case AMLOP_NOT: 1157 res = ~(lhs); 1158 break; 1159 1160 /* Conversion/misc */ 1161 case AMLOP_FINDSETLEFTBIT: 1162 res = aml_msb(lhs); 1163 break; 1164 case AMLOP_FINDSETRIGHTBIT: 1165 res = aml_lsb(lhs); 1166 break; 1167 case AMLOP_TOINTEGER: 1168 res = (lhs); 1169 break; 1170 case AMLOP_FROMBCD: 1171 res = aml_convradix(lhs, 16, 10); 1172 break; 1173 case AMLOP_TOBCD: 1174 res = aml_convradix(lhs, 10, 16); 1175 break; 1176 1177 /* Logical/Comparison */ 1178 case AMLOP_LAND: 1179 res = -(lhs && rhs); 1180 break; 1181 case AMLOP_LOR: 1182 res = -(lhs || rhs); 1183 break; 1184 case AMLOP_LNOT: 1185 res = -(!lhs); 1186 break; 1187 case AMLOP_LNOTEQUAL: 1188 res = -(lhs != rhs); 1189 break; 1190 case AMLOP_LLESSEQUAL: 1191 res = -(lhs <= rhs); 1192 break; 1193 case AMLOP_LGREATEREQUAL: 1194 res = -(lhs >= rhs); 1195 break; 1196 case AMLOP_LEQUAL: 1197 res = -(lhs == rhs); 1198 break; 1199 case AMLOP_LGREATER: 1200 res = -(lhs > rhs); 1201 break; 1202 case AMLOP_LLESS: 1203 res = -(lhs < rhs); 1204 break; 1205 } 1206 1207 dnprintf(15,"aml_evalexpr: %s %llx %llx = %llx\n", 1208 aml_mnem(opcode, NULL), lhs, rhs, res); 1209 1210 return res; 1211 } 1212 1213 /* 1214 * aml_bufcpy copies/shifts buffer data, special case for aligned transfers 1215 * dstPos/srcPos are bit positions within destination/source buffers 1216 */ 1217 void 1218 aml_bufcpy(void *pvDst, int dstPos, const void *pvSrc, int srcPos, int len) 1219 { 1220 const uint8_t *pSrc = pvSrc; 1221 uint8_t *pDst = pvDst; 1222 int idx; 1223 1224 if (aml_bytealigned(dstPos|srcPos|len)) { 1225 /* Aligned transfer: use memcpy */ 1226 memcpy(pDst+aml_bytepos(dstPos), pSrc+aml_bytepos(srcPos), 1227 aml_bytelen(len)); 1228 return; 1229 } 1230 1231 /* Misaligned transfer: perform bitwise copy (slow) */ 1232 for (idx = 0; idx < len; idx++) 1233 aml_setbit(pDst, idx + dstPos, aml_tstbit(pSrc, idx + srcPos)); 1234 } 1235 1236 /* 1237 * @@@: External API 1238 * 1239 * evaluate an AML node 1240 * Returns a copy of the value in res (must be freed by user) 1241 */ 1242 1243 void 1244 aml_walknodes(struct aml_node *node, int mode, 1245 int (*nodecb)(struct aml_node *, void *), void *arg) 1246 { 1247 struct aml_node *child; 1248 1249 if (node == NULL) 1250 return; 1251 if (mode == AML_WALK_PRE) 1252 if (nodecb(node, arg)) 1253 return; 1254 SIMPLEQ_FOREACH(child, &node->son, sib) 1255 aml_walknodes(child, mode, nodecb, arg); 1256 if (mode == AML_WALK_POST) 1257 nodecb(node, arg); 1258 } 1259 1260 void 1261 aml_find_node(struct aml_node *node, const char *name, 1262 int (*cbproc)(struct aml_node *, void *arg), void *arg) 1263 { 1264 struct aml_node *child; 1265 const char *nn; 1266 1267 /* match child of this node first before recursing */ 1268 SIMPLEQ_FOREACH(child, &node->son, sib) { 1269 nn = child->name; 1270 if (nn != NULL) { 1271 if (*nn == AMLOP_ROOTCHAR) nn++; 1272 while (*nn == AMLOP_PARENTPREFIX) nn++; 1273 if (strcmp(name, nn) == 0) { 1274 /* Only recurse if cbproc() wants us to */ 1275 if (cbproc(child, arg) != 0) 1276 return; 1277 } 1278 } 1279 } 1280 1281 SIMPLEQ_FOREACH(child, &node->son, sib) 1282 aml_find_node(child, name, cbproc, arg); 1283 } 1284 1285 /* 1286 * @@@: Parser functions 1287 */ 1288 uint8_t *aml_parsename(struct aml_node *, uint8_t *, struct aml_value **, int); 1289 uint8_t *aml_parseend(struct aml_scope *scope); 1290 int aml_parselength(struct aml_scope *); 1291 int aml_parseopcode(struct aml_scope *); 1292 1293 /* Get AML Opcode */ 1294 int 1295 aml_parseopcode(struct aml_scope *scope) 1296 { 1297 int opcode = (scope->pos[0]); 1298 int twocode = (scope->pos[0]<<8) + scope->pos[1]; 1299 1300 /* Check if this is an embedded name */ 1301 switch (opcode) { 1302 case AMLOP_ROOTCHAR: 1303 case AMLOP_PARENTPREFIX: 1304 case AMLOP_MULTINAMEPREFIX: 1305 case AMLOP_DUALNAMEPREFIX: 1306 case AMLOP_NAMECHAR: 1307 return AMLOP_NAMECHAR; 1308 } 1309 if (opcode >= 'A' && opcode <= 'Z') 1310 return AMLOP_NAMECHAR; 1311 if (twocode == AMLOP_LNOTEQUAL || twocode == AMLOP_LLESSEQUAL || 1312 twocode == AMLOP_LGREATEREQUAL || opcode == AMLOP_EXTPREFIX) { 1313 scope->pos += 2; 1314 return twocode; 1315 } 1316 scope->pos += 1; 1317 return opcode; 1318 } 1319 1320 /* Decode embedded AML Namestring */ 1321 uint8_t * 1322 aml_parsename(struct aml_node *inode, uint8_t *pos, struct aml_value **rval, int create) 1323 { 1324 struct aml_node *relnode, *node = inode; 1325 uint8_t *start = pos; 1326 int i; 1327 1328 if (*pos == AMLOP_ROOTCHAR) { 1329 pos++; 1330 node = &aml_root; 1331 } 1332 while (*pos == AMLOP_PARENTPREFIX) { 1333 pos++; 1334 if ((node = node->parent) == NULL) 1335 node = &aml_root; 1336 } 1337 switch (*pos) { 1338 case 0x00: 1339 pos++; 1340 break; 1341 case AMLOP_MULTINAMEPREFIX: 1342 for (i=0; i<pos[1]; i++) 1343 node = __aml_search(node, pos+2+i*AML_NAMESEG_LEN, 1344 create); 1345 pos += 2+i*AML_NAMESEG_LEN; 1346 break; 1347 case AMLOP_DUALNAMEPREFIX: 1348 node = __aml_search(node, pos+1, create); 1349 node = __aml_search(node, pos+1+AML_NAMESEG_LEN, create); 1350 pos += 1+2*AML_NAMESEG_LEN; 1351 break; 1352 default: 1353 /* If Relative Search (pos == start), recursively go up root */ 1354 relnode = node; 1355 do { 1356 node = __aml_search(relnode, pos, create); 1357 relnode = relnode->parent; 1358 } while (!node && pos == start && relnode); 1359 pos += AML_NAMESEG_LEN; 1360 break; 1361 } 1362 if (node) { 1363 *rval = node->value; 1364 1365 /* Dereference ALIAS here */ 1366 if ((*rval)->type == AML_OBJTYPE_OBJREF && 1367 (*rval)->v_objref.type == AMLOP_ALIAS) { 1368 dnprintf(10, "deref alias: %s\n", aml_nodename(node)); 1369 *rval = (*rval)->v_objref.ref; 1370 } 1371 aml_addref(*rval, 0); 1372 1373 dnprintf(10, "parsename: %s %x\n", aml_nodename(node), 1374 (*rval)->type); 1375 } else { 1376 *rval = aml_allocvalue(AML_OBJTYPE_NAMEREF, 0, start); 1377 1378 dnprintf(10, "%s:%s not found\n", aml_nodename(inode), 1379 aml_getname(start)); 1380 } 1381 1382 return pos; 1383 } 1384 1385 /* Decode AML Length field 1386 * AML Length field is encoded: 1387 * byte0 byte1 byte2 byte3 1388 * 00xxxxxx : if upper bits == 00, length = xxxxxx 1389 * 01--xxxx yyyyyyyy : if upper bits == 01, length = yyyyyyyyxxxx 1390 * 10--xxxx yyyyyyyy zzzzzzzz : if upper bits == 10, length = zzzzzzzzyyyyyyyyxxxx 1391 * 11--xxxx yyyyyyyy zzzzzzzz wwwwwwww : if upper bits == 11, length = wwwwwwwwzzzzzzzzyyyyyyyyxxxx 1392 */ 1393 int 1394 aml_parselength(struct aml_scope *scope) 1395 { 1396 int len; 1397 uint8_t lcode; 1398 1399 lcode = *(scope->pos++); 1400 if (lcode <= 0x3F) 1401 return lcode; 1402 1403 /* lcode >= 0x40, multibyte length, get first byte of extended length */ 1404 len = lcode & 0xF; 1405 len += *(scope->pos++) << 4L; 1406 if (lcode >= 0x80) 1407 len += *(scope->pos++) << 12L; 1408 if (lcode >= 0xC0) 1409 len += *(scope->pos++) << 20L; 1410 return len; 1411 } 1412 1413 /* Get address of end of scope; based on current address */ 1414 uint8_t * 1415 aml_parseend(struct aml_scope *scope) 1416 { 1417 uint8_t *pos = scope->pos; 1418 int len; 1419 1420 len = aml_parselength(scope); 1421 if (pos+len > scope->end) { 1422 dnprintf(10, 1423 "Bad scope... runover pos:%.4x new end:%.4x scope " 1424 "end:%.4x\n", aml_pc(pos), aml_pc(pos+len), 1425 aml_pc(scope->end)); 1426 pos = scope->end; 1427 } 1428 return pos+len; 1429 } 1430 1431 /* 1432 * @@@: Opcode utility functions 1433 */ 1434 1435 /* 1436 * @@@: Opcode functions 1437 */ 1438 1439 int odp; 1440 1441 const char hext[] = "0123456789ABCDEF"; 1442 1443 const char * 1444 aml_eisaid(uint32_t pid) 1445 { 1446 static char id[8]; 1447 1448 id[0] = '@' + ((pid >> 2) & 0x1F); 1449 id[1] = '@' + ((pid << 3) & 0x18) + ((pid >> 13) & 0x7); 1450 id[2] = '@' + ((pid >> 8) & 0x1F); 1451 id[3] = hext[(pid >> 20) & 0xF]; 1452 id[4] = hext[(pid >> 16) & 0xF]; 1453 id[5] = hext[(pid >> 28) & 0xF]; 1454 id[6] = hext[(pid >> 24) & 0xF]; 1455 id[7] = 0; 1456 return id; 1457 } 1458 1459 /* 1460 * @@@: Default Object creation 1461 */ 1462 static char osstring[] = "Macrosift Windogs MT"; 1463 struct aml_defval { 1464 const char *name; 1465 int type; 1466 int64_t ival; 1467 const void *bval; 1468 struct aml_value **gval; 1469 } aml_defobj[] = { 1470 { "_OS_", AML_OBJTYPE_STRING, -1, osstring }, 1471 { "_REV", AML_OBJTYPE_INTEGER, 2, NULL }, 1472 { "_GL", AML_OBJTYPE_MUTEX, 1, NULL, &aml_global_lock }, 1473 { "_OSI", AML_OBJTYPE_METHOD, 1, aml_callosi }, 1474 1475 /* Create default scopes */ 1476 { "_GPE", AML_OBJTYPE_DEVICE }, 1477 { "_PR_", AML_OBJTYPE_DEVICE }, 1478 { "_SB_", AML_OBJTYPE_DEVICE }, 1479 { "_TZ_", AML_OBJTYPE_DEVICE }, 1480 { "_SI_", AML_OBJTYPE_DEVICE }, 1481 1482 { NULL } 1483 }; 1484 1485 /* _OSI Default Method: 1486 * Returns True if string argument matches list of known OS strings 1487 * We return True for Windows to fake out nasty bad AML 1488 */ 1489 char *aml_valid_osi[] = { 1490 AML_VALID_OSI, 1491 NULL 1492 }; 1493 1494 enum acpi_osi acpi_max_osi = OSI_UNKNOWN; 1495 1496 struct aml_value * 1497 aml_callosi(struct aml_scope *scope, struct aml_value *val) 1498 { 1499 int idx, result=0; 1500 struct aml_value *fa; 1501 1502 fa = aml_getstack(scope, AMLOP_ARG0); 1503 1504 if (hw_vendor != NULL && 1505 (strcmp(hw_vendor, "Apple Inc.") == 0 || 1506 strcmp(hw_vendor, "Apple Computer, Inc.") == 0)) { 1507 if (strcmp(fa->v_string, "Darwin") == 0) { 1508 dnprintf(10,"osi: returning 1 for %s on %s hardware\n", 1509 fa->v_string, hw_vendor); 1510 result = 1; 1511 } else 1512 dnprintf(10,"osi: on %s hardware, but ignoring %s\n", 1513 hw_vendor, fa->v_string); 1514 1515 return aml_allocvalue(AML_OBJTYPE_INTEGER, result, NULL); 1516 } 1517 1518 for (idx=0; !result && aml_valid_osi[idx] != NULL; idx++) { 1519 dnprintf(10,"osi: %s,%s\n", fa->v_string, aml_valid_osi[idx]); 1520 result = !strcmp(fa->v_string, aml_valid_osi[idx]); 1521 if (result) { 1522 if (idx > acpi_max_osi) 1523 acpi_max_osi = idx; 1524 break; 1525 } 1526 } 1527 dnprintf(10,"@@ OSI found: %x\n", result); 1528 return aml_allocvalue(AML_OBJTYPE_INTEGER, result, NULL); 1529 } 1530 1531 void 1532 aml_create_defaultobjects(void) 1533 { 1534 struct aml_value *tmp; 1535 struct aml_defval *def; 1536 1537 #ifdef ACPI_MEMDEBUG 1538 LIST_INIT(&acpi_memhead); 1539 #endif 1540 1541 osstring[1] = 'i'; 1542 osstring[6] = 'o'; 1543 osstring[15] = 'w'; 1544 osstring[18] = 'N'; 1545 1546 SIMPLEQ_INIT(&aml_root.son); 1547 strlcpy(aml_root.name, "\\", sizeof(aml_root.name)); 1548 aml_root.value = aml_allocvalue(0, 0, NULL); 1549 aml_root.value->node = &aml_root; 1550 1551 for (def = aml_defobj; def->name; def++) { 1552 /* Allocate object value + add to namespace */ 1553 aml_parsename(&aml_root, (uint8_t *)def->name, &tmp, 1); 1554 _aml_setvalue(tmp, def->type, def->ival, def->bval); 1555 if (def->gval) { 1556 /* Set root object pointer */ 1557 *def->gval = tmp; 1558 } 1559 aml_delref(&tmp, 0); 1560 } 1561 } 1562 1563 #ifdef ACPI_DEBUG 1564 int 1565 aml_print_resource(union acpi_resource *crs, void *arg) 1566 { 1567 int typ = AML_CRSTYPE(crs); 1568 1569 switch (typ) { 1570 case LR_EXTIRQ: 1571 printf("extirq\tflags:%.2x len:%.2x irq:%.4x\n", 1572 crs->lr_extirq.flags, crs->lr_extirq.irq_count, 1573 letoh32(crs->lr_extirq.irq[0])); 1574 break; 1575 case SR_IRQ: 1576 printf("irq\t%.4x %.2x\n", letoh16(crs->sr_irq.irq_mask), 1577 crs->sr_irq.irq_flags); 1578 break; 1579 case SR_DMA: 1580 printf("dma\t%.2x %.2x\n", crs->sr_dma.channel, 1581 crs->sr_dma.flags); 1582 break; 1583 case SR_IOPORT: 1584 printf("ioport\tflags:%.2x _min:%.4x _max:%.4x _aln:%.2x _len:%.2x\n", 1585 crs->sr_ioport.flags, crs->sr_ioport._min, 1586 crs->sr_ioport._max, crs->sr_ioport._aln, 1587 crs->sr_ioport._len); 1588 break; 1589 case SR_STARTDEP: 1590 printf("startdep\n"); 1591 break; 1592 case SR_ENDDEP: 1593 printf("enddep\n"); 1594 break; 1595 case LR_WORD: 1596 printf("word\ttype:%.2x flags:%.2x tflag:%.2x gra:%.4x min:%.4x max:%.4x tra:%.4x len:%.4x\n", 1597 crs->lr_word.type, crs->lr_word.flags, crs->lr_word.tflags, 1598 crs->lr_word._gra, crs->lr_word._min, crs->lr_word._max, 1599 crs->lr_word._tra, crs->lr_word._len); 1600 break; 1601 case LR_DWORD: 1602 printf("dword\ttype:%.2x flags:%.2x tflag:%.2x gra:%.8x min:%.8x max:%.8x tra:%.8x len:%.8x\n", 1603 crs->lr_dword.type, crs->lr_dword.flags, crs->lr_dword.tflags, 1604 crs->lr_dword._gra, crs->lr_dword._min, crs->lr_dword._max, 1605 crs->lr_dword._tra, crs->lr_dword._len); 1606 break; 1607 case LR_QWORD: 1608 printf("dword\ttype:%.2x flags:%.2x tflag:%.2x gra:%.16llx min:%.16llx max:%.16llx tra:%.16llx len:%.16llx\n", 1609 crs->lr_qword.type, crs->lr_qword.flags, crs->lr_qword.tflags, 1610 crs->lr_qword._gra, crs->lr_qword._min, crs->lr_qword._max, 1611 crs->lr_qword._tra, crs->lr_qword._len); 1612 break; 1613 default: 1614 printf("unknown type: %x\n", typ); 1615 break; 1616 } 1617 return (0); 1618 } 1619 #endif /* ACPI_DEBUG */ 1620 1621 union acpi_resource *aml_mapresource(union acpi_resource *); 1622 1623 union acpi_resource * 1624 aml_mapresource(union acpi_resource *crs) 1625 { 1626 static union acpi_resource map; 1627 int rlen; 1628 1629 rlen = AML_CRSLEN(crs); 1630 if (rlen >= sizeof(map)) 1631 return crs; 1632 1633 memset(&map, 0, sizeof(map)); 1634 memcpy(&map, crs, rlen); 1635 1636 return ↦ 1637 } 1638 1639 int 1640 aml_parse_resource(struct aml_value *res, 1641 int (*crs_enum)(int, union acpi_resource *, void *), void *arg) 1642 { 1643 int off, rlen, crsidx; 1644 union acpi_resource *crs; 1645 1646 if (res->type != AML_OBJTYPE_BUFFER || res->length < 5) 1647 return (-1); 1648 for (off = 0, crsidx = 0; off < res->length; off += rlen, crsidx++) { 1649 crs = (union acpi_resource *)(res->v_buffer+off); 1650 1651 rlen = AML_CRSLEN(crs); 1652 if (crs->hdr.typecode == SRT_ENDTAG || !rlen) 1653 break; 1654 1655 crs = aml_mapresource(crs); 1656 #ifdef ACPI_DEBUG 1657 aml_print_resource(crs, NULL); 1658 #endif 1659 crs_enum(crsidx, crs, arg); 1660 } 1661 1662 return (0); 1663 } 1664 1665 void 1666 aml_foreachpkg(struct aml_value *pkg, int start, 1667 void (*fn)(struct aml_value *, void *), void *arg) 1668 { 1669 int idx; 1670 1671 if (pkg->type != AML_OBJTYPE_PACKAGE) 1672 return; 1673 for (idx=start; idx<pkg->length; idx++) 1674 fn(pkg->v_package[idx], arg); 1675 } 1676 1677 /* 1678 * Walk nodes and perform fixups for nameref 1679 */ 1680 int aml_fixup_node(struct aml_node *, void *); 1681 1682 int aml_fixup_node(struct aml_node *node, void *arg) 1683 { 1684 struct aml_value *val = arg; 1685 int i; 1686 1687 if (node->value == NULL) 1688 return (0); 1689 if (arg == NULL) 1690 aml_fixup_node(node, node->value); 1691 else if (val->type == AML_OBJTYPE_NAMEREF) { 1692 node = aml_searchname(node, val->v_nameref); 1693 if (node && node->value) { 1694 _aml_setvalue(val, AML_OBJTYPE_OBJREF, AMLOP_NAMECHAR, 1695 node->value); 1696 } 1697 } else if (val->type == AML_OBJTYPE_PACKAGE) { 1698 for (i = 0; i < val->length; i++) 1699 aml_fixup_node(node, val->v_package[i]); 1700 } 1701 return (0); 1702 } 1703 1704 void 1705 aml_postparse(void) 1706 { 1707 aml_walknodes(&aml_root, AML_WALK_PRE, aml_fixup_node, NULL); 1708 } 1709 1710 #ifndef SMALL_KERNEL 1711 const char * 1712 aml_val_to_string(const struct aml_value *val) 1713 { 1714 static char buffer[256]; 1715 1716 int len; 1717 1718 switch (val->type) { 1719 case AML_OBJTYPE_BUFFER: 1720 len = val->length; 1721 if (len >= sizeof(buffer)) 1722 len = sizeof(buffer) - 1; 1723 memcpy(buffer, val->v_buffer, len); 1724 buffer[len] = 0; 1725 break; 1726 case AML_OBJTYPE_STRING: 1727 strlcpy(buffer, val->v_string, sizeof(buffer)); 1728 break; 1729 case AML_OBJTYPE_INTEGER: 1730 snprintf(buffer, sizeof(buffer), "%llx", val->v_integer); 1731 break; 1732 default: 1733 snprintf(buffer, sizeof(buffer), 1734 "Failed to convert type %d to string!", val->type); 1735 }; 1736 1737 return (buffer); 1738 } 1739 #endif /* SMALL_KERNEL */ 1740 1741 int aml_error; 1742 1743 struct aml_value *aml_gettgt(struct aml_value *, int); 1744 struct aml_value *aml_eval(struct aml_scope *, struct aml_value *, int, int, 1745 struct aml_value *); 1746 struct aml_value *aml_parsesimple(struct aml_scope *, char, 1747 struct aml_value *); 1748 struct aml_value *aml_parse(struct aml_scope *, int, const char *); 1749 struct aml_value *aml_seterror(struct aml_scope *, const char *, ...); 1750 1751 struct aml_scope *aml_findscope(struct aml_scope *, int, int); 1752 struct aml_scope *aml_pushscope(struct aml_scope *, struct aml_value *, 1753 struct aml_node *, int); 1754 struct aml_scope *aml_popscope(struct aml_scope *); 1755 1756 void aml_showstack(struct aml_scope *); 1757 struct aml_value *aml_convert(struct aml_value *, int, int); 1758 1759 int aml_matchtest(int64_t, int64_t, int); 1760 int aml_match(struct aml_value *, int, int, int, int, int); 1761 1762 int aml_compare(struct aml_value *, struct aml_value *, int); 1763 struct aml_value *aml_concat(struct aml_value *, struct aml_value *); 1764 struct aml_value *aml_concatres(struct aml_value *, struct aml_value *); 1765 struct aml_value *aml_mid(struct aml_value *, int, int); 1766 int aml_ccrlen(int, union acpi_resource *, void *); 1767 1768 void aml_store(struct aml_scope *, struct aml_value *, int64_t, 1769 struct aml_value *); 1770 1771 /* 1772 * Reference Count functions 1773 */ 1774 void 1775 aml_addref(struct aml_value *val, const char *lbl) 1776 { 1777 if (val == NULL) 1778 return; 1779 dnprintf(50, "XAddRef: %p %s:[%s] %d\n", 1780 val, lbl, 1781 val->node ? aml_nodename(val->node) : "INTERNAL", 1782 val->refcnt); 1783 val->refcnt++; 1784 } 1785 1786 /* Decrease reference counter */ 1787 void 1788 aml_delref(struct aml_value **pv, const char *lbl) 1789 { 1790 struct aml_value *val; 1791 1792 if (pv == NULL || *pv == NULL) 1793 return; 1794 val = *pv; 1795 val->refcnt--; 1796 if (val->refcnt == 0) { 1797 dnprintf(50, "XDelRef: %p %s %2d [%s] %s\n", 1798 val, lbl, 1799 val->refcnt, 1800 val->node ? aml_nodename(val->node) : "INTERNAL", 1801 val->refcnt ? "" : "---------------- FREEING"); 1802 1803 aml_freevalue(val); 1804 acpi_os_free(val); 1805 *pv = NULL; 1806 } 1807 } 1808 1809 /* Walk list of parent scopes until we find one of 'type' 1810 * If endscope is set, mark all intermediate scopes as invalid (used for Method/While) */ 1811 struct aml_scope * 1812 aml_findscope(struct aml_scope *scope, int type, int endscope) 1813 { 1814 while (scope) { 1815 switch (endscope) { 1816 case AMLOP_RETURN: 1817 scope->pos = scope->end; 1818 if (scope->type == AMLOP_WHILE) 1819 scope->pos = NULL; 1820 break; 1821 case AMLOP_CONTINUE: 1822 scope->pos = scope->end; 1823 break; 1824 case AMLOP_BREAK: 1825 scope->pos = scope->end; 1826 if (scope->type == type) 1827 scope->parent->pos = scope->end; 1828 break; 1829 } 1830 if (scope->type == type) 1831 break; 1832 scope = scope->parent; 1833 } 1834 return scope; 1835 } 1836 1837 struct aml_value * 1838 aml_getstack(struct aml_scope *scope, int opcode) 1839 { 1840 struct aml_value *sp; 1841 1842 sp = NULL; 1843 scope = aml_findscope(scope, AMLOP_METHOD, 0); 1844 if (scope == NULL) 1845 return NULL; 1846 if (opcode >= AMLOP_LOCAL0 && opcode <= AMLOP_LOCAL7) { 1847 if (scope->locals == NULL) 1848 scope->locals = aml_allocvalue(AML_OBJTYPE_PACKAGE, 8, NULL); 1849 sp = scope->locals->v_package[opcode - AMLOP_LOCAL0]; 1850 sp->stack = opcode; 1851 } else if (opcode >= AMLOP_ARG0 && opcode <= AMLOP_ARG6) { 1852 if (scope->args == NULL) 1853 scope->args = aml_allocvalue(AML_OBJTYPE_PACKAGE, 7, NULL); 1854 sp = scope->args->v_package[opcode - AMLOP_ARG0]; 1855 if (sp->type == AML_OBJTYPE_OBJREF) 1856 sp = sp->v_objref.ref; 1857 } 1858 return sp; 1859 } 1860 1861 #ifdef ACPI_DEBUG 1862 /* Dump AML Stack */ 1863 void 1864 aml_showstack(struct aml_scope *scope) 1865 { 1866 struct aml_value *sp; 1867 int idx; 1868 1869 dnprintf(10, "===== Stack %s:%s\n", aml_nodename(scope->node), 1870 aml_mnem(scope->type, 0)); 1871 for (idx=0; scope->args && idx<7; idx++) { 1872 sp = aml_getstack(scope, AMLOP_ARG0+idx); 1873 if (sp && sp->type) { 1874 dnprintf(10," Arg%d: ", idx); 1875 aml_showvalue(sp); 1876 } 1877 } 1878 for (idx=0; scope->locals && idx<8; idx++) { 1879 sp = aml_getstack(scope, AMLOP_LOCAL0+idx); 1880 if (sp && sp->type) { 1881 dnprintf(10," Local%d: ", idx); 1882 aml_showvalue(sp); 1883 } 1884 } 1885 } 1886 #endif 1887 1888 /* Create a new scope object */ 1889 struct aml_scope * 1890 aml_pushscope(struct aml_scope *parent, struct aml_value *range, 1891 struct aml_node *node, int type) 1892 { 1893 struct aml_scope *scope; 1894 uint8_t *start, *end; 1895 1896 if (range->type == AML_OBJTYPE_METHOD) { 1897 start = range->v_method.start; 1898 end = range->v_method.end; 1899 } else { 1900 start = range->v_buffer; 1901 end = start + range->length; 1902 if (start == end) 1903 return NULL; 1904 } 1905 scope = acpi_os_malloc(sizeof(struct aml_scope)); 1906 if (scope == NULL) 1907 return NULL; 1908 1909 scope->node = node; 1910 scope->start = start; 1911 scope->end = end; 1912 scope->pos = scope->start; 1913 scope->parent = parent; 1914 scope->type = type; 1915 scope->sc = acpi_softc; 1916 1917 if (parent) 1918 scope->depth = parent->depth+1; 1919 1920 aml_lastscope = scope; 1921 1922 return scope; 1923 } 1924 1925 /* Free a scope object and any children */ 1926 struct aml_scope * 1927 aml_popscope(struct aml_scope *scope) 1928 { 1929 struct aml_scope *nscope; 1930 1931 if (scope == NULL) 1932 return NULL; 1933 1934 nscope = scope->parent; 1935 1936 if (scope->type == AMLOP_METHOD) 1937 aml_delchildren(scope->node); 1938 if (scope->locals) { 1939 aml_freevalue(scope->locals); 1940 acpi_os_free(scope->locals); 1941 scope->locals = NULL; 1942 } 1943 if (scope->args) { 1944 aml_freevalue(scope->args); 1945 acpi_os_free(scope->args); 1946 scope->args = NULL; 1947 } 1948 acpi_os_free(scope); 1949 aml_lastscope = nscope; 1950 1951 return nscope; 1952 } 1953 1954 /* Test AMLOP_MATCH codes */ 1955 int 1956 aml_matchtest(int64_t a, int64_t b, int op) 1957 { 1958 switch (op) { 1959 case AML_MATCH_TR: 1960 return (1); 1961 case AML_MATCH_EQ: 1962 return (a == b); 1963 case AML_MATCH_LT: 1964 return (a < b); 1965 case AML_MATCH_LE: 1966 return (a <= b); 1967 case AML_MATCH_GE: 1968 return (a >= b); 1969 case AML_MATCH_GT: 1970 return (a > b); 1971 } 1972 return (0); 1973 } 1974 1975 /* Search a package for a matching value */ 1976 int 1977 aml_match(struct aml_value *pkg, int index, 1978 int op1, int v1, 1979 int op2, int v2) 1980 { 1981 struct aml_value *tmp; 1982 int flag; 1983 1984 while (index < pkg->length) { 1985 /* Convert package value to integer */ 1986 tmp = aml_convert(pkg->v_package[index], 1987 AML_OBJTYPE_INTEGER, -1); 1988 1989 /* Perform test */ 1990 flag = aml_matchtest(tmp->v_integer, v1, op1) && 1991 aml_matchtest(tmp->v_integer, v2, op2); 1992 aml_delref(&tmp, "xmatch"); 1993 1994 if (flag) 1995 return index; 1996 index++; 1997 } 1998 return -1; 1999 } 2000 2001 /* 2002 * Conversion routines 2003 */ 2004 int64_t 2005 aml_hextoint(const char *str) 2006 { 2007 int64_t v = 0; 2008 char c; 2009 2010 while (*str) { 2011 if (*str >= '0' && *str <= '9') 2012 c = *(str++) - '0'; 2013 else if (*str >= 'a' && *str <= 'f') 2014 c = *(str++) - 'a' + 10; 2015 else if (*str >= 'A' && *str <= 'F') 2016 c = *(str++) - 'A' + 10; 2017 else 2018 break; 2019 v = (v << 4) + c; 2020 } 2021 return v; 2022 2023 } 2024 2025 struct aml_value * 2026 aml_convert(struct aml_value *a, int ctype, int clen) 2027 { 2028 struct aml_value *c = NULL; 2029 2030 /* Object is already this type */ 2031 if (clen == -1) 2032 clen = a->length; 2033 if (a->type == ctype) { 2034 aml_addref(a, "XConvert"); 2035 return a; 2036 } 2037 switch (ctype) { 2038 case AML_OBJTYPE_BUFFER: 2039 dnprintf(10,"convert to buffer\n"); 2040 switch (a->type) { 2041 case AML_OBJTYPE_INTEGER: 2042 c = aml_allocvalue(AML_OBJTYPE_BUFFER, a->length, 2043 &a->v_integer); 2044 break; 2045 case AML_OBJTYPE_STRING: 2046 c = aml_allocvalue(AML_OBJTYPE_BUFFER, a->length, 2047 a->v_string); 2048 break; 2049 } 2050 break; 2051 case AML_OBJTYPE_INTEGER: 2052 dnprintf(10,"convert to integer : %x\n", a->type); 2053 switch (a->type) { 2054 case AML_OBJTYPE_BUFFER: 2055 c = aml_allocvalue(AML_OBJTYPE_INTEGER, 0, NULL); 2056 memcpy(&c->v_integer, a->v_buffer, 2057 min(a->length, c->length)); 2058 break; 2059 case AML_OBJTYPE_STRING: 2060 c = aml_allocvalue(AML_OBJTYPE_INTEGER, 0, NULL); 2061 c->v_integer = aml_hextoint(a->v_string); 2062 break; 2063 case AML_OBJTYPE_UNINITIALIZED: 2064 c = aml_allocvalue(AML_OBJTYPE_INTEGER, 0, NULL); 2065 break; 2066 } 2067 break; 2068 case AML_OBJTYPE_STRING: 2069 case AML_OBJTYPE_HEXSTRING: 2070 case AML_OBJTYPE_DECSTRING: 2071 dnprintf(10,"convert to string\n"); 2072 switch (a->type) { 2073 case AML_OBJTYPE_INTEGER: 2074 c = aml_allocvalue(AML_OBJTYPE_STRING, 20, NULL); 2075 snprintf(c->v_string, c->length, (ctype == AML_OBJTYPE_HEXSTRING) ? 2076 "0x%llx" : "%lld", a->v_integer); 2077 break; 2078 case AML_OBJTYPE_BUFFER: 2079 c = aml_allocvalue(AML_OBJTYPE_STRING, a->length, 2080 a->v_buffer); 2081 break; 2082 case AML_OBJTYPE_STRING: 2083 aml_addref(a, "XConvert"); 2084 return a; 2085 case AML_OBJTYPE_PACKAGE: /* XXX Deal with broken Lenovo X1 BIOS. */ 2086 c = aml_allocvalue(AML_OBJTYPE_STRING, 0, NULL); 2087 break; 2088 } 2089 break; 2090 } 2091 if (c == NULL) { 2092 #ifndef SMALL_KERNEL 2093 aml_showvalue(a); 2094 #endif 2095 aml_die("Could not convert %x to %x\n", a->type, ctype); 2096 } 2097 return c; 2098 } 2099 2100 int 2101 aml_compare(struct aml_value *a1, struct aml_value *a2, int opcode) 2102 { 2103 int rc = 0; 2104 2105 /* Convert A2 to type of A1 */ 2106 a2 = aml_convert(a2, a1->type, -1); 2107 if (a1->type == AML_OBJTYPE_INTEGER) 2108 rc = aml_evalexpr(a1->v_integer, a2->v_integer, opcode); 2109 else { 2110 /* Perform String/Buffer comparison */ 2111 rc = memcmp(a1->v_buffer, a2->v_buffer, 2112 min(a1->length, a2->length)); 2113 if (rc == 0) { 2114 /* If buffers match, which one is longer */ 2115 rc = a1->length - a2->length; 2116 } 2117 /* Perform comparison against zero */ 2118 rc = aml_evalexpr(rc, 0, opcode); 2119 } 2120 /* Either deletes temp buffer, or decrease refcnt on original A2 */ 2121 aml_delref(&a2, "xcompare"); 2122 return rc; 2123 } 2124 2125 /* Concatenate two objects, returning pointer to new object */ 2126 struct aml_value * 2127 aml_concat(struct aml_value *a1, struct aml_value *a2) 2128 { 2129 struct aml_value *c = NULL; 2130 2131 /* Convert arg2 to type of arg1 */ 2132 a2 = aml_convert(a2, a1->type, -1); 2133 switch (a1->type) { 2134 case AML_OBJTYPE_INTEGER: 2135 c = aml_allocvalue(AML_OBJTYPE_BUFFER, 2136 a1->length + a2->length, NULL); 2137 memcpy(c->v_buffer, &a1->v_integer, a1->length); 2138 memcpy(c->v_buffer+a1->length, &a2->v_integer, a2->length); 2139 break; 2140 case AML_OBJTYPE_BUFFER: 2141 c = aml_allocvalue(AML_OBJTYPE_BUFFER, 2142 a1->length + a2->length, NULL); 2143 memcpy(c->v_buffer, a1->v_buffer, a1->length); 2144 memcpy(c->v_buffer+a1->length, a2->v_buffer, a2->length); 2145 break; 2146 case AML_OBJTYPE_STRING: 2147 c = aml_allocvalue(AML_OBJTYPE_STRING, 2148 a1->length + a2->length, NULL); 2149 memcpy(c->v_string, a1->v_string, a1->length); 2150 memcpy(c->v_string+a1->length, a2->v_string, a2->length); 2151 break; 2152 default: 2153 aml_die("concat type mismatch %d != %d\n", a1->type, a2->type); 2154 break; 2155 } 2156 /* Either deletes temp buffer, or decrease refcnt on original A2 */ 2157 aml_delref(&a2, "xconcat"); 2158 return c; 2159 } 2160 2161 /* Calculate length of Resource Template */ 2162 int 2163 aml_ccrlen(int crsidx, union acpi_resource *rs, void *arg) 2164 { 2165 int *plen = arg; 2166 2167 *plen += AML_CRSLEN(rs); 2168 return (0); 2169 } 2170 2171 /* Concatenate resource templates, returning pointer to new object */ 2172 struct aml_value * 2173 aml_concatres(struct aml_value *a1, struct aml_value *a2) 2174 { 2175 struct aml_value *c; 2176 int l1 = 0, l2 = 0, l3 = 2; 2177 uint8_t a3[] = { SRT_ENDTAG, 0x00 }; 2178 2179 if (a1->type != AML_OBJTYPE_BUFFER || a2->type != AML_OBJTYPE_BUFFER) 2180 aml_die("concatres: not buffers\n"); 2181 2182 /* Walk a1, a2, get length minus end tags, concatenate buffers, add end tag */ 2183 aml_parse_resource(a1, aml_ccrlen, &l1); 2184 aml_parse_resource(a2, aml_ccrlen, &l2); 2185 2186 /* Concatenate buffers, add end tag */ 2187 c = aml_allocvalue(AML_OBJTYPE_BUFFER, l1+l2+l3, NULL); 2188 memcpy(c->v_buffer, a1->v_buffer, l1); 2189 memcpy(c->v_buffer+l1, a2->v_buffer, l2); 2190 memcpy(c->v_buffer+l1+l2, a3, l3); 2191 2192 return c; 2193 } 2194 2195 /* Extract substring from string or buffer */ 2196 struct aml_value * 2197 aml_mid(struct aml_value *src, int index, int length) 2198 { 2199 if (index > src->length) 2200 index = src->length; 2201 if ((index + length) > src->length) 2202 length = src->length - index; 2203 return aml_allocvalue(src->type, length, src->v_buffer + index); 2204 } 2205 2206 /* 2207 * Field I/O utility functions 2208 */ 2209 void aml_createfield(struct aml_value *, int, struct aml_value *, int, int, 2210 struct aml_value *, int, int); 2211 void aml_parsefieldlist(struct aml_scope *, int, int, 2212 struct aml_value *, struct aml_value *, int); 2213 2214 int 2215 aml_evalhid(struct aml_node *node, struct aml_value *val) 2216 { 2217 if (aml_evalname(acpi_softc, node, "_HID", 0, NULL, val)) 2218 return (-1); 2219 2220 /* Integer _HID: convert to EISA ID */ 2221 if (val->type == AML_OBJTYPE_INTEGER) 2222 _aml_setvalue(val, AML_OBJTYPE_STRING, -1, aml_eisaid(val->v_integer)); 2223 return (0); 2224 } 2225 2226 int 2227 aml_opreg_sysmem_handler(void *cookie, int iodir, uint64_t address, int size, 2228 uint64_t *value) 2229 { 2230 return acpi_gasio(acpi_softc, iodir, GAS_SYSTEM_MEMORY, 2231 address, size, size, value); 2232 } 2233 2234 int 2235 aml_opreg_sysio_handler(void *cookie, int iodir, uint64_t address, int size, 2236 uint64_t *value) 2237 { 2238 return acpi_gasio(acpi_softc, iodir, GAS_SYSTEM_IOSPACE, 2239 address, size, size, value); 2240 } 2241 2242 int 2243 aml_opreg_pcicfg_handler(void *cookie, int iodir, uint64_t address, int size, 2244 uint64_t *value) 2245 { 2246 return acpi_gasio(acpi_softc, iodir, GAS_PCI_CFG_SPACE, 2247 address, size, size, value); 2248 } 2249 2250 int 2251 aml_opreg_ec_handler(void *cookie, int iodir, uint64_t address, int size, 2252 uint64_t *value) 2253 { 2254 return acpi_gasio(acpi_softc, iodir, GAS_EMBEDDED, 2255 address, size, size, value); 2256 } 2257 2258 struct aml_regionspace { 2259 void *cookie; 2260 int (*handler)(void *, int, uint64_t, int, uint64_t *); 2261 }; 2262 2263 struct aml_regionspace aml_regionspace[256] = { 2264 [ACPI_OPREG_SYSMEM] = { NULL, aml_opreg_sysmem_handler }, 2265 [ACPI_OPREG_SYSIO] = { NULL, aml_opreg_sysio_handler }, 2266 [ACPI_OPREG_PCICFG] = { NULL, aml_opreg_pcicfg_handler }, 2267 [ACPI_OPREG_EC] = { NULL, aml_opreg_ec_handler }, 2268 }; 2269 2270 void 2271 aml_register_regionspace(struct aml_node *node, int iospace, void *cookie, 2272 int (*handler)(void *, int, uint64_t, int, uint64_t *)) 2273 { 2274 struct aml_value arg[2]; 2275 2276 KASSERT(iospace >= 0 && iospace < 256); 2277 2278 aml_regionspace[iospace].cookie = cookie; 2279 aml_regionspace[iospace].handler = handler; 2280 2281 /* Register address space. */ 2282 memset(&arg, 0, sizeof(arg)); 2283 arg[0].type = AML_OBJTYPE_INTEGER; 2284 arg[0].v_integer = iospace; 2285 arg[1].type = AML_OBJTYPE_INTEGER; 2286 arg[1].v_integer = 1; 2287 node = aml_searchname(node, "_REG"); 2288 if (node) 2289 aml_evalnode(acpi_softc, node, 2, arg, NULL); 2290 } 2291 2292 void aml_rwgen(struct aml_value *, int, int, struct aml_value *, int, int); 2293 void aml_rwgpio(struct aml_value *, int, int, struct aml_value *, int, int); 2294 void aml_rwgsb(struct aml_value *, int, int, int, struct aml_value *, int, int); 2295 void aml_rwindexfield(struct aml_value *, struct aml_value *val, int); 2296 void aml_rwfield(struct aml_value *, int, int, struct aml_value *, int); 2297 2298 /* Get PCI address for opregion objects */ 2299 int 2300 aml_rdpciaddr(struct aml_node *pcidev, union amlpci_t *addr) 2301 { 2302 int64_t res; 2303 2304 addr->bus = 0; 2305 addr->seg = 0; 2306 if (aml_evalinteger(acpi_softc, pcidev, "_ADR", 0, NULL, &res) == 0) { 2307 addr->fun = res & 0xFFFF; 2308 addr->dev = res >> 16; 2309 } 2310 while (pcidev != NULL) { 2311 /* HID device (PCI or PCIE root): eval _SEG and _BBN */ 2312 if (__aml_search(pcidev, "_HID", 0)) { 2313 if (aml_evalinteger(acpi_softc, pcidev, "_SEG", 2314 0, NULL, &res) == 0) { 2315 addr->seg = res; 2316 } 2317 if (aml_evalinteger(acpi_softc, pcidev, "_BBN", 2318 0, NULL, &res) == 0) { 2319 addr->bus = res; 2320 break; 2321 } 2322 } 2323 pcidev = pcidev->parent; 2324 } 2325 return (0); 2326 } 2327 2328 int 2329 acpi_genio(struct acpi_softc *sc, int iodir, int iospace, uint64_t address, 2330 int access_size, int len, void *buffer) 2331 { 2332 struct aml_regionspace *region = &aml_regionspace[iospace]; 2333 uint8_t *pb; 2334 int reg; 2335 2336 dnprintf(50, "genio: %.2x 0x%.8llx %s\n", 2337 iospace, address, (iodir == ACPI_IOWRITE) ? "write" : "read"); 2338 2339 KASSERT((len % access_size) == 0); 2340 2341 pb = (uint8_t *)buffer; 2342 for (reg = 0; reg < len; reg += access_size) { 2343 uint64_t value; 2344 int err; 2345 2346 if (iodir == ACPI_IOREAD) { 2347 err = region->handler(region->cookie, iodir, 2348 address + reg, access_size, &value); 2349 if (err) 2350 return err; 2351 switch (access_size) { 2352 case 1: 2353 *(uint8_t *)(pb + reg) = value; 2354 break; 2355 case 2: 2356 *(uint16_t *)(pb + reg) = value; 2357 break; 2358 case 4: 2359 *(uint32_t *)(pb + reg) = value; 2360 break; 2361 default: 2362 printf("%s: invalid access size %d on read\n", 2363 __func__, access_size); 2364 return -1; 2365 } 2366 } else { 2367 switch (access_size) { 2368 case 1: 2369 value = *(uint8_t *)(pb + reg); 2370 break; 2371 case 2: 2372 value = *(uint16_t *)(pb + reg); 2373 break; 2374 case 4: 2375 value = *(uint32_t *)(pb + reg); 2376 break; 2377 default: 2378 printf("%s: invalid access size %d on write\n", 2379 __func__, access_size); 2380 return -1; 2381 } 2382 err = region->handler(region->cookie, iodir, 2383 address + reg, access_size, &value); 2384 if (err) 2385 return err; 2386 } 2387 } 2388 2389 return 0; 2390 } 2391 2392 /* Read/Write from opregion object */ 2393 void 2394 aml_rwgen(struct aml_value *rgn, int bpos, int blen, struct aml_value *val, 2395 int mode, int flag) 2396 { 2397 struct aml_value tmp; 2398 union amlpci_t pi; 2399 void *tbit, *vbit; 2400 int tlen, type, sz; 2401 2402 dnprintf(10," %5s %.2x %.8llx %.4x [%s]\n", 2403 mode == ACPI_IOREAD ? "read" : "write", 2404 rgn->v_opregion.iospace, 2405 rgn->v_opregion.iobase + (bpos >> 3), 2406 blen, aml_nodename(rgn->node)); 2407 memset(&tmp, 0, sizeof(tmp)); 2408 2409 /* Get field access size */ 2410 switch (AML_FIELD_ACCESS(flag)) { 2411 case AML_FIELD_WORDACC: 2412 sz = 2; 2413 break; 2414 case AML_FIELD_DWORDACC: 2415 sz = 4; 2416 break; 2417 case AML_FIELD_QWORDACC: 2418 sz = 8; 2419 break; 2420 default: 2421 sz = 1; 2422 break; 2423 } 2424 2425 pi.addr = (rgn->v_opregion.iobase + (bpos >> 3)) & ~(sz - 1); 2426 bpos += ((rgn->v_opregion.iobase & (sz - 1)) << 3); 2427 bpos &= ((sz << 3) - 1); 2428 2429 if (rgn->v_opregion.iospace == ACPI_OPREG_PCICFG) { 2430 /* Get PCI Root Address for this opregion */ 2431 aml_rdpciaddr(rgn->node->parent, &pi); 2432 } 2433 2434 tbit = &tmp.v_integer; 2435 vbit = &val->v_integer; 2436 tlen = roundup(bpos + blen, sz << 3); 2437 type = rgn->v_opregion.iospace; 2438 2439 if (aml_regionspace[type].handler == NULL) { 2440 printf("%s: unregistered RegionSpace 0x%x\n", __func__, type); 2441 return; 2442 } 2443 2444 /* Allocate temporary storage */ 2445 if (tlen > aml_intlen) { 2446 _aml_setvalue(&tmp, AML_OBJTYPE_BUFFER, tlen >> 3, 0); 2447 tbit = tmp.v_buffer; 2448 } 2449 2450 if (blen > aml_intlen) { 2451 if (mode == ACPI_IOREAD) { 2452 /* Read from a large field: create buffer */ 2453 _aml_setvalue(val, AML_OBJTYPE_BUFFER, (blen + 7) >> 3, 0); 2454 } else { 2455 /* Write to a large field.. create or convert buffer */ 2456 val = aml_convert(val, AML_OBJTYPE_BUFFER, -1); 2457 2458 if (blen > (val->length << 3)) 2459 blen = val->length << 3; 2460 } 2461 vbit = val->v_buffer; 2462 } else { 2463 if (mode == ACPI_IOREAD) { 2464 /* Read from a short field.. initialize integer */ 2465 _aml_setvalue(val, AML_OBJTYPE_INTEGER, 0, 0); 2466 } else { 2467 /* Write to a short field.. convert to integer */ 2468 val = aml_convert(val, AML_OBJTYPE_INTEGER, -1); 2469 } 2470 } 2471 2472 if (mode == ACPI_IOREAD) { 2473 /* Read bits from opregion */ 2474 acpi_genio(acpi_softc, ACPI_IOREAD, type, pi.addr, 2475 sz, tlen >> 3, tbit); 2476 aml_bufcpy(vbit, 0, tbit, bpos, blen); 2477 } else { 2478 /* Write bits to opregion */ 2479 if (AML_FIELD_UPDATE(flag) == AML_FIELD_PRESERVE && 2480 (bpos != 0 || blen != tlen)) { 2481 acpi_genio(acpi_softc, ACPI_IOREAD, type, pi.addr, 2482 sz, tlen >> 3, tbit); 2483 } else if (AML_FIELD_UPDATE(flag) == AML_FIELD_WRITEASONES) { 2484 memset(tbit, 0xff, tmp.length); 2485 } 2486 /* Copy target bits, then write to region */ 2487 aml_bufcpy(tbit, bpos, vbit, 0, blen); 2488 acpi_genio(acpi_softc, ACPI_IOWRITE, type, pi.addr, 2489 sz, tlen >> 3, tbit); 2490 2491 aml_delref(&val, "fld.write"); 2492 } 2493 aml_freevalue(&tmp); 2494 } 2495 2496 void 2497 aml_rwgpio(struct aml_value *conn, int bpos, int blen, struct aml_value *val, 2498 int mode, int flag) 2499 { 2500 union acpi_resource *crs = (union acpi_resource *)conn->v_buffer; 2501 struct aml_node *node; 2502 uint16_t pin; 2503 int v = 0; 2504 2505 if (conn->type != AML_OBJTYPE_BUFFER || conn->length < 5 || 2506 AML_CRSTYPE(crs) != LR_GPIO || AML_CRSLEN(crs) > conn->length) 2507 aml_die("Invalid GpioIo"); 2508 if (bpos != 0 || blen != 1) 2509 aml_die("Invalid GpioIo access"); 2510 2511 node = aml_searchname(conn->node, 2512 (char *)&crs->pad[crs->lr_gpio.res_off]); 2513 pin = *(uint16_t *)&crs->pad[crs->lr_gpio.pin_off]; 2514 2515 if (node == NULL || node->gpio == NULL) 2516 aml_die("Could not find GpioIo pin"); 2517 2518 if (mode == ACPI_IOWRITE) { 2519 v = aml_val2int(val); 2520 node->gpio->write_pin(node->gpio->cookie, pin, v); 2521 } else { 2522 v = node->gpio->read_pin(node->gpio->cookie, pin); 2523 _aml_setvalue(val, AML_OBJTYPE_INTEGER, v, NULL); 2524 } 2525 } 2526 2527 #ifndef SMALL_KERNEL 2528 2529 void 2530 aml_rwgsb(struct aml_value *conn, int alen, int bpos, int blen, 2531 struct aml_value *val, int mode, int flag) 2532 { 2533 union acpi_resource *crs = (union acpi_resource *)conn->v_buffer; 2534 struct aml_node *node; 2535 i2c_tag_t tag; 2536 i2c_op_t op; 2537 i2c_addr_t addr; 2538 int cmdlen, buflen; 2539 uint8_t cmd; 2540 uint8_t *buf; 2541 int err; 2542 2543 if (conn->type != AML_OBJTYPE_BUFFER || conn->length < 5 || 2544 AML_CRSTYPE(crs) != LR_SERBUS || AML_CRSLEN(crs) > conn->length || 2545 crs->lr_i2cbus.revid != 1 || crs->lr_i2cbus.type != LR_SERBUS_I2C) 2546 aml_die("Invalid GenericSerialBus"); 2547 if (AML_FIELD_ACCESS(flag) != AML_FIELD_BUFFERACC || 2548 bpos & 0x3 || blen != 8) 2549 aml_die("Invalid GenericSerialBus access"); 2550 2551 node = aml_searchname(conn->node, 2552 (char *)&crs->lr_i2cbus.vdata[crs->lr_i2cbus.tlength - 6]); 2553 2554 if (node == NULL || node->i2c == NULL) 2555 aml_die("Could not find GenericSerialBus controller"); 2556 2557 switch (((flag >> 6) & 0x3)) { 2558 case 0: /* Normal */ 2559 switch (AML_FIELD_ATTR(flag)) { 2560 case 0x02: /* AttribQuick */ 2561 cmdlen = 0; 2562 buflen = 0; 2563 break; 2564 case 0x04: /* AttribSendReceive */ 2565 cmdlen = 0; 2566 buflen = 1; 2567 break; 2568 case 0x06: /* AttribByte */ 2569 cmdlen = 1; 2570 buflen = 1; 2571 break; 2572 case 0x08: /* AttribWord */ 2573 cmdlen = 1; 2574 buflen = 2; 2575 break; 2576 case 0x0b: /* AttribBytes */ 2577 cmdlen = 1; 2578 buflen = alen; 2579 break; 2580 case 0x0e: /* AttribRawBytes */ 2581 cmdlen = 0; 2582 buflen = alen; 2583 break; 2584 default: 2585 aml_die("unsupported access type 0x%x", flag); 2586 break; 2587 } 2588 break; 2589 case 1: /* AttribBytes */ 2590 cmdlen = 1; 2591 buflen = AML_FIELD_ATTR(flag); 2592 break; 2593 case 2: /* AttribRawBytes */ 2594 cmdlen = 0; 2595 buflen = AML_FIELD_ATTR(flag); 2596 break; 2597 default: 2598 aml_die("unsupported access type 0x%x", flag); 2599 break; 2600 } 2601 2602 if (mode == ACPI_IOREAD) { 2603 _aml_setvalue(val, AML_OBJTYPE_BUFFER, buflen + 2, NULL); 2604 op = I2C_OP_READ_WITH_STOP; 2605 } else { 2606 op = I2C_OP_WRITE_WITH_STOP; 2607 } 2608 2609 tag = node->i2c; 2610 addr = crs->lr_i2cbus._adr; 2611 cmd = bpos >> 3; 2612 buf = val->v_buffer; 2613 2614 iic_acquire_bus(tag, 0); 2615 err = iic_exec(tag, op, addr, &cmd, cmdlen, &buf[2], buflen, 0); 2616 iic_release_bus(tag, 0); 2617 2618 /* 2619 * The ACPI specification doesn't tell us what the status 2620 * codes mean beyond implying that zero means success. So use 2621 * the error returned from the transfer. All possible error 2622 * numbers should fit in a single byte. 2623 */ 2624 buf[0] = err; 2625 } 2626 2627 #endif 2628 2629 void 2630 aml_rwindexfield(struct aml_value *fld, struct aml_value *val, int mode) 2631 { 2632 struct aml_value tmp, *ref1, *ref2; 2633 void *tbit, *vbit; 2634 int vpos, bpos, blen; 2635 int indexval; 2636 int sz, len; 2637 2638 ref2 = fld->v_field.ref2; 2639 ref1 = fld->v_field.ref1; 2640 bpos = fld->v_field.bitpos; 2641 blen = fld->v_field.bitlen; 2642 2643 memset(&tmp, 0, sizeof(tmp)); 2644 tmp.refcnt = 99; 2645 2646 /* Get field access size */ 2647 switch (AML_FIELD_ACCESS(fld->v_field.flags)) { 2648 case AML_FIELD_WORDACC: 2649 sz = 2; 2650 break; 2651 case AML_FIELD_DWORDACC: 2652 sz = 4; 2653 break; 2654 case AML_FIELD_QWORDACC: 2655 sz = 8; 2656 break; 2657 default: 2658 sz = 1; 2659 break; 2660 } 2661 2662 if (blen > aml_intlen) { 2663 if (mode == ACPI_IOREAD) { 2664 /* Read from a large field: create buffer */ 2665 _aml_setvalue(val, AML_OBJTYPE_BUFFER, 2666 (blen + 7) >> 3, 0); 2667 } 2668 vbit = val->v_buffer; 2669 } else { 2670 if (mode == ACPI_IOREAD) { 2671 /* Read from a short field: initialize integer */ 2672 _aml_setvalue(val, AML_OBJTYPE_INTEGER, 0, 0); 2673 } 2674 vbit = &val->v_integer; 2675 } 2676 tbit = &tmp.v_integer; 2677 vpos = 0; 2678 2679 indexval = (bpos >> 3) & ~(sz - 1); 2680 bpos = bpos - (indexval << 3); 2681 2682 while (blen > 0) { 2683 len = min(blen, (sz << 3) - bpos); 2684 2685 /* Write index register */ 2686 _aml_setvalue(&tmp, AML_OBJTYPE_INTEGER, indexval, 0); 2687 aml_rwfield(ref2, 0, aml_intlen, &tmp, ACPI_IOWRITE); 2688 indexval += sz; 2689 2690 /* Read/write data register */ 2691 _aml_setvalue(&tmp, AML_OBJTYPE_INTEGER, 0, 0); 2692 if (mode == ACPI_IOWRITE) 2693 aml_bufcpy(tbit, 0, vbit, vpos, len); 2694 aml_rwfield(ref1, bpos, len, &tmp, mode); 2695 if (mode == ACPI_IOREAD) 2696 aml_bufcpy(vbit, vpos, tbit, 0, len); 2697 vpos += len; 2698 blen -= len; 2699 bpos = 0; 2700 } 2701 } 2702 2703 void 2704 aml_rwfield(struct aml_value *fld, int bpos, int blen, struct aml_value *val, 2705 int mode) 2706 { 2707 struct aml_value tmp, *ref1, *ref2; 2708 2709 ref2 = fld->v_field.ref2; 2710 ref1 = fld->v_field.ref1; 2711 if (blen > fld->v_field.bitlen) 2712 blen = fld->v_field.bitlen; 2713 2714 aml_lockfield(NULL, fld); 2715 memset(&tmp, 0, sizeof(tmp)); 2716 aml_addref(&tmp, "fld.write"); 2717 if (fld->v_field.type == AMLOP_INDEXFIELD) { 2718 aml_rwindexfield(fld, val, mode); 2719 } else if (fld->v_field.type == AMLOP_BANKFIELD) { 2720 _aml_setvalue(&tmp, AML_OBJTYPE_INTEGER, fld->v_field.ref3, 0); 2721 aml_rwfield(ref2, 0, aml_intlen, &tmp, ACPI_IOWRITE); 2722 aml_rwgen(ref1, fld->v_field.bitpos, fld->v_field.bitlen, 2723 val, mode, fld->v_field.flags); 2724 } else if (fld->v_field.type == AMLOP_FIELD) { 2725 switch (ref1->v_opregion.iospace) { 2726 case ACPI_OPREG_GPIO: 2727 aml_rwgpio(ref2, bpos, blen, val, mode, 2728 fld->v_field.flags); 2729 break; 2730 #ifndef SMALL_KERNEL 2731 case ACPI_OPREG_GSB: 2732 aml_rwgsb(ref2, fld->v_field.ref3, 2733 fld->v_field.bitpos + bpos, blen, 2734 val, mode, fld->v_field.flags); 2735 break; 2736 #endif 2737 default: 2738 aml_rwgen(ref1, fld->v_field.bitpos + bpos, blen, 2739 val, mode, fld->v_field.flags); 2740 break; 2741 } 2742 } else if (mode == ACPI_IOREAD) { 2743 /* bufferfield:read */ 2744 _aml_setvalue(val, AML_OBJTYPE_INTEGER, 0, 0); 2745 aml_bufcpy(&val->v_integer, 0, ref1->v_buffer, 2746 fld->v_field.bitpos, fld->v_field.bitlen); 2747 } else { 2748 /* bufferfield:write */ 2749 val = aml_convert(val, AML_OBJTYPE_INTEGER, -1); 2750 aml_bufcpy(ref1->v_buffer, fld->v_field.bitpos, &val->v_integer, 2751 0, fld->v_field.bitlen); 2752 aml_delref(&val, "wrbuffld"); 2753 } 2754 aml_unlockfield(NULL, fld); 2755 } 2756 2757 /* Create Field Object data index 2758 * AMLOP_FIELD n:OpRegion NULL 2759 * AMLOP_INDEXFIELD n:Field n:Field 2760 * AMLOP_BANKFIELD n:OpRegion n:Field 2761 * AMLOP_CREATEFIELD t:Buffer NULL 2762 * AMLOP_CREATEBITFIELD t:Buffer NULL 2763 * AMLOP_CREATEBYTEFIELD t:Buffer NULL 2764 * AMLOP_CREATEWORDFIELD t:Buffer NULL 2765 * AMLOP_CREATEDWORDFIELD t:Buffer NULL 2766 * AMLOP_CREATEQWORDFIELD t:Buffer NULL 2767 * AMLOP_INDEX t:Buffer NULL 2768 */ 2769 void 2770 aml_createfield(struct aml_value *field, int opcode, 2771 struct aml_value *data, int bpos, int blen, 2772 struct aml_value *index, int indexval, int flags) 2773 { 2774 dnprintf(10, "## %s(%s): %s %.4x-%.4x\n", 2775 aml_mnem(opcode, 0), 2776 blen > aml_intlen ? "BUF" : "INT", 2777 aml_nodename(field->node), bpos, blen); 2778 if (index) { 2779 dnprintf(10, " index:%s:%.2x\n", aml_nodename(index->node), 2780 indexval); 2781 } 2782 dnprintf(10, " data:%s\n", aml_nodename(data->node)); 2783 field->type = (opcode == AMLOP_FIELD || 2784 opcode == AMLOP_INDEXFIELD || 2785 opcode == AMLOP_BANKFIELD) ? 2786 AML_OBJTYPE_FIELDUNIT : 2787 AML_OBJTYPE_BUFFERFIELD; 2788 2789 if (field->type == AML_OBJTYPE_BUFFERFIELD && 2790 data->type != AML_OBJTYPE_BUFFER) 2791 data = aml_convert(data, AML_OBJTYPE_BUFFER, -1); 2792 2793 field->v_field.type = opcode; 2794 field->v_field.bitpos = bpos; 2795 field->v_field.bitlen = blen; 2796 field->v_field.ref3 = indexval; 2797 field->v_field.ref2 = index; 2798 field->v_field.ref1 = data; 2799 field->v_field.flags = flags; 2800 2801 /* Increase reference count */ 2802 aml_addref(data, "Field.Data"); 2803 aml_addref(index, "Field.Index"); 2804 } 2805 2806 /* Parse Field/IndexField/BankField scope */ 2807 void 2808 aml_parsefieldlist(struct aml_scope *mscope, int opcode, int flags, 2809 struct aml_value *data, struct aml_value *index, int indexval) 2810 { 2811 struct aml_value *conn = NULL; 2812 struct aml_value *rv; 2813 int bpos, blen; 2814 2815 if (mscope == NULL) 2816 return; 2817 bpos = 0; 2818 while (mscope->pos < mscope->end) { 2819 switch (*mscope->pos) { 2820 case 0x00: /* ReservedField */ 2821 mscope->pos++; 2822 blen = aml_parselength(mscope); 2823 break; 2824 case 0x01: /* AccessField */ 2825 mscope->pos++; 2826 blen = 0; 2827 flags = aml_get8(mscope->pos++); 2828 flags |= aml_get8(mscope->pos++) << 8; 2829 break; 2830 case 0x02: /* ConnectionField */ 2831 mscope->pos++; 2832 blen = 0; 2833 conn = aml_parse(mscope, 'o', "Connection"); 2834 if (conn == NULL) 2835 aml_die("Could not parse connection"); 2836 conn->node = mscope->node; 2837 break; 2838 case 0x03: /* ExtendedAccessField */ 2839 mscope->pos++; 2840 blen = 0; 2841 flags = aml_get8(mscope->pos++); 2842 flags |= aml_get8(mscope->pos++) << 8; 2843 indexval = aml_get8(mscope->pos++); 2844 break; 2845 default: /* NamedField */ 2846 mscope->pos = aml_parsename(mscope->node, mscope->pos, 2847 &rv, 1); 2848 blen = aml_parselength(mscope); 2849 aml_createfield(rv, opcode, data, bpos, blen, 2850 conn ? conn : index, indexval, flags); 2851 aml_delref(&rv, 0); 2852 break; 2853 } 2854 bpos += blen; 2855 } 2856 aml_popscope(mscope); 2857 } 2858 2859 /* 2860 * Mutex/Event utility functions 2861 */ 2862 int acpi_mutex_acquire(struct aml_scope *, struct aml_value *, int); 2863 void acpi_mutex_release(struct aml_scope *, struct aml_value *); 2864 int acpi_event_wait(struct aml_scope *, struct aml_value *, int); 2865 void acpi_event_signal(struct aml_scope *, struct aml_value *); 2866 void acpi_event_reset(struct aml_scope *, struct aml_value *); 2867 2868 int 2869 acpi_mutex_acquire(struct aml_scope *scope, struct aml_value *mtx, 2870 int timeout) 2871 { 2872 if (mtx->v_mtx.owner == NULL || scope == mtx->v_mtx.owner) { 2873 /* We are now the owner */ 2874 mtx->v_mtx.owner = scope; 2875 if (mtx == aml_global_lock) { 2876 dnprintf(10,"LOCKING GLOBAL\n"); 2877 acpi_glk_enter(); 2878 } 2879 dnprintf(5,"%s acquires mutex %s\n", scope->node->name, 2880 mtx->node->name); 2881 return (0); 2882 } else if (timeout == 0) { 2883 return (-1); 2884 } 2885 /* Wait for mutex */ 2886 return (0); 2887 } 2888 2889 void 2890 acpi_mutex_release(struct aml_scope *scope, struct aml_value *mtx) 2891 { 2892 if (mtx == aml_global_lock) { 2893 dnprintf(10,"UNLOCKING GLOBAL\n"); 2894 acpi_glk_leave(); 2895 } 2896 dnprintf(5, "%s releases mutex %s\n", scope->node->name, 2897 mtx->node->name); 2898 mtx->v_mtx.owner = NULL; 2899 /* Wakeup waiters */ 2900 } 2901 2902 int 2903 acpi_event_wait(struct aml_scope *scope, struct aml_value *evt, int timeout) 2904 { 2905 /* Wait for event to occur; do work in meantime */ 2906 while (evt->v_evt.state == 0 && timeout >= 0) { 2907 if (acpi_dotask(acpi_softc)) 2908 continue; 2909 if (!cold) { 2910 if (rwsleep(evt, &acpi_softc->sc_lck, PWAIT, 2911 "acpievt", 1) == EWOULDBLOCK) { 2912 if (timeout < AML_NO_TIMEOUT) 2913 timeout -= (1000 / hz); 2914 } 2915 } else { 2916 delay(1000); 2917 if (timeout < AML_NO_TIMEOUT) 2918 timeout--; 2919 } 2920 } 2921 if (evt->v_evt.state == 0) 2922 return (-1); 2923 evt->v_evt.state--; 2924 return (0); 2925 } 2926 2927 void 2928 acpi_event_signal(struct aml_scope *scope, struct aml_value *evt) 2929 { 2930 evt->v_evt.state++; 2931 if (evt->v_evt.state > 0) 2932 wakeup_one(evt); 2933 } 2934 2935 void 2936 acpi_event_reset(struct aml_scope *scope, struct aml_value *evt) 2937 { 2938 evt->v_evt.state = 0; 2939 } 2940 2941 /* Store result value into an object */ 2942 void 2943 aml_store(struct aml_scope *scope, struct aml_value *lhs , int64_t ival, 2944 struct aml_value *rhs) 2945 { 2946 struct aml_value tmp; 2947 struct aml_node *node; 2948 int mlen; 2949 2950 /* Already set */ 2951 if (lhs == rhs || lhs == NULL || lhs->type == AML_OBJTYPE_NOTARGET) { 2952 return; 2953 } 2954 memset(&tmp, 0, sizeof(tmp)); 2955 tmp.refcnt=99; 2956 if (rhs == NULL) { 2957 rhs = _aml_setvalue(&tmp, AML_OBJTYPE_INTEGER, ival, NULL); 2958 } 2959 if (rhs->type == AML_OBJTYPE_BUFFERFIELD || 2960 rhs->type == AML_OBJTYPE_FIELDUNIT) { 2961 aml_rwfield(rhs, 0, rhs->v_field.bitlen, &tmp, ACPI_IOREAD); 2962 rhs = &tmp; 2963 } 2964 /* Store to LocalX: free value */ 2965 if (lhs->stack >= AMLOP_LOCAL0 && lhs->stack <= AMLOP_LOCAL7) 2966 aml_freevalue(lhs); 2967 2968 lhs = aml_gettgt(lhs, AMLOP_STORE); 2969 switch (lhs->type) { 2970 case AML_OBJTYPE_UNINITIALIZED: 2971 aml_copyvalue(lhs, rhs); 2972 break; 2973 case AML_OBJTYPE_BUFFERFIELD: 2974 case AML_OBJTYPE_FIELDUNIT: 2975 aml_rwfield(lhs, 0, lhs->v_field.bitlen, rhs, ACPI_IOWRITE); 2976 break; 2977 case AML_OBJTYPE_DEBUGOBJ: 2978 break; 2979 case AML_OBJTYPE_INTEGER: 2980 rhs = aml_convert(rhs, lhs->type, -1); 2981 lhs->v_integer = rhs->v_integer; 2982 aml_delref(&rhs, "store.int"); 2983 break; 2984 case AML_OBJTYPE_BUFFER: 2985 case AML_OBJTYPE_STRING: 2986 rhs = aml_convert(rhs, lhs->type, -1); 2987 if (lhs->length < rhs->length) { 2988 dnprintf(10, "Overrun! %d,%d\n", 2989 lhs->length, rhs->length); 2990 aml_freevalue(lhs); 2991 _aml_setvalue(lhs, rhs->type, rhs->length, NULL); 2992 } 2993 mlen = min(lhs->length, rhs->length); 2994 memset(lhs->v_buffer, 0x00, lhs->length); 2995 memcpy(lhs->v_buffer, rhs->v_buffer, mlen); 2996 aml_delref(&rhs, "store.bufstr"); 2997 break; 2998 case AML_OBJTYPE_PACKAGE: 2999 /* Convert to LHS type, copy into LHS */ 3000 if (rhs->type != AML_OBJTYPE_PACKAGE) { 3001 aml_die("Copy non-package into package?"); 3002 } 3003 aml_freevalue(lhs); 3004 aml_copyvalue(lhs, rhs); 3005 break; 3006 case AML_OBJTYPE_NAMEREF: 3007 node = __aml_searchname(scope->node, lhs->v_nameref, 1); 3008 if (node == NULL) { 3009 aml_die("Could not create node %s", lhs->v_nameref); 3010 } 3011 aml_copyvalue(node->value, rhs); 3012 break; 3013 case AML_OBJTYPE_METHOD: 3014 /* Method override */ 3015 if (rhs->type != AML_OBJTYPE_INTEGER) { 3016 aml_die("Overriding a method with a non-int?"); 3017 } 3018 aml_freevalue(lhs); 3019 aml_copyvalue(lhs, rhs); 3020 break; 3021 default: 3022 aml_die("Store to default type! %x\n", lhs->type); 3023 break; 3024 } 3025 aml_freevalue(&tmp); 3026 } 3027 3028 #ifdef DDB 3029 /* Disassembler routines */ 3030 void aml_disprintf(void *arg, const char *fmt, ...); 3031 3032 void 3033 aml_disprintf(void *arg, const char *fmt, ...) 3034 { 3035 va_list ap; 3036 3037 va_start(ap, fmt); 3038 vprintf(fmt, ap); 3039 va_end(ap); 3040 } 3041 3042 void 3043 aml_disasm(struct aml_scope *scope, int lvl, 3044 void (*dbprintf)(void *, const char *, ...) 3045 __attribute__((__format__(__kprintf__,2,3))), 3046 void *arg) 3047 { 3048 int pc, opcode; 3049 struct aml_opcode *htab; 3050 uint64_t ival; 3051 struct aml_value *rv, tmp; 3052 uint8_t *end = NULL; 3053 struct aml_scope ms; 3054 char *ch; 3055 char mch[64]; 3056 3057 if (dbprintf == NULL) 3058 dbprintf = aml_disprintf; 3059 3060 pc = aml_pc(scope->pos); 3061 opcode = aml_parseopcode(scope); 3062 htab = aml_findopcode(opcode); 3063 3064 /* Display address + indent */ 3065 if (lvl <= 0x7FFF) { 3066 dbprintf(arg, "%.4x ", pc); 3067 for (pc=0; pc<lvl; pc++) { 3068 dbprintf(arg, " "); 3069 } 3070 } 3071 ch = NULL; 3072 switch (opcode) { 3073 case AMLOP_NAMECHAR: 3074 scope->pos = aml_parsename(scope->node, scope->pos, &rv, 0); 3075 if (rv->type == AML_OBJTYPE_NAMEREF) { 3076 ch = "@@@"; 3077 aml_delref(&rv, "disasm"); 3078 break; 3079 } 3080 /* if this is a method, get arguments */ 3081 strlcpy(mch, aml_nodename(rv->node), sizeof(mch)); 3082 if (rv->type == AML_OBJTYPE_METHOD) { 3083 strlcat(mch, "(", sizeof(mch)); 3084 for (ival=0; 3085 ival < AML_METHOD_ARGCOUNT(rv->v_method.flags); 3086 ival++) { 3087 strlcat(mch, ival ? ", %z" : "%z", 3088 sizeof(mch)); 3089 } 3090 strlcat(mch, ")", sizeof(mch)); 3091 } 3092 aml_delref(&rv, ""); 3093 ch = mch; 3094 break; 3095 3096 case AMLOP_ZERO: 3097 case AMLOP_ONE: 3098 case AMLOP_ONES: 3099 case AMLOP_LOCAL0: 3100 case AMLOP_LOCAL1: 3101 case AMLOP_LOCAL2: 3102 case AMLOP_LOCAL3: 3103 case AMLOP_LOCAL4: 3104 case AMLOP_LOCAL5: 3105 case AMLOP_LOCAL6: 3106 case AMLOP_LOCAL7: 3107 case AMLOP_ARG0: 3108 case AMLOP_ARG1: 3109 case AMLOP_ARG2: 3110 case AMLOP_ARG3: 3111 case AMLOP_ARG4: 3112 case AMLOP_ARG5: 3113 case AMLOP_ARG6: 3114 case AMLOP_NOP: 3115 case AMLOP_REVISION: 3116 case AMLOP_DEBUG: 3117 case AMLOP_CONTINUE: 3118 case AMLOP_BREAKPOINT: 3119 case AMLOP_BREAK: 3120 ch="%m"; 3121 break; 3122 case AMLOP_BYTEPREFIX: 3123 ch="%b"; 3124 break; 3125 case AMLOP_WORDPREFIX: 3126 ch="%w"; 3127 break; 3128 case AMLOP_DWORDPREFIX: 3129 ch="%d"; 3130 break; 3131 case AMLOP_QWORDPREFIX: 3132 ch="%q"; 3133 break; 3134 case AMLOP_STRINGPREFIX: 3135 ch="%a"; 3136 break; 3137 3138 case AMLOP_INCREMENT: 3139 case AMLOP_DECREMENT: 3140 case AMLOP_LNOT: 3141 case AMLOP_SIZEOF: 3142 case AMLOP_DEREFOF: 3143 case AMLOP_REFOF: 3144 case AMLOP_OBJECTTYPE: 3145 case AMLOP_UNLOAD: 3146 case AMLOP_RELEASE: 3147 case AMLOP_SIGNAL: 3148 case AMLOP_RESET: 3149 case AMLOP_STALL: 3150 case AMLOP_SLEEP: 3151 case AMLOP_RETURN: 3152 ch="%m(%n)"; 3153 break; 3154 case AMLOP_OR: 3155 case AMLOP_ADD: 3156 case AMLOP_AND: 3157 case AMLOP_NAND: 3158 case AMLOP_XOR: 3159 case AMLOP_SHL: 3160 case AMLOP_SHR: 3161 case AMLOP_NOR: 3162 case AMLOP_MOD: 3163 case AMLOP_SUBTRACT: 3164 case AMLOP_MULTIPLY: 3165 case AMLOP_INDEX: 3166 case AMLOP_CONCAT: 3167 case AMLOP_CONCATRES: 3168 case AMLOP_TOSTRING: 3169 ch="%m(%n, %n, %n)"; 3170 break; 3171 case AMLOP_CREATEBYTEFIELD: 3172 case AMLOP_CREATEWORDFIELD: 3173 case AMLOP_CREATEDWORDFIELD: 3174 case AMLOP_CREATEQWORDFIELD: 3175 case AMLOP_CREATEBITFIELD: 3176 ch="%m(%n, %n, %N)"; 3177 break; 3178 case AMLOP_CREATEFIELD: 3179 ch="%m(%n, %n, %n, %N)"; 3180 break; 3181 case AMLOP_DIVIDE: 3182 case AMLOP_MID: 3183 ch="%m(%n, %n, %n, %n)"; 3184 break; 3185 case AMLOP_LAND: 3186 case AMLOP_LOR: 3187 case AMLOP_LNOTEQUAL: 3188 case AMLOP_LLESSEQUAL: 3189 case AMLOP_LLESS: 3190 case AMLOP_LEQUAL: 3191 case AMLOP_LGREATEREQUAL: 3192 case AMLOP_LGREATER: 3193 case AMLOP_NOT: 3194 case AMLOP_FINDSETLEFTBIT: 3195 case AMLOP_FINDSETRIGHTBIT: 3196 case AMLOP_TOINTEGER: 3197 case AMLOP_TOBUFFER: 3198 case AMLOP_TOHEXSTRING: 3199 case AMLOP_TODECSTRING: 3200 case AMLOP_FROMBCD: 3201 case AMLOP_TOBCD: 3202 case AMLOP_WAIT: 3203 case AMLOP_LOAD: 3204 case AMLOP_STORE: 3205 case AMLOP_NOTIFY: 3206 case AMLOP_COPYOBJECT: 3207 ch="%m(%n, %n)"; 3208 break; 3209 case AMLOP_ACQUIRE: 3210 ch = "%m(%n, %w)"; 3211 break; 3212 case AMLOP_CONDREFOF: 3213 ch="%m(%R, %n)"; 3214 break; 3215 case AMLOP_ALIAS: 3216 ch="%m(%n, %N)"; 3217 break; 3218 case AMLOP_NAME: 3219 ch="%m(%N, %n)"; 3220 break; 3221 case AMLOP_EVENT: 3222 ch="%m(%N)"; 3223 break; 3224 case AMLOP_MUTEX: 3225 ch = "%m(%N, %b)"; 3226 break; 3227 case AMLOP_OPREGION: 3228 ch = "%m(%N, %b, %n, %n)"; 3229 break; 3230 case AMLOP_DATAREGION: 3231 ch="%m(%N, %n, %n, %n)"; 3232 break; 3233 case AMLOP_FATAL: 3234 ch = "%m(%b, %d, %n)"; 3235 break; 3236 case AMLOP_IF: 3237 case AMLOP_WHILE: 3238 case AMLOP_SCOPE: 3239 case AMLOP_THERMALZONE: 3240 case AMLOP_VARPACKAGE: 3241 end = aml_parseend(scope); 3242 ch = "%m(%n) {\n%T}"; 3243 break; 3244 case AMLOP_DEVICE: 3245 end = aml_parseend(scope); 3246 ch = "%m(%N) {\n%T}"; 3247 break; 3248 case AMLOP_POWERRSRC: 3249 end = aml_parseend(scope); 3250 ch = "%m(%N, %b, %w) {\n%T}"; 3251 break; 3252 case AMLOP_PROCESSOR: 3253 end = aml_parseend(scope); 3254 ch = "%m(%N, %b, %d, %b) {\n%T}"; 3255 break; 3256 case AMLOP_METHOD: 3257 end = aml_parseend(scope); 3258 ch = "%m(%N, %b) {\n%T}"; 3259 break; 3260 case AMLOP_PACKAGE: 3261 end = aml_parseend(scope); 3262 ch = "%m(%b) {\n%T}"; 3263 break; 3264 case AMLOP_ELSE: 3265 end = aml_parseend(scope); 3266 ch = "%m {\n%T}"; 3267 break; 3268 case AMLOP_BUFFER: 3269 end = aml_parseend(scope); 3270 ch = "%m(%n) { %B }"; 3271 break; 3272 case AMLOP_INDEXFIELD: 3273 end = aml_parseend(scope); 3274 ch = "%m(%n, %n, %b) {\n%F}"; 3275 break; 3276 case AMLOP_BANKFIELD: 3277 end = aml_parseend(scope); 3278 ch = "%m(%n, %n, %n, %b) {\n%F}"; 3279 break; 3280 case AMLOP_FIELD: 3281 end = aml_parseend(scope); 3282 ch = "%m(%n, %b) {\n%F}"; 3283 break; 3284 case AMLOP_MATCH: 3285 ch = "%m(%n, %b, %n, %b, %n, %n)"; 3286 break; 3287 case AMLOP_LOADTABLE: 3288 ch = "%m(%n, %n, %n, %n, %n, %n)"; 3289 break; 3290 default: 3291 aml_die("opcode = %x\n", opcode); 3292 break; 3293 } 3294 3295 /* Parse printable buffer args */ 3296 while (ch && *ch) { 3297 char c; 3298 3299 if (*ch != '%') { 3300 dbprintf(arg,"%c", *(ch++)); 3301 continue; 3302 } 3303 c = *(++ch); 3304 switch (c) { 3305 case 'b': 3306 case 'w': 3307 case 'd': 3308 case 'q': 3309 /* Parse simple object: don't allocate */ 3310 aml_parsesimple(scope, c, &tmp); 3311 dbprintf(arg,"0x%llx", tmp.v_integer); 3312 break; 3313 case 'a': 3314 dbprintf(arg, "\'%s\'", scope->pos); 3315 scope->pos += strlen(scope->pos)+1; 3316 break; 3317 case 'N': 3318 /* Create Name */ 3319 rv = aml_parsesimple(scope, c, NULL); 3320 dbprintf(arg, "%s", aml_nodename(rv->node)); 3321 break; 3322 case 'm': 3323 /* display mnemonic */ 3324 dbprintf(arg, "%s", htab->mnem); 3325 break; 3326 case 'R': 3327 /* Search name */ 3328 printf("%s", aml_getname(scope->pos)); 3329 scope->pos = aml_parsename(scope->node, scope->pos, 3330 &rv, 0); 3331 aml_delref(&rv, 0); 3332 break; 3333 case 'z': 3334 case 'n': 3335 /* generic arg: recurse */ 3336 aml_disasm(scope, lvl | 0x8000, dbprintf, arg); 3337 break; 3338 case 'B': 3339 /* Buffer */ 3340 scope->pos = end; 3341 break; 3342 case 'F': 3343 /* Scope: Field List */ 3344 memset(&ms, 0, sizeof(ms)); 3345 ms.node = scope->node; 3346 ms.start = scope->pos; 3347 ms.end = end; 3348 ms.pos = ms.start; 3349 ms.type = AMLOP_FIELD; 3350 3351 while (ms.pos < ms.end) { 3352 if (*ms.pos == 0x00) { 3353 ms.pos++; 3354 aml_parselength(&ms); 3355 } else if (*ms.pos == 0x01) { 3356 ms.pos+=3; 3357 } else { 3358 ms.pos = aml_parsename(ms.node, 3359 ms.pos, &rv, 1); 3360 aml_parselength(&ms); 3361 dbprintf(arg," %s\n", 3362 aml_nodename(rv->node)); 3363 aml_delref(&rv, 0); 3364 } 3365 } 3366 3367 /* Display address and closing bracket */ 3368 dbprintf(arg,"%.4x ", aml_pc(scope->pos)); 3369 for (pc=0; pc<(lvl & 0x7FFF); pc++) { 3370 dbprintf(arg," "); 3371 } 3372 scope->pos = end; 3373 break; 3374 case 'T': 3375 /* Scope: Termlist */ 3376 memset(&ms, 0, sizeof(ms)); 3377 ms.node = scope->node; 3378 ms.start = scope->pos; 3379 ms.end = end; 3380 ms.pos = ms.start; 3381 ms.type = AMLOP_SCOPE; 3382 3383 while (ms.pos < ms.end) { 3384 aml_disasm(&ms, (lvl + 1) & 0x7FFF, 3385 dbprintf, arg); 3386 } 3387 3388 /* Display address and closing bracket */ 3389 dbprintf(arg,"%.4x ", aml_pc(scope->pos)); 3390 for (pc=0; pc<(lvl & 0x7FFF); pc++) { 3391 dbprintf(arg," "); 3392 } 3393 scope->pos = end; 3394 break; 3395 } 3396 ch++; 3397 } 3398 if (lvl <= 0x7FFF) { 3399 dbprintf(arg,"\n"); 3400 } 3401 } 3402 #endif /* DDB */ 3403 3404 int aml_busy; 3405 3406 /* Evaluate method or buffervalue objects */ 3407 struct aml_value * 3408 aml_eval(struct aml_scope *scope, struct aml_value *my_ret, int ret_type, 3409 int argc, struct aml_value *argv) 3410 { 3411 struct aml_value *tmp = my_ret; 3412 struct aml_scope *ms; 3413 int idx; 3414 3415 switch (tmp->type) { 3416 case AML_OBJTYPE_NAMEREF: 3417 my_ret = aml_seterror(scope, "Undefined name: %s", 3418 aml_getname(my_ret->v_nameref)); 3419 break; 3420 case AML_OBJTYPE_METHOD: 3421 dnprintf(10,"\n--== Eval Method [%s, %d args] to %c ==--\n", 3422 aml_nodename(tmp->node), 3423 AML_METHOD_ARGCOUNT(tmp->v_method.flags), 3424 ret_type); 3425 ms = aml_pushscope(scope, tmp, tmp->node, AMLOP_METHOD); 3426 3427 /* Parse method arguments */ 3428 for (idx=0; idx<AML_METHOD_ARGCOUNT(tmp->v_method.flags); idx++) { 3429 struct aml_value *sp; 3430 3431 sp = aml_getstack(ms, AMLOP_ARG0+idx); 3432 if (argv) { 3433 aml_copyvalue(sp, &argv[idx]); 3434 } else { 3435 _aml_setvalue(sp, AML_OBJTYPE_OBJREF, AMLOP_ARG0 + idx, 0); 3436 sp->v_objref.ref = aml_parse(scope, 't', "ARGX"); 3437 } 3438 } 3439 #ifdef ACPI_DEBUG 3440 aml_showstack(ms); 3441 #endif 3442 3443 /* Evaluate method scope */ 3444 aml_root.start = tmp->v_method.base; 3445 if (tmp->v_method.fneval != NULL) { 3446 my_ret = tmp->v_method.fneval(ms, NULL); 3447 } else { 3448 aml_parse(ms, 'T', "METHEVAL"); 3449 my_ret = ms->retv; 3450 } 3451 dnprintf(10,"\n--==Finished evaluating method: %s %c\n", 3452 aml_nodename(tmp->node), ret_type); 3453 #ifdef ACPI_DEBUG 3454 aml_showvalue(my_ret); 3455 aml_showstack(ms); 3456 #endif 3457 aml_popscope(ms); 3458 break; 3459 case AML_OBJTYPE_BUFFERFIELD: 3460 case AML_OBJTYPE_FIELDUNIT: 3461 my_ret = aml_allocvalue(0,0,NULL); 3462 dnprintf(20,"quick: Convert Bufferfield to %c %p\n", 3463 ret_type, my_ret); 3464 aml_rwfield(tmp, 0, tmp->v_field.bitlen, my_ret, ACPI_IOREAD); 3465 break; 3466 } 3467 if (ret_type == 'i' && my_ret && my_ret->type != AML_OBJTYPE_INTEGER) { 3468 #ifndef SMALL_KERNEL 3469 aml_showvalue(my_ret); 3470 #endif 3471 aml_die("Not Integer"); 3472 } 3473 return my_ret; 3474 } 3475 3476 /* 3477 * The following opcodes produce return values 3478 * TOSTRING -> Str 3479 * TOHEXSTR -> Str 3480 * TODECSTR -> Str 3481 * STRINGPFX -> Str 3482 * BUFFER -> Buf 3483 * CONCATRES -> Buf 3484 * TOBUFFER -> Buf 3485 * MID -> Buf|Str 3486 * CONCAT -> Buf|Str 3487 * PACKAGE -> Pkg 3488 * VARPACKAGE -> Pkg 3489 * LOCALx -> Obj 3490 * ARGx -> Obj 3491 * NAMECHAR -> Obj 3492 * REFOF -> ObjRef 3493 * INDEX -> ObjRef 3494 * DEREFOF -> DataRefObj 3495 * COPYOBJECT -> DataRefObj 3496 * STORE -> DataRefObj 3497 3498 * ZERO -> Int 3499 * ONE -> Int 3500 * ONES -> Int 3501 * REVISION -> Int 3502 * B/W/D/Q -> Int 3503 * OR -> Int 3504 * AND -> Int 3505 * ADD -> Int 3506 * NAND -> Int 3507 * XOR -> Int 3508 * SHL -> Int 3509 * SHR -> Int 3510 * NOR -> Int 3511 * MOD -> Int 3512 * SUBTRACT -> Int 3513 * MULTIPLY -> Int 3514 * DIVIDE -> Int 3515 * NOT -> Int 3516 * TOBCD -> Int 3517 * FROMBCD -> Int 3518 * FSLEFTBIT -> Int 3519 * FSRIGHTBIT -> Int 3520 * INCREMENT -> Int 3521 * DECREMENT -> Int 3522 * TOINTEGER -> Int 3523 * MATCH -> Int 3524 * SIZEOF -> Int 3525 * OBJECTTYPE -> Int 3526 * TIMER -> Int 3527 3528 * CONDREFOF -> Bool 3529 * ACQUIRE -> Bool 3530 * WAIT -> Bool 3531 * LNOT -> Bool 3532 * LAND -> Bool 3533 * LOR -> Bool 3534 * LLESS -> Bool 3535 * LEQUAL -> Bool 3536 * LGREATER -> Bool 3537 * LNOTEQUAL -> Bool 3538 * LLESSEQUAL -> Bool 3539 * LGREATEREQ -> Bool 3540 3541 * LOADTABLE -> DDB 3542 * DEBUG -> Debug 3543 3544 * The following opcodes do not generate a return value: 3545 * NOP 3546 * BREAKPOINT 3547 * RELEASE 3548 * RESET 3549 * SIGNAL 3550 * NAME 3551 * ALIAS 3552 * OPREGION 3553 * DATAREGION 3554 * EVENT 3555 * MUTEX 3556 * SCOPE 3557 * DEVICE 3558 * THERMALZONE 3559 * POWERRSRC 3560 * PROCESSOR 3561 * METHOD 3562 * CREATEFIELD 3563 * CREATEBITFIELD 3564 * CREATEBYTEFIELD 3565 * CREATEWORDFIELD 3566 * CREATEDWORDFIELD 3567 * CREATEQWORDFIELD 3568 * FIELD 3569 * INDEXFIELD 3570 * BANKFIELD 3571 * STALL 3572 * SLEEP 3573 * NOTIFY 3574 * FATAL 3575 * LOAD 3576 * UNLOAD 3577 * IF 3578 * ELSE 3579 * WHILE 3580 * BREAK 3581 * CONTINUE 3582 */ 3583 3584 /* Parse a simple object from AML Bytestream */ 3585 struct aml_value * 3586 aml_parsesimple(struct aml_scope *scope, char ch, struct aml_value *rv) 3587 { 3588 if (rv == NULL) 3589 rv = aml_allocvalue(0,0,NULL); 3590 switch (ch) { 3591 case AML_ARG_REVISION: 3592 _aml_setvalue(rv, AML_OBJTYPE_INTEGER, AML_REVISION, NULL); 3593 break; 3594 case AML_ARG_DEBUG: 3595 _aml_setvalue(rv, AML_OBJTYPE_DEBUGOBJ, 0, NULL); 3596 break; 3597 case AML_ARG_BYTE: 3598 _aml_setvalue(rv, AML_OBJTYPE_INTEGER, 3599 aml_get8(scope->pos), NULL); 3600 scope->pos += 1; 3601 break; 3602 case AML_ARG_WORD: 3603 _aml_setvalue(rv, AML_OBJTYPE_INTEGER, 3604 aml_get16(scope->pos), NULL); 3605 scope->pos += 2; 3606 break; 3607 case AML_ARG_DWORD: 3608 _aml_setvalue(rv, AML_OBJTYPE_INTEGER, 3609 aml_get32(scope->pos), NULL); 3610 scope->pos += 4; 3611 break; 3612 case AML_ARG_QWORD: 3613 _aml_setvalue(rv, AML_OBJTYPE_INTEGER, 3614 aml_get64(scope->pos), NULL); 3615 scope->pos += 8; 3616 break; 3617 case AML_ARG_STRING: 3618 _aml_setvalue(rv, AML_OBJTYPE_STRING, -1, scope->pos); 3619 scope->pos += rv->length+1; 3620 break; 3621 } 3622 return rv; 3623 } 3624 3625 /* 3626 * Main Opcode Parser/Evaluator 3627 * 3628 * ret_type is expected type for return value 3629 * 'o' = Data Object (Int/Str/Buf/Pkg/Name) 3630 * 'i' = Integer 3631 * 't' = TermArg (Int/Str/Buf/Pkg) 3632 * 'r' = Target (NamedObj/Local/Arg/Null) 3633 * 'S' = SuperName (NamedObj/Local/Arg) 3634 * 'T' = TermList 3635 */ 3636 #define aml_debugger(x) 3637 3638 int maxdp; 3639 3640 struct aml_value * 3641 aml_gettgt(struct aml_value *val, int opcode) 3642 { 3643 while (val && val->type == AML_OBJTYPE_OBJREF) { 3644 val = val->v_objref.ref; 3645 } 3646 return val; 3647 } 3648 3649 struct aml_value * 3650 aml_seterror(struct aml_scope *scope, const char *fmt, ...) 3651 { 3652 va_list ap; 3653 3654 va_start(ap, fmt); 3655 printf("### AML PARSE ERROR (0x%x): ", aml_pc(scope->pos)); 3656 vprintf(fmt, ap); 3657 printf("\n"); 3658 va_end(ap); 3659 3660 while (scope) { 3661 scope->pos = scope->end; 3662 scope = scope->parent; 3663 } 3664 aml_error++; 3665 return aml_allocvalue(AML_OBJTYPE_INTEGER, 0, 0); 3666 } 3667 3668 struct aml_value * 3669 aml_loadtable(struct acpi_softc *sc, const char *signature, 3670 const char *oemid, const char *oemtableid, const char *rootpath, 3671 const char *parameterpath, struct aml_value *parameterdata) 3672 { 3673 struct acpi_table_header *hdr; 3674 struct acpi_dsdt *p_dsdt; 3675 struct acpi_q *entry; 3676 3677 if (strlen(rootpath) > 0) 3678 aml_die("LoadTable: RootPathString unsupported"); 3679 if (strlen(parameterpath) > 0) 3680 aml_die("LoadTable: ParameterPathString unsupported"); 3681 3682 SIMPLEQ_FOREACH(entry, &sc->sc_tables, q_next) { 3683 hdr = entry->q_table; 3684 if (strncmp(hdr->signature, signature, 3685 sizeof(hdr->signature)) == 0 && 3686 strncmp(hdr->oemid, oemid, sizeof(hdr->oemid)) == 0 && 3687 strncmp(hdr->oemtableid, oemtableid, 3688 sizeof(hdr->oemtableid)) == 0) { 3689 p_dsdt = entry->q_table; 3690 acpi_parse_aml(sc, p_dsdt->aml, p_dsdt->hdr_length - 3691 sizeof(p_dsdt->hdr)); 3692 return aml_allocvalue(AML_OBJTYPE_DDBHANDLE, 0, 0); 3693 } 3694 } 3695 3696 return aml_allocvalue(AML_OBJTYPE_INTEGER, 0, 0); 3697 } 3698 3699 /* Load new SSDT scope from memory address */ 3700 struct aml_scope * 3701 aml_load(struct acpi_softc *sc, struct aml_scope *scope, 3702 struct aml_value *rgn, struct aml_value *ddb) 3703 { 3704 struct acpi_q *entry; 3705 struct acpi_dsdt *p_ssdt; 3706 struct aml_value tmp; 3707 3708 ddb->type = AML_OBJTYPE_DDBHANDLE; 3709 ddb->v_integer = 0; 3710 3711 memset(&tmp, 0, sizeof(tmp)); 3712 if (rgn->type != AML_OBJTYPE_OPREGION || 3713 rgn->v_opregion.iospace != GAS_SYSTEM_MEMORY) 3714 goto fail; 3715 3716 /* Load SSDT from memory */ 3717 entry = acpi_maptable(sc, rgn->v_opregion.iobase, "SSDT", NULL, NULL, 1); 3718 if (entry == NULL) 3719 goto fail; 3720 3721 dnprintf(10, "%s: loaded SSDT %s @ %llx\n", sc->sc_dev.dv_xname, 3722 aml_nodename(rgn->node), rgn->v_opregion.iobase); 3723 ddb->v_integer = entry->q_id; 3724 3725 p_ssdt = entry->q_table; 3726 tmp.v_buffer = p_ssdt->aml; 3727 tmp.length = p_ssdt->hdr_length - sizeof(p_ssdt->hdr); 3728 3729 return aml_pushscope(scope, &tmp, scope->node, 3730 AMLOP_LOAD); 3731 fail: 3732 printf("%s: unable to load %s\n", sc->sc_dev.dv_xname, 3733 aml_nodename(rgn->node)); 3734 return NULL; 3735 } 3736 3737 struct aml_value * 3738 aml_parse(struct aml_scope *scope, int ret_type, const char *stype) 3739 { 3740 int opcode, idx, pc; 3741 struct aml_opcode *htab; 3742 struct aml_value *opargs[8], *my_ret, *rv; 3743 struct aml_scope *mscope, *iscope; 3744 uint8_t *start, *end; 3745 const char *ch; 3746 int64_t ival; 3747 struct timespec ts; 3748 3749 my_ret = NULL; 3750 if (scope == NULL || scope->pos >= scope->end) { 3751 return NULL; 3752 } 3753 if (odp++ > 125) 3754 panic("depth"); 3755 if (odp > maxdp) { 3756 maxdp = odp; 3757 dnprintf(10, "max depth: %d\n", maxdp); 3758 } 3759 end = NULL; 3760 iscope = scope; 3761 start: 3762 /* --== Stage 0: Get Opcode ==-- */ 3763 start = scope->pos; 3764 pc = aml_pc(scope->pos); 3765 aml_debugger(scope); 3766 3767 opcode = aml_parseopcode(scope); 3768 htab = aml_findopcode(opcode); 3769 if (htab == NULL) { 3770 /* No opcode handler */ 3771 aml_die("Unknown opcode: %.4x @ %.4x", opcode, pc); 3772 } 3773 dnprintf(18,"%.4x %s\n", pc, aml_mnem(opcode, scope->pos)); 3774 3775 /* --== Stage 1: Process opcode arguments ==-- */ 3776 memset(opargs, 0, sizeof(opargs)); 3777 idx = 0; 3778 for (ch = htab->args; *ch; ch++) { 3779 rv = NULL; 3780 switch (*ch) { 3781 case AML_ARG_OBJLEN: 3782 end = aml_parseend(scope); 3783 break; 3784 case AML_ARG_IFELSE: 3785 /* Special Case: IF-ELSE:piTbpT or IF:piT */ 3786 ch = (*end == AMLOP_ELSE && end < scope->end) ? 3787 "-TbpT" : "-T"; 3788 break; 3789 3790 /* Complex arguments */ 3791 case 's': 3792 case 'S': 3793 case AML_ARG_TARGET: 3794 case AML_ARG_TERMOBJ: 3795 case AML_ARG_INTEGER: 3796 if (*ch == 'r' && *scope->pos == AMLOP_ZERO) { 3797 /* Special case: NULL Target */ 3798 rv = aml_allocvalue(AML_OBJTYPE_NOTARGET, 0, NULL); 3799 scope->pos++; 3800 } 3801 else { 3802 rv = aml_parse(scope, *ch, htab->mnem); 3803 if (rv == NULL || aml_error) 3804 goto parse_error; 3805 } 3806 break; 3807 3808 /* Simple arguments */ 3809 case AML_ARG_BUFFER: 3810 case AML_ARG_METHOD: 3811 case AML_ARG_FIELDLIST: 3812 case AML_ARG_TERMOBJLIST: 3813 rv = aml_allocvalue(AML_OBJTYPE_SCOPE, 0, NULL); 3814 rv->v_buffer = scope->pos; 3815 rv->length = end - scope->pos; 3816 scope->pos = end; 3817 break; 3818 case AML_ARG_CONST: 3819 rv = aml_allocvalue(AML_OBJTYPE_INTEGER, 3820 (char)opcode, NULL); 3821 break; 3822 case AML_ARG_CREATENAME: 3823 scope->pos = aml_parsename(scope->node, scope->pos, 3824 &rv, 1); 3825 break; 3826 case AML_ARG_SEARCHNAME: 3827 scope->pos = aml_parsename(scope->node, scope->pos, 3828 &rv, 0); 3829 break; 3830 case AML_ARG_BYTE: 3831 case AML_ARG_WORD: 3832 case AML_ARG_DWORD: 3833 case AML_ARG_QWORD: 3834 case AML_ARG_DEBUG: 3835 case AML_ARG_STRING: 3836 case AML_ARG_REVISION: 3837 rv = aml_parsesimple(scope, *ch, NULL); 3838 break; 3839 case AML_ARG_STKLOCAL: 3840 case AML_ARG_STKARG: 3841 rv = aml_getstack(scope, opcode); 3842 break; 3843 default: 3844 aml_die("Unknown arg type: %c\n", *ch); 3845 break; 3846 } 3847 if (rv != NULL) 3848 opargs[idx++] = rv; 3849 } 3850 3851 /* --== Stage 2: Process opcode ==-- */ 3852 ival = 0; 3853 my_ret = NULL; 3854 mscope = NULL; 3855 switch (opcode) { 3856 case AMLOP_NOP: 3857 case AMLOP_BREAKPOINT: 3858 break; 3859 case AMLOP_LOCAL0: 3860 case AMLOP_LOCAL1: 3861 case AMLOP_LOCAL2: 3862 case AMLOP_LOCAL3: 3863 case AMLOP_LOCAL4: 3864 case AMLOP_LOCAL5: 3865 case AMLOP_LOCAL6: 3866 case AMLOP_LOCAL7: 3867 case AMLOP_ARG0: 3868 case AMLOP_ARG1: 3869 case AMLOP_ARG2: 3870 case AMLOP_ARG3: 3871 case AMLOP_ARG4: 3872 case AMLOP_ARG5: 3873 case AMLOP_ARG6: 3874 my_ret = opargs[0]; 3875 aml_addref(my_ret, htab->mnem); 3876 break; 3877 case AMLOP_NAMECHAR: 3878 /* opargs[0] = named object (node != NULL), or nameref */ 3879 my_ret = opargs[0]; 3880 if (scope->type == AMLOP_PACKAGE && my_ret->node) { 3881 /* Special case for package */ 3882 my_ret = aml_allocvalue(AML_OBJTYPE_OBJREF, 3883 AMLOP_NAMECHAR, 0); 3884 my_ret->v_objref.ref = opargs[0]; 3885 aml_addref(my_ret, "package"); 3886 } else if (my_ret->type == AML_OBJTYPE_OBJREF) { 3887 my_ret = my_ret->v_objref.ref; 3888 aml_addref(my_ret, "de-alias"); 3889 } 3890 if (ret_type == 'i' || ret_type == 't' || ret_type == 'T') { 3891 /* Return TermArg or Integer: Evaluate object */ 3892 my_ret = aml_eval(scope, my_ret, ret_type, 0, NULL); 3893 } else if (my_ret->type == AML_OBJTYPE_METHOD) { 3894 /* This should only happen with CondRef */ 3895 dnprintf(12,"non-termarg method : %s\n", stype); 3896 aml_addref(my_ret, "zoom"); 3897 } 3898 break; 3899 3900 case AMLOP_ZERO: 3901 case AMLOP_ONE: 3902 case AMLOP_ONES: 3903 case AMLOP_DEBUG: 3904 case AMLOP_REVISION: 3905 case AMLOP_BYTEPREFIX: 3906 case AMLOP_WORDPREFIX: 3907 case AMLOP_DWORDPREFIX: 3908 case AMLOP_QWORDPREFIX: 3909 case AMLOP_STRINGPREFIX: 3910 my_ret = opargs[0]; 3911 break; 3912 3913 case AMLOP_BUFFER: 3914 /* Buffer: iB => Buffer */ 3915 my_ret = aml_allocvalue(AML_OBJTYPE_BUFFER, 3916 opargs[0]->v_integer, NULL); 3917 memcpy(my_ret->v_buffer, opargs[1]->v_buffer, 3918 opargs[1]->length); 3919 break; 3920 case AMLOP_PACKAGE: 3921 case AMLOP_VARPACKAGE: 3922 /* Package/VarPackage: bT/iT => Package */ 3923 my_ret = aml_allocvalue(AML_OBJTYPE_PACKAGE, 3924 opargs[0]->v_integer, 0); 3925 mscope = aml_pushscope(scope, opargs[1], scope->node, 3926 AMLOP_PACKAGE); 3927 3928 /* Recursively parse package contents */ 3929 for (idx=0; idx<my_ret->length; idx++) { 3930 rv = aml_parse(mscope, 'o', "Package"); 3931 if (rv != NULL) { 3932 aml_delref(&my_ret->v_package[idx], "pkginit"); 3933 my_ret->v_package[idx] = rv; 3934 } 3935 } 3936 aml_popscope(mscope); 3937 mscope = NULL; 3938 break; 3939 3940 /* Math/Logical operations */ 3941 case AMLOP_OR: 3942 case AMLOP_ADD: 3943 case AMLOP_AND: 3944 case AMLOP_NAND: 3945 case AMLOP_XOR: 3946 case AMLOP_SHL: 3947 case AMLOP_SHR: 3948 case AMLOP_NOR: 3949 case AMLOP_MOD: 3950 case AMLOP_SUBTRACT: 3951 case AMLOP_MULTIPLY: 3952 /* XXX: iir => I */ 3953 ival = aml_evalexpr(opargs[0]->v_integer, 3954 opargs[1]->v_integer, opcode); 3955 aml_store(scope, opargs[2], ival, NULL); 3956 break; 3957 case AMLOP_DIVIDE: 3958 /* Divide: iirr => I */ 3959 if (opargs[1]->v_integer == 0) { 3960 my_ret = aml_seterror(scope, "Divide by Zero!"); 3961 break; 3962 } 3963 ival = aml_evalexpr(opargs[0]->v_integer, 3964 opargs[1]->v_integer, AMLOP_MOD); 3965 aml_store(scope, opargs[2], ival, NULL); 3966 3967 ival = aml_evalexpr(opargs[0]->v_integer, 3968 opargs[1]->v_integer, AMLOP_DIVIDE); 3969 aml_store(scope, opargs[3], ival, NULL); 3970 break; 3971 case AMLOP_NOT: 3972 case AMLOP_TOBCD: 3973 case AMLOP_FROMBCD: 3974 case AMLOP_FINDSETLEFTBIT: 3975 case AMLOP_FINDSETRIGHTBIT: 3976 /* XXX: ir => I */ 3977 ival = aml_evalexpr(opargs[0]->v_integer, 0, opcode); 3978 aml_store(scope, opargs[1], ival, NULL); 3979 break; 3980 case AMLOP_INCREMENT: 3981 case AMLOP_DECREMENT: 3982 /* Inc/Dec: S => I */ 3983 my_ret = aml_eval(scope, opargs[0], AML_ARG_INTEGER, 0, NULL); 3984 ival = aml_evalexpr(my_ret->v_integer, 1, opcode); 3985 aml_store(scope, opargs[0], ival, NULL); 3986 break; 3987 case AMLOP_LNOT: 3988 /* LNot: i => Bool */ 3989 ival = aml_evalexpr(opargs[0]->v_integer, 0, opcode); 3990 break; 3991 case AMLOP_LOR: 3992 case AMLOP_LAND: 3993 /* XXX: ii => Bool */ 3994 ival = aml_evalexpr(opargs[0]->v_integer, 3995 opargs[1]->v_integer, opcode); 3996 break; 3997 case AMLOP_LLESS: 3998 case AMLOP_LEQUAL: 3999 case AMLOP_LGREATER: 4000 case AMLOP_LNOTEQUAL: 4001 case AMLOP_LLESSEQUAL: 4002 case AMLOP_LGREATEREQUAL: 4003 /* XXX: tt => Bool */ 4004 ival = aml_compare(opargs[0], opargs[1], opcode); 4005 break; 4006 4007 /* Reference/Store operations */ 4008 case AMLOP_CONDREFOF: 4009 /* CondRef: rr => I */ 4010 ival = 0; 4011 if (opargs[0]->node != NULL) { 4012 /* Create Object Reference */ 4013 rv = aml_allocvalue(AML_OBJTYPE_OBJREF, opcode, 4014 opargs[0]); 4015 aml_addref(opargs[0], "CondRef"); 4016 aml_store(scope, opargs[1], 0, rv); 4017 aml_delref(&rv, 0); 4018 4019 /* Mark that we found it */ 4020 ival = -1; 4021 } 4022 break; 4023 case AMLOP_REFOF: 4024 /* RefOf: r => ObjRef */ 4025 my_ret = aml_allocvalue(AML_OBJTYPE_OBJREF, opcode, opargs[0]); 4026 aml_addref(my_ret->v_objref.ref, "RefOf"); 4027 break; 4028 case AMLOP_INDEX: 4029 /* Index: tir => ObjRef */ 4030 idx = opargs[1]->v_integer; 4031 /* Reading past the end of the array? - Ignore */ 4032 if (idx >= opargs[0]->length || idx < 0) 4033 break; 4034 switch (opargs[0]->type) { 4035 case AML_OBJTYPE_PACKAGE: 4036 /* Don't set opargs[0] to NULL */ 4037 if (ret_type == 't' || ret_type == 'i' || ret_type == 'T') { 4038 my_ret = opargs[0]->v_package[idx]; 4039 aml_addref(my_ret, "Index.Package"); 4040 } else { 4041 my_ret = aml_allocvalue(AML_OBJTYPE_OBJREF, AMLOP_PACKAGE, 4042 opargs[0]->v_package[idx]); 4043 aml_addref(my_ret->v_objref.ref, 4044 "Index.Package"); 4045 } 4046 break; 4047 case AML_OBJTYPE_BUFFER: 4048 case AML_OBJTYPE_STRING: 4049 case AML_OBJTYPE_INTEGER: 4050 rv = aml_convert(opargs[0], AML_OBJTYPE_BUFFER, -1); 4051 if (ret_type == 't' || ret_type == 'i' || ret_type == 'T') { 4052 dnprintf(12,"Index.Buf Term: %d = %x\n", 4053 idx, rv->v_buffer[idx]); 4054 ival = rv->v_buffer[idx]; 4055 } else { 4056 dnprintf(12, "Index.Buf Targ\n"); 4057 my_ret = aml_allocvalue(0,0,NULL); 4058 aml_createfield(my_ret, AMLOP_INDEX, rv, 4059 8 * idx, 8, NULL, 0, AML_FIELD_BYTEACC); 4060 } 4061 aml_delref(&rv, "Index.BufStr"); 4062 break; 4063 default: 4064 aml_die("Unknown index : %x\n", opargs[0]->type); 4065 break; 4066 } 4067 aml_store(scope, opargs[2], ival, my_ret); 4068 break; 4069 case AMLOP_DEREFOF: 4070 /* DerefOf: t:ObjRef => DataRefObj */ 4071 if (opargs[0]->type == AML_OBJTYPE_OBJREF) { 4072 my_ret = opargs[0]->v_objref.ref; 4073 aml_addref(my_ret, "DerefOf"); 4074 } else { 4075 my_ret = opargs[0]; 4076 //aml_addref(my_ret, "DerefOf"); 4077 } 4078 break; 4079 case AMLOP_COPYOBJECT: 4080 /* CopyObject: t:DataRefObj, s:implename => DataRefObj */ 4081 my_ret = opargs[0]; 4082 aml_freevalue(opargs[1]); 4083 aml_copyvalue(opargs[1], opargs[0]); 4084 break; 4085 case AMLOP_STORE: 4086 /* Store: t:DataRefObj, S:upername => DataRefObj */ 4087 my_ret = opargs[0]; 4088 aml_store(scope, opargs[1], 0, opargs[0]); 4089 break; 4090 4091 /* Conversion */ 4092 case AMLOP_TOINTEGER: 4093 /* Source:CData, Result => Integer */ 4094 my_ret = aml_convert(opargs[0], AML_OBJTYPE_INTEGER, -1); 4095 aml_store(scope, opargs[1], 0, my_ret); 4096 break; 4097 case AMLOP_TOBUFFER: 4098 /* Source:CData, Result => Buffer */ 4099 my_ret = aml_convert(opargs[0], AML_OBJTYPE_BUFFER, -1); 4100 aml_store(scope, opargs[1], 0, my_ret); 4101 break; 4102 case AMLOP_TOHEXSTRING: 4103 /* Source:CData, Result => String */ 4104 my_ret = aml_convert(opargs[0], AML_OBJTYPE_HEXSTRING, -1); 4105 aml_store(scope, opargs[1], 0, my_ret); 4106 break; 4107 case AMLOP_TODECSTRING: 4108 /* Source:CData, Result => String */ 4109 my_ret = aml_convert(opargs[0], AML_OBJTYPE_DECSTRING, -1); 4110 aml_store(scope, opargs[1], 0, my_ret); 4111 break; 4112 case AMLOP_TOSTRING: 4113 /* Source:B, Length:I, Result => String */ 4114 my_ret = aml_convert(opargs[0], AML_OBJTYPE_STRING, 4115 opargs[1]->v_integer); 4116 aml_store(scope, opargs[2], 0, my_ret); 4117 break; 4118 case AMLOP_CONCAT: 4119 /* Source1:CData, Source2:CData, Result => CData */ 4120 my_ret = aml_concat(opargs[0], opargs[1]); 4121 aml_store(scope, opargs[2], 0, my_ret); 4122 break; 4123 case AMLOP_CONCATRES: 4124 /* Concat two resource buffers: buf1, buf2, result => Buffer */ 4125 my_ret = aml_concatres(opargs[0], opargs[1]); 4126 aml_store(scope, opargs[2], 0, my_ret); 4127 break; 4128 case AMLOP_MID: 4129 /* Source:BS, Index:I, Length:I, Result => BS */ 4130 my_ret = aml_mid(opargs[0], opargs[1]->v_integer, 4131 opargs[2]->v_integer); 4132 aml_store(scope, opargs[3], 0, my_ret); 4133 break; 4134 case AMLOP_MATCH: 4135 /* Match: Pkg, Op1, Val1, Op2, Val2, Index */ 4136 ival = aml_match(opargs[0], opargs[5]->v_integer, 4137 opargs[1]->v_integer, opargs[2]->v_integer, 4138 opargs[3]->v_integer, opargs[4]->v_integer); 4139 break; 4140 case AMLOP_SIZEOF: 4141 /* Sizeof: S => i */ 4142 rv = aml_gettgt(opargs[0], opcode); 4143 ival = rv->length; 4144 break; 4145 case AMLOP_OBJECTTYPE: 4146 /* ObjectType: S => i */ 4147 rv = aml_gettgt(opargs[0], opcode); 4148 ival = rv->type; 4149 break; 4150 4151 /* Mutex/Event handlers */ 4152 case AMLOP_ACQUIRE: 4153 /* Acquire: Sw => Bool */ 4154 rv = aml_gettgt(opargs[0], opcode); 4155 ival = acpi_mutex_acquire(scope, rv, 4156 opargs[1]->v_integer); 4157 break; 4158 case AMLOP_RELEASE: 4159 /* Release: S */ 4160 rv = aml_gettgt(opargs[0], opcode); 4161 acpi_mutex_release(scope, rv); 4162 break; 4163 case AMLOP_WAIT: 4164 /* Wait: Si => Bool */ 4165 rv = aml_gettgt(opargs[0], opcode); 4166 ival = acpi_event_wait(scope, rv, 4167 opargs[1]->v_integer); 4168 break; 4169 case AMLOP_RESET: 4170 /* Reset: S */ 4171 rv = aml_gettgt(opargs[0], opcode); 4172 acpi_event_reset(scope, rv); 4173 break; 4174 case AMLOP_SIGNAL: 4175 /* Signal: S */ 4176 rv = aml_gettgt(opargs[0], opcode); 4177 acpi_event_signal(scope, rv); 4178 break; 4179 4180 /* Named objects */ 4181 case AMLOP_NAME: 4182 /* Name: Nt */ 4183 rv = opargs[0]; 4184 aml_freevalue(rv); 4185 aml_copyvalue(rv, opargs[1]); 4186 break; 4187 case AMLOP_ALIAS: 4188 /* Alias: nN */ 4189 rv = _aml_setvalue(opargs[1], AML_OBJTYPE_OBJREF, opcode, 0); 4190 rv->v_objref.ref = aml_gettgt(opargs[0], opcode); 4191 aml_addref(rv->v_objref.ref, "Alias"); 4192 break; 4193 case AMLOP_OPREGION: 4194 /* OpRegion: Nbii */ 4195 rv = _aml_setvalue(opargs[0], AML_OBJTYPE_OPREGION, 0, 0); 4196 rv->v_opregion.iospace = opargs[1]->v_integer; 4197 rv->v_opregion.iobase = opargs[2]->v_integer; 4198 rv->v_opregion.iolen = opargs[3]->v_integer; 4199 rv->v_opregion.flag = 0; 4200 break; 4201 case AMLOP_DATAREGION: 4202 /* DataTableRegion: N,t:SigStr,t:OemIDStr,t:OemTableIDStr */ 4203 rv = _aml_setvalue(opargs[0], AML_OBJTYPE_OPREGION, 0, 0); 4204 rv->v_opregion.iospace = GAS_SYSTEM_MEMORY; 4205 rv->v_opregion.iobase = 0; 4206 rv->v_opregion.iolen = 0; 4207 aml_die("AML-DataTableRegion\n"); 4208 break; 4209 case AMLOP_EVENT: 4210 /* Event: N */ 4211 rv = _aml_setvalue(opargs[0], AML_OBJTYPE_EVENT, 0, 0); 4212 rv->v_evt.state = 0; 4213 break; 4214 case AMLOP_MUTEX: 4215 /* Mutex: Nw */ 4216 rv = _aml_setvalue(opargs[0], AML_OBJTYPE_MUTEX, 0, 0); 4217 rv->v_mtx.synclvl = opargs[1]->v_integer; 4218 break; 4219 case AMLOP_SCOPE: 4220 /* Scope: NT */ 4221 rv = opargs[0]; 4222 if (rv->type == AML_OBJTYPE_NAMEREF) { 4223 printf("Undefined scope: %s\n", aml_getname(rv->v_nameref)); 4224 break; 4225 } 4226 mscope = aml_pushscope(scope, opargs[1], rv->node, opcode); 4227 break; 4228 case AMLOP_DEVICE: 4229 /* Device: NT */ 4230 rv = _aml_setvalue(opargs[0], AML_OBJTYPE_DEVICE, 0, 0); 4231 mscope = aml_pushscope(scope, opargs[1], rv->node, opcode); 4232 break; 4233 case AMLOP_THERMALZONE: 4234 /* ThermalZone: NT */ 4235 rv = _aml_setvalue(opargs[0], AML_OBJTYPE_THERMZONE, 0, 0); 4236 mscope = aml_pushscope(scope, opargs[1], rv->node, opcode); 4237 break; 4238 case AMLOP_POWERRSRC: 4239 /* PowerRsrc: NbwT */ 4240 rv = _aml_setvalue(opargs[0], AML_OBJTYPE_POWERRSRC, 0, 0); 4241 rv->v_powerrsrc.pwr_level = opargs[1]->v_integer; 4242 rv->v_powerrsrc.pwr_order = opargs[2]->v_integer; 4243 mscope = aml_pushscope(scope, opargs[3], rv->node, opcode); 4244 break; 4245 case AMLOP_PROCESSOR: 4246 /* Processor: NbdbT */ 4247 rv = _aml_setvalue(opargs[0], AML_OBJTYPE_PROCESSOR, 0, 0); 4248 rv->v_processor.proc_id = opargs[1]->v_integer; 4249 rv->v_processor.proc_addr = opargs[2]->v_integer; 4250 rv->v_processor.proc_len = opargs[3]->v_integer; 4251 mscope = aml_pushscope(scope, opargs[4], rv->node, opcode); 4252 break; 4253 case AMLOP_METHOD: 4254 /* Method: NbM */ 4255 rv = _aml_setvalue(opargs[0], AML_OBJTYPE_METHOD, 0, 0); 4256 rv->v_method.flags = opargs[1]->v_integer; 4257 rv->v_method.start = opargs[2]->v_buffer; 4258 rv->v_method.end = rv->v_method.start + opargs[2]->length; 4259 rv->v_method.base = aml_root.start; 4260 break; 4261 4262 /* Field objects */ 4263 case AMLOP_CREATEFIELD: 4264 /* Source:B, BitIndex:I, NumBits:I, FieldName */ 4265 rv = _aml_setvalue(opargs[3], AML_OBJTYPE_BUFFERFIELD, 0, 0); 4266 aml_createfield(rv, opcode, opargs[0], opargs[1]->v_integer, 4267 opargs[2]->v_integer, NULL, 0, 0); 4268 break; 4269 case AMLOP_CREATEBITFIELD: 4270 /* Source:B, BitIndex:I, FieldName */ 4271 rv = _aml_setvalue(opargs[2], AML_OBJTYPE_BUFFERFIELD, 0, 0); 4272 aml_createfield(rv, opcode, opargs[0], opargs[1]->v_integer, 4273 1, NULL, 0, 0); 4274 break; 4275 case AMLOP_CREATEBYTEFIELD: 4276 /* Source:B, ByteIndex:I, FieldName */ 4277 rv = _aml_setvalue(opargs[2], AML_OBJTYPE_BUFFERFIELD, 0, 0); 4278 aml_createfield(rv, opcode, opargs[0], opargs[1]->v_integer*8, 4279 8, NULL, 0, AML_FIELD_BYTEACC); 4280 break; 4281 case AMLOP_CREATEWORDFIELD: 4282 /* Source:B, ByteIndex:I, FieldName */ 4283 rv = _aml_setvalue(opargs[2], AML_OBJTYPE_BUFFERFIELD, 0, 0); 4284 aml_createfield(rv, opcode, opargs[0], opargs[1]->v_integer*8, 4285 16, NULL, 0, AML_FIELD_WORDACC); 4286 break; 4287 case AMLOP_CREATEDWORDFIELD: 4288 /* Source:B, ByteIndex:I, FieldName */ 4289 rv = _aml_setvalue(opargs[2], AML_OBJTYPE_BUFFERFIELD, 0, 0); 4290 aml_createfield(rv, opcode, opargs[0], opargs[1]->v_integer*8, 4291 32, NULL, 0, AML_FIELD_DWORDACC); 4292 break; 4293 case AMLOP_CREATEQWORDFIELD: 4294 /* Source:B, ByteIndex:I, FieldName */ 4295 rv = _aml_setvalue(opargs[2], AML_OBJTYPE_BUFFERFIELD, 0, 0); 4296 aml_createfield(rv, opcode, opargs[0], opargs[1]->v_integer*8, 4297 64, NULL, 0, AML_FIELD_QWORDACC); 4298 break; 4299 case AMLOP_FIELD: 4300 /* Field: n:OpRegion, b:Flags, F:ieldlist */ 4301 mscope = aml_pushscope(scope, opargs[2], scope->node, opcode); 4302 aml_parsefieldlist(mscope, opcode, opargs[1]->v_integer, 4303 opargs[0], NULL, 0); 4304 mscope = NULL; 4305 break; 4306 case AMLOP_INDEXFIELD: 4307 /* IndexField: n:Index, n:Data, b:Flags, F:ieldlist */ 4308 mscope = aml_pushscope(scope, opargs[3], scope->node, opcode); 4309 aml_parsefieldlist(mscope, opcode, opargs[2]->v_integer, 4310 opargs[1], opargs[0], 0); 4311 mscope = NULL; 4312 break; 4313 case AMLOP_BANKFIELD: 4314 /* BankField: n:OpRegion, n:Field, i:Bank, b:Flags, F:ieldlist */ 4315 mscope = aml_pushscope(scope, opargs[4], scope->node, opcode); 4316 aml_parsefieldlist(mscope, opcode, opargs[3]->v_integer, 4317 opargs[0], opargs[1], opargs[2]->v_integer); 4318 mscope = NULL; 4319 break; 4320 4321 /* Misc functions */ 4322 case AMLOP_STALL: 4323 /* Stall: i */ 4324 acpi_stall(opargs[0]->v_integer); 4325 break; 4326 case AMLOP_SLEEP: 4327 /* Sleep: i */ 4328 acpi_sleep(opargs[0]->v_integer, "amlsleep"); 4329 break; 4330 case AMLOP_NOTIFY: 4331 /* Notify: Si */ 4332 rv = aml_gettgt(opargs[0], opcode); 4333 dnprintf(50,"Notifying: %s %llx\n", 4334 aml_nodename(rv->node), 4335 opargs[1]->v_integer); 4336 aml_notify(rv->node, opargs[1]->v_integer); 4337 break; 4338 case AMLOP_TIMER: 4339 /* Timer: => i */ 4340 nanouptime(&ts); 4341 ival = ts.tv_sec * 10000000 + ts.tv_nsec / 100; 4342 break; 4343 case AMLOP_FATAL: 4344 /* Fatal: bdi */ 4345 aml_die("AML FATAL ERROR: %x,%x,%x\n", 4346 opargs[0]->v_integer, opargs[1]->v_integer, 4347 opargs[2]->v_integer); 4348 break; 4349 case AMLOP_LOADTABLE: 4350 /* LoadTable(Sig:Str, OEMID:Str, OEMTable:Str, [RootPath:Str], [ParmPath:Str], 4351 [ParmData:DataRefObj]) => DDBHandle */ 4352 my_ret = aml_loadtable(acpi_softc, opargs[0]->v_string, 4353 opargs[1]->v_string, opargs[2]->v_string, 4354 opargs[3]->v_string, opargs[4]->v_string, opargs[5]); 4355 break; 4356 case AMLOP_LOAD: 4357 /* Load(Object:NameString, DDBHandle:SuperName) */ 4358 mscope = aml_load(acpi_softc, scope, opargs[0], opargs[1]); 4359 break; 4360 case AMLOP_UNLOAD: 4361 /* DDBHandle */ 4362 aml_die("Unload"); 4363 break; 4364 4365 /* Control Flow */ 4366 case AMLOP_IF: 4367 /* Arguments: iT or iTbT */ 4368 if (opargs[0]->v_integer) { 4369 dnprintf(10,"parse-if @ %.4x\n", pc); 4370 mscope = aml_pushscope(scope, opargs[1], scope->node, 4371 AMLOP_IF); 4372 } else if (opargs[3] != NULL) { 4373 dnprintf(10,"parse-else @ %.4x\n", pc); 4374 mscope = aml_pushscope(scope, opargs[3], scope->node, 4375 AMLOP_ELSE); 4376 } 4377 break; 4378 case AMLOP_WHILE: 4379 if (opargs[0]->v_integer) { 4380 /* Set parent position to start of WHILE */ 4381 scope->pos = start; 4382 mscope = aml_pushscope(scope, opargs[1], scope->node, 4383 AMLOP_WHILE); 4384 } 4385 break; 4386 case AMLOP_BREAK: 4387 /* Break: Find While Scope parent, mark type as null */ 4388 aml_findscope(scope, AMLOP_WHILE, AMLOP_BREAK); 4389 break; 4390 case AMLOP_CONTINUE: 4391 /* Find Scope.. mark all objects as invalid on way to root */ 4392 aml_findscope(scope, AMLOP_WHILE, AMLOP_CONTINUE); 4393 break; 4394 case AMLOP_RETURN: 4395 mscope = aml_findscope(scope, AMLOP_METHOD, AMLOP_RETURN); 4396 if (mscope->retv) { 4397 aml_die("already allocated\n"); 4398 } 4399 mscope->retv = aml_allocvalue(0,0,NULL); 4400 aml_copyvalue(mscope->retv, opargs[0]); 4401 mscope = NULL; 4402 break; 4403 default: 4404 /* may be set direct result */ 4405 aml_die("Unknown opcode: %x:%s\n", opcode, htab->mnem); 4406 break; 4407 } 4408 if (mscope != NULL) { 4409 /* Change our scope to new scope */ 4410 scope = mscope; 4411 } 4412 if ((ret_type == 'i' || ret_type == 't') && my_ret == NULL) { 4413 dnprintf(10,"quick: %.4x [%s] alloc return integer = 0x%llx\n", 4414 pc, htab->mnem, ival); 4415 my_ret = aml_allocvalue(AML_OBJTYPE_INTEGER, ival, NULL); 4416 } 4417 if (ret_type == 'i' && my_ret && my_ret->type != AML_OBJTYPE_INTEGER) { 4418 dnprintf(10,"quick: %.4x convert to integer %s -> %s\n", 4419 pc, htab->mnem, stype); 4420 my_ret = aml_convert(my_ret, AML_OBJTYPE_INTEGER, -1); 4421 } 4422 if (my_ret != NULL) { 4423 /* Display result */ 4424 dnprintf(20,"quick: %.4x %18s %c %.4x\n", pc, stype, 4425 ret_type, my_ret->stack); 4426 } 4427 4428 /* End opcode: display/free arguments */ 4429 parse_error: 4430 for (idx=0; idx<8; idx++) { 4431 if (opargs[idx] == my_ret) 4432 opargs[idx] = NULL; 4433 aml_delref(&opargs[idx], "oparg"); 4434 } 4435 4436 /* If parsing whole scope and not done, start again */ 4437 if (ret_type == 'T') { 4438 aml_delref(&my_ret, "scope.loop"); 4439 while (scope->pos >= scope->end && scope != iscope) { 4440 /* Pop intermediate scope */ 4441 scope = aml_popscope(scope); 4442 } 4443 if (scope->pos && scope->pos < scope->end) 4444 goto start; 4445 } 4446 4447 odp--; 4448 dnprintf(50, ">>return [%s] %s %c %p\n", aml_nodename(scope->node), 4449 stype, ret_type, my_ret); 4450 4451 return my_ret; 4452 } 4453 4454 int 4455 acpi_parse_aml(struct acpi_softc *sc, uint8_t *start, uint32_t length) 4456 { 4457 struct aml_scope *scope; 4458 struct aml_value res; 4459 4460 aml_root.start = start; 4461 memset(&res, 0, sizeof(res)); 4462 res.type = AML_OBJTYPE_SCOPE; 4463 res.length = length; 4464 res.v_buffer = start; 4465 4466 /* Push toplevel scope, parse AML */ 4467 aml_error = 0; 4468 scope = aml_pushscope(NULL, &res, &aml_root, AMLOP_SCOPE); 4469 aml_busy++; 4470 aml_parse(scope, 'T', "TopLevel"); 4471 aml_busy--; 4472 aml_popscope(scope); 4473 4474 if (aml_error) { 4475 printf("error in acpi_parse_aml\n"); 4476 return -1; 4477 } 4478 return (0); 4479 } 4480 4481 /* 4482 * @@@: External API 4483 * 4484 * evaluate an AML node 4485 * Returns a copy of the value in res (must be freed by user) 4486 */ 4487 int 4488 aml_evalnode(struct acpi_softc *sc, struct aml_node *node, 4489 int argc, struct aml_value *argv, struct aml_value *res) 4490 { 4491 struct aml_value *xres; 4492 4493 if (res) 4494 memset(res, 0, sizeof(*res)); 4495 if (node == NULL || node->value == NULL) 4496 return (ACPI_E_BADVALUE); 4497 dnprintf(12,"EVALNODE: %s %lx\n", aml_nodename(node), acpi_nalloc); 4498 4499 aml_error = 0; 4500 xres = aml_eval(NULL, node->value, 't', argc, argv); 4501 if (xres) { 4502 if (res) 4503 aml_copyvalue(res, xres); 4504 if (xres != node->value) 4505 aml_delref(&xres, "evalnode"); 4506 } 4507 if (aml_error) { 4508 printf("error evaluating: %s\n", aml_nodename(node)); 4509 return (-1); 4510 } 4511 return (0); 4512 } 4513 4514 int 4515 aml_node_setval(struct acpi_softc *sc, struct aml_node *node, int64_t val) 4516 { 4517 struct aml_value env; 4518 4519 if (!node) 4520 return (0); 4521 4522 memset(&env, 0, sizeof(env)); 4523 env.type = AML_OBJTYPE_INTEGER; 4524 env.v_integer = val; 4525 4526 return aml_evalnode(sc, node, 1, &env, NULL); 4527 } 4528 4529 /* 4530 * evaluate an AML name 4531 * Returns a copy of the value in res (must be freed by user) 4532 */ 4533 int 4534 aml_evalname(struct acpi_softc *sc, struct aml_node *parent, const char *name, 4535 int argc, struct aml_value *argv, struct aml_value *res) 4536 { 4537 parent = aml_searchname(parent, name); 4538 return aml_evalnode(sc, parent, argc, argv, res); 4539 } 4540 4541 /* 4542 * evaluate an AML integer object 4543 */ 4544 int 4545 aml_evalinteger(struct acpi_softc *sc, struct aml_node *parent, 4546 const char *name, int argc, struct aml_value *argv, int64_t *ival) 4547 { 4548 struct aml_value res; 4549 int rc; 4550 4551 parent = aml_searchname(parent, name); 4552 rc = aml_evalnode(sc, parent, argc, argv, &res); 4553 if (rc == 0) { 4554 *ival = aml_val2int(&res); 4555 aml_freevalue(&res); 4556 } 4557 return rc; 4558 } 4559 4560 /* 4561 * Search for an AML name in namespace.. root only 4562 */ 4563 struct aml_node * 4564 __aml_searchname(struct aml_node *root, const void *vname, int create) 4565 { 4566 char *name = (char *)vname; 4567 char nseg[AML_NAMESEG_LEN + 1]; 4568 int i; 4569 4570 dnprintf(25,"Searchname: %s:%s = ", aml_nodename(root), name); 4571 while (*name == AMLOP_ROOTCHAR) { 4572 root = &aml_root; 4573 name++; 4574 } 4575 while (*name != 0) { 4576 /* Ugh.. we can have short names here: append '_' */ 4577 strlcpy(nseg, "____", sizeof(nseg)); 4578 for (i=0; i < AML_NAMESEG_LEN && *name && *name != '.'; i++) 4579 nseg[i] = *name++; 4580 if (*name == '.') 4581 name++; 4582 root = __aml_search(root, nseg, create); 4583 } 4584 dnprintf(25,"%p %s\n", root, aml_nodename(root)); 4585 return root; 4586 } 4587 4588 struct aml_node * 4589 aml_searchname(struct aml_node *root, const void *vname) 4590 { 4591 return __aml_searchname(root, vname, 0); 4592 } 4593 4594 /* 4595 * Search for relative name 4596 */ 4597 struct aml_node * 4598 aml_searchrel(struct aml_node *root, const void *vname) 4599 { 4600 struct aml_node *res; 4601 4602 while (root) { 4603 res = aml_searchname(root, vname); 4604 if (res != NULL) 4605 return res; 4606 root = root->parent; 4607 } 4608 return NULL; 4609 } 4610 4611 #ifndef SMALL_KERNEL 4612 4613 void 4614 acpi_getdevlist(struct acpi_devlist_head *list, struct aml_node *root, 4615 struct aml_value *pkg, int off) 4616 { 4617 struct acpi_devlist *dl; 4618 struct aml_value *val; 4619 int idx; 4620 4621 for (idx = off; idx < pkg->length; idx++) { 4622 val = pkg->v_package[idx]; 4623 if (val->type == AML_OBJTYPE_OBJREF) 4624 val = val->v_objref.ref; 4625 if (val->node) { 4626 dl = acpi_os_malloc(sizeof(*dl)); 4627 if (dl) { 4628 dl->dev_node = val->node; 4629 TAILQ_INSERT_TAIL(list, dl, dev_link); 4630 } 4631 } 4632 } 4633 } 4634 4635 void 4636 acpi_freedevlist(struct acpi_devlist_head *list) 4637 { 4638 struct acpi_devlist *dl; 4639 4640 while ((dl = TAILQ_FIRST(list)) != NULL) { 4641 TAILQ_REMOVE(list, dl, dev_link); 4642 acpi_os_free(dl); 4643 } 4644 } 4645 4646 #endif /* SMALL_KERNEL */ 4647