Mathieu Poirier | a818c56 | 2016-09-16 09:50:00 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(C) 2015 Linaro Limited. All rights reserved. |
| 3 | * Author: Mathieu Poirier <mathieu.poirier@linaro.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <api/fs/fs.h> |
| 19 | #include <linux/bitops.h> |
| 20 | #include <linux/coresight-pmu.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/log2.h> |
| 23 | #include <linux/types.h> |
| 24 | |
| 25 | #include "cs-etm.h" |
| 26 | #include "../../perf.h" |
| 27 | #include "../../util/auxtrace.h" |
| 28 | #include "../../util/cpumap.h" |
| 29 | #include "../../util/evlist.h" |
Mathieu Poirier | 3becf45 | 2016-09-16 09:50:04 -0600 | [diff] [blame] | 30 | #include "../../util/evsel.h" |
Mathieu Poirier | a818c56 | 2016-09-16 09:50:00 -0600 | [diff] [blame] | 31 | #include "../../util/pmu.h" |
| 32 | #include "../../util/thread_map.h" |
| 33 | #include "../../util/cs-etm.h" |
| 34 | |
| 35 | #include <stdlib.h> |
| 36 | |
Mathieu Poirier | 3becf45 | 2016-09-16 09:50:04 -0600 | [diff] [blame] | 37 | #define ENABLE_SINK_MAX 128 |
| 38 | #define CS_BUS_DEVICE_PATH "/bus/coresight/devices/" |
| 39 | |
Mathieu Poirier | a818c56 | 2016-09-16 09:50:00 -0600 | [diff] [blame] | 40 | struct cs_etm_recording { |
| 41 | struct auxtrace_record itr; |
| 42 | struct perf_pmu *cs_etm_pmu; |
| 43 | struct perf_evlist *evlist; |
Mathieu Poirier | 239b64d | 2019-06-05 10:16:33 -0600 | [diff] [blame] | 44 | int wrapped_cnt; |
| 45 | bool *wrapped; |
Mathieu Poirier | a818c56 | 2016-09-16 09:50:00 -0600 | [diff] [blame] | 46 | bool snapshot_mode; |
| 47 | size_t snapshot_size; |
| 48 | }; |
| 49 | |
| 50 | static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu); |
| 51 | |
| 52 | static int cs_etm_parse_snapshot_options(struct auxtrace_record *itr, |
| 53 | struct record_opts *opts, |
| 54 | const char *str) |
| 55 | { |
| 56 | struct cs_etm_recording *ptr = |
| 57 | container_of(itr, struct cs_etm_recording, itr); |
| 58 | unsigned long long snapshot_size = 0; |
| 59 | char *endptr; |
| 60 | |
| 61 | if (str) { |
| 62 | snapshot_size = strtoull(str, &endptr, 0); |
| 63 | if (*endptr || snapshot_size > SIZE_MAX) |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | opts->auxtrace_snapshot_mode = true; |
| 68 | opts->auxtrace_snapshot_size = snapshot_size; |
| 69 | ptr->snapshot_size = snapshot_size; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int cs_etm_recording_options(struct auxtrace_record *itr, |
| 75 | struct perf_evlist *evlist, |
| 76 | struct record_opts *opts) |
| 77 | { |
| 78 | struct cs_etm_recording *ptr = |
| 79 | container_of(itr, struct cs_etm_recording, itr); |
| 80 | struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; |
| 81 | struct perf_evsel *evsel, *cs_etm_evsel = NULL; |
| 82 | const struct cpu_map *cpus = evlist->cpus; |
| 83 | bool privileged = (geteuid() == 0 || perf_event_paranoid() < 0); |
| 84 | |
| 85 | ptr->evlist = evlist; |
| 86 | ptr->snapshot_mode = opts->auxtrace_snapshot_mode; |
| 87 | |
| 88 | evlist__for_each_entry(evlist, evsel) { |
| 89 | if (evsel->attr.type == cs_etm_pmu->type) { |
| 90 | if (cs_etm_evsel) { |
| 91 | pr_err("There may be only one %s event\n", |
| 92 | CORESIGHT_ETM_PMU_NAME); |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | evsel->attr.freq = 0; |
| 96 | evsel->attr.sample_period = 1; |
| 97 | cs_etm_evsel = evsel; |
| 98 | opts->full_auxtrace = true; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* no need to continue if at least one event of interest was found */ |
| 103 | if (!cs_etm_evsel) |
| 104 | return 0; |
| 105 | |
| 106 | if (opts->use_clockid) { |
| 107 | pr_err("Cannot use clockid (-k option) with %s\n", |
| 108 | CORESIGHT_ETM_PMU_NAME); |
| 109 | return -EINVAL; |
| 110 | } |
| 111 | |
| 112 | /* we are in snapshot mode */ |
| 113 | if (opts->auxtrace_snapshot_mode) { |
| 114 | /* |
| 115 | * No size were given to '-S' or '-m,', so go with |
| 116 | * the default |
| 117 | */ |
| 118 | if (!opts->auxtrace_snapshot_size && |
| 119 | !opts->auxtrace_mmap_pages) { |
| 120 | if (privileged) { |
| 121 | opts->auxtrace_mmap_pages = MiB(4) / page_size; |
| 122 | } else { |
| 123 | opts->auxtrace_mmap_pages = |
| 124 | KiB(128) / page_size; |
| 125 | if (opts->mmap_pages == UINT_MAX) |
| 126 | opts->mmap_pages = KiB(256) / page_size; |
| 127 | } |
| 128 | } else if (!opts->auxtrace_mmap_pages && !privileged && |
| 129 | opts->mmap_pages == UINT_MAX) { |
| 130 | opts->mmap_pages = KiB(256) / page_size; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * '-m,xyz' was specified but no snapshot size, so make the |
| 135 | * snapshot size as big as the auxtrace mmap area. |
| 136 | */ |
| 137 | if (!opts->auxtrace_snapshot_size) { |
| 138 | opts->auxtrace_snapshot_size = |
| 139 | opts->auxtrace_mmap_pages * (size_t)page_size; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * -Sxyz was specified but no auxtrace mmap area, so make the |
| 144 | * auxtrace mmap area big enough to fit the requested snapshot |
| 145 | * size. |
| 146 | */ |
| 147 | if (!opts->auxtrace_mmap_pages) { |
| 148 | size_t sz = opts->auxtrace_snapshot_size; |
| 149 | |
| 150 | sz = round_up(sz, page_size) / page_size; |
| 151 | opts->auxtrace_mmap_pages = roundup_pow_of_two(sz); |
| 152 | } |
| 153 | |
| 154 | /* Snapshost size can't be bigger than the auxtrace area */ |
| 155 | if (opts->auxtrace_snapshot_size > |
| 156 | opts->auxtrace_mmap_pages * (size_t)page_size) { |
| 157 | pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n", |
| 158 | opts->auxtrace_snapshot_size, |
| 159 | opts->auxtrace_mmap_pages * (size_t)page_size); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | |
| 163 | /* Something went wrong somewhere - this shouldn't happen */ |
| 164 | if (!opts->auxtrace_snapshot_size || |
| 165 | !opts->auxtrace_mmap_pages) { |
| 166 | pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n"); |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* We are in full trace mode but '-m,xyz' wasn't specified */ |
| 172 | if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) { |
| 173 | if (privileged) { |
| 174 | opts->auxtrace_mmap_pages = MiB(4) / page_size; |
| 175 | } else { |
| 176 | opts->auxtrace_mmap_pages = KiB(128) / page_size; |
| 177 | if (opts->mmap_pages == UINT_MAX) |
| 178 | opts->mmap_pages = KiB(256) / page_size; |
| 179 | } |
| 180 | |
| 181 | } |
| 182 | |
| 183 | /* Validate auxtrace_mmap_pages provided by user */ |
| 184 | if (opts->auxtrace_mmap_pages) { |
| 185 | unsigned int max_page = (KiB(128) / page_size); |
| 186 | size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size; |
| 187 | |
| 188 | if (!privileged && |
| 189 | opts->auxtrace_mmap_pages > max_page) { |
| 190 | opts->auxtrace_mmap_pages = max_page; |
| 191 | pr_err("auxtrace too big, truncating to %d\n", |
| 192 | max_page); |
| 193 | } |
| 194 | |
| 195 | if (!is_power_of_2(sz)) { |
| 196 | pr_err("Invalid mmap size for %s: must be a power of 2\n", |
| 197 | CORESIGHT_ETM_PMU_NAME); |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (opts->auxtrace_snapshot_mode) |
| 203 | pr_debug2("%s snapshot size: %zu\n", CORESIGHT_ETM_PMU_NAME, |
| 204 | opts->auxtrace_snapshot_size); |
| 205 | |
| 206 | if (cs_etm_evsel) { |
| 207 | /* |
| 208 | * To obtain the auxtrace buffer file descriptor, the auxtrace |
| 209 | * event must come first. |
| 210 | */ |
| 211 | perf_evlist__to_front(evlist, cs_etm_evsel); |
| 212 | /* |
| 213 | * In the case of per-cpu mmaps, we need the CPU on the |
| 214 | * AUX event. |
| 215 | */ |
| 216 | if (!cpu_map__empty(cpus)) |
| 217 | perf_evsel__set_sample_bit(cs_etm_evsel, CPU); |
| 218 | } |
| 219 | |
| 220 | /* Add dummy event to keep tracking */ |
| 221 | if (opts->full_auxtrace) { |
| 222 | struct perf_evsel *tracking_evsel; |
| 223 | int err; |
| 224 | |
| 225 | err = parse_events(evlist, "dummy:u", NULL); |
| 226 | if (err) |
| 227 | return err; |
| 228 | |
| 229 | tracking_evsel = perf_evlist__last(evlist); |
| 230 | perf_evlist__set_tracking_event(evlist, tracking_evsel); |
| 231 | |
| 232 | tracking_evsel->attr.freq = 0; |
| 233 | tracking_evsel->attr.sample_period = 1; |
| 234 | |
| 235 | /* In per-cpu case, always need the time of mmap events etc */ |
| 236 | if (!cpu_map__empty(cpus)) |
| 237 | perf_evsel__set_sample_bit(tracking_evsel, TIME); |
| 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static u64 cs_etm_get_config(struct auxtrace_record *itr) |
| 244 | { |
| 245 | u64 config = 0; |
| 246 | struct cs_etm_recording *ptr = |
| 247 | container_of(itr, struct cs_etm_recording, itr); |
| 248 | struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; |
| 249 | struct perf_evlist *evlist = ptr->evlist; |
| 250 | struct perf_evsel *evsel; |
| 251 | |
| 252 | evlist__for_each_entry(evlist, evsel) { |
| 253 | if (evsel->attr.type == cs_etm_pmu->type) { |
| 254 | /* |
| 255 | * Variable perf_event_attr::config is assigned to |
| 256 | * ETMv3/PTM. The bit fields have been made to match |
| 257 | * the ETMv3.5 ETRMCR register specification. See the |
| 258 | * PMU_FORMAT_ATTR() declarations in |
| 259 | * drivers/hwtracing/coresight/coresight-perf.c for |
| 260 | * details. |
| 261 | */ |
| 262 | config = evsel->attr.config; |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return config; |
| 268 | } |
| 269 | |
| 270 | static size_t |
| 271 | cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused, |
| 272 | struct perf_evlist *evlist __maybe_unused) |
| 273 | { |
| 274 | int i; |
| 275 | int etmv3 = 0, etmv4 = 0; |
| 276 | const struct cpu_map *cpus = evlist->cpus; |
| 277 | |
| 278 | /* cpu map is not empty, we have specific CPUs to work with */ |
| 279 | if (!cpu_map__empty(cpus)) { |
| 280 | for (i = 0; i < cpu_map__nr(cpus); i++) { |
| 281 | if (cs_etm_is_etmv4(itr, cpus->map[i])) |
| 282 | etmv4++; |
| 283 | else |
| 284 | etmv3++; |
| 285 | } |
| 286 | } else { |
| 287 | /* get configuration for all CPUs in the system */ |
| 288 | for (i = 0; i < cpu__max_cpu(); i++) { |
| 289 | if (cs_etm_is_etmv4(itr, i)) |
| 290 | etmv4++; |
| 291 | else |
| 292 | etmv3++; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return (CS_ETM_HEADER_SIZE + |
| 297 | (etmv4 * CS_ETMV4_PRIV_SIZE) + |
| 298 | (etmv3 * CS_ETMV3_PRIV_SIZE)); |
| 299 | } |
| 300 | |
| 301 | static const char *metadata_etmv3_ro[CS_ETM_PRIV_MAX] = { |
| 302 | [CS_ETM_ETMCCER] = "mgmt/etmccer", |
| 303 | [CS_ETM_ETMIDR] = "mgmt/etmidr", |
| 304 | }; |
| 305 | |
| 306 | static const char *metadata_etmv4_ro[CS_ETMV4_PRIV_MAX] = { |
| 307 | [CS_ETMV4_TRCIDR0] = "trcidr/trcidr0", |
| 308 | [CS_ETMV4_TRCIDR1] = "trcidr/trcidr1", |
| 309 | [CS_ETMV4_TRCIDR2] = "trcidr/trcidr2", |
| 310 | [CS_ETMV4_TRCIDR8] = "trcidr/trcidr8", |
| 311 | [CS_ETMV4_TRCAUTHSTATUS] = "mgmt/trcauthstatus", |
| 312 | }; |
| 313 | |
| 314 | static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu) |
| 315 | { |
| 316 | bool ret = false; |
| 317 | char path[PATH_MAX]; |
| 318 | int scan; |
| 319 | unsigned int val; |
| 320 | struct cs_etm_recording *ptr = |
| 321 | container_of(itr, struct cs_etm_recording, itr); |
| 322 | struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; |
| 323 | |
| 324 | /* Take any of the RO files for ETMv4 and see if it present */ |
| 325 | snprintf(path, PATH_MAX, "cpu%d/%s", |
| 326 | cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]); |
| 327 | scan = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val); |
| 328 | |
| 329 | /* The file was read successfully, we have a winner */ |
| 330 | if (scan == 1) |
| 331 | ret = true; |
| 332 | |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static int cs_etm_get_ro(struct perf_pmu *pmu, int cpu, const char *path) |
| 337 | { |
| 338 | char pmu_path[PATH_MAX]; |
| 339 | int scan; |
| 340 | unsigned int val = 0; |
| 341 | |
| 342 | /* Get RO metadata from sysfs */ |
| 343 | snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path); |
| 344 | |
| 345 | scan = perf_pmu__scan_file(pmu, pmu_path, "%x", &val); |
| 346 | if (scan != 1) |
| 347 | pr_err("%s: error reading: %s\n", __func__, pmu_path); |
| 348 | |
| 349 | return val; |
| 350 | } |
| 351 | |
| 352 | static void cs_etm_get_metadata(int cpu, u32 *offset, |
| 353 | struct auxtrace_record *itr, |
| 354 | struct auxtrace_info_event *info) |
| 355 | { |
| 356 | u32 increment; |
| 357 | u64 magic; |
| 358 | struct cs_etm_recording *ptr = |
| 359 | container_of(itr, struct cs_etm_recording, itr); |
| 360 | struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; |
| 361 | |
| 362 | /* first see what kind of tracer this cpu is affined to */ |
| 363 | if (cs_etm_is_etmv4(itr, cpu)) { |
| 364 | magic = __perf_cs_etmv4_magic; |
| 365 | /* Get trace configuration register */ |
| 366 | info->priv[*offset + CS_ETMV4_TRCCONFIGR] = |
| 367 | cs_etm_get_config(itr); |
| 368 | /* Get traceID from the framework */ |
| 369 | info->priv[*offset + CS_ETMV4_TRCTRACEIDR] = |
| 370 | coresight_get_trace_id(cpu); |
| 371 | /* Get read-only information from sysFS */ |
| 372 | info->priv[*offset + CS_ETMV4_TRCIDR0] = |
| 373 | cs_etm_get_ro(cs_etm_pmu, cpu, |
| 374 | metadata_etmv4_ro[CS_ETMV4_TRCIDR0]); |
| 375 | info->priv[*offset + CS_ETMV4_TRCIDR1] = |
| 376 | cs_etm_get_ro(cs_etm_pmu, cpu, |
| 377 | metadata_etmv4_ro[CS_ETMV4_TRCIDR1]); |
| 378 | info->priv[*offset + CS_ETMV4_TRCIDR2] = |
| 379 | cs_etm_get_ro(cs_etm_pmu, cpu, |
| 380 | metadata_etmv4_ro[CS_ETMV4_TRCIDR2]); |
| 381 | info->priv[*offset + CS_ETMV4_TRCIDR8] = |
| 382 | cs_etm_get_ro(cs_etm_pmu, cpu, |
| 383 | metadata_etmv4_ro[CS_ETMV4_TRCIDR8]); |
| 384 | info->priv[*offset + CS_ETMV4_TRCAUTHSTATUS] = |
| 385 | cs_etm_get_ro(cs_etm_pmu, cpu, |
| 386 | metadata_etmv4_ro |
| 387 | [CS_ETMV4_TRCAUTHSTATUS]); |
| 388 | |
| 389 | /* How much space was used */ |
| 390 | increment = CS_ETMV4_PRIV_MAX; |
| 391 | } else { |
| 392 | magic = __perf_cs_etmv3_magic; |
| 393 | /* Get configuration register */ |
| 394 | info->priv[*offset + CS_ETM_ETMCR] = cs_etm_get_config(itr); |
| 395 | /* Get traceID from the framework */ |
| 396 | info->priv[*offset + CS_ETM_ETMTRACEIDR] = |
| 397 | coresight_get_trace_id(cpu); |
| 398 | /* Get read-only information from sysFS */ |
| 399 | info->priv[*offset + CS_ETM_ETMCCER] = |
| 400 | cs_etm_get_ro(cs_etm_pmu, cpu, |
| 401 | metadata_etmv3_ro[CS_ETM_ETMCCER]); |
| 402 | info->priv[*offset + CS_ETM_ETMIDR] = |
| 403 | cs_etm_get_ro(cs_etm_pmu, cpu, |
| 404 | metadata_etmv3_ro[CS_ETM_ETMIDR]); |
| 405 | |
| 406 | /* How much space was used */ |
| 407 | increment = CS_ETM_PRIV_MAX; |
| 408 | } |
| 409 | |
| 410 | /* Build generic header portion */ |
| 411 | info->priv[*offset + CS_ETM_MAGIC] = magic; |
| 412 | info->priv[*offset + CS_ETM_CPU] = cpu; |
| 413 | /* Where the next CPU entry should start from */ |
| 414 | *offset += increment; |
| 415 | } |
| 416 | |
| 417 | static int cs_etm_info_fill(struct auxtrace_record *itr, |
| 418 | struct perf_session *session, |
| 419 | struct auxtrace_info_event *info, |
| 420 | size_t priv_size) |
| 421 | { |
| 422 | int i; |
| 423 | u32 offset; |
| 424 | u64 nr_cpu, type; |
| 425 | const struct cpu_map *cpus = session->evlist->cpus; |
| 426 | struct cs_etm_recording *ptr = |
| 427 | container_of(itr, struct cs_etm_recording, itr); |
| 428 | struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; |
| 429 | |
| 430 | if (priv_size != cs_etm_info_priv_size(itr, session->evlist)) |
| 431 | return -EINVAL; |
| 432 | |
| 433 | if (!session->evlist->nr_mmaps) |
| 434 | return -EINVAL; |
| 435 | |
| 436 | /* If the cpu_map is empty all CPUs are involved */ |
| 437 | nr_cpu = cpu_map__empty(cpus) ? cpu__max_cpu() : cpu_map__nr(cpus); |
| 438 | /* Get PMU type as dynamically assigned by the core */ |
| 439 | type = cs_etm_pmu->type; |
| 440 | |
| 441 | /* First fill out the session header */ |
| 442 | info->type = PERF_AUXTRACE_CS_ETM; |
| 443 | info->priv[CS_HEADER_VERSION_0] = 0; |
| 444 | info->priv[CS_PMU_TYPE_CPUS] = type << 32; |
| 445 | info->priv[CS_PMU_TYPE_CPUS] |= nr_cpu; |
| 446 | info->priv[CS_ETM_SNAPSHOT] = ptr->snapshot_mode; |
| 447 | |
| 448 | offset = CS_ETM_SNAPSHOT + 1; |
| 449 | |
| 450 | /* cpu map is not empty, we have specific CPUs to work with */ |
| 451 | if (!cpu_map__empty(cpus)) { |
| 452 | for (i = 0; i < cpu_map__nr(cpus) && offset < priv_size; i++) |
| 453 | cs_etm_get_metadata(cpus->map[i], &offset, itr, info); |
| 454 | } else { |
| 455 | /* get configuration for all CPUs in the system */ |
| 456 | for (i = 0; i < cpu__max_cpu(); i++) |
| 457 | cs_etm_get_metadata(i, &offset, itr, info); |
| 458 | } |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
Mathieu Poirier | 239b64d | 2019-06-05 10:16:33 -0600 | [diff] [blame] | 463 | static int cs_etm_alloc_wrapped_array(struct cs_etm_recording *ptr, int idx) |
| 464 | { |
| 465 | bool *wrapped; |
| 466 | int cnt = ptr->wrapped_cnt; |
| 467 | |
| 468 | /* Make @ptr->wrapped as big as @idx */ |
| 469 | while (cnt <= idx) |
| 470 | cnt++; |
| 471 | |
| 472 | /* |
| 473 | * Free'ed in cs_etm_recording_free(). Using realloc() to avoid |
| 474 | * cross compilation problems where the host's system supports |
| 475 | * reallocarray() but not the target. |
| 476 | */ |
| 477 | wrapped = realloc(ptr->wrapped, cnt * sizeof(bool)); |
| 478 | if (!wrapped) |
| 479 | return -ENOMEM; |
| 480 | |
| 481 | wrapped[cnt - 1] = false; |
| 482 | ptr->wrapped_cnt = cnt; |
| 483 | ptr->wrapped = wrapped; |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static bool cs_etm_buffer_has_wrapped(unsigned char *buffer, |
| 489 | size_t buffer_size, u64 head) |
| 490 | { |
| 491 | u64 i, watermark; |
| 492 | u64 *buf = (u64 *)buffer; |
| 493 | size_t buf_size = buffer_size; |
| 494 | |
| 495 | /* |
| 496 | * We want to look the very last 512 byte (chosen arbitrarily) in |
| 497 | * the ring buffer. |
| 498 | */ |
| 499 | watermark = buf_size - 512; |
| 500 | |
| 501 | /* |
| 502 | * @head is continuously increasing - if its value is equal or greater |
| 503 | * than the size of the ring buffer, it has wrapped around. |
| 504 | */ |
| 505 | if (head >= buffer_size) |
| 506 | return true; |
| 507 | |
| 508 | /* |
| 509 | * The value of @head is somewhere within the size of the ring buffer. |
| 510 | * This can be that there hasn't been enough data to fill the ring |
| 511 | * buffer yet or the trace time was so long that @head has numerically |
| 512 | * wrapped around. To find we need to check if we have data at the very |
| 513 | * end of the ring buffer. We can reliably do this because mmap'ed |
| 514 | * pages are zeroed out and there is a fresh mapping with every new |
| 515 | * session. |
| 516 | */ |
| 517 | |
| 518 | /* @head is less than 512 byte from the end of the ring buffer */ |
| 519 | if (head > watermark) |
| 520 | watermark = head; |
| 521 | |
| 522 | /* |
| 523 | * Speed things up by using 64 bit transactions (see "u64 *buf" above) |
| 524 | */ |
| 525 | watermark >>= 3; |
| 526 | buf_size >>= 3; |
| 527 | |
| 528 | /* |
| 529 | * If we find trace data at the end of the ring buffer, @head has |
| 530 | * been there and has numerically wrapped around at least once. |
| 531 | */ |
| 532 | for (i = watermark; i < buf_size; i++) |
| 533 | if (buf[i]) |
| 534 | return true; |
| 535 | |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | static int cs_etm_find_snapshot(struct auxtrace_record *itr, |
Mathieu Poirier | a818c56 | 2016-09-16 09:50:00 -0600 | [diff] [blame] | 540 | int idx, struct auxtrace_mmap *mm, |
Mathieu Poirier | 239b64d | 2019-06-05 10:16:33 -0600 | [diff] [blame] | 541 | unsigned char *data, |
Mathieu Poirier | a818c56 | 2016-09-16 09:50:00 -0600 | [diff] [blame] | 542 | u64 *head, u64 *old) |
| 543 | { |
Mathieu Poirier | 239b64d | 2019-06-05 10:16:33 -0600 | [diff] [blame] | 544 | int err; |
| 545 | bool wrapped; |
| 546 | struct cs_etm_recording *ptr = |
| 547 | container_of(itr, struct cs_etm_recording, itr); |
| 548 | |
| 549 | /* |
| 550 | * Allocate memory to keep track of wrapping if this is the first |
| 551 | * time we deal with this *mm. |
| 552 | */ |
| 553 | if (idx >= ptr->wrapped_cnt) { |
| 554 | err = cs_etm_alloc_wrapped_array(ptr, idx); |
| 555 | if (err) |
| 556 | return err; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Check to see if *head has wrapped around. If it hasn't only the |
| 561 | * amount of data between *head and *old is snapshot'ed to avoid |
| 562 | * bloating the perf.data file with zeros. But as soon as *head has |
| 563 | * wrapped around the entire size of the AUX ring buffer it taken. |
| 564 | */ |
| 565 | wrapped = ptr->wrapped[idx]; |
| 566 | if (!wrapped && cs_etm_buffer_has_wrapped(data, mm->len, *head)) { |
| 567 | wrapped = true; |
| 568 | ptr->wrapped[idx] = true; |
| 569 | } |
| 570 | |
Mathieu Poirier | a818c56 | 2016-09-16 09:50:00 -0600 | [diff] [blame] | 571 | pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n", |
| 572 | __func__, idx, (size_t)*old, (size_t)*head, mm->len); |
| 573 | |
Mathieu Poirier | 239b64d | 2019-06-05 10:16:33 -0600 | [diff] [blame] | 574 | /* No wrap has occurred, we can just use *head and *old. */ |
| 575 | if (!wrapped) |
| 576 | return 0; |
| 577 | |
| 578 | /* |
| 579 | * *head has wrapped around - adjust *head and *old to pickup the |
| 580 | * entire content of the AUX buffer. |
| 581 | */ |
| 582 | if (*head >= mm->len) { |
| 583 | *old = *head - mm->len; |
| 584 | } else { |
| 585 | *head += mm->len; |
| 586 | *old = *head - mm->len; |
| 587 | } |
Mathieu Poirier | a818c56 | 2016-09-16 09:50:00 -0600 | [diff] [blame] | 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static int cs_etm_snapshot_start(struct auxtrace_record *itr) |
| 593 | { |
| 594 | struct cs_etm_recording *ptr = |
| 595 | container_of(itr, struct cs_etm_recording, itr); |
| 596 | struct perf_evsel *evsel; |
| 597 | |
| 598 | evlist__for_each_entry(ptr->evlist, evsel) { |
| 599 | if (evsel->attr.type == ptr->cs_etm_pmu->type) |
| 600 | return perf_evsel__disable(evsel); |
| 601 | } |
| 602 | return -EINVAL; |
| 603 | } |
| 604 | |
| 605 | static int cs_etm_snapshot_finish(struct auxtrace_record *itr) |
| 606 | { |
| 607 | struct cs_etm_recording *ptr = |
| 608 | container_of(itr, struct cs_etm_recording, itr); |
| 609 | struct perf_evsel *evsel; |
| 610 | |
| 611 | evlist__for_each_entry(ptr->evlist, evsel) { |
| 612 | if (evsel->attr.type == ptr->cs_etm_pmu->type) |
| 613 | return perf_evsel__enable(evsel); |
| 614 | } |
| 615 | return -EINVAL; |
| 616 | } |
| 617 | |
| 618 | static u64 cs_etm_reference(struct auxtrace_record *itr __maybe_unused) |
| 619 | { |
| 620 | return (((u64) rand() << 0) & 0x00000000FFFFFFFFull) | |
| 621 | (((u64) rand() << 32) & 0xFFFFFFFF00000000ull); |
| 622 | } |
| 623 | |
| 624 | static void cs_etm_recording_free(struct auxtrace_record *itr) |
| 625 | { |
| 626 | struct cs_etm_recording *ptr = |
| 627 | container_of(itr, struct cs_etm_recording, itr); |
Mathieu Poirier | 239b64d | 2019-06-05 10:16:33 -0600 | [diff] [blame] | 628 | |
| 629 | zfree(&ptr->wrapped); |
Mathieu Poirier | a818c56 | 2016-09-16 09:50:00 -0600 | [diff] [blame] | 630 | free(ptr); |
| 631 | } |
| 632 | |
| 633 | static int cs_etm_read_finish(struct auxtrace_record *itr, int idx) |
| 634 | { |
| 635 | struct cs_etm_recording *ptr = |
| 636 | container_of(itr, struct cs_etm_recording, itr); |
| 637 | struct perf_evsel *evsel; |
| 638 | |
| 639 | evlist__for_each_entry(ptr->evlist, evsel) { |
| 640 | if (evsel->attr.type == ptr->cs_etm_pmu->type) |
| 641 | return perf_evlist__enable_event_idx(ptr->evlist, |
| 642 | evsel, idx); |
| 643 | } |
| 644 | |
| 645 | return -EINVAL; |
| 646 | } |
| 647 | |
| 648 | struct auxtrace_record *cs_etm_record_init(int *err) |
| 649 | { |
| 650 | struct perf_pmu *cs_etm_pmu; |
| 651 | struct cs_etm_recording *ptr; |
| 652 | |
| 653 | cs_etm_pmu = perf_pmu__find(CORESIGHT_ETM_PMU_NAME); |
| 654 | |
| 655 | if (!cs_etm_pmu) { |
| 656 | *err = -EINVAL; |
| 657 | goto out; |
| 658 | } |
| 659 | |
| 660 | ptr = zalloc(sizeof(struct cs_etm_recording)); |
| 661 | if (!ptr) { |
| 662 | *err = -ENOMEM; |
| 663 | goto out; |
| 664 | } |
| 665 | |
| 666 | ptr->cs_etm_pmu = cs_etm_pmu; |
| 667 | ptr->itr.parse_snapshot_options = cs_etm_parse_snapshot_options; |
| 668 | ptr->itr.recording_options = cs_etm_recording_options; |
| 669 | ptr->itr.info_priv_size = cs_etm_info_priv_size; |
| 670 | ptr->itr.info_fill = cs_etm_info_fill; |
| 671 | ptr->itr.find_snapshot = cs_etm_find_snapshot; |
| 672 | ptr->itr.snapshot_start = cs_etm_snapshot_start; |
| 673 | ptr->itr.snapshot_finish = cs_etm_snapshot_finish; |
| 674 | ptr->itr.reference = cs_etm_reference; |
| 675 | ptr->itr.free = cs_etm_recording_free; |
| 676 | ptr->itr.read_finish = cs_etm_read_finish; |
| 677 | |
| 678 | *err = 0; |
| 679 | return &ptr->itr; |
| 680 | out: |
| 681 | return NULL; |
| 682 | } |
Mathieu Poirier | 3becf45 | 2016-09-16 09:50:04 -0600 | [diff] [blame] | 683 | |
| 684 | static FILE *cs_device__open_file(const char *name) |
| 685 | { |
| 686 | struct stat st; |
| 687 | char path[PATH_MAX]; |
| 688 | const char *sysfs; |
| 689 | |
| 690 | sysfs = sysfs__mountpoint(); |
| 691 | if (!sysfs) |
| 692 | return NULL; |
| 693 | |
| 694 | snprintf(path, PATH_MAX, |
| 695 | "%s" CS_BUS_DEVICE_PATH "%s", sysfs, name); |
| 696 | |
| 697 | printf("path: %s\n", path); |
| 698 | |
| 699 | if (stat(path, &st) < 0) |
| 700 | return NULL; |
| 701 | |
| 702 | return fopen(path, "w"); |
| 703 | |
| 704 | } |
| 705 | |
| 706 | static __attribute__((format(printf, 2, 3))) |
| 707 | int cs_device__print_file(const char *name, const char *fmt, ...) |
| 708 | { |
| 709 | va_list args; |
| 710 | FILE *file; |
| 711 | int ret = -EINVAL; |
| 712 | |
| 713 | va_start(args, fmt); |
| 714 | file = cs_device__open_file(name); |
| 715 | if (file) { |
| 716 | ret = vfprintf(file, fmt, args); |
| 717 | fclose(file); |
| 718 | } |
| 719 | va_end(args); |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | int cs_etm_set_drv_config(struct perf_evsel_config_term *term) |
| 724 | { |
| 725 | int ret; |
| 726 | char enable_sink[ENABLE_SINK_MAX]; |
| 727 | |
| 728 | snprintf(enable_sink, ENABLE_SINK_MAX, "%s/%s", |
| 729 | term->val.drv_cfg, "enable_sink"); |
| 730 | |
| 731 | ret = cs_device__print_file(enable_sink, "%d", 1); |
| 732 | if (ret < 0) |
| 733 | return ret; |
| 734 | |
| 735 | return 0; |
| 736 | } |