1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/acpi/power.c - ACPI Power Resources management.
5 * Copyright (C) 2001 - 2015 Intel Corp.
12 * ACPI power-managed devices may be controlled in two ways:
13 * 1. via "Device Specific (D-State) Control"
14 * 2. via "Power Resource Control".
15 * The code below deals with ACPI Power Resources control.
17 * An ACPI "power resource object" represents a software controllable power
18 * plane, clock plane, or other resource depended on by a device.
20 * A device may rely on multiple power resources, and a power resource
21 * may be shared by multiple devices.
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/sysfs.h>
31 #include <linux/acpi.h>
35 #define _COMPONENT ACPI_POWER_COMPONENT
36 ACPI_MODULE_NAME("power");
37 #define ACPI_POWER_CLASS "power_resource"
38 #define ACPI_POWER_DEVICE_NAME "Power Resource"
39 #define ACPI_POWER_FILE_INFO "info"
40 #define ACPI_POWER_FILE_STATUS "state"
41 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
42 #define ACPI_POWER_RESOURCE_STATE_ON 0x01
43 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
45 struct acpi_power_resource {
46 struct acpi_device device;
47 struct list_head list_node;
51 unsigned int ref_count;
53 struct mutex resource_lock;
56 struct acpi_power_resource_entry {
57 struct list_head node;
58 struct acpi_power_resource *resource;
61 static LIST_HEAD(acpi_power_resource_list);
62 static DEFINE_MUTEX(power_resource_list_lock);
64 /* --------------------------------------------------------------------------
65 Power Resource Management
66 -------------------------------------------------------------------------- */
69 struct acpi_power_resource *to_power_resource(struct acpi_device *device)
71 return container_of(device, struct acpi_power_resource, device);
74 static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
76 struct acpi_device *device;
78 if (acpi_bus_get_device(handle, &device))
81 return to_power_resource(device);
84 static int acpi_power_resources_list_add(acpi_handle handle,
85 struct list_head *list)
87 struct acpi_power_resource *resource = acpi_power_get_context(handle);
88 struct acpi_power_resource_entry *entry;
90 if (!resource || !list)
93 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
97 entry->resource = resource;
98 if (!list_empty(list)) {
99 struct acpi_power_resource_entry *e;
101 list_for_each_entry(e, list, node)
102 if (e->resource->order > resource->order) {
103 list_add_tail(&entry->node, &e->node);
107 list_add_tail(&entry->node, list);
111 void acpi_power_resources_list_free(struct list_head *list)
113 struct acpi_power_resource_entry *entry, *e;
115 list_for_each_entry_safe(entry, e, list, node) {
116 list_del(&entry->node);
121 static bool acpi_power_resource_is_dup(union acpi_object *package,
122 unsigned int start, unsigned int i)
124 acpi_handle rhandle, dup;
127 /* The caller is expected to check the package element types */
128 rhandle = package->package.elements[i].reference.handle;
129 for (j = start; j < i; j++) {
130 dup = package->package.elements[j].reference.handle;
138 int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
139 struct list_head *list)
144 for (i = start; i < package->package.count; i++) {
145 union acpi_object *element = &package->package.elements[i];
148 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
152 rhandle = element->reference.handle;
158 /* Some ACPI tables contain duplicate power resource references */
159 if (acpi_power_resource_is_dup(package, start, i))
162 err = acpi_add_power_resource(rhandle);
166 err = acpi_power_resources_list_add(rhandle, list);
171 acpi_power_resources_list_free(list);
176 static int acpi_power_get_state(acpi_handle handle, int *state)
178 acpi_status status = AE_OK;
179 unsigned long long sta = 0;
181 struct acpi_buffer buffer = { sizeof(node_name), node_name };
184 if (!handle || !state)
187 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
188 if (ACPI_FAILURE(status))
191 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
192 ACPI_POWER_RESOURCE_STATE_OFF;
194 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
196 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
198 *state ? "on" : "off"));
203 static int acpi_power_get_list_state(struct list_head *list, int *state)
205 struct acpi_power_resource_entry *entry;
211 /* 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 int __acpi_power_on(struct acpi_power_resource *resource)
237 acpi_status status = AE_OK;
239 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
240 if (ACPI_FAILURE(status))
243 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
249 static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
253 if (resource->ref_count++) {
254 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
255 "Power resource [%s] already on\n",
258 result = __acpi_power_on(resource);
260 resource->ref_count--;
265 static int acpi_power_on(struct acpi_power_resource *resource)
269 mutex_lock(&resource->resource_lock);
270 result = acpi_power_on_unlocked(resource);
271 mutex_unlock(&resource->resource_lock);
275 static int __acpi_power_off(struct acpi_power_resource *resource)
279 status = acpi_evaluate_object(resource->device.handle, "_OFF",
281 if (ACPI_FAILURE(status))
284 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n",
289 static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
293 if (!resource->ref_count) {
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
295 "Power resource [%s] already off\n",
300 if (--resource->ref_count) {
301 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
302 "Power resource [%s] still in use\n",
305 result = __acpi_power_off(resource);
307 resource->ref_count++;
312 static int acpi_power_off(struct acpi_power_resource *resource)
316 mutex_lock(&resource->resource_lock);
317 result = acpi_power_off_unlocked(resource);
318 mutex_unlock(&resource->resource_lock);
322 static int acpi_power_off_list(struct list_head *list)
324 struct acpi_power_resource_entry *entry;
327 list_for_each_entry_reverse(entry, list, node) {
328 result = acpi_power_off(entry->resource);
335 list_for_each_entry_continue(entry, list, node)
336 acpi_power_on(entry->resource);
341 static int acpi_power_on_list(struct list_head *list)
343 struct acpi_power_resource_entry *entry;
346 list_for_each_entry(entry, list, node) {
347 result = acpi_power_on(entry->resource);
354 list_for_each_entry_continue_reverse(entry, list, node)
355 acpi_power_off(entry->resource);
360 static struct attribute *attrs[] = {
364 static const struct attribute_group attr_groups[] = {
366 .name = "power_resources_D0",
370 .name = "power_resources_D1",
374 .name = "power_resources_D2",
377 [ACPI_STATE_D3_HOT] = {
378 .name = "power_resources_D3hot",
383 static const struct attribute_group wakeup_attr_group = {
384 .name = "power_resources_wakeup",
388 static void acpi_power_hide_list(struct acpi_device *adev,
389 struct list_head *resources,
390 const struct attribute_group *attr_group)
392 struct acpi_power_resource_entry *entry;
394 if (list_empty(resources))
397 list_for_each_entry_reverse(entry, resources, node) {
398 struct acpi_device *res_dev = &entry->resource->device;
400 sysfs_remove_link_from_group(&adev->dev.kobj,
402 dev_name(&res_dev->dev));
404 sysfs_remove_group(&adev->dev.kobj, attr_group);
407 static void acpi_power_expose_list(struct acpi_device *adev,
408 struct list_head *resources,
409 const struct attribute_group *attr_group)
411 struct acpi_power_resource_entry *entry;
414 if (list_empty(resources))
417 ret = sysfs_create_group(&adev->dev.kobj, attr_group);
421 list_for_each_entry(entry, resources, node) {
422 struct acpi_device *res_dev = &entry->resource->device;
424 ret = sysfs_add_link_to_group(&adev->dev.kobj,
427 dev_name(&res_dev->dev));
429 acpi_power_hide_list(adev, resources, attr_group);
435 static void acpi_power_expose_hide(struct acpi_device *adev,
436 struct list_head *resources,
437 const struct attribute_group *attr_group,
441 acpi_power_expose_list(adev, resources, attr_group);
443 acpi_power_hide_list(adev, resources, attr_group);
446 void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
450 if (adev->wakeup.flags.valid)
451 acpi_power_expose_hide(adev, &adev->wakeup.resources,
452 &wakeup_attr_group, add);
454 if (!adev->power.flags.power_resources)
457 for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
458 acpi_power_expose_hide(adev,
459 &adev->power.states[state].resources,
460 &attr_groups[state], add);
463 int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
465 struct acpi_power_resource_entry *entry;
466 int system_level = 5;
468 list_for_each_entry(entry, list, node) {
469 struct acpi_power_resource *resource = entry->resource;
470 acpi_handle handle = resource->device.handle;
474 mutex_lock(&resource->resource_lock);
476 result = acpi_power_get_state(handle, &state);
478 mutex_unlock(&resource->resource_lock);
481 if (state == ACPI_POWER_RESOURCE_STATE_ON) {
482 resource->ref_count++;
483 resource->wakeup_enabled = true;
485 if (system_level > resource->system_level)
486 system_level = resource->system_level;
488 mutex_unlock(&resource->resource_lock);
490 *system_level_p = system_level;
494 /* --------------------------------------------------------------------------
495 Device Power Management
496 -------------------------------------------------------------------------- */
499 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
500 * ACPI 3.0) _PSW (Power State Wake)
501 * @dev: Device to handle.
502 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
503 * @sleep_state: Target sleep state of the system.
504 * @dev_state: Target power state of the device.
506 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
507 * State Wake) for the device, if present. On failure reset the device's
508 * wakeup.flags.valid flag.
511 * 0 if either _DSW or _PSW has been successfully executed
512 * 0 if neither _DSW nor _PSW has been found
513 * -ENODEV if the execution of either _DSW or _PSW has failed
515 int acpi_device_sleep_wake(struct acpi_device *dev,
516 int enable, int sleep_state, int dev_state)
518 union acpi_object in_arg[3];
519 struct acpi_object_list arg_list = { 3, in_arg };
520 acpi_status status = AE_OK;
523 * Try to execute _DSW first.
525 * Three arguments are needed for the _DSW object:
526 * Argument 0: enable/disable the wake capabilities
527 * Argument 1: target system state
528 * Argument 2: target device state
529 * When _DSW object is called to disable the wake capabilities, maybe
530 * the first argument is filled. The values of the other two arguments
533 in_arg[0].type = ACPI_TYPE_INTEGER;
534 in_arg[0].integer.value = enable;
535 in_arg[1].type = ACPI_TYPE_INTEGER;
536 in_arg[1].integer.value = sleep_state;
537 in_arg[2].type = ACPI_TYPE_INTEGER;
538 in_arg[2].integer.value = dev_state;
539 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
540 if (ACPI_SUCCESS(status)) {
542 } else if (status != AE_NOT_FOUND) {
543 printk(KERN_ERR PREFIX "_DSW execution failed\n");
544 dev->wakeup.flags.valid = 0;
549 status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
550 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
551 printk(KERN_ERR PREFIX "_PSW execution failed\n");
552 dev->wakeup.flags.valid = 0;
560 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
561 * 1. Power on the power resources required for the wakeup device
562 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
563 * State Wake) for the device, if present
565 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
567 struct acpi_power_resource_entry *entry;
570 if (!dev || !dev->wakeup.flags.valid)
573 mutex_lock(&acpi_device_lock);
575 if (dev->wakeup.prepare_count++)
578 list_for_each_entry(entry, &dev->wakeup.resources, node) {
579 struct acpi_power_resource *resource = entry->resource;
581 mutex_lock(&resource->resource_lock);
583 if (!resource->wakeup_enabled) {
584 err = acpi_power_on_unlocked(resource);
586 resource->wakeup_enabled = true;
589 mutex_unlock(&resource->resource_lock);
593 "Cannot turn wakeup power resources on\n");
594 dev->wakeup.flags.valid = 0;
599 * Passing 3 as the third argument below means the device may be
600 * put into arbitrary power state afterward.
602 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
604 dev->wakeup.prepare_count = 0;
607 mutex_unlock(&acpi_device_lock);
612 * Shutdown a wakeup device, counterpart of above method
613 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
614 * State Wake) for the device, if present
615 * 2. Shutdown down the power resources
617 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
619 struct acpi_power_resource_entry *entry;
622 if (!dev || !dev->wakeup.flags.valid)
625 mutex_lock(&acpi_device_lock);
627 if (--dev->wakeup.prepare_count > 0)
631 * Executing the code below even if prepare_count is already zero when
632 * the function is called may be useful, for example for initialisation.
634 if (dev->wakeup.prepare_count < 0)
635 dev->wakeup.prepare_count = 0;
637 err = acpi_device_sleep_wake(dev, 0, 0, 0);
641 list_for_each_entry(entry, &dev->wakeup.resources, node) {
642 struct acpi_power_resource *resource = entry->resource;
644 mutex_lock(&resource->resource_lock);
646 if (resource->wakeup_enabled) {
647 err = acpi_power_off_unlocked(resource);
649 resource->wakeup_enabled = false;
652 mutex_unlock(&resource->resource_lock);
656 "Cannot turn wakeup power resources off\n");
657 dev->wakeup.flags.valid = 0;
663 mutex_unlock(&acpi_device_lock);
667 int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
673 if (!device || !state)
677 * We know a device's inferred power state when all the resources
678 * required for a given D-state are 'on'.
680 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
681 struct list_head *list = &device->power.states[i].resources;
683 if (list_empty(list))
686 result = acpi_power_get_list_state(list, &list_state);
690 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
696 *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
697 ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
701 int acpi_power_on_resources(struct acpi_device *device, int state)
703 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
706 return acpi_power_on_list(&device->power.states[state].resources);
709 int acpi_power_transition(struct acpi_device *device, int state)
713 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
716 if (device->power.state == state || !device->flags.power_manageable)
719 if ((device->power.state < ACPI_STATE_D0)
720 || (device->power.state > ACPI_STATE_D3_COLD))
724 * First we reference all power resources required in the target list
725 * (e.g. so the device doesn't lose power while transitioning). Then,
726 * we dereference all power resources used in the current list.
728 if (state < ACPI_STATE_D3_COLD)
729 result = acpi_power_on_list(
730 &device->power.states[state].resources);
732 if (!result && device->power.state < ACPI_STATE_D3_COLD)
734 &device->power.states[device->power.state].resources);
736 /* We shouldn't change the state unless the above operations succeed. */
737 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
742 static void acpi_release_power_resource(struct device *dev)
744 struct acpi_device *device = to_acpi_device(dev);
745 struct acpi_power_resource *resource;
747 resource = container_of(device, struct acpi_power_resource, device);
749 mutex_lock(&power_resource_list_lock);
750 list_del(&resource->list_node);
751 mutex_unlock(&power_resource_list_lock);
753 acpi_free_pnp_ids(&device->pnp);
757 static ssize_t acpi_power_in_use_show(struct device *dev,
758 struct device_attribute *attr,
760 struct acpi_power_resource *resource;
762 resource = to_power_resource(to_acpi_device(dev));
763 return sprintf(buf, "%u\n", !!resource->ref_count);
765 static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
767 static void acpi_power_sysfs_remove(struct acpi_device *device)
769 device_remove_file(&device->dev, &dev_attr_resource_in_use);
772 static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
774 mutex_lock(&power_resource_list_lock);
776 if (!list_empty(&acpi_power_resource_list)) {
777 struct acpi_power_resource *r;
779 list_for_each_entry(r, &acpi_power_resource_list, list_node)
780 if (r->order > resource->order) {
781 list_add_tail(&resource->list_node, &r->list_node);
785 list_add_tail(&resource->list_node, &acpi_power_resource_list);
788 mutex_unlock(&power_resource_list_lock);
791 int acpi_add_power_resource(acpi_handle handle)
793 struct acpi_power_resource *resource;
794 struct acpi_device *device = NULL;
795 union acpi_object acpi_object;
796 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
798 int state, result = -ENODEV;
800 acpi_bus_get_device(handle, &device);
804 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
808 device = &resource->device;
809 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
811 mutex_init(&resource->resource_lock);
812 INIT_LIST_HEAD(&resource->list_node);
813 resource->name = device->pnp.bus_id;
814 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
815 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
816 device->power.state = ACPI_STATE_UNKNOWN;
818 /* Evalute the object to get the system level and resource order. */
819 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
820 if (ACPI_FAILURE(status))
823 resource->system_level = acpi_object.power_resource.system_level;
824 resource->order = acpi_object.power_resource.resource_order;
826 result = acpi_power_get_state(handle, &state);
830 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
831 acpi_device_bid(device), state ? "on" : "off");
833 device->flags.match_driver = true;
834 result = acpi_device_add(device, acpi_release_power_resource);
838 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
839 device->remove = acpi_power_sysfs_remove;
841 acpi_power_add_resource_to_list(resource);
842 acpi_device_add_finalize(device);
846 acpi_release_power_resource(&device->dev);
850 #ifdef CONFIG_ACPI_SLEEP
851 void acpi_resume_power_resources(void)
853 struct acpi_power_resource *resource;
855 mutex_lock(&power_resource_list_lock);
857 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
860 mutex_lock(&resource->resource_lock);
862 result = acpi_power_get_state(resource->device.handle, &state);
864 mutex_unlock(&resource->resource_lock);
868 if (state == ACPI_POWER_RESOURCE_STATE_OFF
869 && resource->ref_count) {
870 dev_info(&resource->device.dev, "Turning ON\n");
871 __acpi_power_on(resource);
874 mutex_unlock(&resource->resource_lock);
877 mutex_unlock(&power_resource_list_lock);
880 void acpi_turn_off_unused_power_resources(void)
882 struct acpi_power_resource *resource;
884 mutex_lock(&power_resource_list_lock);
886 list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
889 mutex_lock(&resource->resource_lock);
891 result = acpi_power_get_state(resource->device.handle, &state);
893 mutex_unlock(&resource->resource_lock);
897 if (state == ACPI_POWER_RESOURCE_STATE_ON
898 && !resource->ref_count) {
899 dev_info(&resource->device.dev, "Turning OFF\n");
900 __acpi_power_off(resource);
903 mutex_unlock(&resource->resource_lock);
906 mutex_unlock(&power_resource_list_lock);