1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 Simon Goldschmidt
12 #include <test/test.h>
15 static inline bool lmb_is_nomap(struct lmb_property *m)
17 return m->flags & LMB_NOMAP;
20 static int check_lmb(struct unit_test_state *uts, struct lmb *lmb,
21 phys_addr_t ram_base, phys_size_t ram_size,
22 unsigned long num_reserved,
23 phys_addr_t base1, phys_size_t size1,
24 phys_addr_t base2, phys_size_t size2,
25 phys_addr_t base3, phys_size_t size3)
28 ut_asserteq(lmb->memory.cnt, 1);
29 ut_asserteq(lmb->memory.region[0].base, ram_base);
30 ut_asserteq(lmb->memory.region[0].size, ram_size);
33 ut_asserteq(lmb->reserved.cnt, num_reserved);
34 if (num_reserved > 0) {
35 ut_asserteq(lmb->reserved.region[0].base, base1);
36 ut_asserteq(lmb->reserved.region[0].size, size1);
38 if (num_reserved > 1) {
39 ut_asserteq(lmb->reserved.region[1].base, base2);
40 ut_asserteq(lmb->reserved.region[1].size, size2);
42 if (num_reserved > 2) {
43 ut_asserteq(lmb->reserved.region[2].base, base3);
44 ut_asserteq(lmb->reserved.region[2].size, size3);
49 #define ASSERT_LMB(lmb, ram_base, ram_size, num_reserved, base1, size1, \
50 base2, size2, base3, size3) \
51 ut_assert(!check_lmb(uts, lmb, ram_base, ram_size, \
52 num_reserved, base1, size1, base2, size2, base3, \
56 * Test helper function that reserves 64 KiB somewhere in the simulated RAM and
57 * then does some alloc + free tests.
59 static int test_multi_alloc(struct unit_test_state *uts, const phys_addr_t ram,
60 const phys_size_t ram_size, const phys_addr_t ram0,
61 const phys_size_t ram0_size,
62 const phys_addr_t alloc_64k_addr)
64 const phys_addr_t ram_end = ram + ram_size;
65 const phys_addr_t alloc_64k_end = alloc_64k_addr + 0x10000;
69 phys_addr_t a, a2, b, b2, c, d;
71 /* check for overflow */
72 ut_assert(ram_end == 0 || ram_end > ram);
73 ut_assert(alloc_64k_end > alloc_64k_addr);
74 /* check input addresses + size */
75 ut_assert(alloc_64k_addr >= ram + 8);
76 ut_assert(alloc_64k_end <= ram_end - 8);
81 ret = lmb_add(&lmb, ram0, ram0_size);
85 ret = lmb_add(&lmb, ram, ram_size);
89 ut_asserteq(lmb.memory.cnt, 2);
90 ut_asserteq(lmb.memory.region[0].base, ram0);
91 ut_asserteq(lmb.memory.region[0].size, ram0_size);
92 ut_asserteq(lmb.memory.region[1].base, ram);
93 ut_asserteq(lmb.memory.region[1].size, ram_size);
95 ut_asserteq(lmb.memory.cnt, 1);
96 ut_asserteq(lmb.memory.region[0].base, ram);
97 ut_asserteq(lmb.memory.region[0].size, ram_size);
100 /* reserve 64KiB somewhere */
101 ret = lmb_reserve(&lmb, alloc_64k_addr, 0x10000);
103 ASSERT_LMB(&lmb, 0, 0, 1, alloc_64k_addr, 0x10000,
106 /* allocate somewhere, should be at the end of RAM */
107 a = lmb_alloc(&lmb, 4, 1);
108 ut_asserteq(a, ram_end - 4);
109 ASSERT_LMB(&lmb, 0, 0, 2, alloc_64k_addr, 0x10000,
110 ram_end - 4, 4, 0, 0);
111 /* alloc below end of reserved region -> below reserved region */
112 b = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
113 ut_asserteq(b, alloc_64k_addr - 4);
114 ASSERT_LMB(&lmb, 0, 0, 2,
115 alloc_64k_addr - 4, 0x10000 + 4, ram_end - 4, 4, 0, 0);
118 c = lmb_alloc(&lmb, 4, 1);
119 ut_asserteq(c, ram_end - 8);
120 ASSERT_LMB(&lmb, 0, 0, 2,
121 alloc_64k_addr - 4, 0x10000 + 4, ram_end - 8, 8, 0, 0);
122 d = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
123 ut_asserteq(d, alloc_64k_addr - 8);
124 ASSERT_LMB(&lmb, 0, 0, 2,
125 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);
127 ret = lmb_free(&lmb, a, 4);
129 ASSERT_LMB(&lmb, 0, 0, 2,
130 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
131 /* allocate again to ensure we get the same address */
132 a2 = lmb_alloc(&lmb, 4, 1);
134 ASSERT_LMB(&lmb, 0, 0, 2,
135 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);
136 ret = lmb_free(&lmb, a2, 4);
138 ASSERT_LMB(&lmb, 0, 0, 2,
139 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
141 ret = lmb_free(&lmb, b, 4);
143 ASSERT_LMB(&lmb, 0, 0, 3,
144 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000,
146 /* allocate again to ensure we get the same address */
147 b2 = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
149 ASSERT_LMB(&lmb, 0, 0, 2,
150 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
151 ret = lmb_free(&lmb, b2, 4);
153 ASSERT_LMB(&lmb, 0, 0, 3,
154 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000,
157 ret = lmb_free(&lmb, c, 4);
159 ASSERT_LMB(&lmb, 0, 0, 2,
160 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, 0, 0);
161 ret = lmb_free(&lmb, d, 4);
163 ASSERT_LMB(&lmb, 0, 0, 1, alloc_64k_addr, 0x10000,
167 ut_asserteq(lmb.memory.cnt, 2);
168 ut_asserteq(lmb.memory.region[0].base, ram0);
169 ut_asserteq(lmb.memory.region[0].size, ram0_size);
170 ut_asserteq(lmb.memory.region[1].base, ram);
171 ut_asserteq(lmb.memory.region[1].size, ram_size);
173 ut_asserteq(lmb.memory.cnt, 1);
174 ut_asserteq(lmb.memory.region[0].base, ram);
175 ut_asserteq(lmb.memory.region[0].size, ram_size);
181 static int test_multi_alloc_512mb(struct unit_test_state *uts,
182 const phys_addr_t ram)
184 return test_multi_alloc(uts, ram, 0x20000000, 0, 0, ram + 0x10000000);
187 static int test_multi_alloc_512mb_x2(struct unit_test_state *uts,
188 const phys_addr_t ram,
189 const phys_addr_t ram0)
191 return test_multi_alloc(uts, ram, 0x20000000, ram0, 0x20000000,
195 /* Create a memory region with one reserved region and allocate */
196 static int lib_test_lmb_simple(struct unit_test_state *uts)
200 /* simulate 512 MiB RAM beginning at 1GiB */
201 ret = test_multi_alloc_512mb(uts, 0x40000000);
205 /* simulate 512 MiB RAM beginning at 1.5GiB */
206 return test_multi_alloc_512mb(uts, 0xE0000000);
209 DM_TEST(lib_test_lmb_simple, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
211 /* Create two memory regions with one reserved region and allocate */
212 static int lib_test_lmb_simple_x2(struct unit_test_state *uts)
216 /* simulate 512 MiB RAM beginning at 2GiB and 1 GiB */
217 ret = test_multi_alloc_512mb_x2(uts, 0x80000000, 0x40000000);
221 /* simulate 512 MiB RAM beginning at 3.5GiB and 1 GiB */
222 return test_multi_alloc_512mb_x2(uts, 0xE0000000, 0x40000000);
225 DM_TEST(lib_test_lmb_simple_x2, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
227 /* Simulate 512 MiB RAM, allocate some blocks that fit/don't fit */
228 static int test_bigblock(struct unit_test_state *uts, const phys_addr_t ram)
230 const phys_size_t ram_size = 0x20000000;
231 const phys_size_t big_block_size = 0x10000000;
232 const phys_addr_t ram_end = ram + ram_size;
233 const phys_addr_t alloc_64k_addr = ram + 0x10000000;
238 /* check for overflow */
239 ut_assert(ram_end == 0 || ram_end > ram);
243 ret = lmb_add(&lmb, ram, ram_size);
246 /* reserve 64KiB in the middle of RAM */
247 ret = lmb_reserve(&lmb, alloc_64k_addr, 0x10000);
249 ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
252 /* allocate a big block, should be below reserved */
253 a = lmb_alloc(&lmb, big_block_size, 1);
255 ASSERT_LMB(&lmb, ram, ram_size, 1, a,
256 big_block_size + 0x10000, 0, 0, 0, 0);
257 /* allocate 2nd big block */
258 /* This should fail, printing an error */
259 b = lmb_alloc(&lmb, big_block_size, 1);
261 ASSERT_LMB(&lmb, ram, ram_size, 1, a,
262 big_block_size + 0x10000, 0, 0, 0, 0);
264 ret = lmb_free(&lmb, a, big_block_size);
266 ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
269 /* allocate too big block */
270 /* This should fail, printing an error */
271 a = lmb_alloc(&lmb, ram_size, 1);
273 ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
279 static int lib_test_lmb_big(struct unit_test_state *uts)
283 /* simulate 512 MiB RAM beginning at 1GiB */
284 ret = test_bigblock(uts, 0x40000000);
288 /* simulate 512 MiB RAM beginning at 1.5GiB */
289 return test_bigblock(uts, 0xE0000000);
292 DM_TEST(lib_test_lmb_big, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
294 /* Simulate 512 MiB RAM, allocate a block without previous reservation */
295 static int test_noreserved(struct unit_test_state *uts, const phys_addr_t ram,
296 const phys_addr_t alloc_size, const ulong align)
298 const phys_size_t ram_size = 0x20000000;
299 const phys_addr_t ram_end = ram + ram_size;
303 const phys_addr_t alloc_size_aligned = (alloc_size + align - 1) &
306 /* check for overflow */
307 ut_assert(ram_end == 0 || ram_end > ram);
311 ret = lmb_add(&lmb, ram, ram_size);
313 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
315 /* allocate a block */
316 a = lmb_alloc(&lmb, alloc_size, align);
318 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
319 alloc_size, 0, 0, 0, 0);
320 /* allocate another block */
321 b = lmb_alloc(&lmb, alloc_size, align);
323 if (alloc_size == alloc_size_aligned) {
324 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size -
325 (alloc_size_aligned * 2), alloc_size * 2, 0, 0, 0,
328 ASSERT_LMB(&lmb, ram, ram_size, 2, ram + ram_size -
329 (alloc_size_aligned * 2), alloc_size, ram + ram_size
330 - alloc_size_aligned, alloc_size, 0, 0);
333 ret = lmb_free(&lmb, b, alloc_size);
335 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
336 alloc_size, 0, 0, 0, 0);
337 ret = lmb_free(&lmb, a, alloc_size);
339 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
341 /* allocate a block with base*/
342 b = lmb_alloc_base(&lmb, alloc_size, align, ram_end);
344 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
345 alloc_size, 0, 0, 0, 0);
347 ret = lmb_free(&lmb, b, alloc_size);
349 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
354 static int lib_test_lmb_noreserved(struct unit_test_state *uts)
358 /* simulate 512 MiB RAM beginning at 1GiB */
359 ret = test_noreserved(uts, 0x40000000, 4, 1);
363 /* simulate 512 MiB RAM beginning at 1.5GiB */
364 return test_noreserved(uts, 0xE0000000, 4, 1);
367 DM_TEST(lib_test_lmb_noreserved, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
369 static int lib_test_lmb_unaligned_size(struct unit_test_state *uts)
373 /* simulate 512 MiB RAM beginning at 1GiB */
374 ret = test_noreserved(uts, 0x40000000, 5, 8);
378 /* simulate 512 MiB RAM beginning at 1.5GiB */
379 return test_noreserved(uts, 0xE0000000, 5, 8);
382 DM_TEST(lib_test_lmb_unaligned_size, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
384 * Simulate a RAM that starts at 0 and allocate down to address 0, which must
385 * fail as '0' means failure for the lmb_alloc functions.
387 static int lib_test_lmb_at_0(struct unit_test_state *uts)
389 const phys_addr_t ram = 0;
390 const phys_size_t ram_size = 0x20000000;
397 ret = lmb_add(&lmb, ram, ram_size);
400 /* allocate nearly everything */
401 a = lmb_alloc(&lmb, ram_size - 4, 1);
402 ut_asserteq(a, ram + 4);
403 ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
405 /* allocate the rest */
406 /* This should fail as the allocated address would be 0 */
407 b = lmb_alloc(&lmb, 4, 1);
409 /* check that this was an error by checking lmb */
410 ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
412 /* check that this was an error by freeing b */
413 ret = lmb_free(&lmb, b, 4);
414 ut_asserteq(ret, -1);
415 ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
418 ret = lmb_free(&lmb, a, ram_size - 4);
420 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
425 DM_TEST(lib_test_lmb_at_0, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
427 /* Check that calling lmb_reserve with overlapping regions fails. */
428 static int lib_test_lmb_overlapping_reserve(struct unit_test_state *uts)
430 const phys_addr_t ram = 0x40000000;
431 const phys_size_t ram_size = 0x20000000;
437 ret = lmb_add(&lmb, ram, ram_size);
440 ret = lmb_reserve(&lmb, 0x40010000, 0x10000);
442 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
444 /* allocate overlapping region should fail */
445 ret = lmb_reserve(&lmb, 0x40011000, 0x10000);
446 ut_asserteq(ret, -1);
447 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
449 /* allocate 3nd region */
450 ret = lmb_reserve(&lmb, 0x40030000, 0x10000);
452 ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40010000, 0x10000,
453 0x40030000, 0x10000, 0, 0);
454 /* allocate 2nd region */
455 ret = lmb_reserve(&lmb, 0x40020000, 0x10000);
457 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x30000,
463 DM_TEST(lib_test_lmb_overlapping_reserve,
464 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
467 * Simulate 512 MiB RAM, reserve 3 blocks, allocate addresses in between.
468 * Expect addresses outside the memory range to fail.
470 static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
472 const phys_size_t ram_size = 0x20000000;
473 const phys_addr_t ram_end = ram + ram_size;
474 const phys_size_t alloc_addr_a = ram + 0x8000000;
475 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
476 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
479 phys_addr_t a, b, c, d, e;
481 /* check for overflow */
482 ut_assert(ram_end == 0 || ram_end > ram);
486 ret = lmb_add(&lmb, ram, ram_size);
489 /* reserve 3 blocks */
490 ret = lmb_reserve(&lmb, alloc_addr_a, 0x10000);
492 ret = lmb_reserve(&lmb, alloc_addr_b, 0x10000);
494 ret = lmb_reserve(&lmb, alloc_addr_c, 0x10000);
496 ASSERT_LMB(&lmb, ram, ram_size, 3, alloc_addr_a, 0x10000,
497 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
499 /* allocate blocks */
500 a = lmb_alloc_addr(&lmb, ram, alloc_addr_a - ram);
502 ASSERT_LMB(&lmb, ram, ram_size, 3, ram, 0x8010000,
503 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
504 b = lmb_alloc_addr(&lmb, alloc_addr_a + 0x10000,
505 alloc_addr_b - alloc_addr_a - 0x10000);
506 ut_asserteq(b, alloc_addr_a + 0x10000);
507 ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x10010000,
508 alloc_addr_c, 0x10000, 0, 0);
509 c = lmb_alloc_addr(&lmb, alloc_addr_b + 0x10000,
510 alloc_addr_c - alloc_addr_b - 0x10000);
511 ut_asserteq(c, alloc_addr_b + 0x10000);
512 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
514 d = lmb_alloc_addr(&lmb, alloc_addr_c + 0x10000,
515 ram_end - alloc_addr_c - 0x10000);
516 ut_asserteq(d, alloc_addr_c + 0x10000);
517 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, ram_size,
520 /* allocating anything else should fail */
521 e = lmb_alloc(&lmb, 1, 1);
523 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, ram_size,
526 ret = lmb_free(&lmb, d, ram_end - alloc_addr_c - 0x10000);
529 /* allocate at 3 points in free range */
531 d = lmb_alloc_addr(&lmb, ram_end - 4, 4);
532 ut_asserteq(d, ram_end - 4);
533 ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x18010000,
535 ret = lmb_free(&lmb, d, 4);
537 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
540 d = lmb_alloc_addr(&lmb, ram_end - 128, 4);
541 ut_asserteq(d, ram_end - 128);
542 ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x18010000,
544 ret = lmb_free(&lmb, d, 4);
546 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
549 d = lmb_alloc_addr(&lmb, alloc_addr_c + 0x10000, 4);
550 ut_asserteq(d, alloc_addr_c + 0x10000);
551 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010004,
553 ret = lmb_free(&lmb, d, 4);
555 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
558 /* allocate at the bottom */
559 ret = lmb_free(&lmb, a, alloc_addr_a - ram);
561 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + 0x8000000, 0x10010000,
563 d = lmb_alloc_addr(&lmb, ram, 4);
565 ASSERT_LMB(&lmb, ram, ram_size, 2, d, 4,
566 ram + 0x8000000, 0x10010000, 0, 0);
568 /* check that allocating outside memory fails */
570 ret = lmb_alloc_addr(&lmb, ram_end, 1);
574 ret = lmb_alloc_addr(&lmb, ram - 1, 1);
581 static int lib_test_lmb_alloc_addr(struct unit_test_state *uts)
585 /* simulate 512 MiB RAM beginning at 1GiB */
586 ret = test_alloc_addr(uts, 0x40000000);
590 /* simulate 512 MiB RAM beginning at 1.5GiB */
591 return test_alloc_addr(uts, 0xE0000000);
594 DM_TEST(lib_test_lmb_alloc_addr, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
596 /* Simulate 512 MiB RAM, reserve 3 blocks, check addresses in between */
597 static int test_get_unreserved_size(struct unit_test_state *uts,
598 const phys_addr_t ram)
600 const phys_size_t ram_size = 0x20000000;
601 const phys_addr_t ram_end = ram + ram_size;
602 const phys_size_t alloc_addr_a = ram + 0x8000000;
603 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
604 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
609 /* check for overflow */
610 ut_assert(ram_end == 0 || ram_end > ram);
614 ret = lmb_add(&lmb, ram, ram_size);
617 /* reserve 3 blocks */
618 ret = lmb_reserve(&lmb, alloc_addr_a, 0x10000);
620 ret = lmb_reserve(&lmb, alloc_addr_b, 0x10000);
622 ret = lmb_reserve(&lmb, alloc_addr_c, 0x10000);
624 ASSERT_LMB(&lmb, ram, ram_size, 3, alloc_addr_a, 0x10000,
625 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
627 /* check addresses in between blocks */
628 s = lmb_get_free_size(&lmb, ram);
629 ut_asserteq(s, alloc_addr_a - ram);
630 s = lmb_get_free_size(&lmb, ram + 0x10000);
631 ut_asserteq(s, alloc_addr_a - ram - 0x10000);
632 s = lmb_get_free_size(&lmb, alloc_addr_a - 4);
635 s = lmb_get_free_size(&lmb, alloc_addr_a + 0x10000);
636 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x10000);
637 s = lmb_get_free_size(&lmb, alloc_addr_a + 0x20000);
638 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x20000);
639 s = lmb_get_free_size(&lmb, alloc_addr_b - 4);
642 s = lmb_get_free_size(&lmb, alloc_addr_c + 0x10000);
643 ut_asserteq(s, ram_end - alloc_addr_c - 0x10000);
644 s = lmb_get_free_size(&lmb, alloc_addr_c + 0x20000);
645 ut_asserteq(s, ram_end - alloc_addr_c - 0x20000);
646 s = lmb_get_free_size(&lmb, ram_end - 4);
652 static int lib_test_lmb_get_free_size(struct unit_test_state *uts)
656 /* simulate 512 MiB RAM beginning at 1GiB */
657 ret = test_get_unreserved_size(uts, 0x40000000);
661 /* simulate 512 MiB RAM beginning at 1.5GiB */
662 return test_get_unreserved_size(uts, 0xE0000000);
665 DM_TEST(lib_test_lmb_get_free_size,
666 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
668 static int lib_test_lmb_max_regions(struct unit_test_state *uts)
670 const phys_addr_t ram = 0x00000000;
671 const phys_size_t ram_size = 0x8000000;
672 const phys_size_t blk_size = 0x10000;
679 ut_asserteq(lmb.memory.cnt, 0);
680 ut_asserteq(lmb.memory.max, 8);
681 ut_asserteq(lmb.reserved.cnt, 0);
682 ut_asserteq(lmb.reserved.max, 8);
684 /* Add 8 memory regions */
685 for (i = 0; i < 8; i++) {
686 offset = ram + 2 * i * ram_size;
687 ret = lmb_add(&lmb, offset, ram_size);
690 ut_asserteq(lmb.memory.cnt, 8);
691 ut_asserteq(lmb.reserved.cnt, 0);
693 /* error for the 9th memory regions */
694 offset = ram + 2 * 8 * ram_size;
695 ret = lmb_add(&lmb, offset, ram_size);
696 ut_asserteq(ret, -1);
698 ut_asserteq(lmb.memory.cnt, 8);
699 ut_asserteq(lmb.reserved.cnt, 0);
701 /* reserve 8 regions */
702 for (i = 0; i < 8; i++) {
703 offset = ram + 2 * i * blk_size;
704 ret = lmb_reserve(&lmb, offset, blk_size);
708 ut_asserteq(lmb.memory.cnt, 8);
709 ut_asserteq(lmb.reserved.cnt, 8);
711 /* error for the 9th reserved blocks */
712 offset = ram + 2 * 8 * blk_size;
713 ret = lmb_reserve(&lmb, offset, blk_size);
714 ut_asserteq(ret, -1);
716 ut_asserteq(lmb.memory.cnt, 8);
717 ut_asserteq(lmb.reserved.cnt, 8);
719 /* check each regions */
720 for (i = 0; i < 8; i++)
721 ut_asserteq(lmb.memory.region[i].base, ram + 2 * i * ram_size);
723 for (i = 0; i < 8; i++)
724 ut_asserteq(lmb.reserved.region[i].base, ram + 2 * i * blk_size);
729 DM_TEST(lib_test_lmb_max_regions,
730 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
732 static int lib_test_lmb_flags(struct unit_test_state *uts)
734 const phys_addr_t ram = 0x40000000;
735 const phys_size_t ram_size = 0x20000000;
741 ret = lmb_add(&lmb, ram, ram_size);
744 /* reserve, same flag */
745 ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NOMAP);
747 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
750 /* reserve again, same flag */
751 ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NOMAP);
753 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
756 /* reserve again, new flag */
757 ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NONE);
758 ut_asserteq(ret, -1);
759 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
762 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
765 ret = lmb_reserve_flags(&lmb, 0x40020000, 0x10000, LMB_NOMAP);
767 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x20000,
771 ret = lmb_reserve_flags(&lmb, 0x40000000, 0x10000, LMB_NOMAP);
773 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40000000, 0x30000,
776 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
778 ret = lmb_reserve_flags(&lmb, 0x40030000, 0x10000, LMB_NONE);
780 ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40000000, 0x30000,
781 0x40030000, 0x10000, 0, 0);
783 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
784 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[1]), 0);
786 /* test that old API use LMB_NONE */
787 ret = lmb_reserve(&lmb, 0x40040000, 0x10000);
789 ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40000000, 0x30000,
790 0x40030000, 0x20000, 0, 0);
792 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
793 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[1]), 0);
795 ret = lmb_reserve_flags(&lmb, 0x40070000, 0x10000, LMB_NOMAP);
797 ASSERT_LMB(&lmb, ram, ram_size, 3, 0x40000000, 0x30000,
798 0x40030000, 0x20000, 0x40070000, 0x10000);
800 ret = lmb_reserve_flags(&lmb, 0x40050000, 0x10000, LMB_NOMAP);
802 ASSERT_LMB(&lmb, ram, ram_size, 4, 0x40000000, 0x30000,
803 0x40030000, 0x20000, 0x40050000, 0x10000);
805 /* merge with 2 adjacent regions */
806 ret = lmb_reserve_flags(&lmb, 0x40060000, 0x10000, LMB_NOMAP);
808 ASSERT_LMB(&lmb, ram, ram_size, 3, 0x40000000, 0x30000,
809 0x40030000, 0x20000, 0x40050000, 0x30000);
811 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
812 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[1]), 0);
813 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[2]), 1);
818 DM_TEST(lib_test_lmb_flags,
819 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);