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