hw/openrisc: Fixed undercounting of TTCR in continuous mode
authorJoel Holdsworth <jholdsworth@nvidia.com>
Fri, 7 Jun 2024 22:29:33 +0000 (15:29 -0700)
committerStafford Horne <shorne@gmail.com>
Tue, 3 Dec 2024 10:59:25 +0000 (10:59 +0000)
commit3eb43aeb164f1f83c97ff693c7d464b49755110c
tree610ee4e06705e4716197555d68b254fffe809ed5
parent26dcf2be7e153defa289d20317707af034aca692
hw/openrisc: Fixed undercounting of TTCR in continuous mode

In the existing design, TTCR is prone to undercounting when running in
continuous mode. This manifests as a timer interrupt appearing to
trigger a few cycles prior to the deadline set in SPR_TTMR_TP.

When the timer triggers, the virtual time delta in nanoseconds between
the time when the timer was set, and when it triggers is calculated.
This nanoseconds value is then divided by TIMER_PERIOD (50) to compute
an increment of cycles to apply to TTCR.

However, this calculation rounds down the number of cycles causing the
undercounting.

A simplistic solution would be to instead round up the number of cycles,
however this will result in the accumulation of timing error over time.

This patch corrects the issue by calculating the time delta in
nanoseconds between when the timer was last reset and the timer event.
This approach allows the TTCR value to be rounded up, but without
accumulating error over time.

Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
[stafford: Incremented version in vmstate_or1k_timer, checkpatch fixes]
Signed-off-by: Stafford Horne <shorne@gmail.com>
hw/openrisc/cputimer.c