x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / drivers / hwtracing / coresight / coresight.c
blobe571e4010dff0c214c4aca8f8463a59116e4ca2a
1 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/device.h>
17 #include <linux/io.h>
18 #include <linux/err.h>
19 #include <linux/export.h>
20 #include <linux/slab.h>
21 #include <linux/mutex.h>
22 #include <linux/clk.h>
23 #include <linux/coresight.h>
24 #include <linux/of_platform.h>
25 #include <linux/delay.h>
26 #include <linux/pm_runtime.h>
28 #include "coresight-priv.h"
30 static DEFINE_MUTEX(coresight_mutex);
32 /**
33 * struct coresight_node - elements of a path, from source to sink
34 * @csdev: Address of an element.
35 * @link: hook to the list.
37 struct coresight_node {
38 struct coresight_device *csdev;
39 struct list_head link;
43 * When operating Coresight drivers from the sysFS interface, only a single
44 * path can exist from a tracer (associated to a CPU) to a sink.
46 static DEFINE_PER_CPU(struct list_head *, tracer_path);
49 * As of this writing only a single STM can be found in CS topologies. Since
50 * there is no way to know if we'll ever see more and what kind of
51 * configuration they will enact, for the time being only define a single path
52 * for STM.
54 static struct list_head *stm_path;
57 * When losing synchronisation a new barrier packet needs to be inserted at the
58 * beginning of the data collected in a buffer. That way the decoder knows that
59 * it needs to look for another sync sequence.
61 const u32 barrier_pkt[5] = {0x7fffffff, 0x7fffffff,
62 0x7fffffff, 0x7fffffff, 0x0};
64 static int coresight_id_match(struct device *dev, void *data)
66 int trace_id, i_trace_id;
67 struct coresight_device *csdev, *i_csdev;
69 csdev = data;
70 i_csdev = to_coresight_device(dev);
73 * No need to care about oneself and components that are not
74 * sources or not enabled
76 if (i_csdev == csdev || !i_csdev->enable ||
77 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
78 return 0;
80 /* Get the source ID for both compoment */
81 trace_id = source_ops(csdev)->trace_id(csdev);
82 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
84 /* All you need is one */
85 if (trace_id == i_trace_id)
86 return 1;
88 return 0;
91 static int coresight_source_is_unique(struct coresight_device *csdev)
93 int trace_id = source_ops(csdev)->trace_id(csdev);
95 /* this shouldn't happen */
96 if (trace_id < 0)
97 return 0;
99 return !bus_for_each_dev(&coresight_bustype, NULL,
100 csdev, coresight_id_match);
103 static int coresight_find_link_inport(struct coresight_device *csdev,
104 struct coresight_device *parent)
106 int i;
107 struct coresight_connection *conn;
109 for (i = 0; i < parent->nr_outport; i++) {
110 conn = &parent->conns[i];
111 if (conn->child_dev == csdev)
112 return conn->child_port;
115 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
116 dev_name(&parent->dev), dev_name(&csdev->dev));
118 return -ENODEV;
121 static int coresight_find_link_outport(struct coresight_device *csdev,
122 struct coresight_device *child)
124 int i;
125 struct coresight_connection *conn;
127 for (i = 0; i < csdev->nr_outport; i++) {
128 conn = &csdev->conns[i];
129 if (conn->child_dev == child)
130 return conn->outport;
133 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
134 dev_name(&csdev->dev), dev_name(&child->dev));
136 return -ENODEV;
139 static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
141 int ret;
143 if (!csdev->enable) {
144 if (sink_ops(csdev)->enable) {
145 ret = sink_ops(csdev)->enable(csdev, mode);
146 if (ret)
147 return ret;
149 csdev->enable = true;
152 atomic_inc(csdev->refcnt);
154 return 0;
157 static void coresight_disable_sink(struct coresight_device *csdev)
159 if (atomic_dec_return(csdev->refcnt) == 0) {
160 if (sink_ops(csdev)->disable) {
161 sink_ops(csdev)->disable(csdev);
162 csdev->enable = false;
167 static int coresight_enable_link(struct coresight_device *csdev,
168 struct coresight_device *parent,
169 struct coresight_device *child)
171 int ret;
172 int link_subtype;
173 int refport, inport, outport;
175 if (!parent || !child)
176 return -EINVAL;
178 inport = coresight_find_link_inport(csdev, parent);
179 outport = coresight_find_link_outport(csdev, child);
180 link_subtype = csdev->subtype.link_subtype;
182 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
183 refport = inport;
184 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
185 refport = outport;
186 else
187 refport = 0;
189 if (refport < 0)
190 return refport;
192 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
193 if (link_ops(csdev)->enable) {
194 ret = link_ops(csdev)->enable(csdev, inport, outport);
195 if (ret)
196 return ret;
200 csdev->enable = true;
202 return 0;
205 static void coresight_disable_link(struct coresight_device *csdev,
206 struct coresight_device *parent,
207 struct coresight_device *child)
209 int i, nr_conns;
210 int link_subtype;
211 int refport, inport, outport;
213 if (!parent || !child)
214 return;
216 inport = coresight_find_link_inport(csdev, parent);
217 outport = coresight_find_link_outport(csdev, child);
218 link_subtype = csdev->subtype.link_subtype;
220 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
221 refport = inport;
222 nr_conns = csdev->nr_inport;
223 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
224 refport = outport;
225 nr_conns = csdev->nr_outport;
226 } else {
227 refport = 0;
228 nr_conns = 1;
231 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
232 if (link_ops(csdev)->disable)
233 link_ops(csdev)->disable(csdev, inport, outport);
236 for (i = 0; i < nr_conns; i++)
237 if (atomic_read(&csdev->refcnt[i]) != 0)
238 return;
240 csdev->enable = false;
243 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
245 int ret;
247 if (!coresight_source_is_unique(csdev)) {
248 dev_warn(&csdev->dev, "traceID %d not unique\n",
249 source_ops(csdev)->trace_id(csdev));
250 return -EINVAL;
253 if (!csdev->enable) {
254 if (source_ops(csdev)->enable) {
255 ret = source_ops(csdev)->enable(csdev, NULL, mode);
256 if (ret)
257 return ret;
259 csdev->enable = true;
262 atomic_inc(csdev->refcnt);
264 return 0;
268 * coresight_disable_source - Drop the reference count by 1 and disable
269 * the device if there are no users left.
271 * @csdev - The coresight device to disable
273 * Returns true if the device has been disabled.
275 static bool coresight_disable_source(struct coresight_device *csdev)
277 if (atomic_dec_return(csdev->refcnt) == 0) {
278 if (source_ops(csdev)->disable)
279 source_ops(csdev)->disable(csdev, NULL);
280 csdev->enable = false;
282 return !csdev->enable;
285 void coresight_disable_path(struct list_head *path)
287 u32 type;
288 struct coresight_node *nd;
289 struct coresight_device *csdev, *parent, *child;
291 list_for_each_entry(nd, path, link) {
292 csdev = nd->csdev;
293 type = csdev->type;
296 * ETF devices are tricky... They can be a link or a sink,
297 * depending on how they are configured. If an ETF has been
298 * "activated" it will be configured as a sink, otherwise
299 * go ahead with the link configuration.
301 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
302 type = (csdev == coresight_get_sink(path)) ?
303 CORESIGHT_DEV_TYPE_SINK :
304 CORESIGHT_DEV_TYPE_LINK;
306 switch (type) {
307 case CORESIGHT_DEV_TYPE_SINK:
308 coresight_disable_sink(csdev);
309 break;
310 case CORESIGHT_DEV_TYPE_SOURCE:
311 /* sources are disabled from either sysFS or Perf */
312 break;
313 case CORESIGHT_DEV_TYPE_LINK:
314 parent = list_prev_entry(nd, link)->csdev;
315 child = list_next_entry(nd, link)->csdev;
316 coresight_disable_link(csdev, parent, child);
317 break;
318 default:
319 break;
324 int coresight_enable_path(struct list_head *path, u32 mode)
327 int ret = 0;
328 u32 type;
329 struct coresight_node *nd;
330 struct coresight_device *csdev, *parent, *child;
332 list_for_each_entry_reverse(nd, path, link) {
333 csdev = nd->csdev;
334 type = csdev->type;
337 * ETF devices are tricky... They can be a link or a sink,
338 * depending on how they are configured. If an ETF has been
339 * "activated" it will be configured as a sink, otherwise
340 * go ahead with the link configuration.
342 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
343 type = (csdev == coresight_get_sink(path)) ?
344 CORESIGHT_DEV_TYPE_SINK :
345 CORESIGHT_DEV_TYPE_LINK;
347 switch (type) {
348 case CORESIGHT_DEV_TYPE_SINK:
349 ret = coresight_enable_sink(csdev, mode);
350 if (ret)
351 goto err;
352 break;
353 case CORESIGHT_DEV_TYPE_SOURCE:
354 /* sources are enabled from either sysFS or Perf */
355 break;
356 case CORESIGHT_DEV_TYPE_LINK:
357 parent = list_prev_entry(nd, link)->csdev;
358 child = list_next_entry(nd, link)->csdev;
359 ret = coresight_enable_link(csdev, parent, child);
360 if (ret)
361 goto err;
362 break;
363 default:
364 goto err;
368 out:
369 return ret;
370 err:
371 coresight_disable_path(path);
372 goto out;
375 struct coresight_device *coresight_get_sink(struct list_head *path)
377 struct coresight_device *csdev;
379 if (!path)
380 return NULL;
382 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
383 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
384 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
385 return NULL;
387 return csdev;
390 static int coresight_enabled_sink(struct device *dev, void *data)
392 bool *reset = data;
393 struct coresight_device *csdev = to_coresight_device(dev);
395 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
396 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
397 csdev->activated) {
399 * Now that we have a handle on the sink for this session,
400 * disable the sysFS "enable_sink" flag so that possible
401 * concurrent perf session that wish to use another sink don't
402 * trip on it. Doing so has no ramification for the current
403 * session.
405 if (*reset)
406 csdev->activated = false;
408 return 1;
411 return 0;
415 * coresight_get_enabled_sink - returns the first enabled sink found on the bus
416 * @deactivate: Whether the 'enable_sink' flag should be reset
418 * When operated from perf the deactivate parameter should be set to 'true'.
419 * That way the "enabled_sink" flag of the sink that was selected can be reset,
420 * allowing for other concurrent perf sessions to choose a different sink.
422 * When operated from sysFS users have full control and as such the deactivate
423 * parameter should be set to 'false', hence mandating users to explicitly
424 * clear the flag.
426 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
428 struct device *dev = NULL;
430 dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
431 coresight_enabled_sink);
433 return dev ? to_coresight_device(dev) : NULL;
437 * _coresight_build_path - recursively build a path from a @csdev to a sink.
438 * @csdev: The device to start from.
439 * @path: The list to add devices to.
441 * The tree of Coresight device is traversed until an activated sink is
442 * found. From there the sink is added to the list along with all the
443 * devices that led to that point - the end result is a list from source
444 * to sink. In that list the source is the first device and the sink the
445 * last one.
447 static int _coresight_build_path(struct coresight_device *csdev,
448 struct coresight_device *sink,
449 struct list_head *path)
451 int i;
452 bool found = false;
453 struct coresight_node *node;
455 /* An activated sink has been found. Enqueue the element */
456 if (csdev == sink)
457 goto out;
459 /* Not a sink - recursively explore each port found on this element */
460 for (i = 0; i < csdev->nr_outport; i++) {
461 struct coresight_device *child_dev = csdev->conns[i].child_dev;
463 if (child_dev &&
464 _coresight_build_path(child_dev, sink, path) == 0) {
465 found = true;
466 break;
470 if (!found)
471 return -ENODEV;
473 out:
475 * A path from this element to a sink has been found. The elements
476 * leading to the sink are already enqueued, all that is left to do
477 * is tell the PM runtime core we need this element and add a node
478 * for it.
480 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
481 if (!node)
482 return -ENOMEM;
484 node->csdev = csdev;
485 list_add(&node->link, path);
486 pm_runtime_get_sync(csdev->dev.parent);
488 return 0;
491 struct list_head *coresight_build_path(struct coresight_device *source,
492 struct coresight_device *sink)
494 struct list_head *path;
495 int rc;
497 if (!sink)
498 return ERR_PTR(-EINVAL);
500 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
501 if (!path)
502 return ERR_PTR(-ENOMEM);
504 INIT_LIST_HEAD(path);
506 rc = _coresight_build_path(source, sink, path);
507 if (rc) {
508 kfree(path);
509 return ERR_PTR(rc);
512 return path;
516 * coresight_release_path - release a previously built path.
517 * @path: the path to release.
519 * Go through all the elements of a path and 1) removed it from the list and
520 * 2) free the memory allocated for each node.
522 void coresight_release_path(struct list_head *path)
524 struct coresight_device *csdev;
525 struct coresight_node *nd, *next;
527 list_for_each_entry_safe(nd, next, path, link) {
528 csdev = nd->csdev;
530 pm_runtime_put_sync(csdev->dev.parent);
531 list_del(&nd->link);
532 kfree(nd);
535 kfree(path);
536 path = NULL;
539 /** coresight_validate_source - make sure a source has the right credentials
540 * @csdev: the device structure for a source.
541 * @function: the function this was called from.
543 * Assumes the coresight_mutex is held.
545 static int coresight_validate_source(struct coresight_device *csdev,
546 const char *function)
548 u32 type, subtype;
550 type = csdev->type;
551 subtype = csdev->subtype.source_subtype;
553 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
554 dev_err(&csdev->dev, "wrong device type in %s\n", function);
555 return -EINVAL;
558 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
559 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
560 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
561 return -EINVAL;
564 return 0;
567 int coresight_enable(struct coresight_device *csdev)
569 int cpu, ret = 0;
570 struct coresight_device *sink;
571 struct list_head *path;
572 enum coresight_dev_subtype_source subtype;
574 subtype = csdev->subtype.source_subtype;
576 mutex_lock(&coresight_mutex);
578 ret = coresight_validate_source(csdev, __func__);
579 if (ret)
580 goto out;
582 if (csdev->enable) {
584 * There could be multiple applications driving the software
585 * source. So keep the refcount for each such user when the
586 * source is already enabled.
588 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
589 atomic_inc(csdev->refcnt);
590 goto out;
594 * Search for a valid sink for this session but don't reset the
595 * "enable_sink" flag in sysFS. Users get to do that explicitly.
597 sink = coresight_get_enabled_sink(false);
598 if (!sink) {
599 ret = -EINVAL;
600 goto out;
603 path = coresight_build_path(csdev, sink);
604 if (IS_ERR(path)) {
605 pr_err("building path(s) failed\n");
606 ret = PTR_ERR(path);
607 goto out;
610 ret = coresight_enable_path(path, CS_MODE_SYSFS);
611 if (ret)
612 goto err_path;
614 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
615 if (ret)
616 goto err_source;
618 switch (subtype) {
619 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
621 * When working from sysFS it is important to keep track
622 * of the paths that were created so that they can be
623 * undone in 'coresight_disable()'. Since there can only
624 * be a single session per tracer (when working from sysFS)
625 * a per-cpu variable will do just fine.
627 cpu = source_ops(csdev)->cpu_id(csdev);
628 per_cpu(tracer_path, cpu) = path;
629 break;
630 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
631 stm_path = path;
632 break;
633 default:
634 /* We can't be here */
635 break;
638 out:
639 mutex_unlock(&coresight_mutex);
640 return ret;
642 err_source:
643 coresight_disable_path(path);
645 err_path:
646 coresight_release_path(path);
647 goto out;
649 EXPORT_SYMBOL_GPL(coresight_enable);
651 void coresight_disable(struct coresight_device *csdev)
653 int cpu, ret;
654 struct list_head *path = NULL;
656 mutex_lock(&coresight_mutex);
658 ret = coresight_validate_source(csdev, __func__);
659 if (ret)
660 goto out;
662 if (!csdev->enable || !coresight_disable_source(csdev))
663 goto out;
665 switch (csdev->subtype.source_subtype) {
666 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
667 cpu = source_ops(csdev)->cpu_id(csdev);
668 path = per_cpu(tracer_path, cpu);
669 per_cpu(tracer_path, cpu) = NULL;
670 break;
671 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
672 path = stm_path;
673 stm_path = NULL;
674 break;
675 default:
676 /* We can't be here */
677 break;
680 coresight_disable_path(path);
681 coresight_release_path(path);
683 out:
684 mutex_unlock(&coresight_mutex);
686 EXPORT_SYMBOL_GPL(coresight_disable);
688 static ssize_t enable_sink_show(struct device *dev,
689 struct device_attribute *attr, char *buf)
691 struct coresight_device *csdev = to_coresight_device(dev);
693 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
696 static ssize_t enable_sink_store(struct device *dev,
697 struct device_attribute *attr,
698 const char *buf, size_t size)
700 int ret;
701 unsigned long val;
702 struct coresight_device *csdev = to_coresight_device(dev);
704 ret = kstrtoul(buf, 10, &val);
705 if (ret)
706 return ret;
708 if (val)
709 csdev->activated = true;
710 else
711 csdev->activated = false;
713 return size;
716 static DEVICE_ATTR_RW(enable_sink);
718 static ssize_t enable_source_show(struct device *dev,
719 struct device_attribute *attr, char *buf)
721 struct coresight_device *csdev = to_coresight_device(dev);
723 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
726 static ssize_t enable_source_store(struct device *dev,
727 struct device_attribute *attr,
728 const char *buf, size_t size)
730 int ret = 0;
731 unsigned long val;
732 struct coresight_device *csdev = to_coresight_device(dev);
734 ret = kstrtoul(buf, 10, &val);
735 if (ret)
736 return ret;
738 if (val) {
739 ret = coresight_enable(csdev);
740 if (ret)
741 return ret;
742 } else {
743 coresight_disable(csdev);
746 return size;
748 static DEVICE_ATTR_RW(enable_source);
750 static struct attribute *coresight_sink_attrs[] = {
751 &dev_attr_enable_sink.attr,
752 NULL,
754 ATTRIBUTE_GROUPS(coresight_sink);
756 static struct attribute *coresight_source_attrs[] = {
757 &dev_attr_enable_source.attr,
758 NULL,
760 ATTRIBUTE_GROUPS(coresight_source);
762 static struct device_type coresight_dev_type[] = {
764 .name = "none",
767 .name = "sink",
768 .groups = coresight_sink_groups,
771 .name = "link",
774 .name = "linksink",
775 .groups = coresight_sink_groups,
778 .name = "source",
779 .groups = coresight_source_groups,
783 static void coresight_device_release(struct device *dev)
785 struct coresight_device *csdev = to_coresight_device(dev);
787 kfree(csdev->conns);
788 kfree(csdev->refcnt);
789 kfree(csdev);
792 static int coresight_orphan_match(struct device *dev, void *data)
794 int i;
795 bool still_orphan = false;
796 struct coresight_device *csdev, *i_csdev;
797 struct coresight_connection *conn;
799 csdev = data;
800 i_csdev = to_coresight_device(dev);
802 /* No need to check oneself */
803 if (csdev == i_csdev)
804 return 0;
806 /* Move on to another component if no connection is orphan */
807 if (!i_csdev->orphan)
808 return 0;
810 * Circle throuch all the connection of that component. If we find
811 * an orphan connection whose name matches @csdev, link it.
813 for (i = 0; i < i_csdev->nr_outport; i++) {
814 conn = &i_csdev->conns[i];
816 /* We have found at least one orphan connection */
817 if (conn->child_dev == NULL) {
818 /* Does it match this newly added device? */
819 if (conn->child_name &&
820 !strcmp(dev_name(&csdev->dev), conn->child_name)) {
821 conn->child_dev = csdev;
822 } else {
823 /* This component still has an orphan */
824 still_orphan = true;
829 i_csdev->orphan = still_orphan;
832 * Returning '0' ensures that all known component on the
833 * bus will be checked.
835 return 0;
838 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
841 * No need to check for a return value as orphan connection(s)
842 * are hooked-up with each newly added component.
844 bus_for_each_dev(&coresight_bustype, NULL,
845 csdev, coresight_orphan_match);
849 static int coresight_name_match(struct device *dev, void *data)
851 char *to_match;
852 struct coresight_device *i_csdev;
854 to_match = data;
855 i_csdev = to_coresight_device(dev);
857 if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
858 return 1;
860 return 0;
863 static void coresight_fixup_device_conns(struct coresight_device *csdev)
865 int i;
866 struct device *dev = NULL;
867 struct coresight_connection *conn;
869 for (i = 0; i < csdev->nr_outport; i++) {
870 conn = &csdev->conns[i];
871 dev = bus_find_device(&coresight_bustype, NULL,
872 (void *)conn->child_name,
873 coresight_name_match);
875 if (dev) {
876 conn->child_dev = to_coresight_device(dev);
877 /* and put reference from 'bus_find_device()' */
878 put_device(dev);
879 } else {
880 csdev->orphan = true;
881 conn->child_dev = NULL;
886 static int coresight_remove_match(struct device *dev, void *data)
888 int i;
889 struct coresight_device *csdev, *iterator;
890 struct coresight_connection *conn;
892 csdev = data;
893 iterator = to_coresight_device(dev);
895 /* No need to check oneself */
896 if (csdev == iterator)
897 return 0;
900 * Circle throuch all the connection of that component. If we find
901 * a connection whose name matches @csdev, remove it.
903 for (i = 0; i < iterator->nr_outport; i++) {
904 conn = &iterator->conns[i];
906 if (conn->child_dev == NULL)
907 continue;
909 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
910 iterator->orphan = true;
911 conn->child_dev = NULL;
912 /* No need to continue */
913 break;
918 * Returning '0' ensures that all known component on the
919 * bus will be checked.
921 return 0;
924 static void coresight_remove_conns(struct coresight_device *csdev)
926 bus_for_each_dev(&coresight_bustype, NULL,
927 csdev, coresight_remove_match);
931 * coresight_timeout - loop until a bit has changed to a specific state.
932 * @addr: base address of the area of interest.
933 * @offset: address of a register, starting from @addr.
934 * @position: the position of the bit of interest.
935 * @value: the value the bit should have.
937 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
938 * TIMEOUT_US has elapsed, which ever happens first.
941 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
943 int i;
944 u32 val;
946 for (i = TIMEOUT_US; i > 0; i--) {
947 val = __raw_readl(addr + offset);
948 /* waiting on the bit to go from 0 to 1 */
949 if (value) {
950 if (val & BIT(position))
951 return 0;
952 /* waiting on the bit to go from 1 to 0 */
953 } else {
954 if (!(val & BIT(position)))
955 return 0;
959 * Delay is arbitrary - the specification doesn't say how long
960 * we are expected to wait. Extra check required to make sure
961 * we don't wait needlessly on the last iteration.
963 if (i - 1)
964 udelay(1);
967 return -EAGAIN;
970 struct bus_type coresight_bustype = {
971 .name = "coresight",
974 static int __init coresight_init(void)
976 return bus_register(&coresight_bustype);
978 postcore_initcall(coresight_init);
980 struct coresight_device *coresight_register(struct coresight_desc *desc)
982 int i;
983 int ret;
984 int link_subtype;
985 int nr_refcnts = 1;
986 atomic_t *refcnts = NULL;
987 struct coresight_device *csdev;
988 struct coresight_connection *conns = NULL;
990 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
991 if (!csdev) {
992 ret = -ENOMEM;
993 goto err_kzalloc_csdev;
996 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
997 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
998 link_subtype = desc->subtype.link_subtype;
1000 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1001 nr_refcnts = desc->pdata->nr_inport;
1002 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1003 nr_refcnts = desc->pdata->nr_outport;
1006 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1007 if (!refcnts) {
1008 ret = -ENOMEM;
1009 goto err_kzalloc_refcnts;
1012 csdev->refcnt = refcnts;
1014 csdev->nr_inport = desc->pdata->nr_inport;
1015 csdev->nr_outport = desc->pdata->nr_outport;
1017 /* Initialise connections if there is at least one outport */
1018 if (csdev->nr_outport) {
1019 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
1020 if (!conns) {
1021 ret = -ENOMEM;
1022 goto err_kzalloc_conns;
1025 for (i = 0; i < csdev->nr_outport; i++) {
1026 conns[i].outport = desc->pdata->outports[i];
1027 conns[i].child_name = desc->pdata->child_names[i];
1028 conns[i].child_port = desc->pdata->child_ports[i];
1032 csdev->conns = conns;
1034 csdev->type = desc->type;
1035 csdev->subtype = desc->subtype;
1036 csdev->ops = desc->ops;
1037 csdev->orphan = false;
1039 csdev->dev.type = &coresight_dev_type[desc->type];
1040 csdev->dev.groups = desc->groups;
1041 csdev->dev.parent = desc->dev;
1042 csdev->dev.release = coresight_device_release;
1043 csdev->dev.bus = &coresight_bustype;
1044 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
1046 ret = device_register(&csdev->dev);
1047 if (ret)
1048 goto err_device_register;
1050 mutex_lock(&coresight_mutex);
1052 coresight_fixup_device_conns(csdev);
1053 coresight_fixup_orphan_conns(csdev);
1055 mutex_unlock(&coresight_mutex);
1057 return csdev;
1059 err_device_register:
1060 kfree(conns);
1061 err_kzalloc_conns:
1062 kfree(refcnts);
1063 err_kzalloc_refcnts:
1064 kfree(csdev);
1065 err_kzalloc_csdev:
1066 return ERR_PTR(ret);
1068 EXPORT_SYMBOL_GPL(coresight_register);
1070 void coresight_unregister(struct coresight_device *csdev)
1072 /* Remove references of that device in the topology */
1073 coresight_remove_conns(csdev);
1074 device_unregister(&csdev->dev);
1076 EXPORT_SYMBOL_GPL(coresight_unregister);