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)
161 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
162 if (link_ops(csdev)->enable) {
163 ret = link_ops(csdev)->enable(csdev, inport, outport);
169 csdev->enable = true;
174 static void coresight_disable_link(struct coresight_device *csdev)
178 int refport, inport, outport;
180 inport = coresight_find_link_inport(csdev);
181 outport = coresight_find_link_outport(csdev);
182 link_subtype = csdev->subtype.link_subtype;
184 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
186 nr_conns = csdev->nr_inport;
187 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
189 nr_conns = csdev->nr_outport;
195 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
196 if (link_ops(csdev)->disable)
197 link_ops(csdev)->disable(csdev, inport, outport);
200 for (i = 0; i < nr_conns; i++)
201 if (atomic_read(&csdev->refcnt[i]) != 0)
204 csdev->enable = false;
207 static int coresight_enable_source(struct coresight_device *csdev)
211 if (!coresight_source_is_unique(csdev)) {
212 dev_warn(&csdev->dev, "traceID %d not unique\n",
213 source_ops(csdev)->trace_id(csdev));
217 if (!csdev->enable) {
218 if (source_ops(csdev)->enable) {
219 ret = source_ops(csdev)->enable(csdev);
223 csdev->enable = true;
226 atomic_inc(csdev->refcnt);
231 static void coresight_disable_source(struct coresight_device *csdev)
233 if (atomic_dec_return(csdev->refcnt) == 0) {
234 if (source_ops(csdev)->disable) {
235 source_ops(csdev)->disable(csdev);
236 csdev->enable = false;
241 static int coresight_enable_path(struct list_head *path)
244 struct coresight_device *cd;
247 * At this point we have a full @path, from source to sink. The
248 * sink is the first entry and the source the last one. Go through
249 * all the components and enable them one by one.
251 list_for_each_entry(cd, path, path_link) {
252 if (cd == list_first_entry(path, struct coresight_device,
254 ret = coresight_enable_sink(cd);
255 } else if (list_is_last(&cd->path_link, path)) {
257 * Don't enable the source just yet - this needs to
258 * happen at the very end when all links and sink
259 * along the path have been configured properly.
263 ret = coresight_enable_link(cd);
271 list_for_each_entry_continue_reverse(cd, path, path_link) {
272 if (cd == list_first_entry(path, struct coresight_device,
274 coresight_disable_sink(cd);
275 } else if (list_is_last(&cd->path_link, path)) {
278 coresight_disable_link(cd);
285 static int coresight_disable_path(struct list_head *path)
287 struct coresight_device *cd;
289 list_for_each_entry_reverse(cd, path, path_link) {
290 if (cd == list_first_entry(path, struct coresight_device,
292 coresight_disable_sink(cd);
293 } else if (list_is_last(&cd->path_link, path)) {
295 * The source has already been stopped, no need
296 * to do it again here.
300 coresight_disable_link(cd);
307 static int coresight_build_paths(struct coresight_device *csdev,
308 struct list_head *path,
311 int i, ret = -EINVAL;
312 struct coresight_connection *conn;
314 list_add(&csdev->path_link, path);
316 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
317 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
320 ret = coresight_enable_path(path);
322 ret = coresight_disable_path(path);
324 for (i = 0; i < csdev->nr_outport; i++) {
325 conn = &csdev->conns[i];
326 if (coresight_build_paths(conn->child_dev,
332 if (list_first_entry(path, struct coresight_device, path_link) != csdev)
333 dev_err(&csdev->dev, "wrong device in %s\n", __func__);
335 list_del(&csdev->path_link);
340 int coresight_enable(struct coresight_device *csdev)
345 mutex_lock(&coresight_mutex);
346 if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
348 dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
354 if (coresight_build_paths(csdev, &path, true)) {
355 dev_err(&csdev->dev, "building path(s) failed\n");
359 if (coresight_enable_source(csdev))
360 dev_err(&csdev->dev, "source enable failed\n");
362 mutex_unlock(&coresight_mutex);
365 EXPORT_SYMBOL_GPL(coresight_enable);
367 void coresight_disable(struct coresight_device *csdev)
371 mutex_lock(&coresight_mutex);
372 if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
373 dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
379 coresight_disable_source(csdev);
380 if (coresight_build_paths(csdev, &path, false))
381 dev_err(&csdev->dev, "releasing path(s) failed\n");
384 mutex_unlock(&coresight_mutex);
386 EXPORT_SYMBOL_GPL(coresight_disable);
388 static ssize_t enable_sink_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct coresight_device *csdev = to_coresight_device(dev);
393 return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->activated);
396 static ssize_t enable_sink_store(struct device *dev,
397 struct device_attribute *attr,
398 const char *buf, size_t size)
402 struct coresight_device *csdev = to_coresight_device(dev);
404 ret = kstrtoul(buf, 10, &val);
409 csdev->activated = true;
411 csdev->activated = false;
416 static DEVICE_ATTR_RW(enable_sink);
418 static ssize_t enable_source_show(struct device *dev,
419 struct device_attribute *attr, char *buf)
421 struct coresight_device *csdev = to_coresight_device(dev);
423 return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->enable);
426 static ssize_t enable_source_store(struct device *dev,
427 struct device_attribute *attr,
428 const char *buf, size_t size)
432 struct coresight_device *csdev = to_coresight_device(dev);
434 ret = kstrtoul(buf, 10, &val);
439 ret = coresight_enable(csdev);
443 coresight_disable(csdev);
448 static DEVICE_ATTR_RW(enable_source);
450 static struct attribute *coresight_sink_attrs[] = {
451 &dev_attr_enable_sink.attr,
454 ATTRIBUTE_GROUPS(coresight_sink);
456 static struct attribute *coresight_source_attrs[] = {
457 &dev_attr_enable_source.attr,
460 ATTRIBUTE_GROUPS(coresight_source);
462 static struct device_type coresight_dev_type[] = {
468 .groups = coresight_sink_groups,
475 .groups = coresight_sink_groups,
479 .groups = coresight_source_groups,
483 static void coresight_device_release(struct device *dev)
485 struct coresight_device *csdev = to_coresight_device(dev);
488 kfree(csdev->refcnt);
492 static int coresight_orphan_match(struct device *dev, void *data)
495 bool still_orphan = false;
496 struct coresight_device *csdev, *i_csdev;
497 struct coresight_connection *conn;
500 i_csdev = to_coresight_device(dev);
502 /* No need to check oneself */
503 if (csdev == i_csdev)
506 /* Move on to another component if no connection is orphan */
507 if (!i_csdev->orphan)
510 * Circle throuch all the connection of that component. If we find
511 * an orphan connection whose name matches @csdev, link it.
513 for (i = 0; i < i_csdev->nr_outport; i++) {
514 conn = &i_csdev->conns[i];
516 /* We have found at least one orphan connection */
517 if (conn->child_dev == NULL) {
518 /* Does it match this newly added device? */
519 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
520 conn->child_dev = csdev;
522 /* This component still has an orphan */
528 i_csdev->orphan = still_orphan;
531 * Returning '0' ensures that all known component on the
532 * bus will be checked.
537 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
540 * No need to check for a return value as orphan connection(s)
541 * are hooked-up with each newly added component.
543 bus_for_each_dev(&coresight_bustype, NULL,
544 csdev, coresight_orphan_match);
548 static int coresight_name_match(struct device *dev, void *data)
551 struct coresight_device *i_csdev;
554 i_csdev = to_coresight_device(dev);
556 if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
562 static void coresight_fixup_device_conns(struct coresight_device *csdev)
565 struct device *dev = NULL;
566 struct coresight_connection *conn;
568 for (i = 0; i < csdev->nr_outport; i++) {
569 conn = &csdev->conns[i];
570 dev = bus_find_device(&coresight_bustype, NULL,
571 (void *)conn->child_name,
572 coresight_name_match);
575 conn->child_dev = to_coresight_device(dev);
576 /* and put reference from 'bus_find_device()' */
579 csdev->orphan = true;
580 conn->child_dev = NULL;
585 static int coresight_remove_match(struct device *dev, void *data)
588 struct coresight_device *csdev, *iterator;
589 struct coresight_connection *conn;
592 iterator = to_coresight_device(dev);
594 /* No need to check oneself */
595 if (csdev == iterator)
599 * Circle throuch all the connection of that component. If we find
600 * a connection whose name matches @csdev, remove it.
602 for (i = 0; i < iterator->nr_outport; i++) {
603 conn = &iterator->conns[i];
605 if (conn->child_dev == NULL)
608 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
609 iterator->orphan = true;
610 conn->child_dev = NULL;
611 /* No need to continue */
617 * Returning '0' ensures that all known component on the
618 * bus will be checked.
623 static void coresight_remove_conns(struct coresight_device *csdev)
625 bus_for_each_dev(&coresight_bustype, NULL,
626 csdev, coresight_remove_match);
630 * coresight_timeout - loop until a bit has changed to a specific state.
631 * @addr: base address of the area of interest.
632 * @offset: address of a register, starting from @addr.
633 * @position: the position of the bit of interest.
634 * @value: the value the bit should have.
636 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
637 * TIMEOUT_US has elapsed, which ever happens first.
640 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
645 for (i = TIMEOUT_US; i > 0; i--) {
646 val = __raw_readl(addr + offset);
647 /* waiting on the bit to go from 0 to 1 */
649 if (val & BIT(position))
651 /* waiting on the bit to go from 1 to 0 */
653 if (!(val & BIT(position)))
658 * Delay is arbitrary - the specification doesn't say how long
659 * we are expected to wait. Extra check required to make sure
660 * we don't wait needlessly on the last iteration.
669 struct bus_type coresight_bustype = {
673 static int __init coresight_init(void)
675 return bus_register(&coresight_bustype);
677 postcore_initcall(coresight_init);
679 struct coresight_device *coresight_register(struct coresight_desc *desc)
685 atomic_t *refcnts = NULL;
686 struct coresight_device *csdev;
687 struct coresight_connection *conns;
689 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
692 goto err_kzalloc_csdev;
695 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
696 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
697 link_subtype = desc->subtype.link_subtype;
699 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
700 nr_refcnts = desc->pdata->nr_inport;
701 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
702 nr_refcnts = desc->pdata->nr_outport;
705 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
708 goto err_kzalloc_refcnts;
711 csdev->refcnt = refcnts;
713 csdev->nr_inport = desc->pdata->nr_inport;
714 csdev->nr_outport = desc->pdata->nr_outport;
715 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
718 goto err_kzalloc_conns;
721 for (i = 0; i < csdev->nr_outport; i++) {
722 conns[i].outport = desc->pdata->outports[i];
723 conns[i].child_name = desc->pdata->child_names[i];
724 conns[i].child_port = desc->pdata->child_ports[i];
727 csdev->conns = conns;
729 csdev->type = desc->type;
730 csdev->subtype = desc->subtype;
731 csdev->ops = desc->ops;
732 csdev->orphan = false;
734 csdev->dev.type = &coresight_dev_type[desc->type];
735 csdev->dev.groups = desc->groups;
736 csdev->dev.parent = desc->dev;
737 csdev->dev.release = coresight_device_release;
738 csdev->dev.bus = &coresight_bustype;
739 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
741 ret = device_register(&csdev->dev);
743 goto err_device_register;
745 mutex_lock(&coresight_mutex);
747 coresight_fixup_device_conns(csdev);
748 coresight_fixup_orphan_conns(csdev);
750 mutex_unlock(&coresight_mutex);
763 EXPORT_SYMBOL_GPL(coresight_register);
765 void coresight_unregister(struct coresight_device *csdev)
767 /* Remove references of that device in the topology */
768 coresight_remove_conns(csdev);
769 device_unregister(&csdev->dev);
771 EXPORT_SYMBOL_GPL(coresight_unregister);
773 MODULE_LICENSE("GPL v2");