From patchwork Tue Dec 12 08:58:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yong-Xuan Wang X-Patchwork-Id: 1874983 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=YUrDaiYt; 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=idMG4BQO; 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 4SqCGC6P8xz20H5 for ; Tue, 12 Dec 2023 19:58:59 +1100 (AEDT) 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:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id: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=4sZfQA1w8c6nTGOp4VbETxy6bvJsV8fWhefM0Plvuus=; b=YUrDaiYtufEze0 zrzwaQR1etzC1SG37OZl2dOGrMoPrOXQ59ONDcaVuCrgJutA3EUFaAXVM8V+XmBXWLyMAOBj72Zog YvEwO8vQeSyH4UPXcf61J+aOIarykDWFftLmWshj2TrJV5SFCPwQdHExmz3TObnWlC1Yx3lektcBz rBEU+jQMo5jcM3jPSVcxaVypQTosNlSeWNbrr7cbcBPANaLTY83ACftmP9wBReJV2Hy5gxyjR83dc 8eY/W7xLzfT05Q4wC84YW1sJgZ4V//OBiFBjwuHnZEeE+mOi3UebKtd5zZyrMYRHif+sAA66D8qBh pWWsD6q2/Wl21nEy4YBg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rCybI-00B78J-1i; Tue, 12 Dec 2023 08:58:48 +0000 Received: from mail-oi1-x236.google.com ([2607:f8b0:4864:20::236]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rCybE-00B76Z-2h for opensbi@lists.infradead.org; Tue, 12 Dec 2023 08:58:46 +0000 Received: by mail-oi1-x236.google.com with SMTP id 5614622812f47-3b9b90f8708so3547912b6e.2 for ; Tue, 12 Dec 2023 00:58:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1702371522; x=1702976322; darn=lists.infradead.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=UINwaXihbsEvKNCn6hzrSA6yk/u3WWwkYYYOmEleX0Y=; b=idMG4BQOd6Xyx4MaxaqMkGaAEdiByQG2uCyP/Qz8yFzorQJ2//Sk6vKtHInbTja7D/ 4uJh+8V728O1u9pwbj6ieWzJ+DToei+92d3CQMF59l50I7CTQTRjC6x6iVymch70oUSB zwkZWGYEvrARhTujQ2EMN1fRw/eHc1LK8ayo9+lM64lXpV/7eDB6PTV7DOw4a8/KdJIv CcT1HqwKaLaBjqpGi8ia31mf7hggqCxCZDJamCw+5sxkwYgxwBwsPNfyDf0dwHRqJdaa oZQiI6H4UOgAhYZZtZ3qiCS+7VMPJUM0WEaK+3Gg9ge+meKYRmi26IfLUPNlP4G5CgGl /GYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702371522; x=1702976322; h=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=UINwaXihbsEvKNCn6hzrSA6yk/u3WWwkYYYOmEleX0Y=; b=tfeVl6zzKIm3DhCl7uqyuTa7ou1oUFHRj34JIpg3SRcN83rmwJ3VXAkBVbyrA7bsPN jR7djHxzm+q05ipkQodeiQrBtbL27i8YqQHpMEg0iFdrNJIO+lVKHlN7HdTeZrMnO8Ep bxFS74N46UN07fctcA67okJaeFFxUKmGQeI9GUgeWyIVBwAeoRXWx/6TEEbP5joKSdKv 2YHjuI+mE229PMJ86Aof16KsdyuVBmtZC/5aNSPH1bEwJHvYY+kj+yGLgbj3AoCPkUrZ VOwXSz21xOou2XbLqp449N287CLv7k07LSfkQyORxHK5GhDpn63JlFQ2WRYDIaNiO1RP FFhA== X-Gm-Message-State: AOJu0Yyrtk247XxT+8HPD8D0q/2GEkSScbdmODiDnuHd+uUnYEtxBtx8 oMVqO2vbzg+10th76j2hxsFAhYXTafBdslgC3bKd7EBMM/pEeDPmWsbXZhdDio+iSodDbNgCytd mcz4bQzT/FZlHNn+McyqiNIm9H6IbVaxIaOQ4NcjK8i9T+/8lzgxAdIoxxyqMFLMH6QziR8lLB9 cOm4611liGDSZug3wNKQ== X-Google-Smtp-Source: AGHT+IGjlPt2UU/5zq0+arZPDXmtiCOfLsoxEQPzbHsFjLQ9wC7U1u1LCdOrtgtS7g9yizEgqFrpPA== X-Received: by 2002:a05:6808:2f10:b0:3b9:de62:2baa with SMTP id gu16-20020a0568082f1000b003b9de622baamr7696179oib.20.1702371522203; Tue, 12 Dec 2023 00:58:42 -0800 (PST) Received: from hsinchu26.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id f26-20020aa79d9a000000b006cb94825843sm7610830pfq.180.2023.12.12.00.58.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 00:58:41 -0800 (PST) From: Yong-Xuan Wang To: opensbi@lists.infradead.org Cc: greentime.hu@sifive.com, vincent.chen@sifive.com, Yong-Xuan Wang Subject: [PATCH v3 1/4] lib: sbi: Improve the code of privilege mode and extensions detection Date: Tue, 12 Dec 2023 08:58:32 +0000 Message-Id: <20231212085835.26824-2-yongxuan.wang@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231212085835.26824-1-yongxuan.wang@sifive.com> References: <20231212085835.26824-1-yongxuan.wang@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231212_005844_872284_DDB13F2A X-CRM114-Status: GOOD ( 10.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: We can enhance the code by creating 2 unified interface with macro for privilege mode and extensions detection, which relies on supported privilege modes and CSRs. Signed-off-by: Yong-Xuan Wang Reviewed-by: Anup Patel --- lib/sbi/sbi_hart.c | 88 ++++++++++++++++++++ 1 file changed, 38 ins [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:236 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 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: , MIME-Version: 1.0 Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org We can enhance the code by creating 2 unified interface with macro for privilege mode and extensions detection, which relies on supported privilege modes and CSRs. Signed-off-by: Yong-Xuan Wang Reviewed-by: Anup Patel --- lib/sbi/sbi_hart.c | 88 ++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 50 deletions(-) diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c index 6acff378..13998f1c 100644 --- a/lib/sbi/sbi_hart.c +++ b/lib/sbi/sbi_hart.c @@ -807,6 +807,7 @@ static int hart_detect_features(struct sbi_scratch *scratch) sbi_memset(hfeatures->extensions, 0, sizeof(hfeatures->extensions)); hfeatures->pmp_count = 0; hfeatures->mhpm_mask = 0; + hfeatures->priv_version = SBI_HART_PRIV_VER_UNKNOWN; #define __check_hpm_csr(__csr, __mask) \ oldval = csr_read_allowed(__csr, (ulong)&trap); \ @@ -899,67 +900,54 @@ __pmp_skip: #undef __check_csr_2 #undef __check_csr - /* Detect if hart supports Priv v1.10 */ - val = csr_read_allowed(CSR_MCOUNTEREN, (unsigned long)&trap); - if (!trap.cause) - hfeatures->priv_version = SBI_HART_PRIV_VER_1_10; - /* Detect if hart supports Priv v1.11 */ - val = csr_read_allowed(CSR_MCOUNTINHIBIT, (unsigned long)&trap); - if (!trap.cause && - (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_10)) - hfeatures->priv_version = SBI_HART_PRIV_VER_1_11; +#define __check_priv(__csr, __base_priv, __priv) \ + val = csr_read_allowed(__csr, (ulong)&trap); \ + if (!trap.cause && (hfeatures->priv_version >= __base_priv)) { \ + hfeatures->priv_version = __priv; \ + } + /* Detect if hart supports Priv v1.10 */ + __check_priv(CSR_MCOUNTEREN, + SBI_HART_PRIV_VER_UNKNOWN, SBI_HART_PRIV_VER_1_10); + /* Detect if hart supports Priv v1.11 */ + __check_priv(CSR_MCOUNTINHIBIT, + SBI_HART_PRIV_VER_1_10, SBI_HART_PRIV_VER_1_11); /* Detect if hart supports Priv v1.12 */ - csr_read_allowed(CSR_MENVCFG, (unsigned long)&trap); - if (!trap.cause && - (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_11)) - hfeatures->priv_version = SBI_HART_PRIV_VER_1_12; + __check_priv(CSR_MENVCFG, + SBI_HART_PRIV_VER_1_11, SBI_HART_PRIV_VER_1_12); - /* Counter overflow/filtering is not useful without mcounter/inhibit */ - if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_11) { - /* Detect if hart supports sscofpmf */ - csr_read_allowed(CSR_SCOUNTOVF, (unsigned long)&trap); - if (!trap.cause) - __sbi_hart_update_extension(hfeatures, - SBI_HART_EXT_SSCOFPMF, true); +#undef __check_priv_csr + +#define __check_ext_csr(__base_priv, __csr, __ext) \ + if (hfeatures->priv_version >= __base_priv) { \ + csr_read_allowed(__csr, (ulong)&trap); \ + if (!trap.cause) \ + __sbi_hart_update_extension(hfeatures, \ + __ext, true); \ } + /* Counter overflow/filtering is not useful without mcounter/inhibit */ + /* Detect if hart supports sscofpmf */ + __check_ext_csr(SBI_HART_PRIV_VER_1_11, + CSR_SCOUNTOVF, SBI_HART_EXT_SSCOFPMF); /* Detect if hart supports time CSR */ - csr_read_allowed(CSR_TIME, (unsigned long)&trap); - if (!trap.cause) - __sbi_hart_update_extension(hfeatures, - SBI_HART_EXT_ZICNTR, true); - + __check_ext_csr(SBI_HART_PRIV_VER_UNKNOWN, + CSR_TIME, SBI_HART_EXT_ZICNTR); /* Detect if hart has AIA local interrupt CSRs */ - csr_read_allowed(CSR_MTOPI, (unsigned long)&trap); - if (!trap.cause) - __sbi_hart_update_extension(hfeatures, - SBI_HART_EXT_SMAIA, true); - + __check_ext_csr(SBI_HART_PRIV_VER_UNKNOWN, + CSR_MTOPI, SBI_HART_EXT_SMAIA); /* Detect if hart supports stimecmp CSR(Sstc extension) */ - if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_12) { - csr_read_allowed(CSR_STIMECMP, (unsigned long)&trap); - if (!trap.cause) - __sbi_hart_update_extension(hfeatures, - SBI_HART_EXT_SSTC, true); - } - + __check_ext_csr(SBI_HART_PRIV_VER_1_12, + CSR_STIMECMP, SBI_HART_EXT_SSTC); /* Detect if hart supports mstateen CSRs */ - if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_12) { - val = csr_read_allowed(CSR_MSTATEEN0, (unsigned long)&trap); - if (!trap.cause) - __sbi_hart_update_extension(hfeatures, - SBI_HART_EXT_SMSTATEEN, true); - } - + __check_ext_csr(SBI_HART_PRIV_VER_1_12, + CSR_MSTATEEN0, SBI_HART_EXT_SMSTATEEN); /* Detect if hart supports smcntrpmf */ - if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_12) { - csr_read_allowed(CSR_MCYCLECFG, (unsigned long)&trap); - if (!trap.cause) - __sbi_hart_update_extension(hfeatures, - SBI_HART_EXT_SMCNTRPMF, true); - } + __check_ext_csr(SBI_HART_PRIV_VER_1_12, + CSR_MCYCLECFG, SBI_HART_EXT_SMCNTRPMF); + +#undef __check_ext_csr /* Let platform populate extensions */ rc = sbi_platform_extensions_init(sbi_platform_thishart_ptr(),