From patchwork Mon Jan 6 15:48:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 13927608 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 8D769E7719A for ; Mon, 6 Jan 2025 15:51:05 +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=G8PkvakKaA033gXAk9LHMiMbSFlffw8YF+5QHkdZqMs=; b=Vz4uH4CJRslKiX NNHPmwwdGwAlJm/aJqhNgoKNlDduJc61tCzOUz7kKHpSUln0xKDynz/lws8ohxb37Jtn6s869vI4m GTEiDPsbsFkZag5AWFui7fu/7DKF2lqX5k7d0oAp4rYWm0MNUduDsSQepCXzm3eNN1R/zzLGvbLTM T9GidyREUrOiZ5iAJN/ggh8s1DRsJhllephD6JnbrZQnIl4nQk1q+8dYGfJsHmMLuMjpsLxVXYLbF fBRFHL0mcjTkF/xUee+67QxPLhWtygBQyH58ojjnd+suF3FN3PxQB6nt/o8mavUbhXjCY9DRyFwCQ NB4YXn4nInmOTFkSNZrw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tUpNd-00000001qVZ-0VRs; Mon, 06 Jan 2025 15:51:01 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tUpLz-00000001q4U-0zJC for linux-riscv@lists.infradead.org; Mon, 06 Jan 2025 15:49:20 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-2164b662090so184054525ad.1 for ; Mon, 06 Jan 2025 07:49:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736178558; x=1736783358; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hUAp13cfLrcRgomb+YDwJflWyLOeBTRPSVeLvL2aSrw=; b=Qvb11ir1oNeTHSjuk2ivkHhEe0CLYYZdfVyNdYEzIqiMHNuIkhYUnwJIjRAfQouFus N/C6elM2GFzAUNeMaAc2jO0ylduygrzJkPvvdB9VsspA90U2n3lgKDDnbOcc2ucyUy+y 7sNitQg4c7Pnn4ZTRRmshU6Avk6OzE1U6uilCyZlk2+lHL6MTaqDV9JRIpbI3tevQfBg PcRYllu9qiURM58+xsXbF0s+bXI7m/OXDtmEuLZ0GtfI1iZxUTcN5a64GDsVJDB99op8 nLhY8d41/ciy98j4aLPtrJWyMib7+hh6Ivh94lxHHCcxeRwU3MZQX8mc2i52Ing6jNs7 +4+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736178558; x=1736783358; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hUAp13cfLrcRgomb+YDwJflWyLOeBTRPSVeLvL2aSrw=; b=KMdGkc97LLtN12rOROBYYm2axOS3n7Bd++OBh7D/V7oUxdOtqbX/XHyj+TCQLLPU3u 7xlsw3gxgw55B0i6g6wcwTOzT2+O+moZ73hjOi1c/WMIvLAVbQvamKYRWYsmud66vWwP HPsi+zWk5E05o7xmEOf2ZNqYzlmUFCP5syNTGiQFa4EjwO/9xn+Rj1ExQugghYAQFYpI xsIGyKO+bdoed/s2Ea22/uq5SbEK3Wm3NXs4QwmQmDL4LONFms7sQhfzOIOo965NXm3i ES6ZesjBW4uU4A8f54NLhcP7Cz82PaVdElWlYMTVdsrImzKD8sVGLgKwnhFj+9a3o1Gg Zw6Q== X-Forwarded-Encrypted: i=1; AJvYcCUcZDR+kn7lXsZOL0cVZoHfQ4N3P++SRfUDZ75PczKUtue++XAN+R+99oTZv3eHMVxa1BI6MKfCT7Q/OQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yy/bB+wLxiEgrS29hBIsVZyfK85XcaH4JPYMJwOxn2eynglFD6p qzXlfjtWJkOLxvhU6pUzljAHoxj8RFOJNaoAvVFxqZ8nd4BcxxbCEvi9pSEoNvs= X-Gm-Gg: ASbGncvdFwLoM+x9W3uk0l/zIwh47/yTz0ceFAWl0FYd6p9U4nWUaCH/YfL9ylKPJoM 6H8COqsx+dfhMoQ/0y+8KGQiIWBZ2nvnBHGYyenSeb8s6iY+XJ+XFu+sjzAdth8Z6uCaWdxUzPF 5MnCrocXSkwl0frAXPNxoL7+Dcyi+Wuu+ng4UbKevINRErgXFKm+WeDdFdBP1da01KLXe3K9DFb ks3vAp/yUT7kIWj1xIli8cggP+ZE5PteBVfkMsnRK7HxJmMzefp8j4SMA== X-Google-Smtp-Source: AGHT+IEAi4C08D2uh9ehheSrgZdf7WN1M3JGxvQSdR5w+ojassJn4RAe3pkLwbVvSQ+6CKRXrqzn6Q== X-Received: by 2002:a17:903:946:b0:216:2af7:a2a3 with SMTP id d9443c01a7336-219e6f27157mr1007178485ad.53.1736178558552; Mon, 06 Jan 2025 07:49:18 -0800 (PST) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9f6967sm292479535ad.214.2025.01.06.07.49.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2025 07:49:17 -0800 (PST) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: Paul Walmsley , Palmer Dabbelt , Anup Patel , Atish Patra , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= Subject: [PATCH 2/6] riscv: request misaligned exception delegation from SBI Date: Mon, 6 Jan 2025 16:48:39 +0100 Message-ID: <20250106154847.1100344-3-cleger@rivosinc.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250106154847.1100344-1-cleger@rivosinc.com> References: <20250106154847.1100344-1-cleger@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250106_074919_273658_65EB88F6 X-CRM114-Status: GOOD ( 15.23 ) 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 Now that the kernel can handle misaligned accesses in S-mode, request misaligned access exception delegation from SBI. This uses the FWFT SBI extension defined in SBI version 3.0. Signed-off-by: Clément Léger Reviewed-by: Jesse Taube --- arch/riscv/include/asm/cpufeature.h | 1 + arch/riscv/kernel/traps_misaligned.c | 59 ++++++++++++++++++++++ arch/riscv/kernel/unaligned_access_speed.c | 2 + 3 files changed, 62 insertions(+) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index 4bd054c54c21..cd406fe37df8 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -62,6 +62,7 @@ void __init riscv_user_isa_enable(void); _RISCV_ISA_EXT_DATA(_name, _id, _sub_exts, ARRAY_SIZE(_sub_exts), _validate) bool check_unaligned_access_emulated_all_cpus(void); +void unaligned_access_init(void); #if defined(CONFIG_RISCV_SCALAR_MISALIGNED) void check_unaligned_access_emulated(struct work_struct *work __always_unused); void unaligned_emulation_finish(void); diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c index 7cc108aed74e..4aca600527e9 100644 --- a/arch/riscv/kernel/traps_misaligned.c +++ b/arch/riscv/kernel/traps_misaligned.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #define INSN_MATCH_LB 0x3 @@ -689,3 +690,61 @@ bool check_unaligned_access_emulated_all_cpus(void) return false; } #endif + +#ifdef CONFIG_RISCV_SBI + +struct misaligned_deleg_req { + bool enable; + int error; +}; + +static void +cpu_unaligned_sbi_request_delegation(void *arg) +{ + struct misaligned_deleg_req *req = arg; + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_FWFT, SBI_EXT_FWFT_SET, + SBI_FWFT_MISALIGNED_EXC_DELEG, req->enable, 0, 0, 0, 0); + if (ret.error) + req->error = 1; +} + +static void unaligned_sbi_request_delegation(void) +{ + struct misaligned_deleg_req req = {true, 0}; + + on_each_cpu(cpu_unaligned_sbi_request_delegation, &req, 1); + if (!req.error) { + pr_info("SBI misaligned access exception delegation ok\n"); + /* + * Note that we don't have to take any specific action here, if + * the delegation is successful, then + * check_unaligned_access_emulated() will verify that indeed the + * platform traps on misaligned accesses. + */ + return; + } + + /* + * If at least delegation request failed on one hart, revert misaligned + * delegation for all harts, if we don't do that, we'll panic at + * misaligned delegation check time (see + * check_unaligned_access_emulated()). + */ + req.enable = false; + req.error = 0; + on_each_cpu(cpu_unaligned_sbi_request_delegation, &req, 1); + if (req.error) + panic("Failed to disable misaligned delegation for all CPUs\n"); + +} + +void unaligned_access_init(void) +{ + if (sbi_probe_extension(SBI_EXT_FWFT) > 0) + unaligned_sbi_request_delegation(); +} +#else +void unaligned_access_init(void) {} +#endif diff --git a/arch/riscv/kernel/unaligned_access_speed.c b/arch/riscv/kernel/unaligned_access_speed.c index 91f189cf1611..1e3166100837 100644 --- a/arch/riscv/kernel/unaligned_access_speed.c +++ b/arch/riscv/kernel/unaligned_access_speed.c @@ -403,6 +403,8 @@ static int check_unaligned_access_all_cpus(void) { bool all_cpus_emulated, all_cpus_vec_unsupported; + unaligned_access_init(); + all_cpus_emulated = check_unaligned_access_emulated_all_cpus(); all_cpus_vec_unsupported = check_vector_unaligned_access_emulated_all_cpus();