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 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 9 | #include <alist.h> |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 10 | #include <efi_loader.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 11 | #include <image.h> |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 12 | #include <mapmem.h> |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 13 | #include <lmb.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Sughosh Ganu | f4fb154 | 2024-08-26 17:29:24 +0530 | [diff] [blame] | 16 | #include <spl.h> |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 17 | |
Marek Vasut | 1274698 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Marek Vasut | bd994c0 | 2021-11-13 18:34:37 +0100 | [diff] [blame] | 19 | #include <asm/sections.h> |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Sughosh Ganu | 6534d26 | 2024-08-26 17:29:30 +0530 | [diff] [blame^] | 21 | #include <linux/sizes.h> |
Marek Vasut | 1274698 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 25 | #define LMB_ALLOC_ANYWHERE 0 |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 26 | #define LMB_ALIST_INITIAL_SIZE 4 |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 27 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 28 | static struct lmb lmb; |
| 29 | |
| 30 | static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name) |
Patrick Delaunay | 358c778 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 31 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 32 | struct lmb_region *rgn = lmb_rgn_lst->data; |
Patrick Delaunay | 358c778 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 33 | unsigned long long base, size, end; |
| 34 | enum lmb_flags flags; |
| 35 | int i; |
| 36 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 37 | printf(" %s.count = 0x%x\n", name, lmb_rgn_lst->count); |
Patrick Delaunay | 358c778 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 38 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 39 | for (i = 0; i < lmb_rgn_lst->count; i++) { |
| 40 | base = rgn[i].base; |
| 41 | size = rgn[i].size; |
Patrick Delaunay | 358c778 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 42 | end = base + size - 1; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 43 | flags = rgn[i].flags; |
Patrick Delaunay | 358c778 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 44 | |
| 45 | printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n", |
| 46 | name, i, base, end, size, flags); |
| 47 | } |
| 48 | } |
| 49 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 50 | void lmb_dump_all_force(void) |
Tero Kristo | 9996cea | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 51 | { |
Tero Kristo | 9996cea | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 52 | printf("lmb_dump_all:\n"); |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 53 | lmb_dump_region(&lmb.free_mem, "memory"); |
| 54 | lmb_dump_region(&lmb.used_mem, "reserved"); |
Tero Kristo | 9996cea | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 55 | } |
| 56 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 57 | void lmb_dump_all(void) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 58 | { |
| 59 | #ifdef DEBUG |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 60 | lmb_dump_all_force(); |
Tero Kristo | 9996cea | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 61 | #endif |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 62 | } |
| 63 | |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 64 | static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1, |
| 65 | phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 66 | { |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 67 | const phys_addr_t base1_end = base1 + size1 - 1; |
| 68 | const phys_addr_t base2_end = base2 + size2 - 1; |
| 69 | |
| 70 | return ((base1 <= base2_end) && (base2 <= base1_end)); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 71 | } |
| 72 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 73 | 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] | 74 | phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 75 | { |
| 76 | if (base2 == base1 + size1) |
| 77 | return 1; |
| 78 | else if (base1 == base2 + size2) |
| 79 | return -1; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 84 | static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1, |
Udit Kumar | edb5824 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 85 | unsigned long r2) |
| 86 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 87 | struct lmb_region *rgn = lmb_rgn_lst->data; |
| 88 | |
| 89 | phys_addr_t base1 = rgn[r1].base; |
| 90 | phys_size_t size1 = rgn[r1].size; |
| 91 | phys_addr_t base2 = rgn[r2].base; |
| 92 | phys_size_t size2 = rgn[r2].size; |
Udit Kumar | edb5824 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 93 | |
| 94 | return lmb_addrs_overlap(base1, size1, base2, size2); |
| 95 | } |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 96 | |
| 97 | static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1, |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 98 | unsigned long r2) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 99 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 100 | struct lmb_region *rgn = lmb_rgn_lst->data; |
| 101 | |
| 102 | phys_addr_t base1 = rgn[r1].base; |
| 103 | phys_size_t size1 = rgn[r1].size; |
| 104 | phys_addr_t base2 = rgn[r2].base; |
| 105 | phys_size_t size2 = rgn[r2].size; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 106 | return lmb_addrs_adjacent(base1, size1, base2, size2); |
| 107 | } |
| 108 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 109 | static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 110 | { |
| 111 | unsigned long i; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 112 | struct lmb_region *rgn = lmb_rgn_lst->data; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 113 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 114 | for (i = r; i < lmb_rgn_lst->count - 1; i++) { |
| 115 | rgn[i].base = rgn[i + 1].base; |
| 116 | rgn[i].size = rgn[i + 1].size; |
| 117 | rgn[i].flags = rgn[i + 1].flags; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 118 | } |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 119 | lmb_rgn_lst->count--; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* Assumption: base addr of region 1 < base addr of region 2 */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 123 | static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1, |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 124 | unsigned long r2) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 125 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 126 | struct lmb_region *rgn = lmb_rgn_lst->data; |
| 127 | |
| 128 | rgn[r1].size += rgn[r2].size; |
| 129 | lmb_remove_region(lmb_rgn_lst, r2); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 130 | } |
| 131 | |
Udit Kumar | edb5824 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 132 | /*Assumption : base addr of region 1 < base addr of region 2*/ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 133 | static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst, |
| 134 | unsigned long r1, unsigned long r2) |
Udit Kumar | edb5824 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 135 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 136 | struct lmb_region *rgn = lmb_rgn_lst->data; |
| 137 | |
| 138 | phys_addr_t base1 = rgn[r1].base; |
| 139 | phys_size_t size1 = rgn[r1].size; |
| 140 | phys_addr_t base2 = rgn[r2].base; |
| 141 | phys_size_t size2 = rgn[r2].size; |
Udit Kumar | edb5824 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 142 | |
| 143 | if (base1 + size1 > base2 + size2) { |
| 144 | printf("This will not be a case any time\n"); |
| 145 | return; |
| 146 | } |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 147 | rgn[r1].size = base2 + size2 - base1; |
| 148 | lmb_remove_region(lmb_rgn_lst, r2); |
Udit Kumar | edb5824 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 149 | } |
| 150 | |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 151 | /** |
| 152 | * efi_lmb_reserve() - add reservations for EFI memory |
| 153 | * |
| 154 | * Add reservations for all EFI memory areas that are not |
| 155 | * EFI_CONVENTIONAL_MEMORY. |
| 156 | * |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 157 | * Return: 0 on success, 1 on failure |
| 158 | */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 159 | static __maybe_unused int efi_lmb_reserve(void) |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 160 | { |
| 161 | struct efi_mem_desc *memmap = NULL, *map; |
| 162 | efi_uintn_t i, map_size = 0; |
| 163 | efi_status_t ret; |
| 164 | |
| 165 | ret = efi_get_memory_map_alloc(&map_size, &memmap); |
| 166 | if (ret != EFI_SUCCESS) |
| 167 | return 1; |
| 168 | |
| 169 | for (i = 0, map = memmap; i < map_size / sizeof(*map); ++map, ++i) { |
Sjoerd Simons | c5279ea | 2023-01-19 09:38:18 +0100 | [diff] [blame] | 170 | if (map->type != EFI_CONVENTIONAL_MEMORY) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 171 | lmb_reserve_flags(map_to_sysmem((void *)(uintptr_t) |
Sjoerd Simons | c5279ea | 2023-01-19 09:38:18 +0100 | [diff] [blame] | 172 | map->physical_start), |
| 173 | map->num_pages * EFI_PAGE_SIZE, |
| 174 | map->type == EFI_RESERVED_MEMORY_TYPE |
| 175 | ? LMB_NOMAP : LMB_NONE); |
| 176 | } |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 177 | } |
| 178 | efi_free_pool(memmap); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Sughosh Ganu | 6534d26 | 2024-08-26 17:29:30 +0530 | [diff] [blame^] | 183 | static void lmb_reserve_uboot_region(void) |
| 184 | { |
| 185 | int bank; |
| 186 | ulong end, bank_end; |
| 187 | phys_addr_t rsv_start; |
| 188 | |
| 189 | rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE; |
| 190 | end = gd->ram_top; |
| 191 | |
| 192 | /* |
| 193 | * Reserve memory from aligned address below the bottom of U-Boot stack |
| 194 | * until end of RAM area to prevent LMB from overwriting that memory. |
| 195 | */ |
| 196 | debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start); |
| 197 | |
| 198 | /* adjust sp by 16K to be safe */ |
| 199 | rsv_start -= SZ_16K; |
| 200 | for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) { |
| 201 | if (!gd->bd->bi_dram[bank].size || |
| 202 | rsv_start < gd->bd->bi_dram[bank].start) |
| 203 | continue; |
| 204 | /* Watch out for RAM at end of address space! */ |
| 205 | bank_end = gd->bd->bi_dram[bank].start + |
| 206 | gd->bd->bi_dram[bank].size - 1; |
| 207 | if (rsv_start > bank_end) |
| 208 | continue; |
| 209 | if (bank_end > end) |
| 210 | bank_end = end - 1; |
| 211 | |
| 212 | lmb_reserve_flags(rsv_start, bank_end - rsv_start + 1, |
| 213 | LMB_NOOVERWRITE); |
| 214 | |
| 215 | if (gd->flags & GD_FLG_SKIP_RELOC) |
| 216 | lmb_reserve_flags((phys_addr_t)(uintptr_t)_start, |
| 217 | gd->mon_len, LMB_NOOVERWRITE); |
| 218 | |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 223 | static void lmb_reserve_common(void *fdt_blob) |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 224 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 225 | board_lmb_reserve(); |
Sughosh Ganu | 6534d26 | 2024-08-26 17:29:30 +0530 | [diff] [blame^] | 226 | lmb_reserve_uboot_region(); |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 227 | |
Simon Glass | 0c303f9 | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 228 | if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob) |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 229 | boot_fdt_add_mem_rsv_regions(fdt_blob); |
Heinrich Schuchardt | 06d514d | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 230 | |
| 231 | if (CONFIG_IS_ENABLED(EFI_LOADER)) |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 232 | efi_lmb_reserve(); |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Sughosh Ganu | f4fb154 | 2024-08-26 17:29:24 +0530 | [diff] [blame] | 235 | static __maybe_unused void lmb_reserve_common_spl(void) |
| 236 | { |
| 237 | phys_addr_t rsv_start; |
| 238 | phys_size_t rsv_size; |
| 239 | |
| 240 | /* |
| 241 | * Assume a SPL stack of 16KB. This must be |
| 242 | * more than enough for the SPL stage. |
| 243 | */ |
| 244 | if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) { |
| 245 | rsv_start = gd->start_addr_sp - 16384; |
| 246 | rsv_size = 16384; |
| 247 | lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE); |
| 248 | } |
| 249 | |
| 250 | if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) { |
| 251 | /* Reserve the bss region */ |
| 252 | rsv_start = (phys_addr_t)(uintptr_t)__bss_start; |
| 253 | rsv_size = (phys_addr_t)(uintptr_t)__bss_end - |
| 254 | (phys_addr_t)(uintptr_t)__bss_start; |
| 255 | lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE); |
| 256 | } |
| 257 | } |
| 258 | |
Sughosh Ganu | 8a9fc30 | 2024-08-26 17:29:23 +0530 | [diff] [blame] | 259 | /** |
| 260 | * lmb_add_memory() - Add memory range for LMB allocations |
| 261 | * |
| 262 | * Add the entire available memory range to the pool of memory that |
| 263 | * can be used by the LMB module for allocations. |
| 264 | * |
| 265 | * Return: None |
| 266 | */ |
| 267 | void lmb_add_memory(void) |
| 268 | { |
| 269 | int i; |
| 270 | phys_size_t size; |
| 271 | phys_addr_t rgn_top; |
| 272 | u64 ram_top = gd->ram_top; |
| 273 | struct bd_info *bd = gd->bd; |
| 274 | |
| 275 | /* Assume a 4GB ram_top if not defined */ |
| 276 | if (!ram_top) |
| 277 | ram_top = 0x100000000ULL; |
| 278 | |
| 279 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 280 | size = bd->bi_dram[i].size; |
| 281 | if (size) { |
| 282 | if (bd->bi_dram[i].start > ram_top) |
| 283 | continue; |
| 284 | |
| 285 | rgn_top = bd->bi_dram[i].start + |
| 286 | bd->bi_dram[i].size; |
| 287 | |
| 288 | if (rgn_top > ram_top) |
| 289 | size -= rgn_top - ram_top; |
| 290 | |
| 291 | lmb_add(bd->bi_dram[i].start, size); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 296 | static long lmb_resize_regions(struct alist *lmb_rgn_lst, |
| 297 | unsigned long idx_start, |
| 298 | phys_addr_t base, phys_size_t size) |
| 299 | { |
| 300 | phys_size_t rgnsize; |
| 301 | unsigned long rgn_cnt, idx, idx_end; |
| 302 | phys_addr_t rgnbase, rgnend; |
| 303 | phys_addr_t mergebase, mergeend; |
| 304 | struct lmb_region *rgn = lmb_rgn_lst->data; |
| 305 | |
| 306 | rgn_cnt = 0; |
| 307 | idx = idx_start; |
| 308 | idx_end = idx_start; |
| 309 | |
| 310 | /* |
| 311 | * First thing to do is to identify how many regions |
| 312 | * the requested region overlaps. |
| 313 | * If the flags match, combine all these overlapping |
| 314 | * regions into a single region, and remove the merged |
| 315 | * regions. |
| 316 | */ |
| 317 | while (idx <= lmb_rgn_lst->count - 1) { |
| 318 | rgnbase = rgn[idx].base; |
| 319 | rgnsize = rgn[idx].size; |
| 320 | |
| 321 | if (lmb_addrs_overlap(base, size, rgnbase, |
| 322 | rgnsize)) { |
| 323 | if (rgn[idx].flags != LMB_NONE) |
| 324 | return -1; |
| 325 | rgn_cnt++; |
| 326 | idx_end = idx; |
| 327 | } |
| 328 | idx++; |
| 329 | } |
| 330 | |
| 331 | /* The merged region's base and size */ |
| 332 | rgnbase = rgn[idx_start].base; |
| 333 | mergebase = min(base, rgnbase); |
| 334 | rgnend = rgn[idx_end].base + rgn[idx_end].size; |
| 335 | mergeend = max(rgnend, (base + size)); |
| 336 | |
| 337 | rgn[idx_start].base = mergebase; |
| 338 | rgn[idx_start].size = mergeend - mergebase; |
| 339 | |
| 340 | /* Now remove the merged regions */ |
| 341 | while (--rgn_cnt) |
| 342 | lmb_remove_region(lmb_rgn_lst, idx_start + 1); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 347 | /** |
| 348 | * lmb_add_region_flags() - Add an lmb region to the given list |
| 349 | * @lmb_rgn_lst: LMB list to which region is to be added(free/used) |
| 350 | * @base: Start address of the region |
| 351 | * @size: Size of the region to be added |
| 352 | * @flags: Attributes of the LMB region |
| 353 | * |
| 354 | * Add a region of memory to the list. If the region does not exist, add |
| 355 | * it to the list. Depending on the attributes of the region to be added, |
| 356 | * the function might resize an already existing region or coalesce two |
| 357 | * adjacent regions. |
| 358 | * |
| 359 | * |
| 360 | * Returns: 0 if the region addition successful, -1 on failure |
| 361 | */ |
| 362 | static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 363 | phys_size_t size, enum lmb_flags flags) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 364 | { |
| 365 | unsigned long coalesced = 0; |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 366 | long ret, i; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 367 | struct lmb_region *rgn = lmb_rgn_lst->data; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 368 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 369 | if (alist_err(lmb_rgn_lst)) |
| 370 | return -1; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 371 | |
| 372 | /* First try and coalesce this LMB with another. */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 373 | for (i = 0; i < lmb_rgn_lst->count; i++) { |
| 374 | phys_addr_t rgnbase = rgn[i].base; |
| 375 | phys_size_t rgnsize = rgn[i].size; |
| 376 | phys_size_t rgnflags = rgn[i].flags; |
Sjoerd Simons | 0d91c88 | 2023-02-12 16:07:05 +0100 | [diff] [blame] | 377 | phys_addr_t end = base + size - 1; |
| 378 | phys_addr_t rgnend = rgnbase + rgnsize - 1; |
Sjoerd Simons | 0d91c88 | 2023-02-12 16:07:05 +0100 | [diff] [blame] | 379 | if (rgnbase <= base && end <= rgnend) { |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 380 | if (flags == rgnflags) |
| 381 | /* Already have this region, so we're done */ |
| 382 | return 0; |
| 383 | else |
| 384 | return -1; /* regions with new flags */ |
| 385 | } |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 386 | |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 387 | ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize); |
| 388 | if (ret > 0) { |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 389 | if (flags != rgnflags) |
| 390 | break; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 391 | rgn[i].base -= size; |
| 392 | rgn[i].size += size; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 393 | coalesced++; |
| 394 | break; |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 395 | } else if (ret < 0) { |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 396 | if (flags != rgnflags) |
| 397 | break; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 398 | rgn[i].size += size; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 399 | coalesced++; |
| 400 | break; |
Simon Goldschmidt | 0f7c51a | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 401 | } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 402 | if (flags == LMB_NONE) { |
| 403 | ret = lmb_resize_regions(lmb_rgn_lst, i, base, |
| 404 | size); |
| 405 | if (ret < 0) |
| 406 | return -1; |
| 407 | |
| 408 | coalesced++; |
| 409 | break; |
| 410 | } else { |
| 411 | return -1; |
| 412 | } |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 416 | if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) { |
| 417 | rgn = lmb_rgn_lst->data; |
| 418 | if (rgn[i].flags == rgn[i + 1].flags) { |
| 419 | if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) { |
| 420 | lmb_coalesce_regions(lmb_rgn_lst, i, i + 1); |
| 421 | coalesced++; |
| 422 | } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) { |
| 423 | /* fix overlapping area */ |
| 424 | lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1); |
| 425 | coalesced++; |
| 426 | } |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 427 | } |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | if (coalesced) |
| 431 | return coalesced; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 432 | |
| 433 | if (alist_full(lmb_rgn_lst) && |
| 434 | !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc)) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 435 | return -1; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 436 | rgn = lmb_rgn_lst->data; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 437 | |
| 438 | /* Couldn't coalesce the LMB, so add it to the sorted table. */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 439 | for (i = lmb_rgn_lst->count; i >= 0; i--) { |
| 440 | if (i && base < rgn[i - 1].base) { |
| 441 | rgn[i] = rgn[i - 1]; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 442 | } else { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 443 | rgn[i].base = base; |
| 444 | rgn[i].size = size; |
| 445 | rgn[i].flags = flags; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | } |
| 449 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 450 | lmb_rgn_lst->count++; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 455 | static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base, |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 456 | phys_size_t size) |
| 457 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 458 | return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE); |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 461 | /* This routine may be called with relocation disabled. */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 462 | long lmb_add(phys_addr_t base, phys_size_t size) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 463 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 464 | struct alist *lmb_rgn_lst = &lmb.free_mem; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 465 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 466 | return lmb_add_region(lmb_rgn_lst, base, size); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 467 | } |
| 468 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 469 | long lmb_free(phys_addr_t base, phys_size_t size) |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 470 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 471 | struct lmb_region *rgn; |
| 472 | struct alist *lmb_rgn_lst = &lmb.used_mem; |
Andy Fleming | 98874ff | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 473 | phys_addr_t rgnbegin, rgnend; |
Simon Goldschmidt | d67f33c | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 474 | phys_addr_t end = base + size - 1; |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 475 | int i; |
| 476 | |
| 477 | rgnbegin = rgnend = 0; /* supress gcc warnings */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 478 | rgn = lmb_rgn_lst->data; |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 479 | /* Find the region where (base, size) belongs to */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 480 | for (i = 0; i < lmb_rgn_lst->count; i++) { |
| 481 | rgnbegin = rgn[i].base; |
| 482 | rgnend = rgnbegin + rgn[i].size - 1; |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 483 | |
| 484 | if ((rgnbegin <= base) && (end <= rgnend)) |
| 485 | break; |
| 486 | } |
| 487 | |
| 488 | /* Didn't find the region */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 489 | if (i == lmb_rgn_lst->count) |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 490 | return -1; |
| 491 | |
| 492 | /* Check to see if we are removing entire region */ |
| 493 | if ((rgnbegin == base) && (rgnend == end)) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 494 | lmb_remove_region(lmb_rgn_lst, i); |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | /* Check to see if region is matching at the front */ |
| 499 | if (rgnbegin == base) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 500 | rgn[i].base = end + 1; |
| 501 | rgn[i].size -= size; |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | /* Check to see if the region is matching at the end */ |
| 506 | if (rgnend == end) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 507 | rgn[i].size -= size; |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * We need to split the entry - adjust the current one to the |
| 513 | * beginging of the hole and add the region after hole. |
| 514 | */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 515 | rgn[i].size = base - rgn[i].base; |
| 516 | return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end, |
| 517 | rgn[i].flags); |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 520 | long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags) |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 521 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 522 | struct alist *lmb_rgn_lst = &lmb.used_mem; |
Patrick Delaunay | 59c0ea5 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 523 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 524 | return lmb_add_region_flags(lmb_rgn_lst, base, size, flags); |
Andy Fleming | 63796c4 | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 525 | } |
| 526 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 527 | long lmb_reserve(phys_addr_t base, phys_size_t size) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 528 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 529 | return lmb_reserve_flags(base, size, LMB_NONE); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 530 | } |
| 531 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 532 | static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base, |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 533 | phys_size_t size) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 534 | { |
| 535 | unsigned long i; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 536 | struct lmb_region *rgn = lmb_rgn_lst->data; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 537 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 538 | for (i = 0; i < lmb_rgn_lst->count; i++) { |
| 539 | phys_addr_t rgnbase = rgn[i].base; |
| 540 | phys_size_t rgnsize = rgn[i].size; |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 541 | if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 542 | break; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 543 | } |
| 544 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 545 | return (i < lmb_rgn_lst->count) ? i : -1; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 546 | } |
| 547 | |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 548 | 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] | 549 | { |
| 550 | return addr & ~(size - 1); |
| 551 | } |
| 552 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 553 | static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align, |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 554 | phys_addr_t max_addr, enum lmb_flags flags) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 555 | { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 556 | long i, rgn; |
Becky Bruce | 391fd93 | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 557 | phys_addr_t base = 0; |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 558 | phys_addr_t res_base; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 559 | struct lmb_region *lmb_used = lmb.used_mem.data; |
| 560 | struct lmb_region *lmb_memory = lmb.free_mem.data; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 561 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 562 | for (i = lmb.free_mem.count - 1; i >= 0; i--) { |
| 563 | phys_addr_t lmbbase = lmb_memory[i].base; |
| 564 | phys_size_t lmbsize = lmb_memory[i].size; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 565 | |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 566 | if (lmbsize < size) |
| 567 | continue; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 568 | if (max_addr == LMB_ALLOC_ANYWHERE) |
| 569 | base = lmb_align_down(lmbbase + lmbsize - size, align); |
| 570 | else if (lmbbase < max_addr) { |
Stephen Warren | ad3fda5 | 2014-07-31 13:40:07 -0600 | [diff] [blame] | 571 | base = lmbbase + lmbsize; |
| 572 | if (base < lmbbase) |
| 573 | base = -1; |
| 574 | base = min(base, max_addr); |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 575 | base = lmb_align_down(base - size, align); |
| 576 | } else |
| 577 | continue; |
| 578 | |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 579 | while (base && lmbbase <= base) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 580 | rgn = lmb_overlaps_region(&lmb.used_mem, base, size); |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 581 | if (rgn < 0) { |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 582 | /* This area isn't reserved, take it */ |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 583 | if (lmb_add_region_flags(&lmb.used_mem, base, |
| 584 | size, flags) < 0) |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 585 | return 0; |
| 586 | return base; |
| 587 | } |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 588 | |
| 589 | res_base = lmb_used[rgn].base; |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 590 | if (res_base < size) |
| 591 | break; |
| 592 | base = lmb_align_down(res_base - size, align); |
| 593 | } |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 594 | } |
Andy Fleming | 7570a99 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 595 | return 0; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 596 | } |
| 597 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 598 | phys_addr_t lmb_alloc(phys_size_t size, ulong align) |
Sughosh Ganu | 3d679ae | 2024-08-26 17:29:16 +0530 | [diff] [blame] | 599 | { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 600 | return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE); |
Sughosh Ganu | 3d679ae | 2024-08-26 17:29:16 +0530 | [diff] [blame] | 601 | } |
| 602 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 603 | phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr) |
Sughosh Ganu | 3d679ae | 2024-08-26 17:29:16 +0530 | [diff] [blame] | 604 | { |
| 605 | phys_addr_t alloc; |
| 606 | |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 607 | alloc = __lmb_alloc_base(size, align, max_addr, LMB_NONE); |
Sughosh Ganu | 3d679ae | 2024-08-26 17:29:16 +0530 | [diff] [blame] | 608 | |
| 609 | if (alloc == 0) |
| 610 | printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", |
| 611 | (ulong)size, (ulong)max_addr); |
| 612 | |
| 613 | return alloc; |
| 614 | } |
| 615 | |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 616 | static phys_addr_t __lmb_alloc_addr(phys_addr_t base, phys_size_t size, |
| 617 | enum lmb_flags flags) |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 618 | { |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 619 | long rgn; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 620 | struct lmb_region *lmb_memory = lmb.free_mem.data; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 621 | |
| 622 | /* Check if the requested address is in one of the memory regions */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 623 | rgn = lmb_overlaps_region(&lmb.free_mem, base, size); |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 624 | if (rgn >= 0) { |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 625 | /* |
| 626 | * Check if the requested end address is in the same memory |
| 627 | * region we found. |
| 628 | */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 629 | if (lmb_addrs_overlap(lmb_memory[rgn].base, |
| 630 | lmb_memory[rgn].size, |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 631 | base + size - 1, 1)) { |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 632 | /* ok, reserve the memory */ |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 633 | if (lmb_reserve_flags(base, size, flags) >= 0) |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 634 | return base; |
| 635 | } |
| 636 | } |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 637 | |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 638 | return 0; |
| 639 | } |
| 640 | |
Sughosh Ganu | 5e9553c | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 641 | /* |
| 642 | * Try to allocate a specific address range: must be in defined memory but not |
| 643 | * reserved |
| 644 | */ |
| 645 | phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size) |
| 646 | { |
| 647 | return __lmb_alloc_addr(base, size, LMB_NONE); |
| 648 | } |
| 649 | |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 650 | /* Return number of bytes from a given address that are free */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 651 | phys_size_t lmb_get_free_size(phys_addr_t addr) |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 652 | { |
| 653 | int i; |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 654 | long rgn; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 655 | struct lmb_region *lmb_used = lmb.used_mem.data; |
| 656 | struct lmb_region *lmb_memory = lmb.free_mem.data; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 657 | |
| 658 | /* check if the requested address is in the memory regions */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 659 | rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1); |
Simon Goldschmidt | e35d2a7 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 660 | if (rgn >= 0) { |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 661 | for (i = 0; i < lmb.used_mem.count; i++) { |
| 662 | if (addr < lmb_used[i].base) { |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 663 | /* first reserved range > requested address */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 664 | return lmb_used[i].base - addr; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 665 | } |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 666 | if (lmb_used[i].base + |
| 667 | lmb_used[i].size > addr) { |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 668 | /* requested addr is in this reserved range */ |
| 669 | return 0; |
| 670 | } |
| 671 | } |
| 672 | /* if we come here: no reserved ranges above requested addr */ |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 673 | return lmb_memory[lmb.free_mem.count - 1].base + |
| 674 | lmb_memory[lmb.free_mem.count - 1].size - addr; |
Simon Goldschmidt | 4cc8af8 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 675 | } |
| 676 | return 0; |
| 677 | } |
| 678 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 679 | int lmb_is_reserved_flags(phys_addr_t addr, int flags) |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 680 | { |
| 681 | int i; |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 682 | struct lmb_region *lmb_used = lmb.used_mem.data; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 683 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 684 | for (i = 0; i < lmb.used_mem.count; i++) { |
| 685 | phys_addr_t upper = lmb_used[i].base + |
| 686 | lmb_used[i].size - 1; |
| 687 | if (addr >= lmb_used[i].base && addr <= upper) |
| 688 | return (lmb_used[i].flags & flags) == flags; |
Kumar Gala | 4ed6552 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 689 | } |
| 690 | return 0; |
| 691 | } |
Mike Frysinger | a16028d | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 692 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 693 | __weak void board_lmb_reserve(void) |
Mike Frysinger | a16028d | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 694 | { |
| 695 | /* please define platform specific board_lmb_reserve() */ |
| 696 | } |
Mike Frysinger | a16028d | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 697 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 698 | static int lmb_setup(void) |
| 699 | { |
| 700 | bool ret; |
| 701 | |
| 702 | ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region), |
| 703 | (uint)LMB_ALIST_INITIAL_SIZE); |
| 704 | if (!ret) { |
| 705 | log_debug("Unable to initialise the list for LMB free memory\n"); |
| 706 | return -ENOMEM; |
| 707 | } |
| 708 | |
| 709 | ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region), |
| 710 | (uint)LMB_ALIST_INITIAL_SIZE); |
| 711 | if (!ret) { |
| 712 | log_debug("Unable to initialise the list for LMB used memory\n"); |
| 713 | return -ENOMEM; |
| 714 | } |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * lmb_init() - Initialise the LMB module |
| 721 | * |
| 722 | * Initialise the LMB lists needed for keeping the memory map. There |
| 723 | * are two lists, in form of alloced list data structure. One for the |
| 724 | * available memory, and one for the used memory. Initialise the two |
| 725 | * lists as part of board init. Add memory to the available memory |
| 726 | * list and reserve common areas by adding them to the used memory |
| 727 | * list. |
| 728 | * |
| 729 | * Return: 0 on success, -ve on error |
| 730 | */ |
| 731 | int lmb_init(void) |
| 732 | { |
| 733 | int ret; |
| 734 | |
| 735 | ret = lmb_setup(); |
| 736 | if (ret) { |
| 737 | log_info("Unable to init LMB\n"); |
| 738 | return ret; |
| 739 | } |
| 740 | |
Sughosh Ganu | 8a9fc30 | 2024-08-26 17:29:23 +0530 | [diff] [blame] | 741 | lmb_add_memory(); |
| 742 | |
Sughosh Ganu | f4fb154 | 2024-08-26 17:29:24 +0530 | [diff] [blame] | 743 | /* Reserve the U-Boot image region once U-Boot has relocated */ |
| 744 | if (spl_phase() == PHASE_SPL) |
| 745 | lmb_reserve_common_spl(); |
| 746 | else if (spl_phase() == PHASE_BOARD_R) |
| 747 | lmb_reserve_common((void *)gd->fdt_blob); |
| 748 | |
Sughosh Ganu | ed17a33 | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | #if CONFIG_IS_ENABLED(UNIT_TEST) |
| 753 | struct lmb *lmb_get(void) |
| 754 | { |
| 755 | return &lmb; |
| 756 | } |
| 757 | |
| 758 | int lmb_push(struct lmb *store) |
| 759 | { |
| 760 | int ret; |
| 761 | |
| 762 | *store = lmb; |
| 763 | ret = lmb_setup(); |
| 764 | if (ret) |
| 765 | return ret; |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | void lmb_pop(struct lmb *store) |
| 771 | { |
| 772 | alist_uninit(&lmb.free_mem); |
| 773 | alist_uninit(&lmb.used_mem); |
| 774 | lmb = *store; |
| 775 | } |
| 776 | #endif /* UNIT_TEST */ |