Skip to contentSkip to conversation

lib: utils: fdt_helper: simplify fdt_parse_isa_extensions() helper

PATCH In review Yu-Chien Peter Lin 3 participants 9 messages Sep 6, 2025 – Sep 12, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 lib: utils: fdt_helper: simplify fdt_parse_isa_extensions() helper

The fdt_isa_bitmap was previously used as a temporary array to store ISA extensions when parsing device-tree. To reduce scratch space consumption, set the ISA bitmap directly in hfeature->extensions, allows us to remove the fdt_parse_isa_all_harts() as fdt_parse_isa_extensions() can now handle the task directly.

This change makes the helper function more versatile, even before scratch space is initialized.

---
 include/sbi_utils/fdt/fdt_helper.h |  3 +-
 lib/utils/fdt/fdt_helper.c         | 48 ++++--------------------------
 platform/generic/platform.c        |  2 +-
 3 files changed, 7 insertions(+), 46 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +1 -2
index 04c850cc..24ad1a94 100644
@@ -54,8 +54,7 @@ int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *c
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-			     unsigned long *extensions);
+int fdt_parse_isa_extensions(const void *fdt, unsigned long *hart_exts);
 
 int fdt_parse_gaisler_uart_node(const void *fdt, int nodeoffset,
 				struct platform_uart_data *uart);
lib/utils/fdt/fdt_helper.c +5 -43

79 lines not on this page · raw message

platform/generic/platform.c +1 -1
index 47e771ad..2e8c6051 100644
@@ -251,7 +251,7 @@ int generic_final_init(bool cold_boot)
 int generic_extensions_init(struct sbi_hart_features *hfeatures)
 {
 	/* Parse the ISA string from FDT and enable the listed extensions */
-	return fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
+	return fdt_parse_isa_extensions(fdt_get_address(),
 					hfeatures->extensions);
 }
 
Signature · 7 lines
2.48.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw
Quoted 11 lines · 在 2025-09-06六的 15:26 +0800,Yu-Chien Peter Lin写道:

The fdt_isa_bitmap was previously used as a temporary array to store ISA extensions when parsing device-tree. To reduce scratch space consumption, set the ISA bitmap directly in hfeature->extensions, allows us to remove the fdt_parse_isa_all_harts() as fdt_parse_isa_extensions() can now handle the task directly.

This change makes the helper function more versatile, even before scratch space is initialized.

LGTM

Quoted 123 lines

---
 include/sbi_utils/fdt/fdt_helper.h |  3 +- lib/utils/fdt/fdt_helper.c         | 48 ++++-------------------------- platform/generic/platform.c        |  2 +-

 3 files changed, 7 insertions(+), 46 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +0 -0
index 04c850cc..24ad1a94 100644
@@ -54,8 +54,7 @@ int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *c


 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);

include/sbi_utils/fdt/fdt_helper.h +1 -2
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-			     unsigned long *extensions);
+int fdt_parse_isa_extensions(const void *fdt, unsigned long *hart_exts);


 int fdt_parse_gaisler_uart_node(const void *fdt, int nodeoffset, struct platform_uart_data *uart);

lib/utils/fdt/fdt_helper.c +0 -0
index 0f4859c1..252dd459 100644
@@ -324,8 +324,6 @@ int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq)


 #define RISCV_ISA_EXT_NAME_LEN_MAX 32

lib/utils/fdt/fdt_helper.c +0 -2
-static unsigned long fdt_isa_bitmap_offset;
-

 static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions) {
  size_t i, j, isa_len;

lib/utils/fdt/fdt_helper.c +0 -0
@@ -405,15 +403,13 @@ static void fdt_parse_isa_extensions_one_hart(const char *isa,
  	}
  }

