Browse Source

spl: make SPL and normal u-boot stage use independent SYS_MALLOC_F_LEN

Some platforms have very limited SRAM to run SPL code, so there may
not be the same amount space for a malloc pool before relocation in
the SPL stage as the normal U-Boot stage.

Make SPL and (the full) U-Boot stage use independent SYS_MALLOC_F_LEN,
so the size of pre-relocation malloc pool can be configured memory
space independently.

Signed-off-by: Andy Yan <andy.yan@rock-chips.com>
Reviewed-by: Tom Rini <trini@konsulko.com>
Acked-by: Philipp Tomsich <philipp.tomsich@theobroma-systems.com>
Reviewed-by: Philipp Tomsich <philipp.tomsich@theobroma-systems.com>
[fixed up commit-message:]
Signed-off-by: Philipp Tomsich <philipp.tomsich@theobroma-systems.com>
Andy Yan 7 years ago
parent
commit
f1896c45cb

+ 10 - 0
Kconfig

@@ -95,6 +95,16 @@ config SYS_MALLOC_F_LEN
 	  particular needs this to operate, so that it can allocate the
 	  particular needs this to operate, so that it can allocate the
 	  initial serial device and any others that are needed.
 	  initial serial device and any others that are needed.
 
 
+config SPL_SYS_MALLOC_F_LEN
+        hex "Size of malloc() pool in spl before relocation"
+        depends on SYS_MALLOC_F
+        default SYS_MALLOC_F_LEN
+        help
+          Before relocation, memory is very limited on many platforms. Still,
+          we can provide a small malloc() pool if needed. Driver model in
+          particular needs this to operate, so that it can allocate the
+          initial serial device and any others that are needed.
+
 menuconfig EXPERT
 menuconfig EXPERT
 	bool "Configure standard U-Boot features (expert users)"
 	bool "Configure standard U-Boot features (expert users)"
 	default y
 	default y

+ 2 - 2
cmd/bdinfo.c

@@ -344,9 +344,9 @@ static int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc,
 #ifdef CONFIG_BOARD_TYPES
 #ifdef CONFIG_BOARD_TYPES
 	printf("Board Type  = %ld\n", gd->board_type);
 	printf("Board Type  = %ld\n", gd->board_type);
 #endif
 #endif
-#ifdef CONFIG_SYS_MALLOC_F
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	printf("Early malloc usage: %lx / %x\n", gd->malloc_ptr,
 	printf("Early malloc usage: %lx / %x\n", gd->malloc_ptr,
-	       CONFIG_SYS_MALLOC_F_LEN);
+	       CONFIG_VAL(SYS_MALLOC_F_LEN));
 #endif
 #endif
 	if (gd->fdt_blob)
 	if (gd->fdt_blob)
 		printf("fdt_blob = %p\n", gd->fdt_blob);
 		printf("fdt_blob = %p\n", gd->fdt_blob);

+ 3 - 1
common/Makefile

@@ -139,9 +139,11 @@ obj-y += console.o
 endif
 endif
 obj-$(CONFIG_CROS_EC) += cros_ec.o
 obj-$(CONFIG_CROS_EC) += cros_ec.o
 obj-y += dlmalloc.o
 obj-y += dlmalloc.o
-ifdef CONFIG_SYS_MALLOC_F_LEN
+ifdef CONFIG_SYS_MALLOC_F
+ifneq ($(CONFIG_$(SPL_)SYS_MALLOC_F_LEN),0)
 obj-y += malloc_simple.o
 obj-y += malloc_simple.o
 endif
 endif
+endif
 obj-y += image.o
 obj-y += image.o
 obj-$(CONFIG_ANDROID_BOOT_IMAGE) += image-android.o
 obj-$(CONFIG_ANDROID_BOOT_IMAGE) += image-android.o
 obj-$(CONFIG_$(SPL_)OF_LIBFDT) += image-fdt.o
 obj-$(CONFIG_$(SPL_)OF_LIBFDT) += image-fdt.o

+ 2 - 2
common/board_f.c

