Deleted Added
sdiff udiff text old ( 89054 ) new ( 91125 )
full compact
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 unchanged lines hidden (view full) ---

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 * $FreeBSD: head/sys/dev/acpica/acpi_powerres.c 89054 2002-01-08 06:46:01Z msmith $
27 */
28
29#include "opt_acpi.h" /* XXX trim includes */
30#include <sys/param.h>
31#include <sys/kernel.h>
32#include <sys/proc.h>
33#include <sys/lock.h>
34#include <sys/malloc.h>

--- 35 unchanged lines hidden (view full) ---

70 */
71
72MALLOC_DEFINE(M_ACPIPWR, "acpipwr", "ACPI power resources");
73
74/*
75 * Hooks for the ACPI CA debugging infrastructure
76 */
77#define _COMPONENT ACPI_POWER
78MODULE_NAME("POWERRES")
79
80/* return values from _STA on a power resource */
81#define ACPI_PWR_OFF 0
82#define ACPI_PWR_ON 1
83
84/*
85 * A relationship between a power resource and a consumer.
86 */

--- 56 unchanged lines hidden (view full) ---

143static ACPI_STATUS
144acpi_pwr_register_resource(ACPI_HANDLE res)
145{
146 ACPI_STATUS status;
147 ACPI_BUFFER buf;
148 ACPI_OBJECT *obj;
149 struct acpi_powerresource *rp, *srp;
150
151 FUNCTION_TRACE(__func__);
152
153 rp = NULL;
154 obj = NULL;
155
156 /* look to see if we know about this resource */
157 if (acpi_pwr_find_resource(res) != NULL)
158 return_ACPI_STATUS(AE_OK); /* already know about it */
159
160 /* allocate a new resource */
161 if ((rp = malloc(sizeof(*rp), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) {
162 status = AE_NO_MEMORY;
163 goto out;
164 }
165 TAILQ_INIT(&rp->ap_references);
166 rp->ap_resource = res;
167
168 /* get the Power Resource object */
169 bzero(&buf, sizeof(buf));
170 if ((status = acpi_EvaluateIntoBuffer(res, NULL, NULL, &buf)) != AE_OK) {
171 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n"));
172 goto out;
173 }
174 obj = buf.Pointer;
175 if (obj->Type != ACPI_TYPE_POWER) {
176 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "questionable power resource object %s\n", acpi_name(res)));
177 status = AE_TYPE;
178 goto out;

--- 13 unchanged lines hidden (view full) ---

192 TAILQ_INSERT_BEFORE(srp, rp, ap_link);
193 goto done;
194 }
195 TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link);
196
197 done:
198 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power resource %s\n", acpi_name(res)));
199 out:
200 if (obj != NULL)
201 AcpiOsFree(obj);
202 if ((status != AE_OK) && (rp != NULL))
203 free(rp, M_ACPIPWR);
204 return_ACPI_STATUS(status);
205}
206
207/*
208 * Deregister a power resource.
209 */
210static ACPI_STATUS
211acpi_pwr_deregister_resource(ACPI_HANDLE res)
212{
213 struct acpi_powerresource *rp;
214
215 FUNCTION_TRACE(__func__);
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 */

--- 14 unchanged lines hidden (view full) ---

238 *
239 * It's OK to call this if we already know about the consumer.
240 */
241static ACPI_STATUS
242acpi_pwr_register_consumer(ACPI_HANDLE consumer)
243{
244 struct acpi_powerconsumer *pc;
245
246 FUNCTION_TRACE(__func__);
247
248 /* check to see whether we know about this consumer already */
249 if ((pc = 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);

--- 14 unchanged lines hidden (view full) ---

269 * This should only be done once the consumer has been powered off.
270 * (XXX is this correct? Check once implemented)
271 */
272static ACPI_STATUS
273acpi_pwr_deregister_consumer(ACPI_HANDLE consumer)
274{
275 struct acpi_powerconsumer *pc;
276
277 FUNCTION_TRACE(__func__);
278
279 /* find the consumer */
280 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL)
281 return_ACPI_STATUS(AE_BAD_PARAMETER);
282
283 /* make sure the consumer's not referencing anything right now */
284 if (TAILQ_FIRST(&pc->ac_references) != NULL)
285 return_ACPI_STATUS(AE_BAD_PARAMETER);

--- 16 unchanged lines hidden (view full) ---

302 struct acpi_powerreference *pr;
303 ACPI_HANDLE method_handle, reslist_handle, pr0_handle;
304 ACPI_BUFFER reslist_buffer;
305 ACPI_OBJECT *reslist_object;
306 ACPI_STATUS status;
307 char *method_name, *reslist_name;
308 int res_changed;
309
310 FUNCTION_TRACE(__func__);
311
312 /* find the consumer */
313 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
314 if ((status = acpi_pwr_register_consumer(consumer)) != AE_OK)
315 return_ACPI_STATUS(status);
316 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
317 return_ACPI_STATUS(AE_ERROR); /* something very wrong */
318 }
319 }
320
321 /* check for valid transitions */
322 if ((pc->ac_state == ACPI_STATE_D3) && (state != ACPI_STATE_D0))

--- 28 unchanged lines hidden (view full) ---

351 * reslist must be present. We need to do this before we go
352 * dereferencing resources (since we might be trying to go to
353 * a state we don't support).
354 *
355 * Note that if any states are supported, the device has to
356 * support D0 and D3. It's never an error to try to go to
357 * D0.
358 */
359 reslist_object = NULL;
360 if (AcpiGetHandle(consumer, method_name, &method_handle) != AE_OK)
361 method_handle = NULL;
362 if (AcpiGetHandle(consumer, reslist_name, &reslist_handle) != AE_OK)
363 reslist_handle = NULL;
364 if ((reslist_handle == NULL) && (method_handle == NULL)) {
365 if (state == ACPI_STATE_D0) {
366 pc->ac_state = ACPI_STATE_D0;
367 return_ACPI_STATUS(AE_OK);
368 }
369 if (state != ACPI_STATE_D3) {
370 goto bad;
371 }
372
373 /* turn off the resources listed in _PR0 to go to D3. */
374 if (AcpiGetHandle(consumer, "_PR0", &pr0_handle) != AE_OK) {
375 goto bad;
376 }
377 bzero(&reslist_buffer, sizeof(reslist_buffer));
378 status = acpi_EvaluateIntoBuffer(pr0_handle, NULL, NULL, &reslist_buffer);
379 if (status != AE_OK) {
380 goto bad;
381 }
382 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
383 if ((reslist_object->Type != ACPI_TYPE_PACKAGE) ||
384 (reslist_object->Package.Count == 0)) {
385 goto bad;
386 }
387 AcpiOsFree(reslist_object);
388 }
389
390 /*
391 * Check that we can actually fetch the list of power resources
392 */
393 if (reslist_handle != NULL) {
394 bzero(&reslist_buffer, sizeof(reslist_buffer));
395 if ((status = acpi_EvaluateIntoBuffer(reslist_handle, NULL, NULL, &reslist_buffer)) != AE_OK) {
396 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't evaluate resource list %s\n",
397 acpi_name(reslist_handle)));
398 return_ACPI_STATUS(status);
399 }
400 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
401 if (reslist_object->Type != ACPI_TYPE_PACKAGE) {
402 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "resource list is not ACPI_TYPE_PACKAGE (%d)\n",
403 reslist_object->Type));
404 return_ACPI_STATUS(AE_TYPE);
405 }
406 } else {
407 reslist_object = NULL;
408 }
409
410 /*
411 * Now we are ready to switch, so kill off any current power resource references.
412 */
413 res_changed = 0;
414 while((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) {
415 res_changed = 1;

--- 14 unchanged lines hidden (view full) ---

430 acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource, pc);
431 res_changed = 1;
432 }
433
434 /*
435 * If we changed anything in the resource list, we need to run a switch
436 * pass now.
437 */
438 if ((status = acpi_pwr_switch_power()) != AE_OK) {
439 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to correctly switch resources to move %s to D%d\n",
440 acpi_name(consumer), state));
441 return_ACPI_STATUS(status); /* XXX is this appropriate? Should we return to previous state? */
442 }
443
444 /* invoke power state switch method (if present) */
445 if (method_handle != NULL) {
446 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "invoking state transition method %s\n",
447 acpi_name(method_handle)));
448 if ((status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL)) != AE_OK)
449 pc->ac_state = ACPI_STATE_UNKNOWN;
450 return_ACPI_STATUS(status); /* XXX is this appropriate? Should we return to previous state? */
451 }
452
453 /* transition was successful */
454 pc->ac_state = state;
455 return_ACPI_STATUS(AE_OK);
456
457 bad:
458 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "attempt to set unsupported state D%d\n",
459 state));
460 if (reslist_object)
461 AcpiOsFree(reslist_object);
462 return_ACPI_STATUS(AE_BAD_PARAMETER);
463}
464
465/*
466 * Called to create a reference between a power consumer and a power resource
467 * identified in the object.
468 */
469static void
470acpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg)
471{
472 struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg;
473 struct acpi_powerreference *pr;
474 struct acpi_powerresource *rp;
475 ACPI_HANDLE res;
476 ACPI_STATUS status;
477
478 FUNCTION_TRACE(__func__);
479
480 /* check the object type */
481 if (obj->Type != ACPI_TYPE_STRING) {
482 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "don't know how to create a power reference to object type %d\n",
483 obj->Type));
484 return_VOID;
485 }
486

