blob: 11e755363e44b9d7bcf7ddc8777840dfd2964e5b [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf8623f922016-03-04 01:10:04 +01002/*
3 * EFI application memory management
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf8623f922016-03-04 01:10:04 +01006 */
7
Alexander Graf8623f922016-03-04 01:10:04 +01008#include <common.h>
9#include <efi_loader.h>
Simon Glass6980b6b2019-11-14 12:57:45 -070010#include <init.h>
Alexander Graf8623f922016-03-04 01:10:04 +010011#include <malloc.h>
Alexander Grafcd405172018-06-18 17:23:15 +020012#include <mapmem.h>
Simon Glass3e852172018-03-08 21:53:27 +010013#include <watchdog.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Alexander Grafde2a13b2016-03-30 16:38:29 +020015#include <linux/list_sort.h>
Alexander Graf15937a22018-09-17 13:54:33 +020016#include <linux/sizes.h>
Alexander Graf8623f922016-03-04 01:10:04 +010017
18DECLARE_GLOBAL_DATA_PTR;
19
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010020/* Magic number identifying memory allocated from pool */
21#define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
22
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +020023efi_uintn_t efi_memory_map_key;
24
Alexander Graf8623f922016-03-04 01:10:04 +010025struct efi_mem_list {
26 struct list_head link;
27 struct efi_mem_desc desc;
28};
29
Alexander Graf66d86232016-05-27 12:25:03 +020030#define EFI_CARVE_NO_OVERLAP -1
31#define EFI_CARVE_LOOP_AGAIN -2
32#define EFI_CARVE_OVERLAPS_NONRAM -3
33
Alexander Graf8623f922016-03-04 01:10:04 +010034/* This list contains all memory map items */
35LIST_HEAD(efi_mem);
36
Alexander Graf7c00a3c2016-05-11 18:25:48 +020037#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
38void *efi_bounce_buffer;
39#endif
40
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010041/**
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020042 * struct efi_pool_allocation - memory block allocated from pool
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010043 *
44 * @num_pages: number of pages allocated
45 * @checksum: checksum
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020046 * @data: allocated pool memory
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010047 *
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020048 * U-Boot services each UEFI AllocatePool() request as a separate
49 * (multiple) page allocation. We have to track the number of pages
Stefan Brüns67b67d92016-10-09 22:17:26 +020050 * to be able to free the correct amount later.
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020051 *
52 * The checksum calculated in function checksum() is used in FreePool() to avoid
53 * freeing memory not allocated by AllocatePool() and duplicate freeing.
54 *
Stefan Brüns67b67d92016-10-09 22:17:26 +020055 * EFI requires 8 byte alignment for pool allocations, so we can
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020056 * prepend each allocation with these header fields.
Stefan Brüns67b67d92016-10-09 22:17:26 +020057 */
58struct efi_pool_allocation {
59 u64 num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010060 u64 checksum;
Rob Clarke597fd42017-09-13 18:05:36 -040061 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns67b67d92016-10-09 22:17:26 +020062};
63
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010064/**
65 * checksum() - calculate checksum for memory allocated from pool
66 *
67 * @alloc: allocation header
68 * Return: checksum, always non-zero
69 */
70static u64 checksum(struct efi_pool_allocation *alloc)
71{
72 u64 addr = (uintptr_t)alloc;
73 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
74 EFI_ALLOC_POOL_MAGIC;
75 if (!ret)
76 ++ret;
77 return ret;
78}
79
Stefan Brüns67b67d92016-10-09 22:17:26 +020080/*
Alexander Grafde2a13b2016-03-30 16:38:29 +020081 * Sorts the memory list from highest address to lowest address
82 *
83 * When allocating memory we should always start from the highest
84 * address chunk, so sort the memory list such that the first list
85 * iterator gets the highest address and goes lower from there.
86 */
87static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
88{
89 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
90 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
91
92 if (mema->desc.physical_start == memb->desc.physical_start)
93 return 0;
94 else if (mema->desc.physical_start < memb->desc.physical_start)
95 return 1;
96 else
97 return -1;
98}
99
Alexander Grafd7288142018-09-16 17:05:29 +0200100static uint64_t desc_get_end(struct efi_mem_desc *desc)
101{
102 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
103}
104
Alexander Grafde2a13b2016-03-30 16:38:29 +0200105static void efi_mem_sort(void)
106{
Alexander Grafd7288142018-09-16 17:05:29 +0200107 struct list_head *lhandle;
108 struct efi_mem_list *prevmem = NULL;
109 bool merge_again = true;
110
Alexander Grafde2a13b2016-03-30 16:38:29 +0200111 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Grafd7288142018-09-16 17:05:29 +0200112
113 /* Now merge entries that can be merged */
114 while (merge_again) {
115 merge_again = false;
116 list_for_each(lhandle, &efi_mem) {
117 struct efi_mem_list *lmem;
118 struct efi_mem_desc *prev = &prevmem->desc;
119 struct efi_mem_desc *cur;
120 uint64_t pages;
121
122 lmem = list_entry(lhandle, struct efi_mem_list, link);
123 if (!prevmem) {
124 prevmem = lmem;
125 continue;
126 }
127
128 cur = &lmem->desc;
129
130 if ((desc_get_end(cur) == prev->physical_start) &&
131 (prev->type == cur->type) &&
132 (prev->attribute == cur->attribute)) {
133 /* There is an existing map before, reuse it */
134 pages = cur->num_pages;
135 prev->num_pages += pages;
136 prev->physical_start -= pages << EFI_PAGE_SHIFT;
137 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
138 list_del(&lmem->link);
139 free(lmem);
140
141 merge_again = true;
142 break;
143 }
144
145 prevmem = lmem;
146 }
147 }
Alexander Grafde2a13b2016-03-30 16:38:29 +0200148}
149
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200150/** efi_mem_carve_out - unmap memory region
Alexander Graf8623f922016-03-04 01:10:04 +0100151 *
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200152 * @map: memory map
153 * @carve_desc: memory region to unmap
154 * @overlap_only_ram: the carved out region may only overlap RAM
155 * Return Value: the number of overlapping pages which have been
156 * removed from the map,
157 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
158 * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
159 * and the map contains anything but free ram
160 * (only when overlap_only_ram is true),
161 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
162 * traversed again, as it has been altered.
163 *
164 * Unmaps all memory occupied by the carve_desc region from the list entry
165 * pointed to by map.
Stefan Brüns1b00dc12016-10-01 23:32:23 +0200166 *
167 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200168 * to re-add the already carved out pages to the mapping.
Alexander Graf8623f922016-03-04 01:10:04 +0100169 */
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200170static s64 efi_mem_carve_out(struct efi_mem_list *map,
Alexander Graf8623f922016-03-04 01:10:04 +0100171 struct efi_mem_desc *carve_desc,
172 bool overlap_only_ram)
173{
174 struct efi_mem_list *newmap;
175 struct efi_mem_desc *map_desc = &map->desc;
176 uint64_t map_start = map_desc->physical_start;
177 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
178 uint64_t carve_start = carve_desc->physical_start;
179 uint64_t carve_end = carve_start +
180 (carve_desc->num_pages << EFI_PAGE_SHIFT);
181
182 /* check whether we're overlapping */
183 if ((carve_end <= map_start) || (carve_start >= map_end))
Alexander Graf66d86232016-05-27 12:25:03 +0200184 return EFI_CARVE_NO_OVERLAP;
Alexander Graf8623f922016-03-04 01:10:04 +0100185
186 /* We're overlapping with non-RAM, warn the caller if desired */
187 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
Alexander Graf66d86232016-05-27 12:25:03 +0200188 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf8623f922016-03-04 01:10:04 +0100189
190 /* Sanitize carve_start and carve_end to lie within our bounds */
191 carve_start = max(carve_start, map_start);
192 carve_end = min(carve_end, map_end);
193
194 /* Carving at the beginning of our map? Just move it! */
195 if (carve_start == map_start) {
196 if (map_end == carve_end) {
197 /* Full overlap, just remove map */
198 list_del(&map->link);
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200199 free(map);
200 } else {
201 map->desc.physical_start = carve_end;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200202 map->desc.virtual_start = carve_end;
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200203 map->desc.num_pages = (map_end - carve_end)
204 >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100205 }
206
Alexander Graf66d86232016-05-27 12:25:03 +0200207 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100208 }
209
210 /*
211 * Overlapping maps, just split the list map at carve_start,
212 * it will get moved or removed in the next iteration.
213 *
214 * [ map_desc |__carve_start__| newmap ]
215 */
216
217 /* Create a new map from [ carve_start ... map_end ] */
218 newmap = calloc(1, sizeof(*newmap));
219 newmap->desc = map->desc;
220 newmap->desc.physical_start = carve_start;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200221 newmap->desc.virtual_start = carve_start;
Alexander Graf8623f922016-03-04 01:10:04 +0100222 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
Stefan Brüns9d656512016-10-01 23:32:28 +0200223 /* Insert before current entry (descending address order) */
224 list_add_tail(&newmap->link, &map->link);
Alexander Graf8623f922016-03-04 01:10:04 +0100225
226 /* Shrink the map to [ map_start ... carve_start ] */
227 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
228
Alexander Graf66d86232016-05-27 12:25:03 +0200229 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf8623f922016-03-04 01:10:04 +0100230}
231
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100232/**
Michael Walle282d3862020-05-17 12:29:19 +0200233 * efi_add_memory_map_pg() - add pages to the memory map
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100234 *
235 * @start: start address, must be a multiple of EFI_PAGE_SIZE
236 * @pages: number of pages to add
237 * @memory_type: type of memory added
Maxim Uvarovec631ea2020-08-28 22:47:41 +0300238 * @overlap_only_ram: region may only overlap RAM
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100239 * Return: status code
240 */
Michael Walle282d3862020-05-17 12:29:19 +0200241static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
242 int memory_type,
243 bool overlap_only_ram)
Alexander Graf8623f922016-03-04 01:10:04 +0100244{
245 struct list_head *lhandle;
246 struct efi_mem_list *newlist;
Alexander Graf66d86232016-05-27 12:25:03 +0200247 bool carve_again;
248 uint64_t carved_pages = 0;
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200249 struct efi_event *evt;
Alexander Graf8623f922016-03-04 01:10:04 +0100250
Heinrich Schuchardt706a2fe2019-04-04 21:50:02 +0200251 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
252 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
Andreas Färber102f8bd2016-07-17 06:57:11 +0200253
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200254 if (memory_type >= EFI_MAX_MEMORY_TYPE)
255 return EFI_INVALID_PARAMETER;
256
Alexander Graf8623f922016-03-04 01:10:04 +0100257 if (!pages)
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100258 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100259
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200260 ++efi_memory_map_key;
Alexander Graf8623f922016-03-04 01:10:04 +0100261 newlist = calloc(1, sizeof(*newlist));
262 newlist->desc.type = memory_type;
263 newlist->desc.physical_start = start;
264 newlist->desc.virtual_start = start;
265 newlist->desc.num_pages = pages;
266
267 switch (memory_type) {
268 case EFI_RUNTIME_SERVICES_CODE:
269 case EFI_RUNTIME_SERVICES_DATA:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200270 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100271 break;
272 case EFI_MMAP_IO:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200273 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100274 break;
275 default:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200276 newlist->desc.attribute = EFI_MEMORY_WB;
Alexander Graf8623f922016-03-04 01:10:04 +0100277 break;
278 }
279
280 /* Add our new map */
281 do {
Alexander Graf66d86232016-05-27 12:25:03 +0200282 carve_again = false;
Alexander Graf8623f922016-03-04 01:10:04 +0100283 list_for_each(lhandle, &efi_mem) {
284 struct efi_mem_list *lmem;
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200285 s64 r;
Alexander Graf8623f922016-03-04 01:10:04 +0100286
287 lmem = list_entry(lhandle, struct efi_mem_list, link);
288 r = efi_mem_carve_out(lmem, &newlist->desc,
289 overlap_only_ram);
Alexander Graf66d86232016-05-27 12:25:03 +0200290 switch (r) {
291 case EFI_CARVE_OVERLAPS_NONRAM:
292 /*
293 * The user requested to only have RAM overlaps,
294 * but we hit a non-RAM region. Error out.
295 */
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100296 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200297 case EFI_CARVE_NO_OVERLAP:
298 /* Just ignore this list entry */
299 break;
300 case EFI_CARVE_LOOP_AGAIN:
301 /*
302 * We split an entry, but need to loop through
303 * the list again to actually carve it.
304 */
305 carve_again = true;
306 break;
307 default:
308 /* We carved a number of pages */
309 carved_pages += r;
310 carve_again = true;
Alexander Graf8623f922016-03-04 01:10:04 +0100311 break;
312 }
Alexander Graf66d86232016-05-27 12:25:03 +0200313
314 if (carve_again) {
315 /* The list changed, we need to start over */
316 break;
317 }
Alexander Graf8623f922016-03-04 01:10:04 +0100318 }
Alexander Graf66d86232016-05-27 12:25:03 +0200319 } while (carve_again);
320
321 if (overlap_only_ram && (carved_pages != pages)) {
322 /*
323 * The payload wanted to have RAM overlaps, but we overlapped
324 * with an unallocated region. Error out.
325 */
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100326 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200327 }
Alexander Graf8623f922016-03-04 01:10:04 +0100328
329 /* Add our new map */
330 list_add_tail(&newlist->link, &efi_mem);
331
Alexander Grafde2a13b2016-03-30 16:38:29 +0200332 /* And make sure memory is listed in descending order */
333 efi_mem_sort();
334
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200335 /* Notify that the memory map was changed */
336 list_for_each_entry(evt, &efi_events, link) {
337 if (evt->group &&
338 !guidcmp(evt->group,
339 &efi_guid_event_group_memory_map_change)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200340 efi_signal_event(evt);
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200341 break;
342 }
343 }
344
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100345 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100346}
347
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200348/**
Michael Walle282d3862020-05-17 12:29:19 +0200349 * efi_add_memory_map() - add memory area to the memory map
350 *
351 * @start: start address of the memory area
352 * @size: length in bytes of the memory area
353 * @memory_type: type of memory added
354 *
355 * Return: status code
356 *
357 * This function automatically aligns the start and size of the memory area
358 * to EFI_PAGE_SIZE.
359 */
360efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
361{
362 u64 pages;
363
364 pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
365 start &= ~EFI_PAGE_MASK;
366
367 return efi_add_memory_map_pg(start, pages, memory_type, false);
368}
369
370/**
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200371 * efi_check_allocated() - validate address to be freed
372 *
373 * Check that the address is within allocated memory:
374 *
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200375 * * The address must be in a range of the memory map.
376 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
377 *
378 * Page alignment is not checked as this is not a requirement of
379 * efi_free_pool().
380 *
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200381 * @addr: address of page to be freed
382 * @must_be_allocated: return success if the page is allocated
383 * Return: status code
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200384 */
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200385static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200386{
387 struct efi_mem_list *item;
388
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200389 list_for_each_entry(item, &efi_mem, link) {
390 u64 start = item->desc.physical_start;
391 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
392
393 if (addr >= start && addr < end) {
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200394 if (must_be_allocated ^
395 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200396 return EFI_SUCCESS;
397 else
398 return EFI_NOT_FOUND;
399 }
400 }
401
402 return EFI_NOT_FOUND;
403}
404
Alexander Graf8623f922016-03-04 01:10:04 +0100405static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
406{
407 struct list_head *lhandle;
408
Alexander Grafc3b2a252018-11-05 00:30:46 +0100409 /*
410 * Prealign input max address, so we simplify our matching
411 * logic below and can just reuse it as return pointer.
412 */
413 max_addr &= ~EFI_PAGE_MASK;
414
Alexander Graf8623f922016-03-04 01:10:04 +0100415 list_for_each(lhandle, &efi_mem) {
416 struct efi_mem_list *lmem = list_entry(lhandle,
417 struct efi_mem_list, link);
418 struct efi_mem_desc *desc = &lmem->desc;
419 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
420 uint64_t desc_end = desc->physical_start + desc_len;
421 uint64_t curmax = min(max_addr, desc_end);
422 uint64_t ret = curmax - len;
423
424 /* We only take memory from free RAM */
425 if (desc->type != EFI_CONVENTIONAL_MEMORY)
426 continue;
427
428 /* Out of bounds for max_addr */
429 if ((ret + len) > max_addr)
430 continue;
431
432 /* Out of bounds for upper map limit */
433 if ((ret + len) > desc_end)
434 continue;
435
436 /* Out of bounds for lower map limit */
437 if (ret < desc->physical_start)
438 continue;
439
440 /* Return the highest address in this map within bounds */
441 return ret;
442 }
443
444 return 0;
445}
446
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100447/*
448 * Allocate memory pages.
449 *
450 * @type type of allocation to be performed
451 * @memory_type usage type of the allocated memory
452 * @pages number of pages to be allocated
453 * @memory allocated memory
454 * @return status code
455 */
Alexander Graf8623f922016-03-04 01:10:04 +0100456efi_status_t efi_allocate_pages(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100457 efi_uintn_t pages, uint64_t *memory)
Alexander Graf8623f922016-03-04 01:10:04 +0100458{
459 u64 len = pages << EFI_PAGE_SHIFT;
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200460 efi_status_t ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100461 uint64_t addr;
462
Heinrich Schuchardtf7272332019-04-23 00:30:53 +0200463 /* Check import parameters */
464 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
465 memory_type <= 0x6FFFFFFF)
466 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200467 if (!memory)
468 return EFI_INVALID_PARAMETER;
469
Alexander Graf8623f922016-03-04 01:10:04 +0100470 switch (type) {
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100471 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf8623f922016-03-04 01:10:04 +0100472 /* Any page */
Stephen Warren66208b92018-08-30 15:43:45 -0600473 addr = efi_find_free_memory(len, -1ULL);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200474 if (!addr)
475 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100476 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100477 case EFI_ALLOCATE_MAX_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100478 /* Max address */
479 addr = efi_find_free_memory(len, *memory);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200480 if (!addr)
481 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100482 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100483 case EFI_ALLOCATE_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100484 /* Exact address, reserve it. The addr is already in *memory. */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200485 ret = efi_check_allocated(*memory, false);
486 if (ret != EFI_SUCCESS)
487 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100488 addr = *memory;
489 break;
490 default:
491 /* UEFI doesn't specify other allocation types */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200492 return EFI_INVALID_PARAMETER;
Alexander Graf8623f922016-03-04 01:10:04 +0100493 }
494
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200495 /* Reserve that map in our memory maps */
Michael Walle282d3862020-05-17 12:29:19 +0200496 ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
497 if (ret != EFI_SUCCESS)
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200498 /* Map would overlap, bail out */
499 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100500
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200501 *memory = addr;
Alexander Graf8623f922016-03-04 01:10:04 +0100502
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200503 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100504}
505
506void *efi_alloc(uint64_t len, int memory_type)
507{
508 uint64_t ret = 0;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100509 uint64_t pages = efi_size_in_pages(len);
Alexander Graf8623f922016-03-04 01:10:04 +0100510 efi_status_t r;
511
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200512 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
513 &ret);
Alexander Graf8623f922016-03-04 01:10:04 +0100514 if (r == EFI_SUCCESS)
515 return (void*)(uintptr_t)ret;
516
517 return NULL;
518}
519
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100520/**
521 * efi_free_pages() - free memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100522 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100523 * @memory: start of the memory area to be freed
524 * @pages: number of pages to be freed
525 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100526 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100527efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf8623f922016-03-04 01:10:04 +0100528{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200529 efi_status_t ret;
530
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200531 ret = efi_check_allocated(memory, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200532 if (ret != EFI_SUCCESS)
533 return ret;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200534
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100535 /* Sanity check */
Heinrich Schuchardt7bf4bc92019-04-25 18:41:40 +0200536 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100537 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
538 memory, pages);
539 return EFI_INVALID_PARAMETER;
540 }
541
Michael Walle282d3862020-05-17 12:29:19 +0200542 ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY,
543 false);
Stefan Brünsa4f43022016-10-01 23:32:27 +0200544 /* Merging of adjacent free regions is missing */
545
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100546 if (ret != EFI_SUCCESS)
547 return EFI_NOT_FOUND;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200548
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/**
553 * efi_allocate_pool - allocate memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100554 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100555 * @pool_type: type of the pool from which memory is to be allocated
556 * @size: number of bytes to be allocated
557 * @buffer: allocated memory
558 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100559 */
560efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200561{
562 efi_status_t r;
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100563 u64 addr;
Alexander Grafcd405172018-06-18 17:23:15 +0200564 struct efi_pool_allocation *alloc;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100565 u64 num_pages = efi_size_in_pages(size +
566 sizeof(struct efi_pool_allocation));
Stefan Brüns67b67d92016-10-09 22:17:26 +0200567
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200568 if (!buffer)
569 return EFI_INVALID_PARAMETER;
570
Stefan Brüns67b67d92016-10-09 22:17:26 +0200571 if (size == 0) {
572 *buffer = NULL;
573 return EFI_SUCCESS;
574 }
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200575
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200576 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100577 &addr);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200578 if (r == EFI_SUCCESS) {
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100579 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200580 alloc->num_pages = num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100581 alloc->checksum = checksum(alloc);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200582 *buffer = alloc->data;
583 }
584
585 return r;
586}
587
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100588/**
589 * efi_free_pool() - free memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100590 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100591 * @buffer: start of memory to be freed
592 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100593 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200594efi_status_t efi_free_pool(void *buffer)
595{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200596 efi_status_t ret;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200597 struct efi_pool_allocation *alloc;
598
Heinrich Schuchardt56b587f72019-06-12 07:17:04 +0200599 if (!buffer)
600 return EFI_INVALID_PARAMETER;
601
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200602 ret = efi_check_allocated((uintptr_t)buffer, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200603 if (ret != EFI_SUCCESS)
604 return ret;
xypron.glpk@gmx.de34ddae72017-07-14 19:12:39 +0200605
Stefan Brüns67b67d92016-10-09 22:17:26 +0200606 alloc = container_of(buffer, struct efi_pool_allocation, data);
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100607
608 /* Check that this memory was allocated by efi_allocate_pool() */
609 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
610 alloc->checksum != checksum(alloc)) {
611 printf("%s: illegal free 0x%p\n", __func__, buffer);
612 return EFI_INVALID_PARAMETER;
613 }
614 /* Avoid double free */
615 alloc->checksum = 0;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200616
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200617 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200618
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200619 return ret;
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200620}
621
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100622/*
623 * Get map describing memory usage.
624 *
625 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
626 * on exit the size of the copied memory map
627 * @memory_map buffer to which the memory map is written
628 * @map_key key for the memory map
629 * @descriptor_size size of an individual memory descriptor
630 * @descriptor_version version number of the memory descriptor structure
631 * @return status code
632 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100633efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
634 struct efi_mem_desc *memory_map,
635 efi_uintn_t *map_key,
636 efi_uintn_t *descriptor_size,
637 uint32_t *descriptor_version)
Alexander Graf8623f922016-03-04 01:10:04 +0100638{
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100639 efi_uintn_t map_size = 0;
Alexander Graf9b591022016-04-11 23:51:02 +0200640 int map_entries = 0;
Alexander Graf8623f922016-03-04 01:10:04 +0100641 struct list_head *lhandle;
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200642 efi_uintn_t provided_map_size;
Alexander Graf8623f922016-03-04 01:10:04 +0100643
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200644 if (!memory_map_size)
645 return EFI_INVALID_PARAMETER;
646
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200647 provided_map_size = *memory_map_size;
648
Alexander Graf8623f922016-03-04 01:10:04 +0100649 list_for_each(lhandle, &efi_mem)
Alexander Graf9b591022016-04-11 23:51:02 +0200650 map_entries++;
651
652 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf8623f922016-03-04 01:10:04 +0100653
Rob Clarka84c9742017-07-26 14:34:05 -0400654 *memory_map_size = map_size;
655
Alexander Graf8623f922016-03-04 01:10:04 +0100656 if (descriptor_size)
657 *descriptor_size = sizeof(struct efi_mem_desc);
658
Mian Yousaf Kaukab338cf562016-09-05 23:59:22 +0200659 if (descriptor_version)
660 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
661
AKASHI Takahiro3ee138d2020-03-11 15:18:18 +0900662 if (provided_map_size < map_size)
663 return EFI_BUFFER_TOO_SMALL;
664
665 if (!memory_map)
666 return EFI_INVALID_PARAMETER;
667
Alexander Graf8623f922016-03-04 01:10:04 +0100668 /* Copy list into array */
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200669 /* Return the list in ascending order */
670 memory_map = &memory_map[map_entries - 1];
671 list_for_each(lhandle, &efi_mem) {
672 struct efi_mem_list *lmem;
Alexander Graf8623f922016-03-04 01:10:04 +0100673
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200674 lmem = list_entry(lhandle, struct efi_mem_list, link);
675 *memory_map = lmem->desc;
676 memory_map--;
Alexander Graf8623f922016-03-04 01:10:04 +0100677 }
678
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200679 if (map_key)
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200680 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dee436e632017-07-21 19:05:44 +0200681
Alexander Graf8623f922016-03-04 01:10:04 +0100682 return EFI_SUCCESS;
683}
684
Park, Aidene009e7a2019-09-03 17:43:43 +0000685/**
686 * efi_add_conventional_memory_map() - add a RAM memory area to the map
687 *
688 * @ram_start: start address of a RAM memory area
689 * @ram_end: end address of a RAM memory area
690 * @ram_top: max address to be used as conventional memory
691 * Return: status code
692 */
693efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
694 u64 ram_top)
695{
696 u64 pages;
697
698 /* Remove partial pages */
699 ram_end &= ~EFI_PAGE_MASK;
700 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
701
702 if (ram_end <= ram_start) {
703 /* Invalid mapping */
704 return EFI_INVALID_PARAMETER;
705 }
706
707 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
708
Michael Walle282d3862020-05-17 12:29:19 +0200709 efi_add_memory_map_pg(ram_start, pages,
710 EFI_CONVENTIONAL_MEMORY, false);
Park, Aidene009e7a2019-09-03 17:43:43 +0000711
712 /*
713 * Boards may indicate to the U-Boot memory core that they
714 * can not support memory above ram_top. Let's honor this
715 * in the efi_loader subsystem too by declaring any memory
716 * above ram_top as "already occupied by firmware".
717 */
718 if (ram_top < ram_start) {
719 /* ram_top is before this region, reserve all */
Michael Walle282d3862020-05-17 12:29:19 +0200720 efi_add_memory_map_pg(ram_start, pages,
721 EFI_BOOT_SERVICES_DATA, true);
Park, Aidene009e7a2019-09-03 17:43:43 +0000722 } else if ((ram_top >= ram_start) && (ram_top < ram_end)) {
723 /* ram_top is inside this region, reserve parts */
724 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
725
Michael Walle282d3862020-05-17 12:29:19 +0200726 efi_add_memory_map_pg(ram_top, pages,
727 EFI_BOOT_SERVICES_DATA, true);
Park, Aidene009e7a2019-09-03 17:43:43 +0000728 }
729
730 return EFI_SUCCESS;
731}
732
York Suna8e9f0a2017-03-06 09:02:27 -0800733__weak void efi_add_known_memory(void)
Alexander Graf8623f922016-03-04 01:10:04 +0100734{
Alexander Grafc214edf2018-11-30 21:24:56 +0100735 u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
Alexander Graf8623f922016-03-04 01:10:04 +0100736 int i;
737
Heinrich Schuchardteb8edcf2019-01-05 23:41:36 +0100738 /*
739 * ram_top is just outside mapped memory. So use an offset of one for
740 * mapping the sandbox address.
741 */
742 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
743
Alexander Grafc214edf2018-11-30 21:24:56 +0100744 /* Fix for 32bit targets with ram_top at 4G */
745 if (!ram_top)
746 ram_top = 0x100000000ULL;
747
Alexander Graf8623f922016-03-04 01:10:04 +0100748 /* Add RAM */
749 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Park, Aidene009e7a2019-09-03 17:43:43 +0000750 u64 ram_end, ram_start;
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100751
Heinrich Schuchardt6738fcd2018-11-18 17:58:46 +0100752 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100753 ram_end = ram_start + gd->bd->bi_dram[i].size;
754
Park, Aidene009e7a2019-09-03 17:43:43 +0000755 efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
Alexander Graf8623f922016-03-04 01:10:04 +0100756 }
York Suna8e9f0a2017-03-06 09:02:27 -0800757}
758
Simon Glass78639432018-06-18 17:23:12 +0200759/* Add memory regions for U-Boot's memory and for the runtime services code */
760static void add_u_boot_and_runtime(void)
York Suna8e9f0a2017-03-06 09:02:27 -0800761{
762 unsigned long runtime_start, runtime_end, runtime_pages;
Alexander Graf15937a22018-09-17 13:54:33 +0200763 unsigned long runtime_mask = EFI_PAGE_MASK;
York Suna8e9f0a2017-03-06 09:02:27 -0800764 unsigned long uboot_start, uboot_pages;
Heinrich Schuchardtc6dcbe12020-07-29 12:37:35 +0200765 unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
York Suna8e9f0a2017-03-06 09:02:27 -0800766
Alexander Graf8623f922016-03-04 01:10:04 +0100767 /* Add U-Boot */
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100768 uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
769 uboot_stack_size) & ~EFI_PAGE_MASK;
770 uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
771 uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Michael Walle282d3862020-05-17 12:29:19 +0200772 efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_LOADER_DATA,
773 false);
Alexander Graf8623f922016-03-04 01:10:04 +0100774
Alexander Graf15937a22018-09-17 13:54:33 +0200775#if defined(__aarch64__)
776 /*
777 * Runtime Services must be 64KiB aligned according to the
778 * "AArch64 Platforms" section in the UEFI spec (2.7+).
779 */
780
781 runtime_mask = SZ_64K - 1;
782#endif
783
784 /*
785 * Add Runtime Services. We mark surrounding boottime code as runtime as
786 * well to fulfill the runtime alignment constraints but avoid padding.
787 */
788 runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100789 runtime_end = (ulong)&__efi_runtime_stop;
Alexander Graf15937a22018-09-17 13:54:33 +0200790 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100791 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
Michael Walle282d3862020-05-17 12:29:19 +0200792 efi_add_memory_map_pg(runtime_start, runtime_pages,
793 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass78639432018-06-18 17:23:12 +0200794}
795
796int efi_memory_init(void)
797{
798 efi_add_known_memory();
799
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100800 add_u_boot_and_runtime();
Alexander Graf8623f922016-03-04 01:10:04 +0100801
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200802#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
803 /* Request a 32bit 64MB bounce buffer region */
804 uint64_t efi_bounce_buffer_addr = 0xffffffff;
805
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200806 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200807 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
808 &efi_bounce_buffer_addr) != EFI_SUCCESS)
809 return -1;
810
811 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
812#endif
813
Alexander Graf8623f922016-03-04 01:10:04 +0100814 return 0;
815}