1 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
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.
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.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/device.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>
28 #include "coresight-priv.h"
30 static DEFINE_MUTEX(coresight_mutex);
33 * struct coresight_node - elements of a path, from source to sink
34 * @csdev: Address of an element.
35 * @link: hook to the list.
37 struct coresight_node {
38 struct coresight_device *csdev;
39 struct list_head link;
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.
46 static DEFINE_PER_CPU(struct list_head *, sysfs_path);
48 static int coresight_id_match(struct device *dev, void *data)
50 int trace_id, i_trace_id;
51 struct coresight_device *csdev, *i_csdev;
54 i_csdev = to_coresight_device(dev);
57 * No need to care about oneself and components that are not
58 * sources or not enabled
60 if (i_csdev == csdev || !i_csdev->enable ||
61 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
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);
68 /* All you need is one */
69 if (trace_id == i_trace_id)
75 static int coresight_source_is_unique(struct coresight_device *csdev)
77 int trace_id = source_ops(csdev)->trace_id(csdev);
79 /* this shouldn't happen */
83 return !bus_for_each_dev(&coresight_bustype, NULL,
84 csdev, coresight_id_match);
87 static int coresight_find_link_inport(struct coresight_device *csdev,
88 struct coresight_device *parent)
91 struct coresight_connection *conn;
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;
99 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
100 dev_name(&parent->dev), dev_name(&csdev->dev));
105 static int coresight_find_link_outport(struct coresight_device *csdev,
106 struct coresight_device *child)
109 struct coresight_connection *conn;
111 for (i = 0; i < csdev->nr_outport; i++) {
112 conn = &csdev->conns[i];
113 if (conn->child_dev == child)
114 return conn->outport;
117 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
118 dev_name(&csdev->dev), dev_name(&child->dev));
123 static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
127 if (!csdev->enable) {
128 if (sink_ops(csdev)->enable) {
129 ret = sink_ops(csdev)->enable(csdev, mode);
133 csdev->enable = true;
136 atomic_inc(csdev->refcnt);
141 static void coresight_disable_sink(struct coresight_device *csdev)
143 if (atomic_dec_return(csdev->refcnt) == 0) {
144 if (sink_ops(csdev)->disable) {
145 sink_ops(csdev)->disable(csdev);
146 csdev->enable = false;
151 static int coresight_enable_link(struct coresight_device *csdev,
152 struct coresight_device *parent,
153 struct coresight_device *child)
157 int refport, inport, outport;
159 if (!parent || !child)
162 inport = coresight_find_link_inport(csdev, parent);
163 outport = coresight_find_link_outport(csdev, child);
164 link_subtype = csdev->subtype.link_subtype;
166 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
168 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
173 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
174 if (link_ops(csdev)->enable) {
175 ret = link_ops(csdev)->enable(csdev, inport, outport);
181 csdev->enable = true;
186 static void coresight_disable_link(struct coresight_device *csdev,
187 struct coresight_device *parent,
188 struct coresight_device *child)
192 int refport, inport, outport;
194 if (!parent || !child)
197 inport = coresight_find_link_inport(csdev, parent);
198 outport = coresight_find_link_outport(csdev, child);
199 link_subtype = csdev->subtype.link_subtype;
201 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
203 nr_conns = csdev->nr_inport;
204 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
206 nr_conns = csdev->nr_outport;
212 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
213 if (link_ops(csdev)->disable)
214 link_ops(csdev)->disable(csdev, inport, outport);
217 for (i = 0; i < nr_conns; i++)
218 if (atomic_read(&csdev->refcnt[i]) != 0)
221 csdev->enable = false;
224 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
228 if (!coresight_source_is_unique(csdev)) {
229 dev_warn(&csdev->dev, "traceID %d not unique\n",
230 source_ops(csdev)->trace_id(csdev));
234 if (!csdev->enable) {
235 if (source_ops(csdev)->enable) {
236 ret = source_ops(csdev)->enable(csdev, NULL, mode);
240 csdev->enable = true;
243 atomic_inc(csdev->refcnt);
248 static void coresight_disable_source(struct coresight_device *csdev)
250 if (atomic_dec_return(csdev->refcnt) == 0) {
251 if (source_ops(csdev)->disable) {
252 source_ops(csdev)->disable(csdev);
253 csdev->enable = false;
258 void coresight_disable_path(struct list_head *path)
260 struct coresight_node *nd;
261 struct coresight_device *csdev, *parent, *child;
263 list_for_each_entry(nd, path, link) {
266 switch (csdev->type) {
267 case CORESIGHT_DEV_TYPE_SINK:
268 case CORESIGHT_DEV_TYPE_LINKSINK:
269 coresight_disable_sink(csdev);
271 case CORESIGHT_DEV_TYPE_SOURCE:
272 /* sources are disabled from either sysFS or Perf */
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);
285 int coresight_enable_path(struct list_head *path, u32 mode)
289 struct coresight_node *nd;
290 struct coresight_device *csdev, *parent, *child;
292 list_for_each_entry_reverse(nd, path, link) {
295 switch (csdev->type) {
296 case CORESIGHT_DEV_TYPE_SINK:
297 case CORESIGHT_DEV_TYPE_LINKSINK:
298 ret = coresight_enable_sink(csdev, mode);
302 case CORESIGHT_DEV_TYPE_SOURCE:
303 /* sources are enabled from either sysFS or Perf */
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);
320 coresight_disable_path(path);
324 struct coresight_device *coresight_get_sink(struct list_head *path)
326 struct coresight_device *csdev;
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)
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.
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
350 static int _coresight_build_path(struct coresight_device *csdev,
351 struct list_head *path)
355 struct coresight_node *node;
356 struct coresight_connection *conn;
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)
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) {
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
382 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
387 list_add(&node->link, path);
388 pm_runtime_get_sync(csdev->dev.parent);
393 struct list_head *coresight_build_path(struct coresight_device *csdev)
395 struct list_head *path;
397 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
401 INIT_LIST_HEAD(path);
403 if (_coresight_build_path(csdev, path)) {
412 * coresight_release_path - release a previously built path.
413 * @path: the path to release.
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.
418 void coresight_release_path(struct list_head *path)
420 struct coresight_device *csdev;
421 struct coresight_node *nd, *next;
423 list_for_each_entry_safe(nd, next, path, link) {
426 pm_runtime_put_sync(csdev->dev.parent);
435 int coresight_enable(struct coresight_device *csdev)
439 struct list_head *path;
441 mutex_lock(&coresight_mutex);
442 if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
444 dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
450 path = coresight_build_path(csdev);
452 pr_err("building path(s) failed\n");
456 ret = coresight_enable_path(path, CS_MODE_SYSFS);
460 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
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.
471 cpu = source_ops(csdev)->cpu_id(csdev);
472 per_cpu(sysfs_path, cpu) = path;
475 mutex_unlock(&coresight_mutex);
479 coresight_disable_path(path);
482 coresight_release_path(path);
485 EXPORT_SYMBOL_GPL(coresight_enable);
487 void coresight_disable(struct coresight_device *csdev)
490 struct list_head *path;
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__);
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;
508 mutex_unlock(&coresight_mutex);
510 EXPORT_SYMBOL_GPL(coresight_disable);
512 static ssize_t enable_sink_show(struct device *dev,
513 struct device_attribute *attr, char *buf)
515 struct coresight_device *csdev = to_coresight_device(dev);
517 return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->activated);
520 static ssize_t enable_sink_store(struct device *dev,
521 struct device_attribute *attr,
522 const char *buf, size_t size)
526 struct coresight_device *csdev = to_coresight_device(dev);
528 ret = kstrtoul(buf, 10, &val);
533 csdev->activated = true;
535 csdev->activated = false;
540 static DEVICE_ATTR_RW(enable_sink);
542 static ssize_t enable_source_show(struct device *dev,
543 struct device_attribute *attr, char *buf)
545 struct coresight_device *csdev = to_coresight_device(dev);
547 return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->enable);
550 static ssize_t enable_source_store(struct device *dev,
551 struct device_attribute *attr,
552 const char *buf, size_t size)
556 struct coresight_device *csdev = to_coresight_device(dev);
558 ret = kstrtoul(buf, 10, &val);
563 ret = coresight_enable(csdev);
567 coresight_disable(csdev);
572 static DEVICE_ATTR_RW(enable_source);
574 static struct attribute *coresight_sink_attrs[] = {
575 &dev_attr_enable_sink.attr,
578 ATTRIBUTE_GROUPS(coresight_sink);
580 static struct attribute *coresight_source_attrs[] = {
581 &dev_attr_enable_source.attr,
584 ATTRIBUTE_GROUPS(coresight_source);
586 static struct device_type coresight_dev_type[] = {
592 .groups = coresight_sink_groups,
599 .groups = coresight_sink_groups,
603 .groups = coresight_source_groups,
607 static void coresight_device_release(struct device *dev)
609 struct coresight_device *csdev = to_coresight_device(dev);
612 kfree(csdev->refcnt);
616 static int coresight_orphan_match(struct device *dev, void *data)
619 bool still_orphan = false;
620 struct coresight_device *csdev, *i_csdev;
621 struct coresight_connection *conn;
624 i_csdev = to_coresight_device(dev);
626 /* No need to check oneself */
627 if (csdev == i_csdev)
630 /* Move on to another component if no connection is orphan */
631 if (!i_csdev->orphan)
634 * Circle throuch all the connection of that component. If we find
635 * an orphan connection whose name matches @csdev, link it.
637 for (i = 0; i < i_csdev->nr_outport; i++) {
638 conn = &i_csdev->conns[i];
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;
646 /* This component still has an orphan */
652 i_csdev->orphan = still_orphan;
655 * Returning '0' ensures that all known component on the
656 * bus will be checked.
661 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
664 * No need to check for a return value as orphan connection(s)
665 * are hooked-up with each newly added component.
667 bus_for_each_dev(&coresight_bustype, NULL,
668 csdev, coresight_orphan_match);
672 static int coresight_name_match(struct device *dev, void *data)
675 struct coresight_device *i_csdev;
678 i_csdev = to_coresight_device(dev);
680 if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
686 static void coresight_fixup_device_conns(struct coresight_device *csdev)
689 struct device *dev = NULL;
690 struct coresight_connection *conn;
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);
699 conn->child_dev = to_coresight_device(dev);
700 /* and put reference from 'bus_find_device()' */
703 csdev->orphan = true;
704 conn->child_dev = NULL;
709 static int coresight_remove_match(struct device *dev, void *data)
712 struct coresight_device *csdev, *iterator;
713 struct coresight_connection *conn;
716 iterator = to_coresight_device(dev);
718 /* No need to check oneself */
719 if (csdev == iterator)
723 * Circle throuch all the connection of that component. If we find
724 * a connection whose name matches @csdev, remove it.
726 for (i = 0; i < iterator->nr_outport; i++) {
727 conn = &iterator->conns[i];
729 if (conn->child_dev == NULL)
732 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
733 iterator->orphan = true;
734 conn->child_dev = NULL;
735 /* No need to continue */
741 * Returning '0' ensures that all known component on the
742 * bus will be checked.
747 static void coresight_remove_conns(struct coresight_device *csdev)
749 bus_for_each_dev(&coresight_bustype, NULL,
750 csdev, coresight_remove_match);
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.
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.
764 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
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 */
773 if (val & BIT(position))
775 /* waiting on the bit to go from 1 to 0 */
777 if (!(val & BIT(position)))
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.
793 struct bus_type coresight_bustype = {
797 static int __init coresight_init(void)
799 return bus_register(&coresight_bustype);
801 postcore_initcall(coresight_init);
803 struct coresight_device *coresight_register(struct coresight_desc *desc)
809 atomic_t *refcnts = NULL;
810 struct coresight_device *csdev;
811 struct coresight_connection *conns;
813 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
816 goto err_kzalloc_csdev;
819 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
820 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
821 link_subtype = desc->subtype.link_subtype;
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;
829 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
832 goto err_kzalloc_refcnts;
835 csdev->refcnt = refcnts;
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);
842 goto err_kzalloc_conns;
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];
851 csdev->conns = conns;
853 csdev->type = desc->type;
854 csdev->subtype = desc->subtype;
855 csdev->ops = desc->ops;
856 csdev->orphan = false;
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);
865 ret = device_register(&csdev->dev);
867 goto err_device_register;
869 mutex_lock(&coresight_mutex);
871 coresight_fixup_device_conns(csdev);
872 coresight_fixup_orphan_conns(csdev);
874 mutex_unlock(&coresight_mutex);
887 EXPORT_SYMBOL_GPL(coresight_register);
889 void coresight_unregister(struct coresight_device *csdev)
891 /* Remove references of that device in the topology */
892 coresight_remove_conns(csdev);
893 device_unregister(&csdev->dev);
895 EXPORT_SYMBOL_GPL(coresight_unregister);