cs-etm.c 23.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7
/*
 * Copyright(C) 2015 Linaro Limited. All rights reserved.
 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
 */

#include <api/fs/fs.h>
8
#include <linux/bits.h>
9
#include <linux/bitops.h>
10
#include <linux/compiler.h>
11 12 13
#include <linux/coresight-pmu.h>
#include <linux/kernel.h>
#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 22
#include "../../util/auxtrace.h"
#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 29
#include "../../util/pmu.h"
#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 38 39

struct cs_etm_recording {
	struct auxtrace_record	itr;
	struct perf_pmu		*cs_etm_pmu;
40
	struct evlist		*evlist;
41 42 43 44
	bool			snapshot_mode;
	size_t			snapshot_size;
};

45 46 47 48 49
static const char *metadata_etmv3_ro[CS_ETM_PRIV_MAX] = {
	[CS_ETM_ETMCCER]	= "mgmt/etmccer",
	[CS_ETM_ETMIDR]		= "mgmt/etmidr",
};

50
static const char * const metadata_etmv4_ro[] = {
51 52 53 54 55
	[CS_ETMV4_TRCIDR0]		= "trcidr/trcidr0",
	[CS_ETMV4_TRCIDR1]		= "trcidr/trcidr1",
	[CS_ETMV4_TRCIDR2]		= "trcidr/trcidr2",
	[CS_ETMV4_TRCIDR8]		= "trcidr/trcidr8",
	[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 66 67 68 69
{
	struct cs_etm_recording *ptr;
	struct perf_pmu *cs_etm_pmu;
	char path[PATH_MAX];
	int err = -EINVAL;
	u32 val;
70
	u64 contextid;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

	ptr = container_of(itr, struct cs_etm_recording, itr);
	cs_etm_pmu = ptr->cs_etm_pmu;

	if (!cs_etm_is_etmv4(itr, cpu))
		goto out;

	/* Get a handle on TRCIRD2 */
	snprintf(path, PATH_MAX, "cpu%d/%s",
		 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR2]);
	err = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val);

	/* There was a problem reading the file, bailing out */
	if (err != 1) {
		pr_err("%s: can't read file %s\n",
		       CORESIGHT_ETM_PMU_NAME, path);
		goto out;
	}

90 91 92 93
	/* User has configured for PID tracing, respects it. */
	contextid = evsel->core.attr.config &
			(BIT(ETM_OPT_CTXTID) | BIT(ETM_OPT_CTXTID2));

94
	/*
95 96 97 98 99
	 * If user doesn't configure the contextid format, parse PMU format and
	 * enable PID tracing according to the "contextid" format bits:
	 *
	 *   If bit ETM_OPT_CTXTID is set, trace CONTEXTIDR_EL1;
	 *   If bit ETM_OPT_CTXTID2 is set, trace CONTEXTIDR_EL2.
100
	 */
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	if (!contextid)
		contextid = perf_pmu__format_bits(&cs_etm_pmu->format,
						  "contextid");

	if (contextid & BIT(ETM_OPT_CTXTID)) {
		/*
		 * TRCIDR2.CIDSIZE, bit [9-5], indicates whether contextID
		 * tracing is supported:
		 *  0b00000 Context ID tracing is not supported.
		 *  0b00100 Maximum of 32-bit Context ID size.
		 *  All other values are reserved.
		 */
		val = BMVAL(val, 5, 9);
		if (!val || val != 0x4) {
			pr_err("%s: CONTEXTIDR_EL1 isn't supported\n",
			       CORESIGHT_ETM_PMU_NAME);
			err = -EINVAL;
			goto out;
		}
	}

	if (contextid & BIT(ETM_OPT_CTXTID2)) {
		/*
		 * TRCIDR2.VMIDOPT[30:29] != 0 and
		 * TRCIDR2.VMIDSIZE[14:10] == 0b00100 (32bit virtual contextid)
		 * We can't support CONTEXTIDR in VMID if the size of the
		 * virtual context id is < 32bit.
		 * Any value of VMIDSIZE >= 4 (i.e, > 32bit) is fine for us.
		 */
		if (!BMVAL(val, 29, 30) || BMVAL(val, 10, 14) < 4) {
			pr_err("%s: CONTEXTIDR_EL2 isn't supported\n",
			       CORESIGHT_ETM_PMU_NAME);
			err = -EINVAL;
			goto out;
		}
136 137 138
	}

	/* All good, let the kernel know */
139
	evsel->core.attr.config |= contextid;
140 141 142 143 144 145
	err = 0;

out:
	return err;
}