@@ -727,7 +727,7 @@ static int initf_bootstage(void)
 
 
 static int initf_console_record(void)
 static int initf_console_record(void)
 {
 {
-#if defined(CONFIG_CONSOLE_RECORD) && defined(CONFIG_SYS_MALLOC_F_LEN)
+#if defined(CONFIG_CONSOLE_RECORD) && CONFIG_VAL(SYS_MALLOC_F_LEN)
 	return console_record_init();
 	return console_record_init();
 #else
 #else
 	return 0;
 	return 0;
@@ -736,7 +736,7 @@ static int initf_console_record(void)
 
 
 static int initf_dm(void)
 static int initf_dm(void)
 {
 {
-#if defined(CONFIG_DM) && defined(CONFIG_SYS_MALLOC_F_LEN)
+#if defined(CONFIG_DM) && CONFIG_VAL(SYS_MALLOC_F_LEN)
 	int ret;
 	int ret;
 
 
 	bootstage_start(BOOTSTATE_ID_ACCUM_DM_F, "dm_f");
 	bootstage_start(BOOTSTATE_ID_ACCUM_DM_F, "dm_f");

+ 1 - 1
common/board_r.c

@@ -256,7 +256,7 @@ static int initr_malloc(void)
 {
 {
 	ulong malloc_start;
 	ulong malloc_start;
 
 
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	debug("Pre-reloc malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
 	debug("Pre-reloc malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
 	      gd->malloc_ptr / 1024);
 	      gd->malloc_ptr / 1024);
 #endif
 #endif

+ 6 - 6
common/dlmalloc.c

@@ -1254,7 +1254,7 @@ Void_t* mALLOc(bytes) size_t bytes;
 
 
   INTERNAL_SIZE_T nb;
   INTERNAL_SIZE_T nb;
 
 
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT))
 	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT))
 		return malloc_simple(bytes);
 		return malloc_simple(bytes);
 #endif
 #endif
@@ -1522,7 +1522,7 @@ void fREe(mem) Void_t* mem;
   mchunkptr fwd;       /* misc temp for linking */
   mchunkptr fwd;       /* misc temp for linking */
   int       islr;      /* track whether merging with last_remainder */
   int       islr;      /* track whether merging with last_remainder */
 
 
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	/* free() is a no-op - all the memory will be freed on relocation */
 	/* free() is a no-op - all the memory will be freed on relocation */
 	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT))
 	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT))
 		return;
 		return;
@@ -1679,7 +1679,7 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
   /* realloc of null is supposed to be same as malloc */
   /* realloc of null is supposed to be same as malloc */
   if (oldmem == NULL) return mALLOc(bytes);
   if (oldmem == NULL) return mALLOc(bytes);
 
 
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
 	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
 		/* This is harder to support and should not be needed */
 		/* This is harder to support and should not be needed */
 		panic("pre-reloc realloc() is not supported");
 		panic("pre-reloc realloc() is not supported");
