From: Ilia Snezhkov via B4 Relay <devnull+iasnezhkov.gmail.com@kernel.org>
To: daniel.lezcano@kernel.org, rafael@kernel.org,
rui.zhang@intel.com, lukasz.luba@arm.com
Cc: matthias.bgg@gmail.com, angelogioacchino.delregno@collabora.com,
bchihi@baylibre.com, nfraprado@collabora.com,
linux-pm@vger.kernel.org, linux-mediatek@lists.infradead.org,
linux-arm-kernel@lists.infradead.org,
phone-devel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 4/5] thermal/drivers/mediatek/lvts: recompute shared trips and mask first
Date: Thu, 03 Dec 2026 02:20:00 +0000 [thread overview]
Message-ID: <20261203021956.43607-5-iasnezhkov@gmail.com> (raw)
In-Reply-To: <20261203021956.43607-1-iasnezhkov@gmail.com>
From: Ilia Snezhkov <iasnezhkov@gmail.com>
Multi-sensor threshold management in lvts_set_trips() has four issues:
1. lvts_should_update_thresh() compares `high > lvts_ctrl->high_thresh`
without excluding INT_MAX (no upper trip). When any sensor on a
controller has no active upper trip, INT_MAX wins the comparison,
overwrites lvts_ctrl->high_thresh with INT_MAX, passes INT_MAX to
lvts_temp_to_raw(), and leaves sibling sensors with finite trips
masked while high_offset_inten stays enabled for the INT_MAX sensor.
2. When the sensor currently holding lvts_ctrl->high_thresh cools down
to a lower high threshold, lvts_should_update_thresh() sees no
sensor matching the old threshold and installs the cooling sensor's
threshold instead of scanning siblings for the next highest finite
threshold, leaving hotter siblings masked.
3. lvts_update_irq_mask() requires both low_thresh and high_thresh to
match simultaneously, masking the high-offset interrupt of sibling
sensors that share the same high_thresh with a different low_thresh.
4. lvts_set_trips() enables bits in LVTS_MONINT via
lvts_update_irq_mask() before writing LVTS_OFFSETL and LVTS_OFFSETH,
exposing a window where interrupts are enabled against stale
threshold registers.
Replace lvts_should_update_thresh() with lvts_recompute_thresh(), which
selects the highest finite high threshold across valid sensors (breaking
ties by highest low threshold so upward trips remain armed on cooler
siblings, and falling back to the highest finite low threshold only when
no sensor has a finite upper trip). Match low and high interrupt masks
independently, skip converting new_high == INT_MAX (leaving
LVTS_OFFSETH untouched with high_offset_inten masked), and mask all
offset interrupts before updating LVTS_OFFSETL and LVTS_OFFSETH.
Fixes: 2bba1acf7a4c ("thermal/drivers/mediatek/lvts_thermal: Manage threshold between sensors")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes in v2:
- New consolidated patch combining v1 patch 2/4's INT_MAX trip handling
with lvts_recompute_thresh(), independent low/high IRQ mask matching,
and masking offset IRQs before writing LVTS_OFFSETL/LVTS_OFFSETH.
Found and drafted with an AI coding assistant.
drivers/thermal/mediatek/lvts_thermal.c | 200 +++++++++++++++---------
1 file changed, 128 insertions(+), 72 deletions(-)
diff --git a/drivers/thermal/mediatek/lvts_thermal.c b/drivers/thermal/mediatek/lvts_thermal.c
index aaadc00..7b4ede8 100644
--- a/drivers/thermal/mediatek/lvts_thermal.c
+++ b/drivers/thermal/mediatek/lvts_thermal.c
@@ -385,61 +385,108 @@ static int lvts_get_temp(struct thermal_zone_device *tz, int *temp)
return 0;
}
+static const u32 lvts_high_offset_inten_masks[] = {
+ LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR0,
+ LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR1,
+ LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR2,
+ LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR3,
+};
+
+static const u32 lvts_low_offset_inten_masks[] = {
+ LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR0,
+ LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR1,
+ LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR2,
+ LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR3,
+};
+
+static void lvts_mask_offset_irqs(struct lvts_ctrl *lvts_ctrl)
+{
+ u32 value;
+ int i;
+
+ value = readl(LVTS_MONINT(lvts_ctrl->base));
+ lvts_for_each_valid_sensor(i, lvts_ctrl)
+ value &= ~(lvts_low_offset_inten_masks[i] |
+ lvts_high_offset_inten_masks[i]);
+ writel(value, LVTS_MONINT(lvts_ctrl->base));
+}
+
static void lvts_update_irq_mask(struct lvts_ctrl *lvts_ctrl)
{
- static const u32 high_offset_inten_masks[] = {
- LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR0,
- LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR1,
- LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR2,
- LVTS_MONINT_OFFSET_HIGH_INTEN_SENSOR3,
- };
- static const u32 low_offset_inten_masks[] = {
- LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR0,
- LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR1,
- LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR2,
- LVTS_MONINT_OFFSET_LOW_INTEN_SENSOR3,
- };
- u32 value = 0;
+ bool low_valid = lvts_ctrl->low_thresh != -INT_MAX &&
+ lvts_ctrl->low_thresh != INT_MIN;
+ bool high_valid = lvts_ctrl->high_thresh != INT_MAX &&
+ lvts_ctrl->high_thresh != INT_MIN;
+ u32 value;
int i;
value = readl(LVTS_MONINT(lvts_ctrl->base));
lvts_for_each_valid_sensor(i, lvts_ctrl) {
- if (lvts_ctrl->sensors[i].high_thresh == lvts_ctrl->high_thresh
- && lvts_ctrl->sensors[i].low_thresh == lvts_ctrl->low_thresh) {
- /*
- * The minimum threshold needs to be configured in the
- * OFFSETL register to get working interrupts, but we
- * don't actually want to generate interrupts when
- * crossing it.
- */
- if (lvts_ctrl->low_thresh == -INT_MAX) {
- value &= ~low_offset_inten_masks[i];
- value |= high_offset_inten_masks[i];
- } else {
- value |= low_offset_inten_masks[i] | high_offset_inten_masks[i];
- }
- } else {
- value &= ~(low_offset_inten_masks[i] | high_offset_inten_masks[i]);
- }
+ /*
+ * The minimum threshold needs to be configured in the
+ * OFFSETL register to get working interrupts, but we
+ * don't actually want to generate interrupts when
+ * crossing it.
+ */
+ if (low_valid &&
+ lvts_ctrl->sensors[i].low_thresh == lvts_ctrl->low_thresh)
+ value |= lvts_low_offset_inten_masks[i];
+ else
+ value &= ~lvts_low_offset_inten_masks[i];
+
+ if (high_valid &&
+ lvts_ctrl->sensors[i].high_thresh == lvts_ctrl->high_thresh)
+ value |= lvts_high_offset_inten_masks[i];
+ else
+ value &= ~lvts_high_offset_inten_masks[i];
}
writel(value, LVTS_MONINT(lvts_ctrl->base));
}
-static bool lvts_should_update_thresh(struct lvts_ctrl *lvts_ctrl, int high)
+static void lvts_recompute_thresh(struct lvts_ctrl *lvts_ctrl,
+ int *low, int *high)
{
+ bool has_high = false;
+ bool has_low = false;
+ int best_high = INT_MIN;
+ int best_high_low = -INT_MAX;
+ int best_low = -INT_MAX;
int i;
- if (high > lvts_ctrl->high_thresh)
- return true;
+ lvts_for_each_valid_sensor(i, lvts_ctrl) {
+ int s_low = lvts_ctrl->sensors[i].low_thresh;
+ int s_high = lvts_ctrl->sensors[i].high_thresh;
- lvts_for_each_valid_sensor(i, lvts_ctrl)
- if (lvts_ctrl->sensors[i].high_thresh == lvts_ctrl->high_thresh
- && lvts_ctrl->sensors[i].low_thresh == lvts_ctrl->low_thresh)
- return false;
+ if (s_high == INT_MIN && s_low == INT_MIN)
+ continue;
+
+ if (s_high != INT_MAX && s_high != INT_MIN) {
+ if (!has_high || s_high > best_high ||
+ (s_high == best_high && s_low > best_high_low)) {
+ has_high = true;
+ best_high = s_high;
+ best_high_low = s_low;
+ }
+ } else if (s_low != -INT_MAX && s_low != INT_MIN) {
+ if (!has_low || s_low > best_low) {
+ has_low = true;
+ best_low = s_low;
+ }
+ }
+ }
- return true;
+ if (has_high) {
+ *low = best_high_low;
+ *high = best_high;
+ } else if (has_low) {
+ *low = best_low;
+ *high = INT_MAX;
+ } else {
+ *low = -INT_MAX;
+ *high = INT_MAX;
+ }
}
static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
@@ -449,51 +496,60 @@ static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
sensors[lvts_sensor->id]);
const struct lvts_data *lvts_data = lvts_ctrl->lvts_data;
void __iomem *base = lvts_sensor->base;
- u32 raw_low = lvts_temp_to_raw(low != -INT_MAX ? low : LVTS_MINIMUM_THRESHOLD,
- lvts_data);
- u32 raw_high = lvts_temp_to_raw(high, lvts_data);
bool should_update_thresh;
+ int new_low, new_high;
guard(spinlock)(&lvts_ctrl->lock);
lvts_sensor->low_thresh = low;
lvts_sensor->high_thresh = high;
- should_update_thresh = lvts_should_update_thresh(lvts_ctrl, high);
+ lvts_recompute_thresh(lvts_ctrl, &new_low, &new_high);
+ should_update_thresh = new_low != lvts_ctrl->low_thresh ||
+ new_high != lvts_ctrl->high_thresh;
+
if (should_update_thresh) {
- lvts_ctrl->high_thresh = high;
- lvts_ctrl->low_thresh = low;
- }
- lvts_update_irq_mask(lvts_ctrl);
+ u32 raw_low = lvts_temp_to_raw(new_low != -INT_MAX ? new_low :
+ LVTS_MINIMUM_THRESHOLD,
+ lvts_data);
- if (!should_update_thresh)
- return 0;
+ lvts_mask_offset_irqs(lvts_ctrl);
- /*
- * Low offset temperature threshold
- *
- * LVTS_OFFSETL
- *
- * Bits:
- *
- * 14-0 : Raw temperature for threshold
- */
- pr_debug("%s: Setting low limit temperature interrupt: %d\n",
- thermal_zone_device_type(tz), low);
- writel(raw_low, LVTS_OFFSETL(base));
+ lvts_ctrl->low_thresh = new_low;
+ lvts_ctrl->high_thresh = new_high;
- /*
- * High offset temperature threshold
- *
- * LVTS_OFFSETH
- *
- * Bits:
- *
- * 14-0 : Raw temperature for threshold
- */
- pr_debug("%s: Setting high limit temperature interrupt: %d\n",
- thermal_zone_device_type(tz), high);
- writel(raw_high, LVTS_OFFSETH(base));
+ /*
+ * Low offset temperature threshold
+ *
+ * LVTS_OFFSETL
+ *
+ * Bits:
+ *
+ * 14-0 : Raw temperature for threshold
+ */
+ pr_debug("%s: Setting low limit temperature interrupt: %d\n",
+ thermal_zone_device_type(tz), new_low);
+ writel(raw_low, LVTS_OFFSETL(base));
+
+ if (new_high != INT_MAX) {
+ u32 raw_high = lvts_temp_to_raw(new_high, lvts_data);
+
+ /*
+ * High offset temperature threshold
+ *
+ * LVTS_OFFSETH
+ *
+ * Bits:
+ *
+ * 14-0 : Raw temperature for threshold
+ */
+ pr_debug("%s: Setting high limit temperature interrupt: %d\n",
+ thermal_zone_device_type(tz), new_high);
+ writel(raw_high, LVTS_OFFSETH(base));
+ }
+ }
+
+ lvts_update_irq_mask(lvts_ctrl);
return 0;
}
--
2.54.0 (Apple Git-157)
next prev parent reply other threads:[~2026-12-03 2:19 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 2:19 [PATCH v2 0/5] thermal/drivers/mediatek/lvts: CALSCALE, overflow, trip, and IRQ fixes Ilia Snezhkov via B4 Relay
2026-12-03 2:19 ` [PATCH v2 1/5] thermal/drivers/mediatek/lvts: write LVTS_CALSCALE in configure Ilia Snezhkov via B4 Relay
2026-12-03 2:19 ` [PATCH v2 2/5] thermal/drivers/mediatek/lvts: fix 32-bit overflow in temp_to_raw Ilia Snezhkov via B4 Relay
2026-12-03 2:19 ` [PATCH v2 3/5] thermal/drivers/mediatek/lvts: serialize lvts_set_trips() per ctrl Ilia Snezhkov via B4 Relay
2026-12-03 2:20 ` Ilia Snezhkov via B4 Relay [this message]
2026-12-03 2:20 ` [PATCH v2 5/5] thermal/drivers/mediatek/lvts: skip sensors without tz in IRQ Ilia Snezhkov via B4 Relay
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=20261203021956.43607-5-iasnezhkov@gmail.com \
--to=devnull+iasnezhkov.gmail.com@kernel.org \
--cc=angelogioacchino.delregno@collabora.com \
--cc=bchihi@baylibre.com \
--cc=daniel.lezcano@kernel.org \
--cc=iasnezhkov@gmail.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=linux-pm@vger.kernel.org \
--cc=lukasz.luba@arm.com \
--cc=matthias.bgg@gmail.com \
--cc=nfraprado@collabora.com \
--cc=phone-devel@vger.kernel.org \
--cc=rafael@kernel.org \
--cc=rui.zhang@intel.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