• 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/kernel.h>
7 #include <linux/init.h>
8 #include <linux/types.h>
9 #include <linux/device.h>
10 #include <linux/io.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/stringhash.h>
15 #include <linux/mutex.h>
16 #include <linux/clk.h>
17 #include <linux/coresight.h>
18 #include <linux/of_platform.h>
19 #include <linux/delay.h>
20 #include <linux/pm_runtime.h>
21 
22 #include "coresight-etm-perf.h"
23 #include "coresight-priv.h"
24 
25 static DEFINE_MUTEX(coresight_mutex);
26 
27 /**
28  * struct coresight_node - elements of a path, from source to sink
29  * @csdev:	Address of an element.
30  * @link:	hook to the list.
31  */
32 struct coresight_node {
33 	struct coresight_device *csdev;
34 	struct list_head link;
35 };
36 
37 /*
38  * When operating Coresight drivers from the sysFS interface, only a single
39  * path can exist from a tracer (associated to a CPU) to a sink.
40  */
41 static DEFINE_PER_CPU(struct list_head *, tracer_path);
42 
43 /*
44  * As of this writing only a single STM can be found in CS topologies.  Since
45  * there is no way to know if we'll ever see more and what kind of
46  * configuration they will enact, for the time being only define a single path
47  * for STM.
48  */
49 static struct list_head *stm_path;
50 
51 /*
52  * When losing synchronisation a new barrier packet needs to be inserted at the
53  * beginning of the data collected in a buffer.  That way the decoder knows that
54  * it needs to look for another sync sequence.
55  */
56 const u32 barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
57 
coresight_id_match(struct device * dev,void * data)58 static int coresight_id_match(struct device *dev, void *data)
59 {
60 	int trace_id, i_trace_id;
61 	struct coresight_device *csdev, *i_csdev;
62 
63 	csdev = data;
64 	i_csdev = to_coresight_device(dev);
65 
66 	/*
67 	 * No need to care about oneself and components that are not
68 	 * sources or not enabled
69 	 */
70 	if (i_csdev == csdev || !i_csdev->enable ||
71 	    i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
72 		return 0;
73 
74 	/* Get the source ID for both compoment */
75 	trace_id = source_ops(csdev)->trace_id(csdev);
76 	i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
77 
78 	/* All you need is one */
79 	if (trace_id == i_trace_id)
80 		return 1;
81 
82 	return 0;
83 }
84 
coresight_source_is_unique(struct coresight_device * csdev)85 static int coresight_source_is_unique(struct coresight_device *csdev)
86 {
87 	int trace_id = source_ops(csdev)->trace_id(csdev);
88 
89 	/* this shouldn't happen */
90 	if (trace_id < 0)
91 		return 0;
92 
93 	return !bus_for_each_dev(&coresight_bustype, NULL,
94 				 csdev, coresight_id_match);
95 }
96 
coresight_find_link_inport(struct coresight_device * csdev,struct coresight_device * parent)97 static int coresight_find_link_inport(struct coresight_device *csdev,
98 				      struct coresight_device *parent)
99 {
100 	int i;
101 	struct coresight_connection *conn;
102 
103 	for (i = 0; i < parent->pdata->nr_outport; i++) {
104 		conn = &parent->pdata->conns[i];
105 		if (conn->child_dev == csdev)
106 			return conn->child_port;
107 	}
108 
109 	dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
110 		dev_name(&parent->dev), dev_name(&csdev->dev));
111 
112 	return -ENODEV;
113 }
114 
coresight_find_link_outport(struct coresight_device * csdev,struct coresight_device * child)115 static int coresight_find_link_outport(struct coresight_device *csdev,
116 				       struct coresight_device *child)
117 {
118 	int i;
119 	struct coresight_connection *conn;
120 
121 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
122 		conn = &csdev->pdata->conns[i];
123 		if (conn->child_dev == child)
124 			return conn->outport;
125 	}
126 
127 	dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
128 		dev_name(&csdev->dev), dev_name(&child->dev));
129 
130 	return -ENODEV;
131 }
132 
coresight_read_claim_tags(void __iomem * base)133 static inline u32 coresight_read_claim_tags(void __iomem *base)
134 {
135 	return readl_relaxed(base + CORESIGHT_CLAIMCLR);
136 }
137 
coresight_is_claimed_self_hosted(void __iomem * base)138 static inline bool coresight_is_claimed_self_hosted(void __iomem *base)
139 {
140 	return coresight_read_claim_tags(base) == CORESIGHT_CLAIM_SELF_HOSTED;
141 }
142 
coresight_is_claimed_any(void __iomem * base)143 static inline bool coresight_is_claimed_any(void __iomem *base)
144 {
145 	return coresight_read_claim_tags(base) != 0;
146 }
147 
coresight_set_claim_tags(void __iomem * base)148 static inline void coresight_set_claim_tags(void __iomem *base)
149 {
150 	writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMSET);
151 	isb();
152 }
153 
coresight_clear_claim_tags(void __iomem * base)154 static inline void coresight_clear_claim_tags(void __iomem *base)
155 {
156 	writel_relaxed(CORESIGHT_CLAIM_SELF_HOSTED, base + CORESIGHT_CLAIMCLR);
157 	isb();
158 }
159 
160 /*
161  * coresight_claim_device_unlocked : Claim the device for self-hosted usage
162  * to prevent an external tool from touching this device. As per PSCI
163  * standards, section "Preserving the execution context" => "Debug and Trace
164  * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and
165  * DBGCLAIM[0] is reserved for external tools.
166  *
167  * Called with CS_UNLOCKed for the component.
168  * Returns : 0 on success
169  */
coresight_claim_device_unlocked(void __iomem * base)170 int coresight_claim_device_unlocked(void __iomem *base)
171 {
172 	if (coresight_is_claimed_any(base))
173 		return -EBUSY;
174 
175 	coresight_set_claim_tags(base);
176 	if (coresight_is_claimed_self_hosted(base))
177 		return 0;
178 	/* There was a race setting the tags, clean up and fail */
179 	coresight_clear_claim_tags(base);
180 	return -EBUSY;
181 }
182 
coresight_claim_device(void __iomem * base)183 int coresight_claim_device(void __iomem *base)
184 {
185 	int rc;
186 
187 	CS_UNLOCK(base);
188 	rc = coresight_claim_device_unlocked(base);
189 	CS_LOCK(base);
190 
191 	return rc;
192 }
193 
194 /*
195  * coresight_disclaim_device_unlocked : Clear the claim tags for the device.
196  * Called with CS_UNLOCKed for the component.
197  */
coresight_disclaim_device_unlocked(void __iomem * base)198 void coresight_disclaim_device_unlocked(void __iomem *base)
199 {
200 
201 	if (coresight_is_claimed_self_hosted(base))
202 		coresight_clear_claim_tags(base);
203 	else
204 		/*
205 		 * The external agent may have not honoured our claim
206 		 * and has manipulated it. Or something else has seriously
207 		 * gone wrong in our driver.
208 		 */
209 		WARN_ON_ONCE(1);
210 }
211 
coresight_disclaim_device(void __iomem * base)212 void coresight_disclaim_device(void __iomem *base)
213 {
214 	CS_UNLOCK(base);
215 	coresight_disclaim_device_unlocked(base);
216 	CS_LOCK(base);
217 }
218 
coresight_enable_sink(struct coresight_device * csdev,u32 mode,void * data)219 static int coresight_enable_sink(struct coresight_device *csdev,
220 				 u32 mode, void *data)
221 {
222 	int ret;
223 
224 	/*
225 	 * We need to make sure the "new" session is compatible with the
226 	 * existing "mode" of operation.
227 	 */
228 	if (!sink_ops(csdev)->enable)
229 		return -EINVAL;
230 
231 	ret = sink_ops(csdev)->enable(csdev, mode, data);
232 	if (ret)
233 		return ret;
234 	csdev->enable = true;
235 
236 	return 0;
237 }
238 
coresight_disable_sink(struct coresight_device * csdev)239 static void coresight_disable_sink(struct coresight_device *csdev)
240 {
241 	int ret;
242 
243 	if (!sink_ops(csdev)->disable)
244 		return;
245 
246 	ret = sink_ops(csdev)->disable(csdev);
247 	if (ret)
248 		return;
249 	csdev->enable = false;
250 }
251 
coresight_enable_link(struct coresight_device * csdev,struct coresight_device * parent,struct coresight_device * child)252 static int coresight_enable_link(struct coresight_device *csdev,
253 				 struct coresight_device *parent,
254 				 struct coresight_device *child)
255 {
256 	int ret = 0;
257 	int link_subtype;
258 	int inport, outport;
259 
260 	if (!parent || !child)
261 		return -EINVAL;
262 
263 	inport = coresight_find_link_inport(csdev, parent);
264 	outport = coresight_find_link_outport(csdev, child);
265 	link_subtype = csdev->subtype.link_subtype;
266 
267 	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && inport < 0)
268 		return inport;
269 	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && outport < 0)
270 		return outport;
271 
272 	if (link_ops(csdev)->enable)
273 		ret = link_ops(csdev)->enable(csdev, inport, outport);
274 	if (!ret)
275 		csdev->enable = true;
276 
277 	return ret;
278 }
279 
coresight_disable_link(struct coresight_device * csdev,struct coresight_device * parent,struct coresight_device * child)280 static void coresight_disable_link(struct coresight_device *csdev,
281 				   struct coresight_device *parent,
282 				   struct coresight_device *child)
283 {
284 	int i, nr_conns;
285 	int link_subtype;
286 	int inport, outport;
287 
288 	if (!parent || !child)
289 		return;
290 
291 	inport = coresight_find_link_inport(csdev, parent);
292 	outport = coresight_find_link_outport(csdev, child);
293 	link_subtype = csdev->subtype.link_subtype;
294 
295 	if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
296 		nr_conns = csdev->pdata->nr_inport;
297 	} else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
298 		nr_conns = csdev->pdata->nr_outport;
299 	} else {
300 		nr_conns = 1;
301 	}
302 
303 	if (link_ops(csdev)->disable)
304 		link_ops(csdev)->disable(csdev, inport, outport);
305 
306 	for (i = 0; i < nr_conns; i++)
307 		if (atomic_read(&csdev->refcnt[i]) != 0)
308 			return;
309 
310 	csdev->enable = false;
311 }
312 
coresight_enable_source(struct coresight_device * csdev,u32 mode)313 static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
314 {
315 	int ret;
316 
317 	if (!coresight_source_is_unique(csdev)) {
318 		dev_warn(&csdev->dev, "traceID %d not unique\n",
319 			 source_ops(csdev)->trace_id(csdev));
320 		return -EINVAL;
321 	}
322 
323 	if (!csdev->enable) {
324 		if (source_ops(csdev)->enable) {
325 			ret = source_ops(csdev)->enable(csdev, NULL, mode);
326 			if (ret)
327 				return ret;
328 		}
329 		csdev->enable = true;
330 	}
331 
332 	atomic_inc(csdev->refcnt);
333 
334 	return 0;
335 }
336 
337 /**
338  *  coresight_disable_source - Drop the reference count by 1 and disable
339  *  the device if there are no users left.
340  *
341  *  @csdev - The coresight device to disable
342  *
343  *  Returns true if the device has been disabled.
344  */
coresight_disable_source(struct coresight_device * csdev)345 static bool coresight_disable_source(struct coresight_device *csdev)
346 {
347 	if (atomic_dec_return(csdev->refcnt) == 0) {
348 		if (source_ops(csdev)->disable)
349 			source_ops(csdev)->disable(csdev, NULL);
350 		csdev->enable = false;
351 	}
352 	return !csdev->enable;
353 }
354 
355 /*
356  * coresight_disable_path_from : Disable components in the given path beyond
357  * @nd in the list. If @nd is NULL, all the components, except the SOURCE are
358  * disabled.
359  */
coresight_disable_path_from(struct list_head * path,struct coresight_node * nd)360 static void coresight_disable_path_from(struct list_head *path,
361 					struct coresight_node *nd)
362 {
363 	u32 type;
364 	struct coresight_device *csdev, *parent, *child;
365 
366 	if (!nd)
367 		nd = list_first_entry(path, struct coresight_node, link);
368 
369 	list_for_each_entry_continue(nd, path, link) {
370 		csdev = nd->csdev;
371 		type = csdev->type;
372 
373 		/*
374 		 * ETF devices are tricky... They can be a link or a sink,
375 		 * depending on how they are configured.  If an ETF has been
376 		 * "activated" it will be configured as a sink, otherwise
377 		 * go ahead with the link configuration.
378 		 */
379 		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
380 			type = (csdev == coresight_get_sink(path)) ?
381 						CORESIGHT_DEV_TYPE_SINK :
382 						CORESIGHT_DEV_TYPE_LINK;
383 
384 		switch (type) {
385 		case CORESIGHT_DEV_TYPE_SINK:
386 			coresight_disable_sink(csdev);
387 			break;
388 		case CORESIGHT_DEV_TYPE_SOURCE:
389 			/*
390 			 * We skip the first node in the path assuming that it
391 			 * is the source. So we don't expect a source device in
392 			 * the middle of a path.
393 			 */
394 			WARN_ON(1);
395 			break;
396 		case CORESIGHT_DEV_TYPE_LINK:
397 			parent = list_prev_entry(nd, link)->csdev;
398 			child = list_next_entry(nd, link)->csdev;
399 			coresight_disable_link(csdev, parent, child);
400 			break;
401 		default:
402 			break;
403 		}
404 	}
405 }
406 
coresight_disable_path(struct list_head * path)407 void coresight_disable_path(struct list_head *path)
408 {
409 	coresight_disable_path_from(path, NULL);
410 }
411 
coresight_enable_path(struct list_head * path,u32 mode,void * sink_data)412 int coresight_enable_path(struct list_head *path, u32 mode, void *sink_data)
413 {
414 
415 	int ret = 0;
416 	u32 type;
417 	struct coresight_node *nd;
418 	struct coresight_device *csdev, *parent, *child;
419 
420 	list_for_each_entry_reverse(nd, path, link) {
421 		csdev = nd->csdev;
422 		type = csdev->type;
423 
424 		/*
425 		 * ETF devices are tricky... They can be a link or a sink,
426 		 * depending on how they are configured.  If an ETF has been
427 		 * "activated" it will be configured as a sink, otherwise
428 		 * go ahead with the link configuration.
429 		 */
430 		if (type == CORESIGHT_DEV_TYPE_LINKSINK)
431 			type = (csdev == coresight_get_sink(path)) ?
432 						CORESIGHT_DEV_TYPE_SINK :
433 						CORESIGHT_DEV_TYPE_LINK;
434 
435 		switch (type) {
436 		case CORESIGHT_DEV_TYPE_SINK:
437 			ret = coresight_enable_sink(csdev, mode, sink_data);
438 			/*
439 			 * Sink is the first component turned on. If we
440 			 * failed to enable the sink, there are no components
441 			 * that need disabling. Disabling the path here
442 			 * would mean we could disrupt an existing session.
443 			 */
444 			if (ret)
445 				goto out;
446 			break;
447 		case CORESIGHT_DEV_TYPE_SOURCE:
448 			/* sources are enabled from either sysFS or Perf */
449 			break;
450 		case CORESIGHT_DEV_TYPE_LINK:
451 			parent = list_prev_entry(nd, link)->csdev;
452 			child = list_next_entry(nd, link)->csdev;
453 			ret = coresight_enable_link(csdev, parent, child);
454 			if (ret)
455 				goto err;
456 			break;
457 		default:
458 			goto err;
459 		}
460 	}
461 
462 out:
463 	return ret;
464 err:
465 	coresight_disable_path_from(path, nd);
466 	goto out;
467 }
468 
coresight_get_sink(struct list_head * path)469 struct coresight_device *coresight_get_sink(struct list_head *path)
470 {
471 	struct coresight_device *csdev;
472 
473 	if (!path)
474 		return NULL;
475 
476 	csdev = list_last_entry(path, struct coresight_node, link)->csdev;
477 	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
478 	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
479 		return NULL;
480 
481 	return csdev;
482 }
483 
coresight_enabled_sink(struct device * dev,const void * data)484 static int coresight_enabled_sink(struct device *dev, const void *data)
485 {
486 	const bool *reset = data;
487 	struct coresight_device *csdev = to_coresight_device(dev);
488 
489 	if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
490 	     csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
491 	     csdev->activated) {
492 		/*
493 		 * Now that we have a handle on the sink for this session,
494 		 * disable the sysFS "enable_sink" flag so that possible
495 		 * concurrent perf session that wish to use another sink don't
496 		 * trip on it.  Doing so has no ramification for the current
497 		 * session.
498 		 */
499 		if (*reset)
500 			csdev->activated = false;
501 
502 		return 1;
503 	}
504 
505 	return 0;
506 }
507 
508 /**
509  * coresight_get_enabled_sink - returns the first enabled sink found on the bus
510  * @deactivate:	Whether the 'enable_sink' flag should be reset
511  *
512  * When operated from perf the deactivate parameter should be set to 'true'.
513  * That way the "enabled_sink" flag of the sink that was selected can be reset,
514  * allowing for other concurrent perf sessions to choose a different sink.
515  *
516  * When operated from sysFS users have full control and as such the deactivate
517  * parameter should be set to 'false', hence mandating users to explicitly
518  * clear the flag.
519  */
coresight_get_enabled_sink(bool deactivate)520 struct coresight_device *coresight_get_enabled_sink(bool deactivate)
521 {
522 	struct device *dev = NULL;
523 
524 	dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
525 			      coresight_enabled_sink);
526 
527 	return dev ? to_coresight_device(dev) : NULL;
528 }
529 
coresight_sink_by_id(struct device * dev,const void * data)530 static int coresight_sink_by_id(struct device *dev, const void *data)
531 {
532 	struct coresight_device *csdev = to_coresight_device(dev);
533 	unsigned long hash;
534 
535 	if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
536 	     csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
537 
538 		if (!csdev->ea)
539 			return 0;
540 		/*
541 		 * See function etm_perf_add_symlink_sink() to know where
542 		 * this comes from.
543 		 */
544 		hash = (unsigned long)csdev->ea->var;
545 
546 		if ((u32)hash == *(u32 *)data)
547 			return 1;
548 	}
549 
550 	return 0;
551 }
552 
553 /**
554  * coresight_get_sink_by_id - returns the sink that matches the id
555  * @id: Id of the sink to match
556  *
557  * The name of a sink is unique, whether it is found on the AMBA bus or
558  * otherwise.  As such the hash of that name can easily be used to identify
559  * a sink.
560  */
coresight_get_sink_by_id(u32 id)561 struct coresight_device *coresight_get_sink_by_id(u32 id)
562 {
563 	struct device *dev = NULL;
564 
565 	dev = bus_find_device(&coresight_bustype, NULL, &id,
566 			      coresight_sink_by_id);
567 
568 	return dev ? to_coresight_device(dev) : NULL;
569 }
570 
571 /*
572  * coresight_grab_device - Power up this device and any of the helper
573  * devices connected to it for trace operation. Since the helper devices
574  * don't appear on the trace path, they should be handled along with the
575  * the master device.
576  */
coresight_grab_device(struct coresight_device * csdev)577 static void coresight_grab_device(struct coresight_device *csdev)
578 {
579 	int i;
580 
581 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
582 		struct coresight_device *child;
583 
584 		child  = csdev->pdata->conns[i].child_dev;
585 		if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
586 			pm_runtime_get_sync(child->dev.parent);
587 	}
588 	pm_runtime_get_sync(csdev->dev.parent);
589 }
590 
591 /*
592  * coresight_drop_device - Release this device and any of the helper
593  * devices connected to it.
594  */
coresight_drop_device(struct coresight_device * csdev)595 static void coresight_drop_device(struct coresight_device *csdev)
596 {
597 	int i;
598 
599 	pm_runtime_put(csdev->dev.parent);
600 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
601 		struct coresight_device *child;
602 
603 		child  = csdev->pdata->conns[i].child_dev;
604 		if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
605 			pm_runtime_put(child->dev.parent);
606 	}
607 }
608 
609 /**
610  * _coresight_build_path - recursively build a path from a @csdev to a sink.
611  * @csdev:	The device to start from.
612  * @path:	The list to add devices to.
613  *
614  * The tree of Coresight device is traversed until an activated sink is
615  * found.  From there the sink is added to the list along with all the
616  * devices that led to that point - the end result is a list from source
617  * to sink. In that list the source is the first device and the sink the
618  * last one.
619  */
_coresight_build_path(struct coresight_device * csdev,struct coresight_device * sink,struct list_head * path)620 static int _coresight_build_path(struct coresight_device *csdev,
621 				 struct coresight_device *sink,
622 				 struct list_head *path)
623 {
624 	int i;
625 	bool found = false;
626 	struct coresight_node *node;
627 
628 	/* An activated sink has been found.  Enqueue the element */
629 	if (csdev == sink)
630 		goto out;
631 
632 	/* Not a sink - recursively explore each port found on this element */
633 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
634 		struct coresight_device *child_dev;
635 
636 		child_dev = csdev->pdata->conns[i].child_dev;
637 		if (child_dev &&
638 		    _coresight_build_path(child_dev, sink, path) == 0) {
639 			found = true;
640 			break;
641 		}
642 	}
643 
644 	if (!found)
645 		return -ENODEV;
646 
647 out:
648 	/*
649 	 * A path from this element to a sink has been found.  The elements
650 	 * leading to the sink are already enqueued, all that is left to do
651 	 * is tell the PM runtime core we need this element and add a node
652 	 * for it.
653 	 */
654 	node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
655 	if (!node)
656 		return -ENOMEM;
657 
658 	coresight_grab_device(csdev);
659 	node->csdev = csdev;
660 	list_add(&node->link, path);
661 
662 	return 0;
663 }
664 
coresight_build_path(struct coresight_device * source,struct coresight_device * sink)665 struct list_head *coresight_build_path(struct coresight_device *source,
666 				       struct coresight_device *sink)
667 {
668 	struct list_head *path;
669 	int rc;
670 
671 	if (!sink)
672 		return ERR_PTR(-EINVAL);
673 
674 	path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
675 	if (!path)
676 		return ERR_PTR(-ENOMEM);
677 
678 	INIT_LIST_HEAD(path);
679 
680 	rc = _coresight_build_path(source, sink, path);
681 	if (rc) {
682 		kfree(path);
683 		return ERR_PTR(rc);
684 	}
685 
686 	return path;
687 }
688 
689 /**
690  * coresight_release_path - release a previously built path.
691  * @path:	the path to release.
692  *
693  * Go through all the elements of a path and 1) removed it from the list and
694  * 2) free the memory allocated for each node.
695  */
coresight_release_path(struct list_head * path)696 void coresight_release_path(struct list_head *path)
697 {
698 	struct coresight_device *csdev;
699 	struct coresight_node *nd, *next;
700 
701 	list_for_each_entry_safe(nd, next, path, link) {
702 		csdev = nd->csdev;
703 
704 		coresight_drop_device(csdev);
705 		list_del(&nd->link);
706 		kfree(nd);
707 	}
708 
709 	kfree(path);
710 	path = NULL;
711 }
712 
713 /** coresight_validate_source - make sure a source has the right credentials
714  *  @csdev:	the device structure for a source.
715  *  @function:	the function this was called from.
716  *
717  * Assumes the coresight_mutex is held.
718  */
coresight_validate_source(struct coresight_device * csdev,const char * function)719 static int coresight_validate_source(struct coresight_device *csdev,
720 				     const char *function)
721 {
722 	u32 type, subtype;
723 
724 	type = csdev->type;
725 	subtype = csdev->subtype.source_subtype;
726 
727 	if (type != CORESIGHT_DEV_TYPE_SOURCE) {
728 		dev_err(&csdev->dev, "wrong device type in %s\n", function);
729 		return -EINVAL;
730 	}
731 
732 	if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
733 	    subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
734 		dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
735 		return -EINVAL;
736 	}
737 
738 	return 0;
739 }
740 
coresight_enable(struct coresight_device * csdev)741 int coresight_enable(struct coresight_device *csdev)
742 {
743 	int cpu, ret = 0;
744 	struct coresight_device *sink;
745 	struct list_head *path;
746 	enum coresight_dev_subtype_source subtype;
747 
748 	subtype = csdev->subtype.source_subtype;
749 
750 	mutex_lock(&coresight_mutex);
751 
752 	ret = coresight_validate_source(csdev, __func__);
753 	if (ret)
754 		goto out;
755 
756 	if (csdev->enable) {
757 		/*
758 		 * There could be multiple applications driving the software
759 		 * source. So keep the refcount for each such user when the
760 		 * source is already enabled.
761 		 */
762 		if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
763 			atomic_inc(csdev->refcnt);
764 		goto out;
765 	}
766 
767 	/*
768 	 * Search for a valid sink for this session but don't reset the
769 	 * "enable_sink" flag in sysFS.  Users get to do that explicitly.
770 	 */
771 	sink = coresight_get_enabled_sink(false);
772 	if (!sink) {
773 		ret = -EINVAL;
774 		goto out;
775 	}
776 
777 	path = coresight_build_path(csdev, sink);
778 	if (IS_ERR(path)) {
779 		pr_err("building path(s) failed\n");
780 		ret = PTR_ERR(path);
781 		goto out;
782 	}
783 
784 	ret = coresight_enable_path(path, CS_MODE_SYSFS, NULL);
785 	if (ret)
786 		goto err_path;
787 
788 	ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
789 	if (ret)
790 		goto err_source;
791 
792 	switch (subtype) {
793 	case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
794 		/*
795 		 * When working from sysFS it is important to keep track
796 		 * of the paths that were created so that they can be
797 		 * undone in 'coresight_disable()'.  Since there can only
798 		 * be a single session per tracer (when working from sysFS)
799 		 * a per-cpu variable will do just fine.
800 		 */
801 		cpu = source_ops(csdev)->cpu_id(csdev);
802 		per_cpu(tracer_path, cpu) = path;
803 		break;
804 	case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
805 		stm_path = path;
806 		break;
807 	default:
808 		/* We can't be here */
809 		break;
810 	}
811 
812 out:
813 	mutex_unlock(&coresight_mutex);
814 	return ret;
815 
816 err_source:
817 	coresight_disable_path(path);
818 
819 err_path:
820 	coresight_release_path(path);
821 	goto out;
822 }
823 EXPORT_SYMBOL_GPL(coresight_enable);
824 
coresight_disable(struct coresight_device * csdev)825 void coresight_disable(struct coresight_device *csdev)
826 {
827 	int cpu, ret;
828 	struct list_head *path = NULL;
829 
830 	mutex_lock(&coresight_mutex);
831 
832 	ret = coresight_validate_source(csdev, __func__);
833 	if (ret)
834 		goto out;
835 
836 	if (!csdev->enable || !coresight_disable_source(csdev))
837 		goto out;
838 
839 	switch (csdev->subtype.source_subtype) {
840 	case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
841 		cpu = source_ops(csdev)->cpu_id(csdev);
842 		path = per_cpu(tracer_path, cpu);
843 		per_cpu(tracer_path, cpu) = NULL;
844 		break;
845 	case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
846 		path = stm_path;
847 		stm_path = NULL;
848 		break;
849 	default:
850 		/* We can't be here */
851 		break;
852 	}
853 
854 	coresight_disable_path(path);
855 	coresight_release_path(path);
856 
857 out:
858 	mutex_unlock(&coresight_mutex);
859 }
860 EXPORT_SYMBOL_GPL(coresight_disable);
861 
enable_sink_show(struct device * dev,struct device_attribute * attr,char * buf)862 static ssize_t enable_sink_show(struct device *dev,
863 				struct device_attribute *attr, char *buf)
864 {
865 	struct coresight_device *csdev = to_coresight_device(dev);
866 
867 	return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
868 }
869 
enable_sink_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)870 static ssize_t enable_sink_store(struct device *dev,
871 				 struct device_attribute *attr,
872 				 const char *buf, size_t size)
873 {
874 	int ret;
875 	unsigned long val;
876 	struct coresight_device *csdev = to_coresight_device(dev);
877 
878 	ret = kstrtoul(buf, 10, &val);
879 	if (ret)
880 		return ret;
881 
882 	if (val)
883 		csdev->activated = true;
884 	else
885 		csdev->activated = false;
886 
887 	return size;
888 
889 }
890 static DEVICE_ATTR_RW(enable_sink);
891 
enable_source_show(struct device * dev,struct device_attribute * attr,char * buf)892 static ssize_t enable_source_show(struct device *dev,
893 				  struct device_attribute *attr, char *buf)
894 {
895 	struct coresight_device *csdev = to_coresight_device(dev);
896 
897 	return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
898 }
899 
enable_source_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)900 static ssize_t enable_source_store(struct device *dev,
901 				   struct device_attribute *attr,
902 				   const char *buf, size_t size)
903 {
904 	int ret = 0;
905 	unsigned long val;
906 	struct coresight_device *csdev = to_coresight_device(dev);
907 
908 	ret = kstrtoul(buf, 10, &val);
909 	if (ret)
910 		return ret;
911 
912 	if (val) {
913 		ret = coresight_enable(csdev);
914 		if (ret)
915 			return ret;
916 	} else {
917 		coresight_disable(csdev);
918 	}
919 
920 	return size;
921 }
922 static DEVICE_ATTR_RW(enable_source);
923 
924 static struct attribute *coresight_sink_attrs[] = {
925 	&dev_attr_enable_sink.attr,
926 	NULL,
927 };
928 ATTRIBUTE_GROUPS(coresight_sink);
929 
930 static struct attribute *coresight_source_attrs[] = {
931 	&dev_attr_enable_source.attr,
932 	NULL,
933 };
934 ATTRIBUTE_GROUPS(coresight_source);
935 
936 static struct device_type coresight_dev_type[] = {
937 	{
938 		.name = "none",
939 	},
940 	{
941 		.name = "sink",
942 		.groups = coresight_sink_groups,
943 	},
944 	{
945 		.name = "link",
946 	},
947 	{
948 		.name = "linksink",
949 		.groups = coresight_sink_groups,
950 	},
951 	{
952 		.name = "source",
953 		.groups = coresight_source_groups,
954 	},
955 	{
956 		.name = "helper",
957 	},
958 };
959 
coresight_device_release(struct device * dev)960 static void coresight_device_release(struct device *dev)
961 {
962 	struct coresight_device *csdev = to_coresight_device(dev);
963 
964 	fwnode_handle_put(csdev->dev.fwnode);
965 	kfree(csdev->refcnt);
966 	kfree(csdev);
967 }
968 
coresight_orphan_match(struct device * dev,void * data)969 static int coresight_orphan_match(struct device *dev, void *data)
970 {
971 	int i;
972 	bool still_orphan = false;
973 	struct coresight_device *csdev, *i_csdev;
974 	struct coresight_connection *conn;
975 
976 	csdev = data;
977 	i_csdev = to_coresight_device(dev);
978 
979 	/* No need to check oneself */
980 	if (csdev == i_csdev)
981 		return 0;
982 
983 	/* Move on to another component if no connection is orphan */
984 	if (!i_csdev->orphan)
985 		return 0;
986 	/*
987 	 * Circle throuch all the connection of that component.  If we find
988 	 * an orphan connection whose name matches @csdev, link it.
989 	 */
990 	for (i = 0; i < i_csdev->pdata->nr_outport; i++) {
991 		conn = &i_csdev->pdata->conns[i];
992 
993 		/* We have found at least one orphan connection */
994 		if (conn->child_dev == NULL) {
995 			/* Does it match this newly added device? */
996 			if (conn->child_fwnode == csdev->dev.fwnode)
997 				conn->child_dev = csdev;
998 			else
999 				/* This component still has an orphan */
1000 				still_orphan = true;
1001 		}
1002 	}
1003 
1004 	i_csdev->orphan = still_orphan;
1005 
1006 	/*
1007 	 * Returning '0' ensures that all known component on the
1008 	 * bus will be checked.
1009 	 */
1010 	return 0;
1011 }
1012 
coresight_fixup_orphan_conns(struct coresight_device * csdev)1013 static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
1014 {
1015 	/*
1016 	 * No need to check for a return value as orphan connection(s)
1017 	 * are hooked-up with each newly added component.
1018 	 */
1019 	bus_for_each_dev(&coresight_bustype, NULL,
1020 			 csdev, coresight_orphan_match);
1021 }
1022 
1023 
coresight_fixup_device_conns(struct coresight_device * csdev)1024 static void coresight_fixup_device_conns(struct coresight_device *csdev)
1025 {
1026 	int i;
1027 
1028 	for (i = 0; i < csdev->pdata->nr_outport; i++) {
1029 		struct coresight_connection *conn = &csdev->pdata->conns[i];
1030 		struct device *dev = NULL;
1031 
1032 		dev = bus_find_device_by_fwnode(&coresight_bustype, conn->child_fwnode);
1033 		if (dev) {
1034 			conn->child_dev = to_coresight_device(dev);
1035 			/* and put reference from 'bus_find_device()' */
1036 			put_device(dev);
1037 		} else {
1038 			csdev->orphan = true;
1039 			conn->child_dev = NULL;
1040 		}
1041 	}
1042 }
1043 
coresight_remove_match(struct device * dev,void * data)1044 static int coresight_remove_match(struct device *dev, void *data)
1045 {
1046 	int i;
1047 	struct coresight_device *csdev, *iterator;
1048 	struct coresight_connection *conn;
1049 
1050 	csdev = data;
1051 	iterator = to_coresight_device(dev);
1052 
1053 	/* No need to check oneself */
1054 	if (csdev == iterator)
1055 		return 0;
1056 
1057 	/*
1058 	 * Circle throuch all the connection of that component.  If we find
1059 	 * a connection whose name matches @csdev, remove it.
1060 	 */
1061 	for (i = 0; i < iterator->pdata->nr_outport; i++) {
1062 		conn = &iterator->pdata->conns[i];
1063 
1064 		if (conn->child_dev == NULL)
1065 			continue;
1066 
1067 		if (csdev->dev.fwnode == conn->child_fwnode) {
1068 			iterator->orphan = true;
1069 			conn->child_dev = NULL;
1070 			/*
1071 			 * Drop the reference to the handle for the remote
1072 			 * device acquired in parsing the connections from
1073 			 * platform data.
1074 			 */
1075 			fwnode_handle_put(conn->child_fwnode);
1076 			/* No need to continue */
1077 			break;
1078 		}
1079 	}
1080 
1081 	/*
1082 	 * Returning '0' ensures that all known component on the
1083 	 * bus will be checked.
1084 	 */
1085 	return 0;
1086 }
1087 
1088 /*
1089  * coresight_remove_conns - Remove references to this given devices
1090  * from the connections of other devices.
1091  */
coresight_remove_conns(struct coresight_device * csdev)1092 static void coresight_remove_conns(struct coresight_device *csdev)
1093 {
1094 	/*
1095 	 * Another device will point to this device only if there is
1096 	 * an output port connected to this one. i.e, if the device
1097 	 * doesn't have at least one input port, there is no point
1098 	 * in searching all the devices.
1099 	 */
1100 	if (csdev->pdata->nr_inport)
1101 		bus_for_each_dev(&coresight_bustype, NULL,
1102 				 csdev, coresight_remove_match);
1103 }
1104 
1105 /**
1106  * coresight_timeout - loop until a bit has changed to a specific state.
1107  * @addr: base address of the area of interest.
1108  * @offset: address of a register, starting from @addr.
1109  * @position: the position of the bit of interest.
1110  * @value: the value the bit should have.
1111  *
1112  * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
1113  * TIMEOUT_US has elapsed, which ever happens first.
1114  */
1115 
coresight_timeout(void __iomem * addr,u32 offset,int position,int value)1116 int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
1117 {
1118 	int i;
1119 	u32 val;
1120 
1121 	for (i = TIMEOUT_US; i > 0; i--) {
1122 		val = __raw_readl(addr + offset);
1123 		/* waiting on the bit to go from 0 to 1 */
1124 		if (value) {
1125 			if (val & BIT(position))
1126 				return 0;
1127 		/* waiting on the bit to go from 1 to 0 */
1128 		} else {
1129 			if (!(val & BIT(position)))
1130 				return 0;
1131 		}
1132 
1133 		/*
1134 		 * Delay is arbitrary - the specification doesn't say how long
1135 		 * we are expected to wait.  Extra check required to make sure
1136 		 * we don't wait needlessly on the last iteration.
1137 		 */
1138 		if (i - 1)
1139 			udelay(1);
1140 	}
1141 
1142 	return -EAGAIN;
1143 }
1144 
1145 struct bus_type coresight_bustype = {
1146 	.name	= "coresight",
1147 };
1148 
coresight_init(void)1149 static int __init coresight_init(void)
1150 {
1151 	return bus_register(&coresight_bustype);
1152 }
1153 postcore_initcall(coresight_init);
1154 
1155 /*
1156  * coresight_release_platform_data: Release references to the devices connected
1157  * to the output port of this device.
1158  */
coresight_release_platform_data(struct coresight_platform_data * pdata)1159 void coresight_release_platform_data(struct coresight_platform_data *pdata)
1160 {
1161 	int i;
1162 
1163 	for (i = 0; i < pdata->nr_outport; i++) {
1164 		if (pdata->conns[i].child_fwnode) {
1165 			fwnode_handle_put(pdata->conns[i].child_fwnode);
1166 			pdata->conns[i].child_fwnode = NULL;
1167 		}
1168 	}
1169 }
1170 
coresight_register(struct coresight_desc * desc)1171 struct coresight_device *coresight_register(struct coresight_desc *desc)
1172 {
1173 	int ret;
1174 	int link_subtype;
1175 	int nr_refcnts = 1;
1176 	atomic_t *refcnts = NULL;
1177 	struct coresight_device *csdev;
1178 
1179 	csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
1180 	if (!csdev) {
1181 		ret = -ENOMEM;
1182 		goto err_out;
1183 	}
1184 
1185 	if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
1186 	    desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1187 		link_subtype = desc->subtype.link_subtype;
1188 
1189 		if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
1190 			nr_refcnts = desc->pdata->nr_inport;
1191 		else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
1192 			nr_refcnts = desc->pdata->nr_outport;
1193 	}
1194 
1195 	refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
1196 	if (!refcnts) {
1197 		ret = -ENOMEM;
1198 		goto err_free_csdev;
1199 	}
1200 
1201 	csdev->refcnt = refcnts;
1202 
1203 	csdev->pdata = desc->pdata;
1204 
1205 	csdev->type = desc->type;
1206 	csdev->subtype = desc->subtype;
1207 	csdev->ops = desc->ops;
1208 	csdev->orphan = false;
1209 
1210 	csdev->dev.type = &coresight_dev_type[desc->type];
1211 	csdev->dev.groups = desc->groups;
1212 	csdev->dev.parent = desc->dev;
1213 	csdev->dev.release = coresight_device_release;
1214 	csdev->dev.bus = &coresight_bustype;
1215 	/*
1216 	 * Hold the reference to our parent device. This will be
1217 	 * dropped only in coresight_device_release().
1218 	 */
1219 	csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev));
1220 	dev_set_name(&csdev->dev, "%s", desc->name);
1221 
1222 	ret = device_register(&csdev->dev);
1223 	if (ret) {
1224 		put_device(&csdev->dev);
1225 		/*
1226 		 * All resources are free'd explicitly via
1227 		 * coresight_device_release(), triggered from put_device().
1228 		 */
1229 		goto err_out;
1230 	}
1231 
1232 	if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
1233 	    csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
1234 		ret = etm_perf_add_symlink_sink(csdev);
1235 
1236 		if (ret) {
1237 			device_unregister(&csdev->dev);
1238 			/*
1239 			 * As with the above, all resources are free'd
1240 			 * explicitly via coresight_device_release() triggered
1241 			 * from put_device(), which is in turn called from
1242 			 * function device_unregister().
1243 			 */
1244 			goto err_out;
1245 		}
1246 	}
1247 
1248 	mutex_lock(&coresight_mutex);
1249 
1250 	coresight_fixup_device_conns(csdev);
1251 	coresight_fixup_orphan_conns(csdev);
1252 
1253 	mutex_unlock(&coresight_mutex);
1254 
1255 	return csdev;
1256 
1257 err_free_csdev:
1258 	kfree(csdev);
1259 err_out:
1260 	/* Cleanup the connection information */
1261 	coresight_release_platform_data(desc->pdata);
1262 	return ERR_PTR(ret);
1263 }
1264 EXPORT_SYMBOL_GPL(coresight_register);
1265 
coresight_unregister(struct coresight_device * csdev)1266 void coresight_unregister(struct coresight_device *csdev)
1267 {
1268 	etm_perf_del_symlink_sink(csdev);
1269 	/* Remove references of that device in the topology */
1270 	coresight_remove_conns(csdev);
1271 	coresight_release_platform_data(csdev->pdata);
1272 	device_unregister(&csdev->dev);
1273 }
1274 EXPORT_SYMBOL_GPL(coresight_unregister);
1275 
1276 
1277 /*
1278  * coresight_search_device_idx - Search the fwnode handle of a device
1279  * in the given dev_idx list. Must be called with the coresight_mutex held.
1280  *
1281  * Returns the index of the entry, when found. Otherwise, -ENOENT.
1282  */
coresight_search_device_idx(struct coresight_dev_list * dict,struct fwnode_handle * fwnode)1283 static inline int coresight_search_device_idx(struct coresight_dev_list *dict,
1284 					      struct fwnode_handle *fwnode)
1285 {
1286 	int i;
1287 
1288 	for (i = 0; i < dict->nr_idx; i++)
1289 		if (dict->fwnode_list[i] == fwnode)
1290 			return i;
1291 	return -ENOENT;
1292 }
1293 
1294 /*
1295  * coresight_alloc_device_name - Get an index for a given device in the
1296  * device index list specific to a driver. An index is allocated for a
1297  * device and is tracked with the fwnode_handle to prevent allocating
1298  * duplicate indices for the same device (e.g, if we defer probing of
1299  * a device due to dependencies), in case the index is requested again.
1300  */
coresight_alloc_device_name(struct coresight_dev_list * dict,struct device * dev)1301 char *coresight_alloc_device_name(struct coresight_dev_list *dict,
1302 				  struct device *dev)
1303 {
1304 	int idx;
1305 	char *name = NULL;
1306 	struct fwnode_handle **list;
1307 
1308 	mutex_lock(&coresight_mutex);
1309 
1310 	idx = coresight_search_device_idx(dict, dev_fwnode(dev));
1311 	if (idx < 0) {
1312 		/* Make space for the new entry */
1313 		idx = dict->nr_idx;
1314 		list = krealloc(dict->fwnode_list,
1315 				(idx + 1) * sizeof(*dict->fwnode_list),
1316 				GFP_KERNEL);
1317 		if (ZERO_OR_NULL_PTR(list)) {
1318 			idx = -ENOMEM;
1319 			goto done;
1320 		}
1321 
1322 		list[idx] = dev_fwnode(dev);
1323 		dict->fwnode_list = list;
1324 		dict->nr_idx = idx + 1;
1325 	}
1326 
1327 	name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx);
1328 done:
1329 	mutex_unlock(&coresight_mutex);
1330 	return name;
1331 }
1332 EXPORT_SYMBOL_GPL(coresight_alloc_device_name);
1333