lib/utils/fdt/fdt_helper.c +1 -1
-static int fdt_parse_isa_all_harts(const void *fdt)
+int fdt_parse_isa_extensions(const void *fdt, unsigned long *hart_exts)

 {
  u32 hartid;
  const fdt32_t *val;

lib/utils/fdt/fdt_helper.c +0 -2
-	unsigned long *hart_exts;
-	struct sbi_scratch *scratch;

  int err, cpu_offset, cpus_offset, len;

lib/utils/fdt/fdt_helper.c +1 -1
-	if (!fdt || !fdt_isa_bitmap_offset)
+	if (!fdt || !hart_exts)

  return SBI_EINVAL;

  cpus_offset = fdt_path_offset(fdt, "/cpus");

lib/utils/fdt/fdt_helper.c +0 -0
@@ -425,15 +421,11 @@ static int fdt_parse_isa_all_harts(const void *fdt)
  		if (err)
  			continue;

lib/utils/fdt/fdt_helper.c +1 -1
-		if (!fdt_node_is_enabled(fdt, cpu_offset))
+		if (hartid != current_hartid())

  continue;

lib/utils/fdt/fdt_helper.c +2 -6
-		scratch = sbi_hartid_to_scratch(hartid);
-		if (!scratch)
-			return SBI_ENOENT;
-
-		hart_exts = sbi_scratch_offset_ptr(scratch,
-						   fdt_isa_bitmap_offset);
+		if (!fdt_node_is_enabled(fdt, cpu_offset))
+			continue;


  val = fdt_getprop(fdt, cpu_offset, "riscv,isa-extensions", &len); if (val && len > 0) {

lib/utils/fdt/fdt_helper.c +0 -0
@@ -454,36 +446,6 @@ static int fdt_parse_isa_all_harts(const void *fdt)
  	return 0;
  }

lib/utils/fdt/fdt_helper.c +0 -30
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-			unsigned long *extensions)
-{
-	int rc, i;
-	unsigned long *hart_exts;
-	struct sbi_scratch *scratch;
-
-	if (!fdt_isa_bitmap_offset) {
-		fdt_isa_bitmap_offset = sbi_scratch_alloc_offset(
-					sizeof(*hart_exts) *
-					BITS_TO_LONGS(SBI_HART_EXT_MAX));
-		if (!fdt_isa_bitmap_offset)
-			return SBI_ENOMEM;
-
-		rc = fdt_parse_isa_all_harts(fdt);
-		if (rc)
-			return rc;
-	}
-
-	scratch = sbi_hartid_to_scratch(hartid);
-	if (!scratch)
-		return SBI_ENOENT;
-
-	hart_exts = sbi_scratch_offset_ptr(scratch, fdt_isa_bitmap_offset);
-
-	for (i = 0; i < BITS_TO_LONGS(SBI_HART_EXT_MAX); i++)
-		extensions[i] |= hart_exts[i];
-	return 0;
-}
-

 static int fdt_parse_uart_node_common(const void *fdt, int nodeoffset, struct platform_uart_data *uart, unsigned long default_freq,

platform/generic/platform.c +1 -1
index 47e771ad..2e8c6051 100644
@@ -251,7 +251,7 @@ int generic_final_init(bool cold_boot)
  int generic_extensions_init(struct sbi_hart_features *hfeatures)
  {
  	/* Parse the ISA string from FDT and enable the listed extensions */
-	return fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
+	return fdt_parse_isa_extensions(fdt_get_address(),
  					hfeatures->extensions);
  }

Signature · 1 line
2.48.0
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#2 lore raw
In reply to Yu-Chien Peter Lin

Hi Peter,

Quoted 6 lines · Yu-Chien Peter Lin

The fdt_isa_bitmap was previously used as a temporary array to store ISA extensions when parsing device-tree. To reduce scratch space consumption, set the ISA bitmap directly in hfeature->extensions, allows us to remove the fdt_parse_isa_all_harts() as fdt_parse_isa_extensions() can now handle the task directly.

Unfortunately, this change is not valid, because sbi_platform_extensions_init() is called from the warm boot process, and it is not safe to access the FDT after the cold boot process completes. The FDT is stored in memory accessible to S-mode, so it could be clobbered as soon as the first supervisor domain starts. Any refactoring must satisfy the requirement that the cold boot hart parses the FDT nodes of all harts.

Regards,
Samuel

Quoted 125 lines

This change makes the helper function more versatile, even before scratch space is initialized.

---
 include/sbi_utils/fdt/fdt_helper.h |  3 +-
 lib/utils/fdt/fdt_helper.c         | 48 ++++--------------------------
 platform/generic/platform.c        |  2 +-
 3 files changed, 7 insertions(+), 46 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +1 -2
index 04c850cc..24ad1a94 100644
@@ -54,8 +54,7 @@ int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *c
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-			     unsigned long *extensions);
+int fdt_parse_isa_extensions(const void *fdt, unsigned long *hart_exts);
 
 int fdt_parse_gaisler_uart_node(const void *fdt, int nodeoffset,
 				struct platform_uart_data *uart);
lib/utils/fdt/fdt_helper.c +5 -43
index 0f4859c1..252dd459 100644
@@ -324,8 +324,6 @@ int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq)
 
 #define RISCV_ISA_EXT_NAME_LEN_MAX	32
 
-static unsigned long fdt_isa_bitmap_offset;
-
 static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions)
 {
 	size_t i, j, isa_len;
@@ -405,15 +403,13 @@ static void fdt_parse_isa_extensions_one_hart(const char *isa,
 	}
 }
 
-static int fdt_parse_isa_all_harts(const void *fdt)
+int fdt_parse_isa_extensions(const void *fdt, unsigned long *hart_exts)
 {
 	u32 hartid;
 	const fdt32_t *val;
-	unsigned long *hart_exts;
-	struct sbi_scratch *scratch;
 	int err, cpu_offset, cpus_offset, len;
 
-	if (!fdt || !fdt_isa_bitmap_offset)
+	if (!fdt || !hart_exts)
 		return SBI_EINVAL;
 
 	cpus_offset = fdt_path_offset(fdt, "/cpus");
@@ -425,15 +421,11 @@ static int fdt_parse_isa_all_harts(const void *fdt)
 		if (err)
 			continue;
 
-		if (!fdt_node_is_enabled(fdt, cpu_offset))
+		if (hartid != current_hartid())
 			continue;
 
-		scratch = sbi_hartid_to_scratch(hartid);
-		if (!scratch)
-			return SBI_ENOENT;
-
-		hart_exts = sbi_scratch_offset_ptr(scratch,
-						   fdt_isa_bitmap_offset);
+		if (!fdt_node_is_enabled(fdt, cpu_offset))
+			continue;
 
 		val = fdt_getprop(fdt, cpu_offset, "riscv,isa-extensions", &len);
 		if (val && len > 0) {
@@ -454,36 +446,6 @@ static int fdt_parse_isa_all_harts(const void *fdt)
 	return 0;
 }
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-			unsigned long *extensions)
-{
-	int rc, i;
-	unsigned long *hart_exts;
-	struct sbi_scratch *scratch;
-
-	if (!fdt_isa_bitmap_offset) {
-		fdt_isa_bitmap_offset = sbi_scratch_alloc_offset(
-					sizeof(*hart_exts) *
-					BITS_TO_LONGS(SBI_HART_EXT_MAX));
-		if (!fdt_isa_bitmap_offset)
-			return SBI_ENOMEM;
-
-		rc = fdt_parse_isa_all_harts(fdt);
-		if (rc)
-			return rc;
-	}
-
-	scratch = sbi_hartid_to_scratch(hartid);
-	if (!scratch)
-		return SBI_ENOENT;
-
-	hart_exts = sbi_scratch_offset_ptr(scratch, fdt_isa_bitmap_offset);
-
-	for (i = 0; i < BITS_TO_LONGS(SBI_HART_EXT_MAX); i++)
-		extensions[i] |= hart_exts[i];
-	return 0;
-}
-
 static int fdt_parse_uart_node_common(const void *fdt, int nodeoffset,
 				      struct platform_uart_data *uart,
 				      unsigned long default_freq,
platform/generic/platform.c +1 -1
index 47e771ad..2e8c6051 100644
@@ -251,7 +251,7 @@ int generic_final_init(bool cold_boot)
 int generic_extensions_init(struct sbi_hart_features *hfeatures)
 {
 	/* Parse the ISA string from FDT and enable the listed extensions */
-	return fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
+	return fdt_parse_isa_extensions(fdt_get_address(),
 					hfeatures->extensions);
 }
 
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#3 lore raw

Hi Samuel,

Thanks for the explanation about the FDT access timing during warm boot. The original code makes sense now - we can drop the patch.

Thanks,
Peter Lin

Quoted 147 lines · Samuel Holland

Hi Peter,

Quoted 6 lines · Yu-Chien Peter Lin

The fdt_isa_bitmap was previously used as a temporary array to store ISA extensions when parsing device-tree. To reduce scratch space consumption, set the ISA bitmap directly in hfeature->extensions, allows us to remove the fdt_parse_isa_all_harts() as fdt_parse_isa_extensions() can now handle the task directly.

Unfortunately, this change is not valid, because sbi_platform_extensions_init() is called from the warm boot process, and it is not safe to access the FDT after the cold boot process completes. The FDT is stored in memory accessible to S-mode, so it could be clobbered as soon as the first supervisor domain starts. Any refactoring must satisfy the requirement that the cold boot hart parses the FDT nodes of all harts.

Regards,
Samuel

Quoted 125 lines

This change makes the helper function more versatile, even before scratch space is initialized.

---
 include/sbi_utils/fdt/fdt_helper.h |  3 +-
 lib/utils/fdt/fdt_helper.c         | 48 ++++--------------------------
 platform/generic/platform.c        |  2 +-
 3 files changed, 7 insertions(+), 46 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +1 -2
index 04c850cc..24ad1a94 100644
@@ -54,8 +54,7 @@ int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *c
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-                          unsigned long *extensions);
+int fdt_parse_isa_extensions(const void *fdt, unsigned long *hart_exts);
 
 int fdt_parse_gaisler_uart_node(const void *fdt, int nodeoffset,
                              struct platform_uart_data *uart);
lib/utils/fdt/fdt_helper.c +5 -43
index 0f4859c1..252dd459 100644
@@ -324,8 +324,6 @@ int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq)
 
 #define RISCV_ISA_EXT_NAME_LEN_MAX   32
 
-static unsigned long fdt_isa_bitmap_offset;
-
 static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions)
 {
      size_t i, j, isa_len;
@@ -405,15 +403,13 @@ static void fdt_parse_isa_extensions_one_hart(const char *isa,
      }
 }
 
