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 *, tracer_path);
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
54 static struct list_head *stm_path;
56 static int coresight_id_match(struct device *dev, void *data)
58 int trace_id, i_trace_id;
59 struct coresight_device *csdev, *i_csdev;
62 i_csdev = to_coresight_device(dev);
65 * No need to care about oneself and components that are not
66 * sources or not enabled
68 if (i_csdev == csdev || !i_csdev->enable ||
69 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
72 /* Get the source ID for both compoment */
73 trace_id = source_ops(csdev)->trace_id(csdev);
74 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
76 /* All you need is one */
77 if (trace_id == i_trace_id)
83 static int coresight_source_is_unique(struct coresight_device *csdev)
85 int trace_id = source_ops(csdev)->trace_id(csdev);
87 /* this shouldn't happen */
91 return !bus_for_each_dev(&coresight_bustype, NULL,
92 csdev, coresight_id_match);
95 static int coresight_find_link_inport(struct coresight_device *csdev,
96 struct coresight_device *parent)
99 struct coresight_connection *conn;
101 for (i = 0; i < parent->nr_outport; i++) {
102 conn = &parent->conns[i];
103 if (conn->child_dev == csdev)
104 return conn->child_port;
107 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
108 dev_name(&parent->dev), dev_name(&csdev->dev));
113 static int coresight_find_link_outport(struct coresight_device *csdev,
114 struct coresight_device *child)
117 struct coresight_connection *conn;
119 for (i = 0; i < csdev->nr_outport; i++) {
120 conn = &csdev->conns[i];
121 if (conn->child_dev == child)
122 return conn->outport;
125 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
126 dev_name(&csdev->dev), dev_name(&child->dev));
131 static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
136 * We need to make sure the "new" session is compatible with the
137 * existing "mode" of operation.
139 if (sink_ops(csdev)->enable) {
140 ret = sink_ops(csdev)->enable(csdev, mode);
143 csdev->enable = true;
146 atomic_inc(csdev->refcnt);
151 static void coresight_disable_sink(struct coresight_device *csdev)
153 if (atomic_dec_return(csdev->refcnt) == 0) {
154 if (sink_ops(csdev)->disable) {
155 sink_ops(csdev)->disable(csdev);
156 csdev->enable = false;
161 static int coresight_enable_link(struct coresight_device *csdev,
162 struct coresight_device *parent,
163 struct coresight_device *child)
167 int refport, inport, outport;
169 if (!parent || !child)
172 inport = coresight_find_link_inport(csdev, parent);
173 outport = coresight_find_link_outport(csdev, child);
174 link_subtype = csdev->subtype.link_subtype;
176 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
178 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
186 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
187 if (link_ops(csdev)->enable) {
188 ret = link_ops(csdev)->enable(csdev, inport, outport);
194 csdev->enable = true;
199 static void coresight_disable_link(struct coresight_device *csdev,
200 struct coresight_device *parent,
201 struct coresight_device *child)
205 int refport, inport, outport;
207 if (!parent || !child)
210 inport = coresight_find_link_inport(csdev, parent);
211 outport = coresight_find_link_outport(csdev, child);
212 link_subtype = csdev->subtype.link_subtype;
214 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
216 nr_conns = csdev->nr_inport;
217 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
219 nr_conns = csdev->nr_outport;
225 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
226 if (link_ops(csdev)->disable)
227 link_ops(csdev)->disable(csdev, inport, outport);
230 for (i = 0; i < nr_conns; i++)
231 if (atomic_read(&csdev->refcnt[i]) != 0)
234 csdev->enable = false;
237 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
241 if (!coresight_source_is_unique(csdev)) {
242 dev_warn(&csdev->dev, "traceID %d not unique\n",
243 source_ops(csdev)->trace_id(csdev));
247 if (!csdev->enable) {
248 if (source_ops(csdev)->enable) {
249 ret = source_ops(csdev)->enable(csdev, NULL, mode);
253 csdev->enable = true;
256 atomic_inc(csdev->refcnt);
261 static void coresight_disable_source(struct coresight_device *csdev)
263 if (atomic_dec_return(csdev->refcnt) == 0) {
264 if (source_ops(csdev)->disable) {
265 source_ops(csdev)->disable(csdev, NULL);
266 csdev->enable = false;
271 void coresight_disable_path(struct list_head *path)
274 struct coresight_node *nd;
275 struct coresight_device *csdev, *parent, *child;
277 list_for_each_entry(nd, path, link) {
282 * ETF devices are tricky... They can be a link or a sink,
283 * depending on how they are configured. If an ETF has been
284 * "activated" it will be configured as a sink, otherwise
285 * go ahead with the link configuration.
287 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
288 type = (csdev == coresight_get_sink(path)) ?
289 CORESIGHT_DEV_TYPE_SINK :
290 CORESIGHT_DEV_TYPE_LINK;
293 case CORESIGHT_DEV_TYPE_SINK:
294 coresight_disable_sink(csdev);
296 case CORESIGHT_DEV_TYPE_SOURCE:
297 /* sources are disabled from either sysFS or Perf */
299 case CORESIGHT_DEV_TYPE_LINK:
300 parent = list_prev_entry(nd, link)->csdev;
301 child = list_next_entry(nd, link)->csdev;
302 coresight_disable_link(csdev, parent, child);
310 int coresight_enable_path(struct list_head *path, u32 mode)
315 struct coresight_node *nd;
316 struct coresight_device *csdev, *parent, *child;
318 list_for_each_entry_reverse(nd, path, link) {
323 * ETF devices are tricky... They can be a link or a sink,
324 * depending on how they are configured. If an ETF has been
325 * "activated" it will be configured as a sink, otherwise
326 * go ahead with the link configuration.
328 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
329 type = (csdev == coresight_get_sink(path)) ?
330 CORESIGHT_DEV_TYPE_SINK :
331 CORESIGHT_DEV_TYPE_LINK;
334 case CORESIGHT_DEV_TYPE_SINK:
335 ret = coresight_enable_sink(csdev, mode);
337 * Sink is the first component turned on. If we
338 * failed to enable the sink, there are no components
339 * that need disabling. Disabling the path here
340 * would mean we could disrupt an existing session.
345 case CORESIGHT_DEV_TYPE_SOURCE:
346 /* sources are enabled from either sysFS or Perf */
348 case CORESIGHT_DEV_TYPE_LINK:
349 parent = list_prev_entry(nd, link)->csdev;
350 child = list_next_entry(nd, link)->csdev;
351 ret = coresight_enable_link(csdev, parent, child);
363 coresight_disable_path(path);
367 struct coresight_device *coresight_get_sink(struct list_head *path)
369 struct coresight_device *csdev;
374 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
375 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
376 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
383 * _coresight_build_path - recursively build a path from a @csdev to a sink.
384 * @csdev: The device to start from.
385 * @path: The list to add devices to.
387 * The tree of Coresight device is traversed until an activated sink is
388 * found. From there the sink is added to the list along with all the
389 * devices that led to that point - the end result is a list from source
390 * to sink. In that list the source is the first device and the sink the
393 static int _coresight_build_path(struct coresight_device *csdev,
394 struct list_head *path)
398 struct coresight_node *node;
400 /* An activated sink has been found. Enqueue the element */
401 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
402 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) && csdev->activated)
405 /* Not a sink - recursively explore each port found on this element */
406 for (i = 0; i < csdev->nr_outport; i++) {
407 struct coresight_device *child_dev = csdev->conns[i].child_dev;
409 if (child_dev && _coresight_build_path(child_dev, path) == 0) {
420 * A path from this element to a sink has been found. The elements
421 * leading to the sink are already enqueued, all that is left to do
422 * is tell the PM runtime core we need this element and add a node
425 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
430 list_add(&node->link, path);
431 pm_runtime_get_sync(csdev->dev.parent);
436 struct list_head *coresight_build_path(struct coresight_device *csdev)
438 struct list_head *path;
441 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
443 return ERR_PTR(-ENOMEM);
445 INIT_LIST_HEAD(path);
447 rc = _coresight_build_path(csdev, path);
457 * coresight_release_path - release a previously built path.
458 * @path: the path to release.
460 * Go through all the elements of a path and 1) removed it from the list and
461 * 2) free the memory allocated for each node.
463 void coresight_release_path(struct list_head *path)
465 struct coresight_device *csdev;
466 struct coresight_node *nd, *next;
468 list_for_each_entry_safe(nd, next, path, link) {
471 pm_runtime_put_sync(csdev->dev.parent);
480 /** coresight_validate_source - make sure a source has the right credentials
481 * @csdev: the device structure for a source.
482 * @function: the function this was called from.
484 * Assumes the coresight_mutex is held.
486 static int coresight_validate_source(struct coresight_device *csdev,
487 const char *function)
492 subtype = csdev->subtype.source_subtype;
494 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
495 dev_err(&csdev->dev, "wrong device type in %s\n", function);
499 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
500 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
501 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
508 int coresight_enable(struct coresight_device *csdev)
511 struct list_head *path;
512 enum coresight_dev_subtype_source subtype;
514 subtype = csdev->subtype.source_subtype;
516 mutex_lock(&coresight_mutex);
518 ret = coresight_validate_source(csdev, __func__);
524 * There could be multiple applications driving the software
525 * source. So keep the refcount for each such user when the
526 * source is already enabled.
528 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
529 atomic_inc(csdev->refcnt);
533 path = coresight_build_path(csdev);
535 pr_err("building path(s) failed\n");
540 ret = coresight_enable_path(path, CS_MODE_SYSFS);
544 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
549 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
551 * When working from sysFS it is important to keep track
552 * of the paths that were created so that they can be
553 * undone in 'coresight_disable()'. Since there can only
554 * be a single session per tracer (when working from sysFS)
555 * a per-cpu variable will do just fine.
557 cpu = source_ops(csdev)->cpu_id(csdev);
558 per_cpu(tracer_path, cpu) = path;
560 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
564 /* We can't be here */
569 mutex_unlock(&coresight_mutex);
573 coresight_disable_path(path);
576 coresight_release_path(path);
579 EXPORT_SYMBOL_GPL(coresight_enable);
581 void coresight_disable(struct coresight_device *csdev)
584 struct list_head *path = NULL;
586 mutex_lock(&coresight_mutex);
588 ret = coresight_validate_source(csdev, __func__);
595 switch (csdev->subtype.source_subtype) {
596 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
597 cpu = source_ops(csdev)->cpu_id(csdev);
598 path = per_cpu(tracer_path, cpu);
599 per_cpu(tracer_path, cpu) = NULL;
601 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
606 /* We can't be here */
610 coresight_disable_source(csdev);
611 coresight_disable_path(path);
612 coresight_release_path(path);
615 mutex_unlock(&coresight_mutex);
617 EXPORT_SYMBOL_GPL(coresight_disable);
619 static ssize_t enable_sink_show(struct device *dev,
620 struct device_attribute *attr, char *buf)
622 struct coresight_device *csdev = to_coresight_device(dev);
624 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
627 static ssize_t enable_sink_store(struct device *dev,
628 struct device_attribute *attr,
629 const char *buf, size_t size)
633 struct coresight_device *csdev = to_coresight_device(dev);
635 ret = kstrtoul(buf, 10, &val);
640 csdev->activated = true;
642 csdev->activated = false;
647 static DEVICE_ATTR_RW(enable_sink);
649 static ssize_t enable_source_show(struct device *dev,
650 struct device_attribute *attr, char *buf)
652 struct coresight_device *csdev = to_coresight_device(dev);
654 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
657 static ssize_t enable_source_store(struct device *dev,
658 struct device_attribute *attr,
659 const char *buf, size_t size)
663 struct coresight_device *csdev = to_coresight_device(dev);
665 ret = kstrtoul(buf, 10, &val);
670 ret = coresight_enable(csdev);
674 coresight_disable(csdev);
679 static DEVICE_ATTR_RW(enable_source);
681 static struct attribute *coresight_sink_attrs[] = {
682 &dev_attr_enable_sink.attr,
685 ATTRIBUTE_GROUPS(coresight_sink);
687 static struct attribute *coresight_source_attrs[] = {
688 &dev_attr_enable_source.attr,
691 ATTRIBUTE_GROUPS(coresight_source);
693 static struct device_type coresight_dev_type[] = {
699 .groups = coresight_sink_groups,
706 .groups = coresight_sink_groups,
710 .groups = coresight_source_groups,
714 static void coresight_device_release(struct device *dev)
716 struct coresight_device *csdev = to_coresight_device(dev);
719 kfree(csdev->refcnt);
723 static int coresight_orphan_match(struct device *dev, void *data)
726 bool still_orphan = false;
727 struct coresight_device *csdev, *i_csdev;
728 struct coresight_connection *conn;
731 i_csdev = to_coresight_device(dev);
733 /* No need to check oneself */
734 if (csdev == i_csdev)
737 /* Move on to another component if no connection is orphan */
738 if (!i_csdev->orphan)
741 * Circle throuch all the connection of that component. If we find
742 * an orphan connection whose name matches @csdev, link it.
744 for (i = 0; i < i_csdev->nr_outport; i++) {
745 conn = &i_csdev->conns[i];
747 /* We have found at least one orphan connection */
748 if (conn->child_dev == NULL) {
749 /* Does it match this newly added device? */
750 if (conn->child_name &&
751 !strcmp(dev_name(&csdev->dev), conn->child_name)) {
752 conn->child_dev = csdev;
754 /* This component still has an orphan */
760 i_csdev->orphan = still_orphan;
763 * Returning '0' ensures that all known component on the
764 * bus will be checked.
769 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
772 * No need to check for a return value as orphan connection(s)
773 * are hooked-up with each newly added component.
775 bus_for_each_dev(&coresight_bustype, NULL,
776 csdev, coresight_orphan_match);
780 static int coresight_name_match(struct device *dev, void *data)
783 struct coresight_device *i_csdev;
786 i_csdev = to_coresight_device(dev);
788 if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
794 static void coresight_fixup_device_conns(struct coresight_device *csdev)
797 struct device *dev = NULL;
798 struct coresight_connection *conn;
800 for (i = 0; i < csdev->nr_outport; i++) {
801 conn = &csdev->conns[i];
802 dev = bus_find_device(&coresight_bustype, NULL,
803 (void *)conn->child_name,
804 coresight_name_match);
807 conn->child_dev = to_coresight_device(dev);
808 /* and put reference from 'bus_find_device()' */
811 csdev->orphan = true;
812 conn->child_dev = NULL;
817 static int coresight_remove_match(struct device *dev, void *data)
820 struct coresight_device *csdev, *iterator;
821 struct coresight_connection *conn;
824 iterator = to_coresight_device(dev);
826 /* No need to check oneself */
827 if (csdev == iterator)
831 * Circle throuch all the connection of that component. If we find
832 * a connection whose name matches @csdev, remove it.
834 for (i = 0; i < iterator->nr_outport; i++) {
835 conn = &iterator->conns[i];
837 if (conn->child_dev == NULL)
840 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
841 iterator->orphan = true;
842 conn->child_dev = NULL;
843 /* No need to continue */
849 * Returning '0' ensures that all known component on the
850 * bus will be checked.
855 static void coresight_remove_conns(struct coresight_device *csdev)
857 bus_for_each_dev(&coresight_bustype, NULL,
858 csdev, coresight_remove_match);
862 * coresight_timeout - loop until a bit has changed to a specific state.
863 * @addr: base address of the area of interest.
864 * @offset: address of a register, starting from @addr.
865 * @position: the position of the bit of interest.
866 * @value: the value the bit should have.
868 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
869 * TIMEOUT_US has elapsed, which ever happens first.
872 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
877 for (i = TIMEOUT_US; i > 0; i--) {
878 val = __raw_readl(addr + offset);
879 /* waiting on the bit to go from 0 to 1 */
881 if (val & BIT(position))
883 /* waiting on the bit to go from 1 to 0 */
885 if (!(val & BIT(position)))
890 * Delay is arbitrary - the specification doesn't say how long
891 * we are expected to wait. Extra check required to make sure
892 * we don't wait needlessly on the last iteration.
901 struct bus_type coresight_bustype = {
905 static int __init coresight_init(void)
907 return bus_register(&coresight_bustype);
909 postcore_initcall(coresight_init);
911 struct coresight_device *coresight_register(struct coresight_desc *desc)
917 atomic_t *refcnts = NULL;
918 struct coresight_device *csdev;
919 struct coresight_connection *conns = NULL;
921 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
924 goto err_kzalloc_csdev;
927 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
928 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
929 link_subtype = desc->subtype.link_subtype;
931 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
932 nr_refcnts = desc->pdata->nr_inport;
933 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
934 nr_refcnts = desc->pdata->nr_outport;
937 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
940 goto err_kzalloc_refcnts;
943 csdev->refcnt = refcnts;
945 csdev->nr_inport = desc->pdata->nr_inport;
946 csdev->nr_outport = desc->pdata->nr_outport;
948 /* Initialise connections if there is at least one outport */
949 if (csdev->nr_outport) {
950 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
953 goto err_kzalloc_conns;
956 for (i = 0; i < csdev->nr_outport; i++) {
957 conns[i].outport = desc->pdata->outports[i];
958 conns[i].child_name = desc->pdata->child_names[i];
959 conns[i].child_port = desc->pdata->child_ports[i];
963 csdev->conns = conns;
965 csdev->type = desc->type;
966 csdev->subtype = desc->subtype;
967 csdev->ops = desc->ops;
968 csdev->orphan = false;
970 csdev->dev.type = &coresight_dev_type[desc->type];
971 csdev->dev.groups = desc->groups;
972 csdev->dev.parent = desc->dev;
973 csdev->dev.release = coresight_device_release;
974 csdev->dev.bus = &coresight_bustype;
975 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
977 ret = device_register(&csdev->dev);
979 goto err_device_register;
981 mutex_lock(&coresight_mutex);
983 coresight_fixup_device_conns(csdev);
984 coresight_fixup_orphan_conns(csdev);
986 mutex_unlock(&coresight_mutex);
999 EXPORT_SYMBOL_GPL(coresight_register);
1001 void coresight_unregister(struct coresight_device *csdev)
1003 /* Remove references of that device in the topology */
1004 coresight_remove_conns(csdev);
1005 device_unregister(&csdev->dev);
1007 EXPORT_SYMBOL_GPL(coresight_unregister);