Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | 4ed6552 | 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 | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 10 | #include <image.h> |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 11 | #include <lmb.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 14 | |
| 15 | #define LMB_ALLOC_ANYWHERE 0 |
| 16 | |
Tero Kristo | 9996cea | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 17 | void lmb_dump_all_force(struct lmb *lmb) |
| 18 | { |
| 19 | unsigned long i; |
| 20 | |
| 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); |
| 25 | for (i = 0; i < lmb->memory.cnt; i++) { |
| 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); |
| 30 | } |
| 31 | |
| 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); |
| 35 | for (i = 0; i < lmb->reserved.cnt; i++) { |
| 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); |
| 40 | } |
| 41 | } |
| 42 | |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 43 | void lmb_dump_all(struct lmb *lmb) |
| 44 | { |
| 45 | #ifdef DEBUG |
Tero Kristo | 9996cea | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 46 | lmb_dump_all_force(lmb); |
| 47 | #endif |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 48 | } |
| 49 | |
Simon Goldschmidt | e35d2a7 | 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 | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 52 | { |
Simon Goldschmidt | d67f33c | 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 | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 57 | } |
| 58 | |
Becky Bruce | 391fd93 | 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 | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 60 | phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 4ed6552 | 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 | e35d2a7 | 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 | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 72 | { |
Becky Bruce | 391fd93 | 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 | 4ed6552 | 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 | e35d2a7 | 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 | 4ed6552 | 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 | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 102 | lmb->memory.cnt = 0; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 103 | lmb->memory.size = 0; |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 104 | lmb->reserved.cnt = 0; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 105 | lmb->reserved.size = 0; |
| 106 | } |
| 107 | |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 108 | static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob) |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 109 | { |
Simon Goldschmidt | aa3c609 | 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 | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 117 | /* Initialize the struct, add memory and call arch/board reserve functions */ |
Masahiro Yamada | b75d8dc | 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 | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 119 | { |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 120 | int i; |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 121 | |
| 122 | lmb_init(lmb); |
Stefan Roese | dfaf6a5 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 123 | |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 124 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 125 | if (bd->bi_dram[i].size) { |
| 126 | lmb_add(lmb, bd->bi_dram[i].start, |
| 127 | bd->bi_dram[i].size); |
| 128 | } |
| 129 | } |
Stefan Roese | dfaf6a5 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 130 | |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 131 | lmb_reserve_common(lmb, fdt_blob); |
| 132 | } |
| 133 | |
| 134 | /* Initialize the struct, add memory and call arch/board reserve functions */ |
| 135 | void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base, |
| 136 | phys_size_t size, void *fdt_blob) |
| 137 | { |
| 138 | lmb_init(lmb); |
| 139 | lmb_add(lmb, base, size); |
| 140 | lmb_reserve_common(lmb, fdt_blob); |
| 141 | } |
| 142 | |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 143 | /* This routine called with relocation disabled. */ |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 144 | static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t size) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 145 | { |
| 146 | unsigned long coalesced = 0; |
| 147 | long adjacent, i; |
| 148 | |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 149 | if (rgn->cnt == 0) { |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 150 | rgn->region[0].base = base; |
| 151 | rgn->region[0].size = size; |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 152 | rgn->cnt = 1; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* First try and coalesce this LMB with another. */ |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 157 | for (i = 0; i < rgn->cnt; i++) { |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 158 | phys_addr_t rgnbase = rgn->region[i].base; |
| 159 | phys_size_t rgnsize = rgn->region[i].size; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 160 | |
| 161 | if ((rgnbase == base) && (rgnsize == size)) |
| 162 | /* Already have this region, so we're done */ |
| 163 | return 0; |
| 164 | |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 165 | adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize); |
| 166 | if (adjacent > 0) { |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 167 | rgn->region[i].base -= size; |
| 168 | rgn->region[i].size += size; |
| 169 | coalesced++; |
| 170 | break; |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 171 | } else if (adjacent < 0) { |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 172 | rgn->region[i].size += size; |
| 173 | coalesced++; |
| 174 | break; |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 175 | } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { |
| 176 | /* regions overlap */ |
| 177 | return -1; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 181 | if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) { |
| 182 | lmb_coalesce_regions(rgn, i, i + 1); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 183 | coalesced++; |
| 184 | } |
| 185 | |
| 186 | if (coalesced) |
| 187 | return coalesced; |
| 188 | if (rgn->cnt >= MAX_LMB_REGIONS) |
| 189 | return -1; |
| 190 | |
| 191 | /* Couldn't coalesce the LMB, so add it to the sorted table. */ |
| 192 | for (i = rgn->cnt-1; i >= 0; i--) { |
| 193 | if (base < rgn->region[i].base) { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 194 | rgn->region[i + 1].base = rgn->region[i].base; |
| 195 | rgn->region[i + 1].size = rgn->region[i].size; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 196 | } else { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 197 | rgn->region[i + 1].base = base; |
| 198 | rgn->region[i + 1].size = size; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if (base < rgn->region[0].base) { |
| 204 | rgn->region[0].base = base; |
| 205 | rgn->region[0].size = size; |
| 206 | } |
| 207 | |
| 208 | rgn->cnt++; |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /* This routine may be called with relocation disabled. */ |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 214 | long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 215 | { |
| 216 | struct lmb_region *_rgn = &(lmb->memory); |
| 217 | |
| 218 | return lmb_add_region(_rgn, base, size); |
| 219 | } |
| 220 | |
Andy Fleming | 98874ff | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 221 | long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 222 | { |
| 223 | struct lmb_region *rgn = &(lmb->reserved); |
Andy Fleming | 98874ff | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 224 | phys_addr_t rgnbegin, rgnend; |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 225 | phys_addr_t end = base + size - 1; |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 226 | int i; |
| 227 | |
| 228 | rgnbegin = rgnend = 0; /* supress gcc warnings */ |
| 229 | |
| 230 | /* Find the region where (base, size) belongs to */ |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 231 | for (i = 0; i < rgn->cnt; i++) { |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 232 | rgnbegin = rgn->region[i].base; |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 233 | rgnend = rgnbegin + rgn->region[i].size - 1; |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 234 | |
| 235 | if ((rgnbegin <= base) && (end <= rgnend)) |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | /* Didn't find the region */ |
| 240 | if (i == rgn->cnt) |
| 241 | return -1; |
| 242 | |
| 243 | /* Check to see if we are removing entire region */ |
| 244 | if ((rgnbegin == base) && (rgnend == end)) { |
| 245 | lmb_remove_region(rgn, i); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /* Check to see if region is matching at the front */ |
| 250 | if (rgnbegin == base) { |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 251 | rgn->region[i].base = end + 1; |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 252 | rgn->region[i].size -= size; |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* Check to see if the region is matching at the end */ |
| 257 | if (rgnend == end) { |
| 258 | rgn->region[i].size -= size; |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * We need to split the entry - adjust the current one to the |
| 264 | * beginging of the hole and add the region after hole. |
| 265 | */ |
| 266 | rgn->region[i].size = base - rgn->region[i].base; |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 267 | return lmb_add_region(rgn, end + 1, rgnend - end); |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 268 | } |
| 269 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 270 | long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 271 | { |
| 272 | struct lmb_region *_rgn = &(lmb->reserved); |
| 273 | |
| 274 | return lmb_add_region(_rgn, base, size); |
| 275 | } |
| 276 | |
Jeroen Hofstee | 750a6ff | 2014-10-08 22:57:39 +0200 | [diff] [blame] | 277 | static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base, |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 278 | phys_size_t size) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 279 | { |
| 280 | unsigned long i; |
| 281 | |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 282 | for (i = 0; i < rgn->cnt; i++) { |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 283 | phys_addr_t rgnbase = rgn->region[i].base; |
| 284 | phys_size_t rgnsize = rgn->region[i].size; |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 285 | if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 286 | break; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | return (i < rgn->cnt) ? i : -1; |
| 290 | } |
| 291 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 292 | phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 293 | { |
| 294 | return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE); |
| 295 | } |
| 296 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 297 | phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 298 | { |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 299 | phys_addr_t alloc; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 300 | |
| 301 | alloc = __lmb_alloc_base(lmb, size, align, max_addr); |
| 302 | |
| 303 | if (alloc == 0) |
| 304 | printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 305 | (ulong)size, (ulong)max_addr); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 306 | |
| 307 | return alloc; |
| 308 | } |
| 309 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 310 | static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 311 | { |
| 312 | return addr & ~(size - 1); |
| 313 | } |
| 314 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 315 | phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 316 | { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 317 | long i, rgn; |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 318 | phys_addr_t base = 0; |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 319 | phys_addr_t res_base; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 320 | |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 321 | for (i = lmb->memory.cnt - 1; i >= 0; i--) { |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 322 | phys_addr_t lmbbase = lmb->memory.region[i].base; |
| 323 | phys_size_t lmbsize = lmb->memory.region[i].size; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 324 | |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 325 | if (lmbsize < size) |
| 326 | continue; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 327 | if (max_addr == LMB_ALLOC_ANYWHERE) |
| 328 | base = lmb_align_down(lmbbase + lmbsize - size, align); |
| 329 | else if (lmbbase < max_addr) { |
Stephen Warren | ad3fda5 | 2014-07-31 13:40:07 -0600 | [diff] [blame] | 330 | base = lmbbase + lmbsize; |
| 331 | if (base < lmbbase) |
| 332 | base = -1; |
| 333 | base = min(base, max_addr); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 334 | base = lmb_align_down(base - size, align); |
| 335 | } else |
| 336 | continue; |
| 337 | |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 338 | while (base && lmbbase <= base) { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 339 | rgn = lmb_overlaps_region(&lmb->reserved, base, size); |
| 340 | if (rgn < 0) { |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 341 | /* This area isn't reserved, take it */ |
| 342 | if (lmb_add_region(&lmb->reserved, base, |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 343 | size) < 0) |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 344 | return 0; |
| 345 | return base; |
| 346 | } |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 347 | res_base = lmb->reserved.region[rgn].base; |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 348 | if (res_base < size) |
| 349 | break; |
| 350 | base = lmb_align_down(res_base - size, align); |
| 351 | } |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 352 | } |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 353 | return 0; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 354 | } |
| 355 | |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 356 | /* |
| 357 | * Try to allocate a specific address range: must be in defined memory but not |
| 358 | * reserved |
| 359 | */ |
| 360 | phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
| 361 | { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 362 | long rgn; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 363 | |
| 364 | /* Check if the requested address is in one of the memory regions */ |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 365 | rgn = lmb_overlaps_region(&lmb->memory, base, size); |
| 366 | if (rgn >= 0) { |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 367 | /* |
| 368 | * Check if the requested end address is in the same memory |
| 369 | * region we found. |
| 370 | */ |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 371 | if (lmb_addrs_overlap(lmb->memory.region[rgn].base, |
| 372 | lmb->memory.region[rgn].size, |
| 373 | base + size - 1, 1)) { |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 374 | /* ok, reserve the memory */ |
| 375 | if (lmb_reserve(lmb, base, size) >= 0) |
| 376 | return base; |
| 377 | } |
| 378 | } |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | /* Return number of bytes from a given address that are free */ |
Simon Goldschmidt | 65304aa | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 383 | phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr) |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 384 | { |
| 385 | int i; |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 386 | long rgn; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 387 | |
| 388 | /* check if the requested address is in the memory regions */ |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 389 | rgn = lmb_overlaps_region(&lmb->memory, addr, 1); |
| 390 | if (rgn >= 0) { |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 391 | for (i = 0; i < lmb->reserved.cnt; i++) { |
| 392 | if (addr < lmb->reserved.region[i].base) { |
| 393 | /* first reserved range > requested address */ |
| 394 | return lmb->reserved.region[i].base - addr; |
| 395 | } |
| 396 | if (lmb->reserved.region[i].base + |
| 397 | lmb->reserved.region[i].size > addr) { |
| 398 | /* requested addr is in this reserved range */ |
| 399 | return 0; |
| 400 | } |
| 401 | } |
| 402 | /* if we come here: no reserved ranges above requested addr */ |
| 403 | return lmb->memory.region[lmb->memory.cnt - 1].base + |
| 404 | lmb->memory.region[lmb->memory.cnt - 1].size - addr; |
| 405 | } |
| 406 | return 0; |
| 407 | } |
| 408 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 409 | int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 410 | { |
| 411 | int i; |
| 412 | |
| 413 | for (i = 0; i < lmb->reserved.cnt; i++) { |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 414 | phys_addr_t upper = lmb->reserved.region[i].base + |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 415 | lmb->reserved.region[i].size - 1; |
| 416 | if ((addr >= lmb->reserved.region[i].base) && (addr <= upper)) |
| 417 | return 1; |
| 418 | } |
| 419 | return 0; |
| 420 | } |
Mike Frysinger | a16028d | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 421 | |
Jeroen Hofstee | 2c34f3f | 2014-06-26 20:04:37 +0200 | [diff] [blame] | 422 | __weak void board_lmb_reserve(struct lmb *lmb) |
Mike Frysinger | a16028d | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 423 | { |
| 424 | /* please define platform specific board_lmb_reserve() */ |
| 425 | } |
Mike Frysinger | a16028d | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 426 | |
Jeroen Hofstee | 2c34f3f | 2014-06-26 20:04:37 +0200 | [diff] [blame] | 427 | __weak void arch_lmb_reserve(struct lmb *lmb) |
Mike Frysinger | a16028d | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 428 | { |
| 429 | /* please define platform specific arch_lmb_reserve() */ |
| 430 | } |