2 * Copyright(C) 2015 Linaro Limited. All rights reserved.
3 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
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.
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
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/>.
18 #include <api/fs/fs.h>
19 #include <linux/bitops.h>
20 #include <linux/compiler.h>
21 #include <linux/coresight-pmu.h>
22 #include <linux/kernel.h>
23 #include <linux/log2.h>
24 #include <linux/types.h>
27 #include "../../perf.h"
28 #include "../../util/auxtrace.h"
29 #include "../../util/cpumap.h"
30 #include "../../util/evlist.h"
31 #include "../../util/evsel.h"
32 #include "../../util/pmu.h"
33 #include "../../util/thread_map.h"
34 #include "../../util/cs-etm.h"
39 #define ENABLE_SINK_MAX 128
40 #define CS_BUS_DEVICE_PATH "/bus/coresight/devices/"
42 struct cs_etm_recording {
43 struct auxtrace_record itr;
44 struct perf_pmu *cs_etm_pmu;
45 struct perf_evlist *evlist;
52 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu);
54 static int cs_etm_parse_snapshot_options(struct auxtrace_record *itr,
55 struct record_opts *opts,
58 struct cs_etm_recording *ptr =
59 container_of(itr, struct cs_etm_recording, itr);
60 unsigned long long snapshot_size = 0;
64 snapshot_size = strtoull(str, &endptr, 0);
65 if (*endptr || snapshot_size > SIZE_MAX)
69 opts->auxtrace_snapshot_mode = true;
70 opts->auxtrace_snapshot_size = snapshot_size;
71 ptr->snapshot_size = snapshot_size;
76 static int cs_etm_recording_options(struct auxtrace_record *itr,
77 struct perf_evlist *evlist,
78 struct record_opts *opts)
80 struct cs_etm_recording *ptr =
81 container_of(itr, struct cs_etm_recording, itr);
82 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
83 struct perf_evsel *evsel, *cs_etm_evsel = NULL;
84 const struct cpu_map *cpus = evlist->cpus;
85 bool privileged = (geteuid() == 0 || perf_event_paranoid() < 0);
88 ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
90 evlist__for_each_entry(evlist, evsel) {
91 if (evsel->attr.type == cs_etm_pmu->type) {
93 pr_err("There may be only one %s event\n",
94 CORESIGHT_ETM_PMU_NAME);
98 evsel->attr.sample_period = 1;
100 opts->full_auxtrace = true;
104 /* no need to continue if at least one event of interest was found */
108 if (opts->use_clockid) {
109 pr_err("Cannot use clockid (-k option) with %s\n",
110 CORESIGHT_ETM_PMU_NAME);
114 /* we are in snapshot mode */
115 if (opts->auxtrace_snapshot_mode) {
117 * No size were given to '-S' or '-m,', so go with
120 if (!opts->auxtrace_snapshot_size &&
121 !opts->auxtrace_mmap_pages) {
123 opts->auxtrace_mmap_pages = MiB(4) / page_size;
125 opts->auxtrace_mmap_pages =
126 KiB(128) / page_size;
127 if (opts->mmap_pages == UINT_MAX)
128 opts->mmap_pages = KiB(256) / page_size;
130 } else if (!opts->auxtrace_mmap_pages && !privileged &&
131 opts->mmap_pages == UINT_MAX) {
132 opts->mmap_pages = KiB(256) / page_size;
136 * '-m,xyz' was specified but no snapshot size, so make the
137 * snapshot size as big as the auxtrace mmap area.
139 if (!opts->auxtrace_snapshot_size) {
140 opts->auxtrace_snapshot_size =
141 opts->auxtrace_mmap_pages * (size_t)page_size;
145 * -Sxyz was specified but no auxtrace mmap area, so make the
146 * auxtrace mmap area big enough to fit the requested snapshot
149 if (!opts->auxtrace_mmap_pages) {
150 size_t sz = opts->auxtrace_snapshot_size;
152 sz = round_up(sz, page_size) / page_size;
153 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
156 /* Snapshost size can't be bigger than the auxtrace area */
157 if (opts->auxtrace_snapshot_size >
158 opts->auxtrace_mmap_pages * (size_t)page_size) {
159 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
160 opts->auxtrace_snapshot_size,
161 opts->auxtrace_mmap_pages * (size_t)page_size);
165 /* Something went wrong somewhere - this shouldn't happen */
166 if (!opts->auxtrace_snapshot_size ||
167 !opts->auxtrace_mmap_pages) {
168 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
173 /* We are in full trace mode but '-m,xyz' wasn't specified */
174 if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
176 opts->auxtrace_mmap_pages = MiB(4) / page_size;
178 opts->auxtrace_mmap_pages = KiB(128) / page_size;
179 if (opts->mmap_pages == UINT_MAX)
180 opts->mmap_pages = KiB(256) / page_size;
185 /* Validate auxtrace_mmap_pages provided by user */
186 if (opts->auxtrace_mmap_pages) {
187 unsigned int max_page = (KiB(128) / page_size);
188 size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
191 opts->auxtrace_mmap_pages > max_page) {
192 opts->auxtrace_mmap_pages = max_page;
193 pr_err("auxtrace too big, truncating to %d\n",
197 if (!is_power_of_2(sz)) {
198 pr_err("Invalid mmap size for %s: must be a power of 2\n",
199 CORESIGHT_ETM_PMU_NAME);
204 if (opts->auxtrace_snapshot_mode)
205 pr_debug2("%s snapshot size: %zu\n", CORESIGHT_ETM_PMU_NAME,
206 opts->auxtrace_snapshot_size);
209 * To obtain the auxtrace buffer file descriptor, the auxtrace
210 * event must come first.
212 perf_evlist__to_front(evlist, cs_etm_evsel);
215 * In the case of per-cpu mmaps, we need the CPU on the
218 if (!cpu_map__empty(cpus))
219 perf_evsel__set_sample_bit(cs_etm_evsel, CPU);
221 /* Add dummy event to keep tracking */
222 if (opts->full_auxtrace) {
223 struct perf_evsel *tracking_evsel;
226 err = parse_events(evlist, "dummy:u", NULL);
230 tracking_evsel = perf_evlist__last(evlist);
231 perf_evlist__set_tracking_event(evlist, tracking_evsel);
233 tracking_evsel->attr.freq = 0;
234 tracking_evsel->attr.sample_period = 1;
236 /* In per-cpu case, always need the time of mmap events etc */
237 if (!cpu_map__empty(cpus))
238 perf_evsel__set_sample_bit(tracking_evsel, TIME);
244 static u64 cs_etm_get_config(struct auxtrace_record *itr)
247 struct cs_etm_recording *ptr =
248 container_of(itr, struct cs_etm_recording, itr);
249 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
250 struct perf_evlist *evlist = ptr->evlist;
251 struct perf_evsel *evsel;
253 evlist__for_each_entry(evlist, evsel) {
254 if (evsel->attr.type == cs_etm_pmu->type) {
256 * Variable perf_event_attr::config is assigned to
257 * ETMv3/PTM. The bit fields have been made to match
258 * the ETMv3.5 ETRMCR register specification. See the
259 * PMU_FORMAT_ATTR() declarations in
260 * drivers/hwtracing/coresight/coresight-perf.c for
263 config = evsel->attr.config;
272 #define BIT(N) (1UL << (N))
275 static u64 cs_etmv4_get_config(struct auxtrace_record *itr)
281 * The perf event variable config bits represent both
282 * the command line options and register programming
283 * bits in ETMv3/PTM. For ETMv4 we must remap options
286 config_opts = cs_etm_get_config(itr);
287 if (config_opts & BIT(ETM_OPT_CYCACC))
288 config |= BIT(ETM4_CFG_BIT_CYCACC);
289 if (config_opts & BIT(ETM_OPT_TS))
290 config |= BIT(ETM4_CFG_BIT_TS);
291 if (config_opts & BIT(ETM_OPT_RETSTK))
292 config |= BIT(ETM4_CFG_BIT_RETSTK);
298 cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
299 struct perf_evlist *evlist __maybe_unused)
302 int etmv3 = 0, etmv4 = 0;
303 const struct cpu_map *cpus = evlist->cpus;
305 /* cpu map is not empty, we have specific CPUs to work with */
306 if (!cpu_map__empty(cpus)) {
307 for (i = 0; i < cpu_map__nr(cpus); i++) {
308 if (cs_etm_is_etmv4(itr, cpus->map[i]))
314 /* get configuration for all CPUs in the system */
315 for (i = 0; i < cpu__max_cpu(); i++) {
316 if (cs_etm_is_etmv4(itr, i))
323 return (CS_ETM_HEADER_SIZE +
324 (etmv4 * CS_ETMV4_PRIV_SIZE) +
325 (etmv3 * CS_ETMV3_PRIV_SIZE));
328 static const char *metadata_etmv3_ro[CS_ETM_PRIV_MAX] = {
329 [CS_ETM_ETMCCER] = "mgmt/etmccer",
330 [CS_ETM_ETMIDR] = "mgmt/etmidr",
333 static const char *metadata_etmv4_ro[CS_ETMV4_PRIV_MAX] = {
334 [CS_ETMV4_TRCIDR0] = "trcidr/trcidr0",
335 [CS_ETMV4_TRCIDR1] = "trcidr/trcidr1",
336 [CS_ETMV4_TRCIDR2] = "trcidr/trcidr2",
337 [CS_ETMV4_TRCIDR8] = "trcidr/trcidr8",
338 [CS_ETMV4_TRCAUTHSTATUS] = "mgmt/trcauthstatus",
341 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu)
347 struct cs_etm_recording *ptr =
348 container_of(itr, struct cs_etm_recording, itr);
349 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
351 /* Take any of the RO files for ETMv4 and see if it present */
352 snprintf(path, PATH_MAX, "cpu%d/%s",
353 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
354 scan = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val);
356 /* The file was read successfully, we have a winner */
363 static int cs_etm_get_ro(struct perf_pmu *pmu, int cpu, const char *path)
365 char pmu_path[PATH_MAX];
367 unsigned int val = 0;
369 /* Get RO metadata from sysfs */
370 snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path);
372 scan = perf_pmu__scan_file(pmu, pmu_path, "%x", &val);
374 pr_err("%s: error reading: %s\n", __func__, pmu_path);
379 static void cs_etm_get_metadata(int cpu, u32 *offset,
380 struct auxtrace_record *itr,
381 struct auxtrace_info_event *info)
385 struct cs_etm_recording *ptr =
386 container_of(itr, struct cs_etm_recording, itr);
387 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
389 /* first see what kind of tracer this cpu is affined to */
390 if (cs_etm_is_etmv4(itr, cpu)) {
391 magic = __perf_cs_etmv4_magic;
392 /* Get trace configuration register */
393 info->priv[*offset + CS_ETMV4_TRCCONFIGR] =
394 cs_etmv4_get_config(itr);
395 /* Get traceID from the framework */
396 info->priv[*offset + CS_ETMV4_TRCTRACEIDR] =
397 coresight_get_trace_id(cpu);
398 /* Get read-only information from sysFS */
399 info->priv[*offset + CS_ETMV4_TRCIDR0] =
400 cs_etm_get_ro(cs_etm_pmu, cpu,
401 metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
402 info->priv[*offset + CS_ETMV4_TRCIDR1] =
403 cs_etm_get_ro(cs_etm_pmu, cpu,
404 metadata_etmv4_ro[CS_ETMV4_TRCIDR1]);
405 info->priv[*offset + CS_ETMV4_TRCIDR2] =
406 cs_etm_get_ro(cs_etm_pmu, cpu,
407 metadata_etmv4_ro[CS_ETMV4_TRCIDR2]);
408 info->priv[*offset + CS_ETMV4_TRCIDR8] =
409 cs_etm_get_ro(cs_etm_pmu, cpu,
410 metadata_etmv4_ro[CS_ETMV4_TRCIDR8]);
411 info->priv[*offset + CS_ETMV4_TRCAUTHSTATUS] =
412 cs_etm_get_ro(cs_etm_pmu, cpu,
414 [CS_ETMV4_TRCAUTHSTATUS]);
416 /* How much space was used */
417 increment = CS_ETMV4_PRIV_MAX;
419 magic = __perf_cs_etmv3_magic;
420 /* Get configuration register */
421 info->priv[*offset + CS_ETM_ETMCR] = cs_etm_get_config(itr);
422 /* Get traceID from the framework */
423 info->priv[*offset + CS_ETM_ETMTRACEIDR] =
424 coresight_get_trace_id(cpu);
425 /* Get read-only information from sysFS */
426 info->priv[*offset + CS_ETM_ETMCCER] =
427 cs_etm_get_ro(cs_etm_pmu, cpu,
428 metadata_etmv3_ro[CS_ETM_ETMCCER]);
429 info->priv[*offset + CS_ETM_ETMIDR] =
430 cs_etm_get_ro(cs_etm_pmu, cpu,
431 metadata_etmv3_ro[CS_ETM_ETMIDR]);
433 /* How much space was used */
434 increment = CS_ETM_PRIV_MAX;
437 /* Build generic header portion */
438 info->priv[*offset + CS_ETM_MAGIC] = magic;
439 info->priv[*offset + CS_ETM_CPU] = cpu;
440 /* Where the next CPU entry should start from */
441 *offset += increment;
444 static int cs_etm_info_fill(struct auxtrace_record *itr,
445 struct perf_session *session,
446 struct auxtrace_info_event *info,
452 const struct cpu_map *cpus = session->evlist->cpus;
453 struct cs_etm_recording *ptr =
454 container_of(itr, struct cs_etm_recording, itr);
455 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
457 if (priv_size != cs_etm_info_priv_size(itr, session->evlist))
460 if (!session->evlist->nr_mmaps)
463 /* If the cpu_map is empty all CPUs are involved */
464 nr_cpu = cpu_map__empty(cpus) ? cpu__max_cpu() : cpu_map__nr(cpus);
465 /* Get PMU type as dynamically assigned by the core */
466 type = cs_etm_pmu->type;
468 /* First fill out the session header */
469 info->type = PERF_AUXTRACE_CS_ETM;
470 info->priv[CS_HEADER_VERSION_0] = 0;
471 info->priv[CS_PMU_TYPE_CPUS] = type << 32;
472 info->priv[CS_PMU_TYPE_CPUS] |= nr_cpu;
473 info->priv[CS_ETM_SNAPSHOT] = ptr->snapshot_mode;
475 offset = CS_ETM_SNAPSHOT + 1;
477 /* cpu map is not empty, we have specific CPUs to work with */
478 if (!cpu_map__empty(cpus)) {
479 for (i = 0; i < cpu_map__nr(cpus) && offset < priv_size; i++)
480 cs_etm_get_metadata(cpus->map[i], &offset, itr, info);
482 /* get configuration for all CPUs in the system */
483 for (i = 0; i < cpu__max_cpu(); i++)
484 cs_etm_get_metadata(i, &offset, itr, info);
490 static int cs_etm_alloc_wrapped_array(struct cs_etm_recording *ptr, int idx)
493 int cnt = ptr->wrapped_cnt;
495 /* Make @ptr->wrapped as big as @idx */
500 * Free'ed in cs_etm_recording_free(). Using realloc() to avoid
501 * cross compilation problems where the host's system supports
502 * reallocarray() but not the target.
504 wrapped = realloc(ptr->wrapped, cnt * sizeof(bool));
508 wrapped[cnt - 1] = false;
509 ptr->wrapped_cnt = cnt;
510 ptr->wrapped = wrapped;
515 static bool cs_etm_buffer_has_wrapped(unsigned char *buffer,
516 size_t buffer_size, u64 head)
519 u64 *buf = (u64 *)buffer;
520 size_t buf_size = buffer_size;
523 * We want to look the very last 512 byte (chosen arbitrarily) in
526 watermark = buf_size - 512;
529 * @head is continuously increasing - if its value is equal or greater
530 * than the size of the ring buffer, it has wrapped around.
532 if (head >= buffer_size)
536 * The value of @head is somewhere within the size of the ring buffer.
537 * This can be that there hasn't been enough data to fill the ring
538 * buffer yet or the trace time was so long that @head has numerically
539 * wrapped around. To find we need to check if we have data at the very
540 * end of the ring buffer. We can reliably do this because mmap'ed
541 * pages are zeroed out and there is a fresh mapping with every new
545 /* @head is less than 512 byte from the end of the ring buffer */
546 if (head > watermark)
550 * Speed things up by using 64 bit transactions (see "u64 *buf" above)
556 * If we find trace data at the end of the ring buffer, @head has
557 * been there and has numerically wrapped around at least once.
559 for (i = watermark; i < buf_size; i++)
566 static int cs_etm_find_snapshot(struct auxtrace_record *itr,
567 int idx, struct auxtrace_mmap *mm,
573 struct cs_etm_recording *ptr =
574 container_of(itr, struct cs_etm_recording, itr);
577 * Allocate memory to keep track of wrapping if this is the first
578 * time we deal with this *mm.
580 if (idx >= ptr->wrapped_cnt) {
581 err = cs_etm_alloc_wrapped_array(ptr, idx);
587 * Check to see if *head has wrapped around. If it hasn't only the
588 * amount of data between *head and *old is snapshot'ed to avoid
589 * bloating the perf.data file with zeros. But as soon as *head has
590 * wrapped around the entire size of the AUX ring buffer it taken.
592 wrapped = ptr->wrapped[idx];
593 if (!wrapped && cs_etm_buffer_has_wrapped(data, mm->len, *head)) {
595 ptr->wrapped[idx] = true;
598 pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n",
599 __func__, idx, (size_t)*old, (size_t)*head, mm->len);
601 /* No wrap has occurred, we can just use *head and *old. */
606 * *head has wrapped around - adjust *head and *old to pickup the
607 * entire content of the AUX buffer.
609 if (*head >= mm->len) {
610 *old = *head - mm->len;
613 *old = *head - mm->len;
619 static int cs_etm_snapshot_start(struct auxtrace_record *itr)
621 struct cs_etm_recording *ptr =
622 container_of(itr, struct cs_etm_recording, itr);
623 struct perf_evsel *evsel;
625 evlist__for_each_entry(ptr->evlist, evsel) {
626 if (evsel->attr.type == ptr->cs_etm_pmu->type)
627 return perf_evsel__disable(evsel);
632 static int cs_etm_snapshot_finish(struct auxtrace_record *itr)
634 struct cs_etm_recording *ptr =
635 container_of(itr, struct cs_etm_recording, itr);
636 struct perf_evsel *evsel;
638 evlist__for_each_entry(ptr->evlist, evsel) {
639 if (evsel->attr.type == ptr->cs_etm_pmu->type)
640 return perf_evsel__enable(evsel);
645 static u64 cs_etm_reference(struct auxtrace_record *itr __maybe_unused)
647 return (((u64) rand() << 0) & 0x00000000FFFFFFFFull) |
648 (((u64) rand() << 32) & 0xFFFFFFFF00000000ull);
651 static void cs_etm_recording_free(struct auxtrace_record *itr)
653 struct cs_etm_recording *ptr =
654 container_of(itr, struct cs_etm_recording, itr);
656 zfree(&ptr->wrapped);
660 static int cs_etm_read_finish(struct auxtrace_record *itr, int idx)
662 struct cs_etm_recording *ptr =
663 container_of(itr, struct cs_etm_recording, itr);
664 struct perf_evsel *evsel;
666 evlist__for_each_entry(ptr->evlist, evsel) {
667 if (evsel->attr.type == ptr->cs_etm_pmu->type)
668 return perf_evlist__enable_event_idx(ptr->evlist,
675 struct auxtrace_record *cs_etm_record_init(int *err)
677 struct perf_pmu *cs_etm_pmu;
678 struct cs_etm_recording *ptr;
680 cs_etm_pmu = perf_pmu__find(CORESIGHT_ETM_PMU_NAME);
687 ptr = zalloc(sizeof(struct cs_etm_recording));
693 ptr->cs_etm_pmu = cs_etm_pmu;
694 ptr->itr.parse_snapshot_options = cs_etm_parse_snapshot_options;
695 ptr->itr.recording_options = cs_etm_recording_options;
696 ptr->itr.info_priv_size = cs_etm_info_priv_size;
697 ptr->itr.info_fill = cs_etm_info_fill;
698 ptr->itr.find_snapshot = cs_etm_find_snapshot;
699 ptr->itr.snapshot_start = cs_etm_snapshot_start;
700 ptr->itr.snapshot_finish = cs_etm_snapshot_finish;
701 ptr->itr.reference = cs_etm_reference;
702 ptr->itr.free = cs_etm_recording_free;
703 ptr->itr.read_finish = cs_etm_read_finish;
711 static FILE *cs_device__open_file(const char *name)
717 sysfs = sysfs__mountpoint();
721 snprintf(path, PATH_MAX,
722 "%s" CS_BUS_DEVICE_PATH "%s", sysfs, name);
724 if (stat(path, &st) < 0)
727 return fopen(path, "w");
731 static int __printf(2, 3) cs_device__print_file(const char *name, const char *fmt, ...)
738 file = cs_device__open_file(name);
740 ret = vfprintf(file, fmt, args);
747 int cs_etm_set_drv_config(struct perf_evsel_config_term *term)
750 char enable_sink[ENABLE_SINK_MAX];
752 snprintf(enable_sink, ENABLE_SINK_MAX, "%s/%s",
753 term->val.drv_cfg, "enable_sink");
755 ret = cs_device__print_file(enable_sink, "%d", 1);