Merge tag 'nfsd-4.5' of git://linux-nfs.org/~bfields/linux
[deliverable/linux.git] / drivers / hwtracing / coresight / coresight.c
1 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
2 *
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.
6 *
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.
11 */
12
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>
27
28 #include "coresight-priv.h"
29
30 static DEFINE_MUTEX(coresight_mutex);
31
32 static int coresight_id_match(struct device *dev, void *data)
33 {
34 int trace_id, i_trace_id;
35 struct coresight_device *csdev, *i_csdev;
36
37 csdev = data;
38 i_csdev = to_coresight_device(dev);
39
40 /*
41 * No need to care about oneself and components that are not
42 * sources or not enabled
43 */
44 if (i_csdev == csdev || !i_csdev->enable ||
45 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
46 return 0;
47
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);
51
52 /* All you need is one */
53 if (trace_id == i_trace_id)
54 return 1;
55
56 return 0;
57 }
58
59 static int coresight_source_is_unique(struct coresight_device *csdev)
60 {
61 int trace_id = source_ops(csdev)->trace_id(csdev);
62
63 /* this shouldn't happen */
64 if (trace_id < 0)
65 return 0;
66
67 return !bus_for_each_dev(&coresight_bustype, NULL,
68 csdev, coresight_id_match);
69 }
70
71 static int coresight_find_link_inport(struct coresight_device *csdev)
72 {
73 int i;
74 struct coresight_device *parent;
75 struct coresight_connection *conn;
76
77 parent = container_of(csdev->path_link.next,
78 struct coresight_device, path_link);
79
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;
84 }
85
86 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
87 dev_name(&parent->dev), dev_name(&csdev->dev));
88
89 return 0;
90 }
91
92 static int coresight_find_link_outport(struct coresight_device *csdev)
93 {
94 int i;
95 struct coresight_device *child;
96 struct coresight_connection *conn;
97
98 child = container_of(csdev->path_link.prev,
99 struct coresight_device, path_link);
100
101 for (i = 0; i < csdev->nr_outport; i++) {
102 conn = &csdev->conns[i];
103 if (conn->child_dev == child)
104 return conn->outport;
105 }
106
107 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
108 dev_name(&csdev->dev), dev_name(&child->dev));
109
110 return 0;
111 }
112
113 static int coresight_enable_sink(struct coresight_device *csdev)
114 {
115 int ret;
116
117 if (!csdev->enable) {
118 if (sink_ops(csdev)->enable) {
119 ret = sink_ops(csdev)->enable(csdev);
120 if (ret)
121 return ret;
122 }
123 csdev->enable = true;
124 }
125
126 atomic_inc(csdev->refcnt);
127
128 return 0;
129 }
130
131 static void coresight_disable_sink(struct coresight_device *csdev)
132 {
133 if (atomic_dec_return(csdev->refcnt) == 0) {
134 if (sink_ops(csdev)->disable) {
135 sink_ops(csdev)->disable(csdev);
136 csdev->enable = false;
137 }
138 }
139 }
140
141 static int coresight_enable_link(struct coresight_device *csdev)
142 {
143 int ret;
144 int link_subtype;
145 int refport, inport, outport;
146
147 inport = coresight_find_link_inport(csdev);
148 outport = coresight_find_link_outport(csdev);
149 link_subtype = csdev->subtype.link_subtype;
150
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;
157
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;
163 }
164 }
165
166 csdev->enable = true;
167
168 return 0;
169 }
170
171 static void coresight_disable_link(struct coresight_device *csdev)
172 {
173 int i, nr_conns;
174 int link_subtype;
175 int refport, inport, outport;
176
177 inport = coresight_find_link_inport(csdev);
178 outport = coresight_find_link_outport(csdev);
179 link_subtype = csdev->subtype.link_subtype;
180
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;
190 }
191
192 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
193 if (link_ops(csdev)->disable)
194 link_ops(csdev)->disable(csdev, inport, outport);
195 }
196
197 for (i = 0; i < nr_conns; i++)
198 if (atomic_read(&csdev->refcnt[i]) != 0)
199 return;
200
201 csdev->enable = false;
202 }
203
204 static int coresight_enable_source(struct coresight_device *csdev)
205 {
206 int ret;
207
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;
212 }
213
214 if (!csdev->enable) {
215 if (source_ops(csdev)->enable) {
216 ret = source_ops(csdev)->enable(csdev);
217 if (ret)
218 return ret;
219 }
220 csdev->enable = true;
221 }
222
223 atomic_inc(csdev->refcnt);
224
225 return 0;
226 }
227
228 static void coresight_disable_source(struct coresight_device *csdev)
229 {
230 if (atomic_dec_return(csdev->refcnt) == 0) {
231 if (source_ops(csdev)->disable) {
232 source_ops(csdev)->disable(csdev);
233 csdev->enable = false;
234 }
235 }
236 }
237
238 static int coresight_enable_path(struct list_head *path)
239 {
240 int ret = 0;
241 struct coresight_device *cd;
242
243 /*
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.
247 */
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)) {
253 /*
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.
257 */
258 ;
259 } else {
260 ret = coresight_enable_link(cd);
261 }
262 if (ret)
263 goto err;
264 }
265
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)) {
273 ;
274 } else {
275 coresight_disable_link(cd);
276 }
277 }
278
279 return ret;
280 }
281
282 static int coresight_disable_path(struct list_head *path)
283 {
284 struct coresight_device *cd;
285
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)) {
291 /*
292 * The source has already been stopped, no need
293 * to do it again here.
294 */
295 ;
296 } else {
297 coresight_disable_link(cd);
298 }
299 }
300
301 return 0;
302 }
303
304 static int coresight_build_paths(struct coresight_device *csdev,
305 struct list_head *path,
306 bool enable)
307 {
308 int i, ret = -EINVAL;
309 struct coresight_connection *conn;
310
311 list_add(&csdev->path_link, path);
312
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;
326 }
327 }
328
329 if (list_first_entry(path, struct coresight_device, path_link) != csdev)
330 dev_err(&csdev->dev, "wrong device in %s\n", __func__);
331
332 list_del(&csdev->path_link);
333
334 return ret;
335 }
336
337 int coresight_enable(struct coresight_device *csdev)
338 {
339 int ret = 0;
340 LIST_HEAD(path);
341
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;
347 }
348 if (csdev->enable)
349 goto out;
350
351 if (coresight_build_paths(csdev, &path, true)) {
352 dev_err(&csdev->dev, "building path(s) failed\n");
353 goto out;
354 }
355
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;
361 }
362 EXPORT_SYMBOL_GPL(coresight_enable);
363
364 void coresight_disable(struct coresight_device *csdev)
365 {
366 LIST_HEAD(path);
367
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;
372 }
373 if (!csdev->enable)
374 goto out;
375
376 coresight_disable_source(csdev);
377 if (coresight_build_paths(csdev, &path, false))
378 dev_err(&csdev->dev, "releasing path(s) failed\n");
379
380 out:
381 mutex_unlock(&coresight_mutex);
382 }
383 EXPORT_SYMBOL_GPL(coresight_disable);
384
385 static ssize_t enable_sink_show(struct device *dev,
386 struct device_attribute *attr, char *buf)
387 {
388 struct coresight_device *csdev = to_coresight_device(dev);
389
390 return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->activated);
391 }
392
393 static ssize_t enable_sink_store(struct device *dev,
394 struct device_attribute *attr,
395 const char *buf, size_t size)
396 {
397 int ret;
398 unsigned long val;
399 struct coresight_device *csdev = to_coresight_device(dev);
400
401 ret = kstrtoul(buf, 10, &val);
402 if (ret)
403 return ret;
404
405 if (val)
406 csdev->activated = true;
407 else
408 csdev->activated = false;
409
410 return size;
411
412 }
413 static DEVICE_ATTR_RW(enable_sink);
414
415 static ssize_t enable_source_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
417 {
418 struct coresight_device *csdev = to_coresight_device(dev);
419
420 return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->enable);
421 }
422
423 static ssize_t enable_source_store(struct device *dev,
424 struct device_attribute *attr,
425 const char *buf, size_t size)
426 {
427 int ret = 0;
428 unsigned long val;
429 struct coresight_device *csdev = to_coresight_device(dev);
430
431 ret = kstrtoul(buf, 10, &val);
432 if (ret)
433 return ret;
434
435 if (val) {
436 ret = coresight_enable(csdev);
437 if (ret)
438 return ret;
439 } else {
440 coresight_disable(csdev);
441 }
442
443 return size;
444 }
445 static DEVICE_ATTR_RW(enable_source);
446
447 static struct attribute *coresight_sink_attrs[] = {
448 &dev_attr_enable_sink.attr,
449 NULL,
450 };
451 ATTRIBUTE_GROUPS(coresight_sink);
452
453 static struct attribute *coresight_source_attrs[] = {
454 &dev_attr_enable_source.attr,
455 NULL,
456 };
457 ATTRIBUTE_GROUPS(coresight_source);
458
459 static struct device_type coresight_dev_type[] = {
460 {
461 .name = "none",
462 },
463 {
464 .name = "sink",
465 .groups = coresight_sink_groups,
466 },
467 {
468 .name = "link",
469 },
470 {
471 .name = "linksink",
472 .groups = coresight_sink_groups,
473 },
474 {
475 .name = "source",
476 .groups = coresight_source_groups,
477 },
478 };
479
480 static void coresight_device_release(struct device *dev)
481 {
482 struct coresight_device *csdev = to_coresight_device(dev);
483
484 kfree(csdev);
485 }
486
487 static int coresight_orphan_match(struct device *dev, void *data)
488 {
489 int i;
490 bool still_orphan = false;
491 struct coresight_device *csdev, *i_csdev;
492 struct coresight_connection *conn;
493
494 csdev = data;
495 i_csdev = to_coresight_device(dev);
496
497 /* No need to check oneself */
498 if (csdev == i_csdev)
499 return 0;
500
501 /* Move on to another component if no connection is orphan */
502 if (!i_csdev->orphan)
503 return 0;
504 /*
505 * Circle throuch all the connection of that component. If we find
506 * an orphan connection whose name matches @csdev, link it.
507 */
508 for (i = 0; i < i_csdev->nr_outport; i++) {
509 conn = &i_csdev->conns[i];
510
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;
519 }
520 }
521 }
522
523 i_csdev->orphan = still_orphan;
524
525 /*
526 * Returning '0' ensures that all known component on the
527 * bus will be checked.
528 */
529 return 0;
530 }
531
532 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
533 {
534 /*
535 * No need to check for a return value as orphan connection(s)
536 * are hooked-up with each newly added component.
537 */
538 bus_for_each_dev(&coresight_bustype, NULL,
539 csdev, coresight_orphan_match);
540 }
541
542
543 static int coresight_name_match(struct device *dev, void *data)
544 {
545 char *to_match;
546 struct coresight_device *i_csdev;
547
548 to_match = data;
549 i_csdev = to_coresight_device(dev);
550
551 if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
552 return 1;
553
554 return 0;
555 }
556
557 static void coresight_fixup_device_conns(struct coresight_device *csdev)
558 {
559 int i;
560 struct device *dev = NULL;
561 struct coresight_connection *conn;
562
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);
568
569 if (dev) {
570 conn->child_dev = to_coresight_device(dev);
571 } else {
572 csdev->orphan = true;
573 conn->child_dev = NULL;
574 }
575 }
576 }
577
578 /**
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.
584 *
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.
587 */
588
589 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
590 {
591 int i;
592 u32 val;
593
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;
604 }
605
606 /*
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.
610 */
611 if (i - 1)
612 udelay(1);
613 }
614
615 return -EAGAIN;
616 }
617
618 struct bus_type coresight_bustype = {
619 .name = "coresight",
620 };
621
622 static int __init coresight_init(void)
623 {
624 return bus_register(&coresight_bustype);
625 }
626 postcore_initcall(coresight_init);
627
628 struct coresight_device *coresight_register(struct coresight_desc *desc)
629 {
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;
637
638 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
639 if (!csdev) {
640 ret = -ENOMEM;
641 goto err_kzalloc_csdev;
642 }
643
644 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
645 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
646 link_subtype = desc->subtype.link_subtype;
647
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;
652 }
653
654 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
655 if (!refcnts) {
656 ret = -ENOMEM;
657 goto err_kzalloc_refcnts;
658 }
659
660 csdev->refcnt = refcnts;
661
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;
668 }
669
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];
674 }
675
676 csdev->conns = conns;
677
678 csdev->type = desc->type;
679 csdev->subtype = desc->subtype;
680 csdev->ops = desc->ops;
681 csdev->orphan = false;
682
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);
689
690 ret = device_register(&csdev->dev);
691 if (ret)
692 goto err_device_register;
693
694 mutex_lock(&coresight_mutex);
695
696 coresight_fixup_device_conns(csdev);
697 coresight_fixup_orphan_conns(csdev);
698
699 mutex_unlock(&coresight_mutex);
700
701 return csdev;
702
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);
711 }
712 EXPORT_SYMBOL_GPL(coresight_register);
713
714 void coresight_unregister(struct coresight_device *csdev)
715 {
716 mutex_lock(&coresight_mutex);
717
718 kfree(csdev->conns);
719 device_unregister(&csdev->dev);
720
721 mutex_unlock(&coresight_mutex);
722 }
723 EXPORT_SYMBOL_GPL(coresight_unregister);
724
725 MODULE_LICENSE("GPL v2");
This page took 0.101838 seconds and 5 git commands to generate.