Skip to contentSkip to conversation

RISC-V Add the OpenTitan Machine

PATCH Superseded Alistair Francis 6 participants 21 messages May 19, 2020 – May 27, 2020 RISC-V

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover RISC-V Add the OpenTitan Machine

OpenTitan is an open source silicon Root of Trust (RoT) project. This series adds initial support for the OpenTitan machine to QEMU.

This series add the Ibex CPU to the QEMU RISC-V target. It then adds the OpenTitan machine, the Ibex UART and the Ibex PLIC.

The UART has been tested sending and receiving data.

With this series QEMU can boot the OpenTitan ROM, Tock OS and a Tock userspace app.

The Ibex PLIC is similar to the RISC-V PLIC (and is based on the QEMU implementation) with some differences. The hope is that the Ibex PLIC will converge to follow the RISC-V spec. As that happens I want to update the QEMU Ibex PLIC and hopefully eventually replace the current PLIC as the implementation is a little overlay complex.

For more details on OpenTitan, see here: https://docs.opentitan.org/

v3:
- Don't set the reset vector in realise
- Small fixes pointed out in review
v2:
- Rebase on master
- Get uart receive working

Alistair Francis (9):
riscv/boot: Add a missing header include target/riscv: Don't overwrite the reset vector target/riscv: Add the lowRISC Ibex CPU
riscv: Initial commit of OpenTitan machine hw/char: Initial commit of Ibex UART hw/intc: Initial commit of lowRISC Ibex PLIC riscv/opentitan: Connect the PLIC device riscv/opentitan: Connect the UART device target/riscv: Use a smaller guess size for no-MMU PMP

 default-configs/riscv32-softmmu.mak |   1 +
 default-configs/riscv64-softmmu.mak |  11 +-
 include/hw/char/ibex_uart.h         | 110 +++++++
 include/hw/intc/ibex_plic.h         |  63 ++++
 include/hw/riscv/boot.h             |   1 +
 include/hw/riscv/opentitan.h        |  79 +++++
 target/riscv/cpu.h                  |   1 +
 hw/char/ibex_uart.c                 | 492 ++++++++++++++++++++++++++++
 hw/intc/ibex_plic.c                 | 261 +++++++++++++++
 hw/riscv/opentitan.c                | 204 ++++++++++++
 target/riscv/cpu.c                  |  13 +-
 target/riscv/pmp.c                  |  14 +-
 MAINTAINERS                         |  13 +
 hw/char/Makefile.objs               |   1 +
 hw/intc/Makefile.objs               |   1 +
 hw/riscv/Kconfig                    |   9 +
 hw/riscv/Makefile.objs              |   1 +
 17 files changed, 1268 insertions(+), 7 deletions(-)
 create mode 100644 include/hw/char/ibex_uart.h
 create mode 100644 include/hw/intc/ibex_plic.h
 create mode 100644 include/hw/riscv/opentitan.h
 create mode 100644 hw/char/ibex_uart.c
 create mode 100644 hw/intc/ibex_plic.c
 create mode 100644 hw/riscv/opentitan.c
Signature · 1 line
2.26.2
#1 lore raw

PATCH v3 1/9 riscv/boot: Add a missing header include

Currently every c file that includes boot.h also includes loader.h before it. Which is why the build works fine. We should be able to include just boot.h though so this is a small fixup to allow that.

---
 include/hw/riscv/boot.h | 1 +
 1 file changed, 1 insertion(+)
include/hw/riscv/boot.h +1 -0
index 474a940ad5..9daa98da08 100644
@@ -21,6 +21,7 @@
 #define RISCV_BOOT_H
 
 #include "exec/cpu-defs.h"
+#include "hw/loader.h"
 
 void riscv_find_and_load_firmware(MachineState *machine,
                                   const char *default_machine_firmware,
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH v3 1/9] riscv/boot: Add a missing header include

Quoted 3 lines · Alistair Francis

Currently every c file that includes boot.h also includes loader.h before it. Which is why the build works fine. We should be able to include just boot.h though so this is a small fixup to allow that.

What about:

'As the functions declared in this header use the symbol_fn_t
typedef itself declared in "hw/loader.h", we need to include it here to make the header file self-contained.'

Quoted 21 lines
---
  include/hw/riscv/boot.h | 1 +
  1 file changed, 1 insertion(+)
include/hw/riscv/boot.h +1 -0
index 474a940ad5..9daa98da08 100644
@@ -21,6 +21,7 @@
  #define RISCV_BOOT_H
 
  #include "exec/cpu-defs.h"
+#include "hw/loader.h"
 
  void riscv_find_and_load_firmware(MachineState *machine,
                                    const char *default_machine_firmware,
#3 lore raw

Re: [PATCH v3 1/9] riscv/boot: Add a missing header include

Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Alistair Francis

Currently every c file that includes boot.h also includes loader.h before it. Which is why the build works fine. We should be able to include just boot.h though so this is a small fixup to allow that.

What about:

'As the functions declared in this header use the symbol_fn_t
typedef itself declared in "hw/loader.h", we need to include it here to make the header file self-contained.'

Sounds good.

Alistair

Quoted 23 lines
Quoted 22 lines
---
  include/hw/riscv/boot.h | 1 +
  1 file changed, 1 insertion(+)
include/hw/riscv/boot.h +1 -0
index 474a940ad5..9daa98da08 100644
@@ -21,6 +21,7 @@
  #define RISCV_BOOT_H
 
  #include "exec/cpu-defs.h"
+#include "hw/loader.h"
 
  void riscv_find_and_load_firmware(MachineState *machine,
                                    const char *default_machine_firmware,
#4 lore raw

PATCH v3 2/9 target/riscv: Don't overwrite the reset vector

In reply to Alistair Francis

The reset vector is set in the init function don't set it again in realize.

---
 target/riscv/cpu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/riscv/cpu.c +2 -1
index 059d71f2c7..5eb3c02735 100644
@@ -133,6 +133,7 @@ static void riscv_base32_cpu_init(Object *obj)
     CPURISCVState *env = &RISCV_CPU(obj)->env;
     /* We set this in the realise function */
     set_misa(env, 0);
+    set_resetvec(env, DEFAULT_RSTVEC);
 }
 
 static void rv32gcsu_priv1_09_1_cpu_init(Object *obj)
@@ -180,6 +181,7 @@ static void riscv_base64_cpu_init(Object *obj)
     CPURISCVState *env = &RISCV_CPU(obj)->env;
     /* We set this in the realise function */
     set_misa(env, 0);
+    set_resetvec(env, DEFAULT_RSTVEC);
 }
 
 static void rv64gcsu_priv1_09_1_cpu_init(Object *obj)
@@ -399,7 +401,6 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
     }
 
     set_priv_version(env, priv_version);
