Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id CE0D1C433F5 for ; Fri, 8 Apr 2022 00:09:36 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 47F6D83936; Fri, 8 Apr 2022 02:09:34 +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=1649376574; bh=Q8eUEQ0XYLIVEWbAupR49ODXiYcYihpbwq2+ltjJZQg=; h=From:To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From; b=c98y3NcsR6Ouoibf2bnizSdcAAqEPWY5MvPDrgRdPuq43iUTJj+hfKW/ajbv3xOzn WyVYty0I+N5PL/U8OL0IJweossmTzcJjW7vsj/ad71i1DJfWj8UJMKtmSQBYanaiRS Tl6ibC+P9Cl3oUZuyYR7kgtIwPgm/JfASy4M1YfejijEp9Yt0HrZiiMyZ/x4lM6NJG idnJob6A5SmDj9cUGW+Us9eX+RqxKpGv3xMMBWDUT/i778bwuY4oXtSNA1eK+J4yPe HrdbnnbD1OjrquFsKBzuYSjML5I0dp7myL7eP1VqfeDOj1uAcV+YcPJ7L9i/gp+/XF 1osDCWLS9Ng7A== 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 0E7E48392C; Fri, 8 Apr 2022 02:09:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1649376572; bh=Q8eUEQ0XYLIVEWbAupR49ODXiYcYihpbwq2+ltjJZQg=; h=From:To:Cc:Subject:Date:From; b=mciSy9V6DCa0m8C5/fSY0rLXlFJbUg6M0OKoUejPGAgb70IBd7p0NJW8LPVH+JHFn kwiqj0TouGm3KxT6r/dlmRZaC75nQb0S+UJyG6jGPNCEIH9o0bTSs/yQHpJVsfTbMR t8qqcxA5w9ey0OYyjXppTGcJp1PtmqchIDohOoVlv7iwPQZeuPQe8T1zt4g0KvEhlB XXvsWIo2tNqVDRbBx6p9vmmFTUuKHbdhfqTtXgKYcIbfFHqfWpNS8x+Zrg7P3/NkCZ 8vWs/Dm4NQvuE29n3CjhSYnW3J03HVlMO7WB19u8s1WMR//Qzj6LNkMV/vHaUMtYq1 i+Om4onI4O8Vg== From: Marek Vasut To: u-boot@lists.denx.de Cc: Marek Vasut , Simon Glass , Heinrich Schuchardt , Tom Rini Subject: [PATCH v2] image: fdt: Fix DT relocation handling with multiple DRAM banks with gap Date: Fri, 8 Apr 2022 02:09:19 +0200 Message-Id: <20220408000919.654653-1-marex@denx.de> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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.5 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. Reviewed-by: Simon Glass Signed-off-by: Marek Vasut Cc: Heinrich Schuchardt Cc: Simon Glass Cc: Tom Rini --- V2: Collect RB, rebase on v2022.04+ --- boot/image-fdt.c | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/boot/image-fdt.c b/boot/image-fdt.c index 692a9ad3e42..fe2fc3351ba 100644 --- a/boot/image-fdt.c +++ b/boot/image-fdt.c @@ -165,8 +165,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; @@ -206,10 +209,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.35.1