1
2
3
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/stringhash.h>
15#include <linux/mutex.h>
16#include <linux/clk.h>
17#include <linux/coresight.h>
18#include <linux/of_platform.h>
19#include <linux/delay.h>
20#include <linux/pm_runtime.h>
21
22#include "coresight-etm-perf.h"
23#include "coresight-priv.h"
24
25static DEFINE_MUTEX(coresight_mutex);
26
27
28
29
30
31
32struct coresight_node {
33 struct coresight_device *csdev;
34 struct list_head link;
35};
36
37
38
39
40
41static DEFINE_PER_CPU(struct list_head *, tracer_path);
42
43
44
45
46
47
48
49static struct list_head *stm_path;
50
51
52
53
54
55
56const u32 barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
57
58static int coresight_id_match(struct device *dev, void *data)
59{
60 int trace_id, i_trace_id;
61 struct coresight_device *csdev, *i_csdev;
62
63 csdev = data;
64 i_csdev = to_coresight_device(dev);
65
66
67
68
69
70 if (i_csdev == csdev || !i_csdev->enable ||
71 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
72 return 0;
73
74
75 trace_id = source_ops(csdev)->trace_id(csdev);
76 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
77
78
79 if (trace_id == i_trace_id)
80 return 1;
81
82 return 0;
83}
84
85static int coresight_source_is_unique(struct coresight_device *csdev)
86{
87 int trace_id = source_ops(csdev)->trace_id(csdev);
88
89
90 if (trace_id < 0)
91 return 0;
92
93 return !bus_for_each_dev(&coresight_bustype, NULL,
94 csdev, coresight_id_match);
95}
96
97static int coresight_find_link_inport(struct coresight_device *csdev,
98 struct coresight_device *parent)
99{
100 int i;
101 struct coresight_connection *conn;
102
103 for (i = 0; i < parent->pdata->nr_outport; i++) {
104 conn = &parent->pdata->conns[i];
105 if (conn->child_dev == csdev)
106 return conn->child_port;
107 }
108
109 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
110 dev_name(&parent->dev), dev_name(&csdev->dev));
111
112 return -ENODEV;
113}
114
115static int coresight_find_link_outport(struct coresight_device *csdev,
116 struct coresight_device *child)
117{
118 int i;
119 struct coresight_connection *conn;
120
121 for (i = 0; i < csdev->pdata->nr_outport; i++) {
122 conn = &csdev->pdata->conns[i];
123 if (conn->child_dev == child)
124 return conn->outport;
125 }
126
127 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
128 dev_name(&csdev->dev), dev_name(&child->dev));
129
130 return -ENODEV;
131}
132
133static inline u32 coresight_read_claim_tags(void __iomem *base)
134{
135 return readl_relaxed(base + CORESIGHT_CLAIMCLR);
136}
137
138static inline bool coresight_is_claimed_self_hosted(void __iomem *base)
139{
140 return coresight_read_claim_tags(base) == CORESIGHT_CLAIM_SELF_HOSTED;
141}
142
143static inline bool coresight_is_claimed_any(void __iomem *base)
144{
145 return coresight_read_claim_tags(base) != 0;
146}
147
148static inline void coresight_set_claim_tags(void __iomem *base)
149{
150 writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMSET);
151 isb();
152}
153
154static inline void coresight_clear_claim_tags(void __iomem *base)
155{
156 writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMCLR);
157 isb();
158}
159
160
161
162
163
164
165
166
167
168
169
170int coresight_claim_device_unlocked(void __iomem *base)
171{
172 if (coresight_is_claimed_any(base))
173 return -EBUSY;
174
175 coresight_set_claim_tags(base);
176 if (coresight_is_claimed_self_hosted(base))
177 return 0;
178
179 coresight_clear_claim_tags(base);
180 return -EBUSY;
181}
182
183int coresight_claim_device(void __iomem *base)
184{
185 int rc;
186
187 CS_UNLOCK(base);
188 rc = coresight_claim_device_unlocked(base);
189 CS_LOCK(base);
190
191 return rc;
192}
193
194
195
196
197
198void coresight_disclaim_device_unlocked(void __iomem *base)
199{
200
201 if (coresight_is_claimed_self_hosted(base))
202 coresight_clear_claim_tags(base);
203 else
204
205
206
207
208
209 WARN_ON_ONCE(1);
210}
211
212void coresight_disclaim_device(void __iomem *base)
213{
214 CS_UNLOCK(base);
215 coresight_disclaim_device_unlocked(base);
216 CS_LOCK(base);
217}
218
219static int coresight_enable_sink(struct coresight_device *csdev,
220 u32 mode, void *data)
221{
222 int ret;
223
224
225
226
227
228 if (!sink_ops(csdev)->enable)
229 return -EINVAL;
230
231 ret = sink_ops(csdev)->enable(csdev, mode, data);
232 if (ret)
233 return ret;
234 csdev->enable = true;
235
236 return 0;
237}
238
239static void coresight_disable_sink(struct coresight_device *csdev)
240{
241 int ret;
242
243 if (!sink_ops(csdev)->disable)
244 return;
245
246 ret = sink_ops(csdev)->disable(csdev);
247 if (ret)
248 return;
249 csdev->enable = false;
250}
251
252static int coresight_enable_link(struct coresight_device *csdev,
253 struct coresight_device *parent,
254 struct coresight_device *child)
255{
256 int ret;
257 int link_subtype;
258 int refport, inport, outport;
259
260 if (!parent || !child)
261 return -EINVAL;
262
263 inport = coresight_find_link_inport(csdev, parent);
264 outport = coresight_find_link_outport(csdev, child);
265 link_subtype = csdev->subtype.link_subtype;
266
267 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
268 refport = inport;
269 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
270 refport = outport;
271 else
272 refport = 0;
273
274 if (refport < 0)
275 return refport;
276
277 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
278 if (link_ops(csdev)->enable) {
279 ret = link_ops(csdev)->enable(csdev, inport, outport);
280 if (ret) {
281 atomic_dec(&csdev->refcnt[refport]);
282 return ret;
283 }
284 }
285 }
286
287 csdev->enable = true;
288
289 return 0;
290}
291
292static void coresight_disable_link(struct coresight_device *csdev,
293 struct coresight_device *parent,
294 struct coresight_device *child)
295{
296 int i, nr_conns;
297 int link_subtype;
298 int refport, inport, outport;
299
300 if (!parent || !child)
301 return;
302
303 inport = coresight_find_link_inport(csdev, parent);
304 outport = coresight_find_link_outport(csdev, child);
305 link_subtype = csdev->subtype.link_subtype;
306
307 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
308 refport = inport;
309 nr_conns = csdev->pdata->nr_inport;
310 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
311 refport = outport;
312 nr_conns = csdev->pdata->nr_outport;
313 } else {
314 refport = 0;
315 nr_conns = 1;
316 }
317
318 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
319 if (link_ops(csdev)->disable)
320 link_ops(csdev)->disable(csdev, inport, outport);
321 }
322
323 for (i = 0; i < nr_conns; i++)
324 if (atomic_read(&csdev->refcnt[i]) != 0)
325 return;
326
327 csdev->enable = false;
328}
329
330static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
331{
332 int ret;
333
334 if (!coresight_source_is_unique(csdev)) {
335 dev_warn(&csdev->dev, "traceID %d not unique\n",
336 source_ops(csdev)->trace_id(csdev));
337 return -EINVAL;
338 }
339
340 if (!csdev->enable) {
341 if (source_ops(csdev)->enable) {
342 ret = source_ops(csdev)->enable(csdev, NULL, mode);
343 if (ret)
344 return ret;
345 }
346 csdev->enable = true;
347 }
348
349 atomic_inc(csdev->refcnt);
350
351 return 0;
352}
353
354
355
356
357
358
359
360
361
362static bool coresight_disable_source(struct coresight_device *csdev)
363{
364 if (atomic_dec_return(csdev->refcnt) == 0) {
365 if (source_ops(csdev)->disable)
366 source_ops(csdev)->disable(csdev, NULL);
367 csdev->enable = false;
368 }
369 return !csdev->enable;
370}
371
372
373
374
375
376
377static void coresight_disable_path_from(struct list_head *path,
378 struct coresight_node *nd)
379{
380 u32 type;
381 struct coresight_device *csdev, *parent, *child;
382
383 if (!nd)
384 nd = list_first_entry(path, struct coresight_node, link);
385
386 list_for_each_entry_continue(nd, path, link) {
387 csdev = nd->csdev;
388 type = csdev->type;
389
390
391
392
393
394
395
396 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
397 type = (csdev == coresight_get_sink(path)) ?
398 CORESIGHT_DEV_TYPE_SINK :
399 CORESIGHT_DEV_TYPE_LINK;
400
401 switch (type) {
402 case CORESIGHT_DEV_TYPE_SINK:
403 coresight_disable_sink(csdev);
404 break;
405 case CORESIGHT_DEV_TYPE_SOURCE:
406
407
408
409
410
411 WARN_ON(1);
412 break;
413 case CORESIGHT_DEV_TYPE_LINK:
414 parent = list_prev_entry(nd, link)->csdev;
415 child = list_next_entry(nd, link)->csdev;
416 coresight_disable_link(csdev, parent, child);
417 break;
418 default:
419 break;
420 }
421 }
422}
423
424void coresight_disable_path(struct list_head *path)
425{
426 coresight_disable_path_from(path, NULL);
427}
428
429int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
430{
431
432 int ret = 0;
433 u32 type;
434 struct coresight_node *nd;
435 struct coresight_device *csdev, *parent, *child;
436
437 list_for_each_entry_reverse(nd, path, link) {
438 csdev = nd->csdev;
439 type = csdev->type;
440
441
442
443
444
445
446
447 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
448 type = (csdev == coresight_get_sink(path)) ?
449 CORESIGHT_DEV_TYPE_SINK :
450 CORESIGHT_DEV_TYPE_LINK;
451
452 switch (type) {
453 case CORESIGHT_DEV_TYPE_SINK:
454 ret = coresight_enable_sink(csdev, mode, sink_data);
455
456
457
458
459
460
461 if (ret)
462 goto out;
463 break;
464 case CORESIGHT_DEV_TYPE_SOURCE:
465
466 break;
467 case CORESIGHT_DEV_TYPE_LINK:
468 parent = list_prev_entry(nd, link)->csdev;
469 child = list_next_entry(nd, link)->csdev;
470 ret = coresight_enable_link(csdev, parent, child);
471 if (ret)
472 goto err;
473 break;
474 default:
475 goto err;
476 }
477 }
478
479out:
480 return ret;
481err:
482 coresight_disable_path_from(path, nd);
483 goto out;
484}
485
486struct coresight_device *coresight_get_sink(struct list_head *path)
487{
488 struct coresight_device *csdev;
489
490 if (!path)
491 return NULL;
492
493 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
494 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
495 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
496 return NULL;
497
498 return csdev;
499}
500
501static int coresight_enabled_sink(struct device *dev, const void *data)
502{
503 const bool *reset = data;
504 struct coresight_device *csdev = to_coresight_device(dev);
505
506 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
507 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
508 csdev->activated) {
509
510
511
512
513
514
515
516 if (*reset)
517 csdev->activated = false;
518
519 return 1;
520 }
521
522 return 0;
523}
524
525
526
527
528
529
530
531
532
533
534
535
536
537struct coresight_device *coresight_get_enabled_sink(bool deactivate)
538{
539 struct device *dev = NULL;
540
541 dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
542 coresight_enabled_sink);
543
544 return dev ? to_coresight_device(dev) : NULL;
545}
546
547static int coresight_sink_by_id(struct device *dev, const void *data)
548{
549 struct coresight_device *csdev = to_coresight_device(dev);
550 unsigned long hash;
551
552 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
553 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
554
555 if (!csdev->ea)
556 return 0;
557
558
559
560
561 hash = (unsigned long)csdev->ea->var;
562
563 if ((u32)hash == *(u32 *)data)
564 return 1;
565 }
566
567 return 0;
568}
569
570
571
572
573
574
575
576
577
578struct coresight_device *coresight_get_sink_by_id(u32 id)
579{
580 struct device *dev = NULL;
581
582 dev = bus_find_device(&coresight_bustype, NULL, &id,
583 coresight_sink_by_id);
584
585 return dev ? to_coresight_device(dev) : NULL;
586}
587
588
589
590
591
592
593
594static void coresight_grab_device(struct coresight_device *csdev)
595{
596 int i;
597
598 for (i = 0; i < csdev->pdata->nr_outport; i++) {
599 struct coresight_device *child;
600
601 child = csdev->pdata->conns[i].child_dev;
602 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
603 pm_runtime_get_sync(child->dev.parent);
604 }
605 pm_runtime_get_sync(csdev->dev.parent);
606}
607
608
609
610
611
612static void coresight_drop_device(struct coresight_device *csdev)
613{
614 int i;
615
616 pm_runtime_put(csdev->dev.parent);
617 for (i = 0; i < csdev->pdata->nr_outport; i++) {
618 struct coresight_device *child;
619
620 child = csdev->pdata->conns[i].child_dev;
621 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
622 pm_runtime_put(child->dev.parent);
623 }
624}
625
626
627
628
629
630
631
632
633
634
635
636
637static int _coresight_build_path(struct coresight_device *csdev,
638 struct coresight_device *sink,
639 struct list_head *path)
640{
641 int i;
642 bool found = false;
643 struct coresight_node *node;
644
645
646 if (csdev == sink)
647 goto out;
648
649
650 for (i = 0; i < csdev->pdata->nr_outport; i++) {
651 struct coresight_device *child_dev;
652
653 child_dev = csdev->pdata->conns[i].child_dev;
654 if (child_dev &&
655 _coresight_build_path(child_dev, sink, path) == 0) {
656 found = true;
657 break;
658 }
659 }
660
661 if (!found)
662 return -ENODEV;
663
664out:
665
666
667
668
669
670
671 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
672 if (!node)
673 return -ENOMEM;
674
675 coresight_grab_device(csdev);
676 node->csdev = csdev;
677 list_add(&node->link, path);
678
679 return 0;
680}
681
682struct list_head *coresight_build_path(struct coresight_device *source,
683 struct coresight_device *sink)
684{
685 struct list_head *path;
686 int rc;
687
688 if (!sink)
689 return ERR_PTR(-EINVAL);
690
691 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
692 if (!path)
693 return ERR_PTR(-ENOMEM);
694
695 INIT_LIST_HEAD(path);
696
697 rc = _coresight_build_path(source, sink, path);
698 if (rc) {
699 kfree(path);
700 return ERR_PTR(rc);
701 }
702
703 return path;
704}
705
706
707
708
709
710
711
712
713void coresight_release_path(struct list_head *path)
714{
715 struct coresight_device *csdev;
716 struct coresight_node *nd, *next;
717
718 list_for_each_entry_safe(nd, next, path, link) {
719 csdev = nd->csdev;
720
721 coresight_drop_device(csdev);
722 list_del(&nd->link);
723 kfree(nd);
724 }
725
726 kfree(path);
727 path = NULL;
728}
729
730
731
732
733
734
735
736static int coresight_validate_source(struct coresight_device *csdev,
737 const char *function)
738{
739 u32 type, subtype;
740
741 type = csdev->type;
742 subtype = csdev->subtype.source_subtype;
743
744 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
745 dev_err(&csdev->dev, "wrong device type in %s\n", function);
746 return -EINVAL;
747 }
748
749 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
750 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
751 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
752 return -EINVAL;
753 }
754
755 return 0;
756}
757
758int coresight_enable(struct coresight_device *csdev)
759{
760 int cpu, ret = 0;
761 struct coresight_device *sink;
762 struct list_head *path;
763 enum coresight_dev_subtype_source subtype;
764
765 subtype = csdev->subtype.source_subtype;
766
767 mutex_lock(&coresight_mutex);
768
769 ret = coresight_validate_source(csdev, __func__);
770 if (ret)
771 goto out;
772
773 if (csdev->enable) {
774
775
776
777
778
779 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
780 atomic_inc(csdev->refcnt);
781 goto out;
782 }
783
784
785
786
787
788 sink = coresight_get_enabled_sink(false);
789 if (!sink) {
790 ret = -EINVAL;
791 goto out;
792 }
793
794 path = coresight_build_path(csdev, sink);
795 if (IS_ERR(path)) {
796 pr_err("building path(s) failed\n");
797 ret = PTR_ERR(path);
798 goto out;
799 }
800
801 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
802 if (ret)
803 goto err_path;
804
805 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
806 if (ret)
807 goto err_source;
808
809 switch (subtype) {
810 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
811
812
813
814
815
816
817
818 cpu = source_ops(csdev)->cpu_id(csdev);
819 per_cpu(tracer_path, cpu) = path;
820 break;
821 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
822 stm_path = path;
823 break;
824 default:
825
826 break;
827 }
828
829out:
830 mutex_unlock(&coresight_mutex);
831 return ret;
832
833err_source:
834 coresight_disable_path(path);
835
836err_path:
837 coresight_release_path(path);
838 goto out;
839}
840EXPORT_SYMBOL_GPL(coresight_enable);
841
842void coresight_disable(struct coresight_device *csdev)
843{
844 int cpu, ret;
845 struct list_head *path = NULL;
846
847 mutex_lock(&coresight_mutex);
848
849 ret = coresight_validate_source(csdev, __func__);
850 if (ret)
851 goto out;
852
853 if (!csdev->enable || !coresight_disable_source(csdev))
854 goto out;
855
856 switch (csdev->subtype.source_subtype) {
857 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
858 cpu = source_ops(csdev)->cpu_id(csdev);
859 path = per_cpu(tracer_path, cpu);
860 per_cpu(tracer_path, cpu) = NULL;
861 break;
862 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
863 path = stm_path;
864 stm_path = NULL;
865 break;
866 default:
867
868 break;
869 }
870
871 coresight_disable_path(path);
872 coresight_release_path(path);
873
874out:
875 mutex_unlock(&coresight_mutex);
876}
877EXPORT_SYMBOL_GPL(coresight_disable);
878
879static ssize_t enable_sink_show(struct device *dev,
880 struct device_attribute *attr, char *buf)
881{
882 struct coresight_device *csdev = to_coresight_device(dev);
883
884 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
885}
886
887static ssize_t enable_sink_store(struct device *dev,
888 struct device_attribute *attr,
889 const char *buf, size_t size)
890{
891 int ret;
892 unsigned long val;
893 struct coresight_device *csdev = to_coresight_device(dev);
894
895 ret = kstrtoul(buf, 10, &val);
896 if (ret)
897 return ret;
898
899 if (val)
900 csdev->activated = true;
901 else
902 csdev->activated = false;
903
904 return size;
905
906}
907static DEVICE_ATTR_RW(enable_sink);
908
909static ssize_t enable_source_show(struct device *dev,
910 struct device_attribute *attr, char *buf)
911{
912 struct coresight_device *csdev = to_coresight_device(dev);
913
914 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
915}
916
917static ssize_t enable_source_store(struct device *dev,
918 struct device_attribute *attr,
919 const char *buf, size_t size)
920{
921 int ret = 0;
922 unsigned long val;
923 struct coresight_device *csdev = to_coresight_device(dev);
924
925 ret = kstrtoul(buf, 10, &val);
926 if (ret)
927 return ret;
928
929 if (val) {
930 ret = coresight_enable(csdev);
931 if (ret)
932 return ret;
933 } else {
934 coresight_disable(csdev);
935 }
936
937 return size;
938}
939static DEVICE_ATTR_RW(enable_source);
940
941static struct attribute *coresight_sink_attrs[] = {
942 &dev_attr_enable_sink.attr,
943 NULL,
944};
945ATTRIBUTE_GROUPS(coresight_sink);
946
947static struct attribute *coresight_source_attrs[] = {
948 &dev_attr_enable_source.attr,
949 NULL,
950};
951ATTRIBUTE_GROUPS(coresight_source);
952
953static struct device_type coresight_dev_type[] = {
954 {
955 .name = "none",
956 },
957 {
958 .name = "sink",
959 .groups = coresight_sink_groups,
960 },
961 {
962 .name = "link",
963 },
964 {
965 .name = "linksink",
966 .groups = coresight_sink_groups,
967 },
968 {
969 .name = "source",
970 .groups = coresight_source_groups,
971 },
972 {
973 .name = "helper",
974 },
975};
976
977static void coresight_device_release(struct device *dev)
978{
979 struct coresight_device *csdev = to_coresight_device(dev);
980
981 fwnode_handle_put(csdev->dev.fwnode);
982 kfree(csdev->refcnt);
983 kfree(csdev);
984}
985
986static int coresight_orphan_match(struct device *dev, void *data)
987{
988 int i;
989 bool still_orphan = false;
990 struct coresight_device *csdev, *i_csdev;
991 struct coresight_connection *conn;
992
993 csdev = data;
994 i_csdev = to_coresight_device(dev);
995
996
997 if (csdev == i_csdev)
998 return 0;
999
1000
1001 if (!i_csdev->orphan)
1002 return 0;
1003
1004
1005
1006
1007 for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
1008 conn = &i_csdev->pdata->conns[i];
1009
1010
1011 if (conn->child_dev == NULL) {
1012
1013 if (conn->child_fwnode == csdev->dev.fwnode)
1014 conn->child_dev = csdev;
1015 else
1016
1017 still_orphan = true;
1018 }
1019 }
1020
1021 i_csdev->orphan = still_orphan;
1022
1023
1024
1025
1026
1027 return 0;
1028}
1029
1030static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
1031{
1032
1033
1034
1035
1036 bus_for_each_dev(&coresight_bustype, NULL,
1037 csdev, coresight_orphan_match);
1038}
1039
1040
1041static void coresight_fixup_device_conns(struct coresight_device *csdev)
1042{
1043 int i;
1044
1045 for (i = 0; i < csdev->pdata->nr_outport; i++) {
1046 struct coresight_connection *conn = &csdev->pdata->conns[i];
1047 struct device *dev = NULL;
1048
1049 dev = bus_find_device(&coresight_bustype, NULL,
1050 (void *)conn->child_fwnode,
1051 coresight_device_fwnode_match);
1052 if (dev) {
1053 conn->child_dev = to_coresight_device(dev);
1054
1055 put_device(dev);
1056 } else {
1057 csdev->orphan = true;
1058 conn->child_dev = NULL;
1059 }
1060 }
1061}
1062
1063static int coresight_remove_match(struct device *dev, void *data)
1064{
1065 int i;
1066 struct coresight_device *csdev, *iterator;
1067 struct coresight_connection *conn;
1068
1069 csdev = data;
1070 iterator = to_coresight_device(dev);
1071
1072
1073 if (csdev == iterator)
1074 return 0;
1075
1076
1077
1078
1079
1080 for (i = 0; i < iterator->pdata->nr_outport; i++) {
1081 conn = &iterator->pdata->conns[i];
1082
1083 if (conn->child_dev == NULL)
1084 continue;
1085
1086 if (csdev->dev.fwnode == conn->child_fwnode) {
1087 iterator->orphan = true;
1088 conn->child_dev = NULL;
1089
1090
1091
1092
1093
1094 fwnode_handle_put(conn->child_fwnode);
1095
1096 break;
1097 }
1098 }
1099
1100
1101
1102
1103
1104 return 0;
1105}
1106
1107
1108
1109
1110
1111static void coresight_remove_conns(struct coresight_device *csdev)
1112{
1113
1114
1115
1116
1117
1118
1119 if (csdev->pdata->nr_inport)
1120 bus_for_each_dev(&coresight_bustype, NULL,
1121 csdev, coresight_remove_match);
1122}
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
1136{
1137 int i;
1138 u32 val;
1139
1140 for (i = TIMEOUT_US; i > 0; i--) {
1141 val = __raw_readl(addr + offset);
1142
1143 if (value) {
1144 if (val & BIT(position))
1145 return 0;
1146
1147 } else {
1148 if (!(val & BIT(position)))
1149 return 0;
1150 }
1151
1152
1153
1154
1155
1156
1157 if (i - 1)
1158 udelay(1);
1159 }
1160
1161 return -EAGAIN;
1162}
1163
1164struct bus_type coresight_bustype = {
1165 .name = "coresight",
1166};
1167
1168static int __init coresight_init(void)
1169{
1170 return bus_register(&coresight_bustype);
1171}
1172postcore_initcall(coresight_init);
1173
1174
1175
1176
1177
1178void coresight_release_platform_data(struct coresight_platform_data *pdata)
1179{
1180 int i;
1181
1182 for (i = 0; i < pdata->nr_outport; i++) {
1183 if (pdata->conns[i].child_fwnode) {
1184 fwnode_handle_put(pdata->conns[i].child_fwnode);
1185 pdata->conns[i].child_fwnode = NULL;
1186 }
1187 }
1188}
1189
1190struct coresight_device *coresight_register(struct coresight_desc *desc)
1191{
1192 int ret;
1193 int link_subtype;
1194 int nr_refcnts = 1;
1195 atomic_t *refcnts = NULL;
1196 struct coresight_device *csdev;
1197
1198 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1199 if (!csdev) {
1200 ret = -ENOMEM;
1201 goto err_out;
1202 }
1203
1204 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1205 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1206 link_subtype = desc->subtype.link_subtype;
1207
1208 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1209 nr_refcnts = desc->pdata->nr_inport;
1210 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1211 nr_refcnts = desc->pdata->nr_outport;
1212 }
1213
1214 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1215 if (!refcnts) {
1216 ret = -ENOMEM;
1217 goto err_free_csdev;
1218 }
1219
1220 csdev->refcnt = refcnts;
1221
1222 csdev->pdata = desc->pdata;
1223
1224 csdev->type = desc->type;
1225 csdev->subtype = desc->subtype;
1226 csdev->ops = desc->ops;
1227 csdev->orphan = false;
1228
1229 csdev->dev.type = &coresight_dev_type[desc->type];
1230 csdev->dev.groups = desc->groups;
1231 csdev->dev.parent = desc->dev;
1232 csdev->dev.release = coresight_device_release;
1233 csdev->dev.bus = &coresight_bustype;
1234
1235
1236
1237
1238 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1239 dev_set_name(&csdev->dev, "%s", desc->name);
1240
1241 ret = device_register(&csdev->dev);
1242 if (ret) {
1243 put_device(&csdev->dev);
1244
1245
1246
1247
1248 goto err_out;
1249 }
1250
1251 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1252 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1253 ret = etm_perf_add_symlink_sink(csdev);
1254
1255 if (ret) {
1256 device_unregister(&csdev->dev);
1257
1258
1259
1260
1261
1262
1263 goto err_out;
1264 }
1265 }
1266
1267 mutex_lock(&coresight_mutex);
1268
1269 coresight_fixup_device_conns(csdev);
1270 coresight_fixup_orphan_conns(csdev);
1271
1272 mutex_unlock(&coresight_mutex);
1273
1274 return csdev;
1275
1276err_free_csdev:
1277 kfree(csdev);
1278err_out:
1279
1280 coresight_release_platform_data(desc->pdata);
1281 return ERR_PTR(ret);
1282}
1283EXPORT_SYMBOL_GPL(coresight_register);
1284
1285void coresight_unregister(struct coresight_device *csdev)
1286{
1287 etm_perf_del_symlink_sink(csdev);
1288
1289 coresight_remove_conns(csdev);
1290 coresight_release_platform_data(csdev->pdata);
1291 device_unregister(&csdev->dev);
1292}
1293EXPORT_SYMBOL_GPL(coresight_unregister);
1294
1295
1296
1297
1298
1299
1300
1301
1302static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1303 struct fwnode_handle *fwnode)
1304{
1305 int i;
1306
1307 for (i = 0; i < dict->nr_idx; i++)
1308 if (dict->fwnode_list[i] == fwnode)
1309 return i;
1310 return -ENOENT;
1311}
1312
1313
1314
1315
1316
1317
1318
1319
1320char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1321 struct device *dev)
1322{
1323 int idx;
1324 char *name = NULL;
1325 struct fwnode_handle **list;
1326
1327 mutex_lock(&coresight_mutex);
1328
1329 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1330 if (idx < 0) {
1331
1332 idx = dict->nr_idx;
1333 list = krealloc(dict->fwnode_list,
1334 (idx + 1) * sizeof(*dict->fwnode_list),
1335 GFP_KERNEL);
1336 if (ZERO_OR_NULL_PTR(list)) {
1337 idx = -ENOMEM;
1338 goto done;
1339 }
1340
1341 list[idx] = dev_fwnode(dev);
1342 dict->fwnode_list = list;
1343 dict->nr_idx = idx + 1;
1344 }
1345
1346 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1347done:
1348 mutex_unlock(&coresight_mutex);
1349 return name;
1350}
1351EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1352