blob: 1600f753fafed4861fcb5d51fbe15df934d0baad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_power.c - ACPI Bus Power Management ($Revision: 39 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26/*
27 * ACPI power-managed devices may be controlled in two ways:
28 * 1. via "Device Specific (D-State) Control"
29 * 2. via "Power Resource Control".
30 * This module is used to manage devices relying on Power Resource Control.
31 *
32 * An ACPI "power resource object" describes a software controllable power
33 * plane, clock plane, or other resource used by a power managed device.
34 * A device may rely on multiple power resources, and a power resource
35 * may be shared by multiple devices.
36 */
37
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Lin Ming0090def2012-03-29 14:09:39 +080043#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020046#include "sleep.h"
Lin Ming0090def2012-03-29 14:09:39 +080047#include "internal.h"
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020048
Len Browna192a952009-07-28 16:45:54 -040049#define PREFIX "ACPI: "
50
Bjorn Helgaas89595b82008-11-07 16:57:45 -070051#define _COMPONENT ACPI_POWER_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050052ACPI_MODULE_NAME("power");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_POWER_CLASS "power_resource"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_POWER_DEVICE_NAME "Power Resource"
55#define ACPI_POWER_FILE_INFO "info"
56#define ACPI_POWER_FILE_STATUS "state"
57#define ACPI_POWER_RESOURCE_STATE_OFF 0x00
58#define ACPI_POWER_RESOURCE_STATE_ON 0x01
59#define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
Zhao Yakuif5adfaa2008-08-11 14:57:50 +080060
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010061struct acpi_power_dependent_device {
62 struct list_head node;
63 struct acpi_device *adev;
64 struct work_struct work;
Lin Ming0090def2012-03-29 14:09:39 +080065};
66
Len Brown4be44fc2005-08-05 00:44:28 -040067struct acpi_power_resource {
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010068 struct acpi_device device;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010069 struct list_head list_node;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010070 struct list_head dependent;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010071 char *name;
Len Brown4be44fc2005-08-05 00:44:28 -040072 u32 system_level;
73 u32 order;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +020074 unsigned int ref_count;
Konstantin Karasyov0a613902007-02-16 01:47:06 -050075 struct mutex resource_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010078struct acpi_power_resource_entry {
79 struct list_head node;
80 struct acpi_power_resource *resource;
81};
82
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010083static LIST_HEAD(acpi_power_resource_list);
84static DEFINE_MUTEX(power_resource_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* --------------------------------------------------------------------------
87 Power Resource Management
88 -------------------------------------------------------------------------- */
89
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010090static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010092 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010094 if (acpi_bus_get_device(handle, &device))
95 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010097 return container_of(device, struct acpi_power_resource, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100100static int acpi_power_resources_list_add(acpi_handle handle,
101 struct list_head *list)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100102{
103 struct acpi_power_resource *resource = acpi_power_get_context(handle);
104 struct acpi_power_resource_entry *entry;
105
106 if (!resource || !list)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100107 return -EINVAL;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100108
109 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
110 if (!entry)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100111 return -ENOMEM;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100112
113 entry->resource = resource;
114 if (!list_empty(list)) {
115 struct acpi_power_resource_entry *e;
116
117 list_for_each_entry(e, list, node)
118 if (e->resource->order > resource->order) {
119 list_add_tail(&entry->node, &e->node);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100120 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100121 }
122 }
123 list_add_tail(&entry->node, list);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100124 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100125}
126
127void acpi_power_resources_list_free(struct list_head *list)
128{
129 struct acpi_power_resource_entry *entry, *e;
130
131 list_for_each_entry_safe(entry, e, list, node) {
132 list_del(&entry->node);
133 kfree(entry);
134 }
135}
136
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100137int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
138 struct list_head *list)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100139{
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100140 unsigned int i;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100141 int err = 0;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100142
143 for (i = start; i < package->package.count; i++) {
144 union acpi_object *element = &package->package.elements[i];
145 acpi_handle rhandle;
146
147 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100148 err = -ENODATA;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100149 break;
150 }
151 rhandle = element->reference.handle;
152 if (!rhandle) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100153 err = -ENODEV;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100154 break;
155 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100156 err = acpi_add_power_resource(rhandle);
157 if (err)
158 break;
159
160 err = acpi_power_resources_list_add(rhandle, list);
161 if (err)
162 break;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100163 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100164 if (err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100165 acpi_power_resources_list_free(list);
166
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100167 return err;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100168}
169
Zhao Yakuia51e1452008-08-11 14:55:05 +0800170static int acpi_power_get_state(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Len Brown4be44fc2005-08-05 00:44:28 -0400172 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400173 unsigned long long sta = 0;
Lin Ming60a4ce72008-12-16 17:02:22 +0800174 char node_name[5];
175 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Zhao Yakuia51e1452008-08-11 14:55:05 +0800178 if (!handle || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Zhao Yakuia51e1452008-08-11 14:55:05 +0800181 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400183 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400185 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
186 ACPI_POWER_RESOURCE_STATE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Lin Ming60a4ce72008-12-16 17:02:22 +0800188 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Lin Ming60a4ce72008-12-16 17:02:22 +0800191 node_name,
Zhao Yakuib1b57fb2008-10-27 16:04:53 +0800192 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Patrick Mocheld550d982006-06-27 00:41:40 -0400194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100197static int acpi_power_get_list_state(struct list_head *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100199 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100200 int cur_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400203 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* The state of the list is 'on' IFF all resources are 'on'. */
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100206 list_for_each_entry(entry, list, node) {
207 struct acpi_power_resource *resource = entry->resource;
208 acpi_handle handle = resource->device.handle;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100209 int result;
210
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100211 mutex_lock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100212 result = acpi_power_get_state(handle, &cur_state);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100213 mutex_unlock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100214 if (result)
215 return result;
216
217 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
219 }
220
221 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100222 cur_state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100224 *state = cur_state;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100228static void acpi_power_resume_dependent(struct work_struct *work)
Lin Ming0090def2012-03-29 14:09:39 +0800229{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100230 struct acpi_power_dependent_device *dep;
231 struct acpi_device_physical_node *pn;
232 struct acpi_device *adev;
Lin Ming0090def2012-03-29 14:09:39 +0800233 int state;
234
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100235 dep = container_of(work, struct acpi_power_dependent_device, work);
236 adev = dep->adev;
237 if (acpi_power_get_inferred_state(adev, &state))
Lin Ming0090def2012-03-29 14:09:39 +0800238 return;
239
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100240 if (state > ACPI_STATE_D0)
Lin Ming0090def2012-03-29 14:09:39 +0800241 return;
242
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100243 mutex_lock(&adev->physical_node_lock);
244
245 list_for_each_entry(pn, &adev->physical_node_list, node)
246 pm_request_resume(pn->dev);
247
248 list_for_each_entry(pn, &adev->power_dependent, node)
249 pm_request_resume(pn->dev);
250
251 mutex_unlock(&adev->physical_node_lock);
Lin Ming0090def2012-03-29 14:09:39 +0800252}
253
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200254static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Len Brown4be44fc2005-08-05 00:44:28 -0400256 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500257
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100258 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400260 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200262 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400263 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200264
Patrick Mocheld550d982006-06-27 00:41:40 -0400265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100268static int acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100270 int result = 0;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500272 mutex_lock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200273
274 if (resource->ref_count++) {
275 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
276 "Power resource [%s] already on",
277 resource->name));
278 } else {
279 result = __acpi_power_on(resource);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100280 if (result) {
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100281 resource->ref_count--;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100282 } else {
283 struct acpi_power_dependent_device *dep;
284
285 list_for_each_entry(dep, &resource->dependent, node)
286 schedule_work(&dep->work);
287 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500290 mutex_unlock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100292 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100295static int acpi_power_off(struct acpi_power_resource *resource)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200296{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200297 acpi_status status = AE_OK;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100298 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200299
300 mutex_lock(&resource->resource_lock);
301
302 if (!resource->ref_count) {
303 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
304 "Power resource [%s] already off",
305 resource->name));
306 goto unlock;
307 }
308
309 if (--resource->ref_count) {
310 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
311 "Power resource [%s] still in use\n",
312 resource->name));
313 goto unlock;
314 }
315
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100316 status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100317 if (ACPI_FAILURE(status))
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200318 result = -ENODEV;
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100319 else
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200320 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
321 "Power resource [%s] turned off\n",
322 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200323
324 unlock:
325 mutex_unlock(&resource->resource_lock);
326
327 return result;
328}
329
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100330static int acpi_power_off_list(struct list_head *list)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100331{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100332 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100333 int result = 0;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100334
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100335 list_for_each_entry_reverse(entry, list, node) {
336 result = acpi_power_off(entry->resource);
337 if (result)
338 goto err;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100339 }
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100340 return 0;
341
342 err:
343 list_for_each_entry_continue(entry, list, node)
344 acpi_power_on(entry->resource);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100345
346 return result;
347}
348
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100349static int acpi_power_on_list(struct list_head *list)
350{
351 struct acpi_power_resource_entry *entry;
352 int result = 0;
353
354 list_for_each_entry(entry, list, node) {
355 result = acpi_power_on(entry->resource);
356 if (result)
357 goto err;
358 }
359 return 0;
360
361 err:
362 list_for_each_entry_continue_reverse(entry, list, node)
363 acpi_power_off(entry->resource);
364
365 return result;
366}
367
368static void acpi_power_add_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100369 struct acpi_device *adev)
Lin Ming0090def2012-03-29 14:09:39 +0800370{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100371 struct acpi_power_dependent_device *dep;
Lin Ming0090def2012-03-29 14:09:39 +0800372
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100373 mutex_lock(&resource->resource_lock);
374
375 list_for_each_entry(dep, &resource->dependent, node)
376 if (dep->adev == adev)
377 goto out;
378
379 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
380 if (!dep)
381 goto out;
382
383 dep->adev = adev;
384 INIT_WORK(&dep->work, acpi_power_resume_dependent);
385 list_add_tail(&dep->node, &resource->dependent);
386
387 out:
388 mutex_unlock(&resource->resource_lock);
389}
390
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100391static void acpi_power_remove_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100392 struct acpi_device *adev)
393{
394 struct acpi_power_dependent_device *dep;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100395 struct work_struct *work = NULL;
396
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100397 mutex_lock(&resource->resource_lock);
398
399 list_for_each_entry(dep, &resource->dependent, node)
400 if (dep->adev == adev) {
401 list_del(&dep->node);
402 work = &dep->work;
403 break;
404 }
405
406 mutex_unlock(&resource->resource_lock);
407
408 if (work) {
409 cancel_work_sync(work);
410 kfree(dep);
411 }
412}
413
414void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
415{
416 if (adev->power.flags.power_resources) {
417 struct acpi_device_power_state *ps;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100418 struct acpi_power_resource_entry *entry;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100419
420 ps = &adev->power.states[ACPI_STATE_D0];
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100421 list_for_each_entry(entry, &ps->resources, node) {
422 struct acpi_power_resource *resource = entry->resource;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100423
424 if (add)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100425 acpi_power_add_dependent(resource, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800426 else
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100427 acpi_power_remove_dependent(resource, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800428 }
429 }
Lin Ming0090def2012-03-29 14:09:39 +0800430}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100431
432/* --------------------------------------------------------------------------
433 Device Power Management
434 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800435
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200436/**
437 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
438 * ACPI 3.0) _PSW (Power State Wake)
439 * @dev: Device to handle.
440 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
441 * @sleep_state: Target sleep state of the system.
442 * @dev_state: Target power state of the device.
443 *
444 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
445 * State Wake) for the device, if present. On failure reset the device's
446 * wakeup.flags.valid flag.
447 *
448 * RETURN VALUE:
449 * 0 if either _DSW or _PSW has been successfully executed
450 * 0 if neither _DSW nor _PSW has been found
451 * -ENODEV if the execution of either _DSW or _PSW has failed
452 */
453int acpi_device_sleep_wake(struct acpi_device *dev,
454 int enable, int sleep_state, int dev_state)
455{
456 union acpi_object in_arg[3];
457 struct acpi_object_list arg_list = { 3, in_arg };
458 acpi_status status = AE_OK;
459
460 /*
461 * Try to execute _DSW first.
462 *
463 * Three agruments are needed for the _DSW object:
464 * Argument 0: enable/disable the wake capabilities
465 * Argument 1: target system state
466 * Argument 2: target device state
467 * When _DSW object is called to disable the wake capabilities, maybe
468 * the first argument is filled. The values of the other two agruments
469 * are meaningless.
470 */
471 in_arg[0].type = ACPI_TYPE_INTEGER;
472 in_arg[0].integer.value = enable;
473 in_arg[1].type = ACPI_TYPE_INTEGER;
474 in_arg[1].integer.value = sleep_state;
475 in_arg[2].type = ACPI_TYPE_INTEGER;
476 in_arg[2].integer.value = dev_state;
477 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
478 if (ACPI_SUCCESS(status)) {
479 return 0;
480 } else if (status != AE_NOT_FOUND) {
481 printk(KERN_ERR PREFIX "_DSW execution failed\n");
482 dev->wakeup.flags.valid = 0;
483 return -ENODEV;
484 }
485
486 /* Execute _PSW */
487 arg_list.count = 1;
488 in_arg[0].integer.value = enable;
489 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
490 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
491 printk(KERN_ERR PREFIX "_PSW execution failed\n");
492 dev->wakeup.flags.valid = 0;
493 return -ENODEV;
494 }
495
496 return 0;
497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/*
500 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
501 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200502 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
503 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200505int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100507 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200510 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200512 mutex_lock(&acpi_device_lock);
513
514 if (dev->wakeup.prepare_count++)
515 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200516
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100517 err = acpi_power_on_list(&dev->wakeup.resources);
518 if (err) {
519 dev_err(&dev->dev, "Cannot turn wakeup power resources on\n");
520 dev->wakeup.flags.valid = 0;
521 } else {
522 /*
523 * Passing 3 as the third argument below means the device may be
524 * put into arbitrary power state afterward.
525 */
526 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200528 if (err)
529 dev->wakeup.prepare_count = 0;
530
531 out:
532 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200533 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
537 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200538 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
539 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * 2. Shutdown down the power resources
541 */
Len Brown4be44fc2005-08-05 00:44:28 -0400542int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100544 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200547 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200549 mutex_lock(&acpi_device_lock);
550
551 if (--dev->wakeup.prepare_count > 0)
552 goto out;
553
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200554 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200555 * Executing the code below even if prepare_count is already zero when
556 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200557 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200558 if (dev->wakeup.prepare_count < 0)
559 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200560
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200561 err = acpi_device_sleep_wake(dev, 0, 0, 0);
562 if (err)
563 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100565 err = acpi_power_off_list(&dev->wakeup.resources);
566 if (err) {
567 dev_err(&dev->dev, "Cannot turn wakeup power resources off\n");
568 dev->wakeup.flags.valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200571 out:
572 mutex_unlock(&acpi_device_lock);
573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100576int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Len Brown4be44fc2005-08-05 00:44:28 -0400578 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400579 int list_state = 0;
580 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100582 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400583 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /*
586 * We know a device's inferred power state when all the resources
587 * required for a given D-state are 'on'.
588 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200589 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100590 struct list_head *list = &device->power.states[i].resources;
591
592 if (list_empty(list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 continue;
594
595 result = acpi_power_get_list_state(list, &list_state);
596 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400597 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100600 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 }
604
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100605 *state = ACPI_STATE_D3;
Patrick Mocheld550d982006-06-27 00:41:40 -0400606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100609int acpi_power_on_resources(struct acpi_device *device, int state)
610{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100611 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_COLD)
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100612 return -EINVAL;
613
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100614 if (state == ACPI_STATE_D3_COLD)
615 return 0;
616
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100617 return acpi_power_on_list(&device->power.states[state].resources);
618}
619
Len Brown4be44fc2005-08-05 00:44:28 -0400620int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200622 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800624 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400625 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100627 if (device->power.state == state || !device->flags.power_manageable)
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100628 return 0;
629
Len Brown4be44fc2005-08-05 00:44:28 -0400630 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800631 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400632 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* TBD: Resources must be ordered. */
635
636 /*
637 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100638 * (e.g. so the device doesn't lose power while transitioning). Then,
639 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200641 if (state < ACPI_STATE_D3_COLD)
642 result = acpi_power_on_list(
643 &device->power.states[state].resources);
644
645 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100646 acpi_power_off_list(
647 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100649 /* We shouldn't change the state unless the above operations succeed. */
650 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Patrick Mocheld550d982006-06-27 00:41:40 -0400652 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100655static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100657 struct acpi_device *device = to_acpi_device(dev);
658 struct acpi_power_resource *resource;
659
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100660 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100661
662 mutex_lock(&power_resource_list_lock);
663 list_del(&resource->list_node);
664 mutex_unlock(&power_resource_list_lock);
665
666 acpi_free_ids(device);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100667 kfree(resource);
668}
669
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100670int acpi_add_power_resource(acpi_handle handle)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100671{
672 struct acpi_power_resource *resource;
673 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400674 union acpi_object acpi_object;
675 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100676 acpi_status status;
677 int state, result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100679 acpi_bus_get_device(handle, &device);
680 if (device)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100683 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (!resource)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100685 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100687 device = &resource->device;
688 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
689 ACPI_STA_DEFAULT);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500690 mutex_init(&resource->resource_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100691 INIT_LIST_HEAD(&resource->dependent);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100692 resource->name = device->pnp.bus_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
694 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100695 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /* Evalute the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100698 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
699 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100700 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 resource->system_level = acpi_object.power_resource.system_level;
703 resource->order = acpi_object.power_resource.resource_order;
704
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100705 result = acpi_power_get_state(handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100707 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Len Brown4be44fc2005-08-05 00:44:28 -0400709 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400710 acpi_device_bid(device), state ? "on" : "off");
Len Brown4be44fc2005-08-05 00:44:28 -0400711
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100712 device->flags.match_driver = true;
713 result = acpi_device_register(device, acpi_release_power_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100715 goto err;
Len Brown4be44fc2005-08-05 00:44:28 -0400716
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100717 mutex_lock(&power_resource_list_lock);
718 list_add(&resource->list_node, &acpi_power_resource_list);
719 mutex_unlock(&power_resource_list_lock);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100720 return 0;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100721
722 err:
723 acpi_release_power_resource(&device->dev);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100724 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100727#ifdef CONFIG_ACPI_SLEEP
728void acpi_resume_power_resources(void)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500729{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200730 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500731
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100732 mutex_lock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500733
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100734 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
735 int result, state;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200736
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100737 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500738
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100739 result = acpi_power_get_state(resource->device.handle, &state);
740 if (!result && state == ACPI_POWER_RESOURCE_STATE_OFF
741 && resource->ref_count) {
742 dev_info(&resource->device.dev, "Turning ON\n");
743 __acpi_power_on(resource);
744 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500745
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100746 mutex_unlock(&resource->resource_lock);
747 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500748
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100749 mutex_unlock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500750}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200751#endif