mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
To: daniel.lezcano@oss.qualcomm.com, rafael@kernel.org
Cc: Zhang Rui <rui.zhang@intel.com>,
	Lukasz Luba <lukasz.luba@arm.com>, Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Anvesh Jain P <anvesh.p@oss.qualcomm.com>,
	Ulf Hansson <ulf.hansson@oss.qualcomm.com>,
	linux-pm@vger.kernel.org,
	Daniel Lezcano <daniel.lezcano@kernel.org>,
	linux-kernel@vger.kernel.org (open list)
Subject: [PATCH v1 10/12] thermal: of: Instantiate aggregates after their source zones
Date: Thu, 19 Nov 2026 10:46:52 +0100	[thread overview]
Message-ID: <20261119094706.3439040-11-daniel.lezcano@oss.qualcomm.com> (raw)
In-Reply-To: <20261119094706.3439040-1-daniel.lezcano@oss.qualcomm.com>

Discover multi-source thermal zones at late init and resolve their
hardware provider and sensor ID references to registered source zones.
Retry pending descriptions synchronously after successful source
publication, without workqueues or changes to sensor driver APIs.

Serialize creation and removal with the OF lifecycle mutex. Acquire
source references while filling aggregation entries, unregister dependent
aggregates before source teardown, and release references and allocations
on failure or removal. Keep descriptions pending for source module reload.

Use the shared node parser with core aggregation operations. Preserve
legacy single-source slope and offset calibration. Missing multi-source
coefficients request averaging defaults; explicit integer multipliers are
converted to thousandths with overflow checks and the offset is unscaled.
Reject partial arrays and explicit zero multipliers, which cannot encode
presence under the current aggregation initialization contract.

Skip multi-source nodes during ordinary hardware zone lookup so they
cannot be bound directly to an individual sensor callback.

Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
 drivers/thermal/thermal_of.c | 217 ++++++++++++++++++++++++++++++++++-
 1 file changed, 216 insertions(+), 1 deletion(-)

diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
index 1fa7e29f5bc8..64ae7769edcb 100644
--- a/drivers/thermal/thermal_of.c
+++ b/drivers/thermal/thermal_of.c
@@ -10,8 +10,11 @@
 
 #include <linux/err.h>
 #include <linux/export.h>
+#include <linux/init.h>
 #include <linux/of.h>
 #include <linux/slab.h>
+#include <linux/overflow.h>
+#include <linux/units.h>
 #include <linux/thermal.h>
 #include <linux/types.h>
 #include <linux/string.h>
@@ -31,6 +34,14 @@ struct thermal_of_source {
 	struct thermal_zone_device *tz;
 };
 
+struct thermal_of_aggregate {
+	struct list_head node;
+	struct device_node *np;
+	struct thermal_zone_aggregate aggregate;
+	struct thermal_zone_device *tz;
+	bool failed;
+};
+
 /*
  * OF lifecycle operations are serialized outside thermal zone locks. Thermal
  * callbacks must not acquire this mutex. Source removal holds it until all OF
@@ -38,6 +49,11 @@ struct thermal_of_source {
  */
 static DEFINE_MUTEX(thermal_of_lock);
 static LIST_HEAD(thermal_of_sources);
+static LIST_HEAD(thermal_of_aggregates);
+static bool thermal_of_aggregates_ready;
+
+static void thermal_of_aggregate_retry(void);
+static void thermal_of_aggregate_release(struct thermal_of_aggregate *aggregate);
 
 static int thermal_of_source_add(struct device_node *provider, int id,
 				 struct thermal_zone_device *tz)
@@ -59,18 +75,29 @@ static int thermal_of_source_add(struct device_node *provider, int id,
 	source->id = id;
 	source->tz = tz;
 	list_add_tail(&source->node, &thermal_of_sources);
+	thermal_of_aggregate_retry();
 	return 0;
 }
 
 static void thermal_of_source_remove(struct thermal_zone_device *tz)
 {
 	struct thermal_of_source *source;
+	struct thermal_of_aggregate *aggregate;
+	unsigned int i;
 
 	guard(mutex)(&thermal_of_lock);
 	list_for_each_entry(source, &thermal_of_sources, node) {
 		if (source->tz != tz)
 			continue;
 		list_del(&source->node);
+		list_for_each_entry(aggregate, &thermal_of_aggregates, node) {
+			for (i = 0; i < aggregate->aggregate.num_entries; i++) {
+				if (aggregate->aggregate.entries[i].tz == tz) {
+					thermal_of_aggregate_release(aggregate);
+					break;
+				}
+			}
+		}
 		of_node_put(source->provider);
 		kfree(source);
 		break;
@@ -217,6 +244,9 @@ static struct device_node *of_thermal_zone_find(struct device_node *sensor, int
 			pr_err("%pOFP: missing thermal sensor\n", child);
 			return ERR_PTR(-EINVAL);
 		}
+		/* Multi-source nodes are instantiated by the OF aggregate manager. */
+		if (count > 1)
+			continue;
 
 		for (i = 0; i < count; i++) {
 
@@ -486,7 +516,8 @@ thermal_of_zone_register_node(struct device_node *np, void *data,
 	if (ret) {
 		pr_err("Failed to enable thermal zone '%s', id=%d: %d\n",
 		       tz->type, tz->id, ret);
-		thermal_of_zone_unregister(tz);
+		thermal_zone_device_disable(tz);
+		thermal_zone_device_unregister(tz);
 		return ERR_PTR(ret);
 	}
 
@@ -528,6 +559,190 @@ thermal_of_zone_register(struct device_node *sensor, int id, void *data,
 	return tz;
 }
 
+#ifdef CONFIG_THERMAL_AGGREGATION
+/* All aggregate construction and destruction runs under thermal_of_lock. */
+static void thermal_of_aggregate_release(struct thermal_of_aggregate *aggregate)
+{
+	unsigned int i;
+
+	lockdep_assert_held(&thermal_of_lock);
+	if (aggregate->tz) {
+		/* Do not recurse into OF source removal while holding the OF mutex. */
+		thermal_zone_device_disable(aggregate->tz);
+		thermal_zone_device_unregister(aggregate->tz);
+		aggregate->tz = NULL;
+	}
+	for (i = 0; i < aggregate->aggregate.num_entries; i++)
+		thermal_zone_device_put(aggregate->aggregate.entries[i].tz);
+	kfree(aggregate->aggregate.entries);
+	aggregate->aggregate.entries = NULL;
+	aggregate->aggregate.num_entries = 0;
+}
+
+static struct thermal_zone_device *
+thermal_of_source_get(const struct of_phandle_args *spec)
+{
+	struct thermal_of_source *source;
+	int id = spec->args_count ? spec->args[0] : 0;
+
+	lockdep_assert_held(&thermal_of_lock);
+	/* The existing OF source registration API identifies sensors by one ID. */
+	if (spec->args_count > 1)
+		return ERR_PTR(-EINVAL);
+
+	list_for_each_entry(source, &thermal_of_sources, node)
+		if (source->provider == spec->np && source->id == id)
+			return thermal_zone_device_get(source->tz);
+
+	return ERR_PTR(-EPROBE_DEFER);
+}
+
+static int thermal_of_aggregate_create(struct thermal_of_aggregate *aggregate)
+{
+	struct thermal_zone_device_ops ops = {};
+	struct thermal_zone_params tzp = { .no_hwmon = true };
+	struct device_node *np = aggregate->np;
+	struct thermal_zone_aggregate *desc = &aggregate->aggregate;
+	int count, ncoef, ret, i;
+
+	lockdep_assert_held(&thermal_of_lock);
+	count = of_count_phandle_with_args(np, "thermal-sensors", "#thermal-sensor-cells");
+	if (count < 2)
+		return -EINVAL;
+
+	desc->entries = kcalloc(count, sizeof(*desc->entries), GFP_KERNEL);
+	if (!desc->entries)
+		return -ENOMEM;
+
+	for (i = 0; i < count; i++) {
+		struct of_phandle_args spec;
+		struct thermal_zone_device *source;
+
+		ret = of_parse_phandle_with_args(np, "thermal-sensors", "#thermal-sensor-cells",
+						 i, &spec);
+		if (ret)
+			goto release;
+		source = thermal_of_source_get(&spec);
+		of_node_put(spec.np);
+		if (IS_ERR(source)) {
+			ret = PTR_ERR(source);
+			goto release;
+		}
+		desc->entries[i].tz = source;
+		desc->num_entries++;
+	}
+
+	/*
+	 * Missing multipliers request the core's averaging defaults. Explicit DT
+	 * multipliers retain their unscaled integer meaning: convert to internal
+	 * thousandths, checking overflow, and leave the final offset unscaled.
+	 * Legacy single-source calibration never passes through this path.
+	 */
+	if (of_find_property(np, "coefficients", NULL)) {
+		ncoef = of_property_count_u32_elems(np, "coefficients");
+		if (ncoef != count && ncoef != count + 1) {
+			ret = -EINVAL;
+			goto release;
+		}
+		for (i = 0; i < count; i++) {
+			u32 raw;
+
+			ret = of_property_read_u32_index(np, "coefficients", i, &raw);
+			if (ret)
+				goto release;
+			if (check_mul_overflow((s32)raw, (s32)PERMILLE,
+					       &desc->entries[i].coefficient)) {
+				ret = -ERANGE;
+				goto release;
+			}
+			/* Zero cannot encode presence in the core initializer. */
+			if (!desc->entries[i].coefficient) {
+				ret = -EINVAL;
+				goto release;
+			}
+		}
+		if (ncoef == count + 1) {
+			u32 raw;
+
+			ret = of_property_read_u32_index(np, "coefficients", count, &raw);
+			if (ret)
+				goto release;
+			tzp.offset = (s32)raw;
+		}
+	}
+	of_property_read_u32(np, "sustainable-power", &tzp.sustainable_power);
+	tzp.aggregate = desc;
+	aggregate->tz = thermal_of_zone_register_node(np, NULL, &ops, &tzp);
+	if (IS_ERR(aggregate->tz)) {
+		ret = PTR_ERR(aggregate->tz);
+		aggregate->tz = NULL;
+		goto release;
+	}
+	return 0;
+
+release:
+	thermal_of_aggregate_release(aggregate);
+	return ret;
+}
+
+static void thermal_of_aggregate_retry(void)
+{
+	struct thermal_of_aggregate *aggregate;
+
+	lockdep_assert_held(&thermal_of_lock);
+	if (!thermal_of_aggregates_ready)
+		return;
+
+	list_for_each_entry(aggregate, &thermal_of_aggregates, node) {
+		int ret;
+
+		if (aggregate->tz || aggregate->failed)
+			continue;
+		ret = thermal_of_aggregate_create(aggregate);
+		if (ret && ret != -EPROBE_DEFER && ret != -ENOMEM) {
+			pr_err("%pOF: failed to create aggregate: %d\n", aggregate->np, ret);
+			aggregate->failed = true;
+		}
+	}
+}
+
+static int __init thermal_of_aggregate_init(void)
+{
+	struct device_node *np __free(device_node) = of_find_node_by_name(NULL, "thermal-zones");
+
+	if (!np)
+		return 0;
+
+	guard(mutex)(&thermal_of_lock);
+	for_each_available_child_of_node_scoped(np, child) {
+		struct thermal_of_aggregate *aggregate;
+		int count;
+
+		count = of_count_phandle_with_args(child, "thermal-sensors",
+						   "#thermal-sensor-cells");
+		if (count <= 1)
+			continue;
+		aggregate = kzalloc_obj(*aggregate);
+		if (!aggregate) {
+			struct thermal_of_aggregate *pos, *next;
+
+			list_for_each_entry_safe(pos, next, &thermal_of_aggregates, node) {
+				list_del(&pos->node);
+				of_node_put(pos->np);
+				kfree(pos);
+			}
+			return -ENOMEM;
+		}
+		aggregate->np = of_node_get(child);
+		list_add_tail(&aggregate->node, &thermal_of_aggregates);
+	}
+	thermal_of_aggregates_ready = true;
+	thermal_of_aggregate_retry();
+	return 0;
+}
+late_initcall(thermal_of_aggregate_init);
+#endif /* CONFIG_THERMAL_AGGREGATION */
+
 static void devm_thermal_of_zone_release(struct device *dev, void *res)
 {
 	thermal_of_zone_unregister(*(struct thermal_zone_device **)res);
-- 
2.43.0


  parent reply	other threads:[~2026-11-19  9:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261119094706.3439040-1-daniel.lezcano@oss.qualcomm.com>
2026-11-19  9:46 ` [PATCH v1 01/12] thermal: core: Add thermal zone device reference helpers Daniel Lezcano
2026-11-19  9:46 ` [PATCH v1 02/12] thermal: core: Introduce private thermal zone aggregation structures Daniel Lezcano
2026-11-19  9:46 ` [PATCH v1 03/12] thermal: core: Validate thermal zone aggregation descriptions Daniel Lezcano
2026-11-19  9:46 ` [PATCH v1 04/12] thermal: core: Initialize thermal zone aggregation coefficients Daniel Lezcano
2026-11-19  9:46 ` [PATCH v1 05/12] thermal: core: Dispatch aggregate temperature reads Daniel Lezcano
2026-11-19  9:46 ` [PATCH v1 06/12] thermal: core: Calculate aggregated thermal zone temperatures Daniel Lezcano
2026-11-19  9:46 ` [PATCH v1 07/12] thermal: core: Initialize aggregation during zone registration Daniel Lezcano
2026-11-19  9:46 ` [PATCH v1 08/12] thermal: of: Separate zone parsing from sensor lookup Daniel Lezcano
2026-11-19  9:46 ` [PATCH v1 09/12] thermal: of: Track registered hardware temperature sources Daniel Lezcano
2026-11-19  9:46 ` Daniel Lezcano [this message]
2026-11-19  9:46 ` [PATCH v1 11/12] dt-bindings: thermal: Describe multi-source zone aggregation Daniel Lezcano
2026-11-19  9:46 ` [PATCH v1 12/12] thermal: testing: Add KUnit coverage for " Daniel Lezcano

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=20261119094706.3439040-11-daniel.lezcano@oss.qualcomm.com \
    --to=daniel.lezcano@oss.qualcomm.com \
    --cc=anvesh.p@oss.qualcomm.com \
    --cc=daniel.lezcano@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    --cc=rafael@kernel.org \
    --cc=robh@kernel.org \
    --cc=rui.zhang@intel.com \
    --cc=ulf.hansson@oss.qualcomm.com \
    /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

all inboxes | Powered by JetHome®