blob: 1336b54b11f214565d3852061fb1e2c938111352 [file] [log] [blame]
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018 Simon Goldschmidt
4 */
5
6#include <common.h>
7#include <lmb.h>
Simon Glass9bc15642020-02-03 07:36:16 -07008#include <malloc.h>
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +01009#include <dm/test.h>
10#include <test/ut.h>
11
12static int check_lmb(struct unit_test_state *uts, struct lmb *lmb,
13 phys_addr_t ram_base, phys_size_t ram_size,
14 unsigned long num_reserved,
15 phys_addr_t base1, phys_size_t size1,
16 phys_addr_t base2, phys_size_t size2,
17 phys_addr_t base3, phys_size_t size3)
18{
Simon Goldschmidtc722dac2019-02-01 21:23:59 +010019 if (ram_size) {
20 ut_asserteq(lmb->memory.cnt, 1);
21 ut_asserteq(lmb->memory.region[0].base, ram_base);
22 ut_asserteq(lmb->memory.region[0].size, ram_size);
23 }
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010024
25 ut_asserteq(lmb->reserved.cnt, num_reserved);
26 if (num_reserved > 0) {
27 ut_asserteq(lmb->reserved.region[0].base, base1);
28 ut_asserteq(lmb->reserved.region[0].size, size1);
29 }
30 if (num_reserved > 1) {
31 ut_asserteq(lmb->reserved.region[1].base, base2);
32 ut_asserteq(lmb->reserved.region[1].size, size2);
33 }
34 if (num_reserved > 2) {
35 ut_asserteq(lmb->reserved.region[2].base, base3);
36 ut_asserteq(lmb->reserved.region[2].size, size3);
37 }
38 return 0;
39}
40
41#define ASSERT_LMB(lmb, ram_base, ram_size, num_reserved, base1, size1, \
42 base2, size2, base3, size3) \
43 ut_assert(!check_lmb(uts, lmb, ram_base, ram_size, \
44 num_reserved, base1, size1, base2, size2, base3, \
45 size3))
46
47/*
48 * Test helper function that reserves 64 KiB somewhere in the simulated RAM and
49 * then does some alloc + free tests.
50 */
Simon Goldschmidtc722dac2019-02-01 21:23:59 +010051static int test_multi_alloc(struct unit_test_state *uts, const phys_addr_t ram,
52 const phys_size_t ram_size, const phys_addr_t ram0,
53 const phys_size_t ram0_size,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010054 const phys_addr_t alloc_64k_addr)
55{
56 const phys_addr_t ram_end = ram + ram_size;
57 const phys_addr_t alloc_64k_end = alloc_64k_addr + 0x10000;
58
59 struct lmb lmb;
60 long ret;
61 phys_addr_t a, a2, b, b2, c, d;
62
63 /* check for overflow */
64 ut_assert(ram_end == 0 || ram_end > ram);
65 ut_assert(alloc_64k_end > alloc_64k_addr);
66 /* check input addresses + size */
67 ut_assert(alloc_64k_addr >= ram + 8);
68 ut_assert(alloc_64k_end <= ram_end - 8);
69
70 lmb_init(&lmb);
71
Simon Goldschmidtc722dac2019-02-01 21:23:59 +010072 if (ram0_size) {
73 ret = lmb_add(&lmb, ram0, ram0_size);
74 ut_asserteq(ret, 0);
75 }
76
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010077 ret = lmb_add(&lmb, ram, ram_size);
78 ut_asserteq(ret, 0);
79
Simon Goldschmidtc722dac2019-02-01 21:23:59 +010080 if (ram0_size) {
81 ut_asserteq(lmb.memory.cnt, 2);
82 ut_asserteq(lmb.memory.region[0].base, ram0);
83 ut_asserteq(lmb.memory.region[0].size, ram0_size);
84 ut_asserteq(lmb.memory.region[1].base, ram);
85 ut_asserteq(lmb.memory.region[1].size, ram_size);
86 } else {
87 ut_asserteq(lmb.memory.cnt, 1);
88 ut_asserteq(lmb.memory.region[0].base, ram);
89 ut_asserteq(lmb.memory.region[0].size, ram_size);
90 }
91
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010092 /* reserve 64KiB somewhere */
93 ret = lmb_reserve(&lmb, alloc_64k_addr, 0x10000);
94 ut_asserteq(ret, 0);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +010095 ASSERT_LMB(&lmb, 0, 0, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010096 0, 0, 0, 0);
97
98 /* allocate somewhere, should be at the end of RAM */
99 a = lmb_alloc(&lmb, 4, 1);
100 ut_asserteq(a, ram_end - 4);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100101 ASSERT_LMB(&lmb, 0, 0, 2, alloc_64k_addr, 0x10000,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100102 ram_end - 4, 4, 0, 0);
103 /* alloc below end of reserved region -> below reserved region */
104 b = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
105 ut_asserteq(b, alloc_64k_addr - 4);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100106 ASSERT_LMB(&lmb, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100107 alloc_64k_addr - 4, 0x10000 + 4, ram_end - 4, 4, 0, 0);
108
109 /* 2nd time */
110 c = lmb_alloc(&lmb, 4, 1);
111 ut_asserteq(c, ram_end - 8);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100112 ASSERT_LMB(&lmb, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100113 alloc_64k_addr - 4, 0x10000 + 4, ram_end - 8, 8, 0, 0);
114 d = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
115 ut_asserteq(d, alloc_64k_addr - 8);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100116 ASSERT_LMB(&lmb, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100117 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);
118
119 ret = lmb_free(&lmb, a, 4);
120 ut_asserteq(ret, 0);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100121 ASSERT_LMB(&lmb, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100122 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
123 /* allocate again to ensure we get the same address */
124 a2 = lmb_alloc(&lmb, 4, 1);
125 ut_asserteq(a, a2);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100126 ASSERT_LMB(&lmb, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100127 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);
128 ret = lmb_free(&lmb, a2, 4);
129 ut_asserteq(ret, 0);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100130 ASSERT_LMB(&lmb, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100131 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
132
133 ret = lmb_free(&lmb, b, 4);
134 ut_asserteq(ret, 0);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100135 ASSERT_LMB(&lmb, 0, 0, 3,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100136 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000,
137 ram_end - 8, 4);
138 /* allocate again to ensure we get the same address */
139 b2 = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
140 ut_asserteq(b, b2);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100141 ASSERT_LMB(&lmb, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100142 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
143 ret = lmb_free(&lmb, b2, 4);
144 ut_asserteq(ret, 0);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100145 ASSERT_LMB(&lmb, 0, 0, 3,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100146 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000,
147 ram_end - 8, 4);
148
149 ret = lmb_free(&lmb, c, 4);
150 ut_asserteq(ret, 0);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100151 ASSERT_LMB(&lmb, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100152 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, 0, 0);
153 ret = lmb_free(&lmb, d, 4);
154 ut_asserteq(ret, 0);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100155 ASSERT_LMB(&lmb, 0, 0, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100156 0, 0, 0, 0);
157
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100158 if (ram0_size) {
159 ut_asserteq(lmb.memory.cnt, 2);
160 ut_asserteq(lmb.memory.region[0].base, ram0);
161 ut_asserteq(lmb.memory.region[0].size, ram0_size);
162 ut_asserteq(lmb.memory.region[1].base, ram);
163 ut_asserteq(lmb.memory.region[1].size, ram_size);
164 } else {
165 ut_asserteq(lmb.memory.cnt, 1);
166 ut_asserteq(lmb.memory.region[0].base, ram);
167 ut_asserteq(lmb.memory.region[0].size, ram_size);
168 }
169
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100170 return 0;
171}
172
173static int test_multi_alloc_512mb(struct unit_test_state *uts,
174 const phys_addr_t ram)
175{
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100176 return test_multi_alloc(uts, ram, 0x20000000, 0, 0, ram + 0x10000000);
177}
178
179static int test_multi_alloc_512mb_x2(struct unit_test_state *uts,
180 const phys_addr_t ram,
181 const phys_addr_t ram0)
182{
183 return test_multi_alloc(uts, ram, 0x20000000, ram0, 0x20000000,
184 ram + 0x10000000);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100185}
186
187/* Create a memory region with one reserved region and allocate */
188static int lib_test_lmb_simple(struct unit_test_state *uts)
189{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100190 int ret;
191
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100192 /* simulate 512 MiB RAM beginning at 1GiB */
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100193 ret = test_multi_alloc_512mb(uts, 0x40000000);
194 if (ret)
195 return ret;
196
197 /* simulate 512 MiB RAM beginning at 1.5GiB */
198 return test_multi_alloc_512mb(uts, 0xE0000000);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100199}
200
201DM_TEST(lib_test_lmb_simple, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
202
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100203/* Create two memory regions with one reserved region and allocate */
204static int lib_test_lmb_simple_x2(struct unit_test_state *uts)
205{
206 int ret;
207
208 /* simulate 512 MiB RAM beginning at 2GiB and 1 GiB */
209 ret = test_multi_alloc_512mb_x2(uts, 0x80000000, 0x40000000);
210 if (ret)
211 return ret;
212
213 /* simulate 512 MiB RAM beginning at 3.5GiB and 1 GiB */
214 return test_multi_alloc_512mb_x2(uts, 0xE0000000, 0x40000000);
215}
216
217DM_TEST(lib_test_lmb_simple_x2, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
218
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100219/* Simulate 512 MiB RAM, allocate some blocks that fit/don't fit */
220static int test_bigblock(struct unit_test_state *uts, const phys_addr_t ram)
221{
222 const phys_size_t ram_size = 0x20000000;
223 const phys_size_t big_block_size = 0x10000000;
224 const phys_addr_t ram_end = ram + ram_size;
225 const phys_addr_t alloc_64k_addr = ram + 0x10000000;
226 struct lmb lmb;
227 long ret;
228 phys_addr_t a, b;
229
230 /* check for overflow */
231 ut_assert(ram_end == 0 || ram_end > ram);
232
233 lmb_init(&lmb);
234
235 ret = lmb_add(&lmb, ram, ram_size);
236 ut_asserteq(ret, 0);
237
238 /* reserve 64KiB in the middle of RAM */
239 ret = lmb_reserve(&lmb, alloc_64k_addr, 0x10000);
240 ut_asserteq(ret, 0);
241 ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
242 0, 0, 0, 0);
243
244 /* allocate a big block, should be below reserved */
245 a = lmb_alloc(&lmb, big_block_size, 1);
246 ut_asserteq(a, ram);
247 ASSERT_LMB(&lmb, ram, ram_size, 1, a,
248 big_block_size + 0x10000, 0, 0, 0, 0);
249 /* allocate 2nd big block */
250 /* This should fail, printing an error */
251 b = lmb_alloc(&lmb, big_block_size, 1);
252 ut_asserteq(b, 0);
253 ASSERT_LMB(&lmb, ram, ram_size, 1, a,
254 big_block_size + 0x10000, 0, 0, 0, 0);
255
256 ret = lmb_free(&lmb, a, big_block_size);
257 ut_asserteq(ret, 0);
258 ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
259 0, 0, 0, 0);
260
261 /* allocate too big block */
262 /* This should fail, printing an error */
263 a = lmb_alloc(&lmb, ram_size, 1);
264 ut_asserteq(a, 0);
265 ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
266 0, 0, 0, 0);
267
268 return 0;
269}
270
271static int lib_test_lmb_big(struct unit_test_state *uts)
272{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100273 int ret;
274
275 /* simulate 512 MiB RAM beginning at 1GiB */
276 ret = test_bigblock(uts, 0x40000000);
277 if (ret)
278 return ret;
279
280 /* simulate 512 MiB RAM beginning at 1.5GiB */
281 return test_bigblock(uts, 0xE0000000);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100282}
283
284DM_TEST(lib_test_lmb_big, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
285
286/* Simulate 512 MiB RAM, allocate a block without previous reservation */
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100287static int test_noreserved(struct unit_test_state *uts, const phys_addr_t ram,
288 const phys_addr_t alloc_size, const ulong align)
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100289{
290 const phys_size_t ram_size = 0x20000000;
291 const phys_addr_t ram_end = ram + ram_size;
292 struct lmb lmb;
293 long ret;
294 phys_addr_t a, b;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100295 const phys_addr_t alloc_size_aligned = (alloc_size + align - 1) &
296 ~(align - 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100297
298 /* check for overflow */
299 ut_assert(ram_end == 0 || ram_end > ram);
300
301 lmb_init(&lmb);
302
303 ret = lmb_add(&lmb, ram, ram_size);
304 ut_asserteq(ret, 0);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100305 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100306
307 /* allocate a block */
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100308 a = lmb_alloc(&lmb, alloc_size, align);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100309 ut_assert(a != 0);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100310 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
311 alloc_size, 0, 0, 0, 0);
312 /* allocate another block */
313 b = lmb_alloc(&lmb, alloc_size, align);
314 ut_assert(b != 0);
315 if (alloc_size == alloc_size_aligned) {
316 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size -
317 (alloc_size_aligned * 2), alloc_size * 2, 0, 0, 0,
318 0);
319 } else {
320 ASSERT_LMB(&lmb, ram, ram_size, 2, ram + ram_size -
321 (alloc_size_aligned * 2), alloc_size, ram + ram_size
322 - alloc_size_aligned, alloc_size, 0, 0);
323 }
324 /* and free them */
325 ret = lmb_free(&lmb, b, alloc_size);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100326 ut_asserteq(ret, 0);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100327 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
328 alloc_size, 0, 0, 0, 0);
329 ret = lmb_free(&lmb, a, alloc_size);
330 ut_asserteq(ret, 0);
331 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100332
333 /* allocate a block with base*/
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100334 b = lmb_alloc_base(&lmb, alloc_size, align, ram_end);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100335 ut_assert(a == b);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100336 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
337 alloc_size, 0, 0, 0, 0);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100338 /* and free it */
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100339 ret = lmb_free(&lmb, b, alloc_size);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100340 ut_asserteq(ret, 0);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100341 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100342
343 return 0;
344}
345
346static int lib_test_lmb_noreserved(struct unit_test_state *uts)
347{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100348 int ret;
349
350 /* simulate 512 MiB RAM beginning at 1GiB */
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100351 ret = test_noreserved(uts, 0x40000000, 4, 1);
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100352 if (ret)
353 return ret;
354
355 /* simulate 512 MiB RAM beginning at 1.5GiB */
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100356 return test_noreserved(uts, 0xE0000000, 4, 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100357}
358
359DM_TEST(lib_test_lmb_noreserved, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
360
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100361static int lib_test_lmb_unaligned_size(struct unit_test_state *uts)
362{
363 int ret;
364
365 /* simulate 512 MiB RAM beginning at 1GiB */
366 ret = test_noreserved(uts, 0x40000000, 5, 8);
367 if (ret)
368 return ret;
369
370 /* simulate 512 MiB RAM beginning at 1.5GiB */
371 return test_noreserved(uts, 0xE0000000, 5, 8);
372}
373
374DM_TEST(lib_test_lmb_unaligned_size, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100375/*
376 * Simulate a RAM that starts at 0 and allocate down to address 0, which must
377 * fail as '0' means failure for the lmb_alloc functions.
378 */
379static int lib_test_lmb_at_0(struct unit_test_state *uts)
380{
381 const phys_addr_t ram = 0;
382 const phys_size_t ram_size = 0x20000000;
383 struct lmb lmb;
384 long ret;
385 phys_addr_t a, b;
386
387 lmb_init(&lmb);
388
389 ret = lmb_add(&lmb, ram, ram_size);
390 ut_asserteq(ret, 0);
391
392 /* allocate nearly everything */
393 a = lmb_alloc(&lmb, ram_size - 4, 1);
394 ut_asserteq(a, ram + 4);
395 ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
396 0, 0, 0, 0);
397 /* allocate the rest */
398 /* This should fail as the allocated address would be 0 */
399 b = lmb_alloc(&lmb, 4, 1);
400 ut_asserteq(b, 0);
401 /* check that this was an error by checking lmb */
402 ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
403 0, 0, 0, 0);
404 /* check that this was an error by freeing b */
405 ret = lmb_free(&lmb, b, 4);
406 ut_asserteq(ret, -1);
407 ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
408 0, 0, 0, 0);
409
410 ret = lmb_free(&lmb, a, ram_size - 4);
411 ut_asserteq(ret, 0);
412 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
413
414 return 0;
415}
416
417DM_TEST(lib_test_lmb_at_0, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100418
419/* Check that calling lmb_reserve with overlapping regions fails. */
420static int lib_test_lmb_overlapping_reserve(struct unit_test_state *uts)
421{
422 const phys_addr_t ram = 0x40000000;
423 const phys_size_t ram_size = 0x20000000;
424 struct lmb lmb;
425 long ret;
426
427 lmb_init(&lmb);
428
429 ret = lmb_add(&lmb, ram, ram_size);
430 ut_asserteq(ret, 0);
431
432 ret = lmb_reserve(&lmb, 0x40010000, 0x10000);
433 ut_asserteq(ret, 0);
434 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
435 0, 0, 0, 0);
436 /* allocate overlapping region should fail */
437 ret = lmb_reserve(&lmb, 0x40011000, 0x10000);
438 ut_asserteq(ret, -1);
439 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
440 0, 0, 0, 0);
441 /* allocate 3nd region */
442 ret = lmb_reserve(&lmb, 0x40030000, 0x10000);
443 ut_asserteq(ret, 0);
444 ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40010000, 0x10000,
445 0x40030000, 0x10000, 0, 0);
446 /* allocate 2nd region */
447 ret = lmb_reserve(&lmb, 0x40020000, 0x10000);
448 ut_assert(ret >= 0);
449 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x30000,
450 0, 0, 0, 0);
451
452 return 0;
453}
454
455DM_TEST(lib_test_lmb_overlapping_reserve,
456 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100457
458/*
459 * Simulate 512 MiB RAM, reserve 3 blocks, allocate addresses in between.
460 * Expect addresses outside the memory range to fail.
461 */
462static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
463{
464 const phys_size_t ram_size = 0x20000000;
465 const phys_addr_t ram_end = ram + ram_size;
466 const phys_size_t alloc_addr_a = ram + 0x8000000;
467 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
468 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
469 struct lmb lmb;
470 long ret;
471 phys_addr_t a, b, c, d, e;
472
473 /* check for overflow */
474 ut_assert(ram_end == 0 || ram_end > ram);
475
476 lmb_init(&lmb);
477
478 ret = lmb_add(&lmb, ram, ram_size);
479 ut_asserteq(ret, 0);
480
481 /* reserve 3 blocks */
482 ret = lmb_reserve(&lmb, alloc_addr_a, 0x10000);
483 ut_asserteq(ret, 0);
484 ret = lmb_reserve(&lmb, alloc_addr_b, 0x10000);
485 ut_asserteq(ret, 0);
486 ret = lmb_reserve(&lmb, alloc_addr_c, 0x10000);
487 ut_asserteq(ret, 0);
488 ASSERT_LMB(&lmb, ram, ram_size, 3, alloc_addr_a, 0x10000,
489 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
490
491 /* allocate blocks */
492 a = lmb_alloc_addr(&lmb, ram, alloc_addr_a - ram);
493 ut_asserteq(a, ram);
494 ASSERT_LMB(&lmb, ram, ram_size, 3, ram, 0x8010000,
495 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
496 b = lmb_alloc_addr(&lmb, alloc_addr_a + 0x10000,
497 alloc_addr_b - alloc_addr_a - 0x10000);
498 ut_asserteq(b, alloc_addr_a + 0x10000);
499 ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x10010000,
500 alloc_addr_c, 0x10000, 0, 0);
501 c = lmb_alloc_addr(&lmb, alloc_addr_b + 0x10000,
502 alloc_addr_c - alloc_addr_b - 0x10000);
503 ut_asserteq(c, alloc_addr_b + 0x10000);
504 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
505 0, 0, 0, 0);
506 d = lmb_alloc_addr(&lmb, alloc_addr_c + 0x10000,
507 ram_end - alloc_addr_c - 0x10000);
508 ut_asserteq(d, alloc_addr_c + 0x10000);
509 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, ram_size,
510 0, 0, 0, 0);
511
512 /* allocating anything else should fail */
513 e = lmb_alloc(&lmb, 1, 1);
514 ut_asserteq(e, 0);
515 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, ram_size,
516 0, 0, 0, 0);
517
518 ret = lmb_free(&lmb, d, ram_end - alloc_addr_c - 0x10000);
519 ut_asserteq(ret, 0);
520
521 /* allocate at 3 points in free range */
522
523 d = lmb_alloc_addr(&lmb, ram_end - 4, 4);
524 ut_asserteq(d, ram_end - 4);
525 ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x18010000,
526 d, 4, 0, 0);
527 ret = lmb_free(&lmb, d, 4);
528 ut_asserteq(ret, 0);
529 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
530 0, 0, 0, 0);
531
532 d = lmb_alloc_addr(&lmb, ram_end - 128, 4);
533 ut_asserteq(d, ram_end - 128);
534 ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x18010000,
535 d, 4, 0, 0);
536 ret = lmb_free(&lmb, d, 4);
537 ut_asserteq(ret, 0);
538 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
539 0, 0, 0, 0);
540
541 d = lmb_alloc_addr(&lmb, alloc_addr_c + 0x10000, 4);
542 ut_asserteq(d, alloc_addr_c + 0x10000);
543 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010004,
544 0, 0, 0, 0);
545 ret = lmb_free(&lmb, d, 4);
546 ut_asserteq(ret, 0);
547 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
548 0, 0, 0, 0);
549
550 /* allocate at the bottom */
551 ret = lmb_free(&lmb, a, alloc_addr_a - ram);
552 ut_asserteq(ret, 0);
553 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + 0x8000000, 0x10010000,
554 0, 0, 0, 0);
555 d = lmb_alloc_addr(&lmb, ram, 4);
556 ut_asserteq(d, ram);
557 ASSERT_LMB(&lmb, ram, ram_size, 2, d, 4,
558 ram + 0x8000000, 0x10010000, 0, 0);
559
560 /* check that allocating outside memory fails */
561 if (ram_end != 0) {
562 ret = lmb_alloc_addr(&lmb, ram_end, 1);
563 ut_asserteq(ret, 0);
564 }
565 if (ram != 0) {
566 ret = lmb_alloc_addr(&lmb, ram - 1, 1);
567 ut_asserteq(ret, 0);
568 }
569
570 return 0;
571}
572
573static int lib_test_lmb_alloc_addr(struct unit_test_state *uts)
574{
575 int ret;
576
577 /* simulate 512 MiB RAM beginning at 1GiB */
578 ret = test_alloc_addr(uts, 0x40000000);
579 if (ret)
580 return ret;
581
582 /* simulate 512 MiB RAM beginning at 1.5GiB */
583 return test_alloc_addr(uts, 0xE0000000);
584}
585
586DM_TEST(lib_test_lmb_alloc_addr, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
587
588/* Simulate 512 MiB RAM, reserve 3 blocks, check addresses in between */
589static int test_get_unreserved_size(struct unit_test_state *uts,
590 const phys_addr_t ram)
591{
592 const phys_size_t ram_size = 0x20000000;
593 const phys_addr_t ram_end = ram + ram_size;
594 const phys_size_t alloc_addr_a = ram + 0x8000000;
595 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
596 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
597 struct lmb lmb;
598 long ret;
599 phys_size_t s;
600
601 /* check for overflow */
602 ut_assert(ram_end == 0 || ram_end > ram);
603
604 lmb_init(&lmb);
605
606 ret = lmb_add(&lmb, ram, ram_size);
607 ut_asserteq(ret, 0);
608
609 /* reserve 3 blocks */
610 ret = lmb_reserve(&lmb, alloc_addr_a, 0x10000);
611 ut_asserteq(ret, 0);
612 ret = lmb_reserve(&lmb, alloc_addr_b, 0x10000);
613 ut_asserteq(ret, 0);
614 ret = lmb_reserve(&lmb, alloc_addr_c, 0x10000);
615 ut_asserteq(ret, 0);
616 ASSERT_LMB(&lmb, ram, ram_size, 3, alloc_addr_a, 0x10000,
617 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
618
619 /* check addresses in between blocks */
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100620 s = lmb_get_free_size(&lmb, ram);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100621 ut_asserteq(s, alloc_addr_a - ram);
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100622 s = lmb_get_free_size(&lmb, ram + 0x10000);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100623 ut_asserteq(s, alloc_addr_a - ram - 0x10000);
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100624 s = lmb_get_free_size(&lmb, alloc_addr_a - 4);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100625 ut_asserteq(s, 4);
626
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100627 s = lmb_get_free_size(&lmb, alloc_addr_a + 0x10000);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100628 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x10000);
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100629 s = lmb_get_free_size(&lmb, alloc_addr_a + 0x20000);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100630 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x20000);
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100631 s = lmb_get_free_size(&lmb, alloc_addr_b - 4);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100632 ut_asserteq(s, 4);
633
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100634 s = lmb_get_free_size(&lmb, alloc_addr_c + 0x10000);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100635 ut_asserteq(s, ram_end - alloc_addr_c - 0x10000);
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100636 s = lmb_get_free_size(&lmb, alloc_addr_c + 0x20000);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100637 ut_asserteq(s, ram_end - alloc_addr_c - 0x20000);
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100638 s = lmb_get_free_size(&lmb, ram_end - 4);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100639 ut_asserteq(s, 4);
640
641 return 0;
642}
643
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100644static int lib_test_lmb_get_free_size(struct unit_test_state *uts)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100645{
646 int ret;
647
648 /* simulate 512 MiB RAM beginning at 1GiB */
649 ret = test_get_unreserved_size(uts, 0x40000000);
650 if (ret)
651 return ret;
652
653 /* simulate 512 MiB RAM beginning at 1.5GiB */
654 return test_get_unreserved_size(uts, 0xE0000000);
655}
656
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100657DM_TEST(lib_test_lmb_get_free_size,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100658 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);