-    set_resetvec(env, DEFAULT_RSTVEC);
 
     if (cpu->cfg.mmu) {
         set_feature(env, RISCV_FEATURE_MMU);
Signature · 1 line
2.26.2
#6 lore raw

PATCH v3 3/9 target/riscv: Add the lowRISC Ibex CPU

In reply to Alistair Francis

Ibex is a small and efficient, 32-bit, in-order RISC-V core with a 2-stage pipeline that implements the RV32IMC instruction set architecture.

For more details on lowRISC see here:
https://github.com/lowRISC/ibex

---
 target/riscv/cpu.h |  1 +
 target/riscv/cpu.c | 10 ++++++++++
 2 files changed, 11 insertions(+)
target/riscv/cpu.h +1 -0
index d0e7f5b9c5..8733d7467f 100644
@@ -35,6 +35,7 @@
 #define TYPE_RISCV_CPU_ANY              RISCV_CPU_TYPE_NAME("any")
 #define TYPE_RISCV_CPU_BASE32           RISCV_CPU_TYPE_NAME("rv32")
 #define TYPE_RISCV_CPU_BASE64           RISCV_CPU_TYPE_NAME("rv64")
+#define TYPE_RISCV_CPU_IBEX             RISCV_CPU_TYPE_NAME("lowrisc-ibex")
 #define TYPE_RISCV_CPU_SIFIVE_E31       RISCV_CPU_TYPE_NAME("sifive-e31")
 #define TYPE_RISCV_CPU_SIFIVE_E34       RISCV_CPU_TYPE_NAME("sifive-e34")
 #define TYPE_RISCV_CPU_SIFIVE_E51       RISCV_CPU_TYPE_NAME("sifive-e51")
target/riscv/cpu.c +10 -0
index 5eb3c02735..eb2bbc87ae 100644
@@ -156,6 +156,15 @@ static void rv32gcsu_priv1_10_0_cpu_init(Object *obj)
     set_feature(env, RISCV_FEATURE_PMP);
 }
 
+static void rv32imcu_nommu_cpu_init(Object *obj)
+{
+    CPURISCVState *env = &RISCV_CPU(obj)->env;
+    set_misa(env, RV32 | RVI | RVM | RVC | RVU);
+    set_priv_version(env, PRIV_VERSION_1_10_0);
+    set_resetvec(env, 0x8090);
+    set_feature(env, RISCV_FEATURE_PMP);
+}
+
 static void rv32imacu_nommu_cpu_init(Object *obj)
 {
     CPURISCVState *env = &RISCV_CPU(obj)->env;
@@ -619,6 +628,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
     DEFINE_CPU(TYPE_RISCV_CPU_ANY,              riscv_any_cpu_init),
 #if defined(TARGET_RISCV32)
     DEFINE_CPU(TYPE_RISCV_CPU_BASE32,           riscv_base32_cpu_init),
+    DEFINE_CPU(TYPE_RISCV_CPU_IBEX,             rv32imcu_nommu_cpu_init),
     DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31,       rv32imacu_nommu_cpu_init),
     DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34,       rv32imafcu_nommu_cpu_init),
     DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34,       rv32gcsu_priv1_10_0_cpu_init),
Signature · 1 line
2.26.2
#7 lore raw

Re: [PATCH v3 3/9] target/riscv: Add the lowRISC Ibex CPU

Quoted 31 lines · Alistair Francis

Ibex is a small and efficient, 32-bit, in-order RISC-V core with a 2-stage pipeline that implements the RV32IMC instruction set architecture.

For more details on lowRISC see here:
https://github.com/lowRISC/ibex

---
  target/riscv/cpu.h |  1 +
  target/riscv/cpu.c | 10 ++++++++++
  2 files changed, 11 insertions(+)
target/riscv/cpu.h +1 -0
index d0e7f5b9c5..8733d7467f 100644
@@ -35,6 +35,7 @@
  #define TYPE_RISCV_CPU_ANY              RISCV_CPU_TYPE_NAME("any")
  #define TYPE_RISCV_CPU_BASE32           RISCV_CPU_TYPE_NAME("rv32")
  #define TYPE_RISCV_CPU_BASE64           RISCV_CPU_TYPE_NAME("rv64")
+#define TYPE_RISCV_CPU_IBEX             RISCV_CPU_TYPE_NAME("lowrisc-ibex")
  #define TYPE_RISCV_CPU_SIFIVE_E31       RISCV_CPU_TYPE_NAME("sifive-e31")
  #define TYPE_RISCV_CPU_SIFIVE_E34       RISCV_CPU_TYPE_NAME("sifive-e34")
  #define TYPE_RISCV_CPU_SIFIVE_E51       RISCV_CPU_TYPE_NAME("sifive-e51")
target/riscv/cpu.c +0 -0
index 5eb3c02735..eb2bbc87ae 100644

target/riscv/cpu.c static void rv32gcsu_priv1_10_0_cpu_init(Object *obj) · line 164

Quoted 9 lines
target/riscv/cpu.c +6 -0
@@ -156,6 +156,15 @@ static void rv32gcsu_priv1_10_0_cpu_init(Object *obj)
      set_feature(env, RISCV_FEATURE_PMP);
  }
 
+static void rv32imcu_nommu_cpu_init(Object *obj)
+{
+    CPURISCVState *env = &RISCV_CPU(obj)->env;
+    set_misa(env, RV32 | RVI | RVM | RVC | RVU);
+    set_priv_version(env, PRIV_VERSION_1_10_0);
+    set_resetvec(env, 0x8090);

Hi Alistair,

I see all RISC-V cpus  have an reset vector which acts as the first pc when machine boots up. However, the first pc is more like an attribute of a machine, not a cpu.

Another reason is that the cpu names are a combination of ISA.
Then the cpus from different vendors may have same ISA, with different reset vectors.

Do you think so?

Zhiwei

Quoted 14 lines
target/riscv/cpu.c +4 -0
+    set_feature(env, RISCV_FEATURE_PMP);
+}
+
  static void rv32imacu_nommu_cpu_init(Object *obj)
  {
      CPURISCVState *env = &RISCV_CPU(obj)->env;
@@ -619,6 +628,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
      DEFINE_CPU(TYPE_RISCV_CPU_ANY,              riscv_any_cpu_init),
  #if defined(TARGET_RISCV32)
      DEFINE_CPU(TYPE_RISCV_CPU_BASE32,           riscv_base32_cpu_init),
+    DEFINE_CPU(TYPE_RISCV_CPU_IBEX,             rv32imcu_nommu_cpu_init),
      DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31,       rv32imacu_nommu_cpu_init),
      DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34,       rv32imafcu_nommu_cpu_init),
      DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34,       rv32gcsu_priv1_10_0_cpu_init),
#8 lore raw

Re: [PATCH v3 3/9] target/riscv: Add the lowRISC Ibex CPU

Quoted 46 lines · LIU Zhiwei
Quoted 31 lines · Alistair Francis

Ibex is a small and efficient, 32-bit, in-order RISC-V core with a 2-stage pipeline that implements the RV32IMC instruction set architecture.

For more details on lowRISC see here:
https://github.com/lowRISC/ibex

---
  target/riscv/cpu.h |  1 +
  target/riscv/cpu.c | 10 ++++++++++
  2 files changed, 11 insertions(+)
target/riscv/cpu.h +1 -0
index d0e7f5b9c5..8733d7467f 100644
@@ -35,6 +35,7 @@
  #define TYPE_RISCV_CPU_ANY              RISCV_CPU_TYPE_NAME("any")
  #define TYPE_RISCV_CPU_BASE32           RISCV_CPU_TYPE_NAME("rv32")
  #define TYPE_RISCV_CPU_BASE64           RISCV_CPU_TYPE_NAME("rv64")
