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