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 | |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 9 | #include <efi_loader.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 10 | #include <image.h> |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 11 | #include <mapmem.h> |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 12 | #include <lmb.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 15 | |
Marek Vasut | 1274698 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Marek Vasut | bd994c0 | 2021-11-13 18:34:37 +0100 | [diff] [blame] | 17 | #include <asm/sections.h> |
Marek Vasut | 1274698 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 21 | #define LMB_ALLOC_ANYWHERE 0 |
| 22 | |
Patrick Delaunay | 358c778 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 23 | static void lmb_dump_region(struct lmb_region *rgn, char *name) |
| 24 | { |
| 25 | unsigned long long base, size, end; |
| 26 | enum lmb_flags flags; |
| 27 | int i; |
| 28 | |
Patrick Delaunay | c9eb37a | 2023-03-22 19:12:26 +0100 | [diff] [blame] | 29 | printf(" %s.cnt = 0x%lx / max = 0x%lx\n", name, rgn->cnt, rgn->max); |
Patrick Delaunay | 358c778 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 30 | |
| 31 | for (i = 0; i < rgn->cnt; i++) { |
| 32 | base = rgn->region[i].base; |
| 33 | size = rgn->region[i].size; |
| 34 | end = base + size - 1; |
| 35 | flags = rgn->region[i].flags; |
| 36 | |
| 37 | printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n", |
| 38 | name, i, base, end, size, flags); |
| 39 | } |
| 40 | } |
| 41 | |
Tero Kristo | 9996cea | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 42 | void lmb_dump_all_force(struct lmb *lmb) |
| 43 | { |
Tero Kristo | 9996cea | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 44 | printf("lmb_dump_all:\n"); |
Patrick Delaunay | 358c778 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 45 | lmb_dump_region(&lmb->memory, "memory"); |
| 46 | lmb_dump_region(&lmb->reserved, "reserved"); |
Tero Kristo | 9996cea | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 47 | } |
| 48 | |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 49 | void lmb_dump_all(struct lmb *lmb) |
| 50 | { |
| 51 | #ifdef DEBUG |
Tero Kristo | 9996cea | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 52 | lmb_dump_all_force(lmb); |
| 53 | #endif |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 54 | } |
| 55 | |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 56 | static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1, |
| 57 | phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 58 | { |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 59 | const phys_addr_t base1_end = base1 + size1 - 1; |
| 60 | const phys_addr_t base2_end = base2 + size2 - 1; |
| 61 | |
| 62 | return ((base1 <= base2_end) && (base2 <= base1_end)); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 63 | } |
| 64 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 65 | 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] | 66 | phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 67 | { |
| 68 | if (base2 == base1 + size1) |
| 69 | return 1; |
| 70 | else if (base1 == base2 + size2) |
| 71 | return -1; |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Udit Kumar | edb5824 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 76 | static long lmb_regions_overlap(struct lmb_region *rgn, unsigned long r1, |
| 77 | unsigned long r2) |
| 78 | { |
| 79 | phys_addr_t base1 = rgn->region[r1].base; |
| 80 | phys_size_t size1 = rgn->region[r1].size; |
| 81 | phys_addr_t base2 = rgn->region[r2].base; |
| 82 | phys_size_t size2 = rgn->region[r2].size; |
| 83 | |
| 84 | return lmb_addrs_overlap(base1, size1, base2, size2); |
| 85 | } |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 86 | static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1, |
| 87 | unsigned long r2) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 88 | { |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 89 | phys_addr_t base1 = rgn->region[r1].base; |
| 90 | phys_size_t size1 = rgn->region[r1].size; |
| 91 | phys_addr_t base2 = rgn->region[r2].base; |
| 92 | phys_size_t size2 = rgn->region[r2].size; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 93 | return lmb_addrs_adjacent(base1, size1, base2, size2); |
| 94 | } |
| 95 | |
| 96 | static void lmb_remove_region(struct lmb_region *rgn, unsigned long r) |
| 97 | { |
| 98 | unsigned long i; |
| 99 | |
| 100 | for (i = r; i < rgn->cnt - 1; i++) { |
| 101 | rgn->region[i].base = rgn->region[i + 1].base; |
| 102 | rgn->region[i].size = rgn->region[i + 1].size; |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 103 | rgn->region[i].flags = rgn->region[i + 1].flags; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 104 | } |
| 105 | rgn->cnt--; |
| 106 | } |
| 107 | |
| 108 | /* Assumption: base addr of region 1 < base addr of region 2 */ |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 109 | static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1, |
| 110 | unsigned long r2) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 111 | { |
| 112 | rgn->region[r1].size += rgn->region[r2].size; |
| 113 | lmb_remove_region(rgn, r2); |
| 114 | } |
| 115 | |
Udit Kumar | edb5824 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 116 | /*Assumption : base addr of region 1 < base addr of region 2*/ |
| 117 | static void lmb_fix_over_lap_regions(struct lmb_region *rgn, unsigned long r1, |
| 118 | unsigned long r2) |
| 119 | { |
| 120 | phys_addr_t base1 = rgn->region[r1].base; |
| 121 | phys_size_t size1 = rgn->region[r1].size; |
| 122 | phys_addr_t base2 = rgn->region[r2].base; |
| 123 | phys_size_t size2 = rgn->region[r2].size; |
| 124 | |
| 125 | if (base1 + size1 > base2 + size2) { |
| 126 | printf("This will not be a case any time\n"); |
| 127 | return; |
| 128 | } |
| 129 | rgn->region[r1].size = base2 + size2 - base1; |
| 130 | lmb_remove_region(rgn, r2); |
| 131 | } |
| 132 | |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 133 | void lmb_init(struct lmb *lmb) |
| 134 | { |
Patrick Delaunay | 6d66502 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 135 | #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS) |
Patrick Delaunay | 4fa0150 | 2021-03-10 10:16:28 +0100 | [diff] [blame] | 136 | lmb->memory.max = CONFIG_LMB_MAX_REGIONS; |
| 137 | lmb->reserved.max = CONFIG_LMB_MAX_REGIONS; |
Patrick Delaunay | 94c8da2 | 2023-03-22 19:12:25 +0100 | [diff] [blame] | 138 | #else |
Patrick Delaunay | 6d66502 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 139 | lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS; |
| 140 | lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS; |
| 141 | lmb->memory.region = lmb->memory_regions; |
| 142 | lmb->reserved.region = lmb->reserved_regions; |
| 143 | #endif |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 144 | lmb->memory.cnt = 0; |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 145 | lmb->reserved.cnt = 0; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 146 | } |
| 147 | |
Marek Vasut | 1274698 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 148 | void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align) |
| 149 | { |
| 150 | ulong bank_end; |
| 151 | int bank; |
| 152 | |
| 153 | /* |
| 154 | * Reserve memory from aligned address below the bottom of U-Boot stack |
| 155 | * until end of U-Boot area using LMB to prevent U-Boot from overwriting |
| 156 | * that memory. |
| 157 | */ |
| 158 | debug("## Current stack ends at 0x%08lx ", sp); |
| 159 | |
| 160 | /* adjust sp by 4K to be safe */ |
| 161 | sp -= align; |
| 162 | for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) { |
| 163 | if (!gd->bd->bi_dram[bank].size || |
| 164 | sp < gd->bd->bi_dram[bank].start) |
| 165 | continue; |
| 166 | /* Watch out for RAM at end of address space! */ |
| 167 | bank_end = gd->bd->bi_dram[bank].start + |
| 168 | gd->bd->bi_dram[bank].size - 1; |
| 169 | if (sp > bank_end) |
| 170 | continue; |
| 171 | if (bank_end > end) |
| 172 | bank_end = end - 1; |
| 173 | |
| 174 | lmb_reserve(lmb, sp, bank_end - sp + 1); |
Marek Vasut | bd994c0 | 2021-11-13 18:34:37 +0100 | [diff] [blame] | 175 | |
| 176 | if (gd->flags & GD_FLG_SKIP_RELOC) |
| 177 | lmb_reserve(lmb, (phys_addr_t)(uintptr_t)_start, gd->mon_len); |
| 178 | |
Marek Vasut | 1274698 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 183 | /** |
| 184 | * efi_lmb_reserve() - add reservations for EFI memory |
| 185 | * |
| 186 | * Add reservations for all EFI memory areas that are not |
| 187 | * EFI_CONVENTIONAL_MEMORY. |
| 188 | * |
| 189 | * @lmb: lmb environment |
| 190 | * Return: 0 on success, 1 on failure |
| 191 | */ |
| 192 | static __maybe_unused int efi_lmb_reserve(struct lmb *lmb) |
| 193 | { |
| 194 | struct efi_mem_desc *memmap = NULL, *map; |
| 195 | efi_uintn_t i, map_size = 0; |
| 196 | efi_status_t ret; |
| 197 | |
| 198 | ret = efi_get_memory_map_alloc(&map_size, &memmap); |
| 199 | if (ret != EFI_SUCCESS) |
| 200 | return 1; |
| 201 | |
| 202 | for (i = 0, map = memmap; i < map_size / sizeof(*map); ++map, ++i) { |
Sjoerd Simons | c5279ea | 2023-01-19 09:38:18 +0100 | [diff] [blame] | 203 | if (map->type != EFI_CONVENTIONAL_MEMORY) { |
| 204 | lmb_reserve_flags(lmb, |
| 205 | map_to_sysmem((void *)(uintptr_t) |
| 206 | map->physical_start), |
| 207 | map->num_pages * EFI_PAGE_SIZE, |
| 208 | map->type == EFI_RESERVED_MEMORY_TYPE |
| 209 | ? LMB_NOMAP : LMB_NONE); |
| 210 | } |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 211 | } |
| 212 | efi_free_pool(memmap); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 217 | static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob) |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 218 | { |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 219 | arch_lmb_reserve(lmb); |
| 220 | board_lmb_reserve(lmb); |
| 221 | |
Simon Glass | 0c303f9 | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 222 | if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob) |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 223 | boot_fdt_add_mem_rsv_regions(lmb, fdt_blob); |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 224 | |
| 225 | if (CONFIG_IS_ENABLED(EFI_LOADER)) |
| 226 | efi_lmb_reserve(lmb); |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 229 | /* Initialize the struct, add memory and call arch/board reserve functions */ |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 230 | 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] | 231 | { |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 232 | int i; |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 233 | |
| 234 | lmb_init(lmb); |
Stefan Roese | dfaf6a5 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 235 | |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 236 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 237 | if (bd->bi_dram[i].size) { |
| 238 | lmb_add(lmb, bd->bi_dram[i].start, |
| 239 | bd->bi_dram[i].size); |
| 240 | } |
| 241 | } |
Stefan Roese | dfaf6a5 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 242 | |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 243 | lmb_reserve_common(lmb, fdt_blob); |
| 244 | } |
| 245 | |
| 246 | /* Initialize the struct, add memory and call arch/board reserve functions */ |
| 247 | void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base, |
| 248 | phys_size_t size, void *fdt_blob) |
| 249 | { |
| 250 | lmb_init(lmb); |
| 251 | lmb_add(lmb, base, size); |
| 252 | lmb_reserve_common(lmb, fdt_blob); |
| 253 | } |
| 254 | |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 255 | /* This routine called with relocation disabled. */ |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 256 | static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base, |
| 257 | phys_size_t size, enum lmb_flags flags) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 258 | { |
| 259 | unsigned long coalesced = 0; |
| 260 | long adjacent, i; |
| 261 | |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 262 | if (rgn->cnt == 0) { |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 263 | rgn->region[0].base = base; |
| 264 | rgn->region[0].size = size; |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 265 | rgn->region[0].flags = flags; |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 266 | rgn->cnt = 1; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | /* First try and coalesce this LMB with another. */ |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 271 | for (i = 0; i < rgn->cnt; i++) { |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 272 | phys_addr_t rgnbase = rgn->region[i].base; |
| 273 | phys_size_t rgnsize = rgn->region[i].size; |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 274 | phys_size_t rgnflags = rgn->region[i].flags; |
Sjoerd Simons | 0d91c88 | 2023-02-12 16:07:05 +0100 | [diff] [blame] | 275 | phys_addr_t end = base + size - 1; |
| 276 | phys_addr_t rgnend = rgnbase + rgnsize - 1; |
Sjoerd Simons | 0d91c88 | 2023-02-12 16:07:05 +0100 | [diff] [blame] | 277 | if (rgnbase <= base && end <= rgnend) { |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 278 | if (flags == rgnflags) |
| 279 | /* Already have this region, so we're done */ |
| 280 | return 0; |
| 281 | else |
| 282 | return -1; /* regions with new flags */ |
| 283 | } |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 284 | |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 285 | adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize); |
| 286 | if (adjacent > 0) { |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 287 | if (flags != rgnflags) |
| 288 | break; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 289 | rgn->region[i].base -= size; |
| 290 | rgn->region[i].size += size; |
| 291 | coalesced++; |
| 292 | break; |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 293 | } else if (adjacent < 0) { |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 294 | if (flags != rgnflags) |
| 295 | break; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 296 | rgn->region[i].size += size; |
| 297 | coalesced++; |
| 298 | break; |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 299 | } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { |
| 300 | /* regions overlap */ |
| 301 | return -1; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
Udit Kumar | edb5824 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 305 | if (i < rgn->cnt - 1 && rgn->region[i].flags == rgn->region[i + 1].flags) { |
| 306 | if (lmb_regions_adjacent(rgn, i, i + 1)) { |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 307 | lmb_coalesce_regions(rgn, i, i + 1); |
| 308 | coalesced++; |
Udit Kumar | edb5824 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 309 | } else if (lmb_regions_overlap(rgn, i, i + 1)) { |
| 310 | /* fix overlapping area */ |
| 311 | lmb_fix_over_lap_regions(rgn, i, i + 1); |
| 312 | coalesced++; |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 313 | } |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | if (coalesced) |
| 317 | return coalesced; |
Patrick Delaunay | 00fd8da | 2021-03-10 10:16:27 +0100 | [diff] [blame] | 318 | if (rgn->cnt >= rgn->max) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 319 | return -1; |
| 320 | |
| 321 | /* Couldn't coalesce the LMB, so add it to the sorted table. */ |
| 322 | for (i = rgn->cnt-1; i >= 0; i--) { |
| 323 | if (base < rgn->region[i].base) { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 324 | rgn->region[i + 1].base = rgn->region[i].base; |
| 325 | rgn->region[i + 1].size = rgn->region[i].size; |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 326 | rgn->region[i + 1].flags = rgn->region[i].flags; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 327 | } else { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 328 | rgn->region[i + 1].base = base; |
| 329 | rgn->region[i + 1].size = size; |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 330 | rgn->region[i + 1].flags = flags; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if (base < rgn->region[0].base) { |
| 336 | rgn->region[0].base = base; |
| 337 | rgn->region[0].size = size; |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 338 | rgn->region[0].flags = flags; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | rgn->cnt++; |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 346 | static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, |
| 347 | phys_size_t size) |
| 348 | { |
| 349 | return lmb_add_region_flags(rgn, base, size, LMB_NONE); |
| 350 | } |
| 351 | |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 352 | /* This routine may be called with relocation disabled. */ |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 353 | 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] | 354 | { |
| 355 | struct lmb_region *_rgn = &(lmb->memory); |
| 356 | |
| 357 | return lmb_add_region(_rgn, base, size); |
| 358 | } |
| 359 | |
Andy Fleming | 98874ff | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 360 | 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] | 361 | { |
| 362 | struct lmb_region *rgn = &(lmb->reserved); |
Andy Fleming | 98874ff | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 363 | phys_addr_t rgnbegin, rgnend; |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 364 | phys_addr_t end = base + size - 1; |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 365 | int i; |
| 366 | |
| 367 | rgnbegin = rgnend = 0; /* supress gcc warnings */ |
| 368 | |
| 369 | /* Find the region where (base, size) belongs to */ |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 370 | for (i = 0; i < rgn->cnt; i++) { |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 371 | rgnbegin = rgn->region[i].base; |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 372 | rgnend = rgnbegin + rgn->region[i].size - 1; |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 373 | |
| 374 | if ((rgnbegin <= base) && (end <= rgnend)) |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | /* Didn't find the region */ |
| 379 | if (i == rgn->cnt) |
| 380 | return -1; |
| 381 | |
| 382 | /* Check to see if we are removing entire region */ |
| 383 | if ((rgnbegin == base) && (rgnend == end)) { |
| 384 | lmb_remove_region(rgn, i); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | /* Check to see if region is matching at the front */ |
| 389 | if (rgnbegin == base) { |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 390 | rgn->region[i].base = end + 1; |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 391 | rgn->region[i].size -= size; |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | /* Check to see if the region is matching at the end */ |
| 396 | if (rgnend == end) { |
| 397 | rgn->region[i].size -= size; |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * We need to split the entry - adjust the current one to the |
| 403 | * beginging of the hole and add the region after hole. |
| 404 | */ |
| 405 | rgn->region[i].size = base - rgn->region[i].base; |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 406 | return lmb_add_region_flags(rgn, end + 1, rgnend - end, |
| 407 | rgn->region[i].flags); |
| 408 | } |
| 409 | |
| 410 | long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size, |
| 411 | enum lmb_flags flags) |
| 412 | { |
| 413 | struct lmb_region *_rgn = &(lmb->reserved); |
| 414 | |
| 415 | return lmb_add_region_flags(_rgn, base, size, flags); |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 416 | } |
| 417 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 418 | 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] | 419 | { |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 420 | return lmb_reserve_flags(lmb, base, size, LMB_NONE); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 421 | } |
| 422 | |
Jeroen Hofstee | 750a6ff | 2014-10-08 22:57:39 +0200 | [diff] [blame] | 423 | 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] | 424 | phys_size_t size) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 425 | { |
| 426 | unsigned long i; |
| 427 | |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 428 | for (i = 0; i < rgn->cnt; i++) { |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 429 | phys_addr_t rgnbase = rgn->region[i].base; |
| 430 | phys_size_t rgnsize = rgn->region[i].size; |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 431 | if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 432 | break; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | return (i < rgn->cnt) ? i : -1; |
| 436 | } |
| 437 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 438 | 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] | 439 | { |
| 440 | return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE); |
| 441 | } |
| 442 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 443 | 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] | 444 | { |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 445 | phys_addr_t alloc; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 446 | |
| 447 | alloc = __lmb_alloc_base(lmb, size, align, max_addr); |
| 448 | |
| 449 | if (alloc == 0) |
| 450 | printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 451 | (ulong)size, (ulong)max_addr); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 452 | |
| 453 | return alloc; |
| 454 | } |
| 455 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 456 | 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] | 457 | { |
| 458 | return addr & ~(size - 1); |
| 459 | } |
| 460 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 461 | 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] | 462 | { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 463 | long i, rgn; |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 464 | phys_addr_t base = 0; |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 465 | phys_addr_t res_base; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 466 | |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 467 | for (i = lmb->memory.cnt - 1; i >= 0; i--) { |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 468 | phys_addr_t lmbbase = lmb->memory.region[i].base; |
| 469 | phys_size_t lmbsize = lmb->memory.region[i].size; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 470 | |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 471 | if (lmbsize < size) |
| 472 | continue; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 473 | if (max_addr == LMB_ALLOC_ANYWHERE) |
| 474 | base = lmb_align_down(lmbbase + lmbsize - size, align); |
| 475 | else if (lmbbase < max_addr) { |
Stephen Warren | ad3fda5 | 2014-07-31 13:40:07 -0600 | [diff] [blame] | 476 | base = lmbbase + lmbsize; |
| 477 | if (base < lmbbase) |
| 478 | base = -1; |
| 479 | base = min(base, max_addr); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 480 | base = lmb_align_down(base - size, align); |
| 481 | } else |
| 482 | continue; |
| 483 | |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 484 | while (base && lmbbase <= base) { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 485 | rgn = lmb_overlaps_region(&lmb->reserved, base, size); |
| 486 | if (rgn < 0) { |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 487 | /* This area isn't reserved, take it */ |
| 488 | if (lmb_add_region(&lmb->reserved, base, |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 489 | size) < 0) |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 490 | return 0; |
| 491 | return base; |
| 492 | } |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 493 | res_base = lmb->reserved.region[rgn].base; |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 494 | if (res_base < size) |
| 495 | break; |
| 496 | base = lmb_align_down(res_base - size, align); |
| 497 | } |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 498 | } |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 499 | return 0; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 500 | } |
| 501 | |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 502 | /* |
| 503 | * Try to allocate a specific address range: must be in defined memory but not |
| 504 | * reserved |
| 505 | */ |
| 506 | phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
| 507 | { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 508 | long rgn; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 509 | |
| 510 | /* Check if the requested address is in one of the memory regions */ |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 511 | rgn = lmb_overlaps_region(&lmb->memory, base, size); |
| 512 | if (rgn >= 0) { |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 513 | /* |
| 514 | * Check if the requested end address is in the same memory |
| 515 | * region we found. |
| 516 | */ |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 517 | if (lmb_addrs_overlap(lmb->memory.region[rgn].base, |
| 518 | lmb->memory.region[rgn].size, |
| 519 | base + size - 1, 1)) { |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 520 | /* ok, reserve the memory */ |
| 521 | if (lmb_reserve(lmb, base, size) >= 0) |
| 522 | return base; |
| 523 | } |
| 524 | } |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /* Return number of bytes from a given address that are free */ |
Simon Goldschmidt | 65304aa | 2019-01-21 20:29:55 +0100 | [diff] [blame] | 529 | 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] | 530 | { |
| 531 | int i; |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 532 | long rgn; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 533 | |
| 534 | /* check if the requested address is in the memory regions */ |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 535 | rgn = lmb_overlaps_region(&lmb->memory, addr, 1); |
| 536 | if (rgn >= 0) { |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 537 | for (i = 0; i < lmb->reserved.cnt; i++) { |
| 538 | if (addr < lmb->reserved.region[i].base) { |
| 539 | /* first reserved range > requested address */ |
| 540 | return lmb->reserved.region[i].base - addr; |
| 541 | } |
| 542 | if (lmb->reserved.region[i].base + |
| 543 | lmb->reserved.region[i].size > addr) { |
| 544 | /* requested addr is in this reserved range */ |
| 545 | return 0; |
| 546 | } |
| 547 | } |
| 548 | /* if we come here: no reserved ranges above requested addr */ |
| 549 | return lmb->memory.region[lmb->memory.cnt - 1].base + |
| 550 | lmb->memory.region[lmb->memory.cnt - 1].size - addr; |
| 551 | } |
| 552 | return 0; |
| 553 | } |
| 554 | |
Patrick Delaunay | e359a4a | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 555 | int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 556 | { |
| 557 | int i; |
| 558 | |
| 559 | for (i = 0; i < lmb->reserved.cnt; i++) { |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 560 | phys_addr_t upper = lmb->reserved.region[i].base + |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 561 | lmb->reserved.region[i].size - 1; |
| 562 | if ((addr >= lmb->reserved.region[i].base) && (addr <= upper)) |
Patrick Delaunay | e359a4a | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 563 | return (lmb->reserved.region[i].flags & flags) == flags; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 564 | } |
| 565 | return 0; |
| 566 | } |
Mike Frysinger | a16028d | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 567 | |
Patrick Delaunay | e359a4a | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 568 | int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr) |
| 569 | { |
| 570 | return lmb_is_reserved_flags(lmb, addr, LMB_NONE); |
| 571 | } |
| 572 | |
Jeroen Hofstee | 2c34f3f | 2014-06-26 20:04:37 +0200 | [diff] [blame] | 573 | __weak void board_lmb_reserve(struct lmb *lmb) |
Mike Frysinger | a16028d | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 574 | { |
| 575 | /* please define platform specific board_lmb_reserve() */ |
| 576 | } |
Mike Frysinger | a16028d | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 577 | |
Jeroen Hofstee | 2c34f3f | 2014-06-26 20:04:37 +0200 | [diff] [blame] | 578 | __weak void arch_lmb_reserve(struct lmb *lmb) |
Mike Frysinger | a16028d | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 579 | { |
| 580 | /* please define platform specific arch_lmb_reserve() */ |
| 581 | } |