aboutsummaryrefslogtreecommitdiff
path: root/test/lib/lmb.c
blob: 4b5b6e5e20984d6feae10fe12427f81f0045afc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2018 Simon Goldschmidt
 */

#include <dm.h>
#include <lmb.h>
#include <log.h>
#include <malloc.h>
#include <dm/test.h>
#include <test/lib.h>
#include <test/test.h>
#include <test/ut.h>

static inline bool lmb_is_nomap(struct lmb_property *m)
{
	return m->flags & LMB_NOMAP;
}

static int check_lmb(struct unit_test_state *uts, struct lmb *lmb,
		     phys_addr_t ram_base, phys_size_t ram_size,
		     unsigned long num_reserved,
		     phys_addr_t base1, phys_size_t size1,
		     phys_addr_t base2, phys_size_t size2,
		     phys_addr_t base3, phys_size_t size3)
{
	if (ram_size) {
		ut_asserteq(lmb->memory.cnt, 1);
		ut_asserteq(lmb->memory.region[0].base, ram_base);
		ut_asserteq(lmb->memory.region[0].size, ram_size);
	}

	ut_asserteq(lmb->reserved.cnt, num_reserved);
	if (num_reserved > 0) {
		ut_asserteq(lmb->reserved.region[0].base, base1);
		ut_asserteq(lmb->reserved.region[0].size, size1);
	}
	if (num_reserved > 1) {
		ut_asserteq(lmb->reserved.region[1].base, base2);
		ut_asserteq(lmb->reserved.region[1].size, size2);
	}
	if (num_reserved > 2) {
		ut_asserteq(lmb->reserved.region[2].base, base3);
		ut_asserteq(lmb->reserved.region[2].size, size3);
	}
	return 0;
}

#define ASSERT_LMB(lmb, ram_base, ram_size, num_reserved, base1, size1, \
		   base2, size2, base3, size3) \
		   ut_assert(!check_lmb(uts, lmb, ram_base, ram_size, \
			     num_reserved, base1, size1, base2, size2, base3, \
			     size3))

/*
 * Test helper function that reserves 64 KiB somewhere in the simulated RAM and
 * then does some alloc + free tests.
 */