-static int fdt_parse_isa_all_harts(const void *fdt)
+int fdt_parse_isa_extensions(const void *fdt, unsigned long *hart_exts)
 {
      u32 hartid;
      const fdt32_t *val;
-     unsigned long *hart_exts;
-     struct sbi_scratch *scratch;
      int err, cpu_offset, cpus_offset, len;
 
-     if (!fdt || !fdt_isa_bitmap_offset)
+     if (!fdt || !hart_exts)
              return SBI_EINVAL;
 
      cpus_offset = fdt_path_offset(fdt, "/cpus");
@@ -425,15 +421,11 @@ static int fdt_parse_isa_all_harts(const void *fdt)
              if (err)
                      continue;
 
-             if (!fdt_node_is_enabled(fdt, cpu_offset))
+             if (hartid != current_hartid())
                      continue;
 
-             scratch = sbi_hartid_to_scratch(hartid);
-             if (!scratch)
-                     return SBI_ENOENT;
-
-             hart_exts = sbi_scratch_offset_ptr(scratch,
-                                                fdt_isa_bitmap_offset);
+             if (!fdt_node_is_enabled(fdt, cpu_offset))
+                     continue;
 
              val = fdt_getprop(fdt, cpu_offset, "riscv,isa-extensions", &len);
              if (val && len > 0) {
@@ -454,36 +446,6 @@ static int fdt_parse_isa_all_harts(const void *fdt)
      return 0;
 }
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-                     unsigned long *extensions)
-{
-     int rc, i;
-     unsigned long *hart_exts;
-     struct sbi_scratch *scratch;
-
-     if (!fdt_isa_bitmap_offset) {
-             fdt_isa_bitmap_offset = sbi_scratch_alloc_offset(
-                                     sizeof(*hart_exts) *
-                                     BITS_TO_LONGS(SBI_HART_EXT_MAX));
-             if (!fdt_isa_bitmap_offset)
-                     return SBI_ENOMEM;
-
-             rc = fdt_parse_isa_all_harts(fdt);
-             if (rc)
-                     return rc;
-     }
-
-     scratch = sbi_hartid_to_scratch(hartid);
-     if (!scratch)
-             return SBI_ENOENT;
-
-     hart_exts = sbi_scratch_offset_ptr(scratch, fdt_isa_bitmap_offset);
-
-     for (i = 0; i < BITS_TO_LONGS(SBI_HART_EXT_MAX); i++)
-             extensions[i] |= hart_exts[i];
-     return 0;
-}
-
 static int fdt_parse_uart_node_common(const void *fdt, int nodeoffset,
                                    struct platform_uart_data *uart,
                                    unsigned long default_freq,
platform/generic/platform.c +1 -1
index 47e771ad..2e8c6051 100644
@@ -251,7 +251,7 @@ int generic_final_init(bool cold_boot)
 int generic_extensions_init(struct sbi_hart_features *hfeatures)
 {
      /* Parse the ISA string from FDT and enable the listed extensions */
-     return fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
+     return fdt_parse_isa_extensions(fdt_get_address(),
                                      hfeatures->extensions);
 }
 
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#4 lore raw

PATCH v3 1/1 lib: utils: fdt_helper: simplify fdt_parse_isa_extensions() helper

The fdt_isa_bitmap was previously used as a temporary array to store ISA extensions when parsing device-tree. To reduce scratch space consumption, set the ISA bitmap directly in hfeature->extensions, allows us to remove the fdt_parse_isa_all_harts() as fdt_parse_isa_extensions() can now handle the task directly.

---
 include/sbi_utils/fdt/fdt_helper.h |  3 +-
 lib/utils/fdt/fdt_helper.c         | 58 ++++++------------------------
 platform/generic/platform.c        |  3 +-
 3 files changed, 13 insertions(+), 51 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +1 -2
index 04c850cc..2ece7f5e 100644
@@ -54,8 +54,7 @@ int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *c
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-			     unsigned long *extensions);
+int fdt_parse_isa_extensions(const void *fdt);
 
 int fdt_parse_gaisler_uart_node(const void *fdt, int nodeoffset,
 				struct platform_uart_data *uart);
lib/utils/fdt/fdt_helper.c +11 -47

110 lines not on this page · raw message

platform/generic/platform.c +1 -2
index 47e771ad..7e3417b0 100644
@@ -251,8 +251,7 @@ int generic_final_init(bool cold_boot)
 int generic_extensions_init(struct sbi_hart_features *hfeatures)
 {
 	/* Parse the ISA string from FDT and enable the listed extensions */
-	return fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
-					hfeatures->extensions);
+	return fdt_parse_isa_extensions(fdt_get_address());
 }
 
 int generic_domains_init(void)
Signature · 7 lines
2.47.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#5 lore raw

Hi Xiang,

Thanks for taking care of this patch,
But how does this version solve the issue of corrupted FDT data during warm boot?

Quoted 167 lines · Xiang W

The fdt_isa_bitmap was previously used as a temporary array to store ISA extensions when parsing device-tree. To reduce scratch space consumption, set the ISA bitmap directly in hfeature->extensions, allows us to remove the fdt_parse_isa_all_harts() as fdt_parse_isa_extensions() can now handle the task directly.

---
 include/sbi_utils/fdt/fdt_helper.h |  3 +-
 lib/utils/fdt/fdt_helper.c         | 58 ++++++------------------------
 platform/generic/platform.c        |  3 +-
 3 files changed, 13 insertions(+), 51 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +1 -2
index 04c850cc..2ece7f5e 100644
@@ -54,8 +54,7 @@ int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *c
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-                            unsigned long *extensions);
+int fdt_parse_isa_extensions(const void *fdt);
 
 int fdt_parse_gaisler_uart_node(const void *fdt, int nodeoffset,
                                struct platform_uart_data *uart);
