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_outconns; i++) {
122 conn = &parent->pdata->out_conns[i];
123 if (conn->dest_dev == csdev)
124 return conn->dest_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_outconns; i++) {
140 conn = &csdev->pdata->out_conns[i];
141 if (conn->dest_dev == child)
142 return conn->src_port;
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 enum cs_mode 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_inconns;
401 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
402 nr_conns = csdev->pdata->nr_outconns;
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,
424 if (!csdev->enable) {
425 if (source_ops(csdev)->enable) {
426 ret = coresight_control_assoc_ectdev(csdev, true);
429 ret = source_ops(csdev)->enable(csdev, NULL, mode);
431 coresight_control_assoc_ectdev(csdev, false);
435 csdev->enable = true;
438 atomic_inc(csdev->refcnt);
444 * coresight_disable_source - Drop the reference count by 1 and disable
445 * the device if there are no users left.
447 * @csdev: The coresight device to disable
449 * Returns true if the device has been disabled.
451 static bool coresight_disable_source(struct coresight_device *csdev)
453 if (atomic_dec_return(csdev->refcnt) == 0) {
454 if (source_ops(csdev)->disable)
455 source_ops(csdev)->disable(csdev, NULL);
456 coresight_control_assoc_ectdev(csdev, false);
457 csdev->enable = false;
459 return !csdev->enable;
463 * coresight_disable_path_from : Disable components in the given path beyond
464 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
467 static void coresight_disable_path_from(struct list_head *path,
468 struct coresight_node *nd)
471 struct coresight_device *csdev, *parent, *child;
474 nd = list_first_entry(path, struct coresight_node, link);
476 list_for_each_entry_continue(nd, path, link) {
481 * ETF devices are tricky... They can be a link or a sink,
482 * depending on how they are configured. If an ETF has been
483 * "activated" it will be configured as a sink, otherwise
484 * go ahead with the link configuration.
486 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
487 type = (csdev == coresight_get_sink(path)) ?
488 CORESIGHT_DEV_TYPE_SINK :
489 CORESIGHT_DEV_TYPE_LINK;
492 case CORESIGHT_DEV_TYPE_SINK:
493 coresight_disable_sink(csdev);
495 case CORESIGHT_DEV_TYPE_SOURCE:
497 * We skip the first node in the path assuming that it
498 * is the source. So we don't expect a source device in
499 * the middle of a path.
503 case CORESIGHT_DEV_TYPE_LINK:
504 parent = list_prev_entry(nd, link)->csdev;
505 child = list_next_entry(nd, link)->csdev;
506 coresight_disable_link(csdev, parent, child);
514 void coresight_disable_path(struct list_head *path)
516 coresight_disable_path_from(path, NULL);
518 EXPORT_SYMBOL_GPL(coresight_disable_path);
520 int coresight_enable_path(struct list_head *path, enum cs_mode mode, void *sink_data)
525 struct coresight_node *nd;
526 struct coresight_device *csdev, *parent, *child;
528 list_for_each_entry_reverse(nd, path, link) {
533 * ETF devices are tricky... They can be a link or a sink,
534 * depending on how they are configured. If an ETF has been
535 * "activated" it will be configured as a sink, otherwise
536 * go ahead with the link configuration.
538 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
539 type = (csdev == coresight_get_sink(path)) ?
540 CORESIGHT_DEV_TYPE_SINK :
541 CORESIGHT_DEV_TYPE_LINK;
544 case CORESIGHT_DEV_TYPE_SINK:
545 ret = coresight_enable_sink(csdev, mode, sink_data);
547 * Sink is the first component turned on. If we
548 * failed to enable the sink, there are no components
549 * that need disabling. Disabling the path here
550 * would mean we could disrupt an existing session.
555 case CORESIGHT_DEV_TYPE_SOURCE:
556 /* sources are enabled from either sysFS or Perf */
558 case CORESIGHT_DEV_TYPE_LINK:
559 parent = list_prev_entry(nd, link)->csdev;
560 child = list_next_entry(nd, link)->csdev;
561 ret = coresight_enable_link(csdev, parent, child);
573 coresight_disable_path_from(path, nd);
577 struct coresight_device *coresight_get_sink(struct list_head *path)
579 struct coresight_device *csdev;
584 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
585 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
586 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
592 static struct coresight_device *
593 coresight_find_enabled_sink(struct coresight_device *csdev)
596 struct coresight_device *sink = NULL;
598 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
599 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
604 * Recursively explore each port found on this element.
606 for (i = 0; i < csdev->pdata->nr_outconns; i++) {
607 struct coresight_device *child_dev;
609 child_dev = csdev->pdata->out_conns[i].dest_dev;
611 sink = coresight_find_enabled_sink(child_dev);
620 * coresight_get_enabled_sink - returns the first enabled sink using
621 * connection based search starting from the source reference
623 * @source: Coresight source device reference
625 struct coresight_device *
626 coresight_get_enabled_sink(struct coresight_device *source)
631 return coresight_find_enabled_sink(source);
634 static int coresight_sink_by_id(struct device *dev, const void *data)
636 struct coresight_device *csdev = to_coresight_device(dev);
639 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
640 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
645 * See function etm_perf_add_symlink_sink() to know where
648 hash = (unsigned long)csdev->ea->var;
650 if ((u32)hash == *(u32 *)data)
658 * coresight_get_sink_by_id - returns the sink that matches the id
659 * @id: Id of the sink to match
661 * The name of a sink is unique, whether it is found on the AMBA bus or
662 * otherwise. As such the hash of that name can easily be used to identify
665 struct coresight_device *coresight_get_sink_by_id(u32 id)
667 struct device *dev = NULL;
669 dev = bus_find_device(&coresight_bustype, NULL, &id,
670 coresight_sink_by_id);
672 return dev ? to_coresight_device(dev) : NULL;
676 * coresight_get_ref- Helper function to increase reference count to module
679 * @csdev: The coresight device to get a reference on.
681 * Return true in successful case and power up the device.
682 * Return false when failed to get reference of module.
684 static inline bool coresight_get_ref(struct coresight_device *csdev)
686 struct device *dev = csdev->dev.parent;
688 /* Make sure the driver can't be removed */
689 if (!try_module_get(dev->driver->owner))
691 /* Make sure the device can't go away */
693 pm_runtime_get_sync(dev);
698 * coresight_put_ref- Helper function to decrease reference count to module
699 * and device. Power off the device.
701 * @csdev: The coresight device to decrement a reference from.
703 static inline void coresight_put_ref(struct coresight_device *csdev)
705 struct device *dev = csdev->dev.parent;
709 module_put(dev->driver->owner);
713 * coresight_grab_device - Power up this device and any of the helper
714 * devices connected to it for trace operation. Since the helper devices
715 * don't appear on the trace path, they should be handled along with the
718 static int coresight_grab_device(struct coresight_device *csdev)
722 for (i = 0; i < csdev->pdata->nr_outconns; i++) {
723 struct coresight_device *child;
725 child = csdev->pdata->out_conns[i].dest_dev;
726 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
727 if (!coresight_get_ref(child))
730 if (coresight_get_ref(csdev))
733 for (i--; i >= 0; i--) {
734 struct coresight_device *child;
736 child = csdev->pdata->out_conns[i].dest_dev;
737 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
738 coresight_put_ref(child);
744 * coresight_drop_device - Release this device and any of the helper
745 * devices connected to it.
747 static void coresight_drop_device(struct coresight_device *csdev)
751 coresight_put_ref(csdev);
752 for (i = 0; i < csdev->pdata->nr_outconns; i++) {
753 struct coresight_device *child;
755 child = csdev->pdata->out_conns[i].dest_dev;
756 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
757 coresight_put_ref(child);
762 * _coresight_build_path - recursively build a path from a @csdev to a sink.
763 * @csdev: The device to start from.
764 * @sink: The final sink we want in this path.
765 * @path: The list to add devices to.
767 * The tree of Coresight device is traversed until an activated sink is
768 * found. From there the sink is added to the list along with all the
769 * devices that led to that point - the end result is a list from source
770 * to sink. In that list the source is the first device and the sink the
773 static int _coresight_build_path(struct coresight_device *csdev,
774 struct coresight_device *sink,
775 struct list_head *path)
779 struct coresight_node *node;
781 /* An activated sink has been found. Enqueue the element */
785 if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&
786 sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {
787 if (_coresight_build_path(sink, sink, path) == 0) {
793 /* Not a sink - recursively explore each port found on this element */
794 for (i = 0; i < csdev->pdata->nr_outconns; i++) {
795 struct coresight_device *child_dev;
797 child_dev = csdev->pdata->out_conns[i].dest_dev;
799 _coresight_build_path(child_dev, sink, path) == 0) {
810 * A path from this element to a sink has been found. The elements
811 * leading to the sink are already enqueued, all that is left to do
812 * is tell the PM runtime core we need this element and add a node
815 ret = coresight_grab_device(csdev);
819 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
824 list_add(&node->link, path);
829 struct list_head *coresight_build_path(struct coresight_device *source,
830 struct coresight_device *sink)
832 struct list_head *path;
836 return ERR_PTR(-EINVAL);
838 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
840 return ERR_PTR(-ENOMEM);
842 INIT_LIST_HEAD(path);
844 rc = _coresight_build_path(source, sink, path);
854 * coresight_release_path - release a previously built path.
855 * @path: the path to release.
857 * Go through all the elements of a path and 1) removed it from the list and
858 * 2) free the memory allocated for each node.
860 void coresight_release_path(struct list_head *path)
862 struct coresight_device *csdev;
863 struct coresight_node *nd, *next;
865 list_for_each_entry_safe(nd, next, path, link) {
868 coresight_drop_device(csdev);
876 /* return true if the device is a suitable type for a default sink */
877 static inline bool coresight_is_def_sink_type(struct coresight_device *csdev)
879 /* sink & correct subtype */
880 if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
881 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
882 (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
888 * coresight_select_best_sink - return the best sink for use as default from
891 * @sink: current best sink.
892 * @depth: search depth where current sink was found.
893 * @new_sink: new sink for comparison with current sink.
894 * @new_depth: search depth where new sink was found.
896 * Sinks prioritised according to coresight_dev_subtype_sink, with only
897 * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used.
899 * Where two sinks of equal priority are found, the sink closest to the
900 * source is used (smallest search depth).
902 * return @new_sink & update @depth if better than @sink, else return @sink.
904 static struct coresight_device *
905 coresight_select_best_sink(struct coresight_device *sink, int *depth,
906 struct coresight_device *new_sink, int new_depth)
911 /* first found at this level */
913 } else if (new_sink->subtype.sink_subtype >
914 sink->subtype.sink_subtype) {
915 /* found better sink */
917 } else if ((new_sink->subtype.sink_subtype ==
918 sink->subtype.sink_subtype) &&
919 (*depth > new_depth)) {
920 /* found same but closer sink */
926 return update ? new_sink : sink;
930 * coresight_find_sink - recursive function to walk trace connections from
931 * source to find a suitable default sink.
933 * @csdev: source / current device to check.
934 * @depth: [in] search depth of calling dev, [out] depth of found sink.
936 * This will walk the connection path from a source (ETM) till a suitable
937 * sink is encountered and return that sink to the original caller.
939 * If current device is a plain sink return that & depth, otherwise recursively
940 * call child connections looking for a sink. Select best possible using
941 * coresight_select_best_sink.
943 * return best sink found, or NULL if not found at this node or child nodes.
945 static struct coresight_device *
946 coresight_find_sink(struct coresight_device *csdev, int *depth)
948 int i, curr_depth = *depth + 1, found_depth = 0;
949 struct coresight_device *found_sink = NULL;
951 if (coresight_is_def_sink_type(csdev)) {
952 found_depth = curr_depth;
954 if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
955 goto return_def_sink;
956 /* look past LINKSINK for something better */
960 * Not a sink we want - or possible child sink may be better.
961 * recursively explore each port found on this element.
963 for (i = 0; i < csdev->pdata->nr_outconns; i++) {
964 struct coresight_device *child_dev, *sink = NULL;
965 int child_depth = curr_depth;
967 child_dev = csdev->pdata->out_conns[i].dest_dev;
969 sink = coresight_find_sink(child_dev, &child_depth);
972 found_sink = coresight_select_best_sink(found_sink,
979 /* return found sink and depth */
981 *depth = found_depth;
986 * coresight_find_default_sink: Find a sink suitable for use as a
989 * @csdev: starting source to find a connected sink.
991 * Walks connections graph looking for a suitable sink to enable for the
992 * supplied source. Uses CoreSight device subtypes and distance from source
993 * to select the best sink.
995 * If a sink is found, then the default sink for this device is set and
996 * will be automatically used in future.
998 * Used in cases where the CoreSight user (perf / sysfs) has not selected a
1001 struct coresight_device *
1002 coresight_find_default_sink(struct coresight_device *csdev)
1006 /* look for a default sink if we have not found for this device */
1007 if (!csdev->def_sink) {
1008 if (coresight_is_percpu_source(csdev))
1009 csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));
1010 if (!csdev->def_sink)
1011 csdev->def_sink = coresight_find_sink(csdev, &depth);
1013 return csdev->def_sink;
1016 static int coresight_remove_sink_ref(struct device *dev, void *data)
1018 struct coresight_device *sink = data;
1019 struct coresight_device *source = to_coresight_device(dev);
1021 if (source->def_sink == sink)
1022 source->def_sink = NULL;
1027 * coresight_clear_default_sink: Remove all default sink references to the
1030 * If supplied device is a sink, then check all the bus devices and clear
1031 * out all the references to this sink from the coresight_device def_sink
1034 * @csdev: coresight sink - remove references to this from all sources.
1036 static void coresight_clear_default_sink(struct coresight_device *csdev)
1038 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
1039 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
1040 bus_for_each_dev(&coresight_bustype, NULL, csdev,
1041 coresight_remove_sink_ref);
1045 /** coresight_validate_source - make sure a source has the right credentials
1046 * @csdev: the device structure for a source.
1047 * @function: the function this was called from.
1049 * Assumes the coresight_mutex is held.
1051 static int coresight_validate_source(struct coresight_device *csdev,
1052 const char *function)
1057 subtype = csdev->subtype.source_subtype;
1059 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
1060 dev_err(&csdev->dev, "wrong device type in %s\n", function);
1064 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
1065 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE &&
1066 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_OTHERS) {
1067 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
1074 int coresight_enable(struct coresight_device *csdev)
1077 struct coresight_device *sink;
1078 struct list_head *path;
1079 enum coresight_dev_subtype_source subtype;
1082 subtype = csdev->subtype.source_subtype;
1084 mutex_lock(&coresight_mutex);
1086 ret = coresight_validate_source(csdev, __func__);
1090 if (csdev->enable) {
1092 * There could be multiple applications driving the software
1093 * source. So keep the refcount for each such user when the
1094 * source is already enabled.
1096 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
1097 atomic_inc(csdev->refcnt);
1101 sink = coresight_get_enabled_sink(csdev);
1107 path = coresight_build_path(csdev, sink);
1109 pr_err("building path(s) failed\n");
1110 ret = PTR_ERR(path);
1114 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
1118 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
1123 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1125 * When working from sysFS it is important to keep track
1126 * of the paths that were created so that they can be
1127 * undone in 'coresight_disable()'. Since there can only
1128 * be a single session per tracer (when working from sysFS)
1129 * a per-cpu variable will do just fine.
1131 cpu = source_ops(csdev)->cpu_id(csdev);
1132 per_cpu(tracer_path, cpu) = path;
1134 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1135 case CORESIGHT_DEV_SUBTYPE_SOURCE_OTHERS:
1137 * Use the hash of source's device name as ID
1138 * and map the ID to the pointer of the path.
1140 hash = hashlen_hash(hashlen_string(NULL, dev_name(&csdev->dev)));
1141 ret = idr_alloc_u32(&path_idr, path, &hash, hash, GFP_KERNEL);
1146 /* We can't be here */
1151 mutex_unlock(&coresight_mutex);
1155 coresight_disable_path(path);
1158 coresight_release_path(path);
1161 EXPORT_SYMBOL_GPL(coresight_enable);
1163 void coresight_disable(struct coresight_device *csdev)
1166 struct list_head *path = NULL;
1169 mutex_lock(&coresight_mutex);
1171 ret = coresight_validate_source(csdev, __func__);
1175 if (!csdev->enable || !coresight_disable_source(csdev))
1178 switch (csdev->subtype.source_subtype) {
1179 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1180 cpu = source_ops(csdev)->cpu_id(csdev);
1181 path = per_cpu(tracer_path, cpu);
1182 per_cpu(tracer_path, cpu) = NULL;
1184 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1185 case CORESIGHT_DEV_SUBTYPE_SOURCE_OTHERS:
1186 hash = hashlen_hash(hashlen_string(NULL, dev_name(&csdev->dev)));
1187 /* Find the path by the hash. */
1188 path = idr_find(&path_idr, hash);
1190 pr_err("Path is not found for %s\n", dev_name(&csdev->dev));
1193 idr_remove(&path_idr, hash);
1196 /* We can't be here */
1200 coresight_disable_path(path);
1201 coresight_release_path(path);
1204 mutex_unlock(&coresight_mutex);
1206 EXPORT_SYMBOL_GPL(coresight_disable);
1208 static ssize_t enable_sink_show(struct device *dev,
1209 struct device_attribute *attr, char *buf)
1211 struct coresight_device *csdev = to_coresight_device(dev);
1213 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
1216 static ssize_t enable_sink_store(struct device *dev,
1217 struct device_attribute *attr,
1218 const char *buf, size_t size)
1222 struct coresight_device *csdev = to_coresight_device(dev);
1224 ret = kstrtoul(buf, 10, &val);
1229 csdev->activated = true;
1231 csdev->activated = false;
1236 static DEVICE_ATTR_RW(enable_sink);
1238 static ssize_t enable_source_show(struct device *dev,
1239 struct device_attribute *attr, char *buf)
1241 struct coresight_device *csdev = to_coresight_device(dev);
1243 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
1246 static ssize_t enable_source_store(struct device *dev,
1247 struct device_attribute *attr,
1248 const char *buf, size_t size)
1252 struct coresight_device *csdev = to_coresight_device(dev);
1254 ret = kstrtoul(buf, 10, &val);
1259 ret = coresight_enable(csdev);
1263 coresight_disable(csdev);
1268 static DEVICE_ATTR_RW(enable_source);
1270 static struct attribute *coresight_sink_attrs[] = {
1271 &dev_attr_enable_sink.attr,
1274 ATTRIBUTE_GROUPS(coresight_sink);
1276 static struct attribute *coresight_source_attrs[] = {
1277 &dev_attr_enable_source.attr,
1280 ATTRIBUTE_GROUPS(coresight_source);
1282 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_outconns; i++) {
1337 conn = &i_csdev->pdata->out_conns[i];
1339 /* Skip the port if FW doesn't describe it */
1340 if (!conn->dest_fwnode)
1342 /* We have found at least one orphan connection */
1343 if (conn->dest_dev == NULL) {
1344 /* Does it match this newly added device? */
1345 if (conn->dest_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_outconns; i++) {
1378 struct coresight_connection *conn = &csdev->pdata->out_conns[i];
1380 if (!conn->dest_fwnode)
1383 coresight_find_csdev_by_fwnode(conn->dest_fwnode);
1384 if (conn->dest_dev && conn->dest_dev->has_conns_grp) {
1385 ret = coresight_make_links(csdev, conn, conn->dest_dev);
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_outconns; i++) {
1414 conn = &iterator->pdata->out_conns[i];
1416 if (conn->dest_dev == NULL || conn->dest_fwnode == NULL)
1419 if (csdev->dev.fwnode == conn->dest_fwnode) {
1420 iterator->orphan = true;
1421 coresight_remove_links(iterator, conn);
1423 conn->dest_dev = NULL;
1424 /* No need to continue */
1430 * Returning '0' ensures that all known component on the
1431 * bus will be checked.
1437 * coresight_remove_conns - Remove references to this given devices
1438 * from the connections of other devices.
1440 static void coresight_remove_conns(struct coresight_device *csdev)
1443 * Another device will point to this device only if there is
1444 * an output port connected to this one. i.e, if the device
1445 * doesn't have at least one input port, there is no point
1446 * in searching all the devices.
1448 if (csdev->pdata->nr_inconns)
1449 bus_for_each_dev(&coresight_bustype, NULL,
1450 csdev, coresight_remove_match);
1454 * coresight_timeout - loop until a bit has changed to a specific register
1456 * @csa: coresight device access for the device
1457 * @offset: Offset of the register from the base of the device.
1458 * @position: the position of the bit of interest.
1459 * @value: the value the bit should have.
1461 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1462 * TIMEOUT_US has elapsed, which ever happens first.
1464 int coresight_timeout(struct csdev_access *csa, u32 offset,
1465 int position, int value)
1470 for (i = TIMEOUT_US; i > 0; i--) {
1471 val = csdev_access_read32(csa, offset);
1472 /* waiting on the bit to go from 0 to 1 */
1474 if (val & BIT(position))
1476 /* waiting on the bit to go from 1 to 0 */
1478 if (!(val & BIT(position)))
1483 * Delay is arbitrary - the specification doesn't say how long
1484 * we are expected to wait. Extra check required to make sure
1485 * we don't wait needlessly on the last iteration.
1493 EXPORT_SYMBOL_GPL(coresight_timeout);
1495 u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset)
1497 return csdev_access_relaxed_read32(&csdev->access, offset);
1500 u32 coresight_read32(struct coresight_device *csdev, u32 offset)
1502 return csdev_access_read32(&csdev->access, offset);
1505 void coresight_relaxed_write32(struct coresight_device *csdev,
1506 u32 val, u32 offset)
1508 csdev_access_relaxed_write32(&csdev->access, val, offset);
1511 void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset)
1513 csdev_access_write32(&csdev->access, val, offset);
1516 u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset)
1518 return csdev_access_relaxed_read64(&csdev->access, offset);
1521 u64 coresight_read64(struct coresight_device *csdev, u32 offset)
1523 return csdev_access_read64(&csdev->access, offset);
1526 void coresight_relaxed_write64(struct coresight_device *csdev,
1527 u64 val, u32 offset)
1529 csdev_access_relaxed_write64(&csdev->access, val, offset);
1532 void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset)
1534 csdev_access_write64(&csdev->access, val, offset);
1538 * coresight_release_platform_data: Release references to the devices connected
1539 * to the output port of this device.
1541 void coresight_release_platform_data(struct coresight_device *csdev,
1542 struct coresight_platform_data *pdata)
1545 struct coresight_connection *conns = pdata->out_conns;
1547 for (i = 0; i < pdata->nr_outconns; i++) {
1548 /* If we have made the links, remove them now */
1549 if (csdev && conns[i].dest_dev)
1550 coresight_remove_links(csdev, &conns[i]);
1552 * Drop the refcount and clear the handle as this device
1555 if (conns[i].dest_fwnode) {
1556 fwnode_handle_put(conns[i].dest_fwnode);
1557 conns[i].dest_fwnode = NULL;
1561 coresight_remove_conns_sysfs_group(csdev);
1564 struct coresight_device *coresight_register(struct coresight_desc *desc)
1569 atomic_t *refcnts = NULL;
1570 struct coresight_device *csdev;
1571 bool registered = false;
1573 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1579 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1580 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1581 link_subtype = desc->subtype.link_subtype;
1583 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1584 nr_refcnts = desc->pdata->nr_inconns;
1585 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1586 nr_refcnts = desc->pdata->nr_outconns;
1589 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1596 csdev->refcnt = refcnts;
1598 csdev->pdata = desc->pdata;
1600 csdev->type = desc->type;
1601 csdev->subtype = desc->subtype;
1602 csdev->ops = desc->ops;
1603 csdev->access = desc->access;
1604 csdev->orphan = false;
1606 csdev->dev.type = &coresight_dev_type[desc->type];
1607 csdev->dev.groups = desc->groups;
1608 csdev->dev.parent = desc->dev;
1609 csdev->dev.release = coresight_device_release;
1610 csdev->dev.bus = &coresight_bustype;
1612 * Hold the reference to our parent device. This will be
1613 * dropped only in coresight_device_release().
1615 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1616 dev_set_name(&csdev->dev, "%s", desc->name);
1619 * Make sure the device registration and the connection fixup
1620 * are synchronised, so that we don't see uninitialised devices
1621 * on the coresight bus while trying to resolve the connections.
1623 mutex_lock(&coresight_mutex);
1625 ret = device_register(&csdev->dev);
1627 put_device(&csdev->dev);
1629 * All resources are free'd explicitly via
1630 * coresight_device_release(), triggered from put_device().
1635 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1636 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1637 ret = etm_perf_add_symlink_sink(csdev);
1640 device_unregister(&csdev->dev);
1642 * As with the above, all resources are free'd
1643 * explicitly via coresight_device_release() triggered
1644 * from put_device(), which is in turn called from
1645 * function device_unregister().
1650 /* Device is now registered */
1653 ret = coresight_create_conns_sysfs_group(csdev);
1655 ret = coresight_fixup_device_conns(csdev);
1657 ret = coresight_fixup_orphan_conns(csdev);
1660 mutex_unlock(&coresight_mutex);
1663 if (cti_assoc_ops && cti_assoc_ops->add)
1664 cti_assoc_ops->add(csdev);
1668 /* Unregister the device if needed */
1670 coresight_unregister(csdev);
1671 return ERR_PTR(ret);
1675 /* Cleanup the connection information */
1676 coresight_release_platform_data(NULL, desc->pdata);
1677 return ERR_PTR(ret);
1679 EXPORT_SYMBOL_GPL(coresight_register);
1681 void coresight_unregister(struct coresight_device *csdev)
1683 etm_perf_del_symlink_sink(csdev);
1684 /* Remove references of that device in the topology */
1685 if (cti_assoc_ops && cti_assoc_ops->remove)
1686 cti_assoc_ops->remove(csdev);
1687 coresight_remove_conns(csdev);
1688 coresight_clear_default_sink(csdev);
1689 coresight_release_platform_data(csdev, csdev->pdata);
1690 device_unregister(&csdev->dev);
1692 EXPORT_SYMBOL_GPL(coresight_unregister);
1696 * coresight_search_device_idx - Search the fwnode handle of a device
1697 * in the given dev_idx list. Must be called with the coresight_mutex held.
1699 * Returns the index of the entry, when found. Otherwise, -ENOENT.
1701 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1702 struct fwnode_handle *fwnode)
1706 for (i = 0; i < dict->nr_idx; i++)
1707 if (dict->fwnode_list[i] == fwnode)
1712 bool coresight_loses_context_with_cpu(struct device *dev)
1714 return fwnode_property_present(dev_fwnode(dev),
1715 "arm,coresight-loses-context-with-cpu");
1717 EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
1720 * coresight_alloc_device_name - Get an index for a given device in the
1721 * device index list specific to a driver. An index is allocated for a
1722 * device and is tracked with the fwnode_handle to prevent allocating
1723 * duplicate indices for the same device (e.g, if we defer probing of
1724 * a device due to dependencies), in case the index is requested again.
1726 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1731 struct fwnode_handle **list;
1733 mutex_lock(&coresight_mutex);
1735 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1737 /* Make space for the new entry */
1739 list = krealloc_array(dict->fwnode_list,
1740 idx + 1, sizeof(*dict->fwnode_list),
1742 if (ZERO_OR_NULL_PTR(list)) {
1747 list[idx] = dev_fwnode(dev);
1748 dict->fwnode_list = list;
1749 dict->nr_idx = idx + 1;
1752 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1754 mutex_unlock(&coresight_mutex);
1757 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1759 struct bus_type coresight_bustype = {
1760 .name = "coresight",
1763 static int __init coresight_init(void)
1767 ret = bus_register(&coresight_bustype);
1771 ret = etm_perf_init();
1773 goto exit_bus_unregister;
1775 /* initialise the coresight syscfg API */
1781 exit_bus_unregister:
1782 bus_unregister(&coresight_bustype);
1786 static void __exit coresight_exit(void)
1790 bus_unregister(&coresight_bustype);
1793 module_init(coresight_init);
1794 module_exit(coresight_exit);
1796 MODULE_LICENSE("GPL v2");
1797 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1798 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1799 MODULE_DESCRIPTION("Arm CoreSight tracer driver");