146
static int cs_etm_set_timestamp(struct auxtrace_record *itr,
147
				struct evsel *evsel, int cpu)
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
	struct cs_etm_recording *ptr;
	struct perf_pmu *cs_etm_pmu;
	char path[PATH_MAX];
	int err = -EINVAL;
	u32 val;

	ptr = container_of(itr, struct cs_etm_recording, itr);
	cs_etm_pmu = ptr->cs_etm_pmu;

	if (!cs_etm_is_etmv4(itr, cpu))
		goto out;

	/* Get a handle on TRCIRD0 */
	snprintf(path, PATH_MAX, "cpu%d/%s",
		 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
	err = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val);

	/* There was a problem reading the file, bailing out */
	if (err != 1) {
		pr_err("%s: can't read file %s\n",
		       CORESIGHT_ETM_PMU_NAME, path);
		goto out;
	}

	/*
	 * TRCIDR0.TSSIZE, bit [28-24], indicates whether global timestamping
	 * is supported:
	 *  0b00000 Global timestamping is not implemented
	 *  0b00110 Implementation supports a maximum timestamp of 48bits.
	 *  0b01000 Implementation supports a maximum timestamp of 64bits.
	 */
	val &= GENMASK(28, 24);
	if (!val) {
		err = -EINVAL;
		goto out;
	}

	/* All good, let the kernel know */
187
	evsel->core.attr.config |= (1 << ETM_OPT_TS);
188 189 190 191 192 193
	err = 0;

out:
	return err;
}

194 195 196 197
#define ETM_SET_OPT_CTXTID	(1 << 0)
#define ETM_SET_OPT_TS		(1 << 1)
#define ETM_SET_OPT_MASK	(ETM_SET_OPT_CTXTID | ETM_SET_OPT_TS)

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 206 207 208 209 210

	/* Set option of each CPU we have */
	for (i = 0; i < cpu__max_cpu(); i++) {
		if (!cpu_map__has(event_cpus, i) ||
		    !cpu_map__has(online_cpus, i))
			continue;

211
		if (option & BIT(ETM_OPT_CTXTID)) {
212 213 214
			err = cs_etm_set_context_id(itr, evsel, i);
			if (err)
				goto out;
215
		}
216
		if (option & BIT(ETM_OPT_TS)) {
217 218 219
			err = cs_etm_set_timestamp(itr, evsel, i);
			if (err)
				goto out;
220
		}
221
		if (option & ~(BIT(ETM_OPT_CTXTID) | BIT(ETM_OPT_TS)))
222 223
			/* Nothing else is currently supported */
			goto out;
224 225 226 227
	}

	err = 0;
out:
228
	perf_cpu_map__put(online_cpus);
229 230 231
	return err;
}

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static int cs_etm_parse_snapshot_options(struct auxtrace_record *itr,
					 struct record_opts *opts,
					 const char *str)
{
	struct cs_etm_recording *ptr =
				container_of(itr, struct cs_etm_recording, itr);
	unsigned long long snapshot_size = 0;
	char *endptr;

	if (str) {
		snapshot_size = strtoull(str, &endptr, 0);
		if (*endptr || snapshot_size > SIZE_MAX)
			return -1;
	}

	opts->auxtrace_snapshot_mode = true;
	opts->auxtrace_snapshot_size = snapshot_size;
	ptr->snapshot_size = snapshot_size;

	return 0;
}

