From patchwork Mon Jun 10 04:34:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 13691453 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 9C308C27C5F for ; Mon, 10 Jun 2024 04:34:52 +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:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=MiVZIO/A2urFZy4rLwzF2hduVWRvgJukeTil9ej9sWs=; b=Ut7QMpYuu/tzSn gtzakCrzrMPNrQpRlahu/WHaTekuAh0YJnOczEfYERGb2rtidyOz3RvhcyPZJ9pkmMIshAJPC9Ow9 wa8mKgVJsQLtn7N8WMGSUls71YKMl4MMJDbSp1Yq+r62qlLgseo73QW3j53Kws+mYeOTip1kWXK92 qK1tD2R1tliqOZtTDLoANeQQfFpDxOzSb57F1XmNmVg+7hcr/pK9UR1nR6j90fSghvzH4xZuvVy5e yx3n1O4vVWo/MVe0LtOxMuWInn1nDUnY76WSUspsb4xv8RafjorCwb5pikag/nLoarnuHZUV5UJG0 N7IlOmrvOKaDxDq8tNZA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sGWk1-00000003lF2-2nE1; Mon, 10 Jun 2024 04:34:45 +0000 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sGWjj-00000003l9f-15qM for linux-riscv@lists.infradead.org; Mon, 10 Jun 2024 04:34:28 +0000 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-70423e8e6c9so1191643b3a.0 for ; Sun, 09 Jun 2024 21:34:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1717994066; x=1718598866; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=PRNaUe9j8adPvbr74Z0H2ivwUey2D0tGBGCSxwe2D/U=; b=3VWE6EJApD5UUaukfIrYTOrfPjAJT5MnAoeSxsnBab84C0OytyWoeMRdl2TQI6l/sA uV4aVssTx6b7PCQByZQRuA0YeqZxLU3ErMoAbNFMKhfQtZ8PxUmB7H3PjfdN51XFmkPY 8nQqi8FbJbG6S8nBs7F/szlaZPsOi1PanJbyH5td6FfoRq/6Gr8AklAjdVMZ9EzcY85u jTyP8qIYuj/quykTjcPuzzMMxlC7Il47SsdYoLpZNA9YEcQ99kgziLKl1Gu7x4zIMYg4 2o3BSulzd9HlA4wCyAIRdE7PU4ITocSY/FqmemCkrBRNGIVQQ1ZYHp6b8z+Ohkulrkkj Xe8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717994066; x=1718598866; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PRNaUe9j8adPvbr74Z0H2ivwUey2D0tGBGCSxwe2D/U=; b=nqDIrDs1V4ZmhXPDwW3DLflx1C239aN/keMMTjArHgawu5XvFk/+pynXvORsR64jJm I/Z9PKRetKghtbED4WaaPafF4+LKvN0Ic2mVoZdt4SGXtX6TeqSiW0HEsDuKNtvWWcLE UkcdNno6AI4NL8WksvxjfBSlIdesN2HNDgwnql8TRS8sqLvsk7LK8D9YQ5Osp6kPmHED bOxwSn2wZnBQV2dCULafbjrzT7xmBJQ5ejiTp6agjVgl5zofDLAGzOOlxxjHHSj+qDjL ixiJc4BKDu13PNxhdIFYIylFlzl+3fM9lIJ1PDO3g0C8hQ2MfeB1L+MPrik7sS4ZZNaN 2T7w== X-Gm-Message-State: AOJu0YyD4PrNvg+pymB6g4xDKMD8TFox6mjqUj+n8WBau5TC2L7gUqmy DQRDprgzfI5XFJd5RzqcT94L8mT/K/kvk4iWg/1ka13eHRP9nvNNcXIYTs+sA/HjDwkhrVDXwqI t X-Google-Smtp-Source: AGHT+IEHuGNZftr0P5W399Ll/GfktVfqQuiEwcOUZM4LeqClcuQW7FJP8H8Qw55t/rgWnRj61I4bRQ== X-Received: by 2002:a05:6a20:7349:b0:1b6:23ae:b295 with SMTP id adf61e73a8af0-1b623af0b89mr3453563637.38.1717994065826; Sun, 09 Jun 2024 21:34:25 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c2fe295cf5sm1894756a91.47.2024.06.09.21.34.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jun 2024 21:34:24 -0700 (PDT) From: Charlie Jenkins Date: Sun, 09 Jun 2024 21:34:17 -0700 Subject: [PATCH v2 4/4] riscv: cpufeature: Extract common elements from extension checking MIME-Version: 1.0 Message-Id: <20240609-support_vendor_extensions-v2-4-9a43f1fdcbb9@rivosinc.com> References: <20240609-support_vendor_extensions-v2-0-9a43f1fdcbb9@rivosinc.com> In-Reply-To: <20240609-support_vendor_extensions-v2-0-9a43f1fdcbb9@rivosinc.com> To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Evan Green , Andy Chiu Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Charlie Jenkins X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1717994057; l=5623; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=uPnFa7HgliYWkEhE+mMi6Koja++p2Kz63ysSy2dgA8g=; b=rpbJpMF89ukqLlHxGd6VYgyyx0i+/SKiuUESb5JSY6z3mso294tYGLGnKTBoowGZ1Hz3+sw33 LHEE+gItcHFCd1Pnb882zm1sWefh3cFF9NbIY4hmQZ2kNrKZiRJItSb X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240609_213427_436979_1E777BD4 X-CRM114-Status: GOOD ( 12.75 ) 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 The __riscv_has_extension_likely() and __riscv_has_extension_unlikely() functions from the vendor_extensions.h can be used to simplify the standard extension checking code as well. Migrate those functions to cpufeature.h and reorganize the code in the file to use the functions. Signed-off-by: Charlie Jenkins Reviewed-by: Conor Dooley Reviewed-by: Andy Chiu --- arch/riscv/include/asm/cpufeature.h | 78 +++++++++++++++++------------- arch/riscv/include/asm/vendor_extensions.h | 28 ----------- 2 files changed, 44 insertions(+), 62 deletions(-) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index 550d661dc78d..b029ca72cebc 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -96,59 +96,66 @@ extern bool riscv_isa_fallback; unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap); +#define STANDARD_EXT 0 + bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, unsigned int bit); #define riscv_isa_extension_available(isa_bitmap, ext) \ __riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_##ext) -static __always_inline bool -riscv_has_extension_likely(const unsigned long ext) +static __always_inline bool __riscv_has_extension_likely(const unsigned long vendor, + const unsigned long ext) { - compiletime_assert(ext < RISCV_ISA_EXT_MAX, - "ext must be < RISCV_ISA_EXT_MAX"); - - if (IS_ENABLED(CONFIG_RISCV_ALTERNATIVE)) { - asm goto( - ALTERNATIVE("j %l[l_no]", "nop", 0, %[ext], 1) - : - : [ext] "i" (ext) - : - : l_no); - } else { - if (!__riscv_isa_extension_available(NULL, ext)) - goto l_no; - } + asm goto(ALTERNATIVE("j %l[l_no]", "nop", %[vendor], %[ext], 1) + : + : [vendor] "i" (vendor), [ext] "i" (ext) + : + : l_no); return true; l_no: return false; } -static __always_inline bool -riscv_has_extension_unlikely(const unsigned long ext) +static __always_inline bool __riscv_has_extension_unlikely(const unsigned long vendor, + const unsigned long ext) { - compiletime_assert(ext < RISCV_ISA_EXT_MAX, - "ext must be < RISCV_ISA_EXT_MAX"); - - if (IS_ENABLED(CONFIG_RISCV_ALTERNATIVE)) { - asm goto( - ALTERNATIVE("nop", "j %l[l_yes]", 0, %[ext], 1) - : - : [ext] "i" (ext) - : - : l_yes); - } else { - if (__riscv_isa_extension_available(NULL, ext)) - goto l_yes; - } + asm goto(ALTERNATIVE("nop", "j %l[l_yes]", %[vendor], %[ext], 1) + : + : [vendor] "i" (vendor), [ext] "i" (ext) + : + : l_yes); return false; l_yes: return true; } +static __always_inline bool riscv_has_extension_unlikely(const unsigned long ext) +{ + compiletime_assert(ext < RISCV_ISA_EXT_MAX, "ext must be < RISCV_ISA_EXT_MAX"); + + if (IS_ENABLED(CONFIG_RISCV_ALTERNATIVE)) + return __riscv_has_extension_unlikely(STANDARD_EXT, ext); + + return __riscv_isa_extension_available(NULL, ext); +} + +static __always_inline bool riscv_has_extension_likely(const unsigned long ext) +{ + compiletime_assert(ext < RISCV_ISA_EXT_MAX, "ext must be < RISCV_ISA_EXT_MAX"); + + if (IS_ENABLED(CONFIG_RISCV_ALTERNATIVE)) + return __riscv_has_extension_likely(STANDARD_EXT, ext); + + return __riscv_isa_extension_available(NULL, ext); +} + static __always_inline bool riscv_cpu_has_extension_likely(int cpu, const unsigned long ext) { - if (IS_ENABLED(CONFIG_RISCV_ALTERNATIVE) && riscv_has_extension_likely(ext)) + compiletime_assert(ext < RISCV_ISA_EXT_MAX, "ext must be < RISCV_ISA_EXT_MAX"); + + if (IS_ENABLED(CONFIG_RISCV_ALTERNATIVE) && + __riscv_has_extension_likely(STANDARD_EXT, ext)) return true; return __riscv_isa_extension_available(hart_isa[cpu].isa, ext); @@ -156,7 +163,10 @@ static __always_inline bool riscv_cpu_has_extension_likely(int cpu, const unsign static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsigned long ext) { - if (IS_ENABLED(CONFIG_RISCV_ALTERNATIVE) && riscv_has_extension_unlikely(ext)) + compiletime_assert(ext < RISCV_ISA_EXT_MAX, "ext must be < RISCV_ISA_EXT_MAX"); + + if (IS_ENABLED(CONFIG_RISCV_ALTERNATIVE) && + __riscv_has_extension_unlikely(STANDARD_EXT, ext)) return true; return __riscv_isa_extension_available(hart_isa[cpu].isa, ext); diff --git a/arch/riscv/include/asm/vendor_extensions.h b/arch/riscv/include/asm/vendor_extensions.h index 04d72b02ae6b..7437304a71b9 100644 --- a/arch/riscv/include/asm/vendor_extensions.h +++ b/arch/riscv/include/asm/vendor_extensions.h @@ -48,34 +48,6 @@ bool __riscv_isa_vendor_extension_available(int cpu, unsigned long vendor, unsig __riscv_isa_vendor_extension_available(VENDOR_EXT_ALL_CPUS, vendor, \ RISCV_ISA_VENDOR_EXT_##ext) -static __always_inline bool __riscv_has_extension_likely(const unsigned long vendor, - const unsigned long ext) -{ - asm goto(ALTERNATIVE("j %l[l_no]", "nop", %[vendor], %[ext], 1) - : - : [vendor] "i" (vendor), [ext] "i" (ext) - : - : l_no); - - return true; -l_no: - return false; -} - -static __always_inline bool __riscv_has_extension_unlikely(const unsigned long vendor, - const unsigned long ext) -{ - asm goto(ALTERNATIVE("nop", "j %l[l_yes]", %[vendor], %[ext], 1) - : - : [vendor] "i" (vendor), [ext] "i" (ext) - : - : l_yes); - - return false; -l_yes: - return true; -} - static __always_inline bool riscv_has_vendor_extension_likely(const unsigned long vendor, const unsigned long ext) {