Linux ARM-MSM sub-architecture
 help / color / mirror / Atom feed
From: Abel Vesa <abel.vesa@linaro.org>
To: "Rafael J . Wysocki" <rafael@kernel.org>,
	Kevin Hilman <khilman@kernel.org>,
	Ulf Hansson <ulf.hansson@linaro.org>,
	Len Brown <len.brown@intel.com>, Pavel Machek <pavel@ucw.cz>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Saravana Kannan <saravanak@google.com>
Cc: Bjorn Andersson <andersson@kernel.org>,
	linux-pm@vger.kernel.org,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-arm-msm@vger.kernel.org,
	Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
Subject: [RFC PATCH v5 3/4] PM: domains: Ignore power off request for enabled unused domains
Date: Wed, 21 Jun 2023 17:40:18 +0300	[thread overview]
Message-ID: <20230621144019.3219858-4-abel.vesa@linaro.org> (raw)
In-Reply-To: <20230621144019.3219858-1-abel.vesa@linaro.org>

First of, safekeep the boot state that is provided on init, then use this
boot state to make decisions whether a power off request should be
ignored or not. In case a domain was left enabled before boot, most
likely such domain is needed and should not be disabled on the 'disable
unused' late initcall, but rather needs to stay powered on until the
consumer driver gets a chance to probe. In order to keep such domain
powered on until the consumer handles it correctly, the domain needs to
be registered by a provider that has a sync_state callback registered
and said provider has state synced.

Signed-off-by: Abel Vesa <abel.vesa@linaro.org>
---
 drivers/base/power/domain.c | 49 +++++++++++++++++++++++++++++++++++++
 include/linux/pm_domain.h   |  1 +
 2 files changed, 50 insertions(+)

diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c
index 51b9d4eaab5e..5967ade160e2 100644
--- a/drivers/base/power/domain.c
+++ b/drivers/base/power/domain.c
@@ -654,6 +654,43 @@ static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
 	queue_work(pm_wq, &genpd->power_off_work);
 }
 
+/**
+ * genpd_keep_on - Tells if the domain should skip the power 'off' request
+ * @genpd: PM domain to be checked.
+ *
+ * If the domain's current state meets the following conditions:
+ *  - marked for being kept as enabled
+ *  - has a provider with a sync state callback registered
+ *  - the provider hasn't state synced yet
+ * then the power 'off' request should be skipped.
+ *
+ * This function should only be called from genpd_power_off and with
+ * the lock held.
+ */
+static inline bool genpd_keep_on(struct generic_pm_domain *genpd)
+{
+	bool ret = false;
+
+	if (!(genpd->boot_keep_on))
+		return false;
+
+	if (!genpd->has_provider)
+		goto out;
+
+	if (!dev_has_sync_state(genpd->provider->dev))
+		goto out;
+
+	if (dev_is_drv_state_synced(genpd->provider->dev))
+		goto out;
+
+	return true;
+
+out:
+	genpd->boot_keep_on = false;
+
+	return ret;
+}
+
 /**
  * genpd_power_off - Remove power from a given PM domain.
  * @genpd: PM domain to power down.
@@ -682,6 +719,13 @@ static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
 	if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
 		return 0;
 
+	/*
+	 * If the domain is enabled and unused, bail out and ignore
+	 * the 'off' request until the provider has state synced.
+	 */
+	if (genpd_keep_on(genpd))
+		return -EBUSY;
+
 	/*
 	 * Abort power off for the PM domain in the following situations:
 	 * (1) The domain is configured as always on.
@@ -2065,6 +2109,7 @@ int pm_genpd_init(struct generic_pm_domain *genpd,
 	atomic_set(&genpd->sd_count, 0);
 	genpd->status = is_off ? GENPD_STATE_OFF : GENPD_STATE_ON;
 	genpd->device_count = 0;
+	genpd->boot_keep_on = !is_off;
 	genpd->provider = NULL;
 	genpd->has_provider = false;
 	genpd->accounting_time = ktime_get_mono_fast_ns();
@@ -2718,6 +2763,10 @@ static void genpd_dev_pm_sync(struct device *dev)
 	if (IS_ERR(pd))
 		return;
 
+	genpd_lock(pd);
+	pd->boot_keep_on = false;
+	genpd_unlock(pd);
+
 	genpd_queue_power_off_work(pd);
 }
 
diff --git a/include/linux/pm_domain.h b/include/linux/pm_domain.h
index f776fb93eaa0..3eb32c4b6d4f 100644
--- a/include/linux/pm_domain.h
+++ b/include/linux/pm_domain.h
@@ -131,6 +131,7 @@ struct generic_pm_domain {
 	const char *name;
 	atomic_t sd_count;	/* Number of subdomains with power "on" */
 	enum gpd_status status;	/* Current state of the domain */
+	bool boot_keep_on;	/* Keep enabled during 'disable unused' late initcall */
 	unsigned int device_count;	/* Number of devices */
 	unsigned int suspended_count;	/* System suspend device counter */
 	unsigned int prepared_count;	/* Suspend counter of prepared devices */
-- 
2.34.1


  parent reply	other threads:[~2023-06-21 14:40 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-21 14:40 [RFC PATCH v5 0/4] PM: domain: Support skiping disabling unused domains until sync state Abel Vesa
2023-06-21 14:40 ` [RFC PATCH v5 1/4] driver core: Add dev_set_drv_sync_state() Abel Vesa
2023-06-21 14:40 ` [RFC PATCH v5 2/4] driver core: Add dev_is_drv_state_synced() Abel Vesa
2023-06-21 15:22   ` Greg Kroah-Hartman
2023-06-28 11:50     ` Abel Vesa
2023-06-21 14:40 ` Abel Vesa [this message]
2023-07-04 13:24   ` [RFC PATCH v5 3/4] PM: domains: Ignore power off request for enabled unused domains Ulf Hansson
2023-06-21 14:40 ` [RFC PATCH v5 4/4] PM: domains: Add and set generic sync state callback Abel Vesa
2023-07-04 13:54   ` Ulf Hansson
     [not found] ` <2786d9ff-0579-429b-b431-a8547cbf6fb6@ti>
2025-04-07 16:13   ` [RFC PATCH v5 0/4] PM: domain: Support skiping disabling unused domains until sync state Ulf Hansson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230621144019.3219858-4-abel.vesa@linaro.org \
    --to=abel.vesa@linaro.org \
    --cc=andersson@kernel.org \
    --cc=dmitry.baryshkov@linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=khilman@kernel.org \
    --cc=len.brown@intel.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=pavel@ucw.cz \
    --cc=rafael@kernel.org \
    --cc=saravanak@google.com \
    --cc=ulf.hansson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox