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