xref: /linux/drivers/hwtracing/coresight/coresight-core.c (revision 13845bdc869f136f92ad3d40ea09b867bb4ce467)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2012, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/build_bug.h>
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/types.h>
10 #include <linux/device.h>
11 #include <linux/io.h>
12 #include <linux/err.h>
13 #include <linux/export.h>
14 #include <linux/slab.h>
15 #include <linux/stringhash.h>
16 #include <linux/mutex.h>
17 #include <linux/clk.h>
18 #include <linux/coresight.h>
19 #include <linux/property.h>
20 #include <linux/delay.h>
21 #include <linux/pm_runtime.h>
22 
23 #include "coresight-etm-perf.h"
24 #include "coresight-priv.h"
25 #include "coresight-syscfg.h"
26 
27 /*
28  * Mutex used to lock all sysfs enable and disable actions and loading and
29  * unloading devices by the Coresight core.
30  */
31 DEFINE_MUTEX(coresight_mutex);
32 static DEFINE_PER_CPU(struct coresight_device *, csdev_sink);
33 
34 /**
35  * struct coresight_node - elements of a path, from source to sink
36  * @csdev:	Address of an element.
37  * @link:	hook to the list.
38  */
39 struct coresight_node {
40 	struct coresight_device *csdev;
41 	struct list_head link;
42 };
43 
44 /*
45  * When losing synchronisation a new barrier packet needs to be inserted at the
46  * beginning of the data collected in a buffer.  That way the decoder knows that
47  * it needs to look for another sync sequence.
48  */
49 const u32 coresight_barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
50 EXPORT_SYMBOL_GPL(coresight_barrier_pkt);
51 
52 static const struct cti_assoc_op *cti_assoc_ops;
53 
54 void coresight_set_cti_ops(const struct cti_assoc_op *cti_op)
55 {
56 	cti_assoc_ops = cti_op;
57 }
58 EXPORT_SYMBOL_GPL(coresight_set_cti_ops);
59 
60 void coresight_remove_cti_ops(void)
61 {
62 	cti_assoc_ops = NULL;
63 }
64 EXPORT_SYMBOL_GPL(coresight_remove_cti_ops);
65 
66 void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev)
67 {
68 	per_cpu(csdev_sink, cpu) = csdev;
69 }
70 EXPORT_SYMBOL_GPL(coresight_set_percpu_sink);
71 
72 struct coresight_device *coresight_get_percpu_sink(int cpu)
73 {
74 	return per_cpu(csdev_sink, cpu);
75 }
76 EXPORT_SYMBOL_GPL(coresight_get_percpu_sink);
77 
78 static struct coresight_device *coresight_get_source(struct list_head *path)
79 {
80 	struct coresight_device *csdev;
81 
82 	if (!path)
83 		return NULL;
84 
85 	csdev = list_first_entry(path, struct coresight_node, link)->csdev;
86 	if (!coresight_is_device_source(csdev))
87 		return NULL;
88 
89 	return csdev;
90 }
91 
92 /**
93  * coresight_blocks_source - checks whether the connection matches the source
94  * of path if connection is bound to specific source.
95  * @src:	The source device of the trace path
96  * @conn:	The connection of one outport
97  *
98  * Return false if the connection doesn't have a source binded or source of the
99  * path matches the source binds to connection.
100  */
101 static bool coresight_blocks_source(struct coresight_device *src,
102 				    struct coresight_connection *conn)
103 {
104 	return conn->filter_src_fwnode && (conn->filter_src_dev != src);
105 }
106 
107 static struct coresight_connection *
108 coresight_find_out_connection(struct coresight_device *csdev,
109 			      struct coresight_device *out_dev,
110 			      struct coresight_device *trace_src)
111 {
112 	int i;
113 	struct coresight_connection *conn;
114 
115 	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
116 		conn = csdev->pdata->out_conns[i];
117 		if (coresight_blocks_source(trace_src, conn))
118 			continue;
119 		if (conn->dest_dev == out_dev)
120 			return conn;
121 	}
122 
123 	dev_err(&csdev->dev,
124 		"couldn't find output connection, csdev: %s, out_dev: %s\n",
125 		dev_name(&csdev->dev), dev_name(&out_dev->dev));
126 
127 	return ERR_PTR(-ENODEV);
128 }
129 
130 static inline u32 coresight_read_claim_tags(struct coresight_device *csdev)
131 {
132 	return csdev_access_relaxed_read32(&csdev->access, CORESIGHT_CLAIMCLR);
133 }
134 
135 static inline bool coresight_is_claimed_self_hosted(struct coresight_device *csdev)
136 {
137 	return coresight_read_claim_tags(csdev) == CORESIGHT_CLAIM_SELF_HOSTED;
138 }
139 
140 static inline bool coresight_is_claimed_any(struct coresight_device *csdev)
141 {
142 	return coresight_read_claim_tags(csdev) != 0;
143 }
144 
145 static inline void coresight_set_claim_tags(struct coresight_device *csdev)
146 {
147 	csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
148 				     CORESIGHT_CLAIMSET);
149 	isb();
150 }
151 
152 static inline void coresight_clear_claim_tags(struct coresight_device *csdev)
153 {
154 	csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
155 				     CORESIGHT_CLAIMCLR);
156 	isb();
157 }
158 
159 /*
160  * coresight_claim_device_unlocked : Claim the device for self-hosted usage
161  * to prevent an external tool from touching this device. As per PSCI
162  * standards, section "Preserving the execution context" => "Debug and Trace
163  * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
164  * DBGCLAIM[0] is reserved for external tools.
165  *
166  * Called with CS_UNLOCKed for the component.
167  * Returns : 0 on success
168  */
169 int coresight_claim_device_unlocked(struct coresight_device *csdev)
170 {
171 	if (WARN_ON(!csdev))
172 		return -EINVAL;
173 
174 	if (coresight_is_claimed_any(csdev))
175 		return -EBUSY;
176 
177 	coresight_set_claim_tags(csdev);
178 	if (coresight_is_claimed_self_hosted(csdev))
179 		return 0;
180 	/* There was a race setting the tags, clean up and fail */
181 	coresight_clear_claim_tags(csdev);
182 	return -EBUSY;
183 }
184 EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked);
185 
186 int coresight_claim_device(struct coresight_device *csdev)
187 {
188 	int rc;
189 
190 	if (WARN_ON(!csdev))
191 		return -EINVAL;
192 
193 	CS_UNLOCK(csdev->access.base);
194 	rc = coresight_claim_device_unlocked(csdev);
195 	CS_LOCK(csdev->access.base);
196 
197 	return rc;
198 }
199 EXPORT_SYMBOL_GPL(coresight_claim_device);
200 
201 /*
202  * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
203  * Called with CS_UNLOCKed for the component.
204  */
205 void coresight_disclaim_device_unlocked(struct coresight_device *csdev)
206 {
207 
208 	if (WARN_ON(!csdev))
209 		return;
210 
211 	if (coresight_is_claimed_self_hosted(csdev))
212 		coresight_clear_claim_tags(csdev);
213 	else
214 		/*
215 		 * The external agent may have not honoured our claim
216 		 * and has manipulated it. Or something else has seriously
217 		 * gone wrong in our driver.
218 		 */
219 		WARN_ON_ONCE(1);
220 }
221 EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked);
222 
223 void coresight_disclaim_device(struct coresight_device *csdev)
224 {
225 	if (WARN_ON(!csdev))
226 		return;
227 
228 	CS_UNLOCK(csdev->access.base);
229 	coresight_disclaim_device_unlocked(csdev);
230 	CS_LOCK(csdev->access.base);
231 }
232 EXPORT_SYMBOL_GPL(coresight_disclaim_device);
233 
234 /*
235  * Add a helper as an output device. This function takes the @coresight_mutex
236  * because it's assumed that it's called from the helper device, outside of the
237  * core code where the mutex would already be held. Don't add new calls to this
238  * from inside the core code, instead try to add the new helper to the DT and
239  * ACPI where it will be picked up and linked automatically.
240  */
241 void coresight_add_helper(struct coresight_device *csdev,
242 			  struct coresight_device *helper)
243 {
244 	int i;
245 	struct coresight_connection conn = {};
246 	struct coresight_connection *new_conn;
247 
248 	mutex_lock(&coresight_mutex);
249 	conn.dest_fwnode = fwnode_handle_get(dev_fwnode(&helper->dev));
250 	conn.dest_dev = helper;
251 	conn.dest_port = conn.src_port = -1;
252 	conn.src_dev = csdev;
253 
254 	/*
255 	 * Check for duplicates because this is called every time a helper
256 	 * device is re-loaded. Existing connections will get re-linked
257 	 * automatically.
258 	 */
259 	for (i = 0; i < csdev->pdata->nr_outconns; ++i)
260 		if (csdev->pdata->out_conns[i]->dest_fwnode == conn.dest_fwnode)
261 			goto unlock;
262 
263 	new_conn = coresight_add_out_conn(csdev->dev.parent, csdev->pdata,
264 					  &conn);
265 	if (!IS_ERR(new_conn))
266 		coresight_add_in_conn(new_conn);
267 
268 unlock:
269 	mutex_unlock(&coresight_mutex);
270 }
271 EXPORT_SYMBOL_GPL(coresight_add_helper);
272 
273 static int coresight_enable_sink(struct coresight_device *csdev,
274 				 enum cs_mode mode, void *data)
275 {
276 	return sink_ops(csdev)->enable(csdev, mode, data);
277 }
278 
279 static void coresight_disable_sink(struct coresight_device *csdev)
280 {
281 	sink_ops(csdev)->disable(csdev);
282 }
283 
284 static int coresight_enable_link(struct coresight_device *csdev,
285 				 struct coresight_device *parent,
286 				 struct coresight_device *child,
287 				 struct coresight_device *source)
288 {
289 	int link_subtype;
290 	struct coresight_connection *inconn, *outconn;
291 
292 	if (!parent || !child)
293 		return -EINVAL;
294 
295 	inconn = coresight_find_out_connection(parent, csdev, source);
296 	outconn = coresight_find_out_connection(csdev, child, source);
297 	link_subtype = csdev->subtype.link_subtype;
298 
299 	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && IS_ERR(inconn))
300 		return PTR_ERR(inconn);
301 	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && IS_ERR(outconn))
302 		return PTR_ERR(outconn);
303 
304 	return link_ops(csdev)->enable(csdev, inconn, outconn);
305 }
306 
307 static void coresight_disable_link(struct coresight_device *csdev,
308 				   struct coresight_device *parent,
309 				   struct coresight_device *child,
310 				   struct coresight_device *source)
311 {
312 	struct coresight_connection *inconn, *outconn;
313 
314 	if (!parent || !child)
315 		return;
316 
317 	inconn = coresight_find_out_connection(parent, csdev, source);
318 	outconn = coresight_find_out_connection(csdev, child, source);
319 
320 	link_ops(csdev)->disable(csdev, inconn, outconn);
321 }
322 
323 static bool coresight_is_helper(struct coresight_device *csdev)
324 {
325 	return csdev->type == CORESIGHT_DEV_TYPE_HELPER;
326 }
327 
328 static int coresight_enable_helper(struct coresight_device *csdev,
329 				   enum cs_mode mode, void *data)
330 {
331 	return helper_ops(csdev)->enable(csdev, mode, data);
332 }
333 
334 static void coresight_disable_helper(struct coresight_device *csdev)
335 {
336 	helper_ops(csdev)->disable(csdev, NULL);
337 }
338 
339 static void coresight_disable_helpers(struct coresight_device *csdev)
340 {
341 	int i;
342 	struct coresight_device *helper;
343 
344 	for (i = 0; i < csdev->pdata->nr_outconns; ++i) {
345 		helper = csdev->pdata->out_conns[i]->dest_dev;
346 		if (helper && coresight_is_helper(helper))
347 			coresight_disable_helper(helper);
348 	}
349 }
350 
351 /*
352  * Helper function to call source_ops(csdev)->disable and also disable the
353  * helpers.
354  *
355  * There is an imbalance between coresight_enable_path() and
356  * coresight_disable_path(). Enabling also enables the source's helpers as part
357  * of the path, but disabling always skips the first item in the path (which is
358  * the source), so sources and their helpers don't get disabled as part of that
359  * function and we need the extra step here.
360  */
361 void coresight_disable_source(struct coresight_device *csdev, void *data)
362 {
363 	source_ops(csdev)->disable(csdev, data);
364 	coresight_disable_helpers(csdev);
365 }
366 EXPORT_SYMBOL_GPL(coresight_disable_source);
367 
368 /*
369  * coresight_disable_path_from : Disable components in the given path beyond
370  * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
371  * disabled.
372  */
373 static void coresight_disable_path_from(struct list_head *path,
374 					struct coresight_node *nd)
375 {
376 	u32 type;
377 	struct coresight_device *csdev, *parent, *child;
378 
379 	if (!nd)
380 		nd = list_first_entry(path, struct coresight_node, link);
381 
382 	list_for_each_entry_continue(nd, path, link) {
383 		csdev = nd->csdev;
384 		type = csdev->type;
385 
386 		/*
387 		 * ETF devices are tricky... They can be a link or a sink,
388 		 * depending on how they are configured.  If an ETF has been
389 		 * selected as a sink it will be configured as a sink, otherwise
390 		 * go ahead with the link configuration.
391 		 */
392 		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
393 			type = (csdev == coresight_get_sink(path)) ?
394 						CORESIGHT_DEV_TYPE_SINK :
395 						CORESIGHT_DEV_TYPE_LINK;
396 
397 		switch (type) {
398 		case CORESIGHT_DEV_TYPE_SINK:
399 			coresight_disable_sink(csdev);
400 			break;
401 		case CORESIGHT_DEV_TYPE_SOURCE:
402 			/*
403 			 * We skip the first node in the path assuming that it
404 			 * is the source. So we don't expect a source device in
405 			 * the middle of a path.
406 			 */
407 			WARN_ON(1);
408 			break;
409 		case CORESIGHT_DEV_TYPE_LINK:
410 			parent = list_prev_entry(nd, link)->csdev;
411 			child = list_next_entry(nd, link)->csdev;
412 			coresight_disable_link(csdev, parent, child,
413 					       coresight_get_source(path));
414 			break;
415 		default:
416 			break;
417 		}
418 
419 		/* Disable all helpers adjacent along the path last */
420 		coresight_disable_helpers(csdev);
421 	}
422 }
423 
424 void coresight_disable_path(struct list_head *path)
425 {
426 	coresight_disable_path_from(path, NULL);
427 }
428 EXPORT_SYMBOL_GPL(coresight_disable_path);
429 
430 static int coresight_enable_helpers(struct coresight_device *csdev,
431 				    enum cs_mode mode, void *data)
432 {
433 	int i, ret = 0;
434 	struct coresight_device *helper;
435 
436 	for (i = 0; i < csdev->pdata->nr_outconns; ++i) {
437 		helper = csdev->pdata->out_conns[i]->dest_dev;
438 		if (!helper || !coresight_is_helper(helper))
439 			continue;
440 
441 		ret = coresight_enable_helper(helper, mode, data);
442 		if (ret)
443 			return ret;
444 	}
445 
446 	return 0;
447 }
448 
449 int coresight_enable_path(struct list_head *path, enum cs_mode mode,
450 			  void *sink_data)
451 {
452 	int ret = 0;
453 	u32 type;
454 	struct coresight_node *nd;
455 	struct coresight_device *csdev, *parent, *child;
456 	struct coresight_device *source;
457 
458 	source = coresight_get_source(path);
459 	list_for_each_entry_reverse(nd, path, link) {
460 		csdev = nd->csdev;
461 		type = csdev->type;
462 
463 		/* Enable all helpers adjacent to the path first */
464 		ret = coresight_enable_helpers(csdev, mode, sink_data);
465 		if (ret)
466 			goto err;
467 		/*
468 		 * ETF devices are tricky... They can be a link or a sink,
469 		 * depending on how they are configured.  If an ETF has been
470 		 * selected as a sink it will be configured as a sink, otherwise
471 		 * go ahead with the link configuration.
472 		 */
473 		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
474 			type = (csdev == coresight_get_sink(path)) ?
475 						CORESIGHT_DEV_TYPE_SINK :
476 						CORESIGHT_DEV_TYPE_LINK;
477 
478 		switch (type) {
479 		case CORESIGHT_DEV_TYPE_SINK:
480 			ret = coresight_enable_sink(csdev, mode, sink_data);
481 			/*
482 			 * Sink is the first component turned on. If we
483 			 * failed to enable the sink, there are no components
484 			 * that need disabling. Disabling the path here
485 			 * would mean we could disrupt an existing session.
486 			 */
487 			if (ret)
488 				goto out;
489 			break;
490 		case CORESIGHT_DEV_TYPE_SOURCE:
491 			/* sources are enabled from either sysFS or Perf */
492 			break;
493 		case CORESIGHT_DEV_TYPE_LINK:
494 			parent = list_prev_entry(nd, link)->csdev;
495 			child = list_next_entry(nd, link)->csdev;
496 			ret = coresight_enable_link(csdev, parent, child, source);
497 			if (ret)
498 				goto err;
499 			break;
500 		default:
501 			goto err;
502 		}
503 	}
504 
505 out:
506 	return ret;
507 err:
508 	coresight_disable_path_from(path, nd);
509 	goto out;
510 }
511 
512 struct coresight_device *coresight_get_sink(struct list_head *path)
513 {
514 	struct coresight_device *csdev;
515 
516 	if (!path)
517 		return NULL;
518 
519 	csdev = list_last_entry(path, struct coresight_node, link)->csdev;
520 	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
521 	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
522 		return NULL;
523 
524 	return csdev;
525 }
526 
527 u32 coresight_get_sink_id(struct coresight_device *csdev)
528 {
529 	if (!csdev->ea)
530 		return 0;
531 
532 	/*
533 	 * See function etm_perf_add_symlink_sink() to know where
534 	 * this comes from.
535 	 */
536 	return (u32) (unsigned long) csdev->ea->var;
537 }
538 
539 static int coresight_sink_by_id(struct device *dev, const void *data)
540 {
541 	struct coresight_device *csdev = to_coresight_device(dev);
542 
543 	if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
544 	    csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
545 		if (coresight_get_sink_id(csdev) == *(u32 *)data)
546 			return 1;
547 	}
548 
549 	return 0;
550 }
551 
552 /**
553  * coresight_get_sink_by_id - returns the sink that matches the id
554  * @id: Id of the sink to match
555  *
556  * The name of a sink is unique, whether it is found on the AMBA bus or
557  * otherwise.  As such the hash of that name can easily be used to identify
558  * a sink.
559  */
560 struct coresight_device *coresight_get_sink_by_id(u32 id)
561 {
562 	struct device *dev = NULL;
563 
564 	dev = bus_find_device(&coresight_bustype, NULL, &id,
565 			      coresight_sink_by_id);
566 
567 	return dev ? to_coresight_device(dev) : NULL;
568 }
569 
570 /**
571  * coresight_get_ref- Helper function to increase reference count to module
572  * and device.
573  *
574  * @csdev: The coresight device to get a reference on.
575  *
576  * Return true in successful case and power up the device.
577  * Return false when failed to get reference of module.
578  */
579 static inline bool coresight_get_ref(struct coresight_device *csdev)
580 {
581 	struct device *dev = csdev->dev.parent;
582 
583 	/* Make sure the driver can't be removed */
584 	if (!try_module_get(dev->driver->owner))
585 		return false;
586 	/* Make sure the device can't go away */
587 	get_device(dev);
588 	pm_runtime_get_sync(dev);
589 	return true;
590 }
591 
592 /**
593  * coresight_put_ref- Helper function to decrease reference count to module
594  * and device. Power off the device.
595  *
596  * @csdev: The coresight device to decrement a reference from.
597  */
598 static inline void coresight_put_ref(struct coresight_device *csdev)
599 {
600 	struct device *dev = csdev->dev.parent;
601 
602 	pm_runtime_put(dev);
603 	put_device(dev);
604 	module_put(dev->driver->owner);
605 }
606 
607 /*
608  * coresight_grab_device - Power up this device and any of the helper
609  * devices connected to it for trace operation. Since the helper devices
610  * don't appear on the trace path, they should be handled along with the
611  * master device.
612  */
613 static int coresight_grab_device(struct coresight_device *csdev)
614 {
615 	int i;
616 
617 	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
618 		struct coresight_device *child;
619 
620 		child = csdev->pdata->out_conns[i]->dest_dev;
621 		if (child && coresight_is_helper(child))
622 			if (!coresight_get_ref(child))
623 				goto err;
624 	}
625 	if (coresight_get_ref(csdev))
626 		return 0;
627 err:
628 	for (i--; i >= 0; i--) {
629 		struct coresight_device *child;
630 
631 		child = csdev->pdata->out_conns[i]->dest_dev;
632 		if (child && coresight_is_helper(child))
633 			coresight_put_ref(child);
634 	}
635 	return -ENODEV;
636 }
637 
638 /*
639  * coresight_drop_device - Release this device and any of the helper
640  * devices connected to it.
641  */
642 static void coresight_drop_device(struct coresight_device *csdev)
643 {
644 	int i;
645 
646 	coresight_put_ref(csdev);
647 	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
648 		struct coresight_device *child;
649 
650 		child = csdev->pdata->out_conns[i]->dest_dev;
651 		if (child && coresight_is_helper(child))
652 			coresight_put_ref(child);
653 	}
654 }
655 
656 /**
657  * _coresight_build_path - recursively build a path from a @csdev to a sink.
658  * @csdev:	The device to start from.
659  * @source:	The trace source device of the path.
660  * @sink:	The final sink we want in this path.
661  * @path:	The list to add devices to.
662  *
663  * The tree of Coresight device is traversed until @sink is found.
664  * From there the sink is added to the list along with all the devices that led
665  * to that point - the end result is a list from source to sink. In that list
666  * the source is the first device and the sink the last one.
667  */
668 static int _coresight_build_path(struct coresight_device *csdev,
669 				 struct coresight_device *source,
670 				 struct coresight_device *sink,
671 				 struct list_head *path)
672 {
673 	int i, ret;
674 	bool found = false;
675 	struct coresight_node *node;
676 
677 	/* The sink has been found.  Enqueue the element */
678 	if (csdev == sink)
679 		goto out;
680 
681 	if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) &&
682 	    sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) {
683 		if (_coresight_build_path(sink, source, sink, path) == 0) {
684 			found = true;
685 			goto out;
686 		}
687 	}
688 
689 	/* Not a sink - recursively explore each port found on this element */
690 	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
691 		struct coresight_device *child_dev;
692 
693 		child_dev = csdev->pdata->out_conns[i]->dest_dev;
694 
695 		if (coresight_blocks_source(source, csdev->pdata->out_conns[i]))
696 			continue;
697 
698 		if (child_dev &&
699 		    _coresight_build_path(child_dev, source, sink, path) == 0) {
700 			found = true;
701 			break;
702 		}
703 	}
704 
705 	if (!found)
706 		return -ENODEV;
707 
708 out:
709 	/*
710 	 * A path from this element to a sink has been found.  The elements
711 	 * leading to the sink are already enqueued, all that is left to do
712 	 * is tell the PM runtime core we need this element and add a node
713 	 * for it.
714 	 */
715 	ret = coresight_grab_device(csdev);
716 	if (ret)
717 		return ret;
718 
719 	node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
720 	if (!node)
721 		return -ENOMEM;
722 
723 	node->csdev = csdev;
724 	list_add(&node->link, path);
725 
726 	return 0;
727 }
728 
729 struct list_head *coresight_build_path(struct coresight_device *source,
730 				       struct coresight_device *sink)
731 {
732 	struct list_head *path;
733 	int rc;
734 
735 	if (!sink)
736 		return ERR_PTR(-EINVAL);
737 
738 	path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
739 	if (!path)
740 		return ERR_PTR(-ENOMEM);
741 
742 	INIT_LIST_HEAD(path);
743 
744 	rc = _coresight_build_path(source, source, sink, path);
745 	if (rc) {
746 		kfree(path);
747 		return ERR_PTR(rc);
748 	}
749 
750 	return path;
751 }
752 
753 /**
754  * coresight_release_path - release a previously built path.
755  * @path:	the path to release.
756  *
757  * Go through all the elements of a path and 1) removed it from the list and
758  * 2) free the memory allocated for each node.
759  */
760 void coresight_release_path(struct list_head *path)
761 {
762 	struct coresight_device *csdev;
763 	struct coresight_node *nd, *next;
764 
765 	list_for_each_entry_safe(nd, next, path, link) {
766 		csdev = nd->csdev;
767 
768 		coresight_drop_device(csdev);
769 		list_del(&nd->link);
770 		kfree(nd);
771 	}
772 
773 	kfree(path);
774 }
775 
776 /* return true if the device is a suitable type for a default sink */
777 static inline bool coresight_is_def_sink_type(struct coresight_device *csdev)
778 {
779 	/* sink & correct subtype */
780 	if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
781 	     (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) &&
782 	    (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER))
783 		return true;
784 	return false;
785 }
786 
787 /**
788  * coresight_select_best_sink - return the best sink for use as default from
789  * the two provided.
790  *
791  * @sink:	current best sink.
792  * @depth:      search depth where current sink was found.
793  * @new_sink:	new sink for comparison with current sink.
794  * @new_depth:  search depth where new sink was found.
795  *
796  * Sinks prioritised according to coresight_dev_subtype_sink, with only
797  * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used.
798  *
799  * Where two sinks of equal priority are found, the sink closest to the
800  * source is used (smallest search depth).
801  *
802  * return @new_sink & update @depth if better than @sink, else return @sink.
803  */
804 static struct coresight_device *
805 coresight_select_best_sink(struct coresight_device *sink, int *depth,
806 			   struct coresight_device *new_sink, int new_depth)
807 {
808 	bool update = false;
809 
810 	if (!sink) {
811 		/* first found at this level */
812 		update = true;
813 	} else if (new_sink->subtype.sink_subtype >
814 		   sink->subtype.sink_subtype) {
815 		/* found better sink */
816 		update = true;
817 	} else if ((new_sink->subtype.sink_subtype ==
818 		    sink->subtype.sink_subtype) &&
819 		   (*depth > new_depth)) {
820 		/* found same but closer sink */
821 		update = true;
822 	}
823 
824 	if (update)
825 		*depth = new_depth;
826 	return update ? new_sink : sink;
827 }
828 
829 /**
830  * coresight_find_sink - recursive function to walk trace connections from
831  * source to find a suitable default sink.
832  *
833  * @csdev: source / current device to check.
834  * @depth: [in] search depth of calling dev, [out] depth of found sink.
835  *
836  * This will walk the connection path from a source (ETM) till a suitable
837  * sink is encountered and return that sink to the original caller.
838  *
839  * If current device is a plain sink return that & depth, otherwise recursively
840  * call child connections looking for a sink. Select best possible using
841  * coresight_select_best_sink.
842  *
843  * return best sink found, or NULL if not found at this node or child nodes.
844  */
845 static struct coresight_device *
846 coresight_find_sink(struct coresight_device *csdev, int *depth)
847 {
848 	int i, curr_depth = *depth + 1, found_depth = 0;
849 	struct coresight_device *found_sink = NULL;
850 
851 	if (coresight_is_def_sink_type(csdev)) {
852 		found_depth = curr_depth;
853 		found_sink = csdev;
854 		if (csdev->type == CORESIGHT_DEV_TYPE_SINK)
855 			goto return_def_sink;
856 		/* look past LINKSINK for something better */
857 	}
858 
859 	/*
860 	 * Not a sink we want - or possible child sink may be better.
861 	 * recursively explore each port found on this element.
862 	 */
863 	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
864 		struct coresight_device *child_dev, *sink = NULL;
865 		int child_depth = curr_depth;
866 
867 		child_dev = csdev->pdata->out_conns[i]->dest_dev;
868 		if (child_dev)
869 			sink = coresight_find_sink(child_dev, &child_depth);
870 
871 		if (sink)
872 			found_sink = coresight_select_best_sink(found_sink,
873 								&found_depth,
874 								sink,
875 								child_depth);
876 	}
877 
878 return_def_sink:
879 	/* return found sink and depth */
880 	if (found_sink)
881 		*depth = found_depth;
882 	return found_sink;
883 }
884 
885 /**
886  * coresight_find_default_sink: Find a sink suitable for use as a
887  * default sink.
888  *
889  * @csdev: starting source to find a connected sink.
890  *
891  * Walks connections graph looking for a suitable sink to enable for the
892  * supplied source. Uses CoreSight device subtypes and distance from source
893  * to select the best sink.
894  *
895  * If a sink is found, then the default sink for this device is set and
896  * will be automatically used in future.
897  *
898  * Used in cases where the CoreSight user (perf / sysfs) has not selected a
899  * sink.
900  */
901 struct coresight_device *
902 coresight_find_default_sink(struct coresight_device *csdev)
903 {
904 	int depth = 0;
905 
906 	/* look for a default sink if we have not found for this device */
907 	if (!csdev->def_sink) {
908 		if (coresight_is_percpu_source(csdev))
909 			csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev));
910 		if (!csdev->def_sink)
911 			csdev->def_sink = coresight_find_sink(csdev, &depth);
912 	}
913 	return csdev->def_sink;
914 }
915 
916 static int coresight_remove_sink_ref(struct device *dev, void *data)
917 {
918 	struct coresight_device *sink = data;
919 	struct coresight_device *source = to_coresight_device(dev);
920 
921 	if (source->def_sink == sink)
922 		source->def_sink = NULL;
923 	return 0;
924 }
925 
926 /**
927  * coresight_clear_default_sink: Remove all default sink references to the
928  * supplied sink.
929  *
930  * If supplied device is a sink, then check all the bus devices and clear
931  * out all the references to this sink from the coresight_device def_sink
932  * parameter.
933  *
934  * @csdev: coresight sink - remove references to this from all sources.
935  */
936 static void coresight_clear_default_sink(struct coresight_device *csdev)
937 {
938 	if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) ||
939 	    (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) {
940 		bus_for_each_dev(&coresight_bustype, NULL, csdev,
941 				 coresight_remove_sink_ref);
942 	}
943 }
944 
945 static void coresight_device_release(struct device *dev)
946 {
947 	struct coresight_device *csdev = to_coresight_device(dev);
948 
949 	fwnode_handle_put(csdev->dev.fwnode);
950 	free_percpu(csdev->perf_sink_id_map.cpu_map);
951 	kfree(csdev);
952 }
953 
954 static int coresight_orphan_match(struct device *dev, void *data)
955 {
956 	int i, ret = 0;
957 	bool still_orphan = false;
958 	struct coresight_device *dst_csdev = data;
959 	struct coresight_device *src_csdev = to_coresight_device(dev);
960 	struct coresight_connection *conn;
961 	bool fixup_self = (src_csdev == dst_csdev);
962 
963 	/* Move on to another component if no connection is orphan */
964 	if (!src_csdev->orphan)
965 		return 0;
966 	/*
967 	 * Circle through all the connections of that component.  If we find
968 	 * an orphan connection whose name matches @dst_csdev, link it.
969 	 */
970 	for (i = 0; i < src_csdev->pdata->nr_outconns; i++) {
971 		conn = src_csdev->pdata->out_conns[i];
972 
973 		/* Fix filter source device before skip the port */
974 		if (conn->filter_src_fwnode && !conn->filter_src_dev) {
975 			if (dst_csdev &&
976 			    (conn->filter_src_fwnode == dst_csdev->dev.fwnode) &&
977 			    !WARN_ON_ONCE(!coresight_is_device_source(dst_csdev)))
978 				conn->filter_src_dev = dst_csdev;
979 			else
980 				still_orphan = true;
981 		}
982 
983 		/* Skip the port if it's already connected. */
984 		if (conn->dest_dev)
985 			continue;
986 
987 		/*
988 		 * If we are at the "new" device, which triggered this search,
989 		 * we must find the remote device from the fwnode in the
990 		 * connection.
991 		 */
992 		if (fixup_self)
993 			dst_csdev = coresight_find_csdev_by_fwnode(
994 				conn->dest_fwnode);
995 
996 		/* Does it match this newly added device? */
997 		if (dst_csdev && conn->dest_fwnode == dst_csdev->dev.fwnode) {
998 			ret = coresight_make_links(src_csdev, conn, dst_csdev);
999 			if (ret)
1000 				return ret;
1001 
1002 			/*
1003 			 * Install the device connection. This also indicates that
1004 			 * the links are operational on both ends.
1005 			 */
1006 			conn->dest_dev = dst_csdev;
1007 			conn->src_dev = src_csdev;
1008 
1009 			ret = coresight_add_in_conn(conn);
1010 			if (ret)
1011 				return ret;
1012 		} else {
1013 			/* This component still has an orphan */
1014 			still_orphan = true;
1015 		}
1016 	}
1017 
1018 	src_csdev->orphan = still_orphan;
1019 
1020 	/*
1021 	 * Returning '0' in case we didn't encounter any error,
1022 	 * ensures that all known component on the bus will be checked.
1023 	 */
1024 	return 0;
1025 }
1026 
1027 static int coresight_fixup_orphan_conns(struct coresight_device *csdev)
1028 {
1029 	return bus_for_each_dev(&coresight_bustype, NULL,
1030 			 csdev, coresight_orphan_match);
1031 }
1032 
1033 static int coresight_clear_filter_source(struct device *dev, void *data)
1034 {
1035 	int i;
1036 	struct coresight_device *source = data;
1037 	struct coresight_device *csdev = to_coresight_device(dev);
1038 
1039 	for (i = 0; i < csdev->pdata->nr_outconns; ++i) {
1040 		if (csdev->pdata->out_conns[i]->filter_src_dev == source)
1041 			csdev->pdata->out_conns[i]->filter_src_dev = NULL;
1042 	}
1043 	return 0;
1044 }
1045 
1046 /* coresight_remove_conns - Remove other device's references to this device */
1047 static void coresight_remove_conns(struct coresight_device *csdev)
1048 {
1049 	int i, j;
1050 	struct coresight_connection *conn;
1051 
1052 	if (coresight_is_device_source(csdev))
1053 		bus_for_each_dev(&coresight_bustype, NULL, csdev,
1054 				 coresight_clear_filter_source);
1055 
1056 	/*
1057 	 * Remove the input connection references from the destination device
1058 	 * for each output connection.
1059 	 */
1060 	for (i = 0; i < csdev->pdata->nr_outconns; i++) {
1061 		conn = csdev->pdata->out_conns[i];
1062 		if (conn->filter_src_fwnode) {
1063 			conn->filter_src_dev = NULL;
1064 			fwnode_handle_put(conn->filter_src_fwnode);
1065 		}
1066 
1067 		if (!conn->dest_dev)
1068 			continue;
1069 
1070 		for (j = 0; j < conn->dest_dev->pdata->nr_inconns; ++j)
1071 			if (conn->dest_dev->pdata->in_conns[j] == conn) {
1072 				conn->dest_dev->pdata->in_conns[j] = NULL;
1073 				break;
1074 			}
1075 	}
1076 
1077 	/*
1078 	 * For all input connections, remove references to this device.
1079 	 * Connection objects are shared so modifying this device's input
1080 	 * connections affects the other device's output connection.
1081 	 */
1082 	for (i = 0; i < csdev->pdata->nr_inconns; ++i) {
1083 		conn = csdev->pdata->in_conns[i];
1084 		/* Input conns array is sparse */
1085 		if (!conn)
1086 			continue;
1087 
1088 		conn->src_dev->orphan = true;
1089 		coresight_remove_links(conn->src_dev, conn);
1090 		conn->dest_dev = NULL;
1091 	}
1092 }
1093 
1094 /**
1095  * coresight_timeout - loop until a bit has changed to a specific register
1096  *			state.
1097  * @csa: coresight device access for the device
1098  * @offset: Offset of the register from the base of the device.
1099  * @position: the position of the bit of interest.
1100  * @value: the value the bit should have.
1101  *
1102  * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1103  * TIMEOUT_US has elapsed, which ever happens first.
1104  */
1105 int coresight_timeout(struct csdev_access *csa, u32 offset,
1106 		      int position, int value)
1107 {
1108 	int i;
1109 	u32 val;
1110 
1111 	for (i = TIMEOUT_US; i > 0; i--) {
1112 		val = csdev_access_read32(csa, offset);
1113 		/* waiting on the bit to go from 0 to 1 */
1114 		if (value) {
1115 			if (val & BIT(position))
1116 				return 0;
1117 		/* waiting on the bit to go from 1 to 0 */
1118 		} else {
1119 			if (!(val & BIT(position)))
1120 				return 0;
1121 		}
1122 
1123 		/*
1124 		 * Delay is arbitrary - the specification doesn't say how long
1125 		 * we are expected to wait.  Extra check required to make sure
1126 		 * we don't wait needlessly on the last iteration.
1127 		 */
1128 		if (i - 1)
1129 			udelay(1);
1130 	}
1131 
1132 	return -EAGAIN;
1133 }
1134 EXPORT_SYMBOL_GPL(coresight_timeout);
1135 
1136 u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset)
1137 {
1138 	return csdev_access_relaxed_read32(&csdev->access, offset);
1139 }
1140 
1141 u32 coresight_read32(struct coresight_device *csdev, u32 offset)
1142 {
1143 	return csdev_access_read32(&csdev->access, offset);
1144 }
1145 
1146 void coresight_relaxed_write32(struct coresight_device *csdev,
1147 			       u32 val, u32 offset)
1148 {
1149 	csdev_access_relaxed_write32(&csdev->access, val, offset);
1150 }
1151 
1152 void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset)
1153 {
1154 	csdev_access_write32(&csdev->access, val, offset);
1155 }
1156 
1157 u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset)
1158 {
1159 	return csdev_access_relaxed_read64(&csdev->access, offset);
1160 }
1161 
1162 u64 coresight_read64(struct coresight_device *csdev, u32 offset)
1163 {
1164 	return csdev_access_read64(&csdev->access, offset);
1165 }
1166 
1167 void coresight_relaxed_write64(struct coresight_device *csdev,
1168 			       u64 val, u32 offset)
1169 {
1170 	csdev_access_relaxed_write64(&csdev->access, val, offset);
1171 }
1172 
1173 void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset)
1174 {
1175 	csdev_access_write64(&csdev->access, val, offset);
1176 }
1177 
1178 /*
1179  * coresight_release_platform_data: Release references to the devices connected
1180  * to the output port of this device.
1181  */
1182 void coresight_release_platform_data(struct coresight_device *csdev,
1183 				     struct device *dev,
1184 				     struct coresight_platform_data *pdata)
1185 {
1186 	int i;
1187 	struct coresight_connection **conns = pdata->out_conns;
1188 
1189 	for (i = 0; i < pdata->nr_outconns; i++) {
1190 		/* If we have made the links, remove them now */
1191 		if (csdev && conns[i]->dest_dev)
1192 			coresight_remove_links(csdev, conns[i]);
1193 		/*
1194 		 * Drop the refcount and clear the handle as this device
1195 		 * is going away
1196 		 */
1197 		fwnode_handle_put(conns[i]->dest_fwnode);
1198 		conns[i]->dest_fwnode = NULL;
1199 		devm_kfree(dev, conns[i]);
1200 	}
1201 	devm_kfree(dev, pdata->out_conns);
1202 	devm_kfree(dev, pdata->in_conns);
1203 	devm_kfree(dev, pdata);
1204 	if (csdev)
1205 		coresight_remove_conns_sysfs_group(csdev);
1206 }
1207 
1208 struct coresight_device *coresight_register(struct coresight_desc *desc)
1209 {
1210 	int ret;
1211 	struct coresight_device *csdev;
1212 	bool registered = false;
1213 
1214 	csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1215 	if (!csdev) {
1216 		ret = -ENOMEM;
1217 		goto err_out;
1218 	}
1219 
1220 	csdev->pdata = desc->pdata;
1221 
1222 	csdev->type = desc->type;
1223 	csdev->subtype = desc->subtype;
1224 	csdev->ops = desc->ops;
1225 	csdev->access = desc->access;
1226 	csdev->orphan = true;
1227 
1228 	csdev->dev.type = &coresight_dev_type[desc->type];
1229 	csdev->dev.groups = desc->groups;
1230 	csdev->dev.parent = desc->dev;
1231 	csdev->dev.release = coresight_device_release;
1232 	csdev->dev.bus = &coresight_bustype;
1233 	/*
1234 	 * Hold the reference to our parent device. This will be
1235 	 * dropped only in coresight_device_release().
1236 	 */
1237 	csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1238 	dev_set_name(&csdev->dev, "%s", desc->name);
1239 
1240 	if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1241 	    csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1242 		spin_lock_init(&csdev->perf_sink_id_map.lock);
1243 		csdev->perf_sink_id_map.cpu_map = alloc_percpu(atomic_t);
1244 		if (!csdev->perf_sink_id_map.cpu_map) {
1245 			kfree(csdev);
1246 			ret = -ENOMEM;
1247 			goto err_out;
1248 		}
1249 	}
1250 	/*
1251 	 * Make sure the device registration and the connection fixup
1252 	 * are synchronised, so that we don't see uninitialised devices
1253 	 * on the coresight bus while trying to resolve the connections.
1254 	 */
1255 	mutex_lock(&coresight_mutex);
1256 
1257 	ret = device_register(&csdev->dev);
1258 	if (ret) {
1259 		put_device(&csdev->dev);
1260 		/*
1261 		 * All resources are free'd explicitly via
1262 		 * coresight_device_release(), triggered from put_device().
1263 		 */
1264 		goto out_unlock;
1265 	}
1266 
1267 	if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1268 	    csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1269 		ret = etm_perf_add_symlink_sink(csdev);
1270 
1271 		if (ret) {
1272 			device_unregister(&csdev->dev);
1273 			/*
1274 			 * As with the above, all resources are free'd
1275 			 * explicitly via coresight_device_release() triggered
1276 			 * from put_device(), which is in turn called from
1277 			 * function device_unregister().
1278 			 */
1279 			goto out_unlock;
1280 		}
1281 	}
1282 	/* Device is now registered */
1283 	registered = true;
1284 
1285 	ret = coresight_create_conns_sysfs_group(csdev);
1286 	if (!ret)
1287 		ret = coresight_fixup_orphan_conns(csdev);
1288 
1289 out_unlock:
1290 	mutex_unlock(&coresight_mutex);
1291 	/* Success */
1292 	if (!ret) {
1293 		if (cti_assoc_ops && cti_assoc_ops->add)
1294 			cti_assoc_ops->add(csdev);
1295 		return csdev;
1296 	}
1297 
1298 	/* Unregister the device if needed */
1299 	if (registered) {
1300 		coresight_unregister(csdev);
1301 		return ERR_PTR(ret);
1302 	}
1303 
1304 err_out:
1305 	/* Cleanup the connection information */
1306 	coresight_release_platform_data(NULL, desc->dev, desc->pdata);
1307 	return ERR_PTR(ret);
1308 }
1309 EXPORT_SYMBOL_GPL(coresight_register);
1310 
1311 void coresight_unregister(struct coresight_device *csdev)
1312 {
1313 	etm_perf_del_symlink_sink(csdev);
1314 	/* Remove references of that device in the topology */
1315 	if (cti_assoc_ops && cti_assoc_ops->remove)
1316 		cti_assoc_ops->remove(csdev);
1317 	coresight_remove_conns(csdev);
1318 	coresight_clear_default_sink(csdev);
1319 	coresight_release_platform_data(csdev, csdev->dev.parent, csdev->pdata);
1320 	device_unregister(&csdev->dev);
1321 }
1322 EXPORT_SYMBOL_GPL(coresight_unregister);
1323 
1324 
1325 /*
1326  * coresight_search_device_idx - Search the fwnode handle of a device
1327  * in the given dev_idx list. Must be called with the coresight_mutex held.
1328  *
1329  * Returns the index of the entry, when found. Otherwise, -ENOENT.
1330  */
1331 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1332 					      struct fwnode_handle *fwnode)
1333 {
1334 	int i;
1335 
1336 	for (i = 0; i < dict->nr_idx; i++)
1337 		if (dict->fwnode_list[i] == fwnode)
1338 			return i;
1339 	return -ENOENT;
1340 }
1341 
1342 static bool coresight_compare_type(enum coresight_dev_type type_a,
1343 				   union coresight_dev_subtype subtype_a,
1344 				   enum coresight_dev_type type_b,
1345 				   union coresight_dev_subtype subtype_b)
1346 {
1347 	if (type_a != type_b)
1348 		return false;
1349 
1350 	switch (type_a) {
1351 	case CORESIGHT_DEV_TYPE_SINK:
1352 		return subtype_a.sink_subtype == subtype_b.sink_subtype;
1353 	case CORESIGHT_DEV_TYPE_LINK:
1354 		return subtype_a.link_subtype == subtype_b.link_subtype;
1355 	case CORESIGHT_DEV_TYPE_LINKSINK:
1356 		return subtype_a.link_subtype == subtype_b.link_subtype &&
1357 		       subtype_a.sink_subtype == subtype_b.sink_subtype;
1358 	case CORESIGHT_DEV_TYPE_SOURCE:
1359 		return subtype_a.source_subtype == subtype_b.source_subtype;
1360 	case CORESIGHT_DEV_TYPE_HELPER:
1361 		return subtype_a.helper_subtype == subtype_b.helper_subtype;
1362 	default:
1363 		return false;
1364 	}
1365 }
1366 
1367 struct coresight_device *
1368 coresight_find_input_type(struct coresight_platform_data *pdata,
1369 			  enum coresight_dev_type type,
1370 			  union coresight_dev_subtype subtype)
1371 {
1372 	int i;
1373 	struct coresight_connection *conn;
1374 
1375 	for (i = 0; i < pdata->nr_inconns; ++i) {
1376 		conn = pdata->in_conns[i];
1377 		if (conn &&
1378 		    coresight_compare_type(type, subtype, conn->src_dev->type,
1379 					   conn->src_dev->subtype))
1380 			return conn->src_dev;
1381 	}
1382 	return NULL;
1383 }
1384 EXPORT_SYMBOL_GPL(coresight_find_input_type);
1385 
1386 struct coresight_device *
1387 coresight_find_output_type(struct coresight_platform_data *pdata,
1388 			   enum coresight_dev_type type,
1389 			   union coresight_dev_subtype subtype)
1390 {
1391 	int i;
1392 	struct coresight_connection *conn;
1393 
1394 	for (i = 0; i < pdata->nr_outconns; ++i) {
1395 		conn = pdata->out_conns[i];
1396 		if (conn->dest_dev &&
1397 		    coresight_compare_type(type, subtype, conn->dest_dev->type,
1398 					   conn->dest_dev->subtype))
1399 			return conn->dest_dev;
1400 	}
1401 	return NULL;
1402 }
1403 EXPORT_SYMBOL_GPL(coresight_find_output_type);
1404 
1405 bool coresight_loses_context_with_cpu(struct device *dev)
1406 {
1407 	return fwnode_property_present(dev_fwnode(dev),
1408 				       "arm,coresight-loses-context-with-cpu");
1409 }
1410 EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu);
1411 
1412 /*
1413  * coresight_alloc_device_name - Get an index for a given device in the
1414  * device index list specific to a driver. An index is allocated for a
1415  * device and is tracked with the fwnode_handle to prevent allocating
1416  * duplicate indices for the same device (e.g, if we defer probing of
1417  * a device due to dependencies), in case the index is requested again.
1418  */
1419 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1420 				  struct device *dev)
1421 {
1422 	int idx;
1423 	char *name = NULL;
1424 	struct fwnode_handle **list;
1425 
1426 	mutex_lock(&coresight_mutex);
1427 
1428 	idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1429 	if (idx < 0) {
1430 		/* Make space for the new entry */
1431 		idx = dict->nr_idx;
1432 		list = krealloc_array(dict->fwnode_list,
1433 				      idx + 1, sizeof(*dict->fwnode_list),
1434 				      GFP_KERNEL);
1435 		if (ZERO_OR_NULL_PTR(list)) {
1436 			idx = -ENOMEM;
1437 			goto done;
1438 		}
1439 
1440 		list[idx] = dev_fwnode(dev);
1441 		dict->fwnode_list = list;
1442 		dict->nr_idx = idx + 1;
1443 	}
1444 
1445 	name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1446 done:
1447 	mutex_unlock(&coresight_mutex);
1448 	return name;
1449 }
1450 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1451 
1452 const struct bus_type coresight_bustype = {
1453 	.name	= "coresight",
1454 };
1455 
1456 static int __init coresight_init(void)
1457 {
1458 	int ret;
1459 
1460 	ret = bus_register(&coresight_bustype);
1461 	if (ret)
1462 		return ret;
1463 
1464 	ret = etm_perf_init();
1465 	if (ret)
1466 		goto exit_bus_unregister;
1467 
1468 	/* initialise the coresight syscfg API */
1469 	ret = cscfg_init();
1470 	if (!ret)
1471 		return 0;
1472 
1473 	etm_perf_exit();
1474 exit_bus_unregister:
1475 	bus_unregister(&coresight_bustype);
1476 	return ret;
1477 }
1478 
1479 static void __exit coresight_exit(void)
1480 {
1481 	cscfg_exit();
1482 	etm_perf_exit();
1483 	bus_unregister(&coresight_bustype);
1484 }
1485 
1486 module_init(coresight_init);
1487 module_exit(coresight_exit);
1488 
1489 int coresight_init_driver(const char *drv, struct amba_driver *amba_drv,
1490 			  struct platform_driver *pdev_drv)
1491 {
1492 	int ret;
1493 
1494 	ret = amba_driver_register(amba_drv);
1495 	if (ret) {
1496 		pr_err("%s: error registering AMBA driver\n", drv);
1497 		return ret;
1498 	}
1499 
1500 	ret = platform_driver_register(pdev_drv);
1501 	if (!ret)
1502 		return 0;
1503 
1504 	pr_err("%s: error registering platform driver\n", drv);
1505 	amba_driver_unregister(amba_drv);
1506 	return ret;
1507 }
1508 EXPORT_SYMBOL_GPL(coresight_init_driver);
1509 
1510 void coresight_remove_driver(struct amba_driver *amba_drv,
1511 			     struct platform_driver *pdev_drv)
1512 {
1513 	amba_driver_unregister(amba_drv);
1514 	platform_driver_unregister(pdev_drv);
1515 }
1516 EXPORT_SYMBOL_GPL(coresight_remove_driver);
1517 
1518 MODULE_LICENSE("GPL v2");
1519 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1520 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1521 MODULE_DESCRIPTION("Arm CoreSight tracer driver");
1522