blob: c6f1dd09456e13dc4f772d191139dc5726f8110b [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf8623f922016-03-04 01:10:04 +01002/*
3 * EFI application memory management
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf8623f922016-03-04 01:10:04 +01006 */
7
Heinrich Schuchardt45640252023-03-19 09:20:22 +01008#define LOG_CATEGORY LOGC_EFI
9
Alexander Graf8623f922016-03-04 01:10:04 +010010#include <efi_loader.h>
Simon Glass6980b6b2019-11-14 12:57:45 -070011#include <init.h>
Heinrich Schuchardt45640252023-03-19 09:20:22 +010012#include <log.h>
Alexander Graf8623f922016-03-04 01:10:04 +010013#include <malloc.h>
Alexander Grafcd405172018-06-18 17:23:15 +020014#include <mapmem.h>
Simon Glass3e852172018-03-08 21:53:27 +010015#include <watchdog.h>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Heinrich Schuchardt211ff082024-04-05 10:12:57 +020018#include <asm/sections.h>
Alexander Grafde2a13b2016-03-30 16:38:29 +020019#include <linux/list_sort.h>
Alexander Graf15937a22018-09-17 13:54:33 +020020#include <linux/sizes.h>
Alexander Graf8623f922016-03-04 01:10:04 +010021
22DECLARE_GLOBAL_DATA_PTR;
23
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010024/* Magic number identifying memory allocated from pool */
25#define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
26
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +020027efi_uintn_t efi_memory_map_key;
28
Alexander Graf8623f922016-03-04 01:10:04 +010029struct efi_mem_list {
30 struct list_head link;
31 struct efi_mem_desc desc;
32};
33
Alexander Graf66d86232016-05-27 12:25:03 +020034#define EFI_CARVE_NO_OVERLAP -1
35#define EFI_CARVE_LOOP_AGAIN -2
36#define EFI_CARVE_OVERLAPS_NONRAM -3
Heinrich Schuchardt17fac0e2023-07-30 12:27:03 +020037#define EFI_CARVE_OUT_OF_RESOURCES -4
Alexander Graf66d86232016-05-27 12:25:03 +020038
Alexander Graf8623f922016-03-04 01:10:04 +010039/* This list contains all memory map items */
Bin Mengc8025a32023-04-05 20:15:18 +080040static LIST_HEAD(efi_mem);
Alexander Graf8623f922016-03-04 01:10:04 +010041
Alexander Graf7c00a3c2016-05-11 18:25:48 +020042#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
43void *efi_bounce_buffer;
44#endif
45
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010046/**
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020047 * struct efi_pool_allocation - memory block allocated from pool
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010048 *
49 * @num_pages: number of pages allocated
50 * @checksum: checksum
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020051 * @data: allocated pool memory
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010052 *
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020053 * U-Boot services each UEFI AllocatePool() request as a separate
54 * (multiple) page allocation. We have to track the number of pages
Stefan Brüns67b67d92016-10-09 22:17:26 +020055 * to be able to free the correct amount later.
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020056 *
57 * The checksum calculated in function checksum() is used in FreePool() to avoid
58 * freeing memory not allocated by AllocatePool() and duplicate freeing.
59 *
Stefan Brüns67b67d92016-10-09 22:17:26 +020060 * EFI requires 8 byte alignment for pool allocations, so we can
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020061 * prepend each allocation with these header fields.
Stefan Brüns67b67d92016-10-09 22:17:26 +020062 */
63struct efi_pool_allocation {
64 u64 num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010065 u64 checksum;
Rob Clarke597fd42017-09-13 18:05:36 -040066 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns67b67d92016-10-09 22:17:26 +020067};
68
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010069/**
70 * checksum() - calculate checksum for memory allocated from pool
71 *
72 * @alloc: allocation header
73 * Return: checksum, always non-zero
74 */
75static u64 checksum(struct efi_pool_allocation *alloc)
76{
77 u64 addr = (uintptr_t)alloc;
78 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
79 EFI_ALLOC_POOL_MAGIC;
80 if (!ret)
81 ++ret;
82 return ret;
83}
84
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +010085/**
86 * efi_mem_cmp() - comparator function for sorting memory map
87 *
Alexander Grafde2a13b2016-03-30 16:38:29 +020088 * Sorts the memory list from highest address to lowest address
89 *
90 * When allocating memory we should always start from the highest
91 * address chunk, so sort the memory list such that the first list
92 * iterator gets the highest address and goes lower from there.
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +010093 *
94 * @priv: unused
95 * @a: first memory area
96 * @b: second memory area
97 * Return: 1 if @a is before @b, -1 if @b is before @a, 0 if equal
Alexander Grafde2a13b2016-03-30 16:38:29 +020098 */
99static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
100{
101 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
102 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
103
104 if (mema->desc.physical_start == memb->desc.physical_start)
105 return 0;
106 else if (mema->desc.physical_start < memb->desc.physical_start)
107 return 1;
108 else
109 return -1;
110}
111
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100112/**
113 * desc_get_end() - get end address of memory area
114 *
115 * @desc: memory descriptor
116 * Return: end address + 1
117 */
Alexander Grafd7288142018-09-16 17:05:29 +0200118static uint64_t desc_get_end(struct efi_mem_desc *desc)
119{
120 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
121}
122
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100123/**
124 * efi_mem_sort() - sort memory map
125 *
126 * Sort the memory map and then try to merge adjacent memory areas.
127 */
Alexander Grafde2a13b2016-03-30 16:38:29 +0200128static void efi_mem_sort(void)
129{
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530130 struct efi_mem_list *lmem;
Alexander Grafd7288142018-09-16 17:05:29 +0200131 struct efi_mem_list *prevmem = NULL;
132 bool merge_again = true;
133
Alexander Grafde2a13b2016-03-30 16:38:29 +0200134 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Grafd7288142018-09-16 17:05:29 +0200135
136 /* Now merge entries that can be merged */
137 while (merge_again) {
138 merge_again = false;
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530139 list_for_each_entry(lmem, &efi_mem, link) {
Sughosh Ganu7d2c2792024-07-30 16:41:30 +0530140 struct efi_mem_desc *prev;
Alexander Grafd7288142018-09-16 17:05:29 +0200141 struct efi_mem_desc *cur;
142 uint64_t pages;
143
Alexander Grafd7288142018-09-16 17:05:29 +0200144 if (!prevmem) {
145 prevmem = lmem;
146 continue;
147 }
148
149 cur = &lmem->desc;
Sughosh Ganu7d2c2792024-07-30 16:41:30 +0530150 prev = &prevmem->desc;
Alexander Grafd7288142018-09-16 17:05:29 +0200151
152 if ((desc_get_end(cur) == prev->physical_start) &&
153 (prev->type == cur->type) &&
154 (prev->attribute == cur->attribute)) {
155 /* There is an existing map before, reuse it */
156 pages = cur->num_pages;
157 prev->num_pages += pages;
158 prev->physical_start -= pages << EFI_PAGE_SHIFT;
159 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
160 list_del(&lmem->link);
161 free(lmem);
162
163 merge_again = true;
164 break;
165 }
166
167 prevmem = lmem;
168 }
169 }
Alexander Grafde2a13b2016-03-30 16:38:29 +0200170}
171
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100172/**
173 * efi_mem_carve_out() - unmap memory region
Alexander Graf8623f922016-03-04 01:10:04 +0100174 *
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200175 * @map: memory map
176 * @carve_desc: memory region to unmap
177 * @overlap_only_ram: the carved out region may only overlap RAM
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100178 * Return: the number of overlapping pages which have been
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200179 * removed from the map,
180 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
181 * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
182 * and the map contains anything but free ram
183 * (only when overlap_only_ram is true),
184 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
185 * traversed again, as it has been altered.
186 *
187 * Unmaps all memory occupied by the carve_desc region from the list entry
188 * pointed to by map.
Stefan Brüns1b00dc12016-10-01 23:32:23 +0200189 *
190 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200191 * to re-add the already carved out pages to the mapping.
Alexander Graf8623f922016-03-04 01:10:04 +0100192 */
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200193static s64 efi_mem_carve_out(struct efi_mem_list *map,
Alexander Graf8623f922016-03-04 01:10:04 +0100194 struct efi_mem_desc *carve_desc,
195 bool overlap_only_ram)
196{
197 struct efi_mem_list *newmap;
198 struct efi_mem_desc *map_desc = &map->desc;
199 uint64_t map_start = map_desc->physical_start;
200 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
201 uint64_t carve_start = carve_desc->physical_start;
202 uint64_t carve_end = carve_start +
203 (carve_desc->num_pages << EFI_PAGE_SHIFT);
204
205 /* check whether we're overlapping */
206 if ((carve_end <= map_start) || (carve_start >= map_end))
Alexander Graf66d86232016-05-27 12:25:03 +0200207 return EFI_CARVE_NO_OVERLAP;
Alexander Graf8623f922016-03-04 01:10:04 +0100208
209 /* We're overlapping with non-RAM, warn the caller if desired */
210 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
Alexander Graf66d86232016-05-27 12:25:03 +0200211 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf8623f922016-03-04 01:10:04 +0100212
213 /* Sanitize carve_start and carve_end to lie within our bounds */
214 carve_start = max(carve_start, map_start);
215 carve_end = min(carve_end, map_end);
216
217 /* Carving at the beginning of our map? Just move it! */
218 if (carve_start == map_start) {
219 if (map_end == carve_end) {
220 /* Full overlap, just remove map */
221 list_del(&map->link);
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200222 free(map);
223 } else {
224 map->desc.physical_start = carve_end;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200225 map->desc.virtual_start = carve_end;
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200226 map->desc.num_pages = (map_end - carve_end)
227 >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100228 }
229
Alexander Graf66d86232016-05-27 12:25:03 +0200230 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100231 }
232
233 /*
234 * Overlapping maps, just split the list map at carve_start,
235 * it will get moved or removed in the next iteration.
236 *
237 * [ map_desc |__carve_start__| newmap ]
238 */
239
240 /* Create a new map from [ carve_start ... map_end ] */
241 newmap = calloc(1, sizeof(*newmap));
Heinrich Schuchardt17fac0e2023-07-30 12:27:03 +0200242 if (!newmap)
243 return EFI_CARVE_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100244 newmap->desc = map->desc;
245 newmap->desc.physical_start = carve_start;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200246 newmap->desc.virtual_start = carve_start;
Alexander Graf8623f922016-03-04 01:10:04 +0100247 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
Stefan Brüns9d656512016-10-01 23:32:28 +0200248 /* Insert before current entry (descending address order) */
249 list_add_tail(&newmap->link, &map->link);
Alexander Graf8623f922016-03-04 01:10:04 +0100250
251 /* Shrink the map to [ map_start ... carve_start ] */
252 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
253
Alexander Graf66d86232016-05-27 12:25:03 +0200254 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf8623f922016-03-04 01:10:04 +0100255}
256
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100257/**
Michael Walle282d3862020-05-17 12:29:19 +0200258 * efi_add_memory_map_pg() - add pages to the memory map
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100259 *
260 * @start: start address, must be a multiple of EFI_PAGE_SIZE
261 * @pages: number of pages to add
262 * @memory_type: type of memory added
Maxim Uvarovec631ea2020-08-28 22:47:41 +0300263 * @overlap_only_ram: region may only overlap RAM
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100264 * Return: status code
265 */
Michael Walle282d3862020-05-17 12:29:19 +0200266static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
267 int memory_type,
268 bool overlap_only_ram)
Alexander Graf8623f922016-03-04 01:10:04 +0100269{
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530270 struct efi_mem_list *lmem;
Alexander Graf8623f922016-03-04 01:10:04 +0100271 struct efi_mem_list *newlist;
Alexander Graf66d86232016-05-27 12:25:03 +0200272 bool carve_again;
273 uint64_t carved_pages = 0;
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200274 struct efi_event *evt;
Alexander Graf8623f922016-03-04 01:10:04 +0100275
Heinrich Schuchardt706a2fe2019-04-04 21:50:02 +0200276 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
277 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
Andreas Färber102f8bd2016-07-17 06:57:11 +0200278
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200279 if (memory_type >= EFI_MAX_MEMORY_TYPE)
280 return EFI_INVALID_PARAMETER;
281
Alexander Graf8623f922016-03-04 01:10:04 +0100282 if (!pages)
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100283 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100284
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200285 ++efi_memory_map_key;
Alexander Graf8623f922016-03-04 01:10:04 +0100286 newlist = calloc(1, sizeof(*newlist));
Heinrich Schuchardt71c24442023-07-30 12:36:17 +0200287 if (!newlist)
288 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100289 newlist->desc.type = memory_type;
290 newlist->desc.physical_start = start;
291 newlist->desc.virtual_start = start;
292 newlist->desc.num_pages = pages;
293
294 switch (memory_type) {
295 case EFI_RUNTIME_SERVICES_CODE:
296 case EFI_RUNTIME_SERVICES_DATA:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200297 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100298 break;
299 case EFI_MMAP_IO:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200300 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100301 break;
302 default:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200303 newlist->desc.attribute = EFI_MEMORY_WB;
Alexander Graf8623f922016-03-04 01:10:04 +0100304 break;
305 }
306
307 /* Add our new map */
308 do {
Alexander Graf66d86232016-05-27 12:25:03 +0200309 carve_again = false;
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530310 list_for_each_entry(lmem, &efi_mem, link) {
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200311 s64 r;
Alexander Graf8623f922016-03-04 01:10:04 +0100312
Alexander Graf8623f922016-03-04 01:10:04 +0100313 r = efi_mem_carve_out(lmem, &newlist->desc,
314 overlap_only_ram);
Alexander Graf66d86232016-05-27 12:25:03 +0200315 switch (r) {
Heinrich Schuchardt17fac0e2023-07-30 12:27:03 +0200316 case EFI_CARVE_OUT_OF_RESOURCES:
317 free(newlist);
318 return EFI_OUT_OF_RESOURCES;
Alexander Graf66d86232016-05-27 12:25:03 +0200319 case EFI_CARVE_OVERLAPS_NONRAM:
320 /*
321 * The user requested to only have RAM overlaps,
322 * but we hit a non-RAM region. Error out.
323 */
Heinrich Schuchardt9f0d3ad2023-07-30 12:59:32 +0200324 free(newlist);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100325 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200326 case EFI_CARVE_NO_OVERLAP:
327 /* Just ignore this list entry */
328 break;
329 case EFI_CARVE_LOOP_AGAIN:
330 /*
331 * We split an entry, but need to loop through
332 * the list again to actually carve it.
333 */
334 carve_again = true;
335 break;
336 default:
337 /* We carved a number of pages */
338 carved_pages += r;
339 carve_again = true;
Alexander Graf8623f922016-03-04 01:10:04 +0100340 break;
341 }
Alexander Graf66d86232016-05-27 12:25:03 +0200342
343 if (carve_again) {
344 /* The list changed, we need to start over */
345 break;
346 }
Alexander Graf8623f922016-03-04 01:10:04 +0100347 }
Alexander Graf66d86232016-05-27 12:25:03 +0200348 } while (carve_again);
349
350 if (overlap_only_ram && (carved_pages != pages)) {
351 /*
352 * The payload wanted to have RAM overlaps, but we overlapped
353 * with an unallocated region. Error out.
354 */
Heinrich Schuchardt9f0d3ad2023-07-30 12:59:32 +0200355 free(newlist);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100356 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200357 }
Alexander Graf8623f922016-03-04 01:10:04 +0100358
359 /* Add our new map */
360 list_add_tail(&newlist->link, &efi_mem);
361
Alexander Grafde2a13b2016-03-30 16:38:29 +0200362 /* And make sure memory is listed in descending order */
363 efi_mem_sort();
364
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200365 /* Notify that the memory map was changed */
366 list_for_each_entry(evt, &efi_events, link) {
367 if (evt->group &&
368 !guidcmp(evt->group,
369 &efi_guid_event_group_memory_map_change)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200370 efi_signal_event(evt);
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200371 break;
372 }
373 }
374
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100375 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100376}
377
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200378/**
Michael Walle282d3862020-05-17 12:29:19 +0200379 * efi_add_memory_map() - add memory area to the memory map
380 *
381 * @start: start address of the memory area
382 * @size: length in bytes of the memory area
383 * @memory_type: type of memory added
384 *
385 * Return: status code
386 *
387 * This function automatically aligns the start and size of the memory area
388 * to EFI_PAGE_SIZE.
389 */
390efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
391{
392 u64 pages;
393
394 pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
395 start &= ~EFI_PAGE_MASK;
396
397 return efi_add_memory_map_pg(start, pages, memory_type, false);
398}
399
400/**
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200401 * efi_check_allocated() - validate address to be freed
402 *
403 * Check that the address is within allocated memory:
404 *
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200405 * * The address must be in a range of the memory map.
406 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
407 *
408 * Page alignment is not checked as this is not a requirement of
409 * efi_free_pool().
410 *
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200411 * @addr: address of page to be freed
412 * @must_be_allocated: return success if the page is allocated
413 * Return: status code
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200414 */
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200415static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200416{
417 struct efi_mem_list *item;
418
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200419 list_for_each_entry(item, &efi_mem, link) {
420 u64 start = item->desc.physical_start;
421 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
422
423 if (addr >= start && addr < end) {
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200424 if (must_be_allocated ^
425 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200426 return EFI_SUCCESS;
427 else
428 return EFI_NOT_FOUND;
429 }
430 }
431
432 return EFI_NOT_FOUND;
433}
434
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100435/**
436 * efi_find_free_memory() - find free memory pages
437 *
438 * @len: size of memory area needed
439 * @max_addr: highest address to allocate
440 * Return: pointer to free memory area or 0
441 */
Alexander Graf8623f922016-03-04 01:10:04 +0100442static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
443{
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530444 struct efi_mem_list *lmem;
Alexander Graf8623f922016-03-04 01:10:04 +0100445
Alexander Grafc3b2a252018-11-05 00:30:46 +0100446 /*
447 * Prealign input max address, so we simplify our matching
448 * logic below and can just reuse it as return pointer.
449 */
450 max_addr &= ~EFI_PAGE_MASK;
451
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530452 list_for_each_entry(lmem, &efi_mem, link) {
Alexander Graf8623f922016-03-04 01:10:04 +0100453 struct efi_mem_desc *desc = &lmem->desc;
454 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
455 uint64_t desc_end = desc->physical_start + desc_len;
456 uint64_t curmax = min(max_addr, desc_end);
457 uint64_t ret = curmax - len;
458
459 /* We only take memory from free RAM */
460 if (desc->type != EFI_CONVENTIONAL_MEMORY)
461 continue;
462
463 /* Out of bounds for max_addr */
464 if ((ret + len) > max_addr)
465 continue;
466
467 /* Out of bounds for upper map limit */
468 if ((ret + len) > desc_end)
469 continue;
470
471 /* Out of bounds for lower map limit */
472 if (ret < desc->physical_start)
473 continue;
474
475 /* Return the highest address in this map within bounds */
476 return ret;
477 }
478
479 return 0;
480}
481
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100482/**
483 * efi_allocate_pages - allocate memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100484 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100485 * @type: type of allocation to be performed
486 * @memory_type: usage type of the allocated memory
487 * @pages: number of pages to be allocated
488 * @memory: allocated memory
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100489 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100490 */
Heinrich Schuchardt98228942021-08-17 15:05:31 +0200491efi_status_t efi_allocate_pages(enum efi_allocate_type type,
492 enum efi_memory_type memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100493 efi_uintn_t pages, uint64_t *memory)
Alexander Graf8623f922016-03-04 01:10:04 +0100494{
Heinrich Schuchardtf0c16f92023-07-30 11:31:08 +0200495 u64 len;
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200496 efi_status_t ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100497 uint64_t addr;
498
Heinrich Schuchardtf7272332019-04-23 00:30:53 +0200499 /* Check import parameters */
500 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
501 memory_type <= 0x6FFFFFFF)
502 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200503 if (!memory)
504 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtf0c16f92023-07-30 11:31:08 +0200505 len = (u64)pages << EFI_PAGE_SHIFT;
506 /* Catch possible overflow on 64bit systems */
507 if (sizeof(efi_uintn_t) == sizeof(u64) &&
508 (len >> EFI_PAGE_SHIFT) != (u64)pages)
509 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200510
Alexander Graf8623f922016-03-04 01:10:04 +0100511 switch (type) {
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100512 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf8623f922016-03-04 01:10:04 +0100513 /* Any page */
Stephen Warren66208b92018-08-30 15:43:45 -0600514 addr = efi_find_free_memory(len, -1ULL);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200515 if (!addr)
516 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100517 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100518 case EFI_ALLOCATE_MAX_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100519 /* Max address */
520 addr = efi_find_free_memory(len, *memory);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200521 if (!addr)
522 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100523 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100524 case EFI_ALLOCATE_ADDRESS:
Heinrich Schuchardt1c180b82022-11-06 01:52:13 +0100525 if (*memory & EFI_PAGE_MASK)
526 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100527 /* Exact address, reserve it. The addr is already in *memory. */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200528 ret = efi_check_allocated(*memory, false);
529 if (ret != EFI_SUCCESS)
530 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100531 addr = *memory;
532 break;
533 default:
534 /* UEFI doesn't specify other allocation types */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200535 return EFI_INVALID_PARAMETER;
Alexander Graf8623f922016-03-04 01:10:04 +0100536 }
537
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200538 /* Reserve that map in our memory maps */
Michael Walle282d3862020-05-17 12:29:19 +0200539 ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
540 if (ret != EFI_SUCCESS)
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200541 /* Map would overlap, bail out */
542 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100543
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200544 *memory = addr;
Alexander Graf8623f922016-03-04 01:10:04 +0100545
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200546 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100547}
548
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100549/**
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100550 * efi_free_pages() - free memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100551 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100552 * @memory: start of the memory area to be freed
553 * @pages: number of pages to be freed
554 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100555 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100556efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf8623f922016-03-04 01:10:04 +0100557{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200558 efi_status_t ret;
559
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200560 ret = efi_check_allocated(memory, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200561 if (ret != EFI_SUCCESS)
562 return ret;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200563
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100564 /* Sanity check */
Heinrich Schuchardt7bf4bc92019-04-25 18:41:40 +0200565 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100566 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
567 memory, pages);
568 return EFI_INVALID_PARAMETER;
569 }
570
Michael Walle282d3862020-05-17 12:29:19 +0200571 ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY,
572 false);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100573 if (ret != EFI_SUCCESS)
574 return EFI_NOT_FOUND;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200575
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100576 return ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100577}
578
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100579/**
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100580 * efi_alloc_aligned_pages() - allocate aligned memory pages
Ilias Apalodimas6e01c232021-10-11 15:10:23 +0300581 *
582 * @len: len in bytes
583 * @memory_type: usage type of the allocated memory
584 * @align: alignment in bytes
585 * Return: aligned memory or NULL
586 */
587void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
588{
589 u64 req_pages = efi_size_in_pages(len);
590 u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
591 u64 free_pages;
592 u64 aligned_mem;
593 efi_status_t r;
594 u64 mem;
595
596 /* align must be zero or a power of two */
597 if (align & (align - 1))
598 return NULL;
599
600 /* Check for overflow */
601 if (true_pages < req_pages)
602 return NULL;
603
604 if (align < EFI_PAGE_SIZE) {
605 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
606 req_pages, &mem);
607 return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL;
608 }
609
610 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
611 true_pages, &mem);
612 if (r != EFI_SUCCESS)
613 return NULL;
614
615 aligned_mem = ALIGN(mem, align);
616 /* Free pages before alignment */
617 free_pages = efi_size_in_pages(aligned_mem - mem);
618 if (free_pages)
619 efi_free_pages(mem, free_pages);
620
621 /* Free trailing pages */
622 free_pages = true_pages - (req_pages + free_pages);
623 if (free_pages) {
624 mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
625 efi_free_pages(mem, free_pages);
626 }
627
628 return (void *)(uintptr_t)aligned_mem;
629}
630
631/**
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100632 * efi_allocate_pool - allocate memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100633 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100634 * @pool_type: type of the pool from which memory is to be allocated
635 * @size: number of bytes to be allocated
636 * @buffer: allocated memory
637 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100638 */
Heinrich Schuchardt98228942021-08-17 15:05:31 +0200639efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer)
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200640{
641 efi_status_t r;
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100642 u64 addr;
Alexander Grafcd405172018-06-18 17:23:15 +0200643 struct efi_pool_allocation *alloc;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100644 u64 num_pages = efi_size_in_pages(size +
645 sizeof(struct efi_pool_allocation));
Stefan Brüns67b67d92016-10-09 22:17:26 +0200646
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200647 if (!buffer)
648 return EFI_INVALID_PARAMETER;
649
Stefan Brüns67b67d92016-10-09 22:17:26 +0200650 if (size == 0) {
651 *buffer = NULL;
652 return EFI_SUCCESS;
653 }
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200654
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200655 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100656 &addr);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200657 if (r == EFI_SUCCESS) {
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100658 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200659 alloc->num_pages = num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100660 alloc->checksum = checksum(alloc);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200661 *buffer = alloc->data;
662 }
663
664 return r;
665}
666
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100667/**
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100668 * efi_alloc() - allocate boot services data pool memory
669 *
670 * Allocate memory from pool and zero it out.
671 *
672 * @size: number of bytes to allocate
673 * Return: pointer to allocated memory or NULL
674 */
675void *efi_alloc(size_t size)
676{
677 void *buf;
678
679 if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, size, &buf) !=
680 EFI_SUCCESS) {
681 log_err("out of memory");
682 return NULL;
683 }
684 memset(buf, 0, size);
685
686 return buf;
687}
688
689/**
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100690 * efi_free_pool() - free memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100691 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100692 * @buffer: start of memory to be freed
693 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100694 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200695efi_status_t efi_free_pool(void *buffer)
696{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200697 efi_status_t ret;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200698 struct efi_pool_allocation *alloc;
699
Heinrich Schuchardt56b587f72019-06-12 07:17:04 +0200700 if (!buffer)
701 return EFI_INVALID_PARAMETER;
702
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200703 ret = efi_check_allocated((uintptr_t)buffer, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200704 if (ret != EFI_SUCCESS)
705 return ret;
xypron.glpk@gmx.de34ddae72017-07-14 19:12:39 +0200706
Stefan Brüns67b67d92016-10-09 22:17:26 +0200707 alloc = container_of(buffer, struct efi_pool_allocation, data);
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100708
709 /* Check that this memory was allocated by efi_allocate_pool() */
710 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
711 alloc->checksum != checksum(alloc)) {
712 printf("%s: illegal free 0x%p\n", __func__, buffer);
713 return EFI_INVALID_PARAMETER;
714 }
715 /* Avoid double free */
716 alloc->checksum = 0;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200717
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200718 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200719
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200720 return ret;
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200721}
722
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100723/**
724 * efi_get_memory_map() - get map describing memory usage.
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100725 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100726 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100727 * on exit the size of the copied memory map
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100728 * @memory_map: buffer to which the memory map is written
729 * @map_key: key for the memory map
730 * @descriptor_size: size of an individual memory descriptor
731 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100732 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100733 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100734efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
735 struct efi_mem_desc *memory_map,
736 efi_uintn_t *map_key,
737 efi_uintn_t *descriptor_size,
738 uint32_t *descriptor_version)
Alexander Graf8623f922016-03-04 01:10:04 +0100739{
Sughosh Ganu38dcdb82024-07-30 16:41:29 +0530740 size_t map_entries;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100741 efi_uintn_t map_size = 0;
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530742 struct efi_mem_list *lmem;
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200743 efi_uintn_t provided_map_size;
Alexander Graf8623f922016-03-04 01:10:04 +0100744
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200745 if (!memory_map_size)
746 return EFI_INVALID_PARAMETER;
747
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200748 provided_map_size = *memory_map_size;
749
Sughosh Ganu38dcdb82024-07-30 16:41:29 +0530750 map_entries = list_count_nodes(&efi_mem);
Alexander Graf9b591022016-04-11 23:51:02 +0200751
752 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf8623f922016-03-04 01:10:04 +0100753
Rob Clarka84c9742017-07-26 14:34:05 -0400754 *memory_map_size = map_size;
755
Alexander Graf8623f922016-03-04 01:10:04 +0100756 if (descriptor_size)
757 *descriptor_size = sizeof(struct efi_mem_desc);
758
Mian Yousaf Kaukab338cf562016-09-05 23:59:22 +0200759 if (descriptor_version)
760 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
761
AKASHI Takahiro3ee138d2020-03-11 15:18:18 +0900762 if (provided_map_size < map_size)
763 return EFI_BUFFER_TOO_SMALL;
764
765 if (!memory_map)
766 return EFI_INVALID_PARAMETER;
767
Alexander Graf8623f922016-03-04 01:10:04 +0100768 /* Copy list into array */
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200769 /* Return the list in ascending order */
770 memory_map = &memory_map[map_entries - 1];
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530771 list_for_each_entry(lmem, &efi_mem, link) {
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200772 *memory_map = lmem->desc;
773 memory_map--;
Alexander Graf8623f922016-03-04 01:10:04 +0100774 }
775
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200776 if (map_key)
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200777 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dee436e632017-07-21 19:05:44 +0200778
Alexander Graf8623f922016-03-04 01:10:04 +0100779 return EFI_SUCCESS;
780}
781
Park, Aidene009e7a2019-09-03 17:43:43 +0000782/**
Heinrich Schuchardt13ce2d92023-01-05 18:26:01 +0100783 * efi_get_memory_map_alloc() - allocate map describing memory usage
784 *
785 * The caller is responsible for calling FreePool() if the call succeeds.
786 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100787 * @map_size: size of the memory map
788 * @memory_map: buffer to which the memory map is written
Heinrich Schuchardt13ce2d92023-01-05 18:26:01 +0100789 * Return: status code
790 */
791efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size,
792 struct efi_mem_desc **memory_map)
793{
794 efi_status_t ret;
795
796 *memory_map = NULL;
797 *map_size = 0;
798 ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL);
799 if (ret == EFI_BUFFER_TOO_SMALL) {
800 *map_size += sizeof(struct efi_mem_desc); /* for the map */
801 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size,
802 (void **)memory_map);
803 if (ret != EFI_SUCCESS)
804 return ret;
805 ret = efi_get_memory_map(map_size, *memory_map,
806 NULL, NULL, NULL);
807 if (ret != EFI_SUCCESS) {
808 efi_free_pool(*memory_map);
809 *memory_map = NULL;
810 }
811 }
812
813 return ret;
814}
815
816/**
Park, Aidene009e7a2019-09-03 17:43:43 +0000817 * efi_add_conventional_memory_map() - add a RAM memory area to the map
818 *
819 * @ram_start: start address of a RAM memory area
820 * @ram_end: end address of a RAM memory area
821 * @ram_top: max address to be used as conventional memory
822 * Return: status code
823 */
824efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
825 u64 ram_top)
826{
827 u64 pages;
828
829 /* Remove partial pages */
830 ram_end &= ~EFI_PAGE_MASK;
831 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
832
833 if (ram_end <= ram_start) {
834 /* Invalid mapping */
835 return EFI_INVALID_PARAMETER;
836 }
837
838 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
839
Michael Walle282d3862020-05-17 12:29:19 +0200840 efi_add_memory_map_pg(ram_start, pages,
841 EFI_CONVENTIONAL_MEMORY, false);
Park, Aidene009e7a2019-09-03 17:43:43 +0000842
843 /*
844 * Boards may indicate to the U-Boot memory core that they
845 * can not support memory above ram_top. Let's honor this
846 * in the efi_loader subsystem too by declaring any memory
847 * above ram_top as "already occupied by firmware".
848 */
849 if (ram_top < ram_start) {
850 /* ram_top is before this region, reserve all */
Michael Walle282d3862020-05-17 12:29:19 +0200851 efi_add_memory_map_pg(ram_start, pages,
852 EFI_BOOT_SERVICES_DATA, true);
Heinrich Schuchardt3546eb22022-04-25 23:54:48 +0200853 } else if (ram_top < ram_end) {
Park, Aidene009e7a2019-09-03 17:43:43 +0000854 /* ram_top is inside this region, reserve parts */
855 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
856
Michael Walle282d3862020-05-17 12:29:19 +0200857 efi_add_memory_map_pg(ram_top, pages,
858 EFI_BOOT_SERVICES_DATA, true);
Park, Aidene009e7a2019-09-03 17:43:43 +0000859 }
860
861 return EFI_SUCCESS;
862}
863
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100864/**
865 * efi_add_known_memory() - add memory banks to map
866 *
867 * This function may be overridden for specific architectures.
868 */
York Suna8e9f0a2017-03-06 09:02:27 -0800869__weak void efi_add_known_memory(void)
Alexander Graf8623f922016-03-04 01:10:04 +0100870{
Heinrich Schuchardt0f0a1b32023-08-14 07:50:53 +0200871 u64 ram_top = gd->ram_top & ~EFI_PAGE_MASK;
Alexander Graf8623f922016-03-04 01:10:04 +0100872 int i;
873
Heinrich Schuchardteb8edcf2019-01-05 23:41:36 +0100874 /*
875 * ram_top is just outside mapped memory. So use an offset of one for
876 * mapping the sandbox address.
877 */
878 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
879
Alexander Grafc214edf2018-11-30 21:24:56 +0100880 /* Fix for 32bit targets with ram_top at 4G */
881 if (!ram_top)
882 ram_top = 0x100000000ULL;
883
Alexander Graf8623f922016-03-04 01:10:04 +0100884 /* Add RAM */
885 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Park, Aidene009e7a2019-09-03 17:43:43 +0000886 u64 ram_end, ram_start;
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100887
Heinrich Schuchardt6738fcd2018-11-18 17:58:46 +0100888 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100889 ram_end = ram_start + gd->bd->bi_dram[i].size;
890
Park, Aidene009e7a2019-09-03 17:43:43 +0000891 efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
Alexander Graf8623f922016-03-04 01:10:04 +0100892 }
York Suna8e9f0a2017-03-06 09:02:27 -0800893}
894
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100895/**
896 * add_u_boot_and_runtime() - add U-Boot code to memory map
897 *
898 * Add memory regions for U-Boot's memory and for the runtime services code.
899 */
Simon Glass78639432018-06-18 17:23:12 +0200900static void add_u_boot_and_runtime(void)
York Suna8e9f0a2017-03-06 09:02:27 -0800901{
902 unsigned long runtime_start, runtime_end, runtime_pages;
Alexander Graf15937a22018-09-17 13:54:33 +0200903 unsigned long runtime_mask = EFI_PAGE_MASK;
York Suna8e9f0a2017-03-06 09:02:27 -0800904 unsigned long uboot_start, uboot_pages;
Heinrich Schuchardtc6dcbe12020-07-29 12:37:35 +0200905 unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
York Suna8e9f0a2017-03-06 09:02:27 -0800906
Alexander Graf8623f922016-03-04 01:10:04 +0100907 /* Add U-Boot */
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100908 uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
909 uboot_stack_size) & ~EFI_PAGE_MASK;
910 uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
911 uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Heinrich Schuchardt296b55c2022-11-29 16:00:41 +0100912 efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE,
Michael Walle282d3862020-05-17 12:29:19 +0200913 false);
Alexander Graf8623f922016-03-04 01:10:04 +0100914
Alexander Graf15937a22018-09-17 13:54:33 +0200915#if defined(__aarch64__)
916 /*
917 * Runtime Services must be 64KiB aligned according to the
918 * "AArch64 Platforms" section in the UEFI spec (2.7+).
919 */
920
921 runtime_mask = SZ_64K - 1;
922#endif
923
924 /*
925 * Add Runtime Services. We mark surrounding boottime code as runtime as
926 * well to fulfill the runtime alignment constraints but avoid padding.
927 */
Ilias Apalodimas2af95532024-04-04 09:35:35 +0300928 runtime_start = (uintptr_t)__efi_runtime_start & ~runtime_mask;
929 runtime_end = (uintptr_t)__efi_runtime_stop;
Alexander Graf15937a22018-09-17 13:54:33 +0200930 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100931 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
Michael Walle282d3862020-05-17 12:29:19 +0200932 efi_add_memory_map_pg(runtime_start, runtime_pages,
933 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass78639432018-06-18 17:23:12 +0200934}
935
936int efi_memory_init(void)
937{
938 efi_add_known_memory();
939
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100940 add_u_boot_and_runtime();
Alexander Graf8623f922016-03-04 01:10:04 +0100941
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200942#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
943 /* Request a 32bit 64MB bounce buffer region */
944 uint64_t efi_bounce_buffer_addr = 0xffffffff;
945
Heinrich Schuchardt296b55c2022-11-29 16:00:41 +0100946 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200947 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
948 &efi_bounce_buffer_addr) != EFI_SUCCESS)
949 return -1;
950
951 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
952#endif
953
Alexander Graf8623f922016-03-04 01:10:04 +0100954 return 0;
955}