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