Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Procedures for maintaining information about logical memory blocks. |
| 4 | * |
| 5 | * Peter Bergner, IBM Corp. June 2001. |
| 6 | * Copyright (C) 2001 Peter Bergner. |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 10 | #include <image.h> |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 11 | #include <lmb.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 14 | |
| 15 | #define LMB_ALLOC_ANYWHERE 0 |
| 16 | |
Tero Kristo | 97c418b | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 17 | void lmb_dump_all_force(struct lmb *lmb) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 18 | { |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 19 | unsigned long i; |
| 20 | |
Tero Kristo | 97c418b | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 21 | printf("lmb_dump_all:\n"); |
| 22 | printf(" memory.cnt = 0x%lx\n", lmb->memory.cnt); |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 23 | for (i = 0; i < lmb->memory.cnt; i++) { |
Tero Kristo | 97c418b | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 24 | printf(" memory.reg[0x%lx].base = 0x%llx\n", i, |
| 25 | (unsigned long long)lmb->memory.region[i].base); |
| 26 | printf(" .size = 0x%llx\n", |
| 27 | (unsigned long long)lmb->memory.region[i].size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 28 | } |
| 29 | |
Tero Kristo | 97c418b | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 30 | printf("\n reserved.cnt = 0x%lx\n", lmb->reserved.cnt); |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 31 | for (i = 0; i < lmb->reserved.cnt; i++) { |
Tero Kristo | 97c418b | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 32 | printf(" reserved.reg[0x%lx].base = 0x%llx\n", i, |
| 33 | (unsigned long long)lmb->reserved.region[i].base); |
| 34 | printf(" .size = 0x%llx\n", |
| 35 | (unsigned long long)lmb->reserved.region[i].size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 36 | } |
Tero Kristo | 97c418b | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void lmb_dump_all(struct lmb *lmb) |
| 40 | { |
| 41 | #ifdef DEBUG |
| 42 | lmb_dump_all_force(lmb); |
| 43 | #endif |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 44 | } |
| 45 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 46 | static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1, |
| 47 | phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 48 | { |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 49 | const phys_addr_t base1_end = base1 + size1 - 1; |
| 50 | const phys_addr_t base2_end = base2 + size2 - 1; |
| 51 | |
| 52 | return ((base1 <= base2_end) && (base2 <= base1_end)); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 53 | } |
| 54 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 55 | static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1, |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 56 | phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 57 | { |
| 58 | if (base2 == base1 + size1) |
| 59 | return 1; |
| 60 | else if (base1 == base2 + size2) |
| 61 | return -1; |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 66 | static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1, |
| 67 | unsigned long r2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 68 | { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 69 | phys_addr_t base1 = rgn->region[r1].base; |
| 70 | phys_size_t size1 = rgn->region[r1].size; |
| 71 | phys_addr_t base2 = rgn->region[r2].base; |
| 72 | phys_size_t size2 = rgn->region[r2].size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 73 | |
| 74 | return lmb_addrs_adjacent(base1, size1, base2, size2); |
| 75 | } |
| 76 | |
| 77 | static void lmb_remove_region(struct lmb_region *rgn, unsigned long r) |
| 78 | { |
| 79 | unsigned long i; |
| 80 | |
| 81 | for (i = r; i < rgn->cnt - 1; i++) { |
| 82 | rgn->region[i].base = rgn->region[i + 1].base; |
| 83 | rgn->region[i].size = rgn->region[i + 1].size; |
| 84 | } |
| 85 | rgn->cnt--; |
| 86 | } |
| 87 | |
| 88 | /* Assumption: base addr of region 1 < base addr of region 2 */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 89 | static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1, |
| 90 | unsigned long r2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 91 | { |
| 92 | rgn->region[r1].size += rgn->region[r2].size; |
| 93 | lmb_remove_region(rgn, r2); |
| 94 | } |
| 95 | |
| 96 | void lmb_init(struct lmb *lmb) |
| 97 | { |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame^] | 98 | #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS) |
Patrick Delaunay | cd831af | 2021-03-10 10:16:28 +0100 | [diff] [blame] | 99 | lmb->memory.max = CONFIG_LMB_MAX_REGIONS; |
| 100 | lmb->reserved.max = CONFIG_LMB_MAX_REGIONS; |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame^] | 101 | #else |
| 102 | lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS; |
| 103 | lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS; |
| 104 | lmb->memory.region = lmb->memory_regions; |
| 105 | lmb->reserved.region = lmb->reserved_regions; |
| 106 | #endif |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 107 | lmb->memory.cnt = 0; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 108 | lmb->reserved.cnt = 0; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 109 | } |
| 110 | |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 111 | static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob) |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 112 | { |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 113 | arch_lmb_reserve(lmb); |
| 114 | board_lmb_reserve(lmb); |
| 115 | |
| 116 | if (IMAGE_ENABLE_OF_LIBFDT && fdt_blob) |
| 117 | boot_fdt_add_mem_rsv_regions(lmb, fdt_blob); |
| 118 | } |
| 119 | |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 120 | /* Initialize the struct, add memory and call arch/board reserve functions */ |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 121 | void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob) |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 122 | { |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 123 | int i; |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 124 | |
| 125 | lmb_init(lmb); |
Stefan Roese | bbc8846 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 126 | |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 127 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 128 | if (bd->bi_dram[i].size) { |
| 129 | lmb_add(lmb, bd->bi_dram[i].start, |
| 130 | bd->bi_dram[i].size); |
| 131 | } |
| 132 | } |
Stefan Roese | bbc8846 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 133 | |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 134 | lmb_reserve_common(lmb, fdt_blob); |
| 135 | } |
| 136 | |
| 137 | /* Initialize the struct, add memory and call arch/board reserve functions */ |
| 138 | void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base, |
| 139 | phys_size_t size, void *fdt_blob) |
| 140 | { |
| 141 | lmb_init(lmb); |
| 142 | lmb_add(lmb, base, size); |
| 143 | lmb_reserve_common(lmb, fdt_blob); |
| 144 | } |
| 145 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 146 | /* This routine called with relocation disabled. */ |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 147 | static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 148 | { |
| 149 | unsigned long coalesced = 0; |
| 150 | long adjacent, i; |
| 151 | |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 152 | if (rgn->cnt == 0) { |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 153 | rgn->region[0].base = base; |
| 154 | rgn->region[0].size = size; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 155 | rgn->cnt = 1; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* First try and coalesce this LMB with another. */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 160 | for (i = 0; i < rgn->cnt; i++) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 161 | phys_addr_t rgnbase = rgn->region[i].base; |
| 162 | phys_size_t rgnsize = rgn->region[i].size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 163 | |
| 164 | if ((rgnbase == base) && (rgnsize == size)) |
| 165 | /* Already have this region, so we're done */ |
| 166 | return 0; |
| 167 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 168 | adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize); |
| 169 | if (adjacent > 0) { |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 170 | rgn->region[i].base -= size; |
| 171 | rgn->region[i].size += size; |
| 172 | coalesced++; |
| 173 | break; |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 174 | } else if (adjacent < 0) { |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 175 | rgn->region[i].size += size; |
| 176 | coalesced++; |
| 177 | break; |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 178 | } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { |
| 179 | /* regions overlap */ |
| 180 | return -1; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 184 | if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) { |
| 185 | lmb_coalesce_regions(rgn, i, i + 1); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 186 | coalesced++; |
| 187 | } |
| 188 | |
| 189 | if (coalesced) |
| 190 | return coalesced; |
Patrick Delaunay | 8c69fc2 | 2021-03-10 10:16:27 +0100 | [diff] [blame] | 191 | if (rgn->cnt >= rgn->max) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 192 | return -1; |
| 193 | |
| 194 | /* Couldn't coalesce the LMB, so add it to the sorted table. */ |
| 195 | for (i = rgn->cnt-1; i >= 0; i--) { |
| 196 | if (base < rgn->region[i].base) { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 197 | rgn->region[i + 1].base = rgn->region[i].base; |
| 198 | rgn->region[i + 1].size = rgn->region[i].size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 199 | } else { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 200 | rgn->region[i + 1].base = base; |
| 201 | rgn->region[i + 1].size = size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (base < rgn->region[0].base) { |
| 207 | rgn->region[0].base = base; |
| 208 | rgn->region[0].size = size; |
| 209 | } |
| 210 | |
| 211 | rgn->cnt++; |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | /* This routine may be called with relocation disabled. */ |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 217 | long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 218 | { |
| 219 | struct lmb_region *_rgn = &(lmb->memory); |
| 220 | |
| 221 | return lmb_add_region(_rgn, base, size); |
| 222 | } |
| 223 | |
Andy Fleming | 09d2a71 | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 224 | long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 225 | { |
| 226 | struct lmb_region *rgn = &(lmb->reserved); |
Andy Fleming | 09d2a71 | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 227 | phys_addr_t rgnbegin, rgnend; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 228 | phys_addr_t end = base + size - 1; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 229 | int i; |
| 230 | |
| 231 | rgnbegin = rgnend = 0; /* supress gcc warnings */ |
| 232 | |
| 233 | /* Find the region where (base, size) belongs to */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 234 | for (i = 0; i < rgn->cnt; i++) { |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 235 | rgnbegin = rgn->region[i].base; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 236 | rgnend = rgnbegin + rgn->region[i].size - 1; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 237 | |
| 238 | if ((rgnbegin <= base) && (end <= rgnend)) |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | /* Didn't find the region */ |
| 243 | if (i == rgn->cnt) |
| 244 | return -1; |
| 245 | |
| 246 | /* Check to see if we are removing entire region */ |
| 247 | if ((rgnbegin == base) && (rgnend == end)) { |
| 248 | lmb_remove_region(rgn, i); |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /* Check to see if region is matching at the front */ |
| 253 | if (rgnbegin == base) { |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 254 | rgn->region[i].base = end + 1; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 255 | rgn->region[i].size -= size; |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* Check to see if the region is matching at the end */ |
| 260 | if (rgnend == end) { |
| 261 | rgn->region[i].size -= size; |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * We need to split the entry - adjust the current one to the |
| 267 | * beginging of the hole and add the region after hole. |
| 268 | */ |
| 269 | rgn->region[i].size = base - rgn->region[i].base; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 270 | return lmb_add_region(rgn, end + 1, rgnend - end); |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 271 | } |
| 272 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 273 | long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 274 | { |
| 275 | struct lmb_region *_rgn = &(lmb->reserved); |
| 276 | |
| 277 | return lmb_add_region(_rgn, base, size); |
| 278 | } |
| 279 | |
Jeroen Hofstee | a020972 | 2014-10-08 22:57:39 +0200 | [diff] [blame] | 280 | static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base, |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 281 | phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 282 | { |
| 283 | unsigned long i; |
| 284 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 285 | for (i = 0; i < rgn->cnt; i++) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 286 | phys_addr_t rgnbase = rgn->region[i].base; |
| 287 | phys_size_t rgnsize = rgn->region[i].size; |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 288 | if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 289 | break; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | return (i < rgn->cnt) ? i : -1; |
| 293 | } |
| 294 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 295 | phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 296 | { |
| 297 | return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE); |
| 298 | } |
| 299 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 300 | phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 301 | { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 302 | phys_addr_t alloc; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 303 | |
| 304 | alloc = __lmb_alloc_base(lmb, size, align, max_addr); |
| 305 | |
| 306 | if (alloc == 0) |
| 307 | printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 308 | (ulong)size, (ulong)max_addr); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 309 | |
| 310 | return alloc; |
| 311 | } |
| 312 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 313 | static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 314 | { |
| 315 | return addr & ~(size - 1); |
| 316 | } |
| 317 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 318 | phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 319 | { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 320 | long i, rgn; |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 321 | phys_addr_t base = 0; |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 322 | phys_addr_t res_base; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 323 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 324 | for (i = lmb->memory.cnt - 1; i >= 0; i--) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 325 | phys_addr_t lmbbase = lmb->memory.region[i].base; |
| 326 | phys_size_t lmbsize = lmb->memory.region[i].size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 327 | |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 328 | if (lmbsize < size) |
| 329 | continue; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 330 | if (max_addr == LMB_ALLOC_ANYWHERE) |
| 331 | base = lmb_align_down(lmbbase + lmbsize - size, align); |
| 332 | else if (lmbbase < max_addr) { |
Stephen Warren | b6a010b | 2014-07-31 13:40:07 -0600 | [diff] [blame] | 333 | base = lmbbase + lmbsize; |
| 334 | if (base < lmbbase) |
| 335 | base = -1; |
| 336 | base = min(base, max_addr); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 337 | base = lmb_align_down(base - size, align); |
| 338 | } else |
| 339 | continue; |
| 340 | |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 341 | while (base && lmbbase <= base) { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 342 | rgn = lmb_overlaps_region(&lmb->reserved, base, size); |
| 343 | if (rgn < 0) { |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 344 | /* This area isn't reserved, take it */ |
| 345 | if (lmb_add_region(&lmb->reserved, base, |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 346 | size) < 0) |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 347 | return 0; |
| 348 | return base; |
| 349 | } |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 350 | res_base = lmb->reserved.region[rgn].base; |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 351 | if (res_base < size) |
| 352 | break; |
| 353 | base = lmb_align_down(res_base - size, align); |
| 354 | } |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 355 | } |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 356 | return 0; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 357 | } |
| 358 | |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 359 | /* |
| 360 | * Try to allocate a specific address range: must be in defined memory but not |
| 361 | * reserved |
| 362 | */ |
| 363 | phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
| 364 | { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 365 | long rgn; |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 366 | |
| 367 | /* Check if the requested address is in one of the memory regions */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 368 | rgn = lmb_overlaps_region(&lmb->memory, base, size); |
| 369 | if (rgn >= 0) { |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 370 | /* |
| 371 | * Check if the requested end address is in the same memory |
| 372 | * region we found. |
| 373 | */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 374 | if (lmb_addrs_overlap(lmb->memory.region[rgn].base, |
| 375 | lmb->memory.region[rgn].size, |
| 376 | base + size - 1, 1)) { |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 377 | /* ok, reserve the memory */ |
| 378 | if (lmb_reserve(lmb, base, size) >= 0) |
| 379 | return base; |
| 380 | } |
| 381 | } |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | /* Return number of bytes from a given address that are free */ |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 386 | phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr) |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 387 | { |
| 388 | int i; |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 389 | long rgn; |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 390 | |
| 391 | /* check if the requested address is in the memory regions */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 392 | rgn = lmb_overlaps_region(&lmb->memory, addr, 1); |
| 393 | if (rgn >= 0) { |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 394 | for (i = 0; i < lmb->reserved.cnt; i++) { |
| 395 | if (addr < lmb->reserved.region[i].base) { |
| 396 | /* first reserved range > requested address */ |
| 397 | return lmb->reserved.region[i].base - addr; |
| 398 | } |
| 399 | if (lmb->reserved.region[i].base + |
| 400 | lmb->reserved.region[i].size > addr) { |
| 401 | /* requested addr is in this reserved range */ |
| 402 | return 0; |
| 403 | } |
| 404 | } |
| 405 | /* if we come here: no reserved ranges above requested addr */ |
| 406 | return lmb->memory.region[lmb->memory.cnt - 1].base + |
| 407 | lmb->memory.region[lmb->memory.cnt - 1].size - addr; |
| 408 | } |
| 409 | return 0; |
| 410 | } |
| 411 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 412 | int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 413 | { |
| 414 | int i; |
| 415 | |
| 416 | for (i = 0; i < lmb->reserved.cnt; i++) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 417 | phys_addr_t upper = lmb->reserved.region[i].base + |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 418 | lmb->reserved.region[i].size - 1; |
| 419 | if ((addr >= lmb->reserved.region[i].base) && (addr <= upper)) |
| 420 | return 1; |
| 421 | } |
| 422 | return 0; |
| 423 | } |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 424 | |
Jeroen Hofstee | 0461dda | 2014-06-26 20:04:37 +0200 | [diff] [blame] | 425 | __weak void board_lmb_reserve(struct lmb *lmb) |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 426 | { |
| 427 | /* please define platform specific board_lmb_reserve() */ |
| 428 | } |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 429 | |
Jeroen Hofstee | 0461dda | 2014-06-26 20:04:37 +0200 | [diff] [blame] | 430 | __weak void arch_lmb_reserve(struct lmb *lmb) |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 431 | { |
| 432 | /* please define platform specific arch_lmb_reserve() */ |
| 433 | } |