Skip to contentSkip to conversation

A few RISC-V fixes

PATCH Superseded Alistair Francis 6 participants 14 messages Jun 30, 2020 – Jul 8, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover A few RISC-V fixes

This series has a few fixes for RISC-V.

Alistair Francis (3):
hw/char: Convert the Ibex UART to use the qdev Clock model hw/riscv: Allow 64 bit access to SiFive CLINT target/riscv: Regen floating point rounding mode in dynamic mode

 include/hw/char/ibex_uart.h |  2 ++
 hw/char/ibex_uart.c         | 19 ++++++++++++++++++-
 hw/riscv/sifive_clint.c     |  2 +-
 target/riscv/translate.c    |  2 +-
 4 files changed, 22 insertions(+), 3 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/3 hw/char: Convert the Ibex UART to use the qdev Clock model

Conver the Ibex UART to use the recently added qdev-clock functions.

---
 include/hw/char/ibex_uart.h |  2 ++
 hw/char/ibex_uart.c         | 19 ++++++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)
include/hw/char/ibex_uart.h +2 -0
index 2bec772615..322bfffd8b 100644
@@ -101,6 +101,8 @@ typedef struct {
     uint32_t uart_val;
     uint32_t uart_timeout_ctrl;
 
+    Clock *f_clk;
+
     CharBackend chr;
     qemu_irq tx_watermark;
     qemu_irq rx_watermark;
hw/char/ibex_uart.c +18 -1
index 45cd724998..f967e6919a 100644
@@ -28,6 +28,7 @@
 #include "qemu/osdep.h"
 #include "hw/char/ibex_uart.h"
 #include "hw/irq.h"
+#include "hw/qdev-clock.h"
 #include "hw/qdev-properties.h"
 #include "migration/vmstate.h"
 #include "qemu/log.h"
@@ -330,7 +331,7 @@ static void ibex_uart_write(void *opaque, hwaddr addr,
         }
         if (value & UART_CTRL_NCO) {
             uint64_t baud = ((value & UART_CTRL_NCO) >> 16);
-            baud *= 1000;
+            baud *= clock_get_hz(s->f_clk);
             baud >>= 20;
 
             s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
@@ -385,6 +386,18 @@ static void ibex_uart_write(void *opaque, hwaddr addr,
     }
 }
 
+static void ibex_uart_clk_update(void *opaque)
+{
+    IbexUartState *s = opaque;
+
+    /* recompute uart's speed on clock change */
+    uint64_t baud = ((s->uart_ctrl & UART_CTRL_NCO) >> 16);
+    baud *= clock_get_hz(s->f_clk);
+    baud >>= 20;
+
+    s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
+}
+
 static void fifo_trigger_update(void *opaque)
 {
     IbexUartState *s = opaque;
@@ -444,6 +457,10 @@ static void ibex_uart_init(Object *obj)
 {
     IbexUartState *s = IBEX_UART(obj);
 
+    s->f_clk = qdev_init_clock_in(DEVICE(obj), "f_clock",
+                                  ibex_uart_clk_update, s);
+    clock_set_hz(s->f_clk, 50000000);
+
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_watermark);
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_watermark);
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
Signature · 1 line
2.27.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH v1 1/3] hw/char: Convert the Ibex UART to use the qdev Clock model

+Damien

Quoted 1 line · Alistair Francis

Conver the Ibex UART to use the recently added qdev-clock functions.

Yeah! This is our first user \o/

Quoted 33 lines
---
 include/hw/char/ibex_uart.h |  2 ++
 hw/char/ibex_uart.c         | 19 ++++++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)
