ACPI PM: Replace wakeup.prepared with reference counter
authorRafael J. Wysocki <rjw@sisk.pl>
Tue, 8 Sep 2009 21:15:31 +0000 (23:15 +0200)
committerJesse Barnes <jbarnes@virtuousgeek.org>
Wed, 9 Sep 2009 21:19:18 +0000 (14:19 -0700)
The wakeup.prepared flag is used for marking devices that have the
wake-up power already enabled, so that the wake-up power is not
enabled twice in a row for the same device.  This assumes, however,
that device wake-up power will only be enabled once, while the device
is being prepared for a system-wide sleep transition, and the second
attempt is made by acpi_enable_wakeup_device_prep().

With the upcoming PCI wake-up rework this assumption will not hold
any more for PCI bridges and the root bridge whose wake-up power
may be enabled as a result of wake-up enable propagation from other
devices (eg. add-on devices that are not associated with any GPEs).
Thus, there may be many attempts to enable wake-up power on a PCI
bridge or the root bridge during a system power state transition
and it's better to replace wakeup.prepared with a reference counter.

Reviewed-by: Matthew Garrett <mjg59@srcf.ucam.org>
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
drivers/acpi/power.c
drivers/acpi/scan.c
drivers/acpi/wakeup.c
include/acpi/acpi_bus.h

index d74365d4a6e7d03f781954663301c50d30ac77da..5a09bf392ec1c815da28d1d1d69b5f963578b3f3 100644 (file)
@@ -44,6 +44,8 @@
 #include <acpi/acpi_bus.h>
 #include <acpi/acpi_drivers.h>
 
+#include "sleep.h"
+
 #define _COMPONENT                     ACPI_POWER_COMPONENT
 ACPI_MODULE_NAME("power");
 #define ACPI_POWER_CLASS               "power_resource"
@@ -361,17 +363,15 @@ int acpi_device_sleep_wake(struct acpi_device *dev,
  */
 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
 {
-       int i, err;
+       int i, err = 0;
 
        if (!dev || !dev->wakeup.flags.valid)
                return -EINVAL;
 
-       /*
-        * Do not execute the code below twice in a row without calling
-        * acpi_disable_wakeup_device_power() in between for the same device
-        */
-       if (dev->wakeup.flags.prepared)
-               return 0;
+       mutex_lock(&acpi_device_lock);
+
+       if (dev->wakeup.prepare_count++)
+               goto out;
 
        /* Open power resource */
        for (i = 0; i < dev->wakeup.resources.count; i++) {
@@ -379,7 +379,8 @@ int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
                if (ret) {
                        printk(KERN_ERR PREFIX "Transition power state\n");
                        dev->wakeup.flags.valid = 0;
-                       return -ENODEV;
+                       err = -ENODEV;
+                       goto err_out;
                }
        }
 
@@ -388,9 +389,13 @@ int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
         * in arbitrary power state afterwards.
         */
        err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
-       if (!err)
-               dev->wakeup.flags.prepared = 1;
 
+ err_out:
+       if (err)
+               dev->wakeup.prepare_count = 0;
+
+ out:
+       mutex_unlock(&acpi_device_lock);
        return err;
 }
 
@@ -402,35 +407,42 @@ int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
  */
 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
 {
-       int i, ret;
+       int i, err = 0;
 
        if (!dev || !dev->wakeup.flags.valid)
                return -EINVAL;
 
+       mutex_lock(&acpi_device_lock);
+
+       if (--dev->wakeup.prepare_count > 0)
+               goto out;
+
        /*
-        * Do not execute the code below twice in a row without calling
-        * acpi_enable_wakeup_device_power() in between for the same device
+        * Executing the code below even if prepare_count is already zero when
+        * the function is called may be useful, for example for initialisation.
         */
-       if (!dev->wakeup.flags.prepared)
-               return 0;
+       if (dev->wakeup.prepare_count < 0)
+               dev->wakeup.prepare_count = 0;
 
-       dev->wakeup.flags.prepared = 0;
-
-       ret = acpi_device_sleep_wake(dev, 0, 0, 0);
-       if (ret)
-               return ret;
+       err = acpi_device_sleep_wake(dev, 0, 0, 0);
+       if (err)
+               goto out;
 
        /* Close power resource */
        for (i = 0; i < dev->wakeup.resources.count; i++) {
-               ret = acpi_power_off_device(dev->wakeup.resources.handles[i], dev);
+               int ret = acpi_power_off_device(
+                               dev->wakeup.resources.handles[i], dev);
                if (ret) {
                        printk(KERN_ERR PREFIX "Transition power state\n");
                        dev->wakeup.flags.valid = 0;
-                       return -ENODEV;
+                       err = -ENODEV;
+                       goto out;
                }
        }
 
-       return ret;
+ out:
+       mutex_unlock(&acpi_device_lock);
+       return err;
 }
 
 /* --------------------------------------------------------------------------
index 781435d7e3692026d81cb6f112e7e422aa00a7dc..318b1ea7a5bf4c9df8a35bb251d7773a5a296b39 100644 (file)
@@ -781,6 +781,7 @@ static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
        kfree(buffer.pointer);
 
        device->wakeup.flags.valid = 1;
+       device->wakeup.prepare_count = 0;
        /* Call _PSW/_DSW object to disable its ability to wake the sleeping
         * system for the ACPI device with the _PRW object.
         * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
index 88725dcdf8bc813e42641b6e81cbdf79c8057159..e0ee0c036f5a674a882f190a4f8812989bfd276a 100644 (file)
@@ -68,7 +68,7 @@ void acpi_enable_wakeup_device(u8 sleep_state)
                /* If users want to disable run-wake GPE,
                 * we only disable it for wake and leave it for runtime
                 */
-               if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
+               if ((!dev->wakeup.state.enabled && !dev->wakeup.prepare_count)
                    || sleep_state > (u32) dev->wakeup.sleep_state) {
                        if (dev->wakeup.flags.run_wake) {
                                /* set_gpe_type will disable GPE, leave it like that */
@@ -100,7 +100,7 @@ void acpi_disable_wakeup_device(u8 sleep_state)
                if (!dev->wakeup.flags.valid)
                        continue;
 
-               if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
+               if ((!dev->wakeup.state.enabled && !dev->wakeup.prepare_count)
                    || sleep_state > (u32) dev->wakeup.sleep_state) {
                        if (dev->wakeup.flags.run_wake) {
                                acpi_set_gpe_type(dev->wakeup.gpe_device,
index c3ace75d57b4e73dd22d49fc7cf1e9cf6b6a16db..1fa3ffb7c93b2b21ec9b57d665fc3e8a935ecca9 100644 (file)
@@ -248,7 +248,6 @@ struct acpi_device_perf {
 /* Wakeup Management */
 struct acpi_device_wakeup_flags {
        u8 valid:1;             /* Can successfully enable wakeup? */
-       u8 prepared:1;          /* Has the wake-up capability been enabled? */
        u8 run_wake:1;          /* Run-Wake GPE devices */
 };
 
@@ -263,6 +262,7 @@ struct acpi_device_wakeup {
        struct acpi_handle_list resources;
        struct acpi_device_wakeup_state state;
        struct acpi_device_wakeup_flags flags;
+       int prepare_count;
 };
 
 /* Device */