drm/nouveau: consume the return of large GSP message
[drm/drm-misc.git] / drivers / hwtracing / coresight / coresight-core.c
blobea38ecf26fcbfb383eb40a222788bf5d7976013d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
4 */
6 #include <linux/build_bug.h>
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/types.h>
10 #include <linux/device.h>
11 #include <linux/io.h>
12 #include <linux/err.h>
13 #include <linux/export.h>
14 #include <linux/slab.h>
15 #include <linux/stringhash.h>
16 #include <linux/mutex.h>
17 #include <linux/clk.h>
18 #include <linux/coresight.h>
19 #include <linux/property.h>
20 #include <linux/delay.h>
21 #include <linux/pm_runtime.h>
23 #include "coresight-etm-perf.h"
24 #include "coresight-priv.h"
25 #include "coresight-syscfg.h"
28 * Mutex used to lock all sysfs enable and disable actions and loading and
29 * unloading devices by the Coresight core.
31 DEFINE_MUTEX(coresight_mutex);
32 static DEFINE_PER_CPU(struct coresight_device *, csdev_sink);
34 /**
35 * struct coresight_node - elements of a path, from source to sink
36 * @csdev: Address of an element.
37 * @link: hook to the list.
39 struct coresight_node {
40 struct coresight_device *csdev;
41 struct list_head link;
45 * When losing synchronisation a new barrier packet needs to be inserted at the
46 * beginning of the data collected in a buffer. That way the decoder knows that
47 * it needs to look for another sync sequence.
49 const u32 coresight_barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
50 EXPORT_SYMBOL_GPL(coresight_barrier_pkt);
52 static const struct cti_assoc_op *cti_assoc_ops;
54 void coresight_set_cti_ops(const struct cti_assoc_op *cti_op)
56 cti_assoc_ops = cti_op;
58 EXPORT_SYMBOL_GPL(coresight_set_cti_ops);
60 void coresight_remove_cti_ops(void)
62 cti_assoc_ops = NULL;
64 EXPORT_SYMBOL_GPL(coresight_remove_cti_ops);
66 void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev)
68 per_cpu(csdev_sink, cpu) = csdev;
70 EXPORT_SYMBOL_GPL(coresight_set_percpu_sink);
72 struct coresight_device *coresight_get_percpu_sink(int cpu)
74 return per_cpu(csdev_sink, cpu);
76 EXPORT_SYMBOL_GPL(coresight_get_percpu_sink);
78 static struct coresight_connection *
79 coresight_find_out_connection(struct coresight_device *src_dev,
80 struct coresight_device *dest_dev)
82 int i;
83 struct coresight_connection *conn;
85 for (i = 0; i < src_dev->pdata->nr_outconns; i++) {
86 conn = src_dev->pdata->out_conns[i];
87 if (conn->dest_dev == dest_dev)
88 return conn;
91 dev_err(&src_dev->dev,
92 "couldn't find output connection, src_dev: %s, dest_dev: %s\n",
93 dev_name(&src_dev->dev), dev_name(&dest_dev->dev));
95 return ERR_PTR(-ENODEV);
98 static inline u32 coresight_read_claim_tags(struct coresight_device *csdev)
100 return csdev_access_relaxed_read32(&csdev->access, CORESIGHT_CLAIMCLR);
103 static inline bool coresight_is_claimed_self_hosted(struct coresight_device *csdev)
105 return coresight_read_claim_tags(csdev) == CORESIGHT_CLAIM_SELF_HOSTED;
108 static inline bool coresight_is_claimed_any(struct coresight_device *csdev)
110 return coresight_read_claim_tags(csdev) != 0;
113 static inline void coresight_set_claim_tags(struct coresight_device *csdev)
115 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
116 CORESIGHT_CLAIMSET);
117 isb();
120 static inline void coresight_clear_claim_tags(struct coresight_device *csdev)
122 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
123 CORESIGHT_CLAIMCLR);
124 isb();
128 * coresight_claim_device_unlocked : Claim the device for self-hosted usage
129 * to prevent an external tool from touching this device. As per PSCI
130 * standards, section "Preserving the execution context" => "Debug and Trace
131 * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
132 * DBGCLAIM[0] is reserved for external tools.
134 * Called with CS_UNLOCKed for the component.
135 * Returns : 0 on success
137 int coresight_claim_device_unlocked(struct coresight_device *csdev)
139 if (WARN_ON(!csdev))
140 return -EINVAL;
142 if (coresight_is_claimed_any(csdev))
143 return -EBUSY;
145 coresight_set_claim_tags(csdev);
146 if (coresight_is_claimed_self_hosted(csdev))
147 return 0;
148 /* There was a race setting the tags, clean up and fail */
149 coresight_clear_claim_tags(csdev);
150 return -EBUSY;
152 EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked);
154 int coresight_claim_device(struct coresight_device *csdev)
156 int rc;
158 if (WARN_ON(!csdev))
159 return -EINVAL;
161 CS_UNLOCK(csdev->access.base);
162 rc = coresight_claim_device_unlocked(csdev);
163 CS_LOCK(csdev->access.base);
165 return rc;
167 EXPORT_SYMBOL_GPL(coresight_claim_device);
170 * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
171 * Called with CS_UNLOCKed for the component.
173 void coresight_disclaim_device_unlocked(struct coresight_device *csdev)
176 if (WARN_ON(!csdev))
177 return;
179 if (coresight_is_claimed_self_hosted(csdev))
180 coresight_clear_claim_tags(csdev);
181 else
183 * The external agent may have not honoured our claim
184 * and has manipulated it. Or something else has seriously
185 * gone wrong in our driver.
187 WARN_ON_ONCE(1);
189 EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked);
191 void coresight_disclaim_device(struct coresight_device *csdev)
193 if (WARN_ON(!csdev))
194 return;
196 CS_UNLOCK(csdev->access.base);
197 coresight_disclaim_device_unlocked(csdev);
198 CS_LOCK(csdev->access.base);
200 EXPORT_SYMBOL_GPL(coresight_disclaim_device);
203 * Add a helper as an output device. This function takes the @coresight_mutex
204 * because it's assumed that it's called from the helper device, outside of the
205 * core code where the mutex would already be held. Don't add new calls to this
206 * from inside the core code, instead try to add the new helper to the DT and
207 * ACPI where it will be picked up and linked automatically.
209 void coresight_add_helper(struct coresight_device *csdev,
210 struct coresight_device *helper)
212 int i;
213 struct coresight_connection conn = {};
214 struct coresight_connection *new_conn;
216 mutex_lock(&coresight_mutex);
217 conn.dest_fwnode = fwnode_handle_get(dev_fwnode(&helper->dev));
218 conn.dest_dev = helper;
219 conn.dest_port = conn.src_port = -1;
220 conn.src_dev = csdev;
223 * Check for duplicates because this is called every time a helper
224 * device is re-loaded. Existing connections will get re-linked
225 * automatically.
227 for (i = 0; i < csdev->pdata->nr_outconns; ++i)
228 if (csdev->pdata->out_conns[i]->dest_fwnode == conn.dest_fwnode)
229 goto unlock;
231 new_conn = coresight_add_out_conn(csdev->dev.parent, csdev->pdata,
232 &conn);
233 if (!IS_ERR(new_conn))
234 coresight_add_in_conn(new_conn);
236 unlock:
237 mutex_unlock(&coresight_mutex);
239 EXPORT_SYMBOL_GPL(coresight_add_helper);
241 static int coresight_enable_sink(struct coresight_device *csdev,
242 enum cs_mode mode, void *data)
244 return sink_ops(csdev)->enable(csdev, mode, data);
247 static void coresight_disable_sink(struct coresight_device *csdev)
249 sink_ops(csdev)->disable(csdev);
252 static int coresight_enable_link(struct coresight_device *csdev,
253 struct coresight_device *parent,
254 struct coresight_device *child)
256 int link_subtype;
257 struct coresight_connection *inconn, *outconn;
259 if (!parent || !child)
260 return -EINVAL;
262 inconn = coresight_find_out_connection(parent, csdev);
263 outconn = coresight_find_out_connection(csdev, child);
264 link_subtype = csdev->subtype.link_subtype;
266 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && IS_ERR(inconn))
267 return PTR_ERR(inconn);
268 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && IS_ERR(outconn))
269 return PTR_ERR(outconn);
271 return link_ops(csdev)->enable(csdev, inconn, outconn);
274 static void coresight_disable_link(struct coresight_device *csdev,
275 struct coresight_device *parent,
276 struct coresight_device *child)
278 struct coresight_connection *inconn, *outconn;
280 if (!parent || !child)
281 return;
283 inconn = coresight_find_out_connection(parent, csdev);
284 outconn = coresight_find_out_connection(csdev, child);
286 link_ops(csdev)->disable(csdev, inconn, outconn);
289 static bool coresight_is_helper(struct coresight_device *csdev)
291 return csdev->type == CORESIGHT_DEV_TYPE_HELPER;
294 static int coresight_enable_helper(struct coresight_device *csdev,
295 enum cs_mode mode, void *data)
297 return helper_ops(csdev)->enable(csdev, mode, data);
300 static void coresight_disable_helper(struct coresight_device *csdev)
302 helper_ops(csdev)->disable(csdev, NULL);
305 static void coresight_disable_helpers(struct coresight_device *csdev)
307 int i;
308 struct coresight_device *helper;
310 for (i = 0; i < csdev->pdata->nr_outconns; ++i) {
311 helper = csdev->pdata->out_conns[i]->dest_dev;
312 if (helper && coresight_is_helper(helper))
313 coresight_disable_helper(helper);
318 * Helper function to call source_ops(csdev)->disable and also disable the
319 * helpers.
321 * There is an imbalance between coresight_enable_path() and
322 * coresight_disable_path(). Enabling also enables the source's helpers as part
323 * of the path, but disabling always skips the first item in the path (which is
324 * the source), so sources and their helpers don't get disabled as part of that
325 * function and we need the extra step here.
327 void coresight_disable_source(struct coresight_device *csdev, void *data)
329 source_ops(csdev)->disable(csdev, data);
330 coresight_disable_helpers(csdev);
332 EXPORT_SYMBOL_GPL(coresight_disable_source);
335 * coresight_disable_path_from : Disable components in the given path beyond
336 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
337 * disabled.
339 static void coresight_disable_path_from(struct list_head *path,
340 struct coresight_node *nd)
342 u32 type;
343 struct coresight_device *csdev, *parent, *child;
345 if (!nd)
346 nd = list_first_entry(path, struct coresight_node, link);
348 list_for_each_entry_continue(nd, path, link) {
349 csdev = nd->csdev;
350 type = csdev->type;
353 * ETF devices are tricky... They can be a link or a sink,
354 * depending on how they are configured. If an ETF has been
355 * selected as a sink it will be configured as a sink, otherwise
356 * go ahead with the link configuration.
358 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
359 type = (csdev == coresight_get_sink(path)) ?
360 CORESIGHT_DEV_TYPE_SINK :
361 CORESIGHT_DEV_TYPE_LINK;
363 switch (type) {
364 case CORESIGHT_DEV_TYPE_SINK:
365 coresight_disable_sink(csdev);
366 break;
367 case CORESIGHT_DEV_TYPE_SOURCE:
369 * We skip the first node in the path assuming that it
370 * is the source. So we don't expect a source device in
371 * the middle of a path.
373 WARN_ON(1);
374 break;
375 case CORESIGHT_DEV_TYPE_LINK:
376 parent = list_prev_entry(nd, link)->csdev;
377 child = list_next_entry(nd, link)->csdev;
378 coresight_disable_link(csdev, parent, child);
379 break;
380 default:
381 break;
384 /* Disable all helpers adjacent along the path last */
385 coresight_disable_helpers(csdev);
389 void coresight_disable_path(struct list_head *path)
391 coresight_disable_path_from(path, NULL);
393 EXPORT_SYMBOL_GPL(coresight_disable_path);
395 static int coresight_enable_helpers(struct coresight_device *csdev,
396 enum cs_mode mode, void *data)
398 int i, ret = 0;
399 struct coresight_device *helper;
401 for (i = 0; i < csdev->pdata->nr_outconns; ++i) {
402 helper = csdev->pdata->out_conns[i]->dest_dev;
403 if (!helper || !coresight_is_helper(helper))
404 continue;
406 ret = coresight_enable_helper(helper, mode, data);
407 if (ret)
408 return ret;
411 return 0;
414 int coresight_enable_path(struct list_head *path, enum cs_mode mode,
415 void *sink_data)
417 int ret = 0;
418 u32 type;
419 struct coresight_node *nd;
420 struct coresight_device *csdev, *parent, *child;
422 list_for_each_entry_reverse(nd, path, link) {
423 csdev = nd->csdev;
424 type = csdev->type;
426 /* Enable all helpers adjacent to the path first */
427 ret = coresight_enable_helpers(csdev, mode, sink_data);
428 if (ret)
429 goto err;
431 * ETF devices are tricky... They can be a link or a sink,
432 * depending on how they are configured. If an ETF has been
433 * selected as a sink it will be configured as a sink, otherwise
434 * go ahead with the link configuration.
436 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
437 type = (csdev == coresight_get_sink(path)) ?
438 CORESIGHT_DEV_TYPE_SINK :
439 CORESIGHT_DEV_TYPE_LINK;
441 switch (type) {
442 case CORESIGHT_DEV_TYPE_SINK:
443 ret = coresight_enable_sink(csdev, mode, sink_data);
445 * Sink is the first component turned on. If we
446 * failed to enable the sink, there are no components
447 * that need disabling. Disabling the path here
448 * would mean we could disrupt an existing session.
450 if (ret)
451 goto out;
452 break;
453 case CORESIGHT_DEV_TYPE_SOURCE:
454 /* sources are enabled from either sysFS or Perf */
455 break;
456 case CORESIGHT_DEV_TYPE_LINK:
457 parent = list_prev_entry(nd, link)->csdev;
458 child = list_next_entry(nd, link)->csdev;
459 ret = coresight_enable_link(csdev, parent, child);
460 if (ret)
461 goto err;
462 break;
463 default:
464 goto err;
468 out:
469 return ret;
470 err:
471 coresight_disable_path_from(path, nd);
472 goto out;
475 struct coresight_device *coresight_get_sink(struct list_head *path)
477 struct coresight_device *csdev;
479 if (!path)
480 return NULL;
482 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
483 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
484 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
485 return NULL;
487 return csdev;
490 u32 coresight_get_sink_id(struct coresight_device *csdev)
492 if (!csdev->ea)
493 return 0;
496 * See function etm_perf_add_symlink_sink() to know where
497 * this comes from.
499 return (u32) (unsigned long) csdev->ea->var;
502 static int coresight_sink_by_id(struct device *dev, const void *data)
504 struct coresight_device *csdev = to_coresight_device(dev);
506 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
507 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
508 if (coresight_get_sink_id(csdev) == *(u32 *)data)
509 return 1;
512 return 0;
516 * coresight_get_sink_by_id - returns the sink that matches the id
517 * @id: Id of the sink to match
519 * The name of a sink is unique, whether it is found on the AMBA bus or
520 * otherwise. As such the hash of that name can easily be used to identify
521 * a sink.
523 struct coresight_device *coresight_get_sink_by_id(u32 id)
525 struct device *dev = NULL;
527 dev = bus_find_device(&coresight_bustype, NULL, &id,
528 coresight_sink_by_id);
530 return dev ? to_coresight_device(dev) : NULL;
534 * coresight_get_ref- Helper function to increase reference count to module
535 * and device.
537 * @csdev: The coresight device to get a reference on.
539 * Return true in successful case and power up the device.
540 * Return false when failed to get reference of module.
542 static inline bool coresight_get_ref(struct coresight_device *csdev)
544 struct device *dev = csdev->dev.parent;
546 /* Make sure the driver can't be removed */
547 if (!try_module_get(dev->driver->owner))
548 return false;
549 /* Make sure the device can't go away */
550 get_device(dev);
551 pm_runtime_get_sync(dev);
552 return true;
556 * coresight_put_ref- Helper function to decrease reference count to module
557 * and device. Power off the device.
559 * @csdev: The coresight device to decrement a reference from.
561 static inline void coresight_put_ref(struct coresight_device *csdev)
563 struct device *dev = csdev->dev.parent;
565 pm_runtime_put(dev);
566 put_device(dev);
567 module_put(dev->driver->owner);
571 * coresight_grab_device - Power up this device and any of the helper
572 * devices connected to it for trace operation. Since the helper devices
573 * don't appear on the trace path, they should be handled along with the
574 * master device.
576 static int coresight_grab_device(struct coresight_device *csdev)
578 int i;
580 for (i = 0; i < csdev->pdata->nr_outconns; i++) {
581 struct coresight_device *child;
583 child = csdev->pdata->out_conns[i]->dest_dev;
584 if (child && coresight_is_helper(child))
585 if (!coresight_get_ref(child))
586 goto err;
588 if (coresight_get_ref(csdev))
589 return 0;
590 err:
591 for (i--; i >= 0; i--) {
592 struct coresight_device *child;
594 child = csdev->pdata->out_conns[i]->dest_dev;
595 if (child && coresight_is_helper(child))
596 coresight_put_ref(child);
598 return -ENODEV;
602 * coresight_drop_device - Release this device and any of the helper
603 * devices connected to it.
605 static void coresight_drop_device(struct coresight_device *csdev)
607 int i;
609 coresight_put_ref(csdev);
610 for (i = 0; i < csdev->pdata->nr_outconns; i++) {
611 struct coresight_device *child;
613 child = csdev->pdata->out_conns[i]->dest_dev;
614 if (child && coresight_is_helper(child))
615 coresight_put_ref(child);
620 * _coresight_build_path - recursively build a path from a @csdev to a sink.
621 * @csdev: The device to start from.
622 * @sink: The final sink we want in this path.
623 * @path: The list to add devices to.
625 * The tree of Coresight device is traversed until @sink is found.
626 * From there the sink is added to the list along with all the devices that led
627 * to that point - the end result is a list from source to sink. In that list
628 * the source is the first device and the sink the last one.
630 static int _coresight_build_path(struct coresight_device *csdev,
631 struct coresight_device *sink,
632 struct list_head *path)
634 int i, ret;
635 bool found = false;
636 struct coresight_node *node;
638 /* The sink has been found. Enqueue the element */
639 if (csdev == sink)
640 goto out;
642 if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&
643 sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {
644 if (_coresight_build_path(sink, sink, path) == 0) {
645 found = true;
646 goto out;
650 /* Not a sink - recursively explore each port found on this element */
651 for (i = 0; i < csdev->pdata->nr_outconns; i++) {
652 struct coresight_device *child_dev;
654 child_dev = csdev->pdata->out_conns[i]->dest_dev;
655 if (child_dev &&
656 _coresight_build_path(child_dev, sink, path) == 0) {
657 found = true;
658 break;
662 if (!found)
663 return -ENODEV;
665 out:
667 * A path from this element to a sink has been found. The elements
668 * leading to the sink are already enqueued, all that is left to do
669 * is tell the PM runtime core we need this element and add a node
670 * for it.
672 ret = coresight_grab_device(csdev);
673 if (ret)
674 return ret;
676 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
677 if (!node)
678 return -ENOMEM;
680 node->csdev = csdev;
681 list_add(&node->link, path);
683 return 0;
686 struct list_head *coresight_build_path(struct coresight_device *source,
687 struct coresight_device *sink)
689 struct list_head *path;
690 int rc;
692 if (!sink)
693 return ERR_PTR(-EINVAL);
695 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
696 if (!path)
697 return ERR_PTR(-ENOMEM);
699 INIT_LIST_HEAD(path);
701 rc = _coresight_build_path(source, sink, path);
702 if (rc) {
703 kfree(path);
704 return ERR_PTR(rc);
707 return path;
711 * coresight_release_path - release a previously built path.
712 * @path: the path to release.
714 * Go through all the elements of a path and 1) removed it from the list and
715 * 2) free the memory allocated for each node.
717 void coresight_release_path(struct list_head *path)
719 struct coresight_device *csdev;
720 struct coresight_node *nd, *next;
722 list_for_each_entry_safe(nd, next, path, link) {
723 csdev = nd->csdev;
725 coresight_drop_device(csdev);
726 list_del(&nd->link);
727 kfree(nd);
730 kfree(path);
733 /* return true if the device is a suitable type for a default sink */
734 static inline bool coresight_is_def_sink_type(struct coresight_device *csdev)
736 /* sink & correct subtype */
737 if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
738 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
739 (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
740 return true;
741 return false;
745 * coresight_select_best_sink - return the best sink for use as default from
746 * the two provided.
748 * @sink: current best sink.
749 * @depth: search depth where current sink was found.
750 * @new_sink: new sink for comparison with current sink.
751 * @new_depth: search depth where new sink was found.
753 * Sinks prioritised according to coresight_dev_subtype_sink, with only
754 * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used.
756 * Where two sinks of equal priority are found, the sink closest to the
757 * source is used (smallest search depth).
759 * return @new_sink & update @depth if better than @sink, else return @sink.
761 static struct coresight_device *
762 coresight_select_best_sink(struct coresight_device *sink, int *depth,
763 struct coresight_device *new_sink, int new_depth)
765 bool update = false;
767 if (!sink) {
768 /* first found at this level */
769 update = true;
770 } else if (new_sink->subtype.sink_subtype >
771 sink->subtype.sink_subtype) {
772 /* found better sink */
773 update = true;
774 } else if ((new_sink->subtype.sink_subtype ==
775 sink->subtype.sink_subtype) &&
776 (*depth > new_depth)) {
777 /* found same but closer sink */
778 update = true;
781 if (update)
782 *depth = new_depth;
783 return update ? new_sink : sink;
787 * coresight_find_sink - recursive function to walk trace connections from
788 * source to find a suitable default sink.
790 * @csdev: source / current device to check.
791 * @depth: [in] search depth of calling dev, [out] depth of found sink.
793 * This will walk the connection path from a source (ETM) till a suitable
794 * sink is encountered and return that sink to the original caller.
796 * If current device is a plain sink return that & depth, otherwise recursively
797 * call child connections looking for a sink. Select best possible using
798 * coresight_select_best_sink.
800 * return best sink found, or NULL if not found at this node or child nodes.
802 static struct coresight_device *
803 coresight_find_sink(struct coresight_device *csdev, int *depth)
805 int i, curr_depth = *depth + 1, found_depth = 0;
806 struct coresight_device *found_sink = NULL;
808 if (coresight_is_def_sink_type(csdev)) {
809 found_depth = curr_depth;
810 found_sink = csdev;
811 if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
812 goto return_def_sink;
813 /* look past LINKSINK for something better */
817 * Not a sink we want - or possible child sink may be better.
818 * recursively explore each port found on this element.
820 for (i = 0; i < csdev->pdata->nr_outconns; i++) {
821 struct coresight_device *child_dev, *sink = NULL;
822 int child_depth = curr_depth;
824 child_dev = csdev->pdata->out_conns[i]->dest_dev;
825 if (child_dev)
826 sink = coresight_find_sink(child_dev, &child_depth);
828 if (sink)
829 found_sink = coresight_select_best_sink(found_sink,
830 &found_depth,
831 sink,
832 child_depth);
835 return_def_sink:
836 /* return found sink and depth */
837 if (found_sink)
838 *depth = found_depth;
839 return found_sink;
843 * coresight_find_default_sink: Find a sink suitable for use as a
844 * default sink.
846 * @csdev: starting source to find a connected sink.
848 * Walks connections graph looking for a suitable sink to enable for the
849 * supplied source. Uses CoreSight device subtypes and distance from source
850 * to select the best sink.
852 * If a sink is found, then the default sink for this device is set and
853 * will be automatically used in future.
855 * Used in cases where the CoreSight user (perf / sysfs) has not selected a
856 * sink.
858 struct coresight_device *
859 coresight_find_default_sink(struct coresight_device *csdev)
861 int depth = 0;
863 /* look for a default sink if we have not found for this device */
864 if (!csdev->def_sink) {
865 if (coresight_is_percpu_source(csdev))
866 csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));
867 if (!csdev->def_sink)
868 csdev->def_sink = coresight_find_sink(csdev, &depth);
870 return csdev->def_sink;
873 static int coresight_remove_sink_ref(struct device *dev, void *data)
875 struct coresight_device *sink = data;
876 struct coresight_device *source = to_coresight_device(dev);
878 if (source->def_sink == sink)
879 source->def_sink = NULL;
880 return 0;
884 * coresight_clear_default_sink: Remove all default sink references to the
885 * supplied sink.
887 * If supplied device is a sink, then check all the bus devices and clear
888 * out all the references to this sink from the coresight_device def_sink
889 * parameter.
891 * @csdev: coresight sink - remove references to this from all sources.
893 static void coresight_clear_default_sink(struct coresight_device *csdev)
895 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
896 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
897 bus_for_each_dev(&coresight_bustype, NULL, csdev,
898 coresight_remove_sink_ref);
902 static void coresight_device_release(struct device *dev)
904 struct coresight_device *csdev = to_coresight_device(dev);
906 fwnode_handle_put(csdev->dev.fwnode);
907 free_percpu(csdev->perf_sink_id_map.cpu_map);
908 kfree(csdev);
911 static int coresight_orphan_match(struct device *dev, void *data)
913 int i, ret = 0;
914 bool still_orphan = false;
915 struct coresight_device *dst_csdev = data;
916 struct coresight_device *src_csdev = to_coresight_device(dev);
917 struct coresight_connection *conn;
918 bool fixup_self = (src_csdev == dst_csdev);
920 /* Move on to another component if no connection is orphan */
921 if (!src_csdev->orphan)
922 return 0;
924 * Circle through all the connections of that component. If we find
925 * an orphan connection whose name matches @dst_csdev, link it.
927 for (i = 0; i < src_csdev->pdata->nr_outconns; i++) {
928 conn = src_csdev->pdata->out_conns[i];
930 /* Skip the port if it's already connected. */
931 if (conn->dest_dev)
932 continue;
935 * If we are at the "new" device, which triggered this search,
936 * we must find the remote device from the fwnode in the
937 * connection.
939 if (fixup_self)
940 dst_csdev = coresight_find_csdev_by_fwnode(
941 conn->dest_fwnode);
943 /* Does it match this newly added device? */
944 if (dst_csdev && conn->dest_fwnode == dst_csdev->dev.fwnode) {
945 ret = coresight_make_links(src_csdev, conn, dst_csdev);
946 if (ret)
947 return ret;
950 * Install the device connection. This also indicates that
951 * the links are operational on both ends.
953 conn->dest_dev = dst_csdev;
954 conn->src_dev = src_csdev;
956 ret = coresight_add_in_conn(conn);
957 if (ret)
958 return ret;
959 } else {
960 /* This component still has an orphan */
961 still_orphan = true;
965 src_csdev->orphan = still_orphan;
968 * Returning '0' in case we didn't encounter any error,
969 * ensures that all known component on the bus will be checked.
971 return 0;
974 static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
976 return bus_for_each_dev(&coresight_bustype, NULL,
977 csdev, coresight_orphan_match);
980 /* coresight_remove_conns - Remove other device's references to this device */
981 static void coresight_remove_conns(struct coresight_device *csdev)
983 int i, j;
984 struct coresight_connection *conn;
987 * Remove the input connection references from the destination device
988 * for each output connection.
990 for (i = 0; i < csdev->pdata->nr_outconns; i++) {
991 conn = csdev->pdata->out_conns[i];
992 if (!conn->dest_dev)
993 continue;
995 for (j = 0; j < conn->dest_dev->pdata->nr_inconns; ++j)
996 if (conn->dest_dev->pdata->in_conns[j] == conn) {
997 conn->dest_dev->pdata->in_conns[j] = NULL;
998 break;
1003 * For all input connections, remove references to this device.
1004 * Connection objects are shared so modifying this device's input
1005 * connections affects the other device's output connection.
1007 for (i = 0; i < csdev->pdata->nr_inconns; ++i) {
1008 conn = csdev->pdata->in_conns[i];
1009 /* Input conns array is sparse */
1010 if (!conn)
1011 continue;
1013 conn->src_dev->orphan = true;
1014 coresight_remove_links(conn->src_dev, conn);
1015 conn->dest_dev = NULL;
1020 * coresight_timeout - loop until a bit has changed to a specific register
1021 * state.
1022 * @csa: coresight device access for the device
1023 * @offset: Offset of the register from the base of the device.
1024 * @position: the position of the bit of interest.
1025 * @value: the value the bit should have.
1027 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1028 * TIMEOUT_US has elapsed, which ever happens first.
1030 int coresight_timeout(struct csdev_access *csa, u32 offset,
1031 int position, int value)
1033 int i;
1034 u32 val;
1036 for (i = TIMEOUT_US; i > 0; i--) {
1037 val = csdev_access_read32(csa, offset);
1038 /* waiting on the bit to go from 0 to 1 */
1039 if (value) {
1040 if (val & BIT(position))
1041 return 0;
1042 /* waiting on the bit to go from 1 to 0 */
1043 } else {
1044 if (!(val & BIT(position)))
1045 return 0;
1049 * Delay is arbitrary - the specification doesn't say how long
1050 * we are expected to wait. Extra check required to make sure
1051 * we don't wait needlessly on the last iteration.
1053 if (i - 1)
1054 udelay(1);
1057 return -EAGAIN;
1059 EXPORT_SYMBOL_GPL(coresight_timeout);
1061 u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset)
1063 return csdev_access_relaxed_read32(&csdev->access, offset);
1066 u32 coresight_read32(struct coresight_device *csdev, u32 offset)
1068 return csdev_access_read32(&csdev->access, offset);
1071 void coresight_relaxed_write32(struct coresight_device *csdev,
1072 u32 val, u32 offset)
1074 csdev_access_relaxed_write32(&csdev->access, val, offset);
1077 void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset)
1079 csdev_access_write32(&csdev->access, val, offset);
1082 u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset)
1084 return csdev_access_relaxed_read64(&csdev->access, offset);
1087 u64 coresight_read64(struct coresight_device *csdev, u32 offset)
1089 return csdev_access_read64(&csdev->access, offset);
1092 void coresight_relaxed_write64(struct coresight_device *csdev,
1093 u64 val, u32 offset)
1095 csdev_access_relaxed_write64(&csdev->access, val, offset);
1098 void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset)
1100 csdev_access_write64(&csdev->access, val, offset);
1104 * coresight_release_platform_data: Release references to the devices connected
1105 * to the output port of this device.
1107 void coresight_release_platform_data(struct coresight_device *csdev,
1108 struct device *dev,
1109 struct coresight_platform_data *pdata)
1111 int i;
1112 struct coresight_connection **conns = pdata->out_conns;
1114 for (i = 0; i < pdata->nr_outconns; i++) {
1115 /* If we have made the links, remove them now */
1116 if (csdev && conns[i]->dest_dev)
1117 coresight_remove_links(csdev, conns[i]);
1119 * Drop the refcount and clear the handle as this device
1120 * is going away
1122 fwnode_handle_put(conns[i]->dest_fwnode);
1123 conns[i]->dest_fwnode = NULL;
1124 devm_kfree(dev, conns[i]);
1126 devm_kfree(dev, pdata->out_conns);
1127 devm_kfree(dev, pdata->in_conns);
1128 devm_kfree(dev, pdata);
1129 if (csdev)
1130 coresight_remove_conns_sysfs_group(csdev);
1133 struct coresight_device *coresight_register(struct coresight_desc *desc)
1135 int ret;
1136 struct coresight_device *csdev;
1137 bool registered = false;
1139 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1140 if (!csdev) {
1141 ret = -ENOMEM;
1142 goto err_out;
1145 csdev->pdata = desc->pdata;
1147 csdev->type = desc->type;
1148 csdev->subtype = desc->subtype;
1149 csdev->ops = desc->ops;
1150 csdev->access = desc->access;
1151 csdev->orphan = true;
1153 csdev->dev.type = &coresight_dev_type[desc->type];
1154 csdev->dev.groups = desc->groups;
1155 csdev->dev.parent = desc->dev;
1156 csdev->dev.release = coresight_device_release;
1157 csdev->dev.bus = &coresight_bustype;
1159 * Hold the reference to our parent device. This will be
1160 * dropped only in coresight_device_release().
1162 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1163 dev_set_name(&csdev->dev, "%s", desc->name);
1165 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1166 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1167 spin_lock_init(&csdev->perf_sink_id_map.lock);
1168 csdev->perf_sink_id_map.cpu_map = alloc_percpu(atomic_t);
1169 if (!csdev->perf_sink_id_map.cpu_map) {
1170 kfree(csdev);
1171 ret = -ENOMEM;
1172 goto err_out;
1176 * Make sure the device registration and the connection fixup
1177 * are synchronised, so that we don't see uninitialised devices
1178 * on the coresight bus while trying to resolve the connections.
1180 mutex_lock(&coresight_mutex);
1182 ret = device_register(&csdev->dev);
1183 if (ret) {
1184 put_device(&csdev->dev);
1186 * All resources are free'd explicitly via
1187 * coresight_device_release(), triggered from put_device().
1189 goto out_unlock;
1192 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1193 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1194 ret = etm_perf_add_symlink_sink(csdev);
1196 if (ret) {
1197 device_unregister(&csdev->dev);
1199 * As with the above, all resources are free'd
1200 * explicitly via coresight_device_release() triggered
1201 * from put_device(), which is in turn called from
1202 * function device_unregister().
1204 goto out_unlock;
1207 /* Device is now registered */
1208 registered = true;
1210 ret = coresight_create_conns_sysfs_group(csdev);
1211 if (!ret)
1212 ret = coresight_fixup_orphan_conns(csdev);
1214 out_unlock:
1215 mutex_unlock(&coresight_mutex);
1216 /* Success */
1217 if (!ret) {
1218 if (cti_assoc_ops && cti_assoc_ops->add)
1219 cti_assoc_ops->add(csdev);
1220 return csdev;
1223 /* Unregister the device if needed */
1224 if (registered) {
1225 coresight_unregister(csdev);
1226 return ERR_PTR(ret);
1229 err_out:
1230 /* Cleanup the connection information */
1231 coresight_release_platform_data(NULL, desc->dev, desc->pdata);
1232 return ERR_PTR(ret);
1234 EXPORT_SYMBOL_GPL(coresight_register);
1236 void coresight_unregister(struct coresight_device *csdev)
1238 etm_perf_del_symlink_sink(csdev);
1239 /* Remove references of that device in the topology */
1240 if (cti_assoc_ops && cti_assoc_ops->remove)
1241 cti_assoc_ops->remove(csdev);
1242 coresight_remove_conns(csdev);
1243 coresight_clear_default_sink(csdev);
1244 coresight_release_platform_data(csdev, csdev->dev.parent, csdev->pdata);
1245 device_unregister(&csdev->dev);
1247 EXPORT_SYMBOL_GPL(coresight_unregister);
1251 * coresight_search_device_idx - Search the fwnode handle of a device
1252 * in the given dev_idx list. Must be called with the coresight_mutex held.
1254 * Returns the index of the entry, when found. Otherwise, -ENOENT.
1256 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1257 struct fwnode_handle *fwnode)
1259 int i;
1261 for (i = 0; i < dict->nr_idx; i++)
1262 if (dict->fwnode_list[i] == fwnode)
1263 return i;
1264 return -ENOENT;
1267 static bool coresight_compare_type(enum coresight_dev_type type_a,
1268 union coresight_dev_subtype subtype_a,
1269 enum coresight_dev_type type_b,
1270 union coresight_dev_subtype subtype_b)
1272 if (type_a != type_b)
1273 return false;
1275 switch (type_a) {
1276 case CORESIGHT_DEV_TYPE_SINK:
1277 return subtype_a.sink_subtype == subtype_b.sink_subtype;
1278 case CORESIGHT_DEV_TYPE_LINK:
1279 return subtype_a.link_subtype == subtype_b.link_subtype;
1280 case CORESIGHT_DEV_TYPE_LINKSINK:
1281 return subtype_a.link_subtype == subtype_b.link_subtype &&
1282 subtype_a.sink_subtype == subtype_b.sink_subtype;
1283 case CORESIGHT_DEV_TYPE_SOURCE:
1284 return subtype_a.source_subtype == subtype_b.source_subtype;
1285 case CORESIGHT_DEV_TYPE_HELPER:
1286 return subtype_a.helper_subtype == subtype_b.helper_subtype;
1287 default:
1288 return false;
1292 struct coresight_device *
1293 coresight_find_input_type(struct coresight_platform_data *pdata,
1294 enum coresight_dev_type type,
1295 union coresight_dev_subtype subtype)
1297 int i;
1298 struct coresight_connection *conn;
1300 for (i = 0; i < pdata->nr_inconns; ++i) {
1301 conn = pdata->in_conns[i];
1302 if (conn &&
1303 coresight_compare_type(type, subtype, conn->src_dev->type,
1304 conn->src_dev->subtype))
1305 return conn->src_dev;
1307 return NULL;
1309 EXPORT_SYMBOL_GPL(coresight_find_input_type);
1311 struct coresight_device *
1312 coresight_find_output_type(struct coresight_platform_data *pdata,
1313 enum coresight_dev_type type,
1314 union coresight_dev_subtype subtype)
1316 int i;
1317 struct coresight_connection *conn;
1319 for (i = 0; i < pdata->nr_outconns; ++i) {
1320 conn = pdata->out_conns[i];
1321 if (conn->dest_dev &&
1322 coresight_compare_type(type, subtype, conn->dest_dev->type,
1323 conn->dest_dev->subtype))
1324 return conn->dest_dev;
1326 return NULL;
1328 EXPORT_SYMBOL_GPL(coresight_find_output_type);
1330 bool coresight_loses_context_with_cpu(struct device *dev)
1332 return fwnode_property_present(dev_fwnode(dev),
1333 "arm,coresight-loses-context-with-cpu");
1335 EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
1338 * coresight_alloc_device_name - Get an index for a given device in the
1339 * device index list specific to a driver. An index is allocated for a
1340 * device and is tracked with the fwnode_handle to prevent allocating
1341 * duplicate indices for the same device (e.g, if we defer probing of
1342 * a device due to dependencies), in case the index is requested again.
1344 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1345 struct device *dev)
1347 int idx;
1348 char *name = NULL;
1349 struct fwnode_handle **list;
1351 mutex_lock(&coresight_mutex);
1353 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1354 if (idx < 0) {
1355 /* Make space for the new entry */
1356 idx = dict->nr_idx;
1357 list = krealloc_array(dict->fwnode_list,
1358 idx + 1, sizeof(*dict->fwnode_list),
1359 GFP_KERNEL);
1360 if (ZERO_OR_NULL_PTR(list)) {
1361 idx = -ENOMEM;
1362 goto done;
1365 list[idx] = dev_fwnode(dev);
1366 dict->fwnode_list = list;
1367 dict->nr_idx = idx + 1;
1370 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1371 done:
1372 mutex_unlock(&coresight_mutex);
1373 return name;
1375 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1377 const struct bus_type coresight_bustype = {
1378 .name = "coresight",
1381 static int __init coresight_init(void)
1383 int ret;
1385 ret = bus_register(&coresight_bustype);
1386 if (ret)
1387 return ret;
1389 ret = etm_perf_init();
1390 if (ret)
1391 goto exit_bus_unregister;
1393 /* initialise the coresight syscfg API */
1394 ret = cscfg_init();
1395 if (!ret)
1396 return 0;
1398 etm_perf_exit();
1399 exit_bus_unregister:
1400 bus_unregister(&coresight_bustype);
1401 return ret;
1404 static void __exit coresight_exit(void)
1406 cscfg_exit();
1407 etm_perf_exit();
1408 bus_unregister(&coresight_bustype);
1411 module_init(coresight_init);
1412 module_exit(coresight_exit);
1414 int coresight_init_driver(const char *drv, struct amba_driver *amba_drv,
1415 struct platform_driver *pdev_drv)
1417 int ret;
1419 ret = amba_driver_register(amba_drv);
1420 if (ret) {
1421 pr_err("%s: error registering AMBA driver\n", drv);
1422 return ret;
1425 ret = platform_driver_register(pdev_drv);
1426 if (!ret)
1427 return 0;
1429 pr_err("%s: error registering platform driver\n", drv);
1430 amba_driver_unregister(amba_drv);
1431 return ret;
1433 EXPORT_SYMBOL_GPL(coresight_init_driver);
1435 void coresight_remove_driver(struct amba_driver *amba_drv,
1436 struct platform_driver *pdev_drv)
1438 amba_driver_unregister(amba_drv);
1439 platform_driver_unregister(pdev_drv);
1441 EXPORT_SYMBOL_GPL(coresight_remove_driver);
1443 MODULE_LICENSE("GPL v2");
1444 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1445 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1446 MODULE_DESCRIPTION("Arm CoreSight tracer driver");