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