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