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 04/12] thermal: core: Initialize thermal zone aggregation coefficients
Date: Thu, 19 Nov 2026 10:46:46 +0100	[thread overview]
Message-ID: <20261119094706.3439040-5-daniel.lezcano@oss.qualcomm.com> (raw)
In-Reply-To: <20261119094706.3439040-1-daniel.lezcano@oss.qualcomm.com>

Use PERMILLE from linux/units.h for coefficients in thousandths and
add a helper in thermal_aggregation.c to initialize them directly in the
aggregation entries before publication.

Treat zero as an unspecified coefficient before initialization. Preserve
supplied coefficient sets without normalization. Require validation of
the description before initialization; partial specification is rejected
by thermal_zone_aggregate_validate().
When all coefficients are uninitialized, distribute a total of 1000 as
evenly as possible, assigning the remainder in source order to approximate
the arithmetic mean.

Reject default generation for more sources than the coefficient scale
because it would produce zero coefficients. Leave source pointers and
the offset in thermal_zone_params unchanged.

Signed-off-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
---
 drivers/thermal/thermal_aggregation.c | 80 +++++++++++++++++++++++++++
 drivers/thermal/thermal_core.h        |  5 +-
 2 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/drivers/thermal/thermal_aggregation.c b/drivers/thermal/thermal_aggregation.c
index f85f5a487d60..8b503fab25bc 100644
--- a/drivers/thermal/thermal_aggregation.c
+++ b/drivers/thermal/thermal_aggregation.c
@@ -1,9 +1,89 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/errno.h>
+#include <linux/units.h>
 
 #include "thermal_core.h"
 
+/**
+ * thermal_zone_aggregate_init_coefficients() - initialize aggregation multipliers
+ * @aggregate: aggregation description to initialize before publication
+ *
+ * The description must be validated before calling this helper. Before
+ * initialization, zero denotes an unspecified coefficient. If all are zero,
+ * generate approximately equal contributions summing to
+ * PERMILLE, distributing the remainder in source order.
+ * If all coefficients are nonzero, leave them unchanged without normalization.
+ * Partial specification is rejected by thermal_zone_aggregate_validate().
+ *
+ * The zone parameters' offset and source pointers are not modified.
+ *
+ * Return: 0 on success or -EINVAL for invalid arguments. Entries are unchanged
+ * on failure.
+ */
+static int __maybe_unused
+thermal_zone_aggregate_init_coefficients(struct thermal_zone_aggregate *aggregate)
+{
+	unsigned int initialized = 0;
+	unsigned int base, remainder, i;
+
+	if (!aggregate || !aggregate->entries || !aggregate->num_entries)
+		return -EINVAL;
+
+	for (i = 0; i < aggregate->num_entries; i++)
+		initialized += aggregate->entries[i].coefficient != 0;
+
+	/*
+	 * Validation has rejected partial specification. If coefficients are
+	 * supplied, preserve the explicit affine combination without normalization.
+	 */
+	if (initialized)
+		return 0;
+
+	/*
+	 * Current systems are far from aggregating 1000 sources, but enforce
+	 * this limit for unlikely yet possible configurations. More sources
+	 * than the scale would produce zero default coefficients, which denote
+	 * uninitialized entries.
+	 */
+	if (aggregate->num_entries > PERMILLE)
+		return -EINVAL;
+
+	/*
+	 * With no coefficients supplied, approximate the arithmetic mean of
+	 * the source temperatures:
+	 *
+	 *     T = sum(coefficient[i] * temperature[i]) / 1000 + offset
+	 *
+	 * Each source should contribute equally, so its ideal coefficient is
+	 * PERMILLE / num_entries. Integer division rounds this
+	 * down, leaving a remainder that must be distributed to keep the total
+	 * coefficient sum exactly equal to PERMILLE.
+	 *
+	 * Assign one additional unit to each of the first 'remainder' sources.
+	 * This makes the distribution deterministic in source order, with any
+	 * two coefficients differing by at most one. For example:
+	 *
+	 *     2 sources: 500, 500
+	 *     3 sources: 334, 333, 333
+	 *     6 sources: 167, 167, 167, 167, 166, 166
+	 *
+	 * Equal source temperatures therefore retain unity gain. For unequal
+	 * temperatures, the result approximates their arithmetic mean with a
+	 * small bias from coefficient rounding. The signed offset in the zone
+	 * parameters remains independent and is applied after division.
+	 *
+	 * These defaults apply only when every coefficient is uninitialized;
+	 * explicitly supplied coefficients are never normalized.
+	 */
+	base = PERMILLE / aggregate->num_entries;
+	remainder = PERMILLE % aggregate->num_entries;
+	for (i = 0; i < aggregate->num_entries; i++)
+		aggregate->entries[i].coefficient = base + (i < remainder);
+
+	return 0;
+}
+
 /**
  * thermal_zone_aggregate_validate() - check an aggregation before initialization
  * @aggregate: aggregation description to validate before coefficient initialization
diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
index a5fdf5c24b6c..365704584966 100644
--- a/drivers/thermal/thermal_core.h
+++ b/drivers/thermal/thermal_core.h
@@ -66,7 +66,7 @@ struct thermal_governor {
 /**
  * struct thermal_zone_aggregate_entry - source zone and its coefficient
  * @tz: source thermal zone
- * @coefficient: signed multiplier for the source zone temperature
+ * @coefficient: signed multiplier in thousandths, zero when uninitialized
  */
 struct thermal_zone_aggregate_entry {
 	struct thermal_zone_device *tz;
@@ -79,7 +79,8 @@ struct thermal_zone_aggregate_entry {
  * @num_entries: number of entries in the array
  *
  * Represents the sum of each source zone temperature multiplied by its
- * coefficient. The constant offset is stored in struct thermal_zone_params.
+ * coefficient in thousandths, divided by 1000. The constant offset is
+ * stored in struct thermal_zone_params.
  */
 struct thermal_zone_aggregate {
 	struct thermal_zone_aggregate_entry *entries;
-- 
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 ` Daniel Lezcano [this message]
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 ` [PATCH v1 10/12] thermal: of: Instantiate aggregates after their source zones Daniel Lezcano
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-5-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®