From patchwork Tue Apr 30 08:42:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nylon Chen X-Patchwork-Id: 1929386 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=A9PeXzA1; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=aHoA02FA; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VTDHd6d06z20fY for ; Tue, 30 Apr 2024 18:43:23 +1000 (AEST) 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: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:In-Reply-To:References: List-Owner; bh=IpnHR79y8LOu5KkqkAMhMaLCW1V4moKr49xySxMr/z0=; b=A9PeXzA1s9tztX NxLarFwPhSzC6Y1b6JblMfedpWg+kfquzBE5brVBp9KMNh0Qn6XO2e1d1kLUtwyL6RO46vW52eZWv WimAVV3MhDc6ytBeCEkuvHh0Fzd03j3uCay0OhBOsTkV6qNqLIt3k3KemeEWPeT+Nf46N3L1TDz26 Cs6iALr0Ec0i/UjsVuJEJ05Y/VlWEyN1q6xYUwbvo0DBiaKv49BxAeqNzdvtqchskKn8c3fMljvgy ctP+Ceu6HPJIgVvlJ9+yyxNQ22t0xSx3khBHORILmyRx/yMaTeQ79ZC9DQ1pW3OO//zPJxOr99l7Q MJKJ/lfrJQcoFRQHFhXA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s1j4u-00000005f9v-4BX2; Tue, 30 Apr 2024 08:43:09 +0000 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s1j4b-00000005f32-37fi for opensbi@lists.infradead.org; Tue, 30 Apr 2024 08:42:51 +0000 Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-5f80aa2d4a3so4425871a12.0 for ; Tue, 30 Apr 2024 01:42:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1714466567; x=1715071367; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=HwVziT8lRvWZo8btbKII34UdA0yayoxqA/aEMsQ16Os=; b=aHoA02FAzK0LI//UGneZebvIMmV/lD2QzjJT5kPxh84/1/IqutTP0unyHOESyOZAc5 0u1h3fHOGDogqp4pfpD+0mkOCeIDgWB6s8GGNGN1t9ygRQv5mrgXtYV30MD4v4Epcaij 2hPMgZC07XuK7j28G9uKaP5TF8X939BPp5H9XhBi4cfqw3g42ofdKgPxU7ZYk5G6o0/W FVr4syD8FLWMataqw8uqyXquHVhWoU9OB/JX84r+VFBJWcLOTPE50YuyCuvp8Zdp5r3i oWhnpQ8p9aPflK5z6LE92wTN4jWjmKjJF301p5+P4chhhEymOozwl7+GKtIw1AvqSDgN UczA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714466567; x=1715071367; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=HwVziT8lRvWZo8btbKII34UdA0yayoxqA/aEMsQ16Os=; b=CN/XiPEIV+DQL3vm6dYC+IyK6k6MdU3/MSGhWh/mOq5A2jIBTlHyuK+NiAaiL09pUV Qrp58a3/EwXV2e+M7Dc6q7XqZt6TlBdbkkQ8zFP0TDAP1xw5uCQDlSnCLDyqSW/zOASG iXNsVYPfDPgLAUhOra0XsgZlC2f/EojPNg5Qt6+c/zlFPeJFH18LSyKV2nHfKo4N8g3h a29FG4mQceyFkge7TXwuJegXCLcM3HSBIzEZcIvScyul2d3gTJ+RAicCKRdm0M8BUbi0 r0CiUSgbwwfplQI1NAUB1i87+4fP9srVl7BstX7hPxKwM/m5V7US7v9yqcNzNuz7f137 H1IA== X-Gm-Message-State: AOJu0YxleldGZ+KaXTWVp6+KYwjNzlaYM+b8kEHFDFrFYZsp/1QJGuxE 1hSw9h2YMxC3M62eaviZF1+p6j4NvAFeVGstHAG/7SIzpbKHZmdGbF6KTib66VF5Vg3MMa+XRvi azZicts+H+subyVdATDbOeBH5guUoOMkczCqsCqP5X6/6RmMmTaE64SxU+Lh3UXV9HxnJclDEa6 /b4BFNG7kD3N18pUSv5LS6XXhvyEYNSu9zx6s0sYYMx6UbxA== X-Google-Smtp-Source: AGHT+IEsqEJ5+HEm5cVhHLewrPFYjQX8Gmg0UC+1VXD+yP+lKacmIzWw+KuArIJydL30wEctpz3NFQ== X-Received: by 2002:a05:6a20:4c2f:b0:1a9:a32d:17cc with SMTP id fm47-20020a056a204c2f00b001a9a32d17ccmr11553803pzb.18.1714466566671; Tue, 30 Apr 2024 01:42:46 -0700 (PDT) Received: from hsinchu35-syssw01.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id y8-20020a17090264c800b001e264b50964sm866565pli.205.2024.04.30.01.42.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 01:42:46 -0700 (PDT) From: Nylon Chen To: opensbi@lists.infradead.org Cc: zong.li@sifive.com, nylon7717@gmail.com, Nylon Chen , Greentime Hu , Yong-Xuan Wang Subject: [PATCH 1/1] lib: sbi: Add Smrnmi extension detection Date: Tue, 30 Apr 2024 16:42:41 +0800 Message-Id: <20240430084241.2009259-1-nylon.chen@sifive.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240430_014249_871520_1B56ACFA X-CRM114-Status: GOOD ( 13.40 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Adds detection and initialization of the Smrnmi extension in the SBI hart initialization code. The Smrnmi extension provides resumable non-maskable interrupts (RNMIs) support. It adds new CSRs (mnscratch, mnepc, mncause, mnstatus) to save the interrupted context and a new instruction MNRET to r [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 SPF_HELO_NONE SPF: HELO does not publish an 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 Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:533 listed in] [list.dnswl.org] X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Adds detection and initialization of the Smrnmi extension in the SBI hart initialization code. The Smrnmi extension provides resumable non-maskable interrupts (RNMIs) support. It adds new CSRs (mnscratch, mnepc, mncause, mnstatus) to save the interrupted context and a new instruction MNRET to resume from the RNMI handler[1]. we need to detect its presence and enable it in the early hart initialization process, before any traps. [1] https://github.com/riscv/riscv-isa-manual/blob/main/src/rnmi.adoc Co-developed-by: Zong Li Signed-off-by: Zong Li Signed-off-by: Nylon Chen Reviewed-by: Greentime Hu Reviewed-by: Yong-Xuan Wang --- include/sbi/riscv_encoding.h | 10 ++++++++++ include/sbi/sbi_hart.h | 2 ++ lib/sbi/sbi_hart.c | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/include/sbi/riscv_encoding.h b/include/sbi/riscv_encoding.h index d914828..320bb2b 100644 --- a/include/sbi/riscv_encoding.h +++ b/include/sbi/riscv_encoding.h @@ -207,6 +207,10 @@ #endif +#define MNSTATUS_NMIE (_UL(0x8)) +#define MNSTATUS_MNPV (_UL(0x80)) +#define MNSTATUS_MNPP (_UL(0x1800)) + #define MHPMEVENT_SSCOF_MASK _ULL(0xFFFF000000000000) #define ENVCFG_STCE (_ULL(1) << 63) @@ -738,6 +742,12 @@ #define CSR_MSTATEEN3 0x30F #define CSR_MSTATEEN3H 0x31F +/* Smrnmi extension registers */ +#define CSR_MNSCRATCH 0x740 +#define CSR_MNEPC 0x741 +#define CSR_MNCAUSE 0x742 +#define CSR_MNSTATUS 0x744 + /* Machine-Level High-Half CSRs (AIA) */ #define CSR_MIDELEGH 0x313 #define CSR_MIEH 0x314 diff --git a/include/sbi/sbi_hart.h b/include/sbi/sbi_hart.h index cc78eec..d49d987 100644 --- a/include/sbi/sbi_hart.h +++ b/include/sbi/sbi_hart.h @@ -63,6 +63,8 @@ enum sbi_hart_extensions { SBI_HART_EXT_SSCSRIND, /** Hart has Ssccfg extension */ SBI_HART_EXT_SSCCFG, + /** Hart has Smrnmi extension */ + SBI_HART_EXT_SMRNMI, /** Maximum index of Hart extension */ SBI_HART_EXT_MAX, diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c index 0e7b5d1..6941a94 100644 --- a/lib/sbi/sbi_hart.c +++ b/lib/sbi/sbi_hart.c @@ -668,6 +668,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = { __SBI_HART_EXT_DATA(smcdeleg, SBI_HART_EXT_SMCDELEG), __SBI_HART_EXT_DATA(sscsrind, SBI_HART_EXT_SSCSRIND), __SBI_HART_EXT_DATA(ssccfg, SBI_HART_EXT_SSCCFG), + __SBI_HART_EXT_DATA(smrnmi, SBI_HART_EXT_SMRNMI), }; _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext), @@ -706,6 +707,20 @@ void sbi_hart_get_extensions_str(struct sbi_scratch *scratch, sbi_strncpy(extensions_str, "none", nestr); } +static int hart_smrnmi_get_allowed(void) +{ + unsigned long val; + struct sbi_trap_info trap = {0}; + + val = csr_read_allowed(CSR_MNSTATUS, (unsigned long)&trap); + if (!trap.cause) { + val |= MNSTATUS_NMIE; + csr_write(CSR_MNSTATUS, val); + return true; + } else { + return false; + } + static unsigned long hart_pmp_get_allowed_addr(void) { unsigned long val = 0; @@ -775,6 +790,13 @@ static int hart_detect_features(struct sbi_scratch *scratch) hfeatures->mhpm_mask = 0; hfeatures->priv_version = SBI_HART_PRIV_VER_UNKNOWN; + /* + * Detect Resumable Non-Maskable Interrupts + * If it exists we must enable it before all traps. + */ + if (hart_smrnmi_get_allowed()) + __sbi_hart_update_extension(hfeatures, SBI_HART_EXT_SMRNMI, true); + #define __check_hpm_csr(__csr, __mask) \ oldval = csr_read_allowed(__csr, (ulong)&trap); \ if (!trap.cause) { \