Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
v4.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 *, sysfs_path);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 47
 48static int coresight_id_match(struct device *dev, void *data)
 49{
 50	int trace_id, i_trace_id;
 51	struct coresight_device *csdev, *i_csdev;
 52
 53	csdev = data;
 54	i_csdev = to_coresight_device(dev);
 55
 56	/*
 57	 * No need to care about oneself and components that are not
 58	 * sources or not enabled
 59	 */
 60	if (i_csdev == csdev || !i_csdev->enable ||
 61	    i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
 62		return 0;
 63
 64	/* Get the source ID for both compoment */
 65	trace_id = source_ops(csdev)->trace_id(csdev);
 66	i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
 67
 68	/* All you need is one */
 69	if (trace_id == i_trace_id)
 70		return 1;
 71
 72	return 0;
 73}
 74
 75static int coresight_source_is_unique(struct coresight_device *csdev)
 76{
 77	int trace_id = source_ops(csdev)->trace_id(csdev);
 78
 79	/* this shouldn't happen */
 80	if (trace_id < 0)
 81		return 0;
 82
 83	return !bus_for_each_dev(&coresight_bustype, NULL,
 84				 csdev, coresight_id_match);
 85}
 86
 87static int coresight_find_link_inport(struct coresight_device *csdev,
 88				      struct coresight_device *parent)
 89{
 90	int i;
 91	struct coresight_connection *conn;
 92
 93	for (i = 0; i < parent->nr_outport; i++) {
 94		conn = &parent->conns[i];
 95		if (conn->child_dev == csdev)
 96			return conn->child_port;
 97	}
 98
 99	dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
100		dev_name(&parent->dev), dev_name(&csdev->dev));
101
102	return 0;
103}
104
105static int coresight_find_link_outport(struct coresight_device *csdev,
106				       struct coresight_device *child)
107{
108	int i;
109	struct coresight_connection *conn;
110
111	for (i = 0; i < csdev->nr_outport; i++) {
112		conn = &csdev->conns[i];
113		if (conn->child_dev == child)
114			return conn->outport;
115	}
116
117	dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
118		dev_name(&csdev->dev), dev_name(&child->dev));
119
120	return 0;
121}
122
123static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
124{
125	int ret;
126
127	if (!csdev->enable) {
128		if (sink_ops(csdev)->enable) {
129			ret = sink_ops(csdev)->enable(csdev, mode);
130			if (ret)
131				return ret;
132		}
133		csdev->enable = true;
134	}
135
136	atomic_inc(csdev->refcnt);
137
138	return 0;
139}
140
141static void coresight_disable_sink(struct coresight_device *csdev)
142{
143	if (atomic_dec_return(csdev->refcnt) == 0) {
144		if (sink_ops(csdev)->disable) {
145			sink_ops(csdev)->disable(csdev);
146			csdev->enable = false;
147		}
148	}
149}
150
151static int coresight_enable_link(struct coresight_device *csdev,
152				 struct coresight_device *parent,
153				 struct coresight_device *child)
154{
155	int ret;
156	int link_subtype;
157	int refport, inport, outport;
158
159	if (!parent || !child)
160		return -EINVAL;
161
162	inport = coresight_find_link_inport(csdev, parent);
163	outport = coresight_find_link_outport(csdev, child);
164	link_subtype = csdev->subtype.link_subtype;
165
166	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
167		refport = inport;
168	else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
169		refport = outport;
170	else
171		refport = 0;
172
173	if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
174		if (link_ops(csdev)->enable) {
175			ret = link_ops(csdev)->enable(csdev, inport, outport);
176			if (ret)
177				return ret;
178		}
179	}
180
181	csdev->enable = true;
182
183	return 0;
184}
185
186static void coresight_disable_link(struct coresight_device *csdev,
187				   struct coresight_device *parent,
188				   struct coresight_device *child)
189{
190	int i, nr_conns;
191	int link_subtype;
192	int refport, inport, outport;
193
194	if (!parent || !child)
195		return;
196
197	inport = coresight_find_link_inport(csdev, parent);
198	outport = coresight_find_link_outport(csdev, child);
199	link_subtype = csdev->subtype.link_subtype;
200
201	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
202		refport = inport;
203		nr_conns = csdev->nr_inport;
204	} else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
205		refport = outport;
206		nr_conns = csdev->nr_outport;
207	} else {
208		refport = 0;
209		nr_conns = 1;
210	}
211
212	if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
213		if (link_ops(csdev)->disable)
214			link_ops(csdev)->disable(csdev, inport, outport);
215	}
216
217	for (i = 0; i < nr_conns; i++)
218		if (atomic_read(&csdev->refcnt[i]) != 0)
219			return;
220
221	csdev->enable = false;
222}
223
224static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
225{
226	int ret;
227
228	if (!coresight_source_is_unique(csdev)) {
229		dev_warn(&csdev->dev, "traceID %d not unique\n",
230			 source_ops(csdev)->trace_id(csdev));
231		return -EINVAL;
232	}
233
234	if (!csdev->enable) {
235		if (source_ops(csdev)->enable) {
236			ret = source_ops(csdev)->enable(csdev, NULL, mode);
237			if (ret)
238				return ret;
239		}
240		csdev->enable = true;
241	}
242
243	atomic_inc(csdev->refcnt);
244
245	return 0;
246}
247
248static void coresight_disable_source(struct coresight_device *csdev)
 
 
 
 
 
 
 
 
249{
250	if (atomic_dec_return(csdev->refcnt) == 0) {
251		if (source_ops(csdev)->disable) {
252			source_ops(csdev)->disable(csdev);
253			csdev->enable = false;
254		}
255	}
 
256}
257
258void coresight_disable_path(struct list_head *path)
259{
 
260	struct coresight_node *nd;
261	struct coresight_device *csdev, *parent, *child;
262
263	list_for_each_entry(nd, path, link) {
264		csdev = nd->csdev;
 
 
 
 
 
 
 
 
 
 
 
 
265
266		switch (csdev->type) {
267		case CORESIGHT_DEV_TYPE_SINK:
268		case CORESIGHT_DEV_TYPE_LINKSINK:
269			coresight_disable_sink(csdev);
270			break;
271		case CORESIGHT_DEV_TYPE_SOURCE:
272			/* sources are disabled from either sysFS or Perf */
273			break;
274		case CORESIGHT_DEV_TYPE_LINK:
275			parent = list_prev_entry(nd, link)->csdev;
276			child = list_next_entry(nd, link)->csdev;
277			coresight_disable_link(csdev, parent, child);
278			break;
279		default:
280			break;
281		}
282	}
283}
284
285int coresight_enable_path(struct list_head *path, u32 mode)
286{
287
288	int ret = 0;
 
289	struct coresight_node *nd;
290	struct coresight_device *csdev, *parent, *child;
291
292	list_for_each_entry_reverse(nd, path, link) {
293		csdev = nd->csdev;
 
294
295		switch (csdev->type) {
 
 
 
 
 
 
 
 
 
 
 
296		case CORESIGHT_DEV_TYPE_SINK:
297		case CORESIGHT_DEV_TYPE_LINKSINK:
298			ret = coresight_enable_sink(csdev, mode);
299			if (ret)
300				goto err;
301			break;
302		case CORESIGHT_DEV_TYPE_SOURCE:
303			/* sources are enabled from either sysFS or Perf */
304			break;
305		case CORESIGHT_DEV_TYPE_LINK:
306			parent = list_prev_entry(nd, link)->csdev;
307			child = list_next_entry(nd, link)->csdev;
308			ret = coresight_enable_link(csdev, parent, child);
309			if (ret)
310				goto err;
311			break;
312		default:
313			goto err;
314		}
315	}
316
317out:
318	return ret;
319err:
320	coresight_disable_path(path);
321	goto out;
322}
323
324struct coresight_device *coresight_get_sink(struct list_head *path)
325{
326	struct coresight_device *csdev;
327
328	if (!path)
329		return NULL;
330
331	csdev = list_last_entry(path, struct coresight_node, link)->csdev;
332	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
333	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
334		return NULL;
335
336	return csdev;
337}
338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
339/**
340 * _coresight_build_path - recursively build a path from a @csdev to a sink.
341 * @csdev:	The device to start from.
342 * @path:	The list to add devices to.
343 *
344 * The tree of Coresight device is traversed until an activated sink is
345 * found.  From there the sink is added to the list along with all the
346 * devices that led to that point - the end result is a list from source
347 * to sink. In that list the source is the first device and the sink the
348 * last one.
349 */
350static int _coresight_build_path(struct coresight_device *csdev,
 
351				 struct list_head *path)
352{
353	int i;
354	bool found = false;
355	struct coresight_node *node;
356	struct coresight_connection *conn;
357
358	/* An activated sink has been found.  Enqueue the element */
359	if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
360	     csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) && csdev->activated)
361		goto out;
362
363	/* Not a sink - recursively explore each port found on this element */
364	for (i = 0; i < csdev->nr_outport; i++) {
365		conn = &csdev->conns[i];
366		if (_coresight_build_path(conn->child_dev, path) == 0) {
 
 
367			found = true;
368			break;
369		}
370	}
371
372	if (!found)
373		return -ENODEV;
374
375out:
376	/*
377	 * A path from this element to a sink has been found.  The elements
378	 * leading to the sink are already enqueued, all that is left to do
379	 * is tell the PM runtime core we need this element and add a node
380	 * for it.
381	 */
382	node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
383	if (!node)
384		return -ENOMEM;
385
386	node->csdev = csdev;
387	list_add(&node->link, path);
388	pm_runtime_get_sync(csdev->dev.parent);
389
390	return 0;
391}
392
393struct list_head *coresight_build_path(struct coresight_device *csdev)
 
