Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

clocksource/drivers/timer-tegra186: Simplify calculating timeleft

It is not necessary to use 64-bit operations to calculate the
remaining watchdog timeout. Simplify to use 32-bit operations,
and add comments explaining why there will be no overflow.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Reviewed-by: Jon Hunter <jonathanh@nvidia.com>
Cc: Pohsun Su <pohsuns@nvidia.com>
Cc: Robert Lin <robelin@nvidia.com>
Link: https://lore.kernel.org/r/20250614175556.922159-2-linux@roeck-us.net

authored by

Guenter Roeck and committed by
Daniel Lezcano
7f3abae5 916aa360

+15 -10
+15 -10
drivers/clocksource/timer-tegra186.c
··· 231 231 { 232 232 struct tegra186_wdt *wdt = to_tegra186_wdt(wdd); 233 233 u32 expiration, val; 234 - u64 timeleft; 234 + u32 timeleft; 235 235 236 236 if (!watchdog_active(&wdt->base)) { 237 237 /* return zero if the watchdog timer is not activated. */ ··· 266 266 * Calculate the time remaining by adding the time for the 267 267 * counter value to the time of the counter expirations that 268 268 * remain. 269 + * Note: Since wdt->base.timeout is bound to 255, the maximum 270 + * value added to timeleft is 271 + * 255 * (1,000,000 / 5) * 4 272 + * = 255 * 200,000 * 4 273 + * = 204,000,000 274 + * TMRSR_PCV is a 29-bit field. 275 + * Its maximum value is 0x1fffffff = 536,870,911. 276 + * 204,000,000 + 536,870,911 = 740,870,911 = 0x2C28CAFF. 277 + * timeleft can therefore not overflow, and 64-bit calculations 278 + * are not necessary. 269 279 */ 270 - timeleft += ((u64)wdt->base.timeout * (USEC_PER_SEC / 5)) * (4 - expiration); 280 + timeleft += (wdt->base.timeout * (USEC_PER_SEC / 5)) * (4 - expiration); 271 281 272 282 /* 273 283 * Convert the current counter value to seconds, 274 - * rounding up to the nearest second. Cast u64 to 275 - * u32 under the assumption that no overflow happens 276 - * when coverting to seconds. 284 + * rounding to the nearest second. 277 285 */ 278 - timeleft = DIV_ROUND_CLOSEST_ULL(timeleft, USEC_PER_SEC); 286 + timeleft = DIV_ROUND_CLOSEST(timeleft, USEC_PER_SEC); 279 287 280 - if (WARN_ON_ONCE(timeleft > U32_MAX)) 281 - return U32_MAX; 282 - 283 - return lower_32_bits(timeleft); 288 + return timeleft; 284 289 } 285 290 286 291 static const struct watchdog_ops tegra186_wdt_ops = {