Skip to contentSkip to conversation

Fix the rtc issue on loongarch

PATCH In review Xianglai Li 2 participants 9 messages Jun 13, 2025 – Jul 7, 2025

lore
Share & tools
lore
#0 lore raw

cover Fix the rtc issue on loongarch

The first patch fixed the issue with rtc hardware
emulation on the loongarch platform.

The second patch adds an acpi table to the rtc hardware simulation of the loongarch platform, enabling the virtual machine kernel to use the loongson rtc driver through the acpi table.

Cc Bibo Mao <[email protected]>
Cc Song Gao <[email protected]>
Cc Jiaxun Yang <[email protected]>
Cc Xianglai Li <[email protected]>

Xianglai Li (2):
hw/rtc: Fixed loongson rtc emulation errors
hw/loongarch: add rtc acpi table

 hw/loongarch/virt-acpi-build.c | 24 ++++++++++++++++++++++++
 hw/loongarch/virt-fdt-build.c  | 11 +++++++++--
 hw/rtc/ls7a_rtc.c              | 26 +++++++++++++++++---------
 3 files changed, 50 insertions(+), 11 deletions(-)
Signature · 1 line
2.39.1
#1 lore raw

PATCH 1/2 hw/rtc: Fixed loongson rtc emulation errors

The expire time is sent to the timer only when the expire Time is greater than 0 or greater than now. Otherwise, the timer will trigger interruption continuously.

Timer interrupts are sent using pulse functions.

---
Cc: Bibo Mao <[email protected]>
Cc: Song Gao <[email protected]>
Cc: Jiaxun Yang <[email protected]>
Cc: Xianglai Li <[email protected]>

 hw/loongarch/virt-fdt-build.c | 11 +++++++++--
 hw/rtc/ls7a_rtc.c             | 26 +++++++++++++++++---------
 2 files changed, 26 insertions(+), 11 deletions(-)
hw/loongarch/virt-fdt-build.c +9 -2
index 728ce46699..c613131a07 100644
@@ -17,6 +17,11 @@
 #include "system/reset.h"
 #include "target/loongarch/cpu.h"
 
+#define FDT_IRQ_FLAGS_EDGE_LO_HI 1
+#define FDT_IRQ_FLAGS_EDGE_HI_LO 2
+#define FDT_IRQ_FLAGS_LEVEL_HI 4
+#define FDT_IRQ_FLAGS_LEVEL_LO 8
+
 static void create_fdt(LoongArchVirtMachineState *lvms)
 {
     MachineState *ms = MACHINE(lvms);
@@ -416,7 +421,8 @@ static void fdt_add_uart_node(LoongArchVirtMachineState *lvms,
     if (chosen) {
         qemu_fdt_setprop_string(ms->fdt, "/chosen", "stdout-path", nodename);
     }
-    qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", irq, 0x4);
+    qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", irq,
+                           FDT_IRQ_FLAGS_LEVEL_HI);
     qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                           *pch_pic_phandle);
     g_free(nodename);
@@ -436,7 +442,8 @@ static void fdt_add_rtc_node(LoongArchVirtMachineState *lvms,
                             "loongson,ls7a-rtc");
     qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 2, base, 2, size);
     qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts",
-                           VIRT_RTC_IRQ - VIRT_GSI_BASE , 0x4);
+                           VIRT_RTC_IRQ - VIRT_GSI_BASE ,
+                           FDT_IRQ_FLAGS_EDGE_LO_HI);
     qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                           *pch_pic_phandle);
     g_free(nodename);
hw/rtc/ls7a_rtc.c +17 -9
index 10097b2db7..7eca75a42a 100644
@@ -145,20 +145,24 @@ static void toymatch_write(LS7ARtcState *s, uint64_t val, int num)
         now = qemu_clock_get_ms(rtc_clock);
         toymatch_val_to_time(s, val, &tm);
         expire_time = now + (qemu_timedate_diff(&tm) - s->offset_toy) * 1000;
-        timer_mod(s->toy_timer[num], expire_time);
+        if (expire_time > now) {
+            timer_mod(s->toy_timer[num], expire_time);
+        }
     }
 }
 
 static void rtcmatch_write(LS7ARtcState *s, uint64_t val, int num)
 {
-    uint64_t expire_ns;
+    int64_t expire_ns;
 
     /* it do not support write when toy disabled */
     if (rtc_enabled(s)) {
         s->rtcmatch[num] = val;
         /* calculate expire time */
         expire_ns = ticks_to_ns(val) - ticks_to_ns(s->offset_rtc);
-        timer_mod_ns(s->rtc_timer[num], expire_ns);
+        if (expire_ns > 0) {
+            timer_mod_ns(s->rtc_timer[num], expire_ns);
+        }
     }
 }
 
