2 * acpi_power.c - ACPI Bus Power Management ($Revision: 39 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 * ACPI power-managed devices may be controlled in two ways:
28 * 1. via "Device Specific (D-State) Control"
29 * 2. via "Power Resource Control".
30 * This module is used to manage devices relying on Power Resource Control.
32 * An ACPI "power resource object" describes a software controllable power
33 * plane, clock plane, or other resource used by a power managed device.
34 * A device may rely on multiple power resources, and a power resource
35 * may be shared by multiple devices.
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/types.h>
42 #include <linux/slab.h>
43 #include <linux/pm_runtime.h>
44 #include <acpi/acpi_bus.h>
45 #include <acpi/acpi_drivers.h>
49 #define PREFIX "ACPI: "
51 #define _COMPONENT ACPI_POWER_COMPONENT
52 ACPI_MODULE_NAME("power");
53 #define ACPI_POWER_CLASS "power_resource"
54 #define ACPI_POWER_DEVICE_NAME "Power Resource"
55 #define ACPI_POWER_FILE_INFO "info"
56 #define ACPI_POWER_FILE_STATUS "state"
57 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
58 #define ACPI_POWER_RESOURCE_STATE_ON 0x01
59 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
61 struct acpi_power_dependent_device
{
62 struct list_head node
;
63 struct acpi_device
*adev
;
64 struct work_struct work
;
67 struct acpi_power_resource
{
68 struct acpi_device device
;
69 struct list_head list_node
;
70 struct list_head dependent
;
74 unsigned int ref_count
;
75 struct mutex resource_lock
;
78 struct acpi_power_resource_entry
{
79 struct list_head node
;
80 struct acpi_power_resource
*resource
;
83 static LIST_HEAD(acpi_power_resource_list
);
84 static DEFINE_MUTEX(power_resource_list_lock
);
86 /* --------------------------------------------------------------------------
87 Power Resource Management
88 -------------------------------------------------------------------------- */
90 static struct acpi_power_resource
*acpi_power_get_context(acpi_handle handle
)
92 struct acpi_device
*device
;
94 if (acpi_bus_get_device(handle
, &device
))
97 return container_of(device
, struct acpi_power_resource
, device
);
100 static int acpi_power_resources_list_add(acpi_handle handle
,
101 struct list_head
*list
)
103 struct acpi_power_resource
*resource
= acpi_power_get_context(handle
);
104 struct acpi_power_resource_entry
*entry
;
106 if (!resource
|| !list
)
109 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
113 entry
->resource
= resource
;
114 if (!list_empty(list
)) {
115 struct acpi_power_resource_entry
*e
;
117 list_for_each_entry(e
, list
, node
)
118 if (e
->resource
->order
> resource
->order
) {
119 list_add_tail(&entry
->node
, &e
->node
);
123 list_add_tail(&entry
->node
, list
);
127 void acpi_power_resources_list_free(struct list_head
*list
)
129 struct acpi_power_resource_entry
*entry
, *e
;
131 list_for_each_entry_safe(entry
, e
, list
, node
) {
132 list_del(&entry
->node
);
137 int acpi_extract_power_resources(union acpi_object
*package
, unsigned int start
,
138 struct list_head
*list
)
143 for (i
= start
; i
< package
->package
.count
; i
++) {
144 union acpi_object
*element
= &package
->package
.elements
[i
];
147 if (element
->type
!= ACPI_TYPE_LOCAL_REFERENCE
) {
151 rhandle
= element
->reference
.handle
;
156 err
= acpi_add_power_resource(rhandle
);
160 err
= acpi_power_resources_list_add(rhandle
, list
);
165 acpi_power_resources_list_free(list
);
170 static int acpi_power_get_state(acpi_handle handle
, int *state
)
172 acpi_status status
= AE_OK
;
173 unsigned long long sta
= 0;
175 struct acpi_buffer buffer
= { sizeof(node_name
), node_name
};
178 if (!handle
|| !state
)
181 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
182 if (ACPI_FAILURE(status
))
185 *state
= (sta
& 0x01)?ACPI_POWER_RESOURCE_STATE_ON
:
186 ACPI_POWER_RESOURCE_STATE_OFF
;
188 acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
);
190 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Resource [%s] is %s\n",
192 *state
? "on" : "off"));
197 static int acpi_power_get_list_state(struct list_head
*list
, int *state
)
199 struct acpi_power_resource_entry
*entry
;
205 /* The state of the list is 'on' IFF all resources are 'on'. */
206 list_for_each_entry(entry
, list
, node
) {
207 struct acpi_power_resource
*resource
= entry
->resource
;
208 acpi_handle handle
= resource
->device
.handle
;
211 mutex_lock(&resource
->resource_lock
);
212 result
= acpi_power_get_state(handle
, &cur_state
);
213 mutex_unlock(&resource
->resource_lock
);
217 if (cur_state
!= ACPI_POWER_RESOURCE_STATE_ON
)
221 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Resource list is %s\n",
222 cur_state
? "on" : "off"));
228 static void acpi_power_resume_dependent(struct work_struct
*work
)
230 struct acpi_power_dependent_device
*dep
;
231 struct acpi_device_physical_node
*pn
;
232 struct acpi_device
*adev
;
235 dep
= container_of(work
, struct acpi_power_dependent_device
, work
);
237 if (acpi_power_get_inferred_state(adev
, &state
))
240 if (state
> ACPI_STATE_D0
)
243 mutex_lock(&adev
->physical_node_lock
);
245 list_for_each_entry(pn
, &adev
->physical_node_list
, node
)
246 pm_request_resume(pn
->dev
);
248 list_for_each_entry(pn
, &adev
->power_dependent
, node
)
249 pm_request_resume(pn
->dev
);
251 mutex_unlock(&adev
->physical_node_lock
);
254 static int __acpi_power_on(struct acpi_power_resource
*resource
)
256 acpi_status status
= AE_OK
;
258 status
= acpi_evaluate_object(resource
->device
.handle
, "_ON", NULL
, NULL
);
259 if (ACPI_FAILURE(status
))
262 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Power resource [%s] turned on\n",
268 static int acpi_power_on(struct acpi_power_resource
*resource
)
272 mutex_lock(&resource
->resource_lock
);
274 if (resource
->ref_count
++) {
275 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
276 "Power resource [%s] already on",
279 result
= __acpi_power_on(resource
);
281 resource
->ref_count
--;
283 struct acpi_power_dependent_device
*dep
;
285 list_for_each_entry(dep
, &resource
->dependent
, node
)
286 schedule_work(&dep
->work
);
290 mutex_unlock(&resource
->resource_lock
);
295 static int acpi_power_off(struct acpi_power_resource
*resource
)
297 acpi_status status
= AE_OK
;
300 mutex_lock(&resource
->resource_lock
);
302 if (!resource
->ref_count
) {
303 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
304 "Power resource [%s] already off",
309 if (--resource
->ref_count
) {
310 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
311 "Power resource [%s] still in use\n",
316 status
= acpi_evaluate_object(resource
->device
.handle
, "_OFF", NULL
, NULL
);
317 if (ACPI_FAILURE(status
))
320 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
321 "Power resource [%s] turned off\n",
325 mutex_unlock(&resource
->resource_lock
);
330 static int acpi_power_off_list(struct list_head
*list
)
332 struct acpi_power_resource_entry
*entry
;
335 list_for_each_entry_reverse(entry
, list
, node
) {
336 result
= acpi_power_off(entry
->resource
);
343 list_for_each_entry_continue(entry
, list
, node
)
344 acpi_power_on(entry
->resource
);
349 static int acpi_power_on_list(struct list_head
*list
)
351 struct acpi_power_resource_entry
*entry
;
354 list_for_each_entry(entry
, list
, node
) {
355 result
= acpi_power_on(entry
->resource
);
362 list_for_each_entry_continue_reverse(entry
, list
, node
)
363 acpi_power_off(entry
->resource
);
368 static void acpi_power_add_dependent(struct acpi_power_resource
*resource
,
369 struct acpi_device
*adev
)
371 struct acpi_power_dependent_device
*dep
;
373 mutex_lock(&resource
->resource_lock
);
375 list_for_each_entry(dep
, &resource
->dependent
, node
)
376 if (dep
->adev
== adev
)
379 dep
= kzalloc(sizeof(*dep
), GFP_KERNEL
);
384 INIT_WORK(&dep
->work
, acpi_power_resume_dependent
);
385 list_add_tail(&dep
->node
, &resource
->dependent
);
388 mutex_unlock(&resource
->resource_lock
);
391 static void acpi_power_remove_dependent(struct acpi_power_resource
*resource
,
392 struct acpi_device
*adev
)
394 struct acpi_power_dependent_device
*dep
;
395 struct work_struct
*work
= NULL
;
397 mutex_lock(&resource
->resource_lock
);
399 list_for_each_entry(dep
, &resource
->dependent
, node
)
400 if (dep
->adev
== adev
) {
401 list_del(&dep
->node
);
406 mutex_unlock(&resource
->resource_lock
);
409 cancel_work_sync(work
);
414 void acpi_power_add_remove_device(struct acpi_device
*adev
, bool add
)
416 if (adev
->power
.flags
.power_resources
) {
417 struct acpi_device_power_state
*ps
;
418 struct acpi_power_resource_entry
*entry
;
420 ps
= &adev
->power
.states
[ACPI_STATE_D0
];
421 list_for_each_entry(entry
, &ps
->resources
, node
) {
422 struct acpi_power_resource
*resource
= entry
->resource
;
425 acpi_power_add_dependent(resource
, adev
);
427 acpi_power_remove_dependent(resource
, adev
);
432 int acpi_power_min_system_level(struct list_head
*list
)
434 struct acpi_power_resource_entry
*entry
;
435 int system_level
= 5;
437 list_for_each_entry(entry
, list
, node
) {
438 struct acpi_power_resource
*resource
= entry
->resource
;
440 if (system_level
> resource
->system_level
)
441 system_level
= resource
->system_level
;
446 /* --------------------------------------------------------------------------
447 Device Power Management
448 -------------------------------------------------------------------------- */
451 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
452 * ACPI 3.0) _PSW (Power State Wake)
453 * @dev: Device to handle.
454 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
455 * @sleep_state: Target sleep state of the system.
456 * @dev_state: Target power state of the device.
458 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
459 * State Wake) for the device, if present. On failure reset the device's
460 * wakeup.flags.valid flag.
463 * 0 if either _DSW or _PSW has been successfully executed
464 * 0 if neither _DSW nor _PSW has been found
465 * -ENODEV if the execution of either _DSW or _PSW has failed
467 int acpi_device_sleep_wake(struct acpi_device
*dev
,
468 int enable
, int sleep_state
, int dev_state
)
470 union acpi_object in_arg
[3];
471 struct acpi_object_list arg_list
= { 3, in_arg
};
472 acpi_status status
= AE_OK
;
475 * Try to execute _DSW first.
477 * Three agruments are needed for the _DSW object:
478 * Argument 0: enable/disable the wake capabilities
479 * Argument 1: target system state
480 * Argument 2: target device state
481 * When _DSW object is called to disable the wake capabilities, maybe
482 * the first argument is filled. The values of the other two agruments
485 in_arg
[0].type
= ACPI_TYPE_INTEGER
;
486 in_arg
[0].integer
.value
= enable
;
487 in_arg
[1].type
= ACPI_TYPE_INTEGER
;
488 in_arg
[1].integer
.value
= sleep_state
;
489 in_arg
[2].type
= ACPI_TYPE_INTEGER
;
490 in_arg
[2].integer
.value
= dev_state
;
491 status
= acpi_evaluate_object(dev
->handle
, "_DSW", &arg_list
, NULL
);
492 if (ACPI_SUCCESS(status
)) {
494 } else if (status
!= AE_NOT_FOUND
) {
495 printk(KERN_ERR PREFIX
"_DSW execution failed\n");
496 dev
->wakeup
.flags
.valid
= 0;
502 in_arg
[0].integer
.value
= enable
;
503 status
= acpi_evaluate_object(dev
->handle
, "_PSW", &arg_list
, NULL
);
504 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
505 printk(KERN_ERR PREFIX
"_PSW execution failed\n");
506 dev
->wakeup
.flags
.valid
= 0;
514 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
515 * 1. Power on the power resources required for the wakeup device
516 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
517 * State Wake) for the device, if present
519 int acpi_enable_wakeup_device_power(struct acpi_device
*dev
, int sleep_state
)
523 if (!dev
|| !dev
->wakeup
.flags
.valid
)
526 mutex_lock(&acpi_device_lock
);
528 if (dev
->wakeup
.prepare_count
++)
531 err
= acpi_power_on_list(&dev
->wakeup
.resources
);
533 dev_err(&dev
->dev
, "Cannot turn wakeup power resources on\n");
534 dev
->wakeup
.flags
.valid
= 0;
537 * Passing 3 as the third argument below means the device may be
538 * put into arbitrary power state afterward.
540 err
= acpi_device_sleep_wake(dev
, 1, sleep_state
, 3);
543 dev
->wakeup
.prepare_count
= 0;
546 mutex_unlock(&acpi_device_lock
);
551 * Shutdown a wakeup device, counterpart of above method
552 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
553 * State Wake) for the device, if present
554 * 2. Shutdown down the power resources
556 int acpi_disable_wakeup_device_power(struct acpi_device
*dev
)
560 if (!dev
|| !dev
->wakeup
.flags
.valid
)
563 mutex_lock(&acpi_device_lock
);
565 if (--dev
->wakeup
.prepare_count
> 0)
569 * Executing the code below even if prepare_count is already zero when
570 * the function is called may be useful, for example for initialisation.
572 if (dev
->wakeup
.prepare_count
< 0)
573 dev
->wakeup
.prepare_count
= 0;
575 err
= acpi_device_sleep_wake(dev
, 0, 0, 0);
579 err
= acpi_power_off_list(&dev
->wakeup
.resources
);
581 dev_err(&dev
->dev
, "Cannot turn wakeup power resources off\n");
582 dev
->wakeup
.flags
.valid
= 0;
586 mutex_unlock(&acpi_device_lock
);
590 int acpi_power_get_inferred_state(struct acpi_device
*device
, int *state
)
596 if (!device
|| !state
)
600 * We know a device's inferred power state when all the resources
601 * required for a given D-state are 'on'.
603 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++) {
604 struct list_head
*list
= &device
->power
.states
[i
].resources
;
606 if (list_empty(list
))
609 result
= acpi_power_get_list_state(list
, &list_state
);
613 if (list_state
== ACPI_POWER_RESOURCE_STATE_ON
) {
619 *state
= ACPI_STATE_D3
;
623 int acpi_power_on_resources(struct acpi_device
*device
, int state
)
625 if (!device
|| state
< ACPI_STATE_D0
|| state
> ACPI_STATE_D3_COLD
)
628 if (state
== ACPI_STATE_D3_COLD
)
631 return acpi_power_on_list(&device
->power
.states
[state
].resources
);
634 int acpi_power_transition(struct acpi_device
*device
, int state
)
638 if (!device
|| (state
< ACPI_STATE_D0
) || (state
> ACPI_STATE_D3_COLD
))
641 if (device
->power
.state
== state
|| !device
->flags
.power_manageable
)
644 if ((device
->power
.state
< ACPI_STATE_D0
)
645 || (device
->power
.state
> ACPI_STATE_D3_COLD
))
648 /* TBD: Resources must be ordered. */
651 * First we reference all power resources required in the target list
652 * (e.g. so the device doesn't lose power while transitioning). Then,
653 * we dereference all power resources used in the current list.
655 if (state
< ACPI_STATE_D3_COLD
)
656 result
= acpi_power_on_list(
657 &device
->power
.states
[state
].resources
);
659 if (!result
&& device
->power
.state
< ACPI_STATE_D3_COLD
)
661 &device
->power
.states
[device
->power
.state
].resources
);
663 /* We shouldn't change the state unless the above operations succeed. */
664 device
->power
.state
= result
? ACPI_STATE_UNKNOWN
: state
;
669 static void acpi_release_power_resource(struct device
*dev
)
671 struct acpi_device
*device
= to_acpi_device(dev
);
672 struct acpi_power_resource
*resource
;
674 resource
= container_of(device
, struct acpi_power_resource
, device
);
676 mutex_lock(&power_resource_list_lock
);
677 list_del(&resource
->list_node
);
678 mutex_unlock(&power_resource_list_lock
);
680 acpi_free_ids(device
);
684 int acpi_add_power_resource(acpi_handle handle
)
686 struct acpi_power_resource
*resource
;
687 struct acpi_device
*device
= NULL
;
688 union acpi_object acpi_object
;
689 struct acpi_buffer buffer
= { sizeof(acpi_object
), &acpi_object
};
691 int state
, result
= -ENODEV
;
693 acpi_bus_get_device(handle
, &device
);
697 resource
= kzalloc(sizeof(*resource
), GFP_KERNEL
);
701 device
= &resource
->device
;
702 acpi_init_device_object(device
, handle
, ACPI_BUS_TYPE_POWER
,
704 mutex_init(&resource
->resource_lock
);
705 INIT_LIST_HEAD(&resource
->dependent
);
706 resource
->name
= device
->pnp
.bus_id
;
707 strcpy(acpi_device_name(device
), ACPI_POWER_DEVICE_NAME
);
708 strcpy(acpi_device_class(device
), ACPI_POWER_CLASS
);
709 device
->power
.state
= ACPI_STATE_UNKNOWN
;
711 /* Evalute the object to get the system level and resource order. */
712 status
= acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
);
713 if (ACPI_FAILURE(status
))
716 resource
->system_level
= acpi_object
.power_resource
.system_level
;
717 resource
->order
= acpi_object
.power_resource
.resource_order
;
719 result
= acpi_power_get_state(handle
, &state
);
723 printk(KERN_INFO PREFIX
"%s [%s] (%s)\n", acpi_device_name(device
),
724 acpi_device_bid(device
), state
? "on" : "off");
726 device
->flags
.match_driver
= true;
727 result
= acpi_device_register(device
, acpi_release_power_resource
);
731 mutex_lock(&power_resource_list_lock
);
732 list_add(&resource
->list_node
, &acpi_power_resource_list
);
733 mutex_unlock(&power_resource_list_lock
);
737 acpi_release_power_resource(&device
->dev
);
741 #ifdef CONFIG_ACPI_SLEEP
742 void acpi_resume_power_resources(void)
744 struct acpi_power_resource
*resource
;
746 mutex_lock(&power_resource_list_lock
);
748 list_for_each_entry(resource
, &acpi_power_resource_list
, list_node
) {
751 mutex_lock(&resource
->resource_lock
);
753 result
= acpi_power_get_state(resource
->device
.handle
, &state
);
754 if (!result
&& state
== ACPI_POWER_RESOURCE_STATE_OFF
755 && resource
->ref_count
) {
756 dev_info(&resource
->device
.dev
, "Turning ON\n");
757 __acpi_power_on(resource
);
760 mutex_unlock(&resource
->resource_lock
);
763 mutex_unlock(&power_resource_list_lock
);