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 barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
58 static int coresight_id_match(struct device *dev, void *data)
60 int trace_id, i_trace_id;
61 struct coresight_device *csdev, *i_csdev;
64 i_csdev = to_coresight_device(dev);
67 * No need to care about oneself and components that are not
68 * sources or not enabled
70 if (i_csdev == csdev || !i_csdev->enable ||
71 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
74 /* Get the source ID for both compoment */
75 trace_id = source_ops(csdev)->trace_id(csdev);
76 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
78 /* All you need is one */
79 if (trace_id == i_trace_id)
85 static int coresight_source_is_unique(struct coresight_device *csdev)
87 int trace_id = source_ops(csdev)->trace_id(csdev);
89 /* this shouldn't happen */
93 return !bus_for_each_dev(&coresight_bustype, NULL,
94 csdev, coresight_id_match);
97 static int coresight_find_link_inport(struct coresight_device *csdev,
98 struct coresight_device *parent)
101 struct coresight_connection *conn;
103 for (i = 0; i < parent->pdata->nr_outport; i++) {
104 conn = &parent->pdata->conns[i];
105 if (conn->child_dev == csdev)
106 return conn->child_port;
109 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
110 dev_name(&parent->dev), dev_name(&csdev->dev));
115 static int coresight_find_link_outport(struct coresight_device *csdev,
116 struct coresight_device *child)
119 struct coresight_connection *conn;
121 for (i = 0; i < csdev->pdata->nr_outport; i++) {
122 conn = &csdev->pdata->conns[i];
123 if (conn->child_dev == child)
124 return conn->outport;
127 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
128 dev_name(&csdev->dev), dev_name(&child->dev));
133 static inline u32 coresight_read_claim_tags(void __iomem *base)
135 return readl_relaxed(base + CORESIGHT_CLAIMCLR);
138 static inline bool coresight_is_claimed_self_hosted(void __iomem *base)
140 return coresight_read_claim_tags(base) == CORESIGHT_CLAIM_SELF_HOSTED;
143 static inline bool coresight_is_claimed_any(void __iomem *base)
145 return coresight_read_claim_tags(base) != 0;
148 static inline void coresight_set_claim_tags(void __iomem *base)
150 writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMSET);
154 static inline void coresight_clear_claim_tags(void __iomem *base)
156 writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMCLR);
161 * coresight_claim_device_unlocked : Claim the device for self-hosted usage
162 * to prevent an external tool from touching this device. As per PSCI
163 * standards, section "Preserving the execution context" => "Debug and Trace
164 * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
165 * DBGCLAIM[0] is reserved for external tools.
167 * Called with CS_UNLOCKed for the component.
168 * Returns : 0 on success
170 int coresight_claim_device_unlocked(void __iomem *base)
172 if (coresight_is_claimed_any(base))
175 coresight_set_claim_tags(base);
176 if (coresight_is_claimed_self_hosted(base))
178 /* There was a race setting the tags, clean up and fail */
179 coresight_clear_claim_tags(base);
183 int coresight_claim_device(void __iomem *base)
188 rc = coresight_claim_device_unlocked(base);
195 * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
196 * Called with CS_UNLOCKed for the component.
198 void coresight_disclaim_device_unlocked(void __iomem *base)
201 if (coresight_is_claimed_self_hosted(base))
202 coresight_clear_claim_tags(base);
205 * The external agent may have not honoured our claim
206 * and has manipulated it. Or something else has seriously
207 * gone wrong in our driver.
212 void coresight_disclaim_device(void __iomem *base)
215 coresight_disclaim_device_unlocked(base);
219 /* enable or disable an associated CTI device of the supplied CS device */
221 coresight_control_assoc_ectdev(struct coresight_device *csdev, bool enable)
224 struct coresight_device *ect_csdev = csdev->ect_dev;
230 if (ect_ops(ect_csdev)->enable)
231 ect_ret = ect_ops(ect_csdev)->enable(ect_csdev);
233 if (ect_ops(ect_csdev)->disable)
234 ect_ret = ect_ops(ect_csdev)->disable(ect_csdev);
237 /* output warning if ECT enable is preventing trace operation */
239 dev_info(&csdev->dev, "Associated ECT device (%s) %s failed\n",
240 dev_name(&ect_csdev->dev),
241 enable ? "enable" : "disable");
246 * Set the associated ect / cti device while holding the coresight_mutex
247 * to avoid a race with coresight_enable that may try to use this value.
249 void coresight_set_assoc_ectdev_mutex(struct coresight_device *csdev,
250 struct coresight_device *ect_csdev)
252 mutex_lock(&coresight_mutex);
253 csdev->ect_dev = ect_csdev;
254 mutex_unlock(&coresight_mutex);
257 static int coresight_enable_sink(struct coresight_device *csdev,
258 u32 mode, void *data)
263 * We need to make sure the "new" session is compatible with the
264 * existing "mode" of operation.
266 if (!sink_ops(csdev)->enable)
269 ret = coresight_control_assoc_ectdev(csdev, true);
272 ret = sink_ops(csdev)->enable(csdev, mode, data);
274 coresight_control_assoc_ectdev(csdev, false);
277 csdev->enable = true;
282 static void coresight_disable_sink(struct coresight_device *csdev)
286 if (!sink_ops(csdev)->disable)
289 ret = sink_ops(csdev)->disable(csdev);
292 coresight_control_assoc_ectdev(csdev, false);
293 csdev->enable = false;
296 static int coresight_enable_link(struct coresight_device *csdev,
297 struct coresight_device *parent,
298 struct coresight_device *child)
304 if (!parent || !child)
307 inport = coresight_find_link_inport(csdev, parent);
308 outport = coresight_find_link_outport(csdev, child);
309 link_subtype = csdev->subtype.link_subtype;
311 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && inport < 0)
313 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && outport < 0)
316 if (link_ops(csdev)->enable) {
317 ret = coresight_control_assoc_ectdev(csdev, true);
319 ret = link_ops(csdev)->enable(csdev, inport, outport);
321 coresight_control_assoc_ectdev(csdev, false);
326 csdev->enable = true;
331 static void coresight_disable_link(struct coresight_device *csdev,
332 struct coresight_device *parent,
333 struct coresight_device *child)
339 if (!parent || !child)
342 inport = coresight_find_link_inport(csdev, parent);
343 outport = coresight_find_link_outport(csdev, child);
344 link_subtype = csdev->subtype.link_subtype;
346 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
347 nr_conns = csdev->pdata->nr_inport;
348 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
349 nr_conns = csdev->pdata->nr_outport;
354 if (link_ops(csdev)->disable) {
355 link_ops(csdev)->disable(csdev, inport, outport);
356 coresight_control_assoc_ectdev(csdev, false);
359 for (i = 0; i < nr_conns; i++)
360 if (atomic_read(&csdev->refcnt[i]) != 0)
363 csdev->enable = false;
366 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
370 if (!coresight_source_is_unique(csdev)) {
371 dev_warn(&csdev->dev, "traceID %d not unique\n",
372 source_ops(csdev)->trace_id(csdev));
376 if (!csdev->enable) {
377 if (source_ops(csdev)->enable) {
378 ret = coresight_control_assoc_ectdev(csdev, true);
381 ret = source_ops(csdev)->enable(csdev, NULL, mode);
383 coresight_control_assoc_ectdev(csdev, false);
387 csdev->enable = true;
390 atomic_inc(csdev->refcnt);
396 * coresight_disable_source - Drop the reference count by 1 and disable
397 * the device if there are no users left.
399 * @csdev - The coresight device to disable
401 * Returns true if the device has been disabled.
403 static bool coresight_disable_source(struct coresight_device *csdev)
405 if (atomic_dec_return(csdev->refcnt) == 0) {
406 if (source_ops(csdev)->disable)
407 source_ops(csdev)->disable(csdev, NULL);
408 coresight_control_assoc_ectdev(csdev, false);
409 csdev->enable = false;
411 return !csdev->enable;
415 * coresight_disable_path_from : Disable components in the given path beyond
416 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
419 static void coresight_disable_path_from(struct list_head *path,
420 struct coresight_node *nd)
423 struct coresight_device *csdev, *parent, *child;
426 nd = list_first_entry(path, struct coresight_node, link);
428 list_for_each_entry_continue(nd, path, link) {
433 * ETF devices are tricky... They can be a link or a sink,
434 * depending on how they are configured. If an ETF has been
435 * "activated" it will be configured as a sink, otherwise
436 * go ahead with the link configuration.
438 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
439 type = (csdev == coresight_get_sink(path)) ?
440 CORESIGHT_DEV_TYPE_SINK :
441 CORESIGHT_DEV_TYPE_LINK;
444 case CORESIGHT_DEV_TYPE_SINK:
445 coresight_disable_sink(csdev);
447 case CORESIGHT_DEV_TYPE_SOURCE:
449 * We skip the first node in the path assuming that it
450 * is the source. So we don't expect a source device in
451 * the middle of a path.
455 case CORESIGHT_DEV_TYPE_LINK:
456 parent = list_prev_entry(nd, link)->csdev;
457 child = list_next_entry(nd, link)->csdev;
458 coresight_disable_link(csdev, parent, child);
466 void coresight_disable_path(struct list_head *path)
468 coresight_disable_path_from(path, NULL);
471 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
476 struct coresight_node *nd;
477 struct coresight_device *csdev, *parent, *child;
479 list_for_each_entry_reverse(nd, path, link) {
484 * ETF devices are tricky... They can be a link or a sink,
485 * depending on how they are configured. If an ETF has been
486 * "activated" it will be configured as a sink, otherwise
487 * go ahead with the link configuration.
489 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
490 type = (csdev == coresight_get_sink(path)) ?
491 CORESIGHT_DEV_TYPE_SINK :
492 CORESIGHT_DEV_TYPE_LINK;
495 case CORESIGHT_DEV_TYPE_SINK:
496 ret = coresight_enable_sink(csdev, mode, sink_data);
498 * Sink is the first component turned on. If we
499 * failed to enable the sink, there are no components
500 * that need disabling. Disabling the path here
501 * would mean we could disrupt an existing session.
506 case CORESIGHT_DEV_TYPE_SOURCE:
507 /* sources are enabled from either sysFS or Perf */
509 case CORESIGHT_DEV_TYPE_LINK:
510 parent = list_prev_entry(nd, link)->csdev;
511 child = list_next_entry(nd, link)->csdev;
512 ret = coresight_enable_link(csdev, parent, child);
524 coresight_disable_path_from(path, nd);
528 struct coresight_device *coresight_get_sink(struct list_head *path)
530 struct coresight_device *csdev;
535 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
536 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
537 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
543 static int coresight_enabled_sink(struct device *dev, const void *data)
545 const bool *reset = data;
546 struct coresight_device *csdev = to_coresight_device(dev);
548 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
549 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
552 * Now that we have a handle on the sink for this session,
553 * disable the sysFS "enable_sink" flag so that possible
554 * concurrent perf session that wish to use another sink don't
555 * trip on it. Doing so has no ramification for the current
559 csdev->activated = false;
568 * coresight_get_enabled_sink - returns the first enabled sink found on the bus
569 * @deactivate: Whether the 'enable_sink' flag should be reset
571 * When operated from perf the deactivate parameter should be set to 'true'.
572 * That way the "enabled_sink" flag of the sink that was selected can be reset,
573 * allowing for other concurrent perf sessions to choose a different sink.
575 * When operated from sysFS users have full control and as such the deactivate
576 * parameter should be set to 'false', hence mandating users to explicitly
579 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
581 struct device *dev = NULL;
583 dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
584 coresight_enabled_sink);
586 return dev ? to_coresight_device(dev) : NULL;
589 static int coresight_sink_by_id(struct device *dev, const void *data)
591 struct coresight_device *csdev = to_coresight_device(dev);
594 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
595 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
600 * See function etm_perf_add_symlink_sink() to know where
603 hash = (unsigned long)csdev->ea->var;
605 if ((u32)hash == *(u32 *)data)
613 * coresight_get_sink_by_id - returns the sink that matches the id
614 * @id: Id of the sink to match
616 * The name of a sink is unique, whether it is found on the AMBA bus or
617 * otherwise. As such the hash of that name can easily be used to identify
620 struct coresight_device *coresight_get_sink_by_id(u32 id)
622 struct device *dev = NULL;
624 dev = bus_find_device(&coresight_bustype, NULL, &id,
625 coresight_sink_by_id);
627 return dev ? to_coresight_device(dev) : NULL;
631 * coresight_grab_device - Power up this device and any of the helper
632 * devices connected to it for trace operation. Since the helper devices
633 * don't appear on the trace path, they should be handled along with the
636 static void coresight_grab_device(struct coresight_device *csdev)
640 for (i = 0; i < csdev->pdata->nr_outport; i++) {
641 struct coresight_device *child;
643 child = csdev->pdata->conns[i].child_dev;
644 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
645 pm_runtime_get_sync(child->dev.parent);
647 pm_runtime_get_sync(csdev->dev.parent);
651 * coresight_drop_device - Release this device and any of the helper
652 * devices connected to it.
654 static void coresight_drop_device(struct coresight_device *csdev)
658 pm_runtime_put(csdev->dev.parent);
659 for (i = 0; i < csdev->pdata->nr_outport; i++) {
660 struct coresight_device *child;
662 child = csdev->pdata->conns[i].child_dev;
663 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
664 pm_runtime_put(child->dev.parent);
669 * _coresight_build_path - recursively build a path from a @csdev to a sink.
670 * @csdev: The device to start from.
671 * @path: The list to add devices to.
673 * The tree of Coresight device is traversed until an activated sink is
674 * found. From there the sink is added to the list along with all the
675 * devices that led to that point - the end result is a list from source
676 * to sink. In that list the source is the first device and the sink the
679 static int _coresight_build_path(struct coresight_device *csdev,
680 struct coresight_device *sink,
681 struct list_head *path)
685 struct coresight_node *node;
687 /* An activated sink has been found. Enqueue the element */
691 /* Not a sink - recursively explore each port found on this element */
692 for (i = 0; i < csdev->pdata->nr_outport; i++) {
693 struct coresight_device *child_dev;
695 child_dev = csdev->pdata->conns[i].child_dev;
697 _coresight_build_path(child_dev, sink, path) == 0) {
708 * A path from this element to a sink has been found. The elements
709 * leading to the sink are already enqueued, all that is left to do
710 * is tell the PM runtime core we need this element and add a node
713 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
717 coresight_grab_device(csdev);
719 list_add(&node->link, path);
724 struct list_head *coresight_build_path(struct coresight_device *source,
725 struct coresight_device *sink)
727 struct list_head *path;
731 return ERR_PTR(-EINVAL);
733 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
735 return ERR_PTR(-ENOMEM);
737 INIT_LIST_HEAD(path);
739 rc = _coresight_build_path(source, sink, path);
749 * coresight_release_path - release a previously built path.
750 * @path: the path to release.
752 * Go through all the elements of a path and 1) removed it from the list and
753 * 2) free the memory allocated for each node.
755 void coresight_release_path(struct list_head *path)
757 struct coresight_device *csdev;
758 struct coresight_node *nd, *next;
760 list_for_each_entry_safe(nd, next, path, link) {
763 coresight_drop_device(csdev);
772 /* return true if the device is a suitable type for a default sink */
773 static inline bool coresight_is_def_sink_type(struct coresight_device *csdev)
775 /* sink & correct subtype */
776 if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
777 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
778 (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
784 * coresight_select_best_sink - return the best sink for use as default from
787 * @sink: current best sink.
788 * @depth: search depth where current sink was found.
789 * @new_sink: new sink for comparison with current sink.
790 * @new_depth: search depth where new sink was found.
792 * Sinks prioritised according to coresight_dev_subtype_sink, with only
793 * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used.
795 * Where two sinks of equal priority are found, the sink closest to the
796 * source is used (smallest search depth).
798 * return @new_sink & update @depth if better than @sink, else return @sink.
800 static struct coresight_device *
801 coresight_select_best_sink(struct coresight_device *sink, int *depth,
802 struct coresight_device *new_sink, int new_depth)
807 /* first found at this level */
809 } else if (new_sink->subtype.sink_subtype >
810 sink->subtype.sink_subtype) {
811 /* found better sink */
813 } else if ((new_sink->subtype.sink_subtype ==
814 sink->subtype.sink_subtype) &&
815 (*depth > new_depth)) {
816 /* found same but closer sink */
822 return update ? new_sink : sink;
826 * coresight_find_sink - recursive function to walk trace connections from
827 * source to find a suitable default sink.
829 * @csdev: source / current device to check.
830 * @depth: [in] search depth of calling dev, [out] depth of found sink.
832 * This will walk the connection path from a source (ETM) till a suitable
833 * sink is encountered and return that sink to the original caller.
835 * If current device is a plain sink return that & depth, otherwise recursively
836 * call child connections looking for a sink. Select best possible using
837 * coresight_select_best_sink.
839 * return best sink found, or NULL if not found at this node or child nodes.
841 static struct coresight_device *
842 coresight_find_sink(struct coresight_device *csdev, int *depth)
844 int i, curr_depth = *depth + 1, found_depth = 0;
845 struct coresight_device *found_sink = NULL;
847 if (coresight_is_def_sink_type(csdev)) {
848 found_depth = curr_depth;
850 if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
851 goto return_def_sink;
852 /* look past LINKSINK for something better */
856 * Not a sink we want - or possible child sink may be better.
857 * recursively explore each port found on this element.
859 for (i = 0; i < csdev->pdata->nr_outport; i++) {
860 struct coresight_device *child_dev, *sink = NULL;
861 int child_depth = curr_depth;
863 child_dev = csdev->pdata->conns[i].child_dev;
865 sink = coresight_find_sink(child_dev, &child_depth);
868 found_sink = coresight_select_best_sink(found_sink,
875 /* return found sink and depth */
877 *depth = found_depth;
882 * coresight_find_default_sink: Find a sink suitable for use as a
885 * @csdev: starting source to find a connected sink.
887 * Walks connections graph looking for a suitable sink to enable for the
888 * supplied source. Uses CoreSight device subtypes and distance from source
889 * to select the best sink.
891 * If a sink is found, then the default sink for this device is set and
892 * will be automatically used in future.
894 * Used in cases where the CoreSight user (perf / sysfs) has not selected a
897 struct coresight_device *
898 coresight_find_default_sink(struct coresight_device *csdev)
902 /* look for a default sink if we have not found for this device */
903 if (!csdev->def_sink)
904 csdev->def_sink = coresight_find_sink(csdev, &depth);
905 return csdev->def_sink;
908 static int coresight_remove_sink_ref(struct device *dev, void *data)
910 struct coresight_device *sink = data;
911 struct coresight_device *source = to_coresight_device(dev);
913 if (source->def_sink == sink)
914 source->def_sink = NULL;
919 * coresight_clear_default_sink: Remove all default sink references to the
922 * If supplied device is a sink, then check all the bus devices and clear
923 * out all the references to this sink from the coresight_device def_sink
926 * @csdev: coresight sink - remove references to this from all sources.
928 static void coresight_clear_default_sink(struct coresight_device *csdev)
930 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
931 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
932 bus_for_each_dev(&coresight_bustype, NULL, csdev,
933 coresight_remove_sink_ref);
937 /** coresight_validate_source - make sure a source has the right credentials
938 * @csdev: the device structure for a source.
939 * @function: the function this was called from.
941 * Assumes the coresight_mutex is held.
943 static int coresight_validate_source(struct coresight_device *csdev,
944 const char *function)
949 subtype = csdev->subtype.source_subtype;
951 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
952 dev_err(&csdev->dev, "wrong device type in %s\n", function);
956 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
957 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
958 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
965 int coresight_enable(struct coresight_device *csdev)
968 struct coresight_device *sink;
969 struct list_head *path;
970 enum coresight_dev_subtype_source subtype;
972 subtype = csdev->subtype.source_subtype;
974 mutex_lock(&coresight_mutex);
976 ret = coresight_validate_source(csdev, __func__);
982 * There could be multiple applications driving the software
983 * source. So keep the refcount for each such user when the
984 * source is already enabled.
986 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
987 atomic_inc(csdev->refcnt);
992 * Search for a valid sink for this session but don't reset the
993 * "enable_sink" flag in sysFS. Users get to do that explicitly.
995 sink = coresight_get_enabled_sink(false);
1001 path = coresight_build_path(csdev, sink);
1003 pr_err("building path(s) failed\n");
1004 ret = PTR_ERR(path);
1008 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
1012 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
1017 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1019 * When working from sysFS it is important to keep track
1020 * of the paths that were created so that they can be
1021 * undone in 'coresight_disable()'. Since there can only
1022 * be a single session per tracer (when working from sysFS)
1023 * a per-cpu variable will do just fine.
1025 cpu = source_ops(csdev)->cpu_id(csdev);
1026 per_cpu(tracer_path, cpu) = path;
1028 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1032 /* We can't be here */
1037 mutex_unlock(&coresight_mutex);
1041 coresight_disable_path(path);
1044 coresight_release_path(path);
1047 EXPORT_SYMBOL_GPL(coresight_enable);
1049 void coresight_disable(struct coresight_device *csdev)
1052 struct list_head *path = NULL;
1054 mutex_lock(&coresight_mutex);
1056 ret = coresight_validate_source(csdev, __func__);
1060 if (!csdev->enable || !coresight_disable_source(csdev))
1063 switch (csdev->subtype.source_subtype) {
1064 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1065 cpu = source_ops(csdev)->cpu_id(csdev);
1066 path = per_cpu(tracer_path, cpu);
1067 per_cpu(tracer_path, cpu) = NULL;
1069 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1074 /* We can't be here */
1078 coresight_disable_path(path);
1079 coresight_release_path(path);
1082 mutex_unlock(&coresight_mutex);
1084 EXPORT_SYMBOL_GPL(coresight_disable);
1086 static ssize_t enable_sink_show(struct device *dev,
1087 struct device_attribute *attr, char *buf)
1089 struct coresight_device *csdev = to_coresight_device(dev);
1091 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
1094 static ssize_t enable_sink_store(struct device *dev,
1095 struct device_attribute *attr,
1096 const char *buf, size_t size)
1100 struct coresight_device *csdev = to_coresight_device(dev);
1102 ret = kstrtoul(buf, 10, &val);
1107 csdev->activated = true;
1109 csdev->activated = false;
1114 static DEVICE_ATTR_RW(enable_sink);
1116 static ssize_t enable_source_show(struct device *dev,
1117 struct device_attribute *attr, char *buf)
1119 struct coresight_device *csdev = to_coresight_device(dev);
1121 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
1124 static ssize_t enable_source_store(struct device *dev,
1125 struct device_attribute *attr,
1126 const char *buf, size_t size)
1130 struct coresight_device *csdev = to_coresight_device(dev);
1132 ret = kstrtoul(buf, 10, &val);
1137 ret = coresight_enable(csdev);
1141 coresight_disable(csdev);
1146 static DEVICE_ATTR_RW(enable_source);
1148 static struct attribute *coresight_sink_attrs[] = {
1149 &dev_attr_enable_sink.attr,
1152 ATTRIBUTE_GROUPS(coresight_sink);
1154 static struct attribute *coresight_source_attrs[] = {
1155 &dev_attr_enable_source.attr,
1158 ATTRIBUTE_GROUPS(coresight_source);
1160 static struct device_type coresight_dev_type[] = {
1166 .groups = coresight_sink_groups,
1173 .groups = coresight_sink_groups,
1177 .groups = coresight_source_groups,
1187 static void coresight_device_release(struct device *dev)
1189 struct coresight_device *csdev = to_coresight_device(dev);
1191 cti_remove_assoc_from_csdev(csdev);
1192 fwnode_handle_put(csdev->dev.fwnode);
1193 kfree(csdev->refcnt);
1197 static int coresight_orphan_match(struct device *dev, void *data)
1200 bool still_orphan = false;
1201 struct coresight_device *csdev, *i_csdev;
1202 struct coresight_connection *conn;
1205 i_csdev = to_coresight_device(dev);
1207 /* No need to check oneself */
1208 if (csdev == i_csdev)
1211 /* Move on to another component if no connection is orphan */
1212 if (!i_csdev->orphan)
1215 * Circle throuch all the connection of that component. If we find
1216 * an orphan connection whose name matches @csdev, link it.
1218 for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
1219 conn = &i_csdev->pdata->conns[i];
1221 /* Skip the port if FW doesn't describe it */
1222 if (!conn->child_fwnode)
1224 /* We have found at least one orphan connection */
1225 if (conn->child_dev == NULL) {
1226 /* Does it match this newly added device? */
1227 if (conn->child_fwnode == csdev->dev.fwnode) {
1228 ret = coresight_make_links(i_csdev,
1233 /* This component still has an orphan */
1234 still_orphan = true;
1239 i_csdev->orphan = still_orphan;
1242 * Returning '0' in case we didn't encounter any error,
1243 * ensures that all known component on the bus will be checked.
1248 static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
1250 return bus_for_each_dev(&coresight_bustype, NULL,
1251 csdev, coresight_orphan_match);
1255 static int coresight_fixup_device_conns(struct coresight_device *csdev)
1259 for (i = 0; i < csdev->pdata->nr_outport; i++) {
1260 struct coresight_connection *conn = &csdev->pdata->conns[i];
1262 if (!conn->child_fwnode)
1265 coresight_find_csdev_by_fwnode(conn->child_fwnode);
1266 if (conn->child_dev) {
1267 ret = coresight_make_links(csdev, conn,
1272 csdev->orphan = true;
1279 static int coresight_remove_match(struct device *dev, void *data)
1282 struct coresight_device *csdev, *iterator;
1283 struct coresight_connection *conn;
1286 iterator = to_coresight_device(dev);
1288 /* No need to check oneself */
1289 if (csdev == iterator)
1293 * Circle throuch all the connection of that component. If we find
1294 * a connection whose name matches @csdev, remove it.
1296 for (i = 0; i < iterator->pdata->nr_outport; i++) {
1297 conn = &iterator->pdata->conns[i];
1299 if (conn->child_dev == NULL || conn->child_fwnode == NULL)
1302 if (csdev->dev.fwnode == conn->child_fwnode) {
1303 iterator->orphan = true;
1304 coresight_remove_links(iterator, conn);
1306 * Drop the reference to the handle for the remote
1307 * device acquired in parsing the connections from
1310 fwnode_handle_put(conn->child_fwnode);
1311 /* No need to continue */
1317 * Returning '0' ensures that all known component on the
1318 * bus will be checked.
1324 * coresight_remove_conns - Remove references to this given devices
1325 * from the connections of other devices.
1327 static void coresight_remove_conns(struct coresight_device *csdev)
1330 * Another device will point to this device only if there is
1331 * an output port connected to this one. i.e, if the device
1332 * doesn't have at least one input port, there is no point
1333 * in searching all the devices.
1335 if (csdev->pdata->nr_inport)
1336 bus_for_each_dev(&coresight_bustype, NULL,
1337 csdev, coresight_remove_match);
1341 * coresight_timeout - loop until a bit has changed to a specific state.
1342 * @addr: base address of the area of interest.
1343 * @offset: address of a register, starting from @addr.
1344 * @position: the position of the bit of interest.
1345 * @value: the value the bit should have.
1347 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1348 * TIMEOUT_US has elapsed, which ever happens first.
1351 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
1356 for (i = TIMEOUT_US; i > 0; i--) {
1357 val = __raw_readl(addr + offset);
1358 /* waiting on the bit to go from 0 to 1 */
1360 if (val & BIT(position))
1362 /* waiting on the bit to go from 1 to 0 */
1364 if (!(val & BIT(position)))
1369 * Delay is arbitrary - the specification doesn't say how long
1370 * we are expected to wait. Extra check required to make sure
1371 * we don't wait needlessly on the last iteration.
1380 struct bus_type coresight_bustype = {
1381 .name = "coresight",
1384 static int __init coresight_init(void)
1386 return bus_register(&coresight_bustype);
1388 postcore_initcall(coresight_init);
1391 * coresight_release_platform_data: Release references to the devices connected
1392 * to the output port of this device.
1394 void coresight_release_platform_data(struct coresight_device *csdev,
1395 struct coresight_platform_data *pdata)
1398 struct coresight_connection *conns = pdata->conns;
1400 for (i = 0; i < pdata->nr_outport; i++) {
1401 /* If we have made the links, remove them now */
1402 if (csdev && conns[i].child_dev)
1403 coresight_remove_links(csdev, &conns[i]);
1405 * Drop the refcount and clear the handle as this device
1408 if (conns[i].child_fwnode) {
1409 fwnode_handle_put(conns[i].child_fwnode);
1410 pdata->conns[i].child_fwnode = NULL;
1414 coresight_remove_conns_sysfs_group(csdev);
1417 struct coresight_device *coresight_register(struct coresight_desc *desc)
1422 atomic_t *refcnts = NULL;
1423 struct coresight_device *csdev;
1425 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1431 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1432 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1433 link_subtype = desc->subtype.link_subtype;
1435 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1436 nr_refcnts = desc->pdata->nr_inport;
1437 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1438 nr_refcnts = desc->pdata->nr_outport;
1441 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1444 goto err_free_csdev;
1447 csdev->refcnt = refcnts;
1449 csdev->pdata = desc->pdata;
1451 csdev->type = desc->type;
1452 csdev->subtype = desc->subtype;
1453 csdev->ops = desc->ops;
1454 csdev->orphan = false;
1456 csdev->dev.type = &coresight_dev_type[desc->type];
1457 csdev->dev.groups = desc->groups;
1458 csdev->dev.parent = desc->dev;
1459 csdev->dev.release = coresight_device_release;
1460 csdev->dev.bus = &coresight_bustype;
1462 * Hold the reference to our parent device. This will be
1463 * dropped only in coresight_device_release().
1465 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1466 dev_set_name(&csdev->dev, "%s", desc->name);
1468 ret = device_register(&csdev->dev);
1470 put_device(&csdev->dev);
1472 * All resources are free'd explicitly via
1473 * coresight_device_release(), triggered from put_device().
1478 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1479 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1480 ret = etm_perf_add_symlink_sink(csdev);
1483 device_unregister(&csdev->dev);
1485 * As with the above, all resources are free'd
1486 * explicitly via coresight_device_release() triggered
1487 * from put_device(), which is in turn called from
1488 * function device_unregister().
1494 mutex_lock(&coresight_mutex);
1496 ret = coresight_create_conns_sysfs_group(csdev);
1498 ret = coresight_fixup_device_conns(csdev);
1500 ret = coresight_fixup_orphan_conns(csdev);
1502 cti_add_assoc_to_csdev(csdev);
1504 mutex_unlock(&coresight_mutex);
1506 coresight_unregister(csdev);
1507 return ERR_PTR(ret);
1515 /* Cleanup the connection information */
1516 coresight_release_platform_data(NULL, desc->pdata);
1517 return ERR_PTR(ret);
1519 EXPORT_SYMBOL_GPL(coresight_register);
1521 void coresight_unregister(struct coresight_device *csdev)
1523 etm_perf_del_symlink_sink(csdev);
1524 /* Remove references of that device in the topology */
1525 coresight_remove_conns(csdev);
1526 coresight_clear_default_sink(csdev);
1527 coresight_release_platform_data(csdev, csdev->pdata);
1528 device_unregister(&csdev->dev);
1530 EXPORT_SYMBOL_GPL(coresight_unregister);
1534 * coresight_search_device_idx - Search the fwnode handle of a device
1535 * in the given dev_idx list. Must be called with the coresight_mutex held.
1537 * Returns the index of the entry, when found. Otherwise, -ENOENT.
1539 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1540 struct fwnode_handle *fwnode)
1544 for (i = 0; i < dict->nr_idx; i++)
1545 if (dict->fwnode_list[i] == fwnode)
1550 bool coresight_loses_context_with_cpu(struct device *dev)
1552 return fwnode_property_present(dev_fwnode(dev),
1553 "arm,coresight-loses-context-with-cpu");
1557 * coresight_alloc_device_name - Get an index for a given device in the
1558 * device index list specific to a driver. An index is allocated for a
1559 * device and is tracked with the fwnode_handle to prevent allocating
1560 * duplicate indices for the same device (e.g, if we defer probing of
1561 * a device due to dependencies), in case the index is requested again.
1563 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1568 struct fwnode_handle **list;
1570 mutex_lock(&coresight_mutex);
1572 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1574 /* Make space for the new entry */
1576 list = krealloc(dict->fwnode_list,
1577 (idx + 1) * sizeof(*dict->fwnode_list),
1579 if (ZERO_OR_NULL_PTR(list)) {
1584 list[idx] = dev_fwnode(dev);
1585 dict->fwnode_list = list;
1586 dict->nr_idx = idx + 1;
1589 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1591 mutex_unlock(&coresight_mutex);
1594 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);