acpi_powerres.c revision 128252
178915Smsmith/*- 278915Smsmith * Copyright (c) 2001 Michael Smith 378915Smsmith * All rights reserved. 478915Smsmith * 578915Smsmith * Redistribution and use in source and binary forms, with or without 678915Smsmith * modification, are permitted provided that the following conditions 778915Smsmith * are met: 878915Smsmith * 1. Redistributions of source code must retain the above copyright 978915Smsmith * notice, this list of conditions and the following disclaimer. 1078915Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1178915Smsmith * notice, this list of conditions and the following disclaimer in the 1278915Smsmith * documentation and/or other materials provided with the distribution. 1378915Smsmith * 1478915Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1578915Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1678915Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1778915Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1878915Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1978915Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2078915Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2178915Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2278915Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2378915Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2478915Smsmith * SUCH DAMAGE. 2578915Smsmith */ 2678915Smsmith 27119418Sobrien#include <sys/cdefs.h> 28119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_powerres.c 128252 2004-04-14 17:58:19Z njl $"); 29119418Sobrien 30119529Snjl#include "opt_acpi.h" 3178915Smsmith#include <sys/param.h> 3278915Smsmith#include <sys/kernel.h> 3378915Smsmith#include <sys/malloc.h> 3478915Smsmith#include <sys/bus.h> 3578915Smsmith 3678915Smsmith#include "acpi.h" 3778915Smsmith#include <dev/acpica/acpivar.h> 3878915Smsmith 3978915Smsmith/* 4078915Smsmith * ACPI power resource management. 4178915Smsmith * 4278915Smsmith * Power resource behaviour is slightly complicated by the fact that 4378915Smsmith * a single power resource may provide power for more than one device. 4478915Smsmith * Thus, we must track the device(s) being powered by a given power 4578915Smsmith * resource, and only deactivate it when there are no powered devices. 4678915Smsmith * 4778915Smsmith * Note that this only manages resources for known devices. There is an 4878915Smsmith * ugly case where we may turn of power to a device which is in use because 4978915Smsmith * we don't know that it depends on a given resource. We should perhaps 5078915Smsmith * try to be smarter about this, but a more complete solution would involve 5178915Smsmith * scanning all of the ACPI namespace to find devices we're not currently 5278915Smsmith * aware of, and this raises questions about whether they should be left 5378915Smsmith * on, turned off, etc. 5478915Smsmith * 5578915Smsmith * XXX locking 5678915Smsmith */ 5778915Smsmith 5878915SmsmithMALLOC_DEFINE(M_ACPIPWR, "acpipwr", "ACPI power resources"); 5978915Smsmith 60119529Snjl/* Hooks for the ACPI CA debugging infrastructure */ 61126517Snjl#define _COMPONENT ACPI_POWERRES 6291125SmsmithACPI_MODULE_NAME("POWERRES") 6378915Smsmith 64119529Snjl/* Return values from _STA on a power resource */ 6579357Smsmith#define ACPI_PWR_OFF 0 6679357Smsmith#define ACPI_PWR_ON 1 6779357Smsmith 68119529Snjl/* A relationship between a power resource and a consumer. */ 6978915Smsmithstruct acpi_powerreference { 70119529Snjl struct acpi_powerconsumer *ar_consumer; 71119529Snjl struct acpi_powerresource *ar_resource; 72119529Snjl TAILQ_ENTRY(acpi_powerreference) ar_rlink; /* link on resource list */ 73119529Snjl TAILQ_ENTRY(acpi_powerreference) ar_clink; /* link on consumer */ 7478915Smsmith}; 7578915Smsmith 76119529Snjl/* A power-managed device. */ 7778915Smsmithstruct acpi_powerconsumer { 78119529Snjl /* Device which is powered */ 79119529Snjl ACPI_HANDLE ac_consumer; 80119529Snjl int ac_state; 81119529Snjl TAILQ_ENTRY(acpi_powerconsumer) ac_link; 82119529Snjl TAILQ_HEAD(,acpi_powerreference) ac_references; 8378915Smsmith}; 8478915Smsmith 85119529Snjl/* A power resource. */ 8678915Smsmithstruct acpi_powerresource { 87119529Snjl TAILQ_ENTRY(acpi_powerresource) ap_link; 88119529Snjl TAILQ_HEAD(,acpi_powerreference) ap_references; 89119529Snjl ACPI_HANDLE ap_resource; 90119529Snjl ACPI_INTEGER ap_systemlevel; 91119529Snjl ACPI_INTEGER ap_order; 9278915Smsmith}; 9378915Smsmith 94119529Snjlstatic TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource) 95119529Snjl acpi_powerresources; 96119529Snjlstatic TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer) 97119529Snjl acpi_powerconsumers; 9878915Smsmith 99119529Snjlstatic ACPI_STATUS acpi_pwr_register_consumer(ACPI_HANDLE consumer); 100128248Snjl#ifdef notyet 101119529Snjlstatic ACPI_STATUS acpi_pwr_deregister_consumer(ACPI_HANDLE consumer); 102128248Snjl#endif /* notyet */ 103119529Snjlstatic ACPI_STATUS acpi_pwr_register_resource(ACPI_HANDLE res); 104128248Snjl#ifdef notyet 105119529Snjlstatic ACPI_STATUS acpi_pwr_deregister_resource(ACPI_HANDLE res); 106128248Snjl#endif /* notyet */ 107119529Snjlstatic void acpi_pwr_reference_resource(ACPI_OBJECT *obj, 108119529Snjl void *arg); 109119529Snjlstatic ACPI_STATUS acpi_pwr_switch_power(void); 110119529Snjlstatic struct acpi_powerresource 111119529Snjl *acpi_pwr_find_resource(ACPI_HANDLE res); 112119529Snjlstatic struct acpi_powerconsumer 113119529Snjl *acpi_pwr_find_consumer(ACPI_HANDLE consumer); 11478915Smsmith 115119529Snjl/* Initialise our lists. */ 11678915Smsmithstatic void 11778915Smsmithacpi_pwr_init(void *junk) 11878915Smsmith{ 11978915Smsmith TAILQ_INIT(&acpi_powerresources); 12078915Smsmith TAILQ_INIT(&acpi_powerconsumers); 12178915Smsmith} 12278915SmsmithSYSINIT(acpi_powerresource, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_pwr_init, NULL); 12378915Smsmith 12478915Smsmith/* 12578915Smsmith * Register a power resource. 12678915Smsmith * 12778915Smsmith * It's OK to call this if we already know about the resource. 12878915Smsmith */ 12978915Smsmithstatic ACPI_STATUS 13078915Smsmithacpi_pwr_register_resource(ACPI_HANDLE res) 13178915Smsmith{ 13278915Smsmith ACPI_STATUS status; 13378915Smsmith ACPI_BUFFER buf; 13479493Smsmith ACPI_OBJECT *obj; 13578915Smsmith struct acpi_powerresource *rp, *srp; 13678915Smsmith 13796926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 13878915Smsmith 13978915Smsmith rp = NULL; 14091125Smsmith buf.Pointer = NULL; 14178915Smsmith 142119529Snjl /* Look to see if we know about this resource */ 14378915Smsmith if (acpi_pwr_find_resource(res) != NULL) 144119529Snjl return_ACPI_STATUS (AE_OK); /* already know about it */ 14578915Smsmith 146119529Snjl /* Allocate a new resource */ 14778915Smsmith if ((rp = malloc(sizeof(*rp), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 14878915Smsmith status = AE_NO_MEMORY; 14978915Smsmith goto out; 15078915Smsmith } 15178915Smsmith TAILQ_INIT(&rp->ap_references); 15278915Smsmith rp->ap_resource = res; 15378915Smsmith 154119529Snjl /* Get the Power Resource object */ 15591125Smsmith buf.Length = ACPI_ALLOCATE_BUFFER; 15691125Smsmith if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) { 15782372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n")); 15878915Smsmith goto out; 15978915Smsmith } 16078915Smsmith obj = buf.Pointer; 16179493Smsmith if (obj->Type != ACPI_TYPE_POWER) { 162119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 163119529Snjl "questionable power resource object %s\n", 164119529Snjl acpi_name(res))); 16579493Smsmith status = AE_TYPE; 16679493Smsmith goto out; 16778915Smsmith } 16879493Smsmith rp->ap_systemlevel = obj->PowerResource.SystemLevel; 16979493Smsmith rp->ap_order = obj->PowerResource.ResourceOrder; 17078915Smsmith 171119529Snjl /* Sort the resource into the list */ 17278915Smsmith status = AE_OK; 17378915Smsmith srp = TAILQ_FIRST(&acpi_powerresources); 174119529Snjl if (srp == NULL || rp->ap_order < srp->ap_order) { 17578915Smsmith TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link); 17679357Smsmith goto done; 17778915Smsmith } 178119529Snjl TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) { 17978915Smsmith if (rp->ap_order < srp->ap_order) { 18078915Smsmith TAILQ_INSERT_BEFORE(srp, rp, ap_link); 18179357Smsmith goto done; 18278915Smsmith } 183119529Snjl } 18478915Smsmith TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link); 18579357Smsmith 18679357Smsmith done: 187119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 188119529Snjl "registered power resource %s\n", acpi_name(res))); 189119529Snjl 19078915Smsmith out: 19191125Smsmith if (buf.Pointer != NULL) 19291125Smsmith AcpiOsFree(buf.Pointer); 193119529Snjl if (ACPI_FAILURE(status) && rp != NULL) 19478915Smsmith free(rp, M_ACPIPWR); 195119529Snjl return_ACPI_STATUS (status); 19678915Smsmith} 19778915Smsmith 198128248Snjl#ifdef notyet 19978915Smsmith/* 20078915Smsmith * Deregister a power resource. 20178915Smsmith */ 20278915Smsmithstatic ACPI_STATUS 20378915Smsmithacpi_pwr_deregister_resource(ACPI_HANDLE res) 20478915Smsmith{ 20578915Smsmith struct acpi_powerresource *rp; 20678915Smsmith 20796926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 20878915Smsmith 20978915Smsmith rp = NULL; 21078915Smsmith 211119529Snjl /* Find the resource */ 21278915Smsmith if ((rp = acpi_pwr_find_resource(res)) == NULL) 213119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 21478915Smsmith 215119529Snjl /* Check that there are no consumers referencing this resource */ 21678915Smsmith if (TAILQ_FIRST(&rp->ap_references) != NULL) 217119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 21878915Smsmith 219119529Snjl /* Pull it off the list and free it */ 22078915Smsmith TAILQ_REMOVE(&acpi_powerresources, rp, ap_link); 22178915Smsmith free(rp, M_ACPIPWR); 22278915Smsmith 223119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n", 224119529Snjl acpi_name(res))); 22578915Smsmith 226119529Snjl return_ACPI_STATUS (AE_OK); 22778915Smsmith} 228128248Snjl#endif /* notyet */ 22978915Smsmith 23078915Smsmith/* 23178915Smsmith * Register a power consumer. 23278915Smsmith * 23378915Smsmith * It's OK to call this if we already know about the consumer. 23478915Smsmith */ 23578915Smsmithstatic ACPI_STATUS 23678915Smsmithacpi_pwr_register_consumer(ACPI_HANDLE consumer) 23778915Smsmith{ 23878915Smsmith struct acpi_powerconsumer *pc; 23978915Smsmith 24096926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 24178915Smsmith 242119529Snjl /* Check to see whether we know about this consumer already */ 24378915Smsmith if ((pc = acpi_pwr_find_consumer(consumer)) != NULL) 244119529Snjl return_ACPI_STATUS (AE_OK); 24578915Smsmith 246119529Snjl /* Allocate a new power consumer */ 24778915Smsmith if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL) 248119529Snjl return_ACPI_STATUS (AE_NO_MEMORY); 24978915Smsmith TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link); 25078915Smsmith TAILQ_INIT(&pc->ac_references); 25178915Smsmith pc->ac_consumer = consumer; 25278915Smsmith 253119529Snjl /* XXX we should try to find its current state */ 254119529Snjl pc->ac_state = ACPI_STATE_UNKNOWN; 25578915Smsmith 256119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n", 257119529Snjl acpi_name(consumer))); 25878915Smsmith 259119529Snjl return_ACPI_STATUS (AE_OK); 26078915Smsmith} 26178915Smsmith 262128248Snjl#ifdef notyet 26378915Smsmith/* 26478915Smsmith * Deregister a power consumer. 26578915Smsmith * 26678915Smsmith * This should only be done once the consumer has been powered off. 26778915Smsmith * (XXX is this correct? Check once implemented) 26878915Smsmith */ 26978915Smsmithstatic ACPI_STATUS 27078915Smsmithacpi_pwr_deregister_consumer(ACPI_HANDLE consumer) 27178915Smsmith{ 27278915Smsmith struct acpi_powerconsumer *pc; 27378915Smsmith 27496926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 27578915Smsmith 276119529Snjl /* Find the consumer */ 27778915Smsmith if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 278119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 27978915Smsmith 280119529Snjl /* Make sure the consumer's not referencing anything right now */ 28178915Smsmith if (TAILQ_FIRST(&pc->ac_references) != NULL) 282119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 28378915Smsmith 284119529Snjl /* Pull the consumer off the list and free it */ 28578915Smsmith TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link); 28678915Smsmith 287119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n", 288119529Snjl acpi_name(consumer))); 28978915Smsmith 290119529Snjl return_ACPI_STATUS (AE_OK); 29178915Smsmith} 292128248Snjl#endif /* notyet */ 29378915Smsmith 29478915Smsmith/* 29578915Smsmith * Set a power consumer to a particular power state. 29678915Smsmith */ 29778915SmsmithACPI_STATUS 29878915Smsmithacpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state) 29978915Smsmith{ 30078915Smsmith struct acpi_powerconsumer *pc; 30178915Smsmith struct acpi_powerreference *pr; 30282084Siwasaki ACPI_HANDLE method_handle, reslist_handle, pr0_handle; 30378915Smsmith ACPI_BUFFER reslist_buffer; 30478915Smsmith ACPI_OBJECT *reslist_object; 30578915Smsmith ACPI_STATUS status; 30678915Smsmith char *method_name, *reslist_name; 30778915Smsmith int res_changed; 30878915Smsmith 30996926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 31078915Smsmith 311128252Snjl /* It's never ok to switch a non-existent consumer. */ 312128252Snjl if (consumer == NULL) 313128252Snjl return_ACPI_STATUS (AE_NOT_FOUND); 314128252Snjl 315119529Snjl /* Find the consumer */ 31678915Smsmith if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 31791125Smsmith if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) 318119529Snjl return_ACPI_STATUS (status); 31978915Smsmith if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 320119529Snjl return_ACPI_STATUS (AE_ERROR); /* something very wrong */ 32178915Smsmith } 32278915Smsmith } 32378915Smsmith 324119529Snjl /* Check for valid transitions */ 325119529Snjl if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0) 326119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); /* can only go to D0 from D3 */ 32778915Smsmith 328119529Snjl /* Find transition mechanism(s) */ 32978915Smsmith switch(state) { 33078915Smsmith case ACPI_STATE_D0: 33178915Smsmith method_name = "_PS0"; 33278915Smsmith reslist_name = "_PR0"; 33378915Smsmith break; 33478915Smsmith case ACPI_STATE_D1: 33578915Smsmith method_name = "_PS1"; 33678915Smsmith reslist_name = "_PR1"; 33778915Smsmith break; 33878915Smsmith case ACPI_STATE_D2: 33978915Smsmith method_name = "_PS2"; 34078915Smsmith reslist_name = "_PR2"; 34178915Smsmith break; 34278915Smsmith case ACPI_STATE_D3: 34378915Smsmith method_name = "_PS3"; 34478915Smsmith reslist_name = "_PR3"; 34578915Smsmith break; 34678915Smsmith default: 347119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 34878915Smsmith } 34982372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n", 350119529Snjl acpi_name(consumer), pc->ac_state, state)); 35178915Smsmith 35278915Smsmith /* 35378915Smsmith * Verify that this state is supported, ie. one of method or 35478915Smsmith * reslist must be present. We need to do this before we go 35578915Smsmith * dereferencing resources (since we might be trying to go to 35678915Smsmith * a state we don't support). 35778915Smsmith * 35878915Smsmith * Note that if any states are supported, the device has to 35978915Smsmith * support D0 and D3. It's never an error to try to go to 36078915Smsmith * D0. 36178915Smsmith */ 36291125Smsmith reslist_buffer.Pointer = NULL; 36382084Siwasaki reslist_object = NULL; 36491125Smsmith if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle))) 36578915Smsmith method_handle = NULL; 36691125Smsmith if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle))) 36778915Smsmith reslist_handle = NULL; 368119529Snjl if (reslist_handle == NULL && method_handle == NULL) { 36978915Smsmith if (state == ACPI_STATE_D0) { 37078915Smsmith pc->ac_state = ACPI_STATE_D0; 371119529Snjl return_ACPI_STATUS (AE_OK); 37278915Smsmith } 373119529Snjl if (state != ACPI_STATE_D3) 37482084Siwasaki goto bad; 37582084Siwasaki 376119529Snjl /* Turn off the resources listed in _PR0 to go to D3. */ 377119529Snjl if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle))) 37882084Siwasaki goto bad; 37991125Smsmith reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 380119529Snjl status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer); 381119529Snjl if (ACPI_FAILURE(status)) 38282084Siwasaki goto bad; 38382084Siwasaki reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 384119529Snjl if (reslist_object->Type != ACPI_TYPE_PACKAGE || 385119529Snjl reslist_object->Package.Count == 0) { 386119529Snjl 38782084Siwasaki goto bad; 38882084Siwasaki } 38991125Smsmith AcpiOsFree(reslist_buffer.Pointer); 39091125Smsmith reslist_buffer.Pointer = NULL; 39191125Smsmith reslist_object = NULL; 39278915Smsmith } 39378915Smsmith 39478915Smsmith /* 39578915Smsmith * Check that we can actually fetch the list of power resources 39678915Smsmith */ 39778915Smsmith if (reslist_handle != NULL) { 39891125Smsmith reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 399119529Snjl status = AcpiEvaluateObject(reslist_handle, NULL, NULL, 400119529Snjl &reslist_buffer); 401119529Snjl if (ACPI_FAILURE(status)) { 402119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 403119529Snjl "can't evaluate resource list %s\n", 404119529Snjl acpi_name(reslist_handle))); 40591125Smsmith goto out; 40678915Smsmith } 40778915Smsmith reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 40878915Smsmith if (reslist_object->Type != ACPI_TYPE_PACKAGE) { 409119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 410119529Snjl "resource list is not ACPI_TYPE_PACKAGE (%d)\n", 411119529Snjl reslist_object->Type)); 41291125Smsmith status = AE_TYPE; 41391125Smsmith goto out; 41478915Smsmith } 41578915Smsmith } 41678915Smsmith 41778915Smsmith /* 418125745Sjhb * Now we are ready to switch, so kill off any current power 419119529Snjl * resource references. 42078915Smsmith */ 42178915Smsmith res_changed = 0; 42279357Smsmith while((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) { 42379357Smsmith res_changed = 1; 424119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n", 425119529Snjl acpi_name(pr->ar_resource->ap_resource))); 42678915Smsmith TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink); 42779357Smsmith TAILQ_REMOVE(&pc->ac_references, pr, ar_clink); 42879357Smsmith free(pr, M_ACPIPWR); 42978915Smsmith } 43078915Smsmith 43178915Smsmith /* 43278915Smsmith * Add new power resource references, if we have any. Traverse the 43378915Smsmith * package that we got from evaluating reslist_handle, and look up each 43478915Smsmith * of the resources that are referenced. 43578915Smsmith */ 43678915Smsmith if (reslist_object != NULL) { 43782372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n", 43882372Smsmith reslist_object->Package.Count)); 439119529Snjl acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource, 440119529Snjl pc); 44178915Smsmith res_changed = 1; 44278915Smsmith } 44378915Smsmith 44478915Smsmith /* 44578915Smsmith * If we changed anything in the resource list, we need to run a switch 44678915Smsmith * pass now. 44778915Smsmith */ 44891125Smsmith if (ACPI_FAILURE(status = acpi_pwr_switch_power())) { 449119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 450119529Snjl "failed to switch resources from %s to D%d\n", 45182372Smsmith acpi_name(consumer), state)); 452119529Snjl 453119529Snjl /* XXX is this appropriate? Should we return to previous state? */ 454119529Snjl goto out; 45578915Smsmith } 45678915Smsmith 457119529Snjl /* Invoke power state switch method (if present) */ 45878915Smsmith if (method_handle != NULL) { 459119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 460119529Snjl "invoking state transition method %s\n", 461119529Snjl acpi_name(method_handle))); 462119529Snjl status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL); 463119529Snjl if (ACPI_FAILURE(status)) { 46491125Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n", 465119529Snjl AcpiFormatException(status))); 46691125Smsmith pc->ac_state = ACPI_STATE_UNKNOWN; 467119529Snjl 468119529Snjl /* XXX Should we return to previous state? */ 469119529Snjl goto out; 47091125Smsmith } 47178915Smsmith } 47291125Smsmith 473119529Snjl /* Transition was successful */ 47478915Smsmith pc->ac_state = state; 475119529Snjl return_ACPI_STATUS (AE_OK); 47682084Siwasaki 47782084Siwasaki bad: 478119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 479119529Snjl "attempt to set unsupported state D%d\n", state)); 48091125Smsmith status = AE_BAD_PARAMETER; 48191125Smsmith 48291125Smsmith out: 48391125Smsmith if (reslist_buffer.Pointer != NULL) 48491125Smsmith AcpiOsFree(reslist_buffer.Pointer); 485119529Snjl return_ACPI_STATUS (status); 48678915Smsmith} 48778915Smsmith 48878915Smsmith/* 48978915Smsmith * Called to create a reference between a power consumer and a power resource 49078915Smsmith * identified in the object. 49178915Smsmith */ 49278915Smsmithstatic void 49378915Smsmithacpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg) 49478915Smsmith{ 49578915Smsmith struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg; 49679357Smsmith struct acpi_powerreference *pr; 49779357Smsmith struct acpi_powerresource *rp; 49879357Smsmith ACPI_HANDLE res; 49979357Smsmith ACPI_STATUS status; 50078915Smsmith 50196926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 50278915Smsmith 503128047Snjl res = acpi_GetReference(NULL, obj); 504128047Snjl if (res == NULL) { 505119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 506128047Snjl "can't create a power reference for object type %d\n", 507119529Snjl obj->Type)); 50879357Smsmith return_VOID; 50979357Smsmith } 51078915Smsmith 511119529Snjl /* Create/look up the resource */ 51279357Smsmith if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) { 513119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 514119529Snjl "couldn't register power resource %s - %s\n", 515119529Snjl obj->String.Pointer, AcpiFormatException(status))); 51679357Smsmith return_VOID; 51779357Smsmith } 51879357Smsmith if ((rp = acpi_pwr_find_resource(res)) == NULL) { 51982372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n")); 52079357Smsmith return_VOID; 52179357Smsmith } 522119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n", 523119529Snjl acpi_name(rp->ap_resource))); 52479357Smsmith 525119529Snjl /* Create a reference between the consumer and resource */ 52679357Smsmith if ((pr = malloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 527119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 528119529Snjl "allocation failed for a power consumer reference\n")); 52979357Smsmith return_VOID; 53079357Smsmith } 53179357Smsmith pr->ar_consumer = pc; 53279357Smsmith pr->ar_resource = rp; 53379357Smsmith TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink); 53479357Smsmith TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink); 53579357Smsmith 53678915Smsmith return_VOID; 53778915Smsmith} 53878915Smsmith 53978915Smsmith 54078915Smsmith/* 54178915Smsmith * Switch power resources to conform to the desired state. 54278915Smsmith * 54378915Smsmith * Consumers may have modified the power resource list in an arbitrary 54478915Smsmith * fashion; we sweep it in sequence order. 54578915Smsmith */ 54678915Smsmithstatic ACPI_STATUS 54778915Smsmithacpi_pwr_switch_power(void) 54878915Smsmith{ 54978915Smsmith struct acpi_powerresource *rp; 55078915Smsmith ACPI_STATUS status; 55178915Smsmith int cur; 55278915Smsmith 55396926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 55478915Smsmith 55578915Smsmith /* 55678915Smsmith * Sweep the list forwards turning things on. 55778915Smsmith */ 55878915Smsmith TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 55979357Smsmith if (TAILQ_FIRST(&rp->ap_references) == NULL) { 560119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 561119529Snjl "%s has no references, not turning on\n", 562119529Snjl acpi_name(rp->ap_resource))); 56379357Smsmith continue; 56479357Smsmith } 56578915Smsmith 566119529Snjl /* We could cache this if we trusted it not to change under us */ 567126560Snjl status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 568119529Snjl if (ACPI_FAILURE(status)) { 56982372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 57082372Smsmith acpi_name(rp->ap_resource), status)); 571119529Snjl 572119529Snjl /* XXX is this correct? Always switch if in doubt? */ 573119529Snjl continue; 57478915Smsmith } 57578915Smsmith 57678915Smsmith /* 57778915Smsmith * Switch if required. Note that we ignore the result of the switch 57878915Smsmith * effort; we don't know what to do if it fails, so checking wouldn't 57978915Smsmith * help much. 58078915Smsmith */ 58179357Smsmith if (cur != ACPI_PWR_ON) { 582119529Snjl status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL); 583119529Snjl if (ACPI_FAILURE(status)) { 584119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 585119529Snjl "failed to switch %s on - %s\n", 586119529Snjl acpi_name(rp->ap_resource), 587119529Snjl AcpiFormatException(status))); 58879357Smsmith } else { 589119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n", 590119529Snjl acpi_name(rp->ap_resource))); 59179357Smsmith } 59279357Smsmith } else { 593119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n", 594119529Snjl acpi_name(rp->ap_resource))); 59579357Smsmith } 59678915Smsmith } 59778915Smsmith 598119529Snjl /* Sweep the list backwards turning things off. */ 599119529Snjl TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list, 600119529Snjl ap_link) { 601119529Snjl 60279357Smsmith if (TAILQ_FIRST(&rp->ap_references) != NULL) { 603119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 604119529Snjl "%s has references, not turning off\n", 605119529Snjl acpi_name(rp->ap_resource))); 60679357Smsmith continue; 60779357Smsmith } 60878915Smsmith 609119529Snjl /* We could cache this if we trusted it not to change under us */ 610126560Snjl status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 611119529Snjl if (ACPI_FAILURE(status)) { 61282372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 61382372Smsmith acpi_name(rp->ap_resource), status)); 614119529Snjl /* XXX is this correct? Always switch if in doubt? */ 615119529Snjl continue; 61678915Smsmith } 61778915Smsmith 61878915Smsmith /* 61978915Smsmith * Switch if required. Note that we ignore the result of the switch 62078915Smsmith * effort; we don't know what to do if it fails, so checking wouldn't 62178915Smsmith * help much. 62278915Smsmith */ 62379357Smsmith if (cur != ACPI_PWR_OFF) { 624119529Snjl status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL); 625119529Snjl if (ACPI_FAILURE(status)) { 626119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 627119529Snjl "failed to switch %s off - %s\n", 628119529Snjl acpi_name(rp->ap_resource), 629119529Snjl AcpiFormatException(status))); 63079357Smsmith } else { 631119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n", 632119529Snjl acpi_name(rp->ap_resource))); 63379357Smsmith } 63479357Smsmith } else { 635119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n", 636119529Snjl acpi_name(rp->ap_resource))); 63779357Smsmith } 63878915Smsmith } 639119529Snjl 640119529Snjl return_ACPI_STATUS (AE_OK); 64178915Smsmith} 64278915Smsmith 64378915Smsmith/* 64478915Smsmith * Find a power resource's control structure. 64578915Smsmith */ 64678915Smsmithstatic struct acpi_powerresource * 64778915Smsmithacpi_pwr_find_resource(ACPI_HANDLE res) 64878915Smsmith{ 64978915Smsmith struct acpi_powerresource *rp; 65078915Smsmith 65196926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 65278915Smsmith 653119529Snjl TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 65478915Smsmith if (rp->ap_resource == res) 65578915Smsmith break; 656119529Snjl } 657119529Snjl 658119529Snjl return_PTR (rp); 65978915Smsmith} 66078915Smsmith 66178915Smsmith/* 66278915Smsmith * Find a power consumer's control structure. 66378915Smsmith */ 66478915Smsmithstatic struct acpi_powerconsumer * 66578915Smsmithacpi_pwr_find_consumer(ACPI_HANDLE consumer) 66678915Smsmith{ 66778915Smsmith struct acpi_powerconsumer *pc; 66878915Smsmith 66996926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 67078915Smsmith 671119529Snjl TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) { 67278915Smsmith if (pc->ac_consumer == consumer) 67378915Smsmith break; 674119529Snjl } 675119529Snjl 676119529Snjl return_PTR (pc); 67778915Smsmith} 678