From patchwork Fri Oct 25 17:27:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 838366 Delivered-To: patch@linaro.org Received: by 2002:adf:e287:0:b0:37d:45d0:187 with SMTP id v7csp414888wri; Fri, 25 Oct 2024 10:27:49 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCU8coblRNSvhe7Mw57t4W0h6FFBk34Q7Xt39ZgzWS9WjnBZI2c/sjMzYs33JrkN4fHdo/vadQ==@linaro.org X-Google-Smtp-Source: AGHT+IGC5yTvYUnJsLZ9kn4d6S5uExodNJ8VZnPg4uoGyTPYa9ztANlL6MTySg/8P2rx5jMm9OjU X-Received: by 2002:a2e:be04:0:b0:2fa:d604:e522 with SMTP id 38308e7fff4ca-2fcbddf96ebmr1204971fa.0.1729877269032; Fri, 25 Oct 2024 10:27:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1729877269; cv=none; d=google.com; s=arc-20240605; b=LEPuTf0M8a40N5eHcaiKhbqNUp2614Pj5f6nkClFcysvspRtNUX5l0BJTkZHeST43r ZoadXf1VcP17sm098E/8+3kRalJ/VZRUQOozjIoPENM7trdSda2e0OG4WcTx3NfwdK8R M+3PydDu/9hkh54xdjSWlQ2imCjmpXmhR1wbTJrWRPO4OUors4Hol6Qol0O0y2lLfEV7 bdMYHo3kdYwmRbC5TNeJguhmqeWM5PU8Gj+2hO88gnfpLaQAvi5d416PIwJoCI296z15 o9jkz29XvTE7HU2LQLoIt1DYLb4d+7UhMPXwPyrxosYL35Jdi93S9miRCBkaHCvRV4ay knNQ== 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=rBYp9qyXxIcC2F+vXCy/tItHtUgNmOF944FUqRvnIT8=; fh=4qhjWgg9bHeH/GNOfE5X3jcKCWXNzATraddlwAAhC5s=; b=bGfcTirzRTvWO5DpYWy7wct7aH4V/vLHK538DaRvMh0M6im+iH5OF6FOPM4OxEZf3h qSXpdp2LNPb5/dAzjy29Lq3RGosihZ2lhvK+sENGApfBxF1evuugQXg+ejSkw5D711Ag bPygbyLqUw9+Hi8juEg/efigs6sIL/fl+NrmfGCDyEhez90pWvbKVuvgCj+a6iw5CA6a 1lZUMkY0NzKo3wU6cEiOxkbi2g/DWjZgHbNoiSKfVjeXQ3S3lXjSvSQlWmrwRzZk/7mo VGjD/qJaSaHEydqD4dM5B9ndD3C9Oi1NrfOvZcMomuqF41JNxGdez0w1qd7IMMp1dpbt MpLw==; 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 38308e7fff4ca-2fcb460681asi4862231fa.380.2024.10.25.10.27.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2024 10:27:49 -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 B08AF888EB; Fri, 25 Oct 2024 19:27:47 +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 B606289017; Fri, 25 Oct 2024 19:27:45 +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_SOFTFAIL,T_SPF_HELO_TEMPERROR 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 61DEF8841E for ; Fri, 25 Oct 2024 19:27:40 +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 91105497; Fri, 25 Oct 2024 10:28:09 -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 0DFF23F71E; Fri, 25 Oct 2024 10:27:37 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Michal Simek , love.kumar@amd.com, Tom Rini , Sughosh Ganu Subject: [PATCH v2 1/2] common: memtop: add logic to detect ram_top Date: Fri, 25 Oct 2024 22:57:23 +0530 Message-Id: <20241025172724.195093-2-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241025172724.195093-1-sughosh.ganu@linaro.org> References: <20241025172724.195093-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 Add generic logic to determine the ram_top value for boards. Earlier, this was achieved in an indirect manner through a set of LMB API's. That has since changed so that the LMB code is available only after relocation. Replace those LMB calls with a single call to get_mem_top() to determine the value of ram_top. Signed-off-by: Sughosh Ganu Reviewed-by: Michal Simek --- Changes since V1: Remove the comment above the function definition common/Makefile | 1 + common/memtop.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++ include/memtop.h | 22 ++++++ 3 files changed, 194 insertions(+) create mode 100644 common/memtop.c create mode 100644 include/memtop.h diff --git a/common/Makefile b/common/Makefile index 2ee5ef9cc6e..35991562a12 100644 --- a/common/Makefile +++ b/common/Makefile @@ -7,6 +7,7 @@ ifndef CONFIG_XPL_BUILD obj-y += init/ obj-y += main.o +obj-y += memtop.o obj-y += exports.o obj-y += cli_getch.o cli_simple.o cli_readline.o obj-$(CONFIG_HUSH_OLD_PARSER) += cli_hush.o diff --git a/common/memtop.c b/common/memtop.c new file mode 100644 index 00000000000..841d89e0799 --- /dev/null +++ b/common/memtop.c @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2024, Linaro Limited + */ + +#include +#include +#include + +#include + +#define MEM_RGN_COUNT 16 + +struct region { + phys_addr_t base; + phys_size_t size; +}; + +struct mem_region { + struct region rgn[MEM_RGN_COUNT]; + uint count; +}; + +static void add_mem_region(struct mem_region *mem_rgn, phys_addr_t base, + phys_size_t size) +{ + long i; + + for (i = mem_rgn->count; i >= 0; i--) { + if (i && base < mem_rgn->rgn[i - 1].base) { + mem_rgn->rgn[i] = mem_rgn->rgn[i - 1]; + } else { + mem_rgn->rgn[i].base = base; + mem_rgn->rgn[i].size = size; + break; + } + } + + mem_rgn->count++; +} + +static void mem_regions_init(struct mem_region *mem) +{ + uint i; + + mem->count = 0; + for (i = 0; i < MEM_RGN_COUNT; i++) { + mem->rgn[i].base = 0; + mem->rgn[i].size = 0; + } +} + +static int fdt_add_reserved_regions(struct mem_region *free_mem, + struct mem_region *reserved_mem, + void *fdt_blob) +{ + u64 addr, size; + int i, total, ret; + int nodeoffset, subnode; + struct fdt_resource res; + + if (fdt_check_header(fdt_blob) != 0) + return -1; + + /* process memreserve sections */ + total = fdt_num_mem_rsv(fdt_blob); + assert_noisy(total < MEM_RGN_COUNT); + for (i = 0; i < total; i++) { + if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0) + continue; + add_mem_region(reserved_mem, addr, size); + } + + i = 0; + /* process reserved-memory */ + nodeoffset = fdt_subnode_offset(fdt_blob, 0, "reserved-memory"); + if (nodeoffset >= 0) { + subnode = fdt_first_subnode(fdt_blob, nodeoffset); + while (subnode >= 0) { + /* check if this subnode has a reg property */ + ret = fdt_get_resource(fdt_blob, subnode, "reg", 0, + &res); + if (!ret && fdtdec_get_is_enabled(fdt_blob, subnode)) { + addr = res.start; + size = res.end - res.start + 1; + assert_noisy(i < MEM_RGN_COUNT); + add_mem_region(reserved_mem, addr, size); + } + + subnode = fdt_next_subnode(fdt_blob, subnode); + ++i; + } + } + + return 0; +} + +static long addrs_overlap(phys_addr_t base1, phys_size_t size1, + phys_addr_t base2, phys_size_t size2) +{ + const phys_addr_t base1_end = base1 + size1 - 1; + const phys_addr_t base2_end = base2 + size2 - 1; + + return ((base1 <= base2_end) && (base2 <= base1_end)); +} + +static long region_overlap_check(struct mem_region *mem_rgn, phys_addr_t base, + phys_size_t size) +{ + unsigned long i; + struct region *rgn = mem_rgn->rgn; + + for (i = 0; i < mem_rgn->count; i++) { + phys_addr_t rgnbase = rgn[i].base; + phys_size_t rgnsize = rgn[i].size; + + if (addrs_overlap(base, size, rgnbase, rgnsize)) + break; + } + + return (i < mem_rgn->count) ? i : -1; +} + +static int find_ram_top(struct mem_region *free_mem, + struct mem_region *reserved_mem, phys_size_t size) +{ + long i, rgn; + phys_addr_t base = 0; + phys_addr_t res_base; + + for (i = free_mem->count - 1; i >= 0; i--) { + phys_addr_t rgnbase = free_mem->rgn[i].base; + phys_size_t rgnsize = free_mem->rgn[i].size; + + if (rgnsize < size) + continue; + + base = rgnbase + rgnsize - size; + while (base && rgnbase <= base) { + rgn = region_overlap_check(reserved_mem, base, size); + if (rgn < 0) + return base; + + res_base = reserved_mem->rgn[rgn].base; + if (res_base < size) + break; + base = res_base - size; + } + } + + return 0; +} + +phys_addr_t get_mem_top(phys_addr_t ram_start, phys_size_t ram_size, + phys_size_t size, void *fdt) +{ + int i; + struct mem_region free_mem; + struct mem_region reserved_mem; + + mem_regions_init(&free_mem); + mem_regions_init(&reserved_mem); + + add_mem_region(&free_mem, ram_start, ram_size); + + i = fdt_add_reserved_regions(&free_mem, &reserved_mem, fdt); + if (i < 0) + return 0; + + return find_ram_top(&free_mem, &reserved_mem, size); +} diff --git a/include/memtop.h b/include/memtop.h new file mode 100644 index 00000000000..28f62e24ea7 --- /dev/null +++ b/include/memtop.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (c) 2024, Linaro Limited + */ + +/** + * get_mem_top() - Compute the value of ram_top + * @ram_start: Start of RAM + * @ram_size: RAM size + * @size: Minimum RAM size requested + * @fdt: FDT blob + * + * The function computes the top address of RAM memory that can be + * used by U-Boot. This is being done by going through the list of + * reserved memory regions specified in the devicetree blob passed + * to the function. The logic used here is derived from the lmb + * allocation function. + * + * Return: address of ram top on success, 0 on failure + */ +phys_addr_t get_mem_top(phys_addr_t ram_start, phys_size_t ram_size, + phys_size_t size, void *fdt); From patchwork Fri Oct 25 17:27:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 838368 Delivered-To: patch@linaro.org Received: by 2002:adf:e287:0:b0:37d:45d0:187 with SMTP id v7csp415089wri; Fri, 25 Oct 2024 10:28:15 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCU/aNU5rEdXFD/uUGOR/0ccdykDEaHQnxLk5IXOBmmWIZwQMXIo0mQL/72ZHeV31V+/iZovGQ==@linaro.org X-Google-Smtp-Source: AGHT+IGUuQaa4lcaTMqX5CO9p/hnib5EKUY6DqJfTLA9BaIQNkp5Z6qzU1IcygE6kqmqL38TZmr1 X-Received: by 2002:a2e:b8c2:0:b0:2fb:565a:d93c with SMTP id 38308e7fff4ca-2fcbdf5ea76mr631071fa.1.1729877295253; Fri, 25 Oct 2024 10:28:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1729877295; cv=none; d=google.com; s=arc-20240605; b=NbkCu2GGgrzBGzh6LD0w8FPy3VbtzYQVRRGAJoblBh+Whvos1uT508z6uWblfffk2z hb9BpjhnN1Nz9+iuJkR1de/KGvj/cvhxkGkc4tN40Wb61l0WgzRFi8hCg22fxmFQmCkz FOshRNQtdAQ2eZ5w23ETVImZfGiJCmwmtFhEj1m7eAck/Ae5q/HjQP6MBPFLh76by+Fi 1cWZ1NLFPxC/hIQ2WmZUd7JBbLRxPY6dR5tOI1SrfO8kCsP7zy1ajWmIhJZ0zbvkq6b0 LrgzEn+PSOqWv3aZ1u6NVqbQ3QdY6/nUW+eEcfYso9PODXpsesgAVVjzi+ZLmYR9eYXh yP+Q== 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=5s4uToBrQknXC9IoJW/4442r9opXD7JwP/Tp1aTRyH0=; fh=4qhjWgg9bHeH/GNOfE5X3jcKCWXNzATraddlwAAhC5s=; b=FId2BBIIoT9PwI+MpWtQGIMyz2G65jh473KT13dNqNysJU3CEDjbOG90MlqCqZtMto XMQ+oZbFkNlJ+Veirv2QacJDO1SExYIrDiN03wdlXqcMjPDp+sa9fRxe/bYRp433Vkun hm8D67WOEwq8+ssWksk4WhWprr5OavtGZNlnFkBBMJqsQ/yIMQRrAFtOFdxNHeOIj4jp ubLnVzZzTNfYg4iOsUpWUW1DWllN0877BoAkNlSE8beMYC4hDwrBsJCBKfSm1pXXW/RL pUILodetuB9LdH+PkjxgK445f+wiT19DslbMJN3wm/b3Ith11WLd8wvh4rBbWsh7tWyq v3Tw==; 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 38308e7fff4ca-2fcb4625b87si5050721fa.469.2024.10.25.10.28.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2024 10:28:15 -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 80A1989020; Fri, 25 Oct 2024 19:27:52 +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 69E5689043; Fri, 25 Oct 2024 19:27: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.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 BD16E88A4C for ; Fri, 25 Oct 2024 19:27:42 +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 EB846497; Fri, 25 Oct 2024 10:28:11 -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 689D43F71E; Fri, 25 Oct 2024 10:27:40 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Michal Simek , love.kumar@amd.com, Tom Rini , Sughosh Ganu Subject: [PATCH v2 2/2] xilinx: use get_mem_top() to compute ram_top Date: Fri, 25 Oct 2024 22:57:24 +0530 Message-Id: <20241025172724.195093-3-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241025172724.195093-1-sughosh.ganu@linaro.org> References: <20241025172724.195093-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 Use the get_mem_top function to compute the value of ram_top. This was earlier done through LMB API's, which are no longer available till after relocation. Use get_mem_top() instead to compute the ram_top value. Signed-off-by: Sughosh Ganu Reviewed-by: Michal Simek --- board/xilinx/common/board.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/board/xilinx/common/board.c b/board/xilinx/common/board.c index 38dd80533fa..e14ed2cff00 100644 --- a/board/xilinx/common/board.c +++ b/board/xilinx/common/board.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include "board.h" #include @@ -676,3 +677,27 @@ int ft_board_setup(void *blob, struct bd_info *bd) return 0; } #endif + +#ifndef MMU_SECTION_SIZE +#define MMU_SECTION_SIZE (1 * 1024 * 1024) +#endif + +phys_addr_t board_get_usable_ram_top(phys_size_t total_size) +{ + phys_size_t size; + phys_addr_t reg; + + if (!total_size) + return gd->ram_top; + + if (!IS_ALIGNED((ulong)gd->fdt_blob, 0x8)) + panic("Not 64bit aligned DT location: %p\n", gd->fdt_blob); + + size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE); + reg = get_mem_top(gd->ram_base, gd->ram_size, size, + (void *)gd->fdt_blob); + if (!reg) + reg = gd->ram_top - size; + + return reg + size; +}