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 unchanged lines hidden (view full) --- 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 * $FreeBSD: head/sys/dev/acpica/acpi_powerres.c 91125 2002-02-23 05:28:22Z msmith $ |
27 */ 28 29#include "opt_acpi.h" /* XXX trim includes */ 30#include <sys/param.h> 31#include <sys/kernel.h> 32#include <sys/proc.h> 33#include <sys/lock.h> 34#include <sys/malloc.h> --- 35 unchanged lines hidden (view full) --- 70 */ 71 72MALLOC_DEFINE(M_ACPIPWR, "acpipwr", "ACPI power resources"); 73 74/* 75 * Hooks for the ACPI CA debugging infrastructure 76 */ 77#define _COMPONENT ACPI_POWER |
78ACPI_MODULE_NAME("POWERRES") |
79 80/* return values from _STA on a power resource */ 81#define ACPI_PWR_OFF 0 82#define ACPI_PWR_ON 1 83 84/* 85 * A relationship between a power resource and a consumer. 86 */ --- 56 unchanged lines hidden (view full) --- 143static ACPI_STATUS 144acpi_pwr_register_resource(ACPI_HANDLE res) 145{ 146 ACPI_STATUS status; 147 ACPI_BUFFER buf; 148 ACPI_OBJECT *obj; 149 struct acpi_powerresource *rp, *srp; 150 |
151 ACPI_FUNCTION_TRACE(__func__); |
152 153 rp = NULL; |
154 buf.Pointer = NULL; |
155 156 /* look to see if we know about this resource */ 157 if (acpi_pwr_find_resource(res) != NULL) 158 return_ACPI_STATUS(AE_OK); /* already know about it */ 159 160 /* allocate a new resource */ 161 if ((rp = malloc(sizeof(*rp), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 162 status = AE_NO_MEMORY; 163 goto out; 164 } 165 TAILQ_INIT(&rp->ap_references); 166 rp->ap_resource = res; 167 168 /* get the Power Resource object */ |
169 buf.Length = ACPI_ALLOCATE_BUFFER; 170 if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) { |
171 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n")); 172 goto out; 173 } 174 obj = buf.Pointer; 175 if (obj->Type != ACPI_TYPE_POWER) { 176 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "questionable power resource object %s\n", acpi_name(res))); 177 status = AE_TYPE; 178 goto out; --- 13 unchanged lines hidden (view full) --- 192 TAILQ_INSERT_BEFORE(srp, rp, ap_link); 193 goto done; 194 } 195 TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link); 196 197 done: 198 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power resource %s\n", acpi_name(res))); 199 out: |
200 if (buf.Pointer != NULL) 201 AcpiOsFree(buf.Pointer); 202 if (ACPI_FAILURE(status) && (rp != NULL)) |
203 free(rp, M_ACPIPWR); 204 return_ACPI_STATUS(status); 205} 206 207/* 208 * Deregister a power resource. 209 */ 210static ACPI_STATUS 211acpi_pwr_deregister_resource(ACPI_HANDLE res) 212{ 213 struct acpi_powerresource *rp; 214 |
215 ACPI_FUNCTION_TRACE(__func__); |
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 */ --- 14 unchanged lines hidden (view full) --- 238 * 239 * It's OK to call this if we already know about the consumer. 240 */ 241static ACPI_STATUS 242acpi_pwr_register_consumer(ACPI_HANDLE consumer) 243{ 244 struct acpi_powerconsumer *pc; 245 |
246 ACPI_FUNCTION_TRACE(__func__); |
247 248 /* check to see whether we know about this consumer already */ 249 if ((pc = 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); --- 14 unchanged lines hidden (view full) --- 269 * This should only be done once the consumer has been powered off. 270 * (XXX is this correct? Check once implemented) 271 */ 272static ACPI_STATUS 273acpi_pwr_deregister_consumer(ACPI_HANDLE consumer) 274{ 275 struct acpi_powerconsumer *pc; 276 |
277 ACPI_FUNCTION_TRACE(__func__); |
278 279 /* find the consumer */ 280 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 281 return_ACPI_STATUS(AE_BAD_PARAMETER); 282 283 /* make sure the consumer's not referencing anything right now */ 284 if (TAILQ_FIRST(&pc->ac_references) != NULL) 285 return_ACPI_STATUS(AE_BAD_PARAMETER); --- 16 unchanged lines hidden (view full) --- 302 struct acpi_powerreference *pr; 303 ACPI_HANDLE method_handle, reslist_handle, pr0_handle; 304 ACPI_BUFFER reslist_buffer; 305 ACPI_OBJECT *reslist_object; 306 ACPI_STATUS status; 307 char *method_name, *reslist_name; 308 int res_changed; 309 |
310 ACPI_FUNCTION_TRACE(__func__); |
311 312 /* find the consumer */ 313 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { |
314 if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) |
315 return_ACPI_STATUS(status); 316 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 317 return_ACPI_STATUS(AE_ERROR); /* something very wrong */ 318 } 319 } 320 321 /* check for valid transitions */ 322 if ((pc->ac_state == ACPI_STATE_D3) && (state != ACPI_STATE_D0)) --- 28 unchanged lines hidden (view full) --- 351 * reslist must be present. We need to do this before we go 352 * dereferencing resources (since we might be trying to go to 353 * a state we don't support). 354 * 355 * Note that if any states are supported, the device has to 356 * support D0 and D3. It's never an error to try to go to 357 * D0. 358 */ |
359 reslist_buffer.Pointer = NULL; |
360 reslist_object = NULL; |
361 if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle))) |
362 method_handle = NULL; |
363 if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle))) |
364 reslist_handle = NULL; 365 if ((reslist_handle == NULL) && (method_handle == NULL)) { 366 if (state == ACPI_STATE_D0) { 367 pc->ac_state = ACPI_STATE_D0; 368 return_ACPI_STATUS(AE_OK); 369 } 370 if (state != ACPI_STATE_D3) { 371 goto bad; 372 } 373 374 /* turn off the resources listed in _PR0 to go to D3. */ |
375 if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle))) { |
376 goto bad; 377 } |
378 reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 379 if (ACPI_FAILURE(status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer))) { |
380 goto bad; 381 } 382 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 383 if ((reslist_object->Type != ACPI_TYPE_PACKAGE) || 384 (reslist_object->Package.Count == 0)) { 385 goto bad; 386 } |
387 AcpiOsFree(reslist_buffer.Pointer); 388 reslist_buffer.Pointer = NULL; 389 reslist_object = NULL; |
390 } 391 392 /* 393 * Check that we can actually fetch the list of power resources 394 */ 395 if (reslist_handle != NULL) { |
396 reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 397 if (ACPI_FAILURE(status = AcpiEvaluateObject(reslist_handle, NULL, NULL, &reslist_buffer))) { |
398 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't evaluate resource list %s\n", 399 acpi_name(reslist_handle))); |
400 goto out; |
401 } 402 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 403 if (reslist_object->Type != ACPI_TYPE_PACKAGE) { 404 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "resource list is not ACPI_TYPE_PACKAGE (%d)\n", 405 reslist_object->Type)); |
406 status = AE_TYPE; 407 goto out; |
408 } |
409 } 410 411 /* 412 * Now we are ready to switch, so kill off any current power resource references. 413 */ 414 res_changed = 0; 415 while((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) { 416 res_changed = 1; --- 14 unchanged lines hidden (view full) --- 431 acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource, pc); 432 res_changed = 1; 433 } 434 435 /* 436 * If we changed anything in the resource list, we need to run a switch 437 * pass now. 438 */ |
439 if (ACPI_FAILURE(status = acpi_pwr_switch_power())) { |
440 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to correctly switch resources to move %s to D%d\n", 441 acpi_name(consumer), state)); |
442 goto out; /* XXX is this appropriate? Should we return to previous state? */ |
443 } 444 445 /* invoke power state switch method (if present) */ 446 if (method_handle != NULL) { 447 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "invoking state transition method %s\n", 448 acpi_name(method_handle))); |
449 if (ACPI_FAILURE(status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL))) { 450 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n", 451 AcpiFormatException(status))); 452 pc->ac_state = ACPI_STATE_UNKNOWN; 453 goto out; /* XXX Should we return to previous state? */ 454 } |
455 } |
456 |
457 /* transition was successful */ 458 pc->ac_state = state; 459 return_ACPI_STATUS(AE_OK); 460 461 bad: 462 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "attempt to set unsupported state D%d\n", 463 state)); |
464 status = AE_BAD_PARAMETER; 465 466 out: 467 if (reslist_buffer.Pointer != NULL) 468 AcpiOsFree(reslist_buffer.Pointer); 469 return_ACPI_STATUS(status); |
470} 471 472/* 473 * Called to create a reference between a power consumer and a power resource 474 * identified in the object. 475 */ 476static void 477acpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg) 478{ 479 struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg; 480 struct acpi_powerreference *pr; 481 struct acpi_powerresource *rp; 482 ACPI_HANDLE res; 483 ACPI_STATUS status; 484 |
485 ACPI_FUNCTION_TRACE(__func__); |
486 487 /* check the object type */ 488 if (obj->Type != ACPI_TYPE_STRING) { 489 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "don't know how to create a power reference to object type %d\n", 490 obj->Type)); 491 return_VOID; 492 } 493 --- 41 unchanged lines hidden (view full) --- 535 */ 536static ACPI_STATUS 537acpi_pwr_switch_power(void) 538{ 539 struct acpi_powerresource *rp; 540 ACPI_STATUS status; 541 int cur; 542 |
543 ACPI_FUNCTION_TRACE(__func__); |
544 545 /* 546 * Sweep the list forwards turning things on. 547 */ 548 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 549 if (TAILQ_FIRST(&rp->ap_references) == NULL) { 550 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s has no references, not turning on\n", 551 acpi_name(rp->ap_resource))); 552 continue; 553 } 554 555 /* we could cache this if we trusted it not to change under us */ |
556 if (ACPI_FAILURE(status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur))) { |
557 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 558 acpi_name(rp->ap_resource), status)); 559 continue; /* XXX is this correct? Always switch if in doubt? */ 560 } 561 562 /* 563 * Switch if required. Note that we ignore the result of the switch 564 * effort; we don't know what to do if it fails, so checking wouldn't --- 17 unchanged lines hidden (view full) --- 582 TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list, ap_link) { 583 if (TAILQ_FIRST(&rp->ap_references) != NULL) { 584 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s has references, not turning off\n", 585 acpi_name(rp->ap_resource))); 586 continue; 587 } 588 589 /* we could cache this if we trusted it not to change under us */ |
590 if (ACPI_FAILURE(status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur))) { |
591 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 592 acpi_name(rp->ap_resource), status)); 593 continue; /* XXX is this correct? Always switch if in doubt? */ 594 } 595 596 /* 597 * Switch if required. Note that we ignore the result of the switch 598 * effort; we don't know what to do if it fails, so checking wouldn't --- 16 unchanged lines hidden (view full) --- 615/* 616 * Find a power resource's control structure. 617 */ 618static struct acpi_powerresource * 619acpi_pwr_find_resource(ACPI_HANDLE res) 620{ 621 struct acpi_powerresource *rp; 622 |
623 ACPI_FUNCTION_TRACE(__func__); |
624 625 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) 626 if (rp->ap_resource == res) 627 break; 628 return_PTR(rp); 629} 630 631/* 632 * Find a power consumer's control structure. 633 */ 634static struct acpi_powerconsumer * 635acpi_pwr_find_consumer(ACPI_HANDLE consumer) 636{ 637 struct acpi_powerconsumer *pc; 638 |
639 ACPI_FUNCTION_TRACE(__func__); |
640 641 TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) 642 if (pc->ac_consumer == consumer) 643 break; 644 return_PTR(pc); 645} 646 |