]> www.infradead.org Git - linux.git/blob
4.1.4-00834-gcd9380b
[linux.git] /
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * drivers/acpi/power.c - ACPI Power Resources management.
4  *
5  * 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>
9  */
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".
15  * The code below deals with ACPI Power Resources control.
16  *
17  * An ACPI "power resource object" represents a software controllable power
18  * plane, clock plane, or other resource depended on by a device.
19  *
20  * A device may rely on multiple power resources, and a power resource
21  * may be shared by multiple devices.
22  */
23
24 #define pr_fmt(fmt) "ACPI: PM: " fmt
25
26 #include <linux/dmi.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/string_choices.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/sysfs.h>
35 #include <linux/acpi.h>
36 #include "sleep.h"
37 #include "internal.h"
38
39 #define ACPI_POWER_CLASS                "power_resource"
40 #define ACPI_POWER_DEVICE_NAME          "Power Resource"
41 #define ACPI_POWER_RESOURCE_STATE_OFF   0x00
42 #define ACPI_POWER_RESOURCE_STATE_ON    0x01
43 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
44
45 struct acpi_power_dependent_device {
46         struct device *dev;
47         struct list_head node;
48 };
49
50 struct acpi_power_resource {
51         struct acpi_device device;
52         struct list_head list_node;
53         u32 system_level;
54         u32 order;
55         unsigned int ref_count;
56         u8 state;
57         struct mutex resource_lock;
58         struct list_head dependents;
59 };
60
61 struct acpi_power_resource_entry {
62         struct list_head node;
63         struct acpi_power_resource *resource;
64 };
65
66 static bool unused_power_resources_quirk;
67
68 static LIST_HEAD(acpi_power_resource_list);
69 static DEFINE_MUTEX(power_resource_list_lock);
70
71 /* --------------------------------------------------------------------------
72                              Power Resource Management
73    -------------------------------------------------------------------------- */
74
75 static inline const char *resource_dev_name(struct acpi_power_resource *pr)
76 {
77         return dev_name(&pr->device.dev);
78 }
79
80 static inline
81 struct acpi_power_resource *to_power_resource(struct acpi_device *device)
82 {
83         return container_of(device, struct acpi_power_resource, device);
84 }
85
86 static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
87 {
88         struct acpi_device *device = acpi_fetch_acpi_dev(handle);
89
90         if (!device)
91                 return NULL;
92
93         return to_power_resource(device);
94 }
95
96 static int acpi_power_resources_list_add(acpi_handle handle,
97                                          struct list_head *list)
98 {
99         struct acpi_power_resource *resource = acpi_power_get_context(handle);
100         struct acpi_power_resource_entry *entry;
101
102         if (!resource || !list)
103                 return -EINVAL;
104
105         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
106         if (!entry)
107                 return -ENOMEM;
108
109         entry->resource = resource;
110         if (!list_empty(list)) {
111                 struct acpi_power_resource_entry *e;
112
113                 list_for_each_entry(e, list, node)
114                         if (e->resource->order > resource->order) {
115                                 list_add_tail(&entry->node, &e->node);
116                                 return 0;
117                         }
118         }
119         list_add_tail(&entry->node, list);
120         return 0;
121 }
122
123 void acpi_power_resources_list_free(struct list_head *list)
124 {
125         struct acpi_power_resource_entry *entry, *e;
126
127         list_for_each_entry_safe(entry, e, list, node) {
128                 list_del(&entry->node);
129                 kfree(entry);
130         }
131 }
132
133 static bool acpi_power_resource_is_dup(union acpi_object *package,
134                                        unsigned int start, unsigned int i)
135 {
136         acpi_handle rhandle, dup;
137         unsigned int j;
138
139         /* The caller is expected to check the package element types */
140         rhandle = package->package.elements[i].reference.handle;
141         for (j = start; j < i; j++) {
142                 dup = package->package.elements[j].reference.handle;
143                 if (dup == rhandle)
144                         return true;
145         }
146
147         return false;
148 }
149
150 int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
151                                  struct list_head *list)
152 {
153         unsigned int i;
154         int err = 0;
155
156         for (i = start; i < package->package.count; i++) {
157                 union acpi_object *element = &package->package.elements[i];
158                 struct acpi_device *rdev;
159                 acpi_handle rhandle;
160
161                 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
162                         err = -ENODATA;
163                         break;
164                 }
165                 rhandle = element->reference.handle;
166                 if (!rhandle) {
167                         err = -ENODEV;
168                         break;
169                 }
170
171                 /* Some ACPI tables contain duplicate power resource references */
172                 if (acpi_power_resource_is_dup(package, start, i))
173                         continue;
174
175                 rdev = acpi_add_power_resource(rhandle);
176                 if (!rdev) {
177                         err = -ENODEV;
178                         break;
179                 }
180                 err = acpi_power_resources_list_add(rhandle, list);
181                 if (err)
182                         break;
183         }
184         if (err)
185                 acpi_power_resources_list_free(list);
186
187         return err;
188 }
189
190 static int __get_state(acpi_handle handle, u8 *state)
191 {
192         acpi_status status = AE_OK;
193         unsigned long long sta = 0;
194         u8 cur_state;
195
196         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
197         if (ACPI_FAILURE(status))
198                 return -ENODEV;
199
200         cur_state = sta & ACPI_POWER_RESOURCE_STATE_ON;
201
202         acpi_handle_debug(handle, "Power resource is %s\n",
203                           str_on_off(cur_state));
204
205         *state = cur_state;
206         return 0;
207 }
208
209 static int acpi_power_get_state(struct acpi_power_resource *resource, u8 *state)
210 {
211         if (resource->state == ACPI_POWER_RESOURCE_STATE_UNKNOWN) {
212                 int ret;
213
214                 ret = __get_state(resource->device.handle, &resource->state);
215                 if (ret)
216                         return ret;
217         }
218
219         *state = resource->state;
220         return 0;
221 }
222
223 static int acpi_power_get_list_state(struct list_head *list, u8 *state)
224 {
225         struct acpi_power_resource_entry *entry;
226         u8 cur_state = ACPI_POWER_RESOURCE_STATE_OFF;
227
228         if (!list || !state)
229                 return -EINVAL;
230
231         /* The state of the list is 'on' IFF all resources are 'on'. */
232         list_for_each_entry(entry, list, node) {
233                 struct acpi_power_resource *resource = entry->resource;
234                 int result;
235
236                 mutex_lock(&resource->resource_lock);
237                 result = acpi_power_get_state(resource, &cur_state);
238                 mutex_unlock(&resource->resource_lock);
239                 if (result)
240                         return result;
241
242                 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
243                         break;
244         }
245
246         pr_debug("Power resource list is %s\n", str_on_off(cur_state));
247
248         *state = cur_state;
249         return 0;
250 }
251
252 static int
253 acpi_power_resource_add_dependent(struct acpi_power_resource *resource,
254                                   struct device *dev)
255 {
256         struct acpi_power_dependent_device *dep;
257         int ret = 0;
258
259         mutex_lock(&resource->resource_lock);
260         list_for_each_entry(dep, &resource->dependents, node) {
261                 /* Only add it once */
262                 if (dep->dev == dev)
263                         goto unlock;
264         }
265
266         dep = kzalloc(sizeof(*dep), GFP_KERNEL);
267         if (!dep) {
268                 ret = -ENOMEM;
269                 goto unlock;
270         }
271
272         dep->dev = dev;
273         list_add_tail(&dep->node, &resource->dependents);
274         dev_dbg(dev, "added power dependency to [%s]\n",
275                 resource_dev_name(resource));
276
277 unlock:
278         mutex_unlock(&resource->resource_lock);
279         return ret;
280 }
281
282 static void
283 acpi_power_resource_remove_dependent(struct acpi_power_resource *resource,
284                                      struct device *dev)
285 {
286         struct acpi_power_dependent_device *dep;
287
288         mutex_lock(&resource->resource_lock);
289         list_for_each_entry(dep, &resource->dependents, node) {
290                 if (dep->dev == dev) {
291                         list_del(&dep->node);
292                         kfree(dep);
293                         dev_dbg(dev, "removed power dependency to [%s]\n",
294                                 resource_dev_name(resource));
295                         break;
296                 }
297         }
298         mutex_unlock(&resource->resource_lock);
299 }
300
301 /**
302  * acpi_device_power_add_dependent - Add dependent device of this ACPI device
303  * @adev: ACPI device pointer
304  * @dev: Dependent device
305  *
306  * If @adev has non-empty _PR0 the @dev is added as dependent device to all
307  * power resources returned by it. This means that whenever these power
308  * resources are turned _ON the dependent devices get runtime resumed. This
309  * is needed for devices such as PCI to allow its driver to re-initialize
310  * it after it went to D0uninitialized.
311  *
312  * If @adev does not have _PR0 this does nothing.
313  *
314  * Returns %0 in case of success and negative errno otherwise.
315  */
316 int acpi_device_power_add_dependent(struct acpi_device *adev,
317                                     struct device *dev)
318 {
319         struct acpi_power_resource_entry *entry;
320         struct list_head *resources;
321         int ret;
322
323         if (!adev->flags.power_manageable)
324                 return 0;
325
326         resources = &adev->power.states[ACPI_STATE_D0].resources;
327         list_for_each_entry(entry, resources, node) {
328                 ret = acpi_power_resource_add_dependent(entry->resource, dev);
329                 if (ret)
330                         goto err;
331         }
332
333         return 0;
334
335 err:
336         list_for_each_entry(entry, resources, node)
337                 acpi_power_resource_remove_dependent(entry->resource, dev);
338
339         return ret;
340 }
341
342 /**
343  * acpi_device_power_remove_dependent - Remove dependent device
344  * @adev: ACPI device pointer
345  * @dev: Dependent device
346  *
347  * Does the opposite of acpi_device_power_add_dependent() and removes the
348  * dependent device if it is found. Can be called to @adev that does not
349  * have _PR0 as well.
350  */
351 void acpi_device_power_remove_dependent(struct acpi_device *adev,
352                                         struct device *dev)
353 {
354         struct acpi_power_resource_entry *entry;
355         struct list_head *resources;
356
357         if (!adev->flags.power_manageable)
358                 return;
359
360         resources = &adev->power.states[ACPI_STATE_D0].resources;
361         list_for_each_entry_reverse(entry, resources, node)
362                 acpi_power_resource_remove_dependent(entry->resource, dev);
363 }
364
365 static int __acpi_power_on(struct acpi_power_resource *resource)
366 {
367         acpi_handle handle = resource->device.handle;
368         struct acpi_power_dependent_device *dep;
369         acpi_status status = AE_OK;
370
371         status = acpi_evaluate_object(handle, "_ON", NULL, NULL);
372         if (ACPI_FAILURE(status)) {
373                 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
374                 return -ENODEV;
375         }
376
377         resource->state = ACPI_POWER_RESOURCE_STATE_ON;
378
379         acpi_handle_debug(handle, "Power resource turned on\n");
380
381         /*
382          * If there are other dependents on this power resource we need to
383          * resume them now so that their drivers can re-initialize the
384          * hardware properly after it went back to D0.
385          */
386         if (list_empty(&resource->dependents) ||
387             list_is_singular(&resource->dependents))
388                 return 0;
389
390         list_for_each_entry(dep, &resource->dependents, node) {
391                 dev_dbg(dep->dev, "runtime resuming because [%s] turned on\n",
392                         resource_dev_name(resource));
393                 pm_request_resume(dep->dev);
394         }
395
396         return 0;
397 }
398
399 static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
400 {
401         int result = 0;
402
403         if (resource->ref_count++) {
404                 acpi_handle_debug(resource->device.handle,
405                                   "Power resource already on\n");
406         } else {
407                 result = __acpi_power_on(resource);
408                 if (result)
409                         resource->ref_count--;
410         }
411         return result;
412 }
413
414 static int acpi_power_on(struct acpi_power_resource *resource)
415 {
416         int result;
417
418         mutex_lock(&resource->resource_lock);
419         result = acpi_power_on_unlocked(resource);
420         mutex_unlock(&resource->resource_lock);
421         return result;
422 }
423
424 static int __acpi_power_off(struct acpi_power_resource *resource)
425 {
426         acpi_handle handle = resource->device.handle;
427         acpi_status status;
428
429         status = acpi_evaluate_object(handle, "_OFF", NULL, NULL);
430         if (ACPI_FAILURE(status)) {
431                 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
432                 return -ENODEV;
433         }
434
435         resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
436
437         acpi_handle_debug(handle, "Power resource turned off\n");
438
439         return 0;
440 }
441
442 static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
443 {
444         int result = 0;
445
446         if (!resource->ref_count) {
447                 acpi_handle_debug(resource->device.handle,
448                                   "Power resource already off\n");
449                 return 0;
450         }
451
452         if (--resource->ref_count) {
453                 acpi_handle_debug(resource->device.handle,
454                                   "Power resource still in use\n");
455         } else {
456                 result = __acpi_power_off(resource);
457                 if (result)
458                         resource->ref_count++;
459         }
460         return result;
461 }
462
463 static int acpi_power_off(struct acpi_power_resource *resource)
464 {
465         int result;
466
467         mutex_lock(&resource->resource_lock);
468         result = acpi_power_off_unlocked(resource);
469         mutex_unlock(&resource->resource_lock);
470         return result;
471 }
472
473 static int acpi_power_off_list(struct list_head *list)
474 {
475         struct acpi_power_resource_entry *entry;
476         int result = 0;
477
478         list_for_each_entry_reverse(entry, list, node) {
479                 result = acpi_power_off(entry->resource);
480                 if (result)
481                         goto err;
482         }
483         return 0;
484
485  err:
486         list_for_each_entry_continue(entry, list, node)
487                 acpi_power_on(entry->resource);
488
489         return result;
490 }
491
492 static int acpi_power_on_list(struct list_head *list)
493 {
494         struct acpi_power_resource_entry *entry;
495         int result = 0;
496
497         list_for_each_entry(entry, list, node) {
498                 result = acpi_power_on(entry->resource);
499                 if (result)
500                         goto err;
501         }
502         return 0;
503
504  err:
505         list_for_each_entry_continue_reverse(entry, list, node)
506                 acpi_power_off(entry->resource);
507
508         return result;
509 }
510
511 static struct attribute *attrs[] = {
512         NULL,
513 };
514
515 static const struct attribute_group attr_groups[] = {
516         [ACPI_STATE_D0] = {
517                 .name = "power_resources_D0",
518                 .attrs = attrs,
519         },
520         [ACPI_STATE_D1] = {
521                 .name = "power_resources_D1",
522                 .attrs = attrs,
523         },
524         [ACPI_STATE_D2] = {
525                 .name = "power_resources_D2",
526                 .attrs = attrs,
527         },
528         [ACPI_STATE_D3_HOT] = {
529                 .name = "power_resources_D3hot",
530                 .attrs = attrs,
531         },
532 };
533
534 static const struct attribute_group wakeup_attr_group = {
535         .name = "power_resources_wakeup",
536         .attrs = attrs,
537 };
538
539 static void acpi_power_hide_list(struct acpi_device *adev,
540                                  struct list_head *resources,
541                                  const struct attribute_group *attr_group)
542 {
543         struct acpi_power_resource_entry *entry;
544
545         if (list_empty(resources))
546                 return;
547
548         list_for_each_entry_reverse(entry, resources, node) {
549                 struct acpi_device *res_dev = &entry->resource->device;
550
551                 sysfs_remove_link_from_group(&adev->dev.kobj,
552                                              attr_group->name,
553                                              dev_name(&res_dev->dev));
554         }
555         sysfs_remove_group(&adev->dev.kobj, attr_group);
556 }
557
558 static void acpi_power_expose_list(struct acpi_device *adev,
559                                    struct list_head *resources,
560                                    const struct attribute_group *attr_group)
561 {
562         struct acpi_power_resource_entry *entry;
563         int ret;
564
565         if (list_empty(resources))
566                 return;
567
568         ret = sysfs_create_group(&adev->dev.kobj, attr_group);
569         if (ret)
570                 return;
571
572         list_for_each_entry(entry, resources, node) {
573                 struct acpi_device *res_dev = &entry->resource->device;
574
575                 ret = sysfs_add_link_to_group(&adev->dev.kobj,
576                                               attr_group->name,
577                                               &res_dev->dev.kobj,
578                                               dev_name(&res_dev->dev));
579                 if (ret) {
580                         acpi_power_hide_list(adev, resources, attr_group);
581                         break;
582                 }
583         }
584 }
585
586 static void acpi_power_expose_hide(struct acpi_device *adev,
587                                    struct list_head *resources,
588                                    const struct attribute_group *attr_group,
589                                    bool expose)
590 {
591         if (expose)
592                 acpi_power_expose_list(adev, resources, attr_group);
593         else
594                 acpi_power_hide_list(adev, resources, attr_group);
595 }
596
597 void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
598 {
599         int state;
600
601         if (adev->wakeup.flags.valid)
602                 acpi_power_expose_hide(adev, &adev->wakeup.resources,
603                                        &wakeup_attr_group, add);
604
605         if (!adev->power.flags.power_resources)
606                 return;
607
608         for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
609                 acpi_power_expose_hide(adev,
610                                        &adev->power.states[state].resources,
611                                        &attr_groups[state], add);
612 }
613
614 int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
615 {
616         struct acpi_power_resource_entry *entry;
617         int system_level = 5;
618
619         list_for_each_entry(entry, list, node) {
620                 struct acpi_power_resource *resource = entry->resource;
621                 u8 state;
622
623                 mutex_lock(&resource->resource_lock);
624
625                 /*
626                  * Make sure that the power resource state and its reference
627                  * counter value are consistent with each other.
628                  */
629                 if (!resource->ref_count &&
630                     !acpi_power_get_state(resource, &state) &&
631                     state == ACPI_POWER_RESOURCE_STATE_ON)
632                         __acpi_power_off(resource);
633
634                 if (system_level > resource->system_level)
635                         system_level = resource->system_level;
636
637                 mutex_unlock(&resource->resource_lock);
638         }
639         *system_level_p = system_level;
640         return 0;
641 }
642
643 /* --------------------------------------------------------------------------
644                              Device Power Management
645    -------------------------------------------------------------------------- */
646
647 /**
648  * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
649  *                          ACPI 3.0) _PSW (Power State Wake)
650  * @dev: Device to handle.
651  * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
652  * @sleep_state: Target sleep state of the system.
653  * @dev_state: Target power state of the device.
654  *
655  * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
656  * State Wake) for the device, if present.  On failure reset the device's
657  * wakeup.flags.valid flag.
658  *
659  * RETURN VALUE:
660  * 0 if either _DSW or _PSW has been successfully executed
661  * 0 if neither _DSW nor _PSW has been found
662  * -ENODEV if the execution of either _DSW or _PSW has failed
663  */
664 int acpi_device_sleep_wake(struct acpi_device *dev,
665                            int enable, int sleep_state, int dev_state)
666 {
667         union acpi_object in_arg[3];
668         struct acpi_object_list arg_list = { 3, in_arg };
669         acpi_status status = AE_OK;
670
671         /*
672          * Try to execute _DSW first.
673          *
674          * Three arguments are needed for the _DSW object:
675          * Argument 0: enable/disable the wake capabilities
676          * Argument 1: target system state
677          * Argument 2: target device state
678          * When _DSW object is called to disable the wake capabilities, maybe
679          * the first argument is filled. The values of the other two arguments
680          * are meaningless.
681          */
682         in_arg[0].type = ACPI_TYPE_INTEGER;
683         in_arg[0].integer.value = enable;
684         in_arg[1].type = ACPI_TYPE_INTEGER;
685         in_arg[1].integer.value = sleep_state;
686         in_arg[2].type = ACPI_TYPE_INTEGER;
687         in_arg[2].integer.value = dev_state;
688         status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
689         if (ACPI_SUCCESS(status)) {
690                 return 0;
691         } else if (status != AE_NOT_FOUND) {
692                 acpi_handle_info(dev->handle, "_DSW execution failed\n");
693                 dev->wakeup.flags.valid = 0;
694                 return -ENODEV;
695         }
696
697         /* Execute _PSW */
698         status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
699         if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
700                 acpi_handle_info(dev->handle, "_PSW execution failed\n");
701                 dev->wakeup.flags.valid = 0;
702                 return -ENODEV;
703         }
704
705         return 0;
706 }
707
708 /*
709  * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
710  * 1. Power on the power resources required for the wakeup device
711  * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
712  *    State Wake) for the device, if present
713  */
714 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
715 {
716         int err = 0;
717
718         if (!dev || !dev->wakeup.flags.valid)
719                 return -EINVAL;
720
721         mutex_lock(&acpi_device_lock);
722
723         dev_dbg(&dev->dev, "Enabling wakeup power (count %d)\n",
724                 dev->wakeup.prepare_count);
725
726         if (dev->wakeup.prepare_count++)
727                 goto out;
728
729         err = acpi_power_on_list(&dev->wakeup.resources);
730         if (err) {
731                 dev_err(&dev->dev, "Cannot turn on wakeup power resources\n");
732                 dev->wakeup.flags.valid = 0;
733                 goto out;
734         }
735
736         /*
737          * Passing 3 as the third argument below means the device may be
738          * put into arbitrary power state afterward.
739          */
740         err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
741         if (err) {
742                 acpi_power_off_list(&dev->wakeup.resources);
743                 dev->wakeup.prepare_count = 0;
744                 goto out;
745         }
746
747         dev_dbg(&dev->dev, "Wakeup power enabled\n");
748
749  out:
750         mutex_unlock(&acpi_device_lock);
751         return err;
752 }
753
754 /*
755  * Shutdown a wakeup device, counterpart of above method
756  * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
757  *    State Wake) for the device, if present
758  * 2. Shutdown down the power resources
759  */
760 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
761 {
762         struct acpi_power_resource_entry *entry;
763         int err = 0;
764
765         if (!dev || !dev->wakeup.flags.valid)
766                 return -EINVAL;
767
768         mutex_lock(&acpi_device_lock);
769
770         dev_dbg(&dev->dev, "Disabling wakeup power (count %d)\n",
771                 dev->wakeup.prepare_count);
772
773         /* Do nothing if wakeup power has not been enabled for this device. */
774         if (dev->wakeup.prepare_count <= 0)
775                 goto out;
776
777         if (--dev->wakeup.prepare_count > 0)
778                 goto out;
779
780         err = acpi_device_sleep_wake(dev, 0, 0, 0);
781         if (err)
782                 goto out;
783
784         /*
785          * All of the power resources in the list need to be turned off even if
786          * there are errors.
787          */
788         list_for_each_entry(entry, &dev->wakeup.resources, node) {
789                 int ret;
790
791                 ret = acpi_power_off(entry->resource);
792                 if (ret && !err)
793                         err = ret;
794         }
795         if (err) {
796                 dev_err(&dev->dev, "Cannot turn off wakeup power resources\n");
797                 dev->wakeup.flags.valid = 0;
798                 goto out;
799         }
800
801         dev_dbg(&dev->dev, "Wakeup power disabled\n");
802
803  out:
804         mutex_unlock(&acpi_device_lock);
805         return err;
806 }
807
808 int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
809 {
810         u8 list_state = ACPI_POWER_RESOURCE_STATE_OFF;
811         int result = 0;
812         int i = 0;
813
814         if (!device || !state)
815                 return -EINVAL;
816
817         /*
818          * We know a device's inferred power state when all the resources
819          * required for a given D-state are 'on'.
820          */
821         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
822                 struct list_head *list = &device->power.states[i].resources;
823
824                 if (list_empty(list))
825                         continue;
826
827                 result = acpi_power_get_list_state(list, &list_state);
828                 if (result)
829                         return result;
830
831                 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
832                         *state = i;
833                         return 0;
834                 }
835         }
836
837         *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
838                 ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
839         return 0;
840 }
841
842 int acpi_power_on_resources(struct acpi_device *device, int state)
843 {
844         if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
845                 return -EINVAL;
846
847         return acpi_power_on_list(&device->power.states[state].resources);
848 }
849
850 int acpi_power_transition(struct acpi_device *device, int state)
851 {
852         int result = 0;
853
854         if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
855                 return -EINVAL;
856
857         if (device->power.state == state || !device->flags.power_manageable)
858                 return 0;
859
860         if ((device->power.state < ACPI_STATE_D0)
861             || (device->power.state > ACPI_STATE_D3_COLD))
862                 return -ENODEV;
863
864         /*
865          * First we reference all power resources required in the target list
866          * (e.g. so the device doesn't lose power while transitioning).  Then,
867          * we dereference all power resources used in the current list.
868          */
869         if (state < ACPI_STATE_D3_COLD)
870                 result = acpi_power_on_list(
871                         &device->power.states[state].resources);
872
873         if (!result && device->power.state < ACPI_STATE_D3_COLD)
874                 acpi_power_off_list(
875                         &device->power.states[device->power.state].resources);
876
877         /* We shouldn't change the state unless the above operations succeed. */
878         device->power.state = result ? ACPI_STATE_UNKNOWN : state;
879
880         return result;
881 }
882
883 static void acpi_release_power_resource(struct device *dev)
884 {
885         struct acpi_device *device = to_acpi_device(dev);
886         struct acpi_power_resource *resource;
887
888         resource = container_of(device, struct acpi_power_resource, device);
889
890         mutex_lock(&power_resource_list_lock);
891         list_del(&resource->list_node);
892         mutex_unlock(&power_resource_list_lock);
893
894         acpi_free_pnp_ids(&device->pnp);
895         kfree(resource);
896 }
897
898 static ssize_t resource_in_use_show(struct device *dev,
899                                     struct device_attribute *attr,
900                                     char *buf)
901 {
902         struct acpi_power_resource *resource;
903
904         resource = to_power_resource(to_acpi_device(dev));
905         return sprintf(buf, "%u\n", !!resource->ref_count);
906 }
907 static DEVICE_ATTR_RO(resource_in_use);
908
909 static void acpi_power_sysfs_remove(struct acpi_device *device)
910 {
911         device_remove_file(&device->dev, &dev_attr_resource_in_use);
912 }
913
914 static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
915 {
916         mutex_lock(&power_resource_list_lock);
917
918         if (!list_empty(&acpi_power_resource_list)) {
919                 struct acpi_power_resource *r;
920
921                 list_for_each_entry(r, &acpi_power_resource_list, list_node)
922                         if (r->order > resource->order) {
923                                 list_add_tail(&resource->list_node, &r->list_node);
924                                 goto out;
925                         }
926         }
927         list_add_tail(&resource->list_node, &acpi_power_resource_list);
928
929  out:
930         mutex_unlock(&power_resource_list_lock);
931 }
932
933 struct acpi_device *acpi_add_power_resource(acpi_handle handle)
934 {
935         struct acpi_device *device = acpi_fetch_acpi_dev(handle);
936         struct acpi_power_resource *resource;
937         union acpi_object acpi_object;
938         struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
939         acpi_status status;
940         u8 state_dummy;
941         int result;
942
943         if (device)
944                 return device;
945
946         resource = kzalloc(sizeof(*resource), GFP_KERNEL);
947         if (!resource)
948                 return NULL;
949
950         device = &resource->device;
951         acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
952                                 acpi_release_power_resource);
953         mutex_init(&resource->resource_lock);
954         INIT_LIST_HEAD(&resource->list_node);
955         INIT_LIST_HEAD(&resource->dependents);
956         strscpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
957         strscpy(acpi_device_class(device), ACPI_POWER_CLASS);
958         device->power.state = ACPI_STATE_UNKNOWN;
959         device->flags.match_driver = true;
960
961         /* Evaluate the object to get the system level and resource order. */
962         status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
963         if (ACPI_FAILURE(status))
964                 goto err;
965
966         resource->system_level = acpi_object.power_resource.system_level;
967         resource->order = acpi_object.power_resource.resource_order;
968         resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
969
970         /* Get the initial state or just flip it on if that fails. */
971         if (acpi_power_get_state(resource, &state_dummy))
972                 __acpi_power_on(resource);
973
974         acpi_handle_info(handle, "New power resource\n");
975
976         result = acpi_tie_acpi_dev(device);
977         if (result)
978                 goto err;
979
980         result = acpi_device_add(device);
981         if (result)
982                 goto err;
983
984         if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
985                 device->remove = acpi_power_sysfs_remove;
986
987         acpi_power_add_resource_to_list(resource);
988         acpi_device_add_finalize(device);
989         return device;
990
991  err:
992         acpi_release_power_resource(&device->dev);
993         return NULL;
994 }
995
996 #ifdef CONFIG_ACPI_SLEEP
997 void acpi_resume_power_resources(void)
998 {
999         struct acpi_power_resource *resource;
1000
1001         mutex_lock(&power_resource_list_lock);
1002
1003         list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
1004                 int result;
1005                 u8 state;
1006
1007                 mutex_lock(&resource->resource_lock);
1008
1009                 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
1010                 result = acpi_power_get_state(resource, &state);
1011                 if (result) {
1012                         mutex_unlock(&resource->resource_lock);
1013                         continue;
1014                 }
1015
1016                 if (state == ACPI_POWER_RESOURCE_STATE_OFF
1017                     && resource->ref_count) {
1018                         acpi_handle_debug(resource->device.handle, "Turning ON\n");
1019                         __acpi_power_on(resource);
1020                 }
1021
1022                 mutex_unlock(&resource->resource_lock);
1023         }
1024
1025         mutex_unlock(&power_resource_list_lock);
1026 }
1027 #endif
1028
1029 static const struct dmi_system_id dmi_leave_unused_power_resources_on[] = {
1030         {
1031                 /*
1032                  * The Toshiba Click Mini has a CPR3 power-resource which must
1033                  * be on for the touchscreen to work, but which is not in any
1034                  * _PR? lists. The other 2 affected power-resources are no-ops.
1035                  */
1036                 .matches = {
1037                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1038                         DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
1039                 },
1040         },
1041         {}
1042 };
1043
1044 /**
1045  * acpi_turn_off_unused_power_resources - Turn off power resources not in use.
1046  */
1047 void acpi_turn_off_unused_power_resources(void)
1048 {
1049         struct acpi_power_resource *resource;
1050
1051         if (unused_power_resources_quirk)
1052                 return;
1053
1054         mutex_lock(&power_resource_list_lock);
1055
1056         list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
1057                 mutex_lock(&resource->resource_lock);
1058
1059                 if (!resource->ref_count &&
1060                     resource->state == ACPI_POWER_RESOURCE_STATE_ON) {
1061                         acpi_handle_debug(resource->device.handle, "Turning OFF\n");
1062                         __acpi_power_off(resource);
1063                 }
1064
1065                 mutex_unlock(&resource->resource_lock);
1066         }
1067
1068         mutex_unlock(&power_resource_list_lock);
1069 }
1070
1071 void __init acpi_power_resources_init(void)
1072 {
1073         unused_power_resources_quirk =
1074                 dmi_check_system(dmi_leave_unused_power_resources_on);
1075 }