Skip to contentSkip to conversation

clk: qcom: gdsc: three pre-existing correctness fixes

PATCH Applied Herman van Hazendonk 3 participants 8 messages Jun 2 – Jul 8

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover clk: qcom: gdsc: three pre-existing correctness fixes

While preparing MSM8x60 support for the GDSC core (a separate series, adding LEGACY_FOOTSWITCH and RPM_ALWAYS_ON), code review surfaced three pre-existing correctness issues in drivers/clk/qcom/gdsc.c that are independent of any new SoC enabling work. This series collects those three fixes so each can be reviewed and queued on its own schedule without blocking the MSM8x60 series.

1/3 - gdsc_poll_status: propagate gdsc_check_status() errno

The polling helper treats any non-zero gdsc_check_status() return as "reached the target state". gdsc_check_status() returns < 0 on regmap_read() failure (e.g. parent regmap unbinding, bus protection, debug-tool CSR removal). The boolean test promotes that negative errno to "true" and the poll returns success with the hardware state unknown -- silently misleading gdsc_toggle_logic() and the gdsc_init() sync path.

2/3 - gdsc_init: propagate gdsc_enable() failure for ALWAYS_ON

GENPD_FLAG_ALWAYS_ON requires the domain to be ON at pm_genpd_init() time. When the cold readback finds an ALWAYS_ON GDSC powered down, gdsc_init() tries to bring it back up but discards the gdsc_enable() return value. If the enable fails the code still sets on=true and registers the domain as ALWAYS_ON, so genpd reports "on" while the silicon is actually off and consumer probes hang on dead registers.

3/3 - gdsc_unregister: tear down per-domain genpds + reorder

gdsc_unregister() removes the OF provider entry and tears down the parent/subdomain wiring, but never calls pm_genpd_remove() on the individual generic_pm_domain structures registered by gdsc_init(). The dangling entries on gpd_list cause the next gdsc_init() to fail with -EEXIST on deferred-probe replay, module reload, or OF-overlay teardown. While here, of_genpd_- del_provider() is moved ahead of gdsc_pm_subdomain_remove() so that a racing of_genpd_get_from_provider() cannot attach to a domain that is mid-removal.

The three changes are independent and could in principle be sent as separate one-off patches, but they share a single file and the cover letter makes it easier to reason about them together. Apply order is not significant: each patch stands on its own.

Built and boot-tested on an HP TouchPad (APQ8060) in conjunction with the MSM8x60 GDSC enabling series.

Herman van Hazendonk (3):
clk: qcom: gdsc: propagate gdsc_check_status() errors from
gdsc_poll_status
clk: qcom: gdsc: propagate gdsc_enable() failure for ALWAYS_ON domains
clk: qcom: gdsc: tear down per-domain genpds in gdsc_unregister()

 drivers/clk/qcom/gdsc.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

base-commit: 944125b4c454b58d2fe6e35f1087a932b2050dff --
2.43.0

#1 lore raw

PATCH 1/3 clk: qcom: gdsc: propagate gdsc_check_status() errors from gdsc_poll_status

gdsc_check_status() returns negative errno when the underlying regmap_read() fails -- e.g. when a parent regmap dies during system suspend, a CSR is removed by an HW debug tool, or the bus controller goes into protection. gdsc_poll_status() treats the result as a plain boolean ("is the GDSC in the requested state?"), so any negative error return is truncated to "true" and the poll exits with success even though the rail's real state is unknown:

	do {
		if (gdsc_check_status(sc, status))
			return 0;
	} while (ktime_us_delta(ktime_get(), start) < STATUS_POLL_TIMEOUT_US);

	if (gdsc_check_status(sc, status))
		return 0;

	return -ETIMEDOUT;

This silently misleads gdsc_toggle_logic() (which writes/un-writes SW_COLLAPSE on the strength of the poll succeeding) and the gdsc_init() sync path (which assumes the readback represents real silicon state).