394{
395	struct list_head *path;
 
 
 
 
396
397	path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
398	if (!path)
399		return NULL;
400
401	INIT_LIST_HEAD(path);
402
403	if (_coresight_build_path(csdev, path)) {
 
404		kfree(path);
405		path = NULL;
406	}
407
408	return path;
409}
410
411/**
412 * coresight_release_path - release a previously built path.
413 * @path:	the path to release.
414 *
415 * Go through all the elements of a path and 1) removed it from the list and
416 * 2) free the memory allocated for each node.
417 */
418void coresight_release_path(struct list_head *path)
419{
420	struct coresight_device *csdev;
421	struct coresight_node *nd, *next;
422
423	list_for_each_entry_safe(nd, next, path, link) {
424		csdev = nd->csdev;
425
426		pm_runtime_put_sync(csdev->dev.parent);
427		list_del(&nd->link);
428		kfree(nd);
429	}
430
431	kfree(path);
432	path = NULL;
433}
434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
435int coresight_enable(struct coresight_device *csdev)
436{
437	int ret = 0;
438	int cpu;
439	struct list_head *path;
 
 
 
440
441	mutex_lock(&coresight_mutex);
442	if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
443		ret = -EINVAL;
444		dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
 
 
 
 
 
 
 
 
 
 
445		goto out;
446	}
447	if (csdev->enable)
 
 
 
 
 
 
 
