1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright(C) 2015 Linaro Limited. All rights reserved.
4 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
5 */
6
7 #include <linux/bitfield.h>
8 #include <linux/coresight.h>
9 #include <linux/coresight-pmu.h>
10 #include <linux/cpumask.h>
11 #include <linux/device.h>
12 #include <linux/list.h>
13 #include <linux/mm.h>
14 #include <linux/init.h>
15 #include <linux/perf_event.h>
16 #include <linux/perf/arm_pmu.h>
17 #include <linux/percpu-defs.h>
18 #include <linux/slab.h>
19 #include <linux/stringhash.h>
20 #include <linux/types.h>
21 #include <linux/workqueue.h>
22
23 #include "coresight-config.h"
24 #include "coresight-etm-perf.h"
25 #include "coresight-priv.h"
26 #include "coresight-syscfg.h"
27 #include "coresight-trace-id.h"
28
29 static struct pmu etm_pmu;
30 static bool etm_perf_up;
31
32 /*
33 * An ETM context for a running event includes the perf aux handle
34 * and aux_data. For ETM, the aux_data (etm_event_data), consists of
35 * the trace path and the sink configuration. The event data is accessible
36 * via perf_get_aux(handle). However, a sink could "end" a perf output
37 * handle via the IRQ handler. And if the "sink" encounters a failure
38 * to "begin" another session (e.g due to lack of space in the buffer),
39 * the handle will be cleared. Thus, the event_data may not be accessible
40 * from the handle when we get to the etm_event_stop(), which is required
41 * for stopping the trace path. The event_data is guaranteed to stay alive
42 * until "free_aux()", which cannot happen as long as the event is active on
43 * the ETM. Thus the event_data for the session must be part of the ETM context
44 * to make sure we can disable the trace path.
45 */
46 struct etm_ctxt {
47 struct perf_output_handle handle;
48 struct etm_event_data *event_data;
49 };
50
51 static DEFINE_PER_CPU(struct etm_ctxt, etm_ctxt);
52
53 GEN_PMU_FORMAT_ATTR(cycacc);
54 GEN_PMU_FORMAT_ATTR(timestamp);
55 GEN_PMU_FORMAT_ATTR(retstack);
56 GEN_PMU_FORMAT_ATTR(sinkid);
57
58 #if IS_ENABLED(CONFIG_CORESIGHT_SOURCE_ETM4X)
59 GEN_PMU_FORMAT_ATTR(branch_broadcast);
60 /* contextid1 enables tracing CONTEXTIDR_EL1*/
61 GEN_PMU_FORMAT_ATTR(contextid1);
62 /* contextid2 enables tracing CONTEXTIDR_EL2*/
63 GEN_PMU_FORMAT_ATTR(contextid2);
64 /* preset - if sink ID is used as a configuration selector */
65 GEN_PMU_FORMAT_ATTR(preset);
66 /* config ID - set if a system configuration is selected */
67 GEN_PMU_FORMAT_ATTR(configid);
68 GEN_PMU_FORMAT_ATTR(cc_threshold);
69
70 /*
71 * contextid always traces the "PID". The PID is in CONTEXTIDR_EL1
72 * when the kernel is running at EL1; when the kernel is at EL2,
73 * the PID is in CONTEXTIDR_EL2.
74 */
format_attr_contextid_show(struct device * dev,struct device_attribute * attr,char * page)75 static ssize_t format_attr_contextid_show(struct device *dev,
76 struct device_attribute *attr,
77 char *page)
78 {
79 if (is_kernel_in_hyp_mode())
80 return contextid2_show(dev, attr, page);
81 return contextid1_show(dev, attr, page);
82 }
83
84 static struct device_attribute format_attr_contextid =
85 __ATTR(contextid, 0444, format_attr_contextid_show, NULL);
86 #endif
87
88 /*
89 * ETMv3 only uses the first 3 attributes for programming itself (see
90 * ETM3X_SUPPORTED_OPTIONS). Sink ID is also supported for selecting a
91 * sink in both, but not used for configuring the ETM. The remaining
92 * attributes are ETMv4 specific.
93 */
94 static struct attribute *etm_config_formats_attr[] = {
95 &format_attr_cycacc.attr,
96 &format_attr_timestamp.attr,
97 &format_attr_retstack.attr,
98 &format_attr_sinkid.attr,
99 #if IS_ENABLED(CONFIG_CORESIGHT_SOURCE_ETM4X)
100 &format_attr_contextid.attr,
101 &format_attr_contextid1.attr,
102 &format_attr_contextid2.attr,
103 &format_attr_preset.attr,
104 &format_attr_configid.attr,
105 &format_attr_branch_broadcast.attr,
106 &format_attr_cc_threshold.attr,
107 #endif
108 NULL,
109 };
110
111 static const struct attribute_group etm_pmu_format_group = {
112 .name = "format",
113 .attrs = etm_config_formats_attr,
114 };
115
116 static struct attribute *etm_config_sinks_attr[] = {
117 NULL,
118 };
119
120 static const struct attribute_group etm_pmu_sinks_group = {
121 .name = "sinks",
122 .attrs = etm_config_sinks_attr,
123 };
124
125 static struct attribute *etm_config_events_attr[] = {
126 NULL,
127 };
128
129 static const struct attribute_group etm_pmu_events_group = {
130 .name = "events",
131 .attrs = etm_config_events_attr,
132 };
133
134 static const struct attribute_group *etm_pmu_attr_groups[] = {
135 &etm_pmu_format_group,
136 &etm_pmu_sinks_group,
137 &etm_pmu_events_group,
138 NULL,
139 };
140
141 static inline struct coresight_path **
etm_event_cpu_path_ptr(struct etm_event_data * data,int cpu)142 etm_event_cpu_path_ptr(struct etm_event_data *data, int cpu)
143 {
144 return per_cpu_ptr(data->path, cpu);
145 }
146
147 static inline struct coresight_path *
etm_event_cpu_path(struct etm_event_data * data,int cpu)148 etm_event_cpu_path(struct etm_event_data *data, int cpu)
149 {
150 return *etm_event_cpu_path_ptr(data, cpu);
151 }
152
etm_event_read(struct perf_event * event)153 static void etm_event_read(struct perf_event *event) {}
154
etm_addr_filters_alloc(struct perf_event * event)155 static int etm_addr_filters_alloc(struct perf_event *event)
156 {
157 struct etm_filters *filters;
158 int node = event->cpu == -1 ? -1 : cpu_to_node(event->cpu);
159
160 filters = kzalloc_node(sizeof(struct etm_filters), GFP_KERNEL, node);
161 if (!filters)
162 return -ENOMEM;
163
164 if (event->parent)
165 memcpy(filters, event->parent->hw.addr_filters,
166 sizeof(*filters));
167
168 event->hw.addr_filters = filters;
169
170 return 0;
171 }
172
etm_event_destroy(struct perf_event * event)173 static void etm_event_destroy(struct perf_event *event)
174 {
175 kfree(event->hw.addr_filters);
176 event->hw.addr_filters = NULL;
177 }
178
etm_event_init(struct perf_event * event)179 static int etm_event_init(struct perf_event *event)
180 {
181 int ret = 0;
182
183 if (event->attr.type != etm_pmu.type) {
184 ret = -ENOENT;
185 goto out;
186 }
187
188 ret = etm_addr_filters_alloc(event);
189 if (ret)
190 goto out;
191
192 event->destroy = etm_event_destroy;
193 out:
194 return ret;
195 }
196
free_sink_buffer(struct etm_event_data * event_data)197 static void free_sink_buffer(struct etm_event_data *event_data)
198 {
199 int cpu;
200 cpumask_t *mask = &event_data->mask;
201 struct coresight_device *sink;
202
203 if (!event_data->snk_config)
204 return;
205
206 if (WARN_ON(cpumask_empty(mask)))
207 return;
208
209 cpu = cpumask_first(mask);
210 sink = coresight_get_sink(etm_event_cpu_path(event_data, cpu));
211 sink_ops(sink)->free_buffer(event_data->snk_config);
212 }
213
free_event_data(struct work_struct * work)214 static void free_event_data(struct work_struct *work)
215 {
216 int cpu;
217 cpumask_t *mask;
218 struct etm_event_data *event_data;
219
220 event_data = container_of(work, struct etm_event_data, work);
221 mask = &event_data->mask;
222
223 /* Free the sink buffers, if there are any */
224 free_sink_buffer(event_data);
225
226 /* clear any configuration we were using */
227 if (event_data->cfg_hash)
228 cscfg_deactivate_config(event_data->cfg_hash);
229
230 for_each_cpu(cpu, mask) {
231 struct coresight_path **ppath;
232
233 ppath = etm_event_cpu_path_ptr(event_data, cpu);
234 if (!(IS_ERR_OR_NULL(*ppath))) {
235 struct coresight_device *sink = coresight_get_sink(*ppath);
236
237 /*
238 * Mark perf event as done for trace id allocator, but don't call
239 * coresight_trace_id_put_cpu_id_map() on individual IDs. Perf sessions
240 * never free trace IDs to ensure that the ID associated with a CPU
241 * cannot change during their and other's concurrent sessions. Instead,
242 * a refcount is used so that the last event to call
243 * coresight_trace_id_perf_stop() frees all IDs.
244 */
245 coresight_trace_id_perf_stop(&sink->perf_sink_id_map);
246
247 coresight_release_path(*ppath);
248 }
249 *ppath = NULL;
250 }
251
252 free_percpu(event_data->path);
253 kfree(event_data);
254 }
255
alloc_event_data(int cpu)256 static void *alloc_event_data(int cpu)
257 {
258 cpumask_t *mask;
259 struct etm_event_data *event_data;
260
261 /* First get memory for the session's data */
262 event_data = kzalloc_obj(struct etm_event_data);
263 if (!event_data)
264 return NULL;
265
266
267 mask = &event_data->mask;
268 if (cpu != -1)
269 cpumask_set_cpu(cpu, mask);
270 else
271 cpumask_copy(mask, cpu_present_mask);
272
273 /*
274 * Each CPU has a single path between source and destination. As such
275 * allocate an array using CPU numbers as indexes. That way a path
276 * for any CPU can easily be accessed at any given time. We proceed
277 * the same way for sessions involving a single CPU. The cost of
278 * unused memory when dealing with single CPU trace scenarios is small
279 * compared to the cost of searching through an optimized array.
280 */
281 event_data->path = alloc_percpu(struct coresight_path *);
282
283 if (!event_data->path) {
284 kfree(event_data);
285 return NULL;
286 }
287
288 return event_data;
289 }
290
etm_free_aux(void * data)291 static void etm_free_aux(void *data)
292 {
293 struct etm_event_data *event_data = data;
294
295 schedule_work(&event_data->work);
296 }
297
298 /*
299 * Check if two given sinks are compatible with each other,
300 * so that they can use the same sink buffers, when an event
301 * moves around.
302 */
sinks_compatible(struct coresight_device * a,struct coresight_device * b)303 static bool sinks_compatible(struct coresight_device *a,
304 struct coresight_device *b)
305 {
306 if (!a || !b)
307 return false;
308 /*
309 * If the sinks are of the same subtype and driven
310 * by the same driver, we can use the same buffer
311 * on these sinks.
312 */
313 return (a->subtype.sink_subtype == b->subtype.sink_subtype) &&
314 (sink_ops(a) == sink_ops(b));
315 }
316
317 /*
318 * This helper is used for fetching the path pointer via the ctxt.
319 *
320 * Perf event callbacks run on the same CPU in atomic context, but AUX pause
321 * and resume may run in NMI context and preempt other callbacks. Since the
322 * event stop callback clears ctxt->event_data before the data is released,
323 * AUX pause/resume will either observe a NULL pointer and stop fetching the
324 * path pointer, or safely access event_data and the path, as the data has
325 * not yet been freed.
326 */
etm_event_get_ctxt_path(struct etm_ctxt * ctxt)327 static struct coresight_path *etm_event_get_ctxt_path(struct etm_ctxt *ctxt)
328 {
329 struct etm_event_data *event_data;
330 struct coresight_path *path;
331
332 if (!ctxt)
333 return NULL;
334
335 event_data = READ_ONCE(ctxt->event_data);
336 if (!event_data)
337 return NULL;
338
339 path = etm_event_cpu_path(event_data, smp_processor_id());
340 if (!path)
341 return NULL;
342
343 return path;
344 }
345
346 static struct coresight_path *
etm_event_build_path(struct perf_event * event,int cpu,struct coresight_device * user_sink,struct coresight_device * match_sink)347 etm_event_build_path(struct perf_event *event, int cpu,
348 struct coresight_device *user_sink,
349 struct coresight_device *match_sink)
350 {
351 struct coresight_path *path = NULL;
352 struct coresight_device *source, *sink;
353 int ret;
354
355 source = coresight_get_percpu_source_ref(cpu);
356
357 /*
358 * If there is no ETM associated with this CPU or ever we try to trace
359 * on this CPU, we handle it accordingly.
360 */
361 if (!source)
362 return NULL;
363
364 /*
365 * If AUX pause feature is enabled but the ETM driver does not
366 * support the operations, skip for this source.
367 */
368 if (event->attr.aux_start_paused &&
369 (!source_ops(source)->pause_perf ||
370 !source_ops(source)->resume_perf)) {
371 dev_err_once(&source->dev, "AUX pause is not supported.\n");
372 goto out;
373 }
374
375 /* If sink has been specified by user, directly use it */
376 if (user_sink) {
377 sink = user_sink;
378 } else {
379 /*
380 * No sink provided - look for a default sink for all the ETMs,
381 * where this event can be scheduled.
382 *
383 * We allocate the sink specific buffers only once for this
384 * event. If the ETMs have different default sink devices, we
385 * can only use a single "type" of sink as the event can carry
386 * only one sink specific buffer. Thus we have to make sure
387 * that the sinks are of the same type and driven by the same
388 * driver, as the one we allocate the buffer for. We don't
389 * trace on a CPU if the sink is not compatible.
390 */
391
392 /* Find the default sink for this ETM */
393 sink = coresight_find_default_sink(source);
394 if (!sink)
395 goto out;
396
397 /* Check if this sink compatible with the last sink */
398 if (match_sink && !sinks_compatible(match_sink, sink))
399 goto out;
400 }
401
402 /*
403 * Building a path doesn't enable it, it simply builds a
404 * list of devices from source to sink that can be
405 * referenced later when the path is actually needed.
406 */
407 path = coresight_build_path(source, sink);
408 if (IS_ERR(path))
409 goto out;
410
411 /* ensure we can allocate a trace ID for this CPU */
412 ret = coresight_path_assign_trace_id(path, CS_MODE_PERF);
413 if (ret) {
414 coresight_release_path(path);
415 path = NULL;
416 goto out;
417 }
418
419 coresight_trace_id_perf_start(&sink->perf_sink_id_map);
420
421 out:
422 coresight_put_percpu_source_ref(source);
423 return IS_ERR_OR_NULL(path) ? NULL : path;
424 }
425
etm_setup_aux(struct perf_event * event,void ** pages,int nr_pages,bool overwrite)426 static void *etm_setup_aux(struct perf_event *event, void **pages,
427 int nr_pages, bool overwrite)
428 {
429 u32 sink_hash, cfg_hash;
430 int cpu = event->cpu;
431 cpumask_t *mask;
432 struct coresight_device *sink = NULL;
433 struct coresight_device *user_sink = NULL;
434 struct etm_event_data *event_data = NULL;
435
436 event_data = alloc_event_data(cpu);
437 if (!event_data)
438 return NULL;
439 INIT_WORK(&event_data->work, free_event_data);
440
441 /* First get the selected sink from user space. */
442 sink_hash = ATTR_CFG_GET_FLD(&event->attr, sinkid);
443 if (sink_hash)
444 sink = user_sink = coresight_get_sink_by_id(sink_hash);
445
446 /* check if user wants a coresight configuration selected */
447 cfg_hash = ATTR_CFG_GET_FLD(&event->attr, configid);
448 if (cfg_hash) {
449 if (cscfg_activate_config(cfg_hash))
450 goto err;
451 event_data->cfg_hash = cfg_hash;
452 }
453
454 mask = &event_data->mask;
455
456 /*
457 * Setup the path for each CPU in a trace session. We try to build
458 * trace path for each CPU in the mask. If we don't find an ETM
459 * for the CPU or fail to build a path, we clear the CPU from the
460 * mask and continue with the rest. If ever we try to trace on those
461 * CPUs, we can handle it and fail the session.
462 */
463 for_each_cpu(cpu, mask) {
464 struct coresight_path *path;
465
466 path = etm_event_build_path(event, cpu, user_sink, sink);
467 if (!path) {
468 /*
469 * Failed to create a path for the CPU, clear it from
470 * the mask and continue to next one.
471 */
472 cpumask_clear_cpu(cpu, mask);
473 continue;
474 }
475
476 /*
477 * The first found sink is saved here and passed to
478 * etm_event_build_path() to check whether the remaining ETMs
479 * have a compatible default sink.
480 */
481 if (!user_sink && !sink)
482 sink = coresight_get_sink(path);
483
484 *etm_event_cpu_path_ptr(event_data, cpu) = path;
485 }
486
487 /* no sink found for any CPU - cannot trace */
488 if (!sink)
489 goto err;
490
491 /* If we don't have any CPUs ready for tracing, abort */
492 cpu = cpumask_first(mask);
493 if (cpu >= nr_cpu_ids)
494 goto err;
495
496 if (!sink_ops(sink)->alloc_buffer || !sink_ops(sink)->free_buffer)
497 goto err;
498
499 /*
500 * Allocate the sink buffer for this session. All the sinks
501 * where this event can be scheduled are ensured to be of the
502 * same type. Thus the same sink configuration is used by the
503 * sinks.
504 */
505 event_data->snk_config =
506 sink_ops(sink)->alloc_buffer(sink, event, pages,
507 nr_pages, overwrite);
508 if (!event_data->snk_config)
509 goto err;
510
511 out:
512 return event_data;
513
514 err:
515 etm_free_aux(event_data);
516 event_data = NULL;
517 goto out;
518 }
519
etm_event_resume(struct coresight_path * path)520 static int etm_event_resume(struct coresight_path *path)
521 {
522 struct coresight_device *source;
523 int ret;
524
525 if (!path)
526 return 0;
527
528 source = coresight_get_source(path);
529 if (!source)
530 return 0;
531
532 ret = coresight_resume_source(source);
533 if (ret < 0)
534 dev_err(&source->dev, "Failed to resume ETM event.\n");
535
536 return ret;
537 }
538
etm_event_start(struct perf_event * event,int flags)539 static void etm_event_start(struct perf_event *event, int flags)
540 {
541 int cpu = smp_processor_id();
542 struct etm_event_data *event_data;
543 struct etm_ctxt *ctxt = this_cpu_ptr(&etm_ctxt);
544 struct perf_output_handle *handle = &ctxt->handle;
545 struct coresight_device *source, *sink;
546 struct coresight_path *path;
547 u64 hw_id;
548
549 if (flags & PERF_EF_RESUME) {
550 path = etm_event_get_ctxt_path(ctxt);
551 if (etm_event_resume(path) < 0)
552 goto fail;
553 return;
554 }
555
556 /* Have we messed up our tracking ? */
557 if (WARN_ON(READ_ONCE(ctxt->event_data)))
558 goto fail;
559
560 /*
561 * Deal with the ring buffer API and get a handle on the
562 * session's information.
563 */
564 event_data = perf_aux_output_begin(handle, event);
565 if (!event_data)
566 goto fail;
567
568 /*
569 * Check if this ETM is allowed to trace, as decided
570 * at etm_setup_aux(). This could be due to an unreachable
571 * sink from this ETM. We can't do much in this case if
572 * the sink was specified or hinted to the driver. For
573 * now, simply don't record anything on this ETM.
574 *
575 * As such we pretend that everything is fine, and let
576 * it continue without actually tracing. The event could
577 * continue tracing when it moves to a CPU where it is
578 * reachable to a sink.
579 */
580 if (!cpumask_test_cpu(cpu, &event_data->mask))
581 goto out;
582
583 path = etm_event_cpu_path(event_data, cpu);
584 path->handle = handle;
585 /* We need source and sink, no need to continue if any is not set */
586 source = coresight_get_source(path);
587 sink = coresight_get_sink(path);
588 if (WARN_ON_ONCE(!source || !sink))
589 goto fail_end_stop;
590
591 /* Nothing will happen without a path */
592 if (coresight_enable_path(path, CS_MODE_PERF))
593 goto fail_end_stop;
594
595 /* Finally enable the tracer */
596 if (coresight_enable_source(source, event, CS_MODE_PERF, path))
597 goto fail_disable_path;
598
599 /*
600 * output cpu / trace ID in perf record, once for the lifetime
601 * of the event.
602 */
603 if (!cpumask_test_cpu(cpu, &event_data->aux_hwid_done)) {
604 cpumask_set_cpu(cpu, &event_data->aux_hwid_done);
605
606 hw_id = FIELD_PREP(CS_AUX_HW_ID_MAJOR_VERSION_MASK,
607 CS_AUX_HW_ID_MAJOR_VERSION);
608 hw_id |= FIELD_PREP(CS_AUX_HW_ID_MINOR_VERSION_MASK,
609 CS_AUX_HW_ID_MINOR_VERSION);
610 hw_id |= FIELD_PREP(CS_AUX_HW_ID_TRACE_ID_MASK, path->trace_id);
611 hw_id |= FIELD_PREP(CS_AUX_HW_ID_SINK_ID_MASK, coresight_get_sink_id(sink));
612
613 perf_report_aux_output_id(event, hw_id);
614 }
615
616 perf_event_itrace_started(event);
617
618 out:
619 /* Tell the perf core the event is alive */
620 event->hw.state = 0;
621 /* Save the event_data for this ETM */
622 WRITE_ONCE(ctxt->event_data, event_data);
623 return;
624
625 fail_disable_path:
626 coresight_disable_path(path);
627 fail_end_stop:
628 /*
629 * Check if the handle is still associated with the event,
630 * to handle cases where if the sink failed to start the
631 * trace and TRUNCATED the handle already.
632 */
633 if (READ_ONCE(handle->event)) {
634 perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
635 perf_aux_output_end(handle, 0);
636 }
637 fail:
638 event->hw.state = PERF_HES_STOPPED;
639 return;
640 }
641
etm_event_pause(struct coresight_path * path,struct perf_event * event,struct etm_ctxt * ctxt)642 static void etm_event_pause(struct coresight_path *path,
643 struct perf_event *event,
644 struct etm_ctxt *ctxt)
645 {
646 struct perf_output_handle *handle = &ctxt->handle;
647 struct coresight_device *source, *sink;
648 struct etm_event_data *event_data;
649 unsigned long size;
650
651 if (!path)
652 return;
653
654 source = coresight_get_source(path);
655 sink = coresight_get_sink(path);
656 if (WARN_ON_ONCE(!source || !sink))
657 return;
658
659 /* Stop tracer */
660 coresight_pause_source(source);
661
662 /*
663 * The per CPU sink has own interrupt handling, it might have
664 * race condition with updating buffer on AUX trace pause if
665 * it is invoked from NMI. To avoid the race condition,
666 * disallows updating buffer for the per CPU sink case.
667 */
668 if (coresight_is_percpu_sink(sink))
669 return;
670
671 if (WARN_ON_ONCE(handle->event != event))
672 return;
673
674 if (!sink_ops(sink)->update_buffer)
675 return;
676
677 event_data = READ_ONCE(ctxt->event_data);
678 size = sink_ops(sink)->update_buffer(sink, handle,
679 event_data->snk_config);
680 if (READ_ONCE(handle->event)) {
681 if (!size)
682 return;
683
684 perf_aux_output_end(handle, size);
685 perf_aux_output_begin(handle, event);
686 } else {
687 WARN_ON_ONCE(size);
688 }
689 }
690
etm_event_stop(struct perf_event * event,int mode)691 static void etm_event_stop(struct perf_event *event, int mode)
692 {
693 int cpu = smp_processor_id();
694 unsigned long size;
695 struct coresight_device *source, *sink;
696 struct etm_ctxt *ctxt = this_cpu_ptr(&etm_ctxt);
697 struct perf_output_handle *handle = &ctxt->handle;
698 struct coresight_path *path = etm_event_get_ctxt_path(ctxt);
699 struct etm_event_data *event_data;
700
701 if (mode & PERF_EF_PAUSE)
702 return etm_event_pause(path, event, ctxt);
703
704 /*
705 * If we still have access to the event_data via handle,
706 * confirm that we haven't messed up the tracking.
707 */
708 if (handle->event &&
709 WARN_ON(perf_get_aux(handle) != ctxt->event_data))
710 return;
711
712 event_data = READ_ONCE(ctxt->event_data);
713 /* Clear the event_data as this ETM is stopping the trace. */
714 WRITE_ONCE(ctxt->event_data, NULL);
715
716 if (event->hw.state == PERF_HES_STOPPED)
717 return;
718
719 /* We must have a valid event_data for a running event */
720 if (WARN_ON(!event_data))
721 return;
722
723 /*
724 * Check if this ETM was allowed to trace, as decided at
725 * etm_setup_aux(). If it wasn't allowed to trace, then
726 * nothing needs to be torn down other than outputting a
727 * zero sized record.
728 */
729 if (handle->event && (mode & PERF_EF_UPDATE) &&
730 !cpumask_test_cpu(cpu, &event_data->mask)) {
731 event->hw.state = PERF_HES_STOPPED;
732 perf_aux_output_end(handle, 0);
733 return;
734 }
735
736 source = coresight_get_source(path);
737 sink = coresight_get_sink(path);
738 if (!source || !sink)
739 return;
740
741 /* stop tracer */
742 coresight_disable_source(source, event);
743
744 /* tell the core */
745 event->hw.state = PERF_HES_STOPPED;
746
747 /*
748 * If the handle is not bound to an event anymore
749 * (e.g, the sink driver was unable to restart the
750 * handle due to lack of buffer space), we don't
751 * have to do anything here.
752 */
753 if (handle->event && (mode & PERF_EF_UPDATE)) {
754 if (WARN_ON_ONCE(handle->event != event))
755 return;
756
757 /* update trace information */
758 if (!sink_ops(sink)->update_buffer)
759 return;
760
761 size = sink_ops(sink)->update_buffer(sink, handle,
762 event_data->snk_config);
763 /*
764 * Make sure the handle is still valid as the
765 * sink could have closed it from an IRQ.
766 * The sink driver must handle the race with
767 * update_buffer() and IRQ. Thus either we
768 * should get a valid handle and valid size
769 * (which may be 0).
770 *
771 * But we should never get a non-zero size with
772 * an invalid handle.
773 */
774 if (READ_ONCE(handle->event))
775 perf_aux_output_end(handle, size);
776 else
777 WARN_ON(size);
778 }
779
780 /* Disabling the path make its elements available to other sessions */
781 coresight_disable_path(path);
782 }
783
etm_event_add(struct perf_event * event,int mode)784 static int etm_event_add(struct perf_event *event, int mode)
785 {
786 int ret = 0;
787 struct hw_perf_event *hwc = &event->hw;
788
789 if (mode & PERF_EF_START) {
790 etm_event_start(event, 0);
791 if (hwc->state & PERF_HES_STOPPED)
792 ret = -EINVAL;
793 } else {
794 hwc->state = PERF_HES_STOPPED;
795 }
796
797 return ret;
798 }
799
etm_event_del(struct perf_event * event,int mode)800 static void etm_event_del(struct perf_event *event, int mode)
801 {
802 etm_event_stop(event, PERF_EF_UPDATE);
803 }
804
etm_addr_filters_validate(struct list_head * filters)805 static int etm_addr_filters_validate(struct list_head *filters)
806 {
807 bool range = false, address = false;
808 int index = 0;
809 struct perf_addr_filter *filter;
810
811 list_for_each_entry(filter, filters, entry) {
812 /*
813 * No need to go further if there's no more
814 * room for filters.
815 */
816 if (++index > ETM_ADDR_CMP_MAX)
817 return -EOPNOTSUPP;
818
819 /* filter::size==0 means single address trigger */
820 if (filter->size) {
821 /*
822 * The existing code relies on START/STOP filters
823 * being address filters.
824 */
825 if (filter->action == PERF_ADDR_FILTER_ACTION_START ||
826 filter->action == PERF_ADDR_FILTER_ACTION_STOP)
827 return -EOPNOTSUPP;
828
829 range = true;
830 } else
831 address = true;
832
833 /*
834 * At this time we don't allow range and start/stop filtering
835 * to cohabitate, they have to be mutually exclusive.
836 */
837 if (range && address)
838 return -EOPNOTSUPP;
839 }
840
841 return 0;
842 }
843
etm_addr_filters_sync(struct perf_event * event)844 static void etm_addr_filters_sync(struct perf_event *event)
845 {
846 struct perf_addr_filters_head *head = perf_event_addr_filters(event);
847 unsigned long start, stop;
848 struct perf_addr_filter_range *fr = event->addr_filter_ranges;
849 struct etm_filters *filters = event->hw.addr_filters;
850 struct etm_filter *etm_filter;
851 struct perf_addr_filter *filter;
852 int i = 0;
853
854 list_for_each_entry(filter, &head->list, entry) {
855 start = fr[i].start;
856 stop = start + fr[i].size;
857 etm_filter = &filters->etm_filter[i];
858
859 switch (filter->action) {
860 case PERF_ADDR_FILTER_ACTION_FILTER:
861 etm_filter->start_addr = start;
862 etm_filter->stop_addr = stop;
863 etm_filter->type = ETM_ADDR_TYPE_RANGE;
864 break;
865 case PERF_ADDR_FILTER_ACTION_START:
866 etm_filter->start_addr = start;
867 etm_filter->type = ETM_ADDR_TYPE_START;
868 break;
869 case PERF_ADDR_FILTER_ACTION_STOP:
870 etm_filter->stop_addr = stop;
871 etm_filter->type = ETM_ADDR_TYPE_STOP;
872 break;
873 }
874 i++;
875 }
876
877 filters->nr_filters = i;
878 }
879
etm_perf_symlink(struct coresight_device * csdev,bool link)880 int etm_perf_symlink(struct coresight_device *csdev, bool link)
881 {
882 char entry[sizeof("cpu9999999")];
883 int ret = 0, cpu = csdev->cpu;
884 struct device *pmu_dev = etm_pmu.dev;
885 struct device *cs_dev = &csdev->dev;
886
887 sprintf(entry, "cpu%d", cpu);
888
889 if (!etm_perf_up)
890 return -EPROBE_DEFER;
891
892 if (link)
893 ret = sysfs_create_link(&pmu_dev->kobj, &cs_dev->kobj, entry);
894 else
895 sysfs_remove_link(&pmu_dev->kobj, entry);
896
897 return ret;
898 }
899 EXPORT_SYMBOL_GPL(etm_perf_symlink);
900
etm_perf_sink_name_show(struct device * dev,struct device_attribute * dattr,char * buf)901 static ssize_t etm_perf_sink_name_show(struct device *dev,
902 struct device_attribute *dattr,
903 char *buf)
904 {
905 struct dev_ext_attribute *ea;
906
907 ea = container_of(dattr, struct dev_ext_attribute, attr);
908 return scnprintf(buf, PAGE_SIZE, "0x%px\n", ea->var);
909 }
910
911 static struct dev_ext_attribute *
etm_perf_add_symlink_group(struct device * dev,const char * name,const char * group_name)912 etm_perf_add_symlink_group(struct device *dev, const char *name, const char *group_name)
913 {
914 struct dev_ext_attribute *ea;
915 unsigned long hash;
916 int ret;
917 struct device *pmu_dev = etm_pmu.dev;
918
919 if (!etm_perf_up)
920 return ERR_PTR(-EPROBE_DEFER);
921
922 ea = devm_kzalloc(dev, sizeof(*ea), GFP_KERNEL);
923 if (!ea)
924 return ERR_PTR(-ENOMEM);
925
926 /*
927 * If this function is called adding a sink then the hash is used for
928 * sink selection - see function coresight_get_sink_by_id().
929 * If adding a configuration then the hash is used for selection in
930 * cscfg_activate_config()
931 */
932 hash = hashlen_hash(hashlen_string(NULL, name));
933
934 sysfs_attr_init(&ea->attr.attr);
935 ea->attr.attr.name = devm_kstrdup(dev, name, GFP_KERNEL);
936 if (!ea->attr.attr.name)
937 return ERR_PTR(-ENOMEM);
938
939 ea->attr.attr.mode = 0444;
940 ea->var = (unsigned long *)hash;
941
942 ret = sysfs_add_file_to_group(&pmu_dev->kobj,
943 &ea->attr.attr, group_name);
944
945 return ret ? ERR_PTR(ret) : ea;
946 }
947
etm_perf_add_symlink_sink(struct coresight_device * csdev)948 int etm_perf_add_symlink_sink(struct coresight_device *csdev)
949 {
950 const char *name;
951 struct device *dev = &csdev->dev;
952 int err = 0;
953
954 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
955 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
956 return -EOPNOTSUPP;
957
958 if (!sink_ops(csdev)->alloc_buffer)
959 return -EOPNOTSUPP;
960
961 if (csdev->ea != NULL)
962 return -EINVAL;
963
964 name = dev_name(dev);
965 csdev->ea = etm_perf_add_symlink_group(dev, name, "sinks");
966 if (IS_ERR(csdev->ea)) {
967 err = PTR_ERR(csdev->ea);
968 csdev->ea = NULL;
969 } else
970 csdev->ea->attr.show = etm_perf_sink_name_show;
971
972 return err;
973 }
974
etm_perf_del_symlink_group(struct dev_ext_attribute * ea,const char * group_name)975 static void etm_perf_del_symlink_group(struct dev_ext_attribute *ea, const char *group_name)
976 {
977 struct device *pmu_dev = etm_pmu.dev;
978
979 sysfs_remove_file_from_group(&pmu_dev->kobj,
980 &ea->attr.attr, group_name);
981 }
982
etm_perf_del_symlink_sink(struct coresight_device * csdev)983 void etm_perf_del_symlink_sink(struct coresight_device *csdev)
984 {
985 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
986 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
987 return;
988
989 if (!csdev->ea)
990 return;
991
992 etm_perf_del_symlink_group(csdev->ea, "sinks");
993 csdev->ea = NULL;
994 }
995
etm_perf_cscfg_event_show(struct device * dev,struct device_attribute * dattr,char * buf)996 static ssize_t etm_perf_cscfg_event_show(struct device *dev,
997 struct device_attribute *dattr,
998 char *buf)
999 {
1000 struct dev_ext_attribute *ea;
1001
1002 ea = container_of(dattr, struct dev_ext_attribute, attr);
1003 return scnprintf(buf, PAGE_SIZE, "configid=0x%px\n", ea->var);
1004 }
1005
etm_perf_add_symlink_cscfg(struct device * dev,struct cscfg_config_desc * config_desc)1006 int etm_perf_add_symlink_cscfg(struct device *dev, struct cscfg_config_desc *config_desc)
1007 {
1008 int err = 0;
1009
1010 if (config_desc->event_ea != NULL)
1011 return 0;
1012
1013 config_desc->event_ea = etm_perf_add_symlink_group(dev, config_desc->name, "events");
1014
1015 /* set the show function to the custom cscfg event */
1016 if (!IS_ERR(config_desc->event_ea))
1017 config_desc->event_ea->attr.show = etm_perf_cscfg_event_show;
1018 else {
1019 err = PTR_ERR(config_desc->event_ea);
1020 config_desc->event_ea = NULL;
1021 }
1022
1023 return err;
1024 }
1025
etm_perf_del_symlink_cscfg(struct cscfg_config_desc * config_desc)1026 void etm_perf_del_symlink_cscfg(struct cscfg_config_desc *config_desc)
1027 {
1028 if (!config_desc->event_ea)
1029 return;
1030
1031 etm_perf_del_symlink_group(config_desc->event_ea, "events");
1032 config_desc->event_ea = NULL;
1033 }
1034
etm_perf_init(void)1035 int __init etm_perf_init(void)
1036 {
1037 int ret;
1038
1039 etm_pmu.capabilities = (PERF_PMU_CAP_EXCLUSIVE |
1040 PERF_PMU_CAP_ITRACE |
1041 PERF_PMU_CAP_AUX_PAUSE);
1042
1043 etm_pmu.attr_groups = etm_pmu_attr_groups;
1044 etm_pmu.task_ctx_nr = perf_sw_context;
1045 etm_pmu.read = etm_event_read;
1046 etm_pmu.event_init = etm_event_init;
1047 etm_pmu.setup_aux = etm_setup_aux;
1048 etm_pmu.free_aux = etm_free_aux;
1049 etm_pmu.start = etm_event_start;
1050 etm_pmu.stop = etm_event_stop;
1051 etm_pmu.add = etm_event_add;
1052 etm_pmu.del = etm_event_del;
1053 etm_pmu.addr_filters_sync = etm_addr_filters_sync;
1054 etm_pmu.addr_filters_validate = etm_addr_filters_validate;
1055 etm_pmu.nr_addr_filters = ETM_ADDR_CMP_MAX;
1056 etm_pmu.module = THIS_MODULE;
1057
1058 ret = perf_pmu_register(&etm_pmu, CORESIGHT_ETM_PMU_NAME, -1);
1059 if (ret == 0)
1060 etm_perf_up = true;
1061
1062 return ret;
1063 }
1064
etm_perf_exit(void)1065 void etm_perf_exit(void)
1066 {
1067 perf_pmu_unregister(&etm_pmu);
1068 }
1069