254
static int cs_etm_set_sink_attr(struct perf_pmu *pmu,
255
				struct evsel *evsel)
256 257
{
	char msg[BUFSIZ], path[PATH_MAX], *sink;
258
	struct evsel_config_term *term;
259 260 261
	int ret = -EINVAL;
	u32 hash;

262
	if (evsel->core.attr.config2 & GENMASK(31, 0))
263 264 265
		return 0;

	list_for_each_entry(term, &evsel->config_terms, list) {
266
		if (term->type != EVSEL__CONFIG_TERM_DRV_CFG)
267 268
			continue;

269
		sink = term->val.str;
270 271 272 273 274
		snprintf(path, PATH_MAX, "sinks/%s", sink);

		ret = perf_pmu__scan_file(pmu, path, "%x", &hash);
		if (ret != 1) {
			pr_err("failed to set sink \"%s\" on event %s with %d (%s)\n",
275
			       sink, evsel__name(evsel), errno,
276 277 278 279
			       str_error_r(errno, msg, sizeof(msg)));
			return ret;
		}

280
		evsel->core.attr.config2 |= hash;
281 282 283 284
		return 0;
	}

	/*
285 286
	 * No sink was provided on the command line - allow the CoreSight
	 * system to look for a default
287
	 */
288
	return 0;
289 290
}

291
static int cs_etm_recording_options(struct auxtrace_record *itr,
292
				    struct evlist *evlist,
293 294
				    struct record_opts *opts)
{
295
	int ret;
296 297 298
	struct cs_etm_recording *ptr =
				container_of(itr, struct cs_etm_recording, itr);
	struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
299
	struct evsel *evsel, *cs_etm_evsel = NULL;
300
	struct perf_cpu_map *cpus = evlist->core.cpus;
301
	bool privileged = perf_event_paranoid_check(-1);
302
	int err = 0;
303 304 305

	ptr->evlist = evlist;
	ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
306

307 308
	if (!record_opts__no_switch_events(opts) &&
	    perf_can_record_switch_events())
309
		opts->record_switch_events = true;
310 311

	evlist__for_each_entry(evlist, evsel) {
312
		if (evsel->core.attr.type == cs_etm_pmu->type) {
313 314 315 316 317
			if (cs_etm_evsel) {
				pr_err("There may be only one %s event\n",
				       CORESIGHT_ETM_PMU_NAME);
				return -EINVAL;
			}
318 319
			evsel->core.attr.freq = 0;
			evsel->core.attr.sample_period = 1;
320 321 322 323 324 325 326 327 328
			cs_etm_evsel = evsel;
			opts->full_auxtrace = true;
		}
	}

	/* no need to continue if at least one event of interest was found */
	if (!cs_etm_evsel)
		return 0;

329 330 331 332
	ret = cs_etm_set_sink_attr(cs_etm_pmu, cs_etm_evsel);
	if (ret)
		return ret;

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
	if (opts->use_clockid) {
		pr_err("Cannot use clockid (-k option) with %s\n",
		       CORESIGHT_ETM_PMU_NAME);
		return -EINVAL;
	}

	/* we are in snapshot mode */
	if (opts->auxtrace_snapshot_mode) {
		/*
		 * No size were given to '-S' or '-m,', so go with
		 * the default
		 */
		if (!opts->auxtrace_snapshot_size &&
		    !opts->auxtrace_mmap_pages) {
			if (privileged) {
				opts->auxtrace_mmap_pages = MiB(4) / page_size;
			} else {
				opts->auxtrace_mmap_pages =
							KiB(128) / page_size;
				if (opts->mmap_pages == UINT_MAX)
					opts->mmap_pages = KiB(256) / page_size;
			}
		} else if (!opts->auxtrace_mmap_pages && !privileged &&
						opts->mmap_pages == UINT_MAX) {
			opts->mmap_pages = KiB(256) / page_size;
		}

		/*
		 * '-m,xyz' was specified but no snapshot size, so make the
		 * snapshot size as big as the auxtrace mmap area.
		 */
		if (!opts->auxtrace_snapshot_size) {
			opts->auxtrace_snapshot_size =
				opts->auxtrace_mmap_pages * (size_t)page_size;
		}

		/*
		 * -Sxyz was specified but no auxtrace mmap area, so make the
		 * auxtrace mmap area big enough to fit the requested snapshot
		 * size.
		 */
		if (!opts->auxtrace_mmap_pages) {
			size_t sz = opts->auxtrace_snapshot_size;

			sz = round_up(sz, page_size) / page_size;
			opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
		}

381
		/* Snapshot size can't be bigger than the auxtrace area */
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
		if (opts->auxtrace_snapshot_size >
				opts->auxtrace_mmap_pages * (size_t)page_size) {
			pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
			       opts->auxtrace_snapshot_size,
			       opts->auxtrace_mmap_pages * (size_t)page_size);
			return -EINVAL;
		}

