blob: d46001f608bc59e8ee9bf7a21908bdb110887162 [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/**
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020040 * struct efi_pool_allocation - memory block allocated from pool
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010041 *
42 * @num_pages: number of pages allocated
43 * @checksum: checksum
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020044 * @data: allocated pool memory
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010045 *
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020046 * U-Boot services each UEFI AllocatePool() request as a separate
47 * (multiple) page allocation. We have to track the number of pages
Stefan Brüns67b67d92016-10-09 22:17:26 +020048 * to be able to free the correct amount later.
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020049 *
50 * The checksum calculated in function checksum() is used in FreePool() to avoid
51 * freeing memory not allocated by AllocatePool() and duplicate freeing.
52 *
Stefan Brüns67b67d92016-10-09 22:17:26 +020053 * EFI requires 8 byte alignment for pool allocations, so we can
Heinrich Schuchardt79f882a2019-07-14 12:28:56 +020054 * prepend each allocation with these header fields.
Stefan Brüns67b67d92016-10-09 22:17:26 +020055 */
56struct efi_pool_allocation {
57 u64 num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010058 u64 checksum;
Rob Clarke597fd42017-09-13 18:05:36 -040059 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns67b67d92016-10-09 22:17:26 +020060};
61
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +010062/**
63 * checksum() - calculate checksum for memory allocated from pool
64 *
65 * @alloc: allocation header
66 * Return: checksum, always non-zero
67 */
68static u64 checksum(struct efi_pool_allocation *alloc)
69{
70 u64 addr = (uintptr_t)alloc;
71 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
72 EFI_ALLOC_POOL_MAGIC;
73 if (!ret)
74 ++ret;
75 return ret;
76}
77
Stefan Brüns67b67d92016-10-09 22:17:26 +020078/*
Alexander Grafde2a13b2016-03-30 16:38:29 +020079 * Sorts the memory list from highest address to lowest address
80 *
81 * When allocating memory we should always start from the highest
82 * address chunk, so sort the memory list such that the first list
83 * iterator gets the highest address and goes lower from there.
84 */
85static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
86{
87 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
88 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
89
90 if (mema->desc.physical_start == memb->desc.physical_start)
91 return 0;
92 else if (mema->desc.physical_start < memb->desc.physical_start)
93 return 1;
94 else
95 return -1;
96}
97
Alexander Grafd7288142018-09-16 17:05:29 +020098static uint64_t desc_get_end(struct efi_mem_desc *desc)
99{
100 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
101}
102
Alexander Grafde2a13b2016-03-30 16:38:29 +0200103static void efi_mem_sort(void)
104{
Alexander Grafd7288142018-09-16 17:05:29 +0200105 struct list_head *lhandle;
106 struct efi_mem_list *prevmem = NULL;
107 bool merge_again = true;
108
Alexander Grafde2a13b2016-03-30 16:38:29 +0200109 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Grafd7288142018-09-16 17:05:29 +0200110
111 /* Now merge entries that can be merged */
112 while (merge_again) {
113 merge_again = false;
114 list_for_each(lhandle, &efi_mem) {
115 struct efi_mem_list *lmem;
116 struct efi_mem_desc *prev = &prevmem->desc;
117 struct efi_mem_desc *cur;
118 uint64_t pages;
119
120 lmem = list_entry(lhandle, struct efi_mem_list, link);
121 if (!prevmem) {
122 prevmem = lmem;
123 continue;
124 }
125
126 cur = &lmem->desc;
127
128 if ((desc_get_end(cur) == prev->physical_start) &&
129 (prev->type == cur->type) &&
130 (prev->attribute == cur->attribute)) {
131 /* There is an existing map before, reuse it */
132 pages = cur->num_pages;
133 prev->num_pages += pages;
134 prev->physical_start -= pages << EFI_PAGE_SHIFT;
135 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
136 list_del(&lmem->link);
137 free(lmem);
138
139 merge_again = true;
140 break;
141 }
142
143 prevmem = lmem;
144 }
145 }
Alexander Grafde2a13b2016-03-30 16:38:29 +0200146}
147
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200148/** efi_mem_carve_out - unmap memory region
Alexander Graf8623f922016-03-04 01:10:04 +0100149 *
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200150 * @map: memory map
151 * @carve_desc: memory region to unmap
152 * @overlap_only_ram: the carved out region may only overlap RAM
153 * Return Value: the number of overlapping pages which have been
154 * removed from the map,
155 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
156 * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
157 * and the map contains anything but free ram
158 * (only when overlap_only_ram is true),
159 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
160 * traversed again, as it has been altered.
161 *
162 * Unmaps all memory occupied by the carve_desc region from the list entry
163 * pointed to by map.
Stefan Brüns1b00dc12016-10-01 23:32:23 +0200164 *
165 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200166 * to re-add the already carved out pages to the mapping.
Alexander Graf8623f922016-03-04 01:10:04 +0100167 */
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200168static s64 efi_mem_carve_out(struct efi_mem_list *map,
Alexander Graf8623f922016-03-04 01:10:04 +0100169 struct efi_mem_desc *carve_desc,
170 bool overlap_only_ram)
171{
172 struct efi_mem_list *newmap;
173 struct efi_mem_desc *map_desc = &map->desc;
174 uint64_t map_start = map_desc->physical_start;
175 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
176 uint64_t carve_start = carve_desc->physical_start;
177 uint64_t carve_end = carve_start +
178 (carve_desc->num_pages << EFI_PAGE_SHIFT);
179
180 /* check whether we're overlapping */
181 if ((carve_end <= map_start) || (carve_start >= map_end))
Alexander Graf66d86232016-05-27 12:25:03 +0200182 return EFI_CARVE_NO_OVERLAP;
Alexander Graf8623f922016-03-04 01:10:04 +0100183
184 /* We're overlapping with non-RAM, warn the caller if desired */
185 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
Alexander Graf66d86232016-05-27 12:25:03 +0200186 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf8623f922016-03-04 01:10:04 +0100187
188 /* Sanitize carve_start and carve_end to lie within our bounds */
189 carve_start = max(carve_start, map_start);
190 carve_end = min(carve_end, map_end);
191
192 /* Carving at the beginning of our map? Just move it! */
193 if (carve_start == map_start) {
194 if (map_end == carve_end) {
195 /* Full overlap, just remove map */
196 list_del(&map->link);
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200197 free(map);
198 } else {
199 map->desc.physical_start = carve_end;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200200 map->desc.virtual_start = carve_end;
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200201 map->desc.num_pages = (map_end - carve_end)
202 >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100203 }
204
Alexander Graf66d86232016-05-27 12:25:03 +0200205 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100206 }
207
208 /*
209 * Overlapping maps, just split the list map at carve_start,
210 * it will get moved or removed in the next iteration.
211 *
212 * [ map_desc |__carve_start__| newmap ]
213 */
214
215 /* Create a new map from [ carve_start ... map_end ] */
216 newmap = calloc(1, sizeof(*newmap));
217 newmap->desc = map->desc;
218 newmap->desc.physical_start = carve_start;
Heinrich Schuchardte83aafa2019-04-11 20:08:54 +0200219 newmap->desc.virtual_start = carve_start;
Alexander Graf8623f922016-03-04 01:10:04 +0100220 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
Stefan Brüns9d656512016-10-01 23:32:28 +0200221 /* Insert before current entry (descending address order) */
222 list_add_tail(&newmap->link, &map->link);
Alexander Graf8623f922016-03-04 01:10:04 +0100223
224 /* Shrink the map to [ map_start ... carve_start ] */
225 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
226
Alexander Graf66d86232016-05-27 12:25:03 +0200227 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf8623f922016-03-04 01:10:04 +0100228}
229
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100230/**
231 * efi_add_memory_map() - add memory area to the memory map
232 *
233 * @start: start address, must be a multiple of EFI_PAGE_SIZE
234 * @pages: number of pages to add
235 * @memory_type: type of memory added
236 * @overlap_only_ram: the memory area must overlap existing
237 * Return: status code
238 */
239efi_status_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
240 bool overlap_only_ram)
Alexander Graf8623f922016-03-04 01:10:04 +0100241{
242 struct list_head *lhandle;
243 struct efi_mem_list *newlist;
Alexander Graf66d86232016-05-27 12:25:03 +0200244 bool carve_again;
245 uint64_t carved_pages = 0;
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200246 struct efi_event *evt;
Alexander Graf8623f922016-03-04 01:10:04 +0100247
Heinrich Schuchardt706a2fe2019-04-04 21:50:02 +0200248 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
249 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
Andreas Färber102f8bd2016-07-17 06:57:11 +0200250
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200251 if (memory_type >= EFI_MAX_MEMORY_TYPE)
252 return EFI_INVALID_PARAMETER;
253
Alexander Graf8623f922016-03-04 01:10:04 +0100254 if (!pages)
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100255 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100256
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200257 ++efi_memory_map_key;
Alexander Graf8623f922016-03-04 01:10:04 +0100258 newlist = calloc(1, sizeof(*newlist));
259 newlist->desc.type = memory_type;
260 newlist->desc.physical_start = start;
261 newlist->desc.virtual_start = start;
262 newlist->desc.num_pages = pages;
263
264 switch (memory_type) {
265 case EFI_RUNTIME_SERVICES_CODE:
266 case EFI_RUNTIME_SERVICES_DATA:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200267 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100268 break;
269 case EFI_MMAP_IO:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200270 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100271 break;
272 default:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200273 newlist->desc.attribute = EFI_MEMORY_WB;
Alexander Graf8623f922016-03-04 01:10:04 +0100274 break;
275 }
276
277 /* Add our new map */
278 do {
Alexander Graf66d86232016-05-27 12:25:03 +0200279 carve_again = false;
Alexander Graf8623f922016-03-04 01:10:04 +0100280 list_for_each(lhandle, &efi_mem) {
281 struct efi_mem_list *lmem;
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200282 s64 r;
Alexander Graf8623f922016-03-04 01:10:04 +0100283
284 lmem = list_entry(lhandle, struct efi_mem_list, link);
285 r = efi_mem_carve_out(lmem, &newlist->desc,
286 overlap_only_ram);
Alexander Graf66d86232016-05-27 12:25:03 +0200287 switch (r) {
288 case EFI_CARVE_OVERLAPS_NONRAM:
289 /*
290 * The user requested to only have RAM overlaps,
291 * but we hit a non-RAM region. Error out.
292 */
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100293 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200294 case EFI_CARVE_NO_OVERLAP:
295 /* Just ignore this list entry */
296 break;
297 case EFI_CARVE_LOOP_AGAIN:
298 /*
299 * We split an entry, but need to loop through
300 * the list again to actually carve it.
301 */
302 carve_again = true;
303 break;
304 default:
305 /* We carved a number of pages */
306 carved_pages += r;
307 carve_again = true;
Alexander Graf8623f922016-03-04 01:10:04 +0100308 break;
309 }
Alexander Graf66d86232016-05-27 12:25:03 +0200310
311 if (carve_again) {
312 /* The list changed, we need to start over */
313 break;
314 }
Alexander Graf8623f922016-03-04 01:10:04 +0100315 }
Alexander Graf66d86232016-05-27 12:25:03 +0200316 } while (carve_again);
317
318 if (overlap_only_ram && (carved_pages != pages)) {
319 /*
320 * The payload wanted to have RAM overlaps, but we overlapped
321 * with an unallocated region. Error out.
322 */
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100323 return EFI_NO_MAPPING;
Alexander Graf66d86232016-05-27 12:25:03 +0200324 }
Alexander Graf8623f922016-03-04 01:10:04 +0100325
326 /* Add our new map */
327 list_add_tail(&newlist->link, &efi_mem);
328
Alexander Grafde2a13b2016-03-30 16:38:29 +0200329 /* And make sure memory is listed in descending order */
330 efi_mem_sort();
331
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200332 /* Notify that the memory map was changed */
333 list_for_each_entry(evt, &efi_events, link) {
334 if (evt->group &&
335 !guidcmp(evt->group,
336 &efi_guid_event_group_memory_map_change)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200337 efi_signal_event(evt);
Heinrich Schuchardt485975b2019-06-04 20:55:12 +0200338 break;
339 }
340 }
341
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100342 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100343}
344
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200345/**
346 * efi_check_allocated() - validate address to be freed
347 *
348 * Check that the address is within allocated memory:
349 *
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200350 * * The address must be in a range of the memory map.
351 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
352 *
353 * Page alignment is not checked as this is not a requirement of
354 * efi_free_pool().
355 *
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200356 * @addr: address of page to be freed
357 * @must_be_allocated: return success if the page is allocated
358 * Return: status code
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200359 */
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200360static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200361{
362 struct efi_mem_list *item;
363
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200364 list_for_each_entry(item, &efi_mem, link) {
365 u64 start = item->desc.physical_start;
366 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
367
368 if (addr >= start && addr < end) {
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200369 if (must_be_allocated ^
370 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200371 return EFI_SUCCESS;
372 else
373 return EFI_NOT_FOUND;
374 }
375 }
376
377 return EFI_NOT_FOUND;
378}
379
Alexander Graf8623f922016-03-04 01:10:04 +0100380static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
381{
382 struct list_head *lhandle;
383
Alexander Grafc3b2a252018-11-05 00:30:46 +0100384 /*
385 * Prealign input max address, so we simplify our matching
386 * logic below and can just reuse it as return pointer.
387 */
388 max_addr &= ~EFI_PAGE_MASK;
389
Alexander Graf8623f922016-03-04 01:10:04 +0100390 list_for_each(lhandle, &efi_mem) {
391 struct efi_mem_list *lmem = list_entry(lhandle,
392 struct efi_mem_list, link);
393 struct efi_mem_desc *desc = &lmem->desc;
394 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
395 uint64_t desc_end = desc->physical_start + desc_len;
396 uint64_t curmax = min(max_addr, desc_end);
397 uint64_t ret = curmax - len;
398
399 /* We only take memory from free RAM */
400 if (desc->type != EFI_CONVENTIONAL_MEMORY)
401 continue;
402
403 /* Out of bounds for max_addr */
404 if ((ret + len) > max_addr)
405 continue;
406
407 /* Out of bounds for upper map limit */
408 if ((ret + len) > desc_end)
409 continue;
410
411 /* Out of bounds for lower map limit */
412 if (ret < desc->physical_start)
413 continue;
414
415 /* Return the highest address in this map within bounds */
416 return ret;
417 }
418
419 return 0;
420}
421
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100422/*
423 * Allocate memory pages.
424 *
425 * @type type of allocation to be performed
426 * @memory_type usage type of the allocated memory
427 * @pages number of pages to be allocated
428 * @memory allocated memory
429 * @return status code
430 */
Alexander Graf8623f922016-03-04 01:10:04 +0100431efi_status_t efi_allocate_pages(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100432 efi_uintn_t pages, uint64_t *memory)
Alexander Graf8623f922016-03-04 01:10:04 +0100433{
434 u64 len = pages << EFI_PAGE_SHIFT;
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200435 efi_status_t ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100436 uint64_t addr;
437
Heinrich Schuchardtf7272332019-04-23 00:30:53 +0200438 /* Check import parameters */
439 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
440 memory_type <= 0x6FFFFFFF)
441 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200442 if (!memory)
443 return EFI_INVALID_PARAMETER;
444
Alexander Graf8623f922016-03-04 01:10:04 +0100445 switch (type) {
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100446 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf8623f922016-03-04 01:10:04 +0100447 /* Any page */
Stephen Warren66208b92018-08-30 15:43:45 -0600448 addr = efi_find_free_memory(len, -1ULL);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200449 if (!addr)
450 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100451 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100452 case EFI_ALLOCATE_MAX_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100453 /* Max address */
454 addr = efi_find_free_memory(len, *memory);
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200455 if (!addr)
456 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100457 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100458 case EFI_ALLOCATE_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100459 /* Exact address, reserve it. The addr is already in *memory. */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200460 ret = efi_check_allocated(*memory, false);
461 if (ret != EFI_SUCCESS)
462 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100463 addr = *memory;
464 break;
465 default:
466 /* UEFI doesn't specify other allocation types */
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200467 return EFI_INVALID_PARAMETER;
Alexander Graf8623f922016-03-04 01:10:04 +0100468 }
469
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200470 /* Reserve that map in our memory maps */
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100471 if (efi_add_memory_map(addr, pages, memory_type, true) != EFI_SUCCESS)
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200472 /* Map would overlap, bail out */
473 return EFI_OUT_OF_RESOURCES;
Alexander Graf8623f922016-03-04 01:10:04 +0100474
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200475 *memory = addr;
Alexander Graf8623f922016-03-04 01:10:04 +0100476
Heinrich Schuchardtfbdb93c2019-05-11 08:43:30 +0200477 return EFI_SUCCESS;
Alexander Graf8623f922016-03-04 01:10:04 +0100478}
479
480void *efi_alloc(uint64_t len, int memory_type)
481{
482 uint64_t ret = 0;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100483 uint64_t pages = efi_size_in_pages(len);
Alexander Graf8623f922016-03-04 01:10:04 +0100484 efi_status_t r;
485
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200486 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
487 &ret);
Alexander Graf8623f922016-03-04 01:10:04 +0100488 if (r == EFI_SUCCESS)
489 return (void*)(uintptr_t)ret;
490
491 return NULL;
492}
493
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100494/**
495 * efi_free_pages() - free memory pages
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100496 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100497 * @memory: start of the memory area to be freed
498 * @pages: number of pages to be freed
499 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100500 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100501efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf8623f922016-03-04 01:10:04 +0100502{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200503 efi_status_t ret;
504
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200505 ret = efi_check_allocated(memory, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200506 if (ret != EFI_SUCCESS)
507 return ret;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200508
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100509 /* Sanity check */
Heinrich Schuchardt7bf4bc92019-04-25 18:41:40 +0200510 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100511 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
512 memory, pages);
513 return EFI_INVALID_PARAMETER;
514 }
515
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100516 ret = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false);
Stefan Brünsa4f43022016-10-01 23:32:27 +0200517 /* Merging of adjacent free regions is missing */
518
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100519 if (ret != EFI_SUCCESS)
520 return EFI_NOT_FOUND;
Stefan Brünsa4f43022016-10-01 23:32:27 +0200521
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100522 return ret;
Alexander Graf8623f922016-03-04 01:10:04 +0100523}
524
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100525/**
526 * efi_allocate_pool - allocate memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100527 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100528 * @pool_type: type of the pool from which memory is to be allocated
529 * @size: number of bytes to be allocated
530 * @buffer: allocated memory
531 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100532 */
533efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200534{
535 efi_status_t r;
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100536 u64 addr;
Alexander Grafcd405172018-06-18 17:23:15 +0200537 struct efi_pool_allocation *alloc;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100538 u64 num_pages = efi_size_in_pages(size +
539 sizeof(struct efi_pool_allocation));
Stefan Brüns67b67d92016-10-09 22:17:26 +0200540
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200541 if (!buffer)
542 return EFI_INVALID_PARAMETER;
543
Stefan Brüns67b67d92016-10-09 22:17:26 +0200544 if (size == 0) {
545 *buffer = NULL;
546 return EFI_SUCCESS;
547 }
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200548
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200549 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100550 &addr);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200551 if (r == EFI_SUCCESS) {
Heinrich Schuchardt657e9792019-03-18 20:01:59 +0100552 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200553 alloc->num_pages = num_pages;
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100554 alloc->checksum = checksum(alloc);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200555 *buffer = alloc->data;
556 }
557
558 return r;
559}
560
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100561/**
562 * efi_free_pool() - free memory from pool
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100563 *
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100564 * @buffer: start of memory to be freed
565 * Return: status code
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100566 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200567efi_status_t efi_free_pool(void *buffer)
568{
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200569 efi_status_t ret;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200570 struct efi_pool_allocation *alloc;
571
Heinrich Schuchardt56b587f72019-06-12 07:17:04 +0200572 if (!buffer)
573 return EFI_INVALID_PARAMETER;
574
Heinrich Schuchardt906f6852019-05-11 08:21:17 +0200575 ret = efi_check_allocated((uintptr_t)buffer, true);
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200576 if (ret != EFI_SUCCESS)
577 return ret;
xypron.glpk@gmx.de34ddae72017-07-14 19:12:39 +0200578
Stefan Brüns67b67d92016-10-09 22:17:26 +0200579 alloc = container_of(buffer, struct efi_pool_allocation, data);
Heinrich Schuchardtb4e2ba32019-03-26 05:31:41 +0100580
581 /* Check that this memory was allocated by efi_allocate_pool() */
582 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
583 alloc->checksum != checksum(alloc)) {
584 printf("%s: illegal free 0x%p\n", __func__, buffer);
585 return EFI_INVALID_PARAMETER;
586 }
587 /* Avoid double free */
588 alloc->checksum = 0;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200589
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200590 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200591
Heinrich Schuchardtac7f8422019-05-10 21:21:30 +0200592 return ret;
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200593}
594
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100595/*
596 * Get map describing memory usage.
597 *
598 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
599 * on exit the size of the copied memory map
600 * @memory_map buffer to which the memory map is written
601 * @map_key key for the memory map
602 * @descriptor_size size of an individual memory descriptor
603 * @descriptor_version version number of the memory descriptor structure
604 * @return status code
605 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100606efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
607 struct efi_mem_desc *memory_map,
608 efi_uintn_t *map_key,
609 efi_uintn_t *descriptor_size,
610 uint32_t *descriptor_version)
Alexander Graf8623f922016-03-04 01:10:04 +0100611{
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100612 efi_uintn_t map_size = 0;
Alexander Graf9b591022016-04-11 23:51:02 +0200613 int map_entries = 0;
Alexander Graf8623f922016-03-04 01:10:04 +0100614 struct list_head *lhandle;
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200615 efi_uintn_t provided_map_size;
Alexander Graf8623f922016-03-04 01:10:04 +0100616
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200617 if (!memory_map_size)
618 return EFI_INVALID_PARAMETER;
619
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200620 provided_map_size = *memory_map_size;
621
Alexander Graf8623f922016-03-04 01:10:04 +0100622 list_for_each(lhandle, &efi_mem)
Alexander Graf9b591022016-04-11 23:51:02 +0200623 map_entries++;
624
625 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf8623f922016-03-04 01:10:04 +0100626
Rob Clarka84c9742017-07-26 14:34:05 -0400627 *memory_map_size = map_size;
628
xypron.glpk@gmx.de5487ab52017-07-21 19:04:33 +0200629 if (provided_map_size < map_size)
630 return EFI_BUFFER_TOO_SMALL;
631
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200632 if (!memory_map)
633 return EFI_INVALID_PARAMETER;
634
Alexander Graf8623f922016-03-04 01:10:04 +0100635 if (descriptor_size)
636 *descriptor_size = sizeof(struct efi_mem_desc);
637
Mian Yousaf Kaukab338cf562016-09-05 23:59:22 +0200638 if (descriptor_version)
639 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
640
Alexander Graf8623f922016-03-04 01:10:04 +0100641 /* Copy list into array */
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200642 /* Return the list in ascending order */
643 memory_map = &memory_map[map_entries - 1];
644 list_for_each(lhandle, &efi_mem) {
645 struct efi_mem_list *lmem;
Alexander Graf8623f922016-03-04 01:10:04 +0100646
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200647 lmem = list_entry(lhandle, struct efi_mem_list, link);
648 *memory_map = lmem->desc;
649 memory_map--;
Alexander Graf8623f922016-03-04 01:10:04 +0100650 }
651
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200652 if (map_key)
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200653 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dee436e632017-07-21 19:05:44 +0200654
Alexander Graf8623f922016-03-04 01:10:04 +0100655 return EFI_SUCCESS;
656}
657
Park, Aidene009e7a2019-09-03 17:43:43 +0000658/**
659 * efi_add_conventional_memory_map() - add a RAM memory area to the map
660 *
661 * @ram_start: start address of a RAM memory area
662 * @ram_end: end address of a RAM memory area
663 * @ram_top: max address to be used as conventional memory
664 * Return: status code
665 */
666efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
667 u64 ram_top)
668{
669 u64 pages;
670
671 /* Remove partial pages */
672 ram_end &= ~EFI_PAGE_MASK;
673 ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
674
675 if (ram_end <= ram_start) {
676 /* Invalid mapping */
677 return EFI_INVALID_PARAMETER;
678 }
679
680 pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
681
682 efi_add_memory_map(ram_start, pages,
683 EFI_CONVENTIONAL_MEMORY, false);
684
685 /*
686 * Boards may indicate to the U-Boot memory core that they
687 * can not support memory above ram_top. Let's honor this
688 * in the efi_loader subsystem too by declaring any memory
689 * above ram_top as "already occupied by firmware".
690 */
691 if (ram_top < ram_start) {
692 /* ram_top is before this region, reserve all */
693 efi_add_memory_map(ram_start, pages,
694 EFI_BOOT_SERVICES_DATA, true);
695 } else if ((ram_top >= ram_start) && (ram_top < ram_end)) {
696 /* ram_top is inside this region, reserve parts */
697 pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
698
699 efi_add_memory_map(ram_top, pages,
700 EFI_BOOT_SERVICES_DATA, true);
701 }
702
703 return EFI_SUCCESS;
704}
705
York Suna8e9f0a2017-03-06 09:02:27 -0800706__weak void efi_add_known_memory(void)
Alexander Graf8623f922016-03-04 01:10:04 +0100707{
Alexander Grafc214edf2018-11-30 21:24:56 +0100708 u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
Alexander Graf8623f922016-03-04 01:10:04 +0100709 int i;
710
Heinrich Schuchardteb8edcf2019-01-05 23:41:36 +0100711 /*
712 * ram_top is just outside mapped memory. So use an offset of one for
713 * mapping the sandbox address.
714 */
715 ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
716
Alexander Grafc214edf2018-11-30 21:24:56 +0100717 /* Fix for 32bit targets with ram_top at 4G */
718 if (!ram_top)
719 ram_top = 0x100000000ULL;
720
Alexander Graf8623f922016-03-04 01:10:04 +0100721 /* Add RAM */
722 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Park, Aidene009e7a2019-09-03 17:43:43 +0000723 u64 ram_end, ram_start;
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100724
Heinrich Schuchardt6738fcd2018-11-18 17:58:46 +0100725 ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
Heinrich Schuchardt084091a2018-11-12 18:55:24 +0100726 ram_end = ram_start + gd->bd->bi_dram[i].size;
727
Park, Aidene009e7a2019-09-03 17:43:43 +0000728 efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
Alexander Graf8623f922016-03-04 01:10:04 +0100729 }
York Suna8e9f0a2017-03-06 09:02:27 -0800730}
731
Simon Glass78639432018-06-18 17:23:12 +0200732/* Add memory regions for U-Boot's memory and for the runtime services code */
733static void add_u_boot_and_runtime(void)
York Suna8e9f0a2017-03-06 09:02:27 -0800734{
735 unsigned long runtime_start, runtime_end, runtime_pages;
Alexander Graf15937a22018-09-17 13:54:33 +0200736 unsigned long runtime_mask = EFI_PAGE_MASK;
York Suna8e9f0a2017-03-06 09:02:27 -0800737 unsigned long uboot_start, uboot_pages;
738 unsigned long uboot_stack_size = 16 * 1024 * 1024;
739
Alexander Graf8623f922016-03-04 01:10:04 +0100740 /* Add U-Boot */
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100741 uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
742 uboot_stack_size) & ~EFI_PAGE_MASK;
743 uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
744 uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100745 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
746
Alexander Graf15937a22018-09-17 13:54:33 +0200747#if defined(__aarch64__)
748 /*
749 * Runtime Services must be 64KiB aligned according to the
750 * "AArch64 Platforms" section in the UEFI spec (2.7+).
751 */
752
753 runtime_mask = SZ_64K - 1;
754#endif
755
756 /*
757 * Add Runtime Services. We mark surrounding boottime code as runtime as
758 * well to fulfill the runtime alignment constraints but avoid padding.
759 */
760 runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100761 runtime_end = (ulong)&__efi_runtime_stop;
Alexander Graf15937a22018-09-17 13:54:33 +0200762 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
Alexander Graf8623f922016-03-04 01:10:04 +0100763 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
764 efi_add_memory_map(runtime_start, runtime_pages,
765 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass78639432018-06-18 17:23:12 +0200766}
767
768int efi_memory_init(void)
769{
770 efi_add_known_memory();
771
Heinrich Schuchardta69976e2019-11-08 20:42:53 +0100772 add_u_boot_and_runtime();
Alexander Graf8623f922016-03-04 01:10:04 +0100773
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200774#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
775 /* Request a 32bit 64MB bounce buffer region */
776 uint64_t efi_bounce_buffer_addr = 0xffffffff;
777
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200778 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200779 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
780 &efi_bounce_buffer_addr) != EFI_SUCCESS)
781 return -1;
782
783 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
784#endif
785
Alexander Graf8623f922016-03-04 01:10:04 +0100786 return 0;
787}