Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75D0FC6377A for ; Thu, 22 Jul 2021 00:05:11 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4129161241 for ; Thu, 22 Jul 2021 00:05:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4129161241 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BFD1C81269; Thu, 22 Jul 2021 02:05:07 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1626912308; bh=3TlWTUQOpbYK+Yzq/tzkQdPHX0fwhFFuNaJhPSgKltE=; h=From:To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From; b=v6h2ivu6sx/yd4ltjciLFs5hbW7TCxi26B1qjkJ7deFs95w3ne+b7/mTUY3NB1+pm E1Jy/JkW8Lh1vu+1VoLTQAPaWt7LFtMLmEILBDxofSnICsI5iLzv8AaJGH+0U/xDSL 8hBv23n5S8GcEadgCVQ+vLyEAYol/O/kxpWRUWLMvHr8QbEMdRlBDtrFf9rCDfQBf9 StCWVLPM2xmx7RX5oHO8Mrboh7gyTm12V1rwIrXlJllICW2UpekFAZs9CpZDEU8d6Z dwCG1PQRo8f7vweS5cNefPemdee5n+Xg8ZHq16g/R6vJd/HBXH0qc+3Px3alcj1X/O IllOUNjAc63mw== Received: from tr.lan (ip-89-176-112-137.net.upcbroadband.cz [89.176.112.137]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: marex@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id AF047800AA; Thu, 22 Jul 2021 02:05:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1626912306; bh=3TlWTUQOpbYK+Yzq/tzkQdPHX0fwhFFuNaJhPSgKltE=; h=From:To:Cc:Subject:Date:From; b=kCyhvA5EAS4TKHM2hWQRFsmINyoNH+UUHK7q9U8MzxrMKQfWhj9KKuVh509WZA8g4 42cig7UkQzAr/aDB7YbwPyE509zrd89lCUInvhkKw3c0dBSTvgvMwKXF2pNmxb1CND qmpRqy3yhFkmOp0852/oFUmy9HKJJeVIccYtv0NJH/2E0az/DpBNcK/S/9LV1JVNz7 slswhCHw/OT9djaedSDx9cPyy6xY1rztR6JGVDVFRtOobu2rzRPPuMa8j+7PVinZFy uhj+JpuF93OsBbqBYSov+sV5cEv8AUl8AQC7audT3PNxOon0LOF1I0BV7JMzYjeBPh 08Lw+OcbTET/g== From: Marek Vasut To: u-boot@lists.denx.de Cc: Marek Vasut , Heinrich Schuchardt , Simon Glass , Tom Rini Subject: [PATCH][RFC] image: fdt: Fix DT relocation handling with multiple DRAM banks with gap Date: Thu, 22 Jul 2021 02:05:02 +0200 Message-Id: <20210722000502.327622-1-marex@denx.de> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.2 at phobos.denx.de X-Virus-Status: Clean The current implementation of boot_relocate_fdt() places DT at the highest usable DRAM address, which is calculated as: env_get_bootm_low() + env_get_bootm_mapsize() which by default becomes gd->ram_base + gd->ram_size. Systems like i.MX53 can have multiple DRAM banks with gap between them, e.g. have DRAM at 0x70000000-0x8fffffff and 0xb0000000-0xcfffffff , so for them the calculated highest DRAM address is 0xafffffff, which is exactly in the gap and thus not usable. Fix this by iterating over all DRAM banks and tracking the remaining amount of the total mapping size obtained from env_get_bootm_mapsize(). Limit the maximum LMB area size to each bank, to avoid using nonexistent DRAM. Signed-off-by: Marek Vasut Cc: Heinrich Schuchardt Cc: Simon Glass Cc: Tom Rini --- common/image-fdt.c | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/common/image-fdt.c b/common/image-fdt.c index 327a8c4c395..2b199ffc2b1 100644 --- a/common/image-fdt.c +++ b/common/image-fdt.c @@ -157,8 +157,11 @@ int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size) { void *fdt_blob = *of_flat_tree; void *of_start = NULL; + u64 start, size, usable; char *fdt_high; + ulong mapsize, low; ulong of_len = 0; + int bank; int err; int disable_relocation = 0; @@ -198,10 +201,39 @@ int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size) (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000); } } else { - of_start = - (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000, - env_get_bootm_mapsize() - + env_get_bootm_low()); + mapsize = env_get_bootm_mapsize(); + low = env_get_bootm_low(); + of_start = NULL; + + for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) { + start = gd->bd->bi_dram[bank].start; + size = gd->bd->bi_dram[bank].size; + + /* DRAM bank addresses are too low, skip it. */ + if (start + size < low) + continue; + + usable = min(size, (u64)mapsize); + + /* + * At least part of this DRAM bank is usable, try + * using it for LMB allocation. + */ + of_start = + (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000, + start + usable); + /* Allocation succeeded, use this block. */ + if (of_start != NULL) + break; + + /* + * Reduce the mapping size in the next bank + * by the size of attempt in current bank. + */ + mapsize -= usable - max(start, (u64)low); + if (!mapsize) + break; + } } if (of_start == NULL) { -- 2.30.2