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 <api/fs/fs.h> 8 #include <linux/bits.h> 9 #include <linux/bitops.h> 10 #include <linux/compiler.h> 11 #include <linux/coresight-pmu.h> 12 #include <linux/kernel.h> 13 #include <linux/log2.h> 14 #include <linux/string.h> 15 #include <linux/types.h> 16 #include <linux/zalloc.h> 17 18 #include "cs-etm.h" 19 #include "../../../util/debug.h" 20 #include "../../../util/record.h" 21 #include "../../../util/auxtrace.h" 22 #include "../../../util/cpumap.h" 23 #include "../../../util/event.h" 24 #include "../../../util/evlist.h" 25 #include "../../../util/evsel.h" 26 #include "../../../util/perf_api_probe.h" 27 #include "../../../util/evsel_config.h" 28 #include "../../../util/pmus.h" 29 #include "../../../util/cs-etm.h" 30 #include <internal/lib.h> // page_size 31 #include "../../../util/session.h" 32 33 #include <errno.h> 34 #include <stdlib.h> 35 #include <sys/stat.h> 36 37 struct cs_etm_recording { 38 struct auxtrace_record itr; 39 struct perf_pmu *cs_etm_pmu; 40 struct evlist *evlist; 41 bool snapshot_mode; 42 size_t snapshot_size; 43 }; 44 45 static const char *metadata_etmv3_ro[CS_ETM_PRIV_MAX] = { 46 [CS_ETM_ETMCCER] = "mgmt/etmccer", 47 [CS_ETM_ETMIDR] = "mgmt/etmidr", 48 }; 49 50 static const char * const metadata_etmv4_ro[] = { 51 [CS_ETMV4_TRCIDR0] = "trcidr/trcidr0", 52 [CS_ETMV4_TRCIDR1] = "trcidr/trcidr1", 53 [CS_ETMV4_TRCIDR2] = "trcidr/trcidr2", 54 [CS_ETMV4_TRCIDR8] = "trcidr/trcidr8", 55 [CS_ETMV4_TRCAUTHSTATUS] = "mgmt/trcauthstatus", 56 [CS_ETMV4_TS_SOURCE] = "ts_source", 57 }; 58 59 static const char * const metadata_ete_ro[] = { 60 [CS_ETE_TRCIDR0] = "trcidr/trcidr0", 61 [CS_ETE_TRCIDR1] = "trcidr/trcidr1", 62 [CS_ETE_TRCIDR2] = "trcidr/trcidr2", 63 [CS_ETE_TRCIDR8] = "trcidr/trcidr8", 64 [CS_ETE_TRCAUTHSTATUS] = "mgmt/trcauthstatus", 65 [CS_ETE_TRCDEVARCH] = "mgmt/trcdevarch", 66 [CS_ETE_TS_SOURCE] = "ts_source", 67 }; 68 69 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu); 70 static bool cs_etm_is_ete(struct auxtrace_record *itr, int cpu); 71 72 static int cs_etm_validate_context_id(struct auxtrace_record *itr, 73 struct evsel *evsel, int cpu) 74 { 75 struct cs_etm_recording *ptr = 76 container_of(itr, struct cs_etm_recording, itr); 77 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 78 char path[PATH_MAX]; 79 int err; 80 u32 val; 81 u64 contextid = evsel->core.attr.config & 82 (perf_pmu__format_bits(cs_etm_pmu, "contextid") | 83 perf_pmu__format_bits(cs_etm_pmu, "contextid1") | 84 perf_pmu__format_bits(cs_etm_pmu, "contextid2")); 85 86 if (!contextid) 87 return 0; 88 89 /* Not supported in etmv3 */ 90 if (!cs_etm_is_etmv4(itr, cpu)) { 91 pr_err("%s: contextid not supported in ETMv3, disable with %s/contextid=0/\n", 92 CORESIGHT_ETM_PMU_NAME, CORESIGHT_ETM_PMU_NAME); 93 return -EINVAL; 94 } 95 96 /* Get a handle on TRCIDR2 */ 97 snprintf(path, PATH_MAX, "cpu%d/%s", 98 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR2]); 99 err = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val); 100 101 /* There was a problem reading the file, bailing out */ 102 if (err != 1) { 103 pr_err("%s: can't read file %s\n", CORESIGHT_ETM_PMU_NAME, 104 path); 105 return err; 106 } 107 108 if (contextid & 109 perf_pmu__format_bits(cs_etm_pmu, "contextid1")) { 110 /* 111 * TRCIDR2.CIDSIZE, bit [9-5], indicates whether contextID 112 * tracing is supported: 113 * 0b00000 Context ID tracing is not supported. 114 * 0b00100 Maximum of 32-bit Context ID size. 115 * All other values are reserved. 116 */ 117 if (BMVAL(val, 5, 9) != 0x4) { 118 pr_err("%s: CONTEXTIDR_EL1 isn't supported, disable with %s/contextid1=0/\n", 119 CORESIGHT_ETM_PMU_NAME, CORESIGHT_ETM_PMU_NAME); 120 return -EINVAL; 121 } 122 } 123 124 if (contextid & 125 perf_pmu__format_bits(cs_etm_pmu, "contextid2")) { 126 /* 127 * TRCIDR2.VMIDOPT[30:29] != 0 and 128 * TRCIDR2.VMIDSIZE[14:10] == 0b00100 (32bit virtual contextid) 129 * We can't support CONTEXTIDR in VMID if the size of the 130 * virtual context id is < 32bit. 131 * Any value of VMIDSIZE >= 4 (i.e, > 32bit) is fine for us. 132 */ 133 if (!BMVAL(val, 29, 30) || BMVAL(val, 10, 14) < 4) { 134 pr_err("%s: CONTEXTIDR_EL2 isn't supported, disable with %s/contextid2=0/\n", 135 CORESIGHT_ETM_PMU_NAME, CORESIGHT_ETM_PMU_NAME); 136 return -EINVAL; 137 } 138 } 139 140 return 0; 141 } 142 143 static int cs_etm_validate_timestamp(struct auxtrace_record *itr, 144 struct evsel *evsel, int cpu) 145 { 146 struct cs_etm_recording *ptr = 147 container_of(itr, struct cs_etm_recording, itr); 148 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 149 char path[PATH_MAX]; 150 int err; 151 u32 val; 152 153 if (!(evsel->core.attr.config & 154 perf_pmu__format_bits(cs_etm_pmu, "timestamp"))) 155 return 0; 156 157 if (!cs_etm_is_etmv4(itr, cpu)) { 158 pr_err("%s: timestamp not supported in ETMv3, disable with %s/timestamp=0/\n", 159 CORESIGHT_ETM_PMU_NAME, CORESIGHT_ETM_PMU_NAME); 160 return -EINVAL; 161 } 162 163 /* Get a handle on TRCIRD0 */ 164 snprintf(path, PATH_MAX, "cpu%d/%s", 165 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]); 166 err = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val); 167 168 /* There was a problem reading the file, bailing out */ 169 if (err != 1) { 170 pr_err("%s: can't read file %s\n", 171 CORESIGHT_ETM_PMU_NAME, path); 172 return err; 173 } 174 175 /* 176 * TRCIDR0.TSSIZE, bit [28-24], indicates whether global timestamping 177 * is supported: 178 * 0b00000 Global timestamping is not implemented 179 * 0b00110 Implementation supports a maximum timestamp of 48bits. 180 * 0b01000 Implementation supports a maximum timestamp of 64bits. 181 */ 182 val &= GENMASK(28, 24); 183 if (!val) { 184 return -EINVAL; 185 } 186 187 return 0; 188 } 189 190 /* 191 * Check whether the requested timestamp and contextid options should be 192 * available on all requested CPUs and if not, tell the user how to override. 193 * The kernel will silently disable any unavailable options so a warning here 194 * first is better. In theory the kernel could still disable the option for 195 * some other reason so this is best effort only. 196 */ 197 static int cs_etm_validate_config(struct auxtrace_record *itr, 198 struct evsel *evsel) 199 { 200 int idx, err = 0; 201 struct perf_cpu_map *event_cpus = evsel->evlist->core.user_requested_cpus; 202 struct perf_cpu_map *intersect_cpus; 203 struct perf_cpu cpu; 204 205 /* 206 * Set option of each CPU we have. In per-cpu case, do the validation 207 * for CPUs to work with. In per-thread case, the CPU map has the "any" 208 * CPU value. Since the traced program can run on any CPUs in this case, 209 * thus don't skip validation. 210 */ 211 if (!perf_cpu_map__has_any_cpu(event_cpus)) { 212 struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus(); 213 214 intersect_cpus = perf_cpu_map__intersect(event_cpus, online_cpus); 215 perf_cpu_map__put(online_cpus); 216 } else { 217 intersect_cpus = perf_cpu_map__new_online_cpus(); 218 } 219 220 perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) { 221 err = cs_etm_validate_context_id(itr, evsel, cpu.cpu); 222 if (err) 223 break; 224 225 err = cs_etm_validate_timestamp(itr, evsel, cpu.cpu); 226 if (err) 227 break; 228 } 229 230 perf_cpu_map__put(intersect_cpus); 231 return err; 232 } 233 234 static int cs_etm_parse_snapshot_options(struct auxtrace_record *itr, 235 struct record_opts *opts, 236 const char *str) 237 { 238 struct cs_etm_recording *ptr = 239 container_of(itr, struct cs_etm_recording, itr); 240 unsigned long long snapshot_size = 0; 241 char *endptr; 242 243 if (str) { 244 snapshot_size = strtoull(str, &endptr, 0); 245 if (*endptr || snapshot_size > SIZE_MAX) 246 return -1; 247 } 248 249 opts->auxtrace_snapshot_mode = true; 250 opts->auxtrace_snapshot_size = snapshot_size; 251 ptr->snapshot_size = snapshot_size; 252 253 return 0; 254 } 255 256 static int cs_etm_set_sink_attr(struct perf_pmu *pmu, 257 struct evsel *evsel) 258 { 259 char msg[BUFSIZ], path[PATH_MAX], *sink; 260 struct evsel_config_term *term; 261 int ret = -EINVAL; 262 u32 hash; 263 264 if (evsel->core.attr.config2 & GENMASK(31, 0)) 265 return 0; 266 267 list_for_each_entry(term, &evsel->config_terms, list) { 268 if (term->type != EVSEL__CONFIG_TERM_DRV_CFG) 269 continue; 270 271 sink = term->val.str; 272 snprintf(path, PATH_MAX, "sinks/%s", sink); 273 274 ret = perf_pmu__scan_file(pmu, path, "%x", &hash); 275 if (ret != 1) { 276 if (errno == ENOENT) 277 pr_err("Couldn't find sink \"%s\" on event %s\n" 278 "Missing kernel or device support?\n\n" 279 "Hint: An appropriate sink will be picked automatically if one isn't specified.\n", 280 sink, evsel__name(evsel)); 281 else 282 pr_err("Failed to set sink \"%s\" on event %s with %d (%s)\n", 283 sink, evsel__name(evsel), errno, 284 str_error_r(errno, msg, sizeof(msg))); 285 return ret; 286 } 287 288 evsel->core.attr.config2 |= hash; 289 return 0; 290 } 291 292 /* 293 * No sink was provided on the command line - allow the CoreSight 294 * system to look for a default 295 */ 296 return 0; 297 } 298 299 static int cs_etm_recording_options(struct auxtrace_record *itr, 300 struct evlist *evlist, 301 struct record_opts *opts) 302 { 303 int ret; 304 struct cs_etm_recording *ptr = 305 container_of(itr, struct cs_etm_recording, itr); 306 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 307 struct evsel *evsel, *cs_etm_evsel = NULL; 308 struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; 309 bool privileged = perf_event_paranoid_check(-1); 310 int err = 0; 311 312 evlist__for_each_entry(evlist, evsel) { 313 if (evsel->core.attr.type == cs_etm_pmu->type) { 314 if (cs_etm_evsel) { 315 pr_err("There may be only one %s event\n", 316 CORESIGHT_ETM_PMU_NAME); 317 return -EINVAL; 318 } 319 cs_etm_evsel = evsel; 320 } 321 } 322 323 /* no need to continue if at least one event of interest was found */ 324 if (!cs_etm_evsel) 325 return 0; 326 327 ptr->evlist = evlist; 328 ptr->snapshot_mode = opts->auxtrace_snapshot_mode; 329 330 if (!record_opts__no_switch_events(opts) && 331 perf_can_record_switch_events()) 332 opts->record_switch_events = true; 333 334 cs_etm_evsel->needs_auxtrace_mmap = true; 335 opts->full_auxtrace = true; 336 337 ret = cs_etm_set_sink_attr(cs_etm_pmu, cs_etm_evsel); 338 if (ret) 339 return ret; 340 341 if (opts->use_clockid) { 342 pr_err("Cannot use clockid (-k option) with %s\n", 343 CORESIGHT_ETM_PMU_NAME); 344 return -EINVAL; 345 } 346 347 /* we are in snapshot mode */ 348 if (opts->auxtrace_snapshot_mode) { 349 /* 350 * No size were given to '-S' or '-m,', so go with 351 * the default 352 */ 353 if (!opts->auxtrace_snapshot_size && 354 !opts->auxtrace_mmap_pages) { 355 if (privileged) { 356 opts->auxtrace_mmap_pages = MiB(4) / page_size; 357 } else { 358 opts->auxtrace_mmap_pages = 359 KiB(128) / page_size; 360 if (opts->mmap_pages == UINT_MAX) 361 opts->mmap_pages = KiB(256) / page_size; 362 } 363 } else if (!opts->auxtrace_mmap_pages && !privileged && 364 opts->mmap_pages == UINT_MAX) { 365 opts->mmap_pages = KiB(256) / page_size; 366 } 367 368 /* 369 * '-m,xyz' was specified but no snapshot size, so make the 370 * snapshot size as big as the auxtrace mmap area. 371 */ 372 if (!opts->auxtrace_snapshot_size) { 373 opts->auxtrace_snapshot_size = 374 opts->auxtrace_mmap_pages * (size_t)page_size; 375 } 376 377 /* 378 * -Sxyz was specified but no auxtrace mmap area, so make the 379 * auxtrace mmap area big enough to fit the requested snapshot 380 * size. 381 */ 382 if (!opts->auxtrace_mmap_pages) { 383 size_t sz = opts->auxtrace_snapshot_size; 384 385 sz = round_up(sz, page_size) / page_size; 386 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz); 387 } 388 389 /* Snapshot size can't be bigger than the auxtrace area */ 390 if (opts->auxtrace_snapshot_size > 391 opts->auxtrace_mmap_pages * (size_t)page_size) { 392 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n", 393 opts->auxtrace_snapshot_size, 394 opts->auxtrace_mmap_pages * (size_t)page_size); 395 return -EINVAL; 396 } 397 398 /* Something went wrong somewhere - this shouldn't happen */ 399 if (!opts->auxtrace_snapshot_size || 400 !opts->auxtrace_mmap_pages) { 401 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n"); 402 return -EINVAL; 403 } 404 } 405 406 /* Buffer sizes weren't specified with '-m,xyz' so give some defaults */ 407 if (!opts->auxtrace_mmap_pages) { 408 if (privileged) { 409 opts->auxtrace_mmap_pages = MiB(4) / page_size; 410 } else { 411 opts->auxtrace_mmap_pages = KiB(128) / page_size; 412 if (opts->mmap_pages == UINT_MAX) 413 opts->mmap_pages = KiB(256) / page_size; 414 } 415 } 416 417 if (opts->auxtrace_snapshot_mode) 418 pr_debug2("%s snapshot size: %zu\n", CORESIGHT_ETM_PMU_NAME, 419 opts->auxtrace_snapshot_size); 420 421 /* 422 * To obtain the auxtrace buffer file descriptor, the auxtrace 423 * event must come first. 424 */ 425 evlist__to_front(evlist, cs_etm_evsel); 426 427 /* 428 * get the CPU on the sample - need it to associate trace ID in the 429 * AUX_OUTPUT_HW_ID event, and the AUX event for per-cpu mmaps. 430 */ 431 evsel__set_sample_bit(cs_etm_evsel, CPU); 432 433 /* 434 * Also the case of per-cpu mmaps, need the contextID in order to be notified 435 * when a context switch happened. 436 */ 437 if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) { 438 evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel, 439 "timestamp", 1); 440 evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel, 441 "contextid", 1); 442 } 443 444 /* 445 * When the option '--timestamp' or '-T' is enabled, the PERF_SAMPLE_TIME 446 * bit is set for all events. In this case, always enable Arm CoreSight 447 * timestamp tracing. 448 */ 449 if (opts->sample_time_set) 450 evsel__set_config_if_unset(cs_etm_pmu, cs_etm_evsel, 451 "timestamp", 1); 452 453 /* Add dummy event to keep tracking */ 454 err = parse_event(evlist, "dummy:u"); 455 if (err) 456 goto out; 457 evsel = evlist__last(evlist); 458 evlist__set_tracking_event(evlist, evsel); 459 evsel->core.attr.freq = 0; 460 evsel->core.attr.sample_period = 1; 461 462 /* In per-cpu case, always need the time of mmap events etc */ 463 if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) 464 evsel__set_sample_bit(evsel, TIME); 465 466 err = cs_etm_validate_config(itr, cs_etm_evsel); 467 out: 468 return err; 469 } 470 471 static u64 cs_etm_get_config(struct auxtrace_record *itr) 472 { 473 u64 config = 0; 474 struct cs_etm_recording *ptr = 475 container_of(itr, struct cs_etm_recording, itr); 476 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 477 struct evlist *evlist = ptr->evlist; 478 struct evsel *evsel; 479 480 evlist__for_each_entry(evlist, evsel) { 481 if (evsel->core.attr.type == cs_etm_pmu->type) { 482 /* 483 * Variable perf_event_attr::config is assigned to 484 * ETMv3/PTM. The bit fields have been made to match 485 * the ETMv3.5 ETRMCR register specification. See the 486 * PMU_FORMAT_ATTR() declarations in 487 * drivers/hwtracing/coresight/coresight-perf.c for 488 * details. 489 */ 490 config = evsel->core.attr.config; 491 break; 492 } 493 } 494 495 return config; 496 } 497 498 #ifndef BIT 499 #define BIT(N) (1UL << (N)) 500 #endif 501 502 static u64 cs_etmv4_get_config(struct auxtrace_record *itr) 503 { 504 u64 config = 0; 505 u64 config_opts = 0; 506 507 /* 508 * The perf event variable config bits represent both 509 * the command line options and register programming 510 * bits in ETMv3/PTM. For ETMv4 we must remap options 511 * to real bits 512 */ 513 config_opts = cs_etm_get_config(itr); 514 if (config_opts & BIT(ETM_OPT_CYCACC)) 515 config |= BIT(ETM4_CFG_BIT_CYCACC); 516 if (config_opts & BIT(ETM_OPT_CTXTID)) 517 config |= BIT(ETM4_CFG_BIT_CTXTID); 518 if (config_opts & BIT(ETM_OPT_TS)) 519 config |= BIT(ETM4_CFG_BIT_TS); 520 if (config_opts & BIT(ETM_OPT_RETSTK)) 521 config |= BIT(ETM4_CFG_BIT_RETSTK); 522 if (config_opts & BIT(ETM_OPT_CTXTID2)) 523 config |= BIT(ETM4_CFG_BIT_VMID) | 524 BIT(ETM4_CFG_BIT_VMID_OPT); 525 if (config_opts & BIT(ETM_OPT_BRANCH_BROADCAST)) 526 config |= BIT(ETM4_CFG_BIT_BB); 527 528 return config; 529 } 530 531 static size_t 532 cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused, 533 struct evlist *evlist __maybe_unused) 534 { 535 int idx; 536 int etmv3 = 0, etmv4 = 0, ete = 0; 537 struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus; 538 struct perf_cpu_map *intersect_cpus; 539 struct perf_cpu cpu; 540 541 if (!perf_cpu_map__has_any_cpu(event_cpus)) { 542 /* cpu map is not "any" CPU , we have specific CPUs to work with */ 543 struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus(); 544 545 intersect_cpus = perf_cpu_map__intersect(event_cpus, online_cpus); 546 perf_cpu_map__put(online_cpus); 547 } else { 548 /* Event can be "any" CPU so count all online CPUs. */ 549 intersect_cpus = perf_cpu_map__new_online_cpus(); 550 } 551 perf_cpu_map__for_each_cpu_skip_any(cpu, idx, intersect_cpus) { 552 if (cs_etm_is_ete(itr, cpu.cpu)) 553 ete++; 554 else if (cs_etm_is_etmv4(itr, cpu.cpu)) 555 etmv4++; 556 else 557 etmv3++; 558 } 559 perf_cpu_map__put(intersect_cpus); 560 561 return (CS_ETM_HEADER_SIZE + 562 (ete * CS_ETE_PRIV_SIZE) + 563 (etmv4 * CS_ETMV4_PRIV_SIZE) + 564 (etmv3 * CS_ETMV3_PRIV_SIZE)); 565 } 566 567 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu) 568 { 569 bool ret = false; 570 char path[PATH_MAX]; 571 int scan; 572 unsigned int val; 573 struct cs_etm_recording *ptr = 574 container_of(itr, struct cs_etm_recording, itr); 575 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 576 577 /* Take any of the RO files for ETMv4 and see if it present */ 578 snprintf(path, PATH_MAX, "cpu%d/%s", 579 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]); 580 scan = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val); 581 582 /* The file was read successfully, we have a winner */ 583 if (scan == 1) 584 ret = true; 585 586 return ret; 587 } 588 589 static int cs_etm_get_ro(struct perf_pmu *pmu, int cpu, const char *path) 590 { 591 char pmu_path[PATH_MAX]; 592 int scan; 593 unsigned int val = 0; 594 595 /* Get RO metadata from sysfs */ 596 snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path); 597 598 scan = perf_pmu__scan_file(pmu, pmu_path, "%x", &val); 599 if (scan != 1) 600 pr_err("%s: error reading: %s\n", __func__, pmu_path); 601 602 return val; 603 } 604 605 static int cs_etm_get_ro_signed(struct perf_pmu *pmu, int cpu, const char *path) 606 { 607 char pmu_path[PATH_MAX]; 608 int scan; 609 int val = 0; 610 611 /* Get RO metadata from sysfs */ 612 snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path); 613 614 scan = perf_pmu__scan_file(pmu, pmu_path, "%d", &val); 615 if (scan != 1) 616 pr_err("%s: error reading: %s\n", __func__, pmu_path); 617 618 return val; 619 } 620 621 static bool cs_etm_pmu_path_exists(struct perf_pmu *pmu, int cpu, const char *path) 622 { 623 char pmu_path[PATH_MAX]; 624 625 /* Get RO metadata from sysfs */ 626 snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path); 627 628 return perf_pmu__file_exists(pmu, pmu_path); 629 } 630 631 #define TRCDEVARCH_ARCHPART_SHIFT 0 632 #define TRCDEVARCH_ARCHPART_MASK GENMASK(11, 0) 633 #define TRCDEVARCH_ARCHPART(x) (((x) & TRCDEVARCH_ARCHPART_MASK) >> TRCDEVARCH_ARCHPART_SHIFT) 634 635 #define TRCDEVARCH_ARCHVER_SHIFT 12 636 #define TRCDEVARCH_ARCHVER_MASK GENMASK(15, 12) 637 #define TRCDEVARCH_ARCHVER(x) (((x) & TRCDEVARCH_ARCHVER_MASK) >> TRCDEVARCH_ARCHVER_SHIFT) 638 639 static bool cs_etm_is_ete(struct auxtrace_record *itr, int cpu) 640 { 641 struct cs_etm_recording *ptr = container_of(itr, struct cs_etm_recording, itr); 642 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 643 int trcdevarch; 644 645 if (!cs_etm_pmu_path_exists(cs_etm_pmu, cpu, metadata_ete_ro[CS_ETE_TRCDEVARCH])) 646 return false; 647 648 trcdevarch = cs_etm_get_ro(cs_etm_pmu, cpu, metadata_ete_ro[CS_ETE_TRCDEVARCH]); 649 /* 650 * ETE if ARCHVER is 5 (ARCHVER is 4 for ETM) and ARCHPART is 0xA13. 651 * See ETM_DEVARCH_ETE_ARCH in coresight-etm4x.h 652 */ 653 return TRCDEVARCH_ARCHVER(trcdevarch) == 5 && TRCDEVARCH_ARCHPART(trcdevarch) == 0xA13; 654 } 655 656 static void cs_etm_save_etmv4_header(__u64 data[], struct auxtrace_record *itr, int cpu) 657 { 658 struct cs_etm_recording *ptr = container_of(itr, struct cs_etm_recording, itr); 659 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 660 661 /* Get trace configuration register */ 662 data[CS_ETMV4_TRCCONFIGR] = cs_etmv4_get_config(itr); 663 /* traceID set to legacy version, in case new perf running on older system */ 664 data[CS_ETMV4_TRCTRACEIDR] = 665 CORESIGHT_LEGACY_CPU_TRACE_ID(cpu) | CORESIGHT_TRACE_ID_UNUSED_FLAG; 666 667 /* Get read-only information from sysFS */ 668 data[CS_ETMV4_TRCIDR0] = cs_etm_get_ro(cs_etm_pmu, cpu, 669 metadata_etmv4_ro[CS_ETMV4_TRCIDR0]); 670 data[CS_ETMV4_TRCIDR1] = cs_etm_get_ro(cs_etm_pmu, cpu, 671 metadata_etmv4_ro[CS_ETMV4_TRCIDR1]); 672 data[CS_ETMV4_TRCIDR2] = cs_etm_get_ro(cs_etm_pmu, cpu, 673 metadata_etmv4_ro[CS_ETMV4_TRCIDR2]); 674 data[CS_ETMV4_TRCIDR8] = cs_etm_get_ro(cs_etm_pmu, cpu, 675 metadata_etmv4_ro[CS_ETMV4_TRCIDR8]); 676 data[CS_ETMV4_TRCAUTHSTATUS] = cs_etm_get_ro(cs_etm_pmu, cpu, 677 metadata_etmv4_ro[CS_ETMV4_TRCAUTHSTATUS]); 678 679 /* Kernels older than 5.19 may not expose ts_source */ 680 if (cs_etm_pmu_path_exists(cs_etm_pmu, cpu, metadata_etmv4_ro[CS_ETMV4_TS_SOURCE])) 681 data[CS_ETMV4_TS_SOURCE] = (__u64) cs_etm_get_ro_signed(cs_etm_pmu, cpu, 682 metadata_etmv4_ro[CS_ETMV4_TS_SOURCE]); 683 else { 684 pr_debug3("[%03d] pmu file 'ts_source' not found. Fallback to safe value (-1)\n", 685 cpu); 686 data[CS_ETMV4_TS_SOURCE] = (__u64) -1; 687 } 688 } 689 690 static void cs_etm_save_ete_header(__u64 data[], struct auxtrace_record *itr, int cpu) 691 { 692 struct cs_etm_recording *ptr = container_of(itr, struct cs_etm_recording, itr); 693 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 694 695 /* Get trace configuration register */ 696 data[CS_ETE_TRCCONFIGR] = cs_etmv4_get_config(itr); 697 /* traceID set to legacy version, in case new perf running on older system */ 698 data[CS_ETE_TRCTRACEIDR] = 699 CORESIGHT_LEGACY_CPU_TRACE_ID(cpu) | CORESIGHT_TRACE_ID_UNUSED_FLAG; 700 701 /* Get read-only information from sysFS */ 702 data[CS_ETE_TRCIDR0] = cs_etm_get_ro(cs_etm_pmu, cpu, 703 metadata_ete_ro[CS_ETE_TRCIDR0]); 704 data[CS_ETE_TRCIDR1] = cs_etm_get_ro(cs_etm_pmu, cpu, 705 metadata_ete_ro[CS_ETE_TRCIDR1]); 706 data[CS_ETE_TRCIDR2] = cs_etm_get_ro(cs_etm_pmu, cpu, 707 metadata_ete_ro[CS_ETE_TRCIDR2]); 708 data[CS_ETE_TRCIDR8] = cs_etm_get_ro(cs_etm_pmu, cpu, 709 metadata_ete_ro[CS_ETE_TRCIDR8]); 710 data[CS_ETE_TRCAUTHSTATUS] = cs_etm_get_ro(cs_etm_pmu, cpu, 711 metadata_ete_ro[CS_ETE_TRCAUTHSTATUS]); 712 /* ETE uses the same registers as ETMv4 plus TRCDEVARCH */ 713 data[CS_ETE_TRCDEVARCH] = cs_etm_get_ro(cs_etm_pmu, cpu, 714 metadata_ete_ro[CS_ETE_TRCDEVARCH]); 715 716 /* Kernels older than 5.19 may not expose ts_source */ 717 if (cs_etm_pmu_path_exists(cs_etm_pmu, cpu, metadata_ete_ro[CS_ETE_TS_SOURCE])) 718 data[CS_ETE_TS_SOURCE] = (__u64) cs_etm_get_ro_signed(cs_etm_pmu, cpu, 719 metadata_ete_ro[CS_ETE_TS_SOURCE]); 720 else { 721 pr_debug3("[%03d] pmu file 'ts_source' not found. Fallback to safe value (-1)\n", 722 cpu); 723 data[CS_ETE_TS_SOURCE] = (__u64) -1; 724 } 725 } 726 727 static void cs_etm_get_metadata(int cpu, u32 *offset, 728 struct auxtrace_record *itr, 729 struct perf_record_auxtrace_info *info) 730 { 731 u32 increment, nr_trc_params; 732 u64 magic; 733 struct cs_etm_recording *ptr = 734 container_of(itr, struct cs_etm_recording, itr); 735 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 736 737 /* first see what kind of tracer this cpu is affined to */ 738 if (cs_etm_is_ete(itr, cpu)) { 739 magic = __perf_cs_ete_magic; 740 cs_etm_save_ete_header(&info->priv[*offset], itr, cpu); 741 742 /* How much space was used */ 743 increment = CS_ETE_PRIV_MAX; 744 nr_trc_params = CS_ETE_PRIV_MAX - CS_ETM_COMMON_BLK_MAX_V1; 745 } else if (cs_etm_is_etmv4(itr, cpu)) { 746 magic = __perf_cs_etmv4_magic; 747 cs_etm_save_etmv4_header(&info->priv[*offset], itr, cpu); 748 749 /* How much space was used */ 750 increment = CS_ETMV4_PRIV_MAX; 751 nr_trc_params = CS_ETMV4_PRIV_MAX - CS_ETMV4_TRCCONFIGR; 752 } else { 753 magic = __perf_cs_etmv3_magic; 754 /* Get configuration register */ 755 info->priv[*offset + CS_ETM_ETMCR] = cs_etm_get_config(itr); 756 /* traceID set to legacy value in case new perf running on old system */ 757 info->priv[*offset + CS_ETM_ETMTRACEIDR] = 758 CORESIGHT_LEGACY_CPU_TRACE_ID(cpu) | CORESIGHT_TRACE_ID_UNUSED_FLAG; 759 /* Get read-only information from sysFS */ 760 info->priv[*offset + CS_ETM_ETMCCER] = 761 cs_etm_get_ro(cs_etm_pmu, cpu, 762 metadata_etmv3_ro[CS_ETM_ETMCCER]); 763 info->priv[*offset + CS_ETM_ETMIDR] = 764 cs_etm_get_ro(cs_etm_pmu, cpu, 765 metadata_etmv3_ro[CS_ETM_ETMIDR]); 766 767 /* How much space was used */ 768 increment = CS_ETM_PRIV_MAX; 769 nr_trc_params = CS_ETM_PRIV_MAX - CS_ETM_ETMCR; 770 } 771 772 /* Build generic header portion */ 773 info->priv[*offset + CS_ETM_MAGIC] = magic; 774 info->priv[*offset + CS_ETM_CPU] = cpu; 775 info->priv[*offset + CS_ETM_NR_TRC_PARAMS] = nr_trc_params; 776 /* Where the next CPU entry should start from */ 777 *offset += increment; 778 } 779 780 static int cs_etm_info_fill(struct auxtrace_record *itr, 781 struct perf_session *session, 782 struct perf_record_auxtrace_info *info, 783 size_t priv_size) 784 { 785 int i; 786 u32 offset; 787 u64 nr_cpu, type; 788 struct perf_cpu_map *cpu_map; 789 struct perf_cpu_map *event_cpus = session->evlist->core.user_requested_cpus; 790 struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus(); 791 struct cs_etm_recording *ptr = 792 container_of(itr, struct cs_etm_recording, itr); 793 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 794 795 if (priv_size != cs_etm_info_priv_size(itr, session->evlist)) 796 return -EINVAL; 797 798 if (!session->evlist->core.nr_mmaps) 799 return -EINVAL; 800 801 /* If the cpu_map has the "any" CPU all online CPUs are involved */ 802 if (perf_cpu_map__has_any_cpu(event_cpus)) { 803 cpu_map = online_cpus; 804 } else { 805 /* Make sure all specified CPUs are online */ 806 struct perf_cpu cpu; 807 808 perf_cpu_map__for_each_cpu(cpu, i, event_cpus) { 809 if (!perf_cpu_map__has(online_cpus, cpu)) 810 return -EINVAL; 811 } 812 813 cpu_map = event_cpus; 814 } 815 816 nr_cpu = perf_cpu_map__nr(cpu_map); 817 /* Get PMU type as dynamically assigned by the core */ 818 type = cs_etm_pmu->type; 819 820 /* First fill out the session header */ 821 info->type = PERF_AUXTRACE_CS_ETM; 822 info->priv[CS_HEADER_VERSION] = CS_HEADER_CURRENT_VERSION; 823 info->priv[CS_PMU_TYPE_CPUS] = type << 32; 824 info->priv[CS_PMU_TYPE_CPUS] |= nr_cpu; 825 info->priv[CS_ETM_SNAPSHOT] = ptr->snapshot_mode; 826 827 offset = CS_ETM_SNAPSHOT + 1; 828 829 for (i = 0; i < cpu__max_cpu().cpu && offset < priv_size; i++) { 830 struct perf_cpu cpu = { .cpu = i, }; 831 832 if (perf_cpu_map__has(cpu_map, cpu)) 833 cs_etm_get_metadata(i, &offset, itr, info); 834 } 835 836 perf_cpu_map__put(online_cpus); 837 838 return 0; 839 } 840 841 static int cs_etm_snapshot_start(struct auxtrace_record *itr) 842 { 843 struct cs_etm_recording *ptr = 844 container_of(itr, struct cs_etm_recording, itr); 845 struct evsel *evsel; 846 847 evlist__for_each_entry(ptr->evlist, evsel) { 848 if (evsel->core.attr.type == ptr->cs_etm_pmu->type) 849 return evsel__disable(evsel); 850 } 851 return -EINVAL; 852 } 853 854 static int cs_etm_snapshot_finish(struct auxtrace_record *itr) 855 { 856 struct cs_etm_recording *ptr = 857 container_of(itr, struct cs_etm_recording, itr); 858 struct evsel *evsel; 859 860 evlist__for_each_entry(ptr->evlist, evsel) { 861 if (evsel->core.attr.type == ptr->cs_etm_pmu->type) 862 return evsel__enable(evsel); 863 } 864 return -EINVAL; 865 } 866 867 static u64 cs_etm_reference(struct auxtrace_record *itr __maybe_unused) 868 { 869 return (((u64) rand() << 0) & 0x00000000FFFFFFFFull) | 870 (((u64) rand() << 32) & 0xFFFFFFFF00000000ull); 871 } 872 873 static void cs_etm_recording_free(struct auxtrace_record *itr) 874 { 875 struct cs_etm_recording *ptr = 876 container_of(itr, struct cs_etm_recording, itr); 877 878 free(ptr); 879 } 880 881 struct auxtrace_record *cs_etm_record_init(int *err) 882 { 883 struct perf_pmu *cs_etm_pmu; 884 struct cs_etm_recording *ptr; 885 886 cs_etm_pmu = perf_pmus__find(CORESIGHT_ETM_PMU_NAME); 887 888 if (!cs_etm_pmu) { 889 *err = -EINVAL; 890 goto out; 891 } 892 893 ptr = zalloc(sizeof(struct cs_etm_recording)); 894 if (!ptr) { 895 *err = -ENOMEM; 896 goto out; 897 } 898 899 ptr->cs_etm_pmu = cs_etm_pmu; 900 ptr->itr.pmu = cs_etm_pmu; 901 ptr->itr.parse_snapshot_options = cs_etm_parse_snapshot_options; 902 ptr->itr.recording_options = cs_etm_recording_options; 903 ptr->itr.info_priv_size = cs_etm_info_priv_size; 904 ptr->itr.info_fill = cs_etm_info_fill; 905 ptr->itr.snapshot_start = cs_etm_snapshot_start; 906 ptr->itr.snapshot_finish = cs_etm_snapshot_finish; 907 ptr->itr.reference = cs_etm_reference; 908 ptr->itr.free = cs_etm_recording_free; 909 ptr->itr.read_finish = auxtrace_record__read_finish; 910 911 *err = 0; 912 return &ptr->itr; 913 out: 914 return NULL; 915 } 916 917 /* 918 * Set a default config to enable the user changed config tracking mechanism 919 * (CFG_CHG and evsel__set_config_if_unset()). If no default is set then user 920 * changes aren't tracked. 921 */ 922 void 923 cs_etm_get_default_config(const struct perf_pmu *pmu __maybe_unused, 924 struct perf_event_attr *attr) 925 { 926 attr->sample_period = 1; 927 } 928