summaryrefslogtreecommitdiff
path: root/lib/efi_loader/efi_memory.c
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2024-10-15 13:46:08 -0600
committerTom Rini <trini@konsulko.com>2024-10-15 13:46:08 -0600
commit77072f9efd3bdb62afc60b98f38d346e506e7e4f (patch)
tree0311d6efdb9d31d30ebcc8ec56b8e77f95e75996 /lib/efi_loader/efi_memory.c
parent29502f6a344be06ad0bcd9076ec612b9e6a7d1cb (diff)
parent8d0df5fde3b8d4083936129e0c01884f9555f197 (diff)
Merge patch series "Make EFI memory allocations synchronous with LMB"
Sughosh Ganu <sughosh.ganu@linaro.org> says: This is part two of the series to have the EFI and LMB modules have a coherent view of memory. Part one of this goal was to change the LMB module to have a global and persistent memory map. Those patches have now been applied to the next branch. These patches are changing the EFI memory allocation API's such that they rely on the LMB module to allocate RAM memory. This fixes the current scenario where the EFI memory module has no visibility of the allocations/reservations made by the LMB module. One thing to note here is that this is limited to the RAM memory region, i.e. the EFI_CONVENTIONAL_MEMORY type. Any other memory type that is to be added to the EFI memory map, still gets handled by the EFI memory module. Changes since V3: * Add comments for the LMB_NOOVERWRITE and LMB_NONOTIFY flags * Drop use of is_addr_in_ram() function * Drop use of CONFIG_MEM_MAP_UPDATE_NOTIFY symbol to check if the notification needs to be sent. * s/lmb_notify/lmb_should_notify * Put a check for EFI_LOADER in the lmb_should_notify() function Some test logs to highlight the issue that is being fixed by the series. Without patch series -------------------- lmb_dump_all: memory.count = 0x1 memory[0] [0x40000000-0x820fffff], 0x42100000 bytes flags: none reserved.count = 0x3 reserved[0] [0xe100000-0xeffffff], 0x00f00000 bytes flags: no-map reserved[1] [0x42000000-0x421fffff], 0x00200000 bytes flags: no-map reserved[2] [0x7f77da00-0x820fffff], 0x02982600 bytes flags: no-overwrite => efidebug memmap -- does not show regions allocated by lmb Missing TPMv2 device for EFI_TCG_PROTOCOL Type Start End Attributes ================ ================ ================ ========== CONVENTIONAL 0000000040000000-000000007f751000 WB BOOT DATA 000000007f751000-000000007f756000 WB RUNTIME DATA 000000007f756000-000000007f757000 WB|RT BOOT DATA 000000007f757000-000000007f758000 WB RUNTIME DATA 000000007f758000-000000007f77a000 WB|RT BOOT DATA 000000007f77a000-000000007f781000 WB BOOT CODE 000000007f781000-00000000807b5000 WB RUNTIME DATA 00000000807b5000-00000000807b6000 WB|RT BOOT CODE 00000000807b6000-00000000817c0000 WB RUNTIME CODE 00000000817c0000-00000000817d0000 WB|RT BOOT CODE 00000000817d0000-0000000082100000 WB => Trying to allocate EFI memory with already allocated region succeeds(should fail) --------------------------------------------------------------------------------- => efi_mem alloc 2000 42000000 Address returned 0x42000000 => efidebug memmap Type Start End Attributes ================ ================ ================ ========== CONVENTIONAL 0000000040000000-0000000042000000 WB BOOT DATA 0000000042000000-0000000042002000 WB CONVENTIONAL 0000000042002000-000000007f751000 WB BOOT DATA 000000007f751000-000000007f756000 WB RUNTIME DATA 000000007f756000-000000007f757000 WB|RT BOOT DATA 000000007f757000-000000007f758000 WB RUNTIME DATA 000000007f758000-000000007f77a000 WB|RT BOOT DATA 000000007f77a000-000000007f781000 WB BOOT CODE 000000007f781000-00000000807b5000 WB RUNTIME DATA 00000000807b5000-00000000807b6000 WB|RT BOOT CODE 00000000807b6000-00000000817c0000 WB RUNTIME CODE 00000000817c0000-00000000817d0000 WB|RT BOOT CODE 00000000817d0000-0000000082100000 WB => With patch series ----------------- lmb_dump_all: memory.count = 0x1 memory[0] [0x40000000-0x820fffff], 0x42100000 bytes flags: none reserved.count = 0x4 reserved[0] [0xe100000-0xeffffff], 0x00f00000 bytes flags: no-map reserved[1] [0x42000000-0x421fffff], 0x00200000 bytes flags: no-map reserved[2] [0x7f74f000-0x7f77dfff], 0x0002f000 bytes flags: no-notify, no-overwrite reserved[3] [0x7f77ea00-0x820fffff], 0x02981600 bytes flags: no-overwrite => efidebug memmap Type Start End Attributes ================ ================ ================ ========== BOOT DATA 000000000e100000-000000000f000000 WB CONVENTIONAL 0000000040000000-0000000042000000 WB BOOT DATA 0000000042000000-0000000042200000 WB CONVENTIONAL 0000000042200000-000000007f74e000 WB BOOT DATA 000000007f74e000-000000007f753000 WB RUNTIME DATA 000000007f753000-000000007f754000 WB|RT BOOT DATA 000000007f754000-000000007f755000 WB RUNTIME DATA 000000007f755000-000000007f777000 WB|RT BOOT DATA 000000007f777000-00000000807b6000 WB RUNTIME DATA 00000000807b6000-00000000807b7000 WB|RT BOOT DATA 00000000807b7000-00000000817c0000 WB RUNTIME CODE 00000000817c0000-00000000817d0000 WB|RT BOOT DATA 00000000817d0000-0000000082100000 WB Trying to allocate EFI memory with already allocated region fails ----------------------------------------------------------------- => efi_mem alloc 2000 42000000 efi_allocate_pages failed 800000000000000e => Trying to allocate EFI memory with non-allocated region succeeds ---------------------------------------------------------------- => efi_mem alloc 2000 42200000 Address returned 0x42200000 => efidebug memmap Type Start End Attributes ================ ================ ================ ========== BOOT DATA 000000000e100000-000000000f000000 WB CONVENTIONAL 0000000040000000-0000000042000000 WB BOOT DATA 0000000042000000-0000000042202000 WB CONVENTIONAL 0000000042202000-000000007f74d000 WB BOOT DATA 000000007f74d000-000000007f752000 WB RUNTIME DATA 000000007f752000-000000007f753000 WB|RT BOOT DATA 000000007f753000-000000007f754000 WB RUNTIME DATA 000000007f754000-000000007f776000 WB|RT BOOT DATA 000000007f776000-00000000807b5000 WB RUNTIME DATA 00000000807b5000-00000000807b6000 WB|RT BOOT DATA 00000000807b6000-00000000817c0000 WB RUNTIME CODE 00000000817c0000-00000000817d0000 WB|RT BOOT DATA 00000000817d0000-0000000082100000 WB => lmb_dump_all: memory.count = 0x1 memory[0] [0x40000000-0x820fffff], 0x42100000 bytes flags: none reserved.count = 0x5 reserved[0] [0xe100000-0xeffffff], 0x00f00000 bytes flags: no-map reserved[1] [0x42000000-0x421fffff], 0x00200000 bytes flags: no-map reserved[2] [0x42200000-0x42201fff], 0x00002000 bytes flags: no-notify, no-overwrite reserved[3] [0x7f74e000-0x7f77cfff], 0x0002f000 bytes flags: no-notify, no-overwrite reserved[4] [0x7f77da00-0x820fffff], 0x02982600 bytes flags: no-overwrite Link: https://lore.kernel.org/r/20241015153717.401371-1-sughosh.ganu@linaro.org
Diffstat (limited to 'lib/efi_loader/efi_memory.c')
-rw-r--r--lib/efi_loader/efi_memory.c217
1 files changed, 58 insertions, 159 deletions
diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
index c6f1dd09456..b63b5cca71e 100644
--- a/lib/efi_loader/efi_memory.c
+++ b/lib/efi_loader/efi_memory.c
@@ -9,6 +9,7 @@
#include <efi_loader.h>
#include <init.h>
+#include <lmb.h>
#include <log.h>
#include <malloc.h>
#include <mapmem.h>
@@ -172,17 +173,19 @@ static void efi_mem_sort(void)
/**
* 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: 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.
+ * @map: memory map
+ * @carve_desc: memory region to unmap
+ * @overlap_conventional: the carved out region may only overlap free,
+ * or conventional memory
+ * Return: 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_conventional is
+ * true),
+ * EFI_CARVE_LOOP_AGAIN, if the mapping list should
+ * be traversed again, as it has been altered.
*
* Unmaps all memory occupied by the carve_desc region from the list entry
* pointed to by map.
@@ -192,7 +195,7 @@ static void efi_mem_sort(void)
*/
static s64 efi_mem_carve_out(struct efi_mem_list *map,
struct efi_mem_desc *carve_desc,
- bool overlap_only_ram)
+ bool overlap_conventional)
{
struct efi_mem_list *newmap;
struct efi_mem_desc *map_desc = &map->desc;
@@ -207,7 +210,7 @@ static s64 efi_mem_carve_out(struct efi_mem_list *map,
return EFI_CARVE_NO_OVERLAP;
/* We're overlapping with non-RAM, warn the caller if desired */
- if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
+ if (overlap_conventional && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
return EFI_CARVE_OVERLAPS_NONRAM;
/* Sanitize carve_start and carve_end to lie within our bounds */
@@ -257,15 +260,17 @@ static s64 efi_mem_carve_out(struct efi_mem_list *map,
/**
* efi_add_memory_map_pg() - add pages to the memory map
*
- * @start: start address, must be a multiple of EFI_PAGE_SIZE
- * @pages: number of pages to add
- * @memory_type: type of memory added
- * @overlap_only_ram: region may only overlap RAM
- * Return: status code
+ * @start: start address, must be a multiple of
+ * EFI_PAGE_SIZE
+ * @pages: number of pages to add
+ * @memory_type: type of memory added
+ * @overlap_conventional: region may only overlap free(conventional)
+ * memory
+ * Return: status code
*/
-static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
- int memory_type,
- bool overlap_only_ram)
+efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
+ int memory_type,
+ bool overlap_conventional)
{
struct efi_mem_list *lmem;
struct efi_mem_list *newlist;
@@ -274,7 +279,8 @@ static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
struct efi_event *evt;
EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
- start, pages, memory_type, overlap_only_ram ? "yes" : "no");
+ start, pages, memory_type, overlap_conventional ?
+ "yes" : "no");
if (memory_type >= EFI_MAX_MEMORY_TYPE)
return EFI_INVALID_PARAMETER;
@@ -311,7 +317,7 @@ static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
s64 r;
r = efi_mem_carve_out(lmem, &newlist->desc,
- overlap_only_ram);
+ overlap_conventional);
switch (r) {
case EFI_CARVE_OUT_OF_RESOURCES:
free(newlist);
@@ -347,7 +353,7 @@ static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
}
} while (carve_again);
- if (overlap_only_ram && (carved_pages != pages)) {
+ if (overlap_conventional && (carved_pages != pages)) {
/*
* The payload wanted to have RAM overlaps, but we overlapped
* with an unallocated region. Error out.
@@ -433,53 +439,6 @@ static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
}
/**
- * efi_find_free_memory() - find free memory pages
- *
- * @len: size of memory area needed
- * @max_addr: highest address to allocate
- * Return: pointer to free memory area or 0
- */
-static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
-{
- struct efi_mem_list *lmem;
-
- /*
- * Prealign input max address, so we simplify our matching
- * logic below and can just reuse it as return pointer.
- */
- max_addr &= ~EFI_PAGE_MASK;
-
- list_for_each_entry(lmem, &efi_mem, 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;
-}
-
-/**
* efi_allocate_pages - allocate memory pages
*
* @type: type of allocation to be performed
@@ -493,8 +452,9 @@ efi_status_t efi_allocate_pages(enum efi_allocate_type type,
efi_uintn_t pages, uint64_t *memory)
{
u64 len;
+ uint flags;
efi_status_t ret;
- uint64_t addr;
+ phys_addr_t addr;
/* Check import parameters */
if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
@@ -508,33 +468,37 @@ efi_status_t efi_allocate_pages(enum efi_allocate_type type,
(len >> EFI_PAGE_SHIFT) != (u64)pages)
return EFI_OUT_OF_RESOURCES;
+ flags = LMB_NOOVERWRITE | LMB_NONOTIFY;
switch (type) {
case EFI_ALLOCATE_ANY_PAGES:
/* Any page */
- addr = efi_find_free_memory(len, -1ULL);
+ addr = (u64)lmb_alloc_flags(len, EFI_PAGE_SIZE, flags);
if (!addr)
return EFI_OUT_OF_RESOURCES;
break;
case EFI_ALLOCATE_MAX_ADDRESS:
/* Max address */
- addr = efi_find_free_memory(len, *memory);
+ addr = map_to_sysmem((void *)(uintptr_t)*memory);
+ addr = (u64)lmb_alloc_base_flags(len, EFI_PAGE_SIZE, addr,
+ flags);
if (!addr)
return EFI_OUT_OF_RESOURCES;
break;
case EFI_ALLOCATE_ADDRESS:
if (*memory & EFI_PAGE_MASK)
return EFI_NOT_FOUND;
- /* Exact address, reserve it. The addr is already in *memory. */
- ret = efi_check_allocated(*memory, false);
- if (ret != EFI_SUCCESS)
+
+ addr = map_to_sysmem((void *)(uintptr_t)*memory);
+ addr = (u64)lmb_alloc_addr_flags(addr, len, flags);
+ if (!addr)
return EFI_NOT_FOUND;
- addr = *memory;
break;
default:
/* UEFI doesn't specify other allocation types */
return EFI_INVALID_PARAMETER;
}
+ addr = (u64)(uintptr_t)map_sysmem(addr, 0);
/* Reserve that map in our memory maps */
ret = efi_add_memory_map_pg(addr, pages, memory_type, true);
if (ret != EFI_SUCCESS)
@@ -555,6 +519,9 @@ efi_status_t efi_allocate_pages(enum efi_allocate_type type,
*/
efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
{
+ u64 len;
+ uint flags;
+ long status;
efi_status_t ret;
ret = efi_check_allocated(memory, true);
@@ -568,6 +535,13 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
return EFI_INVALID_PARAMETER;
}
+ flags = LMB_NOOVERWRITE | LMB_NONOTIFY;
+ len = (u64)pages << EFI_PAGE_SHIFT;
+ status = lmb_free_flags(map_to_sysmem((void *)(uintptr_t)memory), len,
+ flags);
+ if (status)
+ return EFI_NOT_FOUND;
+
ret = efi_add_memory_map_pg(memory, pages, EFI_CONVENTIONAL_MEMORY,
false);
if (ret != EFI_SUCCESS)
@@ -814,82 +788,17 @@ efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size,
}
/**
- * efi_add_conventional_memory_map() - add a RAM memory area to the map
+ * efi_add_known_memory() - add memory types to the EFI memory map
*
- * @ram_start: start address of a RAM memory area
- * @ram_end: end address of a RAM memory area
- * @ram_top: max address to be used as conventional memory
- * Return: status code
- */
-efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
- u64 ram_top)
-{
- u64 pages;
-
- /* Remove partial pages */
- ram_end &= ~EFI_PAGE_MASK;
- ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
-
- if (ram_end <= ram_start) {
- /* Invalid mapping */
- return EFI_INVALID_PARAMETER;
- }
-
- pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
-
- efi_add_memory_map_pg(ram_start, pages,
- EFI_CONVENTIONAL_MEMORY, false);
-
- /*
- * Boards may indicate to the U-Boot memory core that they
- * can not support memory above ram_top. Let's honor this
- * in the efi_loader subsystem too by declaring any memory
- * above ram_top as "already occupied by firmware".
- */
- if (ram_top < ram_start) {
- /* ram_top is before this region, reserve all */
- efi_add_memory_map_pg(ram_start, pages,
- EFI_BOOT_SERVICES_DATA, true);
- } else if (ram_top < ram_end) {
- /* ram_top is inside this region, reserve parts */
- pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
-
- efi_add_memory_map_pg(ram_top, pages,
- EFI_BOOT_SERVICES_DATA, true);
- }
-
- return EFI_SUCCESS;
-}
-
-/**
- * efi_add_known_memory() - add memory banks to map
+ * This function is to be used to add different memory types other
+ * than EFI_CONVENTIONAL_MEMORY to the EFI memory map. The conventional
+ * memory is handled by the LMB module and gets added to the memory
+ * map through the LMB module.
*
- * This function may be overridden for specific architectures.
+ * This function may be overridden for architectures specific purposes.
*/
__weak void efi_add_known_memory(void)
{
- u64 ram_top = gd->ram_top & ~EFI_PAGE_MASK;
- int i;
-
- /*
- * ram_top is just outside mapped memory. So use an offset of one for
- * mapping the sandbox address.
- */
- ram_top = (uintptr_t)map_sysmem(ram_top - 1, 0) + 1;
-
- /* Fix for 32bit targets with ram_top at 4G */
- if (!ram_top)
- ram_top = 0x100000000ULL;
-
- /* Add RAM */
- for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
- u64 ram_end, ram_start;
-
- ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
- ram_end = ram_start + gd->bd->bi_dram[i].size;
-
- efi_add_conventional_memory_map(ram_start, ram_end, ram_top);
- }
}
/**
@@ -901,16 +810,6 @@ static void add_u_boot_and_runtime(void)
{
unsigned long runtime_start, runtime_end, runtime_pages;
unsigned long runtime_mask = EFI_PAGE_MASK;
- unsigned long uboot_start, uboot_pages;
- unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
-
- /* Add U-Boot */
- uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
- uboot_stack_size) & ~EFI_PAGE_MASK;
- uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
- uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
- efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE,
- false);
#if defined(__aarch64__)
/*