aboutsummaryrefslogtreecommitdiffstats
path: root/arch/powerpc/boot
diff options
context:
space:
mode:
authorMark A. Greer <mgreer@mvista.com>2006-10-16 16:54:52 -0400
committerPaul Mackerras <paulus@samba.org>2006-10-22 22:49:19 -0400
commit01a6372008ed450982ba38ee5fd91028b9f5a781 (patch)
treeb933ded4570114b5a63aa86027f9d24f97bd4751 /arch/powerpc/boot
parent0c176fa80fdfa9b4e0753e37223b056994c818d2 (diff)
[POWERPC] Add simple memory allocator to bootwrapper
Provide primitive malloc, free, and realloc functions for bootwrapper. Signed-off-by: Mark A. Greer <mgreer@mvista.com> Signed-off-by: Paul Mackerras <paulus@samba.org>
Diffstat (limited to 'arch/powerpc/boot')
-rw-r--r--arch/powerpc/boot/Makefile2
-rw-r--r--arch/powerpc/boot/simple_alloc.c149
2 files changed, 150 insertions, 1 deletions
diff --git a/arch/powerpc/boot/Makefile b/arch/powerpc/boot/Makefile
index 62435d951dbd..9731c25c54a1 100644
--- a/arch/powerpc/boot/Makefile
+++ b/arch/powerpc/boot/Makefile
@@ -41,7 +41,7 @@ $(addprefix $(obj)/,$(zlib) main.o): $(addprefix $(obj)/,$(zliblinuxheader)) \
41 $(addprefix $(obj)/,$(zlibheader)) 41 $(addprefix $(obj)/,$(zlibheader))
42 42
43src-wlib := string.S stdio.c main.c flatdevtree.c flatdevtree_misc.c \ 43src-wlib := string.S stdio.c main.c flatdevtree.c flatdevtree_misc.c \
44 ns16550.c serial.c div64.S util.S $(zlib) 44 ns16550.c serial.c simple_alloc.c div64.S util.S $(zlib)
45src-plat := of.c 45src-plat := of.c
46src-boot := crt0.S $(src-wlib) $(src-plat) empty.c 46src-boot := crt0.S $(src-wlib) $(src-plat) empty.c
47 47
diff --git a/arch/powerpc/boot/simple_alloc.c b/arch/powerpc/boot/simple_alloc.c
new file mode 100644
index 000000000000..cfe3a7505ba0
--- /dev/null
+++ b/arch/powerpc/boot/simple_alloc.c
@@ -0,0 +1,149 @@
1/*
2 * Implement primitive realloc(3) functionality.
3 *
4 * Author: Mark A. Greer <mgreer@mvista.com>
5 *
6 * 2006 (c) MontaVista, Software, Inc. This file is licensed under
7 * the terms of the GNU General Public License version 2. This program
8 * is licensed "as is" without any warranty of any kind, whether express
9 * or implied.
10 */
11
12#include <stddef.h>
13#include "types.h"
14#include "page.h"
15#include "string.h"
16#include "ops.h"
17
18#define ENTRY_BEEN_USED 0x01
19#define ENTRY_IN_USE 0x02
20
21static struct alloc_info {
22 u32 flags;
23 u32 base;
24 u32 size;
25} *alloc_tbl;
26
27static u32 tbl_entries;
28static u32 alloc_min;
29static u32 next_base;
30static u32 space_left;
31
32/*
33 * First time an entry is used, its base and size are set.
34 * An entry can be freed and re-malloc'd but its base & size don't change.
35 * Should be smart enough for needs of bootwrapper.
36 */
37static void *simple_malloc(u32 size)
38{
39 u32 i;
40 struct alloc_info *p = alloc_tbl;
41
42 if (size == 0)
43 goto err_out;
44
45 size = _ALIGN_UP(size, alloc_min);
46
47 for (i=0; i<tbl_entries; i++, p++)
48 if (!(p->flags & ENTRY_BEEN_USED)) { /* never been used */
49 if (size <= space_left) {
50 p->base = next_base;
51 p->size = size;
52 p->flags = ENTRY_BEEN_USED | ENTRY_IN_USE;
53 next_base += size;
54 space_left -= size;
55 return (void *)p->base;
56 }
57 goto err_out; /* not enough space left */
58 }
59 /* reuse an entry keeping same base & size */
60 else if (!(p->flags & ENTRY_IN_USE) && (size <= p->size)) {
61 p->flags |= ENTRY_IN_USE;
62 return (void *)p->base;
63 }
64err_out:
65 return NULL;
66}
67
68static struct alloc_info *simple_find_entry(void *ptr)
69{
70 u32 i;
71 struct alloc_info *p = alloc_tbl;
72
73 for (i=0; i<tbl_entries; i++,p++) {
74 if (!(p->flags & ENTRY_BEEN_USED))
75 break;
76 if ((p->flags & ENTRY_IN_USE) && (p->base == (u32)ptr))
77 return p;
78 }
79 return NULL;
80}
81
82static void simple_free(void *ptr)
83{
84 struct alloc_info *p = simple_find_entry(ptr);
85
86 if (p != NULL)
87 p->flags &= ~ENTRY_IN_USE;
88}
89
90/*
91 * Change size of area pointed to by 'ptr' to 'size'.
92 * If 'ptr' is NULL, then its a malloc(). If 'size' is 0, then its a free().
93 * 'ptr' must be NULL or a pointer to a non-freed area previously returned by
94 * simple_realloc() or simple_malloc().
95 */
96static void *simple_realloc(void *ptr, unsigned long size)
97{
98 struct alloc_info *p;
99 void *new;
100
101 if (size == 0) {
102 simple_free(ptr);
103 return NULL;
104 }
105
106 if (ptr == NULL)
107 return simple_malloc(size);
108
109 p = simple_find_entry(ptr);
110 if (p == NULL) /* ptr not from simple_malloc/simple_realloc */
111 return NULL;
112 if (size <= p->size) /* fits in current block */
113 return ptr;
114
115 new = simple_malloc(size);
116 memcpy(new, ptr, p->size);
117 simple_free(ptr);
118 return new;
119}
120
121/*
122 * Returns addr of first byte after heap so caller can see if it took
123 * too much space. If so, change args & try again.
124 */
125void *simple_alloc_init(char *base, u32 heap_size, u32 granularity,
126 u32 max_allocs)
127{
128 u32 heap_base, tbl_size;
129
130 heap_size = _ALIGN_UP(heap_size, granularity);
131 alloc_min = granularity;
132 tbl_entries = max_allocs;
133
134 tbl_size = tbl_entries * sizeof(struct alloc_info);
135
136 alloc_tbl = (struct alloc_info *)_ALIGN_UP((unsigned long)base, 8);
137 memset(alloc_tbl, 0, tbl_size);
138
139 heap_base = _ALIGN_UP((u32)alloc_tbl + tbl_size, alloc_min);
140
141 next_base = heap_base;
142 space_left = heap_size;
143
144 platform_ops.malloc = simple_malloc;
145 platform_ops.free = simple_free;
146 platform_ops.realloc = simple_realloc;
147
148 return (void *)(heap_base + heap_size);
149}