Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp120901lqa; Fri, 26 Apr 2024 17:06:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU+YVgFsV3m6O+ylSAkwjuvuKcZrmJ5XDRTzxXM5VsgDuvdjC9H5nfzp9LEH0OIpWWzT6MvPF8kHJSXtQqBCiwY8c8pfrZyZuHa586LWQ== X-Google-Smtp-Source: AGHT+IEoSEdKvc7DM9+7hN6AD6yQVBQXXZ2homMeZGBXiyw/RvMmxtQ2iF5EucyA+LxfX4jyIeTR X-Received: by 2002:a05:6402:50c9:b0:572:6aaf:e0d0 with SMTP id h9-20020a05640250c900b005726aafe0d0mr477265edb.40.1714176417660; Fri, 26 Apr 2024 17:06:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714176417; cv=pass; d=google.com; s=arc-20160816; b=wbLu+44l95gr9zrH6myl3+/SNZQvSaD0fuKnNxLERpQXAzLqlindrOW0/EU+D71bTR gZgW91xRVBYE9iz9Q9oCBjwLESlUJqLV5x/g3yojNcd8vlx7o9AIus/D9gq1N6LdalAE m9r677Mnnt1ai/tjI719uwMjqPETQxd/nAdbIk8rrTP7PWrwPAokR3/xnNuLmBtgXcCf oBd2gqw7qLwOzlRaGHpJfqv4noPGk4FVhLOs8UyidadEKdKeoGvPWUzgHq2Irar7yOie 3reYXJcUNg+6Qk6OXRNqBz4fdRYLoDRvQ+a5zluPSxbM4jAg3jmImOdzNwYctPPvRpPJ 5t0g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=9nKgoSM0sp6sf/g9BAlOnduSP6+NwHasqsoAKKtprFI=; fh=/5cT4GpcNH1BqfnzIbRYTqucPAHcflqkXELanfwonCk=; b=UYMFGefxX57DBITY3rYew4e63dprlwPNci+cSYblpFyrsCTlm0flHhghzx3mT9dziZ Ql4iLAHZ1Jejpb9ALrzqke0sn5uCY18SBKTzcwqpfD/rlQspKbvav8atEo4oClBvqqVd nC9+usUEnJAx2GK01io6ovgc+pI8mOR23qdezSU/2Hdk7o8GMhM63Zzsd7etgOsntq1M vU/OOvzpLrry91JGIciAIgH4UjuKY3On1r5Qj/wKrOceb1kvsNBDxpOjM3bxxCeRzamW 6j+1ShXCucV+XgwID4l20CCr4Nk1OUmPKG3WkJatqp1yuA9v8Ft7z6d3Y+/eYYFnK3fk JI7w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kvZtbYD3; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-160798-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-160798-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id i41-20020a0564020f2900b005726d6243a8si51261eda.222.2024.04.26.17.06.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Apr 2024 17:06:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-160798-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kvZtbYD3; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-160798-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-160798-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DED9E1F261A7 for ; Sat, 27 Apr 2024 00:01:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 50F0B3D552; Sat, 27 Apr 2024 00:00:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="kvZtbYD3" Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97E3C524B1 for ; Sat, 27 Apr 2024 00:00:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714176004; cv=none; b=PdRY7pAm/mW2w0xIhJgrexlHWbY3EdAJI1EGI260QGr2NXuO6EPBtfUq54bq1LBgq7NQRAkaHQfMNcg1UOvUvO6B3s2eTqpA4R63+8zArthMpc1VxJJPN+AnuhqO2CUTatd4Hv/5Xi37mYCNE0arBuGpFsmYeixykNbZr1qSu5s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714176004; c=relaxed/simple; bh=kAXwkZ5ourxONL+nhOJPUSgmn75/9GEyN9evhXr19ow=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=NPOfXXxRw9qbtJzLplwm0SD/f7jVBd8RNbxW8FGvQVDgodE8grg7HvtcnZeMSXCY7Y859wYFg6krHX1M9Do4zIBxa6KLZRY4nFtLB2vXGAc+geZCZ33n33LNgdvb6I79hSG8l76KlU1yyfev8vstMJuiSHuNYK4usPuf7J8+nC8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=kvZtbYD3; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-6ed691fb83eso2354028b3a.1 for ; Fri, 26 Apr 2024 17:00:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1714176002; x=1714780802; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=9nKgoSM0sp6sf/g9BAlOnduSP6+NwHasqsoAKKtprFI=; b=kvZtbYD3keS8bxlHi4+1/9rUYwKgsdn7sMgzNvbAZH+2gxZ4yr8GorvGUXCFkXTk1x hCO7b+OoBIrMnKM0UPwteF5mNo53i8rw3mMEYSGp7KgW41BE9KIDRGXBN2ni4kmq3lVm Ww0PHnHbq3R8Fz38EJQ111dAgbBw532YlTOK14OdoT331X44l5eaoKrPO12p4D9vKAfl R1QOgTLHtGXBAy8TAX+CtGrP/0FRICkrNJRaVsXu0SyBP/yzgFGTq03Hw6o+ymjRgEvo lVXK/ljnO+UDZAymPQjojU2sfhpSNVOhBKJtFwEfK2lg2LUfUOTRrKAY/tbViAXalnm9 Ft3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714176002; x=1714780802; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9nKgoSM0sp6sf/g9BAlOnduSP6+NwHasqsoAKKtprFI=; b=M3PktwvuBAZ3XNnvic0ZdxfpsB4K0YQVr5/qg8pTgTBwM1VNaHQKb1V5KO5Cvw4xjB PC89LPZ2URZ5movdpGhpEYp4Ik90/YCMc6swazeB/Mc/O6Y3IrkP5n9u8Yqh4BkeASzj jo7Qmh2dzDDd1eJLaoNY8SbScnkRqHiLj06PnAz8Bm4whDL/SXHy4BPn1Fk5KGkKne36 ApvnD6fWY9lXaVUrnRSQS7D/+7lUx2aUBxnrkTZiGgl01FcmfgAKvsXTM42JTVX1aTtX rpqYC3VDaOhLLnqmxuijjiwY/Wx5OaB49brsLkbuHC3SqpNAPFOFdgxI5xArbjNfcdaI 0tpA== X-Forwarded-Encrypted: i=1; AJvYcCUf1F8E8o6Z68xaGRoTr6R9l0kwjEurjsy8c7clyMm3dEqSZLwKFQ62+5XlQzN7VEyYYUttwPVvH/Y1dSqpjT8bO0QO/XJ0tj+6HGdy X-Gm-Message-State: AOJu0YyGUCiXp07gJt++my/95htz21EBmfJb9O/85zQnwUs89/BeroL7 wno8J1TSqzQph5m2/3IVWOjJOUWjsS+QPNNCi+MWSzIGsasVPJj8CE3AjrgqxsY= X-Received: by 2002:a05:6a00:1395:b0:6ed:416d:e9a with SMTP id t21-20020a056a00139500b006ed416d0e9amr5962735pfg.7.1714176001896; Fri, 26 Apr 2024 17:00:01 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b12-20020a056a00114c00b006ecfc3a8d6csm15313819pfm.124.2024.04.26.17.00.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Apr 2024 17:00:01 -0700 (PDT) Date: Fri, 26 Apr 2024 16:59:59 -0700 From: Deepak Gupta To: =?iso-8859-1?Q?Cl=E9ment_L=E9ger?= Cc: Conor Dooley , Rob Herring , Krzysztof Kozlowski , Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Atish Patra , linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Ved Shanbhogue Subject: Re: [RFC PATCH 5/7] riscv: add double trap driver Message-ID: References: <20240418142701.1493091-1-cleger@rivosinc.com> <20240418142701.1493091-6-cleger@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20240418142701.1493091-6-cleger@rivosinc.com> On Thu, Apr 18, 2024 at 04:26:44PM +0200, Cl?ment L?ger wrote: >Add a small driver to request double trap enabling as well as >registering a SSE handler for double trap. This will also be used by KVM >SBI FWFT extension support to detect if it is possible to enable double >trap in VS-mode. > >Signed-off-by: Cl?ment L?ger >--- > arch/riscv/include/asm/sbi.h | 1 + > drivers/firmware/Kconfig | 7 +++ > drivers/firmware/Makefile | 1 + > drivers/firmware/riscv_dbltrp.c | 95 +++++++++++++++++++++++++++++++++ > include/linux/riscv_dbltrp.h | 19 +++++++ > 5 files changed, 123 insertions(+) > create mode 100644 drivers/firmware/riscv_dbltrp.c > create mode 100644 include/linux/riscv_dbltrp.h > >diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h >index 744aa1796c92..9cd4ca66487c 100644 >--- a/arch/riscv/include/asm/sbi.h >+++ b/arch/riscv/include/asm/sbi.h >@@ -314,6 +314,7 @@ enum sbi_sse_attr_id { > #define SBI_SSE_ATTR_INTERRUPTED_FLAGS_SPIE (1 << 2) > > #define SBI_SSE_EVENT_LOCAL_RAS 0x00000000 >+#define SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP 0x00000001 > #define SBI_SSE_EVENT_GLOBAL_RAS 0x00008000 > #define SBI_SSE_EVENT_LOCAL_PMU 0x00010000 > #define SBI_SSE_EVENT_LOCAL_SOFTWARE 0xffff0000 >diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig >index 59f611288807..a037f6e89942 100644 >--- a/drivers/firmware/Kconfig >+++ b/drivers/firmware/Kconfig >@@ -197,6 +197,13 @@ config RISCV_SSE_TEST > Select if you want to enable SSE extension testing at boot time. > This will run a series of test which verifies SSE sanity. > >+config RISCV_DBLTRP >+ bool "Enable Double trap handling" >+ depends on RISCV_SSE && RISCV_SBI >+ default n >+ help >+ Select if you want to enable SSE double trap handler. >+ > config SYSFB > bool > select BOOT_VESA_SUPPORT >diff --git a/drivers/firmware/Makefile b/drivers/firmware/Makefile >index fb7b0c08c56d..ad67a1738c0f 100644 >--- a/drivers/firmware/Makefile >+++ b/drivers/firmware/Makefile >@@ -18,6 +18,7 @@ obj-$(CONFIG_RASPBERRYPI_FIRMWARE) += raspberrypi.o > obj-$(CONFIG_FW_CFG_SYSFS) += qemu_fw_cfg.o > obj-$(CONFIG_RISCV_SSE) += riscv_sse.o > obj-$(CONFIG_RISCV_SSE_TEST) += riscv_sse_test.o >+obj-$(CONFIG_RISCV_DBLTRP) += riscv_dbltrp.o > obj-$(CONFIG_SYSFB) += sysfb.o > obj-$(CONFIG_SYSFB_SIMPLEFB) += sysfb_simplefb.o > obj-$(CONFIG_TI_SCI_PROTOCOL) += ti_sci.o >diff --git a/drivers/firmware/riscv_dbltrp.c b/drivers/firmware/riscv_dbltrp.c >new file mode 100644 >index 000000000000..72f9a067e87a >--- /dev/null >+++ b/drivers/firmware/riscv_dbltrp.c >@@ -0,0 +1,95 @@ >+// SPDX-License-Identifier: GPL-2.0-only >+/* >+ * Copyright (C) 2023 Rivos Inc. >+ */ nit: fix copyright year >+ >+#define pr_fmt(fmt) "riscv-dbltrp: " fmt >+ >+#include >+#include >+#include >+#include >+ >+#include >+ >+static bool double_trap_enabled; >+ >+static int riscv_sse_dbltrp_handle(uint32_t evt, void *arg, >+ struct pt_regs *regs) >+{ >+ __show_regs(regs); >+ panic("Double trap !\n"); >+ >+ return 0; Curious: Does panic return? What's the point of returning from here? >+} >+ >+struct cpu_dbltrp_data { >+ int error; >+}; >+ >+static void >+sbi_cpu_enable_double_trap(void *data) >+{ >+ struct sbiret ret; >+ struct cpu_dbltrp_data *cdd = data; >+ >+ ret = sbi_ecall(SBI_EXT_FWFT, SBI_EXT_FWFT_SET, >+ SBI_FWFT_DOUBLE_TRAP_ENABLE, 1, 0, 0, 0, 0); >+ >+ if (ret.error) { >+ cdd->error = 1; >+ pr_err("Failed to enable double trap on cpu %d\n", smp_processor_id()); >+ } >+} >+ >+static int sbi_enable_double_trap(void) >+{ >+ struct cpu_dbltrp_data cdd = {0}; >+ >+ on_each_cpu(sbi_cpu_enable_double_trap, &cdd, 1); >+ if (cdd.error) >+ return -1; There is a bug here. If `sbi_cpu_enable_double_trap` failed on all cpus but last cpu. Then cdd.error would not record error and will be reflect as if double trap was enabled. Its less likely to happen that FW would return success for one cpu and fail for others. But there is non-zero probablity here. >+ >+ double_trap_enabled = true; >+ >+ return 0; >+} >+ >+bool riscv_double_trap_enabled(void) >+{ >+ return double_trap_enabled; >+} >+EXPORT_SYMBOL(riscv_double_trap_enabled); >+ >+static int __init riscv_dbltrp(void) >+{ >+ struct sse_event *evt; >+ >+ if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_SSDBLTRP)) { >+ pr_err("Ssdbltrp extension not available\n"); >+ return 1; >+ } >+ >+ if (!sbi_probe_extension(SBI_EXT_FWFT)) { >+ pr_err("Can not enable double trap, SBI_EXT_FWFT is not available\n"); >+ return 1; >+ } >+ >+ if (sbi_enable_double_trap()) { >+ pr_err("Failed to enable double trap on all cpus\n"); >+ return 1; >+ } >+ >+ evt = sse_event_register(SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP, 0, >+ riscv_sse_dbltrp_handle, NULL); >+ if (IS_ERR(evt)) { >+ pr_err("SSE double trap register failed\n"); >+ return PTR_ERR(evt); >+ } >+ >+ sse_event_enable(evt); >+ pr_info("Double trap handling registered\n"); >+ >+ return 0; >+} >+device_initcall(riscv_dbltrp); >diff --git a/include/linux/riscv_dbltrp.h b/include/linux/riscv_dbltrp.h >new file mode 100644 >index 000000000000..6de4f43fae6b >--- /dev/null >+++ b/include/linux/riscv_dbltrp.h >@@ -0,0 +1,19 @@ >+/* SPDX-License-Identifier: GPL-2.0 */ >+/* >+ * Copyright (C) 2023 Rivos Inc. >+ */ >+ >+#ifndef __LINUX_RISCV_DBLTRP_H >+#define __LINUX_RISCV_DBLTRP_H >+ >+#if defined(CONFIG_RISCV_DBLTRP) >+bool riscv_double_trap_enabled(void); >+#else >+ >+static inline bool riscv_double_trap_enabled(void) >+{ >+ return false; >+} >+#endif >+ >+#endif /* __LINUX_RISCV_DBLTRP_H */ >-- >2.43.0 > >