blob: c01f38f7d4842e4ad61b77eef929d26bcdad8b6d [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}
393
Simon Glass631d8a32023-10-01 19:15:21 -0600394LIB_TEST(lib_test_lmb_noreserved, 0);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100395
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100396static int lib_test_lmb_unaligned_size(struct unit_test_state *uts)
397{
398 int ret;
399
400 /* simulate 512 MiB RAM beginning at 1GiB */
401 ret = test_noreserved(uts, 0x40000000, 5, 8);
402 if (ret)
403 return ret;
404
405 /* simulate 512 MiB RAM beginning at 1.5GiB */
406 return test_noreserved(uts, 0xE0000000, 5, 8);
407}
Simon Glass631d8a32023-10-01 19:15:21 -0600408LIB_TEST(lib_test_lmb_unaligned_size, 0);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100409
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100410/*
411 * Simulate a RAM that starts at 0 and allocate down to address 0, which must
412 * fail as '0' means failure for the lmb_alloc functions.
413 */
414static int lib_test_lmb_at_0(struct unit_test_state *uts)
415{
416 const phys_addr_t ram = 0;
417 const phys_size_t ram_size = 0x20000000;
Sughosh Ganued17a332024-08-26 17:29:18 +0530418 struct lmb store;
419 struct alist *mem_lst, *used_lst;
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100420 long ret;
421 phys_addr_t a, b;
422
Sughosh Ganued17a332024-08-26 17:29:18 +0530423 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100424
Sughosh Ganued17a332024-08-26 17:29:18 +0530425 ret = lmb_add(ram, ram_size);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100426 ut_asserteq(ret, 0);
427
428 /* allocate nearly everything */
Sughosh Ganued17a332024-08-26 17:29:18 +0530429 a = lmb_alloc(ram_size - 4, 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100430 ut_asserteq(a, ram + 4);
Sughosh Ganued17a332024-08-26 17:29:18 +0530431 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100432 0, 0, 0, 0);
433 /* allocate the rest */
434 /* This should fail as the allocated address would be 0 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530435 b = lmb_alloc(4, 1);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100436 ut_asserteq(b, 0);
437 /* check that this was an error by checking lmb */
Sughosh Ganued17a332024-08-26 17:29:18 +0530438 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100439 0, 0, 0, 0);
440 /* check that this was an error by freeing b */
Sughosh Ganued17a332024-08-26 17:29:18 +0530441 ret = lmb_free(b, 4);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100442 ut_asserteq(ret, -1);
Sughosh Ganued17a332024-08-26 17:29:18 +0530443 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4,
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100444 0, 0, 0, 0);
445
Sughosh Ganued17a332024-08-26 17:29:18 +0530446 ret = lmb_free(a, ram_size - 4);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100447 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530448 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0);
449
450 lmb_pop(&store);
Simon Goldschmidta01ae0c2019-01-14 22:38:14 +0100451
452 return 0;
453}
Simon Glass631d8a32023-10-01 19:15:21 -0600454LIB_TEST(lib_test_lmb_at_0, 0);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100455
456/* Check that calling lmb_reserve with overlapping regions fails. */
457static int lib_test_lmb_overlapping_reserve(struct unit_test_state *uts)
458{
459 const phys_addr_t ram = 0x40000000;
460 const phys_size_t ram_size = 0x20000000;
Sughosh Ganued17a332024-08-26 17:29:18 +0530461 struct lmb store;
462 struct alist *mem_lst, *used_lst;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100463 long ret;
464
Sughosh Ganued17a332024-08-26 17:29:18 +0530465 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100466
Sughosh Ganued17a332024-08-26 17:29:18 +0530467 ret = lmb_add(ram, ram_size);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100468 ut_asserteq(ret, 0);
469
Sughosh Ganued17a332024-08-26 17:29:18 +0530470 ret = lmb_reserve(0x40010000, 0x10000);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100471 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530472 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100473 0, 0, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530474
475 /* allocate overlapping region should return the coalesced count */
476 ret = lmb_reserve(0x40011000, 0x10000);
477 ut_asserteq(ret, 1);
478 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x11000,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100479 0, 0, 0, 0);
480 /* allocate 3nd region */
Sughosh Ganued17a332024-08-26 17:29:18 +0530481 ret = lmb_reserve(0x40030000, 0x10000);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100482 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530483 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40010000, 0x11000,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100484 0x40030000, 0x10000, 0, 0);
Udit Kumar4a6105e2023-09-26 16:54:43 +0530485 /* allocate 2nd region , This should coalesced all region into one */
Sughosh Ganued17a332024-08-26 17:29:18 +0530486 ret = lmb_reserve(0x40020000, 0x10000);
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100487 ut_assert(ret >= 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530488 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x30000,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100489 0, 0, 0, 0);
490
Udit Kumar4a6105e2023-09-26 16:54:43 +0530491 /* allocate 2nd region, which should be added as first region */
Sughosh Ganued17a332024-08-26 17:29:18 +0530492 ret = lmb_reserve(0x40000000, 0x8000);
Udit Kumar4a6105e2023-09-26 16:54:43 +0530493 ut_assert(ret >= 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530494 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x8000,
Udit Kumar4a6105e2023-09-26 16:54:43 +0530495 0x40010000, 0x30000, 0, 0);
496
497 /* allocate 3rd region, coalesce with first and overlap with second */
Sughosh Ganued17a332024-08-26 17:29:18 +0530498 ret = lmb_reserve(0x40008000, 0x10000);
Udit Kumar4a6105e2023-09-26 16:54:43 +0530499 ut_assert(ret >= 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530500 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40000000, 0x40000,
Udit Kumar4a6105e2023-09-26 16:54:43 +0530501 0, 0, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530502
503 lmb_pop(&store);
504
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100505 return 0;
506}
Simon Glass631d8a32023-10-01 19:15:21 -0600507LIB_TEST(lib_test_lmb_overlapping_reserve, 0);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100508
509/*
510 * Simulate 512 MiB RAM, reserve 3 blocks, allocate addresses in between.
511 * Expect addresses outside the memory range to fail.
512 */
513static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
514{
Sughosh Ganued17a332024-08-26 17:29:18 +0530515 struct lmb store;
516 struct alist *mem_lst, *used_lst;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100517 const phys_size_t ram_size = 0x20000000;
518 const phys_addr_t ram_end = ram + ram_size;
519 const phys_size_t alloc_addr_a = ram + 0x8000000;
520 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
521 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100522 long ret;
523 phys_addr_t a, b, c, d, e;
524
525 /* check for overflow */
526 ut_assert(ram_end == 0 || ram_end > ram);
527
Sughosh Ganued17a332024-08-26 17:29:18 +0530528 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100529
Sughosh Ganued17a332024-08-26 17:29:18 +0530530 ret = lmb_add(ram, ram_size);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100531 ut_asserteq(ret, 0);
532
533 /* reserve 3 blocks */
Sughosh Ganued17a332024-08-26 17:29:18 +0530534 ret = lmb_reserve(alloc_addr_a, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100535 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530536 ret = lmb_reserve(alloc_addr_b, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100537 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530538 ret = lmb_reserve(alloc_addr_c, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100539 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530540 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, alloc_addr_a, 0x10000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100541 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
542
543 /* allocate blocks */
Sughosh Ganued17a332024-08-26 17:29:18 +0530544 a = lmb_alloc_addr(ram, alloc_addr_a - ram);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100545 ut_asserteq(a, ram);
Sughosh Ganued17a332024-08-26 17:29:18 +0530546 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, ram, 0x8010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100547 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530548 b = lmb_alloc_addr(alloc_addr_a + 0x10000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100549 alloc_addr_b - alloc_addr_a - 0x10000);
550 ut_asserteq(b, alloc_addr_a + 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530551 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x10010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100552 alloc_addr_c, 0x10000, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530553 c = lmb_alloc_addr(alloc_addr_b + 0x10000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100554 alloc_addr_c - alloc_addr_b - 0x10000);
555 ut_asserteq(c, alloc_addr_b + 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530556 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100557 0, 0, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530558 d = lmb_alloc_addr(alloc_addr_c + 0x10000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100559 ram_end - alloc_addr_c - 0x10000);
560 ut_asserteq(d, alloc_addr_c + 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530561 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, ram_size,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100562 0, 0, 0, 0);
563
564 /* allocating anything else should fail */
Sughosh Ganued17a332024-08-26 17:29:18 +0530565 e = lmb_alloc(1, 1);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100566 ut_asserteq(e, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530567 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, ram_size,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100568 0, 0, 0, 0);
569
Sughosh Ganued17a332024-08-26 17:29:18 +0530570 ret = lmb_free(d, ram_end - alloc_addr_c - 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100571 ut_asserteq(ret, 0);
572
573 /* allocate at 3 points in free range */
574
Sughosh Ganued17a332024-08-26 17:29:18 +0530575 d = lmb_alloc_addr(ram_end - 4, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100576 ut_asserteq(d, ram_end - 4);
Sughosh Ganued17a332024-08-26 17:29:18 +0530577 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100578 d, 4, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530579 ret = lmb_free(d, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100580 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530581 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100582 0, 0, 0, 0);
583
Sughosh Ganued17a332024-08-26 17:29:18 +0530584 d = lmb_alloc_addr(ram_end - 128, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100585 ut_asserteq(d, ram_end - 128);
Sughosh Ganued17a332024-08-26 17:29:18 +0530586 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100587 d, 4, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530588 ret = lmb_free(d, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100589 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530590 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100591 0, 0, 0, 0);
592
Sughosh Ganued17a332024-08-26 17:29:18 +0530593 d = lmb_alloc_addr(alloc_addr_c + 0x10000, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100594 ut_asserteq(d, alloc_addr_c + 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530595 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010004,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100596 0, 0, 0, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530597 ret = lmb_free(d, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100598 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530599 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100600 0, 0, 0, 0);
601
602 /* allocate at the bottom */
Sughosh Ganued17a332024-08-26 17:29:18 +0530603 ret = lmb_free(a, alloc_addr_a - ram);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100604 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530605 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram + 0x8000000,
606 0x10010000, 0, 0, 0, 0);
607
608 d = lmb_alloc_addr(ram, 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100609 ut_asserteq(d, ram);
Sughosh Ganued17a332024-08-26 17:29:18 +0530610 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, d, 4,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100611 ram + 0x8000000, 0x10010000, 0, 0);
612
613 /* check that allocating outside memory fails */
614 if (ram_end != 0) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530615 ret = lmb_alloc_addr(ram_end, 1);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100616 ut_asserteq(ret, 0);
617 }
618 if (ram != 0) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530619 ret = lmb_alloc_addr(ram - 1, 1);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100620 ut_asserteq(ret, 0);
621 }
622
Sughosh Ganued17a332024-08-26 17:29:18 +0530623 lmb_pop(&store);
624
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100625 return 0;
626}
627
628static int lib_test_lmb_alloc_addr(struct unit_test_state *uts)
629{
630 int ret;
631
632 /* simulate 512 MiB RAM beginning at 1GiB */
633 ret = test_alloc_addr(uts, 0x40000000);
634 if (ret)
635 return ret;
636
637 /* simulate 512 MiB RAM beginning at 1.5GiB */
638 return test_alloc_addr(uts, 0xE0000000);
639}
Simon Glass631d8a32023-10-01 19:15:21 -0600640LIB_TEST(lib_test_lmb_alloc_addr, 0);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100641
642/* Simulate 512 MiB RAM, reserve 3 blocks, check addresses in between */
643static int test_get_unreserved_size(struct unit_test_state *uts,
644 const phys_addr_t ram)
645{
Sughosh Ganued17a332024-08-26 17:29:18 +0530646 struct lmb store;
647 struct alist *mem_lst, *used_lst;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100648 const phys_size_t ram_size = 0x20000000;
649 const phys_addr_t ram_end = ram + ram_size;
650 const phys_size_t alloc_addr_a = ram + 0x8000000;
651 const phys_size_t alloc_addr_b = ram + 0x8000000 * 2;
652 const phys_size_t alloc_addr_c = ram + 0x8000000 * 3;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100653 long ret;
654 phys_size_t s;
655
656 /* check for overflow */
657 ut_assert(ram_end == 0 || ram_end > ram);
Sughosh Ganued17a332024-08-26 17:29:18 +0530658 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100659
Sughosh Ganued17a332024-08-26 17:29:18 +0530660 ret = lmb_add(ram, ram_size);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100661 ut_asserteq(ret, 0);
662
663 /* reserve 3 blocks */
Sughosh Ganued17a332024-08-26 17:29:18 +0530664 ret = lmb_reserve(alloc_addr_a, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100665 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530666 ret = lmb_reserve(alloc_addr_b, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100667 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530668 ret = lmb_reserve(alloc_addr_c, 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100669 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530670 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, alloc_addr_a, 0x10000,
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100671 alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
672
673 /* check addresses in between blocks */
Sughosh Ganued17a332024-08-26 17:29:18 +0530674 s = lmb_get_free_size(ram);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100675 ut_asserteq(s, alloc_addr_a - ram);
Sughosh Ganued17a332024-08-26 17:29:18 +0530676 s = lmb_get_free_size(ram + 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100677 ut_asserteq(s, alloc_addr_a - ram - 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530678 s = lmb_get_free_size(alloc_addr_a - 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100679 ut_asserteq(s, 4);
680
Sughosh Ganued17a332024-08-26 17:29:18 +0530681 s = lmb_get_free_size(alloc_addr_a + 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100682 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530683 s = lmb_get_free_size(alloc_addr_a + 0x20000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100684 ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x20000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530685 s = lmb_get_free_size(alloc_addr_b - 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100686 ut_asserteq(s, 4);
687
Sughosh Ganued17a332024-08-26 17:29:18 +0530688 s = lmb_get_free_size(alloc_addr_c + 0x10000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100689 ut_asserteq(s, ram_end - alloc_addr_c - 0x10000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530690 s = lmb_get_free_size(alloc_addr_c + 0x20000);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100691 ut_asserteq(s, ram_end - alloc_addr_c - 0x20000);
Sughosh Ganued17a332024-08-26 17:29:18 +0530692 s = lmb_get_free_size(ram_end - 4);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100693 ut_asserteq(s, 4);
694
Sughosh Ganued17a332024-08-26 17:29:18 +0530695 lmb_pop(&store);
696
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100697 return 0;
698}
699
Simon Goldschmidt65304aa2019-01-21 20:29:55 +0100700static int lib_test_lmb_get_free_size(struct unit_test_state *uts)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100701{
702 int ret;
703
704 /* simulate 512 MiB RAM beginning at 1GiB */
705 ret = test_get_unreserved_size(uts, 0x40000000);
706 if (ret)
707 return ret;
708
709 /* simulate 512 MiB RAM beginning at 1.5GiB */
710 return test_get_unreserved_size(uts, 0xE0000000);
711}
Simon Glass631d8a32023-10-01 19:15:21 -0600712LIB_TEST(lib_test_lmb_get_free_size, 0);
Patrick Delaunaya57b5962021-03-10 10:16:30 +0100713
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200714static int lib_test_lmb_flags(struct unit_test_state *uts)
715{
Sughosh Ganued17a332024-08-26 17:29:18 +0530716 struct lmb store;
717 struct lmb_region *mem, *used;
718 struct alist *mem_lst, *used_lst;
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200719 const phys_addr_t ram = 0x40000000;
720 const phys_size_t ram_size = 0x20000000;
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200721 long ret;
722
Sughosh Ganued17a332024-08-26 17:29:18 +0530723 ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst));
724 mem = mem_lst->data;
725 used = used_lst->data;
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200726
Sughosh Ganued17a332024-08-26 17:29:18 +0530727 ret = lmb_add(ram, ram_size);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200728 ut_asserteq(ret, 0);
729
730 /* reserve, same flag */
Sughosh Ganued17a332024-08-26 17:29:18 +0530731 ret = lmb_reserve_flags(0x40010000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200732 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530733 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200734 0, 0, 0, 0);
735
736 /* reserve again, same flag */
Sughosh Ganued17a332024-08-26 17:29:18 +0530737 ret = lmb_reserve_flags(0x40010000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200738 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530739 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200740 0, 0, 0, 0);
741
742 /* reserve again, new flag */
Sughosh Ganued17a332024-08-26 17:29:18 +0530743 ret = lmb_reserve_flags(0x40010000, 0x10000, LMB_NONE);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200744 ut_asserteq(ret, -1);
Sughosh Ganued17a332024-08-26 17:29:18 +0530745 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200746 0, 0, 0, 0);
747
Sughosh Ganued17a332024-08-26 17:29:18 +0530748 ut_asserteq(lmb_is_nomap(&used[0]), 1);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200749
750 /* merge after */
Sughosh Ganued17a332024-08-26 17:29:18 +0530751 ret = lmb_reserve_flags(0x40020000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200752 ut_asserteq(ret, 1);
Sughosh Ganued17a332024-08-26 17:29:18 +0530753 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x20000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200754 0, 0, 0, 0);
755
756 /* merge before */
Sughosh Ganued17a332024-08-26 17:29:18 +0530757 ret = lmb_reserve_flags(0x40000000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200758 ut_asserteq(ret, 1);
Sughosh Ganued17a332024-08-26 17:29:18 +0530759 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200760 0, 0, 0, 0);
761
Sughosh Ganued17a332024-08-26 17:29:18 +0530762 ut_asserteq(lmb_is_nomap(&used[0]), 1);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200763
Sughosh Ganued17a332024-08-26 17:29:18 +0530764 ret = lmb_reserve_flags(0x40030000, 0x10000, LMB_NONE);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200765 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530766 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200767 0x40030000, 0x10000, 0, 0);
768
Sughosh Ganued17a332024-08-26 17:29:18 +0530769 ut_asserteq(lmb_is_nomap(&used[0]), 1);
770 ut_asserteq(lmb_is_nomap(&used[1]), 0);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200771
772 /* test that old API use LMB_NONE */
Sughosh Ganued17a332024-08-26 17:29:18 +0530773 ret = lmb_reserve(0x40040000, 0x10000);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200774 ut_asserteq(ret, 1);
Sughosh Ganued17a332024-08-26 17:29:18 +0530775 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200776 0x40030000, 0x20000, 0, 0);
777
Sughosh Ganued17a332024-08-26 17:29:18 +0530778 ut_asserteq(lmb_is_nomap(&used[0]), 1);
779 ut_asserteq(lmb_is_nomap(&used[1]), 0);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200780
Sughosh Ganued17a332024-08-26 17:29:18 +0530781 ret = lmb_reserve_flags(0x40070000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200782 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530783 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200784 0x40030000, 0x20000, 0x40070000, 0x10000);
785
Sughosh Ganued17a332024-08-26 17:29:18 +0530786 ret = lmb_reserve_flags(0x40050000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200787 ut_asserteq(ret, 0);
Sughosh Ganued17a332024-08-26 17:29:18 +0530788 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 4, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200789 0x40030000, 0x20000, 0x40050000, 0x10000);
790
791 /* merge with 2 adjacent regions */
Sughosh Ganued17a332024-08-26 17:29:18 +0530792 ret = lmb_reserve_flags(0x40060000, 0x10000, LMB_NOMAP);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200793 ut_asserteq(ret, 2);
Sughosh Ganued17a332024-08-26 17:29:18 +0530794 ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, 0x40000000, 0x30000,
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200795 0x40030000, 0x20000, 0x40050000, 0x30000);
796
Sughosh Ganued17a332024-08-26 17:29:18 +0530797 ut_asserteq(lmb_is_nomap(&used[0]), 1);
798 ut_asserteq(lmb_is_nomap(&used[1]), 0);
799 ut_asserteq(lmb_is_nomap(&used[2]), 1);
800
801 lmb_pop(&store);
Patrick Delaunay7d08a3d2021-05-07 14:50:32 +0200802
803 return 0;
804}
Simon Glass631d8a32023-10-01 19:15:21 -0600805LIB_TEST(lib_test_lmb_flags, 0);