coresight: adding sink parameter to function coresight_build_path()
[firefly-linux-kernel-4.4.55.git] / drivers / hwtracing / coresight / coresight.c
1 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
2  *
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.
6  *
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.
11  */
12
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>
27
28 #include "coresight-priv.h"
29
30 static DEFINE_MUTEX(coresight_mutex);
31
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.
36  */
37 struct coresight_node {
38         struct coresight_device *csdev;
39         struct list_head link;
40 };
41
42 /*
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.
45  */
46 static DEFINE_PER_CPU(struct list_head *, tracer_path);
47
48 /*
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.
53  */
54 static struct list_head *stm_path;
55
56 static int coresight_id_match(struct device *dev, void *data)
57 {
58         int trace_id, i_trace_id;
59         struct coresight_device *csdev, *i_csdev;
60
61         csdev = data;
62         i_csdev = to_coresight_device(dev);
63
64         /*
65          * No need to care about oneself and components that are not
66          * sources or not enabled
67          */
68         if (i_csdev == csdev || !i_csdev->enable ||
69             i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
70                 return 0;
71
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);
75
76         /* All you need is one */
77         if (trace_id == i_trace_id)
78                 return 1;
79
80         return 0;
81 }
82
83 static int coresight_source_is_unique(struct coresight_device *csdev)
84 {
85         int trace_id = source_ops(csdev)->trace_id(csdev);
86
87         /* this shouldn't happen */
88         if (trace_id < 0)
89                 return 0;
90
91         return !bus_for_each_dev(&coresight_bustype, NULL,
92                                  csdev, coresight_id_match);
93 }
94
95 static int coresight_find_link_inport(struct coresight_device *csdev,
96                                       struct coresight_device *parent)
97 {
98         int i;
99         struct coresight_connection *conn;
100
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;
105         }
106
107         dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
108                 dev_name(&parent->dev), dev_name(&csdev->dev));
109
110         return 0;
111 }
112
113 static int coresight_find_link_outport(struct coresight_device *csdev,
114                                        struct coresight_device *child)
115 {
116         int i;
117         struct coresight_connection *conn;
118
119         for (i = 0; i < csdev->nr_outport; i++) {
120                 conn = &csdev->conns[i];
121                 if (conn->child_dev == child)
122                         return conn->outport;
123         }
124
125         dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
126                 dev_name(&csdev->dev), dev_name(&child->dev));
127
128         return 0;
129 }
130
131 static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
132 {
133         int ret;
134
135         if (!csdev->enable) {
136                 if (sink_ops(csdev)->enable) {
137                         ret = sink_ops(csdev)->enable(csdev, mode);
138                         if (ret)
139                                 return ret;
140                 }
141                 csdev->enable = true;
142         }
143
144         atomic_inc(csdev->refcnt);
145
146         return 0;
147 }
148
149 static void coresight_disable_sink(struct coresight_device *csdev)
150 {
151         if (atomic_dec_return(csdev->refcnt) == 0) {
152                 if (sink_ops(csdev)->disable) {
153                         sink_ops(csdev)->disable(csdev);
154                         csdev->enable = false;
155                 }
156         }
157 }
158
159 static int coresight_enable_link(struct coresight_device *csdev,
160                                  struct coresight_device *parent,
161                                  struct coresight_device *child)
162 {
163         int ret;
164         int link_subtype;
165         int refport, inport, outport;
166
167         if (!parent || !child)
168                 return -EINVAL;
169
170         inport = coresight_find_link_inport(csdev, parent);
171         outport = coresight_find_link_outport(csdev, child);
172         link_subtype = csdev->subtype.link_subtype;
173
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;
180
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;
186                 }
187         }
188
189         csdev->enable = true;
190
191         return 0;
192 }
193
194 static void coresight_disable_link(struct coresight_device *csdev,
195                                    struct coresight_device *parent,
196                                    struct coresight_device *child)
197 {
198         int i, nr_conns;
199         int link_subtype;
200         int refport, inport, outport;
201
202         if (!parent || !child)
203                 return;
204
205         inport = coresight_find_link_inport(csdev, parent);
206         outport = coresight_find_link_outport(csdev, child);
207         link_subtype = csdev->subtype.link_subtype;
208
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;
218         }
219
220         if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
221                 if (link_ops(csdev)->disable)
222                         link_ops(csdev)->disable(csdev, inport, outport);
223         }
224
225         for (i = 0; i < nr_conns; i++)
226                 if (atomic_read(&csdev->refcnt[i]) != 0)
227                         return;
228
229         csdev->enable = false;
230 }
231
232 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
233 {
234         int ret;
235
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;
240         }
241
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;
247                 }
248                 csdev->enable = true;
249         }
250
251         atomic_inc(csdev->refcnt);
252
253         return 0;
254 }
255
256 static void coresight_disable_source(struct coresight_device *csdev)
257 {
258         if (atomic_dec_return(csdev->refcnt) == 0) {
259                 if (source_ops(csdev)->disable) {
260                         source_ops(csdev)->disable(csdev);
261                         csdev->enable = false;
262                 }
263         }
264 }
265
266 void coresight_disable_path(struct list_head *path)
267 {
268         u32 type;
269         struct coresight_node *nd;
270         struct coresight_device *csdev, *parent, *child;
271
272         list_for_each_entry(nd, path, link) {
273                 csdev = nd->csdev;
274                 type = csdev->type;
275
276                 /*
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.
281                  */
282                 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
283                         type = (csdev == coresight_get_sink(path)) ?
284                                                 CORESIGHT_DEV_TYPE_SINK :
285                                                 CORESIGHT_DEV_TYPE_LINK;
286
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;
301                 }
302         }
303 }
304
305 int coresight_enable_path(struct list_head *path, u32 mode)
306 {
307
308         int ret = 0;
309         u32 type;
310         struct coresight_node *nd;
311         struct coresight_device *csdev, *parent, *child;
312
313         list_for_each_entry_reverse(nd, path, link) {
314                 csdev = nd->csdev;
315                 type = csdev->type;
316
317                 /*
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.
322                  */
323                 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
324                         type = (csdev == coresight_get_sink(path)) ?
325                                                 CORESIGHT_DEV_TYPE_SINK :
326                                                 CORESIGHT_DEV_TYPE_LINK;
327
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;
346                 }
347         }
348
349 out:
350         return ret;
351 err:
352         coresight_disable_path(path);
353         goto out;
354 }
355
356 struct coresight_device *coresight_get_sink(struct list_head *path)
357 {
358         struct coresight_device *csdev;
359
360         if (!path)
361                 return NULL;
362
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;
367
368         return csdev;
369 }
370
371 /**
372  * _coresight_build_path - recursively build a path from a @csdev to a sink.
373  * @csdev:      The device to start from.
374  * @sink:       The name of the sink this path should connect with.
375  * @path:       The list to add devices to.
376  *
377  * The tree of Coresight device is traversed until an activated sink or
378  * the one specified by @sink is found.
379  * From there the sink is added to the list along with all the devices that
380  * led to that point - the end result is a list from source to sink. In that
381  * list the source is the first device and the sink the last one.
382  */
383 static int _coresight_build_path(struct coresight_device *csdev,
384                                  const char *sink, struct list_head *path)
385 {
386         int i;
387         bool found = false;
388         struct coresight_node *node;
389
390         /*
391          * First see if we are dealing with a sink.  If we have one check if
392          * it was selected via sysFS or the perf cmd line.
393          */
394         if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
395             csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
396                 /* Activated via perf cmd line */
397                 if (sink && !strcmp(dev_name(&csdev->dev), sink))
398                         goto out;
399                 /* Activatred via sysFS */
400                 if (csdev->activated)
401                         goto out;
402         }
403
404         /* Not a sink - recursively explore each port found on this element */
405         for (i = 0; i < csdev->nr_outport; i++) {
406                 struct coresight_device *child_dev = csdev->conns[i].child_dev;
407
408                 if (child_dev &&
409                     _coresight_build_path(child_dev, sink, path) == 0) {
410                         found = true;
411                         break;
412                 }
413         }
414
415         if (!found)
416                 return -ENODEV;
417
418 out:
419         /*
420          * A path from this element to a sink has been found.  The elements
421          * leading to the sink are already enqueued, all that is left to do
422          * is tell the PM runtime core we need this element and add a node
423          * for it.
424          */
425         node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
426         if (!node)
427                 return -ENOMEM;
428
429         node->csdev = csdev;
430         list_add(&node->link, path);
431         pm_runtime_get_sync(csdev->dev.parent);
432
433         return 0;
434 }
435
436 struct list_head *coresight_build_path(struct coresight_device *csdev,
437                                        const char *sink)
438 {
439         struct list_head *path;
440         int rc;
441
442         path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
443         if (!path)
444                 return NULL;
445
446         INIT_LIST_HEAD(path);
447
448         rc = _coresight_build_path(csdev, sink, path);
449         if (rc) {
450                 kfree(path);
451                 return ERR_PTR(rc);
452         }
453
454         return path;
455 }
456
457 /**
458  * coresight_release_path - release a previously built path.
459  * @path:       the path to release.
460  *
461  * Go through all the elements of a path and 1) removed it from the list and
462  * 2) free the memory allocated for each node.
463  */
464 void coresight_release_path(struct list_head *path)
465 {
466         struct coresight_device *csdev;
467         struct coresight_node *nd, *next;
468
469         list_for_each_entry_safe(nd, next, path, link) {
470                 csdev = nd->csdev;
471
472                 pm_runtime_put_sync(csdev->dev.parent);
473                 list_del(&nd->link);
474                 kfree(nd);
475         }
476
477         kfree(path);
478         path = NULL;
479 }
480
481 /** coresight_validate_source - make sure a source has the right credentials
482  *  @csdev:     the device structure for a source.
483  *  @function:  the function this was called from.
484  *
485  * Assumes the coresight_mutex is held.
486  */
487 static int coresight_validate_source(struct coresight_device *csdev,
488                                      const char *function)
489 {
490         u32 type, subtype;
491
492         type = csdev->type;
493         subtype = csdev->subtype.source_subtype;
494
495         if (type != CORESIGHT_DEV_TYPE_SOURCE) {
496                 dev_err(&csdev->dev, "wrong device type in %s\n", function);
497                 return -EINVAL;
498         }
499
500         if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
501             subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
502                 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
503                 return -EINVAL;
504         }
505
506         return 0;
507 }
508
509 int coresight_enable(struct coresight_device *csdev)
510 {
511         int cpu, ret = 0;
512         struct list_head *path;
513
514         mutex_lock(&coresight_mutex);
515
516         ret = coresight_validate_source(csdev, __func__);
517         if (ret)
518                 goto out;
519
520         if (csdev->enable)
521                 goto out;
522
523         path = coresight_build_path(csdev, NULL);
524         if (IS_ERR(path)) {
525                 pr_err("building path(s) failed\n");
526                 ret = PTR_ERR(path);
527                 goto out;
528         }
529
530         ret = coresight_enable_path(path, CS_MODE_SYSFS);
531         if (ret)
532                 goto err_path;
533
534         ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
535         if (ret)
536                 goto err_source;
537
538         switch (csdev->subtype.source_subtype) {
539         case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
540                 /*
541                  * When working from sysFS it is important to keep track
542                  * of the paths that were created so that they can be
543                  * undone in 'coresight_disable()'.  Since there can only
544                  * be a single session per tracer (when working from sysFS)
545                  * a per-cpu variable will do just fine.
546                  */
547                 cpu = source_ops(csdev)->cpu_id(csdev);
548                 per_cpu(tracer_path, cpu) = path;
549                 break;
550         case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
551                 stm_path = path;
552                 break;
553         default:
554                 /* We can't be here */
555                 break;
556         }
557
558 out:
559         mutex_unlock(&coresight_mutex);
560         return ret;
561
562 err_source:
563         coresight_disable_path(path);
564
565 err_path:
566         coresight_release_path(path);
567         goto out;
568 }
569 EXPORT_SYMBOL_GPL(coresight_enable);
570
571 void coresight_disable(struct coresight_device *csdev)
572 {
573         int cpu, ret;
574         struct list_head *path = NULL;
575
576         mutex_lock(&coresight_mutex);
577
578         ret = coresight_validate_source(csdev, __func__);
579         if (ret)
580                 goto out;
581
582         if (!csdev->enable)
583                 goto out;
584
585         switch (csdev->subtype.source_subtype) {
586         case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
587                 cpu = source_ops(csdev)->cpu_id(csdev);
588                 path = per_cpu(tracer_path, cpu);
589                 per_cpu(tracer_path, cpu) = NULL;
590                 break;
591         case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
592                 path = stm_path;
593                 stm_path = NULL;
594                 break;
595         default:
596                 /* We can't be here */
597                 break;
598         }
599
600         coresight_disable_source(csdev);
601         coresight_disable_path(path);
602         coresight_release_path(path);
603
604 out:
605         mutex_unlock(&coresight_mutex);
606 }
607 EXPORT_SYMBOL_GPL(coresight_disable);
608
609 static ssize_t enable_sink_show(struct device *dev,
610                                 struct device_attribute *attr, char *buf)
611 {
612         struct coresight_device *csdev = to_coresight_device(dev);
613
614         return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
615 }
616
617 static ssize_t enable_sink_store(struct device *dev,
618                                  struct device_attribute *attr,
619                                  const char *buf, size_t size)
620 {
621         int ret;
622         unsigned long val;
623         struct coresight_device *csdev = to_coresight_device(dev);
624
625         ret = kstrtoul(buf, 10, &val);
626         if (ret)
627                 return ret;
628
629         if (val)
630                 csdev->activated = true;
631         else
632                 csdev->activated = false;
633
634         return size;
635
636 }
637 static DEVICE_ATTR_RW(enable_sink);
638
639 static ssize_t enable_source_show(struct device *dev,
640                                   struct device_attribute *attr, char *buf)
641 {
642         struct coresight_device *csdev = to_coresight_device(dev);
643
644         return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
645 }
646
647 static ssize_t enable_source_store(struct device *dev,
648                                    struct device_attribute *attr,
649                                    const char *buf, size_t size)
650 {
651         int ret = 0;
652         unsigned long val;
653         struct coresight_device *csdev = to_coresight_device(dev);
654
655         ret = kstrtoul(buf, 10, &val);
656         if (ret)
657                 return ret;
658
659         if (val) {
660                 ret = coresight_enable(csdev);
661                 if (ret)
662                         return ret;
663         } else {
664                 coresight_disable(csdev);
665         }
666
667         return size;
668 }
669 static DEVICE_ATTR_RW(enable_source);
670
671 static struct attribute *coresight_sink_attrs[] = {
672         &dev_attr_enable_sink.attr,
673         NULL,
674 };
675 ATTRIBUTE_GROUPS(coresight_sink);
676
677 static struct attribute *coresight_source_attrs[] = {
678         &dev_attr_enable_source.attr,
679         NULL,
680 };
681 ATTRIBUTE_GROUPS(coresight_source);
682
683 static struct device_type coresight_dev_type[] = {
684         {
685                 .name = "none",
686         },
687         {
688                 .name = "sink",
689                 .groups = coresight_sink_groups,
690         },
691         {
692                 .name = "link",
693         },
694         {
695                 .name = "linksink",
696                 .groups = coresight_sink_groups,
697         },
698         {
699                 .name = "source",
700                 .groups = coresight_source_groups,
701         },
702 };
703
704 static void coresight_device_release(struct device *dev)
705 {
706         struct coresight_device *csdev = to_coresight_device(dev);
707
708         kfree(csdev->conns);
709         kfree(csdev->refcnt);
710         kfree(csdev);
711 }
712
713 static int coresight_orphan_match(struct device *dev, void *data)
714 {
715         int i;
716         bool still_orphan = false;
717         struct coresight_device *csdev, *i_csdev;
718         struct coresight_connection *conn;
719
720         csdev = data;
721         i_csdev = to_coresight_device(dev);
722
723         /* No need to check oneself */
724         if (csdev == i_csdev)
725                 return 0;
726
727         /* Move on to another component if no connection is orphan */
728         if (!i_csdev->orphan)
729                 return 0;
730         /*
731          * Circle throuch all the connection of that component.  If we find
732          * an orphan connection whose name matches @csdev, link it.
733          */
734         for (i = 0; i < i_csdev->nr_outport; i++) {
735                 conn = &i_csdev->conns[i];
736
737                 /* We have found at least one orphan connection */
738                 if (conn->child_dev == NULL) {
739                         /* Does it match this newly added device? */
740                         if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
741                                 conn->child_dev = csdev;
742                         } else {
743                                 /* This component still has an orphan */
744                                 still_orphan = true;
745                         }
746                 }
747         }
748
749         i_csdev->orphan = still_orphan;
750
751         /*
752          * Returning '0' ensures that all known component on the
753          * bus will be checked.
754          */
755         return 0;
756 }
757
758 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
759 {
760         /*
761          * No need to check for a return value as orphan connection(s)
762          * are hooked-up with each newly added component.
763          */
764         bus_for_each_dev(&coresight_bustype, NULL,
765                          csdev, coresight_orphan_match);
766 }
767
768
769 static int coresight_name_match(struct device *dev, void *data)
770 {
771         char *to_match;
772         struct coresight_device *i_csdev;
773
774         to_match = data;
775         i_csdev = to_coresight_device(dev);
776
777         if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
778                 return 1;
779
780         return 0;
781 }
782
783 static void coresight_fixup_device_conns(struct coresight_device *csdev)
784 {
785         int i;
786         struct device *dev = NULL;
787         struct coresight_connection *conn;
788
789         for (i = 0; i < csdev->nr_outport; i++) {
790                 conn = &csdev->conns[i];
791                 dev = bus_find_device(&coresight_bustype, NULL,
792                                       (void *)conn->child_name,
793                                       coresight_name_match);
794
795                 if (dev) {
796                         conn->child_dev = to_coresight_device(dev);
797                         /* and put reference from 'bus_find_device()' */
798                         put_device(dev);
799                 } else {
800                         csdev->orphan = true;
801                         conn->child_dev = NULL;
802                 }
803         }
804 }
805
806 static int coresight_remove_match(struct device *dev, void *data)
807 {
808         int i;
809         struct coresight_device *csdev, *iterator;
810         struct coresight_connection *conn;
811
812         csdev = data;
813         iterator = to_coresight_device(dev);
814
815         /* No need to check oneself */
816         if (csdev == iterator)
817                 return 0;
818
819         /*
820          * Circle throuch all the connection of that component.  If we find
821          * a connection whose name matches @csdev, remove it.
822          */
823         for (i = 0; i < iterator->nr_outport; i++) {
824                 conn = &iterator->conns[i];
825
826                 if (conn->child_dev == NULL)
827                         continue;
828
829                 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
830                         iterator->orphan = true;
831                         conn->child_dev = NULL;
832                         /* No need to continue */
833                         break;
834                 }
835         }
836
837         /*
838          * Returning '0' ensures that all known component on the
839          * bus will be checked.
840          */
841         return 0;
842 }
843
844 static void coresight_remove_conns(struct coresight_device *csdev)
845 {
846         bus_for_each_dev(&coresight_bustype, NULL,
847                          csdev, coresight_remove_match);
848 }
849
850 /**
851  * coresight_timeout - loop until a bit has changed to a specific state.
852  * @addr: base address of the area of interest.
853  * @offset: address of a register, starting from @addr.
854  * @position: the position of the bit of interest.
855  * @value: the value the bit should have.
856  *
857  * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
858  * TIMEOUT_US has elapsed, which ever happens first.
859  */
860
861 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
862 {
863         int i;
864         u32 val;
865
866         for (i = TIMEOUT_US; i > 0; i--) {
867                 val = __raw_readl(addr + offset);
868                 /* waiting on the bit to go from 0 to 1 */
869                 if (value) {
870                         if (val & BIT(position))
871                                 return 0;
872                 /* waiting on the bit to go from 1 to 0 */
873                 } else {
874                         if (!(val & BIT(position)))
875                                 return 0;
876                 }
877
878                 /*
879                  * Delay is arbitrary - the specification doesn't say how long
880                  * we are expected to wait.  Extra check required to make sure
881                  * we don't wait needlessly on the last iteration.
882                  */
883                 if (i - 1)
884                         udelay(1);
885         }
886
887         return -EAGAIN;
888 }
889
890 struct bus_type coresight_bustype = {
891         .name   = "coresight",
892 };
893
894 static int __init coresight_init(void)
895 {
896         return bus_register(&coresight_bustype);
897 }
898 postcore_initcall(coresight_init);
899
900 struct coresight_device *coresight_register(struct coresight_desc *desc)
901 {
902         int i;
903         int ret;
904         int link_subtype;
905         int nr_refcnts = 1;
906         atomic_t *refcnts = NULL;
907         struct coresight_device *csdev;
908         struct coresight_connection *conns;
909
910         csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
911         if (!csdev) {
912                 ret = -ENOMEM;
913                 goto err_kzalloc_csdev;
914         }
915
916         if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
917             desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
918                 link_subtype = desc->subtype.link_subtype;
919
920                 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
921                         nr_refcnts = desc->pdata->nr_inport;
922                 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
923                         nr_refcnts = desc->pdata->nr_outport;
924         }
925
926         refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
927         if (!refcnts) {
928                 ret = -ENOMEM;
929                 goto err_kzalloc_refcnts;
930         }
931
932         csdev->refcnt = refcnts;
933
934         csdev->nr_inport = desc->pdata->nr_inport;
935         csdev->nr_outport = desc->pdata->nr_outport;
936         conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
937         if (!conns) {
938                 ret = -ENOMEM;
939                 goto err_kzalloc_conns;
940         }
941
942         for (i = 0; i < csdev->nr_outport; i++) {
943                 conns[i].outport = desc->pdata->outports[i];
944                 conns[i].child_name = desc->pdata->child_names[i];
945                 conns[i].child_port = desc->pdata->child_ports[i];
946         }
947
948         csdev->conns = conns;
949
950         csdev->type = desc->type;
951         csdev->subtype = desc->subtype;
952         csdev->ops = desc->ops;
953         csdev->orphan = false;
954
955         csdev->dev.type = &coresight_dev_type[desc->type];
956         csdev->dev.groups = desc->groups;
957         csdev->dev.parent = desc->dev;
958         csdev->dev.release = coresight_device_release;
959         csdev->dev.bus = &coresight_bustype;
960         dev_set_name(&csdev->dev, "%s", desc->pdata->name);
961
962         ret = device_register(&csdev->dev);
963         if (ret)
964                 goto err_device_register;
965
966         mutex_lock(&coresight_mutex);
967
968         coresight_fixup_device_conns(csdev);
969         coresight_fixup_orphan_conns(csdev);
970
971         mutex_unlock(&coresight_mutex);
972
973         return csdev;
974
975 err_device_register:
976         kfree(conns);
977 err_kzalloc_conns:
978         kfree(refcnts);
979 err_kzalloc_refcnts:
980         kfree(csdev);
981 err_kzalloc_csdev:
982         return ERR_PTR(ret);
983 }
984 EXPORT_SYMBOL_GPL(coresight_register);
985
986 void coresight_unregister(struct coresight_device *csdev)
987 {
988         /* Remove references of that device in the topology */
989         coresight_remove_conns(csdev);
990         device_unregister(&csdev->dev);
991 }
992 EXPORT_SYMBOL_GPL(coresight_unregister);