From bc9b6407bd6df3ab7189e5622816bbc11ae9d2d8 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:05 +0100 Subject: ACPI / PM: Rework the handling of devices depending on power resources Commit 0090def6 (ACPI: Add interface to register/unregister device to/from power resources) made it possible to indicate to the ACPI core that if the given device depends on any power resources, then it should be resumed as soon as all of the power resources required by it to transition to the D0 power state have been turned on. Unfortunately, however, this was a mistake, because all devices depending on power resources should be treated this way (i.e. they should be resumed when all power resources required by their D0 state have been turned on) and for the majority of those devices the ACPI core can figure out by itself which (physical) devices depend on what power resources. For this reason, replace the code added by commit 0090def6 with a new, much more straightforward, mechanism that will be used internally by the ACPI core and remove all references to that code from kernel subsystems using ACPI. For the cases when there are (physical) devices that should be resumed whenever a not directly related ACPI device node goes into D0 as a result of power resources configuration changes, like in the SATA case, add two new routines, acpi_dev_pm_add_dependent() and acpi_dev_pm_remove_dependent(), allowing subsystems to manage such dependencies. Convert the SATA subsystem to use the new functions accordingly. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/device_pm.c | 56 ++++++++++++ drivers/acpi/internal.h | 1 + drivers/acpi/power.c | 219 ++++++++++++++++++----------------------------- drivers/acpi/scan.c | 8 ++ 4 files changed, 147 insertions(+), 137 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c index 995019063f64..8be4b29e38aa 100644 --- a/drivers/acpi/device_pm.c +++ b/drivers/acpi/device_pm.c @@ -665,3 +665,59 @@ void acpi_dev_pm_detach(struct device *dev, bool power_off) } } EXPORT_SYMBOL_GPL(acpi_dev_pm_detach); + +/** + * acpi_dev_pm_add_dependent - Add physical device depending for PM. + * @handle: Handle of ACPI device node. + * @depdev: Device depending on that node for PM. + */ +void acpi_dev_pm_add_dependent(acpi_handle handle, struct device *depdev) +{ + struct acpi_device_physical_node *dep; + struct acpi_device *adev; + + if (!depdev || acpi_bus_get_device(handle, &adev)) + return; + + mutex_lock(&adev->physical_node_lock); + + list_for_each_entry(dep, &adev->power_dependent, node) + if (dep->dev == depdev) + goto out; + + dep = kzalloc(sizeof(*dep), GFP_KERNEL); + if (dep) { + dep->dev = depdev; + list_add_tail(&dep->node, &adev->power_dependent); + } + + out: + mutex_unlock(&adev->physical_node_lock); +} +EXPORT_SYMBOL_GPL(acpi_dev_pm_add_dependent); + +/** + * acpi_dev_pm_remove_dependent - Remove physical device depending for PM. + * @handle: Handle of ACPI device node. + * @depdev: Device depending on that node for PM. + */ +void acpi_dev_pm_remove_dependent(acpi_handle handle, struct device *depdev) +{ + struct acpi_device_physical_node *dep; + struct acpi_device *adev; + + if (!depdev || acpi_bus_get_device(handle, &adev)) + return; + + mutex_lock(&adev->physical_node_lock); + + list_for_each_entry(dep, &adev->power_dependent, node) + if (dep->dev == depdev) { + list_del(&dep->node); + kfree(dep); + break; + } + + mutex_unlock(&adev->physical_node_lock); +} +EXPORT_SYMBOL_GPL(acpi_dev_pm_remove_dependent); diff --git a/drivers/acpi/internal.h b/drivers/acpi/internal.h index e050254ae143..b79b4258bd6b 100644 --- a/drivers/acpi/internal.h +++ b/drivers/acpi/internal.h @@ -38,6 +38,7 @@ static inline void acpi_debugfs_init(void) { return; } Power Resource -------------------------------------------------------------------------- */ int acpi_power_init(void); +void acpi_power_add_remove_device(struct acpi_device *adev, bool add); int acpi_device_sleep_wake(struct acpi_device *dev, int enable, int sleep_state, int dev_state); int acpi_power_get_inferred_state(struct acpi_device *device, int *state); diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 6e7b9d523812..659386c4f0cb 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -83,31 +83,20 @@ static struct acpi_driver acpi_power_driver = { .drv.pm = &acpi_power_pm, }; -/* - * 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_dependent_device { + struct list_head node; + struct acpi_device *adev; + struct work_struct work; }; struct acpi_power_resource { - struct acpi_device * device; + struct acpi_device *device; + struct list_head dependent; acpi_bus_id name; u32 system_level; u32 order; unsigned int ref_count; struct mutex resource_lock; - - /* List of devices relying on this power resource */ - struct acpi_power_resource_device *devices; - struct mutex devices_lock; }; static struct list_head acpi_power_resource_list; @@ -207,21 +196,30 @@ 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) +static void acpi_power_resume_dependent(struct work_struct *work) { - struct acpi_device *acpi_dev; - acpi_handle handle = device->handle; + struct acpi_power_dependent_device *dep; + struct acpi_device_physical_node *pn; + struct acpi_device *adev; int state; - if (acpi_bus_get_device(handle, &acpi_dev)) + dep = container_of(work, struct acpi_power_dependent_device, work); + adev = dep->adev; + if (acpi_power_get_inferred_state(adev, &state)) return; - if(acpi_power_get_inferred_state(acpi_dev, &state)) + if (state > ACPI_STATE_D0) return; - if (state == ACPI_STATE_D0 && pm_runtime_suspended(device->dev)) - pm_request_resume(device->dev); + mutex_lock(&adev->physical_node_lock); + + list_for_each_entry(pn, &adev->physical_node_list, node) + pm_request_resume(pn->dev); + + list_for_each_entry(pn, &adev->power_dependent, node) + pm_request_resume(pn->dev); + + mutex_unlock(&adev->physical_node_lock); } static int __acpi_power_on(struct acpi_power_resource *resource) @@ -244,9 +242,7 @@ static int __acpi_power_on(struct acpi_power_resource *resource) static int acpi_power_on(acpi_handle handle) { int result = 0; - bool resume_device = false; struct acpi_power_resource *resource = NULL; - struct acpi_power_resource_device *device_list; result = acpi_power_get_context(handle, &resource); if (result) @@ -260,26 +256,17 @@ static int acpi_power_on(acpi_handle handle) resource->name)); } else { result = __acpi_power_on(resource); - if (result) + if (result) { resource->ref_count--; - else - resume_device = true; - } + } else { + struct acpi_power_dependent_device *dep; - mutex_unlock(&resource->resource_lock); - - if (!resume_device) - return result; - - mutex_lock(&resource->devices_lock); - - device_list = resource->devices; - while (device_list) { - acpi_power_on_device(device_list->device); - device_list = device_list->next; + list_for_each_entry(dep, &resource->dependent, node) + schedule_work(&dep->work); + } } - mutex_unlock(&resource->devices_lock); + mutex_unlock(&resource->resource_lock); return result; } @@ -357,119 +344,81 @@ 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) +static void acpi_power_add_dependent(acpi_handle rhandle, + struct acpi_device *adev) { - struct acpi_power_resource *resource = NULL; - struct acpi_power_resource_device *prev, *curr; + struct acpi_power_dependent_device *dep; + struct acpi_power_resource *resource; - if (acpi_power_get_context(res_handle, &resource)) + if (!rhandle || !adev || acpi_power_get_context(rhandle, &resource)) return; - mutex_lock(&resource->devices_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->devices_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; + mutex_lock(&resource->resource_lock); - if (!dev || !handle) - return; + list_for_each_entry(dep, &resource->dependent, node) + if (dep->adev == adev) + goto out; - if (acpi_bus_get_device(handle, &acpi_dev)) - return; + dep = kzalloc(sizeof(*dep), GFP_KERNEL); + if (!dep) + goto out; - list = &acpi_dev->power.states[ACPI_STATE_D0].resources; + dep->adev = adev; + INIT_WORK(&dep->work, acpi_power_resume_dependent); + list_add_tail(&dep->node, &resource->dependent); - for (i = 0; i < list->count; i++) - __acpi_power_resource_unregister_device(dev, - list->handles[i]); + out: + mutex_unlock(&resource->resource_lock); } -EXPORT_SYMBOL_GPL(acpi_power_resource_unregister_device); -static int __acpi_power_resource_register_device( - struct acpi_power_managed_device *powered_device, acpi_handle handle) +static void acpi_power_remove_dependent(acpi_handle rhandle, + struct acpi_device *adev) { - struct acpi_power_resource *resource = NULL; - struct acpi_power_resource_device *power_resource_device; - int result; + struct acpi_power_dependent_device *dep; + struct acpi_power_resource *resource; + struct work_struct *work = NULL; - result = acpi_power_get_context(handle, &resource); - if (result) - return result; + if (!rhandle || !adev || acpi_power_get_context(rhandle, &resource)) + return; - power_resource_device = kzalloc( - sizeof(*power_resource_device), GFP_KERNEL); - if (!power_resource_device) - return -ENOMEM; + mutex_lock(&resource->resource_lock); - power_resource_device->device = powered_device; + list_for_each_entry(dep, &resource->dependent, node) + if (dep->adev == adev) { + list_del(&dep->node); + work = &dep->work; + break; + } - mutex_lock(&resource->devices_lock); - power_resource_device->next = resource->devices; - resource->devices = power_resource_device; - mutex_unlock(&resource->devices_lock); + mutex_unlock(&resource->resource_lock); - return 0; + if (work) { + cancel_work_sync(work); + kfree(dep); + } } -/* Link dev to all power resources in _PR0 */ -int acpi_power_resource_register_device(struct device *dev, acpi_handle handle) +void acpi_power_add_remove_device(struct acpi_device *adev, bool add) { - struct acpi_device *acpi_dev; - struct acpi_handle_list *list; - struct acpi_power_managed_device *powered_device; - int i, ret; + if (adev->power.flags.power_resources) { + struct acpi_device_power_state *ps; + int j; - if (!dev || !handle) - return -ENODEV; - - ret = acpi_bus_get_device(handle, &acpi_dev); - if (ret || !acpi_dev->power.flags.power_resources) - return -ENODEV; + ps = &adev->power.states[ACPI_STATE_D0]; + for (j = 0; j < ps->resources.count; j++) { + acpi_handle rhandle = ps->resources.handles[j]; - 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; + if (add) + acpi_power_add_dependent(rhandle, adev); + else + acpi_power_remove_dependent(rhandle, adev); } } - - return ret; } -EXPORT_SYMBOL_GPL(acpi_power_resource_register_device); + +/* -------------------------------------------------------------------------- + Device Power Management + -------------------------------------------------------------------------- */ /** * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in @@ -623,10 +572,6 @@ int acpi_disable_wakeup_device_power(struct acpi_device *dev) return err; } -/* -------------------------------------------------------------------------- - Device Power Management - -------------------------------------------------------------------------- */ - int acpi_power_get_inferred_state(struct acpi_device *device, int *state) { int result = 0; @@ -725,7 +670,7 @@ static int acpi_power_add(struct acpi_device *device) resource->device = device; mutex_init(&resource->resource_lock); - mutex_init(&resource->devices_lock); + INIT_LIST_HEAD(&resource->dependent); strcpy(resource->name, device->pnp.bus_id); strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME); strcpy(acpi_device_class(device), ACPI_POWER_CLASS); diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 7d164a966b0d..c6d60910e8a8 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -633,6 +633,7 @@ static int acpi_device_register(struct acpi_device *device) INIT_LIST_HEAD(&device->wakeup_list); INIT_LIST_HEAD(&device->physical_node_list); mutex_init(&device->physical_node_lock); + INIT_LIST_HEAD(&device->power_dependent); new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL); if (!new_bus_id) { @@ -706,8 +707,14 @@ static void acpi_device_unregister(struct acpi_device *device) acpi_detach_data(device->handle, acpi_bus_data_handler); + acpi_power_add_remove_device(device, false); acpi_device_remove_files(device); device_unregister(&device->dev); + /* + * Drop the reference counts of all power resources the device depends + * on and turn off the ones that have no more references. + */ + acpi_power_transition(device, ACPI_STATE_D3_COLD); } /* -------------------------------------------------------------------------- @@ -1441,6 +1448,7 @@ static int acpi_add_single_object(struct acpi_device **child, end: if (!result) { + acpi_power_add_remove_device(device, true); acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Adding %s [%s] parent %s\n", dev_name(&device->dev), -- cgit v1.2.3 From d43e167db44b37bb284dc72fff2c3b61bb155752 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:05 +0100 Subject: ACPI / scan: More straightforward preparation of ACPI device objects Simplify the code preparing struct acpi_device objects for registration by removing useless code, moving different pieces of code into the functions they belong to and making a couple of int functions always returning 0 void. This also fixes a possible memory leak in ACPI device registration error code path by making acpi_device_register() detach data from device->handle if device_register() fails and prepares the scanning code for special-casing ACPI power resources (next patch). Signed-off-by: Rafael J. Wysocki --- drivers/acpi/scan.c | 178 ++++++++++++++++++---------------------------------- 1 file changed, 61 insertions(+), 117 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index c6d60910e8a8..02629a810c04 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -472,6 +472,7 @@ static void acpi_free_ids(struct acpi_device *device) kfree(id->id); kfree(id); } + kfree(device->pnp.unique_id); } static void acpi_device_release(struct device *dev) @@ -479,7 +480,6 @@ static void acpi_device_release(struct device *dev) struct acpi_device *acpi_dev = to_acpi_device(dev); acpi_free_ids(acpi_dev); - kfree(acpi_dev->pnp.unique_id); kfree(acpi_dev); } @@ -623,6 +623,18 @@ static int acpi_device_register(struct acpi_device *device) struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id; int found = 0; + if (device->handle) { + acpi_status status; + + status = acpi_attach_data(device->handle, acpi_bus_data_handler, + device); + if (ACPI_FAILURE(status)) { + acpi_handle_err(device->handle, + "Unable to attach device data\n"); + return -ENODEV; + } + } + /* * Linkage * ------- @@ -637,8 +649,9 @@ static int acpi_device_register(struct acpi_device *device) new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL); if (!new_bus_id) { - printk(KERN_ERR PREFIX "Memory allocation error\n"); - return -ENOMEM; + pr_err(PREFIX "Memory allocation error\n"); + result = -ENOMEM; + goto err_detach; } mutex_lock(&acpi_device_lock); @@ -677,7 +690,7 @@ static int acpi_device_register(struct acpi_device *device) result = device_register(&device->dev); if (result) { dev_err(&device->dev, "Error registering device\n"); - goto end; + goto err; } result = acpi_device_setup_files(device); @@ -687,12 +700,16 @@ static int acpi_device_register(struct acpi_device *device) device->removal_type = ACPI_BUS_REMOVAL_NORMAL; return 0; -end: + + err: mutex_lock(&acpi_device_lock); if (device->parent) list_del(&device->node); list_del(&device->wakeup_list); mutex_unlock(&acpi_device_lock); + + err_detach: + acpi_detach_data(device->handle, acpi_bus_data_handler); return result; } @@ -857,12 +874,6 @@ void acpi_bus_data_handler(acpi_handle handle, void *context) return; } -static int acpi_bus_get_perf_flags(struct acpi_device *device) -{ - device->performance.state = ACPI_STATE_UNKNOWN; - return 0; -} - static acpi_status acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, struct acpi_device_wakeup *wakeup) @@ -1013,12 +1024,25 @@ static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) static void acpi_bus_add_power_resource(acpi_handle handle); -static int acpi_bus_get_power_flags(struct acpi_device *device) +static void acpi_bus_get_power_flags(struct acpi_device *device) { acpi_status status = 0; acpi_handle handle = NULL; u32 i = 0; + /* Power resources cannot be power manageable. */ + if (device->device_type == ACPI_BUS_TYPE_POWER) + return; + + /* Presence of _PS0|_PR0 indicates 'power manageable' */ + status = acpi_get_handle(device->handle, "_PS0", &handle); + if (ACPI_FAILURE(status)) { + status = acpi_get_handle(device->handle, "_PR0", &handle); + if (ACPI_FAILURE(status)) + return; + } + + device->flags.power_manageable = 1; /* * Power Management Flags @@ -1084,16 +1108,13 @@ static int acpi_bus_get_power_flags(struct acpi_device *device) device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1; acpi_bus_init_power(device); - - return 0; } -static int acpi_bus_get_flags(struct acpi_device *device) +static void acpi_bus_get_flags(struct acpi_device *device) { acpi_status status = AE_OK; acpi_handle temp = NULL; - /* Presence of _STA indicates 'dynamic_status' */ status = acpi_get_handle(device->handle, "_STA", &temp); if (ACPI_SUCCESS(status)) @@ -1113,21 +1134,6 @@ static int acpi_bus_get_flags(struct acpi_device *device) if (ACPI_SUCCESS(status)) device->flags.ejectable = 1; } - - /* Power resources cannot be power manageable. */ - if (device->device_type == ACPI_BUS_TYPE_POWER) - return 0; - - /* Presence of _PS0|_PR0 indicates 'power manageable' */ - status = acpi_get_handle(device->handle, "_PS0", &temp); - if (ACPI_FAILURE(status)) - status = acpi_get_handle(device->handle, "_PR0", &temp); - if (ACPI_SUCCESS(status)) - device->flags.power_manageable = 1; - - /* TBD: Performance management */ - - return 0; } static void acpi_device_get_busid(struct acpi_device *device) @@ -1352,27 +1358,18 @@ static void acpi_device_set_id(struct acpi_device *device) } } -static int acpi_device_set_context(struct acpi_device *device) +static void acpi_init_device_object(struct acpi_device *device, + acpi_handle handle, + int type, unsigned long long sta) { - acpi_status status; - - /* - * Context - * ------- - * Attach this 'struct acpi_device' to the ACPI object. This makes - * resolutions from handle->device very efficient. Fixed hardware - * devices have no handles, so we skip them. - */ - if (!device->handle) - return 0; - - status = acpi_attach_data(device->handle, - acpi_bus_data_handler, device); - if (ACPI_SUCCESS(status)) - return 0; - - printk(KERN_ERR PREFIX "Error attaching device data\n"); - return -ENODEV; + INIT_LIST_HEAD(&device->pnp.ids); + device->device_type = type; + device->handle = handle; + device->parent = acpi_bus_get_parent(handle); + STRUCT_TO_INT(device->status) = sta; + acpi_device_get_busid(device); + acpi_device_set_id(device); + acpi_bus_get_flags(device); } static int acpi_add_single_object(struct acpi_device **child, @@ -1389,78 +1386,25 @@ static int acpi_add_single_object(struct acpi_device **child, return -ENOMEM; } - INIT_LIST_HEAD(&device->pnp.ids); - device->device_type = type; - device->handle = handle; - device->parent = acpi_bus_get_parent(handle); - STRUCT_TO_INT(device->status) = sta; - - acpi_device_get_busid(device); - - /* - * Flags - * ----- - * Note that we only look for object handles -- cannot evaluate objects - * until we know the device is present and properly initialized. - */ - result = acpi_bus_get_flags(device); - if (result) - goto end; - - /* - * Initialize Device - * ----------------- - * TBD: Synch with Core's enumeration/initialization process. - */ - acpi_device_set_id(device); - - /* - * Power Management - * ---------------- - */ - if (device->flags.power_manageable) { - result = acpi_bus_get_power_flags(device); - if (result) - goto end; - } - - /* - * Wakeup device management - *----------------------- - */ + acpi_init_device_object(device, handle, type, sta); + acpi_bus_get_power_flags(device); acpi_bus_get_wakeup_device_flags(device); - /* - * Performance Management - * ---------------------- - */ - if (device->flags.performance_manageable) { - result = acpi_bus_get_perf_flags(device); - if (result) - goto end; - } - - if ((result = acpi_device_set_context(device))) - goto end; - device->flags.match_driver = match_driver; result = acpi_device_register(device); - -end: - if (!result) { - acpi_power_add_remove_device(device, true); - acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); - ACPI_DEBUG_PRINT((ACPI_DB_INFO, - "Adding %s [%s] parent %s\n", dev_name(&device->dev), - (char *) buffer.pointer, - device->parent ? dev_name(&device->parent->dev) : - "(null)")); - kfree(buffer.pointer); - *child = device; - } else + if (result) { acpi_device_release(&device->dev); + return result; + } - return result; + acpi_power_add_remove_device(device, true); + acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); + ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n", + dev_name(&device->dev), (char *) buffer.pointer, + device->parent ? dev_name(&device->parent->dev) : "(null)")); + kfree(buffer.pointer); + *child = device; + return 0; } #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \ -- cgit v1.2.3 From 82c7d5efaadf99fb4a26500cd5b59b6fd7659772 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:05 +0100 Subject: ACPI / scan: Treat power resources in a special way ACPI power resources need to be treated in a special way by the namespace scanning code, because they need to be ready to use as soon as they have been discovered (even before registering ACPI device nodes using them for power management). For this reason, it doesn't make sense to separate the preparation of struct acpi_device objects representing them in the device hierarchy from the creation of struct acpi_power_resource objects actually used for power resource manipulation. Accordingly, it doesn't make sense to define non-empty .add() and .remove() callbacks in the power resources "driver" (in fact, it is questionable whether or not it is useful to register such a "driver" at all). Rearrange the code in scan.c and power.c so that power resources are initialized entirely by one routine, acpi_add_power_resource(), that also prepares their struct acpi_device objects and registers them with the driver core, telling it to use a special release routine, acpi_release_power_resource(), for removing objects that represent power resources from memory. Make the ACPI namespace scanning code in scan.c always use acpi_add_power_resource() for preparing and registering objects that represent power resources. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/internal.h | 13 ++++ drivers/acpi/power.c | 157 ++++++++++++++++++++++-------------------------- drivers/acpi/scan.c | 47 ++++++--------- 3 files changed, 102 insertions(+), 115 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/internal.h b/drivers/acpi/internal.h index b79b4258bd6b..ce6cb24de8c7 100644 --- a/drivers/acpi/internal.h +++ b/drivers/acpi/internal.h @@ -34,10 +34,23 @@ int acpi_debugfs_init(void); static inline void acpi_debugfs_init(void) { return; } #endif +/* -------------------------------------------------------------------------- + Device Node Initialization / Removal + -------------------------------------------------------------------------- */ +#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \ + ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING) + +int acpi_device_register(struct acpi_device *device, + void (*release)(struct device *)); +void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, + int type, unsigned long long sta); +void acpi_free_ids(struct acpi_device *device); + /* -------------------------------------------------------------------------- Power Resource -------------------------------------------------------------------------- */ int acpi_power_init(void); +void acpi_add_power_resource(acpi_handle handle); void acpi_power_add_remove_device(struct acpi_device *adev, bool add); int acpi_device_sleep_wake(struct acpi_device *dev, int enable, int sleep_state, int dev_state); diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 659386c4f0cb..b12933fd2e56 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -58,8 +58,7 @@ ACPI_MODULE_NAME("power"); #define ACPI_POWER_RESOURCE_STATE_ON 0x01 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF -static int acpi_power_add(struct acpi_device *device); -static int acpi_power_remove(struct acpi_device *device, int type); +static inline int acpi_power_add(struct acpi_device *device) { return 0; } static const struct acpi_device_id power_device_ids[] = { {ACPI_POWER_HID, 0}, @@ -76,10 +75,7 @@ static struct acpi_driver acpi_power_driver = { .name = "power", .class = ACPI_POWER_CLASS, .ids = power_device_ids, - .ops = { - .add = acpi_power_add, - .remove = acpi_power_remove, - }, + .ops.add = acpi_power_add, .drv.pm = &acpi_power_pm, }; @@ -90,9 +86,9 @@ struct acpi_power_dependent_device { }; struct acpi_power_resource { - struct acpi_device *device; + struct acpi_device device; struct list_head dependent; - acpi_bus_id name; + char *name; u32 system_level; u32 order; unsigned int ref_count; @@ -105,28 +101,14 @@ static struct list_head acpi_power_resource_list; Power Resource Management -------------------------------------------------------------------------- */ -static int -acpi_power_get_context(acpi_handle handle, - struct acpi_power_resource **resource) +static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) { - int result = 0; - struct acpi_device *device = NULL; - - - if (!resource) - return -ENODEV; - - result = acpi_bus_get_device(handle, &device); - if (result) { - printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle); - return result; - } + struct acpi_device *device; - *resource = acpi_driver_data(device); - if (!*resource) - return -ENODEV; + if (acpi_bus_get_device(handle, &device)) + return NULL; - return 0; + return container_of(device, struct acpi_power_resource, device); } static int acpi_power_get_state(acpi_handle handle, int *state) @@ -171,9 +153,9 @@ static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state) acpi_handle handle = list->handles[i]; int result; - result = acpi_power_get_context(handle, &resource); - if (result) - return result; + resource = acpi_power_get_context(handle); + if (!resource) + return -ENODEV; mutex_lock(&resource->resource_lock); @@ -226,12 +208,12 @@ static int __acpi_power_on(struct acpi_power_resource *resource) { acpi_status status = AE_OK; - status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL); + status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL); if (ACPI_FAILURE(status)) return -ENODEV; /* Update the power resource's _device_ power state */ - resource->device->power.state = ACPI_STATE_D0; + resource->device.power.state = ACPI_STATE_D0; ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n", resource->name)); @@ -242,11 +224,11 @@ static int __acpi_power_on(struct acpi_power_resource *resource) static int acpi_power_on(acpi_handle handle) { int result = 0; - struct acpi_power_resource *resource = NULL; + struct acpi_power_resource *resource; - result = acpi_power_get_context(handle, &resource); - if (result) - return result; + resource = acpi_power_get_context(handle); + if (!resource) + return -ENODEV; mutex_lock(&resource->resource_lock); @@ -275,11 +257,11 @@ static int acpi_power_off(acpi_handle handle) { int result = 0; acpi_status status = AE_OK; - struct acpi_power_resource *resource = NULL; + struct acpi_power_resource *resource; - result = acpi_power_get_context(handle, &resource); - if (result) - return result; + resource = acpi_power_get_context(handle); + if (!resource) + return -ENODEV; mutex_lock(&resource->resource_lock); @@ -297,12 +279,12 @@ static int acpi_power_off(acpi_handle handle) goto unlock; } - status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL); + status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL); if (ACPI_FAILURE(status)) { result = -ENODEV; } else { /* Update the power resource's _device_ power state */ - resource->device->power.state = ACPI_STATE_D3; + resource->device.power.state = ACPI_STATE_D3; ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n", @@ -350,7 +332,11 @@ static void acpi_power_add_dependent(acpi_handle rhandle, struct acpi_power_dependent_device *dep; struct acpi_power_resource *resource; - if (!rhandle || !adev || acpi_power_get_context(rhandle, &resource)) + if (!rhandle || !adev) + return; + + resource = acpi_power_get_context(rhandle); + if (!resource) return; mutex_lock(&resource->resource_lock); @@ -378,7 +364,11 @@ static void acpi_power_remove_dependent(acpi_handle rhandle, struct acpi_power_resource *resource; struct work_struct *work = NULL; - if (!rhandle || !adev || acpi_power_get_context(rhandle, &resource)) + if (!rhandle || !adev) + return; + + resource = acpi_power_get_context(rhandle); + if (!resource) return; mutex_lock(&resource->resource_lock); @@ -648,46 +638,53 @@ int acpi_power_transition(struct acpi_device *device, int state) return result; } -/* -------------------------------------------------------------------------- - Driver Interface - -------------------------------------------------------------------------- */ +static void acpi_release_power_resource(struct device *dev) +{ + struct acpi_device *device = to_acpi_device(dev); + struct acpi_power_resource *resource; + + acpi_free_ids(device); + resource = container_of(device, struct acpi_power_resource, device); + kfree(resource); +} -static int acpi_power_add(struct acpi_device *device) +void acpi_add_power_resource(acpi_handle handle) { - int result = 0, state; - acpi_status status = AE_OK; - struct acpi_power_resource *resource = NULL; + struct acpi_power_resource *resource; + struct acpi_device *device = NULL; union acpi_object acpi_object; struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object }; + acpi_status status; + int state, result = -ENODEV; + acpi_bus_get_device(handle, &device); + if (device) + return; - if (!device) - return -EINVAL; - - resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL); + resource = kzalloc(sizeof(*resource), GFP_KERNEL); if (!resource) - return -ENOMEM; + return; - resource->device = device; + device = &resource->device; + acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER, + ACPI_STA_DEFAULT); mutex_init(&resource->resource_lock); INIT_LIST_HEAD(&resource->dependent); - strcpy(resource->name, device->pnp.bus_id); + resource->name = device->pnp.bus_id; strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME); strcpy(acpi_device_class(device), ACPI_POWER_CLASS); - device->driver_data = resource; /* Evalute the object to get the system level and resource order. */ - status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer); - if (ACPI_FAILURE(status)) { - result = -ENODEV; - goto end; - } + status = acpi_evaluate_object(handle, NULL, NULL, &buffer); + if (ACPI_FAILURE(status)) + goto out; + resource->system_level = acpi_object.power_resource.system_level; resource->order = acpi_object.power_resource.resource_order; - result = acpi_power_get_state(device->handle, &state); + result = acpi_power_get_state(handle, &state); if (result) - goto end; + goto out; switch (state) { case ACPI_POWER_RESOURCE_STATE_ON: @@ -698,34 +695,24 @@ static int acpi_power_add(struct acpi_device *device) break; default: device->power.state = ACPI_STATE_UNKNOWN; - break; } printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device), acpi_device_bid(device), state ? "on" : "off"); - end: + device->flags.match_driver = true; + result = acpi_device_register(device, acpi_release_power_resource); + + out: if (result) - kfree(resource); + acpi_release_power_resource(&device->dev); - return result; + return; } -static int acpi_power_remove(struct acpi_device *device, int type) -{ - struct acpi_power_resource *resource; - - if (!device) - return -EINVAL; - - resource = acpi_driver_data(device); - if (!resource) - return -EINVAL; - - kfree(resource); - - return 0; -} +/* -------------------------------------------------------------------------- + Driver Interface + -------------------------------------------------------------------------- */ #ifdef CONFIG_PM_SLEEP static int acpi_power_resume(struct device *dev) diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 02629a810c04..952b08af91de 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -464,7 +464,7 @@ int acpi_match_device_ids(struct acpi_device *device, } EXPORT_SYMBOL(acpi_match_device_ids); -static void acpi_free_ids(struct acpi_device *device) +void acpi_free_ids(struct acpi_device *device) { struct acpi_hardware_id *id, *tmp; @@ -617,7 +617,8 @@ struct bus_type acpi_bus_type = { .uevent = acpi_device_uevent, }; -static int acpi_device_register(struct acpi_device *device) +int acpi_device_register(struct acpi_device *device, + void (*release)(struct device *)) { int result; struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id; @@ -686,7 +687,7 @@ static int acpi_device_register(struct acpi_device *device) if (device->parent) device->dev.parent = &device->parent->dev; device->dev.bus = &acpi_bus_type; - device->dev.release = &acpi_device_release; + device->dev.release = release; result = device_register(&device->dev); if (result) { dev_err(&device->dev, "Error registering device\n"); @@ -1022,18 +1023,12 @@ static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) "error in _DSW or _PSW evaluation\n")); } -static void acpi_bus_add_power_resource(acpi_handle handle); - static void acpi_bus_get_power_flags(struct acpi_device *device) { acpi_status status = 0; acpi_handle handle = NULL; u32 i = 0; - /* Power resources cannot be power manageable. */ - if (device->device_type == ACPI_BUS_TYPE_POWER) - return; - /* Presence of _PS0|_PR0 indicates 'power manageable' */ status = acpi_get_handle(device->handle, "_PS0", &handle); if (ACPI_FAILURE(status)) { @@ -1068,8 +1063,10 @@ static void acpi_bus_get_power_flags(struct acpi_device *device) int j; device->power.flags.power_resources = 1; - for (j = 0; j < ps->resources.count; j++) - acpi_bus_add_power_resource(ps->resources.handles[j]); + for (j = 0; j < ps->resources.count; j++) { + acpi_handle rhandle = ps->resources.handles[j]; + acpi_add_power_resource(rhandle); + } } /* Evaluate "_PSx" to see if we can do explicit sets */ @@ -1358,9 +1355,8 @@ static void acpi_device_set_id(struct acpi_device *device) } } -static void acpi_init_device_object(struct acpi_device *device, - acpi_handle handle, - int type, unsigned long long sta) +void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, + int type, unsigned long long sta) { INIT_LIST_HEAD(&device->pnp.ids); device->device_type = type; @@ -1391,7 +1387,7 @@ static int acpi_add_single_object(struct acpi_device **child, acpi_bus_get_wakeup_device_flags(device); device->flags.match_driver = match_driver; - result = acpi_device_register(device); + result = acpi_device_register(device, acpi_device_release); if (result) { acpi_device_release(&device->dev); return result; @@ -1407,19 +1403,6 @@ static int acpi_add_single_object(struct acpi_device **child, return 0; } -#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \ - ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING) - -static void acpi_bus_add_power_resource(acpi_handle handle) -{ - struct acpi_device *device = NULL; - - acpi_bus_get_device(handle, &device); - if (!device) - acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER, - ACPI_STA_DEFAULT, true); -} - static int acpi_bus_type_and_status(acpi_handle handle, int *type, unsigned long long *sta) { @@ -1476,6 +1459,11 @@ static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used, if (result) return AE_OK; + if (type == ACPI_BUS_TYPE_POWER) { + acpi_add_power_resource(handle); + return AE_OK; + } + if (!(sta & ACPI_STA_DEVICE_PRESENT) && !(sta & ACPI_STA_DEVICE_FUNCTIONING)) { struct acpi_device_wakeup wakeup; @@ -1488,8 +1476,7 @@ static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used, return AE_CTRL_DEPTH; } - acpi_add_single_object(&device, handle, type, sta, - type == ACPI_BUS_TYPE_POWER); + acpi_add_single_object(&device, handle, type, sta, false); if (!device) return AE_CTRL_DEPTH; -- cgit v1.2.3 From 781d737c7466845035e5ce02885c7436b5278b90 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:06 +0100 Subject: ACPI: Drop power resources driver The ACPI power resources driver is not very useful, because the only thing it really does is to restore the state of the power resources that were "on" before system suspend or hibernation, but that may be achieved in a different way. Drop the ACPI power resources driver entirely and add acpi_resume_power_resources() that will walk the list of all registered power resources during system resume and turn on the ones that were "on" before the preceding system suspend or hibernation. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/power.c | 91 +++++++++++++++++++--------------------------------- drivers/acpi/scan.c | 1 - drivers/acpi/sleep.c | 2 ++ drivers/acpi/sleep.h | 2 ++ 4 files changed, 37 insertions(+), 59 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index b12933fd2e56..29803857a2ef 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -58,27 +58,6 @@ ACPI_MODULE_NAME("power"); #define ACPI_POWER_RESOURCE_STATE_ON 0x01 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF -static inline int acpi_power_add(struct acpi_device *device) { return 0; } - -static const struct acpi_device_id power_device_ids[] = { - {ACPI_POWER_HID, 0}, - {"", 0}, -}; -MODULE_DEVICE_TABLE(acpi, power_device_ids); - -#ifdef CONFIG_PM_SLEEP -static int acpi_power_resume(struct device *dev); -#endif -static SIMPLE_DEV_PM_OPS(acpi_power_pm, NULL, acpi_power_resume); - -static struct acpi_driver acpi_power_driver = { - .name = "power", - .class = ACPI_POWER_CLASS, - .ids = power_device_ids, - .ops.add = acpi_power_add, - .drv.pm = &acpi_power_pm, -}; - struct acpi_power_dependent_device { struct list_head node; struct acpi_device *adev; @@ -87,6 +66,7 @@ struct acpi_power_dependent_device { struct acpi_power_resource { struct acpi_device device; + struct list_head list_node; struct list_head dependent; char *name; u32 system_level; @@ -95,7 +75,8 @@ struct acpi_power_resource { struct mutex resource_lock; }; -static struct list_head acpi_power_resource_list; +static LIST_HEAD(acpi_power_resource_list); +static DEFINE_MUTEX(power_resource_list_lock); /* -------------------------------------------------------------------------- Power Resource Management @@ -643,8 +624,13 @@ static void acpi_release_power_resource(struct device *dev) struct acpi_device *device = to_acpi_device(dev); struct acpi_power_resource *resource; - acpi_free_ids(device); resource = container_of(device, struct acpi_power_resource, device); + + mutex_lock(&power_resource_list_lock); + list_del(&resource->list_node); + mutex_unlock(&power_resource_list_lock); + + acpi_free_ids(device); kfree(resource); } @@ -677,14 +663,14 @@ void acpi_add_power_resource(acpi_handle handle) /* Evalute the object to get the system level and resource order. */ status = acpi_evaluate_object(handle, NULL, NULL, &buffer); if (ACPI_FAILURE(status)) - goto out; + goto err; resource->system_level = acpi_object.power_resource.system_level; resource->order = acpi_object.power_resource.resource_order; result = acpi_power_get_state(handle, &state); if (result) - goto out; + goto err; switch (state) { case ACPI_POWER_RESOURCE_STATE_ON: @@ -702,51 +688,40 @@ void acpi_add_power_resource(acpi_handle handle) device->flags.match_driver = true; result = acpi_device_register(device, acpi_release_power_resource); - - out: if (result) - acpi_release_power_resource(&device->dev); + goto err; + mutex_lock(&power_resource_list_lock); + list_add(&resource->list_node, &acpi_power_resource_list); + mutex_unlock(&power_resource_list_lock); return; -} -/* -------------------------------------------------------------------------- - Driver Interface - -------------------------------------------------------------------------- */ + err: + acpi_release_power_resource(&device->dev); +} -#ifdef CONFIG_PM_SLEEP -static int acpi_power_resume(struct device *dev) +#ifdef CONFIG_ACPI_SLEEP +void acpi_resume_power_resources(void) { - int result = 0, state; - struct acpi_device *device; struct acpi_power_resource *resource; - if (!dev) - return -EINVAL; + mutex_lock(&power_resource_list_lock); - device = to_acpi_device(dev); - resource = acpi_driver_data(device); - if (!resource) - return -EINVAL; - - mutex_lock(&resource->resource_lock); + list_for_each_entry(resource, &acpi_power_resource_list, list_node) { + int result, state; - result = acpi_power_get_state(device->handle, &state); - if (result) - goto unlock; + mutex_lock(&resource->resource_lock); - if (state == ACPI_POWER_RESOURCE_STATE_OFF && resource->ref_count) - result = __acpi_power_on(resource); + result = acpi_power_get_state(resource->device.handle, &state); + if (!result && state == ACPI_POWER_RESOURCE_STATE_OFF + && resource->ref_count) { + dev_info(&resource->device.dev, "Turning ON\n"); + __acpi_power_on(resource); + } - unlock: - mutex_unlock(&resource->resource_lock); + mutex_unlock(&resource->resource_lock); + } - return result; + mutex_unlock(&power_resource_list_lock); } #endif - -int __init acpi_power_init(void) -{ - INIT_LIST_HEAD(&acpi_power_resource_list); - return acpi_bus_register_driver(&acpi_power_driver); -} diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 952b08af91de..c7ea9c2649a4 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -1636,7 +1636,6 @@ int __init acpi_scan_init(void) printk(KERN_ERR PREFIX "Could not register bus type\n"); } - acpi_power_init(); acpi_pci_root_init(); /* diff --git a/drivers/acpi/sleep.c b/drivers/acpi/sleep.c index 2fcc67d34b11..4ef0328579cc 100644 --- a/drivers/acpi/sleep.c +++ b/drivers/acpi/sleep.c @@ -386,6 +386,8 @@ static void acpi_pm_finish(void) acpi_target_sleep_state = ACPI_STATE_S0; + acpi_resume_power_resources(); + /* If we were woken with the fixed power button, provide a small * hint to userspace in the form of a wakeup event on the fixed power * button device (if it can be found). diff --git a/drivers/acpi/sleep.h b/drivers/acpi/sleep.h index 74d59c8f4678..0143540a2519 100644 --- a/drivers/acpi/sleep.h +++ b/drivers/acpi/sleep.h @@ -6,3 +6,5 @@ extern void acpi_disable_wakeup_devices(u8 sleep_state); extern struct list_head acpi_wakeup_device_list; extern struct mutex acpi_device_lock; + +extern void acpi_resume_power_resources(void); -- cgit v1.2.3 From 722c929f32616943d2b67332068f09c08e81eec8 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:06 +0100 Subject: ACPI: Do not use device power states of power resources ACPI power resource objects have struct acpi_device components, but they are only used for registering those resources in the device hierarchy. In particular, power state information stored in them is completely useless (amnong other things, because the power resources "devices" are not power manageable), so there is no reason for the power resources management code to keep it up to date. Remove the code updating device power states of power resources from drivers/acpi/power.c. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/power.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 29803857a2ef..06ad05288af8 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -193,9 +193,6 @@ static int __acpi_power_on(struct acpi_power_resource *resource) if (ACPI_FAILURE(status)) return -ENODEV; - /* Update the power resource's _device_ power state */ - resource->device.power.state = ACPI_STATE_D0; - ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n", resource->name)); @@ -261,16 +258,12 @@ static int acpi_power_off(acpi_handle handle) } status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL); - if (ACPI_FAILURE(status)) { + if (ACPI_FAILURE(status)) result = -ENODEV; - } else { - /* Update the power resource's _device_ power state */ - resource->device.power.state = ACPI_STATE_D3; - + else ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n", resource->name)); - } unlock: mutex_unlock(&resource->resource_lock); @@ -659,6 +652,7 @@ void acpi_add_power_resource(acpi_handle handle) resource->name = device->pnp.bus_id; strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME); strcpy(acpi_device_class(device), ACPI_POWER_CLASS); + device->power.state = ACPI_STATE_UNKNOWN; /* Evalute the object to get the system level and resource order. */ status = acpi_evaluate_object(handle, NULL, NULL, &buffer); @@ -672,17 +666,6 @@ void acpi_add_power_resource(acpi_handle handle) if (result) goto err; - switch (state) { - case ACPI_POWER_RESOURCE_STATE_ON: - device->power.state = ACPI_STATE_D0; - break; - case ACPI_POWER_RESOURCE_STATE_OFF: - device->power.state = ACPI_STATE_D3; - break; - default: - device->power.state = ACPI_STATE_UNKNOWN; - } - printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device), acpi_device_bid(device), state ? "on" : "off"); -- cgit v1.2.3 From 0b224527323669c66e0a37ae05b04034bfcdce14 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:06 +0100 Subject: ACPI / PM: Take order attribute of power resources into account ACPI power resources have an order attribute that should be taken into account when turning them on and off, but it is not used now. Modify the power resources management code to preserve the spec-compliant ordering of power resources that power states of devices depend on (analogous changes will be done separately for power resources used for wakeup). Signed-off-by: Rafael J. Wysocki --- drivers/acpi/internal.h | 2 + drivers/acpi/power.c | 161 ++++++++++++++++++++++++++---------------------- drivers/acpi/scan.c | 31 ++++++++-- include/acpi/acpi_bus.h | 2 +- 4 files changed, 118 insertions(+), 78 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/internal.h b/drivers/acpi/internal.h index ce6cb24de8c7..e28068a765a9 100644 --- a/drivers/acpi/internal.h +++ b/drivers/acpi/internal.h @@ -50,6 +50,8 @@ void acpi_free_ids(struct acpi_device *device); Power Resource -------------------------------------------------------------------------- */ int acpi_power_init(void); +void acpi_power_resources_list_add(acpi_handle handle, struct list_head *list); +void acpi_power_resources_list_free(struct list_head *list); void acpi_add_power_resource(acpi_handle handle); void acpi_power_add_remove_device(struct acpi_device *adev, bool add); int acpi_device_sleep_wake(struct acpi_device *dev, diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 06ad05288af8..22a3d00d0359 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -75,6 +75,11 @@ struct acpi_power_resource { struct mutex resource_lock; }; +struct acpi_power_resource_entry { + struct list_head node; + struct acpi_power_resource *resource; +}; + static LIST_HEAD(acpi_power_resource_list); static DEFINE_MUTEX(power_resource_list_lock); @@ -92,6 +97,41 @@ static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) return container_of(device, struct acpi_power_resource, device); } +void acpi_power_resources_list_add(acpi_handle handle, struct list_head *list) +{ + struct acpi_power_resource *resource = acpi_power_get_context(handle); + struct acpi_power_resource_entry *entry; + + if (!resource || !list) + return; + + entry = kzalloc(sizeof(*entry), GFP_KERNEL); + if (!entry) + return; + + entry->resource = resource; + if (!list_empty(list)) { + struct acpi_power_resource_entry *e; + + list_for_each_entry(e, list, node) + if (e->resource->order > resource->order) { + list_add_tail(&entry->node, &e->node); + return; + } + } + list_add_tail(&entry->node, list); +} + +void acpi_power_resources_list_free(struct list_head *list) +{ + struct acpi_power_resource_entry *entry, *e; + + list_for_each_entry_safe(entry, e, list, node) { + list_del(&entry->node); + kfree(entry); + } +} + static int acpi_power_get_state(acpi_handle handle, int *state) { acpi_status status = AE_OK; @@ -119,31 +159,23 @@ static int acpi_power_get_state(acpi_handle handle, int *state) return 0; } -static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state) +static int acpi_power_get_list_state(struct list_head *list, int *state) { + struct acpi_power_resource_entry *entry; int cur_state; - int i = 0; if (!list || !state) return -EINVAL; /* The state of the list is 'on' IFF all resources are 'on'. */ - - for (i = 0; i < list->count; i++) { - struct acpi_power_resource *resource; - acpi_handle handle = list->handles[i]; + list_for_each_entry(entry, list, node) { + struct acpi_power_resource *resource = entry->resource; + acpi_handle handle = resource->device.handle; int result; - resource = acpi_power_get_context(handle); - if (!resource) - return -ENODEV; - mutex_lock(&resource->resource_lock); - result = acpi_power_get_state(handle, &cur_state); - mutex_unlock(&resource->resource_lock); - if (result) return result; @@ -155,7 +187,6 @@ static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state) cur_state ? "on" : "off")); *state = cur_state; - return 0; } @@ -199,14 +230,9 @@ static int __acpi_power_on(struct acpi_power_resource *resource) return 0; } -static int acpi_power_on(acpi_handle handle) +static int acpi_power_on(struct acpi_power_resource *resource) { - int result = 0; - struct acpi_power_resource *resource; - - resource = acpi_power_get_context(handle); - if (!resource) - return -ENODEV; + int result = 0;; mutex_lock(&resource->resource_lock); @@ -231,15 +257,10 @@ static int acpi_power_on(acpi_handle handle) return result; } -static int acpi_power_off(acpi_handle handle) +static int acpi_power_off(struct acpi_power_resource *resource) { - int result = 0; acpi_status status = AE_OK; - struct acpi_power_resource *resource; - - resource = acpi_power_get_context(handle); - if (!resource) - return -ENODEV; + int result = 0; mutex_lock(&resource->resource_lock); @@ -271,47 +292,48 @@ static int acpi_power_off(acpi_handle handle) return result; } -static void __acpi_power_off_list(struct acpi_handle_list *list, int num_res) +static int acpi_power_off_list(struct list_head *list) { - int i; + struct acpi_power_resource_entry *entry; + int result = 0; - for (i = num_res - 1; i >= 0 ; i--) - acpi_power_off(list->handles[i]); -} + list_for_each_entry_reverse(entry, list, node) { + result = acpi_power_off(entry->resource); + if (result) + goto err; + } + return 0; -static void acpi_power_off_list(struct acpi_handle_list *list) -{ - __acpi_power_off_list(list, list->count); + err: + list_for_each_entry_continue(entry, list, node) + acpi_power_on(entry->resource); + + return result; } -static int acpi_power_on_list(struct acpi_handle_list *list) +static int acpi_power_on_list(struct list_head *list) { + struct acpi_power_resource_entry *entry; int result = 0; - int i; - for (i = 0; i < list->count; i++) { - result = acpi_power_on(list->handles[i]); - if (result) { - __acpi_power_off_list(list, i); - break; - } + list_for_each_entry(entry, list, node) { + result = acpi_power_on(entry->resource); + if (result) + goto err; } + return 0; + + err: + list_for_each_entry_continue_reverse(entry, list, node) + acpi_power_off(entry->resource); return result; } -static void acpi_power_add_dependent(acpi_handle rhandle, +static void acpi_power_add_dependent(struct acpi_power_resource *resource, struct acpi_device *adev) { struct acpi_power_dependent_device *dep; - struct acpi_power_resource *resource; - - if (!rhandle || !adev) - return; - - resource = acpi_power_get_context(rhandle); - if (!resource) - return; mutex_lock(&resource->resource_lock); @@ -331,20 +353,12 @@ static void acpi_power_add_dependent(acpi_handle rhandle, mutex_unlock(&resource->resource_lock); } -static void acpi_power_remove_dependent(acpi_handle rhandle, +static void acpi_power_remove_dependent(struct acpi_power_resource *resource, struct acpi_device *adev) { struct acpi_power_dependent_device *dep; - struct acpi_power_resource *resource; struct work_struct *work = NULL; - if (!rhandle || !adev) - return; - - resource = acpi_power_get_context(rhandle); - if (!resource) - return; - mutex_lock(&resource->resource_lock); list_for_each_entry(dep, &resource->dependent, node) @@ -366,16 +380,16 @@ void acpi_power_add_remove_device(struct acpi_device *adev, bool add) { if (adev->power.flags.power_resources) { struct acpi_device_power_state *ps; - int j; + struct acpi_power_resource_entry *entry; ps = &adev->power.states[ACPI_STATE_D0]; - for (j = 0; j < ps->resources.count; j++) { - acpi_handle rhandle = ps->resources.handles[j]; + list_for_each_entry(entry, &ps->resources, node) { + struct acpi_power_resource *resource = entry->resource; if (add) - acpi_power_add_dependent(rhandle, adev); + acpi_power_add_dependent(resource, adev); else - acpi_power_remove_dependent(rhandle, adev); + acpi_power_remove_dependent(resource, adev); } } } @@ -539,7 +553,6 @@ int acpi_disable_wakeup_device_power(struct acpi_device *dev) int acpi_power_get_inferred_state(struct acpi_device *device, int *state) { int result = 0; - struct acpi_handle_list *list = NULL; int list_state = 0; int i = 0; @@ -551,8 +564,9 @@ int acpi_power_get_inferred_state(struct acpi_device *device, int *state) * required for a given D-state are 'on'. */ for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { - list = &device->power.states[i].resources; - if (list->count < 1) + struct list_head *list = &device->power.states[i].resources; + + if (list_empty(list)) continue; result = acpi_power_get_list_state(list, &list_state); @@ -571,9 +585,12 @@ int acpi_power_get_inferred_state(struct acpi_device *device, int *state) int acpi_power_on_resources(struct acpi_device *device, int state) { - if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3) + if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_COLD) return -EINVAL; + if (state == ACPI_STATE_D3_COLD) + return 0; + return acpi_power_on_list(&device->power.states[state].resources); } @@ -584,7 +601,7 @@ int acpi_power_transition(struct acpi_device *device, int state) if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) return -EINVAL; - if (device->power.state == state) + if (device->power.state == state || !device->flags.power_manageable) return 0; if ((device->power.state < ACPI_STATE_D0) diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index c7ea9c2649a4..d557868c0081 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -475,11 +475,25 @@ void acpi_free_ids(struct acpi_device *device) kfree(device->pnp.unique_id); } +static void acpi_free_power_resources_lists(struct acpi_device *device) +{ + int i; + + if (!device->flags.power_manageable) + return; + + for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { + struct acpi_device_power_state *ps = &device->power.states[i]; + acpi_power_resources_list_free(&ps->resources); + } +} + static void acpi_device_release(struct device *dev) { struct acpi_device *acpi_dev = to_acpi_device(dev); acpi_free_ids(acpi_dev); + acpi_free_power_resources_lists(acpi_dev); kfree(acpi_dev); } @@ -1055,17 +1069,22 @@ static void acpi_bus_get_power_flags(struct acpi_device *device) for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { struct acpi_device_power_state *ps = &device->power.states[i]; char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' }; + struct acpi_handle_list resources; + INIT_LIST_HEAD(&ps->resources); /* Evaluate "_PRx" to se if power resources are referenced */ acpi_evaluate_reference(device->handle, object_name, NULL, - &ps->resources); - if (ps->resources.count) { + &resources); + if (resources.count) { int j; device->power.flags.power_resources = 1; - for (j = 0; j < ps->resources.count; j++) { - acpi_handle rhandle = ps->resources.handles[j]; + for (j = 0; j < resources.count; j++) { + acpi_handle rhandle = resources.handles[j]; + acpi_add_power_resource(rhandle); + acpi_power_resources_list_add(rhandle, + &ps->resources); } } @@ -1079,7 +1098,7 @@ static void acpi_bus_get_power_flags(struct acpi_device *device) * State is valid if there are means to put the device into it. * D3hot is only valid if _PR3 present. */ - if (ps->resources.count || + if (resources.count || (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) { ps->flags.valid = 1; ps->flags.os_accessible = 1; @@ -1089,6 +1108,8 @@ static void acpi_bus_get_power_flags(struct acpi_device *device) ps->latency = -1; /* Unknown - driver assigned */ } + INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources); + /* Set defaults for D0 and D3 states (always valid) */ device->power.states[ACPI_STATE_D0].flags.valid = 1; device->power.states[ACPI_STATE_D0].power = 100; diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 29a1badfca55..32dc679d2c71 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -199,7 +199,7 @@ struct acpi_device_power_state { } flags; int power; /* % Power (compared to D0) */ int latency; /* Dx->D0 time (microseconds) */ - struct acpi_handle_list resources; /* Power resources referenced */ + struct list_head resources; /* Power resources referenced */ }; struct acpi_device_power { -- cgit v1.2.3 From 993cbe595dda731471a07f4f65575fadedc570dc Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:06 +0100 Subject: ACPI / PM: Take order attribute of wakeup power resources into account ACPI power resources have an order attribute that should be taken into account when turning them on and off, but it is not used now. Modify the power resources management code to preserve the spec-compliant ordering of wakeup power resources. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/power.c | 44 ++++++++++++++++---------------------------- drivers/acpi/scan.c | 26 ++++++++++++++++---------- include/acpi/acpi_bus.h | 2 +- 3 files changed, 33 insertions(+), 39 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 22a3d00d0359..242feca231eb 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -469,7 +469,7 @@ 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 = 0; + int err = 0; if (!dev || !dev->wakeup.flags.valid) return -EINVAL; @@ -479,24 +479,17 @@ int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state) if (dev->wakeup.prepare_count++) goto out; - /* Open power resource */ - for (i = 0; i < dev->wakeup.resources.count; i++) { - int ret = acpi_power_on(dev->wakeup.resources.handles[i]); - if (ret) { - printk(KERN_ERR PREFIX "Transition power state\n"); - dev->wakeup.flags.valid = 0; - err = -ENODEV; - goto err_out; - } + err = acpi_power_on_list(&dev->wakeup.resources); + if (err) { + dev_err(&dev->dev, "Cannot turn wakeup power resources on\n"); + dev->wakeup.flags.valid = 0; + } else { + /* + * Passing 3 as the third argument below means the device may be + * put into arbitrary power state afterward. + */ + err = acpi_device_sleep_wake(dev, 1, sleep_state, 3); } - - /* - * Passing 3 as the third argument below means the device may be placed - * in arbitrary power state afterwards. - */ - err = acpi_device_sleep_wake(dev, 1, sleep_state, 3); - - err_out: if (err) dev->wakeup.prepare_count = 0; @@ -513,7 +506,7 @@ 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, err = 0; + int err = 0; if (!dev || !dev->wakeup.flags.valid) return -EINVAL; @@ -534,15 +527,10 @@ int acpi_disable_wakeup_device_power(struct acpi_device *dev) if (err) goto out; - /* Close power resource */ - for (i = 0; i < dev->wakeup.resources.count; i++) { - int ret = acpi_power_off(dev->wakeup.resources.handles[i]); - if (ret) { - printk(KERN_ERR PREFIX "Transition power state\n"); - dev->wakeup.flags.valid = 0; - err = -ENODEV; - goto out; - } + err = acpi_power_off_list(&dev->wakeup.resources); + if (err) { + dev_err(&dev->dev, "Cannot turn wakeup power resources off\n"); + dev->wakeup.flags.valid = 0; } out: diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index d557868c0081..e4ac46a9c664 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -479,6 +479,9 @@ static void acpi_free_power_resources_lists(struct acpi_device *device) { int i; + if (device->wakeup.flags.valid) + acpi_power_resources_list_free(&device->wakeup.resources); + if (!device->flags.power_manageable) return; @@ -902,6 +905,8 @@ acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, if (!wakeup) return AE_BAD_PARAMETER; + INIT_LIST_HEAD(&wakeup->resources); + /* _PRW */ status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer); if (ACPI_FAILURE(status)) { @@ -948,19 +953,17 @@ acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, } wakeup->sleep_state = element->integer.value; - if ((package->package.count - 2) > ACPI_MAX_HANDLES) { - status = AE_NO_MEMORY; - goto out; - } - wakeup->resources.count = package->package.count - 2; - for (i = 0; i < wakeup->resources.count; i++) { - element = &(package->package.elements[i + 2]); + for (i = 2; i < package->package.count; i++) { + acpi_handle rhandle; + + element = &(package->package.elements[i]); if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { status = AE_BAD_DATA; goto out; } - - wakeup->resources.handles[i] = element->reference.handle; + rhandle = element->reference.handle; + acpi_add_power_resource(rhandle); + acpi_power_resources_list_add(rhandle, &wakeup->resources); } acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number); @@ -1018,6 +1021,7 @@ static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) status = acpi_bus_extract_wakeup_device_power_package(device->handle, &device->wakeup); if (ACPI_FAILURE(status)) { + acpi_power_resources_list_free(&device->wakeup.resources); ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package")); return; } @@ -1491,9 +1495,11 @@ static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used, acpi_handle temp; status = acpi_get_handle(handle, "_PRW", &temp); - if (ACPI_SUCCESS(status)) + if (ACPI_SUCCESS(status)) { acpi_bus_extract_wakeup_device_power_package(handle, &wakeup); + acpi_power_resources_list_free(&wakeup.resources); + } return AE_CTRL_DEPTH; } diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 32dc679d2c71..a272c3156999 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -242,7 +242,7 @@ struct acpi_device_wakeup { acpi_handle gpe_device; u64 gpe_number; u64 sleep_state; - struct acpi_handle_list resources; + struct list_head resources; struct acpi_device_wakeup_flags flags; int prepare_count; }; -- cgit v1.2.3 From f33ce568366ab61b5685bae07306e40f17beb943 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:06 +0100 Subject: ACPI / scan: Move power state initialization to a separate routine To reduce indentation level and improve code readability, move the initialization code related to device power states from acpi_bus_get_power_flags() to a new routine, acpi_bus_init_power_state(). Signed-off-by: Rafael J. Wysocki --- drivers/acpi/scan.c | 87 ++++++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 41 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index e4ac46a9c664..10c98ff6b026 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -1041,6 +1041,50 @@ static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) "error in _DSW or _PSW evaluation\n")); } +static void acpi_bus_init_power_state(struct acpi_device *device, int state) +{ + struct acpi_device_power_state *ps = &device->power.states[state]; + char object_name[5] = { '_', 'P', 'R', '0' + state, '\0' }; + struct acpi_handle_list resources; + acpi_handle handle; + acpi_status status; + + INIT_LIST_HEAD(&ps->resources); + + /* Evaluate "_PRx" to se if power resources are referenced */ + acpi_evaluate_reference(device->handle, object_name, NULL, &resources); + if (resources.count) { + int j; + + device->power.flags.power_resources = 1; + for (j = 0; j < resources.count; j++) { + acpi_handle rhandle = resources.handles[j]; + + acpi_add_power_resource(rhandle); + acpi_power_resources_list_add(rhandle, &ps->resources); + } + } + + /* Evaluate "_PSx" to see if we can do explicit sets */ + object_name[2] = 'S'; + status = acpi_get_handle(device->handle, object_name, &handle); + if (ACPI_SUCCESS(status)) + ps->flags.explicit_set = 1; + + /* + * State is valid if there are means to put the device into it. + * D3hot is only valid if _PR3 present. + */ + if (resources.count + || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) { + ps->flags.valid = 1; + ps->flags.os_accessible = 1; + } + + ps->power = -1; /* Unknown - driver assigned */ + ps->latency = -1; /* Unknown - driver assigned */ +} + static void acpi_bus_get_power_flags(struct acpi_device *device) { acpi_status status = 0; @@ -1070,47 +1114,8 @@ static void acpi_bus_get_power_flags(struct acpi_device *device) /* * Enumerate supported power management states */ - for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { - struct acpi_device_power_state *ps = &device->power.states[i]; - char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' }; - struct acpi_handle_list resources; - - INIT_LIST_HEAD(&ps->resources); - /* Evaluate "_PRx" to se if power resources are referenced */ - acpi_evaluate_reference(device->handle, object_name, NULL, - &resources); - if (resources.count) { - int j; - - device->power.flags.power_resources = 1; - for (j = 0; j < resources.count; j++) { - acpi_handle rhandle = resources.handles[j]; - - acpi_add_power_resource(rhandle); - acpi_power_resources_list_add(rhandle, - &ps->resources); - } - } - - /* Evaluate "_PSx" to see if we can do explicit sets */ - object_name[2] = 'S'; - status = acpi_get_handle(device->handle, object_name, &handle); - if (ACPI_SUCCESS(status)) - ps->flags.explicit_set = 1; - - /* - * State is valid if there are means to put the device into it. - * D3hot is only valid if _PR3 present. - */ - if (resources.count || - (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) { - ps->flags.valid = 1; - ps->flags.os_accessible = 1; - } - - ps->power = -1; /* Unknown - driver assigned */ - ps->latency = -1; /* Unknown - driver assigned */ - } + for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) + acpi_bus_init_power_state(device, i); INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources); -- cgit v1.2.3 From 8bc5053bcdff09a6d1c6a61a79a9014884aa0a14 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:07 +0100 Subject: ACPI / scan: Remove unnecessary initialization of local variables The local variables in acpi_bus_get_power_flags() need not be initialized upfront, so change the code accordingly. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/scan.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 10c98ff6b026..8da315418d94 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -1087,9 +1087,9 @@ static void acpi_bus_init_power_state(struct acpi_device *device, int state) static void acpi_bus_get_power_flags(struct acpi_device *device) { - acpi_status status = 0; - acpi_handle handle = NULL; - u32 i = 0; + acpi_status status; + acpi_handle handle; + u32 i; /* Presence of _PS0|_PR0 indicates 'power manageable' */ status = acpi_get_handle(device->handle, "_PS0", &handle); -- cgit v1.2.3 From ef85bdbec444b42775a18580c6bfe1307a63ef0f Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:07 +0100 Subject: ACPI / scan: Consolidate extraction of power resources lists The lists of ACPI power resources are currently extracted in two different ways, one for wakeup power resources and one for power resources that device power states depend on. There is no reason why it should be done differently in those two cases, so introduce a common routine for extracting power resources lists from data returned by AML, acpi_extract_power_resources(), and make the namespace scanning code use it for both wakeup and device power states power resources. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/internal.h | 4 +++- drivers/acpi/power.c | 32 ++++++++++++++++++++++++++++++- drivers/acpi/scan.c | 51 ++++++++++++++++++++----------------------------- 3 files changed, 55 insertions(+), 32 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/internal.h b/drivers/acpi/internal.h index e28068a765a9..c35435e3d760 100644 --- a/drivers/acpi/internal.h +++ b/drivers/acpi/internal.h @@ -50,8 +50,10 @@ void acpi_free_ids(struct acpi_device *device); Power Resource -------------------------------------------------------------------------- */ int acpi_power_init(void); -void acpi_power_resources_list_add(acpi_handle handle, struct list_head *list); void acpi_power_resources_list_free(struct list_head *list); +acpi_status acpi_extract_power_resources(union acpi_object *package, + unsigned int start, + struct list_head *list); void acpi_add_power_resource(acpi_handle handle); void acpi_power_add_remove_device(struct acpi_device *adev, bool add); int acpi_device_sleep_wake(struct acpi_device *dev, diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 242feca231eb..4b93c97aff9f 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -97,7 +97,8 @@ static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) return container_of(device, struct acpi_power_resource, device); } -void acpi_power_resources_list_add(acpi_handle handle, struct list_head *list) +static void acpi_power_resources_list_add(acpi_handle handle, + struct list_head *list) { struct acpi_power_resource *resource = acpi_power_get_context(handle); struct acpi_power_resource_entry *entry; @@ -132,6 +133,35 @@ void acpi_power_resources_list_free(struct list_head *list) } } +acpi_status acpi_extract_power_resources(union acpi_object *package, + unsigned int start, + struct list_head *list) +{ + acpi_status status = AE_OK; + unsigned int i; + + for (i = start; i < package->package.count; i++) { + union acpi_object *element = &package->package.elements[i]; + acpi_handle rhandle; + + if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { + status = AE_BAD_DATA; + break; + } + rhandle = element->reference.handle; + if (!rhandle) { + status = AE_NULL_ENTRY; + break; + } + acpi_add_power_resource(rhandle); + acpi_power_resources_list_add(rhandle, list); + } + if (ACPI_FAILURE(status)) + acpi_power_resources_list_free(list); + + return status; +} + static int acpi_power_get_state(acpi_handle handle, int *state) { acpi_status status = AE_OK; diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 8da315418d94..d80df969f64a 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -900,7 +900,6 @@ acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, union acpi_object *package = NULL; union acpi_object *element = NULL; acpi_status status; - int i = 0; if (!wakeup) return AE_BAD_PARAMETER; @@ -953,18 +952,9 @@ acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, } wakeup->sleep_state = element->integer.value; - for (i = 2; i < package->package.count; i++) { - acpi_handle rhandle; - - element = &(package->package.elements[i]); - if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { - status = AE_BAD_DATA; - goto out; - } - rhandle = element->reference.handle; - acpi_add_power_resource(rhandle); - acpi_power_resources_list_add(rhandle, &wakeup->resources); - } + status = acpi_extract_power_resources(package, 2, &wakeup->resources); + if (ACPI_FAILURE(status)) + goto out; acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number); @@ -1021,7 +1011,6 @@ static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) status = acpi_bus_extract_wakeup_device_power_package(device->handle, &device->wakeup); if (ACPI_FAILURE(status)) { - acpi_power_resources_list_free(&device->wakeup.resources); ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package")); return; } @@ -1044,30 +1033,32 @@ static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) static void acpi_bus_init_power_state(struct acpi_device *device, int state) { struct acpi_device_power_state *ps = &device->power.states[state]; - char object_name[5] = { '_', 'P', 'R', '0' + state, '\0' }; - struct acpi_handle_list resources; + char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' }; + struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; acpi_handle handle; acpi_status status; INIT_LIST_HEAD(&ps->resources); - /* Evaluate "_PRx" to se if power resources are referenced */ - acpi_evaluate_reference(device->handle, object_name, NULL, &resources); - if (resources.count) { - int j; - - device->power.flags.power_resources = 1; - for (j = 0; j < resources.count; j++) { - acpi_handle rhandle = resources.handles[j]; - - acpi_add_power_resource(rhandle); - acpi_power_resources_list_add(rhandle, &ps->resources); + /* Evaluate "_PRx" to get referenced power resources */ + status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer); + if (ACPI_SUCCESS(status)) { + union acpi_object *package = buffer.pointer; + + if (buffer.length && package + && package->type == ACPI_TYPE_PACKAGE + && package->package.count) { + status = acpi_extract_power_resources(package, 0, + &ps->resources); + if (ACPI_SUCCESS(status)) + device->power.flags.power_resources = 1; } + ACPI_FREE(buffer.pointer); } /* Evaluate "_PSx" to see if we can do explicit sets */ - object_name[2] = 'S'; - status = acpi_get_handle(device->handle, object_name, &handle); + pathname[2] = 'S'; + status = acpi_get_handle(device->handle, pathname, &handle); if (ACPI_SUCCESS(status)) ps->flags.explicit_set = 1; @@ -1075,7 +1066,7 @@ static void acpi_bus_init_power_state(struct acpi_device *device, int state) * State is valid if there are means to put the device into it. * D3hot is only valid if _PR3 present. */ - if (resources.count + if (!list_empty(&ps->resources) || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) { ps->flags.valid = 1; ps->flags.os_accessible = 1; -- cgit v1.2.3 From e88c9c603b2ad0cd0fbe90afedba3f1becbbeb79 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:07 +0100 Subject: ACPI: Take power resource initialization errors into account Some ACPI power resource initialization errors, like memory allocation errors, are not taken into account appropriately in some cases, which may lead to a device having an incomplete list of power resources that one of its power states depends on, for one example. Rework the power resource initialization and namespace scanning code so that power resource initialization errors are treated more seriously. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/internal.h | 7 +++--- drivers/acpi/power.c | 44 +++++++++++++++++++++----------------- drivers/acpi/scan.c | 57 ++++++++++++++++++++++--------------------------- 3 files changed, 53 insertions(+), 55 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/internal.h b/drivers/acpi/internal.h index c35435e3d760..8a6c67c9da42 100644 --- a/drivers/acpi/internal.h +++ b/drivers/acpi/internal.h @@ -51,10 +51,9 @@ void acpi_free_ids(struct acpi_device *device); -------------------------------------------------------------------------- */ int acpi_power_init(void); void acpi_power_resources_list_free(struct list_head *list); -acpi_status acpi_extract_power_resources(union acpi_object *package, - unsigned int start, - struct list_head *list); -void acpi_add_power_resource(acpi_handle handle); +int acpi_extract_power_resources(union acpi_object *package, unsigned int start, + struct list_head *list); +int acpi_add_power_resource(acpi_handle handle); void acpi_power_add_remove_device(struct acpi_device *adev, bool add); int acpi_device_sleep_wake(struct acpi_device *dev, int enable, int sleep_state, int dev_state); diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 4b93c97aff9f..1600f753fafe 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -97,18 +97,18 @@ static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) return container_of(device, struct acpi_power_resource, device); } -static void acpi_power_resources_list_add(acpi_handle handle, - struct list_head *list) +static int acpi_power_resources_list_add(acpi_handle handle, + struct list_head *list) { struct acpi_power_resource *resource = acpi_power_get_context(handle); struct acpi_power_resource_entry *entry; if (!resource || !list) - return; + return -EINVAL; entry = kzalloc(sizeof(*entry), GFP_KERNEL); if (!entry) - return; + return -ENOMEM; entry->resource = resource; if (!list_empty(list)) { @@ -117,10 +117,11 @@ static void acpi_power_resources_list_add(acpi_handle handle, list_for_each_entry(e, list, node) if (e->resource->order > resource->order) { list_add_tail(&entry->node, &e->node); - return; + return 0; } } list_add_tail(&entry->node, list); + return 0; } void acpi_power_resources_list_free(struct list_head *list) @@ -133,33 +134,37 @@ void acpi_power_resources_list_free(struct list_head *list) } } -acpi_status acpi_extract_power_resources(union acpi_object *package, - unsigned int start, - struct list_head *list) +int acpi_extract_power_resources(union acpi_object *package, unsigned int start, + struct list_head *list) { - acpi_status status = AE_OK; unsigned int i; + int err = 0; for (i = start; i < package->package.count; i++) { union acpi_object *element = &package->package.elements[i]; acpi_handle rhandle; if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { - status = AE_BAD_DATA; + err = -ENODATA; break; } rhandle = element->reference.handle; if (!rhandle) { - status = AE_NULL_ENTRY; + err = -ENODEV; break; } - acpi_add_power_resource(rhandle); - acpi_power_resources_list_add(rhandle, list); + err = acpi_add_power_resource(rhandle); + if (err) + break; + + err = acpi_power_resources_list_add(rhandle, list); + if (err) + break; } - if (ACPI_FAILURE(status)) + if (err) acpi_power_resources_list_free(list); - return status; + return err; } static int acpi_power_get_state(acpi_handle handle, int *state) @@ -662,7 +667,7 @@ static void acpi_release_power_resource(struct device *dev) kfree(resource); } -void acpi_add_power_resource(acpi_handle handle) +int acpi_add_power_resource(acpi_handle handle) { struct acpi_power_resource *resource; struct acpi_device *device = NULL; @@ -673,11 +678,11 @@ void acpi_add_power_resource(acpi_handle handle) acpi_bus_get_device(handle, &device); if (device) - return; + return 0; resource = kzalloc(sizeof(*resource), GFP_KERNEL); if (!resource) - return; + return -ENOMEM; device = &resource->device; acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER, @@ -712,10 +717,11 @@ void acpi_add_power_resource(acpi_handle handle) mutex_lock(&power_resource_list_lock); list_add(&resource->list_node, &acpi_power_resource_list); mutex_unlock(&power_resource_list_lock); - return; + return 0; err: acpi_release_power_resource(&device->dev); + return result; } #ifdef CONFIG_ACPI_SLEEP diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index d80df969f64a..0b6a6b4febd6 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -892,17 +892,17 @@ void acpi_bus_data_handler(acpi_handle handle, void *context) return; } -static acpi_status -acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, - struct acpi_device_wakeup *wakeup) +static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, + struct acpi_device_wakeup *wakeup) { struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; union acpi_object *package = NULL; union acpi_object *element = NULL; acpi_status status; + int err = -ENODATA; if (!wakeup) - return AE_BAD_PARAMETER; + return -EINVAL; INIT_LIST_HEAD(&wakeup->resources); @@ -910,29 +910,25 @@ acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer); if (ACPI_FAILURE(status)) { ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW")); - return status; + return err; } package = (union acpi_object *)buffer.pointer; - if (!package || (package->package.count < 2)) { - status = AE_BAD_DATA; + if (!package || package->package.count < 2) goto out; - } element = &(package->package.elements[0]); - if (!element) { - status = AE_BAD_DATA; + if (!element) goto out; - } + if (element->type == ACPI_TYPE_PACKAGE) { if ((element->package.count < 2) || (element->package.elements[0].type != ACPI_TYPE_LOCAL_REFERENCE) - || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) { - status = AE_BAD_DATA; + || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) goto out; - } + wakeup->gpe_device = element->package.elements[0].reference.handle; wakeup->gpe_number = @@ -941,27 +937,24 @@ acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, wakeup->gpe_device = NULL; wakeup->gpe_number = element->integer.value; } else { - status = AE_BAD_DATA; goto out; } element = &(package->package.elements[1]); - if (element->type != ACPI_TYPE_INTEGER) { - status = AE_BAD_DATA; + if (element->type != ACPI_TYPE_INTEGER) goto out; - } + wakeup->sleep_state = element->integer.value; - status = acpi_extract_power_resources(package, 2, &wakeup->resources); - if (ACPI_FAILURE(status)) + err = acpi_extract_power_resources(package, 2, &wakeup->resources); + if (err) goto out; acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number); out: kfree(buffer.pointer); - - return status; + return err; } static void acpi_bus_set_run_wake_flags(struct acpi_device *device) @@ -1001,17 +994,17 @@ static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) { acpi_handle temp; acpi_status status = 0; - int psw_error; + int err; /* Presence of _PRW indicates wake capable */ status = acpi_get_handle(device->handle, "_PRW", &temp); if (ACPI_FAILURE(status)) return; - status = acpi_bus_extract_wakeup_device_power_package(device->handle, - &device->wakeup); - if (ACPI_FAILURE(status)) { - ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package")); + err = acpi_bus_extract_wakeup_device_power_package(device->handle, + &device->wakeup); + if (err) { + dev_err(&device->dev, "_PRW evaluation error: %d\n", err); return; } @@ -1024,8 +1017,8 @@ static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) * So it is necessary to call _DSW object first. Only when it is not * present will the _PSW object used. */ - psw_error = acpi_device_sleep_wake(device, 0, 0, 0); - if (psw_error) + err = acpi_device_sleep_wake(device, 0, 0, 0); + if (err) ACPI_DEBUG_PRINT((ACPI_DB_INFO, "error in _DSW or _PSW evaluation\n")); } @@ -1048,9 +1041,9 @@ static void acpi_bus_init_power_state(struct acpi_device *device, int state) if (buffer.length && package && package->type == ACPI_TYPE_PACKAGE && package->package.count) { - status = acpi_extract_power_resources(package, 0, - &ps->resources); - if (ACPI_SUCCESS(status)) + int err = acpi_extract_power_resources(package, 0, + &ps->resources); + if (!err) device->power.flags.power_resources = 1; } ACPI_FREE(buffer.pointer); -- cgit v1.2.3 From 0596a52b8357b25185e06af32973225baeb7196a Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:07 +0100 Subject: ACPI: Use system level attribute of wakeup power resources The system level attribute of ACPI power resources is the lowest system sleep level (S0, S2 etc.) in which the given resource can be "on" (ACPI 5.0, Section 7.1). On the other hand, wakeup power resources have to be "on" for devices depending on them to be able to signal wakeup. Therefore devices cannot wake up the system from sleep states higher than the minimum of the system level attributes of their wakeup power resources. Use the wakeup power resources' system level values to get the deepest system sleep state (highest system sleep level) the given device can wake up the system from. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/internal.h | 1 + drivers/acpi/power.c | 14 ++++++++++++++ drivers/acpi/scan.c | 11 +++++++++++ 3 files changed, 26 insertions(+) (limited to 'drivers/acpi') diff --git a/drivers/acpi/internal.h b/drivers/acpi/internal.h index 8a6c67c9da42..07f61dbd8136 100644 --- a/drivers/acpi/internal.h +++ b/drivers/acpi/internal.h @@ -55,6 +55,7 @@ int acpi_extract_power_resources(union acpi_object *package, unsigned int start, struct list_head *list); int acpi_add_power_resource(acpi_handle handle); void acpi_power_add_remove_device(struct acpi_device *adev, bool add); +int acpi_power_min_system_level(struct list_head *list); int acpi_device_sleep_wake(struct acpi_device *dev, int enable, int sleep_state, int dev_state); int acpi_power_get_inferred_state(struct acpi_device *device, int *state); diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 1600f753fafe..089a7c39348f 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -429,6 +429,20 @@ void acpi_power_add_remove_device(struct acpi_device *adev, bool add) } } +int acpi_power_min_system_level(struct list_head *list) +{ + struct acpi_power_resource_entry *entry; + int system_level = 5; + + list_for_each_entry(entry, list, node) { + struct acpi_power_resource *resource = entry->resource; + + if (system_level > resource->system_level) + system_level = resource->system_level; + } + return system_level; +} + /* -------------------------------------------------------------------------- Device Power Management -------------------------------------------------------------------------- */ diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 0b6a6b4febd6..1fc57a349a3c 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -950,6 +950,17 @@ static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, if (err) goto out; + if (!list_empty(&wakeup->resources)) { + int sleep_state; + + sleep_state = acpi_power_min_system_level(&wakeup->resources); + if (sleep_state < wakeup->sleep_state) { + acpi_handle_warn(handle, "Overriding _PRW sleep state " + "(S%d) by S%d from power resources\n", + (int)wakeup->sleep_state, sleep_state); + wakeup->sleep_state = sleep_state; + } + } acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number); out: -- cgit v1.2.3 From ff0c41942fd9766a158502d8ed6965c8a7726f53 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:08 +0100 Subject: ACPI / PM: Change the way power transitions to D3cold are carried out During power transitions into D3cold from any shallower power states we are supposed to transition the device into D3hot and remove power from it afterward, but the current code in acpi_device_set_power() doesn't work this way. At the same time, though, we need to be careful enough to preserve backwards compatibility for systems that don't distinguish between D3hot and D3cold (e.g. designed before ACPI 4). Modify acpi_device_set_power() so that it works in accordance with the expectations in both cases. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/bus.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/bus.c b/drivers/acpi/bus.c index 1f0d457ecbcf..8e57fc49726e 100644 --- a/drivers/acpi/bus.c +++ b/drivers/acpi/bus.c @@ -270,6 +270,7 @@ int acpi_device_set_power(struct acpi_device *device, int state) int result = 0; acpi_status status = AE_OK; char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' }; + bool cut_power = false; if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) return -EINVAL; @@ -294,9 +295,13 @@ int acpi_device_set_power(struct acpi_device *device, int state) return -ENODEV; } - /* For D3cold we should execute _PS3, not _PS4. */ - if (state == ACPI_STATE_D3_COLD) + /* For D3cold we should first transition into D3hot. */ + if (state == ACPI_STATE_D3_COLD + && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) { + state = ACPI_STATE_D3_HOT; object_name[3] = '3'; + cut_power = true; + } /* * Transition Power @@ -341,6 +346,9 @@ int acpi_device_set_power(struct acpi_device *device, int state) } } + if (cut_power) + result = acpi_power_transition(device, ACPI_STATE_D3_COLD); + end: if (result) printk(KERN_WARNING PREFIX -- cgit v1.2.3 From ad0c3b0e4863185a9f8874a655a8d2999c915131 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:08 +0100 Subject: ACPI / PM: More visible function for retrieving device power states The function used for retrieving ACPI device power states, __acpi_bus_get_power(), is now static, because it is only used internally in drivers/acpi/bus.c. However, it will be used outside of that file going forward, so rename it to acpi_device_get_power(), in analogy with acpi_device_set_power(), add a kerneldoc comment to it and add its header to acpi_bus.h. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/bus.c | 15 ++++++++++++--- include/acpi/acpi_bus.h | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/bus.c b/drivers/acpi/bus.c index 8e57fc49726e..0e1441cc4d7f 100644 --- a/drivers/acpi/bus.c +++ b/drivers/acpi/bus.c @@ -200,7 +200,16 @@ static const char *state_string(int state) } } -static int __acpi_bus_get_power(struct acpi_device *device, int *state) +/** + * acpi_device_get_power - Get power state of an ACPI device. + * @device: Device to get the power state of. + * @state: Place to store the power state of the device. + * + * This function does not update the device's power.state field, but it may + * update its parent's power.state field (when the parent's power state is + * unknown and the device's power state turns out to be D0). + */ +int acpi_device_get_power(struct acpi_device *device, int *state) { int result = ACPI_STATE_UNKNOWN; @@ -397,7 +406,7 @@ int acpi_bus_init_power(struct acpi_device *device) device->power.state = ACPI_STATE_UNKNOWN; - result = __acpi_bus_get_power(device, &state); + result = acpi_device_get_power(device, &state); if (result) return result; @@ -421,7 +430,7 @@ int acpi_bus_update_power(acpi_handle handle, int *state_p) if (result) return result; - result = __acpi_bus_get_power(device, &state); + result = acpi_device_get_power(device, &state); if (result) return result; diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index a272c3156999..9d7c2ca0f1f7 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -331,6 +331,7 @@ acpi_status acpi_bus_get_status_handle(acpi_handle handle, unsigned long long *sta); int acpi_bus_get_status(struct acpi_device *device); int acpi_bus_set_power(acpi_handle handle, int state); +int acpi_device_get_power(struct acpi_device *device, int *state); int acpi_device_set_power(struct acpi_device *device, int state); int acpi_bus_update_power(acpi_handle handle, int *state_p); bool acpi_bus_power_manageable(acpi_handle handle); -- cgit v1.2.3 From 96bfd3cee2a741906b3ef5c1096d2f0a0b8025e0 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:08 +0100 Subject: ACPI / PM: Common string representations of device power states The function returning string representations of ACPI device power states, state_string((), is now static, because it is only used internally in drivers/acpi/bus.c. However, it will be used outside of that file going forward, so rename it to acpi_power_state_string(), add a kerneldoc comment to it and add its header to acpi_bus.h. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/bus.c | 18 ++++++++++++------ include/acpi/acpi_bus.h | 1 + 2 files changed, 13 insertions(+), 6 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/bus.c b/drivers/acpi/bus.c index 0e1441cc4d7f..6c9b16c16660 100644 --- a/drivers/acpi/bus.c +++ b/drivers/acpi/bus.c @@ -182,7 +182,11 @@ EXPORT_SYMBOL(acpi_bus_get_private_data); Power Management -------------------------------------------------------------------------- */ -static const char *state_string(int state) +/** + * acpi_power_state_string - String representation of ACPI device power state. + * @state: ACPI device power state to return the string representation of. + */ +const char *acpi_power_state_string(int state) { switch (state) { case ACPI_STATE_D0: @@ -260,7 +264,7 @@ int acpi_device_get_power(struct acpi_device *device, int *state) out: ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n", - device->pnp.bus_id, state_string(*state))); + device->pnp.bus_id, acpi_power_state_string(*state))); return 0; } @@ -288,13 +292,13 @@ int acpi_device_set_power(struct acpi_device *device, int state) if (state == device->power.state) { ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at %s\n", - state_string(state))); + acpi_power_state_string(state))); return 0; } if (!device->power.states[state].flags.valid) { printk(KERN_WARNING PREFIX "Device does not support %s\n", - state_string(state)); + acpi_power_state_string(state)); return -ENODEV; } if (device->parent && (state < device->parent->power.state)) { @@ -362,12 +366,14 @@ int acpi_device_set_power(struct acpi_device *device, int state) if (result) printk(KERN_WARNING PREFIX "Device [%s] failed to transition to %s\n", - device->pnp.bus_id, state_string(state)); + device->pnp.bus_id, + acpi_power_state_string(state)); else { device->power.state = state; ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] transitioned to %s\n", - device->pnp.bus_id, state_string(state))); + device->pnp.bus_id, + acpi_power_state_string(state))); } return result; diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 9d7c2ca0f1f7..71eceb99d411 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -331,6 +331,7 @@ acpi_status acpi_bus_get_status_handle(acpi_handle handle, unsigned long long *sta); int acpi_bus_get_status(struct acpi_device *device); int acpi_bus_set_power(acpi_handle handle, int state); +const char *acpi_power_state_string(int state); int acpi_device_get_power(struct acpi_device *device, int *state); int acpi_device_set_power(struct acpi_device *device, int state); int acpi_bus_update_power(acpi_handle handle, int *state_p); -- cgit v1.2.3 From 9ce4e607111764673f7a59d7bc87a16ade5c7bba Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:08 +0100 Subject: ACPI / PM: Move device power management functions to device_pm.c Move ACPI device power management functions from drivers/acpi/bus.c to drivers/acpi/device_pm.c. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/bus.c | 293 ----------------------------------------------- drivers/acpi/device_pm.c | 288 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/acpi/internal.h | 1 - include/acpi/acpi_bus.h | 38 ++++++ 4 files changed, 326 insertions(+), 294 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/bus.c b/drivers/acpi/bus.c index 6c9b16c16660..01708a165368 100644 --- a/drivers/acpi/bus.c +++ b/drivers/acpi/bus.c @@ -178,299 +178,6 @@ int acpi_bus_get_private_data(acpi_handle handle, void **data) } EXPORT_SYMBOL(acpi_bus_get_private_data); -/* -------------------------------------------------------------------------- - Power Management - -------------------------------------------------------------------------- */ - -/** - * acpi_power_state_string - String representation of ACPI device power state. - * @state: ACPI device power state to return the string representation of. - */ -const char *acpi_power_state_string(int state) -{ - switch (state) { - case ACPI_STATE_D0: - return "D0"; - case ACPI_STATE_D1: - return "D1"; - case ACPI_STATE_D2: - return "D2"; - case ACPI_STATE_D3_HOT: - return "D3hot"; - case ACPI_STATE_D3_COLD: - return "D3"; - default: - return "(unknown)"; - } -} - -/** - * acpi_device_get_power - Get power state of an ACPI device. - * @device: Device to get the power state of. - * @state: Place to store the power state of the device. - * - * This function does not update the device's power.state field, but it may - * update its parent's power.state field (when the parent's power state is - * unknown and the device's power state turns out to be D0). - */ -int acpi_device_get_power(struct acpi_device *device, int *state) -{ - int result = ACPI_STATE_UNKNOWN; - - if (!device || !state) - return -EINVAL; - - if (!device->flags.power_manageable) { - /* TBD: Non-recursive algorithm for walking up hierarchy. */ - *state = device->parent ? - device->parent->power.state : ACPI_STATE_D0; - goto out; - } - - /* - * Get the device's power state either directly (via _PSC) or - * indirectly (via power resources). - */ - if (device->power.flags.explicit_get) { - unsigned long long psc; - acpi_status status = acpi_evaluate_integer(device->handle, - "_PSC", NULL, &psc); - if (ACPI_FAILURE(status)) - return -ENODEV; - - result = psc; - } - /* The test below covers ACPI_STATE_UNKNOWN too. */ - if (result <= ACPI_STATE_D2) { - ; /* Do nothing. */ - } else if (device->power.flags.power_resources) { - int error = acpi_power_get_inferred_state(device, &result); - if (error) - return error; - } else if (result == ACPI_STATE_D3_HOT) { - result = ACPI_STATE_D3; - } - - /* - * If we were unsure about the device parent's power state up to this - * point, the fact that the device is in D0 implies that the parent has - * to be in D0 too. - */ - if (device->parent && device->parent->power.state == ACPI_STATE_UNKNOWN - && result == ACPI_STATE_D0) - device->parent->power.state = ACPI_STATE_D0; - - *state = result; - - out: - ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n", - device->pnp.bus_id, acpi_power_state_string(*state))); - - return 0; -} - - -/** - * acpi_device_set_power - Set power state of an ACPI device. - * @device: Device to set the power state of. - * @state: New power state to set. - * - * Callers must ensure that the device is power manageable before using this - * function. - */ -int acpi_device_set_power(struct acpi_device *device, int state) -{ - int result = 0; - acpi_status status = AE_OK; - char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' }; - bool cut_power = false; - - if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) - return -EINVAL; - - /* Make sure this is a valid target state */ - - if (state == device->power.state) { - ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at %s\n", - acpi_power_state_string(state))); - return 0; - } - - if (!device->power.states[state].flags.valid) { - printk(KERN_WARNING PREFIX "Device does not support %s\n", - acpi_power_state_string(state)); - return -ENODEV; - } - if (device->parent && (state < device->parent->power.state)) { - printk(KERN_WARNING PREFIX - "Cannot set device to a higher-powered" - " state than parent\n"); - return -ENODEV; - } - - /* For D3cold we should first transition into D3hot. */ - if (state == ACPI_STATE_D3_COLD - && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) { - state = ACPI_STATE_D3_HOT; - object_name[3] = '3'; - cut_power = true; - } - - /* - * Transition Power - * ---------------- - * On transitions to a high-powered state we first apply power (via - * power resources) then evalute _PSx. Conversly for transitions to - * a lower-powered state. - */ - if (state < device->power.state) { - if (device->power.state >= ACPI_STATE_D3_HOT && - state != ACPI_STATE_D0) { - printk(KERN_WARNING PREFIX - "Cannot transition to non-D0 state from D3\n"); - return -ENODEV; - } - if (device->power.flags.power_resources) { - result = acpi_power_transition(device, state); - if (result) - goto end; - } - if (device->power.states[state].flags.explicit_set) { - status = acpi_evaluate_object(device->handle, - object_name, NULL, NULL); - if (ACPI_FAILURE(status)) { - result = -ENODEV; - goto end; - } - } - } else { - if (device->power.states[state].flags.explicit_set) { - status = acpi_evaluate_object(device->handle, - object_name, NULL, NULL); - if (ACPI_FAILURE(status)) { - result = -ENODEV; - goto end; - } - } - if (device->power.flags.power_resources) { - result = acpi_power_transition(device, state); - if (result) - goto end; - } - } - - if (cut_power) - result = acpi_power_transition(device, ACPI_STATE_D3_COLD); - - end: - if (result) - printk(KERN_WARNING PREFIX - "Device [%s] failed to transition to %s\n", - device->pnp.bus_id, - acpi_power_state_string(state)); - else { - device->power.state = state; - ACPI_DEBUG_PRINT((ACPI_DB_INFO, - "Device [%s] transitioned to %s\n", - device->pnp.bus_id, - acpi_power_state_string(state))); - } - - return result; -} -EXPORT_SYMBOL(acpi_device_set_power); - - -int acpi_bus_set_power(acpi_handle handle, int state) -{ - struct acpi_device *device; - int result; - - result = acpi_bus_get_device(handle, &device); - if (result) - return result; - - if (!device->flags.power_manageable) { - ACPI_DEBUG_PRINT((ACPI_DB_INFO, - "Device [%s] is not power manageable\n", - dev_name(&device->dev))); - return -ENODEV; - } - - return acpi_device_set_power(device, state); -} -EXPORT_SYMBOL(acpi_bus_set_power); - - -int acpi_bus_init_power(struct acpi_device *device) -{ - int state; - int result; - - if (!device) - return -EINVAL; - - device->power.state = ACPI_STATE_UNKNOWN; - - result = acpi_device_get_power(device, &state); - if (result) - return result; - - if (device->power.flags.power_resources) - result = acpi_power_on_resources(device, state); - - if (!result) - device->power.state = state; - - return result; -} - - -int acpi_bus_update_power(acpi_handle handle, int *state_p) -{ - struct acpi_device *device; - int state; - int result; - - result = acpi_bus_get_device(handle, &device); - if (result) - return result; - - result = acpi_device_get_power(device, &state); - if (result) - return result; - - result = acpi_device_set_power(device, state); - if (!result && state_p) - *state_p = state; - - return result; -} -EXPORT_SYMBOL_GPL(acpi_bus_update_power); - - -bool acpi_bus_power_manageable(acpi_handle handle) -{ - struct acpi_device *device; - int result; - - result = acpi_bus_get_device(handle, &device); - return result ? false : device->flags.power_manageable; -} - -EXPORT_SYMBOL(acpi_bus_power_manageable); - -bool acpi_bus_can_wakeup(acpi_handle handle) -{ - struct acpi_device *device; - int result; - - result = acpi_bus_get_device(handle, &device); - return result ? false : device->wakeup.flags.valid; -} - -EXPORT_SYMBOL(acpi_bus_can_wakeup); - static void acpi_print_osc_error(acpi_handle handle, struct acpi_osc_context *context, char *error) { diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c index 8be4b29e38aa..8bca7465c78d 100644 --- a/drivers/acpi/device_pm.c +++ b/drivers/acpi/device_pm.c @@ -30,6 +30,12 @@ #include #include +#include + +#include "internal.h" + +#define _COMPONENT ACPI_POWER_COMPONENT +ACPI_MODULE_NAME("device_pm"); static DEFINE_MUTEX(acpi_pm_notifier_lock); @@ -93,6 +99,288 @@ acpi_status acpi_remove_pm_notifier(struct acpi_device *adev, return status; } +/** + * acpi_power_state_string - String representation of ACPI device power state. + * @state: ACPI device power state to return the string representation of. + */ +const char *acpi_power_state_string(int state) +{ + switch (state) { + case ACPI_STATE_D0: + return "D0"; + case ACPI_STATE_D1: + return "D1"; + case ACPI_STATE_D2: + return "D2"; + case ACPI_STATE_D3_HOT: + return "D3hot"; + case ACPI_STATE_D3_COLD: + return "D3"; + default: + return "(unknown)"; + } +} + +/** + * acpi_device_get_power - Get power state of an ACPI device. + * @device: Device to get the power state of. + * @state: Place to store the power state of the device. + * + * This function does not update the device's power.state field, but it may + * update its parent's power.state field (when the parent's power state is + * unknown and the device's power state turns out to be D0). + */ +int acpi_device_get_power(struct acpi_device *device, int *state) +{ + int result = ACPI_STATE_UNKNOWN; + + if (!device || !state) + return -EINVAL; + + if (!device->flags.power_manageable) { + /* TBD: Non-recursive algorithm for walking up hierarchy. */ + *state = device->parent ? + device->parent->power.state : ACPI_STATE_D0; + goto out; + } + + /* + * Get the device's power state either directly (via _PSC) or + * indirectly (via power resources). + */ + if (device->power.flags.explicit_get) { + unsigned long long psc; + acpi_status status = acpi_evaluate_integer(device->handle, + "_PSC", NULL, &psc); + if (ACPI_FAILURE(status)) + return -ENODEV; + + result = psc; + } + /* The test below covers ACPI_STATE_UNKNOWN too. */ + if (result <= ACPI_STATE_D2) { + ; /* Do nothing. */ + } else if (device->power.flags.power_resources) { + int error = acpi_power_get_inferred_state(device, &result); + if (error) + return error; + } else if (result == ACPI_STATE_D3_HOT) { + result = ACPI_STATE_D3; + } + + /* + * If we were unsure about the device parent's power state up to this + * point, the fact that the device is in D0 implies that the parent has + * to be in D0 too. + */ + if (device->parent && device->parent->power.state == ACPI_STATE_UNKNOWN + && result == ACPI_STATE_D0) + device->parent->power.state = ACPI_STATE_D0; + + *state = result; + + out: + ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n", + device->pnp.bus_id, acpi_power_state_string(*state))); + + return 0; +} + +/** + * acpi_device_set_power - Set power state of an ACPI device. + * @device: Device to set the power state of. + * @state: New power state to set. + * + * Callers must ensure that the device is power manageable before using this + * function. + */ +int acpi_device_set_power(struct acpi_device *device, int state) +{ + int result = 0; + acpi_status status = AE_OK; + char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' }; + bool cut_power = false; + + if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) + return -EINVAL; + + /* Make sure this is a valid target state */ + + if (state == device->power.state) { + ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at %s\n", + acpi_power_state_string(state))); + return 0; + } + + if (!device->power.states[state].flags.valid) { + printk(KERN_WARNING PREFIX "Device does not support %s\n", + acpi_power_state_string(state)); + return -ENODEV; + } + if (device->parent && (state < device->parent->power.state)) { + printk(KERN_WARNING PREFIX + "Cannot set device to a higher-powered" + " state than parent\n"); + return -ENODEV; + } + + /* For D3cold we should first transition into D3hot. */ + if (state == ACPI_STATE_D3_COLD + && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) { + state = ACPI_STATE_D3_HOT; + object_name[3] = '3'; + cut_power = true; + } + + /* + * Transition Power + * ---------------- + * On transitions to a high-powered state we first apply power (via + * power resources) then evalute _PSx. Conversly for transitions to + * a lower-powered state. + */ + if (state < device->power.state) { + if (device->power.state >= ACPI_STATE_D3_HOT && + state != ACPI_STATE_D0) { + printk(KERN_WARNING PREFIX + "Cannot transition to non-D0 state from D3\n"); + return -ENODEV; + } + if (device->power.flags.power_resources) { + result = acpi_power_transition(device, state); + if (result) + goto end; + } + if (device->power.states[state].flags.explicit_set) { + status = acpi_evaluate_object(device->handle, + object_name, NULL, NULL); + if (ACPI_FAILURE(status)) { + result = -ENODEV; + goto end; + } + } + } else { + if (device->power.states[state].flags.explicit_set) { + status = acpi_evaluate_object(device->handle, + object_name, NULL, NULL); + if (ACPI_FAILURE(status)) { + result = -ENODEV; + goto end; + } + } + if (device->power.flags.power_resources) { + result = acpi_power_transition(device, state); + if (result) + goto end; + } + } + + if (cut_power) + result = acpi_power_transition(device, ACPI_STATE_D3_COLD); + + end: + if (result) + printk(KERN_WARNING PREFIX + "Device [%s] failed to transition to %s\n", + device->pnp.bus_id, + acpi_power_state_string(state)); + else { + device->power.state = state; + ACPI_DEBUG_PRINT((ACPI_DB_INFO, + "Device [%s] transitioned to %s\n", + device->pnp.bus_id, + acpi_power_state_string(state))); + } + + return result; +} +EXPORT_SYMBOL(acpi_device_set_power); + +int acpi_bus_set_power(acpi_handle handle, int state) +{ + struct acpi_device *device; + int result; + + result = acpi_bus_get_device(handle, &device); + if (result) + return result; + + if (!device->flags.power_manageable) { + ACPI_DEBUG_PRINT((ACPI_DB_INFO, + "Device [%s] is not power manageable\n", + dev_name(&device->dev))); + return -ENODEV; + } + + return acpi_device_set_power(device, state); +} +EXPORT_SYMBOL(acpi_bus_set_power); + +int acpi_bus_init_power(struct acpi_device *device) +{ + int state; + int result; + + if (!device) + return -EINVAL; + + device->power.state = ACPI_STATE_UNKNOWN; + + result = acpi_device_get_power(device, &state); + if (result) + return result; + + if (device->power.flags.power_resources) + result = acpi_power_on_resources(device, state); + + if (!result) + device->power.state = state; + + return result; +} + +int acpi_bus_update_power(acpi_handle handle, int *state_p) +{ + struct acpi_device *device; + int state; + int result; + + result = acpi_bus_get_device(handle, &device); + if (result) + return result; + + result = acpi_device_get_power(device, &state); + if (result) + return result; + + result = acpi_device_set_power(device, state); + if (!result && state_p) + *state_p = state; + + return result; +} +EXPORT_SYMBOL_GPL(acpi_bus_update_power); + +bool acpi_bus_power_manageable(acpi_handle handle) +{ + struct acpi_device *device; + int result; + + result = acpi_bus_get_device(handle, &device); + return result ? false : device->flags.power_manageable; +} +EXPORT_SYMBOL(acpi_bus_power_manageable); + +bool acpi_bus_can_wakeup(acpi_handle handle) +{ + struct acpi_device *device; + int result; + + result = acpi_bus_get_device(handle, &device); + return result ? false : device->wakeup.flags.valid; +} +EXPORT_SYMBOL(acpi_bus_can_wakeup); + /** * acpi_device_power_state - Get preferred power state of ACPI device. * @dev: Device whose preferred target power state to return. diff --git a/drivers/acpi/internal.h b/drivers/acpi/internal.h index 07f61dbd8136..1f004f35bc67 100644 --- a/drivers/acpi/internal.h +++ b/drivers/acpi/internal.h @@ -61,7 +61,6 @@ int acpi_device_sleep_wake(struct acpi_device *dev, int acpi_power_get_inferred_state(struct acpi_device *device, int *state); int acpi_power_on_resources(struct acpi_device *device, int state); int acpi_power_transition(struct acpi_device *device, int state); -int acpi_bus_init_power(struct acpi_device *device); int acpi_wakeup_device_init(void); void acpi_early_processor_set_pdc(void); diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 71eceb99d411..fca1b9cb27d9 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -330,13 +330,51 @@ void acpi_bus_data_handler(acpi_handle handle, void *context); acpi_status acpi_bus_get_status_handle(acpi_handle handle, unsigned long long *sta); int acpi_bus_get_status(struct acpi_device *device); + +#ifdef CONFIG_PM int acpi_bus_set_power(acpi_handle handle, int state); const char *acpi_power_state_string(int state); int acpi_device_get_power(struct acpi_device *device, int *state); int acpi_device_set_power(struct acpi_device *device, int state); +int acpi_bus_init_power(struct acpi_device *device); 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); +#else /* !CONFIG_PM */ +static inline int acpi_bus_set_power(acpi_handle handle, int state) +{ + return 0; +} +static inline const char *acpi_power_state_string(int state) +{ + return "D0"; +} +static inline int acpi_device_get_power(struct acpi_device *device, int *state) +{ + return 0; +} +static inline int acpi_device_set_power(struct acpi_device *device, int state) +{ + return 0; +} +static inline int acpi_bus_init_power(struct acpi_device *device) +{ + return 0; +} +static inline int acpi_bus_update_power(acpi_handle handle, int *state_p) +{ + return 0; +} +static inline bool acpi_bus_power_manageable(acpi_handle handle) +{ + return false; +} +static inline bool acpi_bus_can_wakeup(acpi_handle handle) +{ + return false; +} +#endif /* !CONFIG_PM */ + #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 From 02040f0bba996f93e2a237089aff343515b49fcf Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 17 Jan 2013 14:11:09 +0100 Subject: ACPI / PM: Consolidate suspend-specific and hibernate-specific code Move some suspend-specific and hibernate-specific code from acpi_sleep_init() into separate functions to get rid of explicit #ifdefs in acpi_sleep_init(). Use pr_info() to start and pr_cont() to continue printing the supported ACPI sleep states line. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/sleep.c | 87 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 36 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/sleep.c b/drivers/acpi/sleep.c index 4ef0328579cc..277aa825edd9 100644 --- a/drivers/acpi/sleep.c +++ b/drivers/acpi/sleep.c @@ -579,7 +579,28 @@ static const struct platform_suspend_ops acpi_suspend_ops_old = { .end = acpi_pm_end, .recover = acpi_pm_finish, }; -#endif /* CONFIG_SUSPEND */ + +static void acpi_sleep_suspend_setup(void) +{ + int i; + + for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) { + acpi_status status; + u8 type_a, type_b; + + status = acpi_get_sleep_type_data(i, &type_a, &type_b); + if (ACPI_SUCCESS(status)) { + sleep_states[i] = 1; + pr_cont(" S%d", i); + } + } + + suspend_set_ops(old_suspend_ordering ? + &acpi_suspend_ops_old : &acpi_suspend_ops); +} +#else /* !CONFIG_SUSPEND */ +static inline void acpi_sleep_suspend_setup(void) {} +#endif /* !CONFIG_SUSPEND */ #ifdef CONFIG_HIBERNATION static unsigned long s4_hardware_signature; @@ -700,7 +721,30 @@ static const struct platform_hibernation_ops acpi_hibernation_ops_old = { .restore_cleanup = acpi_pm_thaw, .recover = acpi_pm_finish, }; -#endif /* CONFIG_HIBERNATION */ + +static void acpi_sleep_hibernate_setup(void) +{ + acpi_status status; + u8 type_a, type_b; + + status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b); + if (ACPI_FAILURE(status)) + return; + + hibernation_set_ops(old_suspend_ordering ? + &acpi_hibernation_ops_old : &acpi_hibernation_ops); + sleep_states[ACPI_STATE_S4] = 1; + pr_cont(KERN_CONT " S4"); + if (nosigcheck) + return; + + acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs); + if (facs) + s4_hardware_signature = facs->hardware_signature; +} +#else /* !CONFIG_HIBERNATION */ +static inline void acpi_sleep_hibernate_setup(void) {} +#endif /* !CONFIG_HIBERNATION */ int acpi_suspend(u32 acpi_state) { @@ -736,9 +780,6 @@ int __init acpi_sleep_init(void) { acpi_status status; u8 type_a, type_b; -#ifdef CONFIG_SUSPEND - int i = 0; -#endif if (acpi_disabled) return 0; @@ -746,45 +787,19 @@ int __init acpi_sleep_init(void) acpi_sleep_dmi_check(); sleep_states[ACPI_STATE_S0] = 1; - printk(KERN_INFO PREFIX "(supports S0"); + pr_info(PREFIX "(supports S0"); -#ifdef CONFIG_SUSPEND - for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) { - status = acpi_get_sleep_type_data(i, &type_a, &type_b); - if (ACPI_SUCCESS(status)) { - sleep_states[i] = 1; - printk(KERN_CONT " S%d", i); - } - } + acpi_sleep_suspend_setup(); + acpi_sleep_hibernate_setup(); - suspend_set_ops(old_suspend_ordering ? - &acpi_suspend_ops_old : &acpi_suspend_ops); -#endif - -#ifdef CONFIG_HIBERNATION - status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b); - if (ACPI_SUCCESS(status)) { - hibernation_set_ops(old_suspend_ordering ? - &acpi_hibernation_ops_old : &acpi_hibernation_ops); - sleep_states[ACPI_STATE_S4] = 1; - printk(KERN_CONT " S4"); - if (!nosigcheck) { - acpi_get_table(ACPI_SIG_FACS, 1, - (struct acpi_table_header **)&facs); - if (facs) - s4_hardware_signature = - facs->hardware_signature; - } - } -#endif status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b); if (ACPI_SUCCESS(status)) { sleep_states[ACPI_STATE_S5] = 1; - printk(KERN_CONT " S5"); + pr_cont(" S5"); pm_power_off_prepare = acpi_power_off_prepare; pm_power_off = acpi_power_off; } - printk(KERN_CONT ")\n"); + pr_cont(")\n"); /* * Register the tts_notifier to reboot notifier list so that the _TTS * object can also be evaluated when the system enters S5. -- cgit v1.2.3 From 4d56410b955c3f4f7651a088e1c7a19a0d5d4e4c Mon Sep 17 00:00:00 2001 From: Mika Westerberg Date: Mon, 14 Jan 2013 20:13:37 +0000 Subject: ACPI / PM: remove leading whitespace from #ifdef It is there probably due to an accident, get rid of it so that the format is consistent across the file. Signed-off-by: Mika Westerberg Signed-off-by: Rafael J. Wysocki --- drivers/acpi/device_pm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c index 8bca7465c78d..43116cdbabf9 100644 --- a/drivers/acpi/device_pm.c +++ b/drivers/acpi/device_pm.c @@ -592,7 +592,7 @@ static inline void acpi_wakeup_device(acpi_handle handle, u32 event, void *context) {} #endif /* CONFIG_PM_RUNTIME */ - #ifdef CONFIG_PM_SLEEP +#ifdef CONFIG_PM_SLEEP /** * __acpi_device_sleep_wake - Enable or disable device to wake up the system. * @dev: Device to enable/desible to wake up the system. -- cgit v1.2.3 From a2367807b8d2c0aca5afb92fead2537dcd3d10b0 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Tue, 22 Jan 2013 12:54:38 +0100 Subject: ACPI / PM: Make acpi_bus_init_power() more robust The ACPI specification requires the _PSC method to be present under a device object if its power state cannot be inferred from the states of power resources used by it (ACPI 5, Section 7.6.2). However, it also requires that (for power states D0-D2 and D3hot) if the _PSn (n = 0, 1, 2, 3) method is present under the device object, it also must be executed after the power resources have been set appropriately for the device to go into power state Dn (D3 means D3hot in this case). Thus it is not clear from the specification whether or not the _PSn method should be executed if the initial configuraion of power resources used by the device indicates power state Dn and the _PSC method is not present. The current implementation of acpi_bus_init_power() is based on the assumption that it should not be necessary to execute _PSn in the above situation, but experience shows that in fact that assumption need not be satisfied. For this reason, make acpi_bus_init_power() always execute _PSn if the initial configuration of device power resources indicates power state Dn. Reported-and-tested-by: Mika Westerberg Signed-off-by: Rafael J. Wysocki --- drivers/acpi/device_pm.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c index 43116cdbabf9..c87853f583d8 100644 --- a/drivers/acpi/device_pm.c +++ b/drivers/acpi/device_pm.c @@ -330,13 +330,23 @@ int acpi_bus_init_power(struct acpi_device *device) if (result) return result; - if (device->power.flags.power_resources) + if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) { result = acpi_power_on_resources(device, state); + if (result) + return result; - if (!result) - device->power.state = state; + if (device->power.states[state].flags.explicit_set) { + char method[5] = { '_', 'P', 'S', '0' + state, '\0' }; + acpi_status status; - return result; + status = acpi_evaluate_object(device->handle, method, + NULL, NULL); + if (ACPI_FAILURE(status)) + return -ENODEV; + } + } + device->power.state = state; + return 0; } int acpi_bus_update_power(acpi_handle handle, int *state_p) -- cgit v1.2.3 From 9c0f45e388fb9f9003ea22f98b84ffbab65ba554 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Tue, 22 Jan 2013 12:55:52 +0100 Subject: ACPI / PM: Introduce helper for executing _PSn methods To reduce code duplication between acpi_device_set_power() and acpi_bus_init_power(), introduce a new helper function for executing ACPI devices' _PSn (n = 0..3) methods, acpi_dev_pm_explicit_set(). Signed-off-by: Rafael J. Wysocki --- drivers/acpi/device_pm.c | 51 ++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c index c87853f583d8..d7f3908c2e88 100644 --- a/drivers/acpi/device_pm.c +++ b/drivers/acpi/device_pm.c @@ -186,6 +186,19 @@ int acpi_device_get_power(struct acpi_device *device, int *state) return 0; } +static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state) +{ + if (adev->power.states[state].flags.explicit_set) { + char method[5] = { '_', 'P', 'S', '0' + state, '\0' }; + acpi_status status; + + status = acpi_evaluate_object(adev->handle, method, NULL, NULL); + if (ACPI_FAILURE(status)) + return -ENODEV; + } + return 0; +} + /** * acpi_device_set_power - Set power state of an ACPI device. * @device: Device to set the power state of. @@ -197,8 +210,6 @@ int acpi_device_get_power(struct acpi_device *device, int *state) int acpi_device_set_power(struct acpi_device *device, int state) { int result = 0; - acpi_status status = AE_OK; - char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' }; bool cut_power = false; if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) @@ -228,7 +239,6 @@ int acpi_device_set_power(struct acpi_device *device, int state) if (state == ACPI_STATE_D3_COLD && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) { state = ACPI_STATE_D3_HOT; - object_name[3] = '3'; cut_power = true; } @@ -251,23 +261,14 @@ int acpi_device_set_power(struct acpi_device *device, int state) if (result) goto end; } - if (device->power.states[state].flags.explicit_set) { - status = acpi_evaluate_object(device->handle, - object_name, NULL, NULL); - if (ACPI_FAILURE(status)) { - result = -ENODEV; - goto end; - } - } + result = acpi_dev_pm_explicit_set(device, state); + if (result) + goto end; } else { - if (device->power.states[state].flags.explicit_set) { - status = acpi_evaluate_object(device->handle, - object_name, NULL, NULL); - if (ACPI_FAILURE(status)) { - result = -ENODEV; - goto end; - } - } + result = acpi_dev_pm_explicit_set(device, state); + if (result) + goto end; + if (device->power.flags.power_resources) { result = acpi_power_transition(device, state); if (result) @@ -335,15 +336,9 @@ int acpi_bus_init_power(struct acpi_device *device) if (result) return result; - if (device->power.states[state].flags.explicit_set) { - char method[5] = { '_', 'P', 'S', '0' + state, '\0' }; - acpi_status status; - - status = acpi_evaluate_object(device->handle, method, - NULL, NULL); - if (ACPI_FAILURE(status)) - return -ENODEV; - } + result = acpi_dev_pm_explicit_set(device, state); + if (result) + return result; } device->power.state = state; return 0; -- cgit v1.2.3 From e78adb7595a9d585c60a7497345cb6eaeaaacefb Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Tue, 22 Jan 2013 12:56:04 +0100 Subject: ACPI / PM: Always evaluate _PSn after setting power resources The ACPI specitication (ACPI 5, Sections 7.2.8 - 7.2.11) requires that the _PSn (n = 0..3) method, if present, be executed after the power resources for the given device power state have been set appropriately. However, acpi_device_set_power() does that only if the new power state is going to be higher-power (lower-number) than the power state the device is in already. Otherwise, the ordering is reverse to protect against situations in which _PSn might access device registers unavailable after configuring the power resources for power state Dn (D3 meaning D3hot). Such situations are very unlikely to happen, though, and _PSn may actually be implemented with the assumption that power resources have been configured for power state Dn in advance, so change the code to follow the specification literally. This change was previously porposed in a different form by Lv Zheng. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/device_pm.c | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c index d7f3908c2e88..2ce07cee0434 100644 --- a/drivers/acpi/device_pm.c +++ b/drivers/acpi/device_pm.c @@ -242,50 +242,38 @@ int acpi_device_set_power(struct acpi_device *device, int state) cut_power = true; } + if (state < device->power.state && state != ACPI_STATE_D0 + && device->power.state >= ACPI_STATE_D3_HOT) { + printk(KERN_WARNING PREFIX + "Cannot transition to non-D0 state from D3\n"); + return -ENODEV; + } + /* * Transition Power * ---------------- - * On transitions to a high-powered state we first apply power (via - * power resources) then evalute _PSx. Conversly for transitions to - * a lower-powered state. + * In accordance with the ACPI specification first apply power (via + * power resources) and then evalute _PSx. */ - if (state < device->power.state) { - if (device->power.state >= ACPI_STATE_D3_HOT && - state != ACPI_STATE_D0) { - printk(KERN_WARNING PREFIX - "Cannot transition to non-D0 state from D3\n"); - return -ENODEV; - } - if (device->power.flags.power_resources) { - result = acpi_power_transition(device, state); - if (result) - goto end; - } - result = acpi_dev_pm_explicit_set(device, state); + if (device->power.flags.power_resources) { + result = acpi_power_transition(device, state); if (result) goto end; - } else { - result = acpi_dev_pm_explicit_set(device, state); - if (result) - goto end; - - if (device->power.flags.power_resources) { - result = acpi_power_transition(device, state); - if (result) - goto end; - } } + result = acpi_dev_pm_explicit_set(device, state); + if (result) + goto end; if (cut_power) result = acpi_power_transition(device, ACPI_STATE_D3_COLD); - end: - if (result) + end: + if (result) { printk(KERN_WARNING PREFIX "Device [%s] failed to transition to %s\n", device->pnp.bus_id, acpi_power_state_string(state)); - else { + } else { device->power.state = state; ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] transitioned to %s\n", -- cgit v1.2.3 From 87e753b0065f94314ebdacf6593a172cdd7839c8 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Tue, 22 Jan 2013 12:56:16 +0100 Subject: ACPI / PM: Sanitize checks in acpi_power_on_resources() After the only user of acpi_power_on_resources(), acpi_bus_init_power(), has been changed to avoid calling it for state equal to ACPI_STATE_D3_COLD, it doesn't have to special case that state any more. For this reason, modify the checks in acpi_power_on_resources() so that it returns -EINVAL for ACPI_STATE_D3_COLD as it should. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/power.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 089a7c39348f..6db261c237d4 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -622,12 +622,9 @@ int acpi_power_get_inferred_state(struct acpi_device *device, int *state) int acpi_power_on_resources(struct acpi_device *device, int state) { - if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_COLD) + if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT) return -EINVAL; - if (state == ACPI_STATE_D3_COLD) - return 0; - return acpi_power_on_list(&device->power.states[state].resources); } -- cgit v1.2.3 From 898fee4f6ed52b5b5dd159b221d2ad7ce40ae2dd Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Tue, 22 Jan 2013 12:56:26 +0100 Subject: ACPI / PM: Use string "D3cold" to represent ACPI_STATE_D3_COLD Make acpi_power_state_string() return "D3cold" as the string representation of ACPI power state D3cold instead of "D3" returned currently, which is confusing. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/device_pm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c index 2ce07cee0434..61ae99b09f1c 100644 --- a/drivers/acpi/device_pm.c +++ b/drivers/acpi/device_pm.c @@ -115,7 +115,7 @@ const char *acpi_power_state_string(int state) case ACPI_STATE_D3_HOT: return "D3hot"; case ACPI_STATE_D3_COLD: - return "D3"; + return "D3cold"; default: return "(unknown)"; } -- cgit v1.2.3 From e5656271b0221a53e9f74856385112fdcec0dd60 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Tue, 22 Jan 2013 12:56:35 +0100 Subject: ACPI / PM: Fix device power state value after transitions to D3cold When a transition to the D3cold power state is requested, acpi_device_set_power() first carries out a transition to D3hot and then turns off the device's power resources. However, it fails to update the device's power.state field appropriately and D3hot is stored in it as a result. Fix this, but make sure that the device's power state will be D3hot if its power resources cannot be turned off in the final step. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/device_pm.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c index 61ae99b09f1c..4cbc9505b365 100644 --- a/drivers/acpi/device_pm.c +++ b/drivers/acpi/device_pm.c @@ -264,8 +264,11 @@ int acpi_device_set_power(struct acpi_device *device, int state) if (result) goto end; - if (cut_power) - result = acpi_power_transition(device, ACPI_STATE_D3_COLD); + if (cut_power) { + device->power.state = state; + state = ACPI_STATE_D3_COLD; + result = acpi_power_transition(device, state); + } end: if (result) { -- cgit v1.2.3 From cf860be639d86ed77af179c925085ae0721ae602 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 24 Jan 2013 12:49:49 +0100 Subject: ACPI / scan: Prevent device add uevents from racing with user space ACPI core adds sysfs device files after the given devices have been registered with device_register(), which is not appropriate, because it may lead to race conditions with user space tools using those files. Fix the problem by delaying the KOBJ_ADD uevent for ACPI devices until after all of the devices' sysfs files have been created. This also fixes a use-after-free in acpi_device_unregister(). Signed-off-by: Rafael J. Wysocki Acked-by: Greg Kroah-Hartman --- drivers/acpi/internal.h | 5 +++-- drivers/acpi/power.c | 3 ++- drivers/acpi/scan.c | 20 +++++++++++++++----- 3 files changed, 20 insertions(+), 8 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/internal.h b/drivers/acpi/internal.h index 1f004f35bc67..c5a61cd6c1a5 100644 --- a/drivers/acpi/internal.h +++ b/drivers/acpi/internal.h @@ -40,10 +40,11 @@ static inline void acpi_debugfs_init(void) { return; } #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \ ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING) -int acpi_device_register(struct acpi_device *device, - void (*release)(struct device *)); +int acpi_device_add(struct acpi_device *device, + void (*release)(struct device *)); void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, int type, unsigned long long sta); +void acpi_device_add_finalize(struct acpi_device *device); void acpi_free_ids(struct acpi_device *device); /* -------------------------------------------------------------------------- diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 6db261c237d4..3f16dd4db23e 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -721,13 +721,14 @@ int acpi_add_power_resource(acpi_handle handle) acpi_device_bid(device), state ? "on" : "off"); device->flags.match_driver = true; - result = acpi_device_register(device, acpi_release_power_resource); + result = acpi_device_add(device, acpi_release_power_resource); if (result) goto err; mutex_lock(&power_resource_list_lock); list_add(&resource->list_node, &acpi_power_resource_list); mutex_unlock(&power_resource_list_lock); + acpi_device_add_finalize(device); return 0; err: diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 1fc57a349a3c..8b3b18846c8c 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -634,8 +634,8 @@ struct bus_type acpi_bus_type = { .uevent = acpi_device_uevent, }; -int acpi_device_register(struct acpi_device *device, - void (*release)(struct device *)) +int acpi_device_add(struct acpi_device *device, + void (*release)(struct device *)) { int result; struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id; @@ -705,7 +705,7 @@ int acpi_device_register(struct acpi_device *device, device->dev.parent = &device->parent->dev; device->dev.bus = &acpi_bus_type; device->dev.release = release; - result = device_register(&device->dev); + result = device_add(&device->dev); if (result) { dev_err(&device->dev, "Error registering device\n"); goto err; @@ -744,12 +744,13 @@ static void acpi_device_unregister(struct acpi_device *device) acpi_power_add_remove_device(device, false); acpi_device_remove_files(device); - device_unregister(&device->dev); + device_del(&device->dev); /* * Drop the reference counts of all power resources the device depends * on and turn off the ones that have no more references. */ acpi_power_transition(device, ACPI_STATE_D3_COLD); + put_device(&device->dev); } /* -------------------------------------------------------------------------- @@ -1391,6 +1392,14 @@ void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, acpi_device_get_busid(device); acpi_device_set_id(device); acpi_bus_get_flags(device); + device_initialize(&device->dev); + dev_set_uevent_suppress(&device->dev, true); +} + +void acpi_device_add_finalize(struct acpi_device *device) +{ + dev_set_uevent_suppress(&device->dev, false); + kobject_uevent(&device->dev.kobj, KOBJ_ADD); } static int acpi_add_single_object(struct acpi_device **child, @@ -1412,13 +1421,14 @@ static int acpi_add_single_object(struct acpi_device **child, acpi_bus_get_wakeup_device_flags(device); device->flags.match_driver = match_driver; - result = acpi_device_register(device, acpi_device_release); + result = acpi_device_add(device, acpi_device_release); if (result) { acpi_device_release(&device->dev); return result; } acpi_power_add_remove_device(device, true); + acpi_device_add_finalize(device); acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n", dev_name(&device->dev), (char *) buffer.pointer, -- cgit v1.2.3 From 836aedb1414d4724b2ec68dd19810960c593720c Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 24 Jan 2013 12:49:59 +0100 Subject: ACPI / PM: Expose power states of ACPI devices to user space Make it possible to retrieve the current power state of a device with ACPI power management from user space via sysfs by adding two new attributes, power_state and real_power_state, to the sysfs directory associated with the struct acpi_device object representing the device's ACPI node. Signed-off-by: Rafael J. Wysocki Acked-by: Greg Kroah-Hartman --- .../ABI/testing/sysfs-devices-power_state | 20 +++++++++ .../ABI/testing/sysfs-devices-real_power_state | 23 ++++++++++ drivers/acpi/scan.c | 49 +++++++++++++++++++++- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 Documentation/ABI/testing/sysfs-devices-power_state create mode 100644 Documentation/ABI/testing/sysfs-devices-real_power_state (limited to 'drivers/acpi') diff --git a/Documentation/ABI/testing/sysfs-devices-power_state b/Documentation/ABI/testing/sysfs-devices-power_state new file mode 100644 index 000000000000..7ad9546748f0 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-devices-power_state @@ -0,0 +1,20 @@ +What: /sys/devices/.../power_state +Date: January 2013 +Contact: Rafael J. Wysocki +Description: + The /sys/devices/.../power_state attribute is only present for + device objects representing ACPI device nodes that provide power + management methods. + + If present, it contains a string representing the current ACPI + power state of the given device node. Its possible values, + "D0", "D1", "D2", "D3hot", and "D3cold", reflect the power state + names defined by the ACPI specification (ACPI 4 and above). + + If the device node uses shared ACPI power resources, this state + determines a list of power resources required not to be turned + off. However, some power resources needed by the device node in + higher-power (lower-number) states may also be ON because of + some other devices using them at the moment. + + This attribute is read-only. diff --git a/Documentation/ABI/testing/sysfs-devices-real_power_state b/Documentation/ABI/testing/sysfs-devices-real_power_state new file mode 100644 index 000000000000..8b3527c82a7d --- /dev/null +++ b/Documentation/ABI/testing/sysfs-devices-real_power_state @@ -0,0 +1,23 @@ +What: /sys/devices/.../real_power_state +Date: January 2013 +Contact: Rafael J. Wysocki +Description: + The /sys/devices/.../real_power_state attribute is only present + for device objects representing ACPI device nodes that provide + power management methods and use ACPI power resources for power + management. + + If present, it contains a string representing the real ACPI + power state of the given device node as returned by the _PSC + control method or inferred from the configuration of power + resources. Its possible values, "D0", "D1", "D2", "D3hot", and + "D3cold", reflect the power state names defined by the ACPI + specification (ACPI 4 and above). + + In some situations the value of this attribute may be different + from the value of the /sys/devices/.../power_state attribute for + the same device object. If that happens, some shared power + resources used by the device node are only ON because of some + other devices using them at the moment. + + This attribute is read-only. diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 8b3b18846c8c..9761d589f3f5 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -178,6 +178,32 @@ err_out: } EXPORT_SYMBOL(acpi_bus_hot_remove_device); +static ssize_t real_power_state_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct acpi_device *adev = to_acpi_device(dev); + int state; + int ret; + + ret = acpi_device_get_power(adev, &state); + if (ret) + return ret; + + return sprintf(buf, "%s\n", acpi_power_state_string(state)); +} + +static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL); + +static ssize_t power_state_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct acpi_device *adev = to_acpi_device(dev); + + return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state)); +} + +static DEVICE_ATTR(power_state, 0444, power_state_show, NULL); + static ssize_t acpi_eject_store(struct device *d, struct device_attribute *attr, const char *buf, size_t count) @@ -369,8 +395,22 @@ static int acpi_device_setup_files(struct acpi_device *dev) * hot-removal function from userland. */ status = acpi_get_handle(dev->handle, "_EJ0", &temp); - if (ACPI_SUCCESS(status)) + if (ACPI_SUCCESS(status)) { result = device_create_file(&dev->dev, &dev_attr_eject); + if (result) + return result; + } + + if (dev->flags.power_manageable) { + result = device_create_file(&dev->dev, &dev_attr_power_state); + if (result) + return result; + + if (dev->power.flags.power_resources) + result = device_create_file(&dev->dev, + &dev_attr_real_power_state); + } + end: return result; } @@ -380,6 +420,13 @@ static void acpi_device_remove_files(struct acpi_device *dev) acpi_status status; acpi_handle temp; + if (dev->flags.power_manageable) { + device_remove_file(&dev->dev, &dev_attr_power_state); + if (dev->power.flags.power_resources) + device_remove_file(&dev->dev, + &dev_attr_real_power_state); + } + /* * If device has _STR, remove 'description' file */ -- cgit v1.2.3 From b1c0f99bfb89cd9b42e3119ab822a8102fa87909 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Thu, 24 Jan 2013 12:50:09 +0100 Subject: ACPI / PM: Expose current status of ACPI power resources Since ACPI power resources are going to be used more extensively on new hardware platforms, it becomes necessary for user space (powertop in particular) to observe some properties of those resources for diagnostics purposes. For this reason, expose the current status of each ACPI power resource to user space via sysfs by adding a new resource_in_use attribute to the sysfs directory representing the given power resource. Signed-off-by: Rafael J. Wysocki Acked-by: Greg Kroah-Hartman --- .../ABI/testing/sysfs-devices-resource_in_use | 12 ++++++++++ drivers/acpi/power.c | 26 +++++++++++++++++++++- drivers/acpi/scan.c | 3 +++ include/acpi/acpi_bus.h | 1 + 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 Documentation/ABI/testing/sysfs-devices-resource_in_use (limited to 'drivers/acpi') diff --git a/Documentation/ABI/testing/sysfs-devices-resource_in_use b/Documentation/ABI/testing/sysfs-devices-resource_in_use new file mode 100644 index 000000000000..b4a3bc5922a3 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-devices-resource_in_use @@ -0,0 +1,12 @@ +What: /sys/devices/.../resource_in_use +Date: January 2013 +Contact: Rafael J. Wysocki +Description: + The /sys/devices/.../resource_in_use attribute is only present + for device objects representing ACPI power resources. + + If present, it contains a number (0 or 1) representing the + current status of the given power resource (0 means that the + resource is not in use and therefore it has been turned off). + + This attribute is read-only. diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 3f16dd4db23e..946720a4db57 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -87,6 +87,12 @@ static DEFINE_MUTEX(power_resource_list_lock); Power Resource Management -------------------------------------------------------------------------- */ +static inline +struct acpi_power_resource *to_power_resource(struct acpi_device *device) +{ + return container_of(device, struct acpi_power_resource, device); +} + static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) { struct acpi_device *device; @@ -94,7 +100,7 @@ static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) if (acpi_bus_get_device(handle, &device)) return NULL; - return container_of(device, struct acpi_power_resource, device); + return to_power_resource(device); } static int acpi_power_resources_list_add(acpi_handle handle, @@ -678,6 +684,21 @@ static void acpi_release_power_resource(struct device *dev) kfree(resource); } +static ssize_t acpi_power_in_use_show(struct device *dev, + struct device_attribute *attr, + char *buf) { + struct acpi_power_resource *resource; + + resource = to_power_resource(to_acpi_device(dev)); + return sprintf(buf, "%u\n", !!resource->ref_count); +} +static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL); + +static void acpi_power_sysfs_remove(struct acpi_device *device) +{ + device_remove_file(&device->dev, &dev_attr_resource_in_use); +} + int acpi_add_power_resource(acpi_handle handle) { struct acpi_power_resource *resource; @@ -725,6 +746,9 @@ int acpi_add_power_resource(acpi_handle handle) if (result) goto err; + if (!device_create_file(&device->dev, &dev_attr_resource_in_use)) + device->remove = acpi_power_sysfs_remove; + mutex_lock(&power_resource_list_lock); list_add(&resource->list_node, &acpi_power_resource_list); mutex_unlock(&power_resource_list_lock); diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 9761d589f3f5..9801837876b7 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -791,6 +791,9 @@ static void acpi_device_unregister(struct acpi_device *device) acpi_power_add_remove_device(device, false); acpi_device_remove_files(device); + if (device->remove) + device->remove(device); + device_del(&device->dev); /* * Drop the reference counts of all power resources the device depends diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index fca1b9cb27d9..aef56a9f4e70 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -280,6 +280,7 @@ struct acpi_device { struct mutex physical_node_lock; DECLARE_BITMAP(physical_node_id_bitmap, ACPI_MAX_PHYSICAL_NODE); struct list_head power_dependent; + void (*remove)(struct acpi_device *); }; static inline void *acpi_driver_data(struct acpi_device *d) -- cgit v1.2.3 From 18a387099b3e3fd901cc706f708b163aa45347b6 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Fri, 25 Jan 2013 21:51:32 +0100 Subject: ACPI / PM: Expose lists of device power resources to user space Since ACPI power resources are going to be used more extensively on new hardware platforms, it is necessary to allow user space (powertop in particular) to look at the lists of power resources corresponding to different power states of devices for diagnostics and control purposes. For this reason, for each power state of an ACPI device node using power resources create a special attribute group under the device node's directory in sysfs containing links to sysfs directories representing the power resources in that list. The names of the new attribute groups are "power_resources_", where is the state name i.e. "D0", "D1", "D2", or "D3hot". Signed-off-by: Rafael J. Wysocki Acked-by: Greg Kroah-Hartman --- .../ABI/testing/sysfs-devices-power_resources_D0 | 13 +++ .../ABI/testing/sysfs-devices-power_resources_D1 | 14 +++ .../ABI/testing/sysfs-devices-power_resources_D2 | 14 +++ .../testing/sysfs-devices-power_resources_D3hot | 14 +++ drivers/acpi/power.c | 104 ++++++++++++++++++--- 5 files changed, 146 insertions(+), 13 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-devices-power_resources_D0 create mode 100644 Documentation/ABI/testing/sysfs-devices-power_resources_D1 create mode 100644 Documentation/ABI/testing/sysfs-devices-power_resources_D2 create mode 100644 Documentation/ABI/testing/sysfs-devices-power_resources_D3hot (limited to 'drivers/acpi') diff --git a/Documentation/ABI/testing/sysfs-devices-power_resources_D0 b/Documentation/ABI/testing/sysfs-devices-power_resources_D0 new file mode 100644 index 000000000000..73b77a6be196 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-devices-power_resources_D0 @@ -0,0 +1,13 @@ +What: /sys/devices/.../power_resources_D0/ +Date: January 2013 +Contact: Rafael J. Wysocki +Description: + The /sys/devices/.../power_resources_D0/ directory is only + present for device objects representing ACPI device nodes that + use ACPI power resources for power management. + + If present, it contains symbolic links to device directories + representing ACPI power resources that need to be turned on for + the given device node to be in ACPI power state D0. The names + of the links are the same as the names of the directories they + point to. diff --git a/Documentation/ABI/testing/sysfs-devices-power_resources_D1 b/Documentation/ABI/testing/sysfs-devices-power_resources_D1 new file mode 100644 index 000000000000..30c20703fb8c --- /dev/null +++ b/Documentation/ABI/testing/sysfs-devices-power_resources_D1 @@ -0,0 +1,14 @@ +What: /sys/devices/.../power_resources_D1/ +Date: January 2013 +Contact: Rafael J. Wysocki +Description: + The /sys/devices/.../power_resources_D1/ directory is only + present for device objects representing ACPI device nodes that + use ACPI power resources for power management and support ACPI + power state D1. + + If present, it contains symbolic links to device directories + representing ACPI power resources that need to be turned on for + the given device node to be in ACPI power state D1. The names + of the links are the same as the names of the directories they + point to. diff --git a/Documentation/ABI/testing/sysfs-devices-power_resources_D2 b/Documentation/ABI/testing/sysfs-devices-power_resources_D2 new file mode 100644 index 000000000000..fd9d84b421e1 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-devices-power_resources_D2 @@ -0,0 +1,14 @@ +What: /sys/devices/.../power_resources_D2/ +Date: January 2013 +Contact: Rafael J. Wysocki +Description: + The /sys/devices/.../power_resources_D2/ directory is only + present for device objects representing ACPI device nodes that + use ACPI power resources for power management and support ACPI + power state D2. + + If present, it contains symbolic links to device directories + representing ACPI power resources that need to be turned on for + the given device node to be in ACPI power state D2. The names + of the links are the same as the names of the directories they + point to. diff --git a/Documentation/ABI/testing/sysfs-devices-power_resources_D3hot b/Documentation/ABI/testing/sysfs-devices-power_resources_D3hot new file mode 100644 index 000000000000..3df32c20addf --- /dev/null +++ b/Documentation/ABI/testing/sysfs-devices-power_resources_D3hot @@ -0,0 +1,14 @@ +What: /sys/devices/.../power_resources_D3hot/ +Date: January 2013 +Contact: Rafael J. Wysocki +Description: + The /sys/devices/.../power_resources_D3hot/ directory is only + present for device objects representing ACPI device nodes that + use ACPI power resources for power management and support ACPI + power state D3hot. + + If present, it contains symbolic links to device directories + representing ACPI power resources that need to be turned on for + the given device node to be in ACPI power state D3hot. The + names of the links are the same as the names of the directories + they point to. diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 946720a4db57..9466f56b938f 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include "sleep.h" @@ -417,24 +418,101 @@ static void acpi_power_remove_dependent(struct acpi_power_resource *resource, } } -void acpi_power_add_remove_device(struct acpi_device *adev, bool add) +static struct attribute *attrs[] = { + NULL, +}; + +static struct attribute_group attr_groups[] = { + [ACPI_STATE_D0] = { + .name = "power_resources_D0", + .attrs = attrs, + }, + [ACPI_STATE_D1] = { + .name = "power_resources_D1", + .attrs = attrs, + }, + [ACPI_STATE_D2] = { + .name = "power_resources_D2", + .attrs = attrs, + }, + [ACPI_STATE_D3_HOT] = { + .name = "power_resources_D3hot", + .attrs = attrs, + }, +}; + +static void acpi_power_hide_list(struct acpi_device *adev, int state) +{ + struct acpi_device_power_state *ps = &adev->power.states[state]; + struct acpi_power_resource_entry *entry; + + if (list_empty(&ps->resources)) + return; + + list_for_each_entry_reverse(entry, &ps->resources, node) { + struct acpi_device *res_dev = &entry->resource->device; + + sysfs_remove_link_from_group(&adev->dev.kobj, + attr_groups[state].name, + dev_name(&res_dev->dev)); + } + sysfs_remove_group(&adev->dev.kobj, &attr_groups[state]); +} + +static void acpi_power_expose_list(struct acpi_device *adev, int state) { - if (adev->power.flags.power_resources) { - struct acpi_device_power_state *ps; - struct acpi_power_resource_entry *entry; - - ps = &adev->power.states[ACPI_STATE_D0]; - list_for_each_entry(entry, &ps->resources, node) { - struct acpi_power_resource *resource = entry->resource; - - if (add) - acpi_power_add_dependent(resource, adev); - else - acpi_power_remove_dependent(resource, adev); + struct acpi_device_power_state *ps = &adev->power.states[state]; + struct acpi_power_resource_entry *entry; + int ret; + + if (list_empty(&ps->resources)) + return; + + ret = sysfs_create_group(&adev->dev.kobj, &attr_groups[state]); + if (ret) + return; + + list_for_each_entry(entry, &ps->resources, node) { + struct acpi_device *res_dev = &entry->resource->device; + + ret = sysfs_add_link_to_group(&adev->dev.kobj, + attr_groups[state].name, + &res_dev->dev.kobj, + dev_name(&res_dev->dev)); + if (ret) { + acpi_power_hide_list(adev, state); + break; } } } +void acpi_power_add_remove_device(struct acpi_device *adev, bool add) +{ + struct acpi_device_power_state *ps; + struct acpi_power_resource_entry *entry; + int state; + + if (!adev->power.flags.power_resources) + return; + + ps = &adev->power.states[ACPI_STATE_D0]; + list_for_each_entry(entry, &ps->resources, node) { + struct acpi_power_resource *resource = entry->resource; + + if (add) + acpi_power_add_dependent(resource, adev); + else + acpi_power_remove_dependent(resource, adev); + } + + for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++) { + if (add) + acpi_power_expose_list(adev, state); + else + acpi_power_hide_list(adev, state); + } +} + int acpi_power_min_system_level(struct list_head *list) { struct acpi_power_resource_entry *entry; -- cgit v1.2.3 From 660b1113e0f33a476952cb2cbcb5c9831e7ff4cd Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Fri, 25 Jan 2013 21:51:57 +0100 Subject: ACPI / PM: Fix consistency check for power resources during resume During system resume we check if there are power resources that have been turned off by the BIOS, but our reference counters for them are nonzero (they need to be turned on then). It turns out, however, that we also need to check the opposite, i.e. if there are power resources that have been turned on by the BIOS, but our reference counters for them are zero (which means that no devices are going to need them any time soon) and we should turn them off. Make the power resources resume code do the additional check and turn off the unused power resources as appropriate. This change has been tested on HP nx6325. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/power.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c index 9466f56b938f..b820528a5fa3 100644 --- a/drivers/acpi/power.c +++ b/drivers/acpi/power.c @@ -299,9 +299,22 @@ static int acpi_power_on(struct acpi_power_resource *resource) return result; } +static int __acpi_power_off(struct acpi_power_resource *resource) +{ + acpi_status status; + + status = acpi_evaluate_object(resource->device.handle, "_OFF", + NULL, NULL); + if (ACPI_FAILURE(status)) + return -ENODEV; + + ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n", + resource->name)); + return 0; +} + static int acpi_power_off(struct acpi_power_resource *resource) { - acpi_status status = AE_OK; int result = 0; mutex_lock(&resource->resource_lock); @@ -317,17 +330,12 @@ static int acpi_power_off(struct acpi_power_resource *resource) ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] still in use\n", resource->name)); - goto unlock; + } else { + result = __acpi_power_off(resource); + if (result) + resource->ref_count++; } - status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL); - if (ACPI_FAILURE(status)) - result = -ENODEV; - else - ACPI_DEBUG_PRINT((ACPI_DB_INFO, - "Power resource [%s] turned off\n", - resource->name)); - unlock: mutex_unlock(&resource->resource_lock); @@ -851,10 +859,17 @@ void acpi_resume_power_resources(void) mutex_lock(&resource->resource_lock); result = acpi_power_get_state(resource->device.handle, &state); - if (!result && state == ACPI_POWER_RESOURCE_STATE_OFF + if (result) + continue; + + if (state == ACPI_POWER_RESOURCE_STATE_OFF && resource->ref_count) { dev_info(&resource->device.dev, "Turning ON\n"); __acpi_power_on(resource); + } else if (state == ACPI_POWER_RESOURCE_STATE_ON + && !resource->ref_count) { + dev_info(&resource->device.dev, "Turning OFF\n"); + __acpi_power_off(resource); } mutex_unlock(&resource->resource_lock); -- cgit v1.2.3 From 65ab96f60621c4da8f1b4087a57b788bc4d8f27b Mon Sep 17 00:00:00 2001 From: Andreas Fleig Date: Sun, 27 Jan 2013 14:17:55 +0000 Subject: ACPI / PM: Fix /proc/acpi/wakeup for devices w/o bus or parent Fix /proc/acpi/wakeup for devices without bus or parent This patch fixes printing the wakeup status for devices without a bus or parent, such as laptop lid switches and sleep buttons. These devices have an empty physical_node_list, because acpi_bind_one is never run for them. [rjw: White space and coding style.] Signed-off-by: Andreas Fleig Signed-off-by: Rafael J. Wysocki --- drivers/acpi/proc.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'drivers/acpi') diff --git a/drivers/acpi/proc.c b/drivers/acpi/proc.c index ef98796b3824..52ce76725c20 100644 --- a/drivers/acpi/proc.c +++ b/drivers/acpi/proc.c @@ -311,11 +311,12 @@ acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset) dev->pnp.bus_id, (u32) dev->wakeup.sleep_state); - if (!dev->physical_node_count) + if (!dev->physical_node_count) { seq_printf(seq, "%c%-8s\n", - dev->wakeup.flags.run_wake ? - '*' : ' ', "disabled"); - else { + dev->wakeup.flags.run_wake ? '*' : ' ', + device_may_wakeup(&dev->dev) ? + "enabled" : "disabled"); + } else { struct device *ldev; list_for_each_entry(entry, &dev->physical_node_list, node) { -- cgit v1.2.3