From patchwork Wed Aug 2 11:12:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Conor Dooley X-Patchwork-Id: 13338042 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7E53DC001DF for ; Wed, 2 Aug 2023 11:14:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9h5a4tVrrG/RCuMDYUY8iLhpypPCN/j0sT1RiR8bBoI=; b=GANrrVu1Ix7Pe4 4YVAMjWInyUxoloGR6y7Tw9RKVdUZkhmyT4CCQlZNiEx61YifclTBgQo1+rZ91D+tpH9anVDuwewD 5c+XZcgv7MR8THTOTeXEMDqvBOZ9KO88iddxJKku5TubIAMPvgkTd/DuN9CHviDZjV4yQS8WVfFZQ 7Z+xVApKYKWvAnCdoLn8qMxbJMpTiD3L53laB2FaCTcIsMjPqmb8p8+TrNPJpIJoVkJ73+h+RG0at 2TPFGH08XYn2hq+74o9pLWifNnlScZFME40re3tuh7Le9Fd3MgvnbZCu/FU8NG9irc4xImoXvXsG0 Fe87pLulx0Jtv6lt5ilQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qR9ns-004m5X-1B; Wed, 02 Aug 2023 11:14:08 +0000 Received: from esa.microchip.iphmx.com ([68.232.154.123]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qR9no-004m42-0d for linux-riscv@lists.infradead.org; Wed, 02 Aug 2023 11:14:06 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1690974844; x=1722510844; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hd7d47LxtBd1DyVtDUyG879vKOJH26KzWOIUEmCNKhs=; b=hgQxCjul8vTGbredAdN+R+rjdPXUbE7i7LlAny4FRe42JI+4zrVFJv8O KDQB6/mmObDe/OvWLi6TD44CNURd9oirFIo4YhKtHpWLV2R3JfaAY8aMI 97ylaiHakCyrxP19BoibgrXb8NNQA1+p3kMY/jg1IMZXyNvFvGQbFg7iU 4gKRjvMtKhUKCQgNpj7nrRvI9Vt+USwTmYuHndqEQu8sKpin/5vRTI35d l6bWVEzuPZISq/NPnvp/zzQ53gnivjvgi5OykzzSeYpsfoAE+E8etqKPN p+QIH5ZfxC5PyTOpCwT6myIFjew/R8H6n+iPniquAngrN2lJRfahmo81a w==; X-IronPort-AV: E=Sophos;i="6.01,249,1684825200"; d="scan'208";a="223462640" X-Amp-Result: SKIPPED(no attachment in message) Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa4.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 02 Aug 2023 04:14:00 -0700 Received: from chn-vm-ex03.mchp-main.com (10.10.85.151) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Wed, 2 Aug 2023 04:13:49 -0700 Received: from wendy.microchip.com (10.10.115.15) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server id 15.1.2507.21 via Frontend Transport; Wed, 2 Aug 2023 04:13:47 -0700 From: Conor Dooley To: CC: , , Paul Walmsley , Atish Patra , Anup Patel , Alexandre Ghiti , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Song Shuai , JeeHeng Sia , "Petr Tesarik" , , , Subject: [RFT 1/2] RISC-V: handle missing "no-map" properties for OpenSBI's PMP protected regions Date: Wed, 2 Aug 2023 12:12:52 +0100 Message-ID: <20230802-detention-second-82ab2b53e07a@wendy> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230802-purse-hydrant-6f44f77364b0@wendy> References: <20230802-purse-hydrant-6f44f77364b0@wendy> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4807; i=conor.dooley@microchip.com; h=from:subject:message-id; bh=hd7d47LxtBd1DyVtDUyG879vKOJH26KzWOIUEmCNKhs=; b=owGbwMvMwCFWscWwfUFT0iXG02pJDCmnrIw79JoOSM+ft5fLNlJCZf3HzV9/3m0r7wszedRjd5Pr RPK1jlIWBjEOBlkxRZbE230tUuv/uOxw7nkLM4eVCWQIAxenAEzEfiojw1ZRWbnNXZeKb8ttTvKeeK /CpUxJzvRtdHTfims9TGoP7RgZJpY4XDn29Zj+5Gutgk+OH6rZvkg6bfMDS97tE1O8S8yyWQE= X-Developer-Key: i=conor.dooley@microchip.com; a=openpgp; fpr=F9ECA03CF54F12CD01F1655722E2C55B37CF380C X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230802_041404_610586_7BA3D6FD X-CRM114-Status: GOOD ( 19.01 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Add an erratum for versions [v0.8 to v1.3) of OpenSBI which fail to add the "no-map" property to the reserved memory nodes for the regions it has protected using PMPs. Our existing fix sweeping hibernation under the carpet by marking it NONPORTABLE is insufficient as there are other ways to generate accesses to these reserved memory regions, as Petr discovered [1] while testing crash kernels & kdump. Intercede during the boot process when the afflicted versions of OpenSBI are present & set the "no-map" property in all "mmode_resv" nodes before the kernel does its reserved memory region initialisation. Reported-by: Song Shuai Link: https://lore.kernel.org/all/CAAYs2=gQvkhTeioMmqRDVGjdtNF_vhB+vm_1dHJxPNi75YDQ_Q@mail.gmail.com/ Reported-by: JeeHeng Sia Link: https://groups.google.com/a/groups.riscv.org/g/sw-dev/c/ITXwaKfA6z8 Reported-by: Petr Tesarik Closes: https://lore.kernel.org/linux-riscv/76ff0f51-d6c1-580d-f943-061e93073306@huaweicloud.com/ [1] CC: stable@vger.kernel.org Signed-off-by: Conor Dooley Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/sbi.h | 5 +++++ arch/riscv/kernel/sbi.c | 42 +++++++++++++++++++++++++++++++++++- arch/riscv/mm/init.c | 3 +++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 5b4a1bf5f439..5360f3476278 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -252,6 +252,9 @@ enum sbi_pmu_ctr_type { #define SBI_ERR_ALREADY_STARTED -7 #define SBI_ERR_ALREADY_STOPPED -8 +/* SBI implementation IDs */ +#define SBI_IMP_OPENSBI 1 + extern unsigned long sbi_spec_version; struct sbiret { long error; @@ -259,6 +262,8 @@ struct sbiret { }; void sbi_init(void); +void sbi_apply_reserved_mem_erratum(void *dtb_va); + struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, diff --git a/arch/riscv/kernel/sbi.c b/arch/riscv/kernel/sbi.c index c672c8ba9a2a..aeb27263fa53 100644 --- a/arch/riscv/kernel/sbi.c +++ b/arch/riscv/kernel/sbi.c @@ -5,8 +5,10 @@ * Copyright (c) 2020 Western Digital Corporation or its affiliates. */ +#include #include #include +#include #include #include #include @@ -583,6 +585,40 @@ long sbi_get_mimpid(void) } EXPORT_SYMBOL_GPL(sbi_get_mimpid); +static long sbi_firmware_id; +static long sbi_firmware_version; + +/* + * For devicetrees patched by OpenSBI a "mmode_resv" node is added to cover + * the region OpenSBI has protected by means of a PMP. Some versions of OpenSBI, + * [v0.8 to v1.3), omitted the "no-map" property, but this trips up hibernation + * among other things. + */ +void __init sbi_apply_reserved_mem_erratum(void *dtb_pa) +{ + int child, reserved_mem; + + if (sbi_firmware_id != SBI_IMP_OPENSBI) + return; + + if (!acpi_disabled) + return; + + if (sbi_firmware_version >= 0x10003 || sbi_firmware_version < 0x8) + return; + + reserved_mem = fdt_path_offset((void *)dtb_pa, "/reserved-memory"); + if (reserved_mem < 0) + return; + + fdt_for_each_subnode(child, (void *)dtb_pa, reserved_mem) { + const char *name = fdt_get_name((void *)dtb_pa, child, NULL); + + if (!strncmp(name, "mmode_resv", 10)) + fdt_setprop((void *)dtb_pa, child, "no-map", NULL, 0); + } +}; + void __init sbi_init(void) { int ret; @@ -596,8 +632,12 @@ void __init sbi_init(void) sbi_major_version(), sbi_minor_version()); if (!sbi_spec_is_0_1()) { + sbi_firmware_id = sbi_get_firmware_id(); + sbi_firmware_version = sbi_get_firmware_version(); + pr_info("SBI implementation ID=0x%lx Version=0x%lx\n", - sbi_get_firmware_id(), sbi_get_firmware_version()); + sbi_firmware_id, sbi_firmware_version); + if (sbi_probe_extension(SBI_EXT_TIME)) { __sbi_set_timer = __sbi_set_timer_v02; pr_info("SBI TIME extension detected\n"); diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 70fb31960b63..cb16bfdeacdb 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -253,6 +254,8 @@ static void __init setup_bootmem(void) * in the device tree, otherwise the allocation could end up in a * reserved region. */ + + sbi_apply_reserved_mem_erratum(dtb_early_va); early_init_fdt_scan_reserved_mem(); /* From patchwork Wed Aug 2 11:12:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Conor Dooley X-Patchwork-Id: 13338041 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 29300C04E69 for ; Wed, 2 Aug 2023 11:14:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=k6+NMpkLRDSbZU5LMSrXRS8N2z+bZe8ygU4v0BhMJms=; b=dsBxiJoJarL51v IRF1KRWtRqJQasthETpMT7QJGqXp3ObO302YJ+RRB3xZCs3y+OgNNq4sCBwkQvE47ldtTNZ7OcCDP ZbMC7CPPihV1NYSYMUm3LLTcIhPRpIWQL5Af18K+XfPxITvqLtkhvXIFYQfIO6Abcsy6xpJlc0rWv LA6i3pbeKOGd2y5mKu8cFntRBqqIiSZtdXRDJ9EPB0CmaAnwhnfPD7pbjbuOOAphVGr5LdwsF18Yj 2rH9LueD0F3w6Jkb5G/LPl6GyOL9mBLMMoHQWNtyLs5wH/xvlTR2YDNk2wkc1d6zJtSiDutlB5BXv OwRx2UEFEn+q4KbNLWVA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qR9ns-004m5b-2y; Wed, 02 Aug 2023 11:14:08 +0000 Received: from esa.microchip.iphmx.com ([68.232.154.123]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qR9nq-004m41-0l for linux-riscv@lists.infradead.org; Wed, 02 Aug 2023 11:14:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1690974846; x=1722510846; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+upP9bsbQbhCuHLUsyHfsie8k02JT+wkJ/OXUvH+dCU=; b=lrM4YatxbQ7g0Xa97OMVS4TzEagRsHcfV6oiyKtrxAnhWAoBwb9P/Ifs uP4CZWcgCelALy5rHiH6fWyxfa/RHsb7dAxGKgaDcqCcmdaHBbUt7n3mE Ghz/3dKptOt3GhdpjwmxuhMS1UyZwT2wbvu8rxohQRu75muTqv+XoAQUa 6tStlFB8IKyAeJykr4cUvxL3X51c+3XL806ixZKwolKB3fLD9PgkX6qQO 1TG2AIY2Z+8WzEQymSQsZDP88FykfTYZF1HCo1L6P6rQcODPVJfCNYgPS 3hQyr1CrjbKaBWuVZPe2vBLAWN2vz3BfdCCbGMMbu3F5GE6RIWPj5vx7t A==; X-IronPort-AV: E=Sophos;i="6.01,249,1684825200"; d="scan'208";a="223462648" X-Amp-Result: SKIPPED(no attachment in message) Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa4.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 02 Aug 2023 04:14:01 -0700 Received: from chn-vm-ex03.mchp-main.com (10.10.85.151) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Wed, 2 Aug 2023 04:13:53 -0700 Received: from wendy.microchip.com (10.10.115.15) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server id 15.1.2507.21 via Frontend Transport; Wed, 2 Aug 2023 04:13:50 -0700 From: Conor Dooley To: CC: , , Paul Walmsley , Atish Patra , Anup Patel , Alexandre Ghiti , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Song Shuai , JeeHeng Sia , "Petr Tesarik" , , Subject: [RFT 2/2] Revert "RISC-V: mark hibernation as nonportable" Date: Wed, 2 Aug 2023 12:12:53 +0100 Message-ID: <20230802-chef-throng-d9de8b672a49@wendy> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230802-purse-hydrant-6f44f77364b0@wendy> References: <20230802-purse-hydrant-6f44f77364b0@wendy> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=921; i=conor.dooley@microchip.com; h=from:subject:message-id; bh=+upP9bsbQbhCuHLUsyHfsie8k02JT+wkJ/OXUvH+dCU=; b=owGbwMvMwCFWscWwfUFT0iXG02pJDCmnrIwVuNwC93y7J2XVkK/6/9F8O97lc2RvBM9Z2Hh3YuHx rK2OHaUsDGIcDLJiiiyJt/tapNb/cdnh3PMWZg4rE8gQBi5OAZhI5ktGhomZ9d+dS0QK2WJO3ZFgdt WqPFWSUuq47fa0t5zly3yCaxn+KR3+ObNs0/lPHaUnqg1CpG8tu+WnzOdkF1E/j89m+dYj/AA= X-Developer-Key: i=conor.dooley@microchip.com; a=openpgp; fpr=F9ECA03CF54F12CD01F1655722E2C55B37CF380C X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230802_041406_336234_426A6F6F X-CRM114-Status: UNSURE ( 9.68 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Revert commit ed309ce52218 ("RISC-V: mark hibernation as nonportable") as the kernel now has a way to handle the "no-map"-less, PMP protected, reserved memory nodes for the affected versions of OpenSBI. Signed-off-by: Conor Dooley --- arch/riscv/Kconfig | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 4c07b9189c86..47aef21073f9 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -866,11 +866,8 @@ menu "Power management options" source "kernel/power/Kconfig" -# Hibernation is only possible on systems where the SBI implementation has -# marked its reserved memory as not accessible from, or does not run -# from the same memory as, Linux config ARCH_HIBERNATION_POSSIBLE - def_bool NONPORTABLE + def_bool y config ARCH_HIBERNATION_HEADER def_bool HIBERNATION