lib/utils/fdt/fdt_helper.c +11 -47
index 0f4859c1..8685d9fc 100644
@@ -324,9 +324,7 @@ int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq)
 
 #define RISCV_ISA_EXT_NAME_LEN_MAX     32
 
-static unsigned long fdt_isa_bitmap_offset;
-
-static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions)
+static int fdt_parse_isa_one_hart(struct sbi_scratch *scratch, const char *isa)
 {
        size_t i, j, isa_len;
        char mstr[RISCV_ISA_EXT_NAME_LEN_MAX];
@@ -379,7 +377,8 @@ static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions)
 
 #define set_multi_letter_ext(name, bit)                                \
                        if (!strcmp(mstr, name)) {              \
-                               __set_bit(bit, extensions);     \
+                               sbi_hart_update_extension(      \
+                                       scratch, bit, true);    \
                                continue;                       \
                        }
 
@@ -393,27 +392,25 @@ static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions)
        return 0;
 }
 
-static void fdt_parse_isa_extensions_one_hart(const char *isa,
-                                             unsigned long *extensions,
-                                             int len)
+static void fdt_parse_isa_extensions_one_hart(struct sbi_scratch *scratch,
+                                             const char *isa, int len)
 {
        size_t i;
 
        for (i = 0; i < SBI_HART_EXT_MAX; i++) {
                if (fdt_stringlist_contains(isa, len, sbi_hart_ext[i].name))
-                       __set_bit(sbi_hart_ext[i].id, extensions);
+                       sbi_hart_update_extension(scratch, sbi_hart_ext[i].id, true);
        }
 }
 
-static int fdt_parse_isa_all_harts(const void *fdt)
+int fdt_parse_isa_extensions(const void *fdt)
 {
        u32 hartid;
        const fdt32_t *val;
-       unsigned long *hart_exts;
        struct sbi_scratch *scratch;
        int err, cpu_offset, cpus_offset, len;
 
-       if (!fdt || !fdt_isa_bitmap_offset)
+       if (!fdt)
                return SBI_EINVAL;
 
        cpus_offset = fdt_path_offset(fdt, "/cpus");
@@ -432,13 +429,10 @@ static int fdt_parse_isa_all_harts(const void *fdt)
                if (!scratch)
                        return SBI_ENOENT;
 
-               hart_exts = sbi_scratch_offset_ptr(scratch,
-                                                  fdt_isa_bitmap_offset);
-
                val = fdt_getprop(fdt, cpu_offset, "riscv,isa-extensions", &len);
                if (val && len > 0) {
-                       fdt_parse_isa_extensions_one_hart((const char *)val,
-                                                         hart_exts, len);
+                       fdt_parse_isa_extensions_one_hart(scratch,
+                                                       (const char *)val, len);
                        continue;
                }
 
@@ -446,7 +440,7 @@ static int fdt_parse_isa_all_harts(const void *fdt)
                if (!val || len <= 0)
                        return SBI_ENOENT;
 
-               err = fdt_parse_isa_one_hart((const char *)val, hart_exts);
+               err = fdt_parse_isa_one_hart(scratch, (const char *)val);
                if (err)
                        return err;
        }
@@ -454,36 +448,6 @@ static int fdt_parse_isa_all_harts(const void *fdt)
        return 0;
 }
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-                       unsigned long *extensions)
-{
-       int rc, i;
-       unsigned long *hart_exts;
-       struct sbi_scratch *scratch;
-
-       if (!fdt_isa_bitmap_offset) {
-               fdt_isa_bitmap_offset = sbi_scratch_alloc_offset(
-                                       sizeof(*hart_exts) *
-                                       BITS_TO_LONGS(SBI_HART_EXT_MAX));
-               if (!fdt_isa_bitmap_offset)
-                       return SBI_ENOMEM;
-
-               rc = fdt_parse_isa_all_harts(fdt);
-               if (rc)
-                       return rc;
-       }
-
-       scratch = sbi_hartid_to_scratch(hartid);
-       if (!scratch)
-               return SBI_ENOENT;
-
-       hart_exts = sbi_scratch_offset_ptr(scratch, fdt_isa_bitmap_offset);
-
-       for (i = 0; i < BITS_TO_LONGS(SBI_HART_EXT_MAX); i++)
-               extensions[i] |= hart_exts[i];
-       return 0;
-}
-
 static int fdt_parse_uart_node_common(const void *fdt, int nodeoffset,
                                      struct platform_uart_data *uart,
                                      unsigned long default_freq,
platform/generic/platform.c +1 -3
index 47e771ad..7e3417b0 100644
@@ -251,8 +251,7 @@ int generic_final_init(bool cold_boot)
 int generic_extensions_init(struct sbi_hart_features *hfeatures)
 {
        /* Parse the ISA string from FDT and enable the listed extensions */
-       return fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
-                                       hfeatures->extensions);
+       return fdt_parse_isa_extensions(fdt_get_address());
 }
 
 int generic_domains_init(void)
