[PATCH v3 3/7] lib: utils/fdt: Indicate when cached FDT blob offsets are valid
Samuel Holland
samuel.holland at sifive.com
Wed Oct 14 04:30:20 PDT 2026
During the cold boot process, data such as the CPU node offsets is
repeatedly parsed from the FDT blob. Caching this information can save
code size and significantly speed up the boot process. However, any
changes to the FDT blob may affect its structure and invalidate cached
offsets and pointers. As a result, any caching logic must check if the
FDT blob has been modified. Since all writes to the FDT blob go through
fdt_get_address_rw(), performing the validity check is straightforward.
Signed-off-by: Samuel Holland <samuel.holland at sifive.com>
---
Changes in v3:
- New patch for v3
include/sbi_utils/fdt/fdt_helper.h | 31 +++++++++++++++++++++---------
lib/utils/fdt/fdt_helper.c | 19 ++++++++++++++++++
2 files changed, 41 insertions(+), 9 deletions(-)
diff --git a/include/sbi_utils/fdt/fdt_helper.h b/include/sbi_utils/fdt/fdt_helper.h
index 1246ab69..263e07c7 100644
--- a/include/sbi_utils/fdt/fdt_helper.h
+++ b/include/sbi_utils/fdt/fdt_helper.h
@@ -11,7 +11,6 @@
#define __FDT_HELPER_H__
#include <sbi/sbi_types.h>
-#include <sbi/sbi_domain.h>
struct fdt_match {
const char *compatible;
@@ -106,14 +105,28 @@ int fdt_parse_plicsw_node(const void *fdt, int nodeoffset, unsigned long *plicsw
int fdt_parse_compat_addr(const void *fdt, uint64_t *addr,
const char *compatible);
-static inline const void *fdt_get_address(void)
-{
- return (const void *)root.next_arg1;
-}
+/**
+ * Check whether cached FDT blob offsets are still valid
+ *
+ * If this function returns false, the structure of the FDT blob may have been
+ * modified, and cached offsets must not be passed to FDT accessor functions.
+ *
+ * @returns true if the FDT blob has never been modified during the execution
+ * of the firmware, else false
+ */
+bool fdt_cached_offsets_valid(void);
-static inline void *fdt_get_address_rw(void)
-{
- return (void *)root.next_arg1;
-}
+/**
+ * Get a pointer to the platform's flattened devicetree blob
+ */
+const void *fdt_get_address(void);
+
+/**
+ * Get a writable pointer to the platform's flattened devicetree blob
+ *
+ * This function should be used only by code that explicitly needs to modify
+ * the FDT blob (e.g. FDT fixup routines).
+ */
+void *fdt_get_address_rw(void);
#endif /* __FDT_HELPER_H__ */
diff --git a/lib/utils/fdt/fdt_helper.c b/lib/utils/fdt/fdt_helper.c
index f78ea39e..c5d76707 100644
--- a/lib/utils/fdt/fdt_helper.c
+++ b/lib/utils/fdt/fdt_helper.c
@@ -9,6 +9,7 @@
#include <libfdt.h>
#include <sbi/riscv_asm.h>
#include <sbi/sbi_console.h>
+#include <sbi/sbi_domain.h>
#include <sbi/sbi_hartmask.h>
#include <sbi/sbi_platform.h>
#include <sbi/sbi_scratch.h>
@@ -33,6 +34,24 @@
#define DEFAULT_SHAKTI_UART_FREQ 50000000
#define DEFAULT_SHAKTI_UART_BAUD 115200
+static bool cached_offsets_valid = true;
+
+bool fdt_cached_offsets_valid(void)
+{
+ return cached_offsets_valid;
+}
+
+const void *fdt_get_address(void)
+{
+ return (const void *)root.next_arg1;
+}
+
+void *fdt_get_address_rw(void)
+{
+ cached_offsets_valid = false;
+ return (void *)root.next_arg1;
+}
+
int fdt_parse_phandle_with_args(const void *fdt, int nodeoff,
const char *prop, const char *cells_prop,
int index, struct fdt_phandle_args *out_args)
--
2.52.0
More information about the opensbi
mailing list