Add a separate guest timer and IRQ_VTIMER. To use cpu_has_ptw(), timer init 
should
be after loongarch_la464_initfn() when CPUCFG is set.

Signed-off-by: SignKirigami <[email protected]>
Signed-off-by: Hengyu Yu <[email protected]>
---
 target/loongarch/cpu.c | 16 ++++++++++++++--
 target/loongarch/cpu.h |  6 ++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c
index 20209c1bfc..3a351e43cc 100644
--- a/target/loongarch/cpu.c
+++ b/target/loongarch/cpu.c
@@ -738,10 +738,14 @@ static void loongarch_cpu_realizefn(DeviceState *dev, 
Error **errp)
     CPULoongArchState *env = cpu_env(cs);
 
 #ifdef CONFIG_TCG
-    CPUTimerState *timer;
+    CPUTimerState *timer, *guest_timer;
 #endif
 
-    env->irq_nr = N_IRQS;
+    if (cpu_has_lvz(env)) {
+        env->irq_nr = N_VIRQS;
+    } else {
+        env->irq_nr = N_IRQS;
+    }
     qdev_init_gpio_in(dev, loongarch_cpu_set_irq, env->irq_nr);
 #ifdef CONFIG_TCG
     timer = env_timer(env);
@@ -749,6 +753,14 @@ static void loongarch_cpu_realizefn(DeviceState *dev, 
Error **errp)
     timer->cs  = cs;
     timer_init_ns(&timer->timer, QEMU_CLOCK_VIRTUAL,
                   &cpu_loongarch_timer_cb, timer);
+
+    if (cpu_has_lvz(env)) {
+        guest_timer = env_guest_timer(env);
+        guest_timer->irq = IRQ_VTIMER;
+        guest_timer->cs = cs;
+        timer_init_ns(&guest_timer->timer, QEMU_CLOCK_VIRTUAL,
+            &cpu_loongarch_timer_cb, guest_timer);
+    }
 #endif
 
     cpu_common_realize(cs, &local_err);
diff --git a/target/loongarch/cpu.h b/target/loongarch/cpu.h
index 22f2b0aa47..c1a59882f7 100644
--- a/target/loongarch/cpu.h
+++ b/target/loongarch/cpu.h
@@ -245,6 +245,7 @@ extern const char * const fregnames[32];
 #define IRQ_IPI     12
 #define INT_DMSI    14
 #define N_VIRQS     (N_IRQS * 2)
+#define IRQ_VTIMER  (N_IRQS + IRQ_TIMER)
 
 #define MAX_PERF_EVENTS        16
 #define LOONGARCH_STLB         2048 /* 2048 STLB */
@@ -582,6 +583,11 @@ static inline CPUTimerState *env_timer(CPULoongArchState 
*env)
     return &env->sys_states[0].timer_state;
 }
 
+static inline CPUTimerState *env_guest_timer(CPULoongArchState *env)
+{
+    return &env->sys_states[1].timer_state;
+}
+
 static inline bool is_la64(CPULoongArchState *env)
 {
     return FIELD_EX32(env->cpucfg[1], CPUCFG1, ARCH) == CPUCFG1_ARCH_LA64;
-- 
2.56.0


Reply via email to