		/* Something went wrong somewhere - this shouldn't happen */
		if (!opts->auxtrace_snapshot_size ||
		    !opts->auxtrace_mmap_pages) {
			pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
			return -EINVAL;
		}
	}

	/* We are in full trace mode but '-m,xyz' wasn't specified */
	if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
		if (privileged) {
			opts->auxtrace_mmap_pages = MiB(4) / page_size;
		} else {
			opts->auxtrace_mmap_pages = KiB(128) / page_size;
			if (opts->mmap_pages == UINT_MAX)
				opts->mmap_pages = KiB(256) / page_size;
		}

	}

	/* Validate auxtrace_mmap_pages provided by user */
	if (opts->auxtrace_mmap_pages) {
		unsigned int max_page = (KiB(128) / page_size);
		size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;

		if (!privileged &&
		    opts->auxtrace_mmap_pages > max_page) {
			opts->auxtrace_mmap_pages = max_page;
			pr_err("auxtrace too big, truncating to %d\n",
			       max_page);
		}

		if (!is_power_of_2(sz)) {
			pr_err("Invalid mmap size for %s: must be a power of 2\n",
			       CORESIGHT_ETM_PMU_NAME);
			return -EINVAL;
		}
	}

	if (opts->auxtrace_snapshot_mode)
		pr_debug2("%s snapshot size: %zu\n", CORESIGHT_ETM_PMU_NAME,
			  opts->auxtrace_snapshot_size);

433 434 435 436
	/*
	 * To obtain the auxtrace buffer file descriptor, the auxtrace
	 * event must come first.
	 */
437
	evlist__to_front(evlist, cs_etm_evsel);
438 439 440

	/*
	 * In the case of per-cpu mmaps, we need the CPU on the
441 442
	 * AUX event.  We also need the contextID in order to be notified
	 * when a context switch happened.
443
	 */
444
	if (!perf_cpu_map__empty(cpus)) {
445
		evsel__set_sample_bit(cs_etm_evsel, CPU);
446

447
		err = cs_etm_set_option(itr, cs_etm_evsel,
448
					BIT(ETM_OPT_CTXTID) | BIT(ETM_OPT_TS));
449 450
		if (err)
			goto out;
451 452
	}

453 454
	/* Add dummy event to keep tracking */
	if (opts->full_auxtrace) {
455
		struct evsel *tracking_evsel;
456 457 458

		err = parse_events(evlist, "dummy:u", NULL);
		if (err)
459
			goto out;
460

461
		tracking_evsel = evlist__last(evlist);
462
		evlist__set_tracking_event(evlist, tracking_evsel);
463

464 465
		tracking_evsel->core.attr.freq = 0;
		tracking_evsel->core.attr.sample_period = 1;
466 467

		/* In per-cpu case, always need the time of mmap events etc */
468
		if (!perf_cpu_map__empty(cpus))
469
			evsel__set_sample_bit(tracking_evsel, TIME);
470 471
	}

472 473
out:
	return err;
474 475 476 477 478 479 480 481
}