448		goto out;
 
449
450	path = coresight_build_path(csdev);
451	if (!path) {
452		pr_err("building path(s) failed\n");
 
453		goto out;
454	}
455
456	ret = coresight_enable_path(path, CS_MODE_SYSFS);
457	if (ret)
458		goto err_path;
459
460	ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
461	if (ret)
462		goto err_source;
463
464	/*
465	 * When working from sysFS it is important to keep track
466	 * of the paths that were created so that they can be
467	 * undone in 'coresight_disable()'.  Since there can only
468	 * be a single session per tracer (when working from sysFS)
469	 * a per-cpu variable will do just fine.
470	 */
471	cpu = source_ops(csdev)->cpu_id(csdev);
472	per_cpu(sysfs_path, cpu) = path;
 
 
 
 
 
 
 
 
 
 
473
474out:
475	mutex_unlock(&coresight_mutex);
476	return ret;
477
478err_source:
479	coresight_disable_path(path);
480
481err_path:
482	coresight_release_path(path);
483	goto out;
484}
485EXPORT_SYMBOL_GPL(coresight_enable);
486
487void coresight_disable(struct coresight_device *csdev)
488{
489	int cpu;
490	struct list_head *path;
491
492	mutex_lock(&coresight_mutex);
493	if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
494		dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
 
495		goto out;
496	}
497	if (!csdev->enable)
498		goto out;
499
500	cpu = source_ops(csdev)->cpu_id(csdev);
501	path = per_cpu(sysfs_path, cpu);
502	coresight_disable_source(csdev);
 
 
 
 
 
 
 
 
 
 
 
 
503	coresight_disable_path(path);
504	coresight_release_path(path);
505	per_cpu(sysfs_path, cpu) = NULL;
506
507out:
508	mutex_unlock(&coresight_mutex);
509}
510EXPORT_SYMBOL_GPL(coresight_disable);
511
512static ssize_t enable_sink_show(struct device *dev,
513				struct device_attribute *attr, char *buf)
514{
515	struct coresight_device *csdev = to_coresight_device(dev);
516
517	return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->activated);
518}
519
520static ssize_t enable_sink_store(struct device *dev,
521				 struct device_attribute *attr,
522				 const char *buf, size_t size)
523{
524	int ret;
525	unsigned long val;
526	struct coresight_device *csdev = to_coresight_device(dev);
527
528	ret = kstrtoul(buf, 10, &val);
529	if (ret)
530		return ret;
531
532	if (val)
533		csdev->activated = true;
534	else
535		csdev->activated = false;
536
537	return size;
538
539}
540static DEVICE_ATTR_RW(enable_sink);
541
542static ssize_t enable_source_show(struct device *dev,
543				  struct device_attribute *attr, char *buf)
544{
545	struct coresight_device *csdev = to_coresight_device(dev);
546
547	return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->enable);
548}
549
550static ssize_t enable_source_store(struct device *dev,
551				   struct device_attribute *attr,
552				   const char *buf, size_t size)
553{
554	int ret = 0;
555	unsigned long val;
556	struct coresight_device *csdev = to_coresight_device(dev);
557
558	ret = kstrtoul(buf, 10, &val);
559	if (ret)
560		return ret;
561
562	if (val) {
563		ret = coresight_enable(csdev);
564		if (ret)
565			return ret;
566	} else {
567		coresight_disable(csdev);
568	}
569
570	return size;
571}
572static DEVICE_ATTR_RW(enable_source);
573
574static struct attribute *coresight_sink_attrs[] = {
575	&dev_attr_enable_sink.attr,
576	NULL,
577};
578ATTRIBUTE_GROUPS(coresight_sink);
579
580static struct attribute *coresight_source_attrs[] = {
581	&dev_attr_enable_source.attr,
582	NULL,
583};
584ATTRIBUTE_GROUPS(coresight_source);
585
586static struct device_type coresight_dev_type[] = {
587	{
588		.name = "none",
589	},
590	{
591		.name = "sink",
592		.groups = coresight_sink_groups,
593	},
594	{
595		.name = "link",
596	},
597	{
598		.name = "linksink",
599		.groups = coresight_sink_groups,
600	},
601	{
602		.name = "source",
603		.groups = coresight_source_groups,
604	},
605};
606
607static void coresight_device_release(struct device *dev)
608{
609	struct coresight_device *csdev = to_coresight_device(dev);
610
611	kfree(csdev->conns);
612	kfree(csdev->refcnt);
613	kfree(csdev);
614}
615
616static int coresight_orphan_match(struct device *dev, void *data)
617{
618	int i;
619	bool still_orphan = false;
620	struct coresight_device *csdev, *i_csdev;
621	struct coresight_connection *conn;
622
623	csdev = data;
624	i_csdev = to_coresight_device(dev);
625
626	/* No need to check oneself */
627	if (csdev == i_csdev)
628		return 0;
629
630	/* Move on to another component if no connection is orphan */
631	if (!i_csdev->orphan)
632		return 0;
633	/*
634	 * Circle throuch all the connection of that component.  If we find
635	 * an orphan connection whose name matches @csdev, link it.
636	 */
637	for (i = 0; i < i_csdev->nr_outport; i++) {
638		conn = &i_csdev->conns[i];
639
640		/* We have found at least one orphan connection */
641		if (conn->child_dev == NULL) {
642			/* Does it match this newly added device? */
643			if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
 
644				conn->child_dev = csdev;
645			} else {
646				/* This component still has an orphan */
647				still_orphan = true;
648			}
649		}
650	}
651
652	i_csdev->orphan = still_orphan;
653
654	/*
655	 * Returning '0' ensures that all known component on the
656	 * bus will be checked.
657	 */
658	return 0;
659}
660
661static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
662{
663	/*
664	 * No need to check for a return value as orphan connection(s)
665	 * are hooked-up with each newly added component.
666	 */
667	bus_for_each_dev(&coresight_bustype, NULL,
668			 csdev, coresight_orphan_match);
669}
670
671
672static int coresight_name_match(struct device *dev, void *data)
673{
674	char *to_match;
675	struct coresight_device *i_csdev;
676
677	to_match = data;
678	i_csdev = to_coresight_device(dev);
679
680	if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
681		return 1;
682
683	return 0;
684}
685
686static void coresight_fixup_device_conns(struct coresight_device *csdev)
687{
688	int i;
689	struct device *dev = NULL;
690	struct coresight_connection *conn;
691
692	for (i = 0; i < csdev->nr_outport; i++) {
693		conn = &csdev->conns[i];
694		dev = bus_find_device(&coresight_bustype, NULL,
695				      (void *)conn->child_name,
696				      coresight_name_match);
697
 
 
 
698		if (dev) {
699			conn->child_dev = to_coresight_device(dev);
700			/* and put reference from 'bus_find_device()' */
701			put_device(dev);
702		} else {
703			csdev->orphan = true;
704			conn->child_dev = NULL;
705		}
706	}
707}
708
709static int coresight_remove_match(struct device *dev, void *data)
710{
711	int i;
712	struct coresight_device *csdev, *iterator;
713	struct coresight_connection *conn;
714
715	csdev = data;
716	iterator = to_coresight_device(dev);
717
718	/* No need to check oneself */
719	if (csdev == iterator)
720		return 0;
721
722	/*
723	 * Circle throuch all the connection of that component.  If we find
724	 * a connection whose name matches @csdev, remove it.
725	 */
726	for (i = 0; i < iterator->nr_outport; i++) {
727		conn = &iterator->conns[i];
728
729		if (conn->child_dev == NULL)
730			continue;
731
732		if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
733			iterator->orphan = true;
734			conn->child_dev = NULL;
735			/* No need to continue */
736			break;
737		}
738	}
739
740	/*
741	 * Returning '0' ensures that all known component on the
742	 * bus will be checked.
743	 */
744	return 0;
745}
746
747static void coresight_remove_conns(struct coresight_device *csdev)
748{
749	bus_for_each_dev(&coresight_bustype, NULL,
750			 csdev, coresight_remove_match);
751}
752
753/**
754 * coresight_timeout - loop until a bit has changed to a specific state.
755 * @addr: base address of the area of interest.
756 * @offset: address of a register, starting from @addr.
757 * @position: the position of the bit of interest.
758 * @value: the value the bit should have.
759 *
760 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
761 * TIMEOUT_US has elapsed, which ever happens first.
762 */
763
764int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
765{
766	int i;
767	u32 val;
768
769	for (i = TIMEOUT_US; i > 0; i--) {
770		val = __raw_readl(addr + offset);
771		/* waiting on the bit to go from 0 to 1 */
772		if (value) {
773			if (val & BIT(position))
774				return 0;
775		/* waiting on the bit to go from 1 to 0 */
776		} else {
777			if (!(val & BIT(position)))
778				return 0;
779		}
780
781		/*
782		 * Delay is arbitrary - the specification doesn't say how long
783		 * we are expected to wait.  Extra check required to make sure
784		 * we don't wait needlessly on the last iteration.
785		 */
786		if (i - 1)
787			udelay(1);
788	}
789
790	return -EAGAIN;
791}
792
793struct bus_type coresight_bustype = {
794	.name	= "coresight",
795};
796
797static int __init coresight_init(void)
798{
799	return bus_register(&coresight_bustype);
800}
801postcore_initcall(coresight_init);
802
803struct coresight_device *coresight_register(struct coresight_desc *desc)
804{
805	int i;
806	int ret;
807	int link_subtype;
808	int nr_refcnts = 1;
809	atomic_t *refcnts = NULL;
810	struct coresight_device *csdev;
811	struct coresight_connection *conns;
812
813	csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
814	if (!csdev) {
815		ret = -ENOMEM;
816		goto err_kzalloc_csdev;
817	}
818
819	if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
820	    desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
821		link_subtype = desc->subtype.link_subtype;
822
823		if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
824			nr_refcnts = desc->pdata->nr_inport;
825		else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
826			nr_refcnts = desc->pdata->nr_outport;
827	}
828
829	refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
830	if (!refcnts) {
831		ret = -ENOMEM;
832		goto err_kzalloc_refcnts;
833	}
834
835	csdev->refcnt = refcnts;
836
837	csdev->nr_inport = desc->pdata->nr_inport;
838	csdev->nr_outport = desc->pdata->nr_outport;
839	conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
840	if (!conns) {
841		ret = -ENOMEM;
842		goto err_kzalloc_conns;
843	}
844
845	for (i = 0; i < csdev->nr_outport; i++) {
846		conns[i].outport = desc->pdata->outports[i];
847		conns[i].child_name = desc->pdata->child_names[i];
848		conns[i].child_port = desc->pdata->child_ports[i];
 
 
 
 
 
 
 
 
 
849	}
850
851	csdev->conns = conns;
852
853	csdev->type = desc->type;
854	csdev->subtype = desc->subtype;
855	csdev->ops = desc->ops;
856	csdev->orphan = false;
857
858	csdev->dev.type = &coresight_dev_type[desc->type];
859	csdev->dev.groups = desc->groups;
860	csdev->dev.parent = desc->dev;
861	csdev->dev.release = coresight_device_release;
862	csdev->dev.bus = &coresight_bustype;
863	dev_set_name(&csdev->dev, "%s", desc->pdata->name);
864
865	ret = device_register(&csdev->dev);
866	if (ret)
867		goto err_device_register;
868
869	mutex_lock(&coresight_mutex);
870
871	coresight_fixup_device_conns(csdev);
872	coresight_fixup_orphan_conns(csdev);
873
874	mutex_unlock(&coresight_mutex);
875
876	return csdev;
877
878err_device_register:
879	kfree(conns);
880err_kzalloc_conns:
881	kfree(refcnts);
882err_kzalloc_refcnts:
883	kfree(csdev);
884err_kzalloc_csdev:
885	return ERR_PTR(ret);
886}
887EXPORT_SYMBOL_GPL(coresight_register);
888
889void coresight_unregister(struct coresight_device *csdev)
890{
891	/* Remove references of that device in the topology */
892	coresight_remove_conns(csdev);
893	device_unregister(&csdev->dev);
894}
895EXPORT_SYMBOL_GPL(coresight_unregister);
v4.17
   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);