GNU Linux-libre 4.14.266-gnu1
[releases.git] / tools / perf / arch / arm / util / cs-etm.c
1 /*
2  * Copyright(C) 2015 Linaro Limited. All rights reserved.
3  * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
4  *
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.
8  *
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
12  * more details.
13  *
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/>.
16  */
17
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>
25
26 #include "cs-etm.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"
35
36 #include <stdlib.h>
37 #include <sys/stat.h>
38
39 #define ENABLE_SINK_MAX 128
40 #define CS_BUS_DEVICE_PATH "/bus/coresight/devices/"
41
42 struct cs_etm_recording {
43         struct auxtrace_record  itr;
44         struct perf_pmu         *cs_etm_pmu;
45         struct perf_evlist      *evlist;
46         int                     wrapped_cnt;
47         bool                    *wrapped;
48         bool                    snapshot_mode;
49         size_t                  snapshot_size;
50 };
51
52 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu);
53
54 static int cs_etm_parse_snapshot_options(struct auxtrace_record *itr,
55                                          struct record_opts *opts,
56                                          const char *str)
57 {
58         struct cs_etm_recording *ptr =
59                                 container_of(itr, struct cs_etm_recording, itr);
60         unsigned long long snapshot_size = 0;
61         char *endptr;
62
63         if (str) {
64                 snapshot_size = strtoull(str, &endptr, 0);
65                 if (*endptr || snapshot_size > SIZE_MAX)
66                         return -1;
67         }
68
69         opts->auxtrace_snapshot_mode = true;
70         opts->auxtrace_snapshot_size = snapshot_size;
71         ptr->snapshot_size = snapshot_size;
72
73         return 0;
74 }
75
76 static int cs_etm_recording_options(struct auxtrace_record *itr,
77                                     struct perf_evlist *evlist,
78                                     struct record_opts *opts)
79 {
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);
86
87         ptr->evlist = evlist;
88         ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
89
90         evlist__for_each_entry(evlist, evsel) {
91                 if (evsel->attr.type == cs_etm_pmu->type) {
92                         if (cs_etm_evsel) {
93                                 pr_err("There may be only one %s event\n",
94                                        CORESIGHT_ETM_PMU_NAME);
95                                 return -EINVAL;
96                         }
97                         evsel->attr.freq = 0;
98                         evsel->attr.sample_period = 1;
99                         cs_etm_evsel = evsel;
100                         opts->full_auxtrace = true;
101                 }
102         }
103
104         /* no need to continue if at least one event of interest was found */
105         if (!cs_etm_evsel)
106                 return 0;
107
108         if (opts->use_clockid) {
109                 pr_err("Cannot use clockid (-k option) with %s\n",
110                        CORESIGHT_ETM_PMU_NAME);
111                 return -EINVAL;
112         }
113
114         /* we are in snapshot mode */
115         if (opts->auxtrace_snapshot_mode) {
116                 /*
117                  * No size were given to '-S' or '-m,', so go with
118                  * the default
119                  */
120                 if (!opts->auxtrace_snapshot_size &&
121                     !opts->auxtrace_mmap_pages) {
122                         if (privileged) {
123                                 opts->auxtrace_mmap_pages = MiB(4) / page_size;
124                         } else {
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;
129                         }
130                 } else if (!opts->auxtrace_mmap_pages && !privileged &&
131                                                 opts->mmap_pages == UINT_MAX) {
132                         opts->mmap_pages = KiB(256) / page_size;
133                 }
134
135                 /*
136                  * '-m,xyz' was specified but no snapshot size, so make the
137                  * snapshot size as big as the auxtrace mmap area.
138                  */
139                 if (!opts->auxtrace_snapshot_size) {
140                         opts->auxtrace_snapshot_size =
141                                 opts->auxtrace_mmap_pages * (size_t)page_size;
142                 }
143
144                 /*
145                  * -Sxyz was specified but no auxtrace mmap area, so make the
146                  * auxtrace mmap area big enough to fit the requested snapshot
147                  * size.
148                  */
149                 if (!opts->auxtrace_mmap_pages) {
150                         size_t sz = opts->auxtrace_snapshot_size;
151
152                         sz = round_up(sz, page_size) / page_size;
153                         opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
154                 }
155
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);
162                         return -EINVAL;
163                 }
164
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");
169                         return -EINVAL;
170                 }
171         }
172
173         /* We are in full trace mode but '-m,xyz' wasn't specified */
174         if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
175                 if (privileged) {
176                         opts->auxtrace_mmap_pages = MiB(4) / page_size;
177                 } else {
178                         opts->auxtrace_mmap_pages = KiB(128) / page_size;
179                         if (opts->mmap_pages == UINT_MAX)
180                                 opts->mmap_pages = KiB(256) / page_size;
181                 }
182
183         }
184
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;
189
190                 if (!privileged &&
191                     opts->auxtrace_mmap_pages > max_page) {
192                         opts->auxtrace_mmap_pages = max_page;
193                         pr_err("auxtrace too big, truncating to %d\n",
194                                max_page);
195                 }
196
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);
200                         return -EINVAL;
201                 }
202         }
203
204         if (opts->auxtrace_snapshot_mode)
205                 pr_debug2("%s snapshot size: %zu\n", CORESIGHT_ETM_PMU_NAME,
206                           opts->auxtrace_snapshot_size);
207
208         /*
209          * To obtain the auxtrace buffer file descriptor, the auxtrace
210          * event must come first.
211          */
212         perf_evlist__to_front(evlist, cs_etm_evsel);
213
214         /*
215          * In the case of per-cpu mmaps, we need the CPU on the
216          * AUX event.
217          */
218         if (!cpu_map__empty(cpus))
219                 perf_evsel__set_sample_bit(cs_etm_evsel, CPU);
220
221         /* Add dummy event to keep tracking */
222         if (opts->full_auxtrace) {
223                 struct perf_evsel *tracking_evsel;
224                 int err;
225
226                 err = parse_events(evlist, "dummy:u", NULL);
227                 if (err)
228                         return err;
229
230                 tracking_evsel = perf_evlist__last(evlist);
231                 perf_evlist__set_tracking_event(evlist, tracking_evsel);
232
233                 tracking_evsel->attr.freq = 0;
234                 tracking_evsel->attr.sample_period = 1;
235
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);
239         }
240
241         return 0;
242 }
243
244 static u64 cs_etm_get_config(struct auxtrace_record *itr)
245 {
246         u64 config = 0;
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;
252
253         evlist__for_each_entry(evlist, evsel) {
254                 if (evsel->attr.type == cs_etm_pmu->type) {
255                         /*
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
261                          * details.
262                          */
263                         config = evsel->attr.config;
264                         break;
265                 }
266         }
267
268         return config;
269 }
270
271 #ifndef BIT
272 #define BIT(N) (1UL << (N))
273 #endif
274
275 static u64 cs_etmv4_get_config(struct auxtrace_record *itr)
276 {
277         u64 config = 0;
278         u64 config_opts = 0;
279
280         /*
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
284          * to real bits
285          */
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);
293
294         return config;
295 }
296
297 static size_t
298 cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
299                       struct perf_evlist *evlist __maybe_unused)
300 {
301         int i;
302         int etmv3 = 0, etmv4 = 0;
303         const struct cpu_map *cpus = evlist->cpus;
304
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]))
309                                 etmv4++;
310                         else
311                                 etmv3++;
312                 }
313         } else {
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))
317                                 etmv4++;
318                         else
319                                 etmv3++;
320                 }
321         }
322
323         return (CS_ETM_HEADER_SIZE +
324                (etmv4 * CS_ETMV4_PRIV_SIZE) +
325                (etmv3 * CS_ETMV3_PRIV_SIZE));
326 }
327
328 static const char *metadata_etmv3_ro[CS_ETM_PRIV_MAX] = {
329         [CS_ETM_ETMCCER]        = "mgmt/etmccer",
330         [CS_ETM_ETMIDR]         = "mgmt/etmidr",
331 };
332
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",
339 };
340
341 static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu)
342 {
343         bool ret = false;
344         char path[PATH_MAX];
345         int scan;
346         unsigned int val;
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;
350
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);
355
356         /* The file was read successfully, we have a winner */
357         if (scan == 1)
358                 ret = true;
359
360         return ret;
361 }
362
363 static int cs_etm_get_ro(struct perf_pmu *pmu, int cpu, const char *path)
364 {
365         char pmu_path[PATH_MAX];
366         int scan;
367         unsigned int val = 0;
368
369         /* Get RO metadata from sysfs */
370         snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path);
371
372         scan = perf_pmu__scan_file(pmu, pmu_path, "%x", &val);
373         if (scan != 1)
374                 pr_err("%s: error reading: %s\n", __func__, pmu_path);
375
376         return val;
377 }
378
379 static void cs_etm_get_metadata(int cpu, u32 *offset,
380                                 struct auxtrace_record *itr,
381                                 struct auxtrace_info_event *info)
382 {
383         u32 increment;
384         u64 magic;
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;
388
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,
413                                       metadata_etmv4_ro
414                                       [CS_ETMV4_TRCAUTHSTATUS]);
415
416                 /* How much space was used */
417                 increment = CS_ETMV4_PRIV_MAX;
418         } else {
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]);
432
433                 /* How much space was used */
434                 increment = CS_ETM_PRIV_MAX;
435         }
436
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;
442 }
443
444 static int cs_etm_info_fill(struct auxtrace_record *itr,
445                             struct perf_session *session,
446                             struct auxtrace_info_event *info,
447                             size_t priv_size)
448 {
449         int i;
450         u32 offset;
451         u64 nr_cpu, type;
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;
456
457         if (priv_size != cs_etm_info_priv_size(itr, session->evlist))
458                 return -EINVAL;
459
460         if (!session->evlist->nr_mmaps)
461                 return -EINVAL;
462
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;
467
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;
474
475         offset = CS_ETM_SNAPSHOT + 1;
476
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);
481         } else {
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);
485         }
486
487         return 0;
488 }
489
490 static int cs_etm_alloc_wrapped_array(struct cs_etm_recording *ptr, int idx)
491 {
492         bool *wrapped;
493         int cnt = ptr->wrapped_cnt;
494
495         /* Make @ptr->wrapped as big as @idx */
496         while (cnt <= idx)
497                 cnt++;
498
499         /*
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.
503          */
504         wrapped = realloc(ptr->wrapped, cnt * sizeof(bool));
505         if (!wrapped)
506                 return -ENOMEM;
507
508         wrapped[cnt - 1] = false;
509         ptr->wrapped_cnt = cnt;
510         ptr->wrapped = wrapped;
511
512         return 0;
513 }
514
515 static bool cs_etm_buffer_has_wrapped(unsigned char *buffer,
516                                       size_t buffer_size, u64 head)
517 {
518         u64 i, watermark;
519         u64 *buf = (u64 *)buffer;
520         size_t buf_size = buffer_size;
521
522         /*
523          * We want to look the very last 512 byte (chosen arbitrarily) in
524          * the ring buffer.
525          */
526         watermark = buf_size - 512;
527
528         /*
529          * @head is continuously increasing - if its value is equal or greater
530          * than the size of the ring buffer, it has wrapped around.
531          */
532         if (head >= buffer_size)
533                 return true;
534
535         /*
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
542          * session.
543          */
544
545         /* @head is less than 512 byte from the end of the ring buffer */
546         if (head > watermark)
547                 watermark = head;
548
549         /*
550          * Speed things up by using 64 bit transactions (see "u64 *buf" above)
551          */
552         watermark >>= 3;
553         buf_size >>= 3;
554
555         /*
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.
558          */
559         for (i = watermark; i < buf_size; i++)
560                 if (buf[i])
561                         return true;
562
563         return false;
564 }
565
566 static int cs_etm_find_snapshot(struct auxtrace_record *itr,
567                                 int idx, struct auxtrace_mmap *mm,
568                                 unsigned char *data,
569                                 u64 *head, u64 *old)
570 {
571         int err;
572         bool wrapped;
573         struct cs_etm_recording *ptr =
574                         container_of(itr, struct cs_etm_recording, itr);
575
576         /*
577          * Allocate memory to keep track of wrapping if this is the first
578          * time we deal with this *mm.
579          */
580         if (idx >= ptr->wrapped_cnt) {
581                 err = cs_etm_alloc_wrapped_array(ptr, idx);
582                 if (err)
583                         return err;
584         }
585
586         /*
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.
591          */
592         wrapped = ptr->wrapped[idx];
593         if (!wrapped && cs_etm_buffer_has_wrapped(data, mm->len, *head)) {
594                 wrapped = true;
595                 ptr->wrapped[idx] = true;
596         }
597
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);
600
601         /* No wrap has occurred, we can just use *head and *old. */
602         if (!wrapped)
603                 return 0;
604
605         /*
606          * *head has wrapped around - adjust *head and *old to pickup the
607          * entire content of the AUX buffer.
608          */
609         if (*head >= mm->len) {
610                 *old = *head - mm->len;
611         } else {
612                 *head += mm->len;
613                 *old = *head - mm->len;
614         }
615
616         return 0;
617 }
618
619 static int cs_etm_snapshot_start(struct auxtrace_record *itr)
620 {
621         struct cs_etm_recording *ptr =
622                         container_of(itr, struct cs_etm_recording, itr);
623         struct perf_evsel *evsel;
624
625         evlist__for_each_entry(ptr->evlist, evsel) {
626                 if (evsel->attr.type == ptr->cs_etm_pmu->type)
627                         return perf_evsel__disable(evsel);
628         }
629         return -EINVAL;
630 }
631
632 static int cs_etm_snapshot_finish(struct auxtrace_record *itr)
633 {
634         struct cs_etm_recording *ptr =
635                         container_of(itr, struct cs_etm_recording, itr);
636         struct perf_evsel *evsel;
637
638         evlist__for_each_entry(ptr->evlist, evsel) {
639                 if (evsel->attr.type == ptr->cs_etm_pmu->type)
640                         return perf_evsel__enable(evsel);
641         }
642         return -EINVAL;
643 }
644
645 static u64 cs_etm_reference(struct auxtrace_record *itr __maybe_unused)
646 {
647         return (((u64) rand() <<  0) & 0x00000000FFFFFFFFull) |
648                 (((u64) rand() << 32) & 0xFFFFFFFF00000000ull);
649 }
650
651 static void cs_etm_recording_free(struct auxtrace_record *itr)
652 {
653         struct cs_etm_recording *ptr =
654                         container_of(itr, struct cs_etm_recording, itr);
655
656         zfree(&ptr->wrapped);
657         free(ptr);
658 }
659
660 static int cs_etm_read_finish(struct auxtrace_record *itr, int idx)
661 {
662         struct cs_etm_recording *ptr =
663                         container_of(itr, struct cs_etm_recording, itr);
664         struct perf_evsel *evsel;
665
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,
669                                                              evsel, idx);
670         }
671
672         return -EINVAL;
673 }
674
675 struct auxtrace_record *cs_etm_record_init(int *err)
676 {
677         struct perf_pmu *cs_etm_pmu;
678         struct cs_etm_recording *ptr;
679
680         cs_etm_pmu = perf_pmu__find(CORESIGHT_ETM_PMU_NAME);
681
682         if (!cs_etm_pmu) {
683                 *err = -EINVAL;
684                 goto out;
685         }
686
687         ptr = zalloc(sizeof(struct cs_etm_recording));
688         if (!ptr) {
689                 *err = -ENOMEM;
690                 goto out;
691         }
692
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;
704
705         *err = 0;
706         return &ptr->itr;
707 out:
708         return NULL;
709 }
710
711 static FILE *cs_device__open_file(const char *name)
712 {
713         struct stat st;
714         char path[PATH_MAX];
715         const char *sysfs;
716
717         sysfs = sysfs__mountpoint();
718         if (!sysfs)
719                 return NULL;
720
721         snprintf(path, PATH_MAX,
722                  "%s" CS_BUS_DEVICE_PATH "%s", sysfs, name);
723
724         if (stat(path, &st) < 0)
725                 return NULL;
726
727         return fopen(path, "w");
728
729 }
730
731 static int __printf(2, 3) cs_device__print_file(const char *name, const char *fmt, ...)
732 {
733         va_list args;
734         FILE *file;
735         int ret = -EINVAL;
736
737         va_start(args, fmt);
738         file = cs_device__open_file(name);
739         if (file) {
740                 ret = vfprintf(file, fmt, args);
741                 fclose(file);
742         }
743         va_end(args);
744         return ret;
745 }
746
747 int cs_etm_set_drv_config(struct perf_evsel_config_term *term)
748 {
749         int ret;
750         char enable_sink[ENABLE_SINK_MAX];
751
752         snprintf(enable_sink, ENABLE_SINK_MAX, "%s/%s",
753                  term->val.drv_cfg, "enable_sink");
754
755         ret = cs_device__print_file(enable_sink, "%d", 1);
756         if (ret < 0)
757                 return ret;
758
759         return 0;
760 }