xref: /freebsd/sys/dev/acpica/acpi_powerres.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*-
2  * Copyright (c) 2001 Michael Smith
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/bus.h>
35 
36 #include "acpi.h"
37 #include <dev/acpica/acpivar.h>
38 
39 /*
40  * ACPI power resource management.
41  *
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.
46  *
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.
54  *
55  * XXX locking
56  */
57 
58 MALLOC_DEFINE(M_ACPIPWR, "acpipwr", "ACPI power resources");
59 
60 /* Hooks for the ACPI CA debugging infrastructure */
61 #define _COMPONENT	ACPI_POWER
62 ACPI_MODULE_NAME("POWERRES")
63 
64 /* Return values from _STA on a power resource */
65 #define ACPI_PWR_OFF	0
66 #define ACPI_PWR_ON	1
67 
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 */
74 };
75 
76 /* A power-managed device. */
77 struct acpi_powerconsumer {
78     /* Device which is powered */
79     ACPI_HANDLE				ac_consumer;
80     int					ac_state;
81     TAILQ_ENTRY(acpi_powerconsumer)	ac_link;
82     TAILQ_HEAD(,acpi_powerreference)	ac_references;
83 };
84 
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;
92 };
93 
94 static TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource)
95 	acpi_powerresources;
96 static TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer)
97 	acpi_powerconsumers;
98 
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,
104 						    void *arg);
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);
110 
111 /* Initialise our lists. */
112 static void
113 acpi_pwr_init(void *junk)
114 {
115     TAILQ_INIT(&acpi_powerresources);
116     TAILQ_INIT(&acpi_powerconsumers);
117 }
118 SYSINIT(acpi_powerresource, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_pwr_init, NULL);
119 
120 /*
121  * Register a power resource.
122  *
123  * It's OK to call this if we already know about the resource.
124  */
125 static ACPI_STATUS
126 acpi_pwr_register_resource(ACPI_HANDLE res)
127 {
128     ACPI_STATUS			status;
129     ACPI_BUFFER			buf;
130     ACPI_OBJECT			*obj;
131     struct acpi_powerresource	*rp, *srp;
132 
133     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
134 
135     rp = NULL;
136     buf.Pointer = NULL;
137 
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 */
141 
142     /* Allocate a new resource */
143     if ((rp = malloc(sizeof(*rp), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) {
144 	status = AE_NO_MEMORY;
145 	goto out;
146     }
147     TAILQ_INIT(&rp->ap_references);
148     rp->ap_resource = res;
149 
150     /* Get the Power Resource object */
151     buf.Length = ACPI_ALLOCATE_BUFFER;
152     if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) {
153 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n"));
154 	goto out;
155     }
156     obj = buf.Pointer;
157     if (obj->Type != ACPI_TYPE_POWER) {
158 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
159 			 "questionable power resource object %s\n",
160 			 acpi_name(res)));
161 	status = AE_TYPE;
162 	goto out;
163     }
164     rp->ap_systemlevel = obj->PowerResource.SystemLevel;
165     rp->ap_order = obj->PowerResource.ResourceOrder;
166 
167     /* Sort the resource into the list */
168     status = AE_OK;
169     srp = TAILQ_FIRST(&acpi_powerresources);
170     if (srp == NULL || rp->ap_order < srp->ap_order) {
171 	TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link);
172 	goto done;
173     }
174     TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) {
175 	if (rp->ap_order < srp->ap_order) {
176 	    TAILQ_INSERT_BEFORE(srp, rp, ap_link);
177 	    goto done;
178 	}
179     }
180     TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link);
181 
182  done:
183     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
184 		     "registered power resource %s\n", acpi_name(res)));
185 
186  out:
187     if (buf.Pointer != NULL)
188 	AcpiOsFree(buf.Pointer);
189     if (ACPI_FAILURE(status) && rp != NULL)
190 	free(rp, M_ACPIPWR);
191     return_ACPI_STATUS (status);
192 }
193 
194 /*
195  * Deregister a power resource.
196  */
197 static ACPI_STATUS
198 acpi_pwr_deregister_resource(ACPI_HANDLE res)
199 {
200     struct acpi_powerresource	*rp;
201 
202     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
203 
204     rp = NULL;
205 
206     /* Find the resource */
207     if ((rp = acpi_pwr_find_resource(res)) == NULL)
208 	return_ACPI_STATUS (AE_BAD_PARAMETER);
209 
210     /* Check that there are no consumers referencing this resource */
211     if (TAILQ_FIRST(&rp->ap_references) != NULL)
212 	return_ACPI_STATUS (AE_BAD_PARAMETER);
213 
214     /* Pull it off the list and free it */
215     TAILQ_REMOVE(&acpi_powerresources, rp, ap_link);
216     free(rp, M_ACPIPWR);
217 
218     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n",
219 		     acpi_name(res)));
220 
221     return_ACPI_STATUS (AE_OK);
222 }
223 
224 /*
225  * Register a power consumer.
226  *
227  * It's OK to call this if we already know about the consumer.
228  */
229 static ACPI_STATUS
230 acpi_pwr_register_consumer(ACPI_HANDLE consumer)
231 {
232     struct acpi_powerconsumer	*pc;
233 
234     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
235 
236     /* Check to see whether we know about this consumer already */
237     if ((pc = acpi_pwr_find_consumer(consumer)) != NULL)
238 	return_ACPI_STATUS (AE_OK);
239 
240     /* Allocate a new power consumer */
241     if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL)
242 	return_ACPI_STATUS (AE_NO_MEMORY);
243     TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link);
244     TAILQ_INIT(&pc->ac_references);
245     pc->ac_consumer = consumer;
246 
247     /* XXX we should try to find its current state */
248     pc->ac_state = ACPI_STATE_UNKNOWN;
249 
250     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n",
251 		     acpi_name(consumer)));
252 
253     return_ACPI_STATUS (AE_OK);
254 }
255 
256 /*
257  * Deregister a power consumer.
258  *
259  * This should only be done once the consumer has been powered off.
260  * (XXX is this correct?  Check once implemented)
261  */
262 static ACPI_STATUS
263 acpi_pwr_deregister_consumer(ACPI_HANDLE consumer)
264 {
265     struct acpi_powerconsumer	*pc;
266 
267     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
268 
269     /* Find the consumer */
270     if ((pc = acpi_pwr_find_consumer(consumer)) == NULL)
271 	return_ACPI_STATUS (AE_BAD_PARAMETER);
272 
273     /* Make sure the consumer's not referencing anything right now */
274     if (TAILQ_FIRST(&pc->ac_references) != NULL)
275 	return_ACPI_STATUS (AE_BAD_PARAMETER);
276 
277     /* Pull the consumer off the list and free it */
278     TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link);
279 
280     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n",
281 		     acpi_name(consumer)));
282 
283     return_ACPI_STATUS (AE_OK);
284 }
285 
286 /*
287  * Set a power consumer to a particular power state.
288  */
289 ACPI_STATUS
290 acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state)
291 {
292     struct acpi_powerconsumer	*pc;
293     struct acpi_powerreference	*pr;
294     ACPI_HANDLE			method_handle, reslist_handle, pr0_handle;
295     ACPI_BUFFER			reslist_buffer;
296     ACPI_OBJECT			*reslist_object;
297     ACPI_STATUS			status;
298     char			*method_name, *reslist_name;
299     int				res_changed;
300 
301     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
302 
303     /* Find the consumer */
304     if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
305 	if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer)))
306 	    return_ACPI_STATUS (status);
307 	if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
308 	    return_ACPI_STATUS (AE_ERROR);	/* something very wrong */
309 	}
310     }
311 
312     /* Check for valid transitions */
313     if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0)
314 	return_ACPI_STATUS (AE_BAD_PARAMETER);	/* can only go to D0 from D3 */
315 
316     /* Find transition mechanism(s) */
317     switch(state) {
318     case ACPI_STATE_D0:
319 	method_name = "_PS0";
320 	reslist_name = "_PR0";
321 	break;
322     case ACPI_STATE_D1:
323 	method_name = "_PS1";
324 	reslist_name = "_PR1";
325 	break;
326     case ACPI_STATE_D2:
327 	method_name = "_PS2";
328 	reslist_name = "_PR2";
329 	break;
330     case ACPI_STATE_D3:
331 	method_name = "_PS3";
332 	reslist_name = "_PR3";
333 	break;
334     default:
335 	return_ACPI_STATUS (AE_BAD_PARAMETER);
336     }
337     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n",
338 		     acpi_name(consumer), pc->ac_state, state));
339 
340     /*
341      * Verify that this state is supported, ie. one of method or
342      * reslist must be present.  We need to do this before we go
343      * dereferencing resources (since we might be trying to go to
344      * a state we don't support).
345      *
346      * Note that if any states are supported, the device has to
347      * support D0 and D3.  It's never an error to try to go to
348      * D0.
349      */
350     reslist_buffer.Pointer = NULL;
351     reslist_object = NULL;
352     if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle)))
353 	method_handle = NULL;
354     if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle)))
355 	reslist_handle = NULL;
356     if (reslist_handle == NULL && method_handle == NULL) {
357 	if (state == ACPI_STATE_D0) {
358 	    pc->ac_state = ACPI_STATE_D0;
359 	    return_ACPI_STATUS (AE_OK);
360 	}
361 	if (state != ACPI_STATE_D3)
362 	    goto bad;
363 
364 	/* Turn off the resources listed in _PR0 to go to D3. */
365 	if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle)))
366 	    goto bad;
367 	reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
368 	status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer);
369 	if (ACPI_FAILURE(status))
370 	    goto bad;
371 	reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
372 	if (reslist_object->Type != ACPI_TYPE_PACKAGE ||
373 	    reslist_object->Package.Count == 0) {
374 
375 	    goto bad;
376 	}
377 	AcpiOsFree(reslist_buffer.Pointer);
378 	reslist_buffer.Pointer = NULL;
379 	reslist_object = NULL;
380     }
381 
382     /*
383      * Check that we can actually fetch the list of power resources
384      */
385     if (reslist_handle != NULL) {
386 	reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
387 	status = AcpiEvaluateObject(reslist_handle, NULL, NULL,
388 				    &reslist_buffer);
389 	if (ACPI_FAILURE(status)) {
390 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
391 			     "can't evaluate resource list %s\n",
392 			     acpi_name(reslist_handle)));
393 	    goto out;
394 	}
395 	reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
396 	if (reslist_object->Type != ACPI_TYPE_PACKAGE) {
397 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
398 			     "resource list is not ACPI_TYPE_PACKAGE (%d)\n",
399 			     reslist_object->Type));
400 	    status = AE_TYPE;
401 	    goto out;
402 	}
403     }
404 
405     /*
406      * Now we are ready to switch, so  kill off any current power
407      * resource references.
408      */
409     res_changed = 0;
410     while((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) {
411 	res_changed = 1;
412 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n",
413 			 acpi_name(pr->ar_resource->ap_resource)));
414 	TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink);
415 	TAILQ_REMOVE(&pc->ac_references, pr, ar_clink);
416 	free(pr, M_ACPIPWR);
417     }
418 
419     /*
420      * Add new power resource references, if we have any.  Traverse the
421      * package that we got from evaluating reslist_handle, and look up each
422      * of the resources that are referenced.
423      */
424     if (reslist_object != NULL) {
425 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n",
426 			  reslist_object->Package.Count));
427 	acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource,
428 				  pc);
429 	res_changed = 1;
430     }
431 
432     /*
433      * If we changed anything in the resource list, we need to run a switch
434      * pass now.
435      */
436     if (ACPI_FAILURE(status = acpi_pwr_switch_power())) {
437 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
438 			 "failed to switch resources from %s to D%d\n",
439 			  acpi_name(consumer), state));
440 
441 	/* XXX is this appropriate?  Should we return to previous state? */
442 	goto out;
443     }
444 
445     /* Invoke power state switch method (if present) */
446     if (method_handle != NULL) {
447 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
448 			 "invoking state transition method %s\n",
449 			 acpi_name(method_handle)));
450 	status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL);
451 	if (ACPI_FAILURE(status)) {
452 		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n",
453 				 AcpiFormatException(status)));
454 		pc->ac_state = ACPI_STATE_UNKNOWN;
455 
456 		/* XXX Should we return to previous state? */
457 		goto out;
458 	}
459     }
460 
461     /* Transition was successful */
462     pc->ac_state = state;
463     return_ACPI_STATUS (AE_OK);
464 
465  bad:
466     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
467 		     "attempt to set unsupported state D%d\n", state));
468     status = AE_BAD_PARAMETER;
469 
470  out:
471     if (reslist_buffer.Pointer != NULL)
472 	AcpiOsFree(reslist_buffer.Pointer);
473     return_ACPI_STATUS (status);
474 }
475 
476 /*
477  * Called to create a reference between a power consumer and a power resource
478  * identified in the object.
479  */
480 static void
481 acpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg)
482 {
483     struct acpi_powerconsumer	*pc = (struct acpi_powerconsumer *)arg;
484     struct acpi_powerreference	*pr;
485     struct acpi_powerresource	*rp;
486     ACPI_HANDLE			res;
487     ACPI_STATUS			status;
488 
489     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
490 
491     /* check the object type */
492     switch (obj->Type) {
493     case ACPI_TYPE_ANY:
494 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "building reference from %s to %s\n",
495 			 acpi_name(pc->ac_consumer),
496 			 acpi_name(obj->Reference.Handle)));
497 	res = obj->Reference.Handle;
498 	break;
499     case ACPI_TYPE_STRING:
500 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "building reference from %s to %s\n",
501 			  acpi_name(pc->ac_consumer), obj->String.Pointer));
502 
503 	/* Get the handle of the resource */
504 	status = AcpiGetHandle(NULL, obj->String.Pointer, &res);
505 	if (ACPI_FAILURE(status)) {
506 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
507 			     "couldn't find power resource %s\n",
508 			     obj->String.Pointer));
509 	    return_VOID;
510 	}
511 	break;
512     default:
513 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
514 			 "can't create a power reference for object type %d\n",
515 			 obj->Type));
516 	return_VOID;
517     }
518 
519     /* Create/look up the resource */
520     if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) {
521 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
522 			 "couldn't register power resource %s - %s\n",
523 			 obj->String.Pointer, AcpiFormatException(status)));
524 	return_VOID;
525     }
526     if ((rp = acpi_pwr_find_resource(res)) == NULL) {
527 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n"));
528 	return_VOID;
529     }
530     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n",
531 		     acpi_name(rp->ap_resource)));
532 
533     /* Create a reference between the consumer and resource */
534     if ((pr = malloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) {
535 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
536 			 "allocation failed for a power consumer reference\n"));
537 	return_VOID;
538     }
539     pr->ar_consumer = pc;
540     pr->ar_resource = rp;
541     TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink);
542     TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink);
543 
544     return_VOID;
545 }
546 
547 
548 /*
549  * Switch power resources to conform to the desired state.
550  *
551  * Consumers may have modified the power resource list in an arbitrary
552  * fashion; we sweep it in sequence order.
553  */
554 static ACPI_STATUS
555 acpi_pwr_switch_power(void)
556 {
557     struct acpi_powerresource	*rp;
558     ACPI_STATUS			status;
559     int				cur;
560 
561     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
562 
563     /*
564      * Sweep the list forwards turning things on.
565      */
566     TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
567 	if (TAILQ_FIRST(&rp->ap_references) == NULL) {
568 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
569 			     "%s has no references, not turning on\n",
570 			     acpi_name(rp->ap_resource)));
571 	    continue;
572 	}
573 
574 	/* We could cache this if we trusted it not to change under us */
575 	status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur);
576 	if (ACPI_FAILURE(status)) {
577 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
578 			      acpi_name(rp->ap_resource), status));
579 
580 	    /* XXX is this correct?  Always switch if in doubt? */
581 	    continue;
582 	}
583 
584 	/*
585 	 * Switch if required.  Note that we ignore the result of the switch
586 	 * effort; we don't know what to do if it fails, so checking wouldn't
587 	 * help much.
588 	 */
589 	if (cur != ACPI_PWR_ON) {
590 	    status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL);
591 	    if (ACPI_FAILURE(status)) {
592 		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
593 				 "failed to switch %s on - %s\n",
594 				 acpi_name(rp->ap_resource),
595 				 AcpiFormatException(status)));
596 	    } else {
597 		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n",
598 				 acpi_name(rp->ap_resource)));
599 	    }
600 	} else {
601 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n",
602 			     acpi_name(rp->ap_resource)));
603 	}
604     }
605 
606     /* Sweep the list backwards turning things off. */
607     TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list,
608 	ap_link) {
609 
610 	if (TAILQ_FIRST(&rp->ap_references) != NULL) {
611 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
612 			     "%s has references, not turning off\n",
613 			     acpi_name(rp->ap_resource)));
614 	    continue;
615 	}
616 
617 	/* We could cache this if we trusted it not to change under us */
618 	status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur);
619 	if (ACPI_FAILURE(status)) {
620 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
621 			      acpi_name(rp->ap_resource), status));
622 	    /* XXX is this correct?  Always switch if in doubt? */
623 	    continue;
624 	}
625 
626 	/*
627 	 * Switch if required.  Note that we ignore the result of the switch
628 	 * effort; we don't know what to do if it fails, so checking wouldn't
629 	 * help much.
630 	 */
631 	if (cur != ACPI_PWR_OFF) {
632 	    status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL);
633 	    if (ACPI_FAILURE(status)) {
634 		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
635 				 "failed to switch %s off - %s\n",
636 				 acpi_name(rp->ap_resource),
637 				 AcpiFormatException(status)));
638 	    } else {
639 		ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n",
640 				 acpi_name(rp->ap_resource)));
641 	    }
642 	} else {
643 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n",
644 			     acpi_name(rp->ap_resource)));
645 	}
646     }
647 
648     return_ACPI_STATUS (AE_OK);
649 }
650 
651 /*
652  * Find a power resource's control structure.
653  */
654 static struct acpi_powerresource *
655 acpi_pwr_find_resource(ACPI_HANDLE res)
656 {
657     struct acpi_powerresource	*rp;
658 
659     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
660 
661     TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
662 	if (rp->ap_resource == res)
663 	    break;
664     }
665 
666     return_PTR (rp);
667 }
668 
669 /*
670  * Find a power consumer's control structure.
671  */
672 static struct acpi_powerconsumer *
673 acpi_pwr_find_consumer(ACPI_HANDLE consumer)
674 {
675     struct acpi_powerconsumer	*pc;
676 
677     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
678 
679     TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) {
680 	if (pc->ac_consumer == consumer)
681 	    break;
682     }
683 
684     return_PTR (pc);
685 }
686