include/hw/char/ibex_uart.h +2 -0
index 2bec772615..322bfffd8b 100644
@@ -101,6 +101,8 @@ typedef struct {
     uint32_t uart_val;
     uint32_t uart_timeout_ctrl;
 
+    Clock *f_clk;
+
     CharBackend chr;
     qemu_irq tx_watermark;
     qemu_irq rx_watermark;
hw/char/ibex_uart.c +1 -0
index 45cd724998..f967e6919a 100644
@@ -28,6 +28,7 @@
 #include "qemu/osdep.h"
 #include "hw/char/ibex_uart.h"
 #include "hw/irq.h"
+#include "hw/qdev-clock.h"
 #include "hw/qdev-properties.h"
 #include "migration/vmstate.h"
 #include "qemu/log.h"

hw/char/ibex_uart.c static void ibex_uart_write(void *opaque, hwaddr addr, · line 333

Quoted 3 lines
hw/char/ibex_uart.c +0 -0
@@ -330,7 +331,7 @@ static void ibex_uart_write(void *opaque, hwaddr addr,
         }
         if (value & UART_CTRL_NCO) {
             uint64_t baud = ((value & UART_CTRL_NCO) >> 16);

UART_CTRL_NCO is defined as:

  #define UART_CTRL_NCO           (0xFFFF << 16)

Note for later, convert to the clearer registerfields API?

Quoted 7 lines
hw/char/ibex_uart.c +1 -1
-            baud *= 1000;
+            baud *= clock_get_hz(s->f_clk);
             baud >>= 20;
 
             s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
@@ -385,6 +386,18 @@ static void ibex_uart_write(void *opaque, hwaddr addr,
     }

hw/char/ibex_uart.c static void ibex_uart_write(void *opaque, hwaddr addr, · line 396

Quoted 10 lines
hw/char/ibex_uart.c +8 -0
 }
 
+static void ibex_uart_clk_update(void *opaque)
+{
+    IbexUartState *s = opaque;
+
+    /* recompute uart's speed on clock change */
+    uint64_t baud = ((s->uart_ctrl & UART_CTRL_NCO) >> 16);
+    baud *= clock_get_hz(s->f_clk);
+    baud >>= 20;

Maybe worth to extract:

  uint64_t ibex_uart_get_baud(IbexUartState *s)
  {
       uint64_t baud;
       baud = ((s->uart_ctrl & UART_CTRL_NCO) >> 16);
       baud *= clock_get_hz(s->f_clk);
       baud >>= 20;

       return baud;

}

Quoted 8 lines
hw/char/ibex_uart.c +4 -0
+
+    s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
+}
+
 static void fifo_trigger_update(void *opaque)
 {
     IbexUartState *s = opaque;

hw/char/ibex_uart.c static void ibex_uart_init(Object *obj) · line 462

Quoted 6 lines
hw/char/ibex_uart.c +3 -0
@@ -444,6 +457,10 @@ static void ibex_uart_init(Object *obj)
 {
     IbexUartState *s = IBEX_UART(obj);
 
+    s->f_clk = qdev_init_clock_in(DEVICE(obj), "f_clock",
+                                  ibex_uart_clk_update, s);
+    clock_set_hz(s->f_clk, 50000000);

Can you add a definition for this 50 MHz value:

Otherwise:

Quoted 5 lines
hw/char/ibex_uart.c +1 -0
+
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_watermark);
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_watermark);
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
#3 lore raw

Re: [PATCH v1 1/3] hw/char: Convert the Ibex UART to use the qdev Clock model

Quoted 7 lines · Philippe Mathieu-Daudé

+Damien

Quoted 1 line · Alistair Francis

Conver the Ibex UART to use the recently added qdev-clock functions.

Yeah! This is our first user \o/

:)

Quoted 40 lines
Quoted 34 lines
---
 include/hw/char/ibex_uart.h |  2 ++
 hw/char/ibex_uart.c         | 19 ++++++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)
include/hw/char/ibex_uart.h +2 -0
index 2bec772615..322bfffd8b 100644
@@ -101,6 +101,8 @@ typedef struct {
     uint32_t uart_val;
     uint32_t uart_timeout_ctrl;
 
+    Clock *f_clk;
+
     CharBackend chr;
     qemu_irq tx_watermark;
     qemu_irq rx_watermark;
hw/char/ibex_uart.c +1 -0
index 45cd724998..f967e6919a 100644
@@ -28,6 +28,7 @@
 #include "qemu/osdep.h"
 #include "hw/char/ibex_uart.h"
 #include "hw/irq.h"
+#include "hw/qdev-clock.h"
 #include "hw/qdev-properties.h"
 #include "migration/vmstate.h"
 #include "qemu/log.h"

hw/char/ibex_uart.c static void ibex_uart_write(void *opaque, hwaddr addr, · line 333

Quoted 3 lines
hw/char/ibex_uart.c +0 -0
@@ -330,7 +331,7 @@ static void ibex_uart_write(void *opaque, hwaddr addr,
         }
         if (value & UART_CTRL_NCO) {
             uint64_t baud = ((value & UART_CTRL_NCO) >> 16);

UART_CTRL_NCO is defined as:

Quoted 3 lines
  #define UART_CTRL_NCO           (0xFFFF << 16)

Note for later, convert to the clearer registerfields API?

Done, I have added a patch to do this.

Quoted 25 lines
Quoted 8 lines
hw/char/ibex_uart.c +1 -1
-            baud *= 1000;
+            baud *= clock_get_hz(s->f_clk);
             baud >>= 20;
 
             s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
@@ -385,6 +386,18 @@ static void ibex_uart_write(void *opaque, hwaddr addr,
     }

hw/char/ibex_uart.c static void ibex_uart_write(void *opaque, hwaddr addr, · line 396

Quoted 10 lines
hw/char/ibex_uart.c +8 -0
 }
 
+static void ibex_uart_clk_update(void *opaque)
+{
+    IbexUartState *s = opaque;
+
+    /* recompute uart's speed on clock change */
+    uint64_t baud = ((s->uart_ctrl & UART_CTRL_NCO) >> 16);
+    baud *= clock_get_hz(s->f_clk);
+    baud >>= 20;

Maybe worth to extract:

  uint64_t ibex_uart_get_baud(IbexUartState *s)
  {
       uint64_t baud;
Quoted 6 lines
       baud = ((s->uart_ctrl & UART_CTRL_NCO) >> 16);
       baud *= clock_get_hz(s->f_clk);
       baud >>= 20;

       return baud;

}

Done.

Quoted 12 lines
Quoted 12 lines
hw/char/ibex_uart.c +4 -0
+
+    s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
+}
+
 static void fifo_trigger_update(void *opaque)
 {
     IbexUartState *s = opaque;
@@ -444,6 +457,10 @@ static void ibex_uart_init(Object *obj)
 {
     IbexUartState *s = IBEX_UART(obj);
 
Quoted 5 lines

hw/char/ibex_uart.c static void ibex_uart_init(Object *obj) · line 462

Quoted 3 lines
hw/char/ibex_uart.c +3 -0
+    s->f_clk = qdev_init_clock_in(DEVICE(obj), "f_clock",
+                                  ibex_uart_clk_update, s);
+    clock_set_hz(s->f_clk, 50000000);

Can you add a definition for this 50 MHz value:

Done.

Quoted 3 lines

Otherwise:

Thanks!

Alistair

Quoted 7 lines
Quoted 6 lines
hw/char/ibex_uart.c +1 -0
+
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_watermark);
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_watermark);
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
#4 lore raw

PATCH 2/3 hw/riscv: Allow 64 bit access to SiFive CLINT

In reply to Alistair Francis

Commit 5d971f9e672507210e77d020d89e0e89165c8fc9
"memory: Revert "memory: accept mismatching sizes in memory_region_access_valid"" broke most RISC-V boards as they do 64 bit accesses to the CLINT and QEMU would trigger a fault. Fix this failure by allowing 8 byte accesses.

---
 hw/riscv/sifive_clint.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/riscv/sifive_clint.c +1 -1
index b11ffa0edc..669c21adc2 100644
@@ -181,7 +181,7 @@ static const MemoryRegionOps sifive_clint_ops = {
     .endianness = DEVICE_LITTLE_ENDIAN,
     .valid = {
         .min_access_size = 4,
-        .max_access_size = 4
+        .max_access_size = 8
     }
 };
 
Signature · 1 line
2.27.0
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH v1 2/3] hw/riscv: Allow 64 bit access to SiFive CLINT

Quoted 16 lines · Alistair Francis

Commit 5d971f9e672507210e77d020d89e0e89165c8fc9
"memory: Revert "memory: accept mismatching sizes in memory_region_access_valid"" broke most RISC-V boards as they do 64 bit accesses to the CLINT and QEMU would trigger a fault. Fix this failure by allowing 8 byte accesses.

---
  hw/riscv/sifive_clint.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/riscv/sifive_clint.c +0 -0
index b11ffa0edc..669c21adc2 100644

hw/riscv/sifive_clint.c static const MemoryRegionOps sifive_clint_ops = { · line 187

Quoted 8 lines
hw/riscv/sifive_clint.c +1 -1
@@ -181,7 +181,7 @@ static const MemoryRegionOps sifive_clint_ops = {
      .endianness = DEVICE_LITTLE_ENDIAN,
      .valid = {
          .min_access_size = 4,
-        .max_access_size = 4
+        .max_access_size = 8
      }
  };
 
#6 lore raw

Re: [PATCH v1 2/3] hw/riscv: Allow 64 bit access to SiFive CLINT

Quoted 15 lines · LIU Zhiwei
Quoted 11 lines · Alistair Francis

Commit 5d971f9e672507210e77d020d89e0e89165c8fc9
"memory: Revert "memory: accept mismatching sizes in memory_region_access_valid"" broke most RISC-V boards as they do 64 bit accesses to the CLINT and QEMU would trigger a fault. Fix this failure by allowing 8 byte accesses.

---
  hw/riscv/sifive_clint.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

hw/riscv/sifive_clint.c static const MemoryRegionOps sifive_clint_ops = { · line 187

Quoted 13 lines
hw/riscv/sifive_clint.c +1 -1
index b11ffa0edc..669c21adc2 100644
@@ -181,7 +181,7 @@ static const MemoryRegionOps sifive_clint_ops = {
      .endianness = DEVICE_LITTLE_ENDIAN,
      .valid = {
          .min_access_size = 4,
-        .max_access_size = 4
+        .max_access_size = 8
      }
  };
 

Thanks for the review. As most RISC-V machines are broken without this patch I have applied it to my next PR.

Alistair

Quoted 2 lines
Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH v1 2/3] hw/riscv: Allow 64 bit access to SiFive CLINT

In reply to Alistair Francis
Quoted 6 lines · Alistair Francis

Commit 5d971f9e672507210e77d020d89e0e89165c8fc9
"memory: Revert "memory: accept mismatching sizes in memory_region_access_valid"" broke most RISC-V boards as they do 64 bit accesses to the CLINT and QEMU would trigger a fault. Fix this failure by allowing 8 byte accesses.

Fixes 5d971f9e67 (Revert "accept mismatching sizes in access_valid")
Quoted 19 lines
---
 hw/riscv/sifive_clint.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/riscv/sifive_clint.c +1 -1
index b11ffa0edc..669c21adc2 100644
@@ -181,7 +181,7 @@ static const MemoryRegionOps sifive_clint_ops = {
     .endianness = DEVICE_LITTLE_ENDIAN,
     .valid = {
         .min_access_size = 4,
-        .max_access_size = 4
+        .max_access_size = 8
     }
 };
 
#8 lore raw

PATCH 3/3 target/riscv: Regen floating point rounding mode in dynamic mode

In reply to Alistair Francis

When a guest specificies the the rounding mode should be dynamic 0b111 then we want to re-caclulate the rounding mode on each instruction. The gen_helper_set_rounding_mode() function will correctly check the rounding mode and handle a dynamic rounding, we just need to make sure it's always called if dynamic rounding is selected.

Fixes 1885350 ("RISCV dynamic rounding mode is not behaving correctly")
---
 target/riscv/translate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/translate.c +1 -1
index ce71ca7a92..a39eba679a 100644
@@ -490,7 +490,7 @@ static void gen_set_rm(DisasContext *ctx, int rm)
 {
     TCGv_i32 t0;
 
-    if (ctx->frm == rm) {
+    if (ctx->frm == rm && rm != 7) {
         return;
     }
     ctx->frm = rm;
Signature · 1 line
2.27.0
#9 lore raw

Re: [PATCH v1 3/3] target/riscv: Regen floating point rounding mode in dynamic mode

Quoted 17 lines · Alistair Francis

When a guest specificies the the rounding mode should be dynamic 0b111 then we want to re-caclulate the rounding mode on each instruction. The gen_helper_set_rounding_mode() function will correctly check the rounding mode and handle a dynamic rounding, we just need to make sure it's always called if dynamic rounding is selected.

Fixes 1885350 ("RISCV dynamic rounding mode is not behaving correctly")
---
 target/riscv/translate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/translate.c +0 -0
index ce71ca7a92..a39eba679a 100644

target/riscv/translate.c static void gen_set_rm(DisasContext *ctx, int rm) · line 494

Quoted 6 lines
target/riscv/translate.c +1 -1
@@ -490,7 +490,7 @@ static void gen_set_rm(DisasContext *ctx, int rm)
 {
     TCGv_i32 t0;
 
-    if (ctx->frm == rm) {
+    if (ctx->frm == rm && rm != 7) {
         return;

This should not be necessary.

It was my understanding that after the set to the csr, that we would end the TB. That's certainly what I see in RISCV_OP_CSR_POST.

The next TB will begin wiht ctx->frm = -1, so we will reset the rounding mode with 7. It would be good to understand what's really going on here.

r~

#10 lore raw

Re: [PATCH v1 3/3] target/riscv: Regen floating point rounding mode in dynamic mode

Quoted 26 lines · Richard Henderson
Quoted 17 lines · Alistair Francis

When a guest specificies the the rounding mode should be dynamic 0b111 then we want to re-caclulate the rounding mode on each instruction. The gen_helper_set_rounding_mode() function will correctly check the rounding mode and handle a dynamic rounding, we just need to make sure it's always called if dynamic rounding is selected.

Fixes 1885350 ("RISCV dynamic rounding mode is not behaving correctly")
---
  target/riscv/translate.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/translate.c +0 -0
index ce71ca7a92..a39eba679a 100644

target/riscv/translate.c static void gen_set_rm(DisasContext *ctx, int rm) · line 494

Quoted 6 lines
target/riscv/translate.c +1 -1
@@ -490,7 +490,7 @@ static void gen_set_rm(DisasContext *ctx, int rm)
  {
      TCGv_i32 t0;
 
-    if (ctx->frm == rm) {
+    if (ctx->frm == rm && rm != 7) {
          return;

This should not be necessary.

Quoted 5 lines

It was my understanding that after the set to the csr, that we would end the TB. That's certainly what I see in RISCV_OP_CSR_POST.

The next TB will begin wiht ctx->frm = -1, so we will reset the rounding mode with 7. It would be good to understand what's really going on here.

Agree. I think the 'bug '  is false positive.
Although the round mode process code is  confusing, it it right.

Zhiwei

Quoted 2 lines

r~

#11 lore raw

Re: [PATCH v1 3/3] target/riscv: Regen floating point rounding mode in dynamic mode

In reply to Alistair Francis
Quoted 8 lines · Alistair Francis

When a guest specificies the the rounding mode should be dynamic 0b111 then we want to re-caclulate the rounding mode on each instruction. The gen_helper_set_rounding_mode() function will correctly check the rounding mode and handle a dynamic rounding, we just need to make sure it's always called if dynamic rounding is selected.

Fixes 1885350 ("RISCV dynamic rounding mode is not behaving correctly")

I can't find this commit id.

Quoted 5 lines
---
 target/riscv/translate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Regards,
Bin

#12 lore raw

Re: [PATCH v1 3/3] target/riscv: Regen floating point rounding mode in dynamic mode

Quoted 10 lines · Bin Meng
Quoted 7 lines · Alistair Francis

When a guest specificies the the rounding mode should be dynamic 0b111 then we want to re-caclulate the rounding mode on each instruction. The gen_helper_set_rounding_mode() function will correctly check the rounding mode and handle a dynamic rounding, we just need to make sure it's always called if dynamic rounding is selected.

Quoted 3 lines
Quoted 1 line
Fixes 1885350 ("RISCV dynamic rounding mode is not behaving correctly")

I can't find this commit id.

It's a launchpad bug case: https://bugs.launchpad.net/qemu/+bug/1885350

Alistair

Quoted 9 lines
Quoted 6 lines
---
 target/riscv/translate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Regards,
Bin

#13 lore raw

Re: [PATCH v1 3/3] target/riscv: Regen floating point rounding mode in dynamic mode

Hi Alistair,

Quoted 12 lines · Alistair Francis
Quoted 10 lines · Bin Meng
Quoted 7 lines · Alistair Francis

When a guest specificies the the rounding mode should be dynamic 0b111 then we want to re-caclulate the rounding mode on each instruction. The gen_helper_set_rounding_mode() function will correctly check the rounding mode and handle a dynamic rounding, we just need to make sure it's always called if dynamic rounding is selected.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Fixes 1885350 ("RISCV dynamic rounding mode is not behaving correctly")

I can't find this commit id.

It's a launchpad bug case: https://bugs.launchpad.net/qemu/+bug/1885350

My understanding is that the convention used in "Fixes" tag is for commit id. 1885350 is not a commit id but a QEMU bug report id.

Regards,
Bin