1 /*- 2 * Copyright (c) 2001 Michael Smith 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_acpi.h" 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/malloc.h> 34 #include <sys/bus.h> 35 36 #include <contrib/dev/acpica/include/acpi.h> 37 #include <contrib/dev/acpica/include/accommon.h> 38 39 #include <dev/acpica/acpivar.h> 40 41 /* 42 * ACPI power resource management. 43 * 44 * Power resource behaviour is slightly complicated by the fact that 45 * a single power resource may provide power for more than one device. 46 * Thus, we must track the device(s) being powered by a given power 47 * resource, and only deactivate it when there are no powered devices. 48 * 49 * Note that this only manages resources for known devices. There is an 50 * ugly case where we may turn off power to a device which is in use because 51 * we don't know that it depends on a given resource. We should perhaps 52 * try to be smarter about this, but a more complete solution would involve 53 * scanning all of the ACPI namespace to find devices we're not currently 54 * aware of, and this raises questions about whether they should be left 55 * on, turned off, etc. 56 */ 57 58 static MALLOC_DEFINE(M_ACPIPWR, "acpipwr", "ACPI power resources"); 59 60 /* Hooks for the ACPI CA debugging infrastructure */ 61 #define _COMPONENT ACPI_POWERRES 62 ACPI_MODULE_NAME("POWERRES") 63 64 /* Return values from _STA on a power resource */ 65 #define ACPI_PWR_OFF 0 66 #define ACPI_PWR_ON 1 67 68 /* A relationship between a power resource and a consumer. */ 69 struct acpi_powerreference { 70 struct acpi_powerconsumer *ar_consumer; 71 struct acpi_powerresource *ar_resource; 72 TAILQ_ENTRY(acpi_powerreference) ar_rlink; /* link on resource list */ 73 TAILQ_ENTRY(acpi_powerreference) ar_clink; /* link on consumer */ 74 }; 75 76 /* A power-managed device. */ 77 struct acpi_powerconsumer { 78 /* Device which is powered */ 79 ACPI_HANDLE ac_consumer; 80 int ac_state; 81 TAILQ_ENTRY(acpi_powerconsumer) ac_link; 82 TAILQ_HEAD(,acpi_powerreference) ac_references; 83 }; 84 85 /* A power resource. */ 86 struct acpi_powerresource { 87 TAILQ_ENTRY(acpi_powerresource) ap_link; 88 TAILQ_HEAD(,acpi_powerreference) ap_references; 89 ACPI_HANDLE ap_resource; 90 UINT64 ap_systemlevel; 91 UINT64 ap_order; 92 }; 93 94 static TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource) 95 acpi_powerresources = TAILQ_HEAD_INITIALIZER(acpi_powerresources); 96 static TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer) 97 acpi_powerconsumers = TAILQ_HEAD_INITIALIZER(acpi_powerconsumers); 98 ACPI_SERIAL_DECL(powerres, "ACPI power resources"); 99 100 static ACPI_STATUS acpi_pwr_register_consumer(ACPI_HANDLE consumer); 101 #ifdef notyet 102 static ACPI_STATUS acpi_pwr_deregister_consumer(ACPI_HANDLE consumer); 103 #endif /* notyet */ 104 static ACPI_STATUS acpi_pwr_register_resource(ACPI_HANDLE res); 105 #ifdef notyet 106 static ACPI_STATUS acpi_pwr_deregister_resource(ACPI_HANDLE res); 107 #endif /* notyet */ 108 static void acpi_pwr_reference_resource(ACPI_OBJECT *obj, 109 void *arg); 110 static int acpi_pwr_dereference_resource(struct acpi_powerconsumer 111 *pc); 112 static ACPI_STATUS acpi_pwr_switch_power(void); 113 static struct acpi_powerresource 114 *acpi_pwr_find_resource(ACPI_HANDLE res); 115 static struct acpi_powerconsumer 116 *acpi_pwr_find_consumer(ACPI_HANDLE consumer); 117 118 /* 119 * Register a power resource. 120 * 121 * It's OK to call this if we already know about the resource. 122 */ 123 static ACPI_STATUS 124 acpi_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 */ 197 static ACPI_STATUS 198 acpi_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 */ 231 static ACPI_STATUS 232 acpi_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 */ 266 static ACPI_STATUS 267 acpi_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 */ 296 ACPI_STATUS 297 acpi_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 482 out: 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 */ 490 ACPI_STATUS 491 acpi_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 523 out: 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 */ 532 static void 533 acpi_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 580 static int 581 acpi_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 */ 608 static ACPI_STATUS 609 acpi_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 */ 706 static struct acpi_powerresource * 707 acpi_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 */ 725 static struct acpi_powerconsumer * 726 acpi_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 } 740