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