GNU Linux-libre 4.9.337-gnu1
[releases.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 -ENODEV;
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 -ENODEV;
129 }
130
131 static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
132 {
133         int ret;
134
135         /*
136          * We need to make sure the "new" session is compatible with the
137          * existing "mode" of operation.
138          */
139         if (sink_ops(csdev)->enable) {
140                 ret = sink_ops(csdev)->enable(csdev, mode);
141                 if (ret)
142                         return ret;
143                 csdev->enable = true;
144         }
145
146         atomic_inc(csdev->refcnt);
147
148         return 0;
149 }
150
151 static void coresight_disable_sink(struct coresight_device *csdev)
152 {
153         if (atomic_dec_return(csdev->refcnt) == 0) {
154                 if (sink_ops(csdev)->disable) {
155                         sink_ops(csdev)->disable(csdev);
156                         csdev->enable = false;
157                 }
158         }
159 }
160
161 static int coresight_enable_link(struct coresight_device *csdev,
162                                  struct coresight_device *parent,
163                                  struct coresight_device *child)
164 {
165         int ret;
166         int link_subtype;
167         int refport, inport, outport;
168
169         if (!parent || !child)
170                 return -EINVAL;
171
172         inport = coresight_find_link_inport(csdev, parent);
173         outport = coresight_find_link_outport(csdev, child);
174         link_subtype = csdev->subtype.link_subtype;
175
176         if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
177                 refport = inport;
178         else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
179                 refport = outport;
180         else
181                 refport = 0;
182
183         if (refport < 0)
184                 return refport;
185
186         if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
187                 if (link_ops(csdev)->enable) {
188                         ret = link_ops(csdev)->enable(csdev, inport, outport);
189                         if (ret)
190                                 return ret;
191                 }
192         }
193
194         csdev->enable = true;
195
196         return 0;
197 }
198
199 static void coresight_disable_link(struct coresight_device *csdev,
200                                    struct coresight_device *parent,
201                                    struct coresight_device *child)
202 {
203         int i, nr_conns;
204         int link_subtype;
205         int refport, inport, outport;
206
207         if (!parent || !child)
208                 return;
209
210         inport = coresight_find_link_inport(csdev, parent);
211         outport = coresight_find_link_outport(csdev, child);
212         link_subtype = csdev->subtype.link_subtype;
213
214         if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
215                 refport = inport;
216                 nr_conns = csdev->nr_inport;
217         } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
218                 refport = outport;
219                 nr_conns = csdev->nr_outport;
220         } else {
221                 refport = 0;
222                 nr_conns = 1;
223         }
224
225         if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
226                 if (link_ops(csdev)->disable)
227                         link_ops(csdev)->disable(csdev, inport, outport);
228         }
229
230         for (i = 0; i < nr_conns; i++)
231                 if (atomic_read(&csdev->refcnt[i]) != 0)
232                         return;
233
234         csdev->enable = false;
235 }
236
237 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
238 {
239         int ret;
240
241         if (!coresight_source_is_unique(csdev)) {
242                 dev_warn(&csdev->dev, "traceID %d not unique\n",
243                          source_ops(csdev)->trace_id(csdev));
244                 return -EINVAL;
245         }
246
247         if (!csdev->enable) {
248                 if (source_ops(csdev)->enable) {
249                         ret = source_ops(csdev)->enable(csdev, NULL, mode);
250                         if (ret)
251                                 return ret;
252                 }
253                 csdev->enable = true;
254         }
255
256         atomic_inc(csdev->refcnt);
257
258         return 0;
259 }
260
261 static void coresight_disable_source(struct coresight_device *csdev)
262 {
263         if (atomic_dec_return(csdev->refcnt) == 0) {
264                 if (source_ops(csdev)->disable) {
265                         source_ops(csdev)->disable(csdev, NULL);
266                         csdev->enable = false;
267                 }
268         }
269 }
270
271 void coresight_disable_path(struct list_head *path)
272 {
273         u32 type;
274         struct coresight_node *nd;
275         struct coresight_device *csdev, *parent, *child;
276
277         list_for_each_entry(nd, path, link) {
278                 csdev = nd->csdev;
279                 type = csdev->type;
280
281                 /*
282                  * ETF devices are tricky... They can be a link or a sink,
283                  * depending on how they are configured.  If an ETF has been
284                  * "activated" it will be configured as a sink, otherwise
285                  * go ahead with the link configuration.
286                  */
287                 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
288                         type = (csdev == coresight_get_sink(path)) ?
289                                                 CORESIGHT_DEV_TYPE_SINK :
290                                                 CORESIGHT_DEV_TYPE_LINK;
291
292                 switch (type) {
293                 case CORESIGHT_DEV_TYPE_SINK:
294                         coresight_disable_sink(csdev);
295                         break;
296                 case CORESIGHT_DEV_TYPE_SOURCE:
297                         /* sources are disabled from either sysFS or Perf */
298                         break;
299                 case CORESIGHT_DEV_TYPE_LINK:
300                         parent = list_prev_entry(nd, link)->csdev;
301                         child = list_next_entry(nd, link)->csdev;
302                         coresight_disable_link(csdev, parent, child);
303                         break;
304                 default:
305                         break;
306                 }
307         }
308 }
309
310 int coresight_enable_path(struct list_head *path, u32 mode)
311 {
312
313         int ret = 0;
314         u32 type;
315         struct coresight_node *nd;
316         struct coresight_device *csdev, *parent, *child;
317
318         list_for_each_entry_reverse(nd, path, link) {
319                 csdev = nd->csdev;
320                 type = csdev->type;
321
322                 /*
323                  * ETF devices are tricky... They can be a link or a sink,
324                  * depending on how they are configured.  If an ETF has been
325                  * "activated" it will be configured as a sink, otherwise
326                  * go ahead with the link configuration.
327                  */
328                 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
329                         type = (csdev == coresight_get_sink(path)) ?
330                                                 CORESIGHT_DEV_TYPE_SINK :
331                                                 CORESIGHT_DEV_TYPE_LINK;
332
333                 switch (type) {
334                 case CORESIGHT_DEV_TYPE_SINK:
335                         ret = coresight_enable_sink(csdev, mode);
336                         /*
337                          * Sink is the first component turned on. If we
338                          * failed to enable the sink, there are no components
339                          * that need disabling. Disabling the path here
340                          * would mean we could disrupt an existing session.
341                          */
342                         if (ret)
343                                 goto out;
344                         break;
345                 case CORESIGHT_DEV_TYPE_SOURCE:
346                         /* sources are enabled from either sysFS or Perf */
347                         break;
348                 case CORESIGHT_DEV_TYPE_LINK:
349                         parent = list_prev_entry(nd, link)->csdev;
350                         child = list_next_entry(nd, link)->csdev;
351                         ret = coresight_enable_link(csdev, parent, child);
352                         if (ret)
353                                 goto err;
354                         break;
355                 default:
356                         goto err;
357                 }
358         }
359
360 out:
361         return ret;
362 err:
363         coresight_disable_path(path);
364         goto out;
365 }
366
367 struct coresight_device *coresight_get_sink(struct list_head *path)
368 {
369         struct coresight_device *csdev;
370
371         if (!path)
372                 return NULL;
373
374         csdev = list_last_entry(path, struct coresight_node, link)->csdev;
375         if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
376             csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
377                 return NULL;
378
379         return csdev;
380 }
381
382 /**
383  * _coresight_build_path - recursively build a path from a @csdev to a sink.
384  * @csdev:      The device to start from.
385  * @path:       The list to add devices to.
386  *
387  * The tree of Coresight device is traversed until an activated sink is
388  * found.  From there the sink is added to the list along with all the
389  * devices that led to that point - the end result is a list from source
390  * to sink. In that list the source is the first device and the sink the
391  * last one.
392  */
393 static int _coresight_build_path(struct coresight_device *csdev,
394                                  struct list_head *path)
395 {
396         int i;
397         bool found = false;
398         struct coresight_node *node;
399
400         /* An activated sink has been found.  Enqueue the element */
401         if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
402              csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) && csdev->activated)
403                 goto out;
404
405         /* Not a sink - recursively explore each port found on this element */
406         for (i = 0; i < csdev->nr_outport; i++) {
407                 struct coresight_device *child_dev = csdev->conns[i].child_dev;
408
409                 if (child_dev && _coresight_build_path(child_dev, 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 {
438         struct list_head *path;
439         int rc;
440
441         path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
442         if (!path)
443                 return ERR_PTR(-ENOMEM);
444
445         INIT_LIST_HEAD(path);
446
447         rc = _coresight_build_path(csdev, path);
448         if (rc) {
449                 kfree(path);
450                 return ERR_PTR(rc);
451         }
452
453         return path;
454 }
455
456 /**
457  * coresight_release_path - release a previously built path.
458  * @path:       the path to release.
459  *
460  * Go through all the elements of a path and 1) removed it from the list and
461  * 2) free the memory allocated for each node.
462  */
463 void coresight_release_path(struct list_head *path)
464 {
465         struct coresight_device *csdev;
466         struct coresight_node *nd, *next;
467
468         list_for_each_entry_safe(nd, next, path, link) {
469                 csdev = nd->csdev;
470
471                 pm_runtime_put_sync(csdev->dev.parent);
472                 list_del(&nd->link);
473                 kfree(nd);
474         }
475
476         kfree(path);
477         path = NULL;
478 }
479
480 /** coresight_validate_source - make sure a source has the right credentials
481  *  @csdev:     the device structure for a source.
482  *  @function:  the function this was called from.
483  *
484  * Assumes the coresight_mutex is held.
485  */
486 static int coresight_validate_source(struct coresight_device *csdev,
487                                      const char *function)
488 {
489         u32 type, subtype;
490
491         type = csdev->type;
492         subtype = csdev->subtype.source_subtype;
493
494         if (type != CORESIGHT_DEV_TYPE_SOURCE) {
495                 dev_err(&csdev->dev, "wrong device type in %s\n", function);
496                 return -EINVAL;
497         }
498
499         if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
500             subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
501                 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
502                 return -EINVAL;
503         }
504
505         return 0;
506 }
507
508 int coresight_enable(struct coresight_device *csdev)
509 {
510         int cpu, ret = 0;
511         struct list_head *path;
512         enum coresight_dev_subtype_source subtype;
513
514         subtype = csdev->subtype.source_subtype;
515
516         mutex_lock(&coresight_mutex);
517
518         ret = coresight_validate_source(csdev, __func__);
519         if (ret)
520                 goto out;
521
522         if (csdev->enable) {
523                 /*
524                  * There could be multiple applications driving the software
525                  * source. So keep the refcount for each such user when the
526                  * source is already enabled.
527                  */
528                 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
529                         atomic_inc(csdev->refcnt);
530                 goto out;
531         }
532
533         path = coresight_build_path(csdev);
534         if (IS_ERR(path)) {
535                 pr_err("building path(s) failed\n");
536                 ret = PTR_ERR(path);
537                 goto out;
538         }
539
540         ret = coresight_enable_path(path, CS_MODE_SYSFS);
541         if (ret)
542                 goto err_path;
543
544         ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
545         if (ret)
546                 goto err_source;
547
548         switch (subtype) {
549         case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
550                 /*
551                  * When working from sysFS it is important to keep track
552                  * of the paths that were created so that they can be
553                  * undone in 'coresight_disable()'.  Since there can only
554                  * be a single session per tracer (when working from sysFS)
555                  * a per-cpu variable will do just fine.
556                  */
557                 cpu = source_ops(csdev)->cpu_id(csdev);
558                 per_cpu(tracer_path, cpu) = path;
559                 break;
560         case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
561                 stm_path = path;
562                 break;
563         default:
564                 /* We can't be here */
565                 break;
566         }
567
568 out:
569         mutex_unlock(&coresight_mutex);
570         return ret;
571
572 err_source:
573         coresight_disable_path(path);
574
575 err_path:
576         coresight_release_path(path);
577         goto out;
578 }
579 EXPORT_SYMBOL_GPL(coresight_enable);
580
581 void coresight_disable(struct coresight_device *csdev)
582 {
583         int cpu, ret;
584         struct list_head *path = NULL;
585
586         mutex_lock(&coresight_mutex);
587
588         ret = coresight_validate_source(csdev, __func__);
589         if (ret)
590                 goto out;
591
592         if (!csdev->enable)
593                 goto out;
594
595         switch (csdev->subtype.source_subtype) {
596         case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
597                 cpu = source_ops(csdev)->cpu_id(csdev);
598                 path = per_cpu(tracer_path, cpu);
599                 per_cpu(tracer_path, cpu) = NULL;
600                 break;
601         case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
602                 path = stm_path;
603                 stm_path = NULL;
604                 break;
605         default:
606                 /* We can't be here */
607                 break;
608         }
609
610         coresight_disable_source(csdev);
611         coresight_disable_path(path);
612         coresight_release_path(path);
613
614 out:
615         mutex_unlock(&coresight_mutex);
616 }
617 EXPORT_SYMBOL_GPL(coresight_disable);
618
619 static ssize_t enable_sink_show(struct device *dev,
620                                 struct device_attribute *attr, char *buf)
621 {
622         struct coresight_device *csdev = to_coresight_device(dev);
623
624         return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
625 }
626
627 static ssize_t enable_sink_store(struct device *dev,
628                                  struct device_attribute *attr,
629                                  const char *buf, size_t size)
630 {
631         int ret;
632         unsigned long val;
633         struct coresight_device *csdev = to_coresight_device(dev);
634
635         ret = kstrtoul(buf, 10, &val);
636         if (ret)
637                 return ret;
638
639         if (val)
640                 csdev->activated = true;
641         else
642                 csdev->activated = false;
643
644         return size;
645
646 }
647 static DEVICE_ATTR_RW(enable_sink);
648
649 static ssize_t enable_source_show(struct device *dev,
650                                   struct device_attribute *attr, char *buf)
651 {
652         struct coresight_device *csdev = to_coresight_device(dev);
653
654         return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
655 }
656
657 static ssize_t enable_source_store(struct device *dev,
658                                    struct device_attribute *attr,
659                                    const char *buf, size_t size)
660 {
661         int ret = 0;
662         unsigned long val;
663         struct coresight_device *csdev = to_coresight_device(dev);
664
665         ret = kstrtoul(buf, 10, &val);
666         if (ret)
667                 return ret;
668
669         if (val) {
670                 ret = coresight_enable(csdev);
671                 if (ret)
672                         return ret;
673         } else {
674                 coresight_disable(csdev);
675         }
676
677         return size;
678 }
679 static DEVICE_ATTR_RW(enable_source);
680
681 static struct attribute *coresight_sink_attrs[] = {
682         &dev_attr_enable_sink.attr,
683         NULL,
684 };
685 ATTRIBUTE_GROUPS(coresight_sink);
686
687 static struct attribute *coresight_source_attrs[] = {
688         &dev_attr_enable_source.attr,
689         NULL,
690 };
691 ATTRIBUTE_GROUPS(coresight_source);
692
693 static struct device_type coresight_dev_type[] = {
694         {
695                 .name = "none",
696         },
697         {
698                 .name = "sink",
699                 .groups = coresight_sink_groups,
700         },
701         {
702                 .name = "link",
703         },
704         {
705                 .name = "linksink",
706                 .groups = coresight_sink_groups,
707         },
708         {
709                 .name = "source",
710                 .groups = coresight_source_groups,
711         },
712 };
713
714 static void coresight_device_release(struct device *dev)
715 {
716         struct coresight_device *csdev = to_coresight_device(dev);
717
718         kfree(csdev->conns);
719         kfree(csdev->refcnt);
720         kfree(csdev);
721 }
722
723 static int coresight_orphan_match(struct device *dev, void *data)
724 {
725         int i;
726         bool still_orphan = false;
727         struct coresight_device *csdev, *i_csdev;
728         struct coresight_connection *conn;
729
730         csdev = data;
731         i_csdev = to_coresight_device(dev);
732
733         /* No need to check oneself */
734         if (csdev == i_csdev)
735                 return 0;
736
737         /* Move on to another component if no connection is orphan */
738         if (!i_csdev->orphan)
739                 return 0;
740         /*
741          * Circle throuch all the connection of that component.  If we find
742          * an orphan connection whose name matches @csdev, link it.
743          */
744         for (i = 0; i < i_csdev->nr_outport; i++) {
745                 conn = &i_csdev->conns[i];
746
747                 /* We have found at least one orphan connection */
748                 if (conn->child_dev == NULL) {
749                         /* Does it match this newly added device? */
750                         if (conn->child_name &&
751                             !strcmp(dev_name(&csdev->dev), conn->child_name)) {
752                                 conn->child_dev = csdev;
753                         } else {
754                                 /* This component still has an orphan */
755                                 still_orphan = true;
756                         }
757                 }
758         }
759
760         i_csdev->orphan = still_orphan;
761
762         /*
763          * Returning '0' ensures that all known component on the
764          * bus will be checked.
765          */
766         return 0;
767 }
768
769 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
770 {
771         /*
772          * No need to check for a return value as orphan connection(s)
773          * are hooked-up with each newly added component.
774          */
775         bus_for_each_dev(&coresight_bustype, NULL,
776                          csdev, coresight_orphan_match);
777 }
778
779
780 static int coresight_name_match(struct device *dev, void *data)
781 {
782         char *to_match;
783         struct coresight_device *i_csdev;
784
785         to_match = data;
786         i_csdev = to_coresight_device(dev);
787
788         if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
789                 return 1;
790
791         return 0;
792 }
793
794 static void coresight_fixup_device_conns(struct coresight_device *csdev)
795 {
796         int i;
797         struct device *dev = NULL;
798         struct coresight_connection *conn;
799
800         for (i = 0; i < csdev->nr_outport; i++) {
801                 conn = &csdev->conns[i];
802                 dev = bus_find_device(&coresight_bustype, NULL,
803                                       (void *)conn->child_name,
804                                       coresight_name_match);
805
806                 if (dev) {
807                         conn->child_dev = to_coresight_device(dev);
808                         /* and put reference from 'bus_find_device()' */
809                         put_device(dev);
810                 } else {
811                         csdev->orphan = true;
812                         conn->child_dev = NULL;
813                 }
814         }
815 }
816
817 static int coresight_remove_match(struct device *dev, void *data)
818 {
819         int i;
820         struct coresight_device *csdev, *iterator;
821         struct coresight_connection *conn;
822
823         csdev = data;
824         iterator = to_coresight_device(dev);
825
826         /* No need to check oneself */
827         if (csdev == iterator)
828                 return 0;
829
830         /*
831          * Circle throuch all the connection of that component.  If we find
832          * a connection whose name matches @csdev, remove it.
833          */
834         for (i = 0; i < iterator->nr_outport; i++) {
835                 conn = &iterator->conns[i];
836
837                 if (conn->child_dev == NULL)
838                         continue;
839
840                 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
841                         iterator->orphan = true;
842                         conn->child_dev = NULL;
843                         /* No need to continue */
844                         break;
845                 }
846         }
847
848         /*
849          * Returning '0' ensures that all known component on the
850          * bus will be checked.
851          */
852         return 0;
853 }
854
855 static void coresight_remove_conns(struct coresight_device *csdev)
856 {
857         bus_for_each_dev(&coresight_bustype, NULL,
858                          csdev, coresight_remove_match);
859 }
860
861 /**
862  * coresight_timeout - loop until a bit has changed to a specific state.
863  * @addr: base address of the area of interest.
864  * @offset: address of a register, starting from @addr.
865  * @position: the position of the bit of interest.
866  * @value: the value the bit should have.
867  *
868  * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
869  * TIMEOUT_US has elapsed, which ever happens first.
870  */
871
872 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
873 {
874         int i;
875         u32 val;
876
877         for (i = TIMEOUT_US; i > 0; i--) {
878                 val = __raw_readl(addr + offset);
879                 /* waiting on the bit to go from 0 to 1 */
880                 if (value) {
881                         if (val & BIT(position))
882                                 return 0;
883                 /* waiting on the bit to go from 1 to 0 */
884                 } else {
885                         if (!(val & BIT(position)))
886                                 return 0;
887                 }
888
889                 /*
890                  * Delay is arbitrary - the specification doesn't say how long
891                  * we are expected to wait.  Extra check required to make sure
892                  * we don't wait needlessly on the last iteration.
893                  */
894                 if (i - 1)
895                         udelay(1);
896         }
897
898         return -EAGAIN;
899 }
900
901 struct bus_type coresight_bustype = {
902         .name   = "coresight",
903 };
904
905 static int __init coresight_init(void)
906 {
907         return bus_register(&coresight_bustype);
908 }
909 postcore_initcall(coresight_init);
910
911 struct coresight_device *coresight_register(struct coresight_desc *desc)
912 {
913         int i;
914         int ret;
915         int link_subtype;
916         int nr_refcnts = 1;
917         atomic_t *refcnts = NULL;
918         struct coresight_device *csdev;
919         struct coresight_connection *conns = NULL;
920
921         csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
922         if (!csdev) {
923                 ret = -ENOMEM;
924                 goto err_kzalloc_csdev;
925         }
926
927         if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
928             desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
929                 link_subtype = desc->subtype.link_subtype;
930
931                 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
932                         nr_refcnts = desc->pdata->nr_inport;
933                 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
934                         nr_refcnts = desc->pdata->nr_outport;
935         }
936
937         refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
938         if (!refcnts) {
939                 ret = -ENOMEM;
940                 goto err_kzalloc_refcnts;
941         }
942
943         csdev->refcnt = refcnts;
944
945         csdev->nr_inport = desc->pdata->nr_inport;
946         csdev->nr_outport = desc->pdata->nr_outport;
947
948         /* Initialise connections if there is at least one outport */
949         if (csdev->nr_outport) {
950                 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
951                 if (!conns) {
952                         ret = -ENOMEM;
953                         goto err_kzalloc_conns;
954                 }
955
956                 for (i = 0; i < csdev->nr_outport; i++) {
957                         conns[i].outport = desc->pdata->outports[i];
958                         conns[i].child_name = desc->pdata->child_names[i];
959                         conns[i].child_port = desc->pdata->child_ports[i];
960                 }
961         }
962
963         csdev->conns = conns;
964
965         csdev->type = desc->type;
966         csdev->subtype = desc->subtype;
967         csdev->ops = desc->ops;
968         csdev->orphan = false;
969
970         csdev->dev.type = &coresight_dev_type[desc->type];
971         csdev->dev.groups = desc->groups;
972         csdev->dev.parent = desc->dev;
973         csdev->dev.release = coresight_device_release;
974         csdev->dev.bus = &coresight_bustype;
975         dev_set_name(&csdev->dev, "%s", desc->pdata->name);
976
977         ret = device_register(&csdev->dev);
978         if (ret)
979                 goto err_device_register;
980
981         mutex_lock(&coresight_mutex);
982
983         coresight_fixup_device_conns(csdev);
984         coresight_fixup_orphan_conns(csdev);
985
986         mutex_unlock(&coresight_mutex);
987
988         return csdev;
989
990 err_device_register:
991         kfree(conns);
992 err_kzalloc_conns:
993         kfree(refcnts);
994 err_kzalloc_refcnts:
995         kfree(csdev);
996 err_kzalloc_csdev:
997         return ERR_PTR(ret);
998 }
999 EXPORT_SYMBOL_GPL(coresight_register);
1000
1001 void coresight_unregister(struct coresight_device *csdev)
1002 {
1003         /* Remove references of that device in the topology */
1004         coresight_remove_conns(csdev);
1005         device_unregister(&csdev->dev);
1006 }
1007 EXPORT_SYMBOL_GPL(coresight_unregister);