blob: 1ffcf92eb2e04eb12cb98c255af2474435008590 [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 Graf8623f922016-03-04 01:10:04 +010014
15DECLARE_GLOBAL_DATA_PTR;
16
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +020017efi_uintn_t efi_memory_map_key;
18
Alexander Graf8623f922016-03-04 01:10:04 +010019struct efi_mem_list {
20 struct list_head link;
21 struct efi_mem_desc desc;
22};
23
Alexander Graf66d86232016-05-27 12:25:03 +020024#define EFI_CARVE_NO_OVERLAP -1
25#define EFI_CARVE_LOOP_AGAIN -2
26#define EFI_CARVE_OVERLAPS_NONRAM -3
27
Alexander Graf8623f922016-03-04 01:10:04 +010028/* This list contains all memory map items */
29LIST_HEAD(efi_mem);
30
Alexander Graf7c00a3c2016-05-11 18:25:48 +020031#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
32void *efi_bounce_buffer;
33#endif
34
Alexander Graf8623f922016-03-04 01:10:04 +010035/*
Stefan Brüns67b67d92016-10-09 22:17:26 +020036 * U-Boot services each EFI AllocatePool request as a separate
37 * (multiple) page allocation. We have to track the number of pages
38 * to be able to free the correct amount later.
39 * EFI requires 8 byte alignment for pool allocations, so we can
40 * prepend each allocation with an 64 bit header tracking the
41 * allocation size, and hand out the remainder to the caller.
42 */
43struct efi_pool_allocation {
44 u64 num_pages;
Rob Clarke597fd42017-09-13 18:05:36 -040045 char data[] __aligned(ARCH_DMA_MINALIGN);
Stefan Brüns67b67d92016-10-09 22:17:26 +020046};
47
48/*
Alexander Grafde2a13b2016-03-30 16:38:29 +020049 * Sorts the memory list from highest address to lowest address
50 *
51 * When allocating memory we should always start from the highest
52 * address chunk, so sort the memory list such that the first list
53 * iterator gets the highest address and goes lower from there.
54 */
55static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
56{
57 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
58 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
59
60 if (mema->desc.physical_start == memb->desc.physical_start)
61 return 0;
62 else if (mema->desc.physical_start < memb->desc.physical_start)
63 return 1;
64 else
65 return -1;
66}
67
Alexander Grafd7288142018-09-16 17:05:29 +020068static uint64_t desc_get_end(struct efi_mem_desc *desc)
69{
70 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
71}
72
Alexander Grafde2a13b2016-03-30 16:38:29 +020073static void efi_mem_sort(void)
74{
Alexander Grafd7288142018-09-16 17:05:29 +020075 struct list_head *lhandle;
76 struct efi_mem_list *prevmem = NULL;
77 bool merge_again = true;
78
Alexander Grafde2a13b2016-03-30 16:38:29 +020079 list_sort(NULL, &efi_mem, efi_mem_cmp);
Alexander Grafd7288142018-09-16 17:05:29 +020080
81 /* Now merge entries that can be merged */
82 while (merge_again) {
83 merge_again = false;
84 list_for_each(lhandle, &efi_mem) {
85 struct efi_mem_list *lmem;
86 struct efi_mem_desc *prev = &prevmem->desc;
87 struct efi_mem_desc *cur;
88 uint64_t pages;
89
90 lmem = list_entry(lhandle, struct efi_mem_list, link);
91 if (!prevmem) {
92 prevmem = lmem;
93 continue;
94 }
95
96 cur = &lmem->desc;
97
98 if ((desc_get_end(cur) == prev->physical_start) &&
99 (prev->type == cur->type) &&
100 (prev->attribute == cur->attribute)) {
101 /* There is an existing map before, reuse it */
102 pages = cur->num_pages;
103 prev->num_pages += pages;
104 prev->physical_start -= pages << EFI_PAGE_SHIFT;
105 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
106 list_del(&lmem->link);
107 free(lmem);
108
109 merge_again = true;
110 break;
111 }
112
113 prevmem = lmem;
114 }
115 }
Alexander Grafde2a13b2016-03-30 16:38:29 +0200116}
117
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200118/** efi_mem_carve_out - unmap memory region
Alexander Graf8623f922016-03-04 01:10:04 +0100119 *
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200120 * @map: memory map
121 * @carve_desc: memory region to unmap
122 * @overlap_only_ram: the carved out region may only overlap RAM
123 * Return Value: the number of overlapping pages which have been
124 * removed from the map,
125 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
126 * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
127 * and the map contains anything but free ram
128 * (only when overlap_only_ram is true),
129 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be
130 * traversed again, as it has been altered.
131 *
132 * Unmaps all memory occupied by the carve_desc region from the list entry
133 * pointed to by map.
Stefan Brüns1b00dc12016-10-01 23:32:23 +0200134 *
135 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200136 * to re-add the already carved out pages to the mapping.
Alexander Graf8623f922016-03-04 01:10:04 +0100137 */
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200138static s64 efi_mem_carve_out(struct efi_mem_list *map,
Alexander Graf8623f922016-03-04 01:10:04 +0100139 struct efi_mem_desc *carve_desc,
140 bool overlap_only_ram)
141{
142 struct efi_mem_list *newmap;
143 struct efi_mem_desc *map_desc = &map->desc;
144 uint64_t map_start = map_desc->physical_start;
145 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
146 uint64_t carve_start = carve_desc->physical_start;
147 uint64_t carve_end = carve_start +
148 (carve_desc->num_pages << EFI_PAGE_SHIFT);
149
150 /* check whether we're overlapping */
151 if ((carve_end <= map_start) || (carve_start >= map_end))
Alexander Graf66d86232016-05-27 12:25:03 +0200152 return EFI_CARVE_NO_OVERLAP;
Alexander Graf8623f922016-03-04 01:10:04 +0100153
154 /* We're overlapping with non-RAM, warn the caller if desired */
155 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
Alexander Graf66d86232016-05-27 12:25:03 +0200156 return EFI_CARVE_OVERLAPS_NONRAM;
Alexander Graf8623f922016-03-04 01:10:04 +0100157
158 /* Sanitize carve_start and carve_end to lie within our bounds */
159 carve_start = max(carve_start, map_start);
160 carve_end = min(carve_end, map_end);
161
162 /* Carving at the beginning of our map? Just move it! */
163 if (carve_start == map_start) {
164 if (map_end == carve_end) {
165 /* Full overlap, just remove map */
166 list_del(&map->link);
Stefan Brüns4e9ad8d2016-10-01 23:32:29 +0200167 free(map);
168 } else {
169 map->desc.physical_start = carve_end;
170 map->desc.num_pages = (map_end - carve_end)
171 >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100172 }
173
Alexander Graf66d86232016-05-27 12:25:03 +0200174 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
Alexander Graf8623f922016-03-04 01:10:04 +0100175 }
176
177 /*
178 * Overlapping maps, just split the list map at carve_start,
179 * it will get moved or removed in the next iteration.
180 *
181 * [ map_desc |__carve_start__| newmap ]
182 */
183
184 /* Create a new map from [ carve_start ... map_end ] */
185 newmap = calloc(1, sizeof(*newmap));
186 newmap->desc = map->desc;
187 newmap->desc.physical_start = carve_start;
188 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
Stefan Brüns9d656512016-10-01 23:32:28 +0200189 /* Insert before current entry (descending address order) */
190 list_add_tail(&newmap->link, &map->link);
Alexander Graf8623f922016-03-04 01:10:04 +0100191
192 /* Shrink the map to [ map_start ... carve_start ] */
193 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
194
Alexander Graf66d86232016-05-27 12:25:03 +0200195 return EFI_CARVE_LOOP_AGAIN;
Alexander Graf8623f922016-03-04 01:10:04 +0100196}
197
198uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
199 bool overlap_only_ram)
200{
201 struct list_head *lhandle;
202 struct efi_mem_list *newlist;
Alexander Graf66d86232016-05-27 12:25:03 +0200203 bool carve_again;
204 uint64_t carved_pages = 0;
Alexander Graf8623f922016-03-04 01:10:04 +0100205
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900206 debug("%s: 0x%llx 0x%llx %d %s\n", __func__,
Andreas Färber102f8bd2016-07-17 06:57:11 +0200207 start, pages, memory_type, overlap_only_ram ? "yes" : "no");
208
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200209 if (memory_type >= EFI_MAX_MEMORY_TYPE)
210 return EFI_INVALID_PARAMETER;
211
Alexander Graf8623f922016-03-04 01:10:04 +0100212 if (!pages)
213 return start;
214
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200215 ++efi_memory_map_key;
Alexander Graf8623f922016-03-04 01:10:04 +0100216 newlist = calloc(1, sizeof(*newlist));
217 newlist->desc.type = memory_type;
218 newlist->desc.physical_start = start;
219 newlist->desc.virtual_start = start;
220 newlist->desc.num_pages = pages;
221
222 switch (memory_type) {
223 case EFI_RUNTIME_SERVICES_CODE:
224 case EFI_RUNTIME_SERVICES_DATA:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200225 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100226 break;
227 case EFI_MMAP_IO:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200228 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
Alexander Graf8623f922016-03-04 01:10:04 +0100229 break;
230 default:
Eugeniu Roscac86c0812018-07-14 22:53:30 +0200231 newlist->desc.attribute = EFI_MEMORY_WB;
Alexander Graf8623f922016-03-04 01:10:04 +0100232 break;
233 }
234
235 /* Add our new map */
236 do {
Alexander Graf66d86232016-05-27 12:25:03 +0200237 carve_again = false;
Alexander Graf8623f922016-03-04 01:10:04 +0100238 list_for_each(lhandle, &efi_mem) {
239 struct efi_mem_list *lmem;
Heinrich Schuchardtf00caa52018-05-27 16:45:09 +0200240 s64 r;
Alexander Graf8623f922016-03-04 01:10:04 +0100241
242 lmem = list_entry(lhandle, struct efi_mem_list, link);
243 r = efi_mem_carve_out(lmem, &newlist->desc,
244 overlap_only_ram);
Alexander Graf66d86232016-05-27 12:25:03 +0200245 switch (r) {
246 case EFI_CARVE_OVERLAPS_NONRAM:
247 /*
248 * The user requested to only have RAM overlaps,
249 * but we hit a non-RAM region. Error out.
250 */
Alexander Graf8623f922016-03-04 01:10:04 +0100251 return 0;
Alexander Graf66d86232016-05-27 12:25:03 +0200252 case EFI_CARVE_NO_OVERLAP:
253 /* Just ignore this list entry */
254 break;
255 case EFI_CARVE_LOOP_AGAIN:
256 /*
257 * We split an entry, but need to loop through
258 * the list again to actually carve it.
259 */
260 carve_again = true;
261 break;
262 default:
263 /* We carved a number of pages */
264 carved_pages += r;
265 carve_again = true;
Alexander Graf8623f922016-03-04 01:10:04 +0100266 break;
267 }
Alexander Graf66d86232016-05-27 12:25:03 +0200268
269 if (carve_again) {
270 /* The list changed, we need to start over */
271 break;
272 }
Alexander Graf8623f922016-03-04 01:10:04 +0100273 }
Alexander Graf66d86232016-05-27 12:25:03 +0200274 } while (carve_again);
275
276 if (overlap_only_ram && (carved_pages != pages)) {
277 /*
278 * The payload wanted to have RAM overlaps, but we overlapped
279 * with an unallocated region. Error out.
280 */
281 return 0;
282 }
Alexander Graf8623f922016-03-04 01:10:04 +0100283
284 /* Add our new map */
285 list_add_tail(&newlist->link, &efi_mem);
286
Alexander Grafde2a13b2016-03-30 16:38:29 +0200287 /* And make sure memory is listed in descending order */
288 efi_mem_sort();
289
Alexander Graf8623f922016-03-04 01:10:04 +0100290 return start;
291}
292
293static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
294{
295 struct list_head *lhandle;
296
Alexander Grafc3b2a252018-11-05 00:30:46 +0100297 /*
298 * Prealign input max address, so we simplify our matching
299 * logic below and can just reuse it as return pointer.
300 */
301 max_addr &= ~EFI_PAGE_MASK;
302
Alexander Graf8623f922016-03-04 01:10:04 +0100303 list_for_each(lhandle, &efi_mem) {
304 struct efi_mem_list *lmem = list_entry(lhandle,
305 struct efi_mem_list, link);
306 struct efi_mem_desc *desc = &lmem->desc;
307 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
308 uint64_t desc_end = desc->physical_start + desc_len;
309 uint64_t curmax = min(max_addr, desc_end);
310 uint64_t ret = curmax - len;
311
312 /* We only take memory from free RAM */
313 if (desc->type != EFI_CONVENTIONAL_MEMORY)
314 continue;
315
316 /* Out of bounds for max_addr */
317 if ((ret + len) > max_addr)
318 continue;
319
320 /* Out of bounds for upper map limit */
321 if ((ret + len) > desc_end)
322 continue;
323
324 /* Out of bounds for lower map limit */
325 if (ret < desc->physical_start)
326 continue;
327
328 /* Return the highest address in this map within bounds */
329 return ret;
330 }
331
332 return 0;
333}
334
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100335/*
336 * Allocate memory pages.
337 *
338 * @type type of allocation to be performed
339 * @memory_type usage type of the allocated memory
340 * @pages number of pages to be allocated
341 * @memory allocated memory
342 * @return status code
343 */
Alexander Graf8623f922016-03-04 01:10:04 +0100344efi_status_t efi_allocate_pages(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100345 efi_uintn_t pages, uint64_t *memory)
Alexander Graf8623f922016-03-04 01:10:04 +0100346{
347 u64 len = pages << EFI_PAGE_SHIFT;
348 efi_status_t r = EFI_SUCCESS;
349 uint64_t addr;
350
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200351 if (!memory)
352 return EFI_INVALID_PARAMETER;
353
Alexander Graf8623f922016-03-04 01:10:04 +0100354 switch (type) {
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100355 case EFI_ALLOCATE_ANY_PAGES:
Alexander Graf8623f922016-03-04 01:10:04 +0100356 /* Any page */
Stephen Warren66208b92018-08-30 15:43:45 -0600357 addr = efi_find_free_memory(len, -1ULL);
Alexander Graf8623f922016-03-04 01:10:04 +0100358 if (!addr) {
359 r = EFI_NOT_FOUND;
360 break;
361 }
362 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100363 case EFI_ALLOCATE_MAX_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100364 /* Max address */
365 addr = efi_find_free_memory(len, *memory);
366 if (!addr) {
367 r = EFI_NOT_FOUND;
368 break;
369 }
370 break;
Heinrich Schuchardt3b7b3b62018-01-30 21:08:00 +0100371 case EFI_ALLOCATE_ADDRESS:
Alexander Graf8623f922016-03-04 01:10:04 +0100372 /* Exact address, reserve it. The addr is already in *memory. */
373 addr = *memory;
374 break;
375 default:
376 /* UEFI doesn't specify other allocation types */
377 r = EFI_INVALID_PARAMETER;
378 break;
379 }
380
381 if (r == EFI_SUCCESS) {
382 uint64_t ret;
383
384 /* Reserve that map in our memory maps */
385 ret = efi_add_memory_map(addr, pages, memory_type, true);
386 if (ret == addr) {
Alexander Grafcd405172018-06-18 17:23:15 +0200387 *memory = (uintptr_t)map_sysmem(addr, len);
Alexander Graf8623f922016-03-04 01:10:04 +0100388 } else {
389 /* Map would overlap, bail out */
390 r = EFI_OUT_OF_RESOURCES;
391 }
392 }
393
394 return r;
395}
396
397void *efi_alloc(uint64_t len, int memory_type)
398{
399 uint64_t ret = 0;
400 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
401 efi_status_t r;
402
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200403 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
404 &ret);
Alexander Graf8623f922016-03-04 01:10:04 +0100405 if (r == EFI_SUCCESS)
406 return (void*)(uintptr_t)ret;
407
408 return NULL;
409}
410
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100411/*
412 * Free memory pages.
413 *
414 * @memory start of the memory area to be freed
415 * @pages number of pages to be freed
416 * @return status code
417 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100418efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
Alexander Graf8623f922016-03-04 01:10:04 +0100419{
Stefan Brünsa4f43022016-10-01 23:32:27 +0200420 uint64_t r = 0;
Alexander Grafcd405172018-06-18 17:23:15 +0200421 uint64_t addr = map_to_sysmem((void *)(uintptr_t)memory);
Stefan Brünsa4f43022016-10-01 23:32:27 +0200422
Alexander Grafcd405172018-06-18 17:23:15 +0200423 r = efi_add_memory_map(addr, pages, EFI_CONVENTIONAL_MEMORY, false);
Stefan Brünsa4f43022016-10-01 23:32:27 +0200424 /* Merging of adjacent free regions is missing */
425
Alexander Grafcd405172018-06-18 17:23:15 +0200426 if (r == addr)
Stefan Brünsa4f43022016-10-01 23:32:27 +0200427 return EFI_SUCCESS;
428
429 return EFI_NOT_FOUND;
Alexander Graf8623f922016-03-04 01:10:04 +0100430}
431
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100432/*
433 * Allocate memory from pool.
434 *
435 * @pool_type type of the pool from which memory is to be allocated
436 * @size number of bytes to be allocated
437 * @buffer allocated memory
438 * @return status code
439 */
440efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200441{
442 efi_status_t r;
Alexander Grafcd405172018-06-18 17:23:15 +0200443 struct efi_pool_allocation *alloc;
Rob Clarke597fd42017-09-13 18:05:36 -0400444 u64 num_pages = (size + sizeof(struct efi_pool_allocation) +
445 EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Stefan Brüns67b67d92016-10-09 22:17:26 +0200446
Heinrich Schuchardta137c962018-07-02 12:53:53 +0200447 if (!buffer)
448 return EFI_INVALID_PARAMETER;
449
Stefan Brüns67b67d92016-10-09 22:17:26 +0200450 if (size == 0) {
451 *buffer = NULL;
452 return EFI_SUCCESS;
453 }
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200454
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200455 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
Alexander Grafcd405172018-06-18 17:23:15 +0200456 (uint64_t *)&alloc);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200457
458 if (r == EFI_SUCCESS) {
Stefan Brüns67b67d92016-10-09 22:17:26 +0200459 alloc->num_pages = num_pages;
460 *buffer = alloc->data;
461 }
462
463 return r;
464}
465
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100466/*
467 * Free memory from pool.
468 *
469 * @buffer start of memory to be freed
470 * @return status code
471 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200472efi_status_t efi_free_pool(void *buffer)
473{
474 efi_status_t r;
475 struct efi_pool_allocation *alloc;
476
xypron.glpk@gmx.de34ddae72017-07-14 19:12:39 +0200477 if (buffer == NULL)
478 return EFI_INVALID_PARAMETER;
479
Stefan Brüns67b67d92016-10-09 22:17:26 +0200480 alloc = container_of(buffer, struct efi_pool_allocation, data);
481 /* Sanity check, was the supplied address returned by allocate_pool */
482 assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0);
483
484 r = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200485
486 return r;
487}
488
Heinrich Schuchardt1b32cae2017-12-04 20:52:41 +0100489/*
490 * Get map describing memory usage.
491 *
492 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
493 * on exit the size of the copied memory map
494 * @memory_map buffer to which the memory map is written
495 * @map_key key for the memory map
496 * @descriptor_size size of an individual memory descriptor
497 * @descriptor_version version number of the memory descriptor structure
498 * @return status code
499 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100500efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
501 struct efi_mem_desc *memory_map,
502 efi_uintn_t *map_key,
503 efi_uintn_t *descriptor_size,
504 uint32_t *descriptor_version)
Alexander Graf8623f922016-03-04 01:10:04 +0100505{
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100506 efi_uintn_t map_size = 0;
Alexander Graf9b591022016-04-11 23:51:02 +0200507 int map_entries = 0;
Alexander Graf8623f922016-03-04 01:10:04 +0100508 struct list_head *lhandle;
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200509 efi_uintn_t provided_map_size;
Alexander Graf8623f922016-03-04 01:10:04 +0100510
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200511 if (!memory_map_size)
512 return EFI_INVALID_PARAMETER;
513
Heinrich Schuchardt9b166462018-08-06 22:28:18 +0200514 provided_map_size = *memory_map_size;
515
Alexander Graf8623f922016-03-04 01:10:04 +0100516 list_for_each(lhandle, &efi_mem)
Alexander Graf9b591022016-04-11 23:51:02 +0200517 map_entries++;
518
519 map_size = map_entries * sizeof(struct efi_mem_desc);
Alexander Graf8623f922016-03-04 01:10:04 +0100520
Rob Clarka84c9742017-07-26 14:34:05 -0400521 *memory_map_size = map_size;
522
xypron.glpk@gmx.de5487ab52017-07-21 19:04:33 +0200523 if (provided_map_size < map_size)
524 return EFI_BUFFER_TOO_SMALL;
525
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200526 if (!memory_map)
527 return EFI_INVALID_PARAMETER;
528
Alexander Graf8623f922016-03-04 01:10:04 +0100529 if (descriptor_size)
530 *descriptor_size = sizeof(struct efi_mem_desc);
531
Mian Yousaf Kaukab338cf562016-09-05 23:59:22 +0200532 if (descriptor_version)
533 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
534
Alexander Graf8623f922016-03-04 01:10:04 +0100535 /* Copy list into array */
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200536 /* Return the list in ascending order */
537 memory_map = &memory_map[map_entries - 1];
538 list_for_each(lhandle, &efi_mem) {
539 struct efi_mem_list *lmem;
Alexander Graf8623f922016-03-04 01:10:04 +0100540
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200541 lmem = list_entry(lhandle, struct efi_mem_list, link);
542 *memory_map = lmem->desc;
543 memory_map--;
Alexander Graf8623f922016-03-04 01:10:04 +0100544 }
545
Heinrich Schuchardte1a1a092018-07-02 12:53:54 +0200546 if (map_key)
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +0200547 *map_key = efi_memory_map_key;
xypron.glpk@gmx.dee436e632017-07-21 19:05:44 +0200548
Alexander Graf8623f922016-03-04 01:10:04 +0100549 return EFI_SUCCESS;
550}
551
York Suna8e9f0a2017-03-06 09:02:27 -0800552__weak void efi_add_known_memory(void)
Alexander Graf8623f922016-03-04 01:10:04 +0100553{
Alexander Graf8623f922016-03-04 01:10:04 +0100554 int i;
555
556 /* Add RAM */
557 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
558 u64 ram_start = gd->bd->bi_dram[i].start;
559 u64 ram_size = gd->bd->bi_dram[i].size;
560 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
561 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
562
563 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
564 false);
565 }
York Suna8e9f0a2017-03-06 09:02:27 -0800566}
567
Simon Glass78639432018-06-18 17:23:12 +0200568/* Add memory regions for U-Boot's memory and for the runtime services code */
569static void add_u_boot_and_runtime(void)
York Suna8e9f0a2017-03-06 09:02:27 -0800570{
571 unsigned long runtime_start, runtime_end, runtime_pages;
572 unsigned long uboot_start, uboot_pages;
573 unsigned long uboot_stack_size = 16 * 1024 * 1024;
574
Alexander Graf8623f922016-03-04 01:10:04 +0100575 /* Add U-Boot */
576 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
577 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
578 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
579
580 /* Add Runtime Services */
581 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
582 runtime_end = (ulong)&__efi_runtime_stop;
583 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
584 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
585 efi_add_memory_map(runtime_start, runtime_pages,
586 EFI_RUNTIME_SERVICES_CODE, false);
Simon Glass78639432018-06-18 17:23:12 +0200587}
588
589int efi_memory_init(void)
590{
591 efi_add_known_memory();
592
593 if (!IS_ENABLED(CONFIG_SANDBOX))
594 add_u_boot_and_runtime();
Alexander Graf8623f922016-03-04 01:10:04 +0100595
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200596#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
597 /* Request a 32bit 64MB bounce buffer region */
598 uint64_t efi_bounce_buffer_addr = 0xffffffff;
599
Heinrich Schuchardt0f7186b2018-05-26 10:32:27 +0200600 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
Alexander Graf7c00a3c2016-05-11 18:25:48 +0200601 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
602 &efi_bounce_buffer_addr) != EFI_SUCCESS)
603 return -1;
604
605 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
606#endif
607
Alexander Graf8623f922016-03-04 01:10:04 +0100608 return 0;
609}