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/pmu.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_ETE_TRCDEVARCH] = "mgmt/trcdevarch" 57 }; 58 59 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu); 60 static bool cs_etm_is_ete(struct auxtrace_record *itr, int cpu); 61 62 static int cs_etm_set_context_id(struct auxtrace_record *itr, 63 struct evsel *evsel, int cpu) 64 { 65 struct cs_etm_recording *ptr; 66 struct perf_pmu *cs_etm_pmu; 67 char path[PATH_MAX]; 68 int err = -EINVAL; 69 u32 val; 70 u64 contextid; 71 72 ptr = container_of(itr, struct cs_etm_recording, itr); 73 cs_etm_pmu = ptr->cs_etm_pmu; 74 75 if (!cs_etm_is_etmv4(itr, cpu)) 76 goto out; 77 78 /* Get a handle on TRCIDR2 */ 79 snprintf(path, PATH_MAX, "cpu%d/%s", 80 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR2]); 81 err = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val); 82 83 /* There was a problem reading the file, bailing out */ 84 if (err != 1) { 85 pr_err("%s: can't read file %s\n", 86 CORESIGHT_ETM_PMU_NAME, path); 87 goto out; 88 } 89 90 /* User has configured for PID tracing, respects it. */ 91 contextid = evsel->core.attr.config & 92 (BIT(ETM_OPT_CTXTID) | BIT(ETM_OPT_CTXTID2)); 93 94 /* 95 * If user doesn't configure the contextid format, parse PMU format and 96 * enable PID tracing according to the "contextid" format bits: 97 * 98 * If bit ETM_OPT_CTXTID is set, trace CONTEXTIDR_EL1; 99 * If bit ETM_OPT_CTXTID2 is set, trace CONTEXTIDR_EL2. 100 */ 101 if (!contextid) 102 contextid = perf_pmu__format_bits(&cs_etm_pmu->format, 103 "contextid"); 104 105 if (contextid & BIT(ETM_OPT_CTXTID)) { 106 /* 107 * TRCIDR2.CIDSIZE, bit [9-5], indicates whether contextID 108 * tracing is supported: 109 * 0b00000 Context ID tracing is not supported. 110 * 0b00100 Maximum of 32-bit Context ID size. 111 * All other values are reserved. 112 */ 113 val = BMVAL(val, 5, 9); 114 if (!val || val != 0x4) { 115 pr_err("%s: CONTEXTIDR_EL1 isn't supported\n", 116 CORESIGHT_ETM_PMU_NAME); 117 err = -EINVAL; 118 goto out; 119 } 120 } 121 122 if (contextid & BIT(ETM_OPT_CTXTID2)) { 123 /* 124 * TRCIDR2.VMIDOPT[30:29] != 0 and 125 * TRCIDR2.VMIDSIZE[14:10] == 0b00100 (32bit virtual contextid) 126 * We can't support CONTEXTIDR in VMID if the size of the 127 * virtual context id is < 32bit. 128 * Any value of VMIDSIZE >= 4 (i.e, > 32bit) is fine for us. 129 */ 130 if (!BMVAL(val, 29, 30) || BMVAL(val, 10, 14) < 4) { 131 pr_err("%s: CONTEXTIDR_EL2 isn't supported\n", 132 CORESIGHT_ETM_PMU_NAME); 133 err = -EINVAL; 134 goto out; 135 } 136 } 137 138 /* All good, let the kernel know */ 139 evsel->core.attr.config |= contextid; 140 err = 0; 141 142 out: 143 return err; 144 } 145 146 static int cs_etm_set_timestamp(struct auxtrace_record *itr, 147 struct evsel *evsel, int cpu) 148 { 149 struct cs_etm_recording *ptr; 150 struct perf_pmu *cs_etm_pmu; 151 char path[PATH_MAX]; 152 int err = -EINVAL; 153 u32 val; 154 155 ptr = container_of(itr, struct cs_etm_recording, itr); 156 cs_etm_pmu = ptr->cs_etm_pmu; 157 158 if (!cs_etm_is_etmv4(itr, cpu)) 159 goto out; 160 161 /* Get a handle on TRCIRD0 */ 162 snprintf(path, PATH_MAX, "cpu%d/%s", 163 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]); 164 err = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val); 165 166 /* There was a problem reading the file, bailing out */ 167 if (err != 1) { 168 pr_err("%s: can't read file %s\n", 169 CORESIGHT_ETM_PMU_NAME, path); 170 goto out; 171 } 172 173 /* 174 * TRCIDR0.TSSIZE, bit [28-24], indicates whether global timestamping 175 * is supported: 176 * 0b00000 Global timestamping is not implemented 177 * 0b00110 Implementation supports a maximum timestamp of 48bits. 178 * 0b01000 Implementation supports a maximum timestamp of 64bits. 179 */ 180 val &= GENMASK(28, 24); 181 if (!val) { 182 err = -EINVAL; 183 goto out; 184 } 185 186 /* All good, let the kernel know */ 187 evsel->core.attr.config |= (1 << ETM_OPT_TS); 188 err = 0; 189 190 out: 191 return err; 192 } 193 194 #define ETM_SET_OPT_CTXTID (1 << 0) 195 #define ETM_SET_OPT_TS (1 << 1) 196 #define ETM_SET_OPT_MASK (ETM_SET_OPT_CTXTID | ETM_SET_OPT_TS) 197 198 static int cs_etm_set_option(struct auxtrace_record *itr, 199 struct evsel *evsel, u32 option) 200 { 201 int i, err = -EINVAL; 202 struct perf_cpu_map *event_cpus = evsel->evlist->core.cpus; 203 struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL); 204 205 /* Set option of each CPU we have */ 206 for (i = 0; i < cpu__max_cpu().cpu; i++) { 207 struct perf_cpu cpu = { .cpu = i, }; 208 209 if (!perf_cpu_map__has(event_cpus, cpu) || 210 !perf_cpu_map__has(online_cpus, cpu)) 211 continue; 212 213 if (option & BIT(ETM_OPT_CTXTID)) { 214 err = cs_etm_set_context_id(itr, evsel, i); 215 if (err) 216 goto out; 217 } 218 if (option & BIT(ETM_OPT_TS)) { 219 err = cs_etm_set_timestamp(itr, evsel, i); 220 if (err) 221 goto out; 222 } 223 if (option & ~(BIT(ETM_OPT_CTXTID) | BIT(ETM_OPT_TS))) 224 /* Nothing else is currently supported */ 225 goto out; 226 } 227 228 err = 0; 229 out: 230 perf_cpu_map__put(online_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 pr_err("failed to set sink \"%s\" on event %s with %d (%s)\n", 277 sink, evsel__name(evsel), errno, 278 str_error_r(errno, msg, sizeof(msg))); 279 return ret; 280 } 281 282 evsel->core.attr.config2 |= hash; 283 return 0; 284 } 285 286 /* 287 * No sink was provided on the command line - allow the CoreSight 288 * system to look for a default 289 */ 290 return 0; 291 } 292 293 static int cs_etm_recording_options(struct auxtrace_record *itr, 294 struct evlist *evlist, 295 struct record_opts *opts) 296 { 297 int ret; 298 struct cs_etm_recording *ptr = 299 container_of(itr, struct cs_etm_recording, itr); 300 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 301 struct evsel *evsel, *cs_etm_evsel = NULL; 302 struct perf_cpu_map *cpus = evlist->core.cpus; 303 bool privileged = perf_event_paranoid_check(-1); 304 int err = 0; 305 306 ptr->evlist = evlist; 307 ptr->snapshot_mode = opts->auxtrace_snapshot_mode; 308 309 if (!record_opts__no_switch_events(opts) && 310 perf_can_record_switch_events()) 311 opts->record_switch_events = true; 312 313 evlist__for_each_entry(evlist, evsel) { 314 if (evsel->core.attr.type == cs_etm_pmu->type) { 315 if (cs_etm_evsel) { 316 pr_err("There may be only one %s event\n", 317 CORESIGHT_ETM_PMU_NAME); 318 return -EINVAL; 319 } 320 evsel->core.attr.freq = 0; 321 evsel->core.attr.sample_period = 1; 322 cs_etm_evsel = evsel; 323 opts->full_auxtrace = true; 324 } 325 } 326 327 /* no need to continue if at least one event of interest was found */ 328 if (!cs_etm_evsel) 329 return 0; 330 331 ret = cs_etm_set_sink_attr(cs_etm_pmu, cs_etm_evsel); 332 if (ret) 333 return ret; 334 335 if (opts->use_clockid) { 336 pr_err("Cannot use clockid (-k option) with %s\n", 337 CORESIGHT_ETM_PMU_NAME); 338 return -EINVAL; 339 } 340 341 /* we are in snapshot mode */ 342 if (opts->auxtrace_snapshot_mode) { 343 /* 344 * No size were given to '-S' or '-m,', so go with 345 * the default 346 */ 347 if (!opts->auxtrace_snapshot_size && 348 !opts->auxtrace_mmap_pages) { 349 if (privileged) { 350 opts->auxtrace_mmap_pages = MiB(4) / page_size; 351 } else { 352 opts->auxtrace_mmap_pages = 353 KiB(128) / page_size; 354 if (opts->mmap_pages == UINT_MAX) 355 opts->mmap_pages = KiB(256) / page_size; 356 } 357 } else if (!opts->auxtrace_mmap_pages && !privileged && 358 opts->mmap_pages == UINT_MAX) { 359 opts->mmap_pages = KiB(256) / page_size; 360 } 361 362 /* 363 * '-m,xyz' was specified but no snapshot size, so make the 364 * snapshot size as big as the auxtrace mmap area. 365 */ 366 if (!opts->auxtrace_snapshot_size) { 367 opts->auxtrace_snapshot_size = 368 opts->auxtrace_mmap_pages * (size_t)page_size; 369 } 370 371 /* 372 * -Sxyz was specified but no auxtrace mmap area, so make the 373 * auxtrace mmap area big enough to fit the requested snapshot 374 * size. 375 */ 376 if (!opts->auxtrace_mmap_pages) { 377 size_t sz = opts->auxtrace_snapshot_size; 378 379 sz = round_up(sz, page_size) / page_size; 380 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz); 381 } 382 383 /* Snapshot size can't be bigger than the auxtrace area */ 384 if (opts->auxtrace_snapshot_size > 385 opts->auxtrace_mmap_pages * (size_t)page_size) { 386 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n", 387 opts->auxtrace_snapshot_size, 388 opts->auxtrace_mmap_pages * (size_t)page_size); 389 return -EINVAL; 390 } 391 392 /* Something went wrong somewhere - this shouldn't happen */ 393 if (!opts->auxtrace_snapshot_size || 394 !opts->auxtrace_mmap_pages) { 395 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n"); 396 return -EINVAL; 397 } 398 } 399 400 /* We are in full trace mode but '-m,xyz' wasn't specified */ 401 if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) { 402 if (privileged) { 403 opts->auxtrace_mmap_pages = MiB(4) / page_size; 404 } else { 405 opts->auxtrace_mmap_pages = KiB(128) / page_size; 406 if (opts->mmap_pages == UINT_MAX) 407 opts->mmap_pages = KiB(256) / page_size; 408 } 409 410 } 411 412 if (opts->auxtrace_snapshot_mode) 413 pr_debug2("%s snapshot size: %zu\n", CORESIGHT_ETM_PMU_NAME, 414 opts->auxtrace_snapshot_size); 415 416 /* 417 * To obtain the auxtrace buffer file descriptor, the auxtrace 418 * event must come first. 419 */ 420 evlist__to_front(evlist, cs_etm_evsel); 421 422 /* 423 * In the case of per-cpu mmaps, we need the CPU on the 424 * AUX event. We also need the contextID in order to be notified 425 * when a context switch happened. 426 */ 427 if (!perf_cpu_map__empty(cpus)) { 428 evsel__set_sample_bit(cs_etm_evsel, CPU); 429 430 err = cs_etm_set_option(itr, cs_etm_evsel, 431 BIT(ETM_OPT_CTXTID) | BIT(ETM_OPT_TS)); 432 if (err) 433 goto out; 434 } 435 436 /* Add dummy event to keep tracking */ 437 if (opts->full_auxtrace) { 438 struct evsel *tracking_evsel; 439 440 err = parse_events(evlist, "dummy:u", NULL); 441 if (err) 442 goto out; 443 444 tracking_evsel = evlist__last(evlist); 445 evlist__set_tracking_event(evlist, tracking_evsel); 446 447 tracking_evsel->core.attr.freq = 0; 448 tracking_evsel->core.attr.sample_period = 1; 449 450 /* In per-cpu case, always need the time of mmap events etc */ 451 if (!perf_cpu_map__empty(cpus)) 452 evsel__set_sample_bit(tracking_evsel, TIME); 453 } 454 455 out: 456 return err; 457 } 458 459 static u64 cs_etm_get_config(struct auxtrace_record *itr) 460 { 461 u64 config = 0; 462 struct cs_etm_recording *ptr = 463 container_of(itr, struct cs_etm_recording, itr); 464 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 465 struct evlist *evlist = ptr->evlist; 466 struct evsel *evsel; 467 468 evlist__for_each_entry(evlist, evsel) { 469 if (evsel->core.attr.type == cs_etm_pmu->type) { 470 /* 471 * Variable perf_event_attr::config is assigned to 472 * ETMv3/PTM. The bit fields have been made to match 473 * the ETMv3.5 ETRMCR register specification. See the 474 * PMU_FORMAT_ATTR() declarations in 475 * drivers/hwtracing/coresight/coresight-perf.c for 476 * details. 477 */ 478 config = evsel->core.attr.config; 479 break; 480 } 481 } 482 483 return config; 484 } 485 486 #ifndef BIT 487 #define BIT(N) (1UL << (N)) 488 #endif 489 490 static u64 cs_etmv4_get_config(struct auxtrace_record *itr) 491 { 492 u64 config = 0; 493 u64 config_opts = 0; 494 495 /* 496 * The perf event variable config bits represent both 497 * the command line options and register programming 498 * bits in ETMv3/PTM. For ETMv4 we must remap options 499 * to real bits 500 */ 501 config_opts = cs_etm_get_config(itr); 502 if (config_opts & BIT(ETM_OPT_CYCACC)) 503 config |= BIT(ETM4_CFG_BIT_CYCACC); 504 if (config_opts & BIT(ETM_OPT_CTXTID)) 505 config |= BIT(ETM4_CFG_BIT_CTXTID); 506 if (config_opts & BIT(ETM_OPT_TS)) 507 config |= BIT(ETM4_CFG_BIT_TS); 508 if (config_opts & BIT(ETM_OPT_RETSTK)) 509 config |= BIT(ETM4_CFG_BIT_RETSTK); 510 if (config_opts & BIT(ETM_OPT_CTXTID2)) 511 config |= BIT(ETM4_CFG_BIT_VMID) | 512 BIT(ETM4_CFG_BIT_VMID_OPT); 513 return config; 514 } 515 516 static size_t 517 cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused, 518 struct evlist *evlist __maybe_unused) 519 { 520 int i; 521 int etmv3 = 0, etmv4 = 0, ete = 0; 522 struct perf_cpu_map *event_cpus = evlist->core.cpus; 523 struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL); 524 525 /* cpu map is not empty, we have specific CPUs to work with */ 526 if (!perf_cpu_map__empty(event_cpus)) { 527 for (i = 0; i < cpu__max_cpu().cpu; i++) { 528 struct perf_cpu cpu = { .cpu = i, }; 529 530 if (!perf_cpu_map__has(event_cpus, cpu) || 531 !perf_cpu_map__has(online_cpus, cpu)) 532 continue; 533 534 if (cs_etm_is_ete(itr, i)) 535 ete++; 536 else if (cs_etm_is_etmv4(itr, i)) 537 etmv4++; 538 else 539 etmv3++; 540 } 541 } else { 542 /* get configuration for all CPUs in the system */ 543 for (i = 0; i < cpu__max_cpu().cpu; i++) { 544 struct perf_cpu cpu = { .cpu = i, }; 545 546 if (!perf_cpu_map__has(online_cpus, cpu)) 547 continue; 548 549 if (cs_etm_is_ete(itr, i)) 550 ete++; 551 else if (cs_etm_is_etmv4(itr, i)) 552 etmv4++; 553 else 554 etmv3++; 555 } 556 } 557 558 perf_cpu_map__put(online_cpus); 559 560 return (CS_ETM_HEADER_SIZE + 561 (ete * CS_ETE_PRIV_SIZE) + 562 (etmv4 * CS_ETMV4_PRIV_SIZE) + 563 (etmv3 * CS_ETMV3_PRIV_SIZE)); 564 } 565 566 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu) 567 { 568 bool ret = false; 569 char path[PATH_MAX]; 570 int scan; 571 unsigned int val; 572 struct cs_etm_recording *ptr = 573 container_of(itr, struct cs_etm_recording, itr); 574 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 575 576 /* Take any of the RO files for ETMv4 and see if it present */ 577 snprintf(path, PATH_MAX, "cpu%d/%s", 578 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]); 579 scan = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val); 580 581 /* The file was read successfully, we have a winner */ 582 if (scan == 1) 583 ret = true; 584 585 return ret; 586 } 587 588 static int cs_etm_get_ro(struct perf_pmu *pmu, int cpu, const char *path) 589 { 590 char pmu_path[PATH_MAX]; 591 int scan; 592 unsigned int val = 0; 593 594 /* Get RO metadata from sysfs */ 595 snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path); 596 597 scan = perf_pmu__scan_file(pmu, pmu_path, "%x", &val); 598 if (scan != 1) 599 pr_err("%s: error reading: %s\n", __func__, pmu_path); 600 601 return val; 602 } 603 604 #define TRCDEVARCH_ARCHPART_SHIFT 0 605 #define TRCDEVARCH_ARCHPART_MASK GENMASK(11, 0) 606 #define TRCDEVARCH_ARCHPART(x) (((x) & TRCDEVARCH_ARCHPART_MASK) >> TRCDEVARCH_ARCHPART_SHIFT) 607 608 #define TRCDEVARCH_ARCHVER_SHIFT 12 609 #define TRCDEVARCH_ARCHVER_MASK GENMASK(15, 12) 610 #define TRCDEVARCH_ARCHVER(x) (((x) & TRCDEVARCH_ARCHVER_MASK) >> TRCDEVARCH_ARCHVER_SHIFT) 611 612 static bool cs_etm_is_ete(struct auxtrace_record *itr, int cpu) 613 { 614 struct cs_etm_recording *ptr = container_of(itr, struct cs_etm_recording, itr); 615 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 616 int trcdevarch = cs_etm_get_ro(cs_etm_pmu, cpu, metadata_etmv4_ro[CS_ETE_TRCDEVARCH]); 617 618 /* 619 * ETE if ARCHVER is 5 (ARCHVER is 4 for ETM) and ARCHPART is 0xA13. 620 * See ETM_DEVARCH_ETE_ARCH in coresight-etm4x.h 621 */ 622 return TRCDEVARCH_ARCHVER(trcdevarch) == 5 && TRCDEVARCH_ARCHPART(trcdevarch) == 0xA13; 623 } 624 625 static void cs_etm_save_etmv4_header(__u64 data[], struct auxtrace_record *itr, int cpu) 626 { 627 struct cs_etm_recording *ptr = container_of(itr, struct cs_etm_recording, itr); 628 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 629 630 /* Get trace configuration register */ 631 data[CS_ETMV4_TRCCONFIGR] = cs_etmv4_get_config(itr); 632 /* Get traceID from the framework */ 633 data[CS_ETMV4_TRCTRACEIDR] = coresight_get_trace_id(cpu); 634 /* Get read-only information from sysFS */ 635 data[CS_ETMV4_TRCIDR0] = cs_etm_get_ro(cs_etm_pmu, cpu, 636 metadata_etmv4_ro[CS_ETMV4_TRCIDR0]); 637 data[CS_ETMV4_TRCIDR1] = cs_etm_get_ro(cs_etm_pmu, cpu, 638 metadata_etmv4_ro[CS_ETMV4_TRCIDR1]); 639 data[CS_ETMV4_TRCIDR2] = cs_etm_get_ro(cs_etm_pmu, cpu, 640 metadata_etmv4_ro[CS_ETMV4_TRCIDR2]); 641 data[CS_ETMV4_TRCIDR8] = cs_etm_get_ro(cs_etm_pmu, cpu, 642 metadata_etmv4_ro[CS_ETMV4_TRCIDR8]); 643 data[CS_ETMV4_TRCAUTHSTATUS] = cs_etm_get_ro(cs_etm_pmu, cpu, 644 metadata_etmv4_ro[CS_ETMV4_TRCAUTHSTATUS]); 645 } 646 647 static void cs_etm_get_metadata(int cpu, u32 *offset, 648 struct auxtrace_record *itr, 649 struct perf_record_auxtrace_info *info) 650 { 651 u32 increment, nr_trc_params; 652 u64 magic; 653 struct cs_etm_recording *ptr = 654 container_of(itr, struct cs_etm_recording, itr); 655 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 656 657 /* first see what kind of tracer this cpu is affined to */ 658 if (cs_etm_is_ete(itr, cpu)) { 659 magic = __perf_cs_ete_magic; 660 /* ETE uses the same registers as ETMv4 plus TRCDEVARCH */ 661 cs_etm_save_etmv4_header(&info->priv[*offset], itr, cpu); 662 info->priv[*offset + CS_ETE_TRCDEVARCH] = 663 cs_etm_get_ro(cs_etm_pmu, cpu, 664 metadata_etmv4_ro[CS_ETE_TRCDEVARCH]); 665 666 /* How much space was used */ 667 increment = CS_ETE_PRIV_MAX; 668 nr_trc_params = CS_ETE_PRIV_MAX - CS_ETM_COMMON_BLK_MAX_V1; 669 } else if (cs_etm_is_etmv4(itr, cpu)) { 670 magic = __perf_cs_etmv4_magic; 671 cs_etm_save_etmv4_header(&info->priv[*offset], itr, cpu); 672 673 /* How much space was used */ 674 increment = CS_ETMV4_PRIV_MAX; 675 nr_trc_params = CS_ETMV4_PRIV_MAX - CS_ETMV4_TRCCONFIGR; 676 } else { 677 magic = __perf_cs_etmv3_magic; 678 /* Get configuration register */ 679 info->priv[*offset + CS_ETM_ETMCR] = cs_etm_get_config(itr); 680 /* Get traceID from the framework */ 681 info->priv[*offset + CS_ETM_ETMTRACEIDR] = 682 coresight_get_trace_id(cpu); 683 /* Get read-only information from sysFS */ 684 info->priv[*offset + CS_ETM_ETMCCER] = 685 cs_etm_get_ro(cs_etm_pmu, cpu, 686 metadata_etmv3_ro[CS_ETM_ETMCCER]); 687 info->priv[*offset + CS_ETM_ETMIDR] = 688 cs_etm_get_ro(cs_etm_pmu, cpu, 689 metadata_etmv3_ro[CS_ETM_ETMIDR]); 690 691 /* How much space was used */ 692 increment = CS_ETM_PRIV_MAX; 693 nr_trc_params = CS_ETM_PRIV_MAX - CS_ETM_ETMCR; 694 } 695 696 /* Build generic header portion */ 697 info->priv[*offset + CS_ETM_MAGIC] = magic; 698 info->priv[*offset + CS_ETM_CPU] = cpu; 699 info->priv[*offset + CS_ETM_NR_TRC_PARAMS] = nr_trc_params; 700 /* Where the next CPU entry should start from */ 701 *offset += increment; 702 } 703 704 static int cs_etm_info_fill(struct auxtrace_record *itr, 705 struct perf_session *session, 706 struct perf_record_auxtrace_info *info, 707 size_t priv_size) 708 { 709 int i; 710 u32 offset; 711 u64 nr_cpu, type; 712 struct perf_cpu_map *cpu_map; 713 struct perf_cpu_map *event_cpus = session->evlist->core.cpus; 714 struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL); 715 struct cs_etm_recording *ptr = 716 container_of(itr, struct cs_etm_recording, itr); 717 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu; 718 719 if (priv_size != cs_etm_info_priv_size(itr, session->evlist)) 720 return -EINVAL; 721 722 if (!session->evlist->core.nr_mmaps) 723 return -EINVAL; 724 725 /* If the cpu_map is empty all online CPUs are involved */ 726 if (perf_cpu_map__empty(event_cpus)) { 727 cpu_map = online_cpus; 728 } else { 729 /* Make sure all specified CPUs are online */ 730 for (i = 0; i < perf_cpu_map__nr(event_cpus); i++) { 731 struct perf_cpu cpu = { .cpu = i, }; 732 733 if (perf_cpu_map__has(event_cpus, cpu) && 734 !perf_cpu_map__has(online_cpus, cpu)) 735 return -EINVAL; 736 } 737 738 cpu_map = event_cpus; 739 } 740 741 nr_cpu = perf_cpu_map__nr(cpu_map); 742 /* Get PMU type as dynamically assigned by the core */ 743 type = cs_etm_pmu->type; 744 745 /* First fill out the session header */ 746 info->type = PERF_AUXTRACE_CS_ETM; 747 info->priv[CS_HEADER_VERSION] = CS_HEADER_CURRENT_VERSION; 748 info->priv[CS_PMU_TYPE_CPUS] = type << 32; 749 info->priv[CS_PMU_TYPE_CPUS] |= nr_cpu; 750 info->priv[CS_ETM_SNAPSHOT] = ptr->snapshot_mode; 751 752 offset = CS_ETM_SNAPSHOT + 1; 753 754 for (i = 0; i < cpu__max_cpu().cpu && offset < priv_size; i++) { 755 struct perf_cpu cpu = { .cpu = i, }; 756 757 if (perf_cpu_map__has(cpu_map, cpu)) 758 cs_etm_get_metadata(i, &offset, itr, info); 759 } 760 761 perf_cpu_map__put(online_cpus); 762 763 return 0; 764 } 765 766 static int cs_etm_snapshot_start(struct auxtrace_record *itr) 767 { 768 struct cs_etm_recording *ptr = 769 container_of(itr, struct cs_etm_recording, itr); 770 struct evsel *evsel; 771 772 evlist__for_each_entry(ptr->evlist, evsel) { 773 if (evsel->core.attr.type == ptr->cs_etm_pmu->type) 774 return evsel__disable(evsel); 775 } 776 return -EINVAL; 777 } 778 779 static int cs_etm_snapshot_finish(struct auxtrace_record *itr) 780 { 781 struct cs_etm_recording *ptr = 782 container_of(itr, struct cs_etm_recording, itr); 783 struct evsel *evsel; 784 785 evlist__for_each_entry(ptr->evlist, evsel) { 786 if (evsel->core.attr.type == ptr->cs_etm_pmu->type) 787 return evsel__enable(evsel); 788 } 789 return -EINVAL; 790 } 791 792 static u64 cs_etm_reference(struct auxtrace_record *itr __maybe_unused) 793 { 794 return (((u64) rand() << 0) & 0x00000000FFFFFFFFull) | 795 (((u64) rand() << 32) & 0xFFFFFFFF00000000ull); 796 } 797 798 static void cs_etm_recording_free(struct auxtrace_record *itr) 799 { 800 struct cs_etm_recording *ptr = 801 container_of(itr, struct cs_etm_recording, itr); 802 803 free(ptr); 804 } 805 806 struct auxtrace_record *cs_etm_record_init(int *err) 807 { 808 struct perf_pmu *cs_etm_pmu; 809 struct cs_etm_recording *ptr; 810 811 cs_etm_pmu = perf_pmu__find(CORESIGHT_ETM_PMU_NAME); 812 813 if (!cs_etm_pmu) { 814 *err = -EINVAL; 815 goto out; 816 } 817 818 ptr = zalloc(sizeof(struct cs_etm_recording)); 819 if (!ptr) { 820 *err = -ENOMEM; 821 goto out; 822 } 823 824 ptr->cs_etm_pmu = cs_etm_pmu; 825 ptr->itr.pmu = cs_etm_pmu; 826 ptr->itr.parse_snapshot_options = cs_etm_parse_snapshot_options; 827 ptr->itr.recording_options = cs_etm_recording_options; 828 ptr->itr.info_priv_size = cs_etm_info_priv_size; 829 ptr->itr.info_fill = cs_etm_info_fill; 830 ptr->itr.snapshot_start = cs_etm_snapshot_start; 831 ptr->itr.snapshot_finish = cs_etm_snapshot_finish; 832 ptr->itr.reference = cs_etm_reference; 833 ptr->itr.free = cs_etm_recording_free; 834 ptr->itr.read_finish = auxtrace_record__read_finish; 835 836 *err = 0; 837 return &ptr->itr; 838 out: 839 return NULL; 840 } 841