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