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)
258 int refport, inport, outport;
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)
269 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
277 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
278 if (link_ops(csdev)->enable) {
279 ret = link_ops(csdev)->enable(csdev, inport, outport);
281 atomic_dec(&csdev->refcnt[refport]);
287 csdev->enable = true;
292 static void coresight_disable_link(struct coresight_device *csdev,
293 struct coresight_device *parent,
294 struct coresight_device *child)
298 int refport, inport, outport;
300 if (!parent || !child)
303 inport = coresight_find_link_inport(csdev, parent);
304 outport = coresight_find_link_outport(csdev, child);
305 link_subtype = csdev->subtype.link_subtype;
307 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
309 nr_conns = csdev->pdata->nr_inport;
310 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
312 nr_conns = csdev->pdata->nr_outport;
318 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
319 if (link_ops(csdev)->disable)
320 link_ops(csdev)->disable(csdev, inport, outport);
323 for (i = 0; i < nr_conns; i++)
324 if (atomic_read(&csdev->refcnt[i]) != 0)
327 csdev->enable = false;
330 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
334 if (!coresight_source_is_unique(csdev)) {
335 dev_warn(&csdev->dev, "traceID %d not unique\n",
336 source_ops(csdev)->trace_id(csdev));
340 if (!csdev->enable) {
341 if (source_ops(csdev)->enable) {
342 ret = source_ops(csdev)->enable(csdev, NULL, mode);
346 csdev->enable = true;
349 atomic_inc(csdev->refcnt);
355 * coresight_disable_source - Drop the reference count by 1 and disable
356 * the device if there are no users left.
358 * @csdev - The coresight device to disable
360 * Returns true if the device has been disabled.
362 static bool coresight_disable_source(struct coresight_device *csdev)
364 if (atomic_dec_return(csdev->refcnt) == 0) {
365 if (source_ops(csdev)->disable)
366 source_ops(csdev)->disable(csdev, NULL);
367 csdev->enable = false;
369 return !csdev->enable;
373 * coresight_disable_path_from : Disable components in the given path beyond
374 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
377 static void coresight_disable_path_from(struct list_head *path,
378 struct coresight_node *nd)
381 struct coresight_device *csdev, *parent, *child;
384 nd = list_first_entry(path, struct coresight_node, link);
386 list_for_each_entry_continue(nd, path, link) {
391 * ETF devices are tricky... They can be a link or a sink,
392 * depending on how they are configured. If an ETF has been
393 * "activated" it will be configured as a sink, otherwise
394 * go ahead with the link configuration.
396 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
397 type = (csdev == coresight_get_sink(path)) ?
398 CORESIGHT_DEV_TYPE_SINK :
399 CORESIGHT_DEV_TYPE_LINK;
402 case CORESIGHT_DEV_TYPE_SINK:
403 coresight_disable_sink(csdev);
405 case CORESIGHT_DEV_TYPE_SOURCE:
407 * We skip the first node in the path assuming that it
408 * is the source. So we don't expect a source device in
409 * the middle of a path.
413 case CORESIGHT_DEV_TYPE_LINK:
414 parent = list_prev_entry(nd, link)->csdev;
415 child = list_next_entry(nd, link)->csdev;
416 coresight_disable_link(csdev, parent, child);
424 void coresight_disable_path(struct list_head *path)
426 coresight_disable_path_from(path, NULL);
429 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
434 struct coresight_node *nd;
435 struct coresight_device *csdev, *parent, *child;
437 list_for_each_entry_reverse(nd, path, link) {
442 * ETF devices are tricky... They can be a link or a sink,
443 * depending on how they are configured. If an ETF has been
444 * "activated" it will be configured as a sink, otherwise
445 * go ahead with the link configuration.
447 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
448 type = (csdev == coresight_get_sink(path)) ?
449 CORESIGHT_DEV_TYPE_SINK :
450 CORESIGHT_DEV_TYPE_LINK;
453 case CORESIGHT_DEV_TYPE_SINK:
454 ret = coresight_enable_sink(csdev, mode, sink_data);
456 * Sink is the first component turned on. If we
457 * failed to enable the sink, there are no components
458 * that need disabling. Disabling the path here
459 * would mean we could disrupt an existing session.
464 case CORESIGHT_DEV_TYPE_SOURCE:
465 /* sources are enabled from either sysFS or Perf */
467 case CORESIGHT_DEV_TYPE_LINK:
468 parent = list_prev_entry(nd, link)->csdev;
469 child = list_next_entry(nd, link)->csdev;
470 ret = coresight_enable_link(csdev, parent, child);
482 coresight_disable_path_from(path, nd);
486 struct coresight_device *coresight_get_sink(struct list_head *path)
488 struct coresight_device *csdev;
493 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
494 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
495 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
501 static int coresight_enabled_sink(struct device *dev, const void *data)
503 const bool *reset = data;
504 struct coresight_device *csdev = to_coresight_device(dev);
506 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
507 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
510 * Now that we have a handle on the sink for this session,
511 * disable the sysFS "enable_sink" flag so that possible
512 * concurrent perf session that wish to use another sink don't
513 * trip on it. Doing so has no ramification for the current
517 csdev->activated = false;
526 * coresight_get_enabled_sink - returns the first enabled sink found on the bus
527 * @deactivate: Whether the 'enable_sink' flag should be reset
529 * When operated from perf the deactivate parameter should be set to 'true'.
530 * That way the "enabled_sink" flag of the sink that was selected can be reset,
531 * allowing for other concurrent perf sessions to choose a different sink.
533 * When operated from sysFS users have full control and as such the deactivate
534 * parameter should be set to 'false', hence mandating users to explicitly
537 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
539 struct device *dev = NULL;
541 dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
542 coresight_enabled_sink);
544 return dev ? to_coresight_device(dev) : NULL;
547 static int coresight_sink_by_id(struct device *dev, const void *data)
549 struct coresight_device *csdev = to_coresight_device(dev);
552 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
553 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
558 * See function etm_perf_add_symlink_sink() to know where
561 hash = (unsigned long)csdev->ea->var;
563 if ((u32)hash == *(u32 *)data)
571 * coresight_get_sink_by_id - returns the sink that matches the id
572 * @id: Id of the sink to match
574 * The name of a sink is unique, whether it is found on the AMBA bus or
575 * otherwise. As such the hash of that name can easily be used to identify
578 struct coresight_device *coresight_get_sink_by_id(u32 id)
580 struct device *dev = NULL;
582 dev = bus_find_device(&coresight_bustype, NULL, &id,
583 coresight_sink_by_id);
585 return dev ? to_coresight_device(dev) : NULL;
589 * coresight_grab_device - Power up this device and any of the helper
590 * devices connected to it for trace operation. Since the helper devices
591 * don't appear on the trace path, they should be handled along with the
594 static void coresight_grab_device(struct coresight_device *csdev)
598 for (i = 0; i < csdev->pdata->nr_outport; i++) {
599 struct coresight_device *child;
601 child = csdev->pdata->conns[i].child_dev;
602 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
603 pm_runtime_get_sync(child->dev.parent);
605 pm_runtime_get_sync(csdev->dev.parent);
609 * coresight_drop_device - Release this device and any of the helper
610 * devices connected to it.
612 static void coresight_drop_device(struct coresight_device *csdev)
616 pm_runtime_put(csdev->dev.parent);
617 for (i = 0; i < csdev->pdata->nr_outport; i++) {
618 struct coresight_device *child;
620 child = csdev->pdata->conns[i].child_dev;
621 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
622 pm_runtime_put(child->dev.parent);
627 * _coresight_build_path - recursively build a path from a @csdev to a sink.
628 * @csdev: The device to start from.
629 * @path: The list to add devices to.
631 * The tree of Coresight device is traversed until an activated sink is
632 * found. From there the sink is added to the list along with all the
633 * devices that led to that point - the end result is a list from source
634 * to sink. In that list the source is the first device and the sink the
637 static int _coresight_build_path(struct coresight_device *csdev,
638 struct coresight_device *sink,
639 struct list_head *path)
643 struct coresight_node *node;
645 /* An activated sink has been found. Enqueue the element */
649 /* Not a sink - recursively explore each port found on this element */
650 for (i = 0; i < csdev->pdata->nr_outport; i++) {
651 struct coresight_device *child_dev;
653 child_dev = csdev->pdata->conns[i].child_dev;
655 _coresight_build_path(child_dev, sink, path) == 0) {
666 * A path from this element to a sink has been found. The elements
667 * leading to the sink are already enqueued, all that is left to do
668 * is tell the PM runtime core we need this element and add a node
671 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
675 coresight_grab_device(csdev);
677 list_add(&node->link, path);
682 struct list_head *coresight_build_path(struct coresight_device *source,
683 struct coresight_device *sink)
685 struct list_head *path;
689 return ERR_PTR(-EINVAL);
691 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
693 return ERR_PTR(-ENOMEM);
695 INIT_LIST_HEAD(path);
697 rc = _coresight_build_path(source, sink, path);
707 * coresight_release_path - release a previously built path.
708 * @path: the path to release.
710 * Go through all the elements of a path and 1) removed it from the list and
711 * 2) free the memory allocated for each node.
713 void coresight_release_path(struct list_head *path)
715 struct coresight_device *csdev;
716 struct coresight_node *nd, *next;
718 list_for_each_entry_safe(nd, next, path, link) {
721 coresight_drop_device(csdev);
730 /** coresight_validate_source - make sure a source has the right credentials
731 * @csdev: the device structure for a source.
732 * @function: the function this was called from.
734 * Assumes the coresight_mutex is held.
736 static int coresight_validate_source(struct coresight_device *csdev,
737 const char *function)
742 subtype = csdev->subtype.source_subtype;
744 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
745 dev_err(&csdev->dev, "wrong device type in %s\n", function);
749 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
750 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
751 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
758 int coresight_enable(struct coresight_device *csdev)
761 struct coresight_device *sink;
762 struct list_head *path;
763 enum coresight_dev_subtype_source subtype;
765 subtype = csdev->subtype.source_subtype;
767 mutex_lock(&coresight_mutex);
769 ret = coresight_validate_source(csdev, __func__);
775 * There could be multiple applications driving the software
776 * source. So keep the refcount for each such user when the
777 * source is already enabled.
779 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
780 atomic_inc(csdev->refcnt);
785 * Search for a valid sink for this session but don't reset the
786 * "enable_sink" flag in sysFS. Users get to do that explicitly.
788 sink = coresight_get_enabled_sink(false);
794 path = coresight_build_path(csdev, sink);
796 pr_err("building path(s) failed\n");
801 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
805 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
810 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
812 * When working from sysFS it is important to keep track
813 * of the paths that were created so that they can be
814 * undone in 'coresight_disable()'. Since there can only
815 * be a single session per tracer (when working from sysFS)
816 * a per-cpu variable will do just fine.
818 cpu = source_ops(csdev)->cpu_id(csdev);
819 per_cpu(tracer_path, cpu) = path;
821 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
825 /* We can't be here */
830 mutex_unlock(&coresight_mutex);
834 coresight_disable_path(path);
837 coresight_release_path(path);
840 EXPORT_SYMBOL_GPL(coresight_enable);
842 void coresight_disable(struct coresight_device *csdev)
845 struct list_head *path = NULL;
847 mutex_lock(&coresight_mutex);
849 ret = coresight_validate_source(csdev, __func__);
853 if (!csdev->enable || !coresight_disable_source(csdev))
856 switch (csdev->subtype.source_subtype) {
857 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
858 cpu = source_ops(csdev)->cpu_id(csdev);
859 path = per_cpu(tracer_path, cpu);
860 per_cpu(tracer_path, cpu) = NULL;
862 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
867 /* We can't be here */
871 coresight_disable_path(path);
872 coresight_release_path(path);
875 mutex_unlock(&coresight_mutex);
877 EXPORT_SYMBOL_GPL(coresight_disable);
879 static ssize_t enable_sink_show(struct device *dev,
880 struct device_attribute *attr, char *buf)
882 struct coresight_device *csdev = to_coresight_device(dev);
884 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
887 static ssize_t enable_sink_store(struct device *dev,
888 struct device_attribute *attr,
889 const char *buf, size_t size)
893 struct coresight_device *csdev = to_coresight_device(dev);
895 ret = kstrtoul(buf, 10, &val);
900 csdev->activated = true;
902 csdev->activated = false;
907 static DEVICE_ATTR_RW(enable_sink);
909 static ssize_t enable_source_show(struct device *dev,
910 struct device_attribute *attr, char *buf)
912 struct coresight_device *csdev = to_coresight_device(dev);
914 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
917 static ssize_t enable_source_store(struct device *dev,
918 struct device_attribute *attr,
919 const char *buf, size_t size)
923 struct coresight_device *csdev = to_coresight_device(dev);
925 ret = kstrtoul(buf, 10, &val);
930 ret = coresight_enable(csdev);
934 coresight_disable(csdev);
939 static DEVICE_ATTR_RW(enable_source);
941 static struct attribute *coresight_sink_attrs[] = {
942 &dev_attr_enable_sink.attr,
945 ATTRIBUTE_GROUPS(coresight_sink);
947 static struct attribute *coresight_source_attrs[] = {
948 &dev_attr_enable_source.attr,
951 ATTRIBUTE_GROUPS(coresight_source);
953 static struct device_type coresight_dev_type[] = {
959 .groups = coresight_sink_groups,
966 .groups = coresight_sink_groups,
970 .groups = coresight_source_groups,
977 static void coresight_device_release(struct device *dev)
979 struct coresight_device *csdev = to_coresight_device(dev);
981 fwnode_handle_put(csdev->dev.fwnode);
982 kfree(csdev->refcnt);
986 static int coresight_orphan_match(struct device *dev, void *data)
989 bool still_orphan = false;
990 struct coresight_device *csdev, *i_csdev;
991 struct coresight_connection *conn;
994 i_csdev = to_coresight_device(dev);
996 /* No need to check oneself */
997 if (csdev == i_csdev)
1000 /* Move on to another component if no connection is orphan */
1001 if (!i_csdev->orphan)
1004 * Circle throuch all the connection of that component. If we find
1005 * an orphan connection whose name matches @csdev, link it.
1007 for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
1008 conn = &i_csdev->pdata->conns[i];
1010 /* We have found at least one orphan connection */
1011 if (conn->child_dev == NULL) {
1012 /* Does it match this newly added device? */
1013 if (conn->child_fwnode == csdev->dev.fwnode)
1014 conn->child_dev = csdev;
1016 /* This component still has an orphan */
1017 still_orphan = true;
1021 i_csdev->orphan = still_orphan;
1024 * Returning '0' ensures that all known component on the
1025 * bus will be checked.
1030 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
1033 * No need to check for a return value as orphan connection(s)
1034 * are hooked-up with each newly added component.
1036 bus_for_each_dev(&coresight_bustype, NULL,
1037 csdev, coresight_orphan_match);
1041 static void coresight_fixup_device_conns(struct coresight_device *csdev)
1045 for (i = 0; i < csdev->pdata->nr_outport; i++) {
1046 struct coresight_connection *conn = &csdev->pdata->conns[i];
1047 struct device *dev = NULL;
1049 dev = bus_find_device_by_fwnode(&coresight_bustype, conn->child_fwnode);
1051 conn->child_dev = to_coresight_device(dev);
1052 /* and put reference from 'bus_find_device()' */
1055 csdev->orphan = true;
1056 conn->child_dev = NULL;
1061 static int coresight_remove_match(struct device *dev, void *data)
1064 struct coresight_device *csdev, *iterator;
1065 struct coresight_connection *conn;
1068 iterator = to_coresight_device(dev);
1070 /* No need to check oneself */
1071 if (csdev == iterator)
1075 * Circle throuch all the connection of that component. If we find
1076 * a connection whose name matches @csdev, remove it.
1078 for (i = 0; i < iterator->pdata->nr_outport; i++) {
1079 conn = &iterator->pdata->conns[i];
1081 if (conn->child_dev == NULL)
1084 if (csdev->dev.fwnode == conn->child_fwnode) {
1085 iterator->orphan = true;
1086 conn->child_dev = NULL;
1088 * Drop the reference to the handle for the remote
1089 * device acquired in parsing the connections from
1092 fwnode_handle_put(conn->child_fwnode);
1093 /* No need to continue */
1099 * Returning '0' ensures that all known component on the
1100 * bus will be checked.
1106 * coresight_remove_conns - Remove references to this given devices
1107 * from the connections of other devices.
1109 static void coresight_remove_conns(struct coresight_device *csdev)
1112 * Another device will point to this device only if there is
1113 * an output port connected to this one. i.e, if the device
1114 * doesn't have at least one input port, there is no point
1115 * in searching all the devices.
1117 if (csdev->pdata->nr_inport)
1118 bus_for_each_dev(&coresight_bustype, NULL,
1119 csdev, coresight_remove_match);
1123 * coresight_timeout - loop until a bit has changed to a specific state.
1124 * @addr: base address of the area of interest.
1125 * @offset: address of a register, starting from @addr.
1126 * @position: the position of the bit of interest.
1127 * @value: the value the bit should have.
1129 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1130 * TIMEOUT_US has elapsed, which ever happens first.
1133 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
1138 for (i = TIMEOUT_US; i > 0; i--) {
1139 val = __raw_readl(addr + offset);
1140 /* waiting on the bit to go from 0 to 1 */
1142 if (val & BIT(position))
1144 /* waiting on the bit to go from 1 to 0 */
1146 if (!(val & BIT(position)))
1151 * Delay is arbitrary - the specification doesn't say how long
1152 * we are expected to wait. Extra check required to make sure
1153 * we don't wait needlessly on the last iteration.
1162 struct bus_type coresight_bustype = {
1163 .name = "coresight",
1166 static int __init coresight_init(void)
1168 return bus_register(&coresight_bustype);
1170 postcore_initcall(coresight_init);
1173 * coresight_release_platform_data: Release references to the devices connected
1174 * to the output port of this device.
1176 void coresight_release_platform_data(struct coresight_platform_data *pdata)
1180 for (i = 0; i < pdata->nr_outport; i++) {
1181 if (pdata->conns[i].child_fwnode) {
1182 fwnode_handle_put(pdata->conns[i].child_fwnode);
1183 pdata->conns[i].child_fwnode = NULL;
1188 struct coresight_device *coresight_register(struct coresight_desc *desc)
1193 atomic_t *refcnts = NULL;
1194 struct coresight_device *csdev;
1196 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1202 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1203 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1204 link_subtype = desc->subtype.link_subtype;
1206 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1207 nr_refcnts = desc->pdata->nr_inport;
1208 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1209 nr_refcnts = desc->pdata->nr_outport;
1212 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1215 goto err_free_csdev;
1218 csdev->refcnt = refcnts;
1220 csdev->pdata = desc->pdata;
1222 csdev->type = desc->type;
1223 csdev->subtype = desc->subtype;
1224 csdev->ops = desc->ops;
1225 csdev->orphan = false;
1227 csdev->dev.type = &coresight_dev_type[desc->type];
1228 csdev->dev.groups = desc->groups;
1229 csdev->dev.parent = desc->dev;
1230 csdev->dev.release = coresight_device_release;
1231 csdev->dev.bus = &coresight_bustype;
1233 * Hold the reference to our parent device. This will be
1234 * dropped only in coresight_device_release().
1236 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1237 dev_set_name(&csdev->dev, "%s", desc->name);
1239 ret = device_register(&csdev->dev);
1241 put_device(&csdev->dev);
1243 * All resources are free'd explicitly via
1244 * coresight_device_release(), triggered from put_device().
1249 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1250 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1251 ret = etm_perf_add_symlink_sink(csdev);
1254 device_unregister(&csdev->dev);
1256 * As with the above, all resources are free'd
1257 * explicitly via coresight_device_release() triggered
1258 * from put_device(), which is in turn called from
1259 * function device_unregister().
1265 mutex_lock(&coresight_mutex);
1267 coresight_fixup_device_conns(csdev);
1268 coresight_fixup_orphan_conns(csdev);
1270 mutex_unlock(&coresight_mutex);
1277 /* Cleanup the connection information */
1278 coresight_release_platform_data(desc->pdata);
1279 return ERR_PTR(ret);
1281 EXPORT_SYMBOL_GPL(coresight_register);
1283 void coresight_unregister(struct coresight_device *csdev)
1285 etm_perf_del_symlink_sink(csdev);
1286 /* Remove references of that device in the topology */
1287 coresight_remove_conns(csdev);
1288 coresight_release_platform_data(csdev->pdata);
1289 device_unregister(&csdev->dev);
1291 EXPORT_SYMBOL_GPL(coresight_unregister);
1295 * coresight_search_device_idx - Search the fwnode handle of a device
1296 * in the given dev_idx list. Must be called with the coresight_mutex held.
1298 * Returns the index of the entry, when found. Otherwise, -ENOENT.
1300 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1301 struct fwnode_handle *fwnode)
1305 for (i = 0; i < dict->nr_idx; i++)
1306 if (dict->fwnode_list[i] == fwnode)
1312 * coresight_alloc_device_name - Get an index for a given device in the
1313 * device index list specific to a driver. An index is allocated for a
1314 * device and is tracked with the fwnode_handle to prevent allocating
1315 * duplicate indices for the same device (e.g, if we defer probing of
1316 * a device due to dependencies), in case the index is requested again.
1318 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1323 struct fwnode_handle **list;
1325 mutex_lock(&coresight_mutex);
1327 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1329 /* Make space for the new entry */
1331 list = krealloc(dict->fwnode_list,
1332 (idx + 1) * sizeof(*dict->fwnode_list),
1334 if (ZERO_OR_NULL_PTR(list)) {
1339 list[idx] = dev_fwnode(dev);
1340 dict->fwnode_list = list;
1341 dict->nr_idx = idx + 1;
1344 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1346 mutex_unlock(&coresight_mutex);
1349 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);