From patchwork Wed Jul 24 06:01:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 814135 Delivered-To: patch@linaro.org Received: by 2002:adf:f288:0:b0:367:895a:4699 with SMTP id k8csp2618213wro; Tue, 23 Jul 2024 23:05:55 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWlux5eMP3Dc22U8kKS/H7+awXyCnKwddoTZTh3OvaAlXU7+no6RLi59qNhsBdMjdURgGQehjNtkdqLN4ibSYBP X-Google-Smtp-Source: AGHT+IHug/ZNArU6S75mI5ETC1m92DQl8/VNtLJyRe0/heCxHx5Dn/1W17DK9VxFzXf0X8VWCp8f X-Received: by 2002:a2e:8e8b:0:b0:2ec:5b17:29a2 with SMTP id 38308e7fff4ca-2f03253e744mr5851411fa.32.1721801155691; Tue, 23 Jul 2024 23:05:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1721801155; cv=none; d=google.com; s=arc-20160816; b=KDMs6JQCOz7wNDArn27ITf6SBMVTQxkDQlq6OOXTufZ4ENNsnarUCyrYoR0PBP5n/5 x9VHIQFRsymO386K/esmMLeqy5RE6erxtwD1roBDSXlRHV01dRFVkux+zJmoi7lJDG3w q/at57Lm2fKMvCnyslDo3e/vWo4LqTX5ipzLvNO/56r+fz+hXkhzQwVIZ26wRxrlvo5O 7GM1Z2GvoXx4RCOtvSk8XL0rc3l6WYoMJNnPWGGZhiz1U6sj88hB5LD8d5X7lHYGcBkN YvH9ziyVA7YMjTXo0VJ1uKm/u7WFodUkYhWE48b+46R4cNTdeLLoil7hTdWiIv7DtLPI vuIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=UrGRNH69b7UeEd1/JIXaKsY27ooO2PSoO4g6khBUVQU=; fh=eimiGswTlodK8yi2DcDfSicmKwvq5bxKQKpD+SkkTNY=; b=O8lTbTcEyoCmBRSI8geaGuuOggOhF/WnWWyZe00WhympwoaEH4j5mSfE/s3HFDwutf 4SRxXQkroe1L6iBtiGUl+df1mn0Tyd7gO6gv+JIBd+iLZRDGANRrgk2E7g71Up+Og7fX Udf9yaU/bMdRhQ0uLlJwp2pDECfWT59Dci8sB2vZTa+w+UFoaLdsem/W6dp3Ie92sW8m qagXrvNf3yTMlFg2JsNmzv3BweceEbWz8sR2YU1aqLMpWFHxIqe6RlOQ4EnZ3aoHLQuE VyaLH7WJ+aNwiv5UjsTp/UESqDxXqUSegSwY+ZM9XHepYt66DPYbwpp+joWBVjRiykXu tp3g==; 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 5b1f17b1804b1-427f937f319si3569145e9.78.2024.07.23.23.05.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 23:05:55 -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 5FB03888D1; Wed, 24 Jul 2024 08:03:53 +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 CC88D888E0; Wed, 24 Jul 2024 08:03:51 +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.9 required=5.0 tests=BAYES_00, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, SPF_HELO_NONE,T_SPF_TEMPERROR autolearn=ham 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 B9B25888C7 for ; Wed, 24 Jul 2024 08:03:45 +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 A4BF21476; Tue, 23 Jul 2024 23:04:10 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.162.17.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 8B2AA3F766; Tue, 23 Jul 2024 23:03:40 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Heinrich Schuchardt , Simon Glass , Marek Vasut , Mark Kettenis , Michal Simek , Patrick DELAUNAY , Patrice CHOTARD , Huan Wang , Angelo Dureghello , Daniel Schwierzeck , Thomas Chou , Rick Chen , Max Filippov , Sughosh Ganu Subject: [PATCH 12/40] lmb: allow for resizing lmb regions Date: Wed, 24 Jul 2024 11:31:56 +0530 Message-Id: <20240724060224.3071065-13-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240724060224.3071065-1-sughosh.ganu@linaro.org> References: <20240724060224.3071065-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 Allow for resizing of LMB regions if the region attributes match. The current code returns a failure status on detecting an overlapping address. This worked up until now since the LMB calls were not persistent and global -- the LMB memory map was specific and private to a given caller of the LMB API's. With the change in the LMB code to make the LMB reservations persistent, there needs to be a check on whether the memory region can be resized, and then do it if so. To distinguish between memory that cannot be resized, add a new flag, LMB_NOOVERWRITE. Reserving a region of memory with this attribute would indicate that the region cannot be resized. Signed-off-by: Sughosh Ganu --- Changes since rfc: * Add a function comment for lmb_add_region_flags(). * Change the wording of a comment in lmb_merge_overlap_regions() as per review comment from Simon Glass. include/lmb.h | 1 + lib/lmb.c | 144 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 128 insertions(+), 17 deletions(-) diff --git a/include/lmb.h b/include/lmb.h index a1cc45b726..a308796d58 100644 --- a/include/lmb.h +++ b/include/lmb.h @@ -21,6 +21,7 @@ enum lmb_flags { LMB_NONE = BIT(0), LMB_NOMAP = BIT(1), + LMB_NOOVERWRITE = BIT(2), }; /** diff --git a/lib/lmb.c b/lib/lmb.c index dd6f22654c..88352e9a25 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -247,12 +247,106 @@ void lmb_init_and_reserve_range(phys_addr_t base, phys_size_t size, lmb_reserve_common(fdt_blob); } -/* This routine called with relocation disabled. */ +static bool lmb_region_flags_match(struct lmb_region *rgn, unsigned long r1, + enum lmb_flags flags) +{ + return rgn[r1].flags == flags; +} + +static long lmb_merge_overlap_regions(struct alist *lmb_rgn_lst, + unsigned long i, phys_addr_t base, + phys_size_t size, enum lmb_flags flags) +{ + phys_size_t rgnsize; + unsigned long rgn_cnt, idx; + phys_addr_t rgnbase, rgnend; + phys_addr_t mergebase, mergeend; + struct lmb_region *rgn = lmb_rgn_lst->data; + + rgn_cnt = 0; + idx = i; + + /* + * First thing to do is to identify how many regions + * the requested region overlaps. + * If the flags match, combine all these overlapping + * regions into a single region, and remove the merged + * regions. + */ + while (idx < lmb_rgn_lst->count - 1) { + rgnbase = rgn[idx].base; + rgnsize = rgn[idx].size; + + if (lmb_addrs_overlap(base, size, rgnbase, + rgnsize)) { + if (!lmb_region_flags_match(rgn, idx, flags)) + return -1; + rgn_cnt++; + idx++; + } + } + + /* The merged region's base and size */ + rgnbase = rgn[i].base; + mergebase = min(base, rgnbase); + rgnend = rgn[idx].base + rgn[idx].size; + mergeend = max(rgnend, (base + size)); + + rgn[i].base = mergebase; + rgn[i].size = mergeend - mergebase; + + /* Now remove the merged regions */ + while (--rgn_cnt) + lmb_remove_region(lmb_rgn_lst, i + 1); + + return 0; +} + +static long lmb_resize_regions(struct alist *lmb_rgn_lst, unsigned long i, + phys_addr_t base, phys_size_t size, + enum lmb_flags flags) +{ + long ret = 0; + phys_addr_t rgnend; + struct lmb_region *rgn = lmb_rgn_lst->data; + + if (i == lmb_rgn_lst->count - 1 || + base + size < rgn[i + 1].base) { + if (!lmb_region_flags_match(rgn, i, flags)) + return -1; + + rgnend = rgn[i].base + rgn[i].size; + rgn[i].base = min(base, rgn[i].base); + rgnend = max(base + size, rgnend); + rgn[i].size = rgnend - rgn[i].base; + } else { + ret = lmb_merge_overlap_regions(lmb_rgn_lst, i, base, size, + flags); + } + + return ret; +} + +/** + * lmb_add_region_flags() - Add an lmb region to the given list + * @lmb_rgn_lst: LMB list to which region is to be added(free/used) + * @base: Start address of the region + * @size: Size of the region to be added + * @flags: Attributes of the LMB region + * + * Add a region of memory to the list. If the region does not exist, add + * it to the list. Depending on the attributes of the region to be added, + * the function might resize an already existing region or coalesce two + * adjacent regions. + * + * + * Returns: 0 if the region addition successful, -1 on failure + */ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, phys_size_t size, enum lmb_flags flags) { unsigned long coalesced = 0; - long adjacent, i; + long ret, i; struct lmb_region *rgn = lmb_rgn_lst->data; if (alist_err(lmb_rgn_lst)) @@ -281,23 +375,32 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, return -1; /* regions with new flags */ } - adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize); - if (adjacent > 0) { + ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize); + if (ret > 0) { if (flags != rgnflags) break; rgn[i].base -= size; rgn[i].size += size; coalesced++; break; - } else if (adjacent < 0) { + } else if (ret < 0) { if (flags != rgnflags) break; rgn[i].size += size; coalesced++; break; } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { - /* regions overlap */ - return -1; + if (flags == LMB_NONE) { + ret = lmb_resize_regions(lmb_rgn_lst, i, base, + size, flags); + if (ret < 0) + return -1; + + coalesced++; + break; + } else { + return -1; + } } } @@ -447,7 +550,7 @@ static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size) } static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align, - phys_addr_t max_addr) + phys_addr_t max_addr, enum lmb_flags flags) { long i, rgn; phys_addr_t base = 0; @@ -476,8 +579,8 @@ static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align, rgn = lmb_overlaps_region(&lmb_used_mem, base, size); if (rgn < 0) { /* This area isn't reserved, take it */ - if (lmb_add_region(&lmb_used_mem, base, - size) < 0) + if (lmb_add_region_flags(&lmb_used_mem, base, + size, flags) < 0) return 0; return base; } @@ -500,7 +603,7 @@ phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr) { phys_addr_t alloc; - alloc = __lmb_alloc_base(size, align, max_addr); + alloc = __lmb_alloc_base(size, align, max_addr, LMB_NONE); if (alloc == 0) printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", @@ -509,11 +612,8 @@ phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr) return alloc; } -/* - * Try to allocate a specific address range: must be in defined memory but not - * reserved - */ -phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size) +static phys_addr_t __lmb_alloc_addr(phys_addr_t base, phys_size_t size, + enum lmb_flags flags) { long rgn; struct lmb_region *lmb_memory = lmb_free_mem.data; @@ -529,13 +629,23 @@ phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size) lmb_memory[rgn].size, base + size - 1, 1)) { /* ok, reserve the memory */ - if (lmb_reserve(base, size) >= 0) + if (lmb_reserve_flags(base, size, flags) >= 0) return base; } } + return 0; } +/* + * Try to allocate a specific address range: must be in defined memory but not + * reserved + */ +phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size) +{ + return __lmb_alloc_addr(base, size, LMB_NONE); +} + /* Return number of bytes from a given address that are free */ phys_size_t lmb_get_free_size(phys_addr_t addr) {