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