diff mbox series

[1/1] target/riscv/kvm: fix timebase-frequency when using KVM acceleration

Message ID 20240313081359.20845-1-yongxuan.wang@sifive.com (mailing list archive)
State New, archived
Headers show
Series [1/1] target/riscv/kvm: fix timebase-frequency when using KVM acceleration | expand

Commit Message

Yong-Xuan Wang March 13, 2024, 8:13 a.m. UTC
The timebase-frequency of guest OS should be the same with host
machine. The timebase-frequency value in DTS should be got from
hypervisor when using KVM acceleration.

Signed-off-by: Yong-Xuan Wang <yongxuan.wang@sifive.com>
---
 hw/riscv/virt.c              | 11 +++++++++--
 target/riscv/kvm/kvm-cpu.c   |  9 +++++++++
 target/riscv/kvm/kvm_riscv.h | 13 +++++++++++++
 3 files changed, 31 insertions(+), 2 deletions(-)

Comments

Andrew Jones March 13, 2024, 8:56 a.m. UTC | #1
On Wed, Mar 13, 2024 at 04:13:57PM +0800, Yong-Xuan Wang wrote:
> The timebase-frequency of guest OS should be the same with host
> machine. The timebase-frequency value in DTS should be got from
> hypervisor when using KVM acceleration.
> 
> Signed-off-by: Yong-Xuan Wang <yongxuan.wang@sifive.com>
> ---
>  hw/riscv/virt.c              | 11 +++++++++--
>  target/riscv/kvm/kvm-cpu.c   |  9 +++++++++
>  target/riscv/kvm/kvm_riscv.h | 13 +++++++++++++
>  3 files changed, 31 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/riscv/virt.c b/hw/riscv/virt.c
> index a094af97c32a..a7ed7fa13010 100644
> --- a/hw/riscv/virt.c
> +++ b/hw/riscv/virt.c
> @@ -710,8 +710,15 @@ static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap,
>      int socket_count = riscv_socket_count(ms);
>  
>      qemu_fdt_add_subnode(ms->fdt, "/cpus");
> -    qemu_fdt_setprop_cell(ms->fdt, "/cpus", "timebase-frequency",
> -                          RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ);
> +
> +    if (kvm_enabled()) {
> +        qemu_fdt_setprop_cell(ms->fdt, "/cpus", "timebase-frequency",
> +                              kvm_riscv_get_timebase_frequency(first_cpu));
> +    } else {
> +        qemu_fdt_setprop_cell(ms->fdt, "/cpus", "timebase-frequency",
> +                              RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ);
> +    }
> +
>      qemu_fdt_setprop_cell(ms->fdt, "/cpus", "#size-cells", 0x0);
>      qemu_fdt_setprop_cell(ms->fdt, "/cpus", "#address-cells", 0x1);
>      qemu_fdt_add_subnode(ms->fdt, "/cpus/cpu-map");
> diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
> index c7afdb1e81b7..bbb115eaa867 100644
> --- a/target/riscv/kvm/kvm-cpu.c
> +++ b/target/riscv/kvm/kvm-cpu.c
> @@ -739,6 +739,15 @@ static void kvm_riscv_put_regs_timer(CPUState *cs)
>      env->kvm_timer_dirty = false;
>  }
>  
> +uint64_t kvm_riscv_get_timebase_frequency(CPUState *cs)
> +{
> +    uint64_t reg;
> +
> +    KVM_RISCV_GET_TIMER(cs, frequency, reg);
> +
> +    return reg;
> +}
> +
>  static int kvm_riscv_get_regs_vector(CPUState *cs)
>  {
>      RISCVCPU *cpu = RISCV_CPU(cs);
> diff --git a/target/riscv/kvm/kvm_riscv.h b/target/riscv/kvm/kvm_riscv.h
> index 4bd98fddc776..130a4bde0480 100644
> --- a/target/riscv/kvm/kvm_riscv.h
> +++ b/target/riscv/kvm/kvm_riscv.h
> @@ -29,4 +29,17 @@ void riscv_kvm_aplic_request(void *opaque, int irq, int level);
>  int kvm_riscv_sync_mpstate_to_kvm(RISCVCPU *cpu, int state);
>  void riscv_kvm_cpu_finalize_features(RISCVCPU *cpu, Error **errp);
>  
> +#ifdef CONFIG_KVM
> +
> +uint64_t kvm_riscv_get_timebase_frequency(CPUState *cs);
> +
> +#else
> +
> +static inline uint64_t kvm_riscv_get_timebase_frequency(CPUState *cs)
> +{
> +    g_assert_not_reached();
> +}
> +
> +#endif
> +

I think we've decided we don't need stubs like these since kvm_riscv_*
functions should always be called within 'if (kvm_enabled())' blocks (as
you have above), and then we leave it to the compile testing to point
out kvm function calls without kvm_enabled().

>  #endif
> -- 
> 2.17.1
>

Besides dropping the stubs,

Reviewed-by: Andrew Jones <ajones@ventanamicro.com>

Thanks,
drew
Philippe Mathieu-Daudé March 13, 2024, 10:24 a.m. UTC | #2
On 13/3/24 09:56, Andrew Jones wrote:
> On Wed, Mar 13, 2024 at 04:13:57PM +0800, Yong-Xuan Wang wrote:
>> The timebase-frequency of guest OS should be the same with host
>> machine. The timebase-frequency value in DTS should be got from
>> hypervisor when using KVM acceleration.
>>
>> Signed-off-by: Yong-Xuan Wang <yongxuan.wang@sifive.com>
>> ---
>>   hw/riscv/virt.c              | 11 +++++++++--
>>   target/riscv/kvm/kvm-cpu.c   |  9 +++++++++
>>   target/riscv/kvm/kvm_riscv.h | 13 +++++++++++++
>>   3 files changed, 31 insertions(+), 2 deletions(-)
>>
>> diff --git a/hw/riscv/virt.c b/hw/riscv/virt.c
>> index a094af97c32a..a7ed7fa13010 100644
>> --- a/hw/riscv/virt.c
>> +++ b/hw/riscv/virt.c
>> @@ -710,8 +710,15 @@ static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap,
>>       int socket_count = riscv_socket_count(ms);
>>   
>>       qemu_fdt_add_subnode(ms->fdt, "/cpus");
>> -    qemu_fdt_setprop_cell(ms->fdt, "/cpus", "timebase-frequency",

Alternatively:
                      kvm_enabled() ?
                      kvm_riscv_get_timebase_frequency(first_cpu) :


>> -                          RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ);
>> +
>> +    if (kvm_enabled()) {
>> +        qemu_fdt_setprop_cell(ms->fdt, "/cpus", "timebase-frequency",
>> +                              kvm_riscv_get_timebase_frequency(first_cpu));
>> +    } else {
>> +        qemu_fdt_setprop_cell(ms->fdt, "/cpus", "timebase-frequency",
>> +                              RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ);
>> +    }
>> +
>>       qemu_fdt_setprop_cell(ms->fdt, "/cpus", "#size-cells", 0x0);
>>       qemu_fdt_setprop_cell(ms->fdt, "/cpus", "#address-cells", 0x1);
>>       qemu_fdt_add_subnode(ms->fdt, "/cpus/cpu-map");
>> diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
>> index c7afdb1e81b7..bbb115eaa867 100644
>> --- a/target/riscv/kvm/kvm-cpu.c
>> +++ b/target/riscv/kvm/kvm-cpu.c
>> @@ -739,6 +739,15 @@ static void kvm_riscv_put_regs_timer(CPUState *cs)
>>       env->kvm_timer_dirty = false;
>>   }
>>   
>> +uint64_t kvm_riscv_get_timebase_frequency(CPUState *cs)
>> +{
>> +    uint64_t reg;
>> +
>> +    KVM_RISCV_GET_TIMER(cs, frequency, reg);
>> +
>> +    return reg;
>> +}
>> +
>>   static int kvm_riscv_get_regs_vector(CPUState *cs)
>>   {
>>       RISCVCPU *cpu = RISCV_CPU(cs);
>> diff --git a/target/riscv/kvm/kvm_riscv.h b/target/riscv/kvm/kvm_riscv.h
>> index 4bd98fddc776..130a4bde0480 100644
>> --- a/target/riscv/kvm/kvm_riscv.h
>> +++ b/target/riscv/kvm/kvm_riscv.h
>> @@ -29,4 +29,17 @@ void riscv_kvm_aplic_request(void *opaque, int irq, int level);
>>   int kvm_riscv_sync_mpstate_to_kvm(RISCVCPU *cpu, int state);
>>   void riscv_kvm_cpu_finalize_features(RISCVCPU *cpu, Error **errp);
>>   
>> +#ifdef CONFIG_KVM
>> +
>> +uint64_t kvm_riscv_get_timebase_frequency(CPUState *cs);
>> +
>> +#else
>> +
>> +static inline uint64_t kvm_riscv_get_timebase_frequency(CPUState *cs)
>> +{
>> +    g_assert_not_reached();
>> +}
>> +
>> +#endif
>> +
> 
> I think we've decided we don't need stubs like these since kvm_riscv_*
> functions should always be called within 'if (kvm_enabled())' blocks (as
> you have above), and then we leave it to the compile testing to point
> out kvm function calls without kvm_enabled().

Yes please!

> 
>>   #endif
>> -- 
>> 2.17.1
diff mbox series

Patch

diff --git a/hw/riscv/virt.c b/hw/riscv/virt.c
index a094af97c32a..a7ed7fa13010 100644
--- a/hw/riscv/virt.c
+++ b/hw/riscv/virt.c
@@ -710,8 +710,15 @@  static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap,
     int socket_count = riscv_socket_count(ms);
 
     qemu_fdt_add_subnode(ms->fdt, "/cpus");
-    qemu_fdt_setprop_cell(ms->fdt, "/cpus", "timebase-frequency",
-                          RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ);
+
+    if (kvm_enabled()) {
+        qemu_fdt_setprop_cell(ms->fdt, "/cpus", "timebase-frequency",
+                              kvm_riscv_get_timebase_frequency(first_cpu));
+    } else {
+        qemu_fdt_setprop_cell(ms->fdt, "/cpus", "timebase-frequency",
+                              RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ);
+    }
+
     qemu_fdt_setprop_cell(ms->fdt, "/cpus", "#size-cells", 0x0);
     qemu_fdt_setprop_cell(ms->fdt, "/cpus", "#address-cells", 0x1);
     qemu_fdt_add_subnode(ms->fdt, "/cpus/cpu-map");
diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
index c7afdb1e81b7..bbb115eaa867 100644
--- a/target/riscv/kvm/kvm-cpu.c
+++ b/target/riscv/kvm/kvm-cpu.c
@@ -739,6 +739,15 @@  static void kvm_riscv_put_regs_timer(CPUState *cs)
     env->kvm_timer_dirty = false;
 }
 
