1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright(C) 2015 Linaro Limited. All rights reserved. 4 * Author: Mathieu Poirier <mathieu.poirier@linaro.org> 5 */ 6 7 #include <dirent.h> 8 #include <errno.h> 9 #include <stdbool.h> 10 #include <linux/coresight-pmu.h> 11 #include <linux/zalloc.h> 12 #include <api/fs/fs.h> 13 14 #include "../../../util/auxtrace.h" 15 #include "../../../util/debug.h" 16 #include "../../../util/evlist.h" 17 #include "../../../util/pmu.h" 18 #include "../../../util/pmus.h" 19 #include "cs-etm.h" 20 #include "arm-spe.h" 21 #include "hisi-ptt.h" 22 23 static struct perf_pmu **find_all_arm_spe_pmus(int *nr_spes, int *err) 24 { 25 struct perf_pmu **arm_spe_pmus = NULL; 26 int ret, i, nr_cpus = sysconf(_SC_NPROCESSORS_CONF); 27 /* arm_spe_xxxxxxxxx\0 */ 28 char arm_spe_pmu_name[sizeof(ARM_SPE_PMU_NAME) + 10]; 29 30 arm_spe_pmus = zalloc(sizeof(struct perf_pmu *) * nr_cpus); 31 if (!arm_spe_pmus) { 32 pr_err("spes alloc failed\n"); 33 *err = -ENOMEM; 34 return NULL; 35 } 36 37 for (i = 0; i < nr_cpus; i++) { 38 ret = sprintf(arm_spe_pmu_name, "%s%d", ARM_SPE_PMU_NAME, i); 39 if (ret < 0) { 40 pr_err("sprintf failed\n"); 41 *err = -ENOMEM; 42 return NULL; 43 } 44 45 arm_spe_pmus[*nr_spes] = perf_pmus__find(arm_spe_pmu_name); 46 if (arm_spe_pmus[*nr_spes]) { 47 pr_debug2("%s %d: arm_spe_pmu %d type %d name %s\n", 48 __func__, __LINE__, *nr_spes, 49 arm_spe_pmus[*nr_spes]->type, 50 arm_spe_pmus[*nr_spes]->name); 51 (*nr_spes)++; 52 } 53 } 54 55 return arm_spe_pmus; 56 } 57 58 static struct perf_pmu **find_all_hisi_ptt_pmus(int *nr_ptts, int *err) 59 { 60 struct perf_pmu **hisi_ptt_pmus = NULL; 61 struct dirent *dent; 62 char path[PATH_MAX]; 63 DIR *dir = NULL; 64 int idx = 0; 65 66 perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); 67 dir = opendir(path); 68 if (!dir) { 69 pr_err("can't read directory '%s'\n", path); 70 *err = -EINVAL; 71 return NULL; 72 } 73 74 while ((dent = readdir(dir))) { 75 if (strstr(dent->d_name, HISI_PTT_PMU_NAME)) 76 (*nr_ptts)++; 77 } 78 79 if (!(*nr_ptts)) 80 goto out; 81 82 hisi_ptt_pmus = zalloc(sizeof(struct perf_pmu *) * (*nr_ptts)); 83 if (!hisi_ptt_pmus) { 84 pr_err("hisi_ptt alloc failed\n"); 85 *err = -ENOMEM; 86 goto out; 87 } 88 89 rewinddir(dir); 90 while ((dent = readdir(dir))) { 91 if (strstr(dent->d_name, HISI_PTT_PMU_NAME) && idx < *nr_ptts) { 92 hisi_ptt_pmus[idx] = perf_pmus__find(dent->d_name); 93 if (hisi_ptt_pmus[idx]) 94 idx++; 95 } 96 } 97 98 out: 99 closedir(dir); 100 return hisi_ptt_pmus; 101 } 102 103 static struct perf_pmu *find_pmu_for_event(struct perf_pmu **pmus, 104 int pmu_nr, struct evsel *evsel) 105 { 106 int i; 107 108 if (!pmus) 109 return NULL; 110 111 for (i = 0; i < pmu_nr; i++) { 112 if (evsel->core.attr.type == pmus[i]->type) 113 return pmus[i]; 114 } 115 116 return NULL; 117 } 118 119 struct auxtrace_record 120 *auxtrace_record__init(struct evlist *evlist, int *err) 121 { 122 struct perf_pmu *cs_etm_pmu = NULL; 123 struct perf_pmu **arm_spe_pmus = NULL; 124 struct perf_pmu **hisi_ptt_pmus = NULL; 125 struct evsel *evsel; 126 struct perf_pmu *found_etm = NULL; 127 struct perf_pmu *found_spe = NULL; 128 struct perf_pmu *found_ptt = NULL; 129 int auxtrace_event_cnt = 0; 130 int nr_spes = 0; 131 int nr_ptts = 0; 132 133 if (!evlist) 134 return NULL; 135 136 cs_etm_pmu = perf_pmus__find(CORESIGHT_ETM_PMU_NAME); 137 arm_spe_pmus = find_all_arm_spe_pmus(&nr_spes, err); 138 hisi_ptt_pmus = find_all_hisi_ptt_pmus(&nr_ptts, err); 139 140 evlist__for_each_entry(evlist, evsel) { 141 if (cs_etm_pmu && !found_etm) 142 found_etm = find_pmu_for_event(&cs_etm_pmu, 1, evsel); 143 144 if (arm_spe_pmus && !found_spe) 145 found_spe = find_pmu_for_event(arm_spe_pmus, nr_spes, evsel); 146 147 if (hisi_ptt_pmus && !found_ptt) 148 found_ptt = find_pmu_for_event(hisi_ptt_pmus, nr_ptts, evsel); 149 } 150 151 free(arm_spe_pmus); 152 free(hisi_ptt_pmus); 153 154 if (found_etm) 155 auxtrace_event_cnt++; 156 157 if (found_spe) 158 auxtrace_event_cnt++; 159 160 if (found_ptt) 161 auxtrace_event_cnt++; 162 163 if (auxtrace_event_cnt > 1) { 164 pr_err("Concurrent AUX trace operation not currently supported\n"); 165 *err = -EOPNOTSUPP; 166 return NULL; 167 } 168 169 if (found_etm) 170 return cs_etm_record_init(err); 171 172 #if defined(__aarch64__) 173 if (found_spe) 174 return arm_spe_recording_init(err, found_spe); 175 176 if (found_ptt) 177 return hisi_ptt_recording_init(err, found_ptt); 178 #endif 179 180 /* 181 * Clear 'err' even if we haven't found an event - that way perf 182 * record can still be used even if tracers aren't present. The NULL 183 * return value will take care of telling the infrastructure HW tracing 184 * isn't available. 185 */ 186 *err = 0; 187 return NULL; 188 } 189 190 #if defined(__arm__) 191 u64 compat_auxtrace_mmap__read_head(struct auxtrace_mmap *mm) 192 { 193 struct perf_event_mmap_page *pc = mm->userpg; 194 u64 result; 195 196 __asm__ __volatile__( 197 " ldrd %0, %H0, [%1]" 198 : "=&r" (result) 199 : "r" (&pc->aux_head), "Qo" (pc->aux_head) 200 ); 201 202 return result; 203 } 204 205 int compat_auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail) 206 { 207 struct perf_event_mmap_page *pc = mm->userpg; 208 209 /* Ensure all reads are done before we write the tail out */ 210 smp_mb(); 211 212 __asm__ __volatile__( 213 " strd %2, %H2, [%1]" 214 : "=Qo" (pc->aux_tail) 215 : "r" (&pc->aux_tail), "r" (tail) 216 ); 217 218 return 0; 219 } 220 #endif 221