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"
24 #include "coresight-syscfg.h"
26 static DEFINE_MUTEX(coresight_mutex);
27 static DEFINE_PER_CPU(struct coresight_device *, csdev_sink);
30 * struct coresight_node - elements of a path, from source to sink
31 * @csdev: Address of an element.
32 * @link: hook to the list.
34 struct coresight_node {
35 struct coresight_device *csdev;
36 struct list_head link;
40 * When operating Coresight drivers from the sysFS interface, only a single
41 * path can exist from a tracer (associated to a CPU) to a sink.
43 static DEFINE_PER_CPU(struct list_head *, tracer_path);
46 * As of this writing only a single STM can be found in CS topologies. Since
47 * there is no way to know if we'll ever see more and what kind of
48 * configuration they will enact, for the time being only define a single path
51 static struct list_head *stm_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 void coresight_set_cti_ops(const struct cti_assoc_op *cti_op)
65 cti_assoc_ops = cti_op;
67 EXPORT_SYMBOL_GPL(coresight_set_cti_ops);
69 void coresight_remove_cti_ops(void)
73 EXPORT_SYMBOL_GPL(coresight_remove_cti_ops);
75 void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev)
77 per_cpu(csdev_sink, cpu) = csdev;
79 EXPORT_SYMBOL_GPL(coresight_set_percpu_sink);
81 struct coresight_device *coresight_get_percpu_sink(int cpu)
83 return per_cpu(csdev_sink, cpu);
85 EXPORT_SYMBOL_GPL(coresight_get_percpu_sink);
87 static int coresight_id_match(struct device *dev, void *data)
89 int trace_id, i_trace_id;
90 struct coresight_device *csdev, *i_csdev;
93 i_csdev = to_coresight_device(dev);
96 * No need to care about oneself and components that are not
97 * sources or not enabled
99 if (i_csdev == csdev || !i_csdev->enable ||
100 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
103 /* Get the source ID for both components */
104 trace_id = source_ops(csdev)->trace_id(csdev);
105 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
107 /* All you need is one */
108 if (trace_id == i_trace_id)
114 static int coresight_source_is_unique(struct coresight_device *csdev)
116 int trace_id = source_ops(csdev)->trace_id(csdev);
118 /* this shouldn't happen */
122 return !bus_for_each_dev(&coresight_bustype, NULL,
123 csdev, coresight_id_match);
126 static int coresight_find_link_inport(struct coresight_device *csdev,
127 struct coresight_device *parent)
130 struct coresight_connection *conn;
132 for (i = 0; i < parent->pdata->nr_outport; i++) {
133 conn = &parent->pdata->conns[i];
134 if (conn->child_dev == csdev)
135 return conn->child_port;
138 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
139 dev_name(&parent->dev), dev_name(&csdev->dev));
144 static int coresight_find_link_outport(struct coresight_device *csdev,
145 struct coresight_device *child)
148 struct coresight_connection *conn;
150 for (i = 0; i < csdev->pdata->nr_outport; i++) {
151 conn = &csdev->pdata->conns[i];
152 if (conn->child_dev == child)
153 return conn->outport;
156 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
157 dev_name(&csdev->dev), dev_name(&child->dev));
162 static inline u32 coresight_read_claim_tags(struct coresight_device *csdev)
164 return csdev_access_relaxed_read32(&csdev->access, CORESIGHT_CLAIMCLR);
167 static inline bool coresight_is_claimed_self_hosted(struct coresight_device *csdev)
169 return coresight_read_claim_tags(csdev) == CORESIGHT_CLAIM_SELF_HOSTED;
172 static inline bool coresight_is_claimed_any(struct coresight_device *csdev)
174 return coresight_read_claim_tags(csdev) != 0;
177 static inline void coresight_set_claim_tags(struct coresight_device *csdev)
179 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
184 static inline void coresight_clear_claim_tags(struct coresight_device *csdev)
186 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
192 * coresight_claim_device_unlocked : Claim the device for self-hosted usage
193 * to prevent an external tool from touching this device. As per PSCI
194 * standards, section "Preserving the execution context" => "Debug and Trace
195 * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
196 * DBGCLAIM[0] is reserved for external tools.
198 * Called with CS_UNLOCKed for the component.
199 * Returns : 0 on success
201 int coresight_claim_device_unlocked(struct coresight_device *csdev)
206 if (coresight_is_claimed_any(csdev))
209 coresight_set_claim_tags(csdev);
210 if (coresight_is_claimed_self_hosted(csdev))
212 /* There was a race setting the tags, clean up and fail */
213 coresight_clear_claim_tags(csdev);
216 EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked);
218 int coresight_claim_device(struct coresight_device *csdev)
225 CS_UNLOCK(csdev->access.base);
226 rc = coresight_claim_device_unlocked(csdev);
227 CS_LOCK(csdev->access.base);
231 EXPORT_SYMBOL_GPL(coresight_claim_device);
234 * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
235 * Called with CS_UNLOCKed for the component.
237 void coresight_disclaim_device_unlocked(struct coresight_device *csdev)
243 if (coresight_is_claimed_self_hosted(csdev))
244 coresight_clear_claim_tags(csdev);
247 * The external agent may have not honoured our claim
248 * and has manipulated it. Or something else has seriously
249 * gone wrong in our driver.
253 EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked);
255 void coresight_disclaim_device(struct coresight_device *csdev)
260 CS_UNLOCK(csdev->access.base);
261 coresight_disclaim_device_unlocked(csdev);
262 CS_LOCK(csdev->access.base);
264 EXPORT_SYMBOL_GPL(coresight_disclaim_device);
266 /* enable or disable an associated CTI device of the supplied CS device */
268 coresight_control_assoc_ectdev(struct coresight_device *csdev, bool enable)
271 struct coresight_device *ect_csdev = csdev->ect_dev;
276 if ((!ect_ops(ect_csdev)->enable) || (!ect_ops(ect_csdev)->disable))
279 mod = ect_csdev->dev.parent->driver->owner;
281 if (try_module_get(mod)) {
282 ect_ret = ect_ops(ect_csdev)->enable(ect_csdev);
286 get_device(ect_csdev->dev.parent);
287 csdev->ect_enabled = true;
292 if (csdev->ect_enabled) {
293 ect_ret = ect_ops(ect_csdev)->disable(ect_csdev);
294 put_device(ect_csdev->dev.parent);
296 csdev->ect_enabled = false;
300 /* output warning if ECT enable is preventing trace operation */
302 dev_info(&csdev->dev, "Associated ECT device (%s) %s failed\n",
303 dev_name(&ect_csdev->dev),
304 enable ? "enable" : "disable");
309 * Set the associated ect / cti device while holding the coresight_mutex
310 * to avoid a race with coresight_enable that may try to use this value.
312 void coresight_set_assoc_ectdev_mutex(struct coresight_device *csdev,
313 struct coresight_device *ect_csdev)
315 mutex_lock(&coresight_mutex);
316 csdev->ect_dev = ect_csdev;
317 mutex_unlock(&coresight_mutex);
319 EXPORT_SYMBOL_GPL(coresight_set_assoc_ectdev_mutex);
321 static int coresight_enable_sink(struct coresight_device *csdev,
322 u32 mode, void *data)
327 * We need to make sure the "new" session is compatible with the
328 * existing "mode" of operation.
330 if (!sink_ops(csdev)->enable)
333 ret = coresight_control_assoc_ectdev(csdev, true);
336 ret = sink_ops(csdev)->enable(csdev, mode, data);
338 coresight_control_assoc_ectdev(csdev, false);
341 csdev->enable = true;
346 static void coresight_disable_sink(struct coresight_device *csdev)
350 if (!sink_ops(csdev)->disable)
353 ret = sink_ops(csdev)->disable(csdev);
356 coresight_control_assoc_ectdev(csdev, false);
357 csdev->enable = false;
360 static int coresight_enable_link(struct coresight_device *csdev,
361 struct coresight_device *parent,
362 struct coresight_device *child)
368 if (!parent || !child)
371 inport = coresight_find_link_inport(csdev, parent);
372 outport = coresight_find_link_outport(csdev, child);
373 link_subtype = csdev->subtype.link_subtype;
375 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && inport < 0)
377 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && outport < 0)
380 if (link_ops(csdev)->enable) {
381 ret = coresight_control_assoc_ectdev(csdev, true);
383 ret = link_ops(csdev)->enable(csdev, inport, outport);
385 coresight_control_assoc_ectdev(csdev, false);
390 csdev->enable = true;
395 static void coresight_disable_link(struct coresight_device *csdev,
396 struct coresight_device *parent,
397 struct coresight_device *child)
403 if (!parent || !child)
406 inport = coresight_find_link_inport(csdev, parent);
407 outport = coresight_find_link_outport(csdev, child);
408 link_subtype = csdev->subtype.link_subtype;
410 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
411 nr_conns = csdev->pdata->nr_inport;
412 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
413 nr_conns = csdev->pdata->nr_outport;
418 if (link_ops(csdev)->disable) {
419 link_ops(csdev)->disable(csdev, inport, outport);
420 coresight_control_assoc_ectdev(csdev, false);
423 for (i = 0; i < nr_conns; i++)
424 if (atomic_read(&csdev->refcnt[i]) != 0)
427 csdev->enable = false;
430 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
434 if (!coresight_source_is_unique(csdev)) {
435 dev_warn(&csdev->dev, "traceID %d not unique\n",
436 source_ops(csdev)->trace_id(csdev));
440 if (!csdev->enable) {
441 if (source_ops(csdev)->enable) {
442 ret = coresight_control_assoc_ectdev(csdev, true);
445 ret = source_ops(csdev)->enable(csdev, NULL, mode);
447 coresight_control_assoc_ectdev(csdev, false);
451 csdev->enable = true;
454 atomic_inc(csdev->refcnt);
460 * coresight_disable_source - Drop the reference count by 1 and disable
461 * the device if there are no users left.
463 * @csdev: The coresight device to disable
465 * Returns true if the device has been disabled.
467 static bool coresight_disable_source(struct coresight_device *csdev)
469 if (atomic_dec_return(csdev->refcnt) == 0) {
470 if (source_ops(csdev)->disable)
471 source_ops(csdev)->disable(csdev, NULL);
472 coresight_control_assoc_ectdev(csdev, false);
473 csdev->enable = false;
475 return !csdev->enable;
479 * coresight_disable_path_from : Disable components in the given path beyond
480 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
483 static void coresight_disable_path_from(struct list_head *path,
484 struct coresight_node *nd)
487 struct coresight_device *csdev, *parent, *child;
490 nd = list_first_entry(path, struct coresight_node, link);
492 list_for_each_entry_continue(nd, path, link) {
497 * ETF devices are tricky... They can be a link or a sink,
498 * depending on how they are configured. If an ETF has been
499 * "activated" it will be configured as a sink, otherwise
500 * go ahead with the link configuration.
502 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
503 type = (csdev == coresight_get_sink(path)) ?
504 CORESIGHT_DEV_TYPE_SINK :
505 CORESIGHT_DEV_TYPE_LINK;
508 case CORESIGHT_DEV_TYPE_SINK:
509 coresight_disable_sink(csdev);
511 case CORESIGHT_DEV_TYPE_SOURCE:
513 * We skip the first node in the path assuming that it
514 * is the source. So we don't expect a source device in
515 * the middle of a path.
519 case CORESIGHT_DEV_TYPE_LINK:
520 parent = list_prev_entry(nd, link)->csdev;
521 child = list_next_entry(nd, link)->csdev;
522 coresight_disable_link(csdev, parent, child);
530 void coresight_disable_path(struct list_head *path)
532 coresight_disable_path_from(path, NULL);
534 EXPORT_SYMBOL_GPL(coresight_disable_path);
536 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
541 struct coresight_node *nd;
542 struct coresight_device *csdev, *parent, *child;
544 list_for_each_entry_reverse(nd, path, link) {
549 * ETF devices are tricky... They can be a link or a sink,
550 * depending on how they are configured. If an ETF has been
551 * "activated" it will be configured as a sink, otherwise
552 * go ahead with the link configuration.
554 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
555 type = (csdev == coresight_get_sink(path)) ?
556 CORESIGHT_DEV_TYPE_SINK :
557 CORESIGHT_DEV_TYPE_LINK;
560 case CORESIGHT_DEV_TYPE_SINK:
561 ret = coresight_enable_sink(csdev, mode, sink_data);
563 * Sink is the first component turned on. If we
564 * failed to enable the sink, there are no components
565 * that need disabling. Disabling the path here
566 * would mean we could disrupt an existing session.
571 case CORESIGHT_DEV_TYPE_SOURCE:
572 /* sources are enabled from either sysFS or Perf */
574 case CORESIGHT_DEV_TYPE_LINK:
575 parent = list_prev_entry(nd, link)->csdev;
576 child = list_next_entry(nd, link)->csdev;
577 ret = coresight_enable_link(csdev, parent, child);
589 coresight_disable_path_from(path, nd);
593 struct coresight_device *coresight_get_sink(struct list_head *path)
595 struct coresight_device *csdev;
600 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
601 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
602 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
608 static struct coresight_device *
609 coresight_find_enabled_sink(struct coresight_device *csdev)
612 struct coresight_device *sink = NULL;
614 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
615 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
620 * Recursively explore each port found on this element.
622 for (i = 0; i < csdev->pdata->nr_outport; i++) {
623 struct coresight_device *child_dev;
625 child_dev = csdev->pdata->conns[i].child_dev;
627 sink = coresight_find_enabled_sink(child_dev);
636 * coresight_get_enabled_sink - returns the first enabled sink using
637 * connection based search starting from the source reference
639 * @source: Coresight source device reference
641 struct coresight_device *
642 coresight_get_enabled_sink(struct coresight_device *source)
647 return coresight_find_enabled_sink(source);
650 static int coresight_sink_by_id(struct device *dev, const void *data)
652 struct coresight_device *csdev = to_coresight_device(dev);
655 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
656 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
661 * See function etm_perf_add_symlink_sink() to know where
664 hash = (unsigned long)csdev->ea->var;
666 if ((u32)hash == *(u32 *)data)
674 * coresight_get_sink_by_id - returns the sink that matches the id
675 * @id: Id of the sink to match
677 * The name of a sink is unique, whether it is found on the AMBA bus or
678 * otherwise. As such the hash of that name can easily be used to identify
681 struct coresight_device *coresight_get_sink_by_id(u32 id)
683 struct device *dev = NULL;
685 dev = bus_find_device(&coresight_bustype, NULL, &id,
686 coresight_sink_by_id);
688 return dev ? to_coresight_device(dev) : NULL;
692 * coresight_get_ref- Helper function to increase reference count to module
695 * @csdev: The coresight device to get a reference on.
697 * Return true in successful case and power up the device.
698 * Return false when failed to get reference of module.
700 static inline bool coresight_get_ref(struct coresight_device *csdev)
702 struct device *dev = csdev->dev.parent;
704 /* Make sure the driver can't be removed */
705 if (!try_module_get(dev->driver->owner))
707 /* Make sure the device can't go away */
709 pm_runtime_get_sync(dev);
714 * coresight_put_ref- Helper function to decrease reference count to module
715 * and device. Power off the device.
717 * @csdev: The coresight device to decrement a reference from.
719 static inline void coresight_put_ref(struct coresight_device *csdev)
721 struct device *dev = csdev->dev.parent;
725 module_put(dev->driver->owner);
729 * coresight_grab_device - Power up this device and any of the helper
730 * devices connected to it for trace operation. Since the helper devices
731 * don't appear on the trace path, they should be handled along with the
734 static int coresight_grab_device(struct coresight_device *csdev)
738 for (i = 0; i < csdev->pdata->nr_outport; i++) {
739 struct coresight_device *child;
741 child = csdev->pdata->conns[i].child_dev;
742 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
743 if (!coresight_get_ref(child))
746 if (coresight_get_ref(csdev))
749 for (i--; i >= 0; i--) {
750 struct coresight_device *child;
752 child = csdev->pdata->conns[i].child_dev;
753 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
754 coresight_put_ref(child);
760 * coresight_drop_device - Release this device and any of the helper
761 * devices connected to it.
763 static void coresight_drop_device(struct coresight_device *csdev)
767 coresight_put_ref(csdev);
768 for (i = 0; i < csdev->pdata->nr_outport; i++) {
769 struct coresight_device *child;
771 child = csdev->pdata->conns[i].child_dev;
772 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
773 coresight_put_ref(child);
778 * _coresight_build_path - recursively build a path from a @csdev to a sink.
779 * @csdev: The device to start from.
780 * @sink: The final sink we want in this path.
781 * @path: The list to add devices to.
783 * The tree of Coresight device is traversed until an activated sink is
784 * found. From there the sink is added to the list along with all the
785 * devices that led to that point - the end result is a list from source
786 * to sink. In that list the source is the first device and the sink the
789 static int _coresight_build_path(struct coresight_device *csdev,
790 struct coresight_device *sink,
791 struct list_head *path)
795 struct coresight_node *node;
797 /* An activated sink has been found. Enqueue the element */
801 if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&
802 sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {
803 if (_coresight_build_path(sink, sink, path) == 0) {
809 /* Not a sink - recursively explore each port found on this element */
810 for (i = 0; i < csdev->pdata->nr_outport; i++) {
811 struct coresight_device *child_dev;
813 child_dev = csdev->pdata->conns[i].child_dev;
815 _coresight_build_path(child_dev, sink, path) == 0) {
826 * A path from this element to a sink has been found. The elements
827 * leading to the sink are already enqueued, all that is left to do
828 * is tell the PM runtime core we need this element and add a node
831 ret = coresight_grab_device(csdev);
835 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
840 list_add(&node->link, path);
845 struct list_head *coresight_build_path(struct coresight_device *source,
846 struct coresight_device *sink)
848 struct list_head *path;
852 return ERR_PTR(-EINVAL);
854 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
856 return ERR_PTR(-ENOMEM);
858 INIT_LIST_HEAD(path);
860 rc = _coresight_build_path(source, sink, path);
870 * coresight_release_path - release a previously built path.
871 * @path: the path to release.
873 * Go through all the elements of a path and 1) removed it from the list and
874 * 2) free the memory allocated for each node.
876 void coresight_release_path(struct list_head *path)
878 struct coresight_device *csdev;
879 struct coresight_node *nd, *next;
881 list_for_each_entry_safe(nd, next, path, link) {
884 coresight_drop_device(csdev);
892 /* return true if the device is a suitable type for a default sink */
893 static inline bool coresight_is_def_sink_type(struct coresight_device *csdev)
895 /* sink & correct subtype */
896 if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
897 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
898 (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
904 * coresight_select_best_sink - return the best sink for use as default from
907 * @sink: current best sink.
908 * @depth: search depth where current sink was found.
909 * @new_sink: new sink for comparison with current sink.
910 * @new_depth: search depth where new sink was found.
912 * Sinks prioritised according to coresight_dev_subtype_sink, with only
913 * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used.
915 * Where two sinks of equal priority are found, the sink closest to the
916 * source is used (smallest search depth).
918 * return @new_sink & update @depth if better than @sink, else return @sink.
920 static struct coresight_device *
921 coresight_select_best_sink(struct coresight_device *sink, int *depth,
922 struct coresight_device *new_sink, int new_depth)
927 /* first found at this level */
929 } else if (new_sink->subtype.sink_subtype >
930 sink->subtype.sink_subtype) {
931 /* found better sink */
933 } else if ((new_sink->subtype.sink_subtype ==
934 sink->subtype.sink_subtype) &&
935 (*depth > new_depth)) {
936 /* found same but closer sink */
942 return update ? new_sink : sink;
946 * coresight_find_sink - recursive function to walk trace connections from
947 * source to find a suitable default sink.
949 * @csdev: source / current device to check.
950 * @depth: [in] search depth of calling dev, [out] depth of found sink.
952 * This will walk the connection path from a source (ETM) till a suitable
953 * sink is encountered and return that sink to the original caller.
955 * If current device is a plain sink return that & depth, otherwise recursively
956 * call child connections looking for a sink. Select best possible using
957 * coresight_select_best_sink.
959 * return best sink found, or NULL if not found at this node or child nodes.
961 static struct coresight_device *
962 coresight_find_sink(struct coresight_device *csdev, int *depth)
964 int i, curr_depth = *depth + 1, found_depth = 0;
965 struct coresight_device *found_sink = NULL;
967 if (coresight_is_def_sink_type(csdev)) {
968 found_depth = curr_depth;
970 if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
971 goto return_def_sink;
972 /* look past LINKSINK for something better */
976 * Not a sink we want - or possible child sink may be better.
977 * recursively explore each port found on this element.
979 for (i = 0; i < csdev->pdata->nr_outport; i++) {
980 struct coresight_device *child_dev, *sink = NULL;
981 int child_depth = curr_depth;
983 child_dev = csdev->pdata->conns[i].child_dev;
985 sink = coresight_find_sink(child_dev, &child_depth);
988 found_sink = coresight_select_best_sink(found_sink,
995 /* return found sink and depth */
997 *depth = found_depth;
1002 * coresight_find_default_sink: Find a sink suitable for use as a
1005 * @csdev: starting source to find a connected sink.
1007 * Walks connections graph looking for a suitable sink to enable for the
1008 * supplied source. Uses CoreSight device subtypes and distance from source
1009 * to select the best sink.
1011 * If a sink is found, then the default sink for this device is set and
1012 * will be automatically used in future.
1014 * Used in cases where the CoreSight user (perf / sysfs) has not selected a
1017 struct coresight_device *
1018 coresight_find_default_sink(struct coresight_device *csdev)
1022 /* look for a default sink if we have not found for this device */
1023 if (!csdev->def_sink) {
1024 if (coresight_is_percpu_source(csdev))
1025 csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));
1026 if (!csdev->def_sink)
1027 csdev->def_sink = coresight_find_sink(csdev, &depth);
1029 return csdev->def_sink;
1032 static int coresight_remove_sink_ref(struct device *dev, void *data)
1034 struct coresight_device *sink = data;
1035 struct coresight_device *source = to_coresight_device(dev);
1037 if (source->def_sink == sink)
1038 source->def_sink = NULL;
1043 * coresight_clear_default_sink: Remove all default sink references to the
1046 * If supplied device is a sink, then check all the bus devices and clear
1047 * out all the references to this sink from the coresight_device def_sink
1050 * @csdev: coresight sink - remove references to this from all sources.
1052 static void coresight_clear_default_sink(struct coresight_device *csdev)
1054 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
1055 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
1056 bus_for_each_dev(&coresight_bustype, NULL, csdev,
1057 coresight_remove_sink_ref);
1061 /** coresight_validate_source - make sure a source has the right credentials
1062 * @csdev: the device structure for a source.
1063 * @function: the function this was called from.
1065 * Assumes the coresight_mutex is held.
1067 static int coresight_validate_source(struct coresight_device *csdev,
1068 const char *function)
1073 subtype = csdev->subtype.source_subtype;
1075 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
1076 dev_err(&csdev->dev, "wrong device type in %s\n", function);
1080 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
1081 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
1082 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
1089 int coresight_enable(struct coresight_device *csdev)
1092 struct coresight_device *sink;
1093 struct list_head *path;
1094 enum coresight_dev_subtype_source subtype;
1096 subtype = csdev->subtype.source_subtype;
1098 mutex_lock(&coresight_mutex);
1100 ret = coresight_validate_source(csdev, __func__);
1104 if (csdev->enable) {
1106 * There could be multiple applications driving the software
1107 * source. So keep the refcount for each such user when the
1108 * source is already enabled.
1110 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
1111 atomic_inc(csdev->refcnt);
1115 sink = coresight_get_enabled_sink(csdev);
1121 path = coresight_build_path(csdev, sink);
1123 pr_err("building path(s) failed\n");
1124 ret = PTR_ERR(path);
1128 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
1132 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
1137 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1139 * When working from sysFS it is important to keep track
1140 * of the paths that were created so that they can be
1141 * undone in 'coresight_disable()'. Since there can only
1142 * be a single session per tracer (when working from sysFS)
1143 * a per-cpu variable will do just fine.
1145 cpu = source_ops(csdev)->cpu_id(csdev);
1146 per_cpu(tracer_path, cpu) = path;
1148 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1152 /* We can't be here */
1157 mutex_unlock(&coresight_mutex);
1161 coresight_disable_path(path);
1164 coresight_release_path(path);
1167 EXPORT_SYMBOL_GPL(coresight_enable);
1169 void coresight_disable(struct coresight_device *csdev)
1172 struct list_head *path = NULL;
1174 mutex_lock(&coresight_mutex);
1176 ret = coresight_validate_source(csdev, __func__);
1180 if (!csdev->enable || !coresight_disable_source(csdev))
1183 switch (csdev->subtype.source_subtype) {
1184 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1185 cpu = source_ops(csdev)->cpu_id(csdev);
1186 path = per_cpu(tracer_path, cpu);
1187 per_cpu(tracer_path, cpu) = NULL;
1189 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1194 /* We can't be here */
1198 coresight_disable_path(path);
1199 coresight_release_path(path);
1202 mutex_unlock(&coresight_mutex);
1204 EXPORT_SYMBOL_GPL(coresight_disable);
1206 static ssize_t enable_sink_show(struct device *dev,
1207 struct device_attribute *attr, char *buf)
1209 struct coresight_device *csdev = to_coresight_device(dev);
1211 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
1214 static ssize_t enable_sink_store(struct device *dev,
1215 struct device_attribute *attr,
1216 const char *buf, size_t size)
1220 struct coresight_device *csdev = to_coresight_device(dev);
1222 ret = kstrtoul(buf, 10, &val);
1227 csdev->activated = true;
1229 csdev->activated = false;
1234 static DEVICE_ATTR_RW(enable_sink);
1236 static ssize_t enable_source_show(struct device *dev,
1237 struct device_attribute *attr, char *buf)
1239 struct coresight_device *csdev = to_coresight_device(dev);
1241 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
1244 static ssize_t enable_source_store(struct device *dev,
1245 struct device_attribute *attr,
1246 const char *buf, size_t size)
1250 struct coresight_device *csdev = to_coresight_device(dev);
1252 ret = kstrtoul(buf, 10, &val);
1257 ret = coresight_enable(csdev);
1261 coresight_disable(csdev);
1266 static DEVICE_ATTR_RW(enable_source);
1268 static struct attribute *coresight_sink_attrs[] = {
1269 &dev_attr_enable_sink.attr,
1272 ATTRIBUTE_GROUPS(coresight_sink);
1274 static struct attribute *coresight_source_attrs[] = {
1275 &dev_attr_enable_source.attr,
1278 ATTRIBUTE_GROUPS(coresight_source);
1280 static struct device_type coresight_dev_type[] = {
1283 .groups = coresight_sink_groups,
1290 .groups = coresight_sink_groups,
1294 .groups = coresight_source_groups,
1304 static void coresight_device_release(struct device *dev)
1306 struct coresight_device *csdev = to_coresight_device(dev);
1308 fwnode_handle_put(csdev->dev.fwnode);
1309 kfree(csdev->refcnt);
1313 static int coresight_orphan_match(struct device *dev, void *data)
1316 bool still_orphan = false;
1317 struct coresight_device *csdev, *i_csdev;
1318 struct coresight_connection *conn;
1321 i_csdev = to_coresight_device(dev);
1323 /* No need to check oneself */
1324 if (csdev == i_csdev)
1327 /* Move on to another component if no connection is orphan */
1328 if (!i_csdev->orphan)
1331 * Circle throuch all the connection of that component. If we find
1332 * an orphan connection whose name matches @csdev, link it.
1334 for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
1335 conn = &i_csdev->pdata->conns[i];
1337 /* Skip the port if FW doesn't describe it */
1338 if (!conn->child_fwnode)
1340 /* We have found at least one orphan connection */
1341 if (conn->child_dev == NULL) {
1342 /* Does it match this newly added device? */
1343 if (conn->child_fwnode == csdev->dev.fwnode) {
1344 ret = coresight_make_links(i_csdev,
1349 /* This component still has an orphan */
1350 still_orphan = true;
1355 i_csdev->orphan = still_orphan;
1358 * Returning '0' in case we didn't encounter any error,
1359 * ensures that all known component on the bus will be checked.
1364 static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
1366 return bus_for_each_dev(&coresight_bustype, NULL,
1367 csdev, coresight_orphan_match);
1371 static int coresight_fixup_device_conns(struct coresight_device *csdev)
1375 for (i = 0; i < csdev->pdata->nr_outport; i++) {
1376 struct coresight_connection *conn = &csdev->pdata->conns[i];
1378 if (!conn->child_fwnode)
1381 coresight_find_csdev_by_fwnode(conn->child_fwnode);
1382 if (conn->child_dev && conn->child_dev->has_conns_grp) {
1383 ret = coresight_make_links(csdev, conn,
1388 csdev->orphan = true;
1395 static int coresight_remove_match(struct device *dev, void *data)
1398 struct coresight_device *csdev, *iterator;
1399 struct coresight_connection *conn;
1402 iterator = to_coresight_device(dev);
1404 /* No need to check oneself */
1405 if (csdev == iterator)
1409 * Circle throuch all the connection of that component. If we find
1410 * a connection whose name matches @csdev, remove it.
1412 for (i = 0; i < iterator->pdata->nr_outport; i++) {
1413 conn = &iterator->pdata->conns[i];
1415 if (conn->child_dev == NULL || conn->child_fwnode == NULL)
1418 if (csdev->dev.fwnode == conn->child_fwnode) {
1419 iterator->orphan = true;
1420 coresight_remove_links(iterator, conn);
1422 * Drop the reference to the handle for the remote
1423 * device acquired in parsing the connections from
1426 fwnode_handle_put(conn->child_fwnode);
1427 /* No need to continue */
1433 * Returning '0' ensures that all known component on the
1434 * bus will be checked.
1440 * coresight_remove_conns - Remove references to this given devices
1441 * from the connections of other devices.
1443 static void coresight_remove_conns(struct coresight_device *csdev)
1446 * Another device will point to this device only if there is
1447 * an output port connected to this one. i.e, if the device
1448 * doesn't have at least one input port, there is no point
1449 * in searching all the devices.
1451 if (csdev->pdata->nr_inport)
1452 bus_for_each_dev(&coresight_bustype, NULL,
1453 csdev, coresight_remove_match);
1457 * coresight_timeout - loop until a bit has changed to a specific register
1459 * @csa: coresight device access for the device
1460 * @offset: Offset of the register from the base of the device.
1461 * @position: the position of the bit of interest.
1462 * @value: the value the bit should have.
1464 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1465 * TIMEOUT_US has elapsed, which ever happens first.
1467 int coresight_timeout(struct csdev_access *csa, u32 offset,
1468 int position, int value)
1473 for (i = TIMEOUT_US; i > 0; i--) {
1474 val = csdev_access_read32(csa, offset);
1475 /* waiting on the bit to go from 0 to 1 */
1477 if (val & BIT(position))
1479 /* waiting on the bit to go from 1 to 0 */
1481 if (!(val & BIT(position)))
1486 * Delay is arbitrary - the specification doesn't say how long
1487 * we are expected to wait. Extra check required to make sure
1488 * we don't wait needlessly on the last iteration.
1496 EXPORT_SYMBOL_GPL(coresight_timeout);
1498 u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset)
1500 return csdev_access_relaxed_read32(&csdev->access, offset);
1503 u32 coresight_read32(struct coresight_device *csdev, u32 offset)
1505 return csdev_access_read32(&csdev->access, offset);
1508 void coresight_relaxed_write32(struct coresight_device *csdev,
1509 u32 val, u32 offset)
1511 csdev_access_relaxed_write32(&csdev->access, val, offset);
1514 void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset)
1516 csdev_access_write32(&csdev->access, val, offset);
1519 u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset)
1521 return csdev_access_relaxed_read64(&csdev->access, offset);
1524 u64 coresight_read64(struct coresight_device *csdev, u32 offset)
1526 return csdev_access_read64(&csdev->access, offset);
1529 void coresight_relaxed_write64(struct coresight_device *csdev,
1530 u64 val, u32 offset)
1532 csdev_access_relaxed_write64(&csdev->access, val, offset);
1535 void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset)
1537 csdev_access_write64(&csdev->access, val, offset);
1541 * coresight_release_platform_data: Release references to the devices connected
1542 * to the output port of this device.
1544 void coresight_release_platform_data(struct coresight_device *csdev,
1545 struct coresight_platform_data *pdata)
1548 struct coresight_connection *conns = pdata->conns;
1550 for (i = 0; i < pdata->nr_outport; i++) {
1551 /* If we have made the links, remove them now */
1552 if (csdev && conns[i].child_dev)
1553 coresight_remove_links(csdev, &conns[i]);
1555 * Drop the refcount and clear the handle as this device
1558 if (conns[i].child_fwnode) {
1559 fwnode_handle_put(conns[i].child_fwnode);
1560 pdata->conns[i].child_fwnode = NULL;
1564 coresight_remove_conns_sysfs_group(csdev);
1567 struct coresight_device *coresight_register(struct coresight_desc *desc)
1572 atomic_t *refcnts = NULL;
1573 struct coresight_device *csdev;
1574 bool registered = false;
1576 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1582 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1583 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1584 link_subtype = desc->subtype.link_subtype;
1586 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1587 nr_refcnts = desc->pdata->nr_inport;
1588 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1589 nr_refcnts = desc->pdata->nr_outport;
1592 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
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);
1622 * Make sure the device registration and the connection fixup
1623 * are synchronised, so that we don't see uninitialised devices
1624 * on the coresight bus while trying to resolve the connections.
1626 mutex_lock(&coresight_mutex);
1628 ret = device_register(&csdev->dev);
1630 put_device(&csdev->dev);
1632 * All resources are free'd explicitly via
1633 * coresight_device_release(), triggered from put_device().
1638 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1639 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1640 ret = etm_perf_add_symlink_sink(csdev);
1643 device_unregister(&csdev->dev);
1645 * As with the above, all resources are free'd
1646 * explicitly via coresight_device_release() triggered
1647 * from put_device(), which is in turn called from
1648 * function device_unregister().
1653 /* Device is now registered */
1656 ret = coresight_create_conns_sysfs_group(csdev);
1658 ret = coresight_fixup_device_conns(csdev);
1660 ret = coresight_fixup_orphan_conns(csdev);
1661 if (!ret && cti_assoc_ops && cti_assoc_ops->add)
1662 cti_assoc_ops->add(csdev);
1665 mutex_unlock(&coresight_mutex);
1670 /* Unregister the device if needed */
1672 coresight_unregister(csdev);
1673 return ERR_PTR(ret);
1677 /* Cleanup the connection information */
1678 coresight_release_platform_data(NULL, desc->pdata);
1679 return ERR_PTR(ret);
1681 EXPORT_SYMBOL_GPL(coresight_register);
1683 void coresight_unregister(struct coresight_device *csdev)
1685 etm_perf_del_symlink_sink(csdev);
1686 /* Remove references of that device in the topology */
1687 if (cti_assoc_ops && cti_assoc_ops->remove)
1688 cti_assoc_ops->remove(csdev);
1689 coresight_remove_conns(csdev);
1690 coresight_clear_default_sink(csdev);
1691 coresight_release_platform_data(csdev, csdev->pdata);
1692 device_unregister(&csdev->dev);
1694 EXPORT_SYMBOL_GPL(coresight_unregister);
1698 * coresight_search_device_idx - Search the fwnode handle of a device
1699 * in the given dev_idx list. Must be called with the coresight_mutex held.
1701 * Returns the index of the entry, when found. Otherwise, -ENOENT.
1703 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1704 struct fwnode_handle *fwnode)
1708 for (i = 0; i < dict->nr_idx; i++)
1709 if (dict->fwnode_list[i] == fwnode)
1714 bool coresight_loses_context_with_cpu(struct device *dev)
1716 return fwnode_property_present(dev_fwnode(dev),
1717 "arm,coresight-loses-context-with-cpu");
1719 EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
1722 * coresight_alloc_device_name - Get an index for a given device in the
1723 * device index list specific to a driver. An index is allocated for a
1724 * device and is tracked with the fwnode_handle to prevent allocating
1725 * duplicate indices for the same device (e.g, if we defer probing of
1726 * a device due to dependencies), in case the index is requested again.
1728 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1733 struct fwnode_handle **list;
1735 mutex_lock(&coresight_mutex);
1737 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1739 /* Make space for the new entry */
1741 list = krealloc_array(dict->fwnode_list,
1742 idx + 1, sizeof(*dict->fwnode_list),
1744 if (ZERO_OR_NULL_PTR(list)) {
1749 list[idx] = dev_fwnode(dev);
1750 dict->fwnode_list = list;
1751 dict->nr_idx = idx + 1;
1754 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1756 mutex_unlock(&coresight_mutex);
1759 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1761 struct bus_type coresight_bustype = {
1762 .name = "coresight",
1765 static int __init coresight_init(void)
1769 ret = bus_register(&coresight_bustype);
1773 ret = etm_perf_init();
1775 goto exit_bus_unregister;
1777 /* initialise the coresight syscfg API */
1783 exit_bus_unregister:
1784 bus_unregister(&coresight_bustype);
1788 static void __exit coresight_exit(void)
1792 bus_unregister(&coresight_bustype);
1795 module_init(coresight_init);
1796 module_exit(coresight_exit);
1798 MODULE_LICENSE("GPL v2");
1799 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1800 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1801 MODULE_DESCRIPTION("Arm CoreSight tracer driver");