1 /*
2  * Copyright (c) 2021 Carlo Caione, <ccaione@baylibre.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/sys/sys_heap.h>
10 #include <zephyr/sys/multi_heap.h>
11 
12 #include <zephyr/multi_heap/shared_multi_heap.h>
13 
14 static struct sys_multi_heap shared_multi_heap;
15 
16 static struct {
17 	struct sys_heap heap_pool[MAX_MULTI_HEAPS];
18 	unsigned int heap_cnt;
19 } smh_data[MAX_SHARED_MULTI_HEAP_ATTR];
20 
smh_choice(struct sys_multi_heap * mheap,void * cfg,size_t align,size_t size)21 static void *smh_choice(struct sys_multi_heap *mheap, void *cfg, size_t align, size_t size)
22 {
23 	struct sys_heap *h;
24 	enum shared_multi_heap_attr attr;
25 	void *block;
26 
27 	attr = (enum shared_multi_heap_attr)(long) cfg;
28 
29 	if (attr >= MAX_SHARED_MULTI_HEAP_ATTR || size == 0) {
30 		return NULL;
31 	}
32 
33 	/* Set in case the user requested a non-existing attr */
34 	block = NULL;
35 
36 	for (size_t hdx = 0; hdx < smh_data[attr].heap_cnt; hdx++) {
37 		h = &smh_data[attr].heap_pool[hdx];
38 
39 		if (h->heap == NULL) {
40 			return NULL;
41 		}
42 
43 		block = sys_heap_aligned_alloc(h, align, size);
44 		if (block != NULL) {
45 			break;
46 		}
47 	}
48 
49 	return block;
50 }
51 
shared_multi_heap_add(struct shared_multi_heap_region * region,void * user_data)52 int shared_multi_heap_add(struct shared_multi_heap_region *region, void *user_data)
53 {
54 	enum shared_multi_heap_attr attr;
55 	struct sys_heap *h;
56 	unsigned int slot;
57 
58 	attr = region->attr;
59 
60 	if (attr >= MAX_SHARED_MULTI_HEAP_ATTR) {
61 		return -EINVAL;
62 	}
63 
64 	/* No more heaps available */
65 	if (smh_data[attr].heap_cnt >= MAX_MULTI_HEAPS) {
66 		return -ENOMEM;
67 	}
68 
69 	slot = smh_data[attr].heap_cnt;
70 	h = &smh_data[attr].heap_pool[slot];
71 
72 	sys_heap_init(h, (void *) region->addr, region->size);
73 	sys_multi_heap_add_heap(&shared_multi_heap, h, user_data);
74 
75 	smh_data[attr].heap_cnt++;
76 
77 	return 0;
78 }
79 
shared_multi_heap_free(void * block)80 void shared_multi_heap_free(void *block)
81 {
82 	sys_multi_heap_free(&shared_multi_heap, block);
83 }
84 
shared_multi_heap_alloc(enum shared_multi_heap_attr attr,size_t bytes)85 void *shared_multi_heap_alloc(enum shared_multi_heap_attr attr, size_t bytes)
86 {
87 	if (attr >= MAX_SHARED_MULTI_HEAP_ATTR) {
88 		return NULL;
89 	}
90 
91 	return sys_multi_heap_alloc(&shared_multi_heap, (void *)(long) attr, bytes);
92 }
93 
shared_multi_heap_aligned_alloc(enum shared_multi_heap_attr attr,size_t align,size_t bytes)94 void *shared_multi_heap_aligned_alloc(enum shared_multi_heap_attr attr,
95 				      size_t align, size_t bytes)
96 {
97 	if (attr >= MAX_SHARED_MULTI_HEAP_ATTR) {
98 		return NULL;
99 	}
100 
101 	return sys_multi_heap_aligned_alloc(&shared_multi_heap, (void *)(long) attr,
102 					    align, bytes);
103 }
104 
shared_multi_heap_pool_init(void)105 int shared_multi_heap_pool_init(void)
106 {
107 	static atomic_t state;
108 
109 	if (!atomic_cas(&state, 0, 1)) {
110 		return -EALREADY;
111 	}
112 
113 	sys_multi_heap_init(&shared_multi_heap, smh_choice);
114 
115 	atomic_set(&state, 1);
116 
117 	return 0;
118 }
119