1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
4 * Description: CoreSight Trace Memory Controller driver
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/types.h>
10 #include <linux/device.h>
11 #include <linux/idr.h>
13 #include <linux/err.h>
15 #include <linux/miscdevice.h>
16 #include <linux/mutex.h>
17 #include <linux/property.h>
18 #include <linux/uaccess.h>
19 #include <linux/slab.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/spinlock.h>
22 #include <linux/pm_runtime.h>
24 #include <linux/coresight.h>
25 #include <linux/amba/bus.h>
27 #include "coresight-priv.h"
28 #include "coresight-tmc.h"
30 DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
31 DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
32 DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
34 void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
36 /* Ensure formatter, unformatter and hardware fifo are empty */
37 if (coresight_timeout(drvdata->base,
38 TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
39 dev_err(&drvdata->csdev->dev,
40 "timeout while waiting for TMC to be Ready\n");
44 void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
48 ffcr = readl_relaxed(drvdata->base + TMC_FFCR);
49 ffcr |= TMC_FFCR_STOP_ON_FLUSH;
50 writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
51 ffcr |= BIT(TMC_FFCR_FLUSHMAN_BIT);
52 writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
53 /* Ensure flush completes */
54 if (coresight_timeout(drvdata->base,
55 TMC_FFCR, TMC_FFCR_FLUSHMAN_BIT, 0)) {
56 dev_err(&drvdata->csdev->dev,
57 "timeout while waiting for completion of Manual Flush\n");
60 tmc_wait_for_tmcready(drvdata);
63 void tmc_enable_hw(struct tmc_drvdata *drvdata)
65 writel_relaxed(TMC_CTL_CAPT_EN, drvdata->base + TMC_CTL);
68 void tmc_disable_hw(struct tmc_drvdata *drvdata)
70 writel_relaxed(0x0, drvdata->base + TMC_CTL);
73 u32 tmc_get_memwidth_mask(struct tmc_drvdata *drvdata)
78 * When moving RRP or an offset address forward, the new values must
79 * be byte-address aligned to the width of the trace memory databus
80 * _and_ to a frame boundary (16 byte), whichever is the biggest. For
81 * example, for 32-bit, 64-bit and 128-bit wide trace memory, the four
82 * LSBs must be 0s. For 256-bit wide trace memory, the five LSBs must
85 switch (drvdata->memwidth) {
86 case TMC_MEM_INTF_WIDTH_32BITS:
87 case TMC_MEM_INTF_WIDTH_64BITS:
88 case TMC_MEM_INTF_WIDTH_128BITS:
89 mask = GENMASK(31, 4);
91 case TMC_MEM_INTF_WIDTH_256BITS:
92 mask = GENMASK(31, 5);
99 static int tmc_read_prepare(struct tmc_drvdata *drvdata)
103 switch (drvdata->config_type) {
104 case TMC_CONFIG_TYPE_ETB:
105 case TMC_CONFIG_TYPE_ETF:
106 ret = tmc_read_prepare_etb(drvdata);
108 case TMC_CONFIG_TYPE_ETR:
109 ret = tmc_read_prepare_etr(drvdata);
116 dev_dbg(&drvdata->csdev->dev, "TMC read start\n");
121 static int tmc_read_unprepare(struct tmc_drvdata *drvdata)
125 switch (drvdata->config_type) {
126 case TMC_CONFIG_TYPE_ETB:
127 case TMC_CONFIG_TYPE_ETF:
128 ret = tmc_read_unprepare_etb(drvdata);
130 case TMC_CONFIG_TYPE_ETR:
131 ret = tmc_read_unprepare_etr(drvdata);
138 dev_dbg(&drvdata->csdev->dev, "TMC read end\n");
143 static int tmc_open(struct inode *inode, struct file *file)
146 struct tmc_drvdata *drvdata = container_of(file->private_data,
147 struct tmc_drvdata, miscdev);
149 ret = tmc_read_prepare(drvdata);
153 nonseekable_open(inode, file);
155 dev_dbg(&drvdata->csdev->dev, "%s: successfully opened\n", __func__);
159 static inline ssize_t tmc_get_sysfs_trace(struct tmc_drvdata *drvdata,
160 loff_t pos, size_t len, char **bufpp)
162 switch (drvdata->config_type) {
163 case TMC_CONFIG_TYPE_ETB:
164 case TMC_CONFIG_TYPE_ETF:
165 return tmc_etb_get_sysfs_trace(drvdata, pos, len, bufpp);
166 case TMC_CONFIG_TYPE_ETR:
167 return tmc_etr_get_sysfs_trace(drvdata, pos, len, bufpp);
173 static ssize_t tmc_read(struct file *file, char __user *data, size_t len,
178 struct tmc_drvdata *drvdata = container_of(file->private_data,
179 struct tmc_drvdata, miscdev);
180 actual = tmc_get_sysfs_trace(drvdata, *ppos, len, &bufp);
184 if (copy_to_user(data, bufp, actual)) {
185 dev_dbg(&drvdata->csdev->dev,
186 "%s: copy_to_user failed\n", __func__);
191 dev_dbg(&drvdata->csdev->dev, "%zu bytes copied\n", actual);
196 static int tmc_release(struct inode *inode, struct file *file)
199 struct tmc_drvdata *drvdata = container_of(file->private_data,
200 struct tmc_drvdata, miscdev);
202 ret = tmc_read_unprepare(drvdata);
206 dev_dbg(&drvdata->csdev->dev, "%s: released\n", __func__);
210 static const struct file_operations tmc_fops = {
211 .owner = THIS_MODULE,
214 .release = tmc_release,
218 static enum tmc_mem_intf_width tmc_get_memwidth(u32 devid)
220 enum tmc_mem_intf_width memwidth;
223 * Excerpt from the TRM:
225 * DEVID::MEMWIDTH[10:8]
226 * 0x2 Memory interface databus is 32 bits wide.
227 * 0x3 Memory interface databus is 64 bits wide.
228 * 0x4 Memory interface databus is 128 bits wide.
229 * 0x5 Memory interface databus is 256 bits wide.
231 switch (BMVAL(devid, 8, 10)) {
233 memwidth = TMC_MEM_INTF_WIDTH_32BITS;
236 memwidth = TMC_MEM_INTF_WIDTH_64BITS;
239 memwidth = TMC_MEM_INTF_WIDTH_128BITS;
242 memwidth = TMC_MEM_INTF_WIDTH_256BITS;
251 #define coresight_tmc_reg(name, offset) \
252 coresight_simple_reg32(struct tmc_drvdata, name, offset)
253 #define coresight_tmc_reg64(name, lo_off, hi_off) \
254 coresight_simple_reg64(struct tmc_drvdata, name, lo_off, hi_off)
256 coresight_tmc_reg(rsz, TMC_RSZ);
257 coresight_tmc_reg(sts, TMC_STS);
258 coresight_tmc_reg(trg, TMC_TRG);
259 coresight_tmc_reg(ctl, TMC_CTL);
260 coresight_tmc_reg(ffsr, TMC_FFSR);
261 coresight_tmc_reg(ffcr, TMC_FFCR);
262 coresight_tmc_reg(mode, TMC_MODE);
263 coresight_tmc_reg(pscr, TMC_PSCR);
264 coresight_tmc_reg(axictl, TMC_AXICTL);
265 coresight_tmc_reg(authstatus, TMC_AUTHSTATUS);
266 coresight_tmc_reg(devid, CORESIGHT_DEVID);
267 coresight_tmc_reg64(rrp, TMC_RRP, TMC_RRPHI);
268 coresight_tmc_reg64(rwp, TMC_RWP, TMC_RWPHI);
269 coresight_tmc_reg64(dba, TMC_DBALO, TMC_DBAHI);
271 static struct attribute *coresight_tmc_mgmt_attrs[] = {
282 &dev_attr_devid.attr,
284 &dev_attr_axictl.attr,
285 &dev_attr_authstatus.attr,
289 static ssize_t trigger_cntr_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
292 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
293 unsigned long val = drvdata->trigger_cntr;
295 return sprintf(buf, "%#lx\n", val);
298 static ssize_t trigger_cntr_store(struct device *dev,
299 struct device_attribute *attr,
300 const char *buf, size_t size)
304 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
306 ret = kstrtoul(buf, 16, &val);
310 drvdata->trigger_cntr = val;
313 static DEVICE_ATTR_RW(trigger_cntr);
315 static ssize_t buffer_size_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
320 return sprintf(buf, "%#x\n", drvdata->size);
323 static ssize_t buffer_size_store(struct device *dev,
324 struct device_attribute *attr,
325 const char *buf, size_t size)
329 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
331 /* Only permitted for TMC-ETRs */
332 if (drvdata->config_type != TMC_CONFIG_TYPE_ETR)
335 ret = kstrtoul(buf, 0, &val);
338 /* The buffer size should be page aligned */
339 if (val & (PAGE_SIZE - 1))
345 static DEVICE_ATTR_RW(buffer_size);
347 static struct attribute *coresight_tmc_attrs[] = {
348 &dev_attr_trigger_cntr.attr,
349 &dev_attr_buffer_size.attr,
353 static const struct attribute_group coresight_tmc_group = {
354 .attrs = coresight_tmc_attrs,
357 static const struct attribute_group coresight_tmc_mgmt_group = {
358 .attrs = coresight_tmc_mgmt_attrs,
362 static const struct attribute_group *coresight_tmc_groups[] = {
363 &coresight_tmc_group,
364 &coresight_tmc_mgmt_group,
368 static inline bool tmc_etr_can_use_sg(struct device *dev)
370 return fwnode_property_present(dev->fwnode, "arm,scatter-gather");
373 static inline bool tmc_etr_has_non_secure_access(struct tmc_drvdata *drvdata)
375 u32 auth = readl_relaxed(drvdata->base + TMC_AUTHSTATUS);
377 return (auth & TMC_AUTH_NSID_MASK) == 0x3;
380 /* Detect and initialise the capabilities of a TMC ETR */
381 static int tmc_etr_setup_caps(struct device *parent, u32 devid, void *dev_caps)
385 struct tmc_drvdata *drvdata = dev_get_drvdata(parent);
387 if (!tmc_etr_has_non_secure_access(drvdata))
390 /* Set the unadvertised capabilities */
391 tmc_etr_init_caps(drvdata, (u32)(unsigned long)dev_caps);
393 if (!(devid & TMC_DEVID_NOSCAT) && tmc_etr_can_use_sg(parent))
394 tmc_etr_set_cap(drvdata, TMC_ETR_SG);
396 /* Check if the AXI address width is available */
397 if (devid & TMC_DEVID_AXIAW_VALID)
398 dma_mask = ((devid >> TMC_DEVID_AXIAW_SHIFT) &
399 TMC_DEVID_AXIAW_MASK);
402 * Unless specified in the device configuration, ETR uses a 40-bit
403 * AXI master in place of the embedded SRAM of ETB/ETF.
411 dev_info(parent, "Detected dma mask %dbits\n", dma_mask);
417 rc = dma_set_mask_and_coherent(parent, DMA_BIT_MASK(dma_mask));
419 dev_err(parent, "Failed to setup DMA mask: %d\n", rc);
423 static u32 tmc_etr_get_default_buffer_size(struct device *dev)
427 if (fwnode_property_read_u32(dev->fwnode, "arm,buffer-size", &size))
432 static int tmc_probe(struct amba_device *adev, const struct amba_id *id)
437 struct device *dev = &adev->dev;
438 struct coresight_platform_data *pdata = NULL;
439 struct tmc_drvdata *drvdata;
440 struct resource *res = &adev->res;
441 struct coresight_desc desc = { 0 };
442 struct coresight_dev_list *dev_list = NULL;
445 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
449 dev_set_drvdata(dev, drvdata);
451 /* Validity for the resource is already checked by the AMBA core */
452 base = devm_ioremap_resource(dev, res);
458 drvdata->base = base;
460 spin_lock_init(&drvdata->spinlock);
462 devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
463 drvdata->config_type = BMVAL(devid, 6, 7);
464 drvdata->memwidth = tmc_get_memwidth(devid);
465 /* This device is not associated with a session */
468 if (drvdata->config_type == TMC_CONFIG_TYPE_ETR)
469 drvdata->size = tmc_etr_get_default_buffer_size(dev);
471 drvdata->size = readl_relaxed(drvdata->base + TMC_RSZ) * 4;
474 desc.groups = coresight_tmc_groups;
476 switch (drvdata->config_type) {
477 case TMC_CONFIG_TYPE_ETB:
478 desc.type = CORESIGHT_DEV_TYPE_SINK;
479 desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
480 desc.ops = &tmc_etb_cs_ops;
481 dev_list = &etb_devs;
483 case TMC_CONFIG_TYPE_ETR:
484 desc.type = CORESIGHT_DEV_TYPE_SINK;
485 desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_SYSMEM;
486 desc.ops = &tmc_etr_cs_ops;
487 ret = tmc_etr_setup_caps(dev, devid,
488 coresight_get_uci_data(id));
491 idr_init(&drvdata->idr);
492 mutex_init(&drvdata->idr_mutex);
493 dev_list = &etr_devs;
495 case TMC_CONFIG_TYPE_ETF:
496 desc.type = CORESIGHT_DEV_TYPE_LINKSINK;
497 desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
498 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_FIFO;
499 desc.ops = &tmc_etf_cs_ops;
500 dev_list = &etf_devs;
503 pr_err("%s: Unsupported TMC config\n", desc.name);
508 desc.name = coresight_alloc_device_name(dev_list, dev);
514 pdata = coresight_get_platform_data(dev);
516 ret = PTR_ERR(pdata);
519 adev->dev.platform_data = pdata;
522 drvdata->csdev = coresight_register(&desc);
523 if (IS_ERR(drvdata->csdev)) {
524 ret = PTR_ERR(drvdata->csdev);
528 drvdata->miscdev.name = desc.name;
529 drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
530 drvdata->miscdev.fops = &tmc_fops;
531 ret = misc_register(&drvdata->miscdev);
533 coresight_unregister(drvdata->csdev);
535 pm_runtime_put(&adev->dev);
540 static void tmc_shutdown(struct amba_device *adev)
543 struct tmc_drvdata *drvdata = amba_get_drvdata(adev);
545 spin_lock_irqsave(&drvdata->spinlock, flags);
547 if (drvdata->mode == CS_MODE_DISABLED)
550 if (drvdata->config_type == TMC_CONFIG_TYPE_ETR)
551 tmc_etr_disable_hw(drvdata);
554 * We do not care about coresight unregister here unlike remove
555 * callback which is required for making coresight modular since
556 * the system is going down after this.
559 spin_unlock_irqrestore(&drvdata->spinlock, flags);
562 static void tmc_remove(struct amba_device *adev)
564 struct tmc_drvdata *drvdata = dev_get_drvdata(&adev->dev);
567 * Since misc_open() holds a refcount on the f_ops, which is
568 * etb fops in this case, device is there until last file
569 * handler to this device is closed.
571 misc_deregister(&drvdata->miscdev);
572 coresight_unregister(drvdata->csdev);
575 static const struct amba_id tmc_ids[] = {
576 CS_AMBA_ID(0x000bb961),
577 /* Coresight SoC 600 TMC-ETR/ETS */
578 CS_AMBA_ID_DATA(0x000bb9e8, (unsigned long)CORESIGHT_SOC_600_ETR_CAPS),
579 /* Coresight SoC 600 TMC-ETB */
580 CS_AMBA_ID(0x000bb9e9),
581 /* Coresight SoC 600 TMC-ETF */
582 CS_AMBA_ID(0x000bb9ea),
586 MODULE_DEVICE_TABLE(amba, tmc_ids);
588 static struct amba_driver tmc_driver = {
590 .name = "coresight-tmc",
591 .owner = THIS_MODULE,
592 .suppress_bind_attrs = true,
595 .shutdown = tmc_shutdown,
596 .remove = tmc_remove,
600 module_amba_driver(tmc_driver);
602 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
603 MODULE_DESCRIPTION("Arm CoreSight Trace Memory Controller driver");
604 MODULE_LICENSE("GPL v2");