blob: b2c54fb4bcb8f0d0e8c356420bfd1d7f79ea40de [file] [log] [blame]
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018 Simon Goldschmidt
4 */
5
Sughosh Ganued17a332024-08-26 17:29:18 +05306#include <alist.h>
Simon Glass0e1fad42020-07-19 10:15:37 -06007#include <dm.h>
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +01008#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +010011#include <dm/test.h>
Simon Glass631d8a32023-10-01 19:15:21 -060012#include <test/lib.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060013#include <test/test.h>
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +010014#include <test/ut.h>
15
Sughosh Ganued17a332024-08-26 17:29:18 +053016static inline bool lmb_is_nomap(struct lmb_region *m)
Heinrich Schuchardt5a515132021-11-14 08:41:07 +010017{
18 return m->flags & LMB_NOMAP;
19}
20
Sughosh Ganued17a332024-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 Goldschmidta01ae0c2019-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 Ganued17a332024-08-26 17:29:18 +053028 struct lmb_region *mem, *used;
29
30 mem = mem_lst->data;
31 used = used_lst->data;
32
Simon Goldschmidtdc57be52019-02-01 21:23:59 +010033 if (ram_size) {
Sughosh Ganued17a332024-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 Goldschmidtdc57be52019-02-01 21:23:59 +010037 }
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +010038
Sughosh Ganued17a332024-08-26 17:29:18 +053039 ut_asserteq(used_lst->count, num_reserved);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +010040 if (num_reserved > 0) {
Sughosh Ganued17a332024-08-26 17:29:18 +053041 ut_asserteq(used[0].base, base1);
42 ut_asserteq(used[0].size, size1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +010043 }
44 if (num_reserved > 1) {
Sughosh Ganued17a332024-08-26 17:29:18 +053045 ut_asserteq(used[1].base, base2);
46 ut_asserteq(used[1].size, size2);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +010047 }
48 if (num_reserved > 2) {
Sughosh Ganued17a332024-08-26 17:29:18 +053049 ut_asserteq(used[2].base, base3);
50 ut_asserteq(used[2].size, size3);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +010051 }
52 return 0;
53}
54
Sughosh Ganued17a332024-08-26 17:29:18 +053055#define ASSERT_LMB(mem_lst, used_lst, ram_base, ram_size, num_reserved, base1, size1, \
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +010056 base2, size2, base3, size3) \
Sughosh Ganued17a332024-08-26 17:29:18 +053057 ut_assert(!check_lmb(uts, mem_lst, used_lst, ram_base, ram_size, \
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +010058 num_reserved, base1, size1, base2, size2, base3, \
59 size3))
60
Sughosh Ganued17a332024-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();
68 *mem_lstp = &lmb->free_mem;
69 *used_lstp = &lmb->used_mem;
70
71 return 0;
72}
73
Simon Goldschmidtdc57be52019-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 Goldschmidta01ae0c2019-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 Goldschmidta01ae0c2019-01-14 22:38:14 +010082 long ret;
Sughosh Ganued17a332024-08-26 17:29:18 +053083 struct alist *mem_lst, *used_lst;
84 struct lmb_region *mem, *used;
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +010085 phys_addr_t a, a2, b, b2, c, d;
Sughosh Ganued17a332024-08-26 17:29:18 +053086 struct lmb store;
Simon Goldschmidta01ae0c2019-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 Ganued17a332024-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 Goldschmidta01ae0c2019-01-14 22:38:14 +010098
Simon Goldschmidtdc57be52019-02-01 21:23:59 +010099 if (ram0_size) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530100 ret = lmb_add(ram0, ram0_size);
Simon Goldschmidtdc57be52019-02-01 21:23:59 +0100101 ut_asserteq(ret, 0);
102 }
103
Sughosh Ganued17a332024-08-26 17:29:18 +0530104 ret = lmb_add(ram, ram_size);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100105 ut_asserteq(ret, 0);
106
Simon Goldschmidtdc57be52019-02-01 21:23:59 +0100107 if (ram0_size) {
Sughosh Ganued17a332024-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 Goldschmidtdc57be52019-02-01 21:23:59 +0100113 } else {
Sughosh Ganued17a332024-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 Goldschmidtdc57be52019-02-01 21:23:59 +0100117 }
118
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100119 /* reserve 64KiB somewhere */
Sughosh Ganued17a332024-08-26 17:29:18 +0530120 ret = lmb_reserve(alloc_64k_addr, 0x10000);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100121 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530122 ASSERT_LMB(mem_lst, used_lst, 0, 0, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100123 0, 0, 0, 0);
124
125 /* allocate somewhere, should be at the end of RAM */
Sughosh Ganued17a332024-08-26 17:29:18 +0530126 a = lmb_alloc(4, 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100127 ut_asserteq(a, ram_end - 4);
Sughosh Ganued17a332024-08-26 17:29:18 +0530128 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, alloc_64k_addr, 0x10000,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100129 ram_end - 4, 4, 0, 0);
130 /* alloc below end of reserved region -> below reserved region */
Sughosh Ganued17a332024-08-26 17:29:18 +0530131 b = lmb_alloc_base(4, 1, alloc_64k_end);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100132 ut_asserteq(b, alloc_64k_addr - 4);
Sughosh Ganued17a332024-08-26 17:29:18 +0530133 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100134 alloc_64k_addr - 4, 0x10000 + 4, ram_end - 4, 4, 0, 0);
135
136 /* 2nd time */
Sughosh Ganued17a332024-08-26 17:29:18 +0530137 c = lmb_alloc(4, 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100138 ut_asserteq(c, ram_end - 8);
Sughosh Ganued17a332024-08-26 17:29:18 +0530139 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100140 alloc_64k_addr - 4, 0x10000 + 4, ram_end - 8, 8, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530141 d = lmb_alloc_base(4, 1, alloc_64k_end);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100142 ut_asserteq(d, alloc_64k_addr - 8);
Sughosh Ganued17a332024-08-26 17:29:18 +0530143 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100144 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);
145
Sughosh Ganued17a332024-08-26 17:29:18 +0530146 ret = lmb_free(a, 4);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100147 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530148 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidta01ae0c2019-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 Ganued17a332024-08-26 17:29:18 +0530151 a2 = lmb_alloc(4, 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100152 ut_asserteq(a, a2);
Sughosh Ganued17a332024-08-26 17:29:18 +0530153 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100154 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530155 ret = lmb_free(a2, 4);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100156 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530157 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100158 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
159
Sughosh Ganued17a332024-08-26 17:29:18 +0530160 ret = lmb_free(b, 4);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100161 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530162 ASSERT_LMB(mem_lst, used_lst, 0, 0, 3,
Simon Goldschmidta01ae0c2019-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 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530166 b2 = lmb_alloc_base(4, 1, alloc_64k_end);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100167 ut_asserteq(b, b2);
Sughosh Ganued17a332024-08-26 17:29:18 +0530168 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100169 alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530170 ret = lmb_free(b2, 4);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100171 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530172 ASSERT_LMB(mem_lst, used_lst, 0, 0, 3,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100173 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000,
174 ram_end - 8, 4);
175
Sughosh Ganued17a332024-08-26 17:29:18 +0530176 ret = lmb_free(c, 4);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100177 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530178 ASSERT_LMB(mem_lst, used_lst, 0, 0, 2,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100179 alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530180 ret = lmb_free(d, 4);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100181 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530182 ASSERT_LMB(mem_lst, used_lst, 0, 0, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100183 0, 0, 0, 0);
184
Simon Goldschmidtdc57be52019-02-01 21:23:59 +0100185 if (ram0_size) {
Sughosh Ganued17a332024-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 Goldschmidtdc57be52019-02-01 21:23:59 +0100191 } else {
Sughosh Ganued17a332024-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 Goldschmidtdc57be52019-02-01 21:23:59 +0100195 }
196
Sughosh Ganued17a332024-08-26 17:29:18 +0530197 lmb_pop(&store);
198
Simon Goldschmidta01ae0c2019-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 Goldschmidtdc57be52019-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 Goldschmidta01ae0c2019-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 Goldschmidtd67f33c2019-01-14 22:38:15 +0100219 int ret;
220
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100221 /* simulate 512 MiB RAM beginning at 1GiB */
Simon Goldschmidtd67f33c2019-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 Goldschmidta01ae0c2019-01-14 22:38:14 +0100228}
Simon Glass631d8a32023-10-01 19:15:21 -0600229LIB_TEST(lib_test_lmb_simple, 0);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100230
Simon Goldschmidtdc57be52019-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 Glass631d8a32023-10-01 19:15:21 -0600244LIB_TEST(lib_test_lmb_simple_x2, 0);
Simon Goldschmidtdc57be52019-02-01 21:23:59 +0100245
Simon Goldschmidta01ae0c2019-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 Ganued17a332024-08-26 17:29:18 +0530253 struct alist *mem_lst, *used_lst;
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100254 long ret;
255 phys_addr_t a, b;
Sughosh Ganued17a332024-08-26 17:29:18 +0530256 struct lmb store;
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100257
258 /* check for overflow */
259 ut_assert(ram_end == 0 || ram_end > ram);
260
Sughosh Ganued17a332024-08-26 17:29:18 +0530261 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100262
Sughosh Ganued17a332024-08-26 17:29:18 +0530263 ret = lmb_add(ram, ram_size);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100264 ut_asserteq(ret, 0);
265
266 /* reserve 64KiB in the middle of RAM */
Sughosh Ganued17a332024-08-26 17:29:18 +0530267 ret = lmb_reserve(alloc_64k_addr, 0x10000);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100268 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530269 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100270 0, 0, 0, 0);
271
272 /* allocate a big block, should be below reserved */
Sughosh Ganued17a332024-08-26 17:29:18 +0530273 a = lmb_alloc(big_block_size, 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100274 ut_asserteq(a, ram);
Sughosh Ganued17a332024-08-26 17:29:18 +0530275 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a,
Simon Goldschmidta01ae0c2019-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 Ganued17a332024-08-26 17:29:18 +0530279 b = lmb_alloc(big_block_size, 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100280 ut_asserteq(b, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530281 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100282 big_block_size + 0x10000, 0, 0, 0, 0);
283
Sughosh Ganued17a332024-08-26 17:29:18 +0530284 ret = lmb_free(a, big_block_size);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100285 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530286 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100287 0, 0, 0, 0);
288
289 /* allocate too big block */
290 /* This should fail, printing an error */
Sughosh Ganued17a332024-08-26 17:29:18 +0530291 a = lmb_alloc(ram_size, 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100292 ut_asserteq(a, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530293 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_64k_addr, 0x10000,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100294 0, 0, 0, 0);
295
Sughosh Ganued17a332024-08-26 17:29:18 +0530296 lmb_pop(&store);
297
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100298 return 0;
299}
300
301static int lib_test_lmb_big(struct unit_test_state *uts)
302{
Simon Goldschmidtd67f33c2019-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 Goldschmidta01ae0c2019-01-14 22:38:14 +0100312}
Simon Glass631d8a32023-10-01 19:15:21 -0600313LIB_TEST(lib_test_lmb_big, 0);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100314
315/* Simulate 512 MiB RAM, allocate a block without previous reservation */
Simon Goldschmidt0f7c51a2019-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 Goldschmidta01ae0c2019-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 Goldschmidta01ae0c2019-01-14 22:38:14 +0100321 long ret;
322 phys_addr_t a, b;
Sughosh Ganued17a332024-08-26 17:29:18 +0530323 struct lmb store;
324 struct alist *mem_lst, *used_lst;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100325 const phys_addr_t alloc_size_aligned = (alloc_size + align - 1) &
326 ~(align - 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100327
328 /* check for overflow */
329 ut_assert(ram_end == 0 || ram_end > ram);
330
Sughosh Ganued17a332024-08-26 17:29:18 +0530331 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100332
Sughosh Ganued17a332024-08-26 17:29:18 +0530333 ret = lmb_add(ram, ram_size);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100334 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530335 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100336
337 /* allocate a block */
Sughosh Ganued17a332024-08-26 17:29:18 +0530338 a = lmb_alloc(alloc_size, align);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100339 ut_assert(a != 0);
Sughosh Ganued17a332024-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 Goldschmidt0f7c51a2019-01-14 22:38:16 +0100343 /* allocate another block */
Sughosh Ganued17a332024-08-26 17:29:18 +0530344 b = lmb_alloc(alloc_size, align);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100345 ut_assert(b != 0);
346 if (alloc_size == alloc_size_aligned) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530347 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram + ram_size -
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100348 (alloc_size_aligned * 2), alloc_size * 2, 0, 0, 0,
349 0);
350 } else {
Sughosh Ganued17a332024-08-26 17:29:18 +0530351 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram + ram_size -
Simon Goldschmidt0f7c51a2019-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 Ganued17a332024-08-26 17:29:18 +0530356 ret = lmb_free(b, alloc_size);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100357 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530358 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1,
359 ram + ram_size - alloc_size_aligned,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100360 alloc_size, 0, 0, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530361 ret = lmb_free(a, alloc_size);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100362 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530363 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100364
365 /* allocate a block with base*/
Sughosh Ganued17a332024-08-26 17:29:18 +0530366 b = lmb_alloc_base(alloc_size, align, ram_end);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100367 ut_assert(a == b);
Sughosh Ganued17a332024-08-26 17:29:18 +0530368 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1,
369 ram + ram_size - alloc_size_aligned,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100370 alloc_size, 0, 0, 0, 0);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100371 /* and free it */
Sughosh Ganued17a332024-08-26 17:29:18 +0530372 ret = lmb_free(b, alloc_size);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100373 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-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 Goldschmidta01ae0c2019-01-14 22:38:14 +0100377
378 return 0;
379}
380
381static int lib_test_lmb_noreserved(struct unit_test_state *uts)
382{
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100383 int ret;
384
385 /* simulate 512 MiB RAM beginning at 1GiB */
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100386 ret = test_noreserved(uts, 0x40000000, 4, 1);
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100387 if (ret)
388 return ret;
389
390 /* simulate 512 MiB RAM beginning at 1.5GiB */
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100391 return test_noreserved(uts, 0xE0000000, 4, 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100392}
Simon Glass631d8a32023-10-01 19:15:21 -0600393LIB_TEST(lib_test_lmb_noreserved, 0);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100394
Simon Goldschmidt0f7c51a2019-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 Glass631d8a32023-10-01 19:15:21 -0600407LIB_TEST(lib_test_lmb_unaligned_size, 0);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100408
Simon Goldschmidta01ae0c2019-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 Ganued17a332024-08-26 17:29:18 +0530417 struct lmb store;
418 struct alist *mem_lst, *used_lst;
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100419 long ret;
420 phys_addr_t a, b;
421
Sughosh Ganued17a332024-08-26 17:29:18 +0530422 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100423
Sughosh Ganued17a332024-08-26 17:29:18 +0530424 ret = lmb_add(ram, ram_size);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100425 ut_asserteq(ret, 0);
426
427 /* allocate nearly everything */
Sughosh Ganued17a332024-08-26 17:29:18 +0530428 a = lmb_alloc(ram_size - 4, 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100429 ut_asserteq(a, ram + 4);
Sughosh Ganued17a332024-08-26 17:29:18 +0530430 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4,
Simon Goldschmidta01ae0c2019-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 Ganued17a332024-08-26 17:29:18 +0530434 b = lmb_alloc(4, 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100435 ut_asserteq(b, 0);
436 /* check that this was an error by checking lmb */
Sughosh Ganued17a332024-08-26 17:29:18 +0530437 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100438 0, 0, 0, 0);
439 /* check that this was an error by freeing b */
Sughosh Ganued17a332024-08-26 17:29:18 +0530440 ret = lmb_free(b, 4);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100441 ut_asserteq(ret, -1);
Sughosh Ganued17a332024-08-26 17:29:18 +0530442 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100443 0, 0, 0, 0);
444
Sughosh Ganued17a332024-08-26 17:29:18 +0530445 ret = lmb_free(a, ram_size - 4);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100446 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-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 Goldschmidta01ae0c2019-01-14 22:38:14 +0100450
451 return 0;
452}
Simon Glass631d8a32023-10-01 19:15:21 -0600453LIB_TEST(lib_test_lmb_at_0, 0);
Simon Goldschmidt0f7c51a2019-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 Ganued17a332024-08-26 17:29:18 +0530460 struct lmb store;
461 struct alist *mem_lst, *used_lst;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100462 long ret;
463
Sughosh Ganued17a332024-08-26 17:29:18 +0530464 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100465
Sughosh Ganued17a332024-08-26 17:29:18 +0530466 ret = lmb_add(ram, ram_size);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100467 ut_asserteq(ret, 0);
468
Sughosh Ganued17a332024-08-26 17:29:18 +0530469 ret = lmb_reserve(0x40010000, 0x10000);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100470 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530471 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100472 0, 0, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530473
474 /* allocate overlapping region should return the coalesced count */
475 ret = lmb_reserve(0x40011000, 0x10000);
476 ut_asserteq(ret, 1);
477 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x11000,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100478 0, 0, 0, 0);
479 /* allocate 3nd region */
Sughosh Ganued17a332024-08-26 17:29:18 +0530480 ret = lmb_reserve(0x40030000, 0x10000);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100481 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530482 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40010000, 0x11000,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100483 0x40030000, 0x10000, 0, 0);
Udit Kumar4a6105e2023-09-26 16:54:43 +0530484 /* allocate 2nd region , This should coalesced all region into one */
Sughosh Ganued17a332024-08-26 17:29:18 +0530485 ret = lmb_reserve(0x40020000, 0x10000);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100486 ut_assert(ret >= 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530487 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x30000,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100488 0, 0, 0, 0);
489
Udit Kumar4a6105e2023-09-26 16:54:43 +0530490 /* allocate 2nd region, which should be added as first region */
Sughosh Ganued17a332024-08-26 17:29:18 +0530491 ret = lmb_reserve(0x40000000, 0x8000);
Udit Kumar4a6105e2023-09-26 16:54:43 +0530492 ut_assert(ret >= 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530493 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x8000,
Udit Kumar4a6105e2023-09-26 16:54:43 +0530494 0x40010000, 0x30000, 0, 0);
495
496 /* allocate 3rd region, coalesce with first and overlap with second */
Sughosh Ganued17a332024-08-26 17:29:18 +0530497 ret = lmb_reserve(0x40008000, 0x10000);
Udit Kumar4a6105e2023-09-26 16:54:43 +0530498 ut_assert(ret >= 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530499 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40000000, 0x40000,
Udit Kumar4a6105e2023-09-26 16:54:43 +0530500 0, 0, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530501
502 lmb_pop(&store);
503
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100504 return 0;
505}
Simon Glass631d8a32023-10-01 19:15:21 -0600506LIB_TEST(lib_test_lmb_overlapping_reserve, 0);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100507
508/*
509 * Simulate 512 MiB RAM, reserve 3 blocks, allocate addresses in between.
510 * Expect addresses outside the memory range to fail.
511 */
512static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
513{
Sughosh Ganued17a332024-08-26 17:29:18 +0530514 struct lmb store;
515 struct alist *mem_lst, *used_lst;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100516 const phys_size_t ram_size = 0x20000000;
517 const phys_addr_t ram_end = ram + ram_size;
518 const phys_size_t alloc_addr_a = ram + 0x8000000;
519 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
520 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100521 long ret;
522 phys_addr_t a, b, c, d, e;
523
524 /* check for overflow */
525 ut_assert(ram_end == 0 || ram_end > ram);
526
Sughosh Ganued17a332024-08-26 17:29:18 +0530527 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100528
Sughosh Ganued17a332024-08-26 17:29:18 +0530529 ret = lmb_add(ram, ram_size);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100530 ut_asserteq(ret, 0);
531
532 /* reserve 3 blocks */
Sughosh Ganued17a332024-08-26 17:29:18 +0530533 ret = lmb_reserve(alloc_addr_a, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100534 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530535 ret = lmb_reserve(alloc_addr_b, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100536 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530537 ret = lmb_reserve(alloc_addr_c, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100538 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530539 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, alloc_addr_a, 0x10000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100540 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
541
542 /* allocate blocks */
Sughosh Ganued17a332024-08-26 17:29:18 +0530543 a = lmb_alloc_addr(ram, alloc_addr_a - ram);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100544 ut_asserteq(a, ram);
Sughosh Ganued17a332024-08-26 17:29:18 +0530545 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, ram, 0x8010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100546 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530547 b = lmb_alloc_addr(alloc_addr_a + 0x10000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100548 alloc_addr_b - alloc_addr_a - 0x10000);
549 ut_asserteq(b, alloc_addr_a + 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530550 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x10010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100551 alloc_addr_c, 0x10000, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530552 c = lmb_alloc_addr(alloc_addr_b + 0x10000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100553 alloc_addr_c - alloc_addr_b - 0x10000);
554 ut_asserteq(c, alloc_addr_b + 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530555 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100556 0, 0, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530557 d = lmb_alloc_addr(alloc_addr_c + 0x10000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100558 ram_end - alloc_addr_c - 0x10000);
559 ut_asserteq(d, alloc_addr_c + 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530560 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, ram_size,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100561 0, 0, 0, 0);
562
563 /* allocating anything else should fail */
Sughosh Ganued17a332024-08-26 17:29:18 +0530564 e = lmb_alloc(1, 1);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100565 ut_asserteq(e, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530566 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, ram_size,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100567 0, 0, 0, 0);
568
Sughosh Ganued17a332024-08-26 17:29:18 +0530569 ret = lmb_free(d, ram_end - alloc_addr_c - 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100570 ut_asserteq(ret, 0);
571
572 /* allocate at 3 points in free range */
573
Sughosh Ganued17a332024-08-26 17:29:18 +0530574 d = lmb_alloc_addr(ram_end - 4, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100575 ut_asserteq(d, ram_end - 4);
Sughosh Ganued17a332024-08-26 17:29:18 +0530576 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100577 d, 4, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530578 ret = lmb_free(d, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100579 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530580 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100581 0, 0, 0, 0);
582
Sughosh Ganued17a332024-08-26 17:29:18 +0530583 d = lmb_alloc_addr(ram_end - 128, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100584 ut_asserteq(d, ram_end - 128);
Sughosh Ganued17a332024-08-26 17:29:18 +0530585 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100586 d, 4, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530587 ret = lmb_free(d, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100588 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530589 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100590 0, 0, 0, 0);
591
Sughosh Ganued17a332024-08-26 17:29:18 +0530592 d = lmb_alloc_addr(alloc_addr_c + 0x10000, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100593 ut_asserteq(d, alloc_addr_c + 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530594 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010004,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100595 0, 0, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530596 ret = lmb_free(d, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100597 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530598 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100599 0, 0, 0, 0);
600
601 /* allocate at the bottom */
Sughosh Ganued17a332024-08-26 17:29:18 +0530602 ret = lmb_free(a, alloc_addr_a - ram);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100603 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530604 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram + 0x8000000,
605 0x10010000, 0, 0, 0, 0);
606
607 d = lmb_alloc_addr(ram, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100608 ut_asserteq(d, ram);
Sughosh Ganued17a332024-08-26 17:29:18 +0530609 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, d, 4,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100610 ram + 0x8000000, 0x10010000, 0, 0);
611
612 /* check that allocating outside memory fails */
613 if (ram_end != 0) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530614 ret = lmb_alloc_addr(ram_end, 1);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100615 ut_asserteq(ret, 0);
616 }
617 if (ram != 0) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530618 ret = lmb_alloc_addr(ram - 1, 1);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100619 ut_asserteq(ret, 0);
620 }
621
Sughosh Ganued17a332024-08-26 17:29:18 +0530622 lmb_pop(&store);
623
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100624 return 0;
625}
626
627static int lib_test_lmb_alloc_addr(struct unit_test_state *uts)
628{
629 int ret;
630
631 /* simulate 512 MiB RAM beginning at 1GiB */
632 ret = test_alloc_addr(uts, 0x40000000);
633 if (ret)
634 return ret;
635
636 /* simulate 512 MiB RAM beginning at 1.5GiB */
637 return test_alloc_addr(uts, 0xE0000000);
638}
Simon Glass631d8a32023-10-01 19:15:21 -0600639LIB_TEST(lib_test_lmb_alloc_addr, 0);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100640
641/* Simulate 512 MiB RAM, reserve 3 blocks, check addresses in between */
642static int test_get_unreserved_size(struct unit_test_state *uts,
643 const phys_addr_t ram)
644{
Sughosh Ganued17a332024-08-26 17:29:18 +0530645 struct lmb store;
646 struct alist *mem_lst, *used_lst;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100647 const phys_size_t ram_size = 0x20000000;
648 const phys_addr_t ram_end = ram + ram_size;
649 const phys_size_t alloc_addr_a = ram + 0x8000000;
650 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
651 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100652 long ret;
653 phys_size_t s;
654
655 /* check for overflow */
656 ut_assert(ram_end == 0 || ram_end > ram);
Sughosh Ganued17a332024-08-26 17:29:18 +0530657 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100658
Sughosh Ganued17a332024-08-26 17:29:18 +0530659 ret = lmb_add(ram, ram_size);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100660 ut_asserteq(ret, 0);
661
662 /* reserve 3 blocks */
Sughosh Ganued17a332024-08-26 17:29:18 +0530663 ret = lmb_reserve(alloc_addr_a, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100664 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530665 ret = lmb_reserve(alloc_addr_b, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100666 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530667 ret = lmb_reserve(alloc_addr_c, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100668 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530669 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, alloc_addr_a, 0x10000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100670 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
671
672 /* check addresses in between blocks */
Sughosh Ganued17a332024-08-26 17:29:18 +0530673 s = lmb_get_free_size(ram);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100674 ut_asserteq(s, alloc_addr_a - ram);
Sughosh Ganued17a332024-08-26 17:29:18 +0530675 s = lmb_get_free_size(ram + 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100676 ut_asserteq(s, alloc_addr_a - ram - 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530677 s = lmb_get_free_size(alloc_addr_a - 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100678 ut_asserteq(s, 4);
679
Sughosh Ganued17a332024-08-26 17:29:18 +0530680 s = lmb_get_free_size(alloc_addr_a + 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100681 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530682 s = lmb_get_free_size(alloc_addr_a + 0x20000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100683 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x20000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530684 s = lmb_get_free_size(alloc_addr_b - 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100685 ut_asserteq(s, 4);
686
Sughosh Ganued17a332024-08-26 17:29:18 +0530687 s = lmb_get_free_size(alloc_addr_c + 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100688 ut_asserteq(s, ram_end - alloc_addr_c - 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530689 s = lmb_get_free_size(alloc_addr_c + 0x20000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100690 ut_asserteq(s, ram_end - alloc_addr_c - 0x20000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530691 s = lmb_get_free_size(ram_end - 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100692 ut_asserteq(s, 4);
693
Sughosh Ganued17a332024-08-26 17:29:18 +0530694 lmb_pop(&store);
695
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100696 return 0;
697}
698
Simon Goldschmidt65304aa2019-01-21 20:29:55 +0100699static int lib_test_lmb_get_free_size(struct unit_test_state *uts)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100700{
701 int ret;
702
703 /* simulate 512 MiB RAM beginning at 1GiB */
704 ret = test_get_unreserved_size(uts, 0x40000000);
705 if (ret)
706 return ret;
707
708 /* simulate 512 MiB RAM beginning at 1.5GiB */
709 return test_get_unreserved_size(uts, 0xE0000000);
710}
Simon Glass631d8a32023-10-01 19:15:21 -0600711LIB_TEST(lib_test_lmb_get_free_size, 0);
Patrick Delaunaya57b5962021-03-10 10:16:30 +0100712
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200713static int lib_test_lmb_flags(struct unit_test_state *uts)
714{
Sughosh Ganued17a332024-08-26 17:29:18 +0530715 struct lmb store;
716 struct lmb_region *mem, *used;
717 struct alist *mem_lst, *used_lst;
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200718 const phys_addr_t ram = 0x40000000;
719 const phys_size_t ram_size = 0x20000000;
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200720 long ret;
721
Sughosh Ganued17a332024-08-26 17:29:18 +0530722 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
723 mem = mem_lst->data;
724 used = used_lst->data;
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200725
Sughosh Ganued17a332024-08-26 17:29:18 +0530726 ret = lmb_add(ram, ram_size);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200727 ut_asserteq(ret, 0);
728
729 /* reserve, same flag */
Sughosh Ganued17a332024-08-26 17:29:18 +0530730 ret = lmb_reserve_flags(0x40010000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200731 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530732 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200733 0, 0, 0, 0);
734
735 /* reserve again, same flag */
Sughosh Ganued17a332024-08-26 17:29:18 +0530736 ret = lmb_reserve_flags(0x40010000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200737 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530738 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200739 0, 0, 0, 0);
740
741 /* reserve again, new flag */
Sughosh Ganued17a332024-08-26 17:29:18 +0530742 ret = lmb_reserve_flags(0x40010000, 0x10000, LMB_NONE);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200743 ut_asserteq(ret, -1);
Sughosh Ganued17a332024-08-26 17:29:18 +0530744 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200745 0, 0, 0, 0);
746
Sughosh Ganued17a332024-08-26 17:29:18 +0530747 ut_asserteq(lmb_is_nomap(&used[0]), 1);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200748
749 /* merge after */
Sughosh Ganued17a332024-08-26 17:29:18 +0530750 ret = lmb_reserve_flags(0x40020000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200751 ut_asserteq(ret, 1);
Sughosh Ganued17a332024-08-26 17:29:18 +0530752 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x20000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200753 0, 0, 0, 0);
754
755 /* merge before */
Sughosh Ganued17a332024-08-26 17:29:18 +0530756 ret = lmb_reserve_flags(0x40000000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200757 ut_asserteq(ret, 1);
Sughosh Ganued17a332024-08-26 17:29:18 +0530758 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200759 0, 0, 0, 0);
760
Sughosh Ganued17a332024-08-26 17:29:18 +0530761 ut_asserteq(lmb_is_nomap(&used[0]), 1);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200762
Sughosh Ganued17a332024-08-26 17:29:18 +0530763 ret = lmb_reserve_flags(0x40030000, 0x10000, LMB_NONE);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200764 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530765 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200766 0x40030000, 0x10000, 0, 0);
767
Sughosh Ganued17a332024-08-26 17:29:18 +0530768 ut_asserteq(lmb_is_nomap(&used[0]), 1);
769 ut_asserteq(lmb_is_nomap(&used[1]), 0);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200770
771 /* test that old API use LMB_NONE */
Sughosh Ganued17a332024-08-26 17:29:18 +0530772 ret = lmb_reserve(0x40040000, 0x10000);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200773 ut_asserteq(ret, 1);
Sughosh Ganued17a332024-08-26 17:29:18 +0530774 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200775 0x40030000, 0x20000, 0, 0);
776
Sughosh Ganued17a332024-08-26 17:29:18 +0530777 ut_asserteq(lmb_is_nomap(&used[0]), 1);
778 ut_asserteq(lmb_is_nomap(&used[1]), 0);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200779
Sughosh Ganued17a332024-08-26 17:29:18 +0530780 ret = lmb_reserve_flags(0x40070000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200781 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530782 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200783 0x40030000, 0x20000, 0x40070000, 0x10000);
784
Sughosh Ganued17a332024-08-26 17:29:18 +0530785 ret = lmb_reserve_flags(0x40050000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200786 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530787 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 4, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200788 0x40030000, 0x20000, 0x40050000, 0x10000);
789
790 /* merge with 2 adjacent regions */
Sughosh Ganued17a332024-08-26 17:29:18 +0530791 ret = lmb_reserve_flags(0x40060000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200792 ut_asserteq(ret, 2);
Sughosh Ganued17a332024-08-26 17:29:18 +0530793 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200794 0x40030000, 0x20000, 0x40050000, 0x30000);
795
Sughosh Ganued17a332024-08-26 17:29:18 +0530796 ut_asserteq(lmb_is_nomap(&used[0]), 1);
797 ut_asserteq(lmb_is_nomap(&used[1]), 0);
798 ut_asserteq(lmb_is_nomap(&used[2]), 1);
799
800 lmb_pop(&store);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200801
802 return 0;
803}
Simon Glass631d8a32023-10-01 19:15:21 -0600804LIB_TEST(lib_test_lmb_flags, 0);