Latch the return value, propagate negative errno immediately, and only treat a strictly-positive value as "reached the target state". Make the same change in the post-timeout final check so a regmap that comes back after the deadline does not silently degrade to -ETIMEDOUT.

---
 drivers/clk/qcom/gdsc.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/clk/qcom/gdsc.c +9 -2
index 95aa07120245..b9b47f584f6d 100644
@@ -103,14 +103,21 @@ static int gdsc_hwctrl(struct gdsc *sc, bool en)
 static int gdsc_poll_status(struct gdsc *sc, enum gdsc_status status)
 {
 	ktime_t start;
+	int ret;
 
 	start = ktime_get();
 	do {
-		if (gdsc_check_status(sc, status))
+		ret = gdsc_check_status(sc, status);
+		if (ret < 0)
+			return ret;
+		if (ret)
 			return 0;
 	} while (ktime_us_delta(ktime_get(), start) < STATUS_POLL_TIMEOUT_US);
 
-	if (gdsc_check_status(sc, status))
+	ret = gdsc_check_status(sc, status);
+	if (ret < 0)
+		return ret;
+	if (ret)
 		return 0;
 
 	return -ETIMEDOUT;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] clk: qcom: gdsc: propagate gdsc_check_status() errors from gdsc_poll_status

Quoted 4 lines · Herman van Hazendonk

gdsc_check_status() returns negative errno when the underlying regmap_read() fails -- e.g. when a parent regmap dies during system suspend, a CSR is removed by an HW debug tool, or the bus controller goes into protection.

Well... For MMIO bus this is mostly theoretical...

Quoted 19 lines

gdsc_poll_status() treats the result as a plain
boolean ("is the GDSC in the requested state?"), so any negative error return is truncated to "true" and the poll exits with success even though the rail's real state is unknown:

	do {
		if (gdsc_check_status(sc, status))
			return 0;
	} while (ktime_us_delta(ktime_get(), start) < STATUS_POLL_TIMEOUT_US);

	if (gdsc_check_status(sc, status))
		return 0;

	return -ETIMEDOUT;

This silently misleads gdsc_toggle_logic() (which writes/un-writes SW_COLLAPSE on the strength of the poll succeeding) and the gdsc_init() sync path (which assumes the readback represents real silicon state).

Quoted 6 lines

Latch the return value, propagate negative errno immediately, and only treat a strictly-positive value as "reached the target state". Make the same change in the post-timeout final check so a regmap that comes back after the deadline does not silently degrade to -ETIMEDOUT.

Fixes 77b1067a19b4 ("clk: qcom: gdsc: Add support for gdscs with gds hw controller")
Quoted 35 lines
---
 drivers/clk/qcom/gdsc.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/clk/qcom/gdsc.c +9 -2
index 95aa07120245..b9b47f584f6d 100644
@@ -103,14 +103,21 @@ static int gdsc_hwctrl(struct gdsc *sc, bool en)
 static int gdsc_poll_status(struct gdsc *sc, enum gdsc_status status)
 {
 	ktime_t start;
+	int ret;
 
 	start = ktime_get();
 	do {
-		if (gdsc_check_status(sc, status))
+		ret = gdsc_check_status(sc, status);
+		if (ret < 0)
+			return ret;
+		if (ret)
 			return 0;
 	} while (ktime_us_delta(ktime_get(), start) < STATUS_POLL_TIMEOUT_US);
 
-	if (gdsc_check_status(sc, status))
+	ret = gdsc_check_status(sc, status);
+	if (ret < 0)
+		return ret;
+	if (ret)
 		return 0;
 
 	return -ETIMEDOUT;
Signature · 1 line
2.43.0
Signature · 2 lines
With best wishes
Dmitry
#3 lore raw

PATCH 2/3 clk: qcom: gdsc: propagate gdsc_enable() failure for ALWAYS_ON domains

In reply to Herman van Hazendonk

GENPD_FLAG_ALWAYS_ON requires the underlying domain to be on at genpd_init() time -- the framework will refuse to register the domain otherwise. When the cold readback in gdsc_init() finds an ALWAYS_ON GDSC powered down, the driver tries to bring it back up:

	} else if (sc->flags & ALWAYS_ON) {
		/* If ALWAYS_ON GDSCs are not ON, turn them ON */
		gdsc_enable(&sc->pd);
		on = true;
	}

