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