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