static int test_multi_alloc(struct unit_test_state *uts, const phys_addr_t ram,
			    const phys_size_t ram_size, const phys_addr_t ram0,
			    const phys_size_t ram0_size,
			    const phys_addr_t alloc_64k_addr)
{
	const phys_addr_t ram_end = ram + ram_size;
	const phys_addr_t alloc_64k_end = alloc_64k_addr + 0x10000;

	struct lmb lmb;
	long ret;
	phys_addr_t a, a2, b, b2, c, d;

	/* check for overflow */
	ut_assert(ram_end == 0 || ram_end > ram);
	ut_assert(alloc_64k_end > alloc_64k_addr);
	/* check input addresses + size */
	ut_assert(alloc_64k_addr >= ram + 8);
	ut_assert(alloc_64k_end <= ram_end - 8);

	lmb_init(&lmb);

	if (ram0_size) {
		ret = lmb_add(&lmb, ram0, ram0_size);
		ut_asserteq(ret, 0);
	}

	ret = lmb_add(&lmb, ram, ram_size);
	ut_asserteq(ret, 0);

	if (ram0_size) {
		ut_asserteq(lmb.memory.cnt, 2);
		ut_asserteq(lmb.memory.region[0].base, ram0);
		ut_asserteq(lmb.memory.region[0].size, ram0_size);
		ut_asserteq(lmb.memory.region[1].base, ram);
		ut_asserteq(lmb.memory.region[1].size, ram_size);
	} else {
		ut_asserteq(lmb.memory.cnt, 1);
		ut_asserteq(lmb.memory.region[0].base, ram);
		ut_asserteq(lmb.memory.region[0].size, ram_size);
	}

	/* reserve 64KiB somewhere */
	ret = lmb_reserve(&lmb, alloc_64k_addr, 0x10000);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, 0, 0, 1, alloc_64k_addr, 0x10000,
		   0, 0, 0, 0);

	/* allocate somewhere, should be at the end of RAM */
	a = lmb_alloc(&lmb, 4, 1);
	ut_asserteq(a, ram_end - 4);
	ASSERT_LMB(&lmb, 0, 0, 2, alloc_64k_addr, 0x10000,
		   ram_end - 4, 4, 0, 0);
	/* alloc below end of reserved region -> below reserved region */
	b = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
	ut_asserteq(b, alloc_64k_addr - 4);
	ASSERT_LMB(&lmb, 0, 0, 2,
		   alloc_64k_addr - 4, 0x10000 + 4, ram_end - 4, 4, 0, 0);

	/* 2nd time */
	c = lmb_alloc(&lmb, 4, 1);
	ut_asserteq(c, ram_end - 8);
	ASSERT_LMB(&lmb, 0, 0, 2,
		   alloc_64k_addr - 4, 0x10000 + 4, ram_end - 8, 8, 0, 0);
	d = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
	ut_asserteq(d, alloc_64k_addr - 8);
	ASSERT_LMB(&lmb, 0, 0, 2,
		   alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);

	ret = lmb_free(&lmb, a, 4);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, 0, 0, 2,
		   alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
	/* allocate again to ensure we get the same address */
	a2 = lmb_alloc(&lmb, 4, 1);
	ut_asserteq(a, a2);
	ASSERT_LMB(&lmb, 0, 0, 2,
		   alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);
	ret = lmb_free(&lmb, a2, 4);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, 0, 0, 2,
		   alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);

	ret = lmb_free(&lmb, b, 4);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, 0, 0, 3,
		   alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000,
		   ram_end - 8, 4);
	/* allocate again to ensure we get the same address */
	b2 = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end);
	ut_asserteq(b, b2);
	ASSERT_LMB(&lmb, 0, 0, 2,
		   alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
	ret = lmb_free(&lmb, b2, 4);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, 0, 0, 3,
		   alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000,
		   ram_end - 8, 4);

	ret = lmb_free(&lmb, c, 4);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, 0, 0, 2,
		   alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, 0, 0);
	ret = lmb_free(&lmb, d, 4);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, 0, 0, 1, alloc_64k_addr, 0x10000,
		   0, 0, 0, 0);

	if (ram0_size) {
		ut_asserteq(lmb.memory.cnt, 2);
		ut_asserteq(lmb.memory.region[0].base, ram0);
		ut_asserteq(lmb.memory.region[0].size, ram0_size);
		ut_asserteq(lmb.memory.region[1].base, ram);
		ut_asserteq(lmb.memory.region[1].size, ram_size);
	} else {
		ut_asserteq(lmb.memory.cnt, 1);
		ut_asserteq(lmb.memory.region[0].base, ram);
		ut_asserteq(lmb.memory.region[0].size, ram_size);
	}

	return 0;
}

static int test_multi_alloc_512mb(struct unit_test_state *uts,
				  const phys_addr_t ram)
{
	return test_multi_alloc(uts, ram, 0x20000000, 0, 0, ram + 0x10000000);
}

static int test_multi_alloc_512mb_x2(struct unit_test_state *uts,
				     const phys_addr_t ram,
				     const phys_addr_t ram0)
{
	return test_multi_alloc(uts, ram, 0x20000000, ram0, 0x20000000,
				ram + 0x10000000);
}

/* Create a memory region with one reserved region and allocate */
static int lib_test_lmb_simple(struct unit_test_state *uts)
{
	int ret;

	/* simulate 512 MiB RAM beginning at 1GiB */
	ret = test_multi_alloc_512mb(uts, 0x40000000);
	if (ret)
		return ret;

	/* simulate 512 MiB RAM beginning at 1.5GiB */
	return test_multi_alloc_512mb(uts, 0xE0000000);
}
LIB_TEST(lib_test_lmb_simple, 0);

/* Create two memory regions with one reserved region and allocate */
static int lib_test_lmb_simple_x2(struct unit_test_state *uts)
{
	int ret;

	/* simulate 512 MiB RAM beginning at 2GiB and 1 GiB */
	ret = test_multi_alloc_512mb_x2(uts, 0x80000000, 0x40000000);
	if (ret)
		return ret;

	/* simulate 512 MiB RAM beginning at 3.5GiB and 1 GiB */
	return test_multi_alloc_512mb_x2(uts, 0xE0000000, 0x40000000);
}
LIB_TEST(lib_test_lmb_simple_x2, 0);