static u64 cs_etm_get_config(struct auxtrace_record *itr)
{
	u64 config = 0;
	struct cs_etm_recording *ptr =
			container_of(itr, struct cs_etm_recording, itr);
	struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
482
	struct evlist *evlist = ptr->evlist;
483
	struct evsel *evsel;
484 485

	evlist__for_each_entry(evlist, evsel) {
486
		if (evsel->core.attr.type == cs_etm_pmu->type) {
487 488 489 490 491 492 493 494
			/*
			 * Variable perf_event_attr::config is assigned to
			 * ETMv3/PTM.  The bit fields have been made to match
			 * the ETMv3.5 ETRMCR register specification.  See the
			 * PMU_FORMAT_ATTR() declarations in
			 * drivers/hwtracing/coresight/coresight-perf.c for
			 * details.
			 */
495
			config = evsel->core.attr.config;
496 497 498 499 500 501 502
			break;
		}
	}

	return config;
}

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
#ifndef BIT
#define BIT(N) (1UL << (N))
#endif

static u64 cs_etmv4_get_config(struct auxtrace_record *itr)
{
	u64 config = 0;
	u64 config_opts = 0;

	/*
	 * The perf event variable config bits represent both
	 * the command line options and register programming
	 * bits in ETMv3/PTM. For ETMv4 we must remap options
	 * to real bits
	 */
	config_opts = cs_etm_get_config(itr);
	if (config_opts & BIT(ETM_OPT_CYCACC))
		config |= BIT(ETM4_CFG_BIT_CYCACC);
521 522
	if (config_opts & BIT(ETM_OPT_CTXTID))
		config |= BIT(ETM4_CFG_BIT_CTXTID);
523 524 525 526
	if (config_opts & BIT(ETM_OPT_TS))
		config |= BIT(ETM4_CFG_BIT_TS);
	if (config_opts & BIT(ETM_OPT_RETSTK))
		config |= BIT(ETM4_CFG_BIT_RETSTK);
527 528 529
	if (config_opts & BIT(ETM_OPT_CTXTID2))
		config |= BIT(ETM4_CFG_BIT_VMID) |
			  BIT(ETM4_CFG_BIT_VMID_OPT);
530 531 532
	return config;
}

533 534
static size_t
cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
535
		      struct evlist *evlist __maybe_unused)
536 537
{
	int i;
538
	int etmv3 = 0, etmv4 = 0, ete = 0;
539
	struct perf_cpu_map *event_cpus = evlist->core.cpus;
540
	struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
541 542

	/* cpu map is not empty, we have specific CPUs to work with */
543
	if (!perf_cpu_map__empty(event_cpus)) {
544 545 546 547 548
		for (i = 0; i < cpu__max_cpu(); i++) {
			if (!cpu_map__has(event_cpus, i) ||
			    !cpu_map__has(online_cpus, i))
				continue;

549 550 551
			if (cs_etm_is_ete(itr, i))
				ete++;
			else if (cs_etm_is_etmv4(itr, i))
552 553 554 555 556 557 558
				etmv4++;
			else
				etmv3++;
		}
	} else {
		/* get configuration for all CPUs in the system */
		for (i = 0; i < cpu__max_cpu(); i++) {
559 560 561
			if (!cpu_map__has(online_cpus, i))
				continue;

562 563 564
			if (cs_etm_is_ete(itr, i))
				ete++;
			else if (cs_etm_is_etmv4(itr, i))
565 566 567 568 569 570
				etmv4++;
			else
				etmv3++;
		}
	}

571
	perf_cpu_map__put(online_cpus);
572

573
	return (CS_ETM_HEADER_SIZE +
574
	       (ete   * CS_ETE_PRIV_SIZE) +
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
	       (etmv4 * CS_ETMV4_PRIV_SIZE) +
	       (etmv3 * CS_ETMV3_PRIV_SIZE));
}

