From: <rs@ti.com>
To: <robertcnelson@gmail.com>, <ayush@beagleboard.org>,
<Erik.Welsh@octavosystems.com>, <anshuld@ti.com>, <bb@ti.com>,
<trini@konsulko.com>, <afd@ti.com>, <xypron.glpk@gmx.de>,
<ilias.apalodimas@linaro.org>, <sjg@chromium.org>
Cc: <u-boot@lists.denx.de>
Subject: [PATCHv8 1/3] boot: image-fdt: free old dtb reservations
Date: Wed, 13 May 2026 15:49:41 -0500 [thread overview]
Message-ID: <20260513204943.736142-2-rs@ti.com> (raw)
In-Reply-To: <20260513204943.736142-1-rs@ti.com>
From: Randolph Sapp <rs@ti.com>
Add a free flag and an initial call to free allocations covered by the
global FDT. This assumes that all calls to boot_fdt_add_mem_rsv_regions
occur before the transition to the new device tree, thus we can access
the currently active device tree through the global data pointer.
This allows us to clearly indicate to the user when a device tree
reservation fails. How we handle this can still use some improvement.
Right now we'll keep the default behavior and try to boot anyway.
Fixes: 5a6aa7d5913 ("boot: fdt: Handle already reserved memory in boot_fdt_reserve_region()")
Signed-off-by: Randolph Sapp <rs@ti.com>
Acked-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
---
v7:
- Add disclaimer to boot_fdt_add_mem_rsv_regions
- Add static boot_fdt_handle_mem_rsv_regions as a generic walker for the
two calls into boot_fdt_add_mem_rsv_regions
- Make boot_fdt_add_mem_rsv_regions fdt pointer const
v8:
- Convert ref to fixes tag as requested
boot/image-fdt.c | 77 +++++++++++++++++++++++++++++++++++-------------
include/image.h | 2 +-
2 files changed, 58 insertions(+), 21 deletions(-)
diff --git a/boot/image-fdt.c b/boot/image-fdt.c
index a3a4fb8b558..1150131a11e 100644
--- a/boot/image-fdt.c
+++ b/boot/image-fdt.c
@@ -69,35 +69,50 @@ static const struct legacy_img_hdr *image_get_fdt(ulong fdt_addr)
}
#endif
-static void boot_fdt_reserve_region(u64 addr, u64 size, u32 flags)
+/**
+ * boot_fdt_handle_region - Reserve or free a given FDT region in LMB
+ * @addr: Reservation base address
+ * @size: Reservation size
+ * @flags: Reservation flags
+ * @free: Indicate if region is being freed or allocated
+ *
+ * Add or free a given reservation from LMB. This reports to the user if any
+ * errors occurred during either operation.
+ */
+static void boot_fdt_handle_region(u64 addr, u64 size, u32 flags, bool free)
{
long ret;
phys_addr_t rsv_addr;
rsv_addr = (phys_addr_t)addr;
- ret = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &rsv_addr, size, flags);
+ if (free)
+ ret = lmb_free(rsv_addr, size, flags);
+ else
+ ret = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &rsv_addr, size,
+ flags);
+
if (!ret) {
- debug(" reserving fdt memory region: addr=%llx size=%llx flags=%x\n",
- (unsigned long long)addr,
+ debug(" %s fdt memory region: addr=%llx size=%llx flags=%x\n",
+ free ? "freed" : "reserved", (unsigned long long)addr,
(unsigned long long)size, flags);
- } else if (ret != -EEXIST && ret != -EINVAL) {
- puts("ERROR: reserving fdt memory region failed ");
- printf("(addr=%llx size=%llx flags=%x)\n",
- (unsigned long long)addr,
- (unsigned long long)size, flags);
+ } else {
+ printf("ERROR: %s fdt memory region failed (addr=%llx size=%llx flags=%x): %ld\n",
+ free ? "freeing" : "reserving", (unsigned long long)addr,
+ (unsigned long long)size, flags, ret);
}
}
/**
- * boot_fdt_add_mem_rsv_regions - Mark the memreserve and reserved-memory
- * sections as unusable
+ * boot_fdt_handle_mem_rsv_regions - Handle FDT memreserve and reserved-memory
+ * sections
* @fdt_blob: pointer to fdt blob base address
+ * @free: indicate if regions are being freed
*
- * Adds the and reserved-memorymemreserve regions in the dtb to the lmb block.
- * Adding the memreserve regions prevents u-boot from using them to store the
- * initrd or the fdt blob.
+ * Adds or removes reserved-memory and memreserve regions in the dtb to the lmb
+ * block. Adding the memreserve regions prevents u-boot from using them to store
+ * the initrd or the fdt blob.
*/
-void boot_fdt_add_mem_rsv_regions(void *fdt_blob)
+static void boot_fdt_handle_mem_rsv_regions(const void *fdt_blob, bool free)
{
uint64_t addr, size;
int i, total, ret;
@@ -105,15 +120,12 @@ void boot_fdt_add_mem_rsv_regions(void *fdt_blob)
struct fdt_resource res;
u32 flags;
- if (fdt_check_header(fdt_blob) != 0)
- return;
-
/* process memreserve sections */
total = fdt_num_mem_rsv(fdt_blob);
for (i = 0; i < total; i++) {
if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
continue;
- boot_fdt_reserve_region(addr, size, LMB_NOOVERWRITE);
+ boot_fdt_handle_region(addr, size, LMB_NOOVERWRITE, free);
}
/* process reserved-memory */
@@ -131,7 +143,7 @@ void boot_fdt_add_mem_rsv_regions(void *fdt_blob)
flags = LMB_NOMAP;
addr = res.start;
size = res.end - res.start + 1;
- boot_fdt_reserve_region(addr, size, flags);
+ boot_fdt_handle_region(addr, size, flags, free);
}
subnode = fdt_next_subnode(fdt_blob, subnode);
@@ -139,6 +151,31 @@ void boot_fdt_add_mem_rsv_regions(void *fdt_blob)
}
}
+/**
+ * boot_fdt_add_mem_rsv_regions - Add FDT memreserve and reserved-memory
+ * sections
+ * @fdt_blob: pointer to fdt blob base address
+ *
+ * Adds reserved-memory and memreserve regions in the dtb to the lmb block.
+ * Adding the memreserve regions prevents u-boot from using them to store the
+ * initrd or the fdt blob.
+ *
+ * This function will attempt to clean the currently active reservations if a
+ * new device tree blob is given. This must be called before gd->fdt_blob is
+ * switched.
+ */
+void boot_fdt_add_mem_rsv_regions(const void *fdt_blob)
+{
+ if (fdt_check_header(fdt_blob) != 0)
+ return;
+
+ /* Remove old regions */
+ if (gd->fdt_blob != fdt_blob)
+ boot_fdt_handle_mem_rsv_regions(gd->fdt_blob, true);
+
+ boot_fdt_handle_mem_rsv_regions(fdt_blob, false);
+}
+
/**
* boot_relocate_fdt - relocate flat device tree
* @of_flat_tree: pointer to a char* variable, will hold fdt start address
diff --git a/include/image.h b/include/image.h
index 34efac6056d..151619f42bf 100644
--- a/include/image.h
+++ b/include/image.h
@@ -827,7 +827,7 @@ int boot_get_fdt(void *buf, const char *select, uint arch,
struct bootm_headers *images, char **of_flat_tree,
ulong *of_size);
-void boot_fdt_add_mem_rsv_regions(void *fdt_blob);
+void boot_fdt_add_mem_rsv_regions(const void *fdt_blob);
int boot_relocate_fdt(char **of_flat_tree, ulong *of_size);
int boot_ramdisk_high(ulong rd_data, ulong rd_len, ulong *initrd_start,
--
2.54.0
next prev parent reply other threads:[~2026-05-13 20:50 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-13 20:49 [PATCHv8 0/3] various memory related fixups rs
2026-05-13 20:49 ` rs [this message]
2026-05-13 20:49 ` [PATCHv8 2/3] test: boot: add a fdt reserved region check rs
2026-05-15 7:33 ` Ilias Apalodimas
2026-05-15 14:03 ` Simon Glass
2026-05-13 20:49 ` [PATCHv8 3/3] memory: reserve from start_addr_sp to initial_relocaddr rs
2026-05-15 7:38 ` Ilias Apalodimas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260513204943.736142-2-rs@ti.com \
--to=rs@ti.com \
--cc=Erik.Welsh@octavosystems.com \
--cc=afd@ti.com \
--cc=anshuld@ti.com \
--cc=ayush@beagleboard.org \
--cc=bb@ti.com \
--cc=ilias.apalodimas@linaro.org \
--cc=robertcnelson@gmail.com \
--cc=sjg@chromium.org \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
--cc=xypron.glpk@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.