blob: b7bee98f79c31b6578610283a41af390db17035a [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
Alexander Graf8623f922016-03-04 01:10:04 +01008#include <common.h>
9#include <efi_loader.h>
Simon Glass6980b6b2019-11-14 12:57:45 -070010#include <init.h>
Alexander Graf8623f922016-03-04 01:10:04 +010011#include <malloc.h>
Alexander Grafcd405172018-06-18 17:23:15 +020012#include <mapmem.h>
Simon Glass3e852172018-03-08 21:53:27 +010013#include <watchdog.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Alexander Grafde2a13b2016-03-30 16:38:29 +020016#include <linux/list_sort.h>
Alexander Graf15937a22018-09-17 13:54:33 +020017#include <linux/sizes.h>
Alexander Graf8623f922016-03-04 01:10:04 +010018
19DECLARE_GLOBAL_DATA_PTR;
20
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010021/* Magic number identifying memory allocated from pool */
22#define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
23
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +020024efi_uintn_t efi_memory_map_key;
25
Alexander Graf8623f922016-03-04 01:10:04 +010026struct efi_mem_list {
27 struct list_head link;
28 struct efi_mem_desc desc;
29};
30
Alexander Graf66d86232016-05-27 12:25:03 +020031#define EFI_CARVE_NO_OVERLAP -1
32#define EFI_CARVE_LOOP_AGAIN -2
33#define EFI_CARVE_OVERLAPS_NONRAM -3
34
Alexander Graf8623f922016-03-04 01:10:04 +010035/* This list contains all memory map items */
36LIST_HEAD(efi_mem);
37
Alexander Graf7c00a3c2016-05-11 18:25:48 +020038#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
39void *efi_bounce_buffer;
40#endif
41
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010042/**
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020043 * struct efi_pool_allocation - memory block allocated from pool
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010044 *
45 * @num_pages: number of pages allocated
46 * @checksum: checksum
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020047 * @data: allocated pool memory
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010048 *
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020049 * U-Boot services each UEFI AllocatePool() request as a separate
50 * (multiple) page allocation. We have to track the number of pages
Stefan Brüns67b67d92016-10-09 22:17:26 +020051 * to be able to free the correct amount later.
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020052 *
53 * The checksum calculated in function checksum() is used in FreePool() to avoid
54 * freeing memory not allocated by AllocatePool() and duplicate freeing.
55 *
Stefan Brüns67b67d92016-10-09 22:17:26 +020056 * EFI requires 8 byte alignment for pool allocations, so we can
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020057 * prepend each allocation with these header fields.
Stefan Brüns67b67d92016-10-09 22:17:26 +020058 */
59struct efi_pool_allocation {
60 u64 num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010061 u64 checksum;
Rob Clarke597fd42017-09-13 18:05:36 -040062 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns67b67d92016-10-09 22:17:26 +020063};
64
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010065/**
66 * checksum() - calculate checksum for memory allocated from pool
67 *
68 * @alloc: allocation header
69 * Return: checksum, always non-zero
70 */
71static u64 checksum(struct efi_pool_allocation *alloc)
72{
73 u64 addr = (uintptr_t)alloc;
74 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
75 EFI_ALLOC_POOL_MAGIC;
76 if (!ret)
77 ++ret;
78 return ret;
79}
80
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +010081/**
82 * efi_mem_cmp() - comparator function for sorting memory map
83 *
Alexander Grafde2a13b2016-03-30 16:38:29 +020084 * Sorts the memory list from highest address to lowest address
85 *
86 * When allocating memory we should always start from the highest
87 * address chunk, so sort the memory list such that the first list
88 * iterator gets the highest address and goes lower from there.
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +010089 *
90 * @priv: unused
91 * @a: first memory area
92 * @b: second memory area
93 * Return: 1 if @a is before @b, -1 if @b is before @a, 0 if equal
Alexander Grafde2a13b2016-03-30 16:38:29 +020094 */
95static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
96{
97 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
98 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
99
100 if (mema->desc.physical_start == memb->desc.physical_start)
101 return 0;
102 else if (mema->desc.physical_start < memb->desc.physical_start)
103 return 1;
104 else
105 return -1;
106}
107
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100108/**
109 * desc_get_end() - get end address of memory area
110 *
111 * @desc: memory descriptor
112 * Return: end address + 1
113 */
Alexander Grafd7288142018-09-16 17:05:29 +0200114static uint64_t desc_get_end(struct efi_mem_desc *desc)
115{
116 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
117}
118
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100119/**
120 * efi_mem_sort() - sort memory map
121 *
122 * Sort the memory map and then try to merge adjacent memory areas.
123 */
Alexander Grafde2a13b2016-03-30 16:38:29 +0200124static void efi_mem_sort(void)
125{
Alexander Grafd7288142018-09-16 17:05:29 +0200126 struct list_head *lhandle;
127 struct efi_mem_list *prevmem = NULL;
128 bool merge_again = true;
129
Alexander Grafde2a13b2016-03-30 16:38:29 +0200130 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Grafd7288142018-09-16 17:05:29 +0200131
132 /* Now merge entries that can be merged */
133 while (merge_again) {
134 merge_again = false;
135 list_for_each(lhandle, &efi_mem) {
136 struct efi_mem_list *lmem;
137 struct efi_mem_desc *prev = &prevmem->desc;
138 struct efi_mem_desc *cur;
139 uint64_t pages;
140
141 lmem = list_entry(lhandle, struct efi_mem_list, link);
142 if (!prevmem) {
143 prevmem = lmem;
144 continue;
145 }
146
147 cur = &lmem->desc;
148
149 if ((desc_get_end(cur) == prev->physical_start) &&
150 (prev->type == cur->type) &&
151 (prev->attribute == cur->attribute)) {
152 /* There is an existing map before, reuse it */
153 pages = cur->num_pages;
154 prev->num_pages += pages;
155 prev->physical_start -= pages << EFI_PAGE_SHIFT;
156 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
157 list_del(&lmem->link);
158 free(lmem);
159
160 merge_again = true;
161 break;
162 }
163
164 prevmem = lmem;
165 }
166 }
Alexander Grafde2a13b2016-03-30 16:38:29 +0200167}
168
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100169/**
170 * efi_mem_carve_out() - unmap memory region
Alexander Graf8623f922016-03-04 01:10:04 +0100171 *
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200172 * @map: memory map
173 * @carve_desc: memory region to unmap
174 * @overlap_only_ram: the carved out region may only overlap RAM
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100175 * Return: the number of overlapping pages which have been
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200176 * removed from the map,
177 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
178 * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
179 * and the map contains anything but free ram
180 * (only when overlap_only_ram is true),
181 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
182 * traversed again, as it has been altered.
183 *
184 * Unmaps all memory occupied by the carve_desc region from the list entry
185 * pointed to by map.
Stefan Brüns1b00dc12016-10-01 23:32:23 +0200186 *
187 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200188 * to re-add the already carved out pages to the mapping.
Alexander Graf8623f922016-03-04 01:10:04 +0100189 */
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200190static s64 efi_mem_carve_out(struct efi_mem_list *map,
Alexander Graf8623f922016-03-04 01:10:04 +0100191 struct efi_mem_desc *carve_desc,
192 bool overlap_only_ram)
193{
194 struct efi_mem_list *newmap;
195 struct efi_mem_desc *map_desc = &map->desc;
196 uint64_t map_start = map_desc->physical_start;
197 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
198 uint64_t carve_start = carve_desc->physical_start;
199 uint64_t carve_end = carve_start +
200 (carve_desc->num_pages << EFI_PAGE_SHIFT);
201
202 /* check whether we're overlapping */
203 if ((carve_end <= map_start) || (carve_start >= map_end))
Alexander Graf66d86232016-05-27 12:25:03 +0200204 return EFI_CARVE_NO_OVERLAP;
Alexander Graf8623f922016-03-04 01:10:04 +0100205
206 /* We're overlapping with non-RAM, warn the caller if desired */
207 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
Alexander Graf66d86232016-05-27 12:25:03 +0200208 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf8623f922016-03-04 01:10:04 +0100209
210 /* Sanitize carve_start and carve_end to lie within our bounds */
211 carve_start = max(carve_start, map_start);
212 carve_end = min(carve_end, map_end);
213
214 /* Carving at the beginning of our map? Just move it! */
215 if (carve_start == map_start) {
216 if (map_end == carve_end) {
217 /* Full overlap, just remove map */
218 list_del(&map->link);
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200219 free(map);
220 } else {
221 map->desc.physical_start = carve_end;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200222 map->desc.virtual_start = carve_end;
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200223 map->desc.num_pages = (map_end - carve_end)
224 >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100225 }
226
Alexander Graf66d86232016-05-27 12:25:03 +0200227 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100228 }
229
230 /*
231 * Overlapping maps, just split the list map at carve_start,
232 * it will get moved or removed in the next iteration.
233 *
234 * [ map_desc |__carve_start__| newmap ]
235 */
236
237 /* Create a new map from [ carve_start ... map_end ] */
238 newmap = calloc(1, sizeof(*newmap));
239 newmap->desc = map->desc;
240 newmap->desc.physical_start = carve_start;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200241 newmap->desc.virtual_start = carve_start;
Alexander Graf8623f922016-03-04 01:10:04 +0100242 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
Stefan Brüns9d656512016-10-01 23:32:28 +0200243 /* Insert before current entry (descending address order) */
244 list_add_tail(&newmap->link, &map->link);
Alexander Graf8623f922016-03-04 01:10:04 +0100245
246 /* Shrink the map to [ map_start ... carve_start ] */
247 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
248
Alexander Graf66d86232016-05-27 12:25:03 +0200249 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf8623f922016-03-04 01:10:04 +0100250}
251
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100252/**
Michael Walle282d3862020-05-17 12:29:19 +0200253 * efi_add_memory_map_pg() - add pages to the memory map
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100254 *
255 * @start: start address, must be a multiple of EFI_PAGE_SIZE
256 * @pages: number of pages to add
257 * @memory_type: type of memory added
Maxim Uvarovec631ea2020-08-28 22:47:41 +0300258 * @overlap_only_ram: region may only overlap RAM
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100259 * Return: status code
260 */
Michael Walle282d3862020-05-17 12:29:19 +0200261static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
262 int memory_type,
263 bool overlap_only_ram)
Alexander Graf8623f922016-03-04 01:10:04 +0100264{
265 struct list_head *lhandle;
266 struct efi_mem_list *newlist;
Alexander Graf66d86232016-05-27 12:25:03 +0200267 bool carve_again;
268 uint64_t carved_pages = 0;
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200269 struct efi_event *evt;
Alexander Graf8623f922016-03-04 01:10:04 +0100270
Heinrich Schuchardt706a2fe2019-04-04 21:50:02 +0200271 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
272 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
Andreas Färber102f8bd2016-07-17 06:57:11 +0200273
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200274 if (memory_type >= EFI_MAX_MEMORY_TYPE)
275 return EFI_INVALID_PARAMETER;
276
Alexander Graf8623f922016-03-04 01:10:04 +0100277 if (!pages)
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100278 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100279
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200280 ++efi_memory_map_key;
Alexander Graf8623f922016-03-04 01:10:04 +0100281 newlist = calloc(1, sizeof(*newlist));
282 newlist->desc.type = memory_type;
283 newlist->desc.physical_start = start;
284 newlist->desc.virtual_start = start;
285 newlist->desc.num_pages = pages;
286
287 switch (memory_type) {
288 case EFI_RUNTIME_SERVICES_CODE:
289 case EFI_RUNTIME_SERVICES_DATA:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200290 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100291 break;
292 case EFI_MMAP_IO:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200293 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100294 break;
295 default:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200296 newlist->desc.attribute = EFI_MEMORY_WB;
Alexander Graf8623f922016-03-04 01:10:04 +0100297 break;
298 }
299
300 /* Add our new map */
301 do {
Alexander Graf66d86232016-05-27 12:25:03 +0200302 carve_again = false;
Alexander Graf8623f922016-03-04 01:10:04 +0100303 list_for_each(lhandle, &efi_mem) {
304 struct efi_mem_list *lmem;
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200305 s64 r;
Alexander Graf8623f922016-03-04 01:10:04 +0100306
307 lmem = list_entry(lhandle, struct efi_mem_list, link);
308 r = efi_mem_carve_out(lmem, &newlist->desc,
309 overlap_only_ram);
Alexander Graf66d86232016-05-27 12:25:03 +0200310 switch (r) {
311 case EFI_CARVE_OVERLAPS_NONRAM:
312 /*
313 * The user requested to only have RAM overlaps,
314 * but we hit a non-RAM region. Error out.
315 */
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100316 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200317 case EFI_CARVE_NO_OVERLAP:
318 /* Just ignore this list entry */
319 break;
320 case EFI_CARVE_LOOP_AGAIN:
321 /*
322 * We split an entry, but need to loop through
323 * the list again to actually carve it.
324 */
325 carve_again = true;
326 break;
327 default:
328 /* We carved a number of pages */
329 carved_pages += r;
330 carve_again = true;
Alexander Graf8623f922016-03-04 01:10:04 +0100331 break;
332 }
Alexander Graf66d86232016-05-27 12:25:03 +0200333
334 if (carve_again) {
335 /* The list changed, we need to start over */
336 break;
337 }
Alexander Graf8623f922016-03-04 01:10:04 +0100338 }
Alexander Graf66d86232016-05-27 12:25:03 +0200339 } while (carve_again);
340
341 if (overlap_only_ram && (carved_pages != pages)) {
342 /*
343 * The payload wanted to have RAM overlaps, but we overlapped
344 * with an unallocated region. Error out.
345 */
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100346 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200347 }
Alexander Graf8623f922016-03-04 01:10:04 +0100348
349 /* Add our new map */
350 list_add_tail(&newlist->link, &efi_mem);
351
Alexander Grafde2a13b2016-03-30 16:38:29 +0200352 /* And make sure memory is listed in descending order */
353 efi_mem_sort();
354
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200355 /* Notify that the memory map was changed */
356 list_for_each_entry(evt, &efi_events, link) {
357 if (evt->group &&
358 !guidcmp(evt->group,
359 &efi_guid_event_group_memory_map_change)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200360 efi_signal_event(evt);
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200361 break;
362 }
363 }
364
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100365 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100366}
367
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200368/**
Michael Walle282d3862020-05-17 12:29:19 +0200369 * efi_add_memory_map() - add memory area to the memory map
370 *
371 * @start: start address of the memory area
372 * @size: length in bytes of the memory area
373 * @memory_type: type of memory added
374 *
375 * Return: status code
376 *
377 * This function automatically aligns the start and size of the memory area
378 * to EFI_PAGE_SIZE.
379 */
380efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
381{
382 u64 pages;
383
384 pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
385 start &= ~EFI_PAGE_MASK;
386
387 return efi_add_memory_map_pg(start, pages, memory_type, false);
388}
389
390/**
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200391 * efi_check_allocated() - validate address to be freed
392 *
393 * Check that the address is within allocated memory:
394 *
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200395 * * The address must be in a range of the memory map.
396 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
397 *
398 * Page alignment is not checked as this is not a requirement of
399 * efi_free_pool().
400 *
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200401 * @addr: address of page to be freed
402 * @must_be_allocated: return success if the page is allocated
403 * Return: status code
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200404 */
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200405static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200406{
407 struct efi_mem_list *item;
408
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200409 list_for_each_entry(item, &efi_mem, link) {
410 u64 start = item->desc.physical_start;
411 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
412
413 if (addr >= start && addr < end) {
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200414 if (must_be_allocated ^
415 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200416 return EFI_SUCCESS;
417 else
418 return EFI_NOT_FOUND;
419 }
420 }
421
422 return EFI_NOT_FOUND;
423}
424
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100425/**
426 * efi_find_free_memory() - find free memory pages
427 *
428 * @len: size of memory area needed
429 * @max_addr: highest address to allocate
430 * Return: pointer to free memory area or 0
431 */
Alexander Graf8623f922016-03-04 01:10:04 +0100432static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
433{
434 struct list_head *lhandle;
435
Alexander Grafc3b2a252018-11-05 00:30:46 +0100436 /*
437 * Prealign input max address, so we simplify our matching
438 * logic below and can just reuse it as return pointer.
439 */
440 max_addr &= ~EFI_PAGE_MASK;
441
Alexander Graf8623f922016-03-04 01:10:04 +0100442 list_for_each(lhandle, &efi_mem) {
443 struct efi_mem_list *lmem = list_entry(lhandle,
444 struct efi_mem_list, link);
445 struct efi_mem_desc *desc = &lmem->desc;
446 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
447 uint64_t desc_end = desc->physical_start + desc_len;
448 uint64_t curmax = min(max_addr, desc_end);
449 uint64_t ret = curmax - len;
450
451 /* We only take memory from free RAM */
452 if (desc->type != EFI_CONVENTIONAL_MEMORY)
453 continue;
454
455 /* Out of bounds for max_addr */
456 if ((ret + len) > max_addr)
457 continue;
458
459 /* Out of bounds for upper map limit */
460 if ((ret + len) > desc_end)
461 continue;
462
463 /* Out of bounds for lower map limit */
464 if (ret < desc->physical_start)
465 continue;
466
467 /* Return the highest address in this map within bounds */
468 return ret;
469 }
470
471 return 0;
472}
473
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100474/**
475 * efi_allocate_pages - allocate memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100476 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100477 * @type: type of allocation to be performed
478 * @memory_type: usage type of the allocated memory
479 * @pages: number of pages to be allocated
480 * @memory: allocated memory
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100481 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100482 */
Heinrich Schuchardt98228942021-08-17 15:05:31 +0200483efi_status_t efi_allocate_pages(enum efi_allocate_type type,
484 enum efi_memory_type memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100485 efi_uintn_t pages, uint64_t *memory)
Alexander Graf8623f922016-03-04 01:10:04 +0100486{
487 u64 len = pages << EFI_PAGE_SHIFT;
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200488 efi_status_t ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100489 uint64_t addr;
490
Heinrich Schuchardtf7272332019-04-23 00:30:53 +0200491 /* Check import parameters */
492 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
493 memory_type <= 0x6FFFFFFF)
494 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200495 if (!memory)
496 return EFI_INVALID_PARAMETER;
497
Alexander Graf8623f922016-03-04 01:10:04 +0100498 switch (type) {
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100499 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf8623f922016-03-04 01:10:04 +0100500 /* Any page */
Stephen Warren66208b92018-08-30 15:43:45 -0600501 addr = efi_find_free_memory(len, -1ULL);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200502 if (!addr)
503 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100504 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100505 case EFI_ALLOCATE_MAX_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100506 /* Max address */
507 addr = efi_find_free_memory(len, *memory);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200508 if (!addr)
509 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100510 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100511 case EFI_ALLOCATE_ADDRESS:
Heinrich Schuchardt1c180b82022-11-06 01:52:13 +0100512 if (*memory & EFI_PAGE_MASK)
513 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100514 /* Exact address, reserve it. The addr is already in *memory. */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200515 ret = efi_check_allocated(*memory, false);
516 if (ret != EFI_SUCCESS)
517 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100518 addr = *memory;
519 break;
520 default:
521 /* UEFI doesn't specify other allocation types */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200522 return EFI_INVALID_PARAMETER;
Alexander Graf8623f922016-03-04 01:10:04 +0100523 }
524
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200525 /* Reserve that map in our memory maps */
Michael Walle282d3862020-05-17 12:29:19 +0200526 ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
527 if (ret != EFI_SUCCESS)
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200528 /* Map would overlap, bail out */
529 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100530
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200531 *memory = addr;
Alexander Graf8623f922016-03-04 01:10:04 +0100532
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200533 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100534}
535
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100536/**
537 * efi_alloc() - allocate memory pages
538 *
539 * @len: size of the memory to be allocated
540 * @memory_type: usage type of the allocated memory
541 * Return: pointer to the allocated memory area or NULL
542 */
Alexander Graf8623f922016-03-04 01:10:04 +0100543void *efi_alloc(uint64_t len, int memory_type)
544{
545 uint64_t ret = 0;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100546 uint64_t pages = efi_size_in_pages(len);
Alexander Graf8623f922016-03-04 01:10:04 +0100547 efi_status_t r;
548
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200549 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
550 &ret);
Alexander Graf8623f922016-03-04 01:10:04 +0100551 if (r == EFI_SUCCESS)
552 return (void*)(uintptr_t)ret;
553
554 return NULL;
555}
556
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100557/**
558 * efi_free_pages() - free memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100559 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100560 * @memory: start of the memory area to be freed
561 * @pages: number of pages to be freed
562 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100563 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100564efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf8623f922016-03-04 01:10:04 +0100565{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200566 efi_status_t ret;
567
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200568 ret = efi_check_allocated(memory, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200569 if (ret != EFI_SUCCESS)
570 return ret;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200571
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100572 /* Sanity check */
Heinrich Schuchardt7bf4bc92019-04-25 18:41:40 +0200573 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100574 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
575 memory, pages);
576 return EFI_INVALID_PARAMETER;
577 }
578
Michael Walle282d3862020-05-17 12:29:19 +0200579 ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY,
580 false);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100581 if (ret != EFI_SUCCESS)
582 return EFI_NOT_FOUND;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200583
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100584 return ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100585}
586
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100587/**
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100588 * efi_alloc_aligned_pages() - allocate aligned memory pages
Ilias Apalodimas6e01c232021-10-11 15:10:23 +0300589 *
590 * @len: len in bytes
591 * @memory_type: usage type of the allocated memory
592 * @align: alignment in bytes
593 * Return: aligned memory or NULL
594 */
595void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
596{
597 u64 req_pages = efi_size_in_pages(len);
598 u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
599 u64 free_pages;
600 u64 aligned_mem;
601 efi_status_t r;
602 u64 mem;
603
604 /* align must be zero or a power of two */
605 if (align & (align - 1))
606 return NULL;
607
608 /* Check for overflow */
609 if (true_pages < req_pages)
610 return NULL;
611
612 if (align < EFI_PAGE_SIZE) {
613 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
614 req_pages, &mem);
615 return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL;
616 }
617
618 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
619 true_pages, &mem);
620 if (r != EFI_SUCCESS)
621 return NULL;
622
623 aligned_mem = ALIGN(mem, align);
624 /* Free pages before alignment */
625 free_pages = efi_size_in_pages(aligned_mem - mem);
626 if (free_pages)
627 efi_free_pages(mem, free_pages);
628
629 /* Free trailing pages */
630 free_pages = true_pages - (req_pages + free_pages);
631 if (free_pages) {
632 mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
633 efi_free_pages(mem, free_pages);
634 }
635
636 return (void *)(uintptr_t)aligned_mem;
637}
638
639/**
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100640 * efi_allocate_pool - allocate memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100641 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100642 * @pool_type: type of the pool from which memory is to be allocated
643 * @size: number of bytes to be allocated
644 * @buffer: allocated memory
645 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100646 */
Heinrich Schuchardt98228942021-08-17 15:05:31 +0200647efi_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 +0200648{
649 efi_status_t r;
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100650 u64 addr;
Alexander Grafcd405172018-06-18 17:23:15 +0200651 struct efi_pool_allocation *alloc;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100652 u64 num_pages = efi_size_in_pages(size +
653 sizeof(struct efi_pool_allocation));
Stefan Brüns67b67d92016-10-09 22:17:26 +0200654
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200655 if (!buffer)
656 return EFI_INVALID_PARAMETER;
657
Stefan Brüns67b67d92016-10-09 22:17:26 +0200658 if (size == 0) {
659 *buffer = NULL;
660 return EFI_SUCCESS;
661 }
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200662
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200663 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100664 &addr);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200665 if (r == EFI_SUCCESS) {
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100666 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200667 alloc->num_pages = num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100668 alloc->checksum = checksum(alloc);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200669 *buffer = alloc->data;
670 }
671
672 return r;
673}
674
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100675/**
676 * efi_free_pool() - free memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100677 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100678 * @buffer: start of memory to be freed
679 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100680 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200681efi_status_t efi_free_pool(void *buffer)
682{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200683 efi_status_t ret;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200684 struct efi_pool_allocation *alloc;
685
Heinrich Schuchardt56b587f72019-06-12 07:17:04 +0200686 if (!buffer)
687 return EFI_INVALID_PARAMETER;
688
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200689 ret = efi_check_allocated((uintptr_t)buffer, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200690 if (ret != EFI_SUCCESS)
691 return ret;
xypron.glpk@gmx.de34ddae72017-07-14 19:12:39 +0200692
Stefan Brüns67b67d92016-10-09 22:17:26 +0200693 alloc = container_of(buffer, struct efi_pool_allocation, data);
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100694
695 /* Check that this memory was allocated by efi_allocate_pool() */
696 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
697 alloc->checksum != checksum(alloc)) {
698 printf("%s: illegal free 0x%p\n", __func__, buffer);
699 return EFI_INVALID_PARAMETER;
700 }
701 /* Avoid double free */
702 alloc->checksum = 0;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200703
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200704 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200705
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200706 return ret;
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200707}
708
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100709/**
710 * efi_get_memory_map() - get map describing memory usage.
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100711 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100712 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100713 * on exit the size of the copied memory map
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100714 * @memory_map: buffer to which the memory map is written
715 * @map_key: key for the memory map
716 * @descriptor_size: size of an individual memory descriptor
717 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100718 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100719 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100720efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
721 struct efi_mem_desc *memory_map,
722 efi_uintn_t *map_key,
723 efi_uintn_t *descriptor_size,
724 uint32_t *descriptor_version)
Alexander Graf8623f922016-03-04 01:10:04 +0100725{
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100726 efi_uintn_t map_size = 0;
Alexander Graf9b591022016-04-11 23:51:02 +0200727 int map_entries = 0;
Alexander Graf8623f922016-03-04 01:10:04 +0100728 struct list_head *lhandle;
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200729 efi_uintn_t provided_map_size;
Alexander Graf8623f922016-03-04 01:10:04 +0100730
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200731 if (!memory_map_size)
732 return EFI_INVALID_PARAMETER;
733
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200734 provided_map_size = *memory_map_size;
735
Alexander Graf8623f922016-03-04 01:10:04 +0100736 list_for_each(lhandle, &efi_mem)
Alexander Graf9b591022016-04-11 23:51:02 +0200737 map_entries++;
738
739 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf8623f922016-03-04 01:10:04 +0100740
Rob Clarka84c9742017-07-26 14:34:05 -0400741 *memory_map_size = map_size;
742
Alexander Graf8623f922016-03-04 01:10:04 +0100743 if (descriptor_size)
744 *descriptor_size = sizeof(struct efi_mem_desc);
745
Mian Yousaf Kaukab338cf562016-09-05 23:59:22 +0200746 if (descriptor_version)
747 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
748
AKASHI Takahiro3ee138d2020-03-11 15:18:18 +0900749 if (provided_map_size < map_size)
750 return EFI_BUFFER_TOO_SMALL;
751
752 if (!memory_map)
753 return EFI_INVALID_PARAMETER;
754
Alexander Graf8623f922016-03-04 01:10:04 +0100755 /* Copy list into array */
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200756 /* Return the list in ascending order */
757 memory_map = &memory_map[map_entries - 1];
758 list_for_each(lhandle, &efi_mem) {
759 struct efi_mem_list *lmem;
Alexander Graf8623f922016-03-04 01:10:04 +0100760
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200761 lmem = list_entry(lhandle, struct efi_mem_list, link);
762 *memory_map = lmem->desc;
763 memory_map--;
Alexander Graf8623f922016-03-04 01:10:04 +0100764 }
765
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200766 if (map_key)
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200767 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dee436e632017-07-21 19:05:44 +0200768
Alexander Graf8623f922016-03-04 01:10:04 +0100769 return EFI_SUCCESS;
770}
771
Park, Aidene009e7a2019-09-03 17:43:43 +0000772/**
Heinrich Schuchardt13ce2d92023-01-05 18:26:01 +0100773 * efi_get_memory_map_alloc() - allocate map describing memory usage
774 *
775 * The caller is responsible for calling FreePool() if the call succeeds.
776 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100777 * @map_size: size of the memory map
778 * @memory_map: buffer to which the memory map is written
Heinrich Schuchardt13ce2d92023-01-05 18:26:01 +0100779 * Return: status code
780 */
781efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size,
782 struct efi_mem_desc **memory_map)
783{
784 efi_status_t ret;
785
786 *memory_map = NULL;
787 *map_size = 0;
788 ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL);
789 if (ret == EFI_BUFFER_TOO_SMALL) {
790 *map_size += sizeof(struct efi_mem_desc); /* for the map */
791 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size,
792 (void **)memory_map);
793 if (ret != EFI_SUCCESS)
794 return ret;
795 ret = efi_get_memory_map(map_size, *memory_map,
796 NULL, NULL, NULL);
797 if (ret != EFI_SUCCESS) {
798 efi_free_pool(*memory_map);
799 *memory_map = NULL;
800 }
801 }
802
803 return ret;
804}
805
806/**
Park, Aidene009e7a2019-09-03 17:43:43 +0000807 * efi_add_conventional_memory_map() - add a RAM memory area to the map
808 *
809 * @ram_start: start address of a RAM memory area
810 * @ram_end: end address of a RAM memory area
811 * @ram_top: max address to be used as conventional memory
812 * Return: status code
813 */
814efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
815 u64 ram_top)
816{
817 u64 pages;
818
819 /* Remove partial pages */
820 ram_end &= ~EFI_PAGE_MASK;
821 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
822
823 if (ram_end <= ram_start) {
824 /* Invalid mapping */
825 return EFI_INVALID_PARAMETER;
826 }
827
828 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
829
Michael Walle282d3862020-05-17 12:29:19 +0200830 efi_add_memory_map_pg(ram_start, pages,
831 EFI_CONVENTIONAL_MEMORY, false);
Park, Aidene009e7a2019-09-03 17:43:43 +0000832
833 /*
834 * Boards may indicate to the U-Boot memory core that they
835 * can not support memory above ram_top. Let's honor this
836 * in the efi_loader subsystem too by declaring any memory
837 * above ram_top as "already occupied by firmware".
838 */
839 if (ram_top < ram_start) {
840 /* ram_top is before this region, reserve all */
Michael Walle282d3862020-05-17 12:29:19 +0200841 efi_add_memory_map_pg(ram_start, pages,
842 EFI_BOOT_SERVICES_DATA, true);
Heinrich Schuchardt3546eb22022-04-25 23:54:48 +0200843 } else if (ram_top < ram_end) {
Park, Aidene009e7a2019-09-03 17:43:43 +0000844 /* ram_top is inside this region, reserve parts */
845 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
846
Michael Walle282d3862020-05-17 12:29:19 +0200847 efi_add_memory_map_pg(ram_top, pages,
848 EFI_BOOT_SERVICES_DATA, true);
Park, Aidene009e7a2019-09-03 17:43:43 +0000849 }
850
851 return EFI_SUCCESS;
852}
853
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100854/**
855 * efi_add_known_memory() - add memory banks to map
856 *
857 * This function may be overridden for specific architectures.
858 */
York Suna8e9f0a2017-03-06 09:02:27 -0800859__weak void efi_add_known_memory(void)
Alexander Graf8623f922016-03-04 01:10:04 +0100860{
Alexander Grafc214edf2018-11-30 21:24:56 +0100861 u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
Alexander Graf8623f922016-03-04 01:10:04 +0100862 int i;
863
Heinrich Schuchardteb8edcf2019-01-05 23:41:36 +0100864 /*
865 * ram_top is just outside mapped memory. So use an offset of one for
866 * mapping the sandbox address.
867 */
868 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
869
Alexander Grafc214edf2018-11-30 21:24:56 +0100870 /* Fix for 32bit targets with ram_top at 4G */
871 if (!ram_top)
872 ram_top = 0x100000000ULL;
873
Alexander Graf8623f922016-03-04 01:10:04 +0100874 /* Add RAM */
875 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Park, Aidene009e7a2019-09-03 17:43:43 +0000876 u64 ram_end, ram_start;
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100877
Heinrich Schuchardt6738fcd2018-11-18 17:58:46 +0100878 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100879 ram_end = ram_start + gd->bd->bi_dram[i].size;
880
Park, Aidene009e7a2019-09-03 17:43:43 +0000881 efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
Alexander Graf8623f922016-03-04 01:10:04 +0100882 }
York Suna8e9f0a2017-03-06 09:02:27 -0800883}
884
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100885/**
886 * add_u_boot_and_runtime() - add U-Boot code to memory map
887 *
888 * Add memory regions for U-Boot's memory and for the runtime services code.
889 */
Simon Glass78639432018-06-18 17:23:12 +0200890static void add_u_boot_and_runtime(void)
York Suna8e9f0a2017-03-06 09:02:27 -0800891{
892 unsigned long runtime_start, runtime_end, runtime_pages;
Alexander Graf15937a22018-09-17 13:54:33 +0200893 unsigned long runtime_mask = EFI_PAGE_MASK;
York Suna8e9f0a2017-03-06 09:02:27 -0800894 unsigned long uboot_start, uboot_pages;
Heinrich Schuchardtc6dcbe12020-07-29 12:37:35 +0200895 unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
York Suna8e9f0a2017-03-06 09:02:27 -0800896
Alexander Graf8623f922016-03-04 01:10:04 +0100897 /* Add U-Boot */
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100898 uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
899 uboot_stack_size) & ~EFI_PAGE_MASK;
900 uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
901 uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Heinrich Schuchardt296b55c2022-11-29 16:00:41 +0100902 efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE,
Michael Walle282d3862020-05-17 12:29:19 +0200903 false);
Alexander Graf8623f922016-03-04 01:10:04 +0100904
Alexander Graf15937a22018-09-17 13:54:33 +0200905#if defined(__aarch64__)
906 /*
907 * Runtime Services must be 64KiB aligned according to the
908 * "AArch64 Platforms" section in the UEFI spec (2.7+).
909 */
910
911 runtime_mask = SZ_64K - 1;
912#endif
913
914 /*
915 * Add Runtime Services. We mark surrounding boottime code as runtime as
916 * well to fulfill the runtime alignment constraints but avoid padding.
917 */
918 runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100919 runtime_end = (ulong)&__efi_runtime_stop;
Alexander Graf15937a22018-09-17 13:54:33 +0200920 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100921 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
Michael Walle282d3862020-05-17 12:29:19 +0200922 efi_add_memory_map_pg(runtime_start, runtime_pages,
923 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass78639432018-06-18 17:23:12 +0200924}
925
926int efi_memory_init(void)
927{
928 efi_add_known_memory();
929
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100930 add_u_boot_and_runtime();
Alexander Graf8623f922016-03-04 01:10:04 +0100931
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200932#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
933 /* Request a 32bit 64MB bounce buffer region */
934 uint64_t efi_bounce_buffer_addr = 0xffffffff;
935
Heinrich Schuchardt296b55c2022-11-29 16:00:41 +0100936 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200937 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
938 &efi_bounce_buffer_addr) != EFI_SUCCESS)
939 return -1;
940
941 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
942#endif
943
Alexander Graf8623f922016-03-04 01:10:04 +0100944 return 0;
945}