blob: c0cf1d91267dcf431a92cd2bbbf3c38bff2c57e0 [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>
Alexander Grafde2a13b2016-03-30 16:38:29 +020014#include <linux/list_sort.h>
Alexander Graf15937a22018-09-17 13:54:33 +020015#include <linux/sizes.h>
Alexander Graf8623f922016-03-04 01:10:04 +010016
17DECLARE_GLOBAL_DATA_PTR;
18
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010019/* Magic number identifying memory allocated from pool */
20#define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
21
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +020022efi_uintn_t efi_memory_map_key;
23
Alexander Graf8623f922016-03-04 01:10:04 +010024struct efi_mem_list {
25 struct list_head link;
26 struct efi_mem_desc desc;
27};
28
Alexander Graf66d86232016-05-27 12:25:03 +020029#define EFI_CARVE_NO_OVERLAP -1
30#define EFI_CARVE_LOOP_AGAIN -2
31#define EFI_CARVE_OVERLAPS_NONRAM -3
32
Alexander Graf8623f922016-03-04 01:10:04 +010033/* This list contains all memory map items */
34LIST_HEAD(efi_mem);
35
Alexander Graf7c00a3c2016-05-11 18:25:48 +020036#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
37void *efi_bounce_buffer;
38#endif
39
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010040/**
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020041 * struct efi_pool_allocation - memory block allocated from pool
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010042 *
43 * @num_pages: number of pages allocated
44 * @checksum: checksum
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020045 * @data: allocated pool memory
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010046 *
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020047 * U-Boot services each UEFI AllocatePool() request as a separate
48 * (multiple) page allocation. We have to track the number of pages
Stefan Brüns67b67d92016-10-09 22:17:26 +020049 * to be able to free the correct amount later.
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020050 *
51 * The checksum calculated in function checksum() is used in FreePool() to avoid
52 * freeing memory not allocated by AllocatePool() and duplicate freeing.
53 *
Stefan Brüns67b67d92016-10-09 22:17:26 +020054 * EFI requires 8 byte alignment for pool allocations, so we can
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020055 * prepend each allocation with these header fields.
Stefan Brüns67b67d92016-10-09 22:17:26 +020056 */
57struct efi_pool_allocation {
58 u64 num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010059 u64 checksum;
Rob Clarke597fd42017-09-13 18:05:36 -040060 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns67b67d92016-10-09 22:17:26 +020061};
62
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010063/**
64 * checksum() - calculate checksum for memory allocated from pool
65 *
66 * @alloc: allocation header
67 * Return: checksum, always non-zero
68 */
69static u64 checksum(struct efi_pool_allocation *alloc)
70{
71 u64 addr = (uintptr_t)alloc;
72 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
73 EFI_ALLOC_POOL_MAGIC;
74 if (!ret)
75 ++ret;
76 return ret;
77}
78
Stefan Brüns67b67d92016-10-09 22:17:26 +020079/*
Alexander Grafde2a13b2016-03-30 16:38:29 +020080 * Sorts the memory list from highest address to lowest address
81 *
82 * When allocating memory we should always start from the highest
83 * address chunk, so sort the memory list such that the first list
84 * iterator gets the highest address and goes lower from there.
85 */
86static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
87{
88 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
89 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
90
91 if (mema->desc.physical_start == memb->desc.physical_start)
92 return 0;
93 else if (mema->desc.physical_start < memb->desc.physical_start)
94 return 1;
95 else
96 return -1;
97}
98
Alexander Grafd7288142018-09-16 17:05:29 +020099static uint64_t desc_get_end(struct efi_mem_desc *desc)
100{
101 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
102}
103
Alexander Grafde2a13b2016-03-30 16:38:29 +0200104static void efi_mem_sort(void)
105{
Alexander Grafd7288142018-09-16 17:05:29 +0200106 struct list_head *lhandle;
107 struct efi_mem_list *prevmem = NULL;
108 bool merge_again = true;
109
Alexander Grafde2a13b2016-03-30 16:38:29 +0200110 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Grafd7288142018-09-16 17:05:29 +0200111
112 /* Now merge entries that can be merged */
113 while (merge_again) {
114 merge_again = false;
115 list_for_each(lhandle, &efi_mem) {
116 struct efi_mem_list *lmem;
117 struct efi_mem_desc *prev = &prevmem->desc;
118 struct efi_mem_desc *cur;
119 uint64_t pages;
120
121 lmem = list_entry(lhandle, struct efi_mem_list, link);
122 if (!prevmem) {
123 prevmem = lmem;
124 continue;
125 }
126
127 cur = &lmem->desc;
128
129 if ((desc_get_end(cur) == prev->physical_start) &&
130 (prev->type == cur->type) &&
131 (prev->attribute == cur->attribute)) {
132 /* There is an existing map before, reuse it */
133 pages = cur->num_pages;
134 prev->num_pages += pages;
135 prev->physical_start -= pages << EFI_PAGE_SHIFT;
136 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
137 list_del(&lmem->link);
138 free(lmem);
139
140 merge_again = true;
141 break;
142 }
143
144 prevmem = lmem;
145 }
146 }
Alexander Grafde2a13b2016-03-30 16:38:29 +0200147}
148
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200149/** efi_mem_carve_out - unmap memory region
Alexander Graf8623f922016-03-04 01:10:04 +0100150 *
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200151 * @map: memory map
152 * @carve_desc: memory region to unmap
153 * @overlap_only_ram: the carved out region may only overlap RAM
154 * Return Value: the number of overlapping pages which have been
155 * removed from the map,
156 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
157 * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
158 * and the map contains anything but free ram
159 * (only when overlap_only_ram is true),
160 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
161 * traversed again, as it has been altered.
162 *
163 * Unmaps all memory occupied by the carve_desc region from the list entry
164 * pointed to by map.
Stefan Brüns1b00dc12016-10-01 23:32:23 +0200165 *
166 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200167 * to re-add the already carved out pages to the mapping.
Alexander Graf8623f922016-03-04 01:10:04 +0100168 */
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200169static s64 efi_mem_carve_out(struct efi_mem_list *map,
Alexander Graf8623f922016-03-04 01:10:04 +0100170 struct efi_mem_desc *carve_desc,
171 bool overlap_only_ram)
172{
173 struct efi_mem_list *newmap;
174 struct efi_mem_desc *map_desc = &map->desc;
175 uint64_t map_start = map_desc->physical_start;
176 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
177 uint64_t carve_start = carve_desc->physical_start;
178 uint64_t carve_end = carve_start +
179 (carve_desc->num_pages << EFI_PAGE_SHIFT);
180
181 /* check whether we're overlapping */
182 if ((carve_end <= map_start) || (carve_start >= map_end))
Alexander Graf66d86232016-05-27 12:25:03 +0200183 return EFI_CARVE_NO_OVERLAP;
Alexander Graf8623f922016-03-04 01:10:04 +0100184
185 /* We're overlapping with non-RAM, warn the caller if desired */
186 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
Alexander Graf66d86232016-05-27 12:25:03 +0200187 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf8623f922016-03-04 01:10:04 +0100188
189 /* Sanitize carve_start and carve_end to lie within our bounds */
190 carve_start = max(carve_start, map_start);
191 carve_end = min(carve_end, map_end);
192
193 /* Carving at the beginning of our map? Just move it! */
194 if (carve_start == map_start) {
195 if (map_end == carve_end) {
196 /* Full overlap, just remove map */
197 list_del(&map->link);
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200198 free(map);
199 } else {
200 map->desc.physical_start = carve_end;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200201 map->desc.virtual_start = carve_end;
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200202 map->desc.num_pages = (map_end - carve_end)
203 >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100204 }
205
Alexander Graf66d86232016-05-27 12:25:03 +0200206 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100207 }
208
209 /*
210 * Overlapping maps, just split the list map at carve_start,
211 * it will get moved or removed in the next iteration.
212 *
213 * [ map_desc |__carve_start__| newmap ]
214 */
215
216 /* Create a new map from [ carve_start ... map_end ] */
217 newmap = calloc(1, sizeof(*newmap));
218 newmap->desc = map->desc;
219 newmap->desc.physical_start = carve_start;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200220 newmap->desc.virtual_start = carve_start;
Alexander Graf8623f922016-03-04 01:10:04 +0100221 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
Stefan Brüns9d656512016-10-01 23:32:28 +0200222 /* Insert before current entry (descending address order) */
223 list_add_tail(&newmap->link, &map->link);
Alexander Graf8623f922016-03-04 01:10:04 +0100224
225 /* Shrink the map to [ map_start ... carve_start ] */
226 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
227
Alexander Graf66d86232016-05-27 12:25:03 +0200228 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf8623f922016-03-04 01:10:04 +0100229}
230
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100231/**
Michael Walle282d3862020-05-17 12:29:19 +0200232 * efi_add_memory_map_pg() - add pages to the memory map
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100233 *
234 * @start: start address, must be a multiple of EFI_PAGE_SIZE
235 * @pages: number of pages to add
236 * @memory_type: type of memory added
237 * @overlap_only_ram: the memory area must overlap existing
238 * Return: status code
239 */
Michael Walle282d3862020-05-17 12:29:19 +0200240static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
241 int memory_type,
242 bool overlap_only_ram)
Alexander Graf8623f922016-03-04 01:10:04 +0100243{
244 struct list_head *lhandle;
245 struct efi_mem_list *newlist;
Alexander Graf66d86232016-05-27 12:25:03 +0200246 bool carve_again;
247 uint64_t carved_pages = 0;
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200248 struct efi_event *evt;
Alexander Graf8623f922016-03-04 01:10:04 +0100249
Heinrich Schuchardt706a2fe2019-04-04 21:50:02 +0200250 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
251 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
Andreas Färber102f8bd2016-07-17 06:57:11 +0200252
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200253 if (memory_type >= EFI_MAX_MEMORY_TYPE)
254 return EFI_INVALID_PARAMETER;
255
Alexander Graf8623f922016-03-04 01:10:04 +0100256 if (!pages)
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100257 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100258
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200259 ++efi_memory_map_key;
Alexander Graf8623f922016-03-04 01:10:04 +0100260 newlist = calloc(1, sizeof(*newlist));
261 newlist->desc.type = memory_type;
262 newlist->desc.physical_start = start;
263 newlist->desc.virtual_start = start;
264 newlist->desc.num_pages = pages;
265
266 switch (memory_type) {
267 case EFI_RUNTIME_SERVICES_CODE:
268 case EFI_RUNTIME_SERVICES_DATA:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200269 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100270 break;
271 case EFI_MMAP_IO:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200272 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100273 break;
274 default:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200275 newlist->desc.attribute = EFI_MEMORY_WB;
Alexander Graf8623f922016-03-04 01:10:04 +0100276 break;
277 }
278
279 /* Add our new map */
280 do {
Alexander Graf66d86232016-05-27 12:25:03 +0200281 carve_again = false;
Alexander Graf8623f922016-03-04 01:10:04 +0100282 list_for_each(lhandle, &efi_mem) {
283 struct efi_mem_list *lmem;
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200284 s64 r;
Alexander Graf8623f922016-03-04 01:10:04 +0100285
286 lmem = list_entry(lhandle, struct efi_mem_list, link);
287 r = efi_mem_carve_out(lmem, &newlist->desc,
288 overlap_only_ram);
Alexander Graf66d86232016-05-27 12:25:03 +0200289 switch (r) {
290 case EFI_CARVE_OVERLAPS_NONRAM:
291 /*
292 * The user requested to only have RAM overlaps,
293 * but we hit a non-RAM region. Error out.
294 */
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100295 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200296 case EFI_CARVE_NO_OVERLAP:
297 /* Just ignore this list entry */
298 break;
299 case EFI_CARVE_LOOP_AGAIN:
300 /*
301 * We split an entry, but need to loop through
302 * the list again to actually carve it.
303 */
304 carve_again = true;
305 break;
306 default:
307 /* We carved a number of pages */
308 carved_pages += r;
309 carve_again = true;
Alexander Graf8623f922016-03-04 01:10:04 +0100310 break;
311 }
Alexander Graf66d86232016-05-27 12:25:03 +0200312
313 if (carve_again) {
314 /* The list changed, we need to start over */
315 break;
316 }
Alexander Graf8623f922016-03-04 01:10:04 +0100317 }
Alexander Graf66d86232016-05-27 12:25:03 +0200318 } while (carve_again);
319
320 if (overlap_only_ram && (carved_pages != pages)) {
321 /*
322 * The payload wanted to have RAM overlaps, but we overlapped
323 * with an unallocated region. Error out.
324 */
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100325 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200326 }
Alexander Graf8623f922016-03-04 01:10:04 +0100327
328 /* Add our new map */
329 list_add_tail(&newlist->link, &efi_mem);
330
Alexander Grafde2a13b2016-03-30 16:38:29 +0200331 /* And make sure memory is listed in descending order */
332 efi_mem_sort();
333
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200334 /* Notify that the memory map was changed */
335 list_for_each_entry(evt, &efi_events, link) {
336 if (evt->group &&
337 !guidcmp(evt->group,
338 &efi_guid_event_group_memory_map_change)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200339 efi_signal_event(evt);
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200340 break;
341 }
342 }
343
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100344 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100345}
346
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200347/**
Michael Walle282d3862020-05-17 12:29:19 +0200348 * efi_add_memory_map() - add memory area to the memory map
349 *
350 * @start: start address of the memory area
351 * @size: length in bytes of the memory area
352 * @memory_type: type of memory added
353 *
354 * Return: status code
355 *
356 * This function automatically aligns the start and size of the memory area
357 * to EFI_PAGE_SIZE.
358 */
359efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
360{
361 u64 pages;
362
363 pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
364 start &= ~EFI_PAGE_MASK;
365
366 return efi_add_memory_map_pg(start, pages, memory_type, false);
367}
368
369/**
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200370 * efi_check_allocated() - validate address to be freed
371 *
372 * Check that the address is within allocated memory:
373 *
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200374 * * The address must be in a range of the memory map.
375 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
376 *
377 * Page alignment is not checked as this is not a requirement of
378 * efi_free_pool().
379 *
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200380 * @addr: address of page to be freed
381 * @must_be_allocated: return success if the page is allocated
382 * Return: status code
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200383 */
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200384static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200385{
386 struct efi_mem_list *item;
387
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200388 list_for_each_entry(item, &efi_mem, link) {
389 u64 start = item->desc.physical_start;
390 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
391
392 if (addr >= start && addr < end) {
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200393 if (must_be_allocated ^
394 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200395 return EFI_SUCCESS;
396 else
397 return EFI_NOT_FOUND;
398 }
399 }
400
401 return EFI_NOT_FOUND;
402}
403
Alexander Graf8623f922016-03-04 01:10:04 +0100404static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
405{
406 struct list_head *lhandle;
407
Alexander Grafc3b2a252018-11-05 00:30:46 +0100408 /*
409 * Prealign input max address, so we simplify our matching
410 * logic below and can just reuse it as return pointer.
411 */
412 max_addr &= ~EFI_PAGE_MASK;
413
Alexander Graf8623f922016-03-04 01:10:04 +0100414 list_for_each(lhandle, &efi_mem) {
415 struct efi_mem_list *lmem = list_entry(lhandle,
416 struct efi_mem_list, link);
417 struct efi_mem_desc *desc = &lmem->desc;
418 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
419 uint64_t desc_end = desc->physical_start + desc_len;
420 uint64_t curmax = min(max_addr, desc_end);
421 uint64_t ret = curmax - len;
422
423 /* We only take memory from free RAM */
424 if (desc->type != EFI_CONVENTIONAL_MEMORY)
425 continue;
426
427 /* Out of bounds for max_addr */
428 if ((ret + len) > max_addr)
429 continue;
430
431 /* Out of bounds for upper map limit */
432 if ((ret + len) > desc_end)
433 continue;
434
435 /* Out of bounds for lower map limit */
436 if (ret < desc->physical_start)
437 continue;
438
439 /* Return the highest address in this map within bounds */
440 return ret;
441 }
442
443 return 0;
444}
445
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100446/*
447 * Allocate memory pages.
448 *
449 * @type type of allocation to be performed
450 * @memory_type usage type of the allocated memory
451 * @pages number of pages to be allocated
452 * @memory allocated memory
453 * @return status code
454 */
Alexander Graf8623f922016-03-04 01:10:04 +0100455efi_status_t efi_allocate_pages(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100456 efi_uintn_t pages, uint64_t *memory)
Alexander Graf8623f922016-03-04 01:10:04 +0100457{
458 u64 len = pages << EFI_PAGE_SHIFT;
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200459 efi_status_t ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100460 uint64_t addr;
461
Heinrich Schuchardtf7272332019-04-23 00:30:53 +0200462 /* Check import parameters */
463 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
464 memory_type <= 0x6FFFFFFF)
465 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200466 if (!memory)
467 return EFI_INVALID_PARAMETER;
468
Alexander Graf8623f922016-03-04 01:10:04 +0100469 switch (type) {
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100470 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf8623f922016-03-04 01:10:04 +0100471 /* Any page */
Stephen Warren66208b92018-08-30 15:43:45 -0600472 addr = efi_find_free_memory(len, -1ULL);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200473 if (!addr)
474 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100475 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100476 case EFI_ALLOCATE_MAX_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100477 /* Max address */
478 addr = efi_find_free_memory(len, *memory);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200479 if (!addr)
480 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100481 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100482 case EFI_ALLOCATE_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100483 /* Exact address, reserve it. The addr is already in *memory. */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200484 ret = efi_check_allocated(*memory, false);
485 if (ret != EFI_SUCCESS)
486 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100487 addr = *memory;
488 break;
489 default:
490 /* UEFI doesn't specify other allocation types */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200491 return EFI_INVALID_PARAMETER;
Alexander Graf8623f922016-03-04 01:10:04 +0100492 }
493
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200494 /* Reserve that map in our memory maps */
Michael Walle282d3862020-05-17 12:29:19 +0200495 ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
496 if (ret != EFI_SUCCESS)
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200497 /* Map would overlap, bail out */
498 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100499
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200500 *memory = addr;
Alexander Graf8623f922016-03-04 01:10:04 +0100501
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200502 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100503}
504
505void *efi_alloc(uint64_t len, int memory_type)
506{
507 uint64_t ret = 0;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100508 uint64_t pages = efi_size_in_pages(len);
Alexander Graf8623f922016-03-04 01:10:04 +0100509 efi_status_t r;
510
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200511 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
512 &ret);
Alexander Graf8623f922016-03-04 01:10:04 +0100513 if (r == EFI_SUCCESS)
514 return (void*)(uintptr_t)ret;
515
516 return NULL;
517}
518
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100519/**
520 * efi_free_pages() - free memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100521 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100522 * @memory: start of the memory area to be freed
523 * @pages: number of pages to be freed
524 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100525 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100526efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf8623f922016-03-04 01:10:04 +0100527{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200528 efi_status_t ret;
529
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200530 ret = efi_check_allocated(memory, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200531 if (ret != EFI_SUCCESS)
532 return ret;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200533
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100534 /* Sanity check */
Heinrich Schuchardt7bf4bc92019-04-25 18:41:40 +0200535 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100536 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
537 memory, pages);
538 return EFI_INVALID_PARAMETER;
539 }
540
Michael Walle282d3862020-05-17 12:29:19 +0200541 ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY,
542 false);
Stefan Brünsa4f43022016-10-01 23:32:27 +0200543 /* Merging of adjacent free regions is missing */
544
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100545 if (ret != EFI_SUCCESS)
546 return EFI_NOT_FOUND;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200547
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100548 return ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100549}
550
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100551/**
552 * efi_allocate_pool - allocate memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100553 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100554 * @pool_type: type of the pool from which memory is to be allocated
555 * @size: number of bytes to be allocated
556 * @buffer: allocated memory
557 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100558 */
559efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200560{
561 efi_status_t r;
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100562 u64 addr;
Alexander Grafcd405172018-06-18 17:23:15 +0200563 struct efi_pool_allocation *alloc;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100564 u64 num_pages = efi_size_in_pages(size +
565 sizeof(struct efi_pool_allocation));
Stefan Brüns67b67d92016-10-09 22:17:26 +0200566
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200567 if (!buffer)
568 return EFI_INVALID_PARAMETER;
569
Stefan Brüns67b67d92016-10-09 22:17:26 +0200570 if (size == 0) {
571 *buffer = NULL;
572 return EFI_SUCCESS;
573 }
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200574
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200575 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100576 &addr);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200577 if (r == EFI_SUCCESS) {
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100578 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200579 alloc->num_pages = num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100580 alloc->checksum = checksum(alloc);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200581 *buffer = alloc->data;
582 }
583
584 return r;
585}
586
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100587/**
588 * efi_free_pool() - free memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100589 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100590 * @buffer: start of memory to be freed
591 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100592 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200593efi_status_t efi_free_pool(void *buffer)
594{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200595 efi_status_t ret;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200596 struct efi_pool_allocation *alloc;
597
Heinrich Schuchardt56b587f72019-06-12 07:17:04 +0200598 if (!buffer)
599 return EFI_INVALID_PARAMETER;
600
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200601 ret = efi_check_allocated((uintptr_t)buffer, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200602 if (ret != EFI_SUCCESS)
603 return ret;
xypron.glpk@gmx.de34ddae72017-07-14 19:12:39 +0200604
Stefan Brüns67b67d92016-10-09 22:17:26 +0200605 alloc = container_of(buffer, struct efi_pool_allocation, data);
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100606
607 /* Check that this memory was allocated by efi_allocate_pool() */
608 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
609 alloc->checksum != checksum(alloc)) {
610 printf("%s: illegal free 0x%p\n", __func__, buffer);
611 return EFI_INVALID_PARAMETER;
612 }
613 /* Avoid double free */
614 alloc->checksum = 0;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200615
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200616 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200617
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200618 return ret;
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200619}
620
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100621/*
622 * Get map describing memory usage.
623 *
624 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
625 * on exit the size of the copied memory map
626 * @memory_map buffer to which the memory map is written
627 * @map_key key for the memory map
628 * @descriptor_size size of an individual memory descriptor
629 * @descriptor_version version number of the memory descriptor structure
630 * @return status code
631 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100632efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
633 struct efi_mem_desc *memory_map,
634 efi_uintn_t *map_key,
635 efi_uintn_t *descriptor_size,
636 uint32_t *descriptor_version)
Alexander Graf8623f922016-03-04 01:10:04 +0100637{
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100638 efi_uintn_t map_size = 0;
Alexander Graf9b591022016-04-11 23:51:02 +0200639 int map_entries = 0;
Alexander Graf8623f922016-03-04 01:10:04 +0100640 struct list_head *lhandle;
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200641 efi_uintn_t provided_map_size;
Alexander Graf8623f922016-03-04 01:10:04 +0100642
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200643 if (!memory_map_size)
644 return EFI_INVALID_PARAMETER;
645
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200646 provided_map_size = *memory_map_size;
647
Alexander Graf8623f922016-03-04 01:10:04 +0100648 list_for_each(lhandle, &efi_mem)
Alexander Graf9b591022016-04-11 23:51:02 +0200649 map_entries++;
650
651 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf8623f922016-03-04 01:10:04 +0100652
Rob Clarka84c9742017-07-26 14:34:05 -0400653 *memory_map_size = map_size;
654
Alexander Graf8623f922016-03-04 01:10:04 +0100655 if (descriptor_size)
656 *descriptor_size = sizeof(struct efi_mem_desc);
657
Mian Yousaf Kaukab338cf562016-09-05 23:59:22 +0200658 if (descriptor_version)
659 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
660
AKASHI Takahiro3ee138d2020-03-11 15:18:18 +0900661 if (provided_map_size < map_size)
662 return EFI_BUFFER_TOO_SMALL;
663
664 if (!memory_map)
665 return EFI_INVALID_PARAMETER;
666
Alexander Graf8623f922016-03-04 01:10:04 +0100667 /* Copy list into array */
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200668 /* Return the list in ascending order */
669 memory_map = &memory_map[map_entries - 1];
670 list_for_each(lhandle, &efi_mem) {
671 struct efi_mem_list *lmem;
Alexander Graf8623f922016-03-04 01:10:04 +0100672
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200673 lmem = list_entry(lhandle, struct efi_mem_list, link);
674 *memory_map = lmem->desc;
675 memory_map--;
Alexander Graf8623f922016-03-04 01:10:04 +0100676 }
677
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200678 if (map_key)
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200679 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dee436e632017-07-21 19:05:44 +0200680
Alexander Graf8623f922016-03-04 01:10:04 +0100681 return EFI_SUCCESS;
682}
683
Park, Aidene009e7a2019-09-03 17:43:43 +0000684/**
685 * efi_add_conventional_memory_map() - add a RAM memory area to the map
686 *
687 * @ram_start: start address of a RAM memory area
688 * @ram_end: end address of a RAM memory area
689 * @ram_top: max address to be used as conventional memory
690 * Return: status code
691 */
692efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
693 u64 ram_top)
694{
695 u64 pages;
696
697 /* Remove partial pages */
698 ram_end &= ~EFI_PAGE_MASK;
699 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
700
701 if (ram_end <= ram_start) {
702 /* Invalid mapping */
703 return EFI_INVALID_PARAMETER;
704 }
705
706 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
707
Michael Walle282d3862020-05-17 12:29:19 +0200708 efi_add_memory_map_pg(ram_start, pages,
709 EFI_CONVENTIONAL_MEMORY, false);
Park, Aidene009e7a2019-09-03 17:43:43 +0000710
711 /*
712 * Boards may indicate to the U-Boot memory core that they
713 * can not support memory above ram_top. Let's honor this
714 * in the efi_loader subsystem too by declaring any memory
715 * above ram_top as "already occupied by firmware".
716 */
717 if (ram_top < ram_start) {
718 /* ram_top is before this region, reserve all */
Michael Walle282d3862020-05-17 12:29:19 +0200719 efi_add_memory_map_pg(ram_start, pages,
720 EFI_BOOT_SERVICES_DATA, true);
Park, Aidene009e7a2019-09-03 17:43:43 +0000721 } else if ((ram_top >= ram_start) && (ram_top < ram_end)) {
722 /* ram_top is inside this region, reserve parts */
723 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
724
Michael Walle282d3862020-05-17 12:29:19 +0200725 efi_add_memory_map_pg(ram_top, pages,
726 EFI_BOOT_SERVICES_DATA, true);
Park, Aidene009e7a2019-09-03 17:43:43 +0000727 }
728
729 return EFI_SUCCESS;
730}
731
York Suna8e9f0a2017-03-06 09:02:27 -0800732__weak void efi_add_known_memory(void)
Alexander Graf8623f922016-03-04 01:10:04 +0100733{
Alexander Grafc214edf2018-11-30 21:24:56 +0100734 u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
Alexander Graf8623f922016-03-04 01:10:04 +0100735 int i;
736
Heinrich Schuchardteb8edcf2019-01-05 23:41:36 +0100737 /*
738 * ram_top is just outside mapped memory. So use an offset of one for
739 * mapping the sandbox address.
740 */
741 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
742
Alexander Grafc214edf2018-11-30 21:24:56 +0100743 /* Fix for 32bit targets with ram_top at 4G */
744 if (!ram_top)
745 ram_top = 0x100000000ULL;
746
Alexander Graf8623f922016-03-04 01:10:04 +0100747 /* Add RAM */
748 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Park, Aidene009e7a2019-09-03 17:43:43 +0000749 u64 ram_end, ram_start;
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100750
Heinrich Schuchardt6738fcd2018-11-18 17:58:46 +0100751 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100752 ram_end = ram_start + gd->bd->bi_dram[i].size;
753
Park, Aidene009e7a2019-09-03 17:43:43 +0000754 efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
Alexander Graf8623f922016-03-04 01:10:04 +0100755 }
York Suna8e9f0a2017-03-06 09:02:27 -0800756}
757
Simon Glass78639432018-06-18 17:23:12 +0200758/* Add memory regions for U-Boot's memory and for the runtime services code */
759static void add_u_boot_and_runtime(void)
York Suna8e9f0a2017-03-06 09:02:27 -0800760{
761 unsigned long runtime_start, runtime_end, runtime_pages;
Alexander Graf15937a22018-09-17 13:54:33 +0200762 unsigned long runtime_mask = EFI_PAGE_MASK;
York Suna8e9f0a2017-03-06 09:02:27 -0800763 unsigned long uboot_start, uboot_pages;
764 unsigned long uboot_stack_size = 16 * 1024 * 1024;
765
Alexander Graf8623f922016-03-04 01:10:04 +0100766 /* Add U-Boot */
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100767 uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
768 uboot_stack_size) & ~EFI_PAGE_MASK;
769 uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
770 uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Michael Walle282d3862020-05-17 12:29:19 +0200771 efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_LOADER_DATA,
772 false);
Alexander Graf8623f922016-03-04 01:10:04 +0100773
Alexander Graf15937a22018-09-17 13:54:33 +0200774#if defined(__aarch64__)
775 /*
776 * Runtime Services must be 64KiB aligned according to the
777 * "AArch64 Platforms" section in the UEFI spec (2.7+).
778 */
779
780 runtime_mask = SZ_64K - 1;
781#endif
782
783 /*
784 * Add Runtime Services. We mark surrounding boottime code as runtime as
785 * well to fulfill the runtime alignment constraints but avoid padding.
786 */
787 runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100788 runtime_end = (ulong)&__efi_runtime_stop;
Alexander Graf15937a22018-09-17 13:54:33 +0200789 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100790 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
Michael Walle282d3862020-05-17 12:29:19 +0200791 efi_add_memory_map_pg(runtime_start, runtime_pages,
792 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass78639432018-06-18 17:23:12 +0200793}
794
795int efi_memory_init(void)
796{
797 efi_add_known_memory();
798
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100799 add_u_boot_and_runtime();
Alexander Graf8623f922016-03-04 01:10:04 +0100800
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200801#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
802 /* Request a 32bit 64MB bounce buffer region */
803 uint64_t efi_bounce_buffer_addr = 0xffffffff;
804
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200805 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200806 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
807 &efi_bounce_buffer_addr) != EFI_SUCCESS)
808 return -1;
809
810 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
811#endif
812
Alexander Graf8623f922016-03-04 01:10:04 +0100813 return 0;
814}