127/* 128 * Register a power resource. 129 * 130 * It's OK to call this if we already know about the resource. 131 */ 132static ACPI_STATUS 133acpi_pwr_register_resource(ACPI_HANDLE res) 134{ 135 ACPI_STATUS status; 136 ACPI_BUFFER buf; 137 ACPI_OBJECT *obj; 138 struct acpi_powerresource *rp, *srp; 139 140 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 141 ACPI_SERIAL_ASSERT(powerres); 142 143 rp = NULL; 144 buf.Pointer = NULL; 145 146 /* Look to see if we know about this resource */ 147 if (acpi_pwr_find_resource(res) != NULL) 148 return_ACPI_STATUS (AE_OK); /* already know about it */ 149 150 /* Allocate a new resource */ 151 if ((rp = malloc(sizeof(*rp), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 152 status = AE_NO_MEMORY; 153 goto out; 154 } 155 TAILQ_INIT(&rp->ap_references); 156 rp->ap_resource = res; 157 158 /* Get the Power Resource object */ 159 buf.Length = ACPI_ALLOCATE_BUFFER; 160 if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) { 161 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n")); 162 goto out; 163 } 164 obj = buf.Pointer; 165 if (obj->Type != ACPI_TYPE_POWER) { 166 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 167 "questionable power resource object %s\n", 168 acpi_name(res))); 169 status = AE_TYPE; 170 goto out; 171 } 172 rp->ap_systemlevel = obj->PowerResource.SystemLevel; 173 rp->ap_order = obj->PowerResource.ResourceOrder; 174 175 /* Sort the resource into the list */ 176 status = AE_OK; 177 srp = TAILQ_FIRST(&acpi_powerresources); 178 if (srp == NULL || rp->ap_order < srp->ap_order) { 179 TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link); 180 goto done; 181 } 182 TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) { 183 if (rp->ap_order < srp->ap_order) { 184 TAILQ_INSERT_BEFORE(srp, rp, ap_link); 185 goto done; 186 } 187 } 188 TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link); 189 190 done: 191 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 192 "registered power resource %s\n", acpi_name(res))); 193 194 out: 195 if (buf.Pointer != NULL) 196 AcpiOsFree(buf.Pointer); 197 if (ACPI_FAILURE(status) && rp != NULL) 198 free(rp, M_ACPIPWR); 199 return_ACPI_STATUS (status); 200} 201 202#ifdef notyet 203/* 204 * Deregister a power resource. 205 */ 206static ACPI_STATUS 207acpi_pwr_deregister_resource(ACPI_HANDLE res) 208{ 209 struct acpi_powerresource *rp; 210 211 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 212 ACPI_SERIAL_ASSERT(powerres); 213 214 rp = NULL; 215 216 /* Find the resource */ 217 if ((rp = acpi_pwr_find_resource(res)) == NULL) 218 return_ACPI_STATUS (AE_BAD_PARAMETER); 219 220 /* Check that there are no consumers referencing this resource */ 221 if (TAILQ_FIRST(&rp->ap_references) != NULL) 222 return_ACPI_STATUS (AE_BAD_PARAMETER); 223 224 /* Pull it off the list and free it */ 225 TAILQ_REMOVE(&acpi_powerresources, rp, ap_link); 226 free(rp, M_ACPIPWR); 227 228 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n", 229 acpi_name(res))); 230 231 return_ACPI_STATUS (AE_OK); 232} 233#endif /* notyet */ 234 235/* 236 * Register a power consumer. 237 * 238 * It's OK to call this if we already know about the consumer. 239 */ 240static ACPI_STATUS 241acpi_pwr_register_consumer(ACPI_HANDLE consumer) 242{ 243 struct acpi_powerconsumer *pc; 244 245 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 246 ACPI_SERIAL_ASSERT(powerres); 247 248 /* Check to see whether we know about this consumer already */ 249 if (acpi_pwr_find_consumer(consumer) != NULL) 250 return_ACPI_STATUS (AE_OK); 251 252 /* Allocate a new power consumer */ 253 if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL) 254 return_ACPI_STATUS (AE_NO_MEMORY); 255 TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link); 256 TAILQ_INIT(&pc->ac_references); 257 pc->ac_consumer = consumer; 258 259 /* XXX we should try to find its current state */ 260 pc->ac_state = ACPI_STATE_UNKNOWN; 261 262 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n", 263 acpi_name(consumer))); 264 265 return_ACPI_STATUS (AE_OK); 266} 267 268#ifdef notyet 269/* 270 * Deregister a power consumer. 271 * 272 * This should only be done once the consumer has been powered off. 273 * (XXX is this correct? Check once implemented) 274 */ 275static ACPI_STATUS 276acpi_pwr_deregister_consumer(ACPI_HANDLE consumer) 277{ 278 struct acpi_powerconsumer *pc; 279 280 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 281 ACPI_SERIAL_ASSERT(powerres); 282 283 /* Find the consumer */ 284 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 285 return_ACPI_STATUS (AE_BAD_PARAMETER); 286 287 /* Make sure the consumer's not referencing anything right now */ 288 if (TAILQ_FIRST(&pc->ac_references) != NULL) 289 return_ACPI_STATUS (AE_BAD_PARAMETER); 290 291 /* Pull the consumer off the list and free it */ 292 TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link); 293 free(pc, M_ACPIPWR); 294 295 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n", 296 acpi_name(consumer))); 297 298 return_ACPI_STATUS (AE_OK); 299} 300#endif /* notyet */ 301 302/* 303 * Set a power consumer to a particular power state. 304 */ 305ACPI_STATUS 306acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state) 307{ 308 struct acpi_powerconsumer *pc; 309 ACPI_HANDLE method_handle, reslist_handle, pr0_handle; 310 ACPI_BUFFER reslist_buffer; 311 ACPI_OBJECT *reslist_object; 312 ACPI_STATUS status; 313 char *method_name, *reslist_name; 314 315 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 316 317 /* It's never ok to switch a non-existent consumer. */ 318 if (consumer == NULL) 319 return_ACPI_STATUS (AE_NOT_FOUND); 320 reslist_buffer.Pointer = NULL; 321 reslist_object = NULL; 322 ACPI_SERIAL_BEGIN(powerres); 323 324 /* Find the consumer */ 325 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 326 if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) 327 goto out; 328 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 329 panic("acpi added power consumer but can't find it"); 330 } 331 332 /* Check for valid transitions. We can only go to D0 from D3. */ 333 status = AE_BAD_PARAMETER; 334 if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0) 335 goto out; 336 337 /* Find transition mechanism(s) */ 338 switch (state) { 339 case ACPI_STATE_D0: 340 method_name = "_PS0"; 341 reslist_name = "_PR0"; 342 break; 343 case ACPI_STATE_D1: 344 method_name = "_PS1"; 345 reslist_name = "_PR1"; 346 break; 347 case ACPI_STATE_D2: 348 method_name = "_PS2"; 349 reslist_name = "_PR2"; 350 break; 351 case ACPI_STATE_D3: 352 method_name = "_PS3"; 353 reslist_name = "_PR3"; 354 break; 355 default: 356 goto out; 357 } 358 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n", 359 acpi_name(consumer), pc->ac_state, state)); 360 361 /* 362 * Verify that this state is supported, ie. one of method or 363 * reslist must be present. We need to do this before we go 364 * dereferencing resources (since we might be trying to go to 365 * a state we don't support). 366 * 367 * Note that if any states are supported, the device has to 368 * support D0 and D3. It's never an error to try to go to 369 * D0. 370 */ 371 if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle))) 372 method_handle = NULL; 373 if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle))) 374 reslist_handle = NULL; 375 if (reslist_handle == NULL && method_handle == NULL) { 376 if (state == ACPI_STATE_D0) { 377 pc->ac_state = ACPI_STATE_D0; 378 status = AE_OK; 379 goto out; 380 } 381 if (state != ACPI_STATE_D3) { 382 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 383 "attempt to set unsupported state D%d\n", state)); 384 goto out; 385 } 386 387 /* 388 * Turn off the resources listed in _PR0 to go to D3. If there is 389 * no _PR0 method, this object doesn't support ACPI power states. 390 */ 391 if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle))) { 392 status = AE_NOT_FOUND; 393 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 394 "device missing _PR0 (desired state was D%d)\n", state)); 395 goto out; 396 } 397 reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 398 status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer); 399 if (ACPI_FAILURE(status)) { 400 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 401 "can't evaluate _PR0 for device %s, state D%d\n", 402 acpi_name(consumer), state)); 403 goto out; 404 } 405 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 406 if (!ACPI_PKG_VALID(reslist_object, 1)) { 407 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 408 "invalid package object for state D%d\n", state)); 409 status = AE_TYPE; 410 goto out; 411 } 412 AcpiOsFree(reslist_buffer.Pointer); 413 reslist_buffer.Pointer = NULL; 414 reslist_object = NULL; 415 } 416 417 /* 418 * Check that we can actually fetch the list of power resources 419 */ 420 if (reslist_handle != NULL) { 421 reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 422 status = AcpiEvaluateObject(reslist_handle, NULL, NULL, 423 &reslist_buffer); 424 if (ACPI_FAILURE(status)) { 425 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 426 "can't evaluate resource list %s\n", 427 acpi_name(reslist_handle))); 428 goto out; 429 } 430 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 431 if (reslist_object->Type != ACPI_TYPE_PACKAGE) { 432 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 433 "resource list is not ACPI_TYPE_PACKAGE (%d)\n", 434 reslist_object->Type)); 435 status = AE_TYPE; 436 goto out; 437 } 438 } 439 440 /* 441 * Now we are ready to switch, so kill off any current power 442 * resource references. 443 */ 444 acpi_pwr_dereference_resource(pc); 445 446 /* 447 * Add new power resource references, if we have any. Traverse the 448 * package that we got from evaluating reslist_handle, and look up each 449 * of the resources that are referenced. 450 */ 451 if (reslist_object != NULL) { 452 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n", 453 reslist_object->Package.Count)); 454 acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource, 455 pc); 456 } 457 458 /* 459 * If we changed anything in the resource list, we need to run a switch 460 * pass now. 461 */ 462 if (ACPI_FAILURE(status = acpi_pwr_switch_power())) { 463 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 464 "failed to switch resources from %s to D%d\n", 465 acpi_name(consumer), state)); 466 467 /* XXX is this appropriate? Should we return to previous state? */ 468 goto out; 469 } 470 471 /* Invoke power state switch method (if present) */ 472 if (method_handle != NULL) { 473 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 474 "invoking state transition method %s\n", 475 acpi_name(method_handle))); 476 status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL); 477 if (ACPI_FAILURE(status)) { 478 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n", 479 AcpiFormatException(status))); 480 pc->ac_state = ACPI_STATE_UNKNOWN; 481 482 /* XXX Should we return to previous state? */ 483 goto out; 484 } 485 } 486 487 /* Transition was successful */ 488 pc->ac_state = state; 489 status = AE_OK; 490 491out: 492 ACPI_SERIAL_END(powerres); 493 if (reslist_buffer.Pointer != NULL) 494 AcpiOsFree(reslist_buffer.Pointer); 495 return_ACPI_STATUS (status); 496} 497 498/* Enable or disable a power resource for wake */ 499ACPI_STATUS 500acpi_pwr_wake_enable(ACPI_HANDLE consumer, int enable) 501{ 502 ACPI_STATUS status; 503 struct acpi_powerconsumer *pc; 504 struct acpi_prw_data prw; 505 int i; 506 507 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 508 509 if (consumer == NULL) 510 return (AE_BAD_PARAMETER); 511 512 ACPI_SERIAL_BEGIN(powerres); 513 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 514 if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) 515 goto out; 516 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 517 panic("acpi wake added power consumer but can't find it"); 518 } 519 520 status = AE_OK; 521 if (acpi_parse_prw(consumer, &prw) != 0) 522 goto out; 523 for (i = 0; i < prw.power_res_count; i++) 524 if (enable) 525 acpi_pwr_reference_resource(&prw.power_res[i], pc); 526 else 527 acpi_pwr_dereference_resource(pc); 528 529 if (prw.power_res_count > 0) 530 acpi_pwr_switch_power(); 531 532out: 533 ACPI_SERIAL_END(powerres); 534 return (status); 535} 536 537/* 538 * Called to create a reference between a power consumer and a power resource 539 * identified in the object. 540 */ 541static void 542acpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg) 543{ 544 struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg; 545 struct acpi_powerreference *pr; 546 struct acpi_powerresource *rp; 547 ACPI_HANDLE res; 548 ACPI_STATUS status; 549 550 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 551 ACPI_SERIAL_ASSERT(powerres); 552 553 res = acpi_GetReference(NULL, obj); 554 if (res == NULL) { 555 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 556 "can't create a power reference for object type %d\n", 557 obj->Type)); 558 return_VOID; 559 } 560 561 /* Create/look up the resource */ 562 if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) { 563 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 564 "couldn't register power resource %s - %s\n", 565 obj->String.Pointer, AcpiFormatException(status))); 566 return_VOID; 567 } 568 if ((rp = acpi_pwr_find_resource(res)) == NULL) { 569 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n")); 570 return_VOID; 571 } 572 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n", 573 acpi_name(rp->ap_resource))); 574 575 /* Create a reference between the consumer and resource */ 576 if ((pr = malloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 577 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 578 "allocation failed for a power consumer reference\n")); 579 return_VOID; 580 } 581 pr->ar_consumer = pc; 582 pr->ar_resource = rp; 583 TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink); 584 TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink); 585 586 return_VOID; 587} 588 589static int 590acpi_pwr_dereference_resource(struct acpi_powerconsumer *pc) 591{ 592 struct acpi_powerreference *pr; 593 int changed; 594 595 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 596 ACPI_SERIAL_ASSERT(powerres); 597 598 changed = 0; 599 while ((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) { 600 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n", 601 acpi_name(pr->ar_resource->ap_resource))); 602 TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink); 603 TAILQ_REMOVE(&pc->ac_references, pr, ar_clink); 604 free(pr, M_ACPIPWR); 605 changed = 1; 606 } 607 608 return (changed); 609} 610 611/* 612 * Switch power resources to conform to the desired state. 613 * 614 * Consumers may have modified the power resource list in an arbitrary 615 * fashion; we sweep it in sequence order. 616 */ 617static ACPI_STATUS 618acpi_pwr_switch_power(void) 619{ 620 struct acpi_powerresource *rp; 621 ACPI_STATUS status; 622 int cur; 623 624 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 625 ACPI_SERIAL_ASSERT(powerres); 626 627 /* 628 * Sweep the list forwards turning things on. 629 */ 630 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 631 if (TAILQ_FIRST(&rp->ap_references) == NULL) { 632 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 633 "%s has no references, not turning on\n", 634 acpi_name(rp->ap_resource))); 635 continue; 636 } 637 638 status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 639 if (ACPI_FAILURE(status)) { 640 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 641 acpi_name(rp->ap_resource), status)); 642 /* XXX is this correct? Always switch if in doubt? */ 643 continue; 644 } 645 646 /* 647 * Switch if required. Note that we ignore the result of the switch 648 * effort; we don't know what to do if it fails, so checking wouldn't 649 * help much. 650 */ 651 if (cur != ACPI_PWR_ON) { 652 status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL); 653 if (ACPI_FAILURE(status)) { 654 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 655 "failed to switch %s on - %s\n", 656 acpi_name(rp->ap_resource), 657 AcpiFormatException(status))); 658 } else { 659 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n", 660 acpi_name(rp->ap_resource))); 661 } 662 } else { 663 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n", 664 acpi_name(rp->ap_resource))); 665 } 666 } 667 668 /* Sweep the list backwards turning things off. */ 669 TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list, 670 ap_link) { 671 672 if (TAILQ_FIRST(&rp->ap_references) != NULL) { 673 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 674 "%s has references, not turning off\n", 675 acpi_name(rp->ap_resource))); 676 continue; 677 } 678 679 status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 680 if (ACPI_FAILURE(status)) { 681 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 682 acpi_name(rp->ap_resource), status)); 683 /* XXX is this correct? Always switch if in doubt? */ 684 continue; 685 } 686 687 /* 688 * Switch if required. Note that we ignore the result of the switch 689 * effort; we don't know what to do if it fails, so checking wouldn't 690 * help much. 691 */ 692 if (cur != ACPI_PWR_OFF) { 693 status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL); 694 if (ACPI_FAILURE(status)) { 695 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 696 "failed to switch %s off - %s\n", 697 acpi_name(rp->ap_resource), 698 AcpiFormatException(status))); 699 } else { 700 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n", 701 acpi_name(rp->ap_resource))); 702 } 703 } else { 704 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n", 705 acpi_name(rp->ap_resource))); 706 } 707 } 708 709 return_ACPI_STATUS (AE_OK); 710} 711 712/* 713 * Find a power resource's control structure. 714 */ 715static struct acpi_powerresource * 716acpi_pwr_find_resource(ACPI_HANDLE res) 717{ 718 struct acpi_powerresource *rp; 719 720 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 721 ACPI_SERIAL_ASSERT(powerres); 722 723 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 724 if (rp->ap_resource == res) 725 break; 726 } 727 728 return_PTR (rp); 729} 730 731/* 732 * Find a power consumer's control structure. 733 */ 734static struct acpi_powerconsumer * 735acpi_pwr_find_consumer(ACPI_HANDLE consumer) 736{ 737 struct acpi_powerconsumer *pc; 738 739 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 740 ACPI_SERIAL_ASSERT(powerres); 741 742 TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) { 743 if (pc->ac_consumer == consumer) 744 break; 745 } 746 747 return_PTR (pc); 748}
| 118/* 119 * Register a power resource. 120 * 121 * It's OK to call this if we already know about the resource. 122 */ 123static ACPI_STATUS 124acpi_pwr_register_resource(ACPI_HANDLE res) 125{ 126 ACPI_STATUS status; 127 ACPI_BUFFER buf; 128 ACPI_OBJECT *obj; 129 struct acpi_powerresource *rp, *srp; 130 131 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 132 ACPI_SERIAL_ASSERT(powerres); 133 134 rp = NULL; 135 buf.Pointer = NULL; 136 137 /* Look to see if we know about this resource */ 138 if (acpi_pwr_find_resource(res) != NULL) 139 return_ACPI_STATUS (AE_OK); /* already know about it */ 140 141 /* Allocate a new resource */ 142 if ((rp = malloc(sizeof(*rp), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 143 status = AE_NO_MEMORY; 144 goto out; 145 } 146 TAILQ_INIT(&rp->ap_references); 147 rp->ap_resource = res; 148 149 /* Get the Power Resource object */ 150 buf.Length = ACPI_ALLOCATE_BUFFER; 151 if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) { 152 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n")); 153 goto out; 154 } 155 obj = buf.Pointer; 156 if (obj->Type != ACPI_TYPE_POWER) { 157 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 158 "questionable power resource object %s\n", 159 acpi_name(res))); 160 status = AE_TYPE; 161 goto out; 162 } 163 rp->ap_systemlevel = obj->PowerResource.SystemLevel; 164 rp->ap_order = obj->PowerResource.ResourceOrder; 165 166 /* Sort the resource into the list */ 167 status = AE_OK; 168 srp = TAILQ_FIRST(&acpi_powerresources); 169 if (srp == NULL || rp->ap_order < srp->ap_order) { 170 TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link); 171 goto done; 172 } 173 TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) { 174 if (rp->ap_order < srp->ap_order) { 175 TAILQ_INSERT_BEFORE(srp, rp, ap_link); 176 goto done; 177 } 178 } 179 TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link); 180 181 done: 182 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 183 "registered power resource %s\n", acpi_name(res))); 184 185 out: 186 if (buf.Pointer != NULL) 187 AcpiOsFree(buf.Pointer); 188 if (ACPI_FAILURE(status) && rp != NULL) 189 free(rp, M_ACPIPWR); 190 return_ACPI_STATUS (status); 191} 192 193#ifdef notyet 194/* 195 * Deregister a power resource. 196 */ 197static ACPI_STATUS 198acpi_pwr_deregister_resource(ACPI_HANDLE res) 199{ 200 struct acpi_powerresource *rp; 201 202 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 203 ACPI_SERIAL_ASSERT(powerres); 204 205 rp = NULL; 206 207 /* Find the resource */ 208 if ((rp = acpi_pwr_find_resource(res)) == NULL) 209 return_ACPI_STATUS (AE_BAD_PARAMETER); 210 211 /* Check that there are no consumers referencing this resource */ 212 if (TAILQ_FIRST(&rp->ap_references) != NULL) 213 return_ACPI_STATUS (AE_BAD_PARAMETER); 214 215 /* Pull it off the list and free it */ 216 TAILQ_REMOVE(&acpi_powerresources, rp, ap_link); 217 free(rp, M_ACPIPWR); 218 219 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n", 220 acpi_name(res))); 221 222 return_ACPI_STATUS (AE_OK); 223} 224#endif /* notyet */ 225 226/* 227 * Register a power consumer. 228 * 229 * It's OK to call this if we already know about the consumer. 230 */ 231static ACPI_STATUS 232acpi_pwr_register_consumer(ACPI_HANDLE consumer) 233{ 234 struct acpi_powerconsumer *pc; 235 236 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 237 ACPI_SERIAL_ASSERT(powerres); 238 239 /* Check to see whether we know about this consumer already */ 240 if (acpi_pwr_find_consumer(consumer) != NULL) 241 return_ACPI_STATUS (AE_OK); 242 243 /* Allocate a new power consumer */ 244 if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL) 245 return_ACPI_STATUS (AE_NO_MEMORY); 246 TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link); 247 TAILQ_INIT(&pc->ac_references); 248 pc->ac_consumer = consumer; 249 250 /* XXX we should try to find its current state */ 251 pc->ac_state = ACPI_STATE_UNKNOWN; 252 253 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n", 254 acpi_name(consumer))); 255 256 return_ACPI_STATUS (AE_OK); 257} 258 259#ifdef notyet 260/* 261 * Deregister a power consumer. 262 * 263 * This should only be done once the consumer has been powered off. 264 * (XXX is this correct? Check once implemented) 265 */ 266static ACPI_STATUS 267acpi_pwr_deregister_consumer(ACPI_HANDLE consumer) 268{ 269 struct acpi_powerconsumer *pc; 270 271 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 272 ACPI_SERIAL_ASSERT(powerres); 273 274 /* Find the consumer */ 275 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 276 return_ACPI_STATUS (AE_BAD_PARAMETER); 277 278 /* Make sure the consumer's not referencing anything right now */ 279 if (TAILQ_FIRST(&pc->ac_references) != NULL) 280 return_ACPI_STATUS (AE_BAD_PARAMETER); 281 282 /* Pull the consumer off the list and free it */ 283 TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link); 284 free(pc, M_ACPIPWR); 285 286 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n", 287 acpi_name(consumer))); 288 289 return_ACPI_STATUS (AE_OK); 290} 291#endif /* notyet */ 292 293/* 294 * Set a power consumer to a particular power state. 295 */ 296ACPI_STATUS 297acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state) 298{ 299 struct acpi_powerconsumer *pc; 300 ACPI_HANDLE method_handle, reslist_handle, pr0_handle; 301 ACPI_BUFFER reslist_buffer; 302 ACPI_OBJECT *reslist_object; 303 ACPI_STATUS status; 304 char *method_name, *reslist_name; 305 306 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 307 308 /* It's never ok to switch a non-existent consumer. */ 309 if (consumer == NULL) 310 return_ACPI_STATUS (AE_NOT_FOUND); 311 reslist_buffer.Pointer = NULL; 312 reslist_object = NULL; 313 ACPI_SERIAL_BEGIN(powerres); 314 315 /* Find the consumer */ 316 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 317 if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) 318 goto out; 319 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 320 panic("acpi added power consumer but can't find it"); 321 } 322 323 /* Check for valid transitions. We can only go to D0 from D3. */ 324 status = AE_BAD_PARAMETER; 325 if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0) 326 goto out; 327 328 /* Find transition mechanism(s) */ 329 switch (state) { 330 case ACPI_STATE_D0: 331 method_name = "_PS0"; 332 reslist_name = "_PR0"; 333 break; 334 case ACPI_STATE_D1: 335 method_name = "_PS1"; 336 reslist_name = "_PR1"; 337 break; 338 case ACPI_STATE_D2: 339 method_name = "_PS2"; 340 reslist_name = "_PR2"; 341 break; 342 case ACPI_STATE_D3: 343 method_name = "_PS3"; 344 reslist_name = "_PR3"; 345 break; 346 default: 347 goto out; 348 } 349 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n", 350 acpi_name(consumer), pc->ac_state, state)); 351 352 /* 353 * Verify that this state is supported, ie. one of method or 354 * reslist must be present. We need to do this before we go 355 * dereferencing resources (since we might be trying to go to 356 * a state we don't support). 357 * 358 * Note that if any states are supported, the device has to 359 * support D0 and D3. It's never an error to try to go to 360 * D0. 361 */ 362 if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle))) 363 method_handle = NULL; 364 if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle))) 365 reslist_handle = NULL; 366 if (reslist_handle == NULL && method_handle == NULL) { 367 if (state == ACPI_STATE_D0) { 368 pc->ac_state = ACPI_STATE_D0; 369 status = AE_OK; 370 goto out; 371 } 372 if (state != ACPI_STATE_D3) { 373 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 374 "attempt to set unsupported state D%d\n", state)); 375 goto out; 376 } 377 378 /* 379 * Turn off the resources listed in _PR0 to go to D3. If there is 380 * no _PR0 method, this object doesn't support ACPI power states. 381 */ 382 if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle))) { 383 status = AE_NOT_FOUND; 384 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 385 "device missing _PR0 (desired state was D%d)\n", state)); 386 goto out; 387 } 388 reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 389 status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer); 390 if (ACPI_FAILURE(status)) { 391 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 392 "can't evaluate _PR0 for device %s, state D%d\n", 393 acpi_name(consumer), state)); 394 goto out; 395 } 396 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 397 if (!ACPI_PKG_VALID(reslist_object, 1)) { 398 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 399 "invalid package object for state D%d\n", state)); 400 status = AE_TYPE; 401 goto out; 402 } 403 AcpiOsFree(reslist_buffer.Pointer); 404 reslist_buffer.Pointer = NULL; 405 reslist_object = NULL; 406 } 407 408 /* 409 * Check that we can actually fetch the list of power resources 410 */ 411 if (reslist_handle != NULL) { 412 reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 413 status = AcpiEvaluateObject(reslist_handle, NULL, NULL, 414 &reslist_buffer); 415 if (ACPI_FAILURE(status)) { 416 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 417 "can't evaluate resource list %s\n", 418 acpi_name(reslist_handle))); 419 goto out; 420 } 421 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 422 if (reslist_object->Type != ACPI_TYPE_PACKAGE) { 423 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 424 "resource list is not ACPI_TYPE_PACKAGE (%d)\n", 425 reslist_object->Type)); 426 status = AE_TYPE; 427 goto out; 428 } 429 } 430 431 /* 432 * Now we are ready to switch, so kill off any current power 433 * resource references. 434 */ 435 acpi_pwr_dereference_resource(pc); 436 437 /* 438 * Add new power resource references, if we have any. Traverse the 439 * package that we got from evaluating reslist_handle, and look up each 440 * of the resources that are referenced. 441 */ 442 if (reslist_object != NULL) { 443 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n", 444 reslist_object->Package.Count)); 445 acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource, 446 pc); 447 } 448 449 /* 450 * If we changed anything in the resource list, we need to run a switch 451 * pass now. 452 */ 453 if (ACPI_FAILURE(status = acpi_pwr_switch_power())) { 454 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 455 "failed to switch resources from %s to D%d\n", 456 acpi_name(consumer), state)); 457 458 /* XXX is this appropriate? Should we return to previous state? */ 459 goto out; 460 } 461 462 /* Invoke power state switch method (if present) */ 463 if (method_handle != NULL) { 464 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 465 "invoking state transition method %s\n", 466 acpi_name(method_handle))); 467 status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL); 468 if (ACPI_FAILURE(status)) { 469 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n", 470 AcpiFormatException(status))); 471 pc->ac_state = ACPI_STATE_UNKNOWN; 472 473 /* XXX Should we return to previous state? */ 474 goto out; 475 } 476 } 477 478 /* Transition was successful */ 479 pc->ac_state = state; 480 status = AE_OK; 481 482out: 483 ACPI_SERIAL_END(powerres); 484 if (reslist_buffer.Pointer != NULL) 485 AcpiOsFree(reslist_buffer.Pointer); 486 return_ACPI_STATUS (status); 487} 488 489/* Enable or disable a power resource for wake */ 490ACPI_STATUS 491acpi_pwr_wake_enable(ACPI_HANDLE consumer, int enable) 492{ 493 ACPI_STATUS status; 494 struct acpi_powerconsumer *pc; 495 struct acpi_prw_data prw; 496 int i; 497 498 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 499 500 if (consumer == NULL) 501 return (AE_BAD_PARAMETER); 502 503 ACPI_SERIAL_BEGIN(powerres); 504 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 505 if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) 506 goto out; 507 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 508 panic("acpi wake added power consumer but can't find it"); 509 } 510 511 status = AE_OK; 512 if (acpi_parse_prw(consumer, &prw) != 0) 513 goto out; 514 for (i = 0; i < prw.power_res_count; i++) 515 if (enable) 516 acpi_pwr_reference_resource(&prw.power_res[i], pc); 517 else 518 acpi_pwr_dereference_resource(pc); 519 520 if (prw.power_res_count > 0) 521 acpi_pwr_switch_power(); 522 523out: 524 ACPI_SERIAL_END(powerres); 525 return (status); 526} 527 528/* 529 * Called to create a reference between a power consumer and a power resource 530 * identified in the object. 531 */ 532static void 533acpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg) 534{ 535 struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg; 536 struct acpi_powerreference *pr; 537 struct acpi_powerresource *rp; 538 ACPI_HANDLE res; 539 ACPI_STATUS status; 540 541 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 542 ACPI_SERIAL_ASSERT(powerres); 543 544 res = acpi_GetReference(NULL, obj); 545 if (res == NULL) { 546 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 547 "can't create a power reference for object type %d\n", 548 obj->Type)); 549 return_VOID; 550 } 551 552 /* Create/look up the resource */ 553 if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) { 554 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 555 "couldn't register power resource %s - %s\n", 556 obj->String.Pointer, AcpiFormatException(status))); 557 return_VOID; 558 } 559 if ((rp = acpi_pwr_find_resource(res)) == NULL) { 560 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n")); 561 return_VOID; 562 } 563 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n", 564 acpi_name(rp->ap_resource))); 565 566 /* Create a reference between the consumer and resource */ 567 if ((pr = malloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 568 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 569 "allocation failed for a power consumer reference\n")); 570 return_VOID; 571 } 572 pr->ar_consumer = pc; 573 pr->ar_resource = rp; 574 TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink); 575 TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink); 576 577 return_VOID; 578} 579 580static int 581acpi_pwr_dereference_resource(struct acpi_powerconsumer *pc) 582{ 583 struct acpi_powerreference *pr; 584 int changed; 585 586 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 587 ACPI_SERIAL_ASSERT(powerres); 588 589 changed = 0; 590 while ((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) { 591 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n", 592 acpi_name(pr->ar_resource->ap_resource))); 593 TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink); 594 TAILQ_REMOVE(&pc->ac_references, pr, ar_clink); 595 free(pr, M_ACPIPWR); 596 changed = 1; 597 } 598 599 return (changed); 600} 601 602/* 603 * Switch power resources to conform to the desired state. 604 * 605 * Consumers may have modified the power resource list in an arbitrary 606 * fashion; we sweep it in sequence order. 607 */ 608static ACPI_STATUS 609acpi_pwr_switch_power(void) 610{ 611 struct acpi_powerresource *rp; 612 ACPI_STATUS status; 613 int cur; 614 615 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 616 ACPI_SERIAL_ASSERT(powerres); 617 618 /* 619 * Sweep the list forwards turning things on. 620 */ 621 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 622 if (TAILQ_FIRST(&rp->ap_references) == NULL) { 623 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 624 "%s has no references, not turning on\n", 625 acpi_name(rp->ap_resource))); 626 continue; 627 } 628 629 status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 630 if (ACPI_FAILURE(status)) { 631 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 632 acpi_name(rp->ap_resource), status)); 633 /* XXX is this correct? Always switch if in doubt? */ 634 continue; 635 } 636 637 /* 638 * Switch if required. Note that we ignore the result of the switch 639 * effort; we don't know what to do if it fails, so checking wouldn't 640 * help much. 641 */ 642 if (cur != ACPI_PWR_ON) { 643 status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL); 644 if (ACPI_FAILURE(status)) { 645 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 646 "failed to switch %s on - %s\n", 647 acpi_name(rp->ap_resource), 648 AcpiFormatException(status))); 649 } else { 650 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n", 651 acpi_name(rp->ap_resource))); 652 } 653 } else { 654 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n", 655 acpi_name(rp->ap_resource))); 656 } 657 } 658 659 /* Sweep the list backwards turning things off. */ 660 TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list, 661 ap_link) { 662 663 if (TAILQ_FIRST(&rp->ap_references) != NULL) { 664 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 665 "%s has references, not turning off\n", 666 acpi_name(rp->ap_resource))); 667 continue; 668 } 669 670 status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 671 if (ACPI_FAILURE(status)) { 672 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 673 acpi_name(rp->ap_resource), status)); 674 /* XXX is this correct? Always switch if in doubt? */ 675 continue; 676 } 677 678 /* 679 * Switch if required. Note that we ignore the result of the switch 680 * effort; we don't know what to do if it fails, so checking wouldn't 681 * help much. 682 */ 683 if (cur != ACPI_PWR_OFF) { 684 status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL); 685 if (ACPI_FAILURE(status)) { 686 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 687 "failed to switch %s off - %s\n", 688 acpi_name(rp->ap_resource), 689 AcpiFormatException(status))); 690 } else { 691 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n", 692 acpi_name(rp->ap_resource))); 693 } 694 } else { 695 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n", 696 acpi_name(rp->ap_resource))); 697 } 698 } 699 700 return_ACPI_STATUS (AE_OK); 701} 702 703/* 704 * Find a power resource's control structure. 705 */ 706static struct acpi_powerresource * 707acpi_pwr_find_resource(ACPI_HANDLE res) 708{ 709 struct acpi_powerresource *rp; 710 711 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 712 ACPI_SERIAL_ASSERT(powerres); 713 714 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 715 if (rp->ap_resource == res) 716 break; 717 } 718 719 return_PTR (rp); 720} 721 722/* 723 * Find a power consumer's control structure. 724 */ 725static struct acpi_powerconsumer * 726acpi_pwr_find_consumer(ACPI_HANDLE consumer) 727{ 728 struct acpi_powerconsumer *pc; 729 730 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 731 ACPI_SERIAL_ASSERT(powerres); 732 733 TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) { 734 if (pc->ac_consumer == consumer) 735 break; 736 } 737 738 return_PTR (pc); 739}
|