[PATCH] ACPI: PM: Use nearest power-manageable ancestor

Guan-Chun.Wu posted 1 patch 2 months, 1 week ago
drivers/acpi/device_pm.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
[PATCH] ACPI: PM: Use nearest power-manageable ancestor
Posted by Guan-Chun.Wu 2 months, 1 week ago
When a device’s power_manageable flag is false, we currently only
fall back to the direct parent’s power state.  In a deep hierarchy
there may be a more distant ancestor that does support power
management.

Walk up the parent chain until we find the closest power_manageable
ancestor and use its power state.  If none is found, default to
ACPI_STATE_D0 (fully on).

Signed-off-by: Guan-Chun.Wu <409411716@gms.tku.edu.tw>
---
 drivers/acpi/device_pm.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c
index dbd4446025ec..81b47fb00e80 100644
--- a/drivers/acpi/device_pm.c
+++ b/drivers/acpi/device_pm.c
@@ -84,8 +84,23 @@ int acpi_device_get_power(struct acpi_device *device, int *state)
 	parent = acpi_dev_parent(device);
 
 	if (!device->flags.power_manageable) {
-		/* TBD: Non-recursive algorithm for walking up hierarchy. */
-		*state = parent ? parent->power.state : ACPI_STATE_D0;
+		/*
+		 * If the device itself is not power-manageable,
+		 * walk up the parent hierarchy to find the closest
+		 * ancestor that is power-manageable.
+		 * Use that ancestor's power state as an estimate
+		 * for this device. If no such ancestor exists,
+		 * default to D0 (Fully On).
+		 */
+		struct acpi_device *ancestor = parent;
+		/*
+		 * Keep traversing up until a power-manageable ancestor
+		 * is found or the root is reached
+		 */
+		while (ancestor && !ancestor->flags.power_manageable)
+			ancestor = acpi_dev_parent(ancestor);
+		/* Use the found ancestor's power state, or D0 if none is found */
+		*state = ancestor ? ancestor->power.state : ACPI_STATE_D0;
 		goto out;
 	}
 
-- 
2.34.1

Re: [PATCH] ACPI: PM: Use nearest power-manageable ancestor
Posted by Rafael J. Wysocki 2 months, 1 week ago
On Thu, Jul 24, 2025 at 11:21 AM Guan-Chun.Wu <409411716@gms.tku.edu.tw> wrote:
>
> When a device’s power_manageable flag is false, we currently only
> fall back to the direct parent’s power state.  In a deep hierarchy
> there may be a more distant ancestor that does support power
> management.
>
> Walk up the parent chain until we find the closest power_manageable
> ancestor and use its power state.  If none is found, default to
> ACPI_STATE_D0 (fully on).

Is there a specific use case in the field in which this change is needed?

If there's none, let's not make it.

> Signed-off-by: Guan-Chun.Wu <409411716@gms.tku.edu.tw>
> ---
>  drivers/acpi/device_pm.c | 19 +++++++++++++++++--
>  1 file changed, 17 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c
> index dbd4446025ec..81b47fb00e80 100644
> --- a/drivers/acpi/device_pm.c
> +++ b/drivers/acpi/device_pm.c
> @@ -84,8 +84,23 @@ int acpi_device_get_power(struct acpi_device *device, int *state)
>         parent = acpi_dev_parent(device);
>
>         if (!device->flags.power_manageable) {
> -               /* TBD: Non-recursive algorithm for walking up hierarchy. */
> -               *state = parent ? parent->power.state : ACPI_STATE_D0;
> +               /*
> +                * If the device itself is not power-manageable,
> +                * walk up the parent hierarchy to find the closest
> +                * ancestor that is power-manageable.
> +                * Use that ancestor's power state as an estimate
> +                * for this device. If no such ancestor exists,
> +                * default to D0 (Fully On).
> +                */
> +               struct acpi_device *ancestor = parent;
> +               /*
> +                * Keep traversing up until a power-manageable ancestor
> +                * is found or the root is reached
> +                */
> +               while (ancestor && !ancestor->flags.power_manageable)
> +                       ancestor = acpi_dev_parent(ancestor);
> +               /* Use the found ancestor's power state, or D0 if none is found */
> +               *state = ancestor ? ancestor->power.state : ACPI_STATE_D0;
>                 goto out;
>         }
>
> --
> 2.34.1
>
Re: [PATCH] ACPI: PM: Use nearest power-manageable ancestor
Posted by Guan-Chun.Wu 2 months, 1 week ago
> Is there a specific use case in the field in which this change is needed?
> If there's none, let's not make it.

Thank you for your response.

I am not aware of any specific use case in the field where this change is currently needed.