--- 41 unchanged lines hidden (view full) ---

528 */
529static ACPI_STATUS
530acpi_pwr_switch_power(void)
531{
532 struct acpi_powerresource *rp;
533 ACPI_STATUS status;
534 int cur;
535
536 FUNCTION_TRACE(__func__);
537
538 /*
539 * Sweep the list forwards turning things on.
540 */
541 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
542 if (TAILQ_FIRST(&rp->ap_references) == NULL) {
543 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s has no references, not turning on\n",
544 acpi_name(rp->ap_resource)));
545 continue;
546 }
547
548 /* we could cache this if we trusted it not to change under us */
549 if ((status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur)) != AE_OK) {
550 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
551 acpi_name(rp->ap_resource), status));
552 continue; /* XXX is this correct? Always switch if in doubt? */
553 }
554
555 /*
556 * Switch if required. Note that we ignore the result of the switch
557 * effort; we don't know what to do if it fails, so checking wouldn't

--- 17 unchanged lines hidden (view full) ---

575 TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list, ap_link) {
576 if (TAILQ_FIRST(&rp->ap_references) != NULL) {
577 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s has references, not turning off\n",
578 acpi_name(rp->ap_resource)));
579 continue;
580 }
581
582 /* we could cache this if we trusted it not to change under us */
583 if ((status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur)) != AE_OK) {
584 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
585 acpi_name(rp->ap_resource), status));
586 continue; /* XXX is this correct? Always switch if in doubt? */
587 }
588
589 /*
590 * Switch if required. Note that we ignore the result of the switch
591 * effort; we don't know what to do if it fails, so checking wouldn't

--- 16 unchanged lines hidden (view full) ---

608/*
609 * Find a power resource's control structure.
610 */
611static struct acpi_powerresource *
612acpi_pwr_find_resource(ACPI_HANDLE res)
613{
614 struct acpi_powerresource *rp;
615
616 FUNCTION_TRACE(__func__);
617
618 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link)
619 if (rp->ap_resource == res)
620 break;
621 return_PTR(rp);
622}
623
624/*
625 * Find a power consumer's control structure.
626 */
627static struct acpi_powerconsumer *
628acpi_pwr_find_consumer(ACPI_HANDLE consumer)
629{
630 struct acpi_powerconsumer *pc;
631
632 FUNCTION_TRACE(__func__);
633
634 TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link)
635 if (pc->ac_consumer == consumer)
636 break;
637 return_PTR(pc);
638}
639