Skip to contentSkip to conversation

target/riscv: Add isa extenstion strings to the device tree

PATCH Superseded Atish Patra 4 participants 4 messages Feb 16, 2022 – Feb 22, 2022 RISC-V

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 target/riscv: Add isa extenstion strings to the device tree

The Linux kernel parses the ISA extensions from "riscv,isa" DT property. It used to parse only the single letter base extensions until now. A generic ISA extension parsing framework was proposed[1] recently that can parse multi-letter ISA extensions as well.

Generate the extended ISA string by appending the available ISA extensions to the "riscv,isa" string if it is enabled so that kernel can process it.

[1] https://lkml.org/lkml/2022/2/15/263

---
Changes from v1->v2:
1. Improved the code redability by using arrays instead of individual check

---
 target/riscv/cpu.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
target/riscv/cpu.c +34 -1
index b0a40b83e7a8..9bf8923f164b 100644
@@ -34,6 +34,13 @@
 
 /* RISC-V CPU definitions */
 
+/* This includes the null terminated character '\0' */
+#define MAX_ISA_EXT_LEN 256
+struct isa_ext_data {
+        const char *name;
+        bool enabled;
+};
+
 static const char riscv_exts[26] = "IEMAFDQCLBJTPVNSUHKORWXYZG";
 
 const char * const riscv_int_regnames[] = {
@@ -881,10 +888,35 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data)
     device_class_set_props(dc, riscv_cpu_properties);
 }
 
+static void riscv_isa_string_ext(RISCVCPU *cpu, char *isa_str, int max_str_len)
+{
+    int offset = strnlen(isa_str, max_str_len);
+    int i;
+    struct isa_ext_data isa_edata_arr[] = {
+        { "svpbmt", cpu->cfg.ext_svpbmt   },
+        { "svinval", cpu->cfg.ext_svinval },
+        { "svnapot", cpu->cfg.ext_svnapot },
+    };
+
+    for (i = 0; i < ARRAY_SIZE(isa_edata_arr); i++) {
+        if (!isa_edata_arr[i].enabled) {
+            continue;
+        }
+        /* check available space */
+        if ((offset + strlen(isa_edata_arr[i].name) + 1) > max_str_len) {
+            qemu_log("No space left to append isa extension");
+            return;
+        }
+        offset += snprintf(isa_str + offset, max_str_len, "_%s",
+                           isa_edata_arr[i].name);
+    }
+}
+
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) +
+                          MAX_ISA_EXT_LEN;
     char *isa_str = g_new(char, maxlen);
     char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
     for (i = 0; i < sizeof(riscv_exts); i++) {
@@ -893,6 +925,7 @@ char *riscv_isa_string(RISCVCPU *cpu)
         }
     }
     *p = '\0';
+    riscv_isa_string_ext(cpu, isa_str, maxlen);
     return isa_str;
 }
 
Signature · 1 line
2.30.2
#1 lore raw
Quoted 53 lines · Atish Patra

The Linux kernel parses the ISA extensions from "riscv,isa" DT property. It used to parse only the single letter base extensions until now. A generic ISA extension parsing framework was proposed[1] recently that can parse multi-letter ISA extensions as well.

Generate the extended ISA string by appending the available ISA extensions to the "riscv,isa" string if it is enabled so that kernel can process it.

[1] https://lkml.org/lkml/2022/2/15/263

---
Changes from v1->v2:
1. Improved the code redability by using arrays instead of individual check

---
 target/riscv/cpu.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
target/riscv/cpu.c +18 -0
index b0a40b83e7a8..9bf8923f164b 100644
@@ -34,6 +34,13 @@
 
 /* RISC-V CPU definitions */
 
+/* This includes the null terminated character '\0' */
+#define MAX_ISA_EXT_LEN 256
+struct isa_ext_data {
+        const char *name;
+        bool enabled;
+};
+
 static const char riscv_exts[26] = "IEMAFDQCLBJTPVNSUHKORWXYZG";
 
 const char * const riscv_int_regnames[] = {
@@ -881,10 +888,35 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data)
     device_class_set_props(dc, riscv_cpu_properties);
 }
 
+static void riscv_isa_string_ext(RISCVCPU *cpu, char *isa_str, int max_str_len)
+{
+    int offset = strnlen(isa_str, max_str_len);
+    int i;
+    struct isa_ext_data isa_edata_arr[] = {
+        { "svpbmt", cpu->cfg.ext_svpbmt   },
+        { "svinval", cpu->cfg.ext_svinval },
+        { "svnapot", cpu->cfg.ext_svnapot },
+    };
+
+    for (i = 0; i < ARRAY_SIZE(isa_edata_arr); i++) {

target/riscv/cpu.c static void riscv_cpu_class_init(ObjectClass *c, void *data) · line 911

Quoted 10 lines
target/riscv/cpu.c +10 -0
+        if (!isa_edata_arr[i].enabled) {
+            continue;
+        }
+        /* check available space */
+        if ((offset + strlen(isa_edata_arr[i].name) + 1) > max_str_len) {
+            qemu_log("No space left to append isa extension");
+            return;
+        }
+        offset += snprintf(isa_str + offset, max_str_len, "_%s",
+                           isa_edata_arr[i].name);

You don't need to use snprintf() and MAX_ISA_EXT_LEN if you use g_strconcat() for creating new concat strings and freeing original string using g_free().

Regards,
Anup

Quoted 24 lines
target/riscv/cpu.c +6 -2
+    }
+}
+
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) +
+                          MAX_ISA_EXT_LEN;
     char *isa_str = g_new(char, maxlen);
     char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
     for (i = 0; i < sizeof(riscv_exts); i++) {
@@ -893,6 +925,7 @@ char *riscv_isa_string(RISCVCPU *cpu)
         }
     }
     *p = '\0';
+    riscv_isa_string_ext(cpu, isa_str, maxlen);
     return isa_str;
 }
 
--

2.30.2

#2 lore raw
Quoted 61 lines · Anup Patel
Quoted 59 lines · Atish Patra

The Linux kernel parses the ISA extensions from "riscv,isa" DT property. It used to parse only the single letter base extensions until now. A generic ISA extension parsing framework was proposed[1] recently that can parse multi-letter ISA extensions as well.

Generate the extended ISA string by appending the available ISA extensions to the "riscv,isa" string if it is enabled so that kernel can process it.

[1] https://lkml.org/lkml/2022/2/15/263

---
Changes from v1->v2:
1. Improved the code redability by using arrays instead of individual check

---
 target/riscv/cpu.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
target/riscv/cpu.c +24 -0
index b0a40b83e7a8..9bf8923f164b 100644
@@ -34,6 +34,13 @@
 
 /* RISC-V CPU definitions */
 
+/* This includes the null terminated character '\0' */
+#define MAX_ISA_EXT_LEN 256
+struct isa_ext_data {
+        const char *name;
+        bool enabled;
+};
+
 static const char riscv_exts[26] = "IEMAFDQCLBJTPVNSUHKORWXYZG";
 
 const char * const riscv_int_regnames[] = {
@@ -881,10 +888,35 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data)
     device_class_set_props(dc, riscv_cpu_properties);
 }
 
