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);
26 static DEFINE_PER_CPU(struct coresight_device *, csdev_sink);
29 * struct coresight_node - elements of a path, from source to sink
30 * @csdev: Address of an element.
31 * @link: hook to the list.
33 struct coresight_node {
34 struct coresight_device *csdev;
35 struct list_head link;
39 * When operating Coresight drivers from the sysFS interface, only a single
40 * path can exist from a tracer (associated to a CPU) to a sink.
42 static DEFINE_PER_CPU(struct list_head *, tracer_path);
45 * As of this writing only a single STM can be found in CS topologies. Since
46 * there is no way to know if we'll ever see more and what kind of
47 * configuration they will enact, for the time being only define a single path
50 static struct list_head *stm_path;
53 * When losing synchronisation a new barrier packet needs to be inserted at the
54 * beginning of the data collected in a buffer. That way the decoder knows that
55 * it needs to look for another sync sequence.
57 const u32 coresight_barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
58 EXPORT_SYMBOL_GPL(coresight_barrier_pkt);
60 static const struct cti_assoc_op *cti_assoc_ops;
62 void coresight_set_cti_ops(const struct cti_assoc_op *cti_op)
64 cti_assoc_ops = cti_op;
66 EXPORT_SYMBOL_GPL(coresight_set_cti_ops);
68 void coresight_remove_cti_ops(void)
72 EXPORT_SYMBOL_GPL(coresight_remove_cti_ops);
74 void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev)
76 per_cpu(csdev_sink, cpu) = csdev;
78 EXPORT_SYMBOL_GPL(coresight_set_percpu_sink);
80 struct coresight_device *coresight_get_percpu_sink(int cpu)
82 return per_cpu(csdev_sink, cpu);
84 EXPORT_SYMBOL_GPL(coresight_get_percpu_sink);
86 static int coresight_id_match(struct device *dev, void *data)
88 int trace_id, i_trace_id;
89 struct coresight_device *csdev, *i_csdev;
92 i_csdev = to_coresight_device(dev);
95 * No need to care about oneself and components that are not
96 * sources or not enabled
98 if (i_csdev == csdev || !i_csdev->enable ||
99 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
102 /* Get the source ID for both components */
103 trace_id = source_ops(csdev)->trace_id(csdev);
104 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
106 /* All you need is one */
107 if (trace_id == i_trace_id)
113 static int coresight_source_is_unique(struct coresight_device *csdev)
115 int trace_id = source_ops(csdev)->trace_id(csdev);
117 /* this shouldn't happen */
121 return !bus_for_each_dev(&coresight_bustype, NULL,
122 csdev, coresight_id_match);
125 static int coresight_find_link_inport(struct coresight_device *csdev,
126 struct coresight_device *parent)
129 struct coresight_connection *conn;
131 for (i = 0; i < parent->pdata->nr_outport; i++) {
132 conn = &parent->pdata->conns[i];
133 if (conn->child_dev == csdev)
134 return conn->child_port;
137 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
138 dev_name(&parent->dev), dev_name(&csdev->dev));
143 static int coresight_find_link_outport(struct coresight_device *csdev,
144 struct coresight_device *child)
147 struct coresight_connection *conn;
149 for (i = 0; i < csdev->pdata->nr_outport; i++) {
150 conn = &csdev->pdata->conns[i];
151 if (conn->child_dev == child)
152 return conn->outport;
155 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
156 dev_name(&csdev->dev), dev_name(&child->dev));
161 static inline u32 coresight_read_claim_tags(struct coresight_device *csdev)
163 return csdev_access_relaxed_read32(&csdev->access, CORESIGHT_CLAIMCLR);
166 static inline bool coresight_is_claimed_self_hosted(struct coresight_device *csdev)
168 return coresight_read_claim_tags(csdev) == CORESIGHT_CLAIM_SELF_HOSTED;
171 static inline bool coresight_is_claimed_any(struct coresight_device *csdev)
173 return coresight_read_claim_tags(csdev) != 0;
176 static inline void coresight_set_claim_tags(struct coresight_device *csdev)
178 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
183 static inline void coresight_clear_claim_tags(struct coresight_device *csdev)
185 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
191 * coresight_claim_device_unlocked : Claim the device for self-hosted usage
192 * to prevent an external tool from touching this device. As per PSCI
193 * standards, section "Preserving the execution context" => "Debug and Trace
194 * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
195 * DBGCLAIM[0] is reserved for external tools.
197 * Called with CS_UNLOCKed for the component.
198 * Returns : 0 on success
200 int coresight_claim_device_unlocked(struct coresight_device *csdev)
205 if (coresight_is_claimed_any(csdev))
208 coresight_set_claim_tags(csdev);
209 if (coresight_is_claimed_self_hosted(csdev))
211 /* There was a race setting the tags, clean up and fail */
212 coresight_clear_claim_tags(csdev);
215 EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked);
217 int coresight_claim_device(struct coresight_device *csdev)
224 CS_UNLOCK(csdev->access.base);
225 rc = coresight_claim_device_unlocked(csdev);
226 CS_LOCK(csdev->access.base);
230 EXPORT_SYMBOL_GPL(coresight_claim_device);
233 * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
234 * Called with CS_UNLOCKed for the component.
236 void coresight_disclaim_device_unlocked(struct coresight_device *csdev)
242 if (coresight_is_claimed_self_hosted(csdev))
243 coresight_clear_claim_tags(csdev);
246 * The external agent may have not honoured our claim
247 * and has manipulated it. Or something else has seriously
248 * gone wrong in our driver.
252 EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked);
254 void coresight_disclaim_device(struct coresight_device *csdev)
259 CS_UNLOCK(csdev->access.base);
260 coresight_disclaim_device_unlocked(csdev);
261 CS_LOCK(csdev->access.base);
263 EXPORT_SYMBOL_GPL(coresight_disclaim_device);
265 /* enable or disable an associated CTI device of the supplied CS device */
267 coresight_control_assoc_ectdev(struct coresight_device *csdev, bool enable)
270 struct coresight_device *ect_csdev = csdev->ect_dev;
275 if ((!ect_ops(ect_csdev)->enable) || (!ect_ops(ect_csdev)->disable))
278 mod = ect_csdev->dev.parent->driver->owner;
280 if (try_module_get(mod)) {
281 ect_ret = ect_ops(ect_csdev)->enable(ect_csdev);
285 get_device(ect_csdev->dev.parent);
286 csdev->ect_enabled = true;
291 if (csdev->ect_enabled) {
292 ect_ret = ect_ops(ect_csdev)->disable(ect_csdev);
293 put_device(ect_csdev->dev.parent);
295 csdev->ect_enabled = false;
299 /* output warning if ECT enable is preventing trace operation */
301 dev_info(&csdev->dev, "Associated ECT device (%s) %s failed\n",
302 dev_name(&ect_csdev->dev),
303 enable ? "enable" : "disable");
308 * Set the associated ect / cti device while holding the coresight_mutex
309 * to avoid a race with coresight_enable that may try to use this value.
311 void coresight_set_assoc_ectdev_mutex(struct coresight_device *csdev,
312 struct coresight_device *ect_csdev)
314 mutex_lock(&coresight_mutex);
315 csdev->ect_dev = ect_csdev;
316 mutex_unlock(&coresight_mutex);
318 EXPORT_SYMBOL_GPL(coresight_set_assoc_ectdev_mutex);
320 static int coresight_enable_sink(struct coresight_device *csdev,
321 u32 mode, void *data)
326 * We need to make sure the "new" session is compatible with the
327 * existing "mode" of operation.
329 if (!sink_ops(csdev)->enable)
332 ret = coresight_control_assoc_ectdev(csdev, true);
335 ret = sink_ops(csdev)->enable(csdev, mode, data);
337 coresight_control_assoc_ectdev(csdev, false);
340 csdev->enable = true;
345 static void coresight_disable_sink(struct coresight_device *csdev)
349 if (!sink_ops(csdev)->disable)
352 ret = sink_ops(csdev)->disable(csdev);
355 coresight_control_assoc_ectdev(csdev, false);
356 csdev->enable = false;
359 static int coresight_enable_link(struct coresight_device *csdev,
360 struct coresight_device *parent,
361 struct coresight_device *child)
367 if (!parent || !child)
370 inport = coresight_find_link_inport(csdev, parent);
371 outport = coresight_find_link_outport(csdev, child);
372 link_subtype = csdev->subtype.link_subtype;
374 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && inport < 0)
376 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && outport < 0)
379 if (link_ops(csdev)->enable) {
380 ret = coresight_control_assoc_ectdev(csdev, true);
382 ret = link_ops(csdev)->enable(csdev, inport, outport);
384 coresight_control_assoc_ectdev(csdev, false);
389 csdev->enable = true;
394 static void coresight_disable_link(struct coresight_device *csdev,
395 struct coresight_device *parent,
396 struct coresight_device *child)
402 if (!parent || !child)
405 inport = coresight_find_link_inport(csdev, parent);
406 outport = coresight_find_link_outport(csdev, child);
407 link_subtype = csdev->subtype.link_subtype;
409 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
410 nr_conns = csdev->pdata->nr_inport;
411 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
412 nr_conns = csdev->pdata->nr_outport;
417 if (link_ops(csdev)->disable) {
418 link_ops(csdev)->disable(csdev, inport, outport);
419 coresight_control_assoc_ectdev(csdev, false);
422 for (i = 0; i < nr_conns; i++)
423 if (atomic_read(&csdev->refcnt[i]) != 0)
426 csdev->enable = false;
429 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
433 if (!coresight_source_is_unique(csdev)) {
434 dev_warn(&csdev->dev, "traceID %d not unique\n",
435 source_ops(csdev)->trace_id(csdev));
439 if (!csdev->enable) {
440 if (source_ops(csdev)->enable) {
441 ret = coresight_control_assoc_ectdev(csdev, true);
444 ret = source_ops(csdev)->enable(csdev, NULL, mode);
446 coresight_control_assoc_ectdev(csdev, false);
450 csdev->enable = true;
453 atomic_inc(csdev->refcnt);
459 * coresight_disable_source - Drop the reference count by 1 and disable
460 * the device if there are no users left.
462 * @csdev: The coresight device to disable
464 * Returns true if the device has been disabled.
466 static bool coresight_disable_source(struct coresight_device *csdev)
468 if (atomic_dec_return(csdev->refcnt) == 0) {
469 if (source_ops(csdev)->disable)
470 source_ops(csdev)->disable(csdev, NULL);
471 coresight_control_assoc_ectdev(csdev, false);
472 csdev->enable = false;
474 return !csdev->enable;
478 * coresight_disable_path_from : Disable components in the given path beyond
479 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
482 static void coresight_disable_path_from(struct list_head *path,
483 struct coresight_node *nd)
486 struct coresight_device *csdev, *parent, *child;
489 nd = list_first_entry(path, struct coresight_node, link);
491 list_for_each_entry_continue(nd, path, link) {
496 * ETF devices are tricky... They can be a link or a sink,
497 * depending on how they are configured. If an ETF has been
498 * "activated" it will be configured as a sink, otherwise
499 * go ahead with the link configuration.
501 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
502 type = (csdev == coresight_get_sink(path)) ?
503 CORESIGHT_DEV_TYPE_SINK :
504 CORESIGHT_DEV_TYPE_LINK;
507 case CORESIGHT_DEV_TYPE_SINK:
508 coresight_disable_sink(csdev);
510 case CORESIGHT_DEV_TYPE_SOURCE:
512 * We skip the first node in the path assuming that it
513 * is the source. So we don't expect a source device in
514 * the middle of a path.
518 case CORESIGHT_DEV_TYPE_LINK:
519 parent = list_prev_entry(nd, link)->csdev;
520 child = list_next_entry(nd, link)->csdev;
521 coresight_disable_link(csdev, parent, child);
529 void coresight_disable_path(struct list_head *path)
531 coresight_disable_path_from(path, NULL);
533 EXPORT_SYMBOL_GPL(coresight_disable_path);
535 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
540 struct coresight_node *nd;
541 struct coresight_device *csdev, *parent, *child;
543 list_for_each_entry_reverse(nd, path, link) {
548 * ETF devices are tricky... They can be a link or a sink,
549 * depending on how they are configured. If an ETF has been
550 * "activated" it will be configured as a sink, otherwise
551 * go ahead with the link configuration.
553 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
554 type = (csdev == coresight_get_sink(path)) ?
555 CORESIGHT_DEV_TYPE_SINK :
556 CORESIGHT_DEV_TYPE_LINK;
559 case CORESIGHT_DEV_TYPE_SINK:
560 ret = coresight_enable_sink(csdev, mode, sink_data);
562 * Sink is the first component turned on. If we
563 * failed to enable the sink, there are no components
564 * that need disabling. Disabling the path here
565 * would mean we could disrupt an existing session.
570 case CORESIGHT_DEV_TYPE_SOURCE:
571 /* sources are enabled from either sysFS or Perf */
573 case CORESIGHT_DEV_TYPE_LINK:
574 parent = list_prev_entry(nd, link)->csdev;
575 child = list_next_entry(nd, link)->csdev;
576 ret = coresight_enable_link(csdev, parent, child);
588 coresight_disable_path_from(path, nd);
592 struct coresight_device *coresight_get_sink(struct list_head *path)
594 struct coresight_device *csdev;
599 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
600 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
601 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
607 static struct coresight_device *
608 coresight_find_enabled_sink(struct coresight_device *csdev)
611 struct coresight_device *sink = NULL;
613 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
614 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
619 * Recursively explore each port found on this element.
621 for (i = 0; i < csdev->pdata->nr_outport; i++) {
622 struct coresight_device *child_dev;
624 child_dev = csdev->pdata->conns[i].child_dev;
626 sink = coresight_find_enabled_sink(child_dev);
635 * coresight_get_enabled_sink - returns the first enabled sink using
636 * connection based search starting from the source reference
638 * @source: Coresight source device reference
640 struct coresight_device *
641 coresight_get_enabled_sink(struct coresight_device *source)
646 return coresight_find_enabled_sink(source);
649 static int coresight_sink_by_id(struct device *dev, const void *data)
651 struct coresight_device *csdev = to_coresight_device(dev);
654 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
655 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
660 * See function etm_perf_add_symlink_sink() to know where
663 hash = (unsigned long)csdev->ea->var;
665 if ((u32)hash == *(u32 *)data)
673 * coresight_get_sink_by_id - returns the sink that matches the id
674 * @id: Id of the sink to match
676 * The name of a sink is unique, whether it is found on the AMBA bus or
677 * otherwise. As such the hash of that name can easily be used to identify
680 struct coresight_device *coresight_get_sink_by_id(u32 id)
682 struct device *dev = NULL;
684 dev = bus_find_device(&coresight_bustype, NULL, &id,
685 coresight_sink_by_id);
687 return dev ? to_coresight_device(dev) : NULL;
691 * coresight_get_ref- Helper function to increase reference count to module
694 * @csdev: The coresight device to get a reference on.
696 * Return true in successful case and power up the device.
697 * Return false when failed to get reference of module.
699 static inline bool coresight_get_ref(struct coresight_device *csdev)
701 struct device *dev = csdev->dev.parent;
703 /* Make sure the driver can't be removed */
704 if (!try_module_get(dev->driver->owner))
706 /* Make sure the device can't go away */
708 pm_runtime_get_sync(dev);
713 * coresight_put_ref- Helper function to decrease reference count to module
714 * and device. Power off the device.
716 * @csdev: The coresight device to decrement a reference from.
718 static inline void coresight_put_ref(struct coresight_device *csdev)
720 struct device *dev = csdev->dev.parent;
724 module_put(dev->driver->owner);
728 * coresight_grab_device - Power up this device and any of the helper
729 * devices connected to it for trace operation. Since the helper devices
730 * don't appear on the trace path, they should be handled along with the
733 static int coresight_grab_device(struct coresight_device *csdev)
737 for (i = 0; i < csdev->pdata->nr_outport; i++) {
738 struct coresight_device *child;
740 child = csdev->pdata->conns[i].child_dev;
741 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
742 if (!coresight_get_ref(child))
745 if (coresight_get_ref(csdev))
748 for (i--; i >= 0; i--) {
749 struct coresight_device *child;
751 child = csdev->pdata->conns[i].child_dev;
752 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
753 coresight_put_ref(child);
759 * coresight_drop_device - Release this device and any of the helper
760 * devices connected to it.
762 static void coresight_drop_device(struct coresight_device *csdev)
766 coresight_put_ref(csdev);
767 for (i = 0; i < csdev->pdata->nr_outport; i++) {
768 struct coresight_device *child;
770 child = csdev->pdata->conns[i].child_dev;
771 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
772 coresight_put_ref(child);
777 * _coresight_build_path - recursively build a path from a @csdev to a sink.
778 * @csdev: The device to start from.
779 * @sink: The final sink we want in this path.
780 * @path: The list to add devices to.
782 * The tree of Coresight device is traversed until an activated sink is
783 * found. From there the sink is added to the list along with all the
784 * devices that led to that point - the end result is a list from source
785 * to sink. In that list the source is the first device and the sink the
788 static int _coresight_build_path(struct coresight_device *csdev,
789 struct coresight_device *sink,
790 struct list_head *path)
794 struct coresight_node *node;
796 /* An activated sink has been found. Enqueue the element */
800 if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&
801 sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {
802 if (_coresight_build_path(sink, sink, path) == 0) {
808 /* Not a sink - recursively explore each port found on this element */
809 for (i = 0; i < csdev->pdata->nr_outport; i++) {
810 struct coresight_device *child_dev;
812 child_dev = csdev->pdata->conns[i].child_dev;
814 _coresight_build_path(child_dev, sink, path) == 0) {
825 * A path from this element to a sink has been found. The elements
826 * leading to the sink are already enqueued, all that is left to do
827 * is tell the PM runtime core we need this element and add a node
830 ret = coresight_grab_device(csdev);
834 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
839 list_add(&node->link, path);
844 struct list_head *coresight_build_path(struct coresight_device *source,
845 struct coresight_device *sink)
847 struct list_head *path;
851 return ERR_PTR(-EINVAL);
853 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
855 return ERR_PTR(-ENOMEM);
857 INIT_LIST_HEAD(path);
859 rc = _coresight_build_path(source, sink, path);
869 * coresight_release_path - release a previously built path.
870 * @path: the path to release.
872 * Go through all the elements of a path and 1) removed it from the list and
873 * 2) free the memory allocated for each node.
875 void coresight_release_path(struct list_head *path)
877 struct coresight_device *csdev;
878 struct coresight_node *nd, *next;
880 list_for_each_entry_safe(nd, next, path, link) {
883 coresight_drop_device(csdev);
891 /* return true if the device is a suitable type for a default sink */
892 static inline bool coresight_is_def_sink_type(struct coresight_device *csdev)
894 /* sink & correct subtype */
895 if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
896 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
897 (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
903 * coresight_select_best_sink - return the best sink for use as default from
906 * @sink: current best sink.
907 * @depth: search depth where current sink was found.
908 * @new_sink: new sink for comparison with current sink.
909 * @new_depth: search depth where new sink was found.
911 * Sinks prioritised according to coresight_dev_subtype_sink, with only
912 * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used.
914 * Where two sinks of equal priority are found, the sink closest to the
915 * source is used (smallest search depth).
917 * return @new_sink & update @depth if better than @sink, else return @sink.
919 static struct coresight_device *
920 coresight_select_best_sink(struct coresight_device *sink, int *depth,
921 struct coresight_device *new_sink, int new_depth)
926 /* first found at this level */
928 } else if (new_sink->subtype.sink_subtype >
929 sink->subtype.sink_subtype) {
930 /* found better sink */
932 } else if ((new_sink->subtype.sink_subtype ==
933 sink->subtype.sink_subtype) &&
934 (*depth > new_depth)) {
935 /* found same but closer sink */
941 return update ? new_sink : sink;
945 * coresight_find_sink - recursive function to walk trace connections from
946 * source to find a suitable default sink.
948 * @csdev: source / current device to check.
949 * @depth: [in] search depth of calling dev, [out] depth of found sink.
951 * This will walk the connection path from a source (ETM) till a suitable
952 * sink is encountered and return that sink to the original caller.
954 * If current device is a plain sink return that & depth, otherwise recursively
955 * call child connections looking for a sink. Select best possible using
956 * coresight_select_best_sink.
958 * return best sink found, or NULL if not found at this node or child nodes.
960 static struct coresight_device *
961 coresight_find_sink(struct coresight_device *csdev, int *depth)
963 int i, curr_depth = *depth + 1, found_depth = 0;
964 struct coresight_device *found_sink = NULL;
966 if (coresight_is_def_sink_type(csdev)) {
967 found_depth = curr_depth;
969 if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
970 goto return_def_sink;
971 /* look past LINKSINK for something better */
975 * Not a sink we want - or possible child sink may be better.
976 * recursively explore each port found on this element.
978 for (i = 0; i < csdev->pdata->nr_outport; i++) {
979 struct coresight_device *child_dev, *sink = NULL;
980 int child_depth = curr_depth;
982 child_dev = csdev->pdata->conns[i].child_dev;
984 sink = coresight_find_sink(child_dev, &child_depth);
987 found_sink = coresight_select_best_sink(found_sink,
994 /* return found sink and depth */
996 *depth = found_depth;
1001 * coresight_find_default_sink: Find a sink suitable for use as a
1004 * @csdev: starting source to find a connected sink.
1006 * Walks connections graph looking for a suitable sink to enable for the
1007 * supplied source. Uses CoreSight device subtypes and distance from source
1008 * to select the best sink.
1010 * If a sink is found, then the default sink for this device is set and
1011 * will be automatically used in future.
1013 * Used in cases where the CoreSight user (perf / sysfs) has not selected a
1016 struct coresight_device *
1017 coresight_find_default_sink(struct coresight_device *csdev)
1021 /* look for a default sink if we have not found for this device */
1022 if (!csdev->def_sink) {
1023 if (coresight_is_percpu_source(csdev))
1024 csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));
1025 if (!csdev->def_sink)
1026 csdev->def_sink = coresight_find_sink(csdev, &depth);
1028 return csdev->def_sink;
1031 static int coresight_remove_sink_ref(struct device *dev, void *data)
1033 struct coresight_device *sink = data;
1034 struct coresight_device *source = to_coresight_device(dev);
1036 if (source->def_sink == sink)
1037 source->def_sink = NULL;
1042 * coresight_clear_default_sink: Remove all default sink references to the
1045 * If supplied device is a sink, then check all the bus devices and clear
1046 * out all the references to this sink from the coresight_device def_sink
1049 * @csdev: coresight sink - remove references to this from all sources.
1051 static void coresight_clear_default_sink(struct coresight_device *csdev)
1053 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
1054 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
1055 bus_for_each_dev(&coresight_bustype, NULL, csdev,
1056 coresight_remove_sink_ref);
1060 /** coresight_validate_source - make sure a source has the right credentials
1061 * @csdev: the device structure for a source.
1062 * @function: the function this was called from.
1064 * Assumes the coresight_mutex is held.
1066 static int coresight_validate_source(struct coresight_device *csdev,
1067 const char *function)
1072 subtype = csdev->subtype.source_subtype;
1074 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
1075 dev_err(&csdev->dev, "wrong device type in %s\n", function);
1079 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
1080 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
1081 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
1088 int coresight_enable(struct coresight_device *csdev)
1091 struct coresight_device *sink;
1092 struct list_head *path;
1093 enum coresight_dev_subtype_source subtype;
1095 subtype = csdev->subtype.source_subtype;
1097 mutex_lock(&coresight_mutex);
1099 ret = coresight_validate_source(csdev, __func__);
1103 if (csdev->enable) {
1105 * There could be multiple applications driving the software
1106 * source. So keep the refcount for each such user when the
1107 * source is already enabled.
1109 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
1110 atomic_inc(csdev->refcnt);
1114 sink = coresight_get_enabled_sink(csdev);
1120 path = coresight_build_path(csdev, sink);
1122 pr_err("building path(s) failed\n");
1123 ret = PTR_ERR(path);
1127 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
1131 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
1136 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1138 * When working from sysFS it is important to keep track
1139 * of the paths that were created so that they can be
1140 * undone in 'coresight_disable()'. Since there can only
1141 * be a single session per tracer (when working from sysFS)
1142 * a per-cpu variable will do just fine.
1144 cpu = source_ops(csdev)->cpu_id(csdev);
1145 per_cpu(tracer_path, cpu) = path;
1147 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1151 /* We can't be here */
1156 mutex_unlock(&coresight_mutex);
1160 coresight_disable_path(path);
1163 coresight_release_path(path);
1166 EXPORT_SYMBOL_GPL(coresight_enable);
1168 void coresight_disable(struct coresight_device *csdev)
1171 struct list_head *path = NULL;
1173 mutex_lock(&coresight_mutex);
1175 ret = coresight_validate_source(csdev, __func__);
1179 if (!csdev->enable || !coresight_disable_source(csdev))
1182 switch (csdev->subtype.source_subtype) {
1183 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1184 cpu = source_ops(csdev)->cpu_id(csdev);
1185 path = per_cpu(tracer_path, cpu);
1186 per_cpu(tracer_path, cpu) = NULL;
1188 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1193 /* We can't be here */
1197 coresight_disable_path(path);
1198 coresight_release_path(path);
1201 mutex_unlock(&coresight_mutex);
1203 EXPORT_SYMBOL_GPL(coresight_disable);
1205 static ssize_t enable_sink_show(struct device *dev,
1206 struct device_attribute *attr, char *buf)
1208 struct coresight_device *csdev = to_coresight_device(dev);
1210 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
1213 static ssize_t enable_sink_store(struct device *dev,
1214 struct device_attribute *attr,
1215 const char *buf, size_t size)
1219 struct coresight_device *csdev = to_coresight_device(dev);
1221 ret = kstrtoul(buf, 10, &val);
1226 csdev->activated = true;
1228 csdev->activated = false;
1233 static DEVICE_ATTR_RW(enable_sink);
1235 static ssize_t enable_source_show(struct device *dev,
1236 struct device_attribute *attr, char *buf)
1238 struct coresight_device *csdev = to_coresight_device(dev);
1240 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
1243 static ssize_t enable_source_store(struct device *dev,
1244 struct device_attribute *attr,
1245 const char *buf, size_t size)
1249 struct coresight_device *csdev = to_coresight_device(dev);
1251 ret = kstrtoul(buf, 10, &val);
1256 ret = coresight_enable(csdev);
1260 coresight_disable(csdev);
1265 static DEVICE_ATTR_RW(enable_source);
1267 static struct attribute *coresight_sink_attrs[] = {
1268 &dev_attr_enable_sink.attr,
1271 ATTRIBUTE_GROUPS(coresight_sink);
1273 static struct attribute *coresight_source_attrs[] = {
1274 &dev_attr_enable_source.attr,
1277 ATTRIBUTE_GROUPS(coresight_source);
1279 static struct device_type coresight_dev_type[] = {
1285 .groups = coresight_sink_groups,
1292 .groups = coresight_sink_groups,
1296 .groups = coresight_source_groups,
1306 static void coresight_device_release(struct device *dev)
1308 struct coresight_device *csdev = to_coresight_device(dev);
1310 fwnode_handle_put(csdev->dev.fwnode);
1311 kfree(csdev->refcnt);
1315 static int coresight_orphan_match(struct device *dev, void *data)
1318 bool still_orphan = false;
1319 struct coresight_device *csdev, *i_csdev;
1320 struct coresight_connection *conn;
1323 i_csdev = to_coresight_device(dev);
1325 /* No need to check oneself */
1326 if (csdev == i_csdev)
1329 /* Move on to another component if no connection is orphan */
1330 if (!i_csdev->orphan)
1333 * Circle throuch all the connection of that component. If we find
1334 * an orphan connection whose name matches @csdev, link it.
1336 for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
1337 conn = &i_csdev->pdata->conns[i];
1339 /* Skip the port if FW doesn't describe it */
1340 if (!conn->child_fwnode)
1342 /* We have found at least one orphan connection */
1343 if (conn->child_dev == NULL) {
1344 /* Does it match this newly added device? */
1345 if (conn->child_fwnode == csdev->dev.fwnode) {
1346 ret = coresight_make_links(i_csdev,
1351 /* This component still has an orphan */
1352 still_orphan = true;
1357 i_csdev->orphan = still_orphan;
1360 * Returning '0' in case we didn't encounter any error,
1361 * ensures that all known component on the bus will be checked.
1366 static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
1368 return bus_for_each_dev(&coresight_bustype, NULL,
1369 csdev, coresight_orphan_match);
1373 static int coresight_fixup_device_conns(struct coresight_device *csdev)
1377 for (i = 0; i < csdev->pdata->nr_outport; i++) {
1378 struct coresight_connection *conn = &csdev->pdata->conns[i];
1380 if (!conn->child_fwnode)
1383 coresight_find_csdev_by_fwnode(conn->child_fwnode);
1384 if (conn->child_dev) {
1385 ret = coresight_make_links(csdev, conn,
1390 csdev->orphan = true;
1397 static int coresight_remove_match(struct device *dev, void *data)
1400 struct coresight_device *csdev, *iterator;
1401 struct coresight_connection *conn;
1404 iterator = to_coresight_device(dev);
1406 /* No need to check oneself */
1407 if (csdev == iterator)
1411 * Circle throuch all the connection of that component. If we find
1412 * a connection whose name matches @csdev, remove it.
1414 for (i = 0; i < iterator->pdata->nr_outport; i++) {
1415 conn = &iterator->pdata->conns[i];
1417 if (conn->child_dev == NULL || conn->child_fwnode == NULL)
1420 if (csdev->dev.fwnode == conn->child_fwnode) {
1421 iterator->orphan = true;
1422 coresight_remove_links(iterator, conn);
1424 * Drop the reference to the handle for the remote
1425 * device acquired in parsing the connections from
1428 fwnode_handle_put(conn->child_fwnode);
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;
1577 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1583 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1584 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1585 link_subtype = desc->subtype.link_subtype;
1587 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1588 nr_refcnts = desc->pdata->nr_inport;
1589 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1590 nr_refcnts = desc->pdata->nr_outport;
1593 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1596 goto err_free_csdev;
1599 csdev->refcnt = refcnts;
1601 csdev->pdata = desc->pdata;
1603 csdev->type = desc->type;
1604 csdev->subtype = desc->subtype;
1605 csdev->ops = desc->ops;
1606 csdev->access = desc->access;
1607 csdev->orphan = false;
1609 csdev->dev.type = &coresight_dev_type[desc->type];
1610 csdev->dev.groups = desc->groups;
1611 csdev->dev.parent = desc->dev;
1612 csdev->dev.release = coresight_device_release;
1613 csdev->dev.bus = &coresight_bustype;
1615 * Hold the reference to our parent device. This will be
1616 * dropped only in coresight_device_release().
1618 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1619 dev_set_name(&csdev->dev, "%s", desc->name);
1621 ret = device_register(&csdev->dev);
1623 put_device(&csdev->dev);
1625 * All resources are free'd explicitly via
1626 * coresight_device_release(), triggered from put_device().
1631 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1632 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1633 ret = etm_perf_add_symlink_sink(csdev);
1636 device_unregister(&csdev->dev);
1638 * As with the above, all resources are free'd
1639 * explicitly via coresight_device_release() triggered
1640 * from put_device(), which is in turn called from
1641 * function device_unregister().
1647 mutex_lock(&coresight_mutex);
1649 ret = coresight_create_conns_sysfs_group(csdev);
1651 ret = coresight_fixup_device_conns(csdev);
1653 ret = coresight_fixup_orphan_conns(csdev);
1654 if (!ret && cti_assoc_ops && cti_assoc_ops->add)
1655 cti_assoc_ops->add(csdev);
1657 mutex_unlock(&coresight_mutex);
1659 coresight_unregister(csdev);
1660 return ERR_PTR(ret);
1668 /* Cleanup the connection information */
1669 coresight_release_platform_data(NULL, desc->pdata);
1670 return ERR_PTR(ret);
1672 EXPORT_SYMBOL_GPL(coresight_register);
1674 void coresight_unregister(struct coresight_device *csdev)
1676 etm_perf_del_symlink_sink(csdev);
1677 /* Remove references of that device in the topology */
1678 if (cti_assoc_ops && cti_assoc_ops->remove)
1679 cti_assoc_ops->remove(csdev);
1680 coresight_remove_conns(csdev);
1681 coresight_clear_default_sink(csdev);
1682 coresight_release_platform_data(csdev, csdev->pdata);
1683 device_unregister(&csdev->dev);
1685 EXPORT_SYMBOL_GPL(coresight_unregister);
1689 * coresight_search_device_idx - Search the fwnode handle of a device
1690 * in the given dev_idx list. Must be called with the coresight_mutex held.
1692 * Returns the index of the entry, when found. Otherwise, -ENOENT.
1694 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1695 struct fwnode_handle *fwnode)
1699 for (i = 0; i < dict->nr_idx; i++)
1700 if (dict->fwnode_list[i] == fwnode)
1705 bool coresight_loses_context_with_cpu(struct device *dev)
1707 return fwnode_property_present(dev_fwnode(dev),
1708 "arm,coresight-loses-context-with-cpu");
1710 EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
1713 * coresight_alloc_device_name - Get an index for a given device in the
1714 * device index list specific to a driver. An index is allocated for a
1715 * device and is tracked with the fwnode_handle to prevent allocating
1716 * duplicate indices for the same device (e.g, if we defer probing of
1717 * a device due to dependencies), in case the index is requested again.
1719 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1724 struct fwnode_handle **list;
1726 mutex_lock(&coresight_mutex);
1728 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1730 /* Make space for the new entry */
1732 list = krealloc_array(dict->fwnode_list,
1733 idx + 1, sizeof(*dict->fwnode_list),
1735 if (ZERO_OR_NULL_PTR(list)) {
1740 list[idx] = dev_fwnode(dev);
1741 dict->fwnode_list = list;
1742 dict->nr_idx = idx + 1;
1745 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1747 mutex_unlock(&coresight_mutex);
1750 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1752 struct bus_type coresight_bustype = {
1753 .name = "coresight",
1756 static int __init coresight_init(void)
1760 ret = bus_register(&coresight_bustype);
1764 ret = etm_perf_init();
1766 bus_unregister(&coresight_bustype);
1771 static void __exit coresight_exit(void)
1774 bus_unregister(&coresight_bustype);
1777 module_init(coresight_init);
1778 module_exit(coresight_exit);
1780 MODULE_LICENSE("GPL v2");
1781 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1782 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1783 MODULE_DESCRIPTION("Arm CoreSight tracer driver");