xref: /OK3568_Linux_fs/kernel/drivers/hwtracing/coresight/coresight-core.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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/stringhash.h>
15 #include <linux/mutex.h>
16 #include <linux/clk.h>
17 #include <linux/coresight.h>
18 #include <linux/of_platform.h>
19 #include <linux/delay.h>
20 #include <linux/pm_runtime.h>
21 
22 #include "coresight-etm-perf.h"
23 #include "coresight-priv.h"
24 
25 static DEFINE_MUTEX(coresight_mutex);
26 static DEFINE_PER_CPU(struct coresight_device *, csdev_sink);
27 
28 /**
29  * struct coresight_node - elements of a path, from source to sink
30  * @csdev:	Address of an element.
31  * @link:	hook to the list.
32  */
33 struct coresight_node {
34 	struct coresight_device *csdev;
35 	struct list_head link;
36 };
37 
38 /*
39  * When operating Coresight drivers from the sysFS interface, only a single
40  * path can exist from a tracer (associated to a CPU) to a sink.
41  */
42 static DEFINE_PER_CPU(struct list_head *, tracer_path);
43 
44 /*
45  * As of this writing only a single STM can be found in CS topologies.  Since
46  * there is no way to know if we'll ever see more and what kind of
47  * configuration they will enact, for the time being only define a single path
48  * for STM.
49  */
50 static struct list_head *stm_path;
51 
52 /*
53  * When losing synchronisation a new barrier packet needs to be inserted at the
54  * beginning of the data collected in a buffer.  That way the decoder knows that
55  * it needs to look for another sync sequence.
56  */
57 const u32 coresight_barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
58 EXPORT_SYMBOL_GPL(coresight_barrier_pkt);
59 
60 static const struct cti_assoc_op *cti_assoc_ops;
61 
coresight_set_cti_ops(const struct cti_assoc_op * cti_op)62 void coresight_set_cti_ops(const struct cti_assoc_op *cti_op)
63 {
64 	cti_assoc_ops = cti_op;
65 }
66 EXPORT_SYMBOL_GPL(coresight_set_cti_ops);
67 
coresight_remove_cti_ops(void)68 void coresight_remove_cti_ops(void)
69 {
70 	cti_assoc_ops = NULL;
71 }
72 EXPORT_SYMBOL_GPL(coresight_remove_cti_ops);
73 
coresight_set_percpu_sink(int cpu,struct coresight_device * csdev)74 void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev)
75 {
76 	per_cpu(csdev_sink, cpu) = csdev;
77 }
78 EXPORT_SYMBOL_GPL(coresight_set_percpu_sink);
79 
coresight_get_percpu_sink(int cpu)80 struct coresight_device *coresight_get_percpu_sink(int cpu)
81 {
82 	return per_cpu(csdev_sink, cpu);
83 }
84 EXPORT_SYMBOL_GPL(coresight_get_percpu_sink);
85 
coresight_id_match(struct device * dev,void * data)86 static int coresight_id_match(struct device *dev, void *data)
87 {
88 	int trace_id, i_trace_id;
89 	struct coresight_device *csdev, *i_csdev;
90 
91 	csdev = data;
92 	i_csdev = to_coresight_device(dev);
93 
94 	/*
95 	 * No need to care about oneself and components that are not
96 	 * sources or not enabled
97 	 */
98 	if (i_csdev == csdev || !i_csdev->enable ||
99 	    i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
100 		return 0;
101 
102 	/* Get the source ID for both compoment */
103 	trace_id = source_ops(csdev)->trace_id(csdev);
104 	i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
105 
106 	/* All you need is one */
107 	if (trace_id == i_trace_id)
108 		return 1;
109 
110 	return 0;
111 }
112 
coresight_source_is_unique(struct coresight_device * csdev)113 static int coresight_source_is_unique(struct coresight_device *csdev)
114 {
115 	int trace_id = source_ops(csdev)->trace_id(csdev);
116 
117 	/* this shouldn't happen */
118 	if (trace_id < 0)
119 		return 0;
120 
121 	return !bus_for_each_dev(&coresight_bustype, NULL,
122 				 csdev, coresight_id_match);
123 }
124 
coresight_find_link_inport(struct coresight_device * csdev,struct coresight_device * parent)125 static int coresight_find_link_inport(struct coresight_device *csdev,
126 				      struct coresight_device *parent)
127 {
128 	int i;
129 	struct coresight_connection *conn;
130 
131 	for (i = 0; i < parent->pdata->nr_outport; i++) {
132 		conn = &parent->pdata->conns[i];
133 		if (conn->child_dev == csdev)
134 			return conn->child_port;
135 	}
136 
137 	dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
138 		dev_name(&parent->dev), dev_name(&csdev->dev));
139 
140 	return -ENODEV;
141 }
142 
coresight_find_link_outport(struct coresight_device * csdev,struct coresight_device * child)143 static int coresight_find_link_outport(struct coresight_device *csdev,
144 				       struct coresight_device *child)
145 {
146 	int i;
147 	struct coresight_connection *conn;
148 
149 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
150 		conn = &csdev->pdata->conns[i];
151 		if (conn->child_dev == child)
152 			return conn->outport;
153 	}
154 
155 	dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
156 		dev_name(&csdev->dev), dev_name(&child->dev));
157 
158 	return -ENODEV;
159 }
160 
coresight_read_claim_tags(struct coresight_device * csdev)161 static inline u32 coresight_read_claim_tags(struct coresight_device *csdev)
162 {
163 	return csdev_access_relaxed_read32(&csdev->access, CORESIGHT_CLAIMCLR);
164 }
165 
coresight_is_claimed_self_hosted(struct coresight_device * csdev)166 static inline bool coresight_is_claimed_self_hosted(struct coresight_device *csdev)
167 {
168 	return coresight_read_claim_tags(csdev) == CORESIGHT_CLAIM_SELF_HOSTED;
169 }
170 
coresight_is_claimed_any(struct coresight_device * csdev)171 static inline bool coresight_is_claimed_any(struct coresight_device *csdev)
172 {
173 	return coresight_read_claim_tags(csdev) != 0;
174 }
175 
coresight_set_claim_tags(struct coresight_device * csdev)176 static inline void coresight_set_claim_tags(struct coresight_device *csdev)
177 {
178 	csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
179 				     CORESIGHT_CLAIMSET);
180 	isb();
181 }
182 
coresight_clear_claim_tags(struct coresight_device * csdev)183 static inline void coresight_clear_claim_tags(struct coresight_device *csdev)
184 {
185 	csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
186 				     CORESIGHT_CLAIMCLR);
187 	isb();
188 }
189 
190 /*
191  * coresight_claim_device_unlocked : Claim the device for self-hosted usage
192  * to prevent an external tool from touching this device. As per PSCI
193  * standards, section "Preserving the execution context" => "Debug and Trace
194  * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
195  * DBGCLAIM[0] is reserved for external tools.
196  *
197  * Called with CS_UNLOCKed for the component.
198  * Returns : 0 on success
199  */
coresight_claim_device_unlocked(struct coresight_device * csdev)200 int coresight_claim_device_unlocked(struct coresight_device *csdev)
201 {
202 	if (WARN_ON(!csdev))
203 		return -EINVAL;
204 
205 	if (coresight_is_claimed_any(csdev))
206 		return -EBUSY;
207 
208 	coresight_set_claim_tags(csdev);
209 	if (coresight_is_claimed_self_hosted(csdev))
210 		return 0;
211 	/* There was a race setting the tags, clean up and fail */
212 	coresight_clear_claim_tags(csdev);
213 	return -EBUSY;
214 }
215 EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked);
216 
coresight_claim_device(struct coresight_device * csdev)217 int coresight_claim_device(struct coresight_device *csdev)
218 {
219 	int rc;
220 
221 	if (WARN_ON(!csdev))
222 		return -EINVAL;
223 
224 	CS_UNLOCK(csdev->access.base);
225 	rc = coresight_claim_device_unlocked(csdev);
226 	CS_LOCK(csdev->access.base);
227 
228 	return rc;
229 }
230 EXPORT_SYMBOL_GPL(coresight_claim_device);
231 
232 /*
233  * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
234  * Called with CS_UNLOCKed for the component.
235  */
coresight_disclaim_device_unlocked(struct coresight_device * csdev)236 void coresight_disclaim_device_unlocked(struct coresight_device *csdev)
237 {
238 
239 	if (WARN_ON(!csdev))
240 		return;
241 
242 	if (coresight_is_claimed_self_hosted(csdev))
243 		coresight_clear_claim_tags(csdev);
244 	else
245 		/*
246 		 * The external agent may have not honoured our claim
247 		 * and has manipulated it. Or something else has seriously
248 		 * gone wrong in our driver.
249 		 */
250 		WARN_ON_ONCE(1);
251 }
252 EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked);
253 
coresight_disclaim_device(struct coresight_device * csdev)254 void coresight_disclaim_device(struct coresight_device *csdev)
255 {
256 	if (WARN_ON(!csdev))
257 		return;
258 
259 	CS_UNLOCK(csdev->access.base);
260 	coresight_disclaim_device_unlocked(csdev);
261 	CS_LOCK(csdev->access.base);
262 }
263 EXPORT_SYMBOL_GPL(coresight_disclaim_device);
264 
265 /* enable or disable an associated CTI device of the supplied CS device */
266 static int
coresight_control_assoc_ectdev(struct coresight_device * csdev,bool enable)267 coresight_control_assoc_ectdev(struct coresight_device *csdev, bool enable)
268 {
269 	int ect_ret = 0;
270 	struct coresight_device *ect_csdev = csdev->ect_dev;
271 	struct module *mod;
272 
273 	if (!ect_csdev)
274 		return 0;
275 	if ((!ect_ops(ect_csdev)->enable) || (!ect_ops(ect_csdev)->disable))
276 		return 0;
277 
278 	mod = ect_csdev->dev.parent->driver->owner;
279 	if (enable) {
280 		if (try_module_get(mod)) {
281 			ect_ret = ect_ops(ect_csdev)->enable(ect_csdev);
282 			if (ect_ret) {
283 				module_put(mod);
284 			} else {
285 				get_device(ect_csdev->dev.parent);
286 				csdev->ect_enabled = true;
287 			}
288 		} else
289 			ect_ret = -ENODEV;
290 	} else {
291 		if (csdev->ect_enabled) {
292 			ect_ret = ect_ops(ect_csdev)->disable(ect_csdev);
293 			put_device(ect_csdev->dev.parent);
294 			module_put(mod);
295 			csdev->ect_enabled = false;
296 		}
297 	}
298 
299 	/* output warning if ECT enable is preventing trace operation */
300 	if (ect_ret)
301 		dev_info(&csdev->dev, "Associated ECT device (%s) %s failed\n",
302 			 dev_name(&ect_csdev->dev),
303 			 enable ? "enable" : "disable");
304 	return ect_ret;
305 }
306 
307 /*
308  * Set the associated ect / cti device while holding the coresight_mutex
309  * to avoid a race with coresight_enable that may try to use this value.
310  */
coresight_set_assoc_ectdev_mutex(struct coresight_device * csdev,struct coresight_device * ect_csdev)311 void coresight_set_assoc_ectdev_mutex(struct coresight_device *csdev,
312 				      struct coresight_device *ect_csdev)
313 {
314 	mutex_lock(&coresight_mutex);
315 	csdev->ect_dev = ect_csdev;
316 	mutex_unlock(&coresight_mutex);
317 }
318 EXPORT_SYMBOL_GPL(coresight_set_assoc_ectdev_mutex);
319 
coresight_enable_sink(struct coresight_device * csdev,u32 mode,void * data)320 static int coresight_enable_sink(struct coresight_device *csdev,
321 				 u32 mode, void *data)
322 {
323 	int ret;
324 
325 	/*
326 	 * We need to make sure the "new" session is compatible with the
327 	 * existing "mode" of operation.
328 	 */
329 	if (!sink_ops(csdev)->enable)
330 		return -EINVAL;
331 
332 	ret = coresight_control_assoc_ectdev(csdev, true);
333 	if (ret)
334 		return ret;
335 	ret = sink_ops(csdev)->enable(csdev, mode, data);
336 	if (ret) {
337 		coresight_control_assoc_ectdev(csdev, false);
338 		return ret;
339 	}
340 	csdev->enable = true;
341 
342 	return 0;
343 }
344 
coresight_disable_sink(struct coresight_device * csdev)345 static void coresight_disable_sink(struct coresight_device *csdev)
346 {
347 	int ret;
348 
349 	if (!sink_ops(csdev)->disable)
350 		return;
351 
352 	ret = sink_ops(csdev)->disable(csdev);
353 	if (ret)
354 		return;
355 	coresight_control_assoc_ectdev(csdev, false);
356 	csdev->enable = false;
357 }
358 
coresight_enable_link(struct coresight_device * csdev,struct coresight_device * parent,struct coresight_device * child)359 static int coresight_enable_link(struct coresight_device *csdev,
360 				 struct coresight_device *parent,
361 				 struct coresight_device *child)
362 {
363 	int ret = 0;
364 	int link_subtype;
365 	int inport, outport;
366 
367 	if (!parent || !child)
368 		return -EINVAL;
369 
370 	inport = coresight_find_link_inport(csdev, parent);
371 	outport = coresight_find_link_outport(csdev, child);
372 	link_subtype = csdev->subtype.link_subtype;
373 
374 	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && inport < 0)
375 		return inport;
376 	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && outport < 0)
377 		return outport;
378 
379 	if (link_ops(csdev)->enable) {
380 		ret = coresight_control_assoc_ectdev(csdev, true);
381 		if (!ret) {
382 			ret = link_ops(csdev)->enable(csdev, inport, outport);
383 			if (ret)
384 				coresight_control_assoc_ectdev(csdev, false);
385 		}
386 	}
387 
388 	if (!ret)
389 		csdev->enable = true;
390 
391 	return ret;
392 }
393 
coresight_disable_link(struct coresight_device * csdev,struct coresight_device * parent,struct coresight_device * child)394 static void coresight_disable_link(struct coresight_device *csdev,
395 				   struct coresight_device *parent,
396 				   struct coresight_device *child)
397 {
398 	int i, nr_conns;
399 	int link_subtype;
400 	int inport, outport;
401 
402 	if (!parent || !child)
403 		return;
404 
405 	inport = coresight_find_link_inport(csdev, parent);
406 	outport = coresight_find_link_outport(csdev, child);
407 	link_subtype = csdev->subtype.link_subtype;
408 
409 	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
410 		nr_conns = csdev->pdata->nr_inport;
411 	} else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
412 		nr_conns = csdev->pdata->nr_outport;
413 	} else {
414 		nr_conns = 1;
415 	}
416 
417 	if (link_ops(csdev)->disable) {
418 		link_ops(csdev)->disable(csdev, inport, outport);
419 		coresight_control_assoc_ectdev(csdev, false);
420 	}
421 
422 	for (i = 0; i < nr_conns; i++)
423 		if (atomic_read(&csdev->refcnt[i]) != 0)
424 			return;
425 
426 	csdev->enable = false;
427 }
428 
coresight_enable_source(struct coresight_device * csdev,u32 mode)429 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
430 {
431 	int ret;
432 
433 	if (!coresight_source_is_unique(csdev)) {
434 		dev_warn(&csdev->dev, "traceID %d not unique\n",
435 			 source_ops(csdev)->trace_id(csdev));
436 		return -EINVAL;
437 	}
438 
439 	if (!csdev->enable) {
440 		if (source_ops(csdev)->enable) {
441 			ret = coresight_control_assoc_ectdev(csdev, true);
442 			if (ret)
443 				return ret;
444 			ret = source_ops(csdev)->enable(csdev, NULL, mode);
445 			if (ret) {
446 				coresight_control_assoc_ectdev(csdev, false);
447 				return ret;
448 			};
449 		}
450 		csdev->enable = true;
451 	}
452 
453 	atomic_inc(csdev->refcnt);
454 
455 	return 0;
456 }
457 
458 /**
459  *  coresight_disable_source - Drop the reference count by 1 and disable
460  *  the device if there are no users left.
461  *
462  *  @csdev - The coresight device to disable
463  *
464  *  Returns true if the device has been disabled.
465  */
coresight_disable_source(struct coresight_device * csdev)466 static bool coresight_disable_source(struct coresight_device *csdev)
467 {
468 	if (atomic_dec_return(csdev->refcnt) == 0) {
469 		if (source_ops(csdev)->disable)
470 			source_ops(csdev)->disable(csdev, NULL);
471 		coresight_control_assoc_ectdev(csdev, false);
472 		csdev->enable = false;
473 	}
474 	return !csdev->enable;
475 }
476 
477 /*
478  * coresight_disable_path_from : Disable components in the given path beyond
479  * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
480  * disabled.
481  */
coresight_disable_path_from(struct list_head * path,struct coresight_node * nd)482 static void coresight_disable_path_from(struct list_head *path,
483 					struct coresight_node *nd)
484 {
485 	u32 type;
486 	struct coresight_device *csdev, *parent, *child;
487 
488 	if (!nd)
489 		nd = list_first_entry(path, struct coresight_node, link);
490 
491 	list_for_each_entry_continue(nd, path, link) {
492 		csdev = nd->csdev;
493 		type = csdev->type;
494 
495 		/*
496 		 * ETF devices are tricky... They can be a link or a sink,
497 		 * depending on how they are configured.  If an ETF has been
498 		 * "activated" it will be configured as a sink, otherwise
499 		 * go ahead with the link configuration.
500 		 */
501 		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
502 			type = (csdev == coresight_get_sink(path)) ?
503 						CORESIGHT_DEV_TYPE_SINK :
504 						CORESIGHT_DEV_TYPE_LINK;
505 
506 		switch (type) {
507 		case CORESIGHT_DEV_TYPE_SINK:
508 			coresight_disable_sink(csdev);
509 			break;
510 		case CORESIGHT_DEV_TYPE_SOURCE:
511 			/*
512 			 * We skip the first node in the path assuming that it
513 			 * is the source. So we don't expect a source device in
514 			 * the middle of a path.
515 			 */
516 			WARN_ON(1);
517 			break;
518 		case CORESIGHT_DEV_TYPE_LINK:
519 			parent = list_prev_entry(nd, link)->csdev;
520 			child = list_next_entry(nd, link)->csdev;
521 			coresight_disable_link(csdev, parent, child);
522 			break;
523 		default:
524 			break;
525 		}
526 	}
527 }
528 
coresight_disable_path(struct list_head * path)529 void coresight_disable_path(struct list_head *path)
530 {
531 	coresight_disable_path_from(path, NULL);
532 }
533 EXPORT_SYMBOL_GPL(coresight_disable_path);
534 
coresight_enable_path(struct list_head * path,u32 mode,void * sink_data)535 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
536 {
537 
538 	int ret = 0;
539 	u32 type;
540 	struct coresight_node *nd;
541 	struct coresight_device *csdev, *parent, *child;
542 
543 	list_for_each_entry_reverse(nd, path, link) {
544 		csdev = nd->csdev;
545 		type = csdev->type;
546 
547 		/*
548 		 * ETF devices are tricky... They can be a link or a sink,
549 		 * depending on how they are configured.  If an ETF has been
550 		 * "activated" it will be configured as a sink, otherwise
551 		 * go ahead with the link configuration.
552 		 */
553 		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
554 			type = (csdev == coresight_get_sink(path)) ?
555 						CORESIGHT_DEV_TYPE_SINK :
556 						CORESIGHT_DEV_TYPE_LINK;
557 
558 		switch (type) {
559 		case CORESIGHT_DEV_TYPE_SINK:
560 			ret = coresight_enable_sink(csdev, mode, sink_data);
561 			/*
562 			 * Sink is the first component turned on. If we
563 			 * failed to enable the sink, there are no components
564 			 * that need disabling. Disabling the path here
565 			 * would mean we could disrupt an existing session.
566 			 */
567 			if (ret)
568 				goto out;
569 			break;
570 		case CORESIGHT_DEV_TYPE_SOURCE:
571 			/* sources are enabled from either sysFS or Perf */
572 			break;
573 		case CORESIGHT_DEV_TYPE_LINK:
574 			parent = list_prev_entry(nd, link)->csdev;
575 			child = list_next_entry(nd, link)->csdev;
576 			ret = coresight_enable_link(csdev, parent, child);
577 			if (ret)
578 				goto err;
579 			break;
580 		default:
581 			goto err;
582 		}
583 	}
584 
585 out:
586 	return ret;
587 err:
588 	coresight_disable_path_from(path, nd);
589 	goto out;
590 }
591 
coresight_get_sink(struct list_head * path)592 struct coresight_device *coresight_get_sink(struct list_head *path)
593 {
594 	struct coresight_device *csdev;
595 
596 	if (!path)
597 		return NULL;
598 
599 	csdev = list_last_entry(path, struct coresight_node, link)->csdev;
600 	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
601 	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
602 		return NULL;
603 
604 	return csdev;
605 }
606 
607 static struct coresight_device *
coresight_find_enabled_sink(struct coresight_device * csdev)608 coresight_find_enabled_sink(struct coresight_device *csdev)
609 {
610 	int i;
611 	struct coresight_device *sink = NULL;
612 
613 	if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
614 	     csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
615 	     csdev->activated)
616 		return csdev;
617 
618 	/*
619 	 * Recursively explore each port found on this element.
620 	 */
621 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
622 		struct coresight_device *child_dev;
623 
624 		child_dev = csdev->pdata->conns[i].child_dev;
625 		if (child_dev)
626 			sink = coresight_find_enabled_sink(child_dev);
627 		if (sink)
628 			return sink;
629 	}
630 
631 	return NULL;
632 }
633 
634 /**
635  * coresight_get_enabled_sink - returns the first enabled sink using
636  * connection based search starting from the source reference
637  *
638  * @source: Coresight source device reference
639  */
640 struct coresight_device *
coresight_get_enabled_sink(struct coresight_device * source)641 coresight_get_enabled_sink(struct coresight_device *source)
642 {
643 	if (!source)
644 		return NULL;
645 
646 	return coresight_find_enabled_sink(source);
647 }
648 
coresight_sink_by_id(struct device * dev,const void * data)649 static int coresight_sink_by_id(struct device *dev, const void *data)
650 {
651 	struct coresight_device *csdev = to_coresight_device(dev);
652 	unsigned long hash;
653 
654 	if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
655 	     csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
656 
657 		if (!csdev->ea)
658 			return 0;
659 		/*
660 		 * See function etm_perf_add_symlink_sink() to know where
661 		 * this comes from.
662 		 */
663 		hash = (unsigned long)csdev->ea->var;
664 
665 		if ((u32)hash == *(u32 *)data)
666 			return 1;
667 	}
668 
669 	return 0;
670 }
671 
672 /**
673  * coresight_get_sink_by_id - returns the sink that matches the id
674  * @id: Id of the sink to match
675  *
676  * The name of a sink is unique, whether it is found on the AMBA bus or
677  * otherwise.  As such the hash of that name can easily be used to identify
678  * a sink.
679  */
coresight_get_sink_by_id(u32 id)680 struct coresight_device *coresight_get_sink_by_id(u32 id)
681 {
682 	struct device *dev = NULL;
683 
684 	dev = bus_find_device(&coresight_bustype, NULL, &id,
685 			      coresight_sink_by_id);
686 
687 	return dev ? to_coresight_device(dev) : NULL;
688 }
689 
690 /**
691  * coresight_get_ref- Helper function to increase reference count to module
692  * and device.
693  * Return true in successful case and power up the device.
694  * Return false when failed to get reference of module.
695  */
coresight_get_ref(struct coresight_device * csdev)696 static inline bool coresight_get_ref(struct coresight_device *csdev)
697 {
698 	struct device *dev = csdev->dev.parent;
699 
700 	/* Make sure the driver can't be removed */
701 	if (!try_module_get(dev->driver->owner))
702 		return false;
703 	/* Make sure the device can't go away */
704 	get_device(dev);
705 	pm_runtime_get_sync(dev);
706 	return true;
707 }
708 
709 /**
710  * coresight_put_ref- Helper function to decrease reference count to module
711  * and device. Power off the device.
712  */
coresight_put_ref(struct coresight_device * csdev)713 static inline void coresight_put_ref(struct coresight_device *csdev)
714 {
715 	struct device *dev = csdev->dev.parent;
716 
717 	pm_runtime_put(dev);
718 	put_device(dev);
719 	module_put(dev->driver->owner);
720 }
721 
722 /*
723  * coresight_grab_device - Power up this device and any of the helper
724  * devices connected to it for trace operation. Since the helper devices
725  * don't appear on the trace path, they should be handled along with the
726  * the master device.
727  */
coresight_grab_device(struct coresight_device * csdev)728 static int coresight_grab_device(struct coresight_device *csdev)
729 {
730 	int i;
731 
732 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
733 		struct coresight_device *child;
734 
735 		child  = csdev->pdata->conns[i].child_dev;
736 		if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
737 			if (!coresight_get_ref(child))
738 				goto err;
739 	}
740 	if (coresight_get_ref(csdev))
741 		return 0;
742 err:
743 	for (i--; i >= 0; i--) {
744 		struct coresight_device *child;
745 
746 		child  = csdev->pdata->conns[i].child_dev;
747 		if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
748 			coresight_put_ref(child);
749 	}
750 	return -ENODEV;
751 }
752 
753 /*
754  * coresight_drop_device - Release this device and any of the helper
755  * devices connected to it.
756  */
coresight_drop_device(struct coresight_device * csdev)757 static void coresight_drop_device(struct coresight_device *csdev)
758 {
759 	int i;
760 
761 	coresight_put_ref(csdev);
762 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
763 		struct coresight_device *child;
764 
765 		child  = csdev->pdata->conns[i].child_dev;
766 		if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
767 			coresight_put_ref(child);
768 	}
769 }
770 
771 /**
772  * _coresight_build_path - recursively build a path from a @csdev to a sink.
773  * @csdev:	The device to start from.
774  * @path:	The list to add devices to.
775  *
776  * The tree of Coresight device is traversed until an activated sink is
777  * found.  From there the sink is added to the list along with all the
778  * devices that led to that point - the end result is a list from source
779  * to sink. In that list the source is the first device and the sink the
780  * last one.
781  */
_coresight_build_path(struct coresight_device * csdev,struct coresight_device * sink,struct list_head * path)782 static int _coresight_build_path(struct coresight_device *csdev,
783 				 struct coresight_device *sink,
784 				 struct list_head *path)
785 {
786 	int i, ret;
787 	bool found = false;
788 	struct coresight_node *node;
789 
790 	/* An activated sink has been found.  Enqueue the element */
791 	if (csdev == sink)
792 		goto out;
793 
794 	if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&
795 	    sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {
796 		if (_coresight_build_path(sink, sink, path) == 0) {
797 			found = true;
798 			goto out;
799 		}
800 	}
801 
802 	/* Not a sink - recursively explore each port found on this element */
803 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
804 		struct coresight_device *child_dev;
805 
806 		child_dev = csdev->pdata->conns[i].child_dev;
807 		if (child_dev &&
808 		    _coresight_build_path(child_dev, sink, path) == 0) {
809 			found = true;
810 			break;
811 		}
812 	}
813 
814 	if (!found)
815 		return -ENODEV;
816 
817 out:
818 	/*
819 	 * A path from this element to a sink has been found.  The elements
820 	 * leading to the sink are already enqueued, all that is left to do
821 	 * is tell the PM runtime core we need this element and add a node
822 	 * for it.
823 	 */
824 	ret = coresight_grab_device(csdev);
825 	if (ret)
826 		return ret;
827 
828 	node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
829 	if (!node)
830 		return -ENOMEM;
831 
832 	node->csdev = csdev;
833 	list_add(&node->link, path);
834 
835 	return 0;
836 }
837 
coresight_build_path(struct coresight_device * source,struct coresight_device * sink)838 struct list_head *coresight_build_path(struct coresight_device *source,
839 				       struct coresight_device *sink)
840 {
841 	struct list_head *path;
842 	int rc;
843 
844 	if (!sink)
845 		return ERR_PTR(-EINVAL);
846 
847 	path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
848 	if (!path)
849 		return ERR_PTR(-ENOMEM);
850 
851 	INIT_LIST_HEAD(path);
852 
853 	rc = _coresight_build_path(source, sink, path);
854 	if (rc) {
855 		kfree(path);
856 		return ERR_PTR(rc);
857 	}
858 
859 	return path;
860 }
861 
862 /**
863  * coresight_release_path - release a previously built path.
864  * @path:	the path to release.
865  *
866  * Go through all the elements of a path and 1) removed it from the list and
867  * 2) free the memory allocated for each node.
868  */
coresight_release_path(struct list_head * path)869 void coresight_release_path(struct list_head *path)
870 {
871 	struct coresight_device *csdev;
872 	struct coresight_node *nd, *next;
873 
874 	list_for_each_entry_safe(nd, next, path, link) {
875 		csdev = nd->csdev;
876 
877 		coresight_drop_device(csdev);
878 		list_del(&nd->link);
879 		kfree(nd);
880 	}
881 
882 	kfree(path);
883 	path = NULL;
884 }
885 
886 /* return true if the device is a suitable type for a default sink */
coresight_is_def_sink_type(struct coresight_device * csdev)887 static inline bool coresight_is_def_sink_type(struct coresight_device *csdev)
888 {
889 	/* sink & correct subtype */
890 	if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
891 	     (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
892 	    (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
893 		return true;
894 	return false;
895 }
896 
897 /**
898  * coresight_select_best_sink - return the best sink for use as default from
899  * the two provided.
900  *
901  * @sink:	current best sink.
902  * @depth:      search depth where current sink was found.
903  * @new_sink:	new sink for comparison with current sink.
904  * @new_depth:  search depth where new sink was found.
905  *
906  * Sinks prioritised according to coresight_dev_subtype_sink, with only
907  * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used.
908  *
909  * Where two sinks of equal priority are found, the sink closest to the
910  * source is used (smallest search depth).
911  *
912  * return @new_sink & update @depth if better than @sink, else return @sink.
913  */
914 static struct coresight_device *
coresight_select_best_sink(struct coresight_device * sink,int * depth,struct coresight_device * new_sink,int new_depth)915 coresight_select_best_sink(struct coresight_device *sink, int *depth,
916 			   struct coresight_device *new_sink, int new_depth)
917 {
918 	bool update = false;
919 
920 	if (!sink) {
921 		/* first found at this level */
922 		update = true;
923 	} else if (new_sink->subtype.sink_subtype >
924 		   sink->subtype.sink_subtype) {
925 		/* found better sink */
926 		update = true;
927 	} else if ((new_sink->subtype.sink_subtype ==
928 		    sink->subtype.sink_subtype) &&
929 		   (*depth > new_depth)) {
930 		/* found same but closer sink */
931 		update = true;
932 	}
933 
934 	if (update)
935 		*depth = new_depth;
936 	return update ? new_sink : sink;
937 }
938 
939 /**
940  * coresight_find_sink - recursive function to walk trace connections from
941  * source to find a suitable default sink.
942  *
943  * @csdev: source / current device to check.
944  * @depth: [in] search depth of calling dev, [out] depth of found sink.
945  *
946  * This will walk the connection path from a source (ETM) till a suitable
947  * sink is encountered and return that sink to the original caller.
948  *
949  * If current device is a plain sink return that & depth, otherwise recursively
950  * call child connections looking for a sink. Select best possible using
951  * coresight_select_best_sink.
952  *
953  * return best sink found, or NULL if not found at this node or child nodes.
954  */
955 static struct coresight_device *
coresight_find_sink(struct coresight_device * csdev,int * depth)956 coresight_find_sink(struct coresight_device *csdev, int *depth)
957 {
958 	int i, curr_depth = *depth + 1, found_depth = 0;
959 	struct coresight_device *found_sink = NULL;
960 
961 	if (coresight_is_def_sink_type(csdev)) {
962 		found_depth = curr_depth;
963 		found_sink = csdev;
964 		if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
965 			goto return_def_sink;
966 		/* look past LINKSINK for something better */
967 	}
968 
969 	/*
970 	 * Not a sink we want - or possible child sink may be better.
971 	 * recursively explore each port found on this element.
972 	 */
973 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
974 		struct coresight_device *child_dev, *sink = NULL;
975 		int child_depth = curr_depth;
976 
977 		child_dev = csdev->pdata->conns[i].child_dev;
978 		if (child_dev)
979 			sink = coresight_find_sink(child_dev, &child_depth);
980 
981 		if (sink)
982 			found_sink = coresight_select_best_sink(found_sink,
983 								&found_depth,
984 								sink,
985 								child_depth);
986 	}
987 
988 return_def_sink:
989 	/* return found sink and depth */
990 	if (found_sink)
991 		*depth = found_depth;
992 	return found_sink;
993 }
994 
995 /**
996  * coresight_find_default_sink: Find a sink suitable for use as a
997  * default sink.
998  *
999  * @csdev: starting source to find a connected sink.
1000  *
1001  * Walks connections graph looking for a suitable sink to enable for the
1002  * supplied source. Uses CoreSight device subtypes and distance from source
1003  * to select the best sink.
1004  *
1005  * If a sink is found, then the default sink for this device is set and
1006  * will be automatically used in future.
1007  *
1008  * Used in cases where the CoreSight user (perf / sysfs) has not selected a
1009  * sink.
1010  */
1011 struct coresight_device *
coresight_find_default_sink(struct coresight_device * csdev)1012 coresight_find_default_sink(struct coresight_device *csdev)
1013 {
1014 	int depth = 0;
1015 
1016 	/* look for a default sink if we have not found for this device */
1017 	if (!csdev->def_sink) {
1018 		if (coresight_is_percpu_source(csdev))
1019 			csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));
1020 		if (!csdev->def_sink)
1021 			csdev->def_sink = coresight_find_sink(csdev, &depth);
1022 	}
1023 	return csdev->def_sink;
1024 }
1025 
coresight_remove_sink_ref(struct device * dev,void * data)1026 static int coresight_remove_sink_ref(struct device *dev, void *data)
1027 {
1028 	struct coresight_device *sink = data;
1029 	struct coresight_device *source = to_coresight_device(dev);
1030 
1031 	if (source->def_sink == sink)
1032 		source->def_sink = NULL;
1033 	return 0;
1034 }
1035 
1036 /**
1037  * coresight_clear_default_sink: Remove all default sink references to the
1038  * supplied sink.
1039  *
1040  * If supplied device is a sink, then check all the bus devices and clear
1041  * out all the references to this sink from the coresight_device def_sink
1042  * parameter.
1043  *
1044  * @csdev: coresight sink - remove references to this from all sources.
1045  */
coresight_clear_default_sink(struct coresight_device * csdev)1046 static void coresight_clear_default_sink(struct coresight_device *csdev)
1047 {
1048 	if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
1049 	    (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
1050 		bus_for_each_dev(&coresight_bustype, NULL, csdev,
1051 				 coresight_remove_sink_ref);
1052 	}
1053 }
1054 
1055 /** coresight_validate_source - make sure a source has the right credentials
1056  *  @csdev:	the device structure for a source.
1057  *  @function:	the function this was called from.
1058  *
1059  * Assumes the coresight_mutex is held.
1060  */
coresight_validate_source(struct coresight_device * csdev,const char * function)1061 static int coresight_validate_source(struct coresight_device *csdev,
1062 				     const char *function)
1063 {
1064 	u32 type, subtype;
1065 
1066 	type = csdev->type;
1067 	subtype = csdev->subtype.source_subtype;
1068 
1069 	if (type != CORESIGHT_DEV_TYPE_SOURCE) {
1070 		dev_err(&csdev->dev, "wrong device type in %s\n", function);
1071 		return -EINVAL;
1072 	}
1073 
1074 	if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
1075 	    subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
1076 		dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
1077 		return -EINVAL;
1078 	}
1079 
1080 	return 0;
1081 }
1082 
coresight_enable(struct coresight_device * csdev)1083 int coresight_enable(struct coresight_device *csdev)
1084 {
1085 	int cpu, ret = 0;
1086 	struct coresight_device *sink;
1087 	struct list_head *path;
1088 	enum coresight_dev_subtype_source subtype;
1089 
1090 	subtype = csdev->subtype.source_subtype;
1091 
1092 	mutex_lock(&coresight_mutex);
1093 
1094 	ret = coresight_validate_source(csdev, __func__);
1095 	if (ret)
1096 		goto out;
1097 
1098 	if (csdev->enable) {
1099 		/*
1100 		 * There could be multiple applications driving the software
1101 		 * source. So keep the refcount for each such user when the
1102 		 * source is already enabled.
1103 		 */
1104 		if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
1105 			atomic_inc(csdev->refcnt);
1106 		goto out;
1107 	}
1108 
1109 	sink = coresight_get_enabled_sink(csdev);
1110 	if (!sink) {
1111 		ret = -EINVAL;
1112 		goto out;
1113 	}
1114 
1115 	path = coresight_build_path(csdev, sink);
1116 	if (IS_ERR(path)) {
1117 		pr_err("building path(s) failed\n");
1118 		ret = PTR_ERR(path);
1119 		goto out;
1120 	}
1121 
1122 	ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
1123 	if (ret)
1124 		goto err_path;
1125 
1126 	ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
1127 	if (ret)
1128 		goto err_source;
1129 
1130 	switch (subtype) {
1131 	case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1132 		/*
1133 		 * When working from sysFS it is important to keep track
1134 		 * of the paths that were created so that they can be
1135 		 * undone in 'coresight_disable()'.  Since there can only
1136 		 * be a single session per tracer (when working from sysFS)
1137 		 * a per-cpu variable will do just fine.
1138 		 */
1139 		cpu = source_ops(csdev)->cpu_id(csdev);
1140 		per_cpu(tracer_path, cpu) = path;
1141 		break;
1142 	case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1143 		stm_path = path;
1144 		break;
1145 	default:
1146 		/* We can't be here */
1147 		break;
1148 	}
1149 
1150 out:
1151 	mutex_unlock(&coresight_mutex);
1152 	return ret;
1153 
1154 err_source:
1155 	coresight_disable_path(path);
1156 
1157 err_path:
1158 	coresight_release_path(path);
1159 	goto out;
1160 }
1161 EXPORT_SYMBOL_GPL(coresight_enable);
1162 
coresight_disable(struct coresight_device * csdev)1163 void coresight_disable(struct coresight_device *csdev)
1164 {
1165 	int cpu, ret;
1166 	struct list_head *path = NULL;
1167 
1168 	mutex_lock(&coresight_mutex);
1169 
1170 	ret = coresight_validate_source(csdev, __func__);
1171 	if (ret)
1172 		goto out;
1173 
1174 	if (!csdev->enable || !coresight_disable_source(csdev))
1175 		goto out;
1176 
1177 	switch (csdev->subtype.source_subtype) {
1178 	case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
1179 		cpu = source_ops(csdev)->cpu_id(csdev);
1180 		path = per_cpu(tracer_path, cpu);
1181 		per_cpu(tracer_path, cpu) = NULL;
1182 		break;
1183 	case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
1184 		path = stm_path;
1185 		stm_path = NULL;
1186 		break;
1187 	default:
1188 		/* We can't be here */
1189 		break;
1190 	}
1191 
1192 	coresight_disable_path(path);
1193 	coresight_release_path(path);
1194 
1195 out:
1196 	mutex_unlock(&coresight_mutex);
1197 }
1198 EXPORT_SYMBOL_GPL(coresight_disable);
1199 
enable_sink_show(struct device * dev,struct device_attribute * attr,char * buf)1200 static ssize_t enable_sink_show(struct device *dev,
1201 				struct device_attribute *attr, char *buf)
1202 {
1203 	struct coresight_device *csdev = to_coresight_device(dev);
1204 
1205 	return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
1206 }
1207 
enable_sink_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1208 static ssize_t enable_sink_store(struct device *dev,
1209 				 struct device_attribute *attr,
1210 				 const char *buf, size_t size)
1211 {
1212 	int ret;
1213 	unsigned long val;
1214 	struct coresight_device *csdev = to_coresight_device(dev);
1215 
1216 	ret = kstrtoul(buf, 10, &val);
1217 	if (ret)
1218 		return ret;
1219 
1220 	if (val)
1221 		csdev->activated = true;
1222 	else
1223 		csdev->activated = false;
1224 
1225 	return size;
1226 
1227 }
1228 static DEVICE_ATTR_RW(enable_sink);
1229 
enable_source_show(struct device * dev,struct device_attribute * attr,char * buf)1230 static ssize_t enable_source_show(struct device *dev,
1231 				  struct device_attribute *attr, char *buf)
1232 {
1233 	struct coresight_device *csdev = to_coresight_device(dev);
1234 
1235 	return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
1236 }
1237 
enable_source_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1238 static ssize_t enable_source_store(struct device *dev,
1239 				   struct device_attribute *attr,
1240 				   const char *buf, size_t size)
1241 {
1242 	int ret = 0;
1243 	unsigned long val;
1244 	struct coresight_device *csdev = to_coresight_device(dev);
1245 
1246 	ret = kstrtoul(buf, 10, &val);
1247 	if (ret)
1248 		return ret;
1249 
1250 	if (val) {
1251 		ret = coresight_enable(csdev);
1252 		if (ret)
1253 			return ret;
1254 	} else {
1255 		coresight_disable(csdev);
1256 	}
1257 
1258 	return size;
1259 }
1260 static DEVICE_ATTR_RW(enable_source);
1261 
1262 static struct attribute *coresight_sink_attrs[] = {
1263 	&dev_attr_enable_sink.attr,
1264 	NULL,
1265 };
1266 ATTRIBUTE_GROUPS(coresight_sink);
1267 
1268 static struct attribute *coresight_source_attrs[] = {
1269 	&dev_attr_enable_source.attr,
1270 	NULL,
1271 };
1272 ATTRIBUTE_GROUPS(coresight_source);
1273 
1274 static struct device_type coresight_dev_type[] = {
1275 	{
1276 		.name = "none",
1277 	},
1278 	{
1279 		.name = "sink",
1280 		.groups = coresight_sink_groups,
1281 	},
1282 	{
1283 		.name = "link",
1284 	},
1285 	{
1286 		.name = "linksink",
1287 		.groups = coresight_sink_groups,
1288 	},
1289 	{
1290 		.name = "source",
1291 		.groups = coresight_source_groups,
1292 	},
1293 	{
1294 		.name = "helper",
1295 	},
1296 	{
1297 		.name = "ect",
1298 	},
1299 };
1300 
coresight_device_release(struct device * dev)1301 static void coresight_device_release(struct device *dev)
1302 {
1303 	struct coresight_device *csdev = to_coresight_device(dev);
1304 
1305 	fwnode_handle_put(csdev->dev.fwnode);
1306 	kfree(csdev->refcnt);
1307 	kfree(csdev);
1308 }
1309 
coresight_orphan_match(struct device * dev,void * data)1310 static int coresight_orphan_match(struct device *dev, void *data)
1311 {
1312 	int i, ret = 0;
1313 	bool still_orphan = false;
1314 	struct coresight_device *csdev, *i_csdev;
1315 	struct coresight_connection *conn;
1316 
1317 	csdev = data;
1318 	i_csdev = to_coresight_device(dev);
1319 
1320 	/* No need to check oneself */
1321 	if (csdev == i_csdev)
1322 		return 0;
1323 
1324 	/* Move on to another component if no connection is orphan */
1325 	if (!i_csdev->orphan)
1326 		return 0;
1327 	/*
1328 	 * Circle throuch all the connection of that component.  If we find
1329 	 * an orphan connection whose name matches @csdev, link it.
1330 	 */
1331 	for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
1332 		conn = &i_csdev->pdata->conns[i];
1333 
1334 		/* Skip the port if FW doesn't describe it */
1335 		if (!conn->child_fwnode)
1336 			continue;
1337 		/* We have found at least one orphan connection */
1338 		if (conn->child_dev == NULL) {
1339 			/* Does it match this newly added device? */
1340 			if (conn->child_fwnode == csdev->dev.fwnode) {
1341 				ret = coresight_make_links(i_csdev,
1342 							   conn, csdev);
1343 				if (ret)
1344 					return ret;
1345 			} else {
1346 				/* This component still has an orphan */
1347 				still_orphan = true;
1348 			}
1349 		}
1350 	}
1351 
1352 	i_csdev->orphan = still_orphan;
1353 
1354 	/*
1355 	 * Returning '0' in case we didn't encounter any error,
1356 	 * ensures that all known component on the bus will be checked.
1357 	 */
1358 	return 0;
1359 }
1360 
coresight_fixup_orphan_conns(struct coresight_device * csdev)1361 static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
1362 {
1363 	return bus_for_each_dev(&coresight_bustype, NULL,
1364 			 csdev, coresight_orphan_match);
1365 }
1366 
1367 
coresight_fixup_device_conns(struct coresight_device * csdev)1368 static int coresight_fixup_device_conns(struct coresight_device *csdev)
1369 {
1370 	int i, ret = 0;
1371 
1372 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
1373 		struct coresight_connection *conn = &csdev->pdata->conns[i];
1374 
1375 		if (!conn->child_fwnode)
1376 			continue;
1377 		conn->child_dev =
1378 			coresight_find_csdev_by_fwnode(conn->child_fwnode);
1379 		if (conn->child_dev && conn->child_dev->has_conns_grp) {
1380 			ret = coresight_make_links(csdev, conn,
1381 						   conn->child_dev);
1382 			if (ret)
1383 				break;
1384 		} else {
1385 			csdev->orphan = true;
1386 		}
1387 	}
1388 
1389 	return ret;
1390 }
1391 
coresight_remove_match(struct device * dev,void * data)1392 static int coresight_remove_match(struct device *dev, void *data)
1393 {
1394 	int i;
1395 	struct coresight_device *csdev, *iterator;
1396 	struct coresight_connection *conn;
1397 
1398 	csdev = data;
1399 	iterator = to_coresight_device(dev);
1400 
1401 	/* No need to check oneself */
1402 	if (csdev == iterator)
1403 		return 0;
1404 
1405 	/*
1406 	 * Circle throuch all the connection of that component.  If we find
1407 	 * a connection whose name matches @csdev, remove it.
1408 	 */
1409 	for (i = 0; i < iterator->pdata->nr_outport; i++) {
1410 		conn = &iterator->pdata->conns[i];
1411 
1412 		if (conn->child_dev == NULL || conn->child_fwnode == NULL)
1413 			continue;
1414 
1415 		if (csdev->dev.fwnode == conn->child_fwnode) {
1416 			iterator->orphan = true;
1417 			coresight_remove_links(iterator, conn);
1418 			/*
1419 			 * Drop the reference to the handle for the remote
1420 			 * device acquired in parsing the connections from
1421 			 * platform data.
1422 			 */
1423 			fwnode_handle_put(conn->child_fwnode);
1424 			conn->child_fwnode = NULL;
1425 			/* No need to continue */
1426 			break;
1427 		}
1428 	}
1429 
1430 	/*
1431 	 * Returning '0' ensures that all known component on the
1432 	 * bus will be checked.
1433 	 */
1434 	return 0;
1435 }
1436 
1437 /*
1438  * coresight_remove_conns - Remove references to this given devices
1439  * from the connections of other devices.
1440  */
coresight_remove_conns(struct coresight_device * csdev)1441 static void coresight_remove_conns(struct coresight_device *csdev)
1442 {
1443 	/*
1444 	 * Another device will point to this device only if there is
1445 	 * an output port connected to this one. i.e, if the device
1446 	 * doesn't have at least one input port, there is no point
1447 	 * in searching all the devices.
1448 	 */
1449 	if (csdev->pdata->nr_inport)
1450 		bus_for_each_dev(&coresight_bustype, NULL,
1451 				 csdev, coresight_remove_match);
1452 }
1453 
1454 /**
1455  * coresight_timeout - loop until a bit has changed to a specific register
1456  *			state.
1457  * @csa: coresight device access for the device
1458  * @offset: Offset of the register from the base of the device.
1459  * @position: the position of the bit of interest.
1460  * @value: the value the bit should have.
1461  *
1462  * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1463  * TIMEOUT_US has elapsed, which ever happens first.
1464  */
coresight_timeout(struct csdev_access * csa,u32 offset,int position,int value)1465 int coresight_timeout(struct csdev_access *csa, u32 offset,
1466 		      int position, int value)
1467 {
1468 	int i;
1469 	u32 val;
1470 
1471 	for (i = TIMEOUT_US; i > 0; i--) {
1472 		val = csdev_access_read32(csa, offset);
1473 		/* waiting on the bit to go from 0 to 1 */
1474 		if (value) {
1475 			if (val & BIT(position))
1476 				return 0;
1477 		/* waiting on the bit to go from 1 to 0 */
1478 		} else {
1479 			if (!(val & BIT(position)))
1480 				return 0;
1481 		}
1482 
1483 		/*
1484 		 * Delay is arbitrary - the specification doesn't say how long
1485 		 * we are expected to wait.  Extra check required to make sure
1486 		 * we don't wait needlessly on the last iteration.
1487 		 */
1488 		if (i - 1)
1489 			udelay(1);
1490 	}
1491 
1492 	return -EAGAIN;
1493 }
1494 EXPORT_SYMBOL_GPL(coresight_timeout);
1495 
coresight_relaxed_read32(struct coresight_device * csdev,u32 offset)1496 u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset)
1497 {
1498 	return csdev_access_relaxed_read32(&csdev->access, offset);
1499 }
1500 
coresight_read32(struct coresight_device * csdev,u32 offset)1501 u32 coresight_read32(struct coresight_device *csdev, u32 offset)
1502 {
1503 	return csdev_access_read32(&csdev->access, offset);
1504 }
1505 
coresight_relaxed_write32(struct coresight_device * csdev,u32 val,u32 offset)1506 void coresight_relaxed_write32(struct coresight_device *csdev,
1507 			       u32 val, u32 offset)
1508 {
1509 	csdev_access_relaxed_write32(&csdev->access, val, offset);
1510 }
1511 
coresight_write32(struct coresight_device * csdev,u32 val,u32 offset)1512 void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset)
1513 {
1514 	csdev_access_write32(&csdev->access, val, offset);
1515 }
1516 
coresight_relaxed_read64(struct coresight_device * csdev,u32 offset)1517 u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset)
1518 {
1519 	return csdev_access_relaxed_read64(&csdev->access, offset);
1520 }
1521 
coresight_read64(struct coresight_device * csdev,u32 offset)1522 u64 coresight_read64(struct coresight_device *csdev, u32 offset)
1523 {
1524 	return csdev_access_read64(&csdev->access, offset);
1525 }
1526 
coresight_relaxed_write64(struct coresight_device * csdev,u64 val,u32 offset)1527 void coresight_relaxed_write64(struct coresight_device *csdev,
1528 			       u64 val, u32 offset)
1529 {
1530 	csdev_access_relaxed_write64(&csdev->access, val, offset);
1531 }
1532 
coresight_write64(struct coresight_device * csdev,u64 val,u32 offset)1533 void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset)
1534 {
1535 	csdev_access_write64(&csdev->access, val, offset);
1536 }
1537 
1538 /*
1539  * coresight_release_platform_data: Release references to the devices connected
1540  * to the output port of this device.
1541  */
coresight_release_platform_data(struct coresight_device * csdev,struct coresight_platform_data * pdata)1542 void coresight_release_platform_data(struct coresight_device *csdev,
1543 				     struct coresight_platform_data *pdata)
1544 {
1545 	int i;
1546 	struct coresight_connection *conns = pdata->conns;
1547 
1548 	for (i = 0; i < pdata->nr_outport; i++) {
1549 		/* If we have made the links, remove them now */
1550 		if (csdev && conns[i].child_dev)
1551 			coresight_remove_links(csdev, &conns[i]);
1552 		/*
1553 		 * Drop the refcount and clear the handle as this device
1554 		 * is going away
1555 		 */
1556 		if (conns[i].child_fwnode) {
1557 			fwnode_handle_put(conns[i].child_fwnode);
1558 			pdata->conns[i].child_fwnode = NULL;
1559 		}
1560 	}
1561 	if (csdev)
1562 		coresight_remove_conns_sysfs_group(csdev);
1563 }
1564 
coresight_register(struct coresight_desc * desc)1565 struct coresight_device *coresight_register(struct coresight_desc *desc)
1566 {
1567 	int ret;
1568 	int link_subtype;
1569 	int nr_refcnts = 1;
1570 	atomic_t *refcnts = NULL;
1571 	struct coresight_device *csdev;
1572 	bool registered = false;
1573 
1574 	csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1575 	if (!csdev) {
1576 		ret = -ENOMEM;
1577 		goto err_out;
1578 	}
1579 
1580 	if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1581 	    desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1582 		link_subtype = desc->subtype.link_subtype;
1583 
1584 		if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1585 			nr_refcnts = desc->pdata->nr_inport;
1586 		else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1587 			nr_refcnts = desc->pdata->nr_outport;
1588 	}
1589 
1590 	refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1591 	if (!refcnts) {
1592 		ret = -ENOMEM;
1593 		kfree(csdev);
1594 		goto err_out;
1595 	}
1596 
1597 	csdev->refcnt = refcnts;
1598 
1599 	csdev->pdata = desc->pdata;
1600 
1601 	csdev->type = desc->type;
1602 	csdev->subtype = desc->subtype;
1603 	csdev->ops = desc->ops;
1604 	csdev->access = desc->access;
1605 	csdev->orphan = false;
1606 
1607 	csdev->dev.type = &coresight_dev_type[desc->type];
1608 	csdev->dev.groups = desc->groups;
1609 	csdev->dev.parent = desc->dev;
1610 	csdev->dev.release = coresight_device_release;
1611 	csdev->dev.bus = &coresight_bustype;
1612 	/*
1613 	 * Hold the reference to our parent device. This will be
1614 	 * dropped only in coresight_device_release().
1615 	 */
1616 	csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1617 	dev_set_name(&csdev->dev, "%s", desc->name);
1618 
1619 	/*
1620 	 * Make sure the device registration and the connection fixup
1621 	 * are synchronised, so that we don't see uninitialised devices
1622 	 * on the coresight bus while trying to resolve the connections.
1623 	 */
1624 	mutex_lock(&coresight_mutex);
1625 
1626 	ret = device_register(&csdev->dev);
1627 	if (ret) {
1628 		put_device(&csdev->dev);
1629 		/*
1630 		 * All resources are free'd explicitly via
1631 		 * coresight_device_release(), triggered from put_device().
1632 		 */
1633 		goto out_unlock;
1634 	}
1635 
1636 	if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1637 	    csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1638 		ret = etm_perf_add_symlink_sink(csdev);
1639 
1640 		if (ret) {
1641 			device_unregister(&csdev->dev);
1642 			/*
1643 			 * As with the above, all resources are free'd
1644 			 * explicitly via coresight_device_release() triggered
1645 			 * from put_device(), which is in turn called from
1646 			 * function device_unregister().
1647 			 */
1648 			goto out_unlock;
1649 		}
1650 	}
1651 	/* Device is now registered */
1652 	registered = true;
1653 
1654 	ret = coresight_create_conns_sysfs_group(csdev);
1655 	if (!ret)
1656 		ret = coresight_fixup_device_conns(csdev);
1657 	if (!ret)
1658 		ret = coresight_fixup_orphan_conns(csdev);
1659 	if (!ret && cti_assoc_ops && cti_assoc_ops->add)
1660 		cti_assoc_ops->add(csdev);
1661 
1662 out_unlock:
1663 	mutex_unlock(&coresight_mutex);
1664 	/* Success */
1665 	if (!ret)
1666 		return csdev;
1667 
1668 	/* Unregister the device if needed */
1669 	if (registered) {
1670 		coresight_unregister(csdev);
1671 		return ERR_PTR(ret);
1672 	}
1673 
1674 err_out:
1675 	/* Cleanup the connection information */
1676 	coresight_release_platform_data(NULL, desc->pdata);
1677 	return ERR_PTR(ret);
1678 }
1679 EXPORT_SYMBOL_GPL(coresight_register);
1680 
coresight_unregister(struct coresight_device * csdev)1681 void coresight_unregister(struct coresight_device *csdev)
1682 {
1683 	etm_perf_del_symlink_sink(csdev);
1684 	/* Remove references of that device in the topology */
1685 	if (cti_assoc_ops && cti_assoc_ops->remove)
1686 		cti_assoc_ops->remove(csdev);
1687 	coresight_remove_conns(csdev);
1688 	coresight_clear_default_sink(csdev);
1689 	coresight_release_platform_data(csdev, csdev->pdata);
1690 	device_unregister(&csdev->dev);
1691 }
1692 EXPORT_SYMBOL_GPL(coresight_unregister);
1693 
1694 
1695 /*
1696  * coresight_search_device_idx - Search the fwnode handle of a device
1697  * in the given dev_idx list. Must be called with the coresight_mutex held.
1698  *
1699  * Returns the index of the entry, when found. Otherwise, -ENOENT.
1700  */
coresight_search_device_idx(struct coresight_dev_list * dict,struct fwnode_handle * fwnode)1701 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1702 					      struct fwnode_handle *fwnode)
1703 {
1704 	int i;
1705 
1706 	for (i = 0; i < dict->nr_idx; i++)
1707 		if (dict->fwnode_list[i] == fwnode)
1708 			return i;
1709 	return -ENOENT;
1710 }
1711 
coresight_loses_context_with_cpu(struct device * dev)1712 bool coresight_loses_context_with_cpu(struct device *dev)
1713 {
1714 	return fwnode_property_present(dev_fwnode(dev),
1715 				       "arm,coresight-loses-context-with-cpu");
1716 }
1717 EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
1718 
1719 /*
1720  * coresight_alloc_device_name - Get an index for a given device in the
1721  * device index list specific to a driver. An index is allocated for a
1722  * device and is tracked with the fwnode_handle to prevent allocating
1723  * duplicate indices for the same device (e.g, if we defer probing of
1724  * a device due to dependencies), in case the index is requested again.
1725  */
coresight_alloc_device_name(struct coresight_dev_list * dict,struct device * dev)1726 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1727 				  struct device *dev)
1728 {
1729 	int idx;
1730 	char *name = NULL;
1731 	struct fwnode_handle **list;
1732 
1733 	mutex_lock(&coresight_mutex);
1734 
1735 	idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1736 	if (idx < 0) {
1737 		/* Make space for the new entry */
1738 		idx = dict->nr_idx;
1739 		list = krealloc(dict->fwnode_list,
1740 				(idx + 1) * sizeof(*dict->fwnode_list),
1741 				GFP_KERNEL);
1742 		if (ZERO_OR_NULL_PTR(list)) {
1743 			idx = -ENOMEM;
1744 			goto done;
1745 		}
1746 
1747 		list[idx] = dev_fwnode(dev);
1748 		dict->fwnode_list = list;
1749 		dict->nr_idx = idx + 1;
1750 	}
1751 
1752 	name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1753 done:
1754 	mutex_unlock(&coresight_mutex);
1755 	return name;
1756 }
1757 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1758 
1759 struct bus_type coresight_bustype = {
1760 	.name	= "coresight",
1761 };
1762 
coresight_init(void)1763 static int __init coresight_init(void)
1764 {
1765 	int ret;
1766 
1767 	ret = bus_register(&coresight_bustype);
1768 	if (ret)
1769 		return ret;
1770 
1771 	ret = etm_perf_init();
1772 	if (ret)
1773 		bus_unregister(&coresight_bustype);
1774 
1775 	return ret;
1776 }
1777 
coresight_exit(void)1778 static void __exit coresight_exit(void)
1779 {
1780 	etm_perf_exit();
1781 	bus_unregister(&coresight_bustype);
1782 }
1783 
1784 module_init(coresight_init);
1785 module_exit(coresight_exit);
1786 
1787 MODULE_LICENSE("GPL v2");
1788 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1789 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1790 MODULE_DESCRIPTION("Arm CoreSight tracer driver");
1791