static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu)
{
	bool ret = false;
	char path[PATH_MAX];
	int scan;
	unsigned int val;
	struct cs_etm_recording *ptr =
			container_of(itr, struct cs_etm_recording, itr);
	struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;

	/* Take any of the RO files for ETMv4 and see if it present */
	snprintf(path, PATH_MAX, "cpu%d/%s",
		 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
	scan = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val);

	/* The file was read successfully, we have a winner */
	if (scan == 1)
		ret = true;

	return ret;
}

static int cs_etm_get_ro(struct perf_pmu *pmu, int cpu, const char *path)
{
	char pmu_path[PATH_MAX];
	int scan;
	unsigned int val = 0;

	/* Get RO metadata from sysfs */
	snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path);

	scan = perf_pmu__scan_file(pmu, pmu_path, "%x", &val);
	if (scan != 1)
		pr_err("%s: error reading: %s\n", __func__, pmu_path);

	return val;
}

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
#define TRCDEVARCH_ARCHPART_SHIFT 0
#define TRCDEVARCH_ARCHPART_MASK  GENMASK(11, 0)
#define TRCDEVARCH_ARCHPART(x)    (((x) & TRCDEVARCH_ARCHPART_MASK) >> TRCDEVARCH_ARCHPART_SHIFT)

#define TRCDEVARCH_ARCHVER_SHIFT 12
#define TRCDEVARCH_ARCHVER_MASK  GENMASK(15, 12)
#define TRCDEVARCH_ARCHVER(x)    (((x) & TRCDEVARCH_ARCHVER_MASK) >> TRCDEVARCH_ARCHVER_SHIFT)

