diff mbox series

[U-Boot,v3,5/6] spl: support using full malloc with SYS_MALLOC_F_LEN

Message ID 20190327193907.30654-6-simon.k.r.goldschmidt@gmail.com
State Superseded, archived
Delegated to: Tom Rini
Headers show
Series spl: full-featured heap cleanups | expand

Commit Message

Simon Goldschmidt March 27, 2019, 7:39 p.m. UTC
Some platforms (like socfpga A10) need a big heap before SDRAM is available
(e.g. because FAT is used). For such platforms, simple_malloc is often not
a good option as it does not support freeing memory. These platforms often
use the non-Kconfig defines CONFIG_SYS_SPL_MALLOC_START (and its SIZE).

This patch allows enabling CONFIG_SPL_SYS_MALLOC_F_LEN while leaving
CONFIG_SPL_SYS_MALLOC_SIMPLE disabled. In this case, the full malloc heap
is made available as early as the simple_malloc heap would be normally.

This way, platforms can drop the non-Kconfig options to set up the full
heap and rely on the same automatically calculated heap allocation used
for simple heap.

Signed-off-by: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
---

Changes in v3: None
Changes in v2:
- use if() instead of #if
- adapt documentation to using CONFIG_SPL_SYS_MALLOC_F_LEN for
  full-featured heap as well
- ensure SPL_CLEAR_BSS_F is set when using SYS_MALLOC_F_LEN for full
  featured heap (or else, the heap status stored in bss will be overwritten
  between board_init_f and board_init_r)

 Kconfig              | 24 ++++++++++++++++--------
 README               | 15 +++++++++++----
 common/spl/spl.c     | 10 ++++++++--
 drivers/core/Kconfig | 33 ++++++++++++++++-----------------
 4 files changed, 51 insertions(+), 31 deletions(-)
diff mbox series

Patch

diff --git a/Kconfig b/Kconfig
index 305b265ed7..e4165692d1 100644
--- a/Kconfig
+++ b/Kconfig
@@ -155,22 +155,30 @@  config SYS_MALLOC_LEN
 config SPL_SYS_MALLOC_F_LEN
 	hex "Size of malloc() pool in SPL before relocation"
 	depends on SYS_MALLOC_F
+	depends on SPL_SYS_MALLOC_SIMPLE || SPL_CLEAR_BSS_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.
+	  Before relocation (before calling board_init_r, that is), 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.
+	  This option controls the size of this initial malloc() pool by
+	  default.
 
 config TPL_SYS_MALLOC_F_LEN
 	hex "Size of malloc() pool in TPL before relocation"
 	depends on SYS_MALLOC_F
+	depends on TPL_SYS_MALLOC_SIMPLE || SPL_CLEAR_BSS_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.
+	  Before relocation (before calling board_init_r, that is), 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.
+	  This option controls the size of this initial malloc() pool by
+	  default.
 
 menuconfig EXPERT
 	bool "Configure standard U-Boot features (expert users)"
diff --git a/README b/README
index c9a20db34f..7c0fb8e4a7 100644
--- a/README
+++ b/README
@@ -2462,13 +2462,19 @@  FIT uImage format:
 		CONFIG_SPL_STACK.
 
 		CONFIG_SYS_SPL_MALLOC_START
-		Starting address of the malloc pool used in SPL.
+		This is one way of providing the starting address of the malloc
+		pool used in SPL. If CONFIG_SPL_SYS_MALLOC_SIMPLE isn't set,
+		the full-featured heap will be used and it will allocate its
+		memory from the initial stack if CONFIG_SPL_SYS_MALLOC_F_LEN is
+		!= 0. If you need it to use a dedicated area, use this option
+		to set an absolute address for the initial heap.
 		When this option is set the full malloc is used in SPL and
 		it is set up by spl_init() and before that, the simple malloc()
-		can be used if CONFIG_SYS_MALLOC_F is defined.
+		can still be used if CONFIG_SPL_SYS_MALLOC_F_LEN is defined.
 
 		CONFIG_SYS_SPL_MALLOC_SIZE
-		The size of the malloc pool used in SPL.
+		The size of the malloc pool used in SPL if
+		CONFIG_SYS_SPL_MALLOC_START is set.
 
 		CONFIG_SPL_OS_BOOT
 		Enable booting directly to an OS from SPL.