@@ -185,7 +189,7 @@ static void ls7a_rtc_stop(LS7ARtcState *s)
 static void ls7a_toy_start(LS7ARtcState *s)
 {
     int i;
-    uint64_t expire_time, now;
+    int64_t expire_time, now;
     struct tm tm = {};
 
     now = qemu_clock_get_ms(rtc_clock);
@@ -194,19 +198,23 @@ static void ls7a_toy_start(LS7ARtcState *s)
     for (i = 0; i < TIMER_NUMS; i++) {
         toymatch_val_to_time(s, s->toymatch[i], &tm);
         expire_time = now + (qemu_timedate_diff(&tm) - s->offset_toy) * 1000;
-        timer_mod(s->toy_timer[i], expire_time);
+        if (expire_time > now) {
+            timer_mod(s->toy_timer[i], expire_time);
+        }
     }
 }
 
 static void ls7a_rtc_start(LS7ARtcState *s)
 {
     int i;
-    uint64_t expire_time;
+    int64_t expire_time;
 
     /* recalculate expire time and enable timer */
     for (i = 0; i < TIMER_NUMS; i++) {
         expire_time = ticks_to_ns(s->rtcmatch[i]) - ticks_to_ns(s->offset_rtc);
-        timer_mod_ns(s->rtc_timer[i], expire_time);
+        if (expire_time > 0) {
+            timer_mod_ns(s->rtc_timer[i], expire_time);
+        }
     }
 }
 
@@ -370,7 +378,7 @@ static void toy_timer_cb(void *opaque)
     LS7ARtcState *s = opaque;
 
     if (toy_enabled(s)) {
-        qemu_irq_raise(s->irq);
+        qemu_irq_pulse(s->irq);
     }
 }
 
@@ -379,7 +387,7 @@ static void rtc_timer_cb(void *opaque)
     LS7ARtcState *s = opaque;
 
     if (rtc_enabled(s)) {
-        qemu_irq_raise(s->irq);
+        qemu_irq_pulse(s->irq);
     }
 }
 
Signature · 1 line
2.39.1
#2 lore raw

Re: [PATCH V1 1/2] hw/rtc: Fixed loongson rtc emulation errors

Quoted 4 lines · Xianglai Li

The expire time is sent to the timer only when the expire Time is greater than 0 or greater than now. Otherwise, the timer will trigger interruption continuously.

Again, one line of the comments should no more than 75 bytes :)

I think it should be split into two patches, one is fixup with irq pulse type setting, the other is fixup with expired time out calculation in rtc emulation driver.

Regards
Bibo Mao

Quoted 138 lines

Timer interrupts are sent using pulse functions.

---
Cc: Bibo Mao <[email protected]>
Cc: Song Gao <[email protected]>
Cc: Jiaxun Yang <[email protected]>
Cc: Xianglai Li <[email protected]>

  hw/loongarch/virt-fdt-build.c | 11 +++++++++--
  hw/rtc/ls7a_rtc.c             | 26 +++++++++++++++++---------
  2 files changed, 26 insertions(+), 11 deletions(-)
hw/loongarch/virt-fdt-build.c +9 -2
index 728ce46699..c613131a07 100644
@@ -17,6 +17,11 @@
  #include "system/reset.h"
  #include "target/loongarch/cpu.h"
 
+#define FDT_IRQ_FLAGS_EDGE_LO_HI 1
+#define FDT_IRQ_FLAGS_EDGE_HI_LO 2
+#define FDT_IRQ_FLAGS_LEVEL_HI 4
+#define FDT_IRQ_FLAGS_LEVEL_LO 8
+
  static void create_fdt(LoongArchVirtMachineState *lvms)
  {
      MachineState *ms = MACHINE(lvms);
@@ -416,7 +421,8 @@ static void fdt_add_uart_node(LoongArchVirtMachineState *lvms,
      if (chosen) {
          qemu_fdt_setprop_string(ms->fdt, "/chosen", "stdout-path", nodename);
      }
-    qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", irq, 0x4);
+    qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", irq,
+                           FDT_IRQ_FLAGS_LEVEL_HI);
      qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                            *pch_pic_phandle);
      g_free(nodename);
@@ -436,7 +442,8 @@ static void fdt_add_rtc_node(LoongArchVirtMachineState *lvms,
                              "loongson,ls7a-rtc");
      qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 2, base, 2, size);
      qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts",
-                           VIRT_RTC_IRQ - VIRT_GSI_BASE , 0x4);
+                           VIRT_RTC_IRQ - VIRT_GSI_BASE ,
+                           FDT_IRQ_FLAGS_EDGE_LO_HI);
      qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                            *pch_pic_phandle);
      g_free(nodename);
hw/rtc/ls7a_rtc.c +17 -9
index 10097b2db7..7eca75a42a 100644
@@ -145,20 +145,24 @@ static void toymatch_write(LS7ARtcState *s, uint64_t val, int num)
          now = qemu_clock_get_ms(rtc_clock);
          toymatch_val_to_time(s, val, &tm);
          expire_time = now + (qemu_timedate_diff(&tm) - s->offset_toy) * 1000;