--

2.47.3

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#6 lore raw

PATCH v4 1/1 lib: utils: fdt_helper: simplify fdt_parse_isa_extensions() helper

The fdt_isa_bitmap was previously used as a temporary array to store ISA extensions when parsing device-tree. To reduce scratch space consumption, set the ISA bitmap directly in hfeature->extensions, allows us to remove the fdt_parse_isa_all_harts() as fdt_parse_isa_extensions() can now handle the task directly.

---
 include/sbi_utils/fdt/fdt_helper.h |  3 +-
 lib/utils/fdt/fdt_helper.c         | 63 ++++++++----------------------
 platform/generic/platform.c        |  3 +-
 3 files changed, 18 insertions(+), 51 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +1 -2
index 04c850cc..2ece7f5e 100644
@@ -54,8 +54,7 @@ int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *c
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-			     unsigned long *extensions);
+int fdt_parse_isa_extensions(const void *fdt);
 
 int fdt_parse_gaisler_uart_node(const void *fdt, int nodeoffset,
 				struct platform_uart_data *uart);
lib/utils/fdt/fdt_helper.c +16 -47

117 lines not on this page · raw message

platform/generic/platform.c +1 -2
index 47e771ad..7e3417b0 100644
@@ -251,8 +251,7 @@ int generic_final_init(bool cold_boot)
 int generic_extensions_init(struct sbi_hart_features *hfeatures)
 {
 	/* Parse the ISA string from FDT and enable the listed extensions */
-	return fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
-					hfeatures->extensions);
+	return fdt_parse_isa_extensions(fdt_get_address());
 }
 
 int generic_domains_init(void)
Signature · 7 lines
2.47.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#7 lore raw

Hi Xiang,

Still have an issue, when a secondary hart calls hart_detect_features() the FDT ISA bits set by the boot hart are cleared here [1]. I think it is still necessary to cache the ISA bitmap somewhere before it gets OR'ed into hfeatures->extensions.

[1] https://github.com/riscv-software-src/opensbi/blob/153cdeea5350837c1206a2d2b6189fd0ba06d1f2/lib/sbi/sbi_hart.c#L836-L837

Quoted 174 lines · Xiang W

The fdt_isa_bitmap was previously used as a temporary array to store ISA extensions when parsing device-tree. To reduce scratch space consumption, set the ISA bitmap directly in hfeature->extensions, allows us to remove the fdt_parse_isa_all_harts() as fdt_parse_isa_extensions() can now handle the task directly.

---
 include/sbi_utils/fdt/fdt_helper.h |  3 +-
 lib/utils/fdt/fdt_helper.c         | 63 ++++++++----------------------
 platform/generic/platform.c        |  3 +-
 3 files changed, 18 insertions(+), 51 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +1 -2
index 04c850cc..2ece7f5e 100644
@@ -54,8 +54,7 @@ int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *c
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-                            unsigned long *extensions);
+int fdt_parse_isa_extensions(const void *fdt);
 
 int fdt_parse_gaisler_uart_node(const void *fdt, int nodeoffset,
                                struct platform_uart_data *uart);
lib/utils/fdt/fdt_helper.c +16 -47
index 0f4859c1..64f928f7 100644
@@ -324,9 +324,7 @@ int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq)
 
 #define RISCV_ISA_EXT_NAME_LEN_MAX     32
 
-static unsigned long fdt_isa_bitmap_offset;
-
-static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions)
+static int fdt_parse_isa_one_hart(struct sbi_scratch *scratch, const char *isa)
 {
        size_t i, j, isa_len;
        char mstr[RISCV_ISA_EXT_NAME_LEN_MAX];
@@ -379,7 +377,8 @@ static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions)
 
 #define set_multi_letter_ext(name, bit)                                \
                        if (!strcmp(mstr, name)) {              \
-                               __set_bit(bit, extensions);     \
+                               sbi_hart_update_extension(      \
+                                       scratch, bit, true);    \
                                continue;                       \
                        }
 
@@ -393,29 +392,32 @@ static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions)
        return 0;
 }
 
-static void fdt_parse_isa_extensions_one_hart(const char *isa,
-                                             unsigned long *extensions,
-                                             int len)
+static void fdt_parse_isa_extensions_one_hart(struct sbi_scratch *scratch,
+                                             const char *isa, int len)
 {
        size_t i;
 
        for (i = 0; i < SBI_HART_EXT_MAX; i++) {
                if (fdt_stringlist_contains(isa, len, sbi_hart_ext[i].name))
-                       __set_bit(sbi_hart_ext[i].id, extensions);
+                       sbi_hart_update_extension(scratch, sbi_hart_ext[i].id, true);
        }
 }
 