@@ -2743,7 +2749,8 @@  Configuration Settings:
 - CONFIG_SYS_MALLOC_SIMPLE
 		Provides a simple and small malloc() and calloc() for those
 		boards which do not use the full malloc in SPL (which is
-		enabled with CONFIG_SYS_SPL_MALLOC_START).
+		enabled by default with CONFIG_SYS_SPL_MALLOC_START or
+		CONFIG_SPL_SYS_MALLOC_F_LEN).
 
 - CONFIG_SYS_NONCACHED_MEMORY:
 		Size of non-cached memory area. This area of memory will be
diff --git a/common/spl/spl.c b/common/spl/spl.c
index 88d4b8a9bf..dec06c6e07 100644
--- a/common/spl/spl.c
+++ b/common/spl/spl.c
@@ -383,8 +383,14 @@  static int spl_common_init(bool setup_malloc)
 #ifdef CONFIG_MALLOC_F_ADDR
 		gd->malloc_base = CONFIG_MALLOC_F_ADDR;
 #endif
-		gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
-		gd->malloc_ptr = 0;
+		if (CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)) {
+			gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
+			gd->malloc_ptr = 0;
+		} else {
+			mem_malloc_init(gd->malloc_base,
+					CONFIG_VAL(SYS_MALLOC_F_LEN));
+			gd->flags |= GD_FLG_FULL_MALLOC_INIT;
+		}
 	}
 #endif
 	ret = bootstage_init(true);
diff --git a/drivers/core/Kconfig b/drivers/core/Kconfig
index ddf2fb3fb8..297c19383f 100644
--- a/drivers/core/Kconfig
+++ b/drivers/core/Kconfig
@@ -12,28 +12,27 @@  config SPL_DM
 	bool "Enable Driver Model for SPL"
 	depends on DM && SPL
 	help
-	  Enable driver model in SPL. You will need to provide a
-	  suitable malloc() implementation. If you are not using the
-	  full malloc() enabled by CONFIG_SYS_SPL_MALLOC_START,
-	  consider using CONFIG_SYS_MALLOC_SIMPLE. In that case you
-	  must provide CONFIG_SPL_SYS_MALLOC_F_LEN to set the size.
-	  In most cases driver model will only allocate a few uclasses
-	  and devices in SPL, so 1KB should be enable. See
-	  CONFIG_SPL_SYS_MALLOC_F_LEN for more details on how to enable it.
+	  Enable driver model in SPL. You will need to provide a suitable
+	  malloc() implementation. In most cases driver model will only
+	  allocate a few uclasses and devices in SPL wihout freeing them, so
+	  1KB should be enough.
+	  If full malloc() (default if CONFIG_SYS_SPL_MALLOC_START or
+	  CONFIG_SPL_SYS_MALLOC_F_LEN are set) is too big for your board,
+	  consider using CONFIG_SPL_SYS_MALLOC_SIMPLE (see help of that option
+	  or CONFIG_SPL_SYS_MALLOC_F_LEN for more info).
 
 config TPL_DM
 	bool "Enable Driver Model for TPL"
 	depends on DM && TPL
 	help
-	  Enable driver model in TPL. You will need to provide a
-	  suitable malloc() implementation. If you are not using the
-	  full malloc() enabled by CONFIG_SYS_SPL_MALLOC_START,
-	  consider using CONFIG_SYS_MALLOC_SIMPLE. In that case you
-	  must provide CONFIG_SPL_SYS_MALLOC_F_LEN to set the size.
-	  In most cases driver model will only allocate a few uclasses
-	  and devices in SPL, so 1KB should be enough. See
-	  CONFIG_SPL_SYS_MALLOC_F_LEN for more details on how to enable it.
-	  Disable this for very small implementations.
+	  Enable driver model in TPL. You will need to provide a suitable
+	  malloc() implementation. In most cases driver model will only
+	  allocate a few uclasses and devices in TPL wihout freeing them, so
+	  1KB should be enough.
+	  If full malloc() (default if CONFIG_SYS_SPL_MALLOC_START or
+	  CONFIG_TPL_SYS_MALLOC_F_LEN are set) is too big for your board,
+	  consider using CONFIG_TPL_SYS_MALLOC_SIMPLE (see help of that option
+	  or CONFIG_TPL_SYS_MALLOC_F_LEN for more info).
 
 config DM_WARN
 	bool "Enable warnings in driver model"