power-domain: Add support for refcounting (again)

It is very surprising that such an uclass, specifically designed to
handle resources that may be shared by different devices, is not keeping
the count of the number of times a power domain has been
enabled/disabled to avoid shutting it down unexpectedly or disabling it
several times.

Doing this causes troubles on eg. i.MX8MP because disabling power
domains can be done in recursive loops were the same power domain
disabled up to 4 times in a row. PGCs seem to have tight FSM internal
timings to respect and it is easy to produce a race condition that puts
the power domains in an unstable state, leading to ADB400 errors and
later crashes in Linux.

Some drivers implement their own mechanism for that, but it is probably
best to add this feature in the uclass and share the common code across
drivers. In order to avoid breaking existing drivers, refcounting is
only enabled if the number of subdomains a device node supports is
explicitly set in the probe function. ->xlate() callbacks will return
the power domain ID which is then being used as the array index to reach
the correct refcounter.

As we do not want to break existing users while stile getting
interesting error codes, the implementation is split between:
- a low-level helper reporting error codes if the requested transition
  could not be operated,
- a higher-level helper ignoring the "non error" codes, like EALREADY and
  EBUSY.

CI tests using power domains are slightly updated to make sure the count
of on/off calls is even and the results match what we *now* expect. They
are also extended to test the low-level functions.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
diff --git a/drivers/power/domain/power-domain-uclass.c b/drivers/power/domain/power-domain-uclass.c
index 938bd8c..d9fa8ad 100644
--- a/drivers/power/domain/power-domain-uclass.c
+++ b/drivers/power/domain/power-domain-uclass.c
@@ -12,6 +12,10 @@
 #include <power-domain-uclass.h>
 #include <dm/device-internal.h>
 
+struct power_domain_priv {
+	int *on_count;
+};
+
 static inline struct power_domain_ops *power_domain_dev_ops(struct udevice *dev)
 {
 	return (struct power_domain_ops *)dev->driver->ops;
@@ -107,22 +111,67 @@
 	return ops->rfree ? ops->rfree(power_domain) : 0;
 }
 
-int power_domain_on(struct power_domain *power_domain)
+int power_domain_on_lowlevel(struct power_domain *power_domain)
 {
+	struct power_domain_priv *priv = dev_get_uclass_priv(power_domain->dev);
+	struct power_domain_plat *plat = dev_get_uclass_plat(power_domain->dev);
 	struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
+	int *on_count = plat->subdomains ? &priv->on_count[power_domain->id] : NULL;
+	int ret;
 
-	debug("%s(power_domain=%p)\n", __func__, power_domain);
+	/* Refcounting is not enabled on all drivers by default */
+	if (on_count) {
+		debug("Enable power domain %s.%ld: %d -> %d (%s)\n",
+		      power_domain->dev->name, power_domain->id, *on_count, *on_count + 1,
+		      (((*on_count + 1) > 1) ? "EALREADY" : "todo"));
+
+		(*on_count)++;
+		if (*on_count > 1)
+			return -EALREADY;
+	}
 
-	return ops->on ? ops->on(power_domain) : 0;
+	ret = ops->on ? ops->on(power_domain) : 0;
+	if (ret) {
+		if (on_count)
+			(*on_count)--;
+		return ret;
+	}
+
+	return 0;
 }
 
-int power_domain_off(struct power_domain *power_domain)
+int power_domain_off_lowlevel(struct power_domain *power_domain)
 {
+	struct power_domain_priv *priv = dev_get_uclass_priv(power_domain->dev);
+	struct power_domain_plat *plat = dev_get_uclass_plat(power_domain->dev);
 	struct power_domain_ops *ops = power_domain_dev_ops(power_domain->dev);
+	int *on_count = plat->subdomains ? &priv->on_count[power_domain->id] : NULL;
+	int ret;
 
-	debug("%s(power_domain=%p)\n", __func__, power_domain);
+	/* Refcounting is not enabled on all drivers by default */
+	if (on_count) {
+		debug("Disable power domain %s.%ld: %d -> %d (%s%s)\n",
+		      power_domain->dev->name, power_domain->id, *on_count, *on_count - 1,
+		      (((*on_count) <= 0) ? "EALREADY" : ""),
+		      (((*on_count - 1) > 0) ? "BUSY" : "todo"));
+
+		if (*on_count <= 0)
+			return -EALREADY;
+
+		(*on_count)--;
+		if (*on_count > 0)
+			return -EBUSY;
+	}
+
+	ret = ops->off ? ops->off(power_domain) : 0;
+	if (ret) {
+		if (on_count)
+			(*on_count)++;
 
-	return ops->off ? ops->off(power_domain) : 0;
+		return ret;
+	}
+
+	return 0;
 }
 
 #if CONFIG_IS_ENABLED(OF_REAL)
@@ -177,7 +226,36 @@
 }
 #endif  /* OF_REAL */
 
+static int power_domain_post_probe(struct udevice *dev)
+{
+	struct power_domain_priv *priv = dev_get_uclass_priv(dev);
+	struct power_domain_plat *plat = dev_get_uclass_plat(dev);
+
+	if (plat->subdomains) {
+		priv->on_count = calloc(sizeof(int), plat->subdomains);
+		if (!priv->on_count)
+			return -ENOMEM;
+	}
+
+	return 0;
+}
+
+static int power_domain_pre_remove(struct udevice *dev)
+{
+	struct power_domain_priv *priv = dev_get_uclass_priv(dev);
+	struct power_domain_plat *plat = dev_get_uclass_plat(dev);
+
+	if (plat->subdomains)
+		free(priv->on_count);
+
+	return 0;
+}
+
 UCLASS_DRIVER(power_domain) = {
 	.id		= UCLASS_POWER_DOMAIN,
 	.name		= "power_domain",
+	.post_probe	= power_domain_post_probe,
+	.pre_remove	= power_domain_pre_remove,
+	.per_device_auto = sizeof(struct power_domain_priv),
+	.per_device_plat_auto = sizeof(struct power_domain_plat),
 };