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