From nobody Mon Apr 20 01:12:25 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54D74C43334 for ; Thu, 23 Jun 2022 15:29:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231874AbiFWP35 (ORCPT ); Thu, 23 Jun 2022 11:29:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229863AbiFWP3z (ORCPT ); Thu, 23 Jun 2022 11:29:55 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4778B34B90 for ; Thu, 23 Jun 2022 08:29:54 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id cf14so19314126edb.8 for ; Thu, 23 Jun 2022 08:29:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LE1pCraTp8DcEIpT4Qu2iOgXxVGIQ+g+LxOeogNClfQ=; b=F3E+Nst+sXa2JZdRtZc9avEjwV6g9e4wk0xB4aRhWW/Se26ItykQO90DRspLEKVwA3 1d/EVtdyjjDFNGLRQ2IIwCmOecwFB4EEBLTQkcYKQz3k69gnU6cKqICSO8v51xkq8n1F NJdBVIp0LjiMcPqeJ83UnUUNygqSHVMgQJEcZz1SWpWuRrNkVR81XudwP6UmhdhdiOyN uhTWCB/NVovsb87mjw1z7c0+PV92obYAaR+4QNFivGNbkIxyLflbfRZNA3u0WUb+OFiF w4V3Vn7F0hwHi/Eswa+XHddBy6h1/t8yUN/9FskxHClhKq2w23qqe1C1+MZ1hAt13XG0 PjVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LE1pCraTp8DcEIpT4Qu2iOgXxVGIQ+g+LxOeogNClfQ=; b=H1nFMaWDDqOs0BCYDO9Z0Ae3i2jZH5+kz/PbTGUCrf1iFL/3SpghHq5DhMQqZ7jAc1 stPqvtglF/7xHWqZMdYG4qJzYczGOuvxW1n5VKCC8SbkBfLT3BteGpkyQ371SSCQ7ehX iSaDTix2pD/XNZ+V7udc287qHFK6LUkd7cp879nAHFJoucIK4+sZntdNSmlTc1waL8Wr 7WH+qeAHRdgnYd3lLbeno2tAAhK8ho4dAVVk+XgdxIWtTOvECb3HqomX5pgk5yUj5bHI /3sS0F31SaTp/XBfjihggjNFMXqZ6DrGdBfoogsFN1sNfTi1orH5yJMCKBufuHOvT6TR QA3w== X-Gm-Message-State: AJIora+hzF6rHSgh03C9gaTMl2/ysJcZmHLRRucCj5NF9AUrRiSLnYLY gJjEUBdKTLKVfzP6AnNPcDtcPg== X-Google-Smtp-Source: AGRyM1tLfIfJK1GIEg108XChKFysbc8fly98nIwtnFW2rb7ah1bq0hQXSnB2ixzNToqasmHIgehRKg== X-Received: by 2002:aa7:dac2:0:b0:435:76a2:4ebe with SMTP id x2-20020aa7dac2000000b0043576a24ebemr11236937eds.196.1655998192777; Thu, 23 Jun 2022 08:29:52 -0700 (PDT) Received: from beast.fritz.box (62-178-148-172.cable.dynamic.surfer.at. [62.178.148.172]) by smtp.gmail.com with ESMTPSA id cq18-20020a056402221200b00435651c4a01sm14542785edb.56.2022.06.23.08.29.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 08:29:52 -0700 (PDT) From: Christoph Muellner To: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , Paul Walmsley , Albert Ou , Philipp Tomsich , =?UTF-8?q?Heiko=20St=C3=BCbner?= , Aaron Durbin , Heiko Stuebner , Randy Dunlap , Atish Patra Cc: =?UTF-8?q?Christoph=20M=C3=BCllner?= Subject: [RFC PATCH v2] riscv: Add Zawrs support for spinlocks Date: Thu, 23 Jun 2022 17:29:48 +0200 Message-Id: <20220623152948.1607295-1-christoph.muellner@vrull.eu> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Christoph M=C3=BCllner The current RISC-V code uses the generic ticket lock implementation, that calls the macros smp_cond_load_relaxed() and smp_cond_load_acquire(). Currently, RISC-V uses the generic implementation of these macros. This patch introduces a RISC-V specific implementation, of these macros, that peels off the first loop iteration and modifies the waiting loop such, that it is possible to use the WRS.STO instruction of the Zawrs ISA extension to stall the CPU. The resulting implementation of smp_cond_load_*() will only work for 32-bit or 64-bit types for RV64 and 32-bit types for RV32. This is caused by the restrictions of the LR instruction (RISC-V only has LR.W and LR.D). Compiler assertions guard this new restriction. This patch uses the existing RISC-V ISA extension framework to detect the presents of Zawrs at run-time. If available a NOP instruction will be replaced by WRS.NTO or WRS.STO. The whole mechanism is gated by Kconfig setting, which defaults to Y. The Zawrs specification can be found here: https://github.com/riscv/riscv-zawrs/blob/main/zawrs.adoc Note, that the Zawrs extension is not frozen or ratified yet. Therefore this patch is an RFC and not intended to get merged. Changes since v1: * Adding "depends on !XIP_KERNEL" to RISCV_ISA_ZAWRS * Fixing type checking code in __smp_load_reserved* * Adjustments according to the specification change Signed-off-by: Christoph M=C3=BCllner Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/Kconfig | 11 ++++ arch/riscv/include/asm/barrier.h | 92 ++++++++++++++++++++++++++++ arch/riscv/include/asm/errata_list.h | 19 +++++- arch/riscv/include/asm/hwcap.h | 3 +- arch/riscv/kernel/cpu.c | 1 + arch/riscv/kernel/cpufeature.c | 13 ++++ 6 files changed, 136 insertions(+), 3 deletions(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 32ffef9f6e5b..9d40569237c9 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -358,6 +358,17 @@ config RISCV_ISA_C =20 If you don't know what to do here, say Y. =20 +config RISCV_ISA_ZAWRS + bool "Zawrs extension support" + depends on !XIP_KERNEL + select RISCV_ALTERNATIVE + default y + help + Adds support to dynamically detect the presence of the Zawrs extension + (wait for reservation set) and enable its usage. + + If you don't know what to do here, say Y. + config RISCV_ISA_SVPBMT bool "SVPBMT extension support" depends on 64BIT && MMU diff --git a/arch/riscv/include/asm/barrier.h b/arch/riscv/include/asm/barr= ier.h index d0e24aaa2aa0..1f9628aaa7cb 100644 --- a/arch/riscv/include/asm/barrier.h +++ b/arch/riscv/include/asm/barrier.h @@ -12,6 +12,8 @@ =20 #ifndef __ASSEMBLY__ =20 +#include + #define nop() __asm__ __volatile__ ("nop") =20 #define RISCV_FENCE(p, s) \ @@ -42,6 +44,64 @@ do { \ ___p1; \ }) =20 +#if __riscv_xlen =3D=3D 64 + +#define __riscv_lrsc_word(t) \ + (sizeof(t) =3D=3D sizeof(int) || \ + sizeof(t) =3D=3D sizeof(long)) + +#elif __riscv_xlen =3D=3D 32 + +#define __riscv_lrsc_word(t) \ + (sizeof(t) =3D=3D sizeof(int)) + +#else +#error "Unexpected __riscv_xlen" +#endif /* __riscv_xlen */ + +#define compiletime_assert_atomic_lrsc_type(t) \ + compiletime_assert(__riscv_lrsc_word(t), \ + "Need type compatible with LR/SC instructions.") + +#define ___smp_load_reservedN(pfx, ptr) \ +({ \ + typeof(*ptr) ___p1; \ + __asm__ __volatile__ ("lr." pfx " %[p], %[c]\n" \ + : [p]"=3D&r" (___p1), [c]"+A"(*ptr)); \ + ___p1; \ +}) + +#define ___smp_load_reserved32(ptr) \ + ___smp_load_reservedN("w", ptr) + +#define ___smp_load_reserved64(ptr) \ + ___smp_load_reservedN("d", ptr) + +#define __smp_load_reserved_relaxed(ptr) \ +({ \ + typeof(*ptr) ___p1; \ + compiletime_assert_atomic_lrsc_type(*ptr); \ + if (sizeof(*ptr) =3D=3D 4) { \ + ___p1 =3D ___smp_load_reserved32(ptr); \ + } else { \ + ___p1 =3D ___smp_load_reserved64(ptr); \ + } \ + ___p1; \ +}) + +#define __smp_load_reserved_acquire(ptr) \ +({ \ + typeof(*ptr) ___p1; \ + compiletime_assert_atomic_lrsc_type(*ptr); \ + if (sizeof(*ptr) =3D=3D 4) { \ + ___p1 =3D ___smp_load_reserved32(ptr); \ + } else { \ + ___p1 =3D ___smp_load_reserved64(ptr); \ + } \ + RISCV_FENCE(r,rw); \ + ___p1; \ +}) + /* * This is a very specific barrier: it's currently only used in two places= in * the kernel, both in the scheduler. See include/linux/spinlock.h for th= e two @@ -69,6 +129,38 @@ do { \ */ #define smp_mb__after_spinlock() RISCV_FENCE(iorw,iorw) =20 +#define smp_cond_load_relaxed(ptr, cond_expr) \ +({ \ + typeof(ptr) __PTR =3D (ptr); \ + __unqual_scalar_typeof(*ptr) VAL; \ + VAL =3D READ_ONCE(*__PTR); \ + if (!cond_expr) { \ + for (;;) { \ + VAL =3D __smp_load_reserved_relaxed(__PTR); \ + if (cond_expr) \ + break; \ + ALT_WRS_STO(); \ + } \ + } \ + (typeof(*ptr))VAL; \ +}) + +#define smp_cond_load_acquire(ptr, cond_expr) \ +({ \ + typeof(ptr) __PTR =3D (ptr); \ + __unqual_scalar_typeof(*ptr) VAL; \ + VAL =3D smp_load_acquire(__PTR); \ + if (!cond_expr) { \ + for (;;) { \ + VAL =3D __smp_load_reserved_acquire(__PTR); \ + if (cond_expr) \ + break; \ + ALT_WRS_STO(); \ + } \ + } \ + (typeof(*ptr))VAL; \ +}) + #include =20 #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/include/asm/errata_list.h b/arch/riscv/include/asm/= errata_list.h index 9e2888dbb5b1..e15af9986b1a 100644 --- a/arch/riscv/include/asm/errata_list.h +++ b/arch/riscv/include/asm/errata_list.h @@ -19,8 +19,9 @@ #define ERRATA_THEAD_NUMBER 1 #endif =20 -#define CPUFEATURE_SVPBMT 0 -#define CPUFEATURE_NUMBER 1 +#define CPUFEATURE_ZAWRS 0 +#define CPUFEATURE_SVPBMT 1 +#define CPUFEATURE_NUMBER 2 =20 #ifdef __ASSEMBLY__ =20 @@ -42,6 +43,20 @@ asm(ALTERNATIVE("sfence.vma %0", "sfence.vma", SIFIVE_VE= NDOR_ID, \ ERRATA_SIFIVE_CIP_1200, CONFIG_ERRATA_SIFIVE_CIP_1200) \ : : "r" (addr) : "memory") =20 +#define ZAWRS_WRS_NTO ".long 0x00d00073" +#define ALT_WRS_NTO() \ +asm volatile(ALTERNATIVE( \ + "nop\n\t", \ + ZAWRS_WRS_NTO "\n\t", \ + 0, CPUFEATURE_ZAWRS, CONFIG_RISCV_ISA_ZAWRS)) + +#define ZAWRS_WRS_STO ".long 0x01d00073" +#define ALT_WRS_STO() \ +asm volatile(ALTERNATIVE( \ + "nop\n\t", \ + ZAWRS_WRS_STO "\n\t", \ + 0, CPUFEATURE_ZAWRS, CONFIG_RISCV_ISA_ZAWRS)) + /* * _val is marked as "will be overwritten", so need to set it to 0 * in the default case. diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 4e2486881840..c7dd8cc38bec 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -51,7 +51,8 @@ extern unsigned long elf_hwcap; * available logical extension id. */ enum riscv_isa_ext_id { - RISCV_ISA_EXT_SSCOFPMF =3D RISCV_ISA_EXT_BASE, + RISCV_ISA_EXT_ZAWRS =3D RISCV_ISA_EXT_BASE, + RISCV_ISA_EXT_SSCOFPMF, RISCV_ISA_EXT_SVPBMT, RISCV_ISA_EXT_ID_MAX =3D RISCV_ISA_EXT_MAX, }; diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c index fba9e9f46a8c..6c3a10ff5358 100644 --- a/arch/riscv/kernel/cpu.c +++ b/arch/riscv/kernel/cpu.c @@ -87,6 +87,7 @@ int riscv_of_parent_hartid(struct device_node *node) * extensions by an underscore. */ static struct riscv_isa_ext_data isa_ext_arr[] =3D { + __RISCV_ISA_EXT_DATA(zawrs, RISCV_ISA_EXT_ZAWRS), __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF), __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT), __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX), diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 12b05ce164bb..ce610d8a0e8d 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -199,6 +199,7 @@ void __init riscv_fill_hwcap(void) } else { SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF); SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT); + SET_ISA_EXT_MAP("zawrs", RISCV_ISA_EXT_ZAWRS); } #undef SET_ISA_EXT_MAP } @@ -250,6 +251,14 @@ struct cpufeature_info { bool (*check_func)(unsigned int stage); }; =20 +static bool __init_or_module cpufeature_zawrs_check_func(unsigned int stag= e) +{ + if (stage =3D=3D RISCV_ALTERNATIVES_EARLY_BOOT) + return false; + + return riscv_isa_extension_available(NULL, ZAWRS); +} + static bool __init_or_module cpufeature_svpbmt_check_func(unsigned int sta= ge) { #ifdef CONFIG_RISCV_ISA_SVPBMT @@ -266,6 +275,10 @@ static bool __init_or_module cpufeature_svpbmt_check_f= unc(unsigned int stage) =20 static const struct cpufeature_info __initdata_or_module cpufeature_list[CPUFEATURE_NUMBER] =3D { + { + .name =3D "zawrs", + .check_func =3D cpufeature_zawrs_check_func + }, { .name =3D "svpbmt", .check_func =3D cpufeature_svpbmt_check_func --=20 2.35.3