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#include "coresight-syscfg.h"
25
26static DEFINE_MUTEX(coresight_mutex);
27static DEFINE_PER_CPU(struct coresight_device *, csdev_sink);
28
29
30
31
32
33
34struct coresight_node {
35 struct coresight_device *csdev;
36 struct list_head link;
37};
38
39
40
41
42
43static DEFINE_PER_CPU(struct list_head *, tracer_path);
44
45
46
47
48
49
50
51static struct list_head *stm_path;
52
53
54
55
56
57
58const u32 coresight_barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
59EXPORT_SYMBOL_GPL(coresight_barrier_pkt);
60
61static const struct cti_assoc_op *cti_assoc_ops;
62
63void coresight_set_cti_ops(const struct cti_assoc_op *cti_op)
64{
65 cti_assoc_ops = cti_op;
66}
67EXPORT_SYMBOL_GPL(coresight_set_cti_ops);
68
69void coresight_remove_cti_ops(void)
70{
71 cti_assoc_ops = NULL;
72}
73EXPORT_SYMBOL_GPL(coresight_remove_cti_ops);
74
75void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev)
76{
77 per_cpu(csdev_sink, cpu) = csdev;
78}
79EXPORT_SYMBOL_GPL(coresight_set_percpu_sink);
80
81struct coresight_device *coresight_get_percpu_sink(int cpu)
82{
83 return per_cpu(csdev_sink, cpu);
84}
85EXPORT_SYMBOL_GPL(coresight_get_percpu_sink);
86
87static int coresight_id_match(struct device *dev, void *data)
88{
89 int trace_id, i_trace_id;
90 struct coresight_device *csdev, *i_csdev;
91
92 csdev = data;
93 i_csdev = to_coresight_device(dev);
94
95
96
97
98
99 if (i_csdev == csdev || !i_csdev->enable ||
100 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
101 return 0;
102
103
104 trace_id = source_ops(csdev)->trace_id(csdev);
105 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
106
107
108 if (trace_id == i_trace_id)
109 return 1;
110
111 return 0;
112}
113
114static int coresight_source_is_unique(struct coresight_device *csdev)
115{
116 int trace_id = source_ops(csdev)->trace_id(csdev);
117
118
119 if (trace_id < 0)
120 return 0;
121
122 return !bus_for_each_dev(&coresight_bustype, NULL,
123 csdev, coresight_id_match);
124}
125
126static int coresight_find_link_inport(struct coresight_device *csdev,
127 struct coresight_device *parent)
128{
129 int i;
130 struct coresight_connection *conn;
131
132 for (i = 0; i < parent->pdata->nr_outport; i++) {
133 conn = &parent->pdata->conns[i];
134 if (conn->child_dev == csdev)
135 return conn->child_port;
136 }
137
138 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
139 dev_name(&parent->dev), dev_name(&csdev->dev));
140
141 return -ENODEV;
142}
143
144static int coresight_find_link_outport(struct coresight_device *csdev,
145 struct coresight_device *child)
146{
147 int i;
148 struct coresight_connection *conn;
149
150 for (i = 0; i < csdev->pdata->nr_outport; i++) {
151 conn = &csdev->pdata->conns[i];
152 if (conn->child_dev == child)
153 return conn->outport;
154 }
155
156 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
157 dev_name(&csdev->dev), dev_name(&child->dev));
158
159 return -ENODEV;
160}
161
162static inline u32 coresight_read_claim_tags(struct coresight_device *csdev)
163{
164 return csdev_access_relaxed_read32(&csdev->access, CORESIGHT_CLAIMCLR);
165}
166
167static inline bool coresight_is_claimed_self_hosted(struct coresight_device *csdev)
168{
169 return coresight_read_claim_tags(csdev) == CORESIGHT_CLAIM_SELF_HOSTED;
170}
171
172static inline bool coresight_is_claimed_any(struct coresight_device *csdev)
173{
174 return coresight_read_claim_tags(csdev) != 0;
175}
176
177static inline void coresight_set_claim_tags(struct coresight_device *csdev)
178{
179 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
180 CORESIGHT_CLAIMSET);
181 isb();
182}
183
184static inline void coresight_clear_claim_tags(struct coresight_device *csdev)
185{
186 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
187 CORESIGHT_CLAIMCLR);
188 isb();
189}
190
191
192
193
194
195
196
197
198
199
200
201int coresight_claim_device_unlocked(struct coresight_device *csdev)
202{
203 if (WARN_ON(!csdev))
204 return -EINVAL;
205
206 if (coresight_is_claimed_any(csdev))
207 return -EBUSY;
208
209 coresight_set_claim_tags(csdev);
210 if (coresight_is_claimed_self_hosted(csdev))
211 return 0;
212
213 coresight_clear_claim_tags(csdev);
214 return -EBUSY;
215}
216EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked);
217
218int coresight_claim_device(struct coresight_device *csdev)
219{
220 int rc;
221
222 if (WARN_ON(!csdev))
223 return -EINVAL;
224
225 CS_UNLOCK(csdev->access.base);
226 rc = coresight_claim_device_unlocked(csdev);
227 CS_LOCK(csdev->access.base);
228
229 return rc;
230}
231EXPORT_SYMBOL_GPL(coresight_claim_device);
232
233
234
235
236
237void coresight_disclaim_device_unlocked(struct coresight_device *csdev)
238{
239
240 if (WARN_ON(!csdev))
241 return;
242
243 if (coresight_is_claimed_self_hosted(csdev))
244 coresight_clear_claim_tags(csdev);
245 else
246
247
248
249
250
251 WARN_ON_ONCE(1);
252}
253EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked);
254
255void coresight_disclaim_device(struct coresight_device *csdev)
256{
257 if (WARN_ON(!csdev))
258 return;
259
260 CS_UNLOCK(csdev->access.base);
261 coresight_disclaim_device_unlocked(csdev);
262 CS_LOCK(csdev->access.base);
263}
264EXPORT_SYMBOL_GPL(coresight_disclaim_device);
265
266
267static int
268coresight_control_assoc_ectdev(struct coresight_device *csdev, bool enable)
269{
270 int ect_ret = 0;
271 struct coresight_device *ect_csdev = csdev->ect_dev;
272 struct module *mod;
273
274 if (!ect_csdev)
275 return 0;
276 if ((!ect_ops(ect_csdev)->enable) || (!ect_ops(ect_csdev)->disable))
277 return 0;
278
279 mod = ect_csdev->dev.parent->driver->owner;
280 if (enable) {
281 if (try_module_get(mod)) {
282 ect_ret = ect_ops(ect_csdev)->enable(ect_csdev);
283 if (ect_ret) {
284 module_put(mod);
285 } else {
286 get_device(ect_csdev->dev.parent);
287 csdev->ect_enabled = true;
288 }
289 } else
290 ect_ret = -ENODEV;
291 } else {
292 if (csdev->ect_enabled) {
293 ect_ret = ect_ops(ect_csdev)->disable(ect_csdev);
294 put_device(ect_csdev->dev.parent);
295 module_put(mod);
296 csdev->ect_enabled = false;
297 }
298 }
299
300
301 if (ect_ret)
302 dev_info(&csdev->dev, "Associated ECT device (%s) %s failed\n",
303 dev_name(&ect_csdev->dev),
304 enable ? "enable" : "disable");
305 return ect_ret;
306}
307
308
309
310
311
312void coresight_set_assoc_ectdev_mutex(struct coresight_device *csdev,
313 struct coresight_device *ect_csdev)
314{
315 mutex_lock(&coresight_mutex);
316 csdev->ect_dev = ect_csdev;
317 mutex_unlock(&coresight_mutex);
318}
319EXPORT_SYMBOL_GPL(coresight_set_assoc_ectdev_mutex);
320
321static int coresight_enable_sink(struct coresight_device *csdev,
322 u32 mode, void *data)
323{
324 int ret;
325
326
327
328
329
330 if (!sink_ops(csdev)->enable)
331 return -EINVAL;
332
333 ret = coresight_control_assoc_ectdev(csdev, true);
334 if (ret)
335 return ret;
336 ret = sink_ops(csdev)->enable(csdev, mode, data);
337 if (ret) {
338 coresight_control_assoc_ectdev(csdev, false);
339 return ret;
340 }
341 csdev->enable = true;
342
343 return 0;
344}
345
346static void coresight_disable_sink(struct coresight_device *csdev)
347{
348 int ret;
349
350 if (!sink_ops(csdev)->disable)
351 return;
352
353 ret = sink_ops(csdev)->disable(csdev);
354 if (ret)
355 return;
356 coresight_control_assoc_ectdev(csdev, false);
357 csdev->enable = false;
358}
359
360static int coresight_enable_link(struct coresight_device *csdev,
361 struct coresight_device *parent,
362 struct coresight_device *child)
363{
364 int ret = 0;
365 int link_subtype;
366 int inport, outport;
367
368 if (!parent || !child)
369 return -EINVAL;
370
371 inport = coresight_find_link_inport(csdev, parent);
372 outport = coresight_find_link_outport(csdev, child);
373 link_subtype = csdev->subtype.link_subtype;
374
375 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && inport < 0)
376 return inport;
377 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && outport < 0)
378 return outport;
379
380 if (link_ops(csdev)->enable) {
381 ret = coresight_control_assoc_ectdev(csdev, true);
382 if (!ret) {
383 ret = link_ops(csdev)->enable(csdev, inport, outport);
384 if (ret)
385 coresight_control_assoc_ectdev(csdev, false);
386 }
387 }
388
389 if (!ret)
390 csdev->enable = true;
391
392 return ret;
393}
394
395static void coresight_disable_link(struct coresight_device *csdev,
396 struct coresight_device *parent,
397 struct coresight_device *child)
398{
399 int i, nr_conns;
400 int link_subtype;
401 int inport, outport;
402
403 if (!parent || !child)
404 return;
405
406 inport = coresight_find_link_inport(csdev, parent);
407 outport = coresight_find_link_outport(csdev, child);
408 link_subtype = csdev->subtype.link_subtype;
409
410 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
411 nr_conns = csdev->pdata->nr_inport;
412 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
413 nr_conns = csdev->pdata->nr_outport;
414 } else {
415 nr_conns = 1;
416 }
417
418 if (link_ops(csdev)->disable) {
419 link_ops(csdev)->disable(csdev, inport, outport);
420 coresight_control_assoc_ectdev(csdev, false);
421 }
422
423 for (i = 0; i < nr_conns; i++)
424 if (atomic_read(&csdev->refcnt[i]) != 0)
425 return;
426
427 csdev->enable = false;
428}
429
430static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
431{
432 int ret;
433
434 if (!coresight_source_is_unique(csdev)) {
435 dev_warn(&csdev->dev, "traceID %d not unique\n",
436 source_ops(csdev)->trace_id(csdev));
437 return -EINVAL;
438 }
439
440 if (!csdev->enable) {
441 if (source_ops(csdev)->enable) {
442 ret = coresight_control_assoc_ectdev(csdev, true);
443 if (ret)
444 return ret;
445 ret = source_ops(csdev)->enable(csdev, NULL, mode);
446 if (ret) {
447 coresight_control_assoc_ectdev(csdev, false);
448 return ret;
449 }
450 }
451 csdev->enable = true;
452 }
453
454 atomic_inc(csdev->refcnt);
455
456 return 0;
457}
458
459
460
461
462
463
464
465
466
467static bool coresight_disable_source(struct coresight_device *csdev)
468{
469 if (atomic_dec_return(csdev->refcnt) == 0) {
470 if (source_ops(csdev)->disable)
471 source_ops(csdev)->disable(csdev, NULL);
472 coresight_control_assoc_ectdev(csdev, false);
473 csdev->enable = false;
474 }
475 return !csdev->enable;
476}
477
478
479
480
481
482
483static void coresight_disable_path_from(struct list_head *path,
484 struct coresight_node *nd)
485{
486 u32 type;
487 struct coresight_device *csdev, *parent, *child;
488
489 if (!nd)
490 nd = list_first_entry(path, struct coresight_node, link);
491
492 list_for_each_entry_continue(nd, path, link) {
493 csdev = nd->csdev;
494 type = csdev->type;
495
496
497
498
499
500
501
502 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
503 type = (csdev == coresight_get_sink(path)) ?
504 CORESIGHT_DEV_TYPE_SINK :
505 CORESIGHT_DEV_TYPE_LINK;
506
507 switch (type) {
508 case CORESIGHT_DEV_TYPE_SINK:
509 coresight_disable_sink(csdev);
510 break;
511 case CORESIGHT_DEV_TYPE_SOURCE:
512
513
514
515
516
517 WARN_ON(1);
518 break;
519 case CORESIGHT_DEV_TYPE_LINK:
520 parent = list_prev_entry(nd, link)->csdev;
521 child = list_next_entry(nd, link)->csdev;
522 coresight_disable_link(csdev, parent, child);
523 break;
524 default:
525 break;
526 }
527 }
528}
529
530void coresight_disable_path(struct list_head *path)
531{
532 coresight_disable_path_from(path, NULL);
533}
534EXPORT_SYMBOL_GPL(coresight_disable_path);
535
536int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
537{
538
539 int ret = 0;
540 u32 type;
541 struct coresight_node *nd;
542 struct coresight_device *csdev, *parent, *child;
543
544 list_for_each_entry_reverse(nd, path, link) {
545 csdev = nd->csdev;
546 type = csdev->type;
547
548
549
550
551
552
553
554 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
555 type = (csdev == coresight_get_sink(path)) ?
556 CORESIGHT_DEV_TYPE_SINK :
557 CORESIGHT_DEV_TYPE_LINK;
558
559 switch (type) {
560 case CORESIGHT_DEV_TYPE_SINK:
561 ret = coresight_enable_sink(csdev, mode, sink_data);
562
563
564
565
566
567
568 if (ret)
569 goto out;
570 break;
571 case CORESIGHT_DEV_TYPE_SOURCE:
572
573 break;
574 case CORESIGHT_DEV_TYPE_LINK:
575 parent = list_prev_entry(nd, link)->csdev;
576 child = list_next_entry(nd, link)->csdev;
577 ret = coresight_enable_link(csdev, parent, child);
578 if (ret)
579 goto err;
580 break;
581 default:
582 goto err;
583 }
584 }
585
586out:
587 return ret;
588err:
589 coresight_disable_path_from(path, nd);
590 goto out;
591}
592
593struct coresight_device *coresight_get_sink(struct list_head *path)
594{
595 struct coresight_device *csdev;
596
597 if (!path)
598 return NULL;
599
600 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
601 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
602 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
603 return NULL;
604
605 return csdev;
606}
607
608static struct coresight_device *
609coresight_find_enabled_sink(struct coresight_device *csdev)
610{
611 int i;
612 struct coresight_device *sink = NULL;
613
614 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
615 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
616 csdev->activated)
617 return csdev;
618
619
620
621
622 for (i = 0; i < csdev->pdata->nr_outport; i++) {
623 struct coresight_device *child_dev;
624
625 child_dev = csdev->pdata->conns[i].child_dev;
626 if (child_dev)
627 sink = coresight_find_enabled_sink(child_dev);
628 if (sink)
629 return sink;
630 }
631
632 return NULL;
633}
634
635
636
637
638
639
640
641struct coresight_device *
642coresight_get_enabled_sink(struct coresight_device *source)
643{
644 if (!source)
645 return NULL;
646
647 return coresight_find_enabled_sink(source);
648}
649
650static int coresight_sink_by_id(struct device *dev, const void *data)
651{
652 struct coresight_device *csdev = to_coresight_device(dev);
653 unsigned long hash;
654
655 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
656 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
657
658 if (!csdev->ea)
659 return 0;
660
661
662
663
664 hash = (unsigned long)csdev->ea->var;
665
666 if ((u32)hash == *(u32 *)data)
667 return 1;
668 }
669
670 return 0;
671}
672
673
674
675
676
677
678
679
680
681struct coresight_device *coresight_get_sink_by_id(u32 id)
682{
683 struct device *dev = NULL;
684
685 dev = bus_find_device(&coresight_bustype, NULL, &id,
686 coresight_sink_by_id);
687
688 return dev ? to_coresight_device(dev) : NULL;
689}
690
691
692
693
694
695
696
697
698
699
700static inline bool coresight_get_ref(struct coresight_device *csdev)
701{
702 struct device *dev = csdev->dev.parent;
703
704
705 if (!try_module_get(dev->driver->owner))
706 return false;
707
708 get_device(dev);
709 pm_runtime_get_sync(dev);
710 return true;
711}
712
713
714
715
716
717
718
719static inline void coresight_put_ref(struct coresight_device *csdev)
720{
721 struct device *dev = csdev->dev.parent;
722
723 pm_runtime_put(dev);
724 put_device(dev);
725 module_put(dev->driver->owner);
726}
727
728
729
730
731
732
733
734static int coresight_grab_device(struct coresight_device *csdev)
735{
736 int i;
737
738 for (i = 0; i < csdev->pdata->nr_outport; i++) {
739 struct coresight_device *child;
740
741 child = csdev->pdata->conns[i].child_dev;
742 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
743 if (!coresight_get_ref(child))
744 goto err;
745 }
746 if (coresight_get_ref(csdev))
747 return 0;
748err:
749 for (i--; i >= 0; i--) {
750 struct coresight_device *child;
751
752 child = csdev->pdata->conns[i].child_dev;
753 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
754 coresight_put_ref(child);
755 }
756 return -ENODEV;
757}
758
759
760
761
762
763static void coresight_drop_device(struct coresight_device *csdev)
764{
765 int i;
766
767 coresight_put_ref(csdev);
768 for (i = 0; i < csdev->pdata->nr_outport; i++) {
769 struct coresight_device *child;
770
771 child = csdev->pdata->conns[i].child_dev;
772 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
773 coresight_put_ref(child);
774 }
775}
776
777
778
779
780
781
782
783
784
785
786
787
788
789static int _coresight_build_path(struct coresight_device *csdev,
790 struct coresight_device *sink,
791 struct list_head *path)
792{
793 int i, ret;
794 bool found = false;
795 struct coresight_node *node;
796
797
798 if (csdev == sink)
799 goto out;
800
801 if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&
802 sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {
803 if (_coresight_build_path(sink, sink, path) == 0) {
804 found = true;
805 goto out;
806 }
807 }
808
809
810 for (i = 0; i < csdev->pdata->nr_outport; i++) {
811 struct coresight_device *child_dev;
812
813 child_dev = csdev->pdata->conns[i].child_dev;
814 if (child_dev &&
815 _coresight_build_path(child_dev, sink, path) == 0) {
816 found = true;
817 break;
818 }
819 }
820
821 if (!found)
822 return -ENODEV;
823
824out:
825
826
827
828
829
830
831 ret = coresight_grab_device(csdev);
832 if (ret)
833 return ret;
834
835 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
836 if (!node)
837 return -ENOMEM;
838
839 node->csdev = csdev;
840 list_add(&node->link, path);
841
842 return 0;
843}
844
845struct list_head *coresight_build_path(struct coresight_device *source,
846 struct coresight_device *sink)
847{
848 struct list_head *path;
849 int rc;
850
851 if (!sink)
852 return ERR_PTR(-EINVAL);
853
854 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
855 if (!path)
856 return ERR_PTR(-ENOMEM);
857
858 INIT_LIST_HEAD(path);
859
860 rc = _coresight_build_path(source, sink, path);
861 if (rc) {
862 kfree(path);
863 return ERR_PTR(rc);
864 }
865
866 return path;
867}
868
869
870
871
872
873
874
875
876void coresight_release_path(struct list_head *path)
877{
878 struct coresight_device *csdev;
879 struct coresight_node *nd, *next;
880
881 list_for_each_entry_safe(nd, next, path, link) {
882 csdev = nd->csdev;
883
884 coresight_drop_device(csdev);
885 list_del(&nd->link);
886 kfree(nd);
887 }
888
889 kfree(path);
890}
891
892
893static inline bool coresight_is_def_sink_type(struct coresight_device *csdev)
894{
895
896 if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
897 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
898 (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
899 return true;
900 return false;
901}
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920static struct coresight_device *
921coresight_select_best_sink(struct coresight_device *sink, int *depth,
922 struct coresight_device *new_sink, int new_depth)
923{
924 bool update = false;
925
926 if (!sink) {
927
928 update = true;
929 } else if (new_sink->subtype.sink_subtype >
930 sink->subtype.sink_subtype) {
931
932 update = true;
933 } else if ((new_sink->subtype.sink_subtype ==
934 sink->subtype.sink_subtype) &&
935 (*depth > new_depth)) {
936
937 update = true;
938 }
939
940 if (update)
941 *depth = new_depth;
942 return update ? new_sink : sink;
943}
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961static struct coresight_device *
962coresight_find_sink(struct coresight_device *csdev, int *depth)
963{
964 int i, curr_depth = *depth + 1, found_depth = 0;
965 struct coresight_device *found_sink = NULL;
966
967 if (coresight_is_def_sink_type(csdev)) {
968 found_depth = curr_depth;
969 found_sink = csdev;
970 if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
971 goto return_def_sink;
972
973 }
974
975
976
977
978
979 for (i = 0; i < csdev->pdata->nr_outport; i++) {
980 struct coresight_device *child_dev, *sink = NULL;
981 int child_depth = curr_depth;
982
983 child_dev = csdev->pdata->conns[i].child_dev;
984 if (child_dev)
985 sink = coresight_find_sink(child_dev, &child_depth);
986
987 if (sink)
988 found_sink = coresight_select_best_sink(found_sink,
989 &found_depth,
990 sink,
991 child_depth);
992 }
993
994return_def_sink:
995
996 if (found_sink)
997 *depth = found_depth;
998 return found_sink;
999}
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017struct coresight_device *
1018coresight_find_default_sink(struct coresight_device *csdev)
1019{
1020 int depth = 0;
1021
1022
1023 if (!csdev->def_sink) {
1024 if (coresight_is_percpu_source(csdev))
1025 csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));
1026 if (!csdev->def_sink)
1027 csdev->def_sink = coresight_find_sink(csdev, &depth);
1028 }
1029 return csdev->def_sink;
1030}
1031
1032static int coresight_remove_sink_ref(struct device *dev, void *data)
1033{
1034 struct coresight_device *sink = data;
1035 struct coresight_device *source = to_coresight_device(dev);
1036
1037 if (source->def_sink == sink)
1038 source->def_sink = NULL;
1039 return 0;
1040}
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052static void coresight_clear_default_sink(struct coresight_device *csdev)
1053{
1054 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
1055 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
1056 bus_for_each_dev(&coresight_bustype, NULL, csdev,
1057 coresight_remove_sink_ref);
1058 }
1059}
1060
1061
1062
1063
1064
1065
1066
1067static int coresight_validate_source(struct coresight_device *csdev,
1068 const char *function)
1069{
1070 u32 type, subtype;
1071
1072 type = csdev->type;
1073 subtype = csdev->subtype.source_subtype;
1074
1075 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
1076 dev_err(&csdev->dev, "wrong device type in %s\n", function);
1077 return -EINVAL;
1078 }
1079
1080 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
1081 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
1082 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
1083 return -EINVAL;
1084 }
1085
1086 return 0;
1087}
1088
1089int coresight_enable(struct coresight_device *csdev)
1090{
1091 int cpu, ret = 0;
1092 struct coresight_device *sink;
1093 struct list_head *path;
1094 enum coresight_dev_subtype_source subtype;
1095
1096 subtype = csdev->subtype.source_subtype;
1097
1098 mutex_lock(&coresight_mutex);
1099
1100 ret = coresight_validate_source(csdev, __func__);
1101 if (ret)
1102 goto out;
1103
1104 if (csdev->enable) {
1105
1106
1107
1108
1109
1110 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
1111 atomic_inc(csdev->refcnt);
1112 goto out;
1113 }
1114
1115 sink = coresight_get_enabled_sink(csdev);
1116 if (!sink) {
1117 ret = -EINVAL;
1118 goto out;
1119 }
1120
1121 path = coresight_build_path(csdev, sink);
1122 if (IS_ERR(path)) {
1123 pr_err("building path(s) failed\n");
1124 ret = PTR_ERR(path);
1125 goto out;
1126 }
1127
1128 ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
1129 if (ret)
1130 goto err_path;
1131
1132 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
1133 if (ret)
1134 goto err_source;
1135
1136 switch (subtype) {
1137 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1138
1139
1140
1141
1142
1143
1144
1145 cpu = source_ops(csdev)->cpu_id(csdev);
1146 per_cpu(tracer_path, cpu) = path;
1147 break;
1148 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1149 stm_path = path;
1150 break;
1151 default:
1152
1153 break;
1154 }
1155
1156out:
1157 mutex_unlock(&coresight_mutex);
1158 return ret;
1159
1160err_source:
1161 coresight_disable_path(path);
1162
1163err_path:
1164 coresight_release_path(path);
1165 goto out;
1166}
1167EXPORT_SYMBOL_GPL(coresight_enable);
1168
1169void coresight_disable(struct coresight_device *csdev)
1170{
1171 int cpu, ret;
1172 struct list_head *path = NULL;
1173
1174 mutex_lock(&coresight_mutex);
1175
1176 ret = coresight_validate_source(csdev, __func__);
1177 if (ret)
1178 goto out;
1179
1180 if (!csdev->enable || !coresight_disable_source(csdev))
1181 goto out;
1182
1183 switch (csdev->subtype.source_subtype) {
1184 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1185 cpu = source_ops(csdev)->cpu_id(csdev);
1186 path = per_cpu(tracer_path, cpu);
1187 per_cpu(tracer_path, cpu) = NULL;
1188 break;
1189 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1190 path = stm_path;
1191 stm_path = NULL;
1192 break;
1193 default:
1194
1195 break;
1196 }
1197
1198 coresight_disable_path(path);
1199 coresight_release_path(path);
1200
1201out:
1202 mutex_unlock(&coresight_mutex);
1203}
1204EXPORT_SYMBOL_GPL(coresight_disable);
1205
1206static ssize_t enable_sink_show(struct device *dev,
1207 struct device_attribute *attr, char *buf)
1208{
1209 struct coresight_device *csdev = to_coresight_device(dev);
1210
1211 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
1212}
1213
1214static ssize_t enable_sink_store(struct device *dev,
1215 struct device_attribute *attr,
1216 const char *buf, size_t size)
1217{
1218 int ret;
1219 unsigned long val;
1220 struct coresight_device *csdev = to_coresight_device(dev);
1221
1222 ret = kstrtoul(buf, 10, &val);
1223 if (ret)
1224 return ret;
1225
1226 if (val)
1227 csdev->activated = true;
1228 else
1229 csdev->activated = false;
1230
1231 return size;
1232
1233}
1234static DEVICE_ATTR_RW(enable_sink);
1235
1236static ssize_t enable_source_show(struct device *dev,
1237 struct device_attribute *attr, char *buf)
1238{
1239 struct coresight_device *csdev = to_coresight_device(dev);
1240
1241 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
1242}
1243
1244static ssize_t enable_source_store(struct device *dev,
1245 struct device_attribute *attr,
1246 const char *buf, size_t size)
1247{
1248 int ret = 0;
1249 unsigned long val;
1250 struct coresight_device *csdev = to_coresight_device(dev);
1251
1252 ret = kstrtoul(buf, 10, &val);
1253 if (ret)
1254 return ret;
1255
1256 if (val) {
1257 ret = coresight_enable(csdev);
1258 if (ret)
1259 return ret;
1260 } else {
1261 coresight_disable(csdev);
1262 }
1263
1264 return size;
1265}
1266static DEVICE_ATTR_RW(enable_source);
1267
1268static struct attribute *coresight_sink_attrs[] = {
1269 &dev_attr_enable_sink.attr,
1270 NULL,
1271};
1272ATTRIBUTE_GROUPS(coresight_sink);
1273
1274static struct attribute *coresight_source_attrs[] = {
1275 &dev_attr_enable_source.attr,
1276 NULL,
1277};
1278ATTRIBUTE_GROUPS(coresight_source);
1279
1280static struct device_type coresight_dev_type[] = {
1281 {
1282 .name = "none",
1283 },
1284 {
1285 .name = "sink",
1286 .groups = coresight_sink_groups,
1287 },
1288 {
1289 .name = "link",
1290 },
1291 {
1292 .name = "linksink",
1293 .groups = coresight_sink_groups,
1294 },
1295 {
1296 .name = "source",
1297 .groups = coresight_source_groups,
1298 },
1299 {
1300 .name = "helper",
1301 },
1302 {
1303 .name = "ect",
1304 },
1305};
1306
1307static void coresight_device_release(struct device *dev)
1308{
1309 struct coresight_device *csdev = to_coresight_device(dev);
1310
1311 fwnode_handle_put(csdev->dev.fwnode);
1312 kfree(csdev->refcnt);
1313 kfree(csdev);
1314}
1315
1316static int coresight_orphan_match(struct device *dev, void *data)
1317{
1318 int i, ret = 0;
1319 bool still_orphan = false;
1320 struct coresight_device *csdev, *i_csdev;
1321 struct coresight_connection *conn;
1322
1323 csdev = data;
1324 i_csdev = to_coresight_device(dev);
1325
1326
1327 if (csdev == i_csdev)
1328 return 0;
1329
1330
1331 if (!i_csdev->orphan)
1332 return 0;
1333
1334
1335
1336
1337 for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
1338 conn = &i_csdev->pdata->conns[i];
1339
1340
1341 if (!conn->child_fwnode)
1342 continue;
1343
1344 if (conn->child_dev == NULL) {
1345
1346 if (conn->child_fwnode == csdev->dev.fwnode) {
1347 ret = coresight_make_links(i_csdev,
1348 conn, csdev);
1349 if (ret)
1350 return ret;
1351 } else {
1352
1353 still_orphan = true;
1354 }
1355 }
1356 }
1357
1358 i_csdev->orphan = still_orphan;
1359
1360
1361
1362
1363
1364 return 0;
1365}
1366
1367static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
1368{
1369 return bus_for_each_dev(&coresight_bustype, NULL,
1370 csdev, coresight_orphan_match);
1371}
1372
1373
1374static int coresight_fixup_device_conns(struct coresight_device *csdev)
1375{
1376 int i, ret = 0;
1377
1378 for (i = 0; i < csdev->pdata->nr_outport; i++) {
1379 struct coresight_connection *conn = &csdev->pdata->conns[i];
1380
1381 if (!conn->child_fwnode)
1382 continue;
1383 conn->child_dev =
1384 coresight_find_csdev_by_fwnode(conn->child_fwnode);
1385 if (conn->child_dev) {
1386 ret = coresight_make_links(csdev, conn,
1387 conn->child_dev);
1388 if (ret)
1389 break;
1390 } else {
1391 csdev->orphan = true;
1392 }
1393 }
1394
1395 return ret;
1396}
1397
1398static int coresight_remove_match(struct device *dev, void *data)
1399{
1400 int i;
1401 struct coresight_device *csdev, *iterator;
1402 struct coresight_connection *conn;
1403
1404 csdev = data;
1405 iterator = to_coresight_device(dev);
1406
1407
1408 if (csdev == iterator)
1409 return 0;
1410
1411
1412
1413
1414
1415 for (i = 0; i < iterator->pdata->nr_outport; i++) {
1416 conn = &iterator->pdata->conns[i];
1417
1418 if (conn->child_dev == NULL || conn->child_fwnode == NULL)
1419 continue;
1420
1421 if (csdev->dev.fwnode == conn->child_fwnode) {
1422 iterator->orphan = true;
1423 coresight_remove_links(iterator, conn);
1424
1425
1426
1427
1428
1429 fwnode_handle_put(conn->child_fwnode);
1430
1431 break;
1432 }
1433 }
1434
1435
1436
1437
1438
1439 return 0;
1440}
1441
1442
1443
1444
1445
1446static void coresight_remove_conns(struct coresight_device *csdev)
1447{
1448
1449
1450
1451
1452
1453
1454 if (csdev->pdata->nr_inport)
1455 bus_for_each_dev(&coresight_bustype, NULL,
1456 csdev, coresight_remove_match);
1457}
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470int coresight_timeout(struct csdev_access *csa, u32 offset,
1471 int position, int value)
1472{
1473 int i;
1474 u32 val;
1475
1476 for (i = TIMEOUT_US; i > 0; i--) {
1477 val = csdev_access_read32(csa, offset);
1478
1479 if (value) {
1480 if (val & BIT(position))
1481 return 0;
1482
1483 } else {
1484 if (!(val & BIT(position)))
1485 return 0;
1486 }
1487
1488
1489
1490
1491
1492
1493 if (i - 1)
1494 udelay(1);
1495 }
1496
1497 return -EAGAIN;
1498}
1499EXPORT_SYMBOL_GPL(coresight_timeout);
1500
1501u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset)
1502{
1503 return csdev_access_relaxed_read32(&csdev->access, offset);
1504}
1505
1506u32 coresight_read32(struct coresight_device *csdev, u32 offset)
1507{
1508 return csdev_access_read32(&csdev->access, offset);
1509}
1510
1511void coresight_relaxed_write32(struct coresight_device *csdev,
1512 u32 val, u32 offset)
1513{
1514 csdev_access_relaxed_write32(&csdev->access, val, offset);
1515}
1516
1517void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset)
1518{
1519 csdev_access_write32(&csdev->access, val, offset);
1520}
1521
1522u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset)
1523{
1524 return csdev_access_relaxed_read64(&csdev->access, offset);
1525}
1526
1527u64 coresight_read64(struct coresight_device *csdev, u32 offset)
1528{
1529 return csdev_access_read64(&csdev->access, offset);
1530}
1531
1532void coresight_relaxed_write64(struct coresight_device *csdev,
1533 u64 val, u32 offset)
1534{
1535 csdev_access_relaxed_write64(&csdev->access, val, offset);
1536}
1537
1538void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset)
1539{
1540 csdev_access_write64(&csdev->access, val, offset);
1541}
1542
1543
1544
1545
1546
1547void coresight_release_platform_data(struct coresight_device *csdev,
1548 struct coresight_platform_data *pdata)
1549{
1550 int i;
1551 struct coresight_connection *conns = pdata->conns;
1552
1553 for (i = 0; i < pdata->nr_outport; i++) {
1554
1555 if (csdev && conns[i].child_dev)
1556 coresight_remove_links(csdev, &conns[i]);
1557
1558
1559
1560
1561 if (conns[i].child_fwnode) {
1562 fwnode_handle_put(conns[i].child_fwnode);
1563 pdata->conns[i].child_fwnode = NULL;
1564 }
1565 }
1566 if (csdev)
1567 coresight_remove_conns_sysfs_group(csdev);
1568}
1569
1570struct coresight_device *coresight_register(struct coresight_desc *desc)
1571{
1572 int ret;
1573 int link_subtype;
1574 int nr_refcnts = 1;
1575 atomic_t *refcnts = NULL;
1576 struct coresight_device *csdev;
1577
1578 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1579 if (!csdev) {
1580 ret = -ENOMEM;
1581 goto err_out;
1582 }
1583
1584 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1585 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1586 link_subtype = desc->subtype.link_subtype;
1587
1588 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1589 nr_refcnts = desc->pdata->nr_inport;
1590 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1591 nr_refcnts = desc->pdata->nr_outport;
1592 }
1593
1594 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1595 if (!refcnts) {
1596 ret = -ENOMEM;
1597 goto err_free_csdev;
1598 }
1599
1600 csdev->refcnt = refcnts;
1601
1602 csdev->pdata = desc->pdata;
1603
1604 csdev->type = desc->type;
1605 csdev->subtype = desc->subtype;
1606 csdev->ops = desc->ops;
1607 csdev->access = desc->access;
1608 csdev->orphan = false;
1609
1610 csdev->dev.type = &coresight_dev_type[desc->type];
1611 csdev->dev.groups = desc->groups;
1612 csdev->dev.parent = desc->dev;
1613 csdev->dev.release = coresight_device_release;
1614 csdev->dev.bus = &coresight_bustype;
1615
1616
1617
1618
1619 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1620 dev_set_name(&csdev->dev, "%s", desc->name);
1621
1622 ret = device_register(&csdev->dev);
1623 if (ret) {
1624 put_device(&csdev->dev);
1625
1626
1627
1628
1629 goto err_out;
1630 }
1631
1632 if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1633 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1634 ret = etm_perf_add_symlink_sink(csdev);
1635
1636 if (ret) {
1637 device_unregister(&csdev->dev);
1638
1639
1640
1641
1642
1643
1644 goto err_out;
1645 }
1646 }
1647
1648 mutex_lock(&coresight_mutex);
1649
1650 ret = coresight_create_conns_sysfs_group(csdev);
1651 if (!ret)
1652 ret = coresight_fixup_device_conns(csdev);
1653 if (!ret)
1654 ret = coresight_fixup_orphan_conns(csdev);
1655 if (!ret && cti_assoc_ops && cti_assoc_ops->add)
1656 cti_assoc_ops->add(csdev);
1657
1658 mutex_unlock(&coresight_mutex);
1659 if (ret) {
1660 coresight_unregister(csdev);
1661 return ERR_PTR(ret);
1662 }
1663
1664 return csdev;
1665
1666err_free_csdev:
1667 kfree(csdev);
1668err_out:
1669
1670 coresight_release_platform_data(NULL, desc->pdata);
1671 return ERR_PTR(ret);
1672}
1673EXPORT_SYMBOL_GPL(coresight_register);
1674
1675void coresight_unregister(struct coresight_device *csdev)
1676{
1677 etm_perf_del_symlink_sink(csdev);
1678
1679 if (cti_assoc_ops && cti_assoc_ops->remove)
1680 cti_assoc_ops->remove(csdev);
1681 coresight_remove_conns(csdev);
1682 coresight_clear_default_sink(csdev);
1683 coresight_release_platform_data(csdev, csdev->pdata);
1684 device_unregister(&csdev->dev);
1685}
1686EXPORT_SYMBOL_GPL(coresight_unregister);
1687
1688
1689
1690
1691
1692
1693
1694
1695static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1696 struct fwnode_handle *fwnode)
1697{
1698 int i;
1699
1700 for (i = 0; i < dict->nr_idx; i++)
1701 if (dict->fwnode_list[i] == fwnode)
1702 return i;
1703 return -ENOENT;
1704}
1705
1706bool coresight_loses_context_with_cpu(struct device *dev)
1707{
1708 return fwnode_property_present(dev_fwnode(dev),
1709 "arm,coresight-loses-context-with-cpu");
1710}
1711EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
1712
1713
1714
1715
1716
1717
1718
1719
1720char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1721 struct device *dev)
1722{
1723 int idx;
1724 char *name = NULL;
1725 struct fwnode_handle **list;
1726
1727 mutex_lock(&coresight_mutex);
1728
1729 idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1730 if (idx < 0) {
1731
1732 idx = dict->nr_idx;
1733 list = krealloc_array(dict->fwnode_list,
1734 idx + 1, sizeof(*dict->fwnode_list),
1735 GFP_KERNEL);
1736 if (ZERO_OR_NULL_PTR(list)) {
1737 idx = -ENOMEM;
1738 goto done;
1739 }
1740
1741 list[idx] = dev_fwnode(dev);
1742 dict->fwnode_list = list;
1743 dict->nr_idx = idx + 1;
1744 }
1745
1746 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1747done:
1748 mutex_unlock(&coresight_mutex);
1749 return name;
1750}
1751EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1752
1753struct bus_type coresight_bustype = {
1754 .name = "coresight",
1755};
1756
1757static int __init coresight_init(void)
1758{
1759 int ret;
1760
1761 ret = bus_register(&coresight_bustype);
1762 if (ret)
1763 return ret;
1764
1765 ret = etm_perf_init();
1766 if (ret)
1767 goto exit_bus_unregister;
1768
1769
1770 ret = cscfg_init();
1771 if (!ret)
1772 return 0;
1773
1774 etm_perf_exit();
1775exit_bus_unregister:
1776 bus_unregister(&coresight_bustype);
1777 return ret;
1778}
1779
1780static void __exit coresight_exit(void)
1781{
1782 cscfg_exit();
1783 etm_perf_exit();
1784 bus_unregister(&coresight_bustype);
1785}
1786
1787module_init(coresight_init);
1788module_exit(coresight_exit);
1789
1790MODULE_LICENSE("GPL v2");
1791MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1792MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1793MODULE_DESCRIPTION("Arm CoreSight tracer driver");
1794