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