From 3ebc81b8937d2bc1d0d0064bed29434dfce490aa Mon Sep 17 00:00:00 2001 From: Zhang Rui Date: Thu, 29 Mar 2012 14:09:38 +0800 Subject: ACPI: Introduce ACPI D3_COLD state support If a device has _PR3, it means the device supports D3_COLD. Add the ability to validate and enter D3_COLD state in ACPI. Signed-off-by: Zhang Rui Signed-off-by: Lin Ming Signed-off-by: Len Brown --- drivers/acpi/power.c | 4 ++-- drivers/acpi/scan.c | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 9ac2a9fa90ff..0d681fb7428b 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -500,14 +500,14 @@ int acpi_power_transition(struct acpi_device *device, int state) { int result; - if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3)) + if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) return -EINVAL; if (device->power.state == state) return 0; if ((device->power.state < ACPI_STATE_D0) - || (device->power.state > ACPI_STATE_D3)) + || (device->power.state > ACPI_STATE_D3_COLD)) return -ENODEV; /* TBD: Resources must be ordered. */ diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 8ab80bafe3f1..571396cf6110 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -885,6 +885,13 @@ static int acpi_bus_get_power_flags(struct acpi_device *device) acpi_bus_add_power_resource(ps->resources.handles[j]); } + /* The exist of _PR3 indicates D3Cold support */ + if (i == ACPI_STATE_D3) { + status = acpi_get_handle(device->handle, object_name, &handle); + if (ACPI_SUCCESS(status)) + device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1; + } + /* Evaluate "_PSx" to see if we can do explicit sets */ object_name[2] = 'S'; status = acpi_get_handle(device->handle, object_name, &handle); -- cgit v1.2.3-59-g8ed1b From 0090def6c37c8ea29508a435e581f2ef26fea10f Mon Sep 17 00:00:00 2001 From: Lin Ming Date: Thu, 29 Mar 2012 14:09:39 +0800 Subject: ACPI: Add interface to register/unregister device to/from power resources Devices may share same list of power resources in _PR0, for example Device(Dev0) { Name (_PR0, Package (0x01) { P0PR, P1PR }) } Device(Dev1) { Name (_PR0, Package (0x01) { P0PR, P1PR } } Assume Dev0 and Dev1 were runtime suspended. Then Dev0 is resumed first and it goes into D0 state. But Dev1 is left in D0_Uninitialised state. This is wrong. In this case, Dev1 must be resumed too. In order to hand this case, each power resource maintains a list of devices which relies on it. When power resource is ON, it will check if the devices on its list can be resumed. The device can only be resumed when all the power resouces of its _PR0 are ON. Signed-off-by: Lin Ming Signed-off-by: Len Brown --- drivers/acpi/power.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++ include/acpi/acpi_bus.h | 2 + 2 files changed, 164 insertions(+) diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 0d681fb7428b..7049a7d27c4f 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -40,9 +40,11 @@ #include #include #include +#include #include #include #include "sleep.h" +#include "internal.h" #define PREFIX "ACPI: " @@ -77,6 +79,20 @@ static struct acpi_driver acpi_power_driver = { }, }; +/* + * A power managed device + * A device may rely on multiple power resources. + * */ +struct acpi_power_managed_device { + struct device *dev; /* The physical device */ + acpi_handle *handle; +}; + +struct acpi_power_resource_device { + struct acpi_power_managed_device *device; + struct acpi_power_resource_device *next; +}; + struct acpi_power_resource { struct acpi_device * device; acpi_bus_id name; @@ -84,6 +100,9 @@ struct acpi_power_resource { u32 order; unsigned int ref_count; struct mutex resource_lock; + + /* List of devices relying on this power resource */ + struct acpi_power_resource_device *devices; }; static struct list_head acpi_power_resource_list; @@ -183,8 +202,26 @@ static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state) return 0; } +/* Resume the device when all power resources in _PR0 are on */ +static void acpi_power_on_device(struct acpi_power_managed_device *device) +{ + struct acpi_device *acpi_dev; + acpi_handle handle = device->handle; + int state; + + if (acpi_bus_get_device(handle, &acpi_dev)) + return; + + if(acpi_power_get_inferred_state(acpi_dev, &state)) + return; + + if (state == ACPI_STATE_D0 && pm_runtime_suspended(device->dev)) + pm_request_resume(device->dev); +} + static int __acpi_power_on(struct acpi_power_resource *resource) { + struct acpi_power_resource_device *device_list = resource->devices; acpi_status status = AE_OK; status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL); @@ -197,6 +234,12 @@ static int __acpi_power_on(struct acpi_power_resource *resource) ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n", resource->name)); + while (device_list) { + acpi_power_on_device(device_list->device); + + device_list = device_list->next; + } + return 0; } @@ -299,6 +342,125 @@ static int acpi_power_on_list(struct acpi_handle_list *list) return result; } +static void __acpi_power_resource_unregister_device(struct device *dev, + acpi_handle res_handle) +{ + struct acpi_power_resource *resource = NULL; + struct acpi_power_resource_device *prev, *curr; + + if (acpi_power_get_context(res_handle, &resource)) + return; + + mutex_lock(&resource->resource_lock); + prev = NULL; + curr = resource->devices; + while (curr) { + if (curr->device->dev == dev) { + if (!prev) + resource->devices = curr->next; + else + prev->next = curr->next; + + kfree(curr); + break; + } + + prev = curr; + curr = curr->next; + } + mutex_unlock(&resource->resource_lock); +} + +/* Unlink dev from all power resources in _PR0 */ +void acpi_power_resource_unregister_device(struct device *dev, acpi_handle handle) +{ + struct acpi_device *acpi_dev; + struct acpi_handle_list *list; + int i; + + if (!dev || !handle) + return; + + if (acpi_bus_get_device(handle, &acpi_dev)) + return; + + list = &acpi_dev->power.states[ACPI_STATE_D0].resources; + + for (i = 0; i < list->count; i++) + __acpi_power_resource_unregister_device(dev, + list->handles[i]); +} + +static int __acpi_power_resource_register_device( + struct acpi_power_managed_device *powered_device, acpi_handle handle) +{ + struct acpi_power_resource *resource = NULL; + struct acpi_power_resource_device *power_resource_device; + int result; + + result = acpi_power_get_context(handle, &resource); + if (result) + return result; + + power_resource_device = kzalloc( + sizeof(*power_resource_device), GFP_KERNEL); + if (!power_resource_device) + return -ENOMEM; + + power_resource_device->device = powered_device; + + mutex_lock(&resource->resource_lock); + power_resource_device->next = resource->devices; + resource->devices = power_resource_device; + mutex_unlock(&resource->resource_lock); + + return 0; +} + +/* Link dev to all power resources in _PR0 */ +int acpi_power_resource_register_device(struct device *dev, acpi_handle handle) +{ + struct acpi_device *acpi_dev; + struct acpi_handle_list *list; + struct acpi_power_managed_device *powered_device; + int i, ret; + + if (!dev || !handle) + return -ENODEV; + + ret = acpi_bus_get_device(handle, &acpi_dev); + if (ret) + goto no_power_resource; + + if (!acpi_dev->power.flags.power_resources) + goto no_power_resource; + + powered_device = kzalloc(sizeof(*powered_device), GFP_KERNEL); + if (!powered_device) + return -ENOMEM; + + powered_device->dev = dev; + powered_device->handle = handle; + + list = &acpi_dev->power.states[ACPI_STATE_D0].resources; + + for (i = 0; i < list->count; i++) { + ret = __acpi_power_resource_register_device(powered_device, + list->handles[i]); + + if (ret) { + acpi_power_resource_unregister_device(dev, handle); + break; + } + } + + return ret; + +no_power_resource: + printk(KERN_WARNING PREFIX "Invalid Power Resource to register!"); + return -ENODEV; +} + /** * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in * ACPI 3.0) _PSW (Power State Wake) diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 6cd5b6403a7b..e168fffe1db5 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -323,6 +323,8 @@ int acpi_bus_set_power(acpi_handle handle, int state); int acpi_bus_update_power(acpi_handle handle, int *state_p); bool acpi_bus_power_manageable(acpi_handle handle); bool acpi_bus_can_wakeup(acpi_handle handle); +int acpi_power_resource_register_device(struct device *dev, acpi_handle handle); +void acpi_power_resource_unregister_device(struct device *dev, acpi_handle handle); #ifdef CONFIG_ACPI_PROC_EVENT int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data); int acpi_bus_generate_proc_event4(const char *class, const char *bid, u8 type, int data); -- cgit v1.2.3-59-g8ed1b From b24e5098853653554baf6ec975b9e855f3d6e5c0 Mon Sep 17 00:00:00 2001 From: Lin Ming Date: Tue, 27 Mar 2012 15:43:25 +0800 Subject: ACPI, PCI: Move acpi_dev_run_wake() to ACPI core acpi_dev_run_wake() is a generic function which can be used by other subsystem too. Rename it to acpi_pm_device_run_wake, to be consistent with acpi_pm_device_sleep_wake. Then move it to ACPI core. Acked-by: Rafael J. Wysocki Signed-off-by: Lin Ming Signed-off-by: Len Brown --- drivers/acpi/sleep.c | 35 +++++++++++++++++++++++++++++++++++ drivers/pci/pci-acpi.c | 40 +++------------------------------------- include/acpi/acpi_bus.h | 5 +++++ 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/drivers/acpi/sleep.c b/drivers/acpi/sleep.c index ca191ff97844..00fa664d5fd8 100644 --- a/drivers/acpi/sleep.c +++ b/drivers/acpi/sleep.c @@ -17,6 +17,7 @@ #include #include #include +#include #include @@ -729,6 +730,40 @@ int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p) #endif /* CONFIG_PM */ #ifdef CONFIG_PM_SLEEP +/** + * acpi_pm_device_run_wake - Enable/disable wake-up for given device. + * @phys_dev: Device to enable/disable the platform to wake-up the system for. + * @enable: Whether enable or disable the wake-up functionality. + * + * Find the ACPI device object corresponding to @pci_dev and try to + * enable/disable the GPE associated with it. + */ +int acpi_pm_device_run_wake(struct device *phys_dev, bool enable) +{ + struct acpi_device *dev; + acpi_handle handle; + + if (!device_run_wake(phys_dev)) + return -EINVAL; + + handle = DEVICE_ACPI_HANDLE(phys_dev); + if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) { + dev_dbg(phys_dev, "ACPI handle has no context in %s!\n", + __func__); + return -ENODEV; + } + + if (enable) { + acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0); + acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number); + } else { + acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number); + acpi_disable_wakeup_device_power(dev); + } + + return 0; +} + /** * acpi_pm_device_sleep_wake - enable or disable the system wake-up * capability of given device diff --git a/drivers/pci/pci-acpi.c b/drivers/pci/pci-acpi.c index 060fd22a1103..0f150f271c2a 100644 --- a/drivers/pci/pci-acpi.c +++ b/drivers/pci/pci-acpi.c @@ -277,40 +277,6 @@ static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable) return 0; } -/** - * acpi_dev_run_wake - Enable/disable wake-up for given device. - * @phys_dev: Device to enable/disable the platform to wake-up the system for. - * @enable: Whether enable or disable the wake-up functionality. - * - * Find the ACPI device object corresponding to @pci_dev and try to - * enable/disable the GPE associated with it. - */ -static int acpi_dev_run_wake(struct device *phys_dev, bool enable) -{ - struct acpi_device *dev; - acpi_handle handle; - - if (!device_run_wake(phys_dev)) - return -EINVAL; - - handle = DEVICE_ACPI_HANDLE(phys_dev); - if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) { - dev_dbg(phys_dev, "ACPI handle has no context in %s!\n", - __func__); - return -ENODEV; - } - - if (enable) { - acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0); - acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number); - } else { - acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number); - acpi_disable_wakeup_device_power(dev); - } - - return 0; -} - static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable) { while (bus->parent) { @@ -318,14 +284,14 @@ static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable) if (bridge->pme_interrupt) return; - if (!acpi_dev_run_wake(&bridge->dev, enable)) + if (!acpi_pm_device_run_wake(&bridge->dev, enable)) return; bus = bus->parent; } /* We have reached the root bus. */ if (bus->bridge) - acpi_dev_run_wake(bus->bridge, enable); + acpi_pm_device_run_wake(bus->bridge, enable); } static int acpi_pci_run_wake(struct pci_dev *dev, bool enable) @@ -333,7 +299,7 @@ static int acpi_pci_run_wake(struct pci_dev *dev, bool enable) if (dev->pme_interrupt) return 0; - if (!acpi_dev_run_wake(&dev->dev, enable)) + if (!acpi_pm_device_run_wake(&dev->dev, enable)) return 0; acpi_pci_propagate_run_wake(dev->bus, enable); diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index e168fffe1db5..f1c8ca60e824 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -394,8 +394,13 @@ static inline int acpi_pm_device_sleep_state(struct device *d, int *p) #endif #ifdef CONFIG_PM_SLEEP +int acpi_pm_device_run_wake(struct device *, bool); int acpi_pm_device_sleep_wake(struct device *, bool); #else +static inline int acpi_pm_device_run_wake(struct device *dev, bool enable) +{ + return -ENODEV; +} static inline int acpi_pm_device_sleep_wake(struct device *dev, bool enable) { return -ENODEV; -- cgit v1.2.3-59-g8ed1b