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);
485 kfree(csdev->refcnt);
489 static int coresight_orphan_match(struct device *dev, void *data)
492 bool still_orphan = false;
493 struct coresight_device *csdev, *i_csdev;
494 struct coresight_connection *conn;
497 i_csdev = to_coresight_device(dev);
499 /* No need to check oneself */
500 if (csdev == i_csdev)
503 /* Move on to another component if no connection is orphan */
504 if (!i_csdev->orphan)
507 * Circle throuch all the connection of that component. If we find
508 * an orphan connection whose name matches @csdev, link it.
510 for (i = 0; i < i_csdev->nr_outport; i++) {
511 conn = &i_csdev->conns[i];
513 /* We have found at least one orphan connection */
514 if (conn->child_dev == NULL) {
515 /* Does it match this newly added device? */
516 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
517 conn->child_dev = csdev;
519 /* This component still has an orphan */
525 i_csdev->orphan = still_orphan;
528 * Returning '0' ensures that all known component on the
529 * bus will be checked.
534 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
537 * No need to check for a return value as orphan connection(s)
538 * are hooked-up with each newly added component.
540 bus_for_each_dev(&coresight_bustype, NULL,
541 csdev, coresight_orphan_match);
545 static int coresight_name_match(struct device *dev, void *data)
548 struct coresight_device *i_csdev;
551 i_csdev = to_coresight_device(dev);
553 if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
559 static void coresight_fixup_device_conns(struct coresight_device *csdev)
562 struct device *dev = NULL;
563 struct coresight_connection *conn;
565 for (i = 0; i < csdev->nr_outport; i++) {
566 conn = &csdev->conns[i];
567 dev = bus_find_device(&coresight_bustype, NULL,
568 (void *)conn->child_name,
569 coresight_name_match);
572 conn->child_dev = to_coresight_device(dev);
573 /* and put reference from 'bus_find_device()' */
576 csdev->orphan = true;
577 conn->child_dev = NULL;
582 static int coresight_remove_match(struct device *dev, void *data)
585 struct coresight_device *csdev, *iterator;
586 struct coresight_connection *conn;
589 iterator = to_coresight_device(dev);
591 /* No need to check oneself */
592 if (csdev == iterator)
596 * Circle throuch all the connection of that component. If we find
597 * a connection whose name matches @csdev, remove it.
599 for (i = 0; i < iterator->nr_outport; i++) {
600 conn = &iterator->conns[i];
602 if (conn->child_dev == NULL)
605 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
606 iterator->orphan = true;
607 conn->child_dev = NULL;
608 /* No need to continue */
614 * Returning '0' ensures that all known component on the
615 * bus will be checked.
620 static void coresight_remove_conns(struct coresight_device *csdev)
622 bus_for_each_dev(&coresight_bustype, NULL,
623 csdev, coresight_remove_match);
627 * coresight_timeout - loop until a bit has changed to a specific state.
628 * @addr: base address of the area of interest.
629 * @offset: address of a register, starting from @addr.
630 * @position: the position of the bit of interest.
631 * @value: the value the bit should have.
633 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
634 * TIMEOUT_US has elapsed, which ever happens first.
637 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
642 for (i = TIMEOUT_US; i > 0; i--) {
643 val = __raw_readl(addr + offset);
644 /* waiting on the bit to go from 0 to 1 */
646 if (val & BIT(position))
648 /* waiting on the bit to go from 1 to 0 */
650 if (!(val & BIT(position)))
655 * Delay is arbitrary - the specification doesn't say how long
656 * we are expected to wait. Extra check required to make sure
657 * we don't wait needlessly on the last iteration.
666 struct bus_type coresight_bustype = {
670 static int __init coresight_init(void)
672 return bus_register(&coresight_bustype);
674 postcore_initcall(coresight_init);
676 struct coresight_device *coresight_register(struct coresight_desc *desc)
682 atomic_t *refcnts = NULL;
683 struct coresight_device *csdev;
684 struct coresight_connection *conns;
686 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
689 goto err_kzalloc_csdev;
692 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
693 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
694 link_subtype = desc->subtype.link_subtype;
696 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
697 nr_refcnts = desc->pdata->nr_inport;
698 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
699 nr_refcnts = desc->pdata->nr_outport;
702 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
705 goto err_kzalloc_refcnts;
708 csdev->refcnt = refcnts;
710 csdev->nr_inport = desc->pdata->nr_inport;
711 csdev->nr_outport = desc->pdata->nr_outport;
712 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
715 goto err_kzalloc_conns;
718 for (i = 0; i < csdev->nr_outport; i++) {
719 conns[i].outport = desc->pdata->outports[i];
720 conns[i].child_name = desc->pdata->child_names[i];
721 conns[i].child_port = desc->pdata->child_ports[i];
724 csdev->conns = conns;
726 csdev->type = desc->type;
727 csdev->subtype = desc->subtype;
728 csdev->ops = desc->ops;
729 csdev->orphan = false;
731 csdev->dev.type = &coresight_dev_type[desc->type];
732 csdev->dev.groups = desc->groups;
733 csdev->dev.parent = desc->dev;
734 csdev->dev.release = coresight_device_release;
735 csdev->dev.bus = &coresight_bustype;
736 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
738 ret = device_register(&csdev->dev);
740 goto err_device_register;
742 mutex_lock(&coresight_mutex);
744 coresight_fixup_device_conns(csdev);
745 coresight_fixup_orphan_conns(csdev);
747 mutex_unlock(&coresight_mutex);
760 EXPORT_SYMBOL_GPL(coresight_register);
762 void coresight_unregister(struct coresight_device *csdev)
764 /* Remove references of that device in the topology */
765 coresight_remove_conns(csdev);
766 device_unregister(&csdev->dev);
768 EXPORT_SYMBOL_GPL(coresight_unregister);
770 MODULE_LICENSE("GPL v2");