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