+#define TYPE_RISCV_CPU_IBEX             RISCV_CPU_TYPE_NAME("lowrisc-ibex")
  #define TYPE_RISCV_CPU_SIFIVE_E31       RISCV_CPU_TYPE_NAME("sifive-e31")
  #define TYPE_RISCV_CPU_SIFIVE_E34       RISCV_CPU_TYPE_NAME("sifive-e34")
  #define TYPE_RISCV_CPU_SIFIVE_E51       RISCV_CPU_TYPE_NAME("sifive-e51")
target/riscv/cpu.c +0 -0
index 5eb3c02735..eb2bbc87ae 100644

target/riscv/cpu.c static void rv32gcsu_priv1_10_0_cpu_init(Object *obj) · line 164

Quoted 9 lines
target/riscv/cpu.c +6 -0
@@ -156,6 +156,15 @@ static void rv32gcsu_priv1_10_0_cpu_init(Object *obj)
      set_feature(env, RISCV_FEATURE_PMP);
  }
 
+static void rv32imcu_nommu_cpu_init(Object *obj)
+{
+    CPURISCVState *env = &RISCV_CPU(obj)->env;
+    set_misa(env, RV32 | RVI | RVM | RVC | RVU);
+    set_priv_version(env, PRIV_VERSION_1_10_0);
+    set_resetvec(env, 0x8090);

Hi Alistair,

Quoted 3 lines

I see all RISC-V cpus have an reset vector which acts as the first pc when machine boots up.
However, the first pc is more like an attribute of a machine, not a cpu.

In general it seems to be a CPU property. I assume that some CPUs would allow the reset vector to be selectable though, in which case it becomes a board property.

Quoted 6 lines

Another reason is that the cpu names are a combination of ISA. Then the cpus from different vendors may have same ISA, with different reset vectors.

Do you think so?

If you are worried about CPUs with different vectors we could always make it a property in the future and have boards override it. I don't think we need that yet (only 1 CPU is different) but it is an easy future change.

Alistair

Quoted 18 lines

Zhiwei

Quoted 14 lines
target/riscv/cpu.c +4 -0
+    set_feature(env, RISCV_FEATURE_PMP);
+}
+
  static void rv32imacu_nommu_cpu_init(Object *obj)
  {
      CPURISCVState *env = &RISCV_CPU(obj)->env;
@@ -619,6 +628,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
      DEFINE_CPU(TYPE_RISCV_CPU_ANY,              riscv_any_cpu_init),
  #if defined(TARGET_RISCV32)
      DEFINE_CPU(TYPE_RISCV_CPU_BASE32,           riscv_base32_cpu_init),
+    DEFINE_CPU(TYPE_RISCV_CPU_IBEX,             rv32imcu_nommu_cpu_init),
      DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31,       rv32imacu_nommu_cpu_init),
      DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34,       rv32imafcu_nommu_cpu_init),
      DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34,       rv32gcsu_priv1_10_0_cpu_init),
Reviewed-by
#9 lore raw

Re: [PATCH v3 3/9] target/riscv: Add the lowRISC Ibex CPU

Quoted 57 lines · Alistair Francis
Quoted 45 lines · LIU Zhiwei
Quoted 31 lines · Alistair Francis

Ibex is a small and efficient, 32-bit, in-order RISC-V core with a 2-stage pipeline that implements the RV32IMC instruction set architecture.

For more details on lowRISC see here:
https://github.com/lowRISC/ibex

---
   target/riscv/cpu.h |  1 +
   target/riscv/cpu.c | 10 ++++++++++
   2 files changed, 11 insertions(+)
target/riscv/cpu.h +1 -0
index d0e7f5b9c5..8733d7467f 100644
@@ -35,6 +35,7 @@
   #define TYPE_RISCV_CPU_ANY              RISCV_CPU_TYPE_NAME("any")
   #define TYPE_RISCV_CPU_BASE32           RISCV_CPU_TYPE_NAME("rv32")
   #define TYPE_RISCV_CPU_BASE64           RISCV_CPU_TYPE_NAME("rv64")
+#define TYPE_RISCV_CPU_IBEX             RISCV_CPU_TYPE_NAME("lowrisc-ibex")
   #define TYPE_RISCV_CPU_SIFIVE_E31       RISCV_CPU_TYPE_NAME("sifive-e31")
   #define TYPE_RISCV_CPU_SIFIVE_E34       RISCV_CPU_TYPE_NAME("sifive-e34")
   #define TYPE_RISCV_CPU_SIFIVE_E51       RISCV_CPU_TYPE_NAME("sifive-e51")
target/riscv/cpu.c +0 -0
index 5eb3c02735..eb2bbc87ae 100644

target/riscv/cpu.c static void rv32gcsu_priv1_10_0_cpu_init(Object *obj) · line 164

Quoted 9 lines
target/riscv/cpu.c +6 -0
@@ -156,6 +156,15 @@ static void rv32gcsu_priv1_10_0_cpu_init(Object *obj)
       set_feature(env, RISCV_FEATURE_PMP);
   }
 
+static void rv32imcu_nommu_cpu_init(Object *obj)
+{
+    CPURISCVState *env = &RISCV_CPU(obj)->env;
+    set_misa(env, RV32 | RVI | RVM | RVC | RVU);
+    set_priv_version(env, PRIV_VERSION_1_10_0);
+    set_resetvec(env, 0x8090);

Hi Alistair,

Quoted 3 lines

I see all RISC-V cpus have an reset vector which acts as the first pc when machine boots up.
However, the first pc is more like an attribute of a machine, not a cpu.

In general it seems to be a CPU property. I assume that some CPUs would allow the reset vector to be selectable though, in which case it becomes a board property.

Quoted 4 lines

Another reason is that the cpu names are a combination of ISA. Then the cpus from different vendors may have same ISA, with different reset vectors.

Quoted 5 lines
Quoted 1 line

Do you think so?

If you are worried about CPUs with different vectors we could always make it a property in the future and have boards override it. I don't think we need that yet (only 1 CPU is different) but it is an easy future change.

I think your are right. A cpu reset vector property is better. If there is a conflict in the future, we can add the property there.

Zhiwei

Quoted 18 lines

Alistair

Quoted 16 lines

Zhiwei

Quoted 14 lines
target/riscv/cpu.c +4 -0
+    set_feature(env, RISCV_FEATURE_PMP);
+}
+
   static void rv32imacu_nommu_cpu_init(Object *obj)
   {
       CPURISCVState *env = &RISCV_CPU(obj)->env;
@@ -619,6 +628,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
       DEFINE_CPU(TYPE_RISCV_CPU_ANY,              riscv_any_cpu_init),
   #if defined(TARGET_RISCV32)
       DEFINE_CPU(TYPE_RISCV_CPU_BASE32,           riscv_base32_cpu_init),
+    DEFINE_CPU(TYPE_RISCV_CPU_IBEX,             rv32imcu_nommu_cpu_init),
       DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31,       rv32imacu_nommu_cpu_init),
       DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34,       rv32imafcu_nommu_cpu_init),
       DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34,       rv32gcsu_priv1_10_0_cpu_init),
#10 lore raw

Re: [PATCH v3 3/9] target/riscv: Add the lowRISC Ibex CPU

Quoted 66 lines · LIU Zhiwei
Quoted 57 lines · Alistair Francis
Quoted 45 lines · LIU Zhiwei
Quoted 31 lines · Alistair Francis

Ibex is a small and efficient, 32-bit, in-order RISC-V core with a 2-stage pipeline that implements the RV32IMC instruction set architecture.

