mmc: host: sh_mobile_sdhi: move card_busy from tmio to sdhi
[linux/fpc-iii.git] / drivers / hwtracing / coresight / coresight-etm-perf.c
blob755125f7917f2cb6aa369fd88d28d763e688a9d6
1 /*
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
12 * more details.
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>
23 #include <linux/mm.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;
35 /**
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;
44 cpumask_t mask;
45 void *snk_config;
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,
59 NULL,
62 static struct attribute_group etm_pmu_format_group = {
63 .name = "format",
64 .attrs = etm_config_formats_attr,
67 static const struct attribute_group *etm_pmu_attr_groups[] = {
68 &etm_pmu_format_group,
69 NULL,
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)
77 return -ENOENT;
79 return 0;
82 static void free_event_data(struct work_struct *work)
84 int cpu;
85 cpumask_t *mask;
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);
108 kfree(event_data);
111 static void *alloc_event_data(int cpu)
113 int size;
114 cpumask_t *mask;
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);
119 if (!event_data)
120 return NULL;
122 /* Make sure nothing disappears under us */
123 get_online_cpus();
124 size = num_online_cpus();
126 mask = &event_data->mask;
127 if (cpu != -1)
128 cpumask_set_cpu(cpu, mask);
129 else
130 cpumask_copy(mask, cpu_online_mask);
131 put_online_cpus();
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) {
144 kfree(event_data);
145 return NULL;
148 return event_data;
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)
161 int cpu;
162 cpumask_t *mask;
163 struct coresight_device *sink;
164 struct etm_event_data *event_data = NULL;
166 event_data = alloc_event_data(event_cpu);
167 if (!event_data)
168 return NULL;
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);
179 if (!csdev)
180 goto err;
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])
189 goto err;
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]);
203 if (!sink)
204 goto err;
206 if (!sink_ops(sink)->alloc_buffer)
207 goto err;
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)
214 goto err;
216 out:
217 return event_data;
219 err:
220 etm_free_aux(event_data);
221 event_data = NULL;
222 goto out;
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);
232 if (!csdev)
233 goto fail;
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);
240 if (!event_data)
241 goto fail;
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))
246 goto fail_end_stop;
248 /* Configure the sink */
249 if (sink_ops(sink)->set_buffer(sink, handle,
250 event_data->snk_config))
251 goto fail_end_stop;
253 /* Nothing will happen without a path */
254 if (coresight_enable_path(event_data->path[cpu], CS_MODE_PERF))
255 goto fail_end_stop;
257 /* Tell the perf core the event is alive */
258 event->hw.state = 0;
260 /* Finally enable the tracer */
261 if (source_ops(csdev)->enable(csdev, &event->attr, CS_MODE_PERF))
262 goto fail_end_stop;
264 out:
265 return;
267 fail_end_stop:
268 perf_aux_output_end(handle, 0, true);
269 fail:
270 event->hw.state = PERF_HES_STOPPED;
271 goto out;
274 static void etm_event_stop(struct perf_event *event, int mode)
276 bool lost;
277 int cpu = smp_processor_id();
278 unsigned long size;
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)
284 return;
286 if (!csdev)
287 return;
289 sink = coresight_get_sink(event_data->path[cpu]);
290 if (!sink)
291 return;
293 /* stop tracer */
294 source_ops(csdev)->disable(csdev);
296 /* tell the core */
297 event->hw.state = PERF_HES_STOPPED;
299 if (mode & PERF_EF_UPDATE) {
300 if (WARN_ON_ONCE(handle->event != event))
301 return;
303 /* update trace information */
304 if (!sink_ops(sink)->update_buffer)
305 return;
307 sink_ops(sink)->update_buffer(sink, handle,
308 event_data->snk_config);
310 if (!sink_ops(sink)->reset_buffer)
311 return;
313 size = sink_ops(sink)->reset_buffer(sink, handle,
314 event_data->snk_config,
315 &lost);
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)
326 int ret = 0;
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)
332 ret = -EINVAL;
333 } else {
334 hwc->state = PERF_HES_STOPPED;
337 return ret;
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);
354 if (!etm_perf_up)
355 return -EPROBE_DEFER;
357 if (link) {
358 ret = sysfs_create_link(&pmu_dev->kobj, &cs_dev->kobj, entry);
359 if (ret)
360 return ret;
361 per_cpu(csdev_src, cpu) = csdev;
362 } else {
363 sysfs_remove_link(&pmu_dev->kobj, entry);
364 per_cpu(csdev_src, cpu) = NULL;
367 return 0;
370 static int __init etm_perf_init(void)
372 int ret;
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);
388 if (ret == 0)
389 etm_perf_up = true;
391 return ret;
393 device_initcall(etm_perf_init);