coresight-etm-perf.c 15.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright(C) 2015 Linaro Limited. All rights reserved.
 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
 */

#include <linux/coresight.h>
#include <linux/coresight-pmu.h>
#include <linux/cpumask.h>
#include <linux/device.h>
#include <linux/list.h>
#include <linux/mm.h>
13
#include <linux/init.h>
14
#include <linux/perf_event.h>
15
#include <linux/percpu-defs.h>
16
#include <linux/slab.h>
17
#include <linux/stringhash.h>
18 19 20
#include <linux/types.h>
#include <linux/workqueue.h>

21
#include "coresight-etm-perf.h"
22 23 24 25 26 27 28 29 30 31
#include "coresight-priv.h"

static struct pmu etm_pmu;
static bool etm_perf_up;

static DEFINE_PER_CPU(struct perf_output_handle, ctx_handle);
static DEFINE_PER_CPU(struct coresight_device *, csdev_src);

/* ETMv3.5/PTM's ETMCR is 'config' */
PMU_FORMAT_ATTR(cycacc,		"config:" __stringify(ETM_OPT_CYCACC));
32
PMU_FORMAT_ATTR(contextid,	"config:" __stringify(ETM_OPT_CTXTID));
33
PMU_FORMAT_ATTR(timestamp,	"config:" __stringify(ETM_OPT_TS));
34
PMU_FORMAT_ATTR(retstack,	"config:" __stringify(ETM_OPT_RETSTK));
35 36
/* Sink ID - same for all ETMs */
PMU_FORMAT_ATTR(sinkid,		"config2:0-31");
37 38 39

static struct attribute *etm_config_formats_attr[] = {
	&format_attr_cycacc.attr,
40
	&format_attr_contextid.attr,
41
	&format_attr_timestamp.attr,
42
	&format_attr_retstack.attr,
43
	&format_attr_sinkid.attr,
44 45 46
	NULL,
};

47
static const struct attribute_group etm_pmu_format_group = {
48 49 50 51
	.name   = "format",
	.attrs  = etm_config_formats_attr,
};

52 53 54 55 56 57 58 59 60
static struct attribute *etm_config_sinks_attr[] = {
	NULL,
};

static const struct attribute_group etm_pmu_sinks_group = {
	.name   = "sinks",
	.attrs  = etm_config_sinks_attr,
};

61 62
static const struct attribute_group *etm_pmu_attr_groups[] = {
	&etm_pmu_format_group,
63
	&etm_pmu_sinks_group,
64 65 66
	NULL,
};

67 68 69 70 71 72 73 74 75 76 77 78
static inline struct list_head **
etm_event_cpu_path_ptr(struct etm_event_data *data, int cpu)
{
	return per_cpu_ptr(data->path, cpu);
}

static inline struct list_head *
etm_event_cpu_path(struct etm_event_data *data, int cpu)
{
	return *etm_event_cpu_path_ptr(data, cpu);
}

79 80
static void etm_event_read(struct perf_event *event) {}

