1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2012, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/types.h>
9 #include <linux/device.h>
10 #include <linux/io.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/clk.h>
16 #include <linux/coresight.h>
17 #include <linux/of_platform.h>
18 #include <linux/delay.h>
19 #include <linux/pm_runtime.h>
20 
21 #include "coresight-priv.h"
22 
23 static DEFINE_MUTEX(coresight_mutex);
24 
25 /**
26  * struct coresight_node - elements of a path, from source to sink
27  * @csdev:	Address of an element.
28  * @link:	hook to the list.
29  */
30 struct coresight_node {
31 	struct coresight_device *csdev;
32 	struct list_head link;
33 };
34 
35 /*
36  * When operating Coresight drivers from the sysFS interface, only a single
37  * path can exist from a tracer (associated to a CPU) to a sink.
38  */
39 static DEFINE_PER_CPU(struct list_head *, tracer_path);
40 
41 /*
42  * As of this writing only a single STM can be found in CS topologies.  Since
43  * there is no way to know if we'll ever see more and what kind of
44  * configuration they will enact, for the time being only define a single path
45  * for STM.
46  */
47 static struct list_head *stm_path;
48 
49 /*
50  * When losing synchronisation a new barrier packet needs to be inserted at the
51  * beginning of the data collected in a buffer.  That way the decoder knows that
52  * it needs to look for another sync sequence.
53  */
54 const u32 barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
55 
coresight_id_match(struct device * dev,void * data)56 static int coresight_id_match(struct device *dev, void *data)
57 {
58 	int trace_id, i_trace_id;
59 	struct coresight_device *csdev, *i_csdev;
60 
61 	csdev = data;
62 	i_csdev = to_coresight_device(dev);
63 
64 	/*
65 	 * No need to care about oneself and components that are not
66 	 * sources or not enabled
67 	 */
68 	if (i_csdev == csdev || !i_csdev->enable ||
69 	    i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
70 		return 0;
71 
72 	/* Get the source ID for both compoment */
73 	trace_id = source_ops(csdev)->trace_id(csdev);
74 	i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
75 
76 	/* All you need is one */
77 	if (trace_id == i_trace_id)
78 		return 1;
79 
80 	return 0;
81 }
82 
coresight_source_is_unique(struct coresight_device * csdev)83 static int coresight_source_is_unique(struct coresight_device *csdev)
84 {
85 	int trace_id = source_ops(csdev)->trace_id(csdev);
86 
87 	/* this shouldn't happen */
88 	if (trace_id < 0)
89 		return 0;
90 
91 	return !bus_for_each_dev(&coresight_bustype, NULL,
92 				 csdev, coresight_id_match);
93 }
94 
coresight_find_link_inport(struct coresight_device * csdev,struct coresight_device * parent)95 static int coresight_find_link_inport(struct coresight_device *csdev,
96 				      struct coresight_device *parent)
97 {
98 	int i;
99 	struct coresight_connection *conn;
100 
101 	for (i = 0; i < parent->nr_outport; i++) {
102 		conn = &parent->conns[i];
103 		if (conn->child_dev == csdev)
104 			return conn->child_port;
105 	}
106 
107 	dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
108 		dev_name(&parent->dev), dev_name(&csdev->dev));
109 
110 	return -ENODEV;
111 }
112 
coresight_find_link_outport(struct coresight_device * csdev,struct coresight_device * child)113 static int coresight_find_link_outport(struct coresight_device *csdev,
114 				       struct coresight_device *child)
115 {
116 	int i;
117 	struct coresight_connection *conn;
118 
119 	for (i = 0; i < csdev->nr_outport; i++) {
120 		conn = &csdev->conns[i];
121 		if (conn->child_dev == child)
122 			return conn->outport;
123 	}
124 
125 	dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
126 		dev_name(&csdev->dev), dev_name(&child->dev));
127 
128 	return -ENODEV;
129 }
130 
coresight_enable_sink(struct coresight_device * csdev,u32 mode)131 static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
132 {
133 	int ret;
134 
135 	if (!csdev->enable) {
136 		if (sink_ops(csdev)->enable) {
137 			ret = sink_ops(csdev)->enable(csdev, mode);
138 			if (ret)
139 				return ret;
140 		}
141 		csdev->enable = true;
142 	}
143 
144 	atomic_inc(csdev->refcnt);
145 
146 	return 0;
147 }
148 
coresight_disable_sink(struct coresight_device * csdev)149 static void coresight_disable_sink(struct coresight_device *csdev)
150 {
151 	if (atomic_dec_return(csdev->refcnt) == 0) {
152 		if (sink_ops(csdev)->disable) {
153 			sink_ops(csdev)->disable(csdev);
154 			csdev->enable = false;
155 		}
156 	}
157 }
158 
coresight_enable_link(struct coresight_device * csdev,struct coresight_device * parent,struct coresight_device * child)159 static int coresight_enable_link(struct coresight_device *csdev,
160 				 struct coresight_device *parent,
161 				 struct coresight_device *child)
162 {
163 	int ret;
164 	int link_subtype;
165 	int refport, inport, outport;
166 
167 	if (!parent || !child)
168 		return -EINVAL;
169 
170 	inport = coresight_find_link_inport(csdev, parent);
171 	outport = coresight_find_link_outport(csdev, child);
172 	link_subtype = csdev->subtype.link_subtype;
173 
174 	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
175 		refport = inport;
176 	else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
177 		refport = outport;
178 	else
179 		refport = 0;
180 
181 	if (refport < 0)
182 		return refport;
183 
184 	if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
185 		if (link_ops(csdev)->enable) {
186 			ret = link_ops(csdev)->enable(csdev, inport, outport);
187 			if (ret)
188 				return ret;
189 		}
190 	}
191 
192 	csdev->enable = true;
193 
194 	return 0;
195 }
196 
coresight_disable_link(struct coresight_device * csdev,struct coresight_device * parent,struct coresight_device * child)197 static void coresight_disable_link(struct coresight_device *csdev,
198 				   struct coresight_device *parent,
199 				   struct coresight_device *child)
200 {
201 	int i, nr_conns;
202 	int link_subtype;
203 	int refport, inport, outport;
204 
205 	if (!parent || !child)
206 		return;
207 
208 	inport = coresight_find_link_inport(csdev, parent);
209 	outport = coresight_find_link_outport(csdev, child);
210 	link_subtype = csdev->subtype.link_subtype;
211 
212 	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
213 		refport = inport;
214 		nr_conns = csdev->nr_inport;
215 	} else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
216 		refport = outport;
217 		nr_conns = csdev->nr_outport;
218 	} else {
219 		refport = 0;
220 		nr_conns = 1;
221 	}
222 
223 	if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
224 		if (link_ops(csdev)->disable)
225 			link_ops(csdev)->disable(csdev, inport, outport);
226 	}
227 
228 	for (i = 0; i < nr_conns; i++)
229 		if (atomic_read(&csdev->refcnt[i]) != 0)
230 			return;
231 
232 	csdev->enable = false;
233 }
234 
coresight_enable_source(struct coresight_device * csdev,u32 mode)235 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
236 {
237 	int ret;
238 
239 	if (!coresight_source_is_unique(csdev)) {
240 		dev_warn(&csdev->dev, "traceID %d not unique\n",
241 			 source_ops(csdev)->trace_id(csdev));
242 		return -EINVAL;
243 	}
244 
245 	if (!csdev->enable) {
246 		if (source_ops(csdev)->enable) {
247 			ret = source_ops(csdev)->enable(csdev, NULL, mode);
248 			if (ret)
249 				return ret;
250 		}
251 		csdev->enable = true;
252 	}
253 
254 	atomic_inc(csdev->refcnt);
255 
256 	return 0;
257 }
258 
259 /**
260  *  coresight_disable_source - Drop the reference count by 1 and disable
261  *  the device if there are no users left.
262  *
263  *  @csdev - The coresight device to disable
264  *
265  *  Returns true if the device has been disabled.
266  */
coresight_disable_source(struct coresight_device * csdev)267 static bool coresight_disable_source(struct coresight_device *csdev)
268 {
269 	if (atomic_dec_return(csdev->refcnt) == 0) {
270 		if (source_ops(csdev)->disable)
271 			source_ops(csdev)->disable(csdev, NULL);
272 		csdev->enable = false;
273 	}
274 	return !csdev->enable;
275 }
276 
coresight_disable_path(struct list_head * path)277 void coresight_disable_path(struct list_head *path)
278 {
279 	u32 type;
280 	struct coresight_node *nd;
281 	struct coresight_device *csdev, *parent, *child;
282 
283 	list_for_each_entry(nd, path, link) {
284 		csdev = nd->csdev;
285 		type = csdev->type;
286 
287 		/*
288 		 * ETF devices are tricky... They can be a link or a sink,
289 		 * depending on how they are configured.  If an ETF has been
290 		 * "activated" it will be configured as a sink, otherwise
291 		 * go ahead with the link configuration.
292 		 */
293 		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
294 			type = (csdev == coresight_get_sink(path)) ?
295 						CORESIGHT_DEV_TYPE_SINK :
296 						CORESIGHT_DEV_TYPE_LINK;
297 
298 		switch (type) {
299 		case CORESIGHT_DEV_TYPE_SINK:
300 			coresight_disable_sink(csdev);
301 			break;
302 		case CORESIGHT_DEV_TYPE_SOURCE:
303 			/* sources are disabled from either sysFS or Perf */
304 			break;
305 		case CORESIGHT_DEV_TYPE_LINK:
306 			parent = list_prev_entry(nd, link)->csdev;
307 			child = list_next_entry(nd, link)->csdev;
308 			coresight_disable_link(csdev, parent, child);
309 			break;
310 		default:
311 			break;
312 		}
313 	}
314 }
315 
coresight_enable_path(struct list_head * path,u32 mode)316 int coresight_enable_path(struct list_head *path, u32 mode)
317 {
318 
319 	int ret = 0;
320 	u32 type;
321 	struct coresight_node *nd;
322 	struct coresight_device *csdev, *parent, *child;
323 
324 	list_for_each_entry_reverse(nd, path, link) {
325 		csdev = nd->csdev;
326 		type = csdev->type;
327 
328 		/*
329 		 * ETF devices are tricky... They can be a link or a sink,
330 		 * depending on how they are configured.  If an ETF has been
331 		 * "activated" it will be configured as a sink, otherwise
332 		 * go ahead with the link configuration.
333 		 */
334 		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
335 			type = (csdev == coresight_get_sink(path)) ?
336 						CORESIGHT_DEV_TYPE_SINK :
337 						CORESIGHT_DEV_TYPE_LINK;
338 
339 		switch (type) {
340 		case CORESIGHT_DEV_TYPE_SINK:
341 			ret = coresight_enable_sink(csdev, mode);
342 			if (ret)
343 				goto err;
344 			break;
345 		case CORESIGHT_DEV_TYPE_SOURCE:
346 			/* sources are enabled from either sysFS or Perf */
347 			break;
348 		case CORESIGHT_DEV_TYPE_LINK:
349 			parent = list_prev_entry(nd, link)->csdev;
350 			child = list_next_entry(nd, link)->csdev;
351 			ret = coresight_enable_link(csdev, parent, child);
352 			if (ret)
353 				goto err;
354 			break;
355 		default:
356 			goto err;
357 		}
358 	}
359 
360 out:
361 	return ret;
362 err:
363 	coresight_disable_path(path);
364 	goto out;
365 }
366 
coresight_get_sink(struct list_head * path)367 struct coresight_device *coresight_get_sink(struct list_head *path)
368 {
369 	struct coresight_device *csdev;
370 
371 	if (!path)
372 		return NULL;
373 
374 	csdev = list_last_entry(path, struct coresight_node, link)->csdev;
375 	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
376 	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
377 		return NULL;
378 
379 	return csdev;
380 }
381 
coresight_enabled_sink(struct device * dev,void * data)382 static int coresight_enabled_sink(struct device *dev, void *data)
383 {
384 	bool *reset = data;
385 	struct coresight_device *csdev = to_coresight_device(dev);
386 
387 	if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
388 	     csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
389 	     csdev->activated) {
390 		/*
391 		 * Now that we have a handle on the sink for this session,
392 		 * disable the sysFS "enable_sink" flag so that possible
393 		 * concurrent perf session that wish to use another sink don't
394 		 * trip on it.  Doing so has no ramification for the current
395 		 * session.
396 		 */
397 		if (*reset)
398 			csdev->activated = false;
399 
400 		return 1;
401 	}
402 
403 	return 0;
404 }
405 
406 /**
407  * coresight_get_enabled_sink - returns the first enabled sink found on the bus
408  * @deactivate:	Whether the 'enable_sink' flag should be reset
409  *
410  * When operated from perf the deactivate parameter should be set to 'true'.
411  * That way the "enabled_sink" flag of the sink that was selected can be reset,
412  * allowing for other concurrent perf sessions to choose a different sink.
413  *
414  * When operated from sysFS users have full control and as such the deactivate
415  * parameter should be set to 'false', hence mandating users to explicitly
416  * clear the flag.
417  */
coresight_get_enabled_sink(bool deactivate)418 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
419 {
420 	struct device *dev = NULL;
421 
422 	dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
423 			      coresight_enabled_sink);
424 
425 	return dev ? to_coresight_device(dev) : NULL;
426 }
427 
428 /*
429  * coresight_grab_device - Power up this device and any of the helper
430  * devices connected to it for trace operation. Since the helper devices
431  * don't appear on the trace path, they should be handled along with the
432  * the master device.
433  */
coresight_grab_device(struct coresight_device * csdev)434 static void coresight_grab_device(struct coresight_device *csdev)
435 {
436 	int i;
437 
438 	for (i = 0; i < csdev->nr_outport; i++) {
439 		struct coresight_device *child = csdev->conns[i].child_dev;
440 
441 		if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
442 			pm_runtime_get_sync(child->dev.parent);
443 	}
444 	pm_runtime_get_sync(csdev->dev.parent);
445 }
446 
447 /*
448  * coresight_drop_device - Release this device and any of the helper
449  * devices connected to it.
450  */
coresight_drop_device(struct coresight_device * csdev)451 static void coresight_drop_device(struct coresight_device *csdev)
452 {
453 	int i;
454 
455 	pm_runtime_put(csdev->dev.parent);
456 	for (i = 0; i < csdev->nr_outport; i++) {
457 		struct coresight_device *child = csdev->conns[i].child_dev;
458 
459 		if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
460 			pm_runtime_put(child->dev.parent);
461 	}
462 }
463 
464 /**
465  * _coresight_build_path - recursively build a path from a @csdev to a sink.
466  * @csdev:	The device to start from.
467  * @path:	The list to add devices to.
468  *
469  * The tree of Coresight device is traversed until an activated sink is
470  * found.  From there the sink is added to the list along with all the
471  * devices that led to that point - the end result is a list from source
472  * to sink. In that list the source is the first device and the sink the
473  * last one.
474  */
_coresight_build_path(struct coresight_device * csdev,struct coresight_device * sink,struct list_head * path)475 static int _coresight_build_path(struct coresight_device *csdev,
476 				 struct coresight_device *sink,
477 				 struct list_head *path)
478 {
479 	int i;
480 	bool found = false;
481 	struct coresight_node *node;
482 
483 	/* An activated sink has been found.  Enqueue the element */
484 	if (csdev == sink)
485 		goto out;
486 
487 	/* Not a sink - recursively explore each port found on this element */
488 	for (i = 0; i < csdev->nr_outport; i++) {
489 		struct coresight_device *child_dev = csdev->conns[i].child_dev;
490 
491 		if (child_dev &&
492 		    _coresight_build_path(child_dev, sink, path) == 0) {
493 			found = true;
494 			break;
495 		}
496 	}
497 
498 	if (!found)
499 		return -ENODEV;
500 
501 out:
502 	/*
503 	 * A path from this element to a sink has been found.  The elements
504 	 * leading to the sink are already enqueued, all that is left to do
505 	 * is tell the PM runtime core we need this element and add a node
506 	 * for it.
507 	 */
508 	node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
509 	if (!node)
510 		return -ENOMEM;
511 
512 	coresight_grab_device(csdev);
513 	node->csdev = csdev;
514 	list_add(&node->link, path);
515 
516 	return 0;
517 }
518 
coresight_build_path(struct coresight_device * source,struct coresight_device * sink)519 struct list_head *coresight_build_path(struct coresight_device *source,
520 				       struct coresight_device *sink)
521 {
522 	struct list_head *path;
523 	int rc;
524 
525 	if (!sink)
526 		return ERR_PTR(-EINVAL);
527 
528 	path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
529 	if (!path)
530 		return ERR_PTR(-ENOMEM);
531 
532 	INIT_LIST_HEAD(path);
533 
534 	rc = _coresight_build_path(source, sink, path);
535 	if (rc) {
536 		kfree(path);
537 		return ERR_PTR(rc);
538 	}
539 
540 	return path;
541 }
542 
543 /**
544  * coresight_release_path - release a previously built path.
545  * @path:	the path to release.
546  *
547  * Go through all the elements of a path and 1) removed it from the list and
548  * 2) free the memory allocated for each node.
549  */
coresight_release_path(struct list_head * path)550 void coresight_release_path(struct list_head *path)
551 {
552 	struct coresight_device *csdev;
553 	struct coresight_node *nd, *next;
554 
555 	list_for_each_entry_safe(nd, next, path, link) {
556 		csdev = nd->csdev;
557 
558 		coresight_drop_device(csdev);
559 		list_del(&nd->link);
560 		kfree(nd);
561 	}
562 
563 	kfree(path);
564 	path = NULL;
565 }
566 
567 /** coresight_validate_source - make sure a source has the right credentials
568  *  @csdev:	the device structure for a source.
569  *  @function:	the function this was called from.
570  *
571  * Assumes the coresight_mutex is held.
572  */
coresight_validate_source(struct coresight_device * csdev,const char * function)573 static int coresight_validate_source(struct coresight_device *csdev,
574 				     const char *function)
575 {
576 	u32 type, subtype;
577 
578 	type = csdev->type;
579 	subtype = csdev->subtype.source_subtype;
580 
581 	if (type != CORESIGHT_DEV_TYPE_SOURCE) {
582 		dev_err(&csdev->dev, "wrong device type in %s\n", function);
583 		return -EINVAL;
584 	}
585 
586 	if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
587 	    subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
588 		dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
589 		return -EINVAL;
590 	}
591 
592 	return 0;
593 }
594 
coresight_enable(struct coresight_device * csdev)595 int coresight_enable(struct coresight_device *csdev)
596 {
597 	int cpu, ret = 0;
598 	struct coresight_device *sink;
599 	struct list_head *path;
600 	enum coresight_dev_subtype_source subtype;
601 
602 	subtype = csdev->subtype.source_subtype;
603 
604 	mutex_lock(&coresight_mutex);
605 
606 	ret = coresight_validate_source(csdev, __func__);
607 	if (ret)
608 		goto out;
609 
610 	if (csdev->enable) {
611 		/*
612 		 * There could be multiple applications driving the software
613 		 * source. So keep the refcount for each such user when the
614 		 * source is already enabled.
615 		 */
616 		if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
617 			atomic_inc(csdev->refcnt);
618 		goto out;
619 	}
620 
621 	/*
622 	 * Search for a valid sink for this session but don't reset the
623 	 * "enable_sink" flag in sysFS.  Users get to do that explicitly.
624 	 */
625 	sink = coresight_get_enabled_sink(false);
626 	if (!sink) {
627 		ret = -EINVAL;
628 		goto out;
629 	}
630 
631 	path = coresight_build_path(csdev, sink);
632 	if (IS_ERR(path)) {
633 		pr_err("building path(s) failed\n");
634 		ret = PTR_ERR(path);
635 		goto out;
636 	}
637 
638 	ret = coresight_enable_path(path, CS_MODE_SYSFS);
639 	if (ret)
640 		goto err_path;
641 
642 	ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
643 	if (ret)
644 		goto err_source;
645 
646 	switch (subtype) {
647 	case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
648 		/*
649 		 * When working from sysFS it is important to keep track
650 		 * of the paths that were created so that they can be
651 		 * undone in 'coresight_disable()'.  Since there can only
652 		 * be a single session per tracer (when working from sysFS)
653 		 * a per-cpu variable will do just fine.
654 		 */
655 		cpu = source_ops(csdev)->cpu_id(csdev);
656 		per_cpu(tracer_path, cpu) = path;
657 		break;
658 	case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
659 		stm_path = path;
660 		break;
661 	default:
662 		/* We can't be here */
663 		break;
664 	}
665 
666 out:
667 	mutex_unlock(&coresight_mutex);
668 	return ret;
669 
670 err_source:
671 	coresight_disable_path(path);
672 
673 err_path:
674 	coresight_release_path(path);
675 	goto out;
676 }
677 EXPORT_SYMBOL_GPL(coresight_enable);
678 
coresight_disable(struct coresight_device * csdev)679 void coresight_disable(struct coresight_device *csdev)
680 {
681 	int cpu, ret;
682 	struct list_head *path = NULL;
683 
684 	mutex_lock(&coresight_mutex);
685 
686 	ret = coresight_validate_source(csdev, __func__);
687 	if (ret)
688 		goto out;
689 
690 	if (!csdev->enable || !coresight_disable_source(csdev))
691 		goto out;
692 
693 	switch (csdev->subtype.source_subtype) {
694 	case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
695 		cpu = source_ops(csdev)->cpu_id(csdev);
696 		path = per_cpu(tracer_path, cpu);
697 		per_cpu(tracer_path, cpu) = NULL;
698 		break;
699 	case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
700 		path = stm_path;
701 		stm_path = NULL;
702 		break;
703 	default:
704 		/* We can't be here */
705 		break;
706 	}
707 
708 	coresight_disable_path(path);
709 	coresight_release_path(path);
710 
711 out:
712 	mutex_unlock(&coresight_mutex);
713 }
714 EXPORT_SYMBOL_GPL(coresight_disable);
715 
enable_sink_show(struct device * dev,struct device_attribute * attr,char * buf)716 static ssize_t enable_sink_show(struct device *dev,
717 				struct device_attribute *attr, char *buf)
718 {
719 	struct coresight_device *csdev = to_coresight_device(dev);
720 
721 	return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
722 }
723 
enable_sink_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)724 static ssize_t enable_sink_store(struct device *dev,
725 				 struct device_attribute *attr,
726 				 const char *buf, size_t size)
727 {
728 	int ret;
729 	unsigned long val;
730 	struct coresight_device *csdev = to_coresight_device(dev);
731 
732 	ret = kstrtoul(buf, 10, &val);
733 	if (ret)
734 		return ret;
735 
736 	if (val)
737 		csdev->activated = true;
738 	else
739 		csdev->activated = false;
740 
741 	return size;
742 
743 }
744 static DEVICE_ATTR_RW(enable_sink);
745 
enable_source_show(struct device * dev,struct device_attribute * attr,char * buf)746 static ssize_t enable_source_show(struct device *dev,
747 				  struct device_attribute *attr, char *buf)
748 {
749 	struct coresight_device *csdev = to_coresight_device(dev);
750 
751 	return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
752 }
753 
enable_source_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)754 static ssize_t enable_source_store(struct device *dev,
755 				   struct device_attribute *attr,
756 				   const char *buf, size_t size)
757 {
758 	int ret = 0;
759 	unsigned long val;
760 	struct coresight_device *csdev = to_coresight_device(dev);
761 
762 	ret = kstrtoul(buf, 10, &val);
763 	if (ret)
764 		return ret;
765 
766 	if (val) {
767 		ret = coresight_enable(csdev);
768 		if (ret)
769 			return ret;
770 	} else {
771 		coresight_disable(csdev);
772 	}
773 
774 	return size;
775 }
776 static DEVICE_ATTR_RW(enable_source);
777 
778 static struct attribute *coresight_sink_attrs[] = {
779 	&dev_attr_enable_sink.attr,
780 	NULL,
781 };
782 ATTRIBUTE_GROUPS(coresight_sink);
783 
784 static struct attribute *coresight_source_attrs[] = {
785 	&dev_attr_enable_source.attr,
786 	NULL,
787 };
788 ATTRIBUTE_GROUPS(coresight_source);
789 
790 static struct device_type coresight_dev_type[] = {
791 	{
792 		.name = "none",
793 	},
794 	{
795 		.name = "sink",
796 		.groups = coresight_sink_groups,
797 	},
798 	{
799 		.name = "link",
800 	},
801 	{
802 		.name = "linksink",
803 		.groups = coresight_sink_groups,
804 	},
805 	{
806 		.name = "source",
807 		.groups = coresight_source_groups,
808 	},
809 	{
810 		.name = "helper",
811 	},
812 };
813 
coresight_device_release(struct device * dev)814 static void coresight_device_release(struct device *dev)
815 {
816 	struct coresight_device *csdev = to_coresight_device(dev);
817 
818 	kfree(csdev->conns);
819 	kfree(csdev->refcnt);
820 	kfree(csdev);
821 }
822 
coresight_orphan_match(struct device * dev,void * data)823 static int coresight_orphan_match(struct device *dev, void *data)
824 {
825 	int i;
826 	bool still_orphan = false;
827 	struct coresight_device *csdev, *i_csdev;
828 	struct coresight_connection *conn;
829 
830 	csdev = data;
831 	i_csdev = to_coresight_device(dev);
832 
833 	/* No need to check oneself */
834 	if (csdev == i_csdev)
835 		return 0;
836 
837 	/* Move on to another component if no connection is orphan */
838 	if (!i_csdev->orphan)
839 		return 0;
840 	/*
841 	 * Circle throuch all the connection of that component.  If we find
842 	 * an orphan connection whose name matches @csdev, link it.
843 	 */
844 	for (i = 0; i < i_csdev->nr_outport; i++) {
845 		conn = &i_csdev->conns[i];
846 
847 		/* We have found at least one orphan connection */
848 		if (conn->child_dev == NULL) {
849 			/* Does it match this newly added device? */
850 			if (conn->child_name &&
851 			    !strcmp(dev_name(&csdev->dev), conn->child_name)) {
852 				conn->child_dev = csdev;
853 			} else {
854 				/* This component still has an orphan */
855 				still_orphan = true;
856 			}
857 		}
858 	}
859 
860 	i_csdev->orphan = still_orphan;
861 
862 	/*
863 	 * Returning '0' ensures that all known component on the
864 	 * bus will be checked.
865 	 */
866 	return 0;
867 }
868 
coresight_fixup_orphan_conns(struct coresight_device * csdev)869 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
870 {
871 	/*
872 	 * No need to check for a return value as orphan connection(s)
873 	 * are hooked-up with each newly added component.
874 	 */
875 	bus_for_each_dev(&coresight_bustype, NULL,
876 			 csdev, coresight_orphan_match);
877 }
878 
879 
coresight_fixup_device_conns(struct coresight_device * csdev)880 static void coresight_fixup_device_conns(struct coresight_device *csdev)
881 {
882 	int i;
883 
884 	for (i = 0; i < csdev->nr_outport; i++) {
885 		struct coresight_connection *conn = &csdev->conns[i];
886 		struct device *dev = NULL;
887 
888 		if (conn->child_name)
889 			dev = bus_find_device_by_name(&coresight_bustype, NULL,
890 						      conn->child_name);
891 		if (dev) {
892 			conn->child_dev = to_coresight_device(dev);
893 			/* and put reference from 'bus_find_device()' */
894 			put_device(dev);
895 		} else {
896 			csdev->orphan = true;
897 			conn->child_dev = NULL;
898 		}
899 	}
900 }
901 
coresight_remove_match(struct device * dev,void * data)902 static int coresight_remove_match(struct device *dev, void *data)
903 {
904 	int i;
905 	struct coresight_device *csdev, *iterator;
906 	struct coresight_connection *conn;
907 
908 	csdev = data;
909 	iterator = to_coresight_device(dev);
910 
911 	/* No need to check oneself */
912 	if (csdev == iterator)
913 		return 0;
914 
915 	/*
916 	 * Circle throuch all the connection of that component.  If we find
917 	 * a connection whose name matches @csdev, remove it.
918 	 */
919 	for (i = 0; i < iterator->nr_outport; i++) {
920 		conn = &iterator->conns[i];
921 
922 		if (conn->child_dev == NULL)
923 			continue;
924 
925 		if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
926 			iterator->orphan = true;
927 			conn->child_dev = NULL;
928 			/* No need to continue */
929 			break;
930 		}
931 	}
932 
933 	/*
934 	 * Returning '0' ensures that all known component on the
935 	 * bus will be checked.
936 	 */
937 	return 0;
938 }
939 
coresight_remove_conns(struct coresight_device * csdev)940 static void coresight_remove_conns(struct coresight_device *csdev)
941 {
942 	bus_for_each_dev(&coresight_bustype, NULL,
943 			 csdev, coresight_remove_match);
944 }
945 
946 /**
947  * coresight_timeout - loop until a bit has changed to a specific state.
948  * @addr: base address of the area of interest.
949  * @offset: address of a register, starting from @addr.
950  * @position: the position of the bit of interest.
951  * @value: the value the bit should have.
952  *
953  * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
954  * TIMEOUT_US has elapsed, which ever happens first.
955  */
956 
coresight_timeout(void __iomem * addr,u32 offset,int position,int value)957 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
958 {
959 	int i;
960 	u32 val;
961 
962 	for (i = TIMEOUT_US; i > 0; i--) {
963 		val = __raw_readl(addr + offset);
964 		/* waiting on the bit to go from 0 to 1 */
965 		if (value) {
966 			if (val & BIT(position))
967 				return 0;
968 		/* waiting on the bit to go from 1 to 0 */
969 		} else {
970 			if (!(val & BIT(position)))
971 				return 0;
972 		}
973 
974 		/*
975 		 * Delay is arbitrary - the specification doesn't say how long
976 		 * we are expected to wait.  Extra check required to make sure
977 		 * we don't wait needlessly on the last iteration.
978 		 */
979 		if (i - 1)
980 			udelay(1);
981 	}
982 
983 	return -EAGAIN;
984 }
985 
986 struct bus_type coresight_bustype = {
987 	.name	= "coresight",
988 };
989 
coresight_init(void)990 static int __init coresight_init(void)
991 {
992 	return bus_register(&coresight_bustype);
993 }
994 postcore_initcall(coresight_init);
995 
coresight_register(struct coresight_desc * desc)996 struct coresight_device *coresight_register(struct coresight_desc *desc)
997 {
998 	int i;
999 	int ret;
1000 	int link_subtype;
1001 	int nr_refcnts = 1;
1002 	atomic_t *refcnts = NULL;
1003 	struct coresight_device *csdev;
1004 	struct coresight_connection *conns = NULL;
1005 
1006 	csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1007 	if (!csdev) {
1008 		ret = -ENOMEM;
1009 		goto err_kzalloc_csdev;
1010 	}
1011 
1012 	if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1013 	    desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1014 		link_subtype = desc->subtype.link_subtype;
1015 
1016 		if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1017 			nr_refcnts = desc->pdata->nr_inport;
1018 		else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1019 			nr_refcnts = desc->pdata->nr_outport;
1020 	}
1021 
1022 	refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1023 	if (!refcnts) {
1024 		ret = -ENOMEM;
1025 		goto err_kzalloc_refcnts;
1026 	}
1027 
1028 	csdev->refcnt = refcnts;
1029 
1030 	csdev->nr_inport = desc->pdata->nr_inport;
1031 	csdev->nr_outport = desc->pdata->nr_outport;
1032 
1033 	/* Initialise connections if there is at least one outport */
1034 	if (csdev->nr_outport) {
1035 		conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
1036 		if (!conns) {
1037 			ret = -ENOMEM;
1038 			goto err_kzalloc_conns;
1039 		}
1040 
1041 		for (i = 0; i < csdev->nr_outport; i++) {
1042 			conns[i].outport = desc->pdata->outports[i];
1043 			conns[i].child_name = desc->pdata->child_names[i];
1044 			conns[i].child_port = desc->pdata->child_ports[i];
1045 		}
1046 	}
1047 
1048 	csdev->conns = conns;
1049 
1050 	csdev->type = desc->type;
1051 	csdev->subtype = desc->subtype;
1052 	csdev->ops = desc->ops;
1053 	csdev->orphan = false;
1054 
1055 	csdev->dev.type = &coresight_dev_type[desc->type];
1056 	csdev->dev.groups = desc->groups;
1057 	csdev->dev.parent = desc->dev;
1058 	csdev->dev.release = coresight_device_release;
1059 	csdev->dev.bus = &coresight_bustype;
1060 	dev_set_name(&csdev->dev, "%s", desc->pdata->name);
1061 
1062 	ret = device_register(&csdev->dev);
1063 	if (ret) {
1064 		put_device(&csdev->dev);
1065 		goto err_kzalloc_csdev;
1066 	}
1067 
1068 	mutex_lock(&coresight_mutex);
1069 
1070 	coresight_fixup_device_conns(csdev);
1071 	coresight_fixup_orphan_conns(csdev);
1072 
1073 	mutex_unlock(&coresight_mutex);
1074 
1075 	return csdev;
1076 
1077 err_kzalloc_conns:
1078 	kfree(refcnts);
1079 err_kzalloc_refcnts:
1080 	kfree(csdev);
1081 err_kzalloc_csdev:
1082 	return ERR_PTR(ret);
1083 }
1084 EXPORT_SYMBOL_GPL(coresight_register);
1085 
coresight_unregister(struct coresight_device * csdev)1086 void coresight_unregister(struct coresight_device *csdev)
1087 {
1088 	/* Remove references of that device in the topology */
1089 	coresight_remove_conns(csdev);
1090 	device_unregister(&csdev->dev);
1091 }
1092 EXPORT_SYMBOL_GPL(coresight_unregister);
1093