GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / hwtracing / coresight / coresight.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2012, The Linux Foundation. All rights reserved.
4  */
5
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/types.h>
9 #include <linux/device.h>
10 #include <linux/io.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/clk.h>
16 #include <linux/coresight.h>
17 #include <linux/of_platform.h>
18 #include <linux/delay.h>
19 #include <linux/pm_runtime.h>
20
21 #include "coresight-priv.h"
22
23 static DEFINE_MUTEX(coresight_mutex);
24
25 /**
26  * struct coresight_node - elements of a path, from source to sink
27  * @csdev:      Address of an element.
28  * @link:       hook to the list.
29  */
30 struct coresight_node {
31         struct coresight_device *csdev;
32         struct list_head link;
33 };
34
35 /*
36  * When operating Coresight drivers from the sysFS interface, only a single
37  * path can exist from a tracer (associated to a CPU) to a sink.
38  */
39 static DEFINE_PER_CPU(struct list_head *, tracer_path);
40
41 /*
42  * As of this writing only a single STM can be found in CS topologies.  Since
43  * there is no way to know if we'll ever see more and what kind of
44  * configuration they will enact, for the time being only define a single path
45  * for STM.
46  */
47 static struct list_head *stm_path;
48
49 /*
50  * When losing synchronisation a new barrier packet needs to be inserted at the
51  * beginning of the data collected in a buffer.  That way the decoder knows that
52  * it needs to look for another sync sequence.
53  */
54 const u32 barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
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 /**
262  *  coresight_disable_source - Drop the reference count by 1 and disable
263  *  the device if there are no users left.
264  *
265  *  @csdev - The coresight device to disable
266  *
267  *  Returns true if the device has been disabled.
268  */
269 static bool coresight_disable_source(struct coresight_device *csdev)
270 {
271         if (atomic_dec_return(csdev->refcnt) == 0) {
272                 if (source_ops(csdev)->disable)
273                         source_ops(csdev)->disable(csdev, NULL);
274                 csdev->enable = false;
275         }
276         return !csdev->enable;
277 }
278
279 void coresight_disable_path(struct list_head *path)
280 {
281         u32 type;
282         struct coresight_node *nd;
283         struct coresight_device *csdev, *parent, *child;
284
285         list_for_each_entry(nd, path, link) {
286                 csdev = nd->csdev;
287                 type = csdev->type;
288
289                 /*
290                  * ETF devices are tricky... They can be a link or a sink,
291                  * depending on how they are configured.  If an ETF has been
292                  * "activated" it will be configured as a sink, otherwise
293                  * go ahead with the link configuration.
294                  */
295                 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
296                         type = (csdev == coresight_get_sink(path)) ?
297                                                 CORESIGHT_DEV_TYPE_SINK :
298                                                 CORESIGHT_DEV_TYPE_LINK;
299
300                 switch (type) {
301                 case CORESIGHT_DEV_TYPE_SINK:
302                         coresight_disable_sink(csdev);
303                         break;
304                 case CORESIGHT_DEV_TYPE_SOURCE:
305                         /* sources are disabled from either sysFS or Perf */
306                         break;
307                 case CORESIGHT_DEV_TYPE_LINK:
308                         parent = list_prev_entry(nd, link)->csdev;
309                         child = list_next_entry(nd, link)->csdev;
310                         coresight_disable_link(csdev, parent, child);
311                         break;
312                 default:
313                         break;
314                 }
315         }
316 }
317
318 int coresight_enable_path(struct list_head *path, u32 mode)
319 {
320
321         int ret = 0;
322         u32 type;
323         struct coresight_node *nd;
324         struct coresight_device *csdev, *parent, *child;
325
326         list_for_each_entry_reverse(nd, path, link) {
327                 csdev = nd->csdev;
328                 type = csdev->type;
329
330                 /*
331                  * ETF devices are tricky... They can be a link or a sink,
332                  * depending on how they are configured.  If an ETF has been
333                  * "activated" it will be configured as a sink, otherwise
334                  * go ahead with the link configuration.
335                  */
336                 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
337                         type = (csdev == coresight_get_sink(path)) ?
338                                                 CORESIGHT_DEV_TYPE_SINK :
339                                                 CORESIGHT_DEV_TYPE_LINK;
340
341                 switch (type) {
342                 case CORESIGHT_DEV_TYPE_SINK:
343                         ret = coresight_enable_sink(csdev, mode);
344                         /*
345                          * Sink is the first component turned on. If we
346                          * failed to enable the sink, there are no components
347                          * that need disabling. Disabling the path here
348                          * would mean we could disrupt an existing session.
349                          */
350                         if (ret)
351                                 goto out;
352                         break;
353                 case CORESIGHT_DEV_TYPE_SOURCE:
354                         /* sources are enabled from either sysFS or Perf */
355                         break;
356                 case CORESIGHT_DEV_TYPE_LINK:
357                         parent = list_prev_entry(nd, link)->csdev;
358                         child = list_next_entry(nd, link)->csdev;
359                         ret = coresight_enable_link(csdev, parent, child);
360                         if (ret)
361                                 goto err;
362                         break;
363                 default:
364                         goto err;
365                 }
366         }
367
368 out:
369         return ret;
370 err:
371         coresight_disable_path(path);
372         goto out;
373 }
374
375 struct coresight_device *coresight_get_sink(struct list_head *path)
376 {
377         struct coresight_device *csdev;
378
379         if (!path)
380                 return NULL;
381
382         csdev = list_last_entry(path, struct coresight_node, link)->csdev;
383         if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
384             csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
385                 return NULL;
386
387         return csdev;
388 }
389
390 static int coresight_enabled_sink(struct device *dev, void *data)
391 {
392         bool *reset = data;
393         struct coresight_device *csdev = to_coresight_device(dev);
394
395         if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
396              csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
397              csdev->activated) {
398                 /*
399                  * Now that we have a handle on the sink for this session,
400                  * disable the sysFS "enable_sink" flag so that possible
401                  * concurrent perf session that wish to use another sink don't
402                  * trip on it.  Doing so has no ramification for the current
403                  * session.
404                  */
405                 if (*reset)
406                         csdev->activated = false;
407
408                 return 1;
409         }
410
411         return 0;
412 }
413
414 /**
415  * coresight_get_enabled_sink - returns the first enabled sink found on the bus
416  * @deactivate: Whether the 'enable_sink' flag should be reset
417  *
418  * When operated from perf the deactivate parameter should be set to 'true'.
419  * That way the "enabled_sink" flag of the sink that was selected can be reset,
420  * allowing for other concurrent perf sessions to choose a different sink.
421  *
422  * When operated from sysFS users have full control and as such the deactivate
423  * parameter should be set to 'false', hence mandating users to explicitly
424  * clear the flag.
425  */
426 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
427 {
428         struct device *dev = NULL;
429
430         dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
431                               coresight_enabled_sink);
432
433         return dev ? to_coresight_device(dev) : NULL;
434 }
435
436 /*
437  * coresight_grab_device - Power up this device and any of the helper
438  * devices connected to it for trace operation. Since the helper devices
439  * don't appear on the trace path, they should be handled along with the
440  * the master device.
441  */
442 static void coresight_grab_device(struct coresight_device *csdev)
443 {
444         int i;
445
446         for (i = 0; i < csdev->nr_outport; i++) {
447                 struct coresight_device *child = csdev->conns[i].child_dev;
448
449                 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
450                         pm_runtime_get_sync(child->dev.parent);
451         }
452         pm_runtime_get_sync(csdev->dev.parent);
453 }
454
455 /*
456  * coresight_drop_device - Release this device and any of the helper
457  * devices connected to it.
458  */
459 static void coresight_drop_device(struct coresight_device *csdev)
460 {
461         int i;
462
463         pm_runtime_put(csdev->dev.parent);
464         for (i = 0; i < csdev->nr_outport; i++) {
465                 struct coresight_device *child = csdev->conns[i].child_dev;
466
467                 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
468                         pm_runtime_put(child->dev.parent);
469         }
470 }
471
472 /**
473  * _coresight_build_path - recursively build a path from a @csdev to a sink.
474  * @csdev:      The device to start from.
475  * @path:       The list to add devices to.
476  *
477  * The tree of Coresight device is traversed until an activated sink is
478  * found.  From there the sink is added to the list along with all the
479  * devices that led to that point - the end result is a list from source
480  * to sink. In that list the source is the first device and the sink the
481  * last one.
482  */
483 static int _coresight_build_path(struct coresight_device *csdev,
484                                  struct coresight_device *sink,
485                                  struct list_head *path)
486 {
487         int i;
488         bool found = false;
489         struct coresight_node *node;
490
491         /* An activated sink has been found.  Enqueue the element */
492         if (csdev == sink)
493                 goto out;
494
495         /* Not a sink - recursively explore each port found on this element */
496         for (i = 0; i < csdev->nr_outport; i++) {
497                 struct coresight_device *child_dev = csdev->conns[i].child_dev;
498
499                 if (child_dev &&
500                     _coresight_build_path(child_dev, sink, path) == 0) {
501                         found = true;
502                         break;
503                 }
504         }
505
506         if (!found)
507                 return -ENODEV;
508
509 out:
510         /*
511          * A path from this element to a sink has been found.  The elements
512          * leading to the sink are already enqueued, all that is left to do
513          * is tell the PM runtime core we need this element and add a node
514          * for it.
515          */
516         node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
517         if (!node)
518                 return -ENOMEM;
519
520         coresight_grab_device(csdev);
521         node->csdev = csdev;
522         list_add(&node->link, path);
523
524         return 0;
525 }
526
527 struct list_head *coresight_build_path(struct coresight_device *source,
528                                        struct coresight_device *sink)
529 {
530         struct list_head *path;
531         int rc;
532
533         if (!sink)
534                 return ERR_PTR(-EINVAL);
535
536         path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
537         if (!path)
538                 return ERR_PTR(-ENOMEM);
539
540         INIT_LIST_HEAD(path);
541
542         rc = _coresight_build_path(source, sink, path);
543         if (rc) {
544                 kfree(path);
545                 return ERR_PTR(rc);
546         }
547
548         return path;
549 }
550
551 /**
552  * coresight_release_path - release a previously built path.
553  * @path:       the path to release.
554  *
555  * Go through all the elements of a path and 1) removed it from the list and
556  * 2) free the memory allocated for each node.
557  */
558 void coresight_release_path(struct list_head *path)
559 {
560         struct coresight_device *csdev;
561         struct coresight_node *nd, *next;
562
563         list_for_each_entry_safe(nd, next, path, link) {
564                 csdev = nd->csdev;
565
566                 coresight_drop_device(csdev);
567                 list_del(&nd->link);
568                 kfree(nd);
569         }
570
571         kfree(path);
572         path = NULL;
573 }
574
575 /** coresight_validate_source - make sure a source has the right credentials
576  *  @csdev:     the device structure for a source.
577  *  @function:  the function this was called from.
578  *
579  * Assumes the coresight_mutex is held.
580  */
581 static int coresight_validate_source(struct coresight_device *csdev,
582                                      const char *function)
583 {
584         u32 type, subtype;
585
586         type = csdev->type;
587         subtype = csdev->subtype.source_subtype;
588
589         if (type != CORESIGHT_DEV_TYPE_SOURCE) {
590                 dev_err(&csdev->dev, "wrong device type in %s\n", function);
591                 return -EINVAL;
592         }
593
594         if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
595             subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
596                 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
597                 return -EINVAL;
598         }
599
600         return 0;
601 }
602
603 int coresight_enable(struct coresight_device *csdev)
604 {
605         int cpu, ret = 0;
606         struct coresight_device *sink;
607         struct list_head *path;
608         enum coresight_dev_subtype_source subtype;
609
610         subtype = csdev->subtype.source_subtype;
611
612         mutex_lock(&coresight_mutex);
613
614         ret = coresight_validate_source(csdev, __func__);
615         if (ret)
616                 goto out;
617
618         if (csdev->enable) {
619                 /*
620                  * There could be multiple applications driving the software
621                  * source. So keep the refcount for each such user when the
622                  * source is already enabled.
623                  */
624                 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
625                         atomic_inc(csdev->refcnt);
626                 goto out;
627         }
628
629         /*
630          * Search for a valid sink for this session but don't reset the
631          * "enable_sink" flag in sysFS.  Users get to do that explicitly.
632          */
633         sink = coresight_get_enabled_sink(false);
634         if (!sink) {
635                 ret = -EINVAL;
636                 goto out;
637         }
638
639         path = coresight_build_path(csdev, sink);
640         if (IS_ERR(path)) {
641                 pr_err("building path(s) failed\n");
642                 ret = PTR_ERR(path);
643                 goto out;
644         }
645
646         ret = coresight_enable_path(path, CS_MODE_SYSFS);
647         if (ret)
648                 goto err_path;
649
650         ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
651         if (ret)
652                 goto err_source;
653
654         switch (subtype) {
655         case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
656                 /*
657                  * When working from sysFS it is important to keep track
658                  * of the paths that were created so that they can be
659                  * undone in 'coresight_disable()'.  Since there can only
660                  * be a single session per tracer (when working from sysFS)
661                  * a per-cpu variable will do just fine.
662                  */
663                 cpu = source_ops(csdev)->cpu_id(csdev);
664                 per_cpu(tracer_path, cpu) = path;
665                 break;
666         case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
667                 stm_path = path;
668                 break;
669         default:
670                 /* We can't be here */
671                 break;
672         }
673
674 out:
675         mutex_unlock(&coresight_mutex);
676         return ret;
677
678 err_source:
679         coresight_disable_path(path);
680
681 err_path:
682         coresight_release_path(path);
683         goto out;
684 }
685 EXPORT_SYMBOL_GPL(coresight_enable);
686
687 void coresight_disable(struct coresight_device *csdev)
688 {
689         int cpu, ret;
690         struct list_head *path = NULL;
691
692         mutex_lock(&coresight_mutex);
693
694         ret = coresight_validate_source(csdev, __func__);
695         if (ret)
696                 goto out;
697
698         if (!csdev->enable || !coresight_disable_source(csdev))
699                 goto out;
700
701         switch (csdev->subtype.source_subtype) {
702         case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
703                 cpu = source_ops(csdev)->cpu_id(csdev);
704                 path = per_cpu(tracer_path, cpu);
705                 per_cpu(tracer_path, cpu) = NULL;
706                 break;
707         case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
708                 path = stm_path;
709                 stm_path = NULL;
710                 break;
711         default:
712                 /* We can't be here */
713                 break;
714         }
715
716         coresight_disable_path(path);
717         coresight_release_path(path);
718
719 out:
720         mutex_unlock(&coresight_mutex);
721 }
722 EXPORT_SYMBOL_GPL(coresight_disable);
723
724 static ssize_t enable_sink_show(struct device *dev,
725                                 struct device_attribute *attr, char *buf)
726 {
727         struct coresight_device *csdev = to_coresight_device(dev);
728
729         return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
730 }
731
732 static ssize_t enable_sink_store(struct device *dev,
733                                  struct device_attribute *attr,
734                                  const char *buf, size_t size)
735 {
736         int ret;
737         unsigned long val;
738         struct coresight_device *csdev = to_coresight_device(dev);
739
740         ret = kstrtoul(buf, 10, &val);
741         if (ret)
742                 return ret;
743
744         if (val)
745                 csdev->activated = true;
746         else
747                 csdev->activated = false;
748
749         return size;
750
751 }
752 static DEVICE_ATTR_RW(enable_sink);
753
754 static ssize_t enable_source_show(struct device *dev,
755                                   struct device_attribute *attr, char *buf)
756 {
757         struct coresight_device *csdev = to_coresight_device(dev);
758
759         return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
760 }
761
762 static ssize_t enable_source_store(struct device *dev,
763                                    struct device_attribute *attr,
764                                    const char *buf, size_t size)
765 {
766         int ret = 0;
767         unsigned long val;
768         struct coresight_device *csdev = to_coresight_device(dev);
769
770         ret = kstrtoul(buf, 10, &val);
771         if (ret)
772                 return ret;
773
774         if (val) {
775                 ret = coresight_enable(csdev);
776                 if (ret)
777                         return ret;
778         } else {
779                 coresight_disable(csdev);
780         }
781
782         return size;
783 }
784 static DEVICE_ATTR_RW(enable_source);
785
786 static struct attribute *coresight_sink_attrs[] = {
787         &dev_attr_enable_sink.attr,
788         NULL,
789 };
790 ATTRIBUTE_GROUPS(coresight_sink);
791
792 static struct attribute *coresight_source_attrs[] = {
793         &dev_attr_enable_source.attr,
794         NULL,
795 };
796 ATTRIBUTE_GROUPS(coresight_source);
797
798 static struct device_type coresight_dev_type[] = {
799         {
800                 .name = "none",
801         },
802         {
803                 .name = "sink",
804                 .groups = coresight_sink_groups,
805         },
806         {
807                 .name = "link",
808         },
809         {
810                 .name = "linksink",
811                 .groups = coresight_sink_groups,
812         },
813         {
814                 .name = "source",
815                 .groups = coresight_source_groups,
816         },
817         {
818                 .name = "helper",
819         },
820 };
821
822 static void coresight_device_release(struct device *dev)
823 {
824         struct coresight_device *csdev = to_coresight_device(dev);
825
826         kfree(csdev->conns);
827         kfree(csdev->refcnt);
828         kfree(csdev);
829 }
830
831 static int coresight_orphan_match(struct device *dev, void *data)
832 {
833         int i;
834         bool still_orphan = false;
835         struct coresight_device *csdev, *i_csdev;
836         struct coresight_connection *conn;
837
838         csdev = data;
839         i_csdev = to_coresight_device(dev);
840
841         /* No need to check oneself */
842         if (csdev == i_csdev)
843                 return 0;
844
845         /* Move on to another component if no connection is orphan */
846         if (!i_csdev->orphan)
847                 return 0;
848         /*
849          * Circle throuch all the connection of that component.  If we find
850          * an orphan connection whose name matches @csdev, link it.
851          */
852         for (i = 0; i < i_csdev->nr_outport; i++) {
853                 conn = &i_csdev->conns[i];
854
855                 /* We have found at least one orphan connection */
856                 if (conn->child_dev == NULL) {
857                         /* Does it match this newly added device? */
858                         if (conn->child_name &&
859                             !strcmp(dev_name(&csdev->dev), conn->child_name)) {
860                                 conn->child_dev = csdev;
861                         } else {
862                                 /* This component still has an orphan */
863                                 still_orphan = true;
864                         }
865                 }
866         }
867
868         i_csdev->orphan = still_orphan;
869
870         /*
871          * Returning '0' ensures that all known component on the
872          * bus will be checked.
873          */
874         return 0;
875 }
876
877 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
878 {
879         /*
880          * No need to check for a return value as orphan connection(s)
881          * are hooked-up with each newly added component.
882          */
883         bus_for_each_dev(&coresight_bustype, NULL,
884                          csdev, coresight_orphan_match);
885 }
886
887
888 static void coresight_fixup_device_conns(struct coresight_device *csdev)
889 {
890         int i;
891
892         for (i = 0; i < csdev->nr_outport; i++) {
893                 struct coresight_connection *conn = &csdev->conns[i];
894                 struct device *dev = NULL;
895
896                 if (conn->child_name)
897                         dev = bus_find_device_by_name(&coresight_bustype, NULL,
898                                                       conn->child_name);
899                 if (dev) {
900                         conn->child_dev = to_coresight_device(dev);
901                         /* and put reference from 'bus_find_device()' */
902                         put_device(dev);
903                 } else {
904                         csdev->orphan = true;
905                         conn->child_dev = NULL;
906                 }
907         }
908 }
909
910 static int coresight_remove_match(struct device *dev, void *data)
911 {
912         int i;
913         struct coresight_device *csdev, *iterator;
914         struct coresight_connection *conn;
915
916         csdev = data;
917         iterator = to_coresight_device(dev);
918
919         /* No need to check oneself */
920         if (csdev == iterator)
921                 return 0;
922
923         /*
924          * Circle throuch all the connection of that component.  If we find
925          * a connection whose name matches @csdev, remove it.
926          */
927         for (i = 0; i < iterator->nr_outport; i++) {
928                 conn = &iterator->conns[i];
929
930                 if (conn->child_dev == NULL)
931                         continue;
932
933                 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
934                         iterator->orphan = true;
935                         conn->child_dev = NULL;
936                         /* No need to continue */
937                         break;
938                 }
939         }
940
941         /*
942          * Returning '0' ensures that all known component on the
943          * bus will be checked.
944          */
945         return 0;
946 }
947
948 static void coresight_remove_conns(struct coresight_device *csdev)
949 {
950         bus_for_each_dev(&coresight_bustype, NULL,
951                          csdev, coresight_remove_match);
952 }
953
954 /**
955  * coresight_timeout - loop until a bit has changed to a specific state.
956  * @addr: base address of the area of interest.
957  * @offset: address of a register, starting from @addr.
958  * @position: the position of the bit of interest.
959  * @value: the value the bit should have.
960  *
961  * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
962  * TIMEOUT_US has elapsed, which ever happens first.
963  */
964
965 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
966 {
967         int i;
968         u32 val;
969
970         for (i = TIMEOUT_US; i > 0; i--) {
971                 val = __raw_readl(addr + offset);
972                 /* waiting on the bit to go from 0 to 1 */
973                 if (value) {
974                         if (val & BIT(position))
975                                 return 0;
976                 /* waiting on the bit to go from 1 to 0 */
977                 } else {
978                         if (!(val & BIT(position)))
979                                 return 0;
980                 }
981
982                 /*
983                  * Delay is arbitrary - the specification doesn't say how long
984                  * we are expected to wait.  Extra check required to make sure
985                  * we don't wait needlessly on the last iteration.
986                  */
987                 if (i - 1)
988                         udelay(1);
989         }
990
991         return -EAGAIN;
992 }
993
994 struct bus_type coresight_bustype = {
995         .name   = "coresight",
996 };
997
998 static int __init coresight_init(void)
999 {
1000         return bus_register(&coresight_bustype);
1001 }
1002 postcore_initcall(coresight_init);
1003
1004 struct coresight_device *coresight_register(struct coresight_desc *desc)
1005 {
1006         int i;
1007         int ret;
1008         int link_subtype;
1009         int nr_refcnts = 1;
1010         atomic_t *refcnts = NULL;
1011         struct coresight_device *csdev;
1012         struct coresight_connection *conns = NULL;
1013
1014         csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1015         if (!csdev) {
1016                 ret = -ENOMEM;
1017                 goto err_kzalloc_csdev;
1018         }
1019
1020         if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1021             desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1022                 link_subtype = desc->subtype.link_subtype;
1023
1024                 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1025                         nr_refcnts = desc->pdata->nr_inport;
1026                 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1027                         nr_refcnts = desc->pdata->nr_outport;
1028         }
1029
1030         refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1031         if (!refcnts) {
1032                 ret = -ENOMEM;
1033                 goto err_kzalloc_refcnts;
1034         }
1035
1036         csdev->refcnt = refcnts;
1037
1038         csdev->nr_inport = desc->pdata->nr_inport;
1039         csdev->nr_outport = desc->pdata->nr_outport;
1040
1041         /* Initialise connections if there is at least one outport */
1042         if (csdev->nr_outport) {
1043                 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
1044                 if (!conns) {
1045                         ret = -ENOMEM;
1046                         goto err_kzalloc_conns;
1047                 }
1048
1049                 for (i = 0; i < csdev->nr_outport; i++) {
1050                         conns[i].outport = desc->pdata->outports[i];
1051                         conns[i].child_name = desc->pdata->child_names[i];
1052                         conns[i].child_port = desc->pdata->child_ports[i];
1053                 }
1054         }
1055
1056         csdev->conns = conns;
1057
1058         csdev->type = desc->type;
1059         csdev->subtype = desc->subtype;
1060         csdev->ops = desc->ops;
1061         csdev->orphan = false;
1062
1063         csdev->dev.type = &coresight_dev_type[desc->type];
1064         csdev->dev.groups = desc->groups;
1065         csdev->dev.parent = desc->dev;
1066         csdev->dev.release = coresight_device_release;
1067         csdev->dev.bus = &coresight_bustype;
1068         dev_set_name(&csdev->dev, "%s", desc->pdata->name);
1069
1070         ret = device_register(&csdev->dev);
1071         if (ret) {
1072                 put_device(&csdev->dev);
1073                 goto err_kzalloc_csdev;
1074         }
1075
1076         mutex_lock(&coresight_mutex);
1077
1078         coresight_fixup_device_conns(csdev);
1079         coresight_fixup_orphan_conns(csdev);
1080
1081         mutex_unlock(&coresight_mutex);
1082
1083         return csdev;
1084
1085 err_kzalloc_conns:
1086         kfree(refcnts);
1087 err_kzalloc_refcnts:
1088         kfree(csdev);
1089 err_kzalloc_csdev:
1090         return ERR_PTR(ret);
1091 }
1092 EXPORT_SYMBOL_GPL(coresight_register);
1093
1094 void coresight_unregister(struct coresight_device *csdev)
1095 {
1096         /* Remove references of that device in the topology */
1097         coresight_remove_conns(csdev);
1098         device_unregister(&csdev->dev);
1099 }
1100 EXPORT_SYMBOL_GPL(coresight_unregister);