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->nr_outport; i++) {
104 conn = &parent->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->nr_outport; i++) {
122 conn = &csdev->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) {
229 ret = sink_ops(csdev)->enable(csdev, mode, data);
232 csdev->enable = true;
235 atomic_inc(csdev->refcnt);
240 static void coresight_disable_sink(struct coresight_device *csdev)
242 if (atomic_dec_return(csdev->refcnt) == 0) {
243 if (sink_ops(csdev)->disable) {
244 sink_ops(csdev)->disable(csdev);
245 csdev->enable = false;
250 static int coresight_enable_link(struct coresight_device *csdev,
251 struct coresight_device *parent,
252 struct coresight_device *child)
256 int refport, inport, outport;
258 if (!parent || !child)
261 inport = coresight_find_link_inport(csdev, parent);
262 outport = coresight_find_link_outport(csdev, child);
263 link_subtype = csdev->subtype.link_subtype;
265 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
267 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
275 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
276 if (link_ops(csdev)->enable) {
277 ret = link_ops(csdev)->enable(csdev, inport, outport);
279 atomic_dec(&csdev->refcnt[refport]);
285 csdev->enable = true;
290 static void coresight_disable_link(struct coresight_device *csdev,
291 struct coresight_device *parent,
292 struct coresight_device *child)
296 int refport, inport, outport;
298 if (!parent || !child)
301 inport = coresight_find_link_inport(csdev, parent);
302 outport = coresight_find_link_outport(csdev, child);
303 link_subtype = csdev->subtype.link_subtype;
305 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
307 nr_conns = csdev->nr_inport;
308 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
310 nr_conns = csdev->nr_outport;
316 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
317 if (link_ops(csdev)->disable)
318 link_ops(csdev)->disable(csdev, inport, outport);
321 for (i = 0; i < nr_conns; i++)
322 if (atomic_read(&csdev->refcnt[i]) != 0)
325 csdev->enable = false;
328 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
332 if (!coresight_source_is_unique(csdev)) {
333 dev_warn(&csdev->dev, "traceID %d not unique\n",
334 source_ops(csdev)->trace_id(csdev));
338 if (!csdev->enable) {
339 if (source_ops(csdev)->enable) {
340 ret = source_ops(csdev)->enable(csdev, NULL, mode);
344 csdev->enable = true;
347 atomic_inc(csdev->refcnt);
353 * coresight_disable_source - Drop the reference count by 1 and disable
354 * the device if there are no users left.
356 * @csdev - The coresight device to disable
358 * Returns true if the device has been disabled.
360 static bool coresight_disable_source(struct coresight_device *csdev)
362 if (atomic_dec_return(csdev->refcnt) == 0) {
363 if (source_ops(csdev)->disable)
364 source_ops(csdev)->disable(csdev, NULL);
365 csdev->enable = false;
367 return !csdev->enable;
371 * coresight_disable_path_from : Disable components in the given path beyond
372 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
375 static void coresight_disable_path_from(struct list_head *path,
376 struct coresight_node *nd)
379 struct coresight_device *csdev, *parent, *child;
382 nd = list_first_entry(path, struct coresight_node, link);
384 list_for_each_entry_continue(nd, path, link) {
389 * ETF devices are tricky... They can be a link or a sink,
390 * depending on how they are configured. If an ETF has been
391 * "activated" it will be configured as a sink, otherwise
392 * go ahead with the link configuration.
394 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
395 type = (csdev == coresight_get_sink(path)) ?
396 CORESIGHT_DEV_TYPE_SINK :
397 CORESIGHT_DEV_TYPE_LINK;
400 case CORESIGHT_DEV_TYPE_SINK:
401 coresight_disable_sink(csdev);
403 case CORESIGHT_DEV_TYPE_SOURCE:
405 * We skip the first node in the path assuming that it
406 * is the source. So we don't expect a source device in
407 * the middle of a path.
411 case CORESIGHT_DEV_TYPE_LINK:
412 parent = list_prev_entry(nd, link)->csdev;
413 child = list_next_entry(nd, link)->csdev;
414 coresight_disable_link(csdev, parent, child);
422 void coresight_disable_path(struct list_head *path)
424 coresight_disable_path_from(path, NULL);
427 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
432 struct coresight_node *nd;
433 struct coresight_device *csdev, *parent, *child;
435 list_for_each_entry_reverse(nd, path, link) {
440 * ETF devices are tricky... They can be a link or a sink,
441 * depending on how they are configured. If an ETF has been
442 * "activated" it will be configured as a sink, otherwise
443 * go ahead with the link configuration.
445 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
446 type = (csdev == coresight_get_sink(path)) ?
447 CORESIGHT_DEV_TYPE_SINK :
448 CORESIGHT_DEV_TYPE_LINK;
451 case CORESIGHT_DEV_TYPE_SINK:
452 ret = coresight_enable_sink(csdev, mode, sink_data);
454 * Sink is the first component turned on. If we
455 * failed to enable the sink, there are no components
456 * that need disabling. Disabling the path here
457 * would mean we could disrupt an existing session.
462 case CORESIGHT_DEV_TYPE_SOURCE:
463 /* sources are enabled from either sysFS or Perf */
465 case CORESIGHT_DEV_TYPE_LINK:
466 parent = list_prev_entry(nd, link)->csdev;
467 child = list_next_entry(nd, link)->csdev;
468 ret = coresight_enable_link(csdev, parent, child);
480 coresight_disable_path_from(path, nd);
484 struct coresight_device *coresight_get_sink(struct list_head *path)
486 struct coresight_device *csdev;
491 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
492 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
493 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
499 static int coresight_enabled_sink(struct device *dev, void *data)
502 struct coresight_device *csdev = to_coresight_device(dev);
504 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
505 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
508 * Now that we have a handle on the sink for this session,
509 * disable the sysFS "enable_sink" flag so that possible
510 * concurrent perf session that wish to use another sink don't
511 * trip on it. Doing so has no ramification for the current
515 csdev->activated = false;
524 * coresight_get_enabled_sink - returns the first enabled sink found on the bus
525 * @deactivate: Whether the 'enable_sink' flag should be reset
527 * When operated from perf the deactivate parameter should be set to 'true'.
528 * That way the "enabled_sink" flag of the sink that was selected can be reset,
529 * allowing for other concurrent perf sessions to choose a different sink.
531 * When operated from sysFS users have full control and as such the deactivate
532 * parameter should be set to 'false', hence mandating users to explicitly
535 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
537 struct device *dev = NULL;
539 dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
540 coresight_enabled_sink);
542 return dev ? to_coresight_device(dev) : NULL;
545 static int coresight_sink_by_id(struct device *dev, void *data)
547 struct coresight_device *csdev = to_coresight_device(dev);
550 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
551 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
556 * See function etm_perf_add_symlink_sink() to know where
559 hash = (unsigned long)csdev->ea->var;
561 if ((u32)hash == *(u32 *)data)
569 * coresight_get_sink_by_id - returns the sink that matches the id
570 * @id: Id of the sink to match
572 * The name of a sink is unique, whether it is found on the AMBA bus or
573 * otherwise. As such the hash of that name can easily be used to identify
576 struct coresight_device *coresight_get_sink_by_id(u32 id)
578 struct device *dev = NULL;
580 dev = bus_find_device(&coresight_bustype, NULL, &id,
581 coresight_sink_by_id);
583 return dev ? to_coresight_device(dev) : NULL;
587 * coresight_grab_device - Power up this device and any of the helper
588 * devices connected to it for trace operation. Since the helper devices
589 * don't appear on the trace path, they should be handled along with the
592 static void coresight_grab_device(struct coresight_device *csdev)
596 for (i = 0; i < csdev->nr_outport; i++) {
597 struct coresight_device *child = csdev->conns[i].child_dev;
599 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
600 pm_runtime_get_sync(child->dev.parent);
602 pm_runtime_get_sync(csdev->dev.parent);
606 * coresight_drop_device - Release this device and any of the helper
607 * devices connected to it.
609 static void coresight_drop_device(struct coresight_device *csdev)
613 pm_runtime_put(csdev->dev.parent);
614 for (i = 0; i < csdev->nr_outport; i++) {
615 struct coresight_device *child = csdev->conns[i].child_dev;
617 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
618 pm_runtime_put(child->dev.parent);
623 * _coresight_build_path - recursively build a path from a @csdev to a sink.
624 * @csdev: The device to start from.
625 * @path: The list to add devices to.
627 * The tree of Coresight device is traversed until an activated sink is
628 * found. From there the sink is added to the list along with all the
629 * devices that led to that point - the end result is a list from source
630 * to sink. In that list the source is the first device and the sink the
633 static int _coresight_build_path(struct coresight_device *csdev,
634 struct coresight_device *sink,
635 struct list_head *path)
639 struct coresight_node *node;
641 /* An activated sink has been found. Enqueue the element */
645 /* Not a sink - recursively explore each port found on this element */
646 for (i = 0; i < csdev->nr_outport; i++) {
647 struct coresight_device *child_dev = csdev->conns[i].child_dev;
650 _coresight_build_path(child_dev, sink, path) == 0) {
661 * A path from this element to a sink has been found. The elements
662 * leading to the sink are already enqueued, all that is left to do
663 * is tell the PM runtime core we need this element and add a node
666 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
670 coresight_grab_device(csdev);
672 list_add(&node->link, path);
677 struct list_head *coresight_build_path(struct coresight_device *source,
678 struct coresight_device *sink)
680 struct list_head *path;
684 return ERR_PTR(-EINVAL);
686 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
688 return ERR_PTR(-ENOMEM);
690 INIT_LIST_HEAD(path);
692 rc = _coresight_build_path(source, sink, path);
702 * coresight_release_path - release a previously built path.
703 * @path: the path to release.
705 * Go through all the elements of a path and 1) removed it from the list and
706 * 2) free the memory allocated for each node.
708 void coresight_release_path(struct list_head *path)
710 struct coresight_device *csdev;
711 struct coresight_node *nd, *next;
713 list_for_each_entry_safe(nd, next, path, link) {
716 coresight_drop_device(csdev);
725 /** coresight_validate_source - make sure a source has the right credentials
726 * @csdev: the device structure for a source.
727 * @function: the function this was called from.
729 * Assumes the coresight_mutex is held.
731 static int coresight_validate_source(struct coresight_device *csdev,
732 const char *function)
737 subtype = csdev->subtype.source_subtype;
739 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
740 dev_err(&csdev->dev, "wrong device type in %s\n", function);
744 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
745 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
746 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
753 int coresight_enable(struct coresight_device *csdev)
756 struct coresight_device *sink;
757 struct list_head *path;
758 enum coresight_dev_subtype_source subtype;
760 subtype = csdev->subtype.source_subtype;
762 mutex_lock(&coresight_mutex);
764 ret = coresight_validate_source(csdev, __func__);
770 * There could be multiple applications driving the software
771 * source. So keep the refcount for each such user when the
772 * source is already enabled.
774 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
775 atomic_inc(csdev->refcnt);
780 * Search for a valid sink for this session but don't reset the
781 * "enable_sink" flag in sysFS. Users get to do that explicitly.
783 sink = coresight_get_enabled_sink(false);
789 path = coresight_build_path(csdev, sink);
791 pr_err("building path(s) failed\n");
796 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
800 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
805 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
807 * When working from sysFS it is important to keep track
808 * of the paths that were created so that they can be
809 * undone in 'coresight_disable()'. Since there can only
810 * be a single session per tracer (when working from sysFS)
811 * a per-cpu variable will do just fine.
813 cpu = source_ops(csdev)->cpu_id(csdev);
814 per_cpu(tracer_path, cpu) = path;
816 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
820 /* We can't be here */
825 mutex_unlock(&coresight_mutex);
829 coresight_disable_path(path);
832 coresight_release_path(path);
835 EXPORT_SYMBOL_GPL(coresight_enable);
837 void coresight_disable(struct coresight_device *csdev)
840 struct list_head *path = NULL;
842 mutex_lock(&coresight_mutex);
844 ret = coresight_validate_source(csdev, __func__);
848 if (!csdev->enable || !coresight_disable_source(csdev))
851 switch (csdev->subtype.source_subtype) {
852 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
853 cpu = source_ops(csdev)->cpu_id(csdev);
854 path = per_cpu(tracer_path, cpu);
855 per_cpu(tracer_path, cpu) = NULL;
857 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
862 /* We can't be here */
866 coresight_disable_path(path);
867 coresight_release_path(path);
870 mutex_unlock(&coresight_mutex);
872 EXPORT_SYMBOL_GPL(coresight_disable);
874 static ssize_t enable_sink_show(struct device *dev,
875 struct device_attribute *attr, char *buf)
877 struct coresight_device *csdev = to_coresight_device(dev);
879 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
882 static ssize_t enable_sink_store(struct device *dev,
883 struct device_attribute *attr,
884 const char *buf, size_t size)
888 struct coresight_device *csdev = to_coresight_device(dev);
890 ret = kstrtoul(buf, 10, &val);
895 csdev->activated = true;
897 csdev->activated = false;
902 static DEVICE_ATTR_RW(enable_sink);
904 static ssize_t enable_source_show(struct device *dev,
905 struct device_attribute *attr, char *buf)
907 struct coresight_device *csdev = to_coresight_device(dev);
909 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
912 static ssize_t enable_source_store(struct device *dev,
913 struct device_attribute *attr,
914 const char *buf, size_t size)
918 struct coresight_device *csdev = to_coresight_device(dev);
920 ret = kstrtoul(buf, 10, &val);
925 ret = coresight_enable(csdev);
929 coresight_disable(csdev);
934 static DEVICE_ATTR_RW(enable_source);
936 static struct attribute *coresight_sink_attrs[] = {
937 &dev_attr_enable_sink.attr,
940 ATTRIBUTE_GROUPS(coresight_sink);
942 static struct attribute *coresight_source_attrs[] = {
943 &dev_attr_enable_source.attr,
946 ATTRIBUTE_GROUPS(coresight_source);
948 static struct device_type coresight_dev_type[] = {
954 .groups = coresight_sink_groups,
961 .groups = coresight_sink_groups,
965 .groups = coresight_source_groups,
972 static void coresight_device_release(struct device *dev)
974 struct coresight_device *csdev = to_coresight_device(dev);
977 kfree(csdev->refcnt);
981 static int coresight_orphan_match(struct device *dev, void *data)
984 bool still_orphan = false;
985 struct coresight_device *csdev, *i_csdev;
986 struct coresight_connection *conn;
989 i_csdev = to_coresight_device(dev);
991 /* No need to check oneself */
992 if (csdev == i_csdev)
995 /* Move on to another component if no connection is orphan */
996 if (!i_csdev->orphan)
999 * Circle throuch all the connection of that component. If we find
1000 * an orphan connection whose name matches @csdev, link it.
1002 for (i = 0; i < i_csdev->nr_outport; i++) {
1003 conn = &i_csdev->conns[i];
1005 /* We have found at least one orphan connection */
1006 if (conn->child_dev == NULL) {
1007 /* Does it match this newly added device? */
1008 if (conn->child_name &&
1009 !strcmp(dev_name(&csdev->dev), conn->child_name)) {
1010 conn->child_dev = csdev;
1012 /* This component still has an orphan */
1013 still_orphan = true;
1018 i_csdev->orphan = still_orphan;
1021 * Returning '0' ensures that all known component on the
1022 * bus will be checked.
1027 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
1030 * No need to check for a return value as orphan connection(s)
1031 * are hooked-up with each newly added component.
1033 bus_for_each_dev(&coresight_bustype, NULL,
1034 csdev, coresight_orphan_match);
1038 static void coresight_fixup_device_conns(struct coresight_device *csdev)
1042 for (i = 0; i < csdev->nr_outport; i++) {
1043 struct coresight_connection *conn = &csdev->conns[i];
1044 struct device *dev = NULL;
1046 if (conn->child_name)
1047 dev = bus_find_device_by_name(&coresight_bustype, NULL,
1050 conn->child_dev = to_coresight_device(dev);
1051 /* and put reference from 'bus_find_device()' */
1054 csdev->orphan = true;
1055 conn->child_dev = NULL;
1060 static int coresight_remove_match(struct device *dev, void *data)
1063 struct coresight_device *csdev, *iterator;
1064 struct coresight_connection *conn;
1067 iterator = to_coresight_device(dev);
1069 /* No need to check oneself */
1070 if (csdev == iterator)
1074 * Circle throuch all the connection of that component. If we find
1075 * a connection whose name matches @csdev, remove it.
1077 for (i = 0; i < iterator->nr_outport; i++) {
1078 conn = &iterator->conns[i];
1080 if (conn->child_dev == NULL)
1083 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
1084 iterator->orphan = true;
1085 conn->child_dev = NULL;
1086 /* No need to continue */
1092 * Returning '0' ensures that all known component on the
1093 * bus will be checked.
1098 static void coresight_remove_conns(struct coresight_device *csdev)
1100 bus_for_each_dev(&coresight_bustype, NULL,
1101 csdev, coresight_remove_match);
1105 * coresight_timeout - loop until a bit has changed to a specific state.
1106 * @addr: base address of the area of interest.
1107 * @offset: address of a register, starting from @addr.
1108 * @position: the position of the bit of interest.
1109 * @value: the value the bit should have.
1111 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1112 * TIMEOUT_US has elapsed, which ever happens first.
1115 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
1120 for (i = TIMEOUT_US; i > 0; i--) {
1121 val = __raw_readl(addr + offset);
1122 /* waiting on the bit to go from 0 to 1 */
1124 if (val & BIT(position))
1126 /* waiting on the bit to go from 1 to 0 */
1128 if (!(val & BIT(position)))
1133 * Delay is arbitrary - the specification doesn't say how long
1134 * we are expected to wait. Extra check required to make sure
1135 * we don't wait needlessly on the last iteration.
1144 struct bus_type coresight_bustype = {
1145 .name = "coresight",
1148 static int __init coresight_init(void)
1150 return bus_register(&coresight_bustype);
1152 postcore_initcall(coresight_init);
1154 struct coresight_device *coresight_register(struct coresight_desc *desc)
1159 atomic_t *refcnts = NULL;
1160 struct coresight_device *csdev;
1162 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1168 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1169 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1170 link_subtype = desc->subtype.link_subtype;
1172 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1173 nr_refcnts = desc->pdata->nr_inport;
1174 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1175 nr_refcnts = desc->pdata->nr_outport;
1178 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1181 goto err_free_csdev;
1184 csdev->refcnt = refcnts;
1186 csdev->nr_inport = desc->pdata->nr_inport;
1187 csdev->nr_outport = desc->pdata->nr_outport;
1189 csdev->conns = desc->pdata->conns;
1191 csdev->type = desc->type;
1192 csdev->subtype = desc->subtype;
1193 csdev->ops = desc->ops;
1194 csdev->orphan = false;
1196 csdev->dev.type = &coresight_dev_type[desc->type];
1197 csdev->dev.groups = desc->groups;
1198 csdev->dev.parent = desc->dev;
1199 csdev->dev.release = coresight_device_release;
1200 csdev->dev.bus = &coresight_bustype;
1201 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
1203 ret = device_register(&csdev->dev);
1205 put_device(&csdev->dev);
1207 * All resources are free'd explicitly via
1208 * coresight_device_release(), triggered from put_device().
1213 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1214 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1215 ret = etm_perf_add_symlink_sink(csdev);
1218 device_unregister(&csdev->dev);
1220 * As with the above, all resources are free'd
1221 * explicitly via coresight_device_release() triggered
1222 * from put_device(), which is in turn called from
1223 * function device_unregister().
1229 mutex_lock(&coresight_mutex);
1231 coresight_fixup_device_conns(csdev);
1232 coresight_fixup_orphan_conns(csdev);
1234 mutex_unlock(&coresight_mutex);
1241 return ERR_PTR(ret);
1243 EXPORT_SYMBOL_GPL(coresight_register);
1245 void coresight_unregister(struct coresight_device *csdev)
1247 etm_perf_del_symlink_sink(csdev);
1248 /* Remove references of that device in the topology */
1249 coresight_remove_conns(csdev);
1250 device_unregister(&csdev->dev);
1252 EXPORT_SYMBOL_GPL(coresight_unregister);