Re: [PATCH 08/11] pmdomain: core: Add internal ->sync_state() support for genpd providers

Saravana Kannan
2025-04-18T00:24:05Z

On Thu, Apr 17, 2025 at 7:25 AM Ulf Hansson <ulf.hansson@linaro.org> wrote:
>
> If the genpd provider's fwnode doesn't have an associated struct device
> with it, we can make use of the generic genpd->dev and it corresponding
> driver internally in genpd to manage ->sync_state().
>
> More precisely, while adding a genpd OF provider let's check if the fwnode
> has a device and if not, make the preparation to handle ->sync_state()
> internally through the genpd_provider_driver and the genpd_provider_bus.
>
> Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
> ---
> drivers/pmdomain/core.c | 36 ++++++++++++++++++++++++++++++++++++
> include/linux/pm_domain.h | 7 +++++++
> 2 files changed, 43 insertions(+)
>
> diff --git a/drivers/pmdomain/core.c b/drivers/pmdomain/core.c
> index 512f89e6d302..9c5a77bf59d2 100644
> --- a/drivers/pmdomain/core.c
> +++ b/drivers/pmdomain/core.c
> @@ -2374,6 +2374,7 @@ int pm_genpd_init(struct generic_pm_domain *genpd,
> INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
> atomic_set(&genpd->sd_count, 0);
> genpd->status = is_off ? GENPD_STATE_OFF : GENPD_STATE_ON;
> + genpd->sync_state = GENPD_SYNC_STATE_OFF;
> genpd->device_count = 0;
> genpd->provider = NULL;
> genpd->device_id = -ENXIO;
> @@ -2656,6 +2657,7 @@ int of_genpd_add_provider_simple(struct device_node *np,
> struct generic_pm_domain *genpd)
> {
> struct fwnode_handle *fwnode;
> + struct device *dev;
> int ret;
>
> if (!np || !genpd)
> @@ -2665,6 +2667,10 @@ int of_genpd_add_provider_simple(struct device_node *np,
> return -EINVAL;
>
> fwnode = &np->fwnode;
> + dev = fwnode->dev;
> +
> + if (!dev)
> + genpd->sync_state = GENPD_SYNC_STATE_SIMPLE;

I don't want people directly poking into fwnode.

Use get_device_from_fwnode() that's in drivers/base/core.c? Might need
to move it to a header. Make sure to put_device() it back.

Or ideally, figure it out using some other means like looking for
#power-domain-cells number? (if that'll always give the right answer).
Point being, the framework should know which type it's registering
even if there was no fwnode/fw_devlink.

>
> device_set_node(&genpd->dev, fwnode);
>
> @@ -2718,8 +2724,10 @@ int of_genpd_add_provider_onecell(struct device_node *np,
> {
> struct generic_pm_domain *genpd;
> struct fwnode_handle *fwnode;
> + struct device *dev;
> unsigned int i;
> int ret = -EINVAL;
> + bool sync_state = false;
>
> if (!np || !data)
> return -EINVAL;
> @@ -2728,6 +2736,10 @@ int of_genpd_add_provider_onecell(struct device_node *np,
> data->xlate = genpd_xlate_onecell;
>
> fwnode = &np->fwnode;
> + dev = fwnode->dev;

Can you do this some other way please? I really don't like this look up.

-Saravana

> +
> + if (!dev)
> + sync_state = true;
>
> for (i = 0; i < data->num_domains; i++) {
> genpd = data->domains[i];
> @@ -2737,6 +2749,11 @@ int of_genpd_add_provider_onecell(struct device_node *np,
> if (!genpd_present(genpd))
> goto error;
>
> + if (sync_state) {
> + genpd->sync_state = GENPD_SYNC_STATE_ONECELL;
> + sync_state = false;
> + }
> +
> device_set_node(&genpd->dev, fwnode);
>
> ret = device_add(&genpd->dev);
> @@ -3453,6 +3470,25 @@ static void genpd_provider_remove(struct device *dev)
>
> static void genpd_provider_sync_state(struct device *dev)
> {
> + struct generic_pm_domain *genpd = container_of(dev, struct generic_pm_domain, dev);
> +
> + switch (genpd->sync_state) {
> + case GENPD_SYNC_STATE_OFF:
> + break;
> +
> + case GENPD_SYNC_STATE_ONECELL:
> + of_genpd_sync_state(dev);
> + break;
> +
> + case GENPD_SYNC_STATE_SIMPLE:
> + genpd_lock(genpd);
> + genpd_power_off(genpd, false, 0);
> + genpd_unlock(genpd);
> + break;
> +
> + default:
> + break;
> + }
> }
>
> static struct genpd_provider_drv genpd_provider_drv = {
> diff --git a/include/linux/pm_domain.h b/include/linux/pm_domain.h
> index e9a1f8975c4f..2185ee9e4f7c 100644
> --- a/include/linux/pm_domain.h
> +++ b/include/linux/pm_domain.h
> @@ -127,6 +127,12 @@ enum genpd_notication {
> GENPD_NOTIFY_ON,
> };
>
> +enum genpd_sync_state {
> + GENPD_SYNC_STATE_OFF = 0,
> + GENPD_SYNC_STATE_SIMPLE,
> + GENPD_SYNC_STATE_ONECELL,
> +};
> +
> struct dev_power_governor {
> bool (*power_down_ok)(struct dev_pm_domain *domain);
> bool (*suspend_ok)(struct device *dev);
> @@ -187,6 +193,7 @@ struct generic_pm_domain {
> unsigned int performance_state; /* Aggregated max performance state */
> cpumask_var_t cpus; /* A cpumask of the attached CPUs */
> bool synced_poweroff; /* A consumer needs a synced poweroff */
> + enum genpd_sync_state sync_state; /* How sync_state is managed. */
> int (*power_off)(struct generic_pm_domain *domain);
> int (*power_on)(struct generic_pm_domain *domain);
> struct raw_notifier_head power_notifiers; /* Power on/off notifiers */
> --
> 2.43.0
>