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 <acpi/acpi_bus.h>
46 #include <acpi/acpi_drivers.h>
50 #define PREFIX "ACPI: "
52 #define _COMPONENT ACPI_POWER_COMPONENT
53 ACPI_MODULE_NAME("power");
54 #define ACPI_POWER_CLASS "power_resource"
55 #define ACPI_POWER_DEVICE_NAME "Power Resource"
56 #define ACPI_POWER_FILE_INFO "info"
57 #define ACPI_POWER_FILE_STATUS "state"
58 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
59 #define ACPI_POWER_RESOURCE_STATE_ON 0x01
60 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
62 struct acpi_power_dependent_device {
63 struct list_head node;
64 struct acpi_device *adev;
65 struct work_struct work;
68 struct acpi_power_resource {
69 struct acpi_device device;
70 struct list_head list_node;
71 struct list_head dependent;
75 unsigned int ref_count;
76 struct mutex resource_lock;
79 struct acpi_power_resource_entry {
80 struct list_head node;
81 struct acpi_power_resource *resource;
84 static LIST_HEAD(acpi_power_resource_list);
85 static DEFINE_MUTEX(power_resource_list_lock);
87 /* --------------------------------------------------------------------------
88 Power Resource Management
89 -------------------------------------------------------------------------- */
92 struct acpi_power_resource *to_power_resource(struct acpi_device *device)
94 return container_of(device, struct acpi_power_resource, device);
97 static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
99 struct acpi_device *device;
101 if (acpi_bus_get_device(handle, &device))
104 return to_power_resource(device);
107 static int acpi_power_resources_list_add(acpi_handle handle,
108 struct list_head *list)
110 struct acpi_power_resource *resource = acpi_power_get_context(handle);
111 struct acpi_power_resource_entry *entry;
113 if (!resource || !list)
116 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
120 entry->resource = resource;
121 if (!list_empty(list)) {
122 struct acpi_power_resource_entry *e;
124 list_for_each_entry(e, list, node)
125 if (e->resource->order > resource->order) {
126 list_add_tail(&entry->node, &e->node);
130 list_add_tail(&entry->node, list);
134 void acpi_power_resources_list_free(struct list_head *list)
136 struct acpi_power_resource_entry *entry, *e;
138 list_for_each_entry_safe(entry, e, list, node) {
139 list_del(&entry->node);
144 int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
145 struct list_head *list)
150 for (i = start; i < package->package.count; i++) {
151 union acpi_object *element = &package->package.elements[i];
154 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
158 rhandle = element->reference.handle;
163 err = acpi_add_power_resource(rhandle);
167 err = acpi_power_resources_list_add(rhandle, list);
172 acpi_power_resources_list_free(list);
177 static int acpi_power_get_state(acpi_handle handle, int *state)
179 acpi_status status = AE_OK;
180 unsigned long long sta = 0;
182 struct acpi_buffer buffer = { sizeof(node_name), node_name };
185 if (!handle || !state)
188 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
189 if (ACPI_FAILURE(status))
192 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
193 ACPI_POWER_RESOURCE_STATE_OFF;
195 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
197 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
199 *state ? "on" : "off"));
204 static int acpi_power_get_list_state(struct list_head *list, int *state)
206 struct acpi_power_resource_entry *entry;
212 /* The state of the list is 'on' IFF all resources are 'on'. */
213 list_for_each_entry(entry, list, node) {
214 struct acpi_power_resource *resource = entry->resource;
215 acpi_handle handle = resource->device.handle;
218 mutex_lock(&resource->resource_lock);
219 result = acpi_power_get_state(handle, &cur_state);
220 mutex_unlock(&resource->resource_lock);
224 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
228 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
229 cur_state ? "on" : "off"));
235 static void acpi_power_resume_dependent(struct work_struct *work)
237 struct acpi_power_dependent_device *dep;
238 struct acpi_device_physical_node *pn;
239 struct acpi_device *adev;
242 dep = container_of(work, struct acpi_power_dependent_device, work);
244 if (acpi_power_get_inferred_state(adev, &state))
247 if (state > ACPI_STATE_D0)
250 mutex_lock(&adev->physical_node_lock);
252 list_for_each_entry(pn, &adev->physical_node_list, node)
253 pm_request_resume(pn->dev);
255 list_for_each_entry(pn, &adev->power_dependent, node)
256 pm_request_resume(pn->dev);
258 mutex_unlock(&adev->physical_node_lock);
261 static int __acpi_power_on(struct acpi_power_resource *resource)
263 acpi_status status = AE_OK;
265 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
266 if (ACPI_FAILURE(status))
269 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
275 static int acpi_power_on(struct acpi_power_resource *resource)
279 mutex_lock(&resource->resource_lock);
281 if (resource->ref_count++) {
282 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
283 "Power resource [%s] already on",
286 result = __acpi_power_on(resource);
288 resource->ref_count--;
290 struct acpi_power_dependent_device *dep;
292 list_for_each_entry(dep, &resource->dependent, node)
293 schedule_work(&dep->work);
297 mutex_unlock(&resource->resource_lock);
302 static int __acpi_power_off(struct acpi_power_resource *resource)
306 status = acpi_evaluate_object(resource->device.handle, "_OFF",
308 if (ACPI_FAILURE(status))
311 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n",
316 static int acpi_power_off(struct acpi_power_resource *resource)
320 mutex_lock(&resource->resource_lock);
322 if (!resource->ref_count) {
323 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
324 "Power resource [%s] already off",
329 if (--resource->ref_count) {
330 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
331 "Power resource [%s] still in use\n",
334 result = __acpi_power_off(resource);
336 resource->ref_count++;
340 mutex_unlock(&resource->resource_lock);
345 static int acpi_power_off_list(struct list_head *list)
347 struct acpi_power_resource_entry *entry;
350 list_for_each_entry_reverse(entry, list, node) {
351 result = acpi_power_off(entry->resource);
358 list_for_each_entry_continue(entry, list, node)
359 acpi_power_on(entry->resource);
364 static int acpi_power_on_list(struct list_head *list)
366 struct acpi_power_resource_entry *entry;
369 list_for_each_entry(entry, list, node) {
370 result = acpi_power_on(entry->resource);
377 list_for_each_entry_continue_reverse(entry, list, node)
378 acpi_power_off(entry->resource);
383 static void acpi_power_add_dependent(struct acpi_power_resource *resource,
384 struct acpi_device *adev)
386 struct acpi_power_dependent_device *dep;
388 mutex_lock(&resource->resource_lock);
390 list_for_each_entry(dep, &resource->dependent, node)
391 if (dep->adev == adev)
394 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
399 INIT_WORK(&dep->work, acpi_power_resume_dependent);
400 list_add_tail(&dep->node, &resource->dependent);
403 mutex_unlock(&resource->resource_lock);
406 static void acpi_power_remove_dependent(struct acpi_power_resource *resource,
407 struct acpi_device *adev)
409 struct acpi_power_dependent_device *dep;
410 struct work_struct *work = NULL;
412 mutex_lock(&resource->resource_lock);
414 list_for_each_entry(dep, &resource->dependent, node)
415 if (dep->adev == adev) {
416 list_del(&dep->node);
421 mutex_unlock(&resource->resource_lock);
424 cancel_work_sync(work);
429 static struct attribute *attrs[] = {
433 static struct attribute_group attr_groups[] = {
435 .name = "power_resources_D0",
439 .name = "power_resources_D1",
443 .name = "power_resources_D2",
446 [ACPI_STATE_D3_HOT] = {
447 .name = "power_resources_D3hot",
452 static void acpi_power_hide_list(struct acpi_device *adev, int state)
454 struct acpi_device_power_state *ps = &adev->power.states[state];
455 struct acpi_power_resource_entry *entry;
457 if (list_empty(&ps->resources))
460 list_for_each_entry_reverse(entry, &ps->resources, node) {
461 struct acpi_device *res_dev = &entry->resource->device;
463 sysfs_remove_link_from_group(&adev->dev.kobj,
464 attr_groups[state].name,
465 dev_name(&res_dev->dev));
467 sysfs_remove_group(&adev->dev.kobj, &attr_groups[state]);
470 static void acpi_power_expose_list(struct acpi_device *adev, int state)
472 struct acpi_device_power_state *ps = &adev->power.states[state];
473 struct acpi_power_resource_entry *entry;
476 if (list_empty(&ps->resources))
479 ret = sysfs_create_group(&adev->dev.kobj, &attr_groups[state]);
483 list_for_each_entry(entry, &ps->resources, node) {
484 struct acpi_device *res_dev = &entry->resource->device;
486 ret = sysfs_add_link_to_group(&adev->dev.kobj,
487 attr_groups[state].name,
489 dev_name(&res_dev->dev));
491 acpi_power_hide_list(adev, state);
497 void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
499 struct acpi_device_power_state *ps;
500 struct acpi_power_resource_entry *entry;
503 if (!adev->power.flags.power_resources)
506 ps = &adev->power.states[ACPI_STATE_D0];
507 list_for_each_entry(entry, &ps->resources, node) {
508 struct acpi_power_resource *resource = entry->resource;
511 acpi_power_add_dependent(resource, adev);
513 acpi_power_remove_dependent(resource, adev);
516 for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++) {
518 acpi_power_expose_list(adev, state);
520 acpi_power_hide_list(adev, state);
524 int acpi_power_min_system_level(struct list_head *list)
526 struct acpi_power_resource_entry *entry;
527 int system_level = 5;
529 list_for_each_entry(entry, list, node) {
530 struct acpi_power_resource *resource = entry->resource;
532 if (system_level > resource->system_level)
533 system_level = resource->system_level;
538 /* --------------------------------------------------------------------------
539 Device Power Management
540 -------------------------------------------------------------------------- */
543 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
544 * ACPI 3.0) _PSW (Power State Wake)
545 * @dev: Device to handle.
546 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
547 * @sleep_state: Target sleep state of the system.
548 * @dev_state: Target power state of the device.
550 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
551 * State Wake) for the device, if present. On failure reset the device's
552 * wakeup.flags.valid flag.
555 * 0 if either _DSW or _PSW has been successfully executed
556 * 0 if neither _DSW nor _PSW has been found
557 * -ENODEV if the execution of either _DSW or _PSW has failed
559 int acpi_device_sleep_wake(struct acpi_device *dev,
560 int enable, int sleep_state, int dev_state)
562 union acpi_object in_arg[3];
563 struct acpi_object_list arg_list = { 3, in_arg };
564 acpi_status status = AE_OK;
567 * Try to execute _DSW first.
569 * Three agruments are needed for the _DSW object:
570 * Argument 0: enable/disable the wake capabilities
571 * Argument 1: target system state
572 * Argument 2: target device state
573 * When _DSW object is called to disable the wake capabilities, maybe
574 * the first argument is filled. The values of the other two agruments
577 in_arg[0].type = ACPI_TYPE_INTEGER;
578 in_arg[0].integer.value = enable;
579 in_arg[1].type = ACPI_TYPE_INTEGER;
580 in_arg[1].integer.value = sleep_state;
581 in_arg[2].type = ACPI_TYPE_INTEGER;
582 in_arg[2].integer.value = dev_state;
583 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
584 if (ACPI_SUCCESS(status)) {
586 } else if (status != AE_NOT_FOUND) {
587 printk(KERN_ERR PREFIX "_DSW execution failed\n");
588 dev->wakeup.flags.valid = 0;
594 in_arg[0].integer.value = enable;
595 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
596 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
597 printk(KERN_ERR PREFIX "_PSW execution failed\n");
598 dev->wakeup.flags.valid = 0;
606 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
607 * 1. Power on the power resources required for the wakeup device
608 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
609 * State Wake) for the device, if present
611 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
615 if (!dev || !dev->wakeup.flags.valid)
618 mutex_lock(&acpi_device_lock);
620 if (dev->wakeup.prepare_count++)
623 err = acpi_power_on_list(&dev->wakeup.resources);
625 dev_err(&dev->dev, "Cannot turn wakeup power resources on\n");
626 dev->wakeup.flags.valid = 0;
629 * Passing 3 as the third argument below means the device may be
630 * put into arbitrary power state afterward.
632 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
635 dev->wakeup.prepare_count = 0;
638 mutex_unlock(&acpi_device_lock);
643 * Shutdown a wakeup device, counterpart of above method
644 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
645 * State Wake) for the device, if present
646 * 2. Shutdown down the power resources
648 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
652 if (!dev || !dev->wakeup.flags.valid)
655 mutex_lock(&acpi_device_lock);
657 if (--dev->wakeup.prepare_count > 0)
661 * Executing the code below even if prepare_count is already zero when
662 * the function is called may be useful, for example for initialisation.
664 if (dev->wakeup.prepare_count < 0)
665 dev->wakeup.prepare_count = 0;
667 err = acpi_device_sleep_wake(dev, 0, 0, 0);
671 err = acpi_power_off_list(&dev->wakeup.resources);
673 dev_err(&dev->dev, "Cannot turn wakeup power resources off\n");
674 dev->wakeup.flags.valid = 0;
678 mutex_unlock(&acpi_device_lock);
682 int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
688 if (!device || !state)
692 * We know a device's inferred power state when all the resources
693 * required for a given D-state are 'on'.
695 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
696 struct list_head *list = &device->power.states[i].resources;
698 if (list_empty(list))
701 result = acpi_power_get_list_state(list, &list_state);
705 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
711 *state = ACPI_STATE_D3;
715 int acpi_power_on_resources(struct acpi_device *device, int state)
717 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
720 return acpi_power_on_list(&device->power.states[state].resources);
723 int acpi_power_transition(struct acpi_device *device, int state)
727 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
730 if (device->power.state == state || !device->flags.power_manageable)
733 if ((device->power.state < ACPI_STATE_D0)
734 || (device->power.state > ACPI_STATE_D3_COLD))
737 /* TBD: Resources must be ordered. */
740 * First we reference all power resources required in the target list
741 * (e.g. so the device doesn't lose power while transitioning). Then,
742 * we dereference all power resources used in the current list.
744 if (state < ACPI_STATE_D3_COLD)
745 result = acpi_power_on_list(
746 &device->power.states[state].resources);
748 if (!result && device->power.state < ACPI_STATE_D3_COLD)
750 &device->power.states[device->power.state].resources);
752 /* We shouldn't change the state unless the above operations succeed. */
753 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
758 static void acpi_release_power_resource(struct device *dev)
760 struct acpi_device *device = to_acpi_device(dev);
761 struct acpi_power_resource *resource;
763 resource = container_of(device, struct acpi_power_resource, device);
765 mutex_lock(&power_resource_list_lock);
766 list_del(&resource->list_node);
767 mutex_unlock(&power_resource_list_lock);
769 acpi_free_ids(device);
773 static ssize_t acpi_power_in_use_show(struct device *dev,
774 struct device_attribute *attr,
776 struct acpi_power_resource *resource;
778 resource = to_power_resource(to_acpi_device(dev));
779 return sprintf(buf, "%u\n", !!resource->ref_count);
781 static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
783 static void acpi_power_sysfs_remove(struct acpi_device *device)
785 device_remove_file(&device->dev, &dev_attr_resource_in_use);
788 int acpi_add_power_resource(acpi_handle handle)
790 struct acpi_power_resource *resource;
791 struct acpi_device *device = NULL;
792 union acpi_object acpi_object;
793 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
795 int state, result = -ENODEV;
797 acpi_bus_get_device(handle, &device);
801 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
805 device = &resource->device;
806 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
808 mutex_init(&resource->resource_lock);
809 INIT_LIST_HEAD(&resource->dependent);
810 resource->name = device->pnp.bus_id;
811 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
812 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
813 device->power.state = ACPI_STATE_UNKNOWN;
815 /* Evalute the object to get the system level and resource order. */
816 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
817 if (ACPI_FAILURE(status))
820 resource->system_level = acpi_object.power_resource.system_level;
821 resource->order = acpi_object.power_resource.resource_order;
823 result = acpi_power_get_state(handle, &state);
827 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
828 acpi_device_bid(device), state ? "on" : "off");
830 device->flags.match_driver = true;
831 result = acpi_device_add(device, acpi_release_power_resource);
835 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
836 device->remove = acpi_power_sysfs_remove;
838 mutex_lock(&power_resource_list_lock);
839 list_add(&resource->list_node, &acpi_power_resource_list);
840 mutex_unlock(&power_resource_list_lock);
841 acpi_device_add_finalize(device);
845 acpi_release_power_resource(&device->dev);
849 #ifdef CONFIG_ACPI_SLEEP
850 void acpi_resume_power_resources(void)
852 struct acpi_power_resource *resource;
854 mutex_lock(&power_resource_list_lock);
856 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
859 mutex_lock(&resource->resource_lock);
861 result = acpi_power_get_state(resource->device.handle, &state);
865 if (state == ACPI_POWER_RESOURCE_STATE_OFF
866 && resource->ref_count) {
867 dev_info(&resource->device.dev, "Turning ON\n");
868 __acpi_power_on(resource);
869 } else if (state == ACPI_POWER_RESOURCE_STATE_ON
870 && !resource->ref_count) {
871 dev_info(&resource->device.dev, "Turning OFF\n");
872 __acpi_power_off(resource);
875 mutex_unlock(&resource->resource_lock);
878 mutex_unlock(&power_resource_list_lock);