From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f41.google.com (mail-dl1-f41.google.com [74.125.82.41]) (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 E00552BEC34 for ; Tue, 14 Apr 2026 23:26:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776209190; cv=none; b=D7edqWRwBFHS2bUUSU0u4h3JZogP8+GXtvwOTDAYXBoRD3DkzBBpfCWRtbrkHeSf/d5Z4src6TSRWxhuyLGgNVFt0kBXSQdXPLr7mXGuIGIFazIs/LIIghESN2rJEMuNyndOcpMbUkfx8EE5pwUqaAiz30ub5pOVlhInkLT1aV4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776209190; c=relaxed/simple; bh=pqGHxJBLAdtx6XOtYjbVZMh/eQQvLvofl1jtNikQ1ME=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=rPQNEiK/notUtyUgxqQ85cDZiq1O1jY5wGTJVhuttpz5msjv9m5XQiS07zvQcY85VkLn9OzyRfabg3AA7hGBavPs0MZkDEutc8p8kW5KD7xqElxRIzqlAqI+mt0Towq8hMBxrk/jEbH4rQFIl1S7mftx+JyLSMkkwC5GzcWLs50= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HVfVcbvS; arc=none smtp.client-ip=74.125.82.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HVfVcbvS" Received: by mail-dl1-f41.google.com with SMTP id a92af1059eb24-12c555f886dso2226201c88.0 for ; Tue, 14 Apr 2026 16:26:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776209188; x=1776813988; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=DQKwHS4WLZQFuuFkVYO5PQwY4AUg0AiUXq52eLyqUWE=; b=HVfVcbvSYlfZiDc/jTUiYHIOz8Gj6YfUoOhyyDaF9J8zgukvet+iFPfY4Y7ge3mAhP oVCB8EIQOQSiE21XwMyZLwp8jf7SfM6LtIbu9yz70ICh9R+1ql1YWJkDM/2/sd+Q9F6N BBoQ58csiflAiV7Leas7ECzOqAy6iTIB1HPBqH6cCtRElUaBdG7Qm86QtS7thTKJsqZs CYYTi9+yRI1Jvuv90yTLLBHI1ZniVl8Ar4vM7YYta/xI1C3djnCRb8v4KX5i7u+PiwYz ZqSZAGqcMRqfpLukI6BZK+vkVaavrrvLRaZWD1/NBopfDLJh9SDzhxe1hG3oJISWQsKR N0WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776209188; x=1776813988; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=DQKwHS4WLZQFuuFkVYO5PQwY4AUg0AiUXq52eLyqUWE=; b=P54DQUXasn4dQe0UIMs9dXd6otUwjIg3g2khimNRc0u9Dn4ylPu5YaJG92CRiasb+U aEAxeK/gjPim46GEKCCA79kbwrpcf7L3nwktNrsnz/p9vjDbEZMvNTScdUa28GsJZQoh qNOkwixnVTHwKnZxrJP4U6QSjP2Ru1USWLqrleB3lnZ7i1XXdStM5gDWIXY2SC/wQ4bj S8qjSmHA+LS0zKyoE91IdZAbp/2D2UrgOEQLcjx3iCIDgvHlRSqJIe7of15gDDcihTCG 3oTq+iiVC9pgWJdi24UnhDcwr2VmckaZAFLQ7kKwvVN5LRzNj7Lmin2As4MRZMEoK3L0 6OrQ== X-Forwarded-Encrypted: i=1; AFNElJ9F9YZ7tOaZ1BoSxKv6CtKCso9i1Q0lg91FYVRIL7wWw7nrkTvVLFMvv/qZJvC6N77SD7/kAEsO0LfyurA=@vger.kernel.org X-Gm-Message-State: AOJu0YyIDyEp7b6WYbx1EwuHUarCD5XQd76aS2VVFIqFqUPH7F4dC6kc o8HUj2ND1H8LjpYzmY1eKh8iz3QVisFCv1EWhRgAUQ1+XAPGnhkeZYjw X-Gm-Gg: AeBDieup+w8XinWisr+jARSXVvC0lqdC9vFPGWMKIpu7bovvNOsrqhxB5otjg7m4Q6H tmjBmdGx4uFbrVrFF4inHdrFTYpnNfc8sRJpYL/HVuc689iwMuoTxTk2lC1zD9Fd1yHgF96XHj9 Z0SokjHbGkinW0QmMu1rw7E0ZS0xmMcXzXXdlQvpH7sOVfK7Dey6xf7PCrIrFeYZeEiXGcpXKWN YznkZby1HINp72dtxs5KjU6YcZ0iKT7YaBvjDOv8rHsFEbK07tayd0f5LNOs7WaWRv2/2UoF2/6 EMvgDHeJVH8kDyD19DYvxrPZrTw2HkDKm/QDxGukPSWLOVDg2Kmq13mAwkfwV34UjeDzNwBfl81 mPJjPO+Et1XA0VgrQXdijl2gmfFDbl2g/dxt9r5DJOHVN5VWiHONJGnRB5ZPAwkwdrniudQpAGq txsQRLp5ef1ZovdGt+jvtu+9LITQpznPgyECYdxBOTLA6dF7Y= X-Received: by 2002:a05:7301:150d:b0:2da:1874:f39c with SMTP id 5a478bee46e88-2da1874f8e4mr4066308eec.12.1776209187358; Tue, 14 Apr 2026 16:26:27 -0700 (PDT) Received: from [172.16.0.242] ([192.19.161.250]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2de8eb84511sm83049eec.17.2026.04.14.16.26.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 14 Apr 2026 16:26:25 -0700 (PDT) Message-ID: <80ef37c7-ddfc-4268-b5d5-534c4c5091c3@gmail.com> Date: Tue, 14 Apr 2026 16:31:35 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH 09/12] perf tools: Add RISC-V trace PMU record capabilities To: Zane Leung , 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 References: <20260414034153.3272485-1-liangzhen@linux.spacemit.com> <20260414034153.3272485-10-liangzhen@linux.spacemit.com> Content-Language: en-US From: Bo Gan In-Reply-To: <20260414034153.3272485-10-liangzhen@linux.spacemit.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit NACK. The file is largely copied from tools/perf/arch/x86/util/intel-pt.c There's even markers like "Intel Processor Trace" in file auxtrace.c. There's no mentioning of authorship, but instead Spacemit claming its copyright over it. Bo On 4/13/26 20:41, Zane Leung wrote: > 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