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 <linux/coresight.h>
19 #include <linux/coresight-pmu.h>
20 #include <linux/cpumask.h>
21 #include <linux/device.h>
22 #include <linux/list.h>
24 #include <linux/init.h>
25 #include <linux/perf_event.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
28 #include <linux/workqueue.h>
30 #include "coresight-priv.h"
32 static struct pmu etm_pmu
;
33 static bool etm_perf_up
;
36 * struct etm_event_data - Coresight specifics associated to an event
37 * @work: Handle to free allocated memory outside IRQ context.
38 * @mask: Hold the CPU(s) this event was set for.
39 * @snk_config: The sink configuration.
40 * @path: An array of path, each slot for one CPU.
42 struct etm_event_data
{
43 struct work_struct work
;
46 struct list_head
**path
;
49 static DEFINE_PER_CPU(struct perf_output_handle
, ctx_handle
);
50 static DEFINE_PER_CPU(struct coresight_device
*, csdev_src
);
52 /* ETMv3.5/PTM's ETMCR is 'config' */
53 PMU_FORMAT_ATTR(cycacc
, "config:" __stringify(ETM_OPT_CYCACC
));
54 PMU_FORMAT_ATTR(timestamp
, "config:" __stringify(ETM_OPT_TS
));
56 static struct attribute
*etm_config_formats_attr
[] = {
57 &format_attr_cycacc
.attr
,
58 &format_attr_timestamp
.attr
,
62 static struct attribute_group etm_pmu_format_group
= {
64 .attrs
= etm_config_formats_attr
,
67 static const struct attribute_group
*etm_pmu_attr_groups
[] = {
68 &etm_pmu_format_group
,
72 static void etm_event_read(struct perf_event
*event
) {}
74 static int etm_event_init(struct perf_event
*event
)
76 if (event
->attr
.type
!= etm_pmu
.type
)
82 static void free_event_data(struct work_struct
*work
)
86 struct etm_event_data
*event_data
;
87 struct coresight_device
*sink
;
89 event_data
= container_of(work
, struct etm_event_data
, work
);
90 mask
= &event_data
->mask
;
92 * First deal with the sink configuration. See comment in
93 * etm_setup_aux() about why we take the first available path.
95 if (event_data
->snk_config
) {
96 cpu
= cpumask_first(mask
);
97 sink
= coresight_get_sink(event_data
->path
[cpu
]);
98 if (sink_ops(sink
)->free_buffer
)
99 sink_ops(sink
)->free_buffer(event_data
->snk_config
);
102 for_each_cpu(cpu
, mask
) {
103 if (event_data
->path
[cpu
])
104 coresight_release_path(event_data
->path
[cpu
]);
107 kfree(event_data
->path
);
111 static void *alloc_event_data(int cpu
)
115 struct etm_event_data
*event_data
;
117 /* First get memory for the session's data */
118 event_data
= kzalloc(sizeof(struct etm_event_data
), GFP_KERNEL
);
122 /* Make sure nothing disappears under us */
124 size
= num_online_cpus();
126 mask
= &event_data
->mask
;
128 cpumask_set_cpu(cpu
, mask
);
130 cpumask_copy(mask
, cpu_online_mask
);
134 * Each CPU has a single path between source and destination. As such
135 * allocate an array using CPU numbers as indexes. That way a path
136 * for any CPU can easily be accessed at any given time. We proceed
137 * the same way for sessions involving a single CPU. The cost of
138 * unused memory when dealing with single CPU trace scenarios is small
139 * compared to the cost of searching through an optimized array.
141 event_data
->path
= kcalloc(size
,
142 sizeof(struct list_head
*), GFP_KERNEL
);
143 if (!event_data
->path
) {
151 static void etm_free_aux(void *data
)
153 struct etm_event_data
*event_data
= data
;
155 schedule_work(&event_data
->work
);
158 static void *etm_setup_aux(int event_cpu
, void **pages
,
159 int nr_pages
, bool overwrite
)
163 struct coresight_device
*sink
;
164 struct etm_event_data
*event_data
= NULL
;
166 event_data
= alloc_event_data(event_cpu
);
170 INIT_WORK(&event_data
->work
, free_event_data
);
172 mask
= &event_data
->mask
;
174 /* Setup the path for each CPU in a trace session */
175 for_each_cpu(cpu
, mask
) {
176 struct coresight_device
*csdev
;
178 csdev
= per_cpu(csdev_src
, cpu
);
183 * Building a path doesn't enable it, it simply builds a
184 * list of devices from source to sink that can be
185 * referenced later when the path is actually needed.
187 event_data
->path
[cpu
] = coresight_build_path(csdev
);
188 if (!event_data
->path
[cpu
])
193 * In theory nothing prevent tracers in a trace session from being
194 * associated with different sinks, nor having a sink per tracer. But
195 * until we have HW with this kind of topology and a way to convey
196 * sink assignement from the perf cmd line we need to assume tracers
197 * in a trace session are using the same sink. Therefore pick the sink
198 * found at the end of the first available path.
200 cpu
= cpumask_first(mask
);
201 /* Grab the sink at the end of the path */
202 sink
= coresight_get_sink(event_data
->path
[cpu
]);
206 if (!sink_ops(sink
)->alloc_buffer
)
209 /* Get the AUX specific data from the sink buffer */
210 event_data
->snk_config
=
211 sink_ops(sink
)->alloc_buffer(sink
, cpu
, pages
,
212 nr_pages
, overwrite
);
213 if (!event_data
->snk_config
)
220 etm_free_aux(event_data
);
225 static void etm_event_start(struct perf_event
*event
, int flags
)
227 int cpu
= smp_processor_id();
228 struct etm_event_data
*event_data
;
229 struct perf_output_handle
*handle
= this_cpu_ptr(&ctx_handle
);
230 struct coresight_device
*sink
, *csdev
= per_cpu(csdev_src
, cpu
);
236 * Deal with the ring buffer API and get a handle on the
237 * session's information.
239 event_data
= perf_aux_output_begin(handle
, event
);
243 /* We need a sink, no need to continue without one */
244 sink
= coresight_get_sink(event_data
->path
[cpu
]);
245 if (WARN_ON_ONCE(!sink
|| !sink_ops(sink
)->set_buffer
))
248 /* Configure the sink */
249 if (sink_ops(sink
)->set_buffer(sink
, handle
,
250 event_data
->snk_config
))
253 /* Nothing will happen without a path */
254 if (coresight_enable_path(event_data
->path
[cpu
], CS_MODE_PERF
))
257 /* Tell the perf core the event is alive */
260 /* Finally enable the tracer */
261 if (source_ops(csdev
)->enable(csdev
, &event
->attr
, CS_MODE_PERF
))
268 perf_aux_output_end(handle
, 0, true);
270 event
->hw
.state
= PERF_HES_STOPPED
;
274 static void etm_event_stop(struct perf_event
*event
, int mode
)
277 int cpu
= smp_processor_id();
279 struct coresight_device
*sink
, *csdev
= per_cpu(csdev_src
, cpu
);
280 struct perf_output_handle
*handle
= this_cpu_ptr(&ctx_handle
);
281 struct etm_event_data
*event_data
= perf_get_aux(handle
);
283 if (event
->hw
.state
== PERF_HES_STOPPED
)
289 sink
= coresight_get_sink(event_data
->path
[cpu
]);
294 source_ops(csdev
)->disable(csdev
);
297 event
->hw
.state
= PERF_HES_STOPPED
;
299 if (mode
& PERF_EF_UPDATE
) {
300 if (WARN_ON_ONCE(handle
->event
!= event
))
303 /* update trace information */
304 if (!sink_ops(sink
)->update_buffer
)
307 sink_ops(sink
)->update_buffer(sink
, handle
,
308 event_data
->snk_config
);
310 if (!sink_ops(sink
)->reset_buffer
)
313 size
= sink_ops(sink
)->reset_buffer(sink
, handle
,
314 event_data
->snk_config
,
317 perf_aux_output_end(handle
, size
, lost
);
320 /* Disabling the path make its elements available to other sessions */
321 coresight_disable_path(event_data
->path
[cpu
]);
324 static int etm_event_add(struct perf_event
*event
, int mode
)
327 struct hw_perf_event
*hwc
= &event
->hw
;
329 if (mode
& PERF_EF_START
) {
330 etm_event_start(event
, 0);
331 if (hwc
->state
& PERF_HES_STOPPED
)
334 hwc
->state
= PERF_HES_STOPPED
;
340 static void etm_event_del(struct perf_event
*event
, int mode
)
342 etm_event_stop(event
, PERF_EF_UPDATE
);
345 int etm_perf_symlink(struct coresight_device
*csdev
, bool link
)
347 char entry
[sizeof("cpu9999999")];
348 int ret
= 0, cpu
= source_ops(csdev
)->cpu_id(csdev
);
349 struct device
*pmu_dev
= etm_pmu
.dev
;
350 struct device
*cs_dev
= &csdev
->dev
;
352 sprintf(entry
, "cpu%d", cpu
);
355 return -EPROBE_DEFER
;
358 ret
= sysfs_create_link(&pmu_dev
->kobj
, &cs_dev
->kobj
, entry
);
361 per_cpu(csdev_src
, cpu
) = csdev
;
363 sysfs_remove_link(&pmu_dev
->kobj
, entry
);
364 per_cpu(csdev_src
, cpu
) = NULL
;
370 static int __init
etm_perf_init(void)
374 etm_pmu
.capabilities
= PERF_PMU_CAP_EXCLUSIVE
;
376 etm_pmu
.attr_groups
= etm_pmu_attr_groups
;
377 etm_pmu
.task_ctx_nr
= perf_sw_context
;
378 etm_pmu
.read
= etm_event_read
;
379 etm_pmu
.event_init
= etm_event_init
;
380 etm_pmu
.setup_aux
= etm_setup_aux
;
381 etm_pmu
.free_aux
= etm_free_aux
;
382 etm_pmu
.start
= etm_event_start
;
383 etm_pmu
.stop
= etm_event_stop
;
384 etm_pmu
.add
= etm_event_add
;
385 etm_pmu
.del
= etm_event_del
;
387 ret
= perf_pmu_register(&etm_pmu
, CORESIGHT_ETM_PMU_NAME
, -1);
393 device_initcall(etm_perf_init
);