/* Simulate 512 MiB RAM, allocate some blocks that fit/don't fit */
static int test_bigblock(struct unit_test_state *uts, const phys_addr_t ram)
{
	const phys_size_t ram_size = 0x20000000;
	const phys_size_t big_block_size = 0x10000000;
	const phys_addr_t ram_end = ram + ram_size;
	const phys_addr_t alloc_64k_addr = ram + 0x10000000;
	struct lmb lmb;
	long ret;
	phys_addr_t a, b;

	/* check for overflow */
	ut_assert(ram_end == 0 || ram_end > ram);

	lmb_init(&lmb);

	ret = lmb_add(&lmb, ram, ram_size);
	ut_asserteq(ret, 0);

	/* reserve 64KiB in the middle of RAM */
	ret = lmb_reserve(&lmb, alloc_64k_addr, 0x10000);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
		   0, 0, 0, 0);

	/* allocate a big block, should be below reserved */
	a = lmb_alloc(&lmb, big_block_size, 1);
	ut_asserteq(a, ram);
	ASSERT_LMB(&lmb, ram, ram_size, 1, a,
		   big_block_size + 0x10000, 0, 0, 0, 0);
	/* allocate 2nd big block */
	/* This should fail, printing an error */
	b = lmb_alloc(&lmb, big_block_size, 1);
	ut_asserteq(b, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, a,
		   big_block_size + 0x10000, 0, 0, 0, 0);

	ret = lmb_free(&lmb, a, big_block_size);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
		   0, 0, 0, 0);

	/* allocate too big block */
	/* This should fail, printing an error */
	a = lmb_alloc(&lmb, ram_size, 1);
	ut_asserteq(a, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000,
		   0, 0, 0, 0);

	return 0;
}

static int lib_test_lmb_big(struct unit_test_state *uts)
{
	int ret;

	/* simulate 512 MiB RAM beginning at 1GiB */
	ret = test_bigblock(uts, 0x40000000);
	if (ret)
		return ret;

	/* simulate 512 MiB RAM beginning at 1.5GiB */
	return test_bigblock(uts, 0xE0000000);
}
LIB_TEST(lib_test_lmb_big, 0);

/* Simulate 512 MiB RAM, allocate a block without previous reservation */
static int test_noreserved(struct unit_test_state *uts, const phys_addr_t ram,
			   const phys_addr_t alloc_size, const ulong align)
{
	const phys_size_t ram_size = 0x20000000;
	const phys_addr_t ram_end = ram + ram_size;
	struct lmb lmb;
	long ret;
	phys_addr_t a, b;
	const phys_addr_t alloc_size_aligned = (alloc_size + align - 1) &
		~(align - 1);

	/* check for overflow */
	ut_assert(ram_end == 0 || ram_end > ram);

	lmb_init(&lmb);

	ret = lmb_add(&lmb, ram, ram_size);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);

	/* allocate a block */
	a = lmb_alloc(&lmb, alloc_size, align);
	ut_assert(a != 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
		   alloc_size, 0, 0, 0, 0);
	/* allocate another block */
	b = lmb_alloc(&lmb, alloc_size, align);
	ut_assert(b != 0);
	if (alloc_size == alloc_size_aligned) {
		ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size -
			   (alloc_size_aligned * 2), alloc_size * 2, 0, 0, 0,
			   0);
	} else {
		ASSERT_LMB(&lmb, ram, ram_size, 2, ram + ram_size -
			   (alloc_size_aligned * 2), alloc_size, ram + ram_size
			   - alloc_size_aligned, alloc_size, 0, 0);
	}
	/* and free them */
	ret = lmb_free(&lmb, b, alloc_size);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
		   alloc_size, 0, 0, 0, 0);
	ret = lmb_free(&lmb, a, alloc_size);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);

	/* allocate a block with base*/
	b = lmb_alloc_base(&lmb, alloc_size, align, ram_end);
	ut_assert(a == b);
	ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned,
		   alloc_size, 0, 0, 0, 0);
	/* and free it */
	ret = lmb_free(&lmb, b, alloc_size);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);

	return 0;
}

static int lib_test_lmb_noreserved(struct unit_test_state *uts)
{
	int ret;

	/* simulate 512 MiB RAM beginning at 1GiB */
	ret = test_noreserved(uts, 0x40000000, 4, 1);
	if (ret)
		return ret;

	/* simulate 512 MiB RAM beginning at 1.5GiB */
	return test_noreserved(uts, 0xE0000000, 4, 1);
}

LIB_TEST(lib_test_lmb_noreserved, 0);

