From patchwork Wed Jan 3 16:53:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 184803 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp5135052dyb; Wed, 3 Jan 2024 08:55:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IHBaIMl5RZgSFaiKO0mJLLAfHFepeKYAeIlT9LMC+YvQd3pRP3xPopPglPQxjixqxTxsRmQ X-Received: by 2002:a17:90b:3654:b0:28c:f3f4:7a45 with SMTP id nh20-20020a17090b365400b0028cf3f47a45mr6268pjb.35.1704300914853; Wed, 03 Jan 2024 08:55:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704300914; cv=none; d=google.com; s=arc-20160816; b=0nzZbb3l+EyDIe/vmKTy/rOC4BD581vEcE+4Milt6tefLi12cbp20wMIupdKlj9Zyy bIMtJHxfPXwS8lWa28zgl5OQYzYhGvmc1QcT0c1VSQf9NIyId+a49NWQy5OrlLsTDurO 8qT/M8ANnx64pdD1flQVaavPy+XONYy0SthAJ/NBpvlv08vwKxikG3DPlP1vanBOcDQ2 RcUDhlnzw5/dwotcD28oUPcaUHpGLbwLmhDnxwWEvhCu5nXmCkFgSfGtIjIRoVQsTxJC 3904RRcFygaDEie7ZLQj8Sr8DLkoISjDJmMtADC9MAxx2RsK+vdkjO6HQCxRzhQBcQTc rqgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=At4QHdwp3ZadpLBcNbgmNjMEMqeIcq9YtBwBsEfmi00=; fh=0aqO90p2moxJor0D5bOTp+ibdEFX2ixgxw+EXHZsztc=; b=ysN7DANBufUNjkCoWg1F10tvLG2d9XWKS7Lf09JrH5UbA370hZn7rF8tiJVNPF52gi 6B55kh9upWQ9EKWekRS+bZBRr727UTj8u5QZbtdiLnSU8t6IQs+7OU70zmcHArjJCARp wbrlio1LR7vdEJBE2ojI88zbIaCU7NaNYzbkdEiRpMOqB5VUyNWXyP+8PwLL7Z06p5eM q8EANwBw9Tanw9MAqoE6FCvdrfRlbbmnE27ZmTQG4NKTK/nGxlvZmHBhqZu9SbOHufSU wQyc15K4ALhs9SYHDOb3yXd/3bDMz5oy+jhXhg6KuF3pCQImb+kcAc7JnPfNJzWI5GTK k6kQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=UoWeLLZx; spf=pass (google.com: domain of linux-kernel+bounces-15776-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15776-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id r14-20020a17090a4dce00b0028c0c37de9esi1490861pjl.166.2024.01.03.08.55.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 08:55:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15776-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=UoWeLLZx; spf=pass (google.com: domain of linux-kernel+bounces-15776-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15776-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 6E6C2B21A28 for ; Wed, 3 Jan 2024 16:55:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2A3711C28C; Wed, 3 Jan 2024 16:54:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="UoWeLLZx" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAC341BDDF for ; Wed, 3 Jan 2024 16:54:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-5ce0656a7bdso1970550a12.3 for ; Wed, 03 Jan 2024 08:54:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1704300881; x=1704905681; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=At4QHdwp3ZadpLBcNbgmNjMEMqeIcq9YtBwBsEfmi00=; b=UoWeLLZxCab0VKZC1YVQqfLDYgWaJ5oYSsNdgDQ3eZ2+fsyscXqgY0sp/NQnXzDCnQ nE9tJ4FMyIO3uNG8PJOMl5LBXiZRsxpbrCMC2Y6bMGAPn3FGnULsrSblHGNyAZMXRTq9 KBnmcfyQvjaBW+D7wGCHSJn6+dl6knNXmZXf+A+CuVc4wEyarLXvmECiLhBp3DL8N4SO fWRjtyKUUg81bfi+jwKpTLSPZ6W8jxpwv84bbILwNl1L2ZwBFimAwkCt3rLEPVnBn+CV dv+xI7zuNDVJdpt3TFn+p33x2JaH75X63vXWhUWaikcA/R6KdKAS1gsbqKZv19IVKr+n AVpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704300881; x=1704905681; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=At4QHdwp3ZadpLBcNbgmNjMEMqeIcq9YtBwBsEfmi00=; b=C8wa3Mtvj8jb2b5rcWyFPaZ0/xjWKr7v/sR0Pa6HvT6RKe1guNSsLIIxERhx7bfoeI D4GYiEWsnafGuY4Jv/m8j28oay/3zgjQq5jW+NEIcnhfQEZtYYkdMt56sOwaGPB2QbZ+ Aotbo0ExwPvyOu026B1RtAwXeV5H3rrK/e+2KzsSUhrSPvUks2ixj66DKAdRH67au8fH a6Zncg+/10T5KBHwqdYDmsQml1PsVUeATntC3SKHrI/Yv0uoKpJSzJDwS93iMnyXfzzH V3y33BAM8Di0pq7Mi0LHBXexytft3kYaO6KKtGI+FAgT5XnSIuW3JxVD4903r5gw7KlZ YX6A== X-Gm-Message-State: AOJu0YzxCJdjtSEKnGQilVpwgRobTIcruBrDwwgpy/Q0QWDbzgYj3AuV fNVZMs7AOlFaKPMeUQWYRlmQJL+k4xOnrQ== X-Received: by 2002:a05:6a20:1615:b0:198:ae17:6a86 with SMTP id l21-20020a056a20161500b00198ae176a86mr636668pzj.10.1704300881041; Wed, 03 Jan 2024 08:54:41 -0800 (PST) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id p7-20020a17090a930700b0028b89520c7asm1954805pjo.9.2024.01.03.08.54.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 08:54:40 -0800 (PST) From: Samuel Holland To: Atish Patra , Anup Patel Cc: Samuel Holland , Albert Ou , Mark Rutland , Palmer Dabbelt , Paul Walmsley , Will Deacon , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [PATCH] perf: RISC-V: Check standard event availability Date: Wed, 3 Jan 2024 08:53:48 -0800 Message-ID: <20240103165438.633054-1-samuel.holland@sifive.com> X-Mailer: git-send-email 2.42.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787089036049848571 X-GMAIL-MSGID: 1787089036049848571 The RISC-V SBI PMU specification defines several standard hardware and cache events. Currently, all of these events appear in the `perf list` output, even if they are not actually implemented. Add logic to check which events are supported by the hardware (i.e. can be mapped to some counter), so only usable events are reported to userspace. Signed-off-by: Samuel Holland --- Before this patch: $ perf list hw List of pre-defined events (to be used in -e or -M): branch-instructions OR branches [Hardware event] branch-misses [Hardware event] bus-cycles [Hardware event] cache-misses [Hardware event] cache-references [Hardware event] cpu-cycles OR cycles [Hardware event] instructions [Hardware event] ref-cycles [Hardware event] stalled-cycles-backend OR idle-cycles-backend [Hardware event] stalled-cycles-frontend OR idle-cycles-frontend [Hardware event] $ perf stat -ddd true Performance counter stats for 'true': 4.36 msec task-clock # 0.744 CPUs utilized 1 context-switches # 229.325 /sec 0 cpu-migrations # 0.000 /sec 38 page-faults # 8.714 K/sec 4,375,694 cycles # 1.003 GHz (60.64%) 728,945 instructions # 0.17 insn per cycle 79,199 branches # 18.162 M/sec 17,709 branch-misses # 22.36% of all branches 181,734 L1-dcache-loads # 41.676 M/sec 5,547 L1-dcache-load-misses # 3.05% of all L1-dcache accesses LLC-loads (0.00%) LLC-load-misses (0.00%) L1-icache-loads (0.00%) L1-icache-load-misses (0.00%) dTLB-loads (0.00%) dTLB-load-misses (0.00%) iTLB-loads (0.00%) iTLB-load-misses (0.00%) L1-dcache-prefetches (0.00%) L1-dcache-prefetch-misses (0.00%) 0.005860375 seconds time elapsed 0.000000000 seconds user 0.010383000 seconds sys After this patch: $ perf list hw List of pre-defined events (to be used in -e or -M): branch-instructions OR branches [Hardware event] branch-misses [Hardware event] cache-misses [Hardware event] cache-references [Hardware event] cpu-cycles OR cycles [Hardware event] instructions [Hardware event] $ perf stat -ddd true Performance counter stats for 'true': 5.16 msec task-clock # 0.848 CPUs utilized 1 context-switches # 193.817 /sec 0 cpu-migrations # 0.000 /sec 37 page-faults # 7.171 K/sec 5,183,625 cycles # 1.005 GHz 961,696 instructions # 0.19 insn per cycle 85,853 branches # 16.640 M/sec 20,462 branch-misses # 23.83% of all branches 243,545 L1-dcache-loads # 47.203 M/sec 5,974 L1-dcache-load-misses # 2.45% of all L1-dcache accesses LLC-loads LLC-load-misses L1-icache-loads L1-icache-load-misses dTLB-loads 19,619 dTLB-load-misses iTLB-loads 6,831 iTLB-load-misses L1-dcache-prefetches L1-dcache-prefetch-misses 0.006085625 seconds time elapsed 0.000000000 seconds user 0.013022000 seconds sys drivers/perf/riscv_pmu_sbi.c | 37 ++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 16acd4dcdb96..b58a70ee8317 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -86,7 +86,7 @@ struct sbi_pmu_event_data { }; }; -static const struct sbi_pmu_event_data pmu_hw_event_map[] = { +static struct sbi_pmu_event_data pmu_hw_event_map[] = { [PERF_COUNT_HW_CPU_CYCLES] = {.hw_gen_event = { SBI_PMU_HW_CPU_CYCLES, SBI_PMU_EVENT_TYPE_HW, 0}}, @@ -120,7 +120,7 @@ static const struct sbi_pmu_event_data pmu_hw_event_map[] = { }; #define C(x) PERF_COUNT_HW_CACHE_##x -static const struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_MAX] +static struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_MAX] [PERF_COUNT_HW_CACHE_OP_MAX] [PERF_COUNT_HW_CACHE_RESULT_MAX] = { [C(L1D)] = { @@ -265,6 +265,36 @@ static const struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_M }, }; +static void pmu_sbi_check_event(struct sbi_pmu_event_data *edata) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, + 0, cmask, 0, edata->event_idx, 0, 0); + if (!ret.error) { + sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, + ret.value, 0x1, SBI_PMU_STOP_FLAG_RESET, 0, 0, 0); + } else if (ret.error == SBI_ERR_NOT_SUPPORTED) { + /* This event cannot be monitored by any counter */ + edata->event_idx = -EINVAL; + } +} + +static void pmu_sbi_update_events(void) +{ + /* Ensure events are not already mapped to a counter */ + sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, + 0, cmask, SBI_PMU_STOP_FLAG_RESET, 0, 0, 0); + + for (int i = 0; i < ARRAY_SIZE(pmu_hw_event_map); i++) + pmu_sbi_check_event(&pmu_hw_event_map[i]); + + for (int i = 0; i < ARRAY_SIZE(pmu_cache_event_map); i++) + for (int j = 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++) + for (int k = 0; k < ARRAY_SIZE(pmu_cache_event_map[i][j]); k++) + pmu_sbi_check_event(&pmu_cache_event_map[i][j][k]); +} + static int pmu_sbi_ctr_get_width(int idx) { return pmu_ctr_list[idx].width; @@ -1046,6 +1076,9 @@ static int pmu_sbi_device_probe(struct platform_device *pdev) if (pmu_sbi_get_ctrinfo(num_counters, &cmask)) goto out_free; + /* Check which standard events are available */ + pmu_sbi_update_events(); + ret = pmu_sbi_setup_irqs(pmu, pdev); if (ret < 0) { pr_info("Perf sampling/filtering is not supported as sscof extension is not available\n");