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