Linux 4.18.10
[linux/fpc-iii.git] / drivers / hwtracing / coresight / coresight.c
blobb673718952f6f2b2e7cc027fdcffc975a5826818
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/mutex.h>
15 #include <linux/clk.h>
16 #include <linux/coresight.h>
17 #include <linux/of_platform.h>
18 #include <linux/delay.h>
19 #include <linux/pm_runtime.h>
21 #include "coresight-priv.h"
23 static DEFINE_MUTEX(coresight_mutex);
25 /**
26 * struct coresight_node - elements of a path, from source to sink
27 * @csdev: Address of an element.
28 * @link: hook to the list.
30 struct coresight_node {
31 struct coresight_device *csdev;
32 struct list_head link;
36 * When operating Coresight drivers from the sysFS interface, only a single
37 * path can exist from a tracer (associated to a CPU) to a sink.
39 static DEFINE_PER_CPU(struct list_head *, tracer_path);
42 * As of this writing only a single STM can be found in CS topologies. Since
43 * there is no way to know if we'll ever see more and what kind of
44 * configuration they will enact, for the time being only define a single path
45 * for STM.
47 static struct list_head *stm_path;
50 * When losing synchronisation a new barrier packet needs to be inserted at the
51 * beginning of the data collected in a buffer. That way the decoder knows that
52 * it needs to look for another sync sequence.
54 const u32 barrier_pkt[5] = {0x7fffffff, 0x7fffffff,
55 0x7fffffff, 0x7fffffff, 0x0};
57 static int coresight_id_match(struct device *dev, void *data)
59 int trace_id, i_trace_id;
60 struct coresight_device *csdev, *i_csdev;
62 csdev = data;
63 i_csdev = to_coresight_device(dev);
66 * No need to care about oneself and components that are not
67 * sources or not enabled
69 if (i_csdev == csdev || !i_csdev->enable ||
70 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
71 return 0;
73 /* Get the source ID for both compoment */
74 trace_id = source_ops(csdev)->trace_id(csdev);
75 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
77 /* All you need is one */
78 if (trace_id == i_trace_id)
79 return 1;
81 return 0;
84 static int coresight_source_is_unique(struct coresight_device *csdev)
86 int trace_id = source_ops(csdev)->trace_id(csdev);
88 /* this shouldn't happen */
89 if (trace_id < 0)
90 return 0;
92 return !bus_for_each_dev(&coresight_bustype, NULL,
93 csdev, coresight_id_match);
96 static int coresight_find_link_inport(struct coresight_device *csdev,
97 struct coresight_device *parent)
99 int i;
100 struct coresight_connection *conn;
102 for (i = 0; i < parent->nr_outport; i++) {
103 conn = &parent->conns[i];
104 if (conn->child_dev == csdev)
105 return conn->child_port;
108 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
109 dev_name(&parent->dev), dev_name(&csdev->dev));
111 return -ENODEV;
114 static int coresight_find_link_outport(struct coresight_device *csdev,
115 struct coresight_device *child)
117 int i;
118 struct coresight_connection *conn;
120 for (i = 0; i < csdev->nr_outport; i++) {
121 conn = &csdev->conns[i];
122 if (conn->child_dev == child)
123 return conn->outport;
126 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
127 dev_name(&csdev->dev), dev_name(&child->dev));
129 return -ENODEV;
132 static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
134 int ret;
136 if (!csdev->enable) {
137 if (sink_ops(csdev)->enable) {
138 ret = sink_ops(csdev)->enable(csdev, mode);
139 if (ret)
140 return ret;
142 csdev->enable = true;
145 atomic_inc(csdev->refcnt);
147 return 0;
150 static void coresight_disable_sink(struct coresight_device *csdev)
152 if (atomic_dec_return(csdev->refcnt) == 0) {
153 if (sink_ops(csdev)->disable) {
154 sink_ops(csdev)->disable(csdev);
155 csdev->enable = false;
160 static int coresight_enable_link(struct coresight_device *csdev,
161 struct coresight_device *parent,
162 struct coresight_device *child)
164 int ret;
165 int link_subtype;
166 int refport, inport, outport;
168 if (!parent || !child)
169 return -EINVAL;
171 inport = coresight_find_link_inport(csdev, parent);
172 outport = coresight_find_link_outport(csdev, child);
173 link_subtype = csdev->subtype.link_subtype;
175 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
176 refport = inport;
177 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
178 refport = outport;
179 else
180 refport = 0;
182 if (refport < 0)
183 return refport;
185 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
186 if (link_ops(csdev)->enable) {
187 ret = link_ops(csdev)->enable(csdev, inport, outport);
188 if (ret)
189 return ret;
193 csdev->enable = true;
195 return 0;
198 static void coresight_disable_link(struct coresight_device *csdev,
199 struct coresight_device *parent,
200 struct coresight_device *child)
202 int i, nr_conns;
203 int link_subtype;
204 int refport, inport, outport;
206 if (!parent || !child)
207 return;
209 inport = coresight_find_link_inport(csdev, parent);
210 outport = coresight_find_link_outport(csdev, child);
211 link_subtype = csdev->subtype.link_subtype;
213 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
214 refport = inport;
215 nr_conns = csdev->nr_inport;
216 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
217 refport = outport;
218 nr_conns = csdev->nr_outport;
219 } else {
220 refport = 0;
221 nr_conns = 1;
224 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
225 if (link_ops(csdev)->disable)
226 link_ops(csdev)->disable(csdev, inport, outport);
229 for (i = 0; i < nr_conns; i++)
230 if (atomic_read(&csdev->refcnt[i]) != 0)
231 return;
233 csdev->enable = false;
236 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
238 int ret;
240 if (!coresight_source_is_unique(csdev)) {
241 dev_warn(&csdev->dev, "traceID %d not unique\n",
242 source_ops(csdev)->trace_id(csdev));
243 return -EINVAL;
246 if (!csdev->enable) {
247 if (source_ops(csdev)->enable) {
248 ret = source_ops(csdev)->enable(csdev, NULL, mode);
249 if (ret)
250 return ret;
252 csdev->enable = true;
255 atomic_inc(csdev->refcnt);
257 return 0;
261 * coresight_disable_source - Drop the reference count by 1 and disable
262 * the device if there are no users left.
264 * @csdev - The coresight device to disable
266 * Returns true if the device has been disabled.
268 static bool coresight_disable_source(struct coresight_device *csdev)
270 if (atomic_dec_return(csdev->refcnt) == 0) {
271 if (source_ops(csdev)->disable)
272 source_ops(csdev)->disable(csdev, NULL);
273 csdev->enable = false;
275 return !csdev->enable;
278 void coresight_disable_path(struct list_head *path)
280 u32 type;
281 struct coresight_node *nd;
282 struct coresight_device *csdev, *parent, *child;
284 list_for_each_entry(nd, path, link) {
285 csdev = nd->csdev;
286 type = csdev->type;
289 * ETF devices are tricky... They can be a link or a sink,
290 * depending on how they are configured. If an ETF has been
291 * "activated" it will be configured as a sink, otherwise
292 * go ahead with the link configuration.
294 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
295 type = (csdev == coresight_get_sink(path)) ?
296 CORESIGHT_DEV_TYPE_SINK :
297 CORESIGHT_DEV_TYPE_LINK;
299 switch (type) {
300 case CORESIGHT_DEV_TYPE_SINK:
301 coresight_disable_sink(csdev);
302 break;
303 case CORESIGHT_DEV_TYPE_SOURCE:
304 /* sources are disabled from either sysFS or Perf */
305 break;
306 case CORESIGHT_DEV_TYPE_LINK:
307 parent = list_prev_entry(nd, link)->csdev;
308 child = list_next_entry(nd, link)->csdev;
309 coresight_disable_link(csdev, parent, child);
310 break;
311 default:
312 break;
317 int coresight_enable_path(struct list_head *path, u32 mode)
320 int ret = 0;
321 u32 type;
322 struct coresight_node *nd;
323 struct coresight_device *csdev, *parent, *child;
325 list_for_each_entry_reverse(nd, path, link) {
326 csdev = nd->csdev;
327 type = csdev->type;
330 * ETF devices are tricky... They can be a link or a sink,
331 * depending on how they are configured. If an ETF has been
332 * "activated" it will be configured as a sink, otherwise
333 * go ahead with the link configuration.
335 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
336 type = (csdev == coresight_get_sink(path)) ?
337 CORESIGHT_DEV_TYPE_SINK :
338 CORESIGHT_DEV_TYPE_LINK;
340 switch (type) {
341 case CORESIGHT_DEV_TYPE_SINK:
342 ret = coresight_enable_sink(csdev, mode);
343 if (ret)
344 goto err;
345 break;
346 case CORESIGHT_DEV_TYPE_SOURCE:
347 /* sources are enabled from either sysFS or Perf */
348 break;
349 case CORESIGHT_DEV_TYPE_LINK:
350 parent = list_prev_entry(nd, link)->csdev;
351 child = list_next_entry(nd, link)->csdev;
352 ret = coresight_enable_link(csdev, parent, child);
353 if (ret)
354 goto err;
355 break;
356 default:
357 goto err;
361 out:
362 return ret;
363 err:
364 coresight_disable_path(path);
365 goto out;
368 struct coresight_device *coresight_get_sink(struct list_head *path)
370 struct coresight_device *csdev;
372 if (!path)
373 return NULL;
375 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
376 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
377 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
378 return NULL;
380 return csdev;
383 static int coresight_enabled_sink(struct device *dev, void *data)
385 bool *reset = data;
386 struct coresight_device *csdev = to_coresight_device(dev);
388 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
389 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
390 csdev->activated) {
392 * Now that we have a handle on the sink for this session,
393 * disable the sysFS "enable_sink" flag so that possible
394 * concurrent perf session that wish to use another sink don't
395 * trip on it. Doing so has no ramification for the current
396 * session.
398 if (*reset)
399 csdev->activated = false;
401 return 1;
404 return 0;
408 * coresight_get_enabled_sink - returns the first enabled sink found on the bus
409 * @deactivate: Whether the 'enable_sink' flag should be reset
411 * When operated from perf the deactivate parameter should be set to 'true'.
412 * That way the "enabled_sink" flag of the sink that was selected can be reset,
413 * allowing for other concurrent perf sessions to choose a different sink.
415 * When operated from sysFS users have full control and as such the deactivate
416 * parameter should be set to 'false', hence mandating users to explicitly
417 * clear the flag.
419 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
421 struct device *dev = NULL;
423 dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
424 coresight_enabled_sink);
426 return dev ? to_coresight_device(dev) : NULL;
430 * _coresight_build_path - recursively build a path from a @csdev to a sink.
431 * @csdev: The device to start from.
432 * @path: The list to add devices to.
434 * The tree of Coresight device is traversed until an activated sink is
435 * found. From there the sink is added to the list along with all the
436 * devices that led to that point - the end result is a list from source
437 * to sink. In that list the source is the first device and the sink the
438 * last one.
440 static int _coresight_build_path(struct coresight_device *csdev,
441 struct coresight_device *sink,
442 struct list_head *path)
444 int i;
445 bool found = false;
446 struct coresight_node *node;
448 /* An activated sink has been found. Enqueue the element */
449 if (csdev == sink)
450 goto out;
452 /* Not a sink - recursively explore each port found on this element */
453 for (i = 0; i < csdev->nr_outport; i++) {
454 struct coresight_device *child_dev = csdev->conns[i].child_dev;
456 if (child_dev &&
457 _coresight_build_path(child_dev, sink, path) == 0) {
458 found = true;
459 break;
463 if (!found)
464 return -ENODEV;
466 out:
468 * A path from this element to a sink has been found. The elements
469 * leading to the sink are already enqueued, all that is left to do
470 * is tell the PM runtime core we need this element and add a node
471 * for it.
473 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
474 if (!node)
475 return -ENOMEM;
477 node->csdev = csdev;
478 list_add(&node->link, path);
479 pm_runtime_get_sync(csdev->dev.parent);
481 return 0;
484 struct list_head *coresight_build_path(struct coresight_device *source,
485 struct coresight_device *sink)
487 struct list_head *path;
488 int rc;
490 if (!sink)
491 return ERR_PTR(-EINVAL);
493 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
494 if (!path)
495 return ERR_PTR(-ENOMEM);
497 INIT_LIST_HEAD(path);
499 rc = _coresight_build_path(source, sink, path);
500 if (rc) {
501 kfree(path);
502 return ERR_PTR(rc);
505 return path;
509 * coresight_release_path - release a previously built path.
510 * @path: the path to release.
512 * Go through all the elements of a path and 1) removed it from the list and
513 * 2) free the memory allocated for each node.
515 void coresight_release_path(struct list_head *path)
517 struct coresight_device *csdev;
518 struct coresight_node *nd, *next;
520 list_for_each_entry_safe(nd, next, path, link) {
521 csdev = nd->csdev;
523 pm_runtime_put_sync(csdev->dev.parent);
524 list_del(&nd->link);
525 kfree(nd);
528 kfree(path);
529 path = NULL;
532 /** coresight_validate_source - make sure a source has the right credentials
533 * @csdev: the device structure for a source.
534 * @function: the function this was called from.
536 * Assumes the coresight_mutex is held.
538 static int coresight_validate_source(struct coresight_device *csdev,
539 const char *function)
541 u32 type, subtype;
543 type = csdev->type;
544 subtype = csdev->subtype.source_subtype;
546 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
547 dev_err(&csdev->dev, "wrong device type in %s\n", function);
548 return -EINVAL;
551 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
552 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
553 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
554 return -EINVAL;
557 return 0;
560 int coresight_enable(struct coresight_device *csdev)
562 int cpu, ret = 0;
563 struct coresight_device *sink;
564 struct list_head *path;
565 enum coresight_dev_subtype_source subtype;
567 subtype = csdev->subtype.source_subtype;
569 mutex_lock(&coresight_mutex);
571 ret = coresight_validate_source(csdev, __func__);
572 if (ret)
573 goto out;
575 if (csdev->enable) {
577 * There could be multiple applications driving the software
578 * source. So keep the refcount for each such user when the
579 * source is already enabled.
581 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
582 atomic_inc(csdev->refcnt);
583 goto out;
587 * Search for a valid sink for this session but don't reset the
588 * "enable_sink" flag in sysFS. Users get to do that explicitly.
590 sink = coresight_get_enabled_sink(false);
591 if (!sink) {
592 ret = -EINVAL;
593 goto out;
596 path = coresight_build_path(csdev, sink);
597 if (IS_ERR(path)) {
598 pr_err("building path(s) failed\n");
599 ret = PTR_ERR(path);
600 goto out;
603 ret = coresight_enable_path(path, CS_MODE_SYSFS);
604 if (ret)
605 goto err_path;
607 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
608 if (ret)
609 goto err_source;
611 switch (subtype) {
612 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
614 * When working from sysFS it is important to keep track
615 * of the paths that were created so that they can be
616 * undone in 'coresight_disable()'. Since there can only
617 * be a single session per tracer (when working from sysFS)
618 * a per-cpu variable will do just fine.
620 cpu = source_ops(csdev)->cpu_id(csdev);
621 per_cpu(tracer_path, cpu) = path;
622 break;
623 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
624 stm_path = path;
625 break;
626 default:
627 /* We can't be here */
628 break;
631 out:
632 mutex_unlock(&coresight_mutex);
633 return ret;
635 err_source:
636 coresight_disable_path(path);
638 err_path:
639 coresight_release_path(path);
640 goto out;
642 EXPORT_SYMBOL_GPL(coresight_enable);
644 void coresight_disable(struct coresight_device *csdev)
646 int cpu, ret;
647 struct list_head *path = NULL;
649 mutex_lock(&coresight_mutex);
651 ret = coresight_validate_source(csdev, __func__);
652 if (ret)
653 goto out;
655 if (!csdev->enable || !coresight_disable_source(csdev))
656 goto out;
658 switch (csdev->subtype.source_subtype) {
659 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
660 cpu = source_ops(csdev)->cpu_id(csdev);
661 path = per_cpu(tracer_path, cpu);
662 per_cpu(tracer_path, cpu) = NULL;
663 break;
664 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
665 path = stm_path;
666 stm_path = NULL;
667 break;
668 default:
669 /* We can't be here */
670 break;
673 coresight_disable_path(path);
674 coresight_release_path(path);
676 out:
677 mutex_unlock(&coresight_mutex);
679 EXPORT_SYMBOL_GPL(coresight_disable);
681 static ssize_t enable_sink_show(struct device *dev,
682 struct device_attribute *attr, char *buf)
684 struct coresight_device *csdev = to_coresight_device(dev);
686 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
689 static ssize_t enable_sink_store(struct device *dev,
690 struct device_attribute *attr,
691 const char *buf, size_t size)
693 int ret;
694 unsigned long val;
695 struct coresight_device *csdev = to_coresight_device(dev);
697 ret = kstrtoul(buf, 10, &val);
698 if (ret)
699 return ret;
701 if (val)
702 csdev->activated = true;
703 else
704 csdev->activated = false;
706 return size;
709 static DEVICE_ATTR_RW(enable_sink);
711 static ssize_t enable_source_show(struct device *dev,
712 struct device_attribute *attr, char *buf)
714 struct coresight_device *csdev = to_coresight_device(dev);
716 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
719 static ssize_t enable_source_store(struct device *dev,
720 struct device_attribute *attr,
721 const char *buf, size_t size)
723 int ret = 0;
724 unsigned long val;
725 struct coresight_device *csdev = to_coresight_device(dev);
727 ret = kstrtoul(buf, 10, &val);
728 if (ret)
729 return ret;
731 if (val) {
732 ret = coresight_enable(csdev);
733 if (ret)
734 return ret;
735 } else {
736 coresight_disable(csdev);
739 return size;
741 static DEVICE_ATTR_RW(enable_source);
743 static struct attribute *coresight_sink_attrs[] = {
744 &dev_attr_enable_sink.attr,
745 NULL,
747 ATTRIBUTE_GROUPS(coresight_sink);
749 static struct attribute *coresight_source_attrs[] = {
750 &dev_attr_enable_source.attr,
751 NULL,
753 ATTRIBUTE_GROUPS(coresight_source);
755 static struct device_type coresight_dev_type[] = {
757 .name = "none",
760 .name = "sink",
761 .groups = coresight_sink_groups,
764 .name = "link",
767 .name = "linksink",
768 .groups = coresight_sink_groups,
771 .name = "source",
772 .groups = coresight_source_groups,
776 static void coresight_device_release(struct device *dev)
778 struct coresight_device *csdev = to_coresight_device(dev);
780 kfree(csdev->conns);
781 kfree(csdev->refcnt);
782 kfree(csdev);
785 static int coresight_orphan_match(struct device *dev, void *data)
787 int i;
788 bool still_orphan = false;
789 struct coresight_device *csdev, *i_csdev;
790 struct coresight_connection *conn;
792 csdev = data;
793 i_csdev = to_coresight_device(dev);
795 /* No need to check oneself */
796 if (csdev == i_csdev)
797 return 0;
799 /* Move on to another component if no connection is orphan */
800 if (!i_csdev->orphan)
801 return 0;
803 * Circle throuch all the connection of that component. If we find
804 * an orphan connection whose name matches @csdev, link it.
806 for (i = 0; i < i_csdev->nr_outport; i++) {
807 conn = &i_csdev->conns[i];
809 /* We have found at least one orphan connection */
810 if (conn->child_dev == NULL) {
811 /* Does it match this newly added device? */
812 if (conn->child_name &&
813 !strcmp(dev_name(&csdev->dev), conn->child_name)) {
814 conn->child_dev = csdev;
815 } else {
816 /* This component still has an orphan */
817 still_orphan = true;
822 i_csdev->orphan = still_orphan;
825 * Returning '0' ensures that all known component on the
826 * bus will be checked.
828 return 0;
831 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
834 * No need to check for a return value as orphan connection(s)
835 * are hooked-up with each newly added component.
837 bus_for_each_dev(&coresight_bustype, NULL,
838 csdev, coresight_orphan_match);
842 static void coresight_fixup_device_conns(struct coresight_device *csdev)
844 int i;
846 for (i = 0; i < csdev->nr_outport; i++) {
847 struct coresight_connection *conn = &csdev->conns[i];
848 struct device *dev = NULL;
850 if (conn->child_name)
851 dev = bus_find_device_by_name(&coresight_bustype, NULL,
852 conn->child_name);
853 if (dev) {
854 conn->child_dev = to_coresight_device(dev);
855 /* and put reference from 'bus_find_device()' */
856 put_device(dev);
857 } else {
858 csdev->orphan = true;
859 conn->child_dev = NULL;
864 static int coresight_remove_match(struct device *dev, void *data)
866 int i;
867 struct coresight_device *csdev, *iterator;
868 struct coresight_connection *conn;
870 csdev = data;
871 iterator = to_coresight_device(dev);
873 /* No need to check oneself */
874 if (csdev == iterator)
875 return 0;
878 * Circle throuch all the connection of that component. If we find
879 * a connection whose name matches @csdev, remove it.
881 for (i = 0; i < iterator->nr_outport; i++) {
882 conn = &iterator->conns[i];
884 if (conn->child_dev == NULL)
885 continue;
887 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
888 iterator->orphan = true;
889 conn->child_dev = NULL;
890 /* No need to continue */
891 break;
896 * Returning '0' ensures that all known component on the
897 * bus will be checked.
899 return 0;
902 static void coresight_remove_conns(struct coresight_device *csdev)
904 bus_for_each_dev(&coresight_bustype, NULL,
905 csdev, coresight_remove_match);
909 * coresight_timeout - loop until a bit has changed to a specific state.
910 * @addr: base address of the area of interest.
911 * @offset: address of a register, starting from @addr.
912 * @position: the position of the bit of interest.
913 * @value: the value the bit should have.
915 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
916 * TIMEOUT_US has elapsed, which ever happens first.
919 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
921 int i;
922 u32 val;
924 for (i = TIMEOUT_US; i > 0; i--) {
925 val = __raw_readl(addr + offset);
926 /* waiting on the bit to go from 0 to 1 */
927 if (value) {
928 if (val & BIT(position))
929 return 0;
930 /* waiting on the bit to go from 1 to 0 */
931 } else {
932 if (!(val & BIT(position)))
933 return 0;
937 * Delay is arbitrary - the specification doesn't say how long
938 * we are expected to wait. Extra check required to make sure
939 * we don't wait needlessly on the last iteration.
941 if (i - 1)
942 udelay(1);
945 return -EAGAIN;
948 struct bus_type coresight_bustype = {
949 .name = "coresight",
952 static int __init coresight_init(void)
954 return bus_register(&coresight_bustype);
956 postcore_initcall(coresight_init);
958 struct coresight_device *coresight_register(struct coresight_desc *desc)
960 int i;
961 int ret;
962 int link_subtype;
963 int nr_refcnts = 1;
964 atomic_t *refcnts = NULL;
965 struct coresight_device *csdev;
966 struct coresight_connection *conns = NULL;
968 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
969 if (!csdev) {
970 ret = -ENOMEM;
971 goto err_kzalloc_csdev;
974 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
975 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
976 link_subtype = desc->subtype.link_subtype;
978 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
979 nr_refcnts = desc->pdata->nr_inport;
980 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
981 nr_refcnts = desc->pdata->nr_outport;
984 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
985 if (!refcnts) {
986 ret = -ENOMEM;
987 goto err_kzalloc_refcnts;
990 csdev->refcnt = refcnts;
992 csdev->nr_inport = desc->pdata->nr_inport;
993 csdev->nr_outport = desc->pdata->nr_outport;
995 /* Initialise connections if there is at least one outport */
996 if (csdev->nr_outport) {
997 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
998 if (!conns) {
999 ret = -ENOMEM;
1000 goto err_kzalloc_conns;
1003 for (i = 0; i < csdev->nr_outport; i++) {
1004 conns[i].outport = desc->pdata->outports[i];
1005 conns[i].child_name = desc->pdata->child_names[i];
1006 conns[i].child_port = desc->pdata->child_ports[i];
1010 csdev->conns = conns;
1012 csdev->type = desc->type;
1013 csdev->subtype = desc->subtype;
1014 csdev->ops = desc->ops;
1015 csdev->orphan = false;
1017 csdev->dev.type = &coresight_dev_type[desc->type];
1018 csdev->dev.groups = desc->groups;
1019 csdev->dev.parent = desc->dev;
1020 csdev->dev.release = coresight_device_release;
1021 csdev->dev.bus = &coresight_bustype;
1022 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
1024 ret = device_register(&csdev->dev);
1025 if (ret) {
1026 put_device(&csdev->dev);
1027 goto err_kzalloc_csdev;
1030 mutex_lock(&coresight_mutex);
1032 coresight_fixup_device_conns(csdev);
1033 coresight_fixup_orphan_conns(csdev);
1035 mutex_unlock(&coresight_mutex);
1037 return csdev;
1039 err_kzalloc_conns:
1040 kfree(refcnts);
1041 err_kzalloc_refcnts:
1042 kfree(csdev);
1043 err_kzalloc_csdev:
1044 return ERR_PTR(ret);
1046 EXPORT_SYMBOL_GPL(coresight_register);
1048 void coresight_unregister(struct coresight_device *csdev)
1050 /* Remove references of that device in the topology */
1051 coresight_remove_conns(csdev);
1052 device_unregister(&csdev->dev);
1054 EXPORT_SYMBOL_GPL(coresight_unregister);