1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/types.h>
9 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/stringhash.h>
15 #include <linux/mutex.h>
16 #include <linux/clk.h>
17 #include <linux/coresight.h>
18 #include <linux/of_platform.h>
19 #include <linux/delay.h>
20 #include <linux/pm_runtime.h>
22 #include "coresight-etm-perf.h"
23 #include "coresight-priv.h"
25 static DEFINE_MUTEX(coresight_mutex);
28 * struct coresight_node - elements of a path, from source to sink
29 * @csdev: Address of an element.
30 * @link: hook to the list.
32 struct coresight_node {
33 struct coresight_device *csdev;
34 struct list_head link;
38 * When operating Coresight drivers from the sysFS interface, only a single
39 * path can exist from a tracer (associated to a CPU) to a sink.
41 static DEFINE_PER_CPU(struct list_head *, tracer_path);
44 * As of this writing only a single STM can be found in CS topologies. Since
45 * there is no way to know if we'll ever see more and what kind of
46 * configuration they will enact, for the time being only define a single path
49 static struct list_head *stm_path;
52 * When losing synchronisation a new barrier packet needs to be inserted at the
53 * beginning of the data collected in a buffer. That way the decoder knows that
54 * it needs to look for another sync sequence.
56 const u32 barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
58 static int coresight_id_match(struct device *dev, void *data)
60 int trace_id, i_trace_id;
61 struct coresight_device *csdev, *i_csdev;
64 i_csdev = to_coresight_device(dev);
67 * No need to care about oneself and components that are not
68 * sources or not enabled
70 if (i_csdev == csdev || !i_csdev->enable ||
71 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
74 /* Get the source ID for both compoment */
75 trace_id = source_ops(csdev)->trace_id(csdev);
76 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
78 /* All you need is one */
79 if (trace_id == i_trace_id)
85 static int coresight_source_is_unique(struct coresight_device *csdev)
87 int trace_id = source_ops(csdev)->trace_id(csdev);
89 /* this shouldn't happen */
93 return !bus_for_each_dev(&coresight_bustype, NULL,
94 csdev, coresight_id_match);
97 static int coresight_find_link_inport(struct coresight_device *csdev,
98 struct coresight_device *parent)
101 struct coresight_connection *conn;
103 for (i = 0; i < parent->pdata->nr_outport; i++) {
104 conn = &parent->pdata->conns[i];
105 if (conn->child_dev == csdev)
106 return conn->child_port;
109 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
110 dev_name(&parent->dev), dev_name(&csdev->dev));
115 static int coresight_find_link_outport(struct coresight_device *csdev,
116 struct coresight_device *child)
119 struct coresight_connection *conn;
121 for (i = 0; i < csdev->pdata->nr_outport; i++) {
122 conn = &csdev->pdata->conns[i];
123 if (conn->child_dev == child)
124 return conn->outport;
127 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
128 dev_name(&csdev->dev), dev_name(&child->dev));
133 static inline u32 coresight_read_claim_tags(void __iomem *base)
135 return readl_relaxed(base + CORESIGHT_CLAIMCLR);
138 static inline bool coresight_is_claimed_self_hosted(void __iomem *base)
140 return coresight_read_claim_tags(base) == CORESIGHT_CLAIM_SELF_HOSTED;
143 static inline bool coresight_is_claimed_any(void __iomem *base)
145 return coresight_read_claim_tags(base) != 0;
148 static inline void coresight_set_claim_tags(void __iomem *base)
150 writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMSET);
154 static inline void coresight_clear_claim_tags(void __iomem *base)
156 writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMCLR);
161 * coresight_claim_device_unlocked : Claim the device for self-hosted usage
162 * to prevent an external tool from touching this device. As per PSCI
163 * standards, section "Preserving the execution context" => "Debug and Trace
164 * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
165 * DBGCLAIM[0] is reserved for external tools.
167 * Called with CS_UNLOCKed for the component.
168 * Returns : 0 on success
170 int coresight_claim_device_unlocked(void __iomem *base)
172 if (coresight_is_claimed_any(base))
175 coresight_set_claim_tags(base);
176 if (coresight_is_claimed_self_hosted(base))
178 /* There was a race setting the tags, clean up and fail */
179 coresight_clear_claim_tags(base);
183 int coresight_claim_device(void __iomem *base)
188 rc = coresight_claim_device_unlocked(base);
195 * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
196 * Called with CS_UNLOCKed for the component.
198 void coresight_disclaim_device_unlocked(void __iomem *base)
201 if (coresight_is_claimed_self_hosted(base))
202 coresight_clear_claim_tags(base);
205 * The external agent may have not honoured our claim
206 * and has manipulated it. Or something else has seriously
207 * gone wrong in our driver.
212 void coresight_disclaim_device(void __iomem *base)
215 coresight_disclaim_device_unlocked(base);
219 static int coresight_enable_sink(struct coresight_device *csdev,
220 u32 mode, void *data)
225 * We need to make sure the "new" session is compatible with the
226 * existing "mode" of operation.
228 if (!sink_ops(csdev)->enable)
231 ret = sink_ops(csdev)->enable(csdev, mode, data);
234 csdev->enable = true;
239 static void coresight_disable_sink(struct coresight_device *csdev)
243 if (!sink_ops(csdev)->disable)
246 ret = sink_ops(csdev)->disable(csdev);
249 csdev->enable = false;
252 static int coresight_enable_link(struct coresight_device *csdev,
253 struct coresight_device *parent,
254 struct coresight_device *child)
260 if (!parent || !child)
263 inport = coresight_find_link_inport(csdev, parent);
264 outport = coresight_find_link_outport(csdev, child);
265 link_subtype = csdev->subtype.link_subtype;
267 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && inport < 0)
269 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && outport < 0)
272 if (link_ops(csdev)->enable)
273 ret = link_ops(csdev)->enable(csdev, inport, outport);
275 csdev->enable = true;
280 static void coresight_disable_link(struct coresight_device *csdev,
281 struct coresight_device *parent,
282 struct coresight_device *child)
288 if (!parent || !child)
291 inport = coresight_find_link_inport(csdev, parent);
292 outport = coresight_find_link_outport(csdev, child);
293 link_subtype = csdev->subtype.link_subtype;
295 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
296 nr_conns = csdev->pdata->nr_inport;
297 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
298 nr_conns = csdev->pdata->nr_outport;
303 if (link_ops(csdev)->disable)
304 link_ops(csdev)->disable(csdev, inport, outport);
306 for (i = 0; i < nr_conns; i++)
307 if (atomic_read(&csdev->refcnt[i]) != 0)
310 csdev->enable = false;
313 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
317 if (!coresight_source_is_unique(csdev)) {
318 dev_warn(&csdev->dev, "traceID %d not unique\n",
319 source_ops(csdev)->trace_id(csdev));
323 if (!csdev->enable) {
324 if (source_ops(csdev)->enable) {
325 ret = source_ops(csdev)->enable(csdev, NULL, mode);
329 csdev->enable = true;
332 atomic_inc(csdev->refcnt);
338 * coresight_disable_source - Drop the reference count by 1 and disable
339 * the device if there are no users left.
341 * @csdev - The coresight device to disable
343 * Returns true if the device has been disabled.
345 static bool coresight_disable_source(struct coresight_device *csdev)
347 if (atomic_dec_return(csdev->refcnt) == 0) {
348 if (source_ops(csdev)->disable)
349 source_ops(csdev)->disable(csdev, NULL);
350 csdev->enable = false;
352 return !csdev->enable;
356 * coresight_disable_path_from : Disable components in the given path beyond
357 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
360 static void coresight_disable_path_from(struct list_head *path,
361 struct coresight_node *nd)
364 struct coresight_device *csdev, *parent, *child;
367 nd = list_first_entry(path, struct coresight_node, link);
369 list_for_each_entry_continue(nd, path, link) {
374 * ETF devices are tricky... They can be a link or a sink,
375 * depending on how they are configured. If an ETF has been
376 * "activated" it will be configured as a sink, otherwise
377 * go ahead with the link configuration.
379 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
380 type = (csdev == coresight_get_sink(path)) ?
381 CORESIGHT_DEV_TYPE_SINK :
382 CORESIGHT_DEV_TYPE_LINK;
385 case CORESIGHT_DEV_TYPE_SINK:
386 coresight_disable_sink(csdev);
388 case CORESIGHT_DEV_TYPE_SOURCE:
390 * We skip the first node in the path assuming that it
391 * is the source. So we don't expect a source device in
392 * the middle of a path.
396 case CORESIGHT_DEV_TYPE_LINK:
397 parent = list_prev_entry(nd, link)->csdev;
398 child = list_next_entry(nd, link)->csdev;
399 coresight_disable_link(csdev, parent, child);
407 void coresight_disable_path(struct list_head *path)
409 coresight_disable_path_from(path, NULL);
412 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
417 struct coresight_node *nd;
418 struct coresight_device *csdev, *parent, *child;
420 list_for_each_entry_reverse(nd, path, link) {
425 * ETF devices are tricky... They can be a link or a sink,
426 * depending on how they are configured. If an ETF has been
427 * "activated" it will be configured as a sink, otherwise
428 * go ahead with the link configuration.
430 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
431 type = (csdev == coresight_get_sink(path)) ?
432 CORESIGHT_DEV_TYPE_SINK :
433 CORESIGHT_DEV_TYPE_LINK;
436 case CORESIGHT_DEV_TYPE_SINK:
437 ret = coresight_enable_sink(csdev, mode, sink_data);
439 * Sink is the first component turned on. If we
440 * failed to enable the sink, there are no components
441 * that need disabling. Disabling the path here
442 * would mean we could disrupt an existing session.
447 case CORESIGHT_DEV_TYPE_SOURCE:
448 /* sources are enabled from either sysFS or Perf */
450 case CORESIGHT_DEV_TYPE_LINK:
451 parent = list_prev_entry(nd, link)->csdev;
452 child = list_next_entry(nd, link)->csdev;
453 ret = coresight_enable_link(csdev, parent, child);
465 coresight_disable_path_from(path, nd);
469 struct coresight_device *coresight_get_sink(struct list_head *path)
471 struct coresight_device *csdev;
476 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
477 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
478 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
484 static int coresight_enabled_sink(struct device *dev, const void *data)
486 const bool *reset = data;
487 struct coresight_device *csdev = to_coresight_device(dev);
489 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
490 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
493 * Now that we have a handle on the sink for this session,
494 * disable the sysFS "enable_sink" flag so that possible
495 * concurrent perf session that wish to use another sink don't
496 * trip on it. Doing so has no ramification for the current
500 csdev->activated = false;
509 * coresight_get_enabled_sink - returns the first enabled sink found on the bus
510 * @deactivate: Whether the 'enable_sink' flag should be reset
512 * When operated from perf the deactivate parameter should be set to 'true'.
513 * That way the "enabled_sink" flag of the sink that was selected can be reset,
514 * allowing for other concurrent perf sessions to choose a different sink.
516 * When operated from sysFS users have full control and as such the deactivate
517 * parameter should be set to 'false', hence mandating users to explicitly
520 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
522 struct device *dev = NULL;
524 dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
525 coresight_enabled_sink);
527 return dev ? to_coresight_device(dev) : NULL;
530 static int coresight_sink_by_id(struct device *dev, const void *data)
532 struct coresight_device *csdev = to_coresight_device(dev);
535 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
536 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
541 * See function etm_perf_add_symlink_sink() to know where
544 hash = (unsigned long)csdev->ea->var;
546 if ((u32)hash == *(u32 *)data)
554 * coresight_get_sink_by_id - returns the sink that matches the id
555 * @id: Id of the sink to match
557 * The name of a sink is unique, whether it is found on the AMBA bus or
558 * otherwise. As such the hash of that name can easily be used to identify
561 struct coresight_device *coresight_get_sink_by_id(u32 id)
563 struct device *dev = NULL;
565 dev = bus_find_device(&coresight_bustype, NULL, &id,
566 coresight_sink_by_id);
568 return dev ? to_coresight_device(dev) : NULL;
572 * coresight_grab_device - Power up this device and any of the helper
573 * devices connected to it for trace operation. Since the helper devices
574 * don't appear on the trace path, they should be handled along with the
577 static void coresight_grab_device(struct coresight_device *csdev)
581 for (i = 0; i < csdev->pdata->nr_outport; i++) {
582 struct coresight_device *child;
584 child = csdev->pdata->conns[i].child_dev;
585 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
586 pm_runtime_get_sync(child->dev.parent);
588 pm_runtime_get_sync(csdev->dev.parent);
592 * coresight_drop_device - Release this device and any of the helper
593 * devices connected to it.
595 static void coresight_drop_device(struct coresight_device *csdev)
599 pm_runtime_put(csdev->dev.parent);
600 for (i = 0; i < csdev->pdata->nr_outport; i++) {
601 struct coresight_device *child;
603 child = csdev->pdata->conns[i].child_dev;
604 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
605 pm_runtime_put(child->dev.parent);
610 * _coresight_build_path - recursively build a path from a @csdev to a sink.
611 * @csdev: The device to start from.
612 * @path: The list to add devices to.
614 * The tree of Coresight device is traversed until an activated sink is
615 * found. From there the sink is added to the list along with all the
616 * devices that led to that point - the end result is a list from source
617 * to sink. In that list the source is the first device and the sink the
620 static int _coresight_build_path(struct coresight_device *csdev,
621 struct coresight_device *sink,
622 struct list_head *path)
626 struct coresight_node *node;
628 /* An activated sink has been found. Enqueue the element */
632 /* Not a sink - recursively explore each port found on this element */
633 for (i = 0; i < csdev->pdata->nr_outport; i++) {
634 struct coresight_device *child_dev;
636 child_dev = csdev->pdata->conns[i].child_dev;
638 _coresight_build_path(child_dev, sink, path) == 0) {
649 * A path from this element to a sink has been found. The elements
650 * leading to the sink are already enqueued, all that is left to do
651 * is tell the PM runtime core we need this element and add a node
654 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
658 coresight_grab_device(csdev);
660 list_add(&node->link, path);
665 struct list_head *coresight_build_path(struct coresight_device *source,
666 struct coresight_device *sink)
668 struct list_head *path;
672 return ERR_PTR(-EINVAL);
674 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
676 return ERR_PTR(-ENOMEM);
678 INIT_LIST_HEAD(path);
680 rc = _coresight_build_path(source, sink, path);
690 * coresight_release_path - release a previously built path.
691 * @path: the path to release.
693 * Go through all the elements of a path and 1) removed it from the list and
694 * 2) free the memory allocated for each node.
696 void coresight_release_path(struct list_head *path)
698 struct coresight_device *csdev;
699 struct coresight_node *nd, *next;
701 list_for_each_entry_safe(nd, next, path, link) {
704 coresight_drop_device(csdev);
713 /** coresight_validate_source - make sure a source has the right credentials
714 * @csdev: the device structure for a source.
715 * @function: the function this was called from.
717 * Assumes the coresight_mutex is held.
719 static int coresight_validate_source(struct coresight_device *csdev,
720 const char *function)
725 subtype = csdev->subtype.source_subtype;
727 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
728 dev_err(&csdev->dev, "wrong device type in %s\n", function);
732 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
733 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
734 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
741 int coresight_enable(struct coresight_device *csdev)
744 struct coresight_device *sink;
745 struct list_head *path;
746 enum coresight_dev_subtype_source subtype;
748 subtype = csdev->subtype.source_subtype;
750 mutex_lock(&coresight_mutex);
752 ret = coresight_validate_source(csdev, __func__);
758 * There could be multiple applications driving the software
759 * source. So keep the refcount for each such user when the
760 * source is already enabled.
762 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
763 atomic_inc(csdev->refcnt);
768 * Search for a valid sink for this session but don't reset the
769 * "enable_sink" flag in sysFS. Users get to do that explicitly.
771 sink = coresight_get_enabled_sink(false);
777 path = coresight_build_path(csdev, sink);
779 pr_err("building path(s) failed\n");
784 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
788 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
793 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
795 * When working from sysFS it is important to keep track
796 * of the paths that were created so that they can be
797 * undone in 'coresight_disable()'. Since there can only
798 * be a single session per tracer (when working from sysFS)
799 * a per-cpu variable will do just fine.
801 cpu = source_ops(csdev)->cpu_id(csdev);
802 per_cpu(tracer_path, cpu) = path;
804 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
808 /* We can't be here */
813 mutex_unlock(&coresight_mutex);
817 coresight_disable_path(path);
820 coresight_release_path(path);
823 EXPORT_SYMBOL_GPL(coresight_enable);
825 void coresight_disable(struct coresight_device *csdev)
828 struct list_head *path = NULL;
830 mutex_lock(&coresight_mutex);
832 ret = coresight_validate_source(csdev, __func__);
836 if (!csdev->enable || !coresight_disable_source(csdev))
839 switch (csdev->subtype.source_subtype) {
840 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
841 cpu = source_ops(csdev)->cpu_id(csdev);
842 path = per_cpu(tracer_path, cpu);
843 per_cpu(tracer_path, cpu) = NULL;
845 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
850 /* We can't be here */
854 coresight_disable_path(path);
855 coresight_release_path(path);
858 mutex_unlock(&coresight_mutex);
860 EXPORT_SYMBOL_GPL(coresight_disable);
862 static ssize_t enable_sink_show(struct device *dev,
863 struct device_attribute *attr, char *buf)
865 struct coresight_device *csdev = to_coresight_device(dev);
867 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
870 static ssize_t enable_sink_store(struct device *dev,
871 struct device_attribute *attr,
872 const char *buf, size_t size)
876 struct coresight_device *csdev = to_coresight_device(dev);
878 ret = kstrtoul(buf, 10, &val);
883 csdev->activated = true;
885 csdev->activated = false;
890 static DEVICE_ATTR_RW(enable_sink);
892 static ssize_t enable_source_show(struct device *dev,
893 struct device_attribute *attr, char *buf)
895 struct coresight_device *csdev = to_coresight_device(dev);
897 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
900 static ssize_t enable_source_store(struct device *dev,
901 struct device_attribute *attr,
902 const char *buf, size_t size)
906 struct coresight_device *csdev = to_coresight_device(dev);
908 ret = kstrtoul(buf, 10, &val);
913 ret = coresight_enable(csdev);
917 coresight_disable(csdev);
922 static DEVICE_ATTR_RW(enable_source);
924 static struct attribute *coresight_sink_attrs[] = {
925 &dev_attr_enable_sink.attr,
928 ATTRIBUTE_GROUPS(coresight_sink);
930 static struct attribute *coresight_source_attrs[] = {
931 &dev_attr_enable_source.attr,
934 ATTRIBUTE_GROUPS(coresight_source);
936 static struct device_type coresight_dev_type[] = {
942 .groups = coresight_sink_groups,
949 .groups = coresight_sink_groups,
953 .groups = coresight_source_groups,
960 static void coresight_device_release(struct device *dev)
962 struct coresight_device *csdev = to_coresight_device(dev);
964 fwnode_handle_put(csdev->dev.fwnode);
965 kfree(csdev->refcnt);
969 static int coresight_orphan_match(struct device *dev, void *data)
972 bool still_orphan = false;
973 struct coresight_device *csdev, *i_csdev;
974 struct coresight_connection *conn;
977 i_csdev = to_coresight_device(dev);
979 /* No need to check oneself */
980 if (csdev == i_csdev)
983 /* Move on to another component if no connection is orphan */
984 if (!i_csdev->orphan)
987 * Circle throuch all the connection of that component. If we find
988 * an orphan connection whose name matches @csdev, link it.
990 for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
991 conn = &i_csdev->pdata->conns[i];
993 /* We have found at least one orphan connection */
994 if (conn->child_dev == NULL) {
995 /* Does it match this newly added device? */
996 if (conn->child_fwnode == csdev->dev.fwnode)
997 conn->child_dev = csdev;
999 /* This component still has an orphan */
1000 still_orphan = true;
1004 i_csdev->orphan = still_orphan;
1007 * Returning '0' ensures that all known component on the
1008 * bus will be checked.
1013 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
1016 * No need to check for a return value as orphan connection(s)
1017 * are hooked-up with each newly added component.
1019 bus_for_each_dev(&coresight_bustype, NULL,
1020 csdev, coresight_orphan_match);
1024 static void coresight_fixup_device_conns(struct coresight_device *csdev)
1028 for (i = 0; i < csdev->pdata->nr_outport; i++) {
1029 struct coresight_connection *conn = &csdev->pdata->conns[i];
1030 struct device *dev = NULL;
1032 dev = bus_find_device_by_fwnode(&coresight_bustype, conn->child_fwnode);
1034 conn->child_dev = to_coresight_device(dev);
1035 /* and put reference from 'bus_find_device()' */
1038 csdev->orphan = true;
1039 conn->child_dev = NULL;
1044 static int coresight_remove_match(struct device *dev, void *data)
1047 struct coresight_device *csdev, *iterator;
1048 struct coresight_connection *conn;
1051 iterator = to_coresight_device(dev);
1053 /* No need to check oneself */
1054 if (csdev == iterator)
1058 * Circle throuch all the connection of that component. If we find
1059 * a connection whose name matches @csdev, remove it.
1061 for (i = 0; i < iterator->pdata->nr_outport; i++) {
1062 conn = &iterator->pdata->conns[i];
1064 if (conn->child_dev == NULL)
1067 if (csdev->dev.fwnode == conn->child_fwnode) {
1068 iterator->orphan = true;
1069 conn->child_dev = NULL;
1071 * Drop the reference to the handle for the remote
1072 * device acquired in parsing the connections from
1075 fwnode_handle_put(conn->child_fwnode);
1076 /* No need to continue */
1082 * Returning '0' ensures that all known component on the
1083 * bus will be checked.
1089 * coresight_remove_conns - Remove references to this given devices
1090 * from the connections of other devices.
1092 static void coresight_remove_conns(struct coresight_device *csdev)
1095 * Another device will point to this device only if there is
1096 * an output port connected to this one. i.e, if the device
1097 * doesn't have at least one input port, there is no point
1098 * in searching all the devices.
1100 if (csdev->pdata->nr_inport)
1101 bus_for_each_dev(&coresight_bustype, NULL,
1102 csdev, coresight_remove_match);
1106 * coresight_timeout - loop until a bit has changed to a specific state.
1107 * @addr: base address of the area of interest.
1108 * @offset: address of a register, starting from @addr.
1109 * @position: the position of the bit of interest.
1110 * @value: the value the bit should have.
1112 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1113 * TIMEOUT_US has elapsed, which ever happens first.
1116 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
1121 for (i = TIMEOUT_US; i > 0; i--) {
1122 val = __raw_readl(addr + offset);
1123 /* waiting on the bit to go from 0 to 1 */
1125 if (val & BIT(position))
1127 /* waiting on the bit to go from 1 to 0 */
1129 if (!(val & BIT(position)))
1134 * Delay is arbitrary - the specification doesn't say how long
1135 * we are expected to wait. Extra check required to make sure
1136 * we don't wait needlessly on the last iteration.
1145 struct bus_type coresight_bustype = {
1146 .name = "coresight",
1149 static int __init coresight_init(void)
1151 return bus_register(&coresight_bustype);
1153 postcore_initcall(coresight_init);
1156 * coresight_release_platform_data: Release references to the devices connected
1157 * to the output port of this device.
1159 void coresight_release_platform_data(struct coresight_platform_data *pdata)
1163 for (i = 0; i < pdata->nr_outport; i++) {
1164 if (pdata->conns[i].child_fwnode) {
1165 fwnode_handle_put(pdata->conns[i].child_fwnode);
1166 pdata->conns[i].child_fwnode = NULL;
1171 struct coresight_device *coresight_register(struct coresight_desc *desc)
1176 atomic_t *refcnts = NULL;
1177 struct coresight_device *csdev;
1179 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1185 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1186 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1187 link_subtype = desc->subtype.link_subtype;
1189 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1190 nr_refcnts = desc->pdata->nr_inport;
1191 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1192 nr_refcnts = desc->pdata->nr_outport;
1195 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1198 goto err_free_csdev;
1201 csdev->refcnt = refcnts;
1203 csdev->pdata = desc->pdata;
1205 csdev->type = desc->type;
1206 csdev->subtype = desc->subtype;
1207 csdev->ops = desc->ops;
1208 csdev->orphan = false;
1210 csdev->dev.type = &coresight_dev_type[desc->type];
1211 csdev->dev.groups = desc->groups;
1212 csdev->dev.parent = desc->dev;
1213 csdev->dev.release = coresight_device_release;
1214 csdev->dev.bus = &coresight_bustype;
1216 * Hold the reference to our parent device. This will be
1217 * dropped only in coresight_device_release().
1219 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1220 dev_set_name(&csdev->dev, "%s", desc->name);
1222 ret = device_register(&csdev->dev);
1224 put_device(&csdev->dev);
1226 * All resources are free'd explicitly via
1227 * coresight_device_release(), triggered from put_device().
1232 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1233 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1234 ret = etm_perf_add_symlink_sink(csdev);
1237 device_unregister(&csdev->dev);
1239 * As with the above, all resources are free'd
1240 * explicitly via coresight_device_release() triggered
1241 * from put_device(), which is in turn called from
1242 * function device_unregister().
1248 mutex_lock(&coresight_mutex);
1250 coresight_fixup_device_conns(csdev);
1251 coresight_fixup_orphan_conns(csdev);
1253 mutex_unlock(&coresight_mutex);
1260 /* Cleanup the connection information */
1261 coresight_release_platform_data(desc->pdata);
1262 return ERR_PTR(ret);
1264 EXPORT_SYMBOL_GPL(coresight_register);
1266 void coresight_unregister(struct coresight_device *csdev)
1268 etm_perf_del_symlink_sink(csdev);
1269 /* Remove references of that device in the topology */
1270 coresight_remove_conns(csdev);
1271 coresight_release_platform_data(csdev->pdata);
1272 device_unregister(&csdev->dev);
1274 EXPORT_SYMBOL_GPL(coresight_unregister);
1278 * coresight_search_device_idx - Search the fwnode handle of a device
1279 * in the given dev_idx list. Must be called with the coresight_mutex held.
1281 * Returns the index of the entry, when found. Otherwise, -ENOENT.
1283 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1284 struct fwnode_handle *fwnode)
1288 for (i = 0; i < dict->nr_idx; i++)
1289 if (dict->fwnode_list[i] == fwnode)
1294 bool coresight_loses_context_with_cpu(struct device *dev)
1296 return fwnode_property_present(dev_fwnode(dev),
1297 "arm,coresight-loses-context-with-cpu");
1301 * coresight_alloc_device_name - Get an index for a given device in the
1302 * device index list specific to a driver. An index is allocated for a
1303 * device and is tracked with the fwnode_handle to prevent allocating
1304 * duplicate indices for the same device (e.g, if we defer probing of
1305 * a device due to dependencies), in case the index is requested again.
1307 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1312 struct fwnode_handle **list;
1314 mutex_lock(&coresight_mutex);
1316 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1318 /* Make space for the new entry */
1320 list = krealloc(dict->fwnode_list,
1321 (idx + 1) * sizeof(*dict->fwnode_list),
1323 if (ZERO_OR_NULL_PTR(list)) {
1328 list[idx] = dev_fwnode(dev);
1329 dict->fwnode_list = list;
1330 dict->nr_idx = idx + 1;
1333 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1335 mutex_unlock(&coresight_mutex);
1338 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);