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/coresight-pmu.h>
21 #include <linux/kernel.h>
22 #include <linux/log2.h>
23 #include <linux/types.h>
26 #include "../../perf.h"
27 #include "../../util/auxtrace.h"
28 #include "../../util/cpumap.h"
29 #include "../../util/evlist.h"
30 #include "../../util/evsel.h"
31 #include "../../util/pmu.h"
32 #include "../../util/thread_map.h"
33 #include "../../util/cs-etm.h"
37 #define ENABLE_SINK_MAX 128
38 #define CS_BUS_DEVICE_PATH "/bus/coresight/devices/"
40 struct cs_etm_recording {
41 struct auxtrace_record itr;
42 struct perf_pmu *cs_etm_pmu;
43 struct perf_evlist *evlist;
50 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu);
52 static int cs_etm_parse_snapshot_options(struct auxtrace_record *itr,
53 struct record_opts *opts,
56 struct cs_etm_recording *ptr =
57 container_of(itr, struct cs_etm_recording, itr);
58 unsigned long long snapshot_size = 0;
62 snapshot_size = strtoull(str, &endptr, 0);
63 if (*endptr || snapshot_size > SIZE_MAX)
67 opts->auxtrace_snapshot_mode = true;
68 opts->auxtrace_snapshot_size = snapshot_size;
69 ptr->snapshot_size = snapshot_size;
74 static int cs_etm_recording_options(struct auxtrace_record *itr,
75 struct perf_evlist *evlist,
76 struct record_opts *opts)
78 struct cs_etm_recording *ptr =
79 container_of(itr, struct cs_etm_recording, itr);
80 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
81 struct perf_evsel *evsel, *cs_etm_evsel = NULL;
82 const struct cpu_map *cpus = evlist->cpus;
83 bool privileged = (geteuid() == 0 || perf_event_paranoid() < 0);
86 ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
88 evlist__for_each_entry(evlist, evsel) {
89 if (evsel->attr.type == cs_etm_pmu->type) {
91 pr_err("There may be only one %s event\n",
92 CORESIGHT_ETM_PMU_NAME);
96 evsel->attr.sample_period = 1;
98 opts->full_auxtrace = true;
102 /* no need to continue if at least one event of interest was found */
106 if (opts->use_clockid) {
107 pr_err("Cannot use clockid (-k option) with %s\n",
108 CORESIGHT_ETM_PMU_NAME);
112 /* we are in snapshot mode */
113 if (opts->auxtrace_snapshot_mode) {
115 * No size were given to '-S' or '-m,', so go with
118 if (!opts->auxtrace_snapshot_size &&
119 !opts->auxtrace_mmap_pages) {
121 opts->auxtrace_mmap_pages = MiB(4) / page_size;
123 opts->auxtrace_mmap_pages =
124 KiB(128) / page_size;
125 if (opts->mmap_pages == UINT_MAX)
126 opts->mmap_pages = KiB(256) / page_size;
128 } else if (!opts->auxtrace_mmap_pages && !privileged &&
129 opts->mmap_pages == UINT_MAX) {
130 opts->mmap_pages = KiB(256) / page_size;
134 * '-m,xyz' was specified but no snapshot size, so make the
135 * snapshot size as big as the auxtrace mmap area.
137 if (!opts->auxtrace_snapshot_size) {
138 opts->auxtrace_snapshot_size =
139 opts->auxtrace_mmap_pages * (size_t)page_size;
143 * -Sxyz was specified but no auxtrace mmap area, so make the
144 * auxtrace mmap area big enough to fit the requested snapshot
147 if (!opts->auxtrace_mmap_pages) {
148 size_t sz = opts->auxtrace_snapshot_size;
150 sz = round_up(sz, page_size) / page_size;
151 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
154 /* Snapshost size can't be bigger than the auxtrace area */
155 if (opts->auxtrace_snapshot_size >
156 opts->auxtrace_mmap_pages * (size_t)page_size) {
157 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
158 opts->auxtrace_snapshot_size,
159 opts->auxtrace_mmap_pages * (size_t)page_size);
163 /* Something went wrong somewhere - this shouldn't happen */
164 if (!opts->auxtrace_snapshot_size ||
165 !opts->auxtrace_mmap_pages) {
166 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
171 /* We are in full trace mode but '-m,xyz' wasn't specified */
172 if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
174 opts->auxtrace_mmap_pages = MiB(4) / page_size;
176 opts->auxtrace_mmap_pages = KiB(128) / page_size;
177 if (opts->mmap_pages == UINT_MAX)
178 opts->mmap_pages = KiB(256) / page_size;
183 /* Validate auxtrace_mmap_pages provided by user */
184 if (opts->auxtrace_mmap_pages) {
185 unsigned int max_page = (KiB(128) / page_size);
186 size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
189 opts->auxtrace_mmap_pages > max_page) {
190 opts->auxtrace_mmap_pages = max_page;
191 pr_err("auxtrace too big, truncating to %d\n",
195 if (!is_power_of_2(sz)) {
196 pr_err("Invalid mmap size for %s: must be a power of 2\n",
197 CORESIGHT_ETM_PMU_NAME);
202 if (opts->auxtrace_snapshot_mode)
203 pr_debug2("%s snapshot size: %zu\n", CORESIGHT_ETM_PMU_NAME,
204 opts->auxtrace_snapshot_size);
208 * To obtain the auxtrace buffer file descriptor, the auxtrace
209 * event must come first.
211 perf_evlist__to_front(evlist, cs_etm_evsel);
213 * In the case of per-cpu mmaps, we need the CPU on the
216 if (!cpu_map__empty(cpus))
217 perf_evsel__set_sample_bit(cs_etm_evsel, CPU);
220 /* Add dummy event to keep tracking */
221 if (opts->full_auxtrace) {
222 struct perf_evsel *tracking_evsel;
225 err = parse_events(evlist, "dummy:u", NULL);
229 tracking_evsel = perf_evlist__last(evlist);
230 perf_evlist__set_tracking_event(evlist, tracking_evsel);
232 tracking_evsel->attr.freq = 0;
233 tracking_evsel->attr.sample_period = 1;
235 /* In per-cpu case, always need the time of mmap events etc */
236 if (!cpu_map__empty(cpus))
237 perf_evsel__set_sample_bit(tracking_evsel, TIME);
243 static u64 cs_etm_get_config(struct auxtrace_record *itr)
246 struct cs_etm_recording *ptr =
247 container_of(itr, struct cs_etm_recording, itr);
248 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
249 struct perf_evlist *evlist = ptr->evlist;
250 struct perf_evsel *evsel;
252 evlist__for_each_entry(evlist, evsel) {
253 if (evsel->attr.type == cs_etm_pmu->type) {
255 * Variable perf_event_attr::config is assigned to
256 * ETMv3/PTM. The bit fields have been made to match
257 * the ETMv3.5 ETRMCR register specification. See the
258 * PMU_FORMAT_ATTR() declarations in
259 * drivers/hwtracing/coresight/coresight-perf.c for
262 config = evsel->attr.config;
271 cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
272 struct perf_evlist *evlist __maybe_unused)
275 int etmv3 = 0, etmv4 = 0;
276 const struct cpu_map *cpus = evlist->cpus;
278 /* cpu map is not empty, we have specific CPUs to work with */
279 if (!cpu_map__empty(cpus)) {
280 for (i = 0; i < cpu_map__nr(cpus); i++) {
281 if (cs_etm_is_etmv4(itr, cpus->map[i]))
287 /* get configuration for all CPUs in the system */
288 for (i = 0; i < cpu__max_cpu(); i++) {
289 if (cs_etm_is_etmv4(itr, i))
296 return (CS_ETM_HEADER_SIZE +
297 (etmv4 * CS_ETMV4_PRIV_SIZE) +
298 (etmv3 * CS_ETMV3_PRIV_SIZE));
301 static const char *metadata_etmv3_ro[CS_ETM_PRIV_MAX] = {
302 [CS_ETM_ETMCCER] = "mgmt/etmccer",
303 [CS_ETM_ETMIDR] = "mgmt/etmidr",
306 static const char *metadata_etmv4_ro[CS_ETMV4_PRIV_MAX] = {
307 [CS_ETMV4_TRCIDR0] = "trcidr/trcidr0",
308 [CS_ETMV4_TRCIDR1] = "trcidr/trcidr1",
309 [CS_ETMV4_TRCIDR2] = "trcidr/trcidr2",
310 [CS_ETMV4_TRCIDR8] = "trcidr/trcidr8",
311 [CS_ETMV4_TRCAUTHSTATUS] = "mgmt/trcauthstatus",
314 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu)
320 struct cs_etm_recording *ptr =
321 container_of(itr, struct cs_etm_recording, itr);
322 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
324 /* Take any of the RO files for ETMv4 and see if it present */
325 snprintf(path, PATH_MAX, "cpu%d/%s",
326 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
327 scan = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val);
329 /* The file was read successfully, we have a winner */
336 static int cs_etm_get_ro(struct perf_pmu *pmu, int cpu, const char *path)
338 char pmu_path[PATH_MAX];
340 unsigned int val = 0;
342 /* Get RO metadata from sysfs */
343 snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path);
345 scan = perf_pmu__scan_file(pmu, pmu_path, "%x", &val);
347 pr_err("%s: error reading: %s\n", __func__, pmu_path);
352 static void cs_etm_get_metadata(int cpu, u32 *offset,
353 struct auxtrace_record *itr,
354 struct auxtrace_info_event *info)
358 struct cs_etm_recording *ptr =
359 container_of(itr, struct cs_etm_recording, itr);
360 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
362 /* first see what kind of tracer this cpu is affined to */
363 if (cs_etm_is_etmv4(itr, cpu)) {
364 magic = __perf_cs_etmv4_magic;
365 /* Get trace configuration register */
366 info->priv[*offset + CS_ETMV4_TRCCONFIGR] =
367 cs_etm_get_config(itr);
368 /* Get traceID from the framework */
369 info->priv[*offset + CS_ETMV4_TRCTRACEIDR] =
370 coresight_get_trace_id(cpu);
371 /* Get read-only information from sysFS */
372 info->priv[*offset + CS_ETMV4_TRCIDR0] =
373 cs_etm_get_ro(cs_etm_pmu, cpu,
374 metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
375 info->priv[*offset + CS_ETMV4_TRCIDR1] =
376 cs_etm_get_ro(cs_etm_pmu, cpu,
377 metadata_etmv4_ro[CS_ETMV4_TRCIDR1]);
378 info->priv[*offset + CS_ETMV4_TRCIDR2] =
379 cs_etm_get_ro(cs_etm_pmu, cpu,
380 metadata_etmv4_ro[CS_ETMV4_TRCIDR2]);
381 info->priv[*offset + CS_ETMV4_TRCIDR8] =
382 cs_etm_get_ro(cs_etm_pmu, cpu,
383 metadata_etmv4_ro[CS_ETMV4_TRCIDR8]);
384 info->priv[*offset + CS_ETMV4_TRCAUTHSTATUS] =
385 cs_etm_get_ro(cs_etm_pmu, cpu,
387 [CS_ETMV4_TRCAUTHSTATUS]);
389 /* How much space was used */
390 increment = CS_ETMV4_PRIV_MAX;
392 magic = __perf_cs_etmv3_magic;
393 /* Get configuration register */
394 info->priv[*offset + CS_ETM_ETMCR] = cs_etm_get_config(itr);
395 /* Get traceID from the framework */
396 info->priv[*offset + CS_ETM_ETMTRACEIDR] =
397 coresight_get_trace_id(cpu);
398 /* Get read-only information from sysFS */
399 info->priv[*offset + CS_ETM_ETMCCER] =
400 cs_etm_get_ro(cs_etm_pmu, cpu,
401 metadata_etmv3_ro[CS_ETM_ETMCCER]);
402 info->priv[*offset + CS_ETM_ETMIDR] =
403 cs_etm_get_ro(cs_etm_pmu, cpu,
404 metadata_etmv3_ro[CS_ETM_ETMIDR]);
406 /* How much space was used */
407 increment = CS_ETM_PRIV_MAX;
410 /* Build generic header portion */
411 info->priv[*offset + CS_ETM_MAGIC] = magic;
412 info->priv[*offset + CS_ETM_CPU] = cpu;
413 /* Where the next CPU entry should start from */
414 *offset += increment;
417 static int cs_etm_info_fill(struct auxtrace_record *itr,
418 struct perf_session *session,
419 struct auxtrace_info_event *info,
425 const struct cpu_map *cpus = session->evlist->cpus;
426 struct cs_etm_recording *ptr =
427 container_of(itr, struct cs_etm_recording, itr);
428 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
430 if (priv_size != cs_etm_info_priv_size(itr, session->evlist))
433 if (!session->evlist->nr_mmaps)
436 /* If the cpu_map is empty all CPUs are involved */
437 nr_cpu = cpu_map__empty(cpus) ? cpu__max_cpu() : cpu_map__nr(cpus);
438 /* Get PMU type as dynamically assigned by the core */
439 type = cs_etm_pmu->type;
441 /* First fill out the session header */
442 info->type = PERF_AUXTRACE_CS_ETM;
443 info->priv[CS_HEADER_VERSION_0] = 0;
444 info->priv[CS_PMU_TYPE_CPUS] = type << 32;
445 info->priv[CS_PMU_TYPE_CPUS] |= nr_cpu;
446 info->priv[CS_ETM_SNAPSHOT] = ptr->snapshot_mode;
448 offset = CS_ETM_SNAPSHOT + 1;
450 /* cpu map is not empty, we have specific CPUs to work with */
451 if (!cpu_map__empty(cpus)) {
452 for (i = 0; i < cpu_map__nr(cpus) && offset < priv_size; i++)
453 cs_etm_get_metadata(cpus->map[i], &offset, itr, info);
455 /* get configuration for all CPUs in the system */
456 for (i = 0; i < cpu__max_cpu(); i++)
457 cs_etm_get_metadata(i, &offset, itr, info);
463 static int cs_etm_alloc_wrapped_array(struct cs_etm_recording *ptr, int idx)
466 int cnt = ptr->wrapped_cnt;
468 /* Make @ptr->wrapped as big as @idx */
473 * Free'ed in cs_etm_recording_free(). Using realloc() to avoid
474 * cross compilation problems where the host's system supports
475 * reallocarray() but not the target.
477 wrapped = realloc(ptr->wrapped, cnt * sizeof(bool));
481 wrapped[cnt - 1] = false;
482 ptr->wrapped_cnt = cnt;
483 ptr->wrapped = wrapped;
488 static bool cs_etm_buffer_has_wrapped(unsigned char *buffer,
489 size_t buffer_size, u64 head)
492 u64 *buf = (u64 *)buffer;
493 size_t buf_size = buffer_size;
496 * We want to look the very last 512 byte (chosen arbitrarily) in
499 watermark = buf_size - 512;
502 * @head is continuously increasing - if its value is equal or greater
503 * than the size of the ring buffer, it has wrapped around.
505 if (head >= buffer_size)
509 * The value of @head is somewhere within the size of the ring buffer.
510 * This can be that there hasn't been enough data to fill the ring
511 * buffer yet or the trace time was so long that @head has numerically
512 * wrapped around. To find we need to check if we have data at the very
513 * end of the ring buffer. We can reliably do this because mmap'ed
514 * pages are zeroed out and there is a fresh mapping with every new
518 /* @head is less than 512 byte from the end of the ring buffer */
519 if (head > watermark)
523 * Speed things up by using 64 bit transactions (see "u64 *buf" above)
529 * If we find trace data at the end of the ring buffer, @head has
530 * been there and has numerically wrapped around at least once.
532 for (i = watermark; i < buf_size; i++)
539 static int cs_etm_find_snapshot(struct auxtrace_record *itr,
540 int idx, struct auxtrace_mmap *mm,
546 struct cs_etm_recording *ptr =
547 container_of(itr, struct cs_etm_recording, itr);
550 * Allocate memory to keep track of wrapping if this is the first
551 * time we deal with this *mm.
553 if (idx >= ptr->wrapped_cnt) {
554 err = cs_etm_alloc_wrapped_array(ptr, idx);
560 * Check to see if *head has wrapped around. If it hasn't only the
561 * amount of data between *head and *old is snapshot'ed to avoid
562 * bloating the perf.data file with zeros. But as soon as *head has
563 * wrapped around the entire size of the AUX ring buffer it taken.
565 wrapped = ptr->wrapped[idx];
566 if (!wrapped && cs_etm_buffer_has_wrapped(data, mm->len, *head)) {
568 ptr->wrapped[idx] = true;
571 pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n",
572 __func__, idx, (size_t)*old, (size_t)*head, mm->len);
574 /* No wrap has occurred, we can just use *head and *old. */
579 * *head has wrapped around - adjust *head and *old to pickup the
580 * entire content of the AUX buffer.
582 if (*head >= mm->len) {
583 *old = *head - mm->len;
586 *old = *head - mm->len;
592 static int cs_etm_snapshot_start(struct auxtrace_record *itr)
594 struct cs_etm_recording *ptr =
595 container_of(itr, struct cs_etm_recording, itr);
596 struct perf_evsel *evsel;
598 evlist__for_each_entry(ptr->evlist, evsel) {
599 if (evsel->attr.type == ptr->cs_etm_pmu->type)
600 return perf_evsel__disable(evsel);
605 static int cs_etm_snapshot_finish(struct auxtrace_record *itr)
607 struct cs_etm_recording *ptr =
608 container_of(itr, struct cs_etm_recording, itr);
609 struct perf_evsel *evsel;
611 evlist__for_each_entry(ptr->evlist, evsel) {
612 if (evsel->attr.type == ptr->cs_etm_pmu->type)
613 return perf_evsel__enable(evsel);
618 static u64 cs_etm_reference(struct auxtrace_record *itr __maybe_unused)
620 return (((u64) rand() << 0) & 0x00000000FFFFFFFFull) |
621 (((u64) rand() << 32) & 0xFFFFFFFF00000000ull);
624 static void cs_etm_recording_free(struct auxtrace_record *itr)
626 struct cs_etm_recording *ptr =
627 container_of(itr, struct cs_etm_recording, itr);
629 zfree(&ptr->wrapped);
633 static int cs_etm_read_finish(struct auxtrace_record *itr, int idx)
635 struct cs_etm_recording *ptr =
636 container_of(itr, struct cs_etm_recording, itr);
637 struct perf_evsel *evsel;
639 evlist__for_each_entry(ptr->evlist, evsel) {
640 if (evsel->attr.type == ptr->cs_etm_pmu->type)
641 return perf_evlist__enable_event_idx(ptr->evlist,
648 struct auxtrace_record *cs_etm_record_init(int *err)
650 struct perf_pmu *cs_etm_pmu;
651 struct cs_etm_recording *ptr;
653 cs_etm_pmu = perf_pmu__find(CORESIGHT_ETM_PMU_NAME);
660 ptr = zalloc(sizeof(struct cs_etm_recording));
666 ptr->cs_etm_pmu = cs_etm_pmu;
667 ptr->itr.parse_snapshot_options = cs_etm_parse_snapshot_options;
668 ptr->itr.recording_options = cs_etm_recording_options;
669 ptr->itr.info_priv_size = cs_etm_info_priv_size;
670 ptr->itr.info_fill = cs_etm_info_fill;
671 ptr->itr.find_snapshot = cs_etm_find_snapshot;
672 ptr->itr.snapshot_start = cs_etm_snapshot_start;
673 ptr->itr.snapshot_finish = cs_etm_snapshot_finish;
674 ptr->itr.reference = cs_etm_reference;
675 ptr->itr.free = cs_etm_recording_free;
676 ptr->itr.read_finish = cs_etm_read_finish;
684 static FILE *cs_device__open_file(const char *name)
690 sysfs = sysfs__mountpoint();
694 snprintf(path, PATH_MAX,
695 "%s" CS_BUS_DEVICE_PATH "%s", sysfs, name);
697 printf("path: %s\n", path);
699 if (stat(path, &st) < 0)
702 return fopen(path, "w");
706 static __attribute__((format(printf, 2, 3)))
707 int cs_device__print_file(const char *name, const char *fmt, ...)
714 file = cs_device__open_file(name);
716 ret = vfprintf(file, fmt, args);
723 int cs_etm_set_drv_config(struct perf_evsel_config_term *term)
726 char enable_sink[ENABLE_SINK_MAX];
728 snprintf(enable_sink, ENABLE_SINK_MAX, "%s/%s",
729 term->val.drv_cfg, "enable_sink");
731 ret = cs_device__print_file(enable_sink, "%d", 1);