From patchwork Fri Oct 23 22:47:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 1387005 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=pdQZFLlH; 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=YY6qOloW; 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 4CHzs62pFxz9sSn for ; Sat, 24 Oct 2020 09:47:54 +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=xRpzrk4uMXsfOVcao2VXGm9cPs1EMjNENZ0uwZvvWRo=; b=pdQZFLlHdkmyNgjRLfoFrd5ia7 VV+AoLpPGnd5msx9aUhS1/7FwKr+XO70MBfH6ez6L8Dmp0I5oCoMB6588oT2l1mueHvdNGcpRmA4x Ks134isXWQNyIpeWkE7geYi04Fi0jKRvTd3nTHv3iXn6B7/MFdeiwkpM9dIFGCGgVZzRGJrtu768P Adnv1bFzbYBqVOItcEqdal759bVjPNglKCNw1avtTOChTVwIse9dczNg5rMyN+e9nB+LvqfpfR4oC W02syTAWRWyYvRBruHcJ8hmrLcQFy2OlmGK9txo6IHS3LMhXtxsd5GyTyte6T24OzToQ/kqykiEWw hzGtr8ww==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kW5qf-0001oH-Io; Fri, 23 Oct 2020 22:47:49 +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 1kW5qd-0001no-6N for opensbi@lists.infradead.org; Fri, 23 Oct 2020 22:47:47 +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=1603493267; x=1635029267; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=wUJgapDN6EWtbRbMZ524hDF8WLZ7znIKiBDAWKOhl8M=; b=YY6qOloWXpENaFh/a0tvQTyHbCoNTE83MeTdXkkNYWCbHfU67BJpOXY1 7FMs4InHYXvYv3tTxzQg7rkWFA0+4c3jf3cebmtJfiVv2PwiiIWXA5He8 P63fzTD2g7iVuUoiy4oXnCf4/kRr5R2XZMZyZdyiFskfuyHkhxwNtr9rJ waAWSQC7b/XVD5rtlslsfju5pEvGhUCSZCWCD4ujWCvq3t9Dlmgh8mtmY 71DlWyjCJsnmksLOAKa7W/vBJRWHxrOuJ9Bc8dDJJEyfqKxxy3vG/TC4L Bl3gW+ltrs7ZPfwMnViM8zZvjSR9XOYYeTdbmNxOL/0hT43b44hwXDAZu A==; IronPort-SDR: djz82a+i3Y8jooEPEW8D0Ob/8txQF4+P9ZRRUVEgVekPQEFJTRp3qaNxLnasDQD7RV6Eas38E1 LjxSdh1y60fym/g9xisCsLAPFx4uDWjRYnaUi35Js7YtQnUNHW/koh1kQjbLTIGWf/o1rrnR99 /kvCCLTuDaOvT6FyH3bXlGJSJJMeBt5wf4GoEuGOcRIrZ8SnmJ4vpDWQnep0R8d6UF9rquqSY5 I9QpARN4tWuLd76jrBG7JDjjygWRv/5oB9RcPQfB5qwcUNwC1WEvcExg/DnZ126Y04AiSaYnnJ CyA= X-IronPort-AV: E=Sophos;i="5.77,409,1596470400"; d="scan'208";a="155194747" Received: from h199-255-45-15.hgst.com (HELO uls-op-cesaep02.wdc.com) ([199.255.45.15]) by ob1.hgst.iphmx.com with ESMTP; 24 Oct 2020 06:47:45 +0800 IronPort-SDR: log2N/oAWCZmDCd6bxae0jlH8JGIRJ0REnuEC8GKQZ5Gj4KQIP+LDruuIbWVnqKchltkHcRSv6 uISa01aOcG15Jkw0t+f2l78hDnPJczFmH1Z020XUQTEUxqI1kiSXdSjqHPbF7bz89Dj89Ya8jB JrgOwRk7k9rWgRwMSoSd/RdLIUsSTzfgyW+Bq+c6gLfXxytJybxLJNUJZCb9H22hjou/RIFMYS GbpdaB+aCftHVMvwoAN494KO0OlCG28en7+Rp9lZZFVFF04c5U0fBcpxYud0e30uIqqIdWsOyQ PYIUU2vqHCnuLDcLoGWEIpZD Received: from uls-op-cesaip01.wdc.com ([10.248.3.36]) by uls-op-cesaep02.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Oct 2020 15:33:00 -0700 IronPort-SDR: BuXVVSWTbX/zHU30dJJxlJq5iYaDa+0NckUITaQR/eSSB+JSKycl6JkxjZiAWpqzXrLUy581oN LAgy1VyQiteWGs0JNpOHXXnwCJtnuY8CQRBKA64jBQCqKulSl2qhXsNT4IzfodVQ2t8iRuRvKG lTBXxMm3ZDsk4mFH8+ennHs6ywyRsQ72IbBAB+rO/Hi5DreNCqashC+TnrSX+yJYwUOTRz0/Vh LxnSSDPQTV042Lce18rGWW4lVxU+pBUOYlNgLCli9IKkPNtHmUYwHyPGsQvCtEQclv5r0kKZah Yt4= WDCIronportException: Internal Received: from myd002180.ad.shared (HELO jedi-01.hgst.com) ([10.86.60.107]) by uls-op-cesaip01.wdc.com with ESMTP; 23 Oct 2020 15:47:45 -0700 From: Atish Patra To: opensbi@lists.infradead.org Subject: [PATCH v3] lib: utils: Implement "ranges" property parsing Date: Fri, 23 Oct 2020 15:47:39 -0700 Message-Id: <20201023224739.3837478-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-20201023_184747_373529_FC7C50BD X-CRM114-Status: GOOD ( 16.45 ) 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_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -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 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 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 --- lib/utils/fdt/fdt_helper.c | 50 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/utils/fdt/fdt_helper.c b/lib/utils/fdt/fdt_helper.c index aec73a0598d7..bf19ff92d5bd 100644 --- a/lib/utils/fdt/fdt_helper.c +++ b/lib/utils/fdt/fdt_helper.c @@ -71,10 +71,48 @@ int fdt_find_match(void *fdt, int startoff, return SBI_ENODEV; } +static 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 +136,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;