For more details on lowRISC see here:
https://github.com/lowRISC/ibex

---
   target/riscv/cpu.h |  1 +
   target/riscv/cpu.c | 10 ++++++++++
   2 files changed, 11 insertions(+)
target/riscv/cpu.h +1 -0
index d0e7f5b9c5..8733d7467f 100644
@@ -35,6 +35,7 @@
   #define TYPE_RISCV_CPU_ANY              RISCV_CPU_TYPE_NAME("any")
   #define TYPE_RISCV_CPU_BASE32           RISCV_CPU_TYPE_NAME("rv32")
   #define TYPE_RISCV_CPU_BASE64           RISCV_CPU_TYPE_NAME("rv64")
+#define TYPE_RISCV_CPU_IBEX             RISCV_CPU_TYPE_NAME("lowrisc-ibex")
   #define TYPE_RISCV_CPU_SIFIVE_E31       RISCV_CPU_TYPE_NAME("sifive-e31")
   #define TYPE_RISCV_CPU_SIFIVE_E34       RISCV_CPU_TYPE_NAME("sifive-e34")
   #define TYPE_RISCV_CPU_SIFIVE_E51       RISCV_CPU_TYPE_NAME("sifive-e51")
target/riscv/cpu.c +0 -0
index 5eb3c02735..eb2bbc87ae 100644

target/riscv/cpu.c static void rv32gcsu_priv1_10_0_cpu_init(Object *obj) · line 164

Quoted 9 lines
target/riscv/cpu.c +6 -0
@@ -156,6 +156,15 @@ static void rv32gcsu_priv1_10_0_cpu_init(Object *obj)
       set_feature(env, RISCV_FEATURE_PMP);
   }
 
+static void rv32imcu_nommu_cpu_init(Object *obj)
+{
+    CPURISCVState *env = &RISCV_CPU(obj)->env;
+    set_misa(env, RV32 | RVI | RVM | RVC | RVU);
+    set_priv_version(env, PRIV_VERSION_1_10_0);
+    set_resetvec(env, 0x8090);

Hi Alistair,

Quoted 3 lines

I see all RISC-V cpus have an reset vector which acts as the first pc when machine boots up.
However, the first pc is more like an attribute of a machine, not a cpu.

In general it seems to be a CPU property. I assume that some CPUs would allow the reset vector to be selectable though, in which case it becomes a board property.

Quoted 4 lines

Another reason is that the cpu names are a combination of ISA. Then the cpus from different vendors may have same ISA, with different reset vectors.

Quoted 5 lines
Quoted 1 line

Do you think so?

If you are worried about CPUs with different vectors we could always make it a property in the future and have boards override it. I don't think we need that yet (only 1 CPU is different) but it is an easy future change.

Quoted 5 lines

I think your are right. A cpu reset vector property is better. If there is a conflict in the future,
we can add the property there.

Great! Thanks :)

Alistair

Quoted 2 lines

Zhiwei

#11 lore raw

PATCH v3 4/9 riscv: Initial commit of OpenTitan machine

In reply to Alistair Francis

This adds a barebone OpenTitan machine to QEMU.

---
 default-configs/riscv32-softmmu.mak |   1 +
 default-configs/riscv64-softmmu.mak |  11 +-
 include/hw/riscv/opentitan.h        |  63 +++++++++++
 hw/riscv/opentitan.c                | 169 ++++++++++++++++++++++++++++
 MAINTAINERS                         |   9 ++
 hw/riscv/Kconfig                    |   5 +
 hw/riscv/Makefile.objs              |   1 +
 7 files changed, 258 insertions(+), 1 deletion(-)
 create mode 100644 include/hw/riscv/opentitan.h
 create mode 100644 hw/riscv/opentitan.c
default-configs/riscv32-softmmu.mak +1 -0
index 1ae077ed87..94a236c9c2 100644
@@ -10,3 +10,4 @@ CONFIG_SPIKE=y
 CONFIG_SIFIVE_E=y
 CONFIG_SIFIVE_U=y
 CONFIG_RISCV_VIRT=y
+CONFIG_OPENTITAN=y
default-configs/riscv64-softmmu.mak +10 -1
index 235c6f473f..aaf6d735bb 100644
@@ -1,3 +1,12 @@
 # Default configuration for riscv64-softmmu
 
-include riscv32-softmmu.mak
+# Uncomment the following lines to disable these optional devices:
+#
+#CONFIG_PCI_DEVICES=n
+
+# Boards:
+#
+CONFIG_SPIKE=y
+CONFIG_SIFIVE_E=y
+CONFIG_SIFIVE_U=y
+CONFIG_RISCV_VIRT=y
include/hw/riscv/opentitan.h new file +63 -0

63 lines not on this page · raw message

hw/riscv/opentitan.c new file +169 -0

169 lines not on this page · raw message

MAINTAINERS +9 -0
index 47ef3139e6..a5f6aaa266 100644
@@ -1230,6 +1230,15 @@ F: pc-bios/canyonlands.dt[sb]
 F: pc-bios/u-boot-sam460ex-20100605.bin
 F: roms/u-boot-sam460ex
 
+RISC-V Machines
+---------------
+OpenTitan
+M: Alistair Francis <[email protected]>
+L: [email protected]
+S: Supported
+F: hw/riscv/opentitan.c
+F: include/hw/riscv/opentitan.h
+
 SH4 Machines
 ------------
 R2D
hw/riscv/Kconfig +5 -0
index ff9fbe958a..94d19571f7 100644
@@ -27,6 +27,11 @@ config SPIKE
     select HTIF
     select SIFIVE
 
+config OPENTITAN
+    bool
+    select HART
+    select UNIMP
+
 config RISCV_VIRT
     bool
     imply PCI_DEVICES
hw/riscv/Makefile.objs +1 -0
index fc3c6dd7c8..57cc708f5d 100644
@@ -1,6 +1,7 @@
 obj-y += boot.o
 obj-$(CONFIG_SPIKE) += riscv_htif.o
 obj-$(CONFIG_HART) += riscv_hart.o
+obj-$(CONFIG_OPENTITAN) += opentitan.o
 obj-$(CONFIG_SIFIVE_E) += sifive_e.o
 obj-$(CONFIG_SIFIVE_E) += sifive_e_prci.o
 obj-$(CONFIG_SIFIVE) += sifive_clint.o
Signature · 1 line
2.26.2
#12 lore raw

PATCH v3 5/9 hw/char: Initial commit of Ibex UART

In reply to Alistair Francis

This is the initial commit of the Ibex UART device. Serial TX is working, while RX has been implemeneted but untested.

This is based on the documentation from:
https://docs.opentitan.org/hw/ip/uart/doc/

---
 include/hw/char/ibex_uart.h | 110 ++++++++
 hw/char/ibex_uart.c         | 492 ++++++++++++++++++++++++++++++++++++
 MAINTAINERS                 |   2 +
 hw/char/Makefile.objs       |   1 +
 hw/riscv/Kconfig            |   4 +
 5 files changed, 609 insertions(+)
 create mode 100644 include/hw/char/ibex_uart.h
 create mode 100644 hw/char/ibex_uart.c
include/hw/char/ibex_uart.h new file +110 -0
new file mode 100644
index 0000000000..2bec772615
@@ -0,0 +1,110 @@
+/*
+ * QEMU lowRISC Ibex UART device
+ *
+ * Copyright (c) 2020 Western Digital
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef HW_IBEX_UART_H
+#define HW_IBEX_UART_H
+
+#include "hw/sysbus.h"
+#include "chardev/char-fe.h"
+#include "qemu/timer.h"
+
+#define IBEX_UART_INTR_STATE   0x00
+    #define INTR_STATE_TX_WATERMARK (1 << 0)
+    #define INTR_STATE_RX_WATERMARK (1 << 1)
+    #define INTR_STATE_TX_EMPTY     (1 << 2)
+    #define INTR_STATE_RX_OVERFLOW  (1 << 3)
+#define IBEX_UART_INTR_ENABLE  0x04
+#define IBEX_UART_INTR_TEST    0x08
+
+#define IBEX_UART_CTRL         0x0c
+    #define UART_CTRL_TX_ENABLE     (1 << 0)
+    #define UART_CTRL_RX_ENABLE     (1 << 1)
+    #define UART_CTRL_NF            (1 << 2)
+    #define UART_CTRL_SLPBK         (1 << 4)
+    #define UART_CTRL_LLPBK         (1 << 5)
+    #define UART_CTRL_PARITY_EN     (1 << 6)
+    #define UART_CTRL_PARITY_ODD    (1 << 7)
+    #define UART_CTRL_RXBLVL        (3 << 8)
+    #define UART_CTRL_NCO           (0xFFFF << 16)
+
+#define IBEX_UART_STATUS       0x10
+    #define UART_STATUS_TXFULL  (1 << 0)
+    #define UART_STATUS_RXFULL  (1 << 1)
+    #define UART_STATUS_TXEMPTY (1 << 2)
+    #define UART_STATUS_RXIDLE  (1 << 4)
+    #define UART_STATUS_RXEMPTY (1 << 5)
+
+#define IBEX_UART_RDATA        0x14
+#define IBEX_UART_WDATA        0x18
+
+#define IBEX_UART_FIFO_CTRL    0x1c
+    #define FIFO_CTRL_RXRST          (1 << 0)
+    #define FIFO_CTRL_TXRST          (1 << 1)
+    #define FIFO_CTRL_RXILVL         (7 << 2)
+    #define FIFO_CTRL_RXILVL_SHIFT   (2)
+    #define FIFO_CTRL_TXILVL         (3 << 5)
+    #define FIFO_CTRL_TXILVL_SHIFT   (5)
+
+#define IBEX_UART_FIFO_STATUS  0x20
+#define IBEX_UART_OVRD         0x24
+#define IBEX_UART_VAL          0x28
+#define IBEX_UART_TIMEOUT_CTRL 0x2c
+
+#define IBEX_UART_TX_FIFO_SIZE 16
+
+#define TYPE_IBEX_UART "ibex-uart"
+#define IBEX_UART(obj) \
+    OBJECT_CHECK(IbexUartState, (obj), TYPE_IBEX_UART)
+
+typedef struct {
+    /* <private> */
+    SysBusDevice parent_obj;
+
+    /* <public> */
+    MemoryRegion mmio;
+
+    uint8_t tx_fifo[IBEX_UART_TX_FIFO_SIZE];
+    uint32_t tx_level;
+
+    QEMUTimer *fifo_trigger_handle;
+    uint64_t char_tx_time;
+
+    uint32_t uart_intr_state;
+    uint32_t uart_intr_enable;
+    uint32_t uart_ctrl;
+    uint32_t uart_status;
+    uint32_t uart_rdata;
+    uint32_t uart_fifo_ctrl;
+    uint32_t uart_fifo_status;
+    uint32_t uart_ovrd;
+    uint32_t uart_val;
+    uint32_t uart_timeout_ctrl;
+
+    CharBackend chr;
+    qemu_irq tx_watermark;
+    qemu_irq rx_watermark;
+    qemu_irq tx_empty;
+    qemu_irq rx_overflow;
+} IbexUartState;
+#endif /* HW_IBEX_UART_H */
hw/char/ibex_uart.c new file +492 -0

492 lines not on this page · raw message

MAINTAINERS +2 -0
index a5f6aaa266..7d27a44d15 100644
@@ -1237,7 +1237,9 @@ M: Alistair Francis <[email protected]>
 L: [email protected]
 S: Supported
 F: hw/riscv/opentitan.c
+F: hw/char/ibex_uart.c
 F: include/hw/riscv/opentitan.h
+F: include/hw/char/ibex_uart.h
 
 SH4 Machines
 ------------
hw/char/Makefile.objs +1 -0
index 9e9a6c1aff..633996be5b 100644
@@ -12,6 +12,7 @@ common-obj-$(CONFIG_VIRTIO_SERIAL) += virtio-console.o
 common-obj-$(CONFIG_XILINX) += xilinx_uartlite.o
 common-obj-$(CONFIG_XEN) += xen_console.o
 common-obj-$(CONFIG_CADENCE) += cadence_uart.o
+common-obj-$(CONFIG_IBEX) += ibex_uart.o
 
 common-obj-$(CONFIG_EXYNOS4) += exynos4210_uart.o
 common-obj-$(CONFIG_COLDFIRE) += mcf_uart.o
hw/riscv/Kconfig +4 -0
index 94d19571f7..28947ef3e0 100644
@@ -4,6 +4,9 @@ config HTIF
 config HART
     bool
 
+config IBEX
+    bool
+
 config SIFIVE
     bool
     select MSI_NONBROKEN
@@ -29,6 +32,7 @@ config SPIKE
 
 config OPENTITAN
     bool
+    select IBEX
     select HART
     select UNIMP
 
Signature · 1 line
2.26.2
#13 lore raw

PATCH v3 6/9 hw/intc: Initial commit of lowRISC Ibex PLIC

In reply to Alistair Francis

The Ibex core contains a PLIC that although similar to the RISC-V spec is not RISC-V spec compliant.

This patch implements a Ibex PLIC in a somewhat generic way.

As the current RISC-V PLIC needs tidying up, my hope is that as the Ibex PLIC move towards spec compliance this PLIC implementation can be updated until it can replace the current PLIC.

---
 include/hw/intc/ibex_plic.h |  63 +++++++++
 hw/intc/ibex_plic.c         | 261 ++++++++++++++++++++++++++++++++++++
 MAINTAINERS                 |   2 +
 hw/intc/Makefile.objs       |   1 +
 4 files changed, 327 insertions(+)
 create mode 100644 include/hw/intc/ibex_plic.h
 create mode 100644 hw/intc/ibex_plic.c
include/hw/intc/ibex_plic.h new file +63 -0
new file mode 100644
index 0000000000..ddc7909903
@@ -0,0 +1,63 @@
+/*
+ * QEMU RISC-V lowRISC Ibex PLIC
+ *
+ * Copyright (c) 2020 Western Digital
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2 or later, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef HW_IBEX_PLIC_H
+#define HW_IBEX_PLIC_H
+
+#include "hw/sysbus.h"
+
+#define TYPE_IBEX_PLIC "ibex-plic"
+#define IBEX_PLIC(obj) \
+    OBJECT_CHECK(IbexPlicState, (obj), TYPE_IBEX_PLIC)
+
+typedef struct IbexPlicState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+
+    /*< public >*/
+    MemoryRegion mmio;
+
+    uint32_t *pending;
+    uint32_t *source;
+    uint32_t *priority;
+    uint32_t *enable;
+    uint32_t threshold;
+    uint32_t claim;
+
+    /* config */
+    uint32_t num_cpus;
+    uint32_t num_sources;
+
+    uint32_t pending_base;
+    uint32_t pending_num;
+
+    uint32_t source_base;
+    uint32_t source_num;
+
+    uint32_t priority_base;
+    uint32_t priority_num;
+
+    uint32_t enable_base;
+    uint32_t enable_num;
+
+    uint32_t threshold_base;
+
+    uint32_t claim_base;
+} IbexPlicState;
+
+#endif /* HW_IBEX_PLIC_H */
hw/intc/ibex_plic.c new file +261 -0

