]> Git Repo - linux.git/blob - drivers/hwtracing/coresight/coresight.c
vxlan: Allow configuration of DF behaviour
[linux.git] / drivers / hwtracing / coresight / coresight.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/err.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/clk.h>
16 #include <linux/coresight.h>
17 #include <linux/of_platform.h>
18 #include <linux/delay.h>
19 #include <linux/pm_runtime.h>
20
21 #include "coresight-priv.h"
22
23 static DEFINE_MUTEX(coresight_mutex);
24
25 /**
26  * struct coresight_node - elements of a path, from source to sink
27  * @csdev:      Address of an element.
28  * @link:       hook to the list.
29  */
30 struct coresight_node {
31         struct coresight_device *csdev;
32         struct list_head link;
33 };
34
35 /*
36  * When operating Coresight drivers from the sysFS interface, only a single
37  * path can exist from a tracer (associated to a CPU) to a sink.
38  */
39 static DEFINE_PER_CPU(struct list_head *, tracer_path);
40
41 /*
42  * As of this writing only a single STM can be found in CS topologies.  Since
43  * there is no way to know if we'll ever see more and what kind of
44  * configuration they will enact, for the time being only define a single path
45  * for STM.
46  */
47 static struct list_head *stm_path;
48
49 /*
50  * When losing synchronisation a new barrier packet needs to be inserted at the
51  * beginning of the data collected in a buffer.  That way the decoder knows that
52  * it needs to look for another sync sequence.
53  */
54 const u32 barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
55
56 static int coresight_id_match(struct device *dev, void *data)
57 {
58         int trace_id, i_trace_id;
59         struct coresight_device *csdev, *i_csdev;
60
61         csdev = data;
62         i_csdev = to_coresight_device(dev);
63
64         /*
65          * No need to care about oneself and components that are not
66          * sources or not enabled
67          */
68         if (i_csdev == csdev || !i_csdev->enable ||
69             i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
70                 return 0;
71
72         /* Get the source ID for both compoment */
73         trace_id = source_ops(csdev)->trace_id(csdev);
74         i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
75
76         /* All you need is one */
77         if (trace_id == i_trace_id)
78                 return 1;
79
80         return 0;
81 }
82
83 static int coresight_source_is_unique(struct coresight_device *csdev)
84 {
85         int trace_id = source_ops(csdev)->trace_id(csdev);
86
87         /* this shouldn't happen */
88         if (trace_id < 0)
89                 return 0;
90
91         return !bus_for_each_dev(&coresight_bustype, NULL,
92                                  csdev, coresight_id_match);
93 }
94
95 static int coresight_find_link_inport(struct coresight_device *csdev,
96                                       struct coresight_device *parent)
97 {
98         int i;
99         struct coresight_connection *conn;
100
101         for (i = 0; i < parent->nr_outport; i++) {
102                 conn = &parent->conns[i];
103                 if (conn->child_dev == csdev)
104                         return conn->child_port;
105         }
106
107         dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
108                 dev_name(&parent->dev), dev_name(&csdev->dev));
109
110         return -ENODEV;
111 }
112
113 static int coresight_find_link_outport(struct coresight_device *csdev,
114                                        struct coresight_device *child)
115 {
116         int i;
117         struct coresight_connection *conn;
118
119         for (i = 0; i < csdev->nr_outport; i++) {
120                 conn = &csdev->conns[i];
121                 if (conn->child_dev == child)
122                         return conn->outport;
123         }
124
125         dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
126                 dev_name(&csdev->dev), dev_name(&child->dev));
127
128         return -ENODEV;
129 }
130
131 static inline u32 coresight_read_claim_tags(void __iomem *base)
132 {
133         return readl_relaxed(base + CORESIGHT_CLAIMCLR);
134 }
135
136 static inline bool coresight_is_claimed_self_hosted(void __iomem *base)
137 {
138         return coresight_read_claim_tags(base) == CORESIGHT_CLAIM_SELF_HOSTED;
139 }
140
141 static inline bool coresight_is_claimed_any(void __iomem *base)
142 {
143         return coresight_read_claim_tags(base) != 0;
144 }
145
146 static inline void coresight_set_claim_tags(void __iomem *base)
147 {
148         writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMSET);
149         isb();
150 }
151
152 static inline void coresight_clear_claim_tags(void __iomem *base)
153 {
154         writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMCLR);
155         isb();
156 }
157
158 /*
159  * coresight_claim_device_unlocked : Claim the device for self-hosted usage
160  * to prevent an external tool from touching this device. As per PSCI
161  * standards, section "Preserving the execution context" => "Debug and Trace
162  * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
163  * DBGCLAIM[0] is reserved for external tools.
164  *
165  * Called with CS_UNLOCKed for the component.
166  * Returns : 0 on success
167  */
168 int coresight_claim_device_unlocked(void __iomem *base)
169 {
170         if (coresight_is_claimed_any(base))
171                 return -EBUSY;
172
173         coresight_set_claim_tags(base);
174         if (coresight_is_claimed_self_hosted(base))
175                 return 0;
176         /* There was a race setting the tags, clean up and fail */
177         coresight_clear_claim_tags(base);
178         return -EBUSY;
179 }
180
181 int coresight_claim_device(void __iomem *base)
182 {
183         int rc;
184
185         CS_UNLOCK(base);
186         rc = coresight_claim_device_unlocked(base);
187         CS_LOCK(base);
188
189         return rc;
190 }
191
192 /*
193  * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
194  * Called with CS_UNLOCKed for the component.
195  */
196 void coresight_disclaim_device_unlocked(void __iomem *base)
197 {
198
199         if (coresight_is_claimed_self_hosted(base))
200                 coresight_clear_claim_tags(base);
201         else
202                 /*
203                  * The external agent may have not honoured our claim
204                  * and has manipulated it. Or something else has seriously
205                  * gone wrong in our driver.
206                  */
207                 WARN_ON_ONCE(1);
208 }
209
210 void coresight_disclaim_device(void __iomem *base)
211 {
212         CS_UNLOCK(base);
213         coresight_disclaim_device_unlocked(base);
214         CS_LOCK(base);
215 }
216
217 static int coresight_enable_sink(struct coresight_device *csdev,
218                                  u32 mode, void *data)
219 {
220         int ret;
221
222         /*
223          * We need to make sure the "new" session is compatible with the
224          * existing "mode" of operation.
225          */
226         if (sink_ops(csdev)->enable) {
227                 ret = sink_ops(csdev)->enable(csdev, mode, data);
228                 if (ret)
229                         return ret;
230                 csdev->enable = true;
231         }
232
233         atomic_inc(csdev->refcnt);
234
235         return 0;
236 }
237
238 static void coresight_disable_sink(struct coresight_device *csdev)
239 {
240         if (atomic_dec_return(csdev->refcnt) == 0) {
241                 if (sink_ops(csdev)->disable) {
242                         sink_ops(csdev)->disable(csdev);
243                         csdev->enable = false;
244                 }
245         }
246 }
247
248 static int coresight_enable_link(struct coresight_device *csdev,
249                                  struct coresight_device *parent,
250                                  struct coresight_device *child)
251 {
252         int ret;
253         int link_subtype;
254         int refport, inport, outport;
255
256         if (!parent || !child)
257                 return -EINVAL;
258
259         inport = coresight_find_link_inport(csdev, parent);
260         outport = coresight_find_link_outport(csdev, child);
261         link_subtype = csdev->subtype.link_subtype;
262
263         if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
264                 refport = inport;
265         else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
266                 refport = outport;
267         else
268                 refport = 0;
269
270         if (refport < 0)
271                 return refport;
272
273         if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
274                 if (link_ops(csdev)->enable) {
275                         ret = link_ops(csdev)->enable(csdev, inport, outport);
276                         if (ret) {
277                                 atomic_dec(&csdev->refcnt[refport]);
278                                 return ret;
279                         }
280                 }
281         }
282
283         csdev->enable = true;
284
285         return 0;
286 }
287
288 static void coresight_disable_link(struct coresight_device *csdev,
289                                    struct coresight_device *parent,
290                                    struct coresight_device *child)
291 {
292         int i, nr_conns;
293         int link_subtype;
294         int refport, inport, outport;
295
296         if (!parent || !child)
297                 return;
298
299         inport = coresight_find_link_inport(csdev, parent);
300         outport = coresight_find_link_outport(csdev, child);
301         link_subtype = csdev->subtype.link_subtype;
302
303         if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
304                 refport = inport;
305                 nr_conns = csdev->nr_inport;
306         } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
307                 refport = outport;
308                 nr_conns = csdev->nr_outport;
309         } else {
310                 refport = 0;
311                 nr_conns = 1;
312         }
313
314         if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
315                 if (link_ops(csdev)->disable)
316                         link_ops(csdev)->disable(csdev, inport, outport);
317         }
318
319         for (i = 0; i < nr_conns; i++)
320                 if (atomic_read(&csdev->refcnt[i]) != 0)
321                         return;
322
323         csdev->enable = false;
324 }
325
326 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
327 {
328         int ret;
329
330         if (!coresight_source_is_unique(csdev)) {
331                 dev_warn(&csdev->dev, "traceID %d not unique\n",
332                          source_ops(csdev)->trace_id(csdev));
333                 return -EINVAL;
334         }
335
336         if (!csdev->enable) {
337                 if (source_ops(csdev)->enable) {
338                         ret = source_ops(csdev)->enable(csdev, NULL, mode);
339                         if (ret)
340                                 return ret;
341                 }
342                 csdev->enable = true;
343         }
344
345         atomic_inc(csdev->refcnt);
346
347         return 0;
348 }
349
350 /**
351  *  coresight_disable_source - Drop the reference count by 1 and disable
352  *  the device if there are no users left.
353  *
354  *  @csdev - The coresight device to disable
355  *
356  *  Returns true if the device has been disabled.
357  */
358 static bool coresight_disable_source(struct coresight_device *csdev)
359 {
360         if (atomic_dec_return(csdev->refcnt) == 0) {
361                 if (source_ops(csdev)->disable)
362                         source_ops(csdev)->disable(csdev, NULL);
363                 csdev->enable = false;
364         }
365         return !csdev->enable;
366 }
367
368 /*
369  * coresight_disable_path_from : Disable components in the given path beyond
370  * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
371  * disabled.
372  */
373 static void coresight_disable_path_from(struct list_head *path,
374                                         struct coresight_node *nd)
375 {
376         u32 type;
377         struct coresight_device *csdev, *parent, *child;
378
379         if (!nd)
380                 nd = list_first_entry(path, struct coresight_node, link);
381
382         list_for_each_entry_continue(nd, path, link) {
383                 csdev = nd->csdev;
384                 type = csdev->type;
385
386                 /*
387                  * ETF devices are tricky... They can be a link or a sink,
388                  * depending on how they are configured.  If an ETF has been
389                  * "activated" it will be configured as a sink, otherwise
390                  * go ahead with the link configuration.
391                  */
392                 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
393                         type = (csdev == coresight_get_sink(path)) ?
394                                                 CORESIGHT_DEV_TYPE_SINK :
395                                                 CORESIGHT_DEV_TYPE_LINK;
396
397                 switch (type) {
398                 case CORESIGHT_DEV_TYPE_SINK:
399                         coresight_disable_sink(csdev);
400                         break;
401                 case CORESIGHT_DEV_TYPE_SOURCE:
402                         /*
403                          * We skip the first node in the path assuming that it
404                          * is the source. So we don't expect a source device in
405                          * the middle of a path.
406                          */
407                         WARN_ON(1);
408                         break;
409                 case CORESIGHT_DEV_TYPE_LINK:
410                         parent = list_prev_entry(nd, link)->csdev;
411                         child = list_next_entry(nd, link)->csdev;
412                         coresight_disable_link(csdev, parent, child);
413                         break;
414                 default:
415                         break;
416                 }
417         }
418 }
419
420 void coresight_disable_path(struct list_head *path)
421 {
422         coresight_disable_path_from(path, NULL);
423 }
424
425 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
426 {
427
428         int ret = 0;
429         u32 type;
430         struct coresight_node *nd;
431         struct coresight_device *csdev, *parent, *child;
432
433         list_for_each_entry_reverse(nd, path, link) {
434                 csdev = nd->csdev;
435                 type = csdev->type;
436
437                 /*
438                  * ETF devices are tricky... They can be a link or a sink,
439                  * depending on how they are configured.  If an ETF has been
440                  * "activated" it will be configured as a sink, otherwise
441                  * go ahead with the link configuration.
442                  */
443                 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
444                         type = (csdev == coresight_get_sink(path)) ?
445                                                 CORESIGHT_DEV_TYPE_SINK :
446                                                 CORESIGHT_DEV_TYPE_LINK;
447
448                 switch (type) {
449                 case CORESIGHT_DEV_TYPE_SINK:
450                         ret = coresight_enable_sink(csdev, mode, sink_data);
451                         /*
452                          * Sink is the first component turned on. If we
453                          * failed to enable the sink, there are no components
454                          * that need disabling. Disabling the path here
455                          * would mean we could disrupt an existing session.
456                          */
457                         if (ret)
458                                 goto out;
459                         break;
460                 case CORESIGHT_DEV_TYPE_SOURCE:
461                         /* sources are enabled from either sysFS or Perf */
462                         break;
463                 case CORESIGHT_DEV_TYPE_LINK:
464                         parent = list_prev_entry(nd, link)->csdev;
465                         child = list_next_entry(nd, link)->csdev;
466                         ret = coresight_enable_link(csdev, parent, child);
467                         if (ret)
468                                 goto err;
469                         break;
470                 default:
471                         goto err;
472                 }
473         }
474
475 out:
476         return ret;
477 err:
478         coresight_disable_path_from(path, nd);
479         goto out;
480 }
481
482 struct coresight_device *coresight_get_sink(struct list_head *path)
483 {
484         struct coresight_device *csdev;
485
486         if (!path)
487                 return NULL;
488
489         csdev = list_last_entry(path, struct coresight_node, link)->csdev;
490         if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
491             csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
492                 return NULL;
493
494         return csdev;
495 }
496
497 static int coresight_enabled_sink(struct device *dev, void *data)
498 {
499         bool *reset = data;
500         struct coresight_device *csdev = to_coresight_device(dev);
501
502         if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
503              csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
504              csdev->activated) {
505                 /*
506                  * Now that we have a handle on the sink for this session,
507                  * disable the sysFS "enable_sink" flag so that possible
508                  * concurrent perf session that wish to use another sink don't
509                  * trip on it.  Doing so has no ramification for the current
510                  * session.
511                  */
512                 if (*reset)
513                         csdev->activated = false;
514
515                 return 1;
516         }
517
518         return 0;
519 }
520
521 /**
522  * coresight_get_enabled_sink - returns the first enabled sink found on the bus
523  * @deactivate: Whether the 'enable_sink' flag should be reset
524  *
525  * When operated from perf the deactivate parameter should be set to 'true'.
526  * That way the "enabled_sink" flag of the sink that was selected can be reset,
527  * allowing for other concurrent perf sessions to choose a different sink.
528  *
529  * When operated from sysFS users have full control and as such the deactivate
530  * parameter should be set to 'false', hence mandating users to explicitly
531  * clear the flag.
532  */
533 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
534 {
535         struct device *dev = NULL;
536
537         dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
538                               coresight_enabled_sink);
539
540         return dev ? to_coresight_device(dev) : NULL;
541 }
542
543 /*
544  * coresight_grab_device - Power up this device and any of the helper
545  * devices connected to it for trace operation. Since the helper devices
546  * don't appear on the trace path, they should be handled along with the
547  * the master device.
548  */
549 static void coresight_grab_device(struct coresight_device *csdev)
550 {
551         int i;
552
553         for (i = 0; i < csdev->nr_outport; i++) {
554                 struct coresight_device *child = csdev->conns[i].child_dev;
555
556                 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
557                         pm_runtime_get_sync(child->dev.parent);
558         }
559         pm_runtime_get_sync(csdev->dev.parent);
560 }
561
562 /*
563  * coresight_drop_device - Release this device and any of the helper
564  * devices connected to it.
565  */
566 static void coresight_drop_device(struct coresight_device *csdev)
567 {
568         int i;
569
570         pm_runtime_put(csdev->dev.parent);
571         for (i = 0; i < csdev->nr_outport; i++) {
572                 struct coresight_device *child = csdev->conns[i].child_dev;
573
574                 if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
575                         pm_runtime_put(child->dev.parent);
576         }
577 }
578
579 /**
580  * _coresight_build_path - recursively build a path from a @csdev to a sink.
581  * @csdev:      The device to start from.
582  * @path:       The list to add devices to.
583  *
584  * The tree of Coresight device is traversed until an activated sink is
585  * found.  From there the sink is added to the list along with all the
586  * devices that led to that point - the end result is a list from source
587  * to sink. In that list the source is the first device and the sink the
588  * last one.
589  */
590 static int _coresight_build_path(struct coresight_device *csdev,
591                                  struct coresight_device *sink,
592                                  struct list_head *path)
593 {
594         int i;
595         bool found = false;
596         struct coresight_node *node;
597
598         /* An activated sink has been found.  Enqueue the element */
599         if (csdev == sink)
600                 goto out;
601
602         /* Not a sink - recursively explore each port found on this element */
603         for (i = 0; i < csdev->nr_outport; i++) {
604                 struct coresight_device *child_dev = csdev->conns[i].child_dev;
605
606                 if (child_dev &&
607                     _coresight_build_path(child_dev, sink, path) == 0) {
608                         found = true;
609                         break;
610                 }
611         }
612
613         if (!found)
614                 return -ENODEV;
615
616 out:
617         /*
618          * A path from this element to a sink has been found.  The elements
619          * leading to the sink are already enqueued, all that is left to do
620          * is tell the PM runtime core we need this element and add a node
621          * for it.
622          */
623         node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
624         if (!node)
625                 return -ENOMEM;
626
627         coresight_grab_device(csdev);
628         node->csdev = csdev;
629         list_add(&node->link, path);
630
631         return 0;
632 }
633
634 struct list_head *coresight_build_path(struct coresight_device *source,
635                                        struct coresight_device *sink)
636 {
637         struct list_head *path;
638         int rc;
639
640         if (!sink)
641                 return ERR_PTR(-EINVAL);
642
643         path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
644         if (!path)
645                 return ERR_PTR(-ENOMEM);
646
647         INIT_LIST_HEAD(path);
648
649         rc = _coresight_build_path(source, sink, path);
650         if (rc) {
651                 kfree(path);
652                 return ERR_PTR(rc);
653         }
654
655         return path;
656 }
657
658 /**
659  * coresight_release_path - release a previously built path.
660  * @path:       the path to release.
661  *
662  * Go through all the elements of a path and 1) removed it from the list and
663  * 2) free the memory allocated for each node.
664  */
665 void coresight_release_path(struct list_head *path)
666 {
667         struct coresight_device *csdev;
668         struct coresight_node *nd, *next;
669
670         list_for_each_entry_safe(nd, next, path, link) {
671                 csdev = nd->csdev;
672
673                 coresight_drop_device(csdev);
674                 list_del(&nd->link);
675                 kfree(nd);
676         }
677
678         kfree(path);
679         path = NULL;
680 }
681
682 /** coresight_validate_source - make sure a source has the right credentials
683  *  @csdev:     the device structure for a source.
684  *  @function:  the function this was called from.
685  *
686  * Assumes the coresight_mutex is held.
687  */
688 static int coresight_validate_source(struct coresight_device *csdev,
689                                      const char *function)
690 {
691         u32 type, subtype;
692
693         type = csdev->type;
694         subtype = csdev->subtype.source_subtype;
695
696         if (type != CORESIGHT_DEV_TYPE_SOURCE) {
697                 dev_err(&csdev->dev, "wrong device type in %s\n", function);
698                 return -EINVAL;
699         }
700
701         if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
702             subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
703                 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
704                 return -EINVAL;
705         }
706
707         return 0;
708 }
709
710 int coresight_enable(struct coresight_device *csdev)
711 {
712         int cpu, ret = 0;
713         struct coresight_device *sink;
714         struct list_head *path;
715         enum coresight_dev_subtype_source subtype;
716
717         subtype = csdev->subtype.source_subtype;
718
719         mutex_lock(&coresight_mutex);
720
721         ret = coresight_validate_source(csdev, __func__);
722         if (ret)
723                 goto out;
724
725         if (csdev->enable) {
726                 /*
727                  * There could be multiple applications driving the software
728                  * source. So keep the refcount for each such user when the
729                  * source is already enabled.
730                  */
731                 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
732                         atomic_inc(csdev->refcnt);
733                 goto out;
734         }
735
736         /*
737          * Search for a valid sink for this session but don't reset the
738          * "enable_sink" flag in sysFS.  Users get to do that explicitly.
739          */
740         sink = coresight_get_enabled_sink(false);
741         if (!sink) {
742                 ret = -EINVAL;
743                 goto out;
744         }
745
746         path = coresight_build_path(csdev, sink);
747         if (IS_ERR(path)) {
748                 pr_err("building path(s) failed\n");
749                 ret = PTR_ERR(path);
750                 goto out;
751         }
752
753         ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
754         if (ret)
755                 goto err_path;
756
757         ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
758         if (ret)
759                 goto err_source;
760
761         switch (subtype) {
762         case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
763                 /*
764                  * When working from sysFS it is important to keep track
765                  * of the paths that were created so that they can be
766                  * undone in 'coresight_disable()'.  Since there can only
767                  * be a single session per tracer (when working from sysFS)
768                  * a per-cpu variable will do just fine.
769                  */
770                 cpu = source_ops(csdev)->cpu_id(csdev);
771                 per_cpu(tracer_path, cpu) = path;
772                 break;
773         case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
774                 stm_path = path;
775                 break;
776         default:
777                 /* We can't be here */
778                 break;
779         }
780
781 out:
782         mutex_unlock(&coresight_mutex);
783         return ret;
784
785 err_source:
786         coresight_disable_path(path);
787
788 err_path:
789         coresight_release_path(path);
790         goto out;
791 }
792 EXPORT_SYMBOL_GPL(coresight_enable);
793
794 void coresight_disable(struct coresight_device *csdev)
795 {
796         int cpu, ret;
797         struct list_head *path = NULL;
798
799         mutex_lock(&coresight_mutex);
800
801         ret = coresight_validate_source(csdev, __func__);
802         if (ret)
803                 goto out;
804
805         if (!csdev->enable || !coresight_disable_source(csdev))
806                 goto out;
807
808         switch (csdev->subtype.source_subtype) {
809         case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
810                 cpu = source_ops(csdev)->cpu_id(csdev);
811                 path = per_cpu(tracer_path, cpu);
812                 per_cpu(tracer_path, cpu) = NULL;
813                 break;
814         case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
815                 path = stm_path;
816                 stm_path = NULL;
817                 break;
818         default:
819                 /* We can't be here */
820                 break;
821         }
822
823         coresight_disable_path(path);
824         coresight_release_path(path);
825
826 out:
827         mutex_unlock(&coresight_mutex);
828 }
829 EXPORT_SYMBOL_GPL(coresight_disable);
830
831 static ssize_t enable_sink_show(struct device *dev,
832                                 struct device_attribute *attr, char *buf)
833 {
834         struct coresight_device *csdev = to_coresight_device(dev);
835
836         return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
837 }
838
839 static ssize_t enable_sink_store(struct device *dev,
840                                  struct device_attribute *attr,
841                                  const char *buf, size_t size)
842 {
843         int ret;
844         unsigned long val;
845         struct coresight_device *csdev = to_coresight_device(dev);
846
847         ret = kstrtoul(buf, 10, &val);
848         if (ret)
849                 return ret;
850
851         if (val)
852                 csdev->activated = true;
853         else
854                 csdev->activated = false;
855
856         return size;
857
858 }
859 static DEVICE_ATTR_RW(enable_sink);
860
861 static ssize_t enable_source_show(struct device *dev,
862                                   struct device_attribute *attr, char *buf)
863 {
864         struct coresight_device *csdev = to_coresight_device(dev);
865
866         return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
867 }
868
869 static ssize_t enable_source_store(struct device *dev,
870                                    struct device_attribute *attr,
871                                    const char *buf, size_t size)
872 {
873         int ret = 0;
874         unsigned long val;
875         struct coresight_device *csdev = to_coresight_device(dev);
876
877         ret = kstrtoul(buf, 10, &val);
878         if (ret)
879                 return ret;
880
881         if (val) {
882                 ret = coresight_enable(csdev);
883                 if (ret)
884                         return ret;
885         } else {
886                 coresight_disable(csdev);
887         }
888
889         return size;
890 }
891 static DEVICE_ATTR_RW(enable_source);
892
893 static struct attribute *coresight_sink_attrs[] = {
894         &dev_attr_enable_sink.attr,
895         NULL,
896 };
897 ATTRIBUTE_GROUPS(coresight_sink);
898
899 static struct attribute *coresight_source_attrs[] = {
900         &dev_attr_enable_source.attr,
901         NULL,
902 };
903 ATTRIBUTE_GROUPS(coresight_source);
904
905 static struct device_type coresight_dev_type[] = {
906         {
907                 .name = "none",
908         },
909         {
910                 .name = "sink",
911                 .groups = coresight_sink_groups,
912         },
913         {
914                 .name = "link",
915         },
916         {
917                 .name = "linksink",
918                 .groups = coresight_sink_groups,
919         },
920         {
921                 .name = "source",
922                 .groups = coresight_source_groups,
923         },
924         {
925                 .name = "helper",
926         },
927 };
928
929 static void coresight_device_release(struct device *dev)
930 {
931         struct coresight_device *csdev = to_coresight_device(dev);
932
933         kfree(csdev->conns);
934         kfree(csdev->refcnt);
935         kfree(csdev);
936 }
937
938 static int coresight_orphan_match(struct device *dev, void *data)
939 {
940         int i;
941         bool still_orphan = false;
942         struct coresight_device *csdev, *i_csdev;
943         struct coresight_connection *conn;
944
945         csdev = data;
946         i_csdev = to_coresight_device(dev);
947
948         /* No need to check oneself */
949         if (csdev == i_csdev)
950                 return 0;
951
952         /* Move on to another component if no connection is orphan */
953         if (!i_csdev->orphan)
954                 return 0;
955         /*
956          * Circle throuch all the connection of that component.  If we find
957          * an orphan connection whose name matches @csdev, link it.
958          */
959         for (i = 0; i < i_csdev->nr_outport; i++) {
960                 conn = &i_csdev->conns[i];
961
962                 /* We have found at least one orphan connection */
963                 if (conn->child_dev == NULL) {
964                         /* Does it match this newly added device? */
965                         if (conn->child_name &&
966                             !strcmp(dev_name(&csdev->dev), conn->child_name)) {
967                                 conn->child_dev = csdev;
968                         } else {
969                                 /* This component still has an orphan */
970                                 still_orphan = true;
971                         }
972                 }
973         }
974
975         i_csdev->orphan = still_orphan;
976
977         /*
978          * Returning '0' ensures that all known component on the
979          * bus will be checked.
980          */
981         return 0;
982 }
983
984 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
985 {
986         /*
987          * No need to check for a return value as orphan connection(s)
988          * are hooked-up with each newly added component.
989          */
990         bus_for_each_dev(&coresight_bustype, NULL,
991                          csdev, coresight_orphan_match);
992 }
993
994
995 static void coresight_fixup_device_conns(struct coresight_device *csdev)
996 {
997         int i;
998
999         for (i = 0; i < csdev->nr_outport; i++) {
1000                 struct coresight_connection *conn = &csdev->conns[i];
1001                 struct device *dev = NULL;
1002
1003                 if (conn->child_name)
1004                         dev = bus_find_device_by_name(&coresight_bustype, NULL,
1005                                                       conn->child_name);
1006                 if (dev) {
1007                         conn->child_dev = to_coresight_device(dev);
1008                         /* and put reference from 'bus_find_device()' */
1009                         put_device(dev);
1010                 } else {
1011                         csdev->orphan = true;
1012                         conn->child_dev = NULL;
1013                 }
1014         }
1015 }
1016
1017 static int coresight_remove_match(struct device *dev, void *data)
1018 {
1019         int i;
1020         struct coresight_device *csdev, *iterator;
1021         struct coresight_connection *conn;
1022
1023         csdev = data;
1024         iterator = to_coresight_device(dev);
1025
1026         /* No need to check oneself */
1027         if (csdev == iterator)
1028                 return 0;
1029
1030         /*
1031          * Circle throuch all the connection of that component.  If we find
1032          * a connection whose name matches @csdev, remove it.
1033          */
1034         for (i = 0; i < iterator->nr_outport; i++) {
1035                 conn = &iterator->conns[i];
1036
1037                 if (conn->child_dev == NULL)
1038                         continue;
1039
1040                 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
1041                         iterator->orphan = true;
1042                         conn->child_dev = NULL;
1043                         /* No need to continue */
1044                         break;
1045                 }
1046         }
1047
1048         /*
1049          * Returning '0' ensures that all known component on the
1050          * bus will be checked.
1051          */
1052         return 0;
1053 }
1054
1055 static void coresight_remove_conns(struct coresight_device *csdev)
1056 {
1057         bus_for_each_dev(&coresight_bustype, NULL,
1058                          csdev, coresight_remove_match);
1059 }
1060
1061 /**
1062  * coresight_timeout - loop until a bit has changed to a specific state.
1063  * @addr: base address of the area of interest.
1064  * @offset: address of a register, starting from @addr.
1065  * @position: the position of the bit of interest.
1066  * @value: the value the bit should have.
1067  *
1068  * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1069  * TIMEOUT_US has elapsed, which ever happens first.
1070  */
1071
1072 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
1073 {
1074         int i;
1075         u32 val;
1076
1077         for (i = TIMEOUT_US; i > 0; i--) {
1078                 val = __raw_readl(addr + offset);
1079                 /* waiting on the bit to go from 0 to 1 */
1080                 if (value) {
1081                         if (val & BIT(position))
1082                                 return 0;
1083                 /* waiting on the bit to go from 1 to 0 */
1084                 } else {
1085                         if (!(val & BIT(position)))
1086                                 return 0;
1087                 }
1088
1089                 /*
1090                  * Delay is arbitrary - the specification doesn't say how long
1091                  * we are expected to wait.  Extra check required to make sure
1092                  * we don't wait needlessly on the last iteration.
1093                  */
1094                 if (i - 1)
1095                         udelay(1);
1096         }
1097
1098         return -EAGAIN;
1099 }
1100
1101 struct bus_type coresight_bustype = {
1102         .name   = "coresight",
1103 };
1104
1105 static int __init coresight_init(void)
1106 {
1107         return bus_register(&coresight_bustype);
1108 }
1109 postcore_initcall(coresight_init);
1110
1111 struct coresight_device *coresight_register(struct coresight_desc *desc)
1112 {
1113         int ret;
1114         int link_subtype;
1115         int nr_refcnts = 1;
1116         atomic_t *refcnts = NULL;
1117         struct coresight_device *csdev;
1118
1119         csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1120         if (!csdev) {
1121                 ret = -ENOMEM;
1122                 goto err_out;
1123         }
1124
1125         if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1126             desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1127                 link_subtype = desc->subtype.link_subtype;
1128
1129                 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1130                         nr_refcnts = desc->pdata->nr_inport;
1131                 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1132                         nr_refcnts = desc->pdata->nr_outport;
1133         }
1134
1135         refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1136         if (!refcnts) {
1137                 ret = -ENOMEM;
1138                 goto err_free_csdev;
1139         }
1140
1141         csdev->refcnt = refcnts;
1142
1143         csdev->nr_inport = desc->pdata->nr_inport;
1144         csdev->nr_outport = desc->pdata->nr_outport;
1145
1146         csdev->conns = desc->pdata->conns;
1147
1148         csdev->type = desc->type;
1149         csdev->subtype = desc->subtype;
1150         csdev->ops = desc->ops;
1151         csdev->orphan = false;
1152
1153         csdev->dev.type = &coresight_dev_type[desc->type];
1154         csdev->dev.groups = desc->groups;
1155         csdev->dev.parent = desc->dev;
1156         csdev->dev.release = coresight_device_release;
1157         csdev->dev.bus = &coresight_bustype;
1158         dev_set_name(&csdev->dev, "%s", desc->pdata->name);
1159
1160         ret = device_register(&csdev->dev);
1161         if (ret) {
1162                 put_device(&csdev->dev);
1163                 /*
1164                  * All resources are free'd explicitly via
1165                  * coresight_device_release(), triggered from put_device().
1166                  */
1167                 goto err_out;
1168         }
1169
1170         mutex_lock(&coresight_mutex);
1171
1172         coresight_fixup_device_conns(csdev);
1173         coresight_fixup_orphan_conns(csdev);
1174
1175         mutex_unlock(&coresight_mutex);
1176
1177         return csdev;
1178
1179 err_free_csdev:
1180         kfree(csdev);
1181 err_out:
1182         return ERR_PTR(ret);
1183 }
1184 EXPORT_SYMBOL_GPL(coresight_register);
1185
1186 void coresight_unregister(struct coresight_device *csdev)
1187 {
1188         /* Remove references of that device in the topology */
1189         coresight_remove_conns(csdev);
1190         device_unregister(&csdev->dev);
1191 }
1192 EXPORT_SYMBOL_GPL(coresight_unregister);
This page took 0.10288 seconds and 4 git commands to generate.