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 <linux/sysfs.h>
45 #include <linux/acpi.h>
49 #define _COMPONENT ACPI_POWER_COMPONENT
50 ACPI_MODULE_NAME("power");
51 #define ACPI_POWER_CLASS "power_resource"
52 #define ACPI_POWER_DEVICE_NAME "Power Resource"
53 #define ACPI_POWER_FILE_INFO "info"
54 #define ACPI_POWER_FILE_STATUS "state"
55 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
56 #define ACPI_POWER_RESOURCE_STATE_ON 0x01
57 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
59 struct acpi_power_resource {
60 struct acpi_device device;
61 struct list_head list_node;
65 unsigned int ref_count;
67 struct mutex resource_lock;
70 struct acpi_power_resource_entry {
71 struct list_head node;
72 struct acpi_power_resource *resource;
75 static LIST_HEAD(acpi_power_resource_list);
76 static DEFINE_MUTEX(power_resource_list_lock);
78 /* --------------------------------------------------------------------------
79 Power Resource Management
80 -------------------------------------------------------------------------- */
83 struct acpi_power_resource *to_power_resource(struct acpi_device *device)
85 return container_of(device, struct acpi_power_resource, device);
88 static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
90 struct acpi_device *device;
92 if (acpi_bus_get_device(handle, &device))
95 return to_power_resource(device);
98 static int acpi_power_resources_list_add(acpi_handle handle,
99 struct list_head *list)
101 struct acpi_power_resource *resource = acpi_power_get_context(handle);
102 struct acpi_power_resource_entry *entry;
104 if (!resource || !list)
107 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
111 entry->resource = resource;
112 if (!list_empty(list)) {
113 struct acpi_power_resource_entry *e;
115 list_for_each_entry(e, list, node)
116 if (e->resource->order > resource->order) {
117 list_add_tail(&entry->node, &e->node);
121 list_add_tail(&entry->node, list);
125 void acpi_power_resources_list_free(struct list_head *list)
127 struct acpi_power_resource_entry *entry, *e;
129 list_for_each_entry_safe(entry, e, list, node) {
130 list_del(&entry->node);
135 static bool acpi_power_resource_is_dup(union acpi_object *package,
136 unsigned int start, unsigned int i)
138 acpi_handle rhandle, dup;
141 /* The caller is expected to check the package element types */
142 rhandle = package->package.elements[i].reference.handle;
143 for (j = start; j < i; j++) {
144 dup = package->package.elements[j].reference.handle;
152 int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
153 struct list_head *list)
158 for (i = start; i < package->package.count; i++) {
159 union acpi_object *element = &package->package.elements[i];
162 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
166 rhandle = element->reference.handle;
172 /* Some ACPI tables contain duplicate power resource references */
173 if (acpi_power_resource_is_dup(package, start, i))
176 err = acpi_add_power_resource(rhandle);
180 err = acpi_power_resources_list_add(rhandle, list);
185 acpi_power_resources_list_free(list);
190 static int acpi_power_get_state(acpi_handle handle, int *state)
192 acpi_status status = AE_OK;
193 unsigned long long sta = 0;
195 struct acpi_buffer buffer = { sizeof(node_name), node_name };
198 if (!handle || !state)
201 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
202 if (ACPI_FAILURE(status))
205 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
206 ACPI_POWER_RESOURCE_STATE_OFF;
208 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
210 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
212 *state ? "on" : "off"));
217 static int acpi_power_get_list_state(struct list_head *list, int *state)
219 struct acpi_power_resource_entry *entry;
225 /* The state of the list is 'on' IFF all resources are 'on'. */
227 list_for_each_entry(entry, list, node) {
228 struct acpi_power_resource *resource = entry->resource;
229 acpi_handle handle = resource->device.handle;
232 mutex_lock(&resource->resource_lock);
233 result = acpi_power_get_state(handle, &cur_state);
234 mutex_unlock(&resource->resource_lock);
238 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
242 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
243 cur_state ? "on" : "off"));
249 static int __acpi_power_on(struct acpi_power_resource *resource)
251 acpi_status status = AE_OK;
253 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
254 if (ACPI_FAILURE(status))
257 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
263 static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
267 if (resource->ref_count++) {
268 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
269 "Power resource [%s] already on\n",
272 result = __acpi_power_on(resource);
274 resource->ref_count--;
279 static int acpi_power_on(struct acpi_power_resource *resource)
283 mutex_lock(&resource->resource_lock);
284 result = acpi_power_on_unlocked(resource);
285 mutex_unlock(&resource->resource_lock);
289 static int __acpi_power_off(struct acpi_power_resource *resource)
293 status = acpi_evaluate_object(resource->device.handle, "_OFF",
295 if (ACPI_FAILURE(status))
298 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n",
303 static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
307 if (!resource->ref_count) {
308 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
309 "Power resource [%s] already off\n",
314 if (--resource->ref_count) {
315 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
316 "Power resource [%s] still in use\n",
319 result = __acpi_power_off(resource);
321 resource->ref_count++;
326 static int acpi_power_off(struct acpi_power_resource *resource)
330 mutex_lock(&resource->resource_lock);
331 result = acpi_power_off_unlocked(resource);
332 mutex_unlock(&resource->resource_lock);
336 static int acpi_power_off_list(struct list_head *list)
338 struct acpi_power_resource_entry *entry;
341 list_for_each_entry_reverse(entry, list, node) {
342 result = acpi_power_off(entry->resource);
349 list_for_each_entry_continue(entry, list, node)
350 acpi_power_on(entry->resource);
355 static int acpi_power_on_list(struct list_head *list)
357 struct acpi_power_resource_entry *entry;
360 list_for_each_entry(entry, list, node) {
361 result = acpi_power_on(entry->resource);
368 list_for_each_entry_continue_reverse(entry, list, node)
369 acpi_power_off(entry->resource);
374 static struct attribute *attrs[] = {
378 static struct attribute_group attr_groups[] = {
380 .name = "power_resources_D0",
384 .name = "power_resources_D1",
388 .name = "power_resources_D2",
391 [ACPI_STATE_D3_HOT] = {
392 .name = "power_resources_D3hot",
397 static struct attribute_group wakeup_attr_group = {
398 .name = "power_resources_wakeup",
402 static void acpi_power_hide_list(struct acpi_device *adev,
403 struct list_head *resources,
404 struct attribute_group *attr_group)
406 struct acpi_power_resource_entry *entry;
408 if (list_empty(resources))
411 list_for_each_entry_reverse(entry, resources, node) {
412 struct acpi_device *res_dev = &entry->resource->device;
414 sysfs_remove_link_from_group(&adev->dev.kobj,
416 dev_name(&res_dev->dev));
418 sysfs_remove_group(&adev->dev.kobj, attr_group);
421 static void acpi_power_expose_list(struct acpi_device *adev,
422 struct list_head *resources,
423 struct attribute_group *attr_group)
425 struct acpi_power_resource_entry *entry;
428 if (list_empty(resources))
431 ret = sysfs_create_group(&adev->dev.kobj, attr_group);
435 list_for_each_entry(entry, resources, node) {
436 struct acpi_device *res_dev = &entry->resource->device;
438 ret = sysfs_add_link_to_group(&adev->dev.kobj,
441 dev_name(&res_dev->dev));
443 acpi_power_hide_list(adev, resources, attr_group);
449 static void acpi_power_expose_hide(struct acpi_device *adev,
450 struct list_head *resources,
451 struct attribute_group *attr_group,
455 acpi_power_expose_list(adev, resources, attr_group);
457 acpi_power_hide_list(adev, resources, attr_group);
460 void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
464 if (adev->wakeup.flags.valid)
465 acpi_power_expose_hide(adev, &adev->wakeup.resources,
466 &wakeup_attr_group, add);
468 if (!adev->power.flags.power_resources)
471 for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
472 acpi_power_expose_hide(adev,
473 &adev->power.states[state].resources,
474 &attr_groups[state], add);
477 int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
479 struct acpi_power_resource_entry *entry;
480 int system_level = 5;
482 list_for_each_entry(entry, list, node) {
483 struct acpi_power_resource *resource = entry->resource;
484 acpi_handle handle = resource->device.handle;
488 mutex_lock(&resource->resource_lock);
490 result = acpi_power_get_state(handle, &state);
492 mutex_unlock(&resource->resource_lock);
495 if (state == ACPI_POWER_RESOURCE_STATE_ON) {
496 resource->ref_count++;
497 resource->wakeup_enabled = true;
499 if (system_level > resource->system_level)
500 system_level = resource->system_level;
502 mutex_unlock(&resource->resource_lock);
504 *system_level_p = system_level;
508 /* --------------------------------------------------------------------------
509 Device Power Management
510 -------------------------------------------------------------------------- */
513 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
514 * ACPI 3.0) _PSW (Power State Wake)
515 * @dev: Device to handle.
516 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
517 * @sleep_state: Target sleep state of the system.
518 * @dev_state: Target power state of the device.
520 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
521 * State Wake) for the device, if present. On failure reset the device's
522 * wakeup.flags.valid flag.
525 * 0 if either _DSW or _PSW has been successfully executed
526 * 0 if neither _DSW nor _PSW has been found
527 * -ENODEV if the execution of either _DSW or _PSW has failed
529 int acpi_device_sleep_wake(struct acpi_device *dev,
530 int enable, int sleep_state, int dev_state)
532 union acpi_object in_arg[3];
533 struct acpi_object_list arg_list = { 3, in_arg };
534 acpi_status status = AE_OK;
537 * Try to execute _DSW first.
539 * Three agruments are needed for the _DSW object:
540 * Argument 0: enable/disable the wake capabilities
541 * Argument 1: target system state
542 * Argument 2: target device state
543 * When _DSW object is called to disable the wake capabilities, maybe
544 * the first argument is filled. The values of the other two agruments
547 in_arg[0].type = ACPI_TYPE_INTEGER;
548 in_arg[0].integer.value = enable;
549 in_arg[1].type = ACPI_TYPE_INTEGER;
550 in_arg[1].integer.value = sleep_state;
551 in_arg[2].type = ACPI_TYPE_INTEGER;
552 in_arg[2].integer.value = dev_state;
553 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
554 if (ACPI_SUCCESS(status)) {
556 } else if (status != AE_NOT_FOUND) {
557 printk(KERN_ERR PREFIX "_DSW execution failed\n");
558 dev->wakeup.flags.valid = 0;
563 status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
564 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
565 printk(KERN_ERR PREFIX "_PSW execution failed\n");
566 dev->wakeup.flags.valid = 0;
574 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
575 * 1. Power on the power resources required for the wakeup device
576 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
577 * State Wake) for the device, if present
579 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
581 struct acpi_power_resource_entry *entry;
584 if (!dev || !dev->wakeup.flags.valid)
587 mutex_lock(&acpi_device_lock);
589 if (dev->wakeup.prepare_count++)
592 list_for_each_entry(entry, &dev->wakeup.resources, node) {
593 struct acpi_power_resource *resource = entry->resource;
595 mutex_lock(&resource->resource_lock);
597 if (!resource->wakeup_enabled) {
598 err = acpi_power_on_unlocked(resource);
600 resource->wakeup_enabled = true;
603 mutex_unlock(&resource->resource_lock);
607 "Cannot turn wakeup power resources on\n");
608 dev->wakeup.flags.valid = 0;
613 * Passing 3 as the third argument below means the device may be
614 * put into arbitrary power state afterward.
616 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
618 dev->wakeup.prepare_count = 0;
621 mutex_unlock(&acpi_device_lock);
626 * Shutdown a wakeup device, counterpart of above method
627 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
628 * State Wake) for the device, if present
629 * 2. Shutdown down the power resources
631 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
633 struct acpi_power_resource_entry *entry;
636 if (!dev || !dev->wakeup.flags.valid)
639 mutex_lock(&acpi_device_lock);
641 if (--dev->wakeup.prepare_count > 0)
645 * Executing the code below even if prepare_count is already zero when
646 * the function is called may be useful, for example for initialisation.
648 if (dev->wakeup.prepare_count < 0)
649 dev->wakeup.prepare_count = 0;
651 err = acpi_device_sleep_wake(dev, 0, 0, 0);
655 list_for_each_entry(entry, &dev->wakeup.resources, node) {
656 struct acpi_power_resource *resource = entry->resource;
658 mutex_lock(&resource->resource_lock);
660 if (resource->wakeup_enabled) {
661 err = acpi_power_off_unlocked(resource);
663 resource->wakeup_enabled = false;
666 mutex_unlock(&resource->resource_lock);
670 "Cannot turn wakeup power resources off\n");
671 dev->wakeup.flags.valid = 0;
677 mutex_unlock(&acpi_device_lock);
681 int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
687 if (!device || !state)
691 * We know a device's inferred power state when all the resources
692 * required for a given D-state are 'on'.
694 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
695 struct list_head *list = &device->power.states[i].resources;
697 if (list_empty(list))
700 result = acpi_power_get_list_state(list, &list_state);
704 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
710 *state = ACPI_STATE_D3_COLD;
714 int acpi_power_on_resources(struct acpi_device *device, int state)
716 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
719 return acpi_power_on_list(&device->power.states[state].resources);
722 int acpi_power_transition(struct acpi_device *device, int state)
726 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
729 if (device->power.state == state || !device->flags.power_manageable)
732 if ((device->power.state < ACPI_STATE_D0)
733 || (device->power.state > ACPI_STATE_D3_COLD))
736 /* TBD: Resources must be ordered. */
739 * First we reference all power resources required in the target list
740 * (e.g. so the device doesn't lose power while transitioning). Then,
741 * we dereference all power resources used in the current list.
743 if (state < ACPI_STATE_D3_COLD)
744 result = acpi_power_on_list(
745 &device->power.states[state].resources);
747 if (!result && device->power.state < ACPI_STATE_D3_COLD)
749 &device->power.states[device->power.state].resources);
751 /* We shouldn't change the state unless the above operations succeed. */
752 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
757 static void acpi_release_power_resource(struct device *dev)
759 struct acpi_device *device = to_acpi_device(dev);
760 struct acpi_power_resource *resource;
762 resource = container_of(device, struct acpi_power_resource, device);
764 mutex_lock(&power_resource_list_lock);
765 list_del(&resource->list_node);
766 mutex_unlock(&power_resource_list_lock);
768 acpi_free_pnp_ids(&device->pnp);
772 static ssize_t acpi_power_in_use_show(struct device *dev,
773 struct device_attribute *attr,
775 struct acpi_power_resource *resource;
777 resource = to_power_resource(to_acpi_device(dev));
778 return sprintf(buf, "%u\n", !!resource->ref_count);
780 static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
782 static void acpi_power_sysfs_remove(struct acpi_device *device)
784 device_remove_file(&device->dev, &dev_attr_resource_in_use);
787 int acpi_add_power_resource(acpi_handle handle)
789 struct acpi_power_resource *resource;
790 struct acpi_device *device = NULL;
791 union acpi_object acpi_object;
792 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
794 int state, result = -ENODEV;
796 acpi_bus_get_device(handle, &device);
800 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
804 device = &resource->device;
805 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
807 mutex_init(&resource->resource_lock);
808 INIT_LIST_HEAD(&resource->list_node);
809 resource->name = device->pnp.bus_id;
810 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
811 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
812 device->power.state = ACPI_STATE_UNKNOWN;
814 /* Evalute the object to get the system level and resource order. */
815 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
816 if (ACPI_FAILURE(status))
819 resource->system_level = acpi_object.power_resource.system_level;
820 resource->order = acpi_object.power_resource.resource_order;
822 result = acpi_power_get_state(handle, &state);
826 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
827 acpi_device_bid(device), state ? "on" : "off");
829 device->flags.match_driver = true;
830 result = acpi_device_add(device, acpi_release_power_resource);
834 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
835 device->remove = acpi_power_sysfs_remove;
837 mutex_lock(&power_resource_list_lock);
838 list_add(&resource->list_node, &acpi_power_resource_list);
839 mutex_unlock(&power_resource_list_lock);
840 acpi_device_add_finalize(device);
844 acpi_release_power_resource(&device->dev);
848 #ifdef CONFIG_ACPI_SLEEP
849 void acpi_resume_power_resources(void)
851 struct acpi_power_resource *resource;
853 mutex_lock(&power_resource_list_lock);
855 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
858 mutex_lock(&resource->resource_lock);
860 result = acpi_power_get_state(resource->device.handle, &state);
862 mutex_unlock(&resource->resource_lock);
866 if (state == ACPI_POWER_RESOURCE_STATE_OFF
867 && resource->ref_count) {
868 dev_info(&resource->device.dev, "Turning ON\n");
869 __acpi_power_on(resource);
870 } else if (state == ACPI_POWER_RESOURCE_STATE_ON
871 && !resource->ref_count) {
872 dev_info(&resource->device.dev, "Turning OFF\n");
873 __acpi_power_off(resource);
876 mutex_unlock(&resource->resource_lock);
879 mutex_unlock(&power_resource_list_lock);