coresight: add API to get sink from path
[firefly-linux-kernel-4.4.55.git] / drivers / hwtracing / coresight / coresight.c
1 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 and
5  * only version 2 as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/device.h>
18 #include <linux/io.h>
19 #include <linux/err.h>
20 #include <linux/export.h>
21 #include <linux/slab.h>
22 #include <linux/mutex.h>
23 #include <linux/clk.h>
24 #include <linux/coresight.h>
25 #include <linux/of_platform.h>
26 #include <linux/delay.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 *, sysfs_path);
47
48 static int coresight_id_match(struct device *dev, void *data)
49 {
50         int trace_id, i_trace_id;
51         struct coresight_device *csdev, *i_csdev;
52
53         csdev = data;
54         i_csdev = to_coresight_device(dev);
55
56         /*
57          * No need to care about oneself and components that are not
58          * sources or not enabled
59          */
60         if (i_csdev == csdev || !i_csdev->enable ||
61             i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
62                 return 0;
63
64         /* Get the source ID for both compoment */
65         trace_id = source_ops(csdev)->trace_id(csdev);
66         i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
67
68         /* All you need is one */
69         if (trace_id == i_trace_id)
70                 return 1;
71
72         return 0;
73 }
74
75 static int coresight_source_is_unique(struct coresight_device *csdev)
76 {
77         int trace_id = source_ops(csdev)->trace_id(csdev);
78
79         /* this shouldn't happen */
80         if (trace_id < 0)
81                 return 0;
82
83         return !bus_for_each_dev(&coresight_bustype, NULL,
84                                  csdev, coresight_id_match);
85 }
86
87 static int coresight_find_link_inport(struct coresight_device *csdev,
88                                       struct coresight_device *parent)
89 {
90         int i;
91         struct coresight_connection *conn;
92
93         for (i = 0; i < parent->nr_outport; i++) {
94                 conn = &parent->conns[i];
95                 if (conn->child_dev == csdev)
96                         return conn->child_port;
97         }
98
99         dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
100                 dev_name(&parent->dev), dev_name(&csdev->dev));
101
102         return 0;
103 }
104
105 static int coresight_find_link_outport(struct coresight_device *csdev,
106                                        struct coresight_device *child)
107 {
108         int i;
109         struct coresight_connection *conn;
110
111         for (i = 0; i < csdev->nr_outport; i++) {
112                 conn = &csdev->conns[i];
113                 if (conn->child_dev == child)
114                         return conn->outport;
115         }
116
117         dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
118                 dev_name(&csdev->dev), dev_name(&child->dev));
119
120         return 0;
121 }
122
123 static int coresight_enable_sink(struct coresight_device *csdev)
124 {
125         int ret;
126
127         if (!csdev->enable) {
128                 if (sink_ops(csdev)->enable) {
129                         ret = sink_ops(csdev)->enable(csdev);
130                         if (ret)
131                                 return ret;
132                 }
133                 csdev->enable = true;
134         }
135
136         atomic_inc(csdev->refcnt);
137
138         return 0;
139 }
140
141 static void coresight_disable_sink(struct coresight_device *csdev)
142 {
143         if (atomic_dec_return(csdev->refcnt) == 0) {
144                 if (sink_ops(csdev)->disable) {
145                         sink_ops(csdev)->disable(csdev);
146                         csdev->enable = false;
147                 }
148         }
149 }
150
151 static int coresight_enable_link(struct coresight_device *csdev,
152                                  struct coresight_device *parent,
153                                  struct coresight_device *child)
154 {
155         int ret;
156         int link_subtype;
157         int refport, inport, outport;
158
159         if (!parent || !child)
160                 return -EINVAL;
161
162         inport = coresight_find_link_inport(csdev, parent);
163         outport = coresight_find_link_outport(csdev, child);
164         link_subtype = csdev->subtype.link_subtype;
165
166         if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
167                 refport = inport;
168         else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
169                 refport = outport;
170         else
171                 refport = 0;
172
173         if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
174                 if (link_ops(csdev)->enable) {
175                         ret = link_ops(csdev)->enable(csdev, inport, outport);
176                         if (ret)
177                                 return ret;
178                 }
179         }
180
181         csdev->enable = true;
182
183         return 0;
184 }
185
186 static void coresight_disable_link(struct coresight_device *csdev,
187                                    struct coresight_device *parent,
188                                    struct coresight_device *child)
189 {
190         int i, nr_conns;
191         int link_subtype;
192         int refport, inport, outport;
193
194         if (!parent || !child)
195                 return;
196
197         inport = coresight_find_link_inport(csdev, parent);
198         outport = coresight_find_link_outport(csdev, child);
199         link_subtype = csdev->subtype.link_subtype;
200
201         if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
202                 refport = inport;
203                 nr_conns = csdev->nr_inport;
204         } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
205                 refport = outport;
206                 nr_conns = csdev->nr_outport;
207         } else {
208                 refport = 0;
209                 nr_conns = 1;
210         }
211
212         if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
213                 if (link_ops(csdev)->disable)
214                         link_ops(csdev)->disable(csdev, inport, outport);
215         }
216
217         for (i = 0; i < nr_conns; i++)
218                 if (atomic_read(&csdev->refcnt[i]) != 0)
219                         return;
220
221         csdev->enable = false;
222 }
223
224 static int coresight_enable_source(struct coresight_device *csdev)
225 {
226         int ret;
227
228         if (!coresight_source_is_unique(csdev)) {
229                 dev_warn(&csdev->dev, "traceID %d not unique\n",
230                          source_ops(csdev)->trace_id(csdev));
231                 return -EINVAL;
232         }
233
234         if (!csdev->enable) {
235                 if (source_ops(csdev)->enable) {
236                         ret = source_ops(csdev)->enable(csdev);
237                         if (ret)
238                                 return ret;
239                 }
240                 csdev->enable = true;
241         }
242
243         atomic_inc(csdev->refcnt);
244
245         return 0;
246 }
247
248 static void coresight_disable_source(struct coresight_device *csdev)
249 {
250         if (atomic_dec_return(csdev->refcnt) == 0) {
251                 if (source_ops(csdev)->disable) {
252                         source_ops(csdev)->disable(csdev);
253                         csdev->enable = false;
254                 }
255         }
256 }
257
258 void coresight_disable_path(struct list_head *path)
259 {
260         struct coresight_node *nd;
261         struct coresight_device *csdev, *parent, *child;
262
263         list_for_each_entry(nd, path, link) {
264                 csdev = nd->csdev;
265
266                 switch (csdev->type) {
267                 case CORESIGHT_DEV_TYPE_SINK:
268                 case CORESIGHT_DEV_TYPE_LINKSINK:
269                         coresight_disable_sink(csdev);
270                         break;
271                 case CORESIGHT_DEV_TYPE_SOURCE:
272                         /* sources are disabled from either sysFS or Perf */
273                         break;
274                 case CORESIGHT_DEV_TYPE_LINK:
275                         parent = list_prev_entry(nd, link)->csdev;
276                         child = list_next_entry(nd, link)->csdev;
277                         coresight_disable_link(csdev, parent, child);
278                         break;
279                 default:
280                         break;
281                 }
282         }
283 }
284
285 int coresight_enable_path(struct list_head *path)
286 {
287
288         int ret = 0;
289         struct coresight_node *nd;
290         struct coresight_device *csdev, *parent, *child;
291
292         list_for_each_entry_reverse(nd, path, link) {
293                 csdev = nd->csdev;
294
295                 switch (csdev->type) {
296                 case CORESIGHT_DEV_TYPE_SINK:
297                 case CORESIGHT_DEV_TYPE_LINKSINK:
298                         ret = coresight_enable_sink(csdev);
299                         if (ret)
300                                 goto err;
301                         break;
302                 case CORESIGHT_DEV_TYPE_SOURCE:
303                         /* sources are enabled from either sysFS or Perf */
304                         break;
305                 case CORESIGHT_DEV_TYPE_LINK:
306                         parent = list_prev_entry(nd, link)->csdev;
307                         child = list_next_entry(nd, link)->csdev;
308                         ret = coresight_enable_link(csdev, parent, child);
309                         if (ret)
310                                 goto err;
311                         break;
312                 default:
313                         goto err;
314                 }
315         }
316
317 out:
318         return ret;
319 err:
320         coresight_disable_path(path);
321         goto out;
322 }
323
324 struct coresight_device *coresight_get_sink(struct list_head *path)
325 {
326         struct coresight_device *csdev;
327
328         if (!path)
329                 return NULL;
330
331         csdev = list_last_entry(path, struct coresight_node, link)->csdev;
332         if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
333             csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
334                 return NULL;
335
336         return csdev;
337 }
338
339 /**
340  * _coresight_build_path - recursively build a path from a @csdev to a sink.
341  * @csdev:      The device to start from.
342  * @path:       The list to add devices to.
343  *
344  * The tree of Coresight device is traversed until an activated sink is
345  * found.  From there the sink is added to the list along with all the
346  * devices that led to that point - the end result is a list from source
347  * to sink. In that list the source is the first device and the sink the
348  * last one.
349  */
350 static int _coresight_build_path(struct coresight_device *csdev,
351                                  struct list_head *path)
352 {
353         int i;
354         bool found = false;
355         struct coresight_node *node;
356         struct coresight_connection *conn;
357
358         /* An activated sink has been found.  Enqueue the element */
359         if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
360              csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) && csdev->activated)
361                 goto out;
362
363         /* Not a sink - recursively explore each port found on this element */
364         for (i = 0; i < csdev->nr_outport; i++) {
365                 conn = &csdev->conns[i];
366                 if (_coresight_build_path(conn->child_dev, path) == 0) {
367                         found = true;
368                         break;
369                 }
370         }
371
372         if (!found)
373                 return -ENODEV;
374
375 out:
376         /*
377          * A path from this element to a sink has been found.  The elements
378          * leading to the sink are already enqueued, all that is left to do
379          * is add a node for this element.
380          */
381         node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
382         if (!node)
383                 return -ENOMEM;
384
385         node->csdev = csdev;
386         list_add(&node->link, path);
387
388         return 0;
389 }
390
391 struct list_head *coresight_build_path(struct coresight_device *csdev)
392 {
393         struct list_head *path;
394
395         path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
396         if (!path)
397                 return NULL;
398
399         INIT_LIST_HEAD(path);
400
401         if (_coresight_build_path(csdev, path)) {
402                 kfree(path);
403                 path = NULL;
404         }
405
406         return path;
407 }
408
409 /**
410  * coresight_release_path - release a previously built path.
411  * @path:       the path to release.
412  *
413  * Go through all the elements of a path and 1) removed it from the list and
414  * 2) free the memory allocated for each node.
415  */
416 void coresight_release_path(struct list_head *path)
417 {
418         struct coresight_node *nd, *next;
419
420         list_for_each_entry_safe(nd, next, path, link) {
421                 list_del(&nd->link);
422                 kfree(nd);
423         }
424
425         kfree(path);
426         path = NULL;
427 }
428
429 int coresight_enable(struct coresight_device *csdev)
430 {
431         int ret = 0;
432         int cpu;
433         struct list_head *path;
434
435         mutex_lock(&coresight_mutex);
436         if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
437                 ret = -EINVAL;
438                 dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
439                 goto out;
440         }
441         if (csdev->enable)
442                 goto out;
443
444         path = coresight_build_path(csdev);
445         if (!path) {
446                 pr_err("building path(s) failed\n");
447                 goto out;
448         }
449
450         ret = coresight_enable_path(path);
451         if (ret)
452                 goto err_path;
453
454         ret = coresight_enable_source(csdev);
455         if (ret)
456                 goto err_source;
457
458         /*
459          * When working from sysFS it is important to keep track
460          * of the paths that were created so that they can be
461          * undone in 'coresight_disable()'.  Since there can only
462          * be a single session per tracer (when working from sysFS)
463          * a per-cpu variable will do just fine.
464          */
465         cpu = source_ops(csdev)->cpu_id(csdev);
466         per_cpu(sysfs_path, cpu) = path;
467
468 out:
469         mutex_unlock(&coresight_mutex);
470         return ret;
471
472 err_source:
473         coresight_disable_path(path);
474
475 err_path:
476         coresight_release_path(path);
477         goto out;
478 }
479 EXPORT_SYMBOL_GPL(coresight_enable);
480
481 void coresight_disable(struct coresight_device *csdev)
482 {
483         int cpu;
484         struct list_head *path;
485
486         mutex_lock(&coresight_mutex);
487         if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
488                 dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
489                 goto out;
490         }
491         if (!csdev->enable)
492                 goto out;
493
494         cpu = source_ops(csdev)->cpu_id(csdev);
495         path = per_cpu(sysfs_path, cpu);
496         coresight_disable_source(csdev);
497         coresight_disable_path(path);
498         coresight_release_path(path);
499         per_cpu(sysfs_path, cpu) = NULL;
500
501 out:
502         mutex_unlock(&coresight_mutex);
503 }
504 EXPORT_SYMBOL_GPL(coresight_disable);
505
506 static ssize_t enable_sink_show(struct device *dev,
507                                 struct device_attribute *attr, char *buf)
508 {
509         struct coresight_device *csdev = to_coresight_device(dev);
510
511         return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->activated);
512 }
513
514 static ssize_t enable_sink_store(struct device *dev,
515                                  struct device_attribute *attr,
516                                  const char *buf, size_t size)
517 {
518         int ret;
519         unsigned long val;
520         struct coresight_device *csdev = to_coresight_device(dev);
521
522         ret = kstrtoul(buf, 10, &val);
523         if (ret)
524                 return ret;
525
526         if (val)
527                 csdev->activated = true;
528         else
529                 csdev->activated = false;
530
531         return size;
532
533 }
534 static DEVICE_ATTR_RW(enable_sink);
535
536 static ssize_t enable_source_show(struct device *dev,
537                                   struct device_attribute *attr, char *buf)
538 {
539         struct coresight_device *csdev = to_coresight_device(dev);
540
541         return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->enable);
542 }
543
544 static ssize_t enable_source_store(struct device *dev,
545                                    struct device_attribute *attr,
546                                    const char *buf, size_t size)
547 {
548         int ret = 0;
549         unsigned long val;
550         struct coresight_device *csdev = to_coresight_device(dev);
551
552         ret = kstrtoul(buf, 10, &val);
553         if (ret)
554                 return ret;
555
556         if (val) {
557                 ret = coresight_enable(csdev);
558                 if (ret)
559                         return ret;
560         } else {
561                 coresight_disable(csdev);
562         }
563
564         return size;
565 }
566 static DEVICE_ATTR_RW(enable_source);
567
568 static struct attribute *coresight_sink_attrs[] = {
569         &dev_attr_enable_sink.attr,
570         NULL,
571 };
572 ATTRIBUTE_GROUPS(coresight_sink);
573
574 static struct attribute *coresight_source_attrs[] = {
575         &dev_attr_enable_source.attr,
576         NULL,
577 };
578 ATTRIBUTE_GROUPS(coresight_source);
579
580 static struct device_type coresight_dev_type[] = {
581         {
582                 .name = "none",
583         },
584         {
585                 .name = "sink",
586                 .groups = coresight_sink_groups,
587         },
588         {
589                 .name = "link",
590         },
591         {
592                 .name = "linksink",
593                 .groups = coresight_sink_groups,
594         },
595         {
596                 .name = "source",
597                 .groups = coresight_source_groups,
598         },
599 };
600
601 static void coresight_device_release(struct device *dev)
602 {
603         struct coresight_device *csdev = to_coresight_device(dev);
604
605         kfree(csdev->conns);
606         kfree(csdev->refcnt);
607         kfree(csdev);
608 }
609
610 static int coresight_orphan_match(struct device *dev, void *data)
611 {
612         int i;
613         bool still_orphan = false;
614         struct coresight_device *csdev, *i_csdev;
615         struct coresight_connection *conn;
616
617         csdev = data;
618         i_csdev = to_coresight_device(dev);
619
620         /* No need to check oneself */
621         if (csdev == i_csdev)
622                 return 0;
623
624         /* Move on to another component if no connection is orphan */
625         if (!i_csdev->orphan)
626                 return 0;
627         /*
628          * Circle throuch all the connection of that component.  If we find
629          * an orphan connection whose name matches @csdev, link it.
630          */
631         for (i = 0; i < i_csdev->nr_outport; i++) {
632                 conn = &i_csdev->conns[i];
633
634                 /* We have found at least one orphan connection */
635                 if (conn->child_dev == NULL) {
636                         /* Does it match this newly added device? */
637                         if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
638                                 conn->child_dev = csdev;
639                         } else {
640                                 /* This component still has an orphan */
641                                 still_orphan = true;
642                         }
643                 }
644         }
645
646         i_csdev->orphan = still_orphan;
647
648         /*
649          * Returning '0' ensures that all known component on the
650          * bus will be checked.
651          */
652         return 0;
653 }
654
655 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
656 {
657         /*
658          * No need to check for a return value as orphan connection(s)
659          * are hooked-up with each newly added component.
660          */
661         bus_for_each_dev(&coresight_bustype, NULL,
662                          csdev, coresight_orphan_match);
663 }
664
665
666 static int coresight_name_match(struct device *dev, void *data)
667 {
668         char *to_match;
669         struct coresight_device *i_csdev;
670
671         to_match = data;
672         i_csdev = to_coresight_device(dev);
673
674         if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
675                 return 1;
676
677         return 0;
678 }
679
680 static void coresight_fixup_device_conns(struct coresight_device *csdev)
681 {
682         int i;
683         struct device *dev = NULL;
684         struct coresight_connection *conn;
685
686         for (i = 0; i < csdev->nr_outport; i++) {
687                 conn = &csdev->conns[i];
688                 dev = bus_find_device(&coresight_bustype, NULL,
689                                       (void *)conn->child_name,
690                                       coresight_name_match);
691
692                 if (dev) {
693                         conn->child_dev = to_coresight_device(dev);
694                         /* and put reference from 'bus_find_device()' */
695                         put_device(dev);
696                 } else {
697                         csdev->orphan = true;
698                         conn->child_dev = NULL;
699                 }
700         }
701 }
702
703 static int coresight_remove_match(struct device *dev, void *data)
704 {
705         int i;
706         struct coresight_device *csdev, *iterator;
707         struct coresight_connection *conn;
708
709         csdev = data;
710         iterator = to_coresight_device(dev);
711
712         /* No need to check oneself */
713         if (csdev == iterator)
714                 return 0;
715
716         /*
717          * Circle throuch all the connection of that component.  If we find
718          * a connection whose name matches @csdev, remove it.
719          */
720         for (i = 0; i < iterator->nr_outport; i++) {
721                 conn = &iterator->conns[i];
722
723                 if (conn->child_dev == NULL)
724                         continue;
725
726                 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
727                         iterator->orphan = true;
728                         conn->child_dev = NULL;
729                         /* No need to continue */
730                         break;
731                 }
732         }
733
734         /*
735          * Returning '0' ensures that all known component on the
736          * bus will be checked.
737          */
738         return 0;
739 }
740
741 static void coresight_remove_conns(struct coresight_device *csdev)
742 {
743         bus_for_each_dev(&coresight_bustype, NULL,
744                          csdev, coresight_remove_match);
745 }
746
747 /**
748  * coresight_timeout - loop until a bit has changed to a specific state.
749  * @addr: base address of the area of interest.
750  * @offset: address of a register, starting from @addr.
751  * @position: the position of the bit of interest.
752  * @value: the value the bit should have.
753  *
754  * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
755  * TIMEOUT_US has elapsed, which ever happens first.
756  */
757
758 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
759 {
760         int i;
761         u32 val;
762
763         for (i = TIMEOUT_US; i > 0; i--) {
764                 val = __raw_readl(addr + offset);
765                 /* waiting on the bit to go from 0 to 1 */
766                 if (value) {
767                         if (val & BIT(position))
768                                 return 0;
769                 /* waiting on the bit to go from 1 to 0 */
770                 } else {
771                         if (!(val & BIT(position)))
772                                 return 0;
773                 }
774
775                 /*
776                  * Delay is arbitrary - the specification doesn't say how long
777                  * we are expected to wait.  Extra check required to make sure
778                  * we don't wait needlessly on the last iteration.
779                  */
780                 if (i - 1)
781                         udelay(1);
782         }
783
784         return -EAGAIN;
785 }
786
787 struct bus_type coresight_bustype = {
788         .name   = "coresight",
789 };
790
791 static int __init coresight_init(void)
792 {
793         return bus_register(&coresight_bustype);
794 }
795 postcore_initcall(coresight_init);
796
797 struct coresight_device *coresight_register(struct coresight_desc *desc)
798 {
799         int i;
800         int ret;
801         int link_subtype;
802         int nr_refcnts = 1;
803         atomic_t *refcnts = NULL;
804         struct coresight_device *csdev;
805         struct coresight_connection *conns;
806
807         csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
808         if (!csdev) {
809                 ret = -ENOMEM;
810                 goto err_kzalloc_csdev;
811         }
812
813         if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
814             desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
815                 link_subtype = desc->subtype.link_subtype;
816
817                 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
818                         nr_refcnts = desc->pdata->nr_inport;
819                 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
820                         nr_refcnts = desc->pdata->nr_outport;
821         }
822
823         refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
824         if (!refcnts) {
825                 ret = -ENOMEM;
826                 goto err_kzalloc_refcnts;
827         }
828
829         csdev->refcnt = refcnts;
830
831         csdev->nr_inport = desc->pdata->nr_inport;
832         csdev->nr_outport = desc->pdata->nr_outport;
833         conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
834         if (!conns) {
835                 ret = -ENOMEM;
836                 goto err_kzalloc_conns;
837         }
838
839         for (i = 0; i < csdev->nr_outport; i++) {
840                 conns[i].outport = desc->pdata->outports[i];
841                 conns[i].child_name = desc->pdata->child_names[i];
842                 conns[i].child_port = desc->pdata->child_ports[i];
843         }
844
845         csdev->conns = conns;
846
847         csdev->type = desc->type;
848         csdev->subtype = desc->subtype;
849         csdev->ops = desc->ops;
850         csdev->orphan = false;
851
852         csdev->dev.type = &coresight_dev_type[desc->type];
853         csdev->dev.groups = desc->groups;
854         csdev->dev.parent = desc->dev;
855         csdev->dev.release = coresight_device_release;
856         csdev->dev.bus = &coresight_bustype;
857         dev_set_name(&csdev->dev, "%s", desc->pdata->name);
858
859         ret = device_register(&csdev->dev);
860         if (ret)
861                 goto err_device_register;
862
863         mutex_lock(&coresight_mutex);
864
865         coresight_fixup_device_conns(csdev);
866         coresight_fixup_orphan_conns(csdev);
867
868         mutex_unlock(&coresight_mutex);
869
870         return csdev;
871
872 err_device_register:
873         kfree(conns);
874 err_kzalloc_conns:
875         kfree(refcnts);
876 err_kzalloc_refcnts:
877         kfree(csdev);
878 err_kzalloc_csdev:
879         return ERR_PTR(ret);
880 }
881 EXPORT_SYMBOL_GPL(coresight_register);
882
883 void coresight_unregister(struct coresight_device *csdev)
884 {
885         /* Remove references of that device in the topology */
886         coresight_remove_conns(csdev);
887         device_unregister(&csdev->dev);
888 }
889 EXPORT_SYMBOL_GPL(coresight_unregister);
890
891 MODULE_LICENSE("GPL v2");