of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / hwtracing / coresight / coresight.c
blobe25492137d8bec86a5ff406ff1025724df095fbd
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>
18 #include <linux/io.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;
37 csdev = data;
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)
46 return 0;
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)
54 return 1;
56 return 0;
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 */
64 if (trace_id < 0)
65 return 0;
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)
73 int i;
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));
89 return 0;
92 static int coresight_find_link_outport(struct coresight_device *csdev)
94 int i;
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));
110 return 0;
113 static int coresight_enable_sink(struct coresight_device *csdev)
115 int ret;
117 if (!csdev->enable) {
118 if (sink_ops(csdev)->enable) {
119 ret = sink_ops(csdev)->enable(csdev);
120 if (ret)
121 return ret;
123 csdev->enable = true;
126 atomic_inc(csdev->refcnt);
128 return 0;
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)
143 int ret;
144 int link_subtype;
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)
152 refport = inport;
153 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
154 refport = outport;
155 else
156 refport = 0;
158 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
159 if (link_ops(csdev)->enable) {
160 ret = link_ops(csdev)->enable(csdev, inport, outport);
161 if (ret)
162 return ret;
166 csdev->enable = true;
168 return 0;
171 static void coresight_disable_link(struct coresight_device *csdev)
173 int i, nr_conns;
174 int link_subtype;
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) {
182 refport = inport;
183 nr_conns = csdev->nr_inport;
184 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
185 refport = outport;
186 nr_conns = csdev->nr_outport;
187 } else {
188 refport = 0;
189 nr_conns = 1;
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)
199 return;
201 csdev->enable = false;
204 static int coresight_enable_source(struct coresight_device *csdev)
206 int ret;
208 if (!coresight_source_is_unique(csdev)) {
209 dev_warn(&csdev->dev, "traceID %d not unique\n",
210 source_ops(csdev)->trace_id(csdev));
211 return -EINVAL;
214 if (!csdev->enable) {
215 if (source_ops(csdev)->enable) {
216 ret = source_ops(csdev)->enable(csdev);
217 if (ret)
218 return ret;
220 csdev->enable = true;
223 atomic_inc(csdev->refcnt);
225 return 0;
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)
240 int ret = 0;
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,
250 path_link)) {
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.
259 } else {
260 ret = coresight_enable_link(cd);
262 if (ret)
263 goto err;
266 return 0;
267 err:
268 list_for_each_entry_continue_reverse(cd, path, path_link) {
269 if (cd == list_first_entry(path, struct coresight_device,
270 path_link)) {
271 coresight_disable_sink(cd);
272 } else if (list_is_last(&cd->path_link, path)) {
274 } else {
275 coresight_disable_link(cd);
279 return ret;
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,
288 path_link)) {
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.
296 } else {
297 coresight_disable_link(cd);
301 return 0;
304 static int coresight_build_paths(struct coresight_device *csdev,
305 struct list_head *path,
306 bool enable)
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) &&
315 csdev->activated) {
316 if (enable)
317 ret = coresight_enable_path(path);
318 else
319 ret = coresight_disable_path(path);
320 } else {
321 for (i = 0; i < csdev->nr_outport; i++) {
322 conn = &csdev->conns[i];
323 if (coresight_build_paths(conn->child_dev,
324 path, enable) == 0)
325 ret = 0;
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);
334 return ret;
337 int coresight_enable(struct coresight_device *csdev)
339 int ret = 0;
340 LIST_HEAD(path);
342 mutex_lock(&coresight_mutex);
343 if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
344 ret = -EINVAL;
345 dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
346 goto out;
348 if (csdev->enable)
349 goto out;
351 if (coresight_build_paths(csdev, &path, true)) {
352 dev_err(&csdev->dev, "building path(s) failed\n");
353 goto out;
356 if (coresight_enable_source(csdev))
357 dev_err(&csdev->dev, "source enable failed\n");
358 out:
359 mutex_unlock(&coresight_mutex);
360 return ret;
362 EXPORT_SYMBOL_GPL(coresight_enable);
364 void coresight_disable(struct coresight_device *csdev)
366 LIST_HEAD(path);
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__);
371 goto out;
373 if (!csdev->enable)
374 goto out;
376 coresight_disable_source(csdev);
377 if (coresight_build_paths(csdev, &path, false))
378 dev_err(&csdev->dev, "releasing path(s) failed\n");
380 out:
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)
397 int ret;
398 unsigned long val;
399 struct coresight_device *csdev = to_coresight_device(dev);
401 ret = kstrtoul(buf, 10, &val);
402 if (ret)
403 return ret;
405 if (val)
406 csdev->activated = true;
407 else
408 csdev->activated = false;
410 return size;
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)
427 int ret = 0;
428 unsigned long val;
429 struct coresight_device *csdev = to_coresight_device(dev);
431 ret = kstrtoul(buf, 10, &val);
432 if (ret)
433 return ret;
435 if (val) {
436 ret = coresight_enable(csdev);
437 if (ret)
438 return ret;
439 } else {
440 coresight_disable(csdev);
443 return size;
445 static DEVICE_ATTR_RW(enable_source);
447 static struct attribute *coresight_sink_attrs[] = {
448 &dev_attr_enable_sink.attr,
449 NULL,
451 ATTRIBUTE_GROUPS(coresight_sink);
453 static struct attribute *coresight_source_attrs[] = {
454 &dev_attr_enable_source.attr,
455 NULL,
457 ATTRIBUTE_GROUPS(coresight_source);
459 static struct device_type coresight_dev_type[] = {
461 .name = "none",
464 .name = "sink",
465 .groups = coresight_sink_groups,
468 .name = "link",
471 .name = "linksink",
472 .groups = coresight_sink_groups,
475 .name = "source",
476 .groups = coresight_source_groups,
480 static void coresight_device_release(struct device *dev)
482 struct coresight_device *csdev = to_coresight_device(dev);
484 kfree(csdev);
487 static int coresight_orphan_match(struct device *dev, void *data)
489 int i;
490 bool still_orphan = false;
491 struct coresight_device *csdev, *i_csdev;
492 struct coresight_connection *conn;
494 csdev = data;
495 i_csdev = to_coresight_device(dev);
497 /* No need to check oneself */
498 if (csdev == i_csdev)
499 return 0;
501 /* Move on to another component if no connection is orphan */
502 if (!i_csdev->orphan)
503 return 0;
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;
516 } else {
517 /* This component still has an orphan */
518 still_orphan = true;
523 i_csdev->orphan = still_orphan;
526 * Returning '0' ensures that all known component on the
527 * bus will be checked.
529 return 0;
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)
545 char *to_match;
546 struct coresight_device *i_csdev;
548 to_match = data;
549 i_csdev = to_coresight_device(dev);
551 if (!strcmp(to_match, dev_name(&i_csdev->dev)))
552 return 1;
554 return 0;
557 static void coresight_fixup_device_conns(struct coresight_device *csdev)
559 int i;
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);
569 if (dev) {
570 conn->child_dev = to_coresight_device(dev);
571 } else {
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)
591 int i;
592 u32 val;
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 */
597 if (value) {
598 if (val & BIT(position))
599 return 0;
600 /* waiting on the bit to go from 1 to 0 */
601 } else {
602 if (!(val & BIT(position)))
603 return 0;
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.
611 if (i - 1)
612 udelay(1);
615 return -EAGAIN;
618 struct bus_type coresight_bustype = {
619 .name = "coresight",
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)
630 int i;
631 int ret;
632 int link_subtype;
633 int nr_refcnts = 1;
634 atomic_t *refcnts = NULL;
635 struct coresight_device *csdev;
636 struct coresight_connection *conns;
638 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
639 if (!csdev) {
640 ret = -ENOMEM;
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);
655 if (!refcnts) {
656 ret = -ENOMEM;
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);
665 if (!conns) {
666 ret = -ENOMEM;
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);
691 if (ret)
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);
701 return csdev;
703 err_device_register:
704 kfree(conns);
705 err_kzalloc_conns:
706 kfree(refcnts);
707 err_kzalloc_refcnts:
708 kfree(csdev);
709 err_kzalloc_csdev:
710 return ERR_PTR(ret);
712 EXPORT_SYMBOL_GPL(coresight_register);
714 void coresight_unregister(struct coresight_device *csdev)
716 mutex_lock(&coresight_mutex);
718 kfree(csdev->conns);
719 device_unregister(&csdev->dev);
721 mutex_unlock(&coresight_mutex);
723 EXPORT_SYMBOL_GPL(coresight_unregister);
725 MODULE_LICENSE("GPL v2");