From patchwork Wed Oct 21 00:39:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 1385263 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=tKN/V9AM; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=wdc.com header.i=@wdc.com header.a=rsa-sha256 header.s=dkim.wdc.com header.b=oKm56gqv; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CGBV50Bvbz9sSG for ; Wed, 21 Oct 2020 11:40:10 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=07D/NYT8Q83x9n+i8xETFm4VvXuHHgofCnVZxCf7N/4=; b=tKN/V9AMqTP54C8LZHywG4bAps DfVODQOVrH97oc+ZeelpntXXYJjOigo/p8FWq7FBfGkC/1S+fuggIJDku4BDbQ2Fz6oCDPMPNMso4 0NSWFMWucbFDXkuZFsc+kbdR4DTGoX1uwwQfIU09qRQ5o+TU1M+8Y24PriAJfgoODKGLi0oEjpbnb wW13J6h7aW5FKXS1Fb2fXwlWY7TkdUWh4qj3tavXQUk+CKZW5me4URz9fiGzV63zQEZLd6ywNQloT +07rtSVOn25SF3PgfKlv/Xk9CYWVbThg8AeO56WIgedRi0b1yByJ4IJsg9MyGQz0w65u3Hbnt3Xjh Jx+MZVsA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kV2AW-0006zy-B8; Wed, 21 Oct 2020 00:39:56 +0000 Received: from esa3.hgst.iphmx.com ([216.71.153.141]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kV2AS-0006zb-SZ for opensbi@lists.infradead.org; Wed, 21 Oct 2020 00:39:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1603240793; x=1634776793; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=ZGOAwmr2Ftz3OzRqxNOTdmzlnK2UWn1sVLGB9tB8jpI=; b=oKm56gqvvOXsgvBUxI/sq2m3KBvd5QVpu5vEfUA/oskP/Xv626pzmocM x+AyNK+DUd/16g8I0Sslh+2Obk8Izmg4iZvJ0PRle5dMYIiHzFk5kSnys jLRifD7b1cSTjF0u+AoeT+vd2wDIkxyKWQdvozWC22MrV8bsX5Y6pwCCn EvJiQdcvruqRBAU0aYgSOnAHF+8LVxaRYbsUl+YhzMVK4RHYGQTbSkThv OBHYNCxb52TP/sVI+Xzc+T1vyJx+AL2eVMhDmpj2qWUE9HXgT0Sm048E9 d+eP6MESqZfGwtZdrPnIMBDOz1aSbeIEs9ILRHT+VAEjVsLXWE2HPoegF Q==; IronPort-SDR: Q2YoYyJ86SXG4hwl7KtHrk7ijIUawsALNiUsEs+atN0kLA8mWPDwMonYndC11+irHJt2jCJ2Vb Kpm1HQOTaEjiEaLg50jwf1fKQgC3vdKIKUSGErshtOkHeMQ9Q5+l6s9jRUrmRRBdCEHEYOiGvi CLY2GmJ1VkqXnIS25Vr7nJ0R8c4/4nJnCvirD6JQbeCN8z+qVgPBdzLKtSVn+LhSrtozIa3WNW TZ4LOa1UJWsbsqsCRHN2+4S0OyAUeAlzhnpYfPeMHws0KJJZLGb2ln2+iwGkonotHzdbzlPgDr pPA= X-IronPort-AV: E=Sophos;i="5.77,399,1596470400"; d="scan'208";a="154902504" Received: from h199-255-45-14.hgst.com (HELO uls-op-cesaep01.wdc.com) ([199.255.45.14]) by ob1.hgst.iphmx.com with ESMTP; 21 Oct 2020 08:39:48 +0800 IronPort-SDR: PK5FriMtLIpzdtqhvW0LC3WEywICVM8KlhgqmTANxRmyUeO1rVGiEe7a15Z6ibinm8zsCMUwP6 GqNHsOj7DAzMOCIUXHqSoz/obcppKdoebRr9GHZPSNvrTXsvVPW/H2HG5iDD4+MajB5fgdV9GF Z2nv5dUyDHoMnUnQ9qJXn5PYouc5H5+Ha1BEKYjVBGLJUUT8WToDdEuUHG1dX+EUKsMuXOIN6N TRZvmgaubzCk41DDmol1Llq3p0EJU2EX2IDvidZZmJDQcgMcKjUCDyT/QMxZDz7bheSWs11Cko lovRpLBmoWGiK2cFErvNcnFd Received: from uls-op-cesaip02.wdc.com ([10.248.3.37]) by uls-op-cesaep01.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Oct 2020 17:26:13 -0700 IronPort-SDR: ++qX9bXn9z/TB9rQwQbZp/R0xbjh9dTq7YygAb1OwDYjvfvvyFN0uK9E3oYB1diSYcWwquFJOa 2tuXgMpeAB8ebX6xpGl2V8+3UjetSxBskd7OAqhDMl7s4w/ay1oOq0O5GJwOpyyjYiAxvq2kWt 2iRAeahLDiZfSMycGLP5nOaoIGN/uS3p8HemenFsFbdJlcO7hjLMhnhUO/b7Z5YpYBEzd5jg+D 52P0tcIqKy9mVpyXN28oBOukNM6Ietx1onRmfQ4V2xf1anPN3YHf3qeL58UUlRmKnOtE8CTMs5 TwM= WDCIronportException: Internal Received: from usa003000.ad.shared (HELO jedi-01.hgst.com) ([10.86.60.38]) by uls-op-cesaip02.wdc.com with ESMTP; 20 Oct 2020 17:39:48 -0700 From: Atish Patra To: opensbi@lists.infradead.org Subject: [PATCH v2] lib: utils: Implement "ranges" property parsing Date: Tue, 20 Oct 2020 17:39:13 -0700 Message-Id: <20201021003913.2376881-1-atish.patra@wdc.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201020_203953_195868_EE56AF91 X-CRM114-Status: GOOD ( 17.04 ) X-Spam-Score: -2.5 (--) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-2.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [216.71.153.141 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Atish Patra , anup.patel@wdc.com Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org The "reg" property in a device node may not be the correct address always. If a parent node defines a "ranges" property, the child address need to be translated with respect to parents address. If the ranges property is not present, it will just use 1:1 translation. Signed-off-by: Atish Patra Reviewed-by: Anup Patel --- Changes from v1->v2: 1. Improved the range parsing by supporting multiple parent nodes. --- lib/utils/fdt/fdt_helper.c | 49 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/utils/fdt/fdt_helper.c b/lib/utils/fdt/fdt_helper.c index aec73a0598d7..90d55dcd8e71 100644 --- a/lib/utils/fdt/fdt_helper.c +++ b/lib/utils/fdt/fdt_helper.c @@ -71,10 +71,47 @@ int fdt_find_match(void *fdt, int startoff, return SBI_ENODEV; } +int fdt_translate_address(void *fdt, uint64_t reg, int parent, unsigned long *addr) +{ + int i, rlen; + int cell_addr, cell_size; + const fdt32_t *ranges; + uint64_t offset = 0, caddr = 0, paddr = 0, rsize = 0; + + cell_addr = fdt_address_cells(fdt, parent); + if (cell_addr < 1) + return SBI_ENODEV; + + cell_size = fdt_size_cells(fdt, parent); + if (cell_size < 0) + return SBI_ENODEV; + + ranges = fdt_getprop(fdt, parent, "ranges", &rlen); + if (ranges && rlen > 0) { + for (i = 0; i < cell_addr; i++) + caddr = (caddr << 32) | fdt32_to_cpu(*ranges++); + for (i = 0; i < cell_addr; i++) + paddr = (paddr << 32) | fdt32_to_cpu(*ranges++); + for (i = 0; i < cell_size; i++) + rsize = (rsize << 32) | fdt32_to_cpu(*ranges++); + if (reg < caddr || caddr >= (reg + rsize )) { + sbi_printf("invalid address translation\n"); + return SBI_ENODEV; + } + offset = reg - caddr; + *addr = paddr + offset; + } else { + /* No translation required */ + *addr = reg; + } + + return 0; +} + int fdt_get_node_addr_size(void *fdt, int node, unsigned long *addr, unsigned long *size) { - int parent, len, i; + int parent, len, i, rc; int cell_addr, cell_size; const fdt32_t *prop_addr, *prop_size; uint64_t temp = 0; @@ -98,7 +135,15 @@ int fdt_get_node_addr_size(void *fdt, int node, unsigned long *addr, if (addr) { for (i = 0; i < cell_addr; i++) temp = (temp << 32) | fdt32_to_cpu(*prop_addr++); - *addr = temp; + do { + if (parent < 0) + break; + rc = fdt_translate_address(fdt, temp, parent, addr); + if (rc) + break; + parent = fdt_parent_offset(fdt, parent); + temp = *addr; + } while (1); } temp = 0;