@@ -2074,7 +2074,7 @@ Void_t* cALLOc(n, elem_size) size_t n; size_t elem_size;
     return NULL;
     return NULL;
   else
   else
   {
   {
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
 	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
 		MALLOC_ZERO(mem, sz);
 		MALLOC_ZERO(mem, sz);
 		return mem;
 		return mem;
@@ -2375,9 +2375,9 @@ int mALLOPt(param_number, value) int param_number; int value;
 
 
 int initf_malloc(void)
 int initf_malloc(void)
 {
 {
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	assert(gd->malloc_base);	/* Set up by crt0.S */
 	assert(gd->malloc_base);	/* Set up by crt0.S */
-	gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
+	gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
 	gd->malloc_ptr = 0;
 	gd->malloc_ptr = 0;
 #endif
 #endif
 
 

+ 4 - 4
common/init/board_init.c

@@ -46,8 +46,8 @@ __weak void arch_setup_gd(struct global_data *gd_ptr)
 ulong board_init_f_alloc_reserve(ulong top)
 ulong board_init_f_alloc_reserve(ulong top)
 {
 {
 	/* Reserve early malloc arena */
 	/* Reserve early malloc arena */
-#if defined(CONFIG_SYS_MALLOC_F)
-	top -= CONFIG_SYS_MALLOC_F_LEN;
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
+	top -= CONFIG_VAL(SYS_MALLOC_F_LEN);
 #endif
 #endif
 	/* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
 	/* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
 	top = rounddown(top-sizeof(struct global_data), 16);
 	top = rounddown(top-sizeof(struct global_data), 16);
@@ -121,11 +121,11 @@ void board_init_f_init_reserve(ulong base)
 	 * Use gd as it is now properly set for all architectures.
 	 * Use gd as it is now properly set for all architectures.
 	 */
 	 */
 
 
-#if defined(CONFIG_SYS_MALLOC_F)
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	/* go down one 'early malloc arena' */
 	/* go down one 'early malloc arena' */
 	gd->malloc_base = base;
 	gd->malloc_base = base;
 	/* next alloc will be higher by one 'early malloc arena' size */
 	/* next alloc will be higher by one 'early malloc arena' size */
-	base += CONFIG_SYS_MALLOC_F_LEN;
+	base += CONFIG_VAL(SYS_MALLOC_F_LEN);
 #endif
 #endif
 }
 }
 
 

+ 4 - 4
common/spl/spl.c

@@ -220,12 +220,12 @@ static int spl_common_init(bool setup_malloc)
 
 
 	debug("spl_early_init()\n");
 	debug("spl_early_init()\n");
 
 
-#if defined(CONFIG_SYS_MALLOC_F_LEN)
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	if (setup_malloc) {
 	if (setup_malloc) {
 #ifdef CONFIG_MALLOC_F_ADDR
 #ifdef CONFIG_MALLOC_F_ADDR
 		gd->malloc_base = CONFIG_MALLOC_F_ADDR;
 		gd->malloc_base = CONFIG_MALLOC_F_ADDR;
 #endif
 #endif
-		gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
+		gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
 		gd->malloc_ptr = 0;
 		gd->malloc_ptr = 0;
 	}
 	}
 #endif
 #endif
@@ -419,7 +419,7 @@ void board_init_r(gd_t *dummy1, ulong dummy2)
 	default:
 	default:
 		debug("Unsupported OS image.. Jumping nevertheless..\n");
 		debug("Unsupported OS image.. Jumping nevertheless..\n");
 	}
 	}
-#if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
+#if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
 	debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
 	debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
 	      gd->malloc_ptr / 1024);
 	      gd->malloc_ptr / 1024);
 #endif
 #endif
@@ -486,7 +486,7 @@ ulong spl_relocate_stack_gd(void)
 	gd_t *new_gd;
 	gd_t *new_gd;
 	ulong ptr = CONFIG_SPL_STACK_R_ADDR;
 	ulong ptr = CONFIG_SPL_STACK_R_ADDR;
 
 
-#ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE
+#if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_SPL_SYS_MALLOC_F_LEN
 	if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
 	if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
 		ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
 		ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
 		gd->malloc_base = ptr;
 		gd->malloc_base = ptr;

+ 4 - 4
drivers/core/Kconfig

@@ -16,10 +16,10 @@ config SPL_DM
 	  suitable malloc() implementation. If you are not using the
 	  suitable malloc() implementation. If you are not using the
 	  full malloc() enabled by CONFIG_SYS_SPL_MALLOC_START,
 	  full malloc() enabled by CONFIG_SYS_SPL_MALLOC_START,
 	  consider using CONFIG_SYS_MALLOC_SIMPLE. In that case you
 	  consider using CONFIG_SYS_MALLOC_SIMPLE. In that case you
-	  must provide CONFIG_SYS_MALLOC_F_LEN to set the size.
+	  must provide CONFIG_SPL_SYS_MALLOC_F_LEN to set the size.
 	  In most cases driver model will only allocate a few uclasses
 	  In most cases driver model will only allocate a few uclasses
 	  and devices in SPL, so 1KB should be enable. See
 	  and devices in SPL, so 1KB should be enable. See
