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/module.h>
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/export.h>
21 #include <linux/slab.h>
22 #include <linux/mutex.h>
23 #include <linux/clk.h>
24 #include <linux/coresight.h>
25 #include <linux/of_platform.h>
26 #include <linux/delay.h>
27 #include <linux/pm_runtime.h>
29 #include "coresight-priv.h"
31 static DEFINE_MUTEX(coresight_mutex);
34 * struct coresight_node - elements of a path, from source to sink
35 * @csdev: Address of an element.
36 * @link: hook to the list.
38 struct coresight_node {
39 struct coresight_device *csdev;
40 struct list_head link;
44 * When operating Coresight drivers from the sysFS interface, only a single
45 * path can exist from a tracer (associated to a CPU) to a sink.
47 static DEFINE_PER_CPU(struct list_head *, sysfs_path);
49 static int coresight_id_match(struct device *dev, void *data)
51 int trace_id, i_trace_id;
52 struct coresight_device *csdev, *i_csdev;
55 i_csdev = to_coresight_device(dev);
58 * No need to care about oneself and components that are not
59 * sources or not enabled
61 if (i_csdev == csdev || !i_csdev->enable ||
62 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
65 /* Get the source ID for both compoment */
66 trace_id = source_ops(csdev)->trace_id(csdev);
67 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
69 /* All you need is one */
70 if (trace_id == i_trace_id)
76 static int coresight_source_is_unique(struct coresight_device *csdev)
78 int trace_id = source_ops(csdev)->trace_id(csdev);
80 /* this shouldn't happen */
84 return !bus_for_each_dev(&coresight_bustype, NULL,
85 csdev, coresight_id_match);
88 static int coresight_find_link_inport(struct coresight_device *csdev,
89 struct coresight_device *parent)
92 struct coresight_connection *conn;
94 for (i = 0; i < parent->nr_outport; i++) {
95 conn = &parent->conns[i];
96 if (conn->child_dev == csdev)
97 return conn->child_port;
100 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
101 dev_name(&parent->dev), dev_name(&csdev->dev));
106 static int coresight_find_link_outport(struct coresight_device *csdev,
107 struct coresight_device *child)
110 struct coresight_connection *conn;
112 for (i = 0; i < csdev->nr_outport; i++) {
113 conn = &csdev->conns[i];
114 if (conn->child_dev == child)
115 return conn->outport;
118 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
119 dev_name(&csdev->dev), dev_name(&child->dev));
124 static int coresight_enable_sink(struct coresight_device *csdev)
128 if (!csdev->enable) {
129 if (sink_ops(csdev)->enable) {
130 ret = sink_ops(csdev)->enable(csdev);
134 csdev->enable = true;
137 atomic_inc(csdev->refcnt);
142 static void coresight_disable_sink(struct coresight_device *csdev)
144 if (atomic_dec_return(csdev->refcnt) == 0) {
145 if (sink_ops(csdev)->disable) {
146 sink_ops(csdev)->disable(csdev);
147 csdev->enable = false;
152 static int coresight_enable_link(struct coresight_device *csdev,
153 struct coresight_device *parent,
154 struct coresight_device *child)
158 int refport, inport, outport;
160 if (!parent || !child)
163 inport = coresight_find_link_inport(csdev, parent);
164 outport = coresight_find_link_outport(csdev, child);
165 link_subtype = csdev->subtype.link_subtype;
167 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
169 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
174 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
175 if (link_ops(csdev)->enable) {
176 ret = link_ops(csdev)->enable(csdev, inport, outport);
182 csdev->enable = true;
187 static void coresight_disable_link(struct coresight_device *csdev,
188 struct coresight_device *parent,
189 struct coresight_device *child)
193 int refport, inport, outport;
195 if (!parent || !child)
198 inport = coresight_find_link_inport(csdev, parent);
199 outport = coresight_find_link_outport(csdev, child);
200 link_subtype = csdev->subtype.link_subtype;
202 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
204 nr_conns = csdev->nr_inport;
205 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
207 nr_conns = csdev->nr_outport;
213 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
214 if (link_ops(csdev)->disable)
215 link_ops(csdev)->disable(csdev, inport, outport);
218 for (i = 0; i < nr_conns; i++)
219 if (atomic_read(&csdev->refcnt[i]) != 0)
222 csdev->enable = false;
225 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
229 if (!coresight_source_is_unique(csdev)) {
230 dev_warn(&csdev->dev, "traceID %d not unique\n",
231 source_ops(csdev)->trace_id(csdev));
235 if (!csdev->enable) {
236 if (source_ops(csdev)->enable) {
237 ret = source_ops(csdev)->enable(csdev, mode);
241 csdev->enable = true;
244 atomic_inc(csdev->refcnt);
249 static void coresight_disable_source(struct coresight_device *csdev)
251 if (atomic_dec_return(csdev->refcnt) == 0) {
252 if (source_ops(csdev)->disable) {
253 source_ops(csdev)->disable(csdev);
254 csdev->enable = false;
259 void coresight_disable_path(struct list_head *path)
261 struct coresight_node *nd;
262 struct coresight_device *csdev, *parent, *child;
264 list_for_each_entry(nd, path, link) {
267 switch (csdev->type) {
268 case CORESIGHT_DEV_TYPE_SINK:
269 case CORESIGHT_DEV_TYPE_LINKSINK:
270 coresight_disable_sink(csdev);
272 case CORESIGHT_DEV_TYPE_SOURCE:
273 /* sources are disabled from either sysFS or Perf */
275 case CORESIGHT_DEV_TYPE_LINK:
276 parent = list_prev_entry(nd, link)->csdev;
277 child = list_next_entry(nd, link)->csdev;
278 coresight_disable_link(csdev, parent, child);
286 int coresight_enable_path(struct list_head *path)
290 struct coresight_node *nd;
291 struct coresight_device *csdev, *parent, *child;
293 list_for_each_entry_reverse(nd, path, link) {
296 switch (csdev->type) {
297 case CORESIGHT_DEV_TYPE_SINK:
298 case CORESIGHT_DEV_TYPE_LINKSINK:
299 ret = coresight_enable_sink(csdev);
303 case CORESIGHT_DEV_TYPE_SOURCE:
304 /* sources are enabled from either sysFS or Perf */
306 case CORESIGHT_DEV_TYPE_LINK:
307 parent = list_prev_entry(nd, link)->csdev;
308 child = list_next_entry(nd, link)->csdev;
309 ret = coresight_enable_link(csdev, parent, child);
321 coresight_disable_path(path);
325 struct coresight_device *coresight_get_sink(struct list_head *path)
327 struct coresight_device *csdev;
332 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
333 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
334 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
341 * _coresight_build_path - recursively build a path from a @csdev to a sink.
342 * @csdev: The device to start from.
343 * @path: The list to add devices to.
345 * The tree of Coresight device is traversed until an activated sink is
346 * found. From there the sink is added to the list along with all the
347 * devices that led to that point - the end result is a list from source
348 * to sink. In that list the source is the first device and the sink the
351 static int _coresight_build_path(struct coresight_device *csdev,
352 struct list_head *path)
356 struct coresight_node *node;
357 struct coresight_connection *conn;
359 /* An activated sink has been found. Enqueue the element */
360 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
361 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) && csdev->activated)
364 /* Not a sink - recursively explore each port found on this element */
365 for (i = 0; i < csdev->nr_outport; i++) {
366 conn = &csdev->conns[i];
367 if (_coresight_build_path(conn->child_dev, path) == 0) {
378 * A path from this element to a sink has been found. The elements
379 * leading to the sink are already enqueued, all that is left to do
380 * is tell the PM runtime core we need this element and add a node
383 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
388 list_add(&node->link, path);
389 pm_runtime_get_sync(csdev->dev.parent);
394 struct list_head *coresight_build_path(struct coresight_device *csdev)
396 struct list_head *path;
398 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
402 INIT_LIST_HEAD(path);
404 if (_coresight_build_path(csdev, path)) {
413 * coresight_release_path - release a previously built path.
414 * @path: the path to release.
416 * Go through all the elements of a path and 1) removed it from the list and
417 * 2) free the memory allocated for each node.
419 void coresight_release_path(struct list_head *path)
421 struct coresight_device *csdev;
422 struct coresight_node *nd, *next;
424 list_for_each_entry_safe(nd, next, path, link) {
427 pm_runtime_put_sync(csdev->dev.parent);
436 int coresight_enable(struct coresight_device *csdev)
440 struct list_head *path;
442 mutex_lock(&coresight_mutex);
443 if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
445 dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
451 path = coresight_build_path(csdev);
453 pr_err("building path(s) failed\n");
457 ret = coresight_enable_path(path);
461 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
466 * When working from sysFS it is important to keep track
467 * of the paths that were created so that they can be
468 * undone in 'coresight_disable()'. Since there can only
469 * be a single session per tracer (when working from sysFS)
470 * a per-cpu variable will do just fine.
472 cpu = source_ops(csdev)->cpu_id(csdev);
473 per_cpu(sysfs_path, cpu) = path;
476 mutex_unlock(&coresight_mutex);
480 coresight_disable_path(path);
483 coresight_release_path(path);
486 EXPORT_SYMBOL_GPL(coresight_enable);
488 void coresight_disable(struct coresight_device *csdev)
491 struct list_head *path;
493 mutex_lock(&coresight_mutex);
494 if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
495 dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
501 cpu = source_ops(csdev)->cpu_id(csdev);
502 path = per_cpu(sysfs_path, cpu);
503 coresight_disable_source(csdev);
504 coresight_disable_path(path);
505 coresight_release_path(path);
506 per_cpu(sysfs_path, cpu) = NULL;
509 mutex_unlock(&coresight_mutex);
511 EXPORT_SYMBOL_GPL(coresight_disable);
513 static ssize_t enable_sink_show(struct device *dev,
514 struct device_attribute *attr, char *buf)
516 struct coresight_device *csdev = to_coresight_device(dev);
518 return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->activated);
521 static ssize_t enable_sink_store(struct device *dev,
522 struct device_attribute *attr,
523 const char *buf, size_t size)
527 struct coresight_device *csdev = to_coresight_device(dev);
529 ret = kstrtoul(buf, 10, &val);
534 csdev->activated = true;
536 csdev->activated = false;
541 static DEVICE_ATTR_RW(enable_sink);
543 static ssize_t enable_source_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
546 struct coresight_device *csdev = to_coresight_device(dev);
548 return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->enable);
551 static ssize_t enable_source_store(struct device *dev,
552 struct device_attribute *attr,
553 const char *buf, size_t size)
557 struct coresight_device *csdev = to_coresight_device(dev);
559 ret = kstrtoul(buf, 10, &val);
564 ret = coresight_enable(csdev);
568 coresight_disable(csdev);
573 static DEVICE_ATTR_RW(enable_source);
575 static struct attribute *coresight_sink_attrs[] = {
576 &dev_attr_enable_sink.attr,
579 ATTRIBUTE_GROUPS(coresight_sink);
581 static struct attribute *coresight_source_attrs[] = {
582 &dev_attr_enable_source.attr,
585 ATTRIBUTE_GROUPS(coresight_source);
587 static struct device_type coresight_dev_type[] = {
593 .groups = coresight_sink_groups,
600 .groups = coresight_sink_groups,
604 .groups = coresight_source_groups,
608 static void coresight_device_release(struct device *dev)
610 struct coresight_device *csdev = to_coresight_device(dev);
613 kfree(csdev->refcnt);
617 static int coresight_orphan_match(struct device *dev, void *data)
620 bool still_orphan = false;
621 struct coresight_device *csdev, *i_csdev;
622 struct coresight_connection *conn;
625 i_csdev = to_coresight_device(dev);
627 /* No need to check oneself */
628 if (csdev == i_csdev)
631 /* Move on to another component if no connection is orphan */
632 if (!i_csdev->orphan)
635 * Circle throuch all the connection of that component. If we find
636 * an orphan connection whose name matches @csdev, link it.
638 for (i = 0; i < i_csdev->nr_outport; i++) {
639 conn = &i_csdev->conns[i];
641 /* We have found at least one orphan connection */
642 if (conn->child_dev == NULL) {
643 /* Does it match this newly added device? */
644 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
645 conn->child_dev = csdev;
647 /* This component still has an orphan */
653 i_csdev->orphan = still_orphan;
656 * Returning '0' ensures that all known component on the
657 * bus will be checked.
662 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
665 * No need to check for a return value as orphan connection(s)
666 * are hooked-up with each newly added component.
668 bus_for_each_dev(&coresight_bustype, NULL,
669 csdev, coresight_orphan_match);
673 static int coresight_name_match(struct device *dev, void *data)
676 struct coresight_device *i_csdev;
679 i_csdev = to_coresight_device(dev);
681 if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
687 static void coresight_fixup_device_conns(struct coresight_device *csdev)
690 struct device *dev = NULL;
691 struct coresight_connection *conn;
693 for (i = 0; i < csdev->nr_outport; i++) {
694 conn = &csdev->conns[i];
695 dev = bus_find_device(&coresight_bustype, NULL,
696 (void *)conn->child_name,
697 coresight_name_match);
700 conn->child_dev = to_coresight_device(dev);
701 /* and put reference from 'bus_find_device()' */
704 csdev->orphan = true;
705 conn->child_dev = NULL;
710 static int coresight_remove_match(struct device *dev, void *data)
713 struct coresight_device *csdev, *iterator;
714 struct coresight_connection *conn;
717 iterator = to_coresight_device(dev);
719 /* No need to check oneself */
720 if (csdev == iterator)
724 * Circle throuch all the connection of that component. If we find
725 * a connection whose name matches @csdev, remove it.
727 for (i = 0; i < iterator->nr_outport; i++) {
728 conn = &iterator->conns[i];
730 if (conn->child_dev == NULL)
733 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
734 iterator->orphan = true;
735 conn->child_dev = NULL;
736 /* No need to continue */
742 * Returning '0' ensures that all known component on the
743 * bus will be checked.
748 static void coresight_remove_conns(struct coresight_device *csdev)
750 bus_for_each_dev(&coresight_bustype, NULL,
751 csdev, coresight_remove_match);
755 * coresight_timeout - loop until a bit has changed to a specific state.
756 * @addr: base address of the area of interest.
757 * @offset: address of a register, starting from @addr.
758 * @position: the position of the bit of interest.
759 * @value: the value the bit should have.
761 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
762 * TIMEOUT_US has elapsed, which ever happens first.
765 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
770 for (i = TIMEOUT_US; i > 0; i--) {
771 val = __raw_readl(addr + offset);
772 /* waiting on the bit to go from 0 to 1 */
774 if (val & BIT(position))
776 /* waiting on the bit to go from 1 to 0 */
778 if (!(val & BIT(position)))
783 * Delay is arbitrary - the specification doesn't say how long
784 * we are expected to wait. Extra check required to make sure
785 * we don't wait needlessly on the last iteration.
794 struct bus_type coresight_bustype = {
798 static int __init coresight_init(void)
800 return bus_register(&coresight_bustype);
802 postcore_initcall(coresight_init);
804 struct coresight_device *coresight_register(struct coresight_desc *desc)
810 atomic_t *refcnts = NULL;
811 struct coresight_device *csdev;
812 struct coresight_connection *conns;
814 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
817 goto err_kzalloc_csdev;
820 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
821 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
822 link_subtype = desc->subtype.link_subtype;
824 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
825 nr_refcnts = desc->pdata->nr_inport;
826 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
827 nr_refcnts = desc->pdata->nr_outport;
830 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
833 goto err_kzalloc_refcnts;
836 csdev->refcnt = refcnts;
838 csdev->nr_inport = desc->pdata->nr_inport;
839 csdev->nr_outport = desc->pdata->nr_outport;
840 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
843 goto err_kzalloc_conns;
846 for (i = 0; i < csdev->nr_outport; i++) {
847 conns[i].outport = desc->pdata->outports[i];
848 conns[i].child_name = desc->pdata->child_names[i];
849 conns[i].child_port = desc->pdata->child_ports[i];
852 csdev->conns = conns;
854 csdev->type = desc->type;
855 csdev->subtype = desc->subtype;
856 csdev->ops = desc->ops;
857 csdev->orphan = false;
859 csdev->dev.type = &coresight_dev_type[desc->type];
860 csdev->dev.groups = desc->groups;
861 csdev->dev.parent = desc->dev;
862 csdev->dev.release = coresight_device_release;
863 csdev->dev.bus = &coresight_bustype;
864 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
866 ret = device_register(&csdev->dev);
868 goto err_device_register;
870 mutex_lock(&coresight_mutex);
872 coresight_fixup_device_conns(csdev);
873 coresight_fixup_orphan_conns(csdev);
875 mutex_unlock(&coresight_mutex);
888 EXPORT_SYMBOL_GPL(coresight_register);
890 void coresight_unregister(struct coresight_device *csdev)
892 /* Remove references of that device in the topology */
893 coresight_remove_conns(csdev);
894 device_unregister(&csdev->dev);
896 EXPORT_SYMBOL_GPL(coresight_unregister);
898 MODULE_LICENSE("GPL v2");