[WIP]: PM: domains: Keep domains enabled/unknown at boot enabled until sync state

Expand the is_off from pm_genpd_init to allow unknown state for
providers that do not have a way to read the actual state. Add the boot
status to the genpd struct to keep track of how it was left of at boot
stage and use it to determine if we should power off a domain that is in
on/unknown state.

NOTE: This does not solve yet the problem of disabling unused from late
initcall if the sync_state callback is not assign in the provider.
I'm still working on that.

Not-signed-off-by: Abel Vesa <abel.vesa@linaro.org>
diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c
index 32084e3..3617ee6 100644
--- a/drivers/base/power/domain.c
+++ b/drivers/base/power/domain.c
@@ -130,6 +130,7 @@
 #define genpd_is_active_wakeup(genpd)	(genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
 #define genpd_is_cpu_domain(genpd)	(genpd->flags & GENPD_FLAG_CPU_DOMAIN)
 #define genpd_is_rpm_always_on(genpd)	(genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
+#define genpd_boot_keep_on(genpd)		(!(genpd->boot_status == GENPD_STATE_OFF))
 
 static inline bool irq_safe_dev_in_sleep_domain(struct device *dev,
 		const struct generic_pm_domain *genpd)
@@ -655,6 +656,29 @@
 }
 
 /**
+ * pm_genpd_power_off_unused_sync_state - Power off all domains for provider.
+ * @dev: Provider's device.
+ *
+ * Request power off for all unused domains of the provider.
+ * This should be used exclusively as sync state callback for genpd providers.
+ */
+void pm_genpd_power_off_unused_sync_state(struct device *dev)
+{
+	struct generic_pm_domain *genpd;
+
+	mutex_lock(&gpd_list_lock);
+
+	list_for_each_entry(genpd, &gpd_list, gpd_list_node)
+		if (genpd->provider->dev == dev && genpd_boot_keep_on(genpd)) {
+			genpd->boot_status = GENPD_STATE_OFF;
+			genpd_queue_power_off_work(genpd);
+		}
+
+	mutex_unlock(&gpd_list_lock);
+}
+EXPORT_SYMBOL_GPL(pm_genpd_power_off_unused_sync_state);
+
+/**
  * genpd_power_off - Remove power from a given PM domain.
  * @genpd: PM domain to power down.
  * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
@@ -675,6 +699,12 @@
 	int ret;
 
 	/*
+	 * If the domain was left enabled at boot stage,
+	 * abort power off until sync state is reached.
+	 */
+	if (genpd_boot_keep_on(genpd))
+		return -EBUSY;
+	/*
 	 * Do not try to power off the domain in the following situations:
 	 * (1) The domain is already in the "power off" state.
 	 * (2) System suspend is in progress.
@@ -763,6 +793,13 @@
 	struct gpd_link *link;
 	int ret = 0;
 
+	/*
+	 * Since a power on request means the domain is being used
+	 * from now on, mark the boot status of it as OFF.
+	 */
+	if (genpd_boot_keep_on(genpd))
+		genpd->boot_status = GENPD_STATE_OFF;
+
 	if (genpd_status_on(genpd))
 		return 0;
 
@@ -1124,6 +1161,9 @@
 {
 	struct gpd_link *link;
 
+	if (genpd_boot_keep_on(genpd))
+		return;
+
 	if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
 		return;
 
@@ -2043,12 +2083,13 @@
  * pm_genpd_init - Initialize a generic I/O PM domain object.
  * @genpd: PM domain object to initialize.
  * @gov: PM domain governor to associate with the domain (may be NULL).
- * @is_off: Initial value of the domain's power_is_off field.
+ * @boot_status: Initial (boot) state of the domain.
  *
  * Returns 0 on successful initialization, else a negative error code.
  */
 int pm_genpd_init(struct generic_pm_domain *genpd,
-		  struct dev_power_governor *gov, bool is_off)
+		  struct dev_power_governor *gov,
+		  enum gpd_status boot_status)
 {
 	int ret;
 
@@ -2063,7 +2104,7 @@
 	genpd->gov = gov;
 	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->status = genpd->boot_status = boot_status;
 	genpd->device_count = 0;
 	genpd->provider = NULL;
 	genpd->has_provider = false;
@@ -3112,7 +3153,8 @@
 {
 	static const char * const status_lookup[] = {
 		[GENPD_STATE_ON] = "on",
-		[GENPD_STATE_OFF] = "off"
+		[GENPD_STATE_OFF] = "off",
+		[GENPD_STATE_UNKNOWN] = "unknown"
 	};
 	struct pm_domain_data *pm_data;
 	const char *kobj_path;
@@ -3194,7 +3236,8 @@
 {
 	static const char * const status_lookup[] = {
 		[GENPD_STATE_ON] = "on",
-		[GENPD_STATE_OFF] = "off"
+		[GENPD_STATE_OFF] = "off",
+		[GENPD_STATE_UNKNOWN] = "unknown"
 	};
 
 	struct generic_pm_domain *genpd = s->private;
diff --git a/include/linux/pm_domain.h b/include/linux/pm_domain.h
index f776fb9..86bb531 100644
--- a/include/linux/pm_domain.h
+++ b/include/linux/pm_domain.h
@@ -73,6 +73,7 @@
 enum gpd_status {
 	GENPD_STATE_ON = 0,	/* PM domain is on */
 	GENPD_STATE_OFF,	/* PM domain is off */
+	GENPD_STATE_UNKNOWN,	/* PM domain boot state is unknown */
 };
 
 enum genpd_notication {
@@ -131,6 +132,7 @@
 	const char *name;
 	atomic_t sd_count;	/* Number of subdomains with power "on" */
 	enum gpd_status status;	/* Current state of the domain */
+	enum gpd_status boot_status;	/* Boot state of the domain */
 	unsigned int device_count;	/* Number of devices */
 	unsigned int suspended_count;	/* System suspend device counter */
 	unsigned int prepared_count;	/* Suspend counter of prepared devices */
@@ -229,8 +231,10 @@
 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
 			      struct generic_pm_domain *subdomain);
 int pm_genpd_init(struct generic_pm_domain *genpd,
-		  struct dev_power_governor *gov, bool is_off);
+		  struct dev_power_governor *gov,
+		  enum gpd_status boot_status);
 int pm_genpd_remove(struct generic_pm_domain *genpd);
+void pm_genpd_power_off_unused_sync_state(struct device *dev);
 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
 int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb);
 int dev_pm_genpd_remove_notifier(struct device *dev);
@@ -269,7 +273,8 @@
 	return -ENOSYS;
 }
 static inline int pm_genpd_init(struct generic_pm_domain *genpd,
-				struct dev_power_governor *gov, bool is_off)
+				struct dev_power_governor *gov,
+				enum gpd_status boot_status)
 {
 	return -ENOSYS;
 }
@@ -278,6 +283,9 @@
 	return -EOPNOTSUPP;
 }
 
+static inline void pm_genpd_power_off_unused_sync_state(struct device *dev)
+{ }
+
 static inline int dev_pm_genpd_set_performance_state(struct device *dev,
 						     unsigned int state)
 {