acpi_powerres.c revision 193530
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 193530 2009-06-05 18:44:36Z 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 36193530Sjkim#include <contrib/dev/acpica/include/acpi.h> 37193530Sjkim#include <contrib/dev/acpica/include/accommon.h> 38193530Sjkim 3978915Smsmith#include <dev/acpica/acpivar.h> 4078915Smsmith 4178915Smsmith/* 4278915Smsmith * ACPI power resource management. 4378915Smsmith * 4478915Smsmith * Power resource behaviour is slightly complicated by the fact that 4578915Smsmith * a single power resource may provide power for more than one device. 4678915Smsmith * Thus, we must track the device(s) being powered by a given power 4778915Smsmith * resource, and only deactivate it when there are no powered devices. 4878915Smsmith * 4978915Smsmith * Note that this only manages resources for known devices. There is an 5078915Smsmith * ugly case where we may turn of power to a device which is in use because 5178915Smsmith * we don't know that it depends on a given resource. We should perhaps 5278915Smsmith * try to be smarter about this, but a more complete solution would involve 5378915Smsmith * scanning all of the ACPI namespace to find devices we're not currently 5478915Smsmith * aware of, and this raises questions about whether they should be left 5578915Smsmith * on, turned off, etc. 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 67134908Snjl#define ACPI_PWR_UNK (-1) 6879357Smsmith 69119529Snjl/* A relationship between a power resource and a consumer. */ 7078915Smsmithstruct acpi_powerreference { 71119529Snjl struct acpi_powerconsumer *ar_consumer; 72119529Snjl struct acpi_powerresource *ar_resource; 73119529Snjl TAILQ_ENTRY(acpi_powerreference) ar_rlink; /* link on resource list */ 74119529Snjl TAILQ_ENTRY(acpi_powerreference) ar_clink; /* link on consumer */ 7578915Smsmith}; 7678915Smsmith 77119529Snjl/* A power-managed device. */ 7878915Smsmithstruct acpi_powerconsumer { 79119529Snjl /* Device which is powered */ 80119529Snjl ACPI_HANDLE ac_consumer; 81119529Snjl int ac_state; 82119529Snjl TAILQ_ENTRY(acpi_powerconsumer) ac_link; 83119529Snjl TAILQ_HEAD(,acpi_powerreference) ac_references; 8478915Smsmith}; 8578915Smsmith 86119529Snjl/* A power resource. */ 8778915Smsmithstruct acpi_powerresource { 88119529Snjl TAILQ_ENTRY(acpi_powerresource) ap_link; 89119529Snjl TAILQ_HEAD(,acpi_powerreference) ap_references; 90119529Snjl ACPI_HANDLE ap_resource; 91119529Snjl ACPI_INTEGER ap_systemlevel; 92119529Snjl ACPI_INTEGER ap_order; 93134908Snjl int ap_state; 9478915Smsmith}; 9578915Smsmith 96119529Snjlstatic TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource) 97119529Snjl acpi_powerresources; 98119529Snjlstatic TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer) 99119529Snjl acpi_powerconsumers; 100133622SnjlACPI_SERIAL_DECL(powerres, "ACPI power resources"); 10178915Smsmith 102119529Snjlstatic ACPI_STATUS acpi_pwr_register_consumer(ACPI_HANDLE consumer); 103128248Snjl#ifdef notyet 104119529Snjlstatic ACPI_STATUS acpi_pwr_deregister_consumer(ACPI_HANDLE consumer); 105128248Snjl#endif /* notyet */ 106119529Snjlstatic ACPI_STATUS acpi_pwr_register_resource(ACPI_HANDLE res); 107128248Snjl#ifdef notyet 108119529Snjlstatic ACPI_STATUS acpi_pwr_deregister_resource(ACPI_HANDLE res); 109128248Snjl#endif /* notyet */ 110119529Snjlstatic void acpi_pwr_reference_resource(ACPI_OBJECT *obj, 111119529Snjl void *arg); 112131340Snjlstatic int acpi_pwr_dereference_resource(struct acpi_powerconsumer 113131340Snjl *pc); 114119529Snjlstatic ACPI_STATUS acpi_pwr_switch_power(void); 115119529Snjlstatic struct acpi_powerresource 116119529Snjl *acpi_pwr_find_resource(ACPI_HANDLE res); 117119529Snjlstatic struct acpi_powerconsumer 118119529Snjl *acpi_pwr_find_consumer(ACPI_HANDLE consumer); 11978915Smsmith 120119529Snjl/* Initialise our lists. */ 12178915Smsmithstatic void 12278915Smsmithacpi_pwr_init(void *junk) 12378915Smsmith{ 12478915Smsmith TAILQ_INIT(&acpi_powerresources); 12578915Smsmith TAILQ_INIT(&acpi_powerconsumers); 12678915Smsmith} 12778915SmsmithSYSINIT(acpi_powerresource, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_pwr_init, NULL); 12878915Smsmith 12978915Smsmith/* 13078915Smsmith * Register a power resource. 13178915Smsmith * 13278915Smsmith * It's OK to call this if we already know about the resource. 13378915Smsmith */ 13478915Smsmithstatic ACPI_STATUS 13578915Smsmithacpi_pwr_register_resource(ACPI_HANDLE res) 13678915Smsmith{ 13778915Smsmith ACPI_STATUS status; 13878915Smsmith ACPI_BUFFER buf; 13979493Smsmith ACPI_OBJECT *obj; 14078915Smsmith struct acpi_powerresource *rp, *srp; 14178915Smsmith 14296926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 143133622Snjl ACPI_SERIAL_ASSERT(powerres); 14478915Smsmith 14578915Smsmith rp = NULL; 14691125Smsmith buf.Pointer = NULL; 14778915Smsmith 148119529Snjl /* Look to see if we know about this resource */ 14978915Smsmith if (acpi_pwr_find_resource(res) != NULL) 150119529Snjl return_ACPI_STATUS (AE_OK); /* already know about it */ 15178915Smsmith 152119529Snjl /* Allocate a new resource */ 15378915Smsmith if ((rp = malloc(sizeof(*rp), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 15478915Smsmith status = AE_NO_MEMORY; 15578915Smsmith goto out; 15678915Smsmith } 15778915Smsmith TAILQ_INIT(&rp->ap_references); 15878915Smsmith rp->ap_resource = res; 15978915Smsmith 160119529Snjl /* Get the Power Resource object */ 16191125Smsmith buf.Length = ACPI_ALLOCATE_BUFFER; 16291125Smsmith if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) { 16382372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n")); 16478915Smsmith goto out; 16578915Smsmith } 16678915Smsmith obj = buf.Pointer; 16779493Smsmith if (obj->Type != ACPI_TYPE_POWER) { 168119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 169119529Snjl "questionable power resource object %s\n", 170119529Snjl acpi_name(res))); 17179493Smsmith status = AE_TYPE; 17279493Smsmith goto out; 17378915Smsmith } 17479493Smsmith rp->ap_systemlevel = obj->PowerResource.SystemLevel; 17579493Smsmith rp->ap_order = obj->PowerResource.ResourceOrder; 176134908Snjl rp->ap_state = ACPI_PWR_UNK; 17778915Smsmith 178119529Snjl /* Sort the resource into the list */ 17978915Smsmith status = AE_OK; 18078915Smsmith srp = TAILQ_FIRST(&acpi_powerresources); 181119529Snjl if (srp == NULL || rp->ap_order < srp->ap_order) { 18278915Smsmith TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link); 18379357Smsmith goto done; 18478915Smsmith } 185119529Snjl TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) { 18678915Smsmith if (rp->ap_order < srp->ap_order) { 18778915Smsmith TAILQ_INSERT_BEFORE(srp, rp, ap_link); 18879357Smsmith goto done; 18978915Smsmith } 190119529Snjl } 19178915Smsmith TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link); 19279357Smsmith 19379357Smsmith done: 194119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 195119529Snjl "registered power resource %s\n", acpi_name(res))); 196119529Snjl 19778915Smsmith out: 19891125Smsmith if (buf.Pointer != NULL) 19991125Smsmith AcpiOsFree(buf.Pointer); 200119529Snjl if (ACPI_FAILURE(status) && rp != NULL) 20178915Smsmith free(rp, M_ACPIPWR); 202119529Snjl return_ACPI_STATUS (status); 20378915Smsmith} 20478915Smsmith 205128248Snjl#ifdef notyet 20678915Smsmith/* 20778915Smsmith * Deregister a power resource. 20878915Smsmith */ 20978915Smsmithstatic ACPI_STATUS 21078915Smsmithacpi_pwr_deregister_resource(ACPI_HANDLE res) 21178915Smsmith{ 21278915Smsmith struct acpi_powerresource *rp; 21378915Smsmith 21496926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 215133622Snjl ACPI_SERIAL_ASSERT(powerres); 21678915Smsmith 21778915Smsmith rp = NULL; 21878915Smsmith 219119529Snjl /* Find the resource */ 22078915Smsmith if ((rp = acpi_pwr_find_resource(res)) == NULL) 221119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 22278915Smsmith 223119529Snjl /* Check that there are no consumers referencing this resource */ 22478915Smsmith if (TAILQ_FIRST(&rp->ap_references) != NULL) 225119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 22678915Smsmith 227119529Snjl /* Pull it off the list and free it */ 22878915Smsmith TAILQ_REMOVE(&acpi_powerresources, rp, ap_link); 22978915Smsmith free(rp, M_ACPIPWR); 23078915Smsmith 231119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n", 232119529Snjl acpi_name(res))); 23378915Smsmith 234119529Snjl return_ACPI_STATUS (AE_OK); 23578915Smsmith} 236128248Snjl#endif /* notyet */ 23778915Smsmith 23878915Smsmith/* 23978915Smsmith * Register a power consumer. 24078915Smsmith * 24178915Smsmith * It's OK to call this if we already know about the consumer. 24278915Smsmith */ 24378915Smsmithstatic ACPI_STATUS 24478915Smsmithacpi_pwr_register_consumer(ACPI_HANDLE consumer) 24578915Smsmith{ 24678915Smsmith struct acpi_powerconsumer *pc; 24778915Smsmith 24896926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 249133622Snjl ACPI_SERIAL_ASSERT(powerres); 25078915Smsmith 251119529Snjl /* Check to see whether we know about this consumer already */ 252157947Sjkim if (acpi_pwr_find_consumer(consumer) != NULL) 253119529Snjl return_ACPI_STATUS (AE_OK); 25478915Smsmith 255119529Snjl /* Allocate a new power consumer */ 25678915Smsmith if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL) 257119529Snjl return_ACPI_STATUS (AE_NO_MEMORY); 25878915Smsmith TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link); 25978915Smsmith TAILQ_INIT(&pc->ac_references); 26078915Smsmith pc->ac_consumer = consumer; 26178915Smsmith 262119529Snjl /* XXX we should try to find its current state */ 263119529Snjl pc->ac_state = ACPI_STATE_UNKNOWN; 26478915Smsmith 265119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n", 266119529Snjl acpi_name(consumer))); 26778915Smsmith 268119529Snjl return_ACPI_STATUS (AE_OK); 26978915Smsmith} 27078915Smsmith 271128248Snjl#ifdef notyet 27278915Smsmith/* 27378915Smsmith * Deregister a power consumer. 27478915Smsmith * 27578915Smsmith * This should only be done once the consumer has been powered off. 27678915Smsmith * (XXX is this correct? Check once implemented) 27778915Smsmith */ 27878915Smsmithstatic ACPI_STATUS 27978915Smsmithacpi_pwr_deregister_consumer(ACPI_HANDLE consumer) 28078915Smsmith{ 28178915Smsmith struct acpi_powerconsumer *pc; 28278915Smsmith 28396926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 284133622Snjl ACPI_SERIAL_ASSERT(powerres); 28578915Smsmith 286119529Snjl /* Find the consumer */ 28778915Smsmith if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 288119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 28978915Smsmith 290119529Snjl /* Make sure the consumer's not referencing anything right now */ 29178915Smsmith if (TAILQ_FIRST(&pc->ac_references) != NULL) 292119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 29378915Smsmith 294119529Snjl /* Pull the consumer off the list and free it */ 29578915Smsmith TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link); 296133622Snjl free(pc, M_ACPIPWR); 29778915Smsmith 298119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n", 299119529Snjl acpi_name(consumer))); 30078915Smsmith 301119529Snjl return_ACPI_STATUS (AE_OK); 30278915Smsmith} 303128248Snjl#endif /* notyet */ 30478915Smsmith 30578915Smsmith/* 30678915Smsmith * Set a power consumer to a particular power state. 30778915Smsmith */ 30878915SmsmithACPI_STATUS 30978915Smsmithacpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state) 31078915Smsmith{ 31178915Smsmith struct acpi_powerconsumer *pc; 31282084Siwasaki ACPI_HANDLE method_handle, reslist_handle, pr0_handle; 31378915Smsmith ACPI_BUFFER reslist_buffer; 31478915Smsmith ACPI_OBJECT *reslist_object; 31578915Smsmith ACPI_STATUS status; 31678915Smsmith char *method_name, *reslist_name; 31778915Smsmith int res_changed; 31878915Smsmith 31996926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 32078915Smsmith 321128252Snjl /* It's never ok to switch a non-existent consumer. */ 322128252Snjl if (consumer == NULL) 323128252Snjl return_ACPI_STATUS (AE_NOT_FOUND); 324133622Snjl reslist_buffer.Pointer = NULL; 325133622Snjl reslist_object = NULL; 326133622Snjl ACPI_SERIAL_BEGIN(powerres); 327128252Snjl 328119529Snjl /* Find the consumer */ 32978915Smsmith if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 33091125Smsmith if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) 331133622Snjl goto out; 332133622Snjl if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 333133622Snjl panic("acpi added power consumer but can't find it"); 33478915Smsmith } 33578915Smsmith 336133622Snjl /* Check for valid transitions. We can only go to D0 from D3. */ 337133622Snjl status = AE_BAD_PARAMETER; 338119529Snjl if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0) 339133622Snjl goto out; 34078915Smsmith 341119529Snjl /* Find transition mechanism(s) */ 342130208Snjl switch (state) { 34378915Smsmith case ACPI_STATE_D0: 34478915Smsmith method_name = "_PS0"; 34578915Smsmith reslist_name = "_PR0"; 34678915Smsmith break; 34778915Smsmith case ACPI_STATE_D1: 34878915Smsmith method_name = "_PS1"; 34978915Smsmith reslist_name = "_PR1"; 35078915Smsmith break; 35178915Smsmith case ACPI_STATE_D2: 35278915Smsmith method_name = "_PS2"; 35378915Smsmith reslist_name = "_PR2"; 35478915Smsmith break; 35578915Smsmith case ACPI_STATE_D3: 35678915Smsmith method_name = "_PS3"; 35778915Smsmith reslist_name = "_PR3"; 35878915Smsmith break; 35978915Smsmith default: 360133622Snjl goto out; 36178915Smsmith } 36282372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n", 363119529Snjl acpi_name(consumer), pc->ac_state, state)); 36478915Smsmith 36578915Smsmith /* 36678915Smsmith * Verify that this state is supported, ie. one of method or 36778915Smsmith * reslist must be present. We need to do this before we go 36878915Smsmith * dereferencing resources (since we might be trying to go to 36978915Smsmith * a state we don't support). 37078915Smsmith * 37178915Smsmith * Note that if any states are supported, the device has to 37278915Smsmith * support D0 and D3. It's never an error to try to go to 37378915Smsmith * D0. 37478915Smsmith */ 37591125Smsmith if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle))) 37678915Smsmith method_handle = NULL; 37791125Smsmith if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle))) 37878915Smsmith reslist_handle = NULL; 379119529Snjl if (reslist_handle == NULL && method_handle == NULL) { 38078915Smsmith if (state == ACPI_STATE_D0) { 38178915Smsmith pc->ac_state = ACPI_STATE_D0; 382133622Snjl status = AE_OK; 383133622Snjl goto out; 38478915Smsmith } 385133622Snjl if (state != ACPI_STATE_D3) { 386133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 387133622Snjl "attempt to set unsupported state D%d\n", state)); 388133622Snjl goto out; 389133622Snjl } 39082084Siwasaki 391130208Snjl /* 392130208Snjl * Turn off the resources listed in _PR0 to go to D3. If there is 393130208Snjl * no _PR0 method, this object doesn't support ACPI power states. 394130208Snjl */ 395130208Snjl if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle))) { 396130208Snjl status = AE_NOT_FOUND; 397133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 398133622Snjl "device missing _PR0 (desired state was D%d)\n", state)); 399133622Snjl goto out; 400130208Snjl } 40191125Smsmith reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 402119529Snjl status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer); 403133622Snjl if (ACPI_FAILURE(status)) { 404133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 405133622Snjl "can't evaluate _PR0 for device %s, state D%d\n", 406133622Snjl acpi_name(consumer), state)); 407133622Snjl goto out; 408133622Snjl } 40982084Siwasaki reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 410133622Snjl if (!ACPI_PKG_VALID(reslist_object, 1)) { 411133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 412133622Snjl "invalid package object for state D%d\n", state)); 413133622Snjl status = AE_TYPE; 414133622Snjl goto out; 415133622Snjl } 41691125Smsmith AcpiOsFree(reslist_buffer.Pointer); 41791125Smsmith reslist_buffer.Pointer = NULL; 41891125Smsmith reslist_object = NULL; 41978915Smsmith } 42078915Smsmith 42178915Smsmith /* 42278915Smsmith * Check that we can actually fetch the list of power resources 42378915Smsmith */ 42478915Smsmith if (reslist_handle != NULL) { 42591125Smsmith reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 426119529Snjl status = AcpiEvaluateObject(reslist_handle, NULL, NULL, 427119529Snjl &reslist_buffer); 428119529Snjl if (ACPI_FAILURE(status)) { 429119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 430119529Snjl "can't evaluate resource list %s\n", 431119529Snjl acpi_name(reslist_handle))); 43291125Smsmith goto out; 43378915Smsmith } 43478915Smsmith reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 43578915Smsmith if (reslist_object->Type != ACPI_TYPE_PACKAGE) { 436119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 437119529Snjl "resource list is not ACPI_TYPE_PACKAGE (%d)\n", 438119529Snjl reslist_object->Type)); 43991125Smsmith status = AE_TYPE; 44091125Smsmith goto out; 44178915Smsmith } 44278915Smsmith } 44378915Smsmith 44478915Smsmith /* 445125745Sjhb * Now we are ready to switch, so kill off any current power 446119529Snjl * resource references. 44778915Smsmith */ 448131340Snjl res_changed = acpi_pwr_dereference_resource(pc); 44978915Smsmith 45078915Smsmith /* 45178915Smsmith * Add new power resource references, if we have any. Traverse the 45278915Smsmith * package that we got from evaluating reslist_handle, and look up each 45378915Smsmith * of the resources that are referenced. 45478915Smsmith */ 45578915Smsmith if (reslist_object != NULL) { 45682372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n", 45782372Smsmith reslist_object->Package.Count)); 458119529Snjl acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource, 459119529Snjl pc); 46078915Smsmith res_changed = 1; 46178915Smsmith } 46278915Smsmith 46378915Smsmith /* 46478915Smsmith * If we changed anything in the resource list, we need to run a switch 46578915Smsmith * pass now. 46678915Smsmith */ 46791125Smsmith if (ACPI_FAILURE(status = acpi_pwr_switch_power())) { 468119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 469119529Snjl "failed to switch resources from %s to D%d\n", 47082372Smsmith acpi_name(consumer), state)); 471119529Snjl 472119529Snjl /* XXX is this appropriate? Should we return to previous state? */ 473139339Snjl goto out; 47478915Smsmith } 47578915Smsmith 476119529Snjl /* Invoke power state switch method (if present) */ 47778915Smsmith if (method_handle != NULL) { 478119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 479119529Snjl "invoking state transition method %s\n", 480119529Snjl acpi_name(method_handle))); 481119529Snjl status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL); 482119529Snjl if (ACPI_FAILURE(status)) { 48391125Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n", 484119529Snjl AcpiFormatException(status))); 48591125Smsmith pc->ac_state = ACPI_STATE_UNKNOWN; 486119529Snjl 487119529Snjl /* XXX Should we return to previous state? */ 488119529Snjl goto out; 48991125Smsmith } 49078915Smsmith } 491139339Snjl 492119529Snjl /* Transition was successful */ 49378915Smsmith pc->ac_state = state; 494133622Snjl status = AE_OK; 49582084Siwasaki 496133622Snjlout: 497133622Snjl ACPI_SERIAL_END(powerres); 49891125Smsmith if (reslist_buffer.Pointer != NULL) 49991125Smsmith AcpiOsFree(reslist_buffer.Pointer); 500119529Snjl return_ACPI_STATUS (status); 50178915Smsmith} 50278915Smsmith 503131340Snjl/* Enable or disable a power resource for wake */ 504131340SnjlACPI_STATUS 505131340Snjlacpi_pwr_wake_enable(ACPI_HANDLE consumer, int enable) 506131340Snjl{ 507131340Snjl ACPI_STATUS status; 508131340Snjl struct acpi_powerconsumer *pc; 509131340Snjl struct acpi_prw_data prw; 510131340Snjl int i; 511131340Snjl 512131366Snjl ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 513131366Snjl 514131340Snjl if (consumer == NULL) 515131340Snjl return (AE_BAD_PARAMETER); 516131340Snjl 517133622Snjl ACPI_SERIAL_BEGIN(powerres); 518131340Snjl if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 519131340Snjl if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) 520133622Snjl goto out; 521133622Snjl if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 522133622Snjl panic("acpi wake added power consumer but can't find it"); 523131340Snjl } 524131340Snjl 525133622Snjl status = AE_OK; 526131340Snjl if (acpi_parse_prw(consumer, &prw) != 0) 527133622Snjl goto out; 528131340Snjl for (i = 0; i < prw.power_res_count; i++) 529131340Snjl if (enable) 530131340Snjl acpi_pwr_reference_resource(&prw.power_res[i], pc); 531131340Snjl else 532131340Snjl acpi_pwr_dereference_resource(pc); 533131340Snjl 534131340Snjl if (prw.power_res_count > 0) 535131340Snjl acpi_pwr_switch_power(); 536131340Snjl 537133622Snjlout: 538133622Snjl ACPI_SERIAL_END(powerres); 539133622Snjl return (status); 540131340Snjl} 541131340Snjl 54278915Smsmith/* 54378915Smsmith * Called to create a reference between a power consumer and a power resource 54478915Smsmith * identified in the object. 54578915Smsmith */ 54678915Smsmithstatic void 54778915Smsmithacpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg) 54878915Smsmith{ 54978915Smsmith struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg; 55079357Smsmith struct acpi_powerreference *pr; 55179357Smsmith struct acpi_powerresource *rp; 55279357Smsmith ACPI_HANDLE res; 55379357Smsmith ACPI_STATUS status; 55478915Smsmith 55596926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 556133622Snjl ACPI_SERIAL_ASSERT(powerres); 55778915Smsmith 558128047Snjl res = acpi_GetReference(NULL, obj); 559128047Snjl if (res == NULL) { 560119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 561128047Snjl "can't create a power reference for object type %d\n", 562119529Snjl obj->Type)); 56379357Smsmith return_VOID; 56479357Smsmith } 56578915Smsmith 566119529Snjl /* Create/look up the resource */ 56779357Smsmith if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) { 568119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 569119529Snjl "couldn't register power resource %s - %s\n", 570119529Snjl obj->String.Pointer, AcpiFormatException(status))); 57179357Smsmith return_VOID; 57279357Smsmith } 57379357Smsmith if ((rp = acpi_pwr_find_resource(res)) == NULL) { 57482372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n")); 57579357Smsmith return_VOID; 57679357Smsmith } 577119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n", 578119529Snjl acpi_name(rp->ap_resource))); 57979357Smsmith 580119529Snjl /* Create a reference between the consumer and resource */ 58179357Smsmith if ((pr = malloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 582119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 583119529Snjl "allocation failed for a power consumer reference\n")); 58479357Smsmith return_VOID; 58579357Smsmith } 58679357Smsmith pr->ar_consumer = pc; 58779357Smsmith pr->ar_resource = rp; 58879357Smsmith TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink); 58979357Smsmith TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink); 590131340Snjl 59178915Smsmith return_VOID; 59278915Smsmith} 59378915Smsmith 594131340Snjlstatic int 595131340Snjlacpi_pwr_dereference_resource(struct acpi_powerconsumer *pc) 596131340Snjl{ 597131340Snjl struct acpi_powerreference *pr; 598131340Snjl int changed; 59978915Smsmith 600131366Snjl ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 601133622Snjl ACPI_SERIAL_ASSERT(powerres); 602131366Snjl 603131340Snjl changed = 0; 604131340Snjl while ((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) { 605131340Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n", 606131340Snjl acpi_name(pr->ar_resource->ap_resource))); 607131340Snjl TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink); 608131340Snjl TAILQ_REMOVE(&pc->ac_references, pr, ar_clink); 609131340Snjl free(pr, M_ACPIPWR); 610131340Snjl changed = 1; 611131340Snjl } 612131340Snjl 613131340Snjl return (changed); 614131340Snjl} 615131340Snjl 61678915Smsmith/* 61778915Smsmith * Switch power resources to conform to the desired state. 61878915Smsmith * 61978915Smsmith * Consumers may have modified the power resource list in an arbitrary 62078915Smsmith * fashion; we sweep it in sequence order. 62178915Smsmith */ 62278915Smsmithstatic ACPI_STATUS 62378915Smsmithacpi_pwr_switch_power(void) 62478915Smsmith{ 62578915Smsmith struct acpi_powerresource *rp; 62678915Smsmith ACPI_STATUS status; 62778915Smsmith int cur; 62878915Smsmith 62996926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 630133622Snjl ACPI_SERIAL_ASSERT(powerres); 63178915Smsmith 63278915Smsmith /* 63378915Smsmith * Sweep the list forwards turning things on. 63478915Smsmith */ 63578915Smsmith TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 63679357Smsmith if (TAILQ_FIRST(&rp->ap_references) == NULL) { 637119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 638119529Snjl "%s has no references, not turning on\n", 639119529Snjl acpi_name(rp->ap_resource))); 64079357Smsmith continue; 64179357Smsmith } 64278915Smsmith 643119529Snjl /* We could cache this if we trusted it not to change under us */ 644126560Snjl status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 645119529Snjl if (ACPI_FAILURE(status)) { 64682372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 64782372Smsmith acpi_name(rp->ap_resource), status)); 648119529Snjl /* XXX is this correct? Always switch if in doubt? */ 649119529Snjl continue; 650134908Snjl } else if (rp->ap_state == ACPI_PWR_UNK) 651134908Snjl rp->ap_state = cur; 65278915Smsmith 65378915Smsmith /* 65478915Smsmith * Switch if required. Note that we ignore the result of the switch 65578915Smsmith * effort; we don't know what to do if it fails, so checking wouldn't 65678915Smsmith * help much. 65778915Smsmith */ 658134908Snjl if (rp->ap_state != ACPI_PWR_ON) { 659119529Snjl status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL); 660119529Snjl if (ACPI_FAILURE(status)) { 661119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 662119529Snjl "failed to switch %s on - %s\n", 663119529Snjl acpi_name(rp->ap_resource), 664119529Snjl AcpiFormatException(status))); 66579357Smsmith } else { 666134908Snjl rp->ap_state = ACPI_PWR_ON; 667119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n", 668119529Snjl acpi_name(rp->ap_resource))); 66979357Smsmith } 67079357Smsmith } else { 671119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n", 672119529Snjl acpi_name(rp->ap_resource))); 67379357Smsmith } 67478915Smsmith } 67578915Smsmith 676119529Snjl /* Sweep the list backwards turning things off. */ 677119529Snjl TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list, 678119529Snjl ap_link) { 679119529Snjl 68079357Smsmith if (TAILQ_FIRST(&rp->ap_references) != NULL) { 681119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 682119529Snjl "%s has references, not turning off\n", 683119529Snjl acpi_name(rp->ap_resource))); 68479357Smsmith continue; 68579357Smsmith } 68678915Smsmith 687119529Snjl /* We could cache this if we trusted it not to change under us */ 688126560Snjl status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 689119529Snjl if (ACPI_FAILURE(status)) { 69082372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 69182372Smsmith acpi_name(rp->ap_resource), status)); 692119529Snjl /* XXX is this correct? Always switch if in doubt? */ 693119529Snjl continue; 694134908Snjl } else if (rp->ap_state == ACPI_PWR_UNK) 695134908Snjl rp->ap_state = cur; 69678915Smsmith 69778915Smsmith /* 69878915Smsmith * Switch if required. Note that we ignore the result of the switch 69978915Smsmith * effort; we don't know what to do if it fails, so checking wouldn't 70078915Smsmith * help much. 70178915Smsmith */ 702134908Snjl if (rp->ap_state != ACPI_PWR_OFF) { 703119529Snjl status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL); 704119529Snjl if (ACPI_FAILURE(status)) { 705119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 706119529Snjl "failed to switch %s off - %s\n", 707119529Snjl acpi_name(rp->ap_resource), 708119529Snjl AcpiFormatException(status))); 70979357Smsmith } else { 710134908Snjl rp->ap_state = ACPI_PWR_OFF; 711119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n", 712119529Snjl acpi_name(rp->ap_resource))); 71379357Smsmith } 71479357Smsmith } else { 715119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n", 716119529Snjl acpi_name(rp->ap_resource))); 71779357Smsmith } 71878915Smsmith } 719119529Snjl 720119529Snjl return_ACPI_STATUS (AE_OK); 72178915Smsmith} 72278915Smsmith 72378915Smsmith/* 72478915Smsmith * Find a power resource's control structure. 72578915Smsmith */ 72678915Smsmithstatic struct acpi_powerresource * 72778915Smsmithacpi_pwr_find_resource(ACPI_HANDLE res) 72878915Smsmith{ 72978915Smsmith struct acpi_powerresource *rp; 73078915Smsmith 73196926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 732133622Snjl ACPI_SERIAL_ASSERT(powerres); 73378915Smsmith 734119529Snjl TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 73578915Smsmith if (rp->ap_resource == res) 73678915Smsmith break; 737119529Snjl } 738119529Snjl 739119529Snjl return_PTR (rp); 74078915Smsmith} 74178915Smsmith 74278915Smsmith/* 74378915Smsmith * Find a power consumer's control structure. 74478915Smsmith */ 74578915Smsmithstatic struct acpi_powerconsumer * 74678915Smsmithacpi_pwr_find_consumer(ACPI_HANDLE consumer) 74778915Smsmith{ 74878915Smsmith struct acpi_powerconsumer *pc; 74978915Smsmith 75096926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 751133622Snjl ACPI_SERIAL_ASSERT(powerres); 75278915Smsmith 753119529Snjl TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) { 75478915Smsmith if (pc->ac_consumer == consumer) 75578915Smsmith break; 756119529Snjl } 757119529Snjl 758119529Snjl return_PTR (pc); 75978915Smsmith} 760