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
;
244 * At this point we have a full @path, from source to sink. The
245 * sink is the first entry and the source the last one. Go through
246 * all the components and enable them one by one.
248 list_for_each_entry(cd
, path
, path_link
) {
249 if (cd
== list_first_entry(path
, struct coresight_device
,
251 ret
= coresight_enable_sink(cd
);
252 } else if (list_is_last(&cd
->path_link
, path
)) {
254 * Don't enable the source just yet - this needs to
255 * happen at the very end when all links and sink
256 * along the path have been configured properly.
260 ret
= coresight_enable_link(cd
);
268 list_for_each_entry_continue_reverse(cd
, path
, path_link
) {
269 if (cd
== list_first_entry(path
, struct coresight_device
,
271 coresight_disable_sink(cd
);
272 } else if (list_is_last(&cd
->path_link
, path
)) {
275 coresight_disable_link(cd
);
282 static int coresight_disable_path(struct list_head
*path
)
284 struct coresight_device
*cd
;
286 list_for_each_entry_reverse(cd
, path
, path_link
) {
287 if (cd
== list_first_entry(path
, struct coresight_device
,
289 coresight_disable_sink(cd
);
290 } else if (list_is_last(&cd
->path_link
, path
)) {
292 * The source has already been stopped, no need
293 * to do it again here.
297 coresight_disable_link(cd
);
304 static int coresight_build_paths(struct coresight_device
*csdev
,
305 struct list_head
*path
,
308 int i
, ret
= -EINVAL
;
309 struct coresight_connection
*conn
;
311 list_add(&csdev
->path_link
, path
);
313 if ((csdev
->type
== CORESIGHT_DEV_TYPE_SINK
||
314 csdev
->type
== CORESIGHT_DEV_TYPE_LINKSINK
) &&
317 ret
= coresight_enable_path(path
);
319 ret
= coresight_disable_path(path
);
321 for (i
= 0; i
< csdev
->nr_outport
; i
++) {
322 conn
= &csdev
->conns
[i
];
323 if (coresight_build_paths(conn
->child_dev
,
329 if (list_first_entry(path
, struct coresight_device
, path_link
) != csdev
)
330 dev_err(&csdev
->dev
, "wrong device in %s\n", __func__
);
332 list_del(&csdev
->path_link
);
337 int coresight_enable(struct coresight_device
*csdev
)
342 mutex_lock(&coresight_mutex
);
343 if (csdev
->type
!= CORESIGHT_DEV_TYPE_SOURCE
) {
345 dev_err(&csdev
->dev
, "wrong device type in %s\n", __func__
);
351 if (coresight_build_paths(csdev
, &path
, true)) {
352 dev_err(&csdev
->dev
, "building path(s) failed\n");
356 if (coresight_enable_source(csdev
))
357 dev_err(&csdev
->dev
, "source enable failed\n");
359 mutex_unlock(&coresight_mutex
);
362 EXPORT_SYMBOL_GPL(coresight_enable
);
364 void coresight_disable(struct coresight_device
*csdev
)
368 mutex_lock(&coresight_mutex
);
369 if (csdev
->type
!= CORESIGHT_DEV_TYPE_SOURCE
) {
370 dev_err(&csdev
->dev
, "wrong device type in %s\n", __func__
);
376 coresight_disable_source(csdev
);
377 if (coresight_build_paths(csdev
, &path
, false))
378 dev_err(&csdev
->dev
, "releasing path(s) failed\n");
381 mutex_unlock(&coresight_mutex
);
383 EXPORT_SYMBOL_GPL(coresight_disable
);
385 static ssize_t
enable_sink_show(struct device
*dev
,
386 struct device_attribute
*attr
, char *buf
)
388 struct coresight_device
*csdev
= to_coresight_device(dev
);
390 return scnprintf(buf
, PAGE_SIZE
, "%u\n", (unsigned)csdev
->activated
);
393 static ssize_t
enable_sink_store(struct device
*dev
,
394 struct device_attribute
*attr
,
395 const char *buf
, size_t size
)
399 struct coresight_device
*csdev
= to_coresight_device(dev
);
401 ret
= kstrtoul(buf
, 10, &val
);
406 csdev
->activated
= true;
408 csdev
->activated
= false;
413 static DEVICE_ATTR_RW(enable_sink
);
415 static ssize_t
enable_source_show(struct device
*dev
,
416 struct device_attribute
*attr
, char *buf
)
418 struct coresight_device
*csdev
= to_coresight_device(dev
);
420 return scnprintf(buf
, PAGE_SIZE
, "%u\n", (unsigned)csdev
->enable
);
423 static ssize_t
enable_source_store(struct device
*dev
,
424 struct device_attribute
*attr
,
425 const char *buf
, size_t size
)
429 struct coresight_device
*csdev
= to_coresight_device(dev
);
431 ret
= kstrtoul(buf
, 10, &val
);
436 ret
= coresight_enable(csdev
);
440 coresight_disable(csdev
);
445 static DEVICE_ATTR_RW(enable_source
);
447 static struct attribute
*coresight_sink_attrs
[] = {
448 &dev_attr_enable_sink
.attr
,
451 ATTRIBUTE_GROUPS(coresight_sink
);
453 static struct attribute
*coresight_source_attrs
[] = {
454 &dev_attr_enable_source
.attr
,
457 ATTRIBUTE_GROUPS(coresight_source
);
459 static struct device_type coresight_dev_type
[] = {
465 .groups
= coresight_sink_groups
,
472 .groups
= coresight_sink_groups
,
476 .groups
= coresight_source_groups
,
480 static void coresight_device_release(struct device
*dev
)
482 struct coresight_device
*csdev
= to_coresight_device(dev
);
487 static int coresight_orphan_match(struct device
*dev
, void *data
)
490 bool still_orphan
= false;
491 struct coresight_device
*csdev
, *i_csdev
;
492 struct coresight_connection
*conn
;
495 i_csdev
= to_coresight_device(dev
);
497 /* No need to check oneself */
498 if (csdev
== i_csdev
)
501 /* Move on to another component if no connection is orphan */
502 if (!i_csdev
->orphan
)
505 * Circle throuch all the connection of that component. If we find
506 * an orphan connection whose name matches @csdev, link it.
508 for (i
= 0; i
< i_csdev
->nr_outport
; i
++) {
509 conn
= &i_csdev
->conns
[i
];
511 /* We have found at least one orphan connection */
512 if (conn
->child_dev
== NULL
) {
513 /* Does it match this newly added device? */
514 if (!strcmp(dev_name(&csdev
->dev
), conn
->child_name
)) {
515 conn
->child_dev
= csdev
;
517 /* This component still has an orphan */
523 i_csdev
->orphan
= still_orphan
;
526 * Returning '0' ensures that all known component on the
527 * bus will be checked.
532 static void coresight_fixup_orphan_conns(struct coresight_device
*csdev
)
535 * No need to check for a return value as orphan connection(s)
536 * are hooked-up with each newly added component.
538 bus_for_each_dev(&coresight_bustype
, NULL
,
539 csdev
, coresight_orphan_match
);
543 static int coresight_name_match(struct device
*dev
, void *data
)
546 struct coresight_device
*i_csdev
;
549 i_csdev
= to_coresight_device(dev
);
551 if (!strcmp(to_match
, dev_name(&i_csdev
->dev
)))
557 static void coresight_fixup_device_conns(struct coresight_device
*csdev
)
560 struct device
*dev
= NULL
;
561 struct coresight_connection
*conn
;
563 for (i
= 0; i
< csdev
->nr_outport
; i
++) {
564 conn
= &csdev
->conns
[i
];
565 dev
= bus_find_device(&coresight_bustype
, NULL
,
566 (void *)conn
->child_name
,
567 coresight_name_match
);
570 conn
->child_dev
= to_coresight_device(dev
);
572 csdev
->orphan
= true;
573 conn
->child_dev
= NULL
;
579 * coresight_timeout - loop until a bit has changed to a specific state.
580 * @addr: base address of the area of interest.
581 * @offset: address of a register, starting from @addr.
582 * @position: the position of the bit of interest.
583 * @value: the value the bit should have.
585 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
586 * TIMEOUT_US has elapsed, which ever happens first.
589 int coresight_timeout(void __iomem
*addr
, u32 offset
, int position
, int value
)
594 for (i
= TIMEOUT_US
; i
> 0; i
--) {
595 val
= __raw_readl(addr
+ offset
);
596 /* waiting on the bit to go from 0 to 1 */
598 if (val
& BIT(position
))
600 /* waiting on the bit to go from 1 to 0 */
602 if (!(val
& BIT(position
)))
607 * Delay is arbitrary - the specification doesn't say how long
608 * we are expected to wait. Extra check required to make sure
609 * we don't wait needlessly on the last iteration.
618 struct bus_type coresight_bustype
= {
622 static int __init
coresight_init(void)
624 return bus_register(&coresight_bustype
);
626 postcore_initcall(coresight_init
);
628 struct coresight_device
*coresight_register(struct coresight_desc
*desc
)
634 atomic_t
*refcnts
= NULL
;
635 struct coresight_device
*csdev
;
636 struct coresight_connection
*conns
;
638 csdev
= kzalloc(sizeof(*csdev
), GFP_KERNEL
);
641 goto err_kzalloc_csdev
;
644 if (desc
->type
== CORESIGHT_DEV_TYPE_LINK
||
645 desc
->type
== CORESIGHT_DEV_TYPE_LINKSINK
) {
646 link_subtype
= desc
->subtype
.link_subtype
;
648 if (link_subtype
== CORESIGHT_DEV_SUBTYPE_LINK_MERG
)
649 nr_refcnts
= desc
->pdata
->nr_inport
;
650 else if (link_subtype
== CORESIGHT_DEV_SUBTYPE_LINK_SPLIT
)
651 nr_refcnts
= desc
->pdata
->nr_outport
;
654 refcnts
= kcalloc(nr_refcnts
, sizeof(*refcnts
), GFP_KERNEL
);
657 goto err_kzalloc_refcnts
;
660 csdev
->refcnt
= refcnts
;
662 csdev
->nr_inport
= desc
->pdata
->nr_inport
;
663 csdev
->nr_outport
= desc
->pdata
->nr_outport
;
664 conns
= kcalloc(csdev
->nr_outport
, sizeof(*conns
), GFP_KERNEL
);
667 goto err_kzalloc_conns
;
670 for (i
= 0; i
< csdev
->nr_outport
; i
++) {
671 conns
[i
].outport
= desc
->pdata
->outports
[i
];
672 conns
[i
].child_name
= desc
->pdata
->child_names
[i
];
673 conns
[i
].child_port
= desc
->pdata
->child_ports
[i
];
676 csdev
->conns
= conns
;
678 csdev
->type
= desc
->type
;
679 csdev
->subtype
= desc
->subtype
;
680 csdev
->ops
= desc
->ops
;
681 csdev
->orphan
= false;
683 csdev
->dev
.type
= &coresight_dev_type
[desc
->type
];
684 csdev
->dev
.groups
= desc
->groups
;
685 csdev
->dev
.parent
= desc
->dev
;
686 csdev
->dev
.release
= coresight_device_release
;
687 csdev
->dev
.bus
= &coresight_bustype
;
688 dev_set_name(&csdev
->dev
, "%s", desc
->pdata
->name
);
690 ret
= device_register(&csdev
->dev
);
692 goto err_device_register
;
694 mutex_lock(&coresight_mutex
);
696 coresight_fixup_device_conns(csdev
);
697 coresight_fixup_orphan_conns(csdev
);
699 mutex_unlock(&coresight_mutex
);
712 EXPORT_SYMBOL_GPL(coresight_register
);
714 void coresight_unregister(struct coresight_device
*csdev
)
716 mutex_lock(&coresight_mutex
);
719 device_unregister(&csdev
->dev
);
721 mutex_unlock(&coresight_mutex
);
723 EXPORT_SYMBOL_GPL(coresight_unregister
);
725 MODULE_LICENSE("GPL v2");