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 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 6 | #include <alist.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 8 | #include <lmb.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 11 | #include <dm/test.h> |
Simon Glass | 631d8a3 | 2023-10-01 19:15:21 -0600 | [diff] [blame] | 12 | #include <test/lib.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 13 | #include <test/test.h> |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 14 | #include <test/ut.h> |
| 15 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 16 | static inline bool lmb_is_nomap(struct lmb_region *m) |
Heinrich Schuchardt | 5a51513 | 2021-11-14 08:41:07 +0100 | [diff] [blame] | 17 | { |
| 18 | return m->flags & LMB_NOMAP; |
| 19 | } |
| 20 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 21 | static 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 Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 24 | 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 Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 28 | struct lmb_region *mem, *used; |
| 29 | |
| 30 | mem = mem_lst->data; |
| 31 | used = used_lst->data; |
| 32 | |
Simon Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 33 | if (ram_size) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 34 | ut_asserteq(mem_lst->count, 1); |
| 35 | ut_asserteq(mem[0].base, ram_base); |
| 36 | ut_asserteq(mem[0].size, ram_size); |
Simon Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 37 | } |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 38 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 39 | ut_asserteq(used_lst->count, num_reserved); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 40 | if (num_reserved > 0) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 41 | ut_asserteq(used[0].base, base1); |
| 42 | ut_asserteq(used[0].size, size1); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 43 | } |
| 44 | if (num_reserved > 1) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 45 | ut_asserteq(used[1].base, base2); |
| 46 | ut_asserteq(used[1].size, size2); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 47 | } |
| 48 | if (num_reserved > 2) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 49 | ut_asserteq(used[2].base, base3); |
| 50 | ut_asserteq(used[2].size, size3); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 55 | #define ASSERT_LMB(mem_lst, used_lst, ram_base, ram_size, num_reserved, base1, size1, \ |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 56 | base2, size2, base3, size3) \ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 57 | ut_assert(!check_lmb(uts, mem_lst, used_lst, ram_base, ram_size, \ |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 58 | num_reserved, base1, size1, base2, size2, base3, \ |
| 59 | size3)) |
| 60 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 61 | static 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 Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 74 | static 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 Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 77 | 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 Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 82 | long ret; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 83 | struct alist *mem_lst, *used_lst; |
| 84 | struct lmb_region *mem, *used; |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 85 | phys_addr_t a, a2, b, b2, c, d; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 86 | struct lmb store; |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 87 | |
| 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 Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 95 | ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst)); |
| 96 | mem = mem_lst->data; |
| 97 | used = used_lst->data; |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 98 | |
Simon Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 99 | if (ram0_size) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 100 | ret = lmb_add(ram0, ram0_size); |
Simon Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 101 | ut_asserteq(ret, 0); |
| 102 | } |
| 103 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 104 | ret = lmb_add(ram, ram_size); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 105 | ut_asserteq(ret, 0); |
| 106 | |
Simon Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 107 | if (ram0_size) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 108 | 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 Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 113 | } else { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 114 | ut_asserteq(mem_lst->count, 1); |
| 115 | ut_asserteq(mem[0].base, ram); |
| 116 | ut_asserteq(mem[0].size, ram_size); |
Simon Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 119 | /* reserve 64KiB somewhere */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 120 | ret = lmb_reserve(alloc_64k_addr, 0x10000); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 121 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 122 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 1, alloc_64k_addr, 0x10000, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 123 | 0, 0, 0, 0); |
| 124 | |
| 125 | /* allocate somewhere, should be at the end of RAM */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 126 | a = lmb_alloc(4, 1); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 127 | ut_asserteq(a, ram_end - 4); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 128 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, alloc_64k_addr, 0x10000, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 129 | ram_end - 4, 4, 0, 0); |
| 130 | /* alloc below end of reserved region -> below reserved region */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 131 | b = lmb_alloc_base(4, 1, alloc_64k_end); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 132 | ut_asserteq(b, alloc_64k_addr - 4); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 133 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 134 | alloc_64k_addr - 4, 0x10000 + 4, ram_end - 4, 4, 0, 0); |
| 135 | |
| 136 | /* 2nd time */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 137 | c = lmb_alloc(4, 1); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 138 | ut_asserteq(c, ram_end - 8); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 139 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 140 | alloc_64k_addr - 4, 0x10000 + 4, ram_end - 8, 8, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 141 | d = lmb_alloc_base(4, 1, alloc_64k_end); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 142 | ut_asserteq(d, alloc_64k_addr - 8); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 143 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 144 | alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0); |
| 145 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 146 | ret = lmb_free(a, 4); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 147 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 148 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 149 | alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0); |
| 150 | /* allocate again to ensure we get the same address */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 151 | a2 = lmb_alloc(4, 1); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 152 | ut_asserteq(a, a2); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 153 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 154 | alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 155 | ret = lmb_free(a2, 4); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 156 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 157 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 158 | alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0); |
| 159 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 160 | ret = lmb_free(b, 4); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 161 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 162 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 3, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 163 | 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 Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 166 | b2 = lmb_alloc_base(4, 1, alloc_64k_end); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 167 | ut_asserteq(b, b2); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 168 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 169 | alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 170 | ret = lmb_free(b2, 4); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 171 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 172 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 3, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 173 | alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, |
| 174 | ram_end - 8, 4); |
| 175 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 176 | ret = lmb_free(c, 4); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 177 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 178 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 179 | alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 180 | ret = lmb_free(d, 4); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 181 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 182 | ASSERT_LMB(mem_lst, used_lst, 0, 0, 1, alloc_64k_addr, 0x10000, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 183 | 0, 0, 0, 0); |
| 184 | |
Simon Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 185 | if (ram0_size) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 186 | 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 Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 191 | } else { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 192 | ut_asserteq(mem_lst->count, 1); |
| 193 | ut_asserteq(mem[0].base, ram); |
| 194 | ut_asserteq(mem[0].size, ram_size); |
Simon Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 197 | lmb_pop(&store); |
| 198 | |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int test_multi_alloc_512mb(struct unit_test_state *uts, |
| 203 | const phys_addr_t ram) |
| 204 | { |
Simon Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 205 | return test_multi_alloc(uts, ram, 0x20000000, 0, 0, ram + 0x10000000); |
| 206 | } |
| 207 | |
| 208 | static 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 Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* Create a memory region with one reserved region and allocate */ |
| 217 | static int lib_test_lmb_simple(struct unit_test_state *uts) |
| 218 | { |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 219 | int ret; |
| 220 | |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 221 | /* simulate 512 MiB RAM beginning at 1GiB */ |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 222 | 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 Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 228 | } |
Simon Glass | 631d8a3 | 2023-10-01 19:15:21 -0600 | [diff] [blame] | 229 | LIB_TEST(lib_test_lmb_simple, 0); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 230 | |
Simon Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 231 | /* Create two memory regions with one reserved region and allocate */ |
| 232 | static 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 Glass | 631d8a3 | 2023-10-01 19:15:21 -0600 | [diff] [blame] | 244 | LIB_TEST(lib_test_lmb_simple_x2, 0); |
Simon Goldschmidt | dc57be5 | 2019-02-01 21:23:59 +0100 | [diff] [blame] | 245 | |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 246 | /* Simulate 512 MiB RAM, allocate some blocks that fit/don't fit */ |
| 247 | static 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 Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 253 | struct alist *mem_lst, *used_lst; |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 254 | long ret; |
| 255 | phys_addr_t a, b; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 256 | struct lmb store; |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 257 | |
| 258 | /* check for overflow */ |
| 259 | ut_assert(ram_end == 0 || ram_end > ram); |
| 260 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 261 | ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst)); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 262 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 263 | ret = lmb_add(ram, ram_size); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 264 | ut_asserteq(ret, 0); |
| 265 | |
| 266 | /* reserve 64KiB in the middle of RAM */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 267 | ret = lmb_reserve(alloc_64k_addr, 0x10000); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 268 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 269 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_64k_addr, 0x10000, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 270 | 0, 0, 0, 0); |
| 271 | |
| 272 | /* allocate a big block, should be below reserved */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 273 | a = lmb_alloc(big_block_size, 1); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 274 | ut_asserteq(a, ram); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 275 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 276 | big_block_size + 0x10000, 0, 0, 0, 0); |
| 277 | /* allocate 2nd big block */ |
| 278 | /* This should fail, printing an error */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 279 | b = lmb_alloc(big_block_size, 1); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 280 | ut_asserteq(b, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 281 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 282 | big_block_size + 0x10000, 0, 0, 0, 0); |
| 283 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 284 | ret = lmb_free(a, big_block_size); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 285 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 286 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_64k_addr, 0x10000, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 287 | 0, 0, 0, 0); |
| 288 | |
| 289 | /* allocate too big block */ |
| 290 | /* This should fail, printing an error */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 291 | a = lmb_alloc(ram_size, 1); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 292 | ut_asserteq(a, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 293 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_64k_addr, 0x10000, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 294 | 0, 0, 0, 0); |
| 295 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 296 | lmb_pop(&store); |
| 297 | |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int lib_test_lmb_big(struct unit_test_state *uts) |
| 302 | { |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 303 | 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 Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 312 | } |
Simon Glass | 631d8a3 | 2023-10-01 19:15:21 -0600 | [diff] [blame] | 313 | LIB_TEST(lib_test_lmb_big, 0); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 314 | |
| 315 | /* Simulate 512 MiB RAM, allocate a block without previous reservation */ |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 316 | static int test_noreserved(struct unit_test_state *uts, const phys_addr_t ram, |
| 317 | const phys_addr_t alloc_size, const ulong align) |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 318 | { |
| 319 | const phys_size_t ram_size = 0x20000000; |
| 320 | const phys_addr_t ram_end = ram + ram_size; |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 321 | long ret; |
| 322 | phys_addr_t a, b; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 323 | struct lmb store; |
| 324 | struct alist *mem_lst, *used_lst; |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 325 | const phys_addr_t alloc_size_aligned = (alloc_size + align - 1) & |
| 326 | ~(align - 1); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 327 | |
| 328 | /* check for overflow */ |
| 329 | ut_assert(ram_end == 0 || ram_end > ram); |
| 330 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 331 | ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst)); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 332 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 333 | ret = lmb_add(ram, ram_size); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 334 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 335 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 336 | |
| 337 | /* allocate a block */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 338 | a = lmb_alloc(alloc_size, align); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 339 | ut_assert(a != 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 340 | 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 Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 343 | /* allocate another block */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 344 | b = lmb_alloc(alloc_size, align); |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 345 | ut_assert(b != 0); |
| 346 | if (alloc_size == alloc_size_aligned) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 347 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram + ram_size - |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 348 | (alloc_size_aligned * 2), alloc_size * 2, 0, 0, 0, |
| 349 | 0); |
| 350 | } else { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 351 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram + ram_size - |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 352 | (alloc_size_aligned * 2), alloc_size, ram + ram_size |
| 353 | - alloc_size_aligned, alloc_size, 0, 0); |
| 354 | } |
| 355 | /* and free them */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 356 | ret = lmb_free(b, alloc_size); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 357 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 358 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, |
| 359 | ram + ram_size - alloc_size_aligned, |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 360 | alloc_size, 0, 0, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 361 | ret = lmb_free(a, alloc_size); |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 362 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 363 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 364 | |
| 365 | /* allocate a block with base*/ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 366 | b = lmb_alloc_base(alloc_size, align, ram_end); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 367 | ut_assert(a == b); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 368 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, |
| 369 | ram + ram_size - alloc_size_aligned, |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 370 | alloc_size, 0, 0, 0, 0); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 371 | /* and free it */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 372 | ret = lmb_free(b, alloc_size); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 373 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 374 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0); |
| 375 | |
| 376 | lmb_pop(&store); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static int lib_test_lmb_noreserved(struct unit_test_state *uts) |
| 382 | { |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 383 | int ret; |
| 384 | |
| 385 | /* simulate 512 MiB RAM beginning at 1GiB */ |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 386 | ret = test_noreserved(uts, 0x40000000, 4, 1); |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 387 | if (ret) |
| 388 | return ret; |
| 389 | |
| 390 | /* simulate 512 MiB RAM beginning at 1.5GiB */ |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 391 | return test_noreserved(uts, 0xE0000000, 4, 1); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 392 | } |
Simon Glass | 631d8a3 | 2023-10-01 19:15:21 -0600 | [diff] [blame] | 393 | LIB_TEST(lib_test_lmb_noreserved, 0); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 394 | |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 395 | static int lib_test_lmb_unaligned_size(struct unit_test_state *uts) |
| 396 | { |
| 397 | int ret; |
| 398 | |
| 399 | /* simulate 512 MiB RAM beginning at 1GiB */ |
| 400 | ret = test_noreserved(uts, 0x40000000, 5, 8); |
| 401 | if (ret) |
| 402 | return ret; |
| 403 | |
| 404 | /* simulate 512 MiB RAM beginning at 1.5GiB */ |
| 405 | return test_noreserved(uts, 0xE0000000, 5, 8); |
| 406 | } |
Simon Glass | 631d8a3 | 2023-10-01 19:15:21 -0600 | [diff] [blame] | 407 | LIB_TEST(lib_test_lmb_unaligned_size, 0); |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 408 | |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 409 | /* |
| 410 | * Simulate a RAM that starts at 0 and allocate down to address 0, which must |
| 411 | * fail as '0' means failure for the lmb_alloc functions. |
| 412 | */ |
| 413 | static int lib_test_lmb_at_0(struct unit_test_state *uts) |
| 414 | { |
| 415 | const phys_addr_t ram = 0; |
| 416 | const phys_size_t ram_size = 0x20000000; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 417 | struct lmb store; |
| 418 | struct alist *mem_lst, *used_lst; |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 419 | long ret; |
| 420 | phys_addr_t a, b; |
| 421 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 422 | ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst)); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 423 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 424 | ret = lmb_add(ram, ram_size); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 425 | ut_asserteq(ret, 0); |
| 426 | |
| 427 | /* allocate nearly everything */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 428 | a = lmb_alloc(ram_size - 4, 1); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 429 | ut_asserteq(a, ram + 4); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 430 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 431 | 0, 0, 0, 0); |
| 432 | /* allocate the rest */ |
| 433 | /* This should fail as the allocated address would be 0 */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 434 | b = lmb_alloc(4, 1); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 435 | ut_asserteq(b, 0); |
| 436 | /* check that this was an error by checking lmb */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 437 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 438 | 0, 0, 0, 0); |
| 439 | /* check that this was an error by freeing b */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 440 | ret = lmb_free(b, 4); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 441 | ut_asserteq(ret, -1); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 442 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4, |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 443 | 0, 0, 0, 0); |
| 444 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 445 | ret = lmb_free(a, ram_size - 4); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 446 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 447 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0); |
| 448 | |
| 449 | lmb_pop(&store); |
Simon Goldschmidt | a01ae0c | 2019-01-14 22:38:14 +0100 | [diff] [blame] | 450 | |
| 451 | return 0; |
| 452 | } |
Simon Glass | 631d8a3 | 2023-10-01 19:15:21 -0600 | [diff] [blame] | 453 | LIB_TEST(lib_test_lmb_at_0, 0); |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 454 | |
| 455 | /* Check that calling lmb_reserve with overlapping regions fails. */ |
| 456 | static int lib_test_lmb_overlapping_reserve(struct unit_test_state *uts) |
| 457 | { |
| 458 | const phys_addr_t ram = 0x40000000; |
| 459 | const phys_size_t ram_size = 0x20000000; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 460 | struct lmb store; |
| 461 | struct alist *mem_lst, *used_lst; |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 462 | long ret; |
| 463 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 464 | ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst)); |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 465 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 466 | ret = lmb_add(ram, ram_size); |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 467 | ut_asserteq(ret, 0); |
| 468 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 469 | ret = lmb_reserve(0x40010000, 0x10000); |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 470 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 471 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000, |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 472 | 0, 0, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 473 | |
| 474 | /* allocate overlapping region should return the coalesced count */ |
| 475 | ret = lmb_reserve(0x40011000, 0x10000); |
| 476 | ut_asserteq(ret, 1); |
| 477 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x11000, |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 478 | 0, 0, 0, 0); |
| 479 | /* allocate 3nd region */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 480 | ret = lmb_reserve(0x40030000, 0x10000); |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 481 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 482 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40010000, 0x11000, |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 483 | 0x40030000, 0x10000, 0, 0); |
Udit Kumar | 4a6105e | 2023-09-26 16:54:43 +0530 | [diff] [blame] | 484 | /* allocate 2nd region , This should coalesced all region into one */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 485 | ret = lmb_reserve(0x40020000, 0x10000); |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 486 | ut_assert(ret >= 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 487 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x30000, |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 488 | 0, 0, 0, 0); |
| 489 | |
Udit Kumar | 4a6105e | 2023-09-26 16:54:43 +0530 | [diff] [blame] | 490 | /* allocate 2nd region, which should be added as first region */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 491 | ret = lmb_reserve(0x40000000, 0x8000); |
Udit Kumar | 4a6105e | 2023-09-26 16:54:43 +0530 | [diff] [blame] | 492 | ut_assert(ret >= 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 493 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x8000, |
Udit Kumar | 4a6105e | 2023-09-26 16:54:43 +0530 | [diff] [blame] | 494 | 0x40010000, 0x30000, 0, 0); |
| 495 | |
| 496 | /* allocate 3rd region, coalesce with first and overlap with second */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 497 | ret = lmb_reserve(0x40008000, 0x10000); |
Udit Kumar | 4a6105e | 2023-09-26 16:54:43 +0530 | [diff] [blame] | 498 | ut_assert(ret >= 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 499 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40000000, 0x40000, |
Udit Kumar | 4a6105e | 2023-09-26 16:54:43 +0530 | [diff] [blame] | 500 | 0, 0, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 501 | |
| 502 | lmb_pop(&store); |
| 503 | |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 504 | return 0; |
| 505 | } |
Simon Glass | 631d8a3 | 2023-10-01 19:15:21 -0600 | [diff] [blame] | 506 | LIB_TEST(lib_test_lmb_overlapping_reserve, 0); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 507 | |
| 508 | /* |
| 509 | * Simulate 512 MiB RAM, reserve 3 blocks, allocate addresses in between. |
| 510 | * Expect addresses outside the memory range to fail. |
| 511 | */ |
| 512 | static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) |
| 513 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 514 | struct lmb store; |
| 515 | struct alist *mem_lst, *used_lst; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 516 | const phys_size_t ram_size = 0x20000000; |
| 517 | const phys_addr_t ram_end = ram + ram_size; |
| 518 | const phys_size_t alloc_addr_a = ram + 0x8000000; |
| 519 | const phys_size_t alloc_addr_b = ram + 0x8000000 * 2; |
| 520 | const phys_size_t alloc_addr_c = ram + 0x8000000 * 3; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 521 | long ret; |
| 522 | phys_addr_t a, b, c, d, e; |
| 523 | |
| 524 | /* check for overflow */ |
| 525 | ut_assert(ram_end == 0 || ram_end > ram); |
| 526 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 527 | ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst)); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 528 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 529 | ret = lmb_add(ram, ram_size); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 530 | ut_asserteq(ret, 0); |
| 531 | |
| 532 | /* reserve 3 blocks */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 533 | ret = lmb_reserve(alloc_addr_a, 0x10000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 534 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 535 | ret = lmb_reserve(alloc_addr_b, 0x10000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 536 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 537 | ret = lmb_reserve(alloc_addr_c, 0x10000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 538 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 539 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, alloc_addr_a, 0x10000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 540 | alloc_addr_b, 0x10000, alloc_addr_c, 0x10000); |
| 541 | |
| 542 | /* allocate blocks */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 543 | a = lmb_alloc_addr(ram, alloc_addr_a - ram); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 544 | ut_asserteq(a, ram); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 545 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, ram, 0x8010000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 546 | alloc_addr_b, 0x10000, alloc_addr_c, 0x10000); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 547 | b = lmb_alloc_addr(alloc_addr_a + 0x10000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 548 | alloc_addr_b - alloc_addr_a - 0x10000); |
| 549 | ut_asserteq(b, alloc_addr_a + 0x10000); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 550 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x10010000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 551 | alloc_addr_c, 0x10000, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 552 | c = lmb_alloc_addr(alloc_addr_b + 0x10000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 553 | alloc_addr_c - alloc_addr_b - 0x10000); |
| 554 | ut_asserteq(c, alloc_addr_b + 0x10000); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 555 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 556 | 0, 0, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 557 | d = lmb_alloc_addr(alloc_addr_c + 0x10000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 558 | ram_end - alloc_addr_c - 0x10000); |
| 559 | ut_asserteq(d, alloc_addr_c + 0x10000); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 560 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, ram_size, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 561 | 0, 0, 0, 0); |
| 562 | |
| 563 | /* allocating anything else should fail */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 564 | e = lmb_alloc(1, 1); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 565 | ut_asserteq(e, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 566 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, ram_size, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 567 | 0, 0, 0, 0); |
| 568 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 569 | ret = lmb_free(d, ram_end - alloc_addr_c - 0x10000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 570 | ut_asserteq(ret, 0); |
| 571 | |
| 572 | /* allocate at 3 points in free range */ |
| 573 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 574 | d = lmb_alloc_addr(ram_end - 4, 4); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 575 | ut_asserteq(d, ram_end - 4); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 576 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 577 | d, 4, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 578 | ret = lmb_free(d, 4); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 579 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 580 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 581 | 0, 0, 0, 0); |
| 582 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 583 | d = lmb_alloc_addr(ram_end - 128, 4); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 584 | ut_asserteq(d, ram_end - 128); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 585 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 586 | d, 4, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 587 | ret = lmb_free(d, 4); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 588 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 589 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 590 | 0, 0, 0, 0); |
| 591 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 592 | d = lmb_alloc_addr(alloc_addr_c + 0x10000, 4); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 593 | ut_asserteq(d, alloc_addr_c + 0x10000); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 594 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010004, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 595 | 0, 0, 0, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 596 | ret = lmb_free(d, 4); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 597 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 598 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 599 | 0, 0, 0, 0); |
| 600 | |
| 601 | /* allocate at the bottom */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 602 | ret = lmb_free(a, alloc_addr_a - ram); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 603 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 604 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram + 0x8000000, |
| 605 | 0x10010000, 0, 0, 0, 0); |
| 606 | |
| 607 | d = lmb_alloc_addr(ram, 4); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 608 | ut_asserteq(d, ram); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 609 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, d, 4, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 610 | ram + 0x8000000, 0x10010000, 0, 0); |
| 611 | |
| 612 | /* check that allocating outside memory fails */ |
| 613 | if (ram_end != 0) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 614 | ret = lmb_alloc_addr(ram_end, 1); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 615 | ut_asserteq(ret, 0); |
| 616 | } |
| 617 | if (ram != 0) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 618 | ret = lmb_alloc_addr(ram - 1, 1); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 619 | ut_asserteq(ret, 0); |
| 620 | } |
| 621 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 622 | lmb_pop(&store); |
| 623 | |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static int lib_test_lmb_alloc_addr(struct unit_test_state *uts) |
| 628 | { |
| 629 | int ret; |
| 630 | |
| 631 | /* simulate 512 MiB RAM beginning at 1GiB */ |
| 632 | ret = test_alloc_addr(uts, 0x40000000); |
| 633 | if (ret) |
| 634 | return ret; |
| 635 | |
| 636 | /* simulate 512 MiB RAM beginning at 1.5GiB */ |
| 637 | return test_alloc_addr(uts, 0xE0000000); |
| 638 | } |
Simon Glass | 631d8a3 | 2023-10-01 19:15:21 -0600 | [diff] [blame] | 639 | LIB_TEST(lib_test_lmb_alloc_addr, 0); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 640 | |
| 641 | /* Simulate 512 MiB RAM, reserve 3 blocks, check addresses in between */ |
| 642 | static int test_get_unreserved_size(struct unit_test_state *uts, |
| 643 | const phys_addr_t ram) |
| 644 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 645 | struct lmb store; |
| 646 | struct alist *mem_lst, *used_lst; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 647 | const phys_size_t ram_size = 0x20000000; |
| 648 | const phys_addr_t ram_end = ram + ram_size; |
| 649 | const phys_size_t alloc_addr_a = ram + 0x8000000; |
| 650 | const phys_size_t alloc_addr_b = ram + 0x8000000 * 2; |
| 651 | const phys_size_t alloc_addr_c = ram + 0x8000000 * 3; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 652 | long ret; |
| 653 | phys_size_t s; |
| 654 | |
| 655 | /* check for overflow */ |
| 656 | ut_assert(ram_end == 0 || ram_end > ram); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 657 | ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst)); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 658 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 659 | ret = lmb_add(ram, ram_size); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 660 | ut_asserteq(ret, 0); |
| 661 | |
| 662 | /* reserve 3 blocks */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 663 | ret = lmb_reserve(alloc_addr_a, 0x10000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 664 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 665 | ret = lmb_reserve(alloc_addr_b, 0x10000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 666 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 667 | ret = lmb_reserve(alloc_addr_c, 0x10000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 668 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 669 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, alloc_addr_a, 0x10000, |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 670 | alloc_addr_b, 0x10000, alloc_addr_c, 0x10000); |
| 671 | |
| 672 | /* check addresses in between blocks */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 673 | s = lmb_get_free_size(ram); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 674 | ut_asserteq(s, alloc_addr_a - ram); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 675 | s = lmb_get_free_size(ram + 0x10000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 676 | ut_asserteq(s, alloc_addr_a - ram - 0x10000); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 677 | s = lmb_get_free_size(alloc_addr_a - 4); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 678 | ut_asserteq(s, 4); |
| 679 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 680 | s = lmb_get_free_size(alloc_addr_a + 0x10000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 681 | ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x10000); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 682 | s = lmb_get_free_size(alloc_addr_a + 0x20000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 683 | ut_asserteq(s, alloc_addr_b - alloc_addr_a - 0x20000); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 684 | s = lmb_get_free_size(alloc_addr_b - 4); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 685 | ut_asserteq(s, 4); |
| 686 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 687 | s = lmb_get_free_size(alloc_addr_c + 0x10000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 688 | ut_asserteq(s, ram_end - alloc_addr_c - 0x10000); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 689 | s = lmb_get_free_size(alloc_addr_c + 0x20000); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 690 | ut_asserteq(s, ram_end - alloc_addr_c - 0x20000); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 691 | s = lmb_get_free_size(ram_end - 4); |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 692 | ut_asserteq(s, 4); |
| 693 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 694 | lmb_pop(&store); |
| 695 | |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 696 | return 0; |
| 697 | } |
| 698 | |
Simon Goldschmidt | 65304aa | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 699 | 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] | 700 | { |
| 701 | int ret; |
| 702 | |
| 703 | /* simulate 512 MiB RAM beginning at 1GiB */ |
| 704 | ret = test_get_unreserved_size(uts, 0x40000000); |
| 705 | if (ret) |
| 706 | return ret; |
| 707 | |
| 708 | /* simulate 512 MiB RAM beginning at 1.5GiB */ |
| 709 | return test_get_unreserved_size(uts, 0xE0000000); |
| 710 | } |
Simon Glass | 631d8a3 | 2023-10-01 19:15:21 -0600 | [diff] [blame] | 711 | LIB_TEST(lib_test_lmb_get_free_size, 0); |
Patrick Delaunay | a57b596 | 2021-03-10 10:16:30 +0100 | [diff] [blame] | 712 | |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 713 | static int lib_test_lmb_flags(struct unit_test_state *uts) |
| 714 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 715 | struct lmb store; |
| 716 | struct lmb_region *mem, *used; |
| 717 | struct alist *mem_lst, *used_lst; |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 718 | const phys_addr_t ram = 0x40000000; |
| 719 | const phys_size_t ram_size = 0x20000000; |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 720 | long ret; |
| 721 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 722 | ut_assertok(setup_lmb_test(uts, &store, &mem_lst, &used_lst)); |
| 723 | mem = mem_lst->data; |
| 724 | used = used_lst->data; |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 725 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 726 | ret = lmb_add(ram, ram_size); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 727 | ut_asserteq(ret, 0); |
| 728 | |
| 729 | /* reserve, same flag */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 730 | ret = lmb_reserve_flags(0x40010000, 0x10000, LMB_NOMAP); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 731 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 732 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000, |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 733 | 0, 0, 0, 0); |
| 734 | |
| 735 | /* reserve again, same flag */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 736 | ret = lmb_reserve_flags(0x40010000, 0x10000, LMB_NOMAP); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 737 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 738 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000, |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 739 | 0, 0, 0, 0); |
| 740 | |
| 741 | /* reserve again, new flag */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 742 | ret = lmb_reserve_flags(0x40010000, 0x10000, LMB_NONE); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 743 | ut_asserteq(ret, -1); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 744 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x10000, |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 745 | 0, 0, 0, 0); |
| 746 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 747 | ut_asserteq(lmb_is_nomap(&used[0]), 1); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 748 | |
| 749 | /* merge after */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 750 | ret = lmb_reserve_flags(0x40020000, 0x10000, LMB_NOMAP); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 751 | ut_asserteq(ret, 1); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 752 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40010000, 0x20000, |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 753 | 0, 0, 0, 0); |
| 754 | |
| 755 | /* merge before */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 756 | ret = lmb_reserve_flags(0x40000000, 0x10000, LMB_NOMAP); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 757 | ut_asserteq(ret, 1); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 758 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, 0x40000000, 0x30000, |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 759 | 0, 0, 0, 0); |
| 760 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 761 | ut_asserteq(lmb_is_nomap(&used[0]), 1); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 762 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 763 | ret = lmb_reserve_flags(0x40030000, 0x10000, LMB_NONE); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 764 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 765 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x30000, |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 766 | 0x40030000, 0x10000, 0, 0); |
| 767 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 768 | ut_asserteq(lmb_is_nomap(&used[0]), 1); |
| 769 | ut_asserteq(lmb_is_nomap(&used[1]), 0); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 770 | |
| 771 | /* test that old API use LMB_NONE */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 772 | ret = lmb_reserve(0x40040000, 0x10000); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 773 | ut_asserteq(ret, 1); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 774 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, 0x40000000, 0x30000, |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 775 | 0x40030000, 0x20000, 0, 0); |
| 776 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 777 | ut_asserteq(lmb_is_nomap(&used[0]), 1); |
| 778 | ut_asserteq(lmb_is_nomap(&used[1]), 0); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 779 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 780 | ret = lmb_reserve_flags(0x40070000, 0x10000, LMB_NOMAP); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 781 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 782 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, 0x40000000, 0x30000, |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 783 | 0x40030000, 0x20000, 0x40070000, 0x10000); |
| 784 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 785 | ret = lmb_reserve_flags(0x40050000, 0x10000, LMB_NOMAP); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 786 | ut_asserteq(ret, 0); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 787 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 4, 0x40000000, 0x30000, |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 788 | 0x40030000, 0x20000, 0x40050000, 0x10000); |
| 789 | |
| 790 | /* merge with 2 adjacent regions */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 791 | ret = lmb_reserve_flags(0x40060000, 0x10000, LMB_NOMAP); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 792 | ut_asserteq(ret, 2); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 793 | ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, 0x40000000, 0x30000, |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 794 | 0x40030000, 0x20000, 0x40050000, 0x30000); |
| 795 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 796 | ut_asserteq(lmb_is_nomap(&used[0]), 1); |
| 797 | ut_asserteq(lmb_is_nomap(&used[1]), 0); |
| 798 | ut_asserteq(lmb_is_nomap(&used[2]), 1); |
| 799 | |
| 800 | lmb_pop(&store); |
Patrick Delaunay | 7d08a3d | 2021-05-07 14:50:32 +0200 | [diff] [blame] | 801 | |
| 802 | return 0; |
| 803 | } |
Simon Glass | 631d8a3 | 2023-10-01 19:15:21 -0600 | [diff] [blame] | 804 | LIB_TEST(lib_test_lmb_flags, 0); |