From patchwork Tue Mar 24 04:16:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 1260451 X-Patchwork-Delegate: uboot@andestech.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: ozlabs.org; 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=N8FClzX5; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48mdK43h4Yz9sRf for ; Tue, 24 Mar 2020 15:18:16 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0FC6981854; Tue, 24 Mar 2020 05:17:51 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=wdc.com header.i=@wdc.com header.b="N8FClzX5"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3BB678183D; Tue, 24 Mar 2020 05:17:22 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from esa2.hgst.iphmx.com (esa2.hgst.iphmx.com [68.232.143.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 35AF2817C1 for ; Tue, 24 Mar 2020 05:17:11 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=wdc.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=prvs=345a454b9=atish.patra@wdc.com DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1585023454; x=1616559454; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=M2XNNP944RpmRA71PbUP1WSw6+YkbDF4WVaMU22kr64=; b=N8FClzX51z2KWEgkK7VxFtcD1x3OMA5wUPlt5H+BeKdUJOuhKyBF7qVo ngEN45899Aw0GbeWWhZkP82wNRTiwe2hSczkX/Zw1QkxYDfmBd3dcnTmQ AZKpINW6E88SAkNZ1iMjjvgMt8BbfspQ3WYYzE1IqtnX+vZQz/1fTO0v/ spbo8hnYSnaAzjYogMXTdtBbhgo8KEwVxx4rGHs9rEGxBvh2uiTG8ke0W Xswns+KbBcysw4hLvSfDEbhH6AtHBKmGhoBXSHHNFSHUIxhofAua021D4 /c7N1QJzeaKtmDbVWDtRK9EC62RgLp4rF78e3N7NTlZMMwa2ttcFYunHU g==; IronPort-SDR: 7YoJ1dZ3a0oJe5seG//qSrSUSTbXl0Y50YblHcsv+ognr0wua2Vti+AxkKmgn9BGHOEJTKBpzd KeLPawzYA0usMiNEOAl5JZ6scmlqnKoe64W0oZ990zdMKtjPdkW7lpBp3C+ynV5iZq3dO9sXVt yImfAJK4NeME5PM8Hw7mpxoKWzWqG+NR44dsrv2zKmMaYwV2bXo0KFS1JLcyOzpX9MxNNSOfR5 juwOArtlu8p+p/rBjCGfUMjirIET5/y8bR7pPiGt0OJcBkmy9SuDdIIzXTrKBzxL0LV9YvfQvA zvg= X-IronPort-AV: E=Sophos;i="5.72,298,1580745600"; d="scan'208";a="235522566" Received: from uls-op-cesaip01.wdc.com (HELO uls-op-cesaep01.wdc.com) ([199.255.45.14]) by ob1.hgst.iphmx.com with ESMTP; 24 Mar 2020 12:17:29 +0800 IronPort-SDR: ih82BOCkM456jWLDjih1rJ7B4De2nBpil+W32vApAJ1tfcqvHFClGgelkLA+6kPKQgpXqbvnDW kzs/m8aGjY3QjrhFZBDnnLo+yj8tyxDtsnK2R0+LG3VukgH0pkdsc4M84b4IX895FMNIFbzYrJ MMRyzAyhCFVF8KZ6RnPYL3a9nDcCdDtZv0KHz12vTlRKQSMKLHCyipnUhhcKQFCkStkohsw/cC mH4ckOmEHeYpwLxsbTzQfxrulkGpXynsTSd4Z8U1O58k/YeBa4s+Lt1fY418jc06QZh34fk+iC /aJHf4N29A+Ast2zQBgszoLm 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; 23 Mar 2020 21:08:48 -0700 IronPort-SDR: sFIKduacqEU6evF409CCaunkZDqek0WG5a/XZT/5cmjX3IyHVY++Yte+Zs9759DgVTdB9TvYTd oB1za3TPJ0R1U+rwlhy3qeQsi4Wwha0YMM/bKIf7teTXv+Fu0fi/tu9rRfUr/X9duD6uY4taz2 80Wixvlk+HvTCwQPstmBnoaEDyXJenbyPVAnLJ4CqwkbCdqZp2Uw9Qh/cf30mKLwXQ3hUN8vY5 WCFooCluxFarIpCbajO4IhAteFoasIy7XnHIY0zmSNgrdQTuLmSN0B7b3kNObexUv/y8KvF7FZ z7s= WDCIronportException: Internal Received: from cm122g2.ad.shared (HELO yoda.hgst.com) ([10.86.54.193]) by uls-op-cesaip02.wdc.com with ESMTP; 23 Mar 2020 21:17:07 -0700 From: Atish Patra To: u-boot@lists.denx.de Cc: Atish Patra , Alexander Graf , Anup Patel , Bin Meng , Heinrich Schuchardt , Lukas Auer , Rick Chen , abner.chang@hpe.com, daniel.schaefer@hpe.com, Thierry Reding Subject: [PATCH v4 3/6] riscv: Provide a mechanism to fix DT for reserved memory Date: Mon, 23 Mar 2020 21:16:47 -0700 Message-Id: <20200324041650.280302-4-atish.patra@wdc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200324041650.280302-1-atish.patra@wdc.com> References: <20200324041650.280302-1-atish.patra@wdc.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.30rc1 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.102.2 at phobos.denx.de X-Virus-Status: Clean In RISC-V, M-mode software can reserve physical memory regions by setting appropriate physical memory protection (PMP) csr. As the PMP csr are accessible only in M-mode, S-mode U-Boot can not read this configuration directly. However, M-mode software can pass this information via reserved-memory node in device tree so that S-mode software can access this information. This patch provides a framework to copy to the reserved-memory node from one DT to another. This will be used to update the DT used by U-Boot and the DT passed to the next stage OS. Signed-off-by: Atish Patra --- arch/riscv/cpu/start.S | 1 + arch/riscv/include/asm/global_data.h | 1 + arch/riscv/include/asm/u-boot-riscv.h | 2 + arch/riscv/lib/Makefile | 1 + arch/riscv/lib/asm-offsets.c | 1 + arch/riscv/lib/fdt_fixup.c | 80 +++++++++++++++++++++++++++ 6 files changed, 86 insertions(+) create mode 100644 arch/riscv/lib/fdt_fixup.c diff --git a/arch/riscv/cpu/start.S b/arch/riscv/cpu/start.S index 6b3ff99c3882..0282685c2906 100644 --- a/arch/riscv/cpu/start.S +++ b/arch/riscv/cpu/start.S @@ -121,6 +121,7 @@ call_board_init_f_0: jal board_init_f_init_reserve + SREG s1, GD_FIRMWARE_FDT_ADDR(gp) /* save the boot hart id to global_data */ SREG tp, GD_BOOT_HART(gp) diff --git a/arch/riscv/include/asm/global_data.h b/arch/riscv/include/asm/global_data.h index b74bd7e738bb..51ac8d1c98e2 100644 --- a/arch/riscv/include/asm/global_data.h +++ b/arch/riscv/include/asm/global_data.h @@ -15,6 +15,7 @@ /* Architecture-specific global data */ struct arch_global_data { long boot_hart; /* boot hart id */ + phys_addr_t firmware_fdt_addr; #ifdef CONFIG_SIFIVE_CLINT void __iomem *clint; /* clint base address */ #endif diff --git a/arch/riscv/include/asm/u-boot-riscv.h b/arch/riscv/include/asm/u-boot-riscv.h index 49febd588102..543a1688db8f 100644 --- a/arch/riscv/include/asm/u-boot-riscv.h +++ b/arch/riscv/include/asm/u-boot-riscv.h @@ -17,5 +17,7 @@ int cleanup_before_linux(void); /* board/.../... */ int board_init(void); void board_quiesce_devices(void); +int riscv_board_reserved_mem_fixup(void *fdt); +int riscv_fdt_copy_resv_mem_node(const void *src_fdt, void *dest_fdt); #endif /* _U_BOOT_RISCV_H_ */ diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile index adadbf4bcbef..d132b59ce32c 100644 --- a/arch/riscv/lib/Makefile +++ b/arch/riscv/lib/Makefile @@ -24,6 +24,7 @@ obj-y += reset.o obj-y += setjmp.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SPL_BUILD) += spl.o +obj-y += fdt_fixup.o # For building EFI apps CFLAGS_$(EFI_CRT0) := $(CFLAGS_EFI) diff --git a/arch/riscv/lib/asm-offsets.c b/arch/riscv/lib/asm-offsets.c index 4fa4fd371473..7301c1b98e23 100644 --- a/arch/riscv/lib/asm-offsets.c +++ b/arch/riscv/lib/asm-offsets.c @@ -14,6 +14,7 @@ int main(void) { DEFINE(GD_BOOT_HART, offsetof(gd_t, arch.boot_hart)); + DEFINE(GD_FIRMWARE_FDT_ADDR, offsetof(gd_t, arch.firmware_fdt_addr)); #ifndef CONFIG_XIP DEFINE(GD_AVAILABLE_HARTS, offsetof(gd_t, arch.available_harts)); #endif diff --git a/arch/riscv/lib/fdt_fixup.c b/arch/riscv/lib/fdt_fixup.c new file mode 100644 index 000000000000..f3d1ec5c5d02 --- /dev/null +++ b/arch/riscv/lib/fdt_fixup.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2020 Western Digital Corporation or its affiliates + * + */ + +#include +#include +#include + +DECLARE_GLOBAL_DATA_PTR; + +int riscv_fdt_copy_resv_mem_node(const void *src, void *dst) +{ + u32 phandle; + struct fdt_memory pmp_mem; + fdt_addr_t addr; + fdt_size_t size; + int offset, node, err, rmem_offset; + bool nomap = true; + char basename[32] = {0}; + int bname_len; + int max_len = sizeof(basename); + const char *name; + char *temp; + + offset = fdt_path_offset(src, "/reserved-memory"); + if (offset < 0) { + printf("No reserved memory region found in source FDT\n"); + return 0; + } + + fdt_for_each_subnode(node, src, offset) { + name = fdt_get_name(src, node, NULL); + + addr = fdtdec_get_addr_size_auto_noparent(src, node, + "reg", 0, &size, + false); + if (addr == FDT_ADDR_T_NONE) { + debug("failed to read address/size for %s\n", name); + continue; + } + strncpy(basename, name, max_len); + temp = strchr(basename, '@'); + if (temp) { + bname_len = strnlen(basename, max_len) - strnlen(temp, + max_len); + *(basename + bname_len) = '\0'; + } + pmp_mem.start = addr; + pmp_mem.end = addr + size - 1; + err = fdtdec_add_reserved_memory(dst, basename, &pmp_mem, + &phandle); + if (err < 0) { + printf("failed to add reserved memory: %d\n", err); + return err; + } + if (!fdt_getprop(src, node, "no-map", NULL)) + nomap = false; + if (nomap) { + rmem_offset = fdt_node_offset_by_phandle(dst, phandle); + fdt_setprop_empty(dst, rmem_offset, "no-map"); + } + } + + return 0; +} + +int riscv_board_reserved_mem_fixup(void *fdt) +{ + int err; + void *src_fdt_addr; + + src_fdt_addr = map_sysmem(gd->arch.firmware_fdt_addr, 0); + err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt); + if (err < 0) + return err; + + return 0; +}