From patchwork Fri Jan 27 18:25:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 49573 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp980255wrn; Fri, 27 Jan 2023 10:27:51 -0800 (PST) X-Google-Smtp-Source: AK7set9+IkU0w1MFMPFQbqgNl3piTqQiN5tceXfX9Ap92l1+vs0KXNQn+w/snS+CfcD0HWBm6Sxu X-Received: by 2002:aa7:8484:0:b0:593:89ab:2ec4 with SMTP id u4-20020aa78484000000b0059389ab2ec4mr596800pfn.10.1674844071224; Fri, 27 Jan 2023 10:27:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674844071; cv=none; d=google.com; s=arc-20160816; b=hwDeRadSZAjMfMKCmgP9+KYhM/kStbhNmf+EV3hIzBvcWar1/6fgYwmp+7+eyZ2mVA eoaoXlPjHz61YUMnco9XlvNljMEC4YEt6MU/eueSO+XVOlsmZ3/lxKp2FhP17ukzjV9b cOMB3oflwn3Pi133M2G+w16fMzaokdCYLSJrWFujrsH4n8LSrEKPvslPg/rEJY3UGkLZ ExjQcqwrQGYuQRxlKvtDFeSczyA2RuerD4aNkkCqGqS069SVRgcduf0OWfCdz1F6/xNv 38V71LzzUUoVdSYRIfYLZRvwHygH66uUSkytvQo/PEIZPCY6+s2lfsaPMJ7B/JeXa/k4 d6nA== 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=iGqgvezQ/AlpTuqgVROWAp2lac08ioX9ghbgFT6R5zs=; b=FYLtDKWEbvUBdpu1nrQttVNHSnaf4lI7zseEr+4mNfdQsthRIydcUNudEX0NBEuQyO xPVAChfe1MzCfJbhP2sHbdQBx4kd6S5QFo36kC/rjwsSBjFNcAIgS1YgI8ehUugeX+6u EdrE7MdeZz6PK+YUXUif4qQdaLkrOaf3ElS9RkPa5NVSDAu8Z8GR+9+WdfktXxjzORS2 gCE8ta77d4MyTayr1FDraD6YV21rixd0tGZfTgGG7RD8tSC1nQb4abbxcmk3fxjEXKAF 3AI/c933kD6IaO79I2q/1RsULdXRcb5kqe4wv6zwRYldqmpNhQ4dqHHk8Djug+QpCY01 cXtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=IoRffA7e; 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 o1-20020a62cd01000000b005925f606e33si3422639pfg.293.2023.01.27.10.27.39; Fri, 27 Jan 2023 10:27:51 -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=IoRffA7e; 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 S235260AbjA0S0V (ORCPT + 99 others); Fri, 27 Jan 2023 13:26:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235072AbjA0S0Q (ORCPT ); Fri, 27 Jan 2023 13:26:16 -0500 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C39D7B40A for ; Fri, 27 Jan 2023 10:26:15 -0800 (PST) Received: by mail-pl1-x62f.google.com with SMTP id x5so2411924plr.2 for ; Fri, 27 Jan 2023 10:26:15 -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=iGqgvezQ/AlpTuqgVROWAp2lac08ioX9ghbgFT6R5zs=; b=IoRffA7eaBKD5TnGEAMzpERUl+R39LOxAlgfDLhDNga+rN9SbFYLfdz9WFRgJCTeRN 9Updpbd1OZtc//enWU826k0AachdF3yKioeNtgxkt9/kIfGqMeggP3NJkMHCVrn38zEt o0ySxQgMwR7DstBzrONN9sUSnnkIYSCZmjlPUaPZycJTqTBIKNAj0Y+IKQKTWXZUV/g1 Hbr1Yw2D00RYz6wcO9p7KP1dPScebiMdJ1Wz7lqeW/m8MLImzr1r9OBpSxBUtGPDMeaU EKmOy6JcaGBPxAZXqtqvKGClyDQ4IZH3V1Pgrky1d4NUmTsNJDKgHZ/O9226Ve6thdSn TZ6A== 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=iGqgvezQ/AlpTuqgVROWAp2lac08ioX9ghbgFT6R5zs=; b=kEFC6RLZliSHLbMvOOEVc1ApJwYA8Lw0Bjpu72GCChhqXAoYWiBSahHyjDB9MWxu6x 6kkmdYL0rDw1mpXziY/j44JxKzFlJdB2Ber9y2tRn2C6G/Z0ii37xkg5JgeNd65DDFwn UXkvtsYrD2HVKFY58meaNDXxB5CnsQpgQpDI1fLCKz+p4iRF+J8UfylEvUiwewzBZ/KV wvNVZv4vjoh59Dz1tKxZyp/hV6Npbhbzc9Arb4YNpxp6VOcOS5b07ozWnsIwCJNLihhP XPJU9MWllWW4v3cMjyditYEjx5zazsD+PrWCly50hknj2I9WTHytS+PHVaUyzTPJdmPi p7tA== X-Gm-Message-State: AO0yUKW7Hwq0baU/7aRLyIgFWOr2bW1FfaW6NiTvPZTwIBdLdULBg4+O gcgiZLFwisk+Qvf6Q1yU3r0VL0lBq+Kw/OWl X-Received: by 2002:a17:902:f54c:b0:196:f82:14c9 with SMTP id h12-20020a170902f54c00b001960f8214c9mr17447443plf.57.1674843974605; Fri, 27 Jan 2023 10:26:14 -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.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Jan 2023 10:26:14 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Andrew Jones , Anup Patel , Atish Patra , Guo Ren , Heiko Stuebner , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paul Walmsley , Sergey Matyukevich , Will Deacon Subject: [PATCH v3 01/14] perf: RISC-V: Define helper functions expose hpm counter width and count Date: Fri, 27 Jan 2023 10:25:45 -0800 Message-Id: <20230127182558.2416400-2-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 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756201296921019706?= X-GMAIL-MSGID: =?utf-8?q?1756201296921019706?= KVM module needs to know how many hardware counters and the counter width that the platform supports. Otherwise, it will not be able to show optimal value of virtual counters to the guest. The virtual hardware counters also need to have the same width as the logical hardware counters for simplicity. However, there shouldn't be mapping between virtual hardware counters and logical hardware counters. As we don't support hetergeneous harts or counters with different width as of now, the implementation relies on the counter width of the first available programmable counter. Signed-off-by: Atish Patra Reviewed-by: Anup Patel --- drivers/perf/riscv_pmu_sbi.c | 37 ++++++++++++++++++++++++++++++++-- include/linux/perf/riscv_pmu.h | 3 +++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index f6507ef..6b53adc 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -44,7 +44,7 @@ static const struct attribute_group *riscv_pmu_attr_groups[] = { }; /* - * RISC-V doesn't have hetergenous harts yet. This need to be part of + * RISC-V doesn't have heterogeneous harts yet. This need to be part of * per_cpu in case of harts with different pmu counters */ static union sbi_pmu_ctr_info *pmu_ctr_list; @@ -52,6 +52,9 @@ static bool riscv_pmu_use_irq; static unsigned int riscv_pmu_irq_num; static unsigned int riscv_pmu_irq; +/* Cache the available counters in a bitmask */ +static unsigned long cmask; + struct sbi_pmu_event_data { union { union { @@ -267,6 +270,37 @@ static bool pmu_sbi_ctr_is_fw(int cidx) return (info->type == SBI_PMU_CTR_TYPE_FW) ? true : false; } +/* + * Returns the counter width of a programmable counter and number of hardware + * counters. As we don't support heterogeneous CPUs yet, it is okay to just + * return the counter width of the first programmable counter. + */ +int riscv_pmu_get_hpm_info(u32 *hw_ctr_width, u32 *num_hw_ctr) +{ + int i; + union sbi_pmu_ctr_info *info; + u32 hpm_width = 0, hpm_count = 0; + + if (!cmask) + return -EINVAL; + + for_each_set_bit(i, &cmask, RISCV_MAX_COUNTERS) { + info = &pmu_ctr_list[i]; + if (!info) + continue; + if (!hpm_width && info->csr != CSR_CYCLE && info->csr != CSR_INSTRET) + hpm_width = info->width; + if (info->type == SBI_PMU_CTR_TYPE_HW) + hpm_count++; + } + + *hw_ctr_width = hpm_width; + *num_hw_ctr = hpm_count; + + return 0; +} +EXPORT_SYMBOL_GPL(riscv_pmu_get_hpm_info); + static int pmu_sbi_ctr_get_idx(struct perf_event *event) { struct hw_perf_event *hwc = &event->hw; @@ -812,7 +846,6 @@ static void riscv_pmu_destroy(struct riscv_pmu *pmu) static int pmu_sbi_device_probe(struct platform_device *pdev) { struct riscv_pmu *pmu = NULL; - unsigned long cmask = 0; int ret = -ENODEV; int num_counters; diff --git a/include/linux/perf/riscv_pmu.h b/include/linux/perf/riscv_pmu.h index e17e86a..a1c3f77 100644 --- a/include/linux/perf/riscv_pmu.h +++ b/include/linux/perf/riscv_pmu.h @@ -73,6 +73,9 @@ void riscv_pmu_legacy_skip_init(void); static inline void riscv_pmu_legacy_skip_init(void) {}; #endif struct riscv_pmu *riscv_pmu_alloc(void); +#ifdef CONFIG_RISCV_PMU_SBI +int riscv_pmu_get_hpm_info(u32 *hw_ctr_width, u32 *num_hw_ctr); +#endif #endif /* CONFIG_RISCV_PMU */