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