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