[PATCH v2 2/2] hw/arm/imx8mp-evk: Fix guest time in KVM mode

Bernhard Beschow posted 2 patches 2 weeks, 2 days ago
Maintainers: Bernhard Beschow <shentey@gmail.com>, Paolo Bonzini <pbonzini@redhat.com>, Peter Maydell <peter.maydell@linaro.org>
There is a newer version of this series
[PATCH v2 2/2] hw/arm/imx8mp-evk: Fix guest time in KVM mode
Posted by Bernhard Beschow 2 weeks, 2 days ago
The imx8mp DTB hardcodes the clock frequency of the system counter to 8MHz.
In KVM mode, the host CPU is used whose system counter runs at a different
frequency, resulting in the guest clock running slower or faster. Fix this
by not hardcoding the clock frequency which makes the Linux driver read
the real clock frequency from the register.

Signed-off-by: Bernhard Beschow <shentey@gmail.com>
---
 hw/arm/imx8mp-evk.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/hw/arm/imx8mp-evk.c b/hw/arm/imx8mp-evk.c
index 80036f8589..e26c62aee1 100644
--- a/hw/arm/imx8mp-evk.c
+++ b/hw/arm/imx8mp-evk.c
@@ -43,6 +43,15 @@ static void imx8mp_evk_modify_dtb(const struct arm_boot_info *info, void *fdt)
         fdt_nop_property(fdt, offset, "cpu-idle-states");
         offset = fdt_node_offset_by_compatible(fdt, offset, "arm,cortex-a53");
     }
+
+    if (kvm_enabled()) {
+        /* Use system counter frequency from host CPU to fix time in guest */
+        offset = fdt_node_offset_by_compatible(fdt, -1, "arm,armv8-timer");
+        while (offset >= 0) {
+            fdt_nop_property(fdt, offset, "clock-frequency");
+            offset = fdt_node_offset_by_compatible(fdt, offset, "arm,armv8-timer");
+        }
+    }
 }
 
 static void imx8mp_evk_init(MachineState *machine)
-- 
2.51.2
Re: [PATCH v2 2/2] hw/arm/imx8mp-evk: Fix guest time in KVM mode
Posted by Philippe Mathieu-Daudé 2 weeks ago
On 29/10/25 15:23, Bernhard Beschow wrote:
> The imx8mp DTB hardcodes the clock frequency of the system counter to 8MHz.
> In KVM mode, the host CPU is used whose system counter runs at a different
> frequency, resulting in the guest clock running slower or faster. Fix this
> by not hardcoding the clock frequency which makes the Linux driver read
> the real clock frequency from the register.
> 
> Signed-off-by: Bernhard Beschow <shentey@gmail.com>
> ---
>   hw/arm/imx8mp-evk.c | 9 +++++++++
>   1 file changed, 9 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>