@@ -84,6 +84,7 @@ typedef struct global_data {
unsigned long post_log_res; /* success of POST test */
unsigned long post_init_f_time; /* When post_init_f started */
#endif
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/*
@@ -52,6 +52,7 @@
#include <fdtdec.h>
#include <post.h>
#include <logbuff.h>
+#include <earlymalloc.h>
#ifdef CONFIG_BITBANGMII
#include <miiphy.h>
@@ -273,6 +274,10 @@ void board_init_f(ulong bootflag)
memset((void *)gd, 0, sizeof(gd_t));
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
gd->mon_len = _bss_end_ofs;
#ifdef CONFIG_OF_EMBED
/* Get a pointer to the FDT */
@@ -50,6 +50,7 @@ typedef struct global_data {
#endif
void **jt; /* jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/*
@@ -149,6 +149,10 @@ void board_init_f(ulong board_type)
memset(&gd_data, 0, sizeof(gd_data));
gd = &gd_data;
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
/* Perform initialization sequence */
board_early_init_f();
cpu_init();
@@ -59,6 +59,7 @@ typedef struct global_data {
void **jt; /* jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/*
@@ -250,6 +250,10 @@ void board_init_f(ulong bootflag)
bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;
bd->bi_memsize = CONFIG_SYS_MAX_RAM_SIZE;
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
/* Initialize */
serial_early_puts("IRQ init\n");
irq_init();
@@ -68,6 +68,7 @@ typedef struct global_data {
#endif
void **jt; /* Standalone app jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/*
@@ -227,6 +227,10 @@ board_init_f (ulong bootflag)
/* Clear initial global data */
memset ((void *) gd, 0, sizeof (gd_t));
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
if ((*init_fnc_ptr)() != 0) {
hang ();
@@ -47,6 +47,7 @@ typedef struct global_data {
unsigned long fb_base; /* base address of frame buffer */
void **jt; /* jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/*
@@ -101,6 +101,11 @@ void board_init (void)
asm ("nop"); /* FIXME gd is not initialize - wait */
memset ((void *)gd, 0, GENERATED_GBL_DATA_SIZE);
memset ((void *)bd, 0, GENERATED_BD_INFO_SIZE);
+
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
gd->bd = bd;
gd->baudrate = CONFIG_BAUDRATE;
bd->bi_baudrate = CONFIG_BAUDRATE;
@@ -61,6 +61,7 @@ typedef struct global_data {
unsigned long env_valid; /* Checksum of Environment valid? */
void **jt; /* jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/*
@@ -160,6 +160,10 @@ void board_init_f(ulong bootflag)
memset((void *)gd, 0, sizeof(gd_t));
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
if ((*init_fnc_ptr)() != 0)
hang();
@@ -65,6 +65,7 @@ typedef struct global_data {
void **jt; /* jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/*
@@ -190,6 +190,10 @@ void board_init_f(ulong bootflag)
memset((void *)gd, 0, GENERATED_GBL_DATA_SIZE);
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
gd->mon_len = (unsigned int)(&__bss_end__) - (unsigned int)(&_start);
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
@@ -42,6 +42,7 @@ typedef struct global_data {
#endif
void **jt; /* Standalone app jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/* flags */
@@ -97,6 +97,10 @@ void board_init (void)
memset( gd, 0, GENERATED_GBL_DATA_SIZE );
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
gd->bd = (bd_t *)(gd+1); /* At end of global data */
gd->baudrate = CONFIG_BAUDRATE;
gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
@@ -46,6 +46,7 @@ typedef struct global_data {
unsigned long fb_base; /* base address of frame buffer */
void **jt; /* jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/*
@@ -86,6 +86,10 @@ void board_init(void)
memset((void *)gd, 0, GENERATED_GBL_DATA_SIZE);
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
gd->bd = (bd_t *)(gd+1); /* At end of global data */
gd->baudrate = CONFIG_BAUDRATE;
gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
@@ -184,6 +184,7 @@ typedef struct global_data {
#endif
void **jt; /* jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/*
@@ -389,6 +389,10 @@ void board_init_f(ulong bootflag)
memset((void *) gd, 0, sizeof(gd_t));
#endif
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr)
if ((*init_fnc_ptr) () != 0)
hang();
@@ -47,6 +47,7 @@ typedef struct global_data {
phys_size_t ram_size; /* RAM size */
void **jt; /* jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/*
@@ -156,6 +156,10 @@ void board_init_f(ulong bootflag)
memset((void *)gd, 0, sizeof(gd_t));
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
if ((*init_fnc_ptr)() != 0)
hang();
@@ -42,6 +42,7 @@ typedef struct global_data
unsigned long env_valid; /* Checksum of Environment valid */
void **jt; /* Standalone app jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
#define GD_FLG_RELOC 0x00001 /* Code was relocated to RAM */
@@ -76,6 +76,7 @@ typedef struct global_data {
#endif
void **jt; /* jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
/*
@@ -179,6 +179,10 @@ void board_init_f(ulong bootflag)
/* Clear initial global data */
memset((void *)gd, 0, sizeof(gd_t));
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
gd->bd = (bd_t *) (gd + 1); /* At end of global data */
gd->baudrate = CONFIG_BAUDRATE;
gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
@@ -59,6 +59,7 @@ typedef struct global_data {
unsigned long reset_status; /* reset status register at boot */
void **jt; /* jump table */
char env_buf[32]; /* buffer for getenv() before reloc. */
+ void *early_heap_first; /* early heap for early_malloc */
} gd_t;
static inline gd_t *get_fs_gd_ptr(void)
@@ -220,6 +220,10 @@ void board_init_f(ulong boot_flags)
{
gd->flags = boot_flags;
+ /* Initialize early_malloc */
+ DECLARE_EARLY_HEAP_ON_STACK;
+ early_heap_init(gd->early_heap_first, CONFIG_SYS_EARLY_HEAP_SIZE);
+
do_init_loop(init_sequence_f);
/*
@@ -188,6 +188,7 @@ COBJS-y += console.o
COBJS-y += dlmalloc.o
COBJS-y += memsize.o
COBJS-y += stdio.o
+COBJS-y += earlymalloc.o
COBJS := $(sort $(COBJS-y))
new file mode 100644
@@ -0,0 +1,91 @@
+/*
+ * (C) Copyright 2012
+ * Tomas Hlavacek (tmshlvck@gmail.com)
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <common.h> /* for ROUND_UP */
+#include <asm/u-boot.h>
+#include <asm/global_data.h> /* for gd_t and gd */
+#include <asm/types.h> /* for phys_addr_t and size_addt_t */
+
+#include <earlymalloc.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+
+void early_heap_init(void *heap, size_t size)
+{
+ struct early_heap_header *h = heap;
+
+ h->free_space_pointer = (void *)(roundup((phys_addr_t)heap +
+ sizeof(struct early_heap_header),
+ sizeof(phys_addr_t)));
+ h->free_bytes = size - roundup(sizeof(struct early_heap_header),
+ sizeof(phys_addr_t));
+ h->next_early_heap = NULL;
+}
+
+void *early_malloc(size_t size)
+{
+ phys_addr_t addr;
+ struct early_heap_header *h;
+
+ /* Align size. */
+ size = roundup(size, sizeof(phys_addr_t));
+
+ /* Choose early_heap with enough space. */
+ h = gd->early_heap_first;
+ while ((h->free_bytes < size)&&(h->next_early_heap != NULL))
+ h = h->next_early_heap;
+
+ if(h->free_bytes < size) {
+ debug("Early heap overflow. Heap %08lX, free %d, required %d.",
+ h, h->free_bytes, size);
+ return NULL;
+ }
+
+ /* Choose block beginning address and mark next free space. */
+ addr = h->free_space_pointer;
+
+ h->free_space_pointer += size;
+ h->free_bytes -= size;
+
+ return (void *)addr;
+}
+
+
+int early_malloc_isaddress(void *addr)
+{
+ if ((phys_addr_t)addr < (phys_addr_t)gd->early_heap_first)
+ return 0;
+
+ if ((phys_addr_t)addr >= (phys_addr_t)gd->early_heap_first +
+ CONFIG_SYS_EARLY_HEAP_SIZE)
+ return 0;
+
+ return 1;
+}
+
+int early_malloc_finished(void)
+{
+ return gd->flags & GD_FLG_RELOC;
+}
+
new file mode 100644
@@ -0,0 +1,48 @@
+/*
+ * (C) Copyright 2012
+ * Tomas Hlavacek (tmshlvck@gmail.com)
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef __INCLUDE_EARLYMALLOC_H
+#define __INCLUDE_EARLYMALLOC_H
+
+#include <linux/stddef.h> /* for size_t */
+
+struct early_heap_header {
+ void *free_space_pointer;
+ size_t free_bytes;
+ void *next_early_heap;
+};
+
+void early_heap_init(void *heap, size_t size);
+void *early_malloc(size_t size);
+int early_malloc_isaddress(void *addr);
+int early_malloc_finished(void);
+
+#ifndef CONFIG_SYS_EARLY_HEAP_SIZE
+#define CONFIG_SYS_EARLY_HEAP_SIZE 256
+#endif /* CONFIG_SYS_EARLY_HEAP_SIZE */
+
+#define DECLARE_EARLY_HEAP_ON_STACK char __early_heap[CONFIG_SYS_EARLY_HEAP_SIZE]; \
+ gd->early_heap_first = (void *)__early_heap
+
+#endif /* __INCLUDE_EARLYMALLOC_H */
+
Modular early_malloc for DM with support for more heaps and lightweight first heap on stack. (RFC. Not intended for merging!) Signed-off-by: Tomas Hlavacek <tmshlvck@gmail.com> --- arch/arm/include/asm/global_data.h | 1 + arch/arm/lib/board.c | 5 ++ arch/avr32/include/asm/global_data.h | 1 + arch/avr32/lib/board.c | 4 ++ arch/blackfin/include/asm/global_data.h | 1 + arch/blackfin/lib/board.c | 4 ++ arch/m68k/include/asm/global_data.h | 1 + arch/m68k/lib/board.c | 4 ++ arch/microblaze/include/asm/global_data.h | 1 + arch/microblaze/lib/board.c | 5 ++ arch/mips/include/asm/global_data.h | 1 + arch/mips/lib/board.c | 4 ++ arch/nds32/include/asm/global_data.h | 1 + arch/nds32/lib/board.c | 4 ++ arch/nios2/include/asm/global_data.h | 1 + arch/nios2/lib/board.c | 4 ++ arch/openrisc/include/asm/global_data.h | 1 + arch/openrisc/lib/board.c | 4 ++ arch/powerpc/include/asm/global_data.h | 1 + arch/powerpc/lib/board.c | 4 ++ arch/sandbox/include/asm/global_data.h | 1 + arch/sandbox/lib/board.c | 4 ++ arch/sh/include/asm/global_data.h | 1 + arch/sparc/include/asm/global_data.h | 1 + arch/sparc/lib/board.c | 4 ++ arch/x86/include/asm/global_data.h | 1 + arch/x86/lib/board.c | 4 ++ common/Makefile | 1 + common/earlymalloc.c | 91 +++++++++++++++++++++++++++++ include/earlymalloc.h | 48 +++++++++++++++ 30 files changed, 208 insertions(+) create mode 100644 common/earlymalloc.c create mode 100644 include/earlymalloc.h