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 */ 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 ** 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 * 148 etm_event_cpu_path(struct etm_event_data *data, int cpu) 149 { 150 return *etm_event_cpu_path_ptr(data, cpu); 151 } 152 153 static void etm_event_read(struct perf_event *event) {} 154 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 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 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 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 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 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 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 */ 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 */ 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 * 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 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 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 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 out: 617 /* Tell the perf core the event is alive */ 618 event->hw.state = 0; 619 /* Save the event_data for this ETM */ 620 WRITE_ONCE(ctxt->event_data, event_data); 621 return; 622 623 fail_disable_path: 624 coresight_disable_path(path); 625 fail_end_stop: 626 /* 627 * Check if the handle is still associated with the event, 628 * to handle cases where if the sink failed to start the 629 * trace and TRUNCATED the handle already. 630 */ 631 if (READ_ONCE(handle->event)) { 632 perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED); 633 perf_aux_output_end(handle, 0); 634 } 635 fail: 636 event->hw.state = PERF_HES_STOPPED; 637 return; 638 } 639 640 static void etm_event_pause(struct coresight_path *path, 641 struct perf_event *event, 642 struct etm_ctxt *ctxt) 643 { 644 struct perf_output_handle *handle = &ctxt->handle; 645 struct coresight_device *source, *sink; 646 struct etm_event_data *event_data; 647 unsigned long size; 648 649 if (!path) 650 return; 651 652 source = coresight_get_source(path); 653 sink = coresight_get_sink(path); 654 if (WARN_ON_ONCE(!source || !sink)) 655 return; 656 657 /* Stop tracer */ 658 coresight_pause_source(source); 659 660 /* 661 * The per CPU sink has own interrupt handling, it might have 662 * race condition with updating buffer on AUX trace pause if 663 * it is invoked from NMI. To avoid the race condition, 664 * disallows updating buffer for the per CPU sink case. 665 */ 666 if (coresight_is_percpu_sink(sink)) 667 return; 668 669 if (WARN_ON_ONCE(handle->event != event)) 670 return; 671 672 if (!sink_ops(sink)->update_buffer) 673 return; 674 675 event_data = READ_ONCE(ctxt->event_data); 676 size = sink_ops(sink)->update_buffer(sink, handle, 677 event_data->snk_config); 678 if (READ_ONCE(handle->event)) { 679 if (!size) 680 return; 681 682 perf_aux_output_end(handle, size); 683 perf_aux_output_begin(handle, event); 684 } else { 685 WARN_ON_ONCE(size); 686 } 687 } 688 689 static void etm_event_stop(struct perf_event *event, int mode) 690 { 691 int cpu = smp_processor_id(); 692 unsigned long size; 693 struct coresight_device *source, *sink; 694 struct etm_ctxt *ctxt = this_cpu_ptr(&etm_ctxt); 695 struct perf_output_handle *handle = &ctxt->handle; 696 struct coresight_path *path = etm_event_get_ctxt_path(ctxt); 697 struct etm_event_data *event_data; 698 699 if (mode & PERF_EF_PAUSE) 700 return etm_event_pause(path, event, ctxt); 701 702 /* 703 * If we still have access to the event_data via handle, 704 * confirm that we haven't messed up the tracking. 705 */ 706 if (handle->event && 707 WARN_ON(perf_get_aux(handle) != ctxt->event_data)) 708 return; 709 710 event_data = READ_ONCE(ctxt->event_data); 711 /* Clear the event_data as this ETM is stopping the trace. */ 712 WRITE_ONCE(ctxt->event_data, NULL); 713 714 if (event->hw.state == PERF_HES_STOPPED) 715 return; 716 717 /* We must have a valid event_data for a running event */ 718 if (WARN_ON(!event_data)) 719 return; 720 721 /* 722 * Check if this ETM was allowed to trace, as decided at 723 * etm_setup_aux(). If it wasn't allowed to trace, then 724 * nothing needs to be torn down other than outputting a 725 * zero sized record. 726 */ 727 if (handle->event && (mode & PERF_EF_UPDATE) && 728 !cpumask_test_cpu(cpu, &event_data->mask)) { 729 event->hw.state = PERF_HES_STOPPED; 730 perf_aux_output_end(handle, 0); 731 return; 732 } 733 734 source = coresight_get_source(path); 735 sink = coresight_get_sink(path); 736 if (!source || !sink) 737 return; 738 739 /* stop tracer */ 740 coresight_disable_source(source, event); 741 742 /* tell the core */ 743 event->hw.state = PERF_HES_STOPPED; 744 745 /* 746 * If the handle is not bound to an event anymore 747 * (e.g, the sink driver was unable to restart the 748 * handle due to lack of buffer space), we don't 749 * have to do anything here. 750 */ 751 if (handle->event && (mode & PERF_EF_UPDATE)) { 752 if (WARN_ON_ONCE(handle->event != event)) 753 return; 754 755 /* update trace information */ 756 if (!sink_ops(sink)->update_buffer) 757 return; 758 759 size = sink_ops(sink)->update_buffer(sink, handle, 760 event_data->snk_config); 761 /* 762 * Make sure the handle is still valid as the 763 * sink could have closed it from an IRQ. 764 * The sink driver must handle the race with 765 * update_buffer() and IRQ. Thus either we 766 * should get a valid handle and valid size 767 * (which may be 0). 768 * 769 * But we should never get a non-zero size with 770 * an invalid handle. 771 */ 772 if (READ_ONCE(handle->event)) 773 perf_aux_output_end(handle, size); 774 else 775 WARN_ON(size); 776 } 777 778 /* Disabling the path make its elements available to other sessions */ 779 coresight_disable_path(path); 780 } 781 782 static int etm_event_add(struct perf_event *event, int mode) 783 { 784 int ret = 0; 785 struct hw_perf_event *hwc = &event->hw; 786 787 if (mode & PERF_EF_START) { 788 etm_event_start(event, 0); 789 if (hwc->state & PERF_HES_STOPPED) 790 ret = -EINVAL; 791 } else { 792 hwc->state = PERF_HES_STOPPED; 793 } 794 795 return ret; 796 } 797 798 static void etm_event_del(struct perf_event *event, int mode) 799 { 800 etm_event_stop(event, PERF_EF_UPDATE); 801 } 802 803 static int etm_addr_filters_validate(struct list_head *filters) 804 { 805 bool range = false, address = false; 806 int index = 0; 807 struct perf_addr_filter *filter; 808 809 list_for_each_entry(filter, filters, entry) { 810 /* 811 * No need to go further if there's no more 812 * room for filters. 813 */ 814 if (++index > ETM_ADDR_CMP_MAX) 815 return -EOPNOTSUPP; 816 817 /* filter::size==0 means single address trigger */ 818 if (filter->size) { 819 /* 820 * The existing code relies on START/STOP filters 821 * being address filters. 822 */ 823 if (filter->action == PERF_ADDR_FILTER_ACTION_START || 824 filter->action == PERF_ADDR_FILTER_ACTION_STOP) 825 return -EOPNOTSUPP; 826 827 range = true; 828 } else 829 address = true; 830 831 /* 832 * At this time we don't allow range and start/stop filtering 833 * to cohabitate, they have to be mutually exclusive. 834 */ 835 if (range && address) 836 return -EOPNOTSUPP; 837 } 838 839 return 0; 840 } 841 842 static void etm_addr_filters_sync(struct perf_event *event) 843 { 844 struct perf_addr_filters_head *head = perf_event_addr_filters(event); 845 unsigned long start, stop; 846 struct perf_addr_filter_range *fr = event->addr_filter_ranges; 847 struct etm_filters *filters = event->hw.addr_filters; 848 struct etm_filter *etm_filter; 849 struct perf_addr_filter *filter; 850 int i = 0; 851 852 list_for_each_entry(filter, &head->list, entry) { 853 start = fr[i].start; 854 stop = start + fr[i].size; 855 etm_filter = &filters->etm_filter[i]; 856 857 switch (filter->action) { 858 case PERF_ADDR_FILTER_ACTION_FILTER: 859 etm_filter->start_addr = start; 860 etm_filter->stop_addr = stop; 861 etm_filter->type = ETM_ADDR_TYPE_RANGE; 862 break; 863 case PERF_ADDR_FILTER_ACTION_START: 864 etm_filter->start_addr = start; 865 etm_filter->type = ETM_ADDR_TYPE_START; 866 break; 867 case PERF_ADDR_FILTER_ACTION_STOP: 868 etm_filter->stop_addr = stop; 869 etm_filter->type = ETM_ADDR_TYPE_STOP; 870 break; 871 } 872 i++; 873 } 874 875 filters->nr_filters = i; 876 } 877 878 int etm_perf_symlink(struct coresight_device *csdev, bool link) 879 { 880 char entry[sizeof("cpu9999999")]; 881 int ret = 0, cpu = csdev->cpu; 882 struct device *pmu_dev = etm_pmu.dev; 883 struct device *cs_dev = &csdev->dev; 884 885 sprintf(entry, "cpu%d", cpu); 886 887 if (!etm_perf_up) 888 return -EPROBE_DEFER; 889 890 if (link) 891 ret = sysfs_create_link(&pmu_dev->kobj, &cs_dev->kobj, entry); 892 else 893 sysfs_remove_link(&pmu_dev->kobj, entry); 894 895 return ret; 896 } 897 EXPORT_SYMBOL_GPL(etm_perf_symlink); 898 899 static ssize_t etm_perf_sink_name_show(struct device *dev, 900 struct device_attribute *dattr, 901 char *buf) 902 { 903 struct dev_ext_attribute *ea; 904 905 ea = container_of(dattr, struct dev_ext_attribute, attr); 906 return scnprintf(buf, PAGE_SIZE, "0x%px\n", ea->var); 907 } 908 909 static struct dev_ext_attribute * 910 etm_perf_add_symlink_group(struct device *dev, const char *name, const char *group_name) 911 { 912 struct dev_ext_attribute *ea; 913 unsigned long hash; 914 int ret; 915 struct device *pmu_dev = etm_pmu.dev; 916 917 if (!etm_perf_up) 918 return ERR_PTR(-EPROBE_DEFER); 919 920 ea = devm_kzalloc(dev, sizeof(*ea), GFP_KERNEL); 921 if (!ea) 922 return ERR_PTR(-ENOMEM); 923 924 /* 925 * If this function is called adding a sink then the hash is used for 926 * sink selection - see function coresight_get_sink_by_id(). 927 * If adding a configuration then the hash is used for selection in 928 * cscfg_activate_config() 929 */ 930 hash = hashlen_hash(hashlen_string(NULL, name)); 931 932 sysfs_attr_init(&ea->attr.attr); 933 ea->attr.attr.name = devm_kstrdup(dev, name, GFP_KERNEL); 934 if (!ea->attr.attr.name) 935 return ERR_PTR(-ENOMEM); 936 937 ea->attr.attr.mode = 0444; 938 ea->var = (unsigned long *)hash; 939 940 ret = sysfs_add_file_to_group(&pmu_dev->kobj, 941 &ea->attr.attr, group_name); 942 943 return ret ? ERR_PTR(ret) : ea; 944 } 945 946 int etm_perf_add_symlink_sink(struct coresight_device *csdev) 947 { 948 const char *name; 949 struct device *dev = &csdev->dev; 950 int err = 0; 951 952 if (csdev->type != CORESIGHT_DEV_TYPE_SINK && 953 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK) 954 return -EOPNOTSUPP; 955 956 if (!sink_ops(csdev)->alloc_buffer) 957 return -EOPNOTSUPP; 958 959 if (csdev->ea != NULL) 960 return -EINVAL; 961 962 name = dev_name(dev); 963 csdev->ea = etm_perf_add_symlink_group(dev, name, "sinks"); 964 if (IS_ERR(csdev->ea)) { 965 err = PTR_ERR(csdev->ea); 966 csdev->ea = NULL; 967 } else 968 csdev->ea->attr.show = etm_perf_sink_name_show; 969 970 return err; 971 } 972 973 static void etm_perf_del_symlink_group(struct dev_ext_attribute *ea, const char *group_name) 974 { 975 struct device *pmu_dev = etm_pmu.dev; 976 977 sysfs_remove_file_from_group(&pmu_dev->kobj, 978 &ea->attr.attr, group_name); 979 } 980 981 void etm_perf_del_symlink_sink(struct coresight_device *csdev) 982 { 983 if (csdev->type != CORESIGHT_DEV_TYPE_SINK && 984 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK) 985 return; 986 987 if (!csdev->ea) 988 return; 989 990 etm_perf_del_symlink_group(csdev->ea, "sinks"); 991 csdev->ea = NULL; 992 } 993 994 static ssize_t etm_perf_cscfg_event_show(struct device *dev, 995 struct device_attribute *dattr, 996 char *buf) 997 { 998 struct dev_ext_attribute *ea; 999 1000 ea = container_of(dattr, struct dev_ext_attribute, attr); 1001 return scnprintf(buf, PAGE_SIZE, "configid=0x%px\n", ea->var); 1002 } 1003 1004 int etm_perf_add_symlink_cscfg(struct device *dev, struct cscfg_config_desc *config_desc) 1005 { 1006 int err = 0; 1007 1008 if (config_desc->event_ea != NULL) 1009 return 0; 1010 1011 config_desc->event_ea = etm_perf_add_symlink_group(dev, config_desc->name, "events"); 1012 1013 /* set the show function to the custom cscfg event */ 1014 if (!IS_ERR(config_desc->event_ea)) 1015 config_desc->event_ea->attr.show = etm_perf_cscfg_event_show; 1016 else { 1017 err = PTR_ERR(config_desc->event_ea); 1018 config_desc->event_ea = NULL; 1019 } 1020 1021 return err; 1022 } 1023 1024 void etm_perf_del_symlink_cscfg(struct cscfg_config_desc *config_desc) 1025 { 1026 if (!config_desc->event_ea) 1027 return; 1028 1029 etm_perf_del_symlink_group(config_desc->event_ea, "events"); 1030 config_desc->event_ea = NULL; 1031 } 1032 1033 int __init etm_perf_init(void) 1034 { 1035 int ret; 1036 1037 etm_pmu.capabilities = (PERF_PMU_CAP_EXCLUSIVE | 1038 PERF_PMU_CAP_ITRACE | 1039 PERF_PMU_CAP_AUX_PAUSE); 1040 1041 etm_pmu.attr_groups = etm_pmu_attr_groups; 1042 etm_pmu.task_ctx_nr = perf_sw_context; 1043 etm_pmu.read = etm_event_read; 1044 etm_pmu.event_init = etm_event_init; 1045 etm_pmu.setup_aux = etm_setup_aux; 1046 etm_pmu.free_aux = etm_free_aux; 1047 etm_pmu.start = etm_event_start; 1048 etm_pmu.stop = etm_event_stop; 1049 etm_pmu.add = etm_event_add; 1050 etm_pmu.del = etm_event_del; 1051 etm_pmu.addr_filters_sync = etm_addr_filters_sync; 1052 etm_pmu.addr_filters_validate = etm_addr_filters_validate; 1053 etm_pmu.nr_addr_filters = ETM_ADDR_CMP_MAX; 1054 etm_pmu.module = THIS_MODULE; 1055 1056 ret = perf_pmu_register(&etm_pmu, CORESIGHT_ETM_PMU_NAME, -1); 1057 if (ret == 0) 1058 etm_perf_up = true; 1059 1060 return ret; 1061 } 1062 1063 void etm_perf_exit(void) 1064 { 1065 perf_pmu_unregister(&etm_pmu); 1066 } 1067