Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp1435092imw; Sat, 16 Jul 2022 06:37:03 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v5chn1gLw1CK2g6KpVKbnXEN5sQv/dgPlKZPT9qyU+AHf8wqlDygCDlrkQnvkjRKiThnqB X-Received: by 2002:a05:6402:27cf:b0:43a:de0b:9a82 with SMTP id c15-20020a05640227cf00b0043ade0b9a82mr25702714ede.427.1657978623176; Sat, 16 Jul 2022 06:37:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657978623; cv=none; d=google.com; s=arc-20160816; b=ao5NfYo5JhCoZ6pNg7TL4j/jTUDhpYKq+Gf/Dg2qP1yInWkEG2qPStxUlwDec/+k52 ytzW7SUWuB9no+FW3lqObojd9GLGCuihz73P1ZlN0F+N7UwvOrbYpZWzDC0ccFzRCX6A uJXUwvwzim2nYf3M+uqhwu3r++Dx+r3OZfgV1LXwS0aZ65KWzdzjQQ//lv47VMQ2ATNe kov04f3mCOeLHmYnXH5seTxMfmEwWCOtfDg0LHYJRk/Gd2GFfcoZJxYsGQ9Lzn6ui8tP bvz1SPWSZKKYPolCnVwUxTe3+KHBGAqXna/ibp2rA59MP2rt5ljNjZs64Dgd6xEb4Ov9 UOuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=goMUuNdGTUtd4ILkmyHst+UAZnW1FnodngLycjkQpMQ=; b=sdcCLmjfhzxPKk7daw1wS6gaNi542Haws9DRlF8zfKHgjsgLXwmoJHWe4rk0OP3jOj 9F+DPNYpsAYrC9Ypd+gi2czIp03G3V1dnZCAKoczqtt99kij5OXNRC3n8WrOFFARssSl 3aoVDdjqUBRIkwflHEHPE+25aRjCIFZlSMsVKUtladKehjpbdha393/SL1ZmPyrcnYsQ f9/PIc0NdjzQImz5AWvG6XdSegRySyEpmqoP6RyEThC9MDkX1kwrCx5c1qblSCHiQ0G7 b3IOxIQ4gE1dHKjJ1yvHzhHT0NZZkhtLAB8pd4+X2w36a6FzfMGvqFX44ZW38cOoqUle 998w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Mgbx++rj; 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 he9-20020a1709073d8900b007123952b00dsi9718916ejc.100.2022.07.16.06.36.37; Sat, 16 Jul 2022 06:37:03 -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=Mgbx++rj; 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 S230273AbiGPMwL (ORCPT + 99 others); Sat, 16 Jul 2022 08:52:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229694AbiGPMwK (ORCPT ); Sat, 16 Jul 2022 08:52:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F6EE18380 for ; Sat, 16 Jul 2022 05:52:09 -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 dfw.source.kernel.org (Postfix) with ESMTPS id B348061011 for ; Sat, 16 Jul 2022 12:52:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6A06C34114; Sat, 16 Jul 2022 12:52:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1657975928; bh=mPv+630BhvvE0YS0JSf3EHlCURgNl9vN+gPkhW7hZkY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Mgbx++rjNsKuAFOI9bNpZ3EbRpj2TF1IOs5J3v/URWQHELByGIvoxa0dwCe5IX9Dk ODdxhrhNu2p2TNTicwIdHJX1RgmXLrBMBvyfwu9ULv5zWuMuUeBn2Sbi6lILqPpNlB F6AnhG4hjXiccVfWaY92Ykau2sSnI02tO/gqXfLkm0NeIVpQxl32OBL8/IQMNAw8mM GenhEu6tt6qbIA2CCjx0pCTJSOGbO9W2P4vznUfdlptqJgdJzR7oXfIxpFG32PKLh0 DkAyQT5O5/DMb3jTr8YtD2b2gjXDazbHKKwEyuhZcnGYgsqF5XadkDgTjA3bps7VRR eYZicXp0z2FQw== Date: Sat, 16 Jul 2022 20:43:09 +0800 From: Jisheng Zhang To: Dao Lu Cc: linux-kernel@vger.kernel.org, Heiko Stuebner , Paul Walmsley , Palmer Dabbelt , Albert Ou , Atish Patra , Anup Patel , Guo Ren , Randy Dunlap , Niklas Cassel , Qinglin Pan , Alexandre Ghiti , Rob Herring , Tsukasa OI , Yury Norov , "open list:RISC-V ARCHITECTURE" Subject: Re: [PATCH v4] arch/riscv: add Zihintpause support Message-ID: References: <20220620201530.3929352-1-daolu@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20220620201530.3929352-1-daolu@rivosinc.com> X-Spam-Status: No, score=-7.7 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 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 On Mon, Jun 20, 2022 at 01:15:25PM -0700, Dao Lu wrote: > Implement support for the ZiHintPause extension. > > The PAUSE instruction is a HINT that indicates the current hart’s rate > of instruction retirement should be temporarily reduced or paused. > > Reviewed-by: Heiko Stuebner > Tested-by: Heiko Stuebner > Signed-off-by: Dao Lu Reviewed-by: Jisheng Zhang > --- > > 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 for > 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) := $(riscv-march-y)c > toolchain-need-zicsr-zifencei := $(call cc-option-yn, -march=$(riscv-march-y)_zicsr_zifencei) > riscv-march-$(toolchain-need-zicsr-zifencei) := $(riscv-march-y)_zicsr_zifencei > > +# Check if the toolchain supports Zihintpause extension > +toolchain-supports-zihintpause := $(call cc-option-yn, -march=$(riscv-march-y)_zihintpause) > +riscv-march-$(toolchain-supports-zihintpause) := $(riscv-march-y)_zihintpause > + > KBUILD_CFLAGS += -march=$(subst fd,,$(riscv-march-y)) > KBUILD_AFLAGS += -march=$(riscv-march-y) > > diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.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 = RISCV_ISA_EXT_BASE, > RISCV_ISA_EXT_SVPBMT, > + RISCV_ISA_EXT_ZIHINTPAUSE, > RISCV_ISA_EXT_ID_MAX = 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/include/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" : "=r" (dummy)); > + int dummy; > + /* In lieu of a halt instruction, induce a long-latency stall. */ > + __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy)); > #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[] = { > __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/cpufeature.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_EXT_SSCOFPMF); > SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT); > + SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE); > } > #undef SET_ISA_EXT_MAP > } > -- > 2.25.1 >