261 lines not on this page · raw message

MAINTAINERS +2 -0
index 7d27a44d15..34b7c1745d 100644
@@ -1238,8 +1238,10 @@ L: [email protected]
 S: Supported
 F: hw/riscv/opentitan.c
 F: hw/char/ibex_uart.c
+F: hw/intc/ibex_plic.c
 F: include/hw/riscv/opentitan.h
 F: include/hw/char/ibex_uart.h
+F: include/hw/intc/ibex_plic.h
 
 SH4 Machines
 ------------
hw/intc/Makefile.objs +1 -0
index f726d87532..a61e6728fe 100644
@@ -49,3 +49,4 @@ obj-$(CONFIG_ARM_GIC) += arm_gicv3_cpuif.o
 obj-$(CONFIG_MIPS_CPS) += mips_gic.o
 obj-$(CONFIG_NIOS2) += nios2_iic.o
 obj-$(CONFIG_OMPIC) += ompic.o
+obj-$(CONFIG_IBEX) += ibex_plic.o
Signature · 1 line
2.26.2
#14 lore raw

PATCH v3 7/9 riscv/opentitan: Connect the PLIC device

In reply to Alistair Francis
---
 include/hw/riscv/opentitan.h |  3 +++
 hw/riscv/opentitan.c         | 19 +++++++++++++++++--
 2 files changed, 20 insertions(+), 2 deletions(-)
include/hw/riscv/opentitan.h +3 -0
index 15a3d87ed0..8d6a09b696 100644
@@ -20,6 +20,7 @@
 #define HW_OPENTITAN_H
 
 #include "hw/riscv/riscv_hart.h"
+#include "hw/intc/ibex_plic.h"
 
 #define TYPE_RISCV_IBEX_SOC "riscv.lowrisc.ibex.soc"
 #define RISCV_IBEX_SOC(obj) \
@@ -31,6 +32,8 @@ typedef struct LowRISCIbexSoCState {
 
     /*< public >*/
     RISCVHartArrayState cpus;
+    IbexPlicState plic;
+
     MemoryRegion flash_mem;
     MemoryRegion rom;
 } LowRISCIbexSoCState;
hw/riscv/opentitan.c +17 -2
index c00f0720ab..3926321d8c 100644
@@ -25,6 +25,7 @@
 #include "hw/misc/unimp.h"
 #include "hw/riscv/boot.h"
 #include "exec/address-spaces.h"
+#include "sysemu/sysemu.h"
 
 static const struct MemmapEntry {
     hwaddr base;
@@ -92,6 +93,9 @@ static void riscv_lowrisc_ibex_soc_init(Object *obj)
     object_initialize_child(obj, "cpus", &s->cpus,
                             sizeof(s->cpus), TYPE_RISCV_HART_ARRAY,
                             &error_abort, NULL);
+
+    sysbus_init_child_obj(obj, "plic", &s->plic,
+                          sizeof(s->plic), TYPE_IBEX_PLIC);
 }
 
 static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
@@ -100,6 +104,9 @@ static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
     MachineState *ms = MACHINE(qdev_get_machine());
     LowRISCIbexSoCState *s = RISCV_IBEX_SOC(dev_soc);
     MemoryRegion *sys_mem = get_system_memory();
+    DeviceState *dev;
+    SysBusDevice *busdev;
+    Error *err = NULL;
 
     object_property_set_str(OBJECT(&s->cpus), ms->cpu_type, "cpu-type",
                             &error_abort);
@@ -120,6 +127,16 @@ static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
     memory_region_add_subregion(sys_mem, memmap[IBEX_FLASH].base,
                                 &s->flash_mem);
 
+    /* PLIC */
+    dev = DEVICE(&s->plic);
+    object_property_set_bool(OBJECT(&s->plic), true, "realized", &err);
+    if (err != NULL) {
+        error_propagate(errp, err);
+        return;
+    }
+    busdev = SYS_BUS_DEVICE(dev);
+    sysbus_mmio_map(busdev, 0, memmap[IBEX_PLIC].base);
+
     create_unimplemented_device("riscv.lowrisc.ibex.uart",
         memmap[IBEX_UART].base, memmap[IBEX_UART].size);
     create_unimplemented_device("riscv.lowrisc.ibex.gpio",
@@ -134,8 +151,6 @@ static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
         memmap[IBEX_AES].base, memmap[IBEX_AES].size);
     create_unimplemented_device("riscv.lowrisc.ibex.hmac",
         memmap[IBEX_HMAC].base, memmap[IBEX_HMAC].size);
-    create_unimplemented_device("riscv.lowrisc.ibex.plic",
-        memmap[IBEX_PLIC].base, memmap[IBEX_PLIC].size);
     create_unimplemented_device("riscv.lowrisc.ibex.pinmux",
         memmap[IBEX_PINMUX].base, memmap[IBEX_PINMUX].size);
     create_unimplemented_device("riscv.lowrisc.ibex.alert_handler",
Signature · 1 line
2.26.2
Reviewed-by on v3 7/9
#15 lore raw

Re: [PATCH v3 7/9] riscv/opentitan: Connect the PLIC device

Quoted 79 lines · Alistair Francis
---
  include/hw/riscv/opentitan.h |  3 +++
  hw/riscv/opentitan.c         | 19 +++++++++++++++++--
  2 files changed, 20 insertions(+), 2 deletions(-)
include/hw/riscv/opentitan.h +3 -0
index 15a3d87ed0..8d6a09b696 100644
@@ -20,6 +20,7 @@
  #define HW_OPENTITAN_H
 
  #include "hw/riscv/riscv_hart.h"
+#include "hw/intc/ibex_plic.h"
 
  #define TYPE_RISCV_IBEX_SOC "riscv.lowrisc.ibex.soc"
  #define RISCV_IBEX_SOC(obj) \
@@ -31,6 +32,8 @@ typedef struct LowRISCIbexSoCState {
 
      /*< public >*/
      RISCVHartArrayState cpus;
+    IbexPlicState plic;
+
      MemoryRegion flash_mem;
      MemoryRegion rom;
  } LowRISCIbexSoCState;
hw/riscv/opentitan.c +17 -0
index c00f0720ab..3926321d8c 100644
@@ -25,6 +25,7 @@
  #include "hw/misc/unimp.h"
  #include "hw/riscv/boot.h"
  #include "exec/address-spaces.h"
+#include "sysemu/sysemu.h"
 
  static const struct MemmapEntry {
      hwaddr base;
@@ -92,6 +93,9 @@ static void riscv_lowrisc_ibex_soc_init(Object *obj)
      object_initialize_child(obj, "cpus", &s->cpus,
                              sizeof(s->cpus), TYPE_RISCV_HART_ARRAY,
                              &error_abort, NULL);
+
+    sysbus_init_child_obj(obj, "plic", &s->plic,
+                          sizeof(s->plic), TYPE_IBEX_PLIC);
  }
 
  static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
@@ -100,6 +104,9 @@ static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
      MachineState *ms = MACHINE(qdev_get_machine());
      LowRISCIbexSoCState *s = RISCV_IBEX_SOC(dev_soc);
      MemoryRegion *sys_mem = get_system_memory();
+    DeviceState *dev;
+    SysBusDevice *busdev;
+    Error *err = NULL;
 
      object_property_set_str(OBJECT(&s->cpus), ms->cpu_type, "cpu-type",
                              &error_abort);
@@ -120,6 +127,16 @@ static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
      memory_region_add_subregion(sys_mem, memmap[IBEX_FLASH].base,
                                  &s->flash_mem);
 
+    /* PLIC */
+    dev = DEVICE(&s->plic);
+    object_property_set_bool(OBJECT(&s->plic), true, "realized", &err);
+    if (err != NULL) {
+        error_propagate(errp, err);
+        return;
+    }
+    busdev = SYS_BUS_DEVICE(dev);
+    sysbus_mmio_map(busdev, 0, memmap[IBEX_PLIC].base);
+
      create_unimplemented_device("riscv.lowrisc.ibex.uart",
          memmap[IBEX_UART].base, memmap[IBEX_UART].size);
      create_unimplemented_device("riscv.lowrisc.ibex.gpio",

hw/riscv/opentitan.c static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp) · line 156

Quoted 8 lines
hw/riscv/opentitan.c +0 -2
@@ -134,8 +151,6 @@ static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
          memmap[IBEX_AES].base, memmap[IBEX_AES].size);
      create_unimplemented_device("riscv.lowrisc.ibex.hmac",
          memmap[IBEX_HMAC].base, memmap[IBEX_HMAC].size);
