blob: 8c4a73a1351e8db15b480eaa8ff15a566f465e95 [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. Wysocki99ece712021-12-03 17:37:10 +010084 struct acpi_device *device = acpi_fetch_acpi_dev(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Rafael J. Wysocki99ece712021-12-03 17:37:10 +010086 if (!device)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010087 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
Rafael J. Wysocki5b6a8f12021-11-16 21:06:17 +0100719 dev_dbg(&dev->dev, "Enabling wakeup power (count %d)\n",
720 dev->wakeup.prepare_count);
721
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200722 if (dev->wakeup.prepare_count++)
723 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200724
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200725 err = acpi_power_on_list(&dev->wakeup.resources);
726 if (err) {
727 dev_err(&dev->dev, "Cannot turn on wakeup power resources\n");
728 dev->wakeup.flags.valid = 0;
729 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200731
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100732 /*
733 * Passing 3 as the third argument below means the device may be
734 * put into arbitrary power state afterward.
735 */
736 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Rafael J. Wysockia9a8f822021-10-15 19:04:26 +0200737 if (err) {
738 acpi_power_off_list(&dev->wakeup.resources);
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200739 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki5b6a8f12021-11-16 21:06:17 +0100740 goto out;
Rafael J. Wysockia9a8f822021-10-15 19:04:26 +0200741 }
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200742
Rafael J. Wysocki5b6a8f12021-11-16 21:06:17 +0100743 dev_dbg(&dev->dev, "Wakeup power enabled\n");
744
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200745 out:
746 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200747 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750/*
751 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200752 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
753 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * 2. Shutdown down the power resources
755 */
Len Brown4be44fc2005-08-05 00:44:28 -0400756int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100758 struct acpi_power_resource_entry *entry;
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100759 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200762 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200764 mutex_lock(&acpi_device_lock);
765
Rafael J. Wysocki5b6a8f12021-11-16 21:06:17 +0100766 dev_dbg(&dev->dev, "Disabling wakeup power (count %d)\n",
767 dev->wakeup.prepare_count);
768
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200769 /* Do nothing if wakeup power has not been enabled for this device. */
Rafael J. Wysocki452a3e72021-11-04 22:54:17 +0100770 if (dev->wakeup.prepare_count <= 0)
771 goto out;
772
773 if (--dev->wakeup.prepare_count > 0)
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200774 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200775
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200776 err = acpi_device_sleep_wake(dev, 0, 0, 0);
777 if (err)
778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200780 /*
781 * All of the power resources in the list need to be turned off even if
782 * there are errors.
783 */
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100784 list_for_each_entry(entry, &dev->wakeup.resources, node) {
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200785 int ret;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100786
Rafael J. Wysockia2d7b2e2021-10-16 12:11:08 +0200787 ret = acpi_power_off(entry->resource);
788 if (ret && !err)
789 err = ret;
790 }
791 if (err) {
792 dev_err(&dev->dev, "Cannot turn off wakeup power resources\n");
793 dev->wakeup.flags.valid = 0;
Rafael J. Wysocki5b6a8f12021-11-16 21:06:17 +0100794 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
Rafael J. Wysocki5b6a8f12021-11-16 21:06:17 +0100797 dev_dbg(&dev->dev, "Wakeup power disabled\n");
798
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200799 out:
800 mutex_unlock(&acpi_device_lock);
801 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100804int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200806 u8 list_state = ACPI_POWER_RESOURCE_STATE_OFF;
Len Brown4be44fc2005-08-05 00:44:28 -0400807 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400808 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100810 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400811 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /*
814 * We know a device's inferred power state when all the resources
815 * required for a given D-state are 'on'.
816 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200817 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100818 struct list_head *list = &device->power.states[i].resources;
819
820 if (list_empty(list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 continue;
822
823 result = acpi_power_get_list_state(list, &list_state);
824 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400825 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100828 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 }
832
Rafael J. Wysocki20dacb72015-05-16 01:55:35 +0200833 *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
834 ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
Patrick Mocheld550d982006-06-27 00:41:40 -0400835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100838int acpi_power_on_resources(struct acpi_device *device, int state)
839{
Rafael J. Wysocki87e753b2013-01-22 12:56:16 +0100840 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100841 return -EINVAL;
842
843 return acpi_power_on_list(&device->power.states[state].resources);
844}
845
Len Brown4be44fc2005-08-05 00:44:28 -0400846int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200848 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800850 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400851 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100853 if (device->power.state == state || !device->flags.power_manageable)
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100854 return 0;
855
Len Brown4be44fc2005-08-05 00:44:28 -0400856 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800857 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400858 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /*
861 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100862 * (e.g. so the device doesn't lose power while transitioning). Then,
863 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200865 if (state < ACPI_STATE_D3_COLD)
866 result = acpi_power_on_list(
867 &device->power.states[state].resources);
868
869 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100870 acpi_power_off_list(
871 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100873 /* We shouldn't change the state unless the above operations succeed. */
874 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Patrick Mocheld550d982006-06-27 00:41:40 -0400876 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100879static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100881 struct acpi_device *device = to_acpi_device(dev);
882 struct acpi_power_resource *resource;
883
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100884 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100885
886 mutex_lock(&power_resource_list_lock);
887 list_del(&resource->list_node);
888 mutex_unlock(&power_resource_list_lock);
889
Toshi Kanic0af4172013-03-04 21:30:42 +0000890 acpi_free_pnp_ids(&device->pnp);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100891 kfree(resource);
892}
893
Dwaipayan Ray0f39ee82020-12-17 18:15:36 +0530894static ssize_t resource_in_use_show(struct device *dev,
895 struct device_attribute *attr,
896 char *buf)
897{
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100898 struct acpi_power_resource *resource;
899
900 resource = to_power_resource(to_acpi_device(dev));
901 return sprintf(buf, "%u\n", !!resource->ref_count);
902}
Dwaipayan Ray0f39ee82020-12-17 18:15:36 +0530903static DEVICE_ATTR_RO(resource_in_use);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100904
905static void acpi_power_sysfs_remove(struct acpi_device *device)
906{
907 device_remove_file(&device->dev, &dev_attr_resource_in_use);
908}
909
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +0200910static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
911{
912 mutex_lock(&power_resource_list_lock);
913
914 if (!list_empty(&acpi_power_resource_list)) {
915 struct acpi_power_resource *r;
916
917 list_for_each_entry(r, &acpi_power_resource_list, list_node)
918 if (r->order > resource->order) {
919 list_add_tail(&resource->list_node, &r->list_node);
920 goto out;
921 }
922 }
923 list_add_tail(&resource->list_node, &acpi_power_resource_list);
924
925 out:
926 mutex_unlock(&power_resource_list_lock);
927}
928
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200929struct acpi_device *acpi_add_power_resource(acpi_handle handle)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100930{
Rafael J. Wysocki99ece712021-12-03 17:37:10 +0100931 struct acpi_device *device = acpi_fetch_acpi_dev(handle);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100932 struct acpi_power_resource *resource;
Len Brown4be44fc2005-08-05 00:44:28 -0400933 union acpi_object acpi_object;
934 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100935 acpi_status status;
Rafael J. Wysockia1224f32021-10-15 19:14:10 +0200936 u8 state_dummy;
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200937 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100939 if (device)
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200940 return device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100942 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!resource)
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200944 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100946 device = &resource->device;
Rafael J. Wysockic830dbc2021-04-07 16:33:38 +0200947 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500948 mutex_init(&resource->resource_lock);
Rafael J. Wysocki6ee22e9d2013-06-19 00:44:45 +0200949 INIT_LIST_HEAD(&resource->list_node);
Mika Westerberg45337712019-06-25 13:29:41 +0300950 INIT_LIST_HEAD(&resource->dependents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
952 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100953 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Tom Saeger935ab852021-03-12 18:55:35 -0700955 /* Evaluate the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100956 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
957 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100958 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 resource->system_level = acpi_object.power_resource.system_level;
961 resource->order = acpi_object.power_resource.resource_order;
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200962 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Rafael J. Wysockia1224f32021-10-15 19:14:10 +0200964 /* Get the initial state or just flip it on if that fails. */
965 if (acpi_power_get_state(resource, &state_dummy))
966 __acpi_power_on(resource);
967
Rafael J. Wysockica84f182021-05-24 17:25:23 +0200968 pr_info("%s [%s]\n", acpi_device_name(device), acpi_device_bid(device));
Len Brown4be44fc2005-08-05 00:44:28 -0400969
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100970 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100971 result = acpi_device_add(device, acpi_release_power_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100973 goto err;
Len Brown4be44fc2005-08-05 00:44:28 -0400974
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100975 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
976 device->remove = acpi_power_sysfs_remove;
977
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +0200978 acpi_power_add_resource_to_list(resource);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100979 acpi_device_add_finalize(device);
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200980 return device;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100981
982 err:
983 acpi_release_power_resource(&device->dev);
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +0200984 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100987#ifdef CONFIG_ACPI_SLEEP
988void acpi_resume_power_resources(void)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500989{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200990 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500991
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100992 mutex_lock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500993
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100994 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
Rafael J. Wysocki587024b2021-05-24 17:24:16 +0200995 int result;
996 u8 state;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200997
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100998 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500999
Rafael J. Wysockica84f182021-05-24 17:25:23 +02001000 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
1001 result = acpi_power_get_state(resource, &state);
Lan Tianyud7d49012013-10-15 19:48:11 +08001002 if (result) {
1003 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki660b1112013-01-25 21:51:57 +01001004 continue;
Lan Tianyud7d49012013-10-15 19:48:11 +08001005 }
Rafael J. Wysocki660b1112013-01-25 21:51:57 +01001006
1007 if (state == ACPI_POWER_RESOURCE_STATE_OFF
Rafael J. Wysocki781d7372013-01-17 14:11:06 +01001008 && resource->ref_count) {
Rafael J. Wysockifad40a62021-08-27 21:07:58 +02001009 acpi_handle_debug(resource->device.handle, "Turning ON\n");
Rafael J. Wysocki781d7372013-01-17 14:11:06 +01001010 __acpi_power_on(resource);
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +02001011 }
1012
1013 mutex_unlock(&resource->resource_lock);
1014 }
Hans de Goede8ece1d82017-04-30 22:54:16 +02001015
1016 mutex_unlock(&power_resource_list_lock);
1017}
Rafael J. Wysocki29038ae2021-05-10 14:02:17 +02001018#endif
Hans de Goede8ece1d82017-04-30 22:54:16 +02001019
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +02001020/**
1021 * acpi_turn_off_unused_power_resources - Turn off power resources not in use.
Rafael J. Wysocki9b7ff252021-05-21 15:13:11 +02001022 */
Rafael J. Wysocki63811952021-05-24 17:26:16 +02001023void acpi_turn_off_unused_power_resources(void)
Hans de Goede8ece1d82017-04-30 22:54:16 +02001024{
1025 struct acpi_power_resource *resource;
1026
1027 mutex_lock(&power_resource_list_lock);
1028
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +02001029 list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +02001030 mutex_lock(&resource->resource_lock);
1031
Rafael J. Wysocki63811952021-05-24 17:26:16 +02001032 if (!resource->ref_count &&
Rafael J. Wysockibc283682021-10-15 19:12:21 +02001033 resource->state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysockifad40a62021-08-27 21:07:58 +02001034 acpi_handle_debug(resource->device.handle, "Turning OFF\n");
Rafael J. Wysocki63811952021-05-24 17:26:16 +02001035 __acpi_power_off(resource);
1036 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -05001037
Rafael J. Wysocki781d7372013-01-17 14:11:06 +01001038 mutex_unlock(&resource->resource_lock);
1039 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -05001040
Rafael J. Wysocki781d7372013-01-17 14:11:06 +01001041 mutex_unlock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -05001042}