From patchwork Fri Jun 7 18:52:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 802326 Delivered-To: patch@linaro.org Received: by 2002:adf:f147:0:b0:35b:5a80:51b4 with SMTP id y7csp949323wro; Fri, 7 Jun 2024 11:56:13 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWt2NsONsfjBQiqseF3MPVL7fv6pk/2Ffl0AGHqYnS7fpZT/q+JRvf7L3NtNOfeRTAyTeNBAxS4bBqksDKWiHK3 X-Google-Smtp-Source: AGHT+IGU3ggJQQ+Q53NUjQT0LJ3FiMC0AfTjcnUpjRbDI2/EyYp6VHfaXQmk9hFk6fEmKxL6MXK9 X-Received: by 2002:a17:907:1c0a:b0:a69:384e:29a2 with SMTP id a640c23a62f3a-a6cd56126bdmr305284766b.12.1717786573062; Fri, 07 Jun 2024 11:56:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1717786573; cv=none; d=google.com; s=arc-20160816; b=vXB4EzEDSZAlIQ3VkqRZNnvlnspR1Bv9l5RYNPQ3DgDsd930wSg21USIKHozEyg5Zx ZROQeZW0LWLPhb8iyxZunM8rZOWyB0p9gtz82q4pQY/aOXNghj4A9KwLRJ81s/WD+FR9 wwbXHldPWyH1NwbSulnyd1aGj5da80VULlPzL9zF5q8SxzJJLa0jCC6Kh8K4wRVSe2v0 pmEGWpcMaNlIDaK5WcW1PQ2wwgxCIY4MBW2CSRb8o7JKl4mqBWmdvPI1mvteBokOkCdK HKcoE+Me5x4vWyRYLqHrV1C6qZSWKpUyToJTpsp6soR8R95EAv7bSKxVjzTWmboLUEC4 KeuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=0dOk7j5plQSgnGx7mFJAgN8kXrzsJDDzWIq01YJb2Ps=; fh=xLSOOz5Q0Mrmzd+lwBRTYS7D/8TpUWM8TEHgIjpMzow=; b=hEscs2Pch0itz6LvcZCsGKYRYlD5Yk4xxatvczr+8tcWrevRiReUXwAX52vrwYRRo8 ZbVUanzjJqOW/mcFFVX28MoyqD4GHDNuCjnYwyIEZb0VQQ+2PFeMQTwg3RmOlmb//WP1 /SejY0WpcQQucsA2uIl5+A+HfhnWgsaR9GJz9SMIMk/jrFeInx53596Tyol1qrqjAaOQ LVZHzOB0UoUJxFtjnvLBa6oeaOf7ny3o8fgk/qsgb150CuDp+bg5yVBFfhXDDiEgPImo BxRdAZ9cS9IG9WZB3tvmmYvlopJtZPBz1g4/I1wiIZeNiYVFDjz4pK98g3/j9GPKF70v zb6A==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id a640c23a62f3a-a6c8070bfb5si218351166b.553.2024.06.07.11.56.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 11:56:13 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 973BA8851E; Fri, 7 Jun 2024 20:54:24 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id CDC2A88464; Fri, 7 Jun 2024 20:54:23 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, SPF_HELO_NONE,SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id 9BFFC8850B for ; Fri, 7 Jun 2024 20:54:21 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sughosh.ganu@linaro.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 81AEB15A1; Fri, 7 Jun 2024 11:54:45 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.162.17.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5B9B93F792; Fri, 7 Jun 2024 11:54:18 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Tom Rini , Ilias Apalodimas , Heinrich Schuchardt , Simon Glass , Marek Vasut , Mark Kettenis , Fabio Estevam , Sughosh Ganu Subject: [RFC PATCH 15/31] efi_memory: add an event handler to update memory map Date: Sat, 8 Jun 2024 00:22:24 +0530 Message-Id: <20240607185240.1892031-16-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240607185240.1892031-1-sughosh.ganu@linaro.org> References: <20240607185240.1892031-1-sughosh.ganu@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean There are events that would be used to notify other interested modules of any changes in available and occupied memory. This would happen when a module allocates or reserves memory, or frees up memory. These changes in memory map should be notified to other interested modules so that the allocated memory does not get overwritten. Add an event handler in the EFI memory module to update the EFI memory map accordingly when such changes happen. As a consequence, any subsequent memory request would honour the updated memory map and only available memory would be allocated from. Signed-off-by: Sughosh Ganu --- lib/efi_loader/efi_memory.c | 70 ++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c index 435e580fb3..93244161b0 100644 --- a/lib/efi_loader/efi_memory.c +++ b/lib/efi_loader/efi_memory.c @@ -73,6 +73,10 @@ struct efi_pool_allocation { #if CONFIG_IS_ENABLED(MEM_MAP_UPDATE_NOTIFY) extern bool is_addr_in_ram(uintptr_t addr); +static efi_status_t __efi_add_memory_map_pg(u64 start, u64 pages, + int memory_type, + bool overlap_only_ram); + static void efi_map_update_notify(u64 addr, u64 size, u8 op) { struct event_efi_mem_map_update efi_map = {0}; @@ -84,6 +88,34 @@ static void efi_map_update_notify(u64 addr, u64 size, u8 op) if (is_addr_in_ram((uintptr_t)addr)) event_notify(EVT_EFI_MEM_MAP_UPDATE, &efi_map, sizeof(efi_map)); } + +static int lmb_mem_map_update_sync(void *ctx, struct event *event) +{ + u8 op; + u64 addr; + u64 pages; + efi_status_t status; + struct event_lmb_map_update *lmb_map = &event->data.lmb_map; + + addr = (uintptr_t)map_sysmem(lmb_map->base, 0); + pages = efi_size_in_pages(lmb_map->size + (addr & EFI_PAGE_MASK)); + op = lmb_map->op; + addr &= ~EFI_PAGE_MASK; + + if (op != MAP_OP_RESERVE && op != MAP_OP_FREE) { + log_debug("Invalid map update op received (%d)\n", op); + return -1; + } + + status = __efi_add_memory_map_pg(addr, pages, + op == MAP_OP_FREE ? + EFI_CONVENTIONAL_MEMORY : + EFI_BOOT_SERVICES_DATA, + true); + + return status == EFI_SUCCESS ? 0 : -1; +} +EVENT_SPY_FULL(EVT_LMB_MAP_UPDATE, lmb_mem_map_update_sync); #endif /* MEM_MAP_UPDATE_NOTIFY */ /** @@ -275,18 +307,9 @@ static s64 efi_mem_carve_out(struct efi_mem_list *map, return EFI_CARVE_LOOP_AGAIN; } -/** - * 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 - */ -static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages, - int memory_type, - bool overlap_only_ram) +static efi_status_t __efi_add_memory_map_pg(u64 start, u64 pages, + int memory_type, + bool overlap_only_ram) { struct list_head *lhandle; struct efi_mem_list *newlist; @@ -395,6 +418,29 @@ static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages, } } + return EFI_SUCCESS; +} + +/** + * 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 + */ +static efi_status_t efi_add_memory_map_pg(u64 start, u64 pages, + int memory_type, + bool overlap_only_ram) +{ + efi_status_t status; + + status = __efi_add_memory_map_pg(start, pages, memory_type, + overlap_only_ram); + if (status != EFI_SUCCESS) + return status; + if (CONFIG_IS_ENABLED(MEM_MAP_UPDATE_NOTIFY)) efi_map_update_notify(start, pages << EFI_PAGE_SHIFT, memory_type == EFI_CONVENTIONAL_MEMORY ?