acpi_powerres.c revision 157947
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 157947 2006-04-21 19:06:29Z jkim $"); 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 36150003Sobrien#include <contrib/dev/acpica/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 5678915SmsmithMALLOC_DEFINE(M_ACPIPWR, "acpipwr", "ACPI power resources"); 5778915Smsmith 58119529Snjl/* Hooks for the ACPI CA debugging infrastructure */ 59126517Snjl#define _COMPONENT ACPI_POWERRES 6091125SmsmithACPI_MODULE_NAME("POWERRES") 6178915Smsmith 62119529Snjl/* Return values from _STA on a power resource */ 6379357Smsmith#define ACPI_PWR_OFF 0 6479357Smsmith#define ACPI_PWR_ON 1 65134908Snjl#define ACPI_PWR_UNK (-1) 6679357Smsmith 67119529Snjl/* A relationship between a power resource and a consumer. */ 6878915Smsmithstruct acpi_powerreference { 69119529Snjl struct acpi_powerconsumer *ar_consumer; 70119529Snjl struct acpi_powerresource *ar_resource; 71119529Snjl TAILQ_ENTRY(acpi_powerreference) ar_rlink; /* link on resource list */ 72119529Snjl TAILQ_ENTRY(acpi_powerreference) ar_clink; /* link on consumer */ 7378915Smsmith}; 7478915Smsmith 75119529Snjl/* A power-managed device. */ 7678915Smsmithstruct acpi_powerconsumer { 77119529Snjl /* Device which is powered */ 78119529Snjl ACPI_HANDLE ac_consumer; 79119529Snjl int ac_state; 80119529Snjl TAILQ_ENTRY(acpi_powerconsumer) ac_link; 81119529Snjl TAILQ_HEAD(,acpi_powerreference) ac_references; 8278915Smsmith}; 8378915Smsmith 84119529Snjl/* A power resource. */ 8578915Smsmithstruct acpi_powerresource { 86119529Snjl TAILQ_ENTRY(acpi_powerresource) ap_link; 87119529Snjl TAILQ_HEAD(,acpi_powerreference) ap_references; 88119529Snjl ACPI_HANDLE ap_resource; 89119529Snjl ACPI_INTEGER ap_systemlevel; 90119529Snjl ACPI_INTEGER ap_order; 91134908Snjl int ap_state; 9278915Smsmith}; 9378915Smsmith 94119529Snjlstatic TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource) 95119529Snjl acpi_powerresources; 96119529Snjlstatic TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer) 97119529Snjl acpi_powerconsumers; 98133622SnjlACPI_SERIAL_DECL(powerres, "ACPI power resources"); 9978915Smsmith 100119529Snjlstatic ACPI_STATUS acpi_pwr_register_consumer(ACPI_HANDLE consumer); 101128248Snjl#ifdef notyet 102119529Snjlstatic ACPI_STATUS acpi_pwr_deregister_consumer(ACPI_HANDLE consumer); 103128248Snjl#endif /* notyet */ 104119529Snjlstatic ACPI_STATUS acpi_pwr_register_resource(ACPI_HANDLE res); 105128248Snjl#ifdef notyet 106119529Snjlstatic ACPI_STATUS acpi_pwr_deregister_resource(ACPI_HANDLE res); 107128248Snjl#endif /* notyet */ 108119529Snjlstatic void acpi_pwr_reference_resource(ACPI_OBJECT *obj, 109119529Snjl void *arg); 110131340Snjlstatic int acpi_pwr_dereference_resource(struct acpi_powerconsumer 111131340Snjl *pc); 112119529Snjlstatic ACPI_STATUS acpi_pwr_switch_power(void); 113119529Snjlstatic struct acpi_powerresource 114119529Snjl *acpi_pwr_find_resource(ACPI_HANDLE res); 115119529Snjlstatic struct acpi_powerconsumer 116119529Snjl *acpi_pwr_find_consumer(ACPI_HANDLE consumer); 11778915Smsmith 118119529Snjl/* Initialise our lists. */ 11978915Smsmithstatic void 12078915Smsmithacpi_pwr_init(void *junk) 12178915Smsmith{ 12278915Smsmith TAILQ_INIT(&acpi_powerresources); 12378915Smsmith TAILQ_INIT(&acpi_powerconsumers); 12478915Smsmith} 12578915SmsmithSYSINIT(acpi_powerresource, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_pwr_init, NULL); 12678915Smsmith 12778915Smsmith/* 12878915Smsmith * Register a power resource. 12978915Smsmith * 13078915Smsmith * It's OK to call this if we already know about the resource. 13178915Smsmith */ 13278915Smsmithstatic ACPI_STATUS 13378915Smsmithacpi_pwr_register_resource(ACPI_HANDLE res) 13478915Smsmith{ 13578915Smsmith ACPI_STATUS status; 13678915Smsmith ACPI_BUFFER buf; 13779493Smsmith ACPI_OBJECT *obj; 13878915Smsmith struct acpi_powerresource *rp, *srp; 13978915Smsmith 14096926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 141133622Snjl ACPI_SERIAL_ASSERT(powerres); 14278915Smsmith 14378915Smsmith rp = NULL; 14491125Smsmith buf.Pointer = NULL; 14578915Smsmith 146119529Snjl /* Look to see if we know about this resource */ 14778915Smsmith if (acpi_pwr_find_resource(res) != NULL) 148119529Snjl return_ACPI_STATUS (AE_OK); /* already know about it */ 14978915Smsmith 150119529Snjl /* Allocate a new resource */ 15178915Smsmith if ((rp = malloc(sizeof(*rp), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 15278915Smsmith status = AE_NO_MEMORY; 15378915Smsmith goto out; 15478915Smsmith } 15578915Smsmith TAILQ_INIT(&rp->ap_references); 15678915Smsmith rp->ap_resource = res; 15778915Smsmith 158119529Snjl /* Get the Power Resource object */ 15991125Smsmith buf.Length = ACPI_ALLOCATE_BUFFER; 16091125Smsmith if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) { 16182372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n")); 16278915Smsmith goto out; 16378915Smsmith } 16478915Smsmith obj = buf.Pointer; 16579493Smsmith if (obj->Type != ACPI_TYPE_POWER) { 166119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 167119529Snjl "questionable power resource object %s\n", 168119529Snjl acpi_name(res))); 16979493Smsmith status = AE_TYPE; 17079493Smsmith goto out; 17178915Smsmith } 17279493Smsmith rp->ap_systemlevel = obj->PowerResource.SystemLevel; 17379493Smsmith rp->ap_order = obj->PowerResource.ResourceOrder; 174134908Snjl rp->ap_state = ACPI_PWR_UNK; 17578915Smsmith 176119529Snjl /* Sort the resource into the list */ 17778915Smsmith status = AE_OK; 17878915Smsmith srp = TAILQ_FIRST(&acpi_powerresources); 179119529Snjl if (srp == NULL || rp->ap_order < srp->ap_order) { 18078915Smsmith TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link); 18179357Smsmith goto done; 18278915Smsmith } 183119529Snjl TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) { 18478915Smsmith if (rp->ap_order < srp->ap_order) { 18578915Smsmith TAILQ_INSERT_BEFORE(srp, rp, ap_link); 18679357Smsmith goto done; 18778915Smsmith } 188119529Snjl } 18978915Smsmith TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link); 19079357Smsmith 19179357Smsmith done: 192119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 193119529Snjl "registered power resource %s\n", acpi_name(res))); 194119529Snjl 19578915Smsmith out: 19691125Smsmith if (buf.Pointer != NULL) 19791125Smsmith AcpiOsFree(buf.Pointer); 198119529Snjl if (ACPI_FAILURE(status) && rp != NULL) 19978915Smsmith free(rp, M_ACPIPWR); 200119529Snjl return_ACPI_STATUS (status); 20178915Smsmith} 20278915Smsmith 203128248Snjl#ifdef notyet 20478915Smsmith/* 20578915Smsmith * Deregister a power resource. 20678915Smsmith */ 20778915Smsmithstatic ACPI_STATUS 20878915Smsmithacpi_pwr_deregister_resource(ACPI_HANDLE res) 20978915Smsmith{ 21078915Smsmith struct acpi_powerresource *rp; 21178915Smsmith 21296926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 213133622Snjl ACPI_SERIAL_ASSERT(powerres); 21478915Smsmith 21578915Smsmith rp = NULL; 21678915Smsmith 217119529Snjl /* Find the resource */ 21878915Smsmith if ((rp = acpi_pwr_find_resource(res)) == NULL) 219119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 22078915Smsmith 221119529Snjl /* Check that there are no consumers referencing this resource */ 22278915Smsmith if (TAILQ_FIRST(&rp->ap_references) != NULL) 223119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 22478915Smsmith 225119529Snjl /* Pull it off the list and free it */ 22678915Smsmith TAILQ_REMOVE(&acpi_powerresources, rp, ap_link); 22778915Smsmith free(rp, M_ACPIPWR); 22878915Smsmith 229119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n", 230119529Snjl acpi_name(res))); 23178915Smsmith 232119529Snjl return_ACPI_STATUS (AE_OK); 23378915Smsmith} 234128248Snjl#endif /* notyet */ 23578915Smsmith 23678915Smsmith/* 23778915Smsmith * Register a power consumer. 23878915Smsmith * 23978915Smsmith * It's OK to call this if we already know about the consumer. 24078915Smsmith */ 24178915Smsmithstatic ACPI_STATUS 24278915Smsmithacpi_pwr_register_consumer(ACPI_HANDLE consumer) 24378915Smsmith{ 24478915Smsmith struct acpi_powerconsumer *pc; 24578915Smsmith 24696926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 247133622Snjl ACPI_SERIAL_ASSERT(powerres); 24878915Smsmith 249119529Snjl /* Check to see whether we know about this consumer already */ 250157947Sjkim if (acpi_pwr_find_consumer(consumer) != NULL) 251119529Snjl return_ACPI_STATUS (AE_OK); 25278915Smsmith 253119529Snjl /* Allocate a new power consumer */ 25478915Smsmith if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL) 255119529Snjl return_ACPI_STATUS (AE_NO_MEMORY); 25678915Smsmith TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link); 25778915Smsmith TAILQ_INIT(&pc->ac_references); 25878915Smsmith pc->ac_consumer = consumer; 25978915Smsmith 260119529Snjl /* XXX we should try to find its current state */ 261119529Snjl pc->ac_state = ACPI_STATE_UNKNOWN; 26278915Smsmith 263119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n", 264119529Snjl acpi_name(consumer))); 26578915Smsmith 266119529Snjl return_ACPI_STATUS (AE_OK); 26778915Smsmith} 26878915Smsmith 269128248Snjl#ifdef notyet 27078915Smsmith/* 27178915Smsmith * Deregister a power consumer. 27278915Smsmith * 27378915Smsmith * This should only be done once the consumer has been powered off. 27478915Smsmith * (XXX is this correct? Check once implemented) 27578915Smsmith */ 27678915Smsmithstatic ACPI_STATUS 27778915Smsmithacpi_pwr_deregister_consumer(ACPI_HANDLE consumer) 27878915Smsmith{ 27978915Smsmith struct acpi_powerconsumer *pc; 28078915Smsmith 28196926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 282133622Snjl ACPI_SERIAL_ASSERT(powerres); 28378915Smsmith 284119529Snjl /* Find the consumer */ 28578915Smsmith if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 286119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 28778915Smsmith 288119529Snjl /* Make sure the consumer's not referencing anything right now */ 28978915Smsmith if (TAILQ_FIRST(&pc->ac_references) != NULL) 290119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 29178915Smsmith 292119529Snjl /* Pull the consumer off the list and free it */ 29378915Smsmith TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link); 294133622Snjl free(pc, M_ACPIPWR); 29578915Smsmith 296119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n", 297119529Snjl acpi_name(consumer))); 29878915Smsmith 299119529Snjl return_ACPI_STATUS (AE_OK); 30078915Smsmith} 301128248Snjl#endif /* notyet */ 30278915Smsmith 30378915Smsmith/* 30478915Smsmith * Set a power consumer to a particular power state. 30578915Smsmith */ 30678915SmsmithACPI_STATUS 30778915Smsmithacpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state) 30878915Smsmith{ 30978915Smsmith struct acpi_powerconsumer *pc; 31082084Siwasaki ACPI_HANDLE method_handle, reslist_handle, pr0_handle; 31178915Smsmith ACPI_BUFFER reslist_buffer; 31278915Smsmith ACPI_OBJECT *reslist_object; 31378915Smsmith ACPI_STATUS status; 31478915Smsmith char *method_name, *reslist_name; 31578915Smsmith int res_changed; 31678915Smsmith 31796926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 31878915Smsmith 319128252Snjl /* It's never ok to switch a non-existent consumer. */ 320128252Snjl if (consumer == NULL) 321128252Snjl return_ACPI_STATUS (AE_NOT_FOUND); 322133622Snjl reslist_buffer.Pointer = NULL; 323133622Snjl reslist_object = NULL; 324133622Snjl ACPI_SERIAL_BEGIN(powerres); 325128252Snjl 326119529Snjl /* Find the consumer */ 32778915Smsmith if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 32891125Smsmith if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) 329133622Snjl goto out; 330133622Snjl if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 331133622Snjl panic("acpi added power consumer but can't find it"); 33278915Smsmith } 33378915Smsmith 334133622Snjl /* Check for valid transitions. We can only go to D0 from D3. */ 335133622Snjl status = AE_BAD_PARAMETER; 336119529Snjl if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0) 337133622Snjl goto out; 33878915Smsmith 339119529Snjl /* Find transition mechanism(s) */ 340130208Snjl switch (state) { 34178915Smsmith case ACPI_STATE_D0: 34278915Smsmith method_name = "_PS0"; 34378915Smsmith reslist_name = "_PR0"; 34478915Smsmith break; 34578915Smsmith case ACPI_STATE_D1: 34678915Smsmith method_name = "_PS1"; 34778915Smsmith reslist_name = "_PR1"; 34878915Smsmith break; 34978915Smsmith case ACPI_STATE_D2: 35078915Smsmith method_name = "_PS2"; 35178915Smsmith reslist_name = "_PR2"; 35278915Smsmith break; 35378915Smsmith case ACPI_STATE_D3: 35478915Smsmith method_name = "_PS3"; 35578915Smsmith reslist_name = "_PR3"; 35678915Smsmith break; 35778915Smsmith default: 358133622Snjl goto out; 35978915Smsmith } 36082372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n", 361119529Snjl acpi_name(consumer), pc->ac_state, state)); 36278915Smsmith 36378915Smsmith /* 36478915Smsmith * Verify that this state is supported, ie. one of method or 36578915Smsmith * reslist must be present. We need to do this before we go 36678915Smsmith * dereferencing resources (since we might be trying to go to 36778915Smsmith * a state we don't support). 36878915Smsmith * 36978915Smsmith * Note that if any states are supported, the device has to 37078915Smsmith * support D0 and D3. It's never an error to try to go to 37178915Smsmith * D0. 37278915Smsmith */ 37391125Smsmith if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle))) 37478915Smsmith method_handle = NULL; 37591125Smsmith if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle))) 37678915Smsmith reslist_handle = NULL; 377119529Snjl if (reslist_handle == NULL && method_handle == NULL) { 37878915Smsmith if (state == ACPI_STATE_D0) { 37978915Smsmith pc->ac_state = ACPI_STATE_D0; 380133622Snjl status = AE_OK; 381133622Snjl goto out; 38278915Smsmith } 383133622Snjl if (state != ACPI_STATE_D3) { 384133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 385133622Snjl "attempt to set unsupported state D%d\n", state)); 386133622Snjl goto out; 387133622Snjl } 38882084Siwasaki 389130208Snjl /* 390130208Snjl * Turn off the resources listed in _PR0 to go to D3. If there is 391130208Snjl * no _PR0 method, this object doesn't support ACPI power states. 392130208Snjl */ 393130208Snjl if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle))) { 394130208Snjl status = AE_NOT_FOUND; 395133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 396133622Snjl "device missing _PR0 (desired state was D%d)\n", state)); 397133622Snjl goto out; 398130208Snjl } 39991125Smsmith reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 400119529Snjl status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer); 401133622Snjl if (ACPI_FAILURE(status)) { 402133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 403133622Snjl "can't evaluate _PR0 for device %s, state D%d\n", 404133622Snjl acpi_name(consumer), state)); 405133622Snjl goto out; 406133622Snjl } 40782084Siwasaki reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 408133622Snjl if (!ACPI_PKG_VALID(reslist_object, 1)) { 409133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 410133622Snjl "invalid package object for state D%d\n", state)); 411133622Snjl status = AE_TYPE; 412133622Snjl goto out; 413133622Snjl } 41491125Smsmith AcpiOsFree(reslist_buffer.Pointer); 41591125Smsmith reslist_buffer.Pointer = NULL; 41691125Smsmith reslist_object = NULL; 41778915Smsmith } 41878915Smsmith 41978915Smsmith /* 42078915Smsmith * Check that we can actually fetch the list of power resources 42178915Smsmith */ 42278915Smsmith if (reslist_handle != NULL) { 42391125Smsmith reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 424119529Snjl status = AcpiEvaluateObject(reslist_handle, NULL, NULL, 425119529Snjl &reslist_buffer); 426119529Snjl if (ACPI_FAILURE(status)) { 427119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 428119529Snjl "can't evaluate resource list %s\n", 429119529Snjl acpi_name(reslist_handle))); 43091125Smsmith goto out; 43178915Smsmith } 43278915Smsmith reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 43378915Smsmith if (reslist_object->Type != ACPI_TYPE_PACKAGE) { 434119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 435119529Snjl "resource list is not ACPI_TYPE_PACKAGE (%d)\n", 436119529Snjl reslist_object->Type)); 43791125Smsmith status = AE_TYPE; 43891125Smsmith goto out; 43978915Smsmith } 44078915Smsmith } 44178915Smsmith 44278915Smsmith /* 443125745Sjhb * Now we are ready to switch, so kill off any current power 444119529Snjl * resource references. 44578915Smsmith */ 446131340Snjl res_changed = acpi_pwr_dereference_resource(pc); 44778915Smsmith 44878915Smsmith /* 44978915Smsmith * Add new power resource references, if we have any. Traverse the 45078915Smsmith * package that we got from evaluating reslist_handle, and look up each 45178915Smsmith * of the resources that are referenced. 45278915Smsmith */ 45378915Smsmith if (reslist_object != NULL) { 45482372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n", 45582372Smsmith reslist_object->Package.Count)); 456119529Snjl acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource, 457119529Snjl pc); 45878915Smsmith res_changed = 1; 45978915Smsmith } 46078915Smsmith 46178915Smsmith /* 46278915Smsmith * If we changed anything in the resource list, we need to run a switch 46378915Smsmith * pass now. 46478915Smsmith */ 46591125Smsmith if (ACPI_FAILURE(status = acpi_pwr_switch_power())) { 466119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 467119529Snjl "failed to switch resources from %s to D%d\n", 46882372Smsmith acpi_name(consumer), state)); 469119529Snjl 470119529Snjl /* XXX is this appropriate? Should we return to previous state? */ 471139339Snjl goto out; 47278915Smsmith } 47378915Smsmith 474119529Snjl /* Invoke power state switch method (if present) */ 47578915Smsmith if (method_handle != NULL) { 476119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 477119529Snjl "invoking state transition method %s\n", 478119529Snjl acpi_name(method_handle))); 479119529Snjl status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL); 480119529Snjl if (ACPI_FAILURE(status)) { 48191125Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n", 482119529Snjl AcpiFormatException(status))); 48391125Smsmith pc->ac_state = ACPI_STATE_UNKNOWN; 484119529Snjl 485119529Snjl /* XXX Should we return to previous state? */ 486119529Snjl goto out; 48791125Smsmith } 48878915Smsmith } 489139339Snjl 490119529Snjl /* Transition was successful */ 49178915Smsmith pc->ac_state = state; 492133622Snjl status = AE_OK; 49382084Siwasaki 494133622Snjlout: 495133622Snjl ACPI_SERIAL_END(powerres); 49691125Smsmith if (reslist_buffer.Pointer != NULL) 49791125Smsmith AcpiOsFree(reslist_buffer.Pointer); 498119529Snjl return_ACPI_STATUS (status); 49978915Smsmith} 50078915Smsmith 501131340Snjl/* Enable or disable a power resource for wake */ 502131340SnjlACPI_STATUS 503131340Snjlacpi_pwr_wake_enable(ACPI_HANDLE consumer, int enable) 504131340Snjl{ 505131340Snjl ACPI_STATUS status; 506131340Snjl struct acpi_powerconsumer *pc; 507131340Snjl struct acpi_prw_data prw; 508131340Snjl int i; 509131340Snjl 510131366Snjl ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 511131366Snjl 512131340Snjl if (consumer == NULL) 513131340Snjl return (AE_BAD_PARAMETER); 514131340Snjl 515133622Snjl ACPI_SERIAL_BEGIN(powerres); 516131340Snjl if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 517131340Snjl if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) 518133622Snjl goto out; 519133622Snjl if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 520133622Snjl panic("acpi wake added power consumer but can't find it"); 521131340Snjl } 522131340Snjl 523133622Snjl status = AE_OK; 524131340Snjl if (acpi_parse_prw(consumer, &prw) != 0) 525133622Snjl goto out; 526131340Snjl for (i = 0; i < prw.power_res_count; i++) 527131340Snjl if (enable) 528131340Snjl acpi_pwr_reference_resource(&prw.power_res[i], pc); 529131340Snjl else 530131340Snjl acpi_pwr_dereference_resource(pc); 531131340Snjl 532131340Snjl if (prw.power_res_count > 0) 533131340Snjl acpi_pwr_switch_power(); 534131340Snjl 535133622Snjlout: 536133622Snjl ACPI_SERIAL_END(powerres); 537133622Snjl return (status); 538131340Snjl} 539131340Snjl 54078915Smsmith/* 54178915Smsmith * Called to create a reference between a power consumer and a power resource 54278915Smsmith * identified in the object. 54378915Smsmith */ 54478915Smsmithstatic void 54578915Smsmithacpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg) 54678915Smsmith{ 54778915Smsmith struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg; 54879357Smsmith struct acpi_powerreference *pr; 54979357Smsmith struct acpi_powerresource *rp; 55079357Smsmith ACPI_HANDLE res; 55179357Smsmith ACPI_STATUS status; 55278915Smsmith 55396926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 554133622Snjl ACPI_SERIAL_ASSERT(powerres); 55578915Smsmith 556128047Snjl res = acpi_GetReference(NULL, obj); 557128047Snjl if (res == NULL) { 558119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 559128047Snjl "can't create a power reference for object type %d\n", 560119529Snjl obj->Type)); 56179357Smsmith return_VOID; 56279357Smsmith } 56378915Smsmith 564119529Snjl /* Create/look up the resource */ 56579357Smsmith if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) { 566119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 567119529Snjl "couldn't register power resource %s - %s\n", 568119529Snjl obj->String.Pointer, AcpiFormatException(status))); 56979357Smsmith return_VOID; 57079357Smsmith } 57179357Smsmith if ((rp = acpi_pwr_find_resource(res)) == NULL) { 57282372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n")); 57379357Smsmith return_VOID; 57479357Smsmith } 575119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n", 576119529Snjl acpi_name(rp->ap_resource))); 57779357Smsmith 578119529Snjl /* Create a reference between the consumer and resource */ 57979357Smsmith if ((pr = malloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 580119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 581119529Snjl "allocation failed for a power consumer reference\n")); 58279357Smsmith return_VOID; 58379357Smsmith } 58479357Smsmith pr->ar_consumer = pc; 58579357Smsmith pr->ar_resource = rp; 58679357Smsmith TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink); 58779357Smsmith TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink); 588131340Snjl 58978915Smsmith return_VOID; 59078915Smsmith} 59178915Smsmith 592131340Snjlstatic int 593131340Snjlacpi_pwr_dereference_resource(struct acpi_powerconsumer *pc) 594131340Snjl{ 595131340Snjl struct acpi_powerreference *pr; 596131340Snjl int changed; 59778915Smsmith 598131366Snjl ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 599133622Snjl ACPI_SERIAL_ASSERT(powerres); 600131366Snjl 601131340Snjl changed = 0; 602131340Snjl while ((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) { 603131340Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n", 604131340Snjl acpi_name(pr->ar_resource->ap_resource))); 605131340Snjl TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink); 606131340Snjl TAILQ_REMOVE(&pc->ac_references, pr, ar_clink); 607131340Snjl free(pr, M_ACPIPWR); 608131340Snjl changed = 1; 609131340Snjl } 610131340Snjl 611131340Snjl return (changed); 612131340Snjl} 613131340Snjl 61478915Smsmith/* 61578915Smsmith * Switch power resources to conform to the desired state. 61678915Smsmith * 61778915Smsmith * Consumers may have modified the power resource list in an arbitrary 61878915Smsmith * fashion; we sweep it in sequence order. 61978915Smsmith */ 62078915Smsmithstatic ACPI_STATUS 62178915Smsmithacpi_pwr_switch_power(void) 62278915Smsmith{ 62378915Smsmith struct acpi_powerresource *rp; 62478915Smsmith ACPI_STATUS status; 62578915Smsmith int cur; 62678915Smsmith 62796926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 628133622Snjl ACPI_SERIAL_ASSERT(powerres); 62978915Smsmith 63078915Smsmith /* 63178915Smsmith * Sweep the list forwards turning things on. 63278915Smsmith */ 63378915Smsmith TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 63479357Smsmith if (TAILQ_FIRST(&rp->ap_references) == NULL) { 635119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 636119529Snjl "%s has no references, not turning on\n", 637119529Snjl acpi_name(rp->ap_resource))); 63879357Smsmith continue; 63979357Smsmith } 64078915Smsmith 641119529Snjl /* We could cache this if we trusted it not to change under us */ 642126560Snjl status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 643119529Snjl if (ACPI_FAILURE(status)) { 64482372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 64582372Smsmith acpi_name(rp->ap_resource), status)); 646119529Snjl /* XXX is this correct? Always switch if in doubt? */ 647119529Snjl continue; 648134908Snjl } else if (rp->ap_state == ACPI_PWR_UNK) 649134908Snjl rp->ap_state = cur; 65078915Smsmith 65178915Smsmith /* 65278915Smsmith * Switch if required. Note that we ignore the result of the switch 65378915Smsmith * effort; we don't know what to do if it fails, so checking wouldn't 65478915Smsmith * help much. 65578915Smsmith */ 656134908Snjl if (rp->ap_state != ACPI_PWR_ON) { 657119529Snjl status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL); 658119529Snjl if (ACPI_FAILURE(status)) { 659119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 660119529Snjl "failed to switch %s on - %s\n", 661119529Snjl acpi_name(rp->ap_resource), 662119529Snjl AcpiFormatException(status))); 66379357Smsmith } else { 664134908Snjl rp->ap_state = ACPI_PWR_ON; 665119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n", 666119529Snjl acpi_name(rp->ap_resource))); 66779357Smsmith } 66879357Smsmith } else { 669119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n", 670119529Snjl acpi_name(rp->ap_resource))); 67179357Smsmith } 67278915Smsmith } 67378915Smsmith 674119529Snjl /* Sweep the list backwards turning things off. */ 675119529Snjl TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list, 676119529Snjl ap_link) { 677119529Snjl 67879357Smsmith if (TAILQ_FIRST(&rp->ap_references) != NULL) { 679119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 680119529Snjl "%s has references, not turning off\n", 681119529Snjl acpi_name(rp->ap_resource))); 68279357Smsmith continue; 68379357Smsmith } 68478915Smsmith 685119529Snjl /* We could cache this if we trusted it not to change under us */ 686126560Snjl status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 687119529Snjl if (ACPI_FAILURE(status)) { 68882372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 68982372Smsmith acpi_name(rp->ap_resource), status)); 690119529Snjl /* XXX is this correct? Always switch if in doubt? */ 691119529Snjl continue; 692134908Snjl } else if (rp->ap_state == ACPI_PWR_UNK) 693134908Snjl rp->ap_state = cur; 69478915Smsmith 69578915Smsmith /* 69678915Smsmith * Switch if required. Note that we ignore the result of the switch 69778915Smsmith * effort; we don't know what to do if it fails, so checking wouldn't 69878915Smsmith * help much. 69978915Smsmith */ 700134908Snjl if (rp->ap_state != ACPI_PWR_OFF) { 701119529Snjl status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL); 702119529Snjl if (ACPI_FAILURE(status)) { 703119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 704119529Snjl "failed to switch %s off - %s\n", 705119529Snjl acpi_name(rp->ap_resource), 706119529Snjl AcpiFormatException(status))); 70779357Smsmith } else { 708134908Snjl rp->ap_state = ACPI_PWR_OFF; 709119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n", 710119529Snjl acpi_name(rp->ap_resource))); 71179357Smsmith } 71279357Smsmith } else { 713119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n", 714119529Snjl acpi_name(rp->ap_resource))); 71579357Smsmith } 71678915Smsmith } 717119529Snjl 718119529Snjl return_ACPI_STATUS (AE_OK); 71978915Smsmith} 72078915Smsmith 72178915Smsmith/* 72278915Smsmith * Find a power resource's control structure. 72378915Smsmith */ 72478915Smsmithstatic struct acpi_powerresource * 72578915Smsmithacpi_pwr_find_resource(ACPI_HANDLE res) 72678915Smsmith{ 72778915Smsmith struct acpi_powerresource *rp; 72878915Smsmith 72996926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 730133622Snjl ACPI_SERIAL_ASSERT(powerres); 73178915Smsmith 732119529Snjl TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 73378915Smsmith if (rp->ap_resource == res) 73478915Smsmith break; 735119529Snjl } 736119529Snjl 737119529Snjl return_PTR (rp); 73878915Smsmith} 73978915Smsmith 74078915Smsmith/* 74178915Smsmith * Find a power consumer's control structure. 74278915Smsmith */ 74378915Smsmithstatic struct acpi_powerconsumer * 74478915Smsmithacpi_pwr_find_consumer(ACPI_HANDLE consumer) 74578915Smsmith{ 74678915Smsmith struct acpi_powerconsumer *pc; 74778915Smsmith 74896926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 749133622Snjl ACPI_SERIAL_ASSERT(powerres); 75078915Smsmith 751119529Snjl TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) { 75278915Smsmith if (pc->ac_consumer == consumer) 75378915Smsmith break; 754119529Snjl } 755119529Snjl 756119529Snjl return_PTR (pc); 75778915Smsmith} 758