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 <subratabanik@google.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/90713
Reviewed-by: Kapil Porwal <kapilporwal@google.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
Subrata Banik 2026-01-09 14:35:42 +05:30 committed by Julius Werner
commit a5c0307e9c
2 changed files with 44 additions and 0 deletions

View file

@ -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;
}

View file

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