coresight: Rename connection members to make the direction explicit
[platform/kernel/linux-starfive.git] / drivers / hwtracing / coresight / coresight-core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2012, The Linux Foundation. All rights reserved.
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/idr.h>
12 #include <linux/err.h>
13 #include <linux/export.h>
14 #include <linux/slab.h>
15 #include <linux/stringhash.h>
16 #include <linux/mutex.h>
17 #include <linux/clk.h>
18 #include <linux/coresight.h>
19 #include <linux/of_platform.h>
20 #include <linux/delay.h>
21 #include <linux/pm_runtime.h>
22
23 #include "coresight-etm-perf.h"
24 #include "coresight-priv.h"
25 #include "coresight-syscfg.h"
26
27 static DEFINE_MUTEX(coresight_mutex);
28 static DEFINE_PER_CPU(struct coresight_device *, csdev_sink);
29
30 /*
31  * Use IDR to map the hash of the source's device name
32  * to the pointer of path for the source. The idr is for
33  * the sources which aren't associated with CPU.
34  */
35 static DEFINE_IDR(path_idr);
36
37 /**
38  * struct coresight_node - elements of a path, from source to sink
39  * @csdev:      Address of an element.
40  * @link:       hook to the list.
41  */
42 struct coresight_node {
43         struct coresight_device *csdev;
44         struct list_head link;
45 };
46
47 /*
48  * When operating Coresight drivers from the sysFS interface, only a single
49  * path can exist from a tracer (associated to a CPU) to a sink.
50  */
51 static DEFINE_PER_CPU(struct list_head *, tracer_path);
52
53 /*
54  * When losing synchronisation a new barrier packet needs to be inserted at the
55  * beginning of the data collected in a buffer.  That way the decoder knows that
56  * it needs to look for another sync sequence.
57  */
58 const u32 coresight_barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
59 EXPORT_SYMBOL_GPL(coresight_barrier_pkt);
60
61 static const struct cti_assoc_op *cti_assoc_ops;
62
63 ssize_t coresight_simple_show_pair(struct device *_dev,
64                               struct device_attribute *attr, char *buf)
65 {
66         struct coresight_device *csdev = container_of(_dev, struct coresight_device, dev);
67         struct cs_pair_attribute *cs_attr = container_of(attr, struct cs_pair_attribute, attr);
68         u64 val;
69
70         pm_runtime_get_sync(_dev->parent);
71         val = csdev_access_relaxed_read_pair(&csdev->access, cs_attr->lo_off, cs_attr->hi_off);
72         pm_runtime_put_sync(_dev->parent);
73         return sysfs_emit(buf, "0x%llx\n", val);
74 }
75 EXPORT_SYMBOL_GPL(coresight_simple_show_pair);
76
77 ssize_t coresight_simple_show32(struct device *_dev,
78                               struct device_attribute *attr, char *buf)
79 {
80         struct coresight_device *csdev = container_of(_dev, struct coresight_device, dev);
81         struct cs_off_attribute *cs_attr = container_of(attr, struct cs_off_attribute, attr);
82         u64 val;
83
84         pm_runtime_get_sync(_dev->parent);
85         val = csdev_access_relaxed_read32(&csdev->access, cs_attr->off);
86         pm_runtime_put_sync(_dev->parent);
87         return sysfs_emit(buf, "0x%llx\n", val);
88 }
89 EXPORT_SYMBOL_GPL(coresight_simple_show32);
90
91 void coresight_set_cti_ops(const struct cti_assoc_op *cti_op)
92 {
93         cti_assoc_ops = cti_op;
94 }
95 EXPORT_SYMBOL_GPL(coresight_set_cti_ops);
96
97 void coresight_remove_cti_ops(void)
98 {
99         cti_assoc_ops = NULL;
100 }
101 EXPORT_SYMBOL_GPL(coresight_remove_cti_ops);
102
103 void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev)
104 {
105         per_cpu(csdev_sink, cpu) = csdev;
106 }
107 EXPORT_SYMBOL_GPL(coresight_set_percpu_sink);
108
109 struct coresight_device *coresight_get_percpu_sink(int cpu)
110 {
111         return per_cpu(csdev_sink, cpu);
112 }
113 EXPORT_SYMBOL_GPL(coresight_get_percpu_sink);
114
115 static int coresight_find_link_inport(struct coresight_device *csdev,
116                                       struct coresight_device *parent)
117 {
118         int i;
119         struct coresight_connection *conn;
120
121         for (i = 0; i < parent->pdata->nr_outconns; i++) {
122                 conn = &parent->pdata->out_conns[i];
123                 if (conn->dest_dev == csdev)
124                         return conn->dest_port;
125         }
126
127         dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
128                 dev_name(&parent->dev), dev_name(&csdev->dev));
129
130         return -ENODEV;
131 }
132
133 static int coresight_find_link_outport(struct coresight_device *csdev,
134                                        struct coresight_device *child)
135 {
136         int i;
137         struct coresight_connection *conn;
138
139         for (i = 0; i < csdev->pdata->nr_outconns; i++) {
140                 conn = &csdev->pdata->out_conns[i];
141                 if (conn->dest_dev == child)
142                         return conn->src_port;
143         }
144
145         dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
146                 dev_name(&csdev->dev), dev_name(&child->dev));
147
148         return -ENODEV;
149 }
150
151 static inline u32 coresight_read_claim_tags(struct coresight_device *csdev)
152 {
153         return csdev_access_relaxed_read32(&csdev->access, CORESIGHT_CLAIMCLR);
154 }
155
156 static inline bool coresight_is_claimed_self_hosted(struct coresight_device *csdev)
157 {
158         return coresight_read_claim_tags(csdev) == CORESIGHT_CLAIM_SELF_HOSTED;
159 }
160
161 static inline bool coresight_is_claimed_any(struct coresight_device *csdev)
162 {
163         return coresight_read_claim_tags(csdev) != 0;
164 }
165
166 static inline void coresight_set_claim_tags(struct coresight_device *csdev)
167 {
168         csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
169                                      CORESIGHT_CLAIMSET);
170         isb();
171 }
172
173 static inline void coresight_clear_claim_tags(struct coresight_device *csdev)
174 {
175         csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
176                                      CORESIGHT_CLAIMCLR);
177         isb();
178 }
179
180 /*
181  * coresight_claim_device_unlocked : Claim the device for self-hosted usage
182  * to prevent an external tool from touching this device. As per PSCI
183  * standards, section "Preserving the execution context" => "Debug and Trace
184  * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
185  * DBGCLAIM[0] is reserved for external tools.
186  *
187  * Called with CS_UNLOCKed for the component.
188  * Returns : 0 on success
189  */
190 int coresight_claim_device_unlocked(struct coresight_device *csdev)
191 {
192         if (WARN_ON(!csdev))
193                 return -EINVAL;
194
195         if (coresight_is_claimed_any(csdev))
196                 return -EBUSY;
197
198         coresight_set_claim_tags(csdev);
199         if (coresight_is_claimed_self_hosted(csdev))
200                 return 0;
201         /* There was a race setting the tags, clean up and fail */
202         coresight_clear_claim_tags(csdev);
203         return -EBUSY;
204 }
205 EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked);
206
207 int coresight_claim_device(struct coresight_device *csdev)
208 {
209         int rc;
210
211         if (WARN_ON(!csdev))
212                 return -EINVAL;
213
214         CS_UNLOCK(csdev->access.base);
215         rc = coresight_claim_device_unlocked(csdev);
216         CS_LOCK(csdev->access.base);
217
218         return rc;
219 }
220 EXPORT_SYMBOL_GPL(coresight_claim_device);
221
222 /*
223  * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
224  * Called with CS_UNLOCKed for the component.
225  */
226 void coresight_disclaim_device_unlocked(struct coresight_device *csdev)
227 {
228
229         if (WARN_ON(!csdev))
230                 return;
231
232         if (coresight_is_claimed_self_hosted(csdev))
233                 coresight_clear_claim_tags(csdev);
234         else
235                 /*
236                  * The external agent may have not honoured our claim
237                  * and has manipulated it. Or something else has seriously
238                  * gone wrong in our driver.
239                  */
240                 WARN_ON_ONCE(1);
241 }
242 EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked);
243
244 void coresight_disclaim_device(struct coresight_device *csdev)
245 {
246         if (WARN_ON(!csdev))
247                 return;
248
249         CS_UNLOCK(csdev->access.base);
250         coresight_disclaim_device_unlocked(csdev);
251         CS_LOCK(csdev->access.base);
252 }
253 EXPORT_SYMBOL_GPL(coresight_disclaim_device);
254
255 /* enable or disable an associated CTI device of the supplied CS device */
256 static int
257 coresight_control_assoc_ectdev(struct coresight_device *csdev, bool enable)
258 {
259         int ect_ret = 0;
260         struct coresight_device *ect_csdev = csdev->ect_dev;
261         struct module *mod;
262
263         if (!ect_csdev)
264                 return 0;
265         if ((!ect_ops(ect_csdev)->enable) || (!ect_ops(ect_csdev)->disable))
266                 return 0;
267
268         mod = ect_csdev->dev.parent->driver->owner;
269         if (enable) {
270                 if (try_module_get(mod)) {
271                         ect_ret = ect_ops(ect_csdev)->enable(ect_csdev);
272                         if (ect_ret) {
273                                 module_put(mod);
274                         } else {
275                                 get_device(ect_csdev->dev.parent);
276                                 csdev->ect_enabled = true;
277                         }
278                 } else
279                         ect_ret = -ENODEV;
280         } else {
281                 if (csdev->ect_enabled) {
282                         ect_ret = ect_ops(ect_csdev)->disable(ect_csdev);
283                         put_device(ect_csdev->dev.parent);
284                         module_put(mod);
285                         csdev->ect_enabled = false;
286                 }
287         }
288
289         /* output warning if ECT enable is preventing trace operation */
290         if (ect_ret)
291                 dev_info(&csdev->dev, "Associated ECT device (%s) %s failed\n",
292                          dev_name(&ect_csdev->dev),
293                          enable ? "enable" : "disable");
294         return ect_ret;
295 }
296
297 /*
298  * Set the associated ect / cti device while holding the coresight_mutex
299  * to avoid a race with coresight_enable that may try to use this value.
300  */
301 void coresight_set_assoc_ectdev_mutex(struct coresight_device *csdev,
302                                       struct coresight_device *ect_csdev)
303 {
304         mutex_lock(&coresight_mutex);
305         csdev->ect_dev = ect_csdev;
306         mutex_unlock(&coresight_mutex);
307 }
308 EXPORT_SYMBOL_GPL(coresight_set_assoc_ectdev_mutex);
309
310 static int coresight_enable_sink(struct coresight_device *csdev,
311                                  enum cs_mode mode, void *data)
312 {
313         int ret;
314
315         /*
316          * We need to make sure the "new" session is compatible with the
317          * existing "mode" of operation.
318          */
319         if (!sink_ops(csdev)->enable)
320                 return -EINVAL;
321
322         ret = coresight_control_assoc_ectdev(csdev, true);
323         if (ret)
324                 return ret;
325         ret = sink_ops(csdev)->enable(csdev, mode, data);
326         if (ret) {
327                 coresight_control_assoc_ectdev(csdev, false);
328                 return ret;
329         }
330         csdev->enable = true;
331
332         return 0;
333 }
334
335 static void coresight_disable_sink(struct coresight_device *csdev)
336 {
337         int ret;
338
339         if (!sink_ops(csdev)->disable)
340                 return;
341
342         ret = sink_ops(csdev)->disable(csdev);
343         if (ret)
344                 return;
345         coresight_control_assoc_ectdev(csdev, false);
346         csdev->enable = false;
347 }
348
349 static int coresight_enable_link(struct coresight_device *csdev,
350                                  struct coresight_device *parent,
351                                  struct coresight_device *child)
352 {
353         int ret = 0;
354         int link_subtype;
355         int inport, outport;
356
357         if (!parent || !child)
358                 return -EINVAL;
359
360         inport = coresight_find_link_inport(csdev, parent);
361         outport = coresight_find_link_outport(csdev, child);
362         link_subtype = csdev->subtype.link_subtype;
363
364         if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && inport < 0)
365                 return inport;
366         if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && outport < 0)
367                 return outport;
368
369         if (link_ops(csdev)->enable) {
370                 ret = coresight_control_assoc_ectdev(csdev, true);
371                 if (!ret) {
372                         ret = link_ops(csdev)->enable(csdev, inport, outport);
373                         if (ret)
374                                 coresight_control_assoc_ectdev(csdev, false);
375                 }
376         }
377
378         if (!ret)
379                 csdev->enable = true;
380
381         return ret;
382 }
383
384 static void coresight_disable_link(struct coresight_device *csdev,
385                                    struct coresight_device *parent,
386                                    struct coresight_device *child)
387 {
388         int i, nr_conns;
389         int link_subtype;
390         int inport, outport;
391
392         if (!parent || !child)
393                 return;
394
395         inport = coresight_find_link_inport(csdev, parent);
396         outport = coresight_find_link_outport(csdev, child);
397         link_subtype = csdev->subtype.link_subtype;
398
399         if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
400                 nr_conns = csdev->pdata->nr_inconns;
401         } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
402                 nr_conns = csdev->pdata->nr_outconns;
403         } else {
404                 nr_conns = 1;
405         }
406
407         if (link_ops(csdev)->disable) {
408                 link_ops(csdev)->disable(csdev, inport, outport);
409                 coresight_control_assoc_ectdev(csdev, false);
410         }
411
412         for (i = 0; i < nr_conns; i++)
413                 if (atomic_read(&csdev->refcnt[i]) != 0)
414                         return;
415
416         csdev->enable = false;
417 }
418
419 static int coresight_enable_source(struct coresight_device *csdev,
420                                    enum cs_mode mode)
421 {
422         int ret;
423
424         if (!csdev->enable) {
425                 if (source_ops(csdev)->enable) {
426                         ret = coresight_control_assoc_ectdev(csdev, true);
427                         if (ret)
428                                 return ret;
429                         ret = source_ops(csdev)->enable(csdev, NULL, mode);
430                         if (ret) {
431                                 coresight_control_assoc_ectdev(csdev, false);
432                                 return ret;
433                         }
434                 }
435                 csdev->enable = true;
436         }
437
438         atomic_inc(csdev->refcnt);
439
440         return 0;
441 }
442
443 /**
444  *  coresight_disable_source - Drop the reference count by 1 and disable
445  *  the device if there are no users left.
446  *
447  *  @csdev: The coresight device to disable
448  *
449  *  Returns true if the device has been disabled.
450  */
451 static bool coresight_disable_source(struct coresight_device *csdev)
452 {
453         if (atomic_dec_return(csdev->refcnt) == 0) {
454                 if (source_ops(csdev)->disable)
455                         source_ops(csdev)->disable(csdev, NULL);
456                 coresight_control_assoc_ectdev(csdev, false);
457                 csdev->enable = false;
458         }
459         return !csdev->enable;
460 }
461
462 /*
463  * coresight_disable_path_from : Disable components in the given path beyond
464  * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
465  * disabled.
466  */
467 static void coresight_disable_path_from(struct list_head *path,
468                                         struct coresight_node *nd)
469 {
470         u32 type;
471         struct coresight_device *csdev, *parent, *child;
472
473         if (!nd)
474                 nd = list_first_entry(path, struct coresight_node, link);
475
476         list_for_each_entry_continue(nd, path, link) {
477                 csdev = nd->csdev;
478                 type = csdev->type;
479
480                 /*
481                  * ETF devices are tricky... They can be a link or a sink,
482                  * depending on how they are configured.  If an ETF has been
483                  * "activated" it will be configured as a sink, otherwise
484                  * go ahead with the link configuration.
485                  */
486                 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
487                         type = (csdev == coresight_get_sink(path)) ?
488                                                 CORESIGHT_DEV_TYPE_SINK :
489                                                 CORESIGHT_DEV_TYPE_LINK;
490
491                 switch (type) {
492                 case CORESIGHT_DEV_TYPE_SINK:
493                         coresight_disable_sink(csdev);
494                         break;
495                 case CORESIGHT_DEV_TYPE_SOURCE:
496                         /*
497                          * We skip the first node in the path assuming that it
498                          * is the source. So we don't expect a source device in
499                          * the middle of a path.
500                          */
501                         WARN_ON(1);
502                         break;
503                 case CORESIGHT_DEV_TYPE_LINK:
504                         parent = list_prev_entry(nd, link)->csdev;
505                         child = list_next_entry(nd, link)->csdev;
506                         coresight_disable_link(csdev, parent, child);
507                         break;
508                 default:
509                         break;
510                 }
511         }
512 }
513
514 void coresight_disable_path(struct list_head *path)
515 {
516         coresight_disable_path_from(path, NULL);
517 }
518 EXPORT_SYMBOL_GPL(coresight_disable_path);
519
520 int coresight_enable_path(struct list_head *path, enum cs_mode mode, void *sink_data)
521 {
522
523         int ret = 0;
524         u32 type;
525         struct coresight_node *nd;
526         struct coresight_device *csdev, *parent, *child;
527
528         list_for_each_entry_reverse(nd, path, link) {
529                 csdev = nd->csdev;
530                 type = csdev->type;
531
532                 /*
533                  * ETF devices are tricky... They can be a link or a sink,
534                  * depending on how they are configured.  If an ETF has been
535                  * "activated" it will be configured as a sink, otherwise
536                  * go ahead with the link configuration.
537                  */
538                 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
539                         type = (csdev == coresight_get_sink(path)) ?
540                                                 CORESIGHT_DEV_TYPE_SINK :
541                                                 CORESIGHT_DEV_TYPE_LINK;
542
543                 switch (type) {
544                 case CORESIGHT_DEV_TYPE_SINK:
545                         ret = coresight_enable_sink(csdev, mode, sink_data);
546                         /*
547                          * Sink is the first component turned on. If we
548                          * failed to enable the sink, there are no components
549                          * that need disabling. Disabling the path here
550                          * would mean we could disrupt an existing session.
551                          */
552                         if (ret)
553                                 goto out;
554                         break;
555                 case CORESIGHT_DEV_TYPE_SOURCE:
556                         /* sources are enabled from either sysFS or Perf */
557                         break;
558                 case CORESIGHT_DEV_TYPE_LINK:
559                         parent = list_prev_entry(nd, link)->csdev;
560                         child = list_next_entry(nd, link)->csdev;
561                         ret = coresight_enable_link(csdev, parent, child);
562                         if (ret)
563                                 goto err;
564                         break;
565                 default:
566                         goto err;
567                 }
568         }
569
570 out:
571         return ret;
572 err:
573         coresight_disable_path_from(path, nd);
574         goto out;
575 }
576
577 struct coresight_device *coresight_get_sink(struct list_head *path)
578 {
579         struct coresight_device *csdev;
580
581         if (!path)
582                 return NULL;
583
584         csdev = list_last_entry(path, struct coresight_node, link)->csdev;
585         if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
586             csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
587                 return NULL;
588
589         return csdev;
590 }
591
592 static struct coresight_device *
593 coresight_find_enabled_sink(struct coresight_device *csdev)
594 {
595         int i;
596         struct coresight_device *sink = NULL;
597
598         if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
599              csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
600              csdev->activated)
601                 return csdev;
602
603         /*
604          * Recursively explore each port found on this element.
605          */
606         for (i = 0; i < csdev->pdata->nr_outconns; i++) {
607                 struct coresight_device *child_dev;
608
609                 child_dev = csdev->pdata->out_conns[i].dest_dev;
610                 if (child_dev)
611                         sink = coresight_find_enabled_sink(child_dev);
612                 if (sink)
613                         return sink;
614         }
615
616         return NULL;
617 }
618
619 /**
620  * coresight_get_enabled_sink - returns the first enabled sink using
621  * connection based search starting from the source reference
622  *
623  * @source: Coresight source device reference
624  */
625 struct coresight_device *
626 coresight_get_enabled_sink(struct coresight_device *source)
627 {
628         if (!source)
629                 return NULL;
630
631         return coresight_find_enabled_sink(source);
632 }
633
634 static int coresight_sink_by_id(struct device *dev, const void *data)
635 {
636         struct coresight_device *csdev = to_coresight_device(dev);
637         unsigned long hash;
638
639         if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
640              csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
641
642                 if (!csdev->ea)
643                         return 0;
644                 /*
645                  * See function etm_perf_add_symlink_sink() to know where
646                  * this comes from.
647                  */
648                 hash = (unsigned long)csdev->ea->var;
649
650                 if ((u32)hash == *(u32 *)data)
651                         return 1;
652         }
653
654         return 0;
655 }
656
657 /**
658  * coresight_get_sink_by_id - returns the sink that matches the id
659  * @id: Id of the sink to match
660  *
661  * The name of a sink is unique, whether it is found on the AMBA bus or
662  * otherwise.  As such the hash of that name can easily be used to identify
663  * a sink.
664  */
665 struct coresight_device *coresight_get_sink_by_id(u32 id)
666 {
667         struct device *dev = NULL;
668
669         dev = bus_find_device(&coresight_bustype, NULL, &id,
670                               coresight_sink_by_id);
671
672         return dev ? to_coresight_device(dev) : NULL;
673 }
674
675 /**
676  * coresight_get_ref- Helper function to increase reference count to module
677  * and device.
678  *
679  * @csdev: The coresight device to get a reference on.
680  *
681  * Return true in successful case and power up the device.
682  * Return false when failed to get reference of module.
683  */
684 static inline bool coresight_get_ref(struct coresight_device *csdev)
685 {
686         struct device *dev = csdev->dev.parent;
687
688         /* Make sure the driver can't be removed */
689         if (!try_module_get(dev->driver->owner))
690                 return false;
691         /* Make sure the device can't go away */
692         get_device(dev);
693         pm_runtime_get_sync(dev);
694         return true;
695 }
696
697 /**
698  * coresight_put_ref- Helper function to decrease reference count to module
699  * and device. Power off the device.
700  *
701  * @csdev: The coresight device to decrement a reference from.
702  */
703 static inline void coresight_put_ref(struct coresight_device *csdev)
704 {
705         struct device *dev = csdev->dev.parent;
706
707         pm_runtime_put(dev);
708         put_device(dev);
709         module_put(dev->driver->owner);
710 }
711
712 /*
713  * coresight_grab_device - Power up this device and any of the helper
714  * devices connected to it for trace operation. Since the helper devices
715  * don't appear on the trace path, they should be handled along with the
716  * master device.
717  */
718 static int coresight_grab_device(struct coresight_device *csdev)
719 {
720         int i;
721
722         for (i = 0; i < csdev->pdata->nr_outconns; i++) {
723                 struct coresight_device *child;
724
725                 child = csdev->pdata->out_conns[i].dest_dev;
726                 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
727                         if (!coresight_get_ref(child))
728                                 goto err;
729         }
730         if (coresight_get_ref(csdev))
731                 return 0;
732 err:
733         for (i--; i >= 0; i--) {
734                 struct coresight_device *child;
735
736                 child = csdev->pdata->out_conns[i].dest_dev;
737                 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
738                         coresight_put_ref(child);
739         }
740         return -ENODEV;
741 }
742
743 /*
744  * coresight_drop_device - Release this device and any of the helper
745  * devices connected to it.
746  */
747 static void coresight_drop_device(struct coresight_device *csdev)
748 {
749         int i;
750
751         coresight_put_ref(csdev);
752         for (i = 0; i < csdev->pdata->nr_outconns; i++) {
753                 struct coresight_device *child;
754
755                 child = csdev->pdata->out_conns[i].dest_dev;
756                 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
757                         coresight_put_ref(child);
758         }
759 }
760
761 /**
762  * _coresight_build_path - recursively build a path from a @csdev to a sink.
763  * @csdev:      The device to start from.
764  * @sink:       The final sink we want in this path.
765  * @path:       The list to add devices to.
766  *
767  * The tree of Coresight device is traversed until an activated sink is
768  * found.  From there the sink is added to the list along with all the
769  * devices that led to that point - the end result is a list from source
770  * to sink. In that list the source is the first device and the sink the
771  * last one.
772  */
773 static int _coresight_build_path(struct coresight_device *csdev,
774                                  struct coresight_device *sink,
775                                  struct list_head *path)
776 {
777         int i, ret;
778         bool found = false;
779         struct coresight_node *node;
780
781         /* An activated sink has been found.  Enqueue the element */
782         if (csdev == sink)
783                 goto out;
784
785         if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&
786             sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {
787                 if (_coresight_build_path(sink, sink, path) == 0) {
788                         found = true;
789                         goto out;
790                 }
791         }
792
793         /* Not a sink - recursively explore each port found on this element */
794         for (i = 0; i < csdev->pdata->nr_outconns; i++) {
795                 struct coresight_device *child_dev;
796
797                 child_dev = csdev->pdata->out_conns[i].dest_dev;
798                 if (child_dev &&
799                     _coresight_build_path(child_dev, sink, path) == 0) {
800                         found = true;
801                         break;
802                 }
803         }
804
805         if (!found)
806                 return -ENODEV;
807
808 out:
809         /*
810          * A path from this element to a sink has been found.  The elements
811          * leading to the sink are already enqueued, all that is left to do
812          * is tell the PM runtime core we need this element and add a node
813          * for it.
814          */
815         ret = coresight_grab_device(csdev);
816         if (ret)
817                 return ret;
818
819         node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
820         if (!node)
821                 return -ENOMEM;
822
823         node->csdev = csdev;
824         list_add(&node->link, path);
825
826         return 0;
827 }
828
829 struct list_head *coresight_build_path(struct coresight_device *source,
830                                        struct coresight_device *sink)
831 {
832         struct list_head *path;
833         int rc;
834
835         if (!sink)
836                 return ERR_PTR(-EINVAL);
837
838         path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
839         if (!path)
840                 return ERR_PTR(-ENOMEM);
841
842         INIT_LIST_HEAD(path);
843
844         rc = _coresight_build_path(source, sink, path);
845         if (rc) {
846                 kfree(path);
847                 return ERR_PTR(rc);
848         }
849
850         return path;
851 }
852
853 /**
854  * coresight_release_path - release a previously built path.
855  * @path:       the path to release.
856  *
857  * Go through all the elements of a path and 1) removed it from the list and
858  * 2) free the memory allocated for each node.
859  */
860 void coresight_release_path(struct list_head *path)
861 {
862         struct coresight_device *csdev;
863         struct coresight_node *nd, *next;
864
865         list_for_each_entry_safe(nd, next, path, link) {
866                 csdev = nd->csdev;
867
868                 coresight_drop_device(csdev);
869                 list_del(&nd->link);
870                 kfree(nd);
871         }
872
873         kfree(path);
874 }
875
876 /* return true if the device is a suitable type for a default sink */
877 static inline bool coresight_is_def_sink_type(struct coresight_device *csdev)
878 {
879         /* sink & correct subtype */
880         if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
881              (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
882             (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
883                 return true;
884         return false;
885 }
886
887 /**
888  * coresight_select_best_sink - return the best sink for use as default from
889  * the two provided.
890  *
891  * @sink:       current best sink.
892  * @depth:      search depth where current sink was found.
893  * @new_sink:   new sink for comparison with current sink.
894  * @new_depth:  search depth where new sink was found.
895  *
896  * Sinks prioritised according to coresight_dev_subtype_sink, with only
897  * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used.
898  *
899  * Where two sinks of equal priority are found, the sink closest to the
900  * source is used (smallest search depth).
901  *
902  * return @new_sink & update @depth if better than @sink, else return @sink.
903  */
904 static struct coresight_device *
905 coresight_select_best_sink(struct coresight_device *sink, int *depth,
906                            struct coresight_device *new_sink, int new_depth)
907 {
908         bool update = false;
909
910         if (!sink) {
911                 /* first found at this level */
912                 update = true;
913         } else if (new_sink->subtype.sink_subtype >
914                    sink->subtype.sink_subtype) {
915                 /* found better sink */
916                 update = true;
917         } else if ((new_sink->subtype.sink_subtype ==
918                     sink->subtype.sink_subtype) &&
919                    (*depth > new_depth)) {
920                 /* found same but closer sink */
921                 update = true;
922         }
923
924         if (update)
925                 *depth = new_depth;
926         return update ? new_sink : sink;
927 }
928
929 /**
930  * coresight_find_sink - recursive function to walk trace connections from
931  * source to find a suitable default sink.
932  *
933  * @csdev: source / current device to check.
934  * @depth: [in] search depth of calling dev, [out] depth of found sink.
935  *
936  * This will walk the connection path from a source (ETM) till a suitable
937  * sink is encountered and return that sink to the original caller.
938  *
939  * If current device is a plain sink return that & depth, otherwise recursively
940  * call child connections looking for a sink. Select best possible using
941  * coresight_select_best_sink.
942  *
943  * return best sink found, or NULL if not found at this node or child nodes.
944  */
945 static struct coresight_device *
946 coresight_find_sink(struct coresight_device *csdev, int *depth)
947 {
948         int i, curr_depth = *depth + 1, found_depth = 0;
949         struct coresight_device *found_sink = NULL;
950
951         if (coresight_is_def_sink_type(csdev)) {
952                 found_depth = curr_depth;
953                 found_sink = csdev;
954                 if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
955                         goto return_def_sink;
956                 /* look past LINKSINK for something better */
957         }
958
959         /*
960          * Not a sink we want - or possible child sink may be better.
961          * recursively explore each port found on this element.
962          */
963         for (i = 0; i < csdev->pdata->nr_outconns; i++) {
964                 struct coresight_device *child_dev, *sink = NULL;
965                 int child_depth = curr_depth;
966
967                 child_dev = csdev->pdata->out_conns[i].dest_dev;
968                 if (child_dev)
969                         sink = coresight_find_sink(child_dev, &child_depth);
970
971                 if (sink)
972                         found_sink = coresight_select_best_sink(found_sink,
973                                                                 &found_depth,
974                                                                 sink,
975                                                                 child_depth);
976         }
977
978 return_def_sink:
979         /* return found sink and depth */
980         if (found_sink)
981                 *depth = found_depth;
982         return found_sink;
983 }
984
985 /**
986  * coresight_find_default_sink: Find a sink suitable for use as a
987  * default sink.
988  *
989  * @csdev: starting source to find a connected sink.
990  *
991  * Walks connections graph looking for a suitable sink to enable for the
992  * supplied source. Uses CoreSight device subtypes and distance from source
993  * to select the best sink.
994  *
995  * If a sink is found, then the default sink for this device is set and
996  * will be automatically used in future.
997  *
998  * Used in cases where the CoreSight user (perf / sysfs) has not selected a
999  * sink.
1000  */
1001 struct coresight_device *
1002 coresight_find_default_sink(struct coresight_device *csdev)
1003 {
1004         int depth = 0;
1005
1006         /* look for a default sink if we have not found for this device */
1007         if (!csdev->def_sink) {
1008                 if (coresight_is_percpu_source(csdev))
1009                         csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));
1010                 if (!csdev->def_sink)
1011                         csdev->def_sink = coresight_find_sink(csdev, &depth);
1012         }
1013         return csdev->def_sink;
1014 }
1015
1016 static int coresight_remove_sink_ref(struct device *dev, void *data)
1017 {
1018         struct coresight_device *sink = data;
1019         struct coresight_device *source = to_coresight_device(dev);
1020
1021         if (source->def_sink == sink)
1022                 source->def_sink = NULL;
1023         return 0;
1024 }
1025
1026 /**
1027  * coresight_clear_default_sink: Remove all default sink references to the
1028  * supplied sink.
1029  *
1030  * If supplied device is a sink, then check all the bus devices and clear
1031  * out all the references to this sink from the coresight_device def_sink
1032  * parameter.
1033  *
1034  * @csdev: coresight sink - remove references to this from all sources.
1035  */
1036 static void coresight_clear_default_sink(struct coresight_device *csdev)
1037 {
1038         if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
1039             (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
1040                 bus_for_each_dev(&coresight_bustype, NULL, csdev,
1041                                  coresight_remove_sink_ref);
1042         }
1043 }
1044
1045 /** coresight_validate_source - make sure a source has the right credentials
1046  *  @csdev:     the device structure for a source.
1047  *  @function:  the function this was called from.
1048  *
1049  * Assumes the coresight_mutex is held.
1050  */
1051 static int coresight_validate_source(struct coresight_device *csdev,
1052                                      const char *function)
1053 {
1054         u32 type, subtype;
1055
1056         type = csdev->type;
1057         subtype = csdev->subtype.source_subtype;
1058
1059         if (type != CORESIGHT_DEV_TYPE_SOURCE) {
1060                 dev_err(&csdev->dev, "wrong device type in %s\n", function);
1061                 return -EINVAL;
1062         }
1063
1064         if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
1065             subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE &&
1066             subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_OTHERS) {
1067                 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
1068                 return -EINVAL;
1069         }
1070
1071         return 0;
1072 }
1073
1074 int coresight_enable(struct coresight_device *csdev)
1075 {
1076         int cpu, ret = 0;
1077         struct coresight_device *sink;
1078         struct list_head *path;
1079         enum coresight_dev_subtype_source subtype;
1080         u32 hash;
1081
1082         subtype = csdev->subtype.source_subtype;
1083
1084         mutex_lock(&coresight_mutex);
1085
1086         ret = coresight_validate_source(csdev, __func__);
1087         if (ret)
1088                 goto out;
1089
1090         if (csdev->enable) {
1091                 /*
1092                  * There could be multiple applications driving the software
1093                  * source. So keep the refcount for each such user when the
1094                  * source is already enabled.
1095                  */
1096                 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
1097                         atomic_inc(csdev->refcnt);
1098                 goto out;
1099         }
1100
1101         sink = coresight_get_enabled_sink(csdev);
1102         if (!sink) {
1103                 ret = -EINVAL;
1104                 goto out;
1105         }
1106
1107         path = coresight_build_path(csdev, sink);
1108         if (IS_ERR(path)) {
1109                 pr_err("building path(s) failed\n");
1110                 ret = PTR_ERR(path);
1111                 goto out;
1112         }
1113
1114         ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
1115         if (ret)
1116                 goto err_path;
1117
1118         ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
1119         if (ret)
1120                 goto err_source;
1121
1122         switch (subtype) {
1123         case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1124                 /*
1125                  * When working from sysFS it is important to keep track
1126                  * of the paths that were created so that they can be
1127                  * undone in 'coresight_disable()'.  Since there can only
1128                  * be a single session per tracer (when working from sysFS)
1129                  * a per-cpu variable will do just fine.
1130                  */
1131                 cpu = source_ops(csdev)->cpu_id(csdev);
1132                 per_cpu(tracer_path, cpu) = path;
1133                 break;
1134         case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1135         case CORESIGHT_DEV_SUBTYPE_SOURCE_OTHERS:
1136                 /*
1137                  * Use the hash of source's device name as ID
1138                  * and map the ID to the pointer of the path.
1139                  */
1140                 hash = hashlen_hash(hashlen_string(NULL, dev_name(&csdev->dev)));
1141                 ret = idr_alloc_u32(&path_idr, path, &hash, hash, GFP_KERNEL);
1142                 if (ret)
1143                         goto err_source;
1144                 break;
1145         default:
1146                 /* We can't be here */
1147                 break;
1148         }
1149
1150 out:
1151         mutex_unlock(&coresight_mutex);
1152         return ret;
1153
1154 err_source:
1155         coresight_disable_path(path);
1156
1157 err_path:
1158         coresight_release_path(path);
1159         goto out;
1160 }
1161 EXPORT_SYMBOL_GPL(coresight_enable);
1162
1163 void coresight_disable(struct coresight_device *csdev)
1164 {
1165         int cpu, ret;
1166         struct list_head *path = NULL;
1167         u32 hash;
1168
1169         mutex_lock(&coresight_mutex);
1170
1171         ret = coresight_validate_source(csdev, __func__);
1172         if (ret)
1173                 goto out;
1174
1175         if (!csdev->enable || !coresight_disable_source(csdev))
1176                 goto out;
1177
1178         switch (csdev->subtype.source_subtype) {
1179         case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1180                 cpu = source_ops(csdev)->cpu_id(csdev);
1181                 path = per_cpu(tracer_path, cpu);
1182                 per_cpu(tracer_path, cpu) = NULL;
1183                 break;
1184         case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1185         case CORESIGHT_DEV_SUBTYPE_SOURCE_OTHERS:
1186                 hash = hashlen_hash(hashlen_string(NULL, dev_name(&csdev->dev)));
1187                 /* Find the path by the hash. */
1188                 path = idr_find(&path_idr, hash);
1189                 if (path == NULL) {
1190                         pr_err("Path is not found for %s\n", dev_name(&csdev->dev));
1191                         goto out;
1192                 }
1193                 idr_remove(&path_idr, hash);
1194                 break;
1195         default:
1196                 /* We can't be here */
1197                 break;
1198         }
1199
1200         coresight_disable_path(path);
1201         coresight_release_path(path);
1202
1203 out:
1204         mutex_unlock(&coresight_mutex);
1205 }
1206 EXPORT_SYMBOL_GPL(coresight_disable);
1207
1208 static ssize_t enable_sink_show(struct device *dev,
1209                                 struct device_attribute *attr, char *buf)
1210 {
1211         struct coresight_device *csdev = to_coresight_device(dev);
1212
1213         return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
1214 }
1215
1216 static ssize_t enable_sink_store(struct device *dev,
1217                                  struct device_attribute *attr,
1218                                  const char *buf, size_t size)
1219 {
1220         int ret;
1221         unsigned long val;
1222         struct coresight_device *csdev = to_coresight_device(dev);
1223
1224         ret = kstrtoul(buf, 10, &val);
1225         if (ret)
1226                 return ret;
1227
1228         if (val)
1229                 csdev->activated = true;
1230         else
1231                 csdev->activated = false;
1232
1233         return size;
1234
1235 }
1236 static DEVICE_ATTR_RW(enable_sink);
1237
1238 static ssize_t enable_source_show(struct device *dev,
1239                                   struct device_attribute *attr, char *buf)
1240 {
1241         struct coresight_device *csdev = to_coresight_device(dev);
1242
1243         return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
1244 }
1245
1246 static ssize_t enable_source_store(struct device *dev,
1247                                    struct device_attribute *attr,
1248                                    const char *buf, size_t size)
1249 {
1250         int ret = 0;
1251         unsigned long val;
1252         struct coresight_device *csdev = to_coresight_device(dev);
1253
1254         ret = kstrtoul(buf, 10, &val);
1255         if (ret)
1256                 return ret;
1257
1258         if (val) {
1259                 ret = coresight_enable(csdev);
1260                 if (ret)
1261                         return ret;
1262         } else {
1263                 coresight_disable(csdev);
1264         }
1265
1266         return size;
1267 }
1268 static DEVICE_ATTR_RW(enable_source);
1269
1270 static struct attribute *coresight_sink_attrs[] = {
1271         &dev_attr_enable_sink.attr,
1272         NULL,
1273 };
1274 ATTRIBUTE_GROUPS(coresight_sink);
1275
1276 static struct attribute *coresight_source_attrs[] = {
1277         &dev_attr_enable_source.attr,
1278         NULL,
1279 };
1280 ATTRIBUTE_GROUPS(coresight_source);
1281
1282 static struct device_type coresight_dev_type[] = {
1283         {
1284                 .name = "sink",
1285                 .groups = coresight_sink_groups,
1286         },
1287         {
1288                 .name = "link",
1289         },
1290         {
1291                 .name = "linksink",
1292                 .groups = coresight_sink_groups,
1293         },
1294         {
1295                 .name = "source",
1296                 .groups = coresight_source_groups,
1297         },
1298         {
1299                 .name = "helper",
1300         },
1301         {
1302                 .name = "ect",
1303         },
1304 };
1305
1306 static void coresight_device_release(struct device *dev)
1307 {
1308         struct coresight_device *csdev = to_coresight_device(dev);
1309
1310         fwnode_handle_put(csdev->dev.fwnode);
1311         kfree(csdev->refcnt);
1312         kfree(csdev);
1313 }
1314
1315 static int coresight_orphan_match(struct device *dev, void *data)
1316 {
1317         int i, ret = 0;
1318         bool still_orphan = false;
1319         struct coresight_device *csdev, *i_csdev;
1320         struct coresight_connection *conn;
1321
1322         csdev = data;
1323         i_csdev = to_coresight_device(dev);
1324
1325         /* No need to check oneself */
1326         if (csdev == i_csdev)
1327                 return 0;
1328
1329         /* Move on to another component if no connection is orphan */
1330         if (!i_csdev->orphan)
1331                 return 0;
1332         /*
1333          * Circle throuch all the connection of that component.  If we find
1334          * an orphan connection whose name matches @csdev, link it.
1335          */
1336         for (i = 0; i < i_csdev->pdata->nr_outconns; i++) {
1337                 conn = &i_csdev->pdata->out_conns[i];
1338
1339                 /* Skip the port if FW doesn't describe it */
1340                 if (!conn->dest_fwnode)
1341                         continue;
1342                 /* We have found at least one orphan connection */
1343                 if (conn->dest_dev == NULL) {
1344                         /* Does it match this newly added device? */
1345                         if (conn->dest_fwnode == csdev->dev.fwnode) {
1346                                 ret = coresight_make_links(i_csdev,
1347                                                            conn, csdev);
1348                                 if (ret)
1349                                         return ret;
1350                         } else {
1351                                 /* This component still has an orphan */
1352                                 still_orphan = true;
1353                         }
1354                 }
1355         }
1356
1357         i_csdev->orphan = still_orphan;
1358
1359         /*
1360          * Returning '0' in case we didn't encounter any error,
1361          * ensures that all known component on the bus will be checked.
1362          */
1363         return 0;
1364 }
1365
1366 static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
1367 {
1368         return bus_for_each_dev(&coresight_bustype, NULL,
1369                          csdev, coresight_orphan_match);
1370 }
1371
1372
1373 static int coresight_fixup_device_conns(struct coresight_device *csdev)
1374 {
1375         int i, ret = 0;
1376
1377         for (i = 0; i < csdev->pdata->nr_outconns; i++) {
1378                 struct coresight_connection *conn = &csdev->pdata->out_conns[i];
1379
1380                 if (!conn->dest_fwnode)
1381                         continue;
1382                 conn->dest_dev =
1383                         coresight_find_csdev_by_fwnode(conn->dest_fwnode);
1384                 if (conn->dest_dev && conn->dest_dev->has_conns_grp) {
1385                         ret = coresight_make_links(csdev, conn, conn->dest_dev);
1386                         if (ret)
1387                                 break;
1388                 } else {
1389                         csdev->orphan = true;
1390                 }
1391         }
1392
1393         return ret;
1394 }
1395
1396 static int coresight_remove_match(struct device *dev, void *data)
1397 {
1398         int i;
1399         struct coresight_device *csdev, *iterator;
1400         struct coresight_connection *conn;
1401
1402         csdev = data;
1403         iterator = to_coresight_device(dev);
1404
1405         /* No need to check oneself */
1406         if (csdev == iterator)
1407                 return 0;
1408
1409         /*
1410          * Circle throuch all the connection of that component.  If we find
1411          * a connection whose name matches @csdev, remove it.
1412          */
1413         for (i = 0; i < iterator->pdata->nr_outconns; i++) {
1414                 conn = &iterator->pdata->out_conns[i];
1415
1416                 if (conn->dest_dev == NULL || conn->dest_fwnode == NULL)
1417                         continue;
1418
1419                 if (csdev->dev.fwnode == conn->dest_fwnode) {
1420                         iterator->orphan = true;
1421                         coresight_remove_links(iterator, conn);
1422
1423                         conn->dest_dev = NULL;
1424                         /* No need to continue */
1425                         break;
1426                 }
1427         }
1428
1429         /*
1430          * Returning '0' ensures that all known component on the
1431          * bus will be checked.
1432          */
1433         return 0;
1434 }
1435
1436 /*
1437  * coresight_remove_conns - Remove references to this given devices
1438  * from the connections of other devices.
1439  */
1440 static void coresight_remove_conns(struct coresight_device *csdev)
1441 {
1442         /*
1443          * Another device will point to this device only if there is
1444          * an output port connected to this one. i.e, if the device
1445          * doesn't have at least one input port, there is no point
1446          * in searching all the devices.
1447          */
1448         if (csdev->pdata->nr_inconns)
1449                 bus_for_each_dev(&coresight_bustype, NULL,
1450                                  csdev, coresight_remove_match);
1451 }
1452
1453 /**
1454  * coresight_timeout - loop until a bit has changed to a specific register
1455  *                      state.
1456  * @csa: coresight device access for the device
1457  * @offset: Offset of the register from the base of the device.
1458  * @position: the position of the bit of interest.
1459  * @value: the value the bit should have.
1460  *
1461  * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1462  * TIMEOUT_US has elapsed, which ever happens first.
1463  */
1464 int coresight_timeout(struct csdev_access *csa, u32 offset,
1465                       int position, int value)
1466 {
1467         int i;
1468         u32 val;
1469
1470         for (i = TIMEOUT_US; i > 0; i--) {
1471                 val = csdev_access_read32(csa, offset);
1472                 /* waiting on the bit to go from 0 to 1 */
1473                 if (value) {
1474                         if (val & BIT(position))
1475                                 return 0;
1476                 /* waiting on the bit to go from 1 to 0 */
1477                 } else {
1478                         if (!(val & BIT(position)))
1479                                 return 0;
1480                 }
1481
1482                 /*
1483                  * Delay is arbitrary - the specification doesn't say how long
1484                  * we are expected to wait.  Extra check required to make sure
1485                  * we don't wait needlessly on the last iteration.
1486                  */
1487                 if (i - 1)
1488                         udelay(1);
1489         }
1490
1491         return -EAGAIN;
1492 }
1493 EXPORT_SYMBOL_GPL(coresight_timeout);
1494
1495 u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset)
1496 {
1497         return csdev_access_relaxed_read32(&csdev->access, offset);
1498 }
1499
1500 u32 coresight_read32(struct coresight_device *csdev, u32 offset)
1501 {
1502         return csdev_access_read32(&csdev->access, offset);
1503 }
1504
1505 void coresight_relaxed_write32(struct coresight_device *csdev,
1506                                u32 val, u32 offset)
1507 {
1508         csdev_access_relaxed_write32(&csdev->access, val, offset);
1509 }
1510
1511 void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset)
1512 {
1513         csdev_access_write32(&csdev->access, val, offset);
1514 }
1515
1516 u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset)
1517 {
1518         return csdev_access_relaxed_read64(&csdev->access, offset);
1519 }
1520
1521 u64 coresight_read64(struct coresight_device *csdev, u32 offset)
1522 {
1523         return csdev_access_read64(&csdev->access, offset);
1524 }
1525
1526 void coresight_relaxed_write64(struct coresight_device *csdev,
1527                                u64 val, u32 offset)
1528 {
1529         csdev_access_relaxed_write64(&csdev->access, val, offset);
1530 }
1531
1532 void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset)
1533 {
1534         csdev_access_write64(&csdev->access, val, offset);
1535 }
1536
1537 /*
1538  * coresight_release_platform_data: Release references to the devices connected
1539  * to the output port of this device.
1540  */
1541 void coresight_release_platform_data(struct coresight_device *csdev,
1542                                      struct coresight_platform_data *pdata)
1543 {
1544         int i;
1545         struct coresight_connection *conns = pdata->out_conns;
1546
1547         for (i = 0; i < pdata->nr_outconns; i++) {
1548                 /* If we have made the links, remove them now */
1549                 if (csdev && conns[i].dest_dev)
1550                         coresight_remove_links(csdev, &conns[i]);
1551                 /*
1552                  * Drop the refcount and clear the handle as this device
1553                  * is going away
1554                  */
1555                 if (conns[i].dest_fwnode) {
1556                         fwnode_handle_put(conns[i].dest_fwnode);
1557                         conns[i].dest_fwnode = NULL;
1558                 }
1559         }
1560         if (csdev)
1561                 coresight_remove_conns_sysfs_group(csdev);
1562 }
1563
1564 struct coresight_device *coresight_register(struct coresight_desc *desc)
1565 {
1566         int ret;
1567         int link_subtype;
1568         int nr_refcnts = 1;
1569         atomic_t *refcnts = NULL;
1570         struct coresight_device *csdev;
1571         bool registered = false;
1572
1573         csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1574         if (!csdev) {
1575                 ret = -ENOMEM;
1576                 goto err_out;
1577         }
1578
1579         if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1580             desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1581                 link_subtype = desc->subtype.link_subtype;
1582
1583                 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1584                         nr_refcnts = desc->pdata->nr_inconns;
1585                 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1586                         nr_refcnts = desc->pdata->nr_outconns;
1587         }
1588
1589         refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1590         if (!refcnts) {
1591                 ret = -ENOMEM;
1592                 kfree(csdev);
1593                 goto err_out;
1594         }
1595
1596         csdev->refcnt = refcnts;
1597
1598         csdev->pdata = desc->pdata;
1599
1600         csdev->type = desc->type;
1601         csdev->subtype = desc->subtype;
1602         csdev->ops = desc->ops;
1603         csdev->access = desc->access;
1604         csdev->orphan = false;
1605
1606         csdev->dev.type = &coresight_dev_type[desc->type];
1607         csdev->dev.groups = desc->groups;
1608         csdev->dev.parent = desc->dev;
1609         csdev->dev.release = coresight_device_release;
1610         csdev->dev.bus = &coresight_bustype;
1611         /*
1612          * Hold the reference to our parent device. This will be
1613          * dropped only in coresight_device_release().
1614          */
1615         csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1616         dev_set_name(&csdev->dev, "%s", desc->name);
1617
1618         /*
1619          * Make sure the device registration and the connection fixup
1620          * are synchronised, so that we don't see uninitialised devices
1621          * on the coresight bus while trying to resolve the connections.
1622          */
1623         mutex_lock(&coresight_mutex);
1624
1625         ret = device_register(&csdev->dev);
1626         if (ret) {
1627                 put_device(&csdev->dev);
1628                 /*
1629                  * All resources are free'd explicitly via
1630                  * coresight_device_release(), triggered from put_device().
1631                  */
1632                 goto out_unlock;
1633         }
1634
1635         if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1636             csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1637                 ret = etm_perf_add_symlink_sink(csdev);
1638
1639                 if (ret) {
1640                         device_unregister(&csdev->dev);
1641                         /*
1642                          * As with the above, all resources are free'd
1643                          * explicitly via coresight_device_release() triggered
1644                          * from put_device(), which is in turn called from
1645                          * function device_unregister().
1646                          */
1647                         goto out_unlock;
1648                 }
1649         }
1650         /* Device is now registered */
1651         registered = true;
1652
1653         ret = coresight_create_conns_sysfs_group(csdev);
1654         if (!ret)
1655                 ret = coresight_fixup_device_conns(csdev);
1656         if (!ret)
1657                 ret = coresight_fixup_orphan_conns(csdev);
1658
1659 out_unlock:
1660         mutex_unlock(&coresight_mutex);
1661         /* Success */
1662         if (!ret) {
1663                 if (cti_assoc_ops && cti_assoc_ops->add)
1664                         cti_assoc_ops->add(csdev);
1665                 return csdev;
1666         }
1667
1668         /* Unregister the device if needed */
1669         if (registered) {
1670                 coresight_unregister(csdev);
1671                 return ERR_PTR(ret);
1672         }
1673
1674 err_out:
1675         /* Cleanup the connection information */
1676         coresight_release_platform_data(NULL, desc->pdata);
1677         return ERR_PTR(ret);
1678 }
1679 EXPORT_SYMBOL_GPL(coresight_register);
1680
1681 void coresight_unregister(struct coresight_device *csdev)
1682 {
1683         etm_perf_del_symlink_sink(csdev);
1684         /* Remove references of that device in the topology */
1685         if (cti_assoc_ops && cti_assoc_ops->remove)
1686                 cti_assoc_ops->remove(csdev);
1687         coresight_remove_conns(csdev);
1688         coresight_clear_default_sink(csdev);
1689         coresight_release_platform_data(csdev, csdev->pdata);
1690         device_unregister(&csdev->dev);
1691 }
1692 EXPORT_SYMBOL_GPL(coresight_unregister);
1693
1694
1695 /*
1696  * coresight_search_device_idx - Search the fwnode handle of a device
1697  * in the given dev_idx list. Must be called with the coresight_mutex held.
1698  *
1699  * Returns the index of the entry, when found. Otherwise, -ENOENT.
1700  */
1701 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1702                                               struct fwnode_handle *fwnode)
1703 {
1704         int i;
1705
1706         for (i = 0; i < dict->nr_idx; i++)
1707                 if (dict->fwnode_list[i] == fwnode)
1708                         return i;
1709         return -ENOENT;
1710 }
1711
1712 bool coresight_loses_context_with_cpu(struct device *dev)
1713 {
1714         return fwnode_property_present(dev_fwnode(dev),
1715                                        "arm,coresight-loses-context-with-cpu");
1716 }
1717 EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
1718
1719 /*
1720  * coresight_alloc_device_name - Get an index for a given device in the
1721  * device index list specific to a driver. An index is allocated for a
1722  * device and is tracked with the fwnode_handle to prevent allocating
1723  * duplicate indices for the same device (e.g, if we defer probing of
1724  * a device due to dependencies), in case the index is requested again.
1725  */
1726 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1727                                   struct device *dev)
1728 {
1729         int idx;
1730         char *name = NULL;
1731         struct fwnode_handle **list;
1732
1733         mutex_lock(&coresight_mutex);
1734
1735         idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1736         if (idx < 0) {
1737                 /* Make space for the new entry */
1738                 idx = dict->nr_idx;
1739                 list = krealloc_array(dict->fwnode_list,
1740                                       idx + 1, sizeof(*dict->fwnode_list),
1741                                       GFP_KERNEL);
1742                 if (ZERO_OR_NULL_PTR(list)) {
1743                         idx = -ENOMEM;
1744                         goto done;
1745                 }
1746
1747                 list[idx] = dev_fwnode(dev);
1748                 dict->fwnode_list = list;
1749                 dict->nr_idx = idx + 1;
1750         }
1751
1752         name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1753 done:
1754         mutex_unlock(&coresight_mutex);
1755         return name;
1756 }
1757 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1758
1759 struct bus_type coresight_bustype = {
1760         .name   = "coresight",
1761 };
1762
1763 static int __init coresight_init(void)
1764 {
1765         int ret;
1766
1767         ret = bus_register(&coresight_bustype);
1768         if (ret)
1769                 return ret;
1770
1771         ret = etm_perf_init();
1772         if (ret)
1773                 goto exit_bus_unregister;
1774
1775         /* initialise the coresight syscfg API */
1776         ret = cscfg_init();
1777         if (!ret)
1778                 return 0;
1779
1780         etm_perf_exit();
1781 exit_bus_unregister:
1782         bus_unregister(&coresight_bustype);
1783         return ret;
1784 }
1785
1786 static void __exit coresight_exit(void)
1787 {
1788         cscfg_exit();
1789         etm_perf_exit();
1790         bus_unregister(&coresight_bustype);
1791 }
1792
1793 module_init(coresight_init);
1794 module_exit(coresight_exit);
1795
1796 MODULE_LICENSE("GPL v2");
1797 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1798 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1799 MODULE_DESCRIPTION("Arm CoreSight tracer driver");