-static int fdt_parse_isa_all_harts(const void *fdt)
+int fdt_parse_isa_extensions(const void *fdt)
 {
+       static bool done;
        u32 hartid;
        const fdt32_t *val;
-       unsigned long *hart_exts;
        struct sbi_scratch *scratch;
        int err, cpu_offset, cpus_offset, len;
 
-       if (!fdt || !fdt_isa_bitmap_offset)
+       if (!fdt)
                return SBI_EINVAL;
 
+       if (done)
+               return 0;
+       done = true;
+
        cpus_offset = fdt_path_offset(fdt, "/cpus");
        if (cpus_offset < 0)
                return cpus_offset;
@@ -432,13 +434,10 @@ static int fdt_parse_isa_all_harts(const void *fdt)
                if (!scratch)
                        return SBI_ENOENT;
 
-               hart_exts = sbi_scratch_offset_ptr(scratch,
-                                                  fdt_isa_bitmap_offset);
-
                val = fdt_getprop(fdt, cpu_offset, "riscv,isa-extensions", &len);
                if (val && len > 0) {
-                       fdt_parse_isa_extensions_one_hart((const char *)val,
-                                                         hart_exts, len);
+                       fdt_parse_isa_extensions_one_hart(scratch,
+                                                       (const char *)val, len);
                        continue;
                }
 
@@ -446,7 +445,7 @@ static int fdt_parse_isa_all_harts(const void *fdt)
                if (!val || len <= 0)
                        return SBI_ENOENT;
 
-               err = fdt_parse_isa_one_hart((const char *)val, hart_exts);
+               err = fdt_parse_isa_one_hart(scratch, (const char *)val);
                if (err)
                        return err;
        }
@@ -454,36 +453,6 @@ static int fdt_parse_isa_all_harts(const void *fdt)
        return 0;
 }
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-                       unsigned long *extensions)
-{
-       int rc, i;
-       unsigned long *hart_exts;
-       struct sbi_scratch *scratch;
-
-       if (!fdt_isa_bitmap_offset) {
-               fdt_isa_bitmap_offset = sbi_scratch_alloc_offset(
-                                       sizeof(*hart_exts) *
-                                       BITS_TO_LONGS(SBI_HART_EXT_MAX));
-               if (!fdt_isa_bitmap_offset)
-                       return SBI_ENOMEM;
-
-               rc = fdt_parse_isa_all_harts(fdt);
-               if (rc)
-                       return rc;
-       }
-
-       scratch = sbi_hartid_to_scratch(hartid);
-       if (!scratch)
-               return SBI_ENOENT;
-
-       hart_exts = sbi_scratch_offset_ptr(scratch, fdt_isa_bitmap_offset);
-
-       for (i = 0; i < BITS_TO_LONGS(SBI_HART_EXT_MAX); i++)
-               extensions[i] |= hart_exts[i];
-       return 0;
-}
-
 static int fdt_parse_uart_node_common(const void *fdt, int nodeoffset,
                                      struct platform_uart_data *uart,
                                      unsigned long default_freq,
platform/generic/platform.c +1 -3
index 47e771ad..7e3417b0 100644
@@ -251,8 +251,7 @@ int generic_final_init(bool cold_boot)
 int generic_extensions_init(struct sbi_hart_features *hfeatures)
 {
        /* Parse the ISA string from FDT and enable the listed extensions */
-       return fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
-                                       hfeatures->extensions);
+       return fdt_parse_isa_extensions(fdt_get_address());
 }
 
 int generic_domains_init(void)
--

2.47.3

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#8 lore raw
Quoted 8 lines · 在 2025-09-12五的 16:56 +0800,Peter Lin写道:

Hi Xiang,

Still have an issue, when a secondary hart calls hart_detect_features() the FDT ISA bits set by the boot hart are cleared here [1]. I think it is still necessary to cache the ISA bitmap somewhere before it gets OR'ed into hfeatures->extensions.

[1] https://github.com/riscv-software-src/opensbi/blob/153cdeea5350837c1206a2d2b6189fd0ba06d1f2/lib/sbi/sbi_hart.c#L836-L837

Indeed, let's drop this patch.

Regards,
Xiang W

Quoted 176 lines · Xiang W
Quoted 174 lines

The fdt_isa_bitmap was previously used as a temporary array to store ISA extensions when parsing device-tree. To reduce scratch space consumption, set the ISA bitmap directly in hfeature->extensions, allows us to remove the fdt_parse_isa_all_harts() as fdt_parse_isa_extensions() can now handle the task directly.

---
 include/sbi_utils/fdt/fdt_helper.h |  3 +- lib/utils/fdt/fdt_helper.c         | 63 ++++++++---------------------- platform/generic/platform.c        |  3 +-

 3 files changed, 18 insertions(+), 51 deletions(-)
include/sbi_utils/fdt/fdt_helper.h +1 -2
index 04c850cc..2ece7f5e 100644
@@ -54,8 +54,7 @@ int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *c
 
  int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-                            unsigned long *extensions);
+int fdt_parse_isa_extensions(const void *fdt);
 
  int fdt_parse_gaisler_uart_node(const void *fdt, int nodeoffset,
                                 struct platform_uart_data *uart);
lib/utils/fdt/fdt_helper.c +16 -47
index 0f4859c1..64f928f7 100644
@@ -324,9 +324,7 @@ int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq)
 
  #define RISCV_ISA_EXT_NAME_LEN_MAX     32
 
