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