From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B8BF7C43458 for ; Wed, 1 Jul 2026 08:48:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=EtqTOgDlLTl3/J04XYK88Q4DQK5pQiwKnJeyPQRo3/Q=; b=JShyuvNIJOdc3BTKwyi+G2hZyT fUZcWA/8b5iGoFQJ8uzJh/nIeBSvzaTVDZr1cNTsTuQx9XndOg8urqYrAo76kvMsso60QKYCtB8b9 h78RjjfZ46nAZYLGb5ZUwKrmF0B+AJAsqJZh6JRNjjYwW9pI0Q4juh7VDKbXVXcELXfpg4zDHjl2L ngvloakX+l5Qaq9Zd3+QLrCt44FcZCs1X65KVHxLveLS/xbpxkWgihJeS7fv/1lZS75LphIpn91Jr hRq9An2bWS0i31vkJrXzfHmsi+XLAKf+OFpdOwA1PmGPbyonBMauyA7OSctfPvFYvmO4yO/ZVC4Wu hVGb/qRw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1weqcK-0000000157A-1cCe; Wed, 01 Jul 2026 08:48:24 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1weqcJ-0000000154X-2alZ for linux-arm-kernel@bombadil.infradead.org; Wed, 01 Jul 2026 08:48:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=EtqTOgDlLTl3/J04XYK88Q4DQK5pQiwKnJeyPQRo3/Q=; b=EdOBK8jBCT6+z6jksX4ODL1s/b V0NsQDiDsAfwBW6KxfVfXl7gg9m/YEyD3ixzPOoLgJpxlXfS6Kg6htkl1B+MHZCX4Zh8dccOR3ho4 o0f8zS8hUTyMG0dkTw49Wm5ghSpWk6Bn2ANS/tX/9Nhoozcqmj6gdqhvGUDoLVp3+rRLBdYW3hgRC h/e83Qo0WMRgcBvHGYu6wXW1KZuvXee9OLu7O8R5LidQof6cWfPMD8Uu7vasNODshqUPumbmE/8r8 T32O2BoGxQatVK88ISwEx6Qd2HJRrvGXeo48FoioHdMJ3Oj07ucgwJO13pp9vB6w6RcIXU8Ef54Nz 1kgLKd7g==; Received: from out-176.mta1.migadu.com ([95.215.58.176]) by desiato.infradead.org with esmtps (Exim 4.99.2 #2 (Red Hat Linux)) id 1weqcF-00000002Wje-1Q3t for linux-arm-kernel@lists.infradead.org; Wed, 01 Jul 2026 08:48:21 +0000 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1782895697; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EtqTOgDlLTl3/J04XYK88Q4DQK5pQiwKnJeyPQRo3/Q=; b=HAtekGoXjhlhMx5H1+9v5OlzPy9w6d4f/aM2YI783FKHavo9IdCabyZLfd4Fdnh6XrZu4j NPBlB34AZGc1mlpI2kIetNNnmiDgnXY3DKEKwJhXSpuZzvcx2QdYgzpcqfVQRAFd+k6pkv CCupG0gGsb34+h2zYqJpR5KNb9PuhNs= From: Atish Patra Date: Wed, 01 Jul 2026 01:47:00 -0700 Subject: [PATCH v8 12/22] RISC-V: perf: Modify the counter discovery mechanism MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260701-counter_delegation-v8-12-7909f863a645@meta.com> References: <20260701-counter_delegation-v8-0-7909f863a645@meta.com> In-Reply-To: <20260701-counter_delegation-v8-0-7909f863a645@meta.com> To: Jiri Olsa , Paul Walmsley , Mark Rutland , Rob Herring , Anup Patel , Namhyung Kim , Arnaldo Carvalho de Melo , Krzysztof Kozlowski , Atish Patra , Ian Rogers , Will Deacon , James Clark Cc: linux-arm-kernel@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-perf-users@vger.kernel.org, Conor Dooley X-Migadu-Flow: FLOW_OUT X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260701_094819_599407_6F991554 X-CRM114-Status: GOOD ( 28.91 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Atish Patra If both counter delegation and SBI PMU is present, the counter delegation will be used for hardware pmu counters while the SBI PMU will be used for firmware counters. Thus, the driver has to probe the counters info via SBI PMU to distinguish the firmware counters. The hybrid scheme also requires improvements of the informational logging messages to indicate the user about underlying interface used for each use case. Signed-off-by: Atish Patra --- drivers/perf/riscv_pmu_sbi.c | 139 ++++++++++++++++++++++++++++++++----------- 1 file changed, 104 insertions(+), 35 deletions(-) diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 74d934238821..c20f1e33c65d 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -67,6 +67,20 @@ static bool sbi_v3_available; static DEFINE_STATIC_KEY_FALSE(sbi_pmu_snapshot_available); #define sbi_pmu_snapshot_available() \ static_branch_unlikely(&sbi_pmu_snapshot_available) +static DEFINE_STATIC_KEY_FALSE(riscv_pmu_sbi_available); +static DEFINE_STATIC_KEY_FALSE(riscv_pmu_cdeleg_available); + +/* Avoid unnecessary code patching in the one time booting path*/ +#define riscv_pmu_cdeleg_available_boot() \ + static_key_enabled(&riscv_pmu_cdeleg_available) +#define riscv_pmu_sbi_available_boot() \ + static_key_enabled(&riscv_pmu_sbi_available) + +/* Perform a runtime code patching with static key */ +#define riscv_pmu_cdeleg_available() \ + static_branch_unlikely(&riscv_pmu_cdeleg_available) +#define riscv_pmu_sbi_available() \ + static_branch_likely(&riscv_pmu_sbi_available) static struct attribute *riscv_arch_formats_attr[] = { &format_attr_event.attr, @@ -89,7 +103,8 @@ static int sysctl_perf_user_access __read_mostly = SYSCTL_USER_ACCESS; /* * This structure is SBI specific but counter delegation also require counter - * width, csr mapping. Reuse it for now. + * width, csr mapping. Reuse it for now we can have firmware counters for + * platfroms with counter delegation support. * RISC-V doesn't have heterogeneous harts yet. This need to be part of * per_cpu in case of harts with different pmu counters */ @@ -101,6 +116,8 @@ static unsigned int riscv_pmu_irq; /* Cache the available counters in a bitmask */ static unsigned long cmask; +/* Cache the available firmware counters in another bitmask */ +static unsigned long firmware_cmask; static int sbi_pmu_event_find_cache(u64 config); struct sbi_pmu_event_data { @@ -868,34 +885,38 @@ static int rvpmu_sbi_find_num_ctrs(void) return sbi_err_map_linux_errno(ret.error); } -static int rvpmu_sbi_get_ctrinfo(int nctr, unsigned long *mask) +static u32 rvpmu_deleg_find_ctrs(void) +{ + /* TODO */ + return 0; +} + +static int rvpmu_sbi_get_ctrinfo(u32 nsbi_ctr, u32 *num_fw_ctr, u32 *num_hw_ctr) { struct sbiret ret; - int i, num_hw_ctr = 0, num_fw_ctr = 0; + int i; union sbi_pmu_ctr_info cinfo; - pmu_ctr_list = kzalloc_objs(*pmu_ctr_list, nctr); - if (!pmu_ctr_list) - return -ENOMEM; - - for (i = 0; i < nctr; i++) { + for (i = 0; i < nsbi_ctr; i++) { ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_GET_INFO, i, 0, 0, 0, 0, 0); if (ret.error) /* The logical counter ids are not expected to be contiguous */ continue; - *mask |= BIT(i); - cinfo.value = ret.value; - if (cinfo.type == SBI_PMU_CTR_TYPE_FW) - num_fw_ctr++; - else - num_hw_ctr++; - pmu_ctr_list[i].value = cinfo.value; + if (cinfo.type == SBI_PMU_CTR_TYPE_FW) { + /* Track firmware counters in a different mask */ + firmware_cmask |= BIT(i); + pmu_ctr_list[i].value = cinfo.value; + *num_fw_ctr = *num_fw_ctr + 1; + } else if (cinfo.type == SBI_PMU_CTR_TYPE_HW && + !riscv_pmu_cdeleg_available_boot()) { + *num_hw_ctr = *num_hw_ctr + 1; + cmask |= BIT(i); + pmu_ctr_list[i].value = cinfo.value; + } } - pr_info("%d firmware and %d hardware counters\n", num_fw_ctr, num_hw_ctr); - return 0; } @@ -906,7 +927,7 @@ static inline void rvpmu_sbi_stop_all(struct riscv_pmu *pmu) * which may include counters that are not enabled yet. */ sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, - 0, pmu->cmask, SBI_PMU_STOP_FLAG_RESET, 0, 0, 0); + 0, pmu->cmask | firmware_cmask, SBI_PMU_STOP_FLAG_RESET, 0, 0, 0); } static inline void rvpmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu) @@ -1159,16 +1180,48 @@ static void rvpmu_ctr_stop(struct perf_event *event, unsigned long flag) /* TODO: Counter delegation implementation */ } -static int rvpmu_find_num_ctrs(void) +static int rvpmu_find_ctrs(void) { - return rvpmu_sbi_find_num_ctrs(); - /* TODO: Counter delegation implementation */ -} + int num_sbi_counters = 0; + u32 num_deleg_counters = 0; + u32 num_hw_ctr = 0, num_fw_ctr = 0, num_ctr = 0; + /* + * We don't know how many firmware counters are available. Just allocate + * for maximum counters the driver can support. The default is 64 anyways. + */ + pmu_ctr_list = kcalloc(RISCV_MAX_COUNTERS, sizeof(*pmu_ctr_list), + GFP_KERNEL); + if (!pmu_ctr_list) + return -ENOMEM; -static int rvpmu_get_ctrinfo(int nctr, unsigned long *mask) -{ - return rvpmu_sbi_get_ctrinfo(nctr, mask); - /* TODO: Counter delegation implementation */ + if (riscv_pmu_cdeleg_available_boot()) + num_deleg_counters = rvpmu_deleg_find_ctrs(); + + /* This is required for firmware counters even if the above is true */ + if (riscv_pmu_sbi_available_boot()) { + num_sbi_counters = rvpmu_sbi_find_num_ctrs(); + if (num_sbi_counters < 0) { + kfree(pmu_ctr_list); + pmu_ctr_list = NULL; + return num_sbi_counters; + } + if (num_sbi_counters > RISCV_MAX_COUNTERS) + num_sbi_counters = RISCV_MAX_COUNTERS; + } + + /* cache all the information about counters now */ + if (riscv_pmu_sbi_available_boot()) + rvpmu_sbi_get_ctrinfo(num_sbi_counters, &num_fw_ctr, &num_hw_ctr); + + if (riscv_pmu_cdeleg_available_boot()) { + pr_info("%u firmware and %u hardware counters\n", num_fw_ctr, num_deleg_counters); + num_ctr = num_fw_ctr + num_deleg_counters; + } else { + pr_info("%u firmware and %u hardware counters\n", num_fw_ctr, num_hw_ctr); + num_ctr = num_sbi_counters; + } + + return num_ctr; } static int rvpmu_event_map(struct perf_event *event, u64 *econfig) @@ -1478,12 +1531,21 @@ static int rvpmu_device_probe(struct platform_device *pdev) int num_counters; bool irq_requested = false; - pr_info("SBI PMU extension is available\n"); + if (riscv_pmu_cdeleg_available_boot()) { + pr_info("hpmcounters will use the counter delegation ISA extension\n"); + if (riscv_pmu_sbi_available_boot()) + pr_info("Firmware counters will use SBI PMU extension\n"); + else + pr_info("Firmware counters will not be available as SBI PMU extension is not present\n"); + } else if (riscv_pmu_sbi_available_boot()) { + pr_info("Both hpmcounters and firmware counters will use SBI PMU extension\n"); + } + pmu = riscv_pmu_alloc(); if (!pmu) return -ENOMEM; - num_counters = rvpmu_find_num_ctrs(); + num_counters = rvpmu_find_ctrs(); if (num_counters < 0) { pr_err("SBI PMU extension doesn't provide any counters\n"); goto out_free; @@ -1495,9 +1557,6 @@ static int rvpmu_device_probe(struct platform_device *pdev) pr_info("SBI returned more than maximum number of counters. Limiting the number of counters to %d\n", num_counters); } - /* cache all the information about counters now */ - if (rvpmu_get_ctrinfo(num_counters, &cmask)) - goto out_free; ret = rvpmu_setup_irqs(pmu, pdev); if (ret < 0) { @@ -1599,13 +1658,23 @@ static int __init rvpmu_devinit(void) int ret; struct platform_device *pdev; - if (sbi_spec_version < sbi_mk_version(0, 3) || - !sbi_probe_extension(SBI_EXT_PMU)) { - return 0; - } + if (sbi_spec_version >= sbi_mk_version(0, 3) && + sbi_probe_extension(SBI_EXT_PMU)) + static_branch_enable(&riscv_pmu_sbi_available); if (sbi_spec_version >= sbi_mk_version(2, 0)) sbi_v2_available = true; + /* + * We need all three extensions to be present to access the counters + * in S-mode via Supervisor Counter delegation. + */ + if (riscv_isa_extension_available(NULL, SSCCFG) && + riscv_isa_extension_available(NULL, SMCDELEG) && + riscv_isa_extension_available(NULL, SSCSRIND)) + static_branch_enable(&riscv_pmu_cdeleg_available); + + if (!(riscv_pmu_sbi_available_boot() || riscv_pmu_cdeleg_available_boot())) + return 0; if (sbi_spec_version >= sbi_mk_version(3, 0)) sbi_v3_available = true; -- 2.53.0-Meta