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)
135 if (!csdev->enable) {
136 if (sink_ops(csdev)->enable) {
137 ret = sink_ops(csdev)->enable(csdev, mode);
141 csdev->enable = true;
144 atomic_inc(csdev->refcnt);
149 static void coresight_disable_sink(struct coresight_device *csdev)
151 if (atomic_dec_return(csdev->refcnt) == 0) {
152 if (sink_ops(csdev)->disable) {
153 sink_ops(csdev)->disable(csdev);
154 csdev->enable = false;
159 static int coresight_enable_link(struct coresight_device *csdev,
160 struct coresight_device *parent,
161 struct coresight_device *child)
165 int refport, inport, outport;
167 if (!parent || !child)
170 inport = coresight_find_link_inport(csdev, parent);
171 outport = coresight_find_link_outport(csdev, child);
172 link_subtype = csdev->subtype.link_subtype;
174 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
176 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
181 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
182 if (link_ops(csdev)->enable) {
183 ret = link_ops(csdev)->enable(csdev, inport, outport);
189 csdev->enable = true;
194 static void coresight_disable_link(struct coresight_device *csdev,
195 struct coresight_device *parent,
196 struct coresight_device *child)
200 int refport, inport, outport;
202 if (!parent || !child)
205 inport = coresight_find_link_inport(csdev, parent);
206 outport = coresight_find_link_outport(csdev, child);
207 link_subtype = csdev->subtype.link_subtype;
209 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
211 nr_conns = csdev->nr_inport;
212 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
214 nr_conns = csdev->nr_outport;
220 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
221 if (link_ops(csdev)->disable)
222 link_ops(csdev)->disable(csdev, inport, outport);
225 for (i = 0; i < nr_conns; i++)
226 if (atomic_read(&csdev->refcnt[i]) != 0)
229 csdev->enable = false;
232 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
236 if (!coresight_source_is_unique(csdev)) {
237 dev_warn(&csdev->dev, "traceID %d not unique\n",
238 source_ops(csdev)->trace_id(csdev));
242 if (!csdev->enable) {
243 if (source_ops(csdev)->enable) {
244 ret = source_ops(csdev)->enable(csdev, NULL, mode);
248 csdev->enable = true;
251 atomic_inc(csdev->refcnt);
256 static void coresight_disable_source(struct coresight_device *csdev)
258 if (atomic_dec_return(csdev->refcnt) == 0) {
259 if (source_ops(csdev)->disable) {
260 source_ops(csdev)->disable(csdev);
261 csdev->enable = false;
266 void coresight_disable_path(struct list_head *path)
269 struct coresight_node *nd;
270 struct coresight_device *csdev, *parent, *child;
272 list_for_each_entry(nd, path, link) {
277 * ETF devices are tricky... They can be a link or a sink,
278 * depending on how they are configured. If an ETF has been
279 * "activated" it will be configured as a sink, otherwise
280 * go ahead with the link configuration.
282 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
283 type = (csdev == coresight_get_sink(path)) ?
284 CORESIGHT_DEV_TYPE_SINK :
285 CORESIGHT_DEV_TYPE_LINK;
288 case CORESIGHT_DEV_TYPE_SINK:
289 coresight_disable_sink(csdev);
291 case CORESIGHT_DEV_TYPE_SOURCE:
292 /* sources are disabled from either sysFS or Perf */
294 case CORESIGHT_DEV_TYPE_LINK:
295 parent = list_prev_entry(nd, link)->csdev;
296 child = list_next_entry(nd, link)->csdev;
297 coresight_disable_link(csdev, parent, child);
305 int coresight_enable_path(struct list_head *path, u32 mode)
310 struct coresight_node *nd;
311 struct coresight_device *csdev, *parent, *child;
313 list_for_each_entry_reverse(nd, path, link) {
318 * ETF devices are tricky... They can be a link or a sink,
319 * depending on how they are configured. If an ETF has been
320 * "activated" it will be configured as a sink, otherwise
321 * go ahead with the link configuration.
323 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
324 type = (csdev == coresight_get_sink(path)) ?
325 CORESIGHT_DEV_TYPE_SINK :
326 CORESIGHT_DEV_TYPE_LINK;
329 case CORESIGHT_DEV_TYPE_SINK:
330 ret = coresight_enable_sink(csdev, mode);
334 case CORESIGHT_DEV_TYPE_SOURCE:
335 /* sources are enabled from either sysFS or Perf */
337 case CORESIGHT_DEV_TYPE_LINK:
338 parent = list_prev_entry(nd, link)->csdev;
339 child = list_next_entry(nd, link)->csdev;
340 ret = coresight_enable_link(csdev, parent, child);
352 coresight_disable_path(path);
356 struct coresight_device *coresight_get_sink(struct list_head *path)
358 struct coresight_device *csdev;
363 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
364 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
365 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
372 * _coresight_build_path - recursively build a path from a @csdev to a sink.
373 * @csdev: The device to start from.
374 * @path: The list to add devices to.
376 * The tree of Coresight device is traversed until an activated sink is
377 * found. From there the sink is added to the list along with all the
378 * devices that led to that point - the end result is a list from source
379 * to sink. In that list the source is the first device and the sink the
382 static int _coresight_build_path(struct coresight_device *csdev,
383 struct list_head *path)
387 struct coresight_node *node;
388 struct coresight_connection *conn;
390 /* An activated sink has been found. Enqueue the element */
391 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
392 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) && csdev->activated)
395 /* Not a sink - recursively explore each port found on this element */
396 for (i = 0; i < csdev->nr_outport; i++) {
397 conn = &csdev->conns[i];
398 if (_coresight_build_path(conn->child_dev, path) == 0) {
409 * A path from this element to a sink has been found. The elements
410 * leading to the sink are already enqueued, all that is left to do
411 * is tell the PM runtime core we need this element and add a node
414 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
419 list_add(&node->link, path);
420 pm_runtime_get_sync(csdev->dev.parent);
425 struct list_head *coresight_build_path(struct coresight_device *csdev)
427 struct list_head *path;
429 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
433 INIT_LIST_HEAD(path);
435 if (_coresight_build_path(csdev, path)) {
444 * coresight_release_path - release a previously built path.
445 * @path: the path to release.
447 * Go through all the elements of a path and 1) removed it from the list and
448 * 2) free the memory allocated for each node.
450 void coresight_release_path(struct list_head *path)
452 struct coresight_device *csdev;
453 struct coresight_node *nd, *next;
455 list_for_each_entry_safe(nd, next, path, link) {
458 pm_runtime_put_sync(csdev->dev.parent);
467 /** coresight_validate_source - make sure a source has the right credentials
468 * @csdev: the device structure for a source.
469 * @function: the function this was called from.
471 * Assumes the coresight_mutex is held.
473 static int coresight_validate_source(struct coresight_device *csdev,
474 const char *function)
479 subtype = csdev->subtype.source_subtype;
481 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
482 dev_err(&csdev->dev, "wrong device type in %s\n", function);
486 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
487 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
488 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
495 int coresight_enable(struct coresight_device *csdev)
498 struct list_head *path;
500 mutex_lock(&coresight_mutex);
502 ret = coresight_validate_source(csdev, __func__);
509 path = coresight_build_path(csdev);
511 pr_err("building path(s) failed\n");
515 ret = coresight_enable_path(path, CS_MODE_SYSFS);
519 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
523 switch (csdev->subtype.source_subtype) {
524 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
526 * When working from sysFS it is important to keep track
527 * of the paths that were created so that they can be
528 * undone in 'coresight_disable()'. Since there can only
529 * be a single session per tracer (when working from sysFS)
530 * a per-cpu variable will do just fine.
532 cpu = source_ops(csdev)->cpu_id(csdev);
533 per_cpu(tracer_path, cpu) = path;
535 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
539 /* We can't be here */
544 mutex_unlock(&coresight_mutex);
548 coresight_disable_path(path);
551 coresight_release_path(path);
554 EXPORT_SYMBOL_GPL(coresight_enable);
556 void coresight_disable(struct coresight_device *csdev)
559 struct list_head *path = NULL;
561 mutex_lock(&coresight_mutex);
563 ret = coresight_validate_source(csdev, __func__);
570 switch (csdev->subtype.source_subtype) {
571 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
572 cpu = source_ops(csdev)->cpu_id(csdev);
573 path = per_cpu(tracer_path, cpu);
574 per_cpu(tracer_path, cpu) = NULL;
576 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
581 /* We can't be here */
585 coresight_disable_source(csdev);
586 coresight_disable_path(path);
587 coresight_release_path(path);
590 mutex_unlock(&coresight_mutex);
592 EXPORT_SYMBOL_GPL(coresight_disable);
594 static ssize_t enable_sink_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
597 struct coresight_device *csdev = to_coresight_device(dev);
599 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
602 static ssize_t enable_sink_store(struct device *dev,
603 struct device_attribute *attr,
604 const char *buf, size_t size)
608 struct coresight_device *csdev = to_coresight_device(dev);
610 ret = kstrtoul(buf, 10, &val);
615 csdev->activated = true;
617 csdev->activated = false;
622 static DEVICE_ATTR_RW(enable_sink);
624 static ssize_t enable_source_show(struct device *dev,
625 struct device_attribute *attr, char *buf)
627 struct coresight_device *csdev = to_coresight_device(dev);
629 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
632 static ssize_t enable_source_store(struct device *dev,
633 struct device_attribute *attr,
634 const char *buf, size_t size)
638 struct coresight_device *csdev = to_coresight_device(dev);
640 ret = kstrtoul(buf, 10, &val);
645 ret = coresight_enable(csdev);
649 coresight_disable(csdev);
654 static DEVICE_ATTR_RW(enable_source);
656 static struct attribute *coresight_sink_attrs[] = {
657 &dev_attr_enable_sink.attr,
660 ATTRIBUTE_GROUPS(coresight_sink);
662 static struct attribute *coresight_source_attrs[] = {
663 &dev_attr_enable_source.attr,
666 ATTRIBUTE_GROUPS(coresight_source);
668 static struct device_type coresight_dev_type[] = {
674 .groups = coresight_sink_groups,
681 .groups = coresight_sink_groups,
685 .groups = coresight_source_groups,
689 static void coresight_device_release(struct device *dev)
691 struct coresight_device *csdev = to_coresight_device(dev);
694 kfree(csdev->refcnt);
698 static int coresight_orphan_match(struct device *dev, void *data)
701 bool still_orphan = false;
702 struct coresight_device *csdev, *i_csdev;
703 struct coresight_connection *conn;
706 i_csdev = to_coresight_device(dev);
708 /* No need to check oneself */
709 if (csdev == i_csdev)
712 /* Move on to another component if no connection is orphan */
713 if (!i_csdev->orphan)
716 * Circle throuch all the connection of that component. If we find
717 * an orphan connection whose name matches @csdev, link it.
719 for (i = 0; i < i_csdev->nr_outport; i++) {
720 conn = &i_csdev->conns[i];
722 /* We have found at least one orphan connection */
723 if (conn->child_dev == NULL) {
724 /* Does it match this newly added device? */
725 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
726 conn->child_dev = csdev;
728 /* This component still has an orphan */
734 i_csdev->orphan = still_orphan;
737 * Returning '0' ensures that all known component on the
738 * bus will be checked.
743 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
746 * No need to check for a return value as orphan connection(s)
747 * are hooked-up with each newly added component.
749 bus_for_each_dev(&coresight_bustype, NULL,
750 csdev, coresight_orphan_match);
754 static int coresight_name_match(struct device *dev, void *data)
757 struct coresight_device *i_csdev;
760 i_csdev = to_coresight_device(dev);
762 if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
768 static void coresight_fixup_device_conns(struct coresight_device *csdev)
771 struct device *dev = NULL;
772 struct coresight_connection *conn;
774 for (i = 0; i < csdev->nr_outport; i++) {
775 conn = &csdev->conns[i];
776 dev = bus_find_device(&coresight_bustype, NULL,
777 (void *)conn->child_name,
778 coresight_name_match);
781 conn->child_dev = to_coresight_device(dev);
782 /* and put reference from 'bus_find_device()' */
785 csdev->orphan = true;
786 conn->child_dev = NULL;
791 static int coresight_remove_match(struct device *dev, void *data)
794 struct coresight_device *csdev, *iterator;
795 struct coresight_connection *conn;
798 iterator = to_coresight_device(dev);
800 /* No need to check oneself */
801 if (csdev == iterator)
805 * Circle throuch all the connection of that component. If we find
806 * a connection whose name matches @csdev, remove it.
808 for (i = 0; i < iterator->nr_outport; i++) {
809 conn = &iterator->conns[i];
811 if (conn->child_dev == NULL)
814 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
815 iterator->orphan = true;
816 conn->child_dev = NULL;
817 /* No need to continue */
823 * Returning '0' ensures that all known component on the
824 * bus will be checked.
829 static void coresight_remove_conns(struct coresight_device *csdev)
831 bus_for_each_dev(&coresight_bustype, NULL,
832 csdev, coresight_remove_match);
836 * coresight_timeout - loop until a bit has changed to a specific state.
837 * @addr: base address of the area of interest.
838 * @offset: address of a register, starting from @addr.
839 * @position: the position of the bit of interest.
840 * @value: the value the bit should have.
842 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
843 * TIMEOUT_US has elapsed, which ever happens first.
846 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
851 for (i = TIMEOUT_US; i > 0; i--) {
852 val = __raw_readl(addr + offset);
853 /* waiting on the bit to go from 0 to 1 */
855 if (val & BIT(position))
857 /* waiting on the bit to go from 1 to 0 */
859 if (!(val & BIT(position)))
864 * Delay is arbitrary - the specification doesn't say how long
865 * we are expected to wait. Extra check required to make sure
866 * we don't wait needlessly on the last iteration.
875 struct bus_type coresight_bustype = {
879 static int __init coresight_init(void)
881 return bus_register(&coresight_bustype);
883 postcore_initcall(coresight_init);
885 struct coresight_device *coresight_register(struct coresight_desc *desc)
891 atomic_t *refcnts = NULL;
892 struct coresight_device *csdev;
893 struct coresight_connection *conns;
895 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
898 goto err_kzalloc_csdev;
901 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
902 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
903 link_subtype = desc->subtype.link_subtype;
905 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
906 nr_refcnts = desc->pdata->nr_inport;
907 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
908 nr_refcnts = desc->pdata->nr_outport;
911 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
914 goto err_kzalloc_refcnts;
917 csdev->refcnt = refcnts;
919 csdev->nr_inport = desc->pdata->nr_inport;
920 csdev->nr_outport = desc->pdata->nr_outport;
921 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
924 goto err_kzalloc_conns;
927 for (i = 0; i < csdev->nr_outport; i++) {
928 conns[i].outport = desc->pdata->outports[i];
929 conns[i].child_name = desc->pdata->child_names[i];
930 conns[i].child_port = desc->pdata->child_ports[i];
933 csdev->conns = conns;
935 csdev->type = desc->type;
936 csdev->subtype = desc->subtype;
937 csdev->ops = desc->ops;
938 csdev->orphan = false;
940 csdev->dev.type = &coresight_dev_type[desc->type];
941 csdev->dev.groups = desc->groups;
942 csdev->dev.parent = desc->dev;
943 csdev->dev.release = coresight_device_release;
944 csdev->dev.bus = &coresight_bustype;
945 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
947 ret = device_register(&csdev->dev);
949 goto err_device_register;
951 mutex_lock(&coresight_mutex);
953 coresight_fixup_device_conns(csdev);
954 coresight_fixup_orphan_conns(csdev);
956 mutex_unlock(&coresight_mutex);
969 EXPORT_SYMBOL_GPL(coresight_register);
971 void coresight_unregister(struct coresight_device *csdev)
973 /* Remove references of that device in the topology */
974 coresight_remove_conns(csdev);
975 device_unregister(&csdev->dev);
977 EXPORT_SYMBOL_GPL(coresight_unregister);