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