blob: edfad2d95a1d986b7e7215a331f48dfdf61d9528 [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>
Alexander Grafde2a13b2016-03-30 16:38:29 +020018#include <linux/list_sort.h>
Alexander Graf15937a22018-09-17 13:54:33 +020019#include <linux/sizes.h>
Alexander Graf8623f922016-03-04 01:10:04 +010020
21DECLARE_GLOBAL_DATA_PTR;
22
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010023/* Magic number identifying memory allocated from pool */
24#define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
25
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +020026efi_uintn_t efi_memory_map_key;
27
Alexander Graf8623f922016-03-04 01:10:04 +010028struct efi_mem_list {
29 struct list_head link;
30 struct efi_mem_desc desc;
31};
32
Alexander Graf66d86232016-05-27 12:25:03 +020033#define EFI_CARVE_NO_OVERLAP -1
34#define EFI_CARVE_LOOP_AGAIN -2
35#define EFI_CARVE_OVERLAPS_NONRAM -3
Heinrich Schuchardt17fac0e2023-07-30 12:27:03 +020036#define EFI_CARVE_OUT_OF_RESOURCES -4
Alexander Graf66d86232016-05-27 12:25:03 +020037
Alexander Graf8623f922016-03-04 01:10:04 +010038/* This list contains all memory map items */
Bin Mengc8025a32023-04-05 20:15:18 +080039static LIST_HEAD(efi_mem);
Alexander Graf8623f922016-03-04 01:10:04 +010040
Alexander Graf7c00a3c2016-05-11 18:25:48 +020041#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
42void *efi_bounce_buffer;
43#endif
44
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010045/**
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020046 * struct efi_pool_allocation - memory block allocated from pool
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010047 *
48 * @num_pages: number of pages allocated
49 * @checksum: checksum
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020050 * @data: allocated pool memory
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010051 *
Heinrich Schuchardt79f882a2019-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üns67b67d92016-10-09 22:17:26 +020054 * to be able to free the correct amount later.
Heinrich Schuchardt79f882a2019-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üns67b67d92016-10-09 22:17:26 +020059 * EFI requires 8 byte alignment for pool allocations, so we can
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020060 * prepend each allocation with these header fields.
Stefan Brüns67b67d92016-10-09 22:17:26 +020061 */
62struct efi_pool_allocation {
63 u64 num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010064 u64 checksum;
Rob Clarke597fd42017-09-13 18:05:36 -040065 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns67b67d92016-10-09 22:17:26 +020066};
67
Heinrich Schuchardtb4e2ba32019-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 Schuchardtb9b838d2023-01-08 01:00:00 +010084/**
85 * efi_mem_cmp() - comparator function for sorting memory map
86 *
Alexander Grafde2a13b2016-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 Schuchardtb9b838d2023-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 Grafde2a13b2016-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 Schuchardtb9b838d2023-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 Grafd7288142018-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 Schuchardtb9b838d2023-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 Grafde2a13b2016-03-30 16:38:29 +0200127static void efi_mem_sort(void)
128{
Alexander Grafd7288142018-09-16 17:05:29 +0200129 struct list_head *lhandle;
130 struct efi_mem_list *prevmem = NULL;
131 bool merge_again = true;
132
Alexander Grafde2a13b2016-03-30 16:38:29 +0200133 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Grafd7288142018-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 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{
270 struct list_head *lhandle;
271 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;
Alexander Graf8623f922016-03-04 01:10:04 +0100310 list_for_each(lhandle, &efi_mem) {
311 struct efi_mem_list *lmem;
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200312 s64 r;
Alexander Graf8623f922016-03-04 01:10:04 +0100313
314 lmem = list_entry(lhandle, struct efi_mem_list, link);
315 r = efi_mem_carve_out(lmem, &newlist->desc,
316 overlap_only_ram);
Alexander Graf66d86232016-05-27 12:25:03 +0200317 switch (r) {
Heinrich Schuchardt17fac0e2023-07-30 12:27:03 +0200318 case EFI_CARVE_OUT_OF_RESOURCES:
319 free(newlist);
320 return EFI_OUT_OF_RESOURCES;
Alexander Graf66d86232016-05-27 12:25:03 +0200321 case EFI_CARVE_OVERLAPS_NONRAM:
322 /*
323 * The user requested to only have RAM overlaps,
324 * but we hit a non-RAM region. Error out.
325 */
Heinrich Schuchardt9f0d3ad2023-07-30 12:59:32 +0200326 free(newlist);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100327 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200328 case EFI_CARVE_NO_OVERLAP:
329 /* Just ignore this list entry */
330 break;
331 case EFI_CARVE_LOOP_AGAIN:
332 /*
333 * We split an entry, but need to loop through
334 * the list again to actually carve it.
335 */
336 carve_again = true;
337 break;
338 default:
339 /* We carved a number of pages */
340 carved_pages += r;
341 carve_again = true;
Alexander Graf8623f922016-03-04 01:10:04 +0100342 break;
343 }
Alexander Graf66d86232016-05-27 12:25:03 +0200344
345 if (carve_again) {
346 /* The list changed, we need to start over */
347 break;
348 }
Alexander Graf8623f922016-03-04 01:10:04 +0100349 }
Alexander Graf66d86232016-05-27 12:25:03 +0200350 } while (carve_again);
351
352 if (overlap_only_ram && (carved_pages != pages)) {
353 /*
354 * The payload wanted to have RAM overlaps, but we overlapped
355 * with an unallocated region. Error out.
356 */
Heinrich Schuchardt9f0d3ad2023-07-30 12:59:32 +0200357 free(newlist);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100358 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200359 }
Alexander Graf8623f922016-03-04 01:10:04 +0100360
361 /* Add our new map */
362 list_add_tail(&newlist->link, &efi_mem);
363
Alexander Grafde2a13b2016-03-30 16:38:29 +0200364 /* And make sure memory is listed in descending order */
365 efi_mem_sort();
366
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200367 /* Notify that the memory map was changed */
368 list_for_each_entry(evt, &efi_events, link) {
369 if (evt->group &&
370 !guidcmp(evt->group,
371 &efi_guid_event_group_memory_map_change)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200372 efi_signal_event(evt);
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200373 break;
374 }
375 }
376
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100377 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100378}
379
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200380/**
Michael Walle282d3862020-05-17 12:29:19 +0200381 * efi_add_memory_map() - add memory area to the memory map
382 *
383 * @start: start address of the memory area
384 * @size: length in bytes of the memory area
385 * @memory_type: type of memory added
386 *
387 * Return: status code
388 *
389 * This function automatically aligns the start and size of the memory area
390 * to EFI_PAGE_SIZE.
391 */
392efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
393{
394 u64 pages;
395
396 pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
397 start &= ~EFI_PAGE_MASK;
398
399 return efi_add_memory_map_pg(start, pages, memory_type, false);
400}
401
402/**
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200403 * efi_check_allocated() - validate address to be freed
404 *
405 * Check that the address is within allocated memory:
406 *
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200407 * * The address must be in a range of the memory map.
408 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
409 *
410 * Page alignment is not checked as this is not a requirement of
411 * efi_free_pool().
412 *
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200413 * @addr: address of page to be freed
414 * @must_be_allocated: return success if the page is allocated
415 * Return: status code
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200416 */
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200417static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200418{
419 struct efi_mem_list *item;
420
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200421 list_for_each_entry(item, &efi_mem, link) {
422 u64 start = item->desc.physical_start;
423 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
424
425 if (addr >= start && addr < end) {
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200426 if (must_be_allocated ^
427 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200428 return EFI_SUCCESS;
429 else
430 return EFI_NOT_FOUND;
431 }
432 }
433
434 return EFI_NOT_FOUND;
435}
436
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100437/**
438 * efi_find_free_memory() - find free memory pages
439 *
440 * @len: size of memory area needed
441 * @max_addr: highest address to allocate
442 * Return: pointer to free memory area or 0
443 */
Alexander Graf8623f922016-03-04 01:10:04 +0100444static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
445{
446 struct list_head *lhandle;
447
Alexander Grafc3b2a252018-11-05 00:30:46 +0100448 /*
449 * Prealign input max address, so we simplify our matching
450 * logic below and can just reuse it as return pointer.
451 */
452 max_addr &= ~EFI_PAGE_MASK;
453
Alexander Graf8623f922016-03-04 01:10:04 +0100454 list_for_each(lhandle, &efi_mem) {
455 struct efi_mem_list *lmem = list_entry(lhandle,
456 struct efi_mem_list, link);
457 struct efi_mem_desc *desc = &lmem->desc;
458 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
459 uint64_t desc_end = desc->physical_start + desc_len;
460 uint64_t curmax = min(max_addr, desc_end);
461 uint64_t ret = curmax - len;
462
463 /* We only take memory from free RAM */
464 if (desc->type != EFI_CONVENTIONAL_MEMORY)
465 continue;
466
467 /* Out of bounds for max_addr */
468 if ((ret + len) > max_addr)
469 continue;
470
471 /* Out of bounds for upper map limit */
472 if ((ret + len) > desc_end)
473 continue;
474
475 /* Out of bounds for lower map limit */
476 if (ret < desc->physical_start)
477 continue;
478
479 /* Return the highest address in this map within bounds */
480 return ret;
481 }
482
483 return 0;
484}
485
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100486/**
487 * efi_allocate_pages - allocate memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100488 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100489 * @type: type of allocation to be performed
490 * @memory_type: usage type of the allocated memory
491 * @pages: number of pages to be allocated
492 * @memory: allocated memory
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100493 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100494 */
Heinrich Schuchardt98228942021-08-17 15:05:31 +0200495efi_status_t efi_allocate_pages(enum efi_allocate_type type,
496 enum efi_memory_type memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100497 efi_uintn_t pages, uint64_t *memory)
Alexander Graf8623f922016-03-04 01:10:04 +0100498{
Heinrich Schuchardtf0c16f92023-07-30 11:31:08 +0200499 u64 len;
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200500 efi_status_t ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100501 uint64_t addr;
502
Heinrich Schuchardtf7272332019-04-23 00:30:53 +0200503 /* Check import parameters */
504 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
505 memory_type <= 0x6FFFFFFF)
506 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200507 if (!memory)
508 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtf0c16f92023-07-30 11:31:08 +0200509 len = (u64)pages << EFI_PAGE_SHIFT;
510 /* Catch possible overflow on 64bit systems */
511 if (sizeof(efi_uintn_t) == sizeof(u64) &&
512 (len >> EFI_PAGE_SHIFT) != (u64)pages)
513 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200514
Alexander Graf8623f922016-03-04 01:10:04 +0100515 switch (type) {
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100516 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf8623f922016-03-04 01:10:04 +0100517 /* Any page */
Stephen Warren66208b92018-08-30 15:43:45 -0600518 addr = efi_find_free_memory(len, -1ULL);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200519 if (!addr)
520 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100521 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100522 case EFI_ALLOCATE_MAX_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100523 /* Max address */
524 addr = efi_find_free_memory(len, *memory);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200525 if (!addr)
526 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100527 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100528 case EFI_ALLOCATE_ADDRESS:
Heinrich Schuchardt1c180b82022-11-06 01:52:13 +0100529 if (*memory & EFI_PAGE_MASK)
530 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100531 /* Exact address, reserve it. The addr is already in *memory. */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200532 ret = efi_check_allocated(*memory, false);
533 if (ret != EFI_SUCCESS)
534 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100535 addr = *memory;
536 break;
537 default:
538 /* UEFI doesn't specify other allocation types */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200539 return EFI_INVALID_PARAMETER;
Alexander Graf8623f922016-03-04 01:10:04 +0100540 }
541
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200542 /* Reserve that map in our memory maps */
Michael Walle282d3862020-05-17 12:29:19 +0200543 ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
544 if (ret != EFI_SUCCESS)
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200545 /* Map would overlap, bail out */
546 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100547
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200548 *memory = addr;
Alexander Graf8623f922016-03-04 01:10:04 +0100549
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200550 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100551}
552
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100553/**
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100554 * efi_free_pages() - free memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100555 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100556 * @memory: start of the memory area to be freed
557 * @pages: number of pages to be freed
558 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100559 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100560efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf8623f922016-03-04 01:10:04 +0100561{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200562 efi_status_t ret;
563
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200564 ret = efi_check_allocated(memory, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200565 if (ret != EFI_SUCCESS)
566 return ret;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200567
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100568 /* Sanity check */
Heinrich Schuchardt7bf4bc92019-04-25 18:41:40 +0200569 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100570 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
571 memory, pages);
572 return EFI_INVALID_PARAMETER;
573 }
574
Michael Walle282d3862020-05-17 12:29:19 +0200575 ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY,
576 false);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100577 if (ret != EFI_SUCCESS)
578 return EFI_NOT_FOUND;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200579
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100580 return ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100581}
582
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100583/**
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100584 * efi_alloc_aligned_pages() - allocate aligned memory pages
Ilias Apalodimas6e01c232021-10-11 15:10:23 +0300585 *
586 * @len: len in bytes
587 * @memory_type: usage type of the allocated memory
588 * @align: alignment in bytes
589 * Return: aligned memory or NULL
590 */
591void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
592{
593 u64 req_pages = efi_size_in_pages(len);
594 u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
595 u64 free_pages;
596 u64 aligned_mem;
597 efi_status_t r;
598 u64 mem;
599
600 /* align must be zero or a power of two */
601 if (align & (align - 1))
602 return NULL;
603
604 /* Check for overflow */
605 if (true_pages < req_pages)
606 return NULL;
607
608 if (align < EFI_PAGE_SIZE) {
609 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
610 req_pages, &mem);
611 return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL;
612 }
613
614 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
615 true_pages, &mem);
616 if (r != EFI_SUCCESS)
617 return NULL;
618
619 aligned_mem = ALIGN(mem, align);
620 /* Free pages before alignment */
621 free_pages = efi_size_in_pages(aligned_mem - mem);
622 if (free_pages)
623 efi_free_pages(mem, free_pages);
624
625 /* Free trailing pages */
626 free_pages = true_pages - (req_pages + free_pages);
627 if (free_pages) {
628 mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
629 efi_free_pages(mem, free_pages);
630 }
631
632 return (void *)(uintptr_t)aligned_mem;
633}
634
635/**
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100636 * efi_allocate_pool - allocate memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100637 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100638 * @pool_type: type of the pool from which memory is to be allocated
639 * @size: number of bytes to be allocated
640 * @buffer: allocated memory
641 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100642 */
Heinrich Schuchardt98228942021-08-17 15:05:31 +0200643efi_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 +0200644{
645 efi_status_t r;
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100646 u64 addr;
Alexander Grafcd405172018-06-18 17:23:15 +0200647 struct efi_pool_allocation *alloc;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100648 u64 num_pages = efi_size_in_pages(size +
649 sizeof(struct efi_pool_allocation));
Stefan Brüns67b67d92016-10-09 22:17:26 +0200650
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200651 if (!buffer)
652 return EFI_INVALID_PARAMETER;
653
Stefan Brüns67b67d92016-10-09 22:17:26 +0200654 if (size == 0) {
655 *buffer = NULL;
656 return EFI_SUCCESS;
657 }
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200658
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200659 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100660 &addr);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200661 if (r == EFI_SUCCESS) {
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100662 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200663 alloc->num_pages = num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100664 alloc->checksum = checksum(alloc);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200665 *buffer = alloc->data;
666 }
667
668 return r;
669}
670
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100671/**
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100672 * efi_alloc() - allocate boot services data pool memory
673 *
674 * Allocate memory from pool and zero it out.
675 *
676 * @size: number of bytes to allocate
677 * Return: pointer to allocated memory or NULL
678 */
679void *efi_alloc(size_t size)
680{
681 void *buf;
682
683 if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, size, &buf) !=
684 EFI_SUCCESS) {
685 log_err("out of memory");
686 return NULL;
687 }
688 memset(buf, 0, size);
689
690 return buf;
691}
692
693/**
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100694 * efi_free_pool() - free memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100695 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100696 * @buffer: start of memory to be freed
697 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100698 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200699efi_status_t efi_free_pool(void *buffer)
700{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200701 efi_status_t ret;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200702 struct efi_pool_allocation *alloc;
703
Heinrich Schuchardt56b587f72019-06-12 07:17:04 +0200704 if (!buffer)
705 return EFI_INVALID_PARAMETER;
706
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200707 ret = efi_check_allocated((uintptr_t)buffer, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200708 if (ret != EFI_SUCCESS)
709 return ret;
xypron.glpk@gmx.de34ddae72017-07-14 19:12:39 +0200710
Stefan Brüns67b67d92016-10-09 22:17:26 +0200711 alloc = container_of(buffer, struct efi_pool_allocation, data);
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100712
713 /* Check that this memory was allocated by efi_allocate_pool() */
714 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
715 alloc->checksum != checksum(alloc)) {
716 printf("%s: illegal free 0x%p\n", __func__, buffer);
717 return EFI_INVALID_PARAMETER;
718 }
719 /* Avoid double free */
720 alloc->checksum = 0;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200721
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200722 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200723
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200724 return ret;
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200725}
726
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100727/**
728 * efi_get_memory_map() - get map describing memory usage.
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100729 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100730 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100731 * on exit the size of the copied memory map
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100732 * @memory_map: buffer to which the memory map is written
733 * @map_key: key for the memory map
734 * @descriptor_size: size of an individual memory descriptor
735 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100736 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100737 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100738efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
739 struct efi_mem_desc *memory_map,
740 efi_uintn_t *map_key,
741 efi_uintn_t *descriptor_size,
742 uint32_t *descriptor_version)
Alexander Graf8623f922016-03-04 01:10:04 +0100743{
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100744 efi_uintn_t map_size = 0;
Alexander Graf9b591022016-04-11 23:51:02 +0200745 int map_entries = 0;
Alexander Graf8623f922016-03-04 01:10:04 +0100746 struct list_head *lhandle;
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200747 efi_uintn_t provided_map_size;
Alexander Graf8623f922016-03-04 01:10:04 +0100748
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200749 if (!memory_map_size)
750 return EFI_INVALID_PARAMETER;
751
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200752 provided_map_size = *memory_map_size;
753
Alexander Graf8623f922016-03-04 01:10:04 +0100754 list_for_each(lhandle, &efi_mem)
Alexander Graf9b591022016-04-11 23:51:02 +0200755 map_entries++;
756
757 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf8623f922016-03-04 01:10:04 +0100758
Rob Clarka84c9742017-07-26 14:34:05 -0400759 *memory_map_size = map_size;
760
Alexander Graf8623f922016-03-04 01:10:04 +0100761 if (descriptor_size)
762 *descriptor_size = sizeof(struct efi_mem_desc);
763
Mian Yousaf Kaukab338cf562016-09-05 23:59:22 +0200764 if (descriptor_version)
765 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
766
AKASHI Takahiro3ee138d2020-03-11 15:18:18 +0900767 if (provided_map_size < map_size)
768 return EFI_BUFFER_TOO_SMALL;
769
770 if (!memory_map)
771 return EFI_INVALID_PARAMETER;
772
Alexander Graf8623f922016-03-04 01:10:04 +0100773 /* Copy list into array */
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200774 /* Return the list in ascending order */
775 memory_map = &memory_map[map_entries - 1];
776 list_for_each(lhandle, &efi_mem) {
777 struct efi_mem_list *lmem;
Alexander Graf8623f922016-03-04 01:10:04 +0100778
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200779 lmem = list_entry(lhandle, struct efi_mem_list, link);
780 *memory_map = lmem->desc;
781 memory_map--;
Alexander Graf8623f922016-03-04 01:10:04 +0100782 }
783
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200784 if (map_key)
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200785 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dee436e632017-07-21 19:05:44 +0200786
Alexander Graf8623f922016-03-04 01:10:04 +0100787 return EFI_SUCCESS;
788}
789
Park, Aidene009e7a2019-09-03 17:43:43 +0000790/**
Heinrich Schuchardt13ce2d92023-01-05 18:26:01 +0100791 * efi_get_memory_map_alloc() - allocate map describing memory usage
792 *
793 * The caller is responsible for calling FreePool() if the call succeeds.
794 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100795 * @map_size: size of the memory map
796 * @memory_map: buffer to which the memory map is written
Heinrich Schuchardt13ce2d92023-01-05 18:26:01 +0100797 * Return: status code
798 */
799efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size,
800 struct efi_mem_desc **memory_map)
801{
802 efi_status_t ret;
803
804 *memory_map = NULL;
805 *map_size = 0;
806 ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL);
807 if (ret == EFI_BUFFER_TOO_SMALL) {
808 *map_size += sizeof(struct efi_mem_desc); /* for the map */
809 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size,
810 (void **)memory_map);
811 if (ret != EFI_SUCCESS)
812 return ret;
813 ret = efi_get_memory_map(map_size, *memory_map,
814 NULL, NULL, NULL);
815 if (ret != EFI_SUCCESS) {
816 efi_free_pool(*memory_map);
817 *memory_map = NULL;
818 }
819 }
820
821 return ret;
822}
823
824/**
Park, Aidene009e7a2019-09-03 17:43:43 +0000825 * efi_add_conventional_memory_map() - add a RAM memory area to the map
826 *
827 * @ram_start: start address of a RAM memory area
828 * @ram_end: end address of a RAM memory area
829 * @ram_top: max address to be used as conventional memory
830 * Return: status code
831 */
832efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
833 u64 ram_top)
834{
835 u64 pages;
836
837 /* Remove partial pages */
838 ram_end &= ~EFI_PAGE_MASK;
839 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
840
841 if (ram_end <= ram_start) {
842 /* Invalid mapping */
843 return EFI_INVALID_PARAMETER;
844 }
845
846 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
847
Michael Walle282d3862020-05-17 12:29:19 +0200848 efi_add_memory_map_pg(ram_start, pages,
849 EFI_CONVENTIONAL_MEMORY, false);
Park, Aidene009e7a2019-09-03 17:43:43 +0000850
851 /*
852 * Boards may indicate to the U-Boot memory core that they
853 * can not support memory above ram_top. Let's honor this
854 * in the efi_loader subsystem too by declaring any memory
855 * above ram_top as "already occupied by firmware".
856 */
857 if (ram_top < ram_start) {
858 /* ram_top is before this region, reserve all */
Michael Walle282d3862020-05-17 12:29:19 +0200859 efi_add_memory_map_pg(ram_start, pages,
860 EFI_BOOT_SERVICES_DATA, true);
Heinrich Schuchardt3546eb22022-04-25 23:54:48 +0200861 } else if (ram_top < ram_end) {
Park, Aidene009e7a2019-09-03 17:43:43 +0000862 /* ram_top is inside this region, reserve parts */
863 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
864
Michael Walle282d3862020-05-17 12:29:19 +0200865 efi_add_memory_map_pg(ram_top, pages,
866 EFI_BOOT_SERVICES_DATA, true);
Park, Aidene009e7a2019-09-03 17:43:43 +0000867 }
868
869 return EFI_SUCCESS;
870}
871
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100872/**
873 * efi_add_known_memory() - add memory banks to map
874 *
875 * This function may be overridden for specific architectures.
876 */
York Suna8e9f0a2017-03-06 09:02:27 -0800877__weak void efi_add_known_memory(void)
Alexander Graf8623f922016-03-04 01:10:04 +0100878{
Heinrich Schuchardt0f0a1b32023-08-14 07:50:53 +0200879 u64 ram_top = gd->ram_top & ~EFI_PAGE_MASK;
Alexander Graf8623f922016-03-04 01:10:04 +0100880 int i;
881
Heinrich Schuchardteb8edcf2019-01-05 23:41:36 +0100882 /*
883 * ram_top is just outside mapped memory. So use an offset of one for
884 * mapping the sandbox address.
885 */
886 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
887
Alexander Grafc214edf2018-11-30 21:24:56 +0100888 /* Fix for 32bit targets with ram_top at 4G */
889 if (!ram_top)
890 ram_top = 0x100000000ULL;
891
Alexander Graf8623f922016-03-04 01:10:04 +0100892 /* Add RAM */
893 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Park, Aidene009e7a2019-09-03 17:43:43 +0000894 u64 ram_end, ram_start;
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100895
Heinrich Schuchardt6738fcd2018-11-18 17:58:46 +0100896 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100897 ram_end = ram_start + gd->bd->bi_dram[i].size;
898
Park, Aidene009e7a2019-09-03 17:43:43 +0000899 efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
Alexander Graf8623f922016-03-04 01:10:04 +0100900 }
York Suna8e9f0a2017-03-06 09:02:27 -0800901}
902
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100903/**
904 * add_u_boot_and_runtime() - add U-Boot code to memory map
905 *
906 * Add memory regions for U-Boot's memory and for the runtime services code.
907 */
Simon Glass78639432018-06-18 17:23:12 +0200908static void add_u_boot_and_runtime(void)
York Suna8e9f0a2017-03-06 09:02:27 -0800909{
910 unsigned long runtime_start, runtime_end, runtime_pages;
Alexander Graf15937a22018-09-17 13:54:33 +0200911 unsigned long runtime_mask = EFI_PAGE_MASK;
York Suna8e9f0a2017-03-06 09:02:27 -0800912 unsigned long uboot_start, uboot_pages;
Heinrich Schuchardtc6dcbe12020-07-29 12:37:35 +0200913 unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
York Suna8e9f0a2017-03-06 09:02:27 -0800914
Alexander Graf8623f922016-03-04 01:10:04 +0100915 /* Add U-Boot */
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100916 uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
917 uboot_stack_size) & ~EFI_PAGE_MASK;
918 uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
919 uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Heinrich Schuchardt296b55c2022-11-29 16:00:41 +0100920 efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE,
Michael Walle282d3862020-05-17 12:29:19 +0200921 false);
Alexander Graf8623f922016-03-04 01:10:04 +0100922
Alexander Graf15937a22018-09-17 13:54:33 +0200923#if defined(__aarch64__)
924 /*
925 * Runtime Services must be 64KiB aligned according to the
926 * "AArch64 Platforms" section in the UEFI spec (2.7+).
927 */
928
929 runtime_mask = SZ_64K - 1;
930#endif
931
932 /*
933 * Add Runtime Services. We mark surrounding boottime code as runtime as
934 * well to fulfill the runtime alignment constraints but avoid padding.
935 */
936 runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100937 runtime_end = (ulong)&__efi_runtime_stop;
Alexander Graf15937a22018-09-17 13:54:33 +0200938 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100939 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
Michael Walle282d3862020-05-17 12:29:19 +0200940 efi_add_memory_map_pg(runtime_start, runtime_pages,
941 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass78639432018-06-18 17:23:12 +0200942}
943
944int efi_memory_init(void)
945{
946 efi_add_known_memory();
947
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100948 add_u_boot_and_runtime();
Alexander Graf8623f922016-03-04 01:10:04 +0100949
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200950#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
951 /* Request a 32bit 64MB bounce buffer region */
952 uint64_t efi_bounce_buffer_addr = 0xffffffff;
953
Heinrich Schuchardt296b55c2022-11-29 16:00:41 +0100954 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200955 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
956 &efi_bounce_buffer_addr) != EFI_SUCCESS)
957 return -1;
958
959 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
960#endif
961
Alexander Graf8623f922016-03-04 01:10:04 +0100962 return 0;
963}