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