blob: df63315369386a6f74db16f3488ea5b5bd822a75 [file] [log] [blame]
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -07001/*
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 <linux/coresight.h>
19#include <linux/coresight-pmu.h>
20#include <linux/cpumask.h>
21#include <linux/device.h>
22#include <linux/list.h>
23#include <linux/mm.h>
Paul Gortmakerca48fa22016-02-27 15:21:47 -050024#include <linux/init.h>
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -070025#include <linux/perf_event.h>
26#include <linux/slab.h>
27#include <linux/types.h>
28#include <linux/workqueue.h>
29
Mathieu Poirierca878b12016-08-25 15:19:12 -060030#include "coresight-etm-perf.h"
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -070031#include "coresight-priv.h"
32
33static struct pmu etm_pmu;
34static bool etm_perf_up;
35
36/**
37 * struct etm_event_data - Coresight specifics associated to an event
38 * @work: Handle to free allocated memory outside IRQ context.
39 * @mask: Hold the CPU(s) this event was set for.
40 * @snk_config: The sink configuration.
41 * @path: An array of path, each slot for one CPU.
42 */
43struct etm_event_data {
44 struct work_struct work;
45 cpumask_t mask;
46 void *snk_config;
47 struct list_head **path;
48};
49
50static DEFINE_PER_CPU(struct perf_output_handle, ctx_handle);
51static DEFINE_PER_CPU(struct coresight_device *, csdev_src);
52
53/* ETMv3.5/PTM's ETMCR is 'config' */
54PMU_FORMAT_ATTR(cycacc, "config:" __stringify(ETM_OPT_CYCACC));
55PMU_FORMAT_ATTR(timestamp, "config:" __stringify(ETM_OPT_TS));
56
57static struct attribute *etm_config_formats_attr[] = {
58 &format_attr_cycacc.attr,
59 &format_attr_timestamp.attr,
60 NULL,
61};
62
63static struct attribute_group etm_pmu_format_group = {
64 .name = "format",
65 .attrs = etm_config_formats_attr,
66};
67
68static const struct attribute_group *etm_pmu_attr_groups[] = {
69 &etm_pmu_format_group,
70 NULL,
71};
72
73static void etm_event_read(struct perf_event *event) {}
74
Mathieu Poirierca878b12016-08-25 15:19:12 -060075static int etm_addr_filters_alloc(struct perf_event *event)
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -070076{
Mathieu Poirierca878b12016-08-25 15:19:12 -060077 struct etm_filters *filters;
78 int node = event->cpu == -1 ? -1 : cpu_to_node(event->cpu);
79
80 filters = kzalloc_node(sizeof(struct etm_filters), GFP_KERNEL, node);
81 if (!filters)
82 return -ENOMEM;
83
84 if (event->parent)
85 memcpy(filters, event->parent->hw.addr_filters,
86 sizeof(*filters));
87
88 event->hw.addr_filters = filters;
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -070089
90 return 0;
91}
92
Mathieu Poirierca878b12016-08-25 15:19:12 -060093static void etm_event_destroy(struct perf_event *event)
94{
95 kfree(event->hw.addr_filters);
96 event->hw.addr_filters = NULL;
97}
98
99static int etm_event_init(struct perf_event *event)
100{
101 int ret = 0;
102
103 if (event->attr.type != etm_pmu.type) {
104 ret = -ENOENT;
105 goto out;
106 }
107
108 ret = etm_addr_filters_alloc(event);
109 if (ret)
110 goto out;
111
112 event->destroy = etm_event_destroy;
113out:
114 return ret;
115}
116
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700117static void free_event_data(struct work_struct *work)
118{
119 int cpu;
120 cpumask_t *mask;
121 struct etm_event_data *event_data;
122 struct coresight_device *sink;
123
124 event_data = container_of(work, struct etm_event_data, work);
125 mask = &event_data->mask;
126 /*
127 * First deal with the sink configuration. See comment in
128 * etm_setup_aux() about why we take the first available path.
129 */
130 if (event_data->snk_config) {
131 cpu = cpumask_first(mask);
132 sink = coresight_get_sink(event_data->path[cpu]);
133 if (sink_ops(sink)->free_buffer)
134 sink_ops(sink)->free_buffer(event_data->snk_config);
135 }
136
137 for_each_cpu(cpu, mask) {
Mathieu Poirier8e67cdb2016-09-08 16:50:38 -0600138 if (!(IS_ERR_OR_NULL(event_data->path[cpu])))
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700139 coresight_release_path(event_data->path[cpu]);
140 }
141
142 kfree(event_data->path);
143 kfree(event_data);
144}
145
146static void *alloc_event_data(int cpu)
147{
148 int size;
149 cpumask_t *mask;
150 struct etm_event_data *event_data;
151
152 /* First get memory for the session's data */
153 event_data = kzalloc(sizeof(struct etm_event_data), GFP_KERNEL);
154 if (!event_data)
155 return NULL;
156
157 /* Make sure nothing disappears under us */
158 get_online_cpus();
159 size = num_online_cpus();
160
161 mask = &event_data->mask;
162 if (cpu != -1)
163 cpumask_set_cpu(cpu, mask);
164 else
165 cpumask_copy(mask, cpu_online_mask);
166 put_online_cpus();
167
168 /*
169 * Each CPU has a single path between source and destination. As such
170 * allocate an array using CPU numbers as indexes. That way a path
171 * for any CPU can easily be accessed at any given time. We proceed
172 * the same way for sessions involving a single CPU. The cost of
173 * unused memory when dealing with single CPU trace scenarios is small
174 * compared to the cost of searching through an optimized array.
175 */
176 event_data->path = kcalloc(size,
177 sizeof(struct list_head *), GFP_KERNEL);
178 if (!event_data->path) {
179 kfree(event_data);
180 return NULL;
181 }
182
183 return event_data;
184}
185
186static void etm_free_aux(void *data)
187{
188 struct etm_event_data *event_data = data;
189
190 schedule_work(&event_data->work);
191}
192
193static void *etm_setup_aux(int event_cpu, void **pages,
194 int nr_pages, bool overwrite)
195{
196 int cpu;
197 cpumask_t *mask;
198 struct coresight_device *sink;
199 struct etm_event_data *event_data = NULL;
200
201 event_data = alloc_event_data(event_cpu);
202 if (!event_data)
203 return NULL;
Suzuki K Poulose0d968ed12017-06-05 14:15:04 -0600204 INIT_WORK(&event_data->work, free_event_data);
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700205
Mathieu Poirierebb45d72016-11-29 09:47:14 -0700206 /*
207 * In theory nothing prevent tracers in a trace session from being
208 * associated with different sinks, nor having a sink per tracer. But
209 * until we have HW with this kind of topology we need to assume tracers
210 * in a trace session are using the same sink. Therefore go through
211 * the coresight bus and pick the first enabled sink.
212 *
213 * When operated from sysFS users are responsible to enable the sink
214 * while from perf, the perf tools will do it based on the choice made
215 * on the cmd line. As such the "enable_sink" flag in sysFS is reset.
216 */
217 sink = coresight_get_enabled_sink(true);
218 if (!sink)
Quentin Lambert1576bec2016-11-29 09:47:19 -0700219 goto err;
Mathieu Poirierebb45d72016-11-29 09:47:14 -0700220
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700221 mask = &event_data->mask;
222
223 /* Setup the path for each CPU in a trace session */
224 for_each_cpu(cpu, mask) {
225 struct coresight_device *csdev;
226
227 csdev = per_cpu(csdev_src, cpu);
228 if (!csdev)
229 goto err;
230
231 /*
232 * Building a path doesn't enable it, it simply builds a
233 * list of devices from source to sink that can be
234 * referenced later when the path is actually needed.
235 */
Mathieu Poirierebb45d72016-11-29 09:47:14 -0700236 event_data->path[cpu] = coresight_build_path(csdev, sink);
Mathieu Poirier8e67cdb2016-09-08 16:50:38 -0600237 if (IS_ERR(event_data->path[cpu]))
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700238 goto err;
239 }
240
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700241 if (!sink_ops(sink)->alloc_buffer)
242 goto err;
243
Wang Nan61be6a12017-01-23 10:41:22 -0700244 cpu = cpumask_first(mask);
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700245 /* Get the AUX specific data from the sink buffer */
246 event_data->snk_config =
247 sink_ops(sink)->alloc_buffer(sink, cpu, pages,
248 nr_pages, overwrite);
249 if (!event_data->snk_config)
250 goto err;
251
252out:
253 return event_data;
254
255err:
256 etm_free_aux(event_data);
257 event_data = NULL;
258 goto out;
259}
260
261static void etm_event_start(struct perf_event *event, int flags)
262{
263 int cpu = smp_processor_id();
264 struct etm_event_data *event_data;
265 struct perf_output_handle *handle = this_cpu_ptr(&ctx_handle);
266 struct coresight_device *sink, *csdev = per_cpu(csdev_src, cpu);
267
268 if (!csdev)
269 goto fail;
270
271 /*
272 * Deal with the ring buffer API and get a handle on the
273 * session's information.
274 */
275 event_data = perf_aux_output_begin(handle, event);
276 if (!event_data)
277 goto fail;
278
279 /* We need a sink, no need to continue without one */
280 sink = coresight_get_sink(event_data->path[cpu]);
281 if (WARN_ON_ONCE(!sink || !sink_ops(sink)->set_buffer))
282 goto fail_end_stop;
283
284 /* Configure the sink */
285 if (sink_ops(sink)->set_buffer(sink, handle,
286 event_data->snk_config))
287 goto fail_end_stop;
288
289 /* Nothing will happen without a path */
290 if (coresight_enable_path(event_data->path[cpu], CS_MODE_PERF))
291 goto fail_end_stop;
292
293 /* Tell the perf core the event is alive */
294 event->hw.state = 0;
295
296 /* Finally enable the tracer */
Mathieu Poirier68905d72016-08-25 15:19:10 -0600297 if (source_ops(csdev)->enable(csdev, event, CS_MODE_PERF))
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700298 goto fail_end_stop;
299
300out:
301 return;
302
303fail_end_stop:
304 perf_aux_output_end(handle, 0, true);
305fail:
306 event->hw.state = PERF_HES_STOPPED;
307 goto out;
308}
309
310static void etm_event_stop(struct perf_event *event, int mode)
311{
312 bool lost;
313 int cpu = smp_processor_id();
314 unsigned long size;
315 struct coresight_device *sink, *csdev = per_cpu(csdev_src, cpu);
316 struct perf_output_handle *handle = this_cpu_ptr(&ctx_handle);
317 struct etm_event_data *event_data = perf_get_aux(handle);
318
319 if (event->hw.state == PERF_HES_STOPPED)
320 return;
321
322 if (!csdev)
323 return;
324
325 sink = coresight_get_sink(event_data->path[cpu]);
326 if (!sink)
327 return;
328
329 /* stop tracer */
Mathieu Poirier68905d72016-08-25 15:19:10 -0600330 source_ops(csdev)->disable(csdev, event);
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700331
332 /* tell the core */
333 event->hw.state = PERF_HES_STOPPED;
334
335 if (mode & PERF_EF_UPDATE) {
336 if (WARN_ON_ONCE(handle->event != event))
337 return;
338
339 /* update trace information */
340 if (!sink_ops(sink)->update_buffer)
341 return;
342
343 sink_ops(sink)->update_buffer(sink, handle,
344 event_data->snk_config);
345
346 if (!sink_ops(sink)->reset_buffer)
347 return;
348
349 size = sink_ops(sink)->reset_buffer(sink, handle,
350 event_data->snk_config,
351 &lost);
352
353 perf_aux_output_end(handle, size, lost);
354 }
355
356 /* Disabling the path make its elements available to other sessions */
357 coresight_disable_path(event_data->path[cpu]);
358}
359
360static int etm_event_add(struct perf_event *event, int mode)
361{
362 int ret = 0;
363 struct hw_perf_event *hwc = &event->hw;
364
365 if (mode & PERF_EF_START) {
366 etm_event_start(event, 0);
367 if (hwc->state & PERF_HES_STOPPED)
368 ret = -EINVAL;
369 } else {
370 hwc->state = PERF_HES_STOPPED;
371 }
372
373 return ret;
374}
375
376static void etm_event_del(struct perf_event *event, int mode)
377{
378 etm_event_stop(event, PERF_EF_UPDATE);
379}
380
Mathieu Poirierca878b12016-08-25 15:19:12 -0600381static int etm_addr_filters_validate(struct list_head *filters)
382{
383 bool range = false, address = false;
384 int index = 0;
385 struct perf_addr_filter *filter;
386
387 list_for_each_entry(filter, filters, entry) {
388 /*
389 * No need to go further if there's no more
390 * room for filters.
391 */
392 if (++index > ETM_ADDR_CMP_MAX)
393 return -EOPNOTSUPP;
394
395 /*
396 * As taken from the struct perf_addr_filter documentation:
397 * @range: 1: range, 0: address
398 *
399 * At this time we don't allow range and start/stop filtering
400 * to cohabitate, they have to be mutually exclusive.
401 */
402 if ((filter->range == 1) && address)
403 return -EOPNOTSUPP;
404
405 if ((filter->range == 0) && range)
406 return -EOPNOTSUPP;
407
408 /*
409 * For range filtering, the second address in the address
410 * range comparator needs to be higher than the first.
411 * Invalid otherwise.
412 */
413 if (filter->range && filter->size == 0)
414 return -EINVAL;
415
416 /*
417 * Everything checks out with this filter, record what we've
418 * received before moving on to the next one.
419 */
420 if (filter->range)
421 range = true;
422 else
423 address = true;
424 }
425
426 return 0;
427}
428
429static void etm_addr_filters_sync(struct perf_event *event)
430{
431 struct perf_addr_filters_head *head = perf_event_addr_filters(event);
432 unsigned long start, stop, *offs = event->addr_filters_offs;
433 struct etm_filters *filters = event->hw.addr_filters;
434 struct etm_filter *etm_filter;
435 struct perf_addr_filter *filter;
436 int i = 0;
437
438 list_for_each_entry(filter, &head->list, entry) {
439 start = filter->offset + offs[i];
440 stop = start + filter->size;
441 etm_filter = &filters->etm_filter[i];
442
443 if (filter->range == 1) {
444 etm_filter->start_addr = start;
445 etm_filter->stop_addr = stop;
446 etm_filter->type = ETM_ADDR_TYPE_RANGE;
447 } else {
448 if (filter->filter == 1) {
449 etm_filter->start_addr = start;
450 etm_filter->type = ETM_ADDR_TYPE_START;
451 } else {
452 etm_filter->stop_addr = stop;
453 etm_filter->type = ETM_ADDR_TYPE_STOP;
454 }
455 }
456 i++;
457 }
458
459 filters->nr_filters = i;
460}
461
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700462int etm_perf_symlink(struct coresight_device *csdev, bool link)
463{
464 char entry[sizeof("cpu9999999")];
465 int ret = 0, cpu = source_ops(csdev)->cpu_id(csdev);
466 struct device *pmu_dev = etm_pmu.dev;
467 struct device *cs_dev = &csdev->dev;
468
469 sprintf(entry, "cpu%d", cpu);
470
471 if (!etm_perf_up)
472 return -EPROBE_DEFER;
473
474 if (link) {
475 ret = sysfs_create_link(&pmu_dev->kobj, &cs_dev->kobj, entry);
476 if (ret)
477 return ret;
478 per_cpu(csdev_src, cpu) = csdev;
479 } else {
480 sysfs_remove_link(&pmu_dev->kobj, entry);
481 per_cpu(csdev_src, cpu) = NULL;
482 }
483
484 return 0;
485}
486
487static int __init etm_perf_init(void)
488{
489 int ret;
490
Mathieu Poirierca878b12016-08-25 15:19:12 -0600491 etm_pmu.capabilities = PERF_PMU_CAP_EXCLUSIVE;
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700492
Mathieu Poirierca878b12016-08-25 15:19:12 -0600493 etm_pmu.attr_groups = etm_pmu_attr_groups;
494 etm_pmu.task_ctx_nr = perf_sw_context;
495 etm_pmu.read = etm_event_read;
496 etm_pmu.event_init = etm_event_init;
497 etm_pmu.setup_aux = etm_setup_aux;
498 etm_pmu.free_aux = etm_free_aux;
499 etm_pmu.start = etm_event_start;
500 etm_pmu.stop = etm_event_stop;
501 etm_pmu.add = etm_event_add;
502 etm_pmu.del = etm_event_del;
503 etm_pmu.addr_filters_sync = etm_addr_filters_sync;
504 etm_pmu.addr_filters_validate = etm_addr_filters_validate;
505 etm_pmu.nr_addr_filters = ETM_ADDR_CMP_MAX;
Mathieu Poirier0bcbf2e2016-02-17 17:52:01 -0700506
507 ret = perf_pmu_register(&etm_pmu, CORESIGHT_ETM_PMU_NAME, -1);
508 if (ret == 0)
509 etm_perf_up = true;
510
511 return ret;
512}
Paul Gortmakerca48fa22016-02-27 15:21:47 -0500513device_initcall(etm_perf_init);