1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/types.h>
9 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/stringhash.h>
15 #include <linux/mutex.h>
16 #include <linux/clk.h>
17 #include <linux/coresight.h>
18 #include <linux/of_platform.h>
19 #include <linux/delay.h>
20 #include <linux/pm_runtime.h>
22 #include "coresight-etm-perf.h"
23 #include "coresight-priv.h"
25 static DEFINE_MUTEX(coresight_mutex);
28 * struct coresight_node - elements of a path, from source to sink
29 * @csdev: Address of an element.
30 * @link: hook to the list.
32 struct coresight_node {
33 struct coresight_device *csdev;
34 struct list_head link;
38 * When operating Coresight drivers from the sysFS interface, only a single
39 * path can exist from a tracer (associated to a CPU) to a sink.
41 static DEFINE_PER_CPU(struct list_head *, tracer_path);
44 * As of this writing only a single STM can be found in CS topologies. Since
45 * there is no way to know if we'll ever see more and what kind of
46 * configuration they will enact, for the time being only define a single path
49 static struct list_head *stm_path;
52 * When losing synchronisation a new barrier packet needs to be inserted at the
53 * beginning of the data collected in a buffer. That way the decoder knows that
54 * it needs to look for another sync sequence.
56 const u32 coresight_barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
57 EXPORT_SYMBOL_GPL(coresight_barrier_pkt);
59 static const struct cti_assoc_op *cti_assoc_ops;
61 void coresight_set_cti_ops(const struct cti_assoc_op *cti_op)
63 cti_assoc_ops = cti_op;
65 EXPORT_SYMBOL_GPL(coresight_set_cti_ops);
67 void coresight_remove_cti_ops(void)
71 EXPORT_SYMBOL_GPL(coresight_remove_cti_ops);
73 static int coresight_id_match(struct device *dev, void *data)
75 int trace_id, i_trace_id;
76 struct coresight_device *csdev, *i_csdev;
79 i_csdev = to_coresight_device(dev);
82 * No need to care about oneself and components that are not
83 * sources or not enabled
85 if (i_csdev == csdev || !i_csdev->enable ||
86 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
89 /* Get the source ID for both compoment */
90 trace_id = source_ops(csdev)->trace_id(csdev);
91 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
93 /* All you need is one */
94 if (trace_id == i_trace_id)
100 static int coresight_source_is_unique(struct coresight_device *csdev)
102 int trace_id = source_ops(csdev)->trace_id(csdev);
104 /* this shouldn't happen */
108 return !bus_for_each_dev(&coresight_bustype, NULL,
109 csdev, coresight_id_match);
112 static int coresight_find_link_inport(struct coresight_device *csdev,
113 struct coresight_device *parent)
116 struct coresight_connection *conn;
118 for (i = 0; i < parent->pdata->nr_outport; i++) {
119 conn = &parent->pdata->conns[i];
120 if (conn->child_dev == csdev)
121 return conn->child_port;
124 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
125 dev_name(&parent->dev), dev_name(&csdev->dev));
130 static int coresight_find_link_outport(struct coresight_device *csdev,
131 struct coresight_device *child)
134 struct coresight_connection *conn;
136 for (i = 0; i < csdev->pdata->nr_outport; i++) {
137 conn = &csdev->pdata->conns[i];
138 if (conn->child_dev == child)
139 return conn->outport;
142 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
143 dev_name(&csdev->dev), dev_name(&child->dev));
148 static inline u32 coresight_read_claim_tags(void __iomem *base)
150 return readl_relaxed(base + CORESIGHT_CLAIMCLR);
153 static inline bool coresight_is_claimed_self_hosted(void __iomem *base)
155 return coresight_read_claim_tags(base) == CORESIGHT_CLAIM_SELF_HOSTED;
158 static inline bool coresight_is_claimed_any(void __iomem *base)
160 return coresight_read_claim_tags(base) != 0;
163 static inline void coresight_set_claim_tags(void __iomem *base)
165 writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMSET);
169 static inline void coresight_clear_claim_tags(void __iomem *base)
171 writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMCLR);
176 * coresight_claim_device_unlocked : Claim the device for self-hosted usage
177 * to prevent an external tool from touching this device. As per PSCI
178 * standards, section "Preserving the execution context" => "Debug and Trace
179 * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
180 * DBGCLAIM[0] is reserved for external tools.
182 * Called with CS_UNLOCKed for the component.
183 * Returns : 0 on success
185 int coresight_claim_device_unlocked(void __iomem *base)
187 if (coresight_is_claimed_any(base))
190 coresight_set_claim_tags(base);
191 if (coresight_is_claimed_self_hosted(base))
193 /* There was a race setting the tags, clean up and fail */
194 coresight_clear_claim_tags(base);
197 EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked);
199 int coresight_claim_device(void __iomem *base)
204 rc = coresight_claim_device_unlocked(base);
209 EXPORT_SYMBOL_GPL(coresight_claim_device);
212 * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
213 * Called with CS_UNLOCKed for the component.
215 void coresight_disclaim_device_unlocked(void __iomem *base)
218 if (coresight_is_claimed_self_hosted(base))
219 coresight_clear_claim_tags(base);
222 * The external agent may have not honoured our claim
223 * and has manipulated it. Or something else has seriously
224 * gone wrong in our driver.
228 EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked);
230 void coresight_disclaim_device(void __iomem *base)
233 coresight_disclaim_device_unlocked(base);
236 EXPORT_SYMBOL_GPL(coresight_disclaim_device);
238 /* enable or disable an associated CTI device of the supplied CS device */
240 coresight_control_assoc_ectdev(struct coresight_device *csdev, bool enable)
243 struct coresight_device *ect_csdev = csdev->ect_dev;
248 if ((!ect_ops(ect_csdev)->enable) || (!ect_ops(ect_csdev)->disable))
251 mod = ect_csdev->dev.parent->driver->owner;
253 if (try_module_get(mod)) {
254 ect_ret = ect_ops(ect_csdev)->enable(ect_csdev);
258 get_device(ect_csdev->dev.parent);
259 csdev->ect_enabled = true;
264 if (csdev->ect_enabled) {
265 ect_ret = ect_ops(ect_csdev)->disable(ect_csdev);
266 put_device(ect_csdev->dev.parent);
268 csdev->ect_enabled = false;
272 /* output warning if ECT enable is preventing trace operation */
274 dev_info(&csdev->dev, "Associated ECT device (%s) %s failed\n",
275 dev_name(&ect_csdev->dev),
276 enable ? "enable" : "disable");
281 * Set the associated ect / cti device while holding the coresight_mutex
282 * to avoid a race with coresight_enable that may try to use this value.
284 void coresight_set_assoc_ectdev_mutex(struct coresight_device *csdev,
285 struct coresight_device *ect_csdev)
287 mutex_lock(&coresight_mutex);
288 csdev->ect_dev = ect_csdev;
289 mutex_unlock(&coresight_mutex);
291 EXPORT_SYMBOL_GPL(coresight_set_assoc_ectdev_mutex);
293 static int coresight_enable_sink(struct coresight_device *csdev,
294 u32 mode, void *data)
299 * We need to make sure the "new" session is compatible with the
300 * existing "mode" of operation.
302 if (!sink_ops(csdev)->enable)
305 ret = coresight_control_assoc_ectdev(csdev, true);
308 ret = sink_ops(csdev)->enable(csdev, mode, data);
310 coresight_control_assoc_ectdev(csdev, false);
313 csdev->enable = true;
318 static void coresight_disable_sink(struct coresight_device *csdev)
322 if (!sink_ops(csdev)->disable)
325 ret = sink_ops(csdev)->disable(csdev);
328 coresight_control_assoc_ectdev(csdev, false);
329 csdev->enable = false;
332 static int coresight_enable_link(struct coresight_device *csdev,
333 struct coresight_device *parent,
334 struct coresight_device *child)
340 if (!parent || !child)
343 inport = coresight_find_link_inport(csdev, parent);
344 outport = coresight_find_link_outport(csdev, child);
345 link_subtype = csdev->subtype.link_subtype;
347 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && inport < 0)
349 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && outport < 0)
352 if (link_ops(csdev)->enable) {
353 ret = coresight_control_assoc_ectdev(csdev, true);
355 ret = link_ops(csdev)->enable(csdev, inport, outport);
357 coresight_control_assoc_ectdev(csdev, false);
362 csdev->enable = true;
367 static void coresight_disable_link(struct coresight_device *csdev,
368 struct coresight_device *parent,
369 struct coresight_device *child)
375 if (!parent || !child)
378 inport = coresight_find_link_inport(csdev, parent);
379 outport = coresight_find_link_outport(csdev, child);
380 link_subtype = csdev->subtype.link_subtype;
382 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
383 nr_conns = csdev->pdata->nr_inport;
384 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
385 nr_conns = csdev->pdata->nr_outport;
390 if (link_ops(csdev)->disable) {
391 link_ops(csdev)->disable(csdev, inport, outport);
392 coresight_control_assoc_ectdev(csdev, false);
395 for (i = 0; i < nr_conns; i++)
396 if (atomic_read(&csdev->refcnt[i]) != 0)
399 csdev->enable = false;
402 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
406 if (!coresight_source_is_unique(csdev)) {
407 dev_warn(&csdev->dev, "traceID %d not unique\n",
408 source_ops(csdev)->trace_id(csdev));
412 if (!csdev->enable) {
413 if (source_ops(csdev)->enable) {
414 ret = coresight_control_assoc_ectdev(csdev, true);
417 ret = source_ops(csdev)->enable(csdev, NULL, mode);
419 coresight_control_assoc_ectdev(csdev, false);
423 csdev->enable = true;
426 atomic_inc(csdev->refcnt);
432 * coresight_disable_source - Drop the reference count by 1 and disable
433 * the device if there are no users left.
435 * @csdev - The coresight device to disable
437 * Returns true if the device has been disabled.
439 static bool coresight_disable_source(struct coresight_device *csdev)
441 if (atomic_dec_return(csdev->refcnt) == 0) {
442 if (source_ops(csdev)->disable)
443 source_ops(csdev)->disable(csdev, NULL);
444 coresight_control_assoc_ectdev(csdev, false);
445 csdev->enable = false;
447 return !csdev->enable;
451 * coresight_disable_path_from : Disable components in the given path beyond
452 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
455 static void coresight_disable_path_from(struct list_head *path,
456 struct coresight_node *nd)
459 struct coresight_device *csdev, *parent, *child;
462 nd = list_first_entry(path, struct coresight_node, link);
464 list_for_each_entry_continue(nd, path, link) {
469 * ETF devices are tricky... They can be a link or a sink,
470 * depending on how they are configured. If an ETF has been
471 * "activated" it will be configured as a sink, otherwise
472 * go ahead with the link configuration.
474 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
475 type = (csdev == coresight_get_sink(path)) ?
476 CORESIGHT_DEV_TYPE_SINK :
477 CORESIGHT_DEV_TYPE_LINK;
480 case CORESIGHT_DEV_TYPE_SINK:
481 coresight_disable_sink(csdev);
483 case CORESIGHT_DEV_TYPE_SOURCE:
485 * We skip the first node in the path assuming that it
486 * is the source. So we don't expect a source device in
487 * the middle of a path.
491 case CORESIGHT_DEV_TYPE_LINK:
492 parent = list_prev_entry(nd, link)->csdev;
493 child = list_next_entry(nd, link)->csdev;
494 coresight_disable_link(csdev, parent, child);
502 void coresight_disable_path(struct list_head *path)
504 coresight_disable_path_from(path, NULL);
506 EXPORT_SYMBOL_GPL(coresight_disable_path);
508 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
513 struct coresight_node *nd;
514 struct coresight_device *csdev, *parent, *child;
516 list_for_each_entry_reverse(nd, path, link) {
521 * ETF devices are tricky... They can be a link or a sink,
522 * depending on how they are configured. If an ETF has been
523 * "activated" it will be configured as a sink, otherwise
524 * go ahead with the link configuration.
526 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
527 type = (csdev == coresight_get_sink(path)) ?
528 CORESIGHT_DEV_TYPE_SINK :
529 CORESIGHT_DEV_TYPE_LINK;
532 case CORESIGHT_DEV_TYPE_SINK:
533 ret = coresight_enable_sink(csdev, mode, sink_data);
535 * Sink is the first component turned on. If we
536 * failed to enable the sink, there are no components
537 * that need disabling. Disabling the path here
538 * would mean we could disrupt an existing session.
543 case CORESIGHT_DEV_TYPE_SOURCE:
544 /* sources are enabled from either sysFS or Perf */
546 case CORESIGHT_DEV_TYPE_LINK:
547 parent = list_prev_entry(nd, link)->csdev;
548 child = list_next_entry(nd, link)->csdev;
549 ret = coresight_enable_link(csdev, parent, child);
561 coresight_disable_path_from(path, nd);
565 struct coresight_device *coresight_get_sink(struct list_head *path)
567 struct coresight_device *csdev;
572 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
573 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
574 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
580 static struct coresight_device *
581 coresight_find_enabled_sink(struct coresight_device *csdev)
584 struct coresight_device *sink = NULL;
586 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
587 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
592 * Recursively explore each port found on this element.
594 for (i = 0; i < csdev->pdata->nr_outport; i++) {
595 struct coresight_device *child_dev;
597 child_dev = csdev->pdata->conns[i].child_dev;
599 sink = coresight_find_enabled_sink(child_dev);
608 * coresight_get_enabled_sink - returns the first enabled sink using
609 * connection based search starting from the source reference
611 * @source: Coresight source device reference
613 struct coresight_device *
614 coresight_get_enabled_sink(struct coresight_device *source)
619 return coresight_find_enabled_sink(source);
622 static int coresight_sink_by_id(struct device *dev, const void *data)
624 struct coresight_device *csdev = to_coresight_device(dev);
627 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
628 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
633 * See function etm_perf_add_symlink_sink() to know where
636 hash = (unsigned long)csdev->ea->var;
638 if ((u32)hash == *(u32 *)data)
646 * coresight_get_sink_by_id - returns the sink that matches the id
647 * @id: Id of the sink to match
649 * The name of a sink is unique, whether it is found on the AMBA bus or
650 * otherwise. As such the hash of that name can easily be used to identify
653 struct coresight_device *coresight_get_sink_by_id(u32 id)
655 struct device *dev = NULL;
657 dev = bus_find_device(&coresight_bustype, NULL, &id,
658 coresight_sink_by_id);
660 return dev ? to_coresight_device(dev) : NULL;
664 * coresight_get_ref- Helper function to increase reference count to module
666 * Return true in successful case and power up the device.
667 * Return false when failed to get reference of module.
669 static inline bool coresight_get_ref(struct coresight_device *csdev)
671 struct device *dev = csdev->dev.parent;
673 /* Make sure the driver can't be removed */
674 if (!try_module_get(dev->driver->owner))
676 /* Make sure the device can't go away */
678 pm_runtime_get_sync(dev);
683 * coresight_put_ref- Helper function to decrease reference count to module
684 * and device. Power off the device.
686 static inline void coresight_put_ref(struct coresight_device *csdev)
688 struct device *dev = csdev->dev.parent;
692 module_put(dev->driver->owner);
696 * coresight_grab_device - Power up this device and any of the helper
697 * devices connected to it for trace operation. Since the helper devices
698 * don't appear on the trace path, they should be handled along with the
701 static int coresight_grab_device(struct coresight_device *csdev)
705 for (i = 0; i < csdev->pdata->nr_outport; i++) {
706 struct coresight_device *child;
708 child = csdev->pdata->conns[i].child_dev;
709 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
710 if (!coresight_get_ref(child))
713 if (coresight_get_ref(csdev))
716 for (i--; i >= 0; i--) {
717 struct coresight_device *child;
719 child = csdev->pdata->conns[i].child_dev;
720 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
721 coresight_put_ref(child);
727 * coresight_drop_device - Release this device and any of the helper
728 * devices connected to it.
730 static void coresight_drop_device(struct coresight_device *csdev)
734 coresight_put_ref(csdev);
735 for (i = 0; i < csdev->pdata->nr_outport; i++) {
736 struct coresight_device *child;
738 child = csdev->pdata->conns[i].child_dev;
739 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
740 coresight_put_ref(child);
745 * _coresight_build_path - recursively build a path from a @csdev to a sink.
746 * @csdev: The device to start from.
747 * @path: The list to add devices to.
749 * The tree of Coresight device is traversed until an activated sink is
750 * found. From there the sink is added to the list along with all the
751 * devices that led to that point - the end result is a list from source
752 * to sink. In that list the source is the first device and the sink the
755 static int _coresight_build_path(struct coresight_device *csdev,
756 struct coresight_device *sink,
757 struct list_head *path)
761 struct coresight_node *node;
763 /* An activated sink has been found. Enqueue the element */
767 /* Not a sink - recursively explore each port found on this element */
768 for (i = 0; i < csdev->pdata->nr_outport; i++) {
769 struct coresight_device *child_dev;
771 child_dev = csdev->pdata->conns[i].child_dev;
773 _coresight_build_path(child_dev, sink, path) == 0) {
784 * A path from this element to a sink has been found. The elements
785 * leading to the sink are already enqueued, all that is left to do
786 * is tell the PM runtime core we need this element and add a node
789 ret = coresight_grab_device(csdev);
793 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
798 list_add(&node->link, path);
803 struct list_head *coresight_build_path(struct coresight_device *source,
804 struct coresight_device *sink)
806 struct list_head *path;
810 return ERR_PTR(-EINVAL);
812 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
814 return ERR_PTR(-ENOMEM);
816 INIT_LIST_HEAD(path);
818 rc = _coresight_build_path(source, sink, path);
828 * coresight_release_path - release a previously built path.
829 * @path: the path to release.
831 * Go through all the elements of a path and 1) removed it from the list and
832 * 2) free the memory allocated for each node.
834 void coresight_release_path(struct list_head *path)
836 struct coresight_device *csdev;
837 struct coresight_node *nd, *next;
839 list_for_each_entry_safe(nd, next, path, link) {
842 coresight_drop_device(csdev);
851 /* return true if the device is a suitable type for a default sink */
852 static inline bool coresight_is_def_sink_type(struct coresight_device *csdev)
854 /* sink & correct subtype */
855 if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
856 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
857 (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
863 * coresight_select_best_sink - return the best sink for use as default from
866 * @sink: current best sink.
867 * @depth: search depth where current sink was found.
868 * @new_sink: new sink for comparison with current sink.
869 * @new_depth: search depth where new sink was found.
871 * Sinks prioritised according to coresight_dev_subtype_sink, with only
872 * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used.
874 * Where two sinks of equal priority are found, the sink closest to the
875 * source is used (smallest search depth).
877 * return @new_sink & update @depth if better than @sink, else return @sink.
879 static struct coresight_device *
880 coresight_select_best_sink(struct coresight_device *sink, int *depth,
881 struct coresight_device *new_sink, int new_depth)
886 /* first found at this level */
888 } else if (new_sink->subtype.sink_subtype >
889 sink->subtype.sink_subtype) {
890 /* found better sink */
892 } else if ((new_sink->subtype.sink_subtype ==
893 sink->subtype.sink_subtype) &&
894 (*depth > new_depth)) {
895 /* found same but closer sink */
901 return update ? new_sink : sink;
905 * coresight_find_sink - recursive function to walk trace connections from
906 * source to find a suitable default sink.
908 * @csdev: source / current device to check.
909 * @depth: [in] search depth of calling dev, [out] depth of found sink.
911 * This will walk the connection path from a source (ETM) till a suitable
912 * sink is encountered and return that sink to the original caller.
914 * If current device is a plain sink return that & depth, otherwise recursively
915 * call child connections looking for a sink. Select best possible using
916 * coresight_select_best_sink.
918 * return best sink found, or NULL if not found at this node or child nodes.
920 static struct coresight_device *
921 coresight_find_sink(struct coresight_device *csdev, int *depth)
923 int i, curr_depth = *depth + 1, found_depth = 0;
924 struct coresight_device *found_sink = NULL;
926 if (coresight_is_def_sink_type(csdev)) {
927 found_depth = curr_depth;
929 if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
930 goto return_def_sink;
931 /* look past LINKSINK for something better */
935 * Not a sink we want - or possible child sink may be better.
936 * recursively explore each port found on this element.
938 for (i = 0; i < csdev->pdata->nr_outport; i++) {
939 struct coresight_device *child_dev, *sink = NULL;
940 int child_depth = curr_depth;
942 child_dev = csdev->pdata->conns[i].child_dev;
944 sink = coresight_find_sink(child_dev, &child_depth);
947 found_sink = coresight_select_best_sink(found_sink,
954 /* return found sink and depth */
956 *depth = found_depth;
961 * coresight_find_default_sink: Find a sink suitable for use as a
964 * @csdev: starting source to find a connected sink.
966 * Walks connections graph looking for a suitable sink to enable for the
967 * supplied source. Uses CoreSight device subtypes and distance from source
968 * to select the best sink.
970 * If a sink is found, then the default sink for this device is set and
971 * will be automatically used in future.
973 * Used in cases where the CoreSight user (perf / sysfs) has not selected a
976 struct coresight_device *
977 coresight_find_default_sink(struct coresight_device *csdev)
981 /* look for a default sink if we have not found for this device */
982 if (!csdev->def_sink)
983 csdev->def_sink = coresight_find_sink(csdev, &depth);
984 return csdev->def_sink;
987 static int coresight_remove_sink_ref(struct device *dev, void *data)
989 struct coresight_device *sink = data;
990 struct coresight_device *source = to_coresight_device(dev);
992 if (source->def_sink == sink)
993 source->def_sink = NULL;
998 * coresight_clear_default_sink: Remove all default sink references to the
1001 * If supplied device is a sink, then check all the bus devices and clear
1002 * out all the references to this sink from the coresight_device def_sink
1005 * @csdev: coresight sink - remove references to this from all sources.
1007 static void coresight_clear_default_sink(struct coresight_device *csdev)
1009 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
1010 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
1011 bus_for_each_dev(&coresight_bustype, NULL, csdev,
1012 coresight_remove_sink_ref);
1016 /** coresight_validate_source - make sure a source has the right credentials
1017 * @csdev: the device structure for a source.
1018 * @function: the function this was called from.
1020 * Assumes the coresight_mutex is held.
1022 static int coresight_validate_source(struct coresight_device *csdev,
1023 const char *function)
1028 subtype = csdev->subtype.source_subtype;
1030 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
1031 dev_err(&csdev->dev, "wrong device type in %s\n", function);
1035 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
1036 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
1037 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
1044 int coresight_enable(struct coresight_device *csdev)
1047 struct coresight_device *sink;
1048 struct list_head *path;
1049 enum coresight_dev_subtype_source subtype;
1051 subtype = csdev->subtype.source_subtype;
1053 mutex_lock(&coresight_mutex);
1055 ret = coresight_validate_source(csdev, __func__);
1059 if (csdev->enable) {
1061 * There could be multiple applications driving the software
1062 * source. So keep the refcount for each such user when the
1063 * source is already enabled.
1065 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
1066 atomic_inc(csdev->refcnt);
1070 sink = coresight_get_enabled_sink(csdev);
1076 path = coresight_build_path(csdev, sink);
1078 pr_err("building path(s) failed\n");
1079 ret = PTR_ERR(path);
1083 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
1087 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
1092 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1094 * When working from sysFS it is important to keep track
1095 * of the paths that were created so that they can be
1096 * undone in 'coresight_disable()'. Since there can only
1097 * be a single session per tracer (when working from sysFS)
1098 * a per-cpu variable will do just fine.
1100 cpu = source_ops(csdev)->cpu_id(csdev);
1101 per_cpu(tracer_path, cpu) = path;
1103 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1107 /* We can't be here */
1112 mutex_unlock(&coresight_mutex);
1116 coresight_disable_path(path);
1119 coresight_release_path(path);
1122 EXPORT_SYMBOL_GPL(coresight_enable);
1124 void coresight_disable(struct coresight_device *csdev)
1127 struct list_head *path = NULL;
1129 mutex_lock(&coresight_mutex);
1131 ret = coresight_validate_source(csdev, __func__);
1135 if (!csdev->enable || !coresight_disable_source(csdev))
1138 switch (csdev->subtype.source_subtype) {
1139 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1140 cpu = source_ops(csdev)->cpu_id(csdev);
1141 path = per_cpu(tracer_path, cpu);
1142 per_cpu(tracer_path, cpu) = NULL;
1144 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1149 /* We can't be here */
1153 coresight_disable_path(path);
1154 coresight_release_path(path);
1157 mutex_unlock(&coresight_mutex);
1159 EXPORT_SYMBOL_GPL(coresight_disable);
1161 static ssize_t enable_sink_show(struct device *dev,
1162 struct device_attribute *attr, char *buf)
1164 struct coresight_device *csdev = to_coresight_device(dev);
1166 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
1169 static ssize_t enable_sink_store(struct device *dev,
1170 struct device_attribute *attr,
1171 const char *buf, size_t size)
1175 struct coresight_device *csdev = to_coresight_device(dev);
1177 ret = kstrtoul(buf, 10, &val);
1182 csdev->activated = true;
1184 csdev->activated = false;
1189 static DEVICE_ATTR_RW(enable_sink);
1191 static ssize_t enable_source_show(struct device *dev,
1192 struct device_attribute *attr, char *buf)
1194 struct coresight_device *csdev = to_coresight_device(dev);
1196 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
1199 static ssize_t enable_source_store(struct device *dev,
1200 struct device_attribute *attr,
1201 const char *buf, size_t size)
1205 struct coresight_device *csdev = to_coresight_device(dev);
1207 ret = kstrtoul(buf, 10, &val);
1212 ret = coresight_enable(csdev);
1216 coresight_disable(csdev);
1221 static DEVICE_ATTR_RW(enable_source);
1223 static struct attribute *coresight_sink_attrs[] = {
1224 &dev_attr_enable_sink.attr,
1227 ATTRIBUTE_GROUPS(coresight_sink);
1229 static struct attribute *coresight_source_attrs[] = {
1230 &dev_attr_enable_source.attr,
1233 ATTRIBUTE_GROUPS(coresight_source);
1235 static struct device_type coresight_dev_type[] = {
1241 .groups = coresight_sink_groups,
1248 .groups = coresight_sink_groups,
1252 .groups = coresight_source_groups,
1262 static void coresight_device_release(struct device *dev)
1264 struct coresight_device *csdev = to_coresight_device(dev);
1266 fwnode_handle_put(csdev->dev.fwnode);
1267 kfree(csdev->refcnt);
1271 static int coresight_orphan_match(struct device *dev, void *data)
1274 bool still_orphan = false;
1275 struct coresight_device *csdev, *i_csdev;
1276 struct coresight_connection *conn;
1279 i_csdev = to_coresight_device(dev);
1281 /* No need to check oneself */
1282 if (csdev == i_csdev)
1285 /* Move on to another component if no connection is orphan */
1286 if (!i_csdev->orphan)
1289 * Circle throuch all the connection of that component. If we find
1290 * an orphan connection whose name matches @csdev, link it.
1292 for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
1293 conn = &i_csdev->pdata->conns[i];
1295 /* Skip the port if FW doesn't describe it */
1296 if (!conn->child_fwnode)
1298 /* We have found at least one orphan connection */
1299 if (conn->child_dev == NULL) {
1300 /* Does it match this newly added device? */
1301 if (conn->child_fwnode == csdev->dev.fwnode) {
1302 ret = coresight_make_links(i_csdev,
1307 /* This component still has an orphan */
1308 still_orphan = true;
1313 i_csdev->orphan = still_orphan;
1316 * Returning '0' in case we didn't encounter any error,
1317 * ensures that all known component on the bus will be checked.
1322 static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
1324 return bus_for_each_dev(&coresight_bustype, NULL,
1325 csdev, coresight_orphan_match);
1329 static int coresight_fixup_device_conns(struct coresight_device *csdev)
1333 for (i = 0; i < csdev->pdata->nr_outport; i++) {
1334 struct coresight_connection *conn = &csdev->pdata->conns[i];
1336 if (!conn->child_fwnode)
1339 coresight_find_csdev_by_fwnode(conn->child_fwnode);
1340 if (conn->child_dev && conn->child_dev->has_conns_grp) {
1341 ret = coresight_make_links(csdev, conn,
1346 csdev->orphan = true;
1353 static int coresight_remove_match(struct device *dev, void *data)
1356 struct coresight_device *csdev, *iterator;
1357 struct coresight_connection *conn;
1360 iterator = to_coresight_device(dev);
1362 /* No need to check oneself */
1363 if (csdev == iterator)
1367 * Circle throuch all the connection of that component. If we find
1368 * a connection whose name matches @csdev, remove it.
1370 for (i = 0; i < iterator->pdata->nr_outport; i++) {
1371 conn = &iterator->pdata->conns[i];
1373 if (conn->child_dev == NULL || conn->child_fwnode == NULL)
1376 if (csdev->dev.fwnode == conn->child_fwnode) {
1377 iterator->orphan = true;
1378 coresight_remove_links(iterator, conn);
1380 * Drop the reference to the handle for the remote
1381 * device acquired in parsing the connections from
1384 fwnode_handle_put(conn->child_fwnode);
1385 conn->child_fwnode = NULL;
1386 /* No need to continue */
1392 * Returning '0' ensures that all known component on the
1393 * bus will be checked.
1399 * coresight_remove_conns - Remove references to this given devices
1400 * from the connections of other devices.
1402 static void coresight_remove_conns(struct coresight_device *csdev)
1405 * Another device will point to this device only if there is
1406 * an output port connected to this one. i.e, if the device
1407 * doesn't have at least one input port, there is no point
1408 * in searching all the devices.
1410 if (csdev->pdata->nr_inport)
1411 bus_for_each_dev(&coresight_bustype, NULL,
1412 csdev, coresight_remove_match);
1416 * coresight_timeout - loop until a bit has changed to a specific state.
1417 * @addr: base address of the area of interest.
1418 * @offset: address of a register, starting from @addr.
1419 * @position: the position of the bit of interest.
1420 * @value: the value the bit should have.
1422 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1423 * TIMEOUT_US has elapsed, which ever happens first.
1426 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
1431 for (i = TIMEOUT_US; i > 0; i--) {
1432 val = __raw_readl(addr + offset);
1433 /* waiting on the bit to go from 0 to 1 */
1435 if (val & BIT(position))
1437 /* waiting on the bit to go from 1 to 0 */
1439 if (!(val & BIT(position)))
1444 * Delay is arbitrary - the specification doesn't say how long
1445 * we are expected to wait. Extra check required to make sure
1446 * we don't wait needlessly on the last iteration.
1454 EXPORT_SYMBOL_GPL(coresight_timeout);
1457 * coresight_release_platform_data: Release references to the devices connected
1458 * to the output port of this device.
1460 void coresight_release_platform_data(struct coresight_device *csdev,
1461 struct coresight_platform_data *pdata)
1464 struct coresight_connection *conns = pdata->conns;
1466 for (i = 0; i < pdata->nr_outport; i++) {
1467 /* If we have made the links, remove them now */
1468 if (csdev && conns[i].child_dev)
1469 coresight_remove_links(csdev, &conns[i]);
1471 * Drop the refcount and clear the handle as this device
1474 if (conns[i].child_fwnode) {
1475 fwnode_handle_put(conns[i].child_fwnode);
1476 pdata->conns[i].child_fwnode = NULL;
1480 coresight_remove_conns_sysfs_group(csdev);
1483 struct coresight_device *coresight_register(struct coresight_desc *desc)
1488 atomic_t *refcnts = NULL;
1489 struct coresight_device *csdev;
1490 bool registered = false;
1492 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1498 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1499 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1500 link_subtype = desc->subtype.link_subtype;
1502 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1503 nr_refcnts = desc->pdata->nr_inport;
1504 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1505 nr_refcnts = desc->pdata->nr_outport;
1508 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1515 csdev->refcnt = refcnts;
1517 csdev->pdata = desc->pdata;
1519 csdev->type = desc->type;
1520 csdev->subtype = desc->subtype;
1521 csdev->ops = desc->ops;
1522 csdev->orphan = false;
1524 csdev->dev.type = &coresight_dev_type[desc->type];
1525 csdev->dev.groups = desc->groups;
1526 csdev->dev.parent = desc->dev;
1527 csdev->dev.release = coresight_device_release;
1528 csdev->dev.bus = &coresight_bustype;
1530 * Hold the reference to our parent device. This will be
1531 * dropped only in coresight_device_release().
1533 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1534 dev_set_name(&csdev->dev, "%s", desc->name);
1537 * Make sure the device registration and the connection fixup
1538 * are synchronised, so that we don't see uninitialised devices
1539 * on the coresight bus while trying to resolve the connections.
1541 mutex_lock(&coresight_mutex);
1543 ret = device_register(&csdev->dev);
1545 put_device(&csdev->dev);
1547 * All resources are free'd explicitly via
1548 * coresight_device_release(), triggered from put_device().
1553 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1554 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1555 ret = etm_perf_add_symlink_sink(csdev);
1558 device_unregister(&csdev->dev);
1560 * As with the above, all resources are free'd
1561 * explicitly via coresight_device_release() triggered
1562 * from put_device(), which is in turn called from
1563 * function device_unregister().
1568 /* Device is now registered */
1571 ret = coresight_create_conns_sysfs_group(csdev);
1573 ret = coresight_fixup_device_conns(csdev);
1575 ret = coresight_fixup_orphan_conns(csdev);
1576 if (!ret && cti_assoc_ops && cti_assoc_ops->add)
1577 cti_assoc_ops->add(csdev);
1580 mutex_unlock(&coresight_mutex);
1585 /* Unregister the device if needed */
1587 coresight_unregister(csdev);
1588 return ERR_PTR(ret);
1592 /* Cleanup the connection information */
1593 coresight_release_platform_data(NULL, desc->pdata);
1594 return ERR_PTR(ret);
1596 EXPORT_SYMBOL_GPL(coresight_register);
1598 void coresight_unregister(struct coresight_device *csdev)
1600 etm_perf_del_symlink_sink(csdev);
1601 /* Remove references of that device in the topology */
1602 if (cti_assoc_ops && cti_assoc_ops->remove)
1603 cti_assoc_ops->remove(csdev);
1604 coresight_remove_conns(csdev);
1605 coresight_clear_default_sink(csdev);
1606 coresight_release_platform_data(csdev, csdev->pdata);
1607 device_unregister(&csdev->dev);
1609 EXPORT_SYMBOL_GPL(coresight_unregister);
1613 * coresight_search_device_idx - Search the fwnode handle of a device
1614 * in the given dev_idx list. Must be called with the coresight_mutex held.
1616 * Returns the index of the entry, when found. Otherwise, -ENOENT.
1618 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1619 struct fwnode_handle *fwnode)
1623 for (i = 0; i < dict->nr_idx; i++)
1624 if (dict->fwnode_list[i] == fwnode)
1629 bool coresight_loses_context_with_cpu(struct device *dev)
1631 return fwnode_property_present(dev_fwnode(dev),
1632 "arm,coresight-loses-context-with-cpu");
1634 EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
1637 * coresight_alloc_device_name - Get an index for a given device in the
1638 * device index list specific to a driver. An index is allocated for a
1639 * device and is tracked with the fwnode_handle to prevent allocating
1640 * duplicate indices for the same device (e.g, if we defer probing of
1641 * a device due to dependencies), in case the index is requested again.
1643 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1648 struct fwnode_handle **list;
1650 mutex_lock(&coresight_mutex);
1652 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1654 /* Make space for the new entry */
1656 list = krealloc(dict->fwnode_list,
1657 (idx + 1) * sizeof(*dict->fwnode_list),
1659 if (ZERO_OR_NULL_PTR(list)) {
1664 list[idx] = dev_fwnode(dev);
1665 dict->fwnode_list = list;
1666 dict->nr_idx = idx + 1;
1669 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1671 mutex_unlock(&coresight_mutex);
1674 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1676 struct bus_type coresight_bustype = {
1677 .name = "coresight",
1680 static int __init coresight_init(void)
1684 ret = bus_register(&coresight_bustype);
1688 ret = etm_perf_init();
1690 bus_unregister(&coresight_bustype);
1695 static void __exit coresight_exit(void)
1698 bus_unregister(&coresight_bustype);
1701 module_init(coresight_init);
1702 module_exit(coresight_exit);
1704 MODULE_LICENSE("GPL v2");
1705 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1706 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1707 MODULE_DESCRIPTION("Arm CoreSight tracer driver");