[PATCH v2 4/5] thermal/drivers/mediatek/lvts: recompute shared trips and mask first

Ilia Snezhkov via B4 Relay devnull+iasnezhkov.gmail.com at kernel.org
Wed Dec 2 18:20:00 PST 2026


From: Ilia Snezhkov <iasnezhkov at 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 at 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)






More information about the linux-arm-kernel mailing list