2 * Copyright (c) 2001 Michael Smith
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/dev/acpica/acpi_powerres.c,v 1.17 2004/02/12 20:45:01 jhb Exp $
27 * $DragonFly: src/sys/dev/acpica5/acpi_powerres.c,v 1.2 2004/05/05 22:19:24 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
37 #include <dev/acpica5/acpivar.h>
40 * ACPI power resource management.
42 * Power resource behaviour is slightly complicated by the fact that
43 * a single power resource may provide power for more than one device.
44 * Thus, we must track the device(s) being powered by a given power
45 * resource, and only deactivate it when there are no powered devices.
47 * Note that this only manages resources for known devices. There is an
48 * ugly case where we may turn of power to a device which is in use because
49 * we don't know that it depends on a given resource. We should perhaps
50 * try to be smarter about this, but a more complete solution would involve
51 * scanning all of the ACPI namespace to find devices we're not currently
52 * aware of, and this raises questions about whether they should be left
53 * on, turned off, etc.
58 MALLOC_DEFINE(M_ACPIPWR, "acpipwr", "ACPI power resources");
60 /* Hooks for the ACPI CA debugging infrastructure */
61 #define _COMPONENT ACPI_POWER
62 ACPI_MODULE_NAME("POWERRES")
64 /* Return values from _STA on a power resource */
65 #define ACPI_PWR_OFF 0
68 /* A relationship between a power resource and a consumer. */
69 struct acpi_powerreference {
70 struct acpi_powerconsumer *ar_consumer;
71 struct acpi_powerresource *ar_resource;
72 TAILQ_ENTRY(acpi_powerreference) ar_rlink; /* link on resource list */
73 TAILQ_ENTRY(acpi_powerreference) ar_clink; /* link on consumer */
76 /* A power-managed device. */
77 struct acpi_powerconsumer {
78 /* Device which is powered */
79 ACPI_HANDLE ac_consumer;
81 TAILQ_ENTRY(acpi_powerconsumer) ac_link;
82 TAILQ_HEAD(,acpi_powerreference) ac_references;
85 /* A power resource. */
86 struct acpi_powerresource {
87 TAILQ_ENTRY(acpi_powerresource) ap_link;
88 TAILQ_HEAD(,acpi_powerreference) ap_references;
89 ACPI_HANDLE ap_resource;
90 ACPI_INTEGER ap_systemlevel;
91 ACPI_INTEGER ap_order;
94 static TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource)
96 static TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer)
99 static ACPI_STATUS acpi_pwr_register_consumer(ACPI_HANDLE consumer);
100 static ACPI_STATUS acpi_pwr_deregister_consumer(ACPI_HANDLE consumer);
101 static ACPI_STATUS acpi_pwr_register_resource(ACPI_HANDLE res);
102 static ACPI_STATUS acpi_pwr_deregister_resource(ACPI_HANDLE res);
103 static void acpi_pwr_reference_resource(ACPI_OBJECT *obj,
105 static ACPI_STATUS acpi_pwr_switch_power(void);
106 static struct acpi_powerresource
107 *acpi_pwr_find_resource(ACPI_HANDLE res);
108 static struct acpi_powerconsumer
109 *acpi_pwr_find_consumer(ACPI_HANDLE consumer);
111 /* Initialise our lists. */
113 acpi_pwr_init(void *junk)
115 TAILQ_INIT(&acpi_powerresources);
116 TAILQ_INIT(&acpi_powerconsumers);
118 SYSINIT(acpi_powerresource, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_pwr_init, NULL);
121 * Register a power resource.
123 * It's OK to call this if we already know about the resource.
126 acpi_pwr_register_resource(ACPI_HANDLE res)
131 struct acpi_powerresource *rp, *srp;
133 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
138 /* Look to see if we know about this resource */
139 if (acpi_pwr_find_resource(res) != NULL)
140 return_ACPI_STATUS (AE_OK); /* already know about it */
142 /* Allocate a new resource */
143 rp = malloc(sizeof(*rp), M_ACPIPWR, M_INTWAIT | M_ZERO);
144 TAILQ_INIT(&rp->ap_references);
145 rp->ap_resource = res;
147 /* Get the Power Resource object */
148 buf.Length = ACPI_ALLOCATE_BUFFER;
149 if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) {
150 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n"));
154 if (obj->Type != ACPI_TYPE_POWER) {
155 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
156 "questionable power resource object %s\n",
161 rp->ap_systemlevel = obj->PowerResource.SystemLevel;
162 rp->ap_order = obj->PowerResource.ResourceOrder;
164 /* Sort the resource into the list */
166 srp = TAILQ_FIRST(&acpi_powerresources);
167 if (srp == NULL || rp->ap_order < srp->ap_order) {
168 TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link);
171 TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) {
172 if (rp->ap_order < srp->ap_order) {
173 TAILQ_INSERT_BEFORE(srp, rp, ap_link);
177 TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link);
180 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
181 "registered power resource %s\n", acpi_name(res)));
184 if (buf.Pointer != NULL)
185 AcpiOsFree(buf.Pointer);
186 if (ACPI_FAILURE(status) && rp != NULL)
188 return_ACPI_STATUS (status);
192 * Deregister a power resource.
195 acpi_pwr_deregister_resource(ACPI_HANDLE res)
197 struct acpi_powerresource *rp;
199 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
203 /* Find the resource */
204 if ((rp = acpi_pwr_find_resource(res)) == NULL)
205 return_ACPI_STATUS (AE_BAD_PARAMETER);
207 /* Check that there are no consumers referencing this resource */
208 if (TAILQ_FIRST(&rp->ap_references) != NULL)
209 return_ACPI_STATUS (AE_BAD_PARAMETER);
211 /* Pull it off the list and free it */
212 TAILQ_REMOVE(&acpi_powerresources, rp, ap_link);
215 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n",
218 return_ACPI_STATUS (AE_OK);
222 * Register a power consumer.
224 * It's OK to call this if we already know about the consumer.
227 acpi_pwr_register_consumer(ACPI_HANDLE consumer)
229 struct acpi_powerconsumer *pc;
231 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
233 /* Check to see whether we know about this consumer already */
234 if ((pc = acpi_pwr_find_consumer(consumer)) != NULL)
235 return_ACPI_STATUS (AE_OK);
237 /* Allocate a new power consumer */
238 pc = malloc(sizeof(*pc), M_ACPIPWR, M_INTWAIT);
239 TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link);
240 TAILQ_INIT(&pc->ac_references);
241 pc->ac_consumer = consumer;
243 /* XXX we should try to find its current state */
244 pc->ac_state = ACPI_STATE_UNKNOWN;
246 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n",
247 acpi_name(consumer)));
249 return_ACPI_STATUS (AE_OK);
253 * Deregister a power consumer.
255 * This should only be done once the consumer has been powered off.
256 * (XXX is this correct? Check once implemented)
259 acpi_pwr_deregister_consumer(ACPI_HANDLE consumer)
261 struct acpi_powerconsumer *pc;
263 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
265 /* Find the consumer */
266 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL)
267 return_ACPI_STATUS (AE_BAD_PARAMETER);
269 /* Make sure the consumer's not referencing anything right now */
270 if (TAILQ_FIRST(&pc->ac_references) != NULL)
271 return_ACPI_STATUS (AE_BAD_PARAMETER);
273 /* Pull the consumer off the list and free it */
274 TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link);
276 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n",
277 acpi_name(consumer)));
279 return_ACPI_STATUS (AE_OK);
283 * Set a power consumer to a particular power state.
286 acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state)
288 struct acpi_powerconsumer *pc;
289 struct acpi_powerreference *pr;
290 ACPI_HANDLE method_handle, reslist_handle, pr0_handle;
291 ACPI_BUFFER reslist_buffer;
292 ACPI_OBJECT *reslist_object;
294 char *method_name, *reslist_name;
297 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
299 /* Find the consumer */
300 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
301 if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer)))
302 return_ACPI_STATUS (status);
303 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
304 return_ACPI_STATUS (AE_ERROR); /* something very wrong */
308 /* Check for valid transitions */
309 if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0)
310 return_ACPI_STATUS (AE_BAD_PARAMETER); /* can only go to D0 from D3 */
312 /* Find transition mechanism(s) */
315 method_name = "_PS0";
316 reslist_name = "_PR0";
319 method_name = "_PS1";
320 reslist_name = "_PR1";
323 method_name = "_PS2";
324 reslist_name = "_PR2";
327 method_name = "_PS3";
328 reslist_name = "_PR3";
331 return_ACPI_STATUS (AE_BAD_PARAMETER);
333 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n",
334 acpi_name(consumer), pc->ac_state, state));
337 * Verify that this state is supported, ie. one of method or
338 * reslist must be present. We need to do this before we go
339 * dereferencing resources (since we might be trying to go to
340 * a state we don't support).
342 * Note that if any states are supported, the device has to
343 * support D0 and D3. It's never an error to try to go to
346 reslist_buffer.Pointer = NULL;
347 reslist_object = NULL;
348 if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle)))
349 method_handle = NULL;
350 if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle)))
351 reslist_handle = NULL;
352 if (reslist_handle == NULL && method_handle == NULL) {
353 if (state == ACPI_STATE_D0) {
354 pc->ac_state = ACPI_STATE_D0;
355 return_ACPI_STATUS (AE_OK);
357 if (state != ACPI_STATE_D3)
360 /* Turn off the resources listed in _PR0 to go to D3. */
361 if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle)))
363 reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
364 status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer);
365 if (ACPI_FAILURE(status))
367 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
368 if (reslist_object->Type != ACPI_TYPE_PACKAGE ||
369 reslist_object->Package.Count == 0) {
373 AcpiOsFree(reslist_buffer.Pointer);
374 reslist_buffer.Pointer = NULL;
375 reslist_object = NULL;
379 * Check that we can actually fetch the list of power resources
381 if (reslist_handle != NULL) {
382 reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
383 status = AcpiEvaluateObject(reslist_handle, NULL, NULL,
385 if (ACPI_FAILURE(status)) {
386 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
387 "can't evaluate resource list %s\n",
388 acpi_name(reslist_handle)));
391 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
392 if (reslist_object->Type != ACPI_TYPE_PACKAGE) {
393 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
394 "resource list is not ACPI_TYPE_PACKAGE (%d)\n",
395 reslist_object->Type));
402 * Now we are ready to switch, so kill off any current power
403 * resource references.
406 while((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) {
408 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n",
409 acpi_name(pr->ar_resource->ap_resource)));
410 TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink);
411 TAILQ_REMOVE(&pc->ac_references, pr, ar_clink);
416 * Add new power resource references, if we have any. Traverse the
417 * package that we got from evaluating reslist_handle, and look up each
418 * of the resources that are referenced.
420 if (reslist_object != NULL) {
421 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n",
422 reslist_object->Package.Count));
423 acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource,
429 * If we changed anything in the resource list, we need to run a switch
432 if (ACPI_FAILURE(status = acpi_pwr_switch_power())) {
433 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
434 "failed to switch resources from %s to D%d\n",
435 acpi_name(consumer), state));
437 /* XXX is this appropriate? Should we return to previous state? */
441 /* Invoke power state switch method (if present) */
442 if (method_handle != NULL) {
443 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
444 "invoking state transition method %s\n",
445 acpi_name(method_handle)));
446 status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL);
447 if (ACPI_FAILURE(status)) {
448 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n",
449 AcpiFormatException(status)));
450 pc->ac_state = ACPI_STATE_UNKNOWN;
452 /* XXX Should we return to previous state? */
457 /* Transition was successful */
458 pc->ac_state = state;
459 return_ACPI_STATUS (AE_OK);
462 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
463 "attempt to set unsupported state D%d\n", state));
464 status = AE_BAD_PARAMETER;
467 if (reslist_buffer.Pointer != NULL)
468 AcpiOsFree(reslist_buffer.Pointer);
469 return_ACPI_STATUS (status);
473 * Called to create a reference between a power consumer and a power resource
474 * identified in the object.
477 acpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg)
479 struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg;
480 struct acpi_powerreference *pr;
481 struct acpi_powerresource *rp;
485 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
487 /* check the object type */
490 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "building reference from %s to %s\n",
491 acpi_name(pc->ac_consumer),
492 acpi_name(obj->Reference.Handle)));
493 res = obj->Reference.Handle;
495 case ACPI_TYPE_STRING:
496 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "building reference from %s to %s\n",
497 acpi_name(pc->ac_consumer), obj->String.Pointer));
499 /* Get the handle of the resource */
500 status = AcpiGetHandle(NULL, obj->String.Pointer, &res);
501 if (ACPI_FAILURE(status)) {
502 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
503 "couldn't find power resource %s\n",
504 obj->String.Pointer));
509 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
510 "can't create a power reference for object type %d\n",
515 /* Create/look up the resource */
516 if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) {
517 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
518 "couldn't register power resource %s - %s\n",
519 obj->String.Pointer, AcpiFormatException(status)));
522 if ((rp = acpi_pwr_find_resource(res)) == NULL) {
523 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n"));
526 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n",
527 acpi_name(rp->ap_resource)));
529 /* Create a reference between the consumer and resource */
530 pr = malloc(sizeof(*pr), M_ACPIPWR, M_INTWAIT | M_ZERO);
531 pr->ar_consumer = pc;
532 pr->ar_resource = rp;
533 TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink);
534 TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink);
541 * Switch power resources to conform to the desired state.
543 * Consumers may have modified the power resource list in an arbitrary
544 * fashion; we sweep it in sequence order.
547 acpi_pwr_switch_power(void)
549 struct acpi_powerresource *rp;
553 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
556 * Sweep the list forwards turning things on.
558 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
559 if (TAILQ_FIRST(&rp->ap_references) == NULL) {
560 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
561 "%s has no references, not turning on\n",
562 acpi_name(rp->ap_resource)));
566 /* We could cache this if we trusted it not to change under us */
567 status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur);
568 if (ACPI_FAILURE(status)) {
569 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
570 acpi_name(rp->ap_resource), status));
572 /* XXX is this correct? Always switch if in doubt? */
577 * Switch if required. Note that we ignore the result of the switch
578 * effort; we don't know what to do if it fails, so checking wouldn't
581 if (cur != ACPI_PWR_ON) {
582 status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL);
583 if (ACPI_FAILURE(status)) {
584 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
585 "failed to switch %s on - %s\n",
586 acpi_name(rp->ap_resource),
587 AcpiFormatException(status)));
589 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n",
590 acpi_name(rp->ap_resource)));
593 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n",
594 acpi_name(rp->ap_resource)));
598 /* Sweep the list backwards turning things off. */
599 TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list,
602 if (TAILQ_FIRST(&rp->ap_references) != NULL) {
603 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
604 "%s has references, not turning off\n",
605 acpi_name(rp->ap_resource)));
609 /* We could cache this if we trusted it not to change under us */
610 status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur);
611 if (ACPI_FAILURE(status)) {
612 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
613 acpi_name(rp->ap_resource), status));
614 /* XXX is this correct? Always switch if in doubt? */
619 * Switch if required. Note that we ignore the result of the switch
620 * effort; we don't know what to do if it fails, so checking wouldn't
623 if (cur != ACPI_PWR_OFF) {
624 status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL);
625 if (ACPI_FAILURE(status)) {
626 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
627 "failed to switch %s off - %s\n",
628 acpi_name(rp->ap_resource),
629 AcpiFormatException(status)));
631 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n",
632 acpi_name(rp->ap_resource)));
635 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n",
636 acpi_name(rp->ap_resource)));
640 return_ACPI_STATUS (AE_OK);
644 * Find a power resource's control structure.
646 static struct acpi_powerresource *
647 acpi_pwr_find_resource(ACPI_HANDLE res)
649 struct acpi_powerresource *rp;
651 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
653 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
654 if (rp->ap_resource == res)
662 * Find a power consumer's control structure.
664 static struct acpi_powerconsumer *
665 acpi_pwr_find_consumer(ACPI_HANDLE consumer)
667 struct acpi_powerconsumer *pc;
669 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
671 TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) {
672 if (pc->ac_consumer == consumer)