From a5c0307e9c8aeb613028c0790c4c10aa8e48fb4c Mon Sep 17 00:00:00 2001 From: Subrata Banik Date: Fri, 9 Jan 2026 14:35:42 +0530 Subject: [PATCH] commonlib/device_tree: Add dt_add_reserved_memory_region helper Introduce a centralized helper function, dt_add_reserved_memory_region, to simplify the creation of sub-nodes under /reserved-memory. Currently, various features (such as pKVM, ramoops, and platform- specific firmware reservations) manually handle the creation of reserved memory nodes. This involves repetitive logic for: - Navigating or creating the /reserved-memory parent path. - Calculating cell sizes for 'reg' properties. - Manually adding 'no-map' or 'compatible' properties. This helper abstracts those steps into a single call, reducing boilerplate and the risk of cell-size mismatches across the codebase. The function handles: - Node creation if the path doesn't exist. - Optional 'compatible' string assignment. - Automatic 'reg' property generation using appropriate address/size cells. - Optional 'no-map' property assignment via a boolean flag. Change-Id: Ie58f5fdcfd1863b41c177b63ed9fc25d6d220e3a Signed-off-by: Subrata Banik Reviewed-on: https://review.coreboot.org/c/coreboot/+/90713 Reviewed-by: Kapil Porwal Tested-by: build bot (Jenkins) Reviewed-by: Julius Werner --- src/commonlib/device_tree.c | 29 +++++++++++++++++++ src/commonlib/include/commonlib/device_tree.h | 15 ++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/commonlib/device_tree.c b/src/commonlib/device_tree.c index b51f9fc31d..7d5161db23 100644 --- a/src/commonlib/device_tree.c +++ b/src/commonlib/device_tree.c @@ -2166,3 +2166,32 @@ bool dt_is_overlay(struct device_tree *tree) return false; } + +struct device_tree_node *dt_add_reserved_memory_region(struct device_tree *tree, + const char *name, const char *compatible, uint64_t addr, uint64_t size, + bool nomap) +{ + uint32_t addr_cells, size_cells; + const char *path[] = { "reserved-memory", name, NULL }; + + /* Find or create the node. Using 1 for the 'create' argument. */ + struct device_tree_node *node = dt_find_node(tree->root, path, + &addr_cells, &size_cells, 1); + if (!node) { + printk(BIOS_ERR, "Failed to create reserved-memory node: %s\n", name); + return NULL; + } + + /* Add the compatible string if provided */ + if (compatible) + dt_add_string_prop(node, "compatible", compatible); + + /* Always add the reg property */ + dt_add_reg_prop(node, &addr, &size, 1, addr_cells, size_cells); + + /* Add no-map if requested */ + if (nomap) + dt_add_bin_prop(node, "no-map", NULL, 0); + + return node; +} diff --git a/src/commonlib/include/commonlib/device_tree.h b/src/commonlib/include/commonlib/device_tree.h index b6004fe8c4..ec4b4ffd29 100644 --- a/src/commonlib/include/commonlib/device_tree.h +++ b/src/commonlib/include/commonlib/device_tree.h @@ -237,4 +237,19 @@ struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree); /* Check whether a devicetree is an overlay device tree */ bool dt_is_overlay(struct device_tree *tree); +/* + * dt_add_reserved_memory_region - Helper to add a node under `/reserved-memory` + * @tree: The device tree structure + * @name: Name of the child node (e.g., "ramoops", "pkvm-drng-seed") + * @compatible: Compatible string (optional, pass NULL if not needed) + * @addr: Physical start address + * @size: Size of the region + * @nomap: Boolean, if true adds the "no-map" property + * + * Returns the created node on success, NULL on failure. + */ +struct device_tree_node *dt_add_reserved_memory_region(struct device_tree *tree, + const char *name, const char *compatible, uint64_t addr, uint64_t size, + bool nomap); + #endif /* __COMMONLIB_DEVICE_TREE_H__ */