blob: 1136f4f733493fb918d39059f97840861e6cc289 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf5d009952016-03-04 01:10:04 +01002/*
3 * EFI application memory management
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf5d009952016-03-04 01:10:04 +01006 */
7
Alexander Graf5d009952016-03-04 01:10:04 +01008#include <common.h>
9#include <efi_loader.h>
Simon Glassbdecaeb2018-03-08 21:53:27 +010010#include <malloc.h>
Alexander Graf282a06c2018-06-18 17:23:15 +020011#include <mapmem.h>
Alexander Graf5d009952016-03-04 01:10:04 +010012#include <watchdog.h>
Simon Glassbdecaeb2018-03-08 21:53:27 +010013#include <linux/list_sort.h>
Alexander Graf7a82c302018-09-17 13:54:33 +020014#include <linux/sizes.h>
Alexander Graf5d009952016-03-04 01:10:04 +010015
16DECLARE_GLOBAL_DATA_PTR;
17
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010018/* Magic number identifying memory allocated from pool */
19#define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
20
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +020021efi_uintn_t efi_memory_map_key;
22
Alexander Graf5d009952016-03-04 01:10:04 +010023struct efi_mem_list {
24 struct list_head link;
25 struct efi_mem_desc desc;
26};
27
Alexander Graf74c16ac2016-05-27 12:25:03 +020028#define EFI_CARVE_NO_OVERLAP -1
29#define EFI_CARVE_LOOP_AGAIN -2
30#define EFI_CARVE_OVERLAPS_NONRAM -3
31
Alexander Graf5d009952016-03-04 01:10:04 +010032/* This list contains all memory map items */
33LIST_HEAD(efi_mem);
34
Alexander Graf51735ae2016-05-11 18:25:48 +020035#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
36void *efi_bounce_buffer;
37#endif
38
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010039/**
40 * efi_pool_allocation - memory block allocated from pool
41 *
42 * @num_pages: number of pages allocated
43 * @checksum: checksum
44 *
Stefan Brüns42417bc2016-10-09 22:17:26 +020045 * U-Boot services each EFI AllocatePool request as a separate
46 * (multiple) page allocation. We have to track the number of pages
47 * to be able to free the correct amount later.
48 * EFI requires 8 byte alignment for pool allocations, so we can
49 * prepend each allocation with an 64 bit header tracking the
50 * allocation size, and hand out the remainder to the caller.
51 */
52struct efi_pool_allocation {
53 u64 num_pages;
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010054 u64 checksum;
Rob Clark946160f2017-09-13 18:05:36 -040055 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns42417bc2016-10-09 22:17:26 +020056};
57
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +010058/**
59 * checksum() - calculate checksum for memory allocated from pool
60 *
61 * @alloc: allocation header
62 * Return: checksum, always non-zero
63 */
64static u64 checksum(struct efi_pool_allocation *alloc)
65{
66 u64 addr = (uintptr_t)alloc;
67 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
68 EFI_ALLOC_POOL_MAGIC;
69 if (!ret)
70 ++ret;
71 return ret;
72}
73
Stefan Brüns42417bc2016-10-09 22:17:26 +020074/*
Alexander Graf38ce65e2016-03-30 16:38:29 +020075 * Sorts the memory list from highest address to lowest address
76 *
77 * When allocating memory we should always start from the highest
78 * address chunk, so sort the memory list such that the first list
79 * iterator gets the highest address and goes lower from there.
80 */
81static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
82{
83 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
84 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
85
86 if (mema->desc.physical_start == memb->desc.physical_start)
87 return 0;
88 else if (mema->desc.physical_start < memb->desc.physical_start)
89 return 1;
90 else
91 return -1;
92}
93
Alexander Graf7b056672018-09-16 17:05:29 +020094static uint64_t desc_get_end(struct efi_mem_desc *desc)
95{
96 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
97}
98
Alexander Graf38ce65e2016-03-30 16:38:29 +020099static void efi_mem_sort(void)
100{
Alexander Graf7b056672018-09-16 17:05:29 +0200101 struct list_head *lhandle;
102 struct efi_mem_list *prevmem = NULL;
103 bool merge_again = true;
104
Alexander Graf38ce65e2016-03-30 16:38:29 +0200105 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Graf7b056672018-09-16 17:05:29 +0200106
107 /* Now merge entries that can be merged */
108 while (merge_again) {
109 merge_again = false;
110 list_for_each(lhandle, &efi_mem) {
111 struct efi_mem_list *lmem;
112 struct efi_mem_desc *prev = &prevmem->desc;
113 struct efi_mem_desc *cur;
114 uint64_t pages;
115
116 lmem = list_entry(lhandle, struct efi_mem_list, link);
117 if (!prevmem) {
118 prevmem = lmem;
119 continue;
120 }
121
122 cur = &lmem->desc;
123
124 if ((desc_get_end(cur) == prev->physical_start) &&
125 (prev->type == cur->type) &&
126 (prev->attribute == cur->attribute)) {
127 /* There is an existing map before, reuse it */
128 pages = cur->num_pages;
129 prev->num_pages += pages;
130 prev->physical_start -= pages << EFI_PAGE_SHIFT;
131 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
132 list_del(&lmem->link);
133 free(lmem);
134
135 merge_again = true;
136 break;
137 }
138
139 prevmem = lmem;
140 }
141 }
Alexander Graf38ce65e2016-03-30 16:38:29 +0200142}
143
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200144/** efi_mem_carve_out - unmap memory region
Alexander Graf5d009952016-03-04 01:10:04 +0100145 *
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200146 * @map: memory map
147 * @carve_desc: memory region to unmap
148 * @overlap_only_ram: the carved out region may only overlap RAM
149 * Return Value: the number of overlapping pages which have been
150 * removed from the map,
151 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
152 * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
153 * and the map contains anything but free ram
154 * (only when overlap_only_ram is true),
155 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
156 * traversed again, as it has been altered.
157 *
158 * Unmaps all memory occupied by the carve_desc region from the list entry
159 * pointed to by map.
Stefan Brüns852efbf2016-10-01 23:32:23 +0200160 *
161 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200162 * to re-add the already carved out pages to the mapping.
Alexander Graf5d009952016-03-04 01:10:04 +0100163 */
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200164static s64 efi_mem_carve_out(struct efi_mem_list *map,
Alexander Graf5d009952016-03-04 01:10:04 +0100165 struct efi_mem_desc *carve_desc,
166 bool overlap_only_ram)
167{
168 struct efi_mem_list *newmap;
169 struct efi_mem_desc *map_desc = &map->desc;
170 uint64_t map_start = map_desc->physical_start;
171 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
172 uint64_t carve_start = carve_desc->physical_start;
173 uint64_t carve_end = carve_start +
174 (carve_desc->num_pages << EFI_PAGE_SHIFT);
175
176 /* check whether we're overlapping */
177 if ((carve_end <= map_start) || (carve_start >= map_end))
Alexander Graf74c16ac2016-05-27 12:25:03 +0200178 return EFI_CARVE_NO_OVERLAP;
Alexander Graf5d009952016-03-04 01:10:04 +0100179
180 /* We're overlapping with non-RAM, warn the caller if desired */
181 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
Alexander Graf74c16ac2016-05-27 12:25:03 +0200182 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf5d009952016-03-04 01:10:04 +0100183
184 /* Sanitize carve_start and carve_end to lie within our bounds */
185 carve_start = max(carve_start, map_start);
186 carve_end = min(carve_end, map_end);
187
188 /* Carving at the beginning of our map? Just move it! */
189 if (carve_start == map_start) {
190 if (map_end == carve_end) {
191 /* Full overlap, just remove map */
192 list_del(&map->link);
Stefan Brüns511d0b92016-10-01 23:32:29 +0200193 free(map);
194 } else {
195 map->desc.physical_start = carve_end;
196 map->desc.num_pages = (map_end - carve_end)
197 >> EFI_PAGE_SHIFT;
Alexander Graf5d009952016-03-04 01:10:04 +0100198 }
199
Alexander Graf74c16ac2016-05-27 12:25:03 +0200200 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf5d009952016-03-04 01:10:04 +0100201 }
202
203 /*
204 * Overlapping maps, just split the list map at carve_start,
205 * it will get moved or removed in the next iteration.
206 *
207 * [ map_desc |__carve_start__| newmap ]
208 */
209
210 /* Create a new map from [ carve_start ... map_end ] */
211 newmap = calloc(1, sizeof(*newmap));
212 newmap->desc = map->desc;
213 newmap->desc.physical_start = carve_start;
214 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
Stefan Brünsb6a95172016-10-01 23:32:28 +0200215 /* Insert before current entry (descending address order) */
216 list_add_tail(&newmap->link, &map->link);
Alexander Graf5d009952016-03-04 01:10:04 +0100217
218 /* Shrink the map to [ map_start ... carve_start ] */
219 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
220
Alexander Graf74c16ac2016-05-27 12:25:03 +0200221 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf5d009952016-03-04 01:10:04 +0100222}
223
224uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
225 bool overlap_only_ram)
226{
227 struct list_head *lhandle;
228 struct efi_mem_list *newlist;
Alexander Graf74c16ac2016-05-27 12:25:03 +0200229 bool carve_again;
230 uint64_t carved_pages = 0;
Alexander Graf5d009952016-03-04 01:10:04 +0100231
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900232 debug("%s: 0x%llx 0x%llx %d %s\n", __func__,
Andreas Färberc933ed92016-07-17 06:57:11 +0200233 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
234
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +0200235 if (memory_type >= EFI_MAX_MEMORY_TYPE)
236 return EFI_INVALID_PARAMETER;
237
Alexander Graf5d009952016-03-04 01:10:04 +0100238 if (!pages)
239 return start;
240
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +0200241 ++efi_memory_map_key;
Alexander Graf5d009952016-03-04 01:10:04 +0100242 newlist = calloc(1, sizeof(*newlist));
243 newlist->desc.type = memory_type;
244 newlist->desc.physical_start = start;
245 newlist->desc.virtual_start = start;
246 newlist->desc.num_pages = pages;
247
248 switch (memory_type) {
249 case EFI_RUNTIME_SERVICES_CODE:
250 case EFI_RUNTIME_SERVICES_DATA:
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200251 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
Alexander Graf5d009952016-03-04 01:10:04 +0100252 break;
253 case EFI_MMAP_IO:
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200254 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
Alexander Graf5d009952016-03-04 01:10:04 +0100255 break;
256 default:
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200257 newlist->desc.attribute = EFI_MEMORY_WB;
Alexander Graf5d009952016-03-04 01:10:04 +0100258 break;
259 }
260
261 /* Add our new map */
262 do {
Alexander Graf74c16ac2016-05-27 12:25:03 +0200263 carve_again = false;
Alexander Graf5d009952016-03-04 01:10:04 +0100264 list_for_each(lhandle, &efi_mem) {
265 struct efi_mem_list *lmem;
Heinrich Schuchardt32826142018-05-27 16:45:09 +0200266 s64 r;
Alexander Graf5d009952016-03-04 01:10:04 +0100267
268 lmem = list_entry(lhandle, struct efi_mem_list, link);
269 r = efi_mem_carve_out(lmem, &newlist->desc,
270 overlap_only_ram);
Alexander Graf74c16ac2016-05-27 12:25:03 +0200271 switch (r) {
272 case EFI_CARVE_OVERLAPS_NONRAM:
273 /*
274 * The user requested to only have RAM overlaps,
275 * but we hit a non-RAM region. Error out.
276 */
Alexander Graf5d009952016-03-04 01:10:04 +0100277 return 0;
Alexander Graf74c16ac2016-05-27 12:25:03 +0200278 case EFI_CARVE_NO_OVERLAP:
279 /* Just ignore this list entry */
280 break;
281 case EFI_CARVE_LOOP_AGAIN:
282 /*
283 * We split an entry, but need to loop through
284 * the list again to actually carve it.
285 */
286 carve_again = true;
287 break;
288 default:
289 /* We carved a number of pages */
290 carved_pages += r;
291 carve_again = true;
292 break;
293 }
294
295 if (carve_again) {
296 /* The list changed, we need to start over */
Alexander Graf5d009952016-03-04 01:10:04 +0100297 break;
298 }
299 }
Alexander Graf74c16ac2016-05-27 12:25:03 +0200300 } while (carve_again);
301
302 if (overlap_only_ram && (carved_pages != pages)) {
303 /*
304 * The payload wanted to have RAM overlaps, but we overlapped
305 * with an unallocated region. Error out.
306 */
307 return 0;
308 }
Alexander Graf5d009952016-03-04 01:10:04 +0100309
310 /* Add our new map */
311 list_add_tail(&newlist->link, &efi_mem);
312
Alexander Graf38ce65e2016-03-30 16:38:29 +0200313 /* And make sure memory is listed in descending order */
314 efi_mem_sort();
315
Alexander Graf5d009952016-03-04 01:10:04 +0100316 return start;
317}
318
319static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
320{
321 struct list_head *lhandle;
322
Alexander Grafc2e1ad72018-11-05 00:30:46 +0100323 /*
324 * Prealign input max address, so we simplify our matching
325 * logic below and can just reuse it as return pointer.
326 */
327 max_addr &= ~EFI_PAGE_MASK;
328
Alexander Graf5d009952016-03-04 01:10:04 +0100329 list_for_each(lhandle, &efi_mem) {
330 struct efi_mem_list *lmem = list_entry(lhandle,
331 struct efi_mem_list, link);
332 struct efi_mem_desc *desc = &lmem->desc;
333 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
334 uint64_t desc_end = desc->physical_start + desc_len;
335 uint64_t curmax = min(max_addr, desc_end);
336 uint64_t ret = curmax - len;
337
338 /* We only take memory from free RAM */
339 if (desc->type != EFI_CONVENTIONAL_MEMORY)
340 continue;
341
342 /* Out of bounds for max_addr */
343 if ((ret + len) > max_addr)
344 continue;
345
346 /* Out of bounds for upper map limit */
347 if ((ret + len) > desc_end)
348 continue;
349
350 /* Out of bounds for lower map limit */
351 if (ret < desc->physical_start)
352 continue;
353
354 /* Return the highest address in this map within bounds */
355 return ret;
356 }
357
358 return 0;
359}
360
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100361/*
362 * Allocate memory pages.
363 *
364 * @type type of allocation to be performed
365 * @memory_type usage type of the allocated memory
366 * @pages number of pages to be allocated
367 * @memory allocated memory
368 * @return status code
369 */
Alexander Graf5d009952016-03-04 01:10:04 +0100370efi_status_t efi_allocate_pages(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100371 efi_uintn_t pages, uint64_t *memory)
Alexander Graf5d009952016-03-04 01:10:04 +0100372{
373 u64 len = pages << EFI_PAGE_SHIFT;
374 efi_status_t r = EFI_SUCCESS;
375 uint64_t addr;
376
Heinrich Schuchardt4d5e0712018-07-02 12:53:53 +0200377 if (!memory)
378 return EFI_INVALID_PARAMETER;
379
Alexander Graf5d009952016-03-04 01:10:04 +0100380 switch (type) {
Heinrich Schuchardt7c92fd62018-01-30 21:08:00 +0100381 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf5d009952016-03-04 01:10:04 +0100382 /* Any page */
Stephen Warren14deb5e2018-08-30 15:43:45 -0600383 addr = efi_find_free_memory(len, -1ULL);
Alexander Graf5d009952016-03-04 01:10:04 +0100384 if (!addr) {
385 r = EFI_NOT_FOUND;
386 break;
387 }
388 break;
Heinrich Schuchardt7c92fd62018-01-30 21:08:00 +0100389 case EFI_ALLOCATE_MAX_ADDRESS:
Alexander Graf5d009952016-03-04 01:10:04 +0100390 /* Max address */
391 addr = efi_find_free_memory(len, *memory);
392 if (!addr) {
393 r = EFI_NOT_FOUND;
394 break;
395 }
396 break;
Heinrich Schuchardt7c92fd62018-01-30 21:08:00 +0100397 case EFI_ALLOCATE_ADDRESS:
Alexander Graf5d009952016-03-04 01:10:04 +0100398 /* Exact address, reserve it. The addr is already in *memory. */
399 addr = *memory;
400 break;
401 default:
402 /* UEFI doesn't specify other allocation types */
403 r = EFI_INVALID_PARAMETER;
404 break;
405 }
406
407 if (r == EFI_SUCCESS) {
408 uint64_t ret;
409
410 /* Reserve that map in our memory maps */
411 ret = efi_add_memory_map(addr, pages, memory_type, true);
412 if (ret == addr) {
Heinrich Schuchardt49759742018-11-18 17:58:46 +0100413 *memory = addr;
Alexander Graf5d009952016-03-04 01:10:04 +0100414 } else {
415 /* Map would overlap, bail out */
416 r = EFI_OUT_OF_RESOURCES;
417 }
418 }
419
420 return r;
421}
422
423void *efi_alloc(uint64_t len, int memory_type)
424{
425 uint64_t ret = 0;
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100426 uint64_t pages = efi_size_in_pages(len);
Alexander Graf5d009952016-03-04 01:10:04 +0100427 efi_status_t r;
428
Heinrich Schuchardte09159c2018-05-26 10:32:27 +0200429 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
430 &ret);
Alexander Graf5d009952016-03-04 01:10:04 +0100431 if (r == EFI_SUCCESS)
432 return (void*)(uintptr_t)ret;
433
434 return NULL;
435}
436
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100437/**
438 * efi_free_pages() - free memory pages
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100439 *
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100440 * @memory: start of the memory area to be freed
441 * @pages: number of pages to be freed
442 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100443 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100444efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf5d009952016-03-04 01:10:04 +0100445{
Stefan Brünsb61d8572016-10-01 23:32:27 +0200446 uint64_t r = 0;
447
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100448 /* Sanity check */
449 if (!memory || (memory & EFI_PAGE_MASK)) {
450 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
451 memory, pages);
452 return EFI_INVALID_PARAMETER;
453 }
454
Heinrich Schuchardt49759742018-11-18 17:58:46 +0100455 r = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false);
Stefan Brünsb61d8572016-10-01 23:32:27 +0200456 /* Merging of adjacent free regions is missing */
457
Heinrich Schuchardt49759742018-11-18 17:58:46 +0100458 if (r == memory)
Stefan Brünsb61d8572016-10-01 23:32:27 +0200459 return EFI_SUCCESS;
460
461 return EFI_NOT_FOUND;
Alexander Graf5d009952016-03-04 01:10:04 +0100462}
463
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100464/**
465 * efi_allocate_pool - allocate memory from pool
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100466 *
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100467 * @pool_type: type of the pool from which memory is to be allocated
468 * @size: number of bytes to be allocated
469 * @buffer: allocated memory
470 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100471 */
472efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
Stefan Brünsead12742016-10-09 22:17:18 +0200473{
474 efi_status_t r;
Heinrich Schuchardt306b1672019-03-18 20:01:59 +0100475 u64 addr;
Alexander Graf282a06c2018-06-18 17:23:15 +0200476 struct efi_pool_allocation *alloc;
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100477 u64 num_pages = efi_size_in_pages(size +
478 sizeof(struct efi_pool_allocation));
Stefan Brüns42417bc2016-10-09 22:17:26 +0200479
Heinrich Schuchardt4d5e0712018-07-02 12:53:53 +0200480 if (!buffer)
481 return EFI_INVALID_PARAMETER;
482
Stefan Brüns42417bc2016-10-09 22:17:26 +0200483 if (size == 0) {
484 *buffer = NULL;
485 return EFI_SUCCESS;
486 }
Stefan Brünsead12742016-10-09 22:17:18 +0200487
Heinrich Schuchardte09159c2018-05-26 10:32:27 +0200488 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Heinrich Schuchardt306b1672019-03-18 20:01:59 +0100489 &addr);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200490 if (r == EFI_SUCCESS) {
Heinrich Schuchardt306b1672019-03-18 20:01:59 +0100491 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
Stefan Brüns42417bc2016-10-09 22:17:26 +0200492 alloc->num_pages = num_pages;
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100493 alloc->checksum = checksum(alloc);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200494 *buffer = alloc->data;
495 }
496
497 return r;
498}
499
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100500/**
501 * efi_free_pool() - free memory from pool
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100502 *
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100503 * @buffer: start of memory to be freed
504 * Return: status code
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100505 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200506efi_status_t efi_free_pool(void *buffer)
507{
508 efi_status_t r;
509 struct efi_pool_allocation *alloc;
510
xypron.glpk@gmx.de71275a32017-07-14 19:12:39 +0200511 if (buffer == NULL)
512 return EFI_INVALID_PARAMETER;
513
Stefan Brüns42417bc2016-10-09 22:17:26 +0200514 alloc = container_of(buffer, struct efi_pool_allocation, data);
Heinrich Schuchardt2c3ec282019-03-26 05:31:41 +0100515
516 /* Check that this memory was allocated by efi_allocate_pool() */
517 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
518 alloc->checksum != checksum(alloc)) {
519 printf("%s: illegal free 0x%p\n", __func__, buffer);
520 return EFI_INVALID_PARAMETER;
521 }
522 /* Avoid double free */
523 alloc->checksum = 0;
Stefan Brüns42417bc2016-10-09 22:17:26 +0200524
525 r = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brünsead12742016-10-09 22:17:18 +0200526
527 return r;
528}
529
Heinrich Schuchardt474a6f52017-12-04 20:52:41 +0100530/*
531 * Get map describing memory usage.
532 *
533 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
534 * on exit the size of the copied memory map
535 * @memory_map buffer to which the memory map is written
536 * @map_key key for the memory map
537 * @descriptor_size size of an individual memory descriptor
538 * @descriptor_version version number of the memory descriptor structure
539 * @return status code
540 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100541efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
542 struct efi_mem_desc *memory_map,
543 efi_uintn_t *map_key,
544 efi_uintn_t *descriptor_size,
545 uint32_t *descriptor_version)
Alexander Graf5d009952016-03-04 01:10:04 +0100546{
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100547 efi_uintn_t map_size = 0;
Alexander Grafcee752f2016-04-11 23:51:02 +0200548 int map_entries = 0;
Alexander Graf5d009952016-03-04 01:10:04 +0100549 struct list_head *lhandle;
Heinrich Schuchardtfa995d0d2018-08-06 22:28:18 +0200550 efi_uintn_t provided_map_size;
Alexander Graf5d009952016-03-04 01:10:04 +0100551
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200552 if (!memory_map_size)
553 return EFI_INVALID_PARAMETER;
554
Heinrich Schuchardtfa995d0d2018-08-06 22:28:18 +0200555 provided_map_size = *memory_map_size;
556
Alexander Graf5d009952016-03-04 01:10:04 +0100557 list_for_each(lhandle, &efi_mem)
Alexander Grafcee752f2016-04-11 23:51:02 +0200558 map_entries++;
559
560 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf5d009952016-03-04 01:10:04 +0100561
Rob Clarka1b24822017-07-26 14:34:05 -0400562 *memory_map_size = map_size;
563
xypron.glpk@gmx.de0ecba5d2017-07-21 19:04:33 +0200564 if (provided_map_size < map_size)
565 return EFI_BUFFER_TOO_SMALL;
566
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200567 if (!memory_map)
568 return EFI_INVALID_PARAMETER;
569
Alexander Graf5d009952016-03-04 01:10:04 +0100570 if (descriptor_size)
571 *descriptor_size = sizeof(struct efi_mem_desc);
572
Mian Yousaf Kaukab4c02c112016-09-05 23:59:22 +0200573 if (descriptor_version)
574 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
575
Alexander Graf5d009952016-03-04 01:10:04 +0100576 /* Copy list into array */
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200577 /* Return the list in ascending order */
578 memory_map = &memory_map[map_entries - 1];
579 list_for_each(lhandle, &efi_mem) {
580 struct efi_mem_list *lmem;
Alexander Graf5d009952016-03-04 01:10:04 +0100581
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200582 lmem = list_entry(lhandle, struct efi_mem_list, link);
583 *memory_map = lmem->desc;
584 memory_map--;
Alexander Graf5d009952016-03-04 01:10:04 +0100585 }
586
Heinrich Schuchardt8e835552018-07-02 12:53:54 +0200587 if (map_key)
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +0200588 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dec6e3c3e2017-07-21 19:05:44 +0200589
Alexander Graf5d009952016-03-04 01:10:04 +0100590 return EFI_SUCCESS;
591}
592
York Sun42633742017-03-06 09:02:27 -0800593__weak void efi_add_known_memory(void)
Alexander Graf5d009952016-03-04 01:10:04 +0100594{
Alexander Graf7b78d642018-11-30 21:24:56 +0100595 u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
Alexander Graf5d009952016-03-04 01:10:04 +0100596 int i;
597
Heinrich Schuchardt23f5f4a2019-01-05 23:41:36 +0100598 /*
599 * ram_top is just outside mapped memory. So use an offset of one for
600 * mapping the sandbox address.
601 */
602 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
603
Alexander Graf7b78d642018-11-30 21:24:56 +0100604 /* Fix for 32bit targets with ram_top at 4G */
605 if (!ram_top)
606 ram_top = 0x100000000ULL;
607
Alexander Graf5d009952016-03-04 01:10:04 +0100608 /* Add RAM */
609 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Heinrich Schuchardt108bdff2018-11-12 18:55:24 +0100610 u64 ram_end, ram_start, pages;
Alexander Graf5d009952016-03-04 01:10:04 +0100611
Heinrich Schuchardt49759742018-11-18 17:58:46 +0100612 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
Heinrich Schuchardt108bdff2018-11-12 18:55:24 +0100613 ram_end = ram_start + gd->bd->bi_dram[i].size;
614
615 /* Remove partial pages */
616 ram_end &= ~EFI_PAGE_MASK;
617 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
618
Alexander Graf7b78d642018-11-30 21:24:56 +0100619 if (ram_end <= ram_start) {
620 /* Invalid mapping, keep going. */
621 continue;
622 }
Heinrich Schuchardt108bdff2018-11-12 18:55:24 +0100623
Alexander Graf7b78d642018-11-30 21:24:56 +0100624 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
625
626 efi_add_memory_map(ram_start, pages,
627 EFI_CONVENTIONAL_MEMORY, false);
628
629 /*
630 * Boards may indicate to the U-Boot memory core that they
631 * can not support memory above ram_top. Let's honor this
632 * in the efi_loader subsystem too by declaring any memory
633 * above ram_top as "already occupied by firmware".
634 */
635 if (ram_top < ram_start) {
636 /* ram_top is before this region, reserve all */
Heinrich Schuchardt108bdff2018-11-12 18:55:24 +0100637 efi_add_memory_map(ram_start, pages,
Alexander Graf7b78d642018-11-30 21:24:56 +0100638 EFI_BOOT_SERVICES_DATA, true);
639 } else if ((ram_top >= ram_start) && (ram_top < ram_end)) {
640 /* ram_top is inside this region, reserve parts */
641 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
642
643 efi_add_memory_map(ram_top, pages,
644 EFI_BOOT_SERVICES_DATA, true);
Heinrich Schuchardt108bdff2018-11-12 18:55:24 +0100645 }
Alexander Graf5d009952016-03-04 01:10:04 +0100646 }
York Sun42633742017-03-06 09:02:27 -0800647}
648
Simon Glass69259b82018-06-18 17:23:12 +0200649/* Add memory regions for U-Boot's memory and for the runtime services code */
650static void add_u_boot_and_runtime(void)
York Sun42633742017-03-06 09:02:27 -0800651{
652 unsigned long runtime_start, runtime_end, runtime_pages;
Alexander Graf7a82c302018-09-17 13:54:33 +0200653 unsigned long runtime_mask = EFI_PAGE_MASK;
York Sun42633742017-03-06 09:02:27 -0800654 unsigned long uboot_start, uboot_pages;
655 unsigned long uboot_stack_size = 16 * 1024 * 1024;
656
Alexander Graf5d009952016-03-04 01:10:04 +0100657 /* Add U-Boot */
658 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
659 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
660 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
661
Alexander Graf7a82c302018-09-17 13:54:33 +0200662#if defined(__aarch64__)
663 /*
664 * Runtime Services must be 64KiB aligned according to the
665 * "AArch64 Platforms" section in the UEFI spec (2.7+).
666 */
667
668 runtime_mask = SZ_64K - 1;
669#endif
670
671 /*
672 * Add Runtime Services. We mark surrounding boottime code as runtime as
673 * well to fulfill the runtime alignment constraints but avoid padding.
674 */
675 runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
Alexander Graf5d009952016-03-04 01:10:04 +0100676 runtime_end = (ulong)&__efi_runtime_stop;
Alexander Graf7a82c302018-09-17 13:54:33 +0200677 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
Alexander Graf5d009952016-03-04 01:10:04 +0100678 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
679 efi_add_memory_map(runtime_start, runtime_pages,
680 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass69259b82018-06-18 17:23:12 +0200681}
682
683int efi_memory_init(void)
684{
685 efi_add_known_memory();
686
687 if (!IS_ENABLED(CONFIG_SANDBOX))
688 add_u_boot_and_runtime();
Alexander Graf5d009952016-03-04 01:10:04 +0100689
Alexander Graf51735ae2016-05-11 18:25:48 +0200690#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
691 /* Request a 32bit 64MB bounce buffer region */
692 uint64_t efi_bounce_buffer_addr = 0xffffffff;
693
Heinrich Schuchardte09159c2018-05-26 10:32:27 +0200694 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
Alexander Graf51735ae2016-05-11 18:25:48 +0200695 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
696 &efi_bounce_buffer_addr) != EFI_SUCCESS)
697 return -1;
698
699 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
700#endif
701
Alexander Graf5d009952016-03-04 01:10:04 +0100702 return 0;
703}