1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 Simon Goldschmidt
13 #include <test/test.h>
16 static inline bool lmb_is_nomap(struct lmb_property *m)
18 return m->flags & LMB_NOMAP;
21 static int check_lmb(struct unit_test_state *uts, struct lmb *lmb,
22 phys_addr_t ram_base, phys_size_t ram_size,
23 unsigned long num_reserved,
24 phys_addr_t base1, phys_size_t size1,
25 phys_addr_t base2, phys_size_t size2,
26 phys_addr_t base3, phys_size_t size3)
29 ut_asserteq(lmb->memory.cnt, 1);
30 ut_asserteq(lmb->memory.region[0].base, ram_base);
31 ut_asserteq(lmb->memory.region[0].size, ram_size);
34 ut_asserteq(lmb->reserved.cnt, num_reserved);
35 if (num_reserved > 0) {
36 ut_asserteq(lmb->reserved.region[0].base, base1);
37 ut_asserteq(lmb->reserved.region[0].size, size1);
39 if (num_reserved > 1) {
40 ut_asserteq(lmb->reserved.region[1].base, base2);
41 ut_asserteq(lmb->reserved.region[1].size, size2);
43 if (num_reserved > 2) {
44 ut_asserteq(lmb->reserved.region[2].base, base3);
45 ut_asserteq(lmb->reserved.region[2].size, size3);
50 #define ASSERT_LMB(lmb, ram_base, ram_size, num_reserved, base1, size1, \
51 base2, size2, base3, size3) \
52 ut_assert(!check_lmb(uts, lmb, ram_base, ram_size, \
53 num_reserved, base1, size1, base2, size2, base3, \
57 * Test helper function that reserves 64 KiB somewhere in the simulated RAM and
58 * then does some alloc + free tests.
60 static int test_multi_alloc(struct unit_test_state *uts, const phys_addr_t ram,
61 const phys_size_t ram_size, const phys_addr_t ram0,
62 const phys_size_t ram0_size,
63 const phys_addr_t alloc_64k_addr)
65 const phys_addr_t ram_end = ram + ram_size;
66 const phys_addr_t alloc_64k_end = alloc_64k_addr + 0x10000;
70 phys_addr_t a, a2, b, b2, c, d;
72 /* check for overflow */
73 ut_assert(ram_end == 0 || ram_end > ram);
74 ut_assert(alloc_64k_end > alloc_64k_addr);
75 /* check input addresses + size */
76 ut_assert(alloc_64k_addr >= ram + 8);
77 ut_assert(alloc_64k_end <= ram_end - 8);
82 ret = lmb_add(&lmb, ram0, ram0_size);
86 ret = lmb_add(&lmb, ram, ram_size);
90 ut_asserteq(lmb.memory.cnt, 2);
91 ut_asserteq(lmb.memory.region[0].base, ram0);
92 ut_asserteq(lmb.memory.region[0].size, ram0_size);
93 ut_asserteq(lmb.memory.region[1].base, ram);
94 ut_asserteq(lmb.memory.region[1].size, ram_size);
96 ut_asserteq(lmb.memory.cnt, 1);
97 ut_asserteq(lmb.memory.region[0].base, ram);
98 ut_asserteq(lmb.memory.region[0].size, ram_size);
101 /* reserve 64KiB somewhere */
102 ret = lmb_reserve(&lmb, alloc_64k_addr, 0x10000);
104 ASSERT_LMB(&lmb, 0, 0, 1, alloc_64k_addr, 0x10000,
107 /* allocate somewhere, should be at the end of RAM */
108 a = lmb_alloc(&lmb, 4, 1);
109 ut_asserteq(a, ram_end - 4);
110 ASSERT_LMB(&lmb, 0, 0, 2, alloc_64k_addr, 0x10000,
111 ram_end - 4, 4, 0, 0);
112 /* alloc below end of reserved region -> below reserved region */
113 b = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
114 ut_asserteq(b, alloc_64k_addr - 4);
115 ASSERT_LMB(&lmb, 0, 0, 2,
116 alloc_64k_addr - 4, 0x10000 + 4, ram_end - 4, 4, 0, 0);
119 c = lmb_alloc(&lmb, 4, 1);
120 ut_asserteq(c, ram_end - 8);
121 ASSERT_LMB(&lmb, 0, 0, 2,
122 alloc_64k_addr - 4, 0x10000 + 4, ram_end - 8, 8, 0, 0);
123 d = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
124 ut_asserteq(d, alloc_64k_addr - 8);
125 ASSERT_LMB(&lmb, 0, 0, 2,
126 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);
128 ret = lmb_free(&lmb, a, 4);
130 ASSERT_LMB(&lmb, 0, 0, 2,
131 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
132 /* allocate again to ensure we get the same address */
133 a2 = lmb_alloc(&lmb, 4, 1);
135 ASSERT_LMB(&lmb, 0, 0, 2,
136 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);
137 ret = lmb_free(&lmb, a2, 4);
139 ASSERT_LMB(&lmb, 0, 0, 2,
140 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
142 ret = lmb_free(&lmb, b, 4);
144 ASSERT_LMB(&lmb, 0, 0, 3,
145 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000,
147 /* allocate again to ensure we get the same address */
148 b2 = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
150 ASSERT_LMB(&lmb, 0, 0, 2,
151 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
152 ret = lmb_free(&lmb, b2, 4);
154 ASSERT_LMB(&lmb, 0, 0, 3,
155 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000,
158 ret = lmb_free(&lmb, c, 4);
160 ASSERT_LMB(&lmb, 0, 0, 2,
161 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, 0, 0);
162 ret = lmb_free(&lmb, d, 4);
164 ASSERT_LMB(&lmb, 0, 0, 1, alloc_64k_addr, 0x10000,
168 ut_asserteq(lmb.memory.cnt, 2);
169 ut_asserteq(lmb.memory.region[0].base, ram0);
170 ut_asserteq(lmb.memory.region[0].size, ram0_size);
171 ut_asserteq(lmb.memory.region[1].base, ram);
172 ut_asserteq(lmb.memory.region[1].size, ram_size);
174 ut_asserteq(lmb.memory.cnt, 1);
175 ut_asserteq(lmb.memory.region[0].base, ram);
176 ut_asserteq(lmb.memory.region[0].size, ram_size);
182 static int test_multi_alloc_512mb(struct unit_test_state *uts,
183 const phys_addr_t ram)
185 return test_multi_alloc(uts, ram, 0x20000000, 0, 0, ram + 0x10000000);
188 static int test_multi_alloc_512mb_x2(struct unit_test_state *uts,
189 const phys_addr_t ram,
190 const phys_addr_t ram0)
192 return test_multi_alloc(uts, ram, 0x20000000, ram0, 0x20000000,
196 /* Create a memory region with one reserved region and allocate */
197 static int lib_test_lmb_simple(struct unit_test_state *uts)
201 /* simulate 512 MiB RAM beginning at 1GiB */
202 ret = test_multi_alloc_512mb(uts, 0x40000000);
206 /* simulate 512 MiB RAM beginning at 1.5GiB */
207 return test_multi_alloc_512mb(uts, 0xE0000000);
209 LIB_TEST(lib_test_lmb_simple, 0);
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);
224 LIB_TEST(lib_test_lmb_simple_x2, 0);
226 /* Simulate 512 MiB RAM, allocate some blocks that fit/don't fit */
227 static int test_bigblock(struct unit_test_state *uts, const phys_addr_t ram)
229 const phys_size_t ram_size = 0x20000000;
230 const phys_size_t big_block_size = 0x10000000;
231 const phys_addr_t ram_end = ram + ram_size;
232 const phys_addr_t alloc_64k_addr = ram + 0x10000000;
237 /* check for overflow */
238 ut_assert(ram_end == 0 || ram_end > ram);
242 ret = lmb_add(&lmb, ram, ram_size);
245 /* reserve 64KiB in the middle of RAM */
246 ret = lmb_reserve(&lmb, alloc_64k_addr, 0x10000);
248 ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
251 /* allocate a big block, should be below reserved */
252 a = lmb_alloc(&lmb, big_block_size, 1);
254 ASSERT_LMB(&lmb, ram, ram_size, 1, a,
255 big_block_size + 0x10000, 0, 0, 0, 0);
256 /* allocate 2nd big block */
257 /* This should fail, printing an error */
258 b = lmb_alloc(&lmb, big_block_size, 1);
260 ASSERT_LMB(&lmb, ram, ram_size, 1, a,
261 big_block_size + 0x10000, 0, 0, 0, 0);
263 ret = lmb_free(&lmb, a, big_block_size);
265 ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
268 /* allocate too big block */
269 /* This should fail, printing an error */
270 a = lmb_alloc(&lmb, ram_size, 1);
272 ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
278 static int lib_test_lmb_big(struct unit_test_state *uts)
282 /* simulate 512 MiB RAM beginning at 1GiB */
283 ret = test_bigblock(uts, 0x40000000);
287 /* simulate 512 MiB RAM beginning at 1.5GiB */
288 return test_bigblock(uts, 0xE0000000);
290 LIB_TEST(lib_test_lmb_big, 0);
292 /* Simulate 512 MiB RAM, allocate a block without previous reservation */
293 static int test_noreserved(struct unit_test_state *uts, const phys_addr_t ram,
294 const phys_addr_t alloc_size, const ulong align)
296 const phys_size_t ram_size = 0x20000000;
297 const phys_addr_t ram_end = ram + ram_size;
301 const phys_addr_t alloc_size_aligned = (alloc_size + align - 1) &
304 /* check for overflow */
305 ut_assert(ram_end == 0 || ram_end > ram);
309 ret = lmb_add(&lmb, ram, ram_size);
311 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
313 /* allocate a block */
314 a = lmb_alloc(&lmb, alloc_size, align);
316 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
317 alloc_size, 0, 0, 0, 0);
318 /* allocate another block */
319 b = lmb_alloc(&lmb, alloc_size, align);
321 if (alloc_size == alloc_size_aligned) {
322 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size -
323 (alloc_size_aligned * 2), alloc_size * 2, 0, 0, 0,
326 ASSERT_LMB(&lmb, ram, ram_size, 2, ram + ram_size -
327 (alloc_size_aligned * 2), alloc_size, ram + ram_size
328 - alloc_size_aligned, alloc_size, 0, 0);
331 ret = lmb_free(&lmb, b, alloc_size);
333 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
334 alloc_size, 0, 0, 0, 0);
335 ret = lmb_free(&lmb, a, alloc_size);
337 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
339 /* allocate a block with base*/
340 b = lmb_alloc_base(&lmb, alloc_size, align, ram_end);
342 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
343 alloc_size, 0, 0, 0, 0);
345 ret = lmb_free(&lmb, b, alloc_size);
347 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
352 static int lib_test_lmb_noreserved(struct unit_test_state *uts)
356 /* simulate 512 MiB RAM beginning at 1GiB */
357 ret = test_noreserved(uts, 0x40000000, 4, 1);
361 /* simulate 512 MiB RAM beginning at 1.5GiB */
362 return test_noreserved(uts, 0xE0000000, 4, 1);
365 LIB_TEST(lib_test_lmb_noreserved, 0);
367 static int lib_test_lmb_unaligned_size(struct unit_test_state *uts)
371 /* simulate 512 MiB RAM beginning at 1GiB */
372 ret = test_noreserved(uts, 0x40000000, 5, 8);
376 /* simulate 512 MiB RAM beginning at 1.5GiB */
377 return test_noreserved(uts, 0xE0000000, 5, 8);
379 LIB_TEST(lib_test_lmb_unaligned_size, 0);
382 * Simulate a RAM that starts at 0 and allocate down to address 0, which must
383 * fail as '0' means failure for the lmb_alloc functions.
385 static int lib_test_lmb_at_0(struct unit_test_state *uts)
387 const phys_addr_t ram = 0;
388 const phys_size_t ram_size = 0x20000000;
395 ret = lmb_add(&lmb, ram, ram_size);
398 /* allocate nearly everything */
399 a = lmb_alloc(&lmb, ram_size - 4, 1);
400 ut_asserteq(a, ram + 4);
401 ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
403 /* allocate the rest */
404 /* This should fail as the allocated address would be 0 */
405 b = lmb_alloc(&lmb, 4, 1);
407 /* check that this was an error by checking lmb */
408 ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
410 /* check that this was an error by freeing b */
411 ret = lmb_free(&lmb, b, 4);
412 ut_asserteq(ret, -1);
413 ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
416 ret = lmb_free(&lmb, a, ram_size - 4);
418 ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
422 LIB_TEST(lib_test_lmb_at_0, 0);
424 /* Check that calling lmb_reserve with overlapping regions fails. */
425 static int lib_test_lmb_overlapping_reserve(struct unit_test_state *uts)
427 const phys_addr_t ram = 0x40000000;
428 const phys_size_t ram_size = 0x20000000;
434 ret = lmb_add(&lmb, ram, ram_size);
437 ret = lmb_reserve(&lmb, 0x40010000, 0x10000);
439 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
441 /* allocate overlapping region should fail */
442 ret = lmb_reserve(&lmb, 0x40011000, 0x10000);
443 ut_asserteq(ret, -1);
444 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
446 /* allocate 3nd region */
447 ret = lmb_reserve(&lmb, 0x40030000, 0x10000);
449 ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40010000, 0x10000,
450 0x40030000, 0x10000, 0, 0);
451 /* allocate 2nd region , This should coalesced all region into one */
452 ret = lmb_reserve(&lmb, 0x40020000, 0x10000);
454 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x30000,
457 /* allocate 2nd region, which should be added as first region */
458 ret = lmb_reserve(&lmb, 0x40000000, 0x8000);
460 ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40000000, 0x8000,
461 0x40010000, 0x30000, 0, 0);
463 /* allocate 3rd region, coalesce with first and overlap with second */
464 ret = lmb_reserve(&lmb, 0x40008000, 0x10000);
466 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40000000, 0x40000,
470 LIB_TEST(lib_test_lmb_overlapping_reserve, 0);
473 * Simulate 512 MiB RAM, reserve 3 blocks, allocate addresses in between.
474 * Expect addresses outside the memory range to fail.
476 static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
478 const phys_size_t ram_size = 0x20000000;
479 const phys_addr_t ram_end = ram + ram_size;
480 const phys_size_t alloc_addr_a = ram + 0x8000000;
481 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
482 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
485 phys_addr_t a, b, c, d, e;
487 /* check for overflow */
488 ut_assert(ram_end == 0 || ram_end > ram);
492 ret = lmb_add(&lmb, ram, ram_size);
495 /* reserve 3 blocks */
496 ret = lmb_reserve(&lmb, alloc_addr_a, 0x10000);
498 ret = lmb_reserve(&lmb, alloc_addr_b, 0x10000);
500 ret = lmb_reserve(&lmb, alloc_addr_c, 0x10000);
502 ASSERT_LMB(&lmb, ram, ram_size, 3, alloc_addr_a, 0x10000,
503 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
505 /* allocate blocks */
506 a = lmb_alloc_addr(&lmb, ram, alloc_addr_a - ram);
508 ASSERT_LMB(&lmb, ram, ram_size, 3, ram, 0x8010000,
509 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
510 b = lmb_alloc_addr(&lmb, alloc_addr_a + 0x10000,
511 alloc_addr_b - alloc_addr_a - 0x10000);
512 ut_asserteq(b, alloc_addr_a + 0x10000);
513 ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x10010000,
514 alloc_addr_c, 0x10000, 0, 0);
515 c = lmb_alloc_addr(&lmb, alloc_addr_b + 0x10000,
516 alloc_addr_c - alloc_addr_b - 0x10000);
517 ut_asserteq(c, alloc_addr_b + 0x10000);
518 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
520 d = lmb_alloc_addr(&lmb, alloc_addr_c + 0x10000,
521 ram_end - alloc_addr_c - 0x10000);
522 ut_asserteq(d, alloc_addr_c + 0x10000);
523 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, ram_size,
526 /* allocating anything else should fail */
527 e = lmb_alloc(&lmb, 1, 1);
529 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, ram_size,
532 ret = lmb_free(&lmb, d, ram_end - alloc_addr_c - 0x10000);
535 /* allocate at 3 points in free range */
537 d = lmb_alloc_addr(&lmb, ram_end - 4, 4);
538 ut_asserteq(d, ram_end - 4);
539 ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x18010000,
541 ret = lmb_free(&lmb, d, 4);
543 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
546 d = lmb_alloc_addr(&lmb, ram_end - 128, 4);
547 ut_asserteq(d, ram_end - 128);
548 ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x18010000,
550 ret = lmb_free(&lmb, d, 4);
552 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
555 d = lmb_alloc_addr(&lmb, alloc_addr_c + 0x10000, 4);
556 ut_asserteq(d, alloc_addr_c + 0x10000);
557 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010004,
559 ret = lmb_free(&lmb, d, 4);
561 ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
564 /* allocate at the bottom */
565 ret = lmb_free(&lmb, a, alloc_addr_a - ram);
567 ASSERT_LMB(&lmb, ram, ram_size, 1, ram + 0x8000000, 0x10010000,
569 d = lmb_alloc_addr(&lmb, ram, 4);
571 ASSERT_LMB(&lmb, ram, ram_size, 2, d, 4,
572 ram + 0x8000000, 0x10010000, 0, 0);
574 /* check that allocating outside memory fails */
576 ret = lmb_alloc_addr(&lmb, ram_end, 1);
580 ret = lmb_alloc_addr(&lmb, ram - 1, 1);
587 static int lib_test_lmb_alloc_addr(struct unit_test_state *uts)
591 /* simulate 512 MiB RAM beginning at 1GiB */
592 ret = test_alloc_addr(uts, 0x40000000);
596 /* simulate 512 MiB RAM beginning at 1.5GiB */
597 return test_alloc_addr(uts, 0xE0000000);
599 LIB_TEST(lib_test_lmb_alloc_addr, 0);
601 /* Simulate 512 MiB RAM, reserve 3 blocks, check addresses in between */
602 static int test_get_unreserved_size(struct unit_test_state *uts,
603 const phys_addr_t ram)
605 const phys_size_t ram_size = 0x20000000;
606 const phys_addr_t ram_end = ram + ram_size;
607 const phys_size_t alloc_addr_a = ram + 0x8000000;
608 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
609 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
614 /* check for overflow */
615 ut_assert(ram_end == 0 || ram_end > ram);
619 ret = lmb_add(&lmb, ram, ram_size);
622 /* reserve 3 blocks */
623 ret = lmb_reserve(&lmb, alloc_addr_a, 0x10000);
625 ret = lmb_reserve(&lmb, alloc_addr_b, 0x10000);
627 ret = lmb_reserve(&lmb, alloc_addr_c, 0x10000);
629 ASSERT_LMB(&lmb, ram, ram_size, 3, alloc_addr_a, 0x10000,
630 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
632 /* check addresses in between blocks */
633 s = lmb_get_free_size(&lmb, ram);
634 ut_asserteq(s, alloc_addr_a - ram);
635 s = lmb_get_free_size(&lmb, ram + 0x10000);
636 ut_asserteq(s, alloc_addr_a - ram - 0x10000);
637 s = lmb_get_free_size(&lmb, alloc_addr_a - 4);
640 s = lmb_get_free_size(&lmb, alloc_addr_a + 0x10000);
641 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x10000);
642 s = lmb_get_free_size(&lmb, alloc_addr_a + 0x20000);
643 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x20000);
644 s = lmb_get_free_size(&lmb, alloc_addr_b - 4);
647 s = lmb_get_free_size(&lmb, alloc_addr_c + 0x10000);
648 ut_asserteq(s, ram_end - alloc_addr_c - 0x10000);
649 s = lmb_get_free_size(&lmb, alloc_addr_c + 0x20000);
650 ut_asserteq(s, ram_end - alloc_addr_c - 0x20000);
651 s = lmb_get_free_size(&lmb, ram_end - 4);
657 static int lib_test_lmb_get_free_size(struct unit_test_state *uts)
661 /* simulate 512 MiB RAM beginning at 1GiB */
662 ret = test_get_unreserved_size(uts, 0x40000000);
666 /* simulate 512 MiB RAM beginning at 1.5GiB */
667 return test_get_unreserved_size(uts, 0xE0000000);
669 LIB_TEST(lib_test_lmb_get_free_size, 0);
671 #ifdef CONFIG_LMB_USE_MAX_REGIONS
672 static int lib_test_lmb_max_regions(struct unit_test_state *uts)
674 const phys_addr_t ram = 0x00000000;
676 * All of 32bit memory space will contain regions for this test, so
677 * we need to scale ram_size (which in this case is the size of the lmb
680 const phys_size_t ram_size = ((0xFFFFFFFF >> CONFIG_LMB_MAX_REGIONS)
681 + 1) * CONFIG_LMB_MAX_REGIONS;
682 const phys_size_t blk_size = 0x10000;
689 ut_asserteq(lmb.memory.cnt, 0);
690 ut_asserteq(lmb.memory.max, CONFIG_LMB_MAX_REGIONS);
691 ut_asserteq(lmb.reserved.cnt, 0);
692 ut_asserteq(lmb.reserved.max, CONFIG_LMB_MAX_REGIONS);
694 /* Add CONFIG_LMB_MAX_REGIONS memory regions */
695 for (i = 0; i < CONFIG_LMB_MAX_REGIONS; i++) {
696 offset = ram + 2 * i * ram_size;
697 ret = lmb_add(&lmb, offset, ram_size);
700 ut_asserteq(lmb.memory.cnt, CONFIG_LMB_MAX_REGIONS);
701 ut_asserteq(lmb.reserved.cnt, 0);
703 /* error for the (CONFIG_LMB_MAX_REGIONS + 1) memory regions */
704 offset = ram + 2 * (CONFIG_LMB_MAX_REGIONS + 1) * ram_size;
705 ret = lmb_add(&lmb, offset, ram_size);
706 ut_asserteq(ret, -1);
708 ut_asserteq(lmb.memory.cnt, CONFIG_LMB_MAX_REGIONS);
709 ut_asserteq(lmb.reserved.cnt, 0);
711 /* reserve CONFIG_LMB_MAX_REGIONS regions */
712 for (i = 0; i < CONFIG_LMB_MAX_REGIONS; i++) {
713 offset = ram + 2 * i * blk_size;
714 ret = lmb_reserve(&lmb, offset, blk_size);
718 ut_asserteq(lmb.memory.cnt, CONFIG_LMB_MAX_REGIONS);
719 ut_asserteq(lmb.reserved.cnt, CONFIG_LMB_MAX_REGIONS);
721 /* error for the 9th reserved blocks */
722 offset = ram + 2 * (CONFIG_LMB_MAX_REGIONS + 1) * blk_size;
723 ret = lmb_reserve(&lmb, offset, blk_size);
724 ut_asserteq(ret, -1);
726 ut_asserteq(lmb.memory.cnt, CONFIG_LMB_MAX_REGIONS);
727 ut_asserteq(lmb.reserved.cnt, CONFIG_LMB_MAX_REGIONS);
729 /* check each regions */
730 for (i = 0; i < CONFIG_LMB_MAX_REGIONS; i++)
731 ut_asserteq(lmb.memory.region[i].base, ram + 2 * i * ram_size);
733 for (i = 0; i < CONFIG_LMB_MAX_REGIONS; i++)
734 ut_asserteq(lmb.reserved.region[i].base, ram + 2 * i * blk_size);
738 LIB_TEST(lib_test_lmb_max_regions, 0);
741 static int lib_test_lmb_flags(struct unit_test_state *uts)
743 const phys_addr_t ram = 0x40000000;
744 const phys_size_t ram_size = 0x20000000;
750 ret = lmb_add(&lmb, ram, ram_size);
753 /* reserve, same flag */
754 ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NOMAP);
756 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
759 /* reserve again, same flag */
760 ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NOMAP);
762 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
765 /* reserve again, new flag */
766 ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NONE);
767 ut_asserteq(ret, -1);
768 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
771 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
774 ret = lmb_reserve_flags(&lmb, 0x40020000, 0x10000, LMB_NOMAP);
776 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x20000,
780 ret = lmb_reserve_flags(&lmb, 0x40000000, 0x10000, LMB_NOMAP);
782 ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40000000, 0x30000,
785 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
787 ret = lmb_reserve_flags(&lmb, 0x40030000, 0x10000, LMB_NONE);
789 ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40000000, 0x30000,
790 0x40030000, 0x10000, 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 /* test that old API use LMB_NONE */
796 ret = lmb_reserve(&lmb, 0x40040000, 0x10000);
798 ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40000000, 0x30000,
799 0x40030000, 0x20000, 0, 0);
801 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
802 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[1]), 0);
804 ret = lmb_reserve_flags(&lmb, 0x40070000, 0x10000, LMB_NOMAP);
806 ASSERT_LMB(&lmb, ram, ram_size, 3, 0x40000000, 0x30000,
807 0x40030000, 0x20000, 0x40070000, 0x10000);
809 ret = lmb_reserve_flags(&lmb, 0x40050000, 0x10000, LMB_NOMAP);
811 ASSERT_LMB(&lmb, ram, ram_size, 4, 0x40000000, 0x30000,
812 0x40030000, 0x20000, 0x40050000, 0x10000);
814 /* merge with 2 adjacent regions */
815 ret = lmb_reserve_flags(&lmb, 0x40060000, 0x10000, LMB_NOMAP);
817 ASSERT_LMB(&lmb, ram, ram_size, 3, 0x40000000, 0x30000,
818 0x40030000, 0x20000, 0x40050000, 0x30000);
820 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
821 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[1]), 0);
822 ut_asserteq(lmb_is_nomap(&lmb.reserved.region[2]), 1);
826 LIB_TEST(lib_test_lmb_flags, 0);