From patchwork Tue Jun 17 10:43:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 897364 Delivered-To: patch@linaro.org Received: by 2002:adf:9b99:0:b0:3a4:ee3f:8f15 with SMTP id d25csp2073765wrc; Tue, 17 Jun 2025 03:44:19 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWNSWwMs5VsrB/P9C6q2IwKLrEVtusDX2LYTY4f3L3helxBYk8J9HE3X+QoZ4ZBJXIuTw6iww==@linaro.org X-Google-Smtp-Source: AGHT+IGlpdjd3JEjIBjg6sKwFxLlqajn3BXl39JlpXPh1gZ1Y5RZ9GH5T/xBvjY9v2nOBGrIzoJW X-Received: by 2002:ac8:5808:0:b0:4a4:4da5:8b55 with SMTP id d75a77b69052e-4a73c5f7cb6mr199177091cf.28.1750157059433; Tue, 17 Jun 2025 03:44:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1750157059; cv=none; d=google.com; s=arc-20240605; b=bS7hRWMFD8wLsyd3DTa2j5l5J0+VqD2svwirM16IGte+NrvUKLOxV+ejGmMmIreZw3 IHCymApUCttbaGPU8QVfKtKQYtHLuiU8WZmc5QtPKxdNW8fC6uW7zbHS4dVk5rfHmVHA oOHxUvNiPiusphmFPk6rSydqXkB3YADYuB9rukWwwLkGF7AFLAtJlkczktQl1TxLCMT8 KWvIebkoeLSxOyGURs71g3Vlc8nzEtju42aK5Wda2hAyw5tih6p2RBryC0umFa/mCNRC MW1d9N8mAP7bx62dktW/pTesRsOZJbjmpHmWiQDrOVOEgLbQpD8FNBSg5GQV3N3W+FxV xjdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=/eAf5bAe8HdFPvcUwQjsfUTM0x7vEj+BHihgNJ409iY=; fh=OQsgrM7lYcbKid/8QRKDrRnr3aZuZ+FGeYy6ua/5St0=; b=g4XtFfcL76bZxrWbeRrwaRdx3XltQBNkIV1hWctdl1cejlMqOlu55hK3wL61IDT5WY p2hFdRUtJavnujotIpaZYF+f4v4xYTWYDtt0afWnuSPuIIC/lEQUhEVf/WjzIJ0SUE+2 YOm5I+Skgu2+zqm59Ao4DPSntV4DTzcarXpTBVXhI14U620bhojBvcvG+7A/lzR3JEJ1 VzBomj+XR5cBbunpAxD/1CgzSoZB82RoMIRdx9JH2kEqpD6cMoQDeVezVLHOFEFSY5Z1 /7YRWokXMb/2Ii5RXjn19uB53yOI1lEjRrHvr/8H8nWKZQbPXz5g3/OptRzMfWlZaGc6 YiKA==; 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 d75a77b69052e-4a72a2e718bsi129726791cf.163.2025.06.17.03.44.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 03:44:19 -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 D863482CE7; Tue, 17 Jun 2025 12:44:12 +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 8294382BCD; Tue, 17 Jun 2025 12:44:11 +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_DNSWL_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE,SPF_SOFTFAIL 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 AA80282CBC for ; Tue, 17 Jun 2025 12:44:08 +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 134DD1596; Tue, 17 Jun 2025 03:43:47 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.164.21.38]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 578CA3F58B; Tue, 17 Jun 2025 03:44:04 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Tom Rini , Casey Connolly , Neil Armstrong , Mark Kettenis , Weijie Gao , Heinrich Schuchardt , Simon Glass , Sughosh Ganu Subject: [PATCH v5 1/7] lmb: replace lmb_reserve() and lmb_alloc_addr() API's Date: Tue, 17 Jun 2025 16:13:40 +0530 Message-Id: <20250617104346.1379981-2-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250617104346.1379981-1-sughosh.ganu@linaro.org> References: <20250617104346.1379981-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 currently are multiple allocation API's in the LMB module. There are a couple of API's for allocating memory(lmb_alloc() and lmb_alloc_base()), and then there are two for requesting a reservation for a particular memory region (lmb_reserve() and lmb_alloc_addr()). Introduce a single API lmb_alloc_mem() which will cater to all types of allocation requests and replace lmb_reserve() and lmb_alloc_addr() with the new API. Moreover, the lmb_reserve() API is pretty similar to the lmb_alloc_addr() API, with the one difference being that the lmb_reserve() API allows for reserving any address passed to it -- the address need not be part of the LMB memory map. The lmb_alloc_addr() does check that the address being requested is actually part of the LMB memory map. There is no need to support reserving memory regions which are outside the LMB memory map. Remove the lmb_reserve() API functionality and use the functionality provided by lmb_alloc_addr() instead. The lmb_alloc_addr() will check if the requested address is part of the LMB memory map and return an error if not. Signed-off-by: Sughosh Ganu Acked-by: Ilias Apalodimas --- Changes since V4: * Remove the superfluous return from _lmb_alloc_addr() arch/arm/mach-mediatek/tzcfg.c | 8 +- arch/powerpc/cpu/mpc85xx/mp.c | 4 +- arch/powerpc/lib/misc.c | 5 +- boot/bootm.c | 15 +++- boot/image-board.c | 11 ++- boot/image-fdt.c | 32 ++++++-- cmd/booti.c | 10 ++- cmd/bootz.c | 10 ++- cmd/load.c | 9 ++- fs/fs.c | 5 +- include/lmb.h | 69 ++++++++++-------- lib/efi_loader/efi_memory.c | 2 +- lib/lmb.c | 129 +++++++++++++++++++-------------- test/lib/lmb.c | 27 +++++-- 14 files changed, 223 insertions(+), 113 deletions(-) diff --git a/arch/arm/mach-mediatek/tzcfg.c b/arch/arm/mach-mediatek/tzcfg.c index 71982ba4d20..c8fe8ac0e9b 100644 --- a/arch/arm/mach-mediatek/tzcfg.c +++ b/arch/arm/mach-mediatek/tzcfg.c @@ -173,6 +173,7 @@ phys_addr_t board_get_usable_ram_top(phys_size_t total_size) int arch_misc_init(void) { + phys_addr_t addr; struct arm_smccc_res res; /* @@ -180,11 +181,14 @@ int arch_misc_init(void) * there's no need to check the result */ arm_smccc_smc(MTK_SIP_GET_BL31_REGION, 0, 0, 0, 0, 0, 0, 0, &res); - lmb_reserve(res.a1, res.a2, LMB_NOMAP); + addr = (phys_addr_t)res.a1; + lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr, res.a2, LMB_NOMAP); arm_smccc_smc(MTK_SIP_GET_BL32_REGION, 0, 0, 0, 0, 0, 0, 0, &res); + addr = (phys_addr_t)res.a1; if (!res.a0 && res.a1 && res.a2) - lmb_reserve(res.a1, res.a2, LMB_NOMAP); + lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr, res.a2, + LMB_NOMAP); #if IS_ENABLED(CONFIG_CMD_PSTORE) char cmd[64]; diff --git a/arch/powerpc/cpu/mpc85xx/mp.c b/arch/powerpc/cpu/mpc85xx/mp.c index 8918a401fac..bee6758dc9a 100644 --- a/arch/powerpc/cpu/mpc85xx/mp.c +++ b/arch/powerpc/cpu/mpc85xx/mp.c @@ -410,9 +410,9 @@ static void plat_mp_up(unsigned long bootpg, unsigned int pagesize) void cpu_mp_lmb_reserve(void) { - u32 bootpg = determine_mp_bootpg(NULL); + phys_addr_t bootpg = determine_mp_bootpg(NULL); - lmb_reserve(bootpg, 4096, LMB_NONE); + lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &bootpg, 4096, LMB_NONE); } void setup_mp(void) diff --git a/arch/powerpc/lib/misc.c b/arch/powerpc/lib/misc.c index 7e303419624..fc10ae50cf8 100644 --- a/arch/powerpc/lib/misc.c +++ b/arch/powerpc/lib/misc.c @@ -36,11 +36,12 @@ int arch_misc_init(void) size = min(size, (ulong)CFG_SYS_LINUX_LOWMEM_MAX_SIZE); if (size < bootm_size) { - ulong base = bootmap_base + size; + phys_addr_t base = bootmap_base + size; printf("WARNING: adjusting available memory from 0x%lx to 0x%llx\n", size, (unsigned long long)bootm_size); - lmb_reserve(base, bootm_size - size, LMB_NONE); + lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &base, + bootm_size - size, LMB_NONE); } #ifdef CONFIG_MP diff --git a/boot/bootm.c b/boot/bootm.c index 108ca7fb472..3282bfc0b4b 100644 --- a/boot/bootm.c +++ b/boot/bootm.c @@ -698,9 +698,18 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress) images->os.end = relocated_addr + image_size; } - if (CONFIG_IS_ENABLED(LMB)) - lmb_reserve(images->os.load, (load_end - images->os.load), - LMB_NONE); + if (CONFIG_IS_ENABLED(LMB)) { + phys_addr_t load; + + load = (phys_addr_t)images->os.load; + err = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &load, + (load_end - images->os.load), LMB_NONE); + if (err) { + log_err("Unable to allocate memory %#lx for loading OS\n", + images->os.load); + return 1; + } + } return 0; } diff --git a/boot/image-board.c b/boot/image-board.c index 514f8e63f9c..b0fa028ceac 100644 --- a/boot/image-board.c +++ b/boot/image-board.c @@ -538,6 +538,7 @@ int boot_get_ramdisk(char const *select, struct bootm_headers *images, int boot_ramdisk_high(ulong rd_data, ulong rd_len, ulong *initrd_start, ulong *initrd_end) { + int err; char *s; phys_addr_t initrd_high; int initrd_copy_to_ram = 1; @@ -559,10 +560,18 @@ int boot_ramdisk_high(ulong rd_data, ulong rd_len, ulong *initrd_start, if (rd_data) { if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */ + phys_addr_t initrd_addr; + debug(" in-place initrd\n"); *initrd_start = rd_data; *initrd_end = rd_data + rd_len; - lmb_reserve(rd_data, rd_len, LMB_NONE); + initrd_addr = (phys_addr_t)rd_data; + err = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, + &initrd_addr, rd_len, LMB_NONE); + if (err) { + puts("in-place initrd alloc failed\n"); + goto error; + } } else { if (initrd_high) *initrd_start = diff --git a/boot/image-fdt.c b/boot/image-fdt.c index 8f718ad29f6..bd5a6231140 100644 --- a/boot/image-fdt.c +++ b/boot/image-fdt.c @@ -72,13 +72,15 @@ static const struct legacy_img_hdr *image_get_fdt(ulong fdt_addr) static void boot_fdt_reserve_region(u64 addr, u64 size, u32 flags) { long ret; + phys_addr_t rsv_addr; - ret = lmb_reserve(addr, size, flags); + rsv_addr = (phys_addr_t)addr; + ret = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &rsv_addr, size, flags); if (!ret) { debug(" reserving fdt memory region: addr=%llx size=%llx flags=%x\n", (unsigned long long)addr, (unsigned long long)size, flags); - } else if (ret != -EEXIST) { + } else if (ret != -EEXIST && ret != -EINVAL) { puts("ERROR: reserving fdt memory region failed "); printf("(addr=%llx size=%llx flags=%x)\n", (unsigned long long)addr, @@ -155,7 +157,7 @@ void boot_fdt_add_mem_rsv_regions(void *fdt_blob) */ int boot_relocate_fdt(char **of_flat_tree, ulong *of_size) { - u64 start, size, usable, addr, low, mapsize; + u64 start, size, usable, low, mapsize; void *fdt_blob = *of_flat_tree; void *of_start = NULL; char *fdt_high; @@ -163,6 +165,7 @@ int boot_relocate_fdt(char **of_flat_tree, ulong *of_size) int bank; int err; int disable_relocation = 0; + phys_addr_t addr; /* nothing to do */ if (*of_size == 0) @@ -185,7 +188,15 @@ int boot_relocate_fdt(char **of_flat_tree, ulong *of_size) if (desired_addr == ~0UL) { /* All ones means use fdt in place */ of_start = fdt_blob; - lmb_reserve(map_to_sysmem(of_start), of_len, LMB_NONE); + addr = map_to_sysmem(fdt_blob); + err = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr, + of_len, LMB_NONE); + if (err) { + printf("Failed to reserve memory for fdt at %#llx\n", + (u64)addr); + goto error; + } + disable_relocation = 1; } else if (desired_addr) { addr = lmb_alloc_base(of_len, 0x1000, desired_addr, @@ -682,8 +693,17 @@ int image_setup_libfdt(struct bootm_headers *images, void *blob, bool lmb) of_size = ret; /* Create a new LMB reservation */ - if (CONFIG_IS_ENABLED(LMB) && lmb) - lmb_reserve(map_to_sysmem(blob), of_size, LMB_NONE); + if (CONFIG_IS_ENABLED(LMB) && lmb) { + phys_addr_t fdt_addr; + + fdt_addr = map_to_sysmem(blob); + ret = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &fdt_addr, + of_size, LMB_NONE); + if (ret) { + printf("Failed to reserve memory for the fdt at %#llx\n", + (u64)fdt_addr); + } + } #if defined(CONFIG_ARCH_KEYSTONE) if (IS_ENABLED(CONFIG_OF_BOARD_SETUP)) diff --git a/cmd/booti.c b/cmd/booti.c index 7e6d9426299..e6f67d6e136 100644 --- a/cmd/booti.c +++ b/cmd/booti.c @@ -30,6 +30,7 @@ static int booti_start(struct bootm_info *bmi) uint8_t *temp; ulong dest; ulong dest_end; + phys_addr_t ep_addr; unsigned long comp_len; unsigned long decomp_len; int ctype; @@ -88,7 +89,14 @@ static int booti_start(struct bootm_info *bmi) images->os.start = relocated_addr; images->os.end = relocated_addr + image_size; - lmb_reserve(images->ep, le32_to_cpu(image_size), LMB_NONE); + ep_addr = (phys_addr_t)images->ep; + ret = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &ep_addr, + le32_to_cpu(image_size), LMB_NONE); + if (ret) { + printf("Failed to allocate memory for the image at %#llx\n", + (unsigned long long)images->ep); + return 1; + } /* * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not diff --git a/cmd/bootz.c b/cmd/bootz.c index 99318ff213f..44af7d012aa 100644 --- a/cmd/bootz.c +++ b/cmd/bootz.c @@ -28,6 +28,7 @@ static int bootz_start(struct cmd_tbl *cmdtp, int flag, int argc, { ulong zi_start, zi_end; struct bootm_info bmi; + phys_addr_t ep_addr; int ret; bootm_init(&bmi); @@ -56,7 +57,14 @@ static int bootz_start(struct cmd_tbl *cmdtp, int flag, int argc, if (ret != 0) return 1; - lmb_reserve(images->ep, zi_end - zi_start, LMB_NONE); + ep_addr = (phys_addr_t)images->ep; + ret = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &ep_addr, zi_end - zi_start, + LMB_NONE); + if (ret) { + printf("Failed to allocate memory for the image at %#llx\n", + (unsigned long long)images->ep); + return 1; + } /* * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not diff --git a/cmd/load.c b/cmd/load.c index 899bb4f598e..a6e54fc668c 100644 --- a/cmd/load.c +++ b/cmd/load.c @@ -178,17 +178,20 @@ static ulong load_serial(long offset) #endif { void *dst; + phys_addr_t dst_addr; - ret = lmb_reserve(store_addr, binlen, LMB_NONE); + dst_addr = (phys_addr_t)store_addr; + ret = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &dst_addr, + binlen, LMB_NONE); if (ret) { printf("\nCannot overwrite reserved area (%08lx..%08lx)\n", store_addr, store_addr + binlen); return ret; } - dst = map_sysmem(store_addr, binlen); + dst = map_sysmem(dst_addr, binlen); memcpy(dst, binbuf, binlen); unmap_sysmem(dst); - lmb_free(store_addr, binlen); + lmb_free(dst_addr, binlen); } if ((store_addr) < start_addr) start_addr = store_addr; diff --git a/fs/fs.c b/fs/fs.c index 1f36872fb9a..2650328b358 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -580,6 +580,7 @@ static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset, int ret; loff_t size; loff_t read_len; + phys_addr_t read_addr; /* get the actual size of the file */ ret = info->size(filename, &size); @@ -597,7 +598,9 @@ static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset, lmb_dump_all(); - if (!lmb_alloc_addr(addr, read_len, LMB_NONE)) + read_addr = (phys_addr_t)addr; + if (!lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &read_addr, read_len, + LMB_NONE)) return 0; log_err("** Reading file would overwrite reserved memory **\n"); diff --git a/include/lmb.h b/include/lmb.h index 606a92cca48..8906b42181f 100644 --- a/include/lmb.h +++ b/include/lmb.h @@ -31,6 +31,14 @@ #define LMB_NOOVERWRITE BIT(2) #define LMB_NONOTIFY BIT(3) +/** + * enum lmb_mem_type - type of memory allocation request + * @LMB_MEM_ALLOC_ADDR: request for a particular region of memory + */ +enum lmb_mem_type { + LMB_MEM_ALLOC_ADDR = 1, +}; + /** * enum lmb_map_op - memory map operation */ @@ -67,6 +75,37 @@ struct lmb { bool test; }; +/** + * lmb_alloc_mem() - Request LMB memory + * @type: Type of memory allocation request + * @align: Alignment of the memory region requested(0 for none) + * @addr: Base address of the allocated memory region + * @size: Size in bytes of the allocation request + * @flags: Memory region attributes to be set + * + * Allocate a region of memory where the allocation is based on the parameters + * that have been passed to the function.The first parameter specifies the + * type of allocation that is being requested. The second parameter, @align + * is used to specify if the allocation is to be made with a particular + * alignment. Use 0 for no alignment requirements. + * + * The allocated address is returned through the @addr parameter when @type + * is @LMB_MEM_ALLOC_ANY or @LMB_MEM_ALLOC_MAX. If @type is + * @LMB_MEM_ALLOC_ADDR the @addr parameter would contain the address being + * requested. + * + * The flags parameter is used to specify the memory attributes of the + * requested region. + * + * Return: 0 on success, -ve value on failure + * + * When the allocation is of type @LMB_MEM_ALLOC_ADDR, the return value can + * be -EINVAL if the requested memory region is not part of the LMB memory + * map, and -EEXIST if the requested region is already allocated. + */ +int lmb_alloc_mem(enum lmb_mem_type type, u64 align, phys_addr_t *addr, + phys_size_t size, u32 flags); + /** * lmb_init() - Initialise the LMB module. * @@ -91,19 +130,6 @@ void lmb_add_memory(void); long lmb_add(phys_addr_t base, phys_size_t size); -/** - * lmb_reserve() - Reserve one region with a specific flags bitfield - * @base: Base address of the memory region - * @size: Size of the memory region - * @flags: Flags for the memory region - * - * Return: - * * %0 - Added successfully, or it's already added (only if LMB_NONE) - * * %-EEXIST - The region is already added, and flags != LMB_NONE - * * %-1 - Failure - */ -long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags); - phys_addr_t lmb_alloc(phys_size_t size, ulong align); phys_size_t lmb_get_free_size(phys_addr_t addr); @@ -124,21 +150,6 @@ phys_size_t lmb_get_free_size(phys_addr_t addr); phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr, uint flags); -/** - * lmb_alloc_addr() - 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: 0 on success -1 on error - */ -int lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags); - /** * lmb_is_reserved_flags() - Test if address is in reserved region with flag * bits set @@ -175,7 +186,7 @@ void lmb_pop(struct lmb *store); static inline int lmb_read_check(phys_addr_t addr, phys_size_t len) { - return lmb_alloc_addr(addr, len, LMB_NONE); + return lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr, len, LMB_NONE); } /** diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c index 0abb1f6159a..77950a267cc 100644 --- a/lib/efi_loader/efi_memory.c +++ b/lib/efi_loader/efi_memory.c @@ -493,7 +493,7 @@ efi_status_t efi_allocate_pages(enum efi_allocate_type type, return EFI_NOT_FOUND; addr = map_to_sysmem((void *)(uintptr_t)*memory); - if (lmb_alloc_addr(addr, len, flags)) + if (lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr, len, flags)) return EFI_NOT_FOUND; break; default: diff --git a/lib/lmb.c b/lib/lmb.c index bb6f232f6bc..226cb6f1d9f 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -488,6 +488,54 @@ void lmb_dump_all(void) #endif } +/** + * lmb_can_reserve_region() - check if the region can be reserved + * @base: base address of region to be reserved + * @size: size of region to be reserved + * @flags: flag of the region to be reserved + * + * Go through all the reserved regions and ensure that the requested + * region does not overlap with any existing regions. An overlap is + * allowed only when the flag of the request region and the existing + * region is LMB_NONE. + * + * Return: true if region can be reserved, false otherwise + */ +static bool lmb_can_reserve_region(phys_addr_t base, phys_size_t size, + u32 flags) +{ + uint i; + struct lmb_region *lmb_reserved = lmb.used_mem.data; + + for (i = 0; i < lmb.used_mem.count; i++) { + u32 rgnflags = lmb_reserved[i].flags; + phys_addr_t rgnbase = lmb_reserved[i].base; + phys_size_t rgnsize = lmb_reserved[i].size; + + if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { + if (flags != LMB_NONE || flags != rgnflags) + return false; + } + } + + return true; +} + +static long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags) +{ + long ret = 0; + struct alist *lmb_rgn_lst = &lmb.used_mem; + + if (!lmb_can_reserve_region(base, size, flags)) + return -EEXIST; + + ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags); + if (ret) + return ret; + + return lmb_map_update_notify(base, size, LMB_MAP_OP_RESERVE, flags); +} + static void lmb_reserve_uboot_region(void) { int bank; @@ -557,39 +605,6 @@ static __maybe_unused void lmb_reserve_common_spl(void) } } -/** - * lmb_can_reserve_region() - check if the region can be reserved - * @base: base address of region to be reserved - * @size: size of region to be reserved - * @flags: flag of the region to be reserved - * - * Go through all the reserved regions and ensure that the requested - * region does not overlap with any existing regions. An overlap is - * allowed only when the flag of the request region and the existing - * region is LMB_NONE. - * - * Return: true if region can be reserved, false otherwise - */ -static bool lmb_can_reserve_region(phys_addr_t base, phys_size_t size, - u32 flags) -{ - uint i; - struct lmb_region *lmb_reserved = lmb.used_mem.data; - - for (i = 0; i < lmb.used_mem.count; i++) { - u32 rgnflags = lmb_reserved[i].flags; - phys_addr_t rgnbase = lmb_reserved[i].base; - phys_size_t rgnsize = lmb_reserved[i].size; - - if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { - if (flags != LMB_NONE || flags != rgnflags) - return false; - } - } - - return true; -} - void lmb_add_memory(void) { int i; @@ -657,21 +672,6 @@ long lmb_free(phys_addr_t base, phys_size_t size) return lmb_free_flags(base, size, LMB_NONE); } -long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags) -{ - long ret = 0; - struct alist *lmb_rgn_lst = &lmb.used_mem; - - if (!lmb_can_reserve_region(base, size, flags)) - return -EEXIST; - - ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags); - if (ret) - return ret; - - return lmb_map_update_notify(base, size, LMB_MAP_OP_RESERVE, flags); -} - static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr, u32 flags) { @@ -742,7 +742,7 @@ phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr, return _lmb_alloc_base(size, align, max_addr, flags); } -int lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags) +static int _lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags) { long rgn; struct lmb_region *lmb_memory = lmb.available_mem.data; @@ -756,14 +756,35 @@ int lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags) */ if (lmb_addrs_overlap(lmb_memory[rgn].base, lmb_memory[rgn].size, - base + size - 1, 1)) { + base + size - 1, 1)) /* ok, reserve the memory */ - if (!lmb_reserve(base, size, flags)) - return 0; - } + return lmb_reserve(base, size, flags); + } + + return -EINVAL; +} + +int lmb_alloc_mem(enum lmb_mem_type type, u64 align, phys_addr_t *addr, + phys_size_t size, u32 flags) +{ + int ret = -1; + + if (!size) + return 0; + + if (!addr) + return -EINVAL; + + switch (type) { + case LMB_MEM_ALLOC_ADDR: + ret = _lmb_alloc_addr(*addr, size, flags); + break; + default: + log_debug("%s: Invalid memory allocation type requested %d\n", + __func__, type); } - return -1; + return ret; } /* Return number of bytes from a given address that are free */ diff --git a/test/lib/lmb.c b/test/lib/lmb.c index 3bf558f7f4f..751909fc2cf 100644 --- a/test/lib/lmb.c +++ b/test/lib/lmb.c @@ -71,6 +71,19 @@ static int setup_lmb_test(struct unit_test_state *uts, struct lmb *store, return 0; } +static int lmb_reserve(phys_addr_t addr, phys_size_t size, u32 flags) +{ + int err; + + err = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr, size, flags); + if (err) + return err; + + return 0; +} + +#define lmb_alloc_addr(addr, size, flags) lmb_reserve(addr, size, flags) + static int test_multi_alloc(struct unit_test_state *uts, const phys_addr_t ram, const phys_size_t ram_size, const phys_addr_t ram0, const phys_size_t ram0_size, @@ -568,7 +581,7 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE); ut_asserteq(b, 0); b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE); - ut_asserteq(b, -1); + ut_asserteq(b, -EEXIST); b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE); ut_asserteq(b, 0); b = lmb_alloc_addr(alloc_addr_a, 0x2000, LMB_NONE); @@ -578,9 +591,9 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE); ut_asserteq(b, 0); b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE); - ut_asserteq(b, -1); + ut_asserteq(b, -EEXIST); b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE); - ut_asserteq(b, -1); + ut_asserteq(b, -EEXIST); ret = lmb_free(alloc_addr_a, 0x1000); ut_asserteq(ret, 0); @@ -599,7 +612,7 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) alloc_addr_a + 0x4000, 0x1000, 0, 0); c = lmb_alloc_addr(alloc_addr_a + 0x1000, 0x5000, LMB_NONE); - ut_asserteq(c, -1); + ut_asserteq(c, -EEXIST); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, alloc_addr_a, 0x1000, alloc_addr_a + 0x4000, 0x1000, 0, 0); @@ -646,7 +659,7 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) alloc_addr_a + 0x4000, 0x1000, 0, 0); c = lmb_alloc_addr(alloc_addr_a + 0x1000, 0x5000, LMB_NOOVERWRITE); - ut_asserteq(c, -1); + ut_asserteq(c, -EEXIST); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, alloc_addr_a, 0x1000, alloc_addr_a + 0x4000, 0x1000, 0, 0); @@ -739,11 +752,11 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) /* check that allocating outside memory fails */ if (ram_end != 0) { ret = lmb_alloc_addr(ram_end, 1, LMB_NONE); - ut_asserteq(ret, -1); + ut_asserteq(ret, -EINVAL); } if (ram != 0) { ret = lmb_alloc_addr(ram - 1, 1, LMB_NONE); - ut_asserteq(ret, -1); + ut_asserteq(ret, -EINVAL); } lmb_pop(&store); From patchwork Tue Jun 17 10:43:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 897365 Delivered-To: patch@linaro.org Received: by 2002:adf:9b99:0:b0:3a4:ee3f:8f15 with SMTP id d25csp2073818wrc; Tue, 17 Jun 2025 03:44:30 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCX1HGN68gZ8IQmZlcUsMRfXKwot460dBmzOOqxx/Dv1KBqdVHNg0QxyqlzJT8H5Srk3NGxcow==@linaro.org X-Google-Smtp-Source: AGHT+IGZh7McIAXLk0sfRTGbw4R1ya67ugHjMMiUzW7hUTFlJjaRkxWz3Xzvzvcll7KjlXWA47Or X-Received: by 2002:a05:6214:1d24:b0:6fa:ad2a:7998 with SMTP id 6a1803df08f44-6fb46defcf0mr195981806d6.18.1750157070366; Tue, 17 Jun 2025 03:44:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1750157070; cv=none; d=google.com; s=arc-20240605; b=SUGgnB05OaeYp+5xjY9+4esXHJk3lpxpA0JiWJ8CA7YuQ2ApRKFsgrbJai1CFxZU1H lbpHgds1fBSb8h26bXEqWaBsRT7CAwDCuXYCHe4rkPeyug8rgX2t6OVn7IUoXI6D9xXB KhKfZo4ZDac4ZAmpmtoV9tjuuBEVZrXjhlpQrBDOdb+wgYRppHFXykh9ccrHn82UPrGP hDceM6waHPaOMmcCu5iRLP0fhhmoKdXEJP36AbSLsFKmgSvTiGB4nYxcRn+n6SwNcu1g 23nbvj/8uCMMDH3Y1PSjAqZnkf87QXS93i/td+JcgNjkzMtKDMQKFxAxDnpvDTm7p9du 8lpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=dYlBSd+QIU9R24a8ROoi1mguIEmAr73JqK9Gynf3J5s=; fh=OQsgrM7lYcbKid/8QRKDrRnr3aZuZ+FGeYy6ua/5St0=; b=lc3ZZGR5qBjbn0E6iNwyV9erdYrw3c0En9Zed6wCSdG1ge2H6KvgUfoe0mSrtdkdmT DmjokwZNn/2iKrDPCj+I8yqhCVzCGo0GMEdC4AHzctEAABexaXAcwCFsymVvIrcPPLUk 19aYF0YEfAP/XVp0BW/V5wAfkKCQ+vRQ8ljIF0/rCZNICa/Zp2S8DPLHet4fk0xbQ/dd ZqOcpFUo6NyK/ZpogixeUXRu6+60xOa0nRfPh4NtBIVKgXIkTMZukcqUC3KMXGnKopNp e8shecuOeQHlZv4hlyWEly32POB1ExMc9qv/QZ823V6gEGMKD2GmOOViuEZp7+vMJbhl X3rQ==; 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 6a1803df08f44-6fb5942718csi29181966d6.448.2025.06.17.03.44.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 03:44:30 -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 24AD882CBC; Tue, 17 Jun 2025 12:44:16 +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 1253C82CCE; Tue, 17 Jun 2025 12:44:15 +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_DNSWL_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE,SPF_SOFTFAIL 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 80ACA82BCD for ; Tue, 17 Jun 2025 12:44:12 +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 DDB7C1596; Tue, 17 Jun 2025 03:43:50 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.164.21.38]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B38A13F58B; Tue, 17 Jun 2025 03:44:08 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Tom Rini , Casey Connolly , Neil Armstrong , Mark Kettenis , Weijie Gao , Heinrich Schuchardt , Simon Glass , Sughosh Ganu Subject: [PATCH v5 2/7] lmb: replace the lmb_alloc() and lmb_alloc_base() API's Date: Tue, 17 Jun 2025 16:13:41 +0530 Message-Id: <20250617104346.1379981-3-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250617104346.1379981-1-sughosh.ganu@linaro.org> References: <20250617104346.1379981-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 currently are two API's for requesting memory from the LMB module, lmb_alloc() and lmb_alloc_base(). The function which does the actual allocation is the same. Use the earlier introduced API lmb_alloc_mem() for both types of allocation requests. Signed-off-by: Sughosh Ganu Acked-by: Ilias Apalodimas --- Changes since V4: None arch/arm/mach-apple/board.c | 27 ++++++++++++------ arch/arm/mach-snapdragon/board.c | 30 ++++++++++++------- boot/bootm.c | 12 +++++--- boot/image-board.c | 49 +++++++++++++++++--------------- boot/image-fdt.c | 35 +++++++++++++---------- include/lmb.h | 22 +++----------- lib/efi_loader/efi_memory.c | 14 +++++---- lib/lmb.c | 28 ++++++++---------- test/lib/lmb.c | 26 +++++++++++++++++ 9 files changed, 141 insertions(+), 102 deletions(-) diff --git a/arch/arm/mach-apple/board.c b/arch/arm/mach-apple/board.c index 2604c5a710e..4cd8979bdc2 100644 --- a/arch/arm/mach-apple/board.c +++ b/arch/arm/mach-apple/board.c @@ -773,22 +773,31 @@ u64 get_page_table_size(void) #define KERNEL_COMP_SIZE SZ_128M +#define lmb_alloc(size, addr) lmb_alloc_mem(LMB_MEM_ALLOC_ANY, SZ_2M, addr, size, LMB_NONE) + int board_late_init(void) { u32 status = 0; + phys_addr_t addr; /* somewhat based on the Linux Kernel boot requirements: * align by 2M and maximal FDT size 2M */ - status |= env_set_hex("loadaddr", lmb_alloc(SZ_1G, SZ_2M)); - status |= env_set_hex("fdt_addr_r", lmb_alloc(SZ_2M, SZ_2M)); - status |= env_set_hex("kernel_addr_r", lmb_alloc(SZ_128M, SZ_2M)); - status |= env_set_hex("ramdisk_addr_r", lmb_alloc(SZ_1G, SZ_2M)); - status |= env_set_hex("kernel_comp_addr_r", - lmb_alloc(KERNEL_COMP_SIZE, SZ_2M)); - status |= env_set_hex("kernel_comp_size", KERNEL_COMP_SIZE); - status |= env_set_hex("scriptaddr", lmb_alloc(SZ_4M, SZ_2M)); - status |= env_set_hex("pxefile_addr_r", lmb_alloc(SZ_4M, SZ_2M)); + status |= !lmb_alloc(SZ_1G, &addr) ? env_set_hex("loadaddr", addr) : 1; + status |= !lmb_alloc(SZ_2M, &addr) ? + env_set_hex("fdt_addr_r", addr) : 1; + status |= !lmb_alloc(SZ_128M, &addr) ? + env_set_hex("kernel_addr_r", addr) : 1; + status |= !lmb_alloc(SZ_1G, &addr) ? + env_set_hex("ramdisk_addr_r", addr) : 1; + status |= !lmb_alloc(KERNEL_COMP_SIZE, &addr) ? + env_set_hex("kernel_comp_addr_r", addr) : 1; + status |= !lmb_alloc(KERNEL_COMP_SIZE, &addr) ? + env_set_hex("kernel_comp_size", addr) : 1; + status |= !lmb_alloc(SZ_4M, &addr) ? + env_set_hex("scriptaddr", addr) : 1; + status |= !lmb_alloc(SZ_4M, &addr) ? + env_set_hex("pxefile_addr_r", addr) : 1; if (status) log_warning("late_init: Failed to set run time variables\n"); diff --git a/arch/arm/mach-snapdragon/board.c b/arch/arm/mach-snapdragon/board.c index 5547d6d054f..87a173e0acb 100644 --- a/arch/arm/mach-snapdragon/board.c +++ b/arch/arm/mach-snapdragon/board.c @@ -492,7 +492,7 @@ void __weak qcom_late_init(void) #define FASTBOOT_BUF_SIZE 0 #endif -#define addr_alloc(size) lmb_alloc(size, SZ_2M) +#define lmb_alloc(size, addr) lmb_alloc_mem(LMB_MEM_ALLOC_ANY, SZ_2M, addr, size, LMB_NONE) /* Stolen from arch/arm/mach-apple/board.c */ int board_late_init(void) @@ -502,18 +502,26 @@ int board_late_init(void) struct fdt_header *fdt_blob = (struct fdt_header *)gd->fdt_blob; /* We need to be fairly conservative here as we support boards with just 1G of TOTAL RAM */ - addr = addr_alloc(SZ_128M); + status |= !lmb_alloc(SZ_128M, &addr) ? + env_set_hex("loadaddr", addr) : 1; status |= env_set_hex("kernel_addr_r", addr); - status |= env_set_hex("loadaddr", addr); - status |= env_set_hex("ramdisk_addr_r", addr_alloc(SZ_128M)); - status |= env_set_hex("kernel_comp_addr_r", addr_alloc(KERNEL_COMP_SIZE)); - status |= env_set_hex("kernel_comp_size", KERNEL_COMP_SIZE); + status |= !lmb_alloc(SZ_128M, &addr) ? + env_set_hex("ramdisk_addr_r", addr) : 1; + status |= !lmb_alloc(KERNEL_COMP_SIZE, &addr) ? + env_set_hex("kernel_comp_addr_r", addr) : 1; + status |= !lmb_alloc(KERNEL_COMP_SIZE, &addr) ? + env_set_hex("kernel_comp_size", addr) : 1; + status |= !lmb_alloc(SZ_4M, &addr) ? + env_set_hex("scriptaddr", addr) : 1; + status |= !lmb_alloc(SZ_4M, &addr) ? + env_set_hex("pxefile_addr_r", addr) : 1; + if (IS_ENABLED(CONFIG_FASTBOOT)) - status |= env_set_hex("fastboot_addr_r", addr_alloc(FASTBOOT_BUF_SIZE)); - status |= env_set_hex("scriptaddr", addr_alloc(SZ_4M)); - status |= env_set_hex("pxefile_addr_r", addr_alloc(SZ_4M)); - addr = addr_alloc(SZ_2M); - status |= env_set_hex("fdt_addr_r", addr); + status |= !lmb_alloc(FASTBOOT_BUF_SIZE, &addr) ? + env_set_hex("fastboot_addr_r", addr) : 1; + + status |= !lmb_alloc(SZ_2M, &addr) ? + env_set_hex("fdt_addr_r", addr) : 1; if (status) log_warning("%s: Failed to set run time variables\n", __func__); diff --git a/boot/bootm.c b/boot/bootm.c index 3282bfc0b4b..4bdca22ea8c 100644 --- a/boot/bootm.c +++ b/boot/bootm.c @@ -623,12 +623,16 @@ static int bootm_load_os(struct bootm_headers *images, int boot_progress) */ if (os.type == IH_TYPE_KERNEL_NOLOAD && os.comp != IH_COMP_NONE) { ulong req_size = ALIGN(image_len * 4, SZ_1M); + phys_addr_t addr; - load = lmb_alloc(req_size, SZ_2M); - if (!load) + err = lmb_alloc_mem(LMB_MEM_ALLOC_ANY, SZ_2M, &addr, + req_size, LMB_NONE); + if (err) return 1; - os.load = load; - images->ep = load; + + load = (ulong)addr; + os.load = (ulong)addr; + images->ep = (ulong)addr; debug("Allocated %lx bytes at %lx for kernel (size %lx) decompression\n", req_size, load, image_len); } diff --git a/boot/image-board.c b/boot/image-board.c index b0fa028ceac..005d60caf5c 100644 --- a/boot/image-board.c +++ b/boot/image-board.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -566,27 +567,24 @@ int boot_ramdisk_high(ulong rd_data, ulong rd_len, ulong *initrd_start, *initrd_start = rd_data; *initrd_end = rd_data + rd_len; initrd_addr = (phys_addr_t)rd_data; - err = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, - &initrd_addr, rd_len, LMB_NONE); + err = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &initrd_addr, + rd_len, LMB_NONE); if (err) { puts("in-place initrd alloc failed\n"); goto error; } } else { - if (initrd_high) - *initrd_start = - (ulong)lmb_alloc_base(rd_len, - 0x1000, - initrd_high, - LMB_NONE); - else - *initrd_start = (ulong)lmb_alloc(rd_len, - 0x1000); + enum lmb_mem_type type = initrd_high ? + LMB_MEM_ALLOC_MAX : LMB_MEM_ALLOC_ANY; - if (*initrd_start == 0) { + err = lmb_alloc_mem(type, 0x1000, &initrd_high, rd_len, + LMB_NONE); + if (err) { puts("ramdisk - allocation error\n"); goto error; } + + *initrd_start = (ulong)initrd_high; bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK); *initrd_end = *initrd_start + rd_len; @@ -837,9 +835,10 @@ int boot_get_loadable(struct bootm_headers *images) */ int boot_get_cmdline(ulong *cmd_start, ulong *cmd_end) { - int barg; + int barg, err; char *cmdline; char *s; + phys_addr_t addr; /* * Help the compiler detect that this function is only called when @@ -849,12 +848,14 @@ int boot_get_cmdline(ulong *cmd_start, ulong *cmd_end) return 0; barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE); - cmdline = (char *)(ulong)lmb_alloc_base(barg, 0xf, - env_get_bootm_mapsize() + env_get_bootm_low(), - LMB_NONE); - if (!cmdline) + addr = env_get_bootm_mapsize() + env_get_bootm_low(); + + err = lmb_alloc_mem(LMB_MEM_ALLOC_MAX, 0xf, &addr, barg, LMB_NONE); + if (err) return -1; + cmdline = (char *)(uintptr_t)addr; + s = env_get("bootargs"); if (!s) s = ""; @@ -883,14 +884,16 @@ int boot_get_cmdline(ulong *cmd_start, ulong *cmd_end) */ int boot_get_kbd(struct bd_info **kbd) { - *kbd = (struct bd_info *)(ulong)lmb_alloc_base(sizeof(struct bd_info), - 0xf, - env_get_bootm_mapsize() + - env_get_bootm_low(), - LMB_NONE); - if (!*kbd) + int err; + phys_addr_t addr; + + addr = env_get_bootm_mapsize() + env_get_bootm_low(); + err = lmb_alloc_mem(LMB_MEM_ALLOC_MAX, 0xf, &addr, + sizeof(struct bd_info), LMB_NONE); + if (err) return -1; + *kbd = (struct bd_info *)(uintptr_t)addr; **kbd = *gd->bd; debug("## kernel board info at 0x%08lx\n", (ulong)*kbd); diff --git a/boot/image-fdt.c b/boot/image-fdt.c index bd5a6231140..2720ce6f6f3 100644 --- a/boot/image-fdt.c +++ b/boot/image-fdt.c @@ -183,9 +183,9 @@ int boot_relocate_fdt(char **of_flat_tree, ulong *of_size) /* If fdt_high is set use it to select the relocation address */ fdt_high = env_get("fdt_high"); if (fdt_high) { - ulong desired_addr = hextoul(fdt_high, NULL); + ulong high_addr = hextoul(fdt_high, NULL); - if (desired_addr == ~0UL) { + if (high_addr == ~0UL) { /* All ones means use fdt in place */ of_start = fdt_blob; addr = map_to_sysmem(fdt_blob); @@ -198,16 +198,17 @@ int boot_relocate_fdt(char **of_flat_tree, ulong *of_size) } disable_relocation = 1; - } else if (desired_addr) { - addr = lmb_alloc_base(of_len, 0x1000, desired_addr, - LMB_NONE); - of_start = map_sysmem(addr, of_len); - if (of_start == NULL) { - puts("Failed using fdt_high value for Device Tree"); + } else { + enum lmb_mem_type type = high_addr ? + LMB_MEM_ALLOC_MAX : LMB_MEM_ALLOC_ANY; + + addr = high_addr; + err = lmb_alloc_mem(type, 0x1000, &addr, of_len, + LMB_NONE); + if (err) { + puts("Failed to allocate memory for Device Tree relocation\n"); goto error; } - } else { - addr = lmb_alloc(of_len, 0x1000); of_start = map_sysmem(addr, of_len); } } else { @@ -229,11 +230,15 @@ int boot_relocate_fdt(char **of_flat_tree, ulong *of_size) * for LMB allocation. */ usable = min(start + size, low + mapsize); - addr = lmb_alloc_base(of_len, 0x1000, usable, LMB_NONE); - of_start = map_sysmem(addr, of_len); - /* Allocation succeeded, use this block. */ - if (of_start != NULL) - break; + addr = usable; + err = lmb_alloc_mem(LMB_MEM_ALLOC_MAX, 0x1000, + &addr, of_len, LMB_NONE); + if (!err) { + of_start = map_sysmem(addr, of_len); + /* Allocation succeeded, use this block. */ + if (of_start) + break; + } /* * Reduce the mapping size in the next bank diff --git a/include/lmb.h b/include/lmb.h index 8906b42181f..34dbc25759d 100644 --- a/include/lmb.h +++ b/include/lmb.h @@ -34,9 +34,13 @@ /** * enum lmb_mem_type - type of memory allocation request * @LMB_MEM_ALLOC_ADDR: request for a particular region of memory + * @LMB_MEM_ALLOC_ANY: allocate any available memory region + * @LMB_MEM_ALLOC_MAX: allocate memory below a particular address */ enum lmb_mem_type { LMB_MEM_ALLOC_ADDR = 1, + LMB_MEM_ALLOC_ANY, + LMB_MEM_ALLOC_MAX, }; /** @@ -130,26 +134,8 @@ void lmb_add_memory(void); long lmb_add(phys_addr_t base, phys_size_t size); -phys_addr_t lmb_alloc(phys_size_t size, ulong align); phys_size_t lmb_get_free_size(phys_addr_t addr); -/** - * lmb_alloc_base() - 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(phys_size_t size, ulong align, phys_addr_t max_addr, - uint flags); - /** * lmb_is_reserved_flags() - Test if address is in reserved region with flag * bits set diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c index 77950a267cc..466f45c98b1 100644 --- a/lib/efi_loader/efi_memory.c +++ b/lib/efi_loader/efi_memory.c @@ -454,6 +454,7 @@ efi_status_t efi_allocate_pages(enum efi_allocate_type type, enum efi_memory_type memory_type, efi_uintn_t pages, uint64_t *memory) { + int err; u64 efi_addr, len; uint flags; efi_status_t ret; @@ -475,17 +476,18 @@ efi_status_t efi_allocate_pages(enum efi_allocate_type type, switch (type) { case EFI_ALLOCATE_ANY_PAGES: /* Any page */ - addr = (u64)lmb_alloc_base(len, EFI_PAGE_SIZE, - LMB_ALLOC_ANYWHERE, flags); - if (!addr) + err = lmb_alloc_mem(LMB_MEM_ALLOC_ANY, EFI_PAGE_SIZE, &addr, + len, flags); + if (err) return EFI_OUT_OF_RESOURCES; break; case EFI_ALLOCATE_MAX_ADDRESS: /* Max address */ addr = map_to_sysmem((void *)(uintptr_t)*memory); - addr = (u64)lmb_alloc_base(len, EFI_PAGE_SIZE, addr, - flags); - if (!addr) + + err = lmb_alloc_mem(LMB_MEM_ALLOC_MAX, EFI_PAGE_SIZE, &addr, + len, flags); + if (err) return EFI_OUT_OF_RESOURCES; break; case EFI_ALLOCATE_ADDRESS: diff --git a/lib/lmb.c b/lib/lmb.c index 226cb6f1d9f..a30ae64f8cd 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -672,16 +672,18 @@ long lmb_free(phys_addr_t base, phys_size_t size) return lmb_free_flags(base, size, LMB_NONE); } -static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align, - phys_addr_t max_addr, u32 flags) +static int _lmb_alloc_base(phys_size_t size, ulong align, + phys_addr_t *addr, u32 flags) { int ret; long i, rgn; + phys_addr_t max_addr; phys_addr_t base = 0; phys_addr_t res_base; struct lmb_region *lmb_used = lmb.used_mem.data; struct lmb_region *lmb_memory = lmb.available_mem.data; + max_addr = *addr; for (i = lmb.available_mem.count - 1; i >= 0; i--) { phys_addr_t lmbbase = lmb_memory[i].base; phys_size_t lmbsize = lmb_memory[i].size; @@ -714,8 +716,8 @@ static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align, flags); if (ret) return ret; - - return base; + *addr = base; + return 0; } res_base = lmb_used[rgn].base; @@ -728,18 +730,7 @@ static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align, log_debug("%s: Failed to allocate 0x%lx bytes below 0x%lx\n", __func__, (ulong)size, (ulong)max_addr); - return 0; -} - -phys_addr_t lmb_alloc(phys_size_t size, ulong align) -{ - return _lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE, LMB_NONE); -} - -phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr, - uint flags) -{ - return _lmb_alloc_base(size, align, max_addr, flags); + return -1; } static int _lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags) @@ -776,6 +767,11 @@ int lmb_alloc_mem(enum lmb_mem_type type, u64 align, phys_addr_t *addr, return -EINVAL; switch (type) { + case LMB_MEM_ALLOC_ANY: + *addr = LMB_ALLOC_ANYWHERE; + case LMB_MEM_ALLOC_MAX: + ret = _lmb_alloc_base(size, align, addr, flags); + break; case LMB_MEM_ALLOC_ADDR: ret = _lmb_alloc_addr(*addr, size, flags); break; diff --git a/test/lib/lmb.c b/test/lib/lmb.c index 751909fc2cf..d8eab96527a 100644 --- a/test/lib/lmb.c +++ b/test/lib/lmb.c @@ -82,6 +82,32 @@ static int lmb_reserve(phys_addr_t addr, phys_size_t size, u32 flags) return 0; } +static phys_addr_t lmb_alloc(phys_size_t size, ulong align) +{ + int err; + phys_addr_t addr; + + err = lmb_alloc_mem(LMB_MEM_ALLOC_ANY, align, &addr, size, LMB_NONE); + if (err) + return 0; + + return addr; +} + +static phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, + phys_addr_t max_addr, u32 flags) +{ + int err; + phys_addr_t addr; + + addr = max_addr; + err = lmb_alloc_mem(LMB_MEM_ALLOC_MAX, align, &addr, size, flags); + if (err) + return 0; + + return addr; +} + #define lmb_alloc_addr(addr, size, flags) lmb_reserve(addr, size, flags) static int test_multi_alloc(struct unit_test_state *uts, const phys_addr_t ram, From patchwork Tue Jun 17 10:43:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 897366 Delivered-To: patch@linaro.org Received: by 2002:adf:9b99:0:b0:3a4:ee3f:8f15 with SMTP id d25csp2073868wrc; Tue, 17 Jun 2025 03:44:41 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWD/pDvx8tuqzTbHRTwJwpZRPs7qmJbplRSB4N15F+i1a3TrHCKmOLE9egxsj2s3GYwYf1nvQ==@linaro.org X-Google-Smtp-Source: AGHT+IF0bUhoWb6V7O4u3pUNsi8wToSReoAvEiSUwJVKwQmD3gzEFJbQW5VPOsie23NU0x/W+VnY X-Received: by 2002:ac8:5955:0:b0:494:7043:8a2 with SMTP id d75a77b69052e-4a75b964380mr31708501cf.16.1750157080730; Tue, 17 Jun 2025 03:44:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1750157080; cv=none; d=google.com; s=arc-20240605; b=VRt5CvmZdkuyP2ZdzqzWrm7k+jOWfKoSIrbfWirqkULc2vmkJXucM1waWqg9WEeIwH p9ACed+oUFEoka5FxbTXgkrZ9Bq1KpYLvsw9PpgKGSi25epKnBHUAsW8YwmNBnRqGFBw 2Hh9UfEIQZCAeRuEWuPk6vjns3iqEBoK10rOMCcyxbgh9mvVpdTXuNZYIdF2dpnJ8nqt GW9m/wi1PH5saS+Dh+QJOOhsunNv7HVhwo2fAt/PSgAzZLzMI9qtVU/LN4g03UwXX1Sl +AfvQZXVBRZvQQ54hdba+j/sFDR95jx9jD3Sykz00mW5E5JoElz72hsYQogC2f0KQdQn ai9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=XjtD1v0qlCStE/zM0vTyP8f4PNxMLcRfjBaiXRt9upk=; fh=OQsgrM7lYcbKid/8QRKDrRnr3aZuZ+FGeYy6ua/5St0=; b=gt7IhSWpRncP4499ZCN57vtwLcY4Jr9Bu29GqlpEURysdr52TlGRTuJEuUj4NzVq+j Vcu7dFwXjZjFJ2P+bKkX80LxnZIA+TgNMh+OiNSyA2Eznqu9HsQ76Ek3ZMYgTI+LZGSI eyRJHgg5Qd7fKm4TrhBBzxWi8CJSeWjxtcrgZYiefWGDXYk/2bU1XGEv5KS9uStSvdSp uVg20wh05scFG1nKpSiGCycFlY4uEI5//ubgi7uvxS/ve6F5cHkOlVCJB04s4raqQdSm 1iDyL+ATIGvenJwNcXfElu9yGPSTcm04bRfy8D3BGGDWFlloFFYbYK6T8Lc4dIXxXYU8 bsrA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id d75a77b69052e-4a72a53e58csi117608881cf.605.2025.06.17.03.44.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 03:44:40 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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 B757582D60; Tue, 17 Jun 2025 12:44:19 +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 5441382D60; Tue, 17 Jun 2025 12:44:18 +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_DNSWL_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE,SPF_SOFTFAIL 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 575B682D57 for ; Tue, 17 Jun 2025 12:44:16 +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 B4B7E1596; Tue, 17 Jun 2025 03:43:54 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.164.21.38]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 8A0343F58B; Tue, 17 Jun 2025 03:44:12 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Tom Rini , Casey Connolly , Neil Armstrong , Mark Kettenis , Weijie Gao , Heinrich Schuchardt , Simon Glass , Sughosh Ganu Subject: [PATCH v5 3/7] lmb: staticise lmb_add_memory() Date: Tue, 17 Jun 2025 16:13:42 +0530 Message-Id: <20250617104346.1379981-4-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250617104346.1379981-1-sughosh.ganu@linaro.org> References: <20250617104346.1379981-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 lmb_add_memory() is only called from the lmb module. Mark the function as static. Signed-off-by: Sughosh Ganu Reviewed-by: Ilias Apalodimas --- Changes since V4: None include/lmb.h | 8 -------- lib/lmb.c | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/include/lmb.h b/include/lmb.h index 34dbc25759d..aead799d555 100644 --- a/include/lmb.h +++ b/include/lmb.h @@ -124,14 +124,6 @@ int lmb_alloc_mem(enum lmb_mem_type type, u64 align, phys_addr_t *addr, */ int lmb_init(void); -/** - * lmb_add_memory() - Add memory range for LMB allocations. - * - * Add the entire available memory range to the pool of memory that - * can be used by the LMB module for allocations. - */ -void lmb_add_memory(void); - long lmb_add(phys_addr_t base, phys_size_t size); phys_size_t lmb_get_free_size(phys_addr_t addr); diff --git a/lib/lmb.c b/lib/lmb.c index a30ae64f8cd..3265fad317d 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -605,7 +605,7 @@ static __maybe_unused void lmb_reserve_common_spl(void) } } -void lmb_add_memory(void) +static void lmb_add_memory(void) { int i; phys_addr_t bank_end; From patchwork Tue Jun 17 10:43:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 897367 Delivered-To: patch@linaro.org Received: by 2002:adf:9b99:0:b0:3a4:ee3f:8f15 with SMTP id d25csp2073920wrc; Tue, 17 Jun 2025 03:44:52 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUonAfWiuXn78QsNRuUs/MDx9f83R5EGBJQ+hpCLCB/fIT3I6hWsuq50tP5vPRM6X/8vILiKw==@linaro.org X-Google-Smtp-Source: AGHT+IGngIJ7w97NT0lAGD6Ff/dM6FNMdFyR3c1rrvGNaVOJyEFtRxAfURQzdp+WbU3vxC4OwhBr X-Received: by 2002:a05:620a:249:b0:7d3:e56e:4fd8 with SMTP id af79cd13be357-7d3e56e4ffamr26309585a.12.1750157091728; Tue, 17 Jun 2025 03:44:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1750157091; cv=none; d=google.com; s=arc-20240605; b=It1Q6xSmBYYv9fXT9JF1/ajLzjzvmp3Eh3VlDnQ8qtFtoMMgEN8awCUJpYNarta/aq kwrmdV8x9DFjUkvsgl00j8jROa8Y/1xU8HwtF5UlFc08SQoxr1UQo5vJOSQYjyTHF57Y ittEpZ+khD30KtVrBEKm0yf5P3c+lE4jqMe9uud0eR56M8jXfLT0pmdIXHyVB9pzEWTt Nj5jvYlYpY5qEdEIHfohDzO2j+jeDQTwwdA4hotqHBnpI/Wh9qHLxNP2kg07vrvKAaJ/ be035684TT/cyixqsr1cqJ6+Lk36GFTaW5O4SPlnJUohcY+Fqiru4AyauOctl20XygU0 zy3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=dSp2BgUvb6EwA0EY0rkE5Fka8ibAhVXENeznayMFT+Q=; fh=OQsgrM7lYcbKid/8QRKDrRnr3aZuZ+FGeYy6ua/5St0=; b=UNtsLWMfhGby+9ulMLrEiFpbnBL3XO+U3BkoIcYHtk/l4652wK9aKIRV4tdr7JIoFg ZynJMLAYDIg//x53w4P0pJn3iyA2sf7BmIYPvmWvC6o87CdeYWJrPAbbxMLYvY4yIyIL 4Du5c8z91XAjcj8IvnBJgNioztjbSQab4xycm3riB/Py1KjhPBILSlPpS58XP0pHlBqX VX619UWfcxLTILYBAEW2yzgf+qkj/LXmgkmemp/WiTa3BQ/BMubULM3PcCZ8/xVv1LK0 PyRB/lvIa6tJSZPTRiZqKvPg7JdX2Do8+VZ2DXSbzFZ7g4xnWIUgU+eFOqn+Pmy+VPng XabQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id af79cd13be357-7d3b8ecedc0si1162697085a.347.2025.06.17.03.44.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 03:44:51 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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 0A9F882CCE; Tue, 17 Jun 2025 12:44: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 B00AB82BCD; Tue, 17 Jun 2025 12:44:22 +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_DNSWL_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE,SPF_SOFTFAIL 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 31C7782C79 for ; Tue, 17 Jun 2025 12:44:20 +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 8B3C61596; Tue, 17 Jun 2025 03:43:58 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.164.21.38]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 60A1E3F58B; Tue, 17 Jun 2025 03:44:16 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Tom Rini , Casey Connolly , Neil Armstrong , Mark Kettenis , Weijie Gao , Heinrich Schuchardt , Simon Glass , Sughosh Ganu Subject: [PATCH v5 4/7] lmb: use a single function to free up memory Date: Tue, 17 Jun 2025 16:13:43 +0530 Message-Id: <20250617104346.1379981-5-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250617104346.1379981-1-sughosh.ganu@linaro.org> References: <20250617104346.1379981-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 is no need to have two separate API's for freeing up memory. Use a single API lmb_free() to achieve this. Signed-off-by: Sughosh Ganu Reviewed-by: Ilias Apalodimas --- Changes since V4: None boot/image-fdt.c | 2 +- cmd/load.c | 2 +- include/lmb.h | 6 ++--- lib/efi_loader/efi_memory.c | 6 ++--- lib/lmb.c | 8 +----- test/lib/lmb.c | 49 +++++++++++++++++++------------------ 6 files changed, 33 insertions(+), 40 deletions(-) diff --git a/boot/image-fdt.c b/boot/image-fdt.c index 2720ce6f6f3..97b6385ab7c 100644 --- a/boot/image-fdt.c +++ b/boot/image-fdt.c @@ -690,7 +690,7 @@ int image_setup_libfdt(struct bootm_headers *images, void *blob, bool lmb) /* Delete the old LMB reservation */ if (CONFIG_IS_ENABLED(LMB) && lmb) - lmb_free(map_to_sysmem(blob), fdt_totalsize(blob)); + lmb_free(map_to_sysmem(blob), fdt_totalsize(blob), LMB_NONE); ret = fdt_shrink_to_minimum(blob, 0); if (ret < 0) diff --git a/cmd/load.c b/cmd/load.c index a6e54fc668c..159767aa7f7 100644 --- a/cmd/load.c +++ b/cmd/load.c @@ -191,7 +191,7 @@ static ulong load_serial(long offset) dst = map_sysmem(dst_addr, binlen); memcpy(dst, binbuf, binlen); unmap_sysmem(dst); - lmb_free(dst_addr, binlen); + lmb_free(dst_addr, binlen, LMB_NONE); } if ((store_addr) < start_addr) start_addr = store_addr; diff --git a/include/lmb.h b/include/lmb.h index aead799d555..5d5f037ccb9 100644 --- a/include/lmb.h +++ b/include/lmb.h @@ -142,16 +142,14 @@ phys_size_t lmb_get_free_size(phys_addr_t addr); int lmb_is_reserved_flags(phys_addr_t addr, int flags); /** - * lmb_free_flags() - Free up a region of memory + * lmb_free() - 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 * * Return: 0 on success, negative error code on failure. */ -long lmb_free_flags(phys_addr_t base, phys_size_t size, uint flags); - -long lmb_free(phys_addr_t base, phys_size_t size); +long lmb_free(phys_addr_t base, phys_size_t size, u32 flags); void lmb_dump_all(void); void lmb_dump_all_force(void); diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c index 466f45c98b1..0828a47da61 100644 --- a/lib/efi_loader/efi_memory.c +++ b/lib/efi_loader/efi_memory.c @@ -508,7 +508,7 @@ efi_status_t efi_allocate_pages(enum efi_allocate_type type, ret = efi_update_memory_map(efi_addr, pages, memory_type, true, false); if (ret != EFI_SUCCESS) { /* Map would overlap, bail out */ - lmb_free_flags(addr, (u64)pages << EFI_PAGE_SHIFT, flags); + lmb_free(addr, (u64)pages << EFI_PAGE_SHIFT, flags); unmap_sysmem((void *)(uintptr_t)efi_addr); return EFI_OUT_OF_RESOURCES; } @@ -548,8 +548,8 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages) * been mapped with map_sysmem() from efi_allocate_pages(). Convert * it back to an address LMB understands */ - status = lmb_free_flags(map_to_sysmem((void *)(uintptr_t)memory), len, - LMB_NOOVERWRITE); + status = lmb_free(map_to_sysmem((void *)(uintptr_t)memory), len, + LMB_NOOVERWRITE); if (status) return EFI_NOT_FOUND; diff --git a/lib/lmb.c b/lib/lmb.c index 3265fad317d..4af8dae4359 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -655,8 +655,7 @@ long lmb_add(phys_addr_t base, phys_size_t size) return lmb_map_update_notify(base, size, LMB_MAP_OP_ADD, LMB_NONE); } -long lmb_free_flags(phys_addr_t base, phys_size_t size, - uint flags) +long lmb_free(phys_addr_t base, phys_size_t size, u32 flags) { long ret; @@ -667,11 +666,6 @@ long lmb_free_flags(phys_addr_t base, phys_size_t size, return lmb_map_update_notify(base, size, LMB_MAP_OP_FREE, flags); } -long lmb_free(phys_addr_t base, phys_size_t size) -{ - return lmb_free_flags(base, size, LMB_NONE); -} - static int _lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t *addr, u32 flags) { diff --git a/test/lib/lmb.c b/test/lib/lmb.c index d8eab96527a..b6259bef442 100644 --- a/test/lib/lmb.c +++ b/test/lib/lmb.c @@ -182,7 +182,7 @@ static int test_multi_alloc(struct unit_test_state *uts, const phys_addr_t ram, ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0); - ret = lmb_free(a, 4); + ret = lmb_free(a, 4, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0); @@ -191,12 +191,12 @@ static int test_multi_alloc(struct unit_test_state *uts, const phys_addr_t ram, ut_asserteq(a, a2); ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 8, 0, 0); - ret = lmb_free(a2, 4); + ret = lmb_free(a2, 4, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0); - ret = lmb_free(b, 4); + ret = lmb_free(b, 4, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, 0, 0, 3, alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, @@ -206,17 +206,17 @@ static int test_multi_alloc(struct unit_test_state *uts, const phys_addr_t ram, ut_asserteq(b, b2); ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, alloc_64k_addr - 8, 0x10000 + 8, ram_end - 8, 4, 0, 0); - ret = lmb_free(b2, 4); + ret = lmb_free(b2, 4, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, 0, 0, 3, alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, ram_end - 8, 4); - ret = lmb_free(c, 4); + ret = lmb_free(c, 4, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, 0, 0, 2, alloc_64k_addr - 8, 4, alloc_64k_addr, 0x10000, 0, 0); - ret = lmb_free(d, 4); + ret = lmb_free(d, 4, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, 0, 0, 1, alloc_64k_addr, 0x10000, 0, 0, 0, 0); @@ -320,7 +320,7 @@ static int test_bigblock(struct unit_test_state *uts, const phys_addr_t ram) ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, big_block_size + 0x10000, 0, 0, 0, 0); - ret = lmb_free(a, big_block_size); + ret = lmb_free(a, big_block_size, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_64k_addr, 0x10000, 0, 0, 0, 0); @@ -392,12 +392,12 @@ static int test_noreserved(struct unit_test_state *uts, const phys_addr_t ram, - alloc_size_aligned, alloc_size, 0, 0); } /* and free them */ - ret = lmb_free(b, alloc_size); + ret = lmb_free(b, alloc_size, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram + ram_size - alloc_size_aligned, alloc_size, 0, 0, 0, 0); - ret = lmb_free(a, alloc_size); + ret = lmb_free(a, alloc_size, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0); @@ -408,7 +408,7 @@ static int test_noreserved(struct unit_test_state *uts, const phys_addr_t ram, ram + ram_size - alloc_size_aligned, alloc_size, 0, 0, 0, 0); /* and free it */ - ret = lmb_free(b, alloc_size); + ret = lmb_free(b, alloc_size, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0); @@ -476,12 +476,12 @@ static int lib_test_lmb_at_0(struct unit_test_state *uts) ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4, 0, 0, 0, 0); /* check that this was an error by freeing b */ - ret = lmb_free(b, 4); + ret = lmb_free(b, 4, LMB_NONE); ut_asserteq(ret, -1); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4, 0, 0, 0, 0); - ret = lmb_free(a, ram_size - 4); + ret = lmb_free(a, ram_size - 4, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 0, 0, 0, 0, 0, 0, 0); @@ -612,7 +612,7 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) ut_asserteq(b, 0); b = lmb_alloc_addr(alloc_addr_a, 0x2000, LMB_NONE); ut_asserteq(b, 0); - ret = lmb_free(alloc_addr_a, 0x2000); + ret = lmb_free(alloc_addr_a, 0x2000, LMB_NONE); ut_asserteq(ret, 0); b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE); ut_asserteq(b, 0); @@ -620,7 +620,7 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) ut_asserteq(b, -EEXIST); b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE); ut_asserteq(b, -EEXIST); - ret = lmb_free(alloc_addr_a, 0x1000); + ret = lmb_free(alloc_addr_a, 0x1000, LMB_NONE); ut_asserteq(ret, 0); /* @@ -642,9 +642,9 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, alloc_addr_a, 0x1000, alloc_addr_a + 0x4000, 0x1000, 0, 0); - ret = lmb_free(alloc_addr_a, 0x1000); + ret = lmb_free(alloc_addr_a, 0x1000, LMB_NONE); ut_asserteq(ret, 0); - ret = lmb_free(alloc_addr_a + 0x4000, 0x1000); + ret = lmb_free(alloc_addr_a + 0x4000, 0x1000, LMB_NOOVERWRITE); ut_asserteq(ret, 0); /* @@ -667,7 +667,7 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, alloc_addr_a, 0x6000, 0, 0, 0, 0); - ret = lmb_free(alloc_addr_a, 0x6000); + ret = lmb_free(alloc_addr_a, 0x6000, LMB_NONE); ut_asserteq(ret, 0); /* @@ -689,9 +689,9 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, alloc_addr_a, 0x1000, alloc_addr_a + 0x4000, 0x1000, 0, 0); - ret = lmb_free(alloc_addr_a, 0x1000); + ret = lmb_free(alloc_addr_a, 0x1000, LMB_NOOVERWRITE); ut_asserteq(ret, 0); - ret = lmb_free(alloc_addr_a + 0x4000, 0x1000); + ret = lmb_free(alloc_addr_a + 0x4000, 0x1000, LMB_NOOVERWRITE); ut_asserteq(ret, 0); /* reserve 3 blocks */ @@ -732,7 +732,8 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) 0, 0, 0, 0); /* free thge allocation from d */ - ret = lmb_free(alloc_addr_c + 0x10000, ram_end - alloc_addr_c - 0x10000); + ret = lmb_free(alloc_addr_c + 0x10000, ram_end - alloc_addr_c - 0x10000, + LMB_NONE); ut_asserteq(ret, 0); /* allocate at 3 points in free range */ @@ -741,7 +742,7 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) ut_asserteq(d, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000, ram_end - 4, 4, 0, 0); - ret = lmb_free(ram_end - 4, 4); + ret = lmb_free(ram_end - 4, 4, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000, 0, 0, 0, 0); @@ -750,7 +751,7 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) ut_asserteq(d, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000, ram_end - 128, 4, 0, 0); - ret = lmb_free(ram_end - 128, 4); + ret = lmb_free(ram_end - 128, 4, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000, 0, 0, 0, 0); @@ -759,13 +760,13 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) ut_asserteq(d, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010004, 0, 0, 0, 0); - ret = lmb_free(alloc_addr_c + 0x10000, 4); + ret = lmb_free(alloc_addr_c + 0x10000, 4, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000, 0, 0, 0, 0); /* allocate at the bottom a was assigned to ram at the top */ - ret = lmb_free(ram, alloc_addr_a - ram); + ret = lmb_free(ram, alloc_addr_a - ram, LMB_NONE); ut_asserteq(ret, 0); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram + 0x8000000, 0x10010000, 0, 0, 0, 0); From patchwork Tue Jun 17 10:43:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 897368 Delivered-To: patch@linaro.org Received: by 2002:adf:9b99:0:b0:3a4:ee3f:8f15 with SMTP id d25csp2073993wrc; Tue, 17 Jun 2025 03:45:03 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWQBPd1rRly00Ab+oVDawwMVLObm4CDau5lFeSGGQwQFOhHahHnRUmAAyNHSjXhBQlC76YI1w==@linaro.org X-Google-Smtp-Source: AGHT+IHfUfNy4zynujOyNr6B5Hi0To6BbpUvAZOHKTcflMVVcgtPSndytCTuvFvUe2ar+m8dIzHE X-Received: by 2002:a05:6214:440b:b0:6fa:c45e:34cb with SMTP id 6a1803df08f44-6fb477260a4mr193970686d6.5.1750157102923; Tue, 17 Jun 2025 03:45:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1750157102; cv=none; d=google.com; s=arc-20240605; b=S/DODd9yzVBhq8HIJnqW/6so5KW6dbAzHt5eu2UG+BG5RvjCDNE+KpxUojRuSMhTvH zawxYa0Cz1ifo2CUywqquUsGORa2NEMXWMVI3m8vLjGWttyA3vE7J1eGCLOrqnRsp7uc qbrBNFVNN20CNbeQ3A7dPuF8A6LtaFyqjO7Kt70zEd20/fuGG08oe81mU6p4DiD/w7Vf V6gjndq+w/wgqp7kpXUZ/43CdpapnqdFaHFu1BcohPKaoA1uRVrJA8G4b7XgWEIFD/ix j0+d0iB6aLr1nvYvNisgnwr7pVK1R3iHysz9QchagBAxFf2h30lPa2HcPxXDrnutTAbO 1gpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=5qH/qyRiXnlqmwTZ4e3A2m+pC4LRP6jMTOgtoNTor/U=; fh=OQsgrM7lYcbKid/8QRKDrRnr3aZuZ+FGeYy6ua/5St0=; b=B290cNw5Pru67aipA7xUHw2KpqVDKgouqYtMI+3Vlbppe4qog48t90ZL46ZdjmSDFz lVQ9V2+OBH1fA5wDgbjQOJZtztoLYPbhAu/wyJImtVScaaBgDZJuPWjz/cy/aTHZBmaN 7tKqysBkOiVuH1fS4TJZbwVJvVlD9/fZr6pRsI3ZvspXOTN/uakLFvgyJISVHjVBrFg6 ZtwOd2hSwNK3fmUTK/PmP7Znswneo67sVMVz38WFgVaw4QcziHk5KWFxrBiAlhF0xh9L EM/9lkey33hWBcopofg3Lv0SGaWBKXprglvvccDpfGbQK79xiKJBQQVdyh7UfyAI0sH5 /CSQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id 6a1803df08f44-6fb35bddb3fsi126932826d6.236.2025.06.17.03.45.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 03:45:02 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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 5983E82BCD; Tue, 17 Jun 2025 12:44:27 +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 409AB82C79; Tue, 17 Jun 2025 12:44:26 +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_DNSWL_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE,SPF_SOFTFAIL 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 04DD882BCD for ; Tue, 17 Jun 2025 12:44:24 +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 614D91596; Tue, 17 Jun 2025 03:44:02 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.164.21.38]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 373B93F58B; Tue, 17 Jun 2025 03:44:19 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Tom Rini , Casey Connolly , Neil Armstrong , Mark Kettenis , Weijie Gao , Heinrich Schuchardt , Simon Glass , Sughosh Ganu Subject: [PATCH v5 5/7] lmb: use a single function to check for allocation and reservation requests Date: Tue, 17 Jun 2025 16:13:44 +0530 Message-Id: <20250617104346.1379981-6-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250617104346.1379981-1-sughosh.ganu@linaro.org> References: <20250617104346.1379981-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 The functions that handle allocation requests check if a region of memory overlaps with a used region. This is done through lmb_overlaps_region(). Similar checks are done for reservation requests made to the LMB module, where the caller asks specifically for a particular region of memory. These checks are being done through lmb_can_reserve_region(). There are subtle differences in the checking needed for allocation requests, as against reservation requests. In the former, it is only needed to be checked if a region is overlapping with an existing used region, and return as soon as an overlap is found. For reservation request checks, because U-Boot allows for re-use of in-use regions with a particular memory attribute, this check has to iterate through all the regions that might overlap with the requested region, and then check that the necessary conditions are met to allow for the overlap. Combine these two checks in a single function, lmb_overlap_checks() as both lmb_overlaps_region() and lmb_can_reserve_region() are pretty similar otherwise. Signed-off-by: Sughosh Ganu Reviewed-by: Ilias Apalodimas --- Changes since V4: None lib/lmb.c | 84 +++++++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/lib/lmb.c b/lib/lmb.c index 4af8dae4359..45b26512a5b 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -317,8 +317,34 @@ static long _lmb_free(struct alist *lmb_rgn_lst, phys_addr_t base, rgn[i].flags); } -static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base, - phys_size_t size) +/** + * lmb_overlap_checks() - perform checks to see if region can be allocated or reserved + * @lmb_rgn_lst: list of LMB regions + * @base: base address of region to be checked + * @size: size of region to be checked + * @flags: flag of the region to be checked (only for reservation requests) + * @alloc: if checks are to be done for allocation or reservation request + * + * Check if the region passed to the function overlaps with any one of + * the regions of the passed lmb region list. + * + * If the @alloc flag is set to true, this check stops as soon an + * overlapping region is found. The function can also be called to + * check if a reservation request can be satisfied, by setting + * @alloc to false. In that case, the function then iterates through + * all the regions in the list passed to ensure that the requested + * region does not overlap with any existing regions. An overlap is + * allowed only when the flag of the requested region and the existing + * region is LMB_NONE. + * + * Return: index of the overlapping region, -1 if no overlap is found + * + * When the function is called for a reservation request check, -1 will + * also be returned when there is an allowed overlap, i.e. requested + * region and existing regions have flags as LMB_NONE. + */ +static long lmb_overlap_checks(struct alist *lmb_rgn_lst, phys_addr_t base, + phys_size_t size, u32 flags, bool alloc) { unsigned long i; struct lmb_region *rgn = lmb_rgn_lst->data; @@ -326,9 +352,12 @@ static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base, for (i = 0; i < lmb_rgn_lst->count; i++) { phys_addr_t rgnbase = rgn[i].base; phys_size_t rgnsize = rgn[i].size; + u32 rgnflags = rgn[i].flags; - if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) - break; + if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { + if (alloc || flags != LMB_NONE || flags != rgnflags) + break; + } } return (i < lmb_rgn_lst->count) ? i : -1; @@ -390,7 +419,8 @@ phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align) base = ALIGN_DOWN(lmbbase + lmbsize - size, align); while (base && lmbbase <= base) { - rgn = lmb_overlaps_region(&io_lmb->used_mem, base, size); + rgn = lmb_overlap_checks(&io_lmb->used_mem, base, size, + LMB_NOOVERWRITE, true); if (rgn < 0) { /* This area isn't reserved, take it */ if (lmb_add_region_flags(&io_lmb->used_mem, base, @@ -488,45 +518,12 @@ void lmb_dump_all(void) #endif } -/** - * lmb_can_reserve_region() - check if the region can be reserved - * @base: base address of region to be reserved - * @size: size of region to be reserved - * @flags: flag of the region to be reserved - * - * Go through all the reserved regions and ensure that the requested - * region does not overlap with any existing regions. An overlap is - * allowed only when the flag of the request region and the existing - * region is LMB_NONE. - * - * Return: true if region can be reserved, false otherwise - */ -static bool lmb_can_reserve_region(phys_addr_t base, phys_size_t size, - u32 flags) -{ - uint i; - struct lmb_region *lmb_reserved = lmb.used_mem.data; - - for (i = 0; i < lmb.used_mem.count; i++) { - u32 rgnflags = lmb_reserved[i].flags; - phys_addr_t rgnbase = lmb_reserved[i].base; - phys_size_t rgnsize = lmb_reserved[i].size; - - if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { - if (flags != LMB_NONE || flags != rgnflags) - return false; - } - } - - return true; -} - static long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags) { long ret = 0; struct alist *lmb_rgn_lst = &lmb.used_mem; - if (!lmb_can_reserve_region(base, size, flags)) + if (lmb_overlap_checks(lmb_rgn_lst, base, size, flags, false) != -1) return -EEXIST; ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags); @@ -698,7 +695,8 @@ static int _lmb_alloc_base(phys_size_t size, ulong align, } while (base && lmbbase <= base) { - rgn = lmb_overlaps_region(&lmb.used_mem, base, size); + rgn = lmb_overlap_checks(&lmb.used_mem, base, size, + LMB_NOOVERWRITE, true); if (rgn < 0) { /* This area isn't reserved, take it */ if (lmb_add_region_flags(&lmb.used_mem, base, @@ -733,7 +731,8 @@ static int _lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags) struct lmb_region *lmb_memory = lmb.available_mem.data; /* Check if the requested address is in one of the memory regions */ - rgn = lmb_overlaps_region(&lmb.available_mem, base, size); + rgn = lmb_overlap_checks(&lmb.available_mem, base, size, + LMB_NOOVERWRITE, true); if (rgn >= 0) { /* * Check if the requested end address is in the same memory @@ -786,7 +785,8 @@ phys_size_t lmb_get_free_size(phys_addr_t addr) struct lmb_region *lmb_memory = lmb.available_mem.data; /* check if the requested address is in the memory regions */ - rgn = lmb_overlaps_region(&lmb.available_mem, addr, 1); + rgn = lmb_overlap_checks(&lmb.available_mem, addr, 1, LMB_NOOVERWRITE, + true); if (rgn >= 0) { for (i = 0; i < lmb.used_mem.count; i++) { if (addr < lmb_used[i].base) { From patchwork Tue Jun 17 10:43:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 897369 Delivered-To: patch@linaro.org Received: by 2002:adf:9b99:0:b0:3a4:ee3f:8f15 with SMTP id d25csp2074056wrc; Tue, 17 Jun 2025 03:45:14 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCV7s5T0sRKZ21ounbIDoyZfondZsBgGL5fD0WYZqbkssTI9MpozA63bYMYEvRJHHUCT/wk2OA==@linaro.org X-Google-Smtp-Source: AGHT+IFiQpSGZd+nfBHuBs4W6nZm8NZmtzmO/Awk0TltObfs2/dS22xXqWAi5GGy1K2NNAyS80/5 X-Received: by 2002:ac8:5786:0:b0:4a4:2e99:3a92 with SMTP id d75a77b69052e-4a73c5b09eamr210743511cf.38.1750157113833; Tue, 17 Jun 2025 03:45:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1750157113; cv=none; d=google.com; s=arc-20240605; b=FK9xhDJspCZU4PqlDOCxBqxf05SNT48wdLjDm9G2Psj2MpO/yMAQiQPx70llobO2Id OYOrSLGhtUhVFbhZzzxHwDGVU0/IVMZgLCBUB7ERUPQew8PVSdbRt87mdw6miDTL74n+ +KsftSx/H1vlDelUNfhpVedH39MPJw873ag96Wt7VOXVoXXrm4VIKTQCXA/j4i+NyAcJ jXBCghl/0dPfs+wJtkY2PAulkiBhT+wEW63SOR0TijP8DEVl0I7Vq6uPA3JQ+T18HQj0 dNaYBHSvFtLheOA55MmnGQj1bBXKyR7wEEjlgHVBeRTf2pRIkrEqIluOc6vGZ+PRhaS0 vIJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=fNwfvoBmo5/kNqbuNZY4MvxXhgwNLIRQtyXLEM4IJms=; fh=OQsgrM7lYcbKid/8QRKDrRnr3aZuZ+FGeYy6ua/5St0=; b=QhPt4+4F7Wn9yK/ltOCsqt/PqaZ7SFYqYi3csdXfHm9NM0uBLl6DqxFP0gNERfXY3d Dl7lGKxOzw1+AaTRfE4evmy+2ZcEZaj6lr68extf8Bh9sN+jaNyaSPZgp0vVHwqM2s3B Y65uaJu2SqZPJdHGJrsmtgyaG/zhILMRyp3bfZCUAYSR1CUwPXwJtWvQJrBBCHZuk3aL 8dESuKggvUEX97CgU3v8ld2z7o9B2A2KOF9e5LVMAsX5CI4XOt1h8fcecAWFUwsH6UfQ oLEkpEs5t/PYw9MQb8n7vjT9iiCiWDt2S9Hhr2renSTeWKd5E2y+zjMN0ls8yOa7g67r m2Og==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id d75a77b69052e-4a72a4e4f0bsi124410051cf.450.2025.06.17.03.45.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 03:45:13 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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 9EBB382C79; Tue, 17 Jun 2025 12:44:31 +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 D997C82BA1; Tue, 17 Jun 2025 12:44:29 +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_DNSWL_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE,SPF_SOFTFAIL 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 D7AB682E08 for ; Tue, 17 Jun 2025 12:44:27 +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 378C91596; Tue, 17 Jun 2025 03:44:06 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.164.21.38]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0D30B3F58B; Tue, 17 Jun 2025 03:44:23 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Tom Rini , Casey Connolly , Neil Armstrong , Mark Kettenis , Weijie Gao , Heinrich Schuchardt , Simon Glass , Sughosh Ganu Subject: [PATCH v5 6/7] mach-snapdragon: add a check before copying FDT to fdt_addr_r Date: Tue, 17 Jun 2025 16:13:45 +0530 Message-Id: <20250617104346.1379981-7-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250617104346.1379981-1-sughosh.ganu@linaro.org> References: <20250617104346.1379981-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 The board_late_init() function allocates memory for a bunch of environment variables, including fdt_addr_r. The device-tree then gets copied to the memory pointed to by fdt_addr_r. However, the memory allocation request can fail, in which case the address that is being written to would not be allocated. Add a check that the memory allocation has succeeded before copying the device-tree. Signed-off-by: Sughosh Ganu --- Changes since V4: None arch/arm/mach-snapdragon/board.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/arm/mach-snapdragon/board.c b/arch/arm/mach-snapdragon/board.c index 87a173e0acb..ed2bd5dbade 100644 --- a/arch/arm/mach-snapdragon/board.c +++ b/arch/arm/mach-snapdragon/board.c @@ -497,7 +497,7 @@ void __weak qcom_late_init(void) /* Stolen from arch/arm/mach-apple/board.c */ int board_late_init(void) { - u32 status = 0; + u32 status = 0, fdt_status = 0; phys_addr_t addr; struct fdt_header *fdt_blob = (struct fdt_header *)gd->fdt_blob; @@ -520,14 +520,19 @@ int board_late_init(void) status |= !lmb_alloc(FASTBOOT_BUF_SIZE, &addr) ? env_set_hex("fastboot_addr_r", addr) : 1; - status |= !lmb_alloc(SZ_2M, &addr) ? + fdt_status |= !lmb_alloc(SZ_2M, &addr) ? env_set_hex("fdt_addr_r", addr) : 1; - if (status) + if (status || fdt_status) log_warning("%s: Failed to set run time variables\n", __func__); /* By default copy U-Boots FDT, it will be used as a fallback */ - memcpy((void *)addr, (void *)gd->fdt_blob, fdt32_to_cpu(fdt_blob->totalsize)); + if (fdt_status) + log_warning("%s: Failed to reserve memory for copying FDT\n", + __func__); + else + memcpy((void *)addr, (void *)gd->fdt_blob, + fdt32_to_cpu(fdt_blob->totalsize)); configure_env(); qcom_late_init(); From patchwork Tue Jun 17 10:43:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 897370 Delivered-To: patch@linaro.org Received: by 2002:adf:9b99:0:b0:3a4:ee3f:8f15 with SMTP id d25csp2074147wrc; Tue, 17 Jun 2025 03:45:25 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWY0Yslx5QAqPq2wwY7wAxfJnhfkI2UryKvA9frXIbyCMIeoaOdyOzQkBtJKyp7JWr7flw3fw==@linaro.org X-Google-Smtp-Source: AGHT+IEnviFDY2jwk3mjNtykiaJVCDij2MQTU/jAInx6oY+gUFIfof5AKyjMSTxtErbjwzq9e5ma X-Received: by 2002:a05:620a:bd5:b0:7d0:ab76:8e2d with SMTP id af79cd13be357-7d3c6d02ce7mr2028168385a.51.1750157125242; Tue, 17 Jun 2025 03:45:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1750157125; cv=none; d=google.com; s=arc-20240605; b=ZuMRr1tbpBjXY3eAfmcbbIfdq53msPw5JsN5EXfydZv8jFCJ7d+aK32ZsErifvabvY hxyoR3ma5krFky9Ykm2Q7Qisv6G0qTN/0oIr710YN9YORIfWiK37DWNRkueaifBWNgZs eoukpFwdNiw7f+xUlwHAleHdeRKuPRWIht9Yd1nfT8aWJuFDYvw0hk5NY2p85x5o8PaJ 3zbRVCUz/ZYL/Z9jV1wYfxAv6509H3OwGzrS72Q5l86lW87PdZ8GiwfiE3VK8U/Z6wpa Lw6sOA6iVRpPrkh51WioY35nLjOu4KjDmaAzr4TeEoIktGz7AmSyoh2uDogjSC4Tse+F e64A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=SRzckkuRcA2wwCsRPLmHaFBrnyg1cx5PS1CLcCONkXA=; fh=OQsgrM7lYcbKid/8QRKDrRnr3aZuZ+FGeYy6ua/5St0=; b=RL3qNQVkX8CDvARUH78qrqKq33Ry0RrJTR8XE7N5D8GOB2Czvhy/NlmkvaL6WB+eQ6 jGfQolmkVDE9aD/VVpxXs4sf+aSOnhuOmW9l1uS6aoCGtub8Zo3BVWfgbraJdMNF9zEe NlwkoWVaRTDrPRzt+pSyNd+CeFkaAxr/5QOHZm6kEoC3pJKKGYUaZL1+FDGezJI3IiSL V6Lf+M8s1O3/eN3pIDpjcrCorizAdt5xRUDyLpOUgCbJ0dtWr9fYX3WuVAUgTe6Gnb08 OIWKVePytAJEmTWfmhAOaF+KT2wf9YNYUSJCGOBx90kPw9zqq6lrc+MfEonwY3qd8Dhk m3tw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id af79cd13be357-7d3b8dc7745si1229369685a.21.2025.06.17.03.45.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 03:45:25 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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 E5B0B82D57; Tue, 17 Jun 2025 12:44:34 +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 3A3FC82BA1; Tue, 17 Jun 2025 12:44:34 +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_DNSWL_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE,SPF_SOFTFAIL 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 A504B82D57 for ; Tue, 17 Jun 2025 12:44:31 +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 0EBF91596; Tue, 17 Jun 2025 03:44:10 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.164.21.38]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D83DF3F58B; Tue, 17 Jun 2025 03:44:27 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Tom Rini , Casey Connolly , Neil Armstrong , Mark Kettenis , Weijie Gao , Heinrich Schuchardt , Simon Glass , Sughosh Ganu Subject: [PATCH v5 7/7] doc: add lmb documentation Date: Tue, 17 Jun 2025 16:13:46 +0530 Message-Id: <20250617104346.1379981-8-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250617104346.1379981-1-sughosh.ganu@linaro.org> References: <20250617104346.1379981-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 The LMB module has undergone significant changes in the recent past. Add a document which briefly describes what the LMB module does, and the changes that have been made to it's design since the 2025.01 release. Signed-off-by: Sughosh Ganu Acked-by: Ilias Apalodimas --- Changes since V4: None doc/api/index.rst | 1 - doc/api/lmb.rst | 7 -- doc/develop/index.rst | 1 + doc/develop/lmb.rst | 166 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+), 8 deletions(-) delete mode 100644 doc/api/lmb.rst create mode 100644 doc/develop/lmb.rst diff --git a/doc/api/index.rst b/doc/api/index.rst index 506843ed74a..cf9d21e4c1c 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -17,7 +17,6 @@ U-Boot API documentation interrupt led linker_lists - lmb logging nvmem part diff --git a/doc/api/lmb.rst b/doc/api/lmb.rst deleted file mode 100644 index 2095bfa1618..00000000000 --- a/doc/api/lmb.rst +++ /dev/null @@ -1,7 +0,0 @@ -.. SPDX-License-Identifier: GPL-2.0+ - -Logical memory blocks -===================== - -.. kernel-doc:: include/lmb.h - :internal: diff --git a/doc/develop/index.rst b/doc/develop/index.rst index 0c83ef109ab..3c044e67927 100644 --- a/doc/develop/index.rst +++ b/doc/develop/index.rst @@ -46,6 +46,7 @@ Implementation cedit event global_data + lmb logging makefiles menus diff --git a/doc/develop/lmb.rst b/doc/develop/lmb.rst new file mode 100644 index 00000000000..b9d0f09c2bb --- /dev/null +++ b/doc/develop/lmb.rst @@ -0,0 +1,166 @@ +.. SPDX-License-Identifier: GPL-2.0+ + +Logical Memory Blocks (LMB) +=========================== + +U-Boot has support for reserving chunks of memory which is primarily +used for loading images to the DRAM memory, before these are booted, +or written to non-volatile storage medium. This functionality is +provided through the Logical Memory Blocks (LMB) module. + +Introduction +------------ + +The LMB module manages allocation requests for memory region not +occupied by the U-Boot image. Allocation requests that are made +through malloc() and similar functions result in memory getting +allocated from the heap region, which is part of the U-Boot +image. Typically, the heap memory is a few MiB in size. Loading an +image like the linux kernel might require lot more memory than what +the heap can provide. Such allocations are usually handled through the +LMB module. + +The U-Boot image typically gets relocated to the top of the usable +DRAM memory region. A typical memory layout looks as follows:: + + + + + + | | + | | + | | + | | + | | + --- +--------------+ <--- U-Boot ram top + | | | + | | Text | + | +--------------+ + | | | + | | Data | + | +--------------+ + | | | + | | BSS | + U-Boot Image +--------------+ + | | | + | | Heap | + | | | + | +--------------+ + | | | + | | | + | | Stack | + | | | + | | | + --- +--------------+ + | | + | | + | | + | | + | | + | | + | | + | | + | | + | | + | | + | | + | | + | | + +--------------+ <--- ram start + + + +The region of memory below the U-Boot image is the one controlled by +the LMB module. + + +Types of LMB Allocations +------------------------ + +There are two classes of allocation requests that get made to the LMB +module. One type of allocation requests are requesting memory of a +particular number of bytes. This type of allocation is similar to that +done using the malloc type of function calls. The other type of +allocations, are requests made for a specific memory address. The +second type of allocations are usually made for loading images to a +particular memory address. + + +LMB design Pre 2025.01 +---------------------- + +The earlier versions of U-Boot (pre 2025.01 release) +had a local memory map based LMB implementation whereby it was +possible to declare the LMB map inside a function or a C file. This +design resulted in temporary, non-global LMB maps, which also allowed +for re-use of memory. This meant that it was possible to use a region +of memory to load some image, and subsequently the same region of +memory could be used for loading a different image. A typical example +of this usage would be loading an image to a memory address, followed +by writing that image to some non-volatile storage medium. Once this +is done, the same address can be used for loading a different image +and then writing it to it's non-volatile storage +destination. Typically, environment variables like `loadaddr`, +`kernel_addr_r`, `ramdisk_addr_r` are used for loading images to +memory regions. + + +Current LMB implementation +-------------------------- + +Changes were made in the 2025.01 release to make the LMB memory map +global and persistent. With this, the LMB memory map is the same +across all of U-Boot, and also persists as long as U-Boot is +active. Even with this change, there has been consistency as far as +re-use of memory is concerned to maintain backward compatibility. It +is allowed for re-requesting the same region of memory if the memory +region has a particular attribute (LMB_NONE). + +As part of the platform boot, DRAM memory available for use in U-Boot +gets added to the LMB memory map. Any allocation requests made +subsequently will be made from this memory added as part of the board +init. + + +Allocation API +-------------- + +Any request for non-heap memory can be made through the LMB allocation +API. + +.. code-block:: c + + int lmb_alloc_mem(enum lmb_mem_type type, u64 align, + phys_addr_t *addr, phys_size_t size, + u32 flags); + +Correspondingly, the allocated memory can be free'd + +.. code-block:: c + + long lmb_free(phys_addr_t base, phys_size_t size, u32 flags); + +For a detailed API description, please refer to the header file. + + +UEFI allocations with LMB as the backend +---------------------------------------- + +The UEFI specification describes boot-time API's for allocation of +memory. These API's use the same memory that is being used by the LMB +module. Pre 2025.01 release, there wasn't any synchronisation between +the EFI sub-system and the LMB module about the memory that was +getting allocated by each of these modules. This was the primary +reason for making the LMB memory map global and persistent. With this +change, the EFI memory allocation API's have also been changed to use +the LMB module as the backend for the allocation requests. Any other +sub-system which might wish to use the same memory region for it's use +can then use the LMB as the backend for the memory allocations and +it's associated book-keeping. + + +API documentation +----------------- + +.. kernel-doc:: include/lmb.h +