blob: 386cf924fe26a3c457c60878de55d5dfcb60e544 [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>
10#include <malloc.h>
Alexander Grafcd405172018-06-18 17:23:15 +020011#include <mapmem.h>
Simon Glass3e852172018-03-08 21:53:27 +010012#include <watchdog.h>
Alexander Grafde2a13b2016-03-30 16:38:29 +020013#include <linux/list_sort.h>
Alexander Graf15937a22018-09-17 13:54:33 +020014#include <linux/sizes.h>
Alexander Graf8623f922016-03-04 01:10:04 +010015
16DECLARE_GLOBAL_DATA_PTR;
17
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010018/* Magic number identifying memory allocated from pool */
19#define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
20
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +020021efi_uintn_t efi_memory_map_key;
22
Alexander Graf8623f922016-03-04 01:10:04 +010023struct efi_mem_list {
24 struct list_head link;
25 struct efi_mem_desc desc;
26};
27
Alexander Graf66d86232016-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 Graf8623f922016-03-04 01:10:04 +010032/* This list contains all memory map items */
33LIST_HEAD(efi_mem);
34
Alexander Graf7c00a3c2016-05-11 18:25:48 +020035#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
36void *efi_bounce_buffer;
37#endif
38
Heinrich Schuchardtb4e2ba32019-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üns67b67d92016-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 Schuchardtb4e2ba32019-03-26 05:31:41 +010054 u64 checksum;
Rob Clarke597fd42017-09-13 18:05:36 -040055 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns67b67d92016-10-09 22:17:26 +020056};
57
Heinrich Schuchardtb4e2ba32019-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üns67b67d92016-10-09 22:17:26 +020074/*
Alexander Grafde2a13b2016-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 Grafd7288142018-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 Grafde2a13b2016-03-30 16:38:29 +020099static void efi_mem_sort(void)
100{
Alexander Grafd7288142018-09-16 17:05:29 +0200101 struct list_head *lhandle;
102 struct efi_mem_list *prevmem = NULL;
103 bool merge_again = true;
104
Alexander Grafde2a13b2016-03-30 16:38:29 +0200105 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Grafd7288142018-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 Grafde2a13b2016-03-30 16:38:29 +0200142}
143
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200144/** efi_mem_carve_out - unmap memory region
Alexander Graf8623f922016-03-04 01:10:04 +0100145 *
Heinrich Schuchardtf00caa52018-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üns1b00dc12016-10-01 23:32:23 +0200160 *
161 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200162 * to re-add the already carved out pages to the mapping.
Alexander Graf8623f922016-03-04 01:10:04 +0100163 */
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200164static s64 efi_mem_carve_out(struct efi_mem_list *map,
Alexander Graf8623f922016-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 Graf66d86232016-05-27 12:25:03 +0200178 return EFI_CARVE_NO_OVERLAP;
Alexander Graf8623f922016-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 Graf66d86232016-05-27 12:25:03 +0200182 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf8623f922016-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üns4e9ad8d2016-10-01 23:32:29 +0200193 free(map);
194 } else {
195 map->desc.physical_start = carve_end;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200196 map->desc.virtual_start = carve_end;
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200197 map->desc.num_pages = (map_end - carve_end)
198 >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100199 }
200
Alexander Graf66d86232016-05-27 12:25:03 +0200201 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100202 }
203
204 /*
205 * Overlapping maps, just split the list map at carve_start,
206 * it will get moved or removed in the next iteration.
207 *
208 * [ map_desc |__carve_start__| newmap ]
209 */
210
211 /* Create a new map from [ carve_start ... map_end ] */
212 newmap = calloc(1, sizeof(*newmap));
213 newmap->desc = map->desc;
214 newmap->desc.physical_start = carve_start;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200215 newmap->desc.virtual_start = carve_start;
Alexander Graf8623f922016-03-04 01:10:04 +0100216 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
Stefan Brüns9d656512016-10-01 23:32:28 +0200217 /* Insert before current entry (descending address order) */
218 list_add_tail(&newmap->link, &map->link);
Alexander Graf8623f922016-03-04 01:10:04 +0100219
220 /* Shrink the map to [ map_start ... carve_start ] */
221 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
222
Alexander Graf66d86232016-05-27 12:25:03 +0200223 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf8623f922016-03-04 01:10:04 +0100224}
225
226uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
227 bool overlap_only_ram)
228{
229 struct list_head *lhandle;
230 struct efi_mem_list *newlist;
Alexander Graf66d86232016-05-27 12:25:03 +0200231 bool carve_again;
232 uint64_t carved_pages = 0;
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200233 struct efi_event *evt;
Alexander Graf8623f922016-03-04 01:10:04 +0100234
Heinrich Schuchardt706a2fe2019-04-04 21:50:02 +0200235 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
236 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
Andreas Färber102f8bd2016-07-17 06:57:11 +0200237
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200238 if (memory_type >= EFI_MAX_MEMORY_TYPE)
239 return EFI_INVALID_PARAMETER;
240
Alexander Graf8623f922016-03-04 01:10:04 +0100241 if (!pages)
242 return start;
243
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200244 ++efi_memory_map_key;
Alexander Graf8623f922016-03-04 01:10:04 +0100245 newlist = calloc(1, sizeof(*newlist));
246 newlist->desc.type = memory_type;
247 newlist->desc.physical_start = start;
248 newlist->desc.virtual_start = start;
249 newlist->desc.num_pages = pages;
250
251 switch (memory_type) {
252 case EFI_RUNTIME_SERVICES_CODE:
253 case EFI_RUNTIME_SERVICES_DATA:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200254 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100255 break;
256 case EFI_MMAP_IO:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200257 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100258 break;
259 default:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200260 newlist->desc.attribute = EFI_MEMORY_WB;
Alexander Graf8623f922016-03-04 01:10:04 +0100261 break;
262 }
263
264 /* Add our new map */
265 do {
Alexander Graf66d86232016-05-27 12:25:03 +0200266 carve_again = false;
Alexander Graf8623f922016-03-04 01:10:04 +0100267 list_for_each(lhandle, &efi_mem) {
268 struct efi_mem_list *lmem;
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200269 s64 r;
Alexander Graf8623f922016-03-04 01:10:04 +0100270
271 lmem = list_entry(lhandle, struct efi_mem_list, link);
272 r = efi_mem_carve_out(lmem, &newlist->desc,
273 overlap_only_ram);
Alexander Graf66d86232016-05-27 12:25:03 +0200274 switch (r) {
275 case EFI_CARVE_OVERLAPS_NONRAM:
276 /*
277 * The user requested to only have RAM overlaps,
278 * but we hit a non-RAM region. Error out.
279 */
Alexander Graf8623f922016-03-04 01:10:04 +0100280 return 0;
Alexander Graf66d86232016-05-27 12:25:03 +0200281 case EFI_CARVE_NO_OVERLAP:
282 /* Just ignore this list entry */
283 break;
284 case EFI_CARVE_LOOP_AGAIN:
285 /*
286 * We split an entry, but need to loop through
287 * the list again to actually carve it.
288 */
289 carve_again = true;
290 break;
291 default:
292 /* We carved a number of pages */
293 carved_pages += r;
294 carve_again = true;
Alexander Graf8623f922016-03-04 01:10:04 +0100295 break;
296 }
Alexander Graf66d86232016-05-27 12:25:03 +0200297
298 if (carve_again) {
299 /* The list changed, we need to start over */
300 break;
301 }
Alexander Graf8623f922016-03-04 01:10:04 +0100302 }
Alexander Graf66d86232016-05-27 12:25:03 +0200303 } while (carve_again);
304
305 if (overlap_only_ram && (carved_pages != pages)) {
306 /*
307 * The payload wanted to have RAM overlaps, but we overlapped
308 * with an unallocated region. Error out.
309 */
310 return 0;
311 }
Alexander Graf8623f922016-03-04 01:10:04 +0100312
313 /* Add our new map */
314 list_add_tail(&newlist->link, &efi_mem);
315
Alexander Grafde2a13b2016-03-30 16:38:29 +0200316 /* And make sure memory is listed in descending order */
317 efi_mem_sort();
318
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200319 /* Notify that the memory map was changed */
320 list_for_each_entry(evt, &efi_events, link) {
321 if (evt->group &&
322 !guidcmp(evt->group,
323 &efi_guid_event_group_memory_map_change)) {
324 efi_signal_event(evt, false);
325 break;
326 }
327 }
328
Alexander Graf8623f922016-03-04 01:10:04 +0100329 return start;
330}
331
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200332/**
333 * efi_check_allocated() - validate address to be freed
334 *
335 * Check that the address is within allocated memory:
336 *
337 * * The address cannot be NULL.
338 * * The address must be in a range of the memory map.
339 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
340 *
341 * Page alignment is not checked as this is not a requirement of
342 * efi_free_pool().
343 *
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200344 * @addr: address of page to be freed
345 * @must_be_allocated: return success if the page is allocated
346 * Return: status code
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200347 */
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200348static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200349{
350 struct efi_mem_list *item;
351
352 if (!addr)
353 return EFI_INVALID_PARAMETER;
354 list_for_each_entry(item, &efi_mem, link) {
355 u64 start = item->desc.physical_start;
356 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
357
358 if (addr >= start && addr < end) {
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200359 if (must_be_allocated ^
360 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200361 return EFI_SUCCESS;
362 else
363 return EFI_NOT_FOUND;
364 }
365 }
366
367 return EFI_NOT_FOUND;
368}
369
Alexander Graf8623f922016-03-04 01:10:04 +0100370static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
371{
372 struct list_head *lhandle;
373
Alexander Grafc3b2a252018-11-05 00:30:46 +0100374 /*
375 * Prealign input max address, so we simplify our matching
376 * logic below and can just reuse it as return pointer.
377 */
378 max_addr &= ~EFI_PAGE_MASK;
379
Alexander Graf8623f922016-03-04 01:10:04 +0100380 list_for_each(lhandle, &efi_mem) {
381 struct efi_mem_list *lmem = list_entry(lhandle,
382 struct efi_mem_list, link);
383 struct efi_mem_desc *desc = &lmem->desc;
384 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
385 uint64_t desc_end = desc->physical_start + desc_len;
386 uint64_t curmax = min(max_addr, desc_end);
387 uint64_t ret = curmax - len;
388
389 /* We only take memory from free RAM */
390 if (desc->type != EFI_CONVENTIONAL_MEMORY)
391 continue;
392
393 /* Out of bounds for max_addr */
394 if ((ret + len) > max_addr)
395 continue;
396
397 /* Out of bounds for upper map limit */
398 if ((ret + len) > desc_end)
399 continue;
400
401 /* Out of bounds for lower map limit */
402 if (ret < desc->physical_start)
403 continue;
404
405 /* Return the highest address in this map within bounds */
406 return ret;
407 }
408
409 return 0;
410}
411
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100412/*
413 * Allocate memory pages.
414 *
415 * @type type of allocation to be performed
416 * @memory_type usage type of the allocated memory
417 * @pages number of pages to be allocated
418 * @memory allocated memory
419 * @return status code
420 */
Alexander Graf8623f922016-03-04 01:10:04 +0100421efi_status_t efi_allocate_pages(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100422 efi_uintn_t pages, uint64_t *memory)
Alexander Graf8623f922016-03-04 01:10:04 +0100423{
424 u64 len = pages << EFI_PAGE_SHIFT;
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200425 efi_status_t ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100426 uint64_t addr;
427
Heinrich Schuchardtf7272332019-04-23 00:30:53 +0200428 /* Check import parameters */
429 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
430 memory_type <= 0x6FFFFFFF)
431 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200432 if (!memory)
433 return EFI_INVALID_PARAMETER;
434
Alexander Graf8623f922016-03-04 01:10:04 +0100435 switch (type) {
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100436 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf8623f922016-03-04 01:10:04 +0100437 /* Any page */
Stephen Warren66208b92018-08-30 15:43:45 -0600438 addr = efi_find_free_memory(len, -1ULL);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200439 if (!addr)
440 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100441 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100442 case EFI_ALLOCATE_MAX_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100443 /* Max address */
444 addr = efi_find_free_memory(len, *memory);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200445 if (!addr)
446 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100447 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100448 case EFI_ALLOCATE_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100449 /* Exact address, reserve it. The addr is already in *memory. */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200450 ret = efi_check_allocated(*memory, false);
451 if (ret != EFI_SUCCESS)
452 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100453 addr = *memory;
454 break;
455 default:
456 /* UEFI doesn't specify other allocation types */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200457 return EFI_INVALID_PARAMETER;
Alexander Graf8623f922016-03-04 01:10:04 +0100458 }
459
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200460 /* Reserve that map in our memory maps */
461 if (efi_add_memory_map(addr, pages, memory_type, true) != addr)
462 /* Map would overlap, bail out */
463 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100464
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200465 *memory = addr;
Alexander Graf8623f922016-03-04 01:10:04 +0100466
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200467 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100468}
469
470void *efi_alloc(uint64_t len, int memory_type)
471{
472 uint64_t ret = 0;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100473 uint64_t pages = efi_size_in_pages(len);
Alexander Graf8623f922016-03-04 01:10:04 +0100474 efi_status_t r;
475
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200476 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
477 &ret);
Alexander Graf8623f922016-03-04 01:10:04 +0100478 if (r == EFI_SUCCESS)
479 return (void*)(uintptr_t)ret;
480
481 return NULL;
482}
483
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100484/**
485 * efi_free_pages() - free memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100486 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100487 * @memory: start of the memory area to be freed
488 * @pages: number of pages to be freed
489 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100490 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100491efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf8623f922016-03-04 01:10:04 +0100492{
Stefan Brünsa4f43022016-10-01 23:32:27 +0200493 uint64_t r = 0;
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200494 efi_status_t ret;
495
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200496 ret = efi_check_allocated(memory, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200497 if (ret != EFI_SUCCESS)
498 return ret;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200499
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100500 /* Sanity check */
Heinrich Schuchardt7bf4bc92019-04-25 18:41:40 +0200501 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100502 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
503 memory, pages);
504 return EFI_INVALID_PARAMETER;
505 }
506
Heinrich Schuchardt6738fcd2018-11-18 17:58:46 +0100507 r = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false);
Stefan Brünsa4f43022016-10-01 23:32:27 +0200508 /* Merging of adjacent free regions is missing */
509
Heinrich Schuchardt6738fcd2018-11-18 17:58:46 +0100510 if (r == memory)
Stefan Brünsa4f43022016-10-01 23:32:27 +0200511 return EFI_SUCCESS;
512
513 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100514}
515
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100516/**
517 * efi_allocate_pool - allocate memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100518 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100519 * @pool_type: type of the pool from which memory is to be allocated
520 * @size: number of bytes to be allocated
521 * @buffer: allocated memory
522 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100523 */
524efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200525{
526 efi_status_t r;
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100527 u64 addr;
Alexander Grafcd405172018-06-18 17:23:15 +0200528 struct efi_pool_allocation *alloc;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100529 u64 num_pages = efi_size_in_pages(size +
530 sizeof(struct efi_pool_allocation));
Stefan Brüns67b67d92016-10-09 22:17:26 +0200531
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200532 if (!buffer)
533 return EFI_INVALID_PARAMETER;
534
Stefan Brüns67b67d92016-10-09 22:17:26 +0200535 if (size == 0) {
536 *buffer = NULL;
537 return EFI_SUCCESS;
538 }
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200539
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200540 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100541 &addr);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200542 if (r == EFI_SUCCESS) {
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100543 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200544 alloc->num_pages = num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100545 alloc->checksum = checksum(alloc);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200546 *buffer = alloc->data;
547 }
548
549 return r;
550}
551
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100552/**
553 * efi_free_pool() - free memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100554 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100555 * @buffer: start of memory to be freed
556 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100557 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200558efi_status_t efi_free_pool(void *buffer)
559{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200560 efi_status_t ret;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200561 struct efi_pool_allocation *alloc;
562
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200563 ret = efi_check_allocated((uintptr_t)buffer, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200564 if (ret != EFI_SUCCESS)
565 return ret;
xypron.glpk@gmx.de34ddae72017-07-14 19:12:39 +0200566
Stefan Brüns67b67d92016-10-09 22:17:26 +0200567 alloc = container_of(buffer, struct efi_pool_allocation, data);
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100568
569 /* Check that this memory was allocated by efi_allocate_pool() */
570 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
571 alloc->checksum != checksum(alloc)) {
572 printf("%s: illegal free 0x%p\n", __func__, buffer);
573 return EFI_INVALID_PARAMETER;
574 }
575 /* Avoid double free */
576 alloc->checksum = 0;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200577
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200578 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200579
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200580 return ret;
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200581}
582
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100583/*
584 * Get map describing memory usage.
585 *
586 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
587 * on exit the size of the copied memory map
588 * @memory_map buffer to which the memory map is written
589 * @map_key key for the memory map
590 * @descriptor_size size of an individual memory descriptor
591 * @descriptor_version version number of the memory descriptor structure
592 * @return status code
593 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100594efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
595 struct efi_mem_desc *memory_map,
596 efi_uintn_t *map_key,
597 efi_uintn_t *descriptor_size,
598 uint32_t *descriptor_version)
Alexander Graf8623f922016-03-04 01:10:04 +0100599{
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100600 efi_uintn_t map_size = 0;
Alexander Graf9b591022016-04-11 23:51:02 +0200601 int map_entries = 0;
Alexander Graf8623f922016-03-04 01:10:04 +0100602 struct list_head *lhandle;
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200603 efi_uintn_t provided_map_size;
Alexander Graf8623f922016-03-04 01:10:04 +0100604
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200605 if (!memory_map_size)
606 return EFI_INVALID_PARAMETER;
607
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200608 provided_map_size = *memory_map_size;
609
Alexander Graf8623f922016-03-04 01:10:04 +0100610 list_for_each(lhandle, &efi_mem)
Alexander Graf9b591022016-04-11 23:51:02 +0200611 map_entries++;
612
613 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf8623f922016-03-04 01:10:04 +0100614
Rob Clarka84c9742017-07-26 14:34:05 -0400615 *memory_map_size = map_size;
616
xypron.glpk@gmx.de5487ab52017-07-21 19:04:33 +0200617 if (provided_map_size < map_size)
618 return EFI_BUFFER_TOO_SMALL;
619
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200620 if (!memory_map)
621 return EFI_INVALID_PARAMETER;
622
Alexander Graf8623f922016-03-04 01:10:04 +0100623 if (descriptor_size)
624 *descriptor_size = sizeof(struct efi_mem_desc);
625
Mian Yousaf Kaukab338cf562016-09-05 23:59:22 +0200626 if (descriptor_version)
627 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
628
Alexander Graf8623f922016-03-04 01:10:04 +0100629 /* Copy list into array */
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200630 /* Return the list in ascending order */
631 memory_map = &memory_map[map_entries - 1];
632 list_for_each(lhandle, &efi_mem) {
633 struct efi_mem_list *lmem;
Alexander Graf8623f922016-03-04 01:10:04 +0100634
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200635 lmem = list_entry(lhandle, struct efi_mem_list, link);
636 *memory_map = lmem->desc;
637 memory_map--;
Alexander Graf8623f922016-03-04 01:10:04 +0100638 }
639
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200640 if (map_key)
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200641 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dee436e632017-07-21 19:05:44 +0200642
Alexander Graf8623f922016-03-04 01:10:04 +0100643 return EFI_SUCCESS;
644}
645
York Suna8e9f0a2017-03-06 09:02:27 -0800646__weak void efi_add_known_memory(void)
Alexander Graf8623f922016-03-04 01:10:04 +0100647{
Alexander Grafc214edf2018-11-30 21:24:56 +0100648 u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
Alexander Graf8623f922016-03-04 01:10:04 +0100649 int i;
650
Heinrich Schuchardteb8edcf2019-01-05 23:41:36 +0100651 /*
652 * ram_top is just outside mapped memory. So use an offset of one for
653 * mapping the sandbox address.
654 */
655 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
656
Alexander Grafc214edf2018-11-30 21:24:56 +0100657 /* Fix for 32bit targets with ram_top at 4G */
658 if (!ram_top)
659 ram_top = 0x100000000ULL;
660
Alexander Graf8623f922016-03-04 01:10:04 +0100661 /* Add RAM */
662 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100663 u64 ram_end, ram_start, pages;
664
Heinrich Schuchardt6738fcd2018-11-18 17:58:46 +0100665 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100666 ram_end = ram_start + gd->bd->bi_dram[i].size;
667
668 /* Remove partial pages */
669 ram_end &= ~EFI_PAGE_MASK;
670 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
671
Alexander Grafc214edf2018-11-30 21:24:56 +0100672 if (ram_end <= ram_start) {
673 /* Invalid mapping, keep going. */
674 continue;
675 }
676
677 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100678
Alexander Grafc214edf2018-11-30 21:24:56 +0100679 efi_add_memory_map(ram_start, pages,
680 EFI_CONVENTIONAL_MEMORY, false);
681
682 /*
683 * Boards may indicate to the U-Boot memory core that they
684 * can not support memory above ram_top. Let's honor this
685 * in the efi_loader subsystem too by declaring any memory
686 * above ram_top as "already occupied by firmware".
687 */
688 if (ram_top < ram_start) {
689 /* ram_top is before this region, reserve all */
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100690 efi_add_memory_map(ram_start, pages,
Alexander Grafc214edf2018-11-30 21:24:56 +0100691 EFI_BOOT_SERVICES_DATA, true);
692 } else if ((ram_top >= ram_start) && (ram_top < ram_end)) {
693 /* ram_top is inside this region, reserve parts */
694 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
695
696 efi_add_memory_map(ram_top, pages,
697 EFI_BOOT_SERVICES_DATA, true);
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100698 }
Alexander Graf8623f922016-03-04 01:10:04 +0100699 }
York Suna8e9f0a2017-03-06 09:02:27 -0800700}
701
Simon Glass78639432018-06-18 17:23:12 +0200702/* Add memory regions for U-Boot's memory and for the runtime services code */
703static void add_u_boot_and_runtime(void)
York Suna8e9f0a2017-03-06 09:02:27 -0800704{
705 unsigned long runtime_start, runtime_end, runtime_pages;
Alexander Graf15937a22018-09-17 13:54:33 +0200706 unsigned long runtime_mask = EFI_PAGE_MASK;
York Suna8e9f0a2017-03-06 09:02:27 -0800707 unsigned long uboot_start, uboot_pages;
708 unsigned long uboot_stack_size = 16 * 1024 * 1024;
709
Alexander Graf8623f922016-03-04 01:10:04 +0100710 /* Add U-Boot */
711 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
712 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
713 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
714
Alexander Graf15937a22018-09-17 13:54:33 +0200715#if defined(__aarch64__)
716 /*
717 * Runtime Services must be 64KiB aligned according to the
718 * "AArch64 Platforms" section in the UEFI spec (2.7+).
719 */
720
721 runtime_mask = SZ_64K - 1;
722#endif
723
724 /*
725 * Add Runtime Services. We mark surrounding boottime code as runtime as
726 * well to fulfill the runtime alignment constraints but avoid padding.
727 */
728 runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100729 runtime_end = (ulong)&__efi_runtime_stop;
Alexander Graf15937a22018-09-17 13:54:33 +0200730 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100731 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
732 efi_add_memory_map(runtime_start, runtime_pages,
733 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass78639432018-06-18 17:23:12 +0200734}
735
736int efi_memory_init(void)
737{
738 efi_add_known_memory();
739
740 if (!IS_ENABLED(CONFIG_SANDBOX))
741 add_u_boot_and_runtime();
Alexander Graf8623f922016-03-04 01:10:04 +0100742
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200743#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
744 /* Request a 32bit 64MB bounce buffer region */
745 uint64_t efi_bounce_buffer_addr = 0xffffffff;
746
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200747 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200748 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
749 &efi_bounce_buffer_addr) != EFI_SUCCESS)
750 return -1;
751
752 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
753#endif
754
Alexander Graf8623f922016-03-04 01:10:04 +0100755 return 0;
756}