blob: d2f5d563f2a0e63da38c25e99de2ce7c1fbc57d7 [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>
Sughosh Ganufa0ddab2024-10-15 21:07:06 +053012#include <lmb.h>
Heinrich Schuchardt45640252023-03-19 09:20:22 +010013#include <log.h>
Alexander Graf8623f922016-03-04 01:10:04 +010014#include <malloc.h>
Alexander Grafcd405172018-06-18 17:23:15 +020015#include <mapmem.h>
Simon Glass3e852172018-03-08 21:53:27 +010016#include <watchdog.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Heinrich Schuchardt211ff082024-04-05 10:12:57 +020019#include <asm/sections.h>
Alexander Grafde2a13b2016-03-30 16:38:29 +020020#include <linux/list_sort.h>
Alexander Graf15937a22018-09-17 13:54:33 +020021#include <linux/sizes.h>
Alexander Graf8623f922016-03-04 01:10:04 +010022
23DECLARE_GLOBAL_DATA_PTR;
24
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010025/* Magic number identifying memory allocated from pool */
26#define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
27
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +020028efi_uintn_t efi_memory_map_key;
29
Alexander Graf8623f922016-03-04 01:10:04 +010030struct efi_mem_list {
31 struct list_head link;
32 struct efi_mem_desc desc;
33};
34
Alexander Graf66d86232016-05-27 12:25:03 +020035#define EFI_CARVE_NO_OVERLAP -1
36#define EFI_CARVE_LOOP_AGAIN -2
37#define EFI_CARVE_OVERLAPS_NONRAM -3
Heinrich Schuchardt17fac0e2023-07-30 12:27:03 +020038#define EFI_CARVE_OUT_OF_RESOURCES -4
Alexander Graf66d86232016-05-27 12:25:03 +020039
Alexander Graf8623f922016-03-04 01:10:04 +010040/* This list contains all memory map items */
Bin Mengc8025a32023-04-05 20:15:18 +080041static LIST_HEAD(efi_mem);
Alexander Graf8623f922016-03-04 01:10:04 +010042
Alexander Graf7c00a3c2016-05-11 18:25:48 +020043#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
44void *efi_bounce_buffer;
45#endif
46
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010047/**
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020048 * struct efi_pool_allocation - memory block allocated from pool
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010049 *
50 * @num_pages: number of pages allocated
51 * @checksum: checksum
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020052 * @data: allocated pool memory
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010053 *
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020054 * U-Boot services each UEFI AllocatePool() request as a separate
55 * (multiple) page allocation. We have to track the number of pages
Stefan Brüns67b67d92016-10-09 22:17:26 +020056 * to be able to free the correct amount later.
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020057 *
58 * The checksum calculated in function checksum() is used in FreePool() to avoid
59 * freeing memory not allocated by AllocatePool() and duplicate freeing.
60 *
Stefan Brüns67b67d92016-10-09 22:17:26 +020061 * EFI requires 8 byte alignment for pool allocations, so we can
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020062 * prepend each allocation with these header fields.
Stefan Brüns67b67d92016-10-09 22:17:26 +020063 */
64struct efi_pool_allocation {
65 u64 num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010066 u64 checksum;
Rob Clarke597fd42017-09-13 18:05:36 -040067 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns67b67d92016-10-09 22:17:26 +020068};
69
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010070/**
71 * checksum() - calculate checksum for memory allocated from pool
72 *
73 * @alloc: allocation header
74 * Return: checksum, always non-zero
75 */
76static u64 checksum(struct efi_pool_allocation *alloc)
77{
78 u64 addr = (uintptr_t)alloc;
79 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
80 EFI_ALLOC_POOL_MAGIC;
81 if (!ret)
82 ++ret;
83 return ret;
84}
85
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +010086/**
87 * efi_mem_cmp() - comparator function for sorting memory map
88 *
Alexander Grafde2a13b2016-03-30 16:38:29 +020089 * Sorts the memory list from highest address to lowest address
90 *
91 * When allocating memory we should always start from the highest
92 * address chunk, so sort the memory list such that the first list
93 * iterator gets the highest address and goes lower from there.
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +010094 *
95 * @priv: unused
96 * @a: first memory area
97 * @b: second memory area
98 * Return: 1 if @a is before @b, -1 if @b is before @a, 0 if equal
Alexander Grafde2a13b2016-03-30 16:38:29 +020099 */
100static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
101{
102 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
103 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
104
105 if (mema->desc.physical_start == memb->desc.physical_start)
106 return 0;
107 else if (mema->desc.physical_start < memb->desc.physical_start)
108 return 1;
109 else
110 return -1;
111}
112
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100113/**
114 * desc_get_end() - get end address of memory area
115 *
116 * @desc: memory descriptor
117 * Return: end address + 1
118 */
Alexander Grafd7288142018-09-16 17:05:29 +0200119static uint64_t desc_get_end(struct efi_mem_desc *desc)
120{
121 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
122}
123
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100124/**
125 * efi_mem_sort() - sort memory map
126 *
127 * Sort the memory map and then try to merge adjacent memory areas.
128 */
Alexander Grafde2a13b2016-03-30 16:38:29 +0200129static void efi_mem_sort(void)
130{
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530131 struct efi_mem_list *lmem;
Alexander Grafd7288142018-09-16 17:05:29 +0200132 struct efi_mem_list *prevmem = NULL;
133 bool merge_again = true;
134
Alexander Grafde2a13b2016-03-30 16:38:29 +0200135 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Grafd7288142018-09-16 17:05:29 +0200136
137 /* Now merge entries that can be merged */
138 while (merge_again) {
139 merge_again = false;
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530140 list_for_each_entry(lmem, &efi_mem, link) {
Sughosh Ganu7d2c2792024-07-30 16:41:30 +0530141 struct efi_mem_desc *prev;
Alexander Grafd7288142018-09-16 17:05:29 +0200142 struct efi_mem_desc *cur;
143 uint64_t pages;
144
Alexander Grafd7288142018-09-16 17:05:29 +0200145 if (!prevmem) {
146 prevmem = lmem;
147 continue;
148 }
149
150 cur = &lmem->desc;
Sughosh Ganu7d2c2792024-07-30 16:41:30 +0530151 prev = &prevmem->desc;
Alexander Grafd7288142018-09-16 17:05:29 +0200152
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 Grafde2a13b2016-03-30 16:38:29 +0200171}
172
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100173/**
174 * efi_mem_carve_out() - unmap memory region
Alexander Graf8623f922016-03-04 01:10:04 +0100175 *
Sughosh Ganuc9e6eb62024-10-15 21:07:16 +0530176 * @map: memory map
177 * @carve_desc: memory region to unmap
178 * @overlap_conventional: the carved out region may only overlap free,
179 * or conventional memory
180 * Return: the number of overlapping pages which have been
181 * removed from the map,
182 * EFI_CARVE_NO_OVERLAP, if the regions don't
183 * overlap, EFI_CARVE_OVERLAPS_NONRAM, if the carve
184 * and map overlap, and the map contains anything
185 * but free ram(only when overlap_conventional is
186 * true),
187 * EFI_CARVE_LOOP_AGAIN, if the mapping list should
188 * be traversed again, as it has been altered.
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200189 *
190 * Unmaps all memory occupied by the carve_desc region from the list entry
191 * pointed to by map.
Stefan Brüns1b00dc12016-10-01 23:32:23 +0200192 *
193 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200194 * to re-add the already carved out pages to the mapping.
Alexander Graf8623f922016-03-04 01:10:04 +0100195 */
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200196static s64 efi_mem_carve_out(struct efi_mem_list *map,
Alexander Graf8623f922016-03-04 01:10:04 +0100197 struct efi_mem_desc *carve_desc,
Sughosh Ganuc9e6eb62024-10-15 21:07:16 +0530198 bool overlap_conventional)
Alexander Graf8623f922016-03-04 01:10:04 +0100199{
200 struct efi_mem_list *newmap;
201 struct efi_mem_desc *map_desc = &map->desc;
202 uint64_t map_start = map_desc->physical_start;
203 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
204 uint64_t carve_start = carve_desc->physical_start;
205 uint64_t carve_end = carve_start +
206 (carve_desc->num_pages << EFI_PAGE_SHIFT);
207
208 /* check whether we're overlapping */
209 if ((carve_end <= map_start) || (carve_start >= map_end))
Alexander Graf66d86232016-05-27 12:25:03 +0200210 return EFI_CARVE_NO_OVERLAP;
Alexander Graf8623f922016-03-04 01:10:04 +0100211
212 /* We're overlapping with non-RAM, warn the caller if desired */
Sughosh Ganuc9e6eb62024-10-15 21:07:16 +0530213 if (overlap_conventional && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
Alexander Graf66d86232016-05-27 12:25:03 +0200214 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf8623f922016-03-04 01:10:04 +0100215
216 /* Sanitize carve_start and carve_end to lie within our bounds */
217 carve_start = max(carve_start, map_start);
218 carve_end = min(carve_end, map_end);
219
220 /* Carving at the beginning of our map? Just move it! */
221 if (carve_start == map_start) {
222 if (map_end == carve_end) {
223 /* Full overlap, just remove map */
224 list_del(&map->link);
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200225 free(map);
226 } else {
227 map->desc.physical_start = carve_end;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200228 map->desc.virtual_start = carve_end;
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200229 map->desc.num_pages = (map_end - carve_end)
230 >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100231 }
232
Alexander Graf66d86232016-05-27 12:25:03 +0200233 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100234 }
235
236 /*
237 * Overlapping maps, just split the list map at carve_start,
238 * it will get moved or removed in the next iteration.
239 *
240 * [ map_desc |__carve_start__| newmap ]
241 */
242
243 /* Create a new map from [ carve_start ... map_end ] */
244 newmap = calloc(1, sizeof(*newmap));
Heinrich Schuchardt17fac0e2023-07-30 12:27:03 +0200245 if (!newmap)
246 return EFI_CARVE_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100247 newmap->desc = map->desc;
248 newmap->desc.physical_start = carve_start;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200249 newmap->desc.virtual_start = carve_start;
Alexander Graf8623f922016-03-04 01:10:04 +0100250 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
Stefan Brüns9d656512016-10-01 23:32:28 +0200251 /* Insert before current entry (descending address order) */
252 list_add_tail(&newmap->link, &map->link);
Alexander Graf8623f922016-03-04 01:10:04 +0100253
254 /* Shrink the map to [ map_start ... carve_start ] */
255 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
256
Alexander Graf66d86232016-05-27 12:25:03 +0200257 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf8623f922016-03-04 01:10:04 +0100258}
259
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100260/**
Michael Walle282d3862020-05-17 12:29:19 +0200261 * efi_add_memory_map_pg() - add pages to the memory map
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100262 *
Sughosh Ganuc9e6eb62024-10-15 21:07:16 +0530263 * @start: start address, must be a multiple of
264 * EFI_PAGE_SIZE
265 * @pages: number of pages to add
266 * @memory_type: type of memory added
267 * @overlap_conventional: region may only overlap free(conventional)
268 * memory
269 * Return: status code
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100270 */
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530271efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
Sughosh Ganuc9e6eb62024-10-15 21:07:16 +0530272 int memory_type,
273 bool overlap_conventional)
Alexander Graf8623f922016-03-04 01:10:04 +0100274{
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530275 struct efi_mem_list *lmem;
Alexander Graf8623f922016-03-04 01:10:04 +0100276 struct efi_mem_list *newlist;
Alexander Graf66d86232016-05-27 12:25:03 +0200277 bool carve_again;
278 uint64_t carved_pages = 0;
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200279 struct efi_event *evt;
Alexander Graf8623f922016-03-04 01:10:04 +0100280
Heinrich Schuchardt706a2fe2019-04-04 21:50:02 +0200281 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
Sughosh Ganuc9e6eb62024-10-15 21:07:16 +0530282 start, pages, memory_type, overlap_conventional ?
283 "yes" : "no");
Andreas Färber102f8bd2016-07-17 06:57:11 +0200284
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200285 if (memory_type >= EFI_MAX_MEMORY_TYPE)
286 return EFI_INVALID_PARAMETER;
287
Alexander Graf8623f922016-03-04 01:10:04 +0100288 if (!pages)
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100289 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100290
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200291 ++efi_memory_map_key;
Alexander Graf8623f922016-03-04 01:10:04 +0100292 newlist = calloc(1, sizeof(*newlist));
Heinrich Schuchardt71c24442023-07-30 12:36:17 +0200293 if (!newlist)
294 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100295 newlist->desc.type = memory_type;
296 newlist->desc.physical_start = start;
297 newlist->desc.virtual_start = start;
298 newlist->desc.num_pages = pages;
299
300 switch (memory_type) {
301 case EFI_RUNTIME_SERVICES_CODE:
302 case EFI_RUNTIME_SERVICES_DATA:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200303 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100304 break;
305 case EFI_MMAP_IO:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200306 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100307 break;
308 default:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200309 newlist->desc.attribute = EFI_MEMORY_WB;
Alexander Graf8623f922016-03-04 01:10:04 +0100310 break;
311 }
312
313 /* Add our new map */
314 do {
Alexander Graf66d86232016-05-27 12:25:03 +0200315 carve_again = false;
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530316 list_for_each_entry(lmem, &efi_mem, link) {
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200317 s64 r;
Alexander Graf8623f922016-03-04 01:10:04 +0100318
Alexander Graf8623f922016-03-04 01:10:04 +0100319 r = efi_mem_carve_out(lmem, &newlist->desc,
Sughosh Ganuc9e6eb62024-10-15 21:07:16 +0530320 overlap_conventional);
Alexander Graf66d86232016-05-27 12:25:03 +0200321 switch (r) {
Heinrich Schuchardt17fac0e2023-07-30 12:27:03 +0200322 case EFI_CARVE_OUT_OF_RESOURCES:
323 free(newlist);
324 return EFI_OUT_OF_RESOURCES;
Alexander Graf66d86232016-05-27 12:25:03 +0200325 case EFI_CARVE_OVERLAPS_NONRAM:
326 /*
327 * The user requested to only have RAM overlaps,
328 * but we hit a non-RAM region. Error out.
329 */
Heinrich Schuchardt9f0d3ad2023-07-30 12:59:32 +0200330 free(newlist);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100331 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200332 case EFI_CARVE_NO_OVERLAP:
333 /* Just ignore this list entry */
334 break;
335 case EFI_CARVE_LOOP_AGAIN:
336 /*
337 * We split an entry, but need to loop through
338 * the list again to actually carve it.
339 */
340 carve_again = true;
341 break;
342 default:
343 /* We carved a number of pages */
344 carved_pages += r;
345 carve_again = true;
Alexander Graf8623f922016-03-04 01:10:04 +0100346 break;
347 }
Alexander Graf66d86232016-05-27 12:25:03 +0200348
349 if (carve_again) {
350 /* The list changed, we need to start over */
351 break;
352 }
Alexander Graf8623f922016-03-04 01:10:04 +0100353 }
Alexander Graf66d86232016-05-27 12:25:03 +0200354 } while (carve_again);
355
Sughosh Ganuc9e6eb62024-10-15 21:07:16 +0530356 if (overlap_conventional && (carved_pages != pages)) {
Alexander Graf66d86232016-05-27 12:25:03 +0200357 /*
358 * The payload wanted to have RAM overlaps, but we overlapped
359 * with an unallocated region. Error out.
360 */
Heinrich Schuchardt9f0d3ad2023-07-30 12:59:32 +0200361 free(newlist);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100362 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200363 }
Alexander Graf8623f922016-03-04 01:10:04 +0100364
365 /* Add our new map */
366 list_add_tail(&newlist->link, &efi_mem);
367
Alexander Grafde2a13b2016-03-30 16:38:29 +0200368 /* And make sure memory is listed in descending order */
369 efi_mem_sort();
370
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200371 /* Notify that the memory map was changed */
372 list_for_each_entry(evt, &efi_events, link) {
373 if (evt->group &&
374 !guidcmp(evt->group,
375 &efi_guid_event_group_memory_map_change)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200376 efi_signal_event(evt);
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200377 break;
378 }
379 }
380
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100381 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100382}
383
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200384/**
Michael Walle282d3862020-05-17 12:29:19 +0200385 * efi_add_memory_map() - add memory area to the memory map
386 *
387 * @start: start address of the memory area
388 * @size: length in bytes of the memory area
389 * @memory_type: type of memory added
390 *
391 * Return: status code
392 *
393 * This function automatically aligns the start and size of the memory area
394 * to EFI_PAGE_SIZE.
395 */
396efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
397{
398 u64 pages;
399
400 pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
401 start &= ~EFI_PAGE_MASK;
402
403 return efi_add_memory_map_pg(start, pages, memory_type, false);
404}
405
406/**
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200407 * efi_check_allocated() - validate address to be freed
408 *
409 * Check that the address is within allocated memory:
410 *
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200411 * * The address must be in a range of the memory map.
412 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
413 *
414 * Page alignment is not checked as this is not a requirement of
415 * efi_free_pool().
416 *
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200417 * @addr: address of page to be freed
418 * @must_be_allocated: return success if the page is allocated
419 * Return: status code
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200420 */
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200421static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200422{
423 struct efi_mem_list *item;
424
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200425 list_for_each_entry(item, &efi_mem, link) {
426 u64 start = item->desc.physical_start;
427 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
428
429 if (addr >= start && addr < end) {
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200430 if (must_be_allocated ^
431 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200432 return EFI_SUCCESS;
433 else
434 return EFI_NOT_FOUND;
435 }
436 }
437
438 return EFI_NOT_FOUND;
439}
440
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100441/**
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100442 * efi_allocate_pages - allocate memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100443 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100444 * @type: type of allocation to be performed
445 * @memory_type: usage type of the allocated memory
446 * @pages: number of pages to be allocated
447 * @memory: allocated memory
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100448 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100449 */
Heinrich Schuchardt98228942021-08-17 15:05:31 +0200450efi_status_t efi_allocate_pages(enum efi_allocate_type type,
451 enum efi_memory_type memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100452 efi_uintn_t pages, uint64_t *memory)
Alexander Graf8623f922016-03-04 01:10:04 +0100453{
Heinrich Schuchardtf0c16f92023-07-30 11:31:08 +0200454 u64 len;
Sughosh Ganufa0ddab2024-10-15 21:07:06 +0530455 uint flags;
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200456 efi_status_t ret;
Sughosh Ganufa0ddab2024-10-15 21:07:06 +0530457 phys_addr_t addr;
Alexander Graf8623f922016-03-04 01:10:04 +0100458
Heinrich Schuchardtf7272332019-04-23 00:30:53 +0200459 /* Check import parameters */
460 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
461 memory_type <= 0x6FFFFFFF)
462 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200463 if (!memory)
464 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtf0c16f92023-07-30 11:31:08 +0200465 len = (u64)pages << EFI_PAGE_SHIFT;
466 /* Catch possible overflow on 64bit systems */
467 if (sizeof(efi_uintn_t) == sizeof(u64) &&
468 (len >> EFI_PAGE_SHIFT) != (u64)pages)
469 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200470
Sughosh Ganufa0ddab2024-10-15 21:07:06 +0530471 flags = LMB_NOOVERWRITE | LMB_NONOTIFY;
Alexander Graf8623f922016-03-04 01:10:04 +0100472 switch (type) {
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100473 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf8623f922016-03-04 01:10:04 +0100474 /* Any page */
Ilias Apalodimas7d182fb2024-10-23 18:26:36 +0300475 addr = (u64)lmb_alloc_base_flags(len, EFI_PAGE_SIZE,
476 LMB_ALLOC_ANYWHERE, flags);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200477 if (!addr)
478 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100479 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100480 case EFI_ALLOCATE_MAX_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100481 /* Max address */
Sughosh Ganufa0ddab2024-10-15 21:07:06 +0530482 addr = map_to_sysmem((void *)(uintptr_t)*memory);
483 addr = (u64)lmb_alloc_base_flags(len, EFI_PAGE_SIZE, addr,
484 flags);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200485 if (!addr)
486 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100487 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100488 case EFI_ALLOCATE_ADDRESS:
Heinrich Schuchardt1c180b82022-11-06 01:52:13 +0100489 if (*memory & EFI_PAGE_MASK)
490 return EFI_NOT_FOUND;
Sughosh Ganufa0ddab2024-10-15 21:07:06 +0530491
492 addr = map_to_sysmem((void *)(uintptr_t)*memory);
493 addr = (u64)lmb_alloc_addr_flags(addr, len, flags);
494 if (!addr)
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200495 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100496 break;
497 default:
498 /* UEFI doesn't specify other allocation types */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200499 return EFI_INVALID_PARAMETER;
Alexander Graf8623f922016-03-04 01:10:04 +0100500 }
501
Sughosh Ganufa0ddab2024-10-15 21:07:06 +0530502 addr = (u64)(uintptr_t)map_sysmem(addr, 0);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200503 /* Reserve that map in our memory maps */
Michael Walle282d3862020-05-17 12:29:19 +0200504 ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
505 if (ret != EFI_SUCCESS)
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200506 /* Map would overlap, bail out */
507 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100508
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200509 *memory = addr;
Alexander Graf8623f922016-03-04 01:10:04 +0100510
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200511 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100512}
513
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100514/**
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100515 * efi_free_pages() - free memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100516 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100517 * @memory: start of the memory area to be freed
518 * @pages: number of pages to be freed
519 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100520 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100521efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf8623f922016-03-04 01:10:04 +0100522{
Sughosh Ganufa0ddab2024-10-15 21:07:06 +0530523 u64 len;
Sughosh Ganufa0ddab2024-10-15 21:07:06 +0530524 long status;
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200525 efi_status_t ret;
526
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200527 ret = efi_check_allocated(memory, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200528 if (ret != EFI_SUCCESS)
529 return ret;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200530
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100531 /* Sanity check */
Heinrich Schuchardt7bf4bc92019-04-25 18:41:40 +0200532 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100533 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
534 memory, pages);
535 return EFI_INVALID_PARAMETER;
536 }
537
Sughosh Ganufa0ddab2024-10-15 21:07:06 +0530538 len = (u64)pages << EFI_PAGE_SHIFT;
Ilias Apalodimase9e55c72024-10-24 14:01:55 +0300539 /*
540 * The 'memory' variable for sandbox holds a pointer which has already
541 * been mapped with map_sysmem() from efi_allocate_pages(). Convert
542 * it back to an address LMB understands
543 */
Sughosh Ganufa0ddab2024-10-15 21:07:06 +0530544 status = lmb_free_flags(map_to_sysmem((void *)(uintptr_t)memory), len,
Ilias Apalodimase9e55c72024-10-24 14:01:55 +0300545 LMB_NOOVERWRITE);
Sughosh Ganufa0ddab2024-10-15 21:07:06 +0530546 if (status)
547 return EFI_NOT_FOUND;
548
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100549 return ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100550}
551
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100552/**
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100553 * efi_alloc_aligned_pages() - allocate aligned memory pages
Ilias Apalodimas6e01c232021-10-11 15:10:23 +0300554 *
555 * @len: len in bytes
556 * @memory_type: usage type of the allocated memory
557 * @align: alignment in bytes
558 * Return: aligned memory or NULL
559 */
560void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
561{
562 u64 req_pages = efi_size_in_pages(len);
563 u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
564 u64 free_pages;
565 u64 aligned_mem;
566 efi_status_t r;
567 u64 mem;
568
569 /* align must be zero or a power of two */
570 if (align & (align - 1))
571 return NULL;
572
573 /* Check for overflow */
574 if (true_pages < req_pages)
575 return NULL;
576
577 if (align < EFI_PAGE_SIZE) {
578 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
579 req_pages, &mem);
580 return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL;
581 }
582
583 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
584 true_pages, &mem);
585 if (r != EFI_SUCCESS)
586 return NULL;
587
588 aligned_mem = ALIGN(mem, align);
589 /* Free pages before alignment */
590 free_pages = efi_size_in_pages(aligned_mem - mem);
591 if (free_pages)
592 efi_free_pages(mem, free_pages);
593
594 /* Free trailing pages */
595 free_pages = true_pages - (req_pages + free_pages);
596 if (free_pages) {
597 mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
598 efi_free_pages(mem, free_pages);
599 }
600
601 return (void *)(uintptr_t)aligned_mem;
602}
603
604/**
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100605 * efi_allocate_pool - allocate memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100606 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100607 * @pool_type: type of the pool from which memory is to be allocated
608 * @size: number of bytes to be allocated
609 * @buffer: allocated memory
610 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100611 */
Heinrich Schuchardt98228942021-08-17 15:05:31 +0200612efi_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 +0200613{
614 efi_status_t r;
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100615 u64 addr;
Alexander Grafcd405172018-06-18 17:23:15 +0200616 struct efi_pool_allocation *alloc;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100617 u64 num_pages = efi_size_in_pages(size +
618 sizeof(struct efi_pool_allocation));
Stefan Brüns67b67d92016-10-09 22:17:26 +0200619
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200620 if (!buffer)
621 return EFI_INVALID_PARAMETER;
622
Stefan Brüns67b67d92016-10-09 22:17:26 +0200623 if (size == 0) {
624 *buffer = NULL;
625 return EFI_SUCCESS;
626 }
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200627
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200628 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100629 &addr);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200630 if (r == EFI_SUCCESS) {
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100631 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200632 alloc->num_pages = num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100633 alloc->checksum = checksum(alloc);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200634 *buffer = alloc->data;
635 }
636
637 return r;
638}
639
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100640/**
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100641 * efi_alloc() - allocate boot services data pool memory
642 *
643 * Allocate memory from pool and zero it out.
644 *
645 * @size: number of bytes to allocate
646 * Return: pointer to allocated memory or NULL
647 */
648void *efi_alloc(size_t size)
649{
650 void *buf;
651
652 if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, size, &buf) !=
653 EFI_SUCCESS) {
Heinrich Schuchardtf39486a2024-10-17 20:05:07 +0200654 log_err("out of memory\n");
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100655 return NULL;
656 }
657 memset(buf, 0, size);
658
659 return buf;
660}
661
662/**
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100663 * efi_free_pool() - free memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100664 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100665 * @buffer: start of memory to be freed
666 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100667 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200668efi_status_t efi_free_pool(void *buffer)
669{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200670 efi_status_t ret;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200671 struct efi_pool_allocation *alloc;
672
Heinrich Schuchardt56b587f72019-06-12 07:17:04 +0200673 if (!buffer)
674 return EFI_INVALID_PARAMETER;
675
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200676 ret = efi_check_allocated((uintptr_t)buffer, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200677 if (ret != EFI_SUCCESS)
678 return ret;
xypron.glpk@gmx.de34ddae72017-07-14 19:12:39 +0200679
Stefan Brüns67b67d92016-10-09 22:17:26 +0200680 alloc = container_of(buffer, struct efi_pool_allocation, data);
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100681
682 /* Check that this memory was allocated by efi_allocate_pool() */
683 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
684 alloc->checksum != checksum(alloc)) {
685 printf("%s: illegal free 0x%p\n", __func__, buffer);
686 return EFI_INVALID_PARAMETER;
687 }
688 /* Avoid double free */
689 alloc->checksum = 0;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200690
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200691 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200692
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200693 return ret;
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200694}
695
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100696/**
697 * efi_get_memory_map() - get map describing memory usage.
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100698 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100699 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100700 * on exit the size of the copied memory map
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100701 * @memory_map: buffer to which the memory map is written
702 * @map_key: key for the memory map
703 * @descriptor_size: size of an individual memory descriptor
704 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100705 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100706 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100707efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
708 struct efi_mem_desc *memory_map,
709 efi_uintn_t *map_key,
710 efi_uintn_t *descriptor_size,
711 uint32_t *descriptor_version)
Alexander Graf8623f922016-03-04 01:10:04 +0100712{
Sughosh Ganu38dcdb82024-07-30 16:41:29 +0530713 size_t map_entries;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100714 efi_uintn_t map_size = 0;
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530715 struct efi_mem_list *lmem;
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200716 efi_uintn_t provided_map_size;
Alexander Graf8623f922016-03-04 01:10:04 +0100717
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200718 if (!memory_map_size)
719 return EFI_INVALID_PARAMETER;
720
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200721 provided_map_size = *memory_map_size;
722
Sughosh Ganu38dcdb82024-07-30 16:41:29 +0530723 map_entries = list_count_nodes(&efi_mem);
Alexander Graf9b591022016-04-11 23:51:02 +0200724
725 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf8623f922016-03-04 01:10:04 +0100726
Rob Clarka84c9742017-07-26 14:34:05 -0400727 *memory_map_size = map_size;
728
Alexander Graf8623f922016-03-04 01:10:04 +0100729 if (descriptor_size)
730 *descriptor_size = sizeof(struct efi_mem_desc);
731
Mian Yousaf Kaukab338cf562016-09-05 23:59:22 +0200732 if (descriptor_version)
733 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
734
AKASHI Takahiro3ee138d2020-03-11 15:18:18 +0900735 if (provided_map_size < map_size)
736 return EFI_BUFFER_TOO_SMALL;
737
738 if (!memory_map)
739 return EFI_INVALID_PARAMETER;
740
Alexander Graf8623f922016-03-04 01:10:04 +0100741 /* Copy list into array */
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200742 /* Return the list in ascending order */
743 memory_map = &memory_map[map_entries - 1];
Sughosh Ganu21ad4cc2024-07-30 16:41:31 +0530744 list_for_each_entry(lmem, &efi_mem, link) {
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200745 *memory_map = lmem->desc;
746 memory_map--;
Alexander Graf8623f922016-03-04 01:10:04 +0100747 }
748
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200749 if (map_key)
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200750 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dee436e632017-07-21 19:05:44 +0200751
Alexander Graf8623f922016-03-04 01:10:04 +0100752 return EFI_SUCCESS;
753}
754
Park, Aidene009e7a2019-09-03 17:43:43 +0000755/**
Heinrich Schuchardt13ce2d92023-01-05 18:26:01 +0100756 * efi_get_memory_map_alloc() - allocate map describing memory usage
757 *
758 * The caller is responsible for calling FreePool() if the call succeeds.
759 *
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100760 * @map_size: size of the memory map
761 * @memory_map: buffer to which the memory map is written
Heinrich Schuchardt13ce2d92023-01-05 18:26:01 +0100762 * Return: status code
763 */
764efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size,
765 struct efi_mem_desc **memory_map)
766{
767 efi_status_t ret;
768
769 *memory_map = NULL;
770 *map_size = 0;
771 ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL);
772 if (ret == EFI_BUFFER_TOO_SMALL) {
773 *map_size += sizeof(struct efi_mem_desc); /* for the map */
774 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size,
775 (void **)memory_map);
776 if (ret != EFI_SUCCESS)
777 return ret;
778 ret = efi_get_memory_map(map_size, *memory_map,
779 NULL, NULL, NULL);
780 if (ret != EFI_SUCCESS) {
781 efi_free_pool(*memory_map);
782 *memory_map = NULL;
783 }
784 }
785
786 return ret;
787}
788
789/**
Sughosh Ganufbb00c22024-10-15 21:07:14 +0530790 * efi_add_known_memory() - add memory types to the EFI memory map
Park, Aidene009e7a2019-09-03 17:43:43 +0000791 *
Sughosh Ganufbb00c22024-10-15 21:07:14 +0530792 * This function is to be used to add different memory types other
793 * than EFI_CONVENTIONAL_MEMORY to the EFI memory map. The conventional
794 * memory is handled by the LMB module and gets added to the memory
795 * map through the LMB module.
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100796 *
Sughosh Ganufbb00c22024-10-15 21:07:14 +0530797 * This function may be overridden for architectures specific purposes.
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100798 */
York Suna8e9f0a2017-03-06 09:02:27 -0800799__weak void efi_add_known_memory(void)
Alexander Graf8623f922016-03-04 01:10:04 +0100800{
York Suna8e9f0a2017-03-06 09:02:27 -0800801}
802
Heinrich Schuchardtb9b838d2023-01-08 01:00:00 +0100803/**
804 * add_u_boot_and_runtime() - add U-Boot code to memory map
805 *
806 * Add memory regions for U-Boot's memory and for the runtime services code.
807 */
Simon Glass78639432018-06-18 17:23:12 +0200808static void add_u_boot_and_runtime(void)
York Suna8e9f0a2017-03-06 09:02:27 -0800809{
810 unsigned long runtime_start, runtime_end, runtime_pages;
Alexander Graf15937a22018-09-17 13:54:33 +0200811 unsigned long runtime_mask = EFI_PAGE_MASK;
Alexander Graf8623f922016-03-04 01:10:04 +0100812
Alexander Graf15937a22018-09-17 13:54:33 +0200813#if defined(__aarch64__)
814 /*
815 * Runtime Services must be 64KiB aligned according to the
816 * "AArch64 Platforms" section in the UEFI spec (2.7+).
817 */
818
819 runtime_mask = SZ_64K - 1;
820#endif
821
822 /*
823 * Add Runtime Services. We mark surrounding boottime code as runtime as
824 * well to fulfill the runtime alignment constraints but avoid padding.
825 */
Ilias Apalodimas2af95532024-04-04 09:35:35 +0300826 runtime_start = (uintptr_t)__efi_runtime_start & ~runtime_mask;
827 runtime_end = (uintptr_t)__efi_runtime_stop;
Alexander Graf15937a22018-09-17 13:54:33 +0200828 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100829 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
Michael Walle282d3862020-05-17 12:29:19 +0200830 efi_add_memory_map_pg(runtime_start, runtime_pages,
831 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass78639432018-06-18 17:23:12 +0200832}
833
834int efi_memory_init(void)
835{
836 efi_add_known_memory();
837
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100838 add_u_boot_and_runtime();
Alexander Graf8623f922016-03-04 01:10:04 +0100839
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200840#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
841 /* Request a 32bit 64MB bounce buffer region */
842 uint64_t efi_bounce_buffer_addr = 0xffffffff;
843
Heinrich Schuchardt296b55c2022-11-29 16:00:41 +0100844 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200845 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
846 &efi_bounce_buffer_addr) != EFI_SUCCESS)
847 return -1;
848
849 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
850#endif
851
Alexander Graf8623f922016-03-04 01:10:04 +0100852 return 0;
853}