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/pmu.h"
31 #include "../../util/thread_map.h"
32 #include "../../util/cs-etm.h"
36 struct cs_etm_recording {
37 struct auxtrace_record itr;
38 struct perf_pmu *cs_etm_pmu;
39 struct perf_evlist *evlist;
44 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu);
46 static int cs_etm_parse_snapshot_options(struct auxtrace_record *itr,
47 struct record_opts *opts,
50 struct cs_etm_recording *ptr =
51 container_of(itr, struct cs_etm_recording, itr);
52 unsigned long long snapshot_size = 0;
56 snapshot_size = strtoull(str, &endptr, 0);
57 if (*endptr || snapshot_size > SIZE_MAX)
61 opts->auxtrace_snapshot_mode = true;
62 opts->auxtrace_snapshot_size = snapshot_size;
63 ptr->snapshot_size = snapshot_size;
68 static int cs_etm_recording_options(struct auxtrace_record *itr,
69 struct perf_evlist *evlist,
70 struct record_opts *opts)
72 struct cs_etm_recording *ptr =
73 container_of(itr, struct cs_etm_recording, itr);
74 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
75 struct perf_evsel *evsel, *cs_etm_evsel = NULL;
76 const struct cpu_map *cpus = evlist->cpus;
77 bool privileged = (geteuid() == 0 || perf_event_paranoid() < 0);
80 ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
82 evlist__for_each(evlist, evsel) {
83 if (evsel->attr.type == cs_etm_pmu->type) {
85 pr_err("There may be only one %s event\n",
86 CORESIGHT_ETM_PMU_NAME);
90 evsel->attr.sample_period = 1;
92 opts->full_auxtrace = true;
96 /* no need to continue if at least one event of interest was found */
100 if (opts->use_clockid) {
101 pr_err("Cannot use clockid (-k option) with %s\n",
102 CORESIGHT_ETM_PMU_NAME);
106 /* we are in snapshot mode */
107 if (opts->auxtrace_snapshot_mode) {
109 * No size were given to '-S' or '-m,', so go with
112 if (!opts->auxtrace_snapshot_size &&
113 !opts->auxtrace_mmap_pages) {
115 opts->auxtrace_mmap_pages = MiB(4) / page_size;
117 opts->auxtrace_mmap_pages =
118 KiB(128) / page_size;
119 if (opts->mmap_pages == UINT_MAX)
120 opts->mmap_pages = KiB(256) / page_size;
122 } else if (!opts->auxtrace_mmap_pages && !privileged &&
123 opts->mmap_pages == UINT_MAX) {
124 opts->mmap_pages = KiB(256) / page_size;
128 * '-m,xyz' was specified but no snapshot size, so make the
129 * snapshot size as big as the auxtrace mmap area.
131 if (!opts->auxtrace_snapshot_size) {
132 opts->auxtrace_snapshot_size =
133 opts->auxtrace_mmap_pages * (size_t)page_size;
137 * -Sxyz was specified but no auxtrace mmap area, so make the
138 * auxtrace mmap area big enough to fit the requested snapshot
141 if (!opts->auxtrace_mmap_pages) {
142 size_t sz = opts->auxtrace_snapshot_size;
144 sz = round_up(sz, page_size) / page_size;
145 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
148 /* Snapshost size can't be bigger than the auxtrace area */
149 if (opts->auxtrace_snapshot_size >
150 opts->auxtrace_mmap_pages * (size_t)page_size) {
151 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
152 opts->auxtrace_snapshot_size,
153 opts->auxtrace_mmap_pages * (size_t)page_size);
157 /* Something went wrong somewhere - this shouldn't happen */
158 if (!opts->auxtrace_snapshot_size ||
159 !opts->auxtrace_mmap_pages) {
160 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
165 /* We are in full trace mode but '-m,xyz' wasn't specified */
166 if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
168 opts->auxtrace_mmap_pages = MiB(4) / page_size;
170 opts->auxtrace_mmap_pages = KiB(128) / page_size;
171 if (opts->mmap_pages == UINT_MAX)
172 opts->mmap_pages = KiB(256) / page_size;
177 /* Validate auxtrace_mmap_pages provided by user */
178 if (opts->auxtrace_mmap_pages) {
179 unsigned int max_page = (KiB(128) / page_size);
180 size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
183 opts->auxtrace_mmap_pages > max_page) {
184 opts->auxtrace_mmap_pages = max_page;
185 pr_err("auxtrace too big, truncating to %d\n",
189 if (!is_power_of_2(sz)) {
190 pr_err("Invalid mmap size for %s: must be a power of 2\n",
191 CORESIGHT_ETM_PMU_NAME);
196 if (opts->auxtrace_snapshot_mode)
197 pr_debug2("%s snapshot size: %zu\n", CORESIGHT_ETM_PMU_NAME,
198 opts->auxtrace_snapshot_size);
202 * To obtain the auxtrace buffer file descriptor, the auxtrace
203 * event must come first.
205 perf_evlist__to_front(evlist, cs_etm_evsel);
207 * In the case of per-cpu mmaps, we need the CPU on the
210 if (!cpu_map__empty(cpus))
211 perf_evsel__set_sample_bit(cs_etm_evsel, CPU);
214 /* Add dummy event to keep tracking */
215 if (opts->full_auxtrace) {
216 struct perf_evsel *tracking_evsel;
219 err = parse_events(evlist, "dummy:u", NULL);
223 tracking_evsel = perf_evlist__last(evlist);
224 perf_evlist__set_tracking_event(evlist, tracking_evsel);
226 tracking_evsel->attr.freq = 0;
227 tracking_evsel->attr.sample_period = 1;
229 /* In per-cpu case, always need the time of mmap events etc */
230 if (!cpu_map__empty(cpus))
231 perf_evsel__set_sample_bit(tracking_evsel, TIME);
237 static u64 cs_etm_get_config(struct auxtrace_record *itr)
240 struct cs_etm_recording *ptr =
241 container_of(itr, struct cs_etm_recording, itr);
242 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
243 struct perf_evlist *evlist = ptr->evlist;
244 struct perf_evsel *evsel;
246 evlist__for_each(evlist, evsel) {
247 if (evsel->attr.type == cs_etm_pmu->type) {
249 * Variable perf_event_attr::config is assigned to
250 * ETMv3/PTM. The bit fields have been made to match
251 * the ETMv3.5 ETRMCR register specification. See the
252 * PMU_FORMAT_ATTR() declarations in
253 * drivers/hwtracing/coresight/coresight-perf.c for
256 config = evsel->attr.config;
265 cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
266 struct perf_evlist *evlist __maybe_unused)
269 int etmv3 = 0, etmv4 = 0;
270 const struct cpu_map *cpus = evlist->cpus;
272 /* cpu map is not empty, we have specific CPUs to work with */
273 if (!cpu_map__empty(cpus)) {
274 for (i = 0; i < cpu_map__nr(cpus); i++) {
275 if (cs_etm_is_etmv4(itr, cpus->map[i]))
281 /* get configuration for all CPUs in the system */
282 for (i = 0; i < cpu__max_cpu(); i++) {
283 if (cs_etm_is_etmv4(itr, i))
290 return (CS_ETM_HEADER_SIZE +
291 (etmv4 * CS_ETMV4_PRIV_SIZE) +
292 (etmv3 * CS_ETMV3_PRIV_SIZE));
295 static const char *metadata_etmv3_ro[CS_ETM_PRIV_MAX] = {
296 [CS_ETM_ETMCCER] = "mgmt/etmccer",
297 [CS_ETM_ETMIDR] = "mgmt/etmidr",
300 static const char *metadata_etmv4_ro[CS_ETMV4_PRIV_MAX] = {
301 [CS_ETMV4_TRCIDR0] = "trcidr/trcidr0",
302 [CS_ETMV4_TRCIDR1] = "trcidr/trcidr1",
303 [CS_ETMV4_TRCIDR2] = "trcidr/trcidr2",
304 [CS_ETMV4_TRCIDR8] = "trcidr/trcidr8",
305 [CS_ETMV4_TRCAUTHSTATUS] = "mgmt/trcauthstatus",
308 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu)
314 struct cs_etm_recording *ptr =
315 container_of(itr, struct cs_etm_recording, itr);
316 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
318 /* Take any of the RO files for ETMv4 and see if it present */
319 snprintf(path, PATH_MAX, "cpu%d/%s",
320 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
321 scan = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val);
323 /* The file was read successfully, we have a winner */
330 static int cs_etm_get_ro(struct perf_pmu *pmu, int cpu, const char *path)
332 char pmu_path[PATH_MAX];
334 unsigned int val = 0;
336 /* Get RO metadata from sysfs */
337 snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path);
339 scan = perf_pmu__scan_file(pmu, pmu_path, "%x", &val);
341 pr_err("%s: error reading: %s\n", __func__, pmu_path);
346 static void cs_etm_get_metadata(int cpu, u32 *offset,
347 struct auxtrace_record *itr,
348 struct auxtrace_info_event *info)
352 struct cs_etm_recording *ptr =
353 container_of(itr, struct cs_etm_recording, itr);
354 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
356 /* first see what kind of tracer this cpu is affined to */
357 if (cs_etm_is_etmv4(itr, cpu)) {
358 magic = __perf_cs_etmv4_magic;
359 /* Get trace configuration register */
360 info->priv[*offset + CS_ETMV4_TRCCONFIGR] =
361 cs_etm_get_config(itr);
362 /* Get traceID from the framework */
363 info->priv[*offset + CS_ETMV4_TRCTRACEIDR] =
364 coresight_get_trace_id(cpu);
365 /* Get read-only information from sysFS */
366 info->priv[*offset + CS_ETMV4_TRCIDR0] =
367 cs_etm_get_ro(cs_etm_pmu, cpu,
368 metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
369 info->priv[*offset + CS_ETMV4_TRCIDR1] =
370 cs_etm_get_ro(cs_etm_pmu, cpu,
371 metadata_etmv4_ro[CS_ETMV4_TRCIDR1]);
372 info->priv[*offset + CS_ETMV4_TRCIDR2] =
373 cs_etm_get_ro(cs_etm_pmu, cpu,
374 metadata_etmv4_ro[CS_ETMV4_TRCIDR2]);
375 info->priv[*offset + CS_ETMV4_TRCIDR8] =
376 cs_etm_get_ro(cs_etm_pmu, cpu,
377 metadata_etmv4_ro[CS_ETMV4_TRCIDR8]);
378 info->priv[*offset + CS_ETMV4_TRCAUTHSTATUS] =
379 cs_etm_get_ro(cs_etm_pmu, cpu,
381 [CS_ETMV4_TRCAUTHSTATUS]);
383 /* How much space was used */
384 increment = CS_ETMV4_PRIV_MAX;
386 magic = __perf_cs_etmv3_magic;
387 /* Get configuration register */
388 info->priv[*offset + CS_ETM_ETMCR] = cs_etm_get_config(itr);
389 /* Get traceID from the framework */
390 info->priv[*offset + CS_ETM_ETMTRACEIDR] =
391 coresight_get_trace_id(cpu);
392 /* Get read-only information from sysFS */
393 info->priv[*offset + CS_ETM_ETMCCER] =
394 cs_etm_get_ro(cs_etm_pmu, cpu,
395 metadata_etmv3_ro[CS_ETM_ETMCCER]);
396 info->priv[*offset + CS_ETM_ETMIDR] =
397 cs_etm_get_ro(cs_etm_pmu, cpu,
398 metadata_etmv3_ro[CS_ETM_ETMIDR]);
400 /* How much space was used */
401 increment = CS_ETM_PRIV_MAX;
404 /* Build generic header portion */
405 info->priv[*offset + CS_ETM_MAGIC] = magic;
406 info->priv[*offset + CS_ETM_CPU] = cpu;
407 /* Where the next CPU entry should start from */
408 *offset += increment;
411 static int cs_etm_info_fill(struct auxtrace_record *itr,
412 struct perf_session *session,
413 struct auxtrace_info_event *info,
419 const struct cpu_map *cpus = session->evlist->cpus;
420 struct cs_etm_recording *ptr =
421 container_of(itr, struct cs_etm_recording, itr);
422 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
424 if (priv_size != cs_etm_info_priv_size(itr, session->evlist))
427 if (!session->evlist->nr_mmaps)
430 /* If the cpu_map is empty all CPUs are involved */
431 nr_cpu = cpu_map__empty(cpus) ? cpu__max_cpu() : cpu_map__nr(cpus);
432 /* Get PMU type as dynamically assigned by the core */
433 type = cs_etm_pmu->type;
435 /* First fill out the session header */
436 info->type = PERF_AUXTRACE_CS_ETM;
437 info->priv[CS_HEADER_VERSION_0] = 0;
438 info->priv[CS_PMU_TYPE_CPUS] = type << 32;
439 info->priv[CS_PMU_TYPE_CPUS] |= nr_cpu;
440 info->priv[CS_ETM_SNAPSHOT] = ptr->snapshot_mode;
442 offset = CS_ETM_SNAPSHOT + 1;
444 /* cpu map is not empty, we have specific CPUs to work with */
445 if (!cpu_map__empty(cpus)) {
446 for (i = 0; i < cpu_map__nr(cpus) && offset < priv_size; i++)
447 cs_etm_get_metadata(cpus->map[i], &offset, itr, info);
449 /* get configuration for all CPUs in the system */
450 for (i = 0; i < cpu__max_cpu(); i++)
451 cs_etm_get_metadata(i, &offset, itr, info);
457 static int cs_etm_find_snapshot(struct auxtrace_record *itr __maybe_unused,
458 int idx, struct auxtrace_mmap *mm,
459 unsigned char *data __maybe_unused,
462 pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n",
463 __func__, idx, (size_t)*old, (size_t)*head, mm->len);
471 static int cs_etm_snapshot_start(struct auxtrace_record *itr)
473 struct cs_etm_recording *ptr =
474 container_of(itr, struct cs_etm_recording, itr);
475 struct perf_evsel *evsel;
477 evlist__for_each(ptr->evlist, evsel) {
478 if (evsel->attr.type == ptr->cs_etm_pmu->type)
479 return perf_evsel__disable(evsel);
484 static int cs_etm_snapshot_finish(struct auxtrace_record *itr)
486 struct cs_etm_recording *ptr =
487 container_of(itr, struct cs_etm_recording, itr);
488 struct perf_evsel *evsel;
490 evlist__for_each(ptr->evlist, evsel) {
491 int nthreads = thread_map__nr(evsel->threads);
492 int ncpus = cpu_map__nr(evsel->cpus);
494 if (evsel->attr.type == ptr->cs_etm_pmu->type) {
495 return perf_evsel__enable(evsel, ncpus, nthreads);
501 static u64 cs_etm_reference(struct auxtrace_record *itr __maybe_unused)
503 return (((u64) rand() << 0) & 0x00000000FFFFFFFFull) |
504 (((u64) rand() << 32) & 0xFFFFFFFF00000000ull);
507 static void cs_etm_recording_free(struct auxtrace_record *itr)
509 struct cs_etm_recording *ptr =
510 container_of(itr, struct cs_etm_recording, itr);
514 static int cs_etm_read_finish(struct auxtrace_record *itr, int idx)
516 struct cs_etm_recording *ptr =
517 container_of(itr, struct cs_etm_recording, itr);
518 struct perf_evsel *evsel;
520 evlist__for_each(ptr->evlist, evsel) {
521 if (evsel->attr.type == ptr->cs_etm_pmu->type)
522 return perf_evlist__enable_event_idx(ptr->evlist,
529 struct auxtrace_record *cs_etm_record_init(int *err)
531 struct perf_pmu *cs_etm_pmu;
532 struct cs_etm_recording *ptr;
534 cs_etm_pmu = perf_pmu__find(CORESIGHT_ETM_PMU_NAME);
541 ptr = zalloc(sizeof(struct cs_etm_recording));
547 ptr->cs_etm_pmu = cs_etm_pmu;
548 ptr->itr.parse_snapshot_options = cs_etm_parse_snapshot_options;
549 ptr->itr.recording_options = cs_etm_recording_options;
550 ptr->itr.info_priv_size = cs_etm_info_priv_size;
551 ptr->itr.info_fill = cs_etm_info_fill;
552 ptr->itr.find_snapshot = cs_etm_find_snapshot;
553 ptr->itr.snapshot_start = cs_etm_snapshot_start;
554 ptr->itr.snapshot_finish = cs_etm_snapshot_finish;
555 ptr->itr.reference = cs_etm_reference;
556 ptr->itr.free = cs_etm_recording_free;
557 ptr->itr.read_finish = cs_etm_read_finish;