static int lib_test_lmb_unaligned_size(struct unit_test_state *uts)
{
	int ret;

	/* simulate 512 MiB RAM beginning at 1GiB */
	ret = test_noreserved(uts, 0x40000000, 5, 8);
	if (ret)
		return ret;

	/* simulate 512 MiB RAM beginning at 1.5GiB */
	return test_noreserved(uts, 0xE0000000, 5, 8);
}
LIB_TEST(lib_test_lmb_unaligned_size, 0);

/*
 * Simulate a RAM that starts at 0 and allocate down to address 0, which must
 * fail as '0' means failure for the lmb_alloc functions.
 */
static int lib_test_lmb_at_0(struct unit_test_state *uts)
{
	const phys_addr_t ram = 0;
	const phys_size_t ram_size = 0x20000000;
	struct lmb lmb;
	long ret;
	phys_addr_t a, b;

	lmb_init(&lmb);

	ret = lmb_add(&lmb, ram, ram_size);
	ut_asserteq(ret, 0);

	/* allocate nearly everything */
	a = lmb_alloc(&lmb, ram_size - 4, 1);
	ut_asserteq(a, ram + 4);
	ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
		   0, 0, 0, 0);
	/* allocate the rest */
	/* This should fail as the allocated address would be 0 */
	b = lmb_alloc(&lmb, 4, 1);
	ut_asserteq(b, 0);
	/* check that this was an error by checking lmb */
	ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
		   0, 0, 0, 0);
	/* check that this was an error by freeing b */
	ret = lmb_free(&lmb, b, 4);
	ut_asserteq(ret, -1);
	ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4,
		   0, 0, 0, 0);

	ret = lmb_free(&lmb, a, ram_size - 4);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);

	return 0;
}
LIB_TEST(lib_test_lmb_at_0, 0);

/* Check that calling lmb_reserve with overlapping regions fails. */
static int lib_test_lmb_overlapping_reserve(struct unit_test_state *uts)
{
	const phys_addr_t ram = 0x40000000;
	const phys_size_t ram_size = 0x20000000;
	struct lmb lmb;
	long ret;

	lmb_init(&lmb);

	ret = lmb_add(&lmb, ram, ram_size);
	ut_asserteq(ret, 0);

	ret = lmb_reserve(&lmb, 0x40010000, 0x10000);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
		   0, 0, 0, 0);
	/* allocate overlapping region should fail */
	ret = lmb_reserve(&lmb, 0x40011000, 0x10000);
	ut_asserteq(ret, -1);
	ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
		   0, 0, 0, 0);
	/* allocate 3nd region */
	ret = lmb_reserve(&lmb, 0x40030000, 0x10000);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40010000, 0x10000,
		   0x40030000, 0x10000, 0, 0);
	/* allocate 2nd region , This should coalesced all region into one */
	ret = lmb_reserve(&lmb, 0x40020000, 0x10000);
	ut_assert(ret >= 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x30000,
		   0, 0, 0, 0);

	/* allocate 2nd region, which should be added as first region */
	ret = lmb_reserve(&lmb, 0x40000000, 0x8000);
	ut_assert(ret >= 0);
	ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40000000, 0x8000,
		   0x40010000, 0x30000, 0, 0);

	/* allocate 3rd region, coalesce with first and overlap with second */
	ret = lmb_reserve(&lmb, 0x40008000, 0x10000);
	ut_assert(ret >= 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40000000, 0x40000,
		   0, 0, 0, 0);
	return 0;
}
LIB_TEST(lib_test_lmb_overlapping_reserve, 0);

/*
 * Simulate 512 MiB RAM, reserve 3 blocks, allocate addresses in between.
 * Expect addresses outside the memory range to fail.
 */
