1
2
3
4
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/evsel_config.h"
27#include "../../util/pmu.h"
28#include "../../util/cs-etm.h"
29#include <internal/lib.h>
30#include "../../util/session.h"
31
32#include <errno.h>
33#include <stdlib.h>
34#include <sys/stat.h>
35
36struct cs_etm_recording {
37 struct auxtrace_record itr;
38 struct perf_pmu *cs_etm_pmu;
39 struct evlist *evlist;
40 int wrapped_cnt;
41 bool *wrapped;
42 bool snapshot_mode;
43 size_t snapshot_size;
44};
45
46static const char *metadata_etmv3_ro[CS_ETM_PRIV_MAX] = {
47 [CS_ETM_ETMCCER] = "mgmt/etmccer",
48 [CS_ETM_ETMIDR] = "mgmt/etmidr",
49};
50
51static const char *metadata_etmv4_ro[CS_ETMV4_PRIV_MAX] = {
52 [CS_ETMV4_TRCIDR0] = "trcidr/trcidr0",
53 [CS_ETMV4_TRCIDR1] = "trcidr/trcidr1",
54 [CS_ETMV4_TRCIDR2] = "trcidr/trcidr2",
55 [CS_ETMV4_TRCIDR8] = "trcidr/trcidr8",
56 [CS_ETMV4_TRCAUTHSTATUS] = "mgmt/trcauthstatus",
57};
58
59static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu);
60
61static int cs_etm_set_context_id(struct auxtrace_record *itr,
62 struct evsel *evsel, int cpu)
63{
64 struct cs_etm_recording *ptr;
65 struct perf_pmu *cs_etm_pmu;
66 char path[PATH_MAX];
67 int err = -EINVAL;
68 u32 val;
69
70 ptr = container_of(itr, struct cs_etm_recording, itr);
71 cs_etm_pmu = ptr->cs_etm_pmu;
72
73 if (!cs_etm_is_etmv4(itr, cpu))
74 goto out;
75
76
77 snprintf(path, PATH_MAX, "cpu%d/%s",
78 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR2]);
79 err = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val);
80
81
82 if (err != 1) {
83 pr_err("%s: can't read file %s\n",
84 CORESIGHT_ETM_PMU_NAME, path);
85 goto out;
86 }
87
88
89
90
91
92
93
94
95 val = BMVAL(val, 5, 9);
96 if (!val || val != 0x4) {
97 err = -EINVAL;
98 goto out;
99 }
100
101
102 evsel->core.attr.config |= (1 << ETM_OPT_CTXTID);
103 err = 0;
104
105out:
106
107 return err;
108}
109
110static int cs_etm_set_timestamp(struct auxtrace_record *itr,
111 struct evsel *evsel, int cpu)
112{
113 struct cs_etm_recording *ptr;
114 struct perf_pmu *cs_etm_pmu;
115 char path[PATH_MAX];
116 int err = -EINVAL;
117 u32 val;
118
119 ptr = container_of(itr, struct cs_etm_recording, itr);
120 cs_etm_pmu = ptr->cs_etm_pmu;
121
122 if (!cs_etm_is_etmv4(itr, cpu))
123 goto out;
124
125
126 snprintf(path, PATH_MAX, "cpu%d/%s",
127 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
128 err = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val);
129
130
131 if (err != 1) {
132 pr_err("%s: can't read file %s\n",
133 CORESIGHT_ETM_PMU_NAME, path);
134 goto out;
135 }
136
137
138
139
140
141
142
143
144 val &= GENMASK(28, 24);
145 if (!val) {
146 err = -EINVAL;
147 goto out;
148 }
149
150
151 evsel->core.attr.config |= (1 << ETM_OPT_TS);
152 err = 0;
153
154out:
155 return err;
156}
157
158static int cs_etm_set_option(struct auxtrace_record *itr,
159 struct evsel *evsel, u32 option)
160{
161 int i, err = -EINVAL;
162 struct perf_cpu_map *event_cpus = evsel->evlist->core.cpus;
163 struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
164
165
166 for (i = 0; i < cpu__max_cpu(); i++) {
167 if (!cpu_map__has(event_cpus, i) ||
168 !cpu_map__has(online_cpus, i))
169 continue;
170
171 if (option & ETM_OPT_CTXTID) {
172 err = cs_etm_set_context_id(itr, evsel, i);
173 if (err)
174 goto out;
175 }
176 if (option & ETM_OPT_TS) {
177 err = cs_etm_set_timestamp(itr, evsel, i);
178 if (err)
179 goto out;
180 }
181 if (option & ~(ETM_OPT_CTXTID | ETM_OPT_TS))
182
183 goto out;
184 }
185
186 err = 0;
187out:
188 perf_cpu_map__put(online_cpus);
189 return err;
190}
191
192static int cs_etm_parse_snapshot_options(struct auxtrace_record *itr,
193 struct record_opts *opts,
194 const char *str)
195{
196 struct cs_etm_recording *ptr =
197 container_of(itr, struct cs_etm_recording, itr);
198 unsigned long long snapshot_size = 0;
199 char *endptr;
200
201 if (str) {
202 snapshot_size = strtoull(str, &endptr, 0);
203 if (*endptr || snapshot_size > SIZE_MAX)
204 return -1;
205 }
206
207 opts->auxtrace_snapshot_mode = true;
208 opts->auxtrace_snapshot_size = snapshot_size;
209 ptr->snapshot_size = snapshot_size;
210
211 return 0;
212}
213
214static int cs_etm_set_sink_attr(struct perf_pmu *pmu,
215 struct evsel *evsel)
216{
217 char msg[BUFSIZ], path[PATH_MAX], *sink;
218 struct perf_evsel_config_term *term;
219 int ret = -EINVAL;
220 u32 hash;
221
222 if (evsel->core.attr.config2 & GENMASK(31, 0))
223 return 0;
224
225 list_for_each_entry(term, &evsel->config_terms, list) {
226 if (term->type != PERF_EVSEL__CONFIG_TERM_DRV_CFG)
227 continue;
228
229 sink = term->val.drv_cfg;
230 snprintf(path, PATH_MAX, "sinks/%s", sink);
231
232 ret = perf_pmu__scan_file(pmu, path, "%x", &hash);
233 if (ret != 1) {
234 pr_err("failed to set sink \"%s\" on event %s with %d (%s)\n",
235 sink, perf_evsel__name(evsel), errno,
236 str_error_r(errno, msg, sizeof(msg)));
237 return ret;
238 }
239
240 evsel->core.attr.config2 |= hash;
241 return 0;
242 }
243
244
245
246
247
248 return ret;
249}
250
251static int cs_etm_recording_options(struct auxtrace_record *itr,
252 struct evlist *evlist,
253 struct record_opts *opts)
254{
255 int ret;
256 struct cs_etm_recording *ptr =
257 container_of(itr, struct cs_etm_recording, itr);
258 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
259 struct evsel *evsel, *cs_etm_evsel = NULL;
260 struct perf_cpu_map *cpus = evlist->core.cpus;
261 bool privileged = perf_event_paranoid_check(-1);
262 int err = 0;
263
264 ptr->evlist = evlist;
265 ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
266
267 if (perf_can_record_switch_events())
268 opts->record_switch_events = true;
269
270 evlist__for_each_entry(evlist, evsel) {
271 if (evsel->core.attr.type == cs_etm_pmu->type) {
272 if (cs_etm_evsel) {
273 pr_err("There may be only one %s event\n",
274 CORESIGHT_ETM_PMU_NAME);
275 return -EINVAL;
276 }
277 evsel->core.attr.freq = 0;
278 evsel->core.attr.sample_period = 1;
279 cs_etm_evsel = evsel;
280 opts->full_auxtrace = true;
281 }
282 }
283
284
285 if (!cs_etm_evsel)
286 return 0;
287
288 ret = cs_etm_set_sink_attr(cs_etm_pmu, cs_etm_evsel);
289 if (ret)
290 return ret;
291
292 if (opts->use_clockid) {
293 pr_err("Cannot use clockid (-k option) with %s\n",
294 CORESIGHT_ETM_PMU_NAME);
295 return -EINVAL;
296 }
297
298
299 if (opts->auxtrace_snapshot_mode) {
300
301
302
303
304 if (!opts->auxtrace_snapshot_size &&
305 !opts->auxtrace_mmap_pages) {
306 if (privileged) {
307 opts->auxtrace_mmap_pages = MiB(4) / page_size;
308 } else {
309 opts->auxtrace_mmap_pages =
310 KiB(128) / page_size;
311 if (opts->mmap_pages == UINT_MAX)
312 opts->mmap_pages = KiB(256) / page_size;
313 }
314 } else if (!opts->auxtrace_mmap_pages && !privileged &&
315 opts->mmap_pages == UINT_MAX) {
316 opts->mmap_pages = KiB(256) / page_size;
317 }
318
319
320
321
322
323 if (!opts->auxtrace_snapshot_size) {
324 opts->auxtrace_snapshot_size =
325 opts->auxtrace_mmap_pages * (size_t)page_size;
326 }
327
328
329
330
331
332
333 if (!opts->auxtrace_mmap_pages) {
334 size_t sz = opts->auxtrace_snapshot_size;
335
336 sz = round_up(sz, page_size) / page_size;
337 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
338 }
339
340
341 if (opts->auxtrace_snapshot_size >
342 opts->auxtrace_mmap_pages * (size_t)page_size) {
343 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
344 opts->auxtrace_snapshot_size,
345 opts->auxtrace_mmap_pages * (size_t)page_size);
346 return -EINVAL;
347 }
348
349
350 if (!opts->auxtrace_snapshot_size ||
351 !opts->auxtrace_mmap_pages) {
352 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
353 return -EINVAL;
354 }
355 }
356
357
358 if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
359 if (privileged) {
360 opts->auxtrace_mmap_pages = MiB(4) / page_size;
361 } else {
362 opts->auxtrace_mmap_pages = KiB(128) / page_size;
363 if (opts->mmap_pages == UINT_MAX)
364 opts->mmap_pages = KiB(256) / page_size;
365 }
366
367 }
368
369
370 if (opts->auxtrace_mmap_pages) {
371 unsigned int max_page = (KiB(128) / page_size);
372 size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
373
374 if (!privileged &&
375 opts->auxtrace_mmap_pages > max_page) {
376 opts->auxtrace_mmap_pages = max_page;
377 pr_err("auxtrace too big, truncating to %d\n",
378 max_page);
379 }
380
381 if (!is_power_of_2(sz)) {
382 pr_err("Invalid mmap size for %s: must be a power of 2\n",
383 CORESIGHT_ETM_PMU_NAME);
384 return -EINVAL;
385 }
386 }
387
388 if (opts->auxtrace_snapshot_mode)
389 pr_debug2("%s snapshot size: %zu\n", CORESIGHT_ETM_PMU_NAME,
390 opts->auxtrace_snapshot_size);
391
392
393
394
395
396 perf_evlist__to_front(evlist, cs_etm_evsel);
397
398
399
400
401
402
403 if (!perf_cpu_map__empty(cpus)) {
404 perf_evsel__set_sample_bit(cs_etm_evsel, CPU);
405
406 err = cs_etm_set_option(itr, cs_etm_evsel,
407 ETM_OPT_CTXTID | ETM_OPT_TS);
408 if (err)
409 goto out;
410 }
411
412
413 if (opts->full_auxtrace) {
414 struct evsel *tracking_evsel;
415
416 err = parse_events(evlist, "dummy:u", NULL);
417 if (err)
418 goto out;
419
420 tracking_evsel = evlist__last(evlist);
421 perf_evlist__set_tracking_event(evlist, tracking_evsel);
422
423 tracking_evsel->core.attr.freq = 0;
424 tracking_evsel->core.attr.sample_period = 1;
425
426
427 if (!perf_cpu_map__empty(cpus))
428 perf_evsel__set_sample_bit(tracking_evsel, TIME);
429 }
430
431out:
432 return err;
433}
434
435static u64 cs_etm_get_config(struct auxtrace_record *itr)
436{
437 u64 config = 0;
438 struct cs_etm_recording *ptr =
439 container_of(itr, struct cs_etm_recording, itr);
440 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
441 struct evlist *evlist = ptr->evlist;
442 struct evsel *evsel;
443
444 evlist__for_each_entry(evlist, evsel) {
445 if (evsel->core.attr.type == cs_etm_pmu->type) {
446
447
448
449
450
451
452
453
454 config = evsel->core.attr.config;
455 break;
456 }
457 }
458
459 return config;
460}
461
462#ifndef BIT
463#define BIT(N) (1UL << (N))
464#endif
465
466static u64 cs_etmv4_get_config(struct auxtrace_record *itr)
467{
468 u64 config = 0;
469 u64 config_opts = 0;
470
471
472
473
474
475
476
477 config_opts = cs_etm_get_config(itr);
478 if (config_opts & BIT(ETM_OPT_CYCACC))
479 config |= BIT(ETM4_CFG_BIT_CYCACC);
480 if (config_opts & BIT(ETM_OPT_CTXTID))
481 config |= BIT(ETM4_CFG_BIT_CTXTID);
482 if (config_opts & BIT(ETM_OPT_TS))
483 config |= BIT(ETM4_CFG_BIT_TS);
484 if (config_opts & BIT(ETM_OPT_RETSTK))
485 config |= BIT(ETM4_CFG_BIT_RETSTK);
486
487 return config;
488}
489
490static size_t
491cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
492 struct evlist *evlist __maybe_unused)
493{
494 int i;
495 int etmv3 = 0, etmv4 = 0;
496 struct perf_cpu_map *event_cpus = evlist->core.cpus;
497 struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
498
499
500 if (!perf_cpu_map__empty(event_cpus)) {
501 for (i = 0; i < cpu__max_cpu(); i++) {
502 if (!cpu_map__has(event_cpus, i) ||
503 !cpu_map__has(online_cpus, i))
504 continue;
505
506 if (cs_etm_is_etmv4(itr, i))
507 etmv4++;
508 else
509 etmv3++;
510 }
511 } else {
512
513 for (i = 0; i < cpu__max_cpu(); i++) {
514 if (!cpu_map__has(online_cpus, i))
515 continue;
516
517 if (cs_etm_is_etmv4(itr, i))
518 etmv4++;
519 else
520 etmv3++;
521 }
522 }
523
524 perf_cpu_map__put(online_cpus);
525
526 return (CS_ETM_HEADER_SIZE +
527 (etmv4 * CS_ETMV4_PRIV_SIZE) +
528 (etmv3 * CS_ETMV3_PRIV_SIZE));
529}
530
531static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu)
532{
533 bool ret = false;
534 char path[PATH_MAX];
535 int scan;
536 unsigned int val;
537 struct cs_etm_recording *ptr =
538 container_of(itr, struct cs_etm_recording, itr);
539 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
540
541
542 snprintf(path, PATH_MAX, "cpu%d/%s",
543 cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
544 scan = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val);
545
546
547 if (scan == 1)
548 ret = true;
549
550 return ret;
551}
552
553static int cs_etm_get_ro(struct perf_pmu *pmu, int cpu, const char *path)
554{
555 char pmu_path[PATH_MAX];
556 int scan;
557 unsigned int val = 0;
558
559
560 snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path);
561
562 scan = perf_pmu__scan_file(pmu, pmu_path, "%x", &val);
563 if (scan != 1)
564 pr_err("%s: error reading: %s\n", __func__, pmu_path);
565
566 return val;
567}
568
569static void cs_etm_get_metadata(int cpu, u32 *offset,
570 struct auxtrace_record *itr,
571 struct perf_record_auxtrace_info *info)
572{
573 u32 increment;
574 u64 magic;
575 struct cs_etm_recording *ptr =
576 container_of(itr, struct cs_etm_recording, itr);
577 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
578
579
580 if (cs_etm_is_etmv4(itr, cpu)) {
581 magic = __perf_cs_etmv4_magic;
582
583 info->priv[*offset + CS_ETMV4_TRCCONFIGR] =
584 cs_etmv4_get_config(itr);
585
586 info->priv[*offset + CS_ETMV4_TRCTRACEIDR] =
587 coresight_get_trace_id(cpu);
588
589 info->priv[*offset + CS_ETMV4_TRCIDR0] =
590 cs_etm_get_ro(cs_etm_pmu, cpu,
591 metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
592 info->priv[*offset + CS_ETMV4_TRCIDR1] =
593 cs_etm_get_ro(cs_etm_pmu, cpu,
594 metadata_etmv4_ro[CS_ETMV4_TRCIDR1]);
595 info->priv[*offset + CS_ETMV4_TRCIDR2] =
596 cs_etm_get_ro(cs_etm_pmu, cpu,
597 metadata_etmv4_ro[CS_ETMV4_TRCIDR2]);
598 info->priv[*offset + CS_ETMV4_TRCIDR8] =
599 cs_etm_get_ro(cs_etm_pmu, cpu,
600 metadata_etmv4_ro[CS_ETMV4_TRCIDR8]);
601 info->priv[*offset + CS_ETMV4_TRCAUTHSTATUS] =
602 cs_etm_get_ro(cs_etm_pmu, cpu,
603 metadata_etmv4_ro
604 [CS_ETMV4_TRCAUTHSTATUS]);
605
606
607 increment = CS_ETMV4_PRIV_MAX;
608 } else {
609 magic = __perf_cs_etmv3_magic;
610
611 info->priv[*offset + CS_ETM_ETMCR] = cs_etm_get_config(itr);
612
613 info->priv[*offset + CS_ETM_ETMTRACEIDR] =
614 coresight_get_trace_id(cpu);
615
616 info->priv[*offset + CS_ETM_ETMCCER] =
617 cs_etm_get_ro(cs_etm_pmu, cpu,
618 metadata_etmv3_ro[CS_ETM_ETMCCER]);
619 info->priv[*offset + CS_ETM_ETMIDR] =
620 cs_etm_get_ro(cs_etm_pmu, cpu,
621 metadata_etmv3_ro[CS_ETM_ETMIDR]);
622
623
624 increment = CS_ETM_PRIV_MAX;
625 }
626
627
628 info->priv[*offset + CS_ETM_MAGIC] = magic;
629 info->priv[*offset + CS_ETM_CPU] = cpu;
630
631 *offset += increment;
632}
633
634static int cs_etm_info_fill(struct auxtrace_record *itr,
635 struct perf_session *session,
636 struct perf_record_auxtrace_info *info,
637 size_t priv_size)
638{
639 int i;
640 u32 offset;
641 u64 nr_cpu, type;
642 struct perf_cpu_map *cpu_map;
643 struct perf_cpu_map *event_cpus = session->evlist->core.cpus;
644 struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
645 struct cs_etm_recording *ptr =
646 container_of(itr, struct cs_etm_recording, itr);
647 struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
648
649 if (priv_size != cs_etm_info_priv_size(itr, session->evlist))
650 return -EINVAL;
651
652 if (!session->evlist->core.nr_mmaps)
653 return -EINVAL;
654
655
656 if (perf_cpu_map__empty(event_cpus)) {
657 cpu_map = online_cpus;
658 } else {
659
660 for (i = 0; i < perf_cpu_map__nr(event_cpus); i++) {
661 if (cpu_map__has(event_cpus, i) &&
662 !cpu_map__has(online_cpus, i))
663 return -EINVAL;
664 }
665
666 cpu_map = event_cpus;
667 }
668
669 nr_cpu = perf_cpu_map__nr(cpu_map);
670
671 type = cs_etm_pmu->type;
672
673
674 info->type = PERF_AUXTRACE_CS_ETM;
675 info->priv[CS_HEADER_VERSION_0] = 0;
676 info->priv[CS_PMU_TYPE_CPUS] = type << 32;
677 info->priv[CS_PMU_TYPE_CPUS] |= nr_cpu;
678 info->priv[CS_ETM_SNAPSHOT] = ptr->snapshot_mode;
679
680 offset = CS_ETM_SNAPSHOT + 1;
681
682 for (i = 0; i < cpu__max_cpu() && offset < priv_size; i++)
683 if (cpu_map__has(cpu_map, i))
684 cs_etm_get_metadata(i, &offset, itr, info);
685
686 perf_cpu_map__put(online_cpus);
687
688 return 0;
689}
690
691static int cs_etm_alloc_wrapped_array(struct cs_etm_recording *ptr, int idx)
692{
693 bool *wrapped;
694 int cnt = ptr->wrapped_cnt;
695
696
697 while (cnt <= idx)
698 cnt++;
699
700
701
702
703
704
705 wrapped = realloc(ptr->wrapped, cnt * sizeof(bool));
706 if (!wrapped)
707 return -ENOMEM;
708
709 wrapped[cnt - 1] = false;
710 ptr->wrapped_cnt = cnt;
711 ptr->wrapped = wrapped;
712
713 return 0;
714}
715
716static bool cs_etm_buffer_has_wrapped(unsigned char *buffer,
717 size_t buffer_size, u64 head)
718{
719 u64 i, watermark;
720 u64 *buf = (u64 *)buffer;
721 size_t buf_size = buffer_size;
722
723
724
725
726
727 watermark = buf_size - 512;
728
729
730
731
732
733 if (head >= buffer_size)
734 return true;
735
736
737
738
739
740
741
742
743
744
745
746
747 if (head > watermark)
748 watermark = head;
749
750
751
752
753 watermark >>= 3;
754 buf_size >>= 3;
755
756
757
758
759
760 for (i = watermark; i < buf_size; i++)
761 if (buf[i])
762 return true;
763
764 return false;
765}
766
767static int cs_etm_find_snapshot(struct auxtrace_record *itr,
768 int idx, struct auxtrace_mmap *mm,
769 unsigned char *data,
770 u64 *head, u64 *old)
771{
772 int err;
773 bool wrapped;
774 struct cs_etm_recording *ptr =
775 container_of(itr, struct cs_etm_recording, itr);
776
777
778
779
780
781 if (idx >= ptr->wrapped_cnt) {
782 err = cs_etm_alloc_wrapped_array(ptr, idx);
783 if (err)
784 return err;
785 }
786
787
788
789
790
791
792
793 wrapped = ptr->wrapped[idx];
794 if (!wrapped && cs_etm_buffer_has_wrapped(data, mm->len, *head)) {
795 wrapped = true;
796 ptr->wrapped[idx] = true;
797 }
798
799 pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n",
800 __func__, idx, (size_t)*old, (size_t)*head, mm->len);
801
802
803 if (!wrapped)
804 return 0;
805
806
807
808
809
810 if (*head >= mm->len) {
811 *old = *head - mm->len;
812 } else {
813 *head += mm->len;
814 *old = *head - mm->len;
815 }
816
817 return 0;
818}
819
820static int cs_etm_snapshot_start(struct auxtrace_record *itr)
821{
822 struct cs_etm_recording *ptr =
823 container_of(itr, struct cs_etm_recording, itr);
824 struct evsel *evsel;
825
826 evlist__for_each_entry(ptr->evlist, evsel) {
827 if (evsel->core.attr.type == ptr->cs_etm_pmu->type)
828 return evsel__disable(evsel);
829 }
830 return -EINVAL;
831}
832
833static int cs_etm_snapshot_finish(struct auxtrace_record *itr)
834{
835 struct cs_etm_recording *ptr =
836 container_of(itr, struct cs_etm_recording, itr);
837 struct evsel *evsel;
838
839 evlist__for_each_entry(ptr->evlist, evsel) {
840 if (evsel->core.attr.type == ptr->cs_etm_pmu->type)
841 return evsel__enable(evsel);
842 }
843 return -EINVAL;
844}
845
846static u64 cs_etm_reference(struct auxtrace_record *itr __maybe_unused)
847{
848 return (((u64) rand() << 0) & 0x00000000FFFFFFFFull) |
849 (((u64) rand() << 32) & 0xFFFFFFFF00000000ull);
850}
851
852static void cs_etm_recording_free(struct auxtrace_record *itr)
853{
854 struct cs_etm_recording *ptr =
855 container_of(itr, struct cs_etm_recording, itr);
856
857 zfree(&ptr->wrapped);
858 free(ptr);
859}
860
861static int cs_etm_read_finish(struct auxtrace_record *itr, int idx)
862{
863 struct cs_etm_recording *ptr =
864 container_of(itr, struct cs_etm_recording, itr);
865 struct evsel *evsel;
866
867 evlist__for_each_entry(ptr->evlist, evsel) {
868 if (evsel->core.attr.type == ptr->cs_etm_pmu->type)
869 return perf_evlist__enable_event_idx(ptr->evlist,
870 evsel, idx);
871 }
872
873 return -EINVAL;
874}
875
876struct auxtrace_record *cs_etm_record_init(int *err)
877{
878 struct perf_pmu *cs_etm_pmu;
879 struct cs_etm_recording *ptr;
880
881 cs_etm_pmu = perf_pmu__find(CORESIGHT_ETM_PMU_NAME);
882
883 if (!cs_etm_pmu) {
884 *err = -EINVAL;
885 goto out;
886 }
887
888 ptr = zalloc(sizeof(struct cs_etm_recording));
889 if (!ptr) {
890 *err = -ENOMEM;
891 goto out;
892 }
893
894 ptr->cs_etm_pmu = cs_etm_pmu;
895 ptr->itr.parse_snapshot_options = cs_etm_parse_snapshot_options;
896 ptr->itr.recording_options = cs_etm_recording_options;
897 ptr->itr.info_priv_size = cs_etm_info_priv_size;
898 ptr->itr.info_fill = cs_etm_info_fill;
899 ptr->itr.find_snapshot = cs_etm_find_snapshot;
900 ptr->itr.snapshot_start = cs_etm_snapshot_start;
901 ptr->itr.snapshot_finish = cs_etm_snapshot_finish;
902 ptr->itr.reference = cs_etm_reference;
903 ptr->itr.free = cs_etm_recording_free;
904 ptr->itr.read_finish = cs_etm_read_finish;
905
906 *err = 0;
907 return &ptr->itr;
908out:
909 return NULL;
910}
911