From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: eagle.alexander923@gmail.com, Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master v2 2/5] arch: introduce CONFIG_BAREBOX_MEMORY_OFFSET
Date: Wed, 27 May 2026 14:15:21 +0200 [thread overview]
Message-ID: <20260527121649.3365172-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20260527121649.3365172-1-a.fatoum@pengutronix.de>
With barebox proper placed at end of RAM, the current CONFIG_MALLOC_SIZE
puts us into a tight spot: We must determine the size barebox proper
will occupy to subtract CONFIG_MALLOC_SIZE from it, but we may not know
the size yet in early stages of PBL, especially when we have separate
first and second stages.
This complexity in calculation is imposed onto all users, even though
most users probably don't make use of CONFIG_MALLOC_SIZE and set it to
zero anyway to let barebox compute a suitable malloc area size.
CONFIG_BAREBOX_MEMORY_OFFSET simplifies calculation by starting the
malloc area at the specified offset from start of RAM.
Boards that used to set CONFIG_MALLOC_SIZE will need to set the new
CONFIG_BAREBOX_MEMORY_OFFSET, e.g. by converting the old
CONFIG_MALLOC_SIZE value:
initial memory_size - old CONFIG_MALLOC_SIZE
- the area barebox occupies at end of initial memory
For all other boards, they can just keep the default of
CONFIG_BAREBOX_MEMORY_OFFSET=0 and barebox will start the malloc area at
half of RAM or 1G before end, whichever is smaller.
This computation is independent of barebox size and can thus be used in
early PBL as well in a later commit.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
arch/Kconfig | 13 ++++++++++
common/Kconfig | 38 +++++++++++++++++++++++++++++
common/memory.c | 4 +++
include/asm-generic/memory_layout.h | 24 ++++++++++++++++++
4 files changed, 79 insertions(+)
diff --git a/arch/Kconfig b/arch/Kconfig
index 23e65d58d52b..b4fe34aa9760 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -98,6 +98,19 @@ config ARCH_HAS_MALLOC_SIZE
This is selected by architectures, where CONFIG_MALLOC_SIZE
can be used to specify an exact size of the malloc area.
+ Eventually, this should only be selected by sandbox, with
+ everyone else switched over to ARCH_HAS_BAREBOX_MEMORY_OFFSET.
+
+config ARCH_HAS_BAREBOX_MEMORY_OFFSET
+ bool
+ help
+ This is selected by architectures, where CONFIG_BAREBOX_MEMORY_OFFSET
+ can be used to specify the start offset of the barebox memory
+ within the initially available memory (first memory bank usually).
+
+ Compared to CONFIG_MALLOC_SIZE, this offset simplifies memory layout
+ calculation and allows to reserve the malloc region very early on.
+
config HAVE_EFFICIENT_UNALIGNED_ACCESS
bool
diff --git a/common/Kconfig b/common/Kconfig
index 1b2f12498355..f9985d8aa4b5 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -300,6 +300,44 @@ config MALLOC_SIZE
default 0
prompt "malloc area size" if ARCH_HAS_MALLOC_SIZE
+config BAREBOX_MEMORY_OFFSET
+ hex
+ default 0
+ prompt "relative malloc base offset (optional)"
+ depends on ARCH_HAS_BAREBOX_MEMORY_OFFSET
+ help
+ Most boards should just leave this at the default zero and barebox
+ will dynamically determine the offset, from the start of the initial
+ memory bank in DRAM, at which the barebox runtime region begins.
+
+ Memory below this offset is reserved as the OS load region, used for
+ placement of the kernel, initrd and device tree before boot.
+
+ barebox itself, the relocated binary, BSS, stack and malloc area
+ all live at and above this offset and will not allocate into the
+ OS load region.
+
+ When set to zero, following heuristic will be used:
+
+ - If the prebootloader reports 2GiB of initial memory or more,
+ start the barebox runtime region at offset 1GiB from
+ start of initial memory.
+
+ - Otherwise, start the barebox runtimer region right in the
+ middle of the initial memory region.
+
+ This heuristic should be suitable for most boards with modern DRAM
+ sizes. If you run into problems with it, adapt this option, so
+ it is large enough to hold the biggest OS image you expect to boot,
+ plus initramfs and DTB.
+
+ Note that this affects only the split in the initial memory reported
+ by the bootloader. Placement of final boot artifacts can happen
+ outside of the malloc area and are not restricted to this initial
+ memory or even the first SDRAM bank.
+
+ If unsure, keep the default of 0x0.
+
config SCRATCH_SIZE
hex
default 0x8000
diff --git a/common/memory.c b/common/memory.c
index bf927b6a30f3..e436707cf8da 100644
--- a/common/memory.c
+++ b/common/memory.c
@@ -56,6 +56,10 @@ void mem_malloc_init(void *start, void *end)
static_assert(PAGE_ALIGNED(CONFIG_MALLOC_SIZE));
+#ifdef CONFIG_BAREBOX_MEMORY_OFFSET
+static_assert(PAGE_ALIGNED(CONFIG_BAREBOX_MEMORY_OFFSET));
+#endif
+
static struct resource *barebox_res;
static resource_size_t barebox_start;
static resource_size_t barebox_size;
diff --git a/include/asm-generic/memory_layout.h b/include/asm-generic/memory_layout.h
index d477f7bbdbf7..1df7aec18739 100644
--- a/include/asm-generic/memory_layout.h
+++ b/include/asm-generic/memory_layout.h
@@ -36,6 +36,12 @@
#define STACK_SIZE CONFIG_STACK_SIZE
#define SCRATCH_SIZE CONFIG_SCRATCH_SIZE
+#ifndef __ASSEMBLY__
+
+#include <linux/pagemap.h>
+#include <linux/minmax.h>
+#include <linux/sizes.h>
+
/*
* This generates a useless load from the specified symbol
* to ensure linker garbage collection doesn't delete it
@@ -43,4 +49,22 @@
#define __keep_symbolref(sym) \
__asm__ __volatile__("": :"r"(&sym) :)
+#ifdef CONFIG_BAREBOX_MEMORY_OFFSET
+static inline unsigned long barebox_malloc_base(unsigned long membase,
+ unsigned long memsize)
+ {
+ unsigned long offset = CONFIG_BAREBOX_MEMORY_OFFSET;
+
+ if (offset >= memsize)
+ offset = 0;
+
+ if (!offset)
+ offset = memsize - min_t(unsigned long, memsize / 2, SZ_1G);
+
+ return PAGE_ALIGN(membase + offset);
+}
+#endif
+
+#endif /* __ASSEMBLY__ */
+
#endif /* __ASM_GENERIC_MEMORY_LAYOUT_H */
--
2.47.3
next prev parent reply other threads:[~2026-05-27 12:18 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-27 12:15 [PATCH master v2 0/5] ARM64: unify pbl and proper malloc area start Ahmad Fatoum
2026-05-27 12:15 ` [PATCH master v2 1/5] arch: introduce new CONFIG_ARCH_HAS_MALLOC_SIZE Ahmad Fatoum
2026-05-27 12:15 ` Ahmad Fatoum [this message]
2026-05-27 12:15 ` [PATCH master v2 3/5] ARM64: switch to CONFIG_BAREBOX_MEMORY_OFFSET Ahmad Fatoum
2026-05-27 12:15 ` [PATCH master v2 4/5] ARM64: configs: drop CONFIG_MALLOC_SIZE=0x0 as it's now the default Ahmad Fatoum
2026-05-27 12:15 ` [PATCH master v2 5/5] ARM64: place PBL malloc area at start of barebox proper malloc area Ahmad Fatoum
2026-05-27 13:12 ` Alexander Shiyan
2026-05-28 11:04 ` Ahmad Fatoum
2026-05-28 12:17 ` Alexander Shiyan
2026-05-28 12:50 ` Ahmad Fatoum
2026-05-28 10:55 ` [PATCH master v2] fixup! " Ahmad Fatoum
2026-05-28 12:16 ` Alexander Shiyan
2026-05-28 11:09 ` [PATCH master v2 0/5] ARM64: unify pbl and proper malloc area start Sascha Hauer
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=20260527121649.3365172-3-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=eagle.alexander923@gmail.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox