Skip to contentSkip to conversation

thermal/drivers/mediatek/lvts_thermal: CALSCALE, overflow, locking, and IRQ NULL fixes

PATCH New Ilia Snezhkov 1 participant 5 messages Nov 26

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

cover thermal/drivers/mediatek/lvts_thermal: CALSCALE, overflow, locking, and IRQ NULL fixes

This four-patch series fixes four bugs in
drivers/thermal/mediatek/lvts_thermal.c:

1. Write LVTS_CALSCALE_CONF (0x300) to the LVTS_CALSCALE register instead of overwriting a local variable and discarding it.
2. Avoid 32-bit overflow and unsigned truncation in lvts_temp_to_raw_mt7988(), and skip converting high == INT_MAX in lvts_set_trips() while masking high-offset IRQ.
3. Serialize lvts_set_trips() with a per-controller spinlock_t in struct lvts_ctrl so concurrent updates across thermal zones on the same controller cannot corrupt shared thresholds or LVTS_MONINT via unsynchronized RMW.
4. Check lvts_ctrl->sensors[i].tz before calling thermal_zone_device_update() in lvts_ctrl_irq_handler() for disabled or DT-absent sensor slots.

Found and drafted with an AI coding assistant while bringing up MT6789.

Ilia Snezhkov (4):
thermal/drivers/mediatek/lvts_thermal: write LVTS_CALSCALE in
lvts_ctrl_configure()
thermal/drivers/mediatek/lvts_thermal: avoid 32-bit overflow in

    lvts_temp_to_raw_mt7988() and INT_MAX trips

thermal/drivers/mediatek/lvts_thermal: serialize lvts_set_trips() per
controller
thermal/drivers/mediatek/lvts_thermal: skip sensors without a thermal
zone in IRQ

 drivers/thermal/mediatek/lvts_thermal.c | 30 ++++++++++++++++---------
 1 file changed, 20 insertions(+), 10 deletions(-)
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

PATCH 1/4 thermal/drivers/mediatek/lvts_thermal: write LVTS_CALSCALE in lvts_ctrl_configure()

lvts_ctrl_configure() assigns 0x300 and LVTS_CALSCALE_CONF (0x300) to the local variable value, then immediately overwrites value with the LVTS_MSRCTL0 filter setting without ever writing LVTS_CALSCALE (0x0048).

Remove the dead duplicate assignment and write LVTS_CALSCALE_CONF to LVTS_CALSCALE(lvts_ctrl->base), matching the comment above it and MediaTek's downstream soc_temp_lvts initialization sequence.

Found and drafted with an AI coding assistant.

Fixes f5f633b18234 ("thermal/drivers/mediatek: Add the Low Voltage Thermal Sensor driver")
---
 drivers/thermal/mediatek/lvts_thermal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/thermal/mediatek/lvts_thermal.c +1 -1
index 350fffd..e034c6c 100644
@@ -1197,8 +1197,8 @@ static int lvts_ctrl_configure(struct device *dev, struct lvts_ctrl *lvts_ctrl)
 	/*
 	 * LVTS_CALSCALE : ADC voltage round
 	 */
-	value = 0x300;
 	value = LVTS_CALSCALE_CONF;
+	writel(value, LVTS_CALSCALE(lvts_ctrl->base));
 
 	/*
 	 * LVTS_MSRCTL0 : Sensor filtering strategy
Signature · 1 line
2.54.0 (Apple Git-157)
#2 lore raw

PATCH 2/4 thermal/drivers/mediatek/lvts_thermal: avoid 32-bit overflow in lvts_temp_to_raw_mt7988() and INT_MAX trips

In reply to Ilia Snezhkov

lvts_temp_to_raw_mt7988() subtracts two 32-bit ints before casting to s64, shifts left by 14 bits, and immediately truncates the result to a u32 before passing it to div_s64().

When golden_temp_offset - temperature >= 262144 (for example, golden_temp = 60, temp_offset = 250460, temperature < 18316 mC, or even LVTS_MINIMUM_THRESHOLD = 20000 mC when golden_temp >= 64), the shifted value exceeds U32_MAX and silently wraps modulo 2^32, programming a bogus low raw value (high temperature) into LVTS_OFFSETL/LVTS_OFFSETH.

In addition, lvts_set_trips() passes high == INT_MAX (used by the thermal core when no upper trip exists) directly to lvts_temp_to_raw(), which produces a negative quotient cast to u32 while leaving the sensor's high-offset interrupt enabled.

Promote golden_temp_offset to s64 before subtracting, keep the shifted numerator in s64, clamp negative results to 0, avoid converting high == INT_MAX in lvts_set_trips(), and mask high_offset_inten when high_thresh == INT_MAX.

Found and drafted with an AI coding assistant.

Fixes f5f633b18234 ("thermal/drivers/mediatek: Add the Low Voltage Thermal Sensor driver")
Fixes 2bba1acf7a4c ("thermal/drivers/mediatek/lvts_thermal: Manage threshold between sensors")
---
 drivers/thermal/mediatek/lvts_thermal.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/thermal/mediatek/lvts_thermal.c +10 -7
index e034c6c..9b8b653 100644
@@ -322,11 +322,11 @@ static int lvts_raw_to_temp_mt7988(u32 raw_temp, int temp_factor)
 
 static u32 lvts_temp_to_raw_mt7988(int temperature, int temp_factor)
 {
-	u32 raw_temp = ((s64)(golden_temp_offset - temperature)) << 14;
+	s64 raw_temp = ((s64)golden_temp_offset - temperature) << 14;
 
 	raw_temp = div_s64(raw_temp, -temp_factor);
 
-	return raw_temp;
+	return raw_temp < 0 ? 0 : (u32)raw_temp;
 }
 
 static u32 lvts_temp_to_raw_mt8196(int temperature, int temp_factor)
@@ -409,12 +409,15 @@ static void lvts_update_irq_mask(struct lvts_ctrl *lvts_ctrl)
 			 * don't actually want to generate interrupts when
 			 * crossing it.
 			 */