81
static int etm_addr_filters_alloc(struct perf_event *event)
82
{
83 84 85 86 87 88 89 90 91 92 93 94
	struct etm_filters *filters;
	int node = event->cpu == -1 ? -1 : cpu_to_node(event->cpu);

	filters = kzalloc_node(sizeof(struct etm_filters), GFP_KERNEL, node);
	if (!filters)
		return -ENOMEM;

	if (event->parent)
		memcpy(filters, event->parent->hw.addr_filters,
		       sizeof(*filters));

	event->hw.addr_filters = filters;
95 96 97 98

	return 0;
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
static void etm_event_destroy(struct perf_event *event)
{
	kfree(event->hw.addr_filters);
	event->hw.addr_filters = NULL;
}

static int etm_event_init(struct perf_event *event)
{
	int ret = 0;

	if (event->attr.type != etm_pmu.type) {
		ret = -ENOENT;
		goto out;
	}

	ret = etm_addr_filters_alloc(event);
	if (ret)
		goto out;

	event->destroy = etm_event_destroy;
out:
	return ret;
}

123 124 125 126 127 128
static void free_sink_buffer(struct etm_event_data *event_data)
{
	int cpu;
	cpumask_t *mask = &event_data->mask;
	struct coresight_device *sink;

129
	if (!event_data->snk_config)
130 131
		return;

132
	if (WARN_ON(cpumask_empty(mask)))
133 134 135 136 137 138 139
		return;

	cpu = cpumask_first(mask);
	sink = coresight_get_sink(etm_event_cpu_path(event_data, cpu));
	sink_ops(sink)->free_buffer(event_data->snk_config);
}

140 141 142 143 144 145 146 147
static void free_event_data(struct work_struct *work)
{
	int cpu;
	cpumask_t *mask;
	struct etm_event_data *event_data;

	event_data = container_of(work, struct etm_event_data, work);
	mask = &event_data->mask;
148 149

	/* Free the sink buffers, if there are any */
150
	free_sink_buffer(event_data);
151 152

	for_each_cpu(cpu, mask) {
153 154 155 156 157 158
		struct list_head **ppath;

		ppath = etm_event_cpu_path_ptr(event_data, cpu);
		if (!(IS_ERR_OR_NULL(*ppath)))
			coresight_release_path(*ppath);
		*ppath = NULL;
159 160
	}

161
	free_percpu(event_data->path);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	kfree(event_data);
}

static void *alloc_event_data(int cpu)
{
	cpumask_t *mask;
	struct etm_event_data *event_data;

	/* First get memory for the session's data */
	event_data = kzalloc(sizeof(struct etm_event_data), GFP_KERNEL);
	if (!event_data)
		return NULL;


	mask = &event_data->mask;
	if (cpu != -1)
		cpumask_set_cpu(cpu, mask);
	else
180
		cpumask_copy(mask, cpu_present_mask);
181 182 183 184 185 186 187 188 189

	/*
	 * Each CPU has a single path between source and destination.  As such
	 * allocate an array using CPU numbers as indexes.  That way a path
	 * for any CPU can easily be accessed at any given time.  We proceed
	 * the same way for sessions involving a single CPU.  The cost of
	 * unused memory when dealing with single CPU trace scenarios is small
	 * compared to the cost of searching through an optimized array.
	 */
190 191
	event_data->path = alloc_percpu(struct list_head *);

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	if (!event_data->path) {
		kfree(event_data);
		return NULL;
	}

	return event_data;
}

static void etm_free_aux(void *data)
{
	struct etm_event_data *event_data = data;

	schedule_work(&event_data->work);
}

207
static void *etm_setup_aux(struct perf_event *event, void **pages,
208 209
			   int nr_pages, bool overwrite)
{
210
	u32 id;
211
	int cpu = event->cpu;
212
	cpumask_t *mask;
213
	struct coresight_device *sink = NULL;
214 215
	struct etm_event_data *event_data = NULL;

216
	event_data = alloc_event_data(cpu);
217 218
	if (!event_data)
		return NULL;
219
	INIT_WORK(&event_data->work, free_event_data);
220

221 222 223 224 225 226
	/* First get the selected sink from user space. */
	if (event->attr.config2) {
		id = (u32)event->attr.config2;
		sink = coresight_get_sink_by_id(id);
	}

227 228
	mask = &event_data->mask;

229 230 231 232 233 234 235
	/*
	 * Setup the path for each CPU in a trace session. We try to build
	 * trace path for each CPU in the mask. If we don't find an ETM
	 * for the CPU or fail to build a path, we clear the CPU from the
	 * mask and continue with the rest. If ever we try to trace on those
	 * CPUs, we can handle it and fail the session.
	 */
236
	for_each_cpu(cpu, mask) {
237
		struct list_head *path;
238 239 240
		struct coresight_device *csdev;

		csdev = per_cpu(csdev_src, cpu);
241 242 243 244 245 246 247 248 249
		/*
		 * If there is no ETM associated with this CPU clear it from
		 * the mask and continue with the rest. If ever we try to trace
		 * on this CPU, we handle it accordingly.
		 */
		if (!csdev) {
			cpumask_clear_cpu(cpu, mask);
			continue;
		}
250

251 252 253 254 255 256 257 258 259 260
		/*
		 * No sink provided - look for a default sink for one of the
		 * devices. At present we only support topology where all CPUs
		 * use the same sink [N:1], so only need to find one sink. The
		 * coresight_build_path later will remove any CPU that does not
		 * attach to the sink, or if we have not found a sink.
		 */
		if (!sink)
			sink = coresight_find_default_sink(csdev);

261 262 263 264 265
		/*
		 * Building a path doesn't enable it, it simply builds a
		 * list of devices from source to sink that can be
		 * referenced later when the path is actually needed.
		 */
266
		path = coresight_build_path(csdev, sink);
267 268 269 270
		if (IS_ERR(path)) {
			cpumask_clear_cpu(cpu, mask);
			continue;
		}
271 272

		*etm_event_cpu_path_ptr(event_data, cpu) = path;
273 274
	}

275 276 277 278
	/* no sink found for any CPU - cannot trace */
	if (!sink)
		goto err;

279 280 281
	/* If we don't have any CPUs ready for tracing, abort */
	cpu = cpumask_first(mask);
	if (cpu >= nr_cpu_ids)
282 283
		goto err;

284 285 286
	if (!sink_ops(sink)->alloc_buffer || !sink_ops(sink)->free_buffer)
		goto err;

287
	/* Allocate the sink buffer for this session */
288
	event_data->snk_config =
289
			sink_ops(sink)->alloc_buffer(sink, event, pages,
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
						     nr_pages, overwrite);
	if (!event_data->snk_config)
		goto err;

out:
	return event_data;

err:
	etm_free_aux(event_data);
	event_data = NULL;
	goto out;
}

static void etm_event_start(struct perf_event *event, int flags)
{
	int cpu = smp_processor_id();
	struct etm_event_data *event_data;
	struct perf_output_handle *handle = this_cpu_ptr(&ctx_handle);
	struct coresight_device *sink, *csdev = per_cpu(csdev_src, cpu);
309
	struct list_head *path;
310 311 312 313 314 315 316 317 318 319 320 321

	if (!csdev)
		goto fail;

	/*
	 * Deal with the ring buffer API and get a handle on the
	 * session's information.
	 */
	event_data = perf_aux_output_begin(handle, event);
	if (!event_data)
		goto fail;

322 323 324 325 326 327 328 329 330 331
	/*
	 * Check if this ETM is allowed to trace, as decided
	 * at etm_setup_aux(). This could be due to an unreachable
	 * sink from this ETM. We can't do much in this case if
	 * the sink was specified or hinted to the driver. For
	 * now, simply don't record anything on this ETM.
	 */
	if (!cpumask_test_cpu(cpu, &event_data->mask))
		goto fail_end_stop;

332
	path = etm_event_cpu_path(event_data, cpu);
333
	/* We need a sink, no need to continue without one */
334
	sink = coresight_get_sink(path);
335
	if (WARN_ON_ONCE(!sink))
336 337 338
		goto fail_end_stop;

	/* Nothing will happen without a path */
339
	if (coresight_enable_path(path, CS_MODE_PERF, handle))
340 341 342 343 344 345
		goto fail_end_stop;

	/* Tell the perf core the event is alive */
	event->hw.state = 0;

	/* Finally enable the tracer */
346
	if (source_ops(csdev)->enable(csdev, event, CS_MODE_PERF))
347
		goto fail_disable_path;
348 349 350 351

out:
	return;

352 353
fail_disable_path:
	coresight_disable_path(path);
354
fail_end_stop:
355 356
	perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
	perf_aux_output_end(handle, 0);
357 358 359 360 361 362 363 364 365 366 367 368
fail:
	event->hw.state = PERF_HES_STOPPED;
	goto out;
}

static void etm_event_stop(struct perf_event *event, int mode)
{
	int cpu = smp_processor_id();
	unsigned long size;
	struct coresight_device *sink, *csdev = per_cpu(csdev_src, cpu);
	struct perf_output_handle *handle = this_cpu_ptr(&ctx_handle);
	struct etm_event_data *event_data = perf_get_aux(handle);
369
	struct list_head *path;
370 371 372 373 374 375 376

	if (event->hw.state == PERF_HES_STOPPED)
		return;

	if (!csdev)
		return;

377 378 379 380 381
	path = etm_event_cpu_path(event_data, cpu);
	if (!path)
		return;

	sink = coresight_get_sink(path);
382 383 384 385
	if (!sink)
		return;

	/* stop tracer */
386
	source_ops(csdev)->disable(csdev, event);
387 388 389 390 391 392 393 394 395 396 397 398

	/* tell the core */
	event->hw.state = PERF_HES_STOPPED;

	if (mode & PERF_EF_UPDATE) {
		if (WARN_ON_ONCE(handle->event != event))
			return;

		/* update trace information */
		if (!sink_ops(sink)->update_buffer)
			return;

399
		size = sink_ops(sink)->update_buffer(sink, handle,
400
					      event_data->snk_config);
401
		perf_aux_output_end(handle, size);
402 403 404
	}

	/* Disabling the path make its elements available to other sessions */
405
	coresight_disable_path(path);
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
}

static int etm_event_add(struct perf_event *event, int mode)
{
	int ret = 0;
	struct hw_perf_event *hwc = &event->hw;

	if (mode & PERF_EF_START) {
		etm_event_start(event, 0);
		if (hwc->state & PERF_HES_STOPPED)
			ret = -EINVAL;
	} else {
		hwc->state = PERF_HES_STOPPED;
	}

	return ret;
}

static void etm_event_del(struct perf_event *event, int mode)
{
	etm_event_stop(event, PERF_EF_UPDATE);
}

429 430 431 432 433 434 435 436 437 438 439 440 441 442
static int etm_addr_filters_validate(struct list_head *filters)
{
	bool range = false, address = false;
	int index = 0;
	struct perf_addr_filter *filter;

	list_for_each_entry(filter, filters, entry) {
		/*
		 * No need to go further if there's no more
		 * room for filters.
		 */
		if (++index > ETM_ADDR_CMP_MAX)
			return -EOPNOTSUPP;

443 444 445 446 447 448 449 450 451 452 453 454 455 456
		/* filter::size==0 means single address trigger */
		if (filter->size) {
			/*
			 * The existing code relies on START/STOP filters
			 * being address filters.
			 */
			if (filter->action == PERF_ADDR_FILTER_ACTION_START ||
			    filter->action == PERF_ADDR_FILTER_ACTION_STOP)
				return -EOPNOTSUPP;

			range = true;
		} else
			address = true;

457 458 459 460
		/*
		 * At this time we don't allow range and start/stop filtering
		 * to cohabitate, they have to be mutually exclusive.
		 */
461
		if (range && address)
462 463 464 465 466 467 468 469 470
			return -EOPNOTSUPP;
	}

	return 0;
}

static void etm_addr_filters_sync(struct perf_event *event)
{
	struct perf_addr_filters_head *head = perf_event_addr_filters(event);
471 472
	unsigned long start, stop;
	struct perf_addr_filter_range *fr = event->addr_filter_ranges;
473 474 475 476 477 478
	struct etm_filters *filters = event->hw.addr_filters;
	struct etm_filter *etm_filter;
	struct perf_addr_filter *filter;
	int i = 0;

	list_for_each_entry(filter, &head->list, entry) {
479 480
		start = fr[i].start;
		stop = start + fr[i].size;
481 482
		etm_filter = &filters->etm_filter[i];

483 484
		switch (filter->action) {
		case PERF_ADDR_FILTER_ACTION_FILTER:
485 486 487
			etm_filter->start_addr = start;
			etm_filter->stop_addr = stop;
			etm_filter->type = ETM_ADDR_TYPE_RANGE;
488 489 490 491 492 493 494 495 496
			break;
		case PERF_ADDR_FILTER_ACTION_START:
			etm_filter->start_addr = start;
			etm_filter->type = ETM_ADDR_TYPE_START;
			break;
		case PERF_ADDR_FILTER_ACTION_STOP:
			etm_filter->stop_addr = stop;
			etm_filter->type = ETM_ADDR_TYPE_STOP;
			break;
497 498 499 500 501 502 503
		}
		i++;
	}

	filters->nr_filters = i;
}

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
int etm_perf_symlink(struct coresight_device *csdev, bool link)
{
	char entry[sizeof("cpu9999999")];
	int ret = 0, cpu = source_ops(csdev)->cpu_id(csdev);
	struct device *pmu_dev = etm_pmu.dev;
	struct device *cs_dev = &csdev->dev;

	sprintf(entry, "cpu%d", cpu);

	if (!etm_perf_up)
		return -EPROBE_DEFER;

	if (link) {
		ret = sysfs_create_link(&pmu_dev->kobj, &cs_dev->kobj, entry);
		if (ret)
			return ret;
		per_cpu(csdev_src, cpu) = csdev;
	} else {
		sysfs_remove_link(&pmu_dev->kobj, entry);
		per_cpu(csdev_src, cpu) = NULL;
	}

	return 0;
}
528
EXPORT_SYMBOL_GPL(etm_perf_symlink);
529

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
static ssize_t etm_perf_sink_name_show(struct device *dev,
				       struct device_attribute *dattr,
				       char *buf)
{
	struct dev_ext_attribute *ea;

	ea = container_of(dattr, struct dev_ext_attribute, attr);
	return scnprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)(ea->var));
}

