blob: d3e73a5fdf110ebea28cfe89e0a47af5e7e79967 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2017 MediaTek Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <err.h>
24#include <lib/mempool.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <unittest.h>
28
29struct mempool_range {
30 void *start;
31 size_t size;
32 uint32_t type;
33};
34
35struct mempool_range test_mempool_range[] = {
36 { (void *)0x40000000, 0x20000, MEMPOOL_CACHE },
37 { (void *)0x40020000, 0x20000, MEMPOOL_UNCACHE },
38 { (void *)0x60000000, 0x20000, MEMPOOL_UNCACHE },
39 { (void *)0x60020000, 0x20000, MEMPOOL_CACHE }
40};
41
42#define NUM_OF_TEST_MEM_RANGE (sizeof(test_mempool_range)/sizeof(struct mempool_range))
43
44static int test_init_mempool(uint32_t type)
45{
46 uint32_t i;
47 uint32_t s_type, e_type;
48 int ret;
49 struct mempool_range *m;
50
51 mempool_clear();
52
53 ret = 0;
54 for (i = 0; i < NUM_OF_TEST_MEM_RANGE; i++) {
55 m = &test_mempool_range[i];
56 if ((type != MEMPOOL_ANY) && (m->type != type))
57 continue;
58
59 ret = mempool_init(m->start, m->size, m->type);
60 if (ret != NO_ERROR)
61 return ret;
62 }
63
64 return NO_ERROR;
65}
66
67static bool test_mempool_init(void)
68{
69 BEGIN_TEST;
70
71 int ret;
72
73 mempool_clear();
74
75 /* input arguments check */
76 ret = mempool_init(NULL, 0x20000, MEMPOOL_CACHE);
77 EXPECT_EQ(ERR_INVALID_ARGS, ret, "mempool_init should fail with mem:NULL");
78 ret = mempool_init((void *)0x40000000, 0, MEMPOOL_CACHE);
79 EXPECT_EQ(ERR_INVALID_ARGS, ret, "mempool_init should fail with size:0");
80 ret = mempool_init((void *)0x40000000, 0x20000, MAX_MEMPOOL_TYPE);
81 EXPECT_EQ(ERR_INVALID_ARGS, ret,
82 "mempool_init should fail with invalid type");
83 ret = mempool_init((void *)0x40000003, 0x20000, MEMPOOL_CACHE);
84 EXPECT_EQ(ERR_INVALID_ARGS, ret,
85 "mempool_init should fail with non cache line aligned address");
86
87 EXPECT_EQ(NO_ERROR, test_init_mempool(MEMPOOL_ANY), "fail to init mempool");
88
89 /* memory pool region overlap check */
90 ret = mempool_init((void *)0x40000000, 0x1000, MEMPOOL_CACHE);
91 EXPECT_EQ(ERR_ALREADY_EXISTS, ret,
92 "mempool_init should fail when memory exists in pool");
93 ret = mempool_init((void *)0x40001000, 0x1000, MEMPOOL_CACHE);
94 EXPECT_EQ(ERR_ALREADY_EXISTS, ret,
95 "mempool_init should fail when memory exists in pool");
96 ret = mempool_init((void *)0x40000000, 0x1000, MEMPOOL_UNCACHE);
97 EXPECT_EQ(ERR_ALREADY_EXISTS, ret,
98 "mempool_init should fail when memory exists in other pool");
99 ret = mempool_init((void *)0x60000000, 0x1000, MEMPOOL_UNCACHE);
100 EXPECT_EQ(ERR_ALREADY_EXISTS, ret,
101 "mempool_init should fail when memory exists in pool");
102 ret = mempool_init((void *)0x60008000, 0x20000, MEMPOOL_UNCACHE);
103 EXPECT_EQ(ERR_ALREADY_EXISTS, ret,
104 "mempool_init should fail when memory exists in pool");
105 ret = mempool_init((void *)0x60001000, 0x20000, MEMPOOL_CACHE);
106 EXPECT_EQ(ERR_ALREADY_EXISTS, ret,
107 "mempool_init should fail when memory exists in other pool");
108
109 /* add new memory to pools */
110 ret = mempool_init((void *)0x40040000, 0x30000, MEMPOOL_CACHE);
111 EXPECT_EQ(NO_ERROR, ret,
112 "fail to init memory of MEMPOOL_CACHE with new memory range");
113 ret = mempool_init((void *)0x40080000, 0x40000, MEMPOOL_UNCACHE);
114 EXPECT_EQ(NO_ERROR, ret,
115 "fail to init memory of MEMPOOL_UNCACHE with new memory range");
116
117 END_TEST;
118}
119
120static bool test_mempool_alloc_free(void)
121{
122 BEGIN_TEST;
123
124 int i;
125 uintptr_t mem;
126
127 EXPECT_EQ(NO_ERROR, test_init_mempool(MEMPOOL_ANY), "fail to init mempool");
128
129 /* input argument check */
130 mem = (uintptr_t)mempool_alloc(0, MEMPOOL_CACHE);
131 EXPECT_EQ((uintptr_t)NULL, mem, "mempool_alloc should fail when size = 0");
132 mem = (uintptr_t)mempool_alloc(0x1000, MAX_MEMPOOL_TYPE);
133 EXPECT_EQ((uintptr_t)NULL, mem,
134 "mempool_alloc should fail with invalid mempool type");
135
136 /* alloc fail test */
137 mem = (uintptr_t)mempool_alloc(0x40000, MEMPOOL_CACHE);
138 EXPECT_EQ((uintptr_t)NULL, mem,
139 "mempool_alloc should fail when not enough memory");
140 mem = (uintptr_t)mempool_alloc(0x30000, MEMPOOL_UNCACHE);
141 EXPECT_EQ((uintptr_t)NULL, mem,
142 "mempool_alloc should fail when not enough memory");
143
144 /* free test */
145 mem = (uintptr_t)mempool_alloc(0x20000, MEMPOOL_CACHE);
146 EXPECT_EQ(0x40000000UL, mem, "fail to alloc from cache pool");
147 mempool_free((void *)mem);
148 mem = (uintptr_t)mempool_alloc(0x20000, MEMPOOL_CACHE);
149 EXPECT_EQ(0x40000000UL, mem,
150 "fail to alloc size:0x20000 after free from cache pool");
151
152 /* alloc from 2nd mempool */
153 mem = (uintptr_t)mempool_alloc(0x20000, MEMPOOL_CACHE);
154 EXPECT_EQ(0x60020000UL, mem, "fail to alloc from 2nd cache pool");
155 mempool_free((void *)0x40000000UL);
156 mempool_free((void *)0x60020000UL);
157
158 /* round up size to multiple cache line alloc test */
159 mempool_alloc(0x3, MEMPOOL_CACHE);
160 mempool_alloc(0x8, MEMPOOL_CACHE);
161 mempool_alloc(0x11, MEMPOOL_CACHE);
162 mempool_alloc(0x21, MEMPOOL_CACHE);
163 mem = (uintptr_t)mempool_alloc(0x1000, MEMPOOL_CACHE);
164 i = ROUNDUP(0x3, CACHE_LINE) +
165 ROUNDUP(0x8, CACHE_LINE) +
166 ROUNDUP(0x11, CACHE_LINE) +
167 ROUNDUP(0x21, CACHE_LINE);
168 EXPECT_EQ(0x40000000UL + i, mem,
169 "fail to round up alloc size from cache pool");
170
171 test_init_mempool(MEMPOOL_ANY);
172
173 mem = (uintptr_t)mempool_alloc(0xffff, MEMPOOL_CACHE);
174 EXPECT_EQ(0x40000000UL, mem, "fail to alloc size: 0xffff from cache pool");
175 mem = (uintptr_t)mempool_alloc(0x10000, MEMPOOL_CACHE);
176 EXPECT_EQ(0x40010000UL, mem,
177 "fail to alloc size: 0x10000 after round up size alloc");
178 mem = (uintptr_t)mempool_alloc(0x1ffff, MEMPOOL_CACHE);
179 EXPECT_EQ(0x60020000UL, mem, "fail to alloc from 2nd cache pool");
180 mem = (uintptr_t)mempool_alloc(0x1000, MEMPOOL_CACHE);
181 EXPECT_EQ((uintptr_t)NULL, mem,
182 "mempool_alloc should fail when no enoguh memory");
183
184 /* alloc test from uncache pool */
185 mem = (uintptr_t)mempool_alloc(0x10000, MEMPOOL_UNCACHE);
186 EXPECT_EQ(0x40020000UL, mem, "fail to alloc from uncache pool");
187 mem = (uintptr_t)mempool_alloc(0x1ffff, MEMPOOL_UNCACHE);
188 EXPECT_EQ(0x60000000UL, mem, "fail to alloc from uncache pool");
189 mem = (uintptr_t)mempool_alloc(0x1000, MEMPOOL_UNCACHE);
190 EXPECT_EQ(0x40030000UL, mem, "fail to alloc from uncache pool");
191
192 /* free all previously allocated mem */
193 mempool_free((void *)0x40000000UL);
194 mempool_free((void *)0x40010000UL);
195 mempool_free((void *)0x60020000UL);
196 mempool_free((void *)0x40020000UL);
197 mempool_free((void *)0x40030000UL);
198 mempool_free((void *)0x60000000UL);
199
200 /* interleaved alloc and free test */
201 for (i = 0; i < 0x20000; i += 0x1000) {
202 mem = (uintptr_t)mempool_alloc(0x1000, MEMPOOL_CACHE);
203 EXPECT_EQ(0x40000000UL + i, mem,
204 "fail to continuously alloc from 1st cache pool");
205 }
206
207 for (i = 0; i < 0x20000; i += 0x1000) {
208 mem = (uintptr_t)mempool_alloc(0x1000, MEMPOOL_CACHE);
209 EXPECT_EQ(0x60020000UL + i, mem,
210 "fail to continuously alloc from 1st cache pool");
211 }
212
213 for (i = 0; i < 0x20000; i += 0x1000) {
214 mem = (uintptr_t)mempool_alloc(0x1000, MEMPOOL_UNCACHE);
215 EXPECT_EQ(0x40020000UL + i, mem,
216 "fail to continuously alloc frmo 1st uncache pool");
217 }
218
219 for (i = 0; i < 0x20000; i += 0x1000) {
220 mem = (uintptr_t)mempool_alloc(0x1000, MEMPOOL_UNCACHE);
221 EXPECT_EQ(0x60000000UL + i, mem,
222 "fail to continuously alloc frmo 1st uncache pool");
223 }
224
225 mempool_free((void *)0x40000000);
226 mempool_free((void *)0x40002000);
227 mempool_free((void *)0x4001f000);
228 mempool_free((void *)0x40020000);
229 mempool_free((void *)0x40022000);
230 mempool_free((void *)0x4003f000);
231 mempool_free((void *)0x4003e000);
232
233 mem = (uintptr_t)mempool_alloc(0x2000, MEMPOOL_CACHE);
234 EXPECT_EQ((uintptr_t)NULL, mem,
235 "mempool_alloc should fail when no enough memory");
236 mempool_free((void *)0x40001000);
237 mem = (uintptr_t)mempool_alloc(0x3000, MEMPOOL_CACHE);
238 EXPECT_EQ(0x40000000, mem,
239 "fail to alloc size:0x3000 from 1st cache pool after free");
240 mem = (uintptr_t)mempool_alloc(0x2000, MEMPOOL_CACHE);
241 EXPECT_EQ((uintptr_t)NULL, mem,
242 "mempool_alloc should fail when no enough memory");
243 mem = (uintptr_t)mempool_alloc(0x1000, MEMPOOL_CACHE);
244 EXPECT_EQ(0x4001f000, mem,
245 "fail to alloc size:0x1000 at the end of 1st cache pool");
246
247 mem = (uintptr_t)mempool_alloc(0x4000, MEMPOOL_UNCACHE);
248 EXPECT_EQ((uintptr_t)NULL, mem,
249 "mempool_alloc should fail when no enough memory");
250 mempool_free((void *)0x4003d000);
251 mempool_free((void *)0x4003c000);
252 mem = (uintptr_t)mempool_alloc(0x4000, MEMPOOL_UNCACHE);
253 EXPECT_EQ(0x4003c000, mem,
254 "fail to alloc size:0x4000 at the end of 1st uncache pool");
255 mempool_free((void *)0x40021000);
256 mem = (uintptr_t)mempool_alloc(0x4000, MEMPOOL_UNCACHE);
257 EXPECT_EQ((uintptr_t)NULL, mem,
258 "mempool_alloc should fail when no enough memory");
259 mempool_free((void *)0x40023000);
260 mem = (uintptr_t)mempool_alloc(0x4000, MEMPOOL_UNCACHE);
261 EXPECT_EQ(0x40020000, mem,
262 "fail to alloc size:0x4000 at the begin of 1st uncache pool");
263
264 END_TEST;
265}
266
267static bool test_mempool_alloc_any(void)
268{
269 BEGIN_TEST;
270
271 uintptr_t mem;
272
273 /* init mempool with both cached and uncached mempool */
274 EXPECT_EQ(NO_ERROR, test_init_mempool(MEMPOOL_ANY), "fail to init mempool");
275
276 mem = (uintptr_t)mempool_alloc(0x4000, MEMPOOL_ANY);
277 EXPECT_EQ(0x40000000, mem,
278 "fail to alloc from cache mem for MEMPOOL_ANY");
279 mem = (uintptr_t)mempool_alloc(0x20000, MEMPOOL_ANY);
280 EXPECT_EQ(0x60020000, mem,
281 "fail to alloc from uncached mem for MEMPOOL_ANY");
282 mem = (uintptr_t)mempool_alloc(0x1000, MEMPOOL_ANY);
283 EXPECT_EQ(0x40004000, mem,
284 "fail to alloc from cached mem for MEMPOOL_ANY");
285 mem = (uintptr_t)mempool_alloc(0x1000, MEMPOOL_CACHE);
286 EXPECT_EQ(0x40005000, mem,
287 "fail to alloc from cached mem for MEMPOOL_CACHE");
288 mem = (uintptr_t)mempool_alloc(0x1f000, MEMPOOL_ANY);
289 EXPECT_EQ(0x40020000, mem,
290 "fail to alloc from uncached mem for MEMPOOL_ANY");
291
292 /* init only cached mempool */
293 EXPECT_EQ(NO_ERROR, test_init_mempool(MEMPOOL_CACHE),
294 "fail to init mempool");
295
296 mem = (uintptr_t)mempool_alloc(0x2000, MEMPOOL_ANY);
297 EXPECT_EQ(0x40000000, mem,
298 "fail to alloc from cached mem for MEMPOOL_ANY");
299 mem = (uintptr_t)mempool_alloc(0x2000, MEMPOOL_UNCACHE);
300 EXPECT_EQ((uintptr_t)NULL, mem,
301 "mempool_alloc should fail when no uncached mempool");
302 mem = (uintptr_t)mempool_alloc(0x2000, MEMPOOL_CACHE);
303 EXPECT_EQ(0x40002000, mem,
304 "fail to alloc from cached mem for MEMPOOL_CACHE");
305 mem = (uintptr_t)mempool_alloc(0x20000, MEMPOOL_ANY);
306 EXPECT_EQ(0x60020000, mem,
307 "fail to alloc from cached mem for MEMPOOL_CACHE");
308
309 /* init only uncached mempool */
310 EXPECT_EQ(NO_ERROR, test_init_mempool(MEMPOOL_UNCACHE),
311 "fail to init mempool");
312
313 mem = (uintptr_t)mempool_alloc(0x2000, MEMPOOL_ANY);
314 EXPECT_EQ(0x40020000, mem,
315 "fail to alloc from uncached mem for MEMPOOL_ANY");
316 mem = (uintptr_t)mempool_alloc(0x2000, MEMPOOL_CACHE);
317 EXPECT_EQ((uintptr_t)NULL, mem,
318 "mempool_alloc should fail when no cached mempool");
319 mem = (uintptr_t)mempool_alloc(0x2000, MEMPOOL_UNCACHE);
320 EXPECT_EQ(0x40022000, mem,
321 "fail to alloc from uncached mem for MEMPOOL_UNCACHE");
322 mem = (uintptr_t)mempool_alloc(0x20000, MEMPOOL_ANY);
323 EXPECT_EQ(0x60000000, mem,
324 "fail to alloc from uncached mem for MEMPOOL_ANY");
325
326 END_TEST;
327}
328
329BEGIN_TEST_CASE(mempool_tests);
330RUN_TEST(test_mempool_init);
331RUN_TEST(test_mempool_alloc_free);
332RUN_TEST(test_mempool_alloc_any);
333END_TEST_CASE(mempool_tests);