but discards the return value: if gdsc_enable() fails (regmap write error, the long-form sequence's status poll times out, or the HW_CTRL hand-off errors) the code still sets on=true and falls through to pm_genpd_init(..., !on) -- which then registers the domain in the ON state and sets GENPD_FLAG_ALWAYS_ON, even though the silicon is actually off. Subsequent consumer probes will see genpd report "on" while accessing dead registers and hang or read garbage.

Catch the failure and surface it: returning the error from gdsc_init() makes the provider probe fail with the underlying errno, which propagates to consumers as -EPROBE_DEFER (or fatal if the hardware really is broken) rather than silently lying about the rail state.

---
 drivers/clk/qcom/gdsc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/clk/qcom/gdsc.c +3 -1
index b9b47f584f6d..a80a489763ed 100644
@@ -481,7 +481,9 @@ static int gdsc_init(struct gdsc *sc)
 
 	} else if (sc->flags & ALWAYS_ON) {
 		/* If ALWAYS_ON GDSCs are not ON, turn them ON */
-		gdsc_enable(&sc->pd);
+		ret = gdsc_enable(&sc->pd);
+		if (ret)
+			return ret;
 		on = true;
 	}
 
Signature · 1 line
2.43.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] clk: qcom: gdsc: propagate gdsc_enable() failure for ALWAYS_ON domains

Quoted 26 lines · Herman van Hazendonk

GENPD_FLAG_ALWAYS_ON requires the underlying domain to be on at genpd_init() time -- the framework will refuse to register the domain otherwise. When the cold readback in gdsc_init() finds an ALWAYS_ON GDSC powered down, the driver tries to bring it back up:

	} else if (sc->flags & ALWAYS_ON) {
		/* If ALWAYS_ON GDSCs are not ON, turn them ON */
		gdsc_enable(&sc->pd);
		on = true;
	}