int etm_perf_add_symlink_sink(struct coresight_device *csdev)
{
	int ret;
	unsigned long hash;
	const char *name;
	struct device *pmu_dev = etm_pmu.dev;
546
	struct device *dev = &csdev->dev;
547 548 549 550 551 552 553 554 555 556 557 558
	struct dev_ext_attribute *ea;

	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
		return -EINVAL;

	if (csdev->ea != NULL)
		return -EINVAL;

	if (!etm_perf_up)
		return -EPROBE_DEFER;

559
	ea = devm_kzalloc(dev, sizeof(*ea), GFP_KERNEL);
560 561 562
	if (!ea)
		return -ENOMEM;

563
	name = dev_name(dev);
564 565 566
	/* See function coresight_get_sink_by_id() to know where this is used */
	hash = hashlen_hash(hashlen_string(NULL, name));

567
	sysfs_attr_init(&ea->attr.attr);
568
	ea->attr.attr.name = devm_kstrdup(dev, name, GFP_KERNEL);
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
	if (!ea->attr.attr.name)
		return -ENOMEM;

	ea->attr.attr.mode = 0444;
	ea->attr.show = etm_perf_sink_name_show;
	ea->var = (unsigned long *)hash;

	ret = sysfs_add_file_to_group(&pmu_dev->kobj,
				      &ea->attr.attr, "sinks");

	if (!ret)
		csdev->ea = ea;

	return ret;
}

void etm_perf_del_symlink_sink(struct coresight_device *csdev)
{
	struct device *pmu_dev = etm_pmu.dev;
	struct dev_ext_attribute *ea = csdev->ea;

	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
		return;

	if (!ea)
		return;

	sysfs_remove_file_from_group(&pmu_dev->kobj,
				     &ea->attr.attr, "sinks");
	csdev->ea = NULL;
}

602
int __init etm_perf_init(void)
603 604 605
{
	int ret;

606 607
	etm_pmu.capabilities		= (PERF_PMU_CAP_EXCLUSIVE |
					   PERF_PMU_CAP_ITRACE);
608 609 610 611 612 613 614 615 616 617 618 619 620 621

	etm_pmu.attr_groups		= etm_pmu_attr_groups;
	etm_pmu.task_ctx_nr		= perf_sw_context;
	etm_pmu.read			= etm_event_read;
	etm_pmu.event_init		= etm_event_init;
	etm_pmu.setup_aux		= etm_setup_aux;
	etm_pmu.free_aux		= etm_free_aux;
	etm_pmu.start			= etm_event_start;
	etm_pmu.stop			= etm_event_stop;
	etm_pmu.add			= etm_event_add;
	etm_pmu.del			= etm_event_del;
	etm_pmu.addr_filters_sync	= etm_addr_filters_sync;
	etm_pmu.addr_filters_validate	= etm_addr_filters_validate;
	etm_pmu.nr_addr_filters		= ETM_ADDR_CMP_MAX;
622 623 624 625 626 627 628

	ret = perf_pmu_register(&etm_pmu, CORESIGHT_ETM_PMU_NAME, -1);
	if (ret == 0)
		etm_perf_up = true;

	return ret;
}
629 630 631 632 633

void __exit etm_perf_exit(void)
{
	perf_pmu_unregister(&etm_pmu);
}