+uint64_t kvm_riscv_get_timebase_frequency(CPUState *cs)
+{
+    uint64_t reg;
+
+    KVM_RISCV_GET_TIMER(cs, frequency, reg);
+
+    return reg;
+}
+
 static int kvm_riscv_get_regs_vector(CPUState *cs)
 {
     RISCVCPU *cpu = RISCV_CPU(cs);
diff --git a/target/riscv/kvm/kvm_riscv.h b/target/riscv/kvm/kvm_riscv.h
index 4bd98fddc776..130a4bde0480 100644
--- a/target/riscv/kvm/kvm_riscv.h
+++ b/target/riscv/kvm/kvm_riscv.h
@@ -29,4 +29,17 @@  void riscv_kvm_aplic_request(void *opaque, int irq, int level);
 int kvm_riscv_sync_mpstate_to_kvm(RISCVCPU *cpu, int state);
 void riscv_kvm_cpu_finalize_features(RISCVCPU *cpu, Error **errp);
 
+#ifdef CONFIG_KVM
+
+uint64_t kvm_riscv_get_timebase_frequency(CPUState *cs);
+
+#else
+
+static inline uint64_t kvm_riscv_get_timebase_frequency(CPUState *cs)
+{
+    g_assert_not_reached();
+}
+
+#endif
+
 #endif