acpi_powerres.c revision 128248
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 128248 2004-04-14 17:47:42Z 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
311119529Snjl    /* Find the consumer */
31278915Smsmith    if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
31391125Smsmith	if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer)))
314119529Snjl	    return_ACPI_STATUS (status);
31578915Smsmith	if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
316119529Snjl	    return_ACPI_STATUS (AE_ERROR);	/* something very wrong */
31778915Smsmith	}
31878915Smsmith    }
31978915Smsmith
320119529Snjl    /* Check for valid transitions */
321119529Snjl    if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0)
322119529Snjl	return_ACPI_STATUS (AE_BAD_PARAMETER);	/* can only go to D0 from D3 */
32378915Smsmith
324119529Snjl    /* Find transition mechanism(s) */
32578915Smsmith    switch(state) {
32678915Smsmith    case ACPI_STATE_D0:
32778915Smsmith	method_name = "_PS0";
32878915Smsmith	reslist_name = "_PR0";
32978915Smsmith	break;
33078915Smsmith    case ACPI_STATE_D1:
33178915Smsmith	method_name = "_PS1";
33278915Smsmith	reslist_name = "_PR1";
33378915Smsmith	break;
33478915Smsmith    case ACPI_STATE_D2:
33578915Smsmith	method_name = "_PS2";
33678915Smsmith	reslist_name = "_PR2";
33778915Smsmith	break;
33878915Smsmith    case ACPI_STATE_D3:
33978915Smsmith	method_name = "_PS3";
34078915Smsmith	reslist_name = "_PR3";
34178915Smsmith	break;
34278915Smsmith    default:
343119529Snjl	return_ACPI_STATUS (AE_BAD_PARAMETER);
34478915Smsmith    }
34582372Smsmith    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n",
346119529Snjl		     acpi_name(consumer), pc->ac_state, state));
34778915Smsmith
34878915Smsmith    /*
34978915Smsmith     * Verify that this state is supported, ie. one of method or
35078915Smsmith     * reslist must be present.  We need to do this before we go
35178915Smsmith     * dereferencing resources (since we might be trying to go to
35278915Smsmith     * a state we don't support).
35378915Smsmith     *
35478915Smsmith     * Note that if any states are supported, the device has to
35578915Smsmith     * support D0 and D3.  It's never an error to try to go to
35678915Smsmith     * D0.
35778915Smsmith     */
35891125Smsmith    reslist_buffer.Pointer = NULL;
35982084Siwasaki    reslist_object = NULL;
36091125Smsmith    if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle)))
36178915Smsmith	method_handle = NULL;
36291125Smsmith    if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle)))
36378915Smsmith	reslist_handle = NULL;
364119529Snjl    if (reslist_handle == NULL && method_handle == NULL) {
36578915Smsmith	if (state == ACPI_STATE_D0) {
36678915Smsmith	    pc->ac_state = ACPI_STATE_D0;
367119529Snjl	    return_ACPI_STATUS (AE_OK);
36878915Smsmith	}
369119529Snjl	if (state != ACPI_STATE_D3)
37082084Siwasaki	    goto bad;
37182084Siwasaki
372119529Snjl	/* Turn off the resources listed in _PR0 to go to D3. */
373119529Snjl	if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle)))
37482084Siwasaki	    goto bad;
37591125Smsmith	reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
376119529Snjl	status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer);
377119529Snjl	if (ACPI_FAILURE(status))
37882084Siwasaki	    goto bad;
37982084Siwasaki	reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
380119529Snjl	if (reslist_object->Type != ACPI_TYPE_PACKAGE ||
381119529Snjl	    reslist_object->Package.Count == 0) {
382119529Snjl
38382084Siwasaki	    goto bad;
38482084Siwasaki	}
38591125Smsmith	AcpiOsFree(reslist_buffer.Pointer);
38691125Smsmith	reslist_buffer.Pointer = NULL;
38791125Smsmith	reslist_object = NULL;
38878915Smsmith    }
38978915Smsmith
39078915Smsmith    /*
39178915Smsmith     * Check that we can actually fetch the list of power resources
39278915Smsmith     */
39378915Smsmith    if (reslist_handle != NULL) {
39491125Smsmith	reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
395119529Snjl	status = AcpiEvaluateObject(reslist_handle, NULL, NULL,
396119529Snjl				    &reslist_buffer);
397119529Snjl	if (ACPI_FAILURE(status)) {
398119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
399119529Snjl			     "can't evaluate resource list %s\n",
400119529Snjl			     acpi_name(reslist_handle)));
40191125Smsmith	    goto out;
40278915Smsmith	}
40378915Smsmith	reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
40478915Smsmith	if (reslist_object->Type != ACPI_TYPE_PACKAGE) {
405119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
406119529Snjl			     "resource list is not ACPI_TYPE_PACKAGE (%d)\n",
407119529Snjl			     reslist_object->Type));
40891125Smsmith	    status = AE_TYPE;
40991125Smsmith	    goto out;
41078915Smsmith	}
41178915Smsmith    }
41278915Smsmith
41378915Smsmith    /*
414125745Sjhb     * Now we are ready to switch, so kill off any current power
415119529Snjl     * resource references.
41678915Smsmith     */
41778915Smsmith    res_changed = 0;
41879357Smsmith    while((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) {
41979357Smsmith	res_changed = 1;
420119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n",
421119529Snjl			 acpi_name(pr->ar_resource->ap_resource)));
42278915Smsmith	TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink);
42379357Smsmith	TAILQ_REMOVE(&pc->ac_references, pr, ar_clink);
42479357Smsmith	free(pr, M_ACPIPWR);
42578915Smsmith    }
42678915Smsmith
42778915Smsmith    /*
42878915Smsmith     * Add new power resource references, if we have any.  Traverse the
42978915Smsmith     * package that we got from evaluating reslist_handle, and look up each
43078915Smsmith     * of the resources that are referenced.
43178915Smsmith     */
43278915Smsmith    if (reslist_object != NULL) {
43382372Smsmith	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n",
43482372Smsmith			  reslist_object->Package.Count));
435119529Snjl	acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource,
436119529Snjl				  pc);
43778915Smsmith	res_changed = 1;
43878915Smsmith    }
43978915Smsmith
44078915Smsmith    /*
44178915Smsmith     * If we changed anything in the resource list, we need to run a switch
44278915Smsmith     * pass now.
44378915Smsmith     */
44491125Smsmith    if (ACPI_FAILURE(status = acpi_pwr_switch_power())) {
445119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
446119529Snjl			 "failed to switch resources from %s to D%d\n",
44782372Smsmith			  acpi_name(consumer), state));
448119529Snjl
449119529Snjl	/* XXX is this appropriate?  Should we return to previous state? */
450119529Snjl	goto out;
45178915Smsmith    }
45278915Smsmith
453119529Snjl    /* Invoke power state switch method (if present) */
45478915Smsmith    if (method_handle != NULL) {
455119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
456119529Snjl			 "invoking state transition method %s\n",
457119529Snjl			 acpi_name(method_handle)));
458119529Snjl	status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL);
459119529Snjl	if (ACPI_FAILURE(status)) {
46091125Smsmith		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n",
461119529Snjl				 AcpiFormatException(status)));
46291125Smsmith		pc->ac_state = ACPI_STATE_UNKNOWN;
463119529Snjl
464119529Snjl		/* XXX Should we return to previous state? */
465119529Snjl		goto out;
46691125Smsmith	}
46778915Smsmith    }
46891125Smsmith
469119529Snjl    /* Transition was successful */
47078915Smsmith    pc->ac_state = state;
471119529Snjl    return_ACPI_STATUS (AE_OK);
47282084Siwasaki
47382084Siwasaki bad:
474119529Snjl    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
475119529Snjl		     "attempt to set unsupported state D%d\n", state));
47691125Smsmith    status = AE_BAD_PARAMETER;
47791125Smsmith
47891125Smsmith out:
47991125Smsmith    if (reslist_buffer.Pointer != NULL)
48091125Smsmith	AcpiOsFree(reslist_buffer.Pointer);
481119529Snjl    return_ACPI_STATUS (status);
48278915Smsmith}
48378915Smsmith
48478915Smsmith/*
48578915Smsmith * Called to create a reference between a power consumer and a power resource
48678915Smsmith * identified in the object.
48778915Smsmith */
48878915Smsmithstatic void
48978915Smsmithacpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg)
49078915Smsmith{
49178915Smsmith    struct acpi_powerconsumer	*pc = (struct acpi_powerconsumer *)arg;
49279357Smsmith    struct acpi_powerreference	*pr;
49379357Smsmith    struct acpi_powerresource	*rp;
49479357Smsmith    ACPI_HANDLE			res;
49579357Smsmith    ACPI_STATUS			status;
49678915Smsmith
49796926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
49878915Smsmith
499128047Snjl    res = acpi_GetReference(NULL, obj);
500128047Snjl    if (res == NULL) {
501119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
502128047Snjl			 "can't create a power reference for object type %d\n",
503119529Snjl			 obj->Type));
50479357Smsmith	return_VOID;
50579357Smsmith    }
50678915Smsmith
507119529Snjl    /* Create/look up the resource */
50879357Smsmith    if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) {
509119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
510119529Snjl			 "couldn't register power resource %s - %s\n",
511119529Snjl			 obj->String.Pointer, AcpiFormatException(status)));
51279357Smsmith	return_VOID;
51379357Smsmith    }
51479357Smsmith    if ((rp = acpi_pwr_find_resource(res)) == NULL) {
51582372Smsmith	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n"));
51679357Smsmith	return_VOID;
51779357Smsmith    }
518119529Snjl    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n",
519119529Snjl		     acpi_name(rp->ap_resource)));
52079357Smsmith
521119529Snjl    /* Create a reference between the consumer and resource */
52279357Smsmith    if ((pr = malloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) {
523119529Snjl	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
524119529Snjl			 "allocation failed for a power consumer reference\n"));
52579357Smsmith	return_VOID;
52679357Smsmith    }
52779357Smsmith    pr->ar_consumer = pc;
52879357Smsmith    pr->ar_resource = rp;
52979357Smsmith    TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink);
53079357Smsmith    TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink);
53179357Smsmith
53278915Smsmith    return_VOID;
53378915Smsmith}
53478915Smsmith
53578915Smsmith
53678915Smsmith/*
53778915Smsmith * Switch power resources to conform to the desired state.
53878915Smsmith *
53978915Smsmith * Consumers may have modified the power resource list in an arbitrary
54078915Smsmith * fashion; we sweep it in sequence order.
54178915Smsmith */
54278915Smsmithstatic ACPI_STATUS
54378915Smsmithacpi_pwr_switch_power(void)
54478915Smsmith{
54578915Smsmith    struct acpi_powerresource	*rp;
54678915Smsmith    ACPI_STATUS			status;
54778915Smsmith    int				cur;
54878915Smsmith
54996926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
55078915Smsmith
55178915Smsmith    /*
55278915Smsmith     * Sweep the list forwards turning things on.
55378915Smsmith     */
55478915Smsmith    TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
55579357Smsmith	if (TAILQ_FIRST(&rp->ap_references) == NULL) {
556119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
557119529Snjl			     "%s has no references, not turning on\n",
558119529Snjl			     acpi_name(rp->ap_resource)));
55979357Smsmith	    continue;
56079357Smsmith	}
56178915Smsmith
562119529Snjl	/* We could cache this if we trusted it not to change under us */
563126560Snjl	status = acpi_GetInteger(rp->ap_resource, "_STA", &cur);
564119529Snjl	if (ACPI_FAILURE(status)) {
56582372Smsmith	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
56682372Smsmith			      acpi_name(rp->ap_resource), status));
567119529Snjl
568119529Snjl	    /* XXX is this correct?  Always switch if in doubt? */
569119529Snjl	    continue;
57078915Smsmith	}
57178915Smsmith
57278915Smsmith	/*
57378915Smsmith	 * Switch if required.  Note that we ignore the result of the switch
57478915Smsmith	 * effort; we don't know what to do if it fails, so checking wouldn't
57578915Smsmith	 * help much.
57678915Smsmith	 */
57779357Smsmith	if (cur != ACPI_PWR_ON) {
578119529Snjl	    status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL);
579119529Snjl	    if (ACPI_FAILURE(status)) {
580119529Snjl		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
581119529Snjl				 "failed to switch %s on - %s\n",
582119529Snjl				 acpi_name(rp->ap_resource),
583119529Snjl				 AcpiFormatException(status)));
58479357Smsmith	    } else {
585119529Snjl		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n",
586119529Snjl				 acpi_name(rp->ap_resource)));
58779357Smsmith	    }
58879357Smsmith	} else {
589119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n",
590119529Snjl			     acpi_name(rp->ap_resource)));
59179357Smsmith	}
59278915Smsmith    }
59378915Smsmith
594119529Snjl    /* Sweep the list backwards turning things off. */
595119529Snjl    TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list,
596119529Snjl	ap_link) {
597119529Snjl
59879357Smsmith	if (TAILQ_FIRST(&rp->ap_references) != NULL) {
599119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
600119529Snjl			     "%s has references, not turning off\n",
601119529Snjl			     acpi_name(rp->ap_resource)));
60279357Smsmith	    continue;
60379357Smsmith	}
60478915Smsmith
605119529Snjl	/* We could cache this if we trusted it not to change under us */
606126560Snjl	status = acpi_GetInteger(rp->ap_resource, "_STA", &cur);
607119529Snjl	if (ACPI_FAILURE(status)) {
60882372Smsmith	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
60982372Smsmith			      acpi_name(rp->ap_resource), status));
610119529Snjl	    /* XXX is this correct?  Always switch if in doubt? */
611119529Snjl	    continue;
61278915Smsmith	}
61378915Smsmith
61478915Smsmith	/*
61578915Smsmith	 * Switch if required.  Note that we ignore the result of the switch
61678915Smsmith	 * effort; we don't know what to do if it fails, so checking wouldn't
61778915Smsmith	 * help much.
61878915Smsmith	 */
61979357Smsmith	if (cur != ACPI_PWR_OFF) {
620119529Snjl	    status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL);
621119529Snjl	    if (ACPI_FAILURE(status)) {
622119529Snjl		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
623119529Snjl				 "failed to switch %s off - %s\n",
624119529Snjl				 acpi_name(rp->ap_resource),
625119529Snjl				 AcpiFormatException(status)));
62679357Smsmith	    } else {
627119529Snjl		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n",
628119529Snjl				 acpi_name(rp->ap_resource)));
62979357Smsmith	    }
63079357Smsmith	} else {
631119529Snjl	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n",
632119529Snjl			     acpi_name(rp->ap_resource)));
63379357Smsmith	}
63478915Smsmith    }
635119529Snjl
636119529Snjl    return_ACPI_STATUS (AE_OK);
63778915Smsmith}
63878915Smsmith
63978915Smsmith/*
64078915Smsmith * Find a power resource's control structure.
64178915Smsmith */
64278915Smsmithstatic struct acpi_powerresource *
64378915Smsmithacpi_pwr_find_resource(ACPI_HANDLE res)
64478915Smsmith{
64578915Smsmith    struct acpi_powerresource	*rp;
64678915Smsmith
64796926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
64878915Smsmith
649119529Snjl    TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
65078915Smsmith	if (rp->ap_resource == res)
65178915Smsmith	    break;
652119529Snjl    }
653119529Snjl
654119529Snjl    return_PTR (rp);
65578915Smsmith}
65678915Smsmith
65778915Smsmith/*
65878915Smsmith * Find a power consumer's control structure.
65978915Smsmith */
66078915Smsmithstatic struct acpi_powerconsumer *
66178915Smsmithacpi_pwr_find_consumer(ACPI_HANDLE consumer)
66278915Smsmith{
66378915Smsmith    struct acpi_powerconsumer	*pc;
66478915Smsmith
66596926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
66678915Smsmith
667119529Snjl    TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) {
66878915Smsmith	if (pc->ac_consumer == consumer)
66978915Smsmith	    break;
670119529Snjl    }
671119529Snjl
672119529Snjl    return_PTR (pc);
67378915Smsmith}
674