Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Procedures for maintaining information about logical memory blocks. |
| 4 | * |
| 5 | * Peter Bergner, IBM Corp. June 2001. |
| 6 | * Copyright (C) 2001 Peter Bergner. |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 9 | #include <alist.h> |
Heinrich Schuchardt | c9bb2eb | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 10 | #include <efi_loader.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 11 | #include <image.h> |
Heinrich Schuchardt | c9bb2eb | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 12 | #include <mapmem.h> |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 13 | #include <lmb.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 16 | |
Marek Vasut | a2eec02 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Marek Vasut | 0fcae7f | 2021-11-13 18:34:37 +0100 | [diff] [blame] | 18 | #include <asm/sections.h> |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 19 | #include <linux/kernel.h> |
Marek Vasut | a2eec02 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 23 | #define LMB_ALLOC_ANYWHERE 0 |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 24 | #define LMB_ALIST_INITIAL_SIZE 4 |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 25 | |
Sughosh Ganu | 291bf9c | 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) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 29 | { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 30 | struct lmb_region *rgn = lmb_rgn_lst->data; |
Patrick Delaunay | 4812002 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 31 | unsigned long long base, size, end; |
| 32 | enum lmb_flags flags; |
| 33 | int i; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 34 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 35 | printf(" %s.count = 0x%x\n", name, lmb_rgn_lst->count); |
Patrick Delaunay | 4812002 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 36 | |
Sughosh Ganu | 291bf9c | 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 | 4812002 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 40 | end = base + size - 1; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 41 | flags = rgn[i].flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 42 | |
Patrick Delaunay | 4812002 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 43 | printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n", |
| 44 | name, i, base, end, size, flags); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 45 | } |
Tero Kristo | 97c418b | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 46 | } |
| 47 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 48 | void lmb_dump_all_force(void) |
Patrick Delaunay | 4812002 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 49 | { |
| 50 | printf("lmb_dump_all:\n"); |
Sughosh Ganu | 291bf9c | 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"); |
Patrick Delaunay | 4812002 | 2021-05-07 14:50:31 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 55 | void lmb_dump_all(void) |
Tero Kristo | 97c418b | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 56 | { |
| 57 | #ifdef DEBUG |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 58 | lmb_dump_all_force(); |
Tero Kristo | 97c418b | 2020-07-20 11:10:45 +0300 | [diff] [blame] | 59 | #endif |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 60 | } |
| 61 | |
Simon Goldschmidt | f41f7d6 | 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 | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 64 | { |
Simon Goldschmidt | 6402d9b | 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 | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 69 | } |
| 70 | |
Becky Bruce | d26d67c | 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 | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 72 | phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 6d7bfa8 | 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 | 291bf9c | 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 | 4e8e663 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 83 | unsigned long r2) |
| 84 | { |
Sughosh Ganu | 291bf9c | 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 | 4e8e663 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 91 | |
| 92 | return lmb_addrs_overlap(base1, size1, base2, size2); |
| 93 | } |
Sughosh Ganu | 291bf9c | 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 | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 96 | unsigned long r2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 97 | { |
Sughosh Ganu | 291bf9c | 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 | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 104 | return lmb_addrs_adjacent(base1, size1, base2, size2); |
| 105 | } |
| 106 | |
Sughosh Ganu | 291bf9c | 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 | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 108 | { |
| 109 | unsigned long i; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 110 | struct lmb_region *rgn = lmb_rgn_lst->data; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 111 | |
Sughosh Ganu | 291bf9c | 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 | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 116 | } |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 117 | lmb_rgn_lst->count--; |
Kumar Gala | 6d7bfa8 | 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 | 291bf9c | 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 | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 122 | unsigned long r2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 123 | { |
Sughosh Ganu | 291bf9c | 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 | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 128 | } |
| 129 | |
Udit Kumar | 4e8e663 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 130 | /*Assumption : base addr of region 1 < base addr of region 2*/ |
Sughosh Ganu | 291bf9c | 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 | 4e8e663 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 133 | { |
Sughosh Ganu | 291bf9c | 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 | 4e8e663 | 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 | 291bf9c | 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 | 4e8e663 | 2023-09-26 16:54:42 +0530 | [diff] [blame] | 147 | } |
| 148 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 149 | void arch_lmb_reserve_generic(ulong sp, ulong end, ulong align) |
Marek Vasut | a2eec02 | 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 | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 175 | lmb_reserve(sp, bank_end - sp + 1); |
Marek Vasut | 0fcae7f | 2021-11-13 18:34:37 +0100 | [diff] [blame] | 176 | |
| 177 | if (gd->flags & GD_FLG_SKIP_RELOC) |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 178 | lmb_reserve((phys_addr_t)(uintptr_t)_start, gd->mon_len); |
Marek Vasut | 0fcae7f | 2021-11-13 18:34:37 +0100 | [diff] [blame] | 179 | |
Marek Vasut | a2eec02 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
Heinrich Schuchardt | c9bb2eb | 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 | c9bb2eb | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 190 | * Return: 0 on success, 1 on failure |
| 191 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 192 | static __maybe_unused int efi_lmb_reserve(void) |
Heinrich Schuchardt | c9bb2eb | 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 | 47a5a84 | 2023-01-19 09:38:18 +0100 | [diff] [blame] | 203 | if (map->type != EFI_CONVENTIONAL_MEMORY) { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 204 | lmb_reserve_flags(map_to_sysmem((void *)(uintptr_t) |
Sjoerd Simons | 47a5a84 | 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 | c9bb2eb | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 210 | } |
| 211 | efi_free_pool(memmap); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 216 | static void lmb_reserve_common(void *fdt_blob) |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 217 | { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 218 | arch_lmb_reserve(); |
| 219 | board_lmb_reserve(); |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 220 | |
Simon Glass | 85c057e | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 221 | if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob) |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 222 | boot_fdt_add_mem_rsv_regions(fdt_blob); |
Heinrich Schuchardt | c9bb2eb | 2023-01-04 01:36:14 +0100 | [diff] [blame] | 223 | |
| 224 | if (CONFIG_IS_ENABLED(EFI_LOADER)) |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 225 | efi_lmb_reserve(); |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 228 | /* Initialize the struct, add memory and call arch/board reserve functions */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 229 | void lmb_init_and_reserve(struct bd_info *bd, void *fdt_blob) |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 230 | { |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 231 | int i; |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 232 | |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 233 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
Sughosh Ganu | 291bf9c | 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 | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 236 | } |
Stefan Roese | bbc8846 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 237 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 238 | lmb_reserve_common(fdt_blob); |
Simon Goldschmidt | 8890e7d | 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 | 291bf9c | 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 | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 244 | { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 245 | lmb_add(base, size); |
| 246 | lmb_reserve_common(fdt_blob); |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 249 | /** |
| 250 | * lmb_add_region_flags() - Add an lmb region to the given list |
| 251 | * @lmb_rgn_lst: LMB list to which region is to be added(free/used) |
| 252 | * @base: Start address of the region |
| 253 | * @size: Size of the region to be added |
| 254 | * @flags: Attributes of the LMB region |
| 255 | * |
| 256 | * Add a region of memory to the list. If the region does not exist, add |
| 257 | * it to the list. Depending on the attributes of the region to be added, |
| 258 | * the function might resize an already existing region or coalesce two |
| 259 | * adjacent regions. |
| 260 | * |
| 261 | * |
| 262 | * Returns: 0 if the region addition successful, -1 on failure |
| 263 | */ |
| 264 | 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] | 265 | phys_size_t size, enum lmb_flags flags) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 266 | { |
| 267 | unsigned long coalesced = 0; |
| 268 | long adjacent, i; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 269 | struct lmb_region *rgn = lmb_rgn_lst->data; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 270 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 271 | if (alist_err(lmb_rgn_lst)) |
| 272 | return -1; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 273 | |
| 274 | /* First try and coalesce this LMB with another. */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 275 | for (i = 0; i < lmb_rgn_lst->count; i++) { |
| 276 | phys_addr_t rgnbase = rgn[i].base; |
| 277 | phys_size_t rgnsize = rgn[i].size; |
| 278 | phys_size_t rgnflags = rgn[i].flags; |
Sjoerd Simons | 8cce491 | 2023-02-12 16:07:05 +0100 | [diff] [blame] | 279 | phys_addr_t end = base + size - 1; |
| 280 | phys_addr_t rgnend = rgnbase + rgnsize - 1; |
Sjoerd Simons | 8cce491 | 2023-02-12 16:07:05 +0100 | [diff] [blame] | 281 | if (rgnbase <= base && end <= rgnend) { |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 282 | if (flags == rgnflags) |
| 283 | /* Already have this region, so we're done */ |
| 284 | return 0; |
| 285 | else |
| 286 | return -1; /* regions with new flags */ |
| 287 | } |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 288 | |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 289 | adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize); |
| 290 | if (adjacent > 0) { |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 291 | if (flags != rgnflags) |
| 292 | break; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 293 | rgn[i].base -= size; |
| 294 | rgn[i].size += size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 295 | coalesced++; |
| 296 | break; |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 297 | } else if (adjacent < 0) { |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 298 | if (flags != rgnflags) |
| 299 | break; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 300 | rgn[i].size += size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 301 | coalesced++; |
| 302 | break; |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 303 | } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { |
| 304 | /* regions overlap */ |
| 305 | return -1; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 309 | if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) { |
| 310 | rgn = lmb_rgn_lst->data; |
| 311 | if (rgn[i].flags == rgn[i + 1].flags) { |
| 312 | if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) { |
| 313 | lmb_coalesce_regions(lmb_rgn_lst, i, i + 1); |
| 314 | coalesced++; |
| 315 | } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) { |
| 316 | /* fix overlapping area */ |
| 317 | lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1); |
| 318 | coalesced++; |
| 319 | } |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 320 | } |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if (coalesced) |
| 324 | return coalesced; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 325 | |
| 326 | if (alist_full(lmb_rgn_lst) && |
| 327 | !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc)) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 328 | return -1; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 329 | rgn = lmb_rgn_lst->data; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 330 | |
| 331 | /* Couldn't coalesce the LMB, so add it to the sorted table. */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 332 | for (i = lmb_rgn_lst->count; i >= 0; i--) { |
| 333 | if (i && base < rgn[i - 1].base) { |
| 334 | rgn[i] = rgn[i - 1]; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 335 | } else { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 336 | rgn[i].base = base; |
| 337 | rgn[i].size = size; |
| 338 | rgn[i].flags = flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | } |
| 342 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 343 | lmb_rgn_lst->count++; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 348 | 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] | 349 | phys_size_t size) |
| 350 | { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 351 | return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE); |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 354 | /* This routine may be called with relocation disabled. */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 355 | long lmb_add(phys_addr_t base, phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 356 | { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 357 | struct alist *lmb_rgn_lst = &lmb.free_mem; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 358 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 359 | return lmb_add_region(lmb_rgn_lst, base, size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 360 | } |
| 361 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 362 | long lmb_free(phys_addr_t base, phys_size_t size) |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 363 | { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 364 | struct lmb_region *rgn; |
| 365 | struct alist *lmb_rgn_lst = &lmb.used_mem; |
Andy Fleming | 09d2a71 | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 366 | phys_addr_t rgnbegin, rgnend; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 367 | phys_addr_t end = base + size - 1; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 368 | int i; |
| 369 | |
| 370 | rgnbegin = rgnend = 0; /* supress gcc warnings */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 371 | rgn = lmb_rgn_lst->data; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 372 | /* Find the region where (base, size) belongs to */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 373 | for (i = 0; i < lmb_rgn_lst->count; i++) { |
| 374 | rgnbegin = rgn[i].base; |
| 375 | rgnend = rgnbegin + rgn[i].size - 1; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 376 | |
| 377 | if ((rgnbegin <= base) && (end <= rgnend)) |
| 378 | break; |
| 379 | } |
| 380 | |
| 381 | /* Didn't find the region */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 382 | if (i == lmb_rgn_lst->count) |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 383 | return -1; |
| 384 | |
| 385 | /* Check to see if we are removing entire region */ |
| 386 | if ((rgnbegin == base) && (rgnend == end)) { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 387 | lmb_remove_region(lmb_rgn_lst, i); |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | /* Check to see if region is matching at the front */ |
| 392 | if (rgnbegin == base) { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 393 | rgn[i].base = end + 1; |
| 394 | rgn[i].size -= size; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /* Check to see if the region is matching at the end */ |
| 399 | if (rgnend == end) { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 400 | rgn[i].size -= size; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * We need to split the entry - adjust the current one to the |
| 406 | * beginging of the hole and add the region after hole. |
| 407 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 408 | rgn[i].size = base - rgn[i].base; |
| 409 | return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end, |
| 410 | rgn[i].flags); |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 411 | } |
| 412 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 413 | 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] | 414 | { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 415 | struct alist *lmb_rgn_lst = &lmb.used_mem; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 416 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 417 | return lmb_add_region_flags(lmb_rgn_lst, base, size, flags); |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 420 | long lmb_reserve(phys_addr_t base, phys_size_t size) |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 421 | { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 422 | return lmb_reserve_flags(base, size, LMB_NONE); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 423 | } |
| 424 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 425 | 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] | 426 | phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 427 | { |
| 428 | unsigned long i; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 429 | struct lmb_region *rgn = lmb_rgn_lst->data; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 430 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 431 | for (i = 0; i < lmb_rgn_lst->count; i++) { |
| 432 | phys_addr_t rgnbase = rgn[i].base; |
| 433 | phys_size_t rgnsize = rgn[i].size; |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 434 | if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 435 | break; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 436 | } |
| 437 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 438 | return (i < lmb_rgn_lst->count) ? i : -1; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 439 | } |
| 440 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 441 | 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] | 442 | { |
| 443 | return addr & ~(size - 1); |
| 444 | } |
| 445 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 446 | static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align, |
| 447 | phys_addr_t max_addr) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 448 | { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 449 | long i, rgn; |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 450 | phys_addr_t base = 0; |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 451 | phys_addr_t res_base; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 452 | struct lmb_region *lmb_used = lmb.used_mem.data; |
| 453 | struct lmb_region *lmb_memory = lmb.free_mem.data; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 454 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 455 | for (i = lmb.free_mem.count - 1; i >= 0; i--) { |
| 456 | phys_addr_t lmbbase = lmb_memory[i].base; |
| 457 | phys_size_t lmbsize = lmb_memory[i].size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 458 | |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 459 | if (lmbsize < size) |
| 460 | continue; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 461 | if (max_addr == LMB_ALLOC_ANYWHERE) |
| 462 | base = lmb_align_down(lmbbase + lmbsize - size, align); |
| 463 | else if (lmbbase < max_addr) { |
Stephen Warren | b6a010b | 2014-07-31 13:40:07 -0600 | [diff] [blame] | 464 | base = lmbbase + lmbsize; |
| 465 | if (base < lmbbase) |
| 466 | base = -1; |
| 467 | base = min(base, max_addr); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 468 | base = lmb_align_down(base - size, align); |
| 469 | } else |
| 470 | continue; |
| 471 | |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 472 | while (base && lmbbase <= base) { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 473 | rgn = lmb_overlaps_region(&lmb.used_mem, base, size); |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 474 | if (rgn < 0) { |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 475 | /* This area isn't reserved, take it */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 476 | if (lmb_add_region(&lmb.used_mem, base, |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 477 | size) < 0) |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 478 | return 0; |
| 479 | return base; |
| 480 | } |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 481 | |
| 482 | res_base = lmb_used[rgn].base; |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 483 | if (res_base < size) |
| 484 | break; |
| 485 | base = lmb_align_down(res_base - size, align); |
| 486 | } |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 487 | } |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 488 | return 0; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 489 | } |
| 490 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 491 | phys_addr_t lmb_alloc(phys_size_t size, ulong align) |
Sughosh Ganu | 78435de | 2024-08-26 17:29:16 +0530 | [diff] [blame] | 492 | { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 493 | return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE); |
Sughosh Ganu | 78435de | 2024-08-26 17:29:16 +0530 | [diff] [blame] | 494 | } |
| 495 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 496 | 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] | 497 | { |
| 498 | phys_addr_t alloc; |
| 499 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 500 | alloc = __lmb_alloc_base(size, align, max_addr); |
Sughosh Ganu | 78435de | 2024-08-26 17:29:16 +0530 | [diff] [blame] | 501 | |
| 502 | if (alloc == 0) |
| 503 | printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", |
| 504 | (ulong)size, (ulong)max_addr); |
| 505 | |
| 506 | return alloc; |
| 507 | } |
| 508 | |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 509 | /* |
| 510 | * Try to allocate a specific address range: must be in defined memory but not |
| 511 | * reserved |
| 512 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 513 | phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size) |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 514 | { |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 515 | long rgn; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 516 | struct lmb_region *lmb_memory = lmb.free_mem.data; |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 517 | |
| 518 | /* Check if the requested address is in one of the memory regions */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 519 | rgn = lmb_overlaps_region(&lmb.free_mem, base, size); |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 520 | if (rgn >= 0) { |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 521 | /* |
| 522 | * Check if the requested end address is in the same memory |
| 523 | * region we found. |
| 524 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 525 | if (lmb_addrs_overlap(lmb_memory[rgn].base, |
| 526 | lmb_memory[rgn].size, |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 527 | base + size - 1, 1)) { |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 528 | /* ok, reserve the memory */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 529 | if (lmb_reserve(base, size) >= 0) |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 530 | return base; |
| 531 | } |
| 532 | } |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | /* Return number of bytes from a given address that are free */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 537 | phys_size_t lmb_get_free_size(phys_addr_t addr) |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 538 | { |
| 539 | int i; |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 540 | long rgn; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 541 | struct lmb_region *lmb_used = lmb.used_mem.data; |
| 542 | struct lmb_region *lmb_memory = lmb.free_mem.data; |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 543 | |
| 544 | /* check if the requested address is in the memory regions */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 545 | rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1); |
Simon Goldschmidt | f41f7d6 | 2019-01-21 20:29:56 +0100 | [diff] [blame] | 546 | if (rgn >= 0) { |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 547 | for (i = 0; i < lmb.used_mem.count; i++) { |
| 548 | if (addr < lmb_used[i].base) { |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 549 | /* first reserved range > requested address */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 550 | return lmb_used[i].base - addr; |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 551 | } |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 552 | if (lmb_used[i].base + |
| 553 | lmb_used[i].size > addr) { |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 554 | /* requested addr is in this reserved range */ |
| 555 | return 0; |
| 556 | } |
| 557 | } |
| 558 | /* if we come here: no reserved ranges above requested addr */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 559 | return lmb_memory[lmb.free_mem.count - 1].base + |
| 560 | lmb_memory[lmb.free_mem.count - 1].size - addr; |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame] | 561 | } |
| 562 | return 0; |
| 563 | } |
| 564 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 565 | int lmb_is_reserved_flags(phys_addr_t addr, int flags) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 566 | { |
| 567 | int i; |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 568 | struct lmb_region *lmb_used = lmb.used_mem.data; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 569 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 570 | for (i = 0; i < lmb.used_mem.count; i++) { |
| 571 | phys_addr_t upper = lmb_used[i].base + |
| 572 | lmb_used[i].size - 1; |
| 573 | if (addr >= lmb_used[i].base && addr <= upper) |
| 574 | return (lmb_used[i].flags & flags) == flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 575 | } |
| 576 | return 0; |
| 577 | } |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 578 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 579 | __weak void board_lmb_reserve(void) |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 580 | { |
| 581 | /* please define platform specific board_lmb_reserve() */ |
| 582 | } |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 583 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 584 | __weak void arch_lmb_reserve(void) |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 585 | { |
| 586 | /* please define platform specific arch_lmb_reserve() */ |
| 587 | } |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame^] | 588 | |
| 589 | static int lmb_setup(void) |
| 590 | { |
| 591 | bool ret; |
| 592 | |
| 593 | ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region), |
| 594 | (uint)LMB_ALIST_INITIAL_SIZE); |
| 595 | if (!ret) { |
| 596 | log_debug("Unable to initialise the list for LMB free memory\n"); |
| 597 | return -ENOMEM; |
| 598 | } |
| 599 | |
| 600 | ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region), |
| 601 | (uint)LMB_ALIST_INITIAL_SIZE); |
| 602 | if (!ret) { |
| 603 | log_debug("Unable to initialise the list for LMB used memory\n"); |
| 604 | return -ENOMEM; |
| 605 | } |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * lmb_init() - Initialise the LMB module |
| 612 | * |
| 613 | * Initialise the LMB lists needed for keeping the memory map. There |
| 614 | * are two lists, in form of alloced list data structure. One for the |
| 615 | * available memory, and one for the used memory. Initialise the two |
| 616 | * lists as part of board init. Add memory to the available memory |
| 617 | * list and reserve common areas by adding them to the used memory |
| 618 | * list. |
| 619 | * |
| 620 | * Return: 0 on success, -ve on error |
| 621 | */ |
| 622 | int lmb_init(void) |
| 623 | { |
| 624 | int ret; |
| 625 | |
| 626 | ret = lmb_setup(); |
| 627 | if (ret) { |
| 628 | log_info("Unable to init LMB\n"); |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | #if CONFIG_IS_ENABLED(UNIT_TEST) |
| 636 | struct lmb *lmb_get(void) |
| 637 | { |
| 638 | return &lmb; |
| 639 | } |
| 640 | |
| 641 | int lmb_push(struct lmb *store) |
| 642 | { |
| 643 | int ret; |
| 644 | |
| 645 | *store = lmb; |
| 646 | ret = lmb_setup(); |
| 647 | if (ret) |
| 648 | return ret; |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | void lmb_pop(struct lmb *store) |
| 654 | { |
| 655 | alist_uninit(&lmb.free_mem); |
| 656 | alist_uninit(&lmb.used_mem); |
| 657 | lmb = *store; |
| 658 | } |
| 659 | #endif /* UNIT_TEST */ |