summaryrefslogtreecommitdiff
path: root/tools/testing/memblock/tests/alloc_api.c
blob: e20e326d636facb4255bdd7a0f44b823d07c6667 (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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
// SPDX-License-Identifier: GPL-2.0-or-later
#include "alloc_api.h"

/*
 * A simple test that tries to allocate a small memory region.
 * Expect to allocate an aligned region near the end of the available memory.
 */
static int alloc_top_down_simple_check(void)
{
	struct memblock_region *rgn = &memblock.reserved.regions[0];
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	phys_addr_t size = SZ_2;
	phys_addr_t expected_start;

	setup_memblock();

	expected_start = memblock_end_of_DRAM() - SMP_CACHE_BYTES;

	allocated_ptr = memblock_alloc(size, SMP_CACHE_BYTES);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, size);

	ASSERT_EQ(rgn->size, size);
	ASSERT_EQ(rgn->base, expected_start);

	ASSERT_EQ(memblock.reserved.cnt, 1);
	ASSERT_EQ(memblock.reserved.total_size, size);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory next to a reserved region that starts at
 * the misaligned address. Expect to create two separate entries, with the new
 * entry aligned to the provided alignment:
 *
 *              +
 * |            +--------+         +--------|
 * |            |  rgn2  |         |  rgn1  |
 * +------------+--------+---------+--------+
 *              ^
 *              |
 *              Aligned address boundary
 *
 * The allocation direction is top-down and region arrays are sorted from lower
 * to higher addresses, so the new region will be the first entry in
 * memory.reserved array. The previously reserved region does not get modified.
 * Region counter and total size get updated.
 */
static int alloc_top_down_disjoint_check(void)
{
	/* After allocation, this will point to the "old" region */
	struct memblock_region *rgn1 = &memblock.reserved.regions[1];
	struct memblock_region *rgn2 = &memblock.reserved.regions[0];
	struct region r1;
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	phys_addr_t r2_size = SZ_16;
	/* Use custom alignment */
	phys_addr_t alignment = SMP_CACHE_BYTES * 2;
	phys_addr_t total_size;
	phys_addr_t expected_start;

	setup_memblock();

	r1.base = memblock_end_of_DRAM() - SZ_2;
	r1.size = SZ_2;

	total_size = r1.size + r2_size;
	expected_start = memblock_end_of_DRAM() - alignment;

	memblock_reserve(r1.base, r1.size);

	allocated_ptr = memblock_alloc(r2_size, alignment);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, r2_size);

	ASSERT_EQ(rgn1->size, r1.size);
	ASSERT_EQ(rgn1->base, r1.base);

	ASSERT_EQ(rgn2->size, r2_size);
	ASSERT_EQ(rgn2->base, expected_start);

	ASSERT_EQ(memblock.reserved.cnt, 2);
	ASSERT_EQ(memblock.reserved.total_size, total_size);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when there is enough space at the end
 * of the previously reserved block (i.e. first fit):
 *
 *  |              +--------+--------------|
 *  |              |   r1   |      r2      |
 *  +--------------+--------+--------------+
 *
 * Expect a merge of both regions. Only the region size gets updated.
 */
static int alloc_top_down_before_check(void)
{
	struct memblock_region *rgn = &memblock.reserved.regions[0];
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	/*
	 * The first region ends at the aligned address to test region merging
	 */
	phys_addr_t r1_size = SMP_CACHE_BYTES;
	phys_addr_t r2_size = SZ_512;
	phys_addr_t total_size = r1_size + r2_size;

	setup_memblock();

	memblock_reserve(memblock_end_of_DRAM() - total_size, r1_size);

	allocated_ptr = memblock_alloc(r2_size, SMP_CACHE_BYTES);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, r2_size);

	ASSERT_EQ(rgn->size, total_size);
	ASSERT_EQ(rgn->base, memblock_end_of_DRAM() - total_size);

	ASSERT_EQ(memblock.reserved.cnt, 1);
	ASSERT_EQ(memblock.reserved.total_size, total_size);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when there is not enough space at the
 * end of the previously reserved block (i.e. second fit):
 *
 *  |            +-----------+------+     |
 *  |            |     r2    |  r1  |     |
 *  +------------+-----------+------+-----+
 *
 * Expect a merge of both regions. Both the base address and size of the region
 * get updated.
 */
static int alloc_top_down_after_check(void)
{
	struct memblock_region *rgn = &memblock.reserved.regions[0];
	struct region r1;
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	phys_addr_t r2_size = SZ_512;
	phys_addr_t total_size;

	setup_memblock();

	/*
	 * The first region starts at the aligned address to test region merging
	 */
	r1.base = memblock_end_of_DRAM() - SMP_CACHE_BYTES;
	r1.size = SZ_8;

	total_size = r1.size + r2_size;

	memblock_reserve(r1.base, r1.size);

	allocated_ptr = memblock_alloc(r2_size, SMP_CACHE_BYTES);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, r2_size);

	ASSERT_EQ(rgn->size, total_size);
	ASSERT_EQ(rgn->base, r1.base - r2_size);

	ASSERT_EQ(memblock.reserved.cnt, 1);
	ASSERT_EQ(memblock.reserved.total_size, total_size);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when there are two reserved regions with
 * a gap too small to fit the new region:
 *
 *  |       +--------+----------+   +------|
 *  |       |   r3   |    r2    |   |  r1  |
 *  +-------+--------+----------+---+------+
 *
 * Expect to allocate a region before the one that starts at the lower address,
 * and merge them into one. The region counter and total size fields get
 * updated.
 */
static int alloc_top_down_second_fit_check(void)
{
	struct memblock_region *rgn = &memblock.reserved.regions[0];
	struct region r1, r2;
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	phys_addr_t r3_size = SZ_1K;
	phys_addr_t total_size;

	setup_memblock();

	r1.base = memblock_end_of_DRAM() - SZ_512;
	r1.size = SZ_512;

	r2.base = r1.base - SZ_512;
	r2.size = SZ_256;

	total_size = r1.size + r2.size + r3_size;

	memblock_reserve(r1.base, r1.size);
	memblock_reserve(r2.base, r2.size);

	allocated_ptr = memblock_alloc(r3_size, SMP_CACHE_BYTES);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, r3_size);

	ASSERT_EQ(rgn->size, r2.size + r3_size);
	ASSERT_EQ(rgn->base, r2.base - r3_size);

	ASSERT_EQ(memblock.reserved.cnt, 2);
	ASSERT_EQ(memblock.reserved.total_size, total_size);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when there are two reserved regions with
 * a gap big enough to accommodate the new region:
 *
 *  |     +--------+--------+--------+     |
 *  |     |   r2   |   r3   |   r1   |     |
 *  +-----+--------+--------+--------+-----+
 *
 * Expect to merge all of them, creating one big entry in memblock.reserved
 * array. The region counter and total size fields get updated.
 */
static int alloc_in_between_generic_check(void)
{
	struct memblock_region *rgn = &memblock.reserved.regions[0];
	struct region r1, r2;
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	phys_addr_t gap_size = SMP_CACHE_BYTES;
	phys_addr_t r3_size = SZ_64;
	/*
	 * Calculate regions size so there's just enough space for the new entry
	 */
	phys_addr_t rgn_size = (MEM_SIZE - (2 * gap_size + r3_size)) / 2;
	phys_addr_t total_size;

	setup_memblock();

	r1.size = rgn_size;
	r1.base = memblock_end_of_DRAM() - (gap_size + rgn_size);

	r2.size = rgn_size;
	r2.base = memblock_start_of_DRAM() + gap_size;

	total_size = r1.size + r2.size + r3_size;

	memblock_reserve(r1.base, r1.size);
	memblock_reserve(r2.base, r2.size);

	allocated_ptr = memblock_alloc(r3_size, SMP_CACHE_BYTES);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, r3_size);

	ASSERT_EQ(rgn->size, total_size);
	ASSERT_EQ(rgn->base, r1.base - r2.size - r3_size);

	ASSERT_EQ(memblock.reserved.cnt, 1);
	ASSERT_EQ(memblock.reserved.total_size, total_size);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when the memory is filled with reserved
 * regions with memory gaps too small to fit the new region:
 *
 * +-------+
 * |  new  |
 * +--+----+
 *    |    +-----+    +-----+    +-----+    |
 *    |    | res |    | res |    | res |    |
 *    +----+-----+----+-----+----+-----+----+
 *
 * Expect no allocation to happen.
 */
static int alloc_small_gaps_generic_check(void)
{
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	phys_addr_t region_size = SZ_1K;
	phys_addr_t gap_size = SZ_256;
	phys_addr_t region_end;

	setup_memblock();

	region_end = memblock_start_of_DRAM();

	while (region_end < memblock_end_of_DRAM()) {
		memblock_reserve(region_end + gap_size, region_size);
		region_end += gap_size + region_size;
	}

	allocated_ptr = memblock_alloc(region_size, SMP_CACHE_BYTES);

	ASSERT_EQ(allocated_ptr, NULL);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when all memory is reserved.
 * Expect no allocation to happen.
 */
static int alloc_all_reserved_generic_check(void)
{
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	setup_memblock();

	/* Simulate full memory */
	memblock_reserve(memblock_start_of_DRAM(), MEM_SIZE);

	allocated_ptr = memblock_alloc(SZ_256, SMP_CACHE_BYTES);

	ASSERT_EQ(allocated_ptr, NULL);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when the memory is almost full,
 * with not enough space left for the new region:
 *
 *                                +-------+
 *                                |  new  |
 *                                +-------+
 *  |-----------------------------+   |
 *  |          reserved           |   |
 *  +-----------------------------+---+
 *
 * Expect no allocation to happen.
 */
static int alloc_no_space_generic_check(void)
{
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	setup_memblock();

	phys_addr_t available_size = SZ_256;
	phys_addr_t reserved_size = MEM_SIZE - available_size;

	/* Simulate almost-full memory */
	memblock_reserve(memblock_start_of_DRAM(), reserved_size);

	allocated_ptr = memblock_alloc(SZ_1K, SMP_CACHE_BYTES);

	ASSERT_EQ(allocated_ptr, NULL);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when the memory is almost full,
 * but there is just enough space left:
 *
 *  |---------------------------+---------|
 *  |          reserved         |   new   |
 *  +---------------------------+---------+
 *
 * Expect to allocate memory and merge all the regions. The total size field
 * gets updated.
 */
static int alloc_limited_space_generic_check(void)
{
	struct memblock_region *rgn = &memblock.reserved.regions[0];
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	phys_addr_t available_size = SZ_256;
	phys_addr_t reserved_size = MEM_SIZE - available_size;

	setup_memblock();

	/* Simulate almost-full memory */
	memblock_reserve(memblock_start_of_DRAM(), reserved_size);

	allocated_ptr = memblock_alloc(available_size, SMP_CACHE_BYTES);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, available_size);

	ASSERT_EQ(rgn->size, MEM_SIZE);
	ASSERT_EQ(rgn->base, memblock_start_of_DRAM());

	ASSERT_EQ(memblock.reserved.cnt, 1);
	ASSERT_EQ(memblock.reserved.total_size, MEM_SIZE);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when there is no available memory
 * registered (i.e. memblock.memory has only a dummy entry).
 * Expect no allocation to happen.
 */
static int alloc_no_memory_generic_check(void)
{
	struct memblock_region *rgn = &memblock.reserved.regions[0];
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	reset_memblock_regions();

	allocated_ptr = memblock_alloc(SZ_1K, SMP_CACHE_BYTES);

	ASSERT_EQ(allocated_ptr, NULL);
	ASSERT_EQ(rgn->size, 0);
	ASSERT_EQ(rgn->base, 0);
	ASSERT_EQ(memblock.reserved.total_size, 0);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate a region that is larger than the total size of
 * available memory (memblock.memory):
 *
 *  +-----------------------------------+
 *  |                 new               |
 *  +-----------------------------------+
 *  |                                 |
 *  |                                 |
 *  +---------------------------------+
 *
 * Expect no allocation to happen.
 */
static int alloc_too_large_generic_check(void)
{
	struct memblock_region *rgn = &memblock.reserved.regions[0];
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	setup_memblock();

	allocated_ptr = memblock_alloc(MEM_SIZE + SZ_2, SMP_CACHE_BYTES);

	ASSERT_EQ(allocated_ptr, NULL);
	ASSERT_EQ(rgn->size, 0);
	ASSERT_EQ(rgn->base, 0);
	ASSERT_EQ(memblock.reserved.total_size, 0);

	test_pass_pop();

	return 0;
}

/*
 * A simple test that tries to allocate a small memory region.
 * Expect to allocate an aligned region at the beginning of the available
 * memory.
 */
static int alloc_bottom_up_simple_check(void)
{
	struct memblock_region *rgn = &memblock.reserved.regions[0];
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	setup_memblock();

	allocated_ptr = memblock_alloc(SZ_2, SMP_CACHE_BYTES);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, SZ_2);

	ASSERT_EQ(rgn->size, SZ_2);
	ASSERT_EQ(rgn->base, memblock_start_of_DRAM());

	ASSERT_EQ(memblock.reserved.cnt, 1);
	ASSERT_EQ(memblock.reserved.total_size, SZ_2);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory next to a reserved region that starts at
 * the misaligned address. Expect to create two separate entries, with the new
 * entry aligned to the provided alignment:
 *
 *                      +
 *  |    +----------+   +----------+     |
 *  |    |   rgn1   |   |   rgn2   |     |
 *  +----+----------+---+----------+-----+
 *                      ^
 *                      |
 *                      Aligned address boundary
 *
 * The allocation direction is bottom-up, so the new region will be the second
 * entry in memory.reserved array. The previously reserved region does not get
 * modified. Region counter and total size get updated.
 */
static int alloc_bottom_up_disjoint_check(void)
{
	struct memblock_region *rgn1 = &memblock.reserved.regions[0];
	struct memblock_region *rgn2 = &memblock.reserved.regions[1];
	struct region r1;
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	phys_addr_t r2_size = SZ_16;
	/* Use custom alignment */
	phys_addr_t alignment = SMP_CACHE_BYTES * 2;
	phys_addr_t total_size;
	phys_addr_t expected_start;

	setup_memblock();

	r1.base = memblock_start_of_DRAM() + SZ_2;
	r1.size = SZ_2;

	total_size = r1.size + r2_size;
	expected_start = memblock_start_of_DRAM() + alignment;

	memblock_reserve(r1.base, r1.size);

	allocated_ptr = memblock_alloc(r2_size, alignment);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, r2_size);

	ASSERT_EQ(rgn1->size, r1.size);
	ASSERT_EQ(rgn1->base, r1.base);

	ASSERT_EQ(rgn2->size, r2_size);
	ASSERT_EQ(rgn2->base, expected_start);

	ASSERT_EQ(memblock.reserved.cnt, 2);
	ASSERT_EQ(memblock.reserved.total_size, total_size);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when there is enough space at
 * the beginning of the previously reserved block (i.e. first fit):
 *
 *  |------------------+--------+         |
 *  |        r1        |   r2   |         |
 *  +------------------+--------+---------+
 *
 * Expect a merge of both regions. Only the region size gets updated.
 */
static int alloc_bottom_up_before_check(void)
{
	struct memblock_region *rgn = &memblock.reserved.regions[0];
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	phys_addr_t r1_size = SZ_512;
	phys_addr_t r2_size = SZ_128;
	phys_addr_t total_size = r1_size + r2_size;

	setup_memblock();

	memblock_reserve(memblock_start_of_DRAM() + r1_size, r2_size);

	allocated_ptr = memblock_alloc(r1_size, SMP_CACHE_BYTES);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, r1_size);

	ASSERT_EQ(rgn->size, total_size);
	ASSERT_EQ(rgn->base, memblock_start_of_DRAM());

	ASSERT_EQ(memblock.reserved.cnt, 1);
	ASSERT_EQ(memblock.reserved.total_size, total_size);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when there is not enough space at
 * the beginning of the previously reserved block (i.e. second fit):
 *
 *  |    +--------+--------------+         |
 *  |    |   r1   |      r2      |         |
 *  +----+--------+--------------+---------+
 *
 * Expect a merge of both regions. Only the region size gets updated.
 */
static int alloc_bottom_up_after_check(void)
{
	struct memblock_region *rgn = &memblock.reserved.regions[0];
	struct region r1;
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	phys_addr_t r2_size = SZ_512;
	phys_addr_t total_size;

	setup_memblock();

	/*
	 * The first region starts at the aligned address to test region merging
	 */
	r1.base = memblock_start_of_DRAM() + SMP_CACHE_BYTES;
	r1.size = SZ_64;

	total_size = r1.size + r2_size;

	memblock_reserve(r1.base, r1.size);

	allocated_ptr = memblock_alloc(r2_size, SMP_CACHE_BYTES);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, r2_size);

	ASSERT_EQ(rgn->size, total_size);
	ASSERT_EQ(rgn->base, r1.base);

	ASSERT_EQ(memblock.reserved.cnt, 1);
	ASSERT_EQ(memblock.reserved.total_size, total_size);

	test_pass_pop();

	return 0;
}

/*
 * A test that tries to allocate memory when there are two reserved regions, the
 * first one starting at the beginning of the available memory, with a gap too
 * small to fit the new region:
 *
 *  |------------+     +--------+--------+  |
 *  |     r1     |     |   r2   |   r3   |  |
 *  +------------+-----+--------+--------+--+
 *
 * Expect to allocate after the second region, which starts at the higher
 * address, and merge them into one. The region counter and total size fields
 * get updated.
 */
static int alloc_bottom_up_second_fit_check(void)
{
	struct memblock_region *rgn  = &memblock.reserved.regions[1];
	struct region r1, r2;
	void *allocated_ptr = NULL;

	PREFIX_PUSH();

	phys_addr_t r3_size = SZ_1K;
	phys_addr_t total_size;

	setup_memblock();

	r1.base = memblock_start_of_DRAM();
	r1.size = SZ_512;

	r2.base = r1.base + r1.size + SZ_512;
	r2.size = SZ_256;

	total_size = r1.size + r2.size + r3_size;

	memblock_reserve(r1.base, r1.size);
	memblock_reserve(r2.base, r2.size);

	allocated_ptr = memblock_alloc(r3_size, SMP_CACHE_BYTES);

	ASSERT_NE(allocated_ptr, NULL);
	ASSERT_MEM_EQ(allocated_ptr, 0, r3_size);

	ASSERT_EQ(rgn->size, r2.size + r3_size);
	ASSERT_EQ(rgn->base, r2.base);

	ASSERT_EQ(memblock.reserved.cnt, 2);
	ASSERT_EQ(memblock.reserved.total_size, total_size);

	test_pass_pop();

	return 0;
}

/* Test case wrappers */
static int alloc_simple_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	memblock_set_bottom_up(false);
	alloc_top_down_simple_check();
	memblock_set_bottom_up(true);
	alloc_bottom_up_simple_check();

	return 0;
}

static int alloc_disjoint_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	memblock_set_bottom_up(false);
	alloc_top_down_disjoint_check();
	memblock_set_bottom_up(true);
	alloc_bottom_up_disjoint_check();

	return 0;
}

static int alloc_before_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	memblock_set_bottom_up(false);
	alloc_top_down_before_check();
	memblock_set_bottom_up(true);
	alloc_bottom_up_before_check();

	return 0;
}

static int alloc_after_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	memblock_set_bottom_up(false);
	alloc_top_down_after_check();
	memblock_set_bottom_up(true);
	alloc_bottom_up_after_check();

	return 0;
}

static int alloc_in_between_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	run_top_down(alloc_in_between_generic_check);
	run_bottom_up(alloc_in_between_generic_check);

	return 0;
}

static int alloc_second_fit_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	memblock_set_bottom_up(false);
	alloc_top_down_second_fit_check();
	memblock_set_bottom_up(true);
	alloc_bottom_up_second_fit_check();

	return 0;
}

static int alloc_small_gaps_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	run_top_down(alloc_small_gaps_generic_check);
	run_bottom_up(alloc_small_gaps_generic_check);

	return 0;
}

static int alloc_all_reserved_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	run_top_down(alloc_all_reserved_generic_check);
	run_bottom_up(alloc_all_reserved_generic_check);

	return 0;
}

static int alloc_no_space_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	run_top_down(alloc_no_space_generic_check);
	run_bottom_up(alloc_no_space_generic_check);

	return 0;
}

static int alloc_limited_space_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	run_top_down(alloc_limited_space_generic_check);
	run_bottom_up(alloc_limited_space_generic_check);

	return 0;
}

static int alloc_no_memory_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	run_top_down(alloc_no_memory_generic_check);
	run_bottom_up(alloc_no_memory_generic_check);

	return 0;
}

static int alloc_too_large_check(void)
{
	test_print("\tRunning %s...\n", __func__);
	run_top_down(alloc_too_large_generic_check);
	run_bottom_up(alloc_too_large_generic_check);

	return 0;
}

int memblock_alloc_checks(void)
{
	const char *func_testing = "memblock_alloc";

	prefix_reset();
	prefix_push(func_testing);
	test_print("Running %s tests...\n", func_testing);

	reset_memblock_attributes();
	dummy_physical_memory_init();

	alloc_simple_check();
	alloc_disjoint_check();
	alloc_before_check();
	alloc_after_check();
	alloc_second_fit_check();
	alloc_small_gaps_check();
	alloc_in_between_check();
	alloc_all_reserved_check();
	alloc_no_space_check();
	alloc_limited_space_check();
	alloc_no_memory_check();
	alloc_too_large_check();

	dummy_physical_memory_cleanup();

	prefix_pop();

	return 0;
}