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