Debian Codegraph @

drivers/hwtracing/coresight/coresight-tpda.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2023-2025 Qualcomm Innovation Center, Inc. All rights reserved.
4 */
5
6 #include <linux/amba/bus.h>
7 #include <linux/bitfield.h>
8 #include <linux/coresight.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/fs.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17
18 #include "coresight-priv.h"
19 #include "coresight-tpda.h"
20 #include "coresight-trace-id.h"
21 #include "coresight-tpdm.h"
22
23 DEFINE_CORESIGHT_DEVLIST(tpda_devs, "tpda");
24
25 static void tpda_clear_element_size(struct coresight_device *csdev)
26 {
27 struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
28
29 drvdata->dsb_esize = 0;
30 drvdata->cmb_esize = 0;
31 }
32
33 static void tpda_set_element_size(struct tpda_drvdata *drvdata, u32 *val)
34 {
35 /* Clear all relevant fields */
36 *val &= ~(TPDA_Pn_CR_DSBSIZE | TPDA_Pn_CR_CMBSIZE);
37
38 if (drvdata->dsb_esize == 64)
39 *val |= TPDA_Pn_CR_DSBSIZE;
40 else if (drvdata->dsb_esize == 32)
41 *val &= ~TPDA_Pn_CR_DSBSIZE;
42
43 if (drvdata->cmb_esize == 64)
44 *val |= FIELD_PREP(TPDA_Pn_CR_CMBSIZE, 0x2);
45 else if (drvdata->cmb_esize == 32)
46 *val |= FIELD_PREP(TPDA_Pn_CR_CMBSIZE, 0x1);
47 else if (drvdata->cmb_esize == 8)
48 *val &= ~TPDA_Pn_CR_CMBSIZE;
49 }
50
51 /*
52 * Read the element size from the TPDM device. One TPDM must have at least one of the
53 * element size property.
54 * Returns
55 * 0 - The element size property is read
56 * Others - Cannot read the property of the element size
57 */
58 static int tpdm_read_element_size(struct tpda_drvdata *drvdata,
59 struct coresight_device *csdev)
60 {
61 int rc = -EINVAL;
62 struct tpdm_drvdata *tpdm_data = dev_get_drvdata(csdev->dev.parent);
63
64 if (tpdm_data->dsb) {
65 rc = fwnode_property_read_u32(dev_fwnode(csdev->dev.parent),
66 "qcom,dsb-element-bits", &drvdata->dsb_esize);
67 if (rc)
68 goto out;
69 }
70
71 if (tpdm_data->cmb) {
72 rc = fwnode_property_read_u32(dev_fwnode(csdev->dev.parent),
73 "qcom,cmb-element-bits", &drvdata->cmb_esize);
74 }
75
76 out:
77 if (rc)
78 dev_warn_once(&csdev->dev,
79 "Failed to read TPDM Element size: %d\n", rc);
80
81 return rc;
82 }
83
84 /*
85 * Search and read element data size from the TPDM node in
86 * the devicetree. Each input port of TPDA is connected to
87 * a TPDM. Different TPDM supports different types of dataset,
88 * and some may support more than one type of dataset.
89 * Parameter "inport" is used to pass in the input port number
90 * of TPDA, and it is set to -1 in the recursize call.
91 */
92 static int tpda_get_element_size(struct tpda_drvdata *drvdata,
93 struct coresight_device *csdev,
94 int inport)
95 {
96 int rc = 0;
97 int i;
98 struct coresight_device *in;
99
100 for (i = 0; i < csdev->pdata->nr_inconns; i++) {
101 in = csdev->pdata->in_conns[i]->src_dev;
102 if (!in)
103 continue;
104
105 /* Ignore the paths that do not match port */
106 if (inport >= 0 &&
107 csdev->pdata->in_conns[i]->dest_port != inport)
108 continue;
109
110 /*
111 * If this port has a hardcoded filter, use the source
112 * device directly.
113 */
114 if (csdev->pdata->in_conns[i]->filter_src_fwnode) {
115 in = csdev->pdata->in_conns[i]->filter_src_dev;
116 if (!in)
117 continue;
118 }
119
120 if (coresight_device_is_tpdm(in)) {
121 if (drvdata->dsb_esize || drvdata->cmb_esize)
122 return -EEXIST;
123 rc = tpdm_read_element_size(drvdata, in);
124 if (rc)
125 return rc;
126 } else {
127 /* Recurse down the path */
128 rc = tpda_get_element_size(drvdata, in, -1);
129 if (rc)
130 return rc;
131 }
132 }
133
134 return rc;
135 }
136
137 /* Settings pre enabling port control register */
138 static void tpda_enable_pre_port(struct tpda_drvdata *drvdata)
139 {
140 u32 val = 0;
141
142 val |= FIELD_PREP(TPDA_CR_ATID, drvdata->atid);
143 if (drvdata->trig_async)
144 val |= TPDA_CR_SRIE;
145
146 if (drvdata->trig_flag_ts)
147 val |= TPDA_CR_FLRIE;
148
149 if (drvdata->trig_freq)
150 val |= TPDA_CR_FRIE;
151
152 if (drvdata->freq_ts)
153 val |= TPDA_CR_FREQTS;
154
155 if (drvdata->cmbchan_mode)
156 val |= TPDA_CR_CMBCHANMODE;
157
158 writel_relaxed(val, drvdata->base + TPDA_CR);
159
160 /*
161 * If FLRIE bit is set, set the master and channel
162 * id as zero
163 */
164 if (drvdata->trig_flag_ts)
165 writel_relaxed(0x0, drvdata->base + TPDA_FPID_CR);
166
167 /* Initialize with a value of 0 */
168 val = 0;
169 if (drvdata->syncr_mode)
170 val |= TPDA_SYNCR_MODE_CTRL_MASK;
171
172 if (drvdata->syncr_count > 0 &&
173 drvdata->syncr_count < TPDA_SYNCR_COUNT_MASK)
174 val |= drvdata->syncr_count;
175 else
176 /* Program the count to its MAX value by default */
177 val |= TPDA_SYNCR_COUNT_MASK;
178
179 writel_relaxed(val, drvdata->base + TPDA_SYNCR);
180 }
181
182 static int tpda_enable_port(struct tpda_drvdata *drvdata, int port)
183 {
184 u32 val;
185 int rc;
186
187 val = readl_relaxed(drvdata->base + TPDA_Pn_CR(port));
188 tpda_clear_element_size(drvdata->csdev);
189 rc = tpda_get_element_size(drvdata, drvdata->csdev, port);
190 if (!rc && (drvdata->dsb_esize || drvdata->cmb_esize)) {
191 tpda_set_element_size(drvdata, &val);
192 /* Enable the port */
193 val |= TPDA_Pn_CR_ENA;
194 writel_relaxed(val, drvdata->base + TPDA_Pn_CR(port));
195 } else if (rc == -EEXIST)
196 dev_warn_once(&drvdata->csdev->dev,
197 "Detected multiple TPDMs on port %d", port);
198 else
199 dev_warn_once(&drvdata->csdev->dev,
200 "Didn't find TPDM element size");
201
202 return rc;
203 }
204
205 static int __tpda_enable(struct tpda_drvdata *drvdata, int port)
206 {
207 int ret;
208
209 CS_UNLOCK(drvdata->base);
210
211 /*
212 * Only do pre-port enable for first port that calls enable when the
213 * device's main refcount is still 0
214 */
215 lockdep_assert_held(&drvdata->spinlock);
216 if (!drvdata->csdev->refcnt)
217 tpda_enable_pre_port(drvdata);
218
219 ret = tpda_enable_port(drvdata, port);
220 CS_LOCK(drvdata->base);
221
222 return ret;
223 }
224
225 static int tpda_enable(struct coresight_device *csdev,
226 struct coresight_connection *in,
227 struct coresight_connection *out)
228 {
229 struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
230 int ret = 0;
231
232 spin_lock(&drvdata->spinlock);
233 if (in->dest_refcnt == 0) {
234 ret = __tpda_enable(drvdata, in->dest_port);
235 if (!ret) {
236 in->dest_refcnt++;
237 csdev->refcnt++;
238 dev_dbg(drvdata->dev, "TPDA inport %d enabled.\n", in->dest_port);
239 }
240 }
241
242 spin_unlock(&drvdata->spinlock);
243 return ret;
244 }
245
246 static void __tpda_disable(struct tpda_drvdata *drvdata, int port)
247 {
248 u32 val;
249
250 CS_UNLOCK(drvdata->base);
251
252 val = readl_relaxed(drvdata->base + TPDA_Pn_CR(port));
253 val &= ~TPDA_Pn_CR_ENA;
254 writel_relaxed(val, drvdata->base + TPDA_Pn_CR(port));
255
256 CS_LOCK(drvdata->base);
257 }
258
259 static void tpda_disable(struct coresight_device *csdev,
260 struct coresight_connection *in,
261 struct coresight_connection *out)
262 {
263 struct tpda_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
264
265 spin_lock(&drvdata->spinlock);
266 if (--in->dest_refcnt == 0) {
267 __tpda_disable(drvdata, in->dest_port);
268 csdev->refcnt--;
269 }
270 spin_unlock(&drvdata->spinlock);
271
272 dev_dbg(drvdata->dev, "TPDA inport %d disabled\n", in->dest_port);
273 }
274
275 static int tpda_trace_id(struct coresight_device *csdev, __maybe_unused enum cs_mode mode,
276 __maybe_unused struct coresight_device *sink)
277 {
278 struct tpda_drvdata *drvdata;
279
280 drvdata = dev_get_drvdata(csdev->dev.parent);
281
282 return drvdata->atid;
283 }
284
285 static const struct coresight_ops_link tpda_link_ops = {
286 .enable = tpda_enable,
287 .disable = tpda_disable,
288 };
289
290 static const struct coresight_ops tpda_cs_ops = {
291 .trace_id = tpda_trace_id,
292 .link_ops = &tpda_link_ops,
293 };
294
295 /* Read cross-trigger register member */
296 static ssize_t tpda_trig_sysfs_show(struct device *dev,
297 struct device_attribute *attr,
298 char *buf)
299 {
300 struct tpda_trig_sysfs_attribute *tpda_attr =
301 container_of(attr, struct tpda_trig_sysfs_attribute, attr);
302 struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
303
304 guard(spinlock)(&drvdata->spinlock);
305 switch (tpda_attr->mem) {
306 case FREQTS:
307 return sysfs_emit(buf, "%u\n", (unsigned int)drvdata->freq_ts);
308 case FRIE:
309 return sysfs_emit(buf, "%u\n", (unsigned int)drvdata->trig_freq);
310 case FLRIE:
311 return sysfs_emit(buf, "%u\n", (unsigned int)drvdata->trig_flag_ts);
312 case SRIE:
313 return sysfs_emit(buf, "%u\n", (unsigned int)drvdata->trig_async);
314 case CMBCHANMODE:
315 return sysfs_emit(buf, "%u\n", (unsigned int)drvdata->cmbchan_mode);
316
317 }
318 return -EINVAL;
319 }
320
321 static ssize_t tpda_trig_sysfs_store(struct device *dev,
322 struct device_attribute *attr,
323 const char *buf,
324 size_t size)
325 {
326 struct tpda_trig_sysfs_attribute *tpda_attr =
327 container_of(attr, struct tpda_trig_sysfs_attribute, attr);
328 struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
329 unsigned long val;
330
331 if (kstrtoul(buf, 0, &val))
332 return -EINVAL;
333
334 guard(spinlock)(&drvdata->spinlock);
335 switch (tpda_attr->mem) {
336 case FREQTS:
337 drvdata->freq_ts = !!val;
338 break;
339 case FRIE:
340 drvdata->trig_freq = !!val;
341 break;
342 case FLRIE:
343 drvdata->trig_flag_ts = !!val;
344 break;
345 case SRIE:
346 drvdata->trig_async = !!val;
347 break;
348 case CMBCHANMODE:
349 drvdata->cmbchan_mode = !!val;
350 break;
351 default:
352 return -EINVAL;
353 }
354
355 return size;
356 }
357
358 static ssize_t global_flush_req_show(struct device *dev,
359 struct device_attribute *attr,
360 char *buf)
361 {
362 struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
363 unsigned long val;
364
365 if (!drvdata->csdev->refcnt)
366 return -EINVAL;
367
368 guard(spinlock)(&drvdata->spinlock);
369 val = readl_relaxed(drvdata->base + TPDA_CR);
370 /* read global_flush_req bit */
371 val &= TPDA_CR_FLREQ;
372
373 return sysfs_emit(buf, "%lu\n", val);
374 }
375
376 static ssize_t global_flush_req_store(struct device *dev,
377 struct device_attribute *attr,
378 const char *buf,
379 size_t size)
380 {
381 struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
382 unsigned long val;
383
384 if (kstrtoul(buf, 0, &val))
385 return -EINVAL;
386
387 if (!drvdata->csdev->refcnt || !val)
388 return -EINVAL;
389
390 guard(spinlock)(&drvdata->spinlock);
391 val = readl_relaxed(drvdata->base + TPDA_CR);
392 /* set global_flush_req bit */
393 val |= TPDA_CR_FLREQ;
394 CS_UNLOCK(drvdata->base);
395 writel_relaxed(val, drvdata->base + TPDA_CR);
396 CS_LOCK(drvdata->base);
397
398 return size;
399 }
400 static DEVICE_ATTR_RW(global_flush_req);
401
402 static ssize_t syncr_mode_show(struct device *dev,
403 struct device_attribute *attr,
404 char *buf)
405 {
406 struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
407 unsigned long val, syncr_val;
408
409 if (!drvdata->csdev->refcnt)
410 return -EINVAL;
411
412 guard(spinlock)(&drvdata->spinlock);
413 syncr_val = readl_relaxed(drvdata->base + TPDA_SYNCR);
414 val = FIELD_GET(TPDA_SYNCR_MODE_CTRL_MASK, syncr_val);
415
416 return sysfs_emit(buf, "%lu\n", val);
417 }
418
419 static ssize_t syncr_mode_store(struct device *dev,
420 struct device_attribute *attr,
421 const char *buf,
422 size_t size)
423 {
424 struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
425 unsigned long val;
426
427 if (kstrtoul(buf, 0, &val))
428 return -EINVAL;
429
430 guard(spinlock)(&drvdata->spinlock);
431 /* set the mode when first enabling the device */
432 drvdata->syncr_mode = !!val;
433
434 return size;
435 }
436 static DEVICE_ATTR_RW(syncr_mode);
437
438 static ssize_t syncr_count_show(struct device *dev,
439 struct device_attribute *attr,
440 char *buf)
441 {
442 struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
443 unsigned long val;
444
445 if (!drvdata->csdev->refcnt)
446 return -EINVAL;
447
448 guard(spinlock)(&drvdata->spinlock);
449 val = readl_relaxed(drvdata->base + TPDA_SYNCR);
450 val &= TPDA_SYNCR_COUNT_MASK;
451
452 return sysfs_emit(buf, "%lu\n", val);
453 }
454
455 static ssize_t syncr_count_store(struct device *dev,
456 struct device_attribute *attr,
457 const char *buf,
458 size_t size)
459 {
460 struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
461 unsigned long val;
462
463 if (kstrtoul(buf, 0, &val))
464 return -EINVAL;
465
466 if (val > TPDA_SYNCR_COUNT_MASK)
467 return -EINVAL;
468
469 guard(spinlock)(&drvdata->spinlock);
470 drvdata->syncr_count = val;
471
472 return size;
473 }
474 static DEVICE_ATTR_RW(syncr_count);
475
476 static ssize_t port_flush_req_show(struct device *dev,
477 struct device_attribute *attr,
478 char *buf)
479 {
480 struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
481 unsigned long val;
482
483 if (!drvdata->csdev->refcnt)
484 return -EINVAL;
485
486 guard(spinlock)(&drvdata->spinlock);
487 val = readl_relaxed(drvdata->base + TPDA_FLUSH_CR);
488
489 return sysfs_emit(buf, "0x%lx\n", val);
490 }
491
492 static ssize_t port_flush_req_store(struct device *dev,
493 struct device_attribute *attr,
494 const char *buf,
495 size_t size)
496 {
497 struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
498 u32 val;
499
500 if (kstrtou32(buf, 0, &val))
501 return -EINVAL;
502
503 if (!drvdata->csdev->refcnt || !val)
504 return -EINVAL;
505
506 guard(spinlock)(&drvdata->spinlock);
507 CS_UNLOCK(drvdata->base);
508 writel_relaxed(val, drvdata->base + TPDA_FLUSH_CR);
509 CS_LOCK(drvdata->base);
510
511 return size;
512 }
513 static DEVICE_ATTR_RW(port_flush_req);
514
515 static struct attribute *tpda_attrs[] = {
516 &dev_attr_global_flush_req.attr,
517 &dev_attr_syncr_mode.attr,
518 &dev_attr_syncr_count.attr,
519 &dev_attr_port_flush_req.attr,
520 tpda_trig_sysfs_rw(freq_ts_enable, FREQTS),
521 tpda_trig_sysfs_rw(trig_freq_enable, FRIE),
522 tpda_trig_sysfs_rw(trig_flag_ts_enable, FLRIE),
523 tpda_trig_sysfs_rw(trig_async_enable, SRIE),
524 tpda_trig_sysfs_rw(cmbchan_mode, CMBCHANMODE),
525 NULL,
526 };
527
528 static struct attribute_group tpda_attr_grp = {
529 .attrs = tpda_attrs,
530 };
531
532 static const struct attribute_group *tpda_attr_grps[] = {
533 &tpda_attr_grp,
534 NULL,
535 };
536
537 static int tpda_init_default_data(struct tpda_drvdata *drvdata)
538 {
539 int atid;
540 /*
541 * TPDA must has a unique atid. This atid can uniquely
542 * identify the TPDM trace source connected to the TPDA.
543 * The TPDMs which are connected to same TPDA share the
544 * same trace-id. When TPDA does packetization, different
545 * port will have unique channel number for decoding.
546 */
547 atid = coresight_trace_id_get_system_id();
548 if (atid < 0)
549 return atid;
550
551 drvdata->atid = atid;
552 drvdata->freq_ts = true;
553 return 0;
554 }
555
556 static int tpda_probe(struct amba_device *adev, const struct amba_id *id)
557 {
558 int ret;
559 struct device *dev = &adev->dev;
560 struct coresight_platform_data *pdata;
561 struct tpda_drvdata *drvdata;
562 struct coresight_desc desc = { 0 };
563 void __iomem *base;
564
565 pdata = coresight_get_platform_data(dev);
566 if (IS_ERR(pdata))
567 return PTR_ERR(pdata);
568 adev->dev.platform_data = pdata;
569
570 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
571 if (!drvdata)
572 return -ENOMEM;
573
574 drvdata->dev = &adev->dev;
575 dev_set_drvdata(dev, drvdata);
576
577 base = devm_ioremap_resource(dev, &adev->res);
578 if (IS_ERR(base))
579 return PTR_ERR(base);
580 drvdata->base = base;
581
582 spin_lock_init(&drvdata->spinlock);
583
584 ret = tpda_init_default_data(drvdata);
585 if (ret)
586 return ret;
587
588 desc.name = coresight_alloc_device_name(&tpda_devs, dev);
589 if (!desc.name)
590 return -ENOMEM;
591 desc.type = CORESIGHT_DEV_TYPE_LINK;
592 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
593 desc.ops = &tpda_cs_ops;
594 desc.pdata = adev->dev.platform_data;
595 desc.dev = &adev->dev;
596 desc.groups = tpda_attr_grps;
597 desc.access = CSDEV_ACCESS_IOMEM(base);
598 drvdata->csdev = coresight_register(&desc);
599 if (IS_ERR(drvdata->csdev))
600 return PTR_ERR(drvdata->csdev);
601
602 pm_runtime_put(&adev->dev);
603
604 dev_dbg(drvdata->dev, "TPDA initialized\n");
605 return 0;
606 }
607
608 static void tpda_remove(struct amba_device *adev)
609 {
610 struct tpda_drvdata *drvdata = dev_get_drvdata(&adev->dev);
611
612 coresight_trace_id_put_system_id(drvdata->atid);
613 coresight_unregister(drvdata->csdev);
614 }
615
616 /*
617 * Different TPDA has different periph id.
618 * The difference is 0-7 bits' value. So ignore 0-7 bits.
619 */
620 static const struct amba_id tpda_ids[] = {
621 {
622 .id = 0x000f0f00,
623 .mask = 0x000fff00,
624 },
625 { 0, 0, NULL },
626 };
627
628 static struct amba_driver tpda_driver = {
629 .drv = {
630 .name = "coresight-tpda",
631 .suppress_bind_attrs = true,
632 },
633 .probe = tpda_probe,
634 .remove = tpda_remove,
635 .id_table = tpda_ids,
636 };
637
638 module_amba_driver(tpda_driver);
639
640 MODULE_LICENSE("GPL");
641 MODULE_DESCRIPTION("Trace, Profiling & Diagnostic Aggregator driver");