acpi_powerres.c revision 130208
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 130208 2004-06-07 21:39:15Z njl $");
29119418Sobrien
30119529Snjl#include "opt_acpi.h"
3178915Smsmith#include <sys/param.h>
3278915Smsmith#include <sys/kernel.h>
3378915Smsmith#include <sys/malloc.h>
3478915Smsmith#include <sys/bus.h>
3578915Smsmith
3678915Smsmith#include "acpi.h"
3778915Smsmith#include <dev/acpica/acpivar.h>
3878915Smsmith
3978915Smsmith/*
4078915Smsmith * ACPI power resource management.
4178915Smsmith *
4278915Smsmith * Power resource behaviour is slightly complicated by the fact that
4378915Smsmith * a single power resource may provide power for more than one device.
4478915Smsmith * Thus, we must track the device(s) being powered by a given power
4578915Smsmith * resource, and only deactivate it when there are no powered devices.
4678915Smsmith *
4778915Smsmith * Note that this only manages resources for known devices.  There is an
4878915Smsmith * ugly case where we may turn of power to a device which is in use because
4978915Smsmith * we don't know that it depends on a given resource.  We should perhaps
5078915Smsmith * try to be smarter about this, but a more complete solution would involve
5178915Smsmith * scanning all of the ACPI namespace to find devices we're not currently
5278915Smsmith * aware of, and this raises questions about whether they should be left
5378915Smsmith * on, turned off, etc.
5478915Smsmith *
5578915Smsmith * XXX locking
5678915Smsmith */
5778915Smsmith
5878915SmsmithMALLOC_DEFINE(M_ACPIPWR, "acpipwr", "ACPI power resources");
5978915Smsmith
60119529Snjl/* Hooks for the ACPI CA debugging infrastructure */
61126517Snjl#define _COMPONENT	ACPI_POWERRES
6291125SmsmithACPI_MODULE_NAME("POWERRES")
6378915Smsmith
64119529Snjl/* Return values from _STA on a power resource */
6579357Smsmith#define ACPI_PWR_OFF	0
6679357Smsmith#define ACPI_PWR_ON	1
6779357Smsmith
68119529Snjl/* A relationship between a power resource and a consumer. */
6978915Smsmithstruct acpi_powerreference {
70119529Snjl    struct acpi_powerconsumer		*ar_consumer;
71119529Snjl    struct acpi_powerresource		*ar_resource;
72119529Snjl    TAILQ_ENTRY(acpi_powerreference)	ar_rlink; /* link on resource list */
73119529Snjl    TAILQ_ENTRY(acpi_powerreference)	ar_clink; /* link on consumer */
7478915Smsmith};
7578915Smsmith
76119529Snjl/* A power-managed device. */
7778915Smsmithstruct acpi_powerconsumer {
78119529Snjl    /* Device which is powered */
79119529Snjl    ACPI_HANDLE				ac_consumer;
80119529Snjl    int					ac_state;
81119529Snjl    TAILQ_ENTRY(acpi_powerconsumer)	ac_link;
82119529Snjl    TAILQ_HEAD(,acpi_powerreference)	ac_references;
8378915Smsmith};
8478915Smsmith
85119529Snjl/* A power resource. */
8678915Smsmithstruct acpi_powerresource {
87119529Snjl    TAILQ_ENTRY(acpi_powerresource)	ap_link;
88119529Snjl    TAILQ_HEAD(,acpi_powerreference)	ap_references;
89119529Snjl    ACPI_HANDLE				ap_resource;
90119529Snjl    ACPI_INTEGER			ap_systemlevel;
91119529Snjl    ACPI_INTEGER			ap_order;
9278915Smsmith};
9378915Smsmith
94119529Snjlstatic TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource)
95119529Snjl	acpi_powerresources;
96119529Snjlstatic TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer)
97119529Snjl	acpi_powerconsumers;
9878915Smsmith
99119529Snjlstatic ACPI_STATUS	acpi_pwr_register_consumer(ACPI_HANDLE consumer);
100128248Snjl#ifdef notyet
101119529Snjlstatic ACPI_STATUS	acpi_pwr_deregister_consumer(ACPI_HANDLE consumer);
102128248Snjl#endif /* notyet */
103119529Snjlstatic ACPI_STATUS	acpi_pwr_register_resource(ACPI_HANDLE res);
104128248Snjl#ifdef notyet
105119529Snjlstatic ACPI_STATUS	acpi_pwr_deregister_resource(ACPI_HANDLE res);
106128248Snjl#endif /* notyet */
107119529Snjlstatic void		acpi_pwr_reference_resource(ACPI_OBJECT *obj,
108119529Snjl						    void *arg);
109119529Snjlstatic ACPI_STATUS	acpi_pwr_switch_power(void);
110119529Snjlstatic struct acpi_powerresource
111119529Snjl			*acpi_pwr_find_resource(ACPI_HANDLE res);
112119529Snjlstatic struct acpi_powerconsumer
113119529Snjl			*acpi_pwr_find_consumer(ACPI_HANDLE consumer);
11478915Smsmith
115119529Snjl/* Initialise our lists. */
11678915Smsmithstatic void
11778915Smsmithacpi_pwr_init(void *junk)
11878915Smsmith{
11978915Smsmith    TAILQ_INIT(&acpi_powerresources);
12078915Smsmith    TAILQ_INIT(&acpi_powerconsumers);
12178915Smsmith}
12278915SmsmithSYSINIT(acpi_powerresource, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_pwr_init, NULL);
12378915Smsmith
12478915Smsmith/*
12578915Smsmith * Register a power resource.
12678915Smsmith *
12778915Smsmith * It's OK to call this if we already know about the resource.
12878915Smsmith */
12978915Smsmithstatic ACPI_STATUS
13078915Smsmithacpi_pwr_register_resource(ACPI_HANDLE res)
13178915Smsmith{
13278915Smsmith    ACPI_STATUS			status;
13378915Smsmith    ACPI_BUFFER			buf;
13479493Smsmith    ACPI_OBJECT			*obj;
13578915Smsmith    struct acpi_powerresource	*rp, *srp;
13678915Smsmith
13796926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
13878915Smsmith
13978915Smsmith    rp = NULL;
14091125Smsmith    buf.Pointer = NULL;
14178915Smsmith
142119529Snjl    /* Look to see if we know about this resource */
14378915Smsmith    if (acpi_pwr_find_resource(res) != NULL)
144119529Snjl	return_ACPI_STATUS (AE_OK);		/* already know about it */
14578915Smsmith
146119529Snjl    /* Allocate a new resource */
14778915Smsmith    if ((rp = malloc(sizeof(*rp), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) {
14878915Smsmith	status = AE_NO_MEMORY;
14978915Smsmith	goto out;
15078915Smsmith    }
15178915Smsmith    TAILQ_INIT(&rp->ap_references);
15278915Smsmith    rp->ap_resource = res;
15378915Smsmith
154119529Snjl    /* Get the Power Resource object */
15591125Smsmith    buf.Length = ACPI_ALLOCATE_BUFFER;
15691125Smsmith    if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) {
15782372Smsmith	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n"));
15878915Smsmith	goto out;
15978915Smsmith    }
16078915Smsmith    obj = buf.Pointer;
16179493Smsmith    if (obj->Type != ACPI_TYPE_POWER) {
162119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
163119529Snjl			 "questionable power resource object %s\n",
164119529Snjl			 acpi_name(res)));
16579493Smsmith	status = AE_TYPE;
16679493Smsmith	goto out;
16778915Smsmith    }
16879493Smsmith    rp->ap_systemlevel = obj->PowerResource.SystemLevel;
16979493Smsmith    rp->ap_order = obj->PowerResource.ResourceOrder;
17078915Smsmith
171119529Snjl    /* Sort the resource into the list */
17278915Smsmith    status = AE_OK;
17378915Smsmith    srp = TAILQ_FIRST(&acpi_powerresources);
174119529Snjl    if (srp == NULL || rp->ap_order < srp->ap_order) {
17578915Smsmith	TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link);
17679357Smsmith	goto done;
17778915Smsmith    }
178119529Snjl    TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) {
17978915Smsmith	if (rp->ap_order < srp->ap_order) {
18078915Smsmith	    TAILQ_INSERT_BEFORE(srp, rp, ap_link);
18179357Smsmith	    goto done;
18278915Smsmith	}
183119529Snjl    }
18478915Smsmith    TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link);
18579357Smsmith
18679357Smsmith done:
187119529Snjl    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
188119529Snjl		     "registered power resource %s\n", acpi_name(res)));
189119529Snjl
19078915Smsmith out:
19191125Smsmith    if (buf.Pointer != NULL)
19291125Smsmith	AcpiOsFree(buf.Pointer);
193119529Snjl    if (ACPI_FAILURE(status) && rp != NULL)
19478915Smsmith	free(rp, M_ACPIPWR);
195119529Snjl    return_ACPI_STATUS (status);
19678915Smsmith}
19778915Smsmith
198128248Snjl#ifdef notyet
19978915Smsmith/*
20078915Smsmith * Deregister a power resource.
20178915Smsmith */
20278915Smsmithstatic ACPI_STATUS
20378915Smsmithacpi_pwr_deregister_resource(ACPI_HANDLE res)
20478915Smsmith{
20578915Smsmith    struct acpi_powerresource	*rp;
20678915Smsmith
20796926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
20878915Smsmith
20978915Smsmith    rp = NULL;
21078915Smsmith
211119529Snjl    /* Find the resource */
21278915Smsmith    if ((rp = acpi_pwr_find_resource(res)) == NULL)
213119529Snjl	return_ACPI_STATUS (AE_BAD_PARAMETER);
21478915Smsmith
215119529Snjl    /* Check that there are no consumers referencing this resource */
21678915Smsmith    if (TAILQ_FIRST(&rp->ap_references) != NULL)
217119529Snjl	return_ACPI_STATUS (AE_BAD_PARAMETER);
21878915Smsmith
219119529Snjl    /* Pull it off the list and free it */
22078915Smsmith    TAILQ_REMOVE(&acpi_powerresources, rp, ap_link);
22178915Smsmith    free(rp, M_ACPIPWR);
22278915Smsmith
223119529Snjl    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n",
224119529Snjl		     acpi_name(res)));
22578915Smsmith
226119529Snjl    return_ACPI_STATUS (AE_OK);
22778915Smsmith}
228128248Snjl#endif /* notyet */
22978915Smsmith
23078915Smsmith/*
23178915Smsmith * Register a power consumer.
23278915Smsmith *
23378915Smsmith * It's OK to call this if we already know about the consumer.
23478915Smsmith */
23578915Smsmithstatic ACPI_STATUS
23678915Smsmithacpi_pwr_register_consumer(ACPI_HANDLE consumer)
23778915Smsmith{
23878915Smsmith    struct acpi_powerconsumer	*pc;
23978915Smsmith
24096926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
24178915Smsmith
242119529Snjl    /* Check to see whether we know about this consumer already */
24378915Smsmith    if ((pc = acpi_pwr_find_consumer(consumer)) != NULL)
244119529Snjl	return_ACPI_STATUS (AE_OK);
24578915Smsmith
246119529Snjl    /* Allocate a new power consumer */
24778915Smsmith    if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL)
248119529Snjl	return_ACPI_STATUS (AE_NO_MEMORY);
24978915Smsmith    TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link);
25078915Smsmith    TAILQ_INIT(&pc->ac_references);
25178915Smsmith    pc->ac_consumer = consumer;
25278915Smsmith
253119529Snjl    /* XXX we should try to find its current state */
254119529Snjl    pc->ac_state = ACPI_STATE_UNKNOWN;
25578915Smsmith
256119529Snjl    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n",
257119529Snjl		     acpi_name(consumer)));
25878915Smsmith
259119529Snjl    return_ACPI_STATUS (AE_OK);
26078915Smsmith}
26178915Smsmith
262128248Snjl#ifdef notyet
26378915Smsmith/*
26478915Smsmith * Deregister a power consumer.
26578915Smsmith *
26678915Smsmith * This should only be done once the consumer has been powered off.
26778915Smsmith * (XXX is this correct?  Check once implemented)
26878915Smsmith */
26978915Smsmithstatic ACPI_STATUS
27078915Smsmithacpi_pwr_deregister_consumer(ACPI_HANDLE consumer)
27178915Smsmith{
27278915Smsmith    struct acpi_powerconsumer	*pc;
27378915Smsmith
27496926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
27578915Smsmith
276119529Snjl    /* Find the consumer */
27778915Smsmith    if ((pc = acpi_pwr_find_consumer(consumer)) == NULL)
278119529Snjl	return_ACPI_STATUS (AE_BAD_PARAMETER);
27978915Smsmith
280119529Snjl    /* Make sure the consumer's not referencing anything right now */
28178915Smsmith    if (TAILQ_FIRST(&pc->ac_references) != NULL)
282119529Snjl	return_ACPI_STATUS (AE_BAD_PARAMETER);
28378915Smsmith
284119529Snjl    /* Pull the consumer off the list and free it */
28578915Smsmith    TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link);
28678915Smsmith
287119529Snjl    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n",
288119529Snjl		     acpi_name(consumer)));
28978915Smsmith
290119529Snjl    return_ACPI_STATUS (AE_OK);
29178915Smsmith}
292128248Snjl#endif /* notyet */
29378915Smsmith
29478915Smsmith/*
29578915Smsmith * Set a power consumer to a particular power state.
29678915Smsmith */
29778915SmsmithACPI_STATUS
29878915Smsmithacpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state)
29978915Smsmith{
30078915Smsmith    struct acpi_powerconsumer	*pc;
30178915Smsmith    struct acpi_powerreference	*pr;
30282084Siwasaki    ACPI_HANDLE			method_handle, reslist_handle, pr0_handle;
30378915Smsmith    ACPI_BUFFER			reslist_buffer;
30478915Smsmith    ACPI_OBJECT			*reslist_object;
30578915Smsmith    ACPI_STATUS			status;
30678915Smsmith    char			*method_name, *reslist_name;
30778915Smsmith    int				res_changed;
30878915Smsmith
30996926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
31078915Smsmith
311128252Snjl    /* It's never ok to switch a non-existent consumer. */
312128252Snjl    if (consumer == NULL)
313128252Snjl	return_ACPI_STATUS (AE_NOT_FOUND);
314128252Snjl
315119529Snjl    /* Find the consumer */
31678915Smsmith    if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
31791125Smsmith	if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer)))
318119529Snjl	    return_ACPI_STATUS (status);
31978915Smsmith	if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
320119529Snjl	    return_ACPI_STATUS (AE_ERROR);	/* something very wrong */
32178915Smsmith	}
32278915Smsmith    }
32378915Smsmith
324119529Snjl    /* Check for valid transitions */
325119529Snjl    if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0)
326119529Snjl	return_ACPI_STATUS (AE_BAD_PARAMETER);	/* can only go to D0 from D3 */
32778915Smsmith
328119529Snjl    /* Find transition mechanism(s) */
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:
347119529Snjl	return_ACPI_STATUS (AE_BAD_PARAMETER);
34878915Smsmith    }
34982372Smsmith    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n",
350119529Snjl		     acpi_name(consumer), pc->ac_state, state));
35178915Smsmith
35278915Smsmith    /*
35378915Smsmith     * Verify that this state is supported, ie. one of method or
35478915Smsmith     * reslist must be present.  We need to do this before we go
35578915Smsmith     * dereferencing resources (since we might be trying to go to
35678915Smsmith     * a state we don't support).
35778915Smsmith     *
35878915Smsmith     * Note that if any states are supported, the device has to
35978915Smsmith     * support D0 and D3.  It's never an error to try to go to
36078915Smsmith     * D0.
36178915Smsmith     */
362130208Snjl    status = AE_BAD_PARAMETER;
36391125Smsmith    reslist_buffer.Pointer = NULL;
36482084Siwasaki    reslist_object = NULL;
36591125Smsmith    if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle)))
36678915Smsmith	method_handle = NULL;
36791125Smsmith    if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle)))
36878915Smsmith	reslist_handle = NULL;
369119529Snjl    if (reslist_handle == NULL && method_handle == NULL) {
37078915Smsmith	if (state == ACPI_STATE_D0) {
37178915Smsmith	    pc->ac_state = ACPI_STATE_D0;
372119529Snjl	    return_ACPI_STATUS (AE_OK);
37378915Smsmith	}
374119529Snjl	if (state != ACPI_STATE_D3)
37582084Siwasaki	    goto bad;
37682084Siwasaki
377130208Snjl	/*
378130208Snjl	 * Turn off the resources listed in _PR0 to go to D3.  If there is
379130208Snjl	 * no _PR0 method, this object doesn't support ACPI power states.
380130208Snjl	 */
381130208Snjl	if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle))) {
382130208Snjl	    status = AE_NOT_FOUND;
38382084Siwasaki	    goto bad;
384130208Snjl	}
38591125Smsmith	reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
386119529Snjl	status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer);
387119529Snjl	if (ACPI_FAILURE(status))
38882084Siwasaki	    goto bad;
38982084Siwasaki	reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
390119529Snjl	if (reslist_object->Type != ACPI_TYPE_PACKAGE ||
391130208Snjl	    reslist_object->Package.Count == 0)
39282084Siwasaki	    goto bad;
39391125Smsmith	AcpiOsFree(reslist_buffer.Pointer);
39491125Smsmith	reslist_buffer.Pointer = NULL;
39591125Smsmith	reslist_object = NULL;
39678915Smsmith    }
39778915Smsmith
39878915Smsmith    /*
39978915Smsmith     * Check that we can actually fetch the list of power resources
40078915Smsmith     */
40178915Smsmith    if (reslist_handle != NULL) {
40291125Smsmith	reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
403119529Snjl	status = AcpiEvaluateObject(reslist_handle, NULL, NULL,
404119529Snjl				    &reslist_buffer);
405119529Snjl	if (ACPI_FAILURE(status)) {
406119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
407119529Snjl			     "can't evaluate resource list %s\n",
408119529Snjl			     acpi_name(reslist_handle)));
40991125Smsmith	    goto out;
41078915Smsmith	}
41178915Smsmith	reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
41278915Smsmith	if (reslist_object->Type != ACPI_TYPE_PACKAGE) {
413119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
414119529Snjl			     "resource list is not ACPI_TYPE_PACKAGE (%d)\n",
415119529Snjl			     reslist_object->Type));
41691125Smsmith	    status = AE_TYPE;
41791125Smsmith	    goto out;
41878915Smsmith	}
41978915Smsmith    }
42078915Smsmith
42178915Smsmith    /*
422125745Sjhb     * Now we are ready to switch, so kill off any current power
423119529Snjl     * resource references.
42478915Smsmith     */
42578915Smsmith    res_changed = 0;
426130208Snjl    while ((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) {
42779357Smsmith	res_changed = 1;
428119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n",
429119529Snjl			 acpi_name(pr->ar_resource->ap_resource)));
43078915Smsmith	TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink);
43179357Smsmith	TAILQ_REMOVE(&pc->ac_references, pr, ar_clink);
43279357Smsmith	free(pr, M_ACPIPWR);
43378915Smsmith    }
43478915Smsmith
43578915Smsmith    /*
43678915Smsmith     * Add new power resource references, if we have any.  Traverse the
43778915Smsmith     * package that we got from evaluating reslist_handle, and look up each
43878915Smsmith     * of the resources that are referenced.
43978915Smsmith     */
44078915Smsmith    if (reslist_object != NULL) {
44182372Smsmith	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n",
44282372Smsmith			  reslist_object->Package.Count));
443119529Snjl	acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource,
444119529Snjl				  pc);
44578915Smsmith	res_changed = 1;
44678915Smsmith    }
44778915Smsmith
44878915Smsmith    /*
44978915Smsmith     * If we changed anything in the resource list, we need to run a switch
45078915Smsmith     * pass now.
45178915Smsmith     */
45291125Smsmith    if (ACPI_FAILURE(status = acpi_pwr_switch_power())) {
453119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
454119529Snjl			 "failed to switch resources from %s to D%d\n",
45582372Smsmith			  acpi_name(consumer), state));
456119529Snjl
457119529Snjl	/* XXX is this appropriate?  Should we return to previous state? */
458119529Snjl	goto out;
45978915Smsmith    }
46078915Smsmith
461119529Snjl    /* Invoke power state switch method (if present) */
46278915Smsmith    if (method_handle != NULL) {
463119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
464119529Snjl			 "invoking state transition method %s\n",
465119529Snjl			 acpi_name(method_handle)));
466119529Snjl	status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL);
467119529Snjl	if (ACPI_FAILURE(status)) {
46891125Smsmith		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n",
469119529Snjl				 AcpiFormatException(status)));
47091125Smsmith		pc->ac_state = ACPI_STATE_UNKNOWN;
471119529Snjl
472119529Snjl		/* XXX Should we return to previous state? */
473119529Snjl		goto out;
47491125Smsmith	}
47578915Smsmith    }
47691125Smsmith
477119529Snjl    /* Transition was successful */
47878915Smsmith    pc->ac_state = state;
479119529Snjl    return_ACPI_STATUS (AE_OK);
48082084Siwasaki
48182084Siwasaki bad:
482119529Snjl    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
483119529Snjl		     "attempt to set unsupported state D%d\n", state));
48491125Smsmith
48591125Smsmith out:
48691125Smsmith    if (reslist_buffer.Pointer != NULL)
48791125Smsmith	AcpiOsFree(reslist_buffer.Pointer);
488119529Snjl    return_ACPI_STATUS (status);
48978915Smsmith}
49078915Smsmith
49178915Smsmith/*
49278915Smsmith * Called to create a reference between a power consumer and a power resource
49378915Smsmith * identified in the object.
49478915Smsmith */
49578915Smsmithstatic void
49678915Smsmithacpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg)
49778915Smsmith{
49878915Smsmith    struct acpi_powerconsumer	*pc = (struct acpi_powerconsumer *)arg;
49979357Smsmith    struct acpi_powerreference	*pr;
50079357Smsmith    struct acpi_powerresource	*rp;
50179357Smsmith    ACPI_HANDLE			res;
50279357Smsmith    ACPI_STATUS			status;
50378915Smsmith
50496926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
50578915Smsmith
506128047Snjl    res = acpi_GetReference(NULL, obj);
507128047Snjl    if (res == NULL) {
508119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
509128047Snjl			 "can't create a power reference for object type %d\n",
510119529Snjl			 obj->Type));
51179357Smsmith	return_VOID;
51279357Smsmith    }
51378915Smsmith
514119529Snjl    /* Create/look up the resource */
51579357Smsmith    if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) {
516119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
517119529Snjl			 "couldn't register power resource %s - %s\n",
518119529Snjl			 obj->String.Pointer, AcpiFormatException(status)));
51979357Smsmith	return_VOID;
52079357Smsmith    }
52179357Smsmith    if ((rp = acpi_pwr_find_resource(res)) == NULL) {
52282372Smsmith	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n"));
52379357Smsmith	return_VOID;
52479357Smsmith    }
525119529Snjl    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n",
526119529Snjl		     acpi_name(rp->ap_resource)));
52779357Smsmith
528119529Snjl    /* Create a reference between the consumer and resource */
52979357Smsmith    if ((pr = malloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) {
530119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
531119529Snjl			 "allocation failed for a power consumer reference\n"));
53279357Smsmith	return_VOID;
53379357Smsmith    }
53479357Smsmith    pr->ar_consumer = pc;
53579357Smsmith    pr->ar_resource = rp;
53679357Smsmith    TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink);
53779357Smsmith    TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink);
53879357Smsmith
53978915Smsmith    return_VOID;
54078915Smsmith}
54178915Smsmith
54278915Smsmith
54378915Smsmith/*
54478915Smsmith * Switch power resources to conform to the desired state.
54578915Smsmith *
54678915Smsmith * Consumers may have modified the power resource list in an arbitrary
54778915Smsmith * fashion; we sweep it in sequence order.
54878915Smsmith */
54978915Smsmithstatic ACPI_STATUS
55078915Smsmithacpi_pwr_switch_power(void)
55178915Smsmith{
55278915Smsmith    struct acpi_powerresource	*rp;
55378915Smsmith    ACPI_STATUS			status;
55478915Smsmith    int				cur;
55578915Smsmith
55696926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
55778915Smsmith
55878915Smsmith    /*
55978915Smsmith     * Sweep the list forwards turning things on.
56078915Smsmith     */
56178915Smsmith    TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
56279357Smsmith	if (TAILQ_FIRST(&rp->ap_references) == NULL) {
563119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
564119529Snjl			     "%s has no references, not turning on\n",
565119529Snjl			     acpi_name(rp->ap_resource)));
56679357Smsmith	    continue;
56779357Smsmith	}
56878915Smsmith
569119529Snjl	/* We could cache this if we trusted it not to change under us */
570126560Snjl	status = acpi_GetInteger(rp->ap_resource, "_STA", &cur);
571119529Snjl	if (ACPI_FAILURE(status)) {
57282372Smsmith	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
57382372Smsmith			      acpi_name(rp->ap_resource), status));
574119529Snjl
575119529Snjl	    /* XXX is this correct?  Always switch if in doubt? */
576119529Snjl	    continue;
57778915Smsmith	}
57878915Smsmith
57978915Smsmith	/*
58078915Smsmith	 * Switch if required.  Note that we ignore the result of the switch
58178915Smsmith	 * effort; we don't know what to do if it fails, so checking wouldn't
58278915Smsmith	 * help much.
58378915Smsmith	 */
58479357Smsmith	if (cur != ACPI_PWR_ON) {
585119529Snjl	    status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL);
586119529Snjl	    if (ACPI_FAILURE(status)) {
587119529Snjl		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
588119529Snjl				 "failed to switch %s on - %s\n",
589119529Snjl				 acpi_name(rp->ap_resource),
590119529Snjl				 AcpiFormatException(status)));
59179357Smsmith	    } else {
592119529Snjl		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n",
593119529Snjl				 acpi_name(rp->ap_resource)));
59479357Smsmith	    }
59579357Smsmith	} else {
596119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n",
597119529Snjl			     acpi_name(rp->ap_resource)));
59879357Smsmith	}
59978915Smsmith    }
60078915Smsmith
601119529Snjl    /* Sweep the list backwards turning things off. */
602119529Snjl    TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list,
603119529Snjl	ap_link) {
604119529Snjl
60579357Smsmith	if (TAILQ_FIRST(&rp->ap_references) != NULL) {
606119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
607119529Snjl			     "%s has references, not turning off\n",
608119529Snjl			     acpi_name(rp->ap_resource)));
60979357Smsmith	    continue;
61079357Smsmith	}
61178915Smsmith
612119529Snjl	/* We could cache this if we trusted it not to change under us */
613126560Snjl	status = acpi_GetInteger(rp->ap_resource, "_STA", &cur);
614119529Snjl	if (ACPI_FAILURE(status)) {
61582372Smsmith	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
61682372Smsmith			      acpi_name(rp->ap_resource), status));
617119529Snjl	    /* XXX is this correct?  Always switch if in doubt? */
618119529Snjl	    continue;
61978915Smsmith	}
62078915Smsmith
62178915Smsmith	/*
62278915Smsmith	 * Switch if required.  Note that we ignore the result of the switch
62378915Smsmith	 * effort; we don't know what to do if it fails, so checking wouldn't
62478915Smsmith	 * help much.
62578915Smsmith	 */
62679357Smsmith	if (cur != ACPI_PWR_OFF) {
627119529Snjl	    status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL);
628119529Snjl	    if (ACPI_FAILURE(status)) {
629119529Snjl		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
630119529Snjl				 "failed to switch %s off - %s\n",
631119529Snjl				 acpi_name(rp->ap_resource),
632119529Snjl				 AcpiFormatException(status)));
63379357Smsmith	    } else {
634119529Snjl		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n",
635119529Snjl				 acpi_name(rp->ap_resource)));
63679357Smsmith	    }
63779357Smsmith	} else {
638119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n",
639119529Snjl			     acpi_name(rp->ap_resource)));
64079357Smsmith	}
64178915Smsmith    }
642119529Snjl
643119529Snjl    return_ACPI_STATUS (AE_OK);
64478915Smsmith}
64578915Smsmith
64678915Smsmith/*
64778915Smsmith * Find a power resource's control structure.
64878915Smsmith */
64978915Smsmithstatic struct acpi_powerresource *
65078915Smsmithacpi_pwr_find_resource(ACPI_HANDLE res)
65178915Smsmith{
65278915Smsmith    struct acpi_powerresource	*rp;
65378915Smsmith
65496926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
65578915Smsmith
656119529Snjl    TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
65778915Smsmith	if (rp->ap_resource == res)
65878915Smsmith	    break;
659119529Snjl    }
660119529Snjl
661119529Snjl    return_PTR (rp);
66278915Smsmith}
66378915Smsmith
66478915Smsmith/*
66578915Smsmith * Find a power consumer's control structure.
66678915Smsmith */
66778915Smsmithstatic struct acpi_powerconsumer *
66878915Smsmithacpi_pwr_find_consumer(ACPI_HANDLE consumer)
66978915Smsmith{
67078915Smsmith    struct acpi_powerconsumer	*pc;
67178915Smsmith
67296926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
67378915Smsmith
674119529Snjl    TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) {
67578915Smsmith	if (pc->ac_consumer == consumer)
67678915Smsmith	    break;
677119529Snjl    }
678119529Snjl
679119529Snjl    return_PTR (pc);
68078915Smsmith}
681