From 357ec64b36cd84ca87660407e67eb0cb1cfe932b Mon Sep 17 00:00:00 2001 From: Berend Ozceri Date: Wed, 15 Sep 2021 14:12:42 -0700 Subject: [PATCH] drivers: timer: Fix RISC-V machine timer count drift due integer math If CYC_PER_TICK does not divide the (now - last_count) quantity exactly with integer math, the subsequent multiplication before incrementing last_count causes a drift. This commit eliminates the redundant division-followed-by-multiplication and fixes https://github.com/zephyrproject-rtos/zephyr/issues/37852 Signed-off-by: Berend Ozceri --- drivers/timer/riscv_machine_timer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/timer/riscv_machine_timer.c b/drivers/timer/riscv_machine_timer.c index 1cc25868259..0211dcc1e57 100644 --- a/drivers/timer/riscv_machine_timer.c +++ b/drivers/timer/riscv_machine_timer.c @@ -64,7 +64,7 @@ static void timer_isr(const void *arg) uint64_t now = mtime(); uint32_t dticks = (uint32_t)((now - last_count) / CYC_PER_TICK); - last_count += dticks * CYC_PER_TICK; + last_count = now; if (!TICKLESS) { uint64_t next = last_count + CYC_PER_TICK;