-        timer_mod(s->toy_timer[num], expire_time);
+        if (expire_time > now) {
+            timer_mod(s->toy_timer[num], expire_time);
+        }
      }
  }
 
  static void rtcmatch_write(LS7ARtcState *s, uint64_t val, int num)
  {
-    uint64_t expire_ns;
+    int64_t expire_ns;
 
      /* it do not support write when toy disabled */
      if (rtc_enabled(s)) {
          s->rtcmatch[num] = val;
          /* calculate expire time */
          expire_ns = ticks_to_ns(val) - ticks_to_ns(s->offset_rtc);
-        timer_mod_ns(s->rtc_timer[num], expire_ns);
+        if (expire_ns > 0) {
+            timer_mod_ns(s->rtc_timer[num], expire_ns);
+        }
      }
  }
 
@@ -185,7 +189,7 @@ static void ls7a_rtc_stop(LS7ARtcState *s)
  static void ls7a_toy_start(LS7ARtcState *s)
  {
      int i;
-    uint64_t expire_time, now;
+    int64_t expire_time, now;
      struct tm tm = {};
 
      now = qemu_clock_get_ms(rtc_clock);
@@ -194,19 +198,23 @@ static void ls7a_toy_start(LS7ARtcState *s)
      for (i = 0; i < TIMER_NUMS; i++) {
          toymatch_val_to_time(s, s->toymatch[i], &tm);
          expire_time = now + (qemu_timedate_diff(&tm) - s->offset_toy) * 1000;
-        timer_mod(s->toy_timer[i], expire_time);
+        if (expire_time > now) {
+            timer_mod(s->toy_timer[i], expire_time);
+        }
      }
  }
 
  static void ls7a_rtc_start(LS7ARtcState *s)
  {
      int i;
-    uint64_t expire_time;
+    int64_t expire_time;
 
      /* recalculate expire time and enable timer */
      for (i = 0; i < TIMER_NUMS; i++) {
          expire_time = ticks_to_ns(s->rtcmatch[i]) - ticks_to_ns(s->offset_rtc);
-        timer_mod_ns(s->rtc_timer[i], expire_time);
+        if (expire_time > 0) {
+            timer_mod_ns(s->rtc_timer[i], expire_time);
+        }
      }
  }
 
@@ -370,7 +378,7 @@ static void toy_timer_cb(void *opaque)
      LS7ARtcState *s = opaque;
 
      if (toy_enabled(s)) {
-        qemu_irq_raise(s->irq);
+        qemu_irq_pulse(s->irq);
      }
  }
 
@@ -379,7 +387,7 @@ static void rtc_timer_cb(void *opaque)
      LS7ARtcState *s = opaque;
 
      if (rtc_enabled(s)) {
-        qemu_irq_raise(s->irq);
+        qemu_irq_pulse(s->irq);
      }
  }
 
#3 lore raw

Re: [PATCH V1 1/2] hw/rtc: Fixed loongson rtc emulation errors

In reply to Xianglai Li
Quoted 59 lines · Xianglai Li

The expire time is sent to the timer only when the expire Time is greater than 0 or greater than now. Otherwise, the timer will trigger interruption continuously.

Timer interrupts are sent using pulse functions.

---
Cc: Bibo Mao <[email protected]>
Cc: Song Gao <[email protected]>
Cc: Jiaxun Yang <[email protected]>
Cc: Xianglai Li <[email protected]>

  hw/loongarch/virt-fdt-build.c | 11 +++++++++--
  hw/rtc/ls7a_rtc.c             | 26 +++++++++++++++++---------
  2 files changed, 26 insertions(+), 11 deletions(-)
hw/loongarch/virt-fdt-build.c +9 -2
index 728ce46699..c613131a07 100644
@@ -17,6 +17,11 @@
  #include "system/reset.h"
  #include "target/loongarch/cpu.h"
 
+#define FDT_IRQ_FLAGS_EDGE_LO_HI 1
+#define FDT_IRQ_FLAGS_EDGE_HI_LO 2
+#define FDT_IRQ_FLAGS_LEVEL_HI 4
+#define FDT_IRQ_FLAGS_LEVEL_LO 8
+
  static void create_fdt(LoongArchVirtMachineState *lvms)
  {
      MachineState *ms = MACHINE(lvms);
@@ -416,7 +421,8 @@ static void fdt_add_uart_node(LoongArchVirtMachineState *lvms,
      if (chosen) {
          qemu_fdt_setprop_string(ms->fdt, "/chosen", "stdout-path", nodename);
      }
-    qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", irq, 0x4);
+    qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", irq,
+                           FDT_IRQ_FLAGS_LEVEL_HI);
      qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                            *pch_pic_phandle);
      g_free(nodename);
@@ -436,7 +442,8 @@ static void fdt_add_rtc_node(LoongArchVirtMachineState *lvms,
                              "loongson,ls7a-rtc");
      qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 2, base, 2, size);
      qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts",
-                           VIRT_RTC_IRQ - VIRT_GSI_BASE , 0x4);
+                           VIRT_RTC_IRQ - VIRT_GSI_BASE ,
+                           FDT_IRQ_FLAGS_EDGE_LO_HI);
      qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                            *pch_pic_phandle);
      g_free(nodename);
hw/rtc/ls7a_rtc.c +0 -0
index 10097b2db7..7eca75a42a 100644

hw/rtc/ls7a_rtc.c static void toymatch_write(LS7ARtcState *s, uint64_t val, int num) · line 150

Quoted 7 lines
hw/rtc/ls7a_rtc.c +3 -1
@@ -145,20 +145,24 @@ static void toymatch_write(LS7ARtcState *s, uint64_t val, int num)
          now = qemu_clock_get_ms(rtc_clock);
          toymatch_val_to_time(s, val, &tm);
          expire_time = now + (qemu_timedate_diff(&tm) - s->offset_toy) * 1000;
-        timer_mod(s->toy_timer[num], expire_time);
+        if (expire_time > now) {
+            timer_mod(s->toy_timer[num], expire_time);
+        }

Should rtc alarm interrupt be injected directly if (expire_time <= now)?

Regards
Bibo Mao

Quoted 75 lines
hw/rtc/ls7a_rtc.c +14 -8
      }
  }
 
  static void rtcmatch_write(LS7ARtcState *s, uint64_t val, int num)
  {
-    uint64_t expire_ns;
+    int64_t expire_ns;
 
      /* it do not support write when toy disabled */
      if (rtc_enabled(s)) {
          s->rtcmatch[num] = val;
          /* calculate expire time */
          expire_ns = ticks_to_ns(val) - ticks_to_ns(s->offset_rtc);
-        timer_mod_ns(s->rtc_timer[num], expire_ns);
+        if (expire_ns > 0) {
+            timer_mod_ns(s->rtc_timer[num], expire_ns);
+        }
      }
  }
 
@@ -185,7 +189,7 @@ static void ls7a_rtc_stop(LS7ARtcState *s)
  static void ls7a_toy_start(LS7ARtcState *s)
  {
      int i;
-    uint64_t expire_time, now;
+    int64_t expire_time, now;
      struct tm tm = {};
 
      now = qemu_clock_get_ms(rtc_clock);
@@ -194,19 +198,23 @@ static void ls7a_toy_start(LS7ARtcState *s)
      for (i = 0; i < TIMER_NUMS; i++) {
          toymatch_val_to_time(s, s->toymatch[i], &tm);
          expire_time = now + (qemu_timedate_diff(&tm) - s->offset_toy) * 1000;
-        timer_mod(s->toy_timer[i], expire_time);
+        if (expire_time > now) {
+            timer_mod(s->toy_timer[i], expire_time);
+        }
      }
  }
 
  static void ls7a_rtc_start(LS7ARtcState *s)
  {
      int i;
-    uint64_t expire_time;
+    int64_t expire_time;
 
      /* recalculate expire time and enable timer */
      for (i = 0; i < TIMER_NUMS; i++) {
          expire_time = ticks_to_ns(s->rtcmatch[i]) - ticks_to_ns(s->offset_rtc);
-        timer_mod_ns(s->rtc_timer[i], expire_time);
+        if (expire_time > 0) {
+            timer_mod_ns(s->rtc_timer[i], expire_time);
+        }
      }
  }
 
@@ -370,7 +378,7 @@ static void toy_timer_cb(void *opaque)
      LS7ARtcState *s = opaque;
 
      if (toy_enabled(s)) {
-        qemu_irq_raise(s->irq);
+        qemu_irq_pulse(s->irq);
      }
  }
 
@@ -379,7 +387,7 @@ static void rtc_timer_cb(void *opaque)
      LS7ARtcState *s = opaque;
 
      if (rtc_enabled(s)) {
-        qemu_irq_raise(s->irq);
+        qemu_irq_pulse(s->irq);
      }
  }
 
author
#4 lore raw

Re: [PATCH V1 1/2] hw/rtc: Fixed loongson rtc emulation errors

Hi Bibo Mao:

Quoted 64 lines · Xianglai Li
Quoted 61 lines

The expire time is sent to the timer only when the expire Time is greater than 0 or greater than now. Otherwise, the timer will trigger interruption continuously.

Timer interrupts are sent using pulse functions.

---
Cc: Bibo Mao <[email protected]>
Cc: Song Gao <[email protected]>
Cc: Jiaxun Yang <[email protected]>
Cc: Xianglai Li <[email protected]>

  hw/loongarch/virt-fdt-build.c | 11 +++++++++-- hw/rtc/ls7a_rtc.c             | 26 +++++++++++++++++---------

  2 files changed, 26 insertions(+), 11 deletions(-)
hw/loongarch/virt-fdt-build.c +0 -0
index 728ce46699..c613131a07 100644
@@ -17,6 +17,11 @@
   #include "system/reset.h"
   #include "target/loongarch/cpu.h"

  +#define FDT_IRQ_FLAGS_EDGE_LO_HI 1

