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