-	  CONFIG_SYS_MALLOC_F_LEN for more details on how to enable it.
+	  CONFIG_SPL_SYS_MALLOC_F_LEN for more details on how to enable it.
 
 
 config TPL_DM
 config TPL_DM
 	bool "Enable Driver Model for TPL"
 	bool "Enable Driver Model for TPL"
@@ -29,10 +29,10 @@ config TPL_DM
 	  suitable malloc() implementation. If you are not using the
 	  suitable malloc() implementation. If you are not using the
 	  full malloc() enabled by CONFIG_SYS_SPL_MALLOC_START,
 	  full malloc() enabled by CONFIG_SYS_SPL_MALLOC_START,
 	  consider using CONFIG_SYS_MALLOC_SIMPLE. In that case you
 	  consider using CONFIG_SYS_MALLOC_SIMPLE. In that case you
-	  must provide CONFIG_SYS_MALLOC_F_LEN to set the size.
+	  must provide CONFIG_SPL_SYS_MALLOC_F_LEN to set the size.
 	  In most cases driver model will only allocate a few uclasses
 	  In most cases driver model will only allocate a few uclasses
 	  and devices in SPL, so 1KB should be enough. See
 	  and devices in SPL, so 1KB should be enough. See
-	  CONFIG_SYS_MALLOC_F_LEN for more details on how to enable it.
+	  CONFIG_SPL_SYS_MALLOC_F_LEN for more details on how to enable it.
 	  Disable this for very small implementations.
 	  Disable this for very small implementations.
 
 
 config DM_WARN
 config DM_WARN

+ 2 - 2
drivers/serial/serial-uclass.c

@@ -23,8 +23,8 @@ DECLARE_GLOBAL_DATA_PTR;
  */
  */
 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
 
 
-#ifndef CONFIG_SYS_MALLOC_F_LEN
-#error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
+#if !CONFIG_VAL(SYS_MALLOC_F_LEN)
+#error "Serial is required before relocation - define CONFIG_$(SPL_)SYS_MALLOC_F_LEN to make this work"
 #endif
 #endif
 
 
 static int serial_check_stdout(const void *blob, struct udevice **devp)
 static int serial_check_stdout(const void *blob, struct udevice **devp)

+ 1 - 1
include/asm-generic/global_data.h

@@ -88,7 +88,7 @@ typedef struct global_data {
 #endif
 #endif
 	unsigned int timebase_h;
 	unsigned int timebase_h;
 	unsigned int timebase_l;
 	unsigned int timebase_l;
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	unsigned long malloc_base;	/* base address of early malloc() */
 	unsigned long malloc_base;	/* base address of early malloc() */
 	unsigned long malloc_limit;	/* limit address */
 	unsigned long malloc_limit;	/* limit address */
 	unsigned long malloc_ptr;	/* current address */
 	unsigned long malloc_ptr;	/* current address */

+ 1 - 1
lib/asm-offsets.c

@@ -28,7 +28,7 @@ int main(void)
 	DEFINE(GD_SIZE, sizeof(struct global_data));
 	DEFINE(GD_SIZE, sizeof(struct global_data));
 
 
 	DEFINE(GD_BD, offsetof(struct global_data, bd));
 	DEFINE(GD_BD, offsetof(struct global_data, bd));
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	DEFINE(GD_MALLOC_BASE, offsetof(struct global_data, malloc_base));
 	DEFINE(GD_MALLOC_BASE, offsetof(struct global_data, malloc_base));
 #endif
 #endif
 
 

+ 1 - 1
lib/efi/efi_app.c

@@ -48,7 +48,7 @@ static efi_status_t setup_memory(struct efi_priv *priv)
 		return ret;
 		return ret;
 	memset(gd, '\0', sizeof(*gd));
 	memset(gd, '\0', sizeof(*gd));
 
 
-	gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_SYS_MALLOC_F_LEN,
+	gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_VAL(SYS_MALLOC_F_LEN),
 					    &ret);
 					    &ret);
 	if (!gd->malloc_base)
 	if (!gd->malloc_base)
 		return ret;
 		return ret;