Skip to contentSkip to conversation

hw/intc: Fix upper/lower mtime write calculation

PATCH In review Jason Chien 2 participants 9 messages Jul 28, 2023 – Aug 23, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 hw/intc: Fix upper/lower mtime write calculation

When writing the upper mtime, we should keep the original lower mtime whose value is given by cpu_riscv_read_rtc() instead of cpu_riscv_read_rtc_raw(). The same logic applies to writes to lower mtime.

---
 hw/intc/riscv_aclint.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/intc/riscv_aclint.c +3 -2
index b466a6abaf..bf77e29a70 100644
@@ -208,11 +208,12 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr,
         return;
     } else if (addr == mtimer->time_base || addr == mtimer->time_base + 4) {
         uint64_t rtc_r = cpu_riscv_read_rtc_raw(mtimer->timebase_freq);
+        uint64_t rtc = cpu_riscv_read_rtc(mtimer);
 
         if (addr == mtimer->time_base) {
             if (size == 4) {
                 /* time_lo for RV32/RV64 */
-                mtimer->time_delta = ((rtc_r & ~0xFFFFFFFFULL) | value) - rtc_r;
+                mtimer->time_delta = ((rtc & ~0xFFFFFFFFULL) | value) - rtc_r;
             } else {
                 /* time for RV64 */
                 mtimer->time_delta = value - rtc_r;
@@ -220,7 +221,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr,
         } else {
             if (size == 4) {
                 /* time_hi for RV32/RV64 */
-                mtimer->time_delta = (value << 32 | (rtc_r & 0xFFFFFFFF)) - rtc_r;
+                mtimer->time_delta = (value << 32 | (rtc & 0xFFFFFFFF)) - rtc_r;
             } else {
                 qemu_log_mask(LOG_GUEST_ERROR,
                               "aclint-mtimer: invalid time_hi write: %08x",
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 hw/intc: Make rtc variable names consistent

The variables whose values are given by cpu_riscv_read_rtc() should be named "rtc". The variables whose value are given by cpu_riscv_read_rtc_raw() should be named "rtc_r".

---
 hw/intc/riscv_aclint.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/intc/riscv_aclint.c +3 -3
index bf77e29a70..25cf7a5d9d 100644
@@ -64,13 +64,13 @@ static void riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,
     uint64_t next;
     uint64_t diff;
 
-    uint64_t rtc_r = cpu_riscv_read_rtc(mtimer);
+    uint64_t rtc = cpu_riscv_read_rtc(mtimer);
 
     /* Compute the relative hartid w.r.t the socket */
     hartid = hartid - mtimer->hartid_base;
 
     mtimer->timecmp[hartid] = value;
-    if (mtimer->timecmp[hartid] <= rtc_r) {
+    if (mtimer->timecmp[hartid] <= rtc) {
         /*
          * If we're setting an MTIMECMP value in the "past",
          * immediately raise the timer interrupt
@@ -81,7 +81,7 @@ static void riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,
 
     /* otherwise, set up the future timer interrupt */
     qemu_irq_lower(mtimer->timer_irqs[hartid]);
-    diff = mtimer->timecmp[hartid] - rtc_r;
+    diff = mtimer->timecmp[hartid] - rtc;
     /* back to ns (note args switched in muldiv64) */
     uint64_t ns_diff = muldiv64(diff, NANOSECONDS_PER_SECOND, timebase_freq);
 
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 2/2] hw/intc: Make rtc variable names consistent

Hi,
The patch seems to be ignored. I am not sure who to ping. Could someone please review this patch? Thank you!
patch link:
https://lore.kernel.org/qemu-devel/[email protected]/

Jason

Quoted 46 lines · Jason Chien

The variables whose values are given by cpu_riscv_read_rtc() should be named
"rtc". The variables whose value are given by cpu_riscv_read_rtc_raw() should be named "rtc_r".

---
 hw/intc/riscv_aclint.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/intc/riscv_aclint.c +2 -2
index bf77e29a70..25cf7a5d9d 100644
@@ -64,13 +64,13 @@ static void
 riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,
     uint64_t next;
     uint64_t diff;
 
-    uint64_t rtc_r = cpu_riscv_read_rtc(mtimer);
+    uint64_t rtc = cpu_riscv_read_rtc(mtimer);
 
     /* Compute the relative hartid w.r.t the socket */
     hartid = hartid - mtimer->hartid_base;
 
     mtimer->timecmp[hartid] = value;
-    if (mtimer->timecmp[hartid] <= rtc_r) {
+    if (mtimer->timecmp[hartid] <= rtc) {
         /*
          * If we're setting an MTIMECMP value in the "past",

* immediately raise the timer interrupt

hw/intc/riscv_aclint.c +1 -1
@@ -81,7 +81,7 @@ static void
 riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,
 
     /* otherwise, set up the future timer interrupt */
     qemu_irq_lower(mtimer->timer_irqs[hartid]);
-    diff = mtimer->timecmp[hartid] - rtc_r;
+    diff = mtimer->timecmp[hartid] - rtc;
     /* back to ns (note args switched in muldiv64) */
     uint64_t ns_diff = muldiv64(diff, NANOSECONDS_PER_SECOND,

timebase_freq);

--
2.17.1

Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] hw/intc: Make rtc variable names consistent

In reply to Jason Chien
Quoted 6 lines · Jason Chien

The variables whose values are given by cpu_riscv_read_rtc() should be named "rtc". The variables whose value are given by cpu_riscv_read_rtc_raw() should be named "rtc_r".

Alistair

Quoted 37 lines
---
 hw/intc/riscv_aclint.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/intc/riscv_aclint.c +3 -4
index bf77e29a70..25cf7a5d9d 100644
@@ -64,13 +64,13 @@ static void riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,
     uint64_t next;
     uint64_t diff;
 
-    uint64_t rtc_r = cpu_riscv_read_rtc(mtimer);
+    uint64_t rtc = cpu_riscv_read_rtc(mtimer);
 
     /* Compute the relative hartid w.r.t the socket */
     hartid = hartid - mtimer->hartid_base;
 
     mtimer->timecmp[hartid] = value;
-    if (mtimer->timecmp[hartid] <= rtc_r) {
+    if (mtimer->timecmp[hartid] <= rtc) {
         /*
          * If we're setting an MTIMECMP value in the "past",
          * immediately raise the timer interrupt
@@ -81,7 +81,7 @@ static void riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,
 
     /* otherwise, set up the future timer interrupt */
     qemu_irq_lower(mtimer->timer_irqs[hartid]);
-    diff = mtimer->timecmp[hartid] - rtc_r;
+    diff = mtimer->timecmp[hartid] - rtc;
     /* back to ns (note args switched in muldiv64) */
     uint64_t ns_diff = muldiv64(diff, NANOSECONDS_PER_SECOND, timebase_freq);
 
--

2.17.1

#4 lore raw

Re: [PATCH 2/2] hw/intc: Make rtc variable names consistent

Ping.

Quoted 55 lines · Alistair Francis
Quoted 2 lines · Jason Chien

The variables whose values are given by cpu_riscv_read_rtc() should be

named

Quoted 4 lines

"rtc". The variables whose value are given by cpu_riscv_read_rtc_raw() should be named "rtc_r".

Alistair

hw/intc/riscv_aclint.c static void

Quoted 9 lines
---
 hw/intc/riscv_aclint.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/intc/riscv_aclint.c +0 -0
index bf77e29a70..25cf7a5d9d 100644
@@ -64,13 +64,13 @@ static void

riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,

hw/intc/riscv_aclint.c static void

Quoted 16 lines
hw/intc/riscv_aclint.c +2 -2
     uint64_t next;
     uint64_t diff;
 
-    uint64_t rtc_r = cpu_riscv_read_rtc(mtimer);
+    uint64_t rtc = cpu_riscv_read_rtc(mtimer);
 
     /* Compute the relative hartid w.r.t the socket */
     hartid = hartid - mtimer->hartid_base;
 
     mtimer->timecmp[hartid] = value;
-    if (mtimer->timecmp[hartid] <= rtc_r) {
+    if (mtimer->timecmp[hartid] <= rtc) {
         /*
          * If we're setting an MTIMECMP value in the "past",
          * immediately raise the timer interrupt
@@ -81,7 +81,7 @@ static void

riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,

hw/intc/riscv_aclint.c static void riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,

Quoted 7 lines
hw/intc/riscv_aclint.c +1 -1
     /* otherwise, set up the future timer interrupt */
     qemu_irq_lower(mtimer->timer_irqs[hartid]);
-    diff = mtimer->timecmp[hartid] - rtc_r;
+    diff = mtimer->timecmp[hartid] - rtc;
     /* back to ns (note args switched in muldiv64) */
     uint64_t ns_diff = muldiv64(diff, NANOSECONDS_PER_SECOND,

timebase_freq);

Quoted 5 lines
hw/intc/riscv_aclint.c +0 -1
--

2.17.1

#5 lore raw

Re: [PATCH 2/2] hw/intc: Make rtc variable names consistent

Quoted 2 lines · Jason Chien

Ping.

This has been applied to the RISC-V tree. It will go in after the QEMU release freeze is over (probably a week or two).

Alistair

Quoted 52 lines · Alistair Francis
Quoted 50 lines · Jason Chien
Quoted 6 lines

The variables whose values are given by cpu_riscv_read_rtc() should be named "rtc". The variables whose value are given by cpu_riscv_read_rtc_raw() should be named "rtc_r".

Alistair

Quoted 37 lines
---
 hw/intc/riscv_aclint.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/intc/riscv_aclint.c +3 -4
index bf77e29a70..25cf7a5d9d 100644
@@ -64,13 +64,13 @@ static void riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,
     uint64_t next;
     uint64_t diff;
 
-    uint64_t rtc_r = cpu_riscv_read_rtc(mtimer);
+    uint64_t rtc = cpu_riscv_read_rtc(mtimer);
 
     /* Compute the relative hartid w.r.t the socket */
     hartid = hartid - mtimer->hartid_base;
 
     mtimer->timecmp[hartid] = value;
-    if (mtimer->timecmp[hartid] <= rtc_r) {
+    if (mtimer->timecmp[hartid] <= rtc) {
         /*
          * If we're setting an MTIMECMP value in the "past",
          * immediately raise the timer interrupt
@@ -81,7 +81,7 @@ static void riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,
 
     /* otherwise, set up the future timer interrupt */
     qemu_irq_lower(mtimer->timer_irqs[hartid]);
-    diff = mtimer->timecmp[hartid] - rtc_r;
+    diff = mtimer->timecmp[hartid] - rtc;
     /* back to ns (note args switched in muldiv64) */
     uint64_t ns_diff = muldiv64(diff, NANOSECONDS_PER_SECOND, timebase_freq);
 
--

2.17.1

#6 lore raw

Re: [PATCH 2/2] hw/intc: Make rtc variable names consistent

Thanks for the update.

Quoted 69 lines · Alistair Francis
Quoted 2 lines · Jason Chien

Ping.

This has been applied to the RISC-V tree. It will go in after the QEMU release freeze is over (probably a week or two).

Alistair

Quoted 2 lines

On Fri, Aug 11, 2023 at 2:25 AM Alistair Francis <[email protected]>

Quoted 2 lines · wrote:
Quoted 2 lines

On Fri, Jul 28, 2023 at 4:57 AM Jason Chien <[email protected]>

Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines

The variables whose values are given by cpu_riscv_read_rtc() should

be named

Quoted 13 lines
Quoted 13 lines
Quoted 4 lines

"rtc". The variables whose value are given by cpu_riscv_read_rtc_raw() should be named "rtc_r".

Alistair

Quoted 4 lines
---
 hw/intc/riscv_aclint.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 5 lines
Quoted 5 lines

hw/intc/riscv_aclint.c static void

Quoted 5 lines
hw/intc/riscv_aclint.c +0 -0
index bf77e29a70..25cf7a5d9d 100644
@@ -64,13 +64,13 @@ static void

riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
hw/intc/riscv_aclint.c +1 -1
     uint64_t next;
     uint64_t diff;
 
-    uint64_t rtc_r = cpu_riscv_read_rtc(mtimer);
+    uint64_t rtc = cpu_riscv_read_rtc(mtimer);
 
     /* Compute the relative hartid w.r.t the socket */
     hartid = hartid - mtimer->hartid_base;
 
Quoted 7 lines
Quoted 7 lines

hw/intc/riscv_aclint.c static void

Quoted 7 lines
hw/intc/riscv_aclint.c +1 -1
     mtimer->timecmp[hartid] = value;
-    if (mtimer->timecmp[hartid] <= rtc_r) {
+    if (mtimer->timecmp[hartid] <= rtc) {
         /*
          * If we're setting an MTIMECMP value in the "past",
          * immediately raise the timer interrupt
@@ -81,7 +81,7 @@ static void

riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,

Quoted 7 lines
Quoted 7 lines

hw/intc/riscv_aclint.c static void riscv_aclint_mtimer_write_timecmp(RISCVAclintMTimerState *mtimer,

Quoted 7 lines
hw/intc/riscv_aclint.c +1 -1
     /* otherwise, set up the future timer interrupt */
     qemu_irq_lower(mtimer->timer_irqs[hartid]);
-    diff = mtimer->timecmp[hartid] - rtc_r;
+    diff = mtimer->timecmp[hartid] - rtc;
     /* back to ns (note args switched in muldiv64) */
     uint64_t ns_diff = muldiv64(diff, NANOSECONDS_PER_SECOND,

timebase_freq);

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
hw/intc/riscv_aclint.c +0 -1
--

2.17.1

#7 lore raw
In reply to Jason Chien

Hi,
The patch seems to be ignored. I am not sure who to ping. Could someone please review this patch? Thank you!
patch link:
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 48 lines · Jason Chien

When writing the upper mtime, we should keep the original lower mtime whose value is given by cpu_riscv_read_rtc() instead of cpu_riscv_read_rtc_raw(). The same logic applies to writes to lower mtime.

---
 hw/intc/riscv_aclint.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/intc/riscv_aclint.c +2 -1
index b466a6abaf..bf77e29a70 100644
@@ -208,11 +208,12 @@ static void riscv_aclint_mtimer_write(void *opaque,
 hwaddr addr,
         return;
     } else if (addr == mtimer->time_base || addr == mtimer->time_base +
 4) {
         uint64_t rtc_r = cpu_riscv_read_rtc_raw(mtimer->timebase_freq);
+        uint64_t rtc = cpu_riscv_read_rtc(mtimer);
 
         if (addr == mtimer->time_base) {
             if (size == 4) {
                 /* time_lo for RV32/RV64 */
-                mtimer->time_delta = ((rtc_r & ~0xFFFFFFFFULL) | value) -
 rtc_r;
+                mtimer->time_delta = ((rtc & ~0xFFFFFFFFULL) | value) -
rtc_r;
             } else {
                 /* time for RV64 */
                 mtimer->time_delta = value - rtc_r;
hw/intc/riscv_aclint.c +1 -2
@@ -220,7 +221,7 @@ static void riscv_aclint_mtimer_write(void *opaque,
 hwaddr addr,
         } else {
             if (size == 4) {
                 /* time_hi for RV32/RV64 */
-                mtimer->time_delta = (value << 32 | (rtc_r & 0xFFFFFFFF))
- rtc_r;
+                mtimer->time_delta = (value << 32 | (rtc & 0xFFFFFFFF)) -
 rtc_r;
             } else {
                 qemu_log_mask(LOG_GUEST_ERROR,
                               "aclint-mtimer: invalid time_hi write:

%08x",
--
2.17.1

Reviewed-by on v1 1/2
#8 lore raw
In reply to Jason Chien
Quoted 6 lines · Jason Chien

When writing the upper mtime, we should keep the original lower mtime whose value is given by cpu_riscv_read_rtc() instead of cpu_riscv_read_rtc_raw(). The same logic applies to writes to lower mtime.

Alistair

Quoted 35 lines
---
 hw/intc/riscv_aclint.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/intc/riscv_aclint.c +3 -3
index b466a6abaf..bf77e29a70 100644
@@ -208,11 +208,12 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr,
         return;
     } else if (addr == mtimer->time_base || addr == mtimer->time_base + 4) {
         uint64_t rtc_r = cpu_riscv_read_rtc_raw(mtimer->timebase_freq);
+        uint64_t rtc = cpu_riscv_read_rtc(mtimer);
 
         if (addr == mtimer->time_base) {
             if (size == 4) {
                 /* time_lo for RV32/RV64 */
-                mtimer->time_delta = ((rtc_r & ~0xFFFFFFFFULL) | value) - rtc_r;
+                mtimer->time_delta = ((rtc & ~0xFFFFFFFFULL) | value) - rtc_r;
             } else {
                 /* time for RV64 */
                 mtimer->time_delta = value - rtc_r;
@@ -220,7 +221,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr,
         } else {
             if (size == 4) {
                 /* time_hi for RV32/RV64 */
-                mtimer->time_delta = (value << 32 | (rtc_r & 0xFFFFFFFF)) - rtc_r;
+                mtimer->time_delta = (value << 32 | (rtc & 0xFFFFFFFF)) - rtc_r;
             } else {
                 qemu_log_mask(LOG_GUEST_ERROR,
                               "aclint-mtimer: invalid time_hi write: %08x",
--

2.17.1