-    create_unimplemented_device("riscv.lowrisc.ibex.plic",
-        memmap[IBEX_PLIC].base, memmap[IBEX_PLIC].size);
      create_unimplemented_device("riscv.lowrisc.ibex.pinmux",
          memmap[IBEX_PINMUX].base, memmap[IBEX_PINMUX].size);
      create_unimplemented_device("riscv.lowrisc.ibex.alert_handler",
#16 lore raw

PATCH v3 8/9 riscv/opentitan: Connect the UART device

In reply to Alistair Francis
---
 include/hw/riscv/opentitan.h | 13 +++++++++++++
 hw/riscv/opentitan.c         | 24 ++++++++++++++++++++++--
 2 files changed, 35 insertions(+), 2 deletions(-)
include/hw/riscv/opentitan.h +13 -0
index 8d6a09b696..825a3610bc 100644
@@ -21,6 +21,7 @@
 
 #include "hw/riscv/riscv_hart.h"
 #include "hw/intc/ibex_plic.h"
+#include "hw/char/ibex_uart.h"
 
 #define TYPE_RISCV_IBEX_SOC "riscv.lowrisc.ibex.soc"
 #define RISCV_IBEX_SOC(obj) \
@@ -33,6 +34,7 @@ typedef struct LowRISCIbexSoCState {
     /*< public >*/
     RISCVHartArrayState cpus;
     IbexPlicState plic;
+    IbexUartState uart;
 
     MemoryRegion flash_mem;
     MemoryRegion rom;
@@ -63,4 +65,15 @@ enum {
     IBEX_USBDEV,
 };
 
+enum {
+    IBEX_UART_RX_PARITY_ERR_IRQ = 0x28,
+    IBEX_UART_RX_TIMEOUT_IRQ = 0x27,
+    IBEX_UART_RX_BREAK_ERR_IRQ = 0x26,
+    IBEX_UART_RX_FRAME_ERR_IRQ = 0x25,
+    IBEX_UART_RX_OVERFLOW_IRQ = 0x24,
+    IBEX_UART_TX_EMPTY_IRQ = 0x23,
+    IBEX_UART_RX_WATERMARK_IRQ = 0x22,
+    IBEX_UART_TX_WATERMARK_IRQ = 0x21
+};
+
 #endif
hw/riscv/opentitan.c +22 -2
index 3926321d8c..a6c0b949ca 100644
@@ -96,6 +96,9 @@ static void riscv_lowrisc_ibex_soc_init(Object *obj)
 
     sysbus_init_child_obj(obj, "plic", &s->plic,
                           sizeof(s->plic), TYPE_IBEX_PLIC);
+
+    sysbus_init_child_obj(obj, "uart", &s->uart,
+                          sizeof(s->uart), TYPE_IBEX_UART);
 }
 
 static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
@@ -137,8 +140,25 @@ static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
     busdev = SYS_BUS_DEVICE(dev);
     sysbus_mmio_map(busdev, 0, memmap[IBEX_PLIC].base);
 
-    create_unimplemented_device("riscv.lowrisc.ibex.uart",
-        memmap[IBEX_UART].base, memmap[IBEX_UART].size);
+    /* UART */
+    dev = DEVICE(&(s->uart));
+    qdev_prop_set_chr(dev, "chardev", serial_hd(0));
+    object_property_set_bool(OBJECT(&s->uart), true, "realized", &err);
+    if (err != NULL) {
+        error_propagate(errp, err);
+        return;
+    }
+    busdev = SYS_BUS_DEVICE(dev);
+    sysbus_mmio_map(busdev, 0, memmap[IBEX_UART].base);
+    sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(DEVICE(&s->plic),
+                       IBEX_UART_TX_WATERMARK_IRQ));
+    sysbus_connect_irq(busdev, 1, qdev_get_gpio_in(DEVICE(&s->plic),
+                       IBEX_UART_RX_WATERMARK_IRQ));
+    sysbus_connect_irq(busdev, 2, qdev_get_gpio_in(DEVICE(&s->plic),
+                       IBEX_UART_TX_EMPTY_IRQ));
+    sysbus_connect_irq(busdev, 3, qdev_get_gpio_in(DEVICE(&s->plic),
+                       IBEX_UART_RX_OVERFLOW_IRQ));
+
     create_unimplemented_device("riscv.lowrisc.ibex.gpio",
         memmap[IBEX_GPIO].base, memmap[IBEX_GPIO].size);
     create_unimplemented_device("riscv.lowrisc.ibex.spi",
Signature · 1 line
2.26.2
Reviewed-by on v3 8/9
#17 lore raw

Re: [PATCH v3 8/9] riscv/opentitan: Connect the UART device

Quoted 76 lines · Alistair Francis
---
  include/hw/riscv/opentitan.h | 13 +++++++++++++
  hw/riscv/opentitan.c         | 24 ++++++++++++++++++++++--
  2 files changed, 35 insertions(+), 2 deletions(-)
include/hw/riscv/opentitan.h +13 -0
index 8d6a09b696..825a3610bc 100644
@@ -21,6 +21,7 @@
 
  #include "hw/riscv/riscv_hart.h"
  #include "hw/intc/ibex_plic.h"
+#include "hw/char/ibex_uart.h"
 
  #define TYPE_RISCV_IBEX_SOC "riscv.lowrisc.ibex.soc"
  #define RISCV_IBEX_SOC(obj) \
@@ -33,6 +34,7 @@ typedef struct LowRISCIbexSoCState {
      /*< public >*/
      RISCVHartArrayState cpus;
      IbexPlicState plic;
+    IbexUartState uart;
 
      MemoryRegion flash_mem;
      MemoryRegion rom;
@@ -63,4 +65,15 @@ enum {
      IBEX_USBDEV,
  };
 
+enum {
+    IBEX_UART_RX_PARITY_ERR_IRQ = 0x28,
+    IBEX_UART_RX_TIMEOUT_IRQ = 0x27,
+    IBEX_UART_RX_BREAK_ERR_IRQ = 0x26,
+    IBEX_UART_RX_FRAME_ERR_IRQ = 0x25,
+    IBEX_UART_RX_OVERFLOW_IRQ = 0x24,
+    IBEX_UART_TX_EMPTY_IRQ = 0x23,
+    IBEX_UART_RX_WATERMARK_IRQ = 0x22,
+    IBEX_UART_TX_WATERMARK_IRQ = 0x21
+};
+
  #endif
hw/riscv/opentitan.c +15 -2
index 3926321d8c..a6c0b949ca 100644
@@ -96,6 +96,9 @@ static void riscv_lowrisc_ibex_soc_init(Object *obj)
 
      sysbus_init_child_obj(obj, "plic", &s->plic,
                            sizeof(s->plic), TYPE_IBEX_PLIC);
+
+    sysbus_init_child_obj(obj, "uart", &s->uart,
+                          sizeof(s->uart), TYPE_IBEX_UART);
  }
 
  static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
@@ -137,8 +140,25 @@ static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp)
      busdev = SYS_BUS_DEVICE(dev);
      sysbus_mmio_map(busdev, 0, memmap[IBEX_PLIC].base);
 
