blob: 4630387af02e22e7917349a145dcb5e353da8121 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf5d009952016-03-04 01:10:04 +01002/*
3 * EFI application memory management
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf5d009952016-03-04 01:10:04 +01006 */
7
Heinrich Schuchardtf606fab2023-03-19 09:20:22 +01008#define LOG_CATEGORY LOGC_EFI
9
Alexander Graf5d009952016-03-04 01:10:04 +010010#include <common.h>
11#include <efi_loader.h>
Simon Glass67c4e9f2019-11-14 12:57:45 -070012#include <init.h>
Heinrich Schuchardtf606fab2023-03-19 09:20:22 +010013#include <log.h>
Simon Glassbdecaeb2018-03-08 21:53:27 +010014#include <malloc.h>
Alexander Graf282a06c2018-06-18 17:23:15 +020015#include <mapmem.h>
Alexander Graf5d009952016-03-04 01:10:04 +010016#include <watchdog.h>
Simon Glass90526e92020-05-10 11:39:56 -060017#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060018#include <asm/global_data.h>
Simon Glassbdecaeb2018-03-08 21:53:27 +010019#include <linux/list_sort.h>
Alexander Graf7a82c302018-09-17 13:54:33 +020020#include <linux/sizes.h>
Alexander Graf5d009952016-03-04 01:10:04 +010021
22DECLARE_GLOBAL_DATA_PTR;
23
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010024/* Magic number identifying memory allocated from pool */
25#define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
26
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +020027efi_uintn_t efi_memory_map_key;
28
Alexander Graf5d009952016-03-04 01:10:04 +010029struct efi_mem_list {
30 struct list_head link;
31 struct efi_mem_desc desc;
32};
33
Alexander Graf74c16ac2016-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
37
Alexander Graf5d009952016-03-04 01:10:04 +010038/* This list contains all memory map items */
Bin Meng207b6862023-04-05 20:15:18 +080039static LIST_HEAD(efi_mem);
Alexander Graf5d009952016-03-04 01:10:04 +010040
Alexander Graf51735ae2016-05-11 18:25:48 +020041#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
42void *efi_bounce_buffer;
43#endif
44
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010045/**
Heinrich Schuchardt4be077b2019-07-14 12:28:56 +020046 * struct efi_pool_allocation - memory block allocated from pool
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010047 *
48 * @num_pages: number of pages allocated
49 * @checksum: checksum
Heinrich Schuchardt4be077b2019-07-14 12:28:56 +020050 * @data: allocated pool memory
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010051 *
Heinrich Schuchardt4be077b2019-07-14 12:28:56 +020052 * U-Boot services each UEFI AllocatePool() request as a separate
53 * (multiple) page allocation. We have to track the number of pages
Stefan Brüns42417bc2016-10-09 22:17:26 +020054 * to be able to free the correct amount later.
Heinrich Schuchardt4be077b2019-07-14 12:28:56 +020055 *
56 * The checksum calculated in function checksum() is used in FreePool() to avoid
57 * freeing memory not allocated by AllocatePool() and duplicate freeing.
58 *
Stefan Brüns42417bc2016-10-09 22:17:26 +020059 * EFI requires 8 byte alignment for pool allocations, so we can
Heinrich Schuchardt4be077b2019-07-14 12:28:56 +020060 * prepend each allocation with these header fields.
Stefan Brüns42417bc2016-10-09 22:17:26 +020061 */
62struct efi_pool_allocation {
63 u64 num_pages;
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010064 u64 checksum;
Rob Clark946160f2017-09-13 18:05:36 -040065 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns42417bc2016-10-09 22:17:26 +020066};
67
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010068/**
69 * checksum() - calculate checksum for memory allocated from pool
70 *
71 * @alloc: allocation header
72 * Return: checksum, always non-zero
73 */
74static u64 checksum(struct efi_pool_allocation *alloc)
75{
76 u64 addr = (uintptr_t)alloc;
77 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
78 EFI_ALLOC_POOL_MAGIC;
79 if (!ret)
80 ++ret;
81 return ret;
82}
83
Heinrich Schuchardt0763c022023-01-08 01:00:00 +010084/**
85 * efi_mem_cmp() - comparator function for sorting memory map
86 *
Alexander Graf38ce65e2016-03-30 16:38:29 +020087 * Sorts the memory list from highest address to lowest address
88 *
89 * When allocating memory we should always start from the highest
90 * address chunk, so sort the memory list such that the first list
91 * iterator gets the highest address and goes lower from there.
Heinrich Schuchardt0763c022023-01-08 01:00:00 +010092 *
93 * @priv: unused
94 * @a: first memory area
95 * @b: second memory area
96 * Return: 1 if @a is before @b, -1 if @b is before @a, 0 if equal
Alexander Graf38ce65e2016-03-30 16:38:29 +020097 */
98static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
99{
100 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
101 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
102
103 if (mema->desc.physical_start == memb->desc.physical_start)
104 return 0;
105 else if (mema->desc.physical_start < memb->desc.physical_start)
106 return 1;
107 else
108 return -1;
109}
110
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100111/**
112 * desc_get_end() - get end address of memory area
113 *
114 * @desc: memory descriptor
115 * Return: end address + 1
116 */
Alexander Graf7b056672018-09-16 17:05:29 +0200117static uint64_t desc_get_end(struct efi_mem_desc *desc)
118{
119 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
120}
121
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100122/**
123 * efi_mem_sort() - sort memory map
124 *
125 * Sort the memory map and then try to merge adjacent memory areas.
126 */
Alexander Graf38ce65e2016-03-30 16:38:29 +0200127static void efi_mem_sort(void)
128{
Alexander Graf7b056672018-09-16 17:05:29 +0200129 struct list_head *lhandle;
130 struct efi_mem_list *prevmem = NULL;
131 bool merge_again = true;
132
Alexander Graf38ce65e2016-03-30 16:38:29 +0200133 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Graf7b056672018-09-16 17:05:29 +0200134
135 /* Now merge entries that can be merged */
136 while (merge_again) {
137 merge_again = false;
138 list_for_each(lhandle, &efi_mem) {
139 struct efi_mem_list *lmem;
140 struct efi_mem_desc *prev = &prevmem->desc;
141 struct efi_mem_desc *cur;
142 uint64_t pages;
143
144 lmem = list_entry(lhandle, struct efi_mem_list, link);
145 if (!prevmem) {
146 prevmem = lmem;
147 continue;
148 }
149
150 cur = &lmem->desc;
151
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 Graf38ce65e2016-03-30 16:38:29 +0200170}
171
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100172/**
173 * efi_mem_carve_out() - unmap memory region
Alexander Graf5d009952016-03-04 01:10:04 +0100174 *
Heinrich Schuchardt32826142018-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 Schuchardt0763c022023-01-08 01:00:00 +0100178 * Return: the number of overlapping pages which have been
Heinrich Schuchardt32826142018-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üns852efbf2016-10-01 23:32:23 +0200189 *
190 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200191 * to re-add the already carved out pages to the mapping.
Alexander Graf5d009952016-03-04 01:10:04 +0100192 */
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200193static s64 efi_mem_carve_out(struct efi_mem_list *map,
Alexander Graf5d009952016-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 Graf74c16ac2016-05-27 12:25:03 +0200207 return EFI_CARVE_NO_OVERLAP;
Alexander Graf5d009952016-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 Graf74c16ac2016-05-27 12:25:03 +0200211 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf5d009952016-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üns511d0b92016-10-01 23:32:29 +0200222 free(map);
223 } else {
224 map->desc.physical_start = carve_end;
Heinrich Schuchardt9631fa02019-04-11 20:08:54 +0200225 map->desc.virtual_start = carve_end;
Stefan Brüns511d0b92016-10-01 23:32:29 +0200226 map->desc.num_pages = (map_end - carve_end)
227 >> EFI_PAGE_SHIFT;
Alexander Graf5d009952016-03-04 01:10:04 +0100228 }
229
Alexander Graf74c16ac2016-05-27 12:25:03 +0200230 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf5d009952016-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));
242 newmap->desc = map->desc;
243 newmap->desc.physical_start = carve_start;
Heinrich Schuchardt9631fa02019-04-11 20:08:54 +0200244 newmap->desc.virtual_start = carve_start;
Alexander Graf5d009952016-03-04 01:10:04 +0100245 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
Stefan Brünsb6a95172016-10-01 23:32:28 +0200246 /* Insert before current entry (descending address order) */
247 list_add_tail(&newmap->link, &map->link);
Alexander Graf5d009952016-03-04 01:10:04 +0100248
249 /* Shrink the map to [ map_start ... carve_start ] */
250 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
251
Alexander Graf74c16ac2016-05-27 12:25:03 +0200252 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf5d009952016-03-04 01:10:04 +0100253}
254
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100255/**
Michael Walle714497e2020-05-17 12:29:19 +0200256 * efi_add_memory_map_pg() - add pages to the memory map
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100257 *
258 * @start: start address, must be a multiple of EFI_PAGE_SIZE
259 * @pages: number of pages to add
260 * @memory_type: type of memory added
Maxim Uvarovffbeafe2020-08-28 22:47:41 +0300261 * @overlap_only_ram: region may only overlap RAM
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100262 * Return: status code
263 */
Michael Walle714497e2020-05-17 12:29:19 +0200264static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
265 int memory_type,
266 bool overlap_only_ram)
Alexander Graf5d009952016-03-04 01:10:04 +0100267{
268 struct list_head *lhandle;
269 struct efi_mem_list *newlist;
Alexander Graf74c16ac2016-05-27 12:25:03 +0200270 bool carve_again;
271 uint64_t carved_pages = 0;
Heinrich Schuchardte80474a2019-06-04 20:55:12 +0200272 struct efi_event *evt;
Alexander Graf5d009952016-03-04 01:10:04 +0100273
Heinrich Schuchardte301e022019-04-04 21:50:02 +0200274 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
275 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
Andreas Färberc933ed92016-07-17 06:57:11 +0200276
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +0200277 if (memory_type >= EFI_MAX_MEMORY_TYPE)
278 return EFI_INVALID_PARAMETER;
279
Alexander Graf5d009952016-03-04 01:10:04 +0100280 if (!pages)
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100281 return EFI_SUCCESS;
Alexander Graf5d009952016-03-04 01:10:04 +0100282
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +0200283 ++efi_memory_map_key;
Alexander Graf5d009952016-03-04 01:10:04 +0100284 newlist = calloc(1, sizeof(*newlist));
Heinrich Schuchardtba275632023-07-30 12:36:17 +0200285 if (!newlist)
286 return EFI_OUT_OF_RESOURCES;
Alexander Graf5d009952016-03-04 01:10:04 +0100287 newlist->desc.type = memory_type;
288 newlist->desc.physical_start = start;
289 newlist->desc.virtual_start = start;
290 newlist->desc.num_pages = pages;
291
292 switch (memory_type) {
293 case EFI_RUNTIME_SERVICES_CODE:
294 case EFI_RUNTIME_SERVICES_DATA:
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200295 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
Alexander Graf5d009952016-03-04 01:10:04 +0100296 break;
297 case EFI_MMAP_IO:
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200298 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
Alexander Graf5d009952016-03-04 01:10:04 +0100299 break;
300 default:
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200301 newlist->desc.attribute = EFI_MEMORY_WB;
Alexander Graf5d009952016-03-04 01:10:04 +0100302 break;
303 }
304
305 /* Add our new map */
306 do {
Alexander Graf74c16ac2016-05-27 12:25:03 +0200307 carve_again = false;
Alexander Graf5d009952016-03-04 01:10:04 +0100308 list_for_each(lhandle, &efi_mem) {
309 struct efi_mem_list *lmem;
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200310 s64 r;
Alexander Graf5d009952016-03-04 01:10:04 +0100311
312 lmem = list_entry(lhandle, struct efi_mem_list, link);
313 r = efi_mem_carve_out(lmem, &newlist->desc,
314 overlap_only_ram);
Alexander Graf74c16ac2016-05-27 12:25:03 +0200315 switch (r) {
316 case EFI_CARVE_OVERLAPS_NONRAM:
317 /*
318 * The user requested to only have RAM overlaps,
319 * but we hit a non-RAM region. Error out.
320 */
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100321 return EFI_NO_MAPPING;
Alexander Graf74c16ac2016-05-27 12:25:03 +0200322 case EFI_CARVE_NO_OVERLAP:
323 /* Just ignore this list entry */
324 break;
325 case EFI_CARVE_LOOP_AGAIN:
326 /*
327 * We split an entry, but need to loop through
328 * the list again to actually carve it.
329 */
330 carve_again = true;
331 break;
332 default:
333 /* We carved a number of pages */
334 carved_pages += r;
335 carve_again = true;
336 break;
337 }
338
339 if (carve_again) {
340 /* The list changed, we need to start over */
Alexander Graf5d009952016-03-04 01:10:04 +0100341 break;
342 }
343 }
Alexander Graf74c16ac2016-05-27 12:25:03 +0200344 } while (carve_again);
345
346 if (overlap_only_ram && (carved_pages != pages)) {
347 /*
348 * The payload wanted to have RAM overlaps, but we overlapped
349 * with an unallocated region. Error out.
350 */
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100351 return EFI_NO_MAPPING;
Alexander Graf74c16ac2016-05-27 12:25:03 +0200352 }
Alexander Graf5d009952016-03-04 01:10:04 +0100353
354 /* Add our new map */
355 list_add_tail(&newlist->link, &efi_mem);
356
Alexander Graf38ce65e2016-03-30 16:38:29 +0200357 /* And make sure memory is listed in descending order */
358 efi_mem_sort();
359
Heinrich Schuchardte80474a2019-06-04 20:55:12 +0200360 /* Notify that the memory map was changed */
361 list_for_each_entry(evt, &efi_events, link) {
362 if (evt->group &&
363 !guidcmp(evt->group,
364 &efi_guid_event_group_memory_map_change)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200365 efi_signal_event(evt);
Heinrich Schuchardte80474a2019-06-04 20:55:12 +0200366 break;
367 }
368 }
369
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100370 return EFI_SUCCESS;
Alexander Graf5d009952016-03-04 01:10:04 +0100371}
372
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200373/**
Michael Walle714497e2020-05-17 12:29:19 +0200374 * efi_add_memory_map() - add memory area to the memory map
375 *
376 * @start: start address of the memory area
377 * @size: length in bytes of the memory area
378 * @memory_type: type of memory added
379 *
380 * Return: status code
381 *
382 * This function automatically aligns the start and size of the memory area
383 * to EFI_PAGE_SIZE.
384 */
385efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
386{
387 u64 pages;
388
389 pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
390 start &= ~EFI_PAGE_MASK;
391
392 return efi_add_memory_map_pg(start, pages, memory_type, false);
393}
394
395/**
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200396 * efi_check_allocated() - validate address to be freed
397 *
398 * Check that the address is within allocated memory:
399 *
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200400 * * The address must be in a range of the memory map.
401 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
402 *
403 * Page alignment is not checked as this is not a requirement of
404 * efi_free_pool().
405 *
Heinrich Schuchardtf756fe82019-05-11 08:21:17 +0200406 * @addr: address of page to be freed
407 * @must_be_allocated: return success if the page is allocated
408 * Return: status code
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200409 */
Heinrich Schuchardtf756fe82019-05-11 08:21:17 +0200410static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200411{
412 struct efi_mem_list *item;
413
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200414 list_for_each_entry(item, &efi_mem, link) {
415 u64 start = item->desc.physical_start;
416 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
417
418 if (addr >= start && addr < end) {
Heinrich Schuchardtf756fe82019-05-11 08:21:17 +0200419 if (must_be_allocated ^
420 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200421 return EFI_SUCCESS;
422 else
423 return EFI_NOT_FOUND;
424 }
425 }
426
427 return EFI_NOT_FOUND;
428}
429
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100430/**
431 * efi_find_free_memory() - find free memory pages
432 *
433 * @len: size of memory area needed
434 * @max_addr: highest address to allocate
435 * Return: pointer to free memory area or 0
436 */
Alexander Graf5d009952016-03-04 01:10:04 +0100437static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
438{
439 struct list_head *lhandle;
440
Alexander Grafc2e1ad72018-11-05 00:30:46 +0100441 /*
442 * Prealign input max address, so we simplify our matching
443 * logic below and can just reuse it as return pointer.
444 */
445 max_addr &= ~EFI_PAGE_MASK;
446
Alexander Graf5d009952016-03-04 01:10:04 +0100447 list_for_each(lhandle, &efi_mem) {
448 struct efi_mem_list *lmem = list_entry(lhandle,
449 struct efi_mem_list, link);
450 struct efi_mem_desc *desc = &lmem->desc;
451 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
452 uint64_t desc_end = desc->physical_start + desc_len;
453 uint64_t curmax = min(max_addr, desc_end);
454 uint64_t ret = curmax - len;
455
456 /* We only take memory from free RAM */
457 if (desc->type != EFI_CONVENTIONAL_MEMORY)
458 continue;
459
460 /* Out of bounds for max_addr */
461 if ((ret + len) > max_addr)
462 continue;
463
464 /* Out of bounds for upper map limit */
465 if ((ret + len) > desc_end)
466 continue;
467
468 /* Out of bounds for lower map limit */
469 if (ret < desc->physical_start)
470 continue;
471
472 /* Return the highest address in this map within bounds */
473 return ret;
474 }
475
476 return 0;
477}
478
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100479/**
480 * efi_allocate_pages - allocate memory pages
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100481 *
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100482 * @type: type of allocation to be performed
483 * @memory_type: usage type of the allocated memory
484 * @pages: number of pages to be allocated
485 * @memory: allocated memory
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100486 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100487 */
Heinrich Schuchardt49d225e2021-08-17 15:05:31 +0200488efi_status_t efi_allocate_pages(enum efi_allocate_type type,
489 enum efi_memory_type memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100490 efi_uintn_t pages, uint64_t *memory)
Alexander Graf5d009952016-03-04 01:10:04 +0100491{
Heinrich Schuchardt48d183f2023-07-30 11:31:08 +0200492 u64 len;
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200493 efi_status_t ret;
Alexander Graf5d009952016-03-04 01:10:04 +0100494 uint64_t addr;
495
Heinrich Schuchardtf12bcc92019-04-23 00:30:53 +0200496 /* Check import parameters */
497 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
498 memory_type <= 0x6FFFFFFF)
499 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt4d5e0712018-07-02 12:53:53 +0200500 if (!memory)
501 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt48d183f2023-07-30 11:31:08 +0200502 len = (u64)pages << EFI_PAGE_SHIFT;
503 /* Catch possible overflow on 64bit systems */
504 if (sizeof(efi_uintn_t) == sizeof(u64) &&
505 (len >> EFI_PAGE_SHIFT) != (u64)pages)
506 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt4d5e0712018-07-02 12:53:53 +0200507
Alexander Graf5d009952016-03-04 01:10:04 +0100508 switch (type) {
Heinrich Schuchardt7c92fd62018-01-30 21:08:00 +0100509 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf5d009952016-03-04 01:10:04 +0100510 /* Any page */
Stephen Warren14deb5e2018-08-30 15:43:45 -0600511 addr = efi_find_free_memory(len, -1ULL);
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200512 if (!addr)
513 return EFI_OUT_OF_RESOURCES;
Alexander Graf5d009952016-03-04 01:10:04 +0100514 break;
Heinrich Schuchardt7c92fd62018-01-30 21:08:00 +0100515 case EFI_ALLOCATE_MAX_ADDRESS:
Alexander Graf5d009952016-03-04 01:10:04 +0100516 /* Max address */
517 addr = efi_find_free_memory(len, *memory);
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200518 if (!addr)
519 return EFI_OUT_OF_RESOURCES;
Alexander Graf5d009952016-03-04 01:10:04 +0100520 break;
Heinrich Schuchardt7c92fd62018-01-30 21:08:00 +0100521 case EFI_ALLOCATE_ADDRESS:
Heinrich Schuchardt53def682022-11-06 01:52:13 +0100522 if (*memory & EFI_PAGE_MASK)
523 return EFI_NOT_FOUND;
Alexander Graf5d009952016-03-04 01:10:04 +0100524 /* Exact address, reserve it. The addr is already in *memory. */
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200525 ret = efi_check_allocated(*memory, false);
526 if (ret != EFI_SUCCESS)
527 return EFI_NOT_FOUND;
Alexander Graf5d009952016-03-04 01:10:04 +0100528 addr = *memory;
529 break;
530 default:
531 /* UEFI doesn't specify other allocation types */
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200532 return EFI_INVALID_PARAMETER;
Alexander Graf5d009952016-03-04 01:10:04 +0100533 }
534
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200535 /* Reserve that map in our memory maps */
Michael Walle714497e2020-05-17 12:29:19 +0200536 ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
537 if (ret != EFI_SUCCESS)
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200538 /* Map would overlap, bail out */
539 return EFI_OUT_OF_RESOURCES;
Alexander Graf5d009952016-03-04 01:10:04 +0100540
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200541 *memory = addr;
Alexander Graf5d009952016-03-04 01:10:04 +0100542
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200543 return EFI_SUCCESS;
Alexander Graf5d009952016-03-04 01:10:04 +0100544}
545
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100546/**
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100547 * efi_free_pages() - free memory pages
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100548 *
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100549 * @memory: start of the memory area to be freed
550 * @pages: number of pages to be freed
551 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100552 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100553efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf5d009952016-03-04 01:10:04 +0100554{
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200555 efi_status_t ret;
556
Heinrich Schuchardtf756fe82019-05-11 08:21:17 +0200557 ret = efi_check_allocated(memory, true);
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200558 if (ret != EFI_SUCCESS)
559 return ret;
Stefan Brünsb61d8572016-10-01 23:32:27 +0200560
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100561 /* Sanity check */
Heinrich Schuchardte00b82d2019-04-25 18:41:40 +0200562 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100563 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
564 memory, pages);
565 return EFI_INVALID_PARAMETER;
566 }
567
Michael Walle714497e2020-05-17 12:29:19 +0200568 ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY,
569 false);
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100570 if (ret != EFI_SUCCESS)
571 return EFI_NOT_FOUND;
Stefan Brünsb61d8572016-10-01 23:32:27 +0200572
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100573 return ret;
Alexander Graf5d009952016-03-04 01:10:04 +0100574}
575
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100576/**
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100577 * efi_alloc_aligned_pages() - allocate aligned memory pages
Ilias Apalodimasebdea882021-10-11 15:10:23 +0300578 *
579 * @len: len in bytes
580 * @memory_type: usage type of the allocated memory
581 * @align: alignment in bytes
582 * Return: aligned memory or NULL
583 */
584void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
585{
586 u64 req_pages = efi_size_in_pages(len);
587 u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
588 u64 free_pages;
589 u64 aligned_mem;
590 efi_status_t r;
591 u64 mem;
592
593 /* align must be zero or a power of two */
594 if (align & (align - 1))
595 return NULL;
596
597 /* Check for overflow */
598 if (true_pages < req_pages)
599 return NULL;
600
601 if (align < EFI_PAGE_SIZE) {
602 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
603 req_pages, &mem);
604 return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL;
605 }
606
607 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
608 true_pages, &mem);
609 if (r != EFI_SUCCESS)
610 return NULL;
611
612 aligned_mem = ALIGN(mem, align);
613 /* Free pages before alignment */
614 free_pages = efi_size_in_pages(aligned_mem - mem);
615 if (free_pages)
616 efi_free_pages(mem, free_pages);
617
618 /* Free trailing pages */
619 free_pages = true_pages - (req_pages + free_pages);
620 if (free_pages) {
621 mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
622 efi_free_pages(mem, free_pages);
623 }
624
625 return (void *)(uintptr_t)aligned_mem;
626}
627
628/**
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100629 * efi_allocate_pool - allocate memory from pool
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100630 *
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100631 * @pool_type: type of the pool from which memory is to be allocated
632 * @size: number of bytes to be allocated
633 * @buffer: allocated memory
634 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100635 */
Heinrich Schuchardt49d225e2021-08-17 15:05:31 +0200636efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer)
Stefan Brünsead12742016-10-09 22:17:18 +0200637{
638 efi_status_t r;
Heinrich Schuchardt306b1672019-03-18 20:01:59 +0100639 u64 addr;
Alexander Graf282a06c2018-06-18 17:23:15 +0200640 struct efi_pool_allocation *alloc;
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100641 u64 num_pages = efi_size_in_pages(size +
642 sizeof(struct efi_pool_allocation));
Stefan Brüns42417bc2016-10-09 22:17:26 +0200643
Heinrich Schuchardt4d5e0712018-07-02 12:53:53 +0200644 if (!buffer)
645 return EFI_INVALID_PARAMETER;
646
Stefan Brüns42417bc2016-10-09 22:17:26 +0200647 if (size == 0) {
648 *buffer = NULL;
649 return EFI_SUCCESS;
650 }
Stefan Brünsead12742016-10-09 22:17:18 +0200651
Heinrich Schuchardte09159c2018-05-26 10:32:27 +0200652 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Heinrich Schuchardt306b1672019-03-18 20:01:59 +0100653 &addr);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200654 if (r == EFI_SUCCESS) {
Heinrich Schuchardt306b1672019-03-18 20:01:59 +0100655 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
Stefan Brüns42417bc2016-10-09 22:17:26 +0200656 alloc->num_pages = num_pages;
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100657 alloc->checksum = checksum(alloc);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200658 *buffer = alloc->data;
659 }
660
661 return r;
662}
663
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100664/**
Heinrich Schuchardtf606fab2023-03-19 09:20:22 +0100665 * efi_alloc() - allocate boot services data pool memory
666 *
667 * Allocate memory from pool and zero it out.
668 *
669 * @size: number of bytes to allocate
670 * Return: pointer to allocated memory or NULL
671 */
672void *efi_alloc(size_t size)
673{
674 void *buf;
675
676 if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, size, &buf) !=
677 EFI_SUCCESS) {
678 log_err("out of memory");
679 return NULL;
680 }
681 memset(buf, 0, size);
682
683 return buf;
684}
685
686/**
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100687 * efi_free_pool() - free memory from pool
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100688 *
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100689 * @buffer: start of memory to be freed
690 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100691 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200692efi_status_t efi_free_pool(void *buffer)
693{
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200694 efi_status_t ret;
Stefan Brüns42417bc2016-10-09 22:17:26 +0200695 struct efi_pool_allocation *alloc;
696
Heinrich Schuchardt0e22c7c2019-06-12 07:17:04 +0200697 if (!buffer)
698 return EFI_INVALID_PARAMETER;
699
Heinrich Schuchardtf756fe82019-05-11 08:21:17 +0200700 ret = efi_check_allocated((uintptr_t)buffer, true);
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200701 if (ret != EFI_SUCCESS)
702 return ret;
xypron.glpk@gmx.de71275a32017-07-14 19:12:39 +0200703
Stefan Brüns42417bc2016-10-09 22:17:26 +0200704 alloc = container_of(buffer, struct efi_pool_allocation, data);
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100705
706 /* Check that this memory was allocated by efi_allocate_pool() */
707 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
708 alloc->checksum != checksum(alloc)) {
709 printf("%s: illegal free 0x%p\n", __func__, buffer);
710 return EFI_INVALID_PARAMETER;
711 }
712 /* Avoid double free */
713 alloc->checksum = 0;
Stefan Brüns42417bc2016-10-09 22:17:26 +0200714
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200715 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brünsead12742016-10-09 22:17:18 +0200716
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200717 return ret;
Stefan Brünsead12742016-10-09 22:17:18 +0200718}
719
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100720/**
721 * efi_get_memory_map() - get map describing memory usage.
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100722 *
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100723 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100724 * on exit the size of the copied memory map
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100725 * @memory_map: buffer to which the memory map is written
726 * @map_key: key for the memory map
727 * @descriptor_size: size of an individual memory descriptor
728 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100729 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100730 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100731efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
732 struct efi_mem_desc *memory_map,
733 efi_uintn_t *map_key,
734 efi_uintn_t *descriptor_size,
735 uint32_t *descriptor_version)
Alexander Graf5d009952016-03-04 01:10:04 +0100736{
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100737 efi_uintn_t map_size = 0;
Alexander Grafcee752f2016-04-11 23:51:02 +0200738 int map_entries = 0;
Alexander Graf5d009952016-03-04 01:10:04 +0100739 struct list_head *lhandle;
Heinrich Schuchardtfa995d0d2018-08-06 22:28:18 +0200740 efi_uintn_t provided_map_size;
Alexander Graf5d009952016-03-04 01:10:04 +0100741
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200742 if (!memory_map_size)
743 return EFI_INVALID_PARAMETER;
744
Heinrich Schuchardtfa995d0d2018-08-06 22:28:18 +0200745 provided_map_size = *memory_map_size;
746
Alexander Graf5d009952016-03-04 01:10:04 +0100747 list_for_each(lhandle, &efi_mem)
Alexander Grafcee752f2016-04-11 23:51:02 +0200748 map_entries++;
749
750 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf5d009952016-03-04 01:10:04 +0100751
Rob Clarka1b24822017-07-26 14:34:05 -0400752 *memory_map_size = map_size;
753
Alexander Graf5d009952016-03-04 01:10:04 +0100754 if (descriptor_size)
755 *descriptor_size = sizeof(struct efi_mem_desc);
756
Mian Yousaf Kaukab4c02c112016-09-05 23:59:22 +0200757 if (descriptor_version)
758 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
759
AKASHI Takahirob4842962020-03-11 15:18:18 +0900760 if (provided_map_size < map_size)
761 return EFI_BUFFER_TOO_SMALL;
762
763 if (!memory_map)
764 return EFI_INVALID_PARAMETER;
765
Alexander Graf5d009952016-03-04 01:10:04 +0100766 /* Copy list into array */
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200767 /* Return the list in ascending order */
768 memory_map = &memory_map[map_entries - 1];
769 list_for_each(lhandle, &efi_mem) {
770 struct efi_mem_list *lmem;
Alexander Graf5d009952016-03-04 01:10:04 +0100771
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200772 lmem = list_entry(lhandle, struct efi_mem_list, link);
773 *memory_map = lmem->desc;
774 memory_map--;
Alexander Graf5d009952016-03-04 01:10:04 +0100775 }
776
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200777 if (map_key)
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +0200778 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dec6e3c3e2017-07-21 19:05:44 +0200779
Alexander Graf5d009952016-03-04 01:10:04 +0100780 return EFI_SUCCESS;
781}
782
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000783/**
Heinrich Schuchardteff44402023-01-05 18:26:01 +0100784 * efi_get_memory_map_alloc() - allocate map describing memory usage
785 *
786 * The caller is responsible for calling FreePool() if the call succeeds.
787 *
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100788 * @map_size: size of the memory map
789 * @memory_map: buffer to which the memory map is written
Heinrich Schuchardteff44402023-01-05 18:26:01 +0100790 * Return: status code
791 */
792efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size,
793 struct efi_mem_desc **memory_map)
794{
795 efi_status_t ret;
796
797 *memory_map = NULL;
798 *map_size = 0;
799 ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL);
800 if (ret == EFI_BUFFER_TOO_SMALL) {
801 *map_size += sizeof(struct efi_mem_desc); /* for the map */
802 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size,
803 (void **)memory_map);
804 if (ret != EFI_SUCCESS)
805 return ret;
806 ret = efi_get_memory_map(map_size, *memory_map,
807 NULL, NULL, NULL);
808 if (ret != EFI_SUCCESS) {
809 efi_free_pool(*memory_map);
810 *memory_map = NULL;
811 }
812 }
813
814 return ret;
815}
816
817/**
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000818 * efi_add_conventional_memory_map() - add a RAM memory area to the map
819 *
820 * @ram_start: start address of a RAM memory area
821 * @ram_end: end address of a RAM memory area
822 * @ram_top: max address to be used as conventional memory
823 * Return: status code
824 */
825efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
826 u64 ram_top)
827{
828 u64 pages;
829
830 /* Remove partial pages */
831 ram_end &= ~EFI_PAGE_MASK;
832 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
833
834 if (ram_end <= ram_start) {
835 /* Invalid mapping */
836 return EFI_INVALID_PARAMETER;
837 }
838
839 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
840
Michael Walle714497e2020-05-17 12:29:19 +0200841 efi_add_memory_map_pg(ram_start, pages,
842 EFI_CONVENTIONAL_MEMORY, false);
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000843
844 /*
845 * Boards may indicate to the U-Boot memory core that they
846 * can not support memory above ram_top. Let's honor this
847 * in the efi_loader subsystem too by declaring any memory
848 * above ram_top as "already occupied by firmware".
849 */
850 if (ram_top < ram_start) {
851 /* ram_top is before this region, reserve all */
Michael Walle714497e2020-05-17 12:29:19 +0200852 efi_add_memory_map_pg(ram_start, pages,
853 EFI_BOOT_SERVICES_DATA, true);
Heinrich Schuchardt8da26f52022-04-25 23:54:48 +0200854 } else if (ram_top < ram_end) {
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000855 /* ram_top is inside this region, reserve parts */
856 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
857
Michael Walle714497e2020-05-17 12:29:19 +0200858 efi_add_memory_map_pg(ram_top, pages,
859 EFI_BOOT_SERVICES_DATA, true);
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000860 }
861
862 return EFI_SUCCESS;
863}
864
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100865/**
866 * efi_add_known_memory() - add memory banks to map
867 *
868 * This function may be overridden for specific architectures.
869 */
York Sun42633742017-03-06 09:02:27 -0800870__weak void efi_add_known_memory(void)
Alexander Graf5d009952016-03-04 01:10:04 +0100871{
Alexander Graf7b78d642018-11-30 21:24:56 +0100872 u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
Alexander Graf5d009952016-03-04 01:10:04 +0100873 int i;
874
Heinrich Schuchardt23f5f4a2019-01-05 23:41:36 +0100875 /*
876 * ram_top is just outside mapped memory. So use an offset of one for
877 * mapping the sandbox address.
878 */
879 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
880
Alexander Graf7b78d642018-11-30 21:24:56 +0100881 /* Fix for 32bit targets with ram_top at 4G */
882 if (!ram_top)
883 ram_top = 0x100000000ULL;
884
Alexander Graf5d009952016-03-04 01:10:04 +0100885 /* Add RAM */
886 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000887 u64 ram_end, ram_start;
Alexander Graf5d009952016-03-04 01:10:04 +0100888
Heinrich Schuchardt49759742018-11-18 17:58:46 +0100889 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
Heinrich Schuchardt108bdff2018-11-12 18:55:24 +0100890 ram_end = ram_start + gd->bd->bi_dram[i].size;
891
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000892 efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
Alexander Graf5d009952016-03-04 01:10:04 +0100893 }
York Sun42633742017-03-06 09:02:27 -0800894}
895
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100896/**
897 * add_u_boot_and_runtime() - add U-Boot code to memory map
898 *
899 * Add memory regions for U-Boot's memory and for the runtime services code.
900 */
Simon Glass69259b82018-06-18 17:23:12 +0200901static void add_u_boot_and_runtime(void)
York Sun42633742017-03-06 09:02:27 -0800902{
903 unsigned long runtime_start, runtime_end, runtime_pages;
Alexander Graf7a82c302018-09-17 13:54:33 +0200904 unsigned long runtime_mask = EFI_PAGE_MASK;
York Sun42633742017-03-06 09:02:27 -0800905 unsigned long uboot_start, uboot_pages;
Heinrich Schuchardt74b869b2020-07-29 12:37:35 +0200906 unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
York Sun42633742017-03-06 09:02:27 -0800907
Alexander Graf5d009952016-03-04 01:10:04 +0100908 /* Add U-Boot */
Heinrich Schuchardt7264e212019-11-08 20:42:53 +0100909 uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
910 uboot_stack_size) & ~EFI_PAGE_MASK;
911 uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
912 uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Heinrich Schuchardt1a127962022-11-29 16:00:41 +0100913 efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE,
Michael Walle714497e2020-05-17 12:29:19 +0200914 false);
Alexander Graf5d009952016-03-04 01:10:04 +0100915
Alexander Graf7a82c302018-09-17 13:54:33 +0200916#if defined(__aarch64__)
917 /*
918 * Runtime Services must be 64KiB aligned according to the
919 * "AArch64 Platforms" section in the UEFI spec (2.7+).
920 */
921
922 runtime_mask = SZ_64K - 1;
923#endif
924
925 /*
926 * Add Runtime Services. We mark surrounding boottime code as runtime as
927 * well to fulfill the runtime alignment constraints but avoid padding.
928 */
929 runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
Alexander Graf5d009952016-03-04 01:10:04 +0100930 runtime_end = (ulong)&__efi_runtime_stop;
Alexander Graf7a82c302018-09-17 13:54:33 +0200931 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
Alexander Graf5d009952016-03-04 01:10:04 +0100932 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
Michael Walle714497e2020-05-17 12:29:19 +0200933 efi_add_memory_map_pg(runtime_start, runtime_pages,
934 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass69259b82018-06-18 17:23:12 +0200935}
936
937int efi_memory_init(void)
938{
939 efi_add_known_memory();
940
Heinrich Schuchardt7264e212019-11-08 20:42:53 +0100941 add_u_boot_and_runtime();
Alexander Graf5d009952016-03-04 01:10:04 +0100942
Alexander Graf51735ae2016-05-11 18:25:48 +0200943#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
944 /* Request a 32bit 64MB bounce buffer region */
945 uint64_t efi_bounce_buffer_addr = 0xffffffff;
946
Heinrich Schuchardt1a127962022-11-29 16:00:41 +0100947 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA,
Alexander Graf51735ae2016-05-11 18:25:48 +0200948 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
949 &efi_bounce_buffer_addr) != EFI_SUCCESS)
950 return -1;
951
952 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
953#endif
954
Alexander Graf5d009952016-03-04 01:10:04 +0100955 return 0;
956}