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