-    create_unimplemented_device("riscv.lowrisc.ibex.uart",
-        memmap[IBEX_UART].base, memmap[IBEX_UART].size);
+    /* UART */
+    dev = DEVICE(&(s->uart));
+    qdev_prop_set_chr(dev, "chardev", serial_hd(0));
+    object_property_set_bool(OBJECT(&s->uart), true, "realized", &err);
+    if (err != NULL) {
+        error_propagate(errp, err);
+        return;
+    }
+    busdev = SYS_BUS_DEVICE(dev);
+    sysbus_mmio_map(busdev, 0, memmap[IBEX_UART].base);
+    sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(DEVICE(&s->plic),
+                       IBEX_UART_TX_WATERMARK_IRQ));

hw/riscv/opentitan.c static void riscv_lowrisc_ibex_soc_realize(DeviceState *dev_soc, Error **errp) · line 164

Quoted 10 lines
hw/riscv/opentitan.c +7 -0
+    sysbus_connect_irq(busdev, 1, qdev_get_gpio_in(DEVICE(&s->plic),
+                       IBEX_UART_RX_WATERMARK_IRQ));
+    sysbus_connect_irq(busdev, 2, qdev_get_gpio_in(DEVICE(&s->plic),
+                       IBEX_UART_TX_EMPTY_IRQ));
+    sysbus_connect_irq(busdev, 3, qdev_get_gpio_in(DEVICE(&s->plic),
+                       IBEX_UART_RX_OVERFLOW_IRQ));
+
      create_unimplemented_device("riscv.lowrisc.ibex.gpio",
          memmap[IBEX_GPIO].base, memmap[IBEX_GPIO].size);
      create_unimplemented_device("riscv.lowrisc.ibex.spi",
#18 lore raw

PATCH v3 9/9 target/riscv: Use a smaller guess size for no-MMU PMP

In reply to Alistair Francis
---
 target/riscv/pmp.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
target/riscv/pmp.c +9 -5
index 0e6b640fbd..607a991260 100644
@@ -233,12 +233,16 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
         return true;
     }
 
-    /*
-     * if size is unknown (0), assume that all bytes
-     * from addr to the end of the page will be accessed.
-     */
     if (size == 0) {
-        pmp_size = -(addr | TARGET_PAGE_MASK);
+        if (!riscv_feature(env, RISCV_FEATURE_MMU)) {
+            /*
+             * If size is unknown (0), assume that all bytes
+             * from addr to the end of the page will be accessed.
+             */
+            pmp_size = -(addr | TARGET_PAGE_MASK);
+        } else {
+            pmp_size = sizeof(target_ulong);
+        }
     } else {
         pmp_size = size;
     }
Signature · 1 line
2.26.2
#19 lore raw

Re: [PATCH v3 9/9] target/riscv: Use a smaller guess size for no-MMU PMP

Quoted 11 lines · Alistair Francis
---
 target/riscv/pmp.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
target/riscv/pmp.c +0 -0
index 0e6b640fbd..607a991260 100644

target/riscv/pmp.c bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, · line 237

Quoted 10 lines
target/riscv/pmp.c +1 -5
@@ -233,12 +233,16 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
         return true;
     }
 
-    /*
-     * if size is unknown (0), assume that all bytes
-     * from addr to the end of the page will be accessed.
-     */
     if (size == 0) {
-        pmp_size = -(addr | TARGET_PAGE_MASK);
+        if (!riscv_feature(env, RISCV_FEATURE_MMU)) {

My previous comments were not fully addressed. I think the logic should be:

if (riscv_feature(env, RISCV_FEATURE_MMU))

Otherwise it does not match your comment and the commit title.

target/riscv/pmp.c bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, · line 248

Quoted 11 lines
target/riscv/pmp.c +8 -0
+            /*
+             * If size is unknown (0), assume that all bytes
+             * from addr to the end of the page will be accessed.
+             */
+            pmp_size = -(addr | TARGET_PAGE_MASK);
+        } else {
+            pmp_size = sizeof(target_ulong);
+        }
     } else {
         pmp_size = size;
     }

Regards,
Bin

#20 lore raw

Re: [PATCH v3 9/9] target/riscv: Use a smaller guess size for no-MMU PMP

Quoted 27 lines · Bin Meng
Quoted 11 lines · Alistair Francis
---
 target/riscv/pmp.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
target/riscv/pmp.c +0 -0
index 0e6b640fbd..607a991260 100644

target/riscv/pmp.c bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, · line 237

Quoted 10 lines
target/riscv/pmp.c +1 -5
@@ -233,12 +233,16 @@ bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
         return true;
     }
 
-    /*
-     * if size is unknown (0), assume that all bytes
-     * from addr to the end of the page will be accessed.
-     */
     if (size == 0) {
-        pmp_size = -(addr | TARGET_PAGE_MASK);
+        if (!riscv_feature(env, RISCV_FEATURE_MMU)) {

My previous comments were not fully addressed. I think the logic should be:

Quoted 3 lines

if (riscv_feature(env, RISCV_FEATURE_MMU))

Otherwise it does not match your comment and the commit title.

Ah! You are right. This uncovered a bug with how we set the MMU as well. I have fixed this and added a new patch.

Alistair

Quoted 15 lines

target/riscv/pmp.c bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, · line 248

Quoted 12 lines
target/riscv/pmp.c +8 -0
+            /*
+             * If size is unknown (0), assume that all bytes
+             * from addr to the end of the page will be accessed.
+             */
+            pmp_size = -(addr | TARGET_PAGE_MASK);
+        } else {
+            pmp_size = sizeof(target_ulong);
+        }
     } else {
         pmp_size = size;
     }

Regards,
Bin