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