Message ID | 20230127182558.2416400-8-atishp@rivosinc.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp980736wrn; Fri, 27 Jan 2023 10:28:57 -0800 (PST) X-Google-Smtp-Source: AK7set+WUzvk14cKJG5H9wNmxk+MQ2gpviS6EzBGaUTr70Cz0twl3PQvcgQ2JCgKufRluIGDsTAU X-Received: by 2002:a17:90b:4a8e:b0:22c:51b0:6b49 with SMTP id lp14-20020a17090b4a8e00b0022c51b06b49mr2382014pjb.37.1674844137425; Fri, 27 Jan 2023 10:28:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674844137; cv=none; d=google.com; s=arc-20160816; b=mOq5epe+nlr4cd7sS/kK2Pp/F6WzvkNdd+duONXp8A9izIoDRKhyUpUUn7qv7QG3eO wgkDWXKwKye6k37zsMLtU6WL8NiJR+EB2z3ZuWZc3BZ90uxAIgfMw6jHk6VrK3ogJ9Dm qxboE8C9T4zrXWKq2As1KJ98hc9quhlM5eU3DT+jJyI9cazJtrSIXbWSr+qGn0SknrZI 9v63NQsPSgCW2MpENxvCqmwiD4FnGDmr7xYF2A/A004O7aitu/l+hKWiUF9io0p9KovK 4Pa/5B3fZ32qZp0A67fLhnWtl9/4ezmSM+53LXCmQyEVjcqDBjirlh2CYGsp3579fv9g /IDQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ngz4BJEkYwj1EhZHcesEeRsl9PBRoQJPRxlLhAhdcSk=; b=q/gOLx9VwFDPlY0znlFntohwF6x9ubAcd8DwLn+vzqIV+3e2Sl2xH6M09DQj2bZotn rBr6PtPdK5sazTj/stg1uR2VCXx7zxGc0CilSDO4wljAHyquAc2id9R70ulEo3NaklgO bXapubhu4UKpjF+8/iamBPn2OlM2CTxuoAo2in2xyvAgnd5NJU2ZAmJwLNiw4y9mFWKt 7xkjVZtu25xjFJQZisHHI6gAPuqvF9QOO2L+HWdOlXBm9qVZEG7O1eR55OthN8sLWmmO C7u+6xW0HK6Ekuke7w0FN49NTx7kY9mFEFINRynCkFAK6Nm4kFa48Z6gu6zlGek8daxt 8qQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=u6oB7dWx; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bv13-20020a17090af18d00b0022be0f7773asi5107515pjb.95.2023.01.27.10.28.45; Fri, 27 Jan 2023 10:28:57 -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=u6oB7dWx; 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 S235368AbjA0S0m (ORCPT <rfc822;lekhanya01809@gmail.com> + 99 others); Fri, 27 Jan 2023 13:26:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235288AbjA0S0X (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 27 Jan 2023 13:26:23 -0500 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D35C07F698 for <linux-kernel@vger.kernel.org>; Fri, 27 Jan 2023 10:26:20 -0800 (PST) Received: by mail-pl1-x630.google.com with SMTP id g23so5836730plq.12 for <linux-kernel@vger.kernel.org>; Fri, 27 Jan 2023 10:26:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ngz4BJEkYwj1EhZHcesEeRsl9PBRoQJPRxlLhAhdcSk=; b=u6oB7dWx2nvfeKTmg127o8Z3ghvpoM1bbYLH05DhRneKdCwOmcMQjV+vs0BmoOFFMD FVNEpwEHKcdqtb5ibldK24GhPd3+1OQN7KBbcGwfMJaazEFkMlgxV1U2bsGqANViR6JN 8Y4LZJRP8L5WkwhwASxPgzpSA6Up7PgeQCbLknH30pmTUTJTms+xJx52r1sY63x8MlT7 JeBF+z9A9QNKEcGJbg9gDr+mKRr/0piNHBnonZ/MPyXgqyiZGgL5idGzTiyi/7/3vdi0 6KWv/gT2SOBwQ6Ng4eTG4/xTFMEqd92IamzpE+9JqFY/T5wA2Fo8DUUlzi47HzyoOVVc 3nDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ngz4BJEkYwj1EhZHcesEeRsl9PBRoQJPRxlLhAhdcSk=; b=6yKulKGdN+RenpgZdjnuHbGtTZSg7zPbw9kG0tqAflmLbd5PHeLmnzPWtyq0PjX81R +SNRjYTwinS2lv67Bs57V34g2whulhP/ikCm4YssV/haE0LITYhmCQV4MKzDth7MlNi2 rhoSlbsFK5O0KLXLLern0Zud58fwQgSiF3GfP1Hy6uZIpVekuLUtNFu6PX7abqA7rhjF 45WtsWx5kFlo25BwvIQOWpBG5y+9WnwTyYQtbmixFLnIhBIWxSfegB0lQsFcnok0e28L 9Q61ImL3/xwjf4xOCuiky1dyk2RZrkgaR4mPF5flpTg1ZM21Qt948yq4opTo09jCOiIC fdxQ== X-Gm-Message-State: AFqh2kq3fxlCevksloarJ9c0Bi4lIp0FRP+NIeBRmu5Xw+X8gAhBTfwt mhUQWaTYDi/UKMadPWWMrBPr3FgJwRb7t29u X-Received: by 2002:a17:902:f546:b0:194:d9d7:88c4 with SMTP id h6-20020a170902f54600b00194d9d788c4mr38308129plf.49.1674843980061; Fri, 27 Jan 2023 10:26:20 -0800 (PST) Received: from atishp.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id jc5-20020a17090325c500b00189d4c666c8sm3195219plb.153.2023.01.27.10.26.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Jan 2023 10:26:19 -0800 (PST) From: Atish Patra <atishp@rivosinc.com> To: linux-kernel@vger.kernel.org Cc: Atish Patra <atishp@rivosinc.com>, Andrew Jones <ajones@ventanamicro.com>, Anup Patel <anup@brainfault.org>, Atish Patra <atishp@atishpatra.org>, Guo Ren <guoren@kernel.org>, Heiko Stuebner <heiko@sntech.de>, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland <mark.rutland@arm.com>, Palmer Dabbelt <palmer@dabbelt.com>, Paul Walmsley <paul.walmsley@sifive.com>, Sergey Matyukevich <sergey.matyukevich@syntacore.com>, Will Deacon <will@kernel.org> Subject: [PATCH v3 07/14] RISC-V: KVM: Add skeleton support for perf Date: Fri, 27 Jan 2023 10:25:51 -0800 Message-Id: <20230127182558.2416400-8-atishp@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230127182558.2416400-1-atishp@rivosinc.com> References: <20230127182558.2416400-1-atishp@rivosinc.com> 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 autolearn=unavailable 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756201366244289614?= X-GMAIL-MSGID: =?utf-8?q?1756201366244289614?= |
Series |
KVM perf support
|
|
Commit Message
Atish Patra
Jan. 27, 2023, 6:25 p.m. UTC
This patch only adds barebore structure of perf implementation. Most of
the function returns zero at this point and will be implemented
fully in the future.
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
arch/riscv/include/asm/kvm_host.h | 3 +
arch/riscv/include/asm/kvm_vcpu_pmu.h | 76 ++++++++++++++
arch/riscv/kvm/Makefile | 1 +
arch/riscv/kvm/vcpu.c | 5 +
arch/riscv/kvm/vcpu_pmu.c | 145 ++++++++++++++++++++++++++
5 files changed, 230 insertions(+)
create mode 100644 arch/riscv/include/asm/kvm_vcpu_pmu.h
create mode 100644 arch/riscv/kvm/vcpu_pmu.c
Comments
On Fri, Jan 27, 2023 at 11:56 PM Atish Patra <atishp@rivosinc.com> wrote: > > This patch only adds barebore structure of perf implementation. Most of s/barebore/barebone/ > the function returns zero at this point and will be implemented > fully in the future. > > Signed-off-by: Atish Patra <atishp@rivosinc.com> > --- > arch/riscv/include/asm/kvm_host.h | 3 + > arch/riscv/include/asm/kvm_vcpu_pmu.h | 76 ++++++++++++++ > arch/riscv/kvm/Makefile | 1 + > arch/riscv/kvm/vcpu.c | 5 + > arch/riscv/kvm/vcpu_pmu.c | 145 ++++++++++++++++++++++++++ > 5 files changed, 230 insertions(+) > create mode 100644 arch/riscv/include/asm/kvm_vcpu_pmu.h > create mode 100644 arch/riscv/kvm/vcpu_pmu.c > > diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h > index 93f43a3..f9874b4 100644 > --- a/arch/riscv/include/asm/kvm_host.h > +++ b/arch/riscv/include/asm/kvm_host.h > @@ -18,6 +18,7 @@ > #include <asm/kvm_vcpu_insn.h> > #include <asm/kvm_vcpu_sbi.h> > #include <asm/kvm_vcpu_timer.h> > +#include <asm/kvm_vcpu_pmu.h> > > #define KVM_MAX_VCPUS 1024 > > @@ -228,6 +229,8 @@ struct kvm_vcpu_arch { > > /* Don't run the VCPU (blocked) */ > bool pause; > + > + struct kvm_pmu pmu; Add a single line comment just like other members of the structure. I also suggest naming this variable "pmu_context" or something similar for naming consistency. > }; > > static inline void kvm_arch_hardware_unsetup(void) {} > diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h > new file mode 100644 > index 0000000..3f43a43 > --- /dev/null > +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h > @@ -0,0 +1,76 @@ > +/* SPDX-License-Identifier: GPL-2.0-only */ > +/* > + * Copyright (c) 2023 Rivos Inc > + * > + * Authors: > + * Atish Patra <atishp@rivosinc.com> > + */ > + > +#ifndef __KVM_VCPU_RISCV_PMU_H > +#define __KVM_VCPU_RISCV_PMU_H > + > +#include <linux/perf/riscv_pmu.h> > +#include <asm/kvm_vcpu_sbi.h> > +#include <asm/sbi.h> > + > +#ifdef CONFIG_RISCV_PMU_SBI > +#define RISCV_KVM_MAX_FW_CTRS 32 > +#define RISCV_MAX_COUNTERS 64 > + > +/* Per virtual pmu counter data */ > +struct kvm_pmc { > + u8 idx; > + struct perf_event *perf_event; > + uint64_t counter_val; > + union sbi_pmu_ctr_info cinfo; > + /* Event monitoring status */ > + bool started; > +}; > + > +/* PMU data structure per vcpu */ > +struct kvm_pmu { > + struct kvm_pmc pmc[RISCV_MAX_COUNTERS]; > + /* Number of the virtual firmware counters available */ > + int num_fw_ctrs; > + /* Number of the virtual hardware counters available */ > + int num_hw_ctrs; > + /* A flag to indicate that pmu initialization is done */ > + bool init_done; > + /* Bit map of all the virtual counter used */ > + DECLARE_BITMAP(pmc_in_use, RISCV_MAX_COUNTERS); > +}; > + > +#define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu) > +#define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu)) > + > +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata); > +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, > + struct kvm_vcpu_sbi_ext_data *edata); > +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, > + unsigned long ctr_mask, unsigned long flag, uint64_t ival, > + struct kvm_vcpu_sbi_ext_data *edata); > +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, > + unsigned long ctr_mask, unsigned long flag, > + struct kvm_vcpu_sbi_ext_data *edata); > +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, > + unsigned long ctr_mask, unsigned long flag, > + unsigned long eidx, uint64_t evtdata, > + struct kvm_vcpu_sbi_ext_data *edata); > +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, > + struct kvm_vcpu_sbi_ext_data *edata); > +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); > +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu); > +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu); > + > +#else > +struct kvm_pmu { > +}; > + > +static inline int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > +{ > + return 0; > +} > +static inline void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) {} > +static inline void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) {} > +#endif /* CONFIG_RISCV_PMU_SBI */ > +#endif /* !__KVM_VCPU_RISCV_PMU_H */ > diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile > index 019df920..5de1053 100644 > --- a/arch/riscv/kvm/Makefile > +++ b/arch/riscv/kvm/Makefile > @@ -25,3 +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 > diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c > index 7c08567..b746f21 100644 > --- a/arch/riscv/kvm/vcpu.c > +++ b/arch/riscv/kvm/vcpu.c > @@ -137,6 +137,7 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) > > WRITE_ONCE(vcpu->arch.irqs_pending, 0); > WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); Add an empty newline here. > + kvm_riscv_vcpu_pmu_reset(vcpu); > > vcpu->arch.hfence_head = 0; > vcpu->arch.hfence_tail = 0; > @@ -194,6 +195,9 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) > /* Setup VCPU timer */ > kvm_riscv_vcpu_timer_init(vcpu); > > + /* setup performance monitoring */ > + kvm_riscv_vcpu_pmu_init(vcpu); > + > /* Reset VCPU */ > kvm_riscv_reset_vcpu(vcpu); > > @@ -216,6 +220,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) > /* Cleanup VCPU timer */ > kvm_riscv_vcpu_timer_deinit(vcpu); > > + kvm_riscv_vcpu_pmu_deinit(vcpu); Add an empty newline here. > /* Free unused pages pre-allocated for G-stage page table mappings */ > kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); > } > diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c > new file mode 100644 > index 0000000..d3fd551 > --- /dev/null > +++ b/arch/riscv/kvm/vcpu_pmu.c > @@ -0,0 +1,145 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Copyright (c) 2023 Rivos Inc > + * > + * Authors: > + * Atish Patra <atishp@rivosinc.com> > + */ > + > +#include <linux/errno.h> > +#include <linux/err.h> > +#include <linux/kvm_host.h> > +#include <linux/perf/riscv_pmu.h> > +#include <asm/csr.h> > +#include <asm/kvm_vcpu_sbi.h> > +#include <asm/kvm_vcpu_pmu.h> > +#include <linux/kvm_host.h> > + > +#define kvm_pmu_num_counters(pmu) ((pmu)->num_hw_ctrs + (pmu)->num_fw_ctrs) > + > +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata) > +{ > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > + > + edata->out_val = kvm_pmu_num_counters(kvpmu); > + > + return 0; > +} > + > +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, > + struct kvm_vcpu_sbi_ext_data *edata) > +{ > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > + > + if (cidx > RISCV_MAX_COUNTERS || cidx == 1) { > + edata->err_val = SBI_ERR_INVALID_PARAM; > + return 0; > + } > + > + edata->out_val = kvpmu->pmc[cidx].cinfo.value; > + > + return 0; > +} > + > +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, > + unsigned long ctr_mask, unsigned long flag, uint64_t ival, > + struct kvm_vcpu_sbi_ext_data *edata) > +{ > + /* TODO */ > + return 0; > +} > + > +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, > + unsigned long ctr_mask, unsigned long flag, > + struct kvm_vcpu_sbi_ext_data *edata) > +{ > + /* TODO */ > + return 0; > +} > + > +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, > + unsigned long ctr_mask, unsigned long flag, > + unsigned long eidx, uint64_t evtdata, > + struct kvm_vcpu_sbi_ext_data *edata) > +{ > + /* TODO */ > + return 0; > +} > + > +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, > + struct kvm_vcpu_sbi_ext_data *edata) > +{ > + /* TODO */ > + return 0; > +} > + > +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > +{ > + int i = 0, num_fw_ctrs, ret, num_hw_ctrs = 0, hpm_width = 0; > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > + struct kvm_pmc *pmc; > + > + ret = riscv_pmu_get_hpm_info(&hpm_width, &num_hw_ctrs); > + if (ret < 0) > + return ret; > + > + if (!hpm_width || !num_hw_ctrs) { > + pr_err("Cannot initialize VCPU with NULL hpmcounter width or number of counters\n"); What will happen if underlying M-mode firmware does not implement SBI PMU extension ? > + return -EINVAL; > + } > + > + if ((num_hw_ctrs + RISCV_KVM_MAX_FW_CTRS) > RISCV_MAX_COUNTERS) { > + pr_warn("Limiting fw counters as hw & fw counters exceed maximum counters\n"); How is this possible ? Maximum HW counters is 32 (including time, cycle, and instret), RISCV_KVM_MAX_FW_CTRS = 32, and RISCV_MAX_COUNTERS = 64 > + num_fw_ctrs = RISCV_MAX_COUNTERS - num_hw_ctrs; > + } else > + num_fw_ctrs = RISCV_KVM_MAX_FW_CTRS; > + > + kvpmu->num_hw_ctrs = num_hw_ctrs; > + kvpmu->num_fw_ctrs = num_fw_ctrs; > + > + /* > + * There is no correlation between the logical hardware counter and virtual counters. > + * However, we need to encode a hpmcounter CSR in the counter info field so that > + * KVM can trap n emulate the read. This works well in the migration use case as > + * KVM doesn't care if the actual hpmcounter is available in the hardware or not. > + */ > + for (i = 0; i < kvm_pmu_num_counters(kvpmu); i++) { > + /* TIME CSR shouldn't be read from perf interface */ > + if (i == 1) > + continue; > + pmc = &kvpmu->pmc[i]; > + pmc->idx = i; > + if (i < kvpmu->num_hw_ctrs) { > + kvpmu->pmc[i].cinfo.type = SBI_PMU_CTR_TYPE_HW; > + if (i < 3) > + /* CY, IR counters */ > + kvpmu->pmc[i].cinfo.width = 63; > + else > + kvpmu->pmc[i].cinfo.width = hpm_width; > + /* > + * The CSR number doesn't have any relation with the logical > + * hardware counters. The CSR numbers are encoded sequentially > + * to avoid maintaining a map between the virtual counter > + * and CSR number. > + */ > + pmc->cinfo.csr = CSR_CYCLE + i; > + } else { > + pmc->cinfo.type = SBI_PMU_CTR_TYPE_FW; > + pmc->cinfo.width = BITS_PER_LONG - 1; > + } > + } > + > + kvpmu->init_done = true; > + > + return 0; > +} > + > +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) > +{ > + /* TODO */ > +} > + > +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) > +{ > + kvm_riscv_vcpu_pmu_deinit(vcpu); > +} > -- > 2.25.1 > Regards, Anup
On Sun, Jan 29, 2023 at 4:30 AM Anup Patel <anup@brainfault.org> wrote: > > On Fri, Jan 27, 2023 at 11:56 PM Atish Patra <atishp@rivosinc.com> wrote: > > > > This patch only adds barebore structure of perf implementation. Most of > > s/barebore/barebone/ > > > the function returns zero at this point and will be implemented > > fully in the future. > > > > Signed-off-by: Atish Patra <atishp@rivosinc.com> > > --- > > arch/riscv/include/asm/kvm_host.h | 3 + > > arch/riscv/include/asm/kvm_vcpu_pmu.h | 76 ++++++++++++++ > > arch/riscv/kvm/Makefile | 1 + > > arch/riscv/kvm/vcpu.c | 5 + > > arch/riscv/kvm/vcpu_pmu.c | 145 ++++++++++++++++++++++++++ > > 5 files changed, 230 insertions(+) > > create mode 100644 arch/riscv/include/asm/kvm_vcpu_pmu.h > > create mode 100644 arch/riscv/kvm/vcpu_pmu.c > > > > diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h > > index 93f43a3..f9874b4 100644 > > --- a/arch/riscv/include/asm/kvm_host.h > > +++ b/arch/riscv/include/asm/kvm_host.h > > @@ -18,6 +18,7 @@ > > #include <asm/kvm_vcpu_insn.h> > > #include <asm/kvm_vcpu_sbi.h> > > #include <asm/kvm_vcpu_timer.h> > > +#include <asm/kvm_vcpu_pmu.h> > > > > #define KVM_MAX_VCPUS 1024 > > > > @@ -228,6 +229,8 @@ struct kvm_vcpu_arch { > > > > /* Don't run the VCPU (blocked) */ > > bool pause; > > + > > + struct kvm_pmu pmu; > > Add a single line comment just like other members of the structure. > > I also suggest naming this variable "pmu_context" or something similar > for naming consistency. > Done. > > }; > > > > static inline void kvm_arch_hardware_unsetup(void) {} > > diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h > > new file mode 100644 > > index 0000000..3f43a43 > > --- /dev/null > > +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h > > @@ -0,0 +1,76 @@ > > +/* SPDX-License-Identifier: GPL-2.0-only */ > > +/* > > + * Copyright (c) 2023 Rivos Inc > > + * > > + * Authors: > > + * Atish Patra <atishp@rivosinc.com> > > + */ > > + > > +#ifndef __KVM_VCPU_RISCV_PMU_H > > +#define __KVM_VCPU_RISCV_PMU_H > > + > > +#include <linux/perf/riscv_pmu.h> > > +#include <asm/kvm_vcpu_sbi.h> > > +#include <asm/sbi.h> > > + > > +#ifdef CONFIG_RISCV_PMU_SBI > > +#define RISCV_KVM_MAX_FW_CTRS 32 > > +#define RISCV_MAX_COUNTERS 64 > > + > > +/* Per virtual pmu counter data */ > > +struct kvm_pmc { > > + u8 idx; > > + struct perf_event *perf_event; > > + uint64_t counter_val; > > + union sbi_pmu_ctr_info cinfo; > > + /* Event monitoring status */ > > + bool started; > > +}; > > + > > +/* PMU data structure per vcpu */ > > +struct kvm_pmu { > > + struct kvm_pmc pmc[RISCV_MAX_COUNTERS]; > > + /* Number of the virtual firmware counters available */ > > + int num_fw_ctrs; > > + /* Number of the virtual hardware counters available */ > > + int num_hw_ctrs; > > + /* A flag to indicate that pmu initialization is done */ > > + bool init_done; > > + /* Bit map of all the virtual counter used */ > > + DECLARE_BITMAP(pmc_in_use, RISCV_MAX_COUNTERS); > > +}; > > + > > +#define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu) > > +#define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu)) > > + > > +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata); > > +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, > > + struct kvm_vcpu_sbi_ext_data *edata); > > +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > + unsigned long ctr_mask, unsigned long flag, uint64_t ival, > > + struct kvm_vcpu_sbi_ext_data *edata); > > +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > + unsigned long ctr_mask, unsigned long flag, > > + struct kvm_vcpu_sbi_ext_data *edata); > > +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > + unsigned long ctr_mask, unsigned long flag, > > + unsigned long eidx, uint64_t evtdata, > > + struct kvm_vcpu_sbi_ext_data *edata); > > +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, > > + struct kvm_vcpu_sbi_ext_data *edata); > > +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); > > +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu); > > +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu); > > + > > +#else > > +struct kvm_pmu { > > +}; > > + > > +static inline int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > > +{ > > + return 0; > > +} > > +static inline void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) {} > > +static inline void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) {} > > +#endif /* CONFIG_RISCV_PMU_SBI */ > > +#endif /* !__KVM_VCPU_RISCV_PMU_H */ > > diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile > > index 019df920..5de1053 100644 > > --- a/arch/riscv/kvm/Makefile > > +++ b/arch/riscv/kvm/Makefile > > @@ -25,3 +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 > > diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c > > index 7c08567..b746f21 100644 > > --- a/arch/riscv/kvm/vcpu.c > > +++ b/arch/riscv/kvm/vcpu.c > > @@ -137,6 +137,7 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) > > > > WRITE_ONCE(vcpu->arch.irqs_pending, 0); > > WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); > > Add an empty newline here. > Done. > > + kvm_riscv_vcpu_pmu_reset(vcpu); > > > > vcpu->arch.hfence_head = 0; > > vcpu->arch.hfence_tail = 0; > > @@ -194,6 +195,9 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) > > /* Setup VCPU timer */ > > kvm_riscv_vcpu_timer_init(vcpu); > > > > + /* setup performance monitoring */ > > + kvm_riscv_vcpu_pmu_init(vcpu); > > + > > /* Reset VCPU */ > > kvm_riscv_reset_vcpu(vcpu); > > > > @@ -216,6 +220,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) > > /* Cleanup VCPU timer */ > > kvm_riscv_vcpu_timer_deinit(vcpu); > > > > + kvm_riscv_vcpu_pmu_deinit(vcpu); > > Add an empty newline here. > Done. > > /* Free unused pages pre-allocated for G-stage page table mappings */ > > kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); > > } > > diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c > > new file mode 100644 > > index 0000000..d3fd551 > > --- /dev/null > > +++ b/arch/riscv/kvm/vcpu_pmu.c > > @@ -0,0 +1,145 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +/* > > + * Copyright (c) 2023 Rivos Inc > > + * > > + * Authors: > > + * Atish Patra <atishp@rivosinc.com> > > + */ > > + > > +#include <linux/errno.h> > > +#include <linux/err.h> > > +#include <linux/kvm_host.h> > > +#include <linux/perf/riscv_pmu.h> > > +#include <asm/csr.h> > > +#include <asm/kvm_vcpu_sbi.h> > > +#include <asm/kvm_vcpu_pmu.h> > > +#include <linux/kvm_host.h> > > + > > +#define kvm_pmu_num_counters(pmu) ((pmu)->num_hw_ctrs + (pmu)->num_fw_ctrs) > > + > > +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata) > > +{ > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > + > > + edata->out_val = kvm_pmu_num_counters(kvpmu); > > + > > + return 0; > > +} > > + > > +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, > > + struct kvm_vcpu_sbi_ext_data *edata) > > +{ > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > + > > + if (cidx > RISCV_MAX_COUNTERS || cidx == 1) { > > + edata->err_val = SBI_ERR_INVALID_PARAM; > > + return 0; > > + } > > + > > + edata->out_val = kvpmu->pmc[cidx].cinfo.value; > > + > > + return 0; > > +} > > + > > +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > + unsigned long ctr_mask, unsigned long flag, uint64_t ival, > > + struct kvm_vcpu_sbi_ext_data *edata) > > +{ > > + /* TODO */ > > + return 0; > > +} > > + > > +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > + unsigned long ctr_mask, unsigned long flag, > > + struct kvm_vcpu_sbi_ext_data *edata) > > +{ > > + /* TODO */ > > + return 0; > > +} > > + > > +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > + unsigned long ctr_mask, unsigned long flag, > > + unsigned long eidx, uint64_t evtdata, > > + struct kvm_vcpu_sbi_ext_data *edata) > > +{ > > + /* TODO */ > > + return 0; > > +} > > + > > +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, > > + struct kvm_vcpu_sbi_ext_data *edata) > > +{ > > + /* TODO */ > > + return 0; > > +} > > + > > +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > > +{ > > + int i = 0, num_fw_ctrs, ret, num_hw_ctrs = 0, hpm_width = 0; > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > + struct kvm_pmc *pmc; > > + > > + ret = riscv_pmu_get_hpm_info(&hpm_width, &num_hw_ctrs); > > + if (ret < 0) > > + return ret; > > + > > + if (!hpm_width || !num_hw_ctrs) { > > + pr_err("Cannot initialize VCPU with NULL hpmcounter width or number of counters\n"); > > What will happen if underlying M-mode firmware does not implement > SBI PMU extension ? > riscv_pmu_get_hpm_info will return an error and kvm_riscv_vcpu_pmu_init will fail. > > + return -EINVAL; > > + } > > + > > + if ((num_hw_ctrs + RISCV_KVM_MAX_FW_CTRS) > RISCV_MAX_COUNTERS) { > > + pr_warn("Limiting fw counters as hw & fw counters exceed maximum counters\n"); > > How is this possible ? > > Maximum HW counters is 32 (including time, cycle, and instret), > RISCV_KVM_MAX_FW_CTRS = 32, and > RISCV_MAX_COUNTERS = 64 This was added to prevent the condition where somebody changed the definition RISCV_KVM_MAX_FW_CTRS without incrementing MAX_COUNTERS. The error might be subtle as it may work on some platforms (with less hardware counter) but fail on others (with more hardware counters.) I couldn't find a better way to describe the relationship between RISCV_KVM_MAX_FW_CTRS and RISCV_MAX_COUNTERS. I can just put a big comment here instead of the condition check if you prefer that way. > > > + num_fw_ctrs = RISCV_MAX_COUNTERS - num_hw_ctrs; > > + } else > > + num_fw_ctrs = RISCV_KVM_MAX_FW_CTRS; > > + > > + kvpmu->num_hw_ctrs = num_hw_ctrs; > > + kvpmu->num_fw_ctrs = num_fw_ctrs; > > + > > + /* > > + * There is no correlation between the logical hardware counter and virtual counters. > > + * However, we need to encode a hpmcounter CSR in the counter info field so that > > + * KVM can trap n emulate the read. This works well in the migration use case as > > + * KVM doesn't care if the actual hpmcounter is available in the hardware or not. > > + */ > > + for (i = 0; i < kvm_pmu_num_counters(kvpmu); i++) { > > + /* TIME CSR shouldn't be read from perf interface */ > > + if (i == 1) > > + continue; > > + pmc = &kvpmu->pmc[i]; > > + pmc->idx = i; > > + if (i < kvpmu->num_hw_ctrs) { > > + kvpmu->pmc[i].cinfo.type = SBI_PMU_CTR_TYPE_HW; > > + if (i < 3) > > + /* CY, IR counters */ > > + kvpmu->pmc[i].cinfo.width = 63; > > + else > > + kvpmu->pmc[i].cinfo.width = hpm_width; > > + /* > > + * The CSR number doesn't have any relation with the logical > > + * hardware counters. The CSR numbers are encoded sequentially > > + * to avoid maintaining a map between the virtual counter > > + * and CSR number. > > + */ > > + pmc->cinfo.csr = CSR_CYCLE + i; > > + } else { > > + pmc->cinfo.type = SBI_PMU_CTR_TYPE_FW; > > + pmc->cinfo.width = BITS_PER_LONG - 1; > > + } > > + } > > + > > + kvpmu->init_done = true; > > + > > + return 0; > > +} > > + > > +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) > > +{ > > + /* TODO */ > > +} > > + > > +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) > > +{ > > + kvm_riscv_vcpu_pmu_deinit(vcpu); > > +} > > -- > > 2.25.1 > > > > Regards, > Anup
On Wed, Feb 1, 2023 at 4:05 AM Atish Patra <atishp@atishpatra.org> wrote: > > On Sun, Jan 29, 2023 at 4:30 AM Anup Patel <anup@brainfault.org> wrote: > > > > On Fri, Jan 27, 2023 at 11:56 PM Atish Patra <atishp@rivosinc.com> wrote: > > > > > > This patch only adds barebore structure of perf implementation. Most of > > > > s/barebore/barebone/ > > > > > the function returns zero at this point and will be implemented > > > fully in the future. > > > > > > Signed-off-by: Atish Patra <atishp@rivosinc.com> > > > --- > > > arch/riscv/include/asm/kvm_host.h | 3 + > > > arch/riscv/include/asm/kvm_vcpu_pmu.h | 76 ++++++++++++++ > > > arch/riscv/kvm/Makefile | 1 + > > > arch/riscv/kvm/vcpu.c | 5 + > > > arch/riscv/kvm/vcpu_pmu.c | 145 ++++++++++++++++++++++++++ > > > 5 files changed, 230 insertions(+) > > > create mode 100644 arch/riscv/include/asm/kvm_vcpu_pmu.h > > > create mode 100644 arch/riscv/kvm/vcpu_pmu.c > > > > > > diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h > > > index 93f43a3..f9874b4 100644 > > > --- a/arch/riscv/include/asm/kvm_host.h > > > +++ b/arch/riscv/include/asm/kvm_host.h > > > @@ -18,6 +18,7 @@ > > > #include <asm/kvm_vcpu_insn.h> > > > #include <asm/kvm_vcpu_sbi.h> > > > #include <asm/kvm_vcpu_timer.h> > > > +#include <asm/kvm_vcpu_pmu.h> > > > > > > #define KVM_MAX_VCPUS 1024 > > > > > > @@ -228,6 +229,8 @@ struct kvm_vcpu_arch { > > > > > > /* Don't run the VCPU (blocked) */ > > > bool pause; > > > + > > > + struct kvm_pmu pmu; > > > > Add a single line comment just like other members of the structure. > > > > I also suggest naming this variable "pmu_context" or something similar > > for naming consistency. > > > > Done. > > > > }; > > > > > > static inline void kvm_arch_hardware_unsetup(void) {} > > > diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h > > > new file mode 100644 > > > index 0000000..3f43a43 > > > --- /dev/null > > > +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h > > > @@ -0,0 +1,76 @@ > > > +/* SPDX-License-Identifier: GPL-2.0-only */ > > > +/* > > > + * Copyright (c) 2023 Rivos Inc > > > + * > > > + * Authors: > > > + * Atish Patra <atishp@rivosinc.com> > > > + */ > > > + > > > +#ifndef __KVM_VCPU_RISCV_PMU_H > > > +#define __KVM_VCPU_RISCV_PMU_H > > > + > > > +#include <linux/perf/riscv_pmu.h> > > > +#include <asm/kvm_vcpu_sbi.h> > > > +#include <asm/sbi.h> > > > + > > > +#ifdef CONFIG_RISCV_PMU_SBI > > > +#define RISCV_KVM_MAX_FW_CTRS 32 > > > +#define RISCV_MAX_COUNTERS 64 > > > + > > > +/* Per virtual pmu counter data */ > > > +struct kvm_pmc { > > > + u8 idx; > > > + struct perf_event *perf_event; > > > + uint64_t counter_val; > > > + union sbi_pmu_ctr_info cinfo; > > > + /* Event monitoring status */ > > > + bool started; > > > +}; > > > + > > > +/* PMU data structure per vcpu */ > > > +struct kvm_pmu { > > > + struct kvm_pmc pmc[RISCV_MAX_COUNTERS]; > > > + /* Number of the virtual firmware counters available */ > > > + int num_fw_ctrs; > > > + /* Number of the virtual hardware counters available */ > > > + int num_hw_ctrs; > > > + /* A flag to indicate that pmu initialization is done */ > > > + bool init_done; > > > + /* Bit map of all the virtual counter used */ > > > + DECLARE_BITMAP(pmc_in_use, RISCV_MAX_COUNTERS); > > > +}; > > > + > > > +#define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu) > > > +#define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu)) > > > + > > > +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata); > > > +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > + unsigned long ctr_mask, unsigned long flag, uint64_t ival, > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > + unsigned long ctr_mask, unsigned long flag, > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > + unsigned long ctr_mask, unsigned long flag, > > > + unsigned long eidx, uint64_t evtdata, > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); > > > +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu); > > > +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu); > > > + > > > +#else > > > +struct kvm_pmu { > > > +}; > > > + > > > +static inline int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > > > +{ > > > + return 0; > > > +} > > > +static inline void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) {} > > > +static inline void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) {} > > > +#endif /* CONFIG_RISCV_PMU_SBI */ > > > +#endif /* !__KVM_VCPU_RISCV_PMU_H */ > > > diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile > > > index 019df920..5de1053 100644 > > > --- a/arch/riscv/kvm/Makefile > > > +++ b/arch/riscv/kvm/Makefile > > > @@ -25,3 +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 > > > diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c > > > index 7c08567..b746f21 100644 > > > --- a/arch/riscv/kvm/vcpu.c > > > +++ b/arch/riscv/kvm/vcpu.c > > > @@ -137,6 +137,7 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) > > > > > > WRITE_ONCE(vcpu->arch.irqs_pending, 0); > > > WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); > > > > Add an empty newline here. > > > > Done. > > > + kvm_riscv_vcpu_pmu_reset(vcpu); > > > > > > vcpu->arch.hfence_head = 0; > > > vcpu->arch.hfence_tail = 0; > > > @@ -194,6 +195,9 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) > > > /* Setup VCPU timer */ > > > kvm_riscv_vcpu_timer_init(vcpu); > > > > > > + /* setup performance monitoring */ > > > + kvm_riscv_vcpu_pmu_init(vcpu); > > > + > > > /* Reset VCPU */ > > > kvm_riscv_reset_vcpu(vcpu); > > > > > > @@ -216,6 +220,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) > > > /* Cleanup VCPU timer */ > > > kvm_riscv_vcpu_timer_deinit(vcpu); > > > > > > + kvm_riscv_vcpu_pmu_deinit(vcpu); > > > > Add an empty newline here. > > > > Done. > > > > /* Free unused pages pre-allocated for G-stage page table mappings */ > > > kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); > > > } > > > diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c > > > new file mode 100644 > > > index 0000000..d3fd551 > > > --- /dev/null > > > +++ b/arch/riscv/kvm/vcpu_pmu.c > > > @@ -0,0 +1,145 @@ > > > +// SPDX-License-Identifier: GPL-2.0 > > > +/* > > > + * Copyright (c) 2023 Rivos Inc > > > + * > > > + * Authors: > > > + * Atish Patra <atishp@rivosinc.com> > > > + */ > > > + > > > +#include <linux/errno.h> > > > +#include <linux/err.h> > > > +#include <linux/kvm_host.h> > > > +#include <linux/perf/riscv_pmu.h> > > > +#include <asm/csr.h> > > > +#include <asm/kvm_vcpu_sbi.h> > > > +#include <asm/kvm_vcpu_pmu.h> > > > +#include <linux/kvm_host.h> > > > + > > > +#define kvm_pmu_num_counters(pmu) ((pmu)->num_hw_ctrs + (pmu)->num_fw_ctrs) > > > + > > > +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata) > > > +{ > > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > > + > > > + edata->out_val = kvm_pmu_num_counters(kvpmu); > > > + > > > + return 0; > > > +} > > > + > > > +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > +{ > > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > > + > > > + if (cidx > RISCV_MAX_COUNTERS || cidx == 1) { > > > + edata->err_val = SBI_ERR_INVALID_PARAM; > > > + return 0; > > > + } > > > + > > > + edata->out_val = kvpmu->pmc[cidx].cinfo.value; > > > + > > > + return 0; > > > +} > > > + > > > +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > + unsigned long ctr_mask, unsigned long flag, uint64_t ival, > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > +{ > > > + /* TODO */ > > > + return 0; > > > +} > > > + > > > +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > + unsigned long ctr_mask, unsigned long flag, > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > +{ > > > + /* TODO */ > > > + return 0; > > > +} > > > + > > > +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > + unsigned long ctr_mask, unsigned long flag, > > > + unsigned long eidx, uint64_t evtdata, > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > +{ > > > + /* TODO */ > > > + return 0; > > > +} > > > + > > > +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > +{ > > > + /* TODO */ > > > + return 0; > > > +} > > > + > > > +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > > > +{ > > > + int i = 0, num_fw_ctrs, ret, num_hw_ctrs = 0, hpm_width = 0; > > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > > + struct kvm_pmc *pmc; > > > + > > > + ret = riscv_pmu_get_hpm_info(&hpm_width, &num_hw_ctrs); > > > + if (ret < 0) > > > + return ret; > > > + > > > + if (!hpm_width || !num_hw_ctrs) { > > > + pr_err("Cannot initialize VCPU with NULL hpmcounter width or number of counters\n"); > > > > What will happen if underlying M-mode firmware does not implement > > SBI PMU extension ? > > > > riscv_pmu_get_hpm_info will return an error and > kvm_riscv_vcpu_pmu_init will fail. Ahh okay. The vcpu_create() is not looking at return value of kvm_riscv_vcpu_pmu_init() but this function should fail silently "return 0" if riscv_pmu_get_hpm_info() fails. > > > > + return -EINVAL; > > > + } > > > + > > > + if ((num_hw_ctrs + RISCV_KVM_MAX_FW_CTRS) > RISCV_MAX_COUNTERS) { > > > + pr_warn("Limiting fw counters as hw & fw counters exceed maximum counters\n"); > > > > How is this possible ? > > > > Maximum HW counters is 32 (including time, cycle, and instret), > > RISCV_KVM_MAX_FW_CTRS = 32, and > > RISCV_MAX_COUNTERS = 64 > > This was added to prevent the condition where somebody changed the > definition RISCV_KVM_MAX_FW_CTRS > without incrementing MAX_COUNTERS. The error might be subtle as it may > work on some platforms (with less hardware counter) > but fail on others (with more hardware counters.) > I couldn't find a better way to describe the relationship between > RISCV_KVM_MAX_FW_CTRS and RISCV_MAX_COUNTERS. > > I can just put a big comment here instead of the condition check if > you prefer that way. Maybe you can add a compile-time check using "#if" and "#error" ? > > > > > > > + num_fw_ctrs = RISCV_MAX_COUNTERS - num_hw_ctrs; > > > + } else > > > + num_fw_ctrs = RISCV_KVM_MAX_FW_CTRS; > > > + > > > + kvpmu->num_hw_ctrs = num_hw_ctrs; > > > + kvpmu->num_fw_ctrs = num_fw_ctrs; > > > + > > > + /* > > > + * There is no correlation between the logical hardware counter and virtual counters. > > > + * However, we need to encode a hpmcounter CSR in the counter info field so that > > > + * KVM can trap n emulate the read. This works well in the migration use case as > > > + * KVM doesn't care if the actual hpmcounter is available in the hardware or not. > > > + */ > > > + for (i = 0; i < kvm_pmu_num_counters(kvpmu); i++) { > > > + /* TIME CSR shouldn't be read from perf interface */ > > > + if (i == 1) > > > + continue; > > > + pmc = &kvpmu->pmc[i]; > > > + pmc->idx = i; > > > + if (i < kvpmu->num_hw_ctrs) { > > > + kvpmu->pmc[i].cinfo.type = SBI_PMU_CTR_TYPE_HW; > > > + if (i < 3) > > > + /* CY, IR counters */ > > > + kvpmu->pmc[i].cinfo.width = 63; > > > + else > > > + kvpmu->pmc[i].cinfo.width = hpm_width; > > > + /* > > > + * The CSR number doesn't have any relation with the logical > > > + * hardware counters. The CSR numbers are encoded sequentially > > > + * to avoid maintaining a map between the virtual counter > > > + * and CSR number. > > > + */ > > > + pmc->cinfo.csr = CSR_CYCLE + i; > > > + } else { > > > + pmc->cinfo.type = SBI_PMU_CTR_TYPE_FW; > > > + pmc->cinfo.width = BITS_PER_LONG - 1; > > > + } > > > + } > > > + > > > + kvpmu->init_done = true; > > > + > > > + return 0; > > > +} > > > + > > > +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) > > > +{ > > > + /* TODO */ > > > +} > > > + > > > +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) > > > +{ > > > + kvm_riscv_vcpu_pmu_deinit(vcpu); > > > +} > > > -- > > > 2.25.1 > > > > > > > Regards, > > Anup > > > > -- > Regards, > Atish Regards, Anup
On Tue, Jan 31, 2023 at 7:48 PM Anup Patel <apatel@ventanamicro.com> wrote: > > On Wed, Feb 1, 2023 at 4:05 AM Atish Patra <atishp@atishpatra.org> wrote: > > > > On Sun, Jan 29, 2023 at 4:30 AM Anup Patel <anup@brainfault.org> wrote: > > > > > > On Fri, Jan 27, 2023 at 11:56 PM Atish Patra <atishp@rivosinc.com> wrote: > > > > > > > > This patch only adds barebore structure of perf implementation. Most of > > > > > > s/barebore/barebone/ > > > > > > > the function returns zero at this point and will be implemented > > > > fully in the future. > > > > > > > > Signed-off-by: Atish Patra <atishp@rivosinc.com> > > > > --- > > > > arch/riscv/include/asm/kvm_host.h | 3 + > > > > arch/riscv/include/asm/kvm_vcpu_pmu.h | 76 ++++++++++++++ > > > > arch/riscv/kvm/Makefile | 1 + > > > > arch/riscv/kvm/vcpu.c | 5 + > > > > arch/riscv/kvm/vcpu_pmu.c | 145 ++++++++++++++++++++++++++ > > > > 5 files changed, 230 insertions(+) > > > > create mode 100644 arch/riscv/include/asm/kvm_vcpu_pmu.h > > > > create mode 100644 arch/riscv/kvm/vcpu_pmu.c > > > > > > > > diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h > > > > index 93f43a3..f9874b4 100644 > > > > --- a/arch/riscv/include/asm/kvm_host.h > > > > +++ b/arch/riscv/include/asm/kvm_host.h > > > > @@ -18,6 +18,7 @@ > > > > #include <asm/kvm_vcpu_insn.h> > > > > #include <asm/kvm_vcpu_sbi.h> > > > > #include <asm/kvm_vcpu_timer.h> > > > > +#include <asm/kvm_vcpu_pmu.h> > > > > > > > > #define KVM_MAX_VCPUS 1024 > > > > > > > > @@ -228,6 +229,8 @@ struct kvm_vcpu_arch { > > > > > > > > /* Don't run the VCPU (blocked) */ > > > > bool pause; > > > > + > > > > + struct kvm_pmu pmu; > > > > > > Add a single line comment just like other members of the structure. > > > > > > I also suggest naming this variable "pmu_context" or something similar > > > for naming consistency. > > > > > > > Done. > > > > > > }; > > > > > > > > static inline void kvm_arch_hardware_unsetup(void) {} > > > > diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h > > > > new file mode 100644 > > > > index 0000000..3f43a43 > > > > --- /dev/null > > > > +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h > > > > @@ -0,0 +1,76 @@ > > > > +/* SPDX-License-Identifier: GPL-2.0-only */ > > > > +/* > > > > + * Copyright (c) 2023 Rivos Inc > > > > + * > > > > + * Authors: > > > > + * Atish Patra <atishp@rivosinc.com> > > > > + */ > > > > + > > > > +#ifndef __KVM_VCPU_RISCV_PMU_H > > > > +#define __KVM_VCPU_RISCV_PMU_H > > > > + > > > > +#include <linux/perf/riscv_pmu.h> > > > > +#include <asm/kvm_vcpu_sbi.h> > > > > +#include <asm/sbi.h> > > > > + > > > > +#ifdef CONFIG_RISCV_PMU_SBI > > > > +#define RISCV_KVM_MAX_FW_CTRS 32 > > > > +#define RISCV_MAX_COUNTERS 64 > > > > + > > > > +/* Per virtual pmu counter data */ > > > > +struct kvm_pmc { > > > > + u8 idx; > > > > + struct perf_event *perf_event; > > > > + uint64_t counter_val; > > > > + union sbi_pmu_ctr_info cinfo; > > > > + /* Event monitoring status */ > > > > + bool started; > > > > +}; > > > > + > > > > +/* PMU data structure per vcpu */ > > > > +struct kvm_pmu { > > > > + struct kvm_pmc pmc[RISCV_MAX_COUNTERS]; > > > > + /* Number of the virtual firmware counters available */ > > > > + int num_fw_ctrs; > > > > + /* Number of the virtual hardware counters available */ > > > > + int num_hw_ctrs; > > > > + /* A flag to indicate that pmu initialization is done */ > > > > + bool init_done; > > > > + /* Bit map of all the virtual counter used */ > > > > + DECLARE_BITMAP(pmc_in_use, RISCV_MAX_COUNTERS); > > > > +}; > > > > + > > > > +#define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu) > > > > +#define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu)) > > > > + > > > > +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata); > > > > +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, > > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > > +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > + unsigned long ctr_mask, unsigned long flag, uint64_t ival, > > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > > +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > + unsigned long ctr_mask, unsigned long flag, > > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > > +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > + unsigned long ctr_mask, unsigned long flag, > > > > + unsigned long eidx, uint64_t evtdata, > > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > > +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, > > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > > +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); > > > > +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu); > > > > +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu); > > > > + > > > > +#else > > > > +struct kvm_pmu { > > > > +}; > > > > + > > > > +static inline int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > > > > +{ > > > > + return 0; > > > > +} > > > > +static inline void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) {} > > > > +static inline void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) {} > > > > +#endif /* CONFIG_RISCV_PMU_SBI */ > > > > +#endif /* !__KVM_VCPU_RISCV_PMU_H */ > > > > diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile > > > > index 019df920..5de1053 100644 > > > > --- a/arch/riscv/kvm/Makefile > > > > +++ b/arch/riscv/kvm/Makefile > > > > @@ -25,3 +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 > > > > diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c > > > > index 7c08567..b746f21 100644 > > > > --- a/arch/riscv/kvm/vcpu.c > > > > +++ b/arch/riscv/kvm/vcpu.c > > > > @@ -137,6 +137,7 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) > > > > > > > > WRITE_ONCE(vcpu->arch.irqs_pending, 0); > > > > WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); > > > > > > Add an empty newline here. > > > > > > > Done. > > > > + kvm_riscv_vcpu_pmu_reset(vcpu); > > > > > > > > vcpu->arch.hfence_head = 0; > > > > vcpu->arch.hfence_tail = 0; > > > > @@ -194,6 +195,9 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) > > > > /* Setup VCPU timer */ > > > > kvm_riscv_vcpu_timer_init(vcpu); > > > > > > > > + /* setup performance monitoring */ > > > > + kvm_riscv_vcpu_pmu_init(vcpu); > > > > + > > > > /* Reset VCPU */ > > > > kvm_riscv_reset_vcpu(vcpu); > > > > > > > > @@ -216,6 +220,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) > > > > /* Cleanup VCPU timer */ > > > > kvm_riscv_vcpu_timer_deinit(vcpu); > > > > > > > > + kvm_riscv_vcpu_pmu_deinit(vcpu); > > > > > > Add an empty newline here. > > > > > > > Done. > > > > > > /* Free unused pages pre-allocated for G-stage page table mappings */ > > > > kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); > > > > } > > > > diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c > > > > new file mode 100644 > > > > index 0000000..d3fd551 > > > > --- /dev/null > > > > +++ b/arch/riscv/kvm/vcpu_pmu.c > > > > @@ -0,0 +1,145 @@ > > > > +// SPDX-License-Identifier: GPL-2.0 > > > > +/* > > > > + * Copyright (c) 2023 Rivos Inc > > > > + * > > > > + * Authors: > > > > + * Atish Patra <atishp@rivosinc.com> > > > > + */ > > > > + > > > > +#include <linux/errno.h> > > > > +#include <linux/err.h> > > > > +#include <linux/kvm_host.h> > > > > +#include <linux/perf/riscv_pmu.h> > > > > +#include <asm/csr.h> > > > > +#include <asm/kvm_vcpu_sbi.h> > > > > +#include <asm/kvm_vcpu_pmu.h> > > > > +#include <linux/kvm_host.h> > > > > + > > > > +#define kvm_pmu_num_counters(pmu) ((pmu)->num_hw_ctrs + (pmu)->num_fw_ctrs) > > > > + > > > > +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata) > > > > +{ > > > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > > > + > > > > + edata->out_val = kvm_pmu_num_counters(kvpmu); > > > > + > > > > + return 0; > > > > +} > > > > + > > > > +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, > > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > > +{ > > > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > > > + > > > > + if (cidx > RISCV_MAX_COUNTERS || cidx == 1) { > > > > + edata->err_val = SBI_ERR_INVALID_PARAM; > > > > + return 0; > > > > + } > > > > + > > > > + edata->out_val = kvpmu->pmc[cidx].cinfo.value; > > > > + > > > > + return 0; > > > > +} > > > > + > > > > +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > + unsigned long ctr_mask, unsigned long flag, uint64_t ival, > > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > > +{ > > > > + /* TODO */ > > > > + return 0; > > > > +} > > > > + > > > > +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > + unsigned long ctr_mask, unsigned long flag, > > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > > +{ > > > > + /* TODO */ > > > > + return 0; > > > > +} > > > > + > > > > +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > + unsigned long ctr_mask, unsigned long flag, > > > > + unsigned long eidx, uint64_t evtdata, > > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > > +{ > > > > + /* TODO */ > > > > + return 0; > > > > +} > > > > + > > > > +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, > > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > > +{ > > > > + /* TODO */ > > > > + return 0; > > > > +} > > > > + > > > > +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > > > > +{ > > > > + int i = 0, num_fw_ctrs, ret, num_hw_ctrs = 0, hpm_width = 0; > > > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > > > + struct kvm_pmc *pmc; > > > > + > > > > + ret = riscv_pmu_get_hpm_info(&hpm_width, &num_hw_ctrs); > > > > + if (ret < 0) > > > > + return ret; > > > > + > > > > + if (!hpm_width || !num_hw_ctrs) { > > > > + pr_err("Cannot initialize VCPU with NULL hpmcounter width or number of counters\n"); > > > > > > What will happen if underlying M-mode firmware does not implement > > > SBI PMU extension ? > > > > > > > riscv_pmu_get_hpm_info will return an error and > > kvm_riscv_vcpu_pmu_init will fail. > > Ahh okay. The vcpu_create() is not looking at return value > of kvm_riscv_vcpu_pmu_init() but this function should fail > silently "return 0" if riscv_pmu_get_hpm_info() fails. > Sure. I am assuming it should return 0 for the next case as well. if (!hpm_width || !num_hw_ctrs) Thinking over again, does it make sense to print the error message in the above case ? In case you do see some use of this error message, it probably needs improvement as well "Cannot initialize pmu for vcpu %d with invalid counter width or number of counters" > > > > > > + return -EINVAL; > > > > + } > > > > + > > > > + if ((num_hw_ctrs + RISCV_KVM_MAX_FW_CTRS) > RISCV_MAX_COUNTERS) { > > > > + pr_warn("Limiting fw counters as hw & fw counters exceed maximum counters\n"); > > > > > > How is this possible ? > > > > > > Maximum HW counters is 32 (including time, cycle, and instret), > > > RISCV_KVM_MAX_FW_CTRS = 32, and > > > RISCV_MAX_COUNTERS = 64 > > > > This was added to prevent the condition where somebody changed the > > definition RISCV_KVM_MAX_FW_CTRS > > without incrementing MAX_COUNTERS. The error might be subtle as it may > > work on some platforms (with less hardware counter) > > but fail on others (with more hardware counters.) > > I couldn't find a better way to describe the relationship between > > RISCV_KVM_MAX_FW_CTRS and RISCV_MAX_COUNTERS. > > > > I can just put a big comment here instead of the condition check if > > you prefer that way. > > Maybe you can add a compile-time check using "#if" and "#error" ? > You mean compile time error when RISCV_KVM_MAX_FW_CTRS > 32 ? Because num_hw_ctrs is computed at the runtime. So we can't determine the actual limit of firmware counters unless we know how many hardware counters are there. > > > > > > > > > > > + num_fw_ctrs = RISCV_MAX_COUNTERS - num_hw_ctrs; > > > > + } else > > > > + num_fw_ctrs = RISCV_KVM_MAX_FW_CTRS; > > > > + > > > > + kvpmu->num_hw_ctrs = num_hw_ctrs; > > > > + kvpmu->num_fw_ctrs = num_fw_ctrs; > > > > + > > > > + /* > > > > + * There is no correlation between the logical hardware counter and virtual counters. > > > > + * However, we need to encode a hpmcounter CSR in the counter info field so that > > > > + * KVM can trap n emulate the read. This works well in the migration use case as > > > > + * KVM doesn't care if the actual hpmcounter is available in the hardware or not. > > > > + */ > > > > + for (i = 0; i < kvm_pmu_num_counters(kvpmu); i++) { > > > > + /* TIME CSR shouldn't be read from perf interface */ > > > > + if (i == 1) > > > > + continue; > > > > + pmc = &kvpmu->pmc[i]; > > > > + pmc->idx = i; > > > > + if (i < kvpmu->num_hw_ctrs) { > > > > + kvpmu->pmc[i].cinfo.type = SBI_PMU_CTR_TYPE_HW; > > > > + if (i < 3) > > > > + /* CY, IR counters */ > > > > + kvpmu->pmc[i].cinfo.width = 63; > > > > + else > > > > + kvpmu->pmc[i].cinfo.width = hpm_width; > > > > + /* > > > > + * The CSR number doesn't have any relation with the logical > > > > + * hardware counters. The CSR numbers are encoded sequentially > > > > + * to avoid maintaining a map between the virtual counter > > > > + * and CSR number. > > > > + */ > > > > + pmc->cinfo.csr = CSR_CYCLE + i; > > > > + } else { > > > > + pmc->cinfo.type = SBI_PMU_CTR_TYPE_FW; > > > > + pmc->cinfo.width = BITS_PER_LONG - 1; > > > > + } > > > > + } > > > > + > > > > + kvpmu->init_done = true; > > > > + > > > > + return 0; > > > > +} > > > > + > > > > +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) > > > > +{ > > > > + /* TODO */ > > > > +} > > > > + > > > > +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) > > > > +{ > > > > + kvm_riscv_vcpu_pmu_deinit(vcpu); > > > > +} > > > > -- > > > > 2.25.1 > > > > > > > > > > Regards, > > > Anup > > > > > > > > -- > > Regards, > > Atish > > Regards, > Anup
On Wed, Feb 1, 2023 at 2:11 PM Atish Patra <atishp@atishpatra.org> wrote: > > On Tue, Jan 31, 2023 at 7:48 PM Anup Patel <apatel@ventanamicro.com> wrote: > > > > On Wed, Feb 1, 2023 at 4:05 AM Atish Patra <atishp@atishpatra.org> wrote: > > > > > > On Sun, Jan 29, 2023 at 4:30 AM Anup Patel <anup@brainfault.org> wrote: > > > > > > > > On Fri, Jan 27, 2023 at 11:56 PM Atish Patra <atishp@rivosinc.com> wrote: > > > > > > > > > > This patch only adds barebore structure of perf implementation. Most of > > > > > > > > s/barebore/barebone/ > > > > > > > > > the function returns zero at this point and will be implemented > > > > > fully in the future. > > > > > > > > > > Signed-off-by: Atish Patra <atishp@rivosinc.com> > > > > > --- > > > > > arch/riscv/include/asm/kvm_host.h | 3 + > > > > > arch/riscv/include/asm/kvm_vcpu_pmu.h | 76 ++++++++++++++ > > > > > arch/riscv/kvm/Makefile | 1 + > > > > > arch/riscv/kvm/vcpu.c | 5 + > > > > > arch/riscv/kvm/vcpu_pmu.c | 145 ++++++++++++++++++++++++++ > > > > > 5 files changed, 230 insertions(+) > > > > > create mode 100644 arch/riscv/include/asm/kvm_vcpu_pmu.h > > > > > create mode 100644 arch/riscv/kvm/vcpu_pmu.c > > > > > > > > > > diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h > > > > > index 93f43a3..f9874b4 100644 > > > > > --- a/arch/riscv/include/asm/kvm_host.h > > > > > +++ b/arch/riscv/include/asm/kvm_host.h > > > > > @@ -18,6 +18,7 @@ > > > > > #include <asm/kvm_vcpu_insn.h> > > > > > #include <asm/kvm_vcpu_sbi.h> > > > > > #include <asm/kvm_vcpu_timer.h> > > > > > +#include <asm/kvm_vcpu_pmu.h> > > > > > > > > > > #define KVM_MAX_VCPUS 1024 > > > > > > > > > > @@ -228,6 +229,8 @@ struct kvm_vcpu_arch { > > > > > > > > > > /* Don't run the VCPU (blocked) */ > > > > > bool pause; > > > > > + > > > > > + struct kvm_pmu pmu; > > > > > > > > Add a single line comment just like other members of the structure. > > > > > > > > I also suggest naming this variable "pmu_context" or something similar > > > > for naming consistency. > > > > > > > > > > Done. > > > > > > > > }; > > > > > > > > > > static inline void kvm_arch_hardware_unsetup(void) {} > > > > > diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h > > > > > new file mode 100644 > > > > > index 0000000..3f43a43 > > > > > --- /dev/null > > > > > +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h > > > > > @@ -0,0 +1,76 @@ > > > > > +/* SPDX-License-Identifier: GPL-2.0-only */ > > > > > +/* > > > > > + * Copyright (c) 2023 Rivos Inc > > > > > + * > > > > > + * Authors: > > > > > + * Atish Patra <atishp@rivosinc.com> > > > > > + */ > > > > > + > > > > > +#ifndef __KVM_VCPU_RISCV_PMU_H > > > > > +#define __KVM_VCPU_RISCV_PMU_H > > > > > + > > > > > +#include <linux/perf/riscv_pmu.h> > > > > > +#include <asm/kvm_vcpu_sbi.h> > > > > > +#include <asm/sbi.h> > > > > > + > > > > > +#ifdef CONFIG_RISCV_PMU_SBI > > > > > +#define RISCV_KVM_MAX_FW_CTRS 32 > > > > > +#define RISCV_MAX_COUNTERS 64 > > > > > + > > > > > +/* Per virtual pmu counter data */ > > > > > +struct kvm_pmc { > > > > > + u8 idx; > > > > > + struct perf_event *perf_event; > > > > > + uint64_t counter_val; > > > > > + union sbi_pmu_ctr_info cinfo; > > > > > + /* Event monitoring status */ > > > > > + bool started; > > > > > +}; > > > > > + > > > > > +/* PMU data structure per vcpu */ > > > > > +struct kvm_pmu { > > > > > + struct kvm_pmc pmc[RISCV_MAX_COUNTERS]; > > > > > + /* Number of the virtual firmware counters available */ > > > > > + int num_fw_ctrs; > > > > > + /* Number of the virtual hardware counters available */ > > > > > + int num_hw_ctrs; > > > > > + /* A flag to indicate that pmu initialization is done */ > > > > > + bool init_done; > > > > > + /* Bit map of all the virtual counter used */ > > > > > + DECLARE_BITMAP(pmc_in_use, RISCV_MAX_COUNTERS); > > > > > +}; > > > > > + > > > > > +#define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu) > > > > > +#define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu)) > > > > > + > > > > > +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata); > > > > > +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, > > > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > > > +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > > + unsigned long ctr_mask, unsigned long flag, uint64_t ival, > > > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > > > +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > > + unsigned long ctr_mask, unsigned long flag, > > > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > > > +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > > + unsigned long ctr_mask, unsigned long flag, > > > > > + unsigned long eidx, uint64_t evtdata, > > > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > > > +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, > > > > > + struct kvm_vcpu_sbi_ext_data *edata); > > > > > +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); > > > > > +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu); > > > > > +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu); > > > > > + > > > > > +#else > > > > > +struct kvm_pmu { > > > > > +}; > > > > > + > > > > > +static inline int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > > > > > +{ > > > > > + return 0; > > > > > +} > > > > > +static inline void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) {} > > > > > +static inline void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) {} > > > > > +#endif /* CONFIG_RISCV_PMU_SBI */ > > > > > +#endif /* !__KVM_VCPU_RISCV_PMU_H */ > > > > > diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile > > > > > index 019df920..5de1053 100644 > > > > > --- a/arch/riscv/kvm/Makefile > > > > > +++ b/arch/riscv/kvm/Makefile > > > > > @@ -25,3 +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 > > > > > diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c > > > > > index 7c08567..b746f21 100644 > > > > > --- a/arch/riscv/kvm/vcpu.c > > > > > +++ b/arch/riscv/kvm/vcpu.c > > > > > @@ -137,6 +137,7 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) > > > > > > > > > > WRITE_ONCE(vcpu->arch.irqs_pending, 0); > > > > > WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); > > > > > > > > Add an empty newline here. > > > > > > > > > > Done. > > > > > + kvm_riscv_vcpu_pmu_reset(vcpu); > > > > > > > > > > vcpu->arch.hfence_head = 0; > > > > > vcpu->arch.hfence_tail = 0; > > > > > @@ -194,6 +195,9 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) > > > > > /* Setup VCPU timer */ > > > > > kvm_riscv_vcpu_timer_init(vcpu); > > > > > > > > > > + /* setup performance monitoring */ > > > > > + kvm_riscv_vcpu_pmu_init(vcpu); > > > > > + > > > > > /* Reset VCPU */ > > > > > kvm_riscv_reset_vcpu(vcpu); > > > > > > > > > > @@ -216,6 +220,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) > > > > > /* Cleanup VCPU timer */ > > > > > kvm_riscv_vcpu_timer_deinit(vcpu); > > > > > > > > > > + kvm_riscv_vcpu_pmu_deinit(vcpu); > > > > > > > > Add an empty newline here. > > > > > > > > > > Done. > > > > > > > > /* Free unused pages pre-allocated for G-stage page table mappings */ > > > > > kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); > > > > > } > > > > > diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c > > > > > new file mode 100644 > > > > > index 0000000..d3fd551 > > > > > --- /dev/null > > > > > +++ b/arch/riscv/kvm/vcpu_pmu.c > > > > > @@ -0,0 +1,145 @@ > > > > > +// SPDX-License-Identifier: GPL-2.0 > > > > > +/* > > > > > + * Copyright (c) 2023 Rivos Inc > > > > > + * > > > > > + * Authors: > > > > > + * Atish Patra <atishp@rivosinc.com> > > > > > + */ > > > > > + > > > > > +#include <linux/errno.h> > > > > > +#include <linux/err.h> > > > > > +#include <linux/kvm_host.h> > > > > > +#include <linux/perf/riscv_pmu.h> > > > > > +#include <asm/csr.h> > > > > > +#include <asm/kvm_vcpu_sbi.h> > > > > > +#include <asm/kvm_vcpu_pmu.h> > > > > > +#include <linux/kvm_host.h> > > > > > + > > > > > +#define kvm_pmu_num_counters(pmu) ((pmu)->num_hw_ctrs + (pmu)->num_fw_ctrs) > > > > > + > > > > > +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata) > > > > > +{ > > > > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > > > > + > > > > > + edata->out_val = kvm_pmu_num_counters(kvpmu); > > > > > + > > > > > + return 0; > > > > > +} > > > > > + > > > > > +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, > > > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > > > +{ > > > > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > > > > + > > > > > + if (cidx > RISCV_MAX_COUNTERS || cidx == 1) { > > > > > + edata->err_val = SBI_ERR_INVALID_PARAM; > > > > > + return 0; > > > > > + } > > > > > + > > > > > + edata->out_val = kvpmu->pmc[cidx].cinfo.value; > > > > > + > > > > > + return 0; > > > > > +} > > > > > + > > > > > +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > > + unsigned long ctr_mask, unsigned long flag, uint64_t ival, > > > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > > > +{ > > > > > + /* TODO */ > > > > > + return 0; > > > > > +} > > > > > + > > > > > +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > > + unsigned long ctr_mask, unsigned long flag, > > > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > > > +{ > > > > > + /* TODO */ > > > > > + return 0; > > > > > +} > > > > > + > > > > > +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, > > > > > + unsigned long ctr_mask, unsigned long flag, > > > > > + unsigned long eidx, uint64_t evtdata, > > > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > > > +{ > > > > > + /* TODO */ > > > > > + return 0; > > > > > +} > > > > > + > > > > > +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, > > > > > + struct kvm_vcpu_sbi_ext_data *edata) > > > > > +{ > > > > > + /* TODO */ > > > > > + return 0; > > > > > +} > > > > > + > > > > > +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > > > > > +{ > > > > > + int i = 0, num_fw_ctrs, ret, num_hw_ctrs = 0, hpm_width = 0; > > > > > + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); > > > > > + struct kvm_pmc *pmc; > > > > > + > > > > > + ret = riscv_pmu_get_hpm_info(&hpm_width, &num_hw_ctrs); > > > > > + if (ret < 0) > > > > > + return ret; > > > > > + > > > > > + if (!hpm_width || !num_hw_ctrs) { > > > > > + pr_err("Cannot initialize VCPU with NULL hpmcounter width or number of counters\n"); > > > > > > > > What will happen if underlying M-mode firmware does not implement > > > > SBI PMU extension ? > > > > > > > > > > riscv_pmu_get_hpm_info will return an error and > > > kvm_riscv_vcpu_pmu_init will fail. > > > > Ahh okay. The vcpu_create() is not looking at return value > > of kvm_riscv_vcpu_pmu_init() but this function should fail > > silently "return 0" if riscv_pmu_get_hpm_info() fails. > > > > Sure. I am assuming it should return 0 for the next case as well. > if (!hpm_width || !num_hw_ctrs) Yes, I agree. Better to make kvm_riscv_vcpu_pmu_init() return void. > > Thinking over again, does it make sense to print the error message in > the above case ? > In case you do see some use of this error message, it probably needs > improvement as well > > "Cannot initialize pmu for vcpu %d with invalid counter width or > number of counters" For now, let's not have any error message in kvm_riscv_vcpu_pmu_init(). > > > > > > > > > + return -EINVAL; > > > > > + } > > > > > + > > > > > + if ((num_hw_ctrs + RISCV_KVM_MAX_FW_CTRS) > RISCV_MAX_COUNTERS) { > > > > > + pr_warn("Limiting fw counters as hw & fw counters exceed maximum counters\n"); > > > > > > > > How is this possible ? > > > > > > > > Maximum HW counters is 32 (including time, cycle, and instret), > > > > RISCV_KVM_MAX_FW_CTRS = 32, and > > > > RISCV_MAX_COUNTERS = 64 > > > > > > This was added to prevent the condition where somebody changed the > > > definition RISCV_KVM_MAX_FW_CTRS > > > without incrementing MAX_COUNTERS. The error might be subtle as it may > > > work on some platforms (with less hardware counter) > > > but fail on others (with more hardware counters.) > > > I couldn't find a better way to describe the relationship between > > > RISCV_KVM_MAX_FW_CTRS and RISCV_MAX_COUNTERS. > > > > > > I can just put a big comment here instead of the condition check if > > > you prefer that way. > > > > Maybe you can add a compile-time check using "#if" and "#error" ? > > > > You mean compile time error when RISCV_KVM_MAX_FW_CTRS > 32 ? > Because num_hw_ctrs is computed at the runtime. So we can't determine > the actual limit of firmware counters unless we know how many hardware > counters are there. Yes, we should ensure that RISCV_KVM_MAX_FW_CTRS > 32 does not compile with a proper error message. > > > > > > > > > > > > > > > > + num_fw_ctrs = RISCV_MAX_COUNTERS - num_hw_ctrs; > > > > > + } else > > > > > + num_fw_ctrs = RISCV_KVM_MAX_FW_CTRS; > > > > > + > > > > > + kvpmu->num_hw_ctrs = num_hw_ctrs; > > > > > + kvpmu->num_fw_ctrs = num_fw_ctrs; > > > > > + > > > > > + /* > > > > > + * There is no correlation between the logical hardware counter and virtual counters. > > > > > + * However, we need to encode a hpmcounter CSR in the counter info field so that > > > > > + * KVM can trap n emulate the read. This works well in the migration use case as > > > > > + * KVM doesn't care if the actual hpmcounter is available in the hardware or not. > > > > > + */ > > > > > + for (i = 0; i < kvm_pmu_num_counters(kvpmu); i++) { > > > > > + /* TIME CSR shouldn't be read from perf interface */ > > > > > + if (i == 1) > > > > > + continue; > > > > > + pmc = &kvpmu->pmc[i]; > > > > > + pmc->idx = i; > > > > > + if (i < kvpmu->num_hw_ctrs) { > > > > > + kvpmu->pmc[i].cinfo.type = SBI_PMU_CTR_TYPE_HW; > > > > > + if (i < 3) > > > > > + /* CY, IR counters */ > > > > > + kvpmu->pmc[i].cinfo.width = 63; > > > > > + else > > > > > + kvpmu->pmc[i].cinfo.width = hpm_width; > > > > > + /* > > > > > + * The CSR number doesn't have any relation with the logical > > > > > + * hardware counters. The CSR numbers are encoded sequentially > > > > > + * to avoid maintaining a map between the virtual counter > > > > > + * and CSR number. > > > > > + */ > > > > > + pmc->cinfo.csr = CSR_CYCLE + i; > > > > > + } else { > > > > > + pmc->cinfo.type = SBI_PMU_CTR_TYPE_FW; > > > > > + pmc->cinfo.width = BITS_PER_LONG - 1; > > > > > + } > > > > > + } > > > > > + > > > > > + kvpmu->init_done = true; > > > > > + > > > > > + return 0; > > > > > +} > > > > > + > > > > > +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) > > > > > +{ > > > > > + /* TODO */ > > > > > +} > > > > > + > > > > > +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) > > > > > +{ > > > > > + kvm_riscv_vcpu_pmu_deinit(vcpu); > > > > > +} > > > > > -- > > > > > 2.25.1 > > > > > > > > > > > > > Regards, > > > > Anup > > > > > > > > > > > > -- > > > Regards, > > > Atish > > > > Regards, > > Anup > > > > -- > Regards, > Atish Regards, Anup
diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 93f43a3..f9874b4 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -18,6 +18,7 @@ #include <asm/kvm_vcpu_insn.h> #include <asm/kvm_vcpu_sbi.h> #include <asm/kvm_vcpu_timer.h> +#include <asm/kvm_vcpu_pmu.h> #define KVM_MAX_VCPUS 1024 @@ -228,6 +229,8 @@ struct kvm_vcpu_arch { /* Don't run the VCPU (blocked) */ bool pause; + + struct kvm_pmu pmu; }; static inline void kvm_arch_hardware_unsetup(void) {} diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h new file mode 100644 index 0000000..3f43a43 --- /dev/null +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2023 Rivos Inc + * + * Authors: + * Atish Patra <atishp@rivosinc.com> + */ + +#ifndef __KVM_VCPU_RISCV_PMU_H +#define __KVM_VCPU_RISCV_PMU_H + +#include <linux/perf/riscv_pmu.h> +#include <asm/kvm_vcpu_sbi.h> +#include <asm/sbi.h> + +#ifdef CONFIG_RISCV_PMU_SBI +#define RISCV_KVM_MAX_FW_CTRS 32 +#define RISCV_MAX_COUNTERS 64 + +/* Per virtual pmu counter data */ +struct kvm_pmc { + u8 idx; + struct perf_event *perf_event; + uint64_t counter_val; + union sbi_pmu_ctr_info cinfo; + /* Event monitoring status */ + bool started; +}; + +/* PMU data structure per vcpu */ +struct kvm_pmu { + struct kvm_pmc pmc[RISCV_MAX_COUNTERS]; + /* Number of the virtual firmware counters available */ + int num_fw_ctrs; + /* Number of the virtual hardware counters available */ + int num_hw_ctrs; + /* A flag to indicate that pmu initialization is done */ + bool init_done; + /* Bit map of all the virtual counter used */ + DECLARE_BITMAP(pmc_in_use, RISCV_MAX_COUNTERS); +}; + +#define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu) +#define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu)) + +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata); +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, + struct kvm_vcpu_sbi_ext_data *edata); +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, + unsigned long ctr_mask, unsigned long flag, uint64_t ival, + struct kvm_vcpu_sbi_ext_data *edata); +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, + unsigned long ctr_mask, unsigned long flag, + struct kvm_vcpu_sbi_ext_data *edata); +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, + unsigned long ctr_mask, unsigned long flag, + unsigned long eidx, uint64_t evtdata, + struct kvm_vcpu_sbi_ext_data *edata); +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, + struct kvm_vcpu_sbi_ext_data *edata); +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu); + +#else +struct kvm_pmu { +}; + +static inline int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) +{ + return 0; +} +static inline void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) {} +static inline void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) {} +#endif /* CONFIG_RISCV_PMU_SBI */ +#endif /* !__KVM_VCPU_RISCV_PMU_H */ diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index 019df920..5de1053 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -25,3 +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 diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 7c08567..b746f21 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -137,6 +137,7 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) WRITE_ONCE(vcpu->arch.irqs_pending, 0); WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); + kvm_riscv_vcpu_pmu_reset(vcpu); vcpu->arch.hfence_head = 0; vcpu->arch.hfence_tail = 0; @@ -194,6 +195,9 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) /* Setup VCPU timer */ kvm_riscv_vcpu_timer_init(vcpu); + /* setup performance monitoring */ + kvm_riscv_vcpu_pmu_init(vcpu); + /* Reset VCPU */ kvm_riscv_reset_vcpu(vcpu); @@ -216,6 +220,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) /* Cleanup VCPU timer */ kvm_riscv_vcpu_timer_deinit(vcpu); + kvm_riscv_vcpu_pmu_deinit(vcpu); /* Free unused pages pre-allocated for G-stage page table mappings */ kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); } diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c new file mode 100644 index 0000000..d3fd551 --- /dev/null +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -0,0 +1,145 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2023 Rivos Inc + * + * Authors: + * Atish Patra <atishp@rivosinc.com> + */ + +#include <linux/errno.h> +#include <linux/err.h> +#include <linux/kvm_host.h> +#include <linux/perf/riscv_pmu.h> +#include <asm/csr.h> +#include <asm/kvm_vcpu_sbi.h> +#include <asm/kvm_vcpu_pmu.h> +#include <linux/kvm_host.h> + +#define kvm_pmu_num_counters(pmu) ((pmu)->num_hw_ctrs + (pmu)->num_fw_ctrs) + +int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_ext_data *edata) +{ + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); + + edata->out_val = kvm_pmu_num_counters(kvpmu); + + return 0; +} + +int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, + struct kvm_vcpu_sbi_ext_data *edata) +{ + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); + + if (cidx > RISCV_MAX_COUNTERS || cidx == 1) { + edata->err_val = SBI_ERR_INVALID_PARAM; + return 0; + } + + edata->out_val = kvpmu->pmc[cidx].cinfo.value; + + return 0; +} + +int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base, + unsigned long ctr_mask, unsigned long flag, uint64_t ival, + struct kvm_vcpu_sbi_ext_data *edata) +{ + /* TODO */ + return 0; +} + +int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, + unsigned long ctr_mask, unsigned long flag, + struct kvm_vcpu_sbi_ext_data *edata) +{ + /* TODO */ + return 0; +} + +int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base, + unsigned long ctr_mask, unsigned long flag, + unsigned long eidx, uint64_t evtdata, + struct kvm_vcpu_sbi_ext_data *edata) +{ + /* TODO */ + return 0; +} + +int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, + struct kvm_vcpu_sbi_ext_data *edata) +{ + /* TODO */ + return 0; +} + +int kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) +{ + int i = 0, num_fw_ctrs, ret, num_hw_ctrs = 0, hpm_width = 0; + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); + struct kvm_pmc *pmc; + + ret = riscv_pmu_get_hpm_info(&hpm_width, &num_hw_ctrs); + if (ret < 0) + return ret; + + if (!hpm_width || !num_hw_ctrs) { + pr_err("Cannot initialize VCPU with NULL hpmcounter width or number of counters\n"); + return -EINVAL; + } + + if ((num_hw_ctrs + RISCV_KVM_MAX_FW_CTRS) > RISCV_MAX_COUNTERS) { + pr_warn("Limiting fw counters as hw & fw counters exceed maximum counters\n"); + num_fw_ctrs = RISCV_MAX_COUNTERS - num_hw_ctrs; + } else + num_fw_ctrs = RISCV_KVM_MAX_FW_CTRS; + + kvpmu->num_hw_ctrs = num_hw_ctrs; + kvpmu->num_fw_ctrs = num_fw_ctrs; + + /* + * There is no correlation between the logical hardware counter and virtual counters. + * However, we need to encode a hpmcounter CSR in the counter info field so that + * KVM can trap n emulate the read. This works well in the migration use case as + * KVM doesn't care if the actual hpmcounter is available in the hardware or not. + */ + for (i = 0; i < kvm_pmu_num_counters(kvpmu); i++) { + /* TIME CSR shouldn't be read from perf interface */ + if (i == 1) + continue; + pmc = &kvpmu->pmc[i]; + pmc->idx = i; + if (i < kvpmu->num_hw_ctrs) { + kvpmu->pmc[i].cinfo.type = SBI_PMU_CTR_TYPE_HW; + if (i < 3) + /* CY, IR counters */ + kvpmu->pmc[i].cinfo.width = 63; + else + kvpmu->pmc[i].cinfo.width = hpm_width; + /* + * The CSR number doesn't have any relation with the logical + * hardware counters. The CSR numbers are encoded sequentially + * to avoid maintaining a map between the virtual counter + * and CSR number. + */ + pmc->cinfo.csr = CSR_CYCLE + i; + } else { + pmc->cinfo.type = SBI_PMU_CTR_TYPE_FW; + pmc->cinfo.width = BITS_PER_LONG - 1; + } + } + + kvpmu->init_done = true; + + return 0; +} + +void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) +{ + /* TODO */ +} + +void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) +{ + kvm_riscv_vcpu_pmu_deinit(vcpu); +}