static bool cs_etm_is_ete(struct auxtrace_record *itr, int cpu)
{
	struct cs_etm_recording *ptr = container_of(itr, struct cs_etm_recording, itr);
	struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
	int trcdevarch = cs_etm_get_ro(cs_etm_pmu, cpu, metadata_etmv4_ro[CS_ETE_TRCDEVARCH]);

	/*
	 * ETE if ARCHVER is 5 (ARCHVER is 4 for ETM) and ARCHPART is 0xA13.
	 * See ETM_DEVARCH_ETE_ARCH in coresight-etm4x.h
	 */
	return TRCDEVARCH_ARCHVER(trcdevarch) == 5 && TRCDEVARCH_ARCHPART(trcdevarch) == 0xA13;
}

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
static void cs_etm_save_etmv4_header(__u64 data[], struct auxtrace_record *itr, int cpu)
{
	struct cs_etm_recording *ptr = container_of(itr, struct cs_etm_recording, itr);
	struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;

	/* Get trace configuration register */
	data[CS_ETMV4_TRCCONFIGR] = cs_etmv4_get_config(itr);
	/* Get traceID from the framework */
	data[CS_ETMV4_TRCTRACEIDR] = coresight_get_trace_id(cpu);
	/* Get read-only information from sysFS */
	data[CS_ETMV4_TRCIDR0] = cs_etm_get_ro(cs_etm_pmu, cpu,
					       metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
	data[CS_ETMV4_TRCIDR1] = cs_etm_get_ro(cs_etm_pmu, cpu,
					       metadata_etmv4_ro[CS_ETMV4_TRCIDR1]);
	data[CS_ETMV4_TRCIDR2] = cs_etm_get_ro(cs_etm_pmu, cpu,
					       metadata_etmv4_ro[CS_ETMV4_TRCIDR2]);
	data[CS_ETMV4_TRCIDR8] = cs_etm_get_ro(cs_etm_pmu, cpu,
					       metadata_etmv4_ro[CS_ETMV4_TRCIDR8]);
	data[CS_ETMV4_TRCAUTHSTATUS] = cs_etm_get_ro(cs_etm_pmu, cpu,
						     metadata_etmv4_ro[CS_ETMV4_TRCAUTHSTATUS]);
}

660 661
static void cs_etm_get_metadata(int cpu, u32 *offset,
				struct auxtrace_record *itr,
662
				struct perf_record_auxtrace_info *info)
663
{
664
	u32 increment, nr_trc_params;
665 666 667 668 669 670
	u64 magic;
	struct cs_etm_recording *ptr =
			container_of(itr, struct cs_etm_recording, itr);
	struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;

	/* first see what kind of tracer this cpu is affined to */
671 672 673 674 675 676 677 678 679 680 681 682
	if (cs_etm_is_ete(itr, cpu)) {
		magic = __perf_cs_ete_magic;
		/* ETE uses the same registers as ETMv4 plus TRCDEVARCH */
		cs_etm_save_etmv4_header(&info->priv[*offset], itr, cpu);
		info->priv[*offset + CS_ETE_TRCDEVARCH] =
			cs_etm_get_ro(cs_etm_pmu, cpu,
				      metadata_etmv4_ro[CS_ETE_TRCDEVARCH]);

		/* How much space was used */
		increment = CS_ETE_PRIV_MAX;
		nr_trc_params = CS_ETE_PRIV_MAX - CS_ETM_COMMON_BLK_MAX_V1;
	} else if (cs_etm_is_etmv4(itr, cpu)) {
683
		magic = __perf_cs_etmv4_magic;
684
		cs_etm_save_etmv4_header(&info->priv[*offset], itr, cpu);
685 686 687

		/* How much space was used */
		increment = CS_ETMV4_PRIV_MAX;
688
		nr_trc_params = CS_ETMV4_PRIV_MAX - CS_ETMV4_TRCCONFIGR;
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
	} else {
		magic = __perf_cs_etmv3_magic;
		/* Get configuration register */
		info->priv[*offset + CS_ETM_ETMCR] = cs_etm_get_config(itr);
		/* Get traceID from the framework */
		info->priv[*offset + CS_ETM_ETMTRACEIDR] =
						coresight_get_trace_id(cpu);
		/* Get read-only information from sysFS */
		info->priv[*offset + CS_ETM_ETMCCER] =
			cs_etm_get_ro(cs_etm_pmu, cpu,
				      metadata_etmv3_ro[CS_ETM_ETMCCER]);
		info->priv[*offset + CS_ETM_ETMIDR] =
			cs_etm_get_ro(cs_etm_pmu, cpu,
				      metadata_etmv3_ro[CS_ETM_ETMIDR]);

		/* How much space was used */
		increment = CS_ETM_PRIV_MAX;
706
		nr_trc_params = CS_ETM_PRIV_MAX - CS_ETM_ETMCR;
707 708 709 710 711
	}

	/* Build generic header portion */
	info->priv[*offset + CS_ETM_MAGIC] = magic;
	info->priv[*offset + CS_ETM_CPU] = cpu;
712
	info->priv[*offset + CS_ETM_NR_TRC_PARAMS] = nr_trc_params;
713 714 715 716 717 718
	/* Where the next CPU entry should start from */
	*offset += increment;
}

static int cs_etm_info_fill(struct auxtrace_record *itr,
			    struct perf_session *session,
719
			    struct perf_record_auxtrace_info *info,
720 721 722 723 724
			    size_t priv_size)
{
	int i;
	u32 offset;
	u64 nr_cpu, type;
725
	struct perf_cpu_map *cpu_map;
726
	struct perf_cpu_map *event_cpus = session->evlist->core.cpus;
727
	struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
728 729 730 731 732 733 734
	struct cs_etm_recording *ptr =
			container_of(itr, struct cs_etm_recording, itr);
	struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;

	if (priv_size != cs_etm_info_priv_size(itr, session->evlist))
		return -EINVAL;

735
	if (!session->evlist->core.nr_mmaps)
736 737
		return -EINVAL;

738
	/* If the cpu_map is empty all online CPUs are involved */
739
	if (perf_cpu_map__empty(event_cpus)) {
740 741 742
		cpu_map = online_cpus;
	} else {
		/* Make sure all specified CPUs are online */
743
		for (i = 0; i < perf_cpu_map__nr(event_cpus); i++) {
744 745 746 747 748 749 750 751
			if (cpu_map__has(event_cpus, i) &&
			    !cpu_map__has(online_cpus, i))
				return -EINVAL;
		}

		cpu_map = event_cpus;
	}

752
	nr_cpu = perf_cpu_map__nr(cpu_map);
753 754 755 756 757
	/* Get PMU type as dynamically assigned by the core */
	type = cs_etm_pmu->type;

	/* First fill out the session header */
	info->type = PERF_AUXTRACE_CS_ETM;
758
	info->priv[CS_HEADER_VERSION] = CS_HEADER_CURRENT_VERSION;
759 760 761 762 763 764
	info->priv[CS_PMU_TYPE_CPUS] = type << 32;
	info->priv[CS_PMU_TYPE_CPUS] |= nr_cpu;
	info->priv[CS_ETM_SNAPSHOT] = ptr->snapshot_mode;

	offset = CS_ETM_SNAPSHOT + 1;

765 766
	for (i = 0; i < cpu__max_cpu() && offset < priv_size; i++)
		if (cpu_map__has(cpu_map, i))
767
			cs_etm_get_metadata(i, &offset, itr, info);
768

769
	perf_cpu_map__put(online_cpus);
770 771 772 773 774 775 776 777

	return 0;
}

static int cs_etm_snapshot_start(struct auxtrace_record *itr)
{
	struct cs_etm_recording *ptr =
			container_of(itr, struct cs_etm_recording, itr);
778
	struct evsel *evsel;
779 780

	evlist__for_each_entry(ptr->evlist, evsel) {
781
		if (evsel->core.attr.type == ptr->cs_etm_pmu->type)
782
			return evsel__disable(evsel);
783 784 785 786 787 788 789 790
	}
	return -EINVAL;
}

static int cs_etm_snapshot_finish(struct auxtrace_record *itr)
{
	struct cs_etm_recording *ptr =
			container_of(itr, struct cs_etm_recording, itr);
791
	struct evsel *evsel;
792 793

	evlist__for_each_entry(ptr->evlist, evsel) {
794
		if (evsel->core.attr.type == ptr->cs_etm_pmu->type)
795
			return evsel__enable(evsel);
796 797 798 799 800 801 802 803 804 805 806 807 808 809
	}
	return -EINVAL;
}

static u64 cs_etm_reference(struct auxtrace_record *itr __maybe_unused)
{
	return (((u64) rand() <<  0) & 0x00000000FFFFFFFFull) |
		(((u64) rand() << 32) & 0xFFFFFFFF00000000ull);
}

static void cs_etm_recording_free(struct auxtrace_record *itr)
{
	struct cs_etm_recording *ptr =
			container_of(itr, struct cs_etm_recording, itr);
810

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
	free(ptr);
}

struct auxtrace_record *cs_etm_record_init(int *err)
{
	struct perf_pmu *cs_etm_pmu;
	struct cs_etm_recording *ptr;

	cs_etm_pmu = perf_pmu__find(CORESIGHT_ETM_PMU_NAME);

	if (!cs_etm_pmu) {
		*err = -EINVAL;
		goto out;
	}

	ptr = zalloc(sizeof(struct cs_etm_recording));
	if (!ptr) {
		*err = -ENOMEM;
		goto out;
	}

	ptr->cs_etm_pmu			= cs_etm_pmu;
833
	ptr->itr.pmu			= cs_etm_pmu;
834 835 836 837 838 839 840 841
	ptr->itr.parse_snapshot_options	= cs_etm_parse_snapshot_options;
	ptr->itr.recording_options	= cs_etm_recording_options;
	ptr->itr.info_priv_size		= cs_etm_info_priv_size;
	ptr->itr.info_fill		= cs_etm_info_fill;
	ptr->itr.snapshot_start		= cs_etm_snapshot_start;
	ptr->itr.snapshot_finish	= cs_etm_snapshot_finish;
	ptr->itr.reference		= cs_etm_reference;
	ptr->itr.free			= cs_etm_recording_free;
842
	ptr->itr.read_finish		= auxtrace_record__read_finish;
843 844 845 846 847 848

	*err = 0;
	return &ptr->itr;
out:
	return NULL;
}