Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Procedures for maintaining information about logical memory blocks. |
| 4 | * |
| 5 | * Peter Bergner, IBM Corp. June 2001. |
| 6 | * Copyright (C) 2001 Peter Bergner. |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 10 | #include <image.h> |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 11 | #include <lmb.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 14 | |
Marek Vasut | a2eec02 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 15 | #include <asm/global_data.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 19 | #define LMB_ALLOC_ANYWHERE 0 |
| 20 | |
Patrick Delaunay | 4812002 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 21 | static void lmb_dump_region(struct lmb_region *rgn, char *name) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 22 | { |
Patrick Delaunay | 4812002 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 23 | unsigned long long base, size, end; |
| 24 | enum lmb_flags flags; |
| 25 | int i; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 26 | |
Patrick Delaunay | 4812002 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 27 | printf(" %s.cnt = 0x%lx\n", name, rgn->cnt); |
| 28 | |
| 29 | for (i = 0; i < rgn->cnt; i++) { |
| 30 | base = rgn->region[i].base; |
| 31 | size = rgn->region[i].size; |
| 32 | end = base + size - 1; |
| 33 | flags = rgn->region[i].flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 34 | |
Patrick Delaunay | 4812002 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 35 | printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n", |
| 36 | name, i, base, end, size, flags); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 37 | } |
Tero Kristo | 97c418b | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 38 | } |
| 39 | |
Patrick Delaunay | 4812002 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 40 | void lmb_dump_all_force(struct lmb *lmb) |
| 41 | { |
| 42 | printf("lmb_dump_all:\n"); |
| 43 | lmb_dump_region(&lmb->memory, "memory"); |
| 44 | lmb_dump_region(&lmb->reserved, "reserved"); |
| 45 | } |
| 46 | |
Tero Kristo | 97c418b | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 47 | void lmb_dump_all(struct lmb *lmb) |
| 48 | { |
| 49 | #ifdef DEBUG |
| 50 | lmb_dump_all_force(lmb); |
| 51 | #endif |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 52 | } |
| 53 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 54 | static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1, |
| 55 | phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 56 | { |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 57 | const phys_addr_t base1_end = base1 + size1 - 1; |
| 58 | const phys_addr_t base2_end = base2 + size2 - 1; |
| 59 | |
| 60 | return ((base1 <= base2_end) && (base2 <= base1_end)); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 61 | } |
| 62 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 63 | static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1, |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 64 | phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 65 | { |
| 66 | if (base2 == base1 + size1) |
| 67 | return 1; |
| 68 | else if (base1 == base2 + size2) |
| 69 | return -1; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 74 | static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1, |
| 75 | unsigned long r2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 76 | { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 77 | phys_addr_t base1 = rgn->region[r1].base; |
| 78 | phys_size_t size1 = rgn->region[r1].size; |
| 79 | phys_addr_t base2 = rgn->region[r2].base; |
| 80 | phys_size_t size2 = rgn->region[r2].size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 81 | |
| 82 | return lmb_addrs_adjacent(base1, size1, base2, size2); |
| 83 | } |
| 84 | |
| 85 | static void lmb_remove_region(struct lmb_region *rgn, unsigned long r) |
| 86 | { |
| 87 | unsigned long i; |
| 88 | |
| 89 | for (i = r; i < rgn->cnt - 1; i++) { |
| 90 | rgn->region[i].base = rgn->region[i + 1].base; |
| 91 | rgn->region[i].size = rgn->region[i + 1].size; |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 92 | rgn->region[i].flags = rgn->region[i + 1].flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 93 | } |
| 94 | rgn->cnt--; |
| 95 | } |
| 96 | |
| 97 | /* Assumption: base addr of region 1 < base addr of region 2 */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 98 | static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1, |
| 99 | unsigned long r2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 100 | { |
| 101 | rgn->region[r1].size += rgn->region[r2].size; |
| 102 | lmb_remove_region(rgn, r2); |
| 103 | } |
| 104 | |
| 105 | void lmb_init(struct lmb *lmb) |
| 106 | { |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 107 | #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS) |
Patrick Delaunay | cd831af | 2021-03-10 10:16:28 +0100 | [diff] [blame] | 108 | lmb->memory.max = CONFIG_LMB_MAX_REGIONS; |
| 109 | lmb->reserved.max = CONFIG_LMB_MAX_REGIONS; |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 110 | #else |
| 111 | lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS; |
| 112 | lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS; |
| 113 | lmb->memory.region = lmb->memory_regions; |
| 114 | lmb->reserved.region = lmb->reserved_regions; |
| 115 | #endif |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 116 | lmb->memory.cnt = 0; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 117 | lmb->reserved.cnt = 0; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 118 | } |
| 119 | |
Marek Vasut | a2eec02 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 120 | void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align) |
| 121 | { |
| 122 | ulong bank_end; |
| 123 | int bank; |
| 124 | |
| 125 | /* |
| 126 | * Reserve memory from aligned address below the bottom of U-Boot stack |
| 127 | * until end of U-Boot area using LMB to prevent U-Boot from overwriting |
| 128 | * that memory. |
| 129 | */ |
| 130 | debug("## Current stack ends at 0x%08lx ", sp); |
| 131 | |
| 132 | /* adjust sp by 4K to be safe */ |
| 133 | sp -= align; |
| 134 | for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) { |
| 135 | if (!gd->bd->bi_dram[bank].size || |
| 136 | sp < gd->bd->bi_dram[bank].start) |
| 137 | continue; |
| 138 | /* Watch out for RAM at end of address space! */ |
| 139 | bank_end = gd->bd->bi_dram[bank].start + |
| 140 | gd->bd->bi_dram[bank].size - 1; |
| 141 | if (sp > bank_end) |
| 142 | continue; |
| 143 | if (bank_end > end) |
| 144 | bank_end = end - 1; |
| 145 | |
| 146 | lmb_reserve(lmb, sp, bank_end - sp + 1); |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 151 | static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob) |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 152 | { |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 153 | arch_lmb_reserve(lmb); |
| 154 | board_lmb_reserve(lmb); |
| 155 | |
Simon Glass | 85c057e | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 156 | if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob) |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 157 | boot_fdt_add_mem_rsv_regions(lmb, fdt_blob); |
| 158 | } |
| 159 | |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 160 | /* Initialize the struct, add memory and call arch/board reserve functions */ |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 161 | void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob) |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 162 | { |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 163 | int i; |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 164 | |
| 165 | lmb_init(lmb); |
Stefan Roese | bbc8846 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 166 | |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 167 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 168 | if (bd->bi_dram[i].size) { |
| 169 | lmb_add(lmb, bd->bi_dram[i].start, |
| 170 | bd->bi_dram[i].size); |
| 171 | } |
| 172 | } |
Stefan Roese | bbc8846 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 173 | |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 174 | lmb_reserve_common(lmb, fdt_blob); |
| 175 | } |
| 176 | |
| 177 | /* Initialize the struct, add memory and call arch/board reserve functions */ |
| 178 | void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base, |
| 179 | phys_size_t size, void *fdt_blob) |
| 180 | { |
| 181 | lmb_init(lmb); |
| 182 | lmb_add(lmb, base, size); |
| 183 | lmb_reserve_common(lmb, fdt_blob); |
| 184 | } |
| 185 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 186 | /* This routine called with relocation disabled. */ |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 187 | static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base, |
| 188 | phys_size_t size, enum lmb_flags flags) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 189 | { |
| 190 | unsigned long coalesced = 0; |
| 191 | long adjacent, i; |
| 192 | |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 193 | if (rgn->cnt == 0) { |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 194 | rgn->region[0].base = base; |
| 195 | rgn->region[0].size = size; |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 196 | rgn->region[0].flags = flags; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 197 | rgn->cnt = 1; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /* First try and coalesce this LMB with another. */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 202 | for (i = 0; i < rgn->cnt; i++) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 203 | phys_addr_t rgnbase = rgn->region[i].base; |
| 204 | phys_size_t rgnsize = rgn->region[i].size; |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 205 | phys_size_t rgnflags = rgn->region[i].flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 206 | |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 207 | if (rgnbase == base && rgnsize == size) { |
| 208 | if (flags == rgnflags) |
| 209 | /* Already have this region, so we're done */ |
| 210 | return 0; |
| 211 | else |
| 212 | return -1; /* regions with new flags */ |
| 213 | } |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 214 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 215 | adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize); |
| 216 | if (adjacent > 0) { |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 217 | if (flags != rgnflags) |
| 218 | break; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 219 | rgn->region[i].base -= size; |
| 220 | rgn->region[i].size += size; |
| 221 | coalesced++; |
| 222 | break; |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 223 | } else if (adjacent < 0) { |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 224 | if (flags != rgnflags) |
| 225 | break; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 226 | rgn->region[i].size += size; |
| 227 | coalesced++; |
| 228 | break; |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 229 | } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { |
| 230 | /* regions overlap */ |
| 231 | return -1; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 235 | if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) { |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 236 | if (rgn->region[i].flags == rgn->region[i + 1].flags) { |
| 237 | lmb_coalesce_regions(rgn, i, i + 1); |
| 238 | coalesced++; |
| 239 | } |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | if (coalesced) |
| 243 | return coalesced; |
Patrick Delaunay | 8c69fc2 | 2021-03-10 10:16:27 +0100 | [diff] [blame] | 244 | if (rgn->cnt >= rgn->max) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 245 | return -1; |
| 246 | |
| 247 | /* Couldn't coalesce the LMB, so add it to the sorted table. */ |
| 248 | for (i = rgn->cnt-1; i >= 0; i--) { |
| 249 | if (base < rgn->region[i].base) { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 250 | rgn->region[i + 1].base = rgn->region[i].base; |
| 251 | rgn->region[i + 1].size = rgn->region[i].size; |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 252 | rgn->region[i + 1].flags = rgn->region[i].flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 253 | } else { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 254 | rgn->region[i + 1].base = base; |
| 255 | rgn->region[i + 1].size = size; |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 256 | rgn->region[i + 1].flags = flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (base < rgn->region[0].base) { |
| 262 | rgn->region[0].base = base; |
| 263 | rgn->region[0].size = size; |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 264 | rgn->region[0].flags = flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | rgn->cnt++; |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 272 | static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, |
| 273 | phys_size_t size) |
| 274 | { |
| 275 | return lmb_add_region_flags(rgn, base, size, LMB_NONE); |
| 276 | } |
| 277 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 278 | /* This routine may be called with relocation disabled. */ |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 279 | long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 280 | { |
| 281 | struct lmb_region *_rgn = &(lmb->memory); |
| 282 | |
| 283 | return lmb_add_region(_rgn, base, size); |
| 284 | } |
| 285 | |
Andy Fleming | 09d2a71 | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 286 | long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 287 | { |
| 288 | struct lmb_region *rgn = &(lmb->reserved); |
Andy Fleming | 09d2a71 | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 289 | phys_addr_t rgnbegin, rgnend; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 290 | phys_addr_t end = base + size - 1; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 291 | int i; |
| 292 | |
| 293 | rgnbegin = rgnend = 0; /* supress gcc warnings */ |
| 294 | |
| 295 | /* Find the region where (base, size) belongs to */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 296 | for (i = 0; i < rgn->cnt; i++) { |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 297 | rgnbegin = rgn->region[i].base; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 298 | rgnend = rgnbegin + rgn->region[i].size - 1; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 299 | |
| 300 | if ((rgnbegin <= base) && (end <= rgnend)) |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | /* Didn't find the region */ |
| 305 | if (i == rgn->cnt) |
| 306 | return -1; |
| 307 | |
| 308 | /* Check to see if we are removing entire region */ |
| 309 | if ((rgnbegin == base) && (rgnend == end)) { |
| 310 | lmb_remove_region(rgn, i); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /* Check to see if region is matching at the front */ |
| 315 | if (rgnbegin == base) { |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 316 | rgn->region[i].base = end + 1; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 317 | rgn->region[i].size -= size; |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | /* Check to see if the region is matching at the end */ |
| 322 | if (rgnend == end) { |
| 323 | rgn->region[i].size -= size; |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * We need to split the entry - adjust the current one to the |
| 329 | * beginging of the hole and add the region after hole. |
| 330 | */ |
| 331 | rgn->region[i].size = base - rgn->region[i].base; |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 332 | return lmb_add_region_flags(rgn, end + 1, rgnend - end, |
| 333 | rgn->region[i].flags); |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 336 | long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size, |
| 337 | enum lmb_flags flags) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 338 | { |
| 339 | struct lmb_region *_rgn = &(lmb->reserved); |
| 340 | |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 341 | return lmb_add_region_flags(_rgn, base, size, flags); |
| 342 | } |
| 343 | |
| 344 | long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
| 345 | { |
| 346 | return lmb_reserve_flags(lmb, base, size, LMB_NONE); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 347 | } |
| 348 | |
Jeroen Hofstee | a020972 | 2014-10-08 22:57:39 +0200 | [diff] [blame] | 349 | static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base, |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 350 | phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 351 | { |
| 352 | unsigned long i; |
| 353 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 354 | for (i = 0; i < rgn->cnt; i++) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 355 | phys_addr_t rgnbase = rgn->region[i].base; |
| 356 | phys_size_t rgnsize = rgn->region[i].size; |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 357 | if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 358 | break; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | return (i < rgn->cnt) ? i : -1; |
| 362 | } |
| 363 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 364 | phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 365 | { |
| 366 | return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE); |
| 367 | } |
| 368 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 369 | phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 370 | { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 371 | phys_addr_t alloc; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 372 | |
| 373 | alloc = __lmb_alloc_base(lmb, size, align, max_addr); |
| 374 | |
| 375 | if (alloc == 0) |
| 376 | printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 377 | (ulong)size, (ulong)max_addr); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 378 | |
| 379 | return alloc; |
| 380 | } |
| 381 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 382 | static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 383 | { |
| 384 | return addr & ~(size - 1); |
| 385 | } |
| 386 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 387 | phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 388 | { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 389 | long i, rgn; |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 390 | phys_addr_t base = 0; |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 391 | phys_addr_t res_base; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 392 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 393 | for (i = lmb->memory.cnt - 1; i >= 0; i--) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 394 | phys_addr_t lmbbase = lmb->memory.region[i].base; |
| 395 | phys_size_t lmbsize = lmb->memory.region[i].size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 396 | |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 397 | if (lmbsize < size) |
| 398 | continue; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 399 | if (max_addr == LMB_ALLOC_ANYWHERE) |
| 400 | base = lmb_align_down(lmbbase + lmbsize - size, align); |
| 401 | else if (lmbbase < max_addr) { |
Stephen Warren | b6a010b | 2014-07-31 13:40:07 -0600 | [diff] [blame] | 402 | base = lmbbase + lmbsize; |
| 403 | if (base < lmbbase) |
| 404 | base = -1; |
| 405 | base = min(base, max_addr); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 406 | base = lmb_align_down(base - size, align); |
| 407 | } else |
| 408 | continue; |
| 409 | |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 410 | while (base && lmbbase <= base) { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 411 | rgn = lmb_overlaps_region(&lmb->reserved, base, size); |
| 412 | if (rgn < 0) { |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 413 | /* This area isn't reserved, take it */ |
| 414 | if (lmb_add_region(&lmb->reserved, base, |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 415 | size) < 0) |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 416 | return 0; |
| 417 | return base; |
| 418 | } |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 419 | res_base = lmb->reserved.region[rgn].base; |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 420 | if (res_base < size) |
| 421 | break; |
| 422 | base = lmb_align_down(res_base - size, align); |
| 423 | } |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 424 | } |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 425 | return 0; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 426 | } |
| 427 | |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 428 | /* |
| 429 | * Try to allocate a specific address range: must be in defined memory but not |
| 430 | * reserved |
| 431 | */ |
| 432 | phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
| 433 | { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 434 | long rgn; |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 435 | |
| 436 | /* Check if the requested address is in one of the memory regions */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 437 | rgn = lmb_overlaps_region(&lmb->memory, base, size); |
| 438 | if (rgn >= 0) { |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 439 | /* |
| 440 | * Check if the requested end address is in the same memory |
| 441 | * region we found. |
| 442 | */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 443 | if (lmb_addrs_overlap(lmb->memory.region[rgn].base, |
| 444 | lmb->memory.region[rgn].size, |
| 445 | base + size - 1, 1)) { |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 446 | /* ok, reserve the memory */ |
| 447 | if (lmb_reserve(lmb, base, size) >= 0) |
| 448 | return base; |
| 449 | } |
| 450 | } |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | /* Return number of bytes from a given address that are free */ |
Simon Goldschmidt | 7510a56 | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 455 | phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr) |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 456 | { |
| 457 | int i; |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 458 | long rgn; |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 459 | |
| 460 | /* check if the requested address is in the memory regions */ |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 461 | rgn = lmb_overlaps_region(&lmb->memory, addr, 1); |
| 462 | if (rgn >= 0) { |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 463 | for (i = 0; i < lmb->reserved.cnt; i++) { |
| 464 | if (addr < lmb->reserved.region[i].base) { |
| 465 | /* first reserved range > requested address */ |
| 466 | return lmb->reserved.region[i].base - addr; |
| 467 | } |
| 468 | if (lmb->reserved.region[i].base + |
| 469 | lmb->reserved.region[i].size > addr) { |
| 470 | /* requested addr is in this reserved range */ |
| 471 | return 0; |
| 472 | } |
| 473 | } |
| 474 | /* if we come here: no reserved ranges above requested addr */ |
| 475 | return lmb->memory.region[lmb->memory.cnt - 1].base + |
| 476 | lmb->memory.region[lmb->memory.cnt - 1].size - addr; |
| 477 | } |
| 478 | return 0; |
| 479 | } |
| 480 | |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 481 | int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 482 | { |
| 483 | int i; |
| 484 | |
| 485 | for (i = 0; i < lmb->reserved.cnt; i++) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 486 | phys_addr_t upper = lmb->reserved.region[i].base + |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 487 | lmb->reserved.region[i].size - 1; |
| 488 | if ((addr >= lmb->reserved.region[i].base) && (addr <= upper)) |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 489 | return (lmb->reserved.region[i].flags & flags) == flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 490 | } |
| 491 | return 0; |
| 492 | } |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 493 | |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 494 | int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr) |
| 495 | { |
| 496 | return lmb_is_reserved_flags(lmb, addr, LMB_NONE); |
| 497 | } |
| 498 | |
Jeroen Hofstee | 0461dda | 2014-06-26 20:04:37 +0200 | [diff] [blame] | 499 | __weak void board_lmb_reserve(struct lmb *lmb) |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 500 | { |
| 501 | /* please define platform specific board_lmb_reserve() */ |
| 502 | } |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 503 | |
Jeroen Hofstee | 0461dda | 2014-06-26 20:04:37 +0200 | [diff] [blame] | 504 | __weak void arch_lmb_reserve(struct lmb *lmb) |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 505 | { |
| 506 | /* please define platform specific arch_lmb_reserve() */ |
| 507 | } |