Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.5.6.
   1/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
   2 *
   3 * This program is free software; you can redistribute it and/or modify
   4 * it under the terms of the GNU General Public License version 2 and
   5 * only version 2 as published by the Free Software Foundation.
   6 *
   7 * This program is distributed in the hope that it will be useful,
   8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
   9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10 * GNU General Public License for more details.
  11 */
  12
  13#include <linux/kernel.h>
  14#include <linux/init.h>
  15#include <linux/types.h>
  16#include <linux/device.h>
  17#include <linux/io.h>
  18#include <linux/err.h>
  19#include <linux/export.h>
  20#include <linux/slab.h>
  21#include <linux/mutex.h>
  22#include <linux/clk.h>
  23#include <linux/coresight.h>
  24#include <linux/of_platform.h>
  25#include <linux/delay.h>
  26#include <linux/pm_runtime.h>
  27
  28#include "coresight-priv.h"
  29
  30static DEFINE_MUTEX(coresight_mutex);
  31
  32/**
  33 * struct coresight_node - elements of a path, from source to sink
  34 * @csdev:	Address of an element.
  35 * @link:	hook to the list.
  36 */
  37struct coresight_node {
  38	struct coresight_device *csdev;
  39	struct list_head link;
  40};
  41
  42/*
  43 * When operating Coresight drivers from the sysFS interface, only a single
  44 * path can exist from a tracer (associated to a CPU) to a sink.
  45 */
  46static DEFINE_PER_CPU(struct list_head *, tracer_path);
  47
  48/*
  49 * As of this writing only a single STM can be found in CS topologies.  Since
  50 * there is no way to know if we'll ever see more and what kind of
  51 * configuration they will enact, for the time being only define a single path
  52 * for STM.
  53 */
  54static struct list_head *stm_path;
  55
  56/*
  57 * When losing synchronisation a new barrier packet needs to be inserted at the
  58 * beginning of the data collected in a buffer.  That way the decoder knows that
  59 * it needs to look for another sync sequence.
  60 */
  61const u32 barrier_pkt[5] = {0x7fffffff, 0x7fffffff,
  62			    0x7fffffff, 0x7fffffff, 0x0};
  63
  64static int coresight_id_match(struct device *dev, void *data)
  65{
  66	int trace_id, i_trace_id;
  67	struct coresight_device *csdev, *i_csdev;
  68
  69	csdev = data;
  70	i_csdev = to_coresight_device(dev);
  71
  72	/*
  73	 * No need to care about oneself and components that are not
  74	 * sources or not enabled
  75	 */
  76	if (i_csdev == csdev || !i_csdev->enable ||
  77	    i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
  78		return 0;
  79
  80	/* Get the source ID for both compoment */
  81	trace_id = source_ops(csdev)->trace_id(csdev);
  82	i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
  83
  84	/* All you need is one */
  85	if (trace_id == i_trace_id)
  86		return 1;
  87
  88	return 0;
  89}
  90
  91static int coresight_source_is_unique(struct coresight_device *csdev)
  92{
  93	int trace_id = source_ops(csdev)->trace_id(csdev);
  94
  95	/* this shouldn't happen */
  96	if (trace_id < 0)
  97		return 0;
  98
  99	return !bus_for_each_dev(&coresight_bustype, NULL,
 100				 csdev, coresight_id_match);
 101}
 102
 103static int coresight_find_link_inport(struct coresight_device *csdev,
 104				      struct coresight_device *parent)
 105{
 106	int i;
 107	struct coresight_connection *conn;
 108
 109	for (i = 0; i < parent->nr_outport; i++) {
 110		conn = &parent->conns[i];
 111		if (conn->child_dev == csdev)
 112			return conn->child_port;
 113	}
 114
 115	dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
 116		dev_name(&parent->dev), dev_name(&csdev->dev));
 117
 118	return 0;
 119}
 120
 121static int coresight_find_link_outport(struct coresight_device *csdev,
 122				       struct coresight_device *child)
 123{
 124	int i;
 125	struct coresight_connection *conn;
 126
 127	for (i = 0; i < csdev->nr_outport; i++) {
 128		conn = &csdev->conns[i];
 129		if (conn->child_dev == child)
 130			return conn->outport;
 131	}
 132
 133	dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
 134		dev_name(&csdev->dev), dev_name(&child->dev));
 135
 136	return 0;
 137}
 138
 139static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
 140{
 141	int ret;
 142
 143	if (!csdev->enable) {
 144		if (sink_ops(csdev)->enable) {
 145			ret = sink_ops(csdev)->enable(csdev, mode);
 146			if (ret)
 147				return ret;
 148		}
 149		csdev->enable = true;
 150	}
 151
 152	atomic_inc(csdev->refcnt);
 153
 154	return 0;
 155}
 156
 157static void coresight_disable_sink(struct coresight_device *csdev)
 158{
 159	if (atomic_dec_return(csdev->refcnt) == 0) {
 160		if (sink_ops(csdev)->disable) {
 161			sink_ops(csdev)->disable(csdev);
 162			csdev->enable = false;
 163		}
 164	}
 165}
 166
 167static int coresight_enable_link(struct coresight_device *csdev,
 168				 struct coresight_device *parent,
 169				 struct coresight_device *child)
 170{
 171	int ret;
 172	int link_subtype;
 173	int refport, inport, outport;
 174
 175	if (!parent || !child)
 176		return -EINVAL;
 177
 178	inport = coresight_find_link_inport(csdev, parent);
 179	outport = coresight_find_link_outport(csdev, child);
 180	link_subtype = csdev->subtype.link_subtype;
 181
 182	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
 183		refport = inport;
 184	else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
 185		refport = outport;
 186	else
 187		refport = 0;
 188
 189	if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
 190		if (link_ops(csdev)->enable) {
 191			ret = link_ops(csdev)->enable(csdev, inport, outport);
 192			if (ret)
 193				return ret;
 194		}
 195	}
 196
 197	csdev->enable = true;
 198
 199	return 0;
 200}
 201
 202static void coresight_disable_link(struct coresight_device *csdev,
 203				   struct coresight_device *parent,
 204				   struct coresight_device *child)
 205{
 206	int i, nr_conns;
 207	int link_subtype;
 208	int refport, inport, outport;
 209
 210	if (!parent || !child)
 211		return;
 212
 213	inport = coresight_find_link_inport(csdev, parent);
 214	outport = coresight_find_link_outport(csdev, child);
 215	link_subtype = csdev->subtype.link_subtype;
 216
 217	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
 218		refport = inport;
 219		nr_conns = csdev->nr_inport;
 220	} else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
 221		refport = outport;
 222		nr_conns = csdev->nr_outport;
 223	} else {
 224		refport = 0;
 225		nr_conns = 1;
 226	}
 227
 228	if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
 229		if (link_ops(csdev)->disable)
 230			link_ops(csdev)->disable(csdev, inport, outport);
 231	}
 232
 233	for (i = 0; i < nr_conns; i++)
 234		if (atomic_read(&csdev->refcnt[i]) != 0)
 235			return;
 236
 237	csdev->enable = false;
 238}
 239
 240static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
 241{
 242	int ret;
 243
 244	if (!coresight_source_is_unique(csdev)) {
 245		dev_warn(&csdev->dev, "traceID %d not unique\n",
 246			 source_ops(csdev)->trace_id(csdev));
 247		return -EINVAL;
 248	}
 249
 250	if (!csdev->enable) {
 251		if (source_ops(csdev)->enable) {
 252			ret = source_ops(csdev)->enable(csdev, NULL, mode);
 253			if (ret)
 254				return ret;
 255		}
 256		csdev->enable = true;
 257	}
 258
 259	atomic_inc(csdev->refcnt);
 260
 261	return 0;
 262}
 263
 264/**
 265 *  coresight_disable_source - Drop the reference count by 1 and disable
 266 *  the device if there are no users left.
 267 *
 268 *  @csdev - The coresight device to disable
 269 *
 270 *  Returns true if the device has been disabled.
 271 */
 272static bool coresight_disable_source(struct coresight_device *csdev)
 273{
 274	if (atomic_dec_return(csdev->refcnt) == 0) {
 275		if (source_ops(csdev)->disable)
 276			source_ops(csdev)->disable(csdev, NULL);
 277		csdev->enable = false;
 278	}
 279	return !csdev->enable;
 280}
 281
 282void coresight_disable_path(struct list_head *path)
 283{
 284	u32 type;
 285	struct coresight_node *nd;
 286	struct coresight_device *csdev, *parent, *child;
 287
 288	list_for_each_entry(nd, path, link) {
 289		csdev = nd->csdev;
 290		type = csdev->type;
 291
 292		/*
 293		 * ETF devices are tricky... They can be a link or a sink,
 294		 * depending on how they are configured.  If an ETF has been
 295		 * "activated" it will be configured as a sink, otherwise
 296		 * go ahead with the link configuration.
 297		 */
 298		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
 299			type = (csdev == coresight_get_sink(path)) ?
 300						CORESIGHT_DEV_TYPE_SINK :
 301						CORESIGHT_DEV_TYPE_LINK;
 302
 303		switch (type) {
 304		case CORESIGHT_DEV_TYPE_SINK:
 305			coresight_disable_sink(csdev);
 306			break;
 307		case CORESIGHT_DEV_TYPE_SOURCE:
 308			/* sources are disabled from either sysFS or Perf */
 309			break;
 310		case CORESIGHT_DEV_TYPE_LINK:
 311			parent = list_prev_entry(nd, link)->csdev;
 312			child = list_next_entry(nd, link)->csdev;
 313			coresight_disable_link(csdev, parent, child);
 314			break;
 315		default:
 316			break;
 317		}
 318	}
 319}
 320
 321int coresight_enable_path(struct list_head *path, u32 mode)
 322{
 323
 324	int ret = 0;
 325	u32 type;
 326	struct coresight_node *nd;
 327	struct coresight_device *csdev, *parent, *child;
 328
 329	list_for_each_entry_reverse(nd, path, link) {
 330		csdev = nd->csdev;
 331		type = csdev->type;
 332
 333		/*
 334		 * ETF devices are tricky... They can be a link or a sink,
 335		 * depending on how they are configured.  If an ETF has been
 336		 * "activated" it will be configured as a sink, otherwise
 337		 * go ahead with the link configuration.
 338		 */
 339		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
 340			type = (csdev == coresight_get_sink(path)) ?
 341						CORESIGHT_DEV_TYPE_SINK :
 342						CORESIGHT_DEV_TYPE_LINK;
 343
 344		switch (type) {
 345		case CORESIGHT_DEV_TYPE_SINK:
 346			ret = coresight_enable_sink(csdev, mode);
 347			if (ret)
 348				goto err;
 349			break;
 350		case CORESIGHT_DEV_TYPE_SOURCE:
 351			/* sources are enabled from either sysFS or Perf */
 352			break;
 353		case CORESIGHT_DEV_TYPE_LINK:
 354			parent = list_prev_entry(nd, link)->csdev;
 355			child = list_next_entry(nd, link)->csdev;
 356			ret = coresight_enable_link(csdev, parent, child);
 357			if (ret)
 358				goto err;
 359			break;
 360		default:
 361			goto err;
 362		}
 363	}
 364
 365out:
 366	return ret;
 367err:
 368	coresight_disable_path(path);
 369	goto out;
 370}
 371
 372struct coresight_device *coresight_get_sink(struct list_head *path)
 373{
 374	struct coresight_device *csdev;
 375
 376	if (!path)
 377		return NULL;
 378
 379	csdev = list_last_entry(path, struct coresight_node, link)->csdev;
 380	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
 381	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
 382		return NULL;
 383
 384	return csdev;
 385}
 386
 387static int coresight_enabled_sink(struct device *dev, void *data)
 388{
 389	bool *reset = data;
 390	struct coresight_device *csdev = to_coresight_device(dev);
 391
 392	if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
 393	     csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
 394	     csdev->activated) {
 395		/*
 396		 * Now that we have a handle on the sink for this session,
 397		 * disable the sysFS "enable_sink" flag so that possible
 398		 * concurrent perf session that wish to use another sink don't
 399		 * trip on it.  Doing so has no ramification for the current
 400		 * session.
 401		 */
 402		if (*reset)
 403			csdev->activated = false;
 404
 405		return 1;
 406	}
 407
 408	return 0;
 409}
 410
 411/**
 412 * coresight_get_enabled_sink - returns the first enabled sink found on the bus
 413 * @deactivate:	Whether the 'enable_sink' flag should be reset
 414 *
 415 * When operated from perf the deactivate parameter should be set to 'true'.
 416 * That way the "enabled_sink" flag of the sink that was selected can be reset,
 417 * allowing for other concurrent perf sessions to choose a different sink.
 418 *
 419 * When operated from sysFS users have full control and as such the deactivate
 420 * parameter should be set to 'false', hence mandating users to explicitly
 421 * clear the flag.
 422 */
 423struct coresight_device *coresight_get_enabled_sink(bool deactivate)
 424{
 425	struct device *dev = NULL;
 426
 427	dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
 428			      coresight_enabled_sink);
 429
 430	return dev ? to_coresight_device(dev) : NULL;
 431}
 432
 433/**
 434 * _coresight_build_path - recursively build a path from a @csdev to a sink.
 435 * @csdev:	The device to start from.
 436 * @path:	The list to add devices to.
 437 *
 438 * The tree of Coresight device is traversed until an activated sink is
 439 * found.  From there the sink is added to the list along with all the
 440 * devices that led to that point - the end result is a list from source
 441 * to sink. In that list the source is the first device and the sink the
 442 * last one.
 443 */
 444static int _coresight_build_path(struct coresight_device *csdev,
 445				 struct coresight_device *sink,
 446				 struct list_head *path)
 447{
 448	int i;
 449	bool found = false;
 450	struct coresight_node *node;
 451
 452	/* An activated sink has been found.  Enqueue the element */
 453	if (csdev == sink)
 454		goto out;
 455
 456	/* Not a sink - recursively explore each port found on this element */
 457	for (i = 0; i < csdev->nr_outport; i++) {
 458		struct coresight_device *child_dev = csdev->conns[i].child_dev;
 459
 460		if (child_dev &&
 461		    _coresight_build_path(child_dev, sink, path) == 0) {
 462			found = true;
 463			break;
 464		}
 465	}
 466
 467	if (!found)
 468		return -ENODEV;
 469
 470out:
 471	/*
 472	 * A path from this element to a sink has been found.  The elements
 473	 * leading to the sink are already enqueued, all that is left to do
 474	 * is tell the PM runtime core we need this element and add a node
 475	 * for it.
 476	 */
 477	node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
 478	if (!node)
 479		return -ENOMEM;
 480
 481	node->csdev = csdev;
 482	list_add(&node->link, path);
 483	pm_runtime_get_sync(csdev->dev.parent);
 484
 485	return 0;
 486}
 487
 488struct list_head *coresight_build_path(struct coresight_device *source,
 489				       struct coresight_device *sink)
 490{
 491	struct list_head *path;
 492	int rc;
 493
 494	if (!sink)
 495		return ERR_PTR(-EINVAL);
 496
 497	path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
 498	if (!path)
 499		return ERR_PTR(-ENOMEM);
 500
 501	INIT_LIST_HEAD(path);
 502
 503	rc = _coresight_build_path(source, sink, path);
 504	if (rc) {
 505		kfree(path);
 506		return ERR_PTR(rc);
 507	}
 508
 509	return path;
 510}
 511
 512/**
 513 * coresight_release_path - release a previously built path.
 514 * @path:	the path to release.
 515 *
 516 * Go through all the elements of a path and 1) removed it from the list and
 517 * 2) free the memory allocated for each node.
 518 */
 519void coresight_release_path(struct list_head *path)
 520{
 521	struct coresight_device *csdev;
 522	struct coresight_node *nd, *next;
 523
 524	list_for_each_entry_safe(nd, next, path, link) {
 525		csdev = nd->csdev;
 526
 527		pm_runtime_put_sync(csdev->dev.parent);
 528		list_del(&nd->link);
 529		kfree(nd);
 530	}
 531
 532	kfree(path);
 533	path = NULL;
 534}
 535
 536/** coresight_validate_source - make sure a source has the right credentials
 537 *  @csdev:	the device structure for a source.
 538 *  @function:	the function this was called from.
 539 *
 540 * Assumes the coresight_mutex is held.
 541 */
 542static int coresight_validate_source(struct coresight_device *csdev,
 543				     const char *function)
 544{
 545	u32 type, subtype;
 546
 547	type = csdev->type;
 548	subtype = csdev->subtype.source_subtype;
 549
 550	if (type != CORESIGHT_DEV_TYPE_SOURCE) {
 551		dev_err(&csdev->dev, "wrong device type in %s\n", function);
 552		return -EINVAL;
 553	}
 554
 555	if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
 556	    subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
 557		dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
 558		return -EINVAL;
 559	}
 560
 561	return 0;
 562}
 563
 564int coresight_enable(struct coresight_device *csdev)
 565{
 566	int cpu, ret = 0;
 567	struct coresight_device *sink;
 568	struct list_head *path;
 569	enum coresight_dev_subtype_source subtype;
 570
 571	subtype = csdev->subtype.source_subtype;
 572
 573	mutex_lock(&coresight_mutex);
 574
 575	ret = coresight_validate_source(csdev, __func__);
 576	if (ret)
 577		goto out;
 578
 579	if (csdev->enable) {
 580		/*
 581		 * There could be multiple applications driving the software
 582		 * source. So keep the refcount for each such user when the
 583		 * source is already enabled.
 584		 */
 585		if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
 586			atomic_inc(csdev->refcnt);
 587		goto out;
 588	}
 589
 590	/*
 591	 * Search for a valid sink for this session but don't reset the
 592	 * "enable_sink" flag in sysFS.  Users get to do that explicitly.
 593	 */
 594	sink = coresight_get_enabled_sink(false);
 595	if (!sink) {
 596		ret = -EINVAL;
 597		goto out;
 598	}
 599
 600	path = coresight_build_path(csdev, sink);
 601	if (IS_ERR(path)) {
 602		pr_err("building path(s) failed\n");
 603		ret = PTR_ERR(path);
 604		goto out;
 605	}
 606
 607	ret = coresight_enable_path(path, CS_MODE_SYSFS);
 608	if (ret)
 609		goto err_path;
 610
 611	ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
 612	if (ret)
 613		goto err_source;
 614
 615	switch (subtype) {
 616	case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
 617		/*
 618		 * When working from sysFS it is important to keep track
 619		 * of the paths that were created so that they can be
 620		 * undone in 'coresight_disable()'.  Since there can only
 621		 * be a single session per tracer (when working from sysFS)
 622		 * a per-cpu variable will do just fine.
 623		 */
 624		cpu = source_ops(csdev)->cpu_id(csdev);
 625		per_cpu(tracer_path, cpu) = path;
 626		break;
 627	case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
 628		stm_path = path;
 629		break;
 630	default:
 631		/* We can't be here */
 632		break;
 633	}
 634
 635out:
 636	mutex_unlock(&coresight_mutex);
 637	return ret;
 638
 639err_source:
 640	coresight_disable_path(path);
 641
 642err_path:
 643	coresight_release_path(path);
 644	goto out;
 645}
 646EXPORT_SYMBOL_GPL(coresight_enable);
 647
 648void coresight_disable(struct coresight_device *csdev)
 649{
 650	int cpu, ret;
 651	struct list_head *path = NULL;
 652
 653	mutex_lock(&coresight_mutex);
 654
 655	ret = coresight_validate_source(csdev, __func__);
 656	if (ret)
 657		goto out;
 658
 659	if (!csdev->enable || !coresight_disable_source(csdev))
 660		goto out;
 661
 662	switch (csdev->subtype.source_subtype) {
 663	case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
 664		cpu = source_ops(csdev)->cpu_id(csdev);
 665		path = per_cpu(tracer_path, cpu);
 666		per_cpu(tracer_path, cpu) = NULL;
 667		break;
 668	case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
 669		path = stm_path;
 670		stm_path = NULL;
 671		break;
 672	default:
 673		/* We can't be here */
 674		break;
 675	}
 676
 677	coresight_disable_path(path);
 678	coresight_release_path(path);
 679
 680out:
 681	mutex_unlock(&coresight_mutex);
 682}
 683EXPORT_SYMBOL_GPL(coresight_disable);
 684
 685static ssize_t enable_sink_show(struct device *dev,
 686				struct device_attribute *attr, char *buf)
 687{
 688	struct coresight_device *csdev = to_coresight_device(dev);
 689
 690	return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
 691}
 692
 693static ssize_t enable_sink_store(struct device *dev,
 694				 struct device_attribute *attr,
 695				 const char *buf, size_t size)
 696{
 697	int ret;
 698	unsigned long val;
 699	struct coresight_device *csdev = to_coresight_device(dev);
 700
 701	ret = kstrtoul(buf, 10, &val);
 702	if (ret)
 703		return ret;
 704
 705	if (val)
 706		csdev->activated = true;
 707	else
 708		csdev->activated = false;
 709
 710	return size;
 711
 712}
 713static DEVICE_ATTR_RW(enable_sink);
 714
 715static ssize_t enable_source_show(struct device *dev,
 716				  struct device_attribute *attr, char *buf)
 717{
 718	struct coresight_device *csdev = to_coresight_device(dev);
 719
 720	return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
 721}
 722
 723static ssize_t enable_source_store(struct device *dev,
 724				   struct device_attribute *attr,
 725				   const char *buf, size_t size)
 726{
 727	int ret = 0;
 728	unsigned long val;
 729	struct coresight_device *csdev = to_coresight_device(dev);
 730
 731	ret = kstrtoul(buf, 10, &val);
 732	if (ret)
 733		return ret;
 734
 735	if (val) {
 736		ret = coresight_enable(csdev);
 737		if (ret)
 738			return ret;
 739	} else {
 740		coresight_disable(csdev);
 741	}
 742
 743	return size;
 744}
 745static DEVICE_ATTR_RW(enable_source);
 746
 747static struct attribute *coresight_sink_attrs[] = {
 748	&dev_attr_enable_sink.attr,
 749	NULL,
 750};
 751ATTRIBUTE_GROUPS(coresight_sink);
 752
 753static struct attribute *coresight_source_attrs[] = {
 754	&dev_attr_enable_source.attr,
 755	NULL,
 756};
 757ATTRIBUTE_GROUPS(coresight_source);
 758
 759static struct device_type coresight_dev_type[] = {
 760	{
 761		.name = "none",
 762	},
 763	{
 764		.name = "sink",
 765		.groups = coresight_sink_groups,
 766	},
 767	{
 768		.name = "link",
 769	},
 770	{
 771		.name = "linksink",
 772		.groups = coresight_sink_groups,
 773	},
 774	{
 775		.name = "source",
 776		.groups = coresight_source_groups,
 777	},
 778};
 779
 780static void coresight_device_release(struct device *dev)
 781{
 782	struct coresight_device *csdev = to_coresight_device(dev);
 783
 784	kfree(csdev->conns);
 785	kfree(csdev->refcnt);
 786	kfree(csdev);
 787}
 788
 789static int coresight_orphan_match(struct device *dev, void *data)
 790{
 791	int i;
 792	bool still_orphan = false;
 793	struct coresight_device *csdev, *i_csdev;
 794	struct coresight_connection *conn;
 795
 796	csdev = data;
 797	i_csdev = to_coresight_device(dev);
 798
 799	/* No need to check oneself */
 800	if (csdev == i_csdev)
 801		return 0;
 802
 803	/* Move on to another component if no connection is orphan */
 804	if (!i_csdev->orphan)
 805		return 0;
 806	/*
 807	 * Circle throuch all the connection of that component.  If we find
 808	 * an orphan connection whose name matches @csdev, link it.
 809	 */
 810	for (i = 0; i < i_csdev->nr_outport; i++) {
 811		conn = &i_csdev->conns[i];
 812
 813		/* We have found at least one orphan connection */
 814		if (conn->child_dev == NULL) {
 815			/* Does it match this newly added device? */
 816			if (conn->child_name &&
 817			    !strcmp(dev_name(&csdev->dev), conn->child_name)) {
 818				conn->child_dev = csdev;
 819			} else {
 820				/* This component still has an orphan */
 821				still_orphan = true;
 822			}
 823		}
 824	}
 825
 826	i_csdev->orphan = still_orphan;
 827
 828	/*
 829	 * Returning '0' ensures that all known component on the
 830	 * bus will be checked.
 831	 */
 832	return 0;
 833}
 834
 835static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
 836{
 837	/*
 838	 * No need to check for a return value as orphan connection(s)
 839	 * are hooked-up with each newly added component.
 840	 */
 841	bus_for_each_dev(&coresight_bustype, NULL,
 842			 csdev, coresight_orphan_match);
 843}
 844
 845
 846static void coresight_fixup_device_conns(struct coresight_device *csdev)
 847{
 848	int i;
 849
 850	for (i = 0; i < csdev->nr_outport; i++) {
 851		struct coresight_connection *conn = &csdev->conns[i];
 852		struct device *dev = NULL;
 853
 854		if (conn->child_name)
 855			dev = bus_find_device_by_name(&coresight_bustype, NULL,
 856						      conn->child_name);
 857		if (dev) {
 858			conn->child_dev = to_coresight_device(dev);
 859			/* and put reference from 'bus_find_device()' */
 860			put_device(dev);
 861		} else {
 862			csdev->orphan = true;
 863			conn->child_dev = NULL;
 864		}
 865	}
 866}
 867
 868static int coresight_remove_match(struct device *dev, void *data)
 869{
 870	int i;
 871	struct coresight_device *csdev, *iterator;
 872	struct coresight_connection *conn;
 873
 874	csdev = data;
 875	iterator = to_coresight_device(dev);
 876
 877	/* No need to check oneself */
 878	if (csdev == iterator)
 879		return 0;
 880
 881	/*
 882	 * Circle throuch all the connection of that component.  If we find
 883	 * a connection whose name matches @csdev, remove it.
 884	 */
 885	for (i = 0; i < iterator->nr_outport; i++) {
 886		conn = &iterator->conns[i];
 887
 888		if (conn->child_dev == NULL)
 889			continue;
 890
 891		if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
 892			iterator->orphan = true;
 893			conn->child_dev = NULL;
 894			/* No need to continue */
 895			break;
 896		}
 897	}
 898
 899	/*
 900	 * Returning '0' ensures that all known component on the
 901	 * bus will be checked.
 902	 */
 903	return 0;
 904}
 905
 906static void coresight_remove_conns(struct coresight_device *csdev)
 907{
 908	bus_for_each_dev(&coresight_bustype, NULL,
 909			 csdev, coresight_remove_match);
 910}
 911
 912/**
 913 * coresight_timeout - loop until a bit has changed to a specific state.
 914 * @addr: base address of the area of interest.
 915 * @offset: address of a register, starting from @addr.
 916 * @position: the position of the bit of interest.
 917 * @value: the value the bit should have.
 918 *
 919 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
 920 * TIMEOUT_US has elapsed, which ever happens first.
 921 */
 922
 923int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
 924{
 925	int i;
 926	u32 val;
 927
 928	for (i = TIMEOUT_US; i > 0; i--) {
 929		val = __raw_readl(addr + offset);
 930		/* waiting on the bit to go from 0 to 1 */
 931		if (value) {
 932			if (val & BIT(position))
 933				return 0;
 934		/* waiting on the bit to go from 1 to 0 */
 935		} else {
 936			if (!(val & BIT(position)))
 937				return 0;
 938		}
 939
 940		/*
 941		 * Delay is arbitrary - the specification doesn't say how long
 942		 * we are expected to wait.  Extra check required to make sure
 943		 * we don't wait needlessly on the last iteration.
 944		 */
 945		if (i - 1)
 946			udelay(1);
 947	}
 948
 949	return -EAGAIN;
 950}
 951
 952struct bus_type coresight_bustype = {
 953	.name	= "coresight",
 954};
 955
 956static int __init coresight_init(void)
 957{
 958	return bus_register(&coresight_bustype);
 959}
 960postcore_initcall(coresight_init);
 961
 962struct coresight_device *coresight_register(struct coresight_desc *desc)
 963{
 964	int i;
 965	int ret;
 966	int link_subtype;
 967	int nr_refcnts = 1;
 968	atomic_t *refcnts = NULL;
 969	struct coresight_device *csdev;
 970	struct coresight_connection *conns = NULL;
 971
 972	csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
 973	if (!csdev) {
 974		ret = -ENOMEM;
 975		goto err_kzalloc_csdev;
 976	}
 977
 978	if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
 979	    desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
 980		link_subtype = desc->subtype.link_subtype;
 981
 982		if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
 983			nr_refcnts = desc->pdata->nr_inport;
 984		else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
 985			nr_refcnts = desc->pdata->nr_outport;
 986	}
 987
 988	refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
 989	if (!refcnts) {
 990		ret = -ENOMEM;
 991		goto err_kzalloc_refcnts;
 992	}
 993
 994	csdev->refcnt = refcnts;
 995
 996	csdev->nr_inport = desc->pdata->nr_inport;
 997	csdev->nr_outport = desc->pdata->nr_outport;
 998
 999	/* Initialise connections if there is at least one outport */
