From patchwork Sat Feb 17 00:57:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 1900380 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=PQhmEP4L; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=RIj1oilG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tc9Rg5T97z23j8 for ; Sat, 17 Feb 2024 11:59:11 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=eC6WqagtPCoum1j2S5woEzCzLpSX5ueMLR/Rwm1+O4I=; b=PQhmEP4LR0gR3K yYtXa1oSX4ezQz9jGxpczlMmDGcNI0GTRhpIgSCL+HNnbWottier0DSaJ3axgqDSvvP/q+m8w8csV kCaMMXKecWGvglwiOo1+xaMsDSB4+AnvHgJoxZxhFVsyOtIucDs2N3XlMFgxwTfMKQBOpMuL/iKWN ufnMyGxtOnfFWKzNPzwbF7PyDPw9blkvWd7NNlVfJBJdoaFSsWa1iX59Z5iQACZxslGIIl9c2oebe r+rJcLPlkpyIhBbhrvTBo+9UY5WR4pFEam0l8Nx+mkYTXPF4RLhyeZtmNK5SrJONm1lQzHJGMZfx3 UCfelHygPeZEjj9pb/LA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rb92r-00000004Icg-2SpL; Sat, 17 Feb 2024 00:59:09 +0000 Received: from mail-oi1-x229.google.com ([2607:f8b0:4864:20::229]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rb92Z-00000004INS-36s4 for kvm-riscv@lists.infradead.org; Sat, 17 Feb 2024 00:59:00 +0000 Received: by mail-oi1-x229.google.com with SMTP id 5614622812f47-3bbbc6bcc78so1797521b6e.1 for ; Fri, 16 Feb 2024 16:58:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1708131531; x=1708736331; darn=lists.infradead.org; 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=wzekTDmDm0BHWDBhHgdtQywPCZoHi4eTbi4rsPTxLsk=; b=RIj1oilGOUNHBuBFkfmjbwskA5XqvCgqDibjwZB8TIhSnaGrBThXImbmZQk9rCwoi2 B4eVIr0dFKLjKEWk3SyX8HVo3kUc5mm4Dw+vvmDZjC8A0aYAcmw4RB9oXt/dIPf3J8Tp Q/AgHOFVPeJw7WeqJe2cujyDC/YN76+akEHJJxpaaj9VIhx3Ix6an+ZjhpvY8uPo2lgV C0N04E4TpTTqMfBTHgtfT+ot8h0RzwAZa0Ri2B68rCm2E9cFj9PpfQaAe/wtKfhlxOz1 kvbZzDamE2istCm73Ie8zFIaWeLAwHUDNZCFOvoQKgIz0ZIhsQCUNM4vR/ONRAhiClKE 8CGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708131531; x=1708736331; 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=wzekTDmDm0BHWDBhHgdtQywPCZoHi4eTbi4rsPTxLsk=; b=Lrxd4IfEMcDI4c0jUAdf5DCRUVh1+87kGbcYXRInCLq6gPpU/ivRa/IDI+E3t1Zu1X MQTu8OFiWWGuK7ZnRW8EJejq786vHX+G7ZhLgFC+sZIrLZpM4GJCfR/9MCLeAF/XtppI y1ftndxNZYIgOo0RYhK6rXLkLGa12mVGM/p0qn1IUDVIRqYAx5h6GRdb5xaYIKGgiXO6 Xj7ZY8gbjaG8j40ebb68WEJBMEBDYruiKqMWSZHYipnGMtpZ7NfrPAIFcENN0ue7Gywn SjD8QWCbhJt6DZgF3eMjyxcqTNBM3sKaFaTRPjqbL0WG2je9nVrcJV8To1JLDxg9VIhU F73A== X-Forwarded-Encrypted: i=1; AJvYcCWqT+ng6Vd1SEOvZYKzW2xbIxmOk1kASVgapWKeXWZBgSs9Xg50aFGIrYd6AMR6C2qPaLN/eDjAfLRXA/+8IWr/WfrtvdAsW4QWKh3pbg== X-Gm-Message-State: AOJu0Yz17mTZ9PJjxqZVgBOtTDmwgaIflSPheyLkRn3vwQTWDSMGUD1y P6DNVbVuGrIkkSdeMK5Kk5mhbmgF5qVjZnymUvv8QB4wzSrgKeMR1raDBRA+oDg= X-Google-Smtp-Source: AGHT+IFh5z2qRfi3D9IkKas86nFROYWU3mPjDzUuW4T2X7wbUeQ8t61ZPYIlkMzVMiWXNbGIgenNww== X-Received: by 2002:a05:6808:4d0:b0:3c1:347f:39d1 with SMTP id a16-20020a05680804d000b003c1347f39d1mr6306573oie.22.1708131530901; Fri, 16 Feb 2024 16:58:50 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d188-20020a6336c5000000b005dc89957e06sm487655pga.71.2024.02.16.16.58.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 16:58:50 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Adrian Hunter , Alexander Shishkin , Alexandre Ghiti , Andrew Jones , Anup Patel , Arnaldo Carvalho de Melo , Atish Patra , Christian Brauner , =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , Conor Dooley , devicetree@vger.kernel.org, Evan Green , Guo Ren , Heiko Stuebner , Ian Rogers , Ingo Molnar , James Clark , Jing Zhang , Jiri Olsa , Ji Sheng Teoh , John Garry , Jonathan Corbet , Kan Liang , Krzysztof Kozlowski , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, Ley Foon Tan , linux-doc@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Namhyung Kim , Palmer Dabbelt , Paul Walmsley , Peter Zijlstra , Rob Herring , Samuel Holland , Weilin Wang , Will Deacon , kaiwenxue1@gmail.com, Yang Jihong Subject: [PATCH RFC 14/20] RISC-V: perf: Use config2 for event to counter mapping Date: Fri, 16 Feb 2024 16:57:32 -0800 Message-Id: <20240217005738.3744121-15-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240217005738.3744121-1-atishp@rivosinc.com> References: <20240217005738.3744121-1-atishp@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240216_165852_048330_CD14999A X-CRM114-Status: GOOD ( 24.93 ) X-Spam-Score: -0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: The counter restriction specified in the json file is passed to the drivers via config2 paarameter in perf attributes. This allows any platform vendor to define their custom mapping between event and [...] Content analysis details: (-0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:229 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.0 T_SCC_BODY_TEXT_LINE No description available. X-BeenThere: kvm-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kvm-riscv" Errors-To: kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org The counter restriction specified in the json file is passed to the drivers via config2 paarameter in perf attributes. This allows any platform vendor to define their custom mapping between event and hpmcounters without any rules defined in the ISA. However, the cycle and instruction counters are fixed (0 and 2 respectively) by the ISA. The platform vendor must specify this in the json file if intended to be used while profiling. Otherwise, they can just specify the alternate hpmcounters that may monitor and/or sample the cycle/instruction counts. Signed-off-by: Atish Patra --- drivers/perf/riscv_pmu_dev.c | 36 +++++++++++++++++++++++----------- include/linux/perf/riscv_pmu.h | 2 ++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/drivers/perf/riscv_pmu_dev.c b/drivers/perf/riscv_pmu_dev.c index 0cdad0dafb71..5bad4131e920 100644 --- a/drivers/perf/riscv_pmu_dev.c +++ b/drivers/perf/riscv_pmu_dev.c @@ -49,6 +49,7 @@ static ssize_t __maybe_unused rvpmu_format_show(struct device *dev, RVPMU_ATTR_ENTRY(_name, rvpmu_format_show, (char *)_config) PMU_FORMAT_ATTR(firmware, "config:63"); +PMU_FORMAT_ATTR(counterid_mask, "config2:0-31"); static DEFINE_STATIC_KEY_FALSE(riscv_pmu_sbi_available); static DEFINE_STATIC_KEY_FALSE(riscv_pmu_cdeleg_available); @@ -74,6 +75,7 @@ static const struct attribute_group *riscv_sbi_pmu_attr_groups[] = { static struct attribute *riscv_cdeleg_pmu_formats_attr[] = { RVPMU_FORMAT_ATTR_ENTRY(event, RVPMU_CDELEG_PMU_FORMAT_ATTR), &format_attr_firmware.attr, + &format_attr_counterid_mask.attr, NULL, }; @@ -974,23 +976,39 @@ static int rvpmu_deleg_find_ctrs(void) return num_hw_ctr; } +/* The json file must correctly specify counter 0 or counter 2 is available + * in the counter lists for cycle/instret events. Otherwise, the drivers have + * no way to figure out if a fixed counter must be used and pick a programmable + * counter if available. + */ static int get_deleg_fixed_hw_idx(struct cpu_hw_events *cpuc, struct perf_event *event) { - return -EINVAL; + if (!event->attr.config2) + return -EINVAL; + + if (event->attr.config2 & RISCV_PMU_CYCLE_FIXED_CTR_MASK) + return 0; /* CY counter */ + else if (event->attr.config2 & RISCV_PMU_INSTRUCTION_FIXED_CTR_MASK) + return 2; /* IR counter */ + else + return -EINVAL; } static int get_deleg_next_hpm_hw_idx(struct cpu_hw_events *cpuc, struct perf_event *event) { unsigned long hw_ctr_mask = 0; - /* - * TODO: Treat every hpmcounter can monitor every event for now. - * The event to counter mapping should come from the json file. - * The mapping should also tell if sampling is supported or not. - */ - /* Select only hpmcounters */ hw_ctr_mask = cmask & (~0x7); + + /* + * Mask off the counters that can't monitor this event (specified via json) + * The counter mask for this event is set in config2 via the property 'Counter' + * in the json file or manual configuration of config2. If the config2 is not set, it + * is assumed all the available hpmcounters can monitor this event. + */ + if (event->attr.config2) + hw_ctr_mask = hw_ctr_mask & event->attr.config2; hw_ctr_mask &= ~(cpuc->used_hw_ctrs[0]); return __ffs(hw_ctr_mask); } @@ -1020,10 +1038,6 @@ static int rvpmu_deleg_ctr_get_idx(struct perf_event *event) uint64_t priv_filter; int idx; - /* - * TODO: We should not rely on SBI Perf encoding to check if the event - * is a fixed one or not. - */ if (!is_sampling_event(event)) { idx = get_deleg_fixed_hw_idx(cpuc, event); if (idx == 0 || idx == 2) { diff --git a/include/linux/perf/riscv_pmu.h b/include/linux/perf/riscv_pmu.h index f878369fecc8..425edd6685a9 100644 --- a/include/linux/perf/riscv_pmu.h +++ b/include/linux/perf/riscv_pmu.h @@ -30,6 +30,8 @@ #define RISCV_PMU_CONFIG1_GUEST_EVENTS 0x1 #define RISCV_PMU_DELEG_RAW_EVENT_MASK GENMASK_ULL(55, 0) +#define RISCV_PMU_CYCLE_FIXED_CTR_MASK 0x01 +#define RISCV_PMU_INSTRUCTION_FIXED_CTR_MASK 0x04 struct cpu_hw_events { /* currently enabled events */