hw/loongarch/virt-fdt-build.c +4 -0
+#define FDT_IRQ_FLAGS_EDGE_HI_LO 2
+#define FDT_IRQ_FLAGS_LEVEL_HI 4
+#define FDT_IRQ_FLAGS_LEVEL_LO 8
+

  static void create_fdt(LoongArchVirtMachineState *lvms) {
      MachineState *ms = MACHINE(lvms);

hw/loongarch/virt-fdt-build.c +2 -1
@@ -416,7 +421,8 @@ static void 
 fdt_add_uart_node(LoongArchVirtMachineState *lvms,
       if (chosen) {
           qemu_fdt_setprop_string(ms->fdt, "/chosen", "stdout-path", nodename);
       }
-    qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", irq, 0x4);
+    qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", irq,
+                           FDT_IRQ_FLAGS_LEVEL_HI);
       qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                             *pch_pic_phandle);

      g_free(nodename);

hw/loongarch/virt-fdt-build.c +2 -1
@@ -436,7 +442,8 @@ static void 
 fdt_add_rtc_node(LoongArchVirtMachineState *lvms,
                               "loongson,ls7a-rtc");
       qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 2, base, 2, size);
       qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts",
-                           VIRT_RTC_IRQ - VIRT_GSI_BASE , 0x4);
+                           VIRT_RTC_IRQ - VIRT_GSI_BASE ,
+                           FDT_IRQ_FLAGS_EDGE_LO_HI);
       qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                             *pch_pic_phandle);

      g_free(nodename);

hw/rtc/ls7a_rtc.c +0 -0
index 10097b2db7..7eca75a42a 100644
@@ -145,20 +145,24 @@ static void toymatch_write(LS7ARtcState *s, 
 uint64_t val, int num)
Quoted 9 lines

hw/rtc/ls7a_rtc.c static void toymatch_write(LS7ARtcState *s, uint64_t val, int num) · line 150

Quoted 7 lines
hw/rtc/ls7a_rtc.c +3 -1
           now = qemu_clock_get_ms(rtc_clock);
           toymatch_val_to_time(s, val, &tm);
           expire_time = now + (qemu_timedate_diff(&tm) - s->offset_toy) * 1000;
-        timer_mod(s->toy_timer[num], expire_time);
+        if (expire_time > now) {
+            timer_mod(s->toy_timer[num], expire_time);
+        }

Should rtc alarm interrupt be injected directly if (expire_time <= now)?

Through experiments on the hardware platform, sending an alarm less than the current time to loongson rtc alarm does not trigger an interrupt.

According to the behavior of the hardware, we do not need to trigger an interrupt immediately when expire_time <= now

Thanks,
Xianglai Li.

Quoted 66 lines

Regards
Bibo Mao

Quoted 64 lines
hw/rtc/ls7a_rtc.c +4 -2
       }
   }
     static void rtcmatch_write(LS7ARtcState *s, uint64_t val, int num)
   {
-    uint64_t expire_ns;
+    int64_t expire_ns;
         /* it do not support write when toy disabled */
       if (rtc_enabled(s)) {
           s->rtcmatch[num] = val;
           /* calculate expire time */
           expire_ns = ticks_to_ns(val) - ticks_to_ns(s->offset_rtc);
-        timer_mod_ns(s->rtc_timer[num], expire_ns);
+        if (expire_ns > 0) {
+            timer_mod_ns(s->rtc_timer[num], expire_ns);
+        }
       }
   }
   @@ -185,7 +189,7 @@ static void ls7a_rtc_stop(LS7ARtcState *s)
   static void ls7a_toy_start(LS7ARtcState *s)

  {
      int i;
-    uint64_t expire_time, now;
+    int64_t expire_time, now; struct tm tm = {};
        now = qemu_clock_get_ms(rtc_clock);

hw/rtc/ls7a_rtc.c +8 -4
@@ -194,19 +198,23 @@ static void ls7a_toy_start(LS7ARtcState *s)
       for (i = 0; i < TIMER_NUMS; i++) {
           toymatch_val_to_time(s, s->toymatch[i], &tm);
           expire_time = now + (qemu_timedate_diff(&tm) - s->offset_toy) * 1000;
-        timer_mod(s->toy_timer[i], expire_time);
+        if (expire_time > now) {
+            timer_mod(s->toy_timer[i], expire_time);
+        }
       }
   }
     static void ls7a_rtc_start(LS7ARtcState *s)
   {
       int i;
-    uint64_t expire_time;
+    int64_t expire_time;
         /* recalculate expire time and enable timer */
       for (i = 0; i < TIMER_NUMS; i++) {
           expire_time = ticks_to_ns(s->rtcmatch[i]) - ticks_to_ns(s->offset_rtc);
-        timer_mod_ns(s->rtc_timer[i], expire_time);
+        if (expire_time > 0) {
+            timer_mod_ns(s->rtc_timer[i], expire_time);
+        }
       }
   }
   @@ -370,7 +378,7 @@ static void toy_timer_cb(void *opaque)
       LS7ARtcState *s = opaque;
         if (toy_enabled(s)) {
-        qemu_irq_raise(s->irq);
+        qemu_irq_pulse(s->irq);

      }
  }
  @@ -379,7 +387,7 @@ static void rtc_timer_cb(void *opaque) LS7ARtcState *s = opaque;
        if (rtc_enabled(s)) {
-        qemu_irq_raise(s->irq);
+        qemu_irq_pulse(s->irq);
      }
  }

#5 lore raw

Re: [PATCH V1 1/2] hw/rtc: Fixed loongson rtc emulation errors

Quoted 74 lines · lixianglai

Hi Bibo Mao:

Quoted 64 lines · Xianglai Li
Quoted 61 lines

The expire time is sent to the timer only when the expire Time is greater than 0 or greater than now. Otherwise, the timer will trigger interruption continuously.

Timer interrupts are sent using pulse functions.

---
Cc: Bibo Mao <[email protected]>
Cc: Song Gao <[email protected]>
Cc: Jiaxun Yang <[email protected]>
Cc: Xianglai Li <[email protected]>

  hw/loongarch/virt-fdt-build.c | 11 +++++++++-- hw/rtc/ls7a_rtc.c             | 26 +++++++++++++++++---------

  2 files changed, 26 insertions(+), 11 deletions(-)
hw/loongarch/virt-fdt-build.c +0 -0
index 728ce46699..c613131a07 100644
@@ -17,6 +17,11 @@
   #include "system/reset.h"
   #include "target/loongarch/cpu.h"

  +#define FDT_IRQ_FLAGS_EDGE_LO_HI 1

hw/loongarch/virt-fdt-build.c +4 -0
+#define FDT_IRQ_FLAGS_EDGE_HI_LO 2
+#define FDT_IRQ_FLAGS_LEVEL_HI 4
+#define FDT_IRQ_FLAGS_LEVEL_LO 8
+

  static void create_fdt(LoongArchVirtMachineState *lvms) {
      MachineState *ms = MACHINE(lvms);

hw/loongarch/virt-fdt-build.c +2 -1
@@ -416,7 +421,8 @@ static void 
 fdt_add_uart_node(LoongArchVirtMachineState *lvms,
       if (chosen) {
           qemu_fdt_setprop_string(ms->fdt, "/chosen", "stdout-path", nodename);
       }
-    qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", irq, 0x4);
+    qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts", irq,
+                           FDT_IRQ_FLAGS_LEVEL_HI);
       qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                             *pch_pic_phandle);

      g_free(nodename);

hw/loongarch/virt-fdt-build.c +2 -1
@@ -436,7 +442,8 @@ static void 
 fdt_add_rtc_node(LoongArchVirtMachineState *lvms,
                               "loongson,ls7a-rtc");
       qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 2, base, 2, size);
       qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts",
-                           VIRT_RTC_IRQ - VIRT_GSI_BASE , 0x4);
+                           VIRT_RTC_IRQ - VIRT_GSI_BASE ,
+                           FDT_IRQ_FLAGS_EDGE_LO_HI);
       qemu_fdt_setprop_cell(ms->fdt, nodename, "interrupt-parent",
                             *pch_pic_phandle);

      g_free(nodename);

hw/rtc/ls7a_rtc.c +0 -0
index 10097b2db7..7eca75a42a 100644
@@ -145,20 +145,24 @@ static void toymatch_write(LS7ARtcState *s, 
 uint64_t val, int num)
Quoted 9 lines

hw/rtc/ls7a_rtc.c static void toymatch_write(LS7ARtcState *s, uint64_t val, int num) · line 150

Quoted 7 lines
hw/rtc/ls7a_rtc.c +3 -1
           now = qemu_clock_get_ms(rtc_clock);
           toymatch_val_to_time(s, val, &tm);
           expire_time = now + (qemu_timedate_diff(&tm) - s->offset_toy) * 1000;
-        timer_mod(s->toy_timer[num], expire_time);
+        if (expire_time > now) {
+            timer_mod(s->toy_timer[num], expire_time);
+        }

Should rtc alarm interrupt be injected directly if (expire_time <= now)?

Quoted 5 lines

Through experiments on the hardware platform, sending an alarm less than the current time to loongson rtc alarm does not trigger an interrupt.

According to the behavior of the hardware, we do not need to trigger an interrupt immediately when expire_time <= now

Sounds good. yes, the behavior of qemu emulation should be the same with real HW.

Regards
Bibo Mao

Quoted 70 lines

Thanks,
Xianglai Li.

Quoted 66 lines

Regards
Bibo Mao

