File Index
Files and directories
Sign in
FILE

coresight-core.c

File preview with syntax highlighting.

linux-6.19.14/drivers/hwtracing/coresight/coresight-core.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
   4 */
   5
   6#include <linux/acpi.h>
   7#include <linux/bitfield.h>
   8#include <linux/build_bug.h>
   9#include <linux/kernel.h>
  10#include <linux/init.h>
  11#include <linux/types.h>
  12#include <linux/device.h>
  13#include <linux/io.h>
  14#include <linux/err.h>
  15#include <linux/export.h>
  16#include <linux/slab.h>
  17#include <linux/stringhash.h>
  18#include <linux/mutex.h>
  19#include <linux/clk.h>
  20#include <linux/coresight.h>
  21#include <linux/property.h>
  22#include <linux/delay.h>
  23#include <linux/pm_runtime.h>
  24#include <linux/panic_notifier.h>
  25
  26#include "coresight-etm-perf.h"
  27#include "coresight-priv.h"
  28#include "coresight-syscfg.h"
  29#include "coresight-trace-id.h"
  30
  31/*
  32 * Mutex used to lock all sysfs enable and disable actions and loading and
  33 * unloading devices by the Coresight core.
  34 */
  35DEFINE_MUTEX(coresight_mutex);
  36static DEFINE_PER_CPU(struct coresight_device *, csdev_sink);
  37
  38/**
  39 * struct coresight_node - elements of a path, from source to sink
  40 * @csdev:	Address of an element.
  41 * @link:	hook to the list.
  42 */
  43struct coresight_node {
  44	struct coresight_device *csdev;
  45	struct list_head link;
  46};
  47
  48/*
  49 * When losing synchronisation a new barrier packet needs to be inserted at the
  50 * beginning of the data collected in a buffer.  That way the decoder knows that
  51 * it needs to look for another sync sequence.
  52 */
  53const u32 coresight_barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
  54EXPORT_SYMBOL_GPL(coresight_barrier_pkt);
  55
  56static const struct cti_assoc_op *cti_assoc_ops;
  57
  58void coresight_set_cti_ops(const struct cti_assoc_op *cti_op)
  59{
  60	cti_assoc_ops = cti_op;
  61}
  62EXPORT_SYMBOL_GPL(coresight_set_cti_ops);
  63
  64void coresight_remove_cti_ops(void)
  65{
  66	cti_assoc_ops = NULL;
  67}
  68EXPORT_SYMBOL_GPL(coresight_remove_cti_ops);
  69
  70void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev)
  71{
  72	per_cpu(csdev_sink, cpu) = csdev;
  73}
  74EXPORT_SYMBOL_GPL(coresight_set_percpu_sink);
  75
  76struct coresight_device *coresight_get_percpu_sink(int cpu)
  77{
  78	return per_cpu(csdev_sink, cpu);
  79}
  80EXPORT_SYMBOL_GPL(coresight_get_percpu_sink);
  81
  82static struct coresight_device *coresight_get_source(struct coresight_path *path)
  83{
  84	struct coresight_device *csdev;
  85
  86	if (!path)
  87		return NULL;
  88
  89	csdev = list_first_entry(&path->path_list, struct coresight_node, link)->csdev;
  90	if (!coresight_is_device_source(csdev))
  91		return NULL;
  92
  93	return csdev;
  94}
  95
  96/**
  97 * coresight_blocks_source - checks whether the connection matches the source
  98 * of path if connection is bound to specific source.
  99 * @src:	The source device of the trace path
 100 * @conn:	The connection of one outport
 101 *
 102 * Return false if the connection doesn't have a source binded or source of the
 103 * path matches the source binds to connection.
 104 */
 105static bool coresight_blocks_source(struct coresight_device *src,
 106				    struct coresight_connection *conn)
 107{
 108	return conn->filter_src_fwnode && (conn->filter_src_dev != src);
 109}
 110
 111static struct coresight_connection *
 112coresight_find_out_connection(struct coresight_device *csdev,
 113			      struct coresight_device *out_dev,
 114			      struct coresight_device *trace_src)
 115{
 116	int i;
 117	struct coresight_connection *conn;
 118
 119	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
 120		conn = csdev->pdata->out_conns[i];
 121		if (coresight_blocks_source(trace_src, conn))
 122			continue;
 123		if (conn->dest_dev == out_dev)
 124			return conn;
 125	}
 126
 127	dev_err(&csdev->dev,
 128		"couldn't find output connection, csdev: %s, out_dev: %s\n",
 129		dev_name(&csdev->dev), dev_name(&out_dev->dev));
 130
 131	return ERR_PTR(-ENODEV);
 132}
 133
 134static u32 coresight_read_claim_tags_unlocked(struct coresight_device *csdev)
 135{
 136	return FIELD_GET(CORESIGHT_CLAIM_MASK,
 137			 csdev_access_relaxed_read32(&csdev->access, CORESIGHT_CLAIMCLR));
 138}
 139
 140static void coresight_set_self_claim_tag_unlocked(struct coresight_device *csdev)
 141{
 142	csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
 143				     CORESIGHT_CLAIMSET);
 144	isb();
 145}
 146
 147void coresight_clear_self_claim_tag(struct csdev_access *csa)
 148{
 149	if (csa->io_mem)
 150		CS_UNLOCK(csa->base);
 151	coresight_clear_self_claim_tag_unlocked(csa);
 152	if (csa->io_mem)
 153		CS_LOCK(csa->base);
 154}
 155EXPORT_SYMBOL_GPL(coresight_clear_self_claim_tag);
 156
 157void coresight_clear_self_claim_tag_unlocked(struct csdev_access *csa)
 158{
 159	csdev_access_relaxed_write32(csa, CORESIGHT_CLAIM_SELF_HOSTED,
 160				     CORESIGHT_CLAIMCLR);
 161	isb();
 162}
 163EXPORT_SYMBOL_GPL(coresight_clear_self_claim_tag_unlocked);
 164
 165/*
 166 * coresight_claim_device_unlocked : Claim the device for self-hosted usage
 167 * to prevent an external tool from touching this device. As per PSCI
 168 * standards, section "Preserving the execution context" => "Debug and Trace
 169 * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
 170 * DBGCLAIM[0] is reserved for external tools.
 171 *
 172 * Called with CS_UNLOCKed for the component.
 173 * Returns : 0 on success
 174 */
 175int coresight_claim_device_unlocked(struct coresight_device *csdev)
 176{
 177	int tag;
 178	struct csdev_access *csa;
 179
 180	if (WARN_ON(!csdev))
 181		return -EINVAL;
 182
 183	csa = &csdev->access;
 184	tag = coresight_read_claim_tags_unlocked(csdev);
 185
 186	switch (tag) {
 187	case CORESIGHT_CLAIM_FREE:
 188		coresight_set_self_claim_tag_unlocked(csdev);
 189		if (coresight_read_claim_tags_unlocked(csdev) == CORESIGHT_CLAIM_SELF_HOSTED)
 190			return 0;
 191
 192		/* There was a race setting the tag, clean up and fail */
 193		coresight_clear_self_claim_tag_unlocked(csa);
 194		dev_dbg(&csdev->dev, "Busy: Couldn't set self claim tag");
 195		return -EBUSY;
 196
 197	case CORESIGHT_CLAIM_EXTERNAL:
 198		/* External debug is an expected state, so log and report BUSY */
 199		dev_dbg(&csdev->dev, "Busy: Claimed by external debugger");
 200		return -EBUSY;
 201
 202	default:
 203	case CORESIGHT_CLAIM_SELF_HOSTED:
 204	case CORESIGHT_CLAIM_INVALID:
 205		/*
 206		 * Warn here because we clear a lingering self hosted tag
 207		 * on probe, so other tag combinations are impossible.
 208		 */
 209		dev_err_once(&csdev->dev, "Invalid claim tag state: %x", tag);
 210		return -EBUSY;
 211	}
 212}
 213EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked);
 214
 215int coresight_claim_device(struct coresight_device *csdev)
 216{
 217	int rc;
 218
 219	if (WARN_ON(!csdev))
 220		return -EINVAL;
 221
 222	CS_UNLOCK(csdev->access.base);
 223	rc = coresight_claim_device_unlocked(csdev);
 224	CS_LOCK(csdev->access.base);
 225
 226	return rc;
 227}
 228EXPORT_SYMBOL_GPL(coresight_claim_device);
 229
 230/*
 231 * coresight_disclaim_device_unlocked : Clear the claim tag for the device.
 232 * Called with CS_UNLOCKed for the component.
 233 */
 234void coresight_disclaim_device_unlocked(struct coresight_device *csdev)
 235{
 236
 237	if (WARN_ON(!csdev))
 238		return;
 239
 240	if (coresight_read_claim_tags_unlocked(csdev) == CORESIGHT_CLAIM_SELF_HOSTED)
 241		coresight_clear_self_claim_tag_unlocked(&csdev->access);
 242	else
 243		/*
 244		 * The external agent may have not honoured our claim
 245		 * and has manipulated it. Or something else has seriously
 246		 * gone wrong in our driver.
 247		 */
 248		dev_WARN_ONCE(&csdev->dev, 1, "External agent took claim tag");
 249}
 250EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked);
 251
 252void coresight_disclaim_device(struct coresight_device *csdev)
 253{
 254	if (WARN_ON(!csdev))
 255		return;
 256
 257	CS_UNLOCK(csdev->access.base);
 258	coresight_disclaim_device_unlocked(csdev);
 259	CS_LOCK(csdev->access.base);
 260}
 261EXPORT_SYMBOL_GPL(coresight_disclaim_device);
 262
 263/*
 264 * Add a helper as an output device. This function takes the @coresight_mutex
 265 * because it's assumed that it's called from the helper device, outside of the
 266 * core code where the mutex would already be held. Don't add new calls to this
 267 * from inside the core code, instead try to add the new helper to the DT and
 268 * ACPI where it will be picked up and linked automatically.
 269 */
 270void coresight_add_helper(struct coresight_device *csdev,
 271			  struct coresight_device *helper)
 272{
 273	int i;
 274	struct coresight_connection conn = {};
 275	struct coresight_connection *new_conn;
 276
 277	mutex_lock(&coresight_mutex);
 278	conn.dest_fwnode = fwnode_handle_get(dev_fwnode(&helper->dev));
 279	conn.dest_dev = helper;
 280	conn.dest_port = conn.src_port = -1;
 281	conn.src_dev = csdev;
 282
 283	/*
 284	 * Check for duplicates because this is called every time a helper
 285	 * device is re-loaded. Existing connections will get re-linked
 286	 * automatically.
 287	 */
 288	for (i = 0; i < csdev->pdata->nr_outconns; ++i)
 289		if (csdev->pdata->out_conns[i]->dest_fwnode == conn.dest_fwnode)
 290			goto unlock;
 291
 292	new_conn = coresight_add_out_conn(csdev->dev.parent, csdev->pdata,
 293					  &conn);
 294	if (!IS_ERR(new_conn))
 295		coresight_add_in_conn(new_conn);
 296
 297unlock:
 298	mutex_unlock(&coresight_mutex);
 299}
 300EXPORT_SYMBOL_GPL(coresight_add_helper);
 301
 302static int coresight_enable_sink(struct coresight_device *csdev,
 303				 enum cs_mode mode,
 304				 struct coresight_path *path)
 305{
 306	return sink_ops(csdev)->enable(csdev, mode, path);
 307}
 308
 309static void coresight_disable_sink(struct coresight_device *csdev)
 310{
 311	sink_ops(csdev)->disable(csdev);
 312}
 313
 314static int coresight_enable_link(struct coresight_device *csdev,
 315				 struct coresight_device *parent,
 316				 struct coresight_device *child,
 317				 struct coresight_device *source)
 318{
 319	int link_subtype;
 320	struct coresight_connection *inconn, *outconn;
 321
 322	if (!parent || !child)
 323		return -EINVAL;
 324
 325	inconn = coresight_find_out_connection(parent, csdev, source);
 326	outconn = coresight_find_out_connection(csdev, child, source);
 327	link_subtype = csdev->subtype.link_subtype;
 328
 329	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && IS_ERR(inconn))
 330		return PTR_ERR(inconn);
 331	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && IS_ERR(outconn))
 332		return PTR_ERR(outconn);
 333
 334	return link_ops(csdev)->enable(csdev, inconn, outconn);
 335}
 336
 337static void coresight_disable_link(struct coresight_device *csdev,
 338				   struct coresight_device *parent,
 339				   struct coresight_device *child,
 340				   struct coresight_device *source)
 341{
 342	struct coresight_connection *inconn, *outconn;
 343
 344	if (!parent || !child)
 345		return;
 346
 347	inconn = coresight_find_out_connection(parent, csdev, source);
 348	outconn = coresight_find_out_connection(csdev, child, source);
 349
 350	link_ops(csdev)->disable(csdev, inconn, outconn);
 351}
 352
 353static bool coresight_is_helper(struct coresight_device *csdev)
 354{
 355	return csdev->type == CORESIGHT_DEV_TYPE_HELPER;
 356}
 357
 358static int coresight_enable_helper(struct coresight_device *csdev,
 359				   enum cs_mode mode,
 360				   struct coresight_path *path)
 361{
 362	return helper_ops(csdev)->enable(csdev, mode, path);
 363}
 364
 365static void coresight_disable_helper(struct coresight_device *csdev,
 366				     struct coresight_path *path)
 367{
 368	helper_ops(csdev)->disable(csdev, path);
 369}
 370
 371static void coresight_disable_helpers(struct coresight_device *csdev,
 372				      struct coresight_path *path)
 373{
 374	int i;
 375	struct coresight_device *helper;
 376
 377	for (i = 0; i < csdev->pdata->nr_outconns; ++i) {
 378		helper = csdev->pdata->out_conns[i]->dest_dev;
 379		if (helper && coresight_is_helper(helper))
 380			coresight_disable_helper(helper, path);
 381	}
 382}
 383
 384/*
 385 * Helper function to call source_ops(csdev)->disable and also disable the
 386 * helpers.
 387 *
 388 * There is an imbalance between coresight_enable_path() and
 389 * coresight_disable_path(). Enabling also enables the source's helpers as part
 390 * of the path, but disabling always skips the first item in the path (which is
 391 * the source), so sources and their helpers don't get disabled as part of that
 392 * function and we need the extra step here.
 393 */
 394void coresight_disable_source(struct coresight_device *csdev, void *data)
 395{
 396	source_ops(csdev)->disable(csdev, data);
 397	coresight_disable_helpers(csdev, NULL);
 398}
 399EXPORT_SYMBOL_GPL(coresight_disable_source);
 400
 401void coresight_pause_source(struct coresight_device *csdev)
 402{
 403	if (!coresight_is_percpu_source(csdev))
 404		return;
 405
 406	if (source_ops(csdev)->pause_perf)
 407		source_ops(csdev)->pause_perf(csdev);
 408}
 409EXPORT_SYMBOL_GPL(coresight_pause_source);
 410
 411int coresight_resume_source(struct coresight_device *csdev)
 412{
 413	if (!coresight_is_percpu_source(csdev))
 414		return -EOPNOTSUPP;
 415
 416	if (!source_ops(csdev)->resume_perf)
 417		return -EOPNOTSUPP;
 418
 419	return source_ops(csdev)->resume_perf(csdev);
 420}
 421EXPORT_SYMBOL_GPL(coresight_resume_source);
 422
 423/*
 424 * coresight_disable_path_from : Disable components in the given path beyond
 425 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
 426 * disabled.
 427 */
 428static void coresight_disable_path_from(struct coresight_path *path,
 429					struct coresight_node *nd)
 430{
 431	u32 type;
 432	struct coresight_device *csdev, *parent, *child;
 433
 434	if (!nd)
 435		nd = list_first_entry(&path->path_list, struct coresight_node, link);
 436
 437	list_for_each_entry_continue(nd, &path->path_list, link) {
 438		csdev = nd->csdev;
 439		type = csdev->type;
 440
 441		/*
 442		 * ETF devices are tricky... They can be a link or a sink,
 443		 * depending on how they are configured.  If an ETF has been
 444		 * selected as a sink it will be configured as a sink, otherwise
 445		 * go ahead with the link configuration.
 446		 */
 447		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
 448			type = (csdev == coresight_get_sink(path)) ?
 449						CORESIGHT_DEV_TYPE_SINK :
 450						CORESIGHT_DEV_TYPE_LINK;
 451
 452		switch (type) {
 453		case CORESIGHT_DEV_TYPE_SINK:
 454			coresight_disable_sink(csdev);
 455			break;
 456		case CORESIGHT_DEV_TYPE_SOURCE:
 457			/*
 458			 * We skip the first node in the path assuming that it
 459			 * is the source. So we don't expect a source device in
 460			 * the middle of a path.
 461			 */
 462			WARN_ON(1);
 463			break;
 464		case CORESIGHT_DEV_TYPE_LINK:
 465			parent = list_prev_entry(nd, link)->csdev;
 466			child = list_next_entry(nd, link)->csdev;
 467			coresight_disable_link(csdev, parent, child,
 468					       coresight_get_source(path));
 469			break;
 470		default:
 471			break;
 472		}
 473
 474		/* Disable all helpers adjacent along the path last */
 475		coresight_disable_helpers(csdev, path);
 476	}
 477}
 478
 479void coresight_disable_path(struct coresight_path *path)
 480{
 481	coresight_disable_path_from(path, NULL);
 482}
 483EXPORT_SYMBOL_GPL(coresight_disable_path);
 484
 485static int coresight_enable_helpers(struct coresight_device *csdev,
 486				    enum cs_mode mode,
 487				    struct coresight_path *path)
 488{
 489	int i, ret = 0;
 490	struct coresight_device *helper;
 491
 492	for (i = 0; i < csdev->pdata->nr_outconns; ++i) {
 493		helper = csdev->pdata->out_conns[i]->dest_dev;
 494		if (!helper || !coresight_is_helper(helper))
 495			continue;
 496
 497		ret = coresight_enable_helper(helper, mode, path);
 498		if (ret)
 499			return ret;
 500	}
 501
 502	return 0;
 503}
 504
 505int coresight_enable_path(struct coresight_path *path, enum cs_mode mode)
 506{
 507	int ret = 0;
 508	u32 type;
 509	struct coresight_node *nd;
 510	struct coresight_device *csdev, *parent, *child;
 511	struct coresight_device *source;
 512
 513	source = coresight_get_source(path);
 514	list_for_each_entry_reverse(nd, &path->path_list, link) {
 515		csdev = nd->csdev;
 516		type = csdev->type;
 517
 518		/* Enable all helpers adjacent to the path first */
 519		ret = coresight_enable_helpers(csdev, mode, path);
 520		if (ret)
 521			goto err_disable_path;
 522		/*
 523		 * ETF devices are tricky... They can be a link or a sink,
 524		 * depending on how they are configured.  If an ETF has been
 525		 * selected as a sink it will be configured as a sink, otherwise
 526		 * go ahead with the link configuration.
 527		 */
 528		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
 529			type = (csdev == coresight_get_sink(path)) ?
 530						CORESIGHT_DEV_TYPE_SINK :
 531						CORESIGHT_DEV_TYPE_LINK;
 532
 533		switch (type) {
 534		case CORESIGHT_DEV_TYPE_SINK:
 535			ret = coresight_enable_sink(csdev, mode, path);
 536			/*
 537			 * Sink is the first component turned on. If we
 538			 * failed to enable the sink, there are no components
 539			 * that need disabling. Disabling the path here
 540			 * would mean we could disrupt an existing session.
 541			 */
 542			if (ret) {
 543				coresight_disable_helpers(csdev, path);
 544				goto out;
 545			}
 546			break;
 547		case CORESIGHT_DEV_TYPE_SOURCE:
 548			/* sources are enabled from either sysFS or Perf */
 549			break;
 550		case CORESIGHT_DEV_TYPE_LINK:
 551			parent = list_prev_entry(nd, link)->csdev;
 552			child = list_next_entry(nd, link)->csdev;
 553			ret = coresight_enable_link(csdev, parent, child, source);
 554			if (ret)
 555				goto err_disable_helpers;
 556			break;
 557		default:
 558			ret = -EINVAL;
 559			goto err_disable_helpers;
 560		}
 561	}
 562
 563out:
 564	return ret;
 565err_disable_helpers:
 566	coresight_disable_helpers(csdev, path);
 567err_disable_path:
 568	coresight_disable_path_from(path, nd);
 569	goto out;
 570}
 571
 572struct coresight_device *coresight_get_sink(struct coresight_path *path)
 573{
 574	struct coresight_device *csdev;
 575
 576	if (!path)
 577		return NULL;
 578
 579	csdev = list_last_entry(&path->path_list, struct coresight_node, link)->csdev;
 580	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
 581	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
 582		return NULL;
 583
 584	return csdev;
 585}
 586EXPORT_SYMBOL_GPL(coresight_get_sink);
 587
 588u32 coresight_get_sink_id(struct coresight_device *csdev)
 589{
 590	if (!csdev->ea)
 591		return 0;
 592
 593	/*
 594	 * See function etm_perf_add_symlink_sink() to know where
 595	 * this comes from.
 596	 */
 597	return (u32) (unsigned long) csdev->ea->var;
 598}
 599
 600static int coresight_sink_by_id(struct device *dev, const void *data)
 601{
 602	struct coresight_device *csdev = to_coresight_device(dev);
 603
 604	if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
 605	    csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
 606		if (coresight_get_sink_id(csdev) == *(u32 *)data)
 607			return 1;
 608	}
 609
 610	return 0;
 611}
 612
 613/**
 614 * coresight_get_sink_by_id - returns the sink that matches the id
 615 * @id: Id of the sink to match
 616 *
 617 * The name of a sink is unique, whether it is found on the AMBA bus or
 618 * otherwise.  As such the hash of that name can easily be used to identify
 619 * a sink.
 620 */
 621struct coresight_device *coresight_get_sink_by_id(u32 id)
 622{
 623	struct device *dev = NULL;
 624
 625	dev = bus_find_device(&coresight_bustype, NULL, &id,
 626			      coresight_sink_by_id);
 627
 628	return dev ? to_coresight_device(dev) : NULL;
 629}
 630
 631/**
 632 * coresight_get_ref- Helper function to increase reference count to module
 633 * and device.
 634 *
 635 * @csdev: The coresight device to get a reference on.
 636 *
 637 * Return true in successful case and power up the device.
 638 * Return false when failed to get reference of module.
 639 */
 640static bool coresight_get_ref(struct coresight_device *csdev)
 641{
 642	struct device *dev = csdev->dev.parent;
 643
 644	/* Make sure the driver can't be removed */
 645	if (!try_module_get(dev->driver->owner))
 646		return false;
 647	/* Make sure the device can't go away */
 648	get_device(dev);
 649	pm_runtime_get_sync(dev);
 650	return true;
 651}
 652
 653/**
 654 * coresight_put_ref- Helper function to decrease reference count to module
 655 * and device. Power off the device.
 656 *
 657 * @csdev: The coresight device to decrement a reference from.
 658 */
 659static void coresight_put_ref(struct coresight_device *csdev)
 660{
 661	struct device *dev = csdev->dev.parent;
 662
 663	pm_runtime_put(dev);
 664	put_device(dev);
 665	module_put(dev->driver->owner);
 666}
 667
 668/*
 669 * coresight_grab_device - Power up this device and any of the helper
 670 * devices connected to it for trace operation. Since the helper devices
 671 * don't appear on the trace path, they should be handled along with the
 672 * master device.
 673 */
 674static int coresight_grab_device(struct coresight_device *csdev)
 675{
 676	int i;
 677
 678	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
 679		struct coresight_device *child;
 680
 681		child = csdev->pdata->out_conns[i]->dest_dev;
 682		if (child && coresight_is_helper(child))
 683			if (!coresight_get_ref(child))
 684				goto err;
 685	}
 686	if (coresight_get_ref(csdev))
 687		return 0;
 688err:
 689	for (i--; i >= 0; i--) {
 690		struct coresight_device *child;
 691
 692		child = csdev->pdata->out_conns[i]->dest_dev;
 693		if (child && coresight_is_helper(child))
 694			coresight_put_ref(child);
 695	}
 696	return -ENODEV;
 697}
 698
 699/*
 700 * coresight_drop_device - Release this device and any of the helper
 701 * devices connected to it.
 702 */
 703static void coresight_drop_device(struct coresight_device *csdev)
 704{
 705	int i;
 706
 707	coresight_put_ref(csdev);
 708	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
 709		struct coresight_device *child;
 710
 711		child = csdev->pdata->out_conns[i]->dest_dev;
 712		if (child && coresight_is_helper(child))
 713			coresight_put_ref(child);
 714	}
 715}
 716
 717/*
 718 * coresight device will read their existing or alloc a trace ID, if their trace_id
 719 * callback is set.
 720 *
 721 * Return 0 if the trace_id callback is not set.
 722 * Return the result of the trace_id callback if it is set. The return value
 723 * will be the trace_id if successful, and an error number if it fails.
 724 */
 725static int coresight_get_trace_id(struct coresight_device *csdev,
 726				  enum cs_mode mode,
 727				  struct coresight_device *sink)
 728{
 729	if (coresight_ops(csdev)->trace_id)
 730		return coresight_ops(csdev)->trace_id(csdev, mode, sink);
 731
 732	return 0;
 733}
 734
 735/*
 736 * Call this after creating the path and before enabling it. This leaves
 737 * the trace ID set on the path, or it remains 0 if it couldn't be assigned.
 738 */
 739void coresight_path_assign_trace_id(struct coresight_path *path,
 740				    enum cs_mode mode)
 741{
 742	struct coresight_device *sink = coresight_get_sink(path);
 743	struct coresight_node *nd;
 744	int trace_id;
 745
 746	list_for_each_entry(nd, &path->path_list, link) {
 747		/* Assign a trace ID to the path for the first device that wants to do it */
 748		trace_id = coresight_get_trace_id(nd->csdev, mode, sink);
 749
 750		/*
 751		 * 0 in this context is that it didn't want to assign so keep searching.
 752		 * Non 0 is either success or fail.
 753		 */
 754		if (trace_id != 0) {
 755			path->trace_id = trace_id;
 756			return;
 757		}
 758	}
 759}
 760
 761/**
 762 * _coresight_build_path - recursively build a path from a @csdev to a sink.
 763 * @csdev:	The device to start from.
 764 * @source:	The trace source device of the path.
 765 * @sink:	The final sink we want in this path.
 766 * @path:	The list to add devices to.
 767 *
 768 * The tree of Coresight device is traversed until @sink is found.
 769 * From there the sink is added to the list along with all the devices that led
 770 * to that point - the end result is a list from source to sink. In that list
 771 * the source is the first device and the sink the last one.
 772 */
 773static int _coresight_build_path(struct coresight_device *csdev,
 774				 struct coresight_device *source,
 775				 struct coresight_device *sink,
 776				 struct coresight_path *path)
 777{
 778	int i, ret;
 779	bool found = false;
 780	struct coresight_node *node;
 781
 782	/* The sink has been found.  Enqueue the element */
 783	if (csdev == sink)
 784		goto out;
 785
 786	if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&
 787	    sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {
 788		if (_coresight_build_path(sink, source, sink, path) == 0) {
 789			found = true;
 790			goto out;
 791		}
 792	}
 793
 794	/* Not a sink - recursively explore each port found on this element */
 795	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
 796		struct coresight_device *child_dev;
 797
 798		child_dev = csdev->pdata->out_conns[i]->dest_dev;
 799
 800		if (coresight_blocks_source(source, csdev->pdata->out_conns[i]))
 801			continue;
 802
 803		if (child_dev &&
 804		    _coresight_build_path(child_dev, source, sink, path) == 0) {
 805			found = true;
 806			break;
 807		}
 808	}
 809
 810	if (!found)
 811		return -ENODEV;
 812
 813out:
 814	/*
 815	 * A path from this element to a sink has been found.  The elements
 816	 * leading to the sink are already enqueued, all that is left to do
 817	 * is tell the PM runtime core we need this element and add a node
 818	 * for it.
 819	 */
 820	ret = coresight_grab_device(csdev);
 821	if (ret)
 822		return ret;
 823
 824	node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
 825	if (!node)
 826		return -ENOMEM;
 827
 828	node->csdev = csdev;
 829	list_add(&node->link, &path->path_list);
 830
 831	return 0;
 832}
 833
 834struct coresight_path *coresight_build_path(struct coresight_device *source,
 835				       struct coresight_device *sink)
 836{
 837	struct coresight_path *path;
 838	int rc;
 839
 840	if (!sink)
 841		return ERR_PTR(-EINVAL);
 842
 843	path = kzalloc(sizeof(struct coresight_path), GFP_KERNEL);
 844	if (!path)
 845		return ERR_PTR(-ENOMEM);
 846
 847	INIT_LIST_HEAD(&path->path_list);
 848
 849	rc = _coresight_build_path(source, source, sink, path);
 850	if (rc) {
 851		kfree(path);
 852		return ERR_PTR(rc);
 853	}
 854
 855	return path;
 856}
 857
 858/**
 859 * coresight_release_path - release a previously built path.
 860 * @path:	the path to release.
 861 *
 862 * Go through all the elements of a path and 1) removed it from the list and
 863 * 2) free the memory allocated for each node.
 864 */
 865void coresight_release_path(struct coresight_path *path)
 866{
 867	struct coresight_device *csdev;
 868	struct coresight_node *nd, *next;
 869
 870	list_for_each_entry_safe(nd, next, &path->path_list, link) {
 871		csdev = nd->csdev;
 872
 873		coresight_drop_device(csdev);
 874		list_del(&nd->link);
 875		kfree(nd);
 876	}
 877
 878	kfree(path);
 879}
 880
 881/* return true if the device is a suitable type for a default sink */
 882static bool coresight_is_def_sink_type(struct coresight_device *csdev)
 883{
 884	/* sink & correct subtype */
 885	if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
 886	     (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
 887	    (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
 888		return true;
 889	return false;
 890}
 891
 892/**
 893 * coresight_select_best_sink - return the best sink for use as default from
 894 * the two provided.
 895 *
 896 * @sink:	current best sink.
 897 * @depth:      search depth where current sink was found.
 898 * @new_sink:	new sink for comparison with current sink.
 899 * @new_depth:  search depth where new sink was found.
 900 *
 901 * Sinks prioritised according to coresight_dev_subtype_sink, with only
 902 * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used.
 903 *
 904 * Where two sinks of equal priority are found, the sink closest to the
 905 * source is used (smallest search depth).
 906 *
 907 * return @new_sink & update @depth if better than @sink, else return @sink.
 908 */
 909static struct coresight_device *
 910coresight_select_best_sink(struct coresight_device *sink, int *depth,
 911			   struct coresight_device *new_sink, int new_depth)
 912{
 913	bool update = false;
 914
 915	if (!sink) {
 916		/* first found at this level */
 917		update = true;
 918	} else if (new_sink->subtype.sink_subtype >
 919		   sink->subtype.sink_subtype) {
 920		/* found better sink */
 921		update = true;
 922	} else if ((new_sink->subtype.sink_subtype ==
 923		    sink->subtype.sink_subtype) &&
 924		   (*depth > new_depth)) {
 925		/* found same but closer sink */
 926		update = true;
 927	}
 928
 929	if (update)
 930		*depth = new_depth;
 931	return update ? new_sink : sink;
 932}
 933
 934/**
 935 * coresight_find_sink - recursive function to walk trace connections from
 936 * source to find a suitable default sink.
 937 *
 938 * @csdev: source / current device to check.
 939 * @depth: [in] search depth of calling dev, [out] depth of found sink.
 940 *
 941 * This will walk the connection path from a source (ETM) till a suitable
 942 * sink is encountered and return that sink to the original caller.
 943 *
 944 * If current device is a plain sink return that & depth, otherwise recursively
 945 * call child connections looking for a sink. Select best possible using
 946 * coresight_select_best_sink.
 947 *
 948 * return best sink found, or NULL if not found at this node or child nodes.
 949 */
 950static struct coresight_device *
 951coresight_find_sink(struct coresight_device *csdev, int *depth)
 952{
 953	int i, curr_depth = *depth + 1, found_depth = 0;
 954	struct coresight_device *found_sink = NULL;
 955
 956	if (coresight_is_def_sink_type(csdev)) {
 957		found_depth = curr_depth;
 958		found_sink = csdev;
 959		if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
 960			goto return_def_sink;
 961		/* look past LINKSINK for something better */
 962	}
 963
 964	/*
 965	 * Not a sink we want - or possible child sink may be better.
 966	 * recursively explore each port found on this element.
 967	 */
 968	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
 969		struct coresight_device *child_dev, *sink = NULL;
 970		int child_depth = curr_depth;
 971
 972		child_dev = csdev->pdata->out_conns[i]->dest_dev;
 973		if (child_dev)
 974			sink = coresight_find_sink(child_dev, &child_depth);
 975
 976		if (sink)
 977			found_sink = coresight_select_best_sink(found_sink,
 978								&found_depth,
 979								sink,
 980								child_depth);
 981	}
 982
 983return_def_sink:
 984	/* return found sink and depth */
 985	if (found_sink)
 986		*depth = found_depth;
 987	return found_sink;
 988}
 989
 990/**
 991 * coresight_find_default_sink: Find a sink suitable for use as a
 992 * default sink.
 993 *
 994 * @csdev: starting source to find a connected sink.
 995 *
 996 * Walks connections graph looking for a suitable sink to enable for the
 997 * supplied source. Uses CoreSight device subtypes and distance from source
 998 * to select the best sink.
 999 *
1000 * If a sink is found, then the default sink for this device is set and
1001 * will be automatically used in future.
1002 *
1003 * Used in cases where the CoreSight user (perf / sysfs) has not selected a
1004 * sink.
1005 */
1006struct coresight_device *
1007coresight_find_default_sink(struct coresight_device *csdev)
1008{
1009	int depth = 0;
1010
1011	/* look for a default sink if we have not found for this device */
1012	if (!csdev->def_sink) {
1013		if (coresight_is_percpu_source(csdev))
1014			csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));
1015		if (!csdev->def_sink)
1016			csdev->def_sink = coresight_find_sink(csdev, &depth);
1017	}
1018	return csdev->def_sink;
1019}
1020EXPORT_SYMBOL_GPL(coresight_find_default_sink);
1021
1022static int coresight_remove_sink_ref(struct device *dev, void *data)
1023{
1024	struct coresight_device *sink = data;
1025	struct coresight_device *source = to_coresight_device(dev);
1026
1027	if (source->def_sink == sink)
1028		source->def_sink = NULL;
1029	return 0;
1030}
1031
1032/**
1033 * coresight_clear_default_sink: Remove all default sink references to the
1034 * supplied sink.
1035 *
1036 * If supplied device is a sink, then check all the bus devices and clear
1037 * out all the references to this sink from the coresight_device def_sink
1038 * parameter.
1039 *
1040 * @csdev: coresight sink - remove references to this from all sources.
1041 */
1042static void coresight_clear_default_sink(struct coresight_device *csdev)
1043{
1044	if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
1045	    (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
1046		bus_for_each_dev(&coresight_bustype, NULL, csdev,
1047				 coresight_remove_sink_ref);
1048	}
1049}
1050
1051static void coresight_device_release(struct device *dev)
1052{
1053	struct coresight_device *csdev = to_coresight_device(dev);
1054
1055	fwnode_handle_put(csdev->dev.fwnode);
1056	free_percpu(csdev->perf_sink_id_map.cpu_map);
1057	kfree(csdev);
1058}
1059
1060static int coresight_orphan_match(struct device *dev, void *data)
1061{
1062	int i, ret = 0;
1063	bool still_orphan = false;
1064	struct coresight_device *dst_csdev = data;
1065	struct coresight_device *src_csdev = to_coresight_device(dev);
1066	struct coresight_connection *conn;
1067	bool fixup_self = (src_csdev == dst_csdev);
1068
1069	/* Move on to another component if no connection is orphan */
1070	if (!src_csdev->orphan)
1071		return 0;
1072	/*
1073	 * Circle through all the connections of that component.  If we find
1074	 * an orphan connection whose name matches @dst_csdev, link it.
1075	 */
1076	for (i = 0; i < src_csdev->pdata->nr_outconns; i++) {
1077		conn = src_csdev->pdata->out_conns[i];
1078
1079		/* Fix filter source device before skip the port */
1080		if (conn->filter_src_fwnode && !conn->filter_src_dev) {
1081			if (dst_csdev &&
1082			    (conn->filter_src_fwnode == dst_csdev->dev.fwnode) &&
1083			    !WARN_ON_ONCE(!coresight_is_device_source(dst_csdev)))
1084				conn->filter_src_dev = dst_csdev;
1085			else
1086				still_orphan = true;
1087		}
1088
1089		/* Skip the port if it's already connected. */
1090		if (conn->dest_dev)
1091			continue;
1092
1093		/*
1094		 * If we are at the "new" device, which triggered this search,
1095		 * we must find the remote device from the fwnode in the
1096		 * connection.
1097		 */
1098		if (fixup_self)
1099			dst_csdev = coresight_find_csdev_by_fwnode(
1100				conn->dest_fwnode);
1101
1102		/* Does it match this newly added device? */
1103		if (dst_csdev && conn->dest_fwnode == dst_csdev->dev.fwnode) {
1104			ret = coresight_make_links(src_csdev, conn, dst_csdev);
1105			if (ret)
1106				return ret;
1107
1108			/*
1109			 * Install the device connection. This also indicates that
1110			 * the links are operational on both ends.
1111			 */
1112			conn->dest_dev = dst_csdev;
1113			conn->src_dev = src_csdev;
1114
1115			ret = coresight_add_in_conn(conn);
1116			if (ret)
1117				return ret;
1118		} else {
1119			/* This component still has an orphan */
1120			still_orphan = true;
1121		}
1122	}
1123
1124	src_csdev->orphan = still_orphan;
1125
1126	/*
1127	 * Returning '0' in case we didn't encounter any error,
1128	 * ensures that all known component on the bus will be checked.
1129	 */
1130	return 0;
1131}
1132
1133static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
1134{
1135	return bus_for_each_dev(&coresight_bustype, NULL,
1136			 csdev, coresight_orphan_match);
1137}
1138
1139static int coresight_clear_filter_source(struct device *dev, void *data)
1140{
1141	int i;
1142	struct coresight_device *source = data;
1143	struct coresight_device *csdev = to_coresight_device(dev);
1144
1145	for (i = 0; i < csdev->pdata->nr_outconns; ++i) {
1146		if (csdev->pdata->out_conns[i]->filter_src_dev == source)
1147			csdev->pdata->out_conns[i]->filter_src_dev = NULL;
1148	}
1149	return 0;
1150}
1151
1152/* coresight_remove_conns - Remove other device's references to this device */
1153static void coresight_remove_conns(struct coresight_device *csdev)
1154{
1155	int i, j;
1156	struct coresight_connection *conn;
1157
1158	if (coresight_is_device_source(csdev))
1159		bus_for_each_dev(&coresight_bustype, NULL, csdev,
1160				 coresight_clear_filter_source);
1161
1162	/*
1163	 * Remove the input connection references from the destination device
1164	 * for each output connection.
1165	 */
1166	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
1167		conn = csdev->pdata->out_conns[i];
1168		if (conn->filter_src_fwnode) {
1169			conn->filter_src_dev = NULL;
1170			fwnode_handle_put(conn->filter_src_fwnode);
1171		}
1172
1173		if (!conn->dest_dev)
1174			continue;
1175
1176		for (j = 0; j < conn->dest_dev->pdata->nr_inconns; ++j)
1177			if (conn->dest_dev->pdata->in_conns[j] == conn) {
1178				conn->dest_dev->pdata->in_conns[j] = NULL;
1179				break;
1180			}
1181	}
1182
1183	/*
1184	 * For all input connections, remove references to this device.
1185	 * Connection objects are shared so modifying this device's input
1186	 * connections affects the other device's output connection.
1187	 */
1188	for (i = 0; i < csdev->pdata->nr_inconns; ++i) {
1189		conn = csdev->pdata->in_conns[i];
1190		/* Input conns array is sparse */
1191		if (!conn)
1192			continue;
1193
1194		conn->src_dev->orphan = true;
1195		coresight_remove_links(conn->src_dev, conn);
1196		conn->dest_dev = NULL;
1197	}
1198}
1199
1200/**
1201 * coresight_timeout_action - loop until a bit has changed to a specific register
1202 *                  state, with a callback after every trial.
1203 * @csa: coresight device access for the device
1204 * @offset: Offset of the register from the base of the device.
1205 * @position: the position of the bit of interest.
1206 * @value: the value the bit should have.
1207 * @cb: Call back after each trial.
1208 *
1209 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1210 * TIMEOUT_US has elapsed, which ever happens first.
1211 */
1212int coresight_timeout_action(struct csdev_access *csa, u32 offset,
1213		      int position, int value,
1214			  coresight_timeout_cb_t cb)
1215{
1216	int i;
1217	u32 val;
1218
1219	for (i = TIMEOUT_US; i > 0; i--) {
1220		val = csdev_access_read32(csa, offset);
1221		/* waiting on the bit to go from 0 to 1 */
1222		if (value) {
1223			if (val & BIT(position))
1224				return 0;
1225		/* waiting on the bit to go from 1 to 0 */
1226		} else {
1227			if (!(val & BIT(position)))
1228				return 0;
1229		}
1230		if (cb)
1231			cb(csa, offset, position, value);
1232		/*
1233		 * Delay is arbitrary - the specification doesn't say how long
1234		 * we are expected to wait.  Extra check required to make sure
1235		 * we don't wait needlessly on the last iteration.
1236		 */
1237		if (i - 1)
1238			udelay(1);
1239	}
1240
1241	return -EAGAIN;
1242}
1243EXPORT_SYMBOL_GPL(coresight_timeout_action);
1244
1245int coresight_timeout(struct csdev_access *csa, u32 offset,
1246		      int position, int value)
1247{
1248	return coresight_timeout_action(csa, offset, position, value, NULL);
1249}
1250EXPORT_SYMBOL_GPL(coresight_timeout);
1251
1252u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset)
1253{
1254	return csdev_access_relaxed_read32(&csdev->access, offset);
1255}
1256
1257u32 coresight_read32(struct coresight_device *csdev, u32 offset)
1258{
1259	return csdev_access_read32(&csdev->access, offset);
1260}
1261
1262void coresight_relaxed_write32(struct coresight_device *csdev,
1263			       u32 val, u32 offset)
1264{
1265	csdev_access_relaxed_write32(&csdev->access, val, offset);
1266}
1267
1268void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset)
1269{
1270	csdev_access_write32(&csdev->access, val, offset);
1271}
1272
1273u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset)
1274{
1275	return csdev_access_relaxed_read64(&csdev->access, offset);
1276}
1277
1278u64 coresight_read64(struct coresight_device *csdev, u32 offset)
1279{
1280	return csdev_access_read64(&csdev->access, offset);
1281}
1282
1283void coresight_relaxed_write64(struct coresight_device *csdev,
1284			       u64 val, u32 offset)
1285{
1286	csdev_access_relaxed_write64(&csdev->access, val, offset);
1287}
1288
1289void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset)
1290{
1291	csdev_access_write64(&csdev->access, val, offset);
1292}
1293
1294/*
1295 * coresight_release_platform_data: Release references to the devices connected
1296 * to the output port of this device.
1297 */
1298void coresight_release_platform_data(struct coresight_device *csdev,
1299				     struct device *dev,
1300				     struct coresight_platform_data *pdata)
1301{
1302	int i;
1303	struct coresight_connection **conns = pdata->out_conns;
1304
1305	for (i = 0; i < pdata->nr_outconns; i++) {
1306		/* If we have made the links, remove them now */
1307		if (csdev && conns[i]->dest_dev)
1308			coresight_remove_links(csdev, conns[i]);
1309		/*
1310		 * Drop the refcount and clear the handle as this device
1311		 * is going away
1312		 */
1313		fwnode_handle_put(conns[i]->dest_fwnode);
1314		conns[i]->dest_fwnode = NULL;
1315		devm_kfree(dev, conns[i]);
1316	}
1317	devm_kfree(dev, pdata->out_conns);
1318	devm_kfree(dev, pdata->in_conns);
1319	devm_kfree(dev, pdata);
1320	if (csdev)
1321		coresight_remove_conns_sysfs_group(csdev);
1322}
1323
1324struct coresight_device *coresight_register(struct coresight_desc *desc)
1325{
1326	int ret;
1327	struct coresight_device *csdev;
1328	bool registered = false;
1329
1330	csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1331	if (!csdev) {
1332		ret = -ENOMEM;
1333		goto err_out;
1334	}
1335
1336	csdev->pdata = desc->pdata;
1337
1338	csdev->type = desc->type;
1339	csdev->subtype = desc->subtype;
1340	csdev->ops = desc->ops;
1341	csdev->access = desc->access;
1342	csdev->orphan = true;
1343
1344	csdev->dev.type = &coresight_dev_type[desc->type];
1345	csdev->dev.groups = desc->groups;
1346	csdev->dev.parent = desc->dev;
1347	csdev->dev.release = coresight_device_release;
1348	csdev->dev.bus = &coresight_bustype;
1349	/*
1350	 * Hold the reference to our parent device. This will be
1351	 * dropped only in coresight_device_release().
1352	 */
1353	csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1354	dev_set_name(&csdev->dev, "%s", desc->name);
1355
1356	if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1357	    csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1358		raw_spin_lock_init(&csdev->perf_sink_id_map.lock);
1359		csdev->perf_sink_id_map.cpu_map = alloc_percpu(atomic_t);
1360		if (!csdev->perf_sink_id_map.cpu_map) {
1361			kfree(csdev);
1362			ret = -ENOMEM;
1363			goto err_out;
1364		}
1365	}
1366	/*
1367	 * Make sure the device registration and the connection fixup
1368	 * are synchronised, so that we don't see uninitialised devices
1369	 * on the coresight bus while trying to resolve the connections.
1370	 */
1371	mutex_lock(&coresight_mutex);
1372
1373	ret = device_register(&csdev->dev);
1374	if (ret) {
1375		put_device(&csdev->dev);
1376		/*
1377		 * All resources are free'd explicitly via
1378		 * coresight_device_release(), triggered from put_device().
1379		 */
1380		goto out_unlock;
1381	}
1382
1383	if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1384	     csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
1385	    sink_ops(csdev)->alloc_buffer) {
1386		ret = etm_perf_add_symlink_sink(csdev);
1387
1388		if (ret) {
1389			device_unregister(&csdev->dev);
1390			/*
1391			 * As with the above, all resources are free'd
1392			 * explicitly via coresight_device_release() triggered
1393			 * from put_device(), which is in turn called from
1394			 * function device_unregister().
1395			 */
1396			goto out_unlock;
1397		}
1398	}
1399	/* Device is now registered */
1400	registered = true;
1401
1402	ret = coresight_create_conns_sysfs_group(csdev);
1403	if (!ret)
1404		ret = coresight_fixup_orphan_conns(csdev);
1405
1406out_unlock:
1407	mutex_unlock(&coresight_mutex);
1408	/* Success */
1409	if (!ret) {
1410		if (cti_assoc_ops && cti_assoc_ops->add)
1411			cti_assoc_ops->add(csdev);
1412		return csdev;
1413	}
1414
1415	/* Unregister the device if needed */
1416	if (registered) {
1417		coresight_unregister(csdev);
1418		return ERR_PTR(ret);
1419	}
1420
1421err_out:
1422	/* Cleanup the connection information */
1423	coresight_release_platform_data(NULL, desc->dev, desc->pdata);
1424	return ERR_PTR(ret);
1425}
1426EXPORT_SYMBOL_GPL(coresight_register);
1427
1428void coresight_unregister(struct coresight_device *csdev)
1429{
1430	etm_perf_del_symlink_sink(csdev);
1431	/* Remove references of that device in the topology */
1432	if (cti_assoc_ops && cti_assoc_ops->remove)
1433		cti_assoc_ops->remove(csdev);
1434	coresight_remove_conns(csdev);
1435	coresight_clear_default_sink(csdev);
1436	coresight_release_platform_data(csdev, csdev->dev.parent, csdev->pdata);
1437	device_unregister(&csdev->dev);
1438}
1439EXPORT_SYMBOL_GPL(coresight_unregister);
1440
1441
1442/*
1443 * coresight_search_device_idx - Search the fwnode handle of a device
1444 * in the given dev_idx list. Must be called with the coresight_mutex held.
1445 *
1446 * Returns the index of the entry, when found. Otherwise, -ENOENT.
1447 */
1448static int coresight_search_device_idx(struct coresight_dev_list *dict,
1449				       struct fwnode_handle *fwnode)
1450{
1451	int i;
1452
1453	for (i = 0; i < dict->nr_idx; i++)
1454		if (dict->fwnode_list[i] == fwnode)
1455			return i;
1456	return -ENOENT;
1457}
1458
1459static bool coresight_compare_type(enum coresight_dev_type type_a,
1460				   union coresight_dev_subtype subtype_a,
1461				   enum coresight_dev_type type_b,
1462				   union coresight_dev_subtype subtype_b)
1463{
1464	if (type_a != type_b)
1465		return false;
1466
1467	switch (type_a) {
1468	case CORESIGHT_DEV_TYPE_SINK:
1469		return subtype_a.sink_subtype == subtype_b.sink_subtype;
1470	case CORESIGHT_DEV_TYPE_LINK:
1471		return subtype_a.link_subtype == subtype_b.link_subtype;
1472	case CORESIGHT_DEV_TYPE_LINKSINK:
1473		return subtype_a.link_subtype == subtype_b.link_subtype &&
1474		       subtype_a.sink_subtype == subtype_b.sink_subtype;
1475	case CORESIGHT_DEV_TYPE_SOURCE:
1476		return subtype_a.source_subtype == subtype_b.source_subtype;
1477	case CORESIGHT_DEV_TYPE_HELPER:
1478		return subtype_a.helper_subtype == subtype_b.helper_subtype;
1479	default:
1480		return false;
1481	}
1482}
1483
1484struct coresight_device *
1485coresight_find_input_type(struct coresight_platform_data *pdata,
1486			  enum coresight_dev_type type,
1487			  union coresight_dev_subtype subtype)
1488{
1489	int i;
1490	struct coresight_connection *conn;
1491
1492	for (i = 0; i < pdata->nr_inconns; ++i) {
1493		conn = pdata->in_conns[i];
1494		if (conn &&
1495		    coresight_compare_type(type, subtype, conn->src_dev->type,
1496					   conn->src_dev->subtype))
1497			return conn->src_dev;
1498	}
1499	return NULL;
1500}
1501EXPORT_SYMBOL_GPL(coresight_find_input_type);
1502
1503struct coresight_device *
1504coresight_find_output_type(struct coresight_platform_data *pdata,
1505			   enum coresight_dev_type type,
1506			   union coresight_dev_subtype subtype)
1507{
1508	int i;
1509	struct coresight_connection *conn;
1510
1511	for (i = 0; i < pdata->nr_outconns; ++i) {
1512		conn = pdata->out_conns[i];
1513		if (conn->dest_dev &&
1514		    coresight_compare_type(type, subtype, conn->dest_dev->type,
1515					   conn->dest_dev->subtype))
1516			return conn->dest_dev;
1517	}
1518	return NULL;
1519}
1520EXPORT_SYMBOL_GPL(coresight_find_output_type);
1521
1522bool coresight_loses_context_with_cpu(struct device *dev)
1523{
1524	return fwnode_property_present(dev_fwnode(dev),
1525				       "arm,coresight-loses-context-with-cpu");
1526}
1527EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
1528
1529/*
1530 * coresight_alloc_device_name - Get an index for a given device in the
1531 * device index list specific to a driver. An index is allocated for a
1532 * device and is tracked with the fwnode_handle to prevent allocating
1533 * duplicate indices for the same device (e.g, if we defer probing of
1534 * a device due to dependencies), in case the index is requested again.
1535 */
1536char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1537				  struct device *dev)
1538{
1539	int idx;
1540	char *name = NULL;
1541	struct fwnode_handle **list;
1542
1543	mutex_lock(&coresight_mutex);
1544
1545	idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1546	if (idx < 0) {
1547		/* Make space for the new entry */
1548		idx = dict->nr_idx;
1549		list = krealloc_array(dict->fwnode_list,
1550				      idx + 1, sizeof(*dict->fwnode_list),
1551				      GFP_KERNEL);
1552		if (ZERO_OR_NULL_PTR(list)) {
1553			idx = -ENOMEM;
1554			goto done;
1555		}
1556
1557		list[idx] = dev_fwnode(dev);
1558		dict->fwnode_list = list;
1559		dict->nr_idx = idx + 1;
1560	}
1561
1562	name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1563done:
1564	mutex_unlock(&coresight_mutex);
1565	return name;
1566}
1567EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1568
1569const struct bus_type coresight_bustype = {
1570	.name	= "coresight",
1571};
1572
1573static int coresight_panic_sync(struct device *dev, void *data)
1574{
1575	int mode;
1576	struct coresight_device *csdev;
1577
1578	/* Run through panic sync handlers for all enabled devices */
1579	csdev = container_of(dev, struct coresight_device, dev);
1580	mode = coresight_get_mode(csdev);
1581
1582	if ((mode == CS_MODE_SYSFS) || (mode == CS_MODE_PERF)) {
1583		if (panic_ops(csdev))
1584			panic_ops(csdev)->sync(csdev);
1585	}
1586
1587	return 0;
1588}
1589
1590static int coresight_panic_cb(struct notifier_block *self,
1591			       unsigned long v, void *p)
1592{
1593	bus_for_each_dev(&coresight_bustype, NULL, NULL,
1594				 coresight_panic_sync);
1595
1596	return 0;
1597}
1598
1599static struct notifier_block coresight_notifier = {
1600	.notifier_call = coresight_panic_cb,
1601};
1602
1603static int __init coresight_init(void)
1604{
1605	int ret;
1606
1607	ret = bus_register(&coresight_bustype);
1608	if (ret)
1609		return ret;
1610
1611	ret = etm_perf_init();
1612	if (ret)
1613		goto exit_bus_unregister;
1614
1615	/* Register function to be called for panic */
1616	ret = atomic_notifier_chain_register(&panic_notifier_list,
1617					     &coresight_notifier);
1618	if (ret)
1619		goto exit_perf;
1620
1621	/* initialise the coresight syscfg API */
1622	ret = cscfg_init();
1623	if (!ret)
1624		return 0;
1625
1626	atomic_notifier_chain_unregister(&panic_notifier_list,
1627					     &coresight_notifier);
1628exit_perf:
1629	etm_perf_exit();
1630exit_bus_unregister:
1631	bus_unregister(&coresight_bustype);
1632	return ret;
1633}
1634
1635static void __exit coresight_exit(void)
1636{
1637	cscfg_exit();
1638	atomic_notifier_chain_unregister(&panic_notifier_list,
1639					     &coresight_notifier);
1640	etm_perf_exit();
1641	bus_unregister(&coresight_bustype);
1642}
1643
1644module_init(coresight_init);
1645module_exit(coresight_exit);
1646
1647int coresight_init_driver(const char *drv, struct amba_driver *amba_drv,
1648			  struct platform_driver *pdev_drv, struct module *owner)
1649{
1650	int ret;
1651
1652	ret = __amba_driver_register(amba_drv, owner);
1653	if (ret) {
1654		pr_err("%s: error registering AMBA driver\n", drv);
1655		return ret;
1656	}
1657
1658	ret = __platform_driver_register(pdev_drv, owner);
1659	if (!ret)
1660		return 0;
1661
1662	pr_err("%s: error registering platform driver\n", drv);
1663	amba_driver_unregister(amba_drv);
1664	return ret;
1665}
1666EXPORT_SYMBOL_GPL(coresight_init_driver);
1667
1668void coresight_remove_driver(struct amba_driver *amba_drv,
1669			     struct platform_driver *pdev_drv)
1670{
1671	amba_driver_unregister(amba_drv);
1672	platform_driver_unregister(pdev_drv);
1673}
1674EXPORT_SYMBOL_GPL(coresight_remove_driver);
1675
1676int coresight_etm_get_trace_id(struct coresight_device *csdev, enum cs_mode mode,
1677			       struct coresight_device *sink)
1678{
1679	int cpu, trace_id;
1680
1681	if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE || !source_ops(csdev)->cpu_id)
1682		return -EINVAL;
1683
1684	cpu = source_ops(csdev)->cpu_id(csdev);
1685	switch (mode) {
1686	case CS_MODE_SYSFS:
1687		trace_id = coresight_trace_id_get_cpu_id(cpu);
1688		break;
1689	case CS_MODE_PERF:
1690		if (WARN_ON(!sink))
1691			return -EINVAL;
1692
1693		trace_id = coresight_trace_id_get_cpu_id_map(cpu, &sink->perf_sink_id_map);
1694		break;
1695	default:
1696		trace_id = -EINVAL;
1697		break;
1698	}
1699
1700	if (!IS_VALID_CS_TRACE_ID(trace_id))
1701		dev_err(&csdev->dev,
1702			"Failed to allocate trace ID on CPU%d\n", cpu);
1703
1704	return trace_id;
1705}
1706EXPORT_SYMBOL_GPL(coresight_etm_get_trace_id);
1707
1708/*
1709 * Attempt to find and enable programming clock (pclk) and trace clock (atclk)
1710 * for the given device.
1711 *
1712 * For ACPI devices, clocks are controlled by firmware, so bail out early in
1713 * this case. Also, skip enabling pclk if the clock is managed by the AMBA
1714 * bus driver instead.
1715 *
1716 * atclk is an optional clock, it will be only enabled when it is existed.
1717 * Otherwise, a NULL pointer will be returned to caller.
1718 *
1719 * Returns: '0' on Success; Error code otherwise.
1720 */
1721int coresight_get_enable_clocks(struct device *dev, struct clk **pclk,
1722				struct clk **atclk)
1723{
1724	WARN_ON(!pclk);
1725
1726	if (has_acpi_companion(dev))
1727		return 0;
1728
1729	if (!dev_is_amba(dev)) {
1730		/*
1731		 * "apb_pclk" is the default clock name for an Arm Primecell
1732		 * peripheral, while "apb" is used only by the CTCU driver.
1733		 *
1734		 * For easier maintenance, CoreSight drivers should use
1735		 * "apb_pclk" as the programming clock name.
1736		 */
1737		*pclk = devm_clk_get_optional_enabled(dev, "apb_pclk");
1738		if (!*pclk)
1739			*pclk = devm_clk_get_optional_enabled(dev, "apb");
1740		if (IS_ERR(*pclk))
1741			return PTR_ERR(*pclk);
1742	}
1743
1744	/* Initialization of atclk is skipped if it is a NULL pointer. */
1745	if (atclk) {
1746		*atclk = devm_clk_get_optional_enabled(dev, "atclk");
1747		if (IS_ERR(*atclk))
1748			return PTR_ERR(*atclk);
1749	}
1750
1751	return 0;
1752}
1753EXPORT_SYMBOL_GPL(coresight_get_enable_clocks);
1754
1755MODULE_LICENSE("GPL v2");
1756MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1757MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1758MODULE_DESCRIPTION("Arm CoreSight tracer driver");
~/ Read-only file preview.