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/idr.h>
12 #include <linux/err.h>
13 #include <linux/export.h>
14 #include <linux/slab.h>
15 #include <linux/stringhash.h>
16 #include <linux/mutex.h>
17 #include <linux/clk.h>
18 #include <linux/coresight.h>
19 #include <linux/of_platform.h>
20 #include <linux/delay.h>
21 #include <linux/pm_runtime.h>
23 #include "coresight-etm-perf.h"
24 #include "coresight-priv.h"
25 #include "coresight-syscfg.h"
27 static DEFINE_MUTEX(coresight_mutex);
28 static DEFINE_PER_CPU(struct coresight_device *, csdev_sink);
31 * Use IDR to map the hash of the source's device name
32 * to the pointer of path for the source. The idr is for
33 * the sources which aren't associated with CPU.
35 static DEFINE_IDR(path_idr);
38 * struct coresight_node - elements of a path, from source to sink
39 * @csdev: Address of an element.
40 * @link: hook to the list.
42 struct coresight_node {
43 struct coresight_device *csdev;
44 struct list_head link;
48 * When operating Coresight drivers from the sysFS interface, only a single
49 * path can exist from a tracer (associated to a CPU) to a sink.
51 static DEFINE_PER_CPU(struct list_head *, tracer_path);
54 * When losing synchronisation a new barrier packet needs to be inserted at the
55 * beginning of the data collected in a buffer. That way the decoder knows that
56 * it needs to look for another sync sequence.
58 const u32 coresight_barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
59 EXPORT_SYMBOL_GPL(coresight_barrier_pkt);
61 static const struct cti_assoc_op *cti_assoc_ops;
63 ssize_t coresight_simple_show_pair(struct device *_dev,
64 struct device_attribute *attr, char *buf)
66 struct coresight_device *csdev = container_of(_dev, struct coresight_device, dev);
67 struct cs_pair_attribute *cs_attr = container_of(attr, struct cs_pair_attribute, attr);
70 pm_runtime_get_sync(_dev->parent);
71 val = csdev_access_relaxed_read_pair(&csdev->access, cs_attr->lo_off, cs_attr->hi_off);
72 pm_runtime_put_sync(_dev->parent);
73 return sysfs_emit(buf, "0x%llx\n", val);
75 EXPORT_SYMBOL_GPL(coresight_simple_show_pair);
77 ssize_t coresight_simple_show32(struct device *_dev,
78 struct device_attribute *attr, char *buf)
80 struct coresight_device *csdev = container_of(_dev, struct coresight_device, dev);
81 struct cs_off_attribute *cs_attr = container_of(attr, struct cs_off_attribute, attr);
84 pm_runtime_get_sync(_dev->parent);
85 val = csdev_access_relaxed_read32(&csdev->access, cs_attr->off);
86 pm_runtime_put_sync(_dev->parent);
87 return sysfs_emit(buf, "0x%llx\n", val);
89 EXPORT_SYMBOL_GPL(coresight_simple_show32);
91 void coresight_set_cti_ops(const struct cti_assoc_op *cti_op)
93 cti_assoc_ops = cti_op;
95 EXPORT_SYMBOL_GPL(coresight_set_cti_ops);
97 void coresight_remove_cti_ops(void)
101 EXPORT_SYMBOL_GPL(coresight_remove_cti_ops);
103 void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev)
105 per_cpu(csdev_sink, cpu) = csdev;
107 EXPORT_SYMBOL_GPL(coresight_set_percpu_sink);
109 struct coresight_device *coresight_get_percpu_sink(int cpu)
111 return per_cpu(csdev_sink, cpu);
113 EXPORT_SYMBOL_GPL(coresight_get_percpu_sink);
115 static int coresight_find_link_inport(struct coresight_device *csdev,
116 struct coresight_device *parent)
119 struct coresight_connection *conn;
121 for (i = 0; i < parent->pdata->nr_outport; i++) {
122 conn = &parent->pdata->conns[i];
123 if (conn->child_dev == csdev)
124 return conn->child_port;
127 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
128 dev_name(&parent->dev), dev_name(&csdev->dev));
133 static int coresight_find_link_outport(struct coresight_device *csdev,
134 struct coresight_device *child)
137 struct coresight_connection *conn;
139 for (i = 0; i < csdev->pdata->nr_outport; i++) {
140 conn = &csdev->pdata->conns[i];
141 if (conn->child_dev == child)
142 return conn->outport;
145 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
146 dev_name(&csdev->dev), dev_name(&child->dev));
151 static inline u32 coresight_read_claim_tags(struct coresight_device *csdev)
153 return csdev_access_relaxed_read32(&csdev->access, CORESIGHT_CLAIMCLR);
156 static inline bool coresight_is_claimed_self_hosted(struct coresight_device *csdev)
158 return coresight_read_claim_tags(csdev) == CORESIGHT_CLAIM_SELF_HOSTED;
161 static inline bool coresight_is_claimed_any(struct coresight_device *csdev)
163 return coresight_read_claim_tags(csdev) != 0;
166 static inline void coresight_set_claim_tags(struct coresight_device *csdev)
168 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
173 static inline void coresight_clear_claim_tags(struct coresight_device *csdev)
175 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
181 * coresight_claim_device_unlocked : Claim the device for self-hosted usage
182 * to prevent an external tool from touching this device. As per PSCI
183 * standards, section "Preserving the execution context" => "Debug and Trace
184 * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
185 * DBGCLAIM[0] is reserved for external tools.
187 * Called with CS_UNLOCKed for the component.
188 * Returns : 0 on success
190 int coresight_claim_device_unlocked(struct coresight_device *csdev)
195 if (coresight_is_claimed_any(csdev))
198 coresight_set_claim_tags(csdev);
199 if (coresight_is_claimed_self_hosted(csdev))
201 /* There was a race setting the tags, clean up and fail */
202 coresight_clear_claim_tags(csdev);
205 EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked);
207 int coresight_claim_device(struct coresight_device *csdev)
214 CS_UNLOCK(csdev->access.base);
215 rc = coresight_claim_device_unlocked(csdev);
216 CS_LOCK(csdev->access.base);
220 EXPORT_SYMBOL_GPL(coresight_claim_device);
223 * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
224 * Called with CS_UNLOCKed for the component.
226 void coresight_disclaim_device_unlocked(struct coresight_device *csdev)
232 if (coresight_is_claimed_self_hosted(csdev))
233 coresight_clear_claim_tags(csdev);
236 * The external agent may have not honoured our claim
237 * and has manipulated it. Or something else has seriously
238 * gone wrong in our driver.
242 EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked);
244 void coresight_disclaim_device(struct coresight_device *csdev)
249 CS_UNLOCK(csdev->access.base);
250 coresight_disclaim_device_unlocked(csdev);
251 CS_LOCK(csdev->access.base);
253 EXPORT_SYMBOL_GPL(coresight_disclaim_device);
255 /* enable or disable an associated CTI device of the supplied CS device */
257 coresight_control_assoc_ectdev(struct coresight_device *csdev, bool enable)
260 struct coresight_device *ect_csdev = csdev->ect_dev;
265 if ((!ect_ops(ect_csdev)->enable) || (!ect_ops(ect_csdev)->disable))
268 mod = ect_csdev->dev.parent->driver->owner;
270 if (try_module_get(mod)) {
271 ect_ret = ect_ops(ect_csdev)->enable(ect_csdev);
275 get_device(ect_csdev->dev.parent);
276 csdev->ect_enabled = true;
281 if (csdev->ect_enabled) {
282 ect_ret = ect_ops(ect_csdev)->disable(ect_csdev);
283 put_device(ect_csdev->dev.parent);
285 csdev->ect_enabled = false;
289 /* output warning if ECT enable is preventing trace operation */
291 dev_info(&csdev->dev, "Associated ECT device (%s) %s failed\n",
292 dev_name(&ect_csdev->dev),
293 enable ? "enable" : "disable");
298 * Set the associated ect / cti device while holding the coresight_mutex
299 * to avoid a race with coresight_enable that may try to use this value.
301 void coresight_set_assoc_ectdev_mutex(struct coresight_device *csdev,
302 struct coresight_device *ect_csdev)
304 mutex_lock(&coresight_mutex);
305 csdev->ect_dev = ect_csdev;
306 mutex_unlock(&coresight_mutex);
308 EXPORT_SYMBOL_GPL(coresight_set_assoc_ectdev_mutex);
310 static int coresight_enable_sink(struct coresight_device *csdev,
311 u32 mode, void *data)
316 * We need to make sure the "new" session is compatible with the
317 * existing "mode" of operation.
319 if (!sink_ops(csdev)->enable)
322 ret = coresight_control_assoc_ectdev(csdev, true);
325 ret = sink_ops(csdev)->enable(csdev, mode, data);
327 coresight_control_assoc_ectdev(csdev, false);
330 csdev->enable = true;
335 static void coresight_disable_sink(struct coresight_device *csdev)
339 if (!sink_ops(csdev)->disable)
342 ret = sink_ops(csdev)->disable(csdev);
345 coresight_control_assoc_ectdev(csdev, false);
346 csdev->enable = false;
349 static int coresight_enable_link(struct coresight_device *csdev,
350 struct coresight_device *parent,
351 struct coresight_device *child)
357 if (!parent || !child)
360 inport = coresight_find_link_inport(csdev, parent);
361 outport = coresight_find_link_outport(csdev, child);
362 link_subtype = csdev->subtype.link_subtype;
364 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && inport < 0)
366 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && outport < 0)
369 if (link_ops(csdev)->enable) {
370 ret = coresight_control_assoc_ectdev(csdev, true);
372 ret = link_ops(csdev)->enable(csdev, inport, outport);
374 coresight_control_assoc_ectdev(csdev, false);
379 csdev->enable = true;
384 static void coresight_disable_link(struct coresight_device *csdev,
385 struct coresight_device *parent,
386 struct coresight_device *child)
392 if (!parent || !child)
395 inport = coresight_find_link_inport(csdev, parent);
396 outport = coresight_find_link_outport(csdev, child);
397 link_subtype = csdev->subtype.link_subtype;
399 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
400 nr_conns = csdev->pdata->nr_inport;
401 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
402 nr_conns = csdev->pdata->nr_outport;
407 if (link_ops(csdev)->disable) {
408 link_ops(csdev)->disable(csdev, inport, outport);
409 coresight_control_assoc_ectdev(csdev, false);
412 for (i = 0; i < nr_conns; i++)
413 if (atomic_read(&csdev->refcnt[i]) != 0)
416 csdev->enable = false;
419 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
423 if (!csdev->enable) {
424 if (source_ops(csdev)->enable) {
425 ret = coresight_control_assoc_ectdev(csdev, true);
428 ret = source_ops(csdev)->enable(csdev, NULL, mode);
430 coresight_control_assoc_ectdev(csdev, false);
434 csdev->enable = true;
437 atomic_inc(csdev->refcnt);
443 * coresight_disable_source - Drop the reference count by 1 and disable
444 * the device if there are no users left.
446 * @csdev: The coresight device to disable
448 * Returns true if the device has been disabled.
450 static bool coresight_disable_source(struct coresight_device *csdev)
452 if (atomic_dec_return(csdev->refcnt) == 0) {
453 if (source_ops(csdev)->disable)
454 source_ops(csdev)->disable(csdev, NULL);
455 coresight_control_assoc_ectdev(csdev, false);
456 csdev->enable = false;
458 return !csdev->enable;
462 * coresight_disable_path_from : Disable components in the given path beyond
463 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
466 static void coresight_disable_path_from(struct list_head *path,
467 struct coresight_node *nd)
470 struct coresight_device *csdev, *parent, *child;
473 nd = list_first_entry(path, struct coresight_node, link);
475 list_for_each_entry_continue(nd, path, link) {
480 * ETF devices are tricky... They can be a link or a sink,
481 * depending on how they are configured. If an ETF has been
482 * "activated" it will be configured as a sink, otherwise
483 * go ahead with the link configuration.
485 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
486 type = (csdev == coresight_get_sink(path)) ?
487 CORESIGHT_DEV_TYPE_SINK :
488 CORESIGHT_DEV_TYPE_LINK;
491 case CORESIGHT_DEV_TYPE_SINK:
492 coresight_disable_sink(csdev);
494 case CORESIGHT_DEV_TYPE_SOURCE:
496 * We skip the first node in the path assuming that it
497 * is the source. So we don't expect a source device in
498 * the middle of a path.
502 case CORESIGHT_DEV_TYPE_LINK:
503 parent = list_prev_entry(nd, link)->csdev;
504 child = list_next_entry(nd, link)->csdev;
505 coresight_disable_link(csdev, parent, child);
513 void coresight_disable_path(struct list_head *path)
515 coresight_disable_path_from(path, NULL);
517 EXPORT_SYMBOL_GPL(coresight_disable_path);
519 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
524 struct coresight_node *nd;
525 struct coresight_device *csdev, *parent, *child;
527 list_for_each_entry_reverse(nd, path, link) {
532 * ETF devices are tricky... They can be a link or a sink,
533 * depending on how they are configured. If an ETF has been
534 * "activated" it will be configured as a sink, otherwise
535 * go ahead with the link configuration.
537 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
538 type = (csdev == coresight_get_sink(path)) ?
539 CORESIGHT_DEV_TYPE_SINK :
540 CORESIGHT_DEV_TYPE_LINK;
543 case CORESIGHT_DEV_TYPE_SINK:
544 ret = coresight_enable_sink(csdev, mode, sink_data);
546 * Sink is the first component turned on. If we
547 * failed to enable the sink, there are no components
548 * that need disabling. Disabling the path here
549 * would mean we could disrupt an existing session.
554 case CORESIGHT_DEV_TYPE_SOURCE:
555 /* sources are enabled from either sysFS or Perf */
557 case CORESIGHT_DEV_TYPE_LINK:
558 parent = list_prev_entry(nd, link)->csdev;
559 child = list_next_entry(nd, link)->csdev;
560 ret = coresight_enable_link(csdev, parent, child);
572 coresight_disable_path_from(path, nd);
576 struct coresight_device *coresight_get_sink(struct list_head *path)
578 struct coresight_device *csdev;
583 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
584 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
585 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
591 static struct coresight_device *
592 coresight_find_enabled_sink(struct coresight_device *csdev)
595 struct coresight_device *sink = NULL;
597 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
598 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
603 * Recursively explore each port found on this element.
605 for (i = 0; i < csdev->pdata->nr_outport; i++) {
606 struct coresight_device *child_dev;
608 child_dev = csdev->pdata->conns[i].child_dev;
610 sink = coresight_find_enabled_sink(child_dev);
619 * coresight_get_enabled_sink - returns the first enabled sink using
620 * connection based search starting from the source reference
622 * @source: Coresight source device reference
624 struct coresight_device *
625 coresight_get_enabled_sink(struct coresight_device *source)
630 return coresight_find_enabled_sink(source);
633 static int coresight_sink_by_id(struct device *dev, const void *data)
635 struct coresight_device *csdev = to_coresight_device(dev);
638 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
639 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
644 * See function etm_perf_add_symlink_sink() to know where
647 hash = (unsigned long)csdev->ea->var;
649 if ((u32)hash == *(u32 *)data)
657 * coresight_get_sink_by_id - returns the sink that matches the id
658 * @id: Id of the sink to match
660 * The name of a sink is unique, whether it is found on the AMBA bus or
661 * otherwise. As such the hash of that name can easily be used to identify
664 struct coresight_device *coresight_get_sink_by_id(u32 id)
666 struct device *dev = NULL;
668 dev = bus_find_device(&coresight_bustype, NULL, &id,
669 coresight_sink_by_id);
671 return dev ? to_coresight_device(dev) : NULL;
675 * coresight_get_ref- Helper function to increase reference count to module
678 * @csdev: The coresight device to get a reference on.
680 * Return true in successful case and power up the device.
681 * Return false when failed to get reference of module.
683 static inline bool coresight_get_ref(struct coresight_device *csdev)
685 struct device *dev = csdev->dev.parent;
687 /* Make sure the driver can't be removed */
688 if (!try_module_get(dev->driver->owner))
690 /* Make sure the device can't go away */
692 pm_runtime_get_sync(dev);
697 * coresight_put_ref- Helper function to decrease reference count to module
698 * and device. Power off the device.
700 * @csdev: The coresight device to decrement a reference from.
702 static inline void coresight_put_ref(struct coresight_device *csdev)
704 struct device *dev = csdev->dev.parent;
708 module_put(dev->driver->owner);
712 * coresight_grab_device - Power up this device and any of the helper
713 * devices connected to it for trace operation. Since the helper devices
714 * don't appear on the trace path, they should be handled along with the
717 static int coresight_grab_device(struct coresight_device *csdev)
721 for (i = 0; i < csdev->pdata->nr_outport; i++) {
722 struct coresight_device *child;
724 child = csdev->pdata->conns[i].child_dev;
725 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
726 if (!coresight_get_ref(child))
729 if (coresight_get_ref(csdev))
732 for (i--; i >= 0; i--) {
733 struct coresight_device *child;
735 child = csdev->pdata->conns[i].child_dev;
736 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
737 coresight_put_ref(child);
743 * coresight_drop_device - Release this device and any of the helper
744 * devices connected to it.
746 static void coresight_drop_device(struct coresight_device *csdev)
750 coresight_put_ref(csdev);
751 for (i = 0; i < csdev->pdata->nr_outport; i++) {
752 struct coresight_device *child;
754 child = csdev->pdata->conns[i].child_dev;
755 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
756 coresight_put_ref(child);
761 * _coresight_build_path - recursively build a path from a @csdev to a sink.
762 * @csdev: The device to start from.
763 * @sink: The final sink we want in this path.
764 * @path: The list to add devices to.
766 * The tree of Coresight device is traversed until an activated sink is
767 * found. From there the sink is added to the list along with all the
768 * devices that led to that point - the end result is a list from source
769 * to sink. In that list the source is the first device and the sink the
772 static int _coresight_build_path(struct coresight_device *csdev,
773 struct coresight_device *sink,
774 struct list_head *path)
778 struct coresight_node *node;
780 /* An activated sink has been found. Enqueue the element */
784 if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&
785 sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {
786 if (_coresight_build_path(sink, sink, path) == 0) {
792 /* Not a sink - recursively explore each port found on this element */
793 for (i = 0; i < csdev->pdata->nr_outport; i++) {
794 struct coresight_device *child_dev;
796 child_dev = csdev->pdata->conns[i].child_dev;
798 _coresight_build_path(child_dev, sink, path) == 0) {
809 * A path from this element to a sink has been found. The elements
810 * leading to the sink are already enqueued, all that is left to do
811 * is tell the PM runtime core we need this element and add a node
814 ret = coresight_grab_device(csdev);
818 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
823 list_add(&node->link, path);
828 struct list_head *coresight_build_path(struct coresight_device *source,
829 struct coresight_device *sink)
831 struct list_head *path;
835 return ERR_PTR(-EINVAL);
837 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
839 return ERR_PTR(-ENOMEM);
841 INIT_LIST_HEAD(path);
843 rc = _coresight_build_path(source, sink, path);
853 * coresight_release_path - release a previously built path.
854 * @path: the path to release.
856 * Go through all the elements of a path and 1) removed it from the list and
857 * 2) free the memory allocated for each node.
859 void coresight_release_path(struct list_head *path)
861 struct coresight_device *csdev;
862 struct coresight_node *nd, *next;
864 list_for_each_entry_safe(nd, next, path, link) {
867 coresight_drop_device(csdev);
875 /* return true if the device is a suitable type for a default sink */
876 static inline bool coresight_is_def_sink_type(struct coresight_device *csdev)
878 /* sink & correct subtype */
879 if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
880 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
881 (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
887 * coresight_select_best_sink - return the best sink for use as default from
890 * @sink: current best sink.
891 * @depth: search depth where current sink was found.
892 * @new_sink: new sink for comparison with current sink.
893 * @new_depth: search depth where new sink was found.
895 * Sinks prioritised according to coresight_dev_subtype_sink, with only
896 * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used.
898 * Where two sinks of equal priority are found, the sink closest to the
899 * source is used (smallest search depth).
901 * return @new_sink & update @depth if better than @sink, else return @sink.
903 static struct coresight_device *
904 coresight_select_best_sink(struct coresight_device *sink, int *depth,
905 struct coresight_device *new_sink, int new_depth)
910 /* first found at this level */
912 } else if (new_sink->subtype.sink_subtype >
913 sink->subtype.sink_subtype) {
914 /* found better sink */
916 } else if ((new_sink->subtype.sink_subtype ==
917 sink->subtype.sink_subtype) &&
918 (*depth > new_depth)) {
919 /* found same but closer sink */
925 return update ? new_sink : sink;
929 * coresight_find_sink - recursive function to walk trace connections from
930 * source to find a suitable default sink.
932 * @csdev: source / current device to check.
933 * @depth: [in] search depth of calling dev, [out] depth of found sink.
935 * This will walk the connection path from a source (ETM) till a suitable
936 * sink is encountered and return that sink to the original caller.
938 * If current device is a plain sink return that & depth, otherwise recursively
939 * call child connections looking for a sink. Select best possible using
940 * coresight_select_best_sink.
942 * return best sink found, or NULL if not found at this node or child nodes.
944 static struct coresight_device *
945 coresight_find_sink(struct coresight_device *csdev, int *depth)
947 int i, curr_depth = *depth + 1, found_depth = 0;
948 struct coresight_device *found_sink = NULL;
950 if (coresight_is_def_sink_type(csdev)) {
951 found_depth = curr_depth;
953 if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
954 goto return_def_sink;
955 /* look past LINKSINK for something better */
959 * Not a sink we want - or possible child sink may be better.
960 * recursively explore each port found on this element.
962 for (i = 0; i < csdev->pdata->nr_outport; i++) {
963 struct coresight_device *child_dev, *sink = NULL;
964 int child_depth = curr_depth;
966 child_dev = csdev->pdata->conns[i].child_dev;
968 sink = coresight_find_sink(child_dev, &child_depth);
971 found_sink = coresight_select_best_sink(found_sink,
978 /* return found sink and depth */
980 *depth = found_depth;
985 * coresight_find_default_sink: Find a sink suitable for use as a
988 * @csdev: starting source to find a connected sink.
990 * Walks connections graph looking for a suitable sink to enable for the
991 * supplied source. Uses CoreSight device subtypes and distance from source
992 * to select the best sink.
994 * If a sink is found, then the default sink for this device is set and
995 * will be automatically used in future.
997 * Used in cases where the CoreSight user (perf / sysfs) has not selected a
1000 struct coresight_device *
1001 coresight_find_default_sink(struct coresight_device *csdev)
1005 /* look for a default sink if we have not found for this device */
1006 if (!csdev->def_sink) {
1007 if (coresight_is_percpu_source(csdev))
1008 csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));
1009 if (!csdev->def_sink)
1010 csdev->def_sink = coresight_find_sink(csdev, &depth);
1012 return csdev->def_sink;
1015 static int coresight_remove_sink_ref(struct device *dev, void *data)
1017 struct coresight_device *sink = data;
1018 struct coresight_device *source = to_coresight_device(dev);
1020 if (source->def_sink == sink)
1021 source->def_sink = NULL;
1026 * coresight_clear_default_sink: Remove all default sink references to the
1029 * If supplied device is a sink, then check all the bus devices and clear
1030 * out all the references to this sink from the coresight_device def_sink
1033 * @csdev: coresight sink - remove references to this from all sources.
1035 static void coresight_clear_default_sink(struct coresight_device *csdev)
1037 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
1038 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
1039 bus_for_each_dev(&coresight_bustype, NULL, csdev,
1040 coresight_remove_sink_ref);
1044 /** coresight_validate_source - make sure a source has the right credentials
1045 * @csdev: the device structure for a source.
1046 * @function: the function this was called from.
1048 * Assumes the coresight_mutex is held.
1050 static int coresight_validate_source(struct coresight_device *csdev,
1051 const char *function)
1056 subtype = csdev->subtype.source_subtype;
1058 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
1059 dev_err(&csdev->dev, "wrong device type in %s\n", function);
1063 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
1064 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE &&
1065 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_OTHERS) {
1066 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
1073 int coresight_enable(struct coresight_device *csdev)
1076 struct coresight_device *sink;
1077 struct list_head *path;
1078 enum coresight_dev_subtype_source subtype;
1081 subtype = csdev->subtype.source_subtype;
1083 mutex_lock(&coresight_mutex);
1085 ret = coresight_validate_source(csdev, __func__);
1089 if (csdev->enable) {
1091 * There could be multiple applications driving the software
1092 * source. So keep the refcount for each such user when the
1093 * source is already enabled.
1095 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
1096 atomic_inc(csdev->refcnt);
1100 sink = coresight_get_enabled_sink(csdev);
1106 path = coresight_build_path(csdev, sink);
1108 pr_err("building path(s) failed\n");
1109 ret = PTR_ERR(path);
1113 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
1117 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
1122 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1124 * When working from sysFS it is important to keep track
1125 * of the paths that were created so that they can be
1126 * undone in 'coresight_disable()'. Since there can only
1127 * be a single session per tracer (when working from sysFS)
1128 * a per-cpu variable will do just fine.
1130 cpu = source_ops(csdev)->cpu_id(csdev);
1131 per_cpu(tracer_path, cpu) = path;
1133 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1134 case CORESIGHT_DEV_SUBTYPE_SOURCE_OTHERS:
1136 * Use the hash of source's device name as ID
1137 * and map the ID to the pointer of the path.
1139 hash = hashlen_hash(hashlen_string(NULL, dev_name(&csdev->dev)));
1140 ret = idr_alloc_u32(&path_idr, path, &hash, hash, GFP_KERNEL);
1145 /* We can't be here */
1150 mutex_unlock(&coresight_mutex);
1154 coresight_disable_path(path);
1157 coresight_release_path(path);
1160 EXPORT_SYMBOL_GPL(coresight_enable);
1162 void coresight_disable(struct coresight_device *csdev)
1165 struct list_head *path = NULL;
1168 mutex_lock(&coresight_mutex);
1170 ret = coresight_validate_source(csdev, __func__);
1174 if (!csdev->enable || !coresight_disable_source(csdev))
1177 switch (csdev->subtype.source_subtype) {
1178 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1179 cpu = source_ops(csdev)->cpu_id(csdev);
1180 path = per_cpu(tracer_path, cpu);
1181 per_cpu(tracer_path, cpu) = NULL;
1183 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1184 case CORESIGHT_DEV_SUBTYPE_SOURCE_OTHERS:
1185 hash = hashlen_hash(hashlen_string(NULL, dev_name(&csdev->dev)));
1186 /* Find the path by the hash. */
1187 path = idr_find(&path_idr, hash);
1189 pr_err("Path is not found for %s\n", dev_name(&csdev->dev));
1192 idr_remove(&path_idr, hash);
1195 /* We can't be here */
1199 coresight_disable_path(path);
1200 coresight_release_path(path);
1203 mutex_unlock(&coresight_mutex);
1205 EXPORT_SYMBOL_GPL(coresight_disable);
1207 static ssize_t enable_sink_show(struct device *dev,
1208 struct device_attribute *attr, char *buf)
1210 struct coresight_device *csdev = to_coresight_device(dev);
1212 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
1215 static ssize_t enable_sink_store(struct device *dev,
1216 struct device_attribute *attr,
1217 const char *buf, size_t size)
1221 struct coresight_device *csdev = to_coresight_device(dev);
1223 ret = kstrtoul(buf, 10, &val);
1228 csdev->activated = true;
1230 csdev->activated = false;
1235 static DEVICE_ATTR_RW(enable_sink);
1237 static ssize_t enable_source_show(struct device *dev,
1238 struct device_attribute *attr, char *buf)
1240 struct coresight_device *csdev = to_coresight_device(dev);
1242 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
1245 static ssize_t enable_source_store(struct device *dev,
1246 struct device_attribute *attr,
1247 const char *buf, size_t size)
1251 struct coresight_device *csdev = to_coresight_device(dev);
1253 ret = kstrtoul(buf, 10, &val);
1258 ret = coresight_enable(csdev);
1262 coresight_disable(csdev);
1267 static DEVICE_ATTR_RW(enable_source);
1269 static struct attribute *coresight_sink_attrs[] = {
1270 &dev_attr_enable_sink.attr,
1273 ATTRIBUTE_GROUPS(coresight_sink);
1275 static struct attribute *coresight_source_attrs[] = {
1276 &dev_attr_enable_source.attr,
1279 ATTRIBUTE_GROUPS(coresight_source);
1281 static struct device_type coresight_dev_type[] = {
1284 .groups = coresight_sink_groups,
1291 .groups = coresight_sink_groups,
1295 .groups = coresight_source_groups,
1305 static void coresight_device_release(struct device *dev)
1307 struct coresight_device *csdev = to_coresight_device(dev);
1309 fwnode_handle_put(csdev->dev.fwnode);
1310 kfree(csdev->refcnt);
1314 static int coresight_orphan_match(struct device *dev, void *data)
1317 bool still_orphan = false;
1318 struct coresight_device *csdev, *i_csdev;
1319 struct coresight_connection *conn;
1322 i_csdev = to_coresight_device(dev);
1324 /* No need to check oneself */
1325 if (csdev == i_csdev)
1328 /* Move on to another component if no connection is orphan */
1329 if (!i_csdev->orphan)
1332 * Circle throuch all the connection of that component. If we find
1333 * an orphan connection whose name matches @csdev, link it.
1335 for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
1336 conn = &i_csdev->pdata->conns[i];
1338 /* Skip the port if FW doesn't describe it */
1339 if (!conn->child_fwnode)
1341 /* We have found at least one orphan connection */
1342 if (conn->child_dev == NULL) {
1343 /* Does it match this newly added device? */
1344 if (conn->child_fwnode == csdev->dev.fwnode) {
1345 ret = coresight_make_links(i_csdev,
1350 /* This component still has an orphan */
1351 still_orphan = true;
1356 i_csdev->orphan = still_orphan;
1359 * Returning '0' in case we didn't encounter any error,
1360 * ensures that all known component on the bus will be checked.
1365 static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
1367 return bus_for_each_dev(&coresight_bustype, NULL,
1368 csdev, coresight_orphan_match);
1372 static int coresight_fixup_device_conns(struct coresight_device *csdev)
1376 for (i = 0; i < csdev->pdata->nr_outport; i++) {
1377 struct coresight_connection *conn = &csdev->pdata->conns[i];
1379 if (!conn->child_fwnode)
1382 coresight_find_csdev_by_fwnode(conn->child_fwnode);
1383 if (conn->child_dev && conn->child_dev->has_conns_grp) {
1384 ret = coresight_make_links(csdev, conn,
1389 csdev->orphan = true;
1396 static int coresight_remove_match(struct device *dev, void *data)
1399 struct coresight_device *csdev, *iterator;
1400 struct coresight_connection *conn;
1403 iterator = to_coresight_device(dev);
1405 /* No need to check oneself */
1406 if (csdev == iterator)
1410 * Circle throuch all the connection of that component. If we find
1411 * a connection whose name matches @csdev, remove it.
1413 for (i = 0; i < iterator->pdata->nr_outport; i++) {
1414 conn = &iterator->pdata->conns[i];
1416 if (conn->child_dev == NULL || conn->child_fwnode == NULL)
1419 if (csdev->dev.fwnode == conn->child_fwnode) {
1420 iterator->orphan = true;
1421 coresight_remove_links(iterator, conn);
1423 * Drop the reference to the handle for the remote
1424 * device acquired in parsing the connections from
1427 fwnode_handle_put(conn->child_fwnode);
1428 conn->child_fwnode = NULL;
1429 /* No need to continue */
1435 * Returning '0' ensures that all known component on the
1436 * bus will be checked.
1442 * coresight_remove_conns - Remove references to this given devices
1443 * from the connections of other devices.
1445 static void coresight_remove_conns(struct coresight_device *csdev)
1448 * Another device will point to this device only if there is
1449 * an output port connected to this one. i.e, if the device
1450 * doesn't have at least one input port, there is no point
1451 * in searching all the devices.
1453 if (csdev->pdata->nr_inport)
1454 bus_for_each_dev(&coresight_bustype, NULL,
1455 csdev, coresight_remove_match);
1459 * coresight_timeout - loop until a bit has changed to a specific register
1461 * @csa: coresight device access for the device
1462 * @offset: Offset of the register from the base of the device.
1463 * @position: the position of the bit of interest.
1464 * @value: the value the bit should have.
1466 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1467 * TIMEOUT_US has elapsed, which ever happens first.
1469 int coresight_timeout(struct csdev_access *csa, u32 offset,
1470 int position, int value)
1475 for (i = TIMEOUT_US; i > 0; i--) {
1476 val = csdev_access_read32(csa, offset);
1477 /* waiting on the bit to go from 0 to 1 */
1479 if (val & BIT(position))
1481 /* waiting on the bit to go from 1 to 0 */
1483 if (!(val & BIT(position)))
1488 * Delay is arbitrary - the specification doesn't say how long
1489 * we are expected to wait. Extra check required to make sure
1490 * we don't wait needlessly on the last iteration.
1498 EXPORT_SYMBOL_GPL(coresight_timeout);
1500 u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset)
1502 return csdev_access_relaxed_read32(&csdev->access, offset);
1505 u32 coresight_read32(struct coresight_device *csdev, u32 offset)
1507 return csdev_access_read32(&csdev->access, offset);
1510 void coresight_relaxed_write32(struct coresight_device *csdev,
1511 u32 val, u32 offset)
1513 csdev_access_relaxed_write32(&csdev->access, val, offset);
1516 void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset)
1518 csdev_access_write32(&csdev->access, val, offset);
1521 u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset)
1523 return csdev_access_relaxed_read64(&csdev->access, offset);
1526 u64 coresight_read64(struct coresight_device *csdev, u32 offset)
1528 return csdev_access_read64(&csdev->access, offset);
1531 void coresight_relaxed_write64(struct coresight_device *csdev,
1532 u64 val, u32 offset)
1534 csdev_access_relaxed_write64(&csdev->access, val, offset);
1537 void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset)
1539 csdev_access_write64(&csdev->access, val, offset);
1543 * coresight_release_platform_data: Release references to the devices connected
1544 * to the output port of this device.
1546 void coresight_release_platform_data(struct coresight_device *csdev,
1547 struct coresight_platform_data *pdata)
1550 struct coresight_connection *conns = pdata->conns;
1552 for (i = 0; i < pdata->nr_outport; i++) {
1553 /* If we have made the links, remove them now */
1554 if (csdev && conns[i].child_dev)
1555 coresight_remove_links(csdev, &conns[i]);
1557 * Drop the refcount and clear the handle as this device
1560 if (conns[i].child_fwnode) {
1561 fwnode_handle_put(conns[i].child_fwnode);
1562 pdata->conns[i].child_fwnode = NULL;
1566 coresight_remove_conns_sysfs_group(csdev);
1569 struct coresight_device *coresight_register(struct coresight_desc *desc)
1574 atomic_t *refcnts = NULL;
1575 struct coresight_device *csdev;
1576 bool registered = false;
1578 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1584 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1585 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1586 link_subtype = desc->subtype.link_subtype;
1588 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1589 nr_refcnts = desc->pdata->nr_inport;
1590 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1591 nr_refcnts = desc->pdata->nr_outport;
1594 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1601 csdev->refcnt = refcnts;
1603 csdev->pdata = desc->pdata;
1605 csdev->type = desc->type;
1606 csdev->subtype = desc->subtype;
1607 csdev->ops = desc->ops;
1608 csdev->access = desc->access;
1609 csdev->orphan = false;
1611 csdev->dev.type = &coresight_dev_type[desc->type];
1612 csdev->dev.groups = desc->groups;
1613 csdev->dev.parent = desc->dev;
1614 csdev->dev.release = coresight_device_release;
1615 csdev->dev.bus = &coresight_bustype;
1617 * Hold the reference to our parent device. This will be
1618 * dropped only in coresight_device_release().
1620 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1621 dev_set_name(&csdev->dev, "%s", desc->name);
1624 * Make sure the device registration and the connection fixup
1625 * are synchronised, so that we don't see uninitialised devices
1626 * on the coresight bus while trying to resolve the connections.
1628 mutex_lock(&coresight_mutex);
1630 ret = device_register(&csdev->dev);
1632 put_device(&csdev->dev);
1634 * All resources are free'd explicitly via
1635 * coresight_device_release(), triggered from put_device().
1640 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1641 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1642 ret = etm_perf_add_symlink_sink(csdev);
1645 device_unregister(&csdev->dev);
1647 * As with the above, all resources are free'd
1648 * explicitly via coresight_device_release() triggered
1649 * from put_device(), which is in turn called from
1650 * function device_unregister().
1655 /* Device is now registered */
1658 ret = coresight_create_conns_sysfs_group(csdev);
1660 ret = coresight_fixup_device_conns(csdev);
1662 ret = coresight_fixup_orphan_conns(csdev);
1665 mutex_unlock(&coresight_mutex);
1668 if (cti_assoc_ops && cti_assoc_ops->add)
1669 cti_assoc_ops->add(csdev);
1673 /* Unregister the device if needed */
1675 coresight_unregister(csdev);
1676 return ERR_PTR(ret);
1680 /* Cleanup the connection information */
1681 coresight_release_platform_data(NULL, desc->pdata);
1682 return ERR_PTR(ret);
1684 EXPORT_SYMBOL_GPL(coresight_register);
1686 void coresight_unregister(struct coresight_device *csdev)
1688 etm_perf_del_symlink_sink(csdev);
1689 /* Remove references of that device in the topology */
1690 if (cti_assoc_ops && cti_assoc_ops->remove)
1691 cti_assoc_ops->remove(csdev);
1692 coresight_remove_conns(csdev);
1693 coresight_clear_default_sink(csdev);
1694 coresight_release_platform_data(csdev, csdev->pdata);
1695 device_unregister(&csdev->dev);
1697 EXPORT_SYMBOL_GPL(coresight_unregister);
1701 * coresight_search_device_idx - Search the fwnode handle of a device
1702 * in the given dev_idx list. Must be called with the coresight_mutex held.
1704 * Returns the index of the entry, when found. Otherwise, -ENOENT.
1706 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1707 struct fwnode_handle *fwnode)
1711 for (i = 0; i < dict->nr_idx; i++)
1712 if (dict->fwnode_list[i] == fwnode)
1717 bool coresight_loses_context_with_cpu(struct device *dev)
1719 return fwnode_property_present(dev_fwnode(dev),
1720 "arm,coresight-loses-context-with-cpu");
1722 EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
1725 * coresight_alloc_device_name - Get an index for a given device in the
1726 * device index list specific to a driver. An index is allocated for a
1727 * device and is tracked with the fwnode_handle to prevent allocating
1728 * duplicate indices for the same device (e.g, if we defer probing of
1729 * a device due to dependencies), in case the index is requested again.
1731 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1736 struct fwnode_handle **list;
1738 mutex_lock(&coresight_mutex);
1740 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1742 /* Make space for the new entry */
1744 list = krealloc_array(dict->fwnode_list,
1745 idx + 1, sizeof(*dict->fwnode_list),
1747 if (ZERO_OR_NULL_PTR(list)) {
1752 list[idx] = dev_fwnode(dev);
1753 dict->fwnode_list = list;
1754 dict->nr_idx = idx + 1;
1757 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1759 mutex_unlock(&coresight_mutex);
1762 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1764 struct bus_type coresight_bustype = {
1765 .name = "coresight",
1768 static int __init coresight_init(void)
1772 ret = bus_register(&coresight_bustype);
1776 ret = etm_perf_init();
1778 goto exit_bus_unregister;
1780 /* initialise the coresight syscfg API */
1786 exit_bus_unregister:
1787 bus_unregister(&coresight_bustype);
1791 static void __exit coresight_exit(void)
1795 bus_unregister(&coresight_bustype);
1798 module_init(coresight_init);
1799 module_exit(coresight_exit);
1801 MODULE_LICENSE("GPL v2");
1802 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1803 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1804 MODULE_DESCRIPTION("Arm CoreSight tracer driver");