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