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