mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] pbl: cleanup early malloc
@ 2026-05-18 15:32 Sascha Hauer
  2026-05-18 15:32 ` [PATCH 2/2] ARM: rockchip: initialize PBL malloc Sascha Hauer
  2026-05-18 16:14 ` [PATCH 1/2] pbl: cleanup early malloc Ahmad Fatoum
  0 siblings, 2 replies; 6+ messages in thread
From: Sascha Hauer @ 2026-05-18 15:32 UTC (permalink / raw)
  To: Barebox List

All architectures using PBL define the same variables free_mem_ptr and
free_mem_end_ptr and initialize them. create a helper for that in
pbl/malloc.c and rename simple_*() to pbl_*().

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/cpu/uncompress.c              |  6 +---
 arch/mips/boot/uncompress.c            |  6 +---
 arch/riscv/boot/uncompress.c           |  6 +---
 arch/riscv/include/asm/barebox-riscv.h |  6 ----
 include/linux/decompress/mm.h          | 44 ++++----------------------
 include/mach/bcm283x/debug_ll.h        | 10 ------
 include/pbl.h                          |  7 ++--
 pbl/Makefile                           |  1 +
 pbl/malloc.c                           | 43 +++++++++++++++++++++++++
 9 files changed, 57 insertions(+), 72 deletions(-)
 create mode 100644 pbl/malloc.c

diff --git a/arch/arm/cpu/uncompress.c b/arch/arm/cpu/uncompress.c
index 38f7dbc113..c7802226e6 100644
--- a/arch/arm/cpu/uncompress.c
+++ b/arch/arm/cpu/uncompress.c
@@ -27,9 +27,6 @@
 
 #include "entry.h"
 
-unsigned long free_mem_ptr;
-unsigned long free_mem_end_ptr;
-
 extern unsigned char input_data[];
 extern unsigned char input_data_end[];
 
@@ -78,8 +75,7 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
 
 	handoff_data = (void *)barebox_base + uncompressed_len + MAX_BSS_SIZE;
 
-	free_mem_ptr = barebox_base - ARM_MEM_EARLY_MALLOC_SIZE;
-	free_mem_end_ptr = barebox_base;
+	pbl_malloc_init(barebox_base - ARM_MEM_EARLY_MALLOC_SIZE, ARM_MEM_EARLY_MALLOC_SIZE);
 
 #ifdef DEBUG
 	print_pbl_mem_layout(membase, endmem, barebox_base);
diff --git a/arch/mips/boot/uncompress.c b/arch/mips/boot/uncompress.c
index 0630d03c6b..b58b23ea6b 100644
--- a/arch/mips/boot/uncompress.c
+++ b/arch/mips/boot/uncompress.c
@@ -16,9 +16,6 @@
 extern void *input_data;
 extern void *input_data_end;
 
-unsigned long free_mem_ptr;
-unsigned long free_mem_end_ptr;
-
 void barebox_pbl_start(void *fdt, void *fdt_end, unsigned long ram_size);
 
 void __section(.text_entry) barebox_pbl_start(void *fdt, void *fdt_end,
@@ -34,8 +31,7 @@ void __section(.text_entry) barebox_pbl_start(void *fdt, void *fdt_end,
 	memset(__bss_start, 0, __bss_stop - __bss_start);
 
 	/* set 128 KiB at the end of the MALLOC_BASE for early malloc */
-	free_mem_ptr = TEXT_BASE - SZ_128K;
-	free_mem_end_ptr = free_mem_ptr + SZ_128K;
+	pbl_malloc_init(barebox_base - TEXT_BASE - SZ_128K, SZ_128K);
 
 	piggy_len = (unsigned long)&input_data_end - (unsigned long)&input_data;
 
diff --git a/arch/riscv/boot/uncompress.c b/arch/riscv/boot/uncompress.c
index e51f1b0121..df60b14ed8 100644
--- a/arch/riscv/boot/uncompress.c
+++ b/arch/riscv/boot/uncompress.c
@@ -23,9 +23,6 @@
 
 #include "entry.h"
 
-unsigned long free_mem_ptr;
-unsigned long free_mem_end_ptr;
-
 void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
 				  void *fdt)
 {
@@ -63,8 +60,7 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
 
 	pr_debug("memory at 0x%08lx, size 0x%08lx\n", membase, memsize);
 
-	free_mem_ptr = riscv_mem_early_malloc(membase, endmem);
-	free_mem_end_ptr = riscv_mem_early_malloc_end(membase, endmem);
+	pbl_malloc_init(riscv_mem_early_malloc(), SZ_128K);
 
 	/*
 	 * Enable MMU early to enable caching for faster decompression.
diff --git a/arch/riscv/include/asm/barebox-riscv.h b/arch/riscv/include/asm/barebox-riscv.h
index 083889817b..9fef0c213a 100644
--- a/arch/riscv/include/asm/barebox-riscv.h
+++ b/arch/riscv/include/asm/barebox-riscv.h
@@ -57,12 +57,6 @@ static inline unsigned long riscv_mem_early_malloc(unsigned long membase,
 	return riscv_mem_stack(membase, endmem) - SZ_128K;
 }
 
-static inline unsigned long riscv_mem_early_malloc_end(unsigned long membase,
-						       unsigned long endmem)
-{
-	return riscv_mem_stack(membase, endmem);
-}
-
 static inline unsigned long riscv_mem_ramoops(unsigned long membase,
 					      unsigned long endmem)
 {
diff --git a/include/linux/decompress/mm.h b/include/linux/decompress/mm.h
index 1891a51368..27b13ec9c8 100644
--- a/include/linux/decompress/mm.h
+++ b/include/linux/decompress/mm.h
@@ -14,6 +14,8 @@
 
 #ifdef STATIC
 
+#include <pbl.h>
+
 /* Code active when included from pre-boot environment: */
 
 /*
@@ -26,45 +28,11 @@
 #define STATIC_RW_DATA static
 #endif
 
-/* A trivial malloc implementation, adapted from
- *  malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
- */
-STATIC_RW_DATA unsigned long malloc_ptr;
-STATIC_RW_DATA int malloc_count;
-
-static __maybe_unused void *simple_malloc(int size)
-{
-	void *p;
-
-	if (size < 0)
-		return NULL;
-	if (!malloc_ptr)
-		malloc_ptr = free_mem_ptr;
-
-	malloc_ptr = (malloc_ptr + 3) & ~3;     /* Align */
-
-	p = (void *)malloc_ptr;
-	malloc_ptr += size;
-
-	if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
-		return NULL;
-
-	malloc_count++;
-	return p;
-}
-
-static __maybe_unused void simple_free(void *where)
-{
-	malloc_count--;
-	if (!malloc_count)
-		malloc_ptr = free_mem_ptr;
-}
-
-#define large_malloc(a) simple_malloc(a)
-#define large_free(a) simple_free(a)
+#define large_malloc(a) pbl_malloc(a)
+#define large_free(a) pbl_free(a)
 
-#define MALLOC simple_malloc
-#define FREE simple_free
+#define MALLOC pbl_malloc
+#define FREE pbl_free
 
 #else
 
diff --git a/include/mach/bcm283x/debug_ll.h b/include/mach/bcm283x/debug_ll.h
index 8bbff4fd9a..68542fd6a3 100644
--- a/include/mach/bcm283x/debug_ll.h
+++ b/include/mach/bcm283x/debug_ll.h
@@ -42,16 +42,6 @@ static inline void debug_ll_init(void)
 
 #elif defined CONFIG_DEBUG_RPI3_MINI_UART
 
-static inline uint8_t debug_ll_read_reg(void __iomem *base, int reg)
-{
-	return readb(base + (reg << 2));
-}
-
-static inline void debug_ll_write_reg(void __iomem *base, int reg, uint8_t val)
-{
-	writeb(val, base + (reg << 2));
-}
-
 #include <linux/bits.h>
 
 #define BCM2836_AUX_CLOCK_ENB		0x3f215004 /* BCM2835 AUX Clock enable register */
diff --git a/include/pbl.h b/include/pbl.h
index 514f114370..e30db16088 100644
--- a/include/pbl.h
+++ b/include/pbl.h
@@ -11,9 +11,6 @@
 #include <linux/types.h>
 #include <linux/compiler.h>
 
-extern unsigned long free_mem_ptr;
-extern unsigned long free_mem_end_ptr;
-
 void pbl_barebox_uncompress(void *dest, void *compressed_start, unsigned int len);
 int pbl_dtbz_uncompress(void *dest, void *compressed_start, unsigned long len);
 
@@ -33,6 +30,10 @@ int pbl_barebox_verify(const void *compressed_start, unsigned int len,
 		       const void *hash, unsigned int hash_len);
 int pbl_load_fdt(const void *fdt, void *dest, int destsize);
 
+void *pbl_malloc(int size);
+void pbl_free(void *where);
+void pbl_malloc_init(unsigned long base, size_t size);
+
 #endif
 
 void __noreturn barebox_pbl_entry(ulong, ulong, void *);
diff --git a/pbl/Makefile b/pbl/Makefile
index b78124cdcd..45cfbf5fba 100644
--- a/pbl/Makefile
+++ b/pbl/Makefile
@@ -5,6 +5,7 @@
 #
 pbl-y += misc.o
 pbl-y += string.o
+pbl-y += malloc.o
 pbl-$(CONFIG_HAVE_IMAGE_COMPRESSION) += decomp.o
 pbl-$(CONFIG_LIBFDT) += fdt.o
 pbl-$(CONFIG_PBL_CONSOLE) += console.o
diff --git a/pbl/malloc.c b/pbl/malloc.c
new file mode 100644
index 0000000000..d5ee8ac7ee
--- /dev/null
+++ b/pbl/malloc.c
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <pbl.h>
+
+static unsigned long free_mem_ptr;
+static unsigned long free_mem_end_ptr;
+static unsigned long malloc_ptr;
+static int malloc_count;
+
+void *pbl_malloc(int size)
+{
+	void *p;
+
+	if (size < 0)
+		return NULL;
+	if (!malloc_ptr)
+		malloc_ptr = free_mem_ptr;
+
+	malloc_ptr = (malloc_ptr + 3) & ~3;     /* Align */
+
+	p = (void *)malloc_ptr;
+	malloc_ptr += size;
+
+	if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
+		return NULL;
+
+	malloc_count++;
+	return p;
+}
+
+void pbl_free(void *where)
+{
+	malloc_count--;
+	if (!malloc_count)
+		malloc_ptr = free_mem_ptr;
+}
+
+void pbl_malloc_init(unsigned long base, size_t size)
+{
+	free_mem_ptr = base;
+	malloc_ptr = base;
+	malloc_count = 0;
+	free_mem_end_ptr = base + size;
+}
\ No newline at end of file
-- 
2.47.3




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/2] ARM: rockchip: initialize PBL malloc
  2026-05-18 15:32 [PATCH 1/2] pbl: cleanup early malloc Sascha Hauer
@ 2026-05-18 15:32 ` Sascha Hauer
  2026-05-18 16:20   ` Ahmad Fatoum
  2026-05-18 16:14 ` [PATCH 1/2] pbl: cleanup early malloc Ahmad Fatoum
  1 sibling, 1 reply; 6+ messages in thread
From: Sascha Hauer @ 2026-05-18 15:32 UTC (permalink / raw)
  To: Barebox List

Rockchip uses compressed firmware binaries. Depending on the compression
algorithm we might use early malloc space for decompressing. Initialize
the PBL malloc space to the memory end to make pbl_malloc() work. This
memory area will be overwritten later in barebox_pbl_start(), but it's
ok as the memory is only needed during decompression of the firmware
files.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/mach-rockchip/atf.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/arm/mach-rockchip/atf.c b/arch/arm/mach-rockchip/atf.c
index 3e2e18ac29..03e03c9122 100644
--- a/arch/arm/mach-rockchip/atf.c
+++ b/arch/arm/mach-rockchip/atf.c
@@ -173,6 +173,7 @@ static void rockchip_atf_load_bl31(void *fdt)
 	unsigned long bl31_ep;
 
 	mmu_early_enable(membase[0], memsize[0]);
+	pbl_malloc_init(membase[0] + memsize[0] - SZ_128K, SZ_128K);
 
 	bl31_ep = load_elf64_image_phdr(&bl31);
 
-- 
2.47.3




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] pbl: cleanup early malloc
  2026-05-18 15:32 [PATCH 1/2] pbl: cleanup early malloc Sascha Hauer
  2026-05-18 15:32 ` [PATCH 2/2] ARM: rockchip: initialize PBL malloc Sascha Hauer
@ 2026-05-18 16:14 ` Ahmad Fatoum
  2026-05-18 18:15   ` Sascha Hauer
  1 sibling, 1 reply; 6+ messages in thread
From: Ahmad Fatoum @ 2026-05-18 16:14 UTC (permalink / raw)
  To: Sascha Hauer, Barebox List

On 5/18/26 5:32 PM, Sascha Hauer wrote:
> All architectures using PBL define the same variables free_mem_ptr and
> free_mem_end_ptr and initialize them. create a helper for that in
> pbl/malloc.c and rename simple_*() to pbl_*().
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>

Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

with issues below addressed

> ---
>  arch/arm/cpu/uncompress.c              |  6 +---
>  arch/mips/boot/uncompress.c            |  6 +---
>  arch/riscv/boot/uncompress.c           |  6 +---
>  arch/riscv/include/asm/barebox-riscv.h |  6 ----
>  include/linux/decompress/mm.h          | 44 ++++----------------------
>  include/mach/bcm283x/debug_ll.h        | 10 ------
>  include/pbl.h                          |  7 ++--
>  pbl/Makefile                           |  1 +
>  pbl/malloc.c                           | 43 +++++++++++++++++++++++++
>  9 files changed, 57 insertions(+), 72 deletions(-)
>  create mode 100644 pbl/malloc.c
> 
> diff --git a/arch/arm/cpu/uncompress.c b/arch/arm/cpu/uncompress.c
> index 38f7dbc113..c7802226e6 100644
> --- a/arch/arm/cpu/uncompress.c
> +++ b/arch/arm/cpu/uncompress.c
> @@ -27,9 +27,6 @@
>  
>  #include "entry.h"
>  
> -unsigned long free_mem_ptr;
> -unsigned long free_mem_end_ptr;
> -
>  extern unsigned char input_data[];
>  extern unsigned char input_data_end[];
>  
> @@ -78,8 +75,7 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
>  
>  	handoff_data = (void *)barebox_base + uncompressed_len + MAX_BSS_SIZE;
>  
> -	free_mem_ptr = barebox_base - ARM_MEM_EARLY_MALLOC_SIZE;
> -	free_mem_end_ptr = barebox_base;
> +	pbl_malloc_init(barebox_base - ARM_MEM_EARLY_MALLOC_SIZE, ARM_MEM_EARLY_MALLOC_SIZE);
>  
>  #ifdef DEBUG
>  	print_pbl_mem_layout(membase, endmem, barebox_base);
> diff --git a/arch/mips/boot/uncompress.c b/arch/mips/boot/uncompress.c
> index 0630d03c6b..b58b23ea6b 100644
> --- a/arch/mips/boot/uncompress.c
> +++ b/arch/mips/boot/uncompress.c
> @@ -16,9 +16,6 @@
>  extern void *input_data;
>  extern void *input_data_end;
>  
> -unsigned long free_mem_ptr;
> -unsigned long free_mem_end_ptr;
> -
>  void barebox_pbl_start(void *fdt, void *fdt_end, unsigned long ram_size);
>  
>  void __section(.text_entry) barebox_pbl_start(void *fdt, void *fdt_end,
> @@ -34,8 +31,7 @@ void __section(.text_entry) barebox_pbl_start(void *fdt, void *fdt_end,
>  	memset(__bss_start, 0, __bss_stop - __bss_start);
>  
>  	/* set 128 KiB at the end of the MALLOC_BASE for early malloc */
> -	free_mem_ptr = TEXT_BASE - SZ_128K;
> -	free_mem_end_ptr = free_mem_ptr + SZ_128K;
> +	pbl_malloc_init(barebox_base - TEXT_BASE - SZ_128K, SZ_128K);
>  
>  	piggy_len = (unsigned long)&input_data_end - (unsigned long)&input_data;
>  
> diff --git a/arch/riscv/boot/uncompress.c b/arch/riscv/boot/uncompress.c
> index e51f1b0121..df60b14ed8 100644
> --- a/arch/riscv/boot/uncompress.c
> +++ b/arch/riscv/boot/uncompress.c
> @@ -23,9 +23,6 @@
>  
>  #include "entry.h"
>  
> -unsigned long free_mem_ptr;
> -unsigned long free_mem_end_ptr;
> -
>  void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
>  				  void *fdt)
>  {
> @@ -63,8 +60,7 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
>  
>  	pr_debug("memory at 0x%08lx, size 0x%08lx\n", membase, memsize);
>  
> -	free_mem_ptr = riscv_mem_early_malloc(membase, endmem);
> -	free_mem_end_ptr = riscv_mem_early_malloc_end(membase, endmem);
> +	pbl_malloc_init(riscv_mem_early_malloc(), SZ_128K);

This is a bit dangerous as it now hardcodes SZ_128K at two places and
one could be missed when trying to extend the region.

Could you #define RISCV_MEM_EARLY_MALLOC_SIZE SZ_128K in the same patch?


> diff --git a/include/mach/bcm283x/debug_ll.h b/include/mach/bcm283x/debug_ll.h
> index 8bbff4fd9a..68542fd6a3 100644
> --- a/include/mach/bcm283x/debug_ll.h
> +++ b/include/mach/bcm283x/debug_ll.h
> @@ -42,16 +42,6 @@ static inline void debug_ll_init(void)
>  
>  #elif defined CONFIG_DEBUG_RPI3_MINI_UART
>  
> -static inline uint8_t debug_ll_read_reg(void __iomem *base, int reg)
> -{
> -	return readb(base + (reg << 2));
> -}
> -
> -static inline void debug_ll_write_reg(void __iomem *base, int reg, uint8_t val)
> -{
> -	writeb(val, base + (reg << 2));
> -}
> -

Unrelated change?

Thanks,
Ahmad

-- 
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] ARM: rockchip: initialize PBL malloc
  2026-05-18 15:32 ` [PATCH 2/2] ARM: rockchip: initialize PBL malloc Sascha Hauer
@ 2026-05-18 16:20   ` Ahmad Fatoum
  2026-05-18 18:24     ` Sascha Hauer
  0 siblings, 1 reply; 6+ messages in thread
From: Ahmad Fatoum @ 2026-05-18 16:20 UTC (permalink / raw)
  To: Sascha Hauer, Barebox List

Hello,

On 5/18/26 5:32 PM, Sascha Hauer wrote:
> Rockchip uses compressed firmware binaries. Depending on the compression
> algorithm we might use early malloc space for decompressing. Initialize
> the PBL malloc space to the memory end to make pbl_malloc() work. This
> memory area will be overwritten later in barebox_pbl_start(), but it's
> ok as the memory is only needed during decompression of the firmware
> files.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>

Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Tested-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

> ---
>  arch/arm/mach-rockchip/atf.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/arch/arm/mach-rockchip/atf.c b/arch/arm/mach-rockchip/atf.c
> index 3e2e18ac29..03e03c9122 100644
> --- a/arch/arm/mach-rockchip/atf.c
> +++ b/arch/arm/mach-rockchip/atf.c
> @@ -173,6 +173,7 @@ static void rockchip_atf_load_bl31(void *fdt)
>  	unsigned long bl31_ep;
>  
>  	mmu_early_enable(membase[0], memsize[0]);
> +	pbl_malloc_init(membase[0] + memsize[0] - SZ_128K, SZ_128K);

It would be cool if we could use the same malloc area that would be used
in barebox_pbl_start(), but I guess that's not possible, because it's
unfortunately based on the barebox proper load address, which is now
known here. Let's hope this won't clash with any future use..

Thanks,
Ahmad

>  
>  	bl31_ep = load_elf64_image_phdr(&bl31);
>  

-- 
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] pbl: cleanup early malloc
  2026-05-18 16:14 ` [PATCH 1/2] pbl: cleanup early malloc Ahmad Fatoum
@ 2026-05-18 18:15   ` Sascha Hauer
  0 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2026-05-18 18:15 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: Barebox List

On 2026-05-18 18:14, Ahmad Fatoum wrote:
> On 5/18/26 5:32 PM, Sascha Hauer wrote:
> > All architectures using PBL define the same variables free_mem_ptr and
> > free_mem_end_ptr and initialize them. create a helper for that in
> > pbl/malloc.c and rename simple_*() to pbl_*().
> > 
> > Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> 
> Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> 
> with issues below addressed
> 
> > ---
> >  arch/arm/cpu/uncompress.c              |  6 +---
> >  arch/mips/boot/uncompress.c            |  6 +---
> >  arch/riscv/boot/uncompress.c           |  6 +---
> >  arch/riscv/include/asm/barebox-riscv.h |  6 ----
> >  include/linux/decompress/mm.h          | 44 ++++----------------------
> >  include/mach/bcm283x/debug_ll.h        | 10 ------
> >  include/pbl.h                          |  7 ++--
> >  pbl/Makefile                           |  1 +
> >  pbl/malloc.c                           | 43 +++++++++++++++++++++++++
> >  9 files changed, 57 insertions(+), 72 deletions(-)
> >  create mode 100644 pbl/malloc.c
> > 
> > diff --git a/arch/arm/cpu/uncompress.c b/arch/arm/cpu/uncompress.c
> > index 38f7dbc113..c7802226e6 100644
> > --- a/arch/arm/cpu/uncompress.c
> > +++ b/arch/arm/cpu/uncompress.c
> > @@ -27,9 +27,6 @@
> >  
> >  #include "entry.h"
> >  
> > -unsigned long free_mem_ptr;
> > -unsigned long free_mem_end_ptr;
> > -
> >  extern unsigned char input_data[];
> >  extern unsigned char input_data_end[];
> >  
> > @@ -78,8 +75,7 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
> >  
> >  	handoff_data = (void *)barebox_base + uncompressed_len + MAX_BSS_SIZE;
> >  
> > -	free_mem_ptr = barebox_base - ARM_MEM_EARLY_MALLOC_SIZE;
> > -	free_mem_end_ptr = barebox_base;
> > +	pbl_malloc_init(barebox_base - ARM_MEM_EARLY_MALLOC_SIZE, ARM_MEM_EARLY_MALLOC_SIZE);
> >  
> >  #ifdef DEBUG
> >  	print_pbl_mem_layout(membase, endmem, barebox_base);
> > diff --git a/arch/mips/boot/uncompress.c b/arch/mips/boot/uncompress.c
> > index 0630d03c6b..b58b23ea6b 100644
> > --- a/arch/mips/boot/uncompress.c
> > +++ b/arch/mips/boot/uncompress.c
> > @@ -16,9 +16,6 @@
> >  extern void *input_data;
> >  extern void *input_data_end;
> >  
> > -unsigned long free_mem_ptr;
> > -unsigned long free_mem_end_ptr;
> > -
> >  void barebox_pbl_start(void *fdt, void *fdt_end, unsigned long ram_size);
> >  
> >  void __section(.text_entry) barebox_pbl_start(void *fdt, void *fdt_end,
> > @@ -34,8 +31,7 @@ void __section(.text_entry) barebox_pbl_start(void *fdt, void *fdt_end,
> >  	memset(__bss_start, 0, __bss_stop - __bss_start);
> >  
> >  	/* set 128 KiB at the end of the MALLOC_BASE for early malloc */
> > -	free_mem_ptr = TEXT_BASE - SZ_128K;
> > -	free_mem_end_ptr = free_mem_ptr + SZ_128K;
> > +	pbl_malloc_init(barebox_base - TEXT_BASE - SZ_128K, SZ_128K);
> >  
> >  	piggy_len = (unsigned long)&input_data_end - (unsigned long)&input_data;
> >  
> > diff --git a/arch/riscv/boot/uncompress.c b/arch/riscv/boot/uncompress.c
> > index e51f1b0121..df60b14ed8 100644
> > --- a/arch/riscv/boot/uncompress.c
> > +++ b/arch/riscv/boot/uncompress.c
> > @@ -23,9 +23,6 @@
> >  
> >  #include "entry.h"
> >  
> > -unsigned long free_mem_ptr;
> > -unsigned long free_mem_end_ptr;
> > -
> >  void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
> >  				  void *fdt)
> >  {
> > @@ -63,8 +60,7 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
> >  
> >  	pr_debug("memory at 0x%08lx, size 0x%08lx\n", membase, memsize);
> >  
> > -	free_mem_ptr = riscv_mem_early_malloc(membase, endmem);
> > -	free_mem_end_ptr = riscv_mem_early_malloc_end(membase, endmem);
> > +	pbl_malloc_init(riscv_mem_early_malloc(), SZ_128K);
> 
> This is a bit dangerous as it now hardcodes SZ_128K at two places and
> one could be missed when trying to extend the region.
> 
> Could you #define RISCV_MEM_EARLY_MALLOC_SIZE SZ_128K in the same patch?

All architectures use 128KiB and if we have to change it chances are
good that we have to change it for all architectures, so I'll add a
common define for this instead.

> 
> 
> > diff --git a/include/mach/bcm283x/debug_ll.h b/include/mach/bcm283x/debug_ll.h
> > index 8bbff4fd9a..68542fd6a3 100644
> > --- a/include/mach/bcm283x/debug_ll.h
> > +++ b/include/mach/bcm283x/debug_ll.h
> > @@ -42,16 +42,6 @@ static inline void debug_ll_init(void)
> >  
> >  #elif defined CONFIG_DEBUG_RPI3_MINI_UART
> >  
> > -static inline uint8_t debug_ll_read_reg(void __iomem *base, int reg)
> > -{
> > -	return readb(base + (reg << 2));
> > -}
> > -
> > -static inline void debug_ll_write_reg(void __iomem *base, int reg, uint8_t val)
> > -{
> > -	writeb(val, base + (reg << 2));
> > -}
> > -
> 
> Unrelated change?

Yes, dropped.

Sascha

--
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] ARM: rockchip: initialize PBL malloc
  2026-05-18 16:20   ` Ahmad Fatoum
@ 2026-05-18 18:24     ` Sascha Hauer
  0 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2026-05-18 18:24 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: Barebox List

On 2026-05-18 18:20, Ahmad Fatoum wrote:
> Hello,
> 
> On 5/18/26 5:32 PM, Sascha Hauer wrote:
> > Rockchip uses compressed firmware binaries. Depending on the compression
> > algorithm we might use early malloc space for decompressing. Initialize
> > the PBL malloc space to the memory end to make pbl_malloc() work. This
> > memory area will be overwritten later in barebox_pbl_start(), but it's
> > ok as the memory is only needed during decompression of the firmware
> > files.
> > 
> > Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> 
> Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> Tested-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> 
> > ---
> >  arch/arm/mach-rockchip/atf.c | 1 +
> >  1 file changed, 1 insertion(+)
> > 
> > diff --git a/arch/arm/mach-rockchip/atf.c b/arch/arm/mach-rockchip/atf.c
> > index 3e2e18ac29..03e03c9122 100644
> > --- a/arch/arm/mach-rockchip/atf.c
> > +++ b/arch/arm/mach-rockchip/atf.c
> > @@ -173,6 +173,7 @@ static void rockchip_atf_load_bl31(void *fdt)
> >  	unsigned long bl31_ep;
> >  
> >  	mmu_early_enable(membase[0], memsize[0]);
> > +	pbl_malloc_init(membase[0] + memsize[0] - SZ_128K, SZ_128K);
> 
> It would be cool if we could use the same malloc area that would be used
> in barebox_pbl_start(), but I guess that's not possible, because it's
> unfortunately based on the barebox proper load address, which is now
> known here. Let's hope this won't clash with any future use..

Yes, that's the problem. I am not exactly happy with this solution
either, but haven't found anything better.

Sascha

--
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-05-18 18:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-18 15:32 [PATCH 1/2] pbl: cleanup early malloc Sascha Hauer
2026-05-18 15:32 ` [PATCH 2/2] ARM: rockchip: initialize PBL malloc Sascha Hauer
2026-05-18 16:20   ` Ahmad Fatoum
2026-05-18 18:24     ` Sascha Hauer
2026-05-18 16:14 ` [PATCH 1/2] pbl: cleanup early malloc Ahmad Fatoum
2026-05-18 18:15   ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox