acpi_powerres.c revision 248415
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 248415 2013-03-17 07:28:17Z rpaulo $");
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
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;
91202771Sjkim    UINT64				ap_systemlevel;
92202771Sjkim    UINT64				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
31896926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
31978915Smsmith
320128252Snjl    /* It's never ok to switch a non-existent consumer. */
321128252Snjl    if (consumer == NULL)
322128252Snjl	return_ACPI_STATUS (AE_NOT_FOUND);
323133622Snjl    reslist_buffer.Pointer = NULL;
324133622Snjl    reslist_object = NULL;
325133622Snjl    ACPI_SERIAL_BEGIN(powerres);
326128252Snjl
327119529Snjl    /* Find the consumer */
32878915Smsmith    if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
32991125Smsmith	if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer)))
330133622Snjl	    goto out;
331133622Snjl	if ((pc = acpi_pwr_find_consumer(consumer)) == NULL)
332133622Snjl	    panic("acpi added power consumer but can't find it");
33378915Smsmith    }
33478915Smsmith
335133622Snjl    /* Check for valid transitions.  We can only go to D0 from D3. */
336133622Snjl    status = AE_BAD_PARAMETER;
337119529Snjl    if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0)
338133622Snjl	goto out;
33978915Smsmith
340119529Snjl    /* Find transition mechanism(s) */
341130208Snjl    switch (state) {
34278915Smsmith    case ACPI_STATE_D0:
34378915Smsmith	method_name = "_PS0";
34478915Smsmith	reslist_name = "_PR0";
34578915Smsmith	break;
34678915Smsmith    case ACPI_STATE_D1:
34778915Smsmith	method_name = "_PS1";
34878915Smsmith	reslist_name = "_PR1";
34978915Smsmith	break;
35078915Smsmith    case ACPI_STATE_D2:
35178915Smsmith	method_name = "_PS2";
35278915Smsmith	reslist_name = "_PR2";
35378915Smsmith	break;
35478915Smsmith    case ACPI_STATE_D3:
35578915Smsmith	method_name = "_PS3";
35678915Smsmith	reslist_name = "_PR3";
35778915Smsmith	break;
35878915Smsmith    default:
359133622Snjl	goto out;
36078915Smsmith    }
36182372Smsmith    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n",
362119529Snjl		     acpi_name(consumer), pc->ac_state, state));
36378915Smsmith
36478915Smsmith    /*
36578915Smsmith     * Verify that this state is supported, ie. one of method or
36678915Smsmith     * reslist must be present.  We need to do this before we go
36778915Smsmith     * dereferencing resources (since we might be trying to go to
36878915Smsmith     * a state we don't support).
36978915Smsmith     *
37078915Smsmith     * Note that if any states are supported, the device has to
37178915Smsmith     * support D0 and D3.  It's never an error to try to go to
37278915Smsmith     * D0.
37378915Smsmith     */
37491125Smsmith    if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle)))
37578915Smsmith	method_handle = NULL;
37691125Smsmith    if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle)))
37778915Smsmith	reslist_handle = NULL;
378119529Snjl    if (reslist_handle == NULL && method_handle == NULL) {
37978915Smsmith	if (state == ACPI_STATE_D0) {
38078915Smsmith	    pc->ac_state = ACPI_STATE_D0;
381133622Snjl	    status = AE_OK;
382133622Snjl	    goto out;
38378915Smsmith	}
384133622Snjl	if (state != ACPI_STATE_D3) {
385133622Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
386133622Snjl		"attempt to set unsupported state D%d\n", state));
387133622Snjl	    goto out;
388133622Snjl	}
38982084Siwasaki
390130208Snjl	/*
391130208Snjl	 * Turn off the resources listed in _PR0 to go to D3.  If there is
392130208Snjl	 * no _PR0 method, this object doesn't support ACPI power states.
393130208Snjl	 */
394130208Snjl	if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle))) {
395130208Snjl	    status = AE_NOT_FOUND;
396133622Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
397133622Snjl		"device missing _PR0 (desired state was D%d)\n", state));
398133622Snjl	    goto out;
399130208Snjl	}
40091125Smsmith	reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
401119529Snjl	status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer);
402133622Snjl	if (ACPI_FAILURE(status)) {
403133622Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
404133622Snjl		"can't evaluate _PR0 for device %s, state D%d\n",
405133622Snjl		acpi_name(consumer), state));
406133622Snjl	    goto out;
407133622Snjl	}
40882084Siwasaki	reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
409133622Snjl	if (!ACPI_PKG_VALID(reslist_object, 1)) {
410133622Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
411133622Snjl		"invalid package object for state D%d\n", state));
412133622Snjl	    status = AE_TYPE;
413133622Snjl	    goto out;
414133622Snjl	}
41591125Smsmith	AcpiOsFree(reslist_buffer.Pointer);
41691125Smsmith	reslist_buffer.Pointer = NULL;
41791125Smsmith	reslist_object = NULL;
41878915Smsmith    }
41978915Smsmith
42078915Smsmith    /*
42178915Smsmith     * Check that we can actually fetch the list of power resources
42278915Smsmith     */
42378915Smsmith    if (reslist_handle != NULL) {
42491125Smsmith	reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
425119529Snjl	status = AcpiEvaluateObject(reslist_handle, NULL, NULL,
426119529Snjl				    &reslist_buffer);
427119529Snjl	if (ACPI_FAILURE(status)) {
428119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
429119529Snjl			     "can't evaluate resource list %s\n",
430119529Snjl			     acpi_name(reslist_handle)));
43191125Smsmith	    goto out;
43278915Smsmith	}
43378915Smsmith	reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
43478915Smsmith	if (reslist_object->Type != ACPI_TYPE_PACKAGE) {
435119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
436119529Snjl			     "resource list is not ACPI_TYPE_PACKAGE (%d)\n",
437119529Snjl			     reslist_object->Type));
43891125Smsmith	    status = AE_TYPE;
43991125Smsmith	    goto out;
44078915Smsmith	}
44178915Smsmith    }
44278915Smsmith
44378915Smsmith    /*
444125745Sjhb     * Now we are ready to switch, so kill off any current power
445119529Snjl     * resource references.
44678915Smsmith     */
447238199Seadler    acpi_pwr_dereference_resource(pc);
44878915Smsmith
44978915Smsmith    /*
45078915Smsmith     * Add new power resource references, if we have any.  Traverse the
45178915Smsmith     * package that we got from evaluating reslist_handle, and look up each
45278915Smsmith     * of the resources that are referenced.
45378915Smsmith     */
45478915Smsmith    if (reslist_object != NULL) {
45582372Smsmith	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n",
45682372Smsmith			  reslist_object->Package.Count));
457119529Snjl	acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource,
458119529Snjl				  pc);
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