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