fix(fdt): reserved memory: detect existing region

When fdt_add_reserved_memory() is called to add a memory region, we
unconditionally add a node for that region. However there might be an
existing region node in the DT already, or there might be an overlapping
region. The Linux kernel will complain in those cases.

Cover the simple case of the region already existing in the DT, as this
is what we actually see on the Allwinner H616: The mainline DT contains
a node reserving the memory for TF-A, in case the DT changed by TF-A
itself is not given to the kernel. Our code always adds a region, making
the kernel complain - albeit without further consequences.

Covering all cases of overlapping regions would blow up the generic DT
code too much, so just add a simple check for an existing region
completely containing the to-be-added region, simply bailing out in this
case.

This prevents the kernel warning for the Allwinner H616.

This code requires a function from fdt_wrappers.c, so we have to include
that file for platforms that use the fdt_add_reserved_memory() function
(rpi4 and versal2).

Change-Id: I98404889163316addbb42130d7177f1a21c8be06
Signed-off-by: Andre Przywara <andre.przywara@arm.com>
This commit is contained in:
Andre Przywara 2024-03-21 13:27:56 +00:00 committed by André Przywara
parent 8e9bdc5b1d
commit 42488064e1
3 changed files with 21 additions and 0 deletions

View file

@ -197,6 +197,7 @@ int fdt_add_reserved_memory(void *dtb, const char *node_name,
uintptr_t base, size_t size)
{
int offs = fdt_path_offset(dtb, "/reserved-memory");
int node;
uint32_t addresses[4];
int ac, sc;
unsigned int idx = 0;
@ -213,6 +214,24 @@ int fdt_add_reserved_memory(void *dtb, const char *node_name,
fdt_setprop(dtb, offs, "ranges", NULL, 0);
}
/* Check for existing regions */
fdt_for_each_subnode(node, dtb, offs) {
uintptr_t c_base;
size_t c_size;
int ret;
ret = fdt_get_reg_props_by_index(dtb, node, 0, &c_base, &c_size);
/* Ignore illegal subnodes */
if (ret != 0) {
continue;
}
/* existing region entirely contains the new region */
if (base >= c_base && (base + size) <= (c_base + c_size)) {
return 0;
}
}
if (ac > 1) {
addresses[idx] = cpu_to_fdt32(HIGH_BITS(base));
idx++;

View file

@ -116,6 +116,7 @@ BL31_SOURCES += plat/xilinx/common/plat_fdt.c \
plat/xilinx/common/versal.c \
${PLAT_PATH}/bl31_setup.c \
common/fdt_fixup.c \
common/fdt_wrappers.c \
${LIBFDT_SRCS} \
${PLAT_PATH}/sip_svc_setup.c \
${PLAT_PATH}/gicv3.c

View file

@ -31,6 +31,7 @@ BL31_SOURCES += lib/cpus/aarch64/cortex_a72.S \
plat/common/plat_psci_common.c \
plat/rpi/common/rpi3_topology.c \
common/fdt_fixup.c \
common/fdt_wrappers.c \
${LIBFDT_SRCS} \
${GICV2_SOURCES}