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