blob: f8a9ddf9127012296b79aed73246dd69dc86be91 [file] [log] [blame]
Jordan Crousef6145c32008-03-19 23:56:58 +00001/*
Jordan Crousef6145c32008-03-19 23:56:58 +00002 *
3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
Stefan Reinauere5d30b72010-03-25 22:15:19 +00004 * Copyright (C) 2008-2010 coresystems GmbH
Jordan Crousef6145c32008-03-19 23:56:58 +00005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
Uwe Hermann6a441bf2008-03-20 19:54:59 +000030/*
Uwe Hermann661e3802008-03-21 18:37:23 +000031 * This is a classically weak malloc() implementation. We have a relatively
Uwe Hermann6a441bf2008-03-20 19:54:59 +000032 * small and static heap, so we take the easy route with an O(N) loop
33 * through the tree for every malloc() and free(). Obviously, this doesn't
34 * scale past a few hundred KB (if that).
35 *
Uwe Hermann661e3802008-03-21 18:37:23 +000036 * We're also susceptible to the usual buffer overrun poisoning, though the
Uwe Hermann6a441bf2008-03-20 19:54:59 +000037 * risk is within acceptable ranges for this implementation (don't overrun
38 * your buffers, kids!).
39 */
Jordan Crousef6145c32008-03-19 23:56:58 +000040
Stefan Reinauere5d30b72010-03-25 22:15:19 +000041#define IN_MALLOC_C
Jordan Crousef6145c32008-03-19 23:56:58 +000042#include <libpayload.h>
Furquan Shaikh79a591f2014-05-13 13:47:32 -070043#include <stdint.h>
Jordan Crousef6145c32008-03-19 23:56:58 +000044
Julius Wernerb8fad3d2013-08-27 15:48:32 -070045struct memory_type {
46 void *start;
47 void *end;
48 struct align_region_t* align_regions;
Julius Wernereab2a292019-03-05 16:55:15 -080049#if CONFIG(LP_DEBUG_MALLOC)
Julius Werner9665d382013-09-13 18:21:46 -070050 int magic_initialized;
51 size_t minimal_free;
52 const char *name;
53#endif
Julius Wernerb8fad3d2013-08-27 15:48:32 -070054};
55
Uwe Hermann6a441bf2008-03-20 19:54:59 +000056extern char _heap, _eheap; /* Defined in the ldscript. */
Jordan Crousef6145c32008-03-19 23:56:58 +000057
Julius Werner9665d382013-09-13 18:21:46 -070058static struct memory_type default_type =
59 { (void *)&_heap, (void *)&_eheap, NULL
Julius Wernereab2a292019-03-05 16:55:15 -080060#if CONFIG(LP_DEBUG_MALLOC)
Julius Werner9665d382013-09-13 18:21:46 -070061 , 0, 0, "HEAP"
62#endif
63 };
Julius Wernerb8fad3d2013-08-27 15:48:32 -070064static struct memory_type *const heap = &default_type;
65static struct memory_type *dma = &default_type;
Jordan Crousef6145c32008-03-19 23:56:58 +000066
Nico Huber25dd2472013-06-14 15:34:59 +020067typedef u64 hdrtype_t;
Nico Huber2d4b4ca2013-06-14 15:26:49 +020068#define HDRSIZE (sizeof(hdrtype_t))
Jordan Crousef6145c32008-03-19 23:56:58 +000069
Nico Huber2d4b4ca2013-06-14 15:26:49 +020070#define SIZE_BITS ((HDRSIZE << 3) - 7)
71#define MAGIC (((hdrtype_t)0x2a) << (SIZE_BITS + 1))
72#define FLAG_FREE (((hdrtype_t)0x01) << (SIZE_BITS + 0))
73#define MAX_SIZE ((((hdrtype_t)0x01) << SIZE_BITS) - 1)
Jordan Crousef6145c32008-03-19 23:56:58 +000074
Nico Huber3665ace2012-10-08 15:03:35 +020075#define SIZE(_h) ((_h) & MAX_SIZE)
Jordan Crousef6145c32008-03-19 23:56:58 +000076
Nico Huber3665ace2012-10-08 15:03:35 +020077#define _HEADER(_s, _f) ((hdrtype_t) (MAGIC | (_f) | ((_s) & MAX_SIZE)))
Jordan Crousef6145c32008-03-19 23:56:58 +000078
79#define FREE_BLOCK(_s) _HEADER(_s, FLAG_FREE)
Nico Huber3665ace2012-10-08 15:03:35 +020080#define USED_BLOCK(_s) _HEADER(_s, 0)
Jordan Crousef6145c32008-03-19 23:56:58 +000081
Jordan Crousef6145c32008-03-19 23:56:58 +000082#define IS_FREE(_h) (((_h) & (MAGIC | FLAG_FREE)) == (MAGIC | FLAG_FREE))
83#define HAS_MAGIC(_h) (((_h) & MAGIC) == MAGIC)
84
Julius Wernerb8fad3d2013-08-27 15:48:32 -070085static int free_aligned(void* addr, struct memory_type *type);
Jordan Crousef6145c32008-03-19 23:56:58 +000086void print_malloc_map(void);
87
Julius Wernerb8fad3d2013-08-27 15:48:32 -070088void init_dma_memory(void *start, u32 size)
89{
Julius Werner509c37e2013-08-28 12:29:28 -070090 if (dma_initialized()) {
Julius Werner9665d382013-09-13 18:21:46 -070091 printf("ERROR: %s called twice!\n", __func__);
Julius Wernerb8fad3d2013-08-27 15:48:32 -070092 return;
93 }
94
Julius Werner9665d382013-09-13 18:21:46 -070095 /*
Martin Rothe81ce042017-06-03 20:00:36 -060096 * DMA memory might not be zeroed by coreboot on stage loading, so make
Julius Werner9665d382013-09-13 18:21:46 -070097 * sure we clear the magic cookie from last boot.
98 */
99 *(hdrtype_t *)start = 0;
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700100
101 dma = malloc(sizeof(*dma));
102 dma->start = start;
103 dma->end = start + size;
104 dma->align_regions = NULL;
Julius Werner9665d382013-09-13 18:21:46 -0700105
Julius Wernereab2a292019-03-05 16:55:15 -0800106#if CONFIG(LP_DEBUG_MALLOC)
Julius Werner9665d382013-09-13 18:21:46 -0700107 dma->minimal_free = 0;
108 dma->magic_initialized = 0;
109 dma->name = "DMA";
110
111 printf("Initialized cache-coherent DMA memory at [%p:%p]\n", start, start + size);
112#endif
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700113}
114
Arthur Heymansae57f1d2023-08-25 13:07:25 +0200115int dma_initialized(void)
Julius Werner509c37e2013-08-28 12:29:28 -0700116{
117 return dma != heap;
118}
119
120/* For boards that don't initialize DMA we assume all locations are coherent */
Yu-Ping Wu30d8e722022-08-23 16:40:03 +0800121int dma_coherent(const void *ptr)
Julius Werner509c37e2013-08-28 12:29:28 -0700122{
123 return !dma_initialized() || (dma->start <= ptr && dma->end > ptr);
124}
125
Yi Chou582c2a72023-10-16 11:34:52 +0800126/* Get the range of memory that can be allocated by the dma allocator. */
127void dma_allocator_range(void **start_out, size_t *size_out)
128{
129 if (dma_initialized()) {
130 *start_out = dma->start;
131 *size_out = dma->end - dma->start;
132 } else {
133 *start_out = NULL;
134 *size_out = 0;
135 }
136}
137
Yu-Ping Wuaec3b1f2020-09-11 14:39:03 +0800138/* Find free block of size >= len */
139static hdrtype_t volatile *find_free_block(int len, struct memory_type *type)
Jordan Crousef6145c32008-03-19 23:56:58 +0000140{
141 hdrtype_t header;
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700142 hdrtype_t volatile *ptr = (hdrtype_t volatile *)type->start;
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000143
144 /* Align the size. */
Patrick Georgi04a5b482015-01-21 17:37:34 +0100145 len = ALIGN_UP(len, HDRSIZE);
Jordan Crousef6145c32008-03-19 23:56:58 +0000146
Nico Huber3665ace2012-10-08 15:03:35 +0200147 if (!len || len > MAX_SIZE)
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000148 return (void *)NULL;
Jordan Crousef6145c32008-03-19 23:56:58 +0000149
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000150 /* Make sure the region is setup correctly. */
Julius Werner9665d382013-09-13 18:21:46 -0700151 if (!HAS_MAGIC(*ptr)) {
152 size_t size = (type->end - type->start) - HDRSIZE;
153 *ptr = FREE_BLOCK(size);
Julius Wernereab2a292019-03-05 16:55:15 -0800154#if CONFIG(LP_DEBUG_MALLOC)
Julius Werner9665d382013-09-13 18:21:46 -0700155 type->magic_initialized = 1;
156 type->minimal_free = size;
157#endif
158 }
Jordan Crousef6145c32008-03-19 23:56:58 +0000159
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000160 /* Find some free space. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000161 do {
Marc Jones987e8832012-03-01 16:12:11 -0700162 header = *ptr;
Jordan Crousef6145c32008-03-19 23:56:58 +0000163 int size = SIZE(header);
164
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000165 if (!HAS_MAGIC(header) || size == 0) {
Stefan Reinauere5d30b72010-03-25 22:15:19 +0000166 printf("memory allocator panic. (%s%s)\n",
167 !HAS_MAGIC(header) ? " no magic " : "",
168 size == 0 ? " size=0 " : "");
Jordan Crouse24a04042008-04-25 23:08:47 +0000169 halt();
Stefan Reinaueree673192008-08-14 14:40:10 +0000170 }
Jordan Crouse24a04042008-04-25 23:08:47 +0000171
Yu-Ping Wuaec3b1f2020-09-11 14:39:03 +0800172 if ((header & FLAG_FREE) && len <= size)
173 return ptr;
Jordan Crousef6145c32008-03-19 23:56:58 +0000174
Furquan Shaikh79a591f2014-05-13 13:47:32 -0700175 ptr = (hdrtype_t volatile *)((uintptr_t)ptr + HDRSIZE + size);
Jordan Crousef6145c32008-03-19 23:56:58 +0000176
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700177 } while (ptr < (hdrtype_t *) type->end);
Jordan Crousef6145c32008-03-19 23:56:58 +0000178
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000179 /* Nothing available. */
Yu-Ping Wuaec3b1f2020-09-11 14:39:03 +0800180 return NULL;
181}
182
183/* Mark the block with length 'len' as used */
184static void use_block(hdrtype_t volatile *ptr, int len)
185{
186 /* Align the size. */
187 len = ALIGN_UP(len, HDRSIZE);
188
189 hdrtype_t volatile *nptr = (hdrtype_t volatile *)
190 ((uintptr_t)ptr + HDRSIZE + len);
191 int size = SIZE(*ptr);
192 int nsize = size - (HDRSIZE + len);
193
194 /*
195 * If there is still room in this block, then mark it as such otherwise
196 * account the whole space for that block.
197 */
198 if (nsize > 0) {
199 /* Mark the block as used. */
200 *ptr = USED_BLOCK(len);
201
202 /* Create a new free block. */
203 *nptr = FREE_BLOCK(nsize);
204 } else {
205 /* Mark the block as used. */
206 *ptr = USED_BLOCK(size);
207 }
208}
209
210static void *alloc(int len, struct memory_type *type)
211{
212 hdrtype_t volatile *ptr = find_free_block(len, type);
213
214 if (ptr == NULL)
215 return NULL;
216
217 use_block(ptr, len);
218 return (void *)((uintptr_t)ptr + HDRSIZE);
Jordan Crousef6145c32008-03-19 23:56:58 +0000219}
220
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700221static void _consolidate(struct memory_type *type)
Jordan Crousef6145c32008-03-19 23:56:58 +0000222{
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700223 void *ptr = type->start;
Jordan Crousef6145c32008-03-19 23:56:58 +0000224
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700225 while (ptr < type->end) {
Jordan Crousef6145c32008-03-19 23:56:58 +0000226 void *nptr;
227 hdrtype_t hdr = *((hdrtype_t *) ptr);
228 unsigned int size = 0;
229
230 if (!IS_FREE(hdr)) {
231 ptr += HDRSIZE + SIZE(hdr);
232 continue;
233 }
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000234
Jordan Crousef6145c32008-03-19 23:56:58 +0000235 size = SIZE(hdr);
236 nptr = ptr + HDRSIZE + SIZE(hdr);
237
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700238 while (nptr < type->end) {
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000239 hdrtype_t nhdr = *((hdrtype_t *) nptr);
240
Jordan Crousef6145c32008-03-19 23:56:58 +0000241 if (!(IS_FREE(nhdr)))
242 break;
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000243
Jordan Crousef6145c32008-03-19 23:56:58 +0000244 size += SIZE(nhdr) + HDRSIZE;
245
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000246 *((hdrtype_t *) nptr) = 0;
Jordan Crousef6145c32008-03-19 23:56:58 +0000247
248 nptr += (HDRSIZE + SIZE(nhdr));
249 }
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000250
Jordan Crousef6145c32008-03-19 23:56:58 +0000251 *((hdrtype_t *) ptr) = FREE_BLOCK(size);
252 ptr = nptr;
253 }
254}
255
256void free(void *ptr)
257{
258 hdrtype_t hdr;
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700259 struct memory_type *type = heap;
Jordan Crousef6145c32008-03-19 23:56:58 +0000260
Hsuan Ting Chen8742e2a2020-09-17 15:51:34 +0800261 /* No action occurs on NULL. */
262 if (ptr == NULL)
263 return;
264
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000265 /* Sanity check. */
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700266 if (ptr < type->start || ptr >= type->end) {
267 type = dma;
268 if (ptr < type->start || ptr >= type->end)
269 return;
270 }
Jordan Crousef6145c32008-03-19 23:56:58 +0000271
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700272 if (free_aligned(ptr, type)) return;
273
274 ptr -= HDRSIZE;
Jordan Crousef6145c32008-03-19 23:56:58 +0000275 hdr = *((hdrtype_t *) ptr);
276
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000277 /* Not our header (we're probably poisoned). */
Jordan Crousef6145c32008-03-19 23:56:58 +0000278 if (!HAS_MAGIC(hdr))
279 return;
280
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000281 /* Double free. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000282 if (hdr & FLAG_FREE)
283 return;
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000284
Jordan Crousef6145c32008-03-19 23:56:58 +0000285 *((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr));
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700286 _consolidate(type);
Jordan Crousef6145c32008-03-19 23:56:58 +0000287}
288
289void *malloc(size_t size)
290{
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700291 return alloc(size, heap);
292}
293
294void *dma_malloc(size_t size)
295{
296 return alloc(size, dma);
Jordan Crousef6145c32008-03-19 23:56:58 +0000297}
298
299void *calloc(size_t nmemb, size_t size)
300{
Jordan Crouse24a04042008-04-25 23:08:47 +0000301 size_t total = nmemb * size;
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700302 void *ptr = alloc(total, heap);
Jordan Crousef6145c32008-03-19 23:56:58 +0000303
304 if (ptr)
305 memset(ptr, 0, total);
306
307 return ptr;
308}
309
310void *realloc(void *ptr, size_t size)
311{
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000312 void *ret, *pptr;
Yu-Ping Wuaec3b1f2020-09-11 14:39:03 +0800313 hdrtype_t volatile *block;
Jordan Crousef6145c32008-03-19 23:56:58 +0000314 unsigned int osize;
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700315 struct memory_type *type = heap;
Jordan Crousef6145c32008-03-19 23:56:58 +0000316
317 if (ptr == NULL)
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700318 return alloc(size, type);
Jordan Crousef6145c32008-03-19 23:56:58 +0000319
320 pptr = ptr - HDRSIZE;
321
322 if (!HAS_MAGIC(*((hdrtype_t *) pptr)))
323 return NULL;
Jordan Crousef6145c32008-03-19 23:56:58 +0000324
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700325 if (ptr < type->start || ptr >= type->end)
326 type = dma;
327
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000328 /* Get the original size of the block. */
329 osize = SIZE(*((hdrtype_t *) pptr));
330
331 /*
332 * Free the memory to update the tables - this won't touch the actual
333 * memory, so we can still use it for the copy after we have
334 * reallocated the new space.
335 */
336 free(ptr);
Yu-Ping Wuaec3b1f2020-09-11 14:39:03 +0800337
338 block = find_free_block(size, type);
339 if (block == NULL)
340 return NULL;
341
342 ret = (void *)((uintptr_t)block + HDRSIZE);
Jordan Crousef6145c32008-03-19 23:56:58 +0000343
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000344 /*
Yu-Ping Wuaec3b1f2020-09-11 14:39:03 +0800345 * If ret == ptr, then no copy is needed. Otherwise, move the memory to
346 * the new location, which might be before the old one and overlap since
347 * the free() above includes a _consolidate().
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000348 */
Yu-Ping Wuaec3b1f2020-09-11 14:39:03 +0800349 if (ret != ptr)
350 memmove(ret, ptr, osize > size ? size : osize);
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000351
Yu-Ping Wuaec3b1f2020-09-11 14:39:03 +0800352 /* Mark the block as used. */
353 use_block(block, size);
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000354
Jordan Crousef6145c32008-03-19 23:56:58 +0000355 return ret;
356}
357
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000358struct align_region_t
359{
Elyes HAOUAS824b4b82020-02-15 09:27:11 +0100360 /* If alignment is 0 then the region represents a large region which
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600361 * has no metadata for tracking subelements. */
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000362 int alignment;
363 /* start in memory, and size in bytes */
364 void* start;
365 int size;
366 /* layout within a region:
367 - num_elements bytes, 0: free, 1: used, 2: used, combines with next
368 - padding to alignment
369 - data section
370 - waste space
371
372 start_data points to the start of the data section
373 */
374 void* start_data;
375 /* number of free blocks sized "alignment" */
376 int free;
377 struct align_region_t *next;
378};
379
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600380static inline int region_is_large(const struct align_region_t *r)
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000381{
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600382 return r->alignment == 0;
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000383}
384
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600385static inline int addr_in_region(const struct align_region_t *r, void *addr)
386{
387 return ((addr >= r->start_data) && (addr < r->start_data + r->size));
388}
389
390/* num_elements == 0 indicates a large aligned region instead of a smaller
391 * region comprised of alignment-sized chunks. */
392static struct align_region_t *allocate_region(int alignment, int num_elements,
393 size_t size, struct memory_type *type)
394{
395 struct align_region_t *r;
396 size_t extra_space;
397
Julius Wernereab2a292019-03-05 16:55:15 -0800398#if CONFIG(LP_DEBUG_MALLOC)
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600399 printf("%s(old align_regions=%p, alignment=%u, num_elements=%u, size=%zu)\n",
400 __func__, type->align_regions, alignment, num_elements, size);
401#endif
402
403 r = malloc(sizeof(*r));
404
405 if (r == NULL)
406 return NULL;
407
Jonathan Neuschäfera4fbc382016-04-05 21:36:34 +0200408 memset(r, 0, sizeof(*r));
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600409
410 if (num_elements != 0) {
411 r->alignment = alignment;
412 r->size = num_elements * alignment;
413 r->free = num_elements;
414 /* Allocate enough memory for alignment requirements and
415 * metadata for each chunk. */
416 extra_space = num_elements;
417 } else {
418 /* Large aligned allocation. Set alignment = 0. */
419 r->alignment = 0;
420 r->size = size;
421 extra_space = 0;
422 }
423
424 r->start = alloc(r->size + alignment + extra_space, type);
425
426 if (r->start == NULL) {
427 free(r);
428 return NULL;
429 }
430
431 r->start_data = (void *)ALIGN_UP((uintptr_t)r->start + extra_space,
432 alignment);
433
434 /* Clear any (if requested) metadata. */
435 memset(r->start, 0, extra_space);
436
437 /* Link the region with the rest. */
438 r->next = type->align_regions;
439 type->align_regions = r;
440
441 return r;
442}
443
444static void try_free_region(struct align_region_t **prev_link)
445{
446 struct align_region_t *r = *prev_link;
447
448 /* All large regions are immediately free-able. Non-large regions
449 * need to be checked for the fully freed state. */
450 if (!region_is_large(r)) {
451 if (r->free != r->size / r->alignment)
452 return;
453 }
454
455 /* Unlink region from link list. */
456 *prev_link = r->next;
457
458 /* Free the data and metadata. */
459 free(r->start);
460 free(r);
461}
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000462
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700463static int free_aligned(void* addr, struct memory_type *type)
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000464{
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600465 struct align_region_t **prev_link = &type->align_regions;
466
467 while (*prev_link != NULL)
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000468 {
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600469 if (!addr_in_region(*prev_link, addr)) {
470 prev_link = &((*prev_link)->next);
471 continue;
472 }
473
474 if (region_is_large(*prev_link)) {
475 try_free_region(prev_link);
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000476 return 1;
477 }
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600478
479 int i = (addr-(*prev_link)->start_data)/(*prev_link)->alignment;
480 u8 *meta = (*prev_link)->start;
481 while (meta[i] == 2)
482 {
483 meta[i++] = 0;
484 (*prev_link)->free++;
485 }
486 meta[i] = 0;
487 (*prev_link)->free++;
488 try_free_region(prev_link);
489 return 1;
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000490 }
491 return 0;
492}
493
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700494static void *alloc_aligned(size_t align, size_t size, struct memory_type *type)
Patrick Georgi5ccfa1a2008-09-02 15:49:32 +0000495{
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600496 /* Define a large request to be 1024 bytes for either alignment or
497 * size of allocation. */
498 const size_t large_request = 1024;
499
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000500 if (size == 0) return 0;
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700501 if (type->align_regions == 0) {
502 type->align_regions = malloc(sizeof(struct align_region_t));
503 if (type->align_regions == NULL)
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000504 return NULL;
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700505 memset(type->align_regions, 0, sizeof(struct align_region_t));
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000506 }
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700507 struct align_region_t *reg = type->align_regions;
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600508
509 if (size >= large_request || align >= large_request) {
510 reg = allocate_region(align, 0, size, type);
511 if (reg == NULL)
512 return NULL;
513 return reg->start_data;
514 }
515
Stefan Reinauer14e22772010-04-27 06:56:47 +0000516look_further:
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000517 while (reg != 0)
518 {
519 if ((reg->alignment == align) && (reg->free >= (size + align - 1)/align))
520 {
Julius Wernereab2a292019-03-05 16:55:15 -0800521#if CONFIG(LP_DEBUG_MALLOC)
Julius Wernerf5b76fe2016-05-19 13:15:16 -0700522 printf(" found memalign region. %u free, %zu required\n", reg->free, (size + align - 1)/align);
Stefan Reinauere5d30b72010-03-25 22:15:19 +0000523#endif
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000524 break;
525 }
526 reg = reg->next;
527 }
528 if (reg == 0)
529 {
Julius Wernereab2a292019-03-05 16:55:15 -0800530#if CONFIG(LP_DEBUG_MALLOC)
Stefan Reinauere5d30b72010-03-25 22:15:19 +0000531 printf(" need to allocate a new memalign region\n");
532#endif
533 /* get align regions */
Aaron Durbin8bbd04e2015-01-22 08:59:03 -0600534 reg = allocate_region(align, large_request/align, size, type);
Julius Wernereab2a292019-03-05 16:55:15 -0800535#if CONFIG(LP_DEBUG_MALLOC)
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700536 printf(" ... returned %p\n", reg);
Stefan Reinauere5d30b72010-03-25 22:15:19 +0000537#endif
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000538 }
Stefan Reinauere5d30b72010-03-25 22:15:19 +0000539 if (reg == 0) {
540 /* Nothing available. */
541 return (void *)NULL;
542 }
543
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000544 int i, count = 0, target = (size+align-1)/align;
545 for (i = 0; i < (reg->size/align); i++)
546 {
547 if (((u8*)reg->start)[i] == 0)
548 {
549 count++;
550 if (count == target) {
551 count = i+1-count;
552 for (i=0; i<target-1; i++)
553 {
554 ((u8*)reg->start)[count+i]=2;
555 }
556 ((u8*)reg->start)[count+target-1]=1;
557 reg->free -= target;
558 return reg->start_data+(align*count);
559 }
560 } else {
561 count = 0;
562 }
563 }
Nico Huber7a32e882012-11-22 17:37:32 +0100564 /* The free space in this region is fragmented,
565 so we will move on and try the next one: */
566 reg = reg->next;
Stefan Reinauer1ff26a72009-04-29 19:11:18 +0000567 goto look_further; // end condition is once a new region is allocated - it always has enough space
Patrick Georgi5ccfa1a2008-09-02 15:49:32 +0000568}
569
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700570void *memalign(size_t align, size_t size)
571{
572 return alloc_aligned(align, size, heap);
573}
574
575void *dma_memalign(size_t align, size_t size)
576{
577 return alloc_aligned(align, size, dma);
578}
579
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000580/* This is for debugging purposes. */
Julius Wernereab2a292019-03-05 16:55:15 -0800581#if CONFIG(LP_DEBUG_MALLOC)
Jordan Crousef6145c32008-03-19 23:56:58 +0000582void print_malloc_map(void)
583{
Julius Werner9665d382013-09-13 18:21:46 -0700584 struct memory_type *type = heap;
585 void *ptr;
586 int free_memory;
Jordan Crousef6145c32008-03-19 23:56:58 +0000587
Julius Werner9665d382013-09-13 18:21:46 -0700588again:
589 ptr = type->start;
590 free_memory = 0;
591
592 while (ptr < type->end) {
Jordan Crousef6145c32008-03-19 23:56:58 +0000593 hdrtype_t hdr = *((hdrtype_t *) ptr);
594
595 if (!HAS_MAGIC(hdr)) {
Julius Werner9665d382013-09-13 18:21:46 -0700596 if (type->magic_initialized)
597 printf("%s: Poisoned magic - we're toast\n", type->name);
Stefan Reinauere5d30b72010-03-25 22:15:19 +0000598 else
Julius Werner9665d382013-09-13 18:21:46 -0700599 printf("%s: No magic yet - going to initialize\n", type->name);
Jordan Crousef6145c32008-03-19 23:56:58 +0000600 break;
601 }
602
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000603 /* FIXME: Verify the size of the block. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000604
Julius Wernerf5b76fe2016-05-19 13:15:16 -0700605 printf("%s %x: %s (%llx bytes)\n", type->name,
Julius Werner9665d382013-09-13 18:21:46 -0700606 (unsigned int)(ptr - type->start),
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000607 hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
Jordan Crousef6145c32008-03-19 23:56:58 +0000608
Stefan Reinauere5d30b72010-03-25 22:15:19 +0000609 if (hdr & FLAG_FREE)
610 free_memory += SIZE(hdr);
611
Jordan Crousef6145c32008-03-19 23:56:58 +0000612 ptr += HDRSIZE + SIZE(hdr);
613 }
Stefan Reinauere5d30b72010-03-25 22:15:19 +0000614
Julius Werner9665d382013-09-13 18:21:46 -0700615 if (free_memory && (type->minimal_free > free_memory))
616 type->minimal_free = free_memory;
Julius Wernerf5b76fe2016-05-19 13:15:16 -0700617 printf("%s: Maximum memory consumption: %zu bytes\n", type->name,
Julius Werner9665d382013-09-13 18:21:46 -0700618 (type->end - type->start) - HDRSIZE - type->minimal_free);
619
620 if (type != dma) {
621 type = dma;
622 goto again;
623 }
Jordan Crousef6145c32008-03-19 23:56:58 +0000624}
Jordan Crousef6145c32008-03-19 23:56:58 +0000625#endif