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 2C419F531EC for ; Tue, 14 Apr 2026 03:43:43 +0000 (UTC) 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=pNKulasNs9yVPrg3LOrfSyWmh9Y+Af/c/Rizd3X3Rts=; b=qDbIRKNtMENG31 YnAFr5RATlk4PqYrhMT+ysyC3LwCxqYbkwqQXdahAHBoG06MJiWI4QqpmP93SfjUUbJqgDJx7lRpU TSOE4fi0U9ofEAeZ2syZ5MxH3JbiRVKwF/85knLrABSnuk7o/IDf2La+Jlwvc92jchrxtG8P2gKyY gEmGkmmsphyfCfah7ceLXVuyLTYdNLZDzbe0B/KPphldP8ZvjVTnXDcEfS713+HixikfkER8Dl61L 8ojuJuQ+MxtAwrQb92dYHQzpvkJLwV3R6C1aJUOzN7uYxYMoaPj/sFG3WXN2dopZyFtXo54dUjPhC 4wCoDy1gedy4ejJKnOng==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1wCUgY-0000000GexB-2z27; Tue, 14 Apr 2026 03:43:34 +0000 Received: from smtpbgeu1.qq.com ([52.59.177.22]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1wCUgS-0000000GelY-1Jpw for linux-riscv@lists.infradead.org; Tue, 14 Apr 2026 03:43:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.spacemit.com; s=mxsw2412; t=1776138159; bh=vwV8U5nePLhsGJNgEaagtT6TMD03SbaAsVQl0DHbh5c=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=tSP/pRPAOx7GFPC596RVZGKpv28iJrOmc8gg/6pSWvT+U/DaztwbTfQjduzBRgUvx 9ICTbsVB5NZ7WF9J8dUyon7u0rnRV1vI6rJpGAo23ycTIm75V+vVdxp0OJ35lEytb8 R7bYGaPbea7DhJzpwR9yfeqEF6xKfwoW7/7233wc= X-QQ-mid: zesmtpsz5t1776138150t749d9950 X-QQ-Originating-IP: /JIWyjmHvjx2KWmNpLuZqOjdsDCC8TgLFnXm8KMJAOE= Received: from snode5.. ( [61.145.255.150]) by bizesmtp.qq.com (ESMTP) with id ; Tue, 14 Apr 2026 11:42:27 +0800 (CST) X-QQ-SSF: 0000000000000000000000000000000 X-QQ-GoodBg: 0 X-BIZMAIL-ID: 16457555842091747368 EX-QQ-RecipientCnt: 26 From: Zane Leung To: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, palmer@dabbelt.com, pjw@kernel.org, gregkh@linuxfoundation.org, alexander.shishkin@linux.intel.com, irogers@google.com Cc: coresight@lists.linaro.org, peterz@infradead.org, mingo@redhat.com, namhyung@kernel.org, mark.rutland@arm.com, jolsa@kernel.org, adrian.hunter@intel.com, kan.liang@linux.intel.com, mchitale@gmail.com, anup@brainfault.org, atish.patra@linux.dev, andrew.jones@oss.qualcomm.com, sunilvl@oss.qualcomm.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, anup.patel@oss.qualcomm.com, mayuresh.chitale@oss.qualcomm.com, zhuangqiubin@linux.spacemit.com Subject: [RFC PATCH 09/12] perf tools: Add RISC-V trace PMU record capabilities Date: Tue, 14 Apr 2026 11:41:50 +0800 Message-Id: <20260414034153.3272485-10-liangzhen@linux.spacemit.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260414034153.3272485-1-liangzhen@linux.spacemit.com> References: <20260414034153.3272485-1-liangzhen@linux.spacemit.com> MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: zesmtpsz:linux.spacemit.com:qybglogicsvrgz:qybglogicsvrgz6b-0 X-QQ-XMAILINFO: NrsCZA1AtzF5rY2lPBizRF7fjjRjllzJJBdRT5o/IN7iSlk+kNoLp/mr Noie1zWf5025ClIO1U12eZg7a1KqHzzvFWYD+L8NqaZjSHtUyYtW0CkhSsvXiJltXdfJ2w5 xWuCf5SR9hAkpc/KpiG7n+RGT7/1Zq8TY0U6ZcvhXydf7AiPR3igJiDx6mWPNoAgTz3qk7j Kz13Olri5AJHZlZ2GrLZiI45xcmqlET/is3db6P2kF9OjmnbiY49qfem5RiSi50lAY30UC/ PFaJhakwW7JFSWBhrqclNBQhjMmIZyt9UkG1ObRVJhCgaMi87g087k/6IuflBlpXRBJ/Ckf jQdECSY36h7ONGcwLA01FEsNc3KBLo1tD89Gn6WBwPAy5HK8DwL70hz3Lj2X8/mULwUpzrU h4dvk8CK0ByTcDXeB/aGQMe7DO1GvX4+zHzQHzoxRAHlH2D2Hww+upEVOxAtW2r1E728Fpn 5If/gDvvfmbGvsas3NlL2gMn65sAgJmnzksrkxGe9HJS+B6ObOMw12BphIxsfkWRi8MhwFU 3bNie2fsTVe/hH2QTknMh3+5/T4xweA/SaScxK9iagQKinmTe4Mg9j6sO5kj7GurngNNt3E jB64Zt7L0rAygq4DWL7VU63R1x/fldwjgCxZSTUxGfjm88spUEr+5cEEvctKe/fN2nMWp5c iZ93XqaHfrqviZAaH1LLRSFhrCPV+7V2KD0ghc+qdIONhit3CT5/ZttmV99cP23hiLK3iXL vJckMpHizb3ls3qn+NPbsmpTuUItA8tflgo6hfv25ruFrjnsI2JqxI838UjJ0/VTa3H2Kkt XeorHkgScDV3H4a9Cp7qAbBHH51j8txygBwlUCIGwbw1uqcjQm/vWXRN54eAulCcDgdNWS5 GeSVTvRgfUDxUXJ4nSkpWIc+DZLglH4U5JOvtkaQz4A6Zy6DlQrAReOQpoAZIC6/gIlKivG E+04sMlAO13Xg09AW7eflFtiS3dH6s/q6RrUFla/VcnbNRaWinYqsIFDX3ddfA+UdHcjvnD UDjjH+pGS3BdGlIXG8vT113rsg5QzvXZluPDXJ/Vd1DYi4i3MIyif3G/15ZzYrMt5apUx51 rfu9a6crkROhWoJp8Da0w3pCordlzm3hQ== X-QQ-XMRINFO: OD9hHCdaPRBwH5bRRRw8tsiH4UAatJqXfg== X-QQ-RECHKSPAM: 0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260413_204328_825887_6B1419FD X-CRM114-Status: GOOD ( 25.67 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: liangzhen Introduce the required auxiliary API functions allowing the perf core to interact with RISC-V trace perf driver. Signed-off-by: liangzhen --- tools/perf/arch/riscv/util/Build | 2 +- tools/perf/arch/riscv/util/auxtrace.c | 489 ++++++++++++++++++++++++++ tools/perf/util/auxtrace.c | 1 + tools/perf/util/auxtrace.h | 1 + tools/perf/util/rvtrace.h | 38 ++ 5 files changed, 530 insertions(+), 1 deletion(-) create mode 100644 tools/perf/arch/riscv/util/auxtrace.c create mode 100644 tools/perf/util/rvtrace.h diff --git a/tools/perf/arch/riscv/util/Build b/tools/perf/arch/riscv/util/Build index 748068a3a5c5..10f41f97bc2e 100644 --- a/tools/perf/arch/riscv/util/Build +++ b/tools/perf/arch/riscv/util/Build @@ -1,3 +1,3 @@ perf-util-y += header.o -perf-util-y += pmu.o +perf-util-y += pmu.o auxtrace.o diff --git a/tools/perf/arch/riscv/util/auxtrace.c b/tools/perf/arch/riscv/util/auxtrace.c new file mode 100644 index 000000000000..5feee198ef97 --- /dev/null +++ b/tools/perf/arch/riscv/util/auxtrace.c @@ -0,0 +1,489 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright(C) 2026 Spacemit Limited. All rights reserved. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include // page_size +#include "../../../util/auxtrace.h" +#include "../../../util/cpumap.h" +#include "../../../util/debug.h" +#include "../../../util/event.h" +#include "../../../util/evlist.h" +#include "../../../util/evsel.h" +#include "../../../util/rvtrace.h" +#include "../../../util/pmu.h" +#include "../../../util/record.h" +#include "../../../util/session.h" +#include "../../../util/tsc.h" +#include "../../../util/evsel_config.h" + +#define RVTRACE_PMU_NAME "rvtrace" +#define KiB(x) ((x) * 1024) +#define MiB(x) ((x) * 1024 * 1024) + +static const char * const metadata_encoder_ro[] = { + [RVTRACE_ENCODER_FORMAT] = "control/format", + [RVTRACE_ENCODER_CONTEXT] = "control/context", + [RVTRACE_ENCODER_INHB_SRC] = "control/inhb_src", + [RVTRACE_ENCODER_SRCBITS] = "features/srcb", + [RVTRACE_ENCODER_SRCID] = "features/srcid" +}; + +struct rvtrace_recording { + struct auxtrace_record itr; + struct perf_pmu *rvtrace_pmu; + struct evlist *evlist; + bool snapshot_mode; + size_t snapshot_size; +}; + +static int rvtrace_parse_snapshot_options(struct auxtrace_record *itr, + struct record_opts *opts, + const char *str) +{ + struct rvtrace_recording *ptr = + container_of(itr, struct rvtrace_recording, itr); + unsigned long long snapshot_size = 0; + char *endptr; + + if (str) { + snapshot_size = strtoull(str, &endptr, 0); + if (*endptr || snapshot_size > SIZE_MAX) + return -1; + } + + opts->auxtrace_snapshot_mode = true; + opts->auxtrace_snapshot_size = snapshot_size; + ptr->snapshot_size = snapshot_size; + + return 0; +} + +static size_t rvtrace_info_priv_size(struct auxtrace_record *itr __maybe_unused, + struct evlist *evlist __maybe_unused) +{ + int encoder; + struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus; + struct perf_cpu_map *intersect_cpus; + + if (!perf_cpu_map__has_any_cpu(event_cpus)) { + /* cpu map is not "any" CPU , we have specific CPUs to work with */ + struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus(); + + intersect_cpus = perf_cpu_map__intersect(event_cpus, online_cpus); + perf_cpu_map__put(online_cpus); + } else { + /* Event can be "any" CPU so count all online CPUs. */ + intersect_cpus = perf_cpu_map__new_online_cpus(); + } + + encoder = perf_cpu_map__nr(intersect_cpus); + perf_cpu_map__put(intersect_cpus); + + return (RVTRACE_HEADER_SIZE + encoder * RVTRACE_ENCODER_PRIV_SIZE); +} + +static int rvtrace_get_ro(struct perf_pmu *pmu, struct perf_cpu cpu, const char *path, __u64 *val) +{ + char pmu_path[PATH_MAX]; + int scan; + + /* Get RO metadata from sysfs */ + snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu.cpu, path); + + scan = perf_pmu__scan_file(pmu, pmu_path, "%llx", val); + if (scan != 1) { + pr_err("%s: error reading: %s\n", __func__, pmu_path); + return -EINVAL; + } + + return 0; +} + +static void rvtrace_get_metadata(struct perf_cpu cpu, u32 *offset, + struct auxtrace_record *itr, + struct perf_record_auxtrace_info *info) +{ + struct rvtrace_recording *ptr = container_of(itr, struct rvtrace_recording, itr); + struct perf_pmu *rvtrace_pmu = ptr->rvtrace_pmu; + + info->priv[*offset + RVTRACE_ENCODER_CPU] = cpu.cpu; + info->priv[*offset + RVTRACE_ENCODER_NR_TRC_PARAMS] = RVTRACE_ENCODER_NR_TRC_PARAMS_LENGTH; + + /* Get read-only information from sysFS */ + rvtrace_get_ro(rvtrace_pmu, cpu, metadata_encoder_ro[RVTRACE_ENCODER_FORMAT], + &info->priv[*offset + RVTRACE_ENCODER_FORMAT]); + rvtrace_get_ro(rvtrace_pmu, cpu, metadata_encoder_ro[RVTRACE_ENCODER_CONTEXT], + &info->priv[*offset + RVTRACE_ENCODER_CONTEXT]); + rvtrace_get_ro(rvtrace_pmu, cpu, metadata_encoder_ro[RVTRACE_ENCODER_INHB_SRC], + &info->priv[*offset + RVTRACE_ENCODER_INHB_SRC]); + rvtrace_get_ro(rvtrace_pmu, cpu, metadata_encoder_ro[RVTRACE_ENCODER_SRCBITS], + &info->priv[*offset + RVTRACE_ENCODER_SRCBITS]); + rvtrace_get_ro(rvtrace_pmu, cpu, metadata_encoder_ro[RVTRACE_ENCODER_SRCID], + &info->priv[*offset + RVTRACE_ENCODER_SRCID]); + + /* Where the next CPU entry should start from */ + *offset += RVTRACE_ENCODER_PRIV_MAX; +} + +static int rvtrace_info_fill(struct auxtrace_record *itr, struct perf_session *session, + struct perf_record_auxtrace_info *auxtrace_info, size_t priv_size) +{ + int i; + u32 offset; + u64 nr_cpu, type; + struct perf_cpu_map *cpu_map; + struct perf_cpu_map *event_cpus = session->evlist->core.user_requested_cpus; + struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus(); + struct rvtrace_recording *ptr = container_of(itr, struct rvtrace_recording, itr); + struct perf_pmu *rvtrace_pmu = ptr->rvtrace_pmu; + struct perf_cpu cpu; + + if (priv_size != rvtrace_info_priv_size(itr, session->evlist)) + return -EINVAL; + + if (!session->evlist->core.nr_mmaps) + return -EINVAL; + + /* If the cpu_map has the "any" CPU all online CPUs are involved */ + if (perf_cpu_map__has_any_cpu(event_cpus)) { + cpu_map = online_cpus; + } else { + /* Make sure all specified CPUs are online */ + perf_cpu_map__for_each_cpu(cpu, i, event_cpus) { + if (!perf_cpu_map__has(online_cpus, cpu)) + return -EINVAL; + } + + cpu_map = event_cpus; + } + + nr_cpu = perf_cpu_map__nr(cpu_map); + type = rvtrace_pmu->type; + + /* First fill out the session header */ + auxtrace_info->type = PERF_AUXTRACE_RISCV_TRACE; + auxtrace_info->priv[RVTRACE_PMU_TYPE_CPUS] = type << 32; + auxtrace_info->priv[RVTRACE_PMU_TYPE_CPUS] |= nr_cpu; + + offset = RVTRACE_HEADER_MAX; + + perf_cpu_map__for_each_cpu(cpu, i, cpu_map) { + assert(offset < priv_size); + rvtrace_get_metadata(cpu, &offset, itr, auxtrace_info); + } + + perf_cpu_map__put(online_cpus); + + return 0; +} + +static int rvtrace_set_sink_attr(struct perf_pmu *pmu, + struct evsel *evsel) +{ + char msg[BUFSIZ], path[PATH_MAX], *sink; + struct evsel_config_term *term; + int ret = -EINVAL; + u32 hash; + + if (evsel->core.attr.config2 & GENMASK(31, 0)) + return 0; + + list_for_each_entry(term, &evsel->config_terms, list) { + if (term->type != EVSEL__CONFIG_TERM_DRV_CFG) + continue; + + sink = term->val.str; + snprintf(path, PATH_MAX, "sinks/%s", sink); + + ret = perf_pmu__scan_file(pmu, path, "%x", &hash); + if (ret != 1) { + if (errno == ENOENT) + pr_err("Couldn't find sink \"%s\" on event %s\n" + "Missing kernel or device support?\n\n" + "Hint: An appropriate sink will be picked automatically if one isn't specified.\n", + sink, evsel__name(evsel)); + else + pr_err("Failed to set sink \"%s\" on event %s with %d (%s)\n", + sink, evsel__name(evsel), errno, + str_error_r(errno, msg, sizeof(msg))); + return ret; + } + + evsel->core.attr.config2 |= hash; + return 0; + } + + /* + * No sink was provided on the command line - allow the CoreSight + * system to look for a default + */ + return 0; +} + +static int rvtrace_recording_options(struct auxtrace_record *itr, struct evlist *evlist, + struct record_opts *opts) +{ + struct rvtrace_recording *ptr = container_of(itr, struct rvtrace_recording, itr); + struct perf_pmu *rvtrace_pmu = ptr->rvtrace_pmu; + struct evsel *evsel, *rvtrace_evsel = NULL; + struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; + bool privileged = perf_event_paranoid_check(-1); + struct evsel *tracking_evsel; + int err; + + ptr->evlist = evlist; + ptr->snapshot_mode = opts->auxtrace_snapshot_mode; + evlist__for_each_entry(evlist, evsel) { + if (evsel->core.attr.type == rvtrace_pmu->type) { + if (rvtrace_evsel) { + pr_err("There may be only one " RVTRACE_PMU_NAME "x event\n"); + return -EINVAL; + } + evsel->core.attr.freq = 0; + evsel->core.attr.sample_period = 1; + evsel->needs_auxtrace_mmap = true; + rvtrace_evsel = evsel; + opts->full_auxtrace = true; + } + } + + if (!opts->full_auxtrace) + return 0; + + err = rvtrace_set_sink_attr(rvtrace_pmu, rvtrace_evsel); + if (err) + return err; + + /* we are in snapshot mode */ + if (opts->auxtrace_snapshot_mode) { + /* + * No size were given to '-S' or '-m,', so go with + * the default + */ + if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) { + if (privileged) { + opts->auxtrace_mmap_pages = MiB(4) / page_size; + } else { + opts->auxtrace_mmap_pages = KiB(128) / page_size; + if (opts->mmap_pages == UINT_MAX) + opts->mmap_pages = KiB(256) / page_size; + } + } else if (!opts->auxtrace_mmap_pages && !privileged && + opts->mmap_pages == UINT_MAX) { + opts->mmap_pages = KiB(256) / page_size; + } + + /* + * '-m,xyz' was specified but no snapshot size, so make the + * snapshot size as big as the auxtrace mmap area. + */ + if (!opts->auxtrace_snapshot_size) { + opts->auxtrace_snapshot_size = + opts->auxtrace_mmap_pages * (size_t)page_size; + } + + /* + * -Sxyz was specified but no auxtrace mmap area, so make the + * auxtrace mmap area big enough to fit the requested snapshot + * size. + */ + if (!opts->auxtrace_mmap_pages) { + size_t sz = opts->auxtrace_snapshot_size; + + sz = round_up(sz, page_size) / page_size; + opts->auxtrace_mmap_pages = roundup_pow_of_two(sz); + } + + /* Snapshot size can't be bigger than the auxtrace area */ + if (opts->auxtrace_snapshot_size > + opts->auxtrace_mmap_pages * (size_t)page_size) { + pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n", + opts->auxtrace_snapshot_size, + opts->auxtrace_mmap_pages * (size_t)page_size); + return -EINVAL; + } + + /* Something went wrong somewhere - this shouldn't happen */ + if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) { + pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n"); + return -EINVAL; + } + + pr_debug2("%s snapshot size: %zu\n", RVTRACE_PMU_NAME, + opts->auxtrace_snapshot_size); + } + + /* Buffer sizes weren't specified with '-m,xyz' so give some defaults */ + if (!opts->auxtrace_mmap_pages) { + if (privileged) { + opts->auxtrace_mmap_pages = MiB(4) / page_size; + } else { + opts->auxtrace_mmap_pages = KiB(128) / page_size; + if (opts->mmap_pages == UINT_MAX) + opts->mmap_pages = KiB(256) / page_size; + } + } + + /* Validate auxtrace_mmap_pages */ + if (opts->auxtrace_mmap_pages) { + size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size; + size_t min_sz; + + if (opts->auxtrace_snapshot_mode) + min_sz = KiB(4); + else + min_sz = KiB(8); + + if (sz < min_sz || !is_power_of_2(sz)) { + pr_err("Invalid mmap size for Intel Processor Trace: must be at least %zuKiB and a power of 2\n", + min_sz / 1024); + return -EINVAL; + } + } + + /* + * To obtain the auxtrace buffer file descriptor, the auxtrace event + * must come first. + */ + evlist__to_front(evlist, rvtrace_evsel); + + /* + * get the CPU on the sample - need it to associate trace ID in the + * AUX_OUTPUT_HW_ID event, and the AUX event for per-cpu mmaps. + */ + evsel__set_sample_bit(rvtrace_evsel, CPU); + + /* Add dummy event to keep tracking */ + err = parse_event(evlist, "dummy:u"); + if (err) + return err; + + tracking_evsel = evlist__last(evlist); + evlist__set_tracking_event(evlist, tracking_evsel); + + tracking_evsel->core.attr.freq = 0; + tracking_evsel->core.attr.sample_period = 1; + + /* In per-cpu case, always need the time of mmap events etc */ + if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) + evsel__set_sample_bit(tracking_evsel, TIME); + + return 0; +} + +static int rvtrace_snapshot_start(struct auxtrace_record *itr) +{ + struct rvtrace_recording *ptr = + container_of(itr, struct rvtrace_recording, itr); + struct evsel *evsel; + + evlist__for_each_entry(ptr->evlist, evsel) { + if (evsel->core.attr.type == ptr->rvtrace_pmu->type) + return evsel__disable(evsel); + } + return -EINVAL; +} + +static int rvtrace_snapshot_finish(struct auxtrace_record *itr) +{ + struct rvtrace_recording *ptr = + container_of(itr, struct rvtrace_recording, itr); + struct evsel *evsel; + + evlist__for_each_entry(ptr->evlist, evsel) { + if (evsel->core.attr.type == ptr->rvtrace_pmu->type) + return evsel__enable(evsel); + } + return -EINVAL; +} + +static u64 rvtrace_reference(struct auxtrace_record *itr __maybe_unused) +{ + return rdtsc(); +} + +static void rvtrace_recording_free(struct auxtrace_record *itr) +{ + struct rvtrace_recording *ptr = + container_of(itr, struct rvtrace_recording, itr); + + free(ptr); +} + +static struct auxtrace_record *rvtrace_recording_init(int *err, struct perf_pmu *rvtrace_pmu) +{ + struct rvtrace_recording *ptr; + + if (!rvtrace_pmu) { + *err = -ENODEV; + return NULL; + } + + ptr = zalloc(sizeof(*ptr)); + if (!ptr) { + *err = -ENOMEM; + return NULL; + } + + ptr->rvtrace_pmu = rvtrace_pmu; + ptr->itr.parse_snapshot_options = rvtrace_parse_snapshot_options; + ptr->itr.recording_options = rvtrace_recording_options; + ptr->itr.info_priv_size = rvtrace_info_priv_size; + ptr->itr.info_fill = rvtrace_info_fill; + ptr->itr.snapshot_start = rvtrace_snapshot_start; + ptr->itr.snapshot_finish = rvtrace_snapshot_finish; + ptr->itr.free = rvtrace_recording_free; + ptr->itr.reference = rvtrace_reference; + ptr->itr.read_finish = auxtrace_record__read_finish; + ptr->itr.alignment = 0; + + *err = 0; + return &ptr->itr; +} + +static struct perf_pmu *find_pmu_for_event(struct perf_pmu **pmus, + int pmu_nr, struct evsel *evsel) +{ + int i; + + if (!pmus) + return NULL; + + for (i = 0; i < pmu_nr; i++) { + if (evsel->core.attr.type == pmus[i]->type) + return pmus[i]; + } + + return NULL; +} + +struct auxtrace_record *auxtrace_record__init(struct evlist *evlist, int *err) +{ + struct perf_pmu *rvtrace_pmu = NULL; + struct perf_pmu *found_rvtrace = NULL; + struct evsel *evsel; + + if (!evlist) + return NULL; + + rvtrace_pmu = perf_pmus__find(RVTRACE_PMU_NAME); + evlist__for_each_entry(evlist, evsel) { + if (rvtrace_pmu && !found_rvtrace) + found_rvtrace = find_pmu_for_event(&rvtrace_pmu, 1, evsel); + } + + if (found_rvtrace) + return rvtrace_recording_init(err, rvtrace_pmu); + + *err = 0; + return NULL; +} diff --git a/tools/perf/util/auxtrace.c b/tools/perf/util/auxtrace.c index a224687ffbc1..944a43d48739 100644 --- a/tools/perf/util/auxtrace.c +++ b/tools/perf/util/auxtrace.c @@ -1411,6 +1411,7 @@ int perf_event__process_auxtrace_info(const struct perf_tool *tool __maybe_unuse case PERF_AUXTRACE_VPA_DTL: err = powerpc_vpadtl_process_auxtrace_info(event, session); break; + case PERF_AUXTRACE_RISCV_TRACE: case PERF_AUXTRACE_UNKNOWN: default: return -EINVAL; diff --git a/tools/perf/util/auxtrace.h b/tools/perf/util/auxtrace.h index 6947f3f284c0..4f4714c1b53f 100644 --- a/tools/perf/util/auxtrace.h +++ b/tools/perf/util/auxtrace.h @@ -46,6 +46,7 @@ enum auxtrace_type { PERF_AUXTRACE_S390_CPUMSF, PERF_AUXTRACE_HISI_PTT, PERF_AUXTRACE_VPA_DTL, + PERF_AUXTRACE_RISCV_TRACE, }; enum itrace_period_type { diff --git a/tools/perf/util/rvtrace.h b/tools/perf/util/rvtrace.h new file mode 100644 index 000000000000..1e48ed989dd7 --- /dev/null +++ b/tools/perf/util/rvtrace.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright(C) 2026 Spacemit Limited. All rights reserved. + */ + +#ifndef INCLUDE__UTIL_PERF_RVTRACE_H__ +#define INCLUDE__UTIL_PERF_RVTRACE_H__ + +#include "debug.h" +#include "auxtrace.h" +#include "util/event.h" +#include "util/session.h" +#include + +enum { + /* PMU->type (32 bit), total # of CPUs (32 bit) */ + RVTRACE_PMU_TYPE_CPUS, + RVTRACE_HEADER_MAX, +}; + +/* Trace Encoder metadata */ +enum { + RVTRACE_ENCODER_CPU, + RVTRACE_ENCODER_NR_TRC_PARAMS, + RVTRACE_ENCODER_FORMAT, + RVTRACE_ENCODER_CONTEXT, + RVTRACE_ENCODER_INHB_SRC, + RVTRACE_ENCODER_SRCBITS, + RVTRACE_ENCODER_SRCID, + RVTRACE_ENCODER_PRIV_MAX, +}; + +#define RVTRACE_ENCODER_NR_TRC_PARAMS_LENGTH (RVTRACE_ENCODER_PRIV_MAX - RVTRACE_ENCODER_FORMAT) + +#define RVTRACE_HEADER_SIZE (RVTRACE_HEADER_MAX * sizeof(u64)) +#define RVTRACE_ENCODER_PRIV_SIZE (RVTRACE_ENCODER_PRIV_MAX * sizeof(u64)) + +#endif -- 2.34.1 _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv