blob: 112256154880e016e1043243ce496b441ceea17a [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Rafael J. Wysockiaa57aca2015-07-16 02:01:28 +02003 * drivers/acpi/power.c - ACPI Power Resources management.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Rafael J. Wysockiaa57aca2015-07-16 02:01:28 +02005 * Copyright (C) 2001 - 2015 Intel Corp.
6 * Author: Andy Grover <andrew.grover@intel.com>
7 * Author: Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11/*
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".
Rafael J. Wysockiaa57aca2015-07-16 02:01:28 +020015 * The code below deals with ACPI Power Resources control.
Maximilian Luzc6237b22020-11-05 03:06:00 +010016 *
Rafael J. Wysockiaa57aca2015-07-16 02:01:28 +020017 * An ACPI "power resource object" represents a software controllable power
18 * plane, clock plane, or other resource depended on by a device.
19 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * A device may rely on multiple power resources, and a power resource
21 * may be shared by multiple devices.
22 */
23
Rafael J. Wysocki56ce8332021-01-20 19:57:03 +010024#define pr_fmt(fmt) "ACPI: PM: " fmt
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Lin Ming0090def2012-03-29 14:09:39 +080031#include <linux/pm_runtime.h>
Rafael J. Wysocki18a38702013-01-25 21:51:32 +010032#include <linux/sysfs.h>
Lv Zheng8b484632013-12-03 08:49:16 +080033#include <linux/acpi.h>
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020034#include "sleep.h"
Lin Ming0090def2012-03-29 14:09:39 +080035#include "internal.h"
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define ACPI_POWER_CLASS "power_resource"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define ACPI_POWER_DEVICE_NAME "Power Resource"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define ACPI_POWER_RESOURCE_STATE_OFF 0x00
40#define ACPI_POWER_RESOURCE_STATE_ON 0x01
41#define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
Zhao Yakuif5adfaa2008-08-11 14:57:50 +080042
Mika Westerberg45337712019-06-25 13:29:41 +030043struct acpi_power_dependent_device {
44 struct device *dev;
45 struct list_head node;
46};
47
Len Brown4be44fc2005-08-05 00:44:28 -040048struct acpi_power_resource {
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010049 struct acpi_device device;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010050 struct list_head list_node;
Len Brown4be44fc2005-08-05 00:44:28 -040051 u32 system_level;
52 u32 order;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +020053 unsigned int ref_count;
Rafael J. Wysockica84f182021-05-24 17:25:23 +020054 u8 state;
Konstantin Karasyov0a613902007-02-16 01:47:06 -050055 struct mutex resource_lock;
Mika Westerberg45337712019-06-25 13:29:41 +030056 struct list_head dependents;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010059struct acpi_power_resource_entry {
60 struct list_head node;
61 struct acpi_power_resource *resource;
62};
63
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010064static LIST_HEAD(acpi_power_resource_list);
65static DEFINE_MUTEX(power_resource_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* --------------------------------------------------------------------------
68 Power Resource Management
69 -------------------------------------------------------------------------- */
70
Rafael J. Wysocki2bc4eb92021-08-25 20:31:10 +020071static inline const char *resource_dev_name(struct acpi_power_resource *pr)
72{
73 return dev_name(&pr->device.dev);
74}
75
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +010076static inline
77struct acpi_power_resource *to_power_resource(struct acpi_device *device)
78{
79 return container_of(device, struct acpi_power_resource, device);
80}
81
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010082static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010084 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010086 if (acpi_bus_get_device(handle, &device))
87 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +010089 return to_power_resource(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +010092static int acpi_power_resources_list_add(acpi_handle handle,
93 struct list_head *list)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010094{
95 struct acpi_power_resource *resource = acpi_power_get_context(handle);
96 struct acpi_power_resource_entry *entry;
97
98 if (!resource || !list)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +010099 return -EINVAL;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100100
101 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
102 if (!entry)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100103 return -ENOMEM;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100104
105 entry->resource = resource;
106 if (!list_empty(list)) {
107 struct acpi_power_resource_entry *e;
108
109 list_for_each_entry(e, list, node)
110 if (e->resource->order > resource->order) {
111 list_add_tail(&entry->node, &e->node);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100112 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100113 }
114 }
115 list_add_tail(&entry->node, list);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100116 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100117}
118
119void acpi_power_resources_list_free(struct list_head *list)
120{
121 struct acpi_power_resource_entry *entry, *e;
122
123 list_for_each_entry_safe(entry, e, list, node) {
124 list_del(&entry->node);
125 kfree(entry);
126 }
127}
128
Hans de Goede7d7b4672018-12-30 18:25:00 +0100129static bool acpi_power_resource_is_dup(union acpi_object *package,
130 unsigned int start, unsigned int i)
131{
132 acpi_handle rhandle, dup;
133 unsigned int j;
134
135 /* The caller is expected to check the package element types */
136 rhandle = package->package.elements[i].reference.handle;
137 for (j = start; j < i; j++) {
138 dup = package->package.elements[j].reference.handle;
139 if (dup == rhandle)
140 return true;
141 }
142
143 return false;
144}
145
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100146int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
147 struct list_head *list)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100148{
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100149 unsigned int i;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100150 int err = 0;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100151
152 for (i = start; i < package->package.count; i++) {
153 union acpi_object *element = &package->package.elements[i];
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200154 struct acpi_device *rdev;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100155 acpi_handle rhandle;
156
157 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100158 err = -ENODATA;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100159 break;
160 }
161 rhandle = element->reference.handle;
162 if (!rhandle) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100163 err = -ENODEV;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100164 break;
165 }
Hans de Goede7d7b4672018-12-30 18:25:00 +0100166
167 /* Some ACPI tables contain duplicate power resource references */
168 if (acpi_power_resource_is_dup(package, start, i))
169 continue;
170
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200171 rdev = acpi_add_power_resource(rhandle);
172 if (!rdev) {
173 err = -ENODEV;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100174 break;
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200175 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100176 err = acpi_power_resources_list_add(rhandle, list);
177 if (err)
178 break;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100179 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100180 if (err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100181 acpi_power_resources_list_free(list);
182
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100183 return err;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100184}
185
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200186static int __get_state(acpi_handle handle, u8 *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Len Brown4be44fc2005-08-05 00:44:28 -0400188 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400189 unsigned long long sta = 0;
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200190 u8 cur_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Zhao Yakuia51e1452008-08-11 14:55:05 +0800192 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400194 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200196 cur_state = sta & ACPI_POWER_RESOURCE_STATE_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Rafael J. Wysocki56ce8332021-01-20 19:57:03 +0100198 acpi_handle_debug(handle, "Power resource is %s\n",
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200199 cur_state ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200201 *state = cur_state;
Patrick Mocheld550d982006-06-27 00:41:40 -0400202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200205static int acpi_power_get_state(struct acpi_power_resource *resource, u8 *state)
206{
207 if (resource->state == ACPI_POWER_RESOURCE_STATE_UNKNOWN) {
208 int ret;
209
210 ret = __get_state(resource->device.handle, &resource->state);
211 if (ret)
212 return ret;
213 }
214
215 *state = resource->state;
216 return 0;
217}
218
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200219static int acpi_power_get_list_state(struct list_head *list, u8 *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100221 struct acpi_power_resource_entry *entry;
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200222 u8 cur_state = ACPI_POWER_RESOURCE_STATE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* The state of the list is 'on' IFF all resources are 'on'. */
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100228 list_for_each_entry(entry, list, node) {
229 struct acpi_power_resource *resource = entry->resource;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100230 int result;
231
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100232 mutex_lock(&resource->resource_lock);
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200233 result = acpi_power_get_state(resource, &cur_state);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100234 mutex_unlock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100235 if (result)
236 return result;
237
238 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 break;
240 }
241
Rafael J. Wysocki56ce8332021-01-20 19:57:03 +0100242 pr_debug("Power resource list is %s\n", cur_state ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100244 *state = cur_state;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Mika Westerberg45337712019-06-25 13:29:41 +0300248static int
249acpi_power_resource_add_dependent(struct acpi_power_resource *resource,
250 struct device *dev)
251{
252 struct acpi_power_dependent_device *dep;
253 int ret = 0;
254
255 mutex_lock(&resource->resource_lock);
256 list_for_each_entry(dep, &resource->dependents, node) {
257 /* Only add it once */
258 if (dep->dev == dev)
259 goto unlock;
260 }
261
262 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
263 if (!dep) {
264 ret = -ENOMEM;
265 goto unlock;
266 }
267
268 dep->dev = dev;
269 list_add_tail(&dep->node, &resource->dependents);
Rafael J. Wysocki2bc4eb92021-08-25 20:31:10 +0200270 dev_dbg(dev, "added power dependency to [%s]\n",
271 resource_dev_name(resource));
Mika Westerberg45337712019-06-25 13:29:41 +0300272
273unlock:
274 mutex_unlock(&resource->resource_lock);
275 return ret;
276}
277
278static void
279acpi_power_resource_remove_dependent(struct acpi_power_resource *resource,
280 struct device *dev)
281{
282 struct acpi_power_dependent_device *dep;
283
284 mutex_lock(&resource->resource_lock);
285 list_for_each_entry(dep, &resource->dependents, node) {
286 if (dep->dev == dev) {
287 list_del(&dep->node);
288 kfree(dep);
289 dev_dbg(dev, "removed power dependency to [%s]\n",
Rafael J. Wysocki2bc4eb92021-08-25 20:31:10 +0200290 resource_dev_name(resource));
Mika Westerberg45337712019-06-25 13:29:41 +0300291 break;
292 }
293 }
294 mutex_unlock(&resource->resource_lock);
295}
296
297/**
298 * acpi_device_power_add_dependent - Add dependent device of this ACPI device
299 * @adev: ACPI device pointer
300 * @dev: Dependent device
301 *
302 * If @adev has non-empty _PR0 the @dev is added as dependent device to all
303 * power resources returned by it. This means that whenever these power
304 * resources are turned _ON the dependent devices get runtime resumed. This
305 * is needed for devices such as PCI to allow its driver to re-initialize
306 * it after it went to D0uninitialized.
307 *
308 * If @adev does not have _PR0 this does nothing.
309 *
310 * Returns %0 in case of success and negative errno otherwise.
311 */
312int acpi_device_power_add_dependent(struct acpi_device *adev,
313 struct device *dev)
314{
315 struct acpi_power_resource_entry *entry;
316 struct list_head *resources;
317 int ret;
318
319 if (!adev->flags.power_manageable)
320 return 0;
321
322 resources = &adev->power.states[ACPI_STATE_D0].resources;
323 list_for_each_entry(entry, resources, node) {
324 ret = acpi_power_resource_add_dependent(entry->resource, dev);
325 if (ret)
326 goto err;
327 }
328
329 return 0;
330
331err:
332 list_for_each_entry(entry, resources, node)
333 acpi_power_resource_remove_dependent(entry->resource, dev);
334
335 return ret;
336}
337
338/**
339 * acpi_device_power_remove_dependent - Remove dependent device
340 * @adev: ACPI device pointer
341 * @dev: Dependent device
342 *
343 * Does the opposite of acpi_device_power_add_dependent() and removes the
344 * dependent device if it is found. Can be called to @adev that does not
345 * have _PR0 as well.
346 */
347void acpi_device_power_remove_dependent(struct acpi_device *adev,
348 struct device *dev)
349{
350 struct acpi_power_resource_entry *entry;
351 struct list_head *resources;
352
353 if (!adev->flags.power_manageable)
354 return;
355
356 resources = &adev->power.states[ACPI_STATE_D0].resources;
357 list_for_each_entry_reverse(entry, resources, node)
358 acpi_power_resource_remove_dependent(entry->resource, dev);
359}
360
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200361static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Rafael J. Wysockifad40a62021-08-27 21:07:58 +0200363 acpi_handle handle = resource->device.handle;
Mika Westerberg45337712019-06-25 13:29:41 +0300364 struct acpi_power_dependent_device *dep;
Len Brown4be44fc2005-08-05 00:44:28 -0400365 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500366
Rafael J. Wysockifad40a62021-08-27 21:07:58 +0200367 status = acpi_evaluate_object(handle, "_ON", NULL, NULL);
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200368 if (ACPI_FAILURE(status)) {
369 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
Patrick Mocheld550d982006-06-27 00:41:40 -0400370 return -ENODEV;
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200371 }
372
373 resource->state = ACPI_POWER_RESOURCE_STATE_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Rafael J. Wysockifad40a62021-08-27 21:07:58 +0200375 acpi_handle_debug(handle, "Power resource turned on\n");
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200376
Mika Westerberg45337712019-06-25 13:29:41 +0300377 /*
378 * If there are other dependents on this power resource we need to
379 * resume them now so that their drivers can re-initialize the
380 * hardware properly after it went back to D0.
381 */
382 if (list_empty(&resource->dependents) ||
383 list_is_singular(&resource->dependents))
384 return 0;
385
386 list_for_each_entry(dep, &resource->dependents, node) {
387 dev_dbg(dep->dev, "runtime resuming because [%s] turned on\n",
Rafael J. Wysocki2bc4eb92021-08-25 20:31:10 +0200388 resource_dev_name(resource));
Mika Westerberg45337712019-06-25 13:29:41 +0300389 pm_request_resume(dep->dev);
390 }
391
Patrick Mocheld550d982006-06-27 00:41:40 -0400392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100395static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100397 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200398
399 if (resource->ref_count++) {
Rafael J. Wysockifad40a62021-08-27 21:07:58 +0200400 acpi_handle_debug(resource->device.handle,
401 "Power resource already on\n");
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200402 } else {
403 result = __acpi_power_on(resource);
Rafael J. Wysocki41863fc2013-10-16 23:05:42 +0200404 if (result)
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100405 resource->ref_count--;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500406 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100407 return result;
408}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100410static int acpi_power_on(struct acpi_power_resource *resource)
411{
412 int result;
413
414 mutex_lock(&resource->resource_lock);
415 result = acpi_power_on_unlocked(resource);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500416 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100417 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100420static int __acpi_power_off(struct acpi_power_resource *resource)
421{
Rafael J. Wysockifad40a62021-08-27 21:07:58 +0200422 acpi_handle handle = resource->device.handle;
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100423 acpi_status status;
424
Rafael J. Wysockifad40a62021-08-27 21:07:58 +0200425 status = acpi_evaluate_object(handle, "_OFF", NULL, NULL);
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200426 if (ACPI_FAILURE(status)) {
427 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100428 return -ENODEV;
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200429 }
430
431 resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100432
Rafael J. Wysockifad40a62021-08-27 21:07:58 +0200433 acpi_handle_debug(handle, "Power resource turned off\n");
Rafael J. Wysocki56ce8332021-01-20 19:57:03 +0100434
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100435 return 0;
436}
437
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100438static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200439{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100440 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200441
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200442 if (!resource->ref_count) {
Rafael J. Wysockifad40a62021-08-27 21:07:58 +0200443 acpi_handle_debug(resource->device.handle,
444 "Power resource already off\n");
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100445 return 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200446 }
447
448 if (--resource->ref_count) {
Rafael J. Wysockifad40a62021-08-27 21:07:58 +0200449 acpi_handle_debug(resource->device.handle,
450 "Power resource still in use\n");
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100451 } else {
452 result = __acpi_power_off(resource);
453 if (result)
454 resource->ref_count++;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200455 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100456 return result;
457}
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200458
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100459static int acpi_power_off(struct acpi_power_resource *resource)
460{
461 int result;
462
463 mutex_lock(&resource->resource_lock);
464 result = acpi_power_off_unlocked(resource);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200465 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200466 return result;
467}
468
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100469static int acpi_power_off_list(struct list_head *list)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100470{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100471 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100472 int result = 0;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100473
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100474 list_for_each_entry_reverse(entry, list, node) {
475 result = acpi_power_off(entry->resource);
476 if (result)
477 goto err;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100478 }
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100479 return 0;
480
481 err:
482 list_for_each_entry_continue(entry, list, node)
483 acpi_power_on(entry->resource);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100484
485 return result;
486}
487
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100488static int acpi_power_on_list(struct list_head *list)
489{
490 struct acpi_power_resource_entry *entry;
491 int result = 0;
492
493 list_for_each_entry(entry, list, node) {
494 result = acpi_power_on(entry->resource);
495 if (result)
496 goto err;
497 }
498 return 0;
499
500 err:
501 list_for_each_entry_continue_reverse(entry, list, node)
502 acpi_power_off(entry->resource);
503
504 return result;
505}
506
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100507static struct attribute *attrs[] = {
508 NULL,
509};
510
Arvind Yadav26408b22017-06-30 17:39:05 +0530511static const struct attribute_group attr_groups[] = {
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100512 [ACPI_STATE_D0] = {
513 .name = "power_resources_D0",
514 .attrs = attrs,
515 },
516 [ACPI_STATE_D1] = {
517 .name = "power_resources_D1",
518 .attrs = attrs,
519 },
520 [ACPI_STATE_D2] = {
521 .name = "power_resources_D2",
522 .attrs = attrs,
523 },
524 [ACPI_STATE_D3_HOT] = {
525 .name = "power_resources_D3hot",
526 .attrs = attrs,
527 },
528};
529
Arvind Yadav26408b22017-06-30 17:39:05 +0530530static const struct attribute_group wakeup_attr_group = {
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200531 .name = "power_resources_wakeup",
532 .attrs = attrs,
533};
534
535static void acpi_power_hide_list(struct acpi_device *adev,
536 struct list_head *resources,
Arvind Yadav26408b22017-06-30 17:39:05 +0530537 const struct attribute_group *attr_group)
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100538{
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100539 struct acpi_power_resource_entry *entry;
540
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200541 if (list_empty(resources))
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100542 return;
543
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200544 list_for_each_entry_reverse(entry, resources, node) {
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100545 struct acpi_device *res_dev = &entry->resource->device;
546
547 sysfs_remove_link_from_group(&adev->dev.kobj,
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200548 attr_group->name,
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100549 dev_name(&res_dev->dev));
550 }
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200551 sysfs_remove_group(&adev->dev.kobj, attr_group);
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100552}
553
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200554static void acpi_power_expose_list(struct acpi_device *adev,
555 struct list_head *resources,
Arvind Yadav26408b22017-06-30 17:39:05 +0530556 const struct attribute_group *attr_group)
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100557{
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100558 struct acpi_power_resource_entry *entry;
559 int ret;
560
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200561 if (list_empty(resources))
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100562 return;
563
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200564 ret = sysfs_create_group(&adev->dev.kobj, attr_group);
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100565 if (ret)
566 return;
567
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200568 list_for_each_entry(entry, resources, node) {
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100569 struct acpi_device *res_dev = &entry->resource->device;
570
571 ret = sysfs_add_link_to_group(&adev->dev.kobj,
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200572 attr_group->name,
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100573 &res_dev->dev.kobj,
574 dev_name(&res_dev->dev));
575 if (ret) {
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200576 acpi_power_hide_list(adev, resources, attr_group);
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100577 break;
578 }
579 }
580}
581
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200582static void acpi_power_expose_hide(struct acpi_device *adev,
583 struct list_head *resources,
Arvind Yadav26408b22017-06-30 17:39:05 +0530584 const struct attribute_group *attr_group,
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200585 bool expose)
586{
587 if (expose)
588 acpi_power_expose_list(adev, resources, attr_group);
589 else
590 acpi_power_hide_list(adev, resources, attr_group);
591}
592
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100593void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
594{
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100595 int state;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100596
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200597 if (adev->wakeup.flags.valid)
598 acpi_power_expose_hide(adev, &adev->wakeup.resources,
599 &wakeup_attr_group, add);
600
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100601 if (!adev->power.flags.power_resources)
602 return;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100603
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200604 for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
605 acpi_power_expose_hide(adev,
606 &adev->power.states[state].resources,
607 &attr_groups[state], add);
Lin Ming0090def2012-03-29 14:09:39 +0800608}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100609
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100610int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100611{
612 struct acpi_power_resource_entry *entry;
613 int system_level = 5;
614
615 list_for_each_entry(entry, list, node) {
616 struct acpi_power_resource *resource = entry->resource;
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200617 u8 state;
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100618
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100619 mutex_lock(&resource->resource_lock);
620
Rafael J. Wysocki7a632962021-10-15 19:01:28 +0200621 /*
622 * Make sure that the power resource state and its reference
623 * counter value are consistent with each other.
624 */
625 if (!resource->ref_count &&
626 !acpi_power_get_state(resource, &state) &&
627 state == ACPI_POWER_RESOURCE_STATE_ON)
628 __acpi_power_off(resource);
629
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100630 if (system_level > resource->system_level)
631 system_level = resource->system_level;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100632
633 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100634 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100635 *system_level_p = system_level;
636 return 0;
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100637}
638
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100639/* --------------------------------------------------------------------------
640 Device Power Management
641 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800642
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200643/**
644 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
645 * ACPI 3.0) _PSW (Power State Wake)
646 * @dev: Device to handle.
647 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
648 * @sleep_state: Target sleep state of the system.
649 * @dev_state: Target power state of the device.
650 *
651 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
652 * State Wake) for the device, if present. On failure reset the device's
653 * wakeup.flags.valid flag.
654 *
655 * RETURN VALUE:
656 * 0 if either _DSW or _PSW has been successfully executed
657 * 0 if neither _DSW nor _PSW has been found
658 * -ENODEV if the execution of either _DSW or _PSW has failed
659 */
660int acpi_device_sleep_wake(struct acpi_device *dev,
Maximilian Luzc6237b22020-11-05 03:06:00 +0100661 int enable, int sleep_state, int dev_state)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200662{
663 union acpi_object in_arg[3];
664 struct acpi_object_list arg_list = { 3, in_arg };
665 acpi_status status = AE_OK;
666
667 /*
668 * Try to execute _DSW first.
669 *
Bjorn Helgaas603fadf2019-03-25 13:34:00 -0500670 * Three arguments are needed for the _DSW object:
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200671 * Argument 0: enable/disable the wake capabilities
672 * Argument 1: target system state
673 * Argument 2: target device state
674 * When _DSW object is called to disable the wake capabilities, maybe
Bjorn Helgaas603fadf2019-03-25 13:34:00 -0500675 * the first argument is filled. The values of the other two arguments
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200676 * are meaningless.
677 */
678 in_arg[0].type = ACPI_TYPE_INTEGER;
679 in_arg[0].integer.value = enable;
680 in_arg[1].type = ACPI_TYPE_INTEGER;
681 in_arg[1].integer.value = sleep_state;
682 in_arg[2].type = ACPI_TYPE_INTEGER;
683 in_arg[2].integer.value = dev_state;
684 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
685 if (ACPI_SUCCESS(status)) {
686 return 0;
687 } else if (status != AE_NOT_FOUND) {
Rafael J. Wysocki56ce8332021-01-20 19:57:03 +0100688 acpi_handle_info(dev->handle, "_DSW execution failed\n");
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200689 dev->wakeup.flags.valid = 0;
690 return -ENODEV;
691 }
692
693 /* Execute _PSW */
Jiang Liu0db98202013-06-29 00:24:39 +0800694 status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200695 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
Rafael J. Wysocki56ce8332021-01-20 19:57:03 +0100696 acpi_handle_info(dev->handle, "_PSW execution failed\n");
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200697 dev->wakeup.flags.valid = 0;
698 return -ENODEV;
699 }
700
701 return 0;
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/*
705 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
Maximilian Luzc6237b22020-11-05 03:06:00 +0100706 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200707 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
708 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200710int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100712 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200715 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200717 mutex_lock(&acpi_device_lock);
718
719 if (dev->wakeup.prepare_count++)
720 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200721
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200722 err = acpi_power_on_list(&dev->wakeup.resources);
723 if (err) {
724 dev_err(&dev->dev, "Cannot turn on wakeup power resources\n");
725 dev->wakeup.flags.valid = 0;
726 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200728
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100729 /*
730 * Passing 3 as the third argument below means the device may be
731 * put into arbitrary power state afterward.
732 */
733 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Rafael J. Wysockia9a8f822021-10-15 19:04:26 +0200734 if (err) {
735 acpi_power_off_list(&dev->wakeup.resources);
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200736 dev->wakeup.prepare_count = 0;
Rafael J. Wysockia9a8f822021-10-15 19:04:26 +0200737 }
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200738
739 out:
740 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200741 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744/*
745 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200746 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
747 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * 2. Shutdown down the power resources
749 */
Len Brown4be44fc2005-08-05 00:44:28 -0400750int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100752 struct acpi_power_resource_entry *entry;
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100753 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200758 mutex_lock(&acpi_device_lock);
759
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200760 if (dev->wakeup.prepare_count > 1) {
761 dev->wakeup.prepare_count--;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200762 goto out;
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200763 }
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200764
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200765 /* Do nothing if wakeup power has not been enabled for this device. */
766 if (!dev->wakeup.prepare_count)
767 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200768
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200769 err = acpi_device_sleep_wake(dev, 0, 0, 0);
770 if (err)
771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200773 /*
774 * All of the power resources in the list need to be turned off even if
775 * there are errors.
776 */
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100777 list_for_each_entry(entry, &dev->wakeup.resources, node) {
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200778 int ret;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100779
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200780 ret = acpi_power_off(entry->resource);
781 if (ret && !err)
782 err = ret;
783 }
784 if (err) {
785 dev_err(&dev->dev, "Cannot turn off wakeup power resources\n");
786 dev->wakeup.flags.valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200789 out:
790 mutex_unlock(&acpi_device_lock);
791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100794int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200796 u8 list_state = ACPI_POWER_RESOURCE_STATE_OFF;
Len Brown4be44fc2005-08-05 00:44:28 -0400797 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400798 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100800 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400801 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /*
804 * We know a device's inferred power state when all the resources
805 * required for a given D-state are 'on'.
806 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200807 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100808 struct list_head *list = &device->power.states[i].resources;
809
810 if (list_empty(list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 continue;
812
813 result = acpi_power_get_list_state(list, &list_state);
814 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400815 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100818 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 }
822
Rafael J. Wysocki20dacb72015-05-16 01:55:35 +0200823 *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
824 ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
Patrick Mocheld550d982006-06-27 00:41:40 -0400825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100828int acpi_power_on_resources(struct acpi_device *device, int state)
829{
Rafael J. Wysocki87e753b2013-01-22 12:56:16 +0100830 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100831 return -EINVAL;
832
833 return acpi_power_on_list(&device->power.states[state].resources);
834}
835
Len Brown4be44fc2005-08-05 00:44:28 -0400836int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200838 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800840 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400841 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100843 if (device->power.state == state || !device->flags.power_manageable)
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100844 return 0;
845
Len Brown4be44fc2005-08-05 00:44:28 -0400846 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800847 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400848 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /*
851 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100852 * (e.g. so the device doesn't lose power while transitioning). Then,
853 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200855 if (state < ACPI_STATE_D3_COLD)
856 result = acpi_power_on_list(
857 &device->power.states[state].resources);
858
859 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100860 acpi_power_off_list(
861 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100863 /* We shouldn't change the state unless the above operations succeed. */
864 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Patrick Mocheld550d982006-06-27 00:41:40 -0400866 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100869static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100871 struct acpi_device *device = to_acpi_device(dev);
872 struct acpi_power_resource *resource;
873
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100874 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100875
876 mutex_lock(&power_resource_list_lock);
877 list_del(&resource->list_node);
878 mutex_unlock(&power_resource_list_lock);
879
Toshi Kanic0af4172013-03-04 21:30:42 +0000880 acpi_free_pnp_ids(&device->pnp);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100881 kfree(resource);
882}
883
Dwaipayan Ray0f39ee82020-12-17 18:15:36 +0530884static ssize_t resource_in_use_show(struct device *dev,
885 struct device_attribute *attr,
886 char *buf)
887{
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100888 struct acpi_power_resource *resource;
889
890 resource = to_power_resource(to_acpi_device(dev));
891 return sprintf(buf, "%u\n", !!resource->ref_count);
892}
Dwaipayan Ray0f39ee82020-12-17 18:15:36 +0530893static DEVICE_ATTR_RO(resource_in_use);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100894
895static void acpi_power_sysfs_remove(struct acpi_device *device)
896{
897 device_remove_file(&device->dev, &dev_attr_resource_in_use);
898}
899
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +0200900static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
901{
902 mutex_lock(&power_resource_list_lock);
903
904 if (!list_empty(&acpi_power_resource_list)) {
905 struct acpi_power_resource *r;
906
907 list_for_each_entry(r, &acpi_power_resource_list, list_node)
908 if (r->order > resource->order) {
909 list_add_tail(&resource->list_node, &r->list_node);
910 goto out;
911 }
912 }
913 list_add_tail(&resource->list_node, &acpi_power_resource_list);
914
915 out:
916 mutex_unlock(&power_resource_list_lock);
917}
918
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200919struct acpi_device *acpi_add_power_resource(acpi_handle handle)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100920{
921 struct acpi_power_resource *resource;
922 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400923 union acpi_object acpi_object;
924 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100925 acpi_status status;
Rafael J. Wysockia1224f32021-10-15 19:14:10 +0200926 u8 state_dummy;
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200927 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100929 acpi_bus_get_device(handle, &device);
930 if (device)
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200931 return device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100933 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (!resource)
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200935 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100937 device = &resource->device;
Rafael J. Wysockic830dbc2021-04-07 16:33:38 +0200938 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500939 mutex_init(&resource->resource_lock);
Rafael J. Wysocki6ee22e9d2013-06-19 00:44:45 +0200940 INIT_LIST_HEAD(&resource->list_node);
Mika Westerberg45337712019-06-25 13:29:41 +0300941 INIT_LIST_HEAD(&resource->dependents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
943 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100944 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Tom Saeger935ab852021-03-12 18:55:35 -0700946 /* Evaluate the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100947 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
948 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100949 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 resource->system_level = acpi_object.power_resource.system_level;
952 resource->order = acpi_object.power_resource.resource_order;
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200953 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Rafael J. Wysockia1224f32021-10-15 19:14:10 +0200955 /* Get the initial state or just flip it on if that fails. */
956 if (acpi_power_get_state(resource, &state_dummy))
957 __acpi_power_on(resource);
958
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200959 pr_info("%s [%s]\n", acpi_device_name(device), acpi_device_bid(device));
Len Brown4be44fc2005-08-05 00:44:28 -0400960
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100961 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100962 result = acpi_device_add(device, acpi_release_power_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100964 goto err;
Len Brown4be44fc2005-08-05 00:44:28 -0400965
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100966 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
967 device->remove = acpi_power_sysfs_remove;
968
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +0200969 acpi_power_add_resource_to_list(resource);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100970 acpi_device_add_finalize(device);
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200971 return device;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100972
973 err:
974 acpi_release_power_resource(&device->dev);
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200975 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100978#ifdef CONFIG_ACPI_SLEEP
979void acpi_resume_power_resources(void)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500980{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200981 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500982
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100983 mutex_lock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500984
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100985 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200986 int result;
987 u8 state;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200988
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100989 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500990
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200991 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
992 result = acpi_power_get_state(resource, &state);
Lan Tianyud7d49012013-10-15 19:48:11 +0800993 if (result) {
994 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100995 continue;
Lan Tianyud7d49012013-10-15 19:48:11 +0800996 }
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100997
998 if (state == ACPI_POWER_RESOURCE_STATE_OFF
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100999 && resource->ref_count) {
Rafael J. Wysockifad40a62021-08-27 21:07:58 +02001000 acpi_handle_debug(resource->device.handle, "Turning ON\n");
Rafael J. Wysocki781d7372013-01-17 14:11:06 +01001001 __acpi_power_on(resource);
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +02001002 }
1003
1004 mutex_unlock(&resource->resource_lock);
1005 }
Hans de Goede8ece1d82017-04-30 22:54:16 +02001006
1007 mutex_unlock(&power_resource_list_lock);
1008}
Rafael J. Wysocki29038ae2021-05-10 14:02:17 +02001009#endif
Hans de Goede8ece1d82017-04-30 22:54:16 +02001010
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +02001011/**
1012 * acpi_turn_off_unused_power_resources - Turn off power resources not in use.
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +02001013 */
Rafael J. Wysocki63811952021-05-24 17:26:16 +02001014void acpi_turn_off_unused_power_resources(void)
Hans de Goede8ece1d82017-04-30 22:54:16 +02001015{
1016 struct acpi_power_resource *resource;
1017
1018 mutex_lock(&power_resource_list_lock);
1019
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +02001020 list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +02001021 mutex_lock(&resource->resource_lock);
1022
Rafael J. Wysocki63811952021-05-24 17:26:16 +02001023 if (!resource->ref_count &&
Rafael J. Wysockibc283682021-10-15 19:12:21 +02001024 resource->state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysockifad40a62021-08-27 21:07:58 +02001025 acpi_handle_debug(resource->device.handle, "Turning OFF\n");
Rafael J. Wysocki63811952021-05-24 17:26:16 +02001026 __acpi_power_off(resource);
1027 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -05001028
Rafael J. Wysocki781d7372013-01-17 14:11:06 +01001029 mutex_unlock(&resource->resource_lock);
1030 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -05001031
Rafael J. Wysocki781d7372013-01-17 14:11:06 +01001032 mutex_unlock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -05001033}