Quoted 64 lines
hw/rtc/ls7a_rtc.c +4 -2
       }
   }
     static void rtcmatch_write(LS7ARtcState *s, uint64_t val, int num)
   {
-    uint64_t expire_ns;
+    int64_t expire_ns;
         /* it do not support write when toy disabled */
       if (rtc_enabled(s)) {
           s->rtcmatch[num] = val;
           /* calculate expire time */
           expire_ns = ticks_to_ns(val) - ticks_to_ns(s->offset_rtc);
-        timer_mod_ns(s->rtc_timer[num], expire_ns);
+        if (expire_ns > 0) {
+            timer_mod_ns(s->rtc_timer[num], expire_ns);
+        }
       }
   }
   @@ -185,7 +189,7 @@ static void ls7a_rtc_stop(LS7ARtcState *s)
   static void ls7a_toy_start(LS7ARtcState *s)

  {
      int i;
-    uint64_t expire_time, now;
+    int64_t expire_time, now; struct tm tm = {};
        now = qemu_clock_get_ms(rtc_clock);

hw/rtc/ls7a_rtc.c +8 -4
@@ -194,19 +198,23 @@ static void ls7a_toy_start(LS7ARtcState *s)
       for (i = 0; i < TIMER_NUMS; i++) {
           toymatch_val_to_time(s, s->toymatch[i], &tm);
           expire_time = now + (qemu_timedate_diff(&tm) - s->offset_toy) * 1000;
-        timer_mod(s->toy_timer[i], expire_time);
+        if (expire_time > now) {
+            timer_mod(s->toy_timer[i], expire_time);
+        }
       }
   }
     static void ls7a_rtc_start(LS7ARtcState *s)
   {
       int i;
-    uint64_t expire_time;
+    int64_t expire_time;
         /* recalculate expire time and enable timer */
       for (i = 0; i < TIMER_NUMS; i++) {
           expire_time = ticks_to_ns(s->rtcmatch[i]) - ticks_to_ns(s->offset_rtc);
-        timer_mod_ns(s->rtc_timer[i], expire_time);
+        if (expire_time > 0) {
+            timer_mod_ns(s->rtc_timer[i], expire_time);
+        }
       }
   }
   @@ -370,7 +378,7 @@ static void toy_timer_cb(void *opaque)
       LS7ARtcState *s = opaque;
         if (toy_enabled(s)) {
-        qemu_irq_raise(s->irq);
+        qemu_irq_pulse(s->irq);

      }
  }
  @@ -379,7 +387,7 @@ static void rtc_timer_cb(void *opaque) LS7ARtcState *s = opaque;
        if (rtc_enabled(s)) {
-        qemu_irq_raise(s->irq);
+        qemu_irq_pulse(s->irq);
      }
  }

#6 lore raw

PATCH 2/2 hw/loongarch: add rtc acpi table

In reply to Xianglai Li

Add the acpi table to the loongson rtc hardware
so that the virtual machine kernel can use the
loongson RTC-related drivers.

---
Cc: Bibo Mao <[email protected]>
Cc: Song Gao <[email protected]>
Cc: Jiaxun Yang <[email protected]>
Cc: Xianglai Li <[email protected]>

 hw/loongarch/virt-acpi-build.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
hw/loongarch/virt-acpi-build.c +24 -0
index 2cd2d9d842..2cfc5421c5 100644
@@ -473,6 +473,29 @@ static void acpi_dsdt_add_tpm(Aml *scope, LoongArchVirtMachineState *vms)
 }
 #endif
 
+static void acpi_dsdt_add_rtc(Aml *scope)
+{
+    uint32_t rtc_irq = VIRT_RTC_IRQ;
+    Aml *dev = aml_device("RTC");
+
+    aml_append(dev, aml_name_decl("_HID", aml_string("LOON0001")));
+    aml_append(dev, aml_name_decl("_UID", aml_int(0)));
+
+    Aml *crs = aml_resource_template();
+    aml_append(crs,
+        aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
+                         AML_NON_CACHEABLE, AML_READ_WRITE,
+                         0, VIRT_RTC_REG_BASE,
+                         VIRT_RTC_REG_BASE + VIRT_RTC_LEN - 1,
+                         0, VIRT_RTC_LEN));
+    aml_append(crs,
+               aml_interrupt(AML_CONSUMER, AML_EDGE, AML_ACTIVE_HIGH,
+                             AML_EXCLUSIVE, &rtc_irq, 1));
+
+    aml_append(dev, aml_name_decl("_CRS", crs));
+    aml_append(scope, dev);
+}
+
 /* build DSDT */
 static void
 build_dsdt(GArray *table_data, BIOSLinker *linker, MachineState *machine)
@@ -488,6 +511,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, MachineState *machine)
     for (i = 0; i < VIRT_UART_COUNT; i++) {
         build_uart_device_aml(dsdt, i);
     }
+    acpi_dsdt_add_rtc(dsdt);
     build_pci_device_aml(dsdt, lvms);
     build_la_ged_aml(dsdt, machine);
     build_flash_aml(dsdt, lvms);
Signature · 1 line
2.39.1
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH V1 2/2] hw/loongarch: add rtc acpi table

Xianglai,

Quoted 3 lines · Xianglai Li

Add the acpi table to the loongson rtc hardware
so that the virtual machine kernel can use the
loongson RTC-related drivers.

One line of the comments is no more than 75 bytes, it seems that it is far less than 75 bytes.

Quoted 47 lines

---
Cc: Bibo Mao <[email protected]>
Cc: Song Gao <[email protected]>
Cc: Jiaxun Yang <[email protected]>
Cc: Xianglai Li <[email protected]>

  hw/loongarch/virt-acpi-build.c | 24 ++++++++++++++++++++++++
  1 file changed, 24 insertions(+)
hw/loongarch/virt-acpi-build.c +23 -0
index 2cd2d9d842..2cfc5421c5 100644
@@ -473,6 +473,29 @@ static void acpi_dsdt_add_tpm(Aml *scope, LoongArchVirtMachineState *vms)
  }
  #endif
 
