treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / hwtracing / coresight / coresight.c
blobef20f74c85fafae8ef122676fff468590464de84
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
4 */
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/types.h>
9 #include <linux/device.h>
10 #include <linux/io.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);
27 /**
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
47 * for STM.
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;
63 csdev = data;
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)
72 return 0;
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)
80 return 1;
82 return 0;
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 */
90 if (trace_id < 0)
91 return 0;
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)
100 int i;
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));
112 return -ENODEV;
115 static int coresight_find_link_outport(struct coresight_device *csdev,
116 struct coresight_device *child)
118 int i;
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));
130 return -ENODEV;
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);
151 isb();
154 static inline void coresight_clear_claim_tags(void __iomem *base)
156 writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMCLR);
157 isb();
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))
173 return -EBUSY;
175 coresight_set_claim_tags(base);
176 if (coresight_is_claimed_self_hosted(base))
177 return 0;
178 /* There was a race setting the tags, clean up and fail */
179 coresight_clear_claim_tags(base);
180 return -EBUSY;
183 int coresight_claim_device(void __iomem *base)
185 int rc;
187 CS_UNLOCK(base);
188 rc = coresight_claim_device_unlocked(base);
189 CS_LOCK(base);
191 return rc;
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);
203 else
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.
209 WARN_ON_ONCE(1);
212 void coresight_disclaim_device(void __iomem *base)
214 CS_UNLOCK(base);
215 coresight_disclaim_device_unlocked(base);
216 CS_LOCK(base);
219 static int coresight_enable_sink(struct coresight_device *csdev,
220 u32 mode, void *data)
222 int ret;
225 * We need to make sure the "new" session is compatible with the
226 * existing "mode" of operation.
228 if (!sink_ops(csdev)->enable)
229 return -EINVAL;
231 ret = sink_ops(csdev)->enable(csdev, mode, data);
232 if (ret)
233 return ret;
234 csdev->enable = true;
236 return 0;
239 static void coresight_disable_sink(struct coresight_device *csdev)
241 int ret;
243 if (!sink_ops(csdev)->disable)
244 return;
246 ret = sink_ops(csdev)->disable(csdev);
247 if (ret)
248 return;
249 csdev->enable = false;
252 static int coresight_enable_link(struct coresight_device *csdev,
253 struct coresight_device *parent,
254 struct coresight_device *child)
256 int ret = 0;
257 int link_subtype;
258 int inport, outport;
260 if (!parent || !child)
261 return -EINVAL;
263 inport = coresight_find_link_inport(csdev, parent);
264 outport = coresight_find_link_outport(csdev, child);
265 link_subtype = csdev->subtype.link_subtype;
267 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && inport < 0)
268 return inport;
269 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && outport < 0)
270 return outport;
272 if (link_ops(csdev)->enable)
273 ret = link_ops(csdev)->enable(csdev, inport, outport);
274 if (!ret)
275 csdev->enable = true;
277 return ret;
280 static void coresight_disable_link(struct coresight_device *csdev,
281 struct coresight_device *parent,
282 struct coresight_device *child)
284 int i, nr_conns;
285 int link_subtype;
286 int inport, outport;
288 if (!parent || !child)
289 return;
291 inport = coresight_find_link_inport(csdev, parent);
292 outport = coresight_find_link_outport(csdev, child);
293 link_subtype = csdev->subtype.link_subtype;
295 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
296 nr_conns = csdev->pdata->nr_inport;
297 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
298 nr_conns = csdev->pdata->nr_outport;
299 } else {
300 nr_conns = 1;
303 if (link_ops(csdev)->disable)
304 link_ops(csdev)->disable(csdev, inport, outport);
306 for (i = 0; i < nr_conns; i++)
307 if (atomic_read(&csdev->refcnt[i]) != 0)
308 return;
310 csdev->enable = false;
313 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
315 int ret;
317 if (!coresight_source_is_unique(csdev)) {
318 dev_warn(&csdev->dev, "traceID %d not unique\n",
319 source_ops(csdev)->trace_id(csdev));
320 return -EINVAL;
323 if (!csdev->enable) {
324 if (source_ops(csdev)->enable) {
325 ret = source_ops(csdev)->enable(csdev, NULL, mode);
326 if (ret)
327 return ret;
329 csdev->enable = true;
332 atomic_inc(csdev->refcnt);
334 return 0;
338 * coresight_disable_source - Drop the reference count by 1 and disable
339 * the device if there are no users left.
341 * @csdev - The coresight device to disable
343 * Returns true if the device has been disabled.
345 static bool coresight_disable_source(struct coresight_device *csdev)
347 if (atomic_dec_return(csdev->refcnt) == 0) {
348 if (source_ops(csdev)->disable)
349 source_ops(csdev)->disable(csdev, NULL);
350 csdev->enable = false;
352 return !csdev->enable;
356 * coresight_disable_path_from : Disable components in the given path beyond
357 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
358 * disabled.
360 static void coresight_disable_path_from(struct list_head *path,
361 struct coresight_node *nd)
363 u32 type;
364 struct coresight_device *csdev, *parent, *child;
366 if (!nd)
367 nd = list_first_entry(path, struct coresight_node, link);
369 list_for_each_entry_continue(nd, path, link) {
370 csdev = nd->csdev;
371 type = csdev->type;
374 * ETF devices are tricky... They can be a link or a sink,
375 * depending on how they are configured. If an ETF has been
376 * "activated" it will be configured as a sink, otherwise
377 * go ahead with the link configuration.
379 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
380 type = (csdev == coresight_get_sink(path)) ?
381 CORESIGHT_DEV_TYPE_SINK :
382 CORESIGHT_DEV_TYPE_LINK;
384 switch (type) {
385 case CORESIGHT_DEV_TYPE_SINK:
386 coresight_disable_sink(csdev);
387 break;
388 case CORESIGHT_DEV_TYPE_SOURCE:
390 * We skip the first node in the path assuming that it
391 * is the source. So we don't expect a source device in
392 * the middle of a path.
394 WARN_ON(1);
395 break;
396 case CORESIGHT_DEV_TYPE_LINK:
397 parent = list_prev_entry(nd, link)->csdev;
398 child = list_next_entry(nd, link)->csdev;
399 coresight_disable_link(csdev, parent, child);
400 break;
401 default:
402 break;
407 void coresight_disable_path(struct list_head *path)
409 coresight_disable_path_from(path, NULL);
412 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
415 int ret = 0;
416 u32 type;
417 struct coresight_node *nd;
418 struct coresight_device *csdev, *parent, *child;
420 list_for_each_entry_reverse(nd, path, link) {
421 csdev = nd->csdev;
422 type = csdev->type;
425 * ETF devices are tricky... They can be a link or a sink,
426 * depending on how they are configured. If an ETF has been
427 * "activated" it will be configured as a sink, otherwise
428 * go ahead with the link configuration.
430 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
431 type = (csdev == coresight_get_sink(path)) ?
432 CORESIGHT_DEV_TYPE_SINK :
433 CORESIGHT_DEV_TYPE_LINK;
435 switch (type) {
436 case CORESIGHT_DEV_TYPE_SINK:
437 ret = coresight_enable_sink(csdev, mode, sink_data);
439 * Sink is the first component turned on. If we
440 * failed to enable the sink, there are no components
441 * that need disabling. Disabling the path here
442 * would mean we could disrupt an existing session.
444 if (ret)
445 goto out;
446 break;
447 case CORESIGHT_DEV_TYPE_SOURCE:
448 /* sources are enabled from either sysFS or Perf */
449 break;
450 case CORESIGHT_DEV_TYPE_LINK:
451 parent = list_prev_entry(nd, link)->csdev;
452 child = list_next_entry(nd, link)->csdev;
453 ret = coresight_enable_link(csdev, parent, child);
454 if (ret)
455 goto err;
456 break;
457 default:
458 goto err;
462 out:
463 return ret;
464 err:
465 coresight_disable_path_from(path, nd);
466 goto out;
469 struct coresight_device *coresight_get_sink(struct list_head *path)
471 struct coresight_device *csdev;
473 if (!path)
474 return NULL;
476 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
477 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
478 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
479 return NULL;
481 return csdev;
484 static int coresight_enabled_sink(struct device *dev, const void *data)
486 const bool *reset = data;
487 struct coresight_device *csdev = to_coresight_device(dev);
489 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
490 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
491 csdev->activated) {
493 * Now that we have a handle on the sink for this session,
494 * disable the sysFS "enable_sink" flag so that possible
495 * concurrent perf session that wish to use another sink don't
496 * trip on it. Doing so has no ramification for the current
497 * session.
499 if (*reset)
500 csdev->activated = false;
502 return 1;
505 return 0;
509 * coresight_get_enabled_sink - returns the first enabled sink found on the bus
510 * @deactivate: Whether the 'enable_sink' flag should be reset
512 * When operated from perf the deactivate parameter should be set to 'true'.
513 * That way the "enabled_sink" flag of the sink that was selected can be reset,
514 * allowing for other concurrent perf sessions to choose a different sink.
516 * When operated from sysFS users have full control and as such the deactivate
517 * parameter should be set to 'false', hence mandating users to explicitly
518 * clear the flag.
520 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
522 struct device *dev = NULL;
524 dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
525 coresight_enabled_sink);
527 return dev ? to_coresight_device(dev) : NULL;
530 static int coresight_sink_by_id(struct device *dev, const void *data)
532 struct coresight_device *csdev = to_coresight_device(dev);
533 unsigned long hash;
535 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
536 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
538 if (!csdev->ea)
539 return 0;
541 * See function etm_perf_add_symlink_sink() to know where
542 * this comes from.
544 hash = (unsigned long)csdev->ea->var;
546 if ((u32)hash == *(u32 *)data)
547 return 1;
550 return 0;
554 * coresight_get_sink_by_id - returns the sink that matches the id
555 * @id: Id of the sink to match
557 * The name of a sink is unique, whether it is found on the AMBA bus or
558 * otherwise. As such the hash of that name can easily be used to identify
559 * a sink.
561 struct coresight_device *coresight_get_sink_by_id(u32 id)
563 struct device *dev = NULL;
565 dev = bus_find_device(&coresight_bustype, NULL, &id,
566 coresight_sink_by_id);
568 return dev ? to_coresight_device(dev) : NULL;
572 * coresight_grab_device - Power up this device and any of the helper
573 * devices connected to it for trace operation. Since the helper devices
574 * don't appear on the trace path, they should be handled along with the
575 * the master device.
577 static void coresight_grab_device(struct coresight_device *csdev)
579 int i;
581 for (i = 0; i < csdev->pdata->nr_outport; i++) {
582 struct coresight_device *child;
584 child = csdev->pdata->conns[i].child_dev;
585 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
586 pm_runtime_get_sync(child->dev.parent);
588 pm_runtime_get_sync(csdev->dev.parent);
592 * coresight_drop_device - Release this device and any of the helper
593 * devices connected to it.
595 static void coresight_drop_device(struct coresight_device *csdev)
597 int i;
599 pm_runtime_put(csdev->dev.parent);
600 for (i = 0; i < csdev->pdata->nr_outport; i++) {
601 struct coresight_device *child;
603 child = csdev->pdata->conns[i].child_dev;
604 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
605 pm_runtime_put(child->dev.parent);
610 * _coresight_build_path - recursively build a path from a @csdev to a sink.
611 * @csdev: The device to start from.
612 * @path: The list to add devices to.
614 * The tree of Coresight device is traversed until an activated sink is
615 * found. From there the sink is added to the list along with all the
616 * devices that led to that point - the end result is a list from source
617 * to sink. In that list the source is the first device and the sink the
618 * last one.
620 static int _coresight_build_path(struct coresight_device *csdev,
621 struct coresight_device *sink,
622 struct list_head *path)
624 int i;
625 bool found = false;
626 struct coresight_node *node;
628 /* An activated sink has been found. Enqueue the element */
629 if (csdev == sink)
630 goto out;
632 /* Not a sink - recursively explore each port found on this element */
633 for (i = 0; i < csdev->pdata->nr_outport; i++) {
634 struct coresight_device *child_dev;
636 child_dev = csdev->pdata->conns[i].child_dev;
637 if (child_dev &&
638 _coresight_build_path(child_dev, sink, path) == 0) {
639 found = true;
640 break;
644 if (!found)
645 return -ENODEV;
647 out:
649 * A path from this element to a sink has been found. The elements
650 * leading to the sink are already enqueued, all that is left to do
651 * is tell the PM runtime core we need this element and add a node
652 * for it.
654 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
655 if (!node)
656 return -ENOMEM;
658 coresight_grab_device(csdev);
659 node->csdev = csdev;
660 list_add(&node->link, path);
662 return 0;
665 struct list_head *coresight_build_path(struct coresight_device *source,
666 struct coresight_device *sink)
668 struct list_head *path;
669 int rc;
671 if (!sink)
672 return ERR_PTR(-EINVAL);
674 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
675 if (!path)
676 return ERR_PTR(-ENOMEM);
678 INIT_LIST_HEAD(path);
680 rc = _coresight_build_path(source, sink, path);
681 if (rc) {
682 kfree(path);
683 return ERR_PTR(rc);
686 return path;
690 * coresight_release_path - release a previously built path.
691 * @path: the path to release.
693 * Go through all the elements of a path and 1) removed it from the list and
694 * 2) free the memory allocated for each node.
696 void coresight_release_path(struct list_head *path)
698 struct coresight_device *csdev;
699 struct coresight_node *nd, *next;
701 list_for_each_entry_safe(nd, next, path, link) {
702 csdev = nd->csdev;
704 coresight_drop_device(csdev);
705 list_del(&nd->link);
706 kfree(nd);
709 kfree(path);
710 path = NULL;
713 /** coresight_validate_source - make sure a source has the right credentials
714 * @csdev: the device structure for a source.
715 * @function: the function this was called from.
717 * Assumes the coresight_mutex is held.
719 static int coresight_validate_source(struct coresight_device *csdev,
720 const char *function)
722 u32 type, subtype;
724 type = csdev->type;
725 subtype = csdev->subtype.source_subtype;
727 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
728 dev_err(&csdev->dev, "wrong device type in %s\n", function);
729 return -EINVAL;
732 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
733 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
734 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
735 return -EINVAL;
738 return 0;
741 int coresight_enable(struct coresight_device *csdev)
743 int cpu, ret = 0;
744 struct coresight_device *sink;
745 struct list_head *path;
746 enum coresight_dev_subtype_source subtype;
748 subtype = csdev->subtype.source_subtype;
750 mutex_lock(&coresight_mutex);
752 ret = coresight_validate_source(csdev, __func__);
753 if (ret)
754 goto out;
756 if (csdev->enable) {
758 * There could be multiple applications driving the software
759 * source. So keep the refcount for each such user when the
760 * source is already enabled.
762 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
763 atomic_inc(csdev->refcnt);
764 goto out;
768 * Search for a valid sink for this session but don't reset the
769 * "enable_sink" flag in sysFS. Users get to do that explicitly.
771 sink = coresight_get_enabled_sink(false);
772 if (!sink) {
773 ret = -EINVAL;
774 goto out;
777 path = coresight_build_path(csdev, sink);
778 if (IS_ERR(path)) {
779 pr_err("building path(s) failed\n");
780 ret = PTR_ERR(path);
781 goto out;
784 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
785 if (ret)
786 goto err_path;
788 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
789 if (ret)
790 goto err_source;
792 switch (subtype) {
793 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
795 * When working from sysFS it is important to keep track
796 * of the paths that were created so that they can be
797 * undone in 'coresight_disable()'. Since there can only
798 * be a single session per tracer (when working from sysFS)
799 * a per-cpu variable will do just fine.
801 cpu = source_ops(csdev)->cpu_id(csdev);
802 per_cpu(tracer_path, cpu) = path;
803 break;
804 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
805 stm_path = path;
806 break;
807 default:
808 /* We can't be here */
809 break;
812 out:
813 mutex_unlock(&coresight_mutex);
814 return ret;
816 err_source:
817 coresight_disable_path(path);
819 err_path:
820 coresight_release_path(path);
821 goto out;
823 EXPORT_SYMBOL_GPL(coresight_enable);
825 void coresight_disable(struct coresight_device *csdev)
827 int cpu, ret;
828 struct list_head *path = NULL;
830 mutex_lock(&coresight_mutex);
832 ret = coresight_validate_source(csdev, __func__);
833 if (ret)
834 goto out;
836 if (!csdev->enable || !coresight_disable_source(csdev))
837 goto out;
839 switch (csdev->subtype.source_subtype) {
840 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
841 cpu = source_ops(csdev)->cpu_id(csdev);
842 path = per_cpu(tracer_path, cpu);
843 per_cpu(tracer_path, cpu) = NULL;
844 break;
845 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
846 path = stm_path;
847 stm_path = NULL;
848 break;
849 default:
850 /* We can't be here */
851 break;
854 coresight_disable_path(path);
855 coresight_release_path(path);
857 out:
858 mutex_unlock(&coresight_mutex);
860 EXPORT_SYMBOL_GPL(coresight_disable);
862 static ssize_t enable_sink_show(struct device *dev,
863 struct device_attribute *attr, char *buf)
865 struct coresight_device *csdev = to_coresight_device(dev);
867 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
870 static ssize_t enable_sink_store(struct device *dev,
871 struct device_attribute *attr,
872 const char *buf, size_t size)
874 int ret;
875 unsigned long val;
876 struct coresight_device *csdev = to_coresight_device(dev);
878 ret = kstrtoul(buf, 10, &val);
879 if (ret)
880 return ret;
882 if (val)
883 csdev->activated = true;
884 else
885 csdev->activated = false;
887 return size;
890 static DEVICE_ATTR_RW(enable_sink);
892 static ssize_t enable_source_show(struct device *dev,
893 struct device_attribute *attr, char *buf)
895 struct coresight_device *csdev = to_coresight_device(dev);
897 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
900 static ssize_t enable_source_store(struct device *dev,
901 struct device_attribute *attr,
902 const char *buf, size_t size)
904 int ret = 0;
905 unsigned long val;
906 struct coresight_device *csdev = to_coresight_device(dev);
908 ret = kstrtoul(buf, 10, &val);
909 if (ret)
910 return ret;
912 if (val) {
913 ret = coresight_enable(csdev);
914 if (ret)
915 return ret;
916 } else {
917 coresight_disable(csdev);
920 return size;
922 static DEVICE_ATTR_RW(enable_source);
924 static struct attribute *coresight_sink_attrs[] = {
925 &dev_attr_enable_sink.attr,
926 NULL,
928 ATTRIBUTE_GROUPS(coresight_sink);
930 static struct attribute *coresight_source_attrs[] = {
931 &dev_attr_enable_source.attr,
932 NULL,
934 ATTRIBUTE_GROUPS(coresight_source);
936 static struct device_type coresight_dev_type[] = {
938 .name = "none",
941 .name = "sink",
942 .groups = coresight_sink_groups,
945 .name = "link",
948 .name = "linksink",
949 .groups = coresight_sink_groups,
952 .name = "source",
953 .groups = coresight_source_groups,
956 .name = "helper",
960 static void coresight_device_release(struct device *dev)
962 struct coresight_device *csdev = to_coresight_device(dev);
964 fwnode_handle_put(csdev->dev.fwnode);
965 kfree(csdev->refcnt);
966 kfree(csdev);
969 static int coresight_orphan_match(struct device *dev, void *data)
971 int i;
972 bool still_orphan = false;
973 struct coresight_device *csdev, *i_csdev;
974 struct coresight_connection *conn;
976 csdev = data;
977 i_csdev = to_coresight_device(dev);
979 /* No need to check oneself */
980 if (csdev == i_csdev)
981 return 0;
983 /* Move on to another component if no connection is orphan */
984 if (!i_csdev->orphan)
985 return 0;
987 * Circle throuch all the connection of that component. If we find
988 * an orphan connection whose name matches @csdev, link it.
990 for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
991 conn = &i_csdev->pdata->conns[i];
993 /* We have found at least one orphan connection */
994 if (conn->child_dev == NULL) {
995 /* Does it match this newly added device? */
996 if (conn->child_fwnode == csdev->dev.fwnode)
997 conn->child_dev = csdev;
998 else
999 /* This component still has an orphan */
1000 still_orphan = true;
1004 i_csdev->orphan = still_orphan;
1007 * Returning '0' ensures that all known component on the
1008 * bus will be checked.
1010 return 0;
1013 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
1016 * No need to check for a return value as orphan connection(s)
1017 * are hooked-up with each newly added component.
1019 bus_for_each_dev(&coresight_bustype, NULL,
1020 csdev, coresight_orphan_match);
1024 static void coresight_fixup_device_conns(struct coresight_device *csdev)
1026 int i;
1028 for (i = 0; i < csdev->pdata->nr_outport; i++) {
1029 struct coresight_connection *conn = &csdev->pdata->conns[i];
1030 struct device *dev = NULL;
1032 dev = bus_find_device_by_fwnode(&coresight_bustype, conn->child_fwnode);
1033 if (dev) {
1034 conn->child_dev = to_coresight_device(dev);
1035 /* and put reference from 'bus_find_device()' */
1036 put_device(dev);
1037 } else {
1038 csdev->orphan = true;
1039 conn->child_dev = NULL;
1044 static int coresight_remove_match(struct device *dev, void *data)
1046 int i;
1047 struct coresight_device *csdev, *iterator;
1048 struct coresight_connection *conn;
1050 csdev = data;
1051 iterator = to_coresight_device(dev);
1053 /* No need to check oneself */
1054 if (csdev == iterator)
1055 return 0;
1058 * Circle throuch all the connection of that component. If we find
1059 * a connection whose name matches @csdev, remove it.
1061 for (i = 0; i < iterator->pdata->nr_outport; i++) {
1062 conn = &iterator->pdata->conns[i];
1064 if (conn->child_dev == NULL)
1065 continue;
1067 if (csdev->dev.fwnode == conn->child_fwnode) {
1068 iterator->orphan = true;
1069 conn->child_dev = NULL;
1071 * Drop the reference to the handle for the remote
1072 * device acquired in parsing the connections from
1073 * platform data.
1075 fwnode_handle_put(conn->child_fwnode);
1076 /* No need to continue */
1077 break;
1082 * Returning '0' ensures that all known component on the
1083 * bus will be checked.
1085 return 0;
1089 * coresight_remove_conns - Remove references to this given devices
1090 * from the connections of other devices.
1092 static void coresight_remove_conns(struct coresight_device *csdev)
1095 * Another device will point to this device only if there is
1096 * an output port connected to this one. i.e, if the device
1097 * doesn't have at least one input port, there is no point
1098 * in searching all the devices.
1100 if (csdev->pdata->nr_inport)
1101 bus_for_each_dev(&coresight_bustype, NULL,
1102 csdev, coresight_remove_match);
1106 * coresight_timeout - loop until a bit has changed to a specific state.
1107 * @addr: base address of the area of interest.
1108 * @offset: address of a register, starting from @addr.
1109 * @position: the position of the bit of interest.
1110 * @value: the value the bit should have.
1112 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1113 * TIMEOUT_US has elapsed, which ever happens first.
1116 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
1118 int i;
1119 u32 val;
1121 for (i = TIMEOUT_US; i > 0; i--) {
1122 val = __raw_readl(addr + offset);
1123 /* waiting on the bit to go from 0 to 1 */
1124 if (value) {
1125 if (val & BIT(position))
1126 return 0;
1127 /* waiting on the bit to go from 1 to 0 */
1128 } else {
1129 if (!(val & BIT(position)))
1130 return 0;
1134 * Delay is arbitrary - the specification doesn't say how long
1135 * we are expected to wait. Extra check required to make sure
1136 * we don't wait needlessly on the last iteration.
1138 if (i - 1)
1139 udelay(1);
1142 return -EAGAIN;
1145 struct bus_type coresight_bustype = {
1146 .name = "coresight",
1149 static int __init coresight_init(void)
1151 return bus_register(&coresight_bustype);
1153 postcore_initcall(coresight_init);
1156 * coresight_release_platform_data: Release references to the devices connected
1157 * to the output port of this device.
1159 void coresight_release_platform_data(struct coresight_platform_data *pdata)
1161 int i;
1163 for (i = 0; i < pdata->nr_outport; i++) {
1164 if (pdata->conns[i].child_fwnode) {
1165 fwnode_handle_put(pdata->conns[i].child_fwnode);
1166 pdata->conns[i].child_fwnode = NULL;
1171 struct coresight_device *coresight_register(struct coresight_desc *desc)
1173 int ret;
1174 int link_subtype;
1175 int nr_refcnts = 1;
1176 atomic_t *refcnts = NULL;
1177 struct coresight_device *csdev;
1179 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1180 if (!csdev) {
1181 ret = -ENOMEM;
1182 goto err_out;
1185 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1186 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1187 link_subtype = desc->subtype.link_subtype;
1189 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1190 nr_refcnts = desc->pdata->nr_inport;
1191 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1192 nr_refcnts = desc->pdata->nr_outport;
1195 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1196 if (!refcnts) {
1197 ret = -ENOMEM;
1198 goto err_free_csdev;
1201 csdev->refcnt = refcnts;
1203 csdev->pdata = desc->pdata;
1205 csdev->type = desc->type;
1206 csdev->subtype = desc->subtype;
1207 csdev->ops = desc->ops;
1208 csdev->orphan = false;
1210 csdev->dev.type = &coresight_dev_type[desc->type];
1211 csdev->dev.groups = desc->groups;
1212 csdev->dev.parent = desc->dev;
1213 csdev->dev.release = coresight_device_release;
1214 csdev->dev.bus = &coresight_bustype;
1216 * Hold the reference to our parent device. This will be
1217 * dropped only in coresight_device_release().
1219 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1220 dev_set_name(&csdev->dev, "%s", desc->name);
1222 ret = device_register(&csdev->dev);
1223 if (ret) {
1224 put_device(&csdev->dev);
1226 * All resources are free'd explicitly via
1227 * coresight_device_release(), triggered from put_device().
1229 goto err_out;
1232 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1233 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1234 ret = etm_perf_add_symlink_sink(csdev);
1236 if (ret) {
1237 device_unregister(&csdev->dev);
1239 * As with the above, all resources are free'd
1240 * explicitly via coresight_device_release() triggered
1241 * from put_device(), which is in turn called from
1242 * function device_unregister().
1244 goto err_out;
1248 mutex_lock(&coresight_mutex);
1250 coresight_fixup_device_conns(csdev);
1251 coresight_fixup_orphan_conns(csdev);
1253 mutex_unlock(&coresight_mutex);
1255 return csdev;
1257 err_free_csdev:
1258 kfree(csdev);
1259 err_out:
1260 /* Cleanup the connection information */
1261 coresight_release_platform_data(desc->pdata);
1262 return ERR_PTR(ret);
1264 EXPORT_SYMBOL_GPL(coresight_register);
1266 void coresight_unregister(struct coresight_device *csdev)
1268 etm_perf_del_symlink_sink(csdev);
1269 /* Remove references of that device in the topology */
1270 coresight_remove_conns(csdev);
1271 coresight_release_platform_data(csdev->pdata);
1272 device_unregister(&csdev->dev);
1274 EXPORT_SYMBOL_GPL(coresight_unregister);
1278 * coresight_search_device_idx - Search the fwnode handle of a device
1279 * in the given dev_idx list. Must be called with the coresight_mutex held.
1281 * Returns the index of the entry, when found. Otherwise, -ENOENT.
1283 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1284 struct fwnode_handle *fwnode)
1286 int i;
1288 for (i = 0; i < dict->nr_idx; i++)
1289 if (dict->fwnode_list[i] == fwnode)
1290 return i;
1291 return -ENOENT;
1294 bool coresight_loses_context_with_cpu(struct device *dev)
1296 return fwnode_property_present(dev_fwnode(dev),
1297 "arm,coresight-loses-context-with-cpu");
1301 * coresight_alloc_device_name - Get an index for a given device in the
1302 * device index list specific to a driver. An index is allocated for a
1303 * device and is tracked with the fwnode_handle to prevent allocating
1304 * duplicate indices for the same device (e.g, if we defer probing of
1305 * a device due to dependencies), in case the index is requested again.
1307 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1308 struct device *dev)
1310 int idx;
1311 char *name = NULL;
1312 struct fwnode_handle **list;
1314 mutex_lock(&coresight_mutex);
1316 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1317 if (idx < 0) {
1318 /* Make space for the new entry */
1319 idx = dict->nr_idx;
1320 list = krealloc(dict->fwnode_list,
1321 (idx + 1) * sizeof(*dict->fwnode_list),
1322 GFP_KERNEL);
1323 if (ZERO_OR_NULL_PTR(list)) {
1324 idx = -ENOMEM;
1325 goto done;
1328 list[idx] = dev_fwnode(dev);
1329 dict->fwnode_list = list;
1330 dict->nr_idx = idx + 1;
1333 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1334 done:
1335 mutex_unlock(&coresight_mutex);
1336 return name;
1338 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);