Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp1845774imw; Tue, 5 Jul 2022 16:57:00 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vF6BlG+XUkqkbCu9TMyVgj6gBnwOhjUarjfW0oget2jWZy43KG5gxYaPoSy2SChzGCrIL2 X-Received: by 2002:aa7:da56:0:b0:437:83a8:7dc6 with SMTP id w22-20020aa7da56000000b0043783a87dc6mr49371112eds.324.1657065420124; Tue, 05 Jul 2022 16:57:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657065420; cv=none; d=google.com; s=arc-20160816; b=1IjWfmfA+J42ekbUn6bcJUXAdJZH5Suj4ej5JxhT1Z7YGDX4pxpTur35NDungoc41q DLYM6ml3f4vwC8Vn+fWq0Y86or7gQcV5ZG2qiZGvfq9KxyM0u/cDh7D1Ic2B4sEP3Bm5 4chY+Bo5YSlexrfarJJgWl3y4eQPyYC622ipItuAMJuZrYtdToCXy96Fni0pKyl+9isS oKcNyQWFSKy+/nC7JFYW95SyhNGYrptjUrS+I1ydBwQESDqEKoAELw6IjcvECJ9jJS9t PRtxJMicSqZ6CV1kVeNhs3Jy3tD+bRkBm1zwHRLMrr6V8+ofV+EOOdbcf9wrRu/b8mwD xnSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=j7e5ORKCs6N2eSzQY/3s9lHmZufgEkbJ2BIPMciFVcE=; b=keUBHkQfcTssQrNlj//CMSgA4Cz0WLqnnFd4DuzpYVkTI0KeOThcckziwrQiBnVsId aCO0XsEF3hL7voMwjB8R9bPGf47yeJcXyrKdNeeoreUAWcD9XPgTDn+S7o1Hk8fkdcmN U2bvZ2M8UqDTM3QkCRN31NYmACj3KS7HxBBzo0uMYdkoFWk10XYGhhY9ZFGWoMK7buR1 Tdx+G1kwg4CPFB/w9rXDAlskKU+4BeT4l20q0BunihuqtbuqazMYX1PIwyCypl0POPKt w0vinSOh43qirXZeiNGt5m8bH3CwT26K4aEL3hxQUJ/9+gVAUsG4OS+VrCN1MNvImaJe QHng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LPLjR2SO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e18-20020a056402191200b0043a7cbba8d5si3555508edz.64.2022.07.05.16.56.33; Tue, 05 Jul 2022 16:57:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LPLjR2SO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230101AbiGEXrv (ORCPT + 99 others); Tue, 5 Jul 2022 19:47:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229455AbiGEXru (ORCPT ); Tue, 5 Jul 2022 19:47:50 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6154D12A for ; Tue, 5 Jul 2022 16:47:49 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 0C167B81A4F for ; Tue, 5 Jul 2022 23:47:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDD04C341D8 for ; Tue, 5 Jul 2022 23:47:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1657064866; bh=ptichBaM2hD9wP7dMdNt41FsibNjXNpggiC/v5NP4zE=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=LPLjR2SO6GjtryO2kpwqDUIVVn6VmRHsNDts5wJGlZSIgKa6MjIqF45PDKHsfcEbR V61uQHUI2ihtZo1Ik+Mt9TJ4Upr5ucB4iZevnxLnKiE+DaLGDtow3x/oIYo6oAzTUA hbHUXa32mfXxDvfMalvbeo5Vy53AbLVKfSNkNBSQXoHwyKYdLRbMoM0I/z6p9q5trn PJQUjjFujaiTudSm6s5eWHpuMuoWYhTg1EhhubzdH+RIpTkguyc8qzL/BC/MwpPVmt kcVKtfwXAKF1XCbuIS/z9e0wKNKLDpLLzyiQZS2XbpQlcGDKZ2ZMi5gkz83Iy2XCaQ KGTWyy/KslSHA== Received: by mail-vk1-f176.google.com with SMTP id b81so6658014vkf.1 for ; Tue, 05 Jul 2022 16:47:46 -0700 (PDT) X-Gm-Message-State: AJIora+gUXDXWGNKwPwdwgGMx4uw0V9D9IME/ljEZLJOoko0MMjRmwce ifx5PK0CVdiNO3LHMWU74p7294sXHm+EcCo8NWY= X-Received: by 2002:a1f:1b4b:0:b0:36c:bc20:3982 with SMTP id b72-20020a1f1b4b000000b0036cbc203982mr21618553vkb.8.1657064865584; Tue, 05 Jul 2022 16:47:45 -0700 (PDT) MIME-Version: 1.0 References: <20220620201530.3929352-1-daolu@rivosinc.com> In-Reply-To: From: Guo Ren Date: Wed, 6 Jul 2022 07:47:34 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v4] arch/riscv: add Zihintpause support To: Dao Lu Cc: Linux Kernel Mailing List , Heiko Stuebner , Paul Walmsley , Palmer Dabbelt , Albert Ou , Atish Patra , Anup Patel , Jisheng Zhang , Randy Dunlap , Niklas Cassel , Qinglin Pan , Alexandre Ghiti , Rob Herring , Tsukasa OI , Yury Norov , "open list:RISC-V ARCHITECTURE" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Reviewed-by: Guo Ren On Wed, Jul 6, 2022 at 12:57 AM Dao Lu wrote: > > ping > > On Mon, Jun 20, 2022 at 1:15 PM Dao Lu wrote: > > > > Implement support for the ZiHintPause extension. > > > > The PAUSE instruction is a HINT that indicates the current hart=E2=80= =99s rate > > of instruction retirement should be temporarily reduced or paused. > > > > Reviewed-by: Heiko Stuebner > > Tested-by: Heiko Stuebner > > Signed-off-by: Dao Lu > > --- > > > > v1 -> v2: > > Remove the usage of static branch, use PAUSE if toolchain supports it > > v2 -> v3: > > Added the static branch back, cpu_relax() behavior is kept the same fo= r > > systems that do not support ZiHintPause > > v3 -> v4: > > Adopted the newly added unified static keys for extensions > > --- > > arch/riscv/Makefile | 4 ++++ > > arch/riscv/include/asm/hwcap.h | 5 +++++ > > arch/riscv/include/asm/vdso/processor.h | 21 ++++++++++++++++++--- > > arch/riscv/kernel/cpu.c | 1 + > > arch/riscv/kernel/cpufeature.c | 1 + > > 5 files changed, 29 insertions(+), 3 deletions(-) > > > > diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile > > index 34cf8a598617..6ddacc6f44b9 100644 > > --- a/arch/riscv/Makefile > > +++ b/arch/riscv/Makefile > > @@ -56,6 +56,10 @@ riscv-march-$(CONFIG_RISCV_ISA_C) :=3D $(riscv-ma= rch-y)c > > toolchain-need-zicsr-zifencei :=3D $(call cc-option-yn, -march=3D$(ris= cv-march-y)_zicsr_zifencei) > > riscv-march-$(toolchain-need-zicsr-zifencei) :=3D $(riscv-march-y)_zic= sr_zifencei > > > > +# Check if the toolchain supports Zihintpause extension > > +toolchain-supports-zihintpause :=3D $(call cc-option-yn, -march=3D$(ri= scv-march-y)_zihintpause) > > +riscv-march-$(toolchain-supports-zihintpause) :=3D $(riscv-march-y)_zi= hintpause > > + > > KBUILD_CFLAGS +=3D -march=3D$(subst fd,,$(riscv-march-y)) > > KBUILD_AFLAGS +=3D -march=3D$(riscv-march-y) > > > > diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hw= cap.h > > index e48eebdd2631..dc47019a0b38 100644 > > --- a/arch/riscv/include/asm/hwcap.h > > +++ b/arch/riscv/include/asm/hwcap.h > > @@ -8,6 +8,7 @@ > > #ifndef _ASM_RISCV_HWCAP_H > > #define _ASM_RISCV_HWCAP_H > > > > +#include > > #include > > #include > > > > @@ -54,6 +55,7 @@ extern unsigned long elf_hwcap; > > enum riscv_isa_ext_id { > > RISCV_ISA_EXT_SSCOFPMF =3D RISCV_ISA_EXT_BASE, > > RISCV_ISA_EXT_SVPBMT, > > + RISCV_ISA_EXT_ZIHINTPAUSE, > > RISCV_ISA_EXT_ID_MAX =3D RISCV_ISA_EXT_MAX, > > }; > > > > @@ -64,6 +66,7 @@ enum riscv_isa_ext_id { > > */ > > enum riscv_isa_ext_key { > > RISCV_ISA_EXT_KEY_FPU, /* For 'F' and 'D' */ > > + RISCV_ISA_EXT_KEY_ZIHINTPAUSE, > > RISCV_ISA_EXT_KEY_MAX, > > }; > > > > @@ -83,6 +86,8 @@ static __always_inline int riscv_isa_ext2key(int num) > > return RISCV_ISA_EXT_KEY_FPU; > > case RISCV_ISA_EXT_d: > > return RISCV_ISA_EXT_KEY_FPU; > > + case RISCV_ISA_EXT_ZIHINTPAUSE: > > + return RISCV_ISA_EXT_KEY_ZIHINTPAUSE; > > default: > > return -EINVAL; > > } > > diff --git a/arch/riscv/include/asm/vdso/processor.h b/arch/riscv/inclu= de/asm/vdso/processor.h > > index 134388cbaaa1..1e4f8b4aef79 100644 > > --- a/arch/riscv/include/asm/vdso/processor.h > > +++ b/arch/riscv/include/asm/vdso/processor.h > > @@ -4,15 +4,30 @@ > > > > #ifndef __ASSEMBLY__ > > > > +#include > > #include > > +#include > > > > static inline void cpu_relax(void) > > { > > + if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY= _ZIHINTPAUSE])) { > > #ifdef __riscv_muldiv > > - int dummy; > > - /* In lieu of a halt instruction, induce a long-latency stall. = */ > > - __asm__ __volatile__ ("div %0, %0, zero" : "=3Dr" (dummy)); > > + int dummy; > > + /* In lieu of a halt instruction, induce a long-latency= stall. */ > > + __asm__ __volatile__ ("div %0, %0, zero" : "=3Dr" (dumm= y)); > > #endif > > + } else { > > + /* > > + * Reduce instruction retirement. > > + * This assumes the PC changes. > > + */ > > +#ifdef __riscv_zihintpause > > + __asm__ __volatile__ ("pause"); > > +#else > > + /* Encoding of the pause instruction */ > > + __asm__ __volatile__ (".4byte 0x100000F"); > > +#endif > > + } > > barrier(); > > } > > > > diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c > > index fba9e9f46a8c..a123e92b14dd 100644 > > --- a/arch/riscv/kernel/cpu.c > > +++ b/arch/riscv/kernel/cpu.c > > @@ -89,6 +89,7 @@ int riscv_of_parent_hartid(struct device_node *node) > > static struct riscv_isa_ext_data isa_ext_arr[] =3D { > > __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF), > > __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT), > > + __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE), > > __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX), > > }; > > > > diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeat= ure.c > > index 1b3ec44e25f5..708df2c0bc34 100644 > > --- a/arch/riscv/kernel/cpufeature.c > > +++ b/arch/riscv/kernel/cpufeature.c > > @@ -198,6 +198,7 @@ void __init riscv_fill_hwcap(void) > > } else { > > SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_E= XT_SSCOFPMF); > > SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT= _SVPBMT); > > + SET_ISA_EXT_MAP("zihintpause", RISCV_IS= A_EXT_ZIHINTPAUSE); > > } > > #undef SET_ISA_EXT_MAP > > } > > -- > > 2.25.1 > > --=20 Best Regards Guo Ren