[RFC PATCH 13/31] efi_memory: notify of any changes to the EFI memory map

Sughosh Ganu sughosh.ganu at linaro.org
Fri Jun 7 20:52:22 CEST 2024


In U-Boot, LMB and EFI are two primary modules who provide memory
allocation and reservation API's. Both these modules operate with the
same regions of memory for allocations. Use the EFI memory map update
event to notify other interested listeners about a change in the EFI
memory map. This can then be used by the other module to allocate
memory only from available regions, instead of overwriting already
allocated memory.

Signed-off-by: Sughosh Ganu <sughosh.ganu at linaro.org>
---
 lib/efi_loader/efi_memory.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
index 12cf23fa3f..435e580fb3 100644
--- a/lib/efi_loader/efi_memory.c
+++ b/lib/efi_loader/efi_memory.c
@@ -8,6 +8,7 @@
 #define LOG_CATEGORY LOGC_EFI
 
 #include <efi_loader.h>
+#include <event.h>
 #include <init.h>
 #include <log.h>
 #include <malloc.h>
@@ -36,6 +37,9 @@ struct efi_mem_list {
 #define EFI_CARVE_OVERLAPS_NONRAM	-3
 #define EFI_CARVE_OUT_OF_RESOURCES	-4
 
+#define MAP_OP_RESERVE		(u8)0x1
+#define MAP_OP_FREE		(u8)0x2
+
 /* This list contains all memory map items */
 static LIST_HEAD(efi_mem);
 
@@ -66,6 +70,22 @@ struct efi_pool_allocation {
 	char data[] __aligned(ARCH_DMA_MINALIGN);
 };
 
+#if CONFIG_IS_ENABLED(MEM_MAP_UPDATE_NOTIFY)
+extern bool is_addr_in_ram(uintptr_t addr);
+
+static void efi_map_update_notify(u64 addr, u64 size, u8 op)
+{
+	struct event_efi_mem_map_update efi_map = {0};
+
+	efi_map.base = addr;
+	efi_map.size = size;
+	efi_map.op = op;
+
+	if (is_addr_in_ram((uintptr_t)addr))
+		event_notify(EVT_EFI_MEM_MAP_UPDATE, &efi_map, sizeof(efi_map));
+}
+#endif /* MEM_MAP_UPDATE_NOTIFY */
+
 /**
  * checksum() - calculate checksum for memory allocated from pool
  *
@@ -375,6 +395,11 @@ static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
 		}
 	}
 
+	if (CONFIG_IS_ENABLED(MEM_MAP_UPDATE_NOTIFY))
+		efi_map_update_notify(start, pages << EFI_PAGE_SHIFT,
+				      memory_type == EFI_CONVENTIONAL_MEMORY ?
+				      MAP_OP_FREE : MAP_OP_RESERVE);
+
 	return EFI_SUCCESS;
 }
 
-- 
2.34.1



More information about the U-Boot mailing list