-static unsigned long fdt_isa_bitmap_offset;
-
-static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions)
+static int fdt_parse_isa_one_hart(struct sbi_scratch *scratch, const char *isa)
  {
         size_t i, j, isa_len;
         char mstr[RISCV_ISA_EXT_NAME_LEN_MAX];
@@ -379,7 +377,8 @@ static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions)
 
  #define set_multi_letter_ext(name, bit)                                \
                         if (!strcmp(mstr, name)) {              \
-                               __set_bit(bit, extensions);     \
+                               sbi_hart_update_extension(      \
+                                       scratch, bit, true);    \
                                 continue;                       \
                         }
 
@@ -393,29 +392,32 @@ static int fdt_parse_isa_one_hart(const char *isa, unsigned long *extensions)
         return 0;
  }
 
-static void fdt_parse_isa_extensions_one_hart(const char *isa,
-                                             unsigned long *extensions,
-                                             int len)
+static void fdt_parse_isa_extensions_one_hart(struct sbi_scratch *scratch,
+                                             const char *isa, int len)
  {
         size_t i;
 
         for (i = 0; i < SBI_HART_EXT_MAX; i++) {
                 if (fdt_stringlist_contains(isa, len, sbi_hart_ext[i].name))
-                       __set_bit(sbi_hart_ext[i].id, extensions);
+                       sbi_hart_update_extension(scratch, sbi_hart_ext[i].id, true);
         }
  }
 
-static int fdt_parse_isa_all_harts(const void *fdt)
+int fdt_parse_isa_extensions(const void *fdt)
  {
+       static bool done;
         u32 hartid;
         const fdt32_t *val;
-       unsigned long *hart_exts;
         struct sbi_scratch *scratch;
         int err, cpu_offset, cpus_offset, len;
 
-       if (!fdt || !fdt_isa_bitmap_offset)
+       if (!fdt)
                 return SBI_EINVAL;
 
+       if (done)
+               return 0;
+       done = true;
+
         cpus_offset = fdt_path_offset(fdt, "/cpus");
         if (cpus_offset < 0)
                 return cpus_offset;
@@ -432,13 +434,10 @@ static int fdt_parse_isa_all_harts(const void *fdt)
                 if (!scratch)
                         return SBI_ENOENT;
 
-               hart_exts = sbi_scratch_offset_ptr(scratch,
-                                                  fdt_isa_bitmap_offset);
-
                 val = fdt_getprop(fdt, cpu_offset, "riscv,isa-extensions", &len);
                 if (val && len > 0) {
-                       fdt_parse_isa_extensions_one_hart((const char *)val,
-                                                         hart_exts, len);
+                       fdt_parse_isa_extensions_one_hart(scratch,
+                                                       (const char *)val, len);
                         continue;
                 }
 
@@ -446,7 +445,7 @@ static int fdt_parse_isa_all_harts(const void *fdt)
                 if (!val || len <= 0)
                         return SBI_ENOENT;
 
-               err = fdt_parse_isa_one_hart((const char *)val, hart_exts);
+               err = fdt_parse_isa_one_hart(scratch, (const char *)val);
                 if (err)
                         return err;
         }
@@ -454,36 +453,6 @@ static int fdt_parse_isa_all_harts(const void *fdt)
         return 0;
  }
 
-int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
-                       unsigned long *extensions)
-{
-       int rc, i;
-       unsigned long *hart_exts;
-       struct sbi_scratch *scratch;
-
-       if (!fdt_isa_bitmap_offset) {
-               fdt_isa_bitmap_offset = sbi_scratch_alloc_offset(
-                                       sizeof(*hart_exts) *
-                                       BITS_TO_LONGS(SBI_HART_EXT_MAX));
-               if (!fdt_isa_bitmap_offset)
-                       return SBI_ENOMEM;
-
-               rc = fdt_parse_isa_all_harts(fdt);
-               if (rc)
-                       return rc;
-       }
-
-       scratch = sbi_hartid_to_scratch(hartid);
-       if (!scratch)
-               return SBI_ENOENT;
-
-       hart_exts = sbi_scratch_offset_ptr(scratch, fdt_isa_bitmap_offset);
-
-       for (i = 0; i < BITS_TO_LONGS(SBI_HART_EXT_MAX); i++)
-               extensions[i] |= hart_exts[i];
-       return 0;
-}
-
  static int fdt_parse_uart_node_common(const void *fdt, int nodeoffset,
                                       struct platform_uart_data *uart,
                                       unsigned long default_freq,
platform/generic/platform.c +1 -3
index 47e771ad..7e3417b0 100644
@@ -251,8 +251,7 @@ int generic_final_init(bool cold_boot)
  int generic_extensions_init(struct sbi_hart_features *hfeatures)
  {
         /* Parse the ISA string from FDT and enable the listed extensions */
-       return fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
-                                       hfeatures->extensions);
+       return fdt_parse_isa_extensions(fdt_get_address());
  }
 
  int generic_domains_init(void)
--

2.47.3

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi