2018-05-07 21:02:21 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-03-04 00:10:04 +00:00
|
|
|
/*
|
|
|
|
* EFI application memory management
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Alexander Graf
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <efi_loader.h>
|
|
|
|
#include <malloc.h>
|
2018-06-18 15:23:15 +00:00
|
|
|
#include <mapmem.h>
|
2018-03-08 20:53:27 +00:00
|
|
|
#include <watchdog.h>
|
2016-03-30 14:38:29 +00:00
|
|
|
#include <linux/list_sort.h>
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2018-07-02 10:53:55 +00:00
|
|
|
efi_uintn_t efi_memory_map_key;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
struct efi_mem_list {
|
|
|
|
struct list_head link;
|
|
|
|
struct efi_mem_desc desc;
|
|
|
|
};
|
|
|
|
|
2016-05-27 10:25:03 +00:00
|
|
|
#define EFI_CARVE_NO_OVERLAP -1
|
|
|
|
#define EFI_CARVE_LOOP_AGAIN -2
|
|
|
|
#define EFI_CARVE_OVERLAPS_NONRAM -3
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
/* This list contains all memory map items */
|
|
|
|
LIST_HEAD(efi_mem);
|
|
|
|
|
2016-05-11 16:25:48 +00:00
|
|
|
#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
|
|
|
|
void *efi_bounce_buffer;
|
|
|
|
#endif
|
|
|
|
|
2016-10-09 20:17:26 +00:00
|
|
|
/*
|
|
|
|
* U-Boot services each EFI AllocatePool request as a separate
|
|
|
|
* (multiple) page allocation. We have to track the number of pages
|
|
|
|
* to be able to free the correct amount later.
|
|
|
|
* EFI requires 8 byte alignment for pool allocations, so we can
|
|
|
|
* prepend each allocation with an 64 bit header tracking the
|
|
|
|
* allocation size, and hand out the remainder to the caller.
|
|
|
|
*/
|
|
|
|
struct efi_pool_allocation {
|
|
|
|
u64 num_pages;
|
2017-09-13 22:05:36 +00:00
|
|
|
char data[] __aligned(ARCH_DMA_MINALIGN);
|
2016-10-09 20:17:26 +00:00
|
|
|
};
|
|
|
|
|
2016-03-30 14:38:29 +00:00
|
|
|
/*
|
|
|
|
* Sorts the memory list from highest address to lowest address
|
|
|
|
*
|
|
|
|
* When allocating memory we should always start from the highest
|
|
|
|
* address chunk, so sort the memory list such that the first list
|
|
|
|
* iterator gets the highest address and goes lower from there.
|
|
|
|
*/
|
|
|
|
static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
|
|
|
|
{
|
|
|
|
struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
|
|
|
|
struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
|
|
|
|
|
|
|
|
if (mema->desc.physical_start == memb->desc.physical_start)
|
|
|
|
return 0;
|
|
|
|
else if (mema->desc.physical_start < memb->desc.physical_start)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-16 15:05:29 +00:00
|
|
|
static uint64_t desc_get_end(struct efi_mem_desc *desc)
|
|
|
|
{
|
|
|
|
return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
|
|
|
|
}
|
|
|
|
|
2016-03-30 14:38:29 +00:00
|
|
|
static void efi_mem_sort(void)
|
|
|
|
{
|
2018-09-16 15:05:29 +00:00
|
|
|
struct list_head *lhandle;
|
|
|
|
struct efi_mem_list *prevmem = NULL;
|
|
|
|
bool merge_again = true;
|
|
|
|
|
2016-03-30 14:38:29 +00:00
|
|
|
list_sort(NULL, &efi_mem, efi_mem_cmp);
|
2018-09-16 15:05:29 +00:00
|
|
|
|
|
|
|
/* Now merge entries that can be merged */
|
|
|
|
while (merge_again) {
|
|
|
|
merge_again = false;
|
|
|
|
list_for_each(lhandle, &efi_mem) {
|
|
|
|
struct efi_mem_list *lmem;
|
|
|
|
struct efi_mem_desc *prev = &prevmem->desc;
|
|
|
|
struct efi_mem_desc *cur;
|
|
|
|
uint64_t pages;
|
|
|
|
|
|
|
|
lmem = list_entry(lhandle, struct efi_mem_list, link);
|
|
|
|
if (!prevmem) {
|
|
|
|
prevmem = lmem;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = &lmem->desc;
|
|
|
|
|
|
|
|
if ((desc_get_end(cur) == prev->physical_start) &&
|
|
|
|
(prev->type == cur->type) &&
|
|
|
|
(prev->attribute == cur->attribute)) {
|
|
|
|
/* There is an existing map before, reuse it */
|
|
|
|
pages = cur->num_pages;
|
|
|
|
prev->num_pages += pages;
|
|
|
|
prev->physical_start -= pages << EFI_PAGE_SHIFT;
|
|
|
|
prev->virtual_start -= pages << EFI_PAGE_SHIFT;
|
|
|
|
list_del(&lmem->link);
|
|
|
|
free(lmem);
|
|
|
|
|
|
|
|
merge_again = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
prevmem = lmem;
|
|
|
|
}
|
|
|
|
}
|
2016-03-30 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 14:45:09 +00:00
|
|
|
/** efi_mem_carve_out - unmap memory region
|
|
|
|
*
|
|
|
|
* @map: memory map
|
|
|
|
* @carve_desc: memory region to unmap
|
|
|
|
* @overlap_only_ram: the carved out region may only overlap RAM
|
|
|
|
* Return Value: the number of overlapping pages which have been
|
|
|
|
* removed from the map,
|
|
|
|
* EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
|
|
|
|
* EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
|
|
|
|
* and the map contains anything but free ram
|
|
|
|
* (only when overlap_only_ram is true),
|
|
|
|
* EFI_CARVE_LOOP_AGAIN, if the mapping list should be
|
|
|
|
* traversed again, as it has been altered.
|
2016-03-04 00:10:04 +00:00
|
|
|
*
|
2018-05-27 14:45:09 +00:00
|
|
|
* Unmaps all memory occupied by the carve_desc region from the list entry
|
|
|
|
* pointed to by map.
|
2016-10-01 21:32:23 +00:00
|
|
|
*
|
|
|
|
* In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
|
2018-05-27 14:45:09 +00:00
|
|
|
* to re-add the already carved out pages to the mapping.
|
2016-03-04 00:10:04 +00:00
|
|
|
*/
|
2018-05-27 14:45:09 +00:00
|
|
|
static s64 efi_mem_carve_out(struct efi_mem_list *map,
|
2016-03-04 00:10:04 +00:00
|
|
|
struct efi_mem_desc *carve_desc,
|
|
|
|
bool overlap_only_ram)
|
|
|
|
{
|
|
|
|
struct efi_mem_list *newmap;
|
|
|
|
struct efi_mem_desc *map_desc = &map->desc;
|
|
|
|
uint64_t map_start = map_desc->physical_start;
|
|
|
|
uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
|
|
|
|
uint64_t carve_start = carve_desc->physical_start;
|
|
|
|
uint64_t carve_end = carve_start +
|
|
|
|
(carve_desc->num_pages << EFI_PAGE_SHIFT);
|
|
|
|
|
|
|
|
/* check whether we're overlapping */
|
|
|
|
if ((carve_end <= map_start) || (carve_start >= map_end))
|
2016-05-27 10:25:03 +00:00
|
|
|
return EFI_CARVE_NO_OVERLAP;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
/* We're overlapping with non-RAM, warn the caller if desired */
|
|
|
|
if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
|
2016-05-27 10:25:03 +00:00
|
|
|
return EFI_CARVE_OVERLAPS_NONRAM;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
/* Sanitize carve_start and carve_end to lie within our bounds */
|
|
|
|
carve_start = max(carve_start, map_start);
|
|
|
|
carve_end = min(carve_end, map_end);
|
|
|
|
|
|
|
|
/* Carving at the beginning of our map? Just move it! */
|
|
|
|
if (carve_start == map_start) {
|
|
|
|
if (map_end == carve_end) {
|
|
|
|
/* Full overlap, just remove map */
|
|
|
|
list_del(&map->link);
|
2016-10-01 21:32:29 +00:00
|
|
|
free(map);
|
|
|
|
} else {
|
|
|
|
map->desc.physical_start = carve_end;
|
|
|
|
map->desc.num_pages = (map_end - carve_end)
|
|
|
|
>> EFI_PAGE_SHIFT;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 10:25:03 +00:00
|
|
|
return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Overlapping maps, just split the list map at carve_start,
|
|
|
|
* it will get moved or removed in the next iteration.
|
|
|
|
*
|
|
|
|
* [ map_desc |__carve_start__| newmap ]
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Create a new map from [ carve_start ... map_end ] */
|
|
|
|
newmap = calloc(1, sizeof(*newmap));
|
|
|
|
newmap->desc = map->desc;
|
|
|
|
newmap->desc.physical_start = carve_start;
|
|
|
|
newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
|
2016-10-01 21:32:28 +00:00
|
|
|
/* Insert before current entry (descending address order) */
|
|
|
|
list_add_tail(&newmap->link, &map->link);
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
/* Shrink the map to [ map_start ... carve_start ] */
|
|
|
|
map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
|
|
|
|
|
2016-05-27 10:25:03 +00:00
|
|
|
return EFI_CARVE_LOOP_AGAIN;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
|
|
|
|
bool overlap_only_ram)
|
|
|
|
{
|
|
|
|
struct list_head *lhandle;
|
|
|
|
struct efi_mem_list *newlist;
|
2016-05-27 10:25:03 +00:00
|
|
|
bool carve_again;
|
|
|
|
uint64_t carved_pages = 0;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2018-08-06 11:47:40 +00:00
|
|
|
debug("%s: 0x%llx 0x%llx %d %s\n", __func__,
|
2016-07-17 04:57:11 +00:00
|
|
|
start, pages, memory_type, overlap_only_ram ? "yes" : "no");
|
|
|
|
|
2018-07-02 10:53:55 +00:00
|
|
|
if (memory_type >= EFI_MAX_MEMORY_TYPE)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
if (!pages)
|
|
|
|
return start;
|
|
|
|
|
2018-07-02 10:53:55 +00:00
|
|
|
++efi_memory_map_key;
|
2016-03-04 00:10:04 +00:00
|
|
|
newlist = calloc(1, sizeof(*newlist));
|
|
|
|
newlist->desc.type = memory_type;
|
|
|
|
newlist->desc.physical_start = start;
|
|
|
|
newlist->desc.virtual_start = start;
|
|
|
|
newlist->desc.num_pages = pages;
|
|
|
|
|
|
|
|
switch (memory_type) {
|
|
|
|
case EFI_RUNTIME_SERVICES_CODE:
|
|
|
|
case EFI_RUNTIME_SERVICES_DATA:
|
2018-07-14 20:53:30 +00:00
|
|
|
newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
|
2016-03-04 00:10:04 +00:00
|
|
|
break;
|
|
|
|
case EFI_MMAP_IO:
|
2018-07-14 20:53:30 +00:00
|
|
|
newlist->desc.attribute = EFI_MEMORY_RUNTIME;
|
2016-03-04 00:10:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-07-14 20:53:30 +00:00
|
|
|
newlist->desc.attribute = EFI_MEMORY_WB;
|
2016-03-04 00:10:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add our new map */
|
|
|
|
do {
|
2016-05-27 10:25:03 +00:00
|
|
|
carve_again = false;
|
2016-03-04 00:10:04 +00:00
|
|
|
list_for_each(lhandle, &efi_mem) {
|
|
|
|
struct efi_mem_list *lmem;
|
2018-05-27 14:45:09 +00:00
|
|
|
s64 r;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
lmem = list_entry(lhandle, struct efi_mem_list, link);
|
|
|
|
r = efi_mem_carve_out(lmem, &newlist->desc,
|
|
|
|
overlap_only_ram);
|
2016-05-27 10:25:03 +00:00
|
|
|
switch (r) {
|
|
|
|
case EFI_CARVE_OVERLAPS_NONRAM:
|
|
|
|
/*
|
|
|
|
* The user requested to only have RAM overlaps,
|
|
|
|
* but we hit a non-RAM region. Error out.
|
|
|
|
*/
|
2016-03-04 00:10:04 +00:00
|
|
|
return 0;
|
2016-05-27 10:25:03 +00:00
|
|
|
case EFI_CARVE_NO_OVERLAP:
|
|
|
|
/* Just ignore this list entry */
|
|
|
|
break;
|
|
|
|
case EFI_CARVE_LOOP_AGAIN:
|
|
|
|
/*
|
|
|
|
* We split an entry, but need to loop through
|
|
|
|
* the list again to actually carve it.
|
|
|
|
*/
|
|
|
|
carve_again = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* We carved a number of pages */
|
|
|
|
carved_pages += r;
|
|
|
|
carve_again = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (carve_again) {
|
|
|
|
/* The list changed, we need to start over */
|
2016-03-04 00:10:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 10:25:03 +00:00
|
|
|
} while (carve_again);
|
|
|
|
|
|
|
|
if (overlap_only_ram && (carved_pages != pages)) {
|
|
|
|
/*
|
|
|
|
* The payload wanted to have RAM overlaps, but we overlapped
|
|
|
|
* with an unallocated region. Error out.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-04 00:10:04 +00:00
|
|
|
|
|
|
|
/* Add our new map */
|
|
|
|
list_add_tail(&newlist->link, &efi_mem);
|
|
|
|
|
2016-03-30 14:38:29 +00:00
|
|
|
/* And make sure memory is listed in descending order */
|
|
|
|
efi_mem_sort();
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
|
|
|
|
{
|
|
|
|
struct list_head *lhandle;
|
|
|
|
|
|
|
|
list_for_each(lhandle, &efi_mem) {
|
|
|
|
struct efi_mem_list *lmem = list_entry(lhandle,
|
|
|
|
struct efi_mem_list, link);
|
|
|
|
struct efi_mem_desc *desc = &lmem->desc;
|
|
|
|
uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
|
|
|
|
uint64_t desc_end = desc->physical_start + desc_len;
|
|
|
|
uint64_t curmax = min(max_addr, desc_end);
|
|
|
|
uint64_t ret = curmax - len;
|
|
|
|
|
|
|
|
/* We only take memory from free RAM */
|
|
|
|
if (desc->type != EFI_CONVENTIONAL_MEMORY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Out of bounds for max_addr */
|
|
|
|
if ((ret + len) > max_addr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Out of bounds for upper map limit */
|
|
|
|
if ((ret + len) > desc_end)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Out of bounds for lower map limit */
|
|
|
|
if (ret < desc->physical_start)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Return the highest address in this map within bounds */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:52:41 +00:00
|
|
|
/*
|
|
|
|
* Allocate memory pages.
|
|
|
|
*
|
|
|
|
* @type type of allocation to be performed
|
|
|
|
* @memory_type usage type of the allocated memory
|
|
|
|
* @pages number of pages to be allocated
|
|
|
|
* @memory allocated memory
|
|
|
|
* @return status code
|
|
|
|
*/
|
2016-03-04 00:10:04 +00:00
|
|
|
efi_status_t efi_allocate_pages(int type, int memory_type,
|
2017-11-06 20:17:48 +00:00
|
|
|
efi_uintn_t pages, uint64_t *memory)
|
2016-03-04 00:10:04 +00:00
|
|
|
{
|
|
|
|
u64 len = pages << EFI_PAGE_SHIFT;
|
|
|
|
efi_status_t r = EFI_SUCCESS;
|
|
|
|
uint64_t addr;
|
|
|
|
|
2018-07-02 10:53:53 +00:00
|
|
|
if (!memory)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
switch (type) {
|
2018-01-30 20:08:00 +00:00
|
|
|
case EFI_ALLOCATE_ANY_PAGES:
|
2016-03-04 00:10:04 +00:00
|
|
|
/* Any page */
|
2018-08-30 21:43:45 +00:00
|
|
|
addr = efi_find_free_memory(len, -1ULL);
|
2016-03-04 00:10:04 +00:00
|
|
|
if (!addr) {
|
|
|
|
r = EFI_NOT_FOUND;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2018-01-30 20:08:00 +00:00
|
|
|
case EFI_ALLOCATE_MAX_ADDRESS:
|
2016-03-04 00:10:04 +00:00
|
|
|
/* Max address */
|
|
|
|
addr = efi_find_free_memory(len, *memory);
|
|
|
|
if (!addr) {
|
|
|
|
r = EFI_NOT_FOUND;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2018-01-30 20:08:00 +00:00
|
|
|
case EFI_ALLOCATE_ADDRESS:
|
2016-03-04 00:10:04 +00:00
|
|
|
/* Exact address, reserve it. The addr is already in *memory. */
|
|
|
|
addr = *memory;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* UEFI doesn't specify other allocation types */
|
|
|
|
r = EFI_INVALID_PARAMETER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == EFI_SUCCESS) {
|
|
|
|
uint64_t ret;
|
|
|
|
|
|
|
|
/* Reserve that map in our memory maps */
|
|
|
|
ret = efi_add_memory_map(addr, pages, memory_type, true);
|
|
|
|
if (ret == addr) {
|
2018-06-18 15:23:15 +00:00
|
|
|
*memory = (uintptr_t)map_sysmem(addr, len);
|
2016-03-04 00:10:04 +00:00
|
|
|
} else {
|
|
|
|
/* Map would overlap, bail out */
|
|
|
|
r = EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *efi_alloc(uint64_t len, int memory_type)
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
|
|
|
|
efi_status_t r;
|
|
|
|
|
2018-05-26 08:32:27 +00:00
|
|
|
r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
|
|
|
|
&ret);
|
2016-03-04 00:10:04 +00:00
|
|
|
if (r == EFI_SUCCESS)
|
|
|
|
return (void*)(uintptr_t)ret;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:52:41 +00:00
|
|
|
/*
|
|
|
|
* Free memory pages.
|
|
|
|
*
|
|
|
|
* @memory start of the memory area to be freed
|
|
|
|
* @pages number of pages to be freed
|
|
|
|
* @return status code
|
|
|
|
*/
|
2017-11-06 20:17:48 +00:00
|
|
|
efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
|
2016-03-04 00:10:04 +00:00
|
|
|
{
|
2016-10-01 21:32:27 +00:00
|
|
|
uint64_t r = 0;
|
2018-06-18 15:23:15 +00:00
|
|
|
uint64_t addr = map_to_sysmem((void *)(uintptr_t)memory);
|
2016-10-01 21:32:27 +00:00
|
|
|
|
2018-06-18 15:23:15 +00:00
|
|
|
r = efi_add_memory_map(addr, pages, EFI_CONVENTIONAL_MEMORY, false);
|
2016-10-01 21:32:27 +00:00
|
|
|
/* Merging of adjacent free regions is missing */
|
|
|
|
|
2018-06-18 15:23:15 +00:00
|
|
|
if (r == addr)
|
2016-10-01 21:32:27 +00:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
|
|
|
|
return EFI_NOT_FOUND;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 19:52:41 +00:00
|
|
|
/*
|
|
|
|
* Allocate memory from pool.
|
|
|
|
*
|
|
|
|
* @pool_type type of the pool from which memory is to be allocated
|
|
|
|
* @size number of bytes to be allocated
|
|
|
|
* @buffer allocated memory
|
|
|
|
* @return status code
|
|
|
|
*/
|
|
|
|
efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
|
2016-10-09 20:17:18 +00:00
|
|
|
{
|
|
|
|
efi_status_t r;
|
2018-06-18 15:23:15 +00:00
|
|
|
struct efi_pool_allocation *alloc;
|
2017-09-13 22:05:36 +00:00
|
|
|
u64 num_pages = (size + sizeof(struct efi_pool_allocation) +
|
|
|
|
EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
|
2016-10-09 20:17:26 +00:00
|
|
|
|
2018-07-02 10:53:53 +00:00
|
|
|
if (!buffer)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
2016-10-09 20:17:26 +00:00
|
|
|
if (size == 0) {
|
|
|
|
*buffer = NULL;
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
2016-10-09 20:17:18 +00:00
|
|
|
|
2018-05-26 08:32:27 +00:00
|
|
|
r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
|
2018-06-18 15:23:15 +00:00
|
|
|
(uint64_t *)&alloc);
|
2016-10-09 20:17:26 +00:00
|
|
|
|
|
|
|
if (r == EFI_SUCCESS) {
|
|
|
|
alloc->num_pages = num_pages;
|
|
|
|
*buffer = alloc->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:52:41 +00:00
|
|
|
/*
|
|
|
|
* Free memory from pool.
|
|
|
|
*
|
|
|
|
* @buffer start of memory to be freed
|
|
|
|
* @return status code
|
|
|
|
*/
|
2016-10-09 20:17:26 +00:00
|
|
|
efi_status_t efi_free_pool(void *buffer)
|
|
|
|
{
|
|
|
|
efi_status_t r;
|
|
|
|
struct efi_pool_allocation *alloc;
|
|
|
|
|
2017-07-14 17:12:39 +00:00
|
|
|
if (buffer == NULL)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
2016-10-09 20:17:26 +00:00
|
|
|
alloc = container_of(buffer, struct efi_pool_allocation, data);
|
|
|
|
/* Sanity check, was the supplied address returned by allocate_pool */
|
|
|
|
assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0);
|
|
|
|
|
|
|
|
r = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
|
2016-10-09 20:17:18 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:52:41 +00:00
|
|
|
/*
|
|
|
|
* Get map describing memory usage.
|
|
|
|
*
|
|
|
|
* @memory_map_size on entry the size, in bytes, of the memory map buffer,
|
|
|
|
* on exit the size of the copied memory map
|
|
|
|
* @memory_map buffer to which the memory map is written
|
|
|
|
* @map_key key for the memory map
|
|
|
|
* @descriptor_size size of an individual memory descriptor
|
|
|
|
* @descriptor_version version number of the memory descriptor structure
|
|
|
|
* @return status code
|
|
|
|
*/
|
2017-11-06 20:17:48 +00:00
|
|
|
efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
|
|
|
|
struct efi_mem_desc *memory_map,
|
|
|
|
efi_uintn_t *map_key,
|
|
|
|
efi_uintn_t *descriptor_size,
|
|
|
|
uint32_t *descriptor_version)
|
2016-03-04 00:10:04 +00:00
|
|
|
{
|
2017-11-06 20:17:48 +00:00
|
|
|
efi_uintn_t map_size = 0;
|
2016-04-11 21:51:02 +00:00
|
|
|
int map_entries = 0;
|
2016-03-04 00:10:04 +00:00
|
|
|
struct list_head *lhandle;
|
2018-08-06 20:28:18 +00:00
|
|
|
efi_uintn_t provided_map_size;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2018-07-02 10:53:54 +00:00
|
|
|
if (!memory_map_size)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
2018-08-06 20:28:18 +00:00
|
|
|
provided_map_size = *memory_map_size;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
list_for_each(lhandle, &efi_mem)
|
2016-04-11 21:51:02 +00:00
|
|
|
map_entries++;
|
|
|
|
|
|
|
|
map_size = map_entries * sizeof(struct efi_mem_desc);
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2017-07-26 18:34:05 +00:00
|
|
|
*memory_map_size = map_size;
|
|
|
|
|
2017-07-21 17:04:33 +00:00
|
|
|
if (provided_map_size < map_size)
|
|
|
|
return EFI_BUFFER_TOO_SMALL;
|
|
|
|
|
2018-07-02 10:53:54 +00:00
|
|
|
if (!memory_map)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
if (descriptor_size)
|
|
|
|
*descriptor_size = sizeof(struct efi_mem_desc);
|
|
|
|
|
2016-09-05 21:59:22 +00:00
|
|
|
if (descriptor_version)
|
|
|
|
*descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
/* Copy list into array */
|
2018-07-02 10:53:54 +00:00
|
|
|
/* Return the list in ascending order */
|
|
|
|
memory_map = &memory_map[map_entries - 1];
|
|
|
|
list_for_each(lhandle, &efi_mem) {
|
|
|
|
struct efi_mem_list *lmem;
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2018-07-02 10:53:54 +00:00
|
|
|
lmem = list_entry(lhandle, struct efi_mem_list, link);
|
|
|
|
*memory_map = lmem->desc;
|
|
|
|
memory_map--;
|
2016-03-04 00:10:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 10:53:54 +00:00
|
|
|
if (map_key)
|
2018-07-02 10:53:55 +00:00
|
|
|
*map_key = efi_memory_map_key;
|
2017-07-21 17:05:44 +00:00
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:02:27 +00:00
|
|
|
__weak void efi_add_known_memory(void)
|
2016-03-04 00:10:04 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Add RAM */
|
|
|
|
for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
|
|
|
|
u64 ram_start = gd->bd->bi_dram[i].start;
|
|
|
|
u64 ram_size = gd->bd->bi_dram[i].size;
|
|
|
|
u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
|
|
|
|
u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
|
|
|
|
|
|
|
|
efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
|
|
|
|
false);
|
|
|
|
}
|
2017-03-06 17:02:27 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 15:23:12 +00:00
|
|
|
/* Add memory regions for U-Boot's memory and for the runtime services code */
|
|
|
|
static void add_u_boot_and_runtime(void)
|
2017-03-06 17:02:27 +00:00
|
|
|
{
|
|
|
|
unsigned long runtime_start, runtime_end, runtime_pages;
|
|
|
|
unsigned long uboot_start, uboot_pages;
|
|
|
|
unsigned long uboot_stack_size = 16 * 1024 * 1024;
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
/* Add U-Boot */
|
|
|
|
uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
|
|
|
|
uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
|
|
|
|
efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
|
|
|
|
|
|
|
|
/* Add Runtime Services */
|
|
|
|
runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
|
|
|
|
runtime_end = (ulong)&__efi_runtime_stop;
|
|
|
|
runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
|
|
|
|
runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
|
|
|
|
efi_add_memory_map(runtime_start, runtime_pages,
|
|
|
|
EFI_RUNTIME_SERVICES_CODE, false);
|
2018-06-18 15:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int efi_memory_init(void)
|
|
|
|
{
|
|
|
|
efi_add_known_memory();
|
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_SANDBOX))
|
|
|
|
add_u_boot_and_runtime();
|
2016-03-04 00:10:04 +00:00
|
|
|
|
2016-05-11 16:25:48 +00:00
|
|
|
#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
|
|
|
|
/* Request a 32bit 64MB bounce buffer region */
|
|
|
|
uint64_t efi_bounce_buffer_addr = 0xffffffff;
|
|
|
|
|
2018-05-26 08:32:27 +00:00
|
|
|
if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
|
2016-05-11 16:25:48 +00:00
|
|
|
(64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
|
|
|
|
&efi_bounce_buffer_addr) != EFI_SUCCESS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;
|
|
|
|
#endif
|
|
|
|
|
2016-03-04 00:10:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|