Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp308757iob; Fri, 13 May 2022 02:07:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw6K0g0WrGoBjeyACycEp3e8ym7HMQdBpn+Ge0GKA3rD22lt8OxGB0Azd0eBaBsKjCSDdgw X-Received: by 2002:a17:907:1b1f:b0:6f0:21ec:6051 with SMTP id mp31-20020a1709071b1f00b006f021ec6051mr3187082ejc.533.1652432849354; Fri, 13 May 2022 02:07:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652432849; cv=none; d=google.com; s=arc-20160816; b=JFxpn0FcLNFTNsWW9sdLkZJaj0l2cDWJulc184ZiDGlCWPRLpnWP/xccRRK8okF/tN 1F7eO8r3UFCRp9t3ivtnKC8efCRT/5isbXb25Ri6ZYUnJjOcf9Nf5Xm68iuubvL6Yc9m AIqJ/2zA/FBF9i1EQfVVNdibLJkkcLlKfb/G8hTWx3lqujz3aNeMU7UPMgqZsU+XweIY ayriHM/uFgcmwjBbiqxs9KH4yDAgkfnRCiYd4ByVpN4NQVbgvaCvhABQ9KGpbkSQ6kLs lf3idt8FV5urfGaVHLz5+jKnZwPX6XtJv+7l8z/lnqK6/cv6bAQtB6MMTS0WDUNWAY/w br/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=/JUiAQKzBnPtASw9bBA0S7cim7i54c+lQ4bL89Ie24Y=; b=L++lD75Ci7wQDuN5a85zYyTaTjYklYwj1fVunqW/RMrIB2rmxphRL24w7YI9f7n8wS 3pEe6+nhNlW1Ls38W44/GC+Jjmf9DxqrqRYVd+BTmD4yAMK8e33LJAhKZBuTbRJ567wi YlH0wslDfFejqXbMzHVNgo7FK9QiSLyeurIMkHU2vlsq3+eh+1sDO5b1obTuwkAWUToM x74JU/cmuK5GIl+B5DbGgDa88FezCQZxqCN+D75xaAOVvkLeLDz9dWEnW/6mLRwShZi9 2qv2FERxRA6zGL4xhu1CFapmoFQEIhAT6ZO84/vOsYIMo6I0L38pR/6W1ZYuWEWb4nGv rgCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=I1Hitc0s; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y13-20020a056402270d00b00423e1efca48si1635959edd.198.2022.05.13.02.07.01; Fri, 13 May 2022 02:07:29 -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=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=I1Hitc0s; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343493AbiELDaz (ORCPT + 99 others); Wed, 11 May 2022 23:30:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238977AbiELDax (ORCPT ); Wed, 11 May 2022 23:30:53 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5EDD6424 for ; Wed, 11 May 2022 20:30:49 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id 31so3442227pgp.8 for ; Wed, 11 May 2022 20:30:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/JUiAQKzBnPtASw9bBA0S7cim7i54c+lQ4bL89Ie24Y=; b=I1Hitc0sLSDuMW3ftGYZTxAqH/9FyBOmqNrTUHH81bqpO9FBcaQ/T+/LHHvkStLHRa FU4Ub/I2yIy/K2Vwj28s9yropHNijLHUJ15ftInxMEgmf4XWvO9RIM8mXLkB3PTGugUW d+Yq1psfwoFC1yfBuK6RzDkH0twRPszSUfv5Ohzj2bn2ZEr52p8wU6rBnsvcxU+CD1oL HwFmvyDylbp2gw0rHcXFptpuvC5T5mArzgEIgOdcO7ym7PU9mSRQ4Q4Bqdz1pgOItv80 vwrRrqlNG9gugvt3g07tgEDVemCmKTPwp+XMVAdU/L74vBynDn3roP9wxqLyTYzrh24f iJEg== 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=/JUiAQKzBnPtASw9bBA0S7cim7i54c+lQ4bL89Ie24Y=; b=7v5M4ycMaJDxiNfW1WlZMPb9cR4OUUOqpGYDzG8ZWpusA9xWSYEtCicSKG9pReiWs+ q4RuDAOJjSMueBnQulY5fvHknY5QW4cgWZ5bxwG9qz94Bxke9QxyFKI0NbqTlRf9Hea/ npDddWy8brJkMg0eF2VwCd6qnIbVYJgV1oVkJJlkri9WVx/my1+ySOmQvJuw3Ixx1P46 Ss6KrC4Kva+ybNICdBAMMA13t7W7IMl2vECL9iDFb95ILxYTmi3Dl0bJCDtDXeqGJMVy TSLATF6Y1ouGRBj8qw0xSFnWpcBFkUNsijE9LAE0F8+LSdnYbApo+/5203sYUnfQBWXq 9zKw== X-Gm-Message-State: AOAM530uvT6x6BawIfanYyMw3LbLo70NGZDQ+Emmnp1AUc/8b0GPeFKP W37+mseufnJZ/vt1DRtcAjMwA4wUvTisRA== X-Received: by 2002:aa7:88ce:0:b0:510:72bd:5a61 with SMTP id k14-20020aa788ce000000b0051072bd5a61mr28315689pff.21.1652326249120; Wed, 11 May 2022 20:30:49 -0700 (PDT) Received: from daolu.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id g10-20020a170902d5ca00b0015e8d4eb283sm2695684plh.205.2022.05.11.20.30.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 20:30:48 -0700 (PDT) From: Dao Lu To: linux-kernel@vger.kernel.org Cc: Dao Lu , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org (open list:RISC-V ARCHITECTURE) Subject: [PATCH] arch/riscv: Add Zihintpause extension support Date: Wed, 11 May 2022 20:30:45 -0700 Message-Id: <20220512033045.1101909-1-daolu@rivosinc.com> X-Mailer: git-send-email 2.36.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,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 This patch: 1. Build with _zihintpause if the toolchain has support for it 2. Detects if the platform supports the extension 3. Use PAUSE for cpu_relax if both toolchain and the platform support it Signed-off-by: Dao Lu --- arch/riscv/Makefile | 4 ++++ arch/riscv/include/asm/hwcap.h | 1 + arch/riscv/include/asm/vdso/processor.h | 19 ++++++++++++++++--- arch/riscv/kernel/cpu.c | 1 + arch/riscv/kernel/cpufeature.c | 7 +++++++ 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 7d81102cffd4..900a8fda1a2d 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 0734e42f74f2..caa9ee5459b4 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -52,6 +52,7 @@ extern unsigned long elf_hwcap; */ enum riscv_isa_ext_id { RISCV_ISA_EXT_SSCOFPMF = RISCV_ISA_EXT_BASE, + RISCV_ISA_EXT_ZIHINTPAUSE, RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX, }; diff --git a/arch/riscv/include/asm/vdso/processor.h b/arch/riscv/include/asm/vdso/processor.h index 134388cbaaa1..106b35ba8cac 100644 --- a/arch/riscv/include/asm/vdso/processor.h +++ b/arch/riscv/include/asm/vdso/processor.h @@ -4,15 +4,28 @@ #ifndef __ASSEMBLY__ +#include #include +#include +extern struct static_key_false riscv_pause_available; static inline void cpu_relax(void) { + if (!static_branch_likely(&riscv_pause_available)) { #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 { +#ifdef __riscv_zihintpause + /* + * Reduce instruction retirement. + * This assumes the PC changes. + */ + __asm__ __volatile__ ("pause"); +#endif + } barrier(); } diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c index ccb617791e56..89e563e9c4cc 100644 --- a/arch/riscv/kernel/cpu.c +++ b/arch/riscv/kernel/cpu.c @@ -88,6 +88,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(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 1b2d42d7f589..327c19507dbb 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -24,6 +24,8 @@ static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; #ifdef CONFIG_FPU __ro_after_init DEFINE_STATIC_KEY_FALSE(cpu_hwcap_fpu); #endif +DEFINE_STATIC_KEY_FALSE(riscv_pause_available); +EXPORT_SYMBOL_GPL(riscv_pause_available); /** * riscv_isa_extension_base() - Get base extension word @@ -192,6 +194,7 @@ void __init riscv_fill_hwcap(void) set_bit(*ext - 'a', this_isa); } else { SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF); + SET_ISA_EXT_MAP("zihintpause", RISCV_ISA_EXT_ZIHINTPAUSE); } #undef SET_ISA_EXT_MAP } @@ -213,6 +216,10 @@ void __init riscv_fill_hwcap(void) } + if (__riscv_isa_extension_available(riscv_isa, RISCV_ISA_EXT_ZIHINTPAUSE)) { + static_branch_enable(&riscv_pause_available); + } + /* We don't support systems with F but without D, so mask those out * here. */ if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) { -- 2.36.0