+static void riscv_isa_string_ext(RISCVCPU *cpu, char *isa_str, int max_str_len)
+{
+    int offset = strnlen(isa_str, max_str_len);
+    int i;
+    struct isa_ext_data isa_edata_arr[] = {
+        { "svpbmt", cpu->cfg.ext_svpbmt   },
+        { "svinval", cpu->cfg.ext_svinval },
+        { "svnapot", cpu->cfg.ext_svnapot },
+    };
+
+    for (i = 0; i < ARRAY_SIZE(isa_edata_arr); i++) {
+        if (!isa_edata_arr[i].enabled) {
+            continue;
+        }
+        /* check available space */
+        if ((offset + strlen(isa_edata_arr[i].name) + 1) > max_str_len) {
+            qemu_log("No space left to append isa extension");
Quoted 9 lines

target/riscv/cpu.c static void riscv_cpu_class_init(ObjectClass *c, void *data) · line 911

Quoted 4 lines
target/riscv/cpu.c +4 -0
+            return;
+        }
+        offset += snprintf(isa_str + offset, max_str_len, "_%s",
+                           isa_edata_arr[i].name);

You don't need to use snprintf() and MAX_ISA_EXT_LEN if you use g_strconcat() for creating new concat strings and freeing original string using g_free().

Yeah. That is better. I have reimplemented this part in v3.

Quoted 28 lines

Regards,
Anup

Quoted 24 lines
target/riscv/cpu.c +6 -2
+    }
+}
+
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) +
+                          MAX_ISA_EXT_LEN;
     char *isa_str = g_new(char, maxlen);
     char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
     for (i = 0; i < sizeof(riscv_exts); i++) {
@@ -893,6 +925,7 @@ char *riscv_isa_string(RISCVCPU *cpu)
         }
     }
     *p = '\0';
+    riscv_isa_string_ext(cpu, isa_str, maxlen);
     return isa_str;
 }
 
--

2.30.2

Signature · 2 lines
Regards,
Atish
Tested-by on v2 1/1
#3 lore raw
In reply to Atish Patra

Am Mittwoch, 16. Februar 2022, 01:09:04 CET schrieb Atish Patra:

Quoted 5 lines

The Linux kernel parses the ISA extensions from "riscv,isa" DT property. It used to parse only the single letter base extensions until now. A generic ISA extension parsing framework was proposed[1] recently that can parse multi-letter ISA extensions as well.

Quoted 7 lines

Generate the extended ISA string by appending the available ISA extensions to the "riscv,isa" string if it is enabled so that kernel can process it.

[1] https://lkml.org/lkml/2022/2/15/263

Quoted 71 lines

---
Changes from v1->v2:
1. Improved the code redability by using arrays instead of individual check

---
 target/riscv/cpu.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
target/riscv/cpu.c +34 -1
index b0a40b83e7a8..9bf8923f164b 100644
@@ -34,6 +34,13 @@
 
 /* RISC-V CPU definitions */
 
+/* This includes the null terminated character '\0' */
+#define MAX_ISA_EXT_LEN 256
+struct isa_ext_data {
+        const char *name;
+        bool enabled;
+};
+
 static const char riscv_exts[26] = "IEMAFDQCLBJTPVNSUHKORWXYZG";
 
 const char * const riscv_int_regnames[] = {
@@ -881,10 +888,35 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data)
     device_class_set_props(dc, riscv_cpu_properties);
 }
 
+static void riscv_isa_string_ext(RISCVCPU *cpu, char *isa_str, int max_str_len)
+{
+    int offset = strnlen(isa_str, max_str_len);
+    int i;
+    struct isa_ext_data isa_edata_arr[] = {
+        { "svpbmt", cpu->cfg.ext_svpbmt   },
+        { "svinval", cpu->cfg.ext_svinval },
+        { "svnapot", cpu->cfg.ext_svnapot },
+    };
+
+    for (i = 0; i < ARRAY_SIZE(isa_edata_arr); i++) {
+        if (!isa_edata_arr[i].enabled) {
+            continue;
+        }
+        /* check available space */
+        if ((offset + strlen(isa_edata_arr[i].name) + 1) > max_str_len) {
+            qemu_log("No space left to append isa extension");
+            return;
+        }
+        offset += snprintf(isa_str + offset, max_str_len, "_%s",
+                           isa_edata_arr[i].name);
+    }
+}
+
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) +
+                          MAX_ISA_EXT_LEN;
     char *isa_str = g_new(char, maxlen);
     char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
     for (i = 0; i < sizeof(riscv_exts); i++) {
@@ -893,6 +925,7 @@ char *riscv_isa_string(RISCVCPU *cpu)
         }
     }
     *p = '\0';
+    riscv_isa_string_ext(cpu, isa_str, maxlen);
     return isa_str;
 }