Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf auxtrace arm64: Add support for HiSilicon PCIe Tune and Trace device driver

HiSilicon PCIe tune and trace device (PTT) could dynamically tune the
PCIe link's events, and trace the TLP headers).

This patch add support for PTT device in perf tool, so users could use
'perf record' to get TLP headers trace data.

Reviewed-by: Leo Yan <leo.yan@linaro.org>
Signed-off-by: Qi Liu <liuqi115@huawei.com>
Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
Acked-by: John Garry <john.garry@huawei.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Bjorn Helgaas <helgaas@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jonathan Cameron <jonathan.cameron@huawei.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Qi Liu <liuqi6124@gmail.com>
Cc: Shameerali Kolothum Thodi <shameerali.kolothum.thodi@huawei.com>
Cc: Shaokun Zhang <zhangshaokun@hisilicon.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Zeng Prime <prime.zeng@huawei.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-pci@vger.kernel.org
Cc: linuxarm@huawei.com
Link: https://lore.kernel.org/r/20220927081400.14364-3-yangyicong@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Qi Liu and committed by
Arnaldo Carvalho de Melo
057381a7 45a3975f

+273 -1
+63
tools/perf/arch/arm/util/auxtrace.c
··· 4 4 * Author: Mathieu Poirier <mathieu.poirier@linaro.org> 5 5 */ 6 6 7 + #include <dirent.h> 7 8 #include <stdbool.h> 8 9 #include <linux/coresight-pmu.h> 9 10 #include <linux/zalloc.h> 11 + #include <api/fs/fs.h> 10 12 11 13 #include "../../../util/auxtrace.h" 12 14 #include "../../../util/debug.h" ··· 16 14 #include "../../../util/pmu.h" 17 15 #include "cs-etm.h" 18 16 #include "arm-spe.h" 17 + #include "hisi-ptt.h" 19 18 20 19 static struct perf_pmu **find_all_arm_spe_pmus(int *nr_spes, int *err) 21 20 { ··· 53 50 return arm_spe_pmus; 54 51 } 55 52 53 + static struct perf_pmu **find_all_hisi_ptt_pmus(int *nr_ptts, int *err) 54 + { 55 + const char *sysfs = sysfs__mountpoint(); 56 + struct perf_pmu **hisi_ptt_pmus = NULL; 57 + struct dirent *dent; 58 + char path[PATH_MAX]; 59 + DIR *dir = NULL; 60 + int idx = 0; 61 + 62 + snprintf(path, PATH_MAX, "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); 63 + dir = opendir(path); 64 + if (!dir) { 65 + pr_err("can't read directory '%s'\n", EVENT_SOURCE_DEVICE_PATH); 66 + *err = -EINVAL; 67 + return NULL; 68 + } 69 + 70 + while ((dent = readdir(dir))) { 71 + if (strstr(dent->d_name, HISI_PTT_PMU_NAME)) 72 + (*nr_ptts)++; 73 + } 74 + 75 + if (!(*nr_ptts)) 76 + goto out; 77 + 78 + hisi_ptt_pmus = zalloc(sizeof(struct perf_pmu *) * (*nr_ptts)); 79 + if (!hisi_ptt_pmus) { 80 + pr_err("hisi_ptt alloc failed\n"); 81 + *err = -ENOMEM; 82 + goto out; 83 + } 84 + 85 + rewinddir(dir); 86 + while ((dent = readdir(dir))) { 87 + if (strstr(dent->d_name, HISI_PTT_PMU_NAME) && idx < *nr_ptts) { 88 + hisi_ptt_pmus[idx] = perf_pmu__find(dent->d_name); 89 + if (hisi_ptt_pmus[idx]) 90 + idx++; 91 + } 92 + } 93 + 94 + out: 95 + closedir(dir); 96 + return hisi_ptt_pmus; 97 + } 98 + 56 99 static struct perf_pmu *find_pmu_for_event(struct perf_pmu **pmus, 57 100 int pmu_nr, struct evsel *evsel) 58 101 { ··· 120 71 { 121 72 struct perf_pmu *cs_etm_pmu = NULL; 122 73 struct perf_pmu **arm_spe_pmus = NULL; 74 + struct perf_pmu **hisi_ptt_pmus = NULL; 123 75 struct evsel *evsel; 124 76 struct perf_pmu *found_etm = NULL; 125 77 struct perf_pmu *found_spe = NULL; 78 + struct perf_pmu *found_ptt = NULL; 126 79 int auxtrace_event_cnt = 0; 127 80 int nr_spes = 0; 81 + int nr_ptts = 0; 128 82 129 83 if (!evlist) 130 84 return NULL; 131 85 132 86 cs_etm_pmu = perf_pmu__find(CORESIGHT_ETM_PMU_NAME); 133 87 arm_spe_pmus = find_all_arm_spe_pmus(&nr_spes, err); 88 + hisi_ptt_pmus = find_all_hisi_ptt_pmus(&nr_ptts, err); 134 89 135 90 evlist__for_each_entry(evlist, evsel) { 136 91 if (cs_etm_pmu && !found_etm) ··· 142 89 143 90 if (arm_spe_pmus && !found_spe) 144 91 found_spe = find_pmu_for_event(arm_spe_pmus, nr_spes, evsel); 92 + 93 + if (hisi_ptt_pmus && !found_ptt) 94 + found_ptt = find_pmu_for_event(hisi_ptt_pmus, nr_ptts, evsel); 145 95 } 146 96 147 97 free(arm_spe_pmus); 98 + free(hisi_ptt_pmus); 148 99 149 100 if (found_etm) 150 101 auxtrace_event_cnt++; 151 102 152 103 if (found_spe) 104 + auxtrace_event_cnt++; 105 + 106 + if (found_ptt) 153 107 auxtrace_event_cnt++; 154 108 155 109 if (auxtrace_event_cnt > 1) { ··· 171 111 #if defined(__aarch64__) 172 112 if (found_spe) 173 113 return arm_spe_recording_init(err, found_spe); 114 + 115 + if (found_ptt) 116 + return hisi_ptt_recording_init(err, found_ptt); 174 117 #endif 175 118 176 119 /*
+3
tools/perf/arch/arm/util/pmu.c
··· 10 10 #include <linux/string.h> 11 11 12 12 #include "arm-spe.h" 13 + #include "hisi-ptt.h" 13 14 #include "../../../util/pmu.h" 14 15 15 16 struct perf_event_attr ··· 23 22 #if defined(__aarch64__) 24 23 } else if (strstarts(pmu->name, ARM_SPE_PMU_NAME)) { 25 24 return arm_spe_pmu_default_config(pmu); 25 + } else if (strstarts(pmu->name, HISI_PTT_PMU_NAME)) { 26 + pmu->selectable = true; 26 27 #endif 27 28 } 28 29
+1 -1
tools/perf/arch/arm64/util/Build
··· 11 11 perf-$(CONFIG_AUXTRACE) += ../../arm/util/pmu.o \ 12 12 ../../arm/util/auxtrace.o \ 13 13 ../../arm/util/cs-etm.o \ 14 - arm-spe.o mem-events.o 14 + arm-spe.o mem-events.o hisi-ptt.o
+188
tools/perf/arch/arm64/util/hisi-ptt.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * HiSilicon PCIe Trace and Tuning (PTT) support 4 + * Copyright (c) 2022 HiSilicon Technologies Co., Ltd. 5 + */ 6 + 7 + #include <linux/kernel.h> 8 + #include <linux/types.h> 9 + #include <linux/bitops.h> 10 + #include <linux/log2.h> 11 + #include <linux/zalloc.h> 12 + #include <time.h> 13 + 14 + #include <internal/lib.h> // page_size 15 + #include "../../../util/auxtrace.h" 16 + #include "../../../util/cpumap.h" 17 + #include "../../../util/debug.h" 18 + #include "../../../util/event.h" 19 + #include "../../../util/evlist.h" 20 + #include "../../../util/evsel.h" 21 + #include "../../../util/hisi-ptt.h" 22 + #include "../../../util/pmu.h" 23 + #include "../../../util/record.h" 24 + #include "../../../util/session.h" 25 + #include "../../../util/tsc.h" 26 + 27 + #define KiB(x) ((x) * 1024) 28 + #define MiB(x) ((x) * 1024 * 1024) 29 + 30 + struct hisi_ptt_recording { 31 + struct auxtrace_record itr; 32 + struct perf_pmu *hisi_ptt_pmu; 33 + struct evlist *evlist; 34 + }; 35 + 36 + static size_t 37 + hisi_ptt_info_priv_size(struct auxtrace_record *itr __maybe_unused, 38 + struct evlist *evlist __maybe_unused) 39 + { 40 + return HISI_PTT_AUXTRACE_PRIV_SIZE; 41 + } 42 + 43 + static int hisi_ptt_info_fill(struct auxtrace_record *itr, 44 + struct perf_session *session, 45 + struct perf_record_auxtrace_info *auxtrace_info, 46 + size_t priv_size) 47 + { 48 + struct hisi_ptt_recording *pttr = 49 + container_of(itr, struct hisi_ptt_recording, itr); 50 + struct perf_pmu *hisi_ptt_pmu = pttr->hisi_ptt_pmu; 51 + 52 + if (priv_size != HISI_PTT_AUXTRACE_PRIV_SIZE) 53 + return -EINVAL; 54 + 55 + if (!session->evlist->core.nr_mmaps) 56 + return -EINVAL; 57 + 58 + auxtrace_info->type = PERF_AUXTRACE_HISI_PTT; 59 + auxtrace_info->priv[0] = hisi_ptt_pmu->type; 60 + 61 + return 0; 62 + } 63 + 64 + static int hisi_ptt_set_auxtrace_mmap_page(struct record_opts *opts) 65 + { 66 + bool privileged = perf_event_paranoid_check(-1); 67 + 68 + if (!opts->full_auxtrace) 69 + return 0; 70 + 71 + if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) { 72 + if (privileged) { 73 + opts->auxtrace_mmap_pages = MiB(16) / page_size; 74 + } else { 75 + opts->auxtrace_mmap_pages = KiB(128) / page_size; 76 + if (opts->mmap_pages == UINT_MAX) 77 + opts->mmap_pages = KiB(256) / page_size; 78 + } 79 + } 80 + 81 + /* Validate auxtrace_mmap_pages */ 82 + if (opts->auxtrace_mmap_pages) { 83 + size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size; 84 + size_t min_sz = KiB(8); 85 + 86 + if (sz < min_sz || !is_power_of_2(sz)) { 87 + pr_err("Invalid mmap size for HISI PTT: must be at least %zuKiB and a power of 2\n", 88 + min_sz / 1024); 89 + return -EINVAL; 90 + } 91 + } 92 + 93 + return 0; 94 + } 95 + 96 + static int hisi_ptt_recording_options(struct auxtrace_record *itr, 97 + struct evlist *evlist, 98 + struct record_opts *opts) 99 + { 100 + struct hisi_ptt_recording *pttr = 101 + container_of(itr, struct hisi_ptt_recording, itr); 102 + struct perf_pmu *hisi_ptt_pmu = pttr->hisi_ptt_pmu; 103 + struct evsel *evsel, *hisi_ptt_evsel = NULL; 104 + struct evsel *tracking_evsel; 105 + int err; 106 + 107 + pttr->evlist = evlist; 108 + evlist__for_each_entry(evlist, evsel) { 109 + if (evsel->core.attr.type == hisi_ptt_pmu->type) { 110 + if (hisi_ptt_evsel) { 111 + pr_err("There may be only one " HISI_PTT_PMU_NAME "x event\n"); 112 + return -EINVAL; 113 + } 114 + evsel->core.attr.freq = 0; 115 + evsel->core.attr.sample_period = 1; 116 + evsel->needs_auxtrace_mmap = true; 117 + hisi_ptt_evsel = evsel; 118 + opts->full_auxtrace = true; 119 + } 120 + } 121 + 122 + err = hisi_ptt_set_auxtrace_mmap_page(opts); 123 + if (err) 124 + return err; 125 + /* 126 + * To obtain the auxtrace buffer file descriptor, the auxtrace event 127 + * must come first. 128 + */ 129 + evlist__to_front(evlist, hisi_ptt_evsel); 130 + evsel__set_sample_bit(hisi_ptt_evsel, TIME); 131 + 132 + /* Add dummy event to keep tracking */ 133 + err = parse_event(evlist, "dummy:u"); 134 + if (err) 135 + return err; 136 + 137 + tracking_evsel = evlist__last(evlist); 138 + evlist__set_tracking_event(evlist, tracking_evsel); 139 + 140 + tracking_evsel->core.attr.freq = 0; 141 + tracking_evsel->core.attr.sample_period = 1; 142 + evsel__set_sample_bit(tracking_evsel, TIME); 143 + 144 + return 0; 145 + } 146 + 147 + static u64 hisi_ptt_reference(struct auxtrace_record *itr __maybe_unused) 148 + { 149 + return rdtsc(); 150 + } 151 + 152 + static void hisi_ptt_recording_free(struct auxtrace_record *itr) 153 + { 154 + struct hisi_ptt_recording *pttr = 155 + container_of(itr, struct hisi_ptt_recording, itr); 156 + 157 + free(pttr); 158 + } 159 + 160 + struct auxtrace_record *hisi_ptt_recording_init(int *err, 161 + struct perf_pmu *hisi_ptt_pmu) 162 + { 163 + struct hisi_ptt_recording *pttr; 164 + 165 + if (!hisi_ptt_pmu) { 166 + *err = -ENODEV; 167 + return NULL; 168 + } 169 + 170 + pttr = zalloc(sizeof(*pttr)); 171 + if (!pttr) { 172 + *err = -ENOMEM; 173 + return NULL; 174 + } 175 + 176 + pttr->hisi_ptt_pmu = hisi_ptt_pmu; 177 + pttr->itr.pmu = hisi_ptt_pmu; 178 + pttr->itr.recording_options = hisi_ptt_recording_options; 179 + pttr->itr.info_priv_size = hisi_ptt_info_priv_size; 180 + pttr->itr.info_fill = hisi_ptt_info_fill; 181 + pttr->itr.free = hisi_ptt_recording_free; 182 + pttr->itr.reference = hisi_ptt_reference; 183 + pttr->itr.read_finish = auxtrace_record__read_finish; 184 + pttr->itr.alignment = 0; 185 + 186 + *err = 0; 187 + return &pttr->itr; 188 + }
+1
tools/perf/util/auxtrace.c
··· 1320 1320 case PERF_AUXTRACE_S390_CPUMSF: 1321 1321 err = s390_cpumsf_process_auxtrace_info(event, session); 1322 1322 break; 1323 + case PERF_AUXTRACE_HISI_PTT: 1323 1324 case PERF_AUXTRACE_UNKNOWN: 1324 1325 default: 1325 1326 return -EINVAL;
+1
tools/perf/util/auxtrace.h
··· 48 48 PERF_AUXTRACE_CS_ETM, 49 49 PERF_AUXTRACE_ARM_SPE, 50 50 PERF_AUXTRACE_S390_CPUMSF, 51 + PERF_AUXTRACE_HISI_PTT, 51 52 }; 52 53 53 54 enum itrace_period_type {
+16
tools/perf/util/hisi-ptt.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * HiSilicon PCIe Trace and Tuning (PTT) support 4 + * Copyright (c) 2022 HiSilicon Technologies Co., Ltd. 5 + */ 6 + 7 + #ifndef INCLUDE__PERF_HISI_PTT_H__ 8 + #define INCLUDE__PERF_HISI_PTT_H__ 9 + 10 + #define HISI_PTT_PMU_NAME "hisi_ptt" 11 + #define HISI_PTT_AUXTRACE_PRIV_SIZE sizeof(u64) 12 + 13 + struct auxtrace_record *hisi_ptt_recording_init(int *err, 14 + struct perf_pmu *hisi_ptt_pmu); 15 + 16 + #endif