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