Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp682254rwb; Thu, 12 Jan 2023 10:53:32 -0800 (PST) X-Google-Smtp-Source: AMrXdXvne/UlU0K0CHRbWT9PJgjLM1BrFYTH6HrdS5yPdmJ7LzMWjK7DCfg1azGwK99F98arf2yx X-Received: by 2002:a17:902:7106:b0:194:66db:7789 with SMTP id a6-20020a170902710600b0019466db7789mr1240026pll.50.1673549612675; Thu, 12 Jan 2023 10:53:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673549612; cv=none; d=google.com; s=arc-20160816; b=IgMAYDaKdsHlI6KBzNcmJ2sfB1FXgmi1mng4FDXaqLgix7BL+w/C6qJoYjvo8f/GBs bN4mxOE5BX5f+5Jl5Mpv+OEWRq3Tw0y2k11jnxTgna/3XrwoynoGqsUOxlC6m5xY4nxr j4fP/SzLbv+X2+9arijMnnZSGCgGC2QhjHPFbO0oYLpVY5gIVoUN9Q+oxXg9Hz3EWXkC 00AovyWYTPJUYUcXNiFw1zr15rczqGzIzyZWjSrqX0Nb7BXaEDcnYxnhPZ0cXGem+RX/ d6yf0zXc1MdN00Yl8pUq3dvOjVPqGgYWkuxKjcAIuB8SpjnYZdE17ckGgs7ykKYj88OY n4gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=TxWhCwPu5LJsqg+vUhYvruNUByCvr504SVJ2EQlA0KA=; b=OgHmURdRSjNCFS6IdRUyNUsVatXwEKvkYX6ykifOReGbMEAABUDlWRAcHLKK8/RI4p eDcQ5yhKakVg5vSNTuHNRu5/uAIpplJJ1gdTMx+TIKq5WGQCFgJTwfuEKw4SItfx5WRd D5cGqbUuVAStIhttA7uu0eGm+aRrHXtSws1WNpy1s7NjQJcUyNoYtOtXdeUoYipn18FE uO7Tkk0/CjJ3D/DVOMWJKzDEYMx1G6yBft4sOmL13g7PHXrJvxYqzF76SVEoLeY57adZ mPiLXmAfbdUriEodN6u8vgP1PES1eDYQYQUooZLzEaKntZNx+Hk0oKBVEv4rLtNmTdOS jk5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=BTjV8oUa; 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 u12-20020a17090282cc00b00190fde95ac5si17734042plz.411.2023.01.12.10.53.26; Thu, 12 Jan 2023 10:53:32 -0800 (PST) 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=BTjV8oUa; 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 S240535AbjALSbH (ORCPT + 50 others); Thu, 12 Jan 2023 13:31:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231814AbjALSag (ORCPT ); Thu, 12 Jan 2023 13:30:36 -0500 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C497877AE1 for ; Thu, 12 Jan 2023 10:04:17 -0800 (PST) Received: by mail-pf1-x429.google.com with SMTP id k19so14353654pfg.11 for ; Thu, 12 Jan 2023 10:04:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=TxWhCwPu5LJsqg+vUhYvruNUByCvr504SVJ2EQlA0KA=; b=BTjV8oUa7Ed+ZbWoFOVVgaZTO8KHvYB4mpyDw2hp1beDa6ri8H1s9rmAvwqBSB40/8 Ajwhv1g2NnEBGu5dfQj1Vzt2N0/+s2WXP4HK0KqqY6h9ql/qRJHto+qLngU6H6rSftsG WDd5uTEyEwYCLBiGUSXvHMonkKnb9puFNv2rJDbYWl6MKcW2ceFCVkSx610YhbhzllQE rGe6sunHorm2UE/e2q2Hc/i7PddOpS1j5fIpIl5a7U/7UrV0QaaqGvXWhTVwViBWgrVY rcsF/ZPZ+OsIGBkmBcTViK54nA4NmxM77OM1EeKCLC2g1ahnDA6P+tOY7jSpr68Zb865 TJnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TxWhCwPu5LJsqg+vUhYvruNUByCvr504SVJ2EQlA0KA=; b=N/8+SO8aE2cjRrSiGaTJqC5qmfQA1EYFat7jzpMAW87vdmE+Zhi/+oQCHYwwx8r6Lb bJHd2xqCIMujcH2foUSdHG/U3ipulWDYKveod8xZs2aTcfTfRsyQjNXzOwE9ueQS0xjC ZlG2BLnvMBwmlYsPWVniAL+065EGFeEGAXrr8ovj2EmK8w7bsHzZ69X5u9AP0kYYP/xK KhDf5RKVljhhb5+8pQfzHX19t2TiNHqzK0UImSzK5/kWMyiDh9zfl9Aa7JIJv8vFkSnB GZaWgqOHdtY3fKcAQydo4B8cc9k8/YQcmqtRh498HC1ZNJRt3OwjOVkLk/uvt9/G5nYx /8yQ== X-Gm-Message-State: AFqh2krmlyv2GyhWf1TqyCgO7tY/iM5dyRQAXoeYPDpt7BRc6cj36v78 TgBlB97uE8WojGwndg3/cJxsqGVWHdVvge7zqD/pbg== X-Received: by 2002:a63:104f:0:b0:4ac:d14e:2d01 with SMTP id 15-20020a63104f000000b004acd14e2d01mr1375022pgq.595.1673546657212; Thu, 12 Jan 2023 10:04:17 -0800 (PST) MIME-Version: 1.0 References: <20221215170046.2010255-1-atishp@rivosinc.com> <20221215170046.2010255-8-atishp@rivosinc.com> <20230112152948.3vagzedhegbj4jbj@orel> In-Reply-To: <20230112152948.3vagzedhegbj4jbj@orel> From: Atish Kumar Patra Date: Thu, 12 Jan 2023 10:04:06 -0800 Message-ID: Subject: Re: [PATCH v2 07/11] RISC-V: KVM: Add SBI PMU extension support To: Andrew Jones Cc: linux-kernel@vger.kernel.org, Anup Patel , Atish Patra , Guo Ren , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paul Walmsley , Sergey Matyukevich , Eric Lin , Will Deacon Content-Type: text/plain; charset="UTF-8" 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 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 Thu, Jan 12, 2023 at 7:29 AM Andrew Jones wrote: > > On Thu, Dec 15, 2022 at 09:00:42AM -0800, Atish Patra wrote: > > SBI PMU extension allows KVM guests to configure/start/stop/query about > > the PMU counters in virtualized enviornment as well. > > > > In order to allow that, KVM implements the entire SBI PMU extension. > > > > Signed-off-by: Atish Patra > > --- > > arch/riscv/kvm/Makefile | 2 +- > > arch/riscv/kvm/vcpu_sbi.c | 11 +++++ > > arch/riscv/kvm/vcpu_sbi_pmu.c | 86 +++++++++++++++++++++++++++++++++++ > > 3 files changed, 98 insertions(+), 1 deletion(-) > > create mode 100644 arch/riscv/kvm/vcpu_sbi_pmu.c > > > > diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile > > index 5de1053..278e97c 100644 > > --- a/arch/riscv/kvm/Makefile > > +++ b/arch/riscv/kvm/Makefile > > @@ -25,4 +25,4 @@ kvm-y += vcpu_sbi_base.o > > kvm-y += vcpu_sbi_replace.o > > kvm-y += vcpu_sbi_hsm.o > > kvm-y += vcpu_timer.o > > -kvm-$(CONFIG_RISCV_PMU_SBI) += vcpu_pmu.o > > +kvm-$(CONFIG_RISCV_PMU_SBI) += vcpu_pmu.o vcpu_sbi_pmu.o > > diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c > > index 50c5472..3b8b84e8 100644 > > --- a/arch/riscv/kvm/vcpu_sbi.c > > +++ b/arch/riscv/kvm/vcpu_sbi.c > > @@ -20,6 +20,16 @@ static const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01 = { > > }; > > #endif > > > > +#ifdef CONFIG_RISCV_PMU_SBI > > +extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_pmu; > > +#else > > +static const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_pmu = { > > + .extid_start = -1UL, > > + .extid_end = -1UL, > > + .handler = NULL, > > +}; > > +#endif > > + > > static const struct kvm_vcpu_sbi_extension *sbi_ext[] = { > > &vcpu_sbi_ext_v01, > > &vcpu_sbi_ext_base, > > @@ -28,6 +38,7 @@ static const struct kvm_vcpu_sbi_extension *sbi_ext[] = { > > &vcpu_sbi_ext_rfence, > > &vcpu_sbi_ext_srst, > > &vcpu_sbi_ext_hsm, > > + &vcpu_sbi_ext_pmu, > > &vcpu_sbi_ext_experimental, > > &vcpu_sbi_ext_vendor, > > }; > > diff --git a/arch/riscv/kvm/vcpu_sbi_pmu.c b/arch/riscv/kvm/vcpu_sbi_pmu.c > > new file mode 100644 > > index 0000000..223752f > > --- /dev/null > > +++ b/arch/riscv/kvm/vcpu_sbi_pmu.c > > @@ -0,0 +1,86 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +/* > > + * Copyright (c) 2022 Rivos Inc > > + * > > + * Authors: > > + * Atish Patra > > + */ > > + > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > + > > +static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, > > + struct kvm_vcpu_sbi_ext_data *edata, > > + struct kvm_cpu_trap *utrap) > > +{ > > + int ret = 0; > > + struct kvm_cpu_context *cp = &vcpu->arch.guest_context; > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > + unsigned long funcid = cp->a6; > > + uint64_t temp; > > + > > + /* Return not supported if PMU is not initialized */ > > + if (!kvpmu->init_done) > > + return -EINVAL; > > + > > + switch (funcid) { > > + case SBI_EXT_PMU_NUM_COUNTERS: > > + ret = kvm_riscv_vcpu_pmu_num_ctrs(vcpu, edata); > > + break; > > + case SBI_EXT_PMU_COUNTER_GET_INFO: > > + ret = kvm_riscv_vcpu_pmu_ctr_info(vcpu, cp->a0, edata); > > + break; > > + case SBI_EXT_PMU_COUNTER_CFG_MATCH: > > +#if defined(CONFIG_32BIT) > > + temp = ((uint64_t)cp->a5 << 32) | cp->a4; > > +#else > > + temp = cp->a4; > > +#endif > > + ret = kvm_riscv_vcpu_pmu_ctr_cfg_match(vcpu, cp->a0, cp->a1, > > + cp->a2, cp->a3, temp, edata); > > + break; > > + case SBI_EXT_PMU_COUNTER_START: > > +#if defined(CONFIG_32BIT) > > + temp = ((uint64_t)cp->a4 << 32) | cp->a3; > > +#else > > + temp = cp->a3; > > +#endif > > + ret = kvm_riscv_vcpu_pmu_ctr_start(vcpu, cp->a0, cp->a1, cp->a2, > > + temp, edata); > > + break; > > + case SBI_EXT_PMU_COUNTER_STOP: > > + ret = kvm_riscv_vcpu_pmu_ctr_stop(vcpu, cp->a0, cp->a1, cp->a2, edata); > > + break; > > + case SBI_EXT_PMU_COUNTER_FW_READ: > > + ret = kvm_riscv_vcpu_pmu_ctr_read(vcpu, cp->a0, edata); > > + break; > > + default: > > + edata->err_val = SBI_ERR_NOT_SUPPORTED; > > + } > > + > > + > > + return ret; > > +} > > + > > +unsigned long kvm_sbi_ext_pmu_probe(struct kvm_vcpu *vcpu, unsigned long extid) > > +{ > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > + > > + /* > > + * PMU Extension is only available to guests if privilege mode filtering > > + * is available. Otherwise, guest will always count events while the > > + * execution is in hypervisor mode. > > + */ > > + return kvpmu->init_done && riscv_isa_extension_available(NULL, SSCOFPMF); > > Assuming we're only supporting homogeneous systems, then can't we just > check for Sscofpmf at PMU init time? When the extension isn't present > we'd fail to init and then here init_done wouldn't be set. > Sure. We can do that too. Will revise it v3. > > +} > > + > > +const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_pmu = { > > + .extid_start = SBI_EXT_PMU, > > + .extid_end = SBI_EXT_PMU, > > + .handler = kvm_sbi_ext_pmu_handler, > > + .probe = kvm_sbi_ext_pmu_probe, > > +}; > > -- > > 2.25.1 > > > > Thanks, > drew