xref: /openbmc/linux/arch/powerpc/boot/simple_alloc.c (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
1e01432baSMichael Ellerman // SPDX-License-Identifier: GPL-2.0
201a63720SMark A. Greer /*
301a63720SMark A. Greer  * Implement primitive realloc(3) functionality.
401a63720SMark A. Greer  *
501a63720SMark A. Greer  * Author: Mark A. Greer <mgreer@mvista.com>
601a63720SMark A. Greer  *
7e01432baSMichael Ellerman  * 2006 (c) MontaVista, Software, Inc.
801a63720SMark A. Greer  */
901a63720SMark A. Greer 
1001a63720SMark A. Greer #include <stddef.h>
1101a63720SMark A. Greer #include "types.h"
1201a63720SMark A. Greer #include "page.h"
1301a63720SMark A. Greer #include "string.h"
1401a63720SMark A. Greer #include "ops.h"
1501a63720SMark A. Greer 
1601a63720SMark A. Greer #define	ENTRY_BEEN_USED	0x01
1701a63720SMark A. Greer #define	ENTRY_IN_USE	0x02
1801a63720SMark A. Greer 
1901a63720SMark A. Greer static struct alloc_info {
204ca478e6SGeert Uytterhoeven 	unsigned long	flags;
214ca478e6SGeert Uytterhoeven 	unsigned long	base;
224ca478e6SGeert Uytterhoeven 	unsigned long	size;
2301a63720SMark A. Greer } *alloc_tbl;
2401a63720SMark A. Greer 
254ca478e6SGeert Uytterhoeven static unsigned long tbl_entries;
264ca478e6SGeert Uytterhoeven static unsigned long alloc_min;
274ca478e6SGeert Uytterhoeven static unsigned long next_base;
284ca478e6SGeert Uytterhoeven static unsigned long space_left;
2901a63720SMark A. Greer 
3001a63720SMark A. Greer /*
3101a63720SMark A. Greer  * First time an entry is used, its base and size are set.
3201a63720SMark A. Greer  * An entry can be freed and re-malloc'd but its base & size don't change.
3301a63720SMark A. Greer  * Should be smart enough for needs of bootwrapper.
3401a63720SMark A. Greer  */
simple_malloc(unsigned long size)354ca478e6SGeert Uytterhoeven static void *simple_malloc(unsigned long size)
3601a63720SMark A. Greer {
374ca478e6SGeert Uytterhoeven 	unsigned long i;
3801a63720SMark A. Greer 	struct alloc_info *p = alloc_tbl;
3901a63720SMark A. Greer 
4001a63720SMark A. Greer 	if (size == 0)
4101a63720SMark A. Greer 		goto err_out;
4201a63720SMark A. Greer 
4301a63720SMark A. Greer 	size = _ALIGN_UP(size, alloc_min);
4401a63720SMark A. Greer 
4501a63720SMark A. Greer 	for (i=0; i<tbl_entries; i++, p++)
4601a63720SMark A. Greer 		if (!(p->flags & ENTRY_BEEN_USED)) { /* never been used */
4701a63720SMark A. Greer 			if (size <= space_left) {
4801a63720SMark A. Greer 				p->base = next_base;
4901a63720SMark A. Greer 				p->size = size;
5001a63720SMark A. Greer 				p->flags = ENTRY_BEEN_USED | ENTRY_IN_USE;
5101a63720SMark A. Greer 				next_base += size;
5201a63720SMark A. Greer 				space_left -= size;
5301a63720SMark A. Greer 				return (void *)p->base;
5401a63720SMark A. Greer 			}
5501a63720SMark A. Greer 			goto err_out; /* not enough space left */
5601a63720SMark A. Greer 		}
5701a63720SMark A. Greer 		/* reuse an entry keeping same base & size */
5801a63720SMark A. Greer 		else if (!(p->flags & ENTRY_IN_USE) && (size <= p->size)) {
5901a63720SMark A. Greer 			p->flags |= ENTRY_IN_USE;
6001a63720SMark A. Greer 			return (void *)p->base;
6101a63720SMark A. Greer 		}
6201a63720SMark A. Greer err_out:
6301a63720SMark A. Greer 	return NULL;
6401a63720SMark A. Greer }
6501a63720SMark A. Greer 
simple_find_entry(void * ptr)6601a63720SMark A. Greer static struct alloc_info *simple_find_entry(void *ptr)
6701a63720SMark A. Greer {
684ca478e6SGeert Uytterhoeven 	unsigned long i;
6901a63720SMark A. Greer 	struct alloc_info *p = alloc_tbl;
7001a63720SMark A. Greer 
7101a63720SMark A. Greer 	for (i=0; i<tbl_entries; i++,p++) {
7201a63720SMark A. Greer 		if (!(p->flags & ENTRY_BEEN_USED))
7301a63720SMark A. Greer 			break;
744ca478e6SGeert Uytterhoeven 		if ((p->flags & ENTRY_IN_USE) &&
754ca478e6SGeert Uytterhoeven 		    (p->base == (unsigned long)ptr))
7601a63720SMark A. Greer 			return p;
7701a63720SMark A. Greer 	}
7801a63720SMark A. Greer 	return NULL;
7901a63720SMark A. Greer }
8001a63720SMark A. Greer 
simple_free(void * ptr)8101a63720SMark A. Greer static void simple_free(void *ptr)
8201a63720SMark A. Greer {
8301a63720SMark A. Greer 	struct alloc_info *p = simple_find_entry(ptr);
8401a63720SMark A. Greer 
8501a63720SMark A. Greer 	if (p != NULL)
8601a63720SMark A. Greer 		p->flags &= ~ENTRY_IN_USE;
8701a63720SMark A. Greer }
8801a63720SMark A. Greer 
8901a63720SMark A. Greer /*
9001a63720SMark A. Greer  * Change size of area pointed to by 'ptr' to 'size'.
9101a63720SMark A. Greer  * If 'ptr' is NULL, then its a malloc().  If 'size' is 0, then its a free().
9201a63720SMark A. Greer  * 'ptr' must be NULL or a pointer to a non-freed area previously returned by
9301a63720SMark A. Greer  * simple_realloc() or simple_malloc().
9401a63720SMark A. Greer  */
simple_realloc(void * ptr,unsigned long size)9501a63720SMark A. Greer static void *simple_realloc(void *ptr, unsigned long size)
9601a63720SMark A. Greer {
9701a63720SMark A. Greer 	struct alloc_info *p;
9801a63720SMark A. Greer 	void *new;
9901a63720SMark A. Greer 
10001a63720SMark A. Greer 	if (size == 0) {
10101a63720SMark A. Greer 		simple_free(ptr);
10201a63720SMark A. Greer 		return NULL;
10301a63720SMark A. Greer 	}
10401a63720SMark A. Greer 
10501a63720SMark A. Greer 	if (ptr == NULL)
10601a63720SMark A. Greer 		return simple_malloc(size);
10701a63720SMark A. Greer 
10801a63720SMark A. Greer 	p = simple_find_entry(ptr);
10901a63720SMark A. Greer 	if (p == NULL) /* ptr not from simple_malloc/simple_realloc */
11001a63720SMark A. Greer 		return NULL;
11101a63720SMark A. Greer 	if (size <= p->size) /* fits in current block */
11201a63720SMark A. Greer 		return ptr;
11301a63720SMark A. Greer 
11401a63720SMark A. Greer 	new = simple_malloc(size);
115*97db7b59SMichael Ellerman 	if (new) {
11601a63720SMark A. Greer 		memcpy(new, ptr, p->size);
11701a63720SMark A. Greer 		simple_free(ptr);
118*97db7b59SMichael Ellerman 	}
119*97db7b59SMichael Ellerman 
12001a63720SMark A. Greer 	return new;
12101a63720SMark A. Greer }
12201a63720SMark A. Greer 
12301a63720SMark A. Greer /*
12401a63720SMark A. Greer  * Returns addr of first byte after heap so caller can see if it took
12501a63720SMark A. Greer  * too much space.  If so, change args & try again.
12601a63720SMark A. Greer  */
simple_alloc_init(char * base,unsigned long heap_size,unsigned long granularity,unsigned long max_allocs)1274ca478e6SGeert Uytterhoeven void *simple_alloc_init(char *base, unsigned long heap_size,
1284ca478e6SGeert Uytterhoeven 			unsigned long granularity, unsigned long max_allocs)
12901a63720SMark A. Greer {
1304ca478e6SGeert Uytterhoeven 	unsigned long heap_base, tbl_size;
13101a63720SMark A. Greer 
13201a63720SMark A. Greer 	heap_size = _ALIGN_UP(heap_size, granularity);
13301a63720SMark A. Greer 	alloc_min = granularity;
13401a63720SMark A. Greer 	tbl_entries = max_allocs;
13501a63720SMark A. Greer 
13601a63720SMark A. Greer 	tbl_size = tbl_entries * sizeof(struct alloc_info);
13701a63720SMark A. Greer 
13801a63720SMark A. Greer 	alloc_tbl = (struct alloc_info *)_ALIGN_UP((unsigned long)base, 8);
13901a63720SMark A. Greer 	memset(alloc_tbl, 0, tbl_size);
14001a63720SMark A. Greer 
1414ca478e6SGeert Uytterhoeven 	heap_base = _ALIGN_UP((unsigned long)alloc_tbl + tbl_size, alloc_min);
14201a63720SMark A. Greer 
14301a63720SMark A. Greer 	next_base = heap_base;
14401a63720SMark A. Greer 	space_left = heap_size;
14501a63720SMark A. Greer 
14601a63720SMark A. Greer 	platform_ops.malloc = simple_malloc;
14701a63720SMark A. Greer 	platform_ops.free = simple_free;
14801a63720SMark A. Greer 	platform_ops.realloc = simple_realloc;
14901a63720SMark A. Greer 
15001a63720SMark A. Greer 	return (void *)(heap_base + heap_size);
15101a63720SMark A. Greer }
152