blob: 3bf558f7f4f21712515eb60c3cda7f2e7371d583 [file] [log] [blame]
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018 Simon Goldschmidt
4 */
5
Sughosh Ganu291bf9c2024-08-26 17:29:18 +05306#include <alist.h>
Simon Glass75c4d412020-07-19 10:15:37 -06007#include <dm.h>
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +01008#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010011#include <dm/test.h>
Simon Glassb4c722a2023-10-01 19:15:21 -060012#include <test/lib.h>
Simon Glass75c4d412020-07-19 10:15:37 -060013#include <test/test.h>
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010014#include <test/ut.h>
15
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053016static inline bool lmb_is_nomap(struct lmb_region *m)
Heinrich Schuchardta88181e2021-11-14 08:41:07 +010017{
18 return m->flags & LMB_NOMAP;
19}
20
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053021static int check_lmb(struct unit_test_state *uts, struct alist *mem_lst,
22 struct alist *used_lst, phys_addr_t ram_base,
23 phys_size_t ram_size, unsigned long num_reserved,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010024 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)
27{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053028 struct lmb_region *mem, *used;
29
30 mem = mem_lst->data;
31 used = used_lst->data;
32
Simon Goldschmidtc722dac2019-02-01 21:23:59 +010033 if (ram_size) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053034 ut_asserteq(mem_lst->count, 1);
35 ut_asserteq(mem[0].base, ram_base);
36 ut_asserteq(mem[0].size, ram_size);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +010037 }
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010038
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053039 ut_asserteq(used_lst->count, num_reserved);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010040 if (num_reserved > 0) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053041 ut_asserteq(used[0].base, base1);
42 ut_asserteq(used[0].size, size1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010043 }
44 if (num_reserved > 1) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053045 ut_asserteq(used[1].base, base2);
46 ut_asserteq(used[1].size, size2);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010047 }
48 if (num_reserved > 2) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053049 ut_asserteq(used[2].base, base3);
50 ut_asserteq(used[2].size, size3);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010051 }
52 return 0;
53}
54
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053055#define ASSERT_LMB(mem_lst, used_lst, ram_base, ram_size, num_reserved, base1, size1, \
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010056 base2, size2, base3, size3) \
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053057 ut_assert(!check_lmb(uts, mem_lst, used_lst, ram_base, ram_size, \
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010058 num_reserved, base1, size1, base2, size2, base3, \
59 size3))
60
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053061static int setup_lmb_test(struct unit_test_state *uts, struct lmb *store,
62 struct alist **mem_lstp, struct alist **used_lstp)
63{
64 struct lmb *lmb;
65
66 ut_assertok(lmb_push(store));
67 lmb = lmb_get();
Ilias Apalodimas5421c332024-12-18 09:02:33 +020068 *mem_lstp = &lmb->available_mem;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053069 *used_lstp = &lmb->used_mem;
70
71 return 0;
72}
73
Simon Goldschmidtc722dac2019-02-01 21:23:59 +010074static int test_multi_alloc(struct unit_test_state *uts, const phys_addr_t ram,
75 const phys_size_t ram_size, const phys_addr_t ram0,
76 const phys_size_t ram0_size,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010077 const phys_addr_t alloc_64k_addr)
78{
79 const phys_addr_t ram_end = ram + ram_size;
80 const phys_addr_t alloc_64k_end = alloc_64k_addr + 0x10000;
81
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010082 long ret;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053083 struct alist *mem_lst, *used_lst;
84 struct lmb_region *mem, *used;
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010085 phys_addr_t a, a2, b, b2, c, d;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053086 struct lmb store;
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010087
88 /* check for overflow */
89 ut_assert(ram_end == 0 || ram_end > ram);
90 ut_assert(alloc_64k_end > alloc_64k_addr);
91 /* check input addresses + size */
92 ut_assert(alloc_64k_addr >= ram + 8);
93 ut_assert(alloc_64k_end <= ram_end - 8);
94
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053095 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
96 mem = mem_lst->data;
97 used = used_lst->data;
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +010098
Simon Goldschmidtc722dac2019-02-01 21:23:59 +010099 if (ram0_size) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530100 ret = lmb_add(ram0, ram0_size);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100101 ut_asserteq(ret, 0);
102 }
103
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530104 ret = lmb_add(ram, ram_size);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100105 ut_asserteq(ret, 0);
106
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100107 if (ram0_size) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530108 ut_asserteq(mem_lst->count, 2);
109 ut_asserteq(mem[0].base, ram0);
110 ut_asserteq(mem[0].size, ram0_size);
111 ut_asserteq(mem[1].base, ram);
112 ut_asserteq(mem[1].size, ram_size);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100113 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530114 ut_asserteq(mem_lst->count, 1);
115 ut_asserteq(mem[0].base, ram);
116 ut_asserteq(mem[0].size, ram_size);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100117 }
118
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100119 /* reserve 64KiB somewhere */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200120 ret = lmb_reserve(alloc_64k_addr, 0x10000, LMB_NONE);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100121 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530122 ASSERT_LMB(mem_lst, used_lst, 0, 0, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100123 0, 0, 0, 0);
124
125 /* allocate somewhere, should be at the end of RAM */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530126 a = lmb_alloc(4, 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100127 ut_asserteq(a, ram_end - 4);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530128 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, alloc_64k_addr, 0x10000,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100129 ram_end - 4, 4, 0, 0);
130 /* alloc below end of reserved region -> below reserved region */
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200131 b = lmb_alloc_base(4, 1, alloc_64k_end, LMB_NONE);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100132 ut_asserteq(b, alloc_64k_addr - 4);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530133 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100134 alloc_64k_addr - 4, 0x10000 + 4, ram_end - 4, 4, 0, 0);
135
136 /* 2nd time */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530137 c = lmb_alloc(4, 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100138 ut_asserteq(c, ram_end - 8);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530139 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100140 alloc_64k_addr - 4, 0x10000 + 4, ram_end - 8, 8, 0, 0);
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200141 d = lmb_alloc_base(4, 1, alloc_64k_end, LMB_NONE);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100142 ut_asserteq(d, alloc_64k_addr - 8);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530143 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100144 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);
145
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530146 ret = lmb_free(a, 4);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100147 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530148 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100149 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
150 /* allocate again to ensure we get the same address */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530151 a2 = lmb_alloc(4, 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100152 ut_asserteq(a, a2);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530153 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100154 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530155 ret = lmb_free(a2, 4);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100156 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530157 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100158 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
159
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530160 ret = lmb_free(b, 4);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100161 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530162 ASSERT_LMB(mem_lst, used_lst, 0, 0, 3,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100163 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000,
164 ram_end - 8, 4);
165 /* allocate again to ensure we get the same address */
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200166 b2 = lmb_alloc_base(4, 1, alloc_64k_end, LMB_NONE);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100167 ut_asserteq(b, b2);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530168 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100169 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530170 ret = lmb_free(b2, 4);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100171 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530172 ASSERT_LMB(mem_lst, used_lst, 0, 0, 3,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100173 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000,
174 ram_end - 8, 4);
175
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530176 ret = lmb_free(c, 4);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100177 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530178 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100179 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, 0, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530180 ret = lmb_free(d, 4);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100181 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530182 ASSERT_LMB(mem_lst, used_lst, 0, 0, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100183 0, 0, 0, 0);
184
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100185 if (ram0_size) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530186 ut_asserteq(mem_lst->count, 2);
187 ut_asserteq(mem[0].base, ram0);
188 ut_asserteq(mem[0].size, ram0_size);
189 ut_asserteq(mem[1].base, ram);
190 ut_asserteq(mem[1].size, ram_size);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100191 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530192 ut_asserteq(mem_lst->count, 1);
193 ut_asserteq(mem[0].base, ram);
194 ut_asserteq(mem[0].size, ram_size);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100195 }
196
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530197 lmb_pop(&store);
198
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100199 return 0;
200}
201
202static int test_multi_alloc_512mb(struct unit_test_state *uts,
203 const phys_addr_t ram)
204{
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100205 return test_multi_alloc(uts, ram, 0x20000000, 0, 0, ram + 0x10000000);
206}
207
208static int test_multi_alloc_512mb_x2(struct unit_test_state *uts,
209 const phys_addr_t ram,
210 const phys_addr_t ram0)
211{
212 return test_multi_alloc(uts, ram, 0x20000000, ram0, 0x20000000,
213 ram + 0x10000000);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100214}
215
216/* Create a memory region with one reserved region and allocate */
217static int lib_test_lmb_simple(struct unit_test_state *uts)
218{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100219 int ret;
220
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100221 /* simulate 512 MiB RAM beginning at 1GiB */
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100222 ret = test_multi_alloc_512mb(uts, 0x40000000);
223 if (ret)
224 return ret;
225
226 /* simulate 512 MiB RAM beginning at 1.5GiB */
227 return test_multi_alloc_512mb(uts, 0xE0000000);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100228}
Simon Glassb4c722a2023-10-01 19:15:21 -0600229LIB_TEST(lib_test_lmb_simple, 0);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100230
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100231/* Create two memory regions with one reserved region and allocate */
232static int lib_test_lmb_simple_x2(struct unit_test_state *uts)
233{
234 int ret;
235
236 /* simulate 512 MiB RAM beginning at 2GiB and 1 GiB */
237 ret = test_multi_alloc_512mb_x2(uts, 0x80000000, 0x40000000);
238 if (ret)
239 return ret;
240
241 /* simulate 512 MiB RAM beginning at 3.5GiB and 1 GiB */
242 return test_multi_alloc_512mb_x2(uts, 0xE0000000, 0x40000000);
243}
Simon Glassb4c722a2023-10-01 19:15:21 -0600244LIB_TEST(lib_test_lmb_simple_x2, 0);
Simon Goldschmidtc722dac2019-02-01 21:23:59 +0100245
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100246/* Simulate 512 MiB RAM, allocate some blocks that fit/don't fit */
247static int test_bigblock(struct unit_test_state *uts, const phys_addr_t ram)
248{
249 const phys_size_t ram_size = 0x20000000;
250 const phys_size_t big_block_size = 0x10000000;
251 const phys_addr_t ram_end = ram + ram_size;
252 const phys_addr_t alloc_64k_addr = ram + 0x10000000;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530253 struct alist *mem_lst, *used_lst;
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100254 long ret;
255 phys_addr_t a, b;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530256 struct lmb store;
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100257
258 /* check for overflow */
259 ut_assert(ram_end == 0 || ram_end > ram);
260
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530261 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100262
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530263 ret = lmb_add(ram, ram_size);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100264 ut_asserteq(ret, 0);
265
266 /* reserve 64KiB in the middle of RAM */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200267 ret = lmb_reserve(alloc_64k_addr, 0x10000, LMB_NONE);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100268 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530269 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100270 0, 0, 0, 0);
271
272 /* allocate a big block, should be below reserved */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530273 a = lmb_alloc(big_block_size, 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100274 ut_asserteq(a, ram);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530275 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100276 big_block_size + 0x10000, 0, 0, 0, 0);
277 /* allocate 2nd big block */
278 /* This should fail, printing an error */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530279 b = lmb_alloc(big_block_size, 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100280 ut_asserteq(b, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530281 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100282 big_block_size + 0x10000, 0, 0, 0, 0);
283
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530284 ret = lmb_free(a, big_block_size);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100285 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530286 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100287 0, 0, 0, 0);
288
289 /* allocate too big block */
290 /* This should fail, printing an error */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530291 a = lmb_alloc(ram_size, 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100292 ut_asserteq(a, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530293 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100294 0, 0, 0, 0);
295
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530296 lmb_pop(&store);
297
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100298 return 0;
299}
300
301static int lib_test_lmb_big(struct unit_test_state *uts)
302{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100303 int ret;
304
305 /* simulate 512 MiB RAM beginning at 1GiB */
306 ret = test_bigblock(uts, 0x40000000);
307 if (ret)
308 return ret;
309
310 /* simulate 512 MiB RAM beginning at 1.5GiB */
311 return test_bigblock(uts, 0xE0000000);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100312}
Simon Glassb4c722a2023-10-01 19:15:21 -0600313LIB_TEST(lib_test_lmb_big, 0);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100314
315/* Simulate 512 MiB RAM, allocate a block without previous reservation */
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100316static int test_noreserved(struct unit_test_state *uts, const phys_addr_t ram,
317 const phys_addr_t alloc_size, const ulong align)
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100318{
319 const phys_size_t ram_size = 0x20000000;
320 const phys_addr_t ram_end = ram + ram_size;
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100321 long ret;
322 phys_addr_t a, b;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530323 struct lmb store;
324 struct alist *mem_lst, *used_lst;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100325 const phys_addr_t alloc_size_aligned = (alloc_size + align - 1) &
326 ~(align - 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100327
328 /* check for overflow */
329 ut_assert(ram_end == 0 || ram_end > ram);
330
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530331 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100332
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530333 ret = lmb_add(ram, ram_size);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100334 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530335 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100336
337 /* allocate a block */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530338 a = lmb_alloc(alloc_size, align);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100339 ut_assert(a != 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530340 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1,
341 ram + ram_size - alloc_size_aligned, alloc_size, 0, 0, 0, 0);
342
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100343 /* allocate another block */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530344 b = lmb_alloc(alloc_size, align);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100345 ut_assert(b != 0);
346 if (alloc_size == alloc_size_aligned) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530347 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram + ram_size -
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100348 (alloc_size_aligned * 2), alloc_size * 2, 0, 0, 0,
349 0);
350 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530351 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram + ram_size -
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100352 (alloc_size_aligned * 2), alloc_size, ram + ram_size
353 - alloc_size_aligned, alloc_size, 0, 0);
354 }
355 /* and free them */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530356 ret = lmb_free(b, alloc_size);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100357 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530358 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1,
359 ram + ram_size - alloc_size_aligned,
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100360 alloc_size, 0, 0, 0, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530361 ret = lmb_free(a, alloc_size);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100362 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530363 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100364
365 /* allocate a block with base*/
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200366 b = lmb_alloc_base(alloc_size, align, ram_end, LMB_NONE);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100367 ut_assert(a == b);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530368 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1,
369 ram + ram_size - alloc_size_aligned,
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100370 alloc_size, 0, 0, 0, 0);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100371 /* and free it */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530372 ret = lmb_free(b, alloc_size);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100373 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530374 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
375
376 lmb_pop(&store);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100377
378 return 0;
379}
380
381static int lib_test_lmb_noreserved(struct unit_test_state *uts)
382{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100383 int ret;
384
385 /* simulate 512 MiB RAM beginning at 1GiB */
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100386 ret = test_noreserved(uts, 0x40000000, 4, 1);
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100387 if (ret)
388 return ret;
389
390 /* simulate 512 MiB RAM beginning at 1.5GiB */
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100391 return test_noreserved(uts, 0xE0000000, 4, 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100392}
Simon Glassb4c722a2023-10-01 19:15:21 -0600393LIB_TEST(lib_test_lmb_noreserved, 0);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100394
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100395static int lib_test_lmb_unaligned_size(struct unit_test_state *uts)
396{
397 int ret;
398
399 /* simulate 512 MiB RAM beginning at 1GiB */
400 ret = test_noreserved(uts, 0x40000000, 5, 8);
401 if (ret)
402 return ret;
403
404 /* simulate 512 MiB RAM beginning at 1.5GiB */
405 return test_noreserved(uts, 0xE0000000, 5, 8);
406}
Simon Glassb4c722a2023-10-01 19:15:21 -0600407LIB_TEST(lib_test_lmb_unaligned_size, 0);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100408
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100409/*
410 * Simulate a RAM that starts at 0 and allocate down to address 0, which must
411 * fail as '0' means failure for the lmb_alloc functions.
412 */
413static int lib_test_lmb_at_0(struct unit_test_state *uts)
414{
415 const phys_addr_t ram = 0;
416 const phys_size_t ram_size = 0x20000000;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530417 struct lmb store;
418 struct alist *mem_lst, *used_lst;
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100419 long ret;
420 phys_addr_t a, b;
421
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530422 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100423
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530424 ret = lmb_add(ram, ram_size);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100425 ut_asserteq(ret, 0);
426
427 /* allocate nearly everything */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530428 a = lmb_alloc(ram_size - 4, 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100429 ut_asserteq(a, ram + 4);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530430 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100431 0, 0, 0, 0);
432 /* allocate the rest */
433 /* This should fail as the allocated address would be 0 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530434 b = lmb_alloc(4, 1);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100435 ut_asserteq(b, 0);
436 /* check that this was an error by checking lmb */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530437 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100438 0, 0, 0, 0);
439 /* check that this was an error by freeing b */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530440 ret = lmb_free(b, 4);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100441 ut_asserteq(ret, -1);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530442 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4,
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100443 0, 0, 0, 0);
444
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530445 ret = lmb_free(a, ram_size - 4);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100446 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530447 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
448
449 lmb_pop(&store);
Simon Goldschmidt9f3b6272019-01-14 22:38:14 +0100450
451 return 0;
452}
Simon Glassb4c722a2023-10-01 19:15:21 -0600453LIB_TEST(lib_test_lmb_at_0, 0);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100454
455/* Check that calling lmb_reserve with overlapping regions fails. */
456static int lib_test_lmb_overlapping_reserve(struct unit_test_state *uts)
457{
458 const phys_addr_t ram = 0x40000000;
459 const phys_size_t ram_size = 0x20000000;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530460 struct lmb store;
461 struct alist *mem_lst, *used_lst;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100462 long ret;
463
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530464 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100465
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530466 ret = lmb_add(ram, ram_size);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100467 ut_asserteq(ret, 0);
468
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200469 ret = lmb_reserve(0x40010000, 0x10000, LMB_NONE);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100470 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530471 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100472 0, 0, 0, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530473
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530474 /* allocate overlapping region */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200475 ret = lmb_reserve(0x40011000, 0x10000, LMB_NONE);
Ilias Apalodimas98491252024-10-23 18:22:00 +0300476 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530477 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x11000,
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100478 0, 0, 0, 0);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530479 /* allocate 2nd region */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200480 ret = lmb_reserve(0x40030000, 0x10000, LMB_NONE);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100481 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530482 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40010000, 0x11000,
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100483 0x40030000, 0x10000, 0, 0);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530484 /* allocate 3rd region , This should coalesce all regions into one */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200485 ret = lmb_reserve(0x40020000, 0x10000, LMB_NONE);
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100486 ut_assert(ret >= 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530487 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x30000,
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100488 0, 0, 0, 0);
489
Udit Kumar27575252023-09-26 16:54:43 +0530490 /* allocate 2nd region, which should be added as first region */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200491 ret = lmb_reserve(0x40000000, 0x8000, LMB_NONE);
Udit Kumar27575252023-09-26 16:54:43 +0530492 ut_assert(ret >= 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530493 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x8000,
Udit Kumar27575252023-09-26 16:54:43 +0530494 0x40010000, 0x30000, 0, 0);
495
496 /* allocate 3rd region, coalesce with first and overlap with second */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200497 ret = lmb_reserve(0x40008000, 0x10000, LMB_NONE);
Udit Kumar27575252023-09-26 16:54:43 +0530498 ut_assert(ret >= 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530499 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40000000, 0x40000,
Udit Kumar27575252023-09-26 16:54:43 +0530500 0, 0, 0, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530501
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530502 /* try to allocate overlapping region with a different flag, should fail */
503 ret = lmb_reserve(0x40008000, 0x1000, LMB_NOOVERWRITE);
504 ut_asserteq(ret, -EEXIST);
505
506 /* allocate another region at 0x40050000 with a different flag */
507 ret = lmb_reserve(0x40050000, 0x10000, LMB_NOOVERWRITE);
508 ut_asserteq(ret, 0);
509 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x40000,
510 0x40050000, 0x10000, 0, 0);
511
512 /*
513 * try to reserve a region adjacent to region 1 overlapping the 2nd region,
514 * should fail
515 */
516 ret = lmb_reserve(0x40040000, 0x20000, LMB_NONE);
517 ut_asserteq(ret, -EEXIST);
518
519 /*
520 * try to reserve a region between the two regions, but without an overlap,
521 * should succeed. this added region coalesces with the region 1
522 */
523 ret = lmb_reserve(0x40040000, 0x10000, LMB_NONE);
524 ut_asserteq(ret, 0);
525 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x50000,
526 0x40050000, 0x10000, 0, 0);
527
528 /*
529 * try to reserve a region which overlaps with both the regions,
530 * should fail as the flags do not match
531 */
532 ret = lmb_reserve(0x40020000, 0x80000, LMB_NONE);
533 ut_asserteq(ret, -EEXIST);
534 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x50000,
535 0x40050000, 0x10000, 0, 0);
536
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530537 lmb_pop(&store);
538
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100539 return 0;
540}
Simon Glassb4c722a2023-10-01 19:15:21 -0600541LIB_TEST(lib_test_lmb_overlapping_reserve, 0);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100542
543/*
544 * Simulate 512 MiB RAM, reserve 3 blocks, allocate addresses in between.
545 * Expect addresses outside the memory range to fail.
546 */
547static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
548{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530549 struct lmb store;
550 struct alist *mem_lst, *used_lst;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100551 const phys_size_t ram_size = 0x20000000;
552 const phys_addr_t ram_end = ram + ram_size;
553 const phys_size_t alloc_addr_a = ram + 0x8000000;
554 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
555 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100556 long ret;
557 phys_addr_t a, b, c, d, e;
558
559 /* check for overflow */
560 ut_assert(ram_end == 0 || ram_end > ram);
561
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530562 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100563
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530564 ret = lmb_add(ram, ram_size);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100565 ut_asserteq(ret, 0);
566
Ilias Apalodimas5aaeb0e2024-12-02 16:42:45 +0200567 /* Try to allocate a page twice */
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200568 b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE);
Ilias Apalodimas5aaeb0e2024-12-02 16:42:45 +0200569 ut_asserteq(b, 0);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200570 b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE);
571 ut_asserteq(b, -1);
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200572 b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200573 ut_asserteq(b, 0);
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200574 b = lmb_alloc_addr(alloc_addr_a, 0x2000, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200575 ut_asserteq(b, 0);
Ilias Apalodimas5aaeb0e2024-12-02 16:42:45 +0200576 ret = lmb_free(alloc_addr_a, 0x2000);
577 ut_asserteq(ret, 0);
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200578 b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE);
Ilias Apalodimas5aaeb0e2024-12-02 16:42:45 +0200579 ut_asserteq(b, 0);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200580 b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE);
581 ut_asserteq(b, -1);
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200582 b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200583 ut_asserteq(b, -1);
Ilias Apalodimas5aaeb0e2024-12-02 16:42:45 +0200584 ret = lmb_free(alloc_addr_a, 0x1000);
585 ut_asserteq(ret, 0);
586
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530587 /*
588 * Add two regions with different flags, region1 and region2 with
589 * a gap between them.
590 * Try adding another region, adjacent to region 1 and overlapping
591 * region 2. Should fail.
592 */
593 a = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE);
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530594 ut_asserteq(a, 0);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530595
596 b = lmb_alloc_addr(alloc_addr_a + 0x4000, 0x1000, LMB_NOOVERWRITE);
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530597 ut_asserteq(b, 0);
598 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, alloc_addr_a, 0x1000,
599 alloc_addr_a + 0x4000, 0x1000, 0, 0);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530600
601 c = lmb_alloc_addr(alloc_addr_a + 0x1000, 0x5000, LMB_NONE);
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530602 ut_asserteq(c, -1);
603 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, alloc_addr_a, 0x1000,
604 alloc_addr_a + 0x4000, 0x1000, 0, 0);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530605
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530606 ret = lmb_free(alloc_addr_a, 0x1000);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530607 ut_asserteq(ret, 0);
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530608 ret = lmb_free(alloc_addr_a + 0x4000, 0x1000);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530609 ut_asserteq(ret, 0);
610
611 /*
612 * Add two regions with same flags(LMB_NONE), region1 and region2
613 * with a gap between them.
614 * Try adding another region, adjacent to region 1 and overlapping
615 * region 2. Should succeed. All regions should coalesce into a
616 * single region.
617 */
618 a = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE);
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530619 ut_asserteq(a, 0);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530620
621 b = lmb_alloc_addr(alloc_addr_a + 0x4000, 0x1000, LMB_NONE);
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530622 ut_asserteq(b, 0);
623 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, alloc_addr_a, 0x1000,
624 alloc_addr_a + 0x4000, 0x1000, 0, 0);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530625
626 c = lmb_alloc_addr(alloc_addr_a + 0x1000, 0x5000, LMB_NONE);
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530627 ut_asserteq(c, 0);
628 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_addr_a, 0x6000,
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530629 0, 0, 0, 0);
630
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530631 ret = lmb_free(alloc_addr_a, 0x6000);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530632 ut_asserteq(ret, 0);
633
634 /*
635 * Add two regions with same flags(LMB_NOOVERWRITE), region1 and
636 * region2 with a gap between them.
637 * Try adding another region, adjacent to region 1 and overlapping
638 * region 2. Should fail.
639 */
640 a = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE);
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530641 ut_asserteq(a, 0);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530642
643 b = lmb_alloc_addr(alloc_addr_a + 0x4000, 0x1000, LMB_NOOVERWRITE);
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530644 ut_asserteq(b, 0);
645 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, alloc_addr_a, 0x1000,
646 alloc_addr_a + 0x4000, 0x1000, 0, 0);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530647
648 c = lmb_alloc_addr(alloc_addr_a + 0x1000, 0x5000, LMB_NOOVERWRITE);
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530649 ut_asserteq(c, -1);
650 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, alloc_addr_a, 0x1000,
651 alloc_addr_a + 0x4000, 0x1000, 0, 0);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530652
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530653 ret = lmb_free(alloc_addr_a, 0x1000);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530654 ut_asserteq(ret, 0);
Sughosh Ganu2bbfa552025-03-26 22:23:36 +0530655 ret = lmb_free(alloc_addr_a + 0x4000, 0x1000);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530656 ut_asserteq(ret, 0);
657
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100658 /* reserve 3 blocks */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200659 ret = lmb_reserve(alloc_addr_a, 0x10000, LMB_NONE);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100660 ut_asserteq(ret, 0);
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200661 ret = lmb_reserve(alloc_addr_b, 0x10000, LMB_NONE);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100662 ut_asserteq(ret, 0);
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200663 ret = lmb_reserve(alloc_addr_c, 0x10000, LMB_NONE);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100664 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530665 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, alloc_addr_a, 0x10000,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100666 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
667
668 /* allocate blocks */
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200669 a = lmb_alloc_addr(ram, alloc_addr_a - ram, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200670 ut_asserteq(a, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530671 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, ram, 0x8010000,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100672 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530673 b = lmb_alloc_addr(alloc_addr_a + 0x10000,
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200674 alloc_addr_b - alloc_addr_a - 0x10000, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200675 ut_asserteq(b, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530676 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x10010000,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100677 alloc_addr_c, 0x10000, 0, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530678 c = lmb_alloc_addr(alloc_addr_b + 0x10000,
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200679 alloc_addr_c - alloc_addr_b - 0x10000, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200680 ut_asserteq(c, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530681 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100682 0, 0, 0, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530683 d = lmb_alloc_addr(alloc_addr_c + 0x10000,
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200684 ram_end - alloc_addr_c - 0x10000, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200685 ut_asserteq(d, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530686 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, ram_size,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100687 0, 0, 0, 0);
688
689 /* allocating anything else should fail */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530690 e = lmb_alloc(1, 1);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100691 ut_asserteq(e, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530692 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, ram_size,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100693 0, 0, 0, 0);
694
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200695 /* free thge allocation from d */
696 ret = lmb_free(alloc_addr_c + 0x10000, ram_end - alloc_addr_c - 0x10000);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100697 ut_asserteq(ret, 0);
698
699 /* allocate at 3 points in free range */
700
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200701 d = lmb_alloc_addr(ram_end - 4, 4, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200702 ut_asserteq(d, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530703 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000,
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200704 ram_end - 4, 4, 0, 0);
705 ret = lmb_free(ram_end - 4, 4);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100706 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530707 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100708 0, 0, 0, 0);
709
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200710 d = lmb_alloc_addr(ram_end - 128, 4, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200711 ut_asserteq(d, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530712 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000,
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200713 ram_end - 128, 4, 0, 0);
714 ret = lmb_free(ram_end - 128, 4);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100715 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530716 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100717 0, 0, 0, 0);
718
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200719 d = lmb_alloc_addr(alloc_addr_c + 0x10000, 4, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200720 ut_asserteq(d, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530721 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010004,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100722 0, 0, 0, 0);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200723 ret = lmb_free(alloc_addr_c + 0x10000, 4);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100724 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530725 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100726 0, 0, 0, 0);
727
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200728 /* allocate at the bottom a was assigned to ram at the top */
729 ret = lmb_free(ram, alloc_addr_a - ram);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100730 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530731 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram + 0x8000000,
732 0x10010000, 0, 0, 0, 0);
733
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200734 d = lmb_alloc_addr(ram, 4, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200735 ut_asserteq(d, 0);
736 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 4,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100737 ram + 0x8000000, 0x10010000, 0, 0);
738
739 /* check that allocating outside memory fails */
740 if (ram_end != 0) {
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200741 ret = lmb_alloc_addr(ram_end, 1, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200742 ut_asserteq(ret, -1);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100743 }
744 if (ram != 0) {
Ilias Apalodimascc2ed3d2024-12-18 09:02:35 +0200745 ret = lmb_alloc_addr(ram - 1, 1, LMB_NONE);
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200746 ut_asserteq(ret, -1);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100747 }
748
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530749 lmb_pop(&store);
750
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100751 return 0;
752}
753
754static int lib_test_lmb_alloc_addr(struct unit_test_state *uts)
755{
756 int ret;
757
758 /* simulate 512 MiB RAM beginning at 1GiB */
759 ret = test_alloc_addr(uts, 0x40000000);
760 if (ret)
761 return ret;
762
763 /* simulate 512 MiB RAM beginning at 1.5GiB */
764 return test_alloc_addr(uts, 0xE0000000);
765}
Simon Glassb4c722a2023-10-01 19:15:21 -0600766LIB_TEST(lib_test_lmb_alloc_addr, 0);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100767
768/* Simulate 512 MiB RAM, reserve 3 blocks, check addresses in between */
769static int test_get_unreserved_size(struct unit_test_state *uts,
770 const phys_addr_t ram)
771{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530772 struct lmb store;
773 struct alist *mem_lst, *used_lst;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100774 const phys_size_t ram_size = 0x20000000;
775 const phys_addr_t ram_end = ram + ram_size;
776 const phys_size_t alloc_addr_a = ram + 0x8000000;
777 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
778 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100779 long ret;
780 phys_size_t s;
781
782 /* check for overflow */
783 ut_assert(ram_end == 0 || ram_end > ram);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530784 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100785
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530786 ret = lmb_add(ram, ram_size);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100787 ut_asserteq(ret, 0);
788
789 /* reserve 3 blocks */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200790 ret = lmb_reserve(alloc_addr_a, 0x10000, LMB_NONE);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100791 ut_asserteq(ret, 0);
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200792 ret = lmb_reserve(alloc_addr_b, 0x10000, LMB_NONE);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100793 ut_asserteq(ret, 0);
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200794 ret = lmb_reserve(alloc_addr_c, 0x10000, LMB_NONE);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100795 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530796 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, alloc_addr_a, 0x10000,
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100797 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
798
799 /* check addresses in between blocks */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530800 s = lmb_get_free_size(ram);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100801 ut_asserteq(s, alloc_addr_a - ram);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530802 s = lmb_get_free_size(ram + 0x10000);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100803 ut_asserteq(s, alloc_addr_a - ram - 0x10000);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530804 s = lmb_get_free_size(alloc_addr_a - 4);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100805 ut_asserteq(s, 4);
806
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530807 s = lmb_get_free_size(alloc_addr_a + 0x10000);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100808 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x10000);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530809 s = lmb_get_free_size(alloc_addr_a + 0x20000);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100810 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x20000);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530811 s = lmb_get_free_size(alloc_addr_b - 4);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100812 ut_asserteq(s, 4);
813
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530814 s = lmb_get_free_size(alloc_addr_c + 0x10000);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100815 ut_asserteq(s, ram_end - alloc_addr_c - 0x10000);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530816 s = lmb_get_free_size(alloc_addr_c + 0x20000);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100817 ut_asserteq(s, ram_end - alloc_addr_c - 0x20000);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530818 s = lmb_get_free_size(ram_end - 4);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100819 ut_asserteq(s, 4);
820
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530821 lmb_pop(&store);
822
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100823 return 0;
824}
825
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100826static int lib_test_lmb_get_free_size(struct unit_test_state *uts)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100827{
828 int ret;
829
830 /* simulate 512 MiB RAM beginning at 1GiB */
831 ret = test_get_unreserved_size(uts, 0x40000000);
832 if (ret)
833 return ret;
834
835 /* simulate 512 MiB RAM beginning at 1.5GiB */
836 return test_get_unreserved_size(uts, 0xE0000000);
837}
Simon Glassb4c722a2023-10-01 19:15:21 -0600838LIB_TEST(lib_test_lmb_get_free_size, 0);
Patrick Delaunay1fe3adc2021-03-10 10:16:30 +0100839
Patrick Delaunaya1860722021-05-07 14:50:32 +0200840static int lib_test_lmb_flags(struct unit_test_state *uts)
841{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530842 struct lmb store;
843 struct lmb_region *mem, *used;
844 struct alist *mem_lst, *used_lst;
Patrick Delaunaya1860722021-05-07 14:50:32 +0200845 const phys_addr_t ram = 0x40000000;
846 const phys_size_t ram_size = 0x20000000;
Patrick Delaunaya1860722021-05-07 14:50:32 +0200847 long ret;
848
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530849 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
850 mem = mem_lst->data;
851 used = used_lst->data;
Patrick Delaunaya1860722021-05-07 14:50:32 +0200852
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530853 ret = lmb_add(ram, ram_size);
Patrick Delaunaya1860722021-05-07 14:50:32 +0200854 ut_asserteq(ret, 0);
855
856 /* reserve, same flag */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200857 ret = lmb_reserve(0x40010000, 0x10000, LMB_NOMAP);
Patrick Delaunaya1860722021-05-07 14:50:32 +0200858 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530859 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Patrick Delaunaya1860722021-05-07 14:50:32 +0200860 0, 0, 0, 0);
861
862 /* reserve again, same flag */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200863 ret = lmb_reserve(0x40010000, 0x10000, LMB_NOMAP);
Sam Protsenkob4f81102024-12-10 20:17:01 -0600864 ut_asserteq(ret, -EEXIST);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530865 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Patrick Delaunaya1860722021-05-07 14:50:32 +0200866 0, 0, 0, 0);
867
868 /* reserve again, new flag */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200869 ret = lmb_reserve(0x40010000, 0x10000, LMB_NONE);
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530870 ut_asserteq(ret, -EEXIST);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530871 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Patrick Delaunaya1860722021-05-07 14:50:32 +0200872 0, 0, 0, 0);
873
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530874 ut_asserteq(lmb_is_nomap(&used[0]), 1);
Patrick Delaunaya1860722021-05-07 14:50:32 +0200875
876 /* merge after */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200877 ret = lmb_reserve(0x40020000, 0x10000, LMB_NOMAP);
Ilias Apalodimas98491252024-10-23 18:22:00 +0300878 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530879 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x20000,
Patrick Delaunaya1860722021-05-07 14:50:32 +0200880 0, 0, 0, 0);
881
882 /* merge before */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200883 ret = lmb_reserve(0x40000000, 0x10000, LMB_NOMAP);
Ilias Apalodimas98491252024-10-23 18:22:00 +0300884 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530885 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40000000, 0x30000,
Patrick Delaunaya1860722021-05-07 14:50:32 +0200886 0, 0, 0, 0);
887
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530888 ut_asserteq(lmb_is_nomap(&used[0]), 1);
Patrick Delaunaya1860722021-05-07 14:50:32 +0200889
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200890 ret = lmb_reserve(0x40030000, 0x10000, LMB_NONE);
Patrick Delaunaya1860722021-05-07 14:50:32 +0200891 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530892 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x30000,
Patrick Delaunaya1860722021-05-07 14:50:32 +0200893 0x40030000, 0x10000, 0, 0);
894
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530895 ut_asserteq(lmb_is_nomap(&used[0]), 1);
896 ut_asserteq(lmb_is_nomap(&used[1]), 0);
Patrick Delaunaya1860722021-05-07 14:50:32 +0200897
898 /* test that old API use LMB_NONE */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200899 ret = lmb_reserve(0x40040000, 0x10000, LMB_NONE);
Ilias Apalodimas98491252024-10-23 18:22:00 +0300900 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530901 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x30000,
Patrick Delaunaya1860722021-05-07 14:50:32 +0200902 0x40030000, 0x20000, 0, 0);
903
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530904 ut_asserteq(lmb_is_nomap(&used[0]), 1);
905 ut_asserteq(lmb_is_nomap(&used[1]), 0);
Patrick Delaunaya1860722021-05-07 14:50:32 +0200906
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200907 ret = lmb_reserve(0x40070000, 0x10000, LMB_NOMAP);
Patrick Delaunaya1860722021-05-07 14:50:32 +0200908 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530909 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, 0x40000000, 0x30000,
Patrick Delaunaya1860722021-05-07 14:50:32 +0200910 0x40030000, 0x20000, 0x40070000, 0x10000);
911
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200912 ret = lmb_reserve(0x40050000, 0x10000, LMB_NOMAP);
Patrick Delaunaya1860722021-05-07 14:50:32 +0200913 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530914 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 4, 0x40000000, 0x30000,
Patrick Delaunaya1860722021-05-07 14:50:32 +0200915 0x40030000, 0x20000, 0x40050000, 0x10000);
916
917 /* merge with 2 adjacent regions */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200918 ret = lmb_reserve(0x40060000, 0x10000, LMB_NOMAP);
Ilias Apalodimas98491252024-10-23 18:22:00 +0300919 ut_asserteq(ret, 0);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530920 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, 0x40000000, 0x30000,
Patrick Delaunaya1860722021-05-07 14:50:32 +0200921 0x40030000, 0x20000, 0x40050000, 0x30000);
922
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530923 ut_asserteq(lmb_is_nomap(&used[0]), 1);
924 ut_asserteq(lmb_is_nomap(&used[1]), 0);
925 ut_asserteq(lmb_is_nomap(&used[2]), 1);
926
927 lmb_pop(&store);
Patrick Delaunaya1860722021-05-07 14:50:32 +0200928
929 return 0;
930}
Simon Glassb4c722a2023-10-01 19:15:21 -0600931LIB_TEST(lib_test_lmb_flags, 0);