but discards the return value: if gdsc_enable() fails (regmap write error, the long-form sequence's status poll times out, or the HW_CTRL hand-off errors) the code still sets on=true and falls through to pm_genpd_init(..., !on) -- which then registers the domain in the ON state and sets GENPD_FLAG_ALWAYS_ON, even though the silicon is actually off. Subsequent consumer probes will see genpd report "on" while accessing dead registers and hang or read garbage.

Catch the failure and surface it: returning the error from gdsc_init() makes the provider probe fail with the underlying errno, which propagates to consumers as -EPROBE_DEFER (or fatal if the hardware really is broken) rather than silently lying about the rail state.

Quoted 4 lines
---
 drivers/clk/qcom/gdsc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Fixes fb55bea1fe43 ("clk: qcom: gdsc: Add support for ALWAYS_ON gdscs")
Signature · 2 lines
With best wishes
Dmitry
#5 lore raw

PATCH 3/3 clk: qcom: gdsc: tear down per-domain genpds in gdsc_unregister()

In reply to Herman van Hazendonk

gdsc_unregister() removes the OF provider entry and tears down the parent/subdomain wiring, but never calls pm_genpd_remove() on the individual generic_pm_domain structures registered by gdsc_init():

	void gdsc_unregister(struct gdsc_desc *desc)
	{
		struct device *dev = desc->dev;
		size_t num = desc->num;

		gdsc_pm_subdomain_remove(desc, num);
		of_genpd_del_provider(dev->of_node);
	}

That leaves dangling entries on the global gpd_list. After a provider unbind/rebind cycle (deferred-probe replay during early boot, real module unload of a clk driver that owns GDSCs, or an OF-overlay tear- down) the next gdsc_init() will end up trying to re-register a name that is still in the list and pm_genpd_init() returns -EEXIST.

While we are here, flip the order so the consumer-facing OF provider entry is the first thing removed -- otherwise a fresh of_genpd_get_from_provider() call racing with the teardown could attach to a domain that is mid-removal.

Iterate the scs[] array and pm_genpd_remove() each registered domain after the subdomain links are torn down. The regulators stay devm- managed (devm_regulator_get_optional() in gdsc_register()), so the release happens automatically when the underlying device is unbound; just the genpd accounting needs to be undone explicitly.

---
 drivers/clk/qcom/gdsc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/clk/qcom/gdsc.c +9 -1
index a80a489763ed..71826ccbd9bd 100644
@@ -645,10 +645,18 @@ int gdsc_register(struct gdsc_desc *desc,
 void gdsc_unregister(struct gdsc_desc *desc)
 {
 	struct device *dev = desc->dev;
+	struct gdsc **scs = desc->scs;
 	size_t num = desc->num;
+	int i;
 
-	gdsc_pm_subdomain_remove(desc, num);
 	of_genpd_del_provider(dev->of_node);
+	gdsc_pm_subdomain_remove(desc, num);
+
+	for (i = 0; i < num; i++) {
+		if (!scs[i])
+			continue;
+		pm_genpd_remove(&scs[i]->pd);
+	}
 }
 
 /*
Signature · 1 line
2.43.0
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] clk: qcom: gdsc: tear down per-domain genpds in gdsc_unregister()

Quoted 30 lines · Herman van Hazendonk

gdsc_unregister() removes the OF provider entry and tears down the parent/subdomain wiring, but never calls pm_genpd_remove() on the individual generic_pm_domain structures registered by gdsc_init():

	void gdsc_unregister(struct gdsc_desc *desc)
	{
		struct device *dev = desc->dev;
		size_t num = desc->num;

		gdsc_pm_subdomain_remove(desc, num);
		of_genpd_del_provider(dev->of_node);
	}

That leaves dangling entries on the global gpd_list. After a provider unbind/rebind cycle (deferred-probe replay during early boot, real module unload of a clk driver that owns GDSCs, or an OF-overlay tear- down) the next gdsc_init() will end up trying to re-register a name that is still in the list and pm_genpd_init() returns -EEXIST.

While we are here, flip the order so the consumer-facing OF provider entry is the first thing removed -- otherwise a fresh of_genpd_get_from_provider() call racing with the teardown could attach to a domain that is mid-removal.

Iterate the scs[] array and pm_genpd_remove() each registered domain after the subdomain links are torn down. The regulators stay devm- managed (devm_regulator_get_optional() in gdsc_register()), so the release happens automatically when the underlying device is unbound; just the genpd accounting needs to be undone explicitly.

Quoted 4 lines
---
 drivers/clk/qcom/gdsc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
Fixes 45dd0e55317c ("clk: qcom: Add support for GDSCs")
Signature · 2 lines
With best wishes
Dmitry
maintainer Applied
#7 lore raw
In reply to Herman van Hazendonk
Quoted 8 lines · Herman van Hazendonk

While preparing MSM8x60 support for the GDSC core (a separate series, adding LEGACY_FOOTSWITCH and RPM_ALWAYS_ON), code review surfaced three pre-existing correctness issues in drivers/clk/qcom/gdsc.c that are independent of any new SoC enabling work. This series collects those three fixes so each can be reviewed and queued on its own schedule without blocking the MSM8x60 series.

[...]

Applied, thanks!

[1/3] clk: qcom: gdsc: propagate gdsc_check_status() errors from gdsc_poll_status

      commit: d69f0c2b8d292b4890c9f0fbe184dfc26c4de86c
[2/3] clk: qcom: gdsc: propagate gdsc_enable() failure for ALWAYS_ON domains
      commit: eea55fc694e132aacbe2cf4be7f345115e3d1801
[3/3] clk: qcom: gdsc: tear down per-domain genpds in gdsc_unregister()
      commit: 86b23609d5e17a770d03037e53c6a443e742a6e6

Best regards,

Signature · 1 line
Bjorn Andersson <[email protected]>