blob: 45d3bf52b8a4ae3e71331ea0ea76ec6e151d9aa4 [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
Heinrich Schuchardt257a4982023-07-30 12:27:03 +020037#define EFI_CARVE_OUT_OF_RESOURCES -4
Alexander Graf74c16ac2016-05-27 12:25:03 +020038
Alexander Graf5d009952016-03-04 01:10:04 +010039/* This list contains all memory map items */
Bin Meng207b6862023-04-05 20:15:18 +080040static LIST_HEAD(efi_mem);
Alexander Graf5d009952016-03-04 01:10:04 +010041
Alexander Graf51735ae2016-05-11 18:25:48 +020042#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
43void *efi_bounce_buffer;
44#endif
45
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010046/**
Heinrich Schuchardt4be077b2019-07-14 12:28:56 +020047 * struct efi_pool_allocation - memory block allocated from pool
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010048 *
49 * @num_pages: number of pages allocated
50 * @checksum: checksum
Heinrich Schuchardt4be077b2019-07-14 12:28:56 +020051 * @data: allocated pool memory
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010052 *
Heinrich Schuchardt4be077b2019-07-14 12:28:56 +020053 * U-Boot services each UEFI AllocatePool() request as a separate
54 * (multiple) page allocation. We have to track the number of pages
Stefan Brüns42417bc2016-10-09 22:17:26 +020055 * to be able to free the correct amount later.
Heinrich Schuchardt4be077b2019-07-14 12:28:56 +020056 *
57 * The checksum calculated in function checksum() is used in FreePool() to avoid
58 * freeing memory not allocated by AllocatePool() and duplicate freeing.
59 *
Stefan Brüns42417bc2016-10-09 22:17:26 +020060 * EFI requires 8 byte alignment for pool allocations, so we can
Heinrich Schuchardt4be077b2019-07-14 12:28:56 +020061 * prepend each allocation with these header fields.
Stefan Brüns42417bc2016-10-09 22:17:26 +020062 */
63struct efi_pool_allocation {
64 u64 num_pages;
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010065 u64 checksum;
Rob Clark946160f2017-09-13 18:05:36 -040066 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns42417bc2016-10-09 22:17:26 +020067};
68
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010069/**
70 * checksum() - calculate checksum for memory allocated from pool
71 *
72 * @alloc: allocation header
73 * Return: checksum, always non-zero
74 */
75static u64 checksum(struct efi_pool_allocation *alloc)
76{
77 u64 addr = (uintptr_t)alloc;
78 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
79 EFI_ALLOC_POOL_MAGIC;
80 if (!ret)
81 ++ret;
82 return ret;
83}
84
Heinrich Schuchardt0763c022023-01-08 01:00:00 +010085/**
86 * efi_mem_cmp() - comparator function for sorting memory map
87 *
Alexander Graf38ce65e2016-03-30 16:38:29 +020088 * Sorts the memory list from highest address to lowest address
89 *
90 * When allocating memory we should always start from the highest
91 * address chunk, so sort the memory list such that the first list
92 * iterator gets the highest address and goes lower from there.
Heinrich Schuchardt0763c022023-01-08 01:00:00 +010093 *
94 * @priv: unused
95 * @a: first memory area
96 * @b: second memory area
97 * Return: 1 if @a is before @b, -1 if @b is before @a, 0 if equal
Alexander Graf38ce65e2016-03-30 16:38:29 +020098 */
99static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
100{
101 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
102 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
103
104 if (mema->desc.physical_start == memb->desc.physical_start)
105 return 0;
106 else if (mema->desc.physical_start < memb->desc.physical_start)
107 return 1;
108 else
109 return -1;
110}
111
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100112/**
113 * desc_get_end() - get end address of memory area
114 *
115 * @desc: memory descriptor
116 * Return: end address + 1
117 */
Alexander Graf7b056672018-09-16 17:05:29 +0200118static uint64_t desc_get_end(struct efi_mem_desc *desc)
119{
120 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
121}
122
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100123/**
124 * efi_mem_sort() - sort memory map
125 *
126 * Sort the memory map and then try to merge adjacent memory areas.
127 */
Alexander Graf38ce65e2016-03-30 16:38:29 +0200128static void efi_mem_sort(void)
129{
Alexander Graf7b056672018-09-16 17:05:29 +0200130 struct list_head *lhandle;
131 struct efi_mem_list *prevmem = NULL;
132 bool merge_again = true;
133
Alexander Graf38ce65e2016-03-30 16:38:29 +0200134 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Graf7b056672018-09-16 17:05:29 +0200135
136 /* Now merge entries that can be merged */
137 while (merge_again) {
138 merge_again = false;
139 list_for_each(lhandle, &efi_mem) {
140 struct efi_mem_list *lmem;
141 struct efi_mem_desc *prev = &prevmem->desc;
142 struct efi_mem_desc *cur;
143 uint64_t pages;
144
145 lmem = list_entry(lhandle, struct efi_mem_list, link);
146 if (!prevmem) {
147 prevmem = lmem;
148 continue;
149 }
150
151 cur = &lmem->desc;
152
153 if ((desc_get_end(cur) == prev->physical_start) &&
154 (prev->type == cur->type) &&
155 (prev->attribute == cur->attribute)) {
156 /* There is an existing map before, reuse it */
157 pages = cur->num_pages;
158 prev->num_pages += pages;
159 prev->physical_start -= pages << EFI_PAGE_SHIFT;
160 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
161 list_del(&lmem->link);
162 free(lmem);
163
164 merge_again = true;
165 break;
166 }
167
168 prevmem = lmem;
169 }
170 }
Alexander Graf38ce65e2016-03-30 16:38:29 +0200171}
172
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100173/**
174 * efi_mem_carve_out() - unmap memory region
Alexander Graf5d009952016-03-04 01:10:04 +0100175 *
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200176 * @map: memory map
177 * @carve_desc: memory region to unmap
178 * @overlap_only_ram: the carved out region may only overlap RAM
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100179 * Return: the number of overlapping pages which have been
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200180 * removed from the map,
181 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
182 * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
183 * and the map contains anything but free ram
184 * (only when overlap_only_ram is true),
185 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
186 * traversed again, as it has been altered.
187 *
188 * Unmaps all memory occupied by the carve_desc region from the list entry
189 * pointed to by map.
Stefan Brüns852efbf2016-10-01 23:32:23 +0200190 *
191 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200192 * to re-add the already carved out pages to the mapping.
Alexander Graf5d009952016-03-04 01:10:04 +0100193 */
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200194static s64 efi_mem_carve_out(struct efi_mem_list *map,
Alexander Graf5d009952016-03-04 01:10:04 +0100195 struct efi_mem_desc *carve_desc,
196 bool overlap_only_ram)
197{
198 struct efi_mem_list *newmap;
199 struct efi_mem_desc *map_desc = &map->desc;
200 uint64_t map_start = map_desc->physical_start;
201 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
202 uint64_t carve_start = carve_desc->physical_start;
203 uint64_t carve_end = carve_start +
204 (carve_desc->num_pages << EFI_PAGE_SHIFT);
205
206 /* check whether we're overlapping */
207 if ((carve_end <= map_start) || (carve_start >= map_end))
Alexander Graf74c16ac2016-05-27 12:25:03 +0200208 return EFI_CARVE_NO_OVERLAP;
Alexander Graf5d009952016-03-04 01:10:04 +0100209
210 /* We're overlapping with non-RAM, warn the caller if desired */
211 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
Alexander Graf74c16ac2016-05-27 12:25:03 +0200212 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf5d009952016-03-04 01:10:04 +0100213
214 /* Sanitize carve_start and carve_end to lie within our bounds */
215 carve_start = max(carve_start, map_start);
216 carve_end = min(carve_end, map_end);
217
218 /* Carving at the beginning of our map? Just move it! */
219 if (carve_start == map_start) {
220 if (map_end == carve_end) {
221 /* Full overlap, just remove map */
222 list_del(&map->link);
Stefan Brüns511d0b92016-10-01 23:32:29 +0200223 free(map);
224 } else {
225 map->desc.physical_start = carve_end;
Heinrich Schuchardt9631fa02019-04-11 20:08:54 +0200226 map->desc.virtual_start = carve_end;
Stefan Brüns511d0b92016-10-01 23:32:29 +0200227 map->desc.num_pages = (map_end - carve_end)
228 >> EFI_PAGE_SHIFT;
Alexander Graf5d009952016-03-04 01:10:04 +0100229 }
230
Alexander Graf74c16ac2016-05-27 12:25:03 +0200231 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf5d009952016-03-04 01:10:04 +0100232 }
233
234 /*
235 * Overlapping maps, just split the list map at carve_start,
236 * it will get moved or removed in the next iteration.
237 *
238 * [ map_desc |__carve_start__| newmap ]
239 */
240
241 /* Create a new map from [ carve_start ... map_end ] */
242 newmap = calloc(1, sizeof(*newmap));
Heinrich Schuchardt257a4982023-07-30 12:27:03 +0200243 if (!newmap)
244 return EFI_CARVE_OUT_OF_RESOURCES;
Alexander Graf5d009952016-03-04 01:10:04 +0100245 newmap->desc = map->desc;
246 newmap->desc.physical_start = carve_start;
Heinrich Schuchardt9631fa02019-04-11 20:08:54 +0200247 newmap->desc.virtual_start = carve_start;
Alexander Graf5d009952016-03-04 01:10:04 +0100248 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
Stefan Brünsb6a95172016-10-01 23:32:28 +0200249 /* Insert before current entry (descending address order) */
250 list_add_tail(&newmap->link, &map->link);
Alexander Graf5d009952016-03-04 01:10:04 +0100251
252 /* Shrink the map to [ map_start ... carve_start ] */
253 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
254
Alexander Graf74c16ac2016-05-27 12:25:03 +0200255 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf5d009952016-03-04 01:10:04 +0100256}
257
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100258/**
Michael Walle714497e2020-05-17 12:29:19 +0200259 * efi_add_memory_map_pg() - add pages to the memory map
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100260 *
261 * @start: start address, must be a multiple of EFI_PAGE_SIZE
262 * @pages: number of pages to add
263 * @memory_type: type of memory added
Maxim Uvarovffbeafe2020-08-28 22:47:41 +0300264 * @overlap_only_ram: region may only overlap RAM
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100265 * Return: status code
266 */
Michael Walle714497e2020-05-17 12:29:19 +0200267static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
268 int memory_type,
269 bool overlap_only_ram)
Alexander Graf5d009952016-03-04 01:10:04 +0100270{
271 struct list_head *lhandle;
272 struct efi_mem_list *newlist;
Alexander Graf74c16ac2016-05-27 12:25:03 +0200273 bool carve_again;
274 uint64_t carved_pages = 0;
Heinrich Schuchardte80474a2019-06-04 20:55:12 +0200275 struct efi_event *evt;
Alexander Graf5d009952016-03-04 01:10:04 +0100276
Heinrich Schuchardte301e022019-04-04 21:50:02 +0200277 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
278 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
Andreas Färberc933ed92016-07-17 06:57:11 +0200279
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +0200280 if (memory_type >= EFI_MAX_MEMORY_TYPE)
281 return EFI_INVALID_PARAMETER;
282
Alexander Graf5d009952016-03-04 01:10:04 +0100283 if (!pages)
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100284 return EFI_SUCCESS;
Alexander Graf5d009952016-03-04 01:10:04 +0100285
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +0200286 ++efi_memory_map_key;
Alexander Graf5d009952016-03-04 01:10:04 +0100287 newlist = calloc(1, sizeof(*newlist));
Heinrich Schuchardtba275632023-07-30 12:36:17 +0200288 if (!newlist)
289 return EFI_OUT_OF_RESOURCES;
Alexander Graf5d009952016-03-04 01:10:04 +0100290 newlist->desc.type = memory_type;
291 newlist->desc.physical_start = start;
292 newlist->desc.virtual_start = start;
293 newlist->desc.num_pages = pages;
294
295 switch (memory_type) {
296 case EFI_RUNTIME_SERVICES_CODE:
297 case EFI_RUNTIME_SERVICES_DATA:
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200298 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
Alexander Graf5d009952016-03-04 01:10:04 +0100299 break;
300 case EFI_MMAP_IO:
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200301 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
Alexander Graf5d009952016-03-04 01:10:04 +0100302 break;
303 default:
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200304 newlist->desc.attribute = EFI_MEMORY_WB;
Alexander Graf5d009952016-03-04 01:10:04 +0100305 break;
306 }
307
308 /* Add our new map */
309 do {
Alexander Graf74c16ac2016-05-27 12:25:03 +0200310 carve_again = false;
Alexander Graf5d009952016-03-04 01:10:04 +0100311 list_for_each(lhandle, &efi_mem) {
312 struct efi_mem_list *lmem;
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200313 s64 r;
Alexander Graf5d009952016-03-04 01:10:04 +0100314
315 lmem = list_entry(lhandle, struct efi_mem_list, link);
316 r = efi_mem_carve_out(lmem, &newlist->desc,
317 overlap_only_ram);
Alexander Graf74c16ac2016-05-27 12:25:03 +0200318 switch (r) {
Heinrich Schuchardt257a4982023-07-30 12:27:03 +0200319 case EFI_CARVE_OUT_OF_RESOURCES:
320 free(newlist);
321 return EFI_OUT_OF_RESOURCES;
Alexander Graf74c16ac2016-05-27 12:25:03 +0200322 case EFI_CARVE_OVERLAPS_NONRAM:
323 /*
324 * The user requested to only have RAM overlaps,
325 * but we hit a non-RAM region. Error out.
326 */
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100327 return EFI_NO_MAPPING;
Alexander Graf74c16ac2016-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;
342 break;
343 }
344
345 if (carve_again) {
346 /* The list changed, we need to start over */
Alexander Graf5d009952016-03-04 01:10:04 +0100347 break;
348 }
349 }
Alexander Graf74c16ac2016-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 */
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100357 return EFI_NO_MAPPING;
Alexander Graf74c16ac2016-05-27 12:25:03 +0200358 }
Alexander Graf5d009952016-03-04 01:10:04 +0100359
360 /* Add our new map */
361 list_add_tail(&newlist->link, &efi_mem);
362
Alexander Graf38ce65e2016-03-30 16:38:29 +0200363 /* And make sure memory is listed in descending order */
364 efi_mem_sort();
365
Heinrich Schuchardte80474a2019-06-04 20:55:12 +0200366 /* Notify that the memory map was changed */
367 list_for_each_entry(evt, &efi_events, link) {
368 if (evt->group &&
369 !guidcmp(evt->group,
370 &efi_guid_event_group_memory_map_change)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200371 efi_signal_event(evt);
Heinrich Schuchardte80474a2019-06-04 20:55:12 +0200372 break;
373 }
374 }
375
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100376 return EFI_SUCCESS;
Alexander Graf5d009952016-03-04 01:10:04 +0100377}
378
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200379/**
Michael Walle714497e2020-05-17 12:29:19 +0200380 * efi_add_memory_map() - add memory area to the memory map
381 *
382 * @start: start address of the memory area
383 * @size: length in bytes of the memory area
384 * @memory_type: type of memory added
385 *
386 * Return: status code
387 *
388 * This function automatically aligns the start and size of the memory area
389 * to EFI_PAGE_SIZE.
390 */
391efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
392{
393 u64 pages;
394
395 pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
396 start &= ~EFI_PAGE_MASK;
397
398 return efi_add_memory_map_pg(start, pages, memory_type, false);
399}
400
401/**
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200402 * efi_check_allocated() - validate address to be freed
403 *
404 * Check that the address is within allocated memory:
405 *
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200406 * * The address must be in a range of the memory map.
407 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
408 *
409 * Page alignment is not checked as this is not a requirement of
410 * efi_free_pool().
411 *
Heinrich Schuchardtf756fe82019-05-11 08:21:17 +0200412 * @addr: address of page to be freed
413 * @must_be_allocated: return success if the page is allocated
414 * Return: status code
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200415 */
Heinrich Schuchardtf756fe82019-05-11 08:21:17 +0200416static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200417{
418 struct efi_mem_list *item;
419
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200420 list_for_each_entry(item, &efi_mem, link) {
421 u64 start = item->desc.physical_start;
422 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
423
424 if (addr >= start && addr < end) {
Heinrich Schuchardtf756fe82019-05-11 08:21:17 +0200425 if (must_be_allocated ^
426 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200427 return EFI_SUCCESS;
428 else
429 return EFI_NOT_FOUND;
430 }
431 }
432
433 return EFI_NOT_FOUND;
434}
435
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100436/**
437 * efi_find_free_memory() - find free memory pages
438 *
439 * @len: size of memory area needed
440 * @max_addr: highest address to allocate
441 * Return: pointer to free memory area or 0
442 */
Alexander Graf5d009952016-03-04 01:10:04 +0100443static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
444{
445 struct list_head *lhandle;
446
Alexander Grafc2e1ad72018-11-05 00:30:46 +0100447 /*
448 * Prealign input max address, so we simplify our matching
449 * logic below and can just reuse it as return pointer.
450 */
451 max_addr &= ~EFI_PAGE_MASK;
452
Alexander Graf5d009952016-03-04 01:10:04 +0100453 list_for_each(lhandle, &efi_mem) {
454 struct efi_mem_list *lmem = list_entry(lhandle,
455 struct efi_mem_list, link);
456 struct efi_mem_desc *desc = &lmem->desc;
457 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
458 uint64_t desc_end = desc->physical_start + desc_len;
459 uint64_t curmax = min(max_addr, desc_end);
460 uint64_t ret = curmax - len;
461
462 /* We only take memory from free RAM */
463 if (desc->type != EFI_CONVENTIONAL_MEMORY)
464 continue;
465
466 /* Out of bounds for max_addr */
467 if ((ret + len) > max_addr)
468 continue;
469
470 /* Out of bounds for upper map limit */
471 if ((ret + len) > desc_end)
472 continue;
473
474 /* Out of bounds for lower map limit */
475 if (ret < desc->physical_start)
476 continue;
477
478 /* Return the highest address in this map within bounds */
479 return ret;
480 }
481
482 return 0;
483}
484
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100485/**
486 * efi_allocate_pages - allocate memory pages
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100487 *
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100488 * @type: type of allocation to be performed
489 * @memory_type: usage type of the allocated memory
490 * @pages: number of pages to be allocated
491 * @memory: allocated memory
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100492 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100493 */
Heinrich Schuchardt49d225e2021-08-17 15:05:31 +0200494efi_status_t efi_allocate_pages(enum efi_allocate_type type,
495 enum efi_memory_type memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100496 efi_uintn_t pages, uint64_t *memory)
Alexander Graf5d009952016-03-04 01:10:04 +0100497{
Heinrich Schuchardt48d183f2023-07-30 11:31:08 +0200498 u64 len;
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200499 efi_status_t ret;
Alexander Graf5d009952016-03-04 01:10:04 +0100500 uint64_t addr;
501
Heinrich Schuchardtf12bcc92019-04-23 00:30:53 +0200502 /* Check import parameters */
503 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
504 memory_type <= 0x6FFFFFFF)
505 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt4d5e0712018-07-02 12:53:53 +0200506 if (!memory)
507 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt48d183f2023-07-30 11:31:08 +0200508 len = (u64)pages << EFI_PAGE_SHIFT;
509 /* Catch possible overflow on 64bit systems */
510 if (sizeof(efi_uintn_t) == sizeof(u64) &&
511 (len >> EFI_PAGE_SHIFT) != (u64)pages)
512 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt4d5e0712018-07-02 12:53:53 +0200513
Alexander Graf5d009952016-03-04 01:10:04 +0100514 switch (type) {
Heinrich Schuchardt7c92fd62018-01-30 21:08:00 +0100515 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf5d009952016-03-04 01:10:04 +0100516 /* Any page */
Stephen Warren14deb5e2018-08-30 15:43:45 -0600517 addr = efi_find_free_memory(len, -1ULL);
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_MAX_ADDRESS:
Alexander Graf5d009952016-03-04 01:10:04 +0100522 /* Max address */
523 addr = efi_find_free_memory(len, *memory);
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200524 if (!addr)
525 return EFI_OUT_OF_RESOURCES;
Alexander Graf5d009952016-03-04 01:10:04 +0100526 break;
Heinrich Schuchardt7c92fd62018-01-30 21:08:00 +0100527 case EFI_ALLOCATE_ADDRESS:
Heinrich Schuchardt53def682022-11-06 01:52:13 +0100528 if (*memory & EFI_PAGE_MASK)
529 return EFI_NOT_FOUND;
Alexander Graf5d009952016-03-04 01:10:04 +0100530 /* Exact address, reserve it. The addr is already in *memory. */
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200531 ret = efi_check_allocated(*memory, false);
532 if (ret != EFI_SUCCESS)
533 return EFI_NOT_FOUND;
Alexander Graf5d009952016-03-04 01:10:04 +0100534 addr = *memory;
535 break;
536 default:
537 /* UEFI doesn't specify other allocation types */
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200538 return EFI_INVALID_PARAMETER;
Alexander Graf5d009952016-03-04 01:10:04 +0100539 }
540
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200541 /* Reserve that map in our memory maps */
Michael Walle714497e2020-05-17 12:29:19 +0200542 ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
543 if (ret != EFI_SUCCESS)
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200544 /* Map would overlap, bail out */
545 return EFI_OUT_OF_RESOURCES;
Alexander Graf5d009952016-03-04 01:10:04 +0100546
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200547 *memory = addr;
Alexander Graf5d009952016-03-04 01:10:04 +0100548
Heinrich Schuchardt8ae39852019-05-11 08:43:30 +0200549 return EFI_SUCCESS;
Alexander Graf5d009952016-03-04 01:10:04 +0100550}
551
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100552/**
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100553 * efi_free_pages() - free memory pages
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100554 *
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100555 * @memory: start of the memory area to be freed
556 * @pages: number of pages to be freed
557 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100558 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100559efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf5d009952016-03-04 01:10:04 +0100560{
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200561 efi_status_t ret;
562
Heinrich Schuchardtf756fe82019-05-11 08:21:17 +0200563 ret = efi_check_allocated(memory, true);
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200564 if (ret != EFI_SUCCESS)
565 return ret;
Stefan Brünsb61d8572016-10-01 23:32:27 +0200566
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100567 /* Sanity check */
Heinrich Schuchardte00b82d2019-04-25 18:41:40 +0200568 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100569 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
570 memory, pages);
571 return EFI_INVALID_PARAMETER;
572 }
573
Michael Walle714497e2020-05-17 12:29:19 +0200574 ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY,
575 false);
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100576 if (ret != EFI_SUCCESS)
577 return EFI_NOT_FOUND;
Stefan Brünsb61d8572016-10-01 23:32:27 +0200578
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100579 return ret;
Alexander Graf5d009952016-03-04 01:10:04 +0100580}
581
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100582/**
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100583 * efi_alloc_aligned_pages() - allocate aligned memory pages
Ilias Apalodimasebdea882021-10-11 15:10:23 +0300584 *
585 * @len: len in bytes
586 * @memory_type: usage type of the allocated memory
587 * @align: alignment in bytes
588 * Return: aligned memory or NULL
589 */
590void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
591{
592 u64 req_pages = efi_size_in_pages(len);
593 u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
594 u64 free_pages;
595 u64 aligned_mem;
596 efi_status_t r;
597 u64 mem;
598
599 /* align must be zero or a power of two */
600 if (align & (align - 1))
601 return NULL;
602
603 /* Check for overflow */
604 if (true_pages < req_pages)
605 return NULL;
606
607 if (align < EFI_PAGE_SIZE) {
608 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
609 req_pages, &mem);
610 return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL;
611 }
612
613 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
614 true_pages, &mem);
615 if (r != EFI_SUCCESS)
616 return NULL;
617
618 aligned_mem = ALIGN(mem, align);
619 /* Free pages before alignment */
620 free_pages = efi_size_in_pages(aligned_mem - mem);
621 if (free_pages)
622 efi_free_pages(mem, free_pages);
623
624 /* Free trailing pages */
625 free_pages = true_pages - (req_pages + free_pages);
626 if (free_pages) {
627 mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
628 efi_free_pages(mem, free_pages);
629 }
630
631 return (void *)(uintptr_t)aligned_mem;
632}
633
634/**
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100635 * efi_allocate_pool - allocate memory from pool
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100636 *
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100637 * @pool_type: type of the pool from which memory is to be allocated
638 * @size: number of bytes to be allocated
639 * @buffer: allocated memory
640 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100641 */
Heinrich Schuchardt49d225e2021-08-17 15:05:31 +0200642efi_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 +0200643{
644 efi_status_t r;
Heinrich Schuchardt306b1672019-03-18 20:01:59 +0100645 u64 addr;
Alexander Graf282a06c2018-06-18 17:23:15 +0200646 struct efi_pool_allocation *alloc;
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100647 u64 num_pages = efi_size_in_pages(size +
648 sizeof(struct efi_pool_allocation));
Stefan Brüns42417bc2016-10-09 22:17:26 +0200649
Heinrich Schuchardt4d5e0712018-07-02 12:53:53 +0200650 if (!buffer)
651 return EFI_INVALID_PARAMETER;
652
Stefan Brüns42417bc2016-10-09 22:17:26 +0200653 if (size == 0) {
654 *buffer = NULL;
655 return EFI_SUCCESS;
656 }
Stefan Brünsead12742016-10-09 22:17:18 +0200657
Heinrich Schuchardte09159c2018-05-26 10:32:27 +0200658 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Heinrich Schuchardt306b1672019-03-18 20:01:59 +0100659 &addr);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200660 if (r == EFI_SUCCESS) {
Heinrich Schuchardt306b1672019-03-18 20:01:59 +0100661 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
Stefan Brüns42417bc2016-10-09 22:17:26 +0200662 alloc->num_pages = num_pages;
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100663 alloc->checksum = checksum(alloc);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200664 *buffer = alloc->data;
665 }
666
667 return r;
668}
669
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100670/**
Heinrich Schuchardtf606fab2023-03-19 09:20:22 +0100671 * efi_alloc() - allocate boot services data pool memory
672 *
673 * Allocate memory from pool and zero it out.
674 *
675 * @size: number of bytes to allocate
676 * Return: pointer to allocated memory or NULL
677 */
678void *efi_alloc(size_t size)
679{
680 void *buf;
681
682 if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, size, &buf) !=
683 EFI_SUCCESS) {
684 log_err("out of memory");
685 return NULL;
686 }
687 memset(buf, 0, size);
688
689 return buf;
690}
691
692/**
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100693 * efi_free_pool() - free memory from pool
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100694 *
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100695 * @buffer: start of memory to be freed
696 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100697 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200698efi_status_t efi_free_pool(void *buffer)
699{
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200700 efi_status_t ret;
Stefan Brüns42417bc2016-10-09 22:17:26 +0200701 struct efi_pool_allocation *alloc;
702
Heinrich Schuchardt0e22c7c2019-06-12 07:17:04 +0200703 if (!buffer)
704 return EFI_INVALID_PARAMETER;
705
Heinrich Schuchardtf756fe82019-05-11 08:21:17 +0200706 ret = efi_check_allocated((uintptr_t)buffer, true);
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200707 if (ret != EFI_SUCCESS)
708 return ret;
xypron.glpk@gmx.de71275a32017-07-14 19:12:39 +0200709
Stefan Brüns42417bc2016-10-09 22:17:26 +0200710 alloc = container_of(buffer, struct efi_pool_allocation, data);
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100711
712 /* Check that this memory was allocated by efi_allocate_pool() */
713 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
714 alloc->checksum != checksum(alloc)) {
715 printf("%s: illegal free 0x%p\n", __func__, buffer);
716 return EFI_INVALID_PARAMETER;
717 }
718 /* Avoid double free */
719 alloc->checksum = 0;
Stefan Brüns42417bc2016-10-09 22:17:26 +0200720
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200721 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brünsead12742016-10-09 22:17:18 +0200722
Heinrich Schuchardt7d3af582019-05-10 21:21:30 +0200723 return ret;
Stefan Brünsead12742016-10-09 22:17:18 +0200724}
725
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100726/**
727 * efi_get_memory_map() - get map describing memory usage.
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100728 *
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100729 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100730 * on exit the size of the copied memory map
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100731 * @memory_map: buffer to which the memory map is written
732 * @map_key: key for the memory map
733 * @descriptor_size: size of an individual memory descriptor
734 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100735 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100736 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100737efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
738 struct efi_mem_desc *memory_map,
739 efi_uintn_t *map_key,
740 efi_uintn_t *descriptor_size,
741 uint32_t *descriptor_version)
Alexander Graf5d009952016-03-04 01:10:04 +0100742{
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100743 efi_uintn_t map_size = 0;
Alexander Grafcee752f2016-04-11 23:51:02 +0200744 int map_entries = 0;
Alexander Graf5d009952016-03-04 01:10:04 +0100745 struct list_head *lhandle;
Heinrich Schuchardtfa995d0d2018-08-06 22:28:18 +0200746 efi_uintn_t provided_map_size;
Alexander Graf5d009952016-03-04 01:10:04 +0100747
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200748 if (!memory_map_size)
749 return EFI_INVALID_PARAMETER;
750
Heinrich Schuchardtfa995d0d2018-08-06 22:28:18 +0200751 provided_map_size = *memory_map_size;
752
Alexander Graf5d009952016-03-04 01:10:04 +0100753 list_for_each(lhandle, &efi_mem)
Alexander Grafcee752f2016-04-11 23:51:02 +0200754 map_entries++;
755
756 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf5d009952016-03-04 01:10:04 +0100757
Rob Clarka1b24822017-07-26 14:34:05 -0400758 *memory_map_size = map_size;
759
Alexander Graf5d009952016-03-04 01:10:04 +0100760 if (descriptor_size)
761 *descriptor_size = sizeof(struct efi_mem_desc);
762
Mian Yousaf Kaukab4c02c112016-09-05 23:59:22 +0200763 if (descriptor_version)
764 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
765
AKASHI Takahirob4842962020-03-11 15:18:18 +0900766 if (provided_map_size < map_size)
767 return EFI_BUFFER_TOO_SMALL;
768
769 if (!memory_map)
770 return EFI_INVALID_PARAMETER;
771
Alexander Graf5d009952016-03-04 01:10:04 +0100772 /* Copy list into array */
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200773 /* Return the list in ascending order */
774 memory_map = &memory_map[map_entries - 1];
775 list_for_each(lhandle, &efi_mem) {
776 struct efi_mem_list *lmem;
Alexander Graf5d009952016-03-04 01:10:04 +0100777
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200778 lmem = list_entry(lhandle, struct efi_mem_list, link);
779 *memory_map = lmem->desc;
780 memory_map--;
Alexander Graf5d009952016-03-04 01:10:04 +0100781 }
782
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200783 if (map_key)
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +0200784 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dec6e3c3e2017-07-21 19:05:44 +0200785
Alexander Graf5d009952016-03-04 01:10:04 +0100786 return EFI_SUCCESS;
787}
788
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000789/**
Heinrich Schuchardteff44402023-01-05 18:26:01 +0100790 * efi_get_memory_map_alloc() - allocate map describing memory usage
791 *
792 * The caller is responsible for calling FreePool() if the call succeeds.
793 *
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100794 * @map_size: size of the memory map
795 * @memory_map: buffer to which the memory map is written
Heinrich Schuchardteff44402023-01-05 18:26:01 +0100796 * Return: status code
797 */
798efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size,
799 struct efi_mem_desc **memory_map)
800{
801 efi_status_t ret;
802
803 *memory_map = NULL;
804 *map_size = 0;
805 ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL);
806 if (ret == EFI_BUFFER_TOO_SMALL) {
807 *map_size += sizeof(struct efi_mem_desc); /* for the map */
808 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size,
809 (void **)memory_map);
810 if (ret != EFI_SUCCESS)
811 return ret;
812 ret = efi_get_memory_map(map_size, *memory_map,
813 NULL, NULL, NULL);
814 if (ret != EFI_SUCCESS) {
815 efi_free_pool(*memory_map);
816 *memory_map = NULL;
817 }
818 }
819
820 return ret;
821}
822
823/**
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000824 * efi_add_conventional_memory_map() - add a RAM memory area to the map
825 *
826 * @ram_start: start address of a RAM memory area
827 * @ram_end: end address of a RAM memory area
828 * @ram_top: max address to be used as conventional memory
829 * Return: status code
830 */
831efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
832 u64 ram_top)
833{
834 u64 pages;
835
836 /* Remove partial pages */
837 ram_end &= ~EFI_PAGE_MASK;
838 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
839
840 if (ram_end <= ram_start) {
841 /* Invalid mapping */
842 return EFI_INVALID_PARAMETER;
843 }
844
845 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
846
Michael Walle714497e2020-05-17 12:29:19 +0200847 efi_add_memory_map_pg(ram_start, pages,
848 EFI_CONVENTIONAL_MEMORY, false);
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000849
850 /*
851 * Boards may indicate to the U-Boot memory core that they
852 * can not support memory above ram_top. Let's honor this
853 * in the efi_loader subsystem too by declaring any memory
854 * above ram_top as "already occupied by firmware".
855 */
856 if (ram_top < ram_start) {
857 /* ram_top is before this region, reserve all */
Michael Walle714497e2020-05-17 12:29:19 +0200858 efi_add_memory_map_pg(ram_start, pages,
859 EFI_BOOT_SERVICES_DATA, true);
Heinrich Schuchardt8da26f52022-04-25 23:54:48 +0200860 } else if (ram_top < ram_end) {
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000861 /* ram_top is inside this region, reserve parts */
862 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
863
Michael Walle714497e2020-05-17 12:29:19 +0200864 efi_add_memory_map_pg(ram_top, pages,
865 EFI_BOOT_SERVICES_DATA, true);
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000866 }
867
868 return EFI_SUCCESS;
869}
870
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100871/**
872 * efi_add_known_memory() - add memory banks to map
873 *
874 * This function may be overridden for specific architectures.
875 */
York Sun42633742017-03-06 09:02:27 -0800876__weak void efi_add_known_memory(void)
Alexander Graf5d009952016-03-04 01:10:04 +0100877{
Alexander Graf7b78d642018-11-30 21:24:56 +0100878 u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
Alexander Graf5d009952016-03-04 01:10:04 +0100879 int i;
880
Heinrich Schuchardt23f5f4a2019-01-05 23:41:36 +0100881 /*
882 * ram_top is just outside mapped memory. So use an offset of one for
883 * mapping the sandbox address.
884 */
885 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
886
Alexander Graf7b78d642018-11-30 21:24:56 +0100887 /* Fix for 32bit targets with ram_top at 4G */
888 if (!ram_top)
889 ram_top = 0x100000000ULL;
890
Alexander Graf5d009952016-03-04 01:10:04 +0100891 /* Add RAM */
892 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000893 u64 ram_end, ram_start;
Alexander Graf5d009952016-03-04 01:10:04 +0100894
Heinrich Schuchardt49759742018-11-18 17:58:46 +0100895 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
Heinrich Schuchardt108bdff2018-11-12 18:55:24 +0100896 ram_end = ram_start + gd->bd->bi_dram[i].size;
897
Park, Aidenb5b9eff2019-09-03 17:43:43 +0000898 efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
Alexander Graf5d009952016-03-04 01:10:04 +0100899 }
York Sun42633742017-03-06 09:02:27 -0800900}
901
Heinrich Schuchardt0763c022023-01-08 01:00:00 +0100902/**
903 * add_u_boot_and_runtime() - add U-Boot code to memory map
904 *
905 * Add memory regions for U-Boot's memory and for the runtime services code.
906 */
Simon Glass69259b82018-06-18 17:23:12 +0200907static void add_u_boot_and_runtime(void)
York Sun42633742017-03-06 09:02:27 -0800908{
909 unsigned long runtime_start, runtime_end, runtime_pages;
Alexander Graf7a82c302018-09-17 13:54:33 +0200910 unsigned long runtime_mask = EFI_PAGE_MASK;
York Sun42633742017-03-06 09:02:27 -0800911 unsigned long uboot_start, uboot_pages;
Heinrich Schuchardt74b869b2020-07-29 12:37:35 +0200912 unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
York Sun42633742017-03-06 09:02:27 -0800913
Alexander Graf5d009952016-03-04 01:10:04 +0100914 /* Add U-Boot */
Heinrich Schuchardt7264e212019-11-08 20:42:53 +0100915 uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
916 uboot_stack_size) & ~EFI_PAGE_MASK;
917 uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
918 uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Heinrich Schuchardt1a127962022-11-29 16:00:41 +0100919 efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE,
Michael Walle714497e2020-05-17 12:29:19 +0200920 false);
Alexander Graf5d009952016-03-04 01:10:04 +0100921
Alexander Graf7a82c302018-09-17 13:54:33 +0200922#if defined(__aarch64__)
923 /*
924 * Runtime Services must be 64KiB aligned according to the
925 * "AArch64 Platforms" section in the UEFI spec (2.7+).
926 */
927
928 runtime_mask = SZ_64K - 1;
929#endif
930
931 /*
932 * Add Runtime Services. We mark surrounding boottime code as runtime as
933 * well to fulfill the runtime alignment constraints but avoid padding.
934 */
935 runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
Alexander Graf5d009952016-03-04 01:10:04 +0100936 runtime_end = (ulong)&__efi_runtime_stop;
Alexander Graf7a82c302018-09-17 13:54:33 +0200937 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
Alexander Graf5d009952016-03-04 01:10:04 +0100938 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
Michael Walle714497e2020-05-17 12:29:19 +0200939 efi_add_memory_map_pg(runtime_start, runtime_pages,
940 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass69259b82018-06-18 17:23:12 +0200941}
942
943int efi_memory_init(void)
944{
945 efi_add_known_memory();
946
Heinrich Schuchardt7264e212019-11-08 20:42:53 +0100947 add_u_boot_and_runtime();
Alexander Graf5d009952016-03-04 01:10:04 +0100948
Alexander Graf51735ae2016-05-11 18:25:48 +0200949#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
950 /* Request a 32bit 64MB bounce buffer region */
951 uint64_t efi_bounce_buffer_addr = 0xffffffff;
952
Heinrich Schuchardt1a127962022-11-29 16:00:41 +0100953 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA,
Alexander Graf51735ae2016-05-11 18:25:48 +0200954 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
955 &efi_bounce_buffer_addr) != EFI_SUCCESS)
956 return -1;
957
958 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
959#endif
960
Alexander Graf5d009952016-03-04 01:10:04 +0100961 return 0;
962}