Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2018 Simon Goldschmidt |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <lmb.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 10 | #include <dm/test.h> |
| 11 | #include <test/ut.h> |
| 12 | |
| 13 | static int check_lmb(struct unit_test_state *uts, struct lmb *lmb, |
| 14 | phys_addr_t ram_base, phys_size_t ram_size, |
| 15 | unsigned long num_reserved, |
| 16 | phys_addr_t base1, phys_size_t size1, |
| 17 | phys_addr_t base2, phys_size_t size2, |
| 18 | phys_addr_t base3, phys_size_t size3) |
| 19 | { |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 20 | if (ram_size) { |
| 21 | ut_asserteq(lmb->memory.cnt, 1); |
| 22 | ut_asserteq(lmb->memory.region[0].base, ram_base); |
| 23 | ut_asserteq(lmb->memory.region[0].size, ram_size); |
| 24 | } |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 25 | |
| 26 | ut_asserteq(lmb->reserved.cnt, num_reserved); |
| 27 | if (num_reserved > 0) { |
| 28 | ut_asserteq(lmb->reserved.region[0].base, base1); |
| 29 | ut_asserteq(lmb->reserved.region[0].size, size1); |
| 30 | } |
| 31 | if (num_reserved > 1) { |
| 32 | ut_asserteq(lmb->reserved.region[1].base, base2); |
| 33 | ut_asserteq(lmb->reserved.region[1].size, size2); |
| 34 | } |
| 35 | if (num_reserved > 2) { |
| 36 | ut_asserteq(lmb->reserved.region[2].base, base3); |
| 37 | ut_asserteq(lmb->reserved.region[2].size, size3); |
| 38 | } |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | #define ASSERT_LMB(lmb, ram_base, ram_size, num_reserved, base1, size1, \ |
| 43 | base2, size2, base3, size3) \ |
| 44 | ut_assert(!check_lmb(uts, lmb, ram_base, ram_size, \ |
| 45 | num_reserved, base1, size1, base2, size2, base3, \ |
| 46 | size3)) |
| 47 | |
| 48 | /* |
| 49 | * Test helper function that reserves 64 KiB somewhere in the simulated RAM and |
| 50 | * then does some alloc + free tests. |
| 51 | */ |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 52 | static int test_multi_alloc(struct unit_test_state *uts, const phys_addr_t ram, |
| 53 | const phys_size_t ram_size, const phys_addr_t ram0, |
| 54 | const phys_size_t ram0_size, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 55 | const phys_addr_t alloc_64k_addr) |
| 56 | { |
| 57 | const phys_addr_t ram_end = ram + ram_size; |
| 58 | const phys_addr_t alloc_64k_end = alloc_64k_addr + 0x10000; |
| 59 | |
| 60 | struct lmb lmb; |
| 61 | long ret; |
| 62 | phys_addr_t a, a2, b, b2, c, d; |
| 63 | |
| 64 | /* check for overflow */ |
| 65 | ut_assert(ram_end == 0 || ram_end > ram); |
| 66 | ut_assert(alloc_64k_end > alloc_64k_addr); |
| 67 | /* check input addresses + size */ |
| 68 | ut_assert(alloc_64k_addr >= ram + 8); |
| 69 | ut_assert(alloc_64k_end <= ram_end - 8); |
| 70 | |
| 71 | lmb_init(&lmb); |
| 72 | |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 73 | if (ram0_size) { |
| 74 | ret = lmb_add(&lmb, ram0, ram0_size); |
| 75 | ut_asserteq(ret, 0); |
| 76 | } |
| 77 | |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 78 | ret = lmb_add(&lmb, ram, ram_size); |
| 79 | ut_asserteq(ret, 0); |
| 80 | |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 81 | if (ram0_size) { |
| 82 | ut_asserteq(lmb.memory.cnt, 2); |
| 83 | ut_asserteq(lmb.memory.region[0].base, ram0); |
| 84 | ut_asserteq(lmb.memory.region[0].size, ram0_size); |
| 85 | ut_asserteq(lmb.memory.region[1].base, ram); |
| 86 | ut_asserteq(lmb.memory.region[1].size, ram_size); |
| 87 | } else { |
| 88 | ut_asserteq(lmb.memory.cnt, 1); |
| 89 | ut_asserteq(lmb.memory.region[0].base, ram); |
| 90 | ut_asserteq(lmb.memory.region[0].size, ram_size); |
| 91 | } |
| 92 | |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 93 | /* reserve 64KiB somewhere */ |
| 94 | ret = lmb_reserve(&lmb, alloc_64k_addr, 0x10000); |
| 95 | ut_asserteq(ret, 0); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 96 | ASSERT_LMB(&lmb, 0, 0, 1, alloc_64k_addr, 0x10000, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 97 | 0, 0, 0, 0); |
| 98 | |
| 99 | /* allocate somewhere, should be at the end of RAM */ |
| 100 | a = lmb_alloc(&lmb, 4, 1); |
| 101 | ut_asserteq(a, ram_end - 4); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 102 | ASSERT_LMB(&lmb, 0, 0, 2, alloc_64k_addr, 0x10000, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 103 | ram_end - 4, 4, 0, 0); |
| 104 | /* alloc below end of reserved region -> below reserved region */ |
| 105 | b = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end); |
| 106 | ut_asserteq(b, alloc_64k_addr - 4); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 107 | ASSERT_LMB(&lmb, 0, 0, 2, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 108 | alloc_64k_addr - 4, 0x10000 + 4, ram_end - 4, 4, 0, 0); |
| 109 | |
| 110 | /* 2nd time */ |
| 111 | c = lmb_alloc(&lmb, 4, 1); |
| 112 | ut_asserteq(c, ram_end - 8); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 113 | ASSERT_LMB(&lmb, 0, 0, 2, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 114 | alloc_64k_addr - 4, 0x10000 + 4, ram_end - 8, 8, 0, 0); |
| 115 | d = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end); |
| 116 | ut_asserteq(d, alloc_64k_addr - 8); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 117 | ASSERT_LMB(&lmb, 0, 0, 2, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 118 | alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0); |
| 119 | |
| 120 | ret = lmb_free(&lmb, a, 4); |
| 121 | ut_asserteq(ret, 0); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 122 | ASSERT_LMB(&lmb, 0, 0, 2, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 123 | alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0); |
| 124 | /* allocate again to ensure we get the same address */ |
| 125 | a2 = lmb_alloc(&lmb, 4, 1); |
| 126 | ut_asserteq(a, a2); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 127 | ASSERT_LMB(&lmb, 0, 0, 2, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 128 | alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0); |
| 129 | ret = lmb_free(&lmb, a2, 4); |
| 130 | ut_asserteq(ret, 0); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 131 | ASSERT_LMB(&lmb, 0, 0, 2, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 132 | alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0); |
| 133 | |
| 134 | ret = lmb_free(&lmb, b, 4); |
| 135 | ut_asserteq(ret, 0); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 136 | ASSERT_LMB(&lmb, 0, 0, 3, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 137 | alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, |
| 138 | ram_end - 8, 4); |
| 139 | /* allocate again to ensure we get the same address */ |
| 140 | b2 = lmb_alloc_base(&lmb, 4, 1, alloc_64k_end); |
| 141 | ut_asserteq(b, b2); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 142 | ASSERT_LMB(&lmb, 0, 0, 2, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 143 | alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0); |
| 144 | ret = lmb_free(&lmb, b2, 4); |
| 145 | ut_asserteq(ret, 0); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 146 | ASSERT_LMB(&lmb, 0, 0, 3, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 147 | alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, |
| 148 | ram_end - 8, 4); |
| 149 | |
| 150 | ret = lmb_free(&lmb, c, 4); |
| 151 | ut_asserteq(ret, 0); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 152 | ASSERT_LMB(&lmb, 0, 0, 2, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 153 | alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, 0, 0); |
| 154 | ret = lmb_free(&lmb, d, 4); |
| 155 | ut_asserteq(ret, 0); |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 156 | ASSERT_LMB(&lmb, 0, 0, 1, alloc_64k_addr, 0x10000, |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 157 | 0, 0, 0, 0); |
| 158 | |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 159 | if (ram0_size) { |
| 160 | ut_asserteq(lmb.memory.cnt, 2); |
| 161 | ut_asserteq(lmb.memory.region[0].base, ram0); |
| 162 | ut_asserteq(lmb.memory.region[0].size, ram0_size); |
| 163 | ut_asserteq(lmb.memory.region[1].base, ram); |
| 164 | ut_asserteq(lmb.memory.region[1].size, ram_size); |
| 165 | } else { |
| 166 | ut_asserteq(lmb.memory.cnt, 1); |
| 167 | ut_asserteq(lmb.memory.region[0].base, ram); |
| 168 | ut_asserteq(lmb.memory.region[0].size, ram_size); |
| 169 | } |
| 170 | |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int test_multi_alloc_512mb(struct unit_test_state *uts, |
| 175 | const phys_addr_t ram) |
| 176 | { |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 177 | return test_multi_alloc(uts, ram, 0x20000000, 0, 0, ram + 0x10000000); |
| 178 | } |
| 179 | |
| 180 | static int test_multi_alloc_512mb_x2(struct unit_test_state *uts, |
| 181 | const phys_addr_t ram, |
| 182 | const phys_addr_t ram0) |
| 183 | { |
| 184 | return test_multi_alloc(uts, ram, 0x20000000, ram0, 0x20000000, |
| 185 | ram + 0x10000000); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* Create a memory region with one reserved region and allocate */ |
| 189 | static int lib_test_lmb_simple(struct unit_test_state *uts) |
| 190 | { |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 191 | int ret; |
| 192 | |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 193 | /* simulate 512 MiB RAM beginning at 1GiB */ |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 194 | ret = test_multi_alloc_512mb(uts, 0x40000000); |
| 195 | if (ret) |
| 196 | return ret; |
| 197 | |
| 198 | /* simulate 512 MiB RAM beginning at 1.5GiB */ |
| 199 | return test_multi_alloc_512mb(uts, 0xE0000000); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | DM_TEST(lib_test_lmb_simple, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 203 | |
Simon Goldschmidt | c722dac | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 204 | /* Create two memory regions with one reserved region and allocate */ |
| 205 | static int lib_test_lmb_simple_x2(struct unit_test_state *uts) |
| 206 | { |
| 207 | int ret; |
| 208 | |
| 209 | /* simulate 512 MiB RAM beginning at 2GiB and 1 GiB */ |
| 210 | ret = test_multi_alloc_512mb_x2(uts, 0x80000000, 0x40000000); |
| 211 | if (ret) |
| 212 | return ret; |
| 213 | |
| 214 | /* simulate 512 MiB RAM beginning at 3.5GiB and 1 GiB */ |
| 215 | return test_multi_alloc_512mb_x2(uts, 0xE0000000, 0x40000000); |
| 216 | } |
| 217 | |
| 218 | DM_TEST(lib_test_lmb_simple_x2, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 219 | |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 220 | /* Simulate 512 MiB RAM, allocate some blocks that fit/don't fit */ |
| 221 | static int test_bigblock(struct unit_test_state *uts, const phys_addr_t ram) |
| 222 | { |
| 223 | const phys_size_t ram_size = 0x20000000; |
| 224 | const phys_size_t big_block_size = 0x10000000; |
| 225 | const phys_addr_t ram_end = ram + ram_size; |
| 226 | const phys_addr_t alloc_64k_addr = ram + 0x10000000; |
| 227 | struct lmb lmb; |
| 228 | long ret; |
| 229 | phys_addr_t a, b; |
| 230 | |
| 231 | /* check for overflow */ |
| 232 | ut_assert(ram_end == 0 || ram_end > ram); |
| 233 | |
| 234 | lmb_init(&lmb); |
| 235 | |
| 236 | ret = lmb_add(&lmb, ram, ram_size); |
| 237 | ut_asserteq(ret, 0); |
| 238 | |
| 239 | /* reserve 64KiB in the middle of RAM */ |
| 240 | ret = lmb_reserve(&lmb, alloc_64k_addr, 0x10000); |
| 241 | ut_asserteq(ret, 0); |
| 242 | ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000, |
| 243 | 0, 0, 0, 0); |
| 244 | |
| 245 | /* allocate a big block, should be below reserved */ |
| 246 | a = lmb_alloc(&lmb, big_block_size, 1); |
| 247 | ut_asserteq(a, ram); |
| 248 | ASSERT_LMB(&lmb, ram, ram_size, 1, a, |
| 249 | big_block_size + 0x10000, 0, 0, 0, 0); |
| 250 | /* allocate 2nd big block */ |
| 251 | /* This should fail, printing an error */ |
| 252 | b = lmb_alloc(&lmb, big_block_size, 1); |
| 253 | ut_asserteq(b, 0); |
| 254 | ASSERT_LMB(&lmb, ram, ram_size, 1, a, |
| 255 | big_block_size + 0x10000, 0, 0, 0, 0); |
| 256 | |
| 257 | ret = lmb_free(&lmb, a, big_block_size); |
| 258 | ut_asserteq(ret, 0); |
| 259 | ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000, |
| 260 | 0, 0, 0, 0); |
| 261 | |
| 262 | /* allocate too big block */ |
| 263 | /* This should fail, printing an error */ |
| 264 | a = lmb_alloc(&lmb, ram_size, 1); |
| 265 | ut_asserteq(a, 0); |
| 266 | ASSERT_LMB(&lmb, ram, ram_size, 1, alloc_64k_addr, 0x10000, |
| 267 | 0, 0, 0, 0); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int lib_test_lmb_big(struct unit_test_state *uts) |
| 273 | { |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 274 | int ret; |
| 275 | |
| 276 | /* simulate 512 MiB RAM beginning at 1GiB */ |
| 277 | ret = test_bigblock(uts, 0x40000000); |
| 278 | if (ret) |
| 279 | return ret; |
| 280 | |
| 281 | /* simulate 512 MiB RAM beginning at 1.5GiB */ |
| 282 | return test_bigblock(uts, 0xE0000000); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | DM_TEST(lib_test_lmb_big, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 286 | |
| 287 | /* Simulate 512 MiB RAM, allocate a block without previous reservation */ |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 288 | static int test_noreserved(struct unit_test_state *uts, const phys_addr_t ram, |
| 289 | const phys_addr_t alloc_size, const ulong align) |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 290 | { |
| 291 | const phys_size_t ram_size = 0x20000000; |
| 292 | const phys_addr_t ram_end = ram + ram_size; |
| 293 | struct lmb lmb; |
| 294 | long ret; |
| 295 | phys_addr_t a, b; |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 296 | const phys_addr_t alloc_size_aligned = (alloc_size + align - 1) & |
| 297 | ~(align - 1); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 298 | |
| 299 | /* check for overflow */ |
| 300 | ut_assert(ram_end == 0 || ram_end > ram); |
| 301 | |
| 302 | lmb_init(&lmb); |
| 303 | |
| 304 | ret = lmb_add(&lmb, ram, ram_size); |
| 305 | ut_asserteq(ret, 0); |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 306 | ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 307 | |
| 308 | /* allocate a block */ |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 309 | a = lmb_alloc(&lmb, alloc_size, align); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 310 | ut_assert(a != 0); |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 311 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned, |
| 312 | alloc_size, 0, 0, 0, 0); |
| 313 | /* allocate another block */ |
| 314 | b = lmb_alloc(&lmb, alloc_size, align); |
| 315 | ut_assert(b != 0); |
| 316 | if (alloc_size == alloc_size_aligned) { |
| 317 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - |
| 318 | (alloc_size_aligned * 2), alloc_size * 2, 0, 0, 0, |
| 319 | 0); |
| 320 | } else { |
| 321 | ASSERT_LMB(&lmb, ram, ram_size, 2, ram + ram_size - |
| 322 | (alloc_size_aligned * 2), alloc_size, ram + ram_size |
| 323 | - alloc_size_aligned, alloc_size, 0, 0); |
| 324 | } |
| 325 | /* and free them */ |
| 326 | ret = lmb_free(&lmb, b, alloc_size); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 327 | ut_asserteq(ret, 0); |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 328 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned, |
| 329 | alloc_size, 0, 0, 0, 0); |
| 330 | ret = lmb_free(&lmb, a, alloc_size); |
| 331 | ut_asserteq(ret, 0); |
| 332 | ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 333 | |
| 334 | /* allocate a block with base*/ |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 335 | b = lmb_alloc_base(&lmb, alloc_size, align, ram_end); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 336 | ut_assert(a == b); |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 337 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram + ram_size - alloc_size_aligned, |
| 338 | alloc_size, 0, 0, 0, 0); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 339 | /* and free it */ |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 340 | ret = lmb_free(&lmb, b, alloc_size); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 341 | ut_asserteq(ret, 0); |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 342 | ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static int lib_test_lmb_noreserved(struct unit_test_state *uts) |
| 348 | { |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 349 | int ret; |
| 350 | |
| 351 | /* simulate 512 MiB RAM beginning at 1GiB */ |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 352 | ret = test_noreserved(uts, 0x40000000, 4, 1); |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 353 | if (ret) |
| 354 | return ret; |
| 355 | |
| 356 | /* simulate 512 MiB RAM beginning at 1.5GiB */ |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 357 | return test_noreserved(uts, 0xE0000000, 4, 1); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | DM_TEST(lib_test_lmb_noreserved, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 361 | |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 362 | static int lib_test_lmb_unaligned_size(struct unit_test_state *uts) |
| 363 | { |
| 364 | int ret; |
| 365 | |
| 366 | /* simulate 512 MiB RAM beginning at 1GiB */ |
| 367 | ret = test_noreserved(uts, 0x40000000, 5, 8); |
| 368 | if (ret) |
| 369 | return ret; |
| 370 | |
| 371 | /* simulate 512 MiB RAM beginning at 1.5GiB */ |
| 372 | return test_noreserved(uts, 0xE0000000, 5, 8); |
| 373 | } |
| 374 | |
| 375 | DM_TEST(lib_test_lmb_unaligned_size, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Goldschmidt | 9f3b627 | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 376 | /* |
| 377 | * Simulate a RAM that starts at 0 and allocate down to address 0, which must |
| 378 | * fail as '0' means failure for the lmb_alloc functions. |
| 379 | */ |
| 380 | static int lib_test_lmb_at_0(struct unit_test_state *uts) |
| 381 | { |
| 382 | const phys_addr_t ram = 0; |
| 383 | const phys_size_t ram_size = 0x20000000; |
| 384 | struct lmb lmb; |
| 385 | long ret; |
| 386 | phys_addr_t a, b; |
| 387 | |
| 388 | lmb_init(&lmb); |
| 389 | |
| 390 | ret = lmb_add(&lmb, ram, ram_size); |
| 391 | ut_asserteq(ret, 0); |
| 392 | |
| 393 | /* allocate nearly everything */ |
| 394 | a = lmb_alloc(&lmb, ram_size - 4, 1); |
| 395 | ut_asserteq(a, ram + 4); |
| 396 | ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4, |
| 397 | 0, 0, 0, 0); |
| 398 | /* allocate the rest */ |
| 399 | /* This should fail as the allocated address would be 0 */ |
| 400 | b = lmb_alloc(&lmb, 4, 1); |
| 401 | ut_asserteq(b, 0); |
| 402 | /* check that this was an error by checking lmb */ |
| 403 | ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4, |
| 404 | 0, 0, 0, 0); |
| 405 | /* check that this was an error by freeing b */ |
| 406 | ret = lmb_free(&lmb, b, 4); |
| 407 | ut_asserteq(ret, -1); |
| 408 | ASSERT_LMB(&lmb, ram, ram_size, 1, a, ram_size - 4, |
| 409 | 0, 0, 0, 0); |
| 410 | |
| 411 | ret = lmb_free(&lmb, a, ram_size - 4); |
| 412 | ut_asserteq(ret, 0); |
| 413 | ASSERT_LMB(&lmb, ram, ram_size, 0, 0, 0, 0, 0, 0, 0); |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | DM_TEST(lib_test_lmb_at_0, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 419 | |
| 420 | /* Check that calling lmb_reserve with overlapping regions fails. */ |
| 421 | static int lib_test_lmb_overlapping_reserve(struct unit_test_state *uts) |
| 422 | { |
| 423 | const phys_addr_t ram = 0x40000000; |
| 424 | const phys_size_t ram_size = 0x20000000; |
| 425 | struct lmb lmb; |
| 426 | long ret; |
| 427 | |
| 428 | lmb_init(&lmb); |
| 429 | |
| 430 | ret = lmb_add(&lmb, ram, ram_size); |
| 431 | ut_asserteq(ret, 0); |
| 432 | |
| 433 | ret = lmb_reserve(&lmb, 0x40010000, 0x10000); |
| 434 | ut_asserteq(ret, 0); |
| 435 | ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000, |
| 436 | 0, 0, 0, 0); |
| 437 | /* allocate overlapping region should fail */ |
| 438 | ret = lmb_reserve(&lmb, 0x40011000, 0x10000); |
| 439 | ut_asserteq(ret, -1); |
| 440 | ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x10000, |
| 441 | 0, 0, 0, 0); |
| 442 | /* allocate 3nd region */ |
| 443 | ret = lmb_reserve(&lmb, 0x40030000, 0x10000); |
| 444 | ut_asserteq(ret, 0); |
| 445 | ASSERT_LMB(&lmb, ram, ram_size, 2, 0x40010000, 0x10000, |
| 446 | 0x40030000, 0x10000, 0, 0); |
| 447 | /* allocate 2nd region */ |
| 448 | ret = lmb_reserve(&lmb, 0x40020000, 0x10000); |
| 449 | ut_assert(ret >= 0); |
| 450 | ASSERT_LMB(&lmb, ram, ram_size, 1, 0x40010000, 0x30000, |
| 451 | 0, 0, 0, 0); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | DM_TEST(lib_test_lmb_overlapping_reserve, |
| 457 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 458 | |
| 459 | /* |
| 460 | * Simulate 512 MiB RAM, reserve 3 blocks, allocate addresses in between. |
| 461 | * Expect addresses outside the memory range to fail. |
| 462 | */ |
| 463 | static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) |
| 464 | { |
| 465 | const phys_size_t ram_size = 0x20000000; |
| 466 | const phys_addr_t ram_end = ram + ram_size; |
| 467 | const phys_size_t alloc_addr_a = ram + 0x8000000; |
| 468 | const phys_size_t alloc_addr_b = ram + 0x8000000 * 2; |
| 469 | const phys_size_t alloc_addr_c = ram + 0x8000000 * 3; |
| 470 | struct lmb lmb; |
| 471 | long ret; |
| 472 | phys_addr_t a, b, c, d, e; |
| 473 | |
| 474 | /* check for overflow */ |
| 475 | ut_assert(ram_end == 0 || ram_end > ram); |
| 476 | |
| 477 | lmb_init(&lmb); |
| 478 | |
| 479 | ret = lmb_add(&lmb, ram, ram_size); |
| 480 | ut_asserteq(ret, 0); |
| 481 | |
| 482 | /* reserve 3 blocks */ |
| 483 | ret = lmb_reserve(&lmb, alloc_addr_a, 0x10000); |
| 484 | ut_asserteq(ret, 0); |
| 485 | ret = lmb_reserve(&lmb, alloc_addr_b, 0x10000); |
| 486 | ut_asserteq(ret, 0); |
| 487 | ret = lmb_reserve(&lmb, alloc_addr_c, 0x10000); |
| 488 | ut_asserteq(ret, 0); |
| 489 | ASSERT_LMB(&lmb, ram, ram_size, 3, alloc_addr_a, 0x10000, |
| 490 | alloc_addr_b, 0x10000, alloc_addr_c, 0x10000); |
| 491 | |
| 492 | /* allocate blocks */ |
| 493 | a = lmb_alloc_addr(&lmb, ram, alloc_addr_a - ram); |
| 494 | ut_asserteq(a, ram); |
| 495 | ASSERT_LMB(&lmb, ram, ram_size, 3, ram, 0x8010000, |
| 496 | alloc_addr_b, 0x10000, alloc_addr_c, 0x10000); |
| 497 | b = lmb_alloc_addr(&lmb, alloc_addr_a + 0x10000, |
| 498 | alloc_addr_b - alloc_addr_a - 0x10000); |
| 499 | ut_asserteq(b, alloc_addr_a + 0x10000); |
| 500 | ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x10010000, |
| 501 | alloc_addr_c, 0x10000, 0, 0); |
| 502 | c = lmb_alloc_addr(&lmb, alloc_addr_b + 0x10000, |
| 503 | alloc_addr_c - alloc_addr_b - 0x10000); |
| 504 | ut_asserteq(c, alloc_addr_b + 0x10000); |
| 505 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000, |
| 506 | 0, 0, 0, 0); |
| 507 | d = lmb_alloc_addr(&lmb, alloc_addr_c + 0x10000, |
| 508 | ram_end - alloc_addr_c - 0x10000); |
| 509 | ut_asserteq(d, alloc_addr_c + 0x10000); |
| 510 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram, ram_size, |
| 511 | 0, 0, 0, 0); |
| 512 | |
| 513 | /* allocating anything else should fail */ |
| 514 | e = lmb_alloc(&lmb, 1, 1); |
| 515 | ut_asserteq(e, 0); |
| 516 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram, ram_size, |
| 517 | 0, 0, 0, 0); |
| 518 | |
| 519 | ret = lmb_free(&lmb, d, ram_end - alloc_addr_c - 0x10000); |
| 520 | ut_asserteq(ret, 0); |
| 521 | |
| 522 | /* allocate at 3 points in free range */ |
| 523 | |
| 524 | d = lmb_alloc_addr(&lmb, ram_end - 4, 4); |
| 525 | ut_asserteq(d, ram_end - 4); |
| 526 | ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x18010000, |
| 527 | d, 4, 0, 0); |
| 528 | ret = lmb_free(&lmb, d, 4); |
| 529 | ut_asserteq(ret, 0); |
| 530 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000, |
| 531 | 0, 0, 0, 0); |
| 532 | |
| 533 | d = lmb_alloc_addr(&lmb, ram_end - 128, 4); |
| 534 | ut_asserteq(d, ram_end - 128); |
| 535 | ASSERT_LMB(&lmb, ram, ram_size, 2, ram, 0x18010000, |
| 536 | d, 4, 0, 0); |
| 537 | ret = lmb_free(&lmb, d, 4); |
| 538 | ut_asserteq(ret, 0); |
| 539 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000, |
| 540 | 0, 0, 0, 0); |
| 541 | |
| 542 | d = lmb_alloc_addr(&lmb, alloc_addr_c + 0x10000, 4); |
| 543 | ut_asserteq(d, alloc_addr_c + 0x10000); |
| 544 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010004, |
| 545 | 0, 0, 0, 0); |
| 546 | ret = lmb_free(&lmb, d, 4); |
| 547 | ut_asserteq(ret, 0); |
| 548 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram, 0x18010000, |
| 549 | 0, 0, 0, 0); |
| 550 | |
| 551 | /* allocate at the bottom */ |
| 552 | ret = lmb_free(&lmb, a, alloc_addr_a - ram); |
| 553 | ut_asserteq(ret, 0); |
| 554 | ASSERT_LMB(&lmb, ram, ram_size, 1, ram + 0x8000000, 0x10010000, |
| 555 | 0, 0, 0, 0); |
| 556 | d = lmb_alloc_addr(&lmb, ram, 4); |
| 557 | ut_asserteq(d, ram); |
| 558 | ASSERT_LMB(&lmb, ram, ram_size, 2, d, 4, |
| 559 | ram + 0x8000000, 0x10010000, 0, 0); |
| 560 | |
| 561 | /* check that allocating outside memory fails */ |
| 562 | if (ram_end != 0) { |
| 563 | ret = lmb_alloc_addr(&lmb, ram_end, 1); |
| 564 | ut_asserteq(ret, 0); |
| 565 | } |
| 566 | if (ram != 0) { |
| 567 | ret = lmb_alloc_addr(&lmb, ram - 1, 1); |
| 568 | ut_asserteq(ret, 0); |
| 569 | } |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | static int lib_test_lmb_alloc_addr(struct unit_test_state *uts) |
| 575 | { |
| 576 | int ret; |
| 577 | |
| 578 | /* simulate 512 MiB RAM beginning at 1GiB */ |
| 579 | ret = test_alloc_addr(uts, 0x40000000); |
| 580 | if (ret) |
| 581 | return ret; |
| 582 | |
| 583 | /* simulate 512 MiB RAM beginning at 1.5GiB */ |
| 584 | return test_alloc_addr(uts, 0xE0000000); |
| 585 | } |
| 586 | |
| 587 | DM_TEST(lib_test_lmb_alloc_addr, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 588 | |
| 589 | /* Simulate 512 MiB RAM, reserve 3 blocks, check addresses in between */ |
| 590 | static int test_get_unreserved_size(struct unit_test_state *uts, |
| 591 | const phys_addr_t ram) |
| 592 | { |
| 593 | const phys_size_t ram_size = 0x20000000; |
| 594 | const phys_addr_t ram_end = ram + ram_size; |
| 595 | const phys_size_t alloc_addr_a = ram + 0x8000000; |
| 596 | const phys_size_t alloc_addr_b = ram + 0x8000000 * 2; |
| 597 | const phys_size_t alloc_addr_c = ram + 0x8000000 * 3; |
| 598 | struct lmb lmb; |
| 599 | long ret; |
| 600 | phys_size_t s; |
| 601 | |
| 602 | /* check for overflow */ |
| 603 | ut_assert(ram_end == 0 || ram_end > ram); |
| 604 | |
| 605 | lmb_init(&lmb); |
| 606 | |
| 607 | ret = lmb_add(&lmb, ram, ram_size); |
| 608 | ut_asserteq(ret, 0); |
| 609 | |
| 610 | /* reserve 3 blocks */ |
| 611 | ret = lmb_reserve(&lmb, alloc_addr_a, 0x10000); |
| 612 | ut_asserteq(ret, 0); |
| 613 | ret = lmb_reserve(&lmb, alloc_addr_b, 0x10000); |
| 614 | ut_asserteq(ret, 0); |
| 615 | ret = lmb_reserve(&lmb, alloc_addr_c, 0x10000); |
| 616 | ut_asserteq(ret, 0); |
| 617 | ASSERT_LMB(&lmb, ram, ram_size, 3, alloc_addr_a, 0x10000, |
| 618 | alloc_addr_b, 0x10000, alloc_addr_c, 0x10000); |
| 619 | |
| 620 | /* check addresses in between blocks */ |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 621 | s = lmb_get_free_size(&lmb, ram); |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 622 | ut_asserteq(s, alloc_addr_a - ram); |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 623 | s = lmb_get_free_size(&lmb, ram + 0x10000); |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 624 | ut_asserteq(s, alloc_addr_a - ram - 0x10000); |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 625 | s = lmb_get_free_size(&lmb, alloc_addr_a - 4); |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 626 | ut_asserteq(s, 4); |
| 627 | |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 628 | s = lmb_get_free_size(&lmb, alloc_addr_a + 0x10000); |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 629 | ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x10000); |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 630 | s = lmb_get_free_size(&lmb, alloc_addr_a + 0x20000); |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 631 | ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x20000); |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 632 | s = lmb_get_free_size(&lmb, alloc_addr_b - 4); |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 633 | ut_asserteq(s, 4); |
| 634 | |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 635 | s = lmb_get_free_size(&lmb, alloc_addr_c + 0x10000); |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 636 | ut_asserteq(s, ram_end - alloc_addr_c - 0x10000); |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 637 | s = lmb_get_free_size(&lmb, alloc_addr_c + 0x20000); |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 638 | ut_asserteq(s, ram_end - alloc_addr_c - 0x20000); |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 639 | s = lmb_get_free_size(&lmb, ram_end - 4); |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 640 | ut_asserteq(s, 4); |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 645 | static int lib_test_lmb_get_free_size(struct unit_test_state *uts) |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 646 | { |
| 647 | int ret; |
| 648 | |
| 649 | /* simulate 512 MiB RAM beginning at 1GiB */ |
| 650 | ret = test_get_unreserved_size(uts, 0x40000000); |
| 651 | if (ret) |
| 652 | return ret; |
| 653 | |
| 654 | /* simulate 512 MiB RAM beginning at 1.5GiB */ |
| 655 | return test_get_unreserved_size(uts, 0xE0000000); |
| 656 | } |
| 657 | |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 658 | DM_TEST(lib_test_lmb_get_free_size, |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 659 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |