summaryrefslogtreecommitdiff
path: root/lib/lmb.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/lmb.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/lmb.c')
-rw-r--r--lib/lmb.c242
1 files changed, 186 insertions, 56 deletions
diff --git a/lib/lmb.c b/lib/lmb.c
index 380d92a6718..78fe2d4de7c 100644
--- a/lib/lmb.c
+++ b/lib/lmb.c
@@ -8,6 +8,7 @@
#include <alist.h>
#include <efi_loader.h>
+#include <event.h>
#include <image.h>
#include <mapmem.h>
#include <lmb.h>
@@ -22,15 +23,56 @@
DECLARE_GLOBAL_DATA_PTR;
+#define MAP_OP_RESERVE (u8)0x1
+#define MAP_OP_FREE (u8)0x2
+#define MAP_OP_ADD (u8)0x3
+
#define LMB_ALLOC_ANYWHERE 0
#define LMB_ALIST_INITIAL_SIZE 4
static struct lmb lmb;
+static bool lmb_should_notify(enum lmb_flags flags)
+{
+ return !lmb.test && !(flags & LMB_NONOTIFY) &&
+ CONFIG_IS_ENABLED(EFI_LOADER);
+}
+
+static int __maybe_unused lmb_map_update_notify(phys_addr_t addr,
+ phys_size_t size,
+ u8 op)
+{
+ u64 efi_addr;
+ u64 pages;
+ efi_status_t status;
+
+ if (op != MAP_OP_RESERVE && op != MAP_OP_FREE && op != MAP_OP_ADD) {
+ log_err("Invalid map update op received (%d)\n", op);
+ return -1;
+ }
+
+ efi_addr = (uintptr_t)map_sysmem(addr, 0);
+ pages = efi_size_in_pages(size + (efi_addr & EFI_PAGE_MASK));
+ efi_addr &= ~EFI_PAGE_MASK;
+
+ status = efi_add_memory_map_pg(efi_addr, pages,
+ op == MAP_OP_RESERVE ?
+ EFI_BOOT_SERVICES_DATA :
+ EFI_CONVENTIONAL_MEMORY,
+ false);
+ if (status != EFI_SUCCESS) {
+ log_err("%s: LMB Map notify failure %lu\n", __func__,
+ status & ~EFI_ERROR_MASK);
+ return -1;
+ } else {
+ return 0;
+ }
+}
+
static void lmb_print_region_flags(enum lmb_flags flags)
{
u64 bitpos;
- const char *flag_str[] = { "none", "no-map", "no-overwrite" };
+ const char *flag_str[] = { "none", "no-map", "no-overwrite", "no-notify" };
do {
bitpos = flags ? fls(flags) - 1 : 0;
@@ -162,38 +204,6 @@ static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst,
lmb_remove_region(lmb_rgn_lst, r2);
}
-/**
- * efi_lmb_reserve() - add reservations for EFI memory
- *
- * Add reservations for all EFI memory areas that are not
- * EFI_CONVENTIONAL_MEMORY.
- *
- * Return: 0 on success, 1 on failure
- */
-static __maybe_unused int efi_lmb_reserve(void)
-{
- struct efi_mem_desc *memmap = NULL, *map;
- efi_uintn_t i, map_size = 0;
- efi_status_t ret;
-
- ret = efi_get_memory_map_alloc(&map_size, &memmap);
- if (ret != EFI_SUCCESS)
- return 1;
-
- for (i = 0, map = memmap; i < map_size / sizeof(*map); ++map, ++i) {
- if (map->type != EFI_CONVENTIONAL_MEMORY) {
- lmb_reserve_flags(map_to_sysmem((void *)(uintptr_t)
- map->physical_start),
- map->num_pages * EFI_PAGE_SIZE,
- map->type == EFI_RESERVED_MEMORY_TYPE
- ? LMB_NOMAP : LMB_NONE);
- }
- }
- efi_free_pool(memmap);
-
- return 0;
-}
-
static void lmb_reserve_uboot_region(void)
{
int bank;
@@ -240,9 +250,6 @@ static void lmb_reserve_common(void *fdt_blob)
if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
boot_fdt_add_mem_rsv_regions(fdt_blob);
-
- if (CONFIG_IS_ENABLED(EFI_LOADER))
- efi_lmb_reserve();
}
static __maybe_unused void lmb_reserve_common_spl(void)
@@ -281,10 +288,12 @@ void lmb_add_memory(void)
{
int i;
phys_size_t size;
- phys_addr_t rgn_top;
u64 ram_top = gd->ram_top;
struct bd_info *bd = gd->bd;
+ if (CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP))
+ return lmb_arch_add_memory();
+
/* Assume a 4GB ram_top if not defined */
if (!ram_top)
ram_top = 0x100000000ULL;
@@ -292,16 +301,16 @@ void lmb_add_memory(void)
for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
size = bd->bi_dram[i].size;
if (size) {
- if (bd->bi_dram[i].start > ram_top)
- continue;
-
- rgn_top = bd->bi_dram[i].start +
- bd->bi_dram[i].size;
-
- if (rgn_top > ram_top)
- size -= rgn_top - ram_top;
-
lmb_add(bd->bi_dram[i].start, size);
+
+ /*
+ * Reserve memory above ram_top as
+ * no-overwrite so that it cannot be
+ * allocated
+ */
+ if (bd->bi_dram[i].start >= ram_top)
+ lmb_reserve_flags(bd->bi_dram[i].start, size,
+ LMB_NOOVERWRITE);
}
}
}
@@ -474,12 +483,20 @@ static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base,
/* This routine may be called with relocation disabled. */
long lmb_add(phys_addr_t base, phys_size_t size)
{
+ long ret;
struct alist *lmb_rgn_lst = &lmb.free_mem;
- return lmb_add_region(lmb_rgn_lst, base, size);
+ ret = lmb_add_region(lmb_rgn_lst, base, size);
+ if (ret)
+ return ret;
+
+ if (lmb_should_notify(LMB_NONE))
+ return lmb_map_update_notify(base, size, MAP_OP_ADD);
+
+ return 0;
}
-long lmb_free(phys_addr_t base, phys_size_t size)
+static long _lmb_free(phys_addr_t base, phys_size_t size)
{
struct lmb_region *rgn;
struct alist *lmb_rgn_lst = &lmb.used_mem;
@@ -530,11 +547,49 @@ long lmb_free(phys_addr_t base, phys_size_t size)
rgn[i].flags);
}
+/**
+ * lmb_free_flags() - Free up a region of memory
+ * @base: Base Address of region to be freed
+ * @size: Size of the region to be freed
+ * @flags: Memory region attributes
+ *
+ * Free up a region of memory.
+ *
+ * Return: 0 if successful, -1 on failure
+ */
+long lmb_free_flags(phys_addr_t base, phys_size_t size,
+ uint flags)
+{
+ long ret;
+
+ ret = _lmb_free(base, size);
+ if (ret < 0)
+ return ret;
+
+ if (lmb_should_notify(flags))
+ return lmb_map_update_notify(base, size, MAP_OP_FREE);
+
+ return ret;
+}
+
+long lmb_free(phys_addr_t base, phys_size_t size)
+{
+ return lmb_free_flags(base, size, LMB_NONE);
+}
+
long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags)
{
+ long ret = 0;
struct alist *lmb_rgn_lst = &lmb.used_mem;
- return lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
+ ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
+ if (ret < 0)
+ return -1;
+
+ if (lmb_should_notify(flags))
+ return lmb_map_update_notify(base, size, MAP_OP_RESERVE);
+
+ return ret;
}
long lmb_reserve(phys_addr_t base, phys_size_t size)
@@ -563,9 +618,11 @@ static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
return addr & ~(size - 1);
}
-static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align,
+static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align,
phys_addr_t max_addr, enum lmb_flags flags)
{
+ u8 op;
+ int ret;
long i, rgn;
phys_addr_t base = 0;
phys_addr_t res_base;
@@ -596,6 +653,15 @@ static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align,
if (lmb_add_region_flags(&lmb.used_mem, base,
size, flags) < 0)
return 0;
+
+ if (lmb_should_notify(flags)) {
+ op = MAP_OP_RESERVE;
+ ret = lmb_map_update_notify(base, size,
+ op);
+ if (ret)
+ return ret;
+ }
+
return base;
}
@@ -613,11 +679,28 @@ phys_addr_t lmb_alloc(phys_size_t size, ulong align)
return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
}
+/**
+ * lmb_alloc_flags() - Allocate memory region with specified attributes
+ * @size: Size of the region requested
+ * @align: Alignment of the memory region requested
+ * @flags: Memory region attributes to be set
+ *
+ * Allocate a region of memory with the attributes specified through the
+ * parameter.
+ *
+ * Return: base address on success, 0 on error
+ */
+phys_addr_t lmb_alloc_flags(phys_size_t size, ulong align, uint flags)
+{
+ return _lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE,
+ flags);
+}
+
phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
{
phys_addr_t alloc;
- alloc = __lmb_alloc_base(size, align, max_addr, LMB_NONE);
+ alloc = _lmb_alloc_base(size, align, max_addr, LMB_NONE);
if (alloc == 0)
printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
@@ -626,7 +709,34 @@ phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
return alloc;
}
-static phys_addr_t __lmb_alloc_addr(phys_addr_t base, phys_size_t size,
+/**
+ * lmb_alloc_base_flags() - Allocate specified memory region with specified attributes
+ * @size: Size of the region requested
+ * @align: Alignment of the memory region requested
+ * @max_addr: Maximum address of the requested region
+ * @flags: Memory region attributes to be set
+ *
+ * Allocate a region of memory with the attributes specified through the
+ * parameter. The max_addr parameter is used to specify the maximum address
+ * below which the requested region should be allocated.
+ *
+ * Return: base address on success, 0 on error
+ */
+phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
+ phys_addr_t max_addr, uint flags)
+{
+ phys_addr_t alloc;
+
+ alloc = _lmb_alloc_base(size, align, max_addr, flags);
+
+ if (alloc == 0)
+ printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
+ (ulong)size, (ulong)max_addr);
+
+ return alloc;
+}
+
+static phys_addr_t _lmb_alloc_addr(phys_addr_t base, phys_size_t size,
enum lmb_flags flags)
{
long rgn;
@@ -657,7 +767,25 @@ static phys_addr_t __lmb_alloc_addr(phys_addr_t base, phys_size_t size,
*/
phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
{
- return __lmb_alloc_addr(base, size, LMB_NONE);
+ return _lmb_alloc_addr(base, size, LMB_NONE);
+}
+
+/**
+ * lmb_alloc_addr_flags() - Allocate specified memory address with specified attributes
+ * @base: Base Address requested
+ * @size: Size of the region requested
+ * @flags: Memory region attributes to be set
+ *
+ * Allocate a region of memory with the attributes specified through the
+ * parameter. The base parameter is used to specify the base address
+ * of the requested region.
+ *
+ * Return: base address on success, 0 on error
+ */
+phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
+ uint flags)
+{
+ return _lmb_alloc_addr(base, size, flags);
}
/* Return number of bytes from a given address that are free */
@@ -703,7 +831,7 @@ int lmb_is_reserved_flags(phys_addr_t addr, int flags)
return 0;
}
-static int lmb_setup(void)
+static int lmb_setup(bool test)
{
bool ret;
@@ -721,6 +849,8 @@ static int lmb_setup(void)
return -ENOMEM;
}
+ lmb.test = test;
+
return 0;
}
@@ -740,7 +870,7 @@ int lmb_init(void)
{
int ret;
- ret = lmb_setup();
+ ret = lmb_setup(false);
if (ret) {
log_info("Unable to init LMB\n");
return ret;
@@ -768,7 +898,7 @@ int lmb_push(struct lmb *store)
int ret;
*store = lmb;
- ret = lmb_setup();
+ ret = lmb_setup(true);
if (ret)
return ret;