1 /* 2 * EFI application memory management 3 * 4 * Copyright (c) 2016 Alexander Graf 5 * 6 * SPDX-License-Identifier: GPL-2.0+ 7 */ 8 9 #include <common.h> 10 #include <efi_loader.h> 11 #include <malloc.h> 12 #include <asm/global_data.h> 13 #include <libfdt_env.h> 14 #include <linux/list_sort.h> 15 #include <inttypes.h> 16 #include <watchdog.h> 17 18 DECLARE_GLOBAL_DATA_PTR; 19 20 struct efi_mem_list { 21 struct list_head link; 22 struct efi_mem_desc desc; 23 }; 24 25 #define EFI_CARVE_NO_OVERLAP -1 26 #define EFI_CARVE_LOOP_AGAIN -2 27 #define EFI_CARVE_OVERLAPS_NONRAM -3 28 29 /* This list contains all memory map items */ 30 LIST_HEAD(efi_mem); 31 32 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 33 void *efi_bounce_buffer; 34 #endif 35 36 /* 37 * Sorts the memory list from highest address to lowest address 38 * 39 * When allocating memory we should always start from the highest 40 * address chunk, so sort the memory list such that the first list 41 * iterator gets the highest address and goes lower from there. 42 */ 43 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b) 44 { 45 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link); 46 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link); 47 48 if (mema->desc.physical_start == memb->desc.physical_start) 49 return 0; 50 else if (mema->desc.physical_start < memb->desc.physical_start) 51 return 1; 52 else 53 return -1; 54 } 55 56 static void efi_mem_sort(void) 57 { 58 list_sort(NULL, &efi_mem, efi_mem_cmp); 59 } 60 61 /* 62 * Unmaps all memory occupied by the carve_desc region from the 63 * list entry pointed to by map. 64 * 65 * Returns 1 if carving was performed or 0 if the regions don't overlap. 66 * Returns -1 if it would affect non-RAM regions but overlap_only_ram is set. 67 * Carving is only guaranteed to complete when all regions return 0. 68 */ 69 static int efi_mem_carve_out(struct efi_mem_list *map, 70 struct efi_mem_desc *carve_desc, 71 bool overlap_only_ram) 72 { 73 struct efi_mem_list *newmap; 74 struct efi_mem_desc *map_desc = &map->desc; 75 uint64_t map_start = map_desc->physical_start; 76 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT); 77 uint64_t carve_start = carve_desc->physical_start; 78 uint64_t carve_end = carve_start + 79 (carve_desc->num_pages << EFI_PAGE_SHIFT); 80 81 /* check whether we're overlapping */ 82 if ((carve_end <= map_start) || (carve_start >= map_end)) 83 return EFI_CARVE_NO_OVERLAP; 84 85 /* We're overlapping with non-RAM, warn the caller if desired */ 86 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY)) 87 return EFI_CARVE_OVERLAPS_NONRAM; 88 89 /* Sanitize carve_start and carve_end to lie within our bounds */ 90 carve_start = max(carve_start, map_start); 91 carve_end = min(carve_end, map_end); 92 93 /* Carving at the beginning of our map? Just move it! */ 94 if (carve_start == map_start) { 95 if (map_end == carve_end) { 96 /* Full overlap, just remove map */ 97 list_del(&map->link); 98 } 99 100 map_desc->physical_start = carve_end; 101 map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT; 102 return (carve_end - carve_start) >> EFI_PAGE_SHIFT; 103 } 104 105 /* 106 * Overlapping maps, just split the list map at carve_start, 107 * it will get moved or removed in the next iteration. 108 * 109 * [ map_desc |__carve_start__| newmap ] 110 */ 111 112 /* Create a new map from [ carve_start ... map_end ] */ 113 newmap = calloc(1, sizeof(*newmap)); 114 newmap->desc = map->desc; 115 newmap->desc.physical_start = carve_start; 116 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT; 117 list_add_tail(&newmap->link, &efi_mem); 118 119 /* Shrink the map to [ map_start ... carve_start ] */ 120 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT; 121 122 return EFI_CARVE_LOOP_AGAIN; 123 } 124 125 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type, 126 bool overlap_only_ram) 127 { 128 struct list_head *lhandle; 129 struct efi_mem_list *newlist; 130 bool carve_again; 131 uint64_t carved_pages = 0; 132 133 if (!pages) 134 return start; 135 136 newlist = calloc(1, sizeof(*newlist)); 137 newlist->desc.type = memory_type; 138 newlist->desc.physical_start = start; 139 newlist->desc.virtual_start = start; 140 newlist->desc.num_pages = pages; 141 142 switch (memory_type) { 143 case EFI_RUNTIME_SERVICES_CODE: 144 case EFI_RUNTIME_SERVICES_DATA: 145 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) | 146 (1ULL << EFI_MEMORY_RUNTIME_SHIFT); 147 break; 148 case EFI_MMAP_IO: 149 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT; 150 break; 151 default: 152 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT; 153 break; 154 } 155 156 /* Add our new map */ 157 do { 158 carve_again = false; 159 list_for_each(lhandle, &efi_mem) { 160 struct efi_mem_list *lmem; 161 int r; 162 163 lmem = list_entry(lhandle, struct efi_mem_list, link); 164 r = efi_mem_carve_out(lmem, &newlist->desc, 165 overlap_only_ram); 166 switch (r) { 167 case EFI_CARVE_OVERLAPS_NONRAM: 168 /* 169 * The user requested to only have RAM overlaps, 170 * but we hit a non-RAM region. Error out. 171 */ 172 return 0; 173 case EFI_CARVE_NO_OVERLAP: 174 /* Just ignore this list entry */ 175 break; 176 case EFI_CARVE_LOOP_AGAIN: 177 /* 178 * We split an entry, but need to loop through 179 * the list again to actually carve it. 180 */ 181 carve_again = true; 182 break; 183 default: 184 /* We carved a number of pages */ 185 carved_pages += r; 186 carve_again = true; 187 break; 188 } 189 190 if (carve_again) { 191 /* The list changed, we need to start over */ 192 break; 193 } 194 } 195 } while (carve_again); 196 197 if (overlap_only_ram && (carved_pages != pages)) { 198 /* 199 * The payload wanted to have RAM overlaps, but we overlapped 200 * with an unallocated region. Error out. 201 */ 202 return 0; 203 } 204 205 /* Add our new map */ 206 list_add_tail(&newlist->link, &efi_mem); 207 208 /* And make sure memory is listed in descending order */ 209 efi_mem_sort(); 210 211 return start; 212 } 213 214 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr) 215 { 216 struct list_head *lhandle; 217 218 list_for_each(lhandle, &efi_mem) { 219 struct efi_mem_list *lmem = list_entry(lhandle, 220 struct efi_mem_list, link); 221 struct efi_mem_desc *desc = &lmem->desc; 222 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT; 223 uint64_t desc_end = desc->physical_start + desc_len; 224 uint64_t curmax = min(max_addr, desc_end); 225 uint64_t ret = curmax - len; 226 227 /* We only take memory from free RAM */ 228 if (desc->type != EFI_CONVENTIONAL_MEMORY) 229 continue; 230 231 /* Out of bounds for max_addr */ 232 if ((ret + len) > max_addr) 233 continue; 234 235 /* Out of bounds for upper map limit */ 236 if ((ret + len) > desc_end) 237 continue; 238 239 /* Out of bounds for lower map limit */ 240 if (ret < desc->physical_start) 241 continue; 242 243 /* Return the highest address in this map within bounds */ 244 return ret; 245 } 246 247 return 0; 248 } 249 250 efi_status_t efi_allocate_pages(int type, int memory_type, 251 unsigned long pages, uint64_t *memory) 252 { 253 u64 len = pages << EFI_PAGE_SHIFT; 254 efi_status_t r = EFI_SUCCESS; 255 uint64_t addr; 256 257 switch (type) { 258 case 0: 259 /* Any page */ 260 addr = efi_find_free_memory(len, gd->start_addr_sp); 261 if (!addr) { 262 r = EFI_NOT_FOUND; 263 break; 264 } 265 break; 266 case 1: 267 /* Max address */ 268 addr = efi_find_free_memory(len, *memory); 269 if (!addr) { 270 r = EFI_NOT_FOUND; 271 break; 272 } 273 break; 274 case 2: 275 /* Exact address, reserve it. The addr is already in *memory. */ 276 addr = *memory; 277 break; 278 default: 279 /* UEFI doesn't specify other allocation types */ 280 r = EFI_INVALID_PARAMETER; 281 break; 282 } 283 284 if (r == EFI_SUCCESS) { 285 uint64_t ret; 286 287 /* Reserve that map in our memory maps */ 288 ret = efi_add_memory_map(addr, pages, memory_type, true); 289 if (ret == addr) { 290 *memory = addr; 291 } else { 292 /* Map would overlap, bail out */ 293 r = EFI_OUT_OF_RESOURCES; 294 } 295 } 296 297 return r; 298 } 299 300 void *efi_alloc(uint64_t len, int memory_type) 301 { 302 uint64_t ret = 0; 303 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; 304 efi_status_t r; 305 306 r = efi_allocate_pages(0, memory_type, pages, &ret); 307 if (r == EFI_SUCCESS) 308 return (void*)(uintptr_t)ret; 309 310 return NULL; 311 } 312 313 efi_status_t efi_free_pages(uint64_t memory, unsigned long pages) 314 { 315 /* We don't free, let's cross our fingers we have plenty RAM */ 316 return EFI_SUCCESS; 317 } 318 319 efi_status_t efi_get_memory_map(unsigned long *memory_map_size, 320 struct efi_mem_desc *memory_map, 321 unsigned long *map_key, 322 unsigned long *descriptor_size, 323 uint32_t *descriptor_version) 324 { 325 ulong map_size = 0; 326 int map_entries = 0; 327 struct list_head *lhandle; 328 329 list_for_each(lhandle, &efi_mem) 330 map_entries++; 331 332 map_size = map_entries * sizeof(struct efi_mem_desc); 333 334 *memory_map_size = map_size; 335 336 if (descriptor_size) 337 *descriptor_size = sizeof(struct efi_mem_desc); 338 339 if (*memory_map_size < map_size) 340 return EFI_BUFFER_TOO_SMALL; 341 342 /* Copy list into array */ 343 if (memory_map) { 344 /* Return the list in ascending order */ 345 memory_map = &memory_map[map_entries - 1]; 346 list_for_each(lhandle, &efi_mem) { 347 struct efi_mem_list *lmem; 348 349 lmem = list_entry(lhandle, struct efi_mem_list, link); 350 *memory_map = lmem->desc; 351 memory_map--; 352 } 353 } 354 355 return EFI_SUCCESS; 356 } 357 358 int efi_memory_init(void) 359 { 360 unsigned long runtime_start, runtime_end, runtime_pages; 361 unsigned long uboot_start, uboot_pages; 362 unsigned long uboot_stack_size = 16 * 1024 * 1024; 363 int i; 364 365 /* Add RAM */ 366 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 367 u64 ram_start = gd->bd->bi_dram[i].start; 368 u64 ram_size = gd->bd->bi_dram[i].size; 369 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; 370 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; 371 372 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY, 373 false); 374 } 375 376 /* Add U-Boot */ 377 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK; 378 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT; 379 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false); 380 381 /* Add Runtime Services */ 382 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK; 383 runtime_end = (ulong)&__efi_runtime_stop; 384 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; 385 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT; 386 efi_add_memory_map(runtime_start, runtime_pages, 387 EFI_RUNTIME_SERVICES_CODE, false); 388 389 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 390 /* Request a 32bit 64MB bounce buffer region */ 391 uint64_t efi_bounce_buffer_addr = 0xffffffff; 392 393 if (efi_allocate_pages(1, EFI_LOADER_DATA, 394 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT, 395 &efi_bounce_buffer_addr) != EFI_SUCCESS) 396 return -1; 397 398 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr; 399 #endif 400 401 return 0; 402 } 403