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