-			if (lvts_ctrl->low_thresh == -INT_MAX) {
+			if (lvts_ctrl->low_thresh == -INT_MAX)
 				value &= ~low_offset_inten_masks[i];
+			else
+				value |= low_offset_inten_masks[i];
+
+			if (lvts_ctrl->high_thresh == INT_MAX)
+				value &= ~high_offset_inten_masks[i];
+			else
 				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]);
 		}
@@ -447,7 +450,7 @@ static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
 	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);
+	u32 raw_high = high != INT_MAX ? lvts_temp_to_raw(high, lvts_data) : 0;
 	bool should_update_thresh;
 
 	lvts_sensor->low_thresh = low;
Signature · 1 line
2.54.0 (Apple Git-157)
#3 lore raw

PATCH 3/4 thermal/drivers/mediatek/lvts_thermal: serialize lvts_set_trips() per controller

In reply to Ilia Snezhkov

Each struct lvts_ctrl manages up to four sensors, each registered as an independent thermal_zone_device with its own tz->lock. Concurrent lvts_set_trips() calls for sensors sharing a controller therefore run without mutual exclusion while updating lvts_ctrl->{low,high}_thresh, performing a read-modify-write on LVTS_MONINT in lvts_update_irq_mask(), and writing LVTS_OFFSETL/LVTS_OFFSETH.

Two concurrent callers can lose each other's LVTS_MONINT enable bits or leave the programmed threshold registers inconsistent with the stored controller thresholds.

Add a per-controller spinlock_t to struct lvts_ctrl and serialize lvts_set_trips() with guard(spinlock)(&lvts_ctrl->lock).

Found and drafted with an AI coding assistant.

Fixes 2bba1acf7a4c ("thermal/drivers/mediatek/lvts_thermal: Manage threshold between sensors")
---
 drivers/thermal/mediatek/lvts_thermal.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/thermal/mediatek/lvts_thermal.c +6 -0
index 9b8b653..f0b7caa 100644
@@ -4,6 +4,7 @@
  * Author: Balsam CHIHI <[email protected]>
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/clk-provider.h>
 #include <linux/delay.h>
@@ -16,6 +17,7 @@
 #include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/reset.h>
+#include <linux/spinlock.h>
 #include <linux/thermal.h>
 #include <dt-bindings/thermal/mediatek,lvts-thermal.h>
 
@@ -181,6 +183,7 @@ struct lvts_ctrl {
 	void __iomem *base;
 	int low_thresh;
 	int high_thresh;
+	spinlock_t lock; /* protects thresholds and LVTS_MONINT */
 };
 
 struct lvts_domain {
@@ -453,6 +456,8 @@ static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
 	u32 raw_high = high != INT_MAX ? lvts_temp_to_raw(high, lvts_data) : 0;
 	bool should_update_thresh;
 
+	guard(spinlock)(&lvts_ctrl->lock);
+
 	lvts_sensor->low_thresh = low;
 	lvts_sensor->high_thresh = high;
 
@@ -989,6 +994,7 @@ static int lvts_ctrl_init(struct device *dev, struct lvts_domain *lvts_td,
 
 		lvts_ctrl[i].low_thresh = INT_MIN;
 		lvts_ctrl[i].high_thresh = INT_MIN;
+		spin_lock_init(&lvts_ctrl[i].lock);
 	}
 
 	/*
Signature · 1 line
2.54.0 (Apple Git-157)
#4 lore raw

PATCH 4/4 thermal/drivers/mediatek/lvts_thermal: skip sensors without a thermal zone in IRQ

In reply to Ilia Snezhkov

lvts_ctrl_start() skips sensors absent from valid_sensor_mask as well as sensors whose thermal zone is not present in the device tree (-ENODEV), leaving lvts_ctrl->sensors[i].tz as NULL.

lvts_ctrl_irq_handler(), however, loops over all four sensor slots and passes lvts_ctrl->sensors[i].tz unconditionally to thermal_zone_device_update(), which dereferences tz immediately if a status bit for an unpopulated sensor slot is set in LVTS_MONINTSTS.

Check lvts_ctrl->sensors[i].tz before calling thermal_zone_device_update() while still acknowledging the status bit with W1C.

Found and drafted with an AI coding assistant.

Fixes f5f633b18234 ("thermal/drivers/mediatek: Add the Low Voltage Thermal Sensor driver")
---
 drivers/thermal/mediatek/lvts_thermal.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/thermal/mediatek/lvts_thermal.c +3 -2
index f0b7caa..5fc87a8 100644
@@ -588,8 +588,9 @@ static irqreturn_t lvts_ctrl_irq_handler(struct lvts_ctrl *lvts_ctrl)
 		if (!(value & masks[i]))
 			continue;
 
-		thermal_zone_device_update(lvts_ctrl->sensors[i].tz,
-					   THERMAL_TRIP_VIOLATED);
+		if (lvts_ctrl->sensors[i].tz)
+			thermal_zone_device_update(lvts_ctrl->sensors[i].tz,
+						   THERMAL_TRIP_VIOLATED);
 		iret = IRQ_HANDLED;
 	}
 
Signature · 1 line
2.54.0 (Apple Git-157)