+static void acpi_dsdt_add_rtc(Aml *scope)
+{
+    uint32_t rtc_irq = VIRT_RTC_IRQ;
+    Aml *dev = aml_device("RTC");
+
+    aml_append(dev, aml_name_decl("_HID", aml_string("LOON0001")));
+    aml_append(dev, aml_name_decl("_UID", aml_int(0)));
+
+    Aml *crs = aml_resource_template();
+    aml_append(crs,
+        aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
+                         AML_NON_CACHEABLE, AML_READ_WRITE,
+                         0, VIRT_RTC_REG_BASE,
+                         VIRT_RTC_REG_BASE + VIRT_RTC_LEN - 1,
+                         0, VIRT_RTC_LEN));
+    aml_append(crs,
+               aml_interrupt(AML_CONSUMER, AML_EDGE, AML_ACTIVE_HIGH,
+                             AML_EXCLUSIVE, &rtc_irq, 1));
+
+    aml_append(dev, aml_name_decl("_CRS", crs));
+    aml_append(scope, dev);
+}
+
  /* build DSDT */
  static void
  build_dsdt(GArray *table_data, BIOSLinker *linker, MachineState *machine)

hw/loongarch/virt-acpi-build.c build_dsdt(GArray *table_data, BIOSLinker *linker, MachineState *machine) · line 517

Quoted 7 lines
hw/loongarch/virt-acpi-build.c +1 -0
@@ -488,6 +511,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, MachineState *machine)
      for (i = 0; i < VIRT_UART_COUNT; i++) {
          build_uart_device_aml(dsdt, i);
      }
+    acpi_dsdt_add_rtc(dsdt);
      build_pci_device_aml(dsdt, lvms);
      build_la_ged_aml(dsdt, machine);
      build_flash_aml(dsdt, lvms);

Otherwise looks good to me except comments alignment.

#8 lore raw

Re: [PATCH V1 2/2] hw/loongarch: add rtc acpi table

In reply to Xianglai Li
Quoted 35 lines · Xianglai Li

Add the acpi table to the loongson rtc hardware
so that the virtual machine kernel can use the
loongson RTC-related drivers.

---
Cc: Bibo Mao <[email protected]>
Cc: Song Gao <[email protected]>
Cc: Jiaxun Yang <[email protected]>
Cc: Xianglai Li <[email protected]>

  hw/loongarch/virt-acpi-build.c | 24 ++++++++++++++++++++++++
  1 file changed, 24 insertions(+)
hw/loongarch/virt-acpi-build.c +13 -0
index 2cd2d9d842..2cfc5421c5 100644
@@ -473,6 +473,29 @@ static void acpi_dsdt_add_tpm(Aml *scope, LoongArchVirtMachineState *vms)
  }
  #endif
 
+static void acpi_dsdt_add_rtc(Aml *scope)
+{
+    uint32_t rtc_irq = VIRT_RTC_IRQ;
+    Aml *dev = aml_device("RTC");
+
+    aml_append(dev, aml_name_decl("_HID", aml_string("LOON0001")));
+    aml_append(dev, aml_name_decl("_UID", aml_int(0)));
+
+    Aml *crs = aml_resource_template();
+    aml_append(crs,
+        aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
+                         AML_NON_CACHEABLE, AML_READ_WRITE,
+                         0, VIRT_RTC_REG_BASE,

hw/loongarch/virt-acpi-build.c static void acpi_dsdt_add_tpm(Aml *scope, LoongArchVirtMachineState *vms) · line 498

Quoted 10 lines
hw/loongarch/virt-acpi-build.c +10 -0
+                         VIRT_RTC_REG_BASE + VIRT_RTC_LEN - 1,
+                         0, VIRT_RTC_LEN));
+    aml_append(crs,
+               aml_interrupt(AML_CONSUMER, AML_EDGE, AML_ACTIVE_HIGH,
+                             AML_EXCLUSIVE, &rtc_irq, 1));
+
+    aml_append(dev, aml_name_decl("_CRS", crs));
+    aml_append(scope, dev);
+}
+

The rtc loongson driver drivers/rtc/rtc-loongson.c is bond with acpi pm1 device since rtc device is wake-up source on physical machine, however acpi ged device rather than pm1 device is used on virt machine.

I think firstly we should submit patch to rtc loongson driver drivers/rtc/rtc-loongson.c so that it supports non acpi pm1 device, then submit rtc acpi dsdt table.

Regards
Bibo Mao

Quoted 12 lines
hw/loongarch/virt-acpi-build.c +1 -0
  /* build DSDT */
  static void
  build_dsdt(GArray *table_data, BIOSLinker *linker, MachineState *machine)
@@ -488,6 +511,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, MachineState *machine)
      for (i = 0; i < VIRT_UART_COUNT; i++) {
          build_uart_device_aml(dsdt, i);
      }
+    acpi_dsdt_add_rtc(dsdt);
      build_pci_device_aml(dsdt, lvms);
      build_la_ged_aml(dsdt, machine);
      build_flash_aml(dsdt, lvms);