1 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/export.h>
21 #include <linux/slab.h>
22 #include <linux/mutex.h>
23 #include <linux/clk.h>
24 #include <linux/coresight.h>
25 #include <linux/of_platform.h>
26 #include <linux/delay.h>
28 #include "coresight-priv.h"
30 static DEFINE_MUTEX(coresight_mutex
);
32 static int coresight_id_match(struct device
*dev
, void *data
)
34 int trace_id
, i_trace_id
;
35 struct coresight_device
*csdev
, *i_csdev
;
38 i_csdev
= to_coresight_device(dev
);
41 * No need to care about oneself and components that are not
42 * sources or not enabled
44 if (i_csdev
== csdev
|| !i_csdev
->enable
||
45 i_csdev
->type
!= CORESIGHT_DEV_TYPE_SOURCE
)
48 /* Get the source ID for both compoment */
49 trace_id
= source_ops(csdev
)->trace_id(csdev
);
50 i_trace_id
= source_ops(i_csdev
)->trace_id(i_csdev
);
52 /* All you need is one */
53 if (trace_id
== i_trace_id
)
59 static int coresight_source_is_unique(struct coresight_device
*csdev
)
61 int trace_id
= source_ops(csdev
)->trace_id(csdev
);
63 /* this shouldn't happen */
67 return !bus_for_each_dev(&coresight_bustype
, NULL
,
68 csdev
, coresight_id_match
);
71 static int coresight_find_link_inport(struct coresight_device
*csdev
)
74 struct coresight_device
*parent
;
75 struct coresight_connection
*conn
;
77 parent
= container_of(csdev
->path_link
.next
,
78 struct coresight_device
, path_link
);
80 for (i
= 0; i
< parent
->nr_outport
; i
++) {
81 conn
= &parent
->conns
[i
];
82 if (conn
->child_dev
== csdev
)
83 return conn
->child_port
;
86 dev_err(&csdev
->dev
, "couldn't find inport, parent: %s, child: %s\n",
87 dev_name(&parent
->dev
), dev_name(&csdev
->dev
));
92 static int coresight_find_link_outport(struct coresight_device
*csdev
)
95 struct coresight_device
*child
;
96 struct coresight_connection
*conn
;
98 child
= container_of(csdev
->path_link
.prev
,
99 struct coresight_device
, path_link
);
101 for (i
= 0; i
< csdev
->nr_outport
; i
++) {
102 conn
= &csdev
->conns
[i
];
103 if (conn
->child_dev
== child
)
104 return conn
->outport
;
107 dev_err(&csdev
->dev
, "couldn't find outport, parent: %s, child: %s\n",
108 dev_name(&csdev
->dev
), dev_name(&child
->dev
));
113 static int coresight_enable_sink(struct coresight_device
*csdev
)
117 if (!csdev
->enable
) {
118 if (sink_ops(csdev
)->enable
) {
119 ret
= sink_ops(csdev
)->enable(csdev
);
123 csdev
->enable
= true;
126 atomic_inc(csdev
->refcnt
);
131 static void coresight_disable_sink(struct coresight_device
*csdev
)
133 if (atomic_dec_return(csdev
->refcnt
) == 0) {
134 if (sink_ops(csdev
)->disable
) {
135 sink_ops(csdev
)->disable(csdev
);
136 csdev
->enable
= false;
141 static int coresight_enable_link(struct coresight_device
*csdev
)
145 int refport
, inport
, outport
;
147 inport
= coresight_find_link_inport(csdev
);
148 outport
= coresight_find_link_outport(csdev
);
149 link_subtype
= csdev
->subtype
.link_subtype
;
151 if (link_subtype
== CORESIGHT_DEV_SUBTYPE_LINK_MERG
)
153 else if (link_subtype
== CORESIGHT_DEV_SUBTYPE_LINK_SPLIT
)
158 if (atomic_inc_return(&csdev
->refcnt
[refport
]) == 1) {
159 if (link_ops(csdev
)->enable
) {
160 ret
= link_ops(csdev
)->enable(csdev
, inport
, outport
);
166 csdev
->enable
= true;
171 static void coresight_disable_link(struct coresight_device
*csdev
)
175 int refport
, inport
, outport
;
177 inport
= coresight_find_link_inport(csdev
);
178 outport
= coresight_find_link_outport(csdev
);
179 link_subtype
= csdev
->subtype
.link_subtype
;
181 if (link_subtype
== CORESIGHT_DEV_SUBTYPE_LINK_MERG
) {
183 nr_conns
= csdev
->nr_inport
;
184 } else if (link_subtype
== CORESIGHT_DEV_SUBTYPE_LINK_SPLIT
) {
186 nr_conns
= csdev
->nr_outport
;
192 if (atomic_dec_return(&csdev
->refcnt
[refport
]) == 0) {
193 if (link_ops(csdev
)->disable
)
194 link_ops(csdev
)->disable(csdev
, inport
, outport
);
197 for (i
= 0; i
< nr_conns
; i
++)
198 if (atomic_read(&csdev
->refcnt
[i
]) != 0)
201 csdev
->enable
= false;
204 static int coresight_enable_source(struct coresight_device
*csdev
)
208 if (!coresight_source_is_unique(csdev
)) {
209 dev_warn(&csdev
->dev
, "traceID %d not unique\n",
210 source_ops(csdev
)->trace_id(csdev
));
214 if (!csdev
->enable
) {
215 if (source_ops(csdev
)->enable
) {
216 ret
= source_ops(csdev
)->enable(csdev
);
220 csdev
->enable
= true;
223 atomic_inc(csdev
->refcnt
);
228 static void coresight_disable_source(struct coresight_device
*csdev
)
230 if (atomic_dec_return(csdev
->refcnt
) == 0) {
231 if (source_ops(csdev
)->disable
) {
232 source_ops(csdev
)->disable(csdev
);
233 csdev
->enable
= false;
238 static int coresight_enable_path(struct list_head
*path
)
241 struct coresight_device
*cd
;
243 list_for_each_entry(cd
, path
, path_link
) {
244 if (cd
== list_first_entry(path
, struct coresight_device
,
246 ret
= coresight_enable_sink(cd
);
247 } else if (list_is_last(&cd
->path_link
, path
)) {
249 * Don't enable the source just yet - this needs to
250 * happen at the very end when all links and sink
251 * along the path have been configured properly.
255 ret
= coresight_enable_link(cd
);
263 list_for_each_entry_continue_reverse(cd
, path
, path_link
) {
264 if (cd
== list_first_entry(path
, struct coresight_device
,
266 coresight_disable_sink(cd
);
267 } else if (list_is_last(&cd
->path_link
, path
)) {
270 coresight_disable_link(cd
);
277 static int coresight_disable_path(struct list_head
*path
)
279 struct coresight_device
*cd
;
281 list_for_each_entry_reverse(cd
, path
, path_link
) {
282 if (cd
== list_first_entry(path
, struct coresight_device
,
284 coresight_disable_sink(cd
);
285 } else if (list_is_last(&cd
->path_link
, path
)) {
287 * The source has already been stopped, no need
288 * to do it again here.
292 coresight_disable_link(cd
);
299 static int coresight_build_paths(struct coresight_device
*csdev
,
300 struct list_head
*path
,
303 int i
, ret
= -EINVAL
;
304 struct coresight_connection
*conn
;
306 list_add(&csdev
->path_link
, path
);
308 if (csdev
->type
== CORESIGHT_DEV_TYPE_SINK
&& csdev
->activated
) {
310 ret
= coresight_enable_path(path
);
312 ret
= coresight_disable_path(path
);
314 for (i
= 0; i
< csdev
->nr_outport
; i
++) {
315 conn
= &csdev
->conns
[i
];
316 if (coresight_build_paths(conn
->child_dev
,
322 if (list_first_entry(path
, struct coresight_device
, path_link
) != csdev
)
323 dev_err(&csdev
->dev
, "wrong device in %s\n", __func__
);
325 list_del(&csdev
->path_link
);
330 int coresight_enable(struct coresight_device
*csdev
)
335 mutex_lock(&coresight_mutex
);
336 if (csdev
->type
!= CORESIGHT_DEV_TYPE_SOURCE
) {
338 dev_err(&csdev
->dev
, "wrong device type in %s\n", __func__
);
344 if (coresight_build_paths(csdev
, &path
, true)) {
345 dev_err(&csdev
->dev
, "building path(s) failed\n");
349 if (coresight_enable_source(csdev
))
350 dev_err(&csdev
->dev
, "source enable failed\n");
352 mutex_unlock(&coresight_mutex
);
355 EXPORT_SYMBOL_GPL(coresight_enable
);
357 void coresight_disable(struct coresight_device
*csdev
)
361 mutex_lock(&coresight_mutex
);
362 if (csdev
->type
!= CORESIGHT_DEV_TYPE_SOURCE
) {
363 dev_err(&csdev
->dev
, "wrong device type in %s\n", __func__
);
369 coresight_disable_source(csdev
);
370 if (coresight_build_paths(csdev
, &path
, false))
371 dev_err(&csdev
->dev
, "releasing path(s) failed\n");
374 mutex_unlock(&coresight_mutex
);
376 EXPORT_SYMBOL_GPL(coresight_disable
);
378 static ssize_t
enable_sink_show(struct device
*dev
,
379 struct device_attribute
*attr
, char *buf
)
381 struct coresight_device
*csdev
= to_coresight_device(dev
);
383 return scnprintf(buf
, PAGE_SIZE
, "%u\n", (unsigned)csdev
->activated
);
386 static ssize_t
enable_sink_store(struct device
*dev
,
387 struct device_attribute
*attr
,
388 const char *buf
, size_t size
)
392 struct coresight_device
*csdev
= to_coresight_device(dev
);
394 ret
= kstrtoul(buf
, 10, &val
);
399 csdev
->activated
= true;
401 csdev
->activated
= false;
406 static DEVICE_ATTR_RW(enable_sink
);
408 static ssize_t
enable_source_show(struct device
*dev
,
409 struct device_attribute
*attr
, char *buf
)
411 struct coresight_device
*csdev
= to_coresight_device(dev
);
413 return scnprintf(buf
, PAGE_SIZE
, "%u\n", (unsigned)csdev
->enable
);
416 static ssize_t
enable_source_store(struct device
*dev
,
417 struct device_attribute
*attr
,
418 const char *buf
, size_t size
)
422 struct coresight_device
*csdev
= to_coresight_device(dev
);
424 ret
= kstrtoul(buf
, 10, &val
);
429 ret
= coresight_enable(csdev
);
433 coresight_disable(csdev
);
438 static DEVICE_ATTR_RW(enable_source
);
440 static struct attribute
*coresight_sink_attrs
[] = {
441 &dev_attr_enable_sink
.attr
,
444 ATTRIBUTE_GROUPS(coresight_sink
);
446 static struct attribute
*coresight_source_attrs
[] = {
447 &dev_attr_enable_source
.attr
,
450 ATTRIBUTE_GROUPS(coresight_source
);
452 static struct device_type coresight_dev_type
[] = {
458 .groups
= coresight_sink_groups
,
465 .groups
= coresight_sink_groups
,
469 .groups
= coresight_source_groups
,
473 static void coresight_device_release(struct device
*dev
)
475 struct coresight_device
*csdev
= to_coresight_device(dev
);
480 static int coresight_orphan_match(struct device
*dev
, void *data
)
483 bool still_orphan
= false;
484 struct coresight_device
*csdev
, *i_csdev
;
485 struct coresight_connection
*conn
;
488 i_csdev
= to_coresight_device(dev
);
490 /* No need to check oneself */
491 if (csdev
== i_csdev
)
494 /* Move on to another component if no connection is orphan */
495 if (!i_csdev
->orphan
)
498 * Circle throuch all the connection of that component. If we find
499 * an orphan connection whose name matches @csdev, link it.
501 for (i
= 0; i
< i_csdev
->nr_outport
; i
++) {
502 conn
= &i_csdev
->conns
[i
];
504 /* We have found at least one orphan connection */
505 if (conn
->child_dev
== NULL
) {
506 /* Does it match this newly added device? */
507 if (!strcmp(dev_name(&csdev
->dev
), conn
->child_name
)) {
508 conn
->child_dev
= csdev
;
510 /* This component still has an orphan */
516 i_csdev
->orphan
= still_orphan
;
519 * Returning '0' ensures that all known component on the
520 * bus will be checked.
525 static void coresight_fixup_orphan_conns(struct coresight_device
*csdev
)
528 * No need to check for a return value as orphan connection(s)
529 * are hooked-up with each newly added component.
531 bus_for_each_dev(&coresight_bustype
, NULL
,
532 csdev
, coresight_orphan_match
);
536 static int coresight_name_match(struct device
*dev
, void *data
)
539 struct coresight_device
*i_csdev
;
542 i_csdev
= to_coresight_device(dev
);
544 if (!strcmp(to_match
, dev_name(&i_csdev
->dev
)))
550 static void coresight_fixup_device_conns(struct coresight_device
*csdev
)
553 struct device
*dev
= NULL
;
554 struct coresight_connection
*conn
;
556 for (i
= 0; i
< csdev
->nr_outport
; i
++) {
557 conn
= &csdev
->conns
[i
];
558 dev
= bus_find_device(&coresight_bustype
, NULL
,
559 (void *)conn
->child_name
,
560 coresight_name_match
);
563 conn
->child_dev
= to_coresight_device(dev
);
565 csdev
->orphan
= true;
566 conn
->child_dev
= NULL
;
572 * coresight_timeout - loop until a bit has changed to a specific state.
573 * @addr: base address of the area of interest.
574 * @offset: address of a register, starting from @addr.
575 * @position: the position of the bit of interest.
576 * @value: the value the bit should have.
578 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
579 * TIMEOUT_US has elapsed, which ever happens first.
582 int coresight_timeout(void __iomem
*addr
, u32 offset
, int position
, int value
)
587 for (i
= TIMEOUT_US
; i
> 0; i
--) {
588 val
= __raw_readl(addr
+ offset
);
589 /* waiting on the bit to go from 0 to 1 */
591 if (val
& BIT(position
))
593 /* waiting on the bit to go from 1 to 0 */
595 if (!(val
& BIT(position
)))
600 * Delay is arbitrary - the specification doesn't say how long
601 * we are expected to wait. Extra check required to make sure
602 * we don't wait needlessly on the last iteration.
611 struct bus_type coresight_bustype
= {
615 static int __init
coresight_init(void)
617 return bus_register(&coresight_bustype
);
619 postcore_initcall(coresight_init
);
621 struct coresight_device
*coresight_register(struct coresight_desc
*desc
)
627 atomic_t
*refcnts
= NULL
;
628 struct coresight_device
*csdev
;
629 struct coresight_connection
*conns
;
631 csdev
= kzalloc(sizeof(*csdev
), GFP_KERNEL
);
634 goto err_kzalloc_csdev
;
637 if (desc
->type
== CORESIGHT_DEV_TYPE_LINK
||
638 desc
->type
== CORESIGHT_DEV_TYPE_LINKSINK
) {
639 link_subtype
= desc
->subtype
.link_subtype
;
641 if (link_subtype
== CORESIGHT_DEV_SUBTYPE_LINK_MERG
)
642 nr_refcnts
= desc
->pdata
->nr_inport
;
643 else if (link_subtype
== CORESIGHT_DEV_SUBTYPE_LINK_SPLIT
)
644 nr_refcnts
= desc
->pdata
->nr_outport
;
647 refcnts
= kcalloc(nr_refcnts
, sizeof(*refcnts
), GFP_KERNEL
);
650 goto err_kzalloc_refcnts
;
653 csdev
->refcnt
= refcnts
;
655 csdev
->nr_inport
= desc
->pdata
->nr_inport
;
656 csdev
->nr_outport
= desc
->pdata
->nr_outport
;
657 conns
= kcalloc(csdev
->nr_outport
, sizeof(*conns
), GFP_KERNEL
);
660 goto err_kzalloc_conns
;
663 for (i
= 0; i
< csdev
->nr_outport
; i
++) {
664 conns
[i
].outport
= desc
->pdata
->outports
[i
];
665 conns
[i
].child_name
= desc
->pdata
->child_names
[i
];
666 conns
[i
].child_port
= desc
->pdata
->child_ports
[i
];
669 csdev
->conns
= conns
;
671 csdev
->type
= desc
->type
;
672 csdev
->subtype
= desc
->subtype
;
673 csdev
->ops
= desc
->ops
;
674 csdev
->orphan
= false;
676 csdev
->dev
.type
= &coresight_dev_type
[desc
->type
];
677 csdev
->dev
.groups
= desc
->groups
;
678 csdev
->dev
.parent
= desc
->dev
;
679 csdev
->dev
.release
= coresight_device_release
;
680 csdev
->dev
.bus
= &coresight_bustype
;
681 dev_set_name(&csdev
->dev
, "%s", desc
->pdata
->name
);
683 ret
= device_register(&csdev
->dev
);
685 goto err_device_register
;
687 mutex_lock(&coresight_mutex
);
689 coresight_fixup_device_conns(csdev
);
690 coresight_fixup_orphan_conns(csdev
);
692 mutex_unlock(&coresight_mutex
);
705 EXPORT_SYMBOL_GPL(coresight_register
);
707 void coresight_unregister(struct coresight_device
*csdev
)
709 mutex_lock(&coresight_mutex
);
712 device_unregister(&csdev
->dev
);
714 mutex_unlock(&coresight_mutex
);
716 EXPORT_SYMBOL_GPL(coresight_unregister
);
718 MODULE_LICENSE("GPL v2");