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__ */