1000	if (csdev->nr_outport) {
1001		conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
1002		if (!conns) {
1003			ret = -ENOMEM;
1004			goto err_kzalloc_conns;
1005		}
1006
1007		for (i = 0; i < csdev->nr_outport; i++) {
1008			conns[i].outport = desc->pdata->outports[i];
1009			conns[i].child_name = desc->pdata->child_names[i];
1010			conns[i].child_port = desc->pdata->child_ports[i];
1011		}
1012	}
1013
1014	csdev->conns = conns;
1015
1016	csdev->type = desc->type;
1017	csdev->subtype = desc->subtype;
1018	csdev->ops = desc->ops;
1019	csdev->orphan = false;
1020
1021	csdev->dev.type = &coresight_dev_type[desc->type];
1022	csdev->dev.groups = desc->groups;
1023	csdev->dev.parent = desc->dev;
1024	csdev->dev.release = coresight_device_release;
1025	csdev->dev.bus = &coresight_bustype;
1026	dev_set_name(&csdev->dev, "%s", desc->pdata->name);
1027
1028	ret = device_register(&csdev->dev);
1029	if (ret)
1030		goto err_device_register;
1031
1032	mutex_lock(&coresight_mutex);
1033
1034	coresight_fixup_device_conns(csdev);
1035	coresight_fixup_orphan_conns(csdev);
1036
1037	mutex_unlock(&coresight_mutex);
1038
1039	return csdev;
1040
1041err_device_register:
1042	kfree(conns);
1043err_kzalloc_conns:
1044	kfree(refcnts);
1045err_kzalloc_refcnts:
1046	kfree(csdev);
1047err_kzalloc_csdev:
1048	return ERR_PTR(ret);
1049}
1050EXPORT_SYMBOL_GPL(coresight_register);
1051
1052void coresight_unregister(struct coresight_device *csdev)
1053{
1054	/* Remove references of that device in the topology */
1055	coresight_remove_conns(csdev);
1056	device_unregister(&csdev->dev);
1057}
1058EXPORT_SYMBOL_GPL(coresight_unregister);