sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / hwtracing / coresight / coresight.c
blob0c37356e417ca66131717e83f359357f092df6d3
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;
56 static int coresight_id_match(struct device *dev, void *data)
58 int trace_id, i_trace_id;
59 struct coresight_device *csdev, *i_csdev;
61 csdev = data;
62 i_csdev = to_coresight_device(dev);
65 * No need to care about oneself and components that are not
66 * sources or not enabled
68 if (i_csdev == csdev || !i_csdev->enable ||
69 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
70 return 0;
72 /* Get the source ID for both compoment */
73 trace_id = source_ops(csdev)->trace_id(csdev);
74 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
76 /* All you need is one */
77 if (trace_id == i_trace_id)
78 return 1;
80 return 0;
83 static int coresight_source_is_unique(struct coresight_device *csdev)
85 int trace_id = source_ops(csdev)->trace_id(csdev);
87 /* this shouldn't happen */
88 if (trace_id < 0)
89 return 0;
91 return !bus_for_each_dev(&coresight_bustype, NULL,
92 csdev, coresight_id_match);
95 static int coresight_find_link_inport(struct coresight_device *csdev,
96 struct coresight_device *parent)
98 int i;
99 struct coresight_connection *conn;
101 for (i = 0; i < parent->nr_outport; i++) {
102 conn = &parent->conns[i];
103 if (conn->child_dev == csdev)
104 return conn->child_port;
107 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
108 dev_name(&parent->dev), dev_name(&csdev->dev));
110 return 0;
113 static int coresight_find_link_outport(struct coresight_device *csdev,
114 struct coresight_device *child)
116 int i;
117 struct coresight_connection *conn;
119 for (i = 0; i < csdev->nr_outport; i++) {
120 conn = &csdev->conns[i];
121 if (conn->child_dev == child)
122 return conn->outport;
125 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
126 dev_name(&csdev->dev), dev_name(&child->dev));
128 return 0;
131 static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
133 int ret;
135 if (!csdev->enable) {
136 if (sink_ops(csdev)->enable) {
137 ret = sink_ops(csdev)->enable(csdev, mode);
138 if (ret)
139 return ret;
141 csdev->enable = true;
144 atomic_inc(csdev->refcnt);
146 return 0;
149 static void coresight_disable_sink(struct coresight_device *csdev)
151 if (atomic_dec_return(csdev->refcnt) == 0) {
152 if (sink_ops(csdev)->disable) {
153 sink_ops(csdev)->disable(csdev);
154 csdev->enable = false;
159 static int coresight_enable_link(struct coresight_device *csdev,
160 struct coresight_device *parent,
161 struct coresight_device *child)
163 int ret;
164 int link_subtype;
165 int refport, inport, outport;
167 if (!parent || !child)
168 return -EINVAL;
170 inport = coresight_find_link_inport(csdev, parent);
171 outport = coresight_find_link_outport(csdev, child);
172 link_subtype = csdev->subtype.link_subtype;
174 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
175 refport = inport;
176 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
177 refport = outport;
178 else
179 refport = 0;
181 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
182 if (link_ops(csdev)->enable) {
183 ret = link_ops(csdev)->enable(csdev, inport, outport);
184 if (ret)
185 return ret;
189 csdev->enable = true;
191 return 0;
194 static void coresight_disable_link(struct coresight_device *csdev,
195 struct coresight_device *parent,
196 struct coresight_device *child)
198 int i, nr_conns;
199 int link_subtype;
200 int refport, inport, outport;
202 if (!parent || !child)
203 return;
205 inport = coresight_find_link_inport(csdev, parent);
206 outport = coresight_find_link_outport(csdev, child);
207 link_subtype = csdev->subtype.link_subtype;
209 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
210 refport = inport;
211 nr_conns = csdev->nr_inport;
212 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
213 refport = outport;
214 nr_conns = csdev->nr_outport;
215 } else {
216 refport = 0;
217 nr_conns = 1;
220 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
221 if (link_ops(csdev)->disable)
222 link_ops(csdev)->disable(csdev, inport, outport);
225 for (i = 0; i < nr_conns; i++)
226 if (atomic_read(&csdev->refcnt[i]) != 0)
227 return;
229 csdev->enable = false;
232 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
234 int ret;
236 if (!coresight_source_is_unique(csdev)) {
237 dev_warn(&csdev->dev, "traceID %d not unique\n",
238 source_ops(csdev)->trace_id(csdev));
239 return -EINVAL;
242 if (!csdev->enable) {
243 if (source_ops(csdev)->enable) {
244 ret = source_ops(csdev)->enable(csdev, NULL, mode);
245 if (ret)
246 return ret;
248 csdev->enable = true;
251 atomic_inc(csdev->refcnt);
253 return 0;
256 static void coresight_disable_source(struct coresight_device *csdev)
258 if (atomic_dec_return(csdev->refcnt) == 0) {
259 if (source_ops(csdev)->disable) {
260 source_ops(csdev)->disable(csdev, NULL);
261 csdev->enable = false;
266 void coresight_disable_path(struct list_head *path)
268 u32 type;
269 struct coresight_node *nd;
270 struct coresight_device *csdev, *parent, *child;
272 list_for_each_entry(nd, path, link) {
273 csdev = nd->csdev;
274 type = csdev->type;
277 * ETF devices are tricky... They can be a link or a sink,
278 * depending on how they are configured. If an ETF has been
279 * "activated" it will be configured as a sink, otherwise
280 * go ahead with the link configuration.
282 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
283 type = (csdev == coresight_get_sink(path)) ?
284 CORESIGHT_DEV_TYPE_SINK :
285 CORESIGHT_DEV_TYPE_LINK;
287 switch (type) {
288 case CORESIGHT_DEV_TYPE_SINK:
289 coresight_disable_sink(csdev);
290 break;
291 case CORESIGHT_DEV_TYPE_SOURCE:
292 /* sources are disabled from either sysFS or Perf */
293 break;
294 case CORESIGHT_DEV_TYPE_LINK:
295 parent = list_prev_entry(nd, link)->csdev;
296 child = list_next_entry(nd, link)->csdev;
297 coresight_disable_link(csdev, parent, child);
298 break;
299 default:
300 break;
305 int coresight_enable_path(struct list_head *path, u32 mode)
308 int ret = 0;
309 u32 type;
310 struct coresight_node *nd;
311 struct coresight_device *csdev, *parent, *child;
313 list_for_each_entry_reverse(nd, path, link) {
314 csdev = nd->csdev;
315 type = csdev->type;
318 * ETF devices are tricky... They can be a link or a sink,
319 * depending on how they are configured. If an ETF has been
320 * "activated" it will be configured as a sink, otherwise
321 * go ahead with the link configuration.
323 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
324 type = (csdev == coresight_get_sink(path)) ?
325 CORESIGHT_DEV_TYPE_SINK :
326 CORESIGHT_DEV_TYPE_LINK;
328 switch (type) {
329 case CORESIGHT_DEV_TYPE_SINK:
330 ret = coresight_enable_sink(csdev, mode);
331 if (ret)
332 goto err;
333 break;
334 case CORESIGHT_DEV_TYPE_SOURCE:
335 /* sources are enabled from either sysFS or Perf */
336 break;
337 case CORESIGHT_DEV_TYPE_LINK:
338 parent = list_prev_entry(nd, link)->csdev;
339 child = list_next_entry(nd, link)->csdev;
340 ret = coresight_enable_link(csdev, parent, child);
341 if (ret)
342 goto err;
343 break;
344 default:
345 goto err;
349 out:
350 return ret;
351 err:
352 coresight_disable_path(path);
353 goto out;
356 struct coresight_device *coresight_get_sink(struct list_head *path)
358 struct coresight_device *csdev;
360 if (!path)
361 return NULL;
363 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
364 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
365 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
366 return NULL;
368 return csdev;
371 static int coresight_enabled_sink(struct device *dev, void *data)
373 bool *reset = data;
374 struct coresight_device *csdev = to_coresight_device(dev);
376 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
377 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
378 csdev->activated) {
380 * Now that we have a handle on the sink for this session,
381 * disable the sysFS "enable_sink" flag so that possible
382 * concurrent perf session that wish to use another sink don't
383 * trip on it. Doing so has no ramification for the current
384 * session.
386 if (*reset)
387 csdev->activated = false;
389 return 1;
392 return 0;
396 * coresight_get_enabled_sink - returns the first enabled sink found on the bus
397 * @deactivate: Whether the 'enable_sink' flag should be reset
399 * When operated from perf the deactivate parameter should be set to 'true'.
400 * That way the "enabled_sink" flag of the sink that was selected can be reset,
401 * allowing for other concurrent perf sessions to choose a different sink.
403 * When operated from sysFS users have full control and as such the deactivate
404 * parameter should be set to 'false', hence mandating users to explicitly
405 * clear the flag.
407 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
409 struct device *dev = NULL;
411 dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
412 coresight_enabled_sink);
414 return dev ? to_coresight_device(dev) : NULL;
418 * _coresight_build_path - recursively build a path from a @csdev to a sink.
419 * @csdev: The device to start from.
420 * @path: The list to add devices to.
422 * The tree of Coresight device is traversed until an activated sink is
423 * found. From there the sink is added to the list along with all the
424 * devices that led to that point - the end result is a list from source
425 * to sink. In that list the source is the first device and the sink the
426 * last one.
428 static int _coresight_build_path(struct coresight_device *csdev,
429 struct coresight_device *sink,
430 struct list_head *path)
432 int i;
433 bool found = false;
434 struct coresight_node *node;
436 /* An activated sink has been found. Enqueue the element */
437 if (csdev == sink)
438 goto out;
440 /* Not a sink - recursively explore each port found on this element */
441 for (i = 0; i < csdev->nr_outport; i++) {
442 struct coresight_device *child_dev = csdev->conns[i].child_dev;
444 if (child_dev &&
445 _coresight_build_path(child_dev, sink, path) == 0) {
446 found = true;
447 break;
451 if (!found)
452 return -ENODEV;
454 out:
456 * A path from this element to a sink has been found. The elements
457 * leading to the sink are already enqueued, all that is left to do
458 * is tell the PM runtime core we need this element and add a node
459 * for it.
461 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
462 if (!node)
463 return -ENOMEM;
465 node->csdev = csdev;
466 list_add(&node->link, path);
467 pm_runtime_get_sync(csdev->dev.parent);
469 return 0;
472 struct list_head *coresight_build_path(struct coresight_device *source,
473 struct coresight_device *sink)
475 struct list_head *path;
476 int rc;
478 if (!sink)
479 return ERR_PTR(-EINVAL);
481 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
482 if (!path)
483 return ERR_PTR(-ENOMEM);
485 INIT_LIST_HEAD(path);
487 rc = _coresight_build_path(source, sink, path);
488 if (rc) {
489 kfree(path);
490 return ERR_PTR(rc);
493 return path;
497 * coresight_release_path - release a previously built path.
498 * @path: the path to release.
500 * Go through all the elements of a path and 1) removed it from the list and
501 * 2) free the memory allocated for each node.
503 void coresight_release_path(struct list_head *path)
505 struct coresight_device *csdev;
506 struct coresight_node *nd, *next;
508 list_for_each_entry_safe(nd, next, path, link) {
509 csdev = nd->csdev;
511 pm_runtime_put_sync(csdev->dev.parent);
512 list_del(&nd->link);
513 kfree(nd);
516 kfree(path);
517 path = NULL;
520 /** coresight_validate_source - make sure a source has the right credentials
521 * @csdev: the device structure for a source.
522 * @function: the function this was called from.
524 * Assumes the coresight_mutex is held.
526 static int coresight_validate_source(struct coresight_device *csdev,
527 const char *function)
529 u32 type, subtype;
531 type = csdev->type;
532 subtype = csdev->subtype.source_subtype;
534 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
535 dev_err(&csdev->dev, "wrong device type in %s\n", function);
536 return -EINVAL;
539 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
540 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
541 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
542 return -EINVAL;
545 return 0;
548 int coresight_enable(struct coresight_device *csdev)
550 int cpu, ret = 0;
551 struct coresight_device *sink;
552 struct list_head *path;
554 mutex_lock(&coresight_mutex);
556 ret = coresight_validate_source(csdev, __func__);
557 if (ret)
558 goto out;
560 if (csdev->enable)
561 goto out;
564 * Search for a valid sink for this session but don't reset the
565 * "enable_sink" flag in sysFS. Users get to do that explicitly.
567 sink = coresight_get_enabled_sink(false);
568 if (!sink) {
569 ret = -EINVAL;
570 goto out;
573 path = coresight_build_path(csdev, sink);
574 if (IS_ERR(path)) {
575 pr_err("building path(s) failed\n");
576 ret = PTR_ERR(path);
577 goto out;
580 ret = coresight_enable_path(path, CS_MODE_SYSFS);
581 if (ret)
582 goto err_path;
584 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
585 if (ret)
586 goto err_source;
588 switch (csdev->subtype.source_subtype) {
589 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
591 * When working from sysFS it is important to keep track
592 * of the paths that were created so that they can be
593 * undone in 'coresight_disable()'. Since there can only
594 * be a single session per tracer (when working from sysFS)
595 * a per-cpu variable will do just fine.
597 cpu = source_ops(csdev)->cpu_id(csdev);
598 per_cpu(tracer_path, cpu) = path;
599 break;
600 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
601 stm_path = path;
602 break;
603 default:
604 /* We can't be here */
605 break;
608 out:
609 mutex_unlock(&coresight_mutex);
610 return ret;
612 err_source:
613 coresight_disable_path(path);
615 err_path:
616 coresight_release_path(path);
617 goto out;
619 EXPORT_SYMBOL_GPL(coresight_enable);
621 void coresight_disable(struct coresight_device *csdev)
623 int cpu, ret;
624 struct list_head *path = NULL;
626 mutex_lock(&coresight_mutex);
628 ret = coresight_validate_source(csdev, __func__);
629 if (ret)
630 goto out;
632 if (!csdev->enable)
633 goto out;
635 switch (csdev->subtype.source_subtype) {
636 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
637 cpu = source_ops(csdev)->cpu_id(csdev);
638 path = per_cpu(tracer_path, cpu);
639 per_cpu(tracer_path, cpu) = NULL;
640 break;
641 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
642 path = stm_path;
643 stm_path = NULL;
644 break;
645 default:
646 /* We can't be here */
647 break;
650 coresight_disable_source(csdev);
651 coresight_disable_path(path);
652 coresight_release_path(path);
654 out:
655 mutex_unlock(&coresight_mutex);
657 EXPORT_SYMBOL_GPL(coresight_disable);
659 static ssize_t enable_sink_show(struct device *dev,
660 struct device_attribute *attr, char *buf)
662 struct coresight_device *csdev = to_coresight_device(dev);
664 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
667 static ssize_t enable_sink_store(struct device *dev,
668 struct device_attribute *attr,
669 const char *buf, size_t size)
671 int ret;
672 unsigned long val;
673 struct coresight_device *csdev = to_coresight_device(dev);
675 ret = kstrtoul(buf, 10, &val);
676 if (ret)
677 return ret;
679 if (val)
680 csdev->activated = true;
681 else
682 csdev->activated = false;
684 return size;
687 static DEVICE_ATTR_RW(enable_sink);
689 static ssize_t enable_source_show(struct device *dev,
690 struct device_attribute *attr, char *buf)
692 struct coresight_device *csdev = to_coresight_device(dev);
694 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
697 static ssize_t enable_source_store(struct device *dev,
698 struct device_attribute *attr,
699 const char *buf, size_t size)
701 int ret = 0;
702 unsigned long val;
703 struct coresight_device *csdev = to_coresight_device(dev);
705 ret = kstrtoul(buf, 10, &val);
706 if (ret)
707 return ret;
709 if (val) {
710 ret = coresight_enable(csdev);
711 if (ret)
712 return ret;
713 } else {
714 coresight_disable(csdev);
717 return size;
719 static DEVICE_ATTR_RW(enable_source);
721 static struct attribute *coresight_sink_attrs[] = {
722 &dev_attr_enable_sink.attr,
723 NULL,
725 ATTRIBUTE_GROUPS(coresight_sink);
727 static struct attribute *coresight_source_attrs[] = {
728 &dev_attr_enable_source.attr,
729 NULL,
731 ATTRIBUTE_GROUPS(coresight_source);
733 static struct device_type coresight_dev_type[] = {
735 .name = "none",
738 .name = "sink",
739 .groups = coresight_sink_groups,
742 .name = "link",
745 .name = "linksink",
746 .groups = coresight_sink_groups,
749 .name = "source",
750 .groups = coresight_source_groups,
754 static void coresight_device_release(struct device *dev)
756 struct coresight_device *csdev = to_coresight_device(dev);
758 kfree(csdev->conns);
759 kfree(csdev->refcnt);
760 kfree(csdev);
763 static int coresight_orphan_match(struct device *dev, void *data)
765 int i;
766 bool still_orphan = false;
767 struct coresight_device *csdev, *i_csdev;
768 struct coresight_connection *conn;
770 csdev = data;
771 i_csdev = to_coresight_device(dev);
773 /* No need to check oneself */
774 if (csdev == i_csdev)
775 return 0;
777 /* Move on to another component if no connection is orphan */
778 if (!i_csdev->orphan)
779 return 0;
781 * Circle throuch all the connection of that component. If we find
782 * an orphan connection whose name matches @csdev, link it.
784 for (i = 0; i < i_csdev->nr_outport; i++) {
785 conn = &i_csdev->conns[i];
787 /* We have found at least one orphan connection */
788 if (conn->child_dev == NULL) {
789 /* Does it match this newly added device? */
790 if (conn->child_name &&
791 !strcmp(dev_name(&csdev->dev), conn->child_name)) {
792 conn->child_dev = csdev;
793 } else {
794 /* This component still has an orphan */
795 still_orphan = true;
800 i_csdev->orphan = still_orphan;
803 * Returning '0' ensures that all known component on the
804 * bus will be checked.
806 return 0;
809 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
812 * No need to check for a return value as orphan connection(s)
813 * are hooked-up with each newly added component.
815 bus_for_each_dev(&coresight_bustype, NULL,
816 csdev, coresight_orphan_match);
820 static int coresight_name_match(struct device *dev, void *data)
822 char *to_match;
823 struct coresight_device *i_csdev;
825 to_match = data;
826 i_csdev = to_coresight_device(dev);
828 if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
829 return 1;
831 return 0;
834 static void coresight_fixup_device_conns(struct coresight_device *csdev)
836 int i;
837 struct device *dev = NULL;
838 struct coresight_connection *conn;
840 for (i = 0; i < csdev->nr_outport; i++) {
841 conn = &csdev->conns[i];
842 dev = bus_find_device(&coresight_bustype, NULL,
843 (void *)conn->child_name,
844 coresight_name_match);
846 if (dev) {
847 conn->child_dev = to_coresight_device(dev);
848 /* and put reference from 'bus_find_device()' */
849 put_device(dev);
850 } else {
851 csdev->orphan = true;
852 conn->child_dev = NULL;
857 static int coresight_remove_match(struct device *dev, void *data)
859 int i;
860 struct coresight_device *csdev, *iterator;
861 struct coresight_connection *conn;
863 csdev = data;
864 iterator = to_coresight_device(dev);
866 /* No need to check oneself */
867 if (csdev == iterator)
868 return 0;
871 * Circle throuch all the connection of that component. If we find
872 * a connection whose name matches @csdev, remove it.
874 for (i = 0; i < iterator->nr_outport; i++) {
875 conn = &iterator->conns[i];
877 if (conn->child_dev == NULL)
878 continue;
880 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
881 iterator->orphan = true;
882 conn->child_dev = NULL;
883 /* No need to continue */
884 break;
889 * Returning '0' ensures that all known component on the
890 * bus will be checked.
892 return 0;
895 static void coresight_remove_conns(struct coresight_device *csdev)
897 bus_for_each_dev(&coresight_bustype, NULL,
898 csdev, coresight_remove_match);
902 * coresight_timeout - loop until a bit has changed to a specific state.
903 * @addr: base address of the area of interest.
904 * @offset: address of a register, starting from @addr.
905 * @position: the position of the bit of interest.
906 * @value: the value the bit should have.
908 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
909 * TIMEOUT_US has elapsed, which ever happens first.
912 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
914 int i;
915 u32 val;
917 for (i = TIMEOUT_US; i > 0; i--) {
918 val = __raw_readl(addr + offset);
919 /* waiting on the bit to go from 0 to 1 */
920 if (value) {
921 if (val & BIT(position))
922 return 0;
923 /* waiting on the bit to go from 1 to 0 */
924 } else {
925 if (!(val & BIT(position)))
926 return 0;
930 * Delay is arbitrary - the specification doesn't say how long
931 * we are expected to wait. Extra check required to make sure
932 * we don't wait needlessly on the last iteration.
934 if (i - 1)
935 udelay(1);
938 return -EAGAIN;
941 struct bus_type coresight_bustype = {
942 .name = "coresight",
945 static int __init coresight_init(void)
947 return bus_register(&coresight_bustype);
949 postcore_initcall(coresight_init);
951 struct coresight_device *coresight_register(struct coresight_desc *desc)
953 int i;
954 int ret;
955 int link_subtype;
956 int nr_refcnts = 1;
957 atomic_t *refcnts = NULL;
958 struct coresight_device *csdev;
959 struct coresight_connection *conns = NULL;
961 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
962 if (!csdev) {
963 ret = -ENOMEM;
964 goto err_kzalloc_csdev;
967 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
968 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
969 link_subtype = desc->subtype.link_subtype;
971 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
972 nr_refcnts = desc->pdata->nr_inport;
973 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
974 nr_refcnts = desc->pdata->nr_outport;
977 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
978 if (!refcnts) {
979 ret = -ENOMEM;
980 goto err_kzalloc_refcnts;
983 csdev->refcnt = refcnts;
985 csdev->nr_inport = desc->pdata->nr_inport;
986 csdev->nr_outport = desc->pdata->nr_outport;
988 /* Initialise connections if there is at least one outport */
989 if (csdev->nr_outport) {
990 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
991 if (!conns) {
992 ret = -ENOMEM;
993 goto err_kzalloc_conns;
996 for (i = 0; i < csdev->nr_outport; i++) {
997 conns[i].outport = desc->pdata->outports[i];
998 conns[i].child_name = desc->pdata->child_names[i];
999 conns[i].child_port = desc->pdata->child_ports[i];
1003 csdev->conns = conns;
1005 csdev->type = desc->type;
1006 csdev->subtype = desc->subtype;
1007 csdev->ops = desc->ops;
1008 csdev->orphan = false;
1010 csdev->dev.type = &coresight_dev_type[desc->type];
1011 csdev->dev.groups = desc->groups;
1012 csdev->dev.parent = desc->dev;
1013 csdev->dev.release = coresight_device_release;
1014 csdev->dev.bus = &coresight_bustype;
1015 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
1017 ret = device_register(&csdev->dev);
1018 if (ret)
1019 goto err_device_register;
1021 mutex_lock(&coresight_mutex);
1023 coresight_fixup_device_conns(csdev);
1024 coresight_fixup_orphan_conns(csdev);
1026 mutex_unlock(&coresight_mutex);
1028 return csdev;
1030 err_device_register:
1031 kfree(conns);
1032 err_kzalloc_conns:
1033 kfree(refcnts);
1034 err_kzalloc_refcnts:
1035 kfree(csdev);
1036 err_kzalloc_csdev:
1037 return ERR_PTR(ret);
1039 EXPORT_SYMBOL_GPL(coresight_register);
1041 void coresight_unregister(struct coresight_device *csdev)
1043 /* Remove references of that device in the topology */
1044 coresight_remove_conns(csdev);
1045 device_unregister(&csdev->dev);
1047 EXPORT_SYMBOL_GPL(coresight_unregister);