acpi_powerres.c revision 267910
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 267910 2014-06-26 10:48:01Z hselasky $"); 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 50248415Srpaulo * ugly case where we may turn off 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 58227293Sedstatic MALLOC_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; 90202771Sjkim UINT64 ap_systemlevel; 91202771Sjkim UINT64 ap_order; 9278915Smsmith}; 9378915Smsmith 94119529Snjlstatic TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource) 95267910Shselasky acpi_powerresources = TAILQ_HEAD_INITIALIZER(acpi_powerresources); 96119529Snjlstatic TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer) 97267910Shselasky acpi_powerconsumers = TAILQ_HEAD_INITIALIZER(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 11878915Smsmith/* 11978915Smsmith * Register a power resource. 12078915Smsmith * 12178915Smsmith * It's OK to call this if we already know about the resource. 12278915Smsmith */ 12378915Smsmithstatic ACPI_STATUS 12478915Smsmithacpi_pwr_register_resource(ACPI_HANDLE res) 12578915Smsmith{ 12678915Smsmith ACPI_STATUS status; 12778915Smsmith ACPI_BUFFER buf; 12879493Smsmith ACPI_OBJECT *obj; 12978915Smsmith struct acpi_powerresource *rp, *srp; 13078915Smsmith 13196926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 132133622Snjl ACPI_SERIAL_ASSERT(powerres); 13378915Smsmith 13478915Smsmith rp = NULL; 13591125Smsmith buf.Pointer = NULL; 13678915Smsmith 137119529Snjl /* Look to see if we know about this resource */ 13878915Smsmith if (acpi_pwr_find_resource(res) != NULL) 139119529Snjl return_ACPI_STATUS (AE_OK); /* already know about it */ 14078915Smsmith 141119529Snjl /* Allocate a new resource */ 14278915Smsmith if ((rp = malloc(sizeof(*rp), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 14378915Smsmith status = AE_NO_MEMORY; 14478915Smsmith goto out; 14578915Smsmith } 14678915Smsmith TAILQ_INIT(&rp->ap_references); 14778915Smsmith rp->ap_resource = res; 14878915Smsmith 149119529Snjl /* Get the Power Resource object */ 15091125Smsmith buf.Length = ACPI_ALLOCATE_BUFFER; 15191125Smsmith if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) { 15282372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n")); 15378915Smsmith goto out; 15478915Smsmith } 15578915Smsmith obj = buf.Pointer; 15679493Smsmith if (obj->Type != ACPI_TYPE_POWER) { 157119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 158119529Snjl "questionable power resource object %s\n", 159119529Snjl acpi_name(res))); 16079493Smsmith status = AE_TYPE; 16179493Smsmith goto out; 16278915Smsmith } 16379493Smsmith rp->ap_systemlevel = obj->PowerResource.SystemLevel; 16479493Smsmith rp->ap_order = obj->PowerResource.ResourceOrder; 16578915Smsmith 166119529Snjl /* Sort the resource into the list */ 16778915Smsmith status = AE_OK; 16878915Smsmith srp = TAILQ_FIRST(&acpi_powerresources); 169119529Snjl if (srp == NULL || rp->ap_order < srp->ap_order) { 17078915Smsmith TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link); 17179357Smsmith goto done; 17278915Smsmith } 173119529Snjl TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) { 17478915Smsmith if (rp->ap_order < srp->ap_order) { 17578915Smsmith TAILQ_INSERT_BEFORE(srp, rp, ap_link); 17679357Smsmith goto done; 17778915Smsmith } 178119529Snjl } 17978915Smsmith TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link); 18079357Smsmith 18179357Smsmith done: 182119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 183119529Snjl "registered power resource %s\n", acpi_name(res))); 184119529Snjl 18578915Smsmith out: 18691125Smsmith if (buf.Pointer != NULL) 18791125Smsmith AcpiOsFree(buf.Pointer); 188119529Snjl if (ACPI_FAILURE(status) && rp != NULL) 18978915Smsmith free(rp, M_ACPIPWR); 190119529Snjl return_ACPI_STATUS (status); 19178915Smsmith} 19278915Smsmith 193128248Snjl#ifdef notyet 19478915Smsmith/* 19578915Smsmith * Deregister a power resource. 19678915Smsmith */ 19778915Smsmithstatic ACPI_STATUS 19878915Smsmithacpi_pwr_deregister_resource(ACPI_HANDLE res) 19978915Smsmith{ 20078915Smsmith struct acpi_powerresource *rp; 20178915Smsmith 20296926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 203133622Snjl ACPI_SERIAL_ASSERT(powerres); 20478915Smsmith 20578915Smsmith rp = NULL; 20678915Smsmith 207119529Snjl /* Find the resource */ 20878915Smsmith if ((rp = acpi_pwr_find_resource(res)) == NULL) 209119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 21078915Smsmith 211119529Snjl /* Check that there are no consumers referencing this resource */ 21278915Smsmith if (TAILQ_FIRST(&rp->ap_references) != NULL) 213119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 21478915Smsmith 215119529Snjl /* Pull it off the list and free it */ 21678915Smsmith TAILQ_REMOVE(&acpi_powerresources, rp, ap_link); 21778915Smsmith free(rp, M_ACPIPWR); 21878915Smsmith 219119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n", 220119529Snjl acpi_name(res))); 22178915Smsmith 222119529Snjl return_ACPI_STATUS (AE_OK); 22378915Smsmith} 224128248Snjl#endif /* notyet */ 22578915Smsmith 22678915Smsmith/* 22778915Smsmith * Register a power consumer. 22878915Smsmith * 22978915Smsmith * It's OK to call this if we already know about the consumer. 23078915Smsmith */ 23178915Smsmithstatic ACPI_STATUS 23278915Smsmithacpi_pwr_register_consumer(ACPI_HANDLE consumer) 23378915Smsmith{ 23478915Smsmith struct acpi_powerconsumer *pc; 23578915Smsmith 23696926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 237133622Snjl ACPI_SERIAL_ASSERT(powerres); 23878915Smsmith 239119529Snjl /* Check to see whether we know about this consumer already */ 240157947Sjkim if (acpi_pwr_find_consumer(consumer) != NULL) 241119529Snjl return_ACPI_STATUS (AE_OK); 24278915Smsmith 243119529Snjl /* Allocate a new power consumer */ 24478915Smsmith if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL) 245119529Snjl return_ACPI_STATUS (AE_NO_MEMORY); 24678915Smsmith TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link); 24778915Smsmith TAILQ_INIT(&pc->ac_references); 24878915Smsmith pc->ac_consumer = consumer; 24978915Smsmith 250119529Snjl /* XXX we should try to find its current state */ 251119529Snjl pc->ac_state = ACPI_STATE_UNKNOWN; 25278915Smsmith 253119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n", 254119529Snjl acpi_name(consumer))); 25578915Smsmith 256119529Snjl return_ACPI_STATUS (AE_OK); 25778915Smsmith} 25878915Smsmith 259128248Snjl#ifdef notyet 26078915Smsmith/* 26178915Smsmith * Deregister a power consumer. 26278915Smsmith * 26378915Smsmith * This should only be done once the consumer has been powered off. 26478915Smsmith * (XXX is this correct? Check once implemented) 26578915Smsmith */ 26678915Smsmithstatic ACPI_STATUS 26778915Smsmithacpi_pwr_deregister_consumer(ACPI_HANDLE consumer) 26878915Smsmith{ 26978915Smsmith struct acpi_powerconsumer *pc; 27078915Smsmith 27196926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 272133622Snjl ACPI_SERIAL_ASSERT(powerres); 27378915Smsmith 274119529Snjl /* Find the consumer */ 27578915Smsmith if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 276119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 27778915Smsmith 278119529Snjl /* Make sure the consumer's not referencing anything right now */ 27978915Smsmith if (TAILQ_FIRST(&pc->ac_references) != NULL) 280119529Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 28178915Smsmith 282119529Snjl /* Pull the consumer off the list and free it */ 28378915Smsmith TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link); 284133622Snjl free(pc, M_ACPIPWR); 28578915Smsmith 286119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n", 287119529Snjl acpi_name(consumer))); 28878915Smsmith 289119529Snjl return_ACPI_STATUS (AE_OK); 29078915Smsmith} 291128248Snjl#endif /* notyet */ 29278915Smsmith 29378915Smsmith/* 29478915Smsmith * Set a power consumer to a particular power state. 29578915Smsmith */ 29678915SmsmithACPI_STATUS 29778915Smsmithacpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state) 29878915Smsmith{ 29978915Smsmith struct acpi_powerconsumer *pc; 30082084Siwasaki ACPI_HANDLE method_handle, reslist_handle, pr0_handle; 30178915Smsmith ACPI_BUFFER reslist_buffer; 30278915Smsmith ACPI_OBJECT *reslist_object; 30378915Smsmith ACPI_STATUS status; 30478915Smsmith char *method_name, *reslist_name; 30578915Smsmith 30696926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 30778915Smsmith 308128252Snjl /* It's never ok to switch a non-existent consumer. */ 309128252Snjl if (consumer == NULL) 310128252Snjl return_ACPI_STATUS (AE_NOT_FOUND); 311133622Snjl reslist_buffer.Pointer = NULL; 312133622Snjl reslist_object = NULL; 313133622Snjl ACPI_SERIAL_BEGIN(powerres); 314128252Snjl 315119529Snjl /* Find the consumer */ 31678915Smsmith if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 31791125Smsmith if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) 318133622Snjl goto out; 319133622Snjl if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 320133622Snjl panic("acpi added power consumer but can't find it"); 32178915Smsmith } 32278915Smsmith 323133622Snjl /* Check for valid transitions. We can only go to D0 from D3. */ 324133622Snjl status = AE_BAD_PARAMETER; 325119529Snjl if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0) 326133622Snjl goto out; 32778915Smsmith 328119529Snjl /* Find transition mechanism(s) */ 329130208Snjl 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: 347133622Snjl goto out; 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 if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle))) 36378915Smsmith method_handle = NULL; 36491125Smsmith if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle))) 36578915Smsmith reslist_handle = NULL; 366119529Snjl if (reslist_handle == NULL && method_handle == NULL) { 36778915Smsmith if (state == ACPI_STATE_D0) { 36878915Smsmith pc->ac_state = ACPI_STATE_D0; 369133622Snjl status = AE_OK; 370133622Snjl goto out; 37178915Smsmith } 372133622Snjl if (state != ACPI_STATE_D3) { 373133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 374133622Snjl "attempt to set unsupported state D%d\n", state)); 375133622Snjl goto out; 376133622Snjl } 37782084Siwasaki 378130208Snjl /* 379130208Snjl * Turn off the resources listed in _PR0 to go to D3. If there is 380130208Snjl * no _PR0 method, this object doesn't support ACPI power states. 381130208Snjl */ 382130208Snjl if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle))) { 383130208Snjl status = AE_NOT_FOUND; 384133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 385133622Snjl "device missing _PR0 (desired state was D%d)\n", state)); 386133622Snjl goto out; 387130208Snjl } 38891125Smsmith reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 389119529Snjl status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer); 390133622Snjl if (ACPI_FAILURE(status)) { 391133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 392133622Snjl "can't evaluate _PR0 for device %s, state D%d\n", 393133622Snjl acpi_name(consumer), state)); 394133622Snjl goto out; 395133622Snjl } 39682084Siwasaki reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 397133622Snjl if (!ACPI_PKG_VALID(reslist_object, 1)) { 398133622Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 399133622Snjl "invalid package object for state D%d\n", state)); 400133622Snjl status = AE_TYPE; 401133622Snjl goto out; 402133622Snjl } 40391125Smsmith AcpiOsFree(reslist_buffer.Pointer); 40491125Smsmith reslist_buffer.Pointer = NULL; 40591125Smsmith reslist_object = NULL; 40678915Smsmith } 40778915Smsmith 40878915Smsmith /* 40978915Smsmith * Check that we can actually fetch the list of power resources 41078915Smsmith */ 41178915Smsmith if (reslist_handle != NULL) { 41291125Smsmith reslist_buffer.Length = ACPI_ALLOCATE_BUFFER; 413119529Snjl status = AcpiEvaluateObject(reslist_handle, NULL, NULL, 414119529Snjl &reslist_buffer); 415119529Snjl if (ACPI_FAILURE(status)) { 416119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 417119529Snjl "can't evaluate resource list %s\n", 418119529Snjl acpi_name(reslist_handle))); 41991125Smsmith goto out; 42078915Smsmith } 42178915Smsmith reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 42278915Smsmith if (reslist_object->Type != ACPI_TYPE_PACKAGE) { 423119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 424119529Snjl "resource list is not ACPI_TYPE_PACKAGE (%d)\n", 425119529Snjl reslist_object->Type)); 42691125Smsmith status = AE_TYPE; 42791125Smsmith goto out; 42878915Smsmith } 42978915Smsmith } 43078915Smsmith 43178915Smsmith /* 432125745Sjhb * Now we are ready to switch, so kill off any current power 433119529Snjl * resource references. 43478915Smsmith */ 435238199Seadler acpi_pwr_dereference_resource(pc); 43678915Smsmith 43778915Smsmith /* 43878915Smsmith * Add new power resource references, if we have any. Traverse the 43978915Smsmith * package that we got from evaluating reslist_handle, and look up each 44078915Smsmith * of the resources that are referenced. 44178915Smsmith */ 44278915Smsmith if (reslist_object != NULL) { 44382372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n", 44482372Smsmith reslist_object->Package.Count)); 445119529Snjl acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource, 446119529Snjl pc); 44778915Smsmith } 44878915Smsmith 44978915Smsmith /* 45078915Smsmith * If we changed anything in the resource list, we need to run a switch 45178915Smsmith * pass now. 45278915Smsmith */ 45391125Smsmith if (ACPI_FAILURE(status = acpi_pwr_switch_power())) { 454119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 455119529Snjl "failed to switch resources from %s to D%d\n", 45682372Smsmith acpi_name(consumer), state)); 457119529Snjl 458119529Snjl /* XXX is this appropriate? Should we return to previous state? */ 459139339Snjl goto out; 46078915Smsmith } 46178915Smsmith 462119529Snjl /* Invoke power state switch method (if present) */ 46378915Smsmith if (method_handle != NULL) { 464119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 465119529Snjl "invoking state transition method %s\n", 466119529Snjl acpi_name(method_handle))); 467119529Snjl status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL); 468119529Snjl if (ACPI_FAILURE(status)) { 46991125Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n", 470119529Snjl AcpiFormatException(status))); 47191125Smsmith pc->ac_state = ACPI_STATE_UNKNOWN; 472119529Snjl 473119529Snjl /* XXX Should we return to previous state? */ 474119529Snjl goto out; 47591125Smsmith } 47678915Smsmith } 477139339Snjl 478119529Snjl /* Transition was successful */ 47978915Smsmith pc->ac_state = state; 480133622Snjl status = AE_OK; 48182084Siwasaki 482133622Snjlout: 483133622Snjl ACPI_SERIAL_END(powerres); 48491125Smsmith if (reslist_buffer.Pointer != NULL) 48591125Smsmith AcpiOsFree(reslist_buffer.Pointer); 486119529Snjl return_ACPI_STATUS (status); 48778915Smsmith} 48878915Smsmith 489131340Snjl/* Enable or disable a power resource for wake */ 490131340SnjlACPI_STATUS 491131340Snjlacpi_pwr_wake_enable(ACPI_HANDLE consumer, int enable) 492131340Snjl{ 493131340Snjl ACPI_STATUS status; 494131340Snjl struct acpi_powerconsumer *pc; 495131340Snjl struct acpi_prw_data prw; 496131340Snjl int i; 497131340Snjl 498131366Snjl ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 499131366Snjl 500131340Snjl if (consumer == NULL) 501131340Snjl return (AE_BAD_PARAMETER); 502131340Snjl 503133622Snjl ACPI_SERIAL_BEGIN(powerres); 504131340Snjl if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 505131340Snjl if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer))) 506133622Snjl goto out; 507133622Snjl if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 508133622Snjl panic("acpi wake added power consumer but can't find it"); 509131340Snjl } 510131340Snjl 511133622Snjl status = AE_OK; 512131340Snjl if (acpi_parse_prw(consumer, &prw) != 0) 513133622Snjl goto out; 514131340Snjl for (i = 0; i < prw.power_res_count; i++) 515131340Snjl if (enable) 516131340Snjl acpi_pwr_reference_resource(&prw.power_res[i], pc); 517131340Snjl else 518131340Snjl acpi_pwr_dereference_resource(pc); 519131340Snjl 520131340Snjl if (prw.power_res_count > 0) 521131340Snjl acpi_pwr_switch_power(); 522131340Snjl 523133622Snjlout: 524133622Snjl ACPI_SERIAL_END(powerres); 525133622Snjl return (status); 526131340Snjl} 527131340Snjl 52878915Smsmith/* 52978915Smsmith * Called to create a reference between a power consumer and a power resource 53078915Smsmith * identified in the object. 53178915Smsmith */ 53278915Smsmithstatic void 53378915Smsmithacpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg) 53478915Smsmith{ 53578915Smsmith struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg; 53679357Smsmith struct acpi_powerreference *pr; 53779357Smsmith struct acpi_powerresource *rp; 53879357Smsmith ACPI_HANDLE res; 53979357Smsmith ACPI_STATUS status; 54078915Smsmith 54196926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 542133622Snjl ACPI_SERIAL_ASSERT(powerres); 54378915Smsmith 544128047Snjl res = acpi_GetReference(NULL, obj); 545128047Snjl if (res == NULL) { 546119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 547128047Snjl "can't create a power reference for object type %d\n", 548119529Snjl obj->Type)); 54979357Smsmith return_VOID; 55079357Smsmith } 55178915Smsmith 552119529Snjl /* Create/look up the resource */ 55379357Smsmith if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) { 554119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 555119529Snjl "couldn't register power resource %s - %s\n", 556119529Snjl obj->String.Pointer, AcpiFormatException(status))); 55779357Smsmith return_VOID; 55879357Smsmith } 55979357Smsmith if ((rp = acpi_pwr_find_resource(res)) == NULL) { 56082372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n")); 56179357Smsmith return_VOID; 56279357Smsmith } 563119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n", 564119529Snjl acpi_name(rp->ap_resource))); 56579357Smsmith 566119529Snjl /* Create a reference between the consumer and resource */ 56779357Smsmith if ((pr = malloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 568119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 569119529Snjl "allocation failed for a power consumer reference\n")); 57079357Smsmith return_VOID; 57179357Smsmith } 57279357Smsmith pr->ar_consumer = pc; 57379357Smsmith pr->ar_resource = rp; 57479357Smsmith TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink); 57579357Smsmith TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink); 576131340Snjl 57778915Smsmith return_VOID; 57878915Smsmith} 57978915Smsmith 580131340Snjlstatic int 581131340Snjlacpi_pwr_dereference_resource(struct acpi_powerconsumer *pc) 582131340Snjl{ 583131340Snjl struct acpi_powerreference *pr; 584131340Snjl int changed; 58578915Smsmith 586131366Snjl ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 587133622Snjl ACPI_SERIAL_ASSERT(powerres); 588131366Snjl 589131340Snjl changed = 0; 590131340Snjl while ((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) { 591131340Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n", 592131340Snjl acpi_name(pr->ar_resource->ap_resource))); 593131340Snjl TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink); 594131340Snjl TAILQ_REMOVE(&pc->ac_references, pr, ar_clink); 595131340Snjl free(pr, M_ACPIPWR); 596131340Snjl changed = 1; 597131340Snjl } 598131340Snjl 599131340Snjl return (changed); 600131340Snjl} 601131340Snjl 60278915Smsmith/* 60378915Smsmith * Switch power resources to conform to the desired state. 60478915Smsmith * 60578915Smsmith * Consumers may have modified the power resource list in an arbitrary 60678915Smsmith * fashion; we sweep it in sequence order. 60778915Smsmith */ 60878915Smsmithstatic ACPI_STATUS 60978915Smsmithacpi_pwr_switch_power(void) 61078915Smsmith{ 61178915Smsmith struct acpi_powerresource *rp; 61278915Smsmith ACPI_STATUS status; 61378915Smsmith int cur; 61478915Smsmith 61596926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 616133622Snjl ACPI_SERIAL_ASSERT(powerres); 61778915Smsmith 61878915Smsmith /* 61978915Smsmith * Sweep the list forwards turning things on. 62078915Smsmith */ 62178915Smsmith TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 62279357Smsmith if (TAILQ_FIRST(&rp->ap_references) == NULL) { 623119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 624119529Snjl "%s has no references, not turning on\n", 625119529Snjl acpi_name(rp->ap_resource))); 62679357Smsmith continue; 62779357Smsmith } 62878915Smsmith 629126560Snjl status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 630119529Snjl if (ACPI_FAILURE(status)) { 63182372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 63282372Smsmith acpi_name(rp->ap_resource), status)); 633119529Snjl /* XXX is this correct? Always switch if in doubt? */ 634119529Snjl continue; 635267647Sjhb } 63678915Smsmith 63778915Smsmith /* 63878915Smsmith * Switch if required. Note that we ignore the result of the switch 63978915Smsmith * effort; we don't know what to do if it fails, so checking wouldn't 64078915Smsmith * help much. 64178915Smsmith */ 642267647Sjhb if (cur != ACPI_PWR_ON) { 643119529Snjl status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL); 644119529Snjl if (ACPI_FAILURE(status)) { 645119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 646119529Snjl "failed to switch %s on - %s\n", 647119529Snjl acpi_name(rp->ap_resource), 648119529Snjl AcpiFormatException(status))); 64979357Smsmith } else { 650119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n", 651119529Snjl acpi_name(rp->ap_resource))); 65279357Smsmith } 65379357Smsmith } else { 654119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n", 655119529Snjl acpi_name(rp->ap_resource))); 65679357Smsmith } 65778915Smsmith } 65878915Smsmith 659119529Snjl /* Sweep the list backwards turning things off. */ 660119529Snjl TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list, 661119529Snjl ap_link) { 662119529Snjl 66379357Smsmith if (TAILQ_FIRST(&rp->ap_references) != NULL) { 664119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 665119529Snjl "%s has references, not turning off\n", 666119529Snjl acpi_name(rp->ap_resource))); 66779357Smsmith continue; 66879357Smsmith } 66978915Smsmith 670126560Snjl status = acpi_GetInteger(rp->ap_resource, "_STA", &cur); 671119529Snjl if (ACPI_FAILURE(status)) { 67282372Smsmith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n", 67382372Smsmith acpi_name(rp->ap_resource), status)); 674119529Snjl /* XXX is this correct? Always switch if in doubt? */ 675119529Snjl continue; 676267647Sjhb } 67778915Smsmith 67878915Smsmith /* 67978915Smsmith * Switch if required. Note that we ignore the result of the switch 68078915Smsmith * effort; we don't know what to do if it fails, so checking wouldn't 68178915Smsmith * help much. 68278915Smsmith */ 683267647Sjhb if (cur != ACPI_PWR_OFF) { 684119529Snjl status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL); 685119529Snjl if (ACPI_FAILURE(status)) { 686119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, 687119529Snjl "failed to switch %s off - %s\n", 688119529Snjl acpi_name(rp->ap_resource), 689119529Snjl AcpiFormatException(status))); 69079357Smsmith } else { 691119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n", 692119529Snjl acpi_name(rp->ap_resource))); 69379357Smsmith } 69479357Smsmith } else { 695119529Snjl ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n", 696119529Snjl acpi_name(rp->ap_resource))); 69779357Smsmith } 69878915Smsmith } 699119529Snjl 700119529Snjl return_ACPI_STATUS (AE_OK); 70178915Smsmith} 70278915Smsmith 70378915Smsmith/* 70478915Smsmith * Find a power resource's control structure. 70578915Smsmith */ 70678915Smsmithstatic struct acpi_powerresource * 70778915Smsmithacpi_pwr_find_resource(ACPI_HANDLE res) 70878915Smsmith{ 70978915Smsmith struct acpi_powerresource *rp; 71078915Smsmith 71196926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 712133622Snjl ACPI_SERIAL_ASSERT(powerres); 71378915Smsmith 714119529Snjl TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 71578915Smsmith if (rp->ap_resource == res) 71678915Smsmith break; 717119529Snjl } 718119529Snjl 719119529Snjl return_PTR (rp); 72078915Smsmith} 72178915Smsmith 72278915Smsmith/* 72378915Smsmith * Find a power consumer's control structure. 72478915Smsmith */ 72578915Smsmithstatic struct acpi_powerconsumer * 72678915Smsmithacpi_pwr_find_consumer(ACPI_HANDLE consumer) 72778915Smsmith{ 72878915Smsmith struct acpi_powerconsumer *pc; 72978915Smsmith 73096926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 731133622Snjl ACPI_SERIAL_ASSERT(powerres); 73278915Smsmith 733119529Snjl TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) { 73478915Smsmith if (pc->ac_consumer == consumer) 73578915Smsmith break; 736119529Snjl } 737119529Snjl 738119529Snjl return_PTR (pc); 73978915Smsmith} 740