static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
{
	const phys_size_t ram_size = 0x20000000;
	const phys_addr_t ram_end = ram + ram_size;
	const phys_size_t alloc_addr_a = ram + 0x8000000;
	const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
	const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
	struct lmb lmb;
	long ret;
	phys_addr_t a, b, c, d, e;

	/* check for overflow */
	ut_assert(ram_end == 0 || ram_end > ram);

	lmb_init(&lmb);

	ret = lmb_add(&lmb, ram, ram_size);
	ut_asserteq(ret, 0);

	/*  reserve 3 blocks */
	ret = lmb_reserve(&lmb, alloc_addr_a, 0x10000);
	ut_asserteq(ret, 0);
	ret = lmb_reserve(&lmb, alloc_addr_b, 0x10000);
	ut_asserteq(ret, 0);
	ret = lmb_reserve(&lmb, alloc_addr_c, 0x10000);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 3, alloc_addr_a, 0x10000,
		   alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);

	/* allocate blocks */
	a = lmb_alloc_addr(&lmb, ram, alloc_addr_a - ram);
	ut_asserteq(a, ram);
	ASSERT_LMB(&lmb, ram, ram_size, 3, ram, 0x8010000,
		   alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
	b = lmb_alloc_addr(&lmb, alloc_addr_a + 0x10000,
			   alloc_addr_b - alloc_addr_a - 0x10000);
	ut_asserteq(b, alloc_addr_a + 0x10000);
	ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x10010000,
		   alloc_addr_c, 0x10000, 0, 0);
	c = lmb_alloc_addr(&lmb, alloc_addr_b + 0x10000,
			   alloc_addr_c - alloc_addr_b - 0x10000);
	ut_asserteq(c, alloc_addr_b + 0x10000);
	ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
		   0, 0, 0, 0);
	d = lmb_alloc_addr(&lmb, alloc_addr_c + 0x10000,
			   ram_end - alloc_addr_c - 0x10000);
	ut_asserteq(d, alloc_addr_c + 0x10000);
	ASSERT_LMB(&lmb, ram, ram_size, 1, ram, ram_size,
		   0, 0, 0, 0);

	/* allocating anything else should fail */
	e = lmb_alloc(&lmb, 1, 1);
	ut_asserteq(e, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, ram, ram_size,
		   0, 0, 0, 0);

	ret = lmb_free(&lmb, d, ram_end - alloc_addr_c - 0x10000);
	ut_asserteq(ret, 0);

	/* allocate at 3 points in free range */

	d = lmb_alloc_addr(&lmb, ram_end - 4, 4);
	ut_asserteq(d, ram_end - 4);
	ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x18010000,
		   d, 4, 0, 0);
	ret = lmb_free(&lmb, d, 4);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
		   0, 0, 0, 0);

	d = lmb_alloc_addr(&lmb, ram_end - 128, 4);
	ut_asserteq(d, ram_end - 128);
	ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x18010000,
		   d, 4, 0, 0);
	ret = lmb_free(&lmb, d, 4);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
		   0, 0, 0, 0);

	d = lmb_alloc_addr(&lmb, alloc_addr_c + 0x10000, 4);
	ut_asserteq(d, alloc_addr_c + 0x10000);
	ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010004,
		   0, 0, 0, 0);
	ret = lmb_free(&lmb, d, 4);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000,
		   0, 0, 0, 0);

	/* allocate at the bottom */
	ret = lmb_free(&lmb, a, alloc_addr_a - ram);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, ram + 0x8000000, 0x10010000,
		   0, 0, 0, 0);
	d = lmb_alloc_addr(&lmb, ram, 4);
	ut_asserteq(d, ram);
	ASSERT_LMB(&lmb, ram, ram_size, 2, d, 4,
		   ram + 0x8000000, 0x10010000, 0, 0);

	/* check that allocating outside memory fails */
	if (ram_end != 0) {
		ret = lmb_alloc_addr(&lmb, ram_end, 1);
		ut_asserteq(ret, 0);
	}
	if (ram != 0) {
		ret = lmb_alloc_addr(&lmb, ram - 1, 1);
		ut_asserteq(ret, 0);
	}

	return 0;
}

static int lib_test_lmb_alloc_addr(struct unit_test_state *uts)
{
	int ret;

	/* simulate 512 MiB RAM beginning at 1GiB */
	ret = test_alloc_addr(uts, 0x40000000);
	if (ret)
		return ret;

	/* simulate 512 MiB RAM beginning at 1.5GiB */
	return test_alloc_addr(uts, 0xE0000000);
}
LIB_TEST(lib_test_lmb_alloc_addr, 0);

/* Simulate 512 MiB RAM, reserve 3 blocks, check addresses in between */
static int test_get_unreserved_size(struct unit_test_state *uts,
				    const phys_addr_t ram)
{
	const phys_size_t ram_size = 0x20000000;
	const phys_addr_t ram_end = ram + ram_size;
	const phys_size_t alloc_addr_a = ram + 0x8000000;
	const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
	const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
	struct lmb lmb;
	long ret;
	phys_size_t s;

	/* check for overflow */
	ut_assert(ram_end == 0 || ram_end > ram);

	lmb_init(&lmb);

	ret = lmb_add(&lmb, ram, ram_size);
	ut_asserteq(ret, 0);

	/*  reserve 3 blocks */
	ret = lmb_reserve(&lmb, alloc_addr_a, 0x10000);
	ut_asserteq(ret, 0);
	ret = lmb_reserve(&lmb, alloc_addr_b, 0x10000);
	ut_asserteq(ret, 0);
	ret = lmb_reserve(&lmb, alloc_addr_c, 0x10000);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 3, alloc_addr_a, 0x10000,
		   alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);

	/* check addresses in between blocks */
	s = lmb_get_free_size(&lmb, ram);
	ut_asserteq(s, alloc_addr_a - ram);
	s = lmb_get_free_size(&lmb, ram + 0x10000);
	ut_asserteq(s, alloc_addr_a - ram - 0x10000);
	s = lmb_get_free_size(&lmb, alloc_addr_a - 4);
	ut_asserteq(s, 4);

	s = lmb_get_free_size(&lmb, alloc_addr_a + 0x10000);
	ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x10000);
	s = lmb_get_free_size(&lmb, alloc_addr_a + 0x20000);
	ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x20000);
	s = lmb_get_free_size(&lmb, alloc_addr_b - 4);
	ut_asserteq(s, 4);

	s = lmb_get_free_size(&lmb, alloc_addr_c + 0x10000);
	ut_asserteq(s, ram_end - alloc_addr_c - 0x10000);
	s = lmb_get_free_size(&lmb, alloc_addr_c + 0x20000);
	ut_asserteq(s, ram_end - alloc_addr_c - 0x20000);
	s = lmb_get_free_size(&lmb, ram_end - 4);
	ut_asserteq(s, 4);

	return 0;
}

static int lib_test_lmb_get_free_size(struct unit_test_state *uts)
{
	int ret;

	/* simulate 512 MiB RAM beginning at 1GiB */
	ret = test_get_unreserved_size(uts, 0x40000000);
	if (ret)
		return ret;

	/* simulate 512 MiB RAM beginning at 1.5GiB */
	return test_get_unreserved_size(uts, 0xE0000000);
}
LIB_TEST(lib_test_lmb_get_free_size, 0);

#ifdef CONFIG_LMB_USE_MAX_REGIONS
static int lib_test_lmb_max_regions(struct unit_test_state *uts)
{
	const phys_addr_t ram = 0x00000000;
	/*
	 * All of 32bit memory space will contain regions for this test, so
	 * we need to scale ram_size (which in this case is the size of the lmb
	 * region) to match.
	 */
	const phys_size_t ram_size = ((0xFFFFFFFF >> CONFIG_LMB_MAX_REGIONS)
			+ 1) * CONFIG_LMB_MAX_REGIONS;
	const phys_size_t blk_size = 0x10000;
	phys_addr_t offset;
	struct lmb lmb;
	int ret, i;

	lmb_init(&lmb);

	ut_asserteq(lmb.memory.cnt, 0);
	ut_asserteq(lmb.memory.max, CONFIG_LMB_MAX_REGIONS);
	ut_asserteq(lmb.reserved.cnt, 0);
	ut_asserteq(lmb.reserved.max, CONFIG_LMB_MAX_REGIONS);

	/*  Add CONFIG_LMB_MAX_REGIONS memory regions */
	for (i = 0; i < CONFIG_LMB_MAX_REGIONS; i++) {
		offset = ram + 2 * i * ram_size;
		ret = lmb_add(&lmb, offset, ram_size);
		ut_asserteq(ret, 0);
	}
	ut_asserteq(lmb.memory.cnt, CONFIG_LMB_MAX_REGIONS);
	ut_asserteq(lmb.reserved.cnt, 0);

	/*  error for the (CONFIG_LMB_MAX_REGIONS + 1) memory regions */
	offset = ram + 2 * (CONFIG_LMB_MAX_REGIONS + 1) * ram_size;
	ret = lmb_add(&lmb, offset, ram_size);
	ut_asserteq(ret, -1);

	ut_asserteq(lmb.memory.cnt, CONFIG_LMB_MAX_REGIONS);
	ut_asserteq(lmb.reserved.cnt, 0);

	/*  reserve CONFIG_LMB_MAX_REGIONS regions */
	for (i = 0; i < CONFIG_LMB_MAX_REGIONS; i++) {
		offset = ram + 2 * i * blk_size;
		ret = lmb_reserve(&lmb, offset, blk_size);
		ut_asserteq(ret, 0);
	}

	ut_asserteq(lmb.memory.cnt, CONFIG_LMB_MAX_REGIONS);
	ut_asserteq(lmb.reserved.cnt, CONFIG_LMB_MAX_REGIONS);

	/*  error for the 9th reserved blocks */
	offset = ram + 2 * (CONFIG_LMB_MAX_REGIONS + 1) * blk_size;
	ret = lmb_reserve(&lmb, offset, blk_size);
	ut_asserteq(ret, -1);

	ut_asserteq(lmb.memory.cnt, CONFIG_LMB_MAX_REGIONS);
	ut_asserteq(lmb.reserved.cnt, CONFIG_LMB_MAX_REGIONS);

	/*  check each regions */
	for (i = 0; i < CONFIG_LMB_MAX_REGIONS; i++)
		ut_asserteq(lmb.memory.region[i].base, ram + 2 * i * ram_size);

	for (i = 0; i < CONFIG_LMB_MAX_REGIONS; i++)
		ut_asserteq(lmb.reserved.region[i].base, ram + 2 * i * blk_size);

	return 0;
}
LIB_TEST(lib_test_lmb_max_regions, 0);
#endif

static int lib_test_lmb_flags(struct unit_test_state *uts)
{
	const phys_addr_t ram = 0x40000000;
	const phys_size_t ram_size = 0x20000000;
	struct lmb lmb;
	long ret;

	lmb_init(&lmb);

	ret = lmb_add(&lmb, ram, ram_size);
	ut_asserteq(ret, 0);

	/* reserve, same flag */
	ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NOMAP);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
		   0, 0, 0, 0);

	/* reserve again, same flag */
	ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NOMAP);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
		   0, 0, 0, 0);

	/* reserve again, new flag */
	ret = lmb_reserve_flags(&lmb, 0x40010000, 0x10000, LMB_NONE);
	ut_asserteq(ret, -1);
	ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000,
		   0, 0, 0, 0);

	ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);

	/* merge after */
	ret = lmb_reserve_flags(&lmb, 0x40020000, 0x10000, LMB_NOMAP);
	ut_asserteq(ret, 1);
	ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x20000,
		   0, 0, 0, 0);

	/* merge before */
	ret = lmb_reserve_flags(&lmb, 0x40000000, 0x10000, LMB_NOMAP);
	ut_asserteq(ret, 1);
	ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40000000, 0x30000,
		   0, 0, 0, 0);

	ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);

	ret = lmb_reserve_flags(&lmb, 0x40030000, 0x10000, LMB_NONE);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40000000, 0x30000,
		   0x40030000, 0x10000, 0, 0);

	ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
	ut_asserteq(lmb_is_nomap(&lmb.reserved.region[1]), 0);

	/* test that old API use LMB_NONE */
	ret = lmb_reserve(&lmb, 0x40040000, 0x10000);
	ut_asserteq(ret, 1);
	ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40000000, 0x30000,
		   0x40030000, 0x20000, 0, 0);

	ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
	ut_asserteq(lmb_is_nomap(&lmb.reserved.region[1]), 0);

	ret = lmb_reserve_flags(&lmb, 0x40070000, 0x10000, LMB_NOMAP);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 3, 0x40000000, 0x30000,
		   0x40030000, 0x20000, 0x40070000, 0x10000);

	ret = lmb_reserve_flags(&lmb, 0x40050000, 0x10000, LMB_NOMAP);
	ut_asserteq(ret, 0);
	ASSERT_LMB(&lmb, ram, ram_size, 4, 0x40000000, 0x30000,
		   0x40030000, 0x20000, 0x40050000, 0x10000);

	/* merge with 2 adjacent regions */
	ret = lmb_reserve_flags(&lmb, 0x40060000, 0x10000, LMB_NOMAP);
	ut_asserteq(ret, 2);
	ASSERT_LMB(&lmb, ram, ram_size, 3, 0x40000000, 0x30000,
		   0x40030000, 0x20000, 0x40050000, 0x30000);

	ut_asserteq(lmb_is_nomap(&lmb.reserved.region[0]), 1);
	ut_asserteq(lmb_is_nomap(&lmb.reserved.region[1]), 0);
	ut_asserteq(lmb_is_nomap(&lmb.reserved.region[2]), 